blob: 1dad467eb91899da3eeec73bc6e5e2336912acd8 [file] [log] [blame]
snelson8913d082010-02-26 05:48:29 +00001/*
2 * This file is part of the flashrom project.
3 *
hailfinger39d159a2010-05-21 23:09:42 +00004 * Copyright (C) 2007, 2008, 2009, 2010 Carl-Daniel Hailfinger
snelson8913d082010-02-26 05:48:29 +00005 * Copyright (C) 2008 coresystems GmbH
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
snelson8913d082010-02-26 05:48:29 +000015 */
16
17/*
18 * Contains the common SPI chip driver functions
19 */
20
Nico Huber4c8a9562017-10-15 11:20:58 +020021#include <stddef.h>
snelson8913d082010-02-26 05:48:29 +000022#include <string.h>
Edward O'Callaghan031831d2019-06-19 16:27:43 +100023#include <stdbool.h>
snelson8913d082010-02-26 05:48:29 +000024#include "flash.h"
25#include "flashchips.h"
26#include "chipdrivers.h"
hailfinger428f6852010-07-27 22:41:39 +000027#include "programmer.h"
snelson8913d082010-02-26 05:48:29 +000028#include "spi.h"
29
David Hendricks57b75242015-11-20 15:54:07 -080030enum id_type {
31 RDID,
32 RDID4,
33 REMS,
David Hendricks57b75242015-11-20 15:54:07 -080034 RES2,
Nikolai Artemiev4702c7c2020-08-31 12:49:50 +100035 RES3,
David Hendricks57b75242015-11-20 15:54:07 -080036 NUM_ID_TYPES,
37};
38
39static struct {
40 int is_cached;
41 unsigned char bytes[4]; /* enough to hold largest ID type */
42} id_cache[NUM_ID_TYPES];
43
44void clear_spi_id_cache(void)
45{
46 memset(id_cache, 0, sizeof(id_cache));
47 return;
48}
49
Souvik Ghoshd75cd672016-06-17 14:21:39 -070050static int spi_rdid(struct flashctx *flash, unsigned char *readarr, int bytes)
snelson8913d082010-02-26 05:48:29 +000051{
krause2eb76212011-01-17 07:50:42 +000052 static const unsigned char cmd[JEDEC_RDID_OUTSIZE] = { JEDEC_RDID };
snelson8913d082010-02-26 05:48:29 +000053 int ret;
54 int i;
55
Souvik Ghoshd75cd672016-06-17 14:21:39 -070056 ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr);
snelson8913d082010-02-26 05:48:29 +000057 if (ret)
58 return ret;
snelsonfc007bb2010-03-24 23:14:32 +000059 msg_cspew("RDID returned");
snelson8913d082010-02-26 05:48:29 +000060 for (i = 0; i < bytes; i++)
snelsonfc007bb2010-03-24 23:14:32 +000061 msg_cspew(" 0x%02x", readarr[i]);
62 msg_cspew(". ");
snelson8913d082010-02-26 05:48:29 +000063 return 0;
64}
65
Souvik Ghoshd75cd672016-06-17 14:21:39 -070066static int spi_rems(struct flashctx *flash, unsigned char *readarr)
snelson8913d082010-02-26 05:48:29 +000067{
Edward O'Callaghandfb71542020-05-14 18:41:42 +100068 static const unsigned char cmd[JEDEC_REMS_OUTSIZE] = { JEDEC_REMS, };
snelson8913d082010-02-26 05:48:29 +000069 int ret;
70
Souvik Ghoshd75cd672016-06-17 14:21:39 -070071 ret = spi_send_command(flash, sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
snelson8913d082010-02-26 05:48:29 +000072 if (ret)
73 return ret;
stefanct371e7e82011-07-07 19:56:58 +000074 msg_cspew("REMS returned 0x%02x 0x%02x. ", readarr[0], readarr[1]);
snelson8913d082010-02-26 05:48:29 +000075 return 0;
76}
77
Souvik Ghoshd75cd672016-06-17 14:21:39 -070078static int spi_res(struct flashctx *flash, unsigned char *readarr, int bytes)
snelson8913d082010-02-26 05:48:29 +000079{
Edward O'Callaghandfb71542020-05-14 18:41:42 +100080 static const unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, };
snelson8913d082010-02-26 05:48:29 +000081 int ret;
hailfingercb0564e2010-06-20 10:39:33 +000082 int i;
snelson8913d082010-02-26 05:48:29 +000083
Souvik Ghoshd75cd672016-06-17 14:21:39 -070084 ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr);
snelson8913d082010-02-26 05:48:29 +000085 if (ret)
86 return ret;
hailfingercb0564e2010-06-20 10:39:33 +000087 msg_cspew("RES returned");
88 for (i = 0; i < bytes; i++)
89 msg_cspew(" 0x%02x", readarr[i]);
90 msg_cspew(". ");
snelson8913d082010-02-26 05:48:29 +000091 return 0;
92}
93
Souvik Ghoshd75cd672016-06-17 14:21:39 -070094int spi_write_enable(struct flashctx *flash)
snelson8913d082010-02-26 05:48:29 +000095{
krause2eb76212011-01-17 07:50:42 +000096 static const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN };
snelson8913d082010-02-26 05:48:29 +000097 int result;
98
99 /* Send WREN (Write Enable) */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700100 result = spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
snelson8913d082010-02-26 05:48:29 +0000101
102 if (result)
snelsonfc007bb2010-03-24 23:14:32 +0000103 msg_cerr("%s failed\n", __func__);
snelson8913d082010-02-26 05:48:29 +0000104
105 return result;
106}
107
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700108int spi_write_disable(struct flashctx *flash)
snelson8913d082010-02-26 05:48:29 +0000109{
krause2eb76212011-01-17 07:50:42 +0000110 static const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI };
snelson8913d082010-02-26 05:48:29 +0000111
112 /* Send WRDI (Write Disable) */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700113 return spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
snelson8913d082010-02-26 05:48:29 +0000114}
115
Edward O'Callaghan6d86b102020-10-23 22:57:00 +1100116static void rdid_get_ids(unsigned char *readarr, int bytes,
117 uint32_t *id1, uint32_t *id2)
snelson8913d082010-02-26 05:48:29 +0000118{
snelson8913d082010-02-26 05:48:29 +0000119 if (!oddparity(readarr[0]))
snelsonfc007bb2010-03-24 23:14:32 +0000120 msg_cdbg("RDID byte 0 parity violation. ");
snelson8913d082010-02-26 05:48:29 +0000121
hailfingercb0564e2010-06-20 10:39:33 +0000122 /* Check if this is a continuation vendor ID.
123 * FIXME: Handle continuation device IDs.
124 */
snelson8913d082010-02-26 05:48:29 +0000125 if (readarr[0] == 0x7f) {
126 if (!oddparity(readarr[1]))
snelsonfc007bb2010-03-24 23:14:32 +0000127 msg_cdbg("RDID byte 1 parity violation. ");
David Hendricks7f7c7112012-10-11 17:15:48 -0700128 *id1 = (readarr[0] << 8) | readarr[1];
129 *id2 = readarr[2];
snelson8913d082010-02-26 05:48:29 +0000130 if (bytes > 3) {
David Hendricks7f7c7112012-10-11 17:15:48 -0700131 *id2 <<= 8;
132 *id2 |= readarr[3];
snelson8913d082010-02-26 05:48:29 +0000133 }
134 } else {
David Hendricks7f7c7112012-10-11 17:15:48 -0700135 *id1 = readarr[0];
136 *id2 = (readarr[1] << 8) | readarr[2];
snelson8913d082010-02-26 05:48:29 +0000137 }
David Hendricks7f7c7112012-10-11 17:15:48 -0700138}
snelson8913d082010-02-26 05:48:29 +0000139
Edward O'Callaghan4cfb22f2020-10-15 12:21:04 +1100140static int compare_id(const struct flashctx *flash, uint32_t id1, uint32_t id2)
David Hendricks7f7c7112012-10-11 17:15:48 -0700141{
Edward O'Callaghan4cfb22f2020-10-15 12:21:04 +1100142 const struct flashchip *chip = flash->chip;
snelson8913d082010-02-26 05:48:29 +0000143
Edward O'Callaghan4cfb22f2020-10-15 12:21:04 +1100144 msg_cdbg("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
145 if (id1 == chip->manufacture_id && id2 == chip->model_id)
snelson8913d082010-02-26 05:48:29 +0000146 return 1;
snelson8913d082010-02-26 05:48:29 +0000147
148 /* Test if this is a pure vendor match. */
Edward O'Callaghan4cfb22f2020-10-15 12:21:04 +1100149 if (id1 == chip->manufacture_id && GENERIC_DEVICE_ID == chip->model_id)
snelson8913d082010-02-26 05:48:29 +0000150 return 1;
151
152 /* Test if there is any vendor ID. */
Urja Rannikko544a3a72015-06-22 23:59:15 +0000153 if (GENERIC_MANUF_ID == chip->manufacture_id && id1 != 0xff && id1 != 0x00)
snelson8913d082010-02-26 05:48:29 +0000154 return 1;
155
156 return 0;
157}
158
Edward O'Callaghanc2a12a92020-05-14 17:58:32 +1000159static int probe_spi_rdid_generic(struct flashctx *flash, int bytes)
snelson8913d082010-02-26 05:48:29 +0000160{
David Hendricks57b75242015-11-20 15:54:07 -0800161 uint32_t id1, id2;
Edward O'Callaghanc2a12a92020-05-14 17:58:32 +1000162 enum id_type idty = bytes == 3 ? RDID : RDID4;
David Hendricks7f7c7112012-10-11 17:15:48 -0700163
Edward O'Callaghanc2a12a92020-05-14 17:58:32 +1000164 if (!id_cache[idty].is_cached) {
165 const int ret = spi_rdid(flash, id_cache[idty].bytes, bytes);
166 if (ret == SPI_INVALID_LENGTH)
167 msg_cinfo("%d byte RDID not supported on this SPI controller\n", bytes);
168 if (ret)
David Hendricks7f7c7112012-10-11 17:15:48 -0700169 return 0;
Edward O'Callaghanc2a12a92020-05-14 17:58:32 +1000170 id_cache[idty].is_cached = 1;
David Hendricks7f7c7112012-10-11 17:15:48 -0700171 }
172
Edward O'Callaghanc2a12a92020-05-14 17:58:32 +1000173 rdid_get_ids(id_cache[idty].bytes, bytes, &id1, &id2);
David Hendricks7f7c7112012-10-11 17:15:48 -0700174 return compare_id(flash, id1, id2);
snelson8913d082010-02-26 05:48:29 +0000175}
176
Edward O'Callaghanc2a12a92020-05-14 17:58:32 +1000177int probe_spi_rdid(struct flashctx *flash)
178{
179 return probe_spi_rdid_generic(flash, 3);
180}
181
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700182int probe_spi_rdid4(struct flashctx *flash)
snelson8913d082010-02-26 05:48:29 +0000183{
Edward O'Callaghanc2a12a92020-05-14 17:58:32 +1000184 return probe_spi_rdid_generic(flash, 4);
snelson8913d082010-02-26 05:48:29 +0000185}
186
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700187int probe_spi_rems(struct flashctx *flash)
snelson8913d082010-02-26 05:48:29 +0000188{
David Hendricks57b75242015-11-20 15:54:07 -0800189 uint32_t id1, id2;
snelson8913d082010-02-26 05:48:29 +0000190
David Hendricks57b75242015-11-20 15:54:07 -0800191 if (!id_cache[REMS].is_cached) {
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700192 if (spi_rems(flash, id_cache[REMS].bytes))
David Hendricks7f7c7112012-10-11 17:15:48 -0700193 return 0;
David Hendricks57b75242015-11-20 15:54:07 -0800194 id_cache[REMS].is_cached = 1;
stefanct9e6b98a2011-05-28 02:37:14 +0000195 }
snelson8913d082010-02-26 05:48:29 +0000196
David Hendricks57b75242015-11-20 15:54:07 -0800197 id1 = id_cache[REMS].bytes[0];
198 id2 = id_cache[REMS].bytes[1];
David Hendricks7f7c7112012-10-11 17:15:48 -0700199 return compare_id(flash, id1, id2);
snelson8913d082010-02-26 05:48:29 +0000200}
201
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700202int probe_spi_res1(struct flashctx *flash)
snelson8913d082010-02-26 05:48:29 +0000203{
krause2eb76212011-01-17 07:50:42 +0000204 static const unsigned char allff[] = {0xff, 0xff, 0xff};
205 static const unsigned char all00[] = {0x00, 0x00, 0x00};
snelson8913d082010-02-26 05:48:29 +0000206 unsigned char readarr[3];
207 uint32_t id2;
snelson8913d082010-02-26 05:48:29 +0000208
hailfinger59a83572010-05-28 17:07:57 +0000209 /* We only want one-byte RES if RDID and REMS are unusable. */
210
snelson8913d082010-02-26 05:48:29 +0000211 /* Check if RDID is usable and does not return 0xff 0xff 0xff or
212 * 0x00 0x00 0x00. In that case, RES is pointless.
213 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700214 if (!spi_rdid(flash, readarr, 3) && memcmp(readarr, allff, 3) &&
snelson8913d082010-02-26 05:48:29 +0000215 memcmp(readarr, all00, 3)) {
216 msg_cdbg("Ignoring RES in favour of RDID.\n");
217 return 0;
218 }
219 /* Check if REMS is usable and does not return 0xff 0xff or
220 * 0x00 0x00. In that case, RES is pointless.
221 */
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100222 if (!spi_rems(flash, readarr) &&
223 memcmp(readarr, allff, JEDEC_REMS_INSIZE) &&
snelson8913d082010-02-26 05:48:29 +0000224 memcmp(readarr, all00, JEDEC_REMS_INSIZE)) {
225 msg_cdbg("Ignoring RES in favour of REMS.\n");
226 return 0;
227 }
228
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700229 if (spi_res(flash, readarr, 1)) {
snelson8913d082010-02-26 05:48:29 +0000230 return 0;
stefanct9e6b98a2011-05-28 02:37:14 +0000231 }
snelson8913d082010-02-26 05:48:29 +0000232
snelson8913d082010-02-26 05:48:29 +0000233 id2 = readarr[0];
hailfinger59a83572010-05-28 17:07:57 +0000234
snelsonfc007bb2010-03-24 23:14:32 +0000235 msg_cdbg("%s: id 0x%x\n", __func__, id2);
hailfinger59a83572010-05-28 17:07:57 +0000236
Patrick Georgif3fa2992017-02-02 16:24:44 +0100237 if (id2 != flash->chip->model_id)
snelson8913d082010-02-26 05:48:29 +0000238 return 0;
239
snelson8913d082010-02-26 05:48:29 +0000240 return 1;
241}
242
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700243int probe_spi_res2(struct flashctx *flash)
hailfinger59a83572010-05-28 17:07:57 +0000244{
hailfinger59a83572010-05-28 17:07:57 +0000245 uint32_t id1, id2;
246
David Hendricks57b75242015-11-20 15:54:07 -0800247 if (!id_cache[RES2].is_cached) {
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700248 if (spi_res(flash, id_cache[RES2].bytes, 2))
David Hendricks57b75242015-11-20 15:54:07 -0800249 return 0;
250 id_cache[RES2].is_cached = 1;
stefanct9e6b98a2011-05-28 02:37:14 +0000251 }
hailfinger59a83572010-05-28 17:07:57 +0000252
David Hendricks57b75242015-11-20 15:54:07 -0800253 id1 = id_cache[RES2].bytes[0];
254 id2 = id_cache[RES2].bytes[1];
hailfinger59a83572010-05-28 17:07:57 +0000255 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
256
Patrick Georgif3fa2992017-02-02 16:24:44 +0100257 if (id1 != flash->chip->manufacture_id || id2 != flash->chip->model_id)
hailfinger59a83572010-05-28 17:07:57 +0000258 return 0;
259
hailfinger59a83572010-05-28 17:07:57 +0000260 return 1;
261}
262
Nikolai Artemiev4702c7c2020-08-31 12:49:50 +1000263int probe_spi_res3(struct flashctx *flash)
264{
265 uint32_t id1, id2;
266
267 if (!id_cache[RES3].is_cached) {
268 if (spi_res(flash, id_cache[RES3].bytes, 3))
269 return 0;
270 id_cache[RES3].is_cached = 1;
271 }
272
273 id1 = (id_cache[RES3].bytes[0] << 8) | id_cache[RES3].bytes[1];
274 id2 = id_cache[RES3].bytes[3];
275 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
276
277 if (id1 != flash->chip->manufacture_id || id2 != flash->chip->model_id)
278 return 0;
279
280 return 1;
281}
282
283/* Only used for some Atmel chips. */
284int probe_spi_at25f(struct flashctx *flash)
285{
286 static const unsigned char cmd[AT25F_RDID_OUTSIZE] = { AT25F_RDID };
287 unsigned char readarr[AT25F_RDID_INSIZE];
288 uint32_t id1;
289 uint32_t id2;
290
291 if (spi_send_command(flash, sizeof(cmd), sizeof(readarr), cmd, readarr))
292 return 0;
293
294 id1 = readarr[0];
295 id2 = readarr[1];
296
297 msg_cdbg("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
298
299 if (id1 == flash->chip->manufacture_id && id2 == flash->chip->model_id)
300 return 1;
301
302 return 0;
303}
304
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000305static int spi_poll_wip(struct flashctx *const flash, const unsigned int poll_delay)
306{
307 /* FIXME: We can't tell if spi_read_status_register() failed. */
308 /* FIXME: We don't time out. */
309 while (spi_read_status_register(flash) & SPI_SR_WIP)
310 programmer_delay(poll_delay);
311 /* FIXME: Check the status register for errors. */
312 return 0;
313}
314
Nico Huber4c8a9562017-10-15 11:20:58 +0200315/**
316 * Execute WREN plus another one byte `op`, optionally poll WIP afterwards.
317 *
318 * @param flash the flash chip's context
319 * @param op the operation to execute
320 * @param poll_delay interval in us for polling WIP, don't poll if zero
321 * @return 0 on success, non-zero otherwise
322 */
323static int spi_simple_write_cmd(struct flashctx *const flash, const uint8_t op, const unsigned int poll_delay)
snelson8913d082010-02-26 05:48:29 +0000324{
snelson8913d082010-02-26 05:48:29 +0000325 struct spi_command cmds[] = {
326 {
Edward O'Callaghan8cb48f72020-10-13 14:45:59 +1100327 .readarr = 0,
Nico Huber4c8a9562017-10-15 11:20:58 +0200328 .writecnt = 1,
329 .writearr = (const unsigned char[]){ JEDEC_WREN },
snelson8913d082010-02-26 05:48:29 +0000330 }, {
Edward O'Callaghan8cb48f72020-10-13 14:45:59 +1100331 .readarr = 0,
Nico Huber4c8a9562017-10-15 11:20:58 +0200332 .writecnt = 1,
333 .writearr = (const unsigned char[]){ op },
334 },
335 NULL_SPI_CMD,
336 };
snelson8913d082010-02-26 05:48:29 +0000337
Nico Huber4c8a9562017-10-15 11:20:58 +0200338 const int result = spi_send_multicommand(flash, cmds);
snelson8913d082010-02-26 05:48:29 +0000339 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000340 msg_cerr("%s failed during command execution\n", __func__);
snelson8913d082010-02-26 05:48:29 +0000341 return result;
342 }
343 /* Wait until the Write-In-Progress bit is cleared.
344 * This usually takes 1-85 s, so wait in 1 s steps.
345 */
Nico Huber4c8a9562017-10-15 11:20:58 +0200346
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000347 const int status = poll_delay ? spi_poll_wip(flash, poll_delay) : 0;
348
349 return result ? result : status;
350}
351
Edward O'Callaghan99974452020-10-13 13:28:33 +1100352static int spi_write_extended_address_register(struct flashctx *const flash, const uint8_t regdata)
353{
354 const uint8_t op = flash->chip->wrea_override ? : JEDEC_WRITE_EXT_ADDR_REG;
355 struct spi_command cmds[] = {
356 {
357 .readarr = 0,
358 .writecnt = 1,
359 .writearr = (const unsigned char[]){ JEDEC_WREN },
360 }, {
361 .readarr = 0,
362 .writecnt = 2,
363 .writearr = (const unsigned char[]){ op, regdata },
364 },
365 NULL_SPI_CMD,
366 };
367
368 const int result = spi_send_multicommand(flash, cmds);
369 if (result)
370 msg_cerr("%s failed during command execution\n", __func__);
371 return result;
372}
373
374int spi_set_extended_address(struct flashctx *const flash, const uint8_t addr_high)
Edward O'Callaghana74ffcd2019-06-17 14:59:55 +1000375{
376 if (flash->address_high_byte != addr_high &&
377 spi_write_extended_address_register(flash, addr_high))
378 return -1;
379 flash->address_high_byte = addr_high;
380 return 0;
381}
382
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000383static int spi_prepare_address(struct flashctx *const flash, uint8_t cmd_buf[],
384 const bool native_4ba, const unsigned int addr)
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000385{
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000386 if (native_4ba || flash->in_4ba_mode) {
Edward O'Callaghana6673bd2019-06-24 15:22:28 +1000387 if (!spi_master_4ba(flash)) {
388 msg_cwarn("4-byte address requested but master can't handle 4-byte addresses.\n");
389 return -1;
390 }
Edward O'Callaghana74ffcd2019-06-17 14:59:55 +1000391 cmd_buf[1] = (addr >> 24) & 0xff;
392 cmd_buf[2] = (addr >> 16) & 0xff;
393 cmd_buf[3] = (addr >> 8) & 0xff;
394 cmd_buf[4] = (addr >> 0) & 0xff;
395 return 4;
396 } else {
397 if (flash->chip->feature_bits & FEATURE_4BA_EXT_ADDR) {
398 if (spi_set_extended_address(flash, addr >> 24))
399 return -1;
Edward O'Callaghana6673bd2019-06-24 15:22:28 +1000400 } else if (addr >> 24) {
401 msg_cerr("Can't handle 4-byte address for opcode '0x%02x'\n"
402 "with this chip/programmer combination.\n", cmd_buf[0]);
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100403 return -1;
Edward O'Callaghana74ffcd2019-06-17 14:59:55 +1000404 }
405 cmd_buf[1] = (addr >> 16) & 0xff;
406 cmd_buf[2] = (addr >> 8) & 0xff;
407 cmd_buf[3] = (addr >> 0) & 0xff;
408 return 3;
409 }
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000410}
411
412/**
413 * Execute WREN plus another `op` that takes an address and
414 * optional data, poll WIP afterwards.
415 *
416 * @param flash the flash chip's context
417 * @param op the operation to execute
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100418 * @param native_4ba whether `op` always takes a 4-byte address
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000419 * @param addr the address parameter to `op`
420 * @param out_bytes bytes to send after the address,
421 * may be NULL if and only if `out_bytes` is 0
422 * @param out_bytes number of bytes to send, 256 at most, may be zero
423 * @param poll_delay interval in us for polling WIP
424 * @return 0 on success, non-zero otherwise
425 */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000426static int spi_write_cmd(struct flashctx *const flash, const uint8_t op,
427 const bool native_4ba, const unsigned int addr,
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000428 const uint8_t *const out_bytes, const size_t out_len,
429 const unsigned int poll_delay)
430{
431 uint8_t cmd[1 + JEDEC_MAX_ADDR_LEN + 256];
432 struct spi_command cmds[] = {
433 {
Edward O'Callaghan8cb48f72020-10-13 14:45:59 +1100434 .readarr = 0,
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000435 .writecnt = 1,
436 .writearr = (const unsigned char[]){ JEDEC_WREN },
437 }, {
Edward O'Callaghan8cb48f72020-10-13 14:45:59 +1100438 .readarr = 0,
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000439 .writearr = cmd,
440 },
441 NULL_SPI_CMD,
442 };
443
444 cmd[0] = op;
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000445 const int addr_len = spi_prepare_address(flash, cmd, native_4ba, addr);
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000446 if (addr_len < 0)
447 return 1;
448
449 if (1 + addr_len + out_len > sizeof(cmd)) {
450 msg_cerr("%s called for too long a write\n", __func__);
451 return 1;
452 }
Angel Pons6bfd9e62020-03-31 15:32:10 +0200453 if (!out_bytes && out_len > 0)
454 return 1;
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000455
456 memcpy(cmd + 1 + addr_len, out_bytes, out_len);
457 cmds[1].writecnt = 1 + addr_len + out_len;
458
459 const int result = spi_send_multicommand(flash, cmds);
460 if (result)
461 msg_cerr("%s failed during command execution at address 0x%x\n", __func__, addr);
462
463 const int status = spi_poll_wip(flash, poll_delay);
464
465 return result ? result : status;
Nico Huber4c8a9562017-10-15 11:20:58 +0200466}
467
Edward O'Callaghanb064cb62020-10-13 13:36:53 +1100468static int spi_chip_erase_60(struct flashctx *flash)
Nico Huber4c8a9562017-10-15 11:20:58 +0200469{
470 /* This usually takes 1-85s, so wait in 1s steps. */
471 return spi_simple_write_cmd(flash, 0x60, 1000 * 1000);
472}
473
Edward O'Callaghanb064cb62020-10-13 13:36:53 +1100474static int spi_chip_erase_62(struct flashctx *flash)
Nico Huber4c8a9562017-10-15 11:20:58 +0200475{
476 /* This usually takes 2-5s, so wait in 100ms steps. */
477 return spi_simple_write_cmd(flash, 0x62, 100 * 1000);
478}
479
Edward O'Callaghanb064cb62020-10-13 13:36:53 +1100480static int spi_chip_erase_c7(struct flashctx *flash)
Nico Huber4c8a9562017-10-15 11:20:58 +0200481{
482 /* This usually takes 1-85s, so wait in 1s steps. */
483 return spi_simple_write_cmd(flash, 0xc7, 1000 * 1000);
snelson8913d082010-02-26 05:48:29 +0000484}
485
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100486int spi_block_erase_52(struct flashctx *flash, unsigned int addr,
487 unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000488{
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000489 /* This usually takes 100-4000ms, so wait in 100ms steps. */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000490 return spi_write_cmd(flash, 0x52, false, addr, NULL, 0, 100 * 1000);
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000491}
snelson8913d082010-02-26 05:48:29 +0000492
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000493/* Block size is usually
494 * 32M (one die) for Micron
495 */
496int spi_block_erase_c4(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
497{
498 /* This usually takes 240-480s, so wait in 500ms steps. */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000499 return spi_write_cmd(flash, 0xc4, false, addr, NULL, 0, 500 * 1000);
snelson8913d082010-02-26 05:48:29 +0000500}
501
502/* Block size is usually
503 * 64k for Macronix
504 * 32k for SST
505 * 4-32k non-uniform for EON
506 */
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100507int spi_block_erase_d8(struct flashctx *flash, unsigned int addr,
508 unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000509{
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000510 /* This usually takes 100-4000ms, so wait in 100ms steps. */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000511 return spi_write_cmd(flash, 0xd8, false, addr, NULL, 0, 100 * 1000);
snelson8913d082010-02-26 05:48:29 +0000512}
513
514/* Block size is usually
515 * 4k for PMC
516 */
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100517int spi_block_erase_d7(struct flashctx *flash, unsigned int addr,
518 unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000519{
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000520 /* This usually takes 100-4000ms, so wait in 100ms steps. */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000521 return spi_write_cmd(flash, 0xd7, false, addr, NULL, 0, 100 * 1000);
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000522}
snelson8913d082010-02-26 05:48:29 +0000523
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000524/* Page erase (usually 256B blocks) */
525int spi_block_erase_db(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
526{
527 /* This takes up to 20ms usually (on worn out devices
528 up to the 0.5s range), so wait in 1ms steps. */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000529 return spi_write_cmd(flash, 0xdb, false, addr, NULL, 0, 1 * 1000);
snelson8913d082010-02-26 05:48:29 +0000530}
531
snelson8913d082010-02-26 05:48:29 +0000532/* Sector size is usually 4k, though Macronix eliteflash has 64k */
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100533int spi_block_erase_20(struct flashctx *flash, unsigned int addr,
534 unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000535{
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000536 /* This usually takes 15-800ms, so wait in 10ms steps. */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000537 return spi_write_cmd(flash, 0x20, false, addr, NULL, 0, 10 * 1000);
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000538}
snelson8913d082010-02-26 05:48:29 +0000539
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000540int spi_block_erase_50(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
541{
542 /* This usually takes 10ms, so wait in 1ms steps. */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000543 return spi_write_cmd(flash, 0x50, false, addr, NULL, 0, 1 * 1000);
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000544}
Stefan Reinauercce56d52010-11-22 18:22:21 -0800545
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000546int spi_block_erase_81(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
547{
548 /* This usually takes 8ms, so wait in 1ms steps. */
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000549 return spi_write_cmd(flash, 0x81, false, addr, NULL, 0, 1 * 1000);
snelson8913d082010-02-26 05:48:29 +0000550}
551
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100552int spi_block_erase_60(struct flashctx *flash, unsigned int addr,
553 unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000554{
Patrick Georgif3fa2992017-02-02 16:24:44 +0100555 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
snelsonfc007bb2010-03-24 23:14:32 +0000556 msg_cerr("%s called with incorrect arguments\n",
snelson8913d082010-02-26 05:48:29 +0000557 __func__);
558 return -1;
559 }
560 return spi_chip_erase_60(flash);
561}
562
Alan Green5d709732019-09-16 12:32:25 +1000563int spi_block_erase_62(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
564{
565 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
566 msg_cerr("%s called with incorrect arguments\n",
567 __func__);
568 return -1;
569 }
570 return spi_chip_erase_62(flash);
571}
572
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100573int spi_block_erase_c7(struct flashctx *flash, unsigned int addr,
574 unsigned int blocklen)
snelson8913d082010-02-26 05:48:29 +0000575{
Patrick Georgif3fa2992017-02-02 16:24:44 +0100576 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
snelsonfc007bb2010-03-24 23:14:32 +0000577 msg_cerr("%s called with incorrect arguments\n",
snelson8913d082010-02-26 05:48:29 +0000578 __func__);
579 return -1;
580 }
581 return spi_chip_erase_c7(flash);
582}
583
Edward O'Callaghan6d86b102020-10-23 22:57:00 +1100584/* Erase 4 KB of flash with 4-bytes address from ANY mode (3-bytes or 4-bytes) */
Edward O'Callaghan94934e82019-06-19 17:44:19 +1000585int spi_block_erase_21(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
586{
587 /* This usually takes 15-800ms, so wait in 10ms steps. */
588 return spi_write_cmd(flash, 0x21, true, addr, NULL, 0, 10 * 1000);
589}
590
Edward O'Callaghan6d86b102020-10-23 22:57:00 +1100591/* Erase 32 KB of flash with 4-bytes address from ANY mode (3-bytes or 4-bytes) */
Edward O'Callaghan94934e82019-06-19 17:44:19 +1000592int spi_block_erase_5c(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
593{
594 /* This usually takes 100-4000ms, so wait in 100ms steps. */
595 return spi_write_cmd(flash, 0x5c, true, addr, NULL, 0, 100 * 1000);
596}
597
Edward O'Callaghan6d86b102020-10-23 22:57:00 +1100598/* Erase 64 KB of flash with 4-bytes address from ANY mode (3-bytes or 4-bytes) */
Edward O'Callaghan94934e82019-06-19 17:44:19 +1000599int spi_block_erase_dc(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
600{
601 /* This usually takes 100-4000ms, so wait in 100ms steps. */
602 return spi_write_cmd(flash, 0xdc, true, addr, NULL, 0, 100 * 1000);
603}
604
Nikolai Artemieva66b6cd2020-08-31 18:07:13 +1000605erasefunc_t *spi_get_erasefn_from_opcode(uint8_t opcode)
606{
607 switch(opcode){
608 case 0xff:
609 case 0x00:
610 /* Not specified, assuming "not supported". */
611 return NULL;
612 case 0x20:
613 return &spi_block_erase_20;
614 case 0x21:
615 return &spi_block_erase_21;
616 case 0x50:
617 return &spi_block_erase_50;
618 case 0x52:
619 return &spi_block_erase_52;
620 case 0x5c:
621 return &spi_block_erase_5c;
622 case 0x60:
623 return &spi_block_erase_60;
624 case 0x62:
625 return &spi_block_erase_62;
626 case 0x81:
627 return &spi_block_erase_81;
628 case 0xc4:
629 return &spi_block_erase_c4;
630 case 0xc7:
631 return &spi_block_erase_c7;
632 case 0xd7:
633 return &spi_block_erase_d7;
634 case 0xd8:
635 return &spi_block_erase_d8;
636 case 0xdb:
637 return &spi_block_erase_db;
638 case 0xdc:
639 return &spi_block_erase_dc;
640 default:
641 msg_cinfo("%s: unknown erase opcode (0x%02x). Please report "
642 "this at flashrom@flashrom.org\n", __func__, opcode);
643 return NULL;
644 }
645}
646
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700647int spi_write_status_register_wren(const struct flashctx *flash, int status)
hailfingerc33d4732010-07-29 13:09:18 +0000648{
649 int result;
hailfingeree9ee132010-10-08 00:37:55 +0000650 int i = 0;
hailfingerc33d4732010-07-29 13:09:18 +0000651 struct spi_command cmds[] = {
652 {
653 /* WRSR requires either EWSR or WREN depending on chip type. */
654 .writecnt = JEDEC_WREN_OUTSIZE,
655 .writearr = (const unsigned char[]){ JEDEC_WREN },
656 .readcnt = 0,
657 .readarr = NULL,
658 }, {
659 .writecnt = JEDEC_WRSR_OUTSIZE,
660 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
661 .readcnt = 0,
662 .readarr = NULL,
663 }, {
664 .writecnt = 0,
665 .writearr = NULL,
666 .readcnt = 0,
667 .readarr = NULL,
668 }};
669
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700670 result = spi_send_multicommand(flash, cmds);
hailfingerc33d4732010-07-29 13:09:18 +0000671 if (result) {
672 msg_cerr("%s failed during command execution\n",
673 __func__);
hailfingeree9ee132010-10-08 00:37:55 +0000674 /* No point in waiting for the command to complete if execution
675 * failed.
676 */
677 return result;
hailfingerc33d4732010-07-29 13:09:18 +0000678 }
hailfingeree9ee132010-10-08 00:37:55 +0000679 /* WRSR performs a self-timed erase before the changes take effect.
680 * This may take 50-85 ms in most cases, and some chips apparently
681 * allow running RDSR only once. Therefore pick an initial delay of
682 * 100 ms, then wait in 10 ms steps until a total of 5 s have elapsed.
683 */
hailfingerc33d4732010-07-29 13:09:18 +0000684 programmer_delay(100 * 1000);
Edward O'Callaghan8b5e4732019-03-05 15:27:53 +1100685 while (spi_read_status_register(flash) & SPI_SR_WIP) {
hailfingeree9ee132010-10-08 00:37:55 +0000686 if (++i > 490) {
687 msg_cerr("Error: WIP bit after WRSR never cleared\n");
688 return TIMEOUT_ERROR;
689 }
690 programmer_delay(10 * 1000);
691 }
692 return 0;
hailfingerc33d4732010-07-29 13:09:18 +0000693}
694
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700695int spi_byte_program(struct flashctx *flash, unsigned int addr, uint8_t databyte)
snelson8913d082010-02-26 05:48:29 +0000696{
697 int result;
698 struct spi_command cmds[] = {
699 {
700 .writecnt = JEDEC_WREN_OUTSIZE,
701 .writearr = (const unsigned char[]){ JEDEC_WREN },
702 .readcnt = 0,
703 .readarr = NULL,
704 }, {
705 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE,
706 .writearr = (const unsigned char[]){
707 JEDEC_BYTE_PROGRAM,
708 (addr >> 16) & 0xff,
709 (addr >> 8) & 0xff,
710 (addr & 0xff),
711 databyte
712 },
713 .readcnt = 0,
714 .readarr = NULL,
715 }, {
716 .writecnt = 0,
717 .writearr = NULL,
718 .readcnt = 0,
719 .readarr = NULL,
720 }};
721
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700722 result = spi_send_multicommand(flash, cmds);
snelson8913d082010-02-26 05:48:29 +0000723 if (result) {
snelsonfc007bb2010-03-24 23:14:32 +0000724 msg_cerr("%s failed during command execution at address 0x%x\n",
snelson8913d082010-02-26 05:48:29 +0000725 __func__, addr);
726 }
727 return result;
728}
729
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000730static int spi_nbyte_program(struct flashctx *flash, unsigned int addr, const uint8_t *bytes, unsigned int len)
snelson8913d082010-02-26 05:48:29 +0000731{
Edward O'Callaghana6673bd2019-06-24 15:22:28 +1000732 const bool native_4ba = flash->chip->feature_bits & FEATURE_4BA_WRITE && spi_master_4ba(flash);
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000733 const uint8_t op = native_4ba ? JEDEC_BYTE_PROGRAM_4BA : JEDEC_BYTE_PROGRAM;
734 return spi_write_cmd(flash, op, native_4ba, addr, bytes, len, 10);
snelson8913d082010-02-26 05:48:29 +0000735}
736
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100737int spi_nbyte_read(struct flashctx *flash, unsigned int address, uint8_t *bytes,
738 unsigned int len)
snelson8913d082010-02-26 05:48:29 +0000739{
Edward O'Callaghana6673bd2019-06-24 15:22:28 +1000740 const bool native_4ba = flash->chip->feature_bits & FEATURE_4BA_READ && spi_master_4ba(flash);
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000741 uint8_t cmd[1 + JEDEC_MAX_ADDR_LEN] = { native_4ba ? JEDEC_READ_4BA : JEDEC_READ, };
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000742
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000743 const int addr_len = spi_prepare_address(flash, cmd, native_4ba, address);
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000744 if (addr_len < 0)
745 return 1;
snelson8913d082010-02-26 05:48:29 +0000746
747 /* Send Read */
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000748 return spi_send_command(flash, 1 + addr_len, len, cmd, bytes);
snelson8913d082010-02-26 05:48:29 +0000749}
750
751/*
hailfinger39d159a2010-05-21 23:09:42 +0000752 * Read a part of the flash chip.
hailfingerc7d06c62010-07-14 16:19:05 +0000753 * FIXME: Use the chunk code from Michael Karcher instead.
Edward O'Callaghand825ac02019-07-26 21:36:16 +1000754 * Each naturally aligned area is read separately in chunks with a maximum size of chunksize.
snelson8913d082010-02-26 05:48:29 +0000755 */
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100756int spi_read_chunked(struct flashctx *flash, uint8_t *buf, unsigned int start,
757 unsigned int len, unsigned int chunksize)
snelson8913d082010-02-26 05:48:29 +0000758{
David Hendricks1ed1d352011-11-23 17:54:37 -0800759 int rc = 0, chunk_status = 0;
stefanctc5eb8a92011-11-23 09:13:48 +0000760 unsigned int i, j, starthere, lenhere, toread;
Edward O'Callaghand825ac02019-07-26 21:36:16 +1000761 /* Limit for multi-die 4-byte-addressing chips. */
762 unsigned int area_size = min(flash->chip->total_size * 1024, 16 * 1024 * 1024);
snelson8913d082010-02-26 05:48:29 +0000763
764 /* Warning: This loop has a very unusual condition and body.
Edward O'Callaghand825ac02019-07-26 21:36:16 +1000765 * The loop needs to go through each area with at least one affected
766 * byte. The lowest area number is (start / area_size) since that
767 * division rounds down. The highest area number we want is the area
snelson8913d082010-02-26 05:48:29 +0000768 * where the last byte of the range lives. That last byte has the
Edward O'Callaghand825ac02019-07-26 21:36:16 +1000769 * address (start + len - 1), thus the highest area number is
770 * (start + len - 1) / area_size. Since we want to include that last
771 * area as well, the loop condition uses <=.
snelson8913d082010-02-26 05:48:29 +0000772 */
Edward O'Callaghand825ac02019-07-26 21:36:16 +1000773 for (i = start / area_size; i <= (start + len - 1) / area_size; i++) {
774 /* Byte position of the first byte in the range in this area. */
snelson8913d082010-02-26 05:48:29 +0000775 /* starthere is an offset to the base address of the chip. */
Edward O'Callaghand825ac02019-07-26 21:36:16 +1000776 starthere = max(start, i * area_size);
777 /* Length of bytes in the range in this area. */
778 lenhere = min(start + len, (i + 1) * area_size) - starthere;
snelson8913d082010-02-26 05:48:29 +0000779 for (j = 0; j < lenhere; j += chunksize) {
780 toread = min(chunksize, lenhere - j);
Edward O'Callaghan4fe3a972019-06-19 16:56:10 +1000781 chunk_status = spi_nbyte_read(flash, starthere + j, buf + starthere - start + j, toread);
David Hendricks1ed1d352011-11-23 17:54:37 -0800782 if (chunk_status) {
783 if (ignore_error(chunk_status)) {
784 /* fill this chunk with 0xff bytes and
785 let caller know about the error */
786 memset(buf + starthere - start + j, 0xff, toread);
787 rc = chunk_status;
788 chunk_status = 0;
789 continue;
790 } else {
791 rc = chunk_status;
792 break;
793 }
794 }
snelson8913d082010-02-26 05:48:29 +0000795 }
David Hendricks1ed1d352011-11-23 17:54:37 -0800796 if (chunk_status)
snelson8913d082010-02-26 05:48:29 +0000797 break;
798 }
799
800 return rc;
801}
802
803/*
hailfinger39d159a2010-05-21 23:09:42 +0000804 * Write a part of the flash chip.
hailfingerc7d06c62010-07-14 16:19:05 +0000805 * FIXME: Use the chunk code from Michael Karcher instead.
hailfinger39d159a2010-05-21 23:09:42 +0000806 * Each page is written separately in chunks with a maximum size of chunksize.
807 */
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100808int spi_write_chunked(struct flashctx *flash, const uint8_t *buf, unsigned int start,
809 unsigned int len, unsigned int chunksize)
hailfinger39d159a2010-05-21 23:09:42 +0000810{
stefanctc5eb8a92011-11-23 09:13:48 +0000811 unsigned int i, j, starthere, lenhere, towrite;
hailfinger39d159a2010-05-21 23:09:42 +0000812 /* FIXME: page_size is the wrong variable. We need max_writechunk_size
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700813 * in struct flashctx to do this properly. All chips using
hailfinger39d159a2010-05-21 23:09:42 +0000814 * spi_chip_write_256 have page_size set to max_writechunk_size, so
815 * we're OK for now.
816 */
Patrick Georgif3fa2992017-02-02 16:24:44 +0100817 unsigned int page_size = flash->chip->page_size;
hailfinger39d159a2010-05-21 23:09:42 +0000818
819 /* Warning: This loop has a very unusual condition and body.
820 * The loop needs to go through each page with at least one affected
821 * byte. The lowest page number is (start / page_size) since that
822 * division rounds down. The highest page number we want is the page
823 * where the last byte of the range lives. That last byte has the
824 * address (start + len - 1), thus the highest page number is
825 * (start + len - 1) / page_size. Since we want to include that last
826 * page as well, the loop condition uses <=.
827 */
828 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
829 /* Byte position of the first byte in the range in this page. */
830 /* starthere is an offset to the base address of the chip. */
831 starthere = max(start, i * page_size);
832 /* Length of bytes in the range in this page. */
833 lenhere = min(start + len, (i + 1) * page_size) - starthere;
834 for (j = 0; j < lenhere; j += chunksize) {
Edward O'Callaghan4fe3a972019-06-19 16:56:10 +1000835 int rc;
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100836
hailfinger39d159a2010-05-21 23:09:42 +0000837 towrite = min(chunksize, lenhere - j);
Edward O'Callaghan4fe3a972019-06-19 16:56:10 +1000838 rc = spi_nbyte_program(flash, starthere + j, buf + starthere - start + j, towrite);
hailfinger39d159a2010-05-21 23:09:42 +0000839 if (rc)
Edward O'Callaghan4fe3a972019-06-19 16:56:10 +1000840 return rc;
hailfinger39d159a2010-05-21 23:09:42 +0000841 }
hailfinger39d159a2010-05-21 23:09:42 +0000842 }
843
Edward O'Callaghan4fe3a972019-06-19 16:56:10 +1000844 return 0;
hailfinger39d159a2010-05-21 23:09:42 +0000845}
846
847/*
snelson8913d082010-02-26 05:48:29 +0000848 * Program chip using byte programming. (SLOW!)
849 * This is for chips which can only handle one byte writes
850 * and for chips where memory mapped programming is impossible
851 * (e.g. due to size constraints in IT87* for over 512 kB)
852 */
hailfingerc7d06c62010-07-14 16:19:05 +0000853/* real chunksize is 1, logical chunksize is 1 */
Patrick Georgiab8353e2017-02-03 18:32:01 +0100854int spi_chip_write_1(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
snelson8913d082010-02-26 05:48:29 +0000855{
stefanctc5eb8a92011-11-23 09:13:48 +0000856 unsigned int i;
snelson8913d082010-02-26 05:48:29 +0000857
hailfingerc7d06c62010-07-14 16:19:05 +0000858 for (i = start; i < start + len; i++) {
Edward O'Callaghan4fe3a972019-06-19 16:56:10 +1000859 if (spi_nbyte_program(flash, i, buf + i - start, 1))
snelson8913d082010-02-26 05:48:29 +0000860 return 1;
snelson8913d082010-02-26 05:48:29 +0000861 }
snelson8913d082010-02-26 05:48:29 +0000862 return 0;
863}
864
Edward O'Callaghaneeaac6b2020-10-12 19:51:56 +1100865int default_spi_write_aai(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
hailfingerc7d06c62010-07-14 16:19:05 +0000866{
867 uint32_t pos = start;
snelson8913d082010-02-26 05:48:29 +0000868 int result;
hailfinger19db0922010-06-20 10:41:35 +0000869 unsigned char cmd[JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE] = {
870 JEDEC_AAI_WORD_PROGRAM,
871 };
snelson8913d082010-02-26 05:48:29 +0000872
hailfingerc7d06c62010-07-14 16:19:05 +0000873 /* The even start address and even length requirements can be either
874 * honored outside this function, or we can call spi_byte_program
875 * for the first and/or last byte and use AAI for the rest.
hailfinger71e1bd42010-10-13 22:26:56 +0000876 * FIXME: Move this to generic code.
hailfingerc7d06c62010-07-14 16:19:05 +0000877 */
hailfinger19db0922010-06-20 10:41:35 +0000878 /* The data sheet requires a start address with the low bit cleared. */
hailfingerc7d06c62010-07-14 16:19:05 +0000879 if (start % 2) {
hailfinger19db0922010-06-20 10:41:35 +0000880 msg_cerr("%s: start address not even! Please report a bug at "
881 "flashrom@flashrom.org\n", __func__);
hailfinger71e1bd42010-10-13 22:26:56 +0000882 if (spi_chip_write_1(flash, buf, start, start % 2))
883 return SPI_GENERIC_ERROR;
884 pos += start % 2;
885 /* Do not return an error for now. */
886 //return SPI_GENERIC_ERROR;
hailfinger19db0922010-06-20 10:41:35 +0000887 }
888 /* The data sheet requires total AAI write length to be even. */
889 if (len % 2) {
890 msg_cerr("%s: total write length not even! Please report a "
891 "bug at flashrom@flashrom.org\n", __func__);
hailfinger71e1bd42010-10-13 22:26:56 +0000892 /* Do not return an error for now. */
893 //return SPI_GENERIC_ERROR;
hailfinger19db0922010-06-20 10:41:35 +0000894 }
895
Edward O'Callaghan031831d2019-06-19 16:27:43 +1000896 result = spi_write_cmd(flash, JEDEC_AAI_WORD_PROGRAM, false, start, buf + pos - start, 2, 10);
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000897 if (result)
Edward O'Callaghan633cbd62019-06-17 15:43:56 +1000898 goto bailout;
hailfinger19db0922010-06-20 10:41:35 +0000899
900 /* We already wrote 2 bytes in the multicommand step. */
901 pos += 2;
902
hailfinger71e1bd42010-10-13 22:26:56 +0000903 /* Are there at least two more bytes to write? */
904 while (pos < start + len - 1) {
hailfingerdef852d2010-10-27 22:07:11 +0000905 cmd[1] = buf[pos++ - start];
906 cmd[2] = buf[pos++ - start];
Edward O'Callaghan633cbd62019-06-17 15:43:56 +1000907 result = spi_send_command(flash, JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE, 0, cmd, NULL);
Edward O'Callaghan3eaadb02019-10-14 16:08:23 +1100908 if (result != 0) {
Edward O'Callaghan633cbd62019-06-17 15:43:56 +1000909 msg_cerr("%s failed during followup AAI command execution: %d\n", __func__, result);
910 goto bailout;
911 }
Edward O'Callaghane5190df2019-06-17 15:23:26 +1000912 if (spi_poll_wip(flash, 10))
913 goto bailout;
hailfinger19db0922010-06-20 10:41:35 +0000914 }
915
Edward O'Callaghaneeaac6b2020-10-12 19:51:56 +1100916 /* Use WRDI to exit AAI mode. This needs to be done before issuing any other non-AAI command. */
917 result = spi_write_disable(flash);
918 if (result != 0) {
919 msg_cerr("%s failed to disable AAI mode.\n", __func__);
920 return SPI_GENERIC_ERROR;
921 }
hailfinger71e1bd42010-10-13 22:26:56 +0000922
923 /* Write remaining byte (if any). */
924 if (pos < start + len) {
hailfingerdef852d2010-10-27 22:07:11 +0000925 if (spi_chip_write_1(flash, buf + pos - start, pos, pos % 2))
hailfinger71e1bd42010-10-13 22:26:56 +0000926 return SPI_GENERIC_ERROR;
hailfinger71e1bd42010-10-13 22:26:56 +0000927 }
928
snelson8913d082010-02-26 05:48:29 +0000929 return 0;
Edward O'Callaghan633cbd62019-06-17 15:43:56 +1000930
931bailout:
Edward O'Callaghaneeaac6b2020-10-12 19:51:56 +1100932 result = spi_write_disable(flash);
933 if (result != 0)
934 msg_cerr("%s failed to disable AAI mode.\n", __func__);
Edward O'Callaghan633cbd62019-06-17 15:43:56 +1000935 return SPI_GENERIC_ERROR;
snelson8913d082010-02-26 05:48:29 +0000936}
Edward O'Callaghan99974452020-10-13 13:28:33 +1100937
938static int spi_enter_exit_4ba(struct flashctx *const flash, const bool enter)
939{
940 const unsigned char cmd = enter ? JEDEC_ENTER_4_BYTE_ADDR_MODE : JEDEC_EXIT_4_BYTE_ADDR_MODE;
941 int ret = 1;
942
943 if (flash->chip->feature_bits & FEATURE_4BA_ENTER)
944 ret = spi_send_command(flash, sizeof(cmd), 0, &cmd, NULL);
945 else if (flash->chip->feature_bits & FEATURE_4BA_ENTER_WREN)
946 ret = spi_simple_write_cmd(flash, cmd, 0);
947 else if (flash->chip->feature_bits & FEATURE_4BA_ENTER_EAR7)
948 ret = spi_set_extended_address(flash, enter ? 0x80 : 0x00);
949
950 if (!ret)
951 flash->in_4ba_mode = enter;
952 return ret;
953}
954
955int spi_enter_4ba(struct flashctx *const flash)
956{
957 return spi_enter_exit_4ba(flash, true);
958}
959
960int spi_exit_4ba(struct flashctx *flash)
961{
962 return spi_enter_exit_4ba(flash, false);
963}