blob: ba41ed25a74b9719e824f06929695be7bce84945 [file] [log] [blame]
Edward O'Callaghan97dd9262020-03-26 00:00:41 +11001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2020 The Chromium OS Authors
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#include <stdlib.h>
18#include <stdint.h>
19#include <stdio.h>
20#include <string.h>
21#include <time.h>
22#include <errno.h>
23
24#include "programmer.h"
25#include "spi.h"
26#include "i2c_helper.h"
27
28
29#define MCU_I2C_SLAVE_ADDR 0x94
30#define REGISTER_ADDRESS (0x94 >> 1)
31#define PAGE_SIZE 256
32#define MAX_SPI_WAIT_RETRIES 1000
33
34//opcodes
35#define OPCODE_READ 3
36#define OPCODE_WRITE 2
37
38
39struct realtek_mst_i2c_spi_data {
40 int fd;
41};
42
43static int realtek_mst_i2c_spi_write_data(int fd, uint16_t addr, void *buf, uint16_t len)
44{
45 i2c_buffer_t data;
46 if (i2c_buffer_t_fill(&data, buf, len))
47 return SPI_GENERIC_ERROR;
48
49 return i2c_write(fd, addr, &data) == len ? 0 : SPI_GENERIC_ERROR;
50}
51
52static int realtek_mst_i2c_spi_read_data(int fd, uint16_t addr, void *buf, uint16_t len)
53{
54 i2c_buffer_t data;
55 if (i2c_buffer_t_fill(&data, buf, len))
56 return SPI_GENERIC_ERROR;
57
58 return i2c_read(fd, addr, &data) == len ? 0 : SPI_GENERIC_ERROR;
59}
60
61static int get_fd_from_context(const struct flashctx *flash)
62{
63 if (!flash || !flash->mst || !flash->mst->spi.data) {
64 msg_perr("Unable to extract fd from flash context.\n");
65 return SPI_GENERIC_ERROR;
66 }
67 const struct realtek_mst_i2c_spi_data *data =
68 (const struct realtek_mst_i2c_spi_data *)flash->mst->spi.data;
69
70 return data->fd;
71}
72
73static int realtek_mst_i2c_spi_write_register(int fd, uint8_t reg, uint8_t value)
74{
75 uint8_t command[] = { reg, value };
76 return realtek_mst_i2c_spi_write_data(fd, REGISTER_ADDRESS, command, 2);
77}
78
79static int realtek_mst_i2c_spi_read_register(int fd, uint8_t reg, uint8_t *value)
80{
81 uint8_t command[] = { reg };
82 int ret = realtek_mst_i2c_spi_write_data(fd, REGISTER_ADDRESS, command, 1);
83 ret |= realtek_mst_i2c_spi_read_data(fd, REGISTER_ADDRESS, value, 1);
84
85 return ret ? SPI_GENERIC_ERROR : 0;
86}
87
88static int realtek_mst_i2c_spi_wait_command_done(int fd, unsigned int offset, int mask)
89{
90 uint8_t val;
91 int tried = 0;
92 int ret = 0;
93 do {
94 ret |= realtek_mst_i2c_spi_read_register(fd, offset, &val);
95 } while (!ret && (val & mask) && ++tried < MAX_SPI_WAIT_RETRIES);
96
97 if (tried == MAX_SPI_WAIT_RETRIES) {
98 msg_perr("%s: Time out on sending command.\n", __func__);
99 return -MAX_SPI_WAIT_RETRIES;
100 }
101
102 return (val & mask) ? SPI_GENERIC_ERROR : ret;
103}
104
105static int realtek_mst_i2c_spi_enter_isp_mode(int fd)
106{
107 // 0xFF6F = 0x80;
108 int ret = realtek_mst_i2c_spi_write_register(fd, 0x6F, 0x80); // enter isp mode
109
110 // set internal osc divider register to default to speed up MCU
111 // 0x06A0 = 0x74
112 ret |= realtek_mst_i2c_spi_write_register(fd, 0xF4, 0x9F);
113 ret |= realtek_mst_i2c_spi_write_register(fd, 0xF5, 0x06);
114 ret |= realtek_mst_i2c_spi_write_register(fd, 0xF4, 0xA0);
115 ret |= realtek_mst_i2c_spi_write_register(fd, 0xF5, 0x74);
116
117 return ret;
118}
119
120static int realtek_mst_i2c_spi_reset_mpu(int fd)
121{
122 int ret = 0;
123 // 0xFFEE[1] = 1;
124 uint8_t val = 0;
125 ret |= realtek_mst_i2c_spi_read_register(fd, 0xEE, &val);
126 ret |= realtek_mst_i2c_spi_write_register(fd, 0xEE, (val & 0xFD) | 0x02);
127 return ret;
128}
129
130#if 0
131static int realtek_mst_i2c_spi_set_defaults(int fd)
132{
133 // 0xFF1B = 0x02;
134 int ret = realtek_mst_i2c_spi_write_register(fd, 0x1B, 0x02);
135 ret = realtek_mst_i2c_spi_write_register(fd, 0x1C, 0x30);
136 ret = realtek_mst_i2c_spi_write_register(fd, 0x1D, 0x1C);
137 ret = realtek_mst_i2c_spi_write_register(fd, 0x1E, 0x02);
138 ret = realtek_mst_i2c_spi_write_register(fd, 0x1F, 0x00);
139
140 ret = realtek_mst_i2c_spi_write_register(fd, 0x20, 0x1C);
141 ret = realtek_mst_i2c_spi_write_register(fd, 0x2C, 0x02);
142 ret = realtek_mst_i2c_spi_write_register(fd, 0x2D, 0x00);
143 ret = realtek_mst_i2c_spi_write_register(fd, 0x2E, 0x1C);
144
145 ret = realtek_mst_i2c_spi_write_register(fd, 0x62, 0x06);
146 ret = realtek_mst_i2c_spi_write_register(fd, 0x6A, 0x03);
147 ret = realtek_mst_i2c_spi_write_register(fd, 0x6B, 0x0B);
148 ret = realtek_mst_i2c_spi_write_register(fd, 0x6C, 0x00);
149
150 ret = realtek_mst_i2c_spi_write_register(fd, 0xED, 0x88);
151 ret = realtek_mst_i2c_spi_write_register(fd, 0xEE, 0x04);
152
153 return ret;
154}
155#endif
156
157static int realtek_mst_i2c_spi_disable_protection(int fd)
158{
159 int ret = 0;
160 uint8_t val = 0;
161 // 0xAB[2:0] = b001
162
163 ret |= realtek_mst_i2c_spi_write_register(fd, 0xF4, 0x9F);
164 ret |= realtek_mst_i2c_spi_write_register(fd, 0xF5, 0x10);
165 ret |= realtek_mst_i2c_spi_write_register(fd, 0xF4, 0xAB);
166
167 ret |= realtek_mst_i2c_spi_read_register(fd, 0xF5, &val);
168
169 ret |= realtek_mst_i2c_spi_write_register(fd, 0xF4, 0x9F);
170 ret |= realtek_mst_i2c_spi_write_register(fd, 0xF5, 0x10);
171 ret |= realtek_mst_i2c_spi_write_register(fd, 0xF4, 0xAB);
172
173 ret |= realtek_mst_i2c_spi_write_register(fd, 0xF5, (val & 0xF8) | 0x01);
174
175 /* Set pin value to high, 0xFFD7[0] = 1. */
176 ret |= realtek_mst_i2c_spi_read_register(fd, 0xD7, &val);
177 ret |= realtek_mst_i2c_spi_write_register(fd, 0xD7, (val & 0xFE) | 0x01);
178
179 return ret;
180}
181
182static int realtek_mst_i2c_spi_send_command(const struct flashctx *flash,
183 unsigned int writecnt, unsigned int readcnt,
184 const unsigned char *writearr,
185 unsigned char *readarr)
186{
187 unsigned i;
188 int ret = 0;
189
190 if (writecnt > 4 || readcnt > 3 || writecnt == 0) {
191 //msg_perr("%s: Invalid read/write count for send command.\n", __func__);
192 return SPI_GENERIC_ERROR;
193 }
194 //printf("%s: writearr[0]=0x%x\n", __func__, writearr[0]);
195
196 int fd = get_fd_from_context(flash);
197 if (fd < 0)
198 return SPI_GENERIC_ERROR;
199
200 /* First byte of writearr should be the command value, followed by the value to write. */
201 // 0xFF60 = cmd;
202 writecnt--;
203 uint8_t ctrl_reg_val = (writecnt << 3) | (readcnt << 1) | (2 << 5);
204 ret |= realtek_mst_i2c_spi_write_register(fd, 0x60, ctrl_reg_val);
205 ret |= realtek_mst_i2c_spi_write_register(fd, 0x61, writearr[0]);
206
207 for (i = 0; i < writecnt; ++i)
208 ret |= realtek_mst_i2c_spi_write_register(fd, 0x64 + i, writearr[i + 1]);
209 ret |= realtek_mst_i2c_spi_write_register(fd, 0x60, ctrl_reg_val | 0x1);
210 if (ret)
211 return ret;
212
213 ret = realtek_mst_i2c_spi_wait_command_done(fd, 0x60, 0x01);
214 if (ret)
215 return ret;
216
217 for (i = 0; i < readcnt; ++i)
218 ret |= realtek_mst_i2c_spi_read_register(fd, 0x67 + i, &readarr[i]);
219
220 //for (i = 0; i< readcnt; i++)
221 // printf("DEBUG: readarr[%d]=0x%02x\n", i, readarr[i]);
222
223 return ret;
224}
225
226static int realtek_mst_i2c_spi_map_page(int fd, uint8_t block_idx, uint8_t page_idx, uint8_t byte_idx)
227{
228 int ret = 0;
229 ret |= realtek_mst_i2c_spi_write_register(fd, 0x64, block_idx);
230 ret |= realtek_mst_i2c_spi_write_register(fd, 0x65, page_idx);
231 ret |= realtek_mst_i2c_spi_write_register(fd, 0x66, byte_idx);
232
233 return ret ? SPI_GENERIC_ERROR : 0;
234}
235
236static int realtek_mst_i2c_spi_read(struct flashctx *flash, uint8_t *buf,
237 unsigned int start, unsigned int len)
238{
239 unsigned i;
240 int ret = 0;
241
242 if (start & 0xff)
243 return default_spi_read(flash, buf, start, len);
244
245 int fd = get_fd_from_context(flash);
246 if (fd < 0)
247 return SPI_GENERIC_ERROR;
248
249 start--;
250 ret |= realtek_mst_i2c_spi_write_register(fd, 0x60, 0x46); // **
251 ret |= realtek_mst_i2c_spi_write_register(fd, 0x61, OPCODE_READ);
252 uint8_t block_idx = start >> 16;
253 uint8_t page_idx = start >> 8;
254 uint8_t byte_idx = start;
255 ret |= realtek_mst_i2c_spi_map_page(fd, block_idx, page_idx, byte_idx);
256 ret |= realtek_mst_i2c_spi_write_register(fd, 0x6a, 0x03);
257 ret |= realtek_mst_i2c_spi_write_register(fd, 0x60, 0x47); // **
258 if (ret)
259 return ret;
260
261 ret = realtek_mst_i2c_spi_wait_command_done(fd, 0x60, 0x01);
262 if (ret)
263 return ret;
264
265 /**
266 * The first byte is just a null, probably a status code?
267 * Advance the read by a offset of one byte and continue.
268 */
269 uint8_t dummy;
270 realtek_mst_i2c_spi_read_register(fd, 0x70, &dummy);
271
272 for (i = 0; i < len; i += PAGE_SIZE) {
273 ret |= realtek_mst_i2c_spi_read_data(fd, REGISTER_ADDRESS,
274 buf + i, min(len - i, PAGE_SIZE));
275 if (ret)
276 return ret;
277 }
278
279 return ret;
280}
281
282static int realtek_mst_i2c_spi_write_256(struct flashctx *flash, const uint8_t *buf,
283 unsigned int start, unsigned int len)
284{
285 unsigned i;
286 int ret = 0;
287
288 if (start & 0xff)
289 return default_spi_write_256(flash, buf, start, len);
290
291 int fd = get_fd_from_context(flash);
292 if (fd < 0)
293 return SPI_GENERIC_ERROR;
294
295 ret = realtek_mst_i2c_spi_disable_protection(fd);
296 if (ret)
297 return ret;
298
299 start--;
300 ret |= realtek_mst_i2c_spi_write_register(fd, 0x60, 0x46); // **
301 ret |= realtek_mst_i2c_spi_write_register(fd, 0x61, OPCODE_WRITE);
302 uint8_t block_idx = start >> 16;
303 uint8_t page_idx = start >> 8;
304 uint8_t byte_idx = start;
305 ret |= realtek_mst_i2c_spi_map_page(fd, block_idx, page_idx, byte_idx);
306 ret |= realtek_mst_i2c_spi_write_register(fd, 0x6a, 0x03);
307 ret |= realtek_mst_i2c_spi_write_register(fd, 0x60, 0x47); // **
308 if (ret)
309 goto fail;
310
311 ret = realtek_mst_i2c_spi_wait_command_done(fd, 0x60, 0x01);
312 if (ret)
313 goto fail;
314
315 for (i = 0; i < len; i += PAGE_SIZE) {
316 ret |= realtek_mst_i2c_spi_write_data(fd, REGISTER_ADDRESS,
317 (uint8_t *)buf + i, min(len - i, PAGE_SIZE));
318 if (ret)
319 break;
320 }
321
322fail:
323 /* TODO: re-enable the write protection? */
324
325 return ret;
326}
327
328static int realtek_mst_i2c_spi_write_aai(struct flashctx *flash, const uint8_t *buf,
329 unsigned int start, unsigned int len)
330{
331 msg_perr("%s: AAI write function is not supported.\n", __func__);
332 return SPI_GENERIC_ERROR;
333}
334
335static struct spi_master spi_master_i2c_realtek_mst = {
336 .max_data_read = 16,
337 .max_data_write = 8,
338 .command = realtek_mst_i2c_spi_send_command,
339 .multicommand = default_spi_send_multicommand,
340 .read = realtek_mst_i2c_spi_read,
341 .write_256 = realtek_mst_i2c_spi_write_256,
342 .write_aai = realtek_mst_i2c_spi_write_aai,
343};
344
345static int realtek_mst_i2c_spi_shutdown(void *data)
346{
347 int ret = 0;
348 struct realtek_mst_i2c_spi_data *realtek_mst_data =
349 (struct realtek_mst_i2c_spi_data *)data;
350 int fd = realtek_mst_data->fd;
351 ret |= realtek_mst_i2c_spi_reset_mpu(fd);
352 i2c_close(fd);
353 free(data);
354
355 return ret;
356}
357
358static int get_params(int *i2c_bus)
359{
360 char *bus_str = NULL;
361 int ret = SPI_GENERIC_ERROR;
362
363 bus_str = extract_programmer_param("bus");
364 if (bus_str) {
365 char *bus_suffix;
366 errno = 0;
367 int bus = (int)strtol(bus_str, &bus_suffix, 10);
368 if (errno != 0 || bus_str == bus_suffix) {
369 msg_perr("%s: Could not convert 'bus'.\n", __func__);
370 goto get_params_done;
371 }
372
373 if (bus < 0 || bus > 255) {
374 msg_perr("%s: Value for 'bus' is out of range(0-255).\n", __func__);
375 goto get_params_done;
376 }
377
378 if (strlen(bus_suffix) > 0) {
379 msg_perr("%s: Garbage following 'bus' value.\n", __func__);
380 goto get_params_done;
381 }
382
383 msg_pinfo("Using i2c bus %i.\n", bus);
384 *i2c_bus = bus;
385 ret = 0;
386 goto get_params_done;
387 } else {
388 msg_perr("%s: Bus number not specified.\n", __func__);
389 }
390get_params_done:
391 if (bus_str)
392 free(bus_str);
393
394 return ret;
395}
396
397int realtek_mst_i2c_spi_init(void)
398{
399 int ret = 0;
400 int i2c_bus = 0;
401
402 if (get_params(&i2c_bus))
403 return SPI_GENERIC_ERROR;
404
405 int fd = i2c_open(i2c_bus, REGISTER_ADDRESS, 0);
406 if (fd < 0)
407 return fd;
408
409 /* Ensure we are in a known state before entering ISP mode */
410 ret |= realtek_mst_i2c_spi_reset_mpu(fd);
411 if (ret)
412 return ret;
413
414 ret |= realtek_mst_i2c_spi_enter_isp_mode(fd);
415 if (ret)
416 return ret;
417// XXX: maybe make into a mode:defaults cli param?
418#if 0
419 ret |= realtek_mst_i2c_spi_set_defaults(fd);
420 if (ret)
421 return ret;
422#endif
423
424 struct realtek_mst_i2c_spi_data *data = calloc(1, sizeof(struct realtek_mst_i2c_spi_data));
425 if (!data) {
426 msg_perr("Unable to allocate space for extra SPI master data.\n");
427 return SPI_GENERIC_ERROR;
428 }
429
430 data->fd = fd;
431 ret |= register_shutdown(realtek_mst_i2c_spi_shutdown, data);
432
433 spi_master_i2c_realtek_mst.data = data;
434 ret |= register_spi_master(&spi_master_i2c_realtek_mst);
435
436 return ret;
437}