3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * (C) Copyright 2002 Jun Gu <jung@artesyncp.com>
6 * Add support for Am29F016D and dynamic switch setting.
8 * SPDX-License-Identifier: GPL-2.0+
13 * Wait for completion of each sector erase command issued
15 * Chris Hallinan - DS4.COM, Inc. - clh@net1plus.com
19 #include <asm/ppc4xx.h>
20 #include <asm/processor.h>
21 #include <asm/ppc440.h>
25 #define DEBUGF(x...) printf(x)
30 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
33 * Mark big flash bank (16 bit instead of 8 bit access) in address with bit 0
35 static unsigned long flash_addr_table[][CONFIG_SYS_MAX_FLASH_BANKS] = {
36 {0xfff00000, 0xfff80000, 0xe7c00001}, /* 0:boot from small flash */
37 {0x00000000, 0x00000000, 0x00000000}, /* 1:boot from pci 66 */
38 {0x00000000, 0x00000000, 0x00000000}, /* 2:boot from nand flash */
39 {0xe7F00000, 0xe7F80000, 0xFFC00001}, /* 3:boot from big flash 33*/
40 {0xe7F00000, 0xe7F80000, 0xFFC00001}, /* 4:boot from big flash 66*/
41 {0x00000000, 0x00000000, 0x00000000}, /* 5:boot from */
42 {0x00000000, 0x00000000, 0x00000000}, /* 6:boot from pci 66 */
43 {0x00000000, 0x00000000, 0x00000000}, /* 7:boot from */
44 {0xfff00000, 0xfff80000, 0xe7c00001}, /* 8:boot from small flash */
48 * include common flash code (for amcc boards)
50 /*-----------------------------------------------------------------------
53 static int write_word(flash_info_t * info, ulong dest, ulong data);
54 #ifdef CONFIG_SYS_FLASH_2ND_16BIT_DEV
55 static int write_word_1(flash_info_t * info, ulong dest, ulong data);
56 static int write_word_2(flash_info_t * info, ulong dest, ulong data);
57 static int flash_erase_1(flash_info_t * info, int s_first, int s_last);
58 static int flash_erase_2(flash_info_t * info, int s_first, int s_last);
59 static ulong flash_get_size_1(vu_long * addr, flash_info_t * info);
60 static ulong flash_get_size_2(vu_long * addr, flash_info_t * info);
63 void flash_print_info(flash_info_t * info)
69 volatile unsigned long *flash;
71 if (info->flash_id == FLASH_UNKNOWN) {
72 printf("missing or unknown FLASH type\n");
76 switch (info->flash_id & FLASH_VENDMASK) {
93 printf("Unknown Vendor ");
97 switch (info->flash_id & FLASH_TYPEMASK) {
99 printf("AM29F040 (512 Kbit, uniform sector size)\n");
102 printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
105 printf("AM29LV400T (4 Mbit, top boot sector)\n");
108 printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
111 printf("AM29LV800T (8 Mbit, top boot sector)\n");
114 printf("AM29F016D (16 Mbit, uniform sector size)\n");
117 printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
120 printf("AM29LV160T (16 Mbit, top boot sector)\n");
123 printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
126 printf("AM29LV320T (32 Mbit, top boot sector)\n");
129 printf("AM29LV033C (32 Mbit, top boot sector)\n");
132 printf("SST39LF/VF800 (8 Mbit, uniform sector size)\n");
135 printf("SST39LF/VF160 (16 Mbit, uniform sector size)\n");
137 case FLASH_STMW320DT:
138 printf ("M29W320DT (32 M, top sector)\n");
141 printf ("MXLV320T (32 Mbit, top sector)\n");
144 printf("Unknown Chip Type\n");
148 printf(" Size: %ld KB in %d Sectors\n",
149 info->size >> 10, info->sector_count);
151 printf(" Sector Start Addresses:");
152 for (i = 0; i < info->sector_count; ++i) {
154 * Check if whole sector is erased
156 if (i != (info->sector_count - 1))
157 size = info->start[i + 1] - info->start[i];
159 size = info->start[0] + info->size - info->start[i];
161 flash = (volatile unsigned long *)info->start[i];
162 size = size >> 2; /* divide by 4 for longword access */
163 for (k = 0; k < size; k++) {
164 if (*flash++ != 0xffffffff) {
175 info->protect[i] ? "RO " : " ");
183 * The following code cannot be run from FLASH!
185 #ifdef CONFIG_SYS_FLASH_2ND_16BIT_DEV
186 static ulong flash_get_size(vu_long * addr, flash_info_t * info)
188 /* bit 0 used for big flash marking */
189 if ((ulong)addr & 0x1)
190 return flash_get_size_2((vu_long *)((ulong)addr & 0xfffffffe), info);
192 return flash_get_size_1(addr, info);
195 static ulong flash_get_size_1(vu_long * addr, flash_info_t * info)
197 static ulong flash_get_size(vu_long * addr, flash_info_t * info)
201 CONFIG_SYS_FLASH_WORD_SIZE value;
202 ulong base = (ulong) addr;
203 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) addr;
205 DEBUGF("FLASH ADDR: %08x\n", (unsigned)addr);
207 /* Write auto select command: read Manufacturer ID */
208 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
209 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
210 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00900090;
214 DEBUGF("FLASH MANUFACT: %x\n", value);
217 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_MANUFACT:
218 info->flash_id = FLASH_MAN_AMD;
220 case (CONFIG_SYS_FLASH_WORD_SIZE) FUJ_MANUFACT:
221 info->flash_id = FLASH_MAN_FUJ;
223 case (CONFIG_SYS_FLASH_WORD_SIZE) SST_MANUFACT:
224 info->flash_id = FLASH_MAN_SST;
226 case (CONFIG_SYS_FLASH_WORD_SIZE) STM_MANUFACT:
227 info->flash_id = FLASH_MAN_STM;
230 info->flash_id = FLASH_UNKNOWN;
231 info->sector_count = 0;
233 return (0); /* no or unknown flash */
236 value = addr2[1]; /* device ID */
237 DEBUGF("\nFLASH DEVICEID: %x\n", value);
240 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV040B:
241 info->flash_id += FLASH_AM040;
242 info->sector_count = 8;
243 info->size = 0x0080000; /* => 512 ko */
246 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_F040B:
247 info->flash_id += FLASH_AM040;
248 info->sector_count = 8;
249 info->size = 0x0080000; /* => 512 ko */
252 case (CONFIG_SYS_FLASH_WORD_SIZE) STM_ID_M29W040B:
253 info->flash_id += FLASH_AM040;
254 info->sector_count = 8;
255 info->size = 0x0080000; /* => 512 ko */
258 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_F016D:
259 info->flash_id += FLASH_AMD016;
260 info->sector_count = 32;
261 info->size = 0x00200000;
264 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV033C:
265 info->flash_id += FLASH_AMDLV033C;
266 info->sector_count = 64;
267 info->size = 0x00400000;
270 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV400T:
271 info->flash_id += FLASH_AM400T;
272 info->sector_count = 11;
273 info->size = 0x00080000;
274 break; /* => 0.5 MB */
276 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV400B:
277 info->flash_id += FLASH_AM400B;
278 info->sector_count = 11;
279 info->size = 0x00080000;
280 break; /* => 0.5 MB */
282 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV800T:
283 info->flash_id += FLASH_AM800T;
284 info->sector_count = 19;
285 info->size = 0x00100000;
288 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV800B:
289 info->flash_id += FLASH_AM800B;
290 info->sector_count = 19;
291 info->size = 0x00100000;
294 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV160T:
295 info->flash_id += FLASH_AM160T;
296 info->sector_count = 35;
297 info->size = 0x00200000;
300 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV160B:
301 info->flash_id += FLASH_AM160B;
302 info->sector_count = 35;
303 info->size = 0x00200000;
307 info->flash_id = FLASH_UNKNOWN;
308 return (0); /* => no or unknown flash */
311 /* set up sector start address table */
312 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
313 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) ||
314 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMD016)) {
315 for (i = 0; i < info->sector_count; i++)
316 info->start[i] = base + (i * 0x00010000);
318 if (info->flash_id & FLASH_BTYPE) {
319 /* set sector offsets for bottom boot block type */
320 info->start[0] = base + 0x00000000;
321 info->start[1] = base + 0x00004000;
322 info->start[2] = base + 0x00006000;
323 info->start[3] = base + 0x00008000;
324 for (i = 4; i < info->sector_count; i++) {
326 base + (i * 0x00010000) - 0x00030000;
329 /* set sector offsets for top boot block type */
330 i = info->sector_count - 1;
331 info->start[i--] = base + info->size - 0x00004000;
332 info->start[i--] = base + info->size - 0x00006000;
333 info->start[i--] = base + info->size - 0x00008000;
334 for (; i >= 0; i--) {
335 info->start[i] = base + i * 0x00010000;
340 /* check for protected sectors */
341 for (i = 0; i < info->sector_count; i++) {
342 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
343 /* D0 = 1 if protected */
344 addr2 = (volatile CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[i]);
346 /* For AMD29033C flash we need to resend the command of *
347 * reading flash protection for upper 8 Mb of flash */
349 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xAAAAAAAA;
350 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x55555555;
351 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x90909090;
354 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
355 info->protect[i] = 0;
357 info->protect[i] = addr2[2] & 1;
360 /* issue bank reset to return to read mode */
361 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00F000F0;
366 static int wait_for_DQ7_1(flash_info_t * info, int sect)
368 ulong start, now, last;
369 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr =
370 (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
372 start = get_timer(0);
374 while ((addr[0] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) !=
375 (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) {
376 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
380 /* show that we're waiting */
381 if ((now - last) > 1000) { /* every second */
389 #ifdef CONFIG_SYS_FLASH_2ND_16BIT_DEV
390 int flash_erase(flash_info_t * info, int s_first, int s_last)
392 if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
393 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
394 ((info->flash_id & FLASH_TYPEMASK) == FLASH_STMW320DT) ||
395 ((info->flash_id & FLASH_TYPEMASK) == FLASH_MXLV320T)) {
396 return flash_erase_2(info, s_first, s_last);
398 return flash_erase_1(info, s_first, s_last);
402 static int flash_erase_1(flash_info_t * info, int s_first, int s_last)
404 int flash_erase(flash_info_t * info, int s_first, int s_last)
407 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[0]);
408 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2;
409 int flag, prot, sect;
412 if ((s_first < 0) || (s_first > s_last)) {
413 if (info->flash_id == FLASH_UNKNOWN)
414 printf("- missing\n");
416 printf("- no sectors to erase\n");
420 if (info->flash_id == FLASH_UNKNOWN) {
421 printf("Can't erase unknown flash type - aborted\n");
426 for (sect = s_first; sect <= s_last; ++sect) {
427 if (info->protect[sect])
432 printf("- Warning: %d protected sectors will not be erased!", prot);
436 /* Disable interrupts which might cause a timeout here */
437 flag = disable_interrupts();
439 /* Start erase on unprotected sectors */
440 for (sect = s_first; sect <= s_last; sect++) {
441 if (info->protect[sect] == 0) { /* not protected */
442 addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
444 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
445 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
446 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
447 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080;
448 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
449 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
450 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00500050; /* block erase */
451 for (i = 0; i < 50; i++)
452 udelay(1000); /* wait 1 ms */
454 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
455 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
456 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080;
457 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
458 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
459 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00300030; /* sector erase */
462 * Wait for each sector to complete, it's more
463 * reliable. According to AMD Spec, you must
464 * issue all erase commands within a specified
465 * timeout. This has been seen to fail, especially
466 * if printf()s are included (for debug)!!
468 wait_for_DQ7_1(info, sect);
472 /* re-enable interrupts if necessary */
476 /* wait at least 80us - let's wait 1 ms */
479 /* reset to read mode */
480 addr = (CONFIG_SYS_FLASH_WORD_SIZE *) info->start[0];
481 addr[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
487 /*-----------------------------------------------------------------------
488 * Copy memory to flash, returns:
491 * 2 - Flash not erased
493 int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
498 wp = (addr & ~3); /* get lower word aligned address */
501 * handle unaligned start bytes
503 if ((l = addr - wp) != 0) {
505 for (i = 0, cp = wp; i < l; ++i, ++cp)
506 data = (data << 8) | (*(uchar *) cp);
508 for (; i < 4 && cnt > 0; ++i) {
509 data = (data << 8) | *src++;
514 for (; cnt == 0 && i < 4; ++i, ++cp)
515 data = (data << 8) | (*(uchar *) cp);
517 if ((rc = write_word(info, wp, data)) != 0)
524 * handle word aligned part
528 for (i = 0; i < 4; ++i)
529 data = (data << 8) | *src++;
531 if ((rc = write_word(info, wp, data)) != 0)
542 * handle unaligned tail bytes
545 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
546 data = (data << 8) | *src++;
549 for (; i < 4; ++i, ++cp)
550 data = (data << 8) | (*(uchar *) cp);
552 return (write_word(info, wp, data));
555 /*-----------------------------------------------------------------------
556 * Copy memory to flash, returns:
559 * 2 - Flash not erased
561 #ifdef CONFIG_SYS_FLASH_2ND_16BIT_DEV
562 static int write_word(flash_info_t * info, ulong dest, ulong data)
564 if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
565 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
566 ((info->flash_id & FLASH_TYPEMASK) == FLASH_STMW320DT) ||
567 ((info->flash_id & FLASH_TYPEMASK) == FLASH_MXLV320T)) {
568 return write_word_2(info, dest, data);
570 return write_word_1(info, dest, data);
574 static int write_word_1(flash_info_t * info, ulong dest, ulong data)
576 static int write_word(flash_info_t * info, ulong dest, ulong data)
579 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[0]);
580 volatile CONFIG_SYS_FLASH_WORD_SIZE *dest2 = (CONFIG_SYS_FLASH_WORD_SIZE *) dest;
581 volatile CONFIG_SYS_FLASH_WORD_SIZE *data2 = (CONFIG_SYS_FLASH_WORD_SIZE *) & data;
585 /* Check if Flash is (sufficiently) erased */
586 if ((*((vu_long *)dest) & data) != data)
589 for (i = 0; i < 4 / sizeof(CONFIG_SYS_FLASH_WORD_SIZE); i++) {
590 /* Disable interrupts which might cause a timeout here */
591 flag = disable_interrupts();
593 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
594 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
595 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00A000A0;
599 /* re-enable interrupts if necessary */
603 /* data polling for D7 */
604 start = get_timer(0);
605 while ((dest2[i] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) !=
606 (data2[i] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080)) {
608 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT)
616 #ifdef CONFIG_SYS_FLASH_2ND_16BIT_DEV
618 #undef CONFIG_SYS_FLASH_WORD_SIZE
619 #define CONFIG_SYS_FLASH_WORD_SIZE unsigned short
622 * The following code cannot be run from FLASH!
624 static ulong flash_get_size_2(vu_long * addr, flash_info_t * info)
628 CONFIG_SYS_FLASH_WORD_SIZE value;
629 ulong base = (ulong) addr;
630 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) addr;
632 DEBUGF("FLASH ADDR: %08x\n", (unsigned)addr);
634 /* issue bank reset to return to read mode */
635 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00F000F0;
636 /* Write auto select command: read Manufacturer ID */
637 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
638 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
639 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00900090;
643 DEBUGF("FLASH MANUFACT: %x\n", value);
646 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_MANUFACT:
647 info->flash_id = FLASH_MAN_AMD;
649 case (CONFIG_SYS_FLASH_WORD_SIZE) FUJ_MANUFACT:
650 info->flash_id = FLASH_MAN_FUJ;
652 case (CONFIG_SYS_FLASH_WORD_SIZE) SST_MANUFACT:
653 info->flash_id = FLASH_MAN_SST;
655 case (CONFIG_SYS_FLASH_WORD_SIZE) STM_MANUFACT:
656 info->flash_id = FLASH_MAN_STM;
658 case (CONFIG_SYS_FLASH_WORD_SIZE) MX_MANUFACT:
659 info->flash_id = FLASH_MAN_MX;
662 info->flash_id = FLASH_UNKNOWN;
663 info->sector_count = 0;
665 return (0); /* no or unknown flash */
668 value = addr2[1]; /* device ID */
669 DEBUGF("\nFLASH DEVICEID: %x\n", value);
672 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV320T:
673 info->flash_id += FLASH_AM320T;
674 info->sector_count = 71;
675 info->size = 0x00400000;
677 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV320B:
678 info->flash_id += FLASH_AM320B;
679 info->sector_count = 71;
680 info->size = 0x00400000;
682 case (CONFIG_SYS_FLASH_WORD_SIZE)STM_ID_29W320DT:
683 info->flash_id += FLASH_STMW320DT;
684 info->sector_count = 67;
685 info->size = 0x00400000;
687 case (CONFIG_SYS_FLASH_WORD_SIZE)MX_ID_LV320T:
688 info->flash_id += FLASH_MXLV320T;
689 info->sector_count = 71;
690 info->size = 0x00400000;
693 info->flash_id = FLASH_UNKNOWN;
694 return (0); /* => no or unknown flash */
697 /* set up sector start address table */
698 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
699 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) ||
700 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMD016)) {
701 for (i = 0; i < info->sector_count; i++)
702 info->start[i] = base + (i * 0x00010000);
703 } else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_STMW320DT) {
704 /* set sector offsets for top boot block type */
706 i = info->sector_count;
707 /* 1 x 16k boot sector */
710 info->start[i] = base;
711 /* 2 x 8k boot sectors */
712 for (n = 0; n < 2; ++n) {
715 info->start[i] = base;
717 /* 1 x 32k boot sector */
720 info->start[i] = base;
722 while (i > 0) { /* 64k regular sectors */
725 info->start[i] = base;
727 } else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_MXLV320T) {
728 i = info->sector_count - 1;
729 info->start[i--] = base + info->size - 0x00002000;
730 info->start[i--] = base + info->size - 0x00004000;
731 info->start[i--] = base + info->size - 0x00006000;
732 info->start[i--] = base + info->size - 0x00008000;
733 info->start[i--] = base + info->size - 0x0000a000;
734 info->start[i--] = base + info->size - 0x0000c000;
735 info->start[i--] = base + info->size - 0x0000e000;
736 info->start[i--] = base + info->size - 0x00010000;
739 info->start[i] = base + i * 0x00010000;
741 if (info->flash_id & FLASH_BTYPE) {
742 /* set sector offsets for bottom boot block type */
743 info->start[0] = base + 0x00000000;
744 info->start[1] = base + 0x00004000;
745 info->start[2] = base + 0x00006000;
746 info->start[3] = base + 0x00008000;
748 for (i = 4; i < info->sector_count; i++)
749 info->start[i] = base + (i * 0x00010000) - 0x00030000;
751 /* set sector offsets for top boot block type */
752 i = info->sector_count - 1;
753 info->start[i--] = base + info->size - 0x00004000;
754 info->start[i--] = base + info->size - 0x00006000;
755 info->start[i--] = base + info->size - 0x00008000;
758 info->start[i] = base + i * 0x00010000;
762 /* check for protected sectors */
763 for (i = 0; i < info->sector_count; i++) {
764 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
765 /* D0 = 1 if protected */
766 addr2 = (volatile CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[i]);
768 /* For AMD29033C flash we need to resend the command of *
769 * reading flash protection for upper 8 Mb of flash */
771 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xAAAAAAAA;
772 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x55555555;
773 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x90909090;
776 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
777 info->protect[i] = 0;
779 info->protect[i] = addr2[2] & 1;
782 /* issue bank reset to return to read mode */
783 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00F000F0;
788 static int wait_for_DQ7_2(flash_info_t * info, int sect)
790 ulong start, now, last;
791 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr =
792 (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
794 start = get_timer(0);
796 while ((addr[0] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) !=
797 (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) {
798 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
802 /* show that we're waiting */
803 if ((now - last) > 1000) { /* every second */
811 static int flash_erase_2(flash_info_t * info, int s_first, int s_last)
813 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[0]);
814 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2;
815 int flag, prot, sect;
818 if ((s_first < 0) || (s_first > s_last)) {
819 if (info->flash_id == FLASH_UNKNOWN)
820 printf("- missing\n");
822 printf("- no sectors to erase\n");
826 if (info->flash_id == FLASH_UNKNOWN) {
827 printf("Can't erase unknown flash type - aborted\n");
832 for (sect = s_first; sect <= s_last; ++sect) {
833 if (info->protect[sect])
838 printf("- Warning: %d protected sectors will not be erased!", prot);
842 /* Disable interrupts which might cause a timeout here */
843 flag = disable_interrupts();
845 /* Start erase on unprotected sectors */
846 for (sect = s_first; sect <= s_last; sect++) {
847 if (info->protect[sect] == 0) { /* not protected */
848 addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
850 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
851 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
852 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
853 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080;
854 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
855 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
856 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00500050; /* block erase */
857 for (i = 0; i < 50; i++)
858 udelay(1000); /* wait 1 ms */
860 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
861 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
862 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080;
863 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
864 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
865 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00300030; /* sector erase */
868 * Wait for each sector to complete, it's more
869 * reliable. According to AMD Spec, you must
870 * issue all erase commands within a specified
871 * timeout. This has been seen to fail, especially
872 * if printf()s are included (for debug)!!
874 wait_for_DQ7_2(info, sect);
878 /* re-enable interrupts if necessary */
882 /* wait at least 80us - let's wait 1 ms */
885 /* reset to read mode */
886 addr = (CONFIG_SYS_FLASH_WORD_SIZE *) info->start[0];
887 addr[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
893 static int write_word_2(flash_info_t * info, ulong dest, ulong data)
895 ulong *data_ptr = &data;
896 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[0]);
897 volatile CONFIG_SYS_FLASH_WORD_SIZE *dest2 = (CONFIG_SYS_FLASH_WORD_SIZE *)dest;
898 volatile CONFIG_SYS_FLASH_WORD_SIZE *data2 = (CONFIG_SYS_FLASH_WORD_SIZE *)data_ptr;
902 /* Check if Flash is (sufficiently) erased */
903 if ((*((vu_long *)dest) & data) != data)
906 for (i = 0; i < 4 / sizeof(CONFIG_SYS_FLASH_WORD_SIZE); i++) {
909 /* Disable interrupts which might cause a timeout here */
910 flag = disable_interrupts();
912 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
913 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
914 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00A000A0;
918 /* re-enable interrupts if necessary */
922 /* data polling for D7 */
923 start = get_timer(0);
924 while ((dest2[i] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) !=
925 (data2[i] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080)) {
927 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT)
934 #endif /* CONFIG_SYS_FLASH_2ND_16BIT_DEV */
936 /*-----------------------------------------------------------------------
939 static ulong flash_get_size(vu_long * addr, flash_info_t * info);
940 static int write_word(flash_info_t * info, ulong dest, ulong data);
942 /*-----------------------------------------------------------------------
945 unsigned long flash_init(void)
947 unsigned long total_b = 0;
948 unsigned long size_b[CONFIG_SYS_MAX_FLASH_BANKS];
949 unsigned short index = 0;
952 unsigned long ebc_boot_size;
953 unsigned long boot_selection;
955 mfsdr(sdr_pstrp0, val);
956 index = (val & SDR0_PSTRP0_BOOTSTRAP_MASK) >> 28;
958 if ((index == 0xc) || (index == 8)) {
960 * Boot Settings in IIC EEprom address 0xA8 or 0xA0
961 * Read Serial Device Strap Register1 in PPC440SPe
963 mfsdr(SDR0_SDSTP1, val);
964 boot_selection = val & SDR0_SDSTP1_BOOT_SEL_MASK;
965 ebc_boot_size = val & SDR0_SDSTP1_EBC_ROM_BS_MASK;
967 switch(boot_selection) {
968 case SDR0_SDSTP1_BOOT_SEL_EBC:
969 switch(ebc_boot_size) {
970 case SDR0_SDSTP1_EBC_ROM_BS_16BIT:
973 case SDR0_SDSTP1_EBC_ROM_BS_8BIT:
979 case SDR0_SDSTP1_BOOT_SEL_PCI:
984 } /*else if (index == 0) {*/
985 /* if (in8(FPGA_SETTING_REG) & FPGA_SET_REG_OP_CODE_FLASH_ABOVE)*/
986 /* index = 8;*/ /* sram below op code flash -> new index 8*/
990 DEBUGF("FLASH: Index: %d\n", index);
992 /* Init: no FLASHes known */
993 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
994 flash_info[i].flash_id = FLASH_UNKNOWN;
995 flash_info[i].sector_count = -1;
996 flash_info[i].size = 0;
998 /* check whether the address is 0 */
999 if (flash_addr_table[index][i] == 0)
1002 /* call flash_get_size() to initialize sector address */
1003 size_b[i] = flash_get_size((vu_long *) flash_addr_table[index][i],
1006 flash_info[i].size = size_b[i];
1008 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
1009 printf("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",
1010 i, size_b[i], size_b[i] << 20);
1011 flash_info[i].sector_count = -1;
1012 flash_info[i].size = 0;
1015 /* Monitor protection ON by default */
1016 (void)flash_protect(FLAG_PROTECT_SET, CONFIG_SYS_MONITOR_BASE,
1017 CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN - 1,
1019 #if defined(CONFIG_ENV_IS_IN_FLASH)
1020 (void)flash_protect(FLAG_PROTECT_SET, CONFIG_ENV_ADDR,
1021 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
1023 #if defined(CONFIG_ENV_ADDR_REDUND)
1024 (void)flash_protect(FLAG_PROTECT_SET, CONFIG_ENV_ADDR_REDUND,
1025 CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1,
1029 total_b += flash_info[i].size;