blob: b681976ace963cb84e1ee0096f71d6f99b0c06a5 [file] [log] [blame]
snelson8913d082010-02-26 05:48:29 +00001/*
2 * This file is part of the flashrom project.
3 *
hailfinger39d159a2010-05-21 23:09:42 +00004 * Copyright (C) 2007, 2008, 2009, 2010 Carl-Daniel Hailfinger
snelson8913d082010-02-26 05:48:29 +00005 * Copyright (C) 2008 coresystems GmbH
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.
snelson8913d082010-02-26 05:48:29 +000015 */
16
17/*
18 * Contains the common SPI chip driver functions
19 */
20
Nico Huber4c8a9562017-10-15 11:20:58 +020021#include <stddef.h>
snelson8913d082010-02-26 05:48:29 +000022#include <string.h>
Edward O'Callaghan031831d2019-06-19 16:27:43 +100023#include <stdbool.h>
snelson8913d082010-02-26 05:48:29 +000024#include "flash.h"
25#include "flashchips.h"
26#include "chipdrivers.h"
hailfinger428f6852010-07-27 22:41:39 +000027#include "programmer.h"
snelson8913d082010-02-26 05:48:29 +000028#include "spi.h"
Boris Baykov1a2f5322016-06-11 18:29:00 +020029#include "spi4ba.h"
snelson8913d082010-02-26 05:48:29 +000030
David Hendricks57b75242015-11-20 15:54:07 -080031enum id_type {
32 RDID,
33 RDID4,
34 REMS,
35// RES1, /* TODO */
36 RES2,
Nikolai Artemiev4702c7c2020-08-31 12:49:50 +100037 RES3,
David Hendricks57b75242015-11-20 15:54:07 -080038 NUM_ID_TYPES,
39};
40
41static struct {
42 int is_cached;
43 unsigned char bytes[4]; /* enough to hold largest ID type */
44} id_cache[NUM_ID_TYPES];
45
46void clear_spi_id_cache(void)
47{
48 memset(id_cache, 0, sizeof(id_cache));
49 return;
50}
51
Souvik Ghoshd75cd672016-06-17 14:21:39 -070052static int spi_rdid(struct flashctx *flash, unsigned char *readarr, int bytes)
snelson8913d082010-02-26 05:48:29 +000053{
krause2eb76212011-01-17 07:50:42 +000054 static const unsigned char cmd[JEDEC_RDID_OUTSIZE] = { JEDEC_RDID };
snelson8913d082010-02-26 05:48:29 +000055 int ret;
56 int i;
57
Souvik Ghoshd75cd672016-06-17 14:21:39 -070058 ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr);
snelson8913d082010-02-26 05:48:29 +000059 if (ret)
60 return ret;
snelsonfc007bb2010-03-24 23:14:32 +000061 msg_cspew("RDID returned");
snelson8913d082010-02-26 05:48:29 +000062 for (i = 0; i < bytes; i++)
snelsonfc007bb2010-03-24 23:14:32 +000063 msg_cspew(" 0x%02x", readarr[i]);
64 msg_cspew(". ");
snelson8913d082010-02-26 05:48:29 +000065 return 0;
66}
67
Souvik Ghoshd75cd672016-06-17 14:21:39 -070068static int spi_rems(struct flashctx *flash, unsigned char *readarr)
snelson8913d082010-02-26 05:48:29 +000069{
Edward O'Callaghandfb71542020-05-14 18:41:42 +100070 static const unsigned char cmd[JEDEC_REMS_OUTSIZE] = { JEDEC_REMS, };
snelson8913d082010-02-26 05:48:29 +000071 int ret;
72
Souvik Ghoshd75cd672016-06-17 14:21:39 -070073 ret = spi_send_command(flash, sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
snelson8913d082010-02-26 05:48:29 +000074 if (ret)
75 return ret;
stefanct371e7e82011-07-07 19:56:58 +000076 msg_cspew("REMS returned 0x%02x 0x%02x. ", readarr[0], readarr[1]);
snelson8913d082010-02-26 05:48:29 +000077 return 0;
78}
79
Souvik Ghoshd75cd672016-06-17 14:21:39 -070080static int spi_res(struct flashctx *flash, unsigned char *readarr, int bytes)
snelson8913d082010-02-26 05:48:29 +000081{
Edward O'Callaghandfb71542020-05-14 18:41:42 +100082 static const unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, };
snelson8913d082010-02-26 05:48:29 +000083 int ret;
hailfingercb0564e2010-06-20 10:39:33 +000084 int i;
snelson8913d082010-02-26 05:48:29 +000085
Souvik Ghoshd75cd672016-06-17 14:21:39 -070086 ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr);
snelson8913d082010-02-26 05:48:29 +000087 if (ret)
88 return ret;
hailfingercb0564e2010-06-20 10:39:33 +000089 msg_cspew("RES returned");
90 for (i = 0; i < bytes; i++)
91 msg_cspew(" 0x%02x", readarr[i]);
92 msg_cspew(". ");
snelson8913d082010-02-26 05:48:29 +000093 return 0;
94}
95
Souvik Ghoshd75cd672016-06-17 14:21:39 -070096int spi_write_enable(struct flashctx *flash)
snelson8913d082010-02-26 05:48:29 +000097{
krause2eb76212011-01-17 07:50:42 +000098 static const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN };
snelson8913d082010-02-26 05:48:29 +000099 int result;
100
101 /* Send WREN (Write Enable) */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700102 result = spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
snelson8913d082010-02-26 05:48:29 +0000103
104 if (result)
snelsonfc007bb2010-03-24 23:14:32 +0000105 msg_cerr("%s failed\n", __func__);
snelson8913d082010-02-26 05:48:29 +0000106
107 return result;
108}
109
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700110int spi_write_disable(struct flashctx *flash)
snelson8913d082010-02-26 05:48:29 +0000111{
krause2eb76212011-01-17 07:50:42 +0000112 static const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI };
snelson8913d082010-02-26 05:48:29 +0000113
114 /* Send WRDI (Write Disable) */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700115 return spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
snelson8913d082010-02-26 05:48:29 +0000116}
117
David Hendricks7f7c7112012-10-11 17:15:48 -0700118static void rdid_get_ids(unsigned char *readarr,
119 int bytes, uint32_t *id1, uint32_t *id2)
snelson8913d082010-02-26 05:48:29 +0000120{
snelson8913d082010-02-26 05:48:29 +0000121 if (!oddparity(readarr[0]))
snelsonfc007bb2010-03-24 23:14:32 +0000122 msg_cdbg("RDID byte 0 parity violation. ");
snelson8913d082010-02-26 05:48:29 +0000123
hailfingercb0564e2010-06-20 10:39:33 +0000124 /* Check if this is a continuation vendor ID.
125 * FIXME: Handle continuation device IDs.
126 */
snelson8913d082010-02-26 05:48:29 +0000127 if (readarr[0] == 0x7f) {
128 if (!oddparity(readarr[1]))
snelsonfc007bb2010-03-24 23:14:32 +0000129 msg_cdbg("RDID byte 1 parity violation. ");
David Hendricks7f7c7112012-10-11 17:15:48 -0700130 *id1 = (readarr[0] << 8) | readarr[1];
131 *id2 = readarr[2];
snelson8913d082010-02-26 05:48:29 +0000132 if (bytes > 3) {
David Hendricks7f7c7112012-10-11 17:15:48 -0700133 *id2 <<= 8;
134 *id2 |= readarr[3];
snelson8913d082010-02-26 05:48:29 +0000135 }
136 } else {
David Hendricks7f7c7112012-10-11 17:15:48 -0700137 *id1 = readarr[0];
138 *id2 = (readarr[1] << 8) | readarr[2];
snelson8913d082010-02-26 05:48:29 +0000139 }
David Hendricks7f7c7112012-10-11 17:15:48 -0700140}
snelson8913d082010-02-26 05:48:29 +0000141
Edward O'Callaghan4cfb22f2020-10-15 12:21:04 +1100142static int compare_id(const struct flashctx *flash, uint32_t id1, uint32_t id2)
David Hendricks7f7c7112012-10-11 17:15:48 -0700143{
Edward O'Callaghan4cfb22f2020-10-15 12:21:04 +1100144 const struct flashchip *chip = flash->chip;
snelson8913d082010-02-26 05:48:29 +0000145
Edward O'Callaghan4cfb22f2020-10-15 12:21:04 +1100146 msg_cdbg("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
147 if (id1 == chip->manufacture_id && id2 == chip->model_id)
snelson8913d082010-02-26 05:48:29 +0000148 return 1;
snelson8913d082010-02-26 05:48:29 +0000149
150 /* Test if this is a pure vendor match. */
Edward O'Callaghan4cfb22f2020-10-15 12:21:04 +1100151 if (id1 == chip->manufacture_id && GENERIC_DEVICE_ID == chip->model_id)
snelson8913d082010-02-26 05:48:29 +0000152 return 1;
153
154 /* Test if there is any vendor ID. */
Urja Rannikko544a3a72015-06-22 23:59:15 +0000155 if (GENERIC_MANUF_ID == chip->manufacture_id && id1 != 0xff && id1 != 0x00)
snelson8913d082010-02-26 05:48:29 +0000156 return 1;
157
158 return 0;
159}
160
Edward O'Callaghanc2a12a92020-05-14 17:58:32 +1000161static int probe_spi_rdid_generic(struct flashctx *flash, int bytes)
snelson8913d082010-02-26 05:48:29 +0000162{
David Hendricks57b75242015-11-20 15:54:07 -0800163 uint32_t id1, id2;
Edward O'Callaghanc2a12a92020-05-14 17:58:32 +1000164 enum id_type idty = bytes == 3 ? RDID : RDID4;
David Hendricks7f7c7112012-10-11 17:15:48 -0700165
Edward O'Callaghanc2a12a92020-05-14 17:58:32 +1000166 if (!id_cache[idty].is_cached) {
167 const int ret = spi_rdid(flash, id_cache[idty].bytes, bytes);
168 if (ret == SPI_INVALID_LENGTH)
169 msg_cinfo("%d byte RDID not supported on this SPI controller\n", bytes);
170 if (ret)
David Hendricks7f7c7112012-10-11 17:15:48 -0700171 return 0;
Edward O'Callaghanc2a12a92020-05-14 17:58:32 +1000172 id_cache[idty].is_cached = 1;
David Hendricks7f7c7112012-10-11 17:15:48 -0700173 }
174
Edward O'Callaghanc2a12a92020-05-14 17:58:32 +1000175 rdid_get_ids(id_cache[idty].bytes, bytes, &id1, &id2);
David Hendricks7f7c7112012-10-11 17:15:48 -0700176 return compare_id(flash, id1, id2);
snelson8913d082010-02-26 05:48:29 +0000177}
178
Edward O'Callaghanc2a12a92020-05-14 17:58:32 +1000179int probe_spi_rdid(struct flashctx *flash)
180{
181 return probe_spi_rdid_generic(flash, 3);
182}
183
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700184int probe_spi_rdid4(struct flashctx *flash)
snelson8913d082010-02-26 05:48:29 +0000185{
Edward O'Callaghanc2a12a92020-05-14 17:58:32 +1000186 return probe_spi_rdid_generic(flash, 4);
snelson8913d082010-02-26 05:48:29 +0000187}
188
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700189int probe_spi_rems(struct flashctx *flash)
snelson8913d082010-02-26 05:48:29 +0000190{
David Hendricks57b75242015-11-20 15:54:07 -0800191 uint32_t id1, id2;
snelson8913d082010-02-26 05:48:29 +0000192
David Hendricks57b75242015-11-20 15:54:07 -0800193 if (!id_cache[REMS].is_cached) {
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700194 if (spi_rems(flash, id_cache[REMS].bytes))
David Hendricks7f7c7112012-10-11 17:15:48 -0700195 return 0;
David Hendricks57b75242015-11-20 15:54:07 -0800196 id_cache[REMS].is_cached = 1;
stefanct9e6b98a2011-05-28 02:37:14 +0000197 }
snelson8913d082010-02-26 05:48:29 +0000198
David Hendricks57b75242015-11-20 15:54:07 -0800199 id1 = id_cache[REMS].bytes[0];
200 id2 = id_cache[REMS].bytes[1];
David Hendricks7f7c7112012-10-11 17:15:48 -0700201 return compare_id(flash, id1, id2);
snelson8913d082010-02-26 05:48:29 +0000202}
203
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700204int probe_spi_res1(struct flashctx *flash)
snelson8913d082010-02-26 05:48:29 +0000205{
krause2eb76212011-01-17 07:50:42 +0000206 static const unsigned char allff[] = {0xff, 0xff, 0xff};
207 static const unsigned char all00[] = {0x00, 0x00, 0x00};
snelson8913d082010-02-26 05:48:29 +0000208 unsigned char readarr[3];
209 uint32_t id2;
snelson8913d082010-02-26 05:48:29 +0000210
hailfinger59a83572010-05-28 17:07:57 +0000211 /* We only want one-byte RES if RDID and REMS are unusable. */
212
snelson8913d082010-02-26 05:48:29 +0000213 /* Check if RDID is usable and does not return 0xff 0xff 0xff or
214 * 0x00 0x00 0x00. In that case, RES is pointless.
215 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700216 if (!spi_rdid(flash, readarr, 3) && memcmp(readarr, allff, 3) &&
snelson8913d082010-02-26 05:48:29 +0000217 memcmp(readarr, all00, 3)) {
218 msg_cdbg("Ignoring RES in favour of RDID.\n");
219 return 0;
220 }
221 /* Check if REMS is usable and does not return 0xff 0xff or
222 * 0x00 0x00. In that case, RES is pointless.
223 */
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100224 if (!spi_rems(flash, readarr) &&
225 memcmp(readarr, allff, JEDEC_REMS_INSIZE) &&
snelson8913d082010-02-26 05:48:29 +0000226 memcmp(readarr, all00, JEDEC_REMS_INSIZE)) {
227 msg_cdbg("Ignoring RES in favour of REMS.\n");
228 return 0;
229 }
230
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700231 if (spi_res(flash, readarr, 1)) {
snelson8913d082010-02-26 05:48:29 +0000232 return 0;
stefanct9e6b98a2011-05-28 02:37:14 +0000233 }
snelson8913d082010-02-26 05:48:29 +0000234
snelson8913d082010-02-26 05:48:29 +0000235 id2 = readarr[0];
hailfinger59a83572010-05-28 17:07:57 +0000236
snelsonfc007bb2010-03-24 23:14:32 +0000237 msg_cdbg("%s: id 0x%x\n", __func__, id2);
hailfinger59a83572010-05-28 17:07:57 +0000238
Patrick Georgif3fa2992017-02-02 16:24:44 +0100239 if (id2 != flash->chip->model_id)
snelson8913d082010-02-26 05:48:29 +0000240 return 0;
241
snelson8913d082010-02-26 05:48:29 +0000242 return 1;
243}
244
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700245int probe_spi_res2(struct flashctx *flash)
hailfinger59a83572010-05-28 17:07:57 +0000246{
hailfinger59a83572010-05-28 17:07:57 +0000247 uint32_t id1, id2;
248
David Hendricks57b75242015-11-20 15:54:07 -0800249 if (!id_cache[RES2].is_cached) {
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700250 if (spi_res(flash, id_cache[RES2].bytes, 2))
David Hendricks57b75242015-11-20 15:54:07 -0800251 return 0;
252 id_cache[RES2].is_cached = 1;
stefanct9e6b98a2011-05-28 02:37:14 +0000253 }
hailfinger59a83572010-05-28 17:07:57 +0000254
David Hendricks57b75242015-11-20 15:54:07 -0800255 id1 = id_cache[RES2].bytes[0];
256 id2 = id_cache[RES2].bytes[1];
hailfinger59a83572010-05-28 17:07:57 +0000257 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
258
Patrick Georgif3fa2992017-02-02 16:24:44 +0100259 if (id1 != flash->chip->manufacture_id || id2 != flash->chip->model_id)
hailfinger59a83572010-05-28 17:07:57 +0000260 return 0;
261
hailfinger59a83572010-05-28 17:07:57 +0000262 return 1;
263}
264
Nikolai Artemiev4702c7c2020-08-31 12:49:50 +1000265int probe_spi_res3(struct flashctx *flash)
266{
267 uint32_t id1, id2;
268
269 if (!id_cache[RES3].is_cached) {
270 if (spi_res(flash, id_cache[RES3].bytes, 3))
271 return 0;
272 id_cache[RES3].is_cached = 1;
273 }
274
275 id1 = (id_cache[RES3].bytes[0] << 8) | id_cache[RES3].bytes[1];
276 id2 = id_cache[RES3].bytes[3];
277 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
278
279 if (id1 != flash->chip->manufacture_id || id2 != flash->chip->model_id)
280 return 0;
281
282 return 1;
283}
284
285/* Only used for some Atmel chips. */
286int probe_spi_at25f(struct flashctx *flash)
287{
288 static const unsigned char cmd[AT25F_RDID_OUTSIZE] = { AT25F_RDID };
289 unsigned char readarr[AT25F_RDID_INSIZE];
290 uint32_t id1;
291 uint32_t id2;
292
293 if (spi_send_command(flash, sizeof(cmd), sizeof(readarr), cmd, readarr))
294 return 0;
295
296 id1 = readarr[0];
297 id2 = readarr[1];
298
299 msg_cdbg("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
300
301 if (id1 == flash->chip->manufacture_id && id2 == flash->chip->model_id)
302 return 1;
303
304 return 0;
305}
306
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000307static int spi_poll_wip(struct flashctx *const flash, const unsigned int poll_delay)
308{
309 /* FIXME: We can't tell if spi_read_status_register() failed. */
310 /* FIXME: We don't time out. */
311 while (spi_read_status_register(flash) & SPI_SR_WIP)
312 programmer_delay(poll_delay);
313 /* FIXME: Check the status register for errors. */
314 return 0;
315}
316
Nico Huber4c8a9562017-10-15 11:20:58 +0200317/**
318 * Execute WREN plus another one byte `op`, optionally poll WIP afterwards.
319 *
320 * @param flash the flash chip's context
321 * @param op the operation to execute
322 * @param poll_delay interval in us for polling WIP, don't poll if zero
323 * @return 0 on success, non-zero otherwise
324 */
325static int spi_simple_write_cmd(struct flashctx *const flash, const uint8_t op, const unsigned int poll_delay)
snelson8913d082010-02-26 05:48:29 +0000326{
snelson8913d082010-02-26 05:48:29 +0000327 struct spi_command cmds[] = {
328 {
Edward O'Callaghan8cb48f72020-10-13 14:45:59 +1100329 .readarr = 0,
Nico Huber4c8a9562017-10-15 11:20:58 +0200330 .writecnt = 1,
331 .writearr = (const unsigned char[]){ JEDEC_WREN },
snelson8913d082010-02-26 05:48:29 +0000332 }, {
Edward O'Callaghan8cb48f72020-10-13 14:45:59 +1100333 .readarr = 0,
Nico Huber4c8a9562017-10-15 11:20:58 +0200334 .writecnt = 1,
335 .writearr = (const unsigned char[]){ op },
336 },
337 NULL_SPI_CMD,
338 };
snelson8913d082010-02-26 05:48:29 +0000339
Nico Huber4c8a9562017-10-15 11:20:58 +0200340 const int result = spi_send_multicommand(flash, cmds);
snelson8913d082010-02-26 05:48:29 +0000341 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000342 msg_cerr("%s failed during command execution\n", __func__);
snelson8913d082010-02-26 05:48:29 +0000343 return result;
344 }
345 /* Wait until the Write-In-Progress bit is cleared.
346 * This usually takes 1-85 s, so wait in 1 s steps.
347 */
Nico Huber4c8a9562017-10-15 11:20:58 +0200348
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000349 const int status = poll_delay ? spi_poll_wip(flash, poll_delay) : 0;
350
351 return result ? result : status;
352}
353
Edward O'Callaghana74ffcd2019-06-17 14:59:55 +1000354static int spi_set_extended_address(struct flashctx *const flash, const uint8_t addr_high)
355{
356 if (flash->address_high_byte != addr_high &&
357 spi_write_extended_address_register(flash, addr_high))
358 return -1;
359 flash->address_high_byte = addr_high;
360 return 0;
361}
362
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000363static int spi_prepare_address(struct flashctx *const flash, uint8_t cmd_buf[],
364 const bool native_4ba, const unsigned int addr)
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000365{
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000366 if (native_4ba || flash->in_4ba_mode) {
Edward O'Callaghana6673bd2019-06-24 15:22:28 +1000367 if (!spi_master_4ba(flash)) {
368 msg_cwarn("4-byte address requested but master can't handle 4-byte addresses.\n");
369 return -1;
370 }
Edward O'Callaghana74ffcd2019-06-17 14:59:55 +1000371 cmd_buf[1] = (addr >> 24) & 0xff;
372 cmd_buf[2] = (addr >> 16) & 0xff;
373 cmd_buf[3] = (addr >> 8) & 0xff;
374 cmd_buf[4] = (addr >> 0) & 0xff;
375 return 4;
376 } else {
377 if (flash->chip->feature_bits & FEATURE_4BA_EXT_ADDR) {
378 if (spi_set_extended_address(flash, addr >> 24))
379 return -1;
Edward O'Callaghana6673bd2019-06-24 15:22:28 +1000380 } else if (addr >> 24) {
381 msg_cerr("Can't handle 4-byte address for opcode '0x%02x'\n"
382 "with this chip/programmer combination.\n", cmd_buf[0]);
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100383 return -1;
Edward O'Callaghana74ffcd2019-06-17 14:59:55 +1000384 }
385 cmd_buf[1] = (addr >> 16) & 0xff;
386 cmd_buf[2] = (addr >> 8) & 0xff;
387 cmd_buf[3] = (addr >> 0) & 0xff;
388 return 3;
389 }
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000390}
391
392/**
393 * Execute WREN plus another `op` that takes an address and
394 * optional data, poll WIP afterwards.
395 *
396 * @param flash the flash chip's context
397 * @param op the operation to execute
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100398 * @param native_4ba whether `op` always takes a 4-byte address
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000399 * @param addr the address parameter to `op`
400 * @param out_bytes bytes to send after the address,
401 * may be NULL if and only if `out_bytes` is 0
402 * @param out_bytes number of bytes to send, 256 at most, may be zero
403 * @param poll_delay interval in us for polling WIP
404 * @return 0 on success, non-zero otherwise
405 */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000406static int spi_write_cmd(struct flashctx *const flash, const uint8_t op,
407 const bool native_4ba, const unsigned int addr,
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000408 const uint8_t *const out_bytes, const size_t out_len,
409 const unsigned int poll_delay)
410{
411 uint8_t cmd[1 + JEDEC_MAX_ADDR_LEN + 256];
412 struct spi_command cmds[] = {
413 {
Edward O'Callaghan8cb48f72020-10-13 14:45:59 +1100414 .readarr = 0,
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000415 .writecnt = 1,
416 .writearr = (const unsigned char[]){ JEDEC_WREN },
417 }, {
Edward O'Callaghan8cb48f72020-10-13 14:45:59 +1100418 .readarr = 0,
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000419 .writearr = cmd,
420 },
421 NULL_SPI_CMD,
422 };
423
424 cmd[0] = op;
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000425 const int addr_len = spi_prepare_address(flash, cmd, native_4ba, addr);
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000426 if (addr_len < 0)
427 return 1;
428
429 if (1 + addr_len + out_len > sizeof(cmd)) {
430 msg_cerr("%s called for too long a write\n", __func__);
431 return 1;
432 }
Angel Pons6bfd9e62020-03-31 15:32:10 +0200433 if (!out_bytes && out_len > 0)
434 return 1;
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000435
436 memcpy(cmd + 1 + addr_len, out_bytes, out_len);
437 cmds[1].writecnt = 1 + addr_len + out_len;
438
439 const int result = spi_send_multicommand(flash, cmds);
440 if (result)
441 msg_cerr("%s failed during command execution at address 0x%x\n", __func__, addr);
442
443 const int status = spi_poll_wip(flash, poll_delay);
444
445 return result ? result : status;
Nico Huber4c8a9562017-10-15 11:20:58 +0200446}
447
Edward O'Callaghanb064cb62020-10-13 13:36:53 +1100448static int spi_chip_erase_60(struct flashctx *flash)
Nico Huber4c8a9562017-10-15 11:20:58 +0200449{
450 /* This usually takes 1-85s, so wait in 1s steps. */
451 return spi_simple_write_cmd(flash, 0x60, 1000 * 1000);
452}
453
Edward O'Callaghanb064cb62020-10-13 13:36:53 +1100454static int spi_chip_erase_62(struct flashctx *flash)
Nico Huber4c8a9562017-10-15 11:20:58 +0200455{
456 /* This usually takes 2-5s, so wait in 100ms steps. */
457 return spi_simple_write_cmd(flash, 0x62, 100 * 1000);
458}
459
Edward O'Callaghanb064cb62020-10-13 13:36:53 +1100460static int spi_chip_erase_c7(struct flashctx *flash)
Nico Huber4c8a9562017-10-15 11:20:58 +0200461{
462 /* This usually takes 1-85s, so wait in 1s steps. */
463 return spi_simple_write_cmd(flash, 0xc7, 1000 * 1000);
snelson8913d082010-02-26 05:48:29 +0000464}
465
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100466int spi_block_erase_52(struct flashctx *flash, unsigned int addr,
467 unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000468{
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000469 /* This usually takes 100-4000ms, so wait in 100ms steps. */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000470 return spi_write_cmd(flash, 0x52, false, addr, NULL, 0, 100 * 1000);
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000471}
snelson8913d082010-02-26 05:48:29 +0000472
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000473/* Block size is usually
474 * 32M (one die) for Micron
475 */
476int spi_block_erase_c4(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
477{
478 /* This usually takes 240-480s, so wait in 500ms steps. */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000479 return spi_write_cmd(flash, 0xc4, false, addr, NULL, 0, 500 * 1000);
snelson8913d082010-02-26 05:48:29 +0000480}
481
482/* Block size is usually
483 * 64k for Macronix
484 * 32k for SST
485 * 4-32k non-uniform for EON
486 */
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100487int spi_block_erase_d8(struct flashctx *flash, unsigned int addr,
488 unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000489{
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000490 /* This usually takes 100-4000ms, so wait in 100ms steps. */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000491 return spi_write_cmd(flash, 0xd8, false, addr, NULL, 0, 100 * 1000);
snelson8913d082010-02-26 05:48:29 +0000492}
493
494/* Block size is usually
495 * 4k for PMC
496 */
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100497int spi_block_erase_d7(struct flashctx *flash, unsigned int addr,
498 unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000499{
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000500 /* This usually takes 100-4000ms, so wait in 100ms steps. */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000501 return spi_write_cmd(flash, 0xd7, false, addr, NULL, 0, 100 * 1000);
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000502}
snelson8913d082010-02-26 05:48:29 +0000503
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000504/* Page erase (usually 256B blocks) */
505int spi_block_erase_db(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
506{
507 /* This takes up to 20ms usually (on worn out devices
508 up to the 0.5s range), so wait in 1ms steps. */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000509 return spi_write_cmd(flash, 0xdb, false, addr, NULL, 0, 1 * 1000);
snelson8913d082010-02-26 05:48:29 +0000510}
511
snelson8913d082010-02-26 05:48:29 +0000512/* Sector size is usually 4k, though Macronix eliteflash has 64k */
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100513int spi_block_erase_20(struct flashctx *flash, unsigned int addr,
514 unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000515{
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000516 /* This usually takes 15-800ms, so wait in 10ms steps. */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000517 return spi_write_cmd(flash, 0x20, false, addr, NULL, 0, 10 * 1000);
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000518}
snelson8913d082010-02-26 05:48:29 +0000519
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000520int spi_block_erase_50(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
521{
522 /* This usually takes 10ms, so wait in 1ms steps. */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000523 return spi_write_cmd(flash, 0x50, false, addr, NULL, 0, 1 * 1000);
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000524}
Stefan Reinauercce56d52010-11-22 18:22:21 -0800525
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000526int spi_block_erase_81(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
527{
528 /* This usually takes 8ms, so wait in 1ms steps. */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000529 return spi_write_cmd(flash, 0x81, false, addr, NULL, 0, 1 * 1000);
snelson8913d082010-02-26 05:48:29 +0000530}
531
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100532int spi_block_erase_60(struct flashctx *flash, unsigned int addr,
533 unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000534{
Patrick Georgif3fa2992017-02-02 16:24:44 +0100535 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
snelsonfc007bb2010-03-24 23:14:32 +0000536 msg_cerr("%s called with incorrect arguments\n",
snelson8913d082010-02-26 05:48:29 +0000537 __func__);
538 return -1;
539 }
540 return spi_chip_erase_60(flash);
541}
542
Alan Green5d709732019-09-16 12:32:25 +1000543int spi_block_erase_62(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
544{
545 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
546 msg_cerr("%s called with incorrect arguments\n",
547 __func__);
548 return -1;
549 }
550 return spi_chip_erase_62(flash);
551}
552
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100553int spi_block_erase_c7(struct flashctx *flash, unsigned int addr,
554 unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000555{
Patrick Georgif3fa2992017-02-02 16:24:44 +0100556 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
snelsonfc007bb2010-03-24 23:14:32 +0000557 msg_cerr("%s called with incorrect arguments\n",
snelson8913d082010-02-26 05:48:29 +0000558 __func__);
559 return -1;
560 }
561 return spi_chip_erase_c7(flash);
562}
563
Edward O'Callaghan94934e82019-06-19 17:44:19 +1000564/* Erase 4 KB of flash with 4-bytes address from ANY mode (3-bytes or 4-bytes)
565 JEDEC_SE_4BA (21h) instruction is new for 4-bytes addressing flash chips.
566 The presence of this instruction for an exact chip should be checked
567 by its datasheet or from SFDP 4-Bytes Address Instruction Table (JESD216B). */
568int spi_block_erase_21(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
569{
570 /* This usually takes 15-800ms, so wait in 10ms steps. */
571 return spi_write_cmd(flash, 0x21, true, addr, NULL, 0, 10 * 1000);
572}
573
574/* Erase 32 KB of flash with 4-bytes address from ANY mode (3-bytes or 4-bytes)
575 JEDEC_BE_5C_4BA (5Ch) instruction is new for 4-bytes addressing flash chips.
576 The presence of this instruction for an exact chip should be checked
577 by its datasheet or from SFDP 4-Bytes Address Instruction Table (JESD216B). */
578int spi_block_erase_5c(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
579{
580 /* This usually takes 100-4000ms, so wait in 100ms steps. */
581 return spi_write_cmd(flash, 0x5c, true, addr, NULL, 0, 100 * 1000);
582}
583
584/* Erase 64 KB of flash with 4-bytes address from ANY mode (3-bytes or 4-bytes)
585 JEDEC_BE_DC_4BA (DCh) instruction is new for 4-bytes addressing flash chips.
586 The presence of this instruction for an exact chip should be checked
587 by its datasheet or from SFDP 4-Bytes Address Instruction Table (JESD216B). */
588int spi_block_erase_dc(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
589{
590 /* This usually takes 100-4000ms, so wait in 100ms steps. */
591 return spi_write_cmd(flash, 0xdc, true, addr, NULL, 0, 100 * 1000);
592}
593
Nikolai Artemieva66b6cd2020-08-31 18:07:13 +1000594erasefunc_t *spi_get_erasefn_from_opcode(uint8_t opcode)
595{
596 switch(opcode){
597 case 0xff:
598 case 0x00:
599 /* Not specified, assuming "not supported". */
600 return NULL;
601 case 0x20:
602 return &spi_block_erase_20;
603 case 0x21:
604 return &spi_block_erase_21;
605 case 0x50:
606 return &spi_block_erase_50;
607 case 0x52:
608 return &spi_block_erase_52;
609 case 0x5c:
610 return &spi_block_erase_5c;
611 case 0x60:
612 return &spi_block_erase_60;
613 case 0x62:
614 return &spi_block_erase_62;
615 case 0x81:
616 return &spi_block_erase_81;
617 case 0xc4:
618 return &spi_block_erase_c4;
619 case 0xc7:
620 return &spi_block_erase_c7;
621 case 0xd7:
622 return &spi_block_erase_d7;
623 case 0xd8:
624 return &spi_block_erase_d8;
625 case 0xdb:
626 return &spi_block_erase_db;
627 case 0xdc:
628 return &spi_block_erase_dc;
629 default:
630 msg_cinfo("%s: unknown erase opcode (0x%02x). Please report "
631 "this at flashrom@flashrom.org\n", __func__, opcode);
632 return NULL;
633 }
634}
635
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700636int spi_write_status_register_wren(const struct flashctx *flash, int status)
hailfingerc33d4732010-07-29 13:09:18 +0000637{
638 int result;
hailfingeree9ee132010-10-08 00:37:55 +0000639 int i = 0;
hailfingerc33d4732010-07-29 13:09:18 +0000640 struct spi_command cmds[] = {
641 {
642 /* WRSR requires either EWSR or WREN depending on chip type. */
643 .writecnt = JEDEC_WREN_OUTSIZE,
644 .writearr = (const unsigned char[]){ JEDEC_WREN },
645 .readcnt = 0,
646 .readarr = NULL,
647 }, {
648 .writecnt = JEDEC_WRSR_OUTSIZE,
649 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
650 .readcnt = 0,
651 .readarr = NULL,
652 }, {
653 .writecnt = 0,
654 .writearr = NULL,
655 .readcnt = 0,
656 .readarr = NULL,
657 }};
658
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700659 result = spi_send_multicommand(flash, cmds);
hailfingerc33d4732010-07-29 13:09:18 +0000660 if (result) {
661 msg_cerr("%s failed during command execution\n",
662 __func__);
hailfingeree9ee132010-10-08 00:37:55 +0000663 /* No point in waiting for the command to complete if execution
664 * failed.
665 */
666 return result;
hailfingerc33d4732010-07-29 13:09:18 +0000667 }
hailfingeree9ee132010-10-08 00:37:55 +0000668 /* WRSR performs a self-timed erase before the changes take effect.
669 * This may take 50-85 ms in most cases, and some chips apparently
670 * allow running RDSR only once. Therefore pick an initial delay of
671 * 100 ms, then wait in 10 ms steps until a total of 5 s have elapsed.
672 */
hailfingerc33d4732010-07-29 13:09:18 +0000673 programmer_delay(100 * 1000);
Edward O'Callaghan8b5e4732019-03-05 15:27:53 +1100674 while (spi_read_status_register(flash) & SPI_SR_WIP) {
hailfingeree9ee132010-10-08 00:37:55 +0000675 if (++i > 490) {
676 msg_cerr("Error: WIP bit after WRSR never cleared\n");
677 return TIMEOUT_ERROR;
678 }
679 programmer_delay(10 * 1000);
680 }
681 return 0;
hailfingerc33d4732010-07-29 13:09:18 +0000682}
683
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700684int spi_byte_program(struct flashctx *flash, unsigned int addr, uint8_t databyte)
snelson8913d082010-02-26 05:48:29 +0000685{
686 int result;
687 struct spi_command cmds[] = {
688 {
689 .writecnt = JEDEC_WREN_OUTSIZE,
690 .writearr = (const unsigned char[]){ JEDEC_WREN },
691 .readcnt = 0,
692 .readarr = NULL,
693 }, {
694 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE,
695 .writearr = (const unsigned char[]){
696 JEDEC_BYTE_PROGRAM,
697 (addr >> 16) & 0xff,
698 (addr >> 8) & 0xff,
699 (addr & 0xff),
700 databyte
701 },
702 .readcnt = 0,
703 .readarr = NULL,
704 }, {
705 .writecnt = 0,
706 .writearr = NULL,
707 .readcnt = 0,
708 .readarr = NULL,
709 }};
710
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700711 result = spi_send_multicommand(flash, cmds);
snelson8913d082010-02-26 05:48:29 +0000712 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000713 msg_cerr("%s failed during command execution at address 0x%x\n",
snelson8913d082010-02-26 05:48:29 +0000714 __func__, addr);
715 }
716 return result;
717}
718
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000719static int spi_nbyte_program(struct flashctx *flash, unsigned int addr, const uint8_t *bytes, unsigned int len)
snelson8913d082010-02-26 05:48:29 +0000720{
Edward O'Callaghana6673bd2019-06-24 15:22:28 +1000721 const bool native_4ba = flash->chip->feature_bits & FEATURE_4BA_WRITE && spi_master_4ba(flash);
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000722 const uint8_t op = native_4ba ? JEDEC_BYTE_PROGRAM_4BA : JEDEC_BYTE_PROGRAM;
723 return spi_write_cmd(flash, op, native_4ba, addr, bytes, len, 10);
snelson8913d082010-02-26 05:48:29 +0000724}
725
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100726int spi_nbyte_read(struct flashctx *flash, unsigned int address, uint8_t *bytes,
727 unsigned int len)
snelson8913d082010-02-26 05:48:29 +0000728{
Edward O'Callaghana6673bd2019-06-24 15:22:28 +1000729 const bool native_4ba = flash->chip->feature_bits & FEATURE_4BA_READ && spi_master_4ba(flash);
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000730 uint8_t cmd[1 + JEDEC_MAX_ADDR_LEN] = { native_4ba ? JEDEC_READ_4BA : JEDEC_READ, };
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000731
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000732 const int addr_len = spi_prepare_address(flash, cmd, native_4ba, address);
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000733 if (addr_len < 0)
734 return 1;
snelson8913d082010-02-26 05:48:29 +0000735
736 /* Send Read */
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000737 return spi_send_command(flash, 1 + addr_len, len, cmd, bytes);
snelson8913d082010-02-26 05:48:29 +0000738}
739
740/*
hailfinger39d159a2010-05-21 23:09:42 +0000741 * Read a part of the flash chip.
hailfingerc7d06c62010-07-14 16:19:05 +0000742 * FIXME: Use the chunk code from Michael Karcher instead.
Edward O'Callaghand825ac02019-07-26 21:36:16 +1000743 * Each naturally aligned area is read separately in chunks with a maximum size of chunksize.
snelson8913d082010-02-26 05:48:29 +0000744 */
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100745int spi_read_chunked(struct flashctx *flash, uint8_t *buf, unsigned int start,
746 unsigned int len, unsigned int chunksize)
snelson8913d082010-02-26 05:48:29 +0000747{
David Hendricks1ed1d352011-11-23 17:54:37 -0800748 int rc = 0, chunk_status = 0;
stefanctc5eb8a92011-11-23 09:13:48 +0000749 unsigned int i, j, starthere, lenhere, toread;
Edward O'Callaghand825ac02019-07-26 21:36:16 +1000750 /* Limit for multi-die 4-byte-addressing chips. */
751 unsigned int area_size = min(flash->chip->total_size * 1024, 16 * 1024 * 1024);
snelson8913d082010-02-26 05:48:29 +0000752
753 /* Warning: This loop has a very unusual condition and body.
Edward O'Callaghand825ac02019-07-26 21:36:16 +1000754 * The loop needs to go through each area with at least one affected
755 * byte. The lowest area number is (start / area_size) since that
756 * division rounds down. The highest area number we want is the area
snelson8913d082010-02-26 05:48:29 +0000757 * where the last byte of the range lives. That last byte has the
Edward O'Callaghand825ac02019-07-26 21:36:16 +1000758 * address (start + len - 1), thus the highest area number is
759 * (start + len - 1) / area_size. Since we want to include that last
760 * area as well, the loop condition uses <=.
snelson8913d082010-02-26 05:48:29 +0000761 */
Edward O'Callaghand825ac02019-07-26 21:36:16 +1000762 for (i = start / area_size; i <= (start + len - 1) / area_size; i++) {
763 /* Byte position of the first byte in the range in this area. */
snelson8913d082010-02-26 05:48:29 +0000764 /* starthere is an offset to the base address of the chip. */
Edward O'Callaghand825ac02019-07-26 21:36:16 +1000765 starthere = max(start, i * area_size);
766 /* Length of bytes in the range in this area. */
767 lenhere = min(start + len, (i + 1) * area_size) - starthere;
snelson8913d082010-02-26 05:48:29 +0000768 for (j = 0; j < lenhere; j += chunksize) {
769 toread = min(chunksize, lenhere - j);
Edward O'Callaghan4fe3a972019-06-19 16:56:10 +1000770 chunk_status = spi_nbyte_read(flash, starthere + j, buf + starthere - start + j, toread);
David Hendricks1ed1d352011-11-23 17:54:37 -0800771 if (chunk_status) {
772 if (ignore_error(chunk_status)) {
773 /* fill this chunk with 0xff bytes and
774 let caller know about the error */
775 memset(buf + starthere - start + j, 0xff, toread);
776 rc = chunk_status;
777 chunk_status = 0;
778 continue;
779 } else {
780 rc = chunk_status;
781 break;
782 }
783 }
snelson8913d082010-02-26 05:48:29 +0000784 }
David Hendricks1ed1d352011-11-23 17:54:37 -0800785 if (chunk_status)
snelson8913d082010-02-26 05:48:29 +0000786 break;
787 }
788
789 return rc;
790}
791
792/*
hailfinger39d159a2010-05-21 23:09:42 +0000793 * Write a part of the flash chip.
hailfingerc7d06c62010-07-14 16:19:05 +0000794 * FIXME: Use the chunk code from Michael Karcher instead.
hailfinger39d159a2010-05-21 23:09:42 +0000795 * Each page is written separately in chunks with a maximum size of chunksize.
796 */
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100797int spi_write_chunked(struct flashctx *flash, const uint8_t *buf, unsigned int start,
798 unsigned int len, unsigned int chunksize)
hailfinger39d159a2010-05-21 23:09:42 +0000799{
stefanctc5eb8a92011-11-23 09:13:48 +0000800 unsigned int i, j, starthere, lenhere, towrite;
hailfinger39d159a2010-05-21 23:09:42 +0000801 /* FIXME: page_size is the wrong variable. We need max_writechunk_size
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700802 * in struct flashctx to do this properly. All chips using
hailfinger39d159a2010-05-21 23:09:42 +0000803 * spi_chip_write_256 have page_size set to max_writechunk_size, so
804 * we're OK for now.
805 */
Patrick Georgif3fa2992017-02-02 16:24:44 +0100806 unsigned int page_size = flash->chip->page_size;
hailfinger39d159a2010-05-21 23:09:42 +0000807
808 /* Warning: This loop has a very unusual condition and body.
809 * The loop needs to go through each page with at least one affected
810 * byte. The lowest page number is (start / page_size) since that
811 * division rounds down. The highest page number we want is the page
812 * where the last byte of the range lives. That last byte has the
813 * address (start + len - 1), thus the highest page number is
814 * (start + len - 1) / page_size. Since we want to include that last
815 * page as well, the loop condition uses <=.
816 */
817 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
818 /* Byte position of the first byte in the range in this page. */
819 /* starthere is an offset to the base address of the chip. */
820 starthere = max(start, i * page_size);
821 /* Length of bytes in the range in this page. */
822 lenhere = min(start + len, (i + 1) * page_size) - starthere;
823 for (j = 0; j < lenhere; j += chunksize) {
Edward O'Callaghan4fe3a972019-06-19 16:56:10 +1000824 int rc;
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100825
hailfinger39d159a2010-05-21 23:09:42 +0000826 towrite = min(chunksize, lenhere - j);
Edward O'Callaghan4fe3a972019-06-19 16:56:10 +1000827 rc = spi_nbyte_program(flash, starthere + j, buf + starthere - start + j, towrite);
hailfinger39d159a2010-05-21 23:09:42 +0000828 if (rc)
Edward O'Callaghan4fe3a972019-06-19 16:56:10 +1000829 return rc;
hailfinger39d159a2010-05-21 23:09:42 +0000830 }
hailfinger39d159a2010-05-21 23:09:42 +0000831 }
832
Edward O'Callaghan4fe3a972019-06-19 16:56:10 +1000833 return 0;
hailfinger39d159a2010-05-21 23:09:42 +0000834}
835
836/*
snelson8913d082010-02-26 05:48:29 +0000837 * Program chip using byte programming. (SLOW!)
838 * This is for chips which can only handle one byte writes
839 * and for chips where memory mapped programming is impossible
840 * (e.g. due to size constraints in IT87* for over 512 kB)
841 */
hailfingerc7d06c62010-07-14 16:19:05 +0000842/* real chunksize is 1, logical chunksize is 1 */
Patrick Georgiab8353e2017-02-03 18:32:01 +0100843int spi_chip_write_1(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
snelson8913d082010-02-26 05:48:29 +0000844{
stefanctc5eb8a92011-11-23 09:13:48 +0000845 unsigned int i;
snelson8913d082010-02-26 05:48:29 +0000846
hailfingerc7d06c62010-07-14 16:19:05 +0000847 for (i = start; i < start + len; i++) {
Edward O'Callaghan4fe3a972019-06-19 16:56:10 +1000848 if (spi_nbyte_program(flash, i, buf + i - start, 1))
snelson8913d082010-02-26 05:48:29 +0000849 return 1;
snelson8913d082010-02-26 05:48:29 +0000850 }
snelson8913d082010-02-26 05:48:29 +0000851 return 0;
852}
853
Edward O'Callaghaneeaac6b2020-10-12 19:51:56 +1100854int default_spi_write_aai(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
hailfingerc7d06c62010-07-14 16:19:05 +0000855{
856 uint32_t pos = start;
snelson8913d082010-02-26 05:48:29 +0000857 int result;
hailfinger19db0922010-06-20 10:41:35 +0000858 unsigned char cmd[JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE] = {
859 JEDEC_AAI_WORD_PROGRAM,
860 };
snelson8913d082010-02-26 05:48:29 +0000861
hailfingerc7d06c62010-07-14 16:19:05 +0000862 /* The even start address and even length requirements can be either
863 * honored outside this function, or we can call spi_byte_program
864 * for the first and/or last byte and use AAI for the rest.
hailfinger71e1bd42010-10-13 22:26:56 +0000865 * FIXME: Move this to generic code.
hailfingerc7d06c62010-07-14 16:19:05 +0000866 */
hailfinger19db0922010-06-20 10:41:35 +0000867 /* The data sheet requires a start address with the low bit cleared. */
hailfingerc7d06c62010-07-14 16:19:05 +0000868 if (start % 2) {
hailfinger19db0922010-06-20 10:41:35 +0000869 msg_cerr("%s: start address not even! Please report a bug at "
870 "flashrom@flashrom.org\n", __func__);
hailfinger71e1bd42010-10-13 22:26:56 +0000871 if (spi_chip_write_1(flash, buf, start, start % 2))
872 return SPI_GENERIC_ERROR;
873 pos += start % 2;
874 /* Do not return an error for now. */
875 //return SPI_GENERIC_ERROR;
hailfinger19db0922010-06-20 10:41:35 +0000876 }
877 /* The data sheet requires total AAI write length to be even. */
878 if (len % 2) {
879 msg_cerr("%s: total write length not even! Please report a "
880 "bug at flashrom@flashrom.org\n", __func__);
hailfinger71e1bd42010-10-13 22:26:56 +0000881 /* Do not return an error for now. */
882 //return SPI_GENERIC_ERROR;
hailfinger19db0922010-06-20 10:41:35 +0000883 }
884
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000885 result = spi_write_cmd(flash, JEDEC_AAI_WORD_PROGRAM, false, start, buf + pos - start, 2, 10);
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000886 if (result)
Edward O'Callaghan633cbd62019-06-17 15:43:56 +1000887 goto bailout;
hailfinger19db0922010-06-20 10:41:35 +0000888
889 /* We already wrote 2 bytes in the multicommand step. */
890 pos += 2;
891
hailfinger71e1bd42010-10-13 22:26:56 +0000892 /* Are there at least two more bytes to write? */
893 while (pos < start + len - 1) {
hailfingerdef852d2010-10-27 22:07:11 +0000894 cmd[1] = buf[pos++ - start];
895 cmd[2] = buf[pos++ - start];
Edward O'Callaghan633cbd62019-06-17 15:43:56 +1000896 result = spi_send_command(flash, JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE, 0, cmd, NULL);
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100897 if (result != 0) {
Edward O'Callaghan633cbd62019-06-17 15:43:56 +1000898 msg_cerr("%s failed during followup AAI command execution: %d\n", __func__, result);
899 goto bailout;
900 }
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000901 if (spi_poll_wip(flash, 10))
902 goto bailout;
hailfinger19db0922010-06-20 10:41:35 +0000903 }
904
Edward O'Callaghaneeaac6b2020-10-12 19:51:56 +1100905 /* Use WRDI to exit AAI mode. This needs to be done before issuing any other non-AAI command. */
906 result = spi_write_disable(flash);
907 if (result != 0) {
908 msg_cerr("%s failed to disable AAI mode.\n", __func__);
909 return SPI_GENERIC_ERROR;
910 }
hailfinger71e1bd42010-10-13 22:26:56 +0000911
912 /* Write remaining byte (if any). */
913 if (pos < start + len) {
hailfingerdef852d2010-10-27 22:07:11 +0000914 if (spi_chip_write_1(flash, buf + pos - start, pos, pos % 2))
hailfinger71e1bd42010-10-13 22:26:56 +0000915 return SPI_GENERIC_ERROR;
hailfinger71e1bd42010-10-13 22:26:56 +0000916 }
917
snelson8913d082010-02-26 05:48:29 +0000918 return 0;
Edward O'Callaghan633cbd62019-06-17 15:43:56 +1000919
920bailout:
Edward O'Callaghaneeaac6b2020-10-12 19:51:56 +1100921 result = spi_write_disable(flash);
922 if (result != 0)
923 msg_cerr("%s failed to disable AAI mode.\n", __func__);
Edward O'Callaghan633cbd62019-06-17 15:43:56 +1000924 return SPI_GENERIC_ERROR;
snelson8913d082010-02-26 05:48:29 +0000925}