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[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
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 < CFG_MAX_FLASH_BANKS; ++i)
46 flash_info[i].flash_id = FLASH_UNKNOWN;
49 size_b0 = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
52 flash_get_offsets (CFG_FLASH_BASE, &flash_info[0]);
54 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
55 /* Monitor protection ON by default */
56 flash_protect(FLAG_PROTECT_SET,
58 CFG_MONITOR_BASE+monitor_flash_len-1,
64 flash_info[1].flash_id = FLASH_UNKNOWN;
65 flash_info[1].sector_count = -1;
67 flash_info[0].size = size_b0;
68 flash_info[1].size = size_b1;
70 return (size_b0 + size_b1);
73 /*-----------------------------------------------------------------------
74 * Fix this to support variable sector sizes
76 static void flash_get_offsets (ulong base, flash_info_t *info)
80 /* set up sector start address table */
81 if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) {
82 /* set sector offsets for bottom boot block type */
83 for (i = 0; i < info->sector_count; i++)
84 info->start[i] = base + (i * 0x00010000);
88 /*-----------------------------------------------------------------------
90 void flash_print_info (flash_info_t *info)
94 if (info->flash_id == FLASH_UNKNOWN)
96 puts ("missing or unknown FLASH type\n");
100 switch (info->flash_id & FLASH_VENDMASK)
102 case FLASH_MAN_AMD: printf ("AMD "); break;
103 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
104 case FLASH_MAN_BM: printf ("BRIGHT MICRO "); break;
105 default: printf ("Unknown Vendor "); break;
108 switch (info->flash_id & FLASH_TYPEMASK)
110 case FLASH_AM040: printf ("29F040 or 29LV040 (4 Mbit, uniform sectors)\n");
112 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
114 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
116 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
118 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
120 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
122 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
124 case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
126 case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
128 default: printf ("Unknown Chip Type\n");
132 if (info->size >> 20) {
133 printf (" Size: %ld MB in %d Sectors\n",
137 printf (" Size: %ld KB in %d Sectors\n",
142 puts (" Sector Start Addresses:");
144 for (i=0; i<info->sector_count; ++i)
153 info->protect[i] ? " (RO)" : " ");
159 /*-----------------------------------------------------------------------
163 * The following code cannot be run from FLASH!
166 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
169 volatile unsigned char *caddr;
172 caddr = (volatile unsigned char *)addr ;
174 /* Write auto select command: read Manufacturer ID */
177 printf("Base address is: %08x\n", caddr);
180 caddr[0x0555] = 0xAA;
181 caddr[0x02AA] = 0x55;
182 caddr[0x0555] = 0x90;
187 printf("Manufact ID: %02x\n", value);
191 case 0x1: /* AMD_MANUFACT */
192 info->flash_id = FLASH_MAN_AMD;
195 case 0x4: /* FUJ_MANUFACT */
196 info->flash_id = FLASH_MAN_FUJ;
200 info->flash_id = FLASH_UNKNOWN;
201 info->sector_count = 0;
206 value = caddr[1]; /* device ID */
208 printf("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 */
224 flash_get_offsets ((ulong)addr, &flash_info[0]);
226 /* check for protected sectors */
227 for (i = 0; i < info->sector_count; i++)
229 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
230 /* D0 = 1 if protected */
231 caddr = (volatile unsigned char *)(info->start[i]);
232 info->protect[i] = caddr[2] & 1;
236 * Prevent writes to uninitialized FLASH.
238 if (info->flash_id != FLASH_UNKNOWN)
240 caddr = (volatile unsigned char *)info->start[0];
241 *caddr = 0xF0; /* reset bank */
248 /*-----------------------------------------------------------------------
251 int flash_erase (flash_info_t *info, int s_first, int s_last)
253 volatile unsigned char *addr = (volatile unsigned char *)(info->start[0]);
254 int flag, prot, sect, l_sect;
255 ulong start, now, last;
257 if ((s_first < 0) || (s_first > s_last)) {
258 if (info->flash_id == FLASH_UNKNOWN) {
259 printf ("- missing\n");
261 printf ("- no sectors to erase\n");
266 if ((info->flash_id == FLASH_UNKNOWN) ||
267 (info->flash_id > FLASH_AMD_COMP)) {
268 printf ("Can't erase unknown flash type - aborted\n");
273 for (sect=s_first; sect<=s_last; ++sect) {
274 if (info->protect[sect]) {
280 printf ("- Warning: %d protected sectors will not be erased!\n",
288 /* Disable interrupts which might cause a timeout here */
289 flag = disable_interrupts();
297 /* Start erase on unprotected sectors */
298 for (sect = s_first; sect<=s_last; sect++) {
299 if (info->protect[sect] == 0) { /* not protected */
300 addr = (volatile unsigned char *)(info->start[sect]);
306 /* re-enable interrupts if necessary */
310 /* wait at least 80us - let's wait 1 ms */
314 * We wait for the last triggered sector
319 start = get_timer (0);
321 addr = (volatile unsigned char *)(info->start[l_sect]);
323 while ((addr[0] & 0xFF) != 0xFF)
325 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
326 printf ("Timeout\n");
329 /* show that we're waiting */
330 if ((now - last) > 1000) { /* every second */
337 /* reset to read mode */
338 addr = (volatile unsigned char *)info->start[0];
340 addr[0] = 0xF0; /* reset bank */
346 /*-----------------------------------------------------------------------
347 * Copy memory to flash, returns:
350 * 2 - Flash not erased
353 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
358 wp = (addr & ~3); /* get lower word aligned address */
361 * handle unaligned start bytes
363 if ((l = addr - wp) != 0) {
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);
377 if ((rc = write_word(info, wp, data)) != 0) {
384 * handle word aligned part
388 for (i=0; i<4; ++i) {
389 data = (data << 8) | *src++;
391 if ((rc = write_word(info, wp, data)) != 0) {
403 * handle unaligned tail bytes
406 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
407 data = (data << 8) | *src++;
410 for (; i<4; ++i, ++cp) {
411 data = (data << 8) | (*(uchar *)cp);
414 return (write_word(info, wp, data));
417 /*-----------------------------------------------------------------------
418 * Write a word to Flash, returns:
421 * 2 - Flash not erased
423 static int write_word (flash_info_t *info, ulong dest, ulong data)
425 volatile unsigned char *addr = (volatile unsigned char*)(info->start[0]),
428 int flag, count = 4 ;
430 cdest = (volatile unsigned char *)dest ;
431 cdata = (volatile unsigned char *)&data ;
433 /* Check if Flash is (sufficiently) erased */
434 if ((*((vu_long *)dest) & data) != data) {
440 /* Disable interrupts which might cause a timeout here */
441 flag = disable_interrupts();
449 /* re-enable interrupts if necessary */
453 /* data polling for D7 */
454 start = get_timer (0);
455 while ((*cdest ^ *cdata) & 0x80)
457 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
468 /*-----------------------------------------------------------------------