blob: 585010231bdc37479b546726288878f0146eb401 [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"
29
David Hendricks57b75242015-11-20 15:54:07 -080030enum id_type {
31 RDID,
32 RDID4,
33 REMS,
David Hendricks57b75242015-11-20 15:54:07 -080034 RES2,
Nikolai Artemiev4702c7c2020-08-31 12:49:50 +100035 RES3,
David Hendricks57b75242015-11-20 15:54:07 -080036 NUM_ID_TYPES,
37};
38
39static struct {
40 int is_cached;
41 unsigned char bytes[4]; /* enough to hold largest ID type */
42} id_cache[NUM_ID_TYPES];
43
44void clear_spi_id_cache(void)
45{
46 memset(id_cache, 0, sizeof(id_cache));
47 return;
48}
49
Souvik Ghoshd75cd672016-06-17 14:21:39 -070050static int spi_rdid(struct flashctx *flash, unsigned char *readarr, int bytes)
snelson8913d082010-02-26 05:48:29 +000051{
krause2eb76212011-01-17 07:50:42 +000052 static const unsigned char cmd[JEDEC_RDID_OUTSIZE] = { JEDEC_RDID };
snelson8913d082010-02-26 05:48:29 +000053 int ret;
54 int i;
55
Souvik Ghoshd75cd672016-06-17 14:21:39 -070056 ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr);
snelson8913d082010-02-26 05:48:29 +000057 if (ret)
58 return ret;
snelsonfc007bb2010-03-24 23:14:32 +000059 msg_cspew("RDID returned");
snelson8913d082010-02-26 05:48:29 +000060 for (i = 0; i < bytes; i++)
snelsonfc007bb2010-03-24 23:14:32 +000061 msg_cspew(" 0x%02x", readarr[i]);
62 msg_cspew(". ");
snelson8913d082010-02-26 05:48:29 +000063 return 0;
64}
65
Souvik Ghoshd75cd672016-06-17 14:21:39 -070066static int spi_rems(struct flashctx *flash, unsigned char *readarr)
snelson8913d082010-02-26 05:48:29 +000067{
Edward O'Callaghandfb71542020-05-14 18:41:42 +100068 static const unsigned char cmd[JEDEC_REMS_OUTSIZE] = { JEDEC_REMS, };
snelson8913d082010-02-26 05:48:29 +000069 int ret;
70
Souvik Ghoshd75cd672016-06-17 14:21:39 -070071 ret = spi_send_command(flash, sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
snelson8913d082010-02-26 05:48:29 +000072 if (ret)
73 return ret;
stefanct371e7e82011-07-07 19:56:58 +000074 msg_cspew("REMS returned 0x%02x 0x%02x. ", readarr[0], readarr[1]);
snelson8913d082010-02-26 05:48:29 +000075 return 0;
76}
77
Souvik Ghoshd75cd672016-06-17 14:21:39 -070078static int spi_res(struct flashctx *flash, unsigned char *readarr, int bytes)
snelson8913d082010-02-26 05:48:29 +000079{
Edward O'Callaghandfb71542020-05-14 18:41:42 +100080 static const unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, };
snelson8913d082010-02-26 05:48:29 +000081 int ret;
hailfingercb0564e2010-06-20 10:39:33 +000082 int i;
snelson8913d082010-02-26 05:48:29 +000083
Souvik Ghoshd75cd672016-06-17 14:21:39 -070084 ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr);
snelson8913d082010-02-26 05:48:29 +000085 if (ret)
86 return ret;
hailfingercb0564e2010-06-20 10:39:33 +000087 msg_cspew("RES returned");
88 for (i = 0; i < bytes; i++)
89 msg_cspew(" 0x%02x", readarr[i]);
90 msg_cspew(". ");
snelson8913d082010-02-26 05:48:29 +000091 return 0;
92}
93
Souvik Ghoshd75cd672016-06-17 14:21:39 -070094int spi_write_enable(struct flashctx *flash)
snelson8913d082010-02-26 05:48:29 +000095{
krause2eb76212011-01-17 07:50:42 +000096 static const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN };
snelson8913d082010-02-26 05:48:29 +000097 int result;
98
99 /* Send WREN (Write Enable) */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700100 result = spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
snelson8913d082010-02-26 05:48:29 +0000101
102 if (result)
snelsonfc007bb2010-03-24 23:14:32 +0000103 msg_cerr("%s failed\n", __func__);
snelson8913d082010-02-26 05:48:29 +0000104
105 return result;
106}
107
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700108int spi_write_disable(struct flashctx *flash)
snelson8913d082010-02-26 05:48:29 +0000109{
krause2eb76212011-01-17 07:50:42 +0000110 static const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI };
snelson8913d082010-02-26 05:48:29 +0000111
112 /* Send WRDI (Write Disable) */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700113 return spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
snelson8913d082010-02-26 05:48:29 +0000114}
115
Edward O'Callaghan6d86b102020-10-23 22:57:00 +1100116static void rdid_get_ids(unsigned char *readarr, int bytes,
117 uint32_t *id1, uint32_t *id2)
snelson8913d082010-02-26 05:48:29 +0000118{
snelson8913d082010-02-26 05:48:29 +0000119 if (!oddparity(readarr[0]))
snelsonfc007bb2010-03-24 23:14:32 +0000120 msg_cdbg("RDID byte 0 parity violation. ");
snelson8913d082010-02-26 05:48:29 +0000121
hailfingercb0564e2010-06-20 10:39:33 +0000122 /* Check if this is a continuation vendor ID.
123 * FIXME: Handle continuation device IDs.
124 */
snelson8913d082010-02-26 05:48:29 +0000125 if (readarr[0] == 0x7f) {
126 if (!oddparity(readarr[1]))
snelsonfc007bb2010-03-24 23:14:32 +0000127 msg_cdbg("RDID byte 1 parity violation. ");
David Hendricks7f7c7112012-10-11 17:15:48 -0700128 *id1 = (readarr[0] << 8) | readarr[1];
129 *id2 = readarr[2];
snelson8913d082010-02-26 05:48:29 +0000130 if (bytes > 3) {
David Hendricks7f7c7112012-10-11 17:15:48 -0700131 *id2 <<= 8;
132 *id2 |= readarr[3];
snelson8913d082010-02-26 05:48:29 +0000133 }
134 } else {
David Hendricks7f7c7112012-10-11 17:15:48 -0700135 *id1 = readarr[0];
136 *id2 = (readarr[1] << 8) | readarr[2];
snelson8913d082010-02-26 05:48:29 +0000137 }
David Hendricks7f7c7112012-10-11 17:15:48 -0700138}
snelson8913d082010-02-26 05:48:29 +0000139
Edward O'Callaghan4cfb22f2020-10-15 12:21:04 +1100140static int compare_id(const struct flashctx *flash, uint32_t id1, uint32_t id2)
David Hendricks7f7c7112012-10-11 17:15:48 -0700141{
Edward O'Callaghan4cfb22f2020-10-15 12:21:04 +1100142 const struct flashchip *chip = flash->chip;
snelson8913d082010-02-26 05:48:29 +0000143
Edward O'Callaghan4cfb22f2020-10-15 12:21:04 +1100144 msg_cdbg("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
145 if (id1 == chip->manufacture_id && id2 == chip->model_id)
snelson8913d082010-02-26 05:48:29 +0000146 return 1;
snelson8913d082010-02-26 05:48:29 +0000147
148 /* Test if this is a pure vendor match. */
Edward O'Callaghan4cfb22f2020-10-15 12:21:04 +1100149 if (id1 == chip->manufacture_id && GENERIC_DEVICE_ID == chip->model_id)
snelson8913d082010-02-26 05:48:29 +0000150 return 1;
151
152 /* Test if there is any vendor ID. */
Urja Rannikko544a3a72015-06-22 23:59:15 +0000153 if (GENERIC_MANUF_ID == chip->manufacture_id && id1 != 0xff && id1 != 0x00)
snelson8913d082010-02-26 05:48:29 +0000154 return 1;
155
156 return 0;
157}
158
Edward O'Callaghanc2a12a92020-05-14 17:58:32 +1000159static int probe_spi_rdid_generic(struct flashctx *flash, int bytes)
snelson8913d082010-02-26 05:48:29 +0000160{
David Hendricks57b75242015-11-20 15:54:07 -0800161 uint32_t id1, id2;
Edward O'Callaghanc2a12a92020-05-14 17:58:32 +1000162 enum id_type idty = bytes == 3 ? RDID : RDID4;
David Hendricks7f7c7112012-10-11 17:15:48 -0700163
Edward O'Callaghanc2a12a92020-05-14 17:58:32 +1000164 if (!id_cache[idty].is_cached) {
165 const int ret = spi_rdid(flash, id_cache[idty].bytes, bytes);
166 if (ret == SPI_INVALID_LENGTH)
167 msg_cinfo("%d byte RDID not supported on this SPI controller\n", bytes);
168 if (ret)
David Hendricks7f7c7112012-10-11 17:15:48 -0700169 return 0;
Edward O'Callaghanc2a12a92020-05-14 17:58:32 +1000170 id_cache[idty].is_cached = 1;
David Hendricks7f7c7112012-10-11 17:15:48 -0700171 }
172
Edward O'Callaghanc2a12a92020-05-14 17:58:32 +1000173 rdid_get_ids(id_cache[idty].bytes, bytes, &id1, &id2);
David Hendricks7f7c7112012-10-11 17:15:48 -0700174 return compare_id(flash, id1, id2);
snelson8913d082010-02-26 05:48:29 +0000175}
176
Edward O'Callaghanc2a12a92020-05-14 17:58:32 +1000177int probe_spi_rdid(struct flashctx *flash)
178{
179 return probe_spi_rdid_generic(flash, 3);
180}
181
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700182int probe_spi_rdid4(struct flashctx *flash)
snelson8913d082010-02-26 05:48:29 +0000183{
Edward O'Callaghanc2a12a92020-05-14 17:58:32 +1000184 return probe_spi_rdid_generic(flash, 4);
snelson8913d082010-02-26 05:48:29 +0000185}
186
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700187int probe_spi_rems(struct flashctx *flash)
snelson8913d082010-02-26 05:48:29 +0000188{
David Hendricks57b75242015-11-20 15:54:07 -0800189 uint32_t id1, id2;
snelson8913d082010-02-26 05:48:29 +0000190
David Hendricks57b75242015-11-20 15:54:07 -0800191 if (!id_cache[REMS].is_cached) {
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700192 if (spi_rems(flash, id_cache[REMS].bytes))
David Hendricks7f7c7112012-10-11 17:15:48 -0700193 return 0;
David Hendricks57b75242015-11-20 15:54:07 -0800194 id_cache[REMS].is_cached = 1;
stefanct9e6b98a2011-05-28 02:37:14 +0000195 }
snelson8913d082010-02-26 05:48:29 +0000196
David Hendricks57b75242015-11-20 15:54:07 -0800197 id1 = id_cache[REMS].bytes[0];
198 id2 = id_cache[REMS].bytes[1];
David Hendricks7f7c7112012-10-11 17:15:48 -0700199 return compare_id(flash, id1, id2);
snelson8913d082010-02-26 05:48:29 +0000200}
201
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700202int probe_spi_res1(struct flashctx *flash)
snelson8913d082010-02-26 05:48:29 +0000203{
krause2eb76212011-01-17 07:50:42 +0000204 static const unsigned char allff[] = {0xff, 0xff, 0xff};
205 static const unsigned char all00[] = {0x00, 0x00, 0x00};
snelson8913d082010-02-26 05:48:29 +0000206 unsigned char readarr[3];
207 uint32_t id2;
snelson8913d082010-02-26 05:48:29 +0000208
hailfinger59a83572010-05-28 17:07:57 +0000209 /* We only want one-byte RES if RDID and REMS are unusable. */
210
snelson8913d082010-02-26 05:48:29 +0000211 /* Check if RDID is usable and does not return 0xff 0xff 0xff or
212 * 0x00 0x00 0x00. In that case, RES is pointless.
213 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700214 if (!spi_rdid(flash, readarr, 3) && memcmp(readarr, allff, 3) &&
snelson8913d082010-02-26 05:48:29 +0000215 memcmp(readarr, all00, 3)) {
216 msg_cdbg("Ignoring RES in favour of RDID.\n");
217 return 0;
218 }
219 /* Check if REMS is usable and does not return 0xff 0xff or
220 * 0x00 0x00. In that case, RES is pointless.
221 */
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100222 if (!spi_rems(flash, readarr) &&
223 memcmp(readarr, allff, JEDEC_REMS_INSIZE) &&
snelson8913d082010-02-26 05:48:29 +0000224 memcmp(readarr, all00, JEDEC_REMS_INSIZE)) {
225 msg_cdbg("Ignoring RES in favour of REMS.\n");
226 return 0;
227 }
228
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700229 if (spi_res(flash, readarr, 1)) {
snelson8913d082010-02-26 05:48:29 +0000230 return 0;
stefanct9e6b98a2011-05-28 02:37:14 +0000231 }
snelson8913d082010-02-26 05:48:29 +0000232
snelson8913d082010-02-26 05:48:29 +0000233 id2 = readarr[0];
hailfinger59a83572010-05-28 17:07:57 +0000234
snelsonfc007bb2010-03-24 23:14:32 +0000235 msg_cdbg("%s: id 0x%x\n", __func__, id2);
hailfinger59a83572010-05-28 17:07:57 +0000236
Patrick Georgif3fa2992017-02-02 16:24:44 +0100237 if (id2 != flash->chip->model_id)
snelson8913d082010-02-26 05:48:29 +0000238 return 0;
239
snelson8913d082010-02-26 05:48:29 +0000240 return 1;
241}
242
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700243int probe_spi_res2(struct flashctx *flash)
hailfinger59a83572010-05-28 17:07:57 +0000244{
hailfinger59a83572010-05-28 17:07:57 +0000245 uint32_t id1, id2;
246
David Hendricks57b75242015-11-20 15:54:07 -0800247 if (!id_cache[RES2].is_cached) {
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700248 if (spi_res(flash, id_cache[RES2].bytes, 2))
David Hendricks57b75242015-11-20 15:54:07 -0800249 return 0;
250 id_cache[RES2].is_cached = 1;
stefanct9e6b98a2011-05-28 02:37:14 +0000251 }
hailfinger59a83572010-05-28 17:07:57 +0000252
David Hendricks57b75242015-11-20 15:54:07 -0800253 id1 = id_cache[RES2].bytes[0];
254 id2 = id_cache[RES2].bytes[1];
hailfinger59a83572010-05-28 17:07:57 +0000255 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
256
Patrick Georgif3fa2992017-02-02 16:24:44 +0100257 if (id1 != flash->chip->manufacture_id || id2 != flash->chip->model_id)
hailfinger59a83572010-05-28 17:07:57 +0000258 return 0;
259
hailfinger59a83572010-05-28 17:07:57 +0000260 return 1;
261}
262
Nikolai Artemiev4702c7c2020-08-31 12:49:50 +1000263int probe_spi_res3(struct flashctx *flash)
264{
265 uint32_t id1, id2;
266
267 if (!id_cache[RES3].is_cached) {
268 if (spi_res(flash, id_cache[RES3].bytes, 3))
269 return 0;
270 id_cache[RES3].is_cached = 1;
271 }
272
273 id1 = (id_cache[RES3].bytes[0] << 8) | id_cache[RES3].bytes[1];
274 id2 = id_cache[RES3].bytes[3];
275 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
276
277 if (id1 != flash->chip->manufacture_id || id2 != flash->chip->model_id)
278 return 0;
279
280 return 1;
281}
282
283/* Only used for some Atmel chips. */
284int probe_spi_at25f(struct flashctx *flash)
285{
286 static const unsigned char cmd[AT25F_RDID_OUTSIZE] = { AT25F_RDID };
287 unsigned char readarr[AT25F_RDID_INSIZE];
288 uint32_t id1;
289 uint32_t id2;
290
291 if (spi_send_command(flash, sizeof(cmd), sizeof(readarr), cmd, readarr))
292 return 0;
293
294 id1 = readarr[0];
295 id2 = readarr[1];
296
297 msg_cdbg("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
298
299 if (id1 == flash->chip->manufacture_id && id2 == flash->chip->model_id)
300 return 1;
301
302 return 0;
303}
304
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000305static int spi_poll_wip(struct flashctx *const flash, const unsigned int poll_delay)
306{
307 /* FIXME: We can't tell if spi_read_status_register() failed. */
308 /* FIXME: We don't time out. */
309 while (spi_read_status_register(flash) & SPI_SR_WIP)
310 programmer_delay(poll_delay);
311 /* FIXME: Check the status register for errors. */
312 return 0;
313}
314
Nico Huber4c8a9562017-10-15 11:20:58 +0200315/**
316 * Execute WREN plus another one byte `op`, optionally poll WIP afterwards.
317 *
318 * @param flash the flash chip's context
319 * @param op the operation to execute
320 * @param poll_delay interval in us for polling WIP, don't poll if zero
321 * @return 0 on success, non-zero otherwise
322 */
323static int spi_simple_write_cmd(struct flashctx *const flash, const uint8_t op, const unsigned int poll_delay)
snelson8913d082010-02-26 05:48:29 +0000324{
snelson8913d082010-02-26 05:48:29 +0000325 struct spi_command cmds[] = {
326 {
Edward O'Callaghan8cb48f72020-10-13 14:45:59 +1100327 .readarr = 0,
Edward O'Callaghan787f1e32020-10-30 11:32:28 +1100328 .writecnt = JEDEC_WREN_OUTSIZE,
Nico Huber4c8a9562017-10-15 11:20:58 +0200329 .writearr = (const unsigned char[]){ JEDEC_WREN },
snelson8913d082010-02-26 05:48:29 +0000330 }, {
Edward O'Callaghan8cb48f72020-10-13 14:45:59 +1100331 .readarr = 0,
Nico Huber4c8a9562017-10-15 11:20:58 +0200332 .writecnt = 1,
333 .writearr = (const unsigned char[]){ op },
334 },
335 NULL_SPI_CMD,
336 };
snelson8913d082010-02-26 05:48:29 +0000337
Nico Huber4c8a9562017-10-15 11:20:58 +0200338 const int result = spi_send_multicommand(flash, cmds);
Edward O'Callaghan7b7acea2020-11-23 23:49:44 +1100339 if (result)
snelsonfc007bb2010-03-24 23:14:32 +0000340 msg_cerr("%s failed during command execution\n", __func__);
Nico Huber4c8a9562017-10-15 11:20:58 +0200341
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000342 const int status = poll_delay ? spi_poll_wip(flash, poll_delay) : 0;
343
344 return result ? result : status;
345}
346
Edward O'Callaghan99974452020-10-13 13:28:33 +1100347static int spi_write_extended_address_register(struct flashctx *const flash, const uint8_t regdata)
348{
349 const uint8_t op = flash->chip->wrea_override ? : JEDEC_WRITE_EXT_ADDR_REG;
350 struct spi_command cmds[] = {
351 {
352 .readarr = 0,
353 .writecnt = 1,
354 .writearr = (const unsigned char[]){ JEDEC_WREN },
355 }, {
356 .readarr = 0,
357 .writecnt = 2,
358 .writearr = (const unsigned char[]){ op, regdata },
359 },
360 NULL_SPI_CMD,
361 };
362
363 const int result = spi_send_multicommand(flash, cmds);
364 if (result)
365 msg_cerr("%s failed during command execution\n", __func__);
366 return result;
367}
368
369int spi_set_extended_address(struct flashctx *const flash, const uint8_t addr_high)
Edward O'Callaghana74ffcd2019-06-17 14:59:55 +1000370{
371 if (flash->address_high_byte != addr_high &&
372 spi_write_extended_address_register(flash, addr_high))
373 return -1;
374 flash->address_high_byte = addr_high;
375 return 0;
376}
377
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000378static int spi_prepare_address(struct flashctx *const flash, uint8_t cmd_buf[],
379 const bool native_4ba, const unsigned int addr)
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000380{
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000381 if (native_4ba || flash->in_4ba_mode) {
Edward O'Callaghana6673bd2019-06-24 15:22:28 +1000382 if (!spi_master_4ba(flash)) {
383 msg_cwarn("4-byte address requested but master can't handle 4-byte addresses.\n");
384 return -1;
385 }
Edward O'Callaghana74ffcd2019-06-17 14:59:55 +1000386 cmd_buf[1] = (addr >> 24) & 0xff;
387 cmd_buf[2] = (addr >> 16) & 0xff;
388 cmd_buf[3] = (addr >> 8) & 0xff;
389 cmd_buf[4] = (addr >> 0) & 0xff;
390 return 4;
391 } else {
392 if (flash->chip->feature_bits & FEATURE_4BA_EXT_ADDR) {
393 if (spi_set_extended_address(flash, addr >> 24))
394 return -1;
Edward O'Callaghana6673bd2019-06-24 15:22:28 +1000395 } else if (addr >> 24) {
396 msg_cerr("Can't handle 4-byte address for opcode '0x%02x'\n"
397 "with this chip/programmer combination.\n", cmd_buf[0]);
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100398 return -1;
Edward O'Callaghana74ffcd2019-06-17 14:59:55 +1000399 }
400 cmd_buf[1] = (addr >> 16) & 0xff;
401 cmd_buf[2] = (addr >> 8) & 0xff;
402 cmd_buf[3] = (addr >> 0) & 0xff;
403 return 3;
404 }
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000405}
406
407/**
408 * Execute WREN plus another `op` that takes an address and
409 * optional data, poll WIP afterwards.
410 *
411 * @param flash the flash chip's context
412 * @param op the operation to execute
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100413 * @param native_4ba whether `op` always takes a 4-byte address
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000414 * @param addr the address parameter to `op`
415 * @param out_bytes bytes to send after the address,
416 * may be NULL if and only if `out_bytes` is 0
417 * @param out_bytes number of bytes to send, 256 at most, may be zero
418 * @param poll_delay interval in us for polling WIP
419 * @return 0 on success, non-zero otherwise
420 */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000421static int spi_write_cmd(struct flashctx *const flash, const uint8_t op,
422 const bool native_4ba, const unsigned int addr,
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000423 const uint8_t *const out_bytes, const size_t out_len,
424 const unsigned int poll_delay)
425{
426 uint8_t cmd[1 + JEDEC_MAX_ADDR_LEN + 256];
427 struct spi_command cmds[] = {
428 {
Edward O'Callaghan8cb48f72020-10-13 14:45:59 +1100429 .readarr = 0,
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000430 .writecnt = 1,
431 .writearr = (const unsigned char[]){ JEDEC_WREN },
432 }, {
Edward O'Callaghan8cb48f72020-10-13 14:45:59 +1100433 .readarr = 0,
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000434 .writearr = cmd,
435 },
436 NULL_SPI_CMD,
437 };
438
439 cmd[0] = op;
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000440 const int addr_len = spi_prepare_address(flash, cmd, native_4ba, addr);
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000441 if (addr_len < 0)
442 return 1;
443
444 if (1 + addr_len + out_len > sizeof(cmd)) {
445 msg_cerr("%s called for too long a write\n", __func__);
446 return 1;
447 }
Angel Pons6bfd9e62020-03-31 15:32:10 +0200448 if (!out_bytes && out_len > 0)
449 return 1;
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000450
451 memcpy(cmd + 1 + addr_len, out_bytes, out_len);
452 cmds[1].writecnt = 1 + addr_len + out_len;
453
454 const int result = spi_send_multicommand(flash, cmds);
455 if (result)
456 msg_cerr("%s failed during command execution at address 0x%x\n", __func__, addr);
457
458 const int status = spi_poll_wip(flash, poll_delay);
459
460 return result ? result : status;
Nico Huber4c8a9562017-10-15 11:20:58 +0200461}
462
Edward O'Callaghanb064cb62020-10-13 13:36:53 +1100463static int spi_chip_erase_60(struct flashctx *flash)
Nico Huber4c8a9562017-10-15 11:20:58 +0200464{
465 /* This usually takes 1-85s, so wait in 1s steps. */
Edward O'Callaghan67b5fd12020-10-30 11:41:01 +1100466 return spi_simple_write_cmd(flash, JEDEC_CE_60, 1000 * 1000);
Nico Huber4c8a9562017-10-15 11:20:58 +0200467}
468
Edward O'Callaghanb064cb62020-10-13 13:36:53 +1100469static int spi_chip_erase_62(struct flashctx *flash)
Nico Huber4c8a9562017-10-15 11:20:58 +0200470{
471 /* This usually takes 2-5s, so wait in 100ms steps. */
Edward O'Callaghan67b5fd12020-10-30 11:41:01 +1100472 return spi_simple_write_cmd(flash, JEDEC_CE_62, 100 * 1000);
Nico Huber4c8a9562017-10-15 11:20:58 +0200473}
474
Edward O'Callaghanb064cb62020-10-13 13:36:53 +1100475static int spi_chip_erase_c7(struct flashctx *flash)
Nico Huber4c8a9562017-10-15 11:20:58 +0200476{
477 /* This usually takes 1-85s, so wait in 1s steps. */
Edward O'Callaghan67b5fd12020-10-30 11:41:01 +1100478 return spi_simple_write_cmd(flash, JEDEC_CE_C7, 1000 * 1000);
snelson8913d082010-02-26 05:48:29 +0000479}
480
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100481int spi_block_erase_52(struct flashctx *flash, unsigned int addr,
482 unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000483{
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000484 /* This usually takes 100-4000ms, so wait in 100ms steps. */
Edward O'Callaghan67b5fd12020-10-30 11:41:01 +1100485 return spi_write_cmd(flash, JEDEC_BE_52, false, addr, NULL, 0, 100 * 1000);
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000486}
snelson8913d082010-02-26 05:48:29 +0000487
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000488/* Block size is usually
489 * 32M (one die) for Micron
490 */
491int spi_block_erase_c4(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
492{
493 /* This usually takes 240-480s, so wait in 500ms steps. */
Edward O'Callaghan67b5fd12020-10-30 11:41:01 +1100494 return spi_write_cmd(flash, JEDEC_BE_C4, false, addr, NULL, 0, 500 * 1000);
snelson8913d082010-02-26 05:48:29 +0000495}
496
497/* Block size is usually
498 * 64k for Macronix
499 * 32k for SST
500 * 4-32k non-uniform for EON
501 */
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100502int spi_block_erase_d8(struct flashctx *flash, unsigned int addr,
503 unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000504{
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000505 /* This usually takes 100-4000ms, so wait in 100ms steps. */
Edward O'Callaghan67b5fd12020-10-30 11:41:01 +1100506 return spi_write_cmd(flash, JEDEC_BE_D8, false, addr, NULL, 0, 100 * 1000);
snelson8913d082010-02-26 05:48:29 +0000507}
508
509/* Block size is usually
510 * 4k for PMC
511 */
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100512int spi_block_erase_d7(struct flashctx *flash, unsigned int addr,
513 unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000514{
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000515 /* This usually takes 100-4000ms, so wait in 100ms steps. */
Edward O'Callaghan67b5fd12020-10-30 11:41:01 +1100516 return spi_write_cmd(flash, JEDEC_BE_D7, false, addr, NULL, 0, 100 * 1000);
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000517}
snelson8913d082010-02-26 05:48:29 +0000518
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000519/* Page erase (usually 256B blocks) */
520int spi_block_erase_db(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
521{
522 /* This takes up to 20ms usually (on worn out devices
523 up to the 0.5s range), so wait in 1ms steps. */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000524 return spi_write_cmd(flash, 0xdb, false, addr, NULL, 0, 1 * 1000);
snelson8913d082010-02-26 05:48:29 +0000525}
526
snelson8913d082010-02-26 05:48:29 +0000527/* Sector size is usually 4k, though Macronix eliteflash has 64k */
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100528int spi_block_erase_20(struct flashctx *flash, unsigned int addr,
529 unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000530{
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000531 /* This usually takes 15-800ms, so wait in 10ms steps. */
Edward O'Callaghan67b5fd12020-10-30 11:41:01 +1100532 return spi_write_cmd(flash, JEDEC_SE, false, addr, NULL, 0, 10 * 1000);
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000533}
snelson8913d082010-02-26 05:48:29 +0000534
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000535int spi_block_erase_50(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
536{
537 /* This usually takes 10ms, so wait in 1ms steps. */
Edward O'Callaghan67b5fd12020-10-30 11:41:01 +1100538 return spi_write_cmd(flash, JEDEC_BE_50, false, addr, NULL, 0, 1 * 1000);
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000539}
Stefan Reinauercce56d52010-11-22 18:22:21 -0800540
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000541int spi_block_erase_81(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
542{
543 /* This usually takes 8ms, so wait in 1ms steps. */
Edward O'Callaghan67b5fd12020-10-30 11:41:01 +1100544 return spi_write_cmd(flash, JEDEC_BE_81, false, addr, NULL, 0, 1 * 1000);
snelson8913d082010-02-26 05:48:29 +0000545}
546
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100547int spi_block_erase_60(struct flashctx *flash, unsigned int addr,
548 unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000549{
Patrick Georgif3fa2992017-02-02 16:24:44 +0100550 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
snelsonfc007bb2010-03-24 23:14:32 +0000551 msg_cerr("%s called with incorrect arguments\n",
snelson8913d082010-02-26 05:48:29 +0000552 __func__);
553 return -1;
554 }
555 return spi_chip_erase_60(flash);
556}
557
Alan Green5d709732019-09-16 12:32:25 +1000558int spi_block_erase_62(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
559{
560 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
561 msg_cerr("%s called with incorrect arguments\n",
562 __func__);
563 return -1;
564 }
565 return spi_chip_erase_62(flash);
566}
567
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100568int spi_block_erase_c7(struct flashctx *flash, unsigned int addr,
569 unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000570{
Patrick Georgif3fa2992017-02-02 16:24:44 +0100571 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
snelsonfc007bb2010-03-24 23:14:32 +0000572 msg_cerr("%s called with incorrect arguments\n",
snelson8913d082010-02-26 05:48:29 +0000573 __func__);
574 return -1;
575 }
576 return spi_chip_erase_c7(flash);
577}
578
Edward O'Callaghan6d86b102020-10-23 22:57:00 +1100579/* Erase 4 KB of flash with 4-bytes address from ANY mode (3-bytes or 4-bytes) */
Edward O'Callaghan94934e82019-06-19 17:44:19 +1000580int spi_block_erase_21(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
581{
582 /* This usually takes 15-800ms, so wait in 10ms steps. */
583 return spi_write_cmd(flash, 0x21, true, addr, NULL, 0, 10 * 1000);
584}
585
Edward O'Callaghan6d86b102020-10-23 22:57:00 +1100586/* Erase 32 KB of flash with 4-bytes address from ANY mode (3-bytes or 4-bytes) */
Edward O'Callaghan94934e82019-06-19 17:44:19 +1000587int spi_block_erase_5c(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
588{
589 /* This usually takes 100-4000ms, so wait in 100ms steps. */
590 return spi_write_cmd(flash, 0x5c, true, addr, NULL, 0, 100 * 1000);
591}
592
Edward O'Callaghan6d86b102020-10-23 22:57:00 +1100593/* Erase 64 KB of flash with 4-bytes address from ANY mode (3-bytes or 4-bytes) */
Edward O'Callaghan94934e82019-06-19 17:44:19 +1000594int spi_block_erase_dc(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
595{
596 /* This usually takes 100-4000ms, so wait in 100ms steps. */
597 return spi_write_cmd(flash, 0xdc, true, addr, NULL, 0, 100 * 1000);
598}
599
Nikolai Artemieva66b6cd2020-08-31 18:07:13 +1000600erasefunc_t *spi_get_erasefn_from_opcode(uint8_t opcode)
601{
602 switch(opcode){
603 case 0xff:
604 case 0x00:
605 /* Not specified, assuming "not supported". */
606 return NULL;
607 case 0x20:
608 return &spi_block_erase_20;
609 case 0x21:
610 return &spi_block_erase_21;
611 case 0x50:
612 return &spi_block_erase_50;
613 case 0x52:
614 return &spi_block_erase_52;
615 case 0x5c:
616 return &spi_block_erase_5c;
617 case 0x60:
618 return &spi_block_erase_60;
619 case 0x62:
620 return &spi_block_erase_62;
621 case 0x81:
622 return &spi_block_erase_81;
623 case 0xc4:
624 return &spi_block_erase_c4;
625 case 0xc7:
626 return &spi_block_erase_c7;
627 case 0xd7:
628 return &spi_block_erase_d7;
629 case 0xd8:
630 return &spi_block_erase_d8;
631 case 0xdb:
632 return &spi_block_erase_db;
633 case 0xdc:
634 return &spi_block_erase_dc;
635 default:
636 msg_cinfo("%s: unknown erase opcode (0x%02x). Please report "
637 "this at flashrom@flashrom.org\n", __func__, opcode);
638 return NULL;
639 }
640}
641
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000642static int spi_nbyte_program(struct flashctx *flash, unsigned int addr, const uint8_t *bytes, unsigned int len)
snelson8913d082010-02-26 05:48:29 +0000643{
Edward O'Callaghana6673bd2019-06-24 15:22:28 +1000644 const bool native_4ba = flash->chip->feature_bits & FEATURE_4BA_WRITE && spi_master_4ba(flash);
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000645 const uint8_t op = native_4ba ? JEDEC_BYTE_PROGRAM_4BA : JEDEC_BYTE_PROGRAM;
646 return spi_write_cmd(flash, op, native_4ba, addr, bytes, len, 10);
snelson8913d082010-02-26 05:48:29 +0000647}
648
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100649int spi_nbyte_read(struct flashctx *flash, unsigned int address, uint8_t *bytes,
650 unsigned int len)
snelson8913d082010-02-26 05:48:29 +0000651{
Edward O'Callaghana6673bd2019-06-24 15:22:28 +1000652 const bool native_4ba = flash->chip->feature_bits & FEATURE_4BA_READ && spi_master_4ba(flash);
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000653 uint8_t cmd[1 + JEDEC_MAX_ADDR_LEN] = { native_4ba ? JEDEC_READ_4BA : JEDEC_READ, };
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000654
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000655 const int addr_len = spi_prepare_address(flash, cmd, native_4ba, address);
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000656 if (addr_len < 0)
657 return 1;
snelson8913d082010-02-26 05:48:29 +0000658
659 /* Send Read */
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000660 return spi_send_command(flash, 1 + addr_len, len, cmd, bytes);
snelson8913d082010-02-26 05:48:29 +0000661}
662
663/*
hailfinger39d159a2010-05-21 23:09:42 +0000664 * Read a part of the flash chip.
Nico Huber470a6492019-06-18 23:39:56 +0200665 * Data is read in chunks with a maximum size of chunksize.
snelson8913d082010-02-26 05:48:29 +0000666 */
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100667int spi_read_chunked(struct flashctx *flash, uint8_t *buf, unsigned int start,
668 unsigned int len, unsigned int chunksize)
snelson8913d082010-02-26 05:48:29 +0000669{
Sam McNally50bbf202020-12-18 16:25:52 +1100670 int ret, rc = 0;
Nico Huber470a6492019-06-18 23:39:56 +0200671 size_t to_read;
672 for (; len; len -= to_read, buf += to_read, start += to_read) {
673 to_read = min(chunksize, len);
674 ret = spi_nbyte_read(flash, start, buf, to_read);
675 if (ignore_error(ret)) {
676 /* fill this chunk with 0xff bytes and
677 let caller know about the error */
678 memset(buf, 0xff, to_read);
Sam McNally50bbf202020-12-18 16:25:52 +1100679 rc = ret;
Nico Huber470a6492019-06-18 23:39:56 +0200680 } else if (ret)
681 return ret;
snelson8913d082010-02-26 05:48:29 +0000682 }
Sam McNally50bbf202020-12-18 16:25:52 +1100683 return rc;
snelson8913d082010-02-26 05:48:29 +0000684}
685
686/*
hailfinger39d159a2010-05-21 23:09:42 +0000687 * Write a part of the flash chip.
hailfingerc7d06c62010-07-14 16:19:05 +0000688 * FIXME: Use the chunk code from Michael Karcher instead.
hailfinger39d159a2010-05-21 23:09:42 +0000689 * Each page is written separately in chunks with a maximum size of chunksize.
690 */
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100691int spi_write_chunked(struct flashctx *flash, const uint8_t *buf, unsigned int start,
692 unsigned int len, unsigned int chunksize)
hailfinger39d159a2010-05-21 23:09:42 +0000693{
stefanctc5eb8a92011-11-23 09:13:48 +0000694 unsigned int i, j, starthere, lenhere, towrite;
hailfinger39d159a2010-05-21 23:09:42 +0000695 /* FIXME: page_size is the wrong variable. We need max_writechunk_size
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700696 * in struct flashctx to do this properly. All chips using
hailfinger39d159a2010-05-21 23:09:42 +0000697 * spi_chip_write_256 have page_size set to max_writechunk_size, so
698 * we're OK for now.
699 */
Patrick Georgif3fa2992017-02-02 16:24:44 +0100700 unsigned int page_size = flash->chip->page_size;
hailfinger39d159a2010-05-21 23:09:42 +0000701
702 /* Warning: This loop has a very unusual condition and body.
703 * The loop needs to go through each page with at least one affected
704 * byte. The lowest page number is (start / page_size) since that
705 * division rounds down. The highest page number we want is the page
706 * where the last byte of the range lives. That last byte has the
707 * address (start + len - 1), thus the highest page number is
708 * (start + len - 1) / page_size. Since we want to include that last
709 * page as well, the loop condition uses <=.
710 */
711 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
712 /* Byte position of the first byte in the range in this page. */
713 /* starthere is an offset to the base address of the chip. */
714 starthere = max(start, i * page_size);
715 /* Length of bytes in the range in this page. */
716 lenhere = min(start + len, (i + 1) * page_size) - starthere;
717 for (j = 0; j < lenhere; j += chunksize) {
Edward O'Callaghan4fe3a972019-06-19 16:56:10 +1000718 int rc;
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100719
hailfinger39d159a2010-05-21 23:09:42 +0000720 towrite = min(chunksize, lenhere - j);
Edward O'Callaghan4fe3a972019-06-19 16:56:10 +1000721 rc = spi_nbyte_program(flash, starthere + j, buf + starthere - start + j, towrite);
hailfinger39d159a2010-05-21 23:09:42 +0000722 if (rc)
Edward O'Callaghan4fe3a972019-06-19 16:56:10 +1000723 return rc;
hailfinger39d159a2010-05-21 23:09:42 +0000724 }
hailfinger39d159a2010-05-21 23:09:42 +0000725 }
726
Edward O'Callaghan4fe3a972019-06-19 16:56:10 +1000727 return 0;
hailfinger39d159a2010-05-21 23:09:42 +0000728}
729
730/*
snelson8913d082010-02-26 05:48:29 +0000731 * Program chip using byte programming. (SLOW!)
732 * This is for chips which can only handle one byte writes
733 * and for chips where memory mapped programming is impossible
734 * (e.g. due to size constraints in IT87* for over 512 kB)
735 */
hailfingerc7d06c62010-07-14 16:19:05 +0000736/* real chunksize is 1, logical chunksize is 1 */
Patrick Georgiab8353e2017-02-03 18:32:01 +0100737int spi_chip_write_1(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
snelson8913d082010-02-26 05:48:29 +0000738{
stefanctc5eb8a92011-11-23 09:13:48 +0000739 unsigned int i;
snelson8913d082010-02-26 05:48:29 +0000740
hailfingerc7d06c62010-07-14 16:19:05 +0000741 for (i = start; i < start + len; i++) {
Edward O'Callaghan4fe3a972019-06-19 16:56:10 +1000742 if (spi_nbyte_program(flash, i, buf + i - start, 1))
snelson8913d082010-02-26 05:48:29 +0000743 return 1;
snelson8913d082010-02-26 05:48:29 +0000744 }
snelson8913d082010-02-26 05:48:29 +0000745 return 0;
746}
747
Edward O'Callaghaneeaac6b2020-10-12 19:51:56 +1100748int default_spi_write_aai(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
hailfingerc7d06c62010-07-14 16:19:05 +0000749{
750 uint32_t pos = start;
snelson8913d082010-02-26 05:48:29 +0000751 int result;
hailfinger19db0922010-06-20 10:41:35 +0000752 unsigned char cmd[JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE] = {
753 JEDEC_AAI_WORD_PROGRAM,
754 };
snelson8913d082010-02-26 05:48:29 +0000755
hailfingerc7d06c62010-07-14 16:19:05 +0000756 /* The even start address and even length requirements can be either
757 * honored outside this function, or we can call spi_byte_program
758 * for the first and/or last byte and use AAI for the rest.
hailfinger71e1bd42010-10-13 22:26:56 +0000759 * FIXME: Move this to generic code.
hailfingerc7d06c62010-07-14 16:19:05 +0000760 */
hailfinger19db0922010-06-20 10:41:35 +0000761 /* The data sheet requires a start address with the low bit cleared. */
hailfingerc7d06c62010-07-14 16:19:05 +0000762 if (start % 2) {
hailfinger19db0922010-06-20 10:41:35 +0000763 msg_cerr("%s: start address not even! Please report a bug at "
764 "flashrom@flashrom.org\n", __func__);
hailfinger71e1bd42010-10-13 22:26:56 +0000765 if (spi_chip_write_1(flash, buf, start, start % 2))
766 return SPI_GENERIC_ERROR;
767 pos += start % 2;
768 /* Do not return an error for now. */
769 //return SPI_GENERIC_ERROR;
hailfinger19db0922010-06-20 10:41:35 +0000770 }
771 /* The data sheet requires total AAI write length to be even. */
772 if (len % 2) {
773 msg_cerr("%s: total write length not even! Please report a "
774 "bug at flashrom@flashrom.org\n", __func__);
hailfinger71e1bd42010-10-13 22:26:56 +0000775 /* Do not return an error for now. */
776 //return SPI_GENERIC_ERROR;
hailfinger19db0922010-06-20 10:41:35 +0000777 }
778
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000779 result = spi_write_cmd(flash, JEDEC_AAI_WORD_PROGRAM, false, start, buf + pos - start, 2, 10);
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000780 if (result)
Edward O'Callaghan633cbd62019-06-17 15:43:56 +1000781 goto bailout;
hailfinger19db0922010-06-20 10:41:35 +0000782
783 /* We already wrote 2 bytes in the multicommand step. */
784 pos += 2;
785
hailfinger71e1bd42010-10-13 22:26:56 +0000786 /* Are there at least two more bytes to write? */
787 while (pos < start + len - 1) {
hailfingerdef852d2010-10-27 22:07:11 +0000788 cmd[1] = buf[pos++ - start];
789 cmd[2] = buf[pos++ - start];
Edward O'Callaghan633cbd62019-06-17 15:43:56 +1000790 result = spi_send_command(flash, JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE, 0, cmd, NULL);
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100791 if (result != 0) {
Edward O'Callaghan633cbd62019-06-17 15:43:56 +1000792 msg_cerr("%s failed during followup AAI command execution: %d\n", __func__, result);
793 goto bailout;
794 }
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000795 if (spi_poll_wip(flash, 10))
796 goto bailout;
hailfinger19db0922010-06-20 10:41:35 +0000797 }
798
Edward O'Callaghaneeaac6b2020-10-12 19:51:56 +1100799 /* Use WRDI to exit AAI mode. This needs to be done before issuing any other non-AAI command. */
800 result = spi_write_disable(flash);
801 if (result != 0) {
802 msg_cerr("%s failed to disable AAI mode.\n", __func__);
803 return SPI_GENERIC_ERROR;
804 }
hailfinger71e1bd42010-10-13 22:26:56 +0000805
806 /* Write remaining byte (if any). */
807 if (pos < start + len) {
hailfingerdef852d2010-10-27 22:07:11 +0000808 if (spi_chip_write_1(flash, buf + pos - start, pos, pos % 2))
hailfinger71e1bd42010-10-13 22:26:56 +0000809 return SPI_GENERIC_ERROR;
hailfinger71e1bd42010-10-13 22:26:56 +0000810 }
811
snelson8913d082010-02-26 05:48:29 +0000812 return 0;
Edward O'Callaghan633cbd62019-06-17 15:43:56 +1000813
814bailout:
Edward O'Callaghaneeaac6b2020-10-12 19:51:56 +1100815 result = spi_write_disable(flash);
816 if (result != 0)
817 msg_cerr("%s failed to disable AAI mode.\n", __func__);
Edward O'Callaghan633cbd62019-06-17 15:43:56 +1000818 return SPI_GENERIC_ERROR;
snelson8913d082010-02-26 05:48:29 +0000819}
Edward O'Callaghan99974452020-10-13 13:28:33 +1100820
821static int spi_enter_exit_4ba(struct flashctx *const flash, const bool enter)
822{
823 const unsigned char cmd = enter ? JEDEC_ENTER_4_BYTE_ADDR_MODE : JEDEC_EXIT_4_BYTE_ADDR_MODE;
824 int ret = 1;
825
826 if (flash->chip->feature_bits & FEATURE_4BA_ENTER)
827 ret = spi_send_command(flash, sizeof(cmd), 0, &cmd, NULL);
828 else if (flash->chip->feature_bits & FEATURE_4BA_ENTER_WREN)
829 ret = spi_simple_write_cmd(flash, cmd, 0);
830 else if (flash->chip->feature_bits & FEATURE_4BA_ENTER_EAR7)
831 ret = spi_set_extended_address(flash, enter ? 0x80 : 0x00);
832
833 if (!ret)
834 flash->in_4ba_mode = enter;
835 return ret;
836}
837
838int spi_enter_4ba(struct flashctx *const flash)
839{
840 return spi_enter_exit_4ba(flash, true);
841}
842
843int spi_exit_4ba(struct flashctx *flash)
844{
845 return spi_enter_exit_4ba(flash, false);
846}