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 */
37 /*-----------------------------------------------------------------------
40 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
41 static int write_word (flash_info_t *info, ulong dest, ulong data);
42 static void flash_get_offsets (ulong base, flash_info_t *info);
47 #define FLASH_WORD_SIZE unsigned char
53 #define FLASH_WORD_SIZE unsigned short
56 #ifdef CONFIG_WALNUT405
59 #define FLASH_WORD_SIZE unsigned char
62 /*-----------------------------------------------------------------------
65 unsigned long flash_init (void)
67 unsigned long size_b0, size_b1;
70 unsigned long base_b0, base_b1;
72 /* Init: no FLASHes known */
73 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
74 flash_info[i].flash_id = FLASH_UNKNOWN;
77 /* Static FLASH Bank configuration here - FIXME XXX */
79 size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
81 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
82 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
83 size_b0, size_b0<<20);
87 if (CFG_MAX_FLASH_BANKS == 1)
90 flash_get_offsets (FLASH_BASE0_PRELIM, &flash_info[0]);
92 /* Monitor protection ON by default */
93 (void)flash_protect(FLAG_PROTECT_SET,
95 FLASH_BASE0_PRELIM+monitor_flash_len-1,
98 flash_info[0].size = size_b0;
104 size_b1 = flash_get_size((vu_long *)FLASH_BASE1_PRELIM, &flash_info[1]);
106 /* Re-do sizing to get full correct info */
110 mtdcr(ebccfga, pb0cr);
111 pbcr = mfdcr(ebccfgd);
112 mtdcr(ebccfga, pb0cr);
114 pbcr = (pbcr & 0x0001ffff) | base_b1 | (((size_b1/1024/1024)-1)<<17);
115 mtdcr(ebccfgd, pbcr);
116 /* printf("pb1cr = %x\n", pbcr); */
121 mtdcr(ebccfga, pb1cr);
122 pbcr = mfdcr(ebccfgd);
123 mtdcr(ebccfga, pb1cr);
124 base_b0 = base_b1 - size_b0;
125 pbcr = (pbcr & 0x0001ffff) | base_b0 | (((size_b0/1024/1024)-1)<<17);
126 mtdcr(ebccfgd, pbcr);
127 /* printf("pb0cr = %x\n", pbcr); */
130 size_b0 = flash_get_size((vu_long *)base_b0, &flash_info[0]);
132 flash_get_offsets (base_b0, &flash_info[0]);
134 /* monitor protection ON by default */
135 (void)flash_protect(FLAG_PROTECT_SET,
136 base_b0+size_b0-monitor_flash_len,
141 /* Re-do sizing to get full correct info */
142 size_b1 = flash_get_size((vu_long *)base_b1, &flash_info[1]);
144 flash_get_offsets (base_b1, &flash_info[1]);
146 /* monitor protection ON by default */
147 (void)flash_protect(FLAG_PROTECT_SET,
148 base_b1+size_b1-monitor_flash_len,
151 /* monitor protection OFF by default (one is enough) */
152 (void)flash_protect(FLAG_PROTECT_CLEAR,
153 base_b0+size_b0-monitor_flash_len,
157 flash_info[1].flash_id = FLASH_UNKNOWN;
158 flash_info[1].sector_count = -1;
161 flash_info[0].size = size_b0;
162 flash_info[1].size = size_b1;
164 return (size_b0 + size_b1);
169 /*-----------------------------------------------------------------------
171 static void flash_get_offsets (ulong base, flash_info_t *info)
175 /* set up sector start address table */
176 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
177 (info->flash_id == FLASH_AM040)){
178 for (i = 0; i < info->sector_count; i++)
179 info->start[i] = base + (i * 0x00010000);
181 if (info->flash_id & FLASH_BTYPE) {
182 /* set sector offsets for bottom boot block type */
183 info->start[0] = base + 0x00000000;
184 info->start[1] = base + 0x00004000;
185 info->start[2] = base + 0x00006000;
186 info->start[3] = base + 0x00008000;
187 for (i = 4; i < info->sector_count; i++) {
188 info->start[i] = base + (i * 0x00010000) - 0x00030000;
191 /* set sector offsets for top boot block type */
192 i = info->sector_count - 1;
193 info->start[i--] = base + info->size - 0x00004000;
194 info->start[i--] = base + info->size - 0x00006000;
195 info->start[i--] = base + info->size - 0x00008000;
196 for (; i >= 0; i--) {
197 info->start[i] = base + i * 0x00010000;
203 /*-----------------------------------------------------------------------
205 void flash_print_info (flash_info_t *info)
211 volatile unsigned long *flash;
213 if (info->flash_id == FLASH_UNKNOWN) {
214 printf ("missing or unknown FLASH type\n");
218 switch (info->flash_id & FLASH_VENDMASK) {
219 case FLASH_MAN_AMD: printf ("AMD "); break;
220 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
221 case FLASH_MAN_SST: printf ("SST "); break;
222 default: printf ("Unknown Vendor "); break;
225 switch (info->flash_id & FLASH_TYPEMASK) {
226 case FLASH_AM040: printf ("AM29F040 (512 Kbit, uniform sector size)\n");
228 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
230 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
232 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
234 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
236 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
238 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
240 case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
242 case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
244 case FLASH_SST800A: printf ("SST39LF/VF800 (8 Mbit, uniform sector size)\n");
246 case FLASH_SST160A: printf ("SST39LF/VF160 (16 Mbit, uniform sector size)\n");
248 default: printf ("Unknown Chip Type\n");
252 printf (" Size: %ld KB in %d Sectors\n",
253 info->size >> 10, info->sector_count);
255 printf (" Sector Start Addresses:");
256 for (i=0; i<info->sector_count; ++i) {
258 * Check if whole sector is erased
260 if (i != (info->sector_count-1))
261 size = info->start[i+1] - info->start[i];
263 size = info->start[0] + info->size - info->start[i];
265 flash = (volatile unsigned long *)info->start[i];
266 size = size >> 2; /* divide by 4 for longword access */
267 for (k=0; k<size; k++)
269 if (*flash++ != 0xffffffff)
278 #if 0 /* test-only */
281 info->protect[i] ? " (RO)" : " "
283 printf (" %08lX%s%s",
286 info->protect[i] ? "RO " : " "
294 /*-----------------------------------------------------------------------
298 /*-----------------------------------------------------------------------
302 * The following code cannot be run from FLASH!
304 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
307 FLASH_WORD_SIZE value;
308 ulong base = (ulong)addr;
309 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)addr;
311 /* Write auto select command: read Manufacturer ID */
312 addr2[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
313 addr2[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
314 addr2[ADDR0] = (FLASH_WORD_SIZE)0x00900090;
323 case (FLASH_WORD_SIZE)AMD_MANUFACT:
324 info->flash_id = FLASH_MAN_AMD;
326 case (FLASH_WORD_SIZE)FUJ_MANUFACT:
327 info->flash_id = FLASH_MAN_FUJ;
329 case (FLASH_WORD_SIZE)SST_MANUFACT:
330 info->flash_id = FLASH_MAN_SST;
333 info->flash_id = FLASH_UNKNOWN;
334 info->sector_count = 0;
336 return (0); /* no or unknown flash */
340 value = addr2[0]; /* device ID */
341 /* printf("\ndev_code=%x\n", value); */
343 value = addr2[1]; /* device ID */
347 case (FLASH_WORD_SIZE)AMD_ID_F040B:
348 info->flash_id += FLASH_AM040;
349 info->sector_count = 8;
350 info->size = 0x0080000; /* => 512 ko */
352 case (FLASH_WORD_SIZE)AMD_ID_LV400T:
353 info->flash_id += FLASH_AM400T;
354 info->sector_count = 11;
355 info->size = 0x00080000;
356 break; /* => 0.5 MB */
358 case (FLASH_WORD_SIZE)AMD_ID_LV400B:
359 info->flash_id += FLASH_AM400B;
360 info->sector_count = 11;
361 info->size = 0x00080000;
362 break; /* => 0.5 MB */
364 case (FLASH_WORD_SIZE)AMD_ID_LV800T:
365 info->flash_id += FLASH_AM800T;
366 info->sector_count = 19;
367 info->size = 0x00100000;
370 case (FLASH_WORD_SIZE)AMD_ID_LV800B:
371 info->flash_id += FLASH_AM800B;
372 info->sector_count = 19;
373 info->size = 0x00100000;
376 case (FLASH_WORD_SIZE)AMD_ID_LV160T:
377 info->flash_id += FLASH_AM160T;
378 info->sector_count = 35;
379 info->size = 0x00200000;
382 case (FLASH_WORD_SIZE)AMD_ID_LV160B:
383 info->flash_id += FLASH_AM160B;
384 info->sector_count = 35;
385 info->size = 0x00200000;
387 #if 0 /* enable when device IDs are available */
388 case (FLASH_WORD_SIZE)AMD_ID_LV320T:
389 info->flash_id += FLASH_AM320T;
390 info->sector_count = 67;
391 info->size = 0x00400000;
394 case (FLASH_WORD_SIZE)AMD_ID_LV320B:
395 info->flash_id += FLASH_AM320B;
396 info->sector_count = 67;
397 info->size = 0x00400000;
400 case (FLASH_WORD_SIZE)SST_ID_xF800A:
401 info->flash_id += FLASH_SST800A;
402 info->sector_count = 16;
403 info->size = 0x00100000;
406 case (FLASH_WORD_SIZE)SST_ID_xF160A:
407 info->flash_id += FLASH_SST160A;
408 info->sector_count = 32;
409 info->size = 0x00200000;
413 info->flash_id = FLASH_UNKNOWN;
414 return (0); /* => no or unknown flash */
418 /* set up sector start address table */
419 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
420 (info->flash_id == FLASH_AM040)){
421 for (i = 0; i < info->sector_count; i++)
422 info->start[i] = base + (i * 0x00010000);
424 if (info->flash_id & FLASH_BTYPE) {
425 /* set sector offsets for bottom boot block type */
426 info->start[0] = base + 0x00000000;
427 info->start[1] = base + 0x00004000;
428 info->start[2] = base + 0x00006000;
429 info->start[3] = base + 0x00008000;
430 for (i = 4; i < info->sector_count; i++) {
431 info->start[i] = base + (i * 0x00010000) - 0x00030000;
434 /* set sector offsets for top boot block type */
435 i = info->sector_count - 1;
436 info->start[i--] = base + info->size - 0x00004000;
437 info->start[i--] = base + info->size - 0x00006000;
438 info->start[i--] = base + info->size - 0x00008000;
439 for (; i >= 0; i--) {
440 info->start[i] = base + i * 0x00010000;
445 /* check for protected sectors */
446 for (i = 0; i < info->sector_count; i++) {
447 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
448 /* D0 = 1 if protected */
450 addr2 = (volatile FLASH_WORD_SIZE *)(info->start[i]);
451 info->protect[i] = addr2[4] & 1;
453 addr2 = (volatile FLASH_WORD_SIZE *)(info->start[i]);
454 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
455 info->protect[i] = 0;
457 info->protect[i] = addr2[2] & 1;
462 * Prevent writes to uninitialized FLASH.
464 if (info->flash_id != FLASH_UNKNOWN) {
465 #if 0 /* test-only */
467 addr2 = (volatile unsigned char *)info->start[0];
470 addr2[ADDR0] = 0xF0; /* reset bank */
472 addr2 = (FLASH_WORD_SIZE *)info->start[0];
473 *addr2 = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
475 #else /* test-only */
476 addr2 = (FLASH_WORD_SIZE *)info->start[0];
477 *addr2 = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
478 #endif /* test-only */
484 int wait_for_DQ7(flash_info_t *info, int sect)
486 ulong start, now, last;
487 volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[sect]);
489 start = get_timer (0);
491 while ((addr[0] & (FLASH_WORD_SIZE)0x00800080) != (FLASH_WORD_SIZE)0x00800080) {
492 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
493 printf ("Timeout\n");
496 /* show that we're waiting */
497 if ((now - last) > 1000) { /* every second */
505 /*-----------------------------------------------------------------------
508 int flash_erase (flash_info_t *info, int s_first, int s_last)
510 volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[0]);
511 volatile FLASH_WORD_SIZE *addr2;
512 int flag, prot, sect, l_sect;
515 if ((s_first < 0) || (s_first > s_last)) {
516 if (info->flash_id == FLASH_UNKNOWN) {
517 printf ("- missing\n");
519 printf ("- no sectors to erase\n");
524 if (info->flash_id == FLASH_UNKNOWN) {
525 printf ("Can't erase unknown flash type - aborted\n");
530 for (sect=s_first; sect<=s_last; ++sect) {
531 if (info->protect[sect]) {
537 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 = (FLASH_WORD_SIZE *)(info->start[sect]);
552 printf("Erasing sector %p\n", addr2); /* CLH */
554 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
555 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
556 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
557 addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
558 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
559 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
560 addr2[0] = (FLASH_WORD_SIZE)0x00500050; /* block erase */
562 udelay(1000); /* wait 1 ms */
564 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
565 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
566 addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
567 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
568 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
569 addr2[0] = (FLASH_WORD_SIZE)0x00300030; /* sector erase */
573 * Wait for each sector to complete, it's more
574 * reliable. According to AMD Spec, you must
575 * issue all erase commands within a specified
576 * timeout. This has been seen to fail, especially
577 * if printf()s are included (for debug)!!
579 wait_for_DQ7(info, sect);
583 /* re-enable interrupts if necessary */
587 /* wait at least 80us - let's wait 1 ms */
592 * We wait for the last triggered sector
596 wait_for_DQ7(info, l_sect);
600 /* reset to read mode */
601 addr = (FLASH_WORD_SIZE *)info->start[0];
602 addr[0] = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
608 /*-----------------------------------------------------------------------
609 * Copy memory to flash, returns:
612 * 2 - Flash not erased
615 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
620 wp = (addr & ~3); /* get lower word aligned address */
623 * handle unaligned start bytes
625 if ((l = addr - wp) != 0) {
627 for (i=0, cp=wp; i<l; ++i, ++cp) {
628 data = (data << 8) | (*(uchar *)cp);
630 for (; i<4 && cnt>0; ++i) {
631 data = (data << 8) | *src++;
635 for (; cnt==0 && i<4; ++i, ++cp) {
636 data = (data << 8) | (*(uchar *)cp);
639 if ((rc = write_word(info, wp, data)) != 0) {
646 * handle word aligned part
650 for (i=0; i<4; ++i) {
651 data = (data << 8) | *src++;
653 if ((rc = write_word(info, wp, data)) != 0) {
665 * handle unaligned tail bytes
668 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
669 data = (data << 8) | *src++;
672 for (; i<4; ++i, ++cp) {
673 data = (data << 8) | (*(uchar *)cp);
676 return (write_word(info, wp, data));
679 /*-----------------------------------------------------------------------
680 * Write a word to Flash, returns:
683 * 2 - Flash not erased
685 static int write_word (flash_info_t * info, ulong dest, ulong data)
687 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *) (info->start[0]);
688 volatile FLASH_WORD_SIZE *dest2 = (FLASH_WORD_SIZE *) dest;
689 volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *) & data;
693 /* Check if Flash is (sufficiently) erased */
694 if ((*((volatile FLASH_WORD_SIZE *) dest) &
695 (FLASH_WORD_SIZE) data) != (FLASH_WORD_SIZE) data) {
699 for (i = 0; i < 4 / sizeof (FLASH_WORD_SIZE); i++) {
702 /* Disable interrupts which might cause a timeout here */
703 flag = disable_interrupts ();
705 addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
706 addr2[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
707 addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00A000A0;
711 /* re-enable interrupts if necessary */
713 enable_interrupts ();
715 /* data polling for D7 */
716 start = get_timer (0);
717 while ((dest2[i] & (FLASH_WORD_SIZE) 0x00800080) !=
718 (data2[i] & (FLASH_WORD_SIZE) 0x00800080)) {
720 if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
729 /*-----------------------------------------------------------------------