2 * (C) Copyright 2000-2004
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
33 #include <asm/processor.h>
35 #if CFG_MAX_FLASH_BANKS != 1
36 #error "CFG_MAX_FLASH_BANKS must be 1"
38 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
40 /*-----------------------------------------------------------------------
43 static ulong flash_get_size (vu_long * addr, flash_info_t * info);
44 static int write_word (flash_info_t * info, ulong dest, ulong data);
45 static void flash_get_offsets (ulong base, flash_info_t * info);
49 #define FLASH_WORD_SIZE unsigned char
51 /*-----------------------------------------------------------------------
54 unsigned long flash_init (void)
56 unsigned long size_b0;
57 unsigned long base_b0;
59 /* Init: no FLASHes known */
60 flash_info[0].flash_id = FLASH_UNKNOWN;
62 /* Static FLASH Bank configuration here - FIXME XXX */
65 flash_get_size ((vu_long *) FLASH_BASE0_PRELIM,
68 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
69 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n", size_b0, size_b0 << 20);
74 flash_get_offsets (FLASH_BASE0_PRELIM, &flash_info[0]);
76 /* Monitor protection ON by default */
77 (void) flash_protect (FLAG_PROTECT_SET,
79 FLASH_BASE0_PRELIM + monitor_flash_len - 1,
81 flash_info[0].size = size_b0;
87 /*-----------------------------------------------------------------------
89 static void flash_get_offsets (ulong base, flash_info_t * info)
93 /* set up sector start address table */
94 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
95 (info->flash_id == FLASH_AM040)) {
96 for (i = 0; i < info->sector_count; i++)
97 info->start[i] = base + (i * 0x00010000);
99 if (info->flash_id & FLASH_BTYPE) {
100 /* set sector offsets for bottom boot block type */
101 info->start[0] = base + 0x00000000;
102 info->start[1] = base + 0x00004000;
103 info->start[2] = base + 0x00006000;
104 info->start[3] = base + 0x00008000;
105 for (i = 4; i < info->sector_count; i++) {
107 base + (i * 0x00010000) - 0x00030000;
110 /* set sector offsets for top boot block type */
111 i = info->sector_count - 1;
112 info->start[i--] = base + info->size - 0x00004000;
113 info->start[i--] = base + info->size - 0x00006000;
114 info->start[i--] = base + info->size - 0x00008000;
115 for (; i >= 0; i--) {
116 info->start[i] = base + i * 0x00010000;
122 /*-----------------------------------------------------------------------
124 void flash_print_info (flash_info_t * info)
130 volatile unsigned long *flash;
132 if (info->flash_id == FLASH_UNKNOWN) {
133 printf ("missing or unknown FLASH type\n");
137 switch (info->flash_id & FLASH_VENDMASK) {
148 printf ("Unknown Vendor ");
152 switch (info->flash_id & FLASH_TYPEMASK) {
154 printf ("AM29F040 (512 Kbit, uniform sector size)\n");
157 printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
160 printf ("AM29LV400T (4 Mbit, top boot sector)\n");
163 printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
166 printf ("AM29LV800T (8 Mbit, top boot sector)\n");
169 printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
172 printf ("AM29LV160T (16 Mbit, top boot sector)\n");
175 printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
178 printf ("AM29LV320T (32 Mbit, top boot sector)\n");
181 printf ("SST39LF/VF800 (8 Mbit, uniform sector size)\n");
184 printf ("SST39LF/VF160 (16 Mbit, uniform sector size)\n");
187 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) {
197 * Check if whole sector is erased
199 if (i != (info->sector_count - 1))
200 size = info->start[i + 1] - info->start[i];
202 size = info->start[0] + info->size - info->start[i];
204 flash = (volatile unsigned long *) info->start[i];
205 size = size >> 2; /* divide by 4 for longword access */
206 for (k = 0; k < size; k++) {
207 if (*flash++ != 0xffffffff) {
215 #if 0 /* test-only */
217 info->start[i], info->protect[i] ? " (RO)" : " "
219 printf (" %08lX%s%s",
221 erased ? " E" : " ", info->protect[i] ? "RO " : " "
229 /*-----------------------------------------------------------------------
233 /*-----------------------------------------------------------------------
237 * The following code cannot be run from FLASH!
239 static ulong flash_get_size (vu_long * addr, flash_info_t * info)
242 FLASH_WORD_SIZE value;
243 ulong base = (ulong) addr;
244 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *) addr;
246 /* Write auto select command: read Manufacturer ID */
247 addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
248 addr2[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
249 addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00900090;
258 case (FLASH_WORD_SIZE) AMD_MANUFACT:
259 info->flash_id = FLASH_MAN_AMD;
261 case (FLASH_WORD_SIZE) FUJ_MANUFACT:
262 info->flash_id = FLASH_MAN_FUJ;
264 case (FLASH_WORD_SIZE) SST_MANUFACT:
265 info->flash_id = FLASH_MAN_SST;
268 info->flash_id = FLASH_UNKNOWN;
269 info->sector_count = 0;
271 return (0); /* no or unknown flash */
275 value = addr2[0]; /* device ID */
276 /* printf("\ndev_code=%x\n", value); */
278 value = addr2[1]; /* device ID */
282 case (FLASH_WORD_SIZE) AMD_ID_F040B:
283 info->flash_id += FLASH_AM040;
284 info->sector_count = 8;
285 info->size = 0x0080000; /* => 512 ko */
287 case (FLASH_WORD_SIZE) AMD_ID_LV040B:
288 info->flash_id += FLASH_AM040;
289 info->sector_count = 8;
290 info->size = 0x0080000; /* => 512 ko */
292 case (FLASH_WORD_SIZE) AMD_ID_LV400T:
293 info->flash_id += FLASH_AM400T;
294 info->sector_count = 11;
295 info->size = 0x00080000;
296 break; /* => 0.5 MB */
298 case (FLASH_WORD_SIZE) AMD_ID_LV400B:
299 info->flash_id += FLASH_AM400B;
300 info->sector_count = 11;
301 info->size = 0x00080000;
302 break; /* => 0.5 MB */
304 case (FLASH_WORD_SIZE) AMD_ID_LV800T:
305 info->flash_id += FLASH_AM800T;
306 info->sector_count = 19;
307 info->size = 0x00100000;
310 case (FLASH_WORD_SIZE) AMD_ID_LV800B:
311 info->flash_id += FLASH_AM800B;
312 info->sector_count = 19;
313 info->size = 0x00100000;
316 case (FLASH_WORD_SIZE) AMD_ID_LV160T:
317 info->flash_id += FLASH_AM160T;
318 info->sector_count = 35;
319 info->size = 0x00200000;
322 case (FLASH_WORD_SIZE) AMD_ID_LV160B:
323 info->flash_id += FLASH_AM160B;
324 info->sector_count = 35;
325 info->size = 0x00200000;
327 #if 0 /* enable when device IDs are available */
328 case (FLASH_WORD_SIZE) AMD_ID_LV320T:
329 info->flash_id += FLASH_AM320T;
330 info->sector_count = 67;
331 info->size = 0x00400000;
334 case (FLASH_WORD_SIZE) AMD_ID_LV320B:
335 info->flash_id += FLASH_AM320B;
336 info->sector_count = 67;
337 info->size = 0x00400000;
340 case (FLASH_WORD_SIZE) SST_ID_xF800A:
341 info->flash_id += FLASH_SST800A;
342 info->sector_count = 16;
343 info->size = 0x00100000;
346 case (FLASH_WORD_SIZE) SST_ID_xF160A:
347 info->flash_id += FLASH_SST160A;
348 info->sector_count = 32;
349 info->size = 0x00200000;
353 info->flash_id = FLASH_UNKNOWN;
354 return (0); /* => no or unknown flash */
358 /* set up sector start address table */
359 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
360 (info->flash_id == FLASH_AM040)) {
361 for (i = 0; i < info->sector_count; i++)
362 info->start[i] = base + (i * 0x00010000);
364 if (info->flash_id & FLASH_BTYPE) {
365 /* set sector offsets for bottom boot block type */
366 info->start[0] = base + 0x00000000;
367 info->start[1] = base + 0x00004000;
368 info->start[2] = base + 0x00006000;
369 info->start[3] = base + 0x00008000;
370 for (i = 4; i < info->sector_count; i++) {
372 base + (i * 0x00010000) - 0x00030000;
375 /* set sector offsets for top boot block type */
376 i = info->sector_count - 1;
377 info->start[i--] = base + info->size - 0x00004000;
378 info->start[i--] = base + info->size - 0x00006000;
379 info->start[i--] = base + info->size - 0x00008000;
380 for (; i >= 0; i--) {
381 info->start[i] = base + i * 0x00010000;
386 /* check for protected sectors */
387 for (i = 0; i < info->sector_count; i++) {
388 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
389 /* D0 = 1 if protected */
391 addr2 = (volatile FLASH_WORD_SIZE *) (info->start[i]);
392 info->protect[i] = addr2[4] & 1;
394 addr2 = (volatile FLASH_WORD_SIZE *) (info->start[i]);
395 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
396 info->protect[i] = 0;
398 info->protect[i] = addr2[2] & 1;
403 * Prevent writes to uninitialized FLASH.
405 if (info->flash_id != FLASH_UNKNOWN) {
406 #if 0 /* test-only */
408 addr2 = (volatile unsigned char *) info->start[0];
411 addr2[ADDR0] = 0xF0; /* reset bank */
413 addr2 = (FLASH_WORD_SIZE *) info->start[0];
414 *addr2 = (FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
416 #else /* test-only */
417 addr2 = (FLASH_WORD_SIZE *) info->start[0];
418 *addr2 = (FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
419 #endif /* test-only */
425 int wait_for_DQ7 (flash_info_t * info, int sect)
427 ulong start, now, last;
428 volatile FLASH_WORD_SIZE *addr =
429 (FLASH_WORD_SIZE *) (info->start[sect]);
431 start = get_timer (0);
433 while ((addr[0] & (FLASH_WORD_SIZE) 0x00800080) !=
434 (FLASH_WORD_SIZE) 0x00800080) {
435 if ((now = get_timer (start)) > CFG_FLASH_ERASE_TOUT) {
436 printf ("Timeout\n");
439 /* show that we're waiting */
440 if ((now - last) > 1000) { /* every second */
448 /*-----------------------------------------------------------------------
451 int flash_erase (flash_info_t * info, int s_first, int s_last)
453 volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *) (info->start[0]);
454 volatile FLASH_WORD_SIZE *addr2;
455 int flag, prot, sect, l_sect;
458 if ((s_first < 0) || (s_first > s_last)) {
459 if (info->flash_id == FLASH_UNKNOWN) {
460 printf ("- missing\n");
462 printf ("- no sectors to erase\n");
467 if (info->flash_id == FLASH_UNKNOWN) {
468 printf ("Can't erase unknown flash type - aborted\n");
473 for (sect = s_first; sect <= s_last; ++sect) {
474 if (info->protect[sect]) {
480 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
487 /* Disable interrupts which might cause a timeout here */
488 flag = disable_interrupts ();
490 /* Start erase on unprotected sectors */
491 for (sect = s_first; sect <= s_last; sect++) {
492 if (info->protect[sect] == 0) { /* not protected */
493 addr2 = (FLASH_WORD_SIZE *) (info->start[sect]);
494 printf ("Erasing sector %p\n", addr2); /* CLH */
496 if ((info->flash_id & FLASH_VENDMASK) ==
498 addr[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
499 addr[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
500 addr[ADDR0] = (FLASH_WORD_SIZE) 0x00800080;
501 addr[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
502 addr[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
503 addr2[0] = (FLASH_WORD_SIZE) 0x00500050; /* block erase */
504 for (i = 0; i < 50; i++)
505 udelay (1000); /* wait 1 ms */
507 addr[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
508 addr[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
509 addr[ADDR0] = (FLASH_WORD_SIZE) 0x00800080;
510 addr[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
511 addr[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
512 addr2[0] = (FLASH_WORD_SIZE) 0x00300030; /* sector erase */
516 * Wait for each sector to complete, it's more
517 * reliable. According to AMD Spec, you must
518 * issue all erase commands within a specified
519 * timeout. This has been seen to fail, especially
520 * if printf()s are included (for debug)!!
522 wait_for_DQ7 (info, sect);
526 /* re-enable interrupts if necessary */
528 enable_interrupts ();
530 /* wait at least 80us - let's wait 1 ms */
535 * We wait for the last triggered sector
539 wait_for_DQ7 (info, l_sect);
543 /* reset to read mode */
544 addr = (FLASH_WORD_SIZE *) info->start[0];
545 addr[0] = (FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
551 /*-----------------------------------------------------------------------
552 * Copy memory to flash, returns:
555 * 2 - Flash not erased
558 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
563 wp = (addr & ~3); /* get lower word aligned address */
566 * handle unaligned start bytes
568 if ((l = addr - wp) != 0) {
570 for (i = 0, cp = wp; i < l; ++i, ++cp) {
571 data = (data << 8) | (*(uchar *) cp);
573 for (; i < 4 && cnt > 0; ++i) {
574 data = (data << 8) | *src++;
578 for (; cnt == 0 && i < 4; ++i, ++cp) {
579 data = (data << 8) | (*(uchar *) cp);
582 if ((rc = write_word (info, wp, data)) != 0) {
589 * handle word aligned part
593 for (i = 0; i < 4; ++i) {
594 data = (data << 8) | *src++;
596 if ((rc = write_word (info, wp, data)) != 0) {
608 * handle unaligned tail bytes
611 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
612 data = (data << 8) | *src++;
615 for (; i < 4; ++i, ++cp) {
616 data = (data << 8) | (*(uchar *) cp);
619 return (write_word (info, wp, data));
622 /*-----------------------------------------------------------------------
623 * Write a word to Flash, returns:
626 * 2 - Flash not erased
628 static int write_word (flash_info_t * info, ulong dest, ulong data)
630 volatile FLASH_WORD_SIZE *addr2 =
631 (FLASH_WORD_SIZE *) (info->start[0]);
632 volatile FLASH_WORD_SIZE *dest2 = (FLASH_WORD_SIZE *) dest;
633 volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *) & data;
637 /* Check if Flash is (sufficiently) erased */
638 if ((*((volatile FLASH_WORD_SIZE *) dest) &
639 (FLASH_WORD_SIZE) data) != (FLASH_WORD_SIZE) data) {
643 for (i = 0; i < 4 / sizeof (FLASH_WORD_SIZE); i++) {
646 /* Disable interrupts which might cause a timeout here */
647 flag = disable_interrupts ();
649 addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
650 addr2[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
651 addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00A000A0;
655 /* re-enable interrupts if necessary */
657 enable_interrupts ();
659 /* data polling for D7 */
660 start = get_timer (0);
661 while ((dest2[i] & (FLASH_WORD_SIZE) 0x00800080) !=
662 (data2[i] & (FLASH_WORD_SIZE) 0x00800080)) {
664 if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {