blob: 6b9dce6d99f9aa16058f6b685b220d1aebe662b2 [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.
15 *
snelson8913d082010-02-26 05:48:29 +000016 */
17
18/*
19 * Contains the common SPI chip driver functions
20 */
21
Nico Huber4c8a9562017-10-15 11:20:58 +020022#include <stddef.h>
snelson8913d082010-02-26 05:48:29 +000023#include <string.h>
24#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,
37 NUM_ID_TYPES,
38};
39
40static struct {
41 int is_cached;
42 unsigned char bytes[4]; /* enough to hold largest ID type */
43} id_cache[NUM_ID_TYPES];
44
45void clear_spi_id_cache(void)
46{
47 memset(id_cache, 0, sizeof(id_cache));
48 return;
49}
50
Souvik Ghoshd75cd672016-06-17 14:21:39 -070051static int spi_rdid(struct flashctx *flash, unsigned char *readarr, int bytes)
snelson8913d082010-02-26 05:48:29 +000052{
krause2eb76212011-01-17 07:50:42 +000053 static const unsigned char cmd[JEDEC_RDID_OUTSIZE] = { JEDEC_RDID };
snelson8913d082010-02-26 05:48:29 +000054 int ret;
55 int i;
56
Souvik Ghoshd75cd672016-06-17 14:21:39 -070057 ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr);
snelson8913d082010-02-26 05:48:29 +000058 if (ret)
59 return ret;
snelsonfc007bb2010-03-24 23:14:32 +000060 msg_cspew("RDID returned");
snelson8913d082010-02-26 05:48:29 +000061 for (i = 0; i < bytes; i++)
snelsonfc007bb2010-03-24 23:14:32 +000062 msg_cspew(" 0x%02x", readarr[i]);
63 msg_cspew(". ");
snelson8913d082010-02-26 05:48:29 +000064 return 0;
65}
66
Souvik Ghoshd75cd672016-06-17 14:21:39 -070067static int spi_rems(struct flashctx *flash, unsigned char *readarr)
snelson8913d082010-02-26 05:48:29 +000068{
69 unsigned char cmd[JEDEC_REMS_OUTSIZE] = { JEDEC_REMS, 0, 0, 0 };
70 uint32_t readaddr;
71 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 == SPI_INVALID_ADDRESS) {
75 /* Find the lowest even address allowed for reads. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -070076 readaddr = (spi_get_valid_read_addr(flash) + 1) & ~1;
snelson8913d082010-02-26 05:48:29 +000077 cmd[1] = (readaddr >> 16) & 0xff,
78 cmd[2] = (readaddr >> 8) & 0xff,
79 cmd[3] = (readaddr >> 0) & 0xff,
Souvik Ghoshd75cd672016-06-17 14:21:39 -070080 ret = spi_send_command(flash, sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
snelson8913d082010-02-26 05:48:29 +000081 }
82 if (ret)
83 return ret;
stefanct371e7e82011-07-07 19:56:58 +000084 msg_cspew("REMS returned 0x%02x 0x%02x. ", readarr[0], readarr[1]);
snelson8913d082010-02-26 05:48:29 +000085 return 0;
86}
87
Souvik Ghoshd75cd672016-06-17 14:21:39 -070088static int spi_res(struct flashctx *flash, unsigned char *readarr, int bytes)
snelson8913d082010-02-26 05:48:29 +000089{
90 unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, 0, 0, 0 };
91 uint32_t readaddr;
92 int ret;
hailfingercb0564e2010-06-20 10:39:33 +000093 int i;
snelson8913d082010-02-26 05:48:29 +000094
Souvik Ghoshd75cd672016-06-17 14:21:39 -070095 ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr);
snelson8913d082010-02-26 05:48:29 +000096 if (ret == SPI_INVALID_ADDRESS) {
97 /* Find the lowest even address allowed for reads. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -070098 readaddr = (spi_get_valid_read_addr(flash) + 1) & ~1;
snelson8913d082010-02-26 05:48:29 +000099 cmd[1] = (readaddr >> 16) & 0xff,
100 cmd[2] = (readaddr >> 8) & 0xff,
101 cmd[3] = (readaddr >> 0) & 0xff,
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700102 ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr);
snelson8913d082010-02-26 05:48:29 +0000103 }
104 if (ret)
105 return ret;
hailfingercb0564e2010-06-20 10:39:33 +0000106 msg_cspew("RES returned");
107 for (i = 0; i < bytes; i++)
108 msg_cspew(" 0x%02x", readarr[i]);
109 msg_cspew(". ");
snelson8913d082010-02-26 05:48:29 +0000110 return 0;
111}
112
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700113int spi_write_enable(struct flashctx *flash)
snelson8913d082010-02-26 05:48:29 +0000114{
krause2eb76212011-01-17 07:50:42 +0000115 static const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN };
snelson8913d082010-02-26 05:48:29 +0000116 int result;
117
118 /* Send WREN (Write Enable) */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700119 result = spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
snelson8913d082010-02-26 05:48:29 +0000120
121 if (result)
snelsonfc007bb2010-03-24 23:14:32 +0000122 msg_cerr("%s failed\n", __func__);
snelson8913d082010-02-26 05:48:29 +0000123
124 return result;
125}
126
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700127int spi_write_disable(struct flashctx *flash)
snelson8913d082010-02-26 05:48:29 +0000128{
krause2eb76212011-01-17 07:50:42 +0000129 static const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI };
snelson8913d082010-02-26 05:48:29 +0000130
131 /* Send WRDI (Write Disable) */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700132 return spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
snelson8913d082010-02-26 05:48:29 +0000133}
134
David Hendricks7f7c7112012-10-11 17:15:48 -0700135static void rdid_get_ids(unsigned char *readarr,
136 int bytes, uint32_t *id1, uint32_t *id2)
snelson8913d082010-02-26 05:48:29 +0000137{
snelson8913d082010-02-26 05:48:29 +0000138 if (!oddparity(readarr[0]))
snelsonfc007bb2010-03-24 23:14:32 +0000139 msg_cdbg("RDID byte 0 parity violation. ");
snelson8913d082010-02-26 05:48:29 +0000140
hailfingercb0564e2010-06-20 10:39:33 +0000141 /* Check if this is a continuation vendor ID.
142 * FIXME: Handle continuation device IDs.
143 */
snelson8913d082010-02-26 05:48:29 +0000144 if (readarr[0] == 0x7f) {
145 if (!oddparity(readarr[1]))
snelsonfc007bb2010-03-24 23:14:32 +0000146 msg_cdbg("RDID byte 1 parity violation. ");
David Hendricks7f7c7112012-10-11 17:15:48 -0700147 *id1 = (readarr[0] << 8) | readarr[1];
148 *id2 = readarr[2];
snelson8913d082010-02-26 05:48:29 +0000149 if (bytes > 3) {
David Hendricks7f7c7112012-10-11 17:15:48 -0700150 *id2 <<= 8;
151 *id2 |= readarr[3];
snelson8913d082010-02-26 05:48:29 +0000152 }
153 } else {
David Hendricks7f7c7112012-10-11 17:15:48 -0700154 *id1 = readarr[0];
155 *id2 = (readarr[1] << 8) | readarr[2];
snelson8913d082010-02-26 05:48:29 +0000156 }
David Hendricks7f7c7112012-10-11 17:15:48 -0700157}
snelson8913d082010-02-26 05:48:29 +0000158
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700159static int compare_id(struct flashctx *flash, uint32_t id1, uint32_t id2)
David Hendricks7f7c7112012-10-11 17:15:48 -0700160{
161 msg_cdbg("id1 0x%02x, id2 0x%02x\n", id1, id2);
snelson8913d082010-02-26 05:48:29 +0000162
Edward O'Callaghan71e23142019-03-03 23:08:22 +1100163 if (id1 == flash->chip->manufacture_id && id2 == flash->chip->model_id)
snelson8913d082010-02-26 05:48:29 +0000164 return 1;
snelson8913d082010-02-26 05:48:29 +0000165
166 /* Test if this is a pure vendor match. */
Patrick Georgif3fa2992017-02-02 16:24:44 +0100167 if (id1 == flash->chip->manufacture_id &&
168 GENERIC_DEVICE_ID == flash->chip->model_id)
snelson8913d082010-02-26 05:48:29 +0000169 return 1;
170
171 /* Test if there is any vendor ID. */
Patrick Georgif3fa2992017-02-02 16:24:44 +0100172 if (GENERIC_MANUF_ID == flash->chip->manufacture_id &&
snelson8913d082010-02-26 05:48:29 +0000173 id1 != 0xff)
174 return 1;
175
176 return 0;
177}
178
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700179int probe_spi_rdid(struct flashctx *flash)
snelson8913d082010-02-26 05:48:29 +0000180{
David Hendricks57b75242015-11-20 15:54:07 -0800181 uint32_t id1, id2;
David Hendricks7f7c7112012-10-11 17:15:48 -0700182
David Hendricks57b75242015-11-20 15:54:07 -0800183 if (!id_cache[RDID].is_cached) {
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700184 if (spi_rdid(flash, id_cache[RDID].bytes, 3))
David Hendricks7f7c7112012-10-11 17:15:48 -0700185 return 0;
David Hendricks57b75242015-11-20 15:54:07 -0800186 id_cache[RDID].is_cached = 1;
David Hendricks7f7c7112012-10-11 17:15:48 -0700187 }
188
David Hendricks57b75242015-11-20 15:54:07 -0800189 rdid_get_ids(id_cache[RDID].bytes, 3, &id1, &id2);
David Hendricks7f7c7112012-10-11 17:15:48 -0700190 return compare_id(flash, id1, id2);
snelson8913d082010-02-26 05:48:29 +0000191}
192
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700193int probe_spi_rdid4(struct flashctx *flash)
snelson8913d082010-02-26 05:48:29 +0000194{
David Hendricks57b75242015-11-20 15:54:07 -0800195 uint32_t id1, id2;
David Hendricks7f7c7112012-10-11 17:15:48 -0700196
hailfingercb0564e2010-06-20 10:39:33 +0000197 /* Some SPI controllers do not support commands with writecnt=1 and
198 * readcnt=4.
199 */
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100200 switch (spi_master->type) {
hailfinger90c7d542010-05-31 15:27:27 +0000201#if CONFIG_INTERNAL == 1
hailfinger324a9cc2010-05-26 01:45:41 +0000202#if defined(__i386__) || defined(__x86_64__)
hailfingercb0564e2010-06-20 10:39:33 +0000203 case SPI_CONTROLLER_IT87XX:
snelson8913d082010-02-26 05:48:29 +0000204 case SPI_CONTROLLER_WBSIO:
hailfingercb0564e2010-06-20 10:39:33 +0000205 msg_cinfo("4 byte RDID not supported on this SPI controller\n");
hailfingercb0564e2010-06-20 10:39:33 +0000206 break;
snelson8913d082010-02-26 05:48:29 +0000207#endif
hailfinger324a9cc2010-05-26 01:45:41 +0000208#endif
snelson8913d082010-02-26 05:48:29 +0000209 default:
David Hendricks7f7c7112012-10-11 17:15:48 -0700210 break;
snelson8913d082010-02-26 05:48:29 +0000211 }
212
David Hendricks57b75242015-11-20 15:54:07 -0800213 if (!id_cache[RDID4].is_cached) {
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700214 if (spi_rdid(flash, id_cache[RDID4].bytes, 4))
David Hendricks7f7c7112012-10-11 17:15:48 -0700215 return 0;
David Hendricks57b75242015-11-20 15:54:07 -0800216 id_cache[RDID4].is_cached = 1;
David Hendricks7f7c7112012-10-11 17:15:48 -0700217 }
David Hendricks57b75242015-11-20 15:54:07 -0800218
219 rdid_get_ids(id_cache[RDID4].bytes, 4, &id1, &id2);
David Hendricks7f7c7112012-10-11 17:15:48 -0700220 return compare_id(flash, id1, id2);
snelson8913d082010-02-26 05:48:29 +0000221}
222
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700223int probe_spi_rems(struct flashctx *flash)
snelson8913d082010-02-26 05:48:29 +0000224{
David Hendricks57b75242015-11-20 15:54:07 -0800225 uint32_t id1, id2;
snelson8913d082010-02-26 05:48:29 +0000226
David Hendricks57b75242015-11-20 15:54:07 -0800227 if (!id_cache[REMS].is_cached) {
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700228 if (spi_rems(flash, id_cache[REMS].bytes))
David Hendricks7f7c7112012-10-11 17:15:48 -0700229 return 0;
David Hendricks57b75242015-11-20 15:54:07 -0800230 id_cache[REMS].is_cached = 1;
stefanct9e6b98a2011-05-28 02:37:14 +0000231 }
snelson8913d082010-02-26 05:48:29 +0000232
David Hendricks57b75242015-11-20 15:54:07 -0800233 id1 = id_cache[REMS].bytes[0];
234 id2 = id_cache[REMS].bytes[1];
David Hendricks7f7c7112012-10-11 17:15:48 -0700235 return compare_id(flash, id1, id2);
snelson8913d082010-02-26 05:48:29 +0000236}
237
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700238int probe_spi_res1(struct flashctx *flash)
snelson8913d082010-02-26 05:48:29 +0000239{
krause2eb76212011-01-17 07:50:42 +0000240 static const unsigned char allff[] = {0xff, 0xff, 0xff};
241 static const unsigned char all00[] = {0x00, 0x00, 0x00};
snelson8913d082010-02-26 05:48:29 +0000242 unsigned char readarr[3];
243 uint32_t id2;
snelson8913d082010-02-26 05:48:29 +0000244
hailfinger59a83572010-05-28 17:07:57 +0000245 /* We only want one-byte RES if RDID and REMS are unusable. */
246
snelson8913d082010-02-26 05:48:29 +0000247 /* Check if RDID is usable and does not return 0xff 0xff 0xff or
248 * 0x00 0x00 0x00. In that case, RES is pointless.
249 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700250 if (!spi_rdid(flash, readarr, 3) && memcmp(readarr, allff, 3) &&
snelson8913d082010-02-26 05:48:29 +0000251 memcmp(readarr, all00, 3)) {
252 msg_cdbg("Ignoring RES in favour of RDID.\n");
253 return 0;
254 }
255 /* Check if REMS is usable and does not return 0xff 0xff or
256 * 0x00 0x00. In that case, RES is pointless.
257 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700258 if (!spi_rems(flash, readarr) && memcmp(readarr, allff, JEDEC_REMS_INSIZE) &&
snelson8913d082010-02-26 05:48:29 +0000259 memcmp(readarr, all00, JEDEC_REMS_INSIZE)) {
260 msg_cdbg("Ignoring RES in favour of REMS.\n");
261 return 0;
262 }
263
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700264 if (spi_res(flash, readarr, 1)) {
snelson8913d082010-02-26 05:48:29 +0000265 return 0;
stefanct9e6b98a2011-05-28 02:37:14 +0000266 }
snelson8913d082010-02-26 05:48:29 +0000267
snelson8913d082010-02-26 05:48:29 +0000268 id2 = readarr[0];
hailfinger59a83572010-05-28 17:07:57 +0000269
snelsonfc007bb2010-03-24 23:14:32 +0000270 msg_cdbg("%s: id 0x%x\n", __func__, id2);
hailfinger59a83572010-05-28 17:07:57 +0000271
Patrick Georgif3fa2992017-02-02 16:24:44 +0100272 if (id2 != flash->chip->model_id)
snelson8913d082010-02-26 05:48:29 +0000273 return 0;
274
snelson8913d082010-02-26 05:48:29 +0000275 return 1;
276}
277
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700278int probe_spi_res2(struct flashctx *flash)
hailfinger59a83572010-05-28 17:07:57 +0000279{
hailfinger59a83572010-05-28 17:07:57 +0000280 uint32_t id1, id2;
281
David Hendricks57b75242015-11-20 15:54:07 -0800282 if (!id_cache[RES2].is_cached) {
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700283 if (spi_res(flash, id_cache[RES2].bytes, 2))
David Hendricks57b75242015-11-20 15:54:07 -0800284 return 0;
285 id_cache[RES2].is_cached = 1;
stefanct9e6b98a2011-05-28 02:37:14 +0000286 }
hailfinger59a83572010-05-28 17:07:57 +0000287
David Hendricks57b75242015-11-20 15:54:07 -0800288 id1 = id_cache[RES2].bytes[0];
289 id2 = id_cache[RES2].bytes[1];
hailfinger59a83572010-05-28 17:07:57 +0000290 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
291
Patrick Georgif3fa2992017-02-02 16:24:44 +0100292 if (id1 != flash->chip->manufacture_id || id2 != flash->chip->model_id)
hailfinger59a83572010-05-28 17:07:57 +0000293 return 0;
294
hailfinger59a83572010-05-28 17:07:57 +0000295 return 1;
296}
297
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000298static int spi_poll_wip(struct flashctx *const flash, const unsigned int poll_delay)
299{
300 /* FIXME: We can't tell if spi_read_status_register() failed. */
301 /* FIXME: We don't time out. */
302 while (spi_read_status_register(flash) & SPI_SR_WIP)
303 programmer_delay(poll_delay);
304 /* FIXME: Check the status register for errors. */
305 return 0;
306}
307
Nico Huber4c8a9562017-10-15 11:20:58 +0200308/**
309 * Execute WREN plus another one byte `op`, optionally poll WIP afterwards.
310 *
311 * @param flash the flash chip's context
312 * @param op the operation to execute
313 * @param poll_delay interval in us for polling WIP, don't poll if zero
314 * @return 0 on success, non-zero otherwise
315 */
316static int spi_simple_write_cmd(struct flashctx *const flash, const uint8_t op, const unsigned int poll_delay)
snelson8913d082010-02-26 05:48:29 +0000317{
snelson8913d082010-02-26 05:48:29 +0000318 struct spi_command cmds[] = {
319 {
Nico Huber4c8a9562017-10-15 11:20:58 +0200320 .writecnt = 1,
321 .writearr = (const unsigned char[]){ JEDEC_WREN },
snelson8913d082010-02-26 05:48:29 +0000322 }, {
Nico Huber4c8a9562017-10-15 11:20:58 +0200323 .writecnt = 1,
324 .writearr = (const unsigned char[]){ op },
325 },
326 NULL_SPI_CMD,
327 };
snelson8913d082010-02-26 05:48:29 +0000328
Nico Huber4c8a9562017-10-15 11:20:58 +0200329 const int result = spi_send_multicommand(flash, cmds);
snelson8913d082010-02-26 05:48:29 +0000330 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000331 msg_cerr("%s failed during command execution\n", __func__);
snelson8913d082010-02-26 05:48:29 +0000332 return result;
333 }
334 /* Wait until the Write-In-Progress bit is cleared.
335 * This usually takes 1-85 s, so wait in 1 s steps.
336 */
Nico Huber4c8a9562017-10-15 11:20:58 +0200337
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000338 const int status = poll_delay ? spi_poll_wip(flash, poll_delay) : 0;
339
340 return result ? result : status;
341}
342
343static int spi_prepare_address(struct flashctx *const flash,
344 uint8_t cmd_buf[], const unsigned int addr)
345{
346 /* TODO: extend for 4BA */
347 cmd_buf[1] = (addr >> 16) & 0xff;
348 cmd_buf[2] = (addr >> 8) & 0xff;
349 cmd_buf[3] = (addr >> 0) & 0xff;
350 return 3;
351}
352
353/**
354 * Execute WREN plus another `op` that takes an address and
355 * optional data, poll WIP afterwards.
356 *
357 * @param flash the flash chip's context
358 * @param op the operation to execute
359 * @param addr the address parameter to `op`
360 * @param out_bytes bytes to send after the address,
361 * may be NULL if and only if `out_bytes` is 0
362 * @param out_bytes number of bytes to send, 256 at most, may be zero
363 * @param poll_delay interval in us for polling WIP
364 * @return 0 on success, non-zero otherwise
365 */
366static int spi_write_cmd(struct flashctx *const flash,
367 const uint8_t op, const unsigned int addr,
368 const uint8_t *const out_bytes, const size_t out_len,
369 const unsigned int poll_delay)
370{
371 uint8_t cmd[1 + JEDEC_MAX_ADDR_LEN + 256];
372 struct spi_command cmds[] = {
373 {
374 .writecnt = 1,
375 .writearr = (const unsigned char[]){ JEDEC_WREN },
376 }, {
377 .writearr = cmd,
378 },
379 NULL_SPI_CMD,
380 };
381
382 cmd[0] = op;
383 const int addr_len = spi_prepare_address(flash, cmd, addr);
384 if (addr_len < 0)
385 return 1;
386
387 if (1 + addr_len + out_len > sizeof(cmd)) {
388 msg_cerr("%s called for too long a write\n", __func__);
389 return 1;
390 }
391
392 memcpy(cmd + 1 + addr_len, out_bytes, out_len);
393 cmds[1].writecnt = 1 + addr_len + out_len;
394
395 const int result = spi_send_multicommand(flash, cmds);
396 if (result)
397 msg_cerr("%s failed during command execution at address 0x%x\n", __func__, addr);
398
399 const int status = spi_poll_wip(flash, poll_delay);
400
401 return result ? result : status;
Nico Huber4c8a9562017-10-15 11:20:58 +0200402}
403
404int spi_chip_erase_60(struct flashctx *flash)
405{
406 /* This usually takes 1-85s, so wait in 1s steps. */
407 return spi_simple_write_cmd(flash, 0x60, 1000 * 1000);
408}
409
410int spi_chip_erase_62(struct flashctx *flash)
411{
412 /* This usually takes 2-5s, so wait in 100ms steps. */
413 return spi_simple_write_cmd(flash, 0x62, 100 * 1000);
414}
415
416int spi_chip_erase_c7(struct flashctx *flash)
417{
418 /* This usually takes 1-85s, so wait in 1s steps. */
419 return spi_simple_write_cmd(flash, 0xc7, 1000 * 1000);
snelson8913d082010-02-26 05:48:29 +0000420}
421
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700422int spi_block_erase_52(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000423{
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000424 /* This usually takes 100-4000ms, so wait in 100ms steps. */
425 return spi_write_cmd(flash, 0x52, addr, NULL, 0, 100 * 1000);
426}
snelson8913d082010-02-26 05:48:29 +0000427
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000428/* Block size is usually
429 * 32M (one die) for Micron
430 */
431int spi_block_erase_c4(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
432{
433 /* This usually takes 240-480s, so wait in 500ms steps. */
434 return spi_write_cmd(flash, 0xc4, addr, NULL, 0, 500 * 1000);
snelson8913d082010-02-26 05:48:29 +0000435}
436
437/* Block size is usually
438 * 64k for Macronix
439 * 32k for SST
440 * 4-32k non-uniform for EON
441 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700442int spi_block_erase_d8(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000443{
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000444 /* This usually takes 100-4000ms, so wait in 100ms steps. */
445 return spi_write_cmd(flash, 0xd8, addr, NULL, 0, 100 * 1000);
snelson8913d082010-02-26 05:48:29 +0000446}
447
448/* Block size is usually
449 * 4k for PMC
450 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700451int spi_block_erase_d7(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000452{
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000453 /* This usually takes 100-4000ms, so wait in 100ms steps. */
454 return spi_write_cmd(flash, 0xd7, addr, NULL, 0, 100 * 1000);
455}
snelson8913d082010-02-26 05:48:29 +0000456
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000457/* Page erase (usually 256B blocks) */
458int spi_block_erase_db(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
459{
460 /* This takes up to 20ms usually (on worn out devices
461 up to the 0.5s range), so wait in 1ms steps. */
462 return spi_write_cmd(flash, 0xdb, addr, NULL, 0, 1 * 1000);
snelson8913d082010-02-26 05:48:29 +0000463}
464
snelson8913d082010-02-26 05:48:29 +0000465/* Sector size is usually 4k, though Macronix eliteflash has 64k */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700466int spi_block_erase_20(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000467{
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000468 /* This usually takes 15-800ms, so wait in 10ms steps. */
469 return spi_write_cmd(flash, 0x20, addr, NULL, 0, 10 * 1000);
470}
snelson8913d082010-02-26 05:48:29 +0000471
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000472int spi_block_erase_50(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
473{
474 /* This usually takes 10ms, so wait in 1ms steps. */
475 return spi_write_cmd(flash, 0x50, addr, NULL, 0, 1 * 1000);
476}
Stefan Reinauercce56d52010-11-22 18:22:21 -0800477
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000478int spi_block_erase_81(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
479{
480 /* This usually takes 8ms, so wait in 1ms steps. */
481 return spi_write_cmd(flash, 0x81, addr, NULL, 0, 1 * 1000);
snelson8913d082010-02-26 05:48:29 +0000482}
483
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700484int spi_block_erase_60(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000485{
Patrick Georgif3fa2992017-02-02 16:24:44 +0100486 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
snelsonfc007bb2010-03-24 23:14:32 +0000487 msg_cerr("%s called with incorrect arguments\n",
snelson8913d082010-02-26 05:48:29 +0000488 __func__);
489 return -1;
490 }
491 return spi_chip_erase_60(flash);
492}
493
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700494int spi_block_erase_c7(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000495{
Patrick Georgif3fa2992017-02-02 16:24:44 +0100496 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
snelsonfc007bb2010-03-24 23:14:32 +0000497 msg_cerr("%s called with incorrect arguments\n",
snelson8913d082010-02-26 05:48:29 +0000498 __func__);
499 return -1;
500 }
501 return spi_chip_erase_c7(flash);
502}
503
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700504int spi_write_status_register_wren(const struct flashctx *flash, int status)
hailfingerc33d4732010-07-29 13:09:18 +0000505{
506 int result;
hailfingeree9ee132010-10-08 00:37:55 +0000507 int i = 0;
hailfingerc33d4732010-07-29 13:09:18 +0000508 struct spi_command cmds[] = {
509 {
510 /* WRSR requires either EWSR or WREN depending on chip type. */
511 .writecnt = JEDEC_WREN_OUTSIZE,
512 .writearr = (const unsigned char[]){ JEDEC_WREN },
513 .readcnt = 0,
514 .readarr = NULL,
515 }, {
516 .writecnt = JEDEC_WRSR_OUTSIZE,
517 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
518 .readcnt = 0,
519 .readarr = NULL,
520 }, {
521 .writecnt = 0,
522 .writearr = NULL,
523 .readcnt = 0,
524 .readarr = NULL,
525 }};
526
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700527 result = spi_send_multicommand(flash, cmds);
hailfingerc33d4732010-07-29 13:09:18 +0000528 if (result) {
529 msg_cerr("%s failed during command execution\n",
530 __func__);
hailfingeree9ee132010-10-08 00:37:55 +0000531 /* No point in waiting for the command to complete if execution
532 * failed.
533 */
534 return result;
hailfingerc33d4732010-07-29 13:09:18 +0000535 }
hailfingeree9ee132010-10-08 00:37:55 +0000536 /* WRSR performs a self-timed erase before the changes take effect.
537 * This may take 50-85 ms in most cases, and some chips apparently
538 * allow running RDSR only once. Therefore pick an initial delay of
539 * 100 ms, then wait in 10 ms steps until a total of 5 s have elapsed.
540 */
hailfingerc33d4732010-07-29 13:09:18 +0000541 programmer_delay(100 * 1000);
Edward O'Callaghan8b5e4732019-03-05 15:27:53 +1100542 while (spi_read_status_register(flash) & SPI_SR_WIP) {
hailfingeree9ee132010-10-08 00:37:55 +0000543 if (++i > 490) {
544 msg_cerr("Error: WIP bit after WRSR never cleared\n");
545 return TIMEOUT_ERROR;
546 }
547 programmer_delay(10 * 1000);
548 }
549 return 0;
hailfingerc33d4732010-07-29 13:09:18 +0000550}
551
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700552int spi_byte_program(struct flashctx *flash, unsigned int addr, uint8_t databyte)
snelson8913d082010-02-26 05:48:29 +0000553{
554 int result;
555 struct spi_command cmds[] = {
556 {
557 .writecnt = JEDEC_WREN_OUTSIZE,
558 .writearr = (const unsigned char[]){ JEDEC_WREN },
559 .readcnt = 0,
560 .readarr = NULL,
561 }, {
562 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE,
563 .writearr = (const unsigned char[]){
564 JEDEC_BYTE_PROGRAM,
565 (addr >> 16) & 0xff,
566 (addr >> 8) & 0xff,
567 (addr & 0xff),
568 databyte
569 },
570 .readcnt = 0,
571 .readarr = NULL,
572 }, {
573 .writecnt = 0,
574 .writearr = NULL,
575 .readcnt = 0,
576 .readarr = NULL,
577 }};
578
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700579 result = spi_send_multicommand(flash, cmds);
snelson8913d082010-02-26 05:48:29 +0000580 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000581 msg_cerr("%s failed during command execution at address 0x%x\n",
snelson8913d082010-02-26 05:48:29 +0000582 __func__, addr);
583 }
584 return result;
585}
586
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000587static int spi_nbyte_program(struct flashctx *flash, unsigned int addr, const uint8_t *bytes, unsigned int len)
snelson8913d082010-02-26 05:48:29 +0000588{
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000589 return spi_write_cmd(flash, JEDEC_BYTE_PROGRAM, addr, bytes, len, 10);
snelson8913d082010-02-26 05:48:29 +0000590}
591
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700592int spi_nbyte_read(struct flashctx *flash, unsigned int address, uint8_t *bytes, unsigned int len)
snelson8913d082010-02-26 05:48:29 +0000593{
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000594 uint8_t cmd[1 + JEDEC_MAX_ADDR_LEN] = { JEDEC_READ, };
595
596 const int addr_len = spi_prepare_address(flash, cmd, address);
597 if (addr_len < 0)
598 return 1;
snelson8913d082010-02-26 05:48:29 +0000599
600 /* Send Read */
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000601 return spi_send_command(flash, 1 + addr_len, len, cmd, bytes);
snelson8913d082010-02-26 05:48:29 +0000602}
603
604/*
hailfinger39d159a2010-05-21 23:09:42 +0000605 * Read a part of the flash chip.
hailfingerc7d06c62010-07-14 16:19:05 +0000606 * FIXME: Use the chunk code from Michael Karcher instead.
snelson8913d082010-02-26 05:48:29 +0000607 * Each page is read separately in chunks with a maximum size of chunksize.
608 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700609int spi_read_chunked(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len, unsigned int chunksize)
snelson8913d082010-02-26 05:48:29 +0000610{
David Hendricks1ed1d352011-11-23 17:54:37 -0800611 int rc = 0, chunk_status = 0;
stefanctc5eb8a92011-11-23 09:13:48 +0000612 unsigned int i, j, starthere, lenhere, toread;
Patrick Georgif3fa2992017-02-02 16:24:44 +0100613 unsigned int page_size = flash->chip->page_size;
snelson8913d082010-02-26 05:48:29 +0000614
615 /* Warning: This loop has a very unusual condition and body.
616 * The loop needs to go through each page with at least one affected
617 * byte. The lowest page number is (start / page_size) since that
618 * division rounds down. The highest page number we want is the page
619 * where the last byte of the range lives. That last byte has the
620 * address (start + len - 1), thus the highest page number is
621 * (start + len - 1) / page_size. Since we want to include that last
622 * page as well, the loop condition uses <=.
623 */
624 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
625 /* Byte position of the first byte in the range in this page. */
626 /* starthere is an offset to the base address of the chip. */
627 starthere = max(start, i * page_size);
628 /* Length of bytes in the range in this page. */
629 lenhere = min(start + len, (i + 1) * page_size) - starthere;
630 for (j = 0; j < lenhere; j += chunksize) {
631 toread = min(chunksize, lenhere - j);
Boris Baykov1a2f5322016-06-11 18:29:00 +0200632 chunk_status = (flash->chip->feature_bits & FEATURE_4BA_SUPPORT) == 0
633 ? spi_nbyte_read(flash, starthere + j, buf + starthere - start + j, toread)
634 : flash->chip->four_bytes_addr_funcs.read_nbyte(flash, starthere + j,
635 buf + starthere - start + j, toread);
David Hendricks1ed1d352011-11-23 17:54:37 -0800636 if (chunk_status) {
637 if (ignore_error(chunk_status)) {
638 /* fill this chunk with 0xff bytes and
639 let caller know about the error */
640 memset(buf + starthere - start + j, 0xff, toread);
641 rc = chunk_status;
642 chunk_status = 0;
643 continue;
644 } else {
645 rc = chunk_status;
646 break;
647 }
648 }
snelson8913d082010-02-26 05:48:29 +0000649 }
David Hendricks1ed1d352011-11-23 17:54:37 -0800650 if (chunk_status)
snelson8913d082010-02-26 05:48:29 +0000651 break;
652 }
653
654 return rc;
655}
656
657/*
Duncan Laurie06ffd522015-10-26 12:56:08 -0700658 * Read a part of the flash chip.
659 * Ignore pages and read the data continuously, the only bound is the chunksize.
660 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700661int spi_read_unbound(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len, unsigned int chunksize)
Duncan Laurie06ffd522015-10-26 12:56:08 -0700662{
663 int rc = 0;
664 unsigned int i;
665
666 for (i = start; i < (start + len); i += chunksize) {
David Hendricks37370ec2015-11-24 14:38:17 -0800667 int chunk_status = 0;
668 unsigned int toread = min(chunksize, start + len - i);
669
Duncan Laurie20613a92018-10-10 08:43:12 -0700670 if (flash->chip->feature_bits & FEATURE_4BA_SUPPORT) {
671 chunk_status = flash->chip->four_bytes_addr_funcs.read_nbyte(
672 flash, i, buf + (i - start), toread);
673 } else {
674 chunk_status = spi_nbyte_read(flash, i, buf + (i - start), toread);
675 }
676
David Hendricks37370ec2015-11-24 14:38:17 -0800677 if (chunk_status) {
678 if (ignore_error(chunk_status)) {
679 /* fill this chunk with 0xff bytes and
680 let caller know about the error */
681 memset(buf + (i - start), 0xff, toread);
682 rc = chunk_status;
683 continue;
684 } else {
685 rc = chunk_status;
686 break;
687 }
688 }
Duncan Laurie06ffd522015-10-26 12:56:08 -0700689 }
David Hendricks37370ec2015-11-24 14:38:17 -0800690
Duncan Laurie06ffd522015-10-26 12:56:08 -0700691 return rc;
692}
693
694/*
hailfinger39d159a2010-05-21 23:09:42 +0000695 * Write a part of the flash chip.
hailfingerc7d06c62010-07-14 16:19:05 +0000696 * FIXME: Use the chunk code from Michael Karcher instead.
hailfinger39d159a2010-05-21 23:09:42 +0000697 * Each page is written separately in chunks with a maximum size of chunksize.
698 */
Patrick Georgiab8353e2017-02-03 18:32:01 +0100699int spi_write_chunked(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len, unsigned int chunksize)
hailfinger39d159a2010-05-21 23:09:42 +0000700{
701 int rc = 0;
stefanctc5eb8a92011-11-23 09:13:48 +0000702 unsigned int i, j, starthere, lenhere, towrite;
hailfinger39d159a2010-05-21 23:09:42 +0000703 /* FIXME: page_size is the wrong variable. We need max_writechunk_size
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700704 * in struct flashctx to do this properly. All chips using
hailfinger39d159a2010-05-21 23:09:42 +0000705 * spi_chip_write_256 have page_size set to max_writechunk_size, so
706 * we're OK for now.
707 */
Patrick Georgif3fa2992017-02-02 16:24:44 +0100708 unsigned int page_size = flash->chip->page_size;
hailfinger39d159a2010-05-21 23:09:42 +0000709
710 /* Warning: This loop has a very unusual condition and body.
711 * The loop needs to go through each page with at least one affected
712 * byte. The lowest page number is (start / page_size) since that
713 * division rounds down. The highest page number we want is the page
714 * where the last byte of the range lives. That last byte has the
715 * address (start + len - 1), thus the highest page number is
716 * (start + len - 1) / page_size. Since we want to include that last
717 * page as well, the loop condition uses <=.
718 */
719 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
720 /* Byte position of the first byte in the range in this page. */
721 /* starthere is an offset to the base address of the chip. */
722 starthere = max(start, i * page_size);
723 /* Length of bytes in the range in this page. */
724 lenhere = min(start + len, (i + 1) * page_size) - starthere;
725 for (j = 0; j < lenhere; j += chunksize) {
726 towrite = min(chunksize, lenhere - j);
Boris Baykov1a2f5322016-06-11 18:29:00 +0200727 rc = (flash->chip->feature_bits & FEATURE_4BA_SUPPORT) == 0
728 ? spi_nbyte_program(flash, starthere + j, buf + starthere - start + j, towrite)
729 : flash->chip->four_bytes_addr_funcs.program_nbyte(flash, starthere + j,
730 buf + starthere - start + j, towrite);
hailfinger39d159a2010-05-21 23:09:42 +0000731 if (rc)
732 break;
Edward O'Callaghan8b5e4732019-03-05 15:27:53 +1100733 while (spi_read_status_register(flash) & SPI_SR_WIP)
hailfinger39d159a2010-05-21 23:09:42 +0000734 programmer_delay(10);
735 }
736 if (rc)
737 break;
738 }
739
740 return rc;
741}
742
743/*
snelson8913d082010-02-26 05:48:29 +0000744 * Program chip using byte programming. (SLOW!)
745 * This is for chips which can only handle one byte writes
746 * and for chips where memory mapped programming is impossible
747 * (e.g. due to size constraints in IT87* for over 512 kB)
748 */
hailfingerc7d06c62010-07-14 16:19:05 +0000749/* real chunksize is 1, logical chunksize is 1 */
Patrick Georgiab8353e2017-02-03 18:32:01 +0100750int spi_chip_write_1(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
snelson8913d082010-02-26 05:48:29 +0000751{
stefanctc5eb8a92011-11-23 09:13:48 +0000752 unsigned int i;
753 int result = 0;
snelson8913d082010-02-26 05:48:29 +0000754
hailfingerc7d06c62010-07-14 16:19:05 +0000755 for (i = start; i < start + len; i++) {
Boris Baykov1a2f5322016-06-11 18:29:00 +0200756 result = (flash->chip->feature_bits & FEATURE_4BA_SUPPORT) == 0
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000757 ? spi_nbyte_program(flash, i, buf + i - start, 1)
Boris Baykov1a2f5322016-06-11 18:29:00 +0200758 : flash->chip->four_bytes_addr_funcs.program_byte(flash, i, buf[i - start]);
snelson8913d082010-02-26 05:48:29 +0000759 if (result)
760 return 1;
Edward O'Callaghan8b5e4732019-03-05 15:27:53 +1100761 while (spi_read_status_register(flash) & SPI_SR_WIP)
snelson8913d082010-02-26 05:48:29 +0000762 programmer_delay(10);
763 }
764
765 return 0;
766}
767
Patrick Georgiab8353e2017-02-03 18:32:01 +0100768int spi_aai_write(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
hailfingerc7d06c62010-07-14 16:19:05 +0000769{
770 uint32_t pos = start;
snelson8913d082010-02-26 05:48:29 +0000771 int result;
hailfinger19db0922010-06-20 10:41:35 +0000772 unsigned char cmd[JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE] = {
773 JEDEC_AAI_WORD_PROGRAM,
774 };
snelson8913d082010-02-26 05:48:29 +0000775
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100776 switch (spi_master->type) {
hailfinger90c7d542010-05-31 15:27:27 +0000777#if CONFIG_INTERNAL == 1
hailfinger324a9cc2010-05-26 01:45:41 +0000778#if defined(__i386__) || defined(__x86_64__)
hailfinger19db0922010-06-20 10:41:35 +0000779 case SPI_CONTROLLER_IT87XX:
snelson8913d082010-02-26 05:48:29 +0000780 case SPI_CONTROLLER_WBSIO:
hailfingerc7d06c62010-07-14 16:19:05 +0000781 msg_perr("%s: impossible with this SPI controller,"
snelson8913d082010-02-26 05:48:29 +0000782 " degrading to byte program\n", __func__);
hailfinger71e1bd42010-10-13 22:26:56 +0000783 return spi_chip_write_1(flash, buf, start, len);
snelson8913d082010-02-26 05:48:29 +0000784#endif
hailfinger324a9cc2010-05-26 01:45:41 +0000785#endif
snelson8913d082010-02-26 05:48:29 +0000786 default:
787 break;
788 }
hailfinger19db0922010-06-20 10:41:35 +0000789
hailfingerc7d06c62010-07-14 16:19:05 +0000790 /* The even start address and even length requirements can be either
791 * honored outside this function, or we can call spi_byte_program
792 * for the first and/or last byte and use AAI for the rest.
hailfinger71e1bd42010-10-13 22:26:56 +0000793 * FIXME: Move this to generic code.
hailfingerc7d06c62010-07-14 16:19:05 +0000794 */
hailfinger19db0922010-06-20 10:41:35 +0000795 /* The data sheet requires a start address with the low bit cleared. */
hailfingerc7d06c62010-07-14 16:19:05 +0000796 if (start % 2) {
hailfinger19db0922010-06-20 10:41:35 +0000797 msg_cerr("%s: start address not even! Please report a bug at "
798 "flashrom@flashrom.org\n", __func__);
hailfinger71e1bd42010-10-13 22:26:56 +0000799 if (spi_chip_write_1(flash, buf, start, start % 2))
800 return SPI_GENERIC_ERROR;
801 pos += start % 2;
802 /* Do not return an error for now. */
803 //return SPI_GENERIC_ERROR;
hailfinger19db0922010-06-20 10:41:35 +0000804 }
805 /* The data sheet requires total AAI write length to be even. */
806 if (len % 2) {
807 msg_cerr("%s: total write length not even! Please report a "
808 "bug at flashrom@flashrom.org\n", __func__);
hailfinger71e1bd42010-10-13 22:26:56 +0000809 /* Do not return an error for now. */
810 //return SPI_GENERIC_ERROR;
hailfinger19db0922010-06-20 10:41:35 +0000811 }
812
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000813 result = spi_write_cmd(flash, JEDEC_AAI_WORD_PROGRAM, start, buf + pos - start, 2, 10);
814 if (result)
Edward O'Callaghan633cbd62019-06-17 15:43:56 +1000815 goto bailout;
hailfinger19db0922010-06-20 10:41:35 +0000816
817 /* We already wrote 2 bytes in the multicommand step. */
818 pos += 2;
819
hailfinger71e1bd42010-10-13 22:26:56 +0000820 /* Are there at least two more bytes to write? */
821 while (pos < start + len - 1) {
hailfingerdef852d2010-10-27 22:07:11 +0000822 cmd[1] = buf[pos++ - start];
823 cmd[2] = buf[pos++ - start];
Edward O'Callaghan633cbd62019-06-17 15:43:56 +1000824 result = spi_send_command(flash, JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE, 0, cmd, NULL);
825 if (result) {
826 msg_cerr("%s failed during followup AAI command execution: %d\n", __func__, result);
827 goto bailout;
828 }
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000829 if (spi_poll_wip(flash, 10))
830 goto bailout;
hailfinger19db0922010-06-20 10:41:35 +0000831 }
832
hailfinger71e1bd42010-10-13 22:26:56 +0000833 /* Use WRDI to exit AAI mode. This needs to be done before issuing any
834 * other non-AAI command.
835 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700836 spi_write_disable(flash);
hailfinger71e1bd42010-10-13 22:26:56 +0000837
838 /* Write remaining byte (if any). */
839 if (pos < start + len) {
hailfingerdef852d2010-10-27 22:07:11 +0000840 if (spi_chip_write_1(flash, buf + pos - start, pos, pos % 2))
hailfinger71e1bd42010-10-13 22:26:56 +0000841 return SPI_GENERIC_ERROR;
842 pos += pos % 2;
843 }
844
snelson8913d082010-02-26 05:48:29 +0000845 return 0;
Edward O'Callaghan633cbd62019-06-17 15:43:56 +1000846
847bailout:
848 spi_write_disable(flash);
849 return SPI_GENERIC_ERROR;
snelson8913d082010-02-26 05:48:29 +0000850}