blob: 36f265a7b46dfbe9376f9ad02eb5fe4ec456972d [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"
Sean Nelson14ba6682010-02-26 05:48:29 +000028#include "spi.h"
29
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000030static int spi_rdid(struct flashctx *flash, unsigned char *readarr, int bytes)
Sean Nelson14ba6682010-02-26 05:48:29 +000031{
Mathias Krausea60faab2011-01-17 07:50:42 +000032 static const unsigned char cmd[JEDEC_RDID_OUTSIZE] = { JEDEC_RDID };
Sean Nelson14ba6682010-02-26 05:48:29 +000033 int ret;
34 int i;
35
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000036 ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr);
Sean Nelson14ba6682010-02-26 05:48:29 +000037 if (ret)
38 return ret;
Sean Nelsoned479d22010-03-24 23:14:32 +000039 msg_cspew("RDID returned");
Sean Nelson14ba6682010-02-26 05:48:29 +000040 for (i = 0; i < bytes; i++)
Sean Nelsoned479d22010-03-24 23:14:32 +000041 msg_cspew(" 0x%02x", readarr[i]);
42 msg_cspew(". ");
Sean Nelson14ba6682010-02-26 05:48:29 +000043 return 0;
44}
45
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000046static int spi_rems(struct flashctx *flash, unsigned char *readarr)
Sean Nelson14ba6682010-02-26 05:48:29 +000047{
Nico Hubered098d62017-04-21 23:47:08 +020048 static const unsigned char cmd[JEDEC_REMS_OUTSIZE] = { JEDEC_REMS, };
Sean Nelson14ba6682010-02-26 05:48:29 +000049 int ret;
50
Nico Hubered098d62017-04-21 23:47:08 +020051 ret = spi_send_command(flash, sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
Sean Nelson14ba6682010-02-26 05:48:29 +000052 if (ret)
53 return ret;
Cristian Măgherușan-Stanciu9932c7b2011-07-07 19:56:58 +000054 msg_cspew("REMS returned 0x%02x 0x%02x. ", readarr[0], readarr[1]);
Sean Nelson14ba6682010-02-26 05:48:29 +000055 return 0;
56}
57
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000058static int spi_res(struct flashctx *flash, unsigned char *readarr, int bytes)
Sean Nelson14ba6682010-02-26 05:48:29 +000059{
Nico Hubered098d62017-04-21 23:47:08 +020060 static const unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, };
Sean Nelson14ba6682010-02-26 05:48:29 +000061 int ret;
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +000062 int i;
Sean Nelson14ba6682010-02-26 05:48:29 +000063
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000064 ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr);
Sean Nelson14ba6682010-02-26 05:48:29 +000065 if (ret)
66 return ret;
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +000067 msg_cspew("RES returned");
68 for (i = 0; i < bytes; i++)
69 msg_cspew(" 0x%02x", readarr[i]);
70 msg_cspew(". ");
Sean Nelson14ba6682010-02-26 05:48:29 +000071 return 0;
72}
73
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000074int spi_write_enable(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +000075{
Mathias Krausea60faab2011-01-17 07:50:42 +000076 static const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN };
Sean Nelson14ba6682010-02-26 05:48:29 +000077 int result;
78
79 /* Send WREN (Write Enable) */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000080 result = spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
Sean Nelson14ba6682010-02-26 05:48:29 +000081
82 if (result)
Sean Nelsoned479d22010-03-24 23:14:32 +000083 msg_cerr("%s failed\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +000084
85 return result;
86}
87
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000088int spi_write_disable(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +000089{
Mathias Krausea60faab2011-01-17 07:50:42 +000090 static const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI };
Sean Nelson14ba6682010-02-26 05:48:29 +000091
92 /* Send WRDI (Write Disable) */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000093 return spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
Sean Nelson14ba6682010-02-26 05:48:29 +000094}
95
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +000096static int probe_spi_rdid_generic(struct flashctx *flash, int bytes)
Sean Nelson14ba6682010-02-26 05:48:29 +000097{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +000098 const struct flashchip *chip = flash->chip;
Sean Nelson14ba6682010-02-26 05:48:29 +000099 unsigned char readarr[4];
100 uint32_t id1;
101 uint32_t id2;
102
Nico Huber959aafa2017-04-22 00:13:15 +0200103 const int ret = spi_rdid(flash, readarr, bytes);
104 if (ret == SPI_INVALID_LENGTH)
105 msg_cinfo("%d byte RDID not supported on this SPI controller\n", bytes);
106 if (ret)
Sean Nelson14ba6682010-02-26 05:48:29 +0000107 return 0;
108
109 if (!oddparity(readarr[0]))
Sean Nelsoned479d22010-03-24 23:14:32 +0000110 msg_cdbg("RDID byte 0 parity violation. ");
Sean Nelson14ba6682010-02-26 05:48:29 +0000111
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000112 /* Check if this is a continuation vendor ID.
113 * FIXME: Handle continuation device IDs.
114 */
Sean Nelson14ba6682010-02-26 05:48:29 +0000115 if (readarr[0] == 0x7f) {
116 if (!oddparity(readarr[1]))
Sean Nelsoned479d22010-03-24 23:14:32 +0000117 msg_cdbg("RDID byte 1 parity violation. ");
Sean Nelson14ba6682010-02-26 05:48:29 +0000118 id1 = (readarr[0] << 8) | readarr[1];
119 id2 = readarr[2];
120 if (bytes > 3) {
121 id2 <<= 8;
122 id2 |= readarr[3];
123 }
124 } else {
125 id1 = readarr[0];
126 id2 = (readarr[1] << 8) | readarr[2];
127 }
128
Sean Nelsoned479d22010-03-24 23:14:32 +0000129 msg_cdbg("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
Sean Nelson14ba6682010-02-26 05:48:29 +0000130
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000131 if (id1 == chip->manufacture_id && id2 == chip->model_id)
Sean Nelson14ba6682010-02-26 05:48:29 +0000132 return 1;
Sean Nelson14ba6682010-02-26 05:48:29 +0000133
134 /* Test if this is a pure vendor match. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000135 if (id1 == chip->manufacture_id && GENERIC_DEVICE_ID == chip->model_id)
Sean Nelson14ba6682010-02-26 05:48:29 +0000136 return 1;
137
138 /* Test if there is any vendor ID. */
Urja Rannikko0a5f6e42015-06-22 23:59:15 +0000139 if (GENERIC_MANUF_ID == chip->manufacture_id && id1 != 0xff && id1 != 0x00)
Sean Nelson14ba6682010-02-26 05:48:29 +0000140 return 1;
141
142 return 0;
143}
144
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000145int probe_spi_rdid(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000146{
147 return probe_spi_rdid_generic(flash, 3);
148}
149
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000150int probe_spi_rdid4(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000151{
Nico Huber959aafa2017-04-22 00:13:15 +0200152 return probe_spi_rdid_generic(flash, 4);
Sean Nelson14ba6682010-02-26 05:48:29 +0000153}
154
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000155int probe_spi_rems(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000156{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000157 const struct flashchip *chip = flash->chip;
Sean Nelson14ba6682010-02-26 05:48:29 +0000158 unsigned char readarr[JEDEC_REMS_INSIZE];
159 uint32_t id1, id2;
160
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000161 if (spi_rems(flash, readarr)) {
Sean Nelson14ba6682010-02-26 05:48:29 +0000162 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000163 }
Sean Nelson14ba6682010-02-26 05:48:29 +0000164
165 id1 = readarr[0];
166 id2 = readarr[1];
167
Sean Nelsoned479d22010-03-24 23:14:32 +0000168 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
Sean Nelson14ba6682010-02-26 05:48:29 +0000169
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000170 if (id1 == chip->manufacture_id && id2 == chip->model_id)
Sean Nelson14ba6682010-02-26 05:48:29 +0000171 return 1;
Sean Nelson14ba6682010-02-26 05:48:29 +0000172
173 /* Test if this is a pure vendor match. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000174 if (id1 == chip->manufacture_id && GENERIC_DEVICE_ID == chip->model_id)
Sean Nelson14ba6682010-02-26 05:48:29 +0000175 return 1;
176
177 /* Test if there is any vendor ID. */
Urja Rannikko0a5f6e42015-06-22 23:59:15 +0000178 if (GENERIC_MANUF_ID == chip->manufacture_id && id1 != 0xff && id1 != 0x00)
Sean Nelson14ba6682010-02-26 05:48:29 +0000179 return 1;
180
181 return 0;
182}
183
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000184int probe_spi_res1(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000185{
Mathias Krausea60faab2011-01-17 07:50:42 +0000186 static const unsigned char allff[] = {0xff, 0xff, 0xff};
187 static const unsigned char all00[] = {0x00, 0x00, 0x00};
Sean Nelson14ba6682010-02-26 05:48:29 +0000188 unsigned char readarr[3];
189 uint32_t id2;
Sean Nelson14ba6682010-02-26 05:48:29 +0000190
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000191 /* We only want one-byte RES if RDID and REMS are unusable. */
192
Sean Nelson14ba6682010-02-26 05:48:29 +0000193 /* Check if RDID is usable and does not return 0xff 0xff 0xff or
194 * 0x00 0x00 0x00. In that case, RES is pointless.
195 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000196 if (!spi_rdid(flash, readarr, 3) && memcmp(readarr, allff, 3) &&
Sean Nelson14ba6682010-02-26 05:48:29 +0000197 memcmp(readarr, all00, 3)) {
198 msg_cdbg("Ignoring RES in favour of RDID.\n");
199 return 0;
200 }
201 /* Check if REMS is usable and does not return 0xff 0xff or
202 * 0x00 0x00. In that case, RES is pointless.
203 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000204 if (!spi_rems(flash, readarr) &&
205 memcmp(readarr, allff, JEDEC_REMS_INSIZE) &&
Sean Nelson14ba6682010-02-26 05:48:29 +0000206 memcmp(readarr, all00, JEDEC_REMS_INSIZE)) {
207 msg_cdbg("Ignoring RES in favour of REMS.\n");
208 return 0;
209 }
210
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000211 if (spi_res(flash, readarr, 1)) {
Sean Nelson14ba6682010-02-26 05:48:29 +0000212 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000213 }
Sean Nelson14ba6682010-02-26 05:48:29 +0000214
Sean Nelson14ba6682010-02-26 05:48:29 +0000215 id2 = readarr[0];
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000216
Sean Nelsoned479d22010-03-24 23:14:32 +0000217 msg_cdbg("%s: id 0x%x\n", __func__, id2);
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000218
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000219 if (id2 != flash->chip->model_id)
Sean Nelson14ba6682010-02-26 05:48:29 +0000220 return 0;
221
Sean Nelson14ba6682010-02-26 05:48:29 +0000222 return 1;
223}
224
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000225int probe_spi_res2(struct flashctx *flash)
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000226{
227 unsigned char readarr[2];
228 uint32_t id1, id2;
229
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000230 if (spi_res(flash, readarr, 2)) {
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000231 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000232 }
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000233
234 id1 = readarr[0];
235 id2 = readarr[1];
236
237 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
238
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000239 if (id1 != flash->chip->manufacture_id || id2 != flash->chip->model_id)
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000240 return 0;
241
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000242 return 1;
243}
244
Stefan Tauner3f5e35d2013-04-19 01:58:33 +0000245int probe_spi_res3(struct flashctx *flash)
246{
247 unsigned char readarr[3];
248 uint32_t id1, id2;
249
250 if (spi_res(flash, readarr, 3)) {
251 return 0;
252 }
253
254 id1 = (readarr[0] << 8) | readarr[1];
255 id2 = readarr[2];
256
257 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
258
259 if (id1 != flash->chip->manufacture_id || id2 != flash->chip->model_id)
260 return 0;
261
262 return 1;
263}
264
Stefan Tauner57794ac2012-12-29 15:04:20 +0000265/* Only used for some Atmel chips. */
266int probe_spi_at25f(struct flashctx *flash)
267{
268 static const unsigned char cmd[AT25F_RDID_OUTSIZE] = { AT25F_RDID };
269 unsigned char readarr[AT25F_RDID_INSIZE];
270 uint32_t id1;
271 uint32_t id2;
272
273 if (spi_send_command(flash, sizeof(cmd), sizeof(readarr), cmd, readarr))
274 return 0;
275
276 id1 = readarr[0];
277 id2 = readarr[1];
278
279 msg_cdbg("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
280
281 if (id1 == flash->chip->manufacture_id && id2 == flash->chip->model_id)
282 return 1;
283
284 return 0;
285}
286
Nico Huber0ecbacb2017-10-14 16:50:43 +0200287static int spi_poll_wip(struct flashctx *const flash, const unsigned int poll_delay)
288{
289 /* FIXME: We can't tell if spi_read_status_register() failed. */
290 /* FIXME: We don't time out. */
291 while (spi_read_status_register(flash) & SPI_SR_WIP)
292 programmer_delay(poll_delay);
293 /* FIXME: Check the status register for errors. */
294 return 0;
295}
296
Nico Hubera3140d02017-10-15 11:20:58 +0200297/**
298 * Execute WREN plus another one byte `op`, optionally poll WIP afterwards.
299 *
300 * @param flash the flash chip's context
301 * @param op the operation to execute
302 * @param poll_delay interval in us for polling WIP, don't poll if zero
303 * @return 0 on success, non-zero otherwise
304 */
305static int spi_simple_write_cmd(struct flashctx *const flash, const uint8_t op, const unsigned int poll_delay)
Sean Nelson14ba6682010-02-26 05:48:29 +0000306{
Sean Nelson14ba6682010-02-26 05:48:29 +0000307 struct spi_command cmds[] = {
308 {
Richard Hughesdf490582018-12-19 11:57:15 +0000309 .readarr = 0,
Nico Hubera3140d02017-10-15 11:20:58 +0200310 .writecnt = 1,
311 .writearr = (const unsigned char[]){ JEDEC_WREN },
Sean Nelson14ba6682010-02-26 05:48:29 +0000312 }, {
Richard Hughesdf490582018-12-19 11:57:15 +0000313 .readarr = 0,
Nico Hubera3140d02017-10-15 11:20:58 +0200314 .writecnt = 1,
315 .writearr = (const unsigned char[]){ op },
316 },
317 NULL_SPI_CMD,
318 };
319
320 const int result = spi_send_multicommand(flash, cmds);
321 if (result)
322 msg_cerr("%s failed during command execution\n", __func__);
323
Nico Huber0ecbacb2017-10-14 16:50:43 +0200324 const int status = poll_delay ? spi_poll_wip(flash, poll_delay) : 0;
Nico Hubera3140d02017-10-15 11:20:58 +0200325
Nico Huber0ecbacb2017-10-14 16:50:43 +0200326 return result ? result : status;
327}
328
Nico Huber7e3c81a2017-10-14 18:56:50 +0200329static int spi_write_extended_address_register(struct flashctx *const flash, const uint8_t regdata)
330{
Nico Huber57dbd642018-03-13 18:01:05 +0100331 const uint8_t op = flash->chip->wrea_override ? : JEDEC_WRITE_EXT_ADDR_REG;
Nico Huber7e3c81a2017-10-14 18:56:50 +0200332 struct spi_command cmds[] = {
333 {
Richard Hughesdf490582018-12-19 11:57:15 +0000334 .readarr = 0,
Nico Huber7e3c81a2017-10-14 18:56:50 +0200335 .writecnt = 1,
336 .writearr = (const unsigned char[]){ JEDEC_WREN },
337 }, {
Richard Hughesdf490582018-12-19 11:57:15 +0000338 .readarr = 0,
Nico Huber7e3c81a2017-10-14 18:56:50 +0200339 .writecnt = 2,
Nico Huber57dbd642018-03-13 18:01:05 +0100340 .writearr = (const unsigned char[]){ op, regdata },
Nico Huber7e3c81a2017-10-14 18:56:50 +0200341 },
342 NULL_SPI_CMD,
343 };
344
345 const int result = spi_send_multicommand(flash, cmds);
346 if (result)
347 msg_cerr("%s failed during command execution\n", __func__);
348 return result;
349}
350
Nico Huber7eb38aa2019-03-21 15:42:54 +0100351int spi_set_extended_address(struct flashctx *const flash, const uint8_t addr_high)
Nico Huberf43c6542017-10-14 17:47:28 +0200352{
353 if (flash->address_high_byte != addr_high &&
354 spi_write_extended_address_register(flash, addr_high))
355 return -1;
356 flash->address_high_byte = addr_high;
357 return 0;
358}
359
Nico Hubera1672f82017-10-14 18:00:20 +0200360static int spi_prepare_address(struct flashctx *const flash, uint8_t cmd_buf[],
361 const bool native_4ba, const unsigned int addr)
Nico Huber0ecbacb2017-10-14 16:50:43 +0200362{
Nico Hubera1672f82017-10-14 18:00:20 +0200363 if (native_4ba || flash->in_4ba_mode) {
Nico Huber1cf407b2017-11-10 20:18:23 +0100364 if (!spi_master_4ba(flash)) {
365 msg_cwarn("4-byte address requested but master can't handle 4-byte addresses.\n");
366 return -1;
367 }
Nico Huberf43c6542017-10-14 17:47:28 +0200368 cmd_buf[1] = (addr >> 24) & 0xff;
369 cmd_buf[2] = (addr >> 16) & 0xff;
370 cmd_buf[3] = (addr >> 8) & 0xff;
371 cmd_buf[4] = (addr >> 0) & 0xff;
372 return 4;
373 } else {
374 if (flash->chip->feature_bits & FEATURE_4BA_EXT_ADDR) {
375 if (spi_set_extended_address(flash, addr >> 24))
376 return -1;
Nico Huber1cf407b2017-11-10 20:18:23 +0100377 } else if (addr >> 24) {
378 msg_cerr("Can't handle 4-byte address for opcode '0x%02x'\n"
379 "with this chip/programmer combination.\n", cmd_buf[0]);
380 return -1;
Nico Huberf43c6542017-10-14 17:47:28 +0200381 }
382 cmd_buf[1] = (addr >> 16) & 0xff;
383 cmd_buf[2] = (addr >> 8) & 0xff;
384 cmd_buf[3] = (addr >> 0) & 0xff;
385 return 3;
386 }
Nico Huber0ecbacb2017-10-14 16:50:43 +0200387}
388
389/**
390 * Execute WREN plus another `op` that takes an address and
391 * optional data, poll WIP afterwards.
392 *
393 * @param flash the flash chip's context
394 * @param op the operation to execute
Nico Hubera1672f82017-10-14 18:00:20 +0200395 * @param native_4ba whether `op` always takes a 4-byte address
Nico Huber0ecbacb2017-10-14 16:50:43 +0200396 * @param addr the address parameter to `op`
397 * @param out_bytes bytes to send after the address,
398 * may be NULL if and only if `out_bytes` is 0
399 * @param out_bytes number of bytes to send, 256 at most, may be zero
400 * @param poll_delay interval in us for polling WIP
401 * @return 0 on success, non-zero otherwise
402 */
Nico Hubera1672f82017-10-14 18:00:20 +0200403static int spi_write_cmd(struct flashctx *const flash, const uint8_t op,
404 const bool native_4ba, const unsigned int addr,
Nico Huber0ecbacb2017-10-14 16:50:43 +0200405 const uint8_t *const out_bytes, const size_t out_len,
406 const unsigned int poll_delay)
407{
408 uint8_t cmd[1 + JEDEC_MAX_ADDR_LEN + 256];
409 struct spi_command cmds[] = {
410 {
Richard Hughesdf490582018-12-19 11:57:15 +0000411 .readarr = 0,
Nico Huber0ecbacb2017-10-14 16:50:43 +0200412 .writecnt = 1,
413 .writearr = (const unsigned char[]){ JEDEC_WREN },
414 }, {
Richard Hughesdf490582018-12-19 11:57:15 +0000415 .readarr = 0,
Nico Huber0ecbacb2017-10-14 16:50:43 +0200416 .writearr = cmd,
417 },
418 NULL_SPI_CMD,
419 };
420
421 cmd[0] = op;
Nico Hubera1672f82017-10-14 18:00:20 +0200422 const int addr_len = spi_prepare_address(flash, cmd, native_4ba, addr);
Nico Huber0ecbacb2017-10-14 16:50:43 +0200423 if (addr_len < 0)
424 return 1;
425
426 if (1 + addr_len + out_len > sizeof(cmd)) {
427 msg_cerr("%s called for too long a write\n", __func__);
428 return 1;
429 }
430
431 memcpy(cmd + 1 + addr_len, out_bytes, out_len);
432 cmds[1].writecnt = 1 + addr_len + out_len;
433
434 const int result = spi_send_multicommand(flash, cmds);
435 if (result)
436 msg_cerr("%s failed during command execution at address 0x%x\n", __func__, addr);
437
438 const int status = spi_poll_wip(flash, poll_delay);
439
440 return result ? result : status;
Nico Hubera3140d02017-10-15 11:20:58 +0200441}
442
Jacob Garberbeeb8bc2019-06-21 15:24:17 -0600443static int spi_chip_erase_60(struct flashctx *flash)
Nico Hubera3140d02017-10-15 11:20:58 +0200444{
445 /* This usually takes 1-85s, so wait in 1s steps. */
446 return spi_simple_write_cmd(flash, 0x60, 1000 * 1000);
Sean Nelson14ba6682010-02-26 05:48:29 +0000447}
448
Jacob Garberbeeb8bc2019-06-21 15:24:17 -0600449static int spi_chip_erase_62(struct flashctx *flash)
Stefan Tauner3c0fcd02012-09-21 12:46:56 +0000450{
Nico Hubera3140d02017-10-15 11:20:58 +0200451 /* This usually takes 2-5s, so wait in 100ms steps. */
452 return spi_simple_write_cmd(flash, 0x62, 100 * 1000);
Stefan Tauner3c0fcd02012-09-21 12:46:56 +0000453}
454
Jacob Garberbeeb8bc2019-06-21 15:24:17 -0600455static int spi_chip_erase_c7(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000456{
Nico Hubera3140d02017-10-15 11:20:58 +0200457 /* This usually takes 1-85s, so wait in 1s steps. */
458 return spi_simple_write_cmd(flash, 0xc7, 1000 * 1000);
Sean Nelson14ba6682010-02-26 05:48:29 +0000459}
460
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000461int spi_block_erase_52(struct flashctx *flash, unsigned int addr,
462 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000463{
Nico Huber0ecbacb2017-10-14 16:50:43 +0200464 /* This usually takes 100-4000ms, so wait in 100ms steps. */
Nico Hubera1672f82017-10-14 18:00:20 +0200465 return spi_write_cmd(flash, 0x52, false, addr, NULL, 0, 100 * 1000);
Sean Nelson14ba6682010-02-26 05:48:29 +0000466}
467
468/* Block size is usually
Nikolay Nikolaev6f59b0b2013-06-28 21:29:51 +0000469 * 32M (one die) for Micron
470 */
471int spi_block_erase_c4(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
472{
Nico Huber0ecbacb2017-10-14 16:50:43 +0200473 /* This usually takes 240-480s, so wait in 500ms steps. */
Nico Hubera1672f82017-10-14 18:00:20 +0200474 return spi_write_cmd(flash, 0xc4, false, addr, NULL, 0, 500 * 1000);
Nikolay Nikolaev6f59b0b2013-06-28 21:29:51 +0000475}
476
477/* Block size is usually
Sean Nelson14ba6682010-02-26 05:48:29 +0000478 * 64k for Macronix
479 * 32k for SST
480 * 4-32k non-uniform for EON
481 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000482int spi_block_erase_d8(struct flashctx *flash, unsigned int addr,
483 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000484{
Nico Huber0ecbacb2017-10-14 16:50:43 +0200485 /* This usually takes 100-4000ms, so wait in 100ms steps. */
Nico Hubera1672f82017-10-14 18:00:20 +0200486 return spi_write_cmd(flash, 0xd8, false, addr, NULL, 0, 100 * 1000);
Sean Nelson14ba6682010-02-26 05:48:29 +0000487}
488
489/* Block size is usually
490 * 4k for PMC
491 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000492int spi_block_erase_d7(struct flashctx *flash, unsigned int addr,
493 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000494{
Nico Huber0ecbacb2017-10-14 16:50:43 +0200495 /* This usually takes 100-4000ms, so wait in 100ms steps. */
Nico Hubera1672f82017-10-14 18:00:20 +0200496 return spi_write_cmd(flash, 0xd7, false, addr, NULL, 0, 100 * 1000);
Sean Nelson14ba6682010-02-26 05:48:29 +0000497}
498
Nikolay Nikolaev579f1e02013-06-28 21:28:37 +0000499/* Page erase (usually 256B blocks) */
500int spi_block_erase_db(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
501{
Nico Huber0ecbacb2017-10-14 16:50:43 +0200502 /* This takes up to 20ms usually (on worn out devices
503 up to the 0.5s range), so wait in 1ms steps. */
Nico Hubera1672f82017-10-14 18:00:20 +0200504 return spi_write_cmd(flash, 0xdb, false, addr, NULL, 0, 1 * 1000);
Nikolay Nikolaev579f1e02013-06-28 21:28:37 +0000505}
506
Sean Nelson14ba6682010-02-26 05:48:29 +0000507/* Sector size is usually 4k, though Macronix eliteflash has 64k */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000508int spi_block_erase_20(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 15-800ms, so wait in 10ms steps. */
Nico Hubera1672f82017-10-14 18:00:20 +0200512 return spi_write_cmd(flash, 0x20, false, addr, NULL, 0, 10 * 1000);
Sean Nelson14ba6682010-02-26 05:48:29 +0000513}
514
Stefan Tauner94b39b42012-10-27 00:06:02 +0000515int spi_block_erase_50(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
516{
Nico Huber0ecbacb2017-10-14 16:50:43 +0200517 /* This usually takes 10ms, so wait in 1ms steps. */
Nico Hubera1672f82017-10-14 18:00:20 +0200518 return spi_write_cmd(flash, 0x50, false, addr, NULL, 0, 1 * 1000);
Stefan Tauner94b39b42012-10-27 00:06:02 +0000519}
520
521int spi_block_erase_81(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
522{
Nico Huber0ecbacb2017-10-14 16:50:43 +0200523 /* This usually takes 8ms, so wait in 1ms steps. */
Nico Hubera1672f82017-10-14 18:00:20 +0200524 return spi_write_cmd(flash, 0x81, false, addr, NULL, 0, 1 * 1000);
Stefan Tauner94b39b42012-10-27 00:06:02 +0000525}
526
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000527int spi_block_erase_60(struct flashctx *flash, unsigned int addr,
528 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000529{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000530 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000531 msg_cerr("%s called with incorrect arguments\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000532 __func__);
533 return -1;
534 }
535 return spi_chip_erase_60(flash);
536}
537
Stefan Tauner3c0fcd02012-09-21 12:46:56 +0000538int spi_block_erase_62(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
539{
540 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
541 msg_cerr("%s called with incorrect arguments\n",
542 __func__);
543 return -1;
544 }
545 return spi_chip_erase_62(flash);
546}
547
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000548int spi_block_erase_c7(struct flashctx *flash, unsigned int addr,
549 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000550{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000551 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000552 msg_cerr("%s called with incorrect arguments\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000553 __func__);
554 return -1;
555 }
556 return spi_chip_erase_c7(flash);
557}
558
Nico Huber7e3c81a2017-10-14 18:56:50 +0200559/* Erase 4 KB of flash with 4-bytes address from ANY mode (3-bytes or 4-bytes) */
560int spi_block_erase_21(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
561{
562 /* This usually takes 15-800ms, so wait in 10ms steps. */
563 return spi_write_cmd(flash, 0x21, true, addr, NULL, 0, 10 * 1000);
564}
565
566/* Erase 32 KB of flash with 4-bytes address from ANY mode (3-bytes or 4-bytes) */
567int spi_block_erase_5c(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
568{
569 /* This usually takes 100-4000ms, so wait in 100ms steps. */
570 return spi_write_cmd(flash, 0x5c, true, addr, NULL, 0, 100 * 1000);
571}
572
573/* Erase 64 KB of flash with 4-bytes address from ANY mode (3-bytes or 4-bytes) */
574int spi_block_erase_dc(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
575{
576 /* This usually takes 100-4000ms, so wait in 100ms steps. */
577 return spi_write_cmd(flash, 0xdc, true, addr, NULL, 0, 100 * 1000);
578}
579
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000580erasefunc_t *spi_get_erasefn_from_opcode(uint8_t opcode)
581{
582 switch(opcode){
583 case 0xff:
584 case 0x00:
585 /* Not specified, assuming "not supported". */
586 return NULL;
587 case 0x20:
588 return &spi_block_erase_20;
Nico Huber7e3c81a2017-10-14 18:56:50 +0200589 case 0x21:
590 return &spi_block_erase_21;
Stefan Tauner730e7e72013-05-01 14:04:19 +0000591 case 0x50:
592 return &spi_block_erase_50;
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000593 case 0x52:
594 return &spi_block_erase_52;
Nico Huber7e3c81a2017-10-14 18:56:50 +0200595 case 0x5c:
596 return &spi_block_erase_5c;
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000597 case 0x60:
598 return &spi_block_erase_60;
Stefan Tauner730e7e72013-05-01 14:04:19 +0000599 case 0x62:
600 return &spi_block_erase_62;
601 case 0x81:
602 return &spi_block_erase_81;
Nikolay Nikolaev6f59b0b2013-06-28 21:29:51 +0000603 case 0xc4:
604 return &spi_block_erase_c4;
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000605 case 0xc7:
606 return &spi_block_erase_c7;
607 case 0xd7:
608 return &spi_block_erase_d7;
609 case 0xd8:
610 return &spi_block_erase_d8;
Nikolay Nikolaev579f1e02013-06-28 21:28:37 +0000611 case 0xdb:
612 return &spi_block_erase_db;
Nico Huber7e3c81a2017-10-14 18:56:50 +0200613 case 0xdc:
614 return &spi_block_erase_dc;
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000615 default:
616 msg_cinfo("%s: unknown erase opcode (0x%02x). Please report "
617 "this at flashrom@flashrom.org\n", __func__, opcode);
618 return NULL;
619 }
620}
621
Nico Huber0ecbacb2017-10-14 16:50:43 +0200622static int spi_nbyte_program(struct flashctx *flash, unsigned int addr, const uint8_t *bytes, unsigned int len)
Sean Nelson14ba6682010-02-26 05:48:29 +0000623{
Nico Huber1cf407b2017-11-10 20:18:23 +0100624 const bool native_4ba = flash->chip->feature_bits & FEATURE_4BA_WRITE && spi_master_4ba(flash);
Nico Hubera1672f82017-10-14 18:00:20 +0200625 const uint8_t op = native_4ba ? JEDEC_BYTE_PROGRAM_4BA : JEDEC_BYTE_PROGRAM;
626 return spi_write_cmd(flash, op, native_4ba, addr, bytes, len, 10);
Sean Nelson14ba6682010-02-26 05:48:29 +0000627}
628
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000629int spi_nbyte_read(struct flashctx *flash, unsigned int address, uint8_t *bytes,
630 unsigned int len)
Sean Nelson14ba6682010-02-26 05:48:29 +0000631{
Nico Huber1cf407b2017-11-10 20:18:23 +0100632 const bool native_4ba = flash->chip->feature_bits & FEATURE_4BA_READ && spi_master_4ba(flash);
Nico Hubera1672f82017-10-14 18:00:20 +0200633 uint8_t cmd[1 + JEDEC_MAX_ADDR_LEN] = { native_4ba ? JEDEC_READ_4BA : JEDEC_READ, };
Nico Huber0ecbacb2017-10-14 16:50:43 +0200634
Nico Hubera1672f82017-10-14 18:00:20 +0200635 const int addr_len = spi_prepare_address(flash, cmd, native_4ba, address);
Nico Huber0ecbacb2017-10-14 16:50:43 +0200636 if (addr_len < 0)
637 return 1;
Sean Nelson14ba6682010-02-26 05:48:29 +0000638
639 /* Send Read */
Nico Huber0ecbacb2017-10-14 16:50:43 +0200640 return spi_send_command(flash, 1 + addr_len, len, cmd, bytes);
Sean Nelson14ba6682010-02-26 05:48:29 +0000641}
642
643/*
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000644 * Read a part of the flash chip.
Nico Huberd8b2e802019-06-18 23:39:56 +0200645 * Data is read in chunks with a maximum size of chunksize.
Sean Nelson14ba6682010-02-26 05:48:29 +0000646 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000647int spi_read_chunked(struct flashctx *flash, uint8_t *buf, unsigned int start,
648 unsigned int len, unsigned int chunksize)
Sean Nelson14ba6682010-02-26 05:48:29 +0000649{
Nico Huberd8b2e802019-06-18 23:39:56 +0200650 int ret;
651 size_t to_read;
652 for (; len; len -= to_read, buf += to_read, start += to_read) {
653 to_read = min(chunksize, len);
654 ret = spi_nbyte_read(flash, start, buf, to_read);
655 if (ret)
656 return ret;
Sean Nelson14ba6682010-02-26 05:48:29 +0000657 }
Nico Huberd8b2e802019-06-18 23:39:56 +0200658 return 0;
Sean Nelson14ba6682010-02-26 05:48:29 +0000659}
660
661/*
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000662 * Write a part of the flash chip.
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000663 * FIXME: Use the chunk code from Michael Karcher instead.
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000664 * Each page is written separately in chunks with a maximum size of chunksize.
665 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000666int spi_write_chunked(struct flashctx *flash, const uint8_t *buf, unsigned int start,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000667 unsigned int len, unsigned int chunksize)
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000668{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000669 unsigned int i, j, starthere, lenhere, towrite;
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000670 /* FIXME: page_size is the wrong variable. We need max_writechunk_size
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000671 * in struct flashctx to do this properly. All chips using
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000672 * spi_chip_write_256 have page_size set to max_writechunk_size, so
673 * we're OK for now.
674 */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000675 unsigned int page_size = flash->chip->page_size;
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000676
677 /* Warning: This loop has a very unusual condition and body.
678 * The loop needs to go through each page with at least one affected
679 * byte. The lowest page number is (start / page_size) since that
680 * division rounds down. The highest page number we want is the page
681 * where the last byte of the range lives. That last byte has the
682 * address (start + len - 1), thus the highest page number is
683 * (start + len - 1) / page_size. Since we want to include that last
684 * page as well, the loop condition uses <=.
685 */
686 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
687 /* Byte position of the first byte in the range in this page. */
688 /* starthere is an offset to the base address of the chip. */
689 starthere = max(start, i * page_size);
690 /* Length of bytes in the range in this page. */
691 lenhere = min(start + len, (i + 1) * page_size) - starthere;
692 for (j = 0; j < lenhere; j += chunksize) {
Nico Huber7a077222017-10-14 18:18:30 +0200693 int rc;
694
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000695 towrite = min(chunksize, lenhere - j);
Nico Huber7a077222017-10-14 18:18:30 +0200696 rc = spi_nbyte_program(flash, starthere + j, buf + starthere - start + j, towrite);
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000697 if (rc)
Nico Huber7a077222017-10-14 18:18:30 +0200698 return rc;
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000699 }
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000700 }
701
Nico Huber7a077222017-10-14 18:18:30 +0200702 return 0;
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000703}
704
705/*
Sean Nelson14ba6682010-02-26 05:48:29 +0000706 * Program chip using byte programming. (SLOW!)
707 * This is for chips which can only handle one byte writes
708 * and for chips where memory mapped programming is impossible
709 * (e.g. due to size constraints in IT87* for over 512 kB)
710 */
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000711/* real chunksize is 1, logical chunksize is 1 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000712int spi_chip_write_1(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
Sean Nelson14ba6682010-02-26 05:48:29 +0000713{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000714 unsigned int i;
Sean Nelson14ba6682010-02-26 05:48:29 +0000715
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000716 for (i = start; i < start + len; i++) {
Nico Huber7a077222017-10-14 18:18:30 +0200717 if (spi_nbyte_program(flash, i, buf + i - start, 1))
Sean Nelson14ba6682010-02-26 05:48:29 +0000718 return 1;
Sean Nelson14ba6682010-02-26 05:48:29 +0000719 }
Sean Nelson14ba6682010-02-26 05:48:29 +0000720 return 0;
721}
722
Mark Marshallf20b7be2014-05-09 21:16:21 +0000723int 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 +0000724{
725 uint32_t pos = start;
Sean Nelson14ba6682010-02-26 05:48:29 +0000726 int result;
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +0000727 unsigned char cmd[JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE] = {
728 JEDEC_AAI_WORD_PROGRAM,
729 };
Sean Nelson14ba6682010-02-26 05:48:29 +0000730
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000731 /* The even start address and even length requirements can be either
732 * honored outside this function, or we can call spi_byte_program
733 * for the first and/or last byte and use AAI for the rest.
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +0000734 * FIXME: Move this to generic code.
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000735 */
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +0000736 /* The data sheet requires a start address with the low bit cleared. */
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000737 if (start % 2) {
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +0000738 msg_cerr("%s: start address not even! Please report a bug at "
739 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +0000740 if (spi_chip_write_1(flash, buf, start, start % 2))
741 return SPI_GENERIC_ERROR;
742 pos += start % 2;
743 /* Do not return an error for now. */
744 //return SPI_GENERIC_ERROR;
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +0000745 }
746 /* The data sheet requires total AAI write length to be even. */
747 if (len % 2) {
748 msg_cerr("%s: total write length not even! Please report a "
749 "bug at flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +0000750 /* Do not return an error for now. */
751 //return SPI_GENERIC_ERROR;
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +0000752 }
753
Nico Hubera1672f82017-10-14 18:00:20 +0200754 result = spi_write_cmd(flash, JEDEC_AAI_WORD_PROGRAM, false, start, buf + pos - start, 2, 10);
Nico Huber0ecbacb2017-10-14 16:50:43 +0200755 if (result)
Stefan Reinauer87ace662014-04-26 16:12:55 +0000756 goto bailout;
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +0000757
758 /* We already wrote 2 bytes in the multicommand step. */
759 pos += 2;
760
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +0000761 /* Are there at least two more bytes to write? */
762 while (pos < start + len - 1) {
Carl-Daniel Hailfingerccfe0ac2010-10-27 22:07:11 +0000763 cmd[1] = buf[pos++ - start];
764 cmd[2] = buf[pos++ - start];
Stefan Reinauer87ace662014-04-26 16:12:55 +0000765 result = spi_send_command(flash, JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE, 0, cmd, NULL);
766 if (result != 0) {
767 msg_cerr("%s failed during followup AAI command execution: %d\n", __func__, result);
768 goto bailout;
769 }
Nico Huber0ecbacb2017-10-14 16:50:43 +0200770 if (spi_poll_wip(flash, 10))
771 goto bailout;
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +0000772 }
773
Stefan Tauner59c4d792014-04-26 16:13:09 +0000774 /* Use WRDI to exit AAI mode. This needs to be done before issuing any other non-AAI command. */
775 result = spi_write_disable(flash);
776 if (result != 0) {
777 msg_cerr("%s failed to disable AAI mode.\n", __func__);
778 return SPI_GENERIC_ERROR;
779 }
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +0000780
781 /* Write remaining byte (if any). */
782 if (pos < start + len) {
Carl-Daniel Hailfingerccfe0ac2010-10-27 22:07:11 +0000783 if (spi_chip_write_1(flash, buf + pos - start, pos, pos % 2))
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +0000784 return SPI_GENERIC_ERROR;
785 pos += pos % 2;
786 }
787
Sean Nelson14ba6682010-02-26 05:48:29 +0000788 return 0;
Stefan Reinauer87ace662014-04-26 16:12:55 +0000789
790bailout:
Stefan Tauner59c4d792014-04-26 16:13:09 +0000791 result = spi_write_disable(flash);
792 if (result != 0)
793 msg_cerr("%s failed to disable AAI mode.\n", __func__);
Stefan Reinauer87ace662014-04-26 16:12:55 +0000794 return SPI_GENERIC_ERROR;
Sean Nelson14ba6682010-02-26 05:48:29 +0000795}
Nico Huber7e3c81a2017-10-14 18:56:50 +0200796
Nico Huberfe34d2a2017-11-10 21:10:20 +0100797static int spi_enter_exit_4ba(struct flashctx *const flash, const bool enter)
Nico Huber7e3c81a2017-10-14 18:56:50 +0200798{
Nico Huberfe34d2a2017-11-10 21:10:20 +0100799 const unsigned char cmd = enter ? JEDEC_ENTER_4_BYTE_ADDR_MODE : JEDEC_EXIT_4_BYTE_ADDR_MODE;
800 int ret = 1;
Nico Huber7e3c81a2017-10-14 18:56:50 +0200801
Nico Huberfe34d2a2017-11-10 21:10:20 +0100802 if (flash->chip->feature_bits & FEATURE_4BA_ENTER)
803 ret = spi_send_command(flash, sizeof(cmd), 0, &cmd, NULL);
804 else if (flash->chip->feature_bits & FEATURE_4BA_ENTER_WREN)
805 ret = spi_simple_write_cmd(flash, cmd, 0);
Nico Huber86bddb52018-03-13 18:14:52 +0100806 else if (flash->chip->feature_bits & FEATURE_4BA_ENTER_EAR7)
807 ret = spi_set_extended_address(flash, enter ? 0x80 : 0x00);
Nico Huberfe34d2a2017-11-10 21:10:20 +0100808
Nico Huber7e3c81a2017-10-14 18:56:50 +0200809 if (!ret)
Nico Huberfe34d2a2017-11-10 21:10:20 +0100810 flash->in_4ba_mode = enter;
Nico Huber7e3c81a2017-10-14 18:56:50 +0200811 return ret;
812}
813
Nico Huberfe34d2a2017-11-10 21:10:20 +0100814int spi_enter_4ba(struct flashctx *const flash)
Nico Huber7e3c81a2017-10-14 18:56:50 +0200815{
Nico Huberfe34d2a2017-11-10 21:10:20 +0100816 return spi_enter_exit_4ba(flash, true);
Nico Huber7e3c81a2017-10-14 18:56:50 +0200817}
818
Nico Huberfe34d2a2017-11-10 21:10:20 +0100819int spi_exit_4ba(struct flashctx *flash)
Nico Huber7e3c81a2017-10-14 18:56:50 +0200820{
Nico Huberfe34d2a2017-11-10 21:10:20 +0100821 return spi_enter_exit_4ba(flash, false);
Nico Huber7e3c81a2017-10-14 18:56:50 +0200822}