Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 4 | * Copyright (C) 2007, 2008, 2009, 2010, 2011 Carl-Daniel Hailfinger |
Stefan Reinauer | a9424d5 | 2008-06-27 16:28:34 +0000 | [diff] [blame] | 5 | * Copyright (C) 2008 coresystems GmbH |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; version 2 of the License. |
| 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. |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * Contains the generic SPI framework |
| 19 | */ |
| 20 | |
Patrick Georgi | 97bc95c | 2011-03-08 07:17:44 +0000 | [diff] [blame] | 21 | #include <strings.h> |
Carl-Daniel Hailfinger | ec489e4 | 2010-09-15 00:13:02 +0000 | [diff] [blame] | 22 | #include <string.h> |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 23 | #include "flash.h" |
Carl-Daniel Hailfinger | 0845464 | 2009-06-15 14:14:48 +0000 | [diff] [blame] | 24 | #include "flashchips.h" |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 25 | #include "chipdrivers.h" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 26 | #include "programmer.h" |
Nico Huber | d518563 | 2024-01-05 18:44:41 +0100 | [diff] [blame] | 27 | #include "spi_command.h" |
Carl-Daniel Hailfinger | d6cbf76 | 2008-05-13 14:58:23 +0000 | [diff] [blame] | 28 | #include "spi.h" |
Carl-Daniel Hailfinger | 7053926 | 2007-10-15 21:45:29 +0000 | [diff] [blame] | 29 | |
Edward O'Callaghan | 5eca427 | 2020-04-12 17:27:53 +1000 | [diff] [blame] | 30 | int spi_send_command(const struct flashctx *flash, unsigned int writecnt, |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 31 | unsigned int readcnt, const unsigned char *writearr, |
| 32 | unsigned char *readarr) |
Carl-Daniel Hailfinger | 3d94a0e | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 33 | { |
Nico Huber | 9a11cbf | 2023-01-13 01:19:07 +0100 | [diff] [blame] | 34 | return flash->mst.spi->command(flash, writecnt, readcnt, writearr, |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 35 | readarr); |
Carl-Daniel Hailfinger | 3d94a0e | 2007-10-16 21:09:06 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Edward O'Callaghan | 5eca427 | 2020-04-12 17:27:53 +1000 | [diff] [blame] | 38 | int spi_send_multicommand(const struct flashctx *flash, struct spi_command *cmds) |
Carl-Daniel Hailfinger | d047829 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 39 | { |
Nico Huber | 9a11cbf | 2023-01-13 01:19:07 +0100 | [diff] [blame] | 40 | return flash->mst.spi->multicommand(flash, cmds); |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Edward O'Callaghan | 5eca427 | 2020-04-12 17:27:53 +1000 | [diff] [blame] | 43 | int default_spi_send_command(const struct flashctx *flash, unsigned int writecnt, |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 44 | unsigned int readcnt, |
| 45 | const unsigned char *writearr, |
| 46 | unsigned char *readarr) |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 47 | { |
| 48 | struct spi_command cmd[] = { |
| 49 | { |
Nico Huber | d518563 | 2024-01-05 18:44:41 +0100 | [diff] [blame] | 50 | .io_mode = SINGLE_IO_1_1_1, |
| 51 | .opcode_len = 1, |
| 52 | .address_len = writecnt - 1, |
| 53 | .read_len = readcnt, |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 54 | .writearr = writearr, |
| 55 | .readarr = readarr, |
Nico Huber | d518563 | 2024-01-05 18:44:41 +0100 | [diff] [blame] | 56 | }, |
| 57 | NULL_SPI_CMD |
| 58 | }; |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 59 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 60 | return spi_send_multicommand(flash, cmd); |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Edward O'Callaghan | 5eca427 | 2020-04-12 17:27:53 +1000 | [diff] [blame] | 63 | int default_spi_send_multicommand(const struct flashctx *flash, |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 64 | struct spi_command *cmds) |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 65 | { |
| 66 | int result = 0; |
Nico Huber | d518563 | 2024-01-05 18:44:41 +0100 | [diff] [blame] | 67 | for (; !spi_is_empty(cmds) && !result; cmds++) { |
| 68 | if (cmds->io_mode != SINGLE_IO_1_1_1) |
| 69 | return SPI_FLASHPROG_BUG; |
| 70 | result = spi_send_command(flash, |
| 71 | spi_write_len(cmds), spi_read_len(cmds), |
| 72 | cmds->writearr, cmds->readarr); |
Carl-Daniel Hailfinger | 02487aa | 2009-07-22 15:36:50 +0000 | [diff] [blame] | 73 | } |
| 74 | return result; |
Carl-Daniel Hailfinger | d047829 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 77 | int default_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, |
| 78 | unsigned int len) |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame] | 79 | { |
Nico Huber | 9a11cbf | 2023-01-13 01:19:07 +0100 | [diff] [blame] | 80 | unsigned int max_data = flash->mst.spi->max_data_read; |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame] | 81 | if (max_data == MAX_DATA_UNSPECIFIED) { |
Nico Huber | ac90af6 | 2022-12-18 00:22:47 +0000 | [diff] [blame] | 82 | msg_perr("%s called, but SPI read chunk size not defined on this hardware.\n" |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 83 | "Please report a bug at flashprog@flashprog.org\n", __func__); |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame] | 84 | return 1; |
| 85 | } |
Nico Huber | 7679b5c | 2023-04-28 21:48:53 +0000 | [diff] [blame] | 86 | return flashprog_read_chunked(flash, buf, start, len, max_data, spi_nbyte_read); |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 89 | int default_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len) |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame] | 90 | { |
Nico Huber | 9a11cbf | 2023-01-13 01:19:07 +0100 | [diff] [blame] | 91 | unsigned int max_data = flash->mst.spi->max_data_write; |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame] | 92 | if (max_data == MAX_DATA_UNSPECIFIED) { |
Nico Huber | ac90af6 | 2022-12-18 00:22:47 +0000 | [diff] [blame] | 93 | msg_perr("%s called, but SPI write chunk size not defined on this hardware.\n" |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 94 | "Please report a bug at flashprog@flashprog.org\n", __func__); |
Michael Karcher | 6279751 | 2011-05-11 17:07:02 +0000 | [diff] [blame] | 95 | return 1; |
| 96 | } |
| 97 | return spi_write_chunked(flash, buf, start, len, max_data); |
| 98 | } |
| 99 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 100 | int spi_chip_read(struct flashctx *flash, uint8_t *buf, unsigned int start, |
| 101 | unsigned int len) |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 102 | { |
Nico Huber | d8b2e80 | 2019-06-18 23:39:56 +0200 | [diff] [blame] | 103 | int ret; |
| 104 | size_t to_read; |
| 105 | for (; len; len -= to_read, buf += to_read, start += to_read) { |
| 106 | /* Do not cross 16MiB boundaries in a single transfer. |
| 107 | This helps with |
| 108 | o multi-die 4-byte-addressing chips, |
Nico Huber | cbf9c11 | 2024-03-25 19:24:17 +0100 | [diff] [blame] | 109 | o 4-byte-addressing chips that use an extended address reg, |
Nico Huber | d8b2e80 | 2019-06-18 23:39:56 +0200 | [diff] [blame] | 110 | o dediprog that has a protocol limit of 32MiB-512B. */ |
| 111 | to_read = min(ALIGN_DOWN(start + 16*MiB, 16*MiB) - start, len); |
Nico Huber | 9a11cbf | 2023-01-13 01:19:07 +0100 | [diff] [blame] | 112 | ret = flash->mst.spi->read(flash, buf, start, to_read); |
Nico Huber | d8b2e80 | 2019-06-18 23:39:56 +0200 | [diff] [blame] | 113 | if (ret) |
| 114 | return ret; |
| 115 | } |
| 116 | return 0; |
Ronald Hoogenboom | 7ff530b | 2008-01-19 00:04:46 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Carl-Daniel Hailfinger | 96930c3 | 2009-05-09 02:30:21 +0000 | [diff] [blame] | 119 | /* |
Carl-Daniel Hailfinger | 96930c3 | 2009-05-09 02:30:21 +0000 | [diff] [blame] | 120 | * Program chip using page (256 bytes) programming. |
| 121 | * Some SPI masters can't do this, they use single byte programming instead. |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 122 | * The redirect to single byte programming is achieved by setting |
| 123 | * .write_256 = spi_chip_write_1 |
Carl-Daniel Hailfinger | 96930c3 | 2009-05-09 02:30:21 +0000 | [diff] [blame] | 124 | */ |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 125 | /* real chunksize is up to 256, logical chunksize is 256 */ |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 126 | int spi_chip_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len) |
Carl-Daniel Hailfinger | bfe5b4a | 2008-05-13 23:03:12 +0000 | [diff] [blame] | 127 | { |
Nico Huber | cbf9c11 | 2024-03-25 19:24:17 +0100 | [diff] [blame] | 128 | int ret; |
| 129 | size_t to_write; |
| 130 | for (; len; len -= to_write, buf += to_write, start += to_write) { |
| 131 | /* Do not cross 16MiB boundaries in a single transfer. |
| 132 | This helps with 4-byte-addressing chips using an |
| 133 | extended-address register that has to match the |
| 134 | current 16MiB area. */ |
| 135 | to_write = min(ALIGN_DOWN(start + 16*MiB, 16*MiB) - start, len); |
| 136 | ret = flash->mst.spi->write_256(flash, buf, start, to_write); |
| 137 | if (ret) |
| 138 | return ret; |
| 139 | } |
| 140 | return 0; |
Carl-Daniel Hailfinger | 9a795d8 | 2010-07-14 16:19:05 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 143 | int spi_aai_write(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len) |
Nico Huber | 7bca126 | 2012-06-15 22:28:12 +0000 | [diff] [blame] | 144 | { |
Nico Huber | 9a11cbf | 2023-01-13 01:19:07 +0100 | [diff] [blame] | 145 | if (flash->mst.spi->write_aai) |
| 146 | return flash->mst.spi->write_aai(flash, buf, start, len); |
Edward O'Callaghan | 0b587f9 | 2022-09-09 23:01:05 +1000 | [diff] [blame] | 147 | return default_spi_write_aai(flash, buf, start, len); |
Nico Huber | 7bca126 | 2012-06-15 22:28:12 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Nikolai Artemiev | e7a41e3 | 2022-11-28 17:40:56 +1100 | [diff] [blame] | 150 | bool default_spi_probe_opcode(const struct flashctx *flash, uint8_t opcode) |
Aarya Chaumal | 0cea753 | 2022-07-04 18:21:50 +0530 | [diff] [blame] | 151 | { |
| 152 | return true; |
| 153 | } |
| 154 | |
Nico Huber | 89569d6 | 2023-01-12 23:31:40 +0100 | [diff] [blame] | 155 | int register_spi_master(const struct spi_master *mst, size_t max_rom_decode, void *data) |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 156 | { |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 157 | struct registered_master rmst; |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 158 | |
Anastasia Klimchuk | 7783f2f | 2021-07-05 09:18:06 +1000 | [diff] [blame] | 159 | if (mst->shutdown) { |
| 160 | if (register_shutdown(mst->shutdown, data)) { |
| 161 | mst->shutdown(data); /* cleanup */ |
| 162 | return 1; |
| 163 | } |
| 164 | } |
| 165 | |
Edward O'Callaghan | 0b587f9 | 2022-09-09 23:01:05 +1000 | [diff] [blame] | 166 | if (!mst->write_256 || !mst->read || !mst->command || |
Aarya Chaumal | 0cea753 | 2022-07-04 18:21:50 +0530 | [diff] [blame] | 167 | !mst->multicommand || !mst->probe_opcode || |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 168 | ((mst->command == default_spi_send_command) && |
| 169 | (mst->multicommand == default_spi_send_multicommand))) { |
Nico Huber | ac90af6 | 2022-12-18 00:22:47 +0000 | [diff] [blame] | 170 | msg_perr("%s called with incomplete master definition.\n" |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 171 | "Please report a bug at flashprog@flashprog.org\n", |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 172 | __func__); |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 173 | return ERROR_FLASHPROG_BUG; |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Nico Huber | 89569d6 | 2023-01-12 23:31:40 +0100 | [diff] [blame] | 176 | if (max_rom_decode) |
| 177 | rmst.max_rom_decode = max_rom_decode; |
| 178 | else |
| 179 | rmst.max_rom_decode = MAX_ROM_DECODE_UNLIMITED; |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 180 | rmst.buses_supported = BUS_SPI; |
| 181 | rmst.spi = *mst; |
Nico Huber | 5e08e3e | 2021-05-11 17:38:14 +0200 | [diff] [blame] | 182 | if (data) |
| 183 | rmst.spi.data = data; |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 184 | return register_master(&rmst); |
Stefan Tauner | 93f7023 | 2011-07-26 14:33:46 +0000 | [diff] [blame] | 185 | } |
Thomas Heijligen | e2ff4e9 | 2022-09-19 23:31:08 +0200 | [diff] [blame] | 186 | |
Thomas Heijligen | b0be320 | 2022-09-20 00:07:23 +0200 | [diff] [blame] | 187 | /* |
| 188 | * The following array has erasefn and opcode list pair. The opcode list pair is |
| 189 | * 0 termintated and must have size one more than the maximum number of opcodes |
| 190 | * used by any erasefn. Also the opcodes must be in increasing order. |
| 191 | */ |
Thomas Heijligen | e2ff4e9 | 2022-09-19 23:31:08 +0200 | [diff] [blame] | 192 | static const struct { |
| 193 | erasefunc_t *func; |
Thomas Heijligen | b0be320 | 2022-09-20 00:07:23 +0200 | [diff] [blame] | 194 | uint8_t opcode[3]; |
Nico Huber | 1338936 | 2024-03-05 18:35:30 +0100 | [diff] [blame] | 195 | bool native_4ba; |
Thomas Heijligen | e2ff4e9 | 2022-09-19 23:31:08 +0200 | [diff] [blame] | 196 | } function_opcode_list[] = { |
Nico Huber | 1338936 | 2024-03-05 18:35:30 +0100 | [diff] [blame] | 197 | {spi_block_erase_20, {0x20}, false}, |
| 198 | {spi_block_erase_21, {0x21}, true}, |
| 199 | {spi_block_erase_50, {0x50}, false}, |
| 200 | {spi_block_erase_52, {0x52}, false}, |
| 201 | {spi_block_erase_53, {0x53}, true}, |
| 202 | {spi_block_erase_5c, {0x5c}, true}, |
| 203 | {spi_block_erase_60, {0x60}, false}, |
| 204 | {spi_block_erase_62, {0x62}, false}, |
| 205 | {spi_block_erase_81, {0x81}, false}, |
| 206 | {spi_block_erase_c4, {0xc4}, false}, |
| 207 | {spi_block_erase_c7, {0xc7}, false}, |
| 208 | {spi_block_erase_d7, {0xd7}, false}, |
| 209 | {spi_block_erase_d8, {0xd8}, false}, |
| 210 | {spi_block_erase_db, {0xdb}, false}, |
| 211 | {spi_block_erase_dc, {0xdc}, true}, |
Thomas Heijligen | b0be320 | 2022-09-20 00:07:23 +0200 | [diff] [blame] | 212 | //AT45CS1282 |
Nico Huber | 1338936 | 2024-03-05 18:35:30 +0100 | [diff] [blame] | 213 | {spi_erase_at45cs_sector, {0x50, 0x7c, 0}, false}, |
Thomas Heijligen | b0be320 | 2022-09-20 00:07:23 +0200 | [diff] [blame] | 214 | //AT45DB** |
Nico Huber | 1338936 | 2024-03-05 18:35:30 +0100 | [diff] [blame] | 215 | {spi_erase_at45db_page, {0x81}, false}, |
| 216 | {spi_erase_at45db_block, {0x50}, false}, |
| 217 | {spi_erase_at45db_sector, {0x7c}, false}, |
| 218 | {spi_erase_at45db_chip, {0xc7}, false}, |
Thomas Heijligen | e2ff4e9 | 2022-09-19 23:31:08 +0200 | [diff] [blame] | 219 | }; |
| 220 | |
Nico Huber | 1338936 | 2024-03-05 18:35:30 +0100 | [diff] [blame] | 221 | const uint8_t *spi_get_opcode_from_erasefn(erasefunc_t *func, bool *native_4ba) |
Thomas Heijligen | e2ff4e9 | 2022-09-19 23:31:08 +0200 | [diff] [blame] | 222 | { |
| 223 | size_t i; |
| 224 | for (i = 0; i < ARRAY_SIZE(function_opcode_list); i++) { |
Nico Huber | 1338936 | 2024-03-05 18:35:30 +0100 | [diff] [blame] | 225 | if (function_opcode_list[i].func == func) { |
| 226 | if (native_4ba) |
| 227 | *native_4ba = function_opcode_list[i].native_4ba; |
Thomas Heijligen | e2ff4e9 | 2022-09-19 23:31:08 +0200 | [diff] [blame] | 228 | return function_opcode_list[i].opcode; |
Nico Huber | 1338936 | 2024-03-05 18:35:30 +0100 | [diff] [blame] | 229 | } |
Thomas Heijligen | e2ff4e9 | 2022-09-19 23:31:08 +0200 | [diff] [blame] | 230 | } |
| 231 | msg_cinfo("%s: unknown erase function (0x%p). Please report " |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 232 | "this at flashprog@flashprog.org\n", __func__, func); |
Thomas Heijligen | b0be320 | 2022-09-20 00:07:23 +0200 | [diff] [blame] | 233 | return NULL; |
Thomas Heijligen | e2ff4e9 | 2022-09-19 23:31:08 +0200 | [diff] [blame] | 234 | } |