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,
27 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
29 /*-----------------------------------------------------------------------
32 static ulong flash_get_size(vu_long *addr, flash_info_t *info);
33 static int write_word(flash_info_t *info, ulong dest, ulong data);
34 static void flash_get_offsets(ulong base, flash_info_t *info);
36 /*-----------------------------------------------------------------------
39 unsigned long flash_init(void)
41 unsigned long size_b0, size_b1;
44 /* Init: no FLASHes known */
45 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i)
46 flash_info[i].flash_id = FLASH_UNKNOWN;
49 size_b0 = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE,
53 flash_get_offsets(CONFIG_SYS_FLASH_BASE, &flash_info[0]);
55 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
56 /* Monitor protection ON by default */
57 flash_protect(FLAG_PROTECT_SET,
58 CONFIG_SYS_MONITOR_BASE,
59 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
65 flash_info[1].flash_id = FLASH_UNKNOWN;
66 flash_info[1].sector_count = -1;
68 flash_info[0].size = size_b0;
69 flash_info[1].size = size_b1;
71 return size_b0 + size_b1;
74 /*-----------------------------------------------------------------------
75 * Fix this to support variable sector sizes
77 static void flash_get_offsets(ulong base, flash_info_t *info)
81 /* set up sector start address table */
82 if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) {
83 /* set sector offsets for bottom boot block type */
84 for (i = 0; i < info->sector_count; i++)
85 info->start[i] = base + (i * 0x00010000);
89 /*-----------------------------------------------------------------------
91 void flash_print_info(flash_info_t *info)
95 if (info->flash_id == FLASH_UNKNOWN) {
96 puts("missing or unknown FLASH type\n");
100 switch (info->flash_id & FLASH_VENDMASK) {
108 printf("BRIGHT MICRO ");
111 printf("Unknown Vendor ");
115 switch (info->flash_id & FLASH_TYPEMASK) {
117 printf("29F040 or 29LV040 (4 Mbit, uniform sectors)\n");
120 printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
123 printf("AM29LV400T (4 Mbit, top boot sector)\n");
126 printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
129 printf("AM29LV800T (8 Mbit, top boot sector)\n");
132 printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
135 printf("AM29LV160T (16 Mbit, top boot sector)\n");
138 printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
141 printf("AM29LV320T (32 Mbit, top boot sector)\n");
144 printf("Unknown Chip Type\n");
148 if (info->size >> 20) {
149 printf(" Size: %ld MB in %d Sectors\n",
153 printf(" Size: %ld KB in %d Sectors\n",
158 puts(" Sector Start Addresses:");
160 for (i = 0; i < info->sector_count; ++i) {
166 info->protect[i] ? " (RO)" : " ");
172 /*-----------------------------------------------------------------------
176 * The following code cannot be run from FLASH!
179 static ulong flash_get_size(vu_long *addr, flash_info_t *info)
182 volatile unsigned char *caddr;
185 caddr = (volatile unsigned char *)addr ;
187 /* Write auto select command: read Manufacturer ID */
189 debug("Base address is: %08x\n", caddr);
191 caddr[0x0555] = 0xAA;
192 caddr[0x02AA] = 0x55;
193 caddr[0x0555] = 0x90;
197 debug("Manufact ID: %02x\n", value);
200 case 0x1: /* AMD_MANUFACT */
201 info->flash_id = FLASH_MAN_AMD;
203 case 0x4: /* FUJ_MANUFACT */
204 info->flash_id = FLASH_MAN_FUJ;
207 info->flash_id = FLASH_UNKNOWN;
208 info->sector_count = 0;
213 value = caddr[1]; /* device ID */
215 debug("Device ID: %02x\n", value);
219 info->flash_id += FLASH_AM040;
220 info->sector_count = 8;
221 info->size = 0x00080000;
222 break; /* => 512Kb */
225 info->flash_id = FLASH_UNKNOWN;
226 return 0; /* => no or unknown flash */
229 flash_get_offsets((ulong)addr, &flash_info[0]);
231 /* check for protected sectors */
232 for (i = 0; i < info->sector_count; i++) {
234 * read sector protection at sector address,
236 * D0 = 1 if protected
238 caddr = (volatile unsigned char *)(info->start[i]);
239 info->protect[i] = caddr[2] & 1;
243 * Prevent writes to uninitialized FLASH.
245 if (info->flash_id != FLASH_UNKNOWN) {
246 caddr = (volatile unsigned char *)info->start[0];
247 *caddr = 0xF0; /* reset bank */
253 int flash_erase(flash_info_t *info, int s_first, int s_last)
255 volatile unsigned char *addr =
256 (volatile unsigned char *)(info->start[0]);
257 int flag, prot, sect, l_sect;
258 ulong start, now, last;
260 if ((s_first < 0) || (s_first > s_last)) {
261 if (info->flash_id == FLASH_UNKNOWN)
262 printf("- missing\n");
264 printf("- no sectors to erase\n");
269 if ((info->flash_id == FLASH_UNKNOWN) ||
270 (info->flash_id > FLASH_AMD_COMP)) {
271 printf("Can't erase unknown flash type - aborted\n");
276 for (sect = s_first; sect <= s_last; ++sect) {
277 if (info->protect[sect])
282 printf("- Warning: %d protected sectors will not be erased!\n",
290 /* Disable interrupts which might cause a timeout here */
291 flag = disable_interrupts();
299 /* Start erase on unprotected sectors */
300 for (sect = s_first; sect <= s_last; sect++) {
301 if (info->protect[sect] == 0) { /* not protected */
302 addr = (volatile unsigned char *)(info->start[sect]);
308 /* re-enable interrupts if necessary */
312 /* wait at least 80us - let's wait 1 ms */
316 * We wait for the last triggered sector
321 start = get_timer(0);
323 addr = (volatile unsigned char *)(info->start[l_sect]);
325 while ((addr[0] & 0xFF) != 0xFF) {
327 now = get_timer(start);
329 if (now > CONFIG_SYS_FLASH_ERASE_TOUT) {
333 /* show that we're waiting */
334 if ((now - last) > 1000) { /* every second */
341 /* reset to read mode */
342 addr = (volatile unsigned char *)info->start[0];
344 addr[0] = 0xF0; /* reset bank */
350 /*-----------------------------------------------------------------------
351 * Copy memory to flash, returns:
354 * 2 - Flash not erased
357 int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
362 wp = (addr & ~3); /* get lower word aligned address */
365 * handle unaligned start bytes
371 for (i = 0, cp = wp; i < l; ++i, ++cp)
372 data = (data << 8) | (*(uchar *)cp);
374 for (; i < 4 && cnt > 0; ++i) {
375 data = (data << 8) | *src++;
379 for (; cnt == 0 && i < 4; ++i, ++cp)
380 data = (data << 8) | (*(uchar *)cp);
382 rc = write_word(info, wp, data);
391 * handle word aligned part
395 for (i = 0; i < 4; ++i)
396 data = (data << 8) | *src++;
398 rc = write_word(info, wp, data);
411 * handle unaligned tail bytes
414 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
415 data = (data << 8) | *src++;
418 for (; i < 4; ++i, ++cp)
419 data = (data << 8) | (*(uchar *)cp);
421 return write_word(info, wp, data);
424 /*-----------------------------------------------------------------------
425 * Write a word to Flash, returns:
428 * 2 - Flash not erased
430 static int write_word(flash_info_t *info, ulong dest, ulong data)
432 volatile unsigned char *cdest, *cdata;
433 volatile unsigned char *addr =
434 (volatile unsigned char *)(info->start[0]);
436 int flag, count = 4 ;
438 cdest = (volatile unsigned char *)dest ;
439 cdata = (volatile unsigned char *)&data ;
441 /* Check if Flash is (sufficiently) erased */
442 if ((*((vu_long *)dest) & data) != data)
447 /* Disable interrupts which might cause a timeout here */
448 flag = disable_interrupts();
456 /* re-enable interrupts if necessary */
460 /* data polling for D7 */
461 start = get_timer(0);
462 while ((*cdest ^ *cdata) & 0x80) {
463 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT)