2 * (C) Copyright 2000-2011
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;
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,
63 flash_info[0].size = size_b0;
68 /*-----------------------------------------------------------------------
69 * Fix this to support variable sector sizes
71 static void flash_get_offsets(ulong base, flash_info_t *info)
75 /* set up sector start address table */
76 if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) {
77 /* set sector offsets for bottom boot block type */
78 for (i = 0; i < info->sector_count; i++)
79 info->start[i] = base + (i * 0x00010000);
83 /*-----------------------------------------------------------------------
85 void flash_print_info(flash_info_t *info)
89 if (info->flash_id == FLASH_UNKNOWN) {
90 puts("missing or unknown FLASH type\n");
94 switch (info->flash_id & FLASH_VENDMASK) {
102 printf("BRIGHT MICRO ");
105 printf("Unknown Vendor ");
109 switch (info->flash_id & FLASH_TYPEMASK) {
111 printf("29F040 or 29LV040 (4 Mbit, uniform sectors)\n");
114 printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
117 printf("AM29LV400T (4 Mbit, top boot sector)\n");
120 printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
123 printf("AM29LV800T (8 Mbit, top boot sector)\n");
126 printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
129 printf("AM29LV160T (16 Mbit, top boot sector)\n");
132 printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
135 printf("AM29LV320T (32 Mbit, top boot sector)\n");
138 printf("Unknown Chip Type\n");
142 if (info->size >> 20) {
143 printf(" Size: %ld MB in %d Sectors\n",
147 printf(" Size: %ld KB in %d Sectors\n",
152 puts(" Sector Start Addresses:");
154 for (i = 0; i < info->sector_count; ++i) {
160 info->protect[i] ? " (RO)" : " ");
166 /*-----------------------------------------------------------------------
170 * The following code cannot be run from FLASH!
173 static ulong flash_get_size(vu_long *addr, flash_info_t *info)
176 volatile unsigned char *caddr;
179 caddr = (volatile unsigned char *)addr ;
181 /* Write auto select command: read Manufacturer ID */
183 debug("Base address is: %8p\n", caddr);
185 caddr[0x0555] = 0xAA;
186 caddr[0x02AA] = 0x55;
187 caddr[0x0555] = 0x90;
191 debug("Manufact ID: %02x\n", value);
194 case 0x1: /* AMD_MANUFACT */
195 info->flash_id = FLASH_MAN_AMD;
197 case 0x4: /* FUJ_MANUFACT */
198 info->flash_id = FLASH_MAN_FUJ;
201 info->flash_id = FLASH_UNKNOWN;
202 info->sector_count = 0;
207 value = caddr[1]; /* device ID */
209 debug("Device ID: %02x\n", value);
213 info->flash_id += FLASH_AM040;
214 info->sector_count = 8;
215 info->size = 0x00080000;
216 break; /* => 512Kb */
219 info->flash_id = FLASH_UNKNOWN;
220 return 0; /* => no or unknown flash */
223 flash_get_offsets((ulong)addr, &flash_info[0]);
225 /* check for protected sectors */
226 for (i = 0; i < info->sector_count; i++) {
228 * read sector protection at sector address,
230 * D0 = 1 if protected
232 caddr = (volatile unsigned char *)(info->start[i]);
233 info->protect[i] = caddr[2] & 1;
237 * Prevent writes to uninitialized FLASH.
239 if (info->flash_id != FLASH_UNKNOWN) {
240 caddr = (volatile unsigned char *)info->start[0];
241 *caddr = 0xF0; /* reset bank */
247 int flash_erase(flash_info_t *info, int s_first, int s_last)
249 volatile unsigned char *addr =
250 (volatile unsigned char *)(info->start[0]);
251 int flag, prot, sect, l_sect;
252 ulong start, now, last;
254 if ((s_first < 0) || (s_first > s_last)) {
255 if (info->flash_id == FLASH_UNKNOWN)
256 printf("- missing\n");
258 printf("- no sectors to erase\n");
263 if ((info->flash_id == FLASH_UNKNOWN) ||
264 (info->flash_id > FLASH_AMD_COMP)) {
265 printf("Can't erase unknown flash type - aborted\n");
270 for (sect = s_first; sect <= s_last; ++sect) {
271 if (info->protect[sect])
276 printf("- Warning: %d protected sectors will not be erased!\n",
284 /* Disable interrupts which might cause a timeout here */
285 flag = disable_interrupts();
293 /* Start erase on unprotected sectors */
294 for (sect = s_first; sect <= s_last; sect++) {
295 if (info->protect[sect] == 0) { /* not protected */
296 addr = (volatile unsigned char *)(info->start[sect]);
302 /* re-enable interrupts if necessary */
306 /* wait at least 80us - let's wait 1 ms */
310 * We wait for the last triggered sector
315 start = get_timer(0);
317 addr = (volatile unsigned char *)(info->start[l_sect]);
319 while ((addr[0] & 0xFF) != 0xFF) {
321 now = get_timer(start);
323 if (now > CONFIG_SYS_FLASH_ERASE_TOUT) {
327 /* show that we're waiting */
328 if ((now - last) > 1000) { /* every second */
335 /* reset to read mode */
336 addr = (volatile unsigned char *)info->start[0];
338 addr[0] = 0xF0; /* reset bank */
344 /*-----------------------------------------------------------------------
345 * Copy memory to flash, returns:
348 * 2 - Flash not erased
351 int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
356 wp = (addr & ~3); /* get lower word aligned address */
359 * handle unaligned start bytes
365 for (i = 0, cp = wp; i < l; ++i, ++cp)
366 data = (data << 8) | (*(uchar *)cp);
368 for (; i < 4 && cnt > 0; ++i) {
369 data = (data << 8) | *src++;
373 for (; cnt == 0 && i < 4; ++i, ++cp)
374 data = (data << 8) | (*(uchar *)cp);
376 rc = write_word(info, wp, data);
385 * handle word aligned part
389 for (i = 0; i < 4; ++i)
390 data = (data << 8) | *src++;
392 rc = write_word(info, wp, data);
405 * handle unaligned tail bytes
408 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
409 data = (data << 8) | *src++;
412 for (; i < 4; ++i, ++cp)
413 data = (data << 8) | (*(uchar *)cp);
415 return write_word(info, wp, data);
418 /*-----------------------------------------------------------------------
419 * Write a word to Flash, returns:
422 * 2 - Flash not erased
424 static int write_word(flash_info_t *info, ulong dest, ulong data)
426 volatile unsigned char *cdest, *cdata;
427 volatile unsigned char *addr =
428 (volatile unsigned char *)(info->start[0]);
430 int flag, count = 4 ;
432 cdest = (volatile unsigned char *)dest ;
433 cdata = (volatile unsigned char *)&data ;
435 /* Check if Flash is (sufficiently) erased */
436 if ((*((vu_long *)dest) & data) != data)
441 /* Disable interrupts which might cause a timeout here */
442 flag = disable_interrupts();
450 /* re-enable interrupts if necessary */
454 /* data polling for D7 */
455 start = get_timer(0);
456 while ((*cdest ^ *cdata) & 0x80) {
457 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT)