blob: c6d1b6549b78bb7840dbc1cdb71ce4dc1b2d401e [file] [log] [blame]
Sean Nelson14ba6682010-02-26 05:48:29 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2007, 2008, 2009 Carl-Daniel Hailfinger
5 * 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
70static int spi_res(unsigned char *readarr)
71{
72 unsigned char cmd[JEDEC_RES_OUTSIZE] = { JEDEC_RES, 0, 0, 0 };
73 uint32_t readaddr;
74 int ret;
75
76 ret = spi_send_command(sizeof(cmd), JEDEC_RES_INSIZE, cmd, readarr);
77 if (ret == SPI_INVALID_ADDRESS) {
78 /* Find the lowest even address allowed for reads. */
79 readaddr = (spi_get_valid_read_addr() + 1) & ~1;
80 cmd[1] = (readaddr >> 16) & 0xff,
81 cmd[2] = (readaddr >> 8) & 0xff,
82 cmd[3] = (readaddr >> 0) & 0xff,
83 ret = spi_send_command(sizeof(cmd), JEDEC_RES_INSIZE, cmd, readarr);
84 }
85 if (ret)
86 return ret;
Sean Nelsoned479d22010-03-24 23:14:32 +000087 msg_cspew("RES returned %02x. ", readarr[0]);
Sean Nelson14ba6682010-02-26 05:48:29 +000088 return 0;
89}
90
91int spi_write_enable(void)
92{
93 const unsigned char cmd[JEDEC_WREN_OUTSIZE] = { JEDEC_WREN };
94 int result;
95
96 /* Send WREN (Write Enable) */
97 result = spi_send_command(sizeof(cmd), 0, cmd, NULL);
98
99 if (result)
Sean Nelsoned479d22010-03-24 23:14:32 +0000100 msg_cerr("%s failed\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000101
102 return result;
103}
104
105int spi_write_disable(void)
106{
107 const unsigned char cmd[JEDEC_WRDI_OUTSIZE] = { JEDEC_WRDI };
108
109 /* Send WRDI (Write Disable) */
110 return spi_send_command(sizeof(cmd), 0, cmd, NULL);
111}
112
113static int probe_spi_rdid_generic(struct flashchip *flash, int bytes)
114{
115 unsigned char readarr[4];
116 uint32_t id1;
117 uint32_t id2;
118
119 if (spi_rdid(readarr, bytes))
120 return 0;
121
122 if (!oddparity(readarr[0]))
Sean Nelsoned479d22010-03-24 23:14:32 +0000123 msg_cdbg("RDID byte 0 parity violation. ");
Sean Nelson14ba6682010-02-26 05:48:29 +0000124
125 /* Check if this is a continuation vendor ID */
126 if (readarr[0] == 0x7f) {
127 if (!oddparity(readarr[1]))
Sean Nelsoned479d22010-03-24 23:14:32 +0000128 msg_cdbg("RDID byte 1 parity violation. ");
Sean Nelson14ba6682010-02-26 05:48:29 +0000129 id1 = (readarr[0] << 8) | readarr[1];
130 id2 = readarr[2];
131 if (bytes > 3) {
132 id2 <<= 8;
133 id2 |= readarr[3];
134 }
135 } else {
136 id1 = readarr[0];
137 id2 = (readarr[1] << 8) | readarr[2];
138 }
139
Sean Nelsoned479d22010-03-24 23:14:32 +0000140 msg_cdbg("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
Sean Nelson14ba6682010-02-26 05:48:29 +0000141
142 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
143 /* Print the status register to tell the
144 * user about possible write protection.
145 */
146 spi_prettyprint_status_register(flash);
147
148 return 1;
149 }
150
151 /* Test if this is a pure vendor match. */
152 if (id1 == flash->manufacture_id &&
153 GENERIC_DEVICE_ID == flash->model_id)
154 return 1;
155
156 /* Test if there is any vendor ID. */
157 if (GENERIC_MANUF_ID == flash->manufacture_id &&
158 id1 != 0xff)
159 return 1;
160
161 return 0;
162}
163
164int probe_spi_rdid(struct flashchip *flash)
165{
166 return probe_spi_rdid_generic(flash, 3);
167}
168
169/* support 4 bytes flash ID */
170int probe_spi_rdid4(struct flashchip *flash)
171{
172 /* only some SPI chipsets support 4 bytes commands */
173 switch (spi_controller) {
174#if INTERNAL_SUPPORT == 1
175 case SPI_CONTROLLER_ICH7:
176 case SPI_CONTROLLER_ICH9:
177 case SPI_CONTROLLER_VIA:
178 case SPI_CONTROLLER_SB600:
179 case SPI_CONTROLLER_WBSIO:
180#endif
181#if FT2232_SPI_SUPPORT == 1
182 case SPI_CONTROLLER_FT2232:
183#endif
184#if DUMMY_SUPPORT == 1
185 case SPI_CONTROLLER_DUMMY:
186#endif
187#if BUSPIRATE_SPI_SUPPORT == 1
188 case SPI_CONTROLLER_BUSPIRATE:
189#endif
190#if DEDIPROG_SUPPORT == 1
191 case SPI_CONTROLLER_DEDIPROG:
192#endif
193 return probe_spi_rdid_generic(flash, 4);
194 default:
Sean Nelsoned479d22010-03-24 23:14:32 +0000195 msg_cinfo("4b ID not supported on this SPI controller\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000196 }
197
198 return 0;
199}
200
201int probe_spi_rems(struct flashchip *flash)
202{
203 unsigned char readarr[JEDEC_REMS_INSIZE];
204 uint32_t id1, id2;
205
206 if (spi_rems(readarr))
207 return 0;
208
209 id1 = readarr[0];
210 id2 = readarr[1];
211
Sean Nelsoned479d22010-03-24 23:14:32 +0000212 msg_cdbg("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
Sean Nelson14ba6682010-02-26 05:48:29 +0000213
214 if (id1 == flash->manufacture_id && id2 == flash->model_id) {
215 /* Print the status register to tell the
216 * user about possible write protection.
217 */
218 spi_prettyprint_status_register(flash);
219
220 return 1;
221 }
222
223 /* Test if this is a pure vendor match. */
224 if (id1 == flash->manufacture_id &&
225 GENERIC_DEVICE_ID == flash->model_id)
226 return 1;
227
228 /* Test if there is any vendor ID. */
229 if (GENERIC_MANUF_ID == flash->manufacture_id &&
230 id1 != 0xff)
231 return 1;
232
233 return 0;
234}
235
236int probe_spi_res(struct flashchip *flash)
237{
238 unsigned char readarr[3];
239 uint32_t id2;
240 const unsigned char allff[] = {0xff, 0xff, 0xff};
241 const unsigned char all00[] = {0x00, 0x00, 0x00};
242
243 /* Check if RDID is usable and does not return 0xff 0xff 0xff or
244 * 0x00 0x00 0x00. In that case, RES is pointless.
245 */
246 if (!spi_rdid(readarr, 3) && memcmp(readarr, allff, 3) &&
247 memcmp(readarr, all00, 3)) {
248 msg_cdbg("Ignoring RES in favour of RDID.\n");
249 return 0;
250 }
251 /* Check if REMS is usable and does not return 0xff 0xff or
252 * 0x00 0x00. In that case, RES is pointless.
253 */
254 if (!spi_rems(readarr) && memcmp(readarr, allff, JEDEC_REMS_INSIZE) &&
255 memcmp(readarr, all00, JEDEC_REMS_INSIZE)) {
256 msg_cdbg("Ignoring RES in favour of REMS.\n");
257 return 0;
258 }
259
260 if (spi_res(readarr))
261 return 0;
262
263 /* FIXME: Handle the case where RES gives a 2-byte response. */
264 id2 = readarr[0];
Sean Nelsoned479d22010-03-24 23:14:32 +0000265 msg_cdbg("%s: id 0x%x\n", __func__, id2);
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
276uint8_t spi_read_status_register(void)
277{
278 const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { JEDEC_RDSR };
279 /* FIXME: No workarounds for driver/hardware bugs in generic code. */
280 unsigned char readarr[2]; /* JEDEC_RDSR_INSIZE=1 but wbsio needs 2 */
281 int ret;
282
283 /* Read Status Register */
284 ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
285 if (ret)
Sean Nelsoned479d22010-03-24 23:14:32 +0000286 msg_cerr("RDSR failed!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000287
288 return readarr[0];
289}
290
291/* Prettyprint the status register. Common definitions. */
292void spi_prettyprint_status_register_common(uint8_t status)
293{
Sean Nelsoned479d22010-03-24 23:14:32 +0000294 msg_cdbg("Chip status register: Bit 5 / Block Protect 3 (BP3) is "
Sean Nelson14ba6682010-02-26 05:48:29 +0000295 "%sset\n", (status & (1 << 5)) ? "" : "not ");
Sean Nelsoned479d22010-03-24 23:14:32 +0000296 msg_cdbg("Chip status register: Bit 4 / Block Protect 2 (BP2) is "
Sean Nelson14ba6682010-02-26 05:48:29 +0000297 "%sset\n", (status & (1 << 4)) ? "" : "not ");
Sean Nelsoned479d22010-03-24 23:14:32 +0000298 msg_cdbg("Chip status register: Bit 3 / Block Protect 1 (BP1) is "
Sean Nelson14ba6682010-02-26 05:48:29 +0000299 "%sset\n", (status & (1 << 3)) ? "" : "not ");
Sean Nelsoned479d22010-03-24 23:14:32 +0000300 msg_cdbg("Chip status register: Bit 2 / Block Protect 0 (BP0) is "
Sean Nelson14ba6682010-02-26 05:48:29 +0000301 "%sset\n", (status & (1 << 2)) ? "" : "not ");
Sean Nelsoned479d22010-03-24 23:14:32 +0000302 msg_cdbg("Chip status register: Write Enable Latch (WEL) is "
Sean Nelson14ba6682010-02-26 05:48:29 +0000303 "%sset\n", (status & (1 << 1)) ? "" : "not ");
Sean Nelsoned479d22010-03-24 23:14:32 +0000304 msg_cdbg("Chip status register: Write In Progress (WIP/BUSY) is "
Sean Nelson14ba6682010-02-26 05:48:29 +0000305 "%sset\n", (status & (1 << 0)) ? "" : "not ");
306}
307
308/* Prettyprint the status register. Works for
309 * ST M25P series
310 * MX MX25L series
311 */
312void spi_prettyprint_status_register_st_m25p(uint8_t status)
313{
Sean Nelsoned479d22010-03-24 23:14:32 +0000314 msg_cdbg("Chip status register: Status Register Write Disable "
Sean Nelson14ba6682010-02-26 05:48:29 +0000315 "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not ");
Sean Nelsoned479d22010-03-24 23:14:32 +0000316 msg_cdbg("Chip status register: Bit 6 is "
Sean Nelson14ba6682010-02-26 05:48:29 +0000317 "%sset\n", (status & (1 << 6)) ? "" : "not ");
318 spi_prettyprint_status_register_common(status);
319}
320
321void spi_prettyprint_status_register_sst25(uint8_t status)
322{
Sean Nelsoned479d22010-03-24 23:14:32 +0000323 msg_cdbg("Chip status register: Block Protect Write Disable "
Sean Nelson14ba6682010-02-26 05:48:29 +0000324 "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not ");
Sean Nelsoned479d22010-03-24 23:14:32 +0000325 msg_cdbg("Chip status register: Auto Address Increment Programming "
Sean Nelson14ba6682010-02-26 05:48:29 +0000326 "(AAI) is %sset\n", (status & (1 << 6)) ? "" : "not ");
327 spi_prettyprint_status_register_common(status);
328}
329
330/* Prettyprint the status register. Works for
331 * SST 25VF016
332 */
333void spi_prettyprint_status_register_sst25vf016(uint8_t status)
334{
335 const char *bpt[] = {
336 "none",
337 "1F0000H-1FFFFFH",
338 "1E0000H-1FFFFFH",
339 "1C0000H-1FFFFFH",
340 "180000H-1FFFFFH",
341 "100000H-1FFFFFH",
342 "all", "all"
343 };
344 spi_prettyprint_status_register_sst25(status);
Sean Nelsoned479d22010-03-24 23:14:32 +0000345 msg_cdbg("Resulting block protection : %s\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000346 bpt[(status & 0x1c) >> 2]);
347}
348
349void spi_prettyprint_status_register_sst25vf040b(uint8_t status)
350{
351 const char *bpt[] = {
352 "none",
353 "0x70000-0x7ffff",
354 "0x60000-0x7ffff",
355 "0x40000-0x7ffff",
356 "all blocks", "all blocks", "all blocks", "all blocks"
357 };
358 spi_prettyprint_status_register_sst25(status);
Sean Nelsoned479d22010-03-24 23:14:32 +0000359 msg_cdbg("Resulting block protection : %s\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000360 bpt[(status & 0x1c) >> 2]);
361}
362
363void spi_prettyprint_status_register(struct flashchip *flash)
364{
365 uint8_t status;
366
367 status = spi_read_status_register();
Sean Nelsoned479d22010-03-24 23:14:32 +0000368 msg_cdbg("Chip status register is %02x\n", status);
Sean Nelson14ba6682010-02-26 05:48:29 +0000369 switch (flash->manufacture_id) {
370 case ST_ID:
371 if (((flash->model_id & 0xff00) == 0x2000) ||
372 ((flash->model_id & 0xff00) == 0x2500))
373 spi_prettyprint_status_register_st_m25p(status);
374 break;
375 case MX_ID:
376 if ((flash->model_id & 0xff00) == 0x2000)
377 spi_prettyprint_status_register_st_m25p(status);
378 break;
379 case SST_ID:
380 switch (flash->model_id) {
381 case 0x2541:
382 spi_prettyprint_status_register_sst25vf016(status);
383 break;
384 case 0x8d:
385 case 0x258d:
386 spi_prettyprint_status_register_sst25vf040b(status);
387 break;
388 default:
389 spi_prettyprint_status_register_sst25(status);
390 break;
391 }
392 break;
393 }
394}
395
396int spi_chip_erase_60(struct flashchip *flash)
397{
398 int result;
399 struct spi_command cmds[] = {
400 {
401 .writecnt = JEDEC_WREN_OUTSIZE,
402 .writearr = (const unsigned char[]){ JEDEC_WREN },
403 .readcnt = 0,
404 .readarr = NULL,
405 }, {
406 .writecnt = JEDEC_CE_60_OUTSIZE,
407 .writearr = (const unsigned char[]){ JEDEC_CE_60 },
408 .readcnt = 0,
409 .readarr = NULL,
410 }, {
411 .writecnt = 0,
412 .writearr = NULL,
413 .readcnt = 0,
414 .readarr = NULL,
415 }};
416
417 result = spi_disable_blockprotect();
418 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000419 msg_cerr("spi_disable_blockprotect failed\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000420 return result;
421 }
422
423 result = spi_send_multicommand(cmds);
424 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000425 msg_cerr("%s failed during command execution\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000426 __func__);
427 return result;
428 }
429 /* Wait until the Write-In-Progress bit is cleared.
430 * This usually takes 1-85 s, so wait in 1 s steps.
431 */
432 /* FIXME: We assume spi_read_status_register will never fail. */
433 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
434 programmer_delay(1000 * 1000);
435 if (check_erased_range(flash, 0, flash->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000436 msg_cerr("ERASE FAILED!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000437 return -1;
438 }
439 return 0;
440}
441
442int spi_chip_erase_c7(struct flashchip *flash)
443{
444 int result;
445 struct spi_command cmds[] = {
446 {
447 .writecnt = JEDEC_WREN_OUTSIZE,
448 .writearr = (const unsigned char[]){ JEDEC_WREN },
449 .readcnt = 0,
450 .readarr = NULL,
451 }, {
452 .writecnt = JEDEC_CE_C7_OUTSIZE,
453 .writearr = (const unsigned char[]){ JEDEC_CE_C7 },
454 .readcnt = 0,
455 .readarr = NULL,
456 }, {
457 .writecnt = 0,
458 .writearr = NULL,
459 .readcnt = 0,
460 .readarr = NULL,
461 }};
462
463 result = spi_disable_blockprotect();
464 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000465 msg_cerr("spi_disable_blockprotect failed\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000466 return result;
467 }
468
469 result = spi_send_multicommand(cmds);
470 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000471 msg_cerr("%s failed during command execution\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000472 return result;
473 }
474 /* Wait until the Write-In-Progress bit is cleared.
475 * This usually takes 1-85 s, so wait in 1 s steps.
476 */
477 /* FIXME: We assume spi_read_status_register will never fail. */
478 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
479 programmer_delay(1000 * 1000);
480 if (check_erased_range(flash, 0, flash->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000481 msg_cerr("ERASE FAILED!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000482 return -1;
483 }
484 return 0;
485}
486
Sean Nelson14ba6682010-02-26 05:48:29 +0000487int spi_block_erase_52(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
488{
489 int result;
490 struct spi_command cmds[] = {
491 {
492 .writecnt = JEDEC_WREN_OUTSIZE,
493 .writearr = (const unsigned char[]){ JEDEC_WREN },
494 .readcnt = 0,
495 .readarr = NULL,
496 }, {
497 .writecnt = JEDEC_BE_52_OUTSIZE,
498 .writearr = (const unsigned char[]){
499 JEDEC_BE_52,
500 (addr >> 16) & 0xff,
501 (addr >> 8) & 0xff,
502 (addr & 0xff)
503 },
504 .readcnt = 0,
505 .readarr = NULL,
506 }, {
507 .writecnt = 0,
508 .writearr = NULL,
509 .readcnt = 0,
510 .readarr = NULL,
511 }};
512
513 result = spi_send_multicommand(cmds);
514 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000515 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000516 __func__, addr);
517 return result;
518 }
519 /* Wait until the Write-In-Progress bit is cleared.
520 * This usually takes 100-4000 ms, so wait in 100 ms steps.
521 */
522 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
523 programmer_delay(100 * 1000);
524 if (check_erased_range(flash, addr, blocklen)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000525 msg_cerr("ERASE FAILED!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000526 return -1;
527 }
528 return 0;
529}
530
531/* Block size is usually
532 * 64k for Macronix
533 * 32k for SST
534 * 4-32k non-uniform for EON
535 */
536int spi_block_erase_d8(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
537{
538 int result;
539 struct spi_command cmds[] = {
540 {
541 .writecnt = JEDEC_WREN_OUTSIZE,
542 .writearr = (const unsigned char[]){ JEDEC_WREN },
543 .readcnt = 0,
544 .readarr = NULL,
545 }, {
546 .writecnt = JEDEC_BE_D8_OUTSIZE,
547 .writearr = (const unsigned char[]){
548 JEDEC_BE_D8,
549 (addr >> 16) & 0xff,
550 (addr >> 8) & 0xff,
551 (addr & 0xff)
552 },
553 .readcnt = 0,
554 .readarr = NULL,
555 }, {
556 .writecnt = 0,
557 .writearr = NULL,
558 .readcnt = 0,
559 .readarr = NULL,
560 }};
561
562 result = spi_send_multicommand(cmds);
563 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000564 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000565 __func__, addr);
566 return result;
567 }
568 /* Wait until the Write-In-Progress bit is cleared.
569 * This usually takes 100-4000 ms, so wait in 100 ms steps.
570 */
571 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
572 programmer_delay(100 * 1000);
573 if (check_erased_range(flash, addr, blocklen)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000574 msg_cerr("ERASE FAILED!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000575 return -1;
576 }
577 return 0;
578}
579
580/* Block size is usually
581 * 4k for PMC
582 */
583int spi_block_erase_d7(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
584{
585 int result;
586 struct spi_command cmds[] = {
587 {
588 .writecnt = JEDEC_WREN_OUTSIZE,
589 .writearr = (const unsigned char[]){ JEDEC_WREN },
590 .readcnt = 0,
591 .readarr = NULL,
592 }, {
593 .writecnt = JEDEC_BE_D7_OUTSIZE,
594 .writearr = (const unsigned char[]){
595 JEDEC_BE_D7,
596 (addr >> 16) & 0xff,
597 (addr >> 8) & 0xff,
598 (addr & 0xff)
599 },
600 .readcnt = 0,
601 .readarr = NULL,
602 }, {
603 .writecnt = 0,
604 .writearr = NULL,
605 .readcnt = 0,
606 .readarr = NULL,
607 }};
608
609 result = spi_send_multicommand(cmds);
610 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000611 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000612 __func__, addr);
613 return result;
614 }
615 /* Wait until the Write-In-Progress bit is cleared.
616 * This usually takes 100-4000 ms, so wait in 100 ms steps.
617 */
618 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
619 programmer_delay(100 * 1000);
620 if (check_erased_range(flash, addr, blocklen)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000621 msg_cerr("ERASE FAILED!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000622 return -1;
623 }
624 return 0;
625}
626
627int spi_chip_erase_d8(struct flashchip *flash)
628{
629 int i, rc = 0;
630 int total_size = flash->total_size * 1024;
631 int erase_size = 64 * 1024;
632
633 spi_disable_blockprotect();
634
Sean Nelsoned479d22010-03-24 23:14:32 +0000635 msg_cinfo("Erasing chip: \n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000636
637 for (i = 0; i < total_size / erase_size; i++) {
638 rc = spi_block_erase_d8(flash, i * erase_size, erase_size);
639 if (rc) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000640 msg_cerr("Error erasing block at 0x%x\n", i);
Sean Nelson14ba6682010-02-26 05:48:29 +0000641 break;
642 }
643 }
644
Sean Nelsoned479d22010-03-24 23:14:32 +0000645 msg_cinfo("\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000646
647 return rc;
648}
649
650/* Sector size is usually 4k, though Macronix eliteflash has 64k */
651int spi_block_erase_20(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
652{
653 int result;
654 struct spi_command cmds[] = {
655 {
656 .writecnt = JEDEC_WREN_OUTSIZE,
657 .writearr = (const unsigned char[]){ JEDEC_WREN },
658 .readcnt = 0,
659 .readarr = NULL,
660 }, {
661 .writecnt = JEDEC_SE_OUTSIZE,
662 .writearr = (const unsigned char[]){
663 JEDEC_SE,
664 (addr >> 16) & 0xff,
665 (addr >> 8) & 0xff,
666 (addr & 0xff)
667 },
668 .readcnt = 0,
669 .readarr = NULL,
670 }, {
671 .writecnt = 0,
672 .writearr = NULL,
673 .readcnt = 0,
674 .readarr = NULL,
675 }};
676
677 result = spi_send_multicommand(cmds);
678 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000679 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000680 __func__, addr);
681 return result;
682 }
683 /* Wait until the Write-In-Progress bit is cleared.
684 * This usually takes 15-800 ms, so wait in 10 ms steps.
685 */
686 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
687 programmer_delay(10 * 1000);
688 if (check_erased_range(flash, addr, blocklen)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000689 msg_cerr("ERASE FAILED!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000690 return -1;
691 }
692 return 0;
693}
694
695int spi_block_erase_60(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
696{
697 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000698 msg_cerr("%s called with incorrect arguments\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000699 __func__);
700 return -1;
701 }
702 return spi_chip_erase_60(flash);
703}
704
705int spi_block_erase_c7(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
706{
707 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000708 msg_cerr("%s called with incorrect arguments\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000709 __func__);
710 return -1;
711 }
712 return spi_chip_erase_c7(flash);
713}
714
715int spi_write_status_enable(void)
716{
717 const unsigned char cmd[JEDEC_EWSR_OUTSIZE] = { JEDEC_EWSR };
718 int result;
719
720 /* Send EWSR (Enable Write Status Register). */
721 result = spi_send_command(sizeof(cmd), JEDEC_EWSR_INSIZE, cmd, NULL);
722
723 if (result)
Sean Nelsoned479d22010-03-24 23:14:32 +0000724 msg_cerr("%s failed\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000725
726 return result;
727}
728
729/*
730 * This is according the SST25VF016 datasheet, who knows it is more
731 * generic that this...
732 */
733int spi_write_status_register(int status)
734{
735 int result;
736 struct spi_command cmds[] = {
737 {
738 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
739 .writecnt = JEDEC_EWSR_OUTSIZE,
740 .writearr = (const unsigned char[]){ JEDEC_EWSR },
741 .readcnt = 0,
742 .readarr = NULL,
743 }, {
744 .writecnt = JEDEC_WRSR_OUTSIZE,
745 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
746 .readcnt = 0,
747 .readarr = NULL,
748 }, {
749 .writecnt = 0,
750 .writearr = NULL,
751 .readcnt = 0,
752 .readarr = NULL,
753 }};
754
755 result = spi_send_multicommand(cmds);
756 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000757 msg_cerr("%s failed during command execution\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000758 __func__);
759 }
760 return result;
761}
762
763int spi_byte_program(int addr, uint8_t databyte)
764{
765 int result;
766 struct spi_command cmds[] = {
767 {
768 .writecnt = JEDEC_WREN_OUTSIZE,
769 .writearr = (const unsigned char[]){ JEDEC_WREN },
770 .readcnt = 0,
771 .readarr = NULL,
772 }, {
773 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE,
774 .writearr = (const unsigned char[]){
775 JEDEC_BYTE_PROGRAM,
776 (addr >> 16) & 0xff,
777 (addr >> 8) & 0xff,
778 (addr & 0xff),
779 databyte
780 },
781 .readcnt = 0,
782 .readarr = NULL,
783 }, {
784 .writecnt = 0,
785 .writearr = NULL,
786 .readcnt = 0,
787 .readarr = NULL,
788 }};
789
790 result = spi_send_multicommand(cmds);
791 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000792 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000793 __func__, addr);
794 }
795 return result;
796}
797
798int spi_nbyte_program(int addr, uint8_t *bytes, int len)
799{
800 int result;
801 /* FIXME: Switch to malloc based on len unless that kills speed. */
802 unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + 256] = {
803 JEDEC_BYTE_PROGRAM,
804 (addr >> 16) & 0xff,
805 (addr >> 8) & 0xff,
806 (addr >> 0) & 0xff,
807 };
808 struct spi_command cmds[] = {
809 {
810 .writecnt = JEDEC_WREN_OUTSIZE,
811 .writearr = (const unsigned char[]){ JEDEC_WREN },
812 .readcnt = 0,
813 .readarr = NULL,
814 }, {
815 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + len,
816 .writearr = cmd,
817 .readcnt = 0,
818 .readarr = NULL,
819 }, {
820 .writecnt = 0,
821 .writearr = NULL,
822 .readcnt = 0,
823 .readarr = NULL,
824 }};
825
826 if (!len) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000827 msg_cerr("%s called for zero-length write\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000828 return 1;
829 }
830 if (len > 256) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000831 msg_cerr("%s called for too long a write\n", __func__);
Sean Nelson14ba6682010-02-26 05:48:29 +0000832 return 1;
833 }
834
835 memcpy(&cmd[4], bytes, len);
836
837 result = spi_send_multicommand(cmds);
838 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000839 msg_cerr("%s failed during command execution at address 0x%x\n",
Sean Nelson14ba6682010-02-26 05:48:29 +0000840 __func__, addr);
841 }
842 return result;
843}
844
845int spi_disable_blockprotect(void)
846{
847 uint8_t status;
848 int result;
849
850 status = spi_read_status_register();
851 /* If there is block protection in effect, unprotect it first. */
852 if ((status & 0x3c) != 0) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000853 msg_cdbg("Some block protection in effect, disabling\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000854 result = spi_write_status_register(status & ~0x3c);
855 if (result) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000856 msg_cerr("spi_write_status_register failed\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000857 return result;
858 }
859 }
860 return 0;
861}
862
863int spi_nbyte_read(int address, uint8_t *bytes, int len)
864{
865 const unsigned char cmd[JEDEC_READ_OUTSIZE] = {
866 JEDEC_READ,
867 (address >> 16) & 0xff,
868 (address >> 8) & 0xff,
869 (address >> 0) & 0xff,
870 };
871
872 /* Send Read */
873 return spi_send_command(sizeof(cmd), len, cmd, bytes);
874}
875
876/*
877 * Read a complete flash chip.
878 * Each page is read separately in chunks with a maximum size of chunksize.
879 */
880int spi_read_chunked(struct flashchip *flash, uint8_t *buf, int start, int len, int chunksize)
881{
882 int rc = 0;
883 int i, j, starthere, lenhere;
884 int page_size = flash->page_size;
885 int toread;
886
887 /* Warning: This loop has a very unusual condition and body.
888 * The loop needs to go through each page with at least one affected
889 * byte. The lowest page number is (start / page_size) since that
890 * division rounds down. The highest page number we want is the page
891 * where the last byte of the range lives. That last byte has the
892 * address (start + len - 1), thus the highest page number is
893 * (start + len - 1) / page_size. Since we want to include that last
894 * page as well, the loop condition uses <=.
895 */
896 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
897 /* Byte position of the first byte in the range in this page. */
898 /* starthere is an offset to the base address of the chip. */
899 starthere = max(start, i * page_size);
900 /* Length of bytes in the range in this page. */
901 lenhere = min(start + len, (i + 1) * page_size) - starthere;
902 for (j = 0; j < lenhere; j += chunksize) {
903 toread = min(chunksize, lenhere - j);
904 rc = spi_nbyte_read(starthere + j, buf + starthere - start + j, toread);
905 if (rc)
906 break;
907 }
908 if (rc)
909 break;
910 }
911
912 return rc;
913}
914
915/*
916 * Program chip using byte programming. (SLOW!)
917 * This is for chips which can only handle one byte writes
918 * and for chips where memory mapped programming is impossible
919 * (e.g. due to size constraints in IT87* for over 512 kB)
920 */
921int spi_chip_write_1(struct flashchip *flash, uint8_t *buf)
922{
923 int total_size = 1024 * flash->total_size;
924 int i, result = 0;
925
926 spi_disable_blockprotect();
927 /* Erase first */
Sean Nelsoned479d22010-03-24 23:14:32 +0000928 msg_cinfo("Erasing flash before programming... ");
Sean Nelson14ba6682010-02-26 05:48:29 +0000929 if (erase_flash(flash)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000930 msg_cerr("ERASE FAILED!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000931 return -1;
932 }
Sean Nelsoned479d22010-03-24 23:14:32 +0000933 msg_cinfo("done.\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000934 for (i = 0; i < total_size; i++) {
935 result = spi_byte_program(i, buf[i]);
936 if (result)
937 return 1;
938 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
939 programmer_delay(10);
940 }
941
942 return 0;
943}
944
945int spi_aai_write(struct flashchip *flash, uint8_t *buf)
946{
947 uint32_t pos = 2, size = flash->total_size * 1024;
948 unsigned char w[6] = {0xad, 0, 0, 0, buf[0], buf[1]};
949 int result;
950
951 switch (spi_controller) {
952#if INTERNAL_SUPPORT == 1
953 case SPI_CONTROLLER_WBSIO:
Sean Nelsoned479d22010-03-24 23:14:32 +0000954 msg_cerr("%s: impossible with Winbond SPI masters,"
Sean Nelson14ba6682010-02-26 05:48:29 +0000955 " degrading to byte program\n", __func__);
956 return spi_chip_write_1(flash, buf);
957#endif
958 default:
959 break;
960 }
961 if (erase_flash(flash)) {
Sean Nelsoned479d22010-03-24 23:14:32 +0000962 msg_cerr("ERASE FAILED!\n");
Sean Nelson14ba6682010-02-26 05:48:29 +0000963 return -1;
964 }
965 /* FIXME: This will fail on ICH/VIA SPI. */
966 result = spi_write_enable();
967 if (result)
968 return result;
969 spi_send_command(6, 0, w, NULL);
970 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
971 programmer_delay(5); /* SST25VF040B Tbp is max 10us */
972 while (pos < size) {
973 w[1] = buf[pos++];
974 w[2] = buf[pos++];
975 spi_send_command(3, 0, w, NULL);
976 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
977 programmer_delay(5); /* SST25VF040B Tbp is max 10us */
978 }
979 spi_write_disable();
980 return 0;
981}