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 #ifndef CFG_FLASH_READ0
28 #define CFG_FLASH_READ0 0x0000 /* 0 is standard */
29 #define CFG_FLASH_READ1 0x0001 /* 1 is standard */
30 #define CFG_FLASH_READ2 0x0002 /* 2 is standard */
33 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
38 static int write_word(flash_info_t *info, ulong dest, ulong data);
39 static ulong flash_get_size(vu_long *addr, flash_info_t *info);
41 unsigned long flash_init(void)
43 unsigned long size_b0, size_b1;
45 unsigned long base_b0, base_b1;
47 /* Init: no FLASHes known */
48 for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
49 flash_info[i].flash_id = FLASH_UNKNOWN;
52 /* Static FLASH Bank configuration here - FIXME XXX */
54 base_b0 = FLASH_BASE0_PRELIM;
55 size_b0 = flash_get_size ((vu_long *) base_b0, &flash_info[0]);
57 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
58 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
59 size_b0, size_b0 << 20);
62 base_b1 = FLASH_BASE1_PRELIM;
63 size_b1 = flash_get_size ((vu_long *) base_b1, &flash_info[1]);
65 return (size_b0 + size_b1);
68 void flash_print_info(flash_info_t *info)
74 volatile unsigned long *flash;
76 if (info->flash_id == FLASH_UNKNOWN) {
77 printf ("missing or unknown FLASH type\n");
81 switch (info->flash_id & FLASH_VENDMASK) {
82 case FLASH_MAN_AMD: printf ("AMD "); break;
83 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
84 case FLASH_MAN_SST: printf ("SST "); break;
85 case FLASH_MAN_EXCEL: printf ("Excel Semiconductor "); break;
86 default: printf ("Unknown Vendor "); break;
89 switch (info->flash_id & FLASH_TYPEMASK) {
90 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
92 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
94 case FLASH_AM040: printf ("AM29LV040B (4 Mbit, uniform sector size)\n");
96 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
98 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
100 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
102 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
104 case FLASH_AM320T: printf ("AM29LV320T (32 M, top sector)\n");
106 case FLASH_AM320B: printf ("AM29LV320B (32 M, bottom sector)\n");
108 case FLASH_AMDL322T: printf ("AM29DL322T (32 M, top sector)\n");
110 case FLASH_AMDL322B: printf ("AM29DL322B (32 M, bottom sector)\n");
112 case FLASH_AMDL323T: printf ("AM29DL323T (32 M, top sector)\n");
114 case FLASH_AMDL323B: printf ("AM29DL323B (32 M, bottom sector)\n");
116 case FLASH_SST020: printf ("SST39LF/VF020 (2 Mbit, uniform sector size)\n");
118 case FLASH_SST040: printf ("SST39LF/VF040 (4 Mbit, uniform sector size)\n");
120 default: printf ("Unknown Chip Type\n");
124 printf (" Size: %ld MB in %d Sectors\n",
125 info->size >> 20, info->sector_count);
127 printf (" Sector Start Addresses:");
128 for (i=0; i<info->sector_count; ++i) {
129 #ifdef CFG_FLASH_EMPTY_INFO
131 * Check if whole sector is erased
133 if (i != (info->sector_count-1))
134 size = info->start[i+1] - info->start[i];
136 size = info->start[0] + info->size - info->start[i];
138 flash = (volatile unsigned long *)info->start[i];
139 size = size >> 2; /* divide by 4 for longword access */
140 for (k=0; k<size; k++) {
141 if (*flash++ != 0xffffffff) {
149 /* print empty and read-only info */
150 printf (" %08lX%s%s",
153 info->protect[i] ? "RO " : " ");
159 info->protect[i] ? " (RO)" : " ");
168 * The following code cannot be run from FLASH!
170 static ulong flash_get_size(vu_long *addr, flash_info_t *info)
174 volatile CFG_FLASH_WORD_SIZE value;
175 ulong base = (ulong)addr;
176 volatile CFG_FLASH_WORD_SIZE *addr2 = (volatile CFG_FLASH_WORD_SIZE *)addr;
178 /* Write auto select command: read Manufacturer ID */
179 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
180 addr2[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
181 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00900090;
183 value = addr2[CFG_FLASH_READ0];
186 case (CFG_FLASH_WORD_SIZE)AMD_MANUFACT:
187 info->flash_id = FLASH_MAN_AMD;
189 case (CFG_FLASH_WORD_SIZE)FUJ_MANUFACT:
190 info->flash_id = FLASH_MAN_FUJ;
192 case (CFG_FLASH_WORD_SIZE)SST_MANUFACT:
193 info->flash_id = FLASH_MAN_SST;
195 case (CFG_FLASH_WORD_SIZE)EXCEL_MANUFACT:
196 info->flash_id = FLASH_MAN_EXCEL;
199 info->flash_id = FLASH_UNKNOWN;
200 info->sector_count = 0;
202 return (0); /* no or unknown flash */
205 value = addr2[CFG_FLASH_READ1]; /* device ID */
208 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV400T:
209 info->flash_id += FLASH_AM400T;
210 info->sector_count = 11;
211 info->size = 0x00080000;
212 break; /* => 0.5 MB */
214 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV400B:
215 info->flash_id += FLASH_AM400B;
216 info->sector_count = 11;
217 info->size = 0x00080000;
218 break; /* => 0.5 MB */
220 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV040B:
221 info->flash_id += FLASH_AM040;
222 info->sector_count = 8;
223 info->size = 0x0080000; /* => 0.5 MB */
226 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV800T:
227 info->flash_id += FLASH_AM800T;
228 info->sector_count = 19;
229 info->size = 0x00100000;
232 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV800B:
233 info->flash_id += FLASH_AM800B;
234 info->sector_count = 19;
235 info->size = 0x00100000;
238 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV160T:
239 info->flash_id += FLASH_AM160T;
240 info->sector_count = 35;
241 info->size = 0x00200000;
244 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV160B:
245 info->flash_id += FLASH_AM160B;
246 info->sector_count = 35;
247 info->size = 0x00200000;
250 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV320T:
251 info->flash_id += FLASH_AM320T;
252 info->sector_count = 71;
253 info->size = 0x00400000;
256 case (CFG_FLASH_WORD_SIZE)AMD_ID_LV320B:
257 info->flash_id += FLASH_AM320B;
258 info->sector_count = 71;
259 info->size = 0x00400000;
262 case (CFG_FLASH_WORD_SIZE)AMD_ID_DL322T:
263 info->flash_id += FLASH_AMDL322T;
264 info->sector_count = 71;
265 info->size = 0x00400000;
268 case (CFG_FLASH_WORD_SIZE)AMD_ID_DL322B:
269 info->flash_id += FLASH_AMDL322B;
270 info->sector_count = 71;
271 info->size = 0x00400000;
274 case (CFG_FLASH_WORD_SIZE)AMD_ID_DL323T:
275 info->flash_id += FLASH_AMDL323T;
276 info->sector_count = 71;
277 info->size = 0x00400000;
280 case (CFG_FLASH_WORD_SIZE)AMD_ID_DL323B:
281 info->flash_id += FLASH_AMDL323B;
282 info->sector_count = 71;
283 info->size = 0x00400000;
286 case (CFG_FLASH_WORD_SIZE)SST_ID_xF020:
287 info->flash_id += FLASH_SST020;
288 info->sector_count = 64;
289 info->size = 0x00040000;
290 break; /* => 256 kB */
292 case (CFG_FLASH_WORD_SIZE)SST_ID_xF040:
293 info->flash_id += FLASH_SST040;
294 info->sector_count = 128;
295 info->size = 0x00080000;
296 break; /* => 512 kB */
299 info->flash_id = FLASH_UNKNOWN;
300 return (0); /* => no or unknown flash */
304 /* set up sector start address table */
305 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
306 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM640U)) {
307 for (i = 0; i < info->sector_count; i++)
308 info->start[i] = base + (i * 0x00001000);
309 } else if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) {
310 for (i = 0; i < info->sector_count; i++)
311 info->start[i] = base + (i * 0x00010000);
312 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322B) ||
313 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323B) ||
314 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
315 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324B)) {
316 /* set sector offsets for bottom boot block type */
317 for (i=0; i<8; ++i) { /* 8 x 8k boot sectors */
318 info->start[i] = base;
321 while (i < info->sector_count) { /* 64k regular sectors */
322 info->start[i] = base;
326 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322T) ||
327 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323T) ||
328 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
329 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324T)) {
330 /* set sector offsets for top boot block type */
332 i = info->sector_count;
333 for (n=0; n<8; ++n) { /* 8 x 8k boot sectors */
336 info->start[i] = base;
338 while (i > 0) { /* 64k regular sectors */
341 info->start[i] = base;
344 if (info->flash_id & FLASH_BTYPE) {
345 /* set sector offsets for bottom boot block type */
346 info->start[0] = base + 0x00000000;
347 info->start[1] = base + 0x00004000;
348 info->start[2] = base + 0x00006000;
349 info->start[3] = base + 0x00008000;
350 for (i = 4; i < info->sector_count; i++) {
351 info->start[i] = base + (i * 0x00010000) - 0x00030000;
354 /* set sector offsets for top boot block type */
355 i = info->sector_count - 1;
356 info->start[i--] = base + info->size - 0x00004000;
357 info->start[i--] = base + info->size - 0x00006000;
358 info->start[i--] = base + info->size - 0x00008000;
359 for (; i >= 0; i--) {
360 info->start[i] = base + i * 0x00010000;
365 /* check for protected sectors */
366 for (i = 0; i < info->sector_count; i++) {
367 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
368 /* D0 = 1 if protected */
369 addr2 = (volatile CFG_FLASH_WORD_SIZE *)(info->start[i]);
370 if ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_AMD)
371 info->protect[i] = 0;
373 info->protect[i] = addr2[CFG_FLASH_READ2] & 1;
377 * Prevent writes to uninitialized FLASH.
379 if (info->flash_id != FLASH_UNKNOWN) {
380 addr2 = (CFG_FLASH_WORD_SIZE *)info->start[0];
381 *addr2 = (CFG_FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
388 int flash_erase(flash_info_t *info, int s_first, int s_last)
390 volatile CFG_FLASH_WORD_SIZE *addr = (CFG_FLASH_WORD_SIZE *)(info->start[0]);
391 volatile CFG_FLASH_WORD_SIZE *addr2;
392 int flag, prot, sect, l_sect;
393 ulong start, now, last;
395 if ((s_first < 0) || (s_first > s_last)) {
396 if (info->flash_id == FLASH_UNKNOWN)
397 printf ("- missing\n");
399 printf ("- no sectors to erase\n");
403 if (info->flash_id == FLASH_UNKNOWN) {
404 printf ("Can't erase unknown flash type - aborted\n");
409 for (sect=s_first; sect<=s_last; ++sect)
410 if (info->protect[sect])
414 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
420 /* Disable interrupts which might cause a timeout here */
421 flag = disable_interrupts();
423 /* Start erase on unprotected sectors */
424 for (sect = s_first; sect<=s_last; sect++) {
425 if (info->protect[sect] == 0) { /* not protected */
426 addr2 = (CFG_FLASH_WORD_SIZE *)(info->start[sect]);
427 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
428 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
429 addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
430 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00800080;
431 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
432 addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
433 addr2[0] = (CFG_FLASH_WORD_SIZE)0x00300030; /* sector erase */
435 /* re-enable interrupts if necessary */
441 /* data polling for D7 */
442 start = get_timer (0);
443 while ((addr2[0] & (CFG_FLASH_WORD_SIZE)0x00800080) !=
444 (CFG_FLASH_WORD_SIZE)0x00800080) {
445 if (get_timer(start) > CFG_FLASH_WRITE_TOUT)
449 if (sect == s_first) {
450 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
451 addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
452 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00800080;
453 addr[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
454 addr[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
456 addr2[0] = (CFG_FLASH_WORD_SIZE)0x00300030; /* sector erase */
462 /* re-enable interrupts if necessary */
466 /* wait at least 80us - let's wait 1 ms */
470 * We wait for the last triggered sector
475 start = get_timer (0);
477 addr = (CFG_FLASH_WORD_SIZE *)(info->start[l_sect]);
478 while ((addr[0] & (CFG_FLASH_WORD_SIZE)0x00800080) != (CFG_FLASH_WORD_SIZE)0x00800080) {
479 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
480 printf ("Timeout\n");
483 /* show that we're waiting */
484 if ((now - last) > 1000) { /* every second */
491 /* reset to read mode */
492 addr = (CFG_FLASH_WORD_SIZE *)info->start[0];
493 addr[0] = (CFG_FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
500 * Copy memory to flash, returns:
503 * 2 - Flash not erased
505 int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
510 wp = (addr & ~3); /* get lower word aligned address */
513 * handle unaligned start bytes
515 if ((l = addr - wp) != 0) {
517 for (i=0, cp=wp; i<l; ++i, ++cp) {
518 data = (data << 8) | (*(uchar *)cp);
520 for (; i<4 && cnt>0; ++i) {
521 data = (data << 8) | *src++;
525 for (; cnt==0 && i<4; ++i, ++cp) {
526 data = (data << 8) | (*(uchar *)cp);
529 if ((rc = write_word(info, wp, data)) != 0) {
536 * handle word aligned part
541 data = (data << 8) | *src++;
542 if ((rc = write_word(info, wp, data)) != 0)
552 * handle unaligned tail bytes
555 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
556 data = (data << 8) | *src++;
559 for (; i<4; ++i, ++cp)
560 data = (data << 8) | (*(uchar *)cp);
562 return (write_word(info, wp, data));
566 * Write a word to Flash, returns:
569 * 2 - Flash not erased
571 static int write_word(flash_info_t *info, ulong dest, ulong data)
573 volatile CFG_FLASH_WORD_SIZE *addr2 = (CFG_FLASH_WORD_SIZE *)(info->start[0]);
574 volatile CFG_FLASH_WORD_SIZE *dest2 = (CFG_FLASH_WORD_SIZE *)dest;
575 volatile CFG_FLASH_WORD_SIZE *data2 = (CFG_FLASH_WORD_SIZE *)&data;
580 /* Check if Flash is (sufficiently) erased */
581 if ((*((vu_long *)dest) & data) != data)
584 /* Disable interrupts which might cause a timeout here */
585 flag = disable_interrupts();
587 for (i=0; i<4/sizeof(CFG_FLASH_WORD_SIZE); i++) {
588 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00AA00AA;
589 addr2[CFG_FLASH_ADDR1] = (CFG_FLASH_WORD_SIZE)0x00550055;
590 addr2[CFG_FLASH_ADDR0] = (CFG_FLASH_WORD_SIZE)0x00A000A0;
594 /* re-enable interrupts if necessary */
598 /* data polling for D7 */
599 start = get_timer (0);
600 while ((dest2[i] & (CFG_FLASH_WORD_SIZE)0x00800080) !=
601 (data2[i] & (CFG_FLASH_WORD_SIZE)0x00800080)) {
602 if (get_timer(start) > CFG_FLASH_WRITE_TOUT)