blob: 10c5a4a92f39e321280834c1e6001ebf246e7a86 [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.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/*
22 * Contains the common SPI chip driver functions
23 */
24
25#include <string.h>
26#include "flash.h"
27#include "flashchips.h"
28#include "chipdrivers.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000029#include "programmer.h"
Sean Nelson14ba6682010-02-26 05:48:29 +000030#include "spi.h"
31
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000032static int spi_rdid(struct flashctx *flash, unsigned char *readarr, int bytes)
Sean Nelson14ba6682010-02-26 05:48:29 +000033{
Mathias Krausea60faab2011-01-17 07:50:42 +000034 static const unsigned char cmd[JEDEC_RDID_OUTSIZE] = { JEDEC_RDID };
Sean Nelson14ba6682010-02-26 05:48:29 +000035 int ret;
36 int i;
37
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000038 ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr);
Sean Nelson14ba6682010-02-26 05:48:29 +000039 if (ret)
40 return ret;
Sean Nelsoned479d22010-03-24 23:14:32 +000041 msg_cspew("RDID returned");
Sean Nelson14ba6682010-02-26 05:48:29 +000042 for (i = 0; i < bytes; i++)
Sean Nelsoned479d22010-03-24 23:14:32 +000043 msg_cspew(" 0x%02x", readarr[i]);
44 msg_cspew(". ");
Sean Nelson14ba6682010-02-26 05:48:29 +000045 return 0;
46}
47
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000048static int spi_rems(struct flashctx *flash, unsigned char *readarr)
Sean Nelson14ba6682010-02-26 05:48:29 +000049{
50 unsigned char cmd[JEDEC_REMS_OUTSIZE] = { JEDEC_REMS, 0, 0, 0 };
51 uint32_t readaddr;
52 int ret;
53
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000054 ret = spi_send_command(flash, sizeof(cmd), JEDEC_REMS_INSIZE, cmd,
55 readarr);
Sean Nelson14ba6682010-02-26 05:48:29 +000056 if (ret == SPI_INVALID_ADDRESS) {
57 /* Find the lowest even address allowed for reads. */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000058 readaddr = (spi_get_valid_read_addr(flash) + 1) & ~1;
Sean Nelson14ba6682010-02-26 05:48:29 +000059 cmd[1] = (readaddr >> 16) & 0xff,
60 cmd[2] = (readaddr >> 8) & 0xff,
61 cmd[3] = (readaddr >> 0) & 0xff,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000062 ret = spi_send_command(flash, sizeof(cmd), JEDEC_REMS_INSIZE,
63 cmd, readarr);
Sean Nelson14ba6682010-02-26 05:48:29 +000064 }
65 if (ret)
66 return ret;
Cristian Măgherușan-Stanciu9932c7b2011-07-07 19:56:58 +000067 msg_cspew("REMS returned 0x%02x 0x%02x. ", readarr[0], readarr[1]);
Sean Nelson14ba6682010-02-26 05:48:29 +000068 return 0;
69}
70
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000071static int spi_res(struct flashctx *flash, unsigned char *readarr, int bytes)
Sean Nelson14ba6682010-02-26 05:48:29 +000072{
73 unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, 0, 0, 0 };
74 uint32_t readaddr;
75 int ret;
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +000076 int i;
Sean Nelson14ba6682010-02-26 05:48:29 +000077
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000078 ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr);
Sean Nelson14ba6682010-02-26 05:48:29 +000079 if (ret == SPI_INVALID_ADDRESS) {
80 /* Find the lowest even address allowed for reads. */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000081 readaddr = (spi_get_valid_read_addr(flash) + 1) & ~1;
Sean Nelson14ba6682010-02-26 05:48:29 +000082 cmd[1] = (readaddr >> 16) & 0xff,
83 cmd[2] = (readaddr >> 8) & 0xff,
84 cmd[3] = (readaddr >> 0) & 0xff,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000085 ret = spi_send_command(flash, sizeof(cmd), bytes, cmd, readarr);
Sean Nelson14ba6682010-02-26 05:48:29 +000086 }
87 if (ret)
88 return ret;
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +000089 msg_cspew("RES returned");
90 for (i = 0; i < bytes; i++)
91 msg_cspew(" 0x%02x", readarr[i]);
92 msg_cspew(". ");
Sean Nelson14ba6682010-02-26 05:48:29 +000093 return 0;
94}
95
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +000096int spi_write_enable(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +000097{
Mathias Krausea60faab2011-01-17 07:50:42 +000098 static const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN };
Sean Nelson14ba6682010-02-26 05:48:29 +000099 int result;
100
101 /* Send WREN (Write Enable) */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000102 result = spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
Sean Nelson14ba6682010-02-26 05:48:29 +0000103
104 if (result)
Sean Nelsoned479d22010-03-24 23:14:32 +0000105 msg_cerr("%s failed\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000106
107 return result;
108}
109
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000110int spi_write_disable(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000111{
Mathias Krausea60faab2011-01-17 07:50:42 +0000112 static const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI };
Sean Nelson14ba6682010-02-26 05:48:29 +0000113
114 /* Send WRDI (Write Disable) */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000115 return spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
Sean Nelson14ba6682010-02-26 05:48:29 +0000116}
117
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000118static int probe_spi_rdid_generic(struct flashctx *flash, int bytes)
Sean Nelson14ba6682010-02-26 05:48:29 +0000119{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000120 const struct flashchip *chip = flash->chip;
Sean Nelson14ba6682010-02-26 05:48:29 +0000121 unsigned char readarr[4];
122 uint32_t id1;
123 uint32_t id2;
124
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000125 if (spi_rdid(flash, readarr, bytes)) {
Sean Nelson14ba6682010-02-26 05:48:29 +0000126 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000127 }
Sean Nelson14ba6682010-02-26 05:48:29 +0000128
129 if (!oddparity(readarr[0]))
Sean Nelsoned479d22010-03-24 23:14:32 +0000130 msg_cdbg("RDID byte 0 parity violation. ");
Sean Nelson14ba6682010-02-26 05:48:29 +0000131
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000132 /* Check if this is a continuation vendor ID.
133 * FIXME: Handle continuation device IDs.
134 */
Sean Nelson14ba6682010-02-26 05:48:29 +0000135 if (readarr[0] == 0x7f) {
136 if (!oddparity(readarr[1]))
Sean Nelsoned479d22010-03-24 23:14:32 +0000137 msg_cdbg("RDID byte 1 parity violation. ");
Sean Nelson14ba6682010-02-26 05:48:29 +0000138 id1 = (readarr[0] << 8) | readarr[1];
139 id2 = readarr[2];
140 if (bytes > 3) {
141 id2 <<= 8;
142 id2 |= readarr[3];
143 }
144 } else {
145 id1 = readarr[0];
146 id2 = (readarr[1] << 8) | readarr[2];
147 }
148
Sean Nelsoned479d22010-03-24 23:14:32 +0000149 msg_cdbg("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
Sean Nelson14ba6682010-02-26 05:48:29 +0000150
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000151 if (id1 == chip->manufacture_id && id2 == chip->model_id) {
Sean Nelson14ba6682010-02-26 05:48:29 +0000152 /* Print the status register to tell the
153 * user about possible write protection.
154 */
155 spi_prettyprint_status_register(flash);
156
157 return 1;
158 }
159
160 /* Test if this is a pure vendor match. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000161 if (id1 == chip->manufacture_id && GENERIC_DEVICE_ID == chip->model_id)
Sean Nelson14ba6682010-02-26 05:48:29 +0000162 return 1;
163
164 /* Test if there is any vendor ID. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000165 if (GENERIC_MANUF_ID == chip->manufacture_id && id1 != 0xff)
Sean Nelson14ba6682010-02-26 05:48:29 +0000166 return 1;
167
168 return 0;
169}
170
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000171int probe_spi_rdid(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000172{
173 return probe_spi_rdid_generic(flash, 3);
174}
175
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000176int probe_spi_rdid4(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000177{
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000178 /* Some SPI controllers do not support commands with writecnt=1 and
179 * readcnt=4.
180 */
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000181 switch (flash->pgm->spi.type) {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000182#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000183#if defined(__i386__) || defined(__x86_64__)
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000184 case SPI_CONTROLLER_IT87XX:
Sean Nelson14ba6682010-02-26 05:48:29 +0000185 case SPI_CONTROLLER_WBSIO:
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000186 msg_cinfo("4 byte RDID not supported on this SPI controller\n");
187 return 0;
188 break;
Sean Nelson14ba6682010-02-26 05:48:29 +0000189#endif
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000190#endif
Sean Nelson14ba6682010-02-26 05:48:29 +0000191 default:
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000192 return probe_spi_rdid_generic(flash, 4);
Sean Nelson14ba6682010-02-26 05:48:29 +0000193 }
194
195 return 0;
196}
197
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000198int probe_spi_rems(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000199{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000200 const struct flashchip *chip = flash->chip;
Sean Nelson14ba6682010-02-26 05:48:29 +0000201 unsigned char readarr[JEDEC_REMS_INSIZE];
202 uint32_t id1, id2;
203
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000204 if (spi_rems(flash, readarr)) {
Sean Nelson14ba6682010-02-26 05:48:29 +0000205 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000206 }
Sean Nelson14ba6682010-02-26 05:48:29 +0000207
208 id1 = readarr[0];
209 id2 = readarr[1];
210
Sean Nelsoned479d22010-03-24 23:14:32 +0000211 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
Sean Nelson14ba6682010-02-26 05:48:29 +0000212
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000213 if (id1 == chip->manufacture_id && id2 == chip->model_id) {
Sean Nelson14ba6682010-02-26 05:48:29 +0000214 /* Print the status register to tell the
215 * user about possible write protection.
216 */
217 spi_prettyprint_status_register(flash);
218
219 return 1;
220 }
221
222 /* Test if this is a pure vendor match. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000223 if (id1 == chip->manufacture_id && GENERIC_DEVICE_ID == chip->model_id)
Sean Nelson14ba6682010-02-26 05:48:29 +0000224 return 1;
225
226 /* Test if there is any vendor ID. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000227 if (GENERIC_MANUF_ID == chip->manufacture_id && id1 != 0xff)
Sean Nelson14ba6682010-02-26 05:48:29 +0000228 return 1;
229
230 return 0;
231}
232
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000233int probe_spi_res1(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000234{
Mathias Krausea60faab2011-01-17 07:50:42 +0000235 static const unsigned char allff[] = {0xff, 0xff, 0xff};
236 static const unsigned char all00[] = {0x00, 0x00, 0x00};
Sean Nelson14ba6682010-02-26 05:48:29 +0000237 unsigned char readarr[3];
238 uint32_t id2;
Sean Nelson14ba6682010-02-26 05:48:29 +0000239
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000240 /* We only want one-byte RES if RDID and REMS are unusable. */
241
Sean Nelson14ba6682010-02-26 05:48:29 +0000242 /* Check if RDID is usable and does not return 0xff 0xff 0xff or
243 * 0x00 0x00 0x00. In that case, RES is pointless.
244 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000245 if (!spi_rdid(flash, readarr, 3) && memcmp(readarr, allff, 3) &&
Sean Nelson14ba6682010-02-26 05:48:29 +0000246 memcmp(readarr, all00, 3)) {
247 msg_cdbg("Ignoring RES in favour of RDID.\n");
248 return 0;
249 }
250 /* Check if REMS is usable and does not return 0xff 0xff or
251 * 0x00 0x00. In that case, RES is pointless.
252 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000253 if (!spi_rems(flash, readarr) &&
254 memcmp(readarr, allff, JEDEC_REMS_INSIZE) &&
Sean Nelson14ba6682010-02-26 05:48:29 +0000255 memcmp(readarr, all00, JEDEC_REMS_INSIZE)) {
256 msg_cdbg("Ignoring RES in favour of REMS.\n");
257 return 0;
258 }
259
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000260 if (spi_res(flash, readarr, 1)) {
Sean Nelson14ba6682010-02-26 05:48:29 +0000261 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000262 }
Sean Nelson14ba6682010-02-26 05:48:29 +0000263
Sean Nelson14ba6682010-02-26 05:48:29 +0000264 id2 = readarr[0];
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000265
Sean Nelsoned479d22010-03-24 23:14:32 +0000266 msg_cdbg("%s: id 0x%x\n", __func__, id2);
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000267
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000268 if (id2 != flash->chip->model_id)
Sean Nelson14ba6682010-02-26 05:48:29 +0000269 return 0;
270
271 /* Print the status register to tell the
272 * user about possible write protection.
273 */
274 spi_prettyprint_status_register(flash);
275 return 1;
276}
277
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000278int probe_spi_res2(struct flashctx *flash)
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000279{
280 unsigned char readarr[2];
281 uint32_t id1, id2;
282
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000283 if (spi_res(flash, readarr, 2)) {
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000284 return 0;
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000285 }
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000286
287 id1 = readarr[0];
288 id2 = readarr[1];
289
290 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
291
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000292 if (id1 != flash->chip->manufacture_id || id2 != flash->chip->model_id)
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000293 return 0;
294
295 /* Print the status register to tell the
296 * user about possible write protection.
297 */
298 spi_prettyprint_status_register(flash);
299 return 1;
300}
301
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000302uint8_t spi_read_status_register(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000303{
Mathias Krausea60faab2011-01-17 07:50:42 +0000304 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR };
Sean Nelson14ba6682010-02-26 05:48:29 +0000305 /* FIXME: No workarounds for driver/hardware bugs in generic code. */
306 unsigned char readarr[2]; /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */
307 int ret;
308
309 /* Read Status Register */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000310 ret = spi_send_command(flash, sizeof(cmd), sizeof(readarr), cmd,
311 readarr);
Sean Nelson14ba6682010-02-26 05:48:29 +0000312 if (ret)
Sean Nelsoned479d22010-03-24 23:14:32 +0000313 msg_cerr("RDSR failed!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000314
315 return readarr[0];
316}
317
Stefan Tauner5609f9d2012-09-22 01:38:06 +0000318/* Common highest bit: Status Register Write Disable (SRWD). */
319void spi_prettyprint_status_register_srwd(uint8_t status)
320{
321 msg_cdbg("Chip status register: Status Register Write Disable (SRWD) is %sset\n",
322 (status & (1 << 7)) ? "" : "not ");
323}
324
Carl-Daniel Hailfinger7a3bd8f2011-05-19 00:06:06 +0000325void spi_prettyprint_status_register_welwip(uint8_t status)
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000326{
327 msg_cdbg("Chip status register: Write Enable Latch (WEL) is "
328 "%sset\n", (status & (1 << 1)) ? "" : "not ");
329 msg_cdbg("Chip status register: Write In Progress (WIP/BUSY) is "
330 "%sset\n", (status & (1 << 0)) ? "" : "not ");
331}
332
333/* Prettyprint the status register. Common definitions. */
Stefan Tauner1ba08f62012-08-02 23:51:28 +0000334void spi_prettyprint_status_register_bp(uint8_t status, int bp)
Carl-Daniel Hailfinger7a3bd8f2011-05-19 00:06:06 +0000335{
336 switch (bp) {
337 /* Fall through. */
Stefan Tauner1ba08f62012-08-02 23:51:28 +0000338 case 4:
339 msg_cdbg("Chip status register: Block Protect 4 (BP4) "
340 "is %sset\n", (status & (1 << 5)) ? "" : "not ");
Carl-Daniel Hailfinger7a3bd8f2011-05-19 00:06:06 +0000341 case 3:
Stefan Tauner1ba08f62012-08-02 23:51:28 +0000342 msg_cdbg("Chip status register: Block Protect 3 (BP3) "
Carl-Daniel Hailfinger7a3bd8f2011-05-19 00:06:06 +0000343 "is %sset\n", (status & (1 << 5)) ? "" : "not ");
344 case 2:
Stefan Tauner1ba08f62012-08-02 23:51:28 +0000345 msg_cdbg("Chip status register: Block Protect 2 (BP2) "
Carl-Daniel Hailfinger7a3bd8f2011-05-19 00:06:06 +0000346 "is %sset\n", (status & (1 << 4)) ? "" : "not ");
347 case 1:
Stefan Tauner1ba08f62012-08-02 23:51:28 +0000348 msg_cdbg("Chip status register: Block Protect 1 (BP1) "
Carl-Daniel Hailfinger7a3bd8f2011-05-19 00:06:06 +0000349 "is %sset\n", (status & (1 << 3)) ? "" : "not ");
350 case 0:
Stefan Tauner1ba08f62012-08-02 23:51:28 +0000351 msg_cdbg("Chip status register: Block Protect 0 (BP0) "
Carl-Daniel Hailfinger7a3bd8f2011-05-19 00:06:06 +0000352 "is %sset\n", (status & (1 << 2)) ? "" : "not ");
353 }
354}
355
356/* Prettyprint the status register. Unnamed bits. */
357void spi_prettyprint_status_register_bit(uint8_t status, int bit)
358{
359 msg_cdbg("Chip status register: Bit %i "
360 "is %sset\n", bit, (status & (1 << bit)) ? "" : "not ");
361}
362
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000363static void spi_prettyprint_status_register_common(uint8_t status)
Sean Nelson14ba6682010-02-26 05:48:29 +0000364{
Stefan Tauner1ba08f62012-08-02 23:51:28 +0000365 spi_prettyprint_status_register_bp(status, 3);
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000366 spi_prettyprint_status_register_welwip(status);
Sean Nelson14ba6682010-02-26 05:48:29 +0000367}
368
369/* Prettyprint the status register. Works for
370 * ST M25P series
371 * MX MX25L series
372 */
373void spi_prettyprint_status_register_st_m25p(uint8_t status)
374{
Stefan Tauner5609f9d2012-09-22 01:38:06 +0000375 spi_prettyprint_status_register_srwd(status);
376 spi_prettyprint_status_register_bit(status, 6);
Sean Nelson14ba6682010-02-26 05:48:29 +0000377 spi_prettyprint_status_register_common(status);
378}
379
380void spi_prettyprint_status_register_sst25(uint8_t status)
381{
Sean Nelsoned479d22010-03-24 23:14:32 +0000382 msg_cdbg("Chip status register: Block Protect Write Disable "
Sean Nelson14ba6682010-02-26 05:48:29 +0000383 "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not ");
Sean Nelsoned479d22010-03-24 23:14:32 +0000384 msg_cdbg("Chip status register: Auto Address Increment Programming "
Sean Nelson14ba6682010-02-26 05:48:29 +0000385 "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not ");
386 spi_prettyprint_status_register_common(status);
387}
388
389/* Prettyprint the status register. Works for
390 * SST 25VF016
391 */
392void spi_prettyprint_status_register_sst25vf016(uint8_t status)
393{
Mathias Krausea60faab2011-01-17 07:50:42 +0000394 static const char *const bpt[] = {
Sean Nelson14ba6682010-02-26 05:48:29 +0000395 "none",
396 "1F0000H-1FFFFFH",
397 "1E0000H-1FFFFFH",
398 "1C0000H-1FFFFFH",
399 "180000H-1FFFFFH",
400 "100000H-1FFFFFH",
401 "all", "all"
402 };
403 spi_prettyprint_status_register_sst25(status);
Sean Nelsoned479d22010-03-24 23:14:32 +0000404 msg_cdbg("Resulting block protection : %s\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000405 bpt[(status & 0x1c) >> 2]);
406}
407
408void spi_prettyprint_status_register_sst25vf040b(uint8_t status)
409{
Mathias Krausea60faab2011-01-17 07:50:42 +0000410 static const char *const bpt[] = {
Sean Nelson14ba6682010-02-26 05:48:29 +0000411 "none",
412 "0x70000-0x7ffff",
413 "0x60000-0x7ffff",
414 "0x40000-0x7ffff",
415 "all blocks", "all blocks", "all blocks", "all blocks"
416 };
417 spi_prettyprint_status_register_sst25(status);
Sean Nelsoned479d22010-03-24 23:14:32 +0000418 msg_cdbg("Resulting block protection : %s\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000419 bpt[(status & 0x1c) >> 2]);
420}
421
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000422int spi_prettyprint_status_register(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000423{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000424 const struct flashchip *chip = flash->chip;
Sean Nelson14ba6682010-02-26 05:48:29 +0000425 uint8_t status;
426
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000427 status = spi_read_status_register(flash);
Sean Nelsoned479d22010-03-24 23:14:32 +0000428 msg_cdbg("Chip status register is %02x\n", status);
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000429 switch (chip->manufacture_id) {
Sean Nelson14ba6682010-02-26 05:48:29 +0000430 case ST_ID:
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000431 if (((chip->model_id & 0xff00) == 0x2000) ||
432 ((chip->model_id & 0xff00) == 0x2500))
Sean Nelson14ba6682010-02-26 05:48:29 +0000433 spi_prettyprint_status_register_st_m25p(status);
434 break;
Mattias Mattsson6eabe282010-09-15 23:31:03 +0000435 case MACRONIX_ID:
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000436 if ((chip->model_id & 0xff00) == 0x2000)
Sean Nelson14ba6682010-02-26 05:48:29 +0000437 spi_prettyprint_status_register_st_m25p(status);
438 break;
439 case SST_ID:
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000440 switch (chip->model_id) {
Sean Nelson14ba6682010-02-26 05:48:29 +0000441 case 0x2541:
442 spi_prettyprint_status_register_sst25vf016(status);
443 break;
444 case 0x8d:
445 case 0x258d:
446 spi_prettyprint_status_register_sst25vf040b(status);
447 break;
448 default:
449 spi_prettyprint_status_register_sst25(status);
450 break;
451 }
452 break;
453 }
Carl-Daniel Hailfinger7a3bd8f2011-05-19 00:06:06 +0000454 return 0;
Sean Nelson14ba6682010-02-26 05:48:29 +0000455}
456
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000457int spi_chip_erase_60(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000458{
459 int result;
460 struct spi_command cmds[] = {
461 {
462 .writecnt = JEDEC_WREN_OUTSIZE,
463 .writearr = (const unsigned char[]){ JEDEC_WREN },
464 .readcnt = 0,
465 .readarr = NULL,
466 }, {
467 .writecnt = JEDEC_CE_60_OUTSIZE,
468 .writearr = (const unsigned char[]){ JEDEC_CE_60 },
469 .readcnt = 0,
470 .readarr = NULL,
471 }, {
472 .writecnt = 0,
473 .writearr = NULL,
474 .readcnt = 0,
475 .readarr = NULL,
476 }};
477
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000478 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000479 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000480 msg_cerr("%s failed during command execution\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000481 __func__);
482 return result;
483 }
484 /* Wait until the Write-In-Progress bit is cleared.
485 * This usually takes 1-85 s, so wait in 1 s steps.
486 */
487 /* FIXME: We assume spi_read_status_register will never fail. */
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000488 while (spi_read_status_register(flash) & SPI_SR_WIP)
Sean Nelson14ba6682010-02-26 05:48:29 +0000489 programmer_delay(1000 * 1000);
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000490 /* FIXME: Check the status register for errors. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000491 return 0;
492}
493
Stefan Tauner3c0fcd02012-09-21 12:46:56 +0000494int spi_chip_erase_62(struct flashctx *flash)
495{
496 int result;
497 struct spi_command cmds[] = {
498 {
499 .writecnt = JEDEC_WREN_OUTSIZE,
500 .writearr = (const unsigned char[]){ JEDEC_WREN },
501 .readcnt = 0,
502 .readarr = NULL,
503 }, {
504 .writecnt = JEDEC_CE_62_OUTSIZE,
505 .writearr = (const unsigned char[]){ JEDEC_CE_62 },
506 .readcnt = 0,
507 .readarr = NULL,
508 }, {
509 .writecnt = 0,
510 .writearr = NULL,
511 .readcnt = 0,
512 .readarr = NULL,
513 }};
514
515 result = spi_send_multicommand(flash, cmds);
516 if (result) {
517 msg_cerr("%s failed during command execution\n",
518 __func__);
519 return result;
520 }
521 /* Wait until the Write-In-Progress bit is cleared.
522 * This usually takes 2-5 s, so wait in 100 ms steps.
523 */
524 /* FIXME: We assume spi_read_status_register will never fail. */
525 while (spi_read_status_register(flash) & SPI_SR_WIP)
526 programmer_delay(100 * 1000);
527 /* FIXME: Check the status register for errors. */
528 return 0;
529}
530
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000531int spi_chip_erase_c7(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000532{
533 int result;
534 struct spi_command cmds[] = {
535 {
536 .writecnt = JEDEC_WREN_OUTSIZE,
537 .writearr = (const unsigned char[]){ JEDEC_WREN },
538 .readcnt = 0,
539 .readarr = NULL,
540 }, {
541 .writecnt = JEDEC_CE_C7_OUTSIZE,
542 .writearr = (const unsigned char[]){ JEDEC_CE_C7 },
543 .readcnt = 0,
544 .readarr = NULL,
545 }, {
546 .writecnt = 0,
547 .writearr = NULL,
548 .readcnt = 0,
549 .readarr = NULL,
550 }};
551
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000552 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000553 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000554 msg_cerr("%s failed during command execution\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000555 return result;
556 }
557 /* Wait until the Write-In-Progress bit is cleared.
558 * This usually takes 1-85 s, so wait in 1 s steps.
559 */
560 /* FIXME: We assume spi_read_status_register will never fail. */
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000561 while (spi_read_status_register(flash) & SPI_SR_WIP)
Sean Nelson14ba6682010-02-26 05:48:29 +0000562 programmer_delay(1000 * 1000);
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000563 /* FIXME: Check the status register for errors. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000564 return 0;
565}
566
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000567int spi_block_erase_52(struct flashctx *flash, unsigned int addr,
568 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000569{
570 int result;
571 struct spi_command cmds[] = {
572 {
573 .writecnt = JEDEC_WREN_OUTSIZE,
574 .writearr = (const unsigned char[]){ JEDEC_WREN },
575 .readcnt = 0,
576 .readarr = NULL,
577 }, {
578 .writecnt = JEDEC_BE_52_OUTSIZE,
579 .writearr = (const unsigned char[]){
580 JEDEC_BE_52,
581 (addr >> 16) & 0xff,
582 (addr >> 8) & 0xff,
583 (addr & 0xff)
584 },
585 .readcnt = 0,
586 .readarr = NULL,
587 }, {
588 .writecnt = 0,
589 .writearr = NULL,
590 .readcnt = 0,
591 .readarr = NULL,
592 }};
593
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000594 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000595 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000596 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000597 __func__, addr);
598 return result;
599 }
600 /* Wait until the Write-In-Progress bit is cleared.
601 * This usually takes 100-4000 ms, so wait in 100 ms steps.
602 */
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000603 while (spi_read_status_register(flash) & SPI_SR_WIP)
Sean Nelson14ba6682010-02-26 05:48:29 +0000604 programmer_delay(100 * 1000);
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000605 /* FIXME: Check the status register for errors. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000606 return 0;
607}
608
609/* Block size is usually
610 * 64k for Macronix
611 * 32k for SST
612 * 4-32k non-uniform for EON
613 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000614int spi_block_erase_d8(struct flashctx *flash, unsigned int addr,
615 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000616{
617 int result;
618 struct spi_command cmds[] = {
619 {
620 .writecnt = JEDEC_WREN_OUTSIZE,
621 .writearr = (const unsigned char[]){ JEDEC_WREN },
622 .readcnt = 0,
623 .readarr = NULL,
624 }, {
625 .writecnt = JEDEC_BE_D8_OUTSIZE,
626 .writearr = (const unsigned char[]){
627 JEDEC_BE_D8,
628 (addr >> 16) & 0xff,
629 (addr >> 8) & 0xff,
630 (addr & 0xff)
631 },
632 .readcnt = 0,
633 .readarr = NULL,
634 }, {
635 .writecnt = 0,
636 .writearr = NULL,
637 .readcnt = 0,
638 .readarr = NULL,
639 }};
640
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000641 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000642 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000643 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000644 __func__, addr);
645 return result;
646 }
647 /* Wait until the Write-In-Progress bit is cleared.
648 * This usually takes 100-4000 ms, so wait in 100 ms steps.
649 */
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000650 while (spi_read_status_register(flash) & SPI_SR_WIP)
Sean Nelson14ba6682010-02-26 05:48:29 +0000651 programmer_delay(100 * 1000);
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000652 /* FIXME: Check the status register for errors. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000653 return 0;
654}
655
656/* Block size is usually
657 * 4k for PMC
658 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000659int spi_block_erase_d7(struct flashctx *flash, unsigned int addr,
660 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000661{
662 int result;
663 struct spi_command cmds[] = {
664 {
665 .writecnt = JEDEC_WREN_OUTSIZE,
666 .writearr = (const unsigned char[]){ JEDEC_WREN },
667 .readcnt = 0,
668 .readarr = NULL,
669 }, {
670 .writecnt = JEDEC_BE_D7_OUTSIZE,
671 .writearr = (const unsigned char[]){
672 JEDEC_BE_D7,
673 (addr >> 16) & 0xff,
674 (addr >> 8) & 0xff,
675 (addr & 0xff)
676 },
677 .readcnt = 0,
678 .readarr = NULL,
679 }, {
680 .writecnt = 0,
681 .writearr = NULL,
682 .readcnt = 0,
683 .readarr = NULL,
684 }};
685
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000686 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000687 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000688 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000689 __func__, addr);
690 return result;
691 }
692 /* Wait until the Write-In-Progress bit is cleared.
693 * This usually takes 100-4000 ms, so wait in 100 ms steps.
694 */
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000695 while (spi_read_status_register(flash) & SPI_SR_WIP)
Sean Nelson14ba6682010-02-26 05:48:29 +0000696 programmer_delay(100 * 1000);
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000697 /* FIXME: Check the status register for errors. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000698 return 0;
699}
700
Sean Nelson14ba6682010-02-26 05:48:29 +0000701/* Sector size is usually 4k, though Macronix eliteflash has 64k */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000702int spi_block_erase_20(struct flashctx *flash, unsigned int addr,
703 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000704{
705 int result;
706 struct spi_command cmds[] = {
707 {
708 .writecnt = JEDEC_WREN_OUTSIZE,
709 .writearr = (const unsigned char[]){ JEDEC_WREN },
710 .readcnt = 0,
711 .readarr = NULL,
712 }, {
713 .writecnt = JEDEC_SE_OUTSIZE,
714 .writearr = (const unsigned char[]){
715 JEDEC_SE,
716 (addr >> 16) & 0xff,
717 (addr >> 8) & 0xff,
718 (addr & 0xff)
719 },
720 .readcnt = 0,
721 .readarr = NULL,
722 }, {
723 .writecnt = 0,
724 .writearr = NULL,
725 .readcnt = 0,
726 .readarr = NULL,
727 }};
728
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000729 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000730 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000731 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000732 __func__, addr);
733 return result;
734 }
735 /* Wait until the Write-In-Progress bit is cleared.
736 * This usually takes 15-800 ms, so wait in 10 ms steps.
737 */
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000738 while (spi_read_status_register(flash) & SPI_SR_WIP)
Sean Nelson14ba6682010-02-26 05:48:29 +0000739 programmer_delay(10 * 1000);
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +0000740 /* FIXME: Check the status register for errors. */
Sean Nelson14ba6682010-02-26 05:48:29 +0000741 return 0;
742}
743
Stefan Tauner94b39b42012-10-27 00:06:02 +0000744int spi_block_erase_50(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
745{
746 int result;
747 struct spi_command cmds[] = {
748 {
749/* .writecnt = JEDEC_WREN_OUTSIZE,
750 .writearr = (const unsigned char[]){ JEDEC_WREN },
751 .readcnt = 0,
752 .readarr = NULL,
753 }, { */
754 .writecnt = JEDEC_BE_50_OUTSIZE,
755 .writearr = (const unsigned char[]){
756 JEDEC_BE_50,
757 (addr >> 16) & 0xff,
758 (addr >> 8) & 0xff,
759 (addr & 0xff)
760 },
761 .readcnt = 0,
762 .readarr = NULL,
763 }, {
764 .writecnt = 0,
765 .writearr = NULL,
766 .readcnt = 0,
767 .readarr = NULL,
768 }};
769
770 result = spi_send_multicommand(flash, cmds);
771 if (result) {
772 msg_cerr("%s failed during command execution at address 0x%x\n", __func__, addr);
773 return result;
774 }
775 /* Wait until the Write-In-Progress bit is cleared.
776 * This usually takes 10 ms, so wait in 1 ms steps.
777 */
778 while (spi_read_status_register(flash) & SPI_SR_WIP)
779 programmer_delay(1 * 1000);
780 /* FIXME: Check the status register for errors. */
781 return 0;
782}
783
784int spi_block_erase_81(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
785{
786 int result;
787 struct spi_command cmds[] = {
788 {
789/* .writecnt = JEDEC_WREN_OUTSIZE,
790 .writearr = (const unsigned char[]){ JEDEC_WREN },
791 .readcnt = 0,
792 .readarr = NULL,
793 }, { */
794 .writecnt = JEDEC_BE_81_OUTSIZE,
795 .writearr = (const unsigned char[]){
796 JEDEC_BE_81,
797 (addr >> 16) & 0xff,
798 (addr >> 8) & 0xff,
799 (addr & 0xff)
800 },
801 .readcnt = 0,
802 .readarr = NULL,
803 }, {
804 .writecnt = 0,
805 .writearr = NULL,
806 .readcnt = 0,
807 .readarr = NULL,
808 }};
809
810 result = spi_send_multicommand(flash, cmds);
811 if (result) {
812 msg_cerr("%s failed during command execution at address 0x%x\n", __func__, addr);
813 return result;
814 }
815 /* Wait until the Write-In-Progress bit is cleared.
816 * This usually takes 8 ms, so wait in 1 ms steps.
817 */
818 while (spi_read_status_register(flash) & SPI_SR_WIP)
819 programmer_delay(1 * 1000);
820 /* FIXME: Check the status register for errors. */
821 return 0;
822}
823
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000824int spi_block_erase_60(struct flashctx *flash, unsigned int addr,
825 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000826{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000827 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000828 msg_cerr("%s called with incorrect arguments\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000829 __func__);
830 return -1;
831 }
832 return spi_chip_erase_60(flash);
833}
834
Stefan Tauner3c0fcd02012-09-21 12:46:56 +0000835int spi_block_erase_62(struct flashctx *flash, unsigned int addr, unsigned int blocklen)
836{
837 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
838 msg_cerr("%s called with incorrect arguments\n",
839 __func__);
840 return -1;
841 }
842 return spi_chip_erase_62(flash);
843}
844
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000845int spi_block_erase_c7(struct flashctx *flash, unsigned int addr,
846 unsigned int blocklen)
Sean Nelson14ba6682010-02-26 05:48:29 +0000847{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000848 if ((addr != 0) || (blocklen != flash->chip->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000849 msg_cerr("%s called with incorrect arguments\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000850 __func__);
851 return -1;
852 }
853 return spi_chip_erase_c7(flash);
854}
855
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000856erasefunc_t *spi_get_erasefn_from_opcode(uint8_t opcode)
857{
858 switch(opcode){
859 case 0xff:
860 case 0x00:
861 /* Not specified, assuming "not supported". */
862 return NULL;
863 case 0x20:
864 return &spi_block_erase_20;
865 case 0x52:
866 return &spi_block_erase_52;
867 case 0x60:
868 return &spi_block_erase_60;
869 case 0xc7:
870 return &spi_block_erase_c7;
871 case 0xd7:
872 return &spi_block_erase_d7;
873 case 0xd8:
874 return &spi_block_erase_d8;
875 default:
876 msg_cinfo("%s: unknown erase opcode (0x%02x). Please report "
877 "this at flashrom@flashrom.org\n", __func__, opcode);
878 return NULL;
879 }
880}
881
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000882int spi_write_status_enable(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000883{
Mathias Krausea60faab2011-01-17 07:50:42 +0000884 static const unsigned char cmd[JEDEC_EWSR_OUTSIZE] = { JEDEC_EWSR };
Sean Nelson14ba6682010-02-26 05:48:29 +0000885 int result;
886
887 /* Send EWSR (Enable Write Status Register). */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000888 result = spi_send_command(flash, sizeof(cmd), JEDEC_EWSR_INSIZE, cmd, NULL);
Sean Nelson14ba6682010-02-26 05:48:29 +0000889
890 if (result)
Sean Nelsoned479d22010-03-24 23:14:32 +0000891 msg_cerr("%s failed\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000892
893 return result;
894}
895
896/*
897 * This is according the SST25VF016 datasheet, who knows it is more
898 * generic that this...
899 */
Stefan Tauner96c2dfc2012-05-02 20:08:01 +0000900static int spi_write_status_register_flag(struct flashctx *flash, int status, const unsigned char enable_opcode)
Sean Nelson14ba6682010-02-26 05:48:29 +0000901{
902 int result;
Carl-Daniel Hailfinger174f55b2010-10-08 00:37:55 +0000903 int i = 0;
Stefan Tauner96c2dfc2012-05-02 20:08:01 +0000904 /*
905 * WRSR requires either EWSR or WREN depending on chip type.
906 * The code below relies on the fact hat EWSR and WREN have the same
907 * INSIZE and OUTSIZE.
Carl-Daniel Hailfinger174f55b2010-10-08 00:37:55 +0000908 */
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000909 struct spi_command cmds[] = {
910 {
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000911 .writecnt = JEDEC_WREN_OUTSIZE,
Stefan Tauner96c2dfc2012-05-02 20:08:01 +0000912 .writearr = (const unsigned char[]){ enable_opcode },
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000913 .readcnt = 0,
914 .readarr = NULL,
915 }, {
916 .writecnt = JEDEC_WRSR_OUTSIZE,
917 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
918 .readcnt = 0,
919 .readarr = NULL,
920 }, {
921 .writecnt = 0,
922 .writearr = NULL,
923 .readcnt = 0,
924 .readarr = NULL,
925 }};
926
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000927 result = spi_send_multicommand(flash, cmds);
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000928 if (result) {
Stefan Tauner96c2dfc2012-05-02 20:08:01 +0000929 msg_cerr("%s failed during command execution\n", __func__);
Carl-Daniel Hailfinger174f55b2010-10-08 00:37:55 +0000930 /* No point in waiting for the command to complete if execution
931 * failed.
932 */
933 return result;
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000934 }
Carl-Daniel Hailfinger174f55b2010-10-08 00:37:55 +0000935 /* WRSR performs a self-timed erase before the changes take effect.
936 * This may take 50-85 ms in most cases, and some chips apparently
937 * allow running RDSR only once. Therefore pick an initial delay of
938 * 100 ms, then wait in 10 ms steps until a total of 5 s have elapsed.
939 */
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000940 programmer_delay(100 * 1000);
Stefan Tauner5e695ab2012-05-06 17:03:40 +0000941 while (spi_read_status_register(flash) & SPI_SR_WIP) {
Carl-Daniel Hailfinger174f55b2010-10-08 00:37:55 +0000942 if (++i > 490) {
943 msg_cerr("Error: WIP bit after WRSR never cleared\n");
944 return TIMEOUT_ERROR;
945 }
946 programmer_delay(10 * 1000);
947 }
948 return 0;
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000949}
950
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000951int spi_write_status_register(struct flashctx *flash, int status)
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000952{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000953 int feature_bits = flash->chip->feature_bits;
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000954 int ret = 1;
955
Stefan Tauner96c2dfc2012-05-02 20:08:01 +0000956 if (!(feature_bits & (FEATURE_WRSR_WREN | FEATURE_WRSR_EWSR))) {
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000957 msg_cdbg("Missing status register write definition, assuming "
958 "EWSR is needed\n");
Stefan Tauner96c2dfc2012-05-02 20:08:01 +0000959 feature_bits |= FEATURE_WRSR_EWSR;
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000960 }
Stefan Tauner96c2dfc2012-05-02 20:08:01 +0000961 if (feature_bits & FEATURE_WRSR_WREN)
962 ret = spi_write_status_register_flag(flash, status, JEDEC_WREN);
963 if (ret && (feature_bits & FEATURE_WRSR_EWSR))
964 ret = spi_write_status_register_flag(flash, status, JEDEC_EWSR);
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +0000965 return ret;
966}
967
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000968int spi_byte_program(struct flashctx *flash, unsigned int addr,
969 uint8_t databyte)
Sean Nelson14ba6682010-02-26 05:48:29 +0000970{
971 int result;
972 struct spi_command cmds[] = {
973 {
974 .writecnt = JEDEC_WREN_OUTSIZE,
975 .writearr = (const unsigned char[]){ JEDEC_WREN },
976 .readcnt = 0,
977 .readarr = NULL,
978 }, {
979 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE,
980 .writearr = (const unsigned char[]){
981 JEDEC_BYTE_PROGRAM,
982 (addr >> 16) & 0xff,
983 (addr >> 8) & 0xff,
984 (addr & 0xff),
985 databyte
986 },
987 .readcnt = 0,
988 .readarr = NULL,
989 }, {
990 .writecnt = 0,
991 .writearr = NULL,
992 .readcnt = 0,
993 .readarr = NULL,
994 }};
995
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000996 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +0000997 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000998 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000999 __func__, addr);
1000 }
1001 return result;
1002}
1003
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001004int spi_nbyte_program(struct flashctx *flash, unsigned int addr, uint8_t *bytes,
1005 unsigned int len)
Sean Nelson14ba6682010-02-26 05:48:29 +00001006{
1007 int result;
1008 /* FIXME: Switch to malloc based on len unless that kills speed. */
1009 unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + 256] = {
1010 JEDEC_BYTE_PROGRAM,
1011 (addr >> 16) & 0xff,
1012 (addr >> 8) & 0xff,
1013 (addr >> 0) & 0xff,
1014 };
1015 struct spi_command cmds[] = {
1016 {
1017 .writecnt = JEDEC_WREN_OUTSIZE,
1018 .writearr = (const unsigned char[]){ JEDEC_WREN },
1019 .readcnt = 0,
1020 .readarr = NULL,
1021 }, {
1022 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + len,
1023 .writearr = cmd,
1024 .readcnt = 0,
1025 .readarr = NULL,
1026 }, {
1027 .writecnt = 0,
1028 .writearr = NULL,
1029 .readcnt = 0,
1030 .readarr = NULL,
1031 }};
1032
1033 if (!len) {
Sean Nelsoned479d22010-03-24 23:14:32 +00001034 msg_cerr("%s called for zero-length write\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +00001035 return 1;
1036 }
1037 if (len > 256) {
Sean Nelsoned479d22010-03-24 23:14:32 +00001038 msg_cerr("%s called for too long a write\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +00001039 return 1;
1040 }
1041
1042 memcpy(&cmd[4], bytes, len);
1043
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001044 result = spi_send_multicommand(flash, cmds);
Sean Nelson14ba6682010-02-26 05:48:29 +00001045 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +00001046 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +00001047 __func__, addr);
1048 }
1049 return result;
1050}
1051
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +00001052/* A generic brute-force block protection disable works like this:
1053 * Write 0x00 to the status register. Check if any locks are still set (that
1054 * part is chip specific). Repeat once.
1055 */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +00001056int spi_disable_blockprotect(struct flashctx *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +00001057{
1058 uint8_t status;
1059 int result;
1060
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001061 status = spi_read_status_register(flash);
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +00001062 /* If block protection is disabled, stop here. */
1063 if ((status & 0x3c) == 0)
1064 return 0;
1065
Stefan Tauner87fbb772012-08-02 23:56:49 +00001066 msg_cdbg("Some block protection in effect, disabling... ");
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +00001067 result = spi_write_status_register(flash, status & ~0x3c);
1068 if (result) {
Stefan Tauner87fbb772012-08-02 23:56:49 +00001069 msg_cerr("spi_write_status_register failed.\n");
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +00001070 return result;
1071 }
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001072 status = spi_read_status_register(flash);
Sean Nelson14ba6682010-02-26 05:48:29 +00001073 if ((status & 0x3c) != 0) {
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +00001074 msg_cerr("Block protection could not be disabled!\n");
1075 return 1;
1076 }
Stefan Tauner87fbb772012-08-02 23:56:49 +00001077 msg_cdbg("done.\n");
Carl-Daniel Hailfingerfd7075a2010-07-29 13:09:18 +00001078 return 0;
1079}
1080
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001081int spi_nbyte_read(struct flashctx *flash, unsigned int address, uint8_t *bytes,
1082 unsigned int len)
Sean Nelson14ba6682010-02-26 05:48:29 +00001083{
1084 const unsigned char cmd[JEDEC_READ_OUTSIZE] = {
1085 JEDEC_READ,
1086 (address >> 16) & 0xff,
1087 (address >> 8) & 0xff,
1088 (address >> 0) & 0xff,
1089 };
1090
1091 /* Send Read */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001092 return spi_send_command(flash, sizeof(cmd), len, cmd, bytes);
Sean Nelson14ba6682010-02-26 05:48:29 +00001093}
1094
1095/*
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00001096 * Read a part of the flash chip.
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001097 * FIXME: Use the chunk code from Michael Karcher instead.
Sean Nelson14ba6682010-02-26 05:48:29 +00001098 * Each page is read separately in chunks with a maximum size of chunksize.
1099 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001100int spi_read_chunked(struct flashctx *flash, uint8_t *buf, unsigned int start,
1101 unsigned int len, unsigned int chunksize)
Sean Nelson14ba6682010-02-26 05:48:29 +00001102{
1103 int rc = 0;
Stefan Taunerc69c9c82011-11-23 09:13:48 +00001104 unsigned int i, j, starthere, lenhere, toread;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001105 unsigned int page_size = flash->chip->page_size;
Sean Nelson14ba6682010-02-26 05:48:29 +00001106
1107 /* Warning: This loop has a very unusual condition and body.
1108 * The loop needs to go through each page with at least one affected
1109 * byte. The lowest page number is (start / page_size) since that
1110 * division rounds down. The highest page number we want is the page
1111 * where the last byte of the range lives. That last byte has the
1112 * address (start + len - 1), thus the highest page number is
1113 * (start + len - 1) / page_size. Since we want to include that last
1114 * page as well, the loop condition uses <=.
1115 */
1116 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
1117 /* Byte position of the first byte in the range in this page. */
1118 /* starthere is an offset to the base address of the chip. */
1119 starthere = max(start, i * page_size);
1120 /* Length of bytes in the range in this page. */
1121 lenhere = min(start + len, (i + 1) * page_size) - starthere;
1122 for (j = 0; j < lenhere; j += chunksize) {
1123 toread = min(chunksize, lenhere - j);
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001124 rc = spi_nbyte_read(flash, starthere + j, buf + starthere - start + j, toread);
Sean Nelson14ba6682010-02-26 05:48:29 +00001125 if (rc)
1126 break;
1127 }
1128 if (rc)
1129 break;
1130 }
1131
1132 return rc;
1133}
1134
1135/*
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00001136 * Write a part of the flash chip.
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001137 * FIXME: Use the chunk code from Michael Karcher instead.
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00001138 * Each page is written separately in chunks with a maximum size of chunksize.
1139 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001140int spi_write_chunked(struct flashctx *flash, uint8_t *buf, unsigned int start,
1141 unsigned int len, unsigned int chunksize)
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00001142{
1143 int rc = 0;
Stefan Taunerc69c9c82011-11-23 09:13:48 +00001144 unsigned int i, j, starthere, lenhere, towrite;
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00001145 /* FIXME: page_size is the wrong variable. We need max_writechunk_size
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +00001146 * in struct flashctx to do this properly. All chips using
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00001147 * spi_chip_write_256 have page_size set to max_writechunk_size, so
1148 * we're OK for now.
1149 */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001150 unsigned int page_size = flash->chip->page_size;
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00001151
1152 /* Warning: This loop has a very unusual condition and body.
1153 * The loop needs to go through each page with at least one affected
1154 * byte. The lowest page number is (start / page_size) since that
1155 * division rounds down. The highest page number we want is the page
1156 * where the last byte of the range lives. That last byte has the
1157 * address (start + len - 1), thus the highest page number is
1158 * (start + len - 1) / page_size. Since we want to include that last
1159 * page as well, the loop condition uses <=.
1160 */
1161 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
1162 /* Byte position of the first byte in the range in this page. */
1163 /* starthere is an offset to the base address of the chip. */
1164 starthere = max(start, i * page_size);
1165 /* Length of bytes in the range in this page. */
1166 lenhere = min(start + len, (i + 1) * page_size) - starthere;
1167 for (j = 0; j < lenhere; j += chunksize) {
1168 towrite = min(chunksize, lenhere - j);
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001169 rc = spi_nbyte_program(flash, starthere + j, buf + starthere - start + j, towrite);
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00001170 if (rc)
1171 break;
Stefan Tauner5e695ab2012-05-06 17:03:40 +00001172 while (spi_read_status_register(flash) & SPI_SR_WIP)
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00001173 programmer_delay(10);
1174 }
1175 if (rc)
1176 break;
1177 }
1178
1179 return rc;
1180}
1181
1182/*
Sean Nelson14ba6682010-02-26 05:48:29 +00001183 * Program chip using byte programming. (SLOW!)
1184 * This is for chips which can only handle one byte writes
1185 * and for chips where memory mapped programming is impossible
1186 * (e.g. due to size constraints in IT87* for over 512 kB)
1187 */
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001188/* real chunksize is 1, logical chunksize is 1 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001189int spi_chip_write_1(struct flashctx *flash, uint8_t *buf, unsigned int start,
1190 unsigned int len)
Sean Nelson14ba6682010-02-26 05:48:29 +00001191{
Stefan Taunerc69c9c82011-11-23 09:13:48 +00001192 unsigned int i;
1193 int result = 0;
Sean Nelson14ba6682010-02-26 05:48:29 +00001194
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001195 for (i = start; i < start + len; i++) {
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001196 result = spi_byte_program(flash, i, buf[i - start]);
Sean Nelson14ba6682010-02-26 05:48:29 +00001197 if (result)
1198 return 1;
Stefan Tauner5e695ab2012-05-06 17:03:40 +00001199 while (spi_read_status_register(flash) & SPI_SR_WIP)
Sean Nelson14ba6682010-02-26 05:48:29 +00001200 programmer_delay(10);
1201 }
1202
1203 return 0;
1204}
1205
Nico Huber7bca1262012-06-15 22:28:12 +00001206int default_spi_write_aai(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001207{
1208 uint32_t pos = start;
Sean Nelson14ba6682010-02-26 05:48:29 +00001209 int result;
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001210 unsigned char cmd[JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE] = {
1211 JEDEC_AAI_WORD_PROGRAM,
1212 };
1213 struct spi_command cmds[] = {
1214 {
1215 .writecnt = JEDEC_WREN_OUTSIZE,
1216 .writearr = (const unsigned char[]){ JEDEC_WREN },
1217 .readcnt = 0,
1218 .readarr = NULL,
1219 }, {
1220 .writecnt = JEDEC_AAI_WORD_PROGRAM_OUTSIZE,
1221 .writearr = (const unsigned char[]){
1222 JEDEC_AAI_WORD_PROGRAM,
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001223 (start >> 16) & 0xff,
1224 (start >> 8) & 0xff,
1225 (start & 0xff),
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001226 buf[0],
1227 buf[1]
1228 },
1229 .readcnt = 0,
1230 .readarr = NULL,
1231 }, {
1232 .writecnt = 0,
1233 .writearr = NULL,
1234 .readcnt = 0,
1235 .readarr = NULL,
1236 }};
Sean Nelson14ba6682010-02-26 05:48:29 +00001237
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +00001238 switch (flash->pgm->spi.type) {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +00001239#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001240#if defined(__i386__) || defined(__x86_64__)
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001241 case SPI_CONTROLLER_IT87XX:
Sean Nelson14ba6682010-02-26 05:48:29 +00001242 case SPI_CONTROLLER_WBSIO:
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001243 msg_perr("%s: impossible with this SPI controller,"
Sean Nelson14ba6682010-02-26 05:48:29 +00001244 " degrading to byte program\n", __func__);
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001245 return spi_chip_write_1(flash, buf, start, len);
Sean Nelson14ba6682010-02-26 05:48:29 +00001246#endif
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001247#endif
Sean Nelson14ba6682010-02-26 05:48:29 +00001248 default:
1249 break;
1250 }
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001251
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001252 /* The even start address and even length requirements can be either
1253 * honored outside this function, or we can call spi_byte_program
1254 * for the first and/or last byte and use AAI for the rest.
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001255 * FIXME: Move this to generic code.
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001256 */
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001257 /* The data sheet requires a start address with the low bit cleared. */
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001258 if (start % 2) {
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001259 msg_cerr("%s: start address not even! Please report a bug at "
1260 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001261 if (spi_chip_write_1(flash, buf, start, start % 2))
1262 return SPI_GENERIC_ERROR;
1263 pos += start % 2;
Carl-Daniel Hailfingerccfe0ac2010-10-27 22:07:11 +00001264 cmds[1].writearr = (const unsigned char[]){
1265 JEDEC_AAI_WORD_PROGRAM,
1266 (pos >> 16) & 0xff,
1267 (pos >> 8) & 0xff,
1268 (pos & 0xff),
1269 buf[pos - start],
1270 buf[pos - start + 1]
1271 };
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001272 /* Do not return an error for now. */
1273 //return SPI_GENERIC_ERROR;
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001274 }
1275 /* The data sheet requires total AAI write length to be even. */
1276 if (len % 2) {
1277 msg_cerr("%s: total write length not even! Please report a "
1278 "bug at flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001279 /* Do not return an error for now. */
1280 //return SPI_GENERIC_ERROR;
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001281 }
1282
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001283
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001284 result = spi_send_multicommand(flash, cmds);
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001285 if (result) {
1286 msg_cerr("%s failed during start command execution\n",
1287 __func__);
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001288 /* FIXME: Should we send WRDI here as well to make sure the chip
1289 * is not in AAI mode?
1290 */
Sean Nelson14ba6682010-02-26 05:48:29 +00001291 return result;
Sean Nelson14ba6682010-02-26 05:48:29 +00001292 }
Stefan Tauner5e695ab2012-05-06 17:03:40 +00001293 while (spi_read_status_register(flash) & SPI_SR_WIP)
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001294 programmer_delay(10);
1295
1296 /* We already wrote 2 bytes in the multicommand step. */
1297 pos += 2;
1298
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001299 /* Are there at least two more bytes to write? */
1300 while (pos < start + len - 1) {
Carl-Daniel Hailfingerccfe0ac2010-10-27 22:07:11 +00001301 cmd[1] = buf[pos++ - start];
1302 cmd[2] = buf[pos++ - start];
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001303 spi_send_command(flash, JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE, 0,
1304 cmd, NULL);
Stefan Tauner5e695ab2012-05-06 17:03:40 +00001305 while (spi_read_status_register(flash) & SPI_SR_WIP)
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001306 programmer_delay(10);
1307 }
1308
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001309 /* Use WRDI to exit AAI mode. This needs to be done before issuing any
1310 * other non-AAI command.
1311 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001312 spi_write_disable(flash);
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001313
1314 /* Write remaining byte (if any). */
1315 if (pos < start + len) {
Carl-Daniel Hailfingerccfe0ac2010-10-27 22:07:11 +00001316 if (spi_chip_write_1(flash, buf + pos - start, pos, pos % 2))
Carl-Daniel Hailfinger75a58f92010-10-13 22:26:56 +00001317 return SPI_GENERIC_ERROR;
1318 pos += pos % 2;
1319 }
1320
Sean Nelson14ba6682010-02-26 05:48:29 +00001321 return 0;
1322}