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>
29 #define ROM_CS0_START 0xFF800000
30 #define ROM_CS1_START 0xFF000000
32 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
34 #if defined(CFG_ENV_IS_IN_FLASH)
36 # define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
39 # define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
41 # ifndef CFG_ENV_SECT_SIZE
42 # define CFG_ENV_SECT_SIZE CFG_ENV_SIZE
46 /*-----------------------------------------------------------------------
49 static int write_word (flash_info_t *info, ulong dest, ulong data);
51 static void flash_get_offsets (ulong base, flash_info_t *info);
54 /*flash command address offsets*/
66 #define FLASH_WORD_SIZE unsigned char
68 /*-----------------------------------------------------------------------
72 static int byte_parity_odd(unsigned char x) __attribute__ ((const));
74 static unsigned long flash_id(unsigned char mfct, unsigned char chip) __attribute__ ((const));
78 FLASH_WORD_SIZE extval;
79 unsigned short intval;
84 byte_parity_odd(unsigned char x)
89 return (x & 0x1) != 0;
96 flash_id(unsigned char mfct, unsigned char chip)
98 static const map_entry mfct_map[] =
100 {(FLASH_WORD_SIZE) AMD_MANUFACT, (unsigned short) ((unsigned long) FLASH_MAN_AMD >> 16)},
101 {(FLASH_WORD_SIZE) FUJ_MANUFACT, (unsigned short) ((unsigned long) FLASH_MAN_FUJ >> 16)},
102 {(FLASH_WORD_SIZE) STM_MANUFACT, (unsigned short) ((unsigned long) FLASH_MAN_STM >> 16)},
103 {(FLASH_WORD_SIZE) MT_MANUFACT, (unsigned short) ((unsigned long) FLASH_MAN_MT >> 16)},
104 {(FLASH_WORD_SIZE) INTEL_MANUFACT,(unsigned short) ((unsigned long) FLASH_MAN_INTEL >> 16)},
105 {(FLASH_WORD_SIZE) INTEL_ALT_MANU,(unsigned short) ((unsigned long) FLASH_MAN_INTEL >> 16)}
108 static const map_entry chip_map[] =
110 {AMD_ID_F040B, FLASH_AM040},
111 {(FLASH_WORD_SIZE) STM_ID_x800AB, FLASH_STM800AB}
115 unsigned long result = FLASH_UNKNOWN;
118 for(p = &chip_map[0]; p < &chip_map[sizeof chip_map / sizeof chip_map[0]]; p++)
119 if(p->extval == chip)
121 result = FLASH_VENDMASK | p->intval;
126 for(p = &mfct_map[0]; p < &mfct_map[sizeof mfct_map / sizeof mfct_map[0]]; p++)
127 if(p->extval == mfct)
129 result &= ~FLASH_VENDMASK;
130 result |= (unsigned long) p->intval << 16;
144 static const ulong flash_banks[] = CFG_FLASH_BANKS;
146 /* Init: no FLASHes known */
147 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++)
149 flash_info_t * const pflinfo = &flash_info[i];
150 pflinfo->flash_id = FLASH_UNKNOWN;
152 pflinfo->sector_count = 0;
155 for(i = 0; i < sizeof flash_banks / sizeof flash_banks[0]; i++)
157 flash_info_t * const pflinfo = &flash_info[i];
158 const unsigned long base_address = flash_banks[i];
159 volatile FLASH_WORD_SIZE * const flash = (FLASH_WORD_SIZE *) base_address;
161 volatile FLASH_WORD_SIZE * addr2;
164 /* write autoselect sequence */
165 flash[0x5555] = 0xaa;
166 flash[0x2aaa] = 0x55;
167 flash[0x5555] = 0x90;
169 flash[0xAAA << (3 * i)] = 0xaa;
170 flash[0x555 << (3 * i)] = 0x55;
171 flash[0xAAA << (3 * i)] = 0x90;
173 __asm__ __volatile__("sync");
176 pflinfo->flash_id = flash_id(flash[0x0], flash[0x1]);
178 pflinfo->flash_id = flash_id(flash[0x0], flash[0x2 + 14 * i]);
181 switch(pflinfo->flash_id & FLASH_TYPEMASK)
184 pflinfo->size = 0x00080000;
185 pflinfo->sector_count = 8;
186 for(j = 0; j < 8; j++)
188 pflinfo->start[j] = base_address + 0x00010000 * j;
189 pflinfo->protect[j] = flash[(j << 16) | 0x2];
193 pflinfo->size = 0x00100000;
194 pflinfo->sector_count = 19;
195 pflinfo->start[0] = base_address;
196 pflinfo->start[1] = base_address + 0x4000;
197 pflinfo->start[2] = base_address + 0x6000;
198 pflinfo->start[3] = base_address + 0x8000;
199 for(j = 1; j < 16; j++)
201 pflinfo->start[j+3] = base_address + 0x00010000 * j;
204 /* check for protected sectors */
205 for (j = 0; j < pflinfo->sector_count; j++) {
206 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
207 /* D0 = 1 if protected */
208 addr2 = (volatile FLASH_WORD_SIZE *)(pflinfo->start[j]);
209 if (pflinfo->flash_id & FLASH_MAN_SST)
210 pflinfo->protect[j] = 0;
212 pflinfo->protect[j] = addr2[2] & 1;
217 /* Protect monitor and environment sectors
219 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
220 flash_protect(FLAG_PROTECT_SET,
222 CFG_MONITOR_BASE + monitor_flash_len - 1,
226 #if (CFG_ENV_IS_IN_FLASH == 1) && defined(CFG_ENV_ADDR)
227 flash_protect(FLAG_PROTECT_SET,
229 CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
233 /* reset device to read mode */
234 flash[0x0000] = 0xf0;
235 __asm__ __volatile__("sync");
238 return flash_info[0].size + flash_info[1].size;
243 flash_get_offsets (ulong base, flash_info_t *info)
247 /* set up sector start address table */
248 if (info->flash_id & FLASH_MAN_SST)
250 for (i = 0; i < info->sector_count; i++)
251 info->start[i] = base + (i * 0x00010000);
254 if (info->flash_id & FLASH_BTYPE) {
255 /* set sector offsets for bottom boot block type */
256 info->start[0] = base + 0x00000000;
257 info->start[1] = base + 0x00004000;
258 info->start[2] = base + 0x00006000;
259 info->start[3] = base + 0x00008000;
260 for (i = 4; i < info->sector_count; i++) {
261 info->start[i] = base + (i * 0x00010000) - 0x00030000;
264 /* set sector offsets for top boot block type */
265 i = info->sector_count - 1;
266 info->start[i--] = base + info->size - 0x00004000;
267 info->start[i--] = base + info->size - 0x00006000;
268 info->start[i--] = base + info->size - 0x00008000;
269 for (; i >= 0; i--) {
270 info->start[i] = base + i * 0x00010000;
277 /*-----------------------------------------------------------------------
280 flash_print_info(flash_info_t *info)
282 static const char unk[] = "Unknown";
283 const char *mfct = unk, *type = unk;
286 if(info->flash_id != FLASH_UNKNOWN)
288 switch(info->flash_id & FLASH_VENDMASK)
290 case FLASH_MAN_AMD: mfct = "AMD"; break;
291 case FLASH_MAN_FUJ: mfct = "FUJITSU"; break;
292 case FLASH_MAN_STM: mfct = "STM"; break;
293 case FLASH_MAN_SST: mfct = "SST"; break;
294 case FLASH_MAN_BM: mfct = "Bright Microelectonics"; break;
295 case FLASH_MAN_INTEL: mfct = "Intel"; break;
298 switch(info->flash_id & FLASH_TYPEMASK)
300 case FLASH_AM040: type = "AM29F040B (512K * 8, uniform sector size)"; break;
301 case FLASH_AM400B: type = "AM29LV400B (4 Mbit, bottom boot sect)"; break;
302 case FLASH_AM400T: type = "AM29LV400T (4 Mbit, top boot sector)"; break;
303 case FLASH_AM800B: type = "AM29LV800B (8 Mbit, bottom boot sect)"; break;
304 case FLASH_AM800T: type = "AM29LV800T (8 Mbit, top boot sector)"; break;
305 case FLASH_AM160T: type = "AM29LV160T (16 Mbit, top boot sector)"; break;
306 case FLASH_AM320B: type = "AM29LV320B (32 Mbit, bottom boot sect)"; break;
307 case FLASH_AM320T: type = "AM29LV320T (32 Mbit, top boot sector)"; break;
308 case FLASH_STM800AB: type = "M29W800AB (8 Mbit, bottom boot sect)"; break;
309 case FLASH_SST800A: type = "SST39LF/VF800 (8 Mbit, uniform sector size)"; break;
310 case FLASH_SST160A: type = "SST39LF/VF160 (16 Mbit, uniform sector size)"; break;
315 "\n Brand: %s Type: %s\n"
316 " Size: %lu KB in %d Sectors\n",
323 printf (" Sector Start Addresses:");
325 for (i = 0; i < info->sector_count; i++)
329 unsigned long * flash = (unsigned long *) info->start[i];
332 * Check if whole sector is erased
335 (i != (info->sector_count - 1)) ?
336 (info->start[i + 1] - info->start[i]) >> 2 :
337 (info->start[0] + info->size - info->start[i]) >> 2;
340 flash = (unsigned long *) info->start[i], erased = 1;
341 (flash != (unsigned long *) info->start[i] + size) && erased;
344 erased = *flash == ~0x0UL;
348 (i % 5) ? "" : "\n ",
351 info->protect[i] ? "RO" : " "
362 * The following code cannot be run from FLASH!
365 flash_get_size (vu_long *addr, flash_info_t *info)
368 FLASH_WORD_SIZE value;
369 ulong base = (ulong)addr;
370 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)addr;
372 printf("flash_get_size: \n");
373 /* Write auto select command: read Manufacturer ID */
375 addr2[ADDR0] = (FLASH_WORD_SIZE)0xAA;
376 addr2[ADDR1] = (FLASH_WORD_SIZE)0x55;
377 addr2[ADDR0] = (FLASH_WORD_SIZE)0x90;
381 case (FLASH_WORD_SIZE)AMD_MANUFACT:
382 info->flash_id = FLASH_MAN_AMD;
384 case (FLASH_WORD_SIZE)FUJ_MANUFACT:
385 info->flash_id = FLASH_MAN_FUJ;
387 case (FLASH_WORD_SIZE)SST_MANUFACT:
388 info->flash_id = FLASH_MAN_SST;
391 info->flash_id = FLASH_UNKNOWN;
392 info->sector_count = 0;
394 return (0); /* no or unknown flash */
396 printf("recognised manufacturer");
398 value = addr2[ADDR3]; /* device ID */
399 debug ("\ndev_code=%x\n", value);
402 case (FLASH_WORD_SIZE)AMD_ID_LV400T:
403 info->flash_id += FLASH_AM400T;
404 info->sector_count = 11;
405 info->size = 0x00080000;
406 break; /* => 0.5 MB */
408 case (FLASH_WORD_SIZE)AMD_ID_LV400B:
409 info->flash_id += FLASH_AM400B;
410 info->sector_count = 11;
411 info->size = 0x00080000;
412 break; /* => 0.5 MB */
414 case (FLASH_WORD_SIZE)AMD_ID_LV800T:
415 info->flash_id += FLASH_AM800T;
416 info->sector_count = 19;
417 info->size = 0x00100000;
420 case (FLASH_WORD_SIZE)AMD_ID_LV800B:
421 info->flash_id += FLASH_AM800B;
422 info->sector_count = 19;
423 info->size = 0x00100000;
426 case (FLASH_WORD_SIZE)AMD_ID_LV160T:
427 info->flash_id += FLASH_AM160T;
428 info->sector_count = 35;
429 info->size = 0x00200000;
432 case (FLASH_WORD_SIZE)AMD_ID_LV160B:
433 info->flash_id += FLASH_AM160B;
434 info->sector_count = 35;
435 info->size = 0x00200000;
438 case (FLASH_WORD_SIZE)SST_ID_xF800A:
439 info->flash_id += FLASH_SST800A;
440 info->sector_count = 16;
441 info->size = 0x00100000;
444 case (FLASH_WORD_SIZE)SST_ID_xF160A:
445 info->flash_id += FLASH_SST160A;
446 info->sector_count = 32;
447 info->size = 0x00200000;
450 case (FLASH_WORD_SIZE)AMD_ID_F040B:
451 info->flash_id += FLASH_AM040;
452 info->sector_count = 8;
453 info->size = 0x00080000;
454 break; /* => 0.5 MB */
457 info->flash_id = FLASH_UNKNOWN;
458 return (0); /* => no or unknown flash */
462 printf("flash id %lx; sector count %x, size %lx\n", info->flash_id,info->sector_count,info->size);
463 /* set up sector start address table */
464 if (info->flash_id & FLASH_MAN_SST)
466 for (i = 0; i < info->sector_count; i++)
467 info->start[i] = base + (i * 0x00010000);
470 if (info->flash_id & FLASH_BTYPE) {
471 /* set sector offsets for bottom boot block type */
472 info->start[0] = base + 0x00000000;
473 info->start[1] = base + 0x00004000;
474 info->start[2] = base + 0x00006000;
475 info->start[3] = base + 0x00008000;
476 for (i = 4; i < info->sector_count; i++) {
477 info->start[i] = base + (i * 0x00010000) - 0x00030000;
480 /* set sector offsets for top boot block type */
481 i = info->sector_count - 1;
482 info->start[i--] = base + info->size - 0x00004000;
483 info->start[i--] = base + info->size - 0x00006000;
484 info->start[i--] = base + info->size - 0x00008000;
485 for (; i >= 0; i--) {
486 info->start[i] = base + i * 0x00010000;
490 /* check for protected sectors */
491 for (i = 0; i < info->sector_count; i++) {
492 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
493 /* D0 = 1 if protected */
494 addr2 = (volatile FLASH_WORD_SIZE *)(info->start[i]);
495 if (info->flash_id & FLASH_MAN_SST)
496 info->protect[i] = 0;
498 info->protect[i] = addr2[2] & 1;
502 * Prevent writes to uninitialized FLASH.
504 if (info->flash_id != FLASH_UNKNOWN) {
505 addr2 = (FLASH_WORD_SIZE *)info->start[0];
506 *addr2 = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
516 flash_erase(flash_info_t *info, int s_first, int s_last)
518 volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[0]);
519 int flag, prot, sect, l_sect;
520 ulong start, now, last;
523 if ((s_first < 0) || (s_first > s_last)) {
524 if (info->flash_id == FLASH_UNKNOWN) {
525 printf ("- missing\n");
527 printf ("- no sectors to erase\n");
532 if ((info->flash_id == FLASH_UNKNOWN) ||
533 (info->flash_id > (FLASH_MAN_STM | FLASH_AMD_COMP))) {
534 printf ("Can't erase unknown flash type - aborted\n");
539 for (sect=s_first; sect<=s_last; ++sect) {
540 if (info->protect[sect]) {
546 printf ("- Warning: %d protected sectors will not be erased!\n",
554 /* Check the ROM CS */
555 if ((info->start[0] >= ROM_CS1_START) && (info->start[0] < ROM_CS0_START))
560 /* Disable interrupts which might cause a timeout here */
561 flag = disable_interrupts();
563 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
564 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
565 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00800080;
566 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
567 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
569 /* Start erase on unprotected sectors */
570 for (sect = s_first; sect<=s_last; sect++) {
571 if (info->protect[sect] == 0) { /* not protected */
572 addr = (FLASH_WORD_SIZE *)(info->start[0] + (
573 (info->start[sect] - info->start[0]) << sh8b));
574 if (info->flash_id & FLASH_MAN_SST)
576 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
577 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
578 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00800080;
579 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
580 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
581 addr[0] = (FLASH_WORD_SIZE)0x00500050; /* block erase */
582 udelay(30000); /* wait 30 ms */
585 addr[0] = (FLASH_WORD_SIZE)0x00300030; /* sector erase */
590 /* re-enable interrupts if necessary */
594 /* wait at least 80us - let's wait 1 ms */
598 * We wait for the last triggered sector
603 start = get_timer (0);
605 addr = (FLASH_WORD_SIZE *)(info->start[0] + (
606 (info->start[l_sect] - info->start[0]) << sh8b));
607 while ((addr[0] & (FLASH_WORD_SIZE)0x00800080) != (FLASH_WORD_SIZE)0x00800080) {
608 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
609 printf ("Timeout\n");
612 /* show that we're waiting */
613 if ((now - last) > 1000) { /* every second */
620 /* reset to read mode */
621 addr = (FLASH_WORD_SIZE *)info->start[0];
622 addr[0] = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
628 /*-----------------------------------------------------------------------
629 * Copy memory to flash, returns:
632 * 2 - Flash not erased
635 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
640 wp = (addr & ~3); /* get lower word aligned address */
643 * handle unaligned start bytes
645 if ((l = addr - wp) != 0) {
647 for (i=0, cp=wp; i<l; ++i, ++cp) {
648 data = (data << 8) | (*(uchar *)cp);
650 for (; i<4 && cnt>0; ++i) {
651 data = (data << 8) | *src++;
655 for (; cnt==0 && i<4; ++i, ++cp) {
656 data = (data << 8) | (*(uchar *)cp);
659 if ((rc = write_word(info, wp, data)) != 0) {
666 * handle word aligned part
670 for (i=0; i<4; ++i) {
671 data = (data << 8) | *src++;
673 if ((rc = write_word(info, wp, data)) != 0) {
685 * handle unaligned tail bytes
688 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
689 data = (data << 8) | *src++;
692 for (; i<4; ++i, ++cp) {
693 data = (data << 8) | (*(uchar *)cp);
696 return (write_word(info, wp, data));
699 /*-----------------------------------------------------------------------
700 * Write a word to Flash, returns:
703 * 2 - Flash not erased
705 static int write_word (flash_info_t *info, ulong dest, ulong data)
707 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)info->start[0];
708 volatile FLASH_WORD_SIZE *dest2;
709 volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *)&data;
715 /* Check the ROM CS */
716 if ((info->start[0] >= ROM_CS1_START) && (info->start[0] < ROM_CS0_START))
721 dest2 = (FLASH_WORD_SIZE *)(((dest - info->start[0]) << sh8b) +
724 /* Check if Flash is (sufficiently) erased */
725 if ((*dest2 & (FLASH_WORD_SIZE)data) != (FLASH_WORD_SIZE)data) {
728 /* Disable interrupts which might cause a timeout here */
729 flag = disable_interrupts();
731 for (i=0; i<4/sizeof(FLASH_WORD_SIZE); i++)
733 addr2[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
734 addr2[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
735 addr2[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00A000A0;
737 dest2[i << sh8b] = data2[i];
739 /* re-enable interrupts if necessary */
743 /* data polling for D7 */
744 start = get_timer (0);
745 while ((dest2[i << sh8b] & (FLASH_WORD_SIZE)0x00800080) !=
746 (data2[i] & (FLASH_WORD_SIZE)0x00800080)) {
747 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
756 /*-----------------------------------------------------------------------