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,
25 #include <asm/processor.h>
27 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
29 /*-----------------------------------------------------------------------
32 static int write_word (flash_info_t *info, ulong dest, ulong data);
34 /*-----------------------------------------------------------------------
36 static void flash_get_offsets (ulong base, flash_info_t *info)
41 /* set up sector start address table */
42 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
43 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM640U)) {
44 for (i = 0; i < info->sector_count; i++)
45 info->start[i] = base + (i * 0x00010000);
46 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322B) ||
47 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323B) ||
48 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
49 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324B)) {
50 /* set sector offsets for bottom boot block type */
51 for (i=0; i<8; ++i) { /* 8 x 8k boot sectors */
52 info->start[i] = base;
55 while (i < info->sector_count) { /* 64k regular sectors */
56 info->start[i] = base;
60 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322T) ||
61 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323T) ||
62 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
63 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324T)) {
64 /* set sector offsets for top boot block type */
66 i = info->sector_count;
67 for (n=0; n<8; ++n) { /* 8 x 8k boot sectors */
70 info->start[i] = base;
72 while (i > 0) { /* 64k regular sectors */
75 info->start[i] = base;
78 if (info->flash_id & FLASH_BTYPE) {
79 /* set sector offsets for bottom boot block type */
80 info->start[0] = base + 0x00000000;
81 info->start[1] = base + 0x00004000;
82 info->start[2] = base + 0x00006000;
83 info->start[3] = base + 0x00008000;
84 for (i = 4; i < info->sector_count; i++) {
85 info->start[i] = base + (i * 0x00010000) - 0x00030000;
88 /* set sector offsets for top boot block type */
89 i = info->sector_count - 1;
90 info->start[i--] = base + info->size - 0x00004000;
91 info->start[i--] = base + info->size - 0x00006000;
92 info->start[i--] = base + info->size - 0x00008000;
94 info->start[i] = base + i * 0x00010000;
100 /*-----------------------------------------------------------------------
102 void flash_print_info (flash_info_t *info)
108 volatile unsigned long *flash;
110 if (info->flash_id == FLASH_UNKNOWN) {
111 printf ("missing or unknown FLASH type\n");
115 switch (info->flash_id & FLASH_VENDMASK) {
116 case FLASH_MAN_AMD: printf ("AMD "); break;
117 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
118 case FLASH_MAN_SST: printf ("SST "); break;
119 case FLASH_MAN_STM: printf ("ST "); break;
120 default: printf ("Unknown Vendor "); break;
123 switch (info->flash_id & FLASH_TYPEMASK) {
124 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
126 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
128 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
130 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
132 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
134 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
136 case FLASH_AM320T: printf ("AM29LV320T (32 M, top sector)\n");
138 case FLASH_AM320B: printf ("AM29LV320B (32 M, bottom sector)\n");
140 case FLASH_AMDL322T: printf ("AM29DL322T (32 M, top sector)\n");
142 case FLASH_AMDL322B: printf ("AM29DL322B (32 M, bottom sector)\n");
144 case FLASH_AMDL323T: printf ("AM29DL323T (32 M, top sector)\n");
146 case FLASH_AMDL323B: printf ("AM29DL323B (32 M, bottom sector)\n");
148 case FLASH_AM640U: printf ("AM29LV640D (64 M, uniform sector)\n");
150 case FLASH_SST800A: printf ("SST39LF/VF800 (8 Mbit, uniform sector size)\n");
152 case FLASH_SST160A: printf ("SST39LF/VF160 (16 Mbit, uniform sector size)\n");
154 case FLASH_STMW320DT: printf ("M29W320DT (32 M, top sector)\n");
156 default: printf ("Unknown Chip Type\n");
160 printf (" Size: %ld MB in %d Sectors\n",
161 info->size >> 20, info->sector_count);
163 printf (" Sector Start Addresses:");
164 for (i=0; i<info->sector_count; ++i) {
165 #ifdef CONFIG_SYS_FLASH_EMPTY_INFO
167 * Check if whole sector is erased
169 if (i != (info->sector_count-1))
170 size = info->start[i+1] - info->start[i];
172 size = info->start[0] + info->size - info->start[i];
174 flash = (volatile unsigned long *)info->start[i];
175 size = size >> 2; /* divide by 4 for longword access */
176 for (k=0; k<size; k++)
178 if (*flash++ != 0xffffffff)
187 /* print empty and read-only info */
188 printf (" %08lX%s%s",
191 info->protect[i] ? "RO " : " ");
197 info->protect[i] ? " (RO)" : " ");
205 /*-----------------------------------------------------------------------
209 /*-----------------------------------------------------------------------
213 * The following code cannot be run from FLASH!
215 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
219 CONFIG_SYS_FLASH_WORD_SIZE value;
220 ulong base = (ulong)addr;
221 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)addr;
223 debug("[%s, %d] Entering ...\n", __FUNCTION__, __LINE__);
225 /* Write auto select command: read Manufacturer ID */
226 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
227 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
228 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00900090;
230 value = addr2[CONFIG_SYS_FLASH_READ0];
233 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_MANUFACT:
234 info->flash_id = FLASH_MAN_AMD;
236 case (CONFIG_SYS_FLASH_WORD_SIZE)FUJ_MANUFACT:
237 info->flash_id = FLASH_MAN_FUJ;
239 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_MANUFACT:
240 info->flash_id = FLASH_MAN_SST;
242 case (CONFIG_SYS_FLASH_WORD_SIZE)STM_MANUFACT:
243 info->flash_id = FLASH_MAN_STM;
246 info->flash_id = FLASH_UNKNOWN;
247 info->sector_count = 0;
249 return (0); /* no or unknown flash */
252 value = addr2[CONFIG_SYS_FLASH_READ1]; /* device ID */
255 case (CONFIG_SYS_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 (CONFIG_SYS_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 (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV800T:
268 info->flash_id += FLASH_AM800T;
269 info->sector_count = 19;
270 info->size = 0x00100000;
273 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV800B:
274 info->flash_id += FLASH_AM800B;
275 info->sector_count = 19;
276 info->size = 0x00100000;
279 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV160T:
280 info->flash_id += FLASH_AM160T;
281 info->sector_count = 35;
282 info->size = 0x00200000;
285 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV160B:
286 info->flash_id += FLASH_AM160B;
287 info->sector_count = 35;
288 info->size = 0x00200000;
291 case (CONFIG_SYS_FLASH_WORD_SIZE)STM_ID_29W320DT:
292 info->flash_id += FLASH_STMW320DT;
293 info->sector_count = 67;
294 info->size = 0x00400000; break; /* => 4 MB */
296 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV320T:
297 info->flash_id += FLASH_AM320T;
298 info->sector_count = 71;
299 info->size = 0x00400000; break; /* => 4 MB */
301 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV320B:
302 info->flash_id += FLASH_AM320B;
303 info->sector_count = 71;
304 info->size = 0x00400000; break; /* => 4 MB */
306 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL322T:
307 info->flash_id += FLASH_AMDL322T;
308 info->sector_count = 71;
309 info->size = 0x00400000; break; /* => 4 MB */
311 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL322B:
312 info->flash_id += FLASH_AMDL322B;
313 info->sector_count = 71;
314 info->size = 0x00400000; break; /* => 4 MB */
316 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL323T:
317 info->flash_id += FLASH_AMDL323T;
318 info->sector_count = 71;
319 info->size = 0x00400000; break; /* => 4 MB */
321 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL323B:
322 info->flash_id += FLASH_AMDL323B;
323 info->sector_count = 71;
324 info->size = 0x00400000; break; /* => 4 MB */
326 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV640U:
327 info->flash_id += FLASH_AM640U;
328 info->sector_count = 128;
329 info->size = 0x00800000; break; /* => 8 MB */
331 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF800A:
332 info->flash_id += FLASH_SST800A;
333 info->sector_count = 16;
334 info->size = 0x00100000;
337 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF160A:
338 info->flash_id += FLASH_SST160A;
339 info->sector_count = 32;
340 info->size = 0x00200000;
344 info->flash_id = FLASH_UNKNOWN;
345 return (0); /* => no or unknown flash */
349 /* set up sector start address table */
350 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
351 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM640U)) {
352 for (i = 0; i < info->sector_count; i++)
353 info->start[i] = base + (i * 0x00010000);
354 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322B) ||
355 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323B) ||
356 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
357 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324B)) {
358 /* set sector offsets for bottom boot block type */
359 for (i=0; i<8; ++i) { /* 8 x 8k boot sectors */
360 info->start[i] = base;
363 while (i < info->sector_count) { /* 64k regular sectors */
364 info->start[i] = base;
368 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322T) ||
369 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323T) ||
370 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
371 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324T)) {
372 /* set sector offsets for top boot block type */
374 i = info->sector_count;
375 for (n=0; n<8; ++n) { /* 8 x 8k boot sectors */
378 info->start[i] = base;
380 while (i > 0) { /* 64k regular sectors */
383 info->start[i] = base;
385 } else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_STMW320DT) {
386 /* set sector offsets for top boot block type */
388 i = info->sector_count;
389 /* 1 x 16k boot sector */
392 info->start[i] = base;
393 /* 2 x 8k boot sectors */
394 for (n=0; n<2; ++n) {
397 info->start[i] = base;
399 /* 1 x 32k boot sector */
402 info->start[i] = base;
404 while (i > 0) { /* 64k regular sectors */
407 info->start[i] = base;
410 if (info->flash_id & FLASH_BTYPE) {
411 /* set sector offsets for bottom boot block type */
412 info->start[0] = base + 0x00000000;
413 info->start[1] = base + 0x00004000;
414 info->start[2] = base + 0x00006000;
415 info->start[3] = base + 0x00008000;
416 for (i = 4; i < info->sector_count; i++) {
417 info->start[i] = base + (i * 0x00010000) - 0x00030000;
420 /* set sector offsets for top boot block type */
421 i = info->sector_count - 1;
422 info->start[i--] = base + info->size - 0x00004000;
423 info->start[i--] = base + info->size - 0x00006000;
424 info->start[i--] = base + info->size - 0x00008000;
425 for (; i >= 0; i--) {
426 info->start[i] = base + i * 0x00010000;
431 /* check for protected sectors */
432 for (i = 0; i < info->sector_count; i++) {
433 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
434 /* D0 = 1 if protected */
435 addr2 = (volatile CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[i]);
436 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
437 info->protect[i] = 0;
439 info->protect[i] = addr2[CONFIG_SYS_FLASH_READ2] & 1;
443 * Prevent writes to uninitialized FLASH.
445 if (info->flash_id != FLASH_UNKNOWN) {
446 addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)info->start[0];
447 *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
454 /*-----------------------------------------------------------------------
457 int flash_erase (flash_info_t *info, int s_first, int s_last)
459 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[0]);
460 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2;
461 int flag, prot, sect, l_sect;
462 ulong start, now, last;
465 if ((s_first < 0) || (s_first > s_last)) {
466 if (info->flash_id == FLASH_UNKNOWN) {
467 printf ("- missing\n");
469 printf ("- no sectors to erase\n");
474 if (info->flash_id == FLASH_UNKNOWN) {
475 printf ("Can't erase unknown flash type - aborted\n");
480 for (sect=s_first; sect<=s_last; ++sect) {
481 if (info->protect[sect]) {
487 printf ("- Warning: %d protected sectors will not be erased!\n",
495 /* Disable interrupts which might cause a timeout here */
496 flag = disable_interrupts();
498 /* Start erase on unprotected sectors */
499 for (sect = s_first; sect<=s_last; sect++) {
500 if (info->protect[sect] == 0) { /* not protected */
501 addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[sect]);
502 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
503 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
504 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
505 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080;
506 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
507 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
508 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00500050; /* block erase */
510 udelay(1000); /* wait 1 ms */
512 if (sect == s_first) {
513 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
514 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
515 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080;
516 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
517 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
519 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00300030; /* sector erase */
525 /* re-enable interrupts if necessary */
529 /* wait at least 80us - let's wait 1 ms */
533 * We wait for the last triggered sector
538 start = get_timer (0);
540 addr = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[l_sect]);
541 while ((addr[0] & (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080) != (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080) {
542 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
543 printf ("Timeout\n");
546 /* show that we're waiting */
547 if ((now - last) > 1000) { /* every second */
554 /* reset to read mode */
555 addr = (CONFIG_SYS_FLASH_WORD_SIZE *)info->start[0];
556 addr[0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
562 /*-----------------------------------------------------------------------
563 * Copy memory to flash, returns:
566 * 2 - Flash not erased
569 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
574 wp = (addr & ~3); /* get lower word aligned address */
577 * handle unaligned start bytes
579 if ((l = addr - wp) != 0) {
581 for (i=0, cp=wp; i<l; ++i, ++cp) {
583 data = data | ((*(uchar *)cp)<<(8*i));
585 data = (data << 8) | (*(uchar *)cp);
588 for (; i<4 && cnt>0; ++i) {
590 data = data | ((*src++)<<(8*i));
592 data = (data << 8) | *src++;
597 for (; cnt==0 && i<4; ++i, ++cp) {
599 data = data | ((*(uchar *)cp)<<(8*i));
601 data = (data << 8) | (*(uchar *)cp);
605 if ((rc = write_word(info, wp, data)) != 0) {
612 * handle word aligned part
617 data = (*(ulong*)src);
620 for (i=0; i<4; ++i) {
621 data = (data << 8) | *src++;
624 if ((rc = write_word(info, wp, data)) != 0) {
636 * handle unaligned tail bytes
639 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
641 data = data | ((*src++)<<(8*i));
643 data = (data << 8) | *src++;
647 for (; i<4; ++i, ++cp) {
649 data = data | ((*(uchar *)cp)<<(8*i));
651 data = (data << 8) | (*(uchar *)cp);
655 return (write_word(info, wp, data));
658 /*-----------------------------------------------------------------------
659 * Write a word to Flash, returns:
662 * 2 - Flash not erased
664 static int write_word (flash_info_t *info, ulong dest, ulong data)
666 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[0]);
667 volatile CONFIG_SYS_FLASH_WORD_SIZE *dest2 = (CONFIG_SYS_FLASH_WORD_SIZE *)dest;
668 volatile CONFIG_SYS_FLASH_WORD_SIZE *data2 = (CONFIG_SYS_FLASH_WORD_SIZE *)&data;
673 /* Check if Flash is (sufficiently) erased */
674 if ((*((volatile ulong *)dest) & data) != data) {
677 /* Disable interrupts which might cause a timeout here */
678 flag = disable_interrupts();
680 for (i=0; i<4/sizeof(CONFIG_SYS_FLASH_WORD_SIZE); i++)
682 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
683 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
684 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00A000A0;
688 /* re-enable interrupts if necessary */
692 /* data polling for D7 */
693 start = get_timer (0);
694 while ((dest2[i] & (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080) !=
695 (data2[i] & (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080)) {
696 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
705 /*-----------------------------------------------------------------------