blob: 415a097f0e41c093e858723aa0396d7dd8557e33 [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>
Edward O'Callaghan031831d2019-06-19 16:27:43 +100024#include <stdbool.h>
snelson8913d082010-02-26 05:48:29 +000025#include "flash.h"
26#include "flashchips.h"
27#include "chipdrivers.h"
hailfinger428f6852010-07-27 22:41:39 +000028#include "programmer.h"
snelson8913d082010-02-26 05:48:29 +000029#include "spi.h"
Boris Baykov1a2f5322016-06-11 18:29:00 +020030#include "spi4ba.h"
snelson8913d082010-02-26 05:48:29 +000031
David Hendricks57b75242015-11-20 15:54:07 -080032enum id_type {
33 RDID,
34 RDID4,
35 REMS,
36// RES1, /* TODO */
37 RES2,
38 NUM_ID_TYPES,
39};
40
41static struct {
42 int is_cached;
43 unsigned char bytes[4]; /* enough to hold largest ID type */
44} id_cache[NUM_ID_TYPES];
45
46void clear_spi_id_cache(void)
47{
48 memset(id_cache, 0, sizeof(id_cache));
49 return;
50}
51
Souvik Ghoshd75cd672016-06-17 14:21:39 -070052static int spi_rdid(struct flashctx *flash, unsigned char *readarr, int bytes)
snelson8913d082010-02-26 05:48:29 +000053{
krause2eb76212011-01-17 07:50:42 +000054 static const unsigned char cmd[JEDEC_RDID_OUTSIZE] = { JEDEC_RDID };
snelson8913d082010-02-26 05:48:29 +000055 int ret;
56 int i;
57
Souvik Ghoshd75cd672016-06-17 14:21:39 -070058 ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr);
snelson8913d082010-02-26 05:48:29 +000059 if (ret)
60 return ret;
snelsonfc007bb2010-03-24 23:14:32 +000061 msg_cspew("RDID returned");
snelson8913d082010-02-26 05:48:29 +000062 for (i = 0; i < bytes; i++)
snelsonfc007bb2010-03-24 23:14:32 +000063 msg_cspew(" 0x%02x", readarr[i]);
64 msg_cspew(". ");
snelson8913d082010-02-26 05:48:29 +000065 return 0;
66}
67
Souvik Ghoshd75cd672016-06-17 14:21:39 -070068static int spi_rems(struct flashctx *flash, unsigned char *readarr)
snelson8913d082010-02-26 05:48:29 +000069{
70 unsigned char cmd[JEDEC_REMS_OUTSIZE] = { JEDEC_REMS, 0, 0, 0 };
71 uint32_t readaddr;
72 int ret;
73
Souvik Ghoshd75cd672016-06-17 14:21:39 -070074 ret = spi_send_command(flash, sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
snelson8913d082010-02-26 05:48:29 +000075 if (ret == SPI_INVALID_ADDRESS) {
76 /* Find the lowest even address allowed for reads. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -070077 readaddr = (spi_get_valid_read_addr(flash) + 1) & ~1;
snelson8913d082010-02-26 05:48:29 +000078 cmd[1] = (readaddr >> 16) & 0xff,
79 cmd[2] = (readaddr >> 8) & 0xff,
80 cmd[3] = (readaddr >> 0) & 0xff,
Souvik Ghoshd75cd672016-06-17 14:21:39 -070081 ret = spi_send_command(flash, sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
snelson8913d082010-02-26 05:48:29 +000082 }
83 if (ret)
84 return ret;
stefanct371e7e82011-07-07 19:56:58 +000085 msg_cspew("REMS returned 0x%02x 0x%02x. ", readarr[0], readarr[1]);
snelson8913d082010-02-26 05:48:29 +000086 return 0;
87}
88
Souvik Ghoshd75cd672016-06-17 14:21:39 -070089static int spi_res(struct flashctx *flash, unsigned char *readarr, int bytes)
snelson8913d082010-02-26 05:48:29 +000090{
91 unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, 0, 0, 0 };
92 uint32_t readaddr;
93 int ret;
hailfingercb0564e2010-06-20 10:39:33 +000094 int i;
snelson8913d082010-02-26 05:48:29 +000095
Souvik Ghoshd75cd672016-06-17 14:21:39 -070096 ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr);
snelson8913d082010-02-26 05:48:29 +000097 if (ret == SPI_INVALID_ADDRESS) {
98 /* Find the lowest even address allowed for reads. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -070099 readaddr = (spi_get_valid_read_addr(flash) + 1) & ~1;
snelson8913d082010-02-26 05:48:29 +0000100 cmd[1] = (readaddr >> 16) & 0xff,
101 cmd[2] = (readaddr >> 8) & 0xff,
102 cmd[3] = (readaddr >> 0) & 0xff,
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700103 ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr);
snelson8913d082010-02-26 05:48:29 +0000104 }
105 if (ret)
106 return ret;
hailfingercb0564e2010-06-20 10:39:33 +0000107 msg_cspew("RES returned");
108 for (i = 0; i < bytes; i++)
109 msg_cspew(" 0x%02x", readarr[i]);
110 msg_cspew(". ");
snelson8913d082010-02-26 05:48:29 +0000111 return 0;
112}
113
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700114int spi_write_enable(struct flashctx *flash)
snelson8913d082010-02-26 05:48:29 +0000115{
krause2eb76212011-01-17 07:50:42 +0000116 static const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN };
snelson8913d082010-02-26 05:48:29 +0000117 int result;
118
119 /* Send WREN (Write Enable) */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700120 result = spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
snelson8913d082010-02-26 05:48:29 +0000121
122 if (result)
snelsonfc007bb2010-03-24 23:14:32 +0000123 msg_cerr("%s failed\n", __func__);
snelson8913d082010-02-26 05:48:29 +0000124
125 return result;
126}
127
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700128int spi_write_disable(struct flashctx *flash)
snelson8913d082010-02-26 05:48:29 +0000129{
krause2eb76212011-01-17 07:50:42 +0000130 static const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI };
snelson8913d082010-02-26 05:48:29 +0000131
132 /* Send WRDI (Write Disable) */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700133 return spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
snelson8913d082010-02-26 05:48:29 +0000134}
135
David Hendricks7f7c7112012-10-11 17:15:48 -0700136static void rdid_get_ids(unsigned char *readarr,
137 int bytes, uint32_t *id1, uint32_t *id2)
snelson8913d082010-02-26 05:48:29 +0000138{
snelson8913d082010-02-26 05:48:29 +0000139 if (!oddparity(readarr[0]))
snelsonfc007bb2010-03-24 23:14:32 +0000140 msg_cdbg("RDID byte 0 parity violation. ");
snelson8913d082010-02-26 05:48:29 +0000141
hailfingercb0564e2010-06-20 10:39:33 +0000142 /* Check if this is a continuation vendor ID.
143 * FIXME: Handle continuation device IDs.
144 */
snelson8913d082010-02-26 05:48:29 +0000145 if (readarr[0] == 0x7f) {
146 if (!oddparity(readarr[1]))
snelsonfc007bb2010-03-24 23:14:32 +0000147 msg_cdbg("RDID byte 1 parity violation. ");
David Hendricks7f7c7112012-10-11 17:15:48 -0700148 *id1 = (readarr[0] << 8) | readarr[1];
149 *id2 = readarr[2];
snelson8913d082010-02-26 05:48:29 +0000150 if (bytes > 3) {
David Hendricks7f7c7112012-10-11 17:15:48 -0700151 *id2 <<= 8;
152 *id2 |= readarr[3];
snelson8913d082010-02-26 05:48:29 +0000153 }
154 } else {
David Hendricks7f7c7112012-10-11 17:15:48 -0700155 *id1 = readarr[0];
156 *id2 = (readarr[1] << 8) | readarr[2];
snelson8913d082010-02-26 05:48:29 +0000157 }
David Hendricks7f7c7112012-10-11 17:15:48 -0700158}
snelson8913d082010-02-26 05:48:29 +0000159
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700160static int compare_id(struct flashctx *flash, uint32_t id1, uint32_t id2)
David Hendricks7f7c7112012-10-11 17:15:48 -0700161{
162 msg_cdbg("id1 0x%02x, id2 0x%02x\n", id1, id2);
snelson8913d082010-02-26 05:48:29 +0000163
Edward O'Callaghan71e23142019-03-03 23:08:22 +1100164 if (id1 == flash->chip->manufacture_id && id2 == flash->chip->model_id)
snelson8913d082010-02-26 05:48:29 +0000165 return 1;
snelson8913d082010-02-26 05:48:29 +0000166
167 /* Test if this is a pure vendor match. */
Patrick Georgif3fa2992017-02-02 16:24:44 +0100168 if (id1 == flash->chip->manufacture_id &&
169 GENERIC_DEVICE_ID == flash->chip->model_id)
snelson8913d082010-02-26 05:48:29 +0000170 return 1;
171
172 /* Test if there is any vendor ID. */
Patrick Georgif3fa2992017-02-02 16:24:44 +0100173 if (GENERIC_MANUF_ID == flash->chip->manufacture_id &&
snelson8913d082010-02-26 05:48:29 +0000174 id1 != 0xff)
175 return 1;
176
177 return 0;
178}
179
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700180int probe_spi_rdid(struct flashctx *flash)
snelson8913d082010-02-26 05:48:29 +0000181{
David Hendricks57b75242015-11-20 15:54:07 -0800182 uint32_t id1, id2;
David Hendricks7f7c7112012-10-11 17:15:48 -0700183
David Hendricks57b75242015-11-20 15:54:07 -0800184 if (!id_cache[RDID].is_cached) {
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700185 if (spi_rdid(flash, id_cache[RDID].bytes, 3))
David Hendricks7f7c7112012-10-11 17:15:48 -0700186 return 0;
David Hendricks57b75242015-11-20 15:54:07 -0800187 id_cache[RDID].is_cached = 1;
David Hendricks7f7c7112012-10-11 17:15:48 -0700188 }
189
David Hendricks57b75242015-11-20 15:54:07 -0800190 rdid_get_ids(id_cache[RDID].bytes, 3, &id1, &id2);
David Hendricks7f7c7112012-10-11 17:15:48 -0700191 return compare_id(flash, id1, id2);
snelson8913d082010-02-26 05:48:29 +0000192}
193
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700194int probe_spi_rdid4(struct flashctx *flash)
snelson8913d082010-02-26 05:48:29 +0000195{
David Hendricks57b75242015-11-20 15:54:07 -0800196 uint32_t id1, id2;
David Hendricks7f7c7112012-10-11 17:15:48 -0700197
hailfingercb0564e2010-06-20 10:39:33 +0000198 /* Some SPI controllers do not support commands with writecnt=1 and
199 * readcnt=4.
200 */
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100201 switch (spi_master->type) {
hailfinger90c7d542010-05-31 15:27:27 +0000202#if CONFIG_INTERNAL == 1
hailfinger324a9cc2010-05-26 01:45:41 +0000203#if defined(__i386__) || defined(__x86_64__)
hailfingercb0564e2010-06-20 10:39:33 +0000204 case SPI_CONTROLLER_IT87XX:
snelson8913d082010-02-26 05:48:29 +0000205 case SPI_CONTROLLER_WBSIO:
hailfingercb0564e2010-06-20 10:39:33 +0000206 msg_cinfo("4 byte RDID not supported on this SPI controller\n");
hailfingercb0564e2010-06-20 10:39:33 +0000207 break;
snelson8913d082010-02-26 05:48:29 +0000208#endif
hailfinger324a9cc2010-05-26 01:45:41 +0000209#endif
snelson8913d082010-02-26 05:48:29 +0000210 default:
David Hendricks7f7c7112012-10-11 17:15:48 -0700211 break;
snelson8913d082010-02-26 05:48:29 +0000212 }
213
David Hendricks57b75242015-11-20 15:54:07 -0800214 if (!id_cache[RDID4].is_cached) {
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700215 if (spi_rdid(flash, id_cache[RDID4].bytes, 4))
David Hendricks7f7c7112012-10-11 17:15:48 -0700216 return 0;
David Hendricks57b75242015-11-20 15:54:07 -0800217 id_cache[RDID4].is_cached = 1;
David Hendricks7f7c7112012-10-11 17:15:48 -0700218 }
David Hendricks57b75242015-11-20 15:54:07 -0800219
220 rdid_get_ids(id_cache[RDID4].bytes, 4, &id1, &id2);
David Hendricks7f7c7112012-10-11 17:15:48 -0700221 return compare_id(flash, id1, id2);
snelson8913d082010-02-26 05:48:29 +0000222}
223
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700224int probe_spi_rems(struct flashctx *flash)
snelson8913d082010-02-26 05:48:29 +0000225{
David Hendricks57b75242015-11-20 15:54:07 -0800226 uint32_t id1, id2;
snelson8913d082010-02-26 05:48:29 +0000227
David Hendricks57b75242015-11-20 15:54:07 -0800228 if (!id_cache[REMS].is_cached) {
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700229 if (spi_rems(flash, id_cache[REMS].bytes))
David Hendricks7f7c7112012-10-11 17:15:48 -0700230 return 0;
David Hendricks57b75242015-11-20 15:54:07 -0800231 id_cache[REMS].is_cached = 1;
stefanct9e6b98a2011-05-28 02:37:14 +0000232 }
snelson8913d082010-02-26 05:48:29 +0000233
David Hendricks57b75242015-11-20 15:54:07 -0800234 id1 = id_cache[REMS].bytes[0];
235 id2 = id_cache[REMS].bytes[1];
David Hendricks7f7c7112012-10-11 17:15:48 -0700236 return compare_id(flash, id1, id2);
snelson8913d082010-02-26 05:48:29 +0000237}
238
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700239int probe_spi_res1(struct flashctx *flash)
snelson8913d082010-02-26 05:48:29 +0000240{
krause2eb76212011-01-17 07:50:42 +0000241 static const unsigned char allff[] = {0xff, 0xff, 0xff};
242 static const unsigned char all00[] = {0x00, 0x00, 0x00};
snelson8913d082010-02-26 05:48:29 +0000243 unsigned char readarr[3];
244 uint32_t id2;
snelson8913d082010-02-26 05:48:29 +0000245
hailfinger59a83572010-05-28 17:07:57 +0000246 /* We only want one-byte RES if RDID and REMS are unusable. */
247
snelson8913d082010-02-26 05:48:29 +0000248 /* Check if RDID is usable and does not return 0xff 0xff 0xff or
249 * 0x00 0x00 0x00. In that case, RES is pointless.
250 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700251 if (!spi_rdid(flash, readarr, 3) && memcmp(readarr, allff, 3) &&
snelson8913d082010-02-26 05:48:29 +0000252 memcmp(readarr, all00, 3)) {
253 msg_cdbg("Ignoring RES in favour of RDID.\n");
254 return 0;
255 }
256 /* Check if REMS is usable and does not return 0xff 0xff or
257 * 0x00 0x00. In that case, RES is pointless.
258 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700259 if (!spi_rems(flash, readarr) && memcmp(readarr, allff, JEDEC_REMS_INSIZE) &&
snelson8913d082010-02-26 05:48:29 +0000260 memcmp(readarr, all00, JEDEC_REMS_INSIZE)) {
261 msg_cdbg("Ignoring RES in favour of REMS.\n");
262 return 0;
263 }
264
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700265 if (spi_res(flash, readarr, 1)) {
snelson8913d082010-02-26 05:48:29 +0000266 return 0;
stefanct9e6b98a2011-05-28 02:37:14 +0000267 }
snelson8913d082010-02-26 05:48:29 +0000268
snelson8913d082010-02-26 05:48:29 +0000269 id2 = readarr[0];
hailfinger59a83572010-05-28 17:07:57 +0000270
snelsonfc007bb2010-03-24 23:14:32 +0000271 msg_cdbg("%s: id 0x%x\n", __func__, id2);
hailfinger59a83572010-05-28 17:07:57 +0000272
Patrick Georgif3fa2992017-02-02 16:24:44 +0100273 if (id2 != flash->chip->model_id)
snelson8913d082010-02-26 05:48:29 +0000274 return 0;
275
snelson8913d082010-02-26 05:48:29 +0000276 return 1;
277}
278
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700279int probe_spi_res2(struct flashctx *flash)
hailfinger59a83572010-05-28 17:07:57 +0000280{
hailfinger59a83572010-05-28 17:07:57 +0000281 uint32_t id1, id2;
282
David Hendricks57b75242015-11-20 15:54:07 -0800283 if (!id_cache[RES2].is_cached) {
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700284 if (spi_res(flash, id_cache[RES2].bytes, 2))
David Hendricks57b75242015-11-20 15:54:07 -0800285 return 0;
286 id_cache[RES2].is_cached = 1;
stefanct9e6b98a2011-05-28 02:37:14 +0000287 }
hailfinger59a83572010-05-28 17:07:57 +0000288
David Hendricks57b75242015-11-20 15:54:07 -0800289 id1 = id_cache[RES2].bytes[0];
290 id2 = id_cache[RES2].bytes[1];
hailfinger59a83572010-05-28 17:07:57 +0000291 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
292
Patrick Georgif3fa2992017-02-02 16:24:44 +0100293 if (id1 != flash->chip->manufacture_id || id2 != flash->chip->model_id)
hailfinger59a83572010-05-28 17:07:57 +0000294 return 0;
295
hailfinger59a83572010-05-28 17:07:57 +0000296 return 1;
297}
298
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000299static int spi_poll_wip(struct flashctx *const flash, const unsigned int poll_delay)
300{
301 /* FIXME: We can't tell if spi_read_status_register() failed. */
302 /* FIXME: We don't time out. */
303 while (spi_read_status_register(flash) & SPI_SR_WIP)
304 programmer_delay(poll_delay);
305 /* FIXME: Check the status register for errors. */
306 return 0;
307}
308
Nico Huber4c8a9562017-10-15 11:20:58 +0200309/**
310 * Execute WREN plus another one byte `op`, optionally poll WIP afterwards.
311 *
312 * @param flash the flash chip's context
313 * @param op the operation to execute
314 * @param poll_delay interval in us for polling WIP, don't poll if zero
315 * @return 0 on success, non-zero otherwise
316 */
317static int spi_simple_write_cmd(struct flashctx *const flash, const uint8_t op, const unsigned int poll_delay)
snelson8913d082010-02-26 05:48:29 +0000318{
snelson8913d082010-02-26 05:48:29 +0000319 struct spi_command cmds[] = {
320 {
Nico Huber4c8a9562017-10-15 11:20:58 +0200321 .writecnt = 1,
322 .writearr = (const unsigned char[]){ JEDEC_WREN },
snelson8913d082010-02-26 05:48:29 +0000323 }, {
Nico Huber4c8a9562017-10-15 11:20:58 +0200324 .writecnt = 1,
325 .writearr = (const unsigned char[]){ op },
326 },
327 NULL_SPI_CMD,
328 };
snelson8913d082010-02-26 05:48:29 +0000329
Nico Huber4c8a9562017-10-15 11:20:58 +0200330 const int result = spi_send_multicommand(flash, cmds);
snelson8913d082010-02-26 05:48:29 +0000331 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000332 msg_cerr("%s failed during command execution\n", __func__);
snelson8913d082010-02-26 05:48:29 +0000333 return result;
334 }
335 /* Wait until the Write-In-Progress bit is cleared.
336 * This usually takes 1-85 s, so wait in 1 s steps.
337 */
Nico Huber4c8a9562017-10-15 11:20:58 +0200338
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000339 const int status = poll_delay ? spi_poll_wip(flash, poll_delay) : 0;
340
341 return result ? result : status;
342}
343
Edward O'Callaghana74ffcd2019-06-17 14:59:55 +1000344static int spi_set_extended_address(struct flashctx *const flash, const uint8_t addr_high)
345{
346 if (flash->address_high_byte != addr_high &&
347 spi_write_extended_address_register(flash, addr_high))
348 return -1;
349 flash->address_high_byte = addr_high;
350 return 0;
351}
352
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000353static int spi_prepare_address(struct flashctx *const flash, uint8_t cmd_buf[],
354 const bool native_4ba, const unsigned int addr)
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000355{
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000356 if (native_4ba || flash->in_4ba_mode) {
Edward O'Callaghana74ffcd2019-06-17 14:59:55 +1000357 cmd_buf[1] = (addr >> 24) & 0xff;
358 cmd_buf[2] = (addr >> 16) & 0xff;
359 cmd_buf[3] = (addr >> 8) & 0xff;
360 cmd_buf[4] = (addr >> 0) & 0xff;
361 return 4;
362 } else {
363 if (flash->chip->feature_bits & FEATURE_4BA_EXT_ADDR) {
364 if (spi_set_extended_address(flash, addr >> 24))
365 return -1;
366 } else {
367 if (addr >> 24)
368 return -1;
369 }
370 cmd_buf[1] = (addr >> 16) & 0xff;
371 cmd_buf[2] = (addr >> 8) & 0xff;
372 cmd_buf[3] = (addr >> 0) & 0xff;
373 return 3;
374 }
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000375}
376
377/**
378 * Execute WREN plus another `op` that takes an address and
379 * optional data, poll WIP afterwards.
380 *
381 * @param flash the flash chip's context
382 * @param op the operation to execute
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000383 * @param native_4ba where `op` always takes a 4-byte address
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000384 * @param addr the address parameter to `op`
385 * @param out_bytes bytes to send after the address,
386 * may be NULL if and only if `out_bytes` is 0
387 * @param out_bytes number of bytes to send, 256 at most, may be zero
388 * @param poll_delay interval in us for polling WIP
389 * @return 0 on success, non-zero otherwise
390 */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000391static int spi_write_cmd(struct flashctx *const flash, const uint8_t op,
392 const bool native_4ba, const unsigned int addr,
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000393 const uint8_t *const out_bytes, const size_t out_len,
394 const unsigned int poll_delay)
395{
396 uint8_t cmd[1 + JEDEC_MAX_ADDR_LEN + 256];
397 struct spi_command cmds[] = {
398 {
399 .writecnt = 1,
400 .writearr = (const unsigned char[]){ JEDEC_WREN },
401 }, {
402 .writearr = cmd,
403 },
404 NULL_SPI_CMD,
405 };
406
407 cmd[0] = op;
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000408 const int addr_len = spi_prepare_address(flash, cmd, native_4ba, addr);
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000409 if (addr_len < 0)
410 return 1;
411
412 if (1 + addr_len + out_len > sizeof(cmd)) {
413 msg_cerr("%s called for too long a write\n", __func__);
414 return 1;
415 }
416
417 memcpy(cmd + 1 + addr_len, out_bytes, out_len);
418 cmds[1].writecnt = 1 + addr_len + out_len;
419
420 const int result = spi_send_multicommand(flash, cmds);
421 if (result)
422 msg_cerr("%s failed during command execution at address 0x%x\n", __func__, addr);
423
424 const int status = spi_poll_wip(flash, poll_delay);
425
426 return result ? result : status;
Nico Huber4c8a9562017-10-15 11:20:58 +0200427}
428
429int spi_chip_erase_60(struct flashctx *flash)
430{
431 /* This usually takes 1-85s, so wait in 1s steps. */
432 return spi_simple_write_cmd(flash, 0x60, 1000 * 1000);
433}
434
435int spi_chip_erase_62(struct flashctx *flash)
436{
437 /* This usually takes 2-5s, so wait in 100ms steps. */
438 return spi_simple_write_cmd(flash, 0x62, 100 * 1000);
439}
440
441int spi_chip_erase_c7(struct flashctx *flash)
442{
443 /* This usually takes 1-85s, so wait in 1s steps. */
444 return spi_simple_write_cmd(flash, 0xc7, 1000 * 1000);
snelson8913d082010-02-26 05:48:29 +0000445}
446
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700447int spi_block_erase_52(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000448{
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000449 /* This usually takes 100-4000ms, so wait in 100ms steps. */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000450 return spi_write_cmd(flash, 0x52, false, addr, NULL, 0, 100 * 1000);
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000451}
snelson8913d082010-02-26 05:48:29 +0000452
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000453/* Block size is usually
454 * 32M (one die) for Micron
455 */
456int spi_block_erase_c4(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
457{
458 /* This usually takes 240-480s, so wait in 500ms steps. */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000459 return spi_write_cmd(flash, 0xc4, false, addr, NULL, 0, 500 * 1000);
snelson8913d082010-02-26 05:48:29 +0000460}
461
462/* Block size is usually
463 * 64k for Macronix
464 * 32k for SST
465 * 4-32k non-uniform for EON
466 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700467int spi_block_erase_d8(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000468{
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000469 /* This usually takes 100-4000ms, so wait in 100ms steps. */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000470 return spi_write_cmd(flash, 0xd8, false, addr, NULL, 0, 100 * 1000);
snelson8913d082010-02-26 05:48:29 +0000471}
472
473/* Block size is usually
474 * 4k for PMC
475 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700476int spi_block_erase_d7(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000477{
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000478 /* This usually takes 100-4000ms, so wait in 100ms steps. */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000479 return spi_write_cmd(flash, 0xd7, false, addr, NULL, 0, 100 * 1000);
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000480}
snelson8913d082010-02-26 05:48:29 +0000481
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000482/* Page erase (usually 256B blocks) */
483int spi_block_erase_db(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
484{
485 /* This takes up to 20ms usually (on worn out devices
486 up to the 0.5s range), so wait in 1ms steps. */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000487 return spi_write_cmd(flash, 0xdb, false, addr, NULL, 0, 1 * 1000);
snelson8913d082010-02-26 05:48:29 +0000488}
489
snelson8913d082010-02-26 05:48:29 +0000490/* Sector size is usually 4k, though Macronix eliteflash has 64k */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700491int spi_block_erase_20(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000492{
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000493 /* This usually takes 15-800ms, so wait in 10ms steps. */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000494 return spi_write_cmd(flash, 0x20, false, addr, NULL, 0, 10 * 1000);
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000495}
snelson8913d082010-02-26 05:48:29 +0000496
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000497int spi_block_erase_50(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
498{
499 /* This usually takes 10ms, so wait in 1ms steps. */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000500 return spi_write_cmd(flash, 0x50, false, addr, NULL, 0, 1 * 1000);
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000501}
Stefan Reinauercce56d52010-11-22 18:22:21 -0800502
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000503int spi_block_erase_81(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
504{
505 /* This usually takes 8ms, so wait in 1ms steps. */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000506 return spi_write_cmd(flash, 0x81, false, addr, NULL, 0, 1 * 1000);
snelson8913d082010-02-26 05:48:29 +0000507}
508
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700509int spi_block_erase_60(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000510{
Patrick Georgif3fa2992017-02-02 16:24:44 +0100511 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
snelsonfc007bb2010-03-24 23:14:32 +0000512 msg_cerr("%s called with incorrect arguments\n",
snelson8913d082010-02-26 05:48:29 +0000513 __func__);
514 return -1;
515 }
516 return spi_chip_erase_60(flash);
517}
518
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700519int spi_block_erase_c7(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000520{
Patrick Georgif3fa2992017-02-02 16:24:44 +0100521 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
snelsonfc007bb2010-03-24 23:14:32 +0000522 msg_cerr("%s called with incorrect arguments\n",
snelson8913d082010-02-26 05:48:29 +0000523 __func__);
524 return -1;
525 }
526 return spi_chip_erase_c7(flash);
527}
528
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700529int spi_write_status_register_wren(const struct flashctx *flash, int status)
hailfingerc33d4732010-07-29 13:09:18 +0000530{
531 int result;
hailfingeree9ee132010-10-08 00:37:55 +0000532 int i = 0;
hailfingerc33d4732010-07-29 13:09:18 +0000533 struct spi_command cmds[] = {
534 {
535 /* WRSR requires either EWSR or WREN depending on chip type. */
536 .writecnt = JEDEC_WREN_OUTSIZE,
537 .writearr = (const unsigned char[]){ JEDEC_WREN },
538 .readcnt = 0,
539 .readarr = NULL,
540 }, {
541 .writecnt = JEDEC_WRSR_OUTSIZE,
542 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
543 .readcnt = 0,
544 .readarr = NULL,
545 }, {
546 .writecnt = 0,
547 .writearr = NULL,
548 .readcnt = 0,
549 .readarr = NULL,
550 }};
551
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700552 result = spi_send_multicommand(flash, cmds);
hailfingerc33d4732010-07-29 13:09:18 +0000553 if (result) {
554 msg_cerr("%s failed during command execution\n",
555 __func__);
hailfingeree9ee132010-10-08 00:37:55 +0000556 /* No point in waiting for the command to complete if execution
557 * failed.
558 */
559 return result;
hailfingerc33d4732010-07-29 13:09:18 +0000560 }
hailfingeree9ee132010-10-08 00:37:55 +0000561 /* WRSR performs a self-timed erase before the changes take effect.
562 * This may take 50-85 ms in most cases, and some chips apparently
563 * allow running RDSR only once. Therefore pick an initial delay of
564 * 100 ms, then wait in 10 ms steps until a total of 5 s have elapsed.
565 */
hailfingerc33d4732010-07-29 13:09:18 +0000566 programmer_delay(100 * 1000);
Edward O'Callaghan8b5e4732019-03-05 15:27:53 +1100567 while (spi_read_status_register(flash) & SPI_SR_WIP) {
hailfingeree9ee132010-10-08 00:37:55 +0000568 if (++i > 490) {
569 msg_cerr("Error: WIP bit after WRSR never cleared\n");
570 return TIMEOUT_ERROR;
571 }
572 programmer_delay(10 * 1000);
573 }
574 return 0;
hailfingerc33d4732010-07-29 13:09:18 +0000575}
576
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700577int spi_byte_program(struct flashctx *flash, unsigned int addr, uint8_t databyte)
snelson8913d082010-02-26 05:48:29 +0000578{
579 int result;
580 struct spi_command cmds[] = {
581 {
582 .writecnt = JEDEC_WREN_OUTSIZE,
583 .writearr = (const unsigned char[]){ JEDEC_WREN },
584 .readcnt = 0,
585 .readarr = NULL,
586 }, {
587 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE,
588 .writearr = (const unsigned char[]){
589 JEDEC_BYTE_PROGRAM,
590 (addr >> 16) & 0xff,
591 (addr >> 8) & 0xff,
592 (addr & 0xff),
593 databyte
594 },
595 .readcnt = 0,
596 .readarr = NULL,
597 }, {
598 .writecnt = 0,
599 .writearr = NULL,
600 .readcnt = 0,
601 .readarr = NULL,
602 }};
603
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700604 result = spi_send_multicommand(flash, cmds);
snelson8913d082010-02-26 05:48:29 +0000605 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000606 msg_cerr("%s failed during command execution at address 0x%x\n",
snelson8913d082010-02-26 05:48:29 +0000607 __func__, addr);
608 }
609 return result;
610}
611
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000612static int spi_nbyte_program(struct flashctx *flash, unsigned int addr, const uint8_t *bytes, unsigned int len)
snelson8913d082010-02-26 05:48:29 +0000613{
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000614 const bool native_4ba = !!(flash->chip->feature_bits & FEATURE_4BA_WRITE);
615 const uint8_t op = native_4ba ? JEDEC_BYTE_PROGRAM_4BA : JEDEC_BYTE_PROGRAM;
616 return spi_write_cmd(flash, op, native_4ba, addr, bytes, len, 10);
snelson8913d082010-02-26 05:48:29 +0000617}
618
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700619int spi_nbyte_read(struct flashctx *flash, unsigned int address, uint8_t *bytes, unsigned int len)
snelson8913d082010-02-26 05:48:29 +0000620{
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000621 const bool native_4ba = !!(flash->chip->feature_bits & FEATURE_4BA_READ);
622 uint8_t cmd[1 + JEDEC_MAX_ADDR_LEN] = { native_4ba ? JEDEC_READ_4BA : JEDEC_READ, };
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000623
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000624 const int addr_len = spi_prepare_address(flash, cmd, native_4ba, address);
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000625 if (addr_len < 0)
626 return 1;
snelson8913d082010-02-26 05:48:29 +0000627
628 /* Send Read */
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000629 return spi_send_command(flash, 1 + addr_len, len, cmd, bytes);
snelson8913d082010-02-26 05:48:29 +0000630}
631
632/*
hailfinger39d159a2010-05-21 23:09:42 +0000633 * Read a part of the flash chip.
hailfingerc7d06c62010-07-14 16:19:05 +0000634 * FIXME: Use the chunk code from Michael Karcher instead.
snelson8913d082010-02-26 05:48:29 +0000635 * Each page is read separately in chunks with a maximum size of chunksize.
636 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700637int spi_read_chunked(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len, unsigned int chunksize)
snelson8913d082010-02-26 05:48:29 +0000638{
David Hendricks1ed1d352011-11-23 17:54:37 -0800639 int rc = 0, chunk_status = 0;
stefanctc5eb8a92011-11-23 09:13:48 +0000640 unsigned int i, j, starthere, lenhere, toread;
Patrick Georgif3fa2992017-02-02 16:24:44 +0100641 unsigned int page_size = flash->chip->page_size;
snelson8913d082010-02-26 05:48:29 +0000642
643 /* Warning: This loop has a very unusual condition and body.
644 * The loop needs to go through each page with at least one affected
645 * byte. The lowest page number is (start / page_size) since that
646 * division rounds down. The highest page number we want is the page
647 * where the last byte of the range lives. That last byte has the
648 * address (start + len - 1), thus the highest page number is
649 * (start + len - 1) / page_size. Since we want to include that last
650 * page as well, the loop condition uses <=.
651 */
652 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
653 /* Byte position of the first byte in the range in this page. */
654 /* starthere is an offset to the base address of the chip. */
655 starthere = max(start, i * page_size);
656 /* Length of bytes in the range in this page. */
657 lenhere = min(start + len, (i + 1) * page_size) - starthere;
658 for (j = 0; j < lenhere; j += chunksize) {
659 toread = min(chunksize, lenhere - j);
Boris Baykov1a2f5322016-06-11 18:29:00 +0200660 chunk_status = (flash->chip->feature_bits & FEATURE_4BA_SUPPORT) == 0
661 ? spi_nbyte_read(flash, starthere + j, buf + starthere - start + j, toread)
662 : flash->chip->four_bytes_addr_funcs.read_nbyte(flash, starthere + j,
663 buf + starthere - start + j, toread);
David Hendricks1ed1d352011-11-23 17:54:37 -0800664 if (chunk_status) {
665 if (ignore_error(chunk_status)) {
666 /* fill this chunk with 0xff bytes and
667 let caller know about the error */
668 memset(buf + starthere - start + j, 0xff, toread);
669 rc = chunk_status;
670 chunk_status = 0;
671 continue;
672 } else {
673 rc = chunk_status;
674 break;
675 }
676 }
snelson8913d082010-02-26 05:48:29 +0000677 }
David Hendricks1ed1d352011-11-23 17:54:37 -0800678 if (chunk_status)
snelson8913d082010-02-26 05:48:29 +0000679 break;
680 }
681
682 return rc;
683}
684
685/*
Duncan Laurie06ffd522015-10-26 12:56:08 -0700686 * Read a part of the flash chip.
687 * Ignore pages and read the data continuously, the only bound is the chunksize.
688 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700689int 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 -0700690{
691 int rc = 0;
692 unsigned int i;
693
694 for (i = start; i < (start + len); i += chunksize) {
David Hendricks37370ec2015-11-24 14:38:17 -0800695 int chunk_status = 0;
696 unsigned int toread = min(chunksize, start + len - i);
697
Duncan Laurie20613a92018-10-10 08:43:12 -0700698 if (flash->chip->feature_bits & FEATURE_4BA_SUPPORT) {
699 chunk_status = flash->chip->four_bytes_addr_funcs.read_nbyte(
700 flash, i, buf + (i - start), toread);
701 } else {
702 chunk_status = spi_nbyte_read(flash, i, buf + (i - start), toread);
703 }
704
David Hendricks37370ec2015-11-24 14:38:17 -0800705 if (chunk_status) {
706 if (ignore_error(chunk_status)) {
707 /* fill this chunk with 0xff bytes and
708 let caller know about the error */
709 memset(buf + (i - start), 0xff, toread);
710 rc = chunk_status;
711 continue;
712 } else {
713 rc = chunk_status;
714 break;
715 }
716 }
Duncan Laurie06ffd522015-10-26 12:56:08 -0700717 }
David Hendricks37370ec2015-11-24 14:38:17 -0800718
Duncan Laurie06ffd522015-10-26 12:56:08 -0700719 return rc;
720}
721
722/*
hailfinger39d159a2010-05-21 23:09:42 +0000723 * Write a part of the flash chip.
hailfingerc7d06c62010-07-14 16:19:05 +0000724 * FIXME: Use the chunk code from Michael Karcher instead.
hailfinger39d159a2010-05-21 23:09:42 +0000725 * Each page is written separately in chunks with a maximum size of chunksize.
726 */
Patrick Georgiab8353e2017-02-03 18:32:01 +0100727int 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 +0000728{
729 int rc = 0;
stefanctc5eb8a92011-11-23 09:13:48 +0000730 unsigned int i, j, starthere, lenhere, towrite;
hailfinger39d159a2010-05-21 23:09:42 +0000731 /* FIXME: page_size is the wrong variable. We need max_writechunk_size
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700732 * in struct flashctx to do this properly. All chips using
hailfinger39d159a2010-05-21 23:09:42 +0000733 * spi_chip_write_256 have page_size set to max_writechunk_size, so
734 * we're OK for now.
735 */
Patrick Georgif3fa2992017-02-02 16:24:44 +0100736 unsigned int page_size = flash->chip->page_size;
hailfinger39d159a2010-05-21 23:09:42 +0000737
738 /* Warning: This loop has a very unusual condition and body.
739 * The loop needs to go through each page with at least one affected
740 * byte. The lowest page number is (start / page_size) since that
741 * division rounds down. The highest page number we want is the page
742 * where the last byte of the range lives. That last byte has the
743 * address (start + len - 1), thus the highest page number is
744 * (start + len - 1) / page_size. Since we want to include that last
745 * page as well, the loop condition uses <=.
746 */
747 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
748 /* Byte position of the first byte in the range in this page. */
749 /* starthere is an offset to the base address of the chip. */
750 starthere = max(start, i * page_size);
751 /* Length of bytes in the range in this page. */
752 lenhere = min(start + len, (i + 1) * page_size) - starthere;
753 for (j = 0; j < lenhere; j += chunksize) {
754 towrite = min(chunksize, lenhere - j);
Boris Baykov1a2f5322016-06-11 18:29:00 +0200755 rc = (flash->chip->feature_bits & FEATURE_4BA_SUPPORT) == 0
756 ? spi_nbyte_program(flash, starthere + j, buf + starthere - start + j, towrite)
757 : flash->chip->four_bytes_addr_funcs.program_nbyte(flash, starthere + j,
758 buf + starthere - start + j, towrite);
hailfinger39d159a2010-05-21 23:09:42 +0000759 if (rc)
760 break;
Edward O'Callaghan8b5e4732019-03-05 15:27:53 +1100761 while (spi_read_status_register(flash) & SPI_SR_WIP)
hailfinger39d159a2010-05-21 23:09:42 +0000762 programmer_delay(10);
763 }
764 if (rc)
765 break;
766 }
767
768 return rc;
769}
770
771/*
snelson8913d082010-02-26 05:48:29 +0000772 * Program chip using byte programming. (SLOW!)
773 * This is for chips which can only handle one byte writes
774 * and for chips where memory mapped programming is impossible
775 * (e.g. due to size constraints in IT87* for over 512 kB)
776 */
hailfingerc7d06c62010-07-14 16:19:05 +0000777/* real chunksize is 1, logical chunksize is 1 */
Patrick Georgiab8353e2017-02-03 18:32:01 +0100778int spi_chip_write_1(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
snelson8913d082010-02-26 05:48:29 +0000779{
stefanctc5eb8a92011-11-23 09:13:48 +0000780 unsigned int i;
781 int result = 0;
snelson8913d082010-02-26 05:48:29 +0000782
hailfingerc7d06c62010-07-14 16:19:05 +0000783 for (i = start; i < start + len; i++) {
Boris Baykov1a2f5322016-06-11 18:29:00 +0200784 result = (flash->chip->feature_bits & FEATURE_4BA_SUPPORT) == 0
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000785 ? spi_nbyte_program(flash, i, buf + i - start, 1)
Boris Baykov1a2f5322016-06-11 18:29:00 +0200786 : flash->chip->four_bytes_addr_funcs.program_byte(flash, i, buf[i - start]);
snelson8913d082010-02-26 05:48:29 +0000787 if (result)
788 return 1;
Edward O'Callaghan8b5e4732019-03-05 15:27:53 +1100789 while (spi_read_status_register(flash) & SPI_SR_WIP)
snelson8913d082010-02-26 05:48:29 +0000790 programmer_delay(10);
791 }
792
793 return 0;
794}
795
Patrick Georgiab8353e2017-02-03 18:32:01 +0100796int spi_aai_write(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
hailfingerc7d06c62010-07-14 16:19:05 +0000797{
798 uint32_t pos = start;
snelson8913d082010-02-26 05:48:29 +0000799 int result;
hailfinger19db0922010-06-20 10:41:35 +0000800 unsigned char cmd[JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE] = {
801 JEDEC_AAI_WORD_PROGRAM,
802 };
snelson8913d082010-02-26 05:48:29 +0000803
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100804 switch (spi_master->type) {
hailfinger90c7d542010-05-31 15:27:27 +0000805#if CONFIG_INTERNAL == 1
hailfinger324a9cc2010-05-26 01:45:41 +0000806#if defined(__i386__) || defined(__x86_64__)
hailfinger19db0922010-06-20 10:41:35 +0000807 case SPI_CONTROLLER_IT87XX:
snelson8913d082010-02-26 05:48:29 +0000808 case SPI_CONTROLLER_WBSIO:
hailfingerc7d06c62010-07-14 16:19:05 +0000809 msg_perr("%s: impossible with this SPI controller,"
snelson8913d082010-02-26 05:48:29 +0000810 " degrading to byte program\n", __func__);
hailfinger71e1bd42010-10-13 22:26:56 +0000811 return spi_chip_write_1(flash, buf, start, len);
snelson8913d082010-02-26 05:48:29 +0000812#endif
hailfinger324a9cc2010-05-26 01:45:41 +0000813#endif
snelson8913d082010-02-26 05:48:29 +0000814 default:
815 break;
816 }
hailfinger19db0922010-06-20 10:41:35 +0000817
hailfingerc7d06c62010-07-14 16:19:05 +0000818 /* The even start address and even length requirements can be either
819 * honored outside this function, or we can call spi_byte_program
820 * for the first and/or last byte and use AAI for the rest.
hailfinger71e1bd42010-10-13 22:26:56 +0000821 * FIXME: Move this to generic code.
hailfingerc7d06c62010-07-14 16:19:05 +0000822 */
hailfinger19db0922010-06-20 10:41:35 +0000823 /* The data sheet requires a start address with the low bit cleared. */
hailfingerc7d06c62010-07-14 16:19:05 +0000824 if (start % 2) {
hailfinger19db0922010-06-20 10:41:35 +0000825 msg_cerr("%s: start address not even! Please report a bug at "
826 "flashrom@flashrom.org\n", __func__);
hailfinger71e1bd42010-10-13 22:26:56 +0000827 if (spi_chip_write_1(flash, buf, start, start % 2))
828 return SPI_GENERIC_ERROR;
829 pos += start % 2;
830 /* Do not return an error for now. */
831 //return SPI_GENERIC_ERROR;
hailfinger19db0922010-06-20 10:41:35 +0000832 }
833 /* The data sheet requires total AAI write length to be even. */
834 if (len % 2) {
835 msg_cerr("%s: total write length not even! Please report a "
836 "bug at flashrom@flashrom.org\n", __func__);
hailfinger71e1bd42010-10-13 22:26:56 +0000837 /* Do not return an error for now. */
838 //return SPI_GENERIC_ERROR;
hailfinger19db0922010-06-20 10:41:35 +0000839 }
840
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000841 result = spi_write_cmd(flash, JEDEC_AAI_WORD_PROGRAM, false, start, buf + pos - start, 2, 10);
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000842 if (result)
Edward O'Callaghan633cbd62019-06-17 15:43:56 +1000843 goto bailout;
hailfinger19db0922010-06-20 10:41:35 +0000844
845 /* We already wrote 2 bytes in the multicommand step. */
846 pos += 2;
847
hailfinger71e1bd42010-10-13 22:26:56 +0000848 /* Are there at least two more bytes to write? */
849 while (pos < start + len - 1) {
hailfingerdef852d2010-10-27 22:07:11 +0000850 cmd[1] = buf[pos++ - start];
851 cmd[2] = buf[pos++ - start];
Edward O'Callaghan633cbd62019-06-17 15:43:56 +1000852 result = spi_send_command(flash, JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE, 0, cmd, NULL);
853 if (result) {
854 msg_cerr("%s failed during followup AAI command execution: %d\n", __func__, result);
855 goto bailout;
856 }
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000857 if (spi_poll_wip(flash, 10))
858 goto bailout;
hailfinger19db0922010-06-20 10:41:35 +0000859 }
860
hailfinger71e1bd42010-10-13 22:26:56 +0000861 /* Use WRDI to exit AAI mode. This needs to be done before issuing any
862 * other non-AAI command.
863 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700864 spi_write_disable(flash);
hailfinger71e1bd42010-10-13 22:26:56 +0000865
866 /* Write remaining byte (if any). */
867 if (pos < start + len) {
hailfingerdef852d2010-10-27 22:07:11 +0000868 if (spi_chip_write_1(flash, buf + pos - start, pos, pos % 2))
hailfinger71e1bd42010-10-13 22:26:56 +0000869 return SPI_GENERIC_ERROR;
870 pos += pos % 2;
871 }
872
snelson8913d082010-02-26 05:48:29 +0000873 return 0;
Edward O'Callaghan633cbd62019-06-17 15:43:56 +1000874
875bailout:
876 spi_write_disable(flash);
877 return SPI_GENERIC_ERROR;
snelson8913d082010-02-26 05:48:29 +0000878}