blob: 78675289f6bb5191e9c2efdb543af39af50a1735 [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
Edward O'Callaghan387632a2020-05-06 16:16:47 +100036#define START_WRITE_XFER 0xA0
37#define WRITE_XFER_STATUS_MASK 0x20
Edward O'Callaghan33653fd2020-05-04 13:01:54 +100038
39#define MCU_DATA_PORT 0x70
40
41#define MAP_PAGE_BYTE2 0x64
42#define MAP_PAGE_BYTE1 0x65
43#define MAP_PAGE_BYTE0 0x66
44
Edward O'Callaghan97dd9262020-03-26 00:00:41 +110045//opcodes
46#define OPCODE_READ 3
47#define OPCODE_WRITE 2
48
49
50struct realtek_mst_i2c_spi_data {
51 int fd;
52};
53
54static int realtek_mst_i2c_spi_write_data(int fd, uint16_t addr, void *buf, uint16_t len)
55{
56 i2c_buffer_t data;
57 if (i2c_buffer_t_fill(&data, buf, len))
58 return SPI_GENERIC_ERROR;
59
60 return i2c_write(fd, addr, &data) == len ? 0 : SPI_GENERIC_ERROR;
61}
62
63static int realtek_mst_i2c_spi_read_data(int fd, uint16_t addr, void *buf, uint16_t len)
64{
65 i2c_buffer_t data;
66 if (i2c_buffer_t_fill(&data, buf, len))
67 return SPI_GENERIC_ERROR;
68
69 return i2c_read(fd, addr, &data) == len ? 0 : SPI_GENERIC_ERROR;
70}
71
72static int get_fd_from_context(const struct flashctx *flash)
73{
74 if (!flash || !flash->mst || !flash->mst->spi.data) {
75 msg_perr("Unable to extract fd from flash context.\n");
76 return SPI_GENERIC_ERROR;
77 }
78 const struct realtek_mst_i2c_spi_data *data =
79 (const struct realtek_mst_i2c_spi_data *)flash->mst->spi.data;
80
81 return data->fd;
82}
83
84static int realtek_mst_i2c_spi_write_register(int fd, uint8_t reg, uint8_t value)
85{
86 uint8_t command[] = { reg, value };
87 return realtek_mst_i2c_spi_write_data(fd, REGISTER_ADDRESS, command, 2);
88}
89
90static int realtek_mst_i2c_spi_read_register(int fd, uint8_t reg, uint8_t *value)
91{
92 uint8_t command[] = { reg };
93 int ret = realtek_mst_i2c_spi_write_data(fd, REGISTER_ADDRESS, command, 1);
94 ret |= realtek_mst_i2c_spi_read_data(fd, REGISTER_ADDRESS, value, 1);
95
96 return ret ? SPI_GENERIC_ERROR : 0;
97}
98
Edward O'Callaghana1ae9fe2020-05-07 11:42:56 +100099static int realtek_mst_i2c_spi_wait_command_done(int fd, unsigned int offset, int mask,
100 int target, int multiplier)
Edward O'Callaghan97dd9262020-03-26 00:00:41 +1100101{
102 uint8_t val;
103 int tried = 0;
104 int ret = 0;
105 do {
106 ret |= realtek_mst_i2c_spi_read_register(fd, offset, &val);
Edward O'Callaghana1ae9fe2020-05-07 11:42:56 +1000107 } while (!ret && ((val & mask) != target) && ++tried < (MAX_SPI_WAIT_RETRIES*multiplier));
Edward O'Callaghan97dd9262020-03-26 00:00:41 +1100108
109 if (tried == MAX_SPI_WAIT_RETRIES) {
110 msg_perr("%s: Time out on sending command.\n", __func__);
111 return -MAX_SPI_WAIT_RETRIES;
112 }
113
Edward O'Callaghana1ae9fe2020-05-07 11:42:56 +1000114 return (val & mask) != target ? SPI_GENERIC_ERROR : ret;
Edward O'Callaghan97dd9262020-03-26 00:00:41 +1100115}
116
117static int realtek_mst_i2c_spi_enter_isp_mode(int fd)
118{
Edward O'Callaghan33653fd2020-05-04 13:01:54 +1000119 int ret = realtek_mst_i2c_spi_write_register(fd, MCU_MODE, ENTER_ISP_MODE);
Edward O'Callaghan97dd9262020-03-26 00:00:41 +1100120
121 // set internal osc divider register to default to speed up MCU
122 // 0x06A0 = 0x74
123 ret |= realtek_mst_i2c_spi_write_register(fd, 0xF4, 0x9F);
124 ret |= realtek_mst_i2c_spi_write_register(fd, 0xF5, 0x06);
125 ret |= realtek_mst_i2c_spi_write_register(fd, 0xF4, 0xA0);
126 ret |= realtek_mst_i2c_spi_write_register(fd, 0xF5, 0x74);
127
128 return ret;
129}
130
Edward O'Callaghan387632a2020-05-06 16:16:47 +1000131static int realtek_mst_i2c_execute_write(int fd)
132{
133 int ret = realtek_mst_i2c_spi_write_register(fd, MCU_MODE, START_WRITE_XFER);
Edward O'Callaghana1ae9fe2020-05-07 11:42:56 +1000134 ret |= realtek_mst_i2c_spi_wait_command_done(fd, MCU_MODE, WRITE_XFER_STATUS_MASK, 0, 1);
Edward O'Callaghan387632a2020-05-06 16:16:47 +1000135 return ret;
136}
137
Edward O'Callaghan97dd9262020-03-26 00:00:41 +1100138static int realtek_mst_i2c_spi_reset_mpu(int fd)
139{
140 int ret = 0;
141 // 0xFFEE[1] = 1;
142 uint8_t val = 0;
143 ret |= realtek_mst_i2c_spi_read_register(fd, 0xEE, &val);
144 ret |= realtek_mst_i2c_spi_write_register(fd, 0xEE, (val & 0xFD) | 0x02);
145 return ret;
146}
147
Edward O'Callaghan97dd9262020-03-26 00:00:41 +1100148static int realtek_mst_i2c_spi_disable_protection(int fd)
149{
150 int ret = 0;
151 uint8_t val = 0;
152 // 0xAB[2:0] = b001
153
154 ret |= realtek_mst_i2c_spi_write_register(fd, 0xF4, 0x9F);
155 ret |= realtek_mst_i2c_spi_write_register(fd, 0xF5, 0x10);
156 ret |= realtek_mst_i2c_spi_write_register(fd, 0xF4, 0xAB);
157
158 ret |= realtek_mst_i2c_spi_read_register(fd, 0xF5, &val);
159
160 ret |= realtek_mst_i2c_spi_write_register(fd, 0xF4, 0x9F);
161 ret |= realtek_mst_i2c_spi_write_register(fd, 0xF5, 0x10);
162 ret |= realtek_mst_i2c_spi_write_register(fd, 0xF4, 0xAB);
163
164 ret |= realtek_mst_i2c_spi_write_register(fd, 0xF5, (val & 0xF8) | 0x01);
165
166 /* Set pin value to high, 0xFFD7[0] = 1. */
167 ret |= realtek_mst_i2c_spi_read_register(fd, 0xD7, &val);
168 ret |= realtek_mst_i2c_spi_write_register(fd, 0xD7, (val & 0xFE) | 0x01);
169
170 return ret;
171}
172
173static int realtek_mst_i2c_spi_send_command(const struct flashctx *flash,
174 unsigned int writecnt, unsigned int readcnt,
175 const unsigned char *writearr,
176 unsigned char *readarr)
177{
178 unsigned i;
Edward O'Callaghana1ae9fe2020-05-07 11:42:56 +1000179 int max_timeout_mul = 1;
Edward O'Callaghan97dd9262020-03-26 00:00:41 +1100180 int ret = 0;
181
182 if (writecnt > 4 || readcnt > 3 || writecnt == 0) {
Edward O'Callaghan97dd9262020-03-26 00:00:41 +1100183 return SPI_GENERIC_ERROR;
184 }
Edward O'Callaghan97dd9262020-03-26 00:00:41 +1100185
186 int fd = get_fd_from_context(flash);
187 if (fd < 0)
188 return SPI_GENERIC_ERROR;
189
Edward O'Callaghan562ed8e2020-05-06 15:24:25 +1000190 /* First byte of writearr should be the spi opcode value, followed by the value to write. */
Edward O'Callaghan97dd9262020-03-26 00:00:41 +1100191 writecnt--;
Edward O'Callaghan562ed8e2020-05-06 15:24:25 +1000192
193 /**
194 * Before dispatching a SPI opcode the MCU register 0x60 requires
195 * the following configuration byte set:
196 *
197 * BIT0 - start [0] , end [1].
198 * BITS[1-4] - counts.
199 * BITS[5-7] - opcode type.
200 *
201 * | bit7 | bit6 | bit5 |
202 * +------+------+------+
203 * | 0 | 1 | 0 | ~ JEDEC_RDID,REMS,READ
204 * | 0 | 1 | 1 | ~ JEDEC_WRSR
205 * | 1 | 0 | 1 | ~ JEDEC_.. erasures.
206 */
207 uint8_t ctrl_reg_val = (writecnt << 3) | (readcnt << 1);
208 switch (writearr[0]) {
209 /* WREN isn't a supported somehow? ignore it. */
210 case JEDEC_WREN: return 0;
211 /* WRSR requires BIT6 && BIT5 set. */
212 case JEDEC_WRSR:
213 ctrl_reg_val |= (1 << 5);
214 ctrl_reg_val |= (2 << 5);
215 break;
216 /* Erasures require BIT7 && BIT5 set. */
Edward O'Callaghan562ed8e2020-05-06 15:24:25 +1000217 case JEDEC_CE_C7:
Edward O'Callaghana1ae9fe2020-05-07 11:42:56 +1000218 max_timeout_mul *= 20; /* chip erasures take much longer! */
219 /* FALLTHRU */
220 case JEDEC_CE_60:
Edward O'Callaghan562ed8e2020-05-06 15:24:25 +1000221 case JEDEC_BE_52:
222 case JEDEC_BE_D8:
223 case JEDEC_BE_D7:
224 case JEDEC_SE:
225 ctrl_reg_val |= (1 << 5);
226 ctrl_reg_val |= (4 << 5);
227 break;
228 default:
229 /* Otherwise things like RDID,REMS,READ require BIT6 */
230 ctrl_reg_val |= (2 << 5);
231 }
Edward O'Callaghan97dd9262020-03-26 00:00:41 +1100232 ret |= realtek_mst_i2c_spi_write_register(fd, 0x60, ctrl_reg_val);
Edward O'Callaghan562ed8e2020-05-06 15:24:25 +1000233 ret |= realtek_mst_i2c_spi_write_register(fd, 0x61, writearr[0]); /* opcode */
Edward O'Callaghan97dd9262020-03-26 00:00:41 +1100234
235 for (i = 0; i < writecnt; ++i)
236 ret |= realtek_mst_i2c_spi_write_register(fd, 0x64 + i, writearr[i + 1]);
237 ret |= realtek_mst_i2c_spi_write_register(fd, 0x60, ctrl_reg_val | 0x1);
238 if (ret)
239 return ret;
240
Edward O'Callaghana1ae9fe2020-05-07 11:42:56 +1000241 ret = realtek_mst_i2c_spi_wait_command_done(fd, 0x60, 0x01, 0, max_timeout_mul);
Edward O'Callaghan97dd9262020-03-26 00:00:41 +1100242 if (ret)
243 return ret;
244
245 for (i = 0; i < readcnt; ++i)
246 ret |= realtek_mst_i2c_spi_read_register(fd, 0x67 + i, &readarr[i]);
247
Edward O'Callaghan97dd9262020-03-26 00:00:41 +1100248 return ret;
249}
250
251static int realtek_mst_i2c_spi_map_page(int fd, uint8_t block_idx, uint8_t page_idx, uint8_t byte_idx)
252{
253 int ret = 0;
Edward O'Callaghan33653fd2020-05-04 13:01:54 +1000254 ret |= realtek_mst_i2c_spi_write_register(fd, MAP_PAGE_BYTE2, block_idx);
255 ret |= realtek_mst_i2c_spi_write_register(fd, MAP_PAGE_BYTE1, page_idx);
256 ret |= realtek_mst_i2c_spi_write_register(fd, MAP_PAGE_BYTE0, byte_idx);
Edward O'Callaghan97dd9262020-03-26 00:00:41 +1100257
258 return ret ? SPI_GENERIC_ERROR : 0;
259}
260
Edward O'Callaghan387632a2020-05-06 16:16:47 +1000261static int realtek_mst_i2c_spi_write_page(int fd, uint8_t reg, const uint8_t *buf, unsigned int len)
262{
263 /**
264 * Using static buffer with maximum possible size,
265 * extra byte is needed for prefixing the data port register at index 0.
266 */
267 uint8_t wbuf[PAGE_SIZE + 1] = { MCU_DATA_PORT };
268 if (len > PAGE_SIZE)
269 return SPI_GENERIC_ERROR;
270
271 memcpy(&wbuf[1], buf, len);
272
273 return realtek_mst_i2c_spi_write_data(fd, REGISTER_ADDRESS, wbuf, len + 1);
274}
275
Edward O'Callaghan97dd9262020-03-26 00:00:41 +1100276static int realtek_mst_i2c_spi_read(struct flashctx *flash, uint8_t *buf,
277 unsigned int start, unsigned int len)
278{
279 unsigned i;
280 int ret = 0;
281
282 if (start & 0xff)
283 return default_spi_read(flash, buf, start, len);
284
285 int fd = get_fd_from_context(flash);
286 if (fd < 0)
287 return SPI_GENERIC_ERROR;
288
289 start--;
290 ret |= realtek_mst_i2c_spi_write_register(fd, 0x60, 0x46); // **
291 ret |= realtek_mst_i2c_spi_write_register(fd, 0x61, OPCODE_READ);
292 uint8_t block_idx = start >> 16;
293 uint8_t page_idx = start >> 8;
294 uint8_t byte_idx = start;
295 ret |= realtek_mst_i2c_spi_map_page(fd, block_idx, page_idx, byte_idx);
296 ret |= realtek_mst_i2c_spi_write_register(fd, 0x6a, 0x03);
297 ret |= realtek_mst_i2c_spi_write_register(fd, 0x60, 0x47); // **
298 if (ret)
299 return ret;
300
Edward O'Callaghana1ae9fe2020-05-07 11:42:56 +1000301 ret = realtek_mst_i2c_spi_wait_command_done(fd, 0x60, 0x01, 0, 1);
Edward O'Callaghan97dd9262020-03-26 00:00:41 +1100302 if (ret)
303 return ret;
304
305 /**
306 * The first byte is just a null, probably a status code?
307 * Advance the read by a offset of one byte and continue.
308 */
309 uint8_t dummy;
Edward O'Callaghan33653fd2020-05-04 13:01:54 +1000310 realtek_mst_i2c_spi_read_register(fd, MCU_DATA_PORT, &dummy);
Edward O'Callaghan97dd9262020-03-26 00:00:41 +1100311
312 for (i = 0; i < len; i += PAGE_SIZE) {
313 ret |= realtek_mst_i2c_spi_read_data(fd, REGISTER_ADDRESS,
314 buf + i, min(len - i, PAGE_SIZE));
315 if (ret)
316 return ret;
317 }
318
319 return ret;
320}
321
322static int realtek_mst_i2c_spi_write_256(struct flashctx *flash, const uint8_t *buf,
323 unsigned int start, unsigned int len)
324{
325 unsigned i;
326 int ret = 0;
327
328 if (start & 0xff)
329 return default_spi_write_256(flash, buf, start, len);
330
331 int fd = get_fd_from_context(flash);
332 if (fd < 0)
333 return SPI_GENERIC_ERROR;
334
335 ret = realtek_mst_i2c_spi_disable_protection(fd);
336 if (ret)
337 return ret;
338
Edward O'Callaghan387632a2020-05-06 16:16:47 +1000339 ret |= realtek_mst_i2c_spi_write_register(fd, 0x6D, 0x02); /* write opcode */
340 ret |= realtek_mst_i2c_spi_write_register(fd, 0x71, (PAGE_SIZE - 1)); /* fit len=256 */
Edward O'Callaghan97dd9262020-03-26 00:00:41 +1100341
342 for (i = 0; i < len; i += PAGE_SIZE) {
Edward O'Callaghan387632a2020-05-06 16:16:47 +1000343 uint16_t page_len = min(len - i, PAGE_SIZE);
344 if (len - i < PAGE_SIZE)
345 ret |= realtek_mst_i2c_spi_write_register(fd, 0x71, page_len-1);
346 uint8_t block_idx = (start + i) >> 16;
347 uint8_t page_idx = (start + i) >> 8;
348 ret |= realtek_mst_i2c_spi_map_page(fd, block_idx, page_idx, 0);
349 if (ret)
350 break;
351
352 /* Wait for empty buffer. */
Edward O'Callaghana1ae9fe2020-05-07 11:42:56 +1000353 ret |= realtek_mst_i2c_spi_wait_command_done(fd, MCU_MODE, 0x10, 0x10, 1);
Edward O'Callaghan387632a2020-05-06 16:16:47 +1000354 if (ret)
355 break;
356
357 ret |= realtek_mst_i2c_spi_write_page(fd, MCU_DATA_PORT,
358 buf + i, page_len);
359 if (ret)
360 break;
361 ret |= realtek_mst_i2c_execute_write(fd);
Edward O'Callaghan97dd9262020-03-26 00:00:41 +1100362 if (ret)
363 break;
364 }
365
Edward O'Callaghan387632a2020-05-06 16:16:47 +1000366
Edward O'Callaghan97dd9262020-03-26 00:00:41 +1100367 /* TODO: re-enable the write protection? */
368
369 return ret;
370}
371
372static int realtek_mst_i2c_spi_write_aai(struct flashctx *flash, const uint8_t *buf,
373 unsigned int start, unsigned int len)
374{
375 msg_perr("%s: AAI write function is not supported.\n", __func__);
376 return SPI_GENERIC_ERROR;
377}
378
379static struct spi_master spi_master_i2c_realtek_mst = {
380 .max_data_read = 16,
381 .max_data_write = 8,
382 .command = realtek_mst_i2c_spi_send_command,
383 .multicommand = default_spi_send_multicommand,
384 .read = realtek_mst_i2c_spi_read,
385 .write_256 = realtek_mst_i2c_spi_write_256,
386 .write_aai = realtek_mst_i2c_spi_write_aai,
387};
388
389static int realtek_mst_i2c_spi_shutdown(void *data)
390{
391 int ret = 0;
392 struct realtek_mst_i2c_spi_data *realtek_mst_data =
393 (struct realtek_mst_i2c_spi_data *)data;
394 int fd = realtek_mst_data->fd;
395 ret |= realtek_mst_i2c_spi_reset_mpu(fd);
396 i2c_close(fd);
397 free(data);
398
399 return ret;
400}
401
402static int get_params(int *i2c_bus)
403{
404 char *bus_str = NULL;
405 int ret = SPI_GENERIC_ERROR;
406
407 bus_str = extract_programmer_param("bus");
408 if (bus_str) {
409 char *bus_suffix;
410 errno = 0;
411 int bus = (int)strtol(bus_str, &bus_suffix, 10);
412 if (errno != 0 || bus_str == bus_suffix) {
413 msg_perr("%s: Could not convert 'bus'.\n", __func__);
414 goto get_params_done;
415 }
416
417 if (bus < 0 || bus > 255) {
418 msg_perr("%s: Value for 'bus' is out of range(0-255).\n", __func__);
419 goto get_params_done;
420 }
421
422 if (strlen(bus_suffix) > 0) {
423 msg_perr("%s: Garbage following 'bus' value.\n", __func__);
424 goto get_params_done;
425 }
426
427 msg_pinfo("Using i2c bus %i.\n", bus);
428 *i2c_bus = bus;
429 ret = 0;
430 goto get_params_done;
431 } else {
432 msg_perr("%s: Bus number not specified.\n", __func__);
433 }
434get_params_done:
435 if (bus_str)
436 free(bus_str);
437
438 return ret;
439}
440
441int realtek_mst_i2c_spi_init(void)
442{
443 int ret = 0;
444 int i2c_bus = 0;
445
446 if (get_params(&i2c_bus))
447 return SPI_GENERIC_ERROR;
448
449 int fd = i2c_open(i2c_bus, REGISTER_ADDRESS, 0);
450 if (fd < 0)
451 return fd;
452
Edward O'Callaghan97dd9262020-03-26 00:00:41 +1100453 ret |= realtek_mst_i2c_spi_enter_isp_mode(fd);
454 if (ret)
455 return ret;
Edward O'Callaghan97dd9262020-03-26 00:00:41 +1100456
457 struct realtek_mst_i2c_spi_data *data = calloc(1, sizeof(struct realtek_mst_i2c_spi_data));
458 if (!data) {
459 msg_perr("Unable to allocate space for extra SPI master data.\n");
460 return SPI_GENERIC_ERROR;
461 }
462
463 data->fd = fd;
464 ret |= register_shutdown(realtek_mst_i2c_spi_shutdown, data);
465
466 spi_master_i2c_realtek_mst.data = data;
467 ret |= register_spi_master(&spi_master_i2c_realtek_mst);
468
469 return ret;
470}