blob: 682d4a66eac493ad4c4f1a4d4e5952e4075ed84a [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
Edward O'Callaghan33653fd2020-05-04 13:01:54 +100034#define MCU_MODE 0x6F
35#define ENTER_ISP_MODE 0x80
36
37#define MCU_DATA_PORT 0x70
38
39#define MAP_PAGE_BYTE2 0x64
40#define MAP_PAGE_BYTE1 0x65
41#define MAP_PAGE_BYTE0 0x66
42
Edward O'Callaghan97dd9262020-03-26 00:00:41 +110043//opcodes
44#define OPCODE_READ 3
45#define OPCODE_WRITE 2
46
47
48struct realtek_mst_i2c_spi_data {
49 int fd;
50};
51
52static int realtek_mst_i2c_spi_write_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_write(fd, addr, &data) == len ? 0 : SPI_GENERIC_ERROR;
59}
60
61static int realtek_mst_i2c_spi_read_data(int fd, uint16_t addr, void *buf, uint16_t len)
62{
63 i2c_buffer_t data;
64 if (i2c_buffer_t_fill(&data, buf, len))
65 return SPI_GENERIC_ERROR;
66
67 return i2c_read(fd, addr, &data) == len ? 0 : SPI_GENERIC_ERROR;
68}
69
70static int get_fd_from_context(const struct flashctx *flash)
71{
72 if (!flash || !flash->mst || !flash->mst->spi.data) {
73 msg_perr("Unable to extract fd from flash context.\n");
74 return SPI_GENERIC_ERROR;
75 }
76 const struct realtek_mst_i2c_spi_data *data =
77 (const struct realtek_mst_i2c_spi_data *)flash->mst->spi.data;
78
79 return data->fd;
80}
81
82static int realtek_mst_i2c_spi_write_register(int fd, uint8_t reg, uint8_t value)
83{
84 uint8_t command[] = { reg, value };
85 return realtek_mst_i2c_spi_write_data(fd, REGISTER_ADDRESS, command, 2);
86}
87
88static int realtek_mst_i2c_spi_read_register(int fd, uint8_t reg, uint8_t *value)
89{
90 uint8_t command[] = { reg };
91 int ret = realtek_mst_i2c_spi_write_data(fd, REGISTER_ADDRESS, command, 1);
92 ret |= realtek_mst_i2c_spi_read_data(fd, REGISTER_ADDRESS, value, 1);
93
94 return ret ? SPI_GENERIC_ERROR : 0;
95}
96
97static int realtek_mst_i2c_spi_wait_command_done(int fd, unsigned int offset, int mask)
98{
99 uint8_t val;
100 int tried = 0;
101 int ret = 0;
102 do {
103 ret |= realtek_mst_i2c_spi_read_register(fd, offset, &val);
104 } while (!ret && (val & mask) && ++tried < MAX_SPI_WAIT_RETRIES);
105
106 if (tried == MAX_SPI_WAIT_RETRIES) {
107 msg_perr("%s: Time out on sending command.\n", __func__);
108 return -MAX_SPI_WAIT_RETRIES;
109 }
110
111 return (val & mask) ? SPI_GENERIC_ERROR : ret;
112}
113
114static int realtek_mst_i2c_spi_enter_isp_mode(int fd)
115{
Edward O'Callaghan33653fd2020-05-04 13:01:54 +1000116 int ret = realtek_mst_i2c_spi_write_register(fd, MCU_MODE, ENTER_ISP_MODE);
Edward O'Callaghan97dd9262020-03-26 00:00:41 +1100117
118 // set internal osc divider register to default to speed up MCU
119 // 0x06A0 = 0x74
120 ret |= realtek_mst_i2c_spi_write_register(fd, 0xF4, 0x9F);
121 ret |= realtek_mst_i2c_spi_write_register(fd, 0xF5, 0x06);
122 ret |= realtek_mst_i2c_spi_write_register(fd, 0xF4, 0xA0);
123 ret |= realtek_mst_i2c_spi_write_register(fd, 0xF5, 0x74);
124
125 return ret;
126}
127
128static int realtek_mst_i2c_spi_reset_mpu(int fd)
129{
130 int ret = 0;
131 // 0xFFEE[1] = 1;
132 uint8_t val = 0;
133 ret |= realtek_mst_i2c_spi_read_register(fd, 0xEE, &val);
134 ret |= realtek_mst_i2c_spi_write_register(fd, 0xEE, (val & 0xFD) | 0x02);
135 return ret;
136}
137
138#if 0
139static int realtek_mst_i2c_spi_set_defaults(int fd)
140{
141 // 0xFF1B = 0x02;
142 int ret = realtek_mst_i2c_spi_write_register(fd, 0x1B, 0x02);
143 ret = realtek_mst_i2c_spi_write_register(fd, 0x1C, 0x30);
144 ret = realtek_mst_i2c_spi_write_register(fd, 0x1D, 0x1C);
145 ret = realtek_mst_i2c_spi_write_register(fd, 0x1E, 0x02);
146 ret = realtek_mst_i2c_spi_write_register(fd, 0x1F, 0x00);
147
148 ret = realtek_mst_i2c_spi_write_register(fd, 0x20, 0x1C);
149 ret = realtek_mst_i2c_spi_write_register(fd, 0x2C, 0x02);
150 ret = realtek_mst_i2c_spi_write_register(fd, 0x2D, 0x00);
151 ret = realtek_mst_i2c_spi_write_register(fd, 0x2E, 0x1C);
152
153 ret = realtek_mst_i2c_spi_write_register(fd, 0x62, 0x06);
154 ret = realtek_mst_i2c_spi_write_register(fd, 0x6A, 0x03);
155 ret = realtek_mst_i2c_spi_write_register(fd, 0x6B, 0x0B);
156 ret = realtek_mst_i2c_spi_write_register(fd, 0x6C, 0x00);
157
158 ret = realtek_mst_i2c_spi_write_register(fd, 0xED, 0x88);
159 ret = realtek_mst_i2c_spi_write_register(fd, 0xEE, 0x04);
160
161 return ret;
162}
163#endif
164
165static int realtek_mst_i2c_spi_disable_protection(int fd)
166{
167 int ret = 0;
168 uint8_t val = 0;
169 // 0xAB[2:0] = b001
170
171 ret |= realtek_mst_i2c_spi_write_register(fd, 0xF4, 0x9F);
172 ret |= realtek_mst_i2c_spi_write_register(fd, 0xF5, 0x10);
173 ret |= realtek_mst_i2c_spi_write_register(fd, 0xF4, 0xAB);
174
175 ret |= realtek_mst_i2c_spi_read_register(fd, 0xF5, &val);
176
177 ret |= realtek_mst_i2c_spi_write_register(fd, 0xF4, 0x9F);
178 ret |= realtek_mst_i2c_spi_write_register(fd, 0xF5, 0x10);
179 ret |= realtek_mst_i2c_spi_write_register(fd, 0xF4, 0xAB);
180
181 ret |= realtek_mst_i2c_spi_write_register(fd, 0xF5, (val & 0xF8) | 0x01);
182
183 /* Set pin value to high, 0xFFD7[0] = 1. */
184 ret |= realtek_mst_i2c_spi_read_register(fd, 0xD7, &val);
185 ret |= realtek_mst_i2c_spi_write_register(fd, 0xD7, (val & 0xFE) | 0x01);
186
187 return ret;
188}
189
190static int realtek_mst_i2c_spi_send_command(const struct flashctx *flash,
191 unsigned int writecnt, unsigned int readcnt,
192 const unsigned char *writearr,
193 unsigned char *readarr)
194{
195 unsigned i;
196 int ret = 0;
197
198 if (writecnt > 4 || readcnt > 3 || writecnt == 0) {
Edward O'Callaghan97dd9262020-03-26 00:00:41 +1100199 return SPI_GENERIC_ERROR;
200 }
Edward O'Callaghan97dd9262020-03-26 00:00:41 +1100201
202 int fd = get_fd_from_context(flash);
203 if (fd < 0)
204 return SPI_GENERIC_ERROR;
205
Edward O'Callaghan562ed8e2020-05-06 15:24:25 +1000206 /* First byte of writearr should be the spi opcode value, followed by the value to write. */
Edward O'Callaghan97dd9262020-03-26 00:00:41 +1100207 writecnt--;
Edward O'Callaghan562ed8e2020-05-06 15:24:25 +1000208
209 /**
210 * Before dispatching a SPI opcode the MCU register 0x60 requires
211 * the following configuration byte set:
212 *
213 * BIT0 - start [0] , end [1].
214 * BITS[1-4] - counts.
215 * BITS[5-7] - opcode type.
216 *
217 * | bit7 | bit6 | bit5 |
218 * +------+------+------+
219 * | 0 | 1 | 0 | ~ JEDEC_RDID,REMS,READ
220 * | 0 | 1 | 1 | ~ JEDEC_WRSR
221 * | 1 | 0 | 1 | ~ JEDEC_.. erasures.
222 */
223 uint8_t ctrl_reg_val = (writecnt << 3) | (readcnt << 1);
224 switch (writearr[0]) {
225 /* WREN isn't a supported somehow? ignore it. */
226 case JEDEC_WREN: return 0;
227 /* WRSR requires BIT6 && BIT5 set. */
228 case JEDEC_WRSR:
229 ctrl_reg_val |= (1 << 5);
230 ctrl_reg_val |= (2 << 5);
231 break;
232 /* Erasures require BIT7 && BIT5 set. */
233 case JEDEC_CE_60:
234 case JEDEC_CE_C7:
235 case JEDEC_BE_52:
236 case JEDEC_BE_D8:
237 case JEDEC_BE_D7:
238 case JEDEC_SE:
239 ctrl_reg_val |= (1 << 5);
240 ctrl_reg_val |= (4 << 5);
241 break;
242 default:
243 /* Otherwise things like RDID,REMS,READ require BIT6 */
244 ctrl_reg_val |= (2 << 5);
245 }
Edward O'Callaghan97dd9262020-03-26 00:00:41 +1100246 ret |= realtek_mst_i2c_spi_write_register(fd, 0x60, ctrl_reg_val);
Edward O'Callaghan562ed8e2020-05-06 15:24:25 +1000247 ret |= realtek_mst_i2c_spi_write_register(fd, 0x61, writearr[0]); /* opcode */
Edward O'Callaghan97dd9262020-03-26 00:00:41 +1100248
249 for (i = 0; i < writecnt; ++i)
250 ret |= realtek_mst_i2c_spi_write_register(fd, 0x64 + i, writearr[i + 1]);
251 ret |= realtek_mst_i2c_spi_write_register(fd, 0x60, ctrl_reg_val | 0x1);
252 if (ret)
253 return ret;
254
255 ret = realtek_mst_i2c_spi_wait_command_done(fd, 0x60, 0x01);
256 if (ret)
257 return ret;
258
259 for (i = 0; i < readcnt; ++i)
260 ret |= realtek_mst_i2c_spi_read_register(fd, 0x67 + i, &readarr[i]);
261
Edward O'Callaghan97dd9262020-03-26 00:00:41 +1100262 return ret;
263}
264
265static int realtek_mst_i2c_spi_map_page(int fd, uint8_t block_idx, uint8_t page_idx, uint8_t byte_idx)
266{
267 int ret = 0;
Edward O'Callaghan33653fd2020-05-04 13:01:54 +1000268 ret |= realtek_mst_i2c_spi_write_register(fd, MAP_PAGE_BYTE2, block_idx);
269 ret |= realtek_mst_i2c_spi_write_register(fd, MAP_PAGE_BYTE1, page_idx);
270 ret |= realtek_mst_i2c_spi_write_register(fd, MAP_PAGE_BYTE0, byte_idx);
Edward O'Callaghan97dd9262020-03-26 00:00:41 +1100271
272 return ret ? SPI_GENERIC_ERROR : 0;
273}
274
275static int realtek_mst_i2c_spi_read(struct flashctx *flash, uint8_t *buf,
276 unsigned int start, unsigned int len)
277{
278 unsigned i;
279 int ret = 0;
280
281 if (start & 0xff)
282 return default_spi_read(flash, buf, start, len);
283
284 int fd = get_fd_from_context(flash);
285 if (fd < 0)
286 return SPI_GENERIC_ERROR;
287
288 start--;
289 ret |= realtek_mst_i2c_spi_write_register(fd, 0x60, 0x46); // **
290 ret |= realtek_mst_i2c_spi_write_register(fd, 0x61, OPCODE_READ);
291 uint8_t block_idx = start >> 16;
292 uint8_t page_idx = start >> 8;
293 uint8_t byte_idx = start;
294 ret |= realtek_mst_i2c_spi_map_page(fd, block_idx, page_idx, byte_idx);
295 ret |= realtek_mst_i2c_spi_write_register(fd, 0x6a, 0x03);
296 ret |= realtek_mst_i2c_spi_write_register(fd, 0x60, 0x47); // **
297 if (ret)
298 return ret;
299
300 ret = realtek_mst_i2c_spi_wait_command_done(fd, 0x60, 0x01);
301 if (ret)
302 return ret;
303
304 /**
305 * The first byte is just a null, probably a status code?
306 * Advance the read by a offset of one byte and continue.
307 */
308 uint8_t dummy;
Edward O'Callaghan33653fd2020-05-04 13:01:54 +1000309 realtek_mst_i2c_spi_read_register(fd, MCU_DATA_PORT, &dummy);
Edward O'Callaghan97dd9262020-03-26 00:00:41 +1100310
311 for (i = 0; i < len; i += PAGE_SIZE) {
312 ret |= realtek_mst_i2c_spi_read_data(fd, REGISTER_ADDRESS,
313 buf + i, min(len - i, PAGE_SIZE));
314 if (ret)
315 return ret;
316 }
317
318 return ret;
319}
320
321static int realtek_mst_i2c_spi_write_256(struct flashctx *flash, const uint8_t *buf,
322 unsigned int start, unsigned int len)
323{
324 unsigned i;
325 int ret = 0;
326
327 if (start & 0xff)
328 return default_spi_write_256(flash, buf, start, len);
329
330 int fd = get_fd_from_context(flash);
331 if (fd < 0)
332 return SPI_GENERIC_ERROR;
333
334 ret = realtek_mst_i2c_spi_disable_protection(fd);
335 if (ret)
336 return ret;
337
338 start--;
339 ret |= realtek_mst_i2c_spi_write_register(fd, 0x60, 0x46); // **
340 ret |= realtek_mst_i2c_spi_write_register(fd, 0x61, OPCODE_WRITE);
341 uint8_t block_idx = start >> 16;
342 uint8_t page_idx = start >> 8;
343 uint8_t byte_idx = start;
344 ret |= realtek_mst_i2c_spi_map_page(fd, block_idx, page_idx, byte_idx);
345 ret |= realtek_mst_i2c_spi_write_register(fd, 0x6a, 0x03);
346 ret |= realtek_mst_i2c_spi_write_register(fd, 0x60, 0x47); // **
347 if (ret)
348 goto fail;
349
350 ret = realtek_mst_i2c_spi_wait_command_done(fd, 0x60, 0x01);
351 if (ret)
352 goto fail;
353
354 for (i = 0; i < len; i += PAGE_SIZE) {
355 ret |= realtek_mst_i2c_spi_write_data(fd, REGISTER_ADDRESS,
356 (uint8_t *)buf + i, min(len - i, PAGE_SIZE));
357 if (ret)
358 break;
359 }
360
361fail:
362 /* TODO: re-enable the write protection? */
363
364 return ret;
365}
366
367static int realtek_mst_i2c_spi_write_aai(struct flashctx *flash, const uint8_t *buf,
368 unsigned int start, unsigned int len)
369{
370 msg_perr("%s: AAI write function is not supported.\n", __func__);
371 return SPI_GENERIC_ERROR;
372}
373
374static struct spi_master spi_master_i2c_realtek_mst = {
375 .max_data_read = 16,
376 .max_data_write = 8,
377 .command = realtek_mst_i2c_spi_send_command,
378 .multicommand = default_spi_send_multicommand,
379 .read = realtek_mst_i2c_spi_read,
380 .write_256 = realtek_mst_i2c_spi_write_256,
381 .write_aai = realtek_mst_i2c_spi_write_aai,
382};
383
384static int realtek_mst_i2c_spi_shutdown(void *data)
385{
386 int ret = 0;
387 struct realtek_mst_i2c_spi_data *realtek_mst_data =
388 (struct realtek_mst_i2c_spi_data *)data;
389 int fd = realtek_mst_data->fd;
390 ret |= realtek_mst_i2c_spi_reset_mpu(fd);
391 i2c_close(fd);
392 free(data);
393
394 return ret;
395}
396
397static int get_params(int *i2c_bus)
398{
399 char *bus_str = NULL;
400 int ret = SPI_GENERIC_ERROR;
401
402 bus_str = extract_programmer_param("bus");
403 if (bus_str) {
404 char *bus_suffix;
405 errno = 0;
406 int bus = (int)strtol(bus_str, &bus_suffix, 10);
407 if (errno != 0 || bus_str == bus_suffix) {
408 msg_perr("%s: Could not convert 'bus'.\n", __func__);
409 goto get_params_done;
410 }
411
412 if (bus < 0 || bus > 255) {
413 msg_perr("%s: Value for 'bus' is out of range(0-255).\n", __func__);
414 goto get_params_done;
415 }
416
417 if (strlen(bus_suffix) > 0) {
418 msg_perr("%s: Garbage following 'bus' value.\n", __func__);
419 goto get_params_done;
420 }
421
422 msg_pinfo("Using i2c bus %i.\n", bus);
423 *i2c_bus = bus;
424 ret = 0;
425 goto get_params_done;
426 } else {
427 msg_perr("%s: Bus number not specified.\n", __func__);
428 }
429get_params_done:
430 if (bus_str)
431 free(bus_str);
432
433 return ret;
434}
435
436int realtek_mst_i2c_spi_init(void)
437{
438 int ret = 0;
439 int i2c_bus = 0;
440
441 if (get_params(&i2c_bus))
442 return SPI_GENERIC_ERROR;
443
444 int fd = i2c_open(i2c_bus, REGISTER_ADDRESS, 0);
445 if (fd < 0)
446 return fd;
447
448 /* Ensure we are in a known state before entering ISP mode */
449 ret |= realtek_mst_i2c_spi_reset_mpu(fd);
450 if (ret)
451 return ret;
452
453 ret |= realtek_mst_i2c_spi_enter_isp_mode(fd);
454 if (ret)
455 return ret;
456// XXX: maybe make into a mode:defaults cli param?
457#if 0
458 ret |= realtek_mst_i2c_spi_set_defaults(fd);
459 if (ret)
460 return ret;
461#endif
462
463 struct realtek_mst_i2c_spi_data *data = calloc(1, sizeof(struct realtek_mst_i2c_spi_data));
464 if (!data) {
465 msg_perr("Unable to allocate space for extra SPI master data.\n");
466 return SPI_GENERIC_ERROR;
467 }
468
469 data->fd = fd;
470 ret |= register_shutdown(realtek_mst_i2c_spi_shutdown, data);
471
472 spi_master_i2c_realtek_mst.data = data;
473 ret |= register_spi_master(&spi_master_i2c_realtek_mst);
474
475 return ret;
476}