3 * Stefan Roese, esd gmbh germany, stefan.roese@esd-electronics.com
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 #include <asm/processor.h>
28 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
30 /*-----------------------------------------------------------------------
33 static int write_word (flash_info_t *info, ulong dest, ulong data);
35 /*-----------------------------------------------------------------------
37 static void flash_get_offsets (ulong base, flash_info_t *info)
42 /* set up sector start address table */
43 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
44 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM640U)) {
45 for (i = 0; i < info->sector_count; i++)
46 info->start[i] = base + (i * 0x00010000);
47 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322B) ||
48 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323B) ||
49 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
50 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324B)) {
51 /* set sector offsets for bottom boot block type */
52 for (i=0; i<8; ++i) { /* 8 x 8k boot sectors */
53 info->start[i] = base;
56 while (i < info->sector_count) { /* 64k regular sectors */
57 info->start[i] = base;
61 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322T) ||
62 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323T) ||
63 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
64 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324T)) {
65 /* set sector offsets for top boot block type */
67 i = info->sector_count;
68 for (n=0; n<8; ++n) { /* 8 x 8k boot sectors */
71 info->start[i] = base;
73 while (i > 0) { /* 64k regular sectors */
76 info->start[i] = base;
79 if (info->flash_id & FLASH_BTYPE) {
80 /* set sector offsets for bottom boot block type */
81 info->start[0] = base + 0x00000000;
82 info->start[1] = base + 0x00004000;
83 info->start[2] = base + 0x00006000;
84 info->start[3] = base + 0x00008000;
85 for (i = 4; i < info->sector_count; i++) {
86 info->start[i] = base + (i * 0x00010000) - 0x00030000;
89 /* set sector offsets for top boot block type */
90 i = info->sector_count - 1;
91 info->start[i--] = base + info->size - 0x00004000;
92 info->start[i--] = base + info->size - 0x00006000;
93 info->start[i--] = base + info->size - 0x00008000;
95 info->start[i] = base + i * 0x00010000;
101 /*-----------------------------------------------------------------------
103 void flash_print_info (flash_info_t *info)
109 volatile unsigned long *flash;
111 if (info->flash_id == FLASH_UNKNOWN) {
112 printf ("missing or unknown FLASH type\n");
116 switch (info->flash_id & FLASH_VENDMASK) {
117 case FLASH_MAN_AMD: printf ("AMD "); break;
118 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
119 case FLASH_MAN_SST: printf ("SST "); break;
120 case FLASH_MAN_EXCEL: printf ("Excel Semiconductor "); break;
121 default: printf ("Unknown Vendor "); break;
124 switch (info->flash_id & FLASH_TYPEMASK) {
125 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
127 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
129 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
131 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
133 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
135 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
137 case FLASH_AM320T: printf ("AM29LV320T (32 M, top sector)\n");
139 case FLASH_AM320B: printf ("AM29LV320B (32 M, bottom sector)\n");
141 case FLASH_AMDL322T: printf ("AM29DL322T (32 M, top sector)\n");
143 case FLASH_AMDL322B: printf ("AM29DL322B (32 M, bottom sector)\n");
145 case FLASH_AMDL323T: printf ("AM29DL323T (32 M, top sector)\n");
147 case FLASH_AMDL323B: printf ("AM29DL323B (32 M, bottom sector)\n");
149 case FLASH_AM640U: printf ("AM29LV640D (64 M, uniform sector)\n");
151 case FLASH_SST800A: printf ("SST39LF/VF800 (8 Mbit, uniform sector size)\n");
153 case FLASH_SST160A: printf ("SST39LF/VF160 (16 Mbit, uniform sector size)\n");
155 case FLASH_SST320: printf ("SST39LF/VF320 (32 Mbit, uniform sector size)\n");
157 case FLASH_SST640: printf ("SST39LF/VF640 (64 Mbit, uniform sector size)\n");
159 default: printf ("Unknown Chip Type\n");
163 printf (" Size: %ld MB in %d Sectors\n",
164 info->size >> 20, info->sector_count);
166 printf (" Sector Start Addresses:");
167 for (i=0; i<info->sector_count; ++i) {
168 #ifdef CFG_FLASH_EMPTY_INFO
170 * Check if whole sector is erased
172 if (i != (info->sector_count-1))
173 size = info->start[i+1] - info->start[i];
175 size = info->start[0] + info->size - info->start[i];
177 flash = (volatile unsigned long *)info->start[i];
178 size = size >> 2; /* divide by 4 for longword access */
179 for (k=0; k<size; k++)
181 if (*flash++ != 0xffffffff)
190 /* print empty and read-only info */
191 printf (" %08lX%s%s",
194 info->protect[i] ? "RO " : " ");
200 info->protect[i] ? " (RO)" : " ");
208 /*-----------------------------------------------------------------------
212 /*-----------------------------------------------------------------------
216 * The following code cannot be run from FLASH!
218 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
222 CFG_FLASH_WORD_SIZE value;
223 ulong base = (ulong)addr;
224 volatile CFG_FLASH_WORD_SIZE *addr2 = (CFG_FLASH_WORD_SIZE *)addr;
226 /* Write auto select command: read Manufacturer ID */
227 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
228 addr2[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
229 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00900090;
231 value = addr2[CFG_FLASH_READ0];
234 case (CFG_FLASH_WORD_SIZE)AMD_MANUFACT:
235 info->flash_id = FLASH_MAN_AMD;
237 case (CFG_FLASH_WORD_SIZE)FUJ_MANUFACT:
238 info->flash_id = FLASH_MAN_FUJ;
240 case (CFG_FLASH_WORD_SIZE)SST_MANUFACT:
241 info->flash_id = FLASH_MAN_SST;
243 case (CFG_FLASH_WORD_SIZE)EXCEL_MANUFACT:
244 info->flash_id = FLASH_MAN_EXCEL;
247 info->flash_id = FLASH_UNKNOWN;
248 info->sector_count = 0;
250 return (0); /* no or unknown flash */
253 value = addr2[CFG_FLASH_READ1]; /* device ID */
256 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV400T:
257 info->flash_id += FLASH_AM400T;
258 info->sector_count = 11;
259 info->size = 0x00080000;
260 break; /* => 0.5 MB */
262 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV400B:
263 info->flash_id += FLASH_AM400B;
264 info->sector_count = 11;
265 info->size = 0x00080000;
266 break; /* => 0.5 MB */
268 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV800T:
269 info->flash_id += FLASH_AM800T;
270 info->sector_count = 19;
271 info->size = 0x00100000;
274 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV800B:
275 info->flash_id += FLASH_AM800B;
276 info->sector_count = 19;
277 info->size = 0x00100000;
280 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV160T:
281 info->flash_id += FLASH_AM160T;
282 info->sector_count = 35;
283 info->size = 0x00200000;
286 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV160B:
287 info->flash_id += FLASH_AM160B;
288 info->sector_count = 35;
289 info->size = 0x00200000;
292 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV320T:
293 info->flash_id += FLASH_AM320T;
294 info->sector_count = 71;
295 info->size = 0x00400000; break; /* => 4 MB */
297 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV320B:
298 info->flash_id += FLASH_AM320B;
299 info->sector_count = 71;
300 info->size = 0x00400000; break; /* => 4 MB */
302 case (CFG_FLASH_WORD_SIZE)AMD_ID_DL322T:
303 info->flash_id += FLASH_AMDL322T;
304 info->sector_count = 71;
305 info->size = 0x00400000; break; /* => 4 MB */
307 case (CFG_FLASH_WORD_SIZE)AMD_ID_DL322B:
308 info->flash_id += FLASH_AMDL322B;
309 info->sector_count = 71;
310 info->size = 0x00400000; break; /* => 4 MB */
312 case (CFG_FLASH_WORD_SIZE)AMD_ID_DL323T:
313 info->flash_id += FLASH_AMDL323T;
314 info->sector_count = 71;
315 info->size = 0x00400000; break; /* => 4 MB */
317 case (CFG_FLASH_WORD_SIZE)AMD_ID_DL323B:
318 info->flash_id += FLASH_AMDL323B;
319 info->sector_count = 71;
320 info->size = 0x00400000; break; /* => 4 MB */
322 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV640U:
323 info->flash_id += FLASH_AM640U;
324 info->sector_count = 128;
325 info->size = 0x00800000; break; /* => 8 MB */
327 #if !(defined(CONFIG_ADCIOP) || defined(CONFIG_DASA_SIM))
328 case (CFG_FLASH_WORD_SIZE)SST_ID_xF800A:
329 info->flash_id += FLASH_SST800A;
330 info->sector_count = 16;
331 info->size = 0x00100000;
334 case (CFG_FLASH_WORD_SIZE)SST_ID_xF160A:
335 case (CFG_FLASH_WORD_SIZE)SST_ID_xF1601:
336 case (CFG_FLASH_WORD_SIZE)SST_ID_xF1602:
337 info->flash_id += FLASH_SST160A;
338 info->sector_count = 32;
339 info->size = 0x00200000;
342 case (CFG_FLASH_WORD_SIZE)SST_ID_xF3201:
343 case (CFG_FLASH_WORD_SIZE)SST_ID_xF3202:
344 info->flash_id += FLASH_SST320;
345 info->sector_count = 64;
346 info->size = 0x00400000;
349 case (CFG_FLASH_WORD_SIZE)SST_ID_xF6401:
350 case (CFG_FLASH_WORD_SIZE)SST_ID_xF6402:
351 info->flash_id += FLASH_SST640;
352 info->sector_count = 128;
353 info->size = 0x00800000;
358 info->flash_id = FLASH_UNKNOWN;
359 return (0); /* => no or unknown flash */
363 /* set up sector start address table */
364 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
365 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM640U)) {
366 for (i = 0; i < info->sector_count; i++)
367 info->start[i] = base + (i * 0x00010000);
368 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322B) ||
369 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323B) ||
370 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
371 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324B)) {
372 /* set sector offsets for bottom boot block type */
373 for (i=0; i<8; ++i) { /* 8 x 8k boot sectors */
374 info->start[i] = base;
377 while (i < info->sector_count) { /* 64k regular sectors */
378 info->start[i] = base;
382 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322T) ||
383 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323T) ||
384 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
385 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324T)) {
386 /* set sector offsets for top boot block type */
388 i = info->sector_count;
389 for (n=0; n<8; ++n) { /* 8 x 8k boot sectors */
392 info->start[i] = base;
394 while (i > 0) { /* 64k regular sectors */
397 info->start[i] = base;
400 if (info->flash_id & FLASH_BTYPE) {
401 /* set sector offsets for bottom boot block type */
402 info->start[0] = base + 0x00000000;
403 info->start[1] = base + 0x00004000;
404 info->start[2] = base + 0x00006000;
405 info->start[3] = base + 0x00008000;
406 for (i = 4; i < info->sector_count; i++) {
407 info->start[i] = base + (i * 0x00010000) - 0x00030000;
410 /* set sector offsets for top boot block type */
411 i = info->sector_count - 1;
412 info->start[i--] = base + info->size - 0x00004000;
413 info->start[i--] = base + info->size - 0x00006000;
414 info->start[i--] = base + info->size - 0x00008000;
415 for (; i >= 0; i--) {
416 info->start[i] = base + i * 0x00010000;
421 /* check for protected sectors */
422 for (i = 0; i < info->sector_count; i++) {
423 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
424 /* D0 = 1 if protected */
425 addr2 = (volatile CFG_FLASH_WORD_SIZE *)(info->start[i]);
426 if ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_AMD)
427 info->protect[i] = 0;
429 info->protect[i] = addr2[CFG_FLASH_READ2] & 1;
433 * Prevent writes to uninitialized FLASH.
435 if (info->flash_id != FLASH_UNKNOWN) {
436 addr2 = (CFG_FLASH_WORD_SIZE *)info->start[0];
437 *addr2 = (CFG_FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
444 /*-----------------------------------------------------------------------
447 int flash_erase (flash_info_t *info, int s_first, int s_last)
449 volatile CFG_FLASH_WORD_SIZE *addr = (CFG_FLASH_WORD_SIZE *)(info->start[0]);
450 volatile CFG_FLASH_WORD_SIZE *addr2;
451 int flag, prot, sect, l_sect;
452 ulong start, now, last;
455 if ((s_first < 0) || (s_first > s_last)) {
456 if (info->flash_id == FLASH_UNKNOWN) {
457 printf ("- missing\n");
459 printf ("- no sectors to erase\n");
464 if (info->flash_id == FLASH_UNKNOWN) {
465 printf ("Can't erase unknown flash type - aborted\n");
470 for (sect=s_first; sect<=s_last; ++sect) {
471 if (info->protect[sect]) {
477 printf ("- Warning: %d protected sectors will not be erased!\n",
485 /* Disable interrupts which might cause a timeout here */
486 flag = disable_interrupts();
488 /* Start erase on unprotected sectors */
489 for (sect = s_first; sect<=s_last; sect++) {
490 if (info->protect[sect] == 0) { /* not protected */
491 addr2 = (CFG_FLASH_WORD_SIZE *)(info->start[sect]);
492 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
493 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
494 addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
495 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00800080;
496 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
497 addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
498 addr2[0] = (CFG_FLASH_WORD_SIZE)0x00500050; /* block erase */
500 udelay(1000); /* wait 1 ms */
502 if (sect == s_first) {
503 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
504 addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
505 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00800080;
506 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
507 addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
509 addr2[0] = (CFG_FLASH_WORD_SIZE)0x00300030; /* sector erase */
515 /* re-enable interrupts if necessary */
519 /* wait at least 80us - let's wait 1 ms */
523 * We wait for the last triggered sector
528 start = get_timer (0);
530 addr = (CFG_FLASH_WORD_SIZE *)(info->start[l_sect]);
531 while ((addr[0] & (CFG_FLASH_WORD_SIZE)0x00800080) != (CFG_FLASH_WORD_SIZE)0x00800080) {
532 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
533 printf ("Timeout\n");
536 /* show that we're waiting */
537 if ((now - last) > 1000) { /* every second */
544 /* reset to read mode */
545 addr = (CFG_FLASH_WORD_SIZE *)info->start[0];
546 addr[0] = (CFG_FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
552 /*-----------------------------------------------------------------------
553 * Copy memory to flash, returns:
556 * 2 - Flash not erased
559 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
564 wp = (addr & ~3); /* get lower word aligned address */
567 * handle unaligned start bytes
569 if ((l = addr - wp) != 0) {
571 for (i=0, cp=wp; i<l; ++i, ++cp) {
572 data = (data << 8) | (*(uchar *)cp);
574 for (; i<4 && cnt>0; ++i) {
575 data = (data << 8) | *src++;
579 for (; cnt==0 && i<4; ++i, ++cp) {
580 data = (data << 8) | (*(uchar *)cp);
583 if ((rc = write_word(info, wp, data)) != 0) {
590 * handle word aligned part
594 for (i=0; i<4; ++i) {
595 data = (data << 8) | *src++;
597 if ((rc = write_word(info, wp, data)) != 0) {
609 * handle unaligned tail bytes
612 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
613 data = (data << 8) | *src++;
616 for (; i<4; ++i, ++cp) {
617 data = (data << 8) | (*(uchar *)cp);
620 return (write_word(info, wp, data));
623 /*-----------------------------------------------------------------------
624 * Write a word to Flash, returns:
627 * 2 - Flash not erased
629 static int write_word (flash_info_t *info, ulong dest, ulong data)
631 volatile CFG_FLASH_WORD_SIZE *addr2 = (CFG_FLASH_WORD_SIZE *)(info->start[0]);
632 volatile CFG_FLASH_WORD_SIZE *dest2 = (CFG_FLASH_WORD_SIZE *)dest;
633 volatile CFG_FLASH_WORD_SIZE *data2 = (CFG_FLASH_WORD_SIZE *)&data;
638 /* Check if Flash is (sufficiently) erased */
639 if ((*((vu_long *)dest) & data) != data) {
643 /* Disable interrupts which might cause a timeout here */
644 flag = disable_interrupts();
646 for (i=0; i<4/sizeof(CFG_FLASH_WORD_SIZE); i++)
648 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
649 addr2[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
650 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00A000A0;
654 /* re-enable interrupts if necessary */
658 /* data polling for D7 */
659 start = get_timer (0);
660 while ((dest2[i] & (CFG_FLASH_WORD_SIZE)0x00800080) !=
661 (data2[i] & (CFG_FLASH_WORD_SIZE)0x00800080)) {
662 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
671 /*-----------------------------------------------------------------------