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