3 * Stefan Roese, esd gmbh germany, stefan.roese@esd-electronics.com
5 * SPDX-License-Identifier: GPL-2.0+
10 #include <asm/ppc4xx.h>
12 #include <asm/processor.h>
14 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
16 /*-----------------------------------------------------------------------
19 static int write_word (flash_info_t *info, ulong dest, ulong data);
21 /*-----------------------------------------------------------------------
23 static void flash_get_offsets (ulong base, flash_info_t *info)
28 /* set up sector start address table */
29 if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
30 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM640U)) {
31 for (i = 0; i < info->sector_count; i++)
32 info->start[i] = base + (i * 0x00010000);
33 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322B) ||
34 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323B) ||
35 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320B) ||
36 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324B)) {
37 /* set sector offsets for bottom boot block type */
38 for (i=0; i<8; ++i) { /* 8 x 8k boot sectors */
39 info->start[i] = base;
42 while (i < info->sector_count) { /* 64k regular sectors */
43 info->start[i] = base;
47 } else if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL322T) ||
48 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL323T) ||
49 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM320T) ||
50 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AMDL324T)) {
51 /* set sector offsets for top boot block type */
53 i = info->sector_count;
54 for (n=0; n<8; ++n) { /* 8 x 8k boot sectors */
57 info->start[i] = base;
59 while (i > 0) { /* 64k regular sectors */
62 info->start[i] = base;
65 if (info->flash_id & FLASH_BTYPE) {
66 /* set sector offsets for bottom boot block type */
67 info->start[0] = base + 0x00000000;
68 info->start[1] = base + 0x00004000;
69 info->start[2] = base + 0x00006000;
70 info->start[3] = base + 0x00008000;
71 for (i = 4; i < info->sector_count; i++) {
72 info->start[i] = base + (i * 0x00010000) - 0x00030000;
75 /* set sector offsets for top boot block type */
76 i = info->sector_count - 1;
77 info->start[i--] = base + info->size - 0x00004000;
78 info->start[i--] = base + info->size - 0x00006000;
79 info->start[i--] = base + info->size - 0x00008000;
81 info->start[i] = base + i * 0x00010000;
87 /*-----------------------------------------------------------------------
89 void flash_print_info (flash_info_t *info)
95 volatile unsigned long *flash;
97 if (info->flash_id == FLASH_UNKNOWN) {
98 printf ("missing or unknown FLASH type\n");
102 switch (info->flash_id & FLASH_VENDMASK) {
103 case FLASH_MAN_AMD: printf ("AMD "); break;
104 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
105 case FLASH_MAN_SST: printf ("SST "); break;
106 case FLASH_MAN_EXCEL: printf ("Excel Semiconductor "); break;
107 default: printf ("Unknown Vendor "); break;
110 switch (info->flash_id & FLASH_TYPEMASK) {
111 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
113 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
115 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
117 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
119 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
121 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
123 case FLASH_AM320T: printf ("AM29LV320T (32 M, top sector)\n");
125 case FLASH_AM320B: printf ("AM29LV320B (32 M, bottom sector)\n");
127 case FLASH_AMDL322T: printf ("AM29DL322T (32 M, top sector)\n");
129 case FLASH_AMDL322B: printf ("AM29DL322B (32 M, bottom sector)\n");
131 case FLASH_AMDL323T: printf ("AM29DL323T (32 M, top sector)\n");
133 case FLASH_AMDL323B: printf ("AM29DL323B (32 M, bottom sector)\n");
135 case FLASH_AM640U: printf ("AM29LV640D (64 M, uniform sector)\n");
137 case FLASH_SST800A: printf ("SST39LF/VF800 (8 Mbit, uniform sector size)\n");
139 case FLASH_SST160A: printf ("SST39LF/VF160 (16 Mbit, uniform sector size)\n");
141 case FLASH_SST320: printf ("SST39LF/VF320 (32 Mbit, uniform sector size)\n");
143 case FLASH_SST640: printf ("SST39LF/VF640 (64 Mbit, uniform sector size)\n");
145 default: printf ("Unknown Chip Type\n");
149 printf (" Size: %ld MB in %d Sectors\n",
150 info->size >> 20, info->sector_count);
152 printf (" Sector Start Addresses:");
153 for (i=0; i<info->sector_count; ++i) {
154 #ifdef CONFIG_SYS_FLASH_EMPTY_INFO
156 * Check if whole sector is erased
158 if (i != (info->sector_count-1))
159 size = info->start[i+1] - info->start[i];
161 size = info->start[0] + info->size - info->start[i];
163 flash = (volatile unsigned long *)info->start[i];
164 size = size >> 2; /* divide by 4 for longword access */
165 for (k=0; k<size; k++)
167 if (*flash++ != 0xffffffff)
176 /* print empty and read-only info */
177 printf (" %08lX%s%s",
180 info->protect[i] ? "RO " : " ");
186 info->protect[i] ? " (RO)" : " ");
194 /*-----------------------------------------------------------------------
198 /*-----------------------------------------------------------------------
202 * The following code cannot be run from FLASH!
204 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
208 CONFIG_SYS_FLASH_WORD_SIZE value;
209 ulong base = (ulong)addr;
210 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)addr;
212 /* Write auto select command: read Manufacturer ID */
213 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
214 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
215 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00900090;
217 value = addr2[CONFIG_SYS_FLASH_READ0];
220 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_MANUFACT:
221 info->flash_id = FLASH_MAN_AMD;
223 case (CONFIG_SYS_FLASH_WORD_SIZE)FUJ_MANUFACT:
224 info->flash_id = FLASH_MAN_FUJ;
226 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_MANUFACT:
227 info->flash_id = FLASH_MAN_SST;
229 case (CONFIG_SYS_FLASH_WORD_SIZE)EXCEL_MANUFACT:
230 info->flash_id = FLASH_MAN_EXCEL;
233 info->flash_id = FLASH_UNKNOWN;
234 info->sector_count = 0;
236 return (0); /* no or unknown flash */
239 value = addr2[CONFIG_SYS_FLASH_READ1]; /* device ID */
242 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV400T:
243 info->flash_id += FLASH_AM400T;
244 info->sector_count = 11;
245 info->size = 0x00080000;
246 break; /* => 0.5 MB */
248 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV400B:
249 info->flash_id += FLASH_AM400B;
250 info->sector_count = 11;
251 info->size = 0x00080000;
252 break; /* => 0.5 MB */
254 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV800T:
255 info->flash_id += FLASH_AM800T;
256 info->sector_count = 19;
257 info->size = 0x00100000;
260 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV800B:
261 info->flash_id += FLASH_AM800B;
262 info->sector_count = 19;
263 info->size = 0x00100000;
266 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV160T:
267 info->flash_id += FLASH_AM160T;
268 info->sector_count = 35;
269 info->size = 0x00200000;
272 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV160B:
273 info->flash_id += FLASH_AM160B;
274 info->sector_count = 35;
275 info->size = 0x00200000;
278 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV320T:
279 info->flash_id += FLASH_AM320T;
280 info->sector_count = 71;
281 info->size = 0x00400000; break; /* => 4 MB */
283 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV320B:
284 info->flash_id += FLASH_AM320B;
285 info->sector_count = 71;
286 info->size = 0x00400000; break; /* => 4 MB */
288 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL322T:
289 info->flash_id += FLASH_AMDL322T;
290 info->sector_count = 71;
291 info->size = 0x00400000; break; /* => 4 MB */
293 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL322B:
294 info->flash_id += FLASH_AMDL322B;
295 info->sector_count = 71;
296 info->size = 0x00400000; break; /* => 4 MB */
298 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL323T:
299 info->flash_id += FLASH_AMDL323T;
300 info->sector_count = 71;
301 info->size = 0x00400000; break; /* => 4 MB */
303 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_DL323B:
304 info->flash_id += FLASH_AMDL323B;
305 info->sector_count = 71;
306 info->size = 0x00400000; break; /* => 4 MB */
308 case (CONFIG_SYS_FLASH_WORD_SIZE)AMD_ID_LV640U:
309 info->flash_id += FLASH_AM640U;
310 info->sector_count = 128;
311 info->size = 0x00800000; break; /* => 8 MB */
313 #if !(defined(CONFIG_ADCIOP) || defined(CONFIG_DASA_SIM))
314 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF800A:
315 info->flash_id += FLASH_SST800A;
316 info->sector_count = 16;
317 info->size = 0x00100000;
320 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF160A:
321 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF1601:
322 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF1602:
323 info->flash_id += FLASH_SST160A;
324 info->sector_count = 32;
325 info->size = 0x00200000;
328 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF3201:
329 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF3202:
330 info->flash_id += FLASH_SST320;
331 info->sector_count = 64;
332 info->size = 0x00400000;
335 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF6401:
336 case (CONFIG_SYS_FLASH_WORD_SIZE)SST_ID_xF6402:
337 info->flash_id += FLASH_SST640;
338 info->sector_count = 128;
339 info->size = 0x00800000;
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;
386 if (info->flash_id & FLASH_BTYPE) {
387 /* set sector offsets for bottom boot block type */
388 info->start[0] = base + 0x00000000;
389 info->start[1] = base + 0x00004000;
390 info->start[2] = base + 0x00006000;
391 info->start[3] = base + 0x00008000;
392 for (i = 4; i < info->sector_count; i++) {
393 info->start[i] = base + (i * 0x00010000) - 0x00030000;
396 /* set sector offsets for top boot block type */
397 i = info->sector_count - 1;
398 info->start[i--] = base + info->size - 0x00004000;
399 info->start[i--] = base + info->size - 0x00006000;
400 info->start[i--] = base + info->size - 0x00008000;
401 for (; i >= 0; i--) {
402 info->start[i] = base + i * 0x00010000;
407 /* check for protected sectors */
408 for (i = 0; i < info->sector_count; i++) {
409 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
410 /* D0 = 1 if protected */
411 addr2 = (volatile CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[i]);
412 if ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_AMD)
413 info->protect[i] = 0;
415 info->protect[i] = addr2[CONFIG_SYS_FLASH_READ2] & 1;
419 * Prevent writes to uninitialized FLASH.
421 if (info->flash_id != FLASH_UNKNOWN) {
422 addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)info->start[0];
423 *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
430 /*-----------------------------------------------------------------------
433 int flash_erase (flash_info_t *info, int s_first, int s_last)
435 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[0]);
436 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2;
437 int flag, prot, sect, l_sect;
438 ulong start, now, last;
441 if ((s_first < 0) || (s_first > s_last)) {
442 if (info->flash_id == FLASH_UNKNOWN) {
443 printf ("- missing\n");
445 printf ("- no sectors to erase\n");
450 if (info->flash_id == FLASH_UNKNOWN) {
451 printf ("Can't erase unknown flash type - aborted\n");
456 for (sect=s_first; sect<=s_last; ++sect) {
457 if (info->protect[sect]) {
463 printf ("- Warning: %d protected sectors will not be erased!\n",
471 /* Disable interrupts which might cause a timeout here */
472 flag = disable_interrupts();
474 /* Start erase on unprotected sectors */
475 for (sect = s_first; sect<=s_last; sect++) {
476 if (info->protect[sect] == 0) { /* not protected */
477 addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[sect]);
478 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
479 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
480 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
481 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080;
482 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
483 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
484 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00500050; /* block erase */
486 udelay(1000); /* wait 1 ms */
488 if (sect == s_first) {
489 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
490 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
491 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080;
492 addr[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
493 addr[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
495 addr2[0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00300030; /* sector erase */
501 /* re-enable interrupts if necessary */
505 /* wait at least 80us - let's wait 1 ms */
509 * We wait for the last triggered sector
514 start = get_timer (0);
516 addr = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[l_sect]);
517 while ((addr[0] & (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080) != (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080) {
518 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
519 printf ("Timeout\n");
522 /* show that we're waiting */
523 if ((now - last) > 1000) { /* every second */
530 /* reset to read mode */
531 addr = (CONFIG_SYS_FLASH_WORD_SIZE *)info->start[0];
532 addr[0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
538 /*-----------------------------------------------------------------------
539 * Copy memory to flash, returns:
542 * 2 - Flash not erased
545 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
550 wp = (addr & ~3); /* get lower word aligned address */
553 * handle unaligned start bytes
555 if ((l = addr - wp) != 0) {
557 for (i=0, cp=wp; i<l; ++i, ++cp) {
558 data = (data << 8) | (*(uchar *)cp);
560 for (; i<4 && cnt>0; ++i) {
561 data = (data << 8) | *src++;
565 for (; cnt==0 && i<4; ++i, ++cp) {
566 data = (data << 8) | (*(uchar *)cp);
569 if ((rc = write_word(info, wp, data)) != 0) {
576 * handle word aligned part
580 for (i=0; i<4; ++i) {
581 data = (data << 8) | *src++;
583 if ((rc = write_word(info, wp, data)) != 0) {
595 * handle unaligned tail bytes
598 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
599 data = (data << 8) | *src++;
602 for (; i<4; ++i, ++cp) {
603 data = (data << 8) | (*(uchar *)cp);
606 return (write_word(info, wp, data));
609 /*-----------------------------------------------------------------------
610 * Write a word to Flash, returns:
613 * 2 - Flash not erased
615 static int write_word (flash_info_t *info, ulong dest, ulong data)
617 ulong *data_ptr = &data;
618 volatile CONFIG_SYS_FLASH_WORD_SIZE *addr2 = (CONFIG_SYS_FLASH_WORD_SIZE *)(info->start[0]);
619 volatile CONFIG_SYS_FLASH_WORD_SIZE *dest2 = (CONFIG_SYS_FLASH_WORD_SIZE *)dest;
620 volatile CONFIG_SYS_FLASH_WORD_SIZE *data2 = (CONFIG_SYS_FLASH_WORD_SIZE *)data_ptr;
625 /* Check if Flash is (sufficiently) erased */
626 if ((*((vu_long *)dest) & data) != data) {
630 /* Disable interrupts which might cause a timeout here */
631 flag = disable_interrupts();
633 for (i=0; i<4/sizeof(CONFIG_SYS_FLASH_WORD_SIZE); i++)
635 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00AA00AA;
636 addr2[CONFIG_SYS_FLASH_ADDR1] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00550055;
637 addr2[CONFIG_SYS_FLASH_ADDR0] = (CONFIG_SYS_FLASH_WORD_SIZE)0x00A000A0;
641 /* re-enable interrupts if necessary */
645 /* data polling for D7 */
646 start = get_timer (0);
647 while ((dest2[i] & (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080) !=
648 (data2[i] & (CONFIG_SYS_FLASH_WORD_SIZE)0x00800080)) {
649 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
658 /*-----------------------------------------------------------------------