2 * MOUSSE/MPC8240 Board definitions.
3 * Flash Routines for MOUSSE onboard AMD29LV106DB devices
6 * Marius Groeger <mgroeger@sysgo.de>
7 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
10 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
12 * (C) Copyright 1999, by Curt McDowell, 08-06-99, Broadcom Corp.
13 * (C) Copyright 2001, James Dougherty, 07/18/01, Broadcom Corp.
15 * See file CREDITS for list of people who contributed to this
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License as
20 * published by the Free Software Foundation; either version 2 of
21 * the License, or (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
40 int flashLibDebug = 0;
41 int flashLibInited = 0;
46 #define PRINTF if (flashLibDebug) printf
48 #define PRIVATE static
53 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
55 #define SLEEP_DELAY 166
56 #define FLASH_SECTOR_SIZE (64*1024)
57 /***********************************************************************
59 * Virtual Flash Devices on Mousse board
61 * These must be kept in sync with the definitions in flashLib.h.
63 ***********************************************************************/
65 PRIVATE flash_dev_t flashDev[] = {
66 /* Bank 0 sector SA0 (16 kB) */
67 { "SA0",FLASH0_BANK, FLASH0_SEG0_START, 1, 14,
68 FLASH0_VENDOR_ID, FLASH0_DEVICE_ID
70 /* Bank 0 sector SA1 (8 kB) */
71 { "SA1", FLASH0_BANK, FLASH0_SEG0_START + 0x4000, 1, 13,
72 FLASH0_VENDOR_ID, FLASH0_DEVICE_ID
74 /* Bank 0 sector SA2 (8 kB) */
75 { "SA2", FLASH0_BANK, FLASH0_SEG0_START + 0x6000, 1, 13,
76 FLASH0_VENDOR_ID, FLASH0_DEVICE_ID
78 /* Bank 0 sector SA3 is occluded by Mousse I/O devices */
79 /* Bank 0 sectors SA4-SA18, after Mousse devices up to PLCC (960 kB) */
80 { "KERNEL", FLASH0_BANK, FLASH0_SEG1_START, 15, 16,
81 FLASH0_VENDOR_ID, FLASH0_DEVICE_ID
83 /* Bank 0 sectors SA19-SA26, jumper can occlude this by PLCC (512 kB) */
84 /* This is where the Kahlua boot vector and boot ROM code resides. */
85 { "BOOT",FLASH0_BANK, FLASH0_SEG2_START, 8, 16,
86 FLASH0_VENDOR_ID, FLASH0_DEVICE_ID
88 /* Bank 0 sectors SA27-SA34 (512 kB) */
89 { "RAMDISK",FLASH0_BANK, FLASH0_SEG3_START, 8, 16,
90 FLASH0_VENDOR_ID, FLASH0_DEVICE_ID
94 int flashDevCount = (sizeof (flashDev) / sizeof (flashDev[0]));
96 #define DEV(no) (&flashDev[no])
97 #define DEV_NO(dev) ((dev) - flashDev)
99 /***********************************************************************
101 * Private Flash Routines
103 ***********************************************************************/
108 * "addr" is always the PROM raw address, which is the address of an
109 * 8-bit quantity for flash 0 and 16-bit quantity for flash 1.
111 * "pos" is always a logical byte position from the PROM beginning.
114 #define FLASH0_ADDR(dev, addr) \
115 ((unsigned char *) ((dev)->base + (addr)))
117 #define FLASH0_WRITE(dev, addr, value) \
118 (*FLASH0_ADDR(dev, addr) = (value))
120 #define FLASH0_READ(dev, addr) \
121 (*FLASH0_ADDR(dev, addr))
123 PRIVATE int flashCheck (flash_dev_t * dev)
125 if (!flashLibInited) {
126 printf ("flashCheck: flashLib not initialized\n");
130 if (dev < &flashDev[0] || dev >= &flashDev[flashDevCount]) {
131 printf ("flashCheck: Bad dev parameter\n");
136 printf ("flashCheck: Device %d not available\n", DEV_NO (dev));
143 PRIVATE void flashReset (flash_dev_t * dev)
145 PRINTF ("flashReset: dev=%d\n", DEV_NO (dev));
147 if (dev->bank == FLASH0_BANK) {
148 FLASH0_WRITE (dev, 0x555, 0xaa);
149 FLASH0_WRITE (dev, 0xaaa, 0x55);
150 FLASH0_WRITE (dev, 0x555, 0xf0);
153 udelay (SLEEP_DELAY);
155 PRINTF ("flashReset: done\n");
158 PRIVATE int flashProbe (flash_dev_t * dev)
160 int rv, deviceID, vendorID;
162 PRINTF ("flashProbe: dev=%d\n", DEV_NO (dev));
164 if (dev->bank != FLASH0_BANK) {
169 FLASH0_WRITE (dev, 0xaaa, 0xaa);
170 FLASH0_WRITE (dev, 0x555, 0x55);
171 FLASH0_WRITE (dev, 0xaaa, 0x90);
173 udelay (SLEEP_DELAY);
175 vendorID = FLASH0_READ (dev, 0);
176 deviceID = FLASH0_READ (dev, 2);
178 FLASH0_WRITE (dev, 0, 0xf0);
180 PRINTF ("flashProbe: vendor=0x%x device=0x%x\n", vendorID, deviceID);
182 if (vendorID == dev->vendorID && deviceID == dev->deviceID)
188 PRINTF ("flashProbe: rv=%d\n", rv);
193 PRIVATE int flashWait (flash_dev_t * dev, int addr, int expect, int erase)
200 PRINTF ("flashWait: dev=%d addr=0x%x expect=0x%x erase=%d\n",
201 DEV_NO (dev), addr, expect, erase);
204 if (dev->bank != FLASH0_BANK) {
210 polls = FLASH_ERASE_SECTOR_TIMEOUT; /* Ticks */
212 polls = FLASH_PROGRAM_POLLS; /* Loops */
214 for (i = 0; i < polls; i++) {
216 udelay (SLEEP_DELAY);
218 data = FLASH0_READ (dev, addr);
220 if (((data ^ expect) & 0x80) == 0) {
227 * If the 0x20 bit has come on, it could actually be because
228 * the operation succeeded, so check the done bit again.
231 data = FLASH0_READ (dev, addr);
233 if (((data ^ expect) & 0x80) == 0) {
238 printf ("flashWait: Program error (dev: %d, addr: 0x%x)\n",
247 printf ("flashWait: Timeout %s (dev: %d, addr: 0x%x)\n",
248 erase ? "erasing sector" : "programming byte",
254 PRINTF ("flashWait: rv=%d\n", rv);
260 /***********************************************************************
262 * Public Flash Routines
264 ***********************************************************************/
266 STATUS flashLibInit (void)
270 PRINTF ("flashLibInit: devices=%d\n", flashDevCount);
272 for (i = 0; i < flashDevCount; i++) {
273 flash_dev_t *dev = &flashDev[i];
276 * For bank 1, probe both without and with byte swappage,
277 * so that this module works on both old and new Mousse boards.
282 if (flashProbe (dev) != ERROR)
287 if (flashProbe (dev) != ERROR)
293 PRINTF ("\n FLASH %s[%d]: iobase=0x%x - %d sectors %d KB",
294 flashDev[i].name, i, flashDev[i].base,
296 (flashDev[i].sectors * FLASH_SECTOR_SIZE) / 1024);
303 PRINTF ("flashLibInit: done\n");
308 STATUS flashEraseSector (flash_dev_t * dev, int sector)
312 PRINTF ("flashErasesector: dev=%d sector=%d\n", DEV_NO (dev), sector);
314 if (flashCheck (dev) == ERROR)
317 if (sector < 0 || sector >= dev->sectors) {
318 printf ("flashEraseSector: Sector out of range (dev: %d, sector: %d)\n", DEV_NO (dev), sector);
322 pos = FLASH_SECTOR_POS (dev, sector);
324 if (dev->bank != FLASH0_BANK) {
330 FLASH0_WRITE (dev, 0xaaa, 0xaa);
331 FLASH0_WRITE (dev, 0x555, 0x55);
332 FLASH0_WRITE (dev, 0xaaa, 0x80);
333 FLASH0_WRITE (dev, 0xaaa, 0xaa);
334 FLASH0_WRITE (dev, 0x555, 0x55);
335 FLASH0_WRITE (dev, addr, 0x30);
337 return flashWait (dev, addr, 0xff, 1);
341 * Note: it takes about as long to flash all sectors together with Chip
342 * Erase as it does to flash them one at a time (about 30 seconds for 2
343 * MB). Also since we want to be able to treat subsets of sectors as if
344 * they were complete devices, we don't use Chip Erase.
347 STATUS flashErase (flash_dev_t * dev)
351 PRINTF ("flashErase: dev=%d sectors=%d\n", DEV_NO (dev), dev->sectors);
353 if (flashCheck (dev) == ERROR)
356 for (sector = 0; sector < dev->sectors; sector++) {
357 if (flashEraseSector (dev, sector) == ERROR)
364 * Read and write bytes
367 STATUS flashRead (flash_dev_t * dev, int pos, char *buf, int len)
371 PRINTF ("flashRead: dev=%d pos=0x%x buf=0x%x len=0x%x\n",
372 DEV_NO (dev), pos, (int) buf, len);
374 if (flashCheck (dev) == ERROR)
377 if (pos < 0 || len < 0 || pos + len > FLASH_MAX_POS (dev)) {
378 printf ("flashRead: Position out of range "
379 "(dev: %d, pos: 0x%x, len: 0x%x)\n",
380 DEV_NO (dev), pos, len);
387 if (dev->bank == FLASH0_BANK) {
391 PRINTF ("flashRead: memcpy(0x%x, 0x%x, 0x%x)\n",
392 (int) buf, (int) FLASH0_ADDR (dev, pos), len);
394 memcpy (buf, FLASH0_ADDR (dev, addr), words);
397 PRINTF ("flashRead: rv=OK\n");
402 STATUS flashWrite (flash_dev_t * dev, int pos, char *buf, int len)
406 PRINTF ("flashWrite: dev=%d pos=0x%x buf=0x%x len=0x%x\n",
407 DEV_NO (dev), pos, (int) buf, len);
409 if (flashCheck (dev) == ERROR)
412 if (pos < 0 || len < 0 || pos + len > FLASH_MAX_POS (dev)) {
413 printf ("flashWrite: Position out of range "
414 "(dev: %d, pos: 0x%x, len: 0x%x)\n",
415 DEV_NO (dev), pos, len);
422 if (dev->bank == FLASH0_BANK) {
430 if (~FLASH0_READ (dev, addr) & tmp) {
431 printf ("flashWrite: Attempt to program 0 to 1 "
432 "(dev: %d, addr: 0x%x, data: 0x%x)\n",
433 DEV_NO (dev), addr, tmp);
436 FLASH0_WRITE (dev, 0xaaa, 0xaa);
437 FLASH0_WRITE (dev, 0x555, 0x55);
438 FLASH0_WRITE (dev, 0xaaa, 0xa0);
439 FLASH0_WRITE (dev, addr, tmp);
440 if (flashWait (dev, addr, tmp, 0) < 0)
447 PRINTF ("flashWrite: rv=OK\n");
453 * flashWritable returns TRUE if a range contains all F's.
456 STATUS flashWritable (flash_dev_t * dev, int pos, int len)
461 PRINTF ("flashWritable: dev=%d pos=0x%x len=0x%x\n",
462 DEV_NO (dev), pos, len);
464 if (flashCheck (dev) == ERROR)
467 if (pos < 0 || len < 0 || pos + len > FLASH_MAX_POS (dev)) {
468 printf ("flashWritable: Position out of range "
469 "(dev: %d, pos: 0x%x, len: 0x%x)\n",
470 DEV_NO (dev), pos, len);
479 if (dev->bank == FLASH0_BANK) {
484 if (FLASH0_READ (dev, addr) != 0xff) {
495 PRINTF ("flashWrite: rv=%d\n", rv);
501 * NOTE: the below code cannot run from FLASH!!!
503 /***********************************************************************
507 ***********************************************************************/
509 STATUS flashDiag (flash_dev_t * dev)
511 unsigned int *buf = 0;
515 if (flashCheck (dev) == ERROR)
518 printf ("flashDiag: Testing device %d, "
519 "base: 0x%x, %d sectors @ %d kB = %d kB\n",
520 DEV_NO (dev), dev->base,
522 1 << (dev->lgSectorSize - 10),
523 dev->sectors << (dev->lgSectorSize - 10));
525 len = 1 << dev->lgSectorSize;
527 printf ("flashDiag: Erasing\n");
529 if (flashErase (dev) == ERROR) {
530 printf ("flashDiag: Erase failed\n");
533 printf ("%d bytes requested ...\n", len);
535 printf ("allocated %d bytes ...\n", len);
537 printf ("flashDiag: Out of memory\n");
542 * Write unique counting pattern to each sector
545 for (sector = 0; sector < dev->sectors; sector++) {
546 printf ("flashDiag: Write sector %d\n", sector);
548 for (i = 0; i < len / 4; i++)
549 buf[i] = sector << 24 | i;
552 sector << dev->lgSectorSize,
553 (char *) buf, len) == ERROR) {
554 printf ("flashDiag: Write failed (dev: %d, sector: %d)\n",
555 DEV_NO (dev), sector);
564 for (sector = 0; sector < dev->sectors; sector++) {
565 printf ("flashDiag: Verify sector %d\n", sector);
568 sector << dev->lgSectorSize,
569 (char *) buf, len) == ERROR) {
570 printf ("flashDiag: Read failed (dev: %d, sector: %d)\n",
571 DEV_NO (dev), sector);
575 for (i = 0; i < len / 4; i++) {
576 if (buf[i] != (sector << 24 | i)) {
577 printf ("flashDiag: Verify error "
578 "(dev: %d, sector: %d, offset: 0x%x)\n",
579 DEV_NO (dev), sector, i);
580 printf ("flashDiag: Expected 0x%08x, got 0x%08x\n",
581 sector << 24 | i, buf[i]);
588 printf ("flashDiag: Erasing\n");
590 if (flashErase (dev) == ERROR) {
591 printf ("flashDiag: Final erase failed\n");
602 printf ("flashDiag: Device %d passed\n", DEV_NO (dev));
604 printf ("flashDiag: Device %d failed\n", DEV_NO (dev));
609 STATUS flashDiagAll (void)
614 PRINTF ("flashDiagAll: devices=%d\n", flashDevCount);
616 for (i = 0; i < flashDevCount; i++) {
617 flash_dev_t *dev = &flashDev[i];
619 if (dev->found && flashDiag (dev) == ERROR)
624 printf ("flashDiagAll: Passed\n");
626 printf ("flashDiagAll: Failed because of earlier errors\n");
632 /*-----------------------------------------------------------------------
634 unsigned long flash_init (void)
636 unsigned long size = 0;
637 flash_dev_t *dev = NULL;
642 * Provide info for FLASH (up to 960K) of Kernel Image data.
644 dev = FLASH_DEV_BANK0_LOW;
645 flash_info[FLASH_BANK_KERNEL].flash_id =
646 (dev->vendorID << 16) | dev->deviceID;
647 flash_info[FLASH_BANK_KERNEL].sector_count = dev->sectors;
648 flash_info[FLASH_BANK_KERNEL].size =
649 flash_info[FLASH_BANK_KERNEL].sector_count * FLASH_SECTOR_SIZE;
650 flash_info[FLASH_BANK_KERNEL].start[FIRST_SECTOR] = dev->base;
651 size += flash_info[FLASH_BANK_KERNEL].size;
654 * Provide info for 512K PLCC FLASH ROM (U-Boot)
656 dev = FLASH_DEV_BANK0_BOOT;
657 flash_info[FLASH_BANK_BOOT].flash_id =
658 (dev->vendorID << 16) | dev->deviceID;
659 flash_info[FLASH_BANK_BOOT].sector_count = dev->sectors;
660 flash_info[FLASH_BANK_BOOT].size =
661 flash_info[FLASH_BANK_BOOT].sector_count * FLASH_SECTOR_SIZE;
662 flash_info[FLASH_BANK_BOOT].start[FIRST_SECTOR] = dev->base;
663 size += flash_info[FLASH_BANK_BOOT].size;
667 * Provide info for 512K FLASH0 segment (U-Boot)
669 dev = FLASH_DEV_BANK0_HIGH;
670 flash_info[FLASH_BANK_AUX].flash_id =
671 (dev->vendorID << 16) | dev->deviceID;
672 flash_info[FLASH_BANK_AUX].sector_count = dev->sectors;
673 flash_info[FLASH_BANK_AUX].size =
674 flash_info[FLASH_BANK_AUX].sector_count * FLASH_SECTOR_SIZE;
675 flash_info[FLASH_BANK_AUX].start[FIRST_SECTOR] = dev->base;
676 size += flash_info[FLASH_BANK_AUX].size;
683 * Get flash device from U-Boot flash info.
685 flash_dev_t *getFlashDevFromInfo (flash_info_t * info)
692 for (i = 0; i < flashDevCount; i++) {
693 flash_dev_t *dev = &flashDev[i];
695 if (dev->found && (dev->base == info->start[0]))
698 printf ("ERROR: notice, no FLASH mapped at address 0x%x\n",
699 (unsigned int) info->start[0]);
703 ulong flash_get_size (vu_long * addr, flash_info_t * info)
707 for (i = 0; i < flashDevCount; i++) {
708 flash_dev_t *dev = &flashDev[i];
711 if (dev->base == (unsigned int) addr) {
712 info->flash_id = (dev->vendorID << 16) | dev->deviceID;
713 info->sector_count = dev->sectors;
714 info->size = info->sector_count * FLASH_SECTOR_SIZE;
715 return dev->sectors * FLASH_SECTOR_SIZE;
722 void flash_print_info (flash_info_t * info)
727 if (info->flash_id == FLASH_UNKNOWN) {
728 printf ("missing or unknown FLASH type\n");
732 switch ((info->flash_id >> 16) & 0xff) {
737 printf ("Unknown Vendor ");
740 chip = (unsigned int) info->flash_id & 0x000000ff;
745 printf ("AM29F040B (4 Mbit)\n");
751 printf ("AM29LV160B (16 Mbit / 2M x 8bit)\n");
755 printf ("Unknown Chip Type:0x%x\n", chip);
759 printf (" Size: %ld bytes in %d Sectors\n",
760 info->size, info->sector_count);
762 printf (" Sector Start Addresses:");
763 for (i = 0; i < info->sector_count; ++i) {
767 info->start[FIRST_SECTOR] + i * FLASH_SECTOR_SIZE,
768 info->protect[i] ? " (RO)" : " ");
775 * Erase a range of flash sectors.
777 int flash_erase (flash_info_t * info, int s_first, int s_last)
779 vu_long *addr = (vu_long *) (info->start[0]);
780 int prot, sect, l_sect;
781 flash_dev_t *dev = NULL;
783 if ((s_first < 0) || (s_first > s_last)) {
784 if (info->flash_id == FLASH_UNKNOWN) {
785 printf ("- missing\n");
787 printf ("- no sectors to erase\n");
793 for (sect = s_first; sect <= s_last; sect++) {
794 if (info->protect[sect]) {
800 printf ("- Warning: %d protected sectors will not be erased!\n",
808 /* Start erase on unprotected sectors */
809 dev = getFlashDevFromInfo (info);
811 printf ("Erase FLASH[%s] -%d sectors:", dev->name, dev->sectors);
812 for (sect = s_first; sect <= s_last; sect++) {
813 if (info->protect[sect] == 0) { /* not protected */
814 addr = (vu_long *) (dev->base);
815 /* printf("erase_sector: sector=%d, addr=0x%x\n",
818 if (ERROR == flashEraseSector (dev, sect)) {
819 printf ("ERROR: could not erase sector %d on FLASH[%s]\n", sect, dev->name);
829 /*-----------------------------------------------------------------------
830 * Write a word to Flash, returns:
833 * 2 - Flash not erased
835 static int write_word (flash_info_t * info, ulong dest, ulong data)
838 flash_dev_t *dev = getFlashDevFromInfo (info);
839 int addr = dest - info->start[0];
844 if (OK != flashWrite (dev, addr, (char *) &data, sizeof (ulong))) {
845 printf ("ERROR: could not write to addr=0x%x, data=0x%x\n",
846 (unsigned int) addr, (unsigned) data);
850 if ((addr % FLASH_SECTOR_SIZE) == 0)
854 PRINTF ("write_word:0x%x, base=0x%x, addr=0x%x, data=0x%x\n",
855 (unsigned) info->start[0],
857 (unsigned) (dest - info->start[0]), (unsigned) data);
863 /*-----------------------------------------------------------------------
864 * Copy memory to flash, returns:
867 * 2 - Flash not erased
870 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
874 flash_dev_t *dev = getFlashDevFromInfo (info);
877 printf ("FLASH[%s]:", dev->name);
878 wp = (addr & ~3); /* get lower word aligned address */
881 * handle unaligned start bytes
883 if ((l = addr - wp) != 0) {
885 for (i = 0, cp = wp; i < l; ++i, ++cp) {
886 data = (data << 8) | (*(uchar *) cp);
888 for (; i < 4 && cnt > 0; ++i) {
889 data = (data << 8) | *src++;
893 for (; cnt == 0 && i < 4; ++i, ++cp) {
894 data = (data << 8) | (*(uchar *) cp);
896 if ((rc = write_word (info, wp, data)) != 0) {
903 * handle word aligned part
907 for (i = 0; i < 4; ++i) {
908 data = (data << 8) | *src++;
910 if ((rc = write_word (info, wp, data)) != 0) {
922 * handle unaligned tail bytes
925 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
926 data = (data << 8) | *src++;
929 for (; i < 4; ++i, ++cp) {
930 data = (data << 8) | (*(uchar *) cp);
933 return (write_word (info, wp, data));
938 /*-----------------------------------------------------------------------