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
33 #include <asm/processor.h>
35 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
39 #define DEBUGF(x...) printf(x)
44 #define CFG_FLASH_CHAR_SIZE unsigned char
45 #define CFG_FLASH_CHAR_ADDR0 (0x0aaa)
46 #define CFG_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 < CFG_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,
88 CFG_MONITOR_BASE + CFG_MONITOR_LEN - 1,
90 #ifdef CFG_ENV_IS_IN_FLASH
91 (void)flash_protect(FLAG_PROTECT_SET, CFG_ENV_ADDR,
92 CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1,
94 (void)flash_protect(FLAG_PROTECT_SET, CFG_ENV_ADDR_REDUND,
95 CFG_ENV_ADDR_REDUND + CFG_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 CFG_FLASH_WORD_SIZE value;
303 ulong base = (ulong) addr;
304 volatile CFG_FLASH_WORD_SIZE *addr2 = (CFG_FLASH_WORD_SIZE *) addr;
306 DEBUGF("FLASH ADDR: %08x\n", (unsigned)addr);
308 /* Write auto select command: read Manufacturer ID */
309 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x00AA00AA;
310 addr2[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE) 0x00550055;
311 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x00900090;
315 DEBUGF("FLASH MANUFACT: %x\n", value);
318 case (CFG_FLASH_WORD_SIZE) AMD_MANUFACT:
319 info->flash_id = FLASH_MAN_AMD;
321 case (CFG_FLASH_WORD_SIZE) FUJ_MANUFACT:
322 info->flash_id = FLASH_MAN_FUJ;
324 case (CFG_FLASH_WORD_SIZE) SST_MANUFACT:
325 info->flash_id = FLASH_MAN_SST;
327 case (CFG_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 (CFG_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 (CFG_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 (CFG_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 (CFG_FLASH_WORD_SIZE) AMD_ID_F016D:
360 info->flash_id += FLASH_AMD016;
361 info->sector_count = 32;
362 info->size = 0x00200000;
365 case (CFG_FLASH_WORD_SIZE) AMD_ID_LV033C:
366 info->flash_id += FLASH_AMDLV033C;
367 info->sector_count = 64;
368 info->size = 0x00400000;
371 case (CFG_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 (CFG_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 (CFG_FLASH_WORD_SIZE) AMD_ID_LV800T:
384 info->flash_id += FLASH_AM800T;
385 info->sector_count = 19;
386 info->size = 0x00100000;
389 case (CFG_FLASH_WORD_SIZE) AMD_ID_LV800B:
390 info->flash_id += FLASH_AM800B;
391 info->sector_count = 19;
392 info->size = 0x00100000;
395 case (CFG_FLASH_WORD_SIZE) AMD_ID_LV160T:
396 info->flash_id += FLASH_AM160T;
397 info->sector_count = 35;
398 info->size = 0x00200000;
401 case (CFG_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 CFG_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[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0xAAAAAAAA;
454 addr2[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE) 0x55555555;
455 addr2[CFG_FLASH_ADDR0] = (CFG_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] = (CFG_FLASH_WORD_SIZE) 0x00F000F0;
470 static int wait_for_DQ7_1(flash_info_t * info, int sect)
472 ulong start, now, last;
473 volatile CFG_FLASH_WORD_SIZE *addr =
474 (CFG_FLASH_WORD_SIZE *) (info->start[sect]);
476 start = get_timer(0);
478 while ((addr[0] & (CFG_FLASH_WORD_SIZE) 0x00800080) !=
479 (CFG_FLASH_WORD_SIZE) 0x00800080) {
480 if ((now = get_timer(start)) > CFG_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 CFG_FLASH_WORD_SIZE *addr = (CFG_FLASH_WORD_SIZE *) (info->start[0]);
513 volatile CFG_FLASH_WORD_SIZE *addr2;
514 int flag, prot, sect, l_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",
547 /* Disable interrupts which might cause a timeout here */
548 flag = disable_interrupts();
550 /* Start erase on unprotected sectors */
551 for (sect = s_first; sect <= s_last; sect++) {
552 if (info->protect[sect] == 0) { /* not protected */
553 addr2 = (CFG_FLASH_WORD_SIZE *) (info->start[sect]);
555 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
556 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x00AA00AA;
557 addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE) 0x00550055;
558 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x00800080;
559 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x00AA00AA;
560 addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE) 0x00550055;
561 addr2[0] = (CFG_FLASH_WORD_SIZE) 0x00500050; /* block erase */
562 for (i = 0; i < 50; i++)
563 udelay(1000); /* wait 1 ms */
565 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x00AA00AA;
566 addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE) 0x00550055;
567 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x00800080;
568 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x00AA00AA;
569 addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE) 0x00550055;
570 addr2[0] = (CFG_FLASH_WORD_SIZE) 0x00300030; /* sector erase */
574 * Wait for each sector to complete, it's more
575 * reliable. According to AMD Spec, you must
576 * issue all erase commands within a specified
577 * timeout. This has been seen to fail, especially
578 * if printf()s are included (for debug)!!
580 wait_for_DQ7_1(info, sect);
584 /* re-enable interrupts if necessary */
588 /* wait at least 80us - let's wait 1 ms */
591 /* reset to read mode */
592 addr = (CFG_FLASH_WORD_SIZE *) info->start[0];
593 addr[0] = (CFG_FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
599 /*-----------------------------------------------------------------------
600 * Copy memory to flash, returns:
603 * 2 - Flash not erased
605 int write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
610 wp = (addr & ~3); /* get lower word aligned address */
613 * handle unaligned start bytes
615 if ((l = addr - wp) != 0) {
617 for (i = 0, cp = wp; i < l; ++i, ++cp) {
618 data = (data << 8) | (*(uchar *) cp);
620 for (; i < 4 && cnt > 0; ++i) {
621 data = (data << 8) | *src++;
625 for (; cnt == 0 && i < 4; ++i, ++cp) {
626 data = (data << 8) | (*(uchar *) cp);
629 if ((rc = write_word(info, wp, data)) != 0) {
636 * handle word aligned part
640 for (i = 0; i < 4; ++i) {
641 data = (data << 8) | *src++;
643 if ((rc = write_word(info, wp, data)) != 0) {
655 * handle unaligned tail bytes
658 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
659 data = (data << 8) | *src++;
662 for (; i < 4; ++i, ++cp) {
663 data = (data << 8) | (*(uchar *) cp);
666 return (write_word(info, wp, data));
669 /*-----------------------------------------------------------------------
670 * Copy memory to flash, returns:
673 * 2 - Flash not erased
675 #ifdef FLASH_BASE1_PRELIM
676 static int write_word(flash_info_t * info, ulong dest, ulong data)
678 if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
679 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
680 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMLV128U) ||
681 ((info->flash_id & FLASH_TYPEMASK) == FLASH_S29GL128N) ||
682 ((info->flash_id & FLASH_TYPEMASK) == FLASH_STMW320DT)) {
683 return write_word_2(info, dest, data);
685 return write_word_1(info, dest, data);
689 static int write_word_1(flash_info_t * info, ulong dest, ulong data)
691 static int write_word(flash_info_t * info, ulong dest, ulong data)
694 volatile CFG_FLASH_WORD_SIZE *addr2 = (CFG_FLASH_WORD_SIZE *) (info->start[0]);
695 volatile CFG_FLASH_WORD_SIZE *dest2 = (CFG_FLASH_WORD_SIZE *) dest;
696 volatile CFG_FLASH_WORD_SIZE *data2 = (CFG_FLASH_WORD_SIZE *) & data;
700 /* Check if Flash is (sufficiently) erased */
701 if ((*((vu_long *)dest) & data) != data) {
705 for (i = 0; i < 4 / sizeof(CFG_FLASH_WORD_SIZE); i++) {
708 /* Disable interrupts which might cause a timeout here */
709 flag = disable_interrupts();
711 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x00AA00AA;
712 addr2[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE) 0x00550055;
713 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x00A000A0;
717 /* re-enable interrupts if necessary */
721 /* data polling for D7 */
722 start = get_timer(0);
723 while ((dest2[i] & (CFG_FLASH_WORD_SIZE) 0x00800080) !=
724 (data2[i] & (CFG_FLASH_WORD_SIZE) 0x00800080)) {
726 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
735 #ifdef FLASH_BASE1_PRELIM
738 * The following code cannot be run from FLASH!
740 static ulong flash_get_size_2(vu_long * addr, flash_info_t * info)
743 CFG_FLASH_CHAR_SIZE value;
744 ulong base = (ulong) addr;
745 volatile CFG_FLASH_WORD_SIZE *addr2 = (CFG_FLASH_WORD_SIZE *) addr;
747 DEBUGF("FLASH ADDR: %08x\n", (unsigned)addr);
749 /* Write auto select command: read Manufacturer ID */
750 addr2[CFG_FLASH_CHAR_ADDR0] = (CFG_FLASH_WORD_SIZE) 0xAAAAAAAA;
751 addr2[CFG_FLASH_CHAR_ADDR1] = (CFG_FLASH_WORD_SIZE) 0x55555555;
752 addr2[CFG_FLASH_CHAR_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x90909090;
755 value = (CFG_FLASH_CHAR_SIZE)addr2[0];
756 DEBUGF("FLASH MANUFACT: %x\n", value);
759 case (CFG_FLASH_CHAR_SIZE) AMD_MANUFACT:
760 info->flash_id = FLASH_MAN_AMD;
762 case (CFG_FLASH_CHAR_SIZE) FUJ_MANUFACT:
763 info->flash_id = FLASH_MAN_FUJ;
765 case (CFG_FLASH_CHAR_SIZE) SST_MANUFACT:
766 info->flash_id = FLASH_MAN_SST;
768 case (CFG_FLASH_CHAR_SIZE) STM_MANUFACT:
769 info->flash_id = FLASH_MAN_STM;
772 info->flash_id = FLASH_UNKNOWN;
773 info->sector_count = 0;
775 return 0; /* no or unknown flash */
778 value = (CFG_FLASH_CHAR_SIZE)addr2[2]; /* device ID */
779 DEBUGF("\nFLASH DEVICEID: %x\n", value);
782 case (CFG_FLASH_CHAR_SIZE) AMD_ID_LV040B:
783 info->flash_id += FLASH_AM040;
784 info->sector_count = 8;
785 info->size = 0x0080000; /* => 512 ko */
788 case (CFG_FLASH_CHAR_SIZE) AMD_ID_F040B:
789 info->flash_id += FLASH_AM040;
790 info->sector_count = 8;
791 info->size = 0x0080000; /* => 512 ko */
794 case (CFG_FLASH_CHAR_SIZE) STM_ID_M29W040B:
795 info->flash_id += FLASH_AM040;
796 info->sector_count = 8;
797 info->size = 0x0080000; /* => 512 ko */
800 case (CFG_FLASH_CHAR_SIZE) AMD_ID_F016D:
801 info->flash_id += FLASH_AMD016;
802 info->sector_count = 32;
803 info->size = 0x00200000;
806 case (CFG_FLASH_CHAR_SIZE) AMD_ID_LV033C:
807 info->flash_id += FLASH_AMDLV033C;
808 info->sector_count = 64;
809 info->size = 0x00400000;
812 case (CFG_FLASH_CHAR_SIZE) AMD_ID_LV400T:
813 info->flash_id += FLASH_AM400T;
814 info->sector_count = 11;
815 info->size = 0x00080000;
816 break; /* => 0.5 MB */
818 case (CFG_FLASH_CHAR_SIZE) AMD_ID_LV400B:
819 info->flash_id += FLASH_AM400B;
820 info->sector_count = 11;
821 info->size = 0x00080000;
822 break; /* => 0.5 MB */
824 case (CFG_FLASH_CHAR_SIZE) AMD_ID_LV800T:
825 info->flash_id += FLASH_AM800T;
826 info->sector_count = 19;
827 info->size = 0x00100000;
830 case (CFG_FLASH_CHAR_SIZE) AMD_ID_LV800B:
831 info->flash_id += FLASH_AM800B;
832 info->sector_count = 19;
833 info->size = 0x00100000;
836 case (CFG_FLASH_CHAR_SIZE) AMD_ID_LV160T:
837 info->flash_id += FLASH_AM160T;
838 info->sector_count = 35;
839 info->size = 0x00200000;
842 case (CFG_FLASH_CHAR_SIZE) AMD_ID_LV160B:
843 info->flash_id += FLASH_AM160B;
844 info->sector_count = 35;
845 info->size = 0x00200000;
847 case (CFG_FLASH_CHAR_SIZE) AMD_ID_MIRROR:
848 if ((CFG_FLASH_CHAR_SIZE)addr2[0x1c] == (CFG_FLASH_CHAR_SIZE)AMD_ID_LV128U_2
849 && (CFG_FLASH_CHAR_SIZE)addr2[0x1e] == (CFG_FLASH_CHAR_SIZE)AMD_ID_LV128U_3) {
850 info->flash_id += FLASH_AMLV128U;
851 info->sector_count = 256;
852 info->size = 0x01000000;
853 } else if ((CFG_FLASH_CHAR_SIZE)addr2[0x1c] == (CFG_FLASH_CHAR_SIZE)AMD_ID_GL128N_2
854 && (CFG_FLASH_CHAR_SIZE)addr2[0x1e] == (CFG_FLASH_CHAR_SIZE)AMD_ID_GL128N_3 ) {
855 info->flash_id += FLASH_S29GL128N;
856 info->sector_count = 128;
857 info->size = 0x01000000;
860 info->flash_id = FLASH_UNKNOWN;
864 info->flash_id = FLASH_UNKNOWN;
865 return 0; /* => no or unknown flash */
868 /* set up sector start address table */
869 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
870 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) ||
871 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMD016)) {
872 for (i = 0; i < info->sector_count; i++)
873 info->start[i] = base + (i * 0x00010000);
874 } else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMLV128U) {
875 for (i = 0; i < info->sector_count; i++)
876 info->start[i] = base + (i * 0x00010000);
877 } else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_S29GL128N ) {
878 for (i = 0; i < info->sector_count; i++)
879 info->start[i] = base + (i * 0x00020000);
881 if (info->flash_id & FLASH_BTYPE) {
882 /* set sector offsets for bottom boot block type */
883 info->start[0] = base + 0x00000000;
884 info->start[1] = base + 0x00004000;
885 info->start[2] = base + 0x00006000;
886 info->start[3] = base + 0x00008000;
887 for (i = 4; i < info->sector_count; i++) {
889 base + (i * 0x00010000) - 0x00030000;
892 /* set sector offsets for top boot block type */
893 i = info->sector_count - 1;
894 info->start[i--] = base + info->size - 0x00004000;
895 info->start[i--] = base + info->size - 0x00006000;
896 info->start[i--] = base + info->size - 0x00008000;
897 for (; i >= 0; i--) {
898 info->start[i] = base + i * 0x00010000;
903 /* check for protected sectors */
904 for (i = 0; i < info->sector_count; i++) {
905 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
906 /* D0 = 1 if protected */
907 addr2 = (volatile CFG_FLASH_WORD_SIZE *)(info->start[i]);
909 /* For AMD29033C flash we need to resend the command of *
910 * reading flash protection for upper 8 Mb of flash */
912 addr2[CFG_FLASH_CHAR_ADDR0] = (CFG_FLASH_WORD_SIZE) 0xAAAAAAAA;
913 addr2[CFG_FLASH_CHAR_ADDR1] = (CFG_FLASH_WORD_SIZE) 0x55555555;
914 addr2[CFG_FLASH_CHAR_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x90909090;
917 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
918 info->protect[i] = 0;
920 info->protect[i] = (CFG_FLASH_CHAR_SIZE)addr2[4] & 1;
923 /* issue bank reset to return to read mode */
924 addr2[0] = (CFG_FLASH_WORD_SIZE) 0xF0F0F0F0;
928 static int wait_for_DQ7_2(flash_info_t * info, int sect)
930 ulong start, now, last;
931 volatile CFG_FLASH_WORD_SIZE *addr =
932 (CFG_FLASH_WORD_SIZE *) (info->start[sect]);
934 start = get_timer(0);
936 while (((CFG_FLASH_WORD_SIZE)addr[0] & (CFG_FLASH_WORD_SIZE) 0x80808080) !=
937 (CFG_FLASH_WORD_SIZE) 0x80808080) {
938 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
942 /* show that we're waiting */
943 if ((now - last) > 1000) { /* every second */
951 static int flash_erase_2(flash_info_t * info, int s_first, int s_last)
953 volatile CFG_FLASH_WORD_SIZE *addr = (CFG_FLASH_WORD_SIZE *) (info->start[0]);
954 volatile CFG_FLASH_WORD_SIZE *addr2;
955 int flag, prot, sect, l_sect;
958 if ((s_first < 0) || (s_first > s_last)) {
959 if (info->flash_id == FLASH_UNKNOWN) {
960 printf("- missing\n");
962 printf("- no sectors to erase\n");
967 if (info->flash_id == FLASH_UNKNOWN) {
968 printf("Can't erase unknown flash type - aborted\n");
973 for (sect = s_first; sect <= s_last; ++sect) {
974 if (info->protect[sect]) {
980 printf("- Warning: %d protected sectors will not be erased!\n",
988 /* Disable interrupts which might cause a timeout here */
989 flag = disable_interrupts();
991 /* Start erase on unprotected sectors */
992 for (sect = s_first; sect <= s_last; sect++) {
993 if (info->protect[sect] == 0) { /* not protected */
994 addr2 = (CFG_FLASH_WORD_SIZE *) (info->start[sect]);
996 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
997 addr[CFG_FLASH_CHAR_ADDR0] = (CFG_FLASH_WORD_SIZE) 0xAAAAAAAA;
998 addr[CFG_FLASH_CHAR_ADDR1] = (CFG_FLASH_WORD_SIZE) 0x55555555;
999 addr[CFG_FLASH_CHAR_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x80808080;
1000 addr[CFG_FLASH_CHAR_ADDR0] = (CFG_FLASH_WORD_SIZE) 0xAAAAAAAA;
1001 addr[CFG_FLASH_CHAR_ADDR1] = (CFG_FLASH_WORD_SIZE) 0x55555555;
1002 addr2[0] = (CFG_FLASH_WORD_SIZE) 0x50505050; /* block erase */
1003 for (i = 0; i < 50; i++)
1004 udelay(1000); /* wait 1 ms */
1006 addr[CFG_FLASH_CHAR_ADDR0] = (CFG_FLASH_WORD_SIZE) 0xAAAAAAAA;
1007 addr[CFG_FLASH_CHAR_ADDR1] = (CFG_FLASH_WORD_SIZE) 0x55555555;
1008 addr[CFG_FLASH_CHAR_ADDR0] = (CFG_FLASH_WORD_SIZE) 0x80808080;
1009 addr[CFG_FLASH_CHAR_ADDR0] = (CFG_FLASH_WORD_SIZE) 0xAAAAAAAA;
1010 addr[CFG_FLASH_CHAR_ADDR1] = (CFG_FLASH_WORD_SIZE) 0x55555555;
1011 addr2[0] = (CFG_FLASH_WORD_SIZE) 0x30303030; /* sector erase */
1015 * Wait for each sector to complete, it's more
1016 * reliable. According to AMD Spec, you must
1017 * issue all erase commands within a specified
1018 * timeout. This has been seen to fail, especially
1019 * if printf()s are included (for debug)!!
1021 wait_for_DQ7_2(info, sect);
1025 /* re-enable interrupts if necessary */
1027 enable_interrupts();
1029 /* wait at least 80us - let's wait 1 ms */
1032 /* reset to read mode */
1033 addr = (CFG_FLASH_WORD_SIZE *) info->start[0];
1034 addr[0] = (CFG_FLASH_WORD_SIZE) 0xF0F0F0F0; /* reset bank */
1040 static int write_word_2(flash_info_t * info, ulong dest, ulong data)
1042 volatile CFG_FLASH_WORD_SIZE *addr2 = (CFG_FLASH_WORD_SIZE *) (info->start[0]);
1043 volatile CFG_FLASH_WORD_SIZE *dest2 = (CFG_FLASH_WORD_SIZE *) dest;
1044 volatile CFG_FLASH_WORD_SIZE *data2 = (CFG_FLASH_WORD_SIZE *) & data;
1048 /* Check if Flash is (sufficiently) erased */
1049 if ((*((vu_long *)dest) & data) != data) {
1053 for (i = 0; i < 4 / sizeof(CFG_FLASH_WORD_SIZE); i++) {
1056 /* Disable interrupts which might cause a timeout here */
1057 flag = disable_interrupts();
1059 addr2[CFG_FLASH_CHAR_ADDR0] = (CFG_FLASH_WORD_SIZE) 0xAAAAAAAA;
1060 addr2[CFG_FLASH_CHAR_ADDR1] = (CFG_FLASH_WORD_SIZE) 0x55555555;
1061 addr2[CFG_FLASH_CHAR_ADDR0] = (CFG_FLASH_WORD_SIZE) 0xA0A0A0A0;
1063 dest2[i] = data2[i];
1065 /* re-enable interrupts if necessary */
1067 enable_interrupts();
1069 /* data polling for D7 */
1070 start = get_timer(0);
1071 while ((dest2[i] & (CFG_FLASH_WORD_SIZE) 0x80808080) !=
1072 (data2[i] & (CFG_FLASH_WORD_SIZE) 0x80808080)) {
1074 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
1083 #endif /* FLASH_BASE1_PRELIM */