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,
28 #include <asm/processor.h>
30 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
32 /*-----------------------------------------------------------------------
35 static int write_word (flash_info_t *info, ulong dest, ulong data);
37 /*-----------------------------------------------------------------------
39 static void flash_get_offsets (ulong base, flash_info_t *info)
44 /* set up sector start address table */
45 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
46 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM640U)) {
47 for (i = 0; i < info->sector_count; i++)
48 info->start[i] = base + (i * 0x00010000);
49 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322B) ||
50 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323B) ||
51 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
52 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324B)) {
53 /* set sector offsets for bottom boot block type */
54 for (i=0; i<8; ++i) { /* 8 x 8k boot sectors */
55 info->start[i] = base;
58 while (i < info->sector_count) { /* 64k regular sectors */
59 info->start[i] = base;
63 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322T) ||
64 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323T) ||
65 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
66 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324T)) {
67 /* set sector offsets for top boot block type */
69 i = info->sector_count;
70 for (n=0; n<8; ++n) { /* 8 x 8k boot sectors */
73 info->start[i] = base;
75 while (i > 0) { /* 64k regular sectors */
78 info->start[i] = base;
81 if (info->flash_id & FLASH_BTYPE) {
82 /* set sector offsets for bottom boot block type */
83 info->start[0] = base + 0x00000000;
84 info->start[1] = base + 0x00004000;
85 info->start[2] = base + 0x00006000;
86 info->start[3] = base + 0x00008000;
87 for (i = 4; i < info->sector_count; i++) {
88 info->start[i] = base + (i * 0x00010000) - 0x00030000;
91 /* set sector offsets for top boot block type */
92 i = info->sector_count - 1;
93 info->start[i--] = base + info->size - 0x00004000;
94 info->start[i--] = base + info->size - 0x00006000;
95 info->start[i--] = base + info->size - 0x00008000;
97 info->start[i] = base + i * 0x00010000;
103 /*-----------------------------------------------------------------------
105 void flash_print_info (flash_info_t *info)
111 volatile unsigned long *flash;
113 if (info->flash_id == FLASH_UNKNOWN) {
114 printf ("missing or unknown FLASH type\n");
118 switch (info->flash_id & FLASH_VENDMASK) {
119 case FLASH_MAN_AMD: printf ("AMD "); break;
120 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
121 case FLASH_MAN_SST: printf ("SST "); break;
122 case FLASH_MAN_EXCEL: printf ("Excel Semiconductor "); break;
123 default: printf ("Unknown Vendor "); break;
126 switch (info->flash_id & FLASH_TYPEMASK) {
127 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
129 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
131 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
133 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
135 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
137 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
139 case FLASH_AM320T: printf ("AM29LV320T (32 M, top sector)\n");
141 case FLASH_AM320B: printf ("AM29LV320B (32 M, bottom sector)\n");
143 case FLASH_AMDL322T: printf ("AM29DL322T (32 M, top sector)\n");
145 case FLASH_AMDL322B: printf ("AM29DL322B (32 M, bottom sector)\n");
147 case FLASH_AMDL323T: printf ("AM29DL323T (32 M, top sector)\n");
149 case FLASH_AMDL323B: printf ("AM29DL323B (32 M, bottom sector)\n");
151 case FLASH_AM640U: printf ("AM29LV640D (64 M, uniform sector)\n");
153 case FLASH_SST800A: printf ("SST39LF/VF800 (8 Mbit, uniform sector size)\n");
155 case FLASH_SST160A: printf ("SST39LF/VF160 (16 Mbit, uniform sector size)\n");
157 case FLASH_SST320: printf ("SST39LF/VF320 (32 Mbit, uniform sector size)\n");
159 case FLASH_SST640: printf ("SST39LF/VF640 (64 Mbit, uniform sector size)\n");
161 default: printf ("Unknown Chip Type\n");
165 printf (" Size: %ld MB in %d Sectors\n",
166 info->size >> 20, info->sector_count);
168 printf (" Sector Start Addresses:");
169 for (i=0; i<info->sector_count; ++i) {
170 #ifdef CONFIG_SYS_FLASH_EMPTY_INFO
172 * Check if whole sector is erased
174 if (i != (info->sector_count-1))
175 size = info->start[i+1] - info->start[i];
177 size = info->start[0] + info->size - info->start[i];
179 flash = (volatile unsigned long *)info->start[i];
180 size = size >> 2; /* divide by 4 for longword access */
181 for (k=0; k<size; k++)
183 if (*flash++ != 0xffffffff)
192 /* print empty and read-only info */
193 printf (" %08lX%s%s",
196 info->protect[i] ? "RO " : " ");
202 info->protect[i] ? " (RO)" : " ");
210 /*-----------------------------------------------------------------------
214 /*-----------------------------------------------------------------------
218 * The following code cannot be run from FLASH!
220 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
224 CONFIG_SYS_FLASH_WORD_SIZE value;
225 ulong base = (ulong)addr;
226 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)addr;
228 /* Write auto select command: read Manufacturer ID */
229 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
230 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
231 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00900090;
233 value = addr2[CONFIG_SYS_FLASH_READ0];
236 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_MANUFACT:
237 info->flash_id = FLASH_MAN_AMD;
239 case (CONFIG_SYS_FLASH_WORD_SIZE)FUJ_MANUFACT:
240 info->flash_id = FLASH_MAN_FUJ;
242 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_MANUFACT:
243 info->flash_id = FLASH_MAN_SST;
245 case (CONFIG_SYS_FLASH_WORD_SIZE)EXCEL_MANUFACT:
246 info->flash_id = FLASH_MAN_EXCEL;
249 info->flash_id = FLASH_UNKNOWN;
250 info->sector_count = 0;
252 return (0); /* no or unknown flash */
255 value = addr2[CONFIG_SYS_FLASH_READ1]; /* device ID */
258 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV400T:
259 info->flash_id += FLASH_AM400T;
260 info->sector_count = 11;
261 info->size = 0x00080000;
262 break; /* => 0.5 MB */
264 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV400B:
265 info->flash_id += FLASH_AM400B;
266 info->sector_count = 11;
267 info->size = 0x00080000;
268 break; /* => 0.5 MB */
270 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV800T:
271 info->flash_id += FLASH_AM800T;
272 info->sector_count = 19;
273 info->size = 0x00100000;
276 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV800B:
277 info->flash_id += FLASH_AM800B;
278 info->sector_count = 19;
279 info->size = 0x00100000;
282 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV160T:
283 info->flash_id += FLASH_AM160T;
284 info->sector_count = 35;
285 info->size = 0x00200000;
288 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV160B:
289 info->flash_id += FLASH_AM160B;
290 info->sector_count = 35;
291 info->size = 0x00200000;
294 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV320T:
295 info->flash_id += FLASH_AM320T;
296 info->sector_count = 71;
297 info->size = 0x00400000; break; /* => 4 MB */
299 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV320B:
300 info->flash_id += FLASH_AM320B;
301 info->sector_count = 71;
302 info->size = 0x00400000; break; /* => 4 MB */
304 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL322T:
305 info->flash_id += FLASH_AMDL322T;
306 info->sector_count = 71;
307 info->size = 0x00400000; break; /* => 4 MB */
309 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL322B:
310 info->flash_id += FLASH_AMDL322B;
311 info->sector_count = 71;
312 info->size = 0x00400000; break; /* => 4 MB */
314 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL323T:
315 info->flash_id += FLASH_AMDL323T;
316 info->sector_count = 71;
317 info->size = 0x00400000; break; /* => 4 MB */
319 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL323B:
320 info->flash_id += FLASH_AMDL323B;
321 info->sector_count = 71;
322 info->size = 0x00400000; break; /* => 4 MB */
324 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV640U:
325 info->flash_id += FLASH_AM640U;
326 info->sector_count = 128;
327 info->size = 0x00800000; break; /* => 8 MB */
329 #if !(defined(CONFIG_ADCIOP) || defined(CONFIG_DASA_SIM))
330 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF800A:
331 info->flash_id += FLASH_SST800A;
332 info->sector_count = 16;
333 info->size = 0x00100000;
336 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF160A:
337 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF1601:
338 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF1602:
339 info->flash_id += FLASH_SST160A;
340 info->sector_count = 32;
341 info->size = 0x00200000;
344 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF3201:
345 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF3202:
346 info->flash_id += FLASH_SST320;
347 info->sector_count = 64;
348 info->size = 0x00400000;
351 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF6401:
352 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF6402:
353 info->flash_id += FLASH_SST640;
354 info->sector_count = 128;
355 info->size = 0x00800000;
360 info->flash_id = FLASH_UNKNOWN;
361 return (0); /* => no or unknown flash */
365 /* set up sector start address table */
366 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
367 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM640U)) {
368 for (i = 0; i < info->sector_count; i++)
369 info->start[i] = base + (i * 0x00010000);
370 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322B) ||
371 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323B) ||
372 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
373 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324B)) {
374 /* set sector offsets for bottom boot block type */
375 for (i=0; i<8; ++i) { /* 8 x 8k boot sectors */
376 info->start[i] = base;
379 while (i < info->sector_count) { /* 64k regular sectors */
380 info->start[i] = base;
384 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322T) ||
385 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323T) ||
386 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
387 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324T)) {
388 /* set sector offsets for top boot block type */
390 i = info->sector_count;
391 for (n=0; n<8; ++n) { /* 8 x 8k boot sectors */
394 info->start[i] = base;
396 while (i > 0) { /* 64k regular sectors */
399 info->start[i] = base;
402 if (info->flash_id & FLASH_BTYPE) {
403 /* set sector offsets for bottom boot block type */
404 info->start[0] = base + 0x00000000;
405 info->start[1] = base + 0x00004000;
406 info->start[2] = base + 0x00006000;
407 info->start[3] = base + 0x00008000;
408 for (i = 4; i < info->sector_count; i++) {
409 info->start[i] = base + (i * 0x00010000) - 0x00030000;
412 /* set sector offsets for top boot block type */
413 i = info->sector_count - 1;
414 info->start[i--] = base + info->size - 0x00004000;
415 info->start[i--] = base + info->size - 0x00006000;
416 info->start[i--] = base + info->size - 0x00008000;
417 for (; i >= 0; i--) {
418 info->start[i] = base + i * 0x00010000;
423 /* check for protected sectors */
424 for (i = 0; i < info->sector_count; i++) {
425 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
426 /* D0 = 1 if protected */
427 addr2 = (volatile CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[i]);
428 if ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_AMD)
429 info->protect[i] = 0;
431 info->protect[i] = addr2[CONFIG_SYS_FLASH_READ2] & 1;
435 * Prevent writes to uninitialized FLASH.
437 if (info->flash_id != FLASH_UNKNOWN) {
438 addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)info->start[0];
439 *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
446 /*-----------------------------------------------------------------------
449 int flash_erase (flash_info_t *info, int s_first, int s_last)
451 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[0]);
452 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2;
453 int flag, prot, sect, l_sect;
454 ulong start, now, last;
457 if ((s_first < 0) || (s_first > s_last)) {
458 if (info->flash_id == FLASH_UNKNOWN) {
459 printf ("- missing\n");
461 printf ("- no sectors to erase\n");
466 if (info->flash_id == FLASH_UNKNOWN) {
467 printf ("Can't erase unknown flash type - aborted\n");
472 for (sect=s_first; sect<=s_last; ++sect) {
473 if (info->protect[sect]) {
479 printf ("- Warning: %d protected sectors will not be erased!\n",
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 = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[sect]);
494 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
495 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
496 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
497 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080;
498 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
499 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
500 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00500050; /* block erase */
502 udelay(1000); /* wait 1 ms */
504 if (sect == s_first) {
505 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
506 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
507 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080;
508 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
509 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
511 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00300030; /* sector erase */
517 /* re-enable interrupts if necessary */
521 /* wait at least 80us - let's wait 1 ms */
525 * We wait for the last triggered sector
530 start = get_timer (0);
532 addr = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[l_sect]);
533 while ((addr[0] & (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080) != (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080) {
534 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
535 printf ("Timeout\n");
538 /* show that we're waiting */
539 if ((now - last) > 1000) { /* every second */
546 /* reset to read mode */
547 addr = (CONFIG_SYS_FLASH_WORD_SIZE *)info->start[0];
548 addr[0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
554 /*-----------------------------------------------------------------------
555 * Copy memory to flash, returns:
558 * 2 - Flash not erased
561 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
566 wp = (addr & ~3); /* get lower word aligned address */
569 * handle unaligned start bytes
571 if ((l = addr - wp) != 0) {
573 for (i=0, cp=wp; i<l; ++i, ++cp) {
574 data = (data << 8) | (*(uchar *)cp);
576 for (; i<4 && cnt>0; ++i) {
577 data = (data << 8) | *src++;
581 for (; cnt==0 && i<4; ++i, ++cp) {
582 data = (data << 8) | (*(uchar *)cp);
585 if ((rc = write_word(info, wp, data)) != 0) {
592 * handle word aligned part
596 for (i=0; i<4; ++i) {
597 data = (data << 8) | *src++;
599 if ((rc = write_word(info, wp, data)) != 0) {
611 * handle unaligned tail bytes
614 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
615 data = (data << 8) | *src++;
618 for (; i<4; ++i, ++cp) {
619 data = (data << 8) | (*(uchar *)cp);
622 return (write_word(info, wp, data));
625 /*-----------------------------------------------------------------------
626 * Write a word to Flash, returns:
629 * 2 - Flash not erased
631 static int write_word (flash_info_t *info, ulong dest, ulong data)
633 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[0]);
634 volatile CONFIG_SYS_FLASH_WORD_SIZE *dest2 = (CONFIG_SYS_FLASH_WORD_SIZE *)dest;
635 volatile CONFIG_SYS_FLASH_WORD_SIZE *data2 = (CONFIG_SYS_FLASH_WORD_SIZE *)&data;
640 /* Check if Flash is (sufficiently) erased */
641 if ((*((vu_long *)dest) & data) != data) {
645 /* Disable interrupts which might cause a timeout here */
646 flag = disable_interrupts();
648 for (i=0; i<4/sizeof(CONFIG_SYS_FLASH_WORD_SIZE); i++)
650 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
651 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
652 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00A000A0;
656 /* re-enable interrupts if necessary */
660 /* data polling for D7 */
661 start = get_timer (0);
662 while ((dest2[i] & (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080) !=
663 (data2[i] & (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080)) {
664 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
673 /*-----------------------------------------------------------------------