blob: 4232d5ea64e627f396d92a9d2b67c59b8c99f9cb [file] [log] [blame]
snelson8913d082010-02-26 05:48:29 +00001/*
2 * This file is part of the flashrom project.
3 *
hailfinger39d159a2010-05-21 23:09:42 +00004 * Copyright (C) 2007, 2008, 2009, 2010 Carl-Daniel Hailfinger
snelson8913d082010-02-26 05:48:29 +00005 * Copyright (C) 2008 coresystems GmbH
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
snelson8913d082010-02-26 05:48:29 +000015 */
16
17/*
18 * Contains the common SPI chip driver functions
19 */
20
Nico Huber4c8a9562017-10-15 11:20:58 +020021#include <stddef.h>
snelson8913d082010-02-26 05:48:29 +000022#include <string.h>
Edward O'Callaghan031831d2019-06-19 16:27:43 +100023#include <stdbool.h>
snelson8913d082010-02-26 05:48:29 +000024#include "flash.h"
25#include "flashchips.h"
26#include "chipdrivers.h"
hailfinger428f6852010-07-27 22:41:39 +000027#include "programmer.h"
snelson8913d082010-02-26 05:48:29 +000028#include "spi.h"
Boris Baykov1a2f5322016-06-11 18:29:00 +020029#include "spi4ba.h"
snelson8913d082010-02-26 05:48:29 +000030
David Hendricks57b75242015-11-20 15:54:07 -080031enum id_type {
32 RDID,
33 RDID4,
34 REMS,
35// RES1, /* TODO */
36 RES2,
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{
Edward O'Callaghandfb71542020-05-14 18:41:42 +100069 static const unsigned char cmd[JEDEC_REMS_OUTSIZE] = { JEDEC_REMS, };
snelson8913d082010-02-26 05:48:29 +000070 int ret;
71
Souvik Ghoshd75cd672016-06-17 14:21:39 -070072 ret = spi_send_command(flash, sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
snelson8913d082010-02-26 05:48:29 +000073 if (ret)
74 return ret;
stefanct371e7e82011-07-07 19:56:58 +000075 msg_cspew("REMS returned 0x%02x 0x%02x. ", readarr[0], readarr[1]);
snelson8913d082010-02-26 05:48:29 +000076 return 0;
77}
78
Souvik Ghoshd75cd672016-06-17 14:21:39 -070079static int spi_res(struct flashctx *flash, unsigned char *readarr, int bytes)
snelson8913d082010-02-26 05:48:29 +000080{
Edward O'Callaghandfb71542020-05-14 18:41:42 +100081 static const unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, };
snelson8913d082010-02-26 05:48:29 +000082 int ret;
hailfingercb0564e2010-06-20 10:39:33 +000083 int i;
snelson8913d082010-02-26 05:48:29 +000084
Souvik Ghoshd75cd672016-06-17 14:21:39 -070085 ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr);
snelson8913d082010-02-26 05:48:29 +000086 if (ret)
87 return ret;
hailfingercb0564e2010-06-20 10:39:33 +000088 msg_cspew("RES returned");
89 for (i = 0; i < bytes; i++)
90 msg_cspew(" 0x%02x", readarr[i]);
91 msg_cspew(". ");
snelson8913d082010-02-26 05:48:29 +000092 return 0;
93}
94
Souvik Ghoshd75cd672016-06-17 14:21:39 -070095int spi_write_enable(struct flashctx *flash)
snelson8913d082010-02-26 05:48:29 +000096{
krause2eb76212011-01-17 07:50:42 +000097 static const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN };
snelson8913d082010-02-26 05:48:29 +000098 int result;
99
100 /* Send WREN (Write Enable) */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700101 result = spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
snelson8913d082010-02-26 05:48:29 +0000102
103 if (result)
snelsonfc007bb2010-03-24 23:14:32 +0000104 msg_cerr("%s failed\n", __func__);
snelson8913d082010-02-26 05:48:29 +0000105
106 return result;
107}
108
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700109int spi_write_disable(struct flashctx *flash)
snelson8913d082010-02-26 05:48:29 +0000110{
krause2eb76212011-01-17 07:50:42 +0000111 static const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI };
snelson8913d082010-02-26 05:48:29 +0000112
113 /* Send WRDI (Write Disable) */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700114 return spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
snelson8913d082010-02-26 05:48:29 +0000115}
116
David Hendricks7f7c7112012-10-11 17:15:48 -0700117static void rdid_get_ids(unsigned char *readarr,
118 int bytes, uint32_t *id1, uint32_t *id2)
snelson8913d082010-02-26 05:48:29 +0000119{
snelson8913d082010-02-26 05:48:29 +0000120 if (!oddparity(readarr[0]))
snelsonfc007bb2010-03-24 23:14:32 +0000121 msg_cdbg("RDID byte 0 parity violation. ");
snelson8913d082010-02-26 05:48:29 +0000122
hailfingercb0564e2010-06-20 10:39:33 +0000123 /* Check if this is a continuation vendor ID.
124 * FIXME: Handle continuation device IDs.
125 */
snelson8913d082010-02-26 05:48:29 +0000126 if (readarr[0] == 0x7f) {
127 if (!oddparity(readarr[1]))
snelsonfc007bb2010-03-24 23:14:32 +0000128 msg_cdbg("RDID byte 1 parity violation. ");
David Hendricks7f7c7112012-10-11 17:15:48 -0700129 *id1 = (readarr[0] << 8) | readarr[1];
130 *id2 = readarr[2];
snelson8913d082010-02-26 05:48:29 +0000131 if (bytes > 3) {
David Hendricks7f7c7112012-10-11 17:15:48 -0700132 *id2 <<= 8;
133 *id2 |= readarr[3];
snelson8913d082010-02-26 05:48:29 +0000134 }
135 } else {
David Hendricks7f7c7112012-10-11 17:15:48 -0700136 *id1 = readarr[0];
137 *id2 = (readarr[1] << 8) | readarr[2];
snelson8913d082010-02-26 05:48:29 +0000138 }
David Hendricks7f7c7112012-10-11 17:15:48 -0700139}
snelson8913d082010-02-26 05:48:29 +0000140
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700141static int compare_id(struct flashctx *flash, uint32_t id1, uint32_t id2)
David Hendricks7f7c7112012-10-11 17:15:48 -0700142{
143 msg_cdbg("id1 0x%02x, id2 0x%02x\n", id1, id2);
snelson8913d082010-02-26 05:48:29 +0000144
Edward O'Callaghan71e23142019-03-03 23:08:22 +1100145 if (id1 == flash->chip->manufacture_id && id2 == flash->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. */
Patrick Georgif3fa2992017-02-02 16:24:44 +0100149 if (id1 == flash->chip->manufacture_id &&
150 GENERIC_DEVICE_ID == flash->chip->model_id)
snelson8913d082010-02-26 05:48:29 +0000151 return 1;
152
153 /* Test if there is any vendor ID. */
Patrick Georgif3fa2992017-02-02 16:24:44 +0100154 if (GENERIC_MANUF_ID == flash->chip->manufacture_id &&
snelson8913d082010-02-26 05:48:29 +0000155 id1 != 0xff)
156 return 1;
157
158 return 0;
159}
160
Edward O'Callaghanc2a12a92020-05-14 17:58:32 +1000161static int probe_spi_rdid_generic(struct flashctx *flash, int bytes)
snelson8913d082010-02-26 05:48:29 +0000162{
David Hendricks57b75242015-11-20 15:54:07 -0800163 uint32_t id1, id2;
Edward O'Callaghanc2a12a92020-05-14 17:58:32 +1000164 enum id_type idty = bytes == 3 ? RDID : RDID4;
David Hendricks7f7c7112012-10-11 17:15:48 -0700165
Edward O'Callaghanc2a12a92020-05-14 17:58:32 +1000166 if (!id_cache[idty].is_cached) {
167 const int ret = spi_rdid(flash, id_cache[idty].bytes, bytes);
168 if (ret == SPI_INVALID_LENGTH)
169 msg_cinfo("%d byte RDID not supported on this SPI controller\n", bytes);
170 if (ret)
David Hendricks7f7c7112012-10-11 17:15:48 -0700171 return 0;
Edward O'Callaghanc2a12a92020-05-14 17:58:32 +1000172 id_cache[idty].is_cached = 1;
David Hendricks7f7c7112012-10-11 17:15:48 -0700173 }
174
Edward O'Callaghanc2a12a92020-05-14 17:58:32 +1000175 rdid_get_ids(id_cache[idty].bytes, bytes, &id1, &id2);
David Hendricks7f7c7112012-10-11 17:15:48 -0700176 return compare_id(flash, id1, id2);
snelson8913d082010-02-26 05:48:29 +0000177}
178
Edward O'Callaghanc2a12a92020-05-14 17:58:32 +1000179int probe_spi_rdid(struct flashctx *flash)
180{
181 return probe_spi_rdid_generic(flash, 3);
182}
183
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700184int probe_spi_rdid4(struct flashctx *flash)
snelson8913d082010-02-26 05:48:29 +0000185{
Edward O'Callaghanc2a12a92020-05-14 17:58:32 +1000186 return probe_spi_rdid_generic(flash, 4);
snelson8913d082010-02-26 05:48:29 +0000187}
188
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700189int probe_spi_rems(struct flashctx *flash)
snelson8913d082010-02-26 05:48:29 +0000190{
David Hendricks57b75242015-11-20 15:54:07 -0800191 uint32_t id1, id2;
snelson8913d082010-02-26 05:48:29 +0000192
David Hendricks57b75242015-11-20 15:54:07 -0800193 if (!id_cache[REMS].is_cached) {
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700194 if (spi_rems(flash, id_cache[REMS].bytes))
David Hendricks7f7c7112012-10-11 17:15:48 -0700195 return 0;
David Hendricks57b75242015-11-20 15:54:07 -0800196 id_cache[REMS].is_cached = 1;
stefanct9e6b98a2011-05-28 02:37:14 +0000197 }
snelson8913d082010-02-26 05:48:29 +0000198
David Hendricks57b75242015-11-20 15:54:07 -0800199 id1 = id_cache[REMS].bytes[0];
200 id2 = id_cache[REMS].bytes[1];
David Hendricks7f7c7112012-10-11 17:15:48 -0700201 return compare_id(flash, id1, id2);
snelson8913d082010-02-26 05:48:29 +0000202}
203
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700204int probe_spi_res1(struct flashctx *flash)
snelson8913d082010-02-26 05:48:29 +0000205{
krause2eb76212011-01-17 07:50:42 +0000206 static const unsigned char allff[] = {0xff, 0xff, 0xff};
207 static const unsigned char all00[] = {0x00, 0x00, 0x00};
snelson8913d082010-02-26 05:48:29 +0000208 unsigned char readarr[3];
209 uint32_t id2;
snelson8913d082010-02-26 05:48:29 +0000210
hailfinger59a83572010-05-28 17:07:57 +0000211 /* We only want one-byte RES if RDID and REMS are unusable. */
212
snelson8913d082010-02-26 05:48:29 +0000213 /* Check if RDID is usable and does not return 0xff 0xff 0xff or
214 * 0x00 0x00 0x00. In that case, RES is pointless.
215 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700216 if (!spi_rdid(flash, readarr, 3) && memcmp(readarr, allff, 3) &&
snelson8913d082010-02-26 05:48:29 +0000217 memcmp(readarr, all00, 3)) {
218 msg_cdbg("Ignoring RES in favour of RDID.\n");
219 return 0;
220 }
221 /* Check if REMS is usable and does not return 0xff 0xff or
222 * 0x00 0x00. In that case, RES is pointless.
223 */
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100224 if (!spi_rems(flash, readarr) &&
225 memcmp(readarr, allff, JEDEC_REMS_INSIZE) &&
snelson8913d082010-02-26 05:48:29 +0000226 memcmp(readarr, all00, JEDEC_REMS_INSIZE)) {
227 msg_cdbg("Ignoring RES in favour of REMS.\n");
228 return 0;
229 }
230
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700231 if (spi_res(flash, readarr, 1)) {
snelson8913d082010-02-26 05:48:29 +0000232 return 0;
stefanct9e6b98a2011-05-28 02:37:14 +0000233 }
snelson8913d082010-02-26 05:48:29 +0000234
snelson8913d082010-02-26 05:48:29 +0000235 id2 = readarr[0];
hailfinger59a83572010-05-28 17:07:57 +0000236
snelsonfc007bb2010-03-24 23:14:32 +0000237 msg_cdbg("%s: id 0x%x\n", __func__, id2);
hailfinger59a83572010-05-28 17:07:57 +0000238
Patrick Georgif3fa2992017-02-02 16:24:44 +0100239 if (id2 != flash->chip->model_id)
snelson8913d082010-02-26 05:48:29 +0000240 return 0;
241
snelson8913d082010-02-26 05:48:29 +0000242 return 1;
243}
244
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700245int probe_spi_res2(struct flashctx *flash)
hailfinger59a83572010-05-28 17:07:57 +0000246{
hailfinger59a83572010-05-28 17:07:57 +0000247 uint32_t id1, id2;
248
David Hendricks57b75242015-11-20 15:54:07 -0800249 if (!id_cache[RES2].is_cached) {
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700250 if (spi_res(flash, id_cache[RES2].bytes, 2))
David Hendricks57b75242015-11-20 15:54:07 -0800251 return 0;
252 id_cache[RES2].is_cached = 1;
stefanct9e6b98a2011-05-28 02:37:14 +0000253 }
hailfinger59a83572010-05-28 17:07:57 +0000254
David Hendricks57b75242015-11-20 15:54:07 -0800255 id1 = id_cache[RES2].bytes[0];
256 id2 = id_cache[RES2].bytes[1];
hailfinger59a83572010-05-28 17:07:57 +0000257 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
258
Patrick Georgif3fa2992017-02-02 16:24:44 +0100259 if (id1 != flash->chip->manufacture_id || id2 != flash->chip->model_id)
hailfinger59a83572010-05-28 17:07:57 +0000260 return 0;
261
hailfinger59a83572010-05-28 17:07:57 +0000262 return 1;
263}
264
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000265static int spi_poll_wip(struct flashctx *const flash, const unsigned int poll_delay)
266{
267 /* FIXME: We can't tell if spi_read_status_register() failed. */
268 /* FIXME: We don't time out. */
269 while (spi_read_status_register(flash) & SPI_SR_WIP)
270 programmer_delay(poll_delay);
271 /* FIXME: Check the status register for errors. */
272 return 0;
273}
274
Nico Huber4c8a9562017-10-15 11:20:58 +0200275/**
276 * Execute WREN plus another one byte `op`, optionally poll WIP afterwards.
277 *
278 * @param flash the flash chip's context
279 * @param op the operation to execute
280 * @param poll_delay interval in us for polling WIP, don't poll if zero
281 * @return 0 on success, non-zero otherwise
282 */
283static int spi_simple_write_cmd(struct flashctx *const flash, const uint8_t op, const unsigned int poll_delay)
snelson8913d082010-02-26 05:48:29 +0000284{
snelson8913d082010-02-26 05:48:29 +0000285 struct spi_command cmds[] = {
286 {
Nico Huber4c8a9562017-10-15 11:20:58 +0200287 .writecnt = 1,
288 .writearr = (const unsigned char[]){ JEDEC_WREN },
snelson8913d082010-02-26 05:48:29 +0000289 }, {
Nico Huber4c8a9562017-10-15 11:20:58 +0200290 .writecnt = 1,
291 .writearr = (const unsigned char[]){ op },
292 },
293 NULL_SPI_CMD,
294 };
snelson8913d082010-02-26 05:48:29 +0000295
Nico Huber4c8a9562017-10-15 11:20:58 +0200296 const int result = spi_send_multicommand(flash, cmds);
snelson8913d082010-02-26 05:48:29 +0000297 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000298 msg_cerr("%s failed during command execution\n", __func__);
snelson8913d082010-02-26 05:48:29 +0000299 return result;
300 }
301 /* Wait until the Write-In-Progress bit is cleared.
302 * This usually takes 1-85 s, so wait in 1 s steps.
303 */
Nico Huber4c8a9562017-10-15 11:20:58 +0200304
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000305 const int status = poll_delay ? spi_poll_wip(flash, poll_delay) : 0;
306
307 return result ? result : status;
308}
309
Edward O'Callaghana74ffcd2019-06-17 14:59:55 +1000310static int spi_set_extended_address(struct flashctx *const flash, const uint8_t addr_high)
311{
312 if (flash->address_high_byte != addr_high &&
313 spi_write_extended_address_register(flash, addr_high))
314 return -1;
315 flash->address_high_byte = addr_high;
316 return 0;
317}
318
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000319static int spi_prepare_address(struct flashctx *const flash, uint8_t cmd_buf[],
320 const bool native_4ba, const unsigned int addr)
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000321{
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000322 if (native_4ba || flash->in_4ba_mode) {
Edward O'Callaghana6673bd2019-06-24 15:22:28 +1000323 if (!spi_master_4ba(flash)) {
324 msg_cwarn("4-byte address requested but master can't handle 4-byte addresses.\n");
325 return -1;
326 }
Edward O'Callaghana74ffcd2019-06-17 14:59:55 +1000327 cmd_buf[1] = (addr >> 24) & 0xff;
328 cmd_buf[2] = (addr >> 16) & 0xff;
329 cmd_buf[3] = (addr >> 8) & 0xff;
330 cmd_buf[4] = (addr >> 0) & 0xff;
331 return 4;
332 } else {
333 if (flash->chip->feature_bits & FEATURE_4BA_EXT_ADDR) {
334 if (spi_set_extended_address(flash, addr >> 24))
335 return -1;
Edward O'Callaghana6673bd2019-06-24 15:22:28 +1000336 } else if (addr >> 24) {
337 msg_cerr("Can't handle 4-byte address for opcode '0x%02x'\n"
338 "with this chip/programmer combination.\n", cmd_buf[0]);
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100339 return -1;
Edward O'Callaghana74ffcd2019-06-17 14:59:55 +1000340 }
341 cmd_buf[1] = (addr >> 16) & 0xff;
342 cmd_buf[2] = (addr >> 8) & 0xff;
343 cmd_buf[3] = (addr >> 0) & 0xff;
344 return 3;
345 }
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000346}
347
348/**
349 * Execute WREN plus another `op` that takes an address and
350 * optional data, poll WIP afterwards.
351 *
352 * @param flash the flash chip's context
353 * @param op the operation to execute
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100354 * @param native_4ba whether `op` always takes a 4-byte address
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000355 * @param addr the address parameter to `op`
356 * @param out_bytes bytes to send after the address,
357 * may be NULL if and only if `out_bytes` is 0
358 * @param out_bytes number of bytes to send, 256 at most, may be zero
359 * @param poll_delay interval in us for polling WIP
360 * @return 0 on success, non-zero otherwise
361 */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000362static int spi_write_cmd(struct flashctx *const flash, const uint8_t op,
363 const bool native_4ba, const unsigned int addr,
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000364 const uint8_t *const out_bytes, const size_t out_len,
365 const unsigned int poll_delay)
366{
367 uint8_t cmd[1 + JEDEC_MAX_ADDR_LEN + 256];
368 struct spi_command cmds[] = {
369 {
370 .writecnt = 1,
371 .writearr = (const unsigned char[]){ JEDEC_WREN },
372 }, {
373 .writearr = cmd,
374 },
375 NULL_SPI_CMD,
376 };
377
378 cmd[0] = op;
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000379 const int addr_len = spi_prepare_address(flash, cmd, native_4ba, addr);
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000380 if (addr_len < 0)
381 return 1;
382
383 if (1 + addr_len + out_len > sizeof(cmd)) {
384 msg_cerr("%s called for too long a write\n", __func__);
385 return 1;
386 }
Angel Pons6bfd9e62020-03-31 15:32:10 +0200387 if (!out_bytes && out_len > 0)
388 return 1;
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000389
390 memcpy(cmd + 1 + addr_len, out_bytes, out_len);
391 cmds[1].writecnt = 1 + addr_len + out_len;
392
393 const int result = spi_send_multicommand(flash, cmds);
394 if (result)
395 msg_cerr("%s failed during command execution at address 0x%x\n", __func__, addr);
396
397 const int status = spi_poll_wip(flash, poll_delay);
398
399 return result ? result : status;
Nico Huber4c8a9562017-10-15 11:20:58 +0200400}
401
402int spi_chip_erase_60(struct flashctx *flash)
403{
404 /* This usually takes 1-85s, so wait in 1s steps. */
405 return spi_simple_write_cmd(flash, 0x60, 1000 * 1000);
406}
407
408int spi_chip_erase_62(struct flashctx *flash)
409{
410 /* This usually takes 2-5s, so wait in 100ms steps. */
411 return spi_simple_write_cmd(flash, 0x62, 100 * 1000);
412}
413
414int spi_chip_erase_c7(struct flashctx *flash)
415{
416 /* This usually takes 1-85s, so wait in 1s steps. */
417 return spi_simple_write_cmd(flash, 0xc7, 1000 * 1000);
snelson8913d082010-02-26 05:48:29 +0000418}
419
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100420int spi_block_erase_52(struct flashctx *flash, unsigned int addr,
421 unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000422{
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000423 /* This usually takes 100-4000ms, so wait in 100ms steps. */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000424 return spi_write_cmd(flash, 0x52, false, addr, NULL, 0, 100 * 1000);
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000425}
snelson8913d082010-02-26 05:48:29 +0000426
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000427/* Block size is usually
428 * 32M (one die) for Micron
429 */
430int spi_block_erase_c4(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
431{
432 /* This usually takes 240-480s, so wait in 500ms steps. */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000433 return spi_write_cmd(flash, 0xc4, false, addr, NULL, 0, 500 * 1000);
snelson8913d082010-02-26 05:48:29 +0000434}
435
436/* Block size is usually
437 * 64k for Macronix
438 * 32k for SST
439 * 4-32k non-uniform for EON
440 */
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100441int spi_block_erase_d8(struct flashctx *flash, unsigned int addr,
442 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. */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000445 return spi_write_cmd(flash, 0xd8, false, addr, NULL, 0, 100 * 1000);
snelson8913d082010-02-26 05:48:29 +0000446}
447
448/* Block size is usually
449 * 4k for PMC
450 */
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100451int spi_block_erase_d7(struct flashctx *flash, unsigned int addr,
452 unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000453{
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000454 /* This usually takes 100-4000ms, so wait in 100ms steps. */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000455 return spi_write_cmd(flash, 0xd7, false, addr, NULL, 0, 100 * 1000);
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000456}
snelson8913d082010-02-26 05:48:29 +0000457
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000458/* Page erase (usually 256B blocks) */
459int spi_block_erase_db(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
460{
461 /* This takes up to 20ms usually (on worn out devices
462 up to the 0.5s range), so wait in 1ms steps. */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000463 return spi_write_cmd(flash, 0xdb, false, addr, NULL, 0, 1 * 1000);
snelson8913d082010-02-26 05:48:29 +0000464}
465
snelson8913d082010-02-26 05:48:29 +0000466/* Sector size is usually 4k, though Macronix eliteflash has 64k */
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100467int spi_block_erase_20(struct flashctx *flash, unsigned int addr,
468 unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000469{
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000470 /* This usually takes 15-800ms, so wait in 10ms steps. */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000471 return spi_write_cmd(flash, 0x20, false, addr, NULL, 0, 10 * 1000);
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000472}
snelson8913d082010-02-26 05:48:29 +0000473
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000474int spi_block_erase_50(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
475{
476 /* This usually takes 10ms, so wait in 1ms steps. */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000477 return spi_write_cmd(flash, 0x50, false, addr, NULL, 0, 1 * 1000);
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000478}
Stefan Reinauercce56d52010-11-22 18:22:21 -0800479
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000480int spi_block_erase_81(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
481{
482 /* This usually takes 8ms, so wait in 1ms steps. */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000483 return spi_write_cmd(flash, 0x81, false, addr, NULL, 0, 1 * 1000);
snelson8913d082010-02-26 05:48:29 +0000484}
485
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100486int spi_block_erase_60(struct flashctx *flash, unsigned int addr,
487 unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000488{
Patrick Georgif3fa2992017-02-02 16:24:44 +0100489 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
snelsonfc007bb2010-03-24 23:14:32 +0000490 msg_cerr("%s called with incorrect arguments\n",
snelson8913d082010-02-26 05:48:29 +0000491 __func__);
492 return -1;
493 }
494 return spi_chip_erase_60(flash);
495}
496
Alan Green5d709732019-09-16 12:32:25 +1000497int spi_block_erase_62(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
498{
499 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
500 msg_cerr("%s called with incorrect arguments\n",
501 __func__);
502 return -1;
503 }
504 return spi_chip_erase_62(flash);
505}
506
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100507int spi_block_erase_c7(struct flashctx *flash, unsigned int addr,
508 unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000509{
Patrick Georgif3fa2992017-02-02 16:24:44 +0100510 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
snelsonfc007bb2010-03-24 23:14:32 +0000511 msg_cerr("%s called with incorrect arguments\n",
snelson8913d082010-02-26 05:48:29 +0000512 __func__);
513 return -1;
514 }
515 return spi_chip_erase_c7(flash);
516}
517
Edward O'Callaghan94934e82019-06-19 17:44:19 +1000518/* Erase 4 KB of flash with 4-bytes address from ANY mode (3-bytes or 4-bytes)
519 JEDEC_SE_4BA (21h) instruction is new for 4-bytes addressing flash chips.
520 The presence of this instruction for an exact chip should be checked
521 by its datasheet or from SFDP 4-Bytes Address Instruction Table (JESD216B). */
522int spi_block_erase_21(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
523{
524 /* This usually takes 15-800ms, so wait in 10ms steps. */
525 return spi_write_cmd(flash, 0x21, true, addr, NULL, 0, 10 * 1000);
526}
527
528/* Erase 32 KB of flash with 4-bytes address from ANY mode (3-bytes or 4-bytes)
529 JEDEC_BE_5C_4BA (5Ch) instruction is new for 4-bytes addressing flash chips.
530 The presence of this instruction for an exact chip should be checked
531 by its datasheet or from SFDP 4-Bytes Address Instruction Table (JESD216B). */
532int spi_block_erase_5c(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
533{
534 /* This usually takes 100-4000ms, so wait in 100ms steps. */
535 return spi_write_cmd(flash, 0x5c, true, addr, NULL, 0, 100 * 1000);
536}
537
538/* Erase 64 KB of flash with 4-bytes address from ANY mode (3-bytes or 4-bytes)
539 JEDEC_BE_DC_4BA (DCh) instruction is new for 4-bytes addressing flash chips.
540 The presence of this instruction for an exact chip should be checked
541 by its datasheet or from SFDP 4-Bytes Address Instruction Table (JESD216B). */
542int spi_block_erase_dc(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
543{
544 /* This usually takes 100-4000ms, so wait in 100ms steps. */
545 return spi_write_cmd(flash, 0xdc, true, addr, NULL, 0, 100 * 1000);
546}
547
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700548int spi_write_status_register_wren(const struct flashctx *flash, int status)
hailfingerc33d4732010-07-29 13:09:18 +0000549{
550 int result;
hailfingeree9ee132010-10-08 00:37:55 +0000551 int i = 0;
hailfingerc33d4732010-07-29 13:09:18 +0000552 struct spi_command cmds[] = {
553 {
554 /* WRSR requires either EWSR or WREN depending on chip type. */
555 .writecnt = JEDEC_WREN_OUTSIZE,
556 .writearr = (const unsigned char[]){ JEDEC_WREN },
557 .readcnt = 0,
558 .readarr = NULL,
559 }, {
560 .writecnt = JEDEC_WRSR_OUTSIZE,
561 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
562 .readcnt = 0,
563 .readarr = NULL,
564 }, {
565 .writecnt = 0,
566 .writearr = NULL,
567 .readcnt = 0,
568 .readarr = NULL,
569 }};
570
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700571 result = spi_send_multicommand(flash, cmds);
hailfingerc33d4732010-07-29 13:09:18 +0000572 if (result) {
573 msg_cerr("%s failed during command execution\n",
574 __func__);
hailfingeree9ee132010-10-08 00:37:55 +0000575 /* No point in waiting for the command to complete if execution
576 * failed.
577 */
578 return result;
hailfingerc33d4732010-07-29 13:09:18 +0000579 }
hailfingeree9ee132010-10-08 00:37:55 +0000580 /* WRSR performs a self-timed erase before the changes take effect.
581 * This may take 50-85 ms in most cases, and some chips apparently
582 * allow running RDSR only once. Therefore pick an initial delay of
583 * 100 ms, then wait in 10 ms steps until a total of 5 s have elapsed.
584 */
hailfingerc33d4732010-07-29 13:09:18 +0000585 programmer_delay(100 * 1000);
Edward O'Callaghan8b5e4732019-03-05 15:27:53 +1100586 while (spi_read_status_register(flash) & SPI_SR_WIP) {
hailfingeree9ee132010-10-08 00:37:55 +0000587 if (++i > 490) {
588 msg_cerr("Error: WIP bit after WRSR never cleared\n");
589 return TIMEOUT_ERROR;
590 }
591 programmer_delay(10 * 1000);
592 }
593 return 0;
hailfingerc33d4732010-07-29 13:09:18 +0000594}
595
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700596int spi_byte_program(struct flashctx *flash, unsigned int addr, uint8_t databyte)
snelson8913d082010-02-26 05:48:29 +0000597{
598 int result;
599 struct spi_command cmds[] = {
600 {
601 .writecnt = JEDEC_WREN_OUTSIZE,
602 .writearr = (const unsigned char[]){ JEDEC_WREN },
603 .readcnt = 0,
604 .readarr = NULL,
605 }, {
606 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE,
607 .writearr = (const unsigned char[]){
608 JEDEC_BYTE_PROGRAM,
609 (addr >> 16) & 0xff,
610 (addr >> 8) & 0xff,
611 (addr & 0xff),
612 databyte
613 },
614 .readcnt = 0,
615 .readarr = NULL,
616 }, {
617 .writecnt = 0,
618 .writearr = NULL,
619 .readcnt = 0,
620 .readarr = NULL,
621 }};
622
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700623 result = spi_send_multicommand(flash, cmds);
snelson8913d082010-02-26 05:48:29 +0000624 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000625 msg_cerr("%s failed during command execution at address 0x%x\n",
snelson8913d082010-02-26 05:48:29 +0000626 __func__, addr);
627 }
628 return result;
629}
630
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000631static int spi_nbyte_program(struct flashctx *flash, unsigned int addr, const uint8_t *bytes, unsigned int len)
snelson8913d082010-02-26 05:48:29 +0000632{
Edward O'Callaghana6673bd2019-06-24 15:22:28 +1000633 const bool native_4ba = flash->chip->feature_bits & FEATURE_4BA_WRITE && spi_master_4ba(flash);
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000634 const uint8_t op = native_4ba ? JEDEC_BYTE_PROGRAM_4BA : JEDEC_BYTE_PROGRAM;
635 return spi_write_cmd(flash, op, native_4ba, addr, bytes, len, 10);
snelson8913d082010-02-26 05:48:29 +0000636}
637
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100638int spi_nbyte_read(struct flashctx *flash, unsigned int address, uint8_t *bytes,
639 unsigned int len)
snelson8913d082010-02-26 05:48:29 +0000640{
Edward O'Callaghana6673bd2019-06-24 15:22:28 +1000641 const bool native_4ba = flash->chip->feature_bits & FEATURE_4BA_READ && spi_master_4ba(flash);
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000642 uint8_t cmd[1 + JEDEC_MAX_ADDR_LEN] = { native_4ba ? JEDEC_READ_4BA : JEDEC_READ, };
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000643
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000644 const int addr_len = spi_prepare_address(flash, cmd, native_4ba, address);
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000645 if (addr_len < 0)
646 return 1;
snelson8913d082010-02-26 05:48:29 +0000647
648 /* Send Read */
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000649 return spi_send_command(flash, 1 + addr_len, len, cmd, bytes);
snelson8913d082010-02-26 05:48:29 +0000650}
651
652/*
hailfinger39d159a2010-05-21 23:09:42 +0000653 * Read a part of the flash chip.
hailfingerc7d06c62010-07-14 16:19:05 +0000654 * FIXME: Use the chunk code from Michael Karcher instead.
Edward O'Callaghand825ac02019-07-26 21:36:16 +1000655 * Each naturally aligned area is read separately in chunks with a maximum size of chunksize.
snelson8913d082010-02-26 05:48:29 +0000656 */
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100657int spi_read_chunked(struct flashctx *flash, uint8_t *buf, unsigned int start,
658 unsigned int len, unsigned int chunksize)
snelson8913d082010-02-26 05:48:29 +0000659{
David Hendricks1ed1d352011-11-23 17:54:37 -0800660 int rc = 0, chunk_status = 0;
stefanctc5eb8a92011-11-23 09:13:48 +0000661 unsigned int i, j, starthere, lenhere, toread;
Edward O'Callaghand825ac02019-07-26 21:36:16 +1000662 /* Limit for multi-die 4-byte-addressing chips. */
663 unsigned int area_size = min(flash->chip->total_size * 1024, 16 * 1024 * 1024);
snelson8913d082010-02-26 05:48:29 +0000664
665 /* Warning: This loop has a very unusual condition and body.
Edward O'Callaghand825ac02019-07-26 21:36:16 +1000666 * The loop needs to go through each area with at least one affected
667 * byte. The lowest area number is (start / area_size) since that
668 * division rounds down. The highest area number we want is the area
snelson8913d082010-02-26 05:48:29 +0000669 * where the last byte of the range lives. That last byte has the
Edward O'Callaghand825ac02019-07-26 21:36:16 +1000670 * address (start + len - 1), thus the highest area number is
671 * (start + len - 1) / area_size. Since we want to include that last
672 * area as well, the loop condition uses <=.
snelson8913d082010-02-26 05:48:29 +0000673 */
Edward O'Callaghand825ac02019-07-26 21:36:16 +1000674 for (i = start / area_size; i <= (start + len - 1) / area_size; i++) {
675 /* Byte position of the first byte in the range in this area. */
snelson8913d082010-02-26 05:48:29 +0000676 /* starthere is an offset to the base address of the chip. */
Edward O'Callaghand825ac02019-07-26 21:36:16 +1000677 starthere = max(start, i * area_size);
678 /* Length of bytes in the range in this area. */
679 lenhere = min(start + len, (i + 1) * area_size) - starthere;
snelson8913d082010-02-26 05:48:29 +0000680 for (j = 0; j < lenhere; j += chunksize) {
681 toread = min(chunksize, lenhere - j);
Edward O'Callaghan4fe3a972019-06-19 16:56:10 +1000682 chunk_status = spi_nbyte_read(flash, starthere + j, buf + starthere - start + j, toread);
David Hendricks1ed1d352011-11-23 17:54:37 -0800683 if (chunk_status) {
684 if (ignore_error(chunk_status)) {
685 /* fill this chunk with 0xff bytes and
686 let caller know about the error */
687 memset(buf + starthere - start + j, 0xff, toread);
688 rc = chunk_status;
689 chunk_status = 0;
690 continue;
691 } else {
692 rc = chunk_status;
693 break;
694 }
695 }
snelson8913d082010-02-26 05:48:29 +0000696 }
David Hendricks1ed1d352011-11-23 17:54:37 -0800697 if (chunk_status)
snelson8913d082010-02-26 05:48:29 +0000698 break;
699 }
700
701 return rc;
702}
703
704/*
hailfinger39d159a2010-05-21 23:09:42 +0000705 * Write a part of the flash chip.
hailfingerc7d06c62010-07-14 16:19:05 +0000706 * FIXME: Use the chunk code from Michael Karcher instead.
hailfinger39d159a2010-05-21 23:09:42 +0000707 * Each page is written separately in chunks with a maximum size of chunksize.
708 */
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100709int spi_write_chunked(struct flashctx *flash, const uint8_t *buf, unsigned int start,
710 unsigned int len, unsigned int chunksize)
hailfinger39d159a2010-05-21 23:09:42 +0000711{
stefanctc5eb8a92011-11-23 09:13:48 +0000712 unsigned int i, j, starthere, lenhere, towrite;
hailfinger39d159a2010-05-21 23:09:42 +0000713 /* FIXME: page_size is the wrong variable. We need max_writechunk_size
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700714 * in struct flashctx to do this properly. All chips using
hailfinger39d159a2010-05-21 23:09:42 +0000715 * spi_chip_write_256 have page_size set to max_writechunk_size, so
716 * we're OK for now.
717 */
Patrick Georgif3fa2992017-02-02 16:24:44 +0100718 unsigned int page_size = flash->chip->page_size;
hailfinger39d159a2010-05-21 23:09:42 +0000719
720 /* Warning: This loop has a very unusual condition and body.
721 * The loop needs to go through each page with at least one affected
722 * byte. The lowest page number is (start / page_size) since that
723 * division rounds down. The highest page number we want is the page
724 * where the last byte of the range lives. That last byte has the
725 * address (start + len - 1), thus the highest page number is
726 * (start + len - 1) / page_size. Since we want to include that last
727 * page as well, the loop condition uses <=.
728 */
729 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
730 /* Byte position of the first byte in the range in this page. */
731 /* starthere is an offset to the base address of the chip. */
732 starthere = max(start, i * page_size);
733 /* Length of bytes in the range in this page. */
734 lenhere = min(start + len, (i + 1) * page_size) - starthere;
735 for (j = 0; j < lenhere; j += chunksize) {
Edward O'Callaghan4fe3a972019-06-19 16:56:10 +1000736 int rc;
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100737
hailfinger39d159a2010-05-21 23:09:42 +0000738 towrite = min(chunksize, lenhere - j);
Edward O'Callaghan4fe3a972019-06-19 16:56:10 +1000739 rc = spi_nbyte_program(flash, starthere + j, buf + starthere - start + j, towrite);
hailfinger39d159a2010-05-21 23:09:42 +0000740 if (rc)
Edward O'Callaghan4fe3a972019-06-19 16:56:10 +1000741 return rc;
hailfinger39d159a2010-05-21 23:09:42 +0000742 }
hailfinger39d159a2010-05-21 23:09:42 +0000743 }
744
Edward O'Callaghan4fe3a972019-06-19 16:56:10 +1000745 return 0;
hailfinger39d159a2010-05-21 23:09:42 +0000746}
747
748/*
snelson8913d082010-02-26 05:48:29 +0000749 * Program chip using byte programming. (SLOW!)
750 * This is for chips which can only handle one byte writes
751 * and for chips where memory mapped programming is impossible
752 * (e.g. due to size constraints in IT87* for over 512 kB)
753 */
hailfingerc7d06c62010-07-14 16:19:05 +0000754/* real chunksize is 1, logical chunksize is 1 */
Patrick Georgiab8353e2017-02-03 18:32:01 +0100755int spi_chip_write_1(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
snelson8913d082010-02-26 05:48:29 +0000756{
stefanctc5eb8a92011-11-23 09:13:48 +0000757 unsigned int i;
snelson8913d082010-02-26 05:48:29 +0000758
hailfingerc7d06c62010-07-14 16:19:05 +0000759 for (i = start; i < start + len; i++) {
Edward O'Callaghan4fe3a972019-06-19 16:56:10 +1000760 if (spi_nbyte_program(flash, i, buf + i - start, 1))
snelson8913d082010-02-26 05:48:29 +0000761 return 1;
snelson8913d082010-02-26 05:48:29 +0000762 }
snelson8913d082010-02-26 05:48:29 +0000763 return 0;
764}
765
Patrick Georgiab8353e2017-02-03 18:32:01 +0100766int spi_aai_write(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
hailfingerc7d06c62010-07-14 16:19:05 +0000767{
768 uint32_t pos = start;
snelson8913d082010-02-26 05:48:29 +0000769 int result;
hailfinger19db0922010-06-20 10:41:35 +0000770 unsigned char cmd[JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE] = {
771 JEDEC_AAI_WORD_PROGRAM,
772 };
snelson8913d082010-02-26 05:48:29 +0000773
hailfingerc7d06c62010-07-14 16:19:05 +0000774 /* The even start address and even length requirements can be either
775 * honored outside this function, or we can call spi_byte_program
776 * for the first and/or last byte and use AAI for the rest.
hailfinger71e1bd42010-10-13 22:26:56 +0000777 * FIXME: Move this to generic code.
hailfingerc7d06c62010-07-14 16:19:05 +0000778 */
hailfinger19db0922010-06-20 10:41:35 +0000779 /* The data sheet requires a start address with the low bit cleared. */
hailfingerc7d06c62010-07-14 16:19:05 +0000780 if (start % 2) {
hailfinger19db0922010-06-20 10:41:35 +0000781 msg_cerr("%s: start address not even! Please report a bug at "
782 "flashrom@flashrom.org\n", __func__);
hailfinger71e1bd42010-10-13 22:26:56 +0000783 if (spi_chip_write_1(flash, buf, start, start % 2))
784 return SPI_GENERIC_ERROR;
785 pos += start % 2;
786 /* Do not return an error for now. */
787 //return SPI_GENERIC_ERROR;
hailfinger19db0922010-06-20 10:41:35 +0000788 }
789 /* The data sheet requires total AAI write length to be even. */
790 if (len % 2) {
791 msg_cerr("%s: total write length not even! Please report a "
792 "bug at flashrom@flashrom.org\n", __func__);
hailfinger71e1bd42010-10-13 22:26:56 +0000793 /* Do not return an error for now. */
794 //return SPI_GENERIC_ERROR;
hailfinger19db0922010-06-20 10:41:35 +0000795 }
796
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000797 result = spi_write_cmd(flash, JEDEC_AAI_WORD_PROGRAM, false, start, buf + pos - start, 2, 10);
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000798 if (result)
Edward O'Callaghan633cbd62019-06-17 15:43:56 +1000799 goto bailout;
hailfinger19db0922010-06-20 10:41:35 +0000800
801 /* We already wrote 2 bytes in the multicommand step. */
802 pos += 2;
803
hailfinger71e1bd42010-10-13 22:26:56 +0000804 /* Are there at least two more bytes to write? */
805 while (pos < start + len - 1) {
hailfingerdef852d2010-10-27 22:07:11 +0000806 cmd[1] = buf[pos++ - start];
807 cmd[2] = buf[pos++ - start];
Edward O'Callaghan633cbd62019-06-17 15:43:56 +1000808 result = spi_send_command(flash, JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE, 0, cmd, NULL);
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100809 if (result != 0) {
Edward O'Callaghan633cbd62019-06-17 15:43:56 +1000810 msg_cerr("%s failed during followup AAI command execution: %d\n", __func__, result);
811 goto bailout;
812 }
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000813 if (spi_poll_wip(flash, 10))
814 goto bailout;
hailfinger19db0922010-06-20 10:41:35 +0000815 }
816
hailfinger71e1bd42010-10-13 22:26:56 +0000817 /* Use WRDI to exit AAI mode. This needs to be done before issuing any
818 * other non-AAI command.
819 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700820 spi_write_disable(flash);
hailfinger71e1bd42010-10-13 22:26:56 +0000821
822 /* Write remaining byte (if any). */
823 if (pos < start + len) {
hailfingerdef852d2010-10-27 22:07:11 +0000824 if (spi_chip_write_1(flash, buf + pos - start, pos, pos % 2))
hailfinger71e1bd42010-10-13 22:26:56 +0000825 return SPI_GENERIC_ERROR;
hailfinger71e1bd42010-10-13 22:26:56 +0000826 }
827
snelson8913d082010-02-26 05:48:29 +0000828 return 0;
Edward O'Callaghan633cbd62019-06-17 15:43:56 +1000829
830bailout:
831 spi_write_disable(flash);
832 return SPI_GENERIC_ERROR;
snelson8913d082010-02-26 05:48:29 +0000833}