blob: 096fb7620042b874c345350dc26a1dfe4873b928 [file] [log] [blame]
Shiyu Sun13a2ef62020-03-19 14:37:57 +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#define REGISTER_ADDRESS (0x94 >> 1)
29#define PAGE_ADDRESS (0x9e >> 1)
Angel Ponscbe8a392021-04-17 15:32:12 +020030#define LSPCON_PAGE_SIZE 256
Shiyu Sun13a2ef62020-03-19 14:37:57 +110031#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
72struct lspcon_i2c_spi_data {
73 int fd;
74};
75
76typedef struct {
77 uint8_t command;
78 const uint8_t *data;
79 uint8_t data_size;
80 uint8_t control;
81} packet_t;
82
83static 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
92static 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
Edward O'Callaghane4ddc362020-04-12 17:27:53 +1000101static int get_fd_from_context(const struct flashctx *flash)
Shiyu Sun13a2ef62020-03-19 14:37:57 +1100102{
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
113static 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
119static 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
128static 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
147static 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'Callaghan94991d82020-04-24 13:39:55 +1000154 } while (!ret && (val & mask) && ++tried < MAX_SPI_WAIT_RETRIES);
Shiyu Sun13a2ef62020-03-19 14:37:57 +1100155
156 if (tried == MAX_SPI_WAIT_RETRIES) {
Shiyu Sun7f87f9f2020-04-30 16:51:09 +1000157 msg_perr("%s: Time out on sending command.\n", __func__);
Shiyu Sun13a2ef62020-03-19 14:37:57 +1100158 return -MAX_SPI_WAIT_RETRIES;
159 }
160
161 return (val & mask) ? SPI_GENERIC_ERROR : ret;
162}
163
164static 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 Sun7f87f9f2020-04-30 16:51:09 +1000182 msg_perr("%s: Time out on waiting ROM free.\n", __func__);
Shiyu Sun13a2ef62020-03-19 14:37:57 +1100183 return -MAX_SPI_WAIT_RETRIES;
184 }
185
186 return (val & SWSPICTL_ACCESS_TRIGGER) ? SPI_GENERIC_ERROR : ret;
187}
188
189static 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
195static 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
206static 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
218static 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
230static 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
235static 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
245static 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
Edward O'Callaghane4ddc362020-04-12 17:27:53 +1000254static int lspcon_i2c_spi_send_command(const struct flashctx *flash,
Shiyu Sun13a2ef62020-03-19 14:37:57 +1100255 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 Sun7f87f9f2020-04-30 16:51:09 +1000261 msg_perr("%s: Invalid read/write count for send command.\n",
262 __func__);
Shiyu Sun13a2ef62020-03-19 14:37:57 +1100263 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
297static 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
310static 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
321static 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
330static int lspcon_i2c_spi_map_page(int fd, unsigned int offset)
331{
332 int ret = 0;
Angel Ponscbe8a392021-04-17 15:32:12 +0200333 /* Page number byte, need to / LSPCON_PAGE_SIZE. */
Shiyu Sun13a2ef62020-03-19 14:37:57 +1100334 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
340static 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 Ponscbe8a392021-04-17 15:32:12 +0200352 for (i = 0; i < len; i += LSPCON_PAGE_SIZE) {
Shiyu Sun13a2ef62020-03-19 14:37:57 +1100353 ret |= lspcon_i2c_spi_map_page(fd, start + i);
Angel Ponscbe8a392021-04-17 15:32:12 +0200354 ret |= lspcon_i2c_spi_read_data(fd, PAGE_ADDRESS, buf + i, min(len - i, LSPCON_PAGE_SIZE));
Shiyu Sun13a2ef62020-03-19 14:37:57 +1100355 }
356
357 return ret;
358}
359
360static 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 Ponscbe8a392021-04-17 15:32:12 +0200366 uint8_t write_buffer[LSPCON_PAGE_SIZE + 1] = { 0 };
367 if (len > LSPCON_PAGE_SIZE)
Shiyu Sun13a2ef62020-03-19 14:37:57 +1100368 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
376static 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;
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 Ponscbe8a392021-04-17 15:32:12 +0200392 for (unsigned int i = 0; i < len; i += LSPCON_PAGE_SIZE) {
Shiyu Sun13a2ef62020-03-19 14:37:57 +1100393 ret |= lspcon_i2c_spi_map_page(fd, start + i);
Angel Ponscbe8a392021-04-17 15:32:12 +0200394 ret |= lspcon_i2c_spi_write_page(fd, buf + i, min(len - i, LSPCON_PAGE_SIZE));
Shiyu Sun13a2ef62020-03-19 14:37:57 +1100395 }
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
403static int lspcon_i2c_spi_write_aai(struct flashctx *flash, const uint8_t *buf,
404 unsigned int start, unsigned int len)
405{
Shiyu Sun7f87f9f2020-04-30 16:51:09 +1000406 msg_perr("%s: AAI write function is not supported.\n",
407 __func__);
Shiyu Sun13a2ef62020-03-19 14:37:57 +1100408 return SPI_GENERIC_ERROR;
409}
410
Edward O'Callaghana25c13c2020-04-17 14:10:55 +1000411static struct spi_master spi_master_i2c_lspcon = {
Shiyu Sun13a2ef62020-03-19 14:37:57 +1100412 .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. */
422static 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 Sun13a2ef62020-03-19 14:37:57 +1100436int lspcon_i2c_spi_init(void)
437{
Angel Ponsdb232952021-05-02 18:56:45 +0200438 int fd = i2c_open_from_programmer_params(REGISTER_ADDRESS, 0);
Shiyu Sun13a2ef62020-03-19 14:37:57 +1100439 if (fd < 0)
440 return fd;
441
Peter Marheine0969e432021-03-31 11:28:11 +1100442 int ret = lspcon_i2c_spi_reset_mpu_stop(fd);
Edward O'Callaghana25c13c2020-04-17 14:10:55 +1000443 if (ret) {
444 msg_perr("%s: call to reset_mpu_stop failed.\n", __func__);
Angel Ponsc7dd1702021-05-02 19:00:37 +0200445 i2c_close(fd);
Shiyu Sun13a2ef62020-03-19 14:37:57 +1100446 return ret;
Edward O'Callaghana25c13c2020-04-17 14:10:55 +1000447 }
Shiyu Sun13a2ef62020-03-19 14:37:57 +1100448
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 Ponsc7dd1702021-05-02 19:00:37 +0200452 i2c_close(fd);
Shiyu Sun13a2ef62020-03-19 14:37:57 +1100453 return SPI_GENERIC_ERROR;
454 }
455
456 data->fd = fd;
Edward O'Callaghana25c13c2020-04-17 14:10:55 +1000457 spi_master_i2c_lspcon.data = data;
458
Shiyu Sun13a2ef62020-03-19 14:37:57 +1100459 ret |= register_shutdown(lspcon_i2c_spi_shutdown, data);
Edward O'Callaghana25c13c2020-04-17 14:10:55 +1000460 ret |= register_spi_master(&spi_master_i2c_lspcon);
Shiyu Sun13a2ef62020-03-19 14:37:57 +1100461
462 return ret;
463}