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,
26 * Wait for completion of each sector erase command issued
28 * Chris Hallinan - DS4.COM, Inc. - clh@net1plus.com
31 #include <asm/types.h>
32 #include <asm/u-boot.h>
33 #include <asm/processor.h>
37 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
39 /*-----------------------------------------------------------------------
42 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
43 static int write_word (flash_info_t *info, ulong dest, ulong data);
45 #ifdef MEIGSBOARD_ONBOARD_FLASH /* onboard = 2MB */
46 # ifdef CONFIG_EXBITGEN
47 # define FLASH_WORD_SIZE unsigned long
49 #else /* Meigsboard socket flash = 512KB */
50 # ifdef CONFIG_EXBITGEN
51 # define FLASH_WORD_SIZE unsigned char
55 #ifdef CONFIG_EXBITGEN
58 #define FLASH_WORD_SIZE unsigned char
61 /*-----------------------------------------------------------------------
64 unsigned long flash_init (void)
66 unsigned long bank_size;
67 unsigned long tot_size;
68 unsigned long bank_addr;
71 /* Init: no FLASHes known */
72 for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
73 flash_info[i].flash_id = FLASH_UNKNOWN;
74 flash_info[i].size = 0;
79 /* Detect Boot Flash */
80 bank_addr = CFG_FLASH0_BASE;
81 bank_size = flash_get_size((vu_long *)bank_addr, &flash_info[0]);
83 (void)flash_protect(FLAG_PROTECT_CLEAR,
85 bank_addr + bank_size - 1,
88 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
89 printf ("## Unknown FLASH on Boot Flash Bank\n");
91 flash_info[0].size = bank_size;
92 tot_size += bank_size;
94 /* Detect Application Flash */
95 bank_addr = CFG_FLASH1_BASE;
96 for (i = 1; i < CFG_MAX_FLASH_BANKS; ++i) {
97 bank_size = flash_get_size((vu_long *)bank_addr, &flash_info[i]);
98 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
102 (void)flash_protect(FLAG_PROTECT_CLEAR,
104 bank_addr + bank_size - 1,
107 flash_info[i].size = bank_size;
108 tot_size += bank_size;
109 bank_addr += bank_size;
111 if (flash_info[1].flash_id == FLASH_UNKNOWN) {
112 printf ("## Unknown FLASH on Application Flash Bank\n");
115 /* Protect monitor and environment sectors */
116 #if CFG_MONITOR_BASE >= CFG_FLASH0_BASE
117 flash_protect(FLAG_PROTECT_SET,
119 CFG_MONITOR_BASE + monitor_flash_len - 1,
121 #if 0xfffffffc >= CFG_FLASH0_BASE
122 #if 0xfffffffc <= CFG_FLASH0_BASE + CFG_FLASH0_SIZE - 1
123 flash_protect(FLAG_PROTECT_SET,
124 0xfffffffc, 0xffffffff,
130 #if (CFG_ENV_IS_IN_FLASH == 1) && defined(CFG_ENV_ADDR)
131 flash_protect(FLAG_PROTECT_SET,
133 CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
140 /*-----------------------------------------------------------------------
142 void flash_print_info (flash_info_t *info)
146 if (info->flash_id == FLASH_UNKNOWN) {
147 printf ("missing or unknown FLASH type\n");
151 switch (info->flash_id & FLASH_VENDMASK) {
152 case FLASH_MAN_AMD: printf ("AMD "); break;
153 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
154 case FLASH_MAN_SST: printf ("SST "); break;
155 default: printf ("Unknown Vendor "); break;
158 switch (info->flash_id & FLASH_TYPEMASK) {
159 case FLASH_AM040: printf ("AM29F040 (512 Kbit, uniform sector size)\n");
161 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
163 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
165 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
167 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
169 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
171 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
173 case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
175 case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
177 case FLASH_AMDLV033C: printf ("AM29LV033C (32 Mbit, uniform sector size)\n");
179 case FLASH_AMDLV065D: printf ("AM29LV065D (64 Mbit, uniform sector size)\n");
181 case FLASH_SST800A: printf ("SST39LF/VF800 (8 Mbit, uniform sector size)\n");
183 case FLASH_SST160A: printf ("SST39LF/VF160 (16 Mbit, uniform sector size)\n");
185 case FLASH_SST040: printf ("SST39LF/VF040 (4 Mbit, uniform sector size)\n");
187 default: printf ("Unknown Chip Type\n");
191 printf (" Size: %ld KB in %d Sectors\n",
192 info->size >> 10, info->sector_count);
194 printf (" Sector Start Addresses:");
195 for (i=0; i<info->sector_count; ++i) {
200 info->protect[i] ? " (RO)" : " "
206 /*-----------------------------------------------------------------------
210 /*-----------------------------------------------------------------------
214 * The following code cannot be run from FLASH!
216 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
219 FLASH_WORD_SIZE value;
220 ulong base = (ulong)addr;
221 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)addr;
223 /* Write auto select command: read Manufacturer ID */
224 addr2[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
225 addr2[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
226 addr2[ADDR0] = (FLASH_WORD_SIZE)0x00900090;
231 case (FLASH_WORD_SIZE)AMD_MANUFACT:
232 info->flash_id = FLASH_MAN_AMD;
234 case (FLASH_WORD_SIZE)FUJ_MANUFACT:
235 info->flash_id = FLASH_MAN_FUJ;
237 case (FLASH_WORD_SIZE)SST_MANUFACT:
238 info->flash_id = FLASH_MAN_SST;
241 info->flash_id = FLASH_UNKNOWN;
242 info->sector_count = 0;
244 return (0); /* no or unknown flash */
247 value = addr2[1]; /* device ID */
250 case (FLASH_WORD_SIZE)AMD_ID_F040B:
251 info->flash_id += FLASH_AM040;
252 info->sector_count = 8;
253 info->size = 0x0080000; /* => 512 ko */
255 case (FLASH_WORD_SIZE)AMD_ID_LV400T:
256 info->flash_id += FLASH_AM400T;
257 info->sector_count = 11;
258 info->size = 0x00080000;
259 break; /* => 0.5 MB */
261 case (FLASH_WORD_SIZE)AMD_ID_LV400B:
262 info->flash_id += FLASH_AM400B;
263 info->sector_count = 11;
264 info->size = 0x00080000;
265 break; /* => 0.5 MB */
267 case (FLASH_WORD_SIZE)AMD_ID_LV800T:
268 info->flash_id += FLASH_AM800T;
269 info->sector_count = 19;
270 info->size = 0x00100000;
273 case (FLASH_WORD_SIZE)AMD_ID_LV800B:
274 info->flash_id += FLASH_AM800B;
275 info->sector_count = 19;
276 info->size = 0x00100000;
279 case (FLASH_WORD_SIZE)AMD_ID_LV160T:
280 info->flash_id += FLASH_AM160T;
281 info->sector_count = 35;
282 info->size = 0x00200000;
285 case (FLASH_WORD_SIZE)AMD_ID_LV160B:
286 info->flash_id += FLASH_AM160B;
287 info->sector_count = 35;
288 info->size = 0x00200000;
291 case (FLASH_WORD_SIZE)AMD_ID_LV033C:
292 info->flash_id += FLASH_AMDLV033C;
293 info->sector_count = 64;
294 info->size = 0x00400000;
297 case (FLASH_WORD_SIZE)AMD_ID_LV065D:
298 info->flash_id += FLASH_AMDLV065D;
299 info->sector_count = 128;
300 info->size = 0x00800000;
303 case (FLASH_WORD_SIZE)AMD_ID_LV320T:
304 info->flash_id += FLASH_AM320T;
305 info->sector_count = 67;
306 info->size = 0x00400000;
309 case (FLASH_WORD_SIZE)AMD_ID_LV320B:
310 info->flash_id += FLASH_AM320B;
311 info->sector_count = 67;
312 info->size = 0x00400000;
315 case (FLASH_WORD_SIZE)SST_ID_xF800A:
316 info->flash_id += FLASH_SST800A;
317 info->sector_count = 16;
318 info->size = 0x00100000;
321 case (FLASH_WORD_SIZE)SST_ID_xF160A:
322 info->flash_id += FLASH_SST160A;
323 info->sector_count = 32;
324 info->size = 0x00200000;
326 case (FLASH_WORD_SIZE)SST_ID_xF040:
327 info->flash_id += FLASH_SST040;
328 info->sector_count = 128;
329 info->size = 0x00080000;
330 break; /* => 512KB */
333 info->flash_id = FLASH_UNKNOWN;
334 return (0); /* => no or unknown flash */
338 /* set up sector start address table */
339 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
340 (info->flash_id == FLASH_AM040) ||
341 (info->flash_id == FLASH_AMDLV033C) ||
342 (info->flash_id == FLASH_AMDLV065D)) {
343 ulong sectsize = info->size / info->sector_count;
344 for (i = 0; i < info->sector_count; i++)
345 info->start[i] = base + (i * sectsize);
347 if (info->flash_id & FLASH_BTYPE) {
348 /* set sector offsets for bottom boot block type */
349 info->start[0] = base + 0x00000000;
350 info->start[1] = base + 0x00004000;
351 info->start[2] = base + 0x00006000;
352 info->start[3] = base + 0x00008000;
353 for (i = 4; i < info->sector_count; i++) {
354 info->start[i] = base + (i * 0x00010000) - 0x00030000;
357 /* set sector offsets for top boot block type */
358 i = info->sector_count - 1;
359 info->start[i--] = base + info->size - 0x00004000;
360 info->start[i--] = base + info->size - 0x00006000;
361 info->start[i--] = base + info->size - 0x00008000;
362 for (; i >= 0; i--) {
363 info->start[i] = base + i * 0x00010000;
368 /* check for protected sectors */
369 for (i = 0; i < info->sector_count; i++) {
370 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
371 /* D0 = 1 if protected */
373 addr2 = (volatile FLASH_WORD_SIZE *)(info->start[i]);
374 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
375 info->protect[i] = 0;
377 info->protect[i] = addr2[2] & 1;
380 /* switch to the read mode */
381 if (info->flash_id != FLASH_UNKNOWN) {
382 addr2 = (FLASH_WORD_SIZE *)info->start[0];
383 *addr2 = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
389 /*-----------------------------------------------------------------------
392 int flash_erase (flash_info_t *info, int s_first, int s_last)
394 volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[0]);
395 volatile FLASH_WORD_SIZE *addr2;
396 int flag, prot, sect;
397 ulong start, now, last;
399 if ((s_first < 0) || (s_first > s_last)) {
400 if (info->flash_id == FLASH_UNKNOWN) {
401 printf ("- missing\n");
403 printf ("- no sectors to erase\n");
408 if (info->flash_id == FLASH_UNKNOWN) {
409 printf ("Can't erase unknown flash type - aborted\n");
414 for (sect=s_first; sect<=s_last; ++sect) {
415 if (info->protect[sect]) {
421 printf ("- Warning: %d protected sectors will not be erased!\n",
427 start = get_timer (0);
429 /* Start erase on unprotected sectors */
430 for (sect = s_first; sect <= s_last; sect++) {
431 if (info->protect[sect] == 0) { /* not protected */
432 addr2 = (FLASH_WORD_SIZE *)(info->start[sect]);
434 /* Disable interrupts which might cause a timeout here */
435 flag = disable_interrupts();
437 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
438 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
439 addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
440 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
441 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
442 addr2[0] = (FLASH_WORD_SIZE)0x00300030;
444 /* re-enable interrupts if necessary */
448 /* wait at least 80us - let's wait 1 ms */
451 while ((addr2[0] & 0x00800080) !=
452 (FLASH_WORD_SIZE) 0x00800080) {
453 if ((now=get_timer(start)) >
454 CFG_FLASH_ERASE_TOUT) {
455 printf ("Timeout\n");
456 addr[0] = (FLASH_WORD_SIZE)0x00F000F0;
460 /* show that we're waiting */
461 if ((now - last) > 1000) { /* every second */
467 addr[0] = (FLASH_WORD_SIZE)0x00F000F0;
476 /*-----------------------------------------------------------------------
477 * Copy memory to flash, returns:
480 * 2 - Flash not erased
483 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
488 wp = (addr & ~3); /* get lower word aligned address */
491 * handle unaligned start bytes
493 if ((l = addr - wp) != 0) {
495 for (i=0, cp=wp; i<l; ++i, ++cp) {
496 data = (data << 8) | (*(uchar *)cp);
498 for (; i<4 && cnt>0; ++i) {
499 data = (data << 8) | *src++;
503 for (; cnt==0 && i<4; ++i, ++cp) {
504 data = (data << 8) | (*(uchar *)cp);
507 if ((rc = write_word(info, wp, data)) != 0) {
514 * handle word aligned part
518 for (i=0; i<4; ++i) {
519 data = (data << 8) | *src++;
521 if ((rc = write_word(info, wp, data)) != 0) {
533 * handle unaligned tail bytes
536 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
537 data = (data << 8) | *src++;
540 for (; i<4; ++i, ++cp) {
541 data = (data << 8) | (*(uchar *)cp);
544 return (write_word(info, wp, data));
547 /*-----------------------------------------------------------------------
548 * Write a word to Flash, returns:
551 * 2 - Flash not erased
553 static int write_word (flash_info_t *info, ulong dest, ulong data)
555 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)(info->start[0]);
556 volatile FLASH_WORD_SIZE *dest2 = (FLASH_WORD_SIZE *)dest;
557 volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *)&data;
562 /* Check if Flash is (sufficiently) erased */
563 if ((*((volatile ulong *)dest) & data) != data) {
564 printf("dest = %08lx, *dest = %08lx, data = %08lx\n",
565 dest, *(volatile ulong *)dest, data);
569 for (i=0; i < 4/sizeof(FLASH_WORD_SIZE); i++) {
570 /* Disable interrupts which might cause a timeout here */
571 flag = disable_interrupts();
573 addr2[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
574 addr2[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
575 addr2[ADDR0] = (FLASH_WORD_SIZE)0x00A000A0;
578 /* re-enable interrupts if necessary */
582 /* data polling for D7 */
583 start = get_timer (0);
584 while ((dest2[i] & 0x00800080) != (data2[i] & 0x00800080)) {
585 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
586 addr2[0] = (FLASH_WORD_SIZE)0x00F000F0;
592 addr2[0] = (FLASH_WORD_SIZE)0x00F000F0;
597 /*-----------------------------------------------------------------------