Edward O'Callaghan | 97dd926 | 2020-03-26 00:00:41 +1100 | [diff] [blame] | 1 | /* |
| 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'Callaghan | 33653fd | 2020-05-04 13:01:54 +1000 | [diff] [blame] | 34 | #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'Callaghan | 97dd926 | 2020-03-26 00:00:41 +1100 | [diff] [blame] | 43 | //opcodes |
| 44 | #define OPCODE_READ 3 |
| 45 | #define OPCODE_WRITE 2 |
| 46 | |
| 47 | |
| 48 | struct realtek_mst_i2c_spi_data { |
| 49 | int fd; |
| 50 | }; |
| 51 | |
| 52 | static 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 | |
| 61 | static 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 | |
| 70 | static 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 | |
| 82 | static 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 | |
| 88 | static 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 | |
| 97 | static 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 | |
| 114 | static int realtek_mst_i2c_spi_enter_isp_mode(int fd) |
| 115 | { |
Edward O'Callaghan | 33653fd | 2020-05-04 13:01:54 +1000 | [diff] [blame] | 116 | int ret = realtek_mst_i2c_spi_write_register(fd, MCU_MODE, ENTER_ISP_MODE); |
Edward O'Callaghan | 97dd926 | 2020-03-26 00:00:41 +1100 | [diff] [blame] | 117 | |
| 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 | |
| 128 | static 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 |
| 139 | static 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 | |
| 165 | static 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 | |
| 190 | static 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) { |
| 199 | //msg_perr("%s: Invalid read/write count for send command.\n", __func__); |
| 200 | return SPI_GENERIC_ERROR; |
| 201 | } |
| 202 | //printf("%s: writearr[0]=0x%x\n", __func__, writearr[0]); |
| 203 | |
| 204 | int fd = get_fd_from_context(flash); |
| 205 | if (fd < 0) |
| 206 | return SPI_GENERIC_ERROR; |
| 207 | |
| 208 | /* First byte of writearr should be the command value, followed by the value to write. */ |
| 209 | // 0xFF60 = cmd; |
| 210 | writecnt--; |
| 211 | uint8_t ctrl_reg_val = (writecnt << 3) | (readcnt << 1) | (2 << 5); |
| 212 | ret |= realtek_mst_i2c_spi_write_register(fd, 0x60, ctrl_reg_val); |
| 213 | ret |= realtek_mst_i2c_spi_write_register(fd, 0x61, writearr[0]); |
| 214 | |
| 215 | for (i = 0; i < writecnt; ++i) |
| 216 | ret |= realtek_mst_i2c_spi_write_register(fd, 0x64 + i, writearr[i + 1]); |
| 217 | ret |= realtek_mst_i2c_spi_write_register(fd, 0x60, ctrl_reg_val | 0x1); |
| 218 | if (ret) |
| 219 | return ret; |
| 220 | |
| 221 | ret = realtek_mst_i2c_spi_wait_command_done(fd, 0x60, 0x01); |
| 222 | if (ret) |
| 223 | return ret; |
| 224 | |
| 225 | for (i = 0; i < readcnt; ++i) |
| 226 | ret |= realtek_mst_i2c_spi_read_register(fd, 0x67 + i, &readarr[i]); |
| 227 | |
| 228 | //for (i = 0; i< readcnt; i++) |
| 229 | // printf("DEBUG: readarr[%d]=0x%02x\n", i, readarr[i]); |
| 230 | |
| 231 | return ret; |
| 232 | } |
| 233 | |
| 234 | static int realtek_mst_i2c_spi_map_page(int fd, uint8_t block_idx, uint8_t page_idx, uint8_t byte_idx) |
| 235 | { |
| 236 | int ret = 0; |
Edward O'Callaghan | 33653fd | 2020-05-04 13:01:54 +1000 | [diff] [blame] | 237 | ret |= realtek_mst_i2c_spi_write_register(fd, MAP_PAGE_BYTE2, block_idx); |
| 238 | ret |= realtek_mst_i2c_spi_write_register(fd, MAP_PAGE_BYTE1, page_idx); |
| 239 | ret |= realtek_mst_i2c_spi_write_register(fd, MAP_PAGE_BYTE0, byte_idx); |
Edward O'Callaghan | 97dd926 | 2020-03-26 00:00:41 +1100 | [diff] [blame] | 240 | |
| 241 | return ret ? SPI_GENERIC_ERROR : 0; |
| 242 | } |
| 243 | |
| 244 | static int realtek_mst_i2c_spi_read(struct flashctx *flash, uint8_t *buf, |
| 245 | unsigned int start, unsigned int len) |
| 246 | { |
| 247 | unsigned i; |
| 248 | int ret = 0; |
| 249 | |
| 250 | if (start & 0xff) |
| 251 | return default_spi_read(flash, buf, start, len); |
| 252 | |
| 253 | int fd = get_fd_from_context(flash); |
| 254 | if (fd < 0) |
| 255 | return SPI_GENERIC_ERROR; |
| 256 | |
| 257 | start--; |
| 258 | ret |= realtek_mst_i2c_spi_write_register(fd, 0x60, 0x46); // ** |
| 259 | ret |= realtek_mst_i2c_spi_write_register(fd, 0x61, OPCODE_READ); |
| 260 | uint8_t block_idx = start >> 16; |
| 261 | uint8_t page_idx = start >> 8; |
| 262 | uint8_t byte_idx = start; |
| 263 | ret |= realtek_mst_i2c_spi_map_page(fd, block_idx, page_idx, byte_idx); |
| 264 | ret |= realtek_mst_i2c_spi_write_register(fd, 0x6a, 0x03); |
| 265 | ret |= realtek_mst_i2c_spi_write_register(fd, 0x60, 0x47); // ** |
| 266 | if (ret) |
| 267 | return ret; |
| 268 | |
| 269 | ret = realtek_mst_i2c_spi_wait_command_done(fd, 0x60, 0x01); |
| 270 | if (ret) |
| 271 | return ret; |
| 272 | |
| 273 | /** |
| 274 | * The first byte is just a null, probably a status code? |
| 275 | * Advance the read by a offset of one byte and continue. |
| 276 | */ |
| 277 | uint8_t dummy; |
Edward O'Callaghan | 33653fd | 2020-05-04 13:01:54 +1000 | [diff] [blame] | 278 | realtek_mst_i2c_spi_read_register(fd, MCU_DATA_PORT, &dummy); |
Edward O'Callaghan | 97dd926 | 2020-03-26 00:00:41 +1100 | [diff] [blame] | 279 | |
| 280 | for (i = 0; i < len; i += PAGE_SIZE) { |
| 281 | ret |= realtek_mst_i2c_spi_read_data(fd, REGISTER_ADDRESS, |
| 282 | buf + i, min(len - i, PAGE_SIZE)); |
| 283 | if (ret) |
| 284 | return ret; |
| 285 | } |
| 286 | |
| 287 | return ret; |
| 288 | } |
| 289 | |
| 290 | static int realtek_mst_i2c_spi_write_256(struct flashctx *flash, const uint8_t *buf, |
| 291 | unsigned int start, unsigned int len) |
| 292 | { |
| 293 | unsigned i; |
| 294 | int ret = 0; |
| 295 | |
| 296 | if (start & 0xff) |
| 297 | return default_spi_write_256(flash, buf, start, len); |
| 298 | |
| 299 | int fd = get_fd_from_context(flash); |
| 300 | if (fd < 0) |
| 301 | return SPI_GENERIC_ERROR; |
| 302 | |
| 303 | ret = realtek_mst_i2c_spi_disable_protection(fd); |
| 304 | if (ret) |
| 305 | return ret; |
| 306 | |
| 307 | start--; |
| 308 | ret |= realtek_mst_i2c_spi_write_register(fd, 0x60, 0x46); // ** |
| 309 | ret |= realtek_mst_i2c_spi_write_register(fd, 0x61, OPCODE_WRITE); |
| 310 | uint8_t block_idx = start >> 16; |
| 311 | uint8_t page_idx = start >> 8; |
| 312 | uint8_t byte_idx = start; |
| 313 | ret |= realtek_mst_i2c_spi_map_page(fd, block_idx, page_idx, byte_idx); |
| 314 | ret |= realtek_mst_i2c_spi_write_register(fd, 0x6a, 0x03); |
| 315 | ret |= realtek_mst_i2c_spi_write_register(fd, 0x60, 0x47); // ** |
| 316 | if (ret) |
| 317 | goto fail; |
| 318 | |
| 319 | ret = realtek_mst_i2c_spi_wait_command_done(fd, 0x60, 0x01); |
| 320 | if (ret) |
| 321 | goto fail; |
| 322 | |
| 323 | for (i = 0; i < len; i += PAGE_SIZE) { |
| 324 | ret |= realtek_mst_i2c_spi_write_data(fd, REGISTER_ADDRESS, |
| 325 | (uint8_t *)buf + i, min(len - i, PAGE_SIZE)); |
| 326 | if (ret) |
| 327 | break; |
| 328 | } |
| 329 | |
| 330 | fail: |
| 331 | /* TODO: re-enable the write protection? */ |
| 332 | |
| 333 | return ret; |
| 334 | } |
| 335 | |
| 336 | static int realtek_mst_i2c_spi_write_aai(struct flashctx *flash, const uint8_t *buf, |
| 337 | unsigned int start, unsigned int len) |
| 338 | { |
| 339 | msg_perr("%s: AAI write function is not supported.\n", __func__); |
| 340 | return SPI_GENERIC_ERROR; |
| 341 | } |
| 342 | |
| 343 | static struct spi_master spi_master_i2c_realtek_mst = { |
| 344 | .max_data_read = 16, |
| 345 | .max_data_write = 8, |
| 346 | .command = realtek_mst_i2c_spi_send_command, |
| 347 | .multicommand = default_spi_send_multicommand, |
| 348 | .read = realtek_mst_i2c_spi_read, |
| 349 | .write_256 = realtek_mst_i2c_spi_write_256, |
| 350 | .write_aai = realtek_mst_i2c_spi_write_aai, |
| 351 | }; |
| 352 | |
| 353 | static int realtek_mst_i2c_spi_shutdown(void *data) |
| 354 | { |
| 355 | int ret = 0; |
| 356 | struct realtek_mst_i2c_spi_data *realtek_mst_data = |
| 357 | (struct realtek_mst_i2c_spi_data *)data; |
| 358 | int fd = realtek_mst_data->fd; |
| 359 | ret |= realtek_mst_i2c_spi_reset_mpu(fd); |
| 360 | i2c_close(fd); |
| 361 | free(data); |
| 362 | |
| 363 | return ret; |
| 364 | } |
| 365 | |
| 366 | static int get_params(int *i2c_bus) |
| 367 | { |
| 368 | char *bus_str = NULL; |
| 369 | int ret = SPI_GENERIC_ERROR; |
| 370 | |
| 371 | bus_str = extract_programmer_param("bus"); |
| 372 | if (bus_str) { |
| 373 | char *bus_suffix; |
| 374 | errno = 0; |
| 375 | int bus = (int)strtol(bus_str, &bus_suffix, 10); |
| 376 | if (errno != 0 || bus_str == bus_suffix) { |
| 377 | msg_perr("%s: Could not convert 'bus'.\n", __func__); |
| 378 | goto get_params_done; |
| 379 | } |
| 380 | |
| 381 | if (bus < 0 || bus > 255) { |
| 382 | msg_perr("%s: Value for 'bus' is out of range(0-255).\n", __func__); |
| 383 | goto get_params_done; |
| 384 | } |
| 385 | |
| 386 | if (strlen(bus_suffix) > 0) { |
| 387 | msg_perr("%s: Garbage following 'bus' value.\n", __func__); |
| 388 | goto get_params_done; |
| 389 | } |
| 390 | |
| 391 | msg_pinfo("Using i2c bus %i.\n", bus); |
| 392 | *i2c_bus = bus; |
| 393 | ret = 0; |
| 394 | goto get_params_done; |
| 395 | } else { |
| 396 | msg_perr("%s: Bus number not specified.\n", __func__); |
| 397 | } |
| 398 | get_params_done: |
| 399 | if (bus_str) |
| 400 | free(bus_str); |
| 401 | |
| 402 | return ret; |
| 403 | } |
| 404 | |
| 405 | int realtek_mst_i2c_spi_init(void) |
| 406 | { |
| 407 | int ret = 0; |
| 408 | int i2c_bus = 0; |
| 409 | |
| 410 | if (get_params(&i2c_bus)) |
| 411 | return SPI_GENERIC_ERROR; |
| 412 | |
| 413 | int fd = i2c_open(i2c_bus, REGISTER_ADDRESS, 0); |
| 414 | if (fd < 0) |
| 415 | return fd; |
| 416 | |
| 417 | /* Ensure we are in a known state before entering ISP mode */ |
| 418 | ret |= realtek_mst_i2c_spi_reset_mpu(fd); |
| 419 | if (ret) |
| 420 | return ret; |
| 421 | |
| 422 | ret |= realtek_mst_i2c_spi_enter_isp_mode(fd); |
| 423 | if (ret) |
| 424 | return ret; |
| 425 | // XXX: maybe make into a mode:defaults cli param? |
| 426 | #if 0 |
| 427 | ret |= realtek_mst_i2c_spi_set_defaults(fd); |
| 428 | if (ret) |
| 429 | return ret; |
| 430 | #endif |
| 431 | |
| 432 | struct realtek_mst_i2c_spi_data *data = calloc(1, sizeof(struct realtek_mst_i2c_spi_data)); |
| 433 | if (!data) { |
| 434 | msg_perr("Unable to allocate space for extra SPI master data.\n"); |
| 435 | return SPI_GENERIC_ERROR; |
| 436 | } |
| 437 | |
| 438 | data->fd = fd; |
| 439 | ret |= register_shutdown(realtek_mst_i2c_spi_shutdown, data); |
| 440 | |
| 441 | spi_master_i2c_realtek_mst.data = data; |
| 442 | ret |= register_spi_master(&spi_master_i2c_realtek_mst); |
| 443 | |
| 444 | return ret; |
| 445 | } |