3 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
5 * (C) Copyright 2001-2004
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 * See file CREDITS for list of people who contributed to this
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28 #include <linux/byteorder/swab.h>
31 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
33 /* Board support for 1 or 2 flash devices */
34 #define FLASH_PORT_WIDTH8
36 typedef unsigned char FLASH_PORT_WIDTH;
37 typedef volatile unsigned char FLASH_PORT_WIDTHV;
41 /* Intel-compatible flash ID */
42 #define INTEL_COMPAT 0x89
43 #define INTEL_ALT 0xB0
45 /* Intel-compatible flash commands */
46 #define INTEL_PROGRAM 0x10
47 #define INTEL_ERASE 0x20
48 #define INTEL_CLEAR 0x50
49 #define INTEL_LOCKBIT 0x60
50 #define INTEL_PROTECT 0x01
51 #define INTEL_STATUS 0x70
52 #define INTEL_READID 0x90
53 #define INTEL_CONFIRM 0xD0
54 #define INTEL_RESET 0xFF
56 /* Intel-compatible flash status bits */
57 #define INTEL_FINISHED 0x80
60 #define FPW FLASH_PORT_WIDTH
61 #define FPWV FLASH_PORT_WIDTHV
63 #define FLASH_CYCLE1 0x0555
64 #define FLASH_CYCLE2 0x02aa
67 /*-----------------------------------------------------------------------
70 static ulong flash_get_size (FPW * addr, flash_info_t * info);
71 static int write_data (flash_info_t * info, ulong dest, FPW data);
72 static int write_data_block (flash_info_t * info, ulong src, ulong dest);
73 static int write_word_amd (flash_info_t * info, FPWV * dest, FPW data);
74 static void flash_get_offsets (ulong base, flash_info_t * info);
75 void inline spin_wheel (void);
76 static void flash_sync_real_protect (flash_info_t * info);
77 static unsigned char intel_sector_protected (flash_info_t *info, ushort sector);
78 static unsigned char same_chip_banks (int bank1, int bank2);
80 /*-----------------------------------------------------------------------
83 unsigned long flash_init (void)
89 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
90 memset (&flash_info[i], 0, sizeof (flash_info_t));
94 flash_get_size ((FPW *) CFG_FLASH1_BASE,
96 flash_get_offsets (CFG_FLASH1_BASE, &flash_info[i]);
99 flash_get_size ((FPW *) CFG_FLASH1_BASE,
101 fsize = CFG_FLASH1_BASE + flash_info[i - 1].size;
102 flash_get_offsets (fsize, &flash_info[i]);
105 flash_get_size ((FPW *) CFG_FLASH0_BASE,
107 flash_get_offsets (CFG_FLASH0_BASE, &flash_info[i]);
110 flash_get_size ((FPW *) CFG_FLASH0_BASE,
112 fsize = CFG_FLASH0_BASE + flash_info[i - 1].size;
113 flash_get_offsets (fsize, &flash_info[i]);
116 panic ("configured to many flash banks!\n");
119 size += flash_info[i].size;
121 /* get the h/w and s/w protection status in sync */
122 flash_sync_real_protect(&flash_info[i]);
125 /* Protect monitor and environment sectors
127 #if defined (CFG_AMD_BOOT)
128 flash_protect (FLAG_PROTECT_SET,
130 CFG_MONITOR_BASE + monitor_flash_len - 1,
132 flash_protect (FLAG_PROTECT_SET,
134 CFG_INTEL_BASE + monitor_flash_len - 1,
137 flash_protect (FLAG_PROTECT_SET,
139 CFG_MONITOR_BASE + monitor_flash_len - 1,
141 flash_protect (FLAG_PROTECT_SET,
143 CFG_AMD_BASE + monitor_flash_len - 1, &flash_info[0]);
146 flash_protect (FLAG_PROTECT_SET,
148 CFG_ENV1_ADDR + CFG_ENV1_SIZE - 1, &flash_info[1]);
149 flash_protect (FLAG_PROTECT_SET,
151 CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[3]);
156 /*-----------------------------------------------------------------------
158 static void flash_get_offsets (ulong base, flash_info_t * info)
162 if (info->flash_id == FLASH_UNKNOWN)
165 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_AMD) {
166 for (i = 0; i < info->sector_count; i++) {
167 info->start[i] = base + (i * PHYS_AMD_SECT_SIZE);
168 info->protect[i] = 0;
172 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
173 for (i = 0; i < info->sector_count; i++) {
174 info->start[i] = base + (i * PHYS_INTEL_SECT_SIZE);
179 /*-----------------------------------------------------------------------
181 void flash_print_info (flash_info_t * info)
185 if (info->flash_id == FLASH_UNKNOWN) {
186 printf ("missing or unknown FLASH type\n");
190 switch (info->flash_id & FLASH_VENDMASK) {
191 case FLASH_MAN_INTEL:
198 printf ("Unknown Vendor ");
202 switch (info->flash_id & FLASH_TYPEMASK) {
203 case FLASH_28F128J3A:
204 printf ("28F128J3A\n");
208 printf ("AMD29F040B\n");
212 printf ("Unknown Chip Type\n");
216 printf (" Size: %ld MB in %d Sectors\n",
217 info->size >> 20, info->sector_count);
219 printf (" Sector Start Addresses:");
220 for (i = 0; i < info->sector_count; ++i) {
224 info->start[i], info->protect[i] ? " (RO)" : " ");
231 * The following code cannot be run from FLASH!
233 static ulong flash_get_size (FPW * addr, flash_info_t * info)
238 /* Write auto select command: read Manufacturer ID */
239 /* Write auto select command sequence and test FLASH answer */
240 addr[FLASH_CYCLE1] = (FPW) 0x00AA00AA; /* for AMD, Intel ignores this */
242 addr[FLASH_CYCLE2] = (FPW) 0x00550055; /* for AMD, Intel ignores this */
244 addr[FLASH_CYCLE1] = (FPW) 0x00900090; /* selects Intel or AMD */
249 switch (addr[0] & 0xff) {
251 case (uchar) AMD_MANUFACT:
252 info->flash_id = FLASH_MAN_AMD;
256 case (uchar) INTEL_MANUFACT:
257 info->flash_id = FLASH_MAN_INTEL;
262 printf ("unknown\n");
263 info->flash_id = FLASH_UNKNOWN;
264 info->sector_count = 0;
266 addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
267 return (0); /* no or unknown flash */
272 case (FPW) INTEL_ID_28F128J3A:
273 info->flash_id += FLASH_28F128J3A;
274 info->sector_count = 64;
275 info->size = 0x00800000; /* => 16 MB */
278 case (FPW) AMD_ID_LV040B:
279 info->flash_id += FLASH_AM040;
281 info->sector_count = 7;
282 info->size = 0x00070000; /* => 448 KB */
285 /* for Environment settings */
286 info->sector_count = 1;
287 info->size = PHYS_AMD_SECT_SIZE; /* => 64 KB */
293 info->flash_id = FLASH_UNKNOWN;
297 if (info->sector_count > CFG_MAX_FLASH_SECT) {
298 printf ("** ERROR: sector count %d > max (%d) **\n",
299 info->sector_count, CFG_MAX_FLASH_SECT);
300 info->sector_count = CFG_MAX_FLASH_SECT;
303 if (value == (FPW) INTEL_ID_28F128J3A)
304 addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
306 addr[0] = (FPW) 0x00F000F0; /* restore read mode */
313 * This function gets the u-boot flash sector protection status
314 * (flash_info_t.protect[]) in sync with the sector protection
315 * status stored in hardware.
317 static void flash_sync_real_protect (flash_info_t * info)
321 switch (info->flash_id & FLASH_TYPEMASK) {
322 case FLASH_28F128J3A:
323 for (i = 0; i < info->sector_count; ++i) {
324 info->protect[i] = intel_sector_protected(info, i);
329 /* no h/w protect support */
336 * checks if "sector" in bank "info" is protected. Should work on intel
337 * strata flash chips 28FxxxJ3x in 8-bit mode.
338 * Returns 1 if sector is protected (or timed-out while trying to read
339 * protection status), 0 if it is not.
341 static unsigned char intel_sector_protected (flash_info_t *info, ushort sector)
344 FPWV *lock_conf_addr;
349 * first, wait for the WSM to be finished. The rationale for
350 * waiting for the WSM to become idle for at most
351 * CFG_FLASH_ERASE_TOUT is as follows. The WSM can be busy
352 * because of: (1) erase, (2) program or (3) lock bit
353 * configuration. So we just wait for the longest timeout of
354 * the (1)-(3), i.e. the erase timeout.
357 /* wait at least 35ns (W12) before issuing Read Status Register */
359 addr = (FPWV *) info->start[sector];
360 *addr = (FPW) INTEL_STATUS;
362 start = get_timer (0);
363 while ((*addr & (FPW) INTEL_FINISHED) != (FPW) INTEL_FINISHED) {
364 if (get_timer (start) > CFG_FLASH_ERASE_TOUT) {
365 *addr = (FPW) INTEL_RESET; /* restore read mode */
366 printf("WSM busy too long, can't get prot status\n");
371 /* issue the Read Identifier Codes command */
372 *addr = (FPW) INTEL_READID;
374 /* wait at least 35ns (W12) before reading */
377 /* Intel example code uses offset of 4 for 8-bit flash */
378 lock_conf_addr = (FPWV *) info->start[sector] + 4;
379 ret = (*lock_conf_addr & (FPW) INTEL_PROTECT) ? 1 : 0;
381 /* put flash back in read mode */
382 *addr = (FPW) INTEL_RESET;
389 * Checks if "bank1" and "bank2" are on the same chip. Returns 1 if they
390 * are and 0 otherwise.
392 static unsigned char same_chip_banks (int bank1, int bank2)
394 unsigned char same_chip[CFG_MAX_FLASH_BANKS][CFG_MAX_FLASH_BANKS] = {
400 return same_chip[bank1][bank2];
404 /*-----------------------------------------------------------------------
406 int flash_erase (flash_info_t * info, int s_first, int s_last)
408 int flag, prot, sect;
409 ulong type, start, last;
410 int rcode = 0, intel = 0;
412 if ((s_first < 0) || (s_first > s_last)) {
413 if (info->flash_id == FLASH_UNKNOWN)
414 printf ("- missing\n");
416 printf ("- no sectors to erase\n");
420 type = (info->flash_id & FLASH_VENDMASK);
421 if ((type != FLASH_MAN_INTEL)) {
422 type = (info->flash_id & FLASH_VENDMASK);
423 if ((type != FLASH_MAN_AMD)) {
424 printf ("Can't erase unknown flash type %08lx - aborted\n",
430 if (type == FLASH_MAN_INTEL)
434 for (sect = s_first; sect <= s_last; ++sect) {
435 if (info->protect[sect]) {
441 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
446 start = get_timer (0);
449 /* Disable interrupts which might cause a timeout here */
450 flag = disable_interrupts ();
452 /* Start erase on unprotected sectors */
453 for (sect = s_first; sect <= s_last; sect++) {
454 if (info->protect[sect] == 0) { /* not protected */
455 FPWV *addr = (FPWV *) (info->start[sect]);
458 printf ("Erasing sector %2d ... ", sect);
460 /* arm simple, non interrupt dependent timer */
461 start = get_timer (0);
464 *addr = (FPW) 0x00500050; /* clear status register */
465 *addr = (FPW) 0x00200020; /* erase setup */
466 *addr = (FPW) 0x00D000D0; /* erase confirm */
468 FPWV *base; /* first address in bank */
470 base = (FPWV *) (CFG_AMD_BASE);
471 base[FLASH_CYCLE1] = (FPW) 0x00AA00AA; /* unlock */
472 base[FLASH_CYCLE2] = (FPW) 0x00550055; /* unlock */
473 base[FLASH_CYCLE1] = (FPW) 0x00800080; /* erase mode */
474 base[FLASH_CYCLE1] = (FPW) 0x00AA00AA; /* unlock */
475 base[FLASH_CYCLE2] = (FPW) 0x00550055; /* unlock */
476 *addr = (FPW) 0x00300030; /* erase sector */
480 *addr) & (FPW) 0x00800080) !=
482 if (get_timer (start) > CFG_FLASH_ERASE_TOUT) {
483 printf ("Timeout\n");
485 *addr = (FPW) 0x00B000B0; /* suspend erase */
486 *addr = (FPW) 0x00FF00FF; /* reset to read mode */
488 *addr = (FPW) 0x00F000F0; /* reset to read mode */
496 *addr = (FPW) 0x00500050; /* clear status register cmd. */
497 *addr = (FPW) 0x00FF00FF; /* resest to read mode */
499 *addr = (FPW) 0x00F000F0; /* reset to read mode */
507 /*-----------------------------------------------------------------------
508 * Copy memory to flash, returns:
511 * 2 - Flash not erased
512 * 4 - Flash not identified
515 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
517 if (info->flash_id == FLASH_UNKNOWN) {
521 switch (info->flash_id & FLASH_VENDMASK) {
524 FPW data = 0; /* 16 or 32 bit word, matches flash bus width */
525 int bytes; /* number of bytes to program in current word */
526 int left; /* number of bytes left to program */
529 for (left = cnt, res = 0;
530 left > 0 && res == 0;
531 addr += sizeof (data), left -=
532 sizeof (data) - bytes) {
534 bytes = addr & (sizeof (data) - 1);
535 addr &= ~(sizeof (data) - 1);
537 /* combine source and destination data so can program
538 * an entire word of 16 or 32 bits
540 for (i = 0; i < sizeof (data); i++) {
542 if (i < bytes || i - bytes >= left)
543 data += *((uchar *) addr + i);
548 res = write_word_amd (info, (FPWV *) addr,
552 } /* case FLASH_MAN_AMD */
554 case FLASH_MAN_INTEL:
558 int count, i, l, rc, port_width;
560 /* get lower word aligned address */
565 * handle unaligned start bytes
567 if ((l = addr - wp) != 0) {
569 for (i = 0, cp = wp; i < l; ++i, ++cp) {
570 data = (data << 8) | (*(uchar *) cp);
573 for (; i < port_width && cnt > 0; ++i) {
574 data = (data << 8) | *src++;
579 for (; cnt == 0 && i < port_width; ++i, ++cp)
580 data = (data << 8) | (*(uchar *) cp);
583 write_data (info, wp, SWAP (data))) != 0)
588 if (cnt > WR_BLOCK) {
590 * handle word aligned part
593 while (cnt >= WR_BLOCK) {
596 write_data_block (info,
605 if (count++ > 0x800) {
612 if (cnt < WR_BLOCK) {
614 * handle word aligned part
617 while (cnt >= port_width) {
619 for (i = 0; i < port_width; ++i)
620 data = (data << 8) | *src++;
623 write_data (info, wp,
629 if (count++ > 0x800) {
640 * handle unaligned tail bytes
643 for (i = 0, cp = wp; i < port_width && cnt > 0;
645 data = (data << 8) | *src++;
649 for (; i < port_width; ++i, ++cp)
650 data = (data << 8) | (*(uchar *) cp);
652 return (write_data (info, wp, SWAP (data)));
653 } /* case FLASH_MAN_INTEL */
659 /*-----------------------------------------------------------------------
660 * Write a word or halfword to Flash, returns:
663 * 2 - Flash not erased
665 static int write_data (flash_info_t * info, ulong dest, FPW data)
667 FPWV *addr = (FPWV *) dest;
671 /* Check if Flash is (sufficiently) erased */
672 if ((*addr & data) != data) {
673 printf ("not erased at %08lx (%lx)\n", (ulong) addr, *addr);
676 /* Disable interrupts which might cause a timeout here */
677 flag = disable_interrupts ();
679 *addr = (FPW) 0x00400040; /* write setup */
682 /* arm simple, non interrupt dependent timer */
683 start = get_timer (0);
685 /* wait while polling the status register */
686 while ((*addr & (FPW) 0x00800080) != (FPW) 0x00800080) {
687 if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
688 *addr = (FPW) 0x00FF00FF; /* restore read mode */
693 *addr = (FPW) 0x00FF00FF; /* restore read mode */
698 /*-----------------------------------------------------------------------
699 * Write a word or halfword to Flash, returns:
702 * 2 - Flash not erased
704 static int write_data_block (flash_info_t * info, ulong src, ulong dest)
706 FPWV *srcaddr = (FPWV *) src;
707 FPWV *dstaddr = (FPWV *) dest;
711 /* Check if Flash is (sufficiently) erased */
712 for (i = 0; i < WR_BLOCK; i++)
713 if ((*dstaddr++ & 0xff) != 0xff) {
714 printf ("not erased at %08lx (%lx)\n",
715 (ulong) dstaddr, *dstaddr);
719 dstaddr = (FPWV *) dest;
721 /* Disable interrupts which might cause a timeout here */
722 flag = disable_interrupts ();
724 *dstaddr = (FPW) 0x00e800e8; /* write block setup */
726 /* arm simple, non interrupt dependent timer */
727 start = get_timer (0);
729 /* wait while polling the status register */
730 while ((*dstaddr & (FPW) 0x00800080) != (FPW) 0x00800080) {
731 if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
732 *dstaddr = (FPW) 0x00FF00FF; /* restore read mode */
737 *dstaddr = (FPW) 0x001f001f; /* write 32 to buffer */
738 for (i = 0; i < WR_BLOCK; i++)
739 *dstaddr++ = *srcaddr++;
742 *dstaddr = (FPW) 0x00d000d0; /* write 32 to buffer */
744 /* arm simple, non interrupt dependent timer */
745 start = get_timer (0);
747 /* wait while polling the status register */
748 while ((*dstaddr & (FPW) 0x00800080) != (FPW) 0x00800080) {
749 if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
750 *dstaddr = (FPW) 0x00FF00FF; /* restore read mode */
755 *dstaddr = (FPW) 0x00FF00FF; /* restore read mode */
760 /*-----------------------------------------------------------------------
761 * Write a word to Flash for AMD FLASH
762 * A word is 16 or 32 bits, whichever the bus width of the flash bank
763 * (not an individual chip) is.
768 * 2 - Flash not erased
770 static int write_word_amd (flash_info_t * info, FPWV * dest, FPW data)
774 int res = 0; /* result, assume success */
775 FPWV *base; /* first address in flash bank */
777 /* Check if Flash is (sufficiently) erased */
778 if ((*dest & data) != data) {
782 base = (FPWV *) (CFG_AMD_BASE);
784 /* Disable interrupts which might cause a timeout here */
785 flag = disable_interrupts ();
787 base[FLASH_CYCLE1] = (FPW) 0x00AA00AA; /* unlock */
788 base[FLASH_CYCLE2] = (FPW) 0x00550055; /* unlock */
789 base[FLASH_CYCLE1] = (FPW) 0x00A000A0; /* selects program mode */
791 *dest = data; /* start programming the data */
793 /* re-enable interrupts if necessary */
795 enable_interrupts ();
797 start = get_timer (0);
799 /* data polling for D7 */
801 && (*dest & (FPW) 0x00800080) != (data & (FPW) 0x00800080)) {
802 if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
803 *dest = (FPW) 0x00F000F0; /* reset bank */
811 void inline spin_wheel (void)
814 static char w[] = "\\/-";
816 printf ("\010%c", w[p]);
817 (++p == 3) ? (p = 0) : 0;
820 /*-----------------------------------------------------------------------
821 * Set/Clear sector's lock bit, returns:
823 * 1 - Error (timeout, voltage problems, etc.)
825 int flash_real_protect (flash_info_t * info, long sector, int prot)
832 FPWV *addr = (FPWV *) (info->start[sector]);
833 int flag = disable_interrupts ();
836 * 29F040B AMD flash does not support software protection/unprotection,
837 * the only way to protect the AMD flash is marked it as prot bit.
838 * This flash only support hardware protection, by supply or not supply
841 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_AMD) {
842 info->protect[sector] = prot;
847 *addr = INTEL_CLEAR; /* Clear status register */
848 if (prot) { /* Set sector lock bit */
849 *addr = INTEL_LOCKBIT; /* Sector lock bit */
850 *addr = INTEL_PROTECT; /* set */
851 } else { /* Clear sector lock bit */
852 *addr = INTEL_LOCKBIT; /* All sectors lock bits */
853 *addr = INTEL_CONFIRM; /* clear */
856 start = get_timer (0);
858 while ((*addr & INTEL_FINISHED) != INTEL_FINISHED) {
859 if (get_timer (start) > CFG_FLASH_UNLOCK_TOUT) {
860 printf ("Flash lock bit operation timed out\n");
866 if (*addr != INTEL_OK) {
867 printf ("Flash lock bit operation failed at %08X, CSR=%08X\n",
868 (uint) addr, (uint) * addr);
873 info->protect[sector] = prot;
876 * Clear lock bit command clears all sectors lock bits, so
877 * we have to restore lock bits of protected sectors.
881 * re-locking must be done for all banks that belong on one
882 * FLASH chip, as all the sectors on the chip were unlocked
883 * by INTEL_LOCKBIT/INTEL_CONFIRM commands. (let's hope
884 * that banks never span chips, in particular chips which
885 * support h/w protection differently).
888 /* find the current bank number */
889 curr_bank = CFG_MAX_FLASH_BANKS + 1;
890 for (j = 0; j < CFG_MAX_FLASH_BANKS; ++j) {
891 if (&flash_info[j] == info) {
895 if (curr_bank == CFG_MAX_FLASH_BANKS + 1) {
896 printf("Error: can't determine bank number!\n");
899 for (bank = 0; bank < CFG_MAX_FLASH_BANKS; ++bank) {
900 if (!same_chip_banks(curr_bank, bank)) {
903 info = &flash_info[bank];
904 for (i = 0; i < info->sector_count; i++) {
905 if (info->protect[i]) {
906 start = get_timer (0);
907 addr = (FPWV *) (info->start[i]);
908 *addr = INTEL_LOCKBIT; /* Sector lock bit */
909 *addr = INTEL_PROTECT; /* set */
910 while ((*addr & INTEL_FINISHED) !=
912 if (get_timer (start) >
913 CFG_FLASH_UNLOCK_TOUT) {
914 printf ("Flash lock bit operation timed out\n");
924 * get the s/w sector protection status in sync with the h/w,
925 * in case something went wrong during the re-locking.
927 flash_sync_real_protect(info); /* resets flash to read mode */
931 enable_interrupts ();
933 *addr = INTEL_RESET; /* Reset to read array mode */