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];
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 unsigned long flash_init(void)
38 unsigned long size_b0;
41 /* Init: no FLASHes known */
42 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i)
43 flash_info[i].flash_id = FLASH_UNKNOWN;
46 size_b0 = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE,
50 flash_get_offsets(CONFIG_SYS_FLASH_BASE, &flash_info[0]);
52 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
53 /* Monitor protection ON by default */
54 flash_protect(FLAG_PROTECT_SET,
55 CONFIG_SYS_MONITOR_BASE,
56 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
60 flash_info[0].size = size_b0;
65 /*-----------------------------------------------------------------------
66 * Fix this to support variable sector sizes
68 static void flash_get_offsets(ulong base, flash_info_t *info)
72 /* set up sector start address table */
73 if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) {
74 /* set sector offsets for bottom boot block type */
75 for (i = 0; i < info->sector_count; i++)
76 info->start[i] = base + (i * 0x00010000);
80 /*-----------------------------------------------------------------------
82 void flash_print_info(flash_info_t *info)
86 if (info->flash_id == FLASH_UNKNOWN) {
87 puts("missing or unknown FLASH type\n");
91 switch (info->flash_id & FLASH_VENDMASK) {
99 printf("BRIGHT MICRO ");
102 printf("Unknown Vendor ");
106 switch (info->flash_id & FLASH_TYPEMASK) {
108 printf("29F040 or 29LV040 (4 Mbit, uniform sectors)\n");
111 printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
114 printf("AM29LV400T (4 Mbit, top boot sector)\n");
117 printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
120 printf("AM29LV800T (8 Mbit, top boot sector)\n");
123 printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
126 printf("AM29LV160T (16 Mbit, top boot sector)\n");
129 printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
132 printf("AM29LV320T (32 Mbit, top boot sector)\n");
135 printf("Unknown Chip Type\n");
139 if (info->size >> 20) {
140 printf(" Size: %ld MB in %d Sectors\n",
144 printf(" Size: %ld KB in %d Sectors\n",
149 puts(" Sector Start Addresses:");
151 for (i = 0; i < info->sector_count; ++i) {
157 info->protect[i] ? " (RO)" : " ");
165 * The following code cannot be run from FLASH!
168 static ulong flash_get_size(vu_long *addr, flash_info_t *info)
171 volatile unsigned char *caddr;
174 caddr = (volatile unsigned char *)addr ;
176 /* Write auto select command: read Manufacturer ID */
178 debug("Base address is: %8p\n", caddr);
180 caddr[0x0555] = 0xAA;
181 caddr[0x02AA] = 0x55;
182 caddr[0x0555] = 0x90;
186 debug("Manufact ID: %02x\n", value);
189 case 0x01: /*AMD_MANUFACT*/
190 info->flash_id = FLASH_MAN_AMD;
193 case 0x04: /*FUJ_MANUFACT*/
194 info->flash_id = FLASH_MAN_FUJ;
198 info->flash_id = FLASH_UNKNOWN;
199 info->sector_count = 0;
204 value = caddr[1]; /* device ID */
206 debug("Device ID: %02x\n", value);
210 info->flash_id += FLASH_AM040;
211 info->sector_count = 8;
212 info->size = 0x00080000;
213 break; /* => 512Kb */
216 info->flash_id = FLASH_UNKNOWN;
217 return 0; /* => no or unknown flash */
220 flash_get_offsets((ulong)addr, &flash_info[0]);
222 /* check for protected sectors */
223 for (i = 0; i < info->sector_count; i++) {
225 * read sector protection at sector address,
227 * D0 = 1 if protected
229 caddr = (volatile unsigned char *)(info->start[i]);
230 info->protect[i] = caddr[2] & 1;
234 * Prevent writes to uninitialized FLASH.
236 if (info->flash_id != FLASH_UNKNOWN) {
237 caddr = (volatile unsigned char *)info->start[0];
238 *caddr = 0xF0; /* reset bank */
245 int flash_erase(flash_info_t *info, int s_first, int s_last)
247 volatile unsigned char *addr =
248 (volatile unsigned char *)(info->start[0]);
249 int flag, prot, sect, l_sect;
250 ulong start, now, last;
252 if ((s_first < 0) || (s_first > s_last)) {
253 if (info->flash_id == FLASH_UNKNOWN)
254 printf("- missing\n");
256 printf("- no sectors to erase\n");
261 if ((info->flash_id == FLASH_UNKNOWN) ||
262 (info->flash_id > FLASH_AMD_COMP)) {
263 printf("Can't erase unknown flash type - aborted\n");
268 for (sect = s_first; sect <= s_last; ++sect) {
269 if (info->protect[sect])
274 printf("- Warning: %d protected sectors will not be erased!\n",
282 /* Disable interrupts which might cause a timeout here */
283 flag = disable_interrupts();
291 /* Start erase on unprotected sectors */
292 for (sect = s_first; sect <= s_last; sect++) {
293 if (info->protect[sect] == 0) { /* not protected */
294 addr = (volatile unsigned char *)(info->start[sect]);
300 /* re-enable interrupts if necessary */
304 /* wait at least 80us - let's wait 1 ms */
308 * We wait for the last triggered sector
313 start = get_timer(0);
315 addr = (volatile unsigned char *)(info->start[l_sect]);
317 while ((addr[0] & 0xFF) != 0xFF) {
318 now = get_timer(start);
319 if (now > CONFIG_SYS_FLASH_ERASE_TOUT) {
323 /* show that we're waiting */
324 if ((now - last) > 1000) { /* every second */
331 /* reset to read mode */
332 addr = (volatile unsigned char *)info->start[0];
334 addr[0] = 0xF0; /* reset bank */
341 * Copy memory to flash, returns:
344 * 2 - Flash not erased
347 int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
352 wp = (addr & ~3); /* get lower word aligned address */
355 * handle unaligned start bytes
361 for (i = 0, cp = wp; i < l; ++i, ++cp)
362 data = (data << 8) | (*(uchar *)cp);
364 for (; i < 4 && cnt > 0; ++i) {
365 data = (data << 8) | *src++;
370 for (; cnt == 0 && i < 4; ++i, ++cp)
371 data = (data << 8) | (*(uchar *)cp);
373 rc = write_word(info, wp, data);
382 * handle word aligned part
386 for (i = 0; i < 4; ++i)
387 data = (data << 8) | *src++;
389 rc = write_word(info, wp, data);
402 * handle unaligned tail bytes
405 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
406 data = (data << 8) | *src++;
409 for (; i < 4; ++i, ++cp)
410 data = (data << 8) | (*(uchar *)cp);
412 return write_word(info, wp, data);
416 * Write a word to Flash, returns:
419 * 2 - Flash not erased
421 static int write_word(flash_info_t *info, ulong dest, ulong data)
423 volatile unsigned char *cdest, *cdata;
424 volatile unsigned char *addr =
425 (volatile unsigned char *)(info->start[0]);
427 int flag, count = 4 ;
429 cdest = (volatile unsigned char *)dest ;
430 cdata = (volatile unsigned char *)&data ;
432 /* Check if Flash is (sufficiently) erased */
433 if ((*((vu_long *)dest)&data) != data)
437 /* Disable interrupts which might cause a timeout here */
438 flag = disable_interrupts();
446 /* re-enable interrupts if necessary */
450 /* data polling for D7 */
451 start = get_timer(0);
452 while ((*cdest ^ *cdata) & 0x80) {
453 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT)