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,
28 /*---------------------------------------------------------------------*/
29 #define BANKSZ (8 * 1024 * 1024)
30 #define SECTSZ (64 * 1024)
31 #define USERFLASH (2 * 1024 * 1024) /* bottom 2 MB for user */
33 flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
35 #define FLASH_WORD_SIZE unsigned char
37 /*---------------------------------------------------------------------*/
39 unsigned long flash_init (void)
43 flash_info_t *fli = &flash_info[0];
46 fli->sector_count = CFG_MAX_FLASH_SECT;
47 fli->flash_id = FLASH_MAN_AMD;
49 addr = CFG_FLASH_BASE;
50 for (i = 0; i < fli->sector_count; ++i) {
54 /* Protect all but 2 MByte user area */
55 if (addr < (CFG_FLASH_BASE + USERFLASH))
64 /*--------------------------------------------------------------------*/
65 void flash_print_info (flash_info_t * info)
70 volatile unsigned char *flash;
72 printf (" Size: %ld KB in %d Sectors\n",
73 info->size >> 10, info->sector_count);
74 printf (" Sector Start Addresses:");
75 for (i = 0; i < info->sector_count; ++i) {
77 /* Check if whole sector is erased */
78 if (i != (info->sector_count - 1))
79 size = info->start[i + 1] - info->start[i];
81 size = info->start[0] + info->size - info->start[i];
83 flash = (volatile unsigned char *) info->start[i];
84 for (k = 0; k < size; k++) {
85 if (*flash++ != 0xff) {
94 printf (" %08lX%s%s", info->start[i], erased ? " E" : " ",
95 info->protect[i] ? "RO " : " ");
100 /*-------------------------------------------------------------------*/
103 int flash_erase (flash_info_t * info, int s_first, int s_last)
105 volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *) (info->start[0]);
106 volatile FLASH_WORD_SIZE *addr2;
112 /* Some sanity checking */
113 if ((s_first < 0) || (s_first > s_last)) {
114 printf ("- no sectors to erase\n");
119 for (sect = s_first; sect <= s_last; ++sect) {
120 if (info->protect[sect]) {
125 printf ("- Warning: %d protected sectors will not be erased!\n",
131 /* NOTE: disabling interrupts on Nios can be very bad since it
132 * also disables the LO_LIMIT exception. It's better here to
133 * set the interrupt priority to 3 & restore it when we're done.
137 /* It's ok to erase multiple sectors provided we don't delay more
138 * than 50 usec between cmds ... at which point the erase time-out
139 * occurs. So don't go and put printf() calls in the loop ... it
140 * won't be very helpful ;-)
142 for (sect = s_first; sect <= s_last; sect++) {
143 if (info->protect[sect] == 0) { /* not protected */
144 addr2 = (FLASH_WORD_SIZE *) (info->start[sect]);
155 /* Now just wait for 0xff & provide some user feedback while
159 addr2 = (FLASH_WORD_SIZE *) (info->start[sect]);
160 start = get_timer (0);
161 while (*addr2 != 0xff) {
162 udelay (1000 * 1000);
164 if (get_timer (start) > CFG_FLASH_ERASE_TOUT) {
165 printf ("timeout\n");
172 /* Restore interrupt priority */
178 /*-----------------------------------------------------------------------
179 * Copy memory to flash, returns:
182 * 2 - Flash not erased
185 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
188 vu_char *cmd = (vu_char *) info->start[0];
189 vu_char *dst = (vu_char *) addr;
195 /* Check for sufficient erase */
197 if ((*dst & b) != b) {
198 printf ("%02x : %02x\n", *dst, b);
202 /* Disable interrupts other than window underflow
203 * (interrupt priority 2)
212 start = get_timer (0);
214 if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {