blob: c403570afc1b4afcf2f5bfc8b37180b8fec6223c [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
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000103 if (spi_rdid(flash, readarr, bytes)) {
Sean Nelson14ba6682010-02-26 05:48:29 +0000104 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000105 }
Sean Nelson14ba6682010-02-26 05:48:29 +0000106
107 if (!oddparity(readarr[0]))
Sean Nelsoned479d22010-03-24 23:14:32 +0000108 msg_cdbg("RDID byte 0 parity violation. ");
Sean Nelson14ba6682010-02-26 05:48:29 +0000109
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000110 /* Check if this is a continuation vendor ID.
111 * FIXME: Handle continuation device IDs.
112 */
Sean Nelson14ba6682010-02-26 05:48:29 +0000113 if (readarr[0] == 0x7f) {
114 if (!oddparity(readarr[1]))
Sean Nelsoned479d22010-03-24 23:14:32 +0000115 msg_cdbg("RDID byte 1 parity violation. ");
Sean Nelson14ba6682010-02-26 05:48:29 +0000116 id1 = (readarr[0] << 8) | readarr[1];
117 id2 = readarr[2];
118 if (bytes > 3) {
119 id2 <<= 8;
120 id2 |= readarr[3];
121 }
122 } else {
123 id1 = readarr[0];
124 id2 = (readarr[1] << 8) | readarr[2];
125 }
126
Sean Nelsoned479d22010-03-24 23:14:32 +0000127 msg_cdbg("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
Sean Nelson14ba6682010-02-26 05:48:29 +0000128
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000129 if (id1 == chip->manufacture_id && id2 == chip->model_id)
Sean Nelson14ba6682010-02-26 05:48:29 +0000130 return 1;
Sean Nelson14ba6682010-02-26 05:48:29 +0000131
132 /* Test if this is a pure vendor match. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000133 if (id1 == chip->manufacture_id && GENERIC_DEVICE_ID == chip->model_id)
Sean Nelson14ba6682010-02-26 05:48:29 +0000134 return 1;
135
136 /* Test if there is any vendor ID. */
Urja Rannikko0a5f6e42015-06-22 23:59:15 +0000137 if (GENERIC_MANUF_ID == chip->manufacture_id && id1 != 0xff && id1 != 0x00)
Sean Nelson14ba6682010-02-26 05:48:29 +0000138 return 1;
139
140 return 0;
141}
142
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000143int probe_spi_rdid(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000144{
145 return probe_spi_rdid_generic(flash, 3);
146}
147
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000148int probe_spi_rdid4(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000149{
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000150 /* Some SPI controllers do not support commands with writecnt=1 and
151 * readcnt=4.
152 */
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000153 switch (flash->mst->spi.type) {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000154#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000155#if defined(__i386__) || defined(__x86_64__)
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000156 case SPI_CONTROLLER_IT87XX:
Sean Nelson14ba6682010-02-26 05:48:29 +0000157 case SPI_CONTROLLER_WBSIO:
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000158 msg_cinfo("4 byte RDID not supported on this SPI controller\n");
159 return 0;
160 break;
Sean Nelson14ba6682010-02-26 05:48:29 +0000161#endif
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000162#endif
Sean Nelson14ba6682010-02-26 05:48:29 +0000163 default:
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000164 return probe_spi_rdid_generic(flash, 4);
Sean Nelson14ba6682010-02-26 05:48:29 +0000165 }
166
167 return 0;
168}
169
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000170int probe_spi_rems(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000171{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000172 const struct flashchip *chip = flash->chip;
Sean Nelson14ba6682010-02-26 05:48:29 +0000173 unsigned char readarr[JEDEC_REMS_INSIZE];
174 uint32_t id1, id2;
175
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000176 if (spi_rems(flash, readarr)) {
Sean Nelson14ba6682010-02-26 05:48:29 +0000177 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000178 }
Sean Nelson14ba6682010-02-26 05:48:29 +0000179
180 id1 = readarr[0];
181 id2 = readarr[1];
182
Sean Nelsoned479d22010-03-24 23:14:32 +0000183 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
Sean Nelson14ba6682010-02-26 05:48:29 +0000184
Stefan Tauner6ee37e22012-12-29 15:03:51 +0000185 if (id1 == chip->manufacture_id && id2 == chip->model_id)
Sean Nelson14ba6682010-02-26 05:48:29 +0000186 return 1;
Sean Nelson14ba6682010-02-26 05:48:29 +0000187
188 /* Test if this is a pure vendor match. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000189 if (id1 == chip->manufacture_id && GENERIC_DEVICE_ID == chip->model_id)
Sean Nelson14ba6682010-02-26 05:48:29 +0000190 return 1;
191
192 /* Test if there is any vendor ID. */
Urja Rannikko0a5f6e42015-06-22 23:59:15 +0000193 if (GENERIC_MANUF_ID == chip->manufacture_id && id1 != 0xff && id1 != 0x00)
Sean Nelson14ba6682010-02-26 05:48:29 +0000194 return 1;
195
196 return 0;
197}
198
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000199int probe_spi_res1(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000200{
Mathias Krausea60faab2011-01-17 07:50:42 +0000201 static const unsigned char allff[] = {0xff, 0xff, 0xff};
202 static const unsigned char all00[] = {0x00, 0x00, 0x00};
Sean Nelson14ba6682010-02-26 05:48:29 +0000203 unsigned char readarr[3];
204 uint32_t id2;
Sean Nelson14ba6682010-02-26 05:48:29 +0000205
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000206 /* We only want one-byte RES if RDID and REMS are unusable. */
207
Sean Nelson14ba6682010-02-26 05:48:29 +0000208 /* Check if RDID is usable and does not return 0xff 0xff 0xff or
209 * 0x00 0x00 0x00. In that case, RES is pointless.
210 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000211 if (!spi_rdid(flash, readarr, 3) && memcmp(readarr, allff, 3) &&
Sean Nelson14ba6682010-02-26 05:48:29 +0000212 memcmp(readarr, all00, 3)) {
213 msg_cdbg("Ignoring RES in favour of RDID.\n");
214 return 0;
215 }
216 /* Check if REMS is usable and does not return 0xff 0xff or
217 * 0x00 0x00. In that case, RES is pointless.
218 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000219 if (!spi_rems(flash, readarr) &&
220 memcmp(readarr, allff, JEDEC_REMS_INSIZE) &&
Sean Nelson14ba6682010-02-26 05:48:29 +0000221 memcmp(readarr, all00, JEDEC_REMS_INSIZE)) {
222 msg_cdbg("Ignoring RES in favour of REMS.\n");
223 return 0;
224 }
225
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000226 if (spi_res(flash, readarr, 1)) {
Sean Nelson14ba6682010-02-26 05:48:29 +0000227 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000228 }
Sean Nelson14ba6682010-02-26 05:48:29 +0000229
Sean Nelson14ba6682010-02-26 05:48:29 +0000230 id2 = readarr[0];
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000231
Sean Nelsoned479d22010-03-24 23:14:32 +0000232 msg_cdbg("%s: id 0x%x\n", __func__, id2);
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000233
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000234 if (id2 != flash->chip->model_id)
Sean Nelson14ba6682010-02-26 05:48:29 +0000235 return 0;
236
Sean Nelson14ba6682010-02-26 05:48:29 +0000237 return 1;
238}
239
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000240int probe_spi_res2(struct flashctx *flash)
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000241{
242 unsigned char readarr[2];
243 uint32_t id1, id2;
244
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000245 if (spi_res(flash, readarr, 2)) {
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000246 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000247 }
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000248
249 id1 = readarr[0];
250 id2 = readarr[1];
251
252 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
253
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000254 if (id1 != flash->chip->manufacture_id || id2 != flash->chip->model_id)
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000255 return 0;
256
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000257 return 1;
258}
259
Stefan Tauner3f5e35d2013-04-19 01:58:33 +0000260int probe_spi_res3(struct flashctx *flash)
261{
262 unsigned char readarr[3];
263 uint32_t id1, id2;
264
265 if (spi_res(flash, readarr, 3)) {
266 return 0;
267 }
268
269 id1 = (readarr[0] << 8) | readarr[1];
270 id2 = readarr[2];
271
272 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
273
274 if (id1 != flash->chip->manufacture_id || id2 != flash->chip->model_id)
275 return 0;
276
277 return 1;
278}
279
Stefan Tauner57794ac2012-12-29 15:04:20 +0000280/* Only used for some Atmel chips. */
281int probe_spi_at25f(struct flashctx *flash)
282{
283 static const unsigned char cmd[AT25F_RDID_OUTSIZE] = { AT25F_RDID };
284 unsigned char readarr[AT25F_RDID_INSIZE];
285 uint32_t id1;
286 uint32_t id2;
287
288 if (spi_send_command(flash, sizeof(cmd), sizeof(readarr), cmd, readarr))
289 return 0;
290
291 id1 = readarr[0];
292 id2 = readarr[1];
293
294 msg_cdbg("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
295
296 if (id1 == flash->chip->manufacture_id && id2 == flash->chip->model_id)
297 return 1;
298
299 return 0;
300}
301
Nico Huber0ecbacb2017-10-14 16:50:43 +0200302static int spi_poll_wip(struct flashctx *const flash, const unsigned int poll_delay)
303{
304 /* FIXME: We can't tell if spi_read_status_register() failed. */
305 /* FIXME: We don't time out. */
306 while (spi_read_status_register(flash) & SPI_SR_WIP)
307 programmer_delay(poll_delay);
308 /* FIXME: Check the status register for errors. */
309 return 0;
310}
311
Nico Hubera3140d02017-10-15 11:20:58 +0200312/**
313 * Execute WREN plus another one byte `op`, optionally poll WIP afterwards.
314 *
315 * @param flash the flash chip's context
316 * @param op the operation to execute
317 * @param poll_delay interval in us for polling WIP, don't poll if zero
318 * @return 0 on success, non-zero otherwise
319 */
320static 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 +0000321{
Sean Nelson14ba6682010-02-26 05:48:29 +0000322 struct spi_command cmds[] = {
323 {
Nico Hubera3140d02017-10-15 11:20:58 +0200324 .writecnt = 1,
325 .writearr = (const unsigned char[]){ JEDEC_WREN },
Sean Nelson14ba6682010-02-26 05:48:29 +0000326 }, {
Nico Hubera3140d02017-10-15 11:20:58 +0200327 .writecnt = 1,
328 .writearr = (const unsigned char[]){ op },
329 },
330 NULL_SPI_CMD,
331 };
332
333 const int result = spi_send_multicommand(flash, cmds);
334 if (result)
335 msg_cerr("%s failed during command execution\n", __func__);
336
Nico Huber0ecbacb2017-10-14 16:50:43 +0200337 const int status = poll_delay ? spi_poll_wip(flash, poll_delay) : 0;
Nico Hubera3140d02017-10-15 11:20:58 +0200338
Nico Huber0ecbacb2017-10-14 16:50:43 +0200339 return result ? result : status;
340}
341
Nico Huber7e3c81a2017-10-14 18:56:50 +0200342static int spi_write_extended_address_register(struct flashctx *const flash, const uint8_t regdata)
343{
344 struct spi_command cmds[] = {
345 {
346 .writecnt = 1,
347 .writearr = (const unsigned char[]){ JEDEC_WREN },
348 }, {
349 .writecnt = 2,
350 .writearr = (const unsigned char[]){ JEDEC_WRITE_EXT_ADDR_REG, regdata },
351 },
352 NULL_SPI_CMD,
353 };
354
355 const int result = spi_send_multicommand(flash, cmds);
356 if (result)
357 msg_cerr("%s failed during command execution\n", __func__);
358 return result;
359}
360
Nico Huberf43c6542017-10-14 17:47:28 +0200361static int spi_set_extended_address(struct flashctx *const flash, const uint8_t addr_high)
362{
363 if (flash->address_high_byte != addr_high &&
364 spi_write_extended_address_register(flash, addr_high))
365 return -1;
366 flash->address_high_byte = addr_high;
367 return 0;
368}
369
Nico Hubera1672f82017-10-14 18:00:20 +0200370static int spi_prepare_address(struct flashctx *const flash, uint8_t cmd_buf[],
371 const bool native_4ba, const unsigned int addr)
Nico Huber0ecbacb2017-10-14 16:50:43 +0200372{
Nico Hubera1672f82017-10-14 18:00:20 +0200373 if (native_4ba || flash->in_4ba_mode) {
Nico Huber1cf407b2017-11-10 20:18:23 +0100374 if (!spi_master_4ba(flash)) {
375 msg_cwarn("4-byte address requested but master can't handle 4-byte addresses.\n");
376 return -1;
377 }
Nico Huberf43c6542017-10-14 17:47:28 +0200378 cmd_buf[1] = (addr >> 24) & 0xff;
379 cmd_buf[2] = (addr >> 16) & 0xff;
380 cmd_buf[3] = (addr >> 8) & 0xff;
381 cmd_buf[4] = (addr >> 0) & 0xff;
382 return 4;
383 } else {
384 if (flash->chip->feature_bits & FEATURE_4BA_EXT_ADDR) {
385 if (spi_set_extended_address(flash, addr >> 24))
386 return -1;
Nico Huber1cf407b2017-11-10 20:18:23 +0100387 } else if (addr >> 24) {
388 msg_cerr("Can't handle 4-byte address for opcode '0x%02x'\n"
389 "with this chip/programmer combination.\n", cmd_buf[0]);
390 return -1;
Nico Huberf43c6542017-10-14 17:47:28 +0200391 }
392 cmd_buf[1] = (addr >> 16) & 0xff;
393 cmd_buf[2] = (addr >> 8) & 0xff;
394 cmd_buf[3] = (addr >> 0) & 0xff;
395 return 3;
396 }
Nico Huber0ecbacb2017-10-14 16:50:43 +0200397}
398
399/**
400 * Execute WREN plus another `op` that takes an address and
401 * optional data, poll WIP afterwards.
402 *
403 * @param flash the flash chip's context
404 * @param op the operation to execute
Nico Hubera1672f82017-10-14 18:00:20 +0200405 * @param native_4ba whether `op` always takes a 4-byte address
Nico Huber0ecbacb2017-10-14 16:50:43 +0200406 * @param addr the address parameter to `op`
407 * @param out_bytes bytes to send after the address,
408 * may be NULL if and only if `out_bytes` is 0
409 * @param out_bytes number of bytes to send, 256 at most, may be zero
410 * @param poll_delay interval in us for polling WIP
411 * @return 0 on success, non-zero otherwise
412 */
Nico Hubera1672f82017-10-14 18:00:20 +0200413static int spi_write_cmd(struct flashctx *const flash, const uint8_t op,
414 const bool native_4ba, const unsigned int addr,
Nico Huber0ecbacb2017-10-14 16:50:43 +0200415 const uint8_t *const out_bytes, const size_t out_len,
416 const unsigned int poll_delay)
417{
418 uint8_t cmd[1 + JEDEC_MAX_ADDR_LEN + 256];
419 struct spi_command cmds[] = {
420 {
421 .writecnt = 1,
422 .writearr = (const unsigned char[]){ JEDEC_WREN },
423 }, {
424 .writearr = cmd,
425 },
426 NULL_SPI_CMD,
427 };
428
429 cmd[0] = op;
Nico Hubera1672f82017-10-14 18:00:20 +0200430 const int addr_len = spi_prepare_address(flash, cmd, native_4ba, addr);
Nico Huber0ecbacb2017-10-14 16:50:43 +0200431 if (addr_len < 0)
432 return 1;
433
434 if (1 + addr_len + out_len > sizeof(cmd)) {
435 msg_cerr("%s called for too long a write\n", __func__);
436 return 1;
437 }
438
439 memcpy(cmd + 1 + addr_len, out_bytes, out_len);
440 cmds[1].writecnt = 1 + addr_len + out_len;
441
442 const int result = spi_send_multicommand(flash, cmds);
443 if (result)
444 msg_cerr("%s failed during command execution at address 0x%x\n", __func__, addr);
445
446 const int status = spi_poll_wip(flash, poll_delay);
447
448 return result ? result : status;
Nico Hubera3140d02017-10-15 11:20:58 +0200449}
450
451int spi_chip_erase_60(struct flashctx *flash)
452{
453 /* This usually takes 1-85s, so wait in 1s steps. */
454 return spi_simple_write_cmd(flash, 0x60, 1000 * 1000);
Sean Nelson14ba6682010-02-26 05:48:29 +0000455}
456
Stefan Tauner3c0fcd02012-09-21 12:46:56 +0000457int spi_chip_erase_62(struct flashctx *flash)
458{
Nico Hubera3140d02017-10-15 11:20:58 +0200459 /* This usually takes 2-5s, so wait in 100ms steps. */
460 return spi_simple_write_cmd(flash, 0x62, 100 * 1000);
Stefan Tauner3c0fcd02012-09-21 12:46:56 +0000461}
462
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000463int spi_chip_erase_c7(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000464{
Nico Hubera3140d02017-10-15 11:20:58 +0200465 /* This usually takes 1-85s, so wait in 1s steps. */
466 return spi_simple_write_cmd(flash, 0xc7, 1000 * 1000);
Sean Nelson14ba6682010-02-26 05:48:29 +0000467}
468
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000469int spi_block_erase_52(struct flashctx *flash, unsigned int addr,
470 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000471{
Nico Huber0ecbacb2017-10-14 16:50:43 +0200472 /* This usually takes 100-4000ms, so wait in 100ms steps. */
Nico Hubera1672f82017-10-14 18:00:20 +0200473 return spi_write_cmd(flash, 0x52, false, addr, NULL, 0, 100 * 1000);
Sean Nelson14ba6682010-02-26 05:48:29 +0000474}
475
476/* Block size is usually
Nikolay Nikolaev6f59b0b2013-06-28 21:29:51 +0000477 * 32M (one die) for Micron
478 */
479int spi_block_erase_c4(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
480{
Nico Huber0ecbacb2017-10-14 16:50:43 +0200481 /* This usually takes 240-480s, so wait in 500ms steps. */
Nico Hubera1672f82017-10-14 18:00:20 +0200482 return spi_write_cmd(flash, 0xc4, false, addr, NULL, 0, 500 * 1000);
Nikolay Nikolaev6f59b0b2013-06-28 21:29:51 +0000483}
484
485/* Block size is usually
Sean Nelson14ba6682010-02-26 05:48:29 +0000486 * 64k for Macronix
487 * 32k for SST
488 * 4-32k non-uniform for EON
489 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000490int spi_block_erase_d8(struct flashctx *flash, unsigned int addr,
491 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000492{
Nico Huber0ecbacb2017-10-14 16:50:43 +0200493 /* This usually takes 100-4000ms, so wait in 100ms steps. */
Nico Hubera1672f82017-10-14 18:00:20 +0200494 return spi_write_cmd(flash, 0xd8, false, addr, NULL, 0, 100 * 1000);
Sean Nelson14ba6682010-02-26 05:48:29 +0000495}
496
497/* Block size is usually
498 * 4k for PMC
499 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000500int spi_block_erase_d7(struct flashctx *flash, unsigned int addr,
501 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000502{
Nico Huber0ecbacb2017-10-14 16:50:43 +0200503 /* This usually takes 100-4000ms, so wait in 100ms steps. */
Nico Hubera1672f82017-10-14 18:00:20 +0200504 return spi_write_cmd(flash, 0xd7, false, addr, NULL, 0, 100 * 1000);
Sean Nelson14ba6682010-02-26 05:48:29 +0000505}
506
Nikolay Nikolaev579f1e02013-06-28 21:28:37 +0000507/* Page erase (usually 256B blocks) */
508int spi_block_erase_db(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
509{
Nico Huber0ecbacb2017-10-14 16:50:43 +0200510 /* This takes up to 20ms usually (on worn out devices
511 up to the 0.5s range), so wait in 1ms steps. */
Nico Hubera1672f82017-10-14 18:00:20 +0200512 return spi_write_cmd(flash, 0xdb, false, addr, NULL, 0, 1 * 1000);
Nikolay Nikolaev579f1e02013-06-28 21:28:37 +0000513}
514
Sean Nelson14ba6682010-02-26 05:48:29 +0000515/* Sector size is usually 4k, though Macronix eliteflash has 64k */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000516int spi_block_erase_20(struct flashctx *flash, unsigned int addr,
517 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000518{
Nico Huber0ecbacb2017-10-14 16:50:43 +0200519 /* This usually takes 15-800ms, so wait in 10ms steps. */
Nico Hubera1672f82017-10-14 18:00:20 +0200520 return spi_write_cmd(flash, 0x20, false, addr, NULL, 0, 10 * 1000);
Sean Nelson14ba6682010-02-26 05:48:29 +0000521}
522
Stefan Tauner94b39b42012-10-27 00:06:02 +0000523int spi_block_erase_50(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
524{
Nico Huber0ecbacb2017-10-14 16:50:43 +0200525 /* This usually takes 10ms, so wait in 1ms steps. */
Nico Hubera1672f82017-10-14 18:00:20 +0200526 return spi_write_cmd(flash, 0x50, false, addr, NULL, 0, 1 * 1000);
Stefan Tauner94b39b42012-10-27 00:06:02 +0000527}
528
529int spi_block_erase_81(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
530{
Nico Huber0ecbacb2017-10-14 16:50:43 +0200531 /* This usually takes 8ms, so wait in 1ms steps. */
Nico Hubera1672f82017-10-14 18:00:20 +0200532 return spi_write_cmd(flash, 0x81, false, addr, NULL, 0, 1 * 1000);
Stefan Tauner94b39b42012-10-27 00:06:02 +0000533}
534
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000535int spi_block_erase_60(struct flashctx *flash, unsigned int addr,
536 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000537{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000538 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000539 msg_cerr("%s called with incorrect arguments\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000540 __func__);
541 return -1;
542 }
543 return spi_chip_erase_60(flash);
544}
545
Stefan Tauner3c0fcd02012-09-21 12:46:56 +0000546int spi_block_erase_62(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
547{
548 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
549 msg_cerr("%s called with incorrect arguments\n",
550 __func__);
551 return -1;
552 }
553 return spi_chip_erase_62(flash);
554}
555
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000556int spi_block_erase_c7(struct flashctx *flash, unsigned int addr,
557 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000558{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000559 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000560 msg_cerr("%s called with incorrect arguments\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000561 __func__);
562 return -1;
563 }
564 return spi_chip_erase_c7(flash);
565}
566
Nico Huber7e3c81a2017-10-14 18:56:50 +0200567/* Erase 4 KB of flash with 4-bytes address from ANY mode (3-bytes or 4-bytes) */
568int spi_block_erase_21(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
569{
570 /* This usually takes 15-800ms, so wait in 10ms steps. */
571 return spi_write_cmd(flash, 0x21, true, addr, NULL, 0, 10 * 1000);
572}
573
574/* Erase 32 KB of flash with 4-bytes address from ANY mode (3-bytes or 4-bytes) */
575int spi_block_erase_5c(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
576{
577 /* This usually takes 100-4000ms, so wait in 100ms steps. */
578 return spi_write_cmd(flash, 0x5c, true, addr, NULL, 0, 100 * 1000);
579}
580
581/* Erase 64 KB of flash with 4-bytes address from ANY mode (3-bytes or 4-bytes) */
582int spi_block_erase_dc(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
583{
584 /* This usually takes 100-4000ms, so wait in 100ms steps. */
585 return spi_write_cmd(flash, 0xdc, true, addr, NULL, 0, 100 * 1000);
586}
587
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000588erasefunc_t *spi_get_erasefn_from_opcode(uint8_t opcode)
589{
590 switch(opcode){
591 case 0xff:
592 case 0x00:
593 /* Not specified, assuming "not supported". */
594 return NULL;
595 case 0x20:
596 return &spi_block_erase_20;
Nico Huber7e3c81a2017-10-14 18:56:50 +0200597 case 0x21:
598 return &spi_block_erase_21;
Stefan Tauner730e7e72013-05-01 14:04:19 +0000599 case 0x50:
600 return &spi_block_erase_50;
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000601 case 0x52:
602 return &spi_block_erase_52;
Nico Huber7e3c81a2017-10-14 18:56:50 +0200603 case 0x5c:
604 return &spi_block_erase_5c;
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000605 case 0x60:
606 return &spi_block_erase_60;
Stefan Tauner730e7e72013-05-01 14:04:19 +0000607 case 0x62:
608 return &spi_block_erase_62;
609 case 0x81:
610 return &spi_block_erase_81;
Nikolay Nikolaev6f59b0b2013-06-28 21:29:51 +0000611 case 0xc4:
612 return &spi_block_erase_c4;
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000613 case 0xc7:
614 return &spi_block_erase_c7;
615 case 0xd7:
616 return &spi_block_erase_d7;
617 case 0xd8:
618 return &spi_block_erase_d8;
Nikolay Nikolaev579f1e02013-06-28 21:28:37 +0000619 case 0xdb:
620 return &spi_block_erase_db;
Nico Huber7e3c81a2017-10-14 18:56:50 +0200621 case 0xdc:
622 return &spi_block_erase_dc;
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000623 default:
624 msg_cinfo("%s: unknown erase opcode (0x%02x). Please report "
625 "this at flashrom@flashrom.org\n", __func__, opcode);
626 return NULL;
627 }
628}
629
Nico Huber0ecbacb2017-10-14 16:50:43 +0200630static int spi_nbyte_program(struct flashctx *flash, unsigned int addr, const uint8_t *bytes, 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_WRITE && spi_master_4ba(flash);
Nico Hubera1672f82017-10-14 18:00:20 +0200633 const uint8_t op = native_4ba ? JEDEC_BYTE_PROGRAM_4BA : JEDEC_BYTE_PROGRAM;
634 return spi_write_cmd(flash, op, native_4ba, addr, bytes, len, 10);
Sean Nelson14ba6682010-02-26 05:48:29 +0000635}
636
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000637int spi_nbyte_read(struct flashctx *flash, unsigned int address, uint8_t *bytes,
638 unsigned int len)
Sean Nelson14ba6682010-02-26 05:48:29 +0000639{
Nico Huber1cf407b2017-11-10 20:18:23 +0100640 const bool native_4ba = flash->chip->feature_bits & FEATURE_4BA_READ && spi_master_4ba(flash);
Nico Hubera1672f82017-10-14 18:00:20 +0200641 uint8_t cmd[1 + JEDEC_MAX_ADDR_LEN] = { native_4ba ? JEDEC_READ_4BA : JEDEC_READ, };
Nico Huber0ecbacb2017-10-14 16:50:43 +0200642
Nico Hubera1672f82017-10-14 18:00:20 +0200643 const int addr_len = spi_prepare_address(flash, cmd, native_4ba, address);
Nico Huber0ecbacb2017-10-14 16:50:43 +0200644 if (addr_len < 0)
645 return 1;
Sean Nelson14ba6682010-02-26 05:48:29 +0000646
647 /* Send Read */
Nico Huber0ecbacb2017-10-14 16:50:43 +0200648 return spi_send_command(flash, 1 + addr_len, len, cmd, bytes);
Sean Nelson14ba6682010-02-26 05:48:29 +0000649}
650
651/*
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000652 * Read a part of the flash chip.
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000653 * FIXME: Use the chunk code from Michael Karcher instead.
Urja Rannikko731316a2017-06-15 13:32:01 +0300654 * Each naturally aligned area is read separately in chunks with a maximum size of chunksize.
Sean Nelson14ba6682010-02-26 05:48:29 +0000655 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000656int spi_read_chunked(struct flashctx *flash, uint8_t *buf, unsigned int start,
657 unsigned int len, unsigned int chunksize)
Sean Nelson14ba6682010-02-26 05:48:29 +0000658{
659 int rc = 0;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000660 unsigned int i, j, starthere, lenhere, toread;
Urja Rannikko731316a2017-06-15 13:32:01 +0300661 /* Limit for multi-die 4-byte-addressing chips. */
662 unsigned int area_size = min(flash->chip->total_size * 1024, 16 * 1024 * 1024);
Sean Nelson14ba6682010-02-26 05:48:29 +0000663
664 /* Warning: This loop has a very unusual condition and body.
Urja Rannikko731316a2017-06-15 13:32:01 +0300665 * The loop needs to go through each area with at least one affected
666 * byte. The lowest area number is (start / area_size) since that
667 * division rounds down. The highest area number we want is the area
Sean Nelson14ba6682010-02-26 05:48:29 +0000668 * where the last byte of the range lives. That last byte has the
Urja Rannikko731316a2017-06-15 13:32:01 +0300669 * address (start + len - 1), thus the highest area number is
670 * (start + len - 1) / area_size. Since we want to include that last
671 * area as well, the loop condition uses <=.
Sean Nelson14ba6682010-02-26 05:48:29 +0000672 */
Urja Rannikko731316a2017-06-15 13:32:01 +0300673 for (i = start / area_size; i <= (start + len - 1) / area_size; i++) {
674 /* Byte position of the first byte in the range in this area. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000675 /* starthere is an offset to the base address of the chip. */
Urja Rannikko731316a2017-06-15 13:32:01 +0300676 starthere = max(start, i * area_size);
677 /* Length of bytes in the range in this area. */
678 lenhere = min(start + len, (i + 1) * area_size) - starthere;
Sean Nelson14ba6682010-02-26 05:48:29 +0000679 for (j = 0; j < lenhere; j += chunksize) {
680 toread = min(chunksize, lenhere - j);
Nico Huber7a077222017-10-14 18:18:30 +0200681 rc = spi_nbyte_read(flash, starthere + j, buf + starthere - start + j, toread);
Sean Nelson14ba6682010-02-26 05:48:29 +0000682 if (rc)
683 break;
684 }
685 if (rc)
686 break;
687 }
688
689 return rc;
690}
691
692/*
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000693 * Write a part of the flash chip.
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000694 * FIXME: Use the chunk code from Michael Karcher instead.
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000695 * Each page is written separately in chunks with a maximum size of chunksize.
696 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000697int spi_write_chunked(struct flashctx *flash, const uint8_t *buf, unsigned int start,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000698 unsigned int len, unsigned int chunksize)
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000699{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000700 unsigned int i, j, starthere, lenhere, towrite;
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000701 /* FIXME: page_size is the wrong variable. We need max_writechunk_size
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000702 * in struct flashctx to do this properly. All chips using
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000703 * spi_chip_write_256 have page_size set to max_writechunk_size, so
704 * we're OK for now.
705 */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000706 unsigned int page_size = flash->chip->page_size;
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000707
708 /* Warning: This loop has a very unusual condition and body.
709 * The loop needs to go through each page with at least one affected
710 * byte. The lowest page number is (start / page_size) since that
711 * division rounds down. The highest page number we want is the page
712 * where the last byte of the range lives. That last byte has the
713 * address (start + len - 1), thus the highest page number is
714 * (start + len - 1) / page_size. Since we want to include that last
715 * page as well, the loop condition uses <=.
716 */
717 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
718 /* Byte position of the first byte in the range in this page. */
719 /* starthere is an offset to the base address of the chip. */
720 starthere = max(start, i * page_size);
721 /* Length of bytes in the range in this page. */
722 lenhere = min(start + len, (i + 1) * page_size) - starthere;
723 for (j = 0; j < lenhere; j += chunksize) {
Nico Huber7a077222017-10-14 18:18:30 +0200724 int rc;
725
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000726 towrite = min(chunksize, lenhere - j);
Nico Huber7a077222017-10-14 18:18:30 +0200727 rc = spi_nbyte_program(flash, starthere + j, buf + starthere - start + j, towrite);
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000728 if (rc)
Nico Huber7a077222017-10-14 18:18:30 +0200729 return rc;
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000730 }
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000731 }
732
Nico Huber7a077222017-10-14 18:18:30 +0200733 return 0;
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000734}
735
736/*
Sean Nelson14ba6682010-02-26 05:48:29 +0000737 * Program chip using byte programming. (SLOW!)
738 * This is for chips which can only handle one byte writes
739 * and for chips where memory mapped programming is impossible
740 * (e.g. due to size constraints in IT87* for over 512 kB)
741 */
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000742/* real chunksize is 1, logical chunksize is 1 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000743int spi_chip_write_1(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
Sean Nelson14ba6682010-02-26 05:48:29 +0000744{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000745 unsigned int i;
Sean Nelson14ba6682010-02-26 05:48:29 +0000746
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000747 for (i = start; i < start + len; i++) {
Nico Huber7a077222017-10-14 18:18:30 +0200748 if (spi_nbyte_program(flash, i, buf + i - start, 1))
Sean Nelson14ba6682010-02-26 05:48:29 +0000749 return 1;
Sean Nelson14ba6682010-02-26 05:48:29 +0000750 }
Sean Nelson14ba6682010-02-26 05:48:29 +0000751 return 0;
752}
753
Mark Marshallf20b7be2014-05-09 21:16:21 +0000754int 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 +0000755{
756 uint32_t pos = start;
Sean Nelson14ba6682010-02-26 05:48:29 +0000757 int result;
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +0000758 unsigned char cmd[JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE] = {
759 JEDEC_AAI_WORD_PROGRAM,
760 };
Sean Nelson14ba6682010-02-26 05:48:29 +0000761
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000762 switch (flash->mst->spi.type) {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000763#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000764#if defined(__i386__) || defined(__x86_64__)
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +0000765 case SPI_CONTROLLER_IT87XX:
Sean Nelson14ba6682010-02-26 05:48:29 +0000766 case SPI_CONTROLLER_WBSIO:
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000767 msg_perr("%s: impossible with this SPI controller,"
Sean Nelson14ba6682010-02-26 05:48:29 +0000768 " degrading to byte program\n", __func__);
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +0000769 return spi_chip_write_1(flash, buf, start, len);
Sean Nelson14ba6682010-02-26 05:48:29 +0000770#endif
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000771#endif
Sean Nelson14ba6682010-02-26 05:48:29 +0000772 default:
773 break;
774 }
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +0000775
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000776 /* The even start address and even length requirements can be either
777 * honored outside this function, or we can call spi_byte_program
778 * for the first and/or last byte and use AAI for the rest.
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +0000779 * FIXME: Move this to generic code.
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000780 */
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +0000781 /* The data sheet requires a start address with the low bit cleared. */
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000782 if (start % 2) {
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +0000783 msg_cerr("%s: start address not even! Please report a bug at "
784 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +0000785 if (spi_chip_write_1(flash, buf, start, start % 2))
786 return SPI_GENERIC_ERROR;
787 pos += start % 2;
788 /* Do not return an error for now. */
789 //return SPI_GENERIC_ERROR;
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +0000790 }
791 /* The data sheet requires total AAI write length to be even. */
792 if (len % 2) {
793 msg_cerr("%s: total write length not even! Please report a "
794 "bug at flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +0000795 /* Do not return an error for now. */
796 //return SPI_GENERIC_ERROR;
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +0000797 }
798
Nico Hubera1672f82017-10-14 18:00:20 +0200799 result = spi_write_cmd(flash, JEDEC_AAI_WORD_PROGRAM, false, start, buf + pos - start, 2, 10);
Nico Huber0ecbacb2017-10-14 16:50:43 +0200800 if (result)
Stefan Reinauer87ace662014-04-26 16:12:55 +0000801 goto bailout;
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +0000802
803 /* We already wrote 2 bytes in the multicommand step. */
804 pos += 2;
805
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +0000806 /* Are there at least two more bytes to write? */
807 while (pos < start + len - 1) {
Carl-Daniel Hailfingerccfe0ac2010-10-27 22:07:11 +0000808 cmd[1] = buf[pos++ - start];
809 cmd[2] = buf[pos++ - start];
Stefan Reinauer87ace662014-04-26 16:12:55 +0000810 result = spi_send_command(flash, JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE, 0, cmd, NULL);
811 if (result != 0) {
812 msg_cerr("%s failed during followup AAI command execution: %d\n", __func__, result);
813 goto bailout;
814 }
Nico Huber0ecbacb2017-10-14 16:50:43 +0200815 if (spi_poll_wip(flash, 10))
816 goto bailout;
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +0000817 }
818
Stefan Tauner59c4d792014-04-26 16:13:09 +0000819 /* Use WRDI to exit AAI mode. This needs to be done before issuing any other non-AAI command. */
820 result = spi_write_disable(flash);
821 if (result != 0) {
822 msg_cerr("%s failed to disable AAI mode.\n", __func__);
823 return SPI_GENERIC_ERROR;
824 }
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +0000825
826 /* Write remaining byte (if any). */
827 if (pos < start + len) {
Carl-Daniel Hailfingerccfe0ac2010-10-27 22:07:11 +0000828 if (spi_chip_write_1(flash, buf + pos - start, pos, pos % 2))
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +0000829 return SPI_GENERIC_ERROR;
830 pos += pos % 2;
831 }
832
Sean Nelson14ba6682010-02-26 05:48:29 +0000833 return 0;
Stefan Reinauer87ace662014-04-26 16:12:55 +0000834
835bailout:
Stefan Tauner59c4d792014-04-26 16:13:09 +0000836 result = spi_write_disable(flash);
837 if (result != 0)
838 msg_cerr("%s failed to disable AAI mode.\n", __func__);
Stefan Reinauer87ace662014-04-26 16:12:55 +0000839 return SPI_GENERIC_ERROR;
Sean Nelson14ba6682010-02-26 05:48:29 +0000840}
Nico Huber7e3c81a2017-10-14 18:56:50 +0200841
Nico Huberfe34d2a2017-11-10 21:10:20 +0100842static int spi_enter_exit_4ba(struct flashctx *const flash, const bool enter)
Nico Huber7e3c81a2017-10-14 18:56:50 +0200843{
Nico Huberfe34d2a2017-11-10 21:10:20 +0100844 const unsigned char cmd = enter ? JEDEC_ENTER_4_BYTE_ADDR_MODE : JEDEC_EXIT_4_BYTE_ADDR_MODE;
845 int ret = 1;
Nico Huber7e3c81a2017-10-14 18:56:50 +0200846
Nico Huberfe34d2a2017-11-10 21:10:20 +0100847 if (flash->chip->feature_bits & FEATURE_4BA_ENTER)
848 ret = spi_send_command(flash, sizeof(cmd), 0, &cmd, NULL);
849 else if (flash->chip->feature_bits & FEATURE_4BA_ENTER_WREN)
850 ret = spi_simple_write_cmd(flash, cmd, 0);
851
Nico Huber7e3c81a2017-10-14 18:56:50 +0200852 if (!ret)
Nico Huberfe34d2a2017-11-10 21:10:20 +0100853 flash->in_4ba_mode = enter;
Nico Huber7e3c81a2017-10-14 18:56:50 +0200854 return ret;
855}
856
Nico Huberfe34d2a2017-11-10 21:10:20 +0100857int spi_enter_4ba(struct flashctx *const flash)
Nico Huber7e3c81a2017-10-14 18:56:50 +0200858{
Nico Huberfe34d2a2017-11-10 21:10:20 +0100859 return spi_enter_exit_4ba(flash, true);
Nico Huber7e3c81a2017-10-14 18:56:50 +0200860}
861
Nico Huberfe34d2a2017-11-10 21:10:20 +0100862int spi_exit_4ba(struct flashctx *flash)
Nico Huber7e3c81a2017-10-14 18:56:50 +0200863{
Nico Huberfe34d2a2017-11-10 21:10:20 +0100864 return spi_enter_exit_4ba(flash, false);
Nico Huber7e3c81a2017-10-14 18:56:50 +0200865}