Shiyu Sun | 9dde716 | 2020-04-16 17:32:55 +1000 | [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 | #define REGISTER_ADDRESS (0x94 >> 1) |
| 29 | #define PAGE_ADDRESS (0x9e >> 1) |
Angel Pons | be532a5 | 2021-04-17 15:32:12 +0200 | [diff] [blame] | 30 | #define LSPCON_PAGE_SIZE 256 |
Shiyu Sun | 9dde716 | 2020-04-16 17:32:55 +1000 | [diff] [blame] | 31 | #define MAX_SPI_WAIT_RETRIES 1000 |
| 32 | |
| 33 | #define CLT2_SPI 0x82 |
| 34 | #define SPIEDID_BASE_ADDR2 0x8d |
| 35 | #define ROMADDR_BYTE1 0x8e |
| 36 | #define ROMADDR_BYTE2 0x8f |
| 37 | #define SWSPI_WDATA 0x90 |
| 38 | #define SWSPI_WDATA_CLEAR_STATUS 0x00 |
| 39 | #define SWSPI_WDATA_WRITE_REGISTER 0x01 |
| 40 | #define SWSPI_WDATA_READ_REGISTER 0x05 |
| 41 | #define SWSPI_WDATA_ENABLE_REGISTER 0x06 |
| 42 | #define SWSPI_WDATA_SECTOR_ERASE 0x20 |
| 43 | #define SWSPI_WDATA_PROTECT_BP 0x8c |
| 44 | #define SWSPI_RDATA 0x91 |
| 45 | #define SWSPI_LEN 0x92 |
| 46 | #define SWSPICTL 0x93 |
| 47 | #define SWSPICTL_ACCESS_TRIGGER 1 |
| 48 | #define SWSPICTL_CLEAR_PTR (1 << 1) |
| 49 | #define SWSPICTL_NO_READ (1 << 2) |
| 50 | #define SWSPICTL_ENABLE_READBACK (1 << 3) |
| 51 | #define SWSPICTL_MOT (1 << 4) |
| 52 | #define SPISTATUS 0x9e |
| 53 | #define SPISTATUS_BYTE_PROGRAM_FINISHED 0 |
| 54 | #define SPISTATUS_BYTE_PROGRAM_IN_IF 1 |
| 55 | #define SPISTATUS_BYTE_PROGRAM_SEND_DONE (1 << 1) |
| 56 | #define SPISTATUS_SECTOR_ERASE_FINISHED 0 |
| 57 | #define SPISTATUS_SECTOR_ERASE_IN_IF (1 << 2) |
| 58 | #define SPISTATUS_SECTOR_ERASE_SEND_DONE (1 << 3) |
| 59 | #define SPISTATUS_CHIP_ERASE_FINISHED 0 |
| 60 | #define SPISTATUS_CHIP_ERASE_IN_IF (1 << 4) |
| 61 | #define SPISTATUS_CHIP_ERASE_SEND_DONE (1 << 5) |
| 62 | #define SPISTATUS_FW_UPDATE_ENABLE (1 << 6) |
| 63 | #define WRITE_PROTECTION 0xb3 |
| 64 | #define WRITE_PROTECTION_ON 0 |
| 65 | #define WRITE_PROTECTION_OFF 0x10 |
| 66 | #define MPU 0xbc |
| 67 | #define PAGE_HW_WRITE 0xda |
| 68 | #define PAGE_HW_WRITE_DISABLE 0 |
| 69 | #define PAGE_HW_COFIG_REGISTER 0xaa |
| 70 | #define PAGE_HW_WRITE_ENABLE 0x55 |
| 71 | |
| 72 | struct lspcon_i2c_spi_data { |
| 73 | int fd; |
| 74 | }; |
| 75 | |
| 76 | typedef struct { |
| 77 | uint8_t command; |
| 78 | const uint8_t *data; |
| 79 | uint8_t data_size; |
| 80 | uint8_t control; |
| 81 | } packet_t; |
| 82 | |
| 83 | static int lspcon_i2c_spi_write_data(int fd, uint16_t addr, void *buf, uint16_t len) |
| 84 | { |
| 85 | i2c_buffer_t data; |
| 86 | if (i2c_buffer_t_fill(&data, buf, len)) |
| 87 | return SPI_GENERIC_ERROR; |
| 88 | |
| 89 | return i2c_write(fd, addr, &data) == len ? 0 : SPI_GENERIC_ERROR; |
| 90 | } |
| 91 | |
| 92 | static int lspcon_i2c_spi_read_data(int fd, uint16_t addr, void *buf, uint16_t len) |
| 93 | { |
| 94 | i2c_buffer_t data; |
| 95 | if (i2c_buffer_t_fill(&data, buf, len)) |
| 96 | return SPI_GENERIC_ERROR; |
| 97 | |
| 98 | return i2c_read(fd, addr, &data) == len ? 0 : SPI_GENERIC_ERROR; |
| 99 | } |
| 100 | |
| 101 | static int get_fd_from_context(const struct flashctx *flash) |
| 102 | { |
| 103 | if (!flash || !flash->mst || !flash->mst->spi.data) { |
| 104 | msg_perr("Unable to extract fd from flash context.\n"); |
| 105 | return SPI_GENERIC_ERROR; |
| 106 | } |
| 107 | const struct lspcon_i2c_spi_data *data = |
| 108 | (const struct lspcon_i2c_spi_data *)flash->mst->spi.data; |
| 109 | |
| 110 | return data->fd; |
| 111 | } |
| 112 | |
| 113 | static int lspcon_i2c_spi_write_register(int fd, uint8_t i2c_register, uint8_t value) |
| 114 | { |
| 115 | uint8_t command[] = { i2c_register, value }; |
| 116 | return lspcon_i2c_spi_write_data(fd, REGISTER_ADDRESS, command, 2); |
| 117 | } |
| 118 | |
| 119 | static int lspcon_i2c_spi_read_register(int fd, uint8_t i2c_register, uint8_t *value) |
| 120 | { |
| 121 | uint8_t command[] = { i2c_register }; |
| 122 | int ret = lspcon_i2c_spi_write_data(fd, REGISTER_ADDRESS, command, 1); |
| 123 | ret |= lspcon_i2c_spi_read_data(fd, REGISTER_ADDRESS, value, 1); |
| 124 | |
| 125 | return ret ? SPI_GENERIC_ERROR : 0; |
| 126 | } |
| 127 | |
| 128 | static int lspcon_i2c_spi_register_control(int fd, packet_t *packet) |
| 129 | { |
| 130 | int i; |
| 131 | int ret = lspcon_i2c_spi_write_register(fd, SWSPI_WDATA, packet->command); |
| 132 | if (ret) |
| 133 | return ret; |
| 134 | |
| 135 | /* Higher 4 bits are read size. */ |
| 136 | int write_size = packet->data_size & 0x0f; |
| 137 | for (i = 0; i < write_size; ++i) { |
| 138 | ret |= lspcon_i2c_spi_write_register(fd, SWSPI_WDATA, packet->data[i]); |
| 139 | } |
| 140 | |
| 141 | ret |= lspcon_i2c_spi_write_register(fd, SWSPI_LEN, packet->data_size); |
| 142 | ret |= lspcon_i2c_spi_write_register(fd, SWSPICTL, packet->control); |
| 143 | |
| 144 | return ret; |
| 145 | } |
| 146 | |
| 147 | static int lspcon_i2c_spi_wait_command_done(int fd, unsigned int offset, int mask) |
| 148 | { |
| 149 | uint8_t val; |
| 150 | int tried = 0; |
| 151 | int ret = 0; |
| 152 | do { |
| 153 | ret |= lspcon_i2c_spi_read_register(fd, offset, &val); |
Edward O'Callaghan | 53da100 | 2020-04-24 13:39:55 +1000 | [diff] [blame] | 154 | } while (!ret && (val & mask) && ++tried < MAX_SPI_WAIT_RETRIES); |
Shiyu Sun | 9dde716 | 2020-04-16 17:32:55 +1000 | [diff] [blame] | 155 | |
| 156 | if (tried == MAX_SPI_WAIT_RETRIES) { |
Shiyu Sun | 6e8b54c | 2020-04-30 16:51:09 +1000 | [diff] [blame] | 157 | msg_perr("%s: Time out on sending command.\n", __func__); |
Shiyu Sun | 9dde716 | 2020-04-16 17:32:55 +1000 | [diff] [blame] | 158 | return -MAX_SPI_WAIT_RETRIES; |
| 159 | } |
| 160 | |
| 161 | return (val & mask) ? SPI_GENERIC_ERROR : ret; |
| 162 | } |
| 163 | |
| 164 | static int lspcon_i2c_spi_wait_rom_free(int fd) |
| 165 | { |
| 166 | uint8_t val; |
| 167 | int tried = 0; |
| 168 | int ret = 0; |
| 169 | ret |= lspcon_i2c_spi_wait_command_done(fd, SPISTATUS, |
| 170 | SPISTATUS_SECTOR_ERASE_IN_IF | SPISTATUS_SECTOR_ERASE_SEND_DONE); |
| 171 | if (ret) |
| 172 | return ret; |
| 173 | |
| 174 | do { |
| 175 | packet_t packet = { SWSPI_WDATA_READ_REGISTER, NULL, 0, SWSPICTL_ACCESS_TRIGGER }; |
| 176 | ret |= lspcon_i2c_spi_register_control(fd, &packet); |
| 177 | ret |= lspcon_i2c_spi_wait_command_done(fd, SWSPICTL, SWSPICTL_ACCESS_TRIGGER); |
| 178 | ret |= lspcon_i2c_spi_read_register(fd, SWSPI_RDATA, &val); |
| 179 | } while (!ret && (val & SWSPICTL_ACCESS_TRIGGER) && ++tried < MAX_SPI_WAIT_RETRIES); |
| 180 | |
| 181 | if (tried == MAX_SPI_WAIT_RETRIES) { |
Shiyu Sun | 6e8b54c | 2020-04-30 16:51:09 +1000 | [diff] [blame] | 182 | msg_perr("%s: Time out on waiting ROM free.\n", __func__); |
Shiyu Sun | 9dde716 | 2020-04-16 17:32:55 +1000 | [diff] [blame] | 183 | return -MAX_SPI_WAIT_RETRIES; |
| 184 | } |
| 185 | |
| 186 | return (val & SWSPICTL_ACCESS_TRIGGER) ? SPI_GENERIC_ERROR : ret; |
| 187 | } |
| 188 | |
| 189 | static int lspcon_i2c_spi_toggle_register_protection(int fd, int toggle) |
| 190 | { |
| 191 | return lspcon_i2c_spi_write_register(fd, WRITE_PROTECTION, |
| 192 | toggle ? WRITE_PROTECTION_OFF : WRITE_PROTECTION_ON); |
| 193 | } |
| 194 | |
| 195 | static int lspcon_i2c_spi_enable_write_status_register(int fd) |
| 196 | { |
| 197 | int ret = lspcon_i2c_spi_toggle_register_protection(fd, 1); |
| 198 | packet_t packet = { |
| 199 | SWSPI_WDATA_ENABLE_REGISTER, NULL, 0, SWSPICTL_ACCESS_TRIGGER | SWSPICTL_NO_READ }; |
| 200 | ret |= lspcon_i2c_spi_register_control(fd, &packet); |
| 201 | ret |= lspcon_i2c_spi_toggle_register_protection(fd, 0); |
| 202 | |
| 203 | return ret; |
| 204 | } |
| 205 | |
| 206 | static int lspcon_i2c_spi_enable_write_status_register_protection(int fd) |
| 207 | { |
| 208 | int ret = lspcon_i2c_spi_toggle_register_protection(fd, 1); |
| 209 | uint8_t data[] = { SWSPI_WDATA_PROTECT_BP }; |
| 210 | packet_t packet = { |
| 211 | SWSPI_WDATA_WRITE_REGISTER, data, 1, SWSPICTL_ACCESS_TRIGGER | SWSPICTL_NO_READ }; |
| 212 | ret |= lspcon_i2c_spi_register_control(fd, &packet); |
| 213 | ret |= lspcon_i2c_spi_toggle_register_protection(fd, 0); |
| 214 | |
| 215 | return ret; |
| 216 | } |
| 217 | |
| 218 | static int lspcon_i2c_spi_disable_protection(int fd) |
| 219 | { |
| 220 | int ret = lspcon_i2c_spi_toggle_register_protection(fd, 1); |
| 221 | uint8_t data[] = { SWSPI_WDATA_CLEAR_STATUS }; |
| 222 | packet_t packet = { |
| 223 | SWSPI_WDATA_WRITE_REGISTER, data, 1, SWSPICTL_ACCESS_TRIGGER | SWSPICTL_NO_READ }; |
| 224 | ret |= lspcon_i2c_spi_register_control(fd, &packet); |
| 225 | ret |= lspcon_i2c_spi_toggle_register_protection(fd, 0); |
| 226 | |
| 227 | return ret; |
| 228 | } |
| 229 | |
| 230 | static int lspcon_i2c_spi_disable_hw_write(int fd) |
| 231 | { |
| 232 | return lspcon_i2c_spi_write_register(fd, PAGE_HW_WRITE, PAGE_HW_WRITE_DISABLE); |
| 233 | } |
| 234 | |
| 235 | static int lspcon_i2c_spi_enable_write_protection(int fd) |
| 236 | { |
| 237 | int ret = lspcon_i2c_spi_enable_write_status_register(fd); |
| 238 | ret |= lspcon_i2c_spi_enable_write_status_register_protection(fd); |
| 239 | ret |= lspcon_i2c_spi_wait_rom_free(fd); |
| 240 | ret |= lspcon_i2c_spi_disable_hw_write(fd); |
| 241 | |
| 242 | return ret; |
| 243 | } |
| 244 | |
| 245 | static int lspcon_i2c_spi_disable_all_protection(int fd) |
| 246 | { |
| 247 | int ret = lspcon_i2c_spi_enable_write_status_register(fd); |
| 248 | ret |= lspcon_i2c_spi_disable_protection(fd); |
| 249 | ret |= lspcon_i2c_spi_wait_rom_free(fd); |
| 250 | |
| 251 | return ret; |
| 252 | } |
| 253 | |
| 254 | static int lspcon_i2c_spi_send_command(const struct flashctx *flash, |
| 255 | unsigned int writecnt, unsigned int readcnt, |
| 256 | const unsigned char *writearr, |
| 257 | unsigned char *readarr) |
| 258 | { |
| 259 | unsigned int i; |
| 260 | if (writecnt > 16 || readcnt > 16 || writecnt == 0) { |
Shiyu Sun | 6e8b54c | 2020-04-30 16:51:09 +1000 | [diff] [blame] | 261 | msg_perr("%s: Invalid read/write count for send command.\n", |
| 262 | __func__); |
Shiyu Sun | 9dde716 | 2020-04-16 17:32:55 +1000 | [diff] [blame] | 263 | return SPI_GENERIC_ERROR; |
| 264 | } |
| 265 | |
| 266 | int fd = get_fd_from_context(flash); |
| 267 | if (fd < 0) |
| 268 | return SPI_GENERIC_ERROR; |
| 269 | |
| 270 | int ret = lspcon_i2c_spi_disable_all_protection(fd); |
| 271 | ret |= lspcon_i2c_spi_enable_write_status_register(fd); |
| 272 | ret |= lspcon_i2c_spi_toggle_register_protection(fd, 1); |
| 273 | |
| 274 | /* First byte of writearr shuld be the command value, followed by the value to write. |
| 275 | Read length occupies 4 bit and represents 16 level, thus if read 1 byte, |
| 276 | read length should be set 0. */ |
| 277 | packet_t packet = { |
| 278 | writearr[0], &writearr[1], (writecnt - 1) | ((readcnt - 1) << 4), |
| 279 | SWSPICTL_ACCESS_TRIGGER | (readcnt ? 0 : SWSPICTL_NO_READ), |
| 280 | }; |
| 281 | |
| 282 | ret |= lspcon_i2c_spi_register_control(fd, &packet); |
| 283 | ret |= lspcon_i2c_spi_wait_command_done(fd, SWSPICTL, SWSPICTL_ACCESS_TRIGGER); |
| 284 | ret |= lspcon_i2c_spi_toggle_register_protection(fd, 0); |
| 285 | if (ret) |
| 286 | return ret; |
| 287 | |
| 288 | for (i = 0; i < readcnt; ++i) { |
| 289 | ret |= lspcon_i2c_spi_read_register(fd, SWSPI_RDATA, &readarr[i]); |
| 290 | } |
| 291 | |
| 292 | ret |= lspcon_i2c_spi_wait_rom_free(fd); |
| 293 | |
| 294 | return ret; |
| 295 | } |
| 296 | |
| 297 | static int lspcon_i2c_spi_enable_hw_write(int fd) |
| 298 | { |
| 299 | int ret = 0; |
| 300 | ret |= lspcon_i2c_spi_write_register(fd, PAGE_HW_WRITE, PAGE_HW_COFIG_REGISTER); |
| 301 | ret |= lspcon_i2c_spi_write_register(fd, PAGE_HW_WRITE, PAGE_HW_WRITE_ENABLE); |
| 302 | ret |= lspcon_i2c_spi_write_register(fd, PAGE_HW_WRITE, 0x50); |
| 303 | ret |= lspcon_i2c_spi_write_register(fd, PAGE_HW_WRITE, 0x41); |
| 304 | ret |= lspcon_i2c_spi_write_register(fd, PAGE_HW_WRITE, 0x52); |
| 305 | ret |= lspcon_i2c_spi_write_register(fd, PAGE_HW_WRITE, 0x44); |
| 306 | |
| 307 | return ret; |
| 308 | } |
| 309 | |
| 310 | static int lspcon_i2c_clt2_spi_reset(int fd) |
| 311 | { |
| 312 | int ret = 0; |
| 313 | ret |= lspcon_i2c_spi_write_register(fd, CLT2_SPI, 0x20); |
| 314 | struct timespec wait_100ms = { 0, (unsigned)1e8 }; |
| 315 | nanosleep(&wait_100ms, NULL); |
| 316 | ret |= lspcon_i2c_spi_write_register(fd, CLT2_SPI, 0x00); |
| 317 | |
| 318 | return ret; |
| 319 | } |
| 320 | |
| 321 | static int lspcon_i2c_spi_reset_mpu_stop(int fd) |
| 322 | { |
| 323 | int ret = 0; |
| 324 | ret |= lspcon_i2c_spi_write_register(fd, MPU, 0xc0); // cmd mode |
| 325 | ret |= lspcon_i2c_spi_write_register(fd, MPU, 0x40); // stop mcu |
| 326 | |
| 327 | return ret; |
| 328 | } |
| 329 | |
| 330 | static int lspcon_i2c_spi_map_page(int fd, unsigned int offset) |
| 331 | { |
| 332 | int ret = 0; |
Angel Pons | be532a5 | 2021-04-17 15:32:12 +0200 | [diff] [blame] | 333 | /* Page number byte, need to / LSPCON_PAGE_SIZE. */ |
Shiyu Sun | 9dde716 | 2020-04-16 17:32:55 +1000 | [diff] [blame] | 334 | ret |= lspcon_i2c_spi_write_register(fd, ROMADDR_BYTE1, (offset >> 8) & 0xff); |
| 335 | ret |= lspcon_i2c_spi_write_register(fd, ROMADDR_BYTE2, (offset >> 16)); |
| 336 | |
| 337 | return ret ? SPI_GENERIC_ERROR : 0; |
| 338 | } |
| 339 | |
| 340 | static int lspcon_i2c_spi_read(struct flashctx *flash, uint8_t *buf, |
| 341 | unsigned int start, unsigned int len) |
| 342 | { |
| 343 | unsigned int i; |
| 344 | int ret = 0; |
| 345 | if (start & 0xff) |
| 346 | return default_spi_read(flash, buf, start, len); |
| 347 | |
| 348 | int fd = get_fd_from_context(flash); |
| 349 | if (fd < 0) |
| 350 | return SPI_GENERIC_ERROR; |
| 351 | |
Angel Pons | be532a5 | 2021-04-17 15:32:12 +0200 | [diff] [blame] | 352 | for (i = 0; i < len; i += LSPCON_PAGE_SIZE) { |
Shiyu Sun | 9dde716 | 2020-04-16 17:32:55 +1000 | [diff] [blame] | 353 | ret |= lspcon_i2c_spi_map_page(fd, start + i); |
Angel Pons | be532a5 | 2021-04-17 15:32:12 +0200 | [diff] [blame] | 354 | ret |= lspcon_i2c_spi_read_data(fd, PAGE_ADDRESS, buf + i, min(len - i, LSPCON_PAGE_SIZE)); |
Shiyu Sun | 9dde716 | 2020-04-16 17:32:55 +1000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | return ret; |
| 358 | } |
| 359 | |
| 360 | static int lspcon_i2c_spi_write_page(int fd, const uint8_t *buf, unsigned int len) |
| 361 | { |
| 362 | /** |
| 363 | * Using static buffer with maximum possible size, |
| 364 | * extra byte is needed for prefixing zero at index 0. |
| 365 | */ |
Angel Pons | be532a5 | 2021-04-17 15:32:12 +0200 | [diff] [blame] | 366 | uint8_t write_buffer[LSPCON_PAGE_SIZE + 1] = { 0 }; |
| 367 | if (len > LSPCON_PAGE_SIZE) |
Shiyu Sun | 9dde716 | 2020-04-16 17:32:55 +1000 | [diff] [blame] | 368 | return SPI_GENERIC_ERROR; |
| 369 | |
| 370 | /* First byte represents the writing offset and should always be zero. */ |
| 371 | memcpy(&write_buffer[1], buf, len); |
| 372 | |
| 373 | return lspcon_i2c_spi_write_data(fd, PAGE_ADDRESS, write_buffer, len + 1); |
| 374 | } |
| 375 | |
| 376 | static int lspcon_i2c_spi_write_256(struct flashctx *flash, const uint8_t *buf, |
| 377 | unsigned int start, unsigned int len) |
| 378 | { |
| 379 | int ret = 0; |
Shiyu Sun | 9dde716 | 2020-04-16 17:32:55 +1000 | [diff] [blame] | 380 | if (start & 0xff) |
| 381 | return default_spi_write_256(flash, buf, start, len); |
| 382 | |
| 383 | int fd = get_fd_from_context(flash); |
| 384 | if (fd < 0) |
| 385 | return SPI_GENERIC_ERROR; |
| 386 | |
| 387 | ret |= lspcon_i2c_spi_disable_all_protection(fd); |
| 388 | /* Enable hardware write and reset clt2SPI interface. */ |
| 389 | ret |= lspcon_i2c_spi_enable_hw_write(fd); |
| 390 | ret |= lspcon_i2c_clt2_spi_reset(fd); |
| 391 | |
Angel Pons | be532a5 | 2021-04-17 15:32:12 +0200 | [diff] [blame] | 392 | for (unsigned int i = 0; i < len; i += LSPCON_PAGE_SIZE) { |
Shiyu Sun | 9dde716 | 2020-04-16 17:32:55 +1000 | [diff] [blame] | 393 | ret |= lspcon_i2c_spi_map_page(fd, start + i); |
Angel Pons | be532a5 | 2021-04-17 15:32:12 +0200 | [diff] [blame] | 394 | ret |= lspcon_i2c_spi_write_page(fd, buf + i, min(len - i, LSPCON_PAGE_SIZE)); |
Shiyu Sun | 9dde716 | 2020-04-16 17:32:55 +1000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | ret |= lspcon_i2c_spi_enable_write_protection(fd); |
| 398 | ret |= lspcon_i2c_spi_disable_hw_write(fd); |
| 399 | |
| 400 | return ret; |
| 401 | } |
| 402 | |
| 403 | static int lspcon_i2c_spi_write_aai(struct flashctx *flash, const uint8_t *buf, |
| 404 | unsigned int start, unsigned int len) |
| 405 | { |
Shiyu Sun | 6e8b54c | 2020-04-30 16:51:09 +1000 | [diff] [blame] | 406 | msg_perr("%s: AAI write function is not supported.\n", |
| 407 | __func__); |
Shiyu Sun | 9dde716 | 2020-04-16 17:32:55 +1000 | [diff] [blame] | 408 | return SPI_GENERIC_ERROR; |
| 409 | } |
| 410 | |
Nico Huber | 2ef004f | 2021-05-11 17:53:34 +0200 | [diff] [blame] | 411 | static const struct spi_master spi_master_i2c_lspcon = { |
Shiyu Sun | 9dde716 | 2020-04-16 17:32:55 +1000 | [diff] [blame] | 412 | .max_data_read = 16, |
| 413 | .max_data_write = 12, |
| 414 | .command = lspcon_i2c_spi_send_command, |
| 415 | .multicommand = default_spi_send_multicommand, |
| 416 | .read = lspcon_i2c_spi_read, |
| 417 | .write_256 = lspcon_i2c_spi_write_256, |
| 418 | .write_aai = lspcon_i2c_spi_write_aai, |
| 419 | }; |
| 420 | |
| 421 | /* TODO: MPU still stopped at this point, probably need to reset it. */ |
| 422 | static int lspcon_i2c_spi_shutdown(void *data) |
| 423 | { |
| 424 | int ret = 0; |
| 425 | struct lspcon_i2c_spi_data *lspcon_data = |
| 426 | (struct lspcon_i2c_spi_data *)data; |
| 427 | int fd = lspcon_data->fd; |
| 428 | ret |= lspcon_i2c_spi_enable_write_protection(fd); |
| 429 | ret |= lspcon_i2c_spi_toggle_register_protection(fd, 0); |
| 430 | i2c_close(fd); |
| 431 | free(data); |
| 432 | |
| 433 | return ret; |
| 434 | } |
| 435 | |
Shiyu Sun | 9dde716 | 2020-04-16 17:32:55 +1000 | [diff] [blame] | 436 | int lspcon_i2c_spi_init(void) |
| 437 | { |
Angel Pons | e0be7a0 | 2021-05-02 18:56:45 +0200 | [diff] [blame] | 438 | int fd = i2c_open_from_programmer_params(REGISTER_ADDRESS, 0); |
Shiyu Sun | 9dde716 | 2020-04-16 17:32:55 +1000 | [diff] [blame] | 439 | if (fd < 0) |
| 440 | return fd; |
| 441 | |
Peter Marheine | 659f7b4 | 2021-03-31 11:28:11 +1100 | [diff] [blame] | 442 | int ret = lspcon_i2c_spi_reset_mpu_stop(fd); |
Edward O'Callaghan | ed6c711 | 2020-04-17 14:10:55 +1000 | [diff] [blame] | 443 | if (ret) { |
| 444 | msg_perr("%s: call to reset_mpu_stop failed.\n", __func__); |
Angel Pons | 911704f | 2021-05-02 19:00:37 +0200 | [diff] [blame] | 445 | i2c_close(fd); |
Shiyu Sun | 9dde716 | 2020-04-16 17:32:55 +1000 | [diff] [blame] | 446 | return ret; |
Edward O'Callaghan | ed6c711 | 2020-04-17 14:10:55 +1000 | [diff] [blame] | 447 | } |
Shiyu Sun | 9dde716 | 2020-04-16 17:32:55 +1000 | [diff] [blame] | 448 | |
| 449 | struct lspcon_i2c_spi_data *data = calloc(1, sizeof(struct lspcon_i2c_spi_data)); |
| 450 | if (!data) { |
| 451 | msg_perr("Unable to allocate space for extra SPI master data.\n"); |
Angel Pons | 911704f | 2021-05-02 19:00:37 +0200 | [diff] [blame] | 452 | i2c_close(fd); |
Shiyu Sun | 9dde716 | 2020-04-16 17:32:55 +1000 | [diff] [blame] | 453 | return SPI_GENERIC_ERROR; |
| 454 | } |
| 455 | |
| 456 | data->fd = fd; |
Edward O'Callaghan | ed6c711 | 2020-04-17 14:10:55 +1000 | [diff] [blame] | 457 | |
Shiyu Sun | 9dde716 | 2020-04-16 17:32:55 +1000 | [diff] [blame] | 458 | ret |= register_shutdown(lspcon_i2c_spi_shutdown, data); |
Nico Huber | 2ef004f | 2021-05-11 17:53:34 +0200 | [diff] [blame] | 459 | ret |= register_spi_master(&spi_master_i2c_lspcon, data); |
Shiyu Sun | 9dde716 | 2020-04-16 17:32:55 +1000 | [diff] [blame] | 460 | |
| 461 | return ret; |
| 462 | } |