2 * (C) Copyright 2000, 2001
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 * - adapted for pip405, Denis Peter, MPL AG Switzerland
40 #include <asm/processor.h>
41 #include "common_util.h"
43 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
44 /*-----------------------------------------------------------------------
47 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
48 static int write_word (flash_info_t *info, ulong dest, ulong data);
50 void unlock_intel_sectors(flash_info_t *info,ulong addr,ulong cnt);
56 #define FLASH_WORD_SIZE unsigned short
62 #define FLASH_WORD_SIZE unsigned short
68 /*-----------------------------------------------------------------------
72 unsigned long flash_init (void)
74 unsigned long size_b0, size_b1;
77 /* Since we are relocated, we can set-up the CS finally */
79 /* get and display boot mode */
82 printf("(PCI Boot %s Map) ",(i & BOOT_MPS) ?
85 printf("(%s Boot) ",(i & BOOT_MPS) ?
87 /* Init: no FLASHes known */
88 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
89 flash_info[i].flash_id = FLASH_UNKNOWN;
92 /* Static FLASH Bank configuration here - FIXME XXX */
94 size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
96 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
97 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
98 size_b0, size_b0<<20);
100 /* protect the bootloader */
101 /* Monitor protection ON by default */
102 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
103 flash_protect(FLAG_PROTECT_SET,
105 CFG_MONITOR_BASE+monitor_flash_len-1,
108 /* protect reset vector */
109 flash_info[0].protect[flash_info[0].sector_count-1] = 1;
111 flash_info[0].size = size_b0;
113 /* include this if you want to test if
114 the relocation has be done ok.
115 This will disable both Chipselects */
116 mtdcr (ebccfga, pb0cr);
118 mtdcr (ebccfga, pb1cr);
120 printf("CS0 & CS1 switched off for test\n");
126 /*-----------------------------------------------------------------------
128 void flash_print_info (flash_info_t *info)
134 volatile unsigned long *flash;
136 if (info->flash_id == FLASH_UNKNOWN) {
137 printf ("missing or unknown FLASH type\n");
141 switch (info->flash_id & FLASH_VENDMASK) {
142 case FLASH_MAN_AMD: printf ("AMD "); break;
143 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
144 case FLASH_MAN_SST: printf ("SST "); break;
145 case FLASH_MAN_INTEL: printf ("Intel "); break;
146 default: printf ("Unknown Vendor "); break;
149 switch (info->flash_id & FLASH_TYPEMASK) {
150 case FLASH_AM040: printf ("AM29F040 (512 Kbit, uniform sector size)\n");
152 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
154 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
156 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
158 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
160 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
162 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
164 case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
166 case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
168 case FLASH_SST800A: printf ("SST39LF/VF800 (8 Mbit, uniform sector size)\n");
170 case FLASH_SST160A: printf ("SST39LF/VF160 (16 Mbit, uniform sector size)\n");
172 case FLASH_INTEL320T: printf ("TE28F320C3 (32 Mbit, top sector size)\n");
174 default: printf ("Unknown Chip Type\n");
178 printf (" Size: %ld KB in %d Sectors\n",
179 info->size >> 10, info->sector_count);
181 printf (" Sector Start Addresses:");
182 for (i=0; i<info->sector_count; ++i) {
184 * Check if whole sector is erased
186 if (i != (info->sector_count-1))
187 size = info->start[i+1] - info->start[i];
189 size = info->start[0] + info->size - info->start[i];
191 flash = (volatile unsigned long *)info->start[i];
192 size = size >> 2; /* divide by 4 for longword access */
193 for (k=0; k<size; k++) {
194 if (*flash++ != 0xffffffff) {
201 printf (" %08lX%s%s",
204 info->protect[i] ? "RO " : " ");
209 /*-----------------------------------------------------------------------
213 /*-----------------------------------------------------------------------
217 * The following code cannot be run from FLASH!
219 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
222 FLASH_WORD_SIZE value;
223 ulong base = (ulong)addr;
224 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)addr;
226 /* Write auto select command: read Manufacturer ID */
227 addr2[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
228 addr2[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
229 addr2[ADDR0] = (FLASH_WORD_SIZE)0x00900090;
232 /* printf("flash_get_size value: %x\n",value); */
234 case (FLASH_WORD_SIZE)AMD_MANUFACT:
235 info->flash_id = FLASH_MAN_AMD;
237 case (FLASH_WORD_SIZE)FUJ_MANUFACT:
238 info->flash_id = FLASH_MAN_FUJ;
240 case (FLASH_WORD_SIZE)INTEL_MANUFACT:
241 info->flash_id = FLASH_MAN_INTEL;
243 case (FLASH_WORD_SIZE)SST_MANUFACT:
244 info->flash_id = FLASH_MAN_SST;
247 info->flash_id = FLASH_UNKNOWN;
248 info->sector_count = 0;
250 return (0); /* no or unknown flash */
252 value = addr2[1]; /* device ID */
253 /* printf("Device value %x\n",value); */
255 case (FLASH_WORD_SIZE)AMD_ID_F040B:
256 info->flash_id += FLASH_AM040;
257 info->sector_count = 8;
258 info->size = 0x0080000; /* => 512 ko */
260 case (FLASH_WORD_SIZE)AMD_ID_LV400T:
261 info->flash_id += FLASH_AM400T;
262 info->sector_count = 11;
263 info->size = 0x00080000;
264 break; /* => 0.5 MB */
266 case (FLASH_WORD_SIZE)AMD_ID_LV400B:
267 info->flash_id += FLASH_AM400B;
268 info->sector_count = 11;
269 info->size = 0x00080000;
270 break; /* => 0.5 MB */
272 case (FLASH_WORD_SIZE)AMD_ID_LV800T:
273 info->flash_id += FLASH_AM800T;
274 info->sector_count = 19;
275 info->size = 0x00100000;
278 case (FLASH_WORD_SIZE)AMD_ID_LV800B:
279 info->flash_id += FLASH_AM800B;
280 info->sector_count = 19;
281 info->size = 0x00100000;
284 case (FLASH_WORD_SIZE)AMD_ID_LV160T:
285 info->flash_id += FLASH_AM160T;
286 info->sector_count = 35;
287 info->size = 0x00200000;
290 case (FLASH_WORD_SIZE)AMD_ID_LV160B:
291 info->flash_id += FLASH_AM160B;
292 info->sector_count = 35;
293 info->size = 0x00200000;
295 #if 0 /* enable when device IDs are available */
296 case (FLASH_WORD_SIZE)AMD_ID_LV320T:
297 info->flash_id += FLASH_AM320T;
298 info->sector_count = 67;
299 info->size = 0x00400000;
302 case (FLASH_WORD_SIZE)AMD_ID_LV320B:
303 info->flash_id += FLASH_AM320B;
304 info->sector_count = 67;
305 info->size = 0x00400000;
308 case (FLASH_WORD_SIZE)SST_ID_xF800A:
309 info->flash_id += FLASH_SST800A;
310 info->sector_count = 16;
311 info->size = 0x00100000;
313 case (FLASH_WORD_SIZE)INTEL_ID_28F320C3T:
314 info->flash_id += FLASH_INTEL320T;
315 info->sector_count = 71;
316 info->size = 0x00400000;
320 case (FLASH_WORD_SIZE)SST_ID_xF160A:
321 info->flash_id += FLASH_SST160A;
322 info->sector_count = 32;
323 info->size = 0x00200000;
327 info->flash_id = FLASH_UNKNOWN;
328 return (0); /* => no or unknown flash */
332 /* set up sector start address table */
333 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
334 (info->flash_id == FLASH_AM040)){
335 for (i = 0; i < info->sector_count; i++)
336 info->start[i] = base + (i * 0x00010000);
339 if (info->flash_id & FLASH_BTYPE) {
340 /* set sector offsets for bottom boot block type */
341 info->start[0] = base + 0x00000000;
342 info->start[1] = base + 0x00004000;
343 info->start[2] = base + 0x00006000;
344 info->start[3] = base + 0x00008000;
345 for (i = 4; i < info->sector_count; i++)
346 info->start[i] = base + (i * 0x00010000) - 0x00030000;
349 /* set sector offsets for top boot block type */
350 i = info->sector_count - 1;
351 if(info->sector_count==71) {
353 info->start[i--] = base + info->size - 0x00002000;
354 info->start[i--] = base + info->size - 0x00004000;
355 info->start[i--] = base + info->size - 0x00006000;
356 info->start[i--] = base + info->size - 0x00008000;
357 info->start[i--] = base + info->size - 0x0000A000;
358 info->start[i--] = base + info->size - 0x0000C000;
359 info->start[i--] = base + info->size - 0x0000E000;
361 info->start[i] = base + i * 0x000010000;
364 info->start[i--] = base + info->size - 0x00004000;
365 info->start[i--] = base + info->size - 0x00006000;
366 info->start[i--] = base + info->size - 0x00008000;
368 info->start[i] = base + i * 0x00010000;
373 /* check for protected sectors */
374 for (i = 0; i < info->sector_count; i++) {
375 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
376 /* D0 = 1 if protected */
377 addr2 = (volatile FLASH_WORD_SIZE *)(info->start[i]);
378 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL)
379 info->protect[i] = 0;
381 info->protect[i] = addr2[2] & 1;
385 * Prevent writes to uninitialized FLASH.
387 if (info->flash_id != FLASH_UNKNOWN) {
388 addr2 = (FLASH_WORD_SIZE *)info->start[0];
389 *addr2 = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
395 int wait_for_DQ7(flash_info_t *info, int sect)
397 ulong start, now, last;
398 volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[sect]);
400 start = get_timer (0);
402 while ((addr[0] & (FLASH_WORD_SIZE)0x00800080) != (FLASH_WORD_SIZE)0x00800080) {
403 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
404 printf ("Timeout\n");
407 /* show that we're waiting */
408 if ((now - last) > 1000) { /* every second */
416 int intel_wait_for_DQ7(flash_info_t *info, int sect)
418 ulong start, now, last;
419 volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[sect]);
421 start = get_timer (0);
423 while ((addr[0] & (FLASH_WORD_SIZE)0x00800080) != (FLASH_WORD_SIZE)0x00800080) {
424 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
425 printf ("Timeout\n");
428 /* show that we're waiting */
429 if ((now - last) > 1000) { /* every second */
434 addr[0]=(FLASH_WORD_SIZE)0x00500050;
438 /*-----------------------------------------------------------------------
441 int flash_erase (flash_info_t *info, int s_first, int s_last)
443 volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[0]);
444 volatile FLASH_WORD_SIZE *addr2;
445 int flag, prot, sect, l_sect;
449 if ((s_first < 0) || (s_first > s_last)) {
450 if (info->flash_id == FLASH_UNKNOWN) {
451 printf ("- missing\n");
453 printf ("- no sectors to erase\n");
458 if (info->flash_id == FLASH_UNKNOWN) {
459 printf ("Can't erase unknown flash type - aborted\n");
464 for (sect=s_first; sect<=s_last; ++sect) {
465 if (info->protect[sect]) {
471 printf ("- Warning: %d protected sectors will not be erased!\n",
479 /* Disable interrupts which might cause a timeout here */
480 flag = disable_interrupts();
482 /* Start erase on unprotected sectors */
483 for (sect = s_first; sect<=s_last; sect++) {
484 if (info->protect[sect] == 0) { /* not protected */
485 addr2 = (FLASH_WORD_SIZE *)(info->start[sect]);
486 /* printf("Erasing sector %p\n", addr2); */ /* CLH */
487 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
488 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
489 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
490 addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
491 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
492 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
493 addr2[0] = (FLASH_WORD_SIZE)0x00500050; /* block erase */
495 udelay(1000); /* wait 1 ms */
496 wait_for_DQ7(info, sect);
499 if((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL){
500 addr2[0] = (FLASH_WORD_SIZE)0x00600060; /* unlock sector */
501 addr2[0] = (FLASH_WORD_SIZE)0x00D000D0; /* sector erase */
502 intel_wait_for_DQ7(info, sect);
503 addr2[0] = (FLASH_WORD_SIZE)0x00200020; /* sector erase */
504 addr2[0] = (FLASH_WORD_SIZE)0x00D000D0; /* sector erase */
505 intel_wait_for_DQ7(info, sect);
508 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
509 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
510 addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
511 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
512 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
513 addr2[0] = (FLASH_WORD_SIZE)0x00300030; /* sector erase */
514 wait_for_DQ7(info, sect);
519 * Wait for each sector to complete, it's more
520 * reliable. According to AMD Spec, you must
521 * issue all erase commands within a specified
522 * timeout. This has been seen to fail, especially
523 * if printf()s are included (for debug)!!
525 /* wait_for_DQ7(info, sect); */
529 /* re-enable interrupts if necessary */
533 /* wait at least 80us - let's wait 1 ms */
538 * We wait for the last triggered sector
542 wait_for_DQ7(info, l_sect);
546 /* reset to read mode */
547 addr = (FLASH_WORD_SIZE *)info->start[0];
548 addr[0] = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
555 void unlock_intel_sectors(flash_info_t *info,ulong addr,ulong cnt)
558 volatile FLASH_WORD_SIZE *addr2;
561 for(i=info->sector_count-1;i>0;i--)
563 if(addr>=info->start[i])
567 addr2 = (FLASH_WORD_SIZE *)(info->start[i]);
568 addr2[0] = (FLASH_WORD_SIZE)0x00600060; /* unlock sector setup */
569 addr2[0] = (FLASH_WORD_SIZE)0x00D000D0; /* unlock sector */
570 intel_wait_for_DQ7(info, i);
572 c-=(info->start[i]-info->start[i-1]);
577 /*-----------------------------------------------------------------------
578 * Copy memory to flash, returns:
581 * 2 - Flash not erased
584 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
589 if((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL){
590 unlock_intel_sectors(info,addr,cnt);
592 wp = (addr & ~3); /* get lower word aligned address */
594 * handle unaligned start bytes
596 if ((l = addr - wp) != 0) {
598 for (i=0, cp=wp; i<l; ++i, ++cp) {
599 data = (data << 8) | (*(uchar *)cp);
601 for (; i<4 && cnt>0; ++i) {
602 data = (data << 8) | *src++;
606 for (; cnt==0 && i<4; ++i, ++cp) {
607 data = (data << 8) | (*(uchar *)cp);
610 if ((rc = write_word(info, wp, data)) != 0) {
617 * handle word aligned part
621 for (i=0; i<4; ++i) {
622 data = (data << 8) | *src++;
624 if ((rc = write_word(info, wp, data)) != 0) {
628 if((wp % 0x10000)==0)
629 printf("."); /* show Progress */
638 * handle unaligned tail bytes
641 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
642 data = (data << 8) | *src++;
645 for (; i<4; ++i, ++cp) {
646 data = (data << 8) | (*(uchar *)cp);
648 rc=write_word(info, wp, data);
652 /*-----------------------------------------------------------------------
653 * Write a word to Flash, returns:
656 * 2 - Flash not erased
658 static FLASH_WORD_SIZE *read_val = (FLASH_WORD_SIZE *)0x200000;
660 static int write_word (flash_info_t *info, ulong dest, ulong data)
662 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)(info->start[0]);
663 volatile FLASH_WORD_SIZE *dest2 = (FLASH_WORD_SIZE *)dest;
664 volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *)&data;
669 /* Check if Flash is (sufficiently) erased */
670 if ((*((volatile FLASH_WORD_SIZE *)dest) &
671 (FLASH_WORD_SIZE)data) != (FLASH_WORD_SIZE)data) {
674 /* Disable interrupts which might cause a timeout here */
675 flag = disable_interrupts();
676 for (i=0; i<4/sizeof(FLASH_WORD_SIZE); i++)
678 if((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL){
679 /* intel style writting */
680 dest2[i] = (FLASH_WORD_SIZE)0x00500050;
681 dest2[i] = (FLASH_WORD_SIZE)0x00400040;
682 *read_val++ = data2[i];
686 /* data polling for D7 */
687 start = get_timer (0);
689 while ((dest2[i] & (FLASH_WORD_SIZE)0x00800080) != (FLASH_WORD_SIZE)0x00800080)
691 if (get_timer(start) > CFG_FLASH_WRITE_TOUT)
694 dest2[i] = (FLASH_WORD_SIZE)0x00FF00FF; /* return to read mode */
696 dest2[i] = (FLASH_WORD_SIZE)0x00FF00FF; /* return to read mode */
697 if(dest2[i]!=data2[i])
698 printf("Error at %p 0x%04X != 0x%04X\n",&dest2[i],dest2[i],data2[i]);
701 addr2[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
702 addr2[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
703 addr2[ADDR0] = (FLASH_WORD_SIZE)0x00A000A0;
705 /* re-enable interrupts if necessary */
708 /* data polling for D7 */
709 start = get_timer (0);
710 while ((dest2[i] & (FLASH_WORD_SIZE)0x00800080) !=
711 (data2[i] & (FLASH_WORD_SIZE)0x00800080)) {
712 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
721 /*-----------------------------------------------------------------------