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;
95 flash_id(unsigned char mfct, unsigned char chip)
97 static const map_entry mfct_map[] =
99 {(FLASH_WORD_SIZE) AMD_MANUFACT, (unsigned short) ((unsigned long) FLASH_MAN_AMD >> 16)},
100 {(FLASH_WORD_SIZE) FUJ_MANUFACT, (unsigned short) ((unsigned long) FLASH_MAN_FUJ >> 16)},
101 {(FLASH_WORD_SIZE) STM_MANUFACT, (unsigned short) ((unsigned long) FLASH_MAN_STM >> 16)},
102 {(FLASH_WORD_SIZE) MT_MANUFACT, (unsigned short) ((unsigned long) FLASH_MAN_MT >> 16)},
103 {(FLASH_WORD_SIZE) INTEL_MANUFACT,(unsigned short) ((unsigned long) FLASH_MAN_INTEL >> 16)},
104 {(FLASH_WORD_SIZE) INTEL_ALT_MANU,(unsigned short) ((unsigned long) FLASH_MAN_INTEL >> 16)}
107 static const map_entry chip_map[] =
109 {AMD_ID_F040B, FLASH_AM040},
110 {(FLASH_WORD_SIZE) STM_ID_x800AB, FLASH_STM800AB}
114 unsigned long result = FLASH_UNKNOWN;
117 for(p = &chip_map[0]; p < &chip_map[sizeof chip_map / sizeof chip_map[0]]; p++)
118 if(p->extval == chip)
120 result = FLASH_VENDMASK | p->intval;
125 for(p = &mfct_map[0]; p < &mfct_map[sizeof mfct_map / sizeof mfct_map[0]]; p++)
126 if(p->extval == mfct)
128 result &= ~FLASH_VENDMASK;
129 result |= (unsigned long) p->intval << 16;
142 static const ulong flash_banks[] = CFG_FLASH_BANKS;
144 /* Init: no FLASHes known */
145 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++)
147 flash_info_t * const pflinfo = &flash_info[i];
148 pflinfo->flash_id = FLASH_UNKNOWN;
150 pflinfo->sector_count = 0;
153 for(i = 0; i < sizeof flash_banks / sizeof flash_banks[0]; i++)
155 flash_info_t * const pflinfo = &flash_info[i];
156 const unsigned long base_address = flash_banks[i];
157 volatile FLASH_WORD_SIZE * const flash = (FLASH_WORD_SIZE *) base_address;
159 volatile FLASH_WORD_SIZE * addr2;
162 /* write autoselect sequence */
163 flash[0x5555] = 0xaa;
164 flash[0x2aaa] = 0x55;
165 flash[0x5555] = 0x90;
167 flash[0xAAA << (3 * i)] = 0xaa;
168 flash[0x555 << (3 * i)] = 0x55;
169 flash[0xAAA << (3 * i)] = 0x90;
171 __asm__ __volatile__("sync");
174 pflinfo->flash_id = flash_id(flash[0x0], flash[0x1]);
176 pflinfo->flash_id = flash_id(flash[0x0], flash[0x2 + 14 * i]);
179 switch(pflinfo->flash_id & FLASH_TYPEMASK)
182 pflinfo->size = 0x00080000;
183 pflinfo->sector_count = 8;
184 for(j = 0; j < 8; j++)
186 pflinfo->start[j] = base_address + 0x00010000 * j;
187 pflinfo->protect[j] = flash[(j << 16) | 0x2];
191 pflinfo->size = 0x00100000;
192 pflinfo->sector_count = 19;
193 pflinfo->start[0] = base_address;
194 pflinfo->start[1] = base_address + 0x4000;
195 pflinfo->start[2] = base_address + 0x6000;
196 pflinfo->start[3] = base_address + 0x8000;
197 for(j = 1; j < 16; j++)
199 pflinfo->start[j+3] = base_address + 0x00010000 * j;
202 /* check for protected sectors */
203 for (j = 0; j < pflinfo->sector_count; j++) {
204 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
205 /* D0 = 1 if protected */
206 addr2 = (volatile FLASH_WORD_SIZE *)(pflinfo->start[j]);
207 if (pflinfo->flash_id & FLASH_MAN_SST)
208 pflinfo->protect[j] = 0;
210 pflinfo->protect[j] = addr2[2] & 1;
215 /* Protect monitor and environment sectors
217 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
218 flash_protect(FLAG_PROTECT_SET,
220 CFG_MONITOR_BASE + monitor_flash_len - 1,
224 #if (CFG_ENV_IS_IN_FLASH == 1) && defined(CFG_ENV_ADDR)
225 flash_protect(FLAG_PROTECT_SET,
227 CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
231 /* reset device to read mode */
232 flash[0x0000] = 0xf0;
233 __asm__ __volatile__("sync");
236 return flash_info[0].size + flash_info[1].size;
241 flash_get_offsets (ulong base, flash_info_t *info)
245 /* set up sector start address table */
246 if (info->flash_id & FLASH_MAN_SST)
248 for (i = 0; i < info->sector_count; i++)
249 info->start[i] = base + (i * 0x00010000);
252 if (info->flash_id & FLASH_BTYPE) {
253 /* set sector offsets for bottom boot block type */
254 info->start[0] = base + 0x00000000;
255 info->start[1] = base + 0x00004000;
256 info->start[2] = base + 0x00006000;
257 info->start[3] = base + 0x00008000;
258 for (i = 4; i < info->sector_count; i++) {
259 info->start[i] = base + (i * 0x00010000) - 0x00030000;
262 /* set sector offsets for top boot block type */
263 i = info->sector_count - 1;
264 info->start[i--] = base + info->size - 0x00004000;
265 info->start[i--] = base + info->size - 0x00006000;
266 info->start[i--] = base + info->size - 0x00008000;
267 for (; i >= 0; i--) {
268 info->start[i] = base + i * 0x00010000;
275 /*-----------------------------------------------------------------------
278 flash_print_info(flash_info_t *info)
280 static const char unk[] = "Unknown";
281 const char *mfct = unk, *type = unk;
284 if(info->flash_id != FLASH_UNKNOWN)
286 switch(info->flash_id & FLASH_VENDMASK)
288 case FLASH_MAN_AMD: mfct = "AMD"; break;
289 case FLASH_MAN_FUJ: mfct = "FUJITSU"; break;
290 case FLASH_MAN_STM: mfct = "STM"; break;
291 case FLASH_MAN_SST: mfct = "SST"; break;
292 case FLASH_MAN_BM: mfct = "Bright Microelectonics"; break;
293 case FLASH_MAN_INTEL: mfct = "Intel"; break;
296 switch(info->flash_id & FLASH_TYPEMASK)
298 case FLASH_AM040: type = "AM29F040B (512K * 8, uniform sector size)"; break;
299 case FLASH_AM400B: type = "AM29LV400B (4 Mbit, bottom boot sect)"; break;
300 case FLASH_AM400T: type = "AM29LV400T (4 Mbit, top boot sector)"; break;
301 case FLASH_AM800B: type = "AM29LV800B (8 Mbit, bottom boot sect)"; break;
302 case FLASH_AM800T: type = "AM29LV800T (8 Mbit, top boot sector)"; break;
303 case FLASH_AM160T: type = "AM29LV160T (16 Mbit, top boot sector)"; break;
304 case FLASH_AM320B: type = "AM29LV320B (32 Mbit, bottom boot sect)"; break;
305 case FLASH_AM320T: type = "AM29LV320T (32 Mbit, top boot sector)"; break;
306 case FLASH_STM800AB: type = "M29W800AB (8 Mbit, bottom boot sect)"; break;
307 case FLASH_SST800A: type = "SST39LF/VF800 (8 Mbit, uniform sector size)"; break;
308 case FLASH_SST160A: type = "SST39LF/VF160 (16 Mbit, uniform sector size)"; break;
313 "\n Brand: %s Type: %s\n"
314 " Size: %lu KB in %d Sectors\n",
321 printf (" Sector Start Addresses:");
323 for (i = 0; i < info->sector_count; i++)
327 unsigned long * flash = (unsigned long *) info->start[i];
330 * Check if whole sector is erased
333 (i != (info->sector_count - 1)) ?
334 (info->start[i + 1] - info->start[i]) >> 2 :
335 (info->start[0] + info->size - info->start[i]) >> 2;
338 flash = (unsigned long *) info->start[i], erased = 1;
339 (flash != (unsigned long *) info->start[i] + size) && erased;
342 erased = *flash == ~0x0UL;
346 (i % 5) ? "" : "\n ",
349 info->protect[i] ? "RO" : " "
360 * The following code cannot be run from FLASH!
363 flash_get_size (vu_long *addr, flash_info_t *info)
366 FLASH_WORD_SIZE value;
367 ulong base = (ulong)addr;
368 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)addr;
370 printf("flash_get_size: \n");
371 /* Write auto select command: read Manufacturer ID */
373 addr2[ADDR0] = (FLASH_WORD_SIZE)0xAA;
374 addr2[ADDR1] = (FLASH_WORD_SIZE)0x55;
375 addr2[ADDR0] = (FLASH_WORD_SIZE)0x90;
379 case (FLASH_WORD_SIZE)AMD_MANUFACT:
380 info->flash_id = FLASH_MAN_AMD;
382 case (FLASH_WORD_SIZE)FUJ_MANUFACT:
383 info->flash_id = FLASH_MAN_FUJ;
385 case (FLASH_WORD_SIZE)SST_MANUFACT:
386 info->flash_id = FLASH_MAN_SST;
389 info->flash_id = FLASH_UNKNOWN;
390 info->sector_count = 0;
392 return (0); /* no or unknown flash */
394 printf("recognised manufacturer");
396 value = addr2[ADDR3]; /* device ID */
397 debug ("\ndev_code=%x\n", value);
400 case (FLASH_WORD_SIZE)AMD_ID_LV400T:
401 info->flash_id += FLASH_AM400T;
402 info->sector_count = 11;
403 info->size = 0x00080000;
404 break; /* => 0.5 MB */
406 case (FLASH_WORD_SIZE)AMD_ID_LV400B:
407 info->flash_id += FLASH_AM400B;
408 info->sector_count = 11;
409 info->size = 0x00080000;
410 break; /* => 0.5 MB */
412 case (FLASH_WORD_SIZE)AMD_ID_LV800T:
413 info->flash_id += FLASH_AM800T;
414 info->sector_count = 19;
415 info->size = 0x00100000;
418 case (FLASH_WORD_SIZE)AMD_ID_LV800B:
419 info->flash_id += FLASH_AM800B;
420 info->sector_count = 19;
421 info->size = 0x00100000;
424 case (FLASH_WORD_SIZE)AMD_ID_LV160T:
425 info->flash_id += FLASH_AM160T;
426 info->sector_count = 35;
427 info->size = 0x00200000;
430 case (FLASH_WORD_SIZE)AMD_ID_LV160B:
431 info->flash_id += FLASH_AM160B;
432 info->sector_count = 35;
433 info->size = 0x00200000;
436 case (FLASH_WORD_SIZE)SST_ID_xF800A:
437 info->flash_id += FLASH_SST800A;
438 info->sector_count = 16;
439 info->size = 0x00100000;
442 case (FLASH_WORD_SIZE)SST_ID_xF160A:
443 info->flash_id += FLASH_SST160A;
444 info->sector_count = 32;
445 info->size = 0x00200000;
448 case (FLASH_WORD_SIZE)AMD_ID_F040B:
449 info->flash_id += FLASH_AM040;
450 info->sector_count = 8;
451 info->size = 0x00080000;
452 break; /* => 0.5 MB */
455 info->flash_id = FLASH_UNKNOWN;
456 return (0); /* => no or unknown flash */
460 printf("flash id %lx; sector count %x, size %lx\n", info->flash_id,info->sector_count,info->size);
461 /* set up sector start address table */
462 if (info->flash_id & FLASH_MAN_SST)
464 for (i = 0; i < info->sector_count; i++)
465 info->start[i] = base + (i * 0x00010000);
468 if (info->flash_id & FLASH_BTYPE) {
469 /* set sector offsets for bottom boot block type */
470 info->start[0] = base + 0x00000000;
471 info->start[1] = base + 0x00004000;
472 info->start[2] = base + 0x00006000;
473 info->start[3] = base + 0x00008000;
474 for (i = 4; i < info->sector_count; i++) {
475 info->start[i] = base + (i * 0x00010000) - 0x00030000;
478 /* set sector offsets for top boot block type */
479 i = info->sector_count - 1;
480 info->start[i--] = base + info->size - 0x00004000;
481 info->start[i--] = base + info->size - 0x00006000;
482 info->start[i--] = base + info->size - 0x00008000;
483 for (; i >= 0; i--) {
484 info->start[i] = base + i * 0x00010000;
488 /* check for protected sectors */
489 for (i = 0; i < info->sector_count; i++) {
490 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
491 /* D0 = 1 if protected */
492 addr2 = (volatile FLASH_WORD_SIZE *)(info->start[i]);
493 if (info->flash_id & FLASH_MAN_SST)
494 info->protect[i] = 0;
496 info->protect[i] = addr2[2] & 1;
500 * Prevent writes to uninitialized FLASH.
502 if (info->flash_id != FLASH_UNKNOWN) {
503 addr2 = (FLASH_WORD_SIZE *)info->start[0];
504 *addr2 = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
514 flash_erase(flash_info_t *info, int s_first, int s_last)
516 volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[0]);
517 int flag, prot, sect, l_sect;
518 ulong start, now, last;
521 if ((s_first < 0) || (s_first > s_last)) {
522 if (info->flash_id == FLASH_UNKNOWN) {
523 printf ("- missing\n");
525 printf ("- no sectors to erase\n");
530 if ((info->flash_id == FLASH_UNKNOWN) ||
531 (info->flash_id > (FLASH_MAN_STM | FLASH_AMD_COMP))) {
532 printf ("Can't erase unknown flash type - aborted\n");
537 for (sect=s_first; sect<=s_last; ++sect) {
538 if (info->protect[sect]) {
544 printf ("- Warning: %d protected sectors will not be erased!\n",
552 /* Check the ROM CS */
553 if ((info->start[0] >= ROM_CS1_START) && (info->start[0] < ROM_CS0_START))
558 /* Disable interrupts which might cause a timeout here */
559 flag = disable_interrupts();
561 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
562 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
563 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00800080;
564 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
565 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
567 /* Start erase on unprotected sectors */
568 for (sect = s_first; sect<=s_last; sect++) {
569 if (info->protect[sect] == 0) { /* not protected */
570 addr = (FLASH_WORD_SIZE *)(info->start[0] + (
571 (info->start[sect] - info->start[0]) << sh8b));
572 if (info->flash_id & FLASH_MAN_SST)
574 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
575 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
576 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00800080;
577 addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
578 addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
579 addr[0] = (FLASH_WORD_SIZE)0x00500050; /* block erase */
580 udelay(30000); /* wait 30 ms */
583 addr[0] = (FLASH_WORD_SIZE)0x00300030; /* sector erase */
588 /* re-enable interrupts if necessary */
592 /* wait at least 80us - let's wait 1 ms */
596 * We wait for the last triggered sector
601 start = get_timer (0);
603 addr = (FLASH_WORD_SIZE *)(info->start[0] + (
604 (info->start[l_sect] - info->start[0]) << sh8b));
605 while ((addr[0] & (FLASH_WORD_SIZE)0x00800080) != (FLASH_WORD_SIZE)0x00800080) {
606 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
607 printf ("Timeout\n");
610 /* show that we're waiting */
611 if ((now - last) > 1000) { /* every second */
618 /* reset to read mode */
619 addr = (FLASH_WORD_SIZE *)info->start[0];
620 addr[0] = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
626 /*-----------------------------------------------------------------------
627 * Copy memory to flash, returns:
630 * 2 - Flash not erased
633 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
638 wp = (addr & ~3); /* get lower word aligned address */
641 * handle unaligned start bytes
643 if ((l = addr - wp) != 0) {
645 for (i=0, cp=wp; i<l; ++i, ++cp) {
646 data = (data << 8) | (*(uchar *)cp);
648 for (; i<4 && cnt>0; ++i) {
649 data = (data << 8) | *src++;
653 for (; cnt==0 && i<4; ++i, ++cp) {
654 data = (data << 8) | (*(uchar *)cp);
657 if ((rc = write_word(info, wp, data)) != 0) {
664 * handle word aligned part
668 for (i=0; i<4; ++i) {
669 data = (data << 8) | *src++;
671 if ((rc = write_word(info, wp, data)) != 0) {
683 * handle unaligned tail bytes
686 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
687 data = (data << 8) | *src++;
690 for (; i<4; ++i, ++cp) {
691 data = (data << 8) | (*(uchar *)cp);
694 return (write_word(info, wp, data));
697 /*-----------------------------------------------------------------------
698 * Write a word to Flash, returns:
701 * 2 - Flash not erased
703 static int write_word (flash_info_t *info, ulong dest, ulong data)
705 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)info->start[0];
706 volatile FLASH_WORD_SIZE *dest2;
707 volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *)&data;
713 /* Check the ROM CS */
714 if ((info->start[0] >= ROM_CS1_START) && (info->start[0] < ROM_CS0_START))
719 dest2 = (FLASH_WORD_SIZE *)(((dest - info->start[0]) << sh8b) +
722 /* Check if Flash is (sufficiently) erased */
723 if ((*dest2 & (FLASH_WORD_SIZE)data) != (FLASH_WORD_SIZE)data) {
726 /* Disable interrupts which might cause a timeout here */
727 flag = disable_interrupts();
729 for (i=0; i<4/sizeof(FLASH_WORD_SIZE); i++)
731 addr2[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
732 addr2[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
733 addr2[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00A000A0;
735 dest2[i << sh8b] = data2[i];
737 /* re-enable interrupts if necessary */
741 /* data polling for D7 */
742 start = get_timer (0);
743 while ((dest2[i << sh8b] & (FLASH_WORD_SIZE)0x00800080) !=
744 (data2[i] & (FLASH_WORD_SIZE)0x00800080)) {
745 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
754 /*-----------------------------------------------------------------------