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 #include <asm/processor.h>
27 #include <asm/pci_io.h>
30 #define ROM_CS0_START 0xFF800000
31 #define ROM_CS1_START 0xFF000000
33 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
35 #if defined(CFG_ENV_IS_IN_FLASH)
37 # define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
40 # define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
42 # ifndef CFG_ENV_SECT_SIZE
43 # define CFG_ENV_SECT_SIZE CFG_ENV_SIZE
47 /*-----------------------------------------------------------------------
50 static int write_word (flash_info_t *info, ulong dest, ulong data);
52 static void flash_get_offsets (ulong base, flash_info_t *info);
55 /*flash command address offsets*/
67 #define FLASH_WORD_SIZE unsigned char
69 /*-----------------------------------------------------------------------
73 static int byte_parity_odd(unsigned char x) __attribute__ ((const));
75 static unsigned long flash_id(unsigned char mfct, unsigned char chip) __attribute__ ((const));
79 FLASH_WORD_SIZE extval;
80 unsigned short intval;
85 byte_parity_odd(unsigned char x)
90 return (x & 0x1) != 0;
97 flash_id(unsigned char mfct, unsigned char chip)
99 static const map_entry mfct_map[] =
101 {(FLASH_WORD_SIZE) AMD_MANUFACT, (unsigned short) ((unsigned long) FLASH_MAN_AMD >> 16)},
102 {(FLASH_WORD_SIZE) FUJ_MANUFACT, (unsigned short) ((unsigned long) FLASH_MAN_FUJ >> 16)},
103 {(FLASH_WORD_SIZE) STM_MANUFACT, (unsigned short) ((unsigned long) FLASH_MAN_STM >> 16)},
104 {(FLASH_WORD_SIZE) MT_MANUFACT, (unsigned short) ((unsigned long) FLASH_MAN_MT >> 16)},
105 {(FLASH_WORD_SIZE) INTEL_MANUFACT,(unsigned short) ((unsigned long) FLASH_MAN_INTEL >> 16)},
106 {(FLASH_WORD_SIZE) INTEL_ALT_MANU,(unsigned short) ((unsigned long) FLASH_MAN_INTEL >> 16)}
109 static const map_entry chip_map[] =
111 {AMD_ID_F040B, FLASH_AM040},
112 {(FLASH_WORD_SIZE) STM_ID_x800AB, FLASH_STM800AB}
116 unsigned long result = FLASH_UNKNOWN;
119 for(p = &chip_map[0]; p < &chip_map[sizeof chip_map / sizeof chip_map[0]]; p++)
120 if(p->extval == chip)
122 result = FLASH_VENDMASK | p->intval;
127 for(p = &mfct_map[0]; p < &mfct_map[sizeof mfct_map / sizeof mfct_map[0]]; p++)
128 if(p->extval == mfct)
130 result &= ~FLASH_VENDMASK;
131 result |= (unsigned long) p->intval << 16;
145 static const ulong flash_banks[] = CFG_FLASH_BANKS;
147 /* Init: no FLASHes known */
148 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++)
150 flash_info_t * const pflinfo = &flash_info[i];
151 pflinfo->flash_id = FLASH_UNKNOWN;
153 pflinfo->sector_count = 0;
156 /* Enable writes to Sandpoint flash */
158 register unsigned char temp;
159 CONFIG_READ_BYTE(CFG_WINBOND_ISA_CFG_ADDR + WINBOND_CSCR, temp);
160 temp &= ~0x20; /* clear BIOSWP bit */
161 CONFIG_WRITE_BYTE(CFG_WINBOND_ISA_CFG_ADDR + WINBOND_CSCR, temp);
164 for(i = 0; i < sizeof flash_banks / sizeof flash_banks[0]; i++)
166 flash_info_t * const pflinfo = &flash_info[i];
167 const unsigned long base_address = flash_banks[i];
168 volatile FLASH_WORD_SIZE * const flash = (FLASH_WORD_SIZE *) base_address;
170 volatile FLASH_WORD_SIZE * addr2;
173 /* write autoselect sequence */
174 flash[0x5555] = 0xaa;
175 flash[0x2aaa] = 0x55;
176 flash[0x5555] = 0x90;
178 flash[0xAAA << (3 * i)] = 0xaa;
179 flash[0x555 << (3 * i)] = 0x55;
180 flash[0xAAA << (3 * i)] = 0x90;
182 __asm__ __volatile__("sync");
185 pflinfo->flash_id = flash_id(flash[0x0], flash[0x1]);
187 pflinfo->flash_id = flash_id(flash[0x0], flash[0x2 + 14 * i]);
190 switch(pflinfo->flash_id & FLASH_TYPEMASK)
193 pflinfo->size = 0x00080000;
194 pflinfo->sector_count = 8;
195 for(j = 0; j < 8; j++)
197 pflinfo->start[j] = base_address + 0x00010000 * j;
198 pflinfo->protect[j] = flash[(j << 16) | 0x2];
202 pflinfo->size = 0x00100000;
203 pflinfo->sector_count = 19;
204 pflinfo->start[0] = base_address;
205 pflinfo->start[1] = base_address + 0x4000;
206 pflinfo->start[2] = base_address + 0x6000;
207 pflinfo->start[3] = base_address + 0x8000;
208 for(j = 1; j < 16; j++)
210 pflinfo->start[j+3] = base_address + 0x00010000 * j;
213 /* check for protected sectors */
214 for (j = 0; j < pflinfo->sector_count; j++) {
215 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
216 /* D0 = 1 if protected */
217 addr2 = (volatile FLASH_WORD_SIZE *)(pflinfo->start[j]);
218 if (pflinfo->flash_id & FLASH_MAN_SST)
219 pflinfo->protect[j] = 0;
221 pflinfo->protect[j] = addr2[2] & 1;
226 /* Protect monitor and environment sectors
228 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
229 flash_protect(FLAG_PROTECT_SET,
231 CFG_MONITOR_BASE + CFG_MONITOR_LEN - 1,
235 #if (CFG_ENV_IS_IN_FLASH == 1) && defined(CFG_ENV_ADDR)
236 flash_protect(FLAG_PROTECT_SET,
238 CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
242 /* reset device to read mode */
243 flash[0x0000] = 0xf0;
244 __asm__ __volatile__("sync");
247 return flash_info[0].size + flash_info[1].size;
252 flash_get_offsets (ulong base, flash_info_t *info)
256 /* set up sector start address table */
257 if (info->flash_id & FLASH_MAN_SST)
259 for (i = 0; i < info->sector_count; i++)
260 info->start[i] = base + (i * 0x00010000);
263 if (info->flash_id & FLASH_BTYPE) {
264 /* set sector offsets for bottom boot block type */
265 info->start[0] = base + 0x00000000;
266 info->start[1] = base + 0x00004000;
267 info->start[2] = base + 0x00006000;
268 info->start[3] = base + 0x00008000;
269 for (i = 4; i < info->sector_count; i++) {
270 info->start[i] = base + (i * 0x00010000) - 0x00030000;
273 /* set sector offsets for top boot block type */
274 i = info->sector_count - 1;
275 info->start[i--] = base + info->size - 0x00004000;
276 info->start[i--] = base + info->size - 0x00006000;
277 info->start[i--] = base + info->size - 0x00008000;
278 for (; i >= 0; i--) {
279 info->start[i] = base + i * 0x00010000;
286 /*-----------------------------------------------------------------------
289 flash_print_info(flash_info_t *info)
291 static const char unk[] = "Unknown";
292 const char *mfct = unk, *type = unk;
295 if(info->flash_id != FLASH_UNKNOWN)
297 switch(info->flash_id & FLASH_VENDMASK)
299 case FLASH_MAN_AMD: mfct = "AMD"; break;
300 case FLASH_MAN_FUJ: mfct = "FUJITSU"; break;
301 case FLASH_MAN_STM: mfct = "STM"; break;
302 case FLASH_MAN_SST: mfct = "SST"; break;
303 case FLASH_MAN_BM: mfct = "Bright Microelectonics"; break;
304 case FLASH_MAN_INTEL: mfct = "Intel"; break;
307 switch(info->flash_id & FLASH_TYPEMASK)
309 case FLASH_AM040: type = "AM29F040B (512K * 8, uniform sector size)"; break;
310 case FLASH_AM400B: type = "AM29LV400B (4 Mbit, bottom boot sect)"; break;
311 case FLASH_AM400T: type = "AM29LV400T (4 Mbit, top boot sector)"; break;
312 case FLASH_AM800B: type = "AM29LV800B (8 Mbit, bottom boot sect)"; break;
313 case FLASH_AM800T: type = "AM29LV800T (8 Mbit, top boot sector)"; break;
314 case FLASH_AM160T: type = "AM29LV160T (16 Mbit, top boot sector)"; break;
315 case FLASH_AM320B: type = "AM29LV320B (32 Mbit, bottom boot sect)"; break;
316 case FLASH_AM320T: type = "AM29LV320T (32 Mbit, top boot sector)"; break;
317 case FLASH_STM800AB: type = "M29W800AB (8 Mbit, bottom boot sect)"; break;
318 case FLASH_SST800A: type = "SST39LF/VF800 (8 Mbit, uniform sector size)"; break;
319 case FLASH_SST160A: type = "SST39LF/VF160 (16 Mbit, uniform sector size)"; break;
324 "\n Brand: %s Type: %s\n"
325 " Size: %lu KB in %d Sectors\n",
332 printf (" Sector Start Addresses:");
334 for (i = 0; i < info->sector_count; i++)
338 unsigned long * flash = (unsigned long *) info->start[i];
341 * Check if whole sector is erased
344 (i != (info->sector_count - 1)) ?
345 (info->start[i + 1] - info->start[i]) >> 2 :
346 (info->start[0] + info->size - info->start[i]) >> 2;
349 flash = (unsigned long *) info->start[i], erased = 1;
350 (flash != (unsigned long *) info->start[i] + size) && erased;
353 erased = *flash == ~0x0UL;
357 (i % 5) ? "" : "\n ",
360 info->protect[i] ? "RO" : " "
371 * The following code cannot be run from FLASH!
374 flash_get_size (vu_long *addr, flash_info_t *info)
377 FLASH_WORD_SIZE value;
378 ulong base = (ulong)addr;
379 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)addr;
381 printf("flash_get_size: \n");
382 /* Write auto select command: read Manufacturer ID */
384 addr2[ADDR0] = (FLASH_WORD_SIZE)0xAA;
385 addr2[ADDR1] = (FLASH_WORD_SIZE)0x55;
386 addr2[ADDR0] = (FLASH_WORD_SIZE)0x90;
390 case (FLASH_WORD_SIZE)AMD_MANUFACT:
391 info->flash_id = FLASH_MAN_AMD;
393 case (FLASH_WORD_SIZE)FUJ_MANUFACT:
394 info->flash_id = FLASH_MAN_FUJ;
396 case (FLASH_WORD_SIZE)SST_MANUFACT:
397 info->flash_id = FLASH_MAN_SST;
400 info->flash_id = FLASH_UNKNOWN;
401 info->sector_count = 0;
403 return (0); /* no or unknown flash */
405 printf("recognised manufacturer");
407 value = addr2[ADDR3]; /* device ID */
408 debug ("\ndev_code=%x\n", value);
411 case (FLASH_WORD_SIZE)AMD_ID_LV400T:
412 info->flash_id += FLASH_AM400T;
413 info->sector_count = 11;
414 info->size = 0x00080000;
415 break; /* => 0.5 MB */
417 case (FLASH_WORD_SIZE)AMD_ID_LV400B:
418 info->flash_id += FLASH_AM400B;
419 info->sector_count = 11;
420 info->size = 0x00080000;
421 break; /* => 0.5 MB */
423 case (FLASH_WORD_SIZE)AMD_ID_LV800T:
424 info->flash_id += FLASH_AM800T;
425 info->sector_count = 19;
426 info->size = 0x00100000;
429 case (FLASH_WORD_SIZE)AMD_ID_LV800B:
430 info->flash_id += FLASH_AM800B;
431 info->sector_count = 19;
432 info->size = 0x00100000;
435 case (FLASH_WORD_SIZE)AMD_ID_LV160T:
436 info->flash_id += FLASH_AM160T;
437 info->sector_count = 35;
438 info->size = 0x00200000;
441 case (FLASH_WORD_SIZE)AMD_ID_LV160B:
442 info->flash_id += FLASH_AM160B;
443 info->sector_count = 35;
444 info->size = 0x00200000;
447 case (FLASH_WORD_SIZE)SST_ID_xF800A:
448 info->flash_id += FLASH_SST800A;
449 info->sector_count = 16;
450 info->size = 0x00100000;
453 case (FLASH_WORD_SIZE)SST_ID_xF160A:
454 info->flash_id += FLASH_SST160A;
455 info->sector_count = 32;
456 info->size = 0x00200000;
459 case (FLASH_WORD_SIZE)AMD_ID_F040B:
460 info->flash_id += FLASH_AM040;
461 info->sector_count = 8;
462 info->size = 0x00080000;
463 break; /* => 0.5 MB */
466 info->flash_id = FLASH_UNKNOWN;
467 return (0); /* => no or unknown flash */
471 printf("flash id %lx; sector count %x, size %lx\n", info->flash_id,info->sector_count,info->size);
472 /* set up sector start address table */
473 if (info->flash_id & FLASH_MAN_SST)
475 for (i = 0; i < info->sector_count; i++)
476 info->start[i] = base + (i * 0x00010000);
479 if (info->flash_id & FLASH_BTYPE) {
480 /* set sector offsets for bottom boot block type */
481 info->start[0] = base + 0x00000000;
482 info->start[1] = base + 0x00004000;
483 info->start[2] = base + 0x00006000;
484 info->start[3] = base + 0x00008000;
485 for (i = 4; i < info->sector_count; i++) {
486 info->start[i] = base + (i * 0x00010000) - 0x00030000;
489 /* set sector offsets for top boot block type */
490 i = info->sector_count - 1;
491 info->start[i--] = base + info->size - 0x00004000;
492 info->start[i--] = base + info->size - 0x00006000;
493 info->start[i--] = base + info->size - 0x00008000;
494 for (; i >= 0; i--) {
495 info->start[i] = base + i * 0x00010000;
499 /* check for protected sectors */
500 for (i = 0; i < info->sector_count; i++) {
501 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
502 /* D0 = 1 if protected */
503 addr2 = (volatile FLASH_WORD_SIZE *)(info->start[i]);
504 if (info->flash_id & FLASH_MAN_SST)
505 info->protect[i] = 0;
507 info->protect[i] = addr2[2] & 1;
511 * Prevent writes to uninitialized FLASH.
513 if (info->flash_id != FLASH_UNKNOWN) {
514 addr2 = (FLASH_WORD_SIZE *)info->start[0];
515 *addr2 = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
525 flash_erase(flash_info_t *info, int s_first, int s_last)
527 volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[0]);
528 int flag, prot, sect, l_sect;
529 ulong start, now, last;
532 if ((s_first < 0) || (s_first > s_last)) {
533 if (info->flash_id == FLASH_UNKNOWN) {
534 printf ("- missing\n");
536 printf ("- no sectors to erase\n");
541 if ((info->flash_id == FLASH_UNKNOWN) ||
542 (info->flash_id > (FLASH_MAN_STM | FLASH_AMD_COMP))) {
543 printf ("Can't erase unknown flash type - aborted\n");
548 for (sect=s_first; sect<=s_last; ++sect) {
549 if (info->protect[sect]) {
555 printf ("- Warning: %d protected sectors will not be erased!\n",
563 /* Check the ROM CS */
564 if ((info->start[0] >= ROM_CS1_START) && (info->start[0] < ROM_CS0_START))
569 /* Disable interrupts which might cause a timeout here */
570 flag = disable_interrupts();
572 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
573 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
574 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00800080;
575 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
576 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
578 /* Start erase on unprotected sectors */
579 for (sect = s_first; sect<=s_last; sect++) {
580 if (info->protect[sect] == 0) { /* not protected */
581 addr = (FLASH_WORD_SIZE *)(info->start[0] + (
582 (info->start[sect] - info->start[0]) << sh8b));
583 if (info->flash_id & FLASH_MAN_SST)
585 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
586 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
587 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00800080;
588 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
589 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
590 addr[0] = (FLASH_WORD_SIZE)0x00500050; /* block erase */
591 udelay(30000); /* wait 30 ms */
594 addr[0] = (FLASH_WORD_SIZE)0x00300030; /* sector erase */
599 /* re-enable interrupts if necessary */
603 /* wait at least 80us - let's wait 1 ms */
607 * We wait for the last triggered sector
612 start = get_timer (0);
614 addr = (FLASH_WORD_SIZE *)(info->start[0] + (
615 (info->start[l_sect] - info->start[0]) << sh8b));
616 while ((addr[0] & (FLASH_WORD_SIZE)0x00800080) != (FLASH_WORD_SIZE)0x00800080) {
617 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
618 printf ("Timeout\n");
621 /* show that we're waiting */
622 if ((now - last) > 1000) { /* every second */
629 /* reset to read mode */
630 addr = (FLASH_WORD_SIZE *)info->start[0];
631 addr[0] = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
637 /*-----------------------------------------------------------------------
638 * Copy memory to flash, returns:
641 * 2 - Flash not erased
644 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
649 wp = (addr & ~3); /* get lower word aligned address */
652 * handle unaligned start bytes
654 if ((l = addr - wp) != 0) {
656 for (i=0, cp=wp; i<l; ++i, ++cp) {
657 data = (data << 8) | (*(uchar *)cp);
659 for (; i<4 && cnt>0; ++i) {
660 data = (data << 8) | *src++;
664 for (; cnt==0 && i<4; ++i, ++cp) {
665 data = (data << 8) | (*(uchar *)cp);
668 if ((rc = write_word(info, wp, data)) != 0) {
675 * handle word aligned part
679 for (i=0; i<4; ++i) {
680 data = (data << 8) | *src++;
682 if ((rc = write_word(info, wp, data)) != 0) {
694 * handle unaligned tail bytes
697 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
698 data = (data << 8) | *src++;
701 for (; i<4; ++i, ++cp) {
702 data = (data << 8) | (*(uchar *)cp);
705 return (write_word(info, wp, data));
708 /*-----------------------------------------------------------------------
709 * Write a word to Flash, returns:
712 * 2 - Flash not erased
714 static int write_word (flash_info_t *info, ulong dest, ulong data)
716 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)info->start[0];
717 volatile FLASH_WORD_SIZE *dest2;
718 volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *)&data;
724 /* Check the ROM CS */
725 if ((info->start[0] >= ROM_CS1_START) && (info->start[0] < ROM_CS0_START))
730 dest2 = (FLASH_WORD_SIZE *)(((dest - info->start[0]) << sh8b) +
733 /* Check if Flash is (sufficiently) erased */
734 if ((*dest2 & (FLASH_WORD_SIZE)data) != (FLASH_WORD_SIZE)data) {
737 /* Disable interrupts which might cause a timeout here */
738 flag = disable_interrupts();
740 for (i=0; i<4/sizeof(FLASH_WORD_SIZE); i++)
742 addr2[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
743 addr2[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
744 addr2[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00A000A0;
746 dest2[i << sh8b] = data2[i];
748 /* re-enable interrupts if necessary */
752 /* data polling for D7 */
753 start = get_timer (0);
754 while ((dest2[i << sh8b] & (FLASH_WORD_SIZE)0x00800080) !=
755 (data2[i] & (FLASH_WORD_SIZE)0x00800080)) {
756 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
765 /*-----------------------------------------------------------------------