blob: b8b26bded85f507cadecadc427d18b3f3bed9c8c [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"
29#include "spi.h"
30
31void spi_prettyprint_status_register(struct flashchip *flash);
32
33static int spi_rdid(unsigned char *readarr, int bytes)
34{
35 const unsigned char cmd[JEDEC_RDID_OUTSIZE] = { JEDEC_RDID };
36 int ret;
37 int i;
38
39 ret = spi_send_command(sizeof(cmd), bytes, cmd, readarr);
40 if (ret)
41 return ret;
Sean Nelsoned479d22010-03-24 23:14:32 +000042 msg_cspew("RDID returned");
Sean Nelson14ba6682010-02-26 05:48:29 +000043 for (i = 0; i < bytes; i++)
Sean Nelsoned479d22010-03-24 23:14:32 +000044 msg_cspew(" 0x%02x", readarr[i]);
45 msg_cspew(". ");
Sean Nelson14ba6682010-02-26 05:48:29 +000046 return 0;
47}
48
49static int spi_rems(unsigned char *readarr)
50{
51 unsigned char cmd[JEDEC_REMS_OUTSIZE] = { JEDEC_REMS, 0, 0, 0 };
52 uint32_t readaddr;
53 int ret;
54
55 ret = spi_send_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
56 if (ret == SPI_INVALID_ADDRESS) {
57 /* Find the lowest even address allowed for reads. */
58 readaddr = (spi_get_valid_read_addr() + 1) & ~1;
59 cmd[1] = (readaddr >> 16) & 0xff,
60 cmd[2] = (readaddr >> 8) & 0xff,
61 cmd[3] = (readaddr >> 0) & 0xff,
62 ret = spi_send_command(sizeof(cmd), JEDEC_REMS_INSIZE, cmd, readarr);
63 }
64 if (ret)
65 return ret;
Sean Nelsoned479d22010-03-24 23:14:32 +000066 msg_cspew("REMS returned %02x %02x. ", readarr[0], readarr[1]);
Sean Nelson14ba6682010-02-26 05:48:29 +000067 return 0;
68}
69
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +000070static int spi_res(unsigned char *readarr, int bytes)
Sean Nelson14ba6682010-02-26 05:48:29 +000071{
72 unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, 0, 0, 0 };
73 uint32_t readaddr;
74 int ret;
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +000075 int i;
Sean Nelson14ba6682010-02-26 05:48:29 +000076
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +000077 ret = spi_send_command(sizeof(cmd), bytes, cmd, readarr);
Sean Nelson14ba6682010-02-26 05:48:29 +000078 if (ret == SPI_INVALID_ADDRESS) {
79 /* Find the lowest even address allowed for reads. */
80 readaddr = (spi_get_valid_read_addr() + 1) & ~1;
81 cmd[1] = (readaddr >> 16) & 0xff,
82 cmd[2] = (readaddr >> 8) & 0xff,
83 cmd[3] = (readaddr >> 0) & 0xff,
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +000084 ret = spi_send_command(sizeof(cmd), bytes, cmd, readarr);
Sean Nelson14ba6682010-02-26 05:48:29 +000085 }
86 if (ret)
87 return ret;
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +000088 msg_cspew("RES returned");
89 for (i = 0; i < bytes; i++)
90 msg_cspew(" 0x%02x", readarr[i]);
91 msg_cspew(". ");
Sean Nelson14ba6682010-02-26 05:48:29 +000092 return 0;
93}
94
95int spi_write_enable(void)
96{
97 const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN };
98 int result;
99
100 /* Send WREN (Write Enable) */
101 result = spi_send_command(sizeof(cmd), 0, cmd, NULL);
102
103 if (result)
Sean Nelsoned479d22010-03-24 23:14:32 +0000104 msg_cerr("%s failed\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000105
106 return result;
107}
108
109int spi_write_disable(void)
110{
111 const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI };
112
113 /* Send WRDI (Write Disable) */
114 return spi_send_command(sizeof(cmd), 0, cmd, NULL);
115}
116
117static int probe_spi_rdid_generic(struct flashchip *flash, int bytes)
118{
119 unsigned char readarr[4];
120 uint32_t id1;
121 uint32_t id2;
122
123 if (spi_rdid(readarr, bytes))
124 return 0;
125
126 if (!oddparity(readarr[0]))
Sean Nelsoned479d22010-03-24 23:14:32 +0000127 msg_cdbg("RDID byte 0 parity violation. ");
Sean Nelson14ba6682010-02-26 05:48:29 +0000128
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000129 /* Check if this is a continuation vendor ID.
130 * FIXME: Handle continuation device IDs.
131 */
Sean Nelson14ba6682010-02-26 05:48:29 +0000132 if (readarr[0] == 0x7f) {
133 if (!oddparity(readarr[1]))
Sean Nelsoned479d22010-03-24 23:14:32 +0000134 msg_cdbg("RDID byte 1 parity violation. ");
Sean Nelson14ba6682010-02-26 05:48:29 +0000135 id1 = (readarr[0] << 8) | readarr[1];
136 id2 = readarr[2];
137 if (bytes > 3) {
138 id2 <<= 8;
139 id2 |= readarr[3];
140 }
141 } else {
142 id1 = readarr[0];
143 id2 = (readarr[1] << 8) | readarr[2];
144 }
145
Sean Nelsoned479d22010-03-24 23:14:32 +0000146 msg_cdbg("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
Sean Nelson14ba6682010-02-26 05:48:29 +0000147
148 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
149 /* Print the status register to tell the
150 * user about possible write protection.
151 */
152 spi_prettyprint_status_register(flash);
153
154 return 1;
155 }
156
157 /* Test if this is a pure vendor match. */
158 if (id1 == flash->manufacture_id &&
159 GENERIC_DEVICE_ID == flash->model_id)
160 return 1;
161
162 /* Test if there is any vendor ID. */
163 if (GENERIC_MANUF_ID == flash->manufacture_id &&
164 id1 != 0xff)
165 return 1;
166
167 return 0;
168}
169
170int probe_spi_rdid(struct flashchip *flash)
171{
172 return probe_spi_rdid_generic(flash, 3);
173}
174
Sean Nelson14ba6682010-02-26 05:48:29 +0000175int probe_spi_rdid4(struct flashchip *flash)
176{
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000177 /* Some SPI controllers do not support commands with writecnt=1 and
178 * readcnt=4.
179 */
Sean Nelson14ba6682010-02-26 05:48:29 +0000180 switch (spi_controller) {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000181#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000182#if defined(__i386__) || defined(__x86_64__)
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000183 case SPI_CONTROLLER_IT87XX:
Sean Nelson14ba6682010-02-26 05:48:29 +0000184 case SPI_CONTROLLER_WBSIO:
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000185 msg_cinfo("4 byte RDID not supported on this SPI controller\n");
186 return 0;
187 break;
Sean Nelson14ba6682010-02-26 05:48:29 +0000188#endif
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000189#endif
Sean Nelson14ba6682010-02-26 05:48:29 +0000190 default:
Carl-Daniel Hailfinger8ae500e2010-06-20 10:39:33 +0000191 return probe_spi_rdid_generic(flash, 4);
Sean Nelson14ba6682010-02-26 05:48:29 +0000192 }
193
194 return 0;
195}
196
197int probe_spi_rems(struct flashchip *flash)
198{
199 unsigned char readarr[JEDEC_REMS_INSIZE];
200 uint32_t id1, id2;
201
202 if (spi_rems(readarr))
203 return 0;
204
205 id1 = readarr[0];
206 id2 = readarr[1];
207
Sean Nelsoned479d22010-03-24 23:14:32 +0000208 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
Sean Nelson14ba6682010-02-26 05:48:29 +0000209
210 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
211 /* Print the status register to tell the
212 * user about possible write protection.
213 */
214 spi_prettyprint_status_register(flash);
215
216 return 1;
217 }
218
219 /* Test if this is a pure vendor match. */
220 if (id1 == flash->manufacture_id &&
221 GENERIC_DEVICE_ID == flash->model_id)
222 return 1;
223
224 /* Test if there is any vendor ID. */
225 if (GENERIC_MANUF_ID == flash->manufacture_id &&
226 id1 != 0xff)
227 return 1;
228
229 return 0;
230}
231
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000232int probe_spi_res1(struct flashchip *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000233{
234 unsigned char readarr[3];
235 uint32_t id2;
236 const unsigned char allff[] = {0xff, 0xff, 0xff};
237 const unsigned char all00[] = {0x00, 0x00, 0x00};
238
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000239 /* We only want one-byte RES if RDID and REMS are unusable. */
240
Sean Nelson14ba6682010-02-26 05:48:29 +0000241 /* Check if RDID is usable and does not return 0xff 0xff 0xff or
242 * 0x00 0x00 0x00. In that case, RES is pointless.
243 */
244 if (!spi_rdid(readarr, 3) && memcmp(readarr, allff, 3) &&
245 memcmp(readarr, all00, 3)) {
246 msg_cdbg("Ignoring RES in favour of RDID.\n");
247 return 0;
248 }
249 /* Check if REMS is usable and does not return 0xff 0xff or
250 * 0x00 0x00. In that case, RES is pointless.
251 */
252 if (!spi_rems(readarr) && memcmp(readarr, allff, JEDEC_REMS_INSIZE) &&
253 memcmp(readarr, all00, JEDEC_REMS_INSIZE)) {
254 msg_cdbg("Ignoring RES in favour of REMS.\n");
255 return 0;
256 }
257
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000258 if (spi_res(readarr, 1))
Sean Nelson14ba6682010-02-26 05:48:29 +0000259 return 0;
260
Sean Nelson14ba6682010-02-26 05:48:29 +0000261 id2 = readarr[0];
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000262
Sean Nelsoned479d22010-03-24 23:14:32 +0000263 msg_cdbg("%s: id 0x%x\n", __func__, id2);
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000264
Sean Nelson14ba6682010-02-26 05:48:29 +0000265 if (id2 != flash->model_id)
266 return 0;
267
268 /* Print the status register to tell the
269 * user about possible write protection.
270 */
271 spi_prettyprint_status_register(flash);
272 return 1;
273}
274
Carl-Daniel Hailfingerdc1cda12010-05-28 17:07:57 +0000275int probe_spi_res2(struct flashchip *flash)
276{
277 unsigned char readarr[2];
278 uint32_t id1, id2;
279
280 if (spi_res(readarr, 2))
281 return 0;
282
283 id1 = readarr[0];
284 id2 = readarr[1];
285
286 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
287
288 if (id1 != flash->manufacture_id || id2 != flash->model_id)
289 return 0;
290
291 /* Print the status register to tell the
292 * user about possible write protection.
293 */
294 spi_prettyprint_status_register(flash);
295 return 1;
296}
297
Sean Nelson14ba6682010-02-26 05:48:29 +0000298uint8_t spi_read_status_register(void)
299{
300 const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR };
301 /* FIXME: No workarounds for driver/hardware bugs in generic code. */
302 unsigned char readarr[2]; /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */
303 int ret;
304
305 /* Read Status Register */
306 ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
307 if (ret)
Sean Nelsoned479d22010-03-24 23:14:32 +0000308 msg_cerr("RDSR failed!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000309
310 return readarr[0];
311}
312
313/* Prettyprint the status register. Common definitions. */
314void spi_prettyprint_status_register_common(uint8_t status)
315{
Sean Nelsoned479d22010-03-24 23:14:32 +0000316 msg_cdbg("Chip status register: Bit 5 / Block Protect 3 (BP3) is "
Sean Nelson14ba6682010-02-26 05:48:29 +0000317 "%sset\n", (status & (1 << 5)) ? "" : "not ");
Sean Nelsoned479d22010-03-24 23:14:32 +0000318 msg_cdbg("Chip status register: Bit 4 / Block Protect 2 (BP2) is "
Sean Nelson14ba6682010-02-26 05:48:29 +0000319 "%sset\n", (status & (1 << 4)) ? "" : "not ");
Sean Nelsoned479d22010-03-24 23:14:32 +0000320 msg_cdbg("Chip status register: Bit 3 / Block Protect 1 (BP1) is "
Sean Nelson14ba6682010-02-26 05:48:29 +0000321 "%sset\n", (status & (1 << 3)) ? "" : "not ");
Sean Nelsoned479d22010-03-24 23:14:32 +0000322 msg_cdbg("Chip status register: Bit 2 / Block Protect 0 (BP0) is "
Sean Nelson14ba6682010-02-26 05:48:29 +0000323 "%sset\n", (status & (1 << 2)) ? "" : "not ");
Sean Nelsoned479d22010-03-24 23:14:32 +0000324 msg_cdbg("Chip status register: Write Enable Latch (WEL) is "
Sean Nelson14ba6682010-02-26 05:48:29 +0000325 "%sset\n", (status & (1 << 1)) ? "" : "not ");
Sean Nelsoned479d22010-03-24 23:14:32 +0000326 msg_cdbg("Chip status register: Write In Progress (WIP/BUSY) is "
Sean Nelson14ba6682010-02-26 05:48:29 +0000327 "%sset\n", (status & (1 << 0)) ? "" : "not ");
328}
329
330/* Prettyprint the status register. Works for
Daniel Lenskidf90d3a2010-07-22 11:44:38 +0000331 * AMIC A25L series
332 */
333void spi_prettyprint_status_register_amic_a25l(uint8_t status)
334{
335 msg_cdbg("Chip status register: Status Register Write Disable "
336 "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not ");
337 spi_prettyprint_status_register_common(status);
338}
339
340/* Prettyprint the status register. Works for
Sean Nelson14ba6682010-02-26 05:48:29 +0000341 * ST M25P series
342 * MX MX25L series
343 */
344void spi_prettyprint_status_register_st_m25p(uint8_t status)
345{
Sean Nelsoned479d22010-03-24 23:14:32 +0000346 msg_cdbg("Chip status register: Status Register Write Disable "
Sean Nelson14ba6682010-02-26 05:48:29 +0000347 "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not ");
Sean Nelsoned479d22010-03-24 23:14:32 +0000348 msg_cdbg("Chip status register: Bit 6 is "
Sean Nelson14ba6682010-02-26 05:48:29 +0000349 "%sset\n", (status & (1 << 6)) ? "" : "not ");
350 spi_prettyprint_status_register_common(status);
351}
352
353void spi_prettyprint_status_register_sst25(uint8_t status)
354{
Sean Nelsoned479d22010-03-24 23:14:32 +0000355 msg_cdbg("Chip status register: Block Protect Write Disable "
Sean Nelson14ba6682010-02-26 05:48:29 +0000356 "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not ");
Sean Nelsoned479d22010-03-24 23:14:32 +0000357 msg_cdbg("Chip status register: Auto Address Increment Programming "
Sean Nelson14ba6682010-02-26 05:48:29 +0000358 "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not ");
359 spi_prettyprint_status_register_common(status);
360}
361
362/* Prettyprint the status register. Works for
363 * SST 25VF016
364 */
365void spi_prettyprint_status_register_sst25vf016(uint8_t status)
366{
367 const char *bpt[] = {
368 "none",
369 "1F0000H-1FFFFFH",
370 "1E0000H-1FFFFFH",
371 "1C0000H-1FFFFFH",
372 "180000H-1FFFFFH",
373 "100000H-1FFFFFH",
374 "all", "all"
375 };
376 spi_prettyprint_status_register_sst25(status);
Sean Nelsoned479d22010-03-24 23:14:32 +0000377 msg_cdbg("Resulting block protection : %s\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000378 bpt[(status & 0x1c) >> 2]);
379}
380
381void spi_prettyprint_status_register_sst25vf040b(uint8_t status)
382{
383 const char *bpt[] = {
384 "none",
385 "0x70000-0x7ffff",
386 "0x60000-0x7ffff",
387 "0x40000-0x7ffff",
388 "all blocks", "all blocks", "all blocks", "all blocks"
389 };
390 spi_prettyprint_status_register_sst25(status);
Sean Nelsoned479d22010-03-24 23:14:32 +0000391 msg_cdbg("Resulting block protection : %s\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000392 bpt[(status & 0x1c) >> 2]);
393}
394
395void spi_prettyprint_status_register(struct flashchip *flash)
396{
397 uint8_t status;
398
399 status = spi_read_status_register();
Sean Nelsoned479d22010-03-24 23:14:32 +0000400 msg_cdbg("Chip status register is %02x\n", status);
Sean Nelson14ba6682010-02-26 05:48:29 +0000401 switch (flash->manufacture_id) {
Daniel Lenskidf90d3a2010-07-22 11:44:38 +0000402 case AMIC_ID:
403 if ((flash->model_id & 0xff00) == 0x2000)
404 spi_prettyprint_status_register_amic_a25l(status);
405 break;
Sean Nelson14ba6682010-02-26 05:48:29 +0000406 case ST_ID:
407 if (((flash->model_id & 0xff00) == 0x2000) ||
408 ((flash->model_id & 0xff00) == 0x2500))
409 spi_prettyprint_status_register_st_m25p(status);
410 break;
411 case MX_ID:
412 if ((flash->model_id & 0xff00) == 0x2000)
413 spi_prettyprint_status_register_st_m25p(status);
414 break;
415 case SST_ID:
416 switch (flash->model_id) {
417 case 0x2541:
418 spi_prettyprint_status_register_sst25vf016(status);
419 break;
420 case 0x8d:
421 case 0x258d:
422 spi_prettyprint_status_register_sst25vf040b(status);
423 break;
424 default:
425 spi_prettyprint_status_register_sst25(status);
426 break;
427 }
428 break;
429 }
430}
431
432int spi_chip_erase_60(struct flashchip *flash)
433{
434 int result;
435 struct spi_command cmds[] = {
436 {
437 .writecnt = JEDEC_WREN_OUTSIZE,
438 .writearr = (const unsigned char[]){ JEDEC_WREN },
439 .readcnt = 0,
440 .readarr = NULL,
441 }, {
442 .writecnt = JEDEC_CE_60_OUTSIZE,
443 .writearr = (const unsigned char[]){ JEDEC_CE_60 },
444 .readcnt = 0,
445 .readarr = NULL,
446 }, {
447 .writecnt = 0,
448 .writearr = NULL,
449 .readcnt = 0,
450 .readarr = NULL,
451 }};
452
Sean Nelson14ba6682010-02-26 05:48:29 +0000453 result = spi_send_multicommand(cmds);
454 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000455 msg_cerr("%s failed during command execution\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000456 __func__);
457 return result;
458 }
459 /* Wait until the Write-In-Progress bit is cleared.
460 * This usually takes 1-85 s, so wait in 1 s steps.
461 */
462 /* FIXME: We assume spi_read_status_register will never fail. */
463 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
464 programmer_delay(1000 * 1000);
465 if (check_erased_range(flash, 0, flash->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000466 msg_cerr("ERASE FAILED!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000467 return -1;
468 }
469 return 0;
470}
471
472int spi_chip_erase_c7(struct flashchip *flash)
473{
474 int result;
475 struct spi_command cmds[] = {
476 {
477 .writecnt = JEDEC_WREN_OUTSIZE,
478 .writearr = (const unsigned char[]){ JEDEC_WREN },
479 .readcnt = 0,
480 .readarr = NULL,
481 }, {
482 .writecnt = JEDEC_CE_C7_OUTSIZE,
483 .writearr = (const unsigned char[]){ JEDEC_CE_C7 },
484 .readcnt = 0,
485 .readarr = NULL,
486 }, {
487 .writecnt = 0,
488 .writearr = NULL,
489 .readcnt = 0,
490 .readarr = NULL,
491 }};
492
Sean Nelson14ba6682010-02-26 05:48:29 +0000493 result = spi_send_multicommand(cmds);
494 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000495 msg_cerr("%s failed during command execution\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000496 return result;
497 }
498 /* Wait until the Write-In-Progress bit is cleared.
499 * This usually takes 1-85 s, so wait in 1 s steps.
500 */
501 /* FIXME: We assume spi_read_status_register will never fail. */
502 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
503 programmer_delay(1000 * 1000);
504 if (check_erased_range(flash, 0, flash->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000505 msg_cerr("ERASE FAILED!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000506 return -1;
507 }
508 return 0;
509}
510
Sean Nelson14ba6682010-02-26 05:48:29 +0000511int spi_block_erase_52(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
512{
513 int result;
514 struct spi_command cmds[] = {
515 {
516 .writecnt = JEDEC_WREN_OUTSIZE,
517 .writearr = (const unsigned char[]){ JEDEC_WREN },
518 .readcnt = 0,
519 .readarr = NULL,
520 }, {
521 .writecnt = JEDEC_BE_52_OUTSIZE,
522 .writearr = (const unsigned char[]){
523 JEDEC_BE_52,
524 (addr >> 16) & 0xff,
525 (addr >> 8) & 0xff,
526 (addr & 0xff)
527 },
528 .readcnt = 0,
529 .readarr = NULL,
530 }, {
531 .writecnt = 0,
532 .writearr = NULL,
533 .readcnt = 0,
534 .readarr = NULL,
535 }};
536
537 result = spi_send_multicommand(cmds);
538 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000539 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000540 __func__, addr);
541 return result;
542 }
543 /* Wait until the Write-In-Progress bit is cleared.
544 * This usually takes 100-4000 ms, so wait in 100 ms steps.
545 */
546 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
547 programmer_delay(100 * 1000);
548 if (check_erased_range(flash, addr, blocklen)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000549 msg_cerr("ERASE FAILED!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000550 return -1;
551 }
552 return 0;
553}
554
555/* Block size is usually
556 * 64k for Macronix
557 * 32k for SST
558 * 4-32k non-uniform for EON
559 */
560int spi_block_erase_d8(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
561{
562 int result;
563 struct spi_command cmds[] = {
564 {
565 .writecnt = JEDEC_WREN_OUTSIZE,
566 .writearr = (const unsigned char[]){ JEDEC_WREN },
567 .readcnt = 0,
568 .readarr = NULL,
569 }, {
570 .writecnt = JEDEC_BE_D8_OUTSIZE,
571 .writearr = (const unsigned char[]){
572 JEDEC_BE_D8,
573 (addr >> 16) & 0xff,
574 (addr >> 8) & 0xff,
575 (addr & 0xff)
576 },
577 .readcnt = 0,
578 .readarr = NULL,
579 }, {
580 .writecnt = 0,
581 .writearr = NULL,
582 .readcnt = 0,
583 .readarr = NULL,
584 }};
585
586 result = spi_send_multicommand(cmds);
587 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000588 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000589 __func__, addr);
590 return result;
591 }
592 /* Wait until the Write-In-Progress bit is cleared.
593 * This usually takes 100-4000 ms, so wait in 100 ms steps.
594 */
595 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
596 programmer_delay(100 * 1000);
597 if (check_erased_range(flash, addr, blocklen)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000598 msg_cerr("ERASE FAILED!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000599 return -1;
600 }
601 return 0;
602}
603
604/* Block size is usually
605 * 4k for PMC
606 */
607int spi_block_erase_d7(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
608{
609 int result;
610 struct spi_command cmds[] = {
611 {
612 .writecnt = JEDEC_WREN_OUTSIZE,
613 .writearr = (const unsigned char[]){ JEDEC_WREN },
614 .readcnt = 0,
615 .readarr = NULL,
616 }, {
617 .writecnt = JEDEC_BE_D7_OUTSIZE,
618 .writearr = (const unsigned char[]){
619 JEDEC_BE_D7,
620 (addr >> 16) & 0xff,
621 (addr >> 8) & 0xff,
622 (addr & 0xff)
623 },
624 .readcnt = 0,
625 .readarr = NULL,
626 }, {
627 .writecnt = 0,
628 .writearr = NULL,
629 .readcnt = 0,
630 .readarr = NULL,
631 }};
632
633 result = spi_send_multicommand(cmds);
634 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000635 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000636 __func__, addr);
637 return result;
638 }
639 /* Wait until the Write-In-Progress bit is cleared.
640 * This usually takes 100-4000 ms, so wait in 100 ms steps.
641 */
642 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
643 programmer_delay(100 * 1000);
644 if (check_erased_range(flash, addr, blocklen)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000645 msg_cerr("ERASE FAILED!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000646 return -1;
647 }
648 return 0;
649}
650
Sean Nelson14ba6682010-02-26 05:48:29 +0000651/* Sector size is usually 4k, though Macronix eliteflash has 64k */
652int spi_block_erase_20(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
653{
654 int result;
655 struct spi_command cmds[] = {
656 {
657 .writecnt = JEDEC_WREN_OUTSIZE,
658 .writearr = (const unsigned char[]){ JEDEC_WREN },
659 .readcnt = 0,
660 .readarr = NULL,
661 }, {
662 .writecnt = JEDEC_SE_OUTSIZE,
663 .writearr = (const unsigned char[]){
664 JEDEC_SE,
665 (addr >> 16) & 0xff,
666 (addr >> 8) & 0xff,
667 (addr & 0xff)
668 },
669 .readcnt = 0,
670 .readarr = NULL,
671 }, {
672 .writecnt = 0,
673 .writearr = NULL,
674 .readcnt = 0,
675 .readarr = NULL,
676 }};
677
678 result = spi_send_multicommand(cmds);
679 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000680 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000681 __func__, addr);
682 return result;
683 }
684 /* Wait until the Write-In-Progress bit is cleared.
685 * This usually takes 15-800 ms, so wait in 10 ms steps.
686 */
687 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
688 programmer_delay(10 * 1000);
689 if (check_erased_range(flash, addr, blocklen)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000690 msg_cerr("ERASE FAILED!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000691 return -1;
692 }
693 return 0;
694}
695
696int spi_block_erase_60(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
697{
698 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000699 msg_cerr("%s called with incorrect arguments\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000700 __func__);
701 return -1;
702 }
703 return spi_chip_erase_60(flash);
704}
705
706int spi_block_erase_c7(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
707{
708 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000709 msg_cerr("%s called with incorrect arguments\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000710 __func__);
711 return -1;
712 }
713 return spi_chip_erase_c7(flash);
714}
715
716int spi_write_status_enable(void)
717{
718 const unsigned char cmd[JEDEC_EWSR_OUTSIZE] = { JEDEC_EWSR };
719 int result;
720
721 /* Send EWSR (Enable Write Status Register). */
722 result = spi_send_command(sizeof(cmd), JEDEC_EWSR_INSIZE, cmd, NULL);
723
724 if (result)
Sean Nelsoned479d22010-03-24 23:14:32 +0000725 msg_cerr("%s failed\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000726
727 return result;
728}
729
730/*
731 * This is according the SST25VF016 datasheet, who knows it is more
732 * generic that this...
733 */
734int spi_write_status_register(int status)
735{
736 int result;
737 struct spi_command cmds[] = {
738 {
739 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
740 .writecnt = JEDEC_EWSR_OUTSIZE,
741 .writearr = (const unsigned char[]){ JEDEC_EWSR },
742 .readcnt = 0,
743 .readarr = NULL,
744 }, {
745 .writecnt = JEDEC_WRSR_OUTSIZE,
746 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
747 .readcnt = 0,
748 .readarr = NULL,
749 }, {
750 .writecnt = 0,
751 .writearr = NULL,
752 .readcnt = 0,
753 .readarr = NULL,
754 }};
755
756 result = spi_send_multicommand(cmds);
757 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000758 msg_cerr("%s failed during command execution\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000759 __func__);
760 }
761 return result;
762}
763
764int spi_byte_program(int addr, uint8_t databyte)
765{
766 int result;
767 struct spi_command cmds[] = {
768 {
769 .writecnt = JEDEC_WREN_OUTSIZE,
770 .writearr = (const unsigned char[]){ JEDEC_WREN },
771 .readcnt = 0,
772 .readarr = NULL,
773 }, {
774 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE,
775 .writearr = (const unsigned char[]){
776 JEDEC_BYTE_PROGRAM,
777 (addr >> 16) & 0xff,
778 (addr >> 8) & 0xff,
779 (addr & 0xff),
780 databyte
781 },
782 .readcnt = 0,
783 .readarr = NULL,
784 }, {
785 .writecnt = 0,
786 .writearr = NULL,
787 .readcnt = 0,
788 .readarr = NULL,
789 }};
790
791 result = spi_send_multicommand(cmds);
792 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000793 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000794 __func__, addr);
795 }
796 return result;
797}
798
799int spi_nbyte_program(int addr, uint8_t *bytes, int len)
800{
801 int result;
802 /* FIXME: Switch to malloc based on len unless that kills speed. */
803 unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + 256] = {
804 JEDEC_BYTE_PROGRAM,
805 (addr >> 16) & 0xff,
806 (addr >> 8) & 0xff,
807 (addr >> 0) & 0xff,
808 };
809 struct spi_command cmds[] = {
810 {
811 .writecnt = JEDEC_WREN_OUTSIZE,
812 .writearr = (const unsigned char[]){ JEDEC_WREN },
813 .readcnt = 0,
814 .readarr = NULL,
815 }, {
816 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + len,
817 .writearr = cmd,
818 .readcnt = 0,
819 .readarr = NULL,
820 }, {
821 .writecnt = 0,
822 .writearr = NULL,
823 .readcnt = 0,
824 .readarr = NULL,
825 }};
826
827 if (!len) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000828 msg_cerr("%s called for zero-length write\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000829 return 1;
830 }
831 if (len > 256) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000832 msg_cerr("%s called for too long a write\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000833 return 1;
834 }
835
836 memcpy(&cmd[4], bytes, len);
837
838 result = spi_send_multicommand(cmds);
839 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000840 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000841 __func__, addr);
842 }
843 return result;
844}
845
Carl-Daniel Hailfinger29a1c662010-07-14 20:21:22 +0000846int spi_disable_blockprotect(struct flashchip *flash)
Sean Nelson14ba6682010-02-26 05:48:29 +0000847{
848 uint8_t status;
849 int result;
850
851 status = spi_read_status_register();
852 /* If there is block protection in effect, unprotect it first. */
853 if ((status & 0x3c) != 0) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000854 msg_cdbg("Some block protection in effect, disabling\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000855 result = spi_write_status_register(status & ~0x3c);
856 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000857 msg_cerr("spi_write_status_register failed\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000858 return result;
859 }
Carl-Daniel Hailfinger29a1c662010-07-14 20:21:22 +0000860 status = spi_read_status_register();
861 if ((status & 0x3c) != 0) {
862 msg_cerr("Block protection could not be disabled!\n");
863 return 1;
864 }
Sean Nelson14ba6682010-02-26 05:48:29 +0000865 }
866 return 0;
867}
868
869int spi_nbyte_read(int address, uint8_t *bytes, int len)
870{
871 const unsigned char cmd[JEDEC_READ_OUTSIZE] = {
872 JEDEC_READ,
873 (address >> 16) & 0xff,
874 (address >> 8) & 0xff,
875 (address >> 0) & 0xff,
876 };
877
878 /* Send Read */
879 return spi_send_command(sizeof(cmd), len, cmd, bytes);
880}
881
882/*
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000883 * Read a part of the flash chip.
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000884 * FIXME: Use the chunk code from Michael Karcher instead.
Sean Nelson14ba6682010-02-26 05:48:29 +0000885 * Each page is read separately in chunks with a maximum size of chunksize.
886 */
887int spi_read_chunked(struct flashchip *flash, uint8_t *buf, int start, int len, int chunksize)
888{
889 int rc = 0;
890 int i, j, starthere, lenhere;
891 int page_size = flash->page_size;
892 int toread;
893
894 /* Warning: This loop has a very unusual condition and body.
895 * The loop needs to go through each page with at least one affected
896 * byte. The lowest page number is (start / page_size) since that
897 * division rounds down. The highest page number we want is the page
898 * where the last byte of the range lives. That last byte has the
899 * address (start + len - 1), thus the highest page number is
900 * (start + len - 1) / page_size. Since we want to include that last
901 * page as well, the loop condition uses <=.
902 */
903 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
904 /* Byte position of the first byte in the range in this page. */
905 /* starthere is an offset to the base address of the chip. */
906 starthere = max(start, i * page_size);
907 /* Length of bytes in the range in this page. */
908 lenhere = min(start + len, (i + 1) * page_size) - starthere;
909 for (j = 0; j < lenhere; j += chunksize) {
910 toread = min(chunksize, lenhere - j);
911 rc = spi_nbyte_read(starthere + j, buf + starthere - start + j, toread);
912 if (rc)
913 break;
914 }
915 if (rc)
916 break;
917 }
918
919 return rc;
920}
921
922/*
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000923 * Write a part of the flash chip.
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000924 * FIXME: Use the chunk code from Michael Karcher instead.
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +0000925 * Each page is written separately in chunks with a maximum size of chunksize.
926 */
927int spi_write_chunked(struct flashchip *flash, uint8_t *buf, int start, int len, int chunksize)
928{
929 int rc = 0;
930 int i, j, starthere, lenhere;
931 /* FIXME: page_size is the wrong variable. We need max_writechunk_size
932 * in struct flashchip to do this properly. All chips using
933 * spi_chip_write_256 have page_size set to max_writechunk_size, so
934 * we're OK for now.
935 */
936 int page_size = flash->page_size;
937 int towrite;
938
939 /* Warning: This loop has a very unusual condition and body.
940 * The loop needs to go through each page with at least one affected
941 * byte. The lowest page number is (start / page_size) since that
942 * division rounds down. The highest page number we want is the page
943 * where the last byte of the range lives. That last byte has the
944 * address (start + len - 1), thus the highest page number is
945 * (start + len - 1) / page_size. Since we want to include that last
946 * page as well, the loop condition uses <=.
947 */
948 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
949 /* Byte position of the first byte in the range in this page. */
950 /* starthere is an offset to the base address of the chip. */
951 starthere = max(start, i * page_size);
952 /* Length of bytes in the range in this page. */
953 lenhere = min(start + len, (i + 1) * page_size) - starthere;
954 for (j = 0; j < lenhere; j += chunksize) {
955 towrite = min(chunksize, lenhere - j);
956 rc = spi_nbyte_program(starthere + j, buf + starthere - start + j, towrite);
957 if (rc)
958 break;
959 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
960 programmer_delay(10);
961 }
962 if (rc)
963 break;
964 }
965
966 return rc;
967}
968
969/*
Sean Nelson14ba6682010-02-26 05:48:29 +0000970 * Program chip using byte programming. (SLOW!)
971 * This is for chips which can only handle one byte writes
972 * and for chips where memory mapped programming is impossible
973 * (e.g. due to size constraints in IT87* for over 512 kB)
974 */
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000975/* real chunksize is 1, logical chunksize is 1 */
976int spi_chip_write_1_new(struct flashchip *flash, uint8_t *buf, int start, int len)
Sean Nelson14ba6682010-02-26 05:48:29 +0000977{
Sean Nelson14ba6682010-02-26 05:48:29 +0000978 int i, result = 0;
979
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000980 for (i = start; i < start + len; i++) {
Sean Nelson14ba6682010-02-26 05:48:29 +0000981 result = spi_byte_program(i, buf[i]);
982 if (result)
983 return 1;
984 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
985 programmer_delay(10);
986 }
987
988 return 0;
989}
990
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000991int spi_chip_write_1(struct flashchip *flash, uint8_t *buf)
Sean Nelson14ba6682010-02-26 05:48:29 +0000992{
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000993 /* Erase first */
994 msg_cinfo("Erasing flash before programming... ");
995 if (erase_flash(flash)) {
996 msg_cerr("ERASE FAILED!\n");
997 return -1;
998 }
999 msg_cinfo("done.\n");
1000
1001 return spi_chip_write_1_new(flash, buf, 0, flash->total_size * 1024);
1002}
1003
1004int spi_aai_write(struct flashchip *flash, uint8_t *buf, int start, int len)
1005{
1006 uint32_t pos = start;
Sean Nelson14ba6682010-02-26 05:48:29 +00001007 int result;
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001008 unsigned char cmd[JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE] = {
1009 JEDEC_AAI_WORD_PROGRAM,
1010 };
1011 struct spi_command cmds[] = {
1012 {
1013 .writecnt = JEDEC_WREN_OUTSIZE,
1014 .writearr = (const unsigned char[]){ JEDEC_WREN },
1015 .readcnt = 0,
1016 .readarr = NULL,
1017 }, {
1018 .writecnt = JEDEC_AAI_WORD_PROGRAM_OUTSIZE,
1019 .writearr = (const unsigned char[]){
1020 JEDEC_AAI_WORD_PROGRAM,
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001021 (start >> 16) & 0xff,
1022 (start >> 8) & 0xff,
1023 (start & 0xff),
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001024 buf[0],
1025 buf[1]
1026 },
1027 .readcnt = 0,
1028 .readarr = NULL,
1029 }, {
1030 .writecnt = 0,
1031 .writearr = NULL,
1032 .readcnt = 0,
1033 .readarr = NULL,
1034 }};
Sean Nelson14ba6682010-02-26 05:48:29 +00001035
1036 switch (spi_controller) {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +00001037#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001038#if defined(__i386__) || defined(__x86_64__)
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001039 case SPI_CONTROLLER_IT87XX:
Sean Nelson14ba6682010-02-26 05:48:29 +00001040 case SPI_CONTROLLER_WBSIO:
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001041 msg_perr("%s: impossible with this SPI controller,"
Sean Nelson14ba6682010-02-26 05:48:29 +00001042 " degrading to byte program\n", __func__);
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001043 return spi_chip_write_1_new(flash, buf, start, len);
Sean Nelson14ba6682010-02-26 05:48:29 +00001044#endif
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001045#endif
Sean Nelson14ba6682010-02-26 05:48:29 +00001046 default:
1047 break;
1048 }
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001049
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001050 /* The even start address and even length requirements can be either
1051 * honored outside this function, or we can call spi_byte_program
1052 * for the first and/or last byte and use AAI for the rest.
1053 */
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001054 /* The data sheet requires a start address with the low bit cleared. */
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001055 if (start % 2) {
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001056 msg_cerr("%s: start address not even! Please report a bug at "
1057 "flashrom@flashrom.org\n", __func__);
1058 return SPI_GENERIC_ERROR;
1059 }
1060 /* The data sheet requires total AAI write length to be even. */
1061 if (len % 2) {
1062 msg_cerr("%s: total write length not even! Please report a "
1063 "bug at flashrom@flashrom.org\n", __func__);
1064 return SPI_GENERIC_ERROR;
1065 }
1066
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001067
1068 result = spi_send_multicommand(cmds);
1069 if (result) {
1070 msg_cerr("%s failed during start command execution\n",
1071 __func__);
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001072 /* FIXME: Should we send WRDI here as well to make sure the chip
1073 * is not in AAI mode?
1074 */
Sean Nelson14ba6682010-02-26 05:48:29 +00001075 return result;
Sean Nelson14ba6682010-02-26 05:48:29 +00001076 }
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001077 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
1078 programmer_delay(10);
1079
1080 /* We already wrote 2 bytes in the multicommand step. */
1081 pos += 2;
1082
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +00001083 while (pos < start + len) {
Carl-Daniel Hailfinger9c62d112010-06-20 10:41:35 +00001084 cmd[1] = buf[pos++];
1085 cmd[2] = buf[pos++];
1086 spi_send_command(JEDEC_AAI_WORD_PROGRAM_CONT_OUTSIZE, 0, cmd, NULL);
1087 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
1088 programmer_delay(10);
1089 }
1090
1091 /* Use WRDI to exit AAI mode. */
Sean Nelson14ba6682010-02-26 05:48:29 +00001092 spi_write_disable();
1093 return 0;
1094}