blob: 15809c656d1c622d72aa5c0428570b4429cf423c [file] [log] [blame]
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +00001/*
2 * This file is part of the flashrom project.
3 *
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +00004 * Copyright (C) 2007, 2008, 2009, 2010, 2011 Carl-Daniel Hailfinger
Stefan Reinauera9424d52008-06-27 16:28:34 +00005 * Copyright (C) 2008 coresystems GmbH
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +00006 *
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 Hailfinger70539262007-10-15 21:45:29 +000015 */
16
17/*
18 * Contains the generic SPI framework
19 */
20
Patrick Georgi97bc95c2011-03-08 07:17:44 +000021#include <strings.h>
Carl-Daniel Hailfingerec489e42010-09-15 00:13:02 +000022#include <string.h>
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000023#include "flash.h"
Carl-Daniel Hailfinger08454642009-06-15 14:14:48 +000024#include "flashchips.h"
Nico Huberf3113ac2026-02-21 12:50:19 +010025#include "chipdrivers/edi.h"
Nico Huberfbc41d22026-02-22 23:04:01 +010026#include "chipdrivers/spi.h"
Nico Huber43125762023-05-01 15:56:16 +020027#include "chipdrivers/probing.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000028#include "programmer.h"
Nico Huberd5185632024-01-05 18:44:41 +010029#include "spi_command.h"
Carl-Daniel Hailfingerd6cbf762008-05-13 14:58:23 +000030#include "spi.h"
Carl-Daniel Hailfinger70539262007-10-15 21:45:29 +000031
Nico Huberdc344092024-12-07 00:22:21 +010032static int spi_send_wrapped_command(
Nico Huber610c1aa2023-02-15 02:56:05 +010033 const struct spi_master *mst, enum io_mode io_mode,
Nico Huberdc344092024-12-07 00:22:21 +010034 unsigned int writecnt, unsigned int readcnt,
35 const unsigned char *writearr, unsigned char *readarr)
36{
37 struct spi_command cmd[] = {
38 {
39 .io_mode = io_mode,
40 .opcode_len = 1,
41 .address_len = writecnt - 1,
42 .read_len = readcnt,
43 .writearr = writearr,
44 .readarr = readarr,
45 },
46 NULL_SPI_CMD
47 };
48
Nico Huber610c1aa2023-02-15 02:56:05 +010049 return mst->multicommand(mst, cmd);
Nico Huberdc344092024-12-07 00:22:21 +010050}
51
Edward O'Callaghan5eca4272020-04-12 17:27:53 +100052int spi_send_command(const struct flashctx *flash, unsigned int writecnt,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000053 unsigned int readcnt, const unsigned char *writearr,
54 unsigned char *readarr)
Carl-Daniel Hailfinger3d94a0e2007-10-16 21:09:06 +000055{
Nico Huber1b1deda2024-04-18 00:35:48 +020056 if (spi_current_io_mode(flash) != SINGLE_IO_1_1_1)
Nico Huber610c1aa2023-02-15 02:56:05 +010057 return spi_send_wrapped_command(flash->mst.spi, spi_current_io_mode(flash),
Nico Huberdc344092024-12-07 00:22:21 +010058 writecnt, readcnt, writearr, readarr);
Nico Huber1b1deda2024-04-18 00:35:48 +020059
Nico Huber610c1aa2023-02-15 02:56:05 +010060 return flash->mst.spi->command(flash->mst.spi, writecnt, readcnt, writearr, readarr);
Carl-Daniel Hailfinger3d94a0e2007-10-16 21:09:06 +000061}
62
Edward O'Callaghan5eca4272020-04-12 17:27:53 +100063int spi_send_multicommand(const struct flashctx *flash, struct spi_command *cmds)
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +000064{
Nico Huber610c1aa2023-02-15 02:56:05 +010065 return flash->mst.spi->multicommand(flash->mst.spi, cmds);
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +000066}
67
Nico Huber610c1aa2023-02-15 02:56:05 +010068int default_spi_send_command(const struct spi_master *mst,
69 unsigned int writecnt, unsigned int readcnt,
70 const unsigned char *writearr, unsigned char *readarr)
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +000071{
Nico Huber610c1aa2023-02-15 02:56:05 +010072 return spi_send_wrapped_command(mst, SINGLE_IO_1_1_1, writecnt, readcnt, writearr, readarr);
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +000073}
74
Nico Huber610c1aa2023-02-15 02:56:05 +010075int default_spi_send_multicommand(const struct spi_master *mst, struct spi_command *cmds)
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +000076{
77 int result = 0;
Nico Huberd5185632024-01-05 18:44:41 +010078 for (; !spi_is_empty(cmds) && !result; cmds++) {
79 if (cmds->io_mode != SINGLE_IO_1_1_1)
80 return SPI_FLASHPROG_BUG;
Nico Huber610c1aa2023-02-15 02:56:05 +010081 result = mst->command(mst,
Nico Huberd5185632024-01-05 18:44:41 +010082 spi_write_len(cmds), spi_read_len(cmds),
83 cmds->writearr, cmds->readarr);
Carl-Daniel Hailfinger02487aa2009-07-22 15:36:50 +000084 }
85 return result;
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +000086}
87
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000088int default_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start,
89 unsigned int len)
Michael Karcher62797512011-05-11 17:07:02 +000090{
Nico Huber9a11cbf2023-01-13 01:19:07 +010091 unsigned int max_data = flash->mst.spi->max_data_read;
Michael Karcher62797512011-05-11 17:07:02 +000092 if (max_data == MAX_DATA_UNSPECIFIED) {
Nico Huberac90af62022-12-18 00:22:47 +000093 msg_perr("%s called, but SPI read chunk size not defined on this hardware.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +020094 "Please report a bug at flashprog@flashprog.org\n", __func__);
Michael Karcher62797512011-05-11 17:07:02 +000095 return 1;
96 }
Nico Huber7679b5c2023-04-28 21:48:53 +000097 return flashprog_read_chunked(flash, buf, start, len, max_data, spi_nbyte_read);
Michael Karcher62797512011-05-11 17:07:02 +000098}
99
Mark Marshallf20b7be2014-05-09 21:16:21 +0000100int default_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
Michael Karcher62797512011-05-11 17:07:02 +0000101{
Nico Huber9a11cbf2023-01-13 01:19:07 +0100102 unsigned int max_data = flash->mst.spi->max_data_write;
Michael Karcher62797512011-05-11 17:07:02 +0000103 if (max_data == MAX_DATA_UNSPECIFIED) {
Nico Huberac90af62022-12-18 00:22:47 +0000104 msg_perr("%s called, but SPI write chunk size not defined on this hardware.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200105 "Please report a bug at flashprog@flashprog.org\n", __func__);
Michael Karcher62797512011-05-11 17:07:02 +0000106 return 1;
107 }
108 return spi_write_chunked(flash, buf, start, len, max_data);
109}
110
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000111int spi_chip_read(struct flashctx *flash, uint8_t *buf, unsigned int start,
112 unsigned int len)
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000113{
Nico Huberd8b2e802019-06-18 23:39:56 +0200114 int ret;
115 size_t to_read;
116 for (; len; len -= to_read, buf += to_read, start += to_read) {
117 /* Do not cross 16MiB boundaries in a single transfer.
118 This helps with
119 o multi-die 4-byte-addressing chips,
Nico Hubercbf9c112024-03-25 19:24:17 +0100120 o 4-byte-addressing chips that use an extended address reg,
Nico Huberd8b2e802019-06-18 23:39:56 +0200121 o dediprog that has a protocol limit of 32MiB-512B. */
122 to_read = min(ALIGN_DOWN(start + 16*MiB, 16*MiB) - start, len);
Nico Huber9a11cbf2023-01-13 01:19:07 +0100123 ret = flash->mst.spi->read(flash, buf, start, to_read);
Nico Huberd8b2e802019-06-18 23:39:56 +0200124 if (ret)
125 return ret;
126 }
127 return 0;
Ronald Hoogenboom7ff530b2008-01-19 00:04:46 +0000128}
129
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000130/*
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000131 * Program chip using page (256 bytes) programming.
132 * Some SPI masters can't do this, they use single byte programming instead.
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000133 * The redirect to single byte programming is achieved by setting
134 * .write_256 = spi_chip_write_1
Carl-Daniel Hailfinger96930c32009-05-09 02:30:21 +0000135 */
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000136/* real chunksize is up to 256, logical chunksize is 256 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000137int spi_chip_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
Carl-Daniel Hailfingerbfe5b4a2008-05-13 23:03:12 +0000138{
Nico Hubercbf9c112024-03-25 19:24:17 +0100139 int ret;
140 size_t to_write;
141 for (; len; len -= to_write, buf += to_write, start += to_write) {
142 /* Do not cross 16MiB boundaries in a single transfer.
143 This helps with 4-byte-addressing chips using an
144 extended-address register that has to match the
145 current 16MiB area. */
146 to_write = min(ALIGN_DOWN(start + 16*MiB, 16*MiB) - start, len);
147 ret = flash->mst.spi->write_256(flash, buf, start, to_write);
148 if (ret)
149 return ret;
150 }
151 return 0;
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000152}
153
Mark Marshallf20b7be2014-05-09 21:16:21 +0000154int spi_aai_write(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
Nico Huber7bca1262012-06-15 22:28:12 +0000155{
Nico Huber9a11cbf2023-01-13 01:19:07 +0100156 if (flash->mst.spi->write_aai)
157 return flash->mst.spi->write_aai(flash, buf, start, len);
Edward O'Callaghan0b587f92022-09-09 23:01:05 +1000158 return default_spi_write_aai(flash, buf, start, len);
Nico Huber7bca1262012-06-15 22:28:12 +0000159}
160
Nikolai Artemieve7a41e32022-11-28 17:40:56 +1100161bool default_spi_probe_opcode(const struct flashctx *flash, uint8_t opcode)
Aarya Chaumal0cea7532022-07-04 18:21:50 +0530162{
163 return true;
164}
165
Nico Huber43125762023-05-01 15:56:16 +0200166static const struct bus_probe spi_probes[] = {
Nico Huberac138732026-02-28 17:42:27 +0100167 /* prio. type function function argument */
168 { 0, ID_SPI_RDID, probe_spi_rdid, NULL },
169 { 0, ID_SPI_AT25F, probe_spi_at25f, NULL },
170 { 0, ID_SPI_REMS, probe_spi_rems, NULL },
171 { 0, ID_SPI_RES3, probe_spi_res, NULL },
172 { 0, ID_SPI_RES2, probe_spi_res, NULL },
173 { 0, ID_SPI_RES1, probe_spi_res, NULL },
Nico Huber2e0a0032026-03-07 22:32:27 +0100174 { 0, ID_SPI_SFDP, probe_spi_sfdp, NULL },
Nico Huberb9e47cc2026-02-21 13:25:17 +0100175 { 1, ID_SPI_ST95, probe_spi_st95, (void *)(uintptr_t)3 },
176 { 1, ID_SPI_ST95, probe_spi_st95, (void *)(uintptr_t)2 },
Nico Huberf3113ac2026-02-21 12:50:19 +0100177 { 2, ID_EDI, probe_edi, NULL },
Nico Huber43125762023-05-01 15:56:16 +0200178};
179
180static bool spi_probe_match(const struct flashchip *chip, const struct id_info_ext *found)
181{
182 if (memcmp(&chip->id, &found->id, sizeof(found->id)) == 0)
183 return true;
184
185 /* Test if this is a pure vendor match. */
186 if (found->id.manufacture == chip->id.manufacture && GENERIC_DEVICE_ID == chip->id.model)
187 return true;
188
189 /* Test if there is any vendor ID. */
190 if (GENERIC_MANUF_ID == chip->id.manufacture &&
191 found->id.manufacture != 0xff && found->id.manufacture != 0x00)
192 return true;
193
194 return false;
195}
196
Nico Huber89569d62023-01-12 23:31:40 +0100197int register_spi_master(const struct spi_master *mst, size_t max_rom_decode, void *data)
Michael Karcherb9dbe482011-05-11 17:07:07 +0000198{
Nico Huberaf9d7382023-05-01 13:33:26 +0200199 struct registered_master rmst = { 0 };
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000200
Anastasia Klimchuk7783f2f2021-07-05 09:18:06 +1000201 if (mst->shutdown) {
202 if (register_shutdown(mst->shutdown, data)) {
203 mst->shutdown(data); /* cleanup */
204 return 1;
205 }
206 }
207
Edward O'Callaghan0b587f92022-09-09 23:01:05 +1000208 if (!mst->write_256 || !mst->read || !mst->command ||
Aarya Chaumal0cea7532022-07-04 18:21:50 +0530209 !mst->multicommand || !mst->probe_opcode ||
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000210 ((mst->command == default_spi_send_command) &&
211 (mst->multicommand == default_spi_send_multicommand))) {
Nico Huberac90af62022-12-18 00:22:47 +0000212 msg_perr("%s called with incomplete master definition.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200213 "Please report a bug at flashprog@flashprog.org\n",
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000214 __func__);
Nico Huberc3b02dc2023-08-12 01:13:45 +0200215 return ERROR_FLASHPROG_BUG;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000216 }
217
Nico Huber4760b6e2024-01-06 23:45:28 +0100218 if ((mst->features & (SPI_MASTER_DUAL | SPI_MASTER_QUAD | SPI_MASTER_DTR_IN)) &&
219 mst->read == default_spi_read && mst->multicommand == default_spi_send_multicommand) {
220 msg_perr("%s called with incomplete master definition.\n"
221 "Dual/quad I/O and DTR require multicommand or custom read function.\n"
222 "Please report a bug at flashprog@flashprog.org\n",
223 __func__);
224 return ERROR_FLASHPROG_BUG;
225 }
226
Nico Huber89569d62023-01-12 23:31:40 +0100227 if (max_rom_decode)
228 rmst.max_rom_decode = max_rom_decode;
229 else
230 rmst.max_rom_decode = MAX_ROM_DECODE_UNLIMITED;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000231 rmst.buses_supported = BUS_SPI;
Nico Huber43125762023-05-01 15:56:16 +0200232 rmst.probing.probe_count = ARRAY_SIZE(spi_probes);
233 rmst.probing.probes = spi_probes;
234 rmst.probing.match = spi_probe_match;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000235 rmst.spi = *mst;
Nico Huber5e08e3e2021-05-11 17:38:14 +0200236 if (data)
237 rmst.spi.data = data;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000238 return register_master(&rmst);
Stefan Tauner93f70232011-07-26 14:33:46 +0000239}
Thomas Heijligene2ff4e92022-09-19 23:31:08 +0200240
Thomas Heijligenb0be3202022-09-20 00:07:23 +0200241/*
242 * The following array has erasefn and opcode list pair. The opcode list pair is
243 * 0 termintated and must have size one more than the maximum number of opcodes
244 * used by any erasefn. Also the opcodes must be in increasing order.
245 */
Thomas Heijligene2ff4e92022-09-19 23:31:08 +0200246static const struct {
247 erasefunc_t *func;
Thomas Heijligenb0be3202022-09-20 00:07:23 +0200248 uint8_t opcode[3];
Nico Huber13389362024-03-05 18:35:30 +0100249 bool native_4ba;
Thomas Heijligene2ff4e92022-09-19 23:31:08 +0200250} function_opcode_list[] = {
Nico Huber13389362024-03-05 18:35:30 +0100251 {spi_block_erase_20, {0x20}, false},
252 {spi_block_erase_21, {0x21}, true},
253 {spi_block_erase_50, {0x50}, false},
254 {spi_block_erase_52, {0x52}, false},
255 {spi_block_erase_53, {0x53}, true},
256 {spi_block_erase_5c, {0x5c}, true},
257 {spi_block_erase_60, {0x60}, false},
258 {spi_block_erase_62, {0x62}, false},
259 {spi_block_erase_81, {0x81}, false},
260 {spi_block_erase_c4, {0xc4}, false},
261 {spi_block_erase_c7, {0xc7}, false},
262 {spi_block_erase_d7, {0xd7}, false},
263 {spi_block_erase_d8, {0xd8}, false},
264 {spi_block_erase_db, {0xdb}, false},
265 {spi_block_erase_dc, {0xdc}, true},
Thomas Heijligenb0be3202022-09-20 00:07:23 +0200266 //AT45CS1282
Nico Huber13389362024-03-05 18:35:30 +0100267 {spi_erase_at45cs_sector, {0x50, 0x7c, 0}, false},
Thomas Heijligenb0be3202022-09-20 00:07:23 +0200268 //AT45DB**
Nico Huber13389362024-03-05 18:35:30 +0100269 {spi_erase_at45db_page, {0x81}, false},
270 {spi_erase_at45db_block, {0x50}, false},
271 {spi_erase_at45db_sector, {0x7c}, false},
272 {spi_erase_at45db_chip, {0xc7}, false},
Thomas Heijligene2ff4e92022-09-19 23:31:08 +0200273};
274
Nico Huber13389362024-03-05 18:35:30 +0100275const uint8_t *spi_get_opcode_from_erasefn(erasefunc_t *func, bool *native_4ba)
Thomas Heijligene2ff4e92022-09-19 23:31:08 +0200276{
277 size_t i;
278 for (i = 0; i < ARRAY_SIZE(function_opcode_list); i++) {
Nico Huber13389362024-03-05 18:35:30 +0100279 if (function_opcode_list[i].func == func) {
280 if (native_4ba)
281 *native_4ba = function_opcode_list[i].native_4ba;
Thomas Heijligene2ff4e92022-09-19 23:31:08 +0200282 return function_opcode_list[i].opcode;
Nico Huber13389362024-03-05 18:35:30 +0100283 }
Thomas Heijligene2ff4e92022-09-19 23:31:08 +0200284 }
285 msg_cinfo("%s: unknown erase function (0x%p). Please report "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200286 "this at flashprog@flashprog.org\n", __func__, func);
Thomas Heijligenb0be3202022-09-20 00:07:23 +0200287 return NULL;
Thomas Heijligene2ff4e92022-09-19 23:31:08 +0200288}