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[CFG_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 CFG_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 CFG_FLASH_WORD_SIZE value;
220 ulong base = (ulong)addr;
221 volatile CFG_FLASH_WORD_SIZE *addr2 = (CFG_FLASH_WORD_SIZE *)addr;
223 /* Write auto select command: read Manufacturer ID */
224 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
225 addr2[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
226 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00900090;
228 value = addr2[CFG_FLASH_READ0];
231 case (CFG_FLASH_WORD_SIZE)AMD_MANUFACT:
232 info->flash_id = FLASH_MAN_AMD;
234 case (CFG_FLASH_WORD_SIZE)FUJ_MANUFACT:
235 info->flash_id = FLASH_MAN_FUJ;
237 case (CFG_FLASH_WORD_SIZE)SST_MANUFACT:
238 info->flash_id = FLASH_MAN_SST;
240 case (CFG_FLASH_WORD_SIZE)STM_MANUFACT:
241 info->flash_id = FLASH_MAN_STM;
244 info->flash_id = FLASH_UNKNOWN;
245 info->sector_count = 0;
247 return (0); /* no or unknown flash */
250 value = addr2[CFG_FLASH_READ1]; /* device ID */
253 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV400T:
254 info->flash_id += FLASH_AM400T;
255 info->sector_count = 11;
256 info->size = 0x00080000;
257 break; /* => 0.5 MB */
259 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV400B:
260 info->flash_id += FLASH_AM400B;
261 info->sector_count = 11;
262 info->size = 0x00080000;
263 break; /* => 0.5 MB */
265 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV800T:
266 info->flash_id += FLASH_AM800T;
267 info->sector_count = 19;
268 info->size = 0x00100000;
271 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV800B:
272 info->flash_id += FLASH_AM800B;
273 info->sector_count = 19;
274 info->size = 0x00100000;
277 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV160T:
278 info->flash_id += FLASH_AM160T;
279 info->sector_count = 35;
280 info->size = 0x00200000;
283 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV160B:
284 info->flash_id += FLASH_AM160B;
285 info->sector_count = 35;
286 info->size = 0x00200000;
289 case (CFG_FLASH_WORD_SIZE)STM_ID_29W320DT:
290 info->flash_id += FLASH_STMW320DT;
291 info->sector_count = 67;
292 info->size = 0x00400000; break; /* => 4 MB */
294 case (CFG_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 (CFG_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 (CFG_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 (CFG_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 (CFG_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 (CFG_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 (CFG_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 case (CFG_FLASH_WORD_SIZE)SST_ID_xF800A:
330 info->flash_id += FLASH_SST800A;
331 info->sector_count = 16;
332 info->size = 0x00100000;
335 case (CFG_FLASH_WORD_SIZE)SST_ID_xF160A:
336 info->flash_id += FLASH_SST160A;
337 info->sector_count = 32;
338 info->size = 0x00200000;
342 info->flash_id = FLASH_UNKNOWN;
343 return (0); /* => no or unknown flash */
347 /* set up sector start address table */
348 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
349 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM640U)) {
350 for (i = 0; i < info->sector_count; i++)
351 info->start[i] = base + (i * 0x00010000);
352 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322B) ||
353 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323B) ||
354 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
355 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324B)) {
356 /* set sector offsets for bottom boot block type */
357 for (i=0; i<8; ++i) { /* 8 x 8k boot sectors */
358 info->start[i] = base;
361 while (i < info->sector_count) { /* 64k regular sectors */
362 info->start[i] = base;
366 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322T) ||
367 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323T) ||
368 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
369 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324T)) {
370 /* set sector offsets for top boot block type */
372 i = info->sector_count;
373 for (n=0; n<8; ++n) { /* 8 x 8k boot sectors */
376 info->start[i] = base;
378 while (i > 0) { /* 64k regular sectors */
381 info->start[i] = base;
383 } else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_STMW320DT) {
384 /* set sector offsets for top boot block type */
386 i = info->sector_count;
387 /* 1 x 16k boot sector */
390 info->start[i] = base;
391 /* 2 x 8k boot sectors */
392 for (n=0; n<2; ++n) {
395 info->start[i] = base;
397 /* 1 x 32k boot sector */
400 info->start[i] = base;
402 while (i > 0) { /* 64k regular sectors */
405 info->start[i] = base;
408 if (info->flash_id & FLASH_BTYPE) {
409 /* set sector offsets for bottom boot block type */
410 info->start[0] = base + 0x00000000;
411 info->start[1] = base + 0x00004000;
412 info->start[2] = base + 0x00006000;
413 info->start[3] = base + 0x00008000;
414 for (i = 4; i < info->sector_count; i++) {
415 info->start[i] = base + (i * 0x00010000) - 0x00030000;
418 /* set sector offsets for top boot block type */
419 i = info->sector_count - 1;
420 info->start[i--] = base + info->size - 0x00004000;
421 info->start[i--] = base + info->size - 0x00006000;
422 info->start[i--] = base + info->size - 0x00008000;
423 for (; i >= 0; i--) {
424 info->start[i] = base + i * 0x00010000;
429 /* check for protected sectors */
430 for (i = 0; i < info->sector_count; i++) {
431 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
432 /* D0 = 1 if protected */
433 addr2 = (volatile CFG_FLASH_WORD_SIZE *)(info->start[i]);
434 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
435 info->protect[i] = 0;
437 info->protect[i] = addr2[CFG_FLASH_READ2] & 1;
441 * Prevent writes to uninitialized FLASH.
443 if (info->flash_id != FLASH_UNKNOWN) {
444 addr2 = (CFG_FLASH_WORD_SIZE *)info->start[0];
445 *addr2 = (CFG_FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
452 /*-----------------------------------------------------------------------
455 int flash_erase (flash_info_t *info, int s_first, int s_last)
457 volatile CFG_FLASH_WORD_SIZE *addr = (CFG_FLASH_WORD_SIZE *)(info->start[0]);
458 volatile CFG_FLASH_WORD_SIZE *addr2;
459 int flag, prot, sect, l_sect;
460 ulong start, now, last;
463 if ((s_first < 0) || (s_first > s_last)) {
464 if (info->flash_id == FLASH_UNKNOWN) {
465 printf ("- missing\n");
467 printf ("- no sectors to erase\n");
472 if (info->flash_id == FLASH_UNKNOWN) {
473 printf ("Can't erase unknown flash type - aborted\n");
478 for (sect=s_first; sect<=s_last; ++sect) {
479 if (info->protect[sect]) {
485 printf ("- Warning: %d protected sectors will not be erased!\n",
493 /* Disable interrupts which might cause a timeout here */
494 flag = disable_interrupts();
496 /* Start erase on unprotected sectors */
497 for (sect = s_first; sect<=s_last; sect++) {
498 if (info->protect[sect] == 0) { /* not protected */
499 addr2 = (CFG_FLASH_WORD_SIZE *)(info->start[sect]);
500 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
501 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
502 addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
503 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00800080;
504 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
505 addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
506 addr2[0] = (CFG_FLASH_WORD_SIZE)0x00500050; /* block erase */
508 udelay(1000); /* wait 1 ms */
510 if (sect == s_first) {
511 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
512 addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
513 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00800080;
514 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
515 addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
517 addr2[0] = (CFG_FLASH_WORD_SIZE)0x00300030; /* sector erase */
523 /* re-enable interrupts if necessary */
527 /* wait at least 80us - let's wait 1 ms */
531 * We wait for the last triggered sector
536 start = get_timer (0);
538 addr = (CFG_FLASH_WORD_SIZE *)(info->start[l_sect]);
539 while ((addr[0] & (CFG_FLASH_WORD_SIZE)0x00800080) != (CFG_FLASH_WORD_SIZE)0x00800080) {
540 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
541 printf ("Timeout\n");
544 /* show that we're waiting */
545 if ((now - last) > 1000) { /* every second */
552 /* reset to read mode */
553 addr = (CFG_FLASH_WORD_SIZE *)info->start[0];
554 addr[0] = (CFG_FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
560 /*-----------------------------------------------------------------------
561 * Copy memory to flash, returns:
564 * 2 - Flash not erased
567 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
572 wp = (addr & ~3); /* get lower word aligned address */
575 * handle unaligned start bytes
577 if ((l = addr - wp) != 0) {
579 for (i=0, cp=wp; i<l; ++i, ++cp) {
581 data = data | ((*(uchar *)cp)<<(8*i));
583 data = (data << 8) | (*(uchar *)cp);
586 for (; i<4 && cnt>0; ++i) {
588 data = data | ((*src++)<<(8*i));
590 data = (data << 8) | *src++;
595 for (; cnt==0 && i<4; ++i, ++cp) {
597 data = data | ((*(uchar *)cp)<<(8*i));
599 data = (data << 8) | (*(uchar *)cp);
603 if ((rc = write_word(info, wp, data)) != 0) {
610 * handle word aligned part
615 data = (*(ulong*)src);
618 for (i=0; i<4; ++i) {
619 data = (data << 8) | *src++;
622 if ((rc = write_word(info, wp, data)) != 0) {
634 * handle unaligned tail bytes
637 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
639 data = data | ((*src++)<<(8*i));
641 data = (data << 8) | *src++;
645 for (; i<4; ++i, ++cp) {
647 data = data | ((*(uchar *)cp)<<(8*i));
649 data = (data << 8) | (*(uchar *)cp);
653 return (write_word(info, wp, data));
656 /*-----------------------------------------------------------------------
657 * Write a word to Flash, returns:
660 * 2 - Flash not erased
662 static int write_word (flash_info_t *info, ulong dest, ulong data)
664 volatile CFG_FLASH_WORD_SIZE *addr2 = (CFG_FLASH_WORD_SIZE *)(info->start[0]);
665 volatile CFG_FLASH_WORD_SIZE *dest2 = (CFG_FLASH_WORD_SIZE *)dest;
666 volatile CFG_FLASH_WORD_SIZE *data2 = (CFG_FLASH_WORD_SIZE *)&data;
671 /* Check if Flash is (sufficiently) erased */
672 if ((*((volatile ulong *)dest) & data) != data) {
675 /* Disable interrupts which might cause a timeout here */
676 flag = disable_interrupts();
678 for (i=0; i<4/sizeof(CFG_FLASH_WORD_SIZE); i++)
680 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
681 addr2[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
682 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00A000A0;
686 /* re-enable interrupts if necessary */
690 /* data polling for D7 */
691 start = get_timer (0);
692 while ((dest2[i] & (CFG_FLASH_WORD_SIZE)0x00800080) !=
693 (data2[i] & (CFG_FLASH_WORD_SIZE)0x00800080)) {
694 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
703 /*-----------------------------------------------------------------------