blob: 54121b08112ef5a8a320ecf4f55579a81682b4dc [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;
42 printf_debug("RDID returned");
43 for (i = 0; i < bytes; i++)
44 printf_debug(" 0x%02x", readarr[i]);
45 printf_debug(". ");
46 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;
66 printf_debug("REMS returned %02x %02x. ", readarr[0], readarr[1]);
67 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;
87 printf_debug("RES returned %02x. ", readarr[0]);
88 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)
100 fprintf(stderr, "%s failed\n", __func__);
101
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]))
123 printf_debug("RDID byte 0 parity violation. ");
124
125 /* Check if this is a continuation vendor ID */
126 if (readarr[0] == 0x7f) {
127 if (!oddparity(readarr[1]))
128 printf_debug("RDID byte 1 parity violation. ");
129 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
140 printf_debug("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
141
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:
195 printf_debug("4b ID not supported on this SPI controller\n");
196 }
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
212 printf_debug("%s: id1 0x%x, id2 0x%x\n", __func__, id1, id2);
213
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];
265 printf_debug("%s: id 0x%x\n", __func__, id2);
266 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)
286 fprintf(stderr, "RDSR failed!\n");
287
288 return readarr[0];
289}
290
291/* Prettyprint the status register. Common definitions. */
292void spi_prettyprint_status_register_common(uint8_t status)
293{
294 printf_debug("Chip status register: Bit 5 / Block Protect 3 (BP3) is "
295 "%sset\n", (status & (1 << 5)) ? "" : "not ");
296 printf_debug("Chip status register: Bit 4 / Block Protect 2 (BP2) is "
297 "%sset\n", (status & (1 << 4)) ? "" : "not ");
298 printf_debug("Chip status register: Bit 3 / Block Protect 1 (BP1) is "
299 "%sset\n", (status & (1 << 3)) ? "" : "not ");
300 printf_debug("Chip status register: Bit 2 / Block Protect 0 (BP0) is "
301 "%sset\n", (status & (1 << 2)) ? "" : "not ");
302 printf_debug("Chip status register: Write Enable Latch (WEL) is "
303 "%sset\n", (status & (1 << 1)) ? "" : "not ");
304 printf_debug("Chip status register: Write In Progress (WIP/BUSY) is "
305 "%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{
314 printf_debug("Chip status register: Status Register Write Disable "
315 "(SRWD) is %sset\n", (status & (1 << 7)) ? "" : "not ");
316 printf_debug("Chip status register: Bit 6 is "
317 "%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{
323 printf_debug("Chip status register: Block Protect Write Disable "
324 "(BPL) is %sset\n", (status & (1 << 7)) ? "" : "not ");
325 printf_debug("Chip status register: Auto Address Increment Programming "
326 "(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);
345 printf_debug("Resulting block protection : %s\n",
346 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);
359 printf_debug("Resulting block protection : %s\n",
360 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();
368 printf_debug("Chip status register is %02x\n", status);
369 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) {
419 fprintf(stderr, "spi_disable_blockprotect failed\n");
420 return result;
421 }
422
423 result = spi_send_multicommand(cmds);
424 if (result) {
425 fprintf(stderr, "%s failed during command execution\n",
426 __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)) {
436 fprintf(stderr, "ERASE FAILED!\n");
437 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) {
465 fprintf(stderr, "spi_disable_blockprotect failed\n");
466 return result;
467 }
468
469 result = spi_send_multicommand(cmds);
470 if (result) {
471 fprintf(stderr, "%s failed during command execution\n", __func__);
472 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)) {
481 fprintf(stderr, "ERASE FAILED!\n");
482 return -1;
483 }
484 return 0;
485}
486
487int spi_chip_erase_60_c7(struct flashchip *flash)
488{
489 int result;
490 result = spi_chip_erase_60(flash);
491 if (result) {
492 printf_debug("spi_chip_erase_60 failed, trying c7\n");
493 result = spi_chip_erase_c7(flash);
494 }
495 return result;
496}
497
498int spi_block_erase_52(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
499{
500 int result;
501 struct spi_command cmds[] = {
502 {
503 .writecnt = JEDEC_WREN_OUTSIZE,
504 .writearr = (const unsigned char[]){ JEDEC_WREN },
505 .readcnt = 0,
506 .readarr = NULL,
507 }, {
508 .writecnt = JEDEC_BE_52_OUTSIZE,
509 .writearr = (const unsigned char[]){
510 JEDEC_BE_52,
511 (addr >> 16) & 0xff,
512 (addr >> 8) & 0xff,
513 (addr & 0xff)
514 },
515 .readcnt = 0,
516 .readarr = NULL,
517 }, {
518 .writecnt = 0,
519 .writearr = NULL,
520 .readcnt = 0,
521 .readarr = NULL,
522 }};
523
524 result = spi_send_multicommand(cmds);
525 if (result) {
526 fprintf(stderr, "%s failed during command execution at address 0x%x\n",
527 __func__, addr);
528 return result;
529 }
530 /* Wait until the Write-In-Progress bit is cleared.
531 * This usually takes 100-4000 ms, so wait in 100 ms steps.
532 */
533 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
534 programmer_delay(100 * 1000);
535 if (check_erased_range(flash, addr, blocklen)) {
536 fprintf(stderr, "ERASE FAILED!\n");
537 return -1;
538 }
539 return 0;
540}
541
542/* Block size is usually
543 * 64k for Macronix
544 * 32k for SST
545 * 4-32k non-uniform for EON
546 */
547int spi_block_erase_d8(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
548{
549 int result;
550 struct spi_command cmds[] = {
551 {
552 .writecnt = JEDEC_WREN_OUTSIZE,
553 .writearr = (const unsigned char[]){ JEDEC_WREN },
554 .readcnt = 0,
555 .readarr = NULL,
556 }, {
557 .writecnt = JEDEC_BE_D8_OUTSIZE,
558 .writearr = (const unsigned char[]){
559 JEDEC_BE_D8,
560 (addr >> 16) & 0xff,
561 (addr >> 8) & 0xff,
562 (addr & 0xff)
563 },
564 .readcnt = 0,
565 .readarr = NULL,
566 }, {
567 .writecnt = 0,
568 .writearr = NULL,
569 .readcnt = 0,
570 .readarr = NULL,
571 }};
572
573 result = spi_send_multicommand(cmds);
574 if (result) {
575 fprintf(stderr, "%s failed during command execution at address 0x%x\n",
576 __func__, addr);
577 return result;
578 }
579 /* Wait until the Write-In-Progress bit is cleared.
580 * This usually takes 100-4000 ms, so wait in 100 ms steps.
581 */
582 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
583 programmer_delay(100 * 1000);
584 if (check_erased_range(flash, addr, blocklen)) {
585 fprintf(stderr, "ERASE FAILED!\n");
586 return -1;
587 }
588 return 0;
589}
590
591/* Block size is usually
592 * 4k for PMC
593 */
594int spi_block_erase_d7(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
595{
596 int result;
597 struct spi_command cmds[] = {
598 {
599 .writecnt = JEDEC_WREN_OUTSIZE,
600 .writearr = (const unsigned char[]){ JEDEC_WREN },
601 .readcnt = 0,
602 .readarr = NULL,
603 }, {
604 .writecnt = JEDEC_BE_D7_OUTSIZE,
605 .writearr = (const unsigned char[]){
606 JEDEC_BE_D7,
607 (addr >> 16) & 0xff,
608 (addr >> 8) & 0xff,
609 (addr & 0xff)
610 },
611 .readcnt = 0,
612 .readarr = NULL,
613 }, {
614 .writecnt = 0,
615 .writearr = NULL,
616 .readcnt = 0,
617 .readarr = NULL,
618 }};
619
620 result = spi_send_multicommand(cmds);
621 if (result) {
622 fprintf(stderr, "%s failed during command execution at address 0x%x\n",
623 __func__, addr);
624 return result;
625 }
626 /* Wait until the Write-In-Progress bit is cleared.
627 * This usually takes 100-4000 ms, so wait in 100 ms steps.
628 */
629 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
630 programmer_delay(100 * 1000);
631 if (check_erased_range(flash, addr, blocklen)) {
632 fprintf(stderr, "ERASE FAILED!\n");
633 return -1;
634 }
635 return 0;
636}
637
638int spi_chip_erase_d8(struct flashchip *flash)
639{
640 int i, rc = 0;
641 int total_size = flash->total_size * 1024;
642 int erase_size = 64 * 1024;
643
644 spi_disable_blockprotect();
645
646 printf("Erasing chip: \n");
647
648 for (i = 0; i < total_size / erase_size; i++) {
649 rc = spi_block_erase_d8(flash, i * erase_size, erase_size);
650 if (rc) {
651 fprintf(stderr, "Error erasing block at 0x%x\n", i);
652 break;
653 }
654 }
655
656 printf("\n");
657
658 return rc;
659}
660
661/* Sector size is usually 4k, though Macronix eliteflash has 64k */
662int spi_block_erase_20(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
663{
664 int result;
665 struct spi_command cmds[] = {
666 {
667 .writecnt = JEDEC_WREN_OUTSIZE,
668 .writearr = (const unsigned char[]){ JEDEC_WREN },
669 .readcnt = 0,
670 .readarr = NULL,
671 }, {
672 .writecnt = JEDEC_SE_OUTSIZE,
673 .writearr = (const unsigned char[]){
674 JEDEC_SE,
675 (addr >> 16) & 0xff,
676 (addr >> 8) & 0xff,
677 (addr & 0xff)
678 },
679 .readcnt = 0,
680 .readarr = NULL,
681 }, {
682 .writecnt = 0,
683 .writearr = NULL,
684 .readcnt = 0,
685 .readarr = NULL,
686 }};
687
688 result = spi_send_multicommand(cmds);
689 if (result) {
690 fprintf(stderr, "%s failed during command execution at address 0x%x\n",
691 __func__, addr);
692 return result;
693 }
694 /* Wait until the Write-In-Progress bit is cleared.
695 * This usually takes 15-800 ms, so wait in 10 ms steps.
696 */
697 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
698 programmer_delay(10 * 1000);
699 if (check_erased_range(flash, addr, blocklen)) {
700 fprintf(stderr, "ERASE FAILED!\n");
701 return -1;
702 }
703 return 0;
704}
705
706int spi_block_erase_60(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
707{
708 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
709 fprintf(stderr, "%s called with incorrect arguments\n",
710 __func__);
711 return -1;
712 }
713 return spi_chip_erase_60(flash);
714}
715
716int spi_block_erase_c7(struct flashchip *flash, unsigned int addr, unsigned int blocklen)
717{
718 if ((addr != 0) || (blocklen != flash->total_size * 1024)) {
719 fprintf(stderr, "%s called with incorrect arguments\n",
720 __func__);
721 return -1;
722 }
723 return spi_chip_erase_c7(flash);
724}
725
726int spi_write_status_enable(void)
727{
728 const unsigned char cmd[JEDEC_EWSR_OUTSIZE] = { JEDEC_EWSR };
729 int result;
730
731 /* Send EWSR (Enable Write Status Register). */
732 result = spi_send_command(sizeof(cmd), JEDEC_EWSR_INSIZE, cmd, NULL);
733
734 if (result)
735 fprintf(stderr, "%s failed\n", __func__);
736
737 return result;
738}
739
740/*
741 * This is according the SST25VF016 datasheet, who knows it is more
742 * generic that this...
743 */
744int spi_write_status_register(int status)
745{
746 int result;
747 struct spi_command cmds[] = {
748 {
749 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
750 .writecnt = JEDEC_EWSR_OUTSIZE,
751 .writearr = (const unsigned char[]){ JEDEC_EWSR },
752 .readcnt = 0,
753 .readarr = NULL,
754 }, {
755 .writecnt = JEDEC_WRSR_OUTSIZE,
756 .writearr = (const unsigned char[]){ JEDEC_WRSR, (unsigned char) status },
757 .readcnt = 0,
758 .readarr = NULL,
759 }, {
760 .writecnt = 0,
761 .writearr = NULL,
762 .readcnt = 0,
763 .readarr = NULL,
764 }};
765
766 result = spi_send_multicommand(cmds);
767 if (result) {
768 fprintf(stderr, "%s failed during command execution\n",
769 __func__);
770 }
771 return result;
772}
773
774int spi_byte_program(int addr, uint8_t databyte)
775{
776 int result;
777 struct spi_command cmds[] = {
778 {
779 .writecnt = JEDEC_WREN_OUTSIZE,
780 .writearr = (const unsigned char[]){ JEDEC_WREN },
781 .readcnt = 0,
782 .readarr = NULL,
783 }, {
784 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE,
785 .writearr = (const unsigned char[]){
786 JEDEC_BYTE_PROGRAM,
787 (addr >> 16) & 0xff,
788 (addr >> 8) & 0xff,
789 (addr & 0xff),
790 databyte
791 },
792 .readcnt = 0,
793 .readarr = NULL,
794 }, {
795 .writecnt = 0,
796 .writearr = NULL,
797 .readcnt = 0,
798 .readarr = NULL,
799 }};
800
801 result = spi_send_multicommand(cmds);
802 if (result) {
803 fprintf(stderr, "%s failed during command execution at address 0x%x\n",
804 __func__, addr);
805 }
806 return result;
807}
808
809int spi_nbyte_program(int addr, uint8_t *bytes, int len)
810{
811 int result;
812 /* FIXME: Switch to malloc based on len unless that kills speed. */
813 unsigned char cmd[JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + 256] = {
814 JEDEC_BYTE_PROGRAM,
815 (addr >> 16) & 0xff,
816 (addr >> 8) & 0xff,
817 (addr >> 0) & 0xff,
818 };
819 struct spi_command cmds[] = {
820 {
821 .writecnt = JEDEC_WREN_OUTSIZE,
822 .writearr = (const unsigned char[]){ JEDEC_WREN },
823 .readcnt = 0,
824 .readarr = NULL,
825 }, {
826 .writecnt = JEDEC_BYTE_PROGRAM_OUTSIZE - 1 + len,
827 .writearr = cmd,
828 .readcnt = 0,
829 .readarr = NULL,
830 }, {
831 .writecnt = 0,
832 .writearr = NULL,
833 .readcnt = 0,
834 .readarr = NULL,
835 }};
836
837 if (!len) {
838 fprintf(stderr, "%s called for zero-length write\n", __func__);
839 return 1;
840 }
841 if (len > 256) {
842 fprintf(stderr, "%s called for too long a write\n", __func__);
843 return 1;
844 }
845
846 memcpy(&cmd[4], bytes, len);
847
848 result = spi_send_multicommand(cmds);
849 if (result) {
850 fprintf(stderr, "%s failed during command execution at address 0x%x\n",
851 __func__, addr);
852 }
853 return result;
854}
855
856int spi_disable_blockprotect(void)
857{
858 uint8_t status;
859 int result;
860
861 status = spi_read_status_register();
862 /* If there is block protection in effect, unprotect it first. */
863 if ((status & 0x3c) != 0) {
864 printf_debug("Some block protection in effect, disabling\n");
865 result = spi_write_status_register(status & ~0x3c);
866 if (result) {
867 fprintf(stderr, "spi_write_status_register failed\n");
868 return result;
869 }
870 }
871 return 0;
872}
873
874int spi_nbyte_read(int address, uint8_t *bytes, int len)
875{
876 const unsigned char cmd[JEDEC_READ_OUTSIZE] = {
877 JEDEC_READ,
878 (address >> 16) & 0xff,
879 (address >> 8) & 0xff,
880 (address >> 0) & 0xff,
881 };
882
883 /* Send Read */
884 return spi_send_command(sizeof(cmd), len, cmd, bytes);
885}
886
887/*
888 * Read a complete flash chip.
889 * Each page is read separately in chunks with a maximum size of chunksize.
890 */
891int spi_read_chunked(struct flashchip *flash, uint8_t *buf, int start, int len, int chunksize)
892{
893 int rc = 0;
894 int i, j, starthere, lenhere;
895 int page_size = flash->page_size;
896 int toread;
897
898 /* Warning: This loop has a very unusual condition and body.
899 * The loop needs to go through each page with at least one affected
900 * byte. The lowest page number is (start / page_size) since that
901 * division rounds down. The highest page number we want is the page
902 * where the last byte of the range lives. That last byte has the
903 * address (start + len - 1), thus the highest page number is
904 * (start + len - 1) / page_size. Since we want to include that last
905 * page as well, the loop condition uses <=.
906 */
907 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
908 /* Byte position of the first byte in the range in this page. */
909 /* starthere is an offset to the base address of the chip. */
910 starthere = max(start, i * page_size);
911 /* Length of bytes in the range in this page. */
912 lenhere = min(start + len, (i + 1) * page_size) - starthere;
913 for (j = 0; j < lenhere; j += chunksize) {
914 toread = min(chunksize, lenhere - j);
915 rc = spi_nbyte_read(starthere + j, buf + starthere - start + j, toread);
916 if (rc)
917 break;
918 }
919 if (rc)
920 break;
921 }
922
923 return rc;
924}
925
926/*
927 * Program chip using byte programming. (SLOW!)
928 * This is for chips which can only handle one byte writes
929 * and for chips where memory mapped programming is impossible
930 * (e.g. due to size constraints in IT87* for over 512 kB)
931 */
932int spi_chip_write_1(struct flashchip *flash, uint8_t *buf)
933{
934 int total_size = 1024 * flash->total_size;
935 int i, result = 0;
936
937 spi_disable_blockprotect();
938 /* Erase first */
939 printf("Erasing flash before programming... ");
940 if (erase_flash(flash)) {
941 fprintf(stderr, "ERASE FAILED!\n");
942 return -1;
943 }
944 printf("done.\n");
945 for (i = 0; i < total_size; i++) {
946 result = spi_byte_program(i, buf[i]);
947 if (result)
948 return 1;
949 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
950 programmer_delay(10);
951 }
952
953 return 0;
954}
955
956int spi_aai_write(struct flashchip *flash, uint8_t *buf)
957{
958 uint32_t pos = 2, size = flash->total_size * 1024;
959 unsigned char w[6] = {0xad, 0, 0, 0, buf[0], buf[1]};
960 int result;
961
962 switch (spi_controller) {
963#if INTERNAL_SUPPORT == 1
964 case SPI_CONTROLLER_WBSIO:
965 fprintf(stderr, "%s: impossible with Winbond SPI masters,"
966 " degrading to byte program\n", __func__);
967 return spi_chip_write_1(flash, buf);
968#endif
969 default:
970 break;
971 }
972 if (erase_flash(flash)) {
973 fprintf(stderr, "ERASE FAILED!\n");
974 return -1;
975 }
976 /* FIXME: This will fail on ICH/VIA SPI. */
977 result = spi_write_enable();
978 if (result)
979 return result;
980 spi_send_command(6, 0, w, NULL);
981 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
982 programmer_delay(5); /* SST25VF040B Tbp is max 10us */
983 while (pos < size) {
984 w[1] = buf[pos++];
985 w[2] = buf[pos++];
986 spi_send_command(3, 0, w, NULL);
987 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
988 programmer_delay(5); /* SST25VF040B Tbp is max 10us */
989 }
990 spi_write_disable();
991 return 0;
992}