blob: bc54c6d95892bb0d5ab763b2a296917777ce0311 [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
Edward O'Callaghana74ffcd2019-06-17 14:59:55 +1000343static int spi_set_extended_address(struct flashctx *const flash, const uint8_t addr_high)
344{
345 if (flash->address_high_byte != addr_high &&
346 spi_write_extended_address_register(flash, addr_high))
347 return -1;
348 flash->address_high_byte = addr_high;
349 return 0;
350}
351
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000352static int spi_prepare_address(struct flashctx *const flash,
353 uint8_t cmd_buf[], const unsigned int addr)
354{
Edward O'Callaghana74ffcd2019-06-17 14:59:55 +1000355 if (flash->in_4ba_mode) {
356 cmd_buf[1] = (addr >> 24) & 0xff;
357 cmd_buf[2] = (addr >> 16) & 0xff;
358 cmd_buf[3] = (addr >> 8) & 0xff;
359 cmd_buf[4] = (addr >> 0) & 0xff;
360 return 4;
361 } else {
362 if (flash->chip->feature_bits & FEATURE_4BA_EXT_ADDR) {
363 if (spi_set_extended_address(flash, addr >> 24))
364 return -1;
365 } else {
366 if (addr >> 24)
367 return -1;
368 }
369 cmd_buf[1] = (addr >> 16) & 0xff;
370 cmd_buf[2] = (addr >> 8) & 0xff;
371 cmd_buf[3] = (addr >> 0) & 0xff;
372 return 3;
373 }
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000374}
375
376/**
377 * Execute WREN plus another `op` that takes an address and
378 * optional data, poll WIP afterwards.
379 *
380 * @param flash the flash chip's context
381 * @param op the operation to execute
382 * @param addr the address parameter to `op`
383 * @param out_bytes bytes to send after the address,
384 * may be NULL if and only if `out_bytes` is 0
385 * @param out_bytes number of bytes to send, 256 at most, may be zero
386 * @param poll_delay interval in us for polling WIP
387 * @return 0 on success, non-zero otherwise
388 */
389static int spi_write_cmd(struct flashctx *const flash,
390 const uint8_t op, const unsigned int addr,
391 const uint8_t *const out_bytes, const size_t out_len,
392 const unsigned int poll_delay)
393{
394 uint8_t cmd[1 + JEDEC_MAX_ADDR_LEN + 256];
395 struct spi_command cmds[] = {
396 {
397 .writecnt = 1,
398 .writearr = (const unsigned char[]){ JEDEC_WREN },
399 }, {
400 .writearr = cmd,
401 },
402 NULL_SPI_CMD,
403 };
404
405 cmd[0] = op;
406 const int addr_len = spi_prepare_address(flash, cmd, addr);
407 if (addr_len < 0)
408 return 1;
409
410 if (1 + addr_len + out_len > sizeof(cmd)) {
411 msg_cerr("%s called for too long a write\n", __func__);
412 return 1;
413 }
414
415 memcpy(cmd + 1 + addr_len, out_bytes, out_len);
416 cmds[1].writecnt = 1 + addr_len + out_len;
417
418 const int result = spi_send_multicommand(flash, cmds);
419 if (result)
420 msg_cerr("%s failed during command execution at address 0x%x\n", __func__, addr);
421
422 const int status = spi_poll_wip(flash, poll_delay);
423
424 return result ? result : status;
Nico Huber4c8a9562017-10-15 11:20:58 +0200425}
426
427int spi_chip_erase_60(struct flashctx *flash)
428{
429 /* This usually takes 1-85s, so wait in 1s steps. */
430 return spi_simple_write_cmd(flash, 0x60, 1000 * 1000);
431}
432
433int spi_chip_erase_62(struct flashctx *flash)
434{
435 /* This usually takes 2-5s, so wait in 100ms steps. */
436 return spi_simple_write_cmd(flash, 0x62, 100 * 1000);
437}
438
439int spi_chip_erase_c7(struct flashctx *flash)
440{
441 /* This usually takes 1-85s, so wait in 1s steps. */
442 return spi_simple_write_cmd(flash, 0xc7, 1000 * 1000);
snelson8913d082010-02-26 05:48:29 +0000443}
444
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700445int spi_block_erase_52(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000446{
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000447 /* This usually takes 100-4000ms, so wait in 100ms steps. */
448 return spi_write_cmd(flash, 0x52, addr, NULL, 0, 100 * 1000);
449}
snelson8913d082010-02-26 05:48:29 +0000450
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000451/* Block size is usually
452 * 32M (one die) for Micron
453 */
454int spi_block_erase_c4(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
455{
456 /* This usually takes 240-480s, so wait in 500ms steps. */
457 return spi_write_cmd(flash, 0xc4, addr, NULL, 0, 500 * 1000);
snelson8913d082010-02-26 05:48:29 +0000458}
459
460/* Block size is usually
461 * 64k for Macronix
462 * 32k for SST
463 * 4-32k non-uniform for EON
464 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700465int spi_block_erase_d8(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000466{
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000467 /* This usually takes 100-4000ms, so wait in 100ms steps. */
468 return spi_write_cmd(flash, 0xd8, addr, NULL, 0, 100 * 1000);
snelson8913d082010-02-26 05:48:29 +0000469}
470
471/* Block size is usually
472 * 4k for PMC
473 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700474int spi_block_erase_d7(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000475{
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000476 /* This usually takes 100-4000ms, so wait in 100ms steps. */
477 return spi_write_cmd(flash, 0xd7, addr, NULL, 0, 100 * 1000);
478}
snelson8913d082010-02-26 05:48:29 +0000479
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000480/* Page erase (usually 256B blocks) */
481int spi_block_erase_db(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
482{
483 /* This takes up to 20ms usually (on worn out devices
484 up to the 0.5s range), so wait in 1ms steps. */
485 return spi_write_cmd(flash, 0xdb, addr, NULL, 0, 1 * 1000);
snelson8913d082010-02-26 05:48:29 +0000486}
487
snelson8913d082010-02-26 05:48:29 +0000488/* Sector size is usually 4k, though Macronix eliteflash has 64k */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700489int spi_block_erase_20(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000490{
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000491 /* This usually takes 15-800ms, so wait in 10ms steps. */
492 return spi_write_cmd(flash, 0x20, addr, NULL, 0, 10 * 1000);
493}
snelson8913d082010-02-26 05:48:29 +0000494
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000495int spi_block_erase_50(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
496{
497 /* This usually takes 10ms, so wait in 1ms steps. */
498 return spi_write_cmd(flash, 0x50, addr, NULL, 0, 1 * 1000);
499}
Stefan Reinauercce56d52010-11-22 18:22:21 -0800500
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000501int spi_block_erase_81(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
502{
503 /* This usually takes 8ms, so wait in 1ms steps. */
504 return spi_write_cmd(flash, 0x81, addr, NULL, 0, 1 * 1000);
snelson8913d082010-02-26 05:48:29 +0000505}
506
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700507int spi_block_erase_60(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000508{
Patrick Georgif3fa2992017-02-02 16:24:44 +0100509 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
snelsonfc007bb2010-03-24 23:14:32 +0000510 msg_cerr("%s called with incorrect arguments\n",
snelson8913d082010-02-26 05:48:29 +0000511 __func__);
512 return -1;
513 }
514 return spi_chip_erase_60(flash);
515}
516
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700517int spi_block_erase_c7(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000518{
Patrick Georgif3fa2992017-02-02 16:24:44 +0100519 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
snelsonfc007bb2010-03-24 23:14:32 +0000520 msg_cerr("%s called with incorrect arguments\n",
snelson8913d082010-02-26 05:48:29 +0000521 __func__);
522 return -1;
523 }
524 return spi_chip_erase_c7(flash);
525}
526
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700527int spi_write_status_register_wren(const struct flashctx *flash, int status)
hailfingerc33d4732010-07-29 13:09:18 +0000528{
529 int result;
hailfingeree9ee132010-10-08 00:37:55 +0000530 int i = 0;
hailfingerc33d4732010-07-29 13:09:18 +0000531 struct spi_command cmds[] = {
532 {
533 /* WRSR requires either EWSR or WREN depending on chip type. */
534 .writecnt = JEDEC_WREN_OUTSIZE,
535 .writearr = (const unsigned char[]){ JEDEC_WREN },
536 .readcnt = 0,
537 .readarr = NULL,
538 }, {
539 .writecnt = JEDEC_WRSR_OUTSIZE,
540 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
541 .readcnt = 0,
542 .readarr = NULL,
543 }, {
544 .writecnt = 0,
545 .writearr = NULL,
546 .readcnt = 0,
547 .readarr = NULL,
548 }};
549
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700550 result = spi_send_multicommand(flash, cmds);
hailfingerc33d4732010-07-29 13:09:18 +0000551 if (result) {
552 msg_cerr("%s failed during command execution\n",
553 __func__);
hailfingeree9ee132010-10-08 00:37:55 +0000554 /* No point in waiting for the command to complete if execution
555 * failed.
556 */
557 return result;
hailfingerc33d4732010-07-29 13:09:18 +0000558 }
hailfingeree9ee132010-10-08 00:37:55 +0000559 /* WRSR performs a self-timed erase before the changes take effect.
560 * This may take 50-85 ms in most cases, and some chips apparently
561 * allow running RDSR only once. Therefore pick an initial delay of
562 * 100 ms, then wait in 10 ms steps until a total of 5 s have elapsed.
563 */
hailfingerc33d4732010-07-29 13:09:18 +0000564 programmer_delay(100 * 1000);
Edward O'Callaghan8b5e4732019-03-05 15:27:53 +1100565 while (spi_read_status_register(flash) & SPI_SR_WIP) {
hailfingeree9ee132010-10-08 00:37:55 +0000566 if (++i > 490) {
567 msg_cerr("Error: WIP bit after WRSR never cleared\n");
568 return TIMEOUT_ERROR;
569 }
570 programmer_delay(10 * 1000);
571 }
572 return 0;
hailfingerc33d4732010-07-29 13:09:18 +0000573}
574
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700575int spi_byte_program(struct flashctx *flash, unsigned int addr, uint8_t databyte)
snelson8913d082010-02-26 05:48:29 +0000576{
577 int result;
578 struct spi_command cmds[] = {
579 {
580 .writecnt = JEDEC_WREN_OUTSIZE,
581 .writearr = (const unsigned char[]){ JEDEC_WREN },
582 .readcnt = 0,
583 .readarr = NULL,
584 }, {
585 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE,
586 .writearr = (const unsigned char[]){
587 JEDEC_BYTE_PROGRAM,
588 (addr >> 16) & 0xff,
589 (addr >> 8) & 0xff,
590 (addr & 0xff),
591 databyte
592 },
593 .readcnt = 0,
594 .readarr = NULL,
595 }, {
596 .writecnt = 0,
597 .writearr = NULL,
598 .readcnt = 0,
599 .readarr = NULL,
600 }};
601
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700602 result = spi_send_multicommand(flash, cmds);
snelson8913d082010-02-26 05:48:29 +0000603 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000604 msg_cerr("%s failed during command execution at address 0x%x\n",
snelson8913d082010-02-26 05:48:29 +0000605 __func__, addr);
606 }
607 return result;
608}
609
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000610static int spi_nbyte_program(struct flashctx *flash, unsigned int addr, const uint8_t *bytes, unsigned int len)
snelson8913d082010-02-26 05:48:29 +0000611{
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000612 return spi_write_cmd(flash, JEDEC_BYTE_PROGRAM, addr, bytes, len, 10);
snelson8913d082010-02-26 05:48:29 +0000613}
614
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700615int spi_nbyte_read(struct flashctx *flash, unsigned int address, uint8_t *bytes, unsigned int len)
snelson8913d082010-02-26 05:48:29 +0000616{
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000617 uint8_t cmd[1 + JEDEC_MAX_ADDR_LEN] = { JEDEC_READ, };
618
619 const int addr_len = spi_prepare_address(flash, cmd, address);
620 if (addr_len < 0)
621 return 1;
snelson8913d082010-02-26 05:48:29 +0000622
623 /* Send Read */
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000624 return spi_send_command(flash, 1 + addr_len, len, cmd, bytes);
snelson8913d082010-02-26 05:48:29 +0000625}
626
627/*
hailfinger39d159a2010-05-21 23:09:42 +0000628 * Read a part of the flash chip.
hailfingerc7d06c62010-07-14 16:19:05 +0000629 * FIXME: Use the chunk code from Michael Karcher instead.
snelson8913d082010-02-26 05:48:29 +0000630 * Each page is read separately in chunks with a maximum size of chunksize.
631 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700632int spi_read_chunked(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len, unsigned int chunksize)
snelson8913d082010-02-26 05:48:29 +0000633{
David Hendricks1ed1d352011-11-23 17:54:37 -0800634 int rc = 0, chunk_status = 0;
stefanctc5eb8a92011-11-23 09:13:48 +0000635 unsigned int i, j, starthere, lenhere, toread;
Patrick Georgif3fa2992017-02-02 16:24:44 +0100636 unsigned int page_size = flash->chip->page_size;
snelson8913d082010-02-26 05:48:29 +0000637
638 /* Warning: This loop has a very unusual condition and body.
639 * The loop needs to go through each page with at least one affected
640 * byte. The lowest page number is (start / page_size) since that
641 * division rounds down. The highest page number we want is the page
642 * where the last byte of the range lives. That last byte has the
643 * address (start + len - 1), thus the highest page number is
644 * (start + len - 1) / page_size. Since we want to include that last
645 * page as well, the loop condition uses <=.
646 */
647 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
648 /* Byte position of the first byte in the range in this page. */
649 /* starthere is an offset to the base address of the chip. */
650 starthere = max(start, i * page_size);
651 /* Length of bytes in the range in this page. */
652 lenhere = min(start + len, (i + 1) * page_size) - starthere;
653 for (j = 0; j < lenhere; j += chunksize) {
654 toread = min(chunksize, lenhere - j);
Boris Baykov1a2f5322016-06-11 18:29:00 +0200655 chunk_status = (flash->chip->feature_bits & FEATURE_4BA_SUPPORT) == 0
656 ? spi_nbyte_read(flash, starthere + j, buf + starthere - start + j, toread)
657 : flash->chip->four_bytes_addr_funcs.read_nbyte(flash, starthere + j,
658 buf + starthere - start + j, toread);
David Hendricks1ed1d352011-11-23 17:54:37 -0800659 if (chunk_status) {
660 if (ignore_error(chunk_status)) {
661 /* fill this chunk with 0xff bytes and
662 let caller know about the error */
663 memset(buf + starthere - start + j, 0xff, toread);
664 rc = chunk_status;
665 chunk_status = 0;
666 continue;
667 } else {
668 rc = chunk_status;
669 break;
670 }
671 }
snelson8913d082010-02-26 05:48:29 +0000672 }
David Hendricks1ed1d352011-11-23 17:54:37 -0800673 if (chunk_status)
snelson8913d082010-02-26 05:48:29 +0000674 break;
675 }
676
677 return rc;
678}
679
680/*
Duncan Laurie06ffd522015-10-26 12:56:08 -0700681 * Read a part of the flash chip.
682 * Ignore pages and read the data continuously, the only bound is the chunksize.
683 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700684int 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 -0700685{
686 int rc = 0;
687 unsigned int i;
688
689 for (i = start; i < (start + len); i += chunksize) {
David Hendricks37370ec2015-11-24 14:38:17 -0800690 int chunk_status = 0;
691 unsigned int toread = min(chunksize, start + len - i);
692
Duncan Laurie20613a92018-10-10 08:43:12 -0700693 if (flash->chip->feature_bits & FEATURE_4BA_SUPPORT) {
694 chunk_status = flash->chip->four_bytes_addr_funcs.read_nbyte(
695 flash, i, buf + (i - start), toread);
696 } else {
697 chunk_status = spi_nbyte_read(flash, i, buf + (i - start), toread);
698 }
699
David Hendricks37370ec2015-11-24 14:38:17 -0800700 if (chunk_status) {
701 if (ignore_error(chunk_status)) {
702 /* fill this chunk with 0xff bytes and
703 let caller know about the error */
704 memset(buf + (i - start), 0xff, toread);
705 rc = chunk_status;
706 continue;
707 } else {
708 rc = chunk_status;
709 break;
710 }
711 }
Duncan Laurie06ffd522015-10-26 12:56:08 -0700712 }
David Hendricks37370ec2015-11-24 14:38:17 -0800713
Duncan Laurie06ffd522015-10-26 12:56:08 -0700714 return rc;
715}
716
717/*
hailfinger39d159a2010-05-21 23:09:42 +0000718 * Write a part of the flash chip.
hailfingerc7d06c62010-07-14 16:19:05 +0000719 * FIXME: Use the chunk code from Michael Karcher instead.
hailfinger39d159a2010-05-21 23:09:42 +0000720 * Each page is written separately in chunks with a maximum size of chunksize.
721 */
Patrick Georgiab8353e2017-02-03 18:32:01 +0100722int 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 +0000723{
724 int rc = 0;
stefanctc5eb8a92011-11-23 09:13:48 +0000725 unsigned int i, j, starthere, lenhere, towrite;
hailfinger39d159a2010-05-21 23:09:42 +0000726 /* FIXME: page_size is the wrong variable. We need max_writechunk_size
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700727 * in struct flashctx to do this properly. All chips using
hailfinger39d159a2010-05-21 23:09:42 +0000728 * spi_chip_write_256 have page_size set to max_writechunk_size, so
729 * we're OK for now.
730 */
Patrick Georgif3fa2992017-02-02 16:24:44 +0100731 unsigned int page_size = flash->chip->page_size;
hailfinger39d159a2010-05-21 23:09:42 +0000732
733 /* Warning: This loop has a very unusual condition and body.
734 * The loop needs to go through each page with at least one affected
735 * byte. The lowest page number is (start / page_size) since that
736 * division rounds down. The highest page number we want is the page
737 * where the last byte of the range lives. That last byte has the
738 * address (start + len - 1), thus the highest page number is
739 * (start + len - 1) / page_size. Since we want to include that last
740 * page as well, the loop condition uses <=.
741 */
742 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
743 /* Byte position of the first byte in the range in this page. */
744 /* starthere is an offset to the base address of the chip. */
745 starthere = max(start, i * page_size);
746 /* Length of bytes in the range in this page. */
747 lenhere = min(start + len, (i + 1) * page_size) - starthere;
748 for (j = 0; j < lenhere; j += chunksize) {
749 towrite = min(chunksize, lenhere - j);
Boris Baykov1a2f5322016-06-11 18:29:00 +0200750 rc = (flash->chip->feature_bits & FEATURE_4BA_SUPPORT) == 0
751 ? spi_nbyte_program(flash, starthere + j, buf + starthere - start + j, towrite)
752 : flash->chip->four_bytes_addr_funcs.program_nbyte(flash, starthere + j,
753 buf + starthere - start + j, towrite);
hailfinger39d159a2010-05-21 23:09:42 +0000754 if (rc)
755 break;
Edward O'Callaghan8b5e4732019-03-05 15:27:53 +1100756 while (spi_read_status_register(flash) & SPI_SR_WIP)
hailfinger39d159a2010-05-21 23:09:42 +0000757 programmer_delay(10);
758 }
759 if (rc)
760 break;
761 }
762
763 return rc;
764}
765
766/*
snelson8913d082010-02-26 05:48:29 +0000767 * Program chip using byte programming. (SLOW!)
768 * This is for chips which can only handle one byte writes
769 * and for chips where memory mapped programming is impossible
770 * (e.g. due to size constraints in IT87* for over 512 kB)
771 */
hailfingerc7d06c62010-07-14 16:19:05 +0000772/* real chunksize is 1, logical chunksize is 1 */
Patrick Georgiab8353e2017-02-03 18:32:01 +0100773int spi_chip_write_1(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
snelson8913d082010-02-26 05:48:29 +0000774{
stefanctc5eb8a92011-11-23 09:13:48 +0000775 unsigned int i;
776 int result = 0;
snelson8913d082010-02-26 05:48:29 +0000777
hailfingerc7d06c62010-07-14 16:19:05 +0000778 for (i = start; i < start + len; i++) {
Boris Baykov1a2f5322016-06-11 18:29:00 +0200779 result = (flash->chip->feature_bits & FEATURE_4BA_SUPPORT) == 0
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000780 ? spi_nbyte_program(flash, i, buf + i - start, 1)
Boris Baykov1a2f5322016-06-11 18:29:00 +0200781 : flash->chip->four_bytes_addr_funcs.program_byte(flash, i, buf[i - start]);
snelson8913d082010-02-26 05:48:29 +0000782 if (result)
783 return 1;
Edward O'Callaghan8b5e4732019-03-05 15:27:53 +1100784 while (spi_read_status_register(flash) & SPI_SR_WIP)
snelson8913d082010-02-26 05:48:29 +0000785 programmer_delay(10);
786 }
787
788 return 0;
789}
790
Patrick Georgiab8353e2017-02-03 18:32:01 +0100791int spi_aai_write(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
hailfingerc7d06c62010-07-14 16:19:05 +0000792{
793 uint32_t pos = start;
snelson8913d082010-02-26 05:48:29 +0000794 int result;
hailfinger19db0922010-06-20 10:41:35 +0000795 unsigned char cmd[JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE] = {
796 JEDEC_AAI_WORD_PROGRAM,
797 };
snelson8913d082010-02-26 05:48:29 +0000798
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100799 switch (spi_master->type) {
hailfinger90c7d542010-05-31 15:27:27 +0000800#if CONFIG_INTERNAL == 1
hailfinger324a9cc2010-05-26 01:45:41 +0000801#if defined(__i386__) || defined(__x86_64__)
hailfinger19db0922010-06-20 10:41:35 +0000802 case SPI_CONTROLLER_IT87XX:
snelson8913d082010-02-26 05:48:29 +0000803 case SPI_CONTROLLER_WBSIO:
hailfingerc7d06c62010-07-14 16:19:05 +0000804 msg_perr("%s: impossible with this SPI controller,"
snelson8913d082010-02-26 05:48:29 +0000805 " degrading to byte program\n", __func__);
hailfinger71e1bd42010-10-13 22:26:56 +0000806 return spi_chip_write_1(flash, buf, start, len);
snelson8913d082010-02-26 05:48:29 +0000807#endif
hailfinger324a9cc2010-05-26 01:45:41 +0000808#endif
snelson8913d082010-02-26 05:48:29 +0000809 default:
810 break;
811 }
hailfinger19db0922010-06-20 10:41:35 +0000812
hailfingerc7d06c62010-07-14 16:19:05 +0000813 /* The even start address and even length requirements can be either
814 * honored outside this function, or we can call spi_byte_program
815 * for the first and/or last byte and use AAI for the rest.
hailfinger71e1bd42010-10-13 22:26:56 +0000816 * FIXME: Move this to generic code.
hailfingerc7d06c62010-07-14 16:19:05 +0000817 */
hailfinger19db0922010-06-20 10:41:35 +0000818 /* The data sheet requires a start address with the low bit cleared. */
hailfingerc7d06c62010-07-14 16:19:05 +0000819 if (start % 2) {
hailfinger19db0922010-06-20 10:41:35 +0000820 msg_cerr("%s: start address not even! Please report a bug at "
821 "flashrom@flashrom.org\n", __func__);
hailfinger71e1bd42010-10-13 22:26:56 +0000822 if (spi_chip_write_1(flash, buf, start, start % 2))
823 return SPI_GENERIC_ERROR;
824 pos += start % 2;
825 /* Do not return an error for now. */
826 //return SPI_GENERIC_ERROR;
hailfinger19db0922010-06-20 10:41:35 +0000827 }
828 /* The data sheet requires total AAI write length to be even. */
829 if (len % 2) {
830 msg_cerr("%s: total write length not even! Please report a "
831 "bug at flashrom@flashrom.org\n", __func__);
hailfinger71e1bd42010-10-13 22:26:56 +0000832 /* Do not return an error for now. */
833 //return SPI_GENERIC_ERROR;
hailfinger19db0922010-06-20 10:41:35 +0000834 }
835
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000836 result = spi_write_cmd(flash, JEDEC_AAI_WORD_PROGRAM, start, buf + pos - start, 2, 10);
837 if (result)
Edward O'Callaghan633cbd62019-06-17 15:43:56 +1000838 goto bailout;
hailfinger19db0922010-06-20 10:41:35 +0000839
840 /* We already wrote 2 bytes in the multicommand step. */
841 pos += 2;
842
hailfinger71e1bd42010-10-13 22:26:56 +0000843 /* Are there at least two more bytes to write? */
844 while (pos < start + len - 1) {
hailfingerdef852d2010-10-27 22:07:11 +0000845 cmd[1] = buf[pos++ - start];
846 cmd[2] = buf[pos++ - start];
Edward O'Callaghan633cbd62019-06-17 15:43:56 +1000847 result = spi_send_command(flash, JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE, 0, cmd, NULL);
848 if (result) {
849 msg_cerr("%s failed during followup AAI command execution: %d\n", __func__, result);
850 goto bailout;
851 }
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000852 if (spi_poll_wip(flash, 10))
853 goto bailout;
hailfinger19db0922010-06-20 10:41:35 +0000854 }
855
hailfinger71e1bd42010-10-13 22:26:56 +0000856 /* Use WRDI to exit AAI mode. This needs to be done before issuing any
857 * other non-AAI command.
858 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700859 spi_write_disable(flash);
hailfinger71e1bd42010-10-13 22:26:56 +0000860
861 /* Write remaining byte (if any). */
862 if (pos < start + len) {
hailfingerdef852d2010-10-27 22:07:11 +0000863 if (spi_chip_write_1(flash, buf + pos - start, pos, pos % 2))
hailfinger71e1bd42010-10-13 22:26:56 +0000864 return SPI_GENERIC_ERROR;
865 pos += pos % 2;
866 }
867
snelson8913d082010-02-26 05:48:29 +0000868 return 0;
Edward O'Callaghan633cbd62019-06-17 15:43:56 +1000869
870bailout:
871 spi_write_disable(flash);
872 return SPI_GENERIC_ERROR;
snelson8913d082010-02-26 05:48:29 +0000873}