blob: 0f49a13f9df620c0005d58ff8140b120facb47dd [file] [log] [blame]
Sean Nelson14ba6682010-02-26 05:48:29 +00001/*
2 * This file is part of the flashrom project.
3 *
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00004 * Copyright (C) 2007, 2008, 2009, 2010 Carl-Daniel Hailfinger
Sean Nelson14ba6682010-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.
Sean Nelson14ba6682010-02-26 05:48:29 +000015 */
16
17/*
18 * Contains the common SPI chip driver functions
19 */
20
Nico Hubera3140d02017-10-15 11:20:58 +020021#include <stddef.h>
Sean Nelson14ba6682010-02-26 05:48:29 +000022#include <string.h>
Nico Hubera1672f82017-10-14 18:00:20 +020023#include <stdbool.h>
Sean Nelson14ba6682010-02-26 05:48:29 +000024#include "flash.h"
25#include "flashchips.h"
26#include "chipdrivers.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000027#include "programmer.h"
Nico Huberd5185632024-01-05 18:44:41 +010028#include "spi_command.h"
Sean Nelson14ba6682010-02-26 05:48:29 +000029#include "spi.h"
30
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000031static int spi_rdid(struct flashctx *flash, unsigned char *readarr, int bytes)
Sean Nelson14ba6682010-02-26 05:48:29 +000032{
Mathias Krausea60faab2011-01-17 07:50:42 +000033 static const unsigned char cmd[JEDEC_RDID_OUTSIZE] = { JEDEC_RDID };
Sean Nelson14ba6682010-02-26 05:48:29 +000034 int ret;
35 int i;
36
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000037 ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr);
Sean Nelson14ba6682010-02-26 05:48:29 +000038 if (ret)
39 return ret;
Sean Nelsoned479d22010-03-24 23:14:32 +000040 msg_cspew("RDID returned");
Sean Nelson14ba6682010-02-26 05:48:29 +000041 for (i = 0; i < bytes; i++)
Sean Nelsoned479d22010-03-24 23:14:32 +000042 msg_cspew(" 0x%02x", readarr[i]);
43 msg_cspew(". ");
Sean Nelson14ba6682010-02-26 05:48:29 +000044 return 0;
45}
46
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000047static int spi_rems(struct flashctx *flash, unsigned char *readarr)
Sean Nelson14ba6682010-02-26 05:48:29 +000048{
Nico Hubered098d62017-04-21 23:47:08 +020049 static const unsigned char cmd[JEDEC_REMS_OUTSIZE] = { JEDEC_REMS, };
Sean Nelson14ba6682010-02-26 05:48:29 +000050 int ret;
51
Nico Hubered098d62017-04-21 23:47:08 +020052 ret = spi_send_command(flash, sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
Sean Nelson14ba6682010-02-26 05:48:29 +000053 if (ret)
54 return ret;
Cristian Măgherușan-Stanciu9932c7b2011-07-07 19:56:58 +000055 msg_cspew("REMS returned 0x%02x 0x%02x. ", readarr[0], readarr[1]);
Sean Nelson14ba6682010-02-26 05:48:29 +000056 return 0;
57}
58
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000059static int spi_res(struct flashctx *flash, unsigned char *readarr, int bytes)
Sean Nelson14ba6682010-02-26 05:48:29 +000060{
Nico Hubered098d62017-04-21 23:47:08 +020061 static const unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, };
Sean Nelson14ba6682010-02-26 05:48:29 +000062 int ret;
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +000063 int i;
Sean Nelson14ba6682010-02-26 05:48:29 +000064
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000065 ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr);
Sean Nelson14ba6682010-02-26 05:48:29 +000066 if (ret)
67 return ret;
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +000068 msg_cspew("RES returned");
69 for (i = 0; i < bytes; i++)
70 msg_cspew(" 0x%02x", readarr[i]);
71 msg_cspew(". ");
Sean Nelson14ba6682010-02-26 05:48:29 +000072 return 0;
73}
74
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000075int spi_write_enable(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +000076{
Mathias Krausea60faab2011-01-17 07:50:42 +000077 static const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN };
Sean Nelson14ba6682010-02-26 05:48:29 +000078 int result;
79
80 /* Send WREN (Write Enable) */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000081 result = spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
Sean Nelson14ba6682010-02-26 05:48:29 +000082
83 if (result)
Sean Nelsoned479d22010-03-24 23:14:32 +000084 msg_cerr("%s failed\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +000085
86 return result;
87}
88
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000089int spi_write_disable(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +000090{
Mathias Krausea60faab2011-01-17 07:50:42 +000091 static const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI };
Sean Nelson14ba6682010-02-26 05:48:29 +000092
93 /* Send WRDI (Write Disable) */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000094 return spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
Sean Nelson14ba6682010-02-26 05:48:29 +000095}
96
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +000097static int probe_spi_rdid_generic(struct flashctx *flash, int bytes)
Sean Nelson14ba6682010-02-26 05:48:29 +000098{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +000099 const struct flashchip *chip = flash->chip;
Sean Nelson14ba6682010-02-26 05:48:29 +0000100 unsigned char readarr[4];
101 uint32_t id1;
102 uint32_t id2;
103
Nico Huber959aafa2017-04-22 00:13:15 +0200104 const int ret = spi_rdid(flash, readarr, bytes);
105 if (ret == SPI_INVALID_LENGTH)
106 msg_cinfo("%d byte RDID not supported on this SPI controller\n", bytes);
107 if (ret)
Sean Nelson14ba6682010-02-26 05:48:29 +0000108 return 0;
109
110 if (!oddparity(readarr[0]))
Sean Nelsoned479d22010-03-24 23:14:32 +0000111 msg_cdbg("RDID byte 0 parity violation. ");
Sean Nelson14ba6682010-02-26 05:48:29 +0000112
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000113 /* Check if this is a continuation vendor ID.
114 * FIXME: Handle continuation device IDs.
115 */
Sean Nelson14ba6682010-02-26 05:48:29 +0000116 if (readarr[0] == 0x7f) {
117 if (!oddparity(readarr[1]))
Sean Nelsoned479d22010-03-24 23:14:32 +0000118 msg_cdbg("RDID byte 1 parity violation. ");
Sean Nelson14ba6682010-02-26 05:48:29 +0000119 id1 = (readarr[0] << 8) | readarr[1];
120 id2 = readarr[2];
121 if (bytes > 3) {
122 id2 <<= 8;
123 id2 |= readarr[3];
124 }
125 } else {
126 id1 = readarr[0];
127 id2 = (readarr[1] << 8) | readarr[2];
128 }
129
Sean Nelsoned479d22010-03-24 23:14:32 +0000130 msg_cdbg("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
Sean Nelson14ba6682010-02-26 05:48:29 +0000131
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000132 if (id1 == chip->manufacture_id && id2 == chip->model_id)
Sean Nelson14ba6682010-02-26 05:48:29 +0000133 return 1;
Sean Nelson14ba6682010-02-26 05:48:29 +0000134
135 /* Test if this is a pure vendor match. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000136 if (id1 == chip->manufacture_id && GENERIC_DEVICE_ID == chip->model_id)
Sean Nelson14ba6682010-02-26 05:48:29 +0000137 return 1;
138
139 /* Test if there is any vendor ID. */
Urja Rannikko0a5f6e42015-06-22 23:59:15 +0000140 if (GENERIC_MANUF_ID == chip->manufacture_id && id1 != 0xff && id1 != 0x00)
Sean Nelson14ba6682010-02-26 05:48:29 +0000141 return 1;
142
143 return 0;
144}
145
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000146int probe_spi_rdid(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000147{
148 return probe_spi_rdid_generic(flash, 3);
149}
150
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000151int probe_spi_rdid4(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000152{
Nico Huber959aafa2017-04-22 00:13:15 +0200153 return probe_spi_rdid_generic(flash, 4);
Sean Nelson14ba6682010-02-26 05:48:29 +0000154}
155
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000156int probe_spi_rems(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000157{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000158 const struct flashchip *chip = flash->chip;
Sean Nelson14ba6682010-02-26 05:48:29 +0000159 unsigned char readarr[JEDEC_REMS_INSIZE];
160 uint32_t id1, id2;
161
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000162 if (spi_rems(flash, readarr)) {
Sean Nelson14ba6682010-02-26 05:48:29 +0000163 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000164 }
Sean Nelson14ba6682010-02-26 05:48:29 +0000165
166 id1 = readarr[0];
167 id2 = readarr[1];
168
Sean Nelsoned479d22010-03-24 23:14:32 +0000169 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
Sean Nelson14ba6682010-02-26 05:48:29 +0000170
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000171 if (id1 == chip->manufacture_id && id2 == chip->model_id)
Sean Nelson14ba6682010-02-26 05:48:29 +0000172 return 1;
Sean Nelson14ba6682010-02-26 05:48:29 +0000173
174 /* Test if this is a pure vendor match. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000175 if (id1 == chip->manufacture_id && GENERIC_DEVICE_ID == chip->model_id)
Sean Nelson14ba6682010-02-26 05:48:29 +0000176 return 1;
177
178 /* Test if there is any vendor ID. */
Urja Rannikko0a5f6e42015-06-22 23:59:15 +0000179 if (GENERIC_MANUF_ID == chip->manufacture_id && id1 != 0xff && id1 != 0x00)
Sean Nelson14ba6682010-02-26 05:48:29 +0000180 return 1;
181
182 return 0;
183}
184
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000185int probe_spi_res1(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000186{
Mathias Krausea60faab2011-01-17 07:50:42 +0000187 static const unsigned char allff[] = {0xff, 0xff, 0xff};
188 static const unsigned char all00[] = {0x00, 0x00, 0x00};
Sean Nelson14ba6682010-02-26 05:48:29 +0000189 unsigned char readarr[3];
190 uint32_t id2;
Sean Nelson14ba6682010-02-26 05:48:29 +0000191
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000192 /* We only want one-byte RES if RDID and REMS are unusable. */
193
Sean Nelson14ba6682010-02-26 05:48:29 +0000194 /* Check if RDID is usable and does not return 0xff 0xff 0xff or
195 * 0x00 0x00 0x00. In that case, RES is pointless.
196 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000197 if (!spi_rdid(flash, readarr, 3) && memcmp(readarr, allff, 3) &&
Sean Nelson14ba6682010-02-26 05:48:29 +0000198 memcmp(readarr, all00, 3)) {
199 msg_cdbg("Ignoring RES in favour of RDID.\n");
200 return 0;
201 }
202 /* Check if REMS is usable and does not return 0xff 0xff or
203 * 0x00 0x00. In that case, RES is pointless.
204 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000205 if (!spi_rems(flash, readarr) &&
206 memcmp(readarr, allff, JEDEC_REMS_INSIZE) &&
Sean Nelson14ba6682010-02-26 05:48:29 +0000207 memcmp(readarr, all00, JEDEC_REMS_INSIZE)) {
208 msg_cdbg("Ignoring RES in favour of REMS.\n");
209 return 0;
210 }
211
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000212 if (spi_res(flash, readarr, 1)) {
Sean Nelson14ba6682010-02-26 05:48:29 +0000213 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000214 }
Sean Nelson14ba6682010-02-26 05:48:29 +0000215
Sean Nelson14ba6682010-02-26 05:48:29 +0000216 id2 = readarr[0];
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000217
Sean Nelsoned479d22010-03-24 23:14:32 +0000218 msg_cdbg("%s: id 0x%x\n", __func__, id2);
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000219
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000220 if (id2 != flash->chip->model_id)
Sean Nelson14ba6682010-02-26 05:48:29 +0000221 return 0;
222
Sean Nelson14ba6682010-02-26 05:48:29 +0000223 return 1;
224}
225
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000226int probe_spi_res2(struct flashctx *flash)
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000227{
228 unsigned char readarr[2];
229 uint32_t id1, id2;
230
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000231 if (spi_res(flash, readarr, 2)) {
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000232 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000233 }
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000234
235 id1 = readarr[0];
236 id2 = readarr[1];
237
238 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
239
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000240 if (id1 != flash->chip->manufacture_id || id2 != flash->chip->model_id)
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000241 return 0;
242
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000243 return 1;
244}
245
Stefan Tauner3f5e35d2013-04-19 01:58:33 +0000246int probe_spi_res3(struct flashctx *flash)
247{
248 unsigned char readarr[3];
249 uint32_t id1, id2;
250
251 if (spi_res(flash, readarr, 3)) {
252 return 0;
253 }
254
255 id1 = (readarr[0] << 8) | readarr[1];
256 id2 = readarr[2];
257
258 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
259
260 if (id1 != flash->chip->manufacture_id || id2 != flash->chip->model_id)
261 return 0;
262
263 return 1;
264}
265
Stefan Tauner57794ac2012-12-29 15:04:20 +0000266/* Only used for some Atmel chips. */
267int probe_spi_at25f(struct flashctx *flash)
268{
269 static const unsigned char cmd[AT25F_RDID_OUTSIZE] = { AT25F_RDID };
270 unsigned char readarr[AT25F_RDID_INSIZE];
271 uint32_t id1;
272 uint32_t id2;
273
274 if (spi_send_command(flash, sizeof(cmd), sizeof(readarr), cmd, readarr))
275 return 0;
276
277 id1 = readarr[0];
278 id2 = readarr[1];
279
280 msg_cdbg("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
281
282 if (id1 == flash->chip->manufacture_id && id2 == flash->chip->model_id)
283 return 1;
284
285 return 0;
286}
287
Nico Huber0ecbacb2017-10-14 16:50:43 +0200288static int spi_poll_wip(struct flashctx *const flash, const unsigned int poll_delay)
289{
Nico Huber0ecbacb2017-10-14 16:50:43 +0200290 /* FIXME: We don't time out. */
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100291 while (true) {
292 uint8_t status;
293 int ret = spi_read_register(flash, STATUS1, &status);
294 if (ret)
295 return ret;
296 if (!(status & SPI_SR_WIP))
297 return 0;
298
Nico Huber0ecbacb2017-10-14 16:50:43 +0200299 programmer_delay(poll_delay);
Nikolai Artemievb8a90d02021-10-28 16:18:28 +1100300 }
Nico Huber0ecbacb2017-10-14 16:50:43 +0200301}
302
Nico Hubera3140d02017-10-15 11:20:58 +0200303/**
304 * Execute WREN plus another one byte `op`, optionally poll WIP afterwards.
305 *
306 * @param flash the flash chip's context
307 * @param op the operation to execute
308 * @param poll_delay interval in us for polling WIP, don't poll if zero
309 * @return 0 on success, non-zero otherwise
310 */
Nico Huber8d0f4652024-05-04 18:52:51 +0200311int spi_simple_write_cmd(struct flashctx *const flash, const uint8_t op, const unsigned int poll_delay)
Sean Nelson14ba6682010-02-26 05:48:29 +0000312{
Sean Nelson14ba6682010-02-26 05:48:29 +0000313 struct spi_command cmds[] = {
314 {
Nico Huber1b1deda2024-04-18 00:35:48 +0200315 .io_mode = spi_current_io_mode(flash),
Richard Hughesdf490582018-12-19 11:57:15 +0000316 .readarr = 0,
Nico Huberd5185632024-01-05 18:44:41 +0100317 .opcode_len = 1,
Nico Hubera3140d02017-10-15 11:20:58 +0200318 .writearr = (const unsigned char[]){ JEDEC_WREN },
Sean Nelson14ba6682010-02-26 05:48:29 +0000319 }, {
Nico Huber1b1deda2024-04-18 00:35:48 +0200320 .io_mode = spi_current_io_mode(flash),
Richard Hughesdf490582018-12-19 11:57:15 +0000321 .readarr = 0,
Nico Huberd5185632024-01-05 18:44:41 +0100322 .opcode_len = 1,
Nico Hubera3140d02017-10-15 11:20:58 +0200323 .writearr = (const unsigned char[]){ op },
324 },
325 NULL_SPI_CMD,
326 };
327
328 const int result = spi_send_multicommand(flash, cmds);
329 if (result)
330 msg_cerr("%s failed during command execution\n", __func__);
331
Nico Huber0ecbacb2017-10-14 16:50:43 +0200332 const int status = poll_delay ? spi_poll_wip(flash, poll_delay) : 0;
Nico Hubera3140d02017-10-15 11:20:58 +0200333
Nico Huber0ecbacb2017-10-14 16:50:43 +0200334 return result ? result : status;
335}
336
Nico Huber7e3c81a2017-10-14 18:56:50 +0200337static int spi_write_extended_address_register(struct flashctx *const flash, const uint8_t regdata)
338{
Nico Huber9bb8a322022-05-24 15:07:34 +0200339 uint8_t op;
340 if (flash->chip->feature_bits & FEATURE_4BA_EAR_C5C8) {
341 op = JEDEC_WRITE_EXT_ADDR_REG;
342 } else if (flash->chip->feature_bits & FEATURE_4BA_EAR_1716) {
343 op = ALT_WRITE_EXT_ADDR_REG_17;
344 } else {
345 msg_cerr("Flash misses feature flag for extended-address register.\n");
346 return -1;
347 }
348
Nico Huber7e3c81a2017-10-14 18:56:50 +0200349 struct spi_command cmds[] = {
350 {
Nico Huber1b1deda2024-04-18 00:35:48 +0200351 .io_mode = spi_current_io_mode(flash),
Richard Hughesdf490582018-12-19 11:57:15 +0000352 .readarr = 0,
Nico Huberd5185632024-01-05 18:44:41 +0100353 .opcode_len = 1,
Nico Huber7e3c81a2017-10-14 18:56:50 +0200354 .writearr = (const unsigned char[]){ JEDEC_WREN },
355 }, {
Nico Huber1b1deda2024-04-18 00:35:48 +0200356 .io_mode = spi_current_io_mode(flash),
Richard Hughesdf490582018-12-19 11:57:15 +0000357 .readarr = 0,
Nico Huberd5185632024-01-05 18:44:41 +0100358 .opcode_len = 1,
359 .write_len = 1,
Nico Huber57dbd642018-03-13 18:01:05 +0100360 .writearr = (const unsigned char[]){ op, regdata },
Nico Huber7e3c81a2017-10-14 18:56:50 +0200361 },
362 NULL_SPI_CMD,
363 };
364
365 const int result = spi_send_multicommand(flash, cmds);
366 if (result)
367 msg_cerr("%s failed during command execution\n", __func__);
368 return result;
369}
370
Nico Huber7eb38aa2019-03-21 15:42:54 +0100371int spi_set_extended_address(struct flashctx *const flash, const uint8_t addr_high)
Nico Huberf43c6542017-10-14 17:47:28 +0200372{
373 if (flash->address_high_byte != addr_high &&
374 spi_write_extended_address_register(flash, addr_high))
375 return -1;
376 flash->address_high_byte = addr_high;
377 return 0;
378}
379
Nico Hubera1672f82017-10-14 18:00:20 +0200380static int spi_prepare_address(struct flashctx *const flash, uint8_t cmd_buf[],
381 const bool native_4ba, const unsigned int addr)
Nico Huber0ecbacb2017-10-14 16:50:43 +0200382{
Nico Hubera1672f82017-10-14 18:00:20 +0200383 if (native_4ba || flash->in_4ba_mode) {
Nico Huber1cf407b2017-11-10 20:18:23 +0100384 if (!spi_master_4ba(flash)) {
385 msg_cwarn("4-byte address requested but master can't handle 4-byte addresses.\n");
386 return -1;
387 }
Nico Huberf43c6542017-10-14 17:47:28 +0200388 cmd_buf[1] = (addr >> 24) & 0xff;
389 cmd_buf[2] = (addr >> 16) & 0xff;
390 cmd_buf[3] = (addr >> 8) & 0xff;
391 cmd_buf[4] = (addr >> 0) & 0xff;
392 return 4;
393 } else {
Nico Huber9bb8a322022-05-24 15:07:34 +0200394 if (flash->chip->feature_bits & FEATURE_4BA_EAR_ANY) {
Nico Huberf43c6542017-10-14 17:47:28 +0200395 if (spi_set_extended_address(flash, addr >> 24))
396 return -1;
Nico Huber1cf407b2017-11-10 20:18:23 +0100397 } else if (addr >> 24) {
398 msg_cerr("Can't handle 4-byte address for opcode '0x%02x'\n"
399 "with this chip/programmer combination.\n", cmd_buf[0]);
400 return -1;
Nico Huberf43c6542017-10-14 17:47:28 +0200401 }
402 cmd_buf[1] = (addr >> 16) & 0xff;
403 cmd_buf[2] = (addr >> 8) & 0xff;
404 cmd_buf[3] = (addr >> 0) & 0xff;
405 return 3;
406 }
Nico Huber0ecbacb2017-10-14 16:50:43 +0200407}
408
409/**
410 * Execute WREN plus another `op` that takes an address and
411 * optional data, poll WIP afterwards.
412 *
413 * @param flash the flash chip's context
414 * @param op the operation to execute
Nico Hubera1672f82017-10-14 18:00:20 +0200415 * @param native_4ba whether `op` always takes a 4-byte address
Nico Huber0ecbacb2017-10-14 16:50:43 +0200416 * @param addr the address parameter to `op`
417 * @param out_bytes bytes to send after the address,
418 * may be NULL if and only if `out_bytes` is 0
419 * @param out_bytes number of bytes to send, 256 at most, may be zero
420 * @param poll_delay interval in us for polling WIP
421 * @return 0 on success, non-zero otherwise
422 */
Nico Hubera1672f82017-10-14 18:00:20 +0200423static int spi_write_cmd(struct flashctx *const flash, const uint8_t op,
424 const bool native_4ba, const unsigned int addr,
Nico Huber0ecbacb2017-10-14 16:50:43 +0200425 const uint8_t *const out_bytes, const size_t out_len,
426 const unsigned int poll_delay)
427{
428 uint8_t cmd[1 + JEDEC_MAX_ADDR_LEN + 256];
429 struct spi_command cmds[] = {
430 {
Nico Huber1b1deda2024-04-18 00:35:48 +0200431 .io_mode = spi_current_io_mode(flash),
Richard Hughesdf490582018-12-19 11:57:15 +0000432 .readarr = 0,
Nico Huberd5185632024-01-05 18:44:41 +0100433 .opcode_len = 1,
Nico Huber0ecbacb2017-10-14 16:50:43 +0200434 .writearr = (const unsigned char[]){ JEDEC_WREN },
435 }, {
Nico Huber1b1deda2024-04-18 00:35:48 +0200436 .io_mode = spi_current_io_mode(flash),
Richard Hughesdf490582018-12-19 11:57:15 +0000437 .readarr = 0,
Nico Huber0ecbacb2017-10-14 16:50:43 +0200438 .writearr = cmd,
439 },
440 NULL_SPI_CMD,
441 };
442
443 cmd[0] = op;
Nico Hubera1672f82017-10-14 18:00:20 +0200444 const int addr_len = spi_prepare_address(flash, cmd, native_4ba, addr);
Nico Huber0ecbacb2017-10-14 16:50:43 +0200445 if (addr_len < 0)
446 return 1;
447
448 if (1 + addr_len + out_len > sizeof(cmd)) {
449 msg_cerr("%s called for too long a write\n", __func__);
450 return 1;
451 }
Angel Ponsc92f6872020-03-31 15:32:10 +0200452 if (!out_bytes && out_len > 0)
453 return 1;
Nico Huber0ecbacb2017-10-14 16:50:43 +0200454
455 memcpy(cmd + 1 + addr_len, out_bytes, out_len);
Nico Huberd5185632024-01-05 18:44:41 +0100456 cmds[1].opcode_len = 1;
457 cmds[1].address_len = addr_len;
458 cmds[1].write_len = out_len;
Nico Huber0ecbacb2017-10-14 16:50:43 +0200459
460 const int result = spi_send_multicommand(flash, cmds);
461 if (result)
462 msg_cerr("%s failed during command execution at address 0x%x\n", __func__, addr);
463
464 const int status = spi_poll_wip(flash, poll_delay);
465
466 return result ? result : status;
Nico Hubera3140d02017-10-15 11:20:58 +0200467}
468
Jacob Garberbeeb8bc2019-06-21 15:24:17 -0600469static int spi_chip_erase_60(struct flashctx *flash)
Nico Hubera3140d02017-10-15 11:20:58 +0200470{
471 /* This usually takes 1-85s, so wait in 1s steps. */
472 return spi_simple_write_cmd(flash, 0x60, 1000 * 1000);
Sean Nelson14ba6682010-02-26 05:48:29 +0000473}
474
Jacob Garberbeeb8bc2019-06-21 15:24:17 -0600475static int spi_chip_erase_62(struct flashctx *flash)
Stefan Tauner3c0fcd02012-09-21 12:46:56 +0000476{
Nico Hubera3140d02017-10-15 11:20:58 +0200477 /* This usually takes 2-5s, so wait in 100ms steps. */
478 return spi_simple_write_cmd(flash, 0x62, 100 * 1000);
Stefan Tauner3c0fcd02012-09-21 12:46:56 +0000479}
480
Jacob Garberbeeb8bc2019-06-21 15:24:17 -0600481static int spi_chip_erase_c7(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000482{
Nico Hubera3140d02017-10-15 11:20:58 +0200483 /* This usually takes 1-85s, so wait in 1s steps. */
484 return spi_simple_write_cmd(flash, 0xc7, 1000 * 1000);
Sean Nelson14ba6682010-02-26 05:48:29 +0000485}
486
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000487int spi_block_erase_52(struct flashctx *flash, unsigned int addr,
488 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000489{
Nico Huber0ecbacb2017-10-14 16:50:43 +0200490 /* This usually takes 100-4000ms, so wait in 100ms steps. */
Nico Hubera1672f82017-10-14 18:00:20 +0200491 return spi_write_cmd(flash, 0x52, false, addr, NULL, 0, 100 * 1000);
Sean Nelson14ba6682010-02-26 05:48:29 +0000492}
493
494/* Block size is usually
Nikolay Nikolaev6f59b0b2013-06-28 21:29:51 +0000495 * 32M (one die) for Micron
496 */
497int spi_block_erase_c4(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
498{
Nico Huber0ecbacb2017-10-14 16:50:43 +0200499 /* This usually takes 240-480s, so wait in 500ms steps. */
Nico Hubera1672f82017-10-14 18:00:20 +0200500 return spi_write_cmd(flash, 0xc4, false, addr, NULL, 0, 500 * 1000);
Nikolay Nikolaev6f59b0b2013-06-28 21:29:51 +0000501}
502
503/* Block size is usually
Sean Nelson14ba6682010-02-26 05:48:29 +0000504 * 64k for Macronix
505 * 32k for SST
506 * 4-32k non-uniform for EON
507 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000508int spi_block_erase_d8(struct flashctx *flash, unsigned int addr,
509 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000510{
Nico Huber0ecbacb2017-10-14 16:50:43 +0200511 /* This usually takes 100-4000ms, so wait in 100ms steps. */
Nico Hubera1672f82017-10-14 18:00:20 +0200512 return spi_write_cmd(flash, 0xd8, false, addr, NULL, 0, 100 * 1000);
Sean Nelson14ba6682010-02-26 05:48:29 +0000513}
514
515/* Block size is usually
516 * 4k for PMC
517 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000518int spi_block_erase_d7(struct flashctx *flash, unsigned int addr,
519 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000520{
Nico Huber0ecbacb2017-10-14 16:50:43 +0200521 /* This usually takes 100-4000ms, so wait in 100ms steps. */
Nico Hubera1672f82017-10-14 18:00:20 +0200522 return spi_write_cmd(flash, 0xd7, false, addr, NULL, 0, 100 * 1000);
Sean Nelson14ba6682010-02-26 05:48:29 +0000523}
524
Nikolay Nikolaev579f1e02013-06-28 21:28:37 +0000525/* Page erase (usually 256B blocks) */
526int spi_block_erase_db(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
527{
Nico Huber0ecbacb2017-10-14 16:50:43 +0200528 /* This takes up to 20ms usually (on worn out devices
529 up to the 0.5s range), so wait in 1ms steps. */
Nico Hubera1672f82017-10-14 18:00:20 +0200530 return spi_write_cmd(flash, 0xdb, false, addr, NULL, 0, 1 * 1000);
Nikolay Nikolaev579f1e02013-06-28 21:28:37 +0000531}
532
Sean Nelson14ba6682010-02-26 05:48:29 +0000533/* Sector size is usually 4k, though Macronix eliteflash has 64k */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000534int spi_block_erase_20(struct flashctx *flash, unsigned int addr,
535 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000536{
Nico Huber0ecbacb2017-10-14 16:50:43 +0200537 /* This usually takes 15-800ms, so wait in 10ms steps. */
Nico Hubera1672f82017-10-14 18:00:20 +0200538 return spi_write_cmd(flash, 0x20, false, addr, NULL, 0, 10 * 1000);
Sean Nelson14ba6682010-02-26 05:48:29 +0000539}
540
Stefan Tauner94b39b42012-10-27 00:06:02 +0000541int spi_block_erase_50(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
542{
Nico Huber0ecbacb2017-10-14 16:50:43 +0200543 /* This usually takes 10ms, so wait in 1ms steps. */
Nico Hubera1672f82017-10-14 18:00:20 +0200544 return spi_write_cmd(flash, 0x50, false, addr, NULL, 0, 1 * 1000);
Stefan Tauner94b39b42012-10-27 00:06:02 +0000545}
546
547int spi_block_erase_81(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
548{
Nico Huber0ecbacb2017-10-14 16:50:43 +0200549 /* This usually takes 8ms, so wait in 1ms steps. */
Nico Hubera1672f82017-10-14 18:00:20 +0200550 return spi_write_cmd(flash, 0x81, false, addr, NULL, 0, 1 * 1000);
Stefan Tauner94b39b42012-10-27 00:06:02 +0000551}
552
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000553int spi_block_erase_60(struct flashctx *flash, unsigned int addr,
554 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000555{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000556 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000557 msg_cerr("%s called with incorrect arguments\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000558 __func__);
559 return -1;
560 }
561 return spi_chip_erase_60(flash);
562}
563
Stefan Tauner3c0fcd02012-09-21 12:46:56 +0000564int spi_block_erase_62(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
565{
566 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
567 msg_cerr("%s called with incorrect arguments\n",
568 __func__);
569 return -1;
570 }
571 return spi_chip_erase_62(flash);
572}
573
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000574int spi_block_erase_c7(struct flashctx *flash, unsigned int addr,
575 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000576{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000577 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000578 msg_cerr("%s called with incorrect arguments\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000579 __func__);
580 return -1;
581 }
582 return spi_chip_erase_c7(flash);
583}
584
Nico Huber7e3c81a2017-10-14 18:56:50 +0200585/* Erase 4 KB of flash with 4-bytes address from ANY mode (3-bytes or 4-bytes) */
586int spi_block_erase_21(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
587{
588 /* This usually takes 15-800ms, so wait in 10ms steps. */
589 return spi_write_cmd(flash, 0x21, true, addr, NULL, 0, 10 * 1000);
590}
591
592/* Erase 32 KB of flash with 4-bytes address from ANY mode (3-bytes or 4-bytes) */
Nico Huberfffc48d2022-05-28 14:26:06 +0200593int spi_block_erase_53(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
594{
595 /* This usually takes 100-4000ms, so wait in 100ms steps. */
596 return spi_write_cmd(flash, 0x53, true, addr, NULL, 0, 100 * 1000);
597}
598
599/* Erase 32 KB of flash with 4-bytes address from ANY mode (3-bytes or 4-bytes) */
Nico Huber7e3c81a2017-10-14 18:56:50 +0200600int spi_block_erase_5c(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
601{
602 /* This usually takes 100-4000ms, so wait in 100ms steps. */
603 return spi_write_cmd(flash, 0x5c, true, addr, NULL, 0, 100 * 1000);
604}
605
606/* Erase 64 KB of flash with 4-bytes address from ANY mode (3-bytes or 4-bytes) */
607int spi_block_erase_dc(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
608{
609 /* This usually takes 100-4000ms, so wait in 100ms steps. */
610 return spi_write_cmd(flash, 0xdc, true, addr, NULL, 0, 100 * 1000);
611}
612
Aarya Chaumalb725c0c2022-06-23 16:12:12 +0530613static const struct {
614 erasefunc_t *func;
615 uint8_t opcode;
Thomas Heijligen35614512022-09-19 23:46:58 +0200616} spi25_function_opcode_list[] = {
Aarya Chaumalb725c0c2022-06-23 16:12:12 +0530617 {&spi_block_erase_20, 0x20},
618 {&spi_block_erase_21, 0x21},
619 {&spi_block_erase_50, 0x50},
620 {&spi_block_erase_52, 0x52},
621 {&spi_block_erase_53, 0x53},
622 {&spi_block_erase_5c, 0x5c},
623 {&spi_block_erase_60, 0x60},
624 {&spi_block_erase_62, 0x62},
625 {&spi_block_erase_81, 0x81},
626 {&spi_block_erase_c4, 0xc4},
627 {&spi_block_erase_c7, 0xc7},
628 {&spi_block_erase_d7, 0xd7},
629 {&spi_block_erase_d8, 0xd8},
630 {&spi_block_erase_db, 0xdb},
631 {&spi_block_erase_dc, 0xdc},
632};
633
Thomas Heijligen35614512022-09-19 23:46:58 +0200634erasefunc_t *spi25_get_erasefn_from_opcode(uint8_t opcode)
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000635{
Aarya Chaumalb725c0c2022-06-23 16:12:12 +0530636 size_t i;
Thomas Heijligen35614512022-09-19 23:46:58 +0200637 for (i = 0; i < ARRAY_SIZE(spi25_function_opcode_list); i++) {
638 if (spi25_function_opcode_list[i].opcode == opcode)
639 return spi25_function_opcode_list[i].func;
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000640 }
Aarya Chaumalb725c0c2022-06-23 16:12:12 +0530641 msg_cinfo("%s: unknown erase opcode (0x%02x). Please report "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200642 "this at flashprog@flashprog.org\n", __func__, opcode);
Aarya Chaumalb725c0c2022-06-23 16:12:12 +0530643 return NULL;
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000644}
645
Nico Huber0ecbacb2017-10-14 16:50:43 +0200646static int spi_nbyte_program(struct flashctx *flash, unsigned int addr, const uint8_t *bytes, unsigned int len)
Sean Nelson14ba6682010-02-26 05:48:29 +0000647{
Nico Huber1cf407b2017-11-10 20:18:23 +0100648 const bool native_4ba = flash->chip->feature_bits & FEATURE_4BA_WRITE && spi_master_4ba(flash);
Nico Hubera1672f82017-10-14 18:00:20 +0200649 const uint8_t op = native_4ba ? JEDEC_BYTE_PROGRAM_4BA : JEDEC_BYTE_PROGRAM;
650 return spi_write_cmd(flash, op, native_4ba, addr, bytes, len, 10);
Sean Nelson14ba6682010-02-26 05:48:29 +0000651}
652
Nico Hubera1b7f352024-03-25 18:32:11 +0100653const struct spi_read_op *get_spi_read_op(const struct flashctx *flash)
Nico Huber4760b6e2024-01-06 23:45:28 +0100654{
655 static const struct spi_read_op sio_read = { SINGLE_IO_1_1_1, false, JEDEC_READ, 0x00, 0 };
656 static const struct spi_read_op sio_read_4ba = { SINGLE_IO_1_1_1, true, JEDEC_READ_4BA, 0x00, 0 };
657
658 if (flash->spi_fast_read)
659 return flash->spi_fast_read;
660
661 if (flash->chip->feature_bits & FEATURE_4BA_READ && spi_master_4ba(flash))
662 return &sio_read_4ba;
663
664 return &sio_read;
665}
666
Nico Huberca1c7fd2023-04-28 21:44:41 +0000667int spi_nbyte_read(struct flashctx *flash, uint8_t *dst, unsigned int address, unsigned int len)
Sean Nelson14ba6682010-02-26 05:48:29 +0000668{
Nico Huber4760b6e2024-01-06 23:45:28 +0100669 const struct spi_read_op *const read_op = get_spi_read_op(flash);
670 const size_t mode_len = read_op->mode_byte ? 1 : 0;
671 uint8_t cmd_buf[1 + JEDEC_MAX_ADDR_LEN + 1];
Nico Huber0ecbacb2017-10-14 16:50:43 +0200672
Nico Huber4760b6e2024-01-06 23:45:28 +0100673 const int addr_len = spi_prepare_address(flash, cmd_buf, read_op->native_4ba, address);
Nico Huber0ecbacb2017-10-14 16:50:43 +0200674 if (addr_len < 0)
675 return 1;
Sean Nelson14ba6682010-02-26 05:48:29 +0000676
Nico Huber4760b6e2024-01-06 23:45:28 +0100677 cmd_buf[0] = read_op->opcode;
678 cmd_buf[addr_len + 1] = read_op->mode_byte;
679
680 struct spi_command cmd[] = {
681 {
682 .io_mode = read_op->io_mode,
683 .opcode_len = 1,
684 .address_len = addr_len,
685 .write_len = mode_len,
686 .high_z_len = read_op->dummy_len - mode_len,
687 .read_len = len,
688 .writearr = cmd_buf,
689 .readarr = dst,
690 },
691 NULL_SPI_CMD
692 };
693
694 return spi_send_multicommand(flash, cmd);
Sean Nelson14ba6682010-02-26 05:48:29 +0000695}
696
697/*
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000698 * Write a part of the flash chip.
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000699 * FIXME: Use the chunk code from Michael Karcher instead.
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000700 * Each page is written separately in chunks with a maximum size of chunksize.
701 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000702int spi_write_chunked(struct flashctx *flash, const uint8_t *buf, unsigned int start,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000703 unsigned int len, unsigned int chunksize)
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000704{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000705 unsigned int i, j, starthere, lenhere, towrite;
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000706 /* FIXME: page_size is the wrong variable. We need max_writechunk_size
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000707 * in struct flashctx to do this properly. All chips using
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000708 * spi_chip_write_256 have page_size set to max_writechunk_size, so
709 * we're OK for now.
710 */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000711 unsigned int page_size = flash->chip->page_size;
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000712
713 /* Warning: This loop has a very unusual condition and body.
714 * The loop needs to go through each page with at least one affected
715 * byte. The lowest page number is (start / page_size) since that
716 * division rounds down. The highest page number we want is the page
717 * where the last byte of the range lives. That last byte has the
718 * address (start + len - 1), thus the highest page number is
719 * (start + len - 1) / page_size. Since we want to include that last
720 * page as well, the loop condition uses <=.
721 */
722 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
723 /* Byte position of the first byte in the range in this page. */
724 /* starthere is an offset to the base address of the chip. */
725 starthere = max(start, i * page_size);
726 /* Length of bytes in the range in this page. */
727 lenhere = min(start + len, (i + 1) * page_size) - starthere;
728 for (j = 0; j < lenhere; j += chunksize) {
Nico Huber7a077222017-10-14 18:18:30 +0200729 int rc;
730
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000731 towrite = min(chunksize, lenhere - j);
Nico Huber7a077222017-10-14 18:18:30 +0200732 rc = spi_nbyte_program(flash, starthere + j, buf + starthere - start + j, towrite);
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000733 if (rc)
Nico Huber7a077222017-10-14 18:18:30 +0200734 return rc;
Richard Hughes842d6782021-01-15 09:48:12 +0000735 flashprog_progress_add(flash, towrite);
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000736 }
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000737 }
738
Nico Huber7a077222017-10-14 18:18:30 +0200739 return 0;
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000740}
741
742/*
Sean Nelson14ba6682010-02-26 05:48:29 +0000743 * Program chip using byte programming. (SLOW!)
744 * This is for chips which can only handle one byte writes
745 * and for chips where memory mapped programming is impossible
746 * (e.g. due to size constraints in IT87* for over 512 kB)
747 */
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000748/* real chunksize is 1, logical chunksize is 1 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000749int spi_chip_write_1(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
Sean Nelson14ba6682010-02-26 05:48:29 +0000750{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000751 unsigned int i;
Sean Nelson14ba6682010-02-26 05:48:29 +0000752
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000753 for (i = start; i < start + len; i++) {
Nico Huber7a077222017-10-14 18:18:30 +0200754 if (spi_nbyte_program(flash, i, buf + i - start, 1))
Sean Nelson14ba6682010-02-26 05:48:29 +0000755 return 1;
Richard Hughes842d6782021-01-15 09:48:12 +0000756 flashprog_progress_add(flash, 1);
Sean Nelson14ba6682010-02-26 05:48:29 +0000757 }
Sean Nelson14ba6682010-02-26 05:48:29 +0000758 return 0;
759}
760
Mark Marshallf20b7be2014-05-09 21:16:21 +0000761int default_spi_write_aai(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000762{
763 uint32_t pos = start;
Sean Nelson14ba6682010-02-26 05:48:29 +0000764 int result;
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +0000765 unsigned char cmd[JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE] = {
766 JEDEC_AAI_WORD_PROGRAM,
767 };
Sean Nelson14ba6682010-02-26 05:48:29 +0000768
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000769 /* The even start address and even length requirements can be either
770 * honored outside this function, or we can call spi_byte_program
771 * for the first and/or last byte and use AAI for the rest.
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +0000772 * FIXME: Move this to generic code.
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000773 */
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +0000774 /* The data sheet requires a start address with the low bit cleared. */
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000775 if (start % 2) {
Nico Huberac90af62022-12-18 00:22:47 +0000776 msg_cerr("%s: start address not even!\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200777 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000778 __func__);
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +0000779 if (spi_chip_write_1(flash, buf, start, start % 2))
780 return SPI_GENERIC_ERROR;
781 pos += start % 2;
782 /* Do not return an error for now. */
783 //return SPI_GENERIC_ERROR;
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +0000784 }
785 /* The data sheet requires total AAI write length to be even. */
786 if (len % 2) {
Nico Huberac90af62022-12-18 00:22:47 +0000787 msg_cerr("%s: total write length not even!\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200788 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000789 __func__);
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +0000790 /* Do not return an error for now. */
791 //return SPI_GENERIC_ERROR;
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +0000792 }
793
Nico Hubera1672f82017-10-14 18:00:20 +0200794 result = spi_write_cmd(flash, JEDEC_AAI_WORD_PROGRAM, false, start, buf + pos - start, 2, 10);
Nico Huber0ecbacb2017-10-14 16:50:43 +0200795 if (result)
Stefan Reinauer87ace662014-04-26 16:12:55 +0000796 goto bailout;
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +0000797
798 /* We already wrote 2 bytes in the multicommand step. */
Richard Hughes842d6782021-01-15 09:48:12 +0000799 flashprog_progress_add(flash, 2);
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +0000800 pos += 2;
801
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +0000802 /* Are there at least two more bytes to write? */
803 while (pos < start + len - 1) {
Carl-Daniel Hailfingerccfe0ac2010-10-27 22:07:11 +0000804 cmd[1] = buf[pos++ - start];
805 cmd[2] = buf[pos++ - start];
Stefan Reinauer87ace662014-04-26 16:12:55 +0000806 result = spi_send_command(flash, JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE, 0, cmd, NULL);
807 if (result != 0) {
808 msg_cerr("%s failed during followup AAI command execution: %d\n", __func__, result);
809 goto bailout;
810 }
Nico Huber0ecbacb2017-10-14 16:50:43 +0200811 if (spi_poll_wip(flash, 10))
812 goto bailout;
Richard Hughes842d6782021-01-15 09:48:12 +0000813 flashprog_progress_add(flash, 2);
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +0000814 }
815
Stefan Tauner59c4d792014-04-26 16:13:09 +0000816 /* Use WRDI to exit AAI mode. This needs to be done before issuing any other non-AAI command. */
817 result = spi_write_disable(flash);
818 if (result != 0) {
819 msg_cerr("%s failed to disable AAI mode.\n", __func__);
820 return SPI_GENERIC_ERROR;
821 }
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +0000822
823 /* Write remaining byte (if any). */
824 if (pos < start + len) {
Carl-Daniel Hailfingerccfe0ac2010-10-27 22:07:11 +0000825 if (spi_chip_write_1(flash, buf + pos - start, pos, pos % 2))
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +0000826 return SPI_GENERIC_ERROR;
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +0000827 }
828
Sean Nelson14ba6682010-02-26 05:48:29 +0000829 return 0;
Stefan Reinauer87ace662014-04-26 16:12:55 +0000830
831bailout:
Stefan Tauner59c4d792014-04-26 16:13:09 +0000832 result = spi_write_disable(flash);
833 if (result != 0)
834 msg_cerr("%s failed to disable AAI mode.\n", __func__);
Stefan Reinauer87ace662014-04-26 16:12:55 +0000835 return SPI_GENERIC_ERROR;
Sean Nelson14ba6682010-02-26 05:48:29 +0000836}