3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 * Wait for completion of each sector erase command issued
28 * Chris Hallinan - DS4.COM, Inc. - clh@net1plus.com
32 #include <asm/ppc4xx.h>
33 #include <asm/processor.h>
35 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
39 #define DEBUGF(x...) printf(x)
44 #define CONFIG_SYS_FLASH_CHAR_SIZE unsigned char
45 #define CONFIG_SYS_FLASH_CHAR_ADDR0 (0x0aaa)
46 #define CONFIG_SYS_FLASH_CHAR_ADDR1 (0x0555)
47 /*-----------------------------------------------------------------------
50 static ulong flash_get_size(vu_long * addr, flash_info_t * info);
51 static void flash_get_offsets(ulong base, flash_info_t * info);
52 static int write_word(flash_info_t * info, ulong dest, ulong data);
53 #ifdef FLASH_BASE1_PRELIM
54 static int write_word_1(flash_info_t * info, ulong dest, ulong data);
55 static int write_word_2(flash_info_t * info, ulong dest, ulong data);
56 static int flash_erase_1(flash_info_t * info, int s_first, int s_last);
57 static int flash_erase_2(flash_info_t * info, int s_first, int s_last);
58 static ulong flash_get_size_1(vu_long * addr, flash_info_t * info);
59 static ulong flash_get_size_2(vu_long * addr, flash_info_t * info);
62 unsigned long flash_init(void)
64 unsigned long size_b0, size_b1=0;
67 /* Init: no FLASHes known */
68 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
69 flash_info[i].flash_id = FLASH_UNKNOWN;
72 /* Static FLASH Bank configuration here - FIXME XXX */
75 flash_get_size((vu_long *) FLASH_BASE0_PRELIM, &flash_info[0]);
77 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
78 printf("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
79 size_b0, size_b0 << 20);
84 flash_get_offsets(FLASH_BASE0_PRELIM, &flash_info[0]);
85 /* Monitor protection ON by default */
86 (void)flash_protect(FLAG_PROTECT_SET,
87 CONFIG_SYS_MONITOR_BASE,
88 CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN - 1,
90 #ifdef CONFIG_ENV_IS_IN_FLASH
91 (void)flash_protect(FLAG_PROTECT_SET, CONFIG_ENV_ADDR,
92 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1,
94 (void)flash_protect(FLAG_PROTECT_SET, CONFIG_ENV_ADDR_REDUND,
95 CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1,
98 /* Also protect sector containing initial power-up instruction */
99 /* (flash_protect() checks address range - other call ignored) */
100 (void)flash_protect(FLAG_PROTECT_SET,
101 0xFFFFFFFC, 0xFFFFFFFF, &flash_info[0]);
103 flash_info[0].size = size_b0;
105 #ifdef FLASH_BASE1_PRELIM
107 flash_get_size((vu_long *) FLASH_BASE1_PRELIM, &flash_info[1])*2;
109 if (flash_info[1].flash_id == FLASH_UNKNOWN) {
110 printf("## Unknown FLASH on Bank 1 - Size = 0x%08lx = %ld MB\n",
111 size_b1, size_b1 << 20);
116 flash_get_offsets(FLASH_BASE1_PRELIM, &flash_info[1]);
117 flash_info[1].size = size_b1;
120 return (size_b0 + size_b1);
123 static void flash_get_offsets(ulong base, flash_info_t * info)
127 /* set up sector start address table */
128 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
129 (info->flash_id == FLASH_AM040)) {
130 for (i = 0; i < info->sector_count; i++)
131 info->start[i] = base + (i * 0x00010000);
132 } else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMLV128U) {
133 for (i = 0; i < info->sector_count; i++) {
134 info->start[i] = base + (i * 0x00010000*2);
136 } else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_S29GL128N ) {
137 for (i = 0; i < info->sector_count; i++) {
138 info->start[i] = base + (i * 0x00020000*2);
141 if (info->flash_id & FLASH_BTYPE) {
142 /* set sector offsets for bottom boot block type */
143 info->start[0] = base + 0x00000000;
144 info->start[1] = base + 0x00004000;
145 info->start[2] = base + 0x00006000;
146 info->start[3] = base + 0x00008000;
147 for (i = 4; i < info->sector_count; i++) {
149 base + (i * 0x00010000) - 0x00030000;
152 /* set sector offsets for top boot block type */
153 i = info->sector_count - 1;
154 info->start[i--] = base + info->size - 0x00004000;
155 info->start[i--] = base + info->size - 0x00006000;
156 info->start[i--] = base + info->size - 0x00008000;
157 for (; i >= 0; i--) {
158 info->start[i] = base + i * 0x00010000;
165 void flash_print_info(flash_info_t * info)
171 volatile unsigned long *flash;
173 if (info->flash_id == FLASH_UNKNOWN) {
174 printf("missing or unknown FLASH type\n");
178 switch (info->flash_id & FLASH_VENDMASK) {
192 printf("Unknown Vendor ");
196 switch (info->flash_id & FLASH_TYPEMASK) {
198 printf("AM29F040 (512 Kbit, uniform sector size)\n");
201 printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
204 printf("AM29LV400T (4 Mbit, top boot sector)\n");
207 printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
210 printf("AM29LV800T (8 Mbit, top boot sector)\n");
213 printf("AM29F016D (16 Mbit, uniform sector size)\n");
216 printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
219 printf("AM29LV160T (16 Mbit, top boot sector)\n");
222 printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
225 printf("AM29LV320T (32 Mbit, top boot sector)\n");
228 printf("AM29LV033C (32 Mbit, top boot sector)\n");
231 printf("AM29LV128U (128 Mbit * 2, top boot sector)\n");
234 printf("SST39LF/VF800 (8 Mbit, uniform sector size)\n");
237 printf("SST39LF/VF160 (16 Mbit, uniform sector size)\n");
239 case FLASH_STMW320DT:
240 printf ("M29W320DT (32 M, top sector)\n");
242 case FLASH_S29GL128N:
243 printf ("S29GL128N (256 Mbit, uniform sector size)\n");
246 printf("Unknown Chip Type\n");
250 printf(" Size: %ld KB in %d Sectors\n",
251 info->size >> 10, info->sector_count);
253 printf(" Sector Start Addresses:");
254 for (i = 0; i < info->sector_count; ++i) {
256 * Check if whole sector is erased
258 if (i != (info->sector_count - 1))
259 size = info->start[i + 1] - info->start[i];
261 size = info->start[0] + info->size - info->start[i];
263 flash = (volatile unsigned long *)info->start[i];
264 size = size >> 2; /* divide by 4 for longword access */
265 for (k = 0; k < size; k++) {
266 if (*flash++ != 0xffffffff) {
276 erased ? " E" : " ", info->protect[i] ? "RO " : " ");
284 * The following code cannot be run from FLASH!
286 #ifdef FLASH_BASE1_PRELIM
287 static ulong flash_get_size(vu_long * addr, flash_info_t * info)
289 if ((ulong)addr == FLASH_BASE1_PRELIM) {
290 return flash_get_size_2(addr, info);
292 return flash_get_size_1(addr, info);
296 static ulong flash_get_size_1(vu_long * addr, flash_info_t * info)
298 static ulong flash_get_size(vu_long * addr, flash_info_t * info)
302 CONFIG_SYS_FLASH_WORD_SIZE value;
303 ulong base = (ulong) addr;
304 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) addr;
306 DEBUGF("FLASH ADDR: %08x\n", (unsigned)addr);
308 /* Write auto select command: read Manufacturer ID */
309 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
310 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
311 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00900090;
315 DEBUGF("FLASH MANUFACT: %x\n", value);
318 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_MANUFACT:
319 info->flash_id = FLASH_MAN_AMD;
321 case (CONFIG_SYS_FLASH_WORD_SIZE) FUJ_MANUFACT:
322 info->flash_id = FLASH_MAN_FUJ;
324 case (CONFIG_SYS_FLASH_WORD_SIZE) SST_MANUFACT:
325 info->flash_id = FLASH_MAN_SST;
327 case (CONFIG_SYS_FLASH_WORD_SIZE) STM_MANUFACT:
328 info->flash_id = FLASH_MAN_STM;
331 info->flash_id = FLASH_UNKNOWN;
332 info->sector_count = 0;
334 return 0; /* no or unknown flash */
337 value = addr2[1]; /* device ID */
338 DEBUGF("\nFLASH DEVICEID: %x\n", value);
341 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV040B:
342 info->flash_id += FLASH_AM040;
343 info->sector_count = 8;
344 info->size = 0x0080000; /* => 512 ko */
347 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_F040B:
348 info->flash_id += FLASH_AM040;
349 info->sector_count = 8;
350 info->size = 0x0080000; /* => 512 ko */
353 case (CONFIG_SYS_FLASH_WORD_SIZE) STM_ID_M29W040B:
354 info->flash_id += FLASH_AM040;
355 info->sector_count = 8;
356 info->size = 0x0080000; /* => 512 ko */
359 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_F016D:
360 info->flash_id += FLASH_AMD016;
361 info->sector_count = 32;
362 info->size = 0x00200000;
365 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV033C:
366 info->flash_id += FLASH_AMDLV033C;
367 info->sector_count = 64;
368 info->size = 0x00400000;
371 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV400T:
372 info->flash_id += FLASH_AM400T;
373 info->sector_count = 11;
374 info->size = 0x00080000;
375 break; /* => 0.5 MB */
377 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV400B:
378 info->flash_id += FLASH_AM400B;
379 info->sector_count = 11;
380 info->size = 0x00080000;
381 break; /* => 0.5 MB */
383 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV800T:
384 info->flash_id += FLASH_AM800T;
385 info->sector_count = 19;
386 info->size = 0x00100000;
389 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV800B:
390 info->flash_id += FLASH_AM800B;
391 info->sector_count = 19;
392 info->size = 0x00100000;
395 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV160T:
396 info->flash_id += FLASH_AM160T;
397 info->sector_count = 35;
398 info->size = 0x00200000;
401 case (CONFIG_SYS_FLASH_WORD_SIZE) AMD_ID_LV160B:
402 info->flash_id += FLASH_AM160B;
403 info->sector_count = 35;
404 info->size = 0x00200000;
407 info->flash_id = FLASH_UNKNOWN;
408 return 0; /* => no or unknown flash */
411 /* set up sector start address table */
412 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
413 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) ||
414 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMD016)) {
415 for (i = 0; i < info->sector_count; i++)
416 info->start[i] = base + (i * 0x00010000);
418 else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMLV128U) {
419 for (i = 0; i < info->sector_count; i++)
420 info->start[i] = base + (i * 0x00010000 * 2);
422 if (info->flash_id & FLASH_BTYPE) {
423 /* set sector offsets for bottom boot block type */
424 info->start[0] = base + 0x00000000;
425 info->start[1] = base + 0x00004000;
426 info->start[2] = base + 0x00006000;
427 info->start[3] = base + 0x00008000;
428 for (i = 4; i < info->sector_count; i++) {
430 base + (i * 0x00010000) - 0x00030000;
433 /* set sector offsets for top boot block type */
434 i = info->sector_count - 1;
435 info->start[i--] = base + info->size - 0x00004000;
436 info->start[i--] = base + info->size - 0x00006000;
437 info->start[i--] = base + info->size - 0x00008000;
438 for (; i >= 0; i--) {
439 info->start[i] = base + i * 0x00010000;
444 /* check for protected sectors */
445 for (i = 0; i < info->sector_count; i++) {
446 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
447 /* D0 = 1 if protected */
448 addr2 = (volatile CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[i]);
450 /* For AMD29033C flash we need to resend the command of *
451 * reading flash protection for upper 8 Mb of flash */
453 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xAAAAAAAA;
454 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x55555555;
455 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x90909090;
458 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
459 info->protect[i] = 0;
461 info->protect[i] = addr2[2] & 1;
464 /* issue bank reset to return to read mode */
465 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00F000F0;
470 static int wait_for_DQ7_1(flash_info_t * info, int sect)
472 ulong start, now, last;
473 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr =
474 (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
476 start = get_timer(0);
478 while ((addr[0] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) !=
479 (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) {
480 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
484 /* show that we're waiting */
485 if ((now - last) > 1000) { /* every second */
493 #ifdef FLASH_BASE1_PRELIM
494 int flash_erase(flash_info_t * info, int s_first, int s_last)
496 if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
497 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
498 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMLV128U) ||
499 ((info->flash_id & FLASH_TYPEMASK) == FLASH_S29GL128N) ||
500 ((info->flash_id & FLASH_TYPEMASK) == FLASH_STMW320DT)) {
501 return flash_erase_2(info, s_first, s_last);
503 return flash_erase_1(info, s_first, s_last);
507 static int flash_erase_1(flash_info_t * info, int s_first, int s_last)
509 int flash_erase(flash_info_t * info, int s_first, int s_last)
512 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[0]);
513 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2;
514 int flag, prot, sect;
517 if ((s_first < 0) || (s_first > s_last)) {
518 if (info->flash_id == FLASH_UNKNOWN) {
519 printf("- missing\n");
521 printf("- no sectors to erase\n");
526 if (info->flash_id == FLASH_UNKNOWN) {
527 printf("Can't erase unknown flash type - aborted\n");
532 for (sect = s_first; sect <= s_last; ++sect) {
533 if (info->protect[sect]) {
539 printf("- Warning: %d protected sectors will not be erased!\n",
545 /* Disable interrupts which might cause a timeout here */
546 flag = disable_interrupts();
548 /* Start erase on unprotected sectors */
549 for (sect = s_first; sect <= s_last; sect++) {
550 if (info->protect[sect] == 0) { /* not protected */
551 addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
553 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
554 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
555 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
556 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080;
557 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
558 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
559 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00500050; /* block erase */
560 for (i = 0; i < 50; i++)
561 udelay(1000); /* wait 1 ms */
563 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
564 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
565 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080;
566 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
567 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
568 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00300030; /* sector erase */
571 * Wait for each sector to complete, it's more
572 * reliable. According to AMD Spec, you must
573 * issue all erase commands within a specified
574 * timeout. This has been seen to fail, especially
575 * if printf()s are included (for debug)!!
577 wait_for_DQ7_1(info, sect);
581 /* re-enable interrupts if necessary */
585 /* wait at least 80us - let's wait 1 ms */
588 /* reset to read mode */
589 addr = (CONFIG_SYS_FLASH_WORD_SIZE *) info->start[0];
590 addr[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
596 /*-----------------------------------------------------------------------
597 * Copy memory to flash, returns:
600 * 2 - Flash not erased
602 int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
607 wp = (addr & ~3); /* get lower word aligned address */
610 * handle unaligned start bytes
612 if ((l = addr - wp) != 0) {
614 for (i = 0, cp = wp; i < l; ++i, ++cp) {
615 data = (data << 8) | (*(uchar *) cp);
617 for (; i < 4 && cnt > 0; ++i) {
618 data = (data << 8) | *src++;
622 for (; cnt == 0 && i < 4; ++i, ++cp) {
623 data = (data << 8) | (*(uchar *) cp);
626 if ((rc = write_word(info, wp, data)) != 0) {
633 * handle word aligned part
637 for (i = 0; i < 4; ++i) {
638 data = (data << 8) | *src++;
640 if ((rc = write_word(info, wp, data)) != 0) {
652 * handle unaligned tail bytes
655 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
656 data = (data << 8) | *src++;
659 for (; i < 4; ++i, ++cp) {
660 data = (data << 8) | (*(uchar *) cp);
663 return (write_word(info, wp, data));
666 /*-----------------------------------------------------------------------
667 * Copy memory to flash, returns:
670 * 2 - Flash not erased
672 #ifdef FLASH_BASE1_PRELIM
673 static int write_word(flash_info_t * info, ulong dest, ulong data)
675 if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
676 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
677 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMLV128U) ||
678 ((info->flash_id & FLASH_TYPEMASK) == FLASH_S29GL128N) ||
679 ((info->flash_id & FLASH_TYPEMASK) == FLASH_STMW320DT)) {
680 return write_word_2(info, dest, data);
682 return write_word_1(info, dest, data);
686 static int write_word_1(flash_info_t * info, ulong dest, ulong data)
688 static int write_word(flash_info_t * info, ulong dest, ulong data)
691 ulong *data_ptr = &data;
692 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[0]);
693 volatile CONFIG_SYS_FLASH_WORD_SIZE *dest2 = (CONFIG_SYS_FLASH_WORD_SIZE *)dest;
694 volatile CONFIG_SYS_FLASH_WORD_SIZE *data2 = (CONFIG_SYS_FLASH_WORD_SIZE *)data_ptr;
698 /* Check if Flash is (sufficiently) erased */
699 if ((*((vu_long *)dest) & data) != data) {
703 for (i = 0; i < 4 / sizeof(CONFIG_SYS_FLASH_WORD_SIZE); i++) {
706 /* Disable interrupts which might cause a timeout here */
707 flag = disable_interrupts();
709 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00AA00AA;
710 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00550055;
711 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x00A000A0;
715 /* re-enable interrupts if necessary */
719 /* data polling for D7 */
720 start = get_timer(0);
721 while ((dest2[i] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080) !=
722 (data2[i] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x00800080)) {
724 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
733 #ifdef FLASH_BASE1_PRELIM
736 * The following code cannot be run from FLASH!
738 static ulong flash_get_size_2(vu_long * addr, flash_info_t * info)
741 CONFIG_SYS_FLASH_CHAR_SIZE value;
742 ulong base = (ulong) addr;
743 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) addr;
745 DEBUGF("FLASH ADDR: %08x\n", (unsigned)addr);
747 /* Write auto select command: read Manufacturer ID */
748 addr2[CONFIG_SYS_FLASH_CHAR_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xAAAAAAAA;
749 addr2[CONFIG_SYS_FLASH_CHAR_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x55555555;
750 addr2[CONFIG_SYS_FLASH_CHAR_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x90909090;
753 value = (CONFIG_SYS_FLASH_CHAR_SIZE)addr2[0];
754 DEBUGF("FLASH MANUFACT: %x\n", value);
757 case (CONFIG_SYS_FLASH_CHAR_SIZE) AMD_MANUFACT:
758 info->flash_id = FLASH_MAN_AMD;
760 case (CONFIG_SYS_FLASH_CHAR_SIZE) FUJ_MANUFACT:
761 info->flash_id = FLASH_MAN_FUJ;
763 case (CONFIG_SYS_FLASH_CHAR_SIZE) SST_MANUFACT:
764 info->flash_id = FLASH_MAN_SST;
766 case (CONFIG_SYS_FLASH_CHAR_SIZE) STM_MANUFACT:
767 info->flash_id = FLASH_MAN_STM;
770 info->flash_id = FLASH_UNKNOWN;
771 info->sector_count = 0;
773 return 0; /* no or unknown flash */
776 value = (CONFIG_SYS_FLASH_CHAR_SIZE)addr2[2]; /* device ID */
777 DEBUGF("\nFLASH DEVICEID: %x\n", value);
780 case (CONFIG_SYS_FLASH_CHAR_SIZE) AMD_ID_LV040B:
781 info->flash_id += FLASH_AM040;
782 info->sector_count = 8;
783 info->size = 0x0080000; /* => 512 ko */
786 case (CONFIG_SYS_FLASH_CHAR_SIZE) AMD_ID_F040B:
787 info->flash_id += FLASH_AM040;
788 info->sector_count = 8;
789 info->size = 0x0080000; /* => 512 ko */
792 case (CONFIG_SYS_FLASH_CHAR_SIZE) STM_ID_M29W040B:
793 info->flash_id += FLASH_AM040;
794 info->sector_count = 8;
795 info->size = 0x0080000; /* => 512 ko */
798 case (CONFIG_SYS_FLASH_CHAR_SIZE) AMD_ID_F016D:
799 info->flash_id += FLASH_AMD016;
800 info->sector_count = 32;
801 info->size = 0x00200000;
804 case (CONFIG_SYS_FLASH_CHAR_SIZE) AMD_ID_LV033C:
805 info->flash_id += FLASH_AMDLV033C;
806 info->sector_count = 64;
807 info->size = 0x00400000;
810 case (CONFIG_SYS_FLASH_CHAR_SIZE) AMD_ID_LV400T:
811 info->flash_id += FLASH_AM400T;
812 info->sector_count = 11;
813 info->size = 0x00080000;
814 break; /* => 0.5 MB */
816 case (CONFIG_SYS_FLASH_CHAR_SIZE) AMD_ID_LV400B:
817 info->flash_id += FLASH_AM400B;
818 info->sector_count = 11;
819 info->size = 0x00080000;
820 break; /* => 0.5 MB */
822 case (CONFIG_SYS_FLASH_CHAR_SIZE) AMD_ID_LV800T:
823 info->flash_id += FLASH_AM800T;
824 info->sector_count = 19;
825 info->size = 0x00100000;
828 case (CONFIG_SYS_FLASH_CHAR_SIZE) AMD_ID_LV800B:
829 info->flash_id += FLASH_AM800B;
830 info->sector_count = 19;
831 info->size = 0x00100000;
834 case (CONFIG_SYS_FLASH_CHAR_SIZE) AMD_ID_LV160T:
835 info->flash_id += FLASH_AM160T;
836 info->sector_count = 35;
837 info->size = 0x00200000;
840 case (CONFIG_SYS_FLASH_CHAR_SIZE) AMD_ID_LV160B:
841 info->flash_id += FLASH_AM160B;
842 info->sector_count = 35;
843 info->size = 0x00200000;
845 case (CONFIG_SYS_FLASH_CHAR_SIZE) AMD_ID_MIRROR:
846 if ((CONFIG_SYS_FLASH_CHAR_SIZE)addr2[0x1c] == (CONFIG_SYS_FLASH_CHAR_SIZE)AMD_ID_LV128U_2
847 && (CONFIG_SYS_FLASH_CHAR_SIZE)addr2[0x1e] == (CONFIG_SYS_FLASH_CHAR_SIZE)AMD_ID_LV128U_3) {
848 info->flash_id += FLASH_AMLV128U;
849 info->sector_count = 256;
850 info->size = 0x01000000;
851 } else if ((CONFIG_SYS_FLASH_CHAR_SIZE)addr2[0x1c] == (CONFIG_SYS_FLASH_CHAR_SIZE)AMD_ID_GL128N_2
852 && (CONFIG_SYS_FLASH_CHAR_SIZE)addr2[0x1e] == (CONFIG_SYS_FLASH_CHAR_SIZE)AMD_ID_GL128N_3 ) {
853 info->flash_id += FLASH_S29GL128N;
854 info->sector_count = 128;
855 info->size = 0x01000000;
858 info->flash_id = FLASH_UNKNOWN;
862 info->flash_id = FLASH_UNKNOWN;
863 return 0; /* => no or unknown flash */
866 /* set up sector start address table */
867 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
868 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) ||
869 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMD016)) {
870 for (i = 0; i < info->sector_count; i++)
871 info->start[i] = base + (i * 0x00010000);
872 } else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMLV128U) {
873 for (i = 0; i < info->sector_count; i++)
874 info->start[i] = base + (i * 0x00010000);
875 } else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_S29GL128N ) {
876 for (i = 0; i < info->sector_count; i++)
877 info->start[i] = base + (i * 0x00020000);
879 if (info->flash_id & FLASH_BTYPE) {
880 /* set sector offsets for bottom boot block type */
881 info->start[0] = base + 0x00000000;
882 info->start[1] = base + 0x00004000;
883 info->start[2] = base + 0x00006000;
884 info->start[3] = base + 0x00008000;
885 for (i = 4; i < info->sector_count; i++) {
887 base + (i * 0x00010000) - 0x00030000;
890 /* set sector offsets for top boot block type */
891 i = info->sector_count - 1;
892 info->start[i--] = base + info->size - 0x00004000;
893 info->start[i--] = base + info->size - 0x00006000;
894 info->start[i--] = base + info->size - 0x00008000;
895 for (; i >= 0; i--) {
896 info->start[i] = base + i * 0x00010000;
901 /* check for protected sectors */
902 for (i = 0; i < info->sector_count; i++) {
903 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
904 /* D0 = 1 if protected */
905 addr2 = (volatile CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[i]);
907 /* For AMD29033C flash we need to resend the command of *
908 * reading flash protection for upper 8 Mb of flash */
910 addr2[CONFIG_SYS_FLASH_CHAR_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xAAAAAAAA;
911 addr2[CONFIG_SYS_FLASH_CHAR_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x55555555;
912 addr2[CONFIG_SYS_FLASH_CHAR_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x90909090;
915 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
916 info->protect[i] = 0;
918 info->protect[i] = (CONFIG_SYS_FLASH_CHAR_SIZE)addr2[4] & 1;
921 /* issue bank reset to return to read mode */
922 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xF0F0F0F0;
926 static int wait_for_DQ7_2(flash_info_t * info, int sect)
928 ulong start, now, last;
929 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr =
930 (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
932 start = get_timer(0);
934 while (((CONFIG_SYS_FLASH_WORD_SIZE)addr[0] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x80808080) !=
935 (CONFIG_SYS_FLASH_WORD_SIZE) 0x80808080) {
936 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
940 /* show that we're waiting */
941 if ((now - last) > 1000) { /* every second */
949 static int flash_erase_2(flash_info_t * info, int s_first, int s_last)
951 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[0]);
952 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2;
953 int flag, prot, sect;
956 if ((s_first < 0) || (s_first > s_last)) {
957 if (info->flash_id == FLASH_UNKNOWN) {
958 printf("- missing\n");
960 printf("- no sectors to erase\n");
965 if (info->flash_id == FLASH_UNKNOWN) {
966 printf("Can't erase unknown flash type - aborted\n");
971 for (sect = s_first; sect <= s_last; ++sect) {
972 if (info->protect[sect]) {
978 printf("- Warning: %d protected sectors will not be erased!\n",
984 /* Disable interrupts which might cause a timeout here */
985 flag = disable_interrupts();
987 /* Start erase on unprotected sectors */
988 for (sect = s_first; sect <= s_last; sect++) {
989 if (info->protect[sect] == 0) { /* not protected */
990 addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *) (info->start[sect]);
992 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
993 addr[CONFIG_SYS_FLASH_CHAR_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xAAAAAAAA;
994 addr[CONFIG_SYS_FLASH_CHAR_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x55555555;
995 addr[CONFIG_SYS_FLASH_CHAR_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x80808080;
996 addr[CONFIG_SYS_FLASH_CHAR_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xAAAAAAAA;
997 addr[CONFIG_SYS_FLASH_CHAR_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x55555555;
998 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x50505050; /* block erase */
999 for (i = 0; i < 50; i++)
1000 udelay(1000); /* wait 1 ms */
1002 addr[CONFIG_SYS_FLASH_CHAR_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xAAAAAAAA;
1003 addr[CONFIG_SYS_FLASH_CHAR_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x55555555;
1004 addr[CONFIG_SYS_FLASH_CHAR_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x80808080;
1005 addr[CONFIG_SYS_FLASH_CHAR_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xAAAAAAAA;
1006 addr[CONFIG_SYS_FLASH_CHAR_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x55555555;
1007 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x30303030; /* sector erase */
1010 * Wait for each sector to complete, it's more
1011 * reliable. According to AMD Spec, you must
1012 * issue all erase commands within a specified
1013 * timeout. This has been seen to fail, especially
1014 * if printf()s are included (for debug)!!
1016 wait_for_DQ7_2(info, sect);
1020 /* re-enable interrupts if necessary */
1022 enable_interrupts();
1024 /* wait at least 80us - let's wait 1 ms */
1027 /* reset to read mode */
1028 addr = (CONFIG_SYS_FLASH_WORD_SIZE *) info->start[0];
1029 addr[0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xF0F0F0F0; /* reset bank */
1035 static int write_word_2(flash_info_t * info, ulong dest, ulong data)
1037 ulong *data_ptr = &data;
1038 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[0]);
1039 volatile CONFIG_SYS_FLASH_WORD_SIZE *dest2 = (CONFIG_SYS_FLASH_WORD_SIZE *)dest;
1040 volatile CONFIG_SYS_FLASH_WORD_SIZE *data2 = (CONFIG_SYS_FLASH_WORD_SIZE *)data_ptr;
1044 /* Check if Flash is (sufficiently) erased */
1045 if ((*((vu_long *)dest) & data) != data) {
1049 for (i = 0; i < 4 / sizeof(CONFIG_SYS_FLASH_WORD_SIZE); i++) {
1052 /* Disable interrupts which might cause a timeout here */
1053 flag = disable_interrupts();
1055 addr2[CONFIG_SYS_FLASH_CHAR_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xAAAAAAAA;
1056 addr2[CONFIG_SYS_FLASH_CHAR_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE) 0x55555555;
1057 addr2[CONFIG_SYS_FLASH_CHAR_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE) 0xA0A0A0A0;
1059 dest2[i] = data2[i];
1061 /* re-enable interrupts if necessary */
1063 enable_interrupts();
1065 /* data polling for D7 */
1066 start = get_timer(0);
1067 while ((dest2[i] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x80808080) !=
1068 (data2[i] & (CONFIG_SYS_FLASH_WORD_SIZE) 0x80808080)) {
1070 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
1079 #endif /* FLASH_BASE1_PRELIM */