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_STM: printf ("ST "); 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_STMW320DT: printf ("M29W320DT (32 M, top sector)\n");
157 default: printf ("Unknown Chip Type\n");
161 printf (" Size: %ld MB in %d Sectors\n",
162 info->size >> 20, info->sector_count);
164 printf (" Sector Start Addresses:");
165 for (i=0; i<info->sector_count; ++i) {
166 #ifdef CFG_FLASH_EMPTY_INFO
168 * Check if whole sector is erased
170 if (i != (info->sector_count-1))
171 size = info->start[i+1] - info->start[i];
173 size = info->start[0] + info->size - info->start[i];
175 flash = (volatile unsigned long *)info->start[i];
176 size = size >> 2; /* divide by 4 for longword access */
177 for (k=0; k<size; k++)
179 if (*flash++ != 0xffffffff)
188 /* print empty and read-only info */
189 printf (" %08lX%s%s",
192 info->protect[i] ? "RO " : " ");
198 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)
220 CFG_FLASH_WORD_SIZE value;
221 ulong base = (ulong)addr;
222 volatile CFG_FLASH_WORD_SIZE *addr2 = (CFG_FLASH_WORD_SIZE *)addr;
224 /* Write auto select command: read Manufacturer ID */
225 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
226 addr2[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
227 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00900090;
229 value = addr2[CFG_FLASH_READ0];
232 case (CFG_FLASH_WORD_SIZE)AMD_MANUFACT:
233 info->flash_id = FLASH_MAN_AMD;
235 case (CFG_FLASH_WORD_SIZE)FUJ_MANUFACT:
236 info->flash_id = FLASH_MAN_FUJ;
238 case (CFG_FLASH_WORD_SIZE)SST_MANUFACT:
239 info->flash_id = FLASH_MAN_SST;
241 case (CFG_FLASH_WORD_SIZE)STM_MANUFACT:
242 info->flash_id = FLASH_MAN_STM;
245 info->flash_id = FLASH_UNKNOWN;
246 info->sector_count = 0;
248 return (0); /* no or unknown flash */
251 value = addr2[CFG_FLASH_READ1]; /* device ID */
254 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV400T:
255 info->flash_id += FLASH_AM400T;
256 info->sector_count = 11;
257 info->size = 0x00080000;
258 break; /* => 0.5 MB */
260 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV400B:
261 info->flash_id += FLASH_AM400B;
262 info->sector_count = 11;
263 info->size = 0x00080000;
264 break; /* => 0.5 MB */
266 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV800T:
267 info->flash_id += FLASH_AM800T;
268 info->sector_count = 19;
269 info->size = 0x00100000;
272 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV800B:
273 info->flash_id += FLASH_AM800B;
274 info->sector_count = 19;
275 info->size = 0x00100000;
278 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV160T:
279 info->flash_id += FLASH_AM160T;
280 info->sector_count = 35;
281 info->size = 0x00200000;
284 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV160B:
285 info->flash_id += FLASH_AM160B;
286 info->sector_count = 35;
287 info->size = 0x00200000;
290 case (CFG_FLASH_WORD_SIZE)STM_ID_29W320DT:
291 info->flash_id += FLASH_STMW320DT;
292 info->sector_count = 67;
293 info->size = 0x00400000; break; /* => 4 MB */
295 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV320T:
296 info->flash_id += FLASH_AM320T;
297 info->sector_count = 71;
298 info->size = 0x00400000; break; /* => 4 MB */
300 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV320B:
301 info->flash_id += FLASH_AM320B;
302 info->sector_count = 71;
303 info->size = 0x00400000; break; /* => 4 MB */
305 case (CFG_FLASH_WORD_SIZE)AMD_ID_DL322T:
306 info->flash_id += FLASH_AMDL322T;
307 info->sector_count = 71;
308 info->size = 0x00400000; break; /* => 4 MB */
310 case (CFG_FLASH_WORD_SIZE)AMD_ID_DL322B:
311 info->flash_id += FLASH_AMDL322B;
312 info->sector_count = 71;
313 info->size = 0x00400000; break; /* => 4 MB */
315 case (CFG_FLASH_WORD_SIZE)AMD_ID_DL323T:
316 info->flash_id += FLASH_AMDL323T;
317 info->sector_count = 71;
318 info->size = 0x00400000; break; /* => 4 MB */
320 case (CFG_FLASH_WORD_SIZE)AMD_ID_DL323B:
321 info->flash_id += FLASH_AMDL323B;
322 info->sector_count = 71;
323 info->size = 0x00400000; break; /* => 4 MB */
325 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV640U:
326 info->flash_id += FLASH_AM640U;
327 info->sector_count = 128;
328 info->size = 0x00800000; break; /* => 8 MB */
330 case (CFG_FLASH_WORD_SIZE)SST_ID_xF800A:
331 info->flash_id += FLASH_SST800A;
332 info->sector_count = 16;
333 info->size = 0x00100000;
336 case (CFG_FLASH_WORD_SIZE)SST_ID_xF160A:
337 info->flash_id += FLASH_SST160A;
338 info->sector_count = 32;
339 info->size = 0x00200000;
343 info->flash_id = FLASH_UNKNOWN;
344 return (0); /* => no or unknown flash */
348 /* set up sector start address table */
349 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
350 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM640U)) {
351 for (i = 0; i < info->sector_count; i++)
352 info->start[i] = base + (i * 0x00010000);
353 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322B) ||
354 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323B) ||
355 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
356 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324B)) {
357 /* set sector offsets for bottom boot block type */
358 for (i=0; i<8; ++i) { /* 8 x 8k boot sectors */
359 info->start[i] = base;
362 while (i < info->sector_count) { /* 64k regular sectors */
363 info->start[i] = base;
367 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322T) ||
368 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323T) ||
369 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
370 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324T)) {
371 /* set sector offsets for top boot block type */
373 i = info->sector_count;
374 for (n=0; n<8; ++n) { /* 8 x 8k boot sectors */
377 info->start[i] = base;
379 while (i > 0) { /* 64k regular sectors */
382 info->start[i] = base;
384 } else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_STMW320DT) {
385 /* set sector offsets for top boot block type */
387 i = info->sector_count;
388 /* 1 x 16k boot sector */
391 info->start[i] = base;
392 /* 2 x 8k boot sectors */
393 for (n=0; n<2; ++n) {
396 info->start[i] = base;
398 /* 1 x 32k boot sector */
401 info->start[i] = base;
403 while (i > 0) { /* 64k regular sectors */
406 info->start[i] = base;
409 if (info->flash_id & FLASH_BTYPE) {
410 /* set sector offsets for bottom boot block type */
411 info->start[0] = base + 0x00000000;
412 info->start[1] = base + 0x00004000;
413 info->start[2] = base + 0x00006000;
414 info->start[3] = base + 0x00008000;
415 for (i = 4; i < info->sector_count; i++) {
416 info->start[i] = base + (i * 0x00010000) - 0x00030000;
419 /* set sector offsets for top boot block type */
420 i = info->sector_count - 1;
421 info->start[i--] = base + info->size - 0x00004000;
422 info->start[i--] = base + info->size - 0x00006000;
423 info->start[i--] = base + info->size - 0x00008000;
424 for (; i >= 0; i--) {
425 info->start[i] = base + i * 0x00010000;
430 /* check for protected sectors */
431 for (i = 0; i < info->sector_count; i++) {
432 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
433 /* D0 = 1 if protected */
434 addr2 = (volatile CFG_FLASH_WORD_SIZE *)(info->start[i]);
435 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
436 info->protect[i] = 0;
438 info->protect[i] = addr2[CFG_FLASH_READ2] & 1;
442 * Prevent writes to uninitialized FLASH.
444 if (info->flash_id != FLASH_UNKNOWN) {
445 addr2 = (CFG_FLASH_WORD_SIZE *)info->start[0];
446 *addr2 = (CFG_FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
453 /*-----------------------------------------------------------------------
456 int flash_erase (flash_info_t *info, int s_first, int s_last)
458 volatile CFG_FLASH_WORD_SIZE *addr = (CFG_FLASH_WORD_SIZE *)(info->start[0]);
459 volatile CFG_FLASH_WORD_SIZE *addr2;
460 int flag, prot, sect, l_sect;
461 ulong start, now, last;
464 if ((s_first < 0) || (s_first > s_last)) {
465 if (info->flash_id == FLASH_UNKNOWN) {
466 printf ("- missing\n");
468 printf ("- no sectors to erase\n");
473 if (info->flash_id == FLASH_UNKNOWN) {
474 printf ("Can't erase unknown flash type - aborted\n");
479 for (sect=s_first; sect<=s_last; ++sect) {
480 if (info->protect[sect]) {
486 printf ("- Warning: %d protected sectors will not be erased!\n",
494 /* Disable interrupts which might cause a timeout here */
495 flag = disable_interrupts();
497 /* Start erase on unprotected sectors */
498 for (sect = s_first; sect<=s_last; sect++) {
499 if (info->protect[sect] == 0) { /* not protected */
500 addr2 = (CFG_FLASH_WORD_SIZE *)(info->start[sect]);
501 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
502 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
503 addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
504 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00800080;
505 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
506 addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
507 addr2[0] = (CFG_FLASH_WORD_SIZE)0x00500050; /* block erase */
509 udelay(1000); /* wait 1 ms */
511 if (sect == s_first) {
512 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
513 addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
514 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00800080;
515 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
516 addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
518 addr2[0] = (CFG_FLASH_WORD_SIZE)0x00300030; /* sector erase */
524 /* re-enable interrupts if necessary */
528 /* wait at least 80us - let's wait 1 ms */
532 * We wait for the last triggered sector
537 start = get_timer (0);
539 addr = (CFG_FLASH_WORD_SIZE *)(info->start[l_sect]);
540 while ((addr[0] & (CFG_FLASH_WORD_SIZE)0x00800080) != (CFG_FLASH_WORD_SIZE)0x00800080) {
541 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
542 printf ("Timeout\n");
545 /* show that we're waiting */
546 if ((now - last) > 1000) { /* every second */
553 /* reset to read mode */
554 addr = (CFG_FLASH_WORD_SIZE *)info->start[0];
555 addr[0] = (CFG_FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
561 /*-----------------------------------------------------------------------
562 * Copy memory to flash, returns:
565 * 2 - Flash not erased
568 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
573 wp = (addr & ~3); /* get lower word aligned address */
576 * handle unaligned start bytes
578 if ((l = addr - wp) != 0) {
580 for (i=0, cp=wp; i<l; ++i, ++cp) {
581 data = (data << 8) | (*(uchar *)cp);
583 for (; i<4 && cnt>0; ++i) {
584 data = (data << 8) | *src++;
588 for (; cnt==0 && i<4; ++i, ++cp) {
589 data = (data << 8) | (*(uchar *)cp);
592 if ((rc = write_word(info, wp, data)) != 0) {
599 * handle word aligned part
603 for (i=0; i<4; ++i) {
604 data = (data << 8) | *src++;
606 if ((rc = write_word(info, wp, data)) != 0) {
618 * handle unaligned tail bytes
621 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
622 data = (data << 8) | *src++;
625 for (; i<4; ++i, ++cp) {
626 data = (data << 8) | (*(uchar *)cp);
629 return (write_word(info, wp, data));
632 /*-----------------------------------------------------------------------
633 * Write a word to Flash, returns:
636 * 2 - Flash not erased
638 static int write_word (flash_info_t *info, ulong dest, ulong data)
640 volatile CFG_FLASH_WORD_SIZE *addr2 = (CFG_FLASH_WORD_SIZE *)(info->start[0]);
641 volatile CFG_FLASH_WORD_SIZE *dest2 = (CFG_FLASH_WORD_SIZE *)dest;
642 volatile CFG_FLASH_WORD_SIZE *data2 = (CFG_FLASH_WORD_SIZE *)&data;
647 /* Check if Flash is (sufficiently) erased */
648 if ((*((volatile CFG_FLASH_WORD_SIZE *)dest) &
649 (CFG_FLASH_WORD_SIZE)data) != (CFG_FLASH_WORD_SIZE)data) {
652 /* Disable interrupts which might cause a timeout here */
653 flag = disable_interrupts();
655 for (i=0; i<4/sizeof(CFG_FLASH_WORD_SIZE); i++)
657 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
658 addr2[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
659 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00A000A0;
663 /* re-enable interrupts if necessary */
667 /* data polling for D7 */
668 start = get_timer (0);
669 while ((dest2[i] & (CFG_FLASH_WORD_SIZE)0x00800080) !=
670 (data2[i] & (CFG_FLASH_WORD_SIZE)0x00800080)) {
671 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
680 /*-----------------------------------------------------------------------