3 * Erik Theisen, Wave 7 Optics, etheisen@mindspring.com.
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * SPDX-License-Identifier: GPL-2.0+
11 #include <asm/ppc4xx.h>
12 #include <asm/processor.h>
16 /* info for FLASH chips */
17 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
22 static ulong flash_get_size(vu_long *addr, flash_info_t *info);
23 static int write_word8(flash_info_t *info, ulong dest, ulong data);
24 static int write_word32(flash_info_t *info, ulong dest, ulong data);
25 static void flash_get_offsets(ulong base, flash_info_t *info);
27 unsigned long flash_init(void)
30 unsigned long size_b0, base_b0;
31 unsigned long size_b1;
33 /* Init: no FLASHes known */
34 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i)
35 flash_info[i].flash_id = FLASH_UNKNOWN;
37 /* Get Size of Boot and Main Flashes */
38 size_b0 = flash_get_size((vu_long *) FLASH_BASE0_PRELIM,
40 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
41 printf("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
42 size_b0, size_b0 << 20);
46 flash_get_size((vu_long *) FLASH_BASE1_PRELIM,
48 if (flash_info[1].flash_id == FLASH_UNKNOWN) {
49 printf("## Unknown FLASH on Bank 1 - Size = 0x%08lx = %ld MB\n",
50 size_b1, size_b1 << 20);
54 /* Calculate base addresses */
57 /* Setup offsets for Boot Flash */
58 flash_get_offsets(base_b0, &flash_info[0]);
60 /* Protect board level data */
61 (void) flash_protect(FLAG_PROTECT_SET,
63 flash_info[0].start[1] - 1, &flash_info[0]);
65 /* Monitor protection ON by default */
66 (void) flash_protect(FLAG_PROTECT_SET,
67 base_b0 + size_b0 - monitor_flash_len,
68 base_b0 + size_b0 - 1, &flash_info[0]);
70 /* Protect the FPGA image */
71 (void) flash_protect(FLAG_PROTECT_SET,
73 FLASH_BASE1_PRELIM + CONFIG_SYS_FPGA_IMAGE_LEN -
76 /* Protect the default boot image */
77 (void) flash_protect(FLAG_PROTECT_SET,
78 FLASH_BASE1_PRELIM + CONFIG_SYS_FPGA_IMAGE_LEN,
79 FLASH_BASE1_PRELIM + CONFIG_SYS_FPGA_IMAGE_LEN +
80 0x600000 - 1, &flash_info[1]);
82 /* Setup offsets for Main Flash */
83 flash_get_offsets(FLASH_BASE1_PRELIM, &flash_info[1]);
85 return size_b0 + size_b1;
88 static void flash_get_offsets(ulong base, flash_info_t *info)
92 /* set up sector start address table - FOR BOOT ROM ONLY!!! */
93 if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) {
94 for (i = 0; i < info->sector_count; i++)
95 info->start[i] = base + (i * 0x00010000);
97 } /* end flash_get_offsets() */
99 void flash_print_info(flash_info_t *info)
105 volatile unsigned long *flash;
107 if (info->flash_id == FLASH_UNKNOWN) {
108 printf("missing or unknown FLASH type\n");
112 switch (info->flash_id & FLASH_VENDMASK) {
119 case FLASH_MAN_INTEL:
120 printf("2 x Intel ");
123 printf("Unknown Vendor ");
126 switch (info->flash_id & FLASH_TYPEMASK) {
128 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_AMD)
129 printf("AM29LV040 (4096 Kbit, uniform sector size)\n");
130 else if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_STM)
131 printf("M29W040B (4096 Kbit, uniform block size)\n");
133 printf("UNKNOWN 29x040x (4096 Kbit, uniform sector size)\n");
135 case FLASH_28F320J3A:
136 printf("28F320J3A (32 Mbit = 128K x 32)\n");
138 case FLASH_28F640J3A:
139 printf("28F640J3A (64 Mbit = 128K x 64)\n");
141 case FLASH_28F128J3A:
142 printf("28F128J3A (128 Mbit = 128K x 128)\n");
145 printf("Unknown Chip Type\n");
148 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_STM) {
149 printf(" Size: %ld KB in %d Blocks\n",
150 info->size >> 10, info->sector_count);
152 printf(" Size: %ld KB in %d Sectors\n",
153 info->size >> 10, info->sector_count);
156 printf(" Sector Start Addresses:");
157 for (i = 0; i < info->sector_count; ++i) {
159 * Check if whole sector is erased
161 if (i != (info->sector_count - 1))
162 size = info->start[i + 1] - info->start[i];
164 size = info->start[0] + info->size - info->start[i];
166 flash = (volatile unsigned long *) info->start[i];
167 size = size >> 2; /* divide by 4 for longword access */
168 for (k = 0; k < size; k++) {
169 if (*flash++ != 0xffffffff) {
180 info->protect[i] ? "RO " : " ");
183 } /* end flash_print_info() */
186 * The following code cannot be run from FLASH!
188 static ulong flash_get_size(vu_long *addr, flash_info_t *info)
191 ulong base = (ulong) addr;
193 /* Setup default type */
194 info->flash_id = FLASH_UNKNOWN;
195 info->sector_count = 0;
198 /* Test for Boot Flash */
199 if (base == FLASH_BASE0_PRELIM) {
201 volatile unsigned char *addr2 = (unsigned char *) addr;
203 /* Write auto select command: read Manufacturer ID */
204 *(addr2 + 0x555) = 0xaa;
205 *(addr2 + 0x2aa) = 0x55;
206 *(addr2 + 0x555) = 0x90;
211 case (unsigned char) AMD_MANUFACT:
212 info->flash_id = FLASH_MAN_AMD;
214 case (unsigned char) STM_MANUFACT:
215 info->flash_id = FLASH_MAN_STM;
218 *addr2 = 0xf0; /* no or unknown flash */
223 value = *(addr2 + 1);
225 case (unsigned char) AMD_ID_LV040B:
226 case (unsigned char) STM_ID_29W040B:
227 info->flash_id += FLASH_AM040;
228 info->sector_count = 8;
229 info->size = 0x00080000;
230 break; /* => 512Kb */
232 *addr2 = 0xf0; /* => no or unknown flash */
235 } else { /* MAIN Flash */
237 volatile unsigned long *addr2 = (unsigned long *) addr;
239 /* Write auto select command: read Manufacturer ID */
245 case (unsigned long) INTEL_MANUFACT:
246 info->flash_id = FLASH_MAN_INTEL;
249 *addr2 = 0xff; /* no or unknown flash */
253 /* Device ID - This shit is interleaved... */
254 value = *(addr2 + 1);
256 case (unsigned long) INTEL_ID_28F320J3A:
257 info->flash_id += FLASH_28F320J3A;
258 info->sector_count = 32;
259 info->size = 0x00400000 * 2;
260 break; /* => 2 X 4 MB */
261 case (unsigned long) INTEL_ID_28F640J3A:
262 info->flash_id += FLASH_28F640J3A;
263 info->sector_count = 64;
264 info->size = 0x00800000 * 2;
265 break; /* => 2 X 8 MB */
266 case (unsigned long) INTEL_ID_28F128J3A:
267 info->flash_id += FLASH_28F128J3A;
268 info->sector_count = 128;
269 info->size = 0x01000000 * 2;
270 break; /* => 2 X 16 MB */
272 *addr2 = 0xff; /* => no or unknown flash */
276 /* Make sure we don't exceed CONFIG_SYS_MAX_FLASH_SECT */
277 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
278 printf("** ERROR: sector count %d > max (%d) **\n",
279 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
280 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
283 /* set up sector start address table */
284 switch (info->flash_id & FLASH_TYPEMASK) {
286 for (i = 0; i < info->sector_count; i++)
287 info->start[i] = base + (i * 0x00010000);
289 case FLASH_28F320J3A:
290 case FLASH_28F640J3A:
291 case FLASH_28F128J3A:
292 for (i = 0; i < info->sector_count; i++)
293 info->start[i] = base +
294 (i * 0x00020000 * 2); /* 2 Banks */
298 /* Test for Boot Flash */
299 if (base == FLASH_BASE0_PRELIM) {
300 volatile unsigned char *addr2;
302 /* check for protected sectors */
303 for (i = 0; i < info->sector_count; i++) {
305 * read sector protection at sector address,
307 * D0 = 1 if protected
309 addr2 = (volatile unsigned char *) (info->start[i]);
310 info->protect[i] = *(addr2 + 2) & 1;
313 /* Restore read mode */
314 *(unsigned char *) base = 0xF0; /* Reset NORMAL Flash */
315 } else { /* Main Flash */
316 volatile unsigned long *addr2;
318 /* check for protected sectors */
319 for (i = 0; i < info->sector_count; i++) {
321 * read sector protection at sector address,
323 * D0 = 1 if protected
325 addr2 = (volatile unsigned long *) (info->start[i]);
326 info->protect[i] = *(addr2 + 2) & 0x1;
329 /* Restore read mode */
330 *(unsigned long *) base = 0xFFFFFFFF; /* Reset Flash */
334 } /* end flash_get_size() */
336 static int wait_for_DQ7(ulong addr, uchar cmp_val, ulong tout)
340 volatile uchar *vaddr = (uchar *) addr;
343 for (i = 1; i <= (100 * tout); i++) { /* Wait up to tout ms */
347 /* Check for completion */
348 if ((vaddr[0] & 0x80) == (cmp_val & 0x80))
351 /* KEEP THE LUSER HAPPY - Print a dot every 1.1 seconds */
355 /* Kick the dog if needed */
360 } /* wait_for_DQ7() */
362 static int flash_erase8(flash_info_t *info, int s_first, int s_last)
364 int tcode, rcode = 0;
365 volatile uchar *addr = (uchar *) (info->start[0]);
366 volatile uchar *sector_addr;
367 int flag, prot, sect;
369 /* Validate arguments */
370 if ((s_first < 0) || (s_first > s_last)) {
371 if (info->flash_id == FLASH_UNKNOWN)
372 printf("- missing\n");
374 printf("- no sectors to erase\n");
378 /* Check for KNOWN flash type */
379 if (info->flash_id == FLASH_UNKNOWN) {
380 printf("Can't erase unknown flash type - aborted\n");
384 /* Check for protected sectors */
386 for (sect = s_first; sect <= s_last; ++sect) {
387 if (info->protect[sect])
391 printf("- Warning: %d protected sectors will not be erased!\n",
397 /* Start erase on unprotected sectors */
398 for (sect = s_first; sect <= s_last; sect++) {
399 if (info->protect[sect] == 0) { /* not protected */
400 sector_addr = (uchar *) (info->start[sect]);
402 if ((info->flash_id & FLASH_VENDMASK) ==
404 printf("Erasing block %p\n", sector_addr);
406 printf("Erasing sector %p\n", sector_addr);
408 /* Disable interrupts which might cause timeout */
409 flag = disable_interrupts();
411 *(addr + 0x555) = (uchar) 0xAA;
412 *(addr + 0x2aa) = (uchar) 0x55;
413 *(addr + 0x555) = (uchar) 0x80;
414 *(addr + 0x555) = (uchar) 0xAA;
415 *(addr + 0x2aa) = (uchar) 0x55;
416 *sector_addr = (uchar) 0x30; /* sector erase */
419 * Wait for each sector to complete, it's more
420 * reliable. According to AMD Spec, you must
421 * issue all erase commands within a specified
422 * timeout. This has been seen to fail, especially
423 * if printf()s are included (for debug)!!
424 * Takes up to 6 seconds.
426 tcode = wait_for_DQ7((ulong) sector_addr, 0x80, 6000);
428 /* re-enable interrupts if necessary */
432 /* Make sure we didn't timeout */
440 /* wait at least 80us - let's wait 1 ms */
443 /* reset to read mode */
444 addr = (uchar *) info->start[0];
445 *addr = (uchar) 0xF0; /* reset bank */
449 } /* end flash_erase8() */
451 static int flash_erase32(flash_info_t *info, int s_first, int s_last)
454 ulong start, now, last;
457 /* Validate arguments */
458 if ((s_first < 0) || (s_first > s_last)) {
459 if (info->flash_id == FLASH_UNKNOWN)
460 printf("- missing\n");
462 printf("- no sectors to erase\n");
466 /* Check for KNOWN flash type */
467 if ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL) {
468 printf("Can erase only Intel flash types - aborted\n");
472 /* Check for protected sectors */
473 for (sect = s_first; sect <= s_last; ++sect) {
474 if (info->protect[sect])
478 printf("- Warning: %d protected sectors will not be erased!\n",
484 start = get_timer(0);
486 /* Start erase on unprotected sectors */
487 for (sect = s_first; sect <= s_last; sect++) {
489 if (info->protect[sect] == 0) { /* not protected */
490 vu_long *addr = (vu_long *) (info->start[sect]);
491 unsigned long status;
493 /* Disable interrupts which might cause a timeout */
494 flag = disable_interrupts();
496 *addr = 0x00500050; /* clear status register */
497 *addr = 0x00200020; /* erase setup */
498 *addr = 0x00D000D0; /* erase confirm */
500 /* re-enable interrupts if necessary */
504 /* Wait at least 80us - let's wait 1 ms */
507 while (((status = *addr) & 0x00800080) != 0x00800080) {
508 now = get_timer(start);
509 if (now > CONFIG_SYS_FLASH_ERASE_TOUT) {
513 /* reset to read mode */
519 * show that we're waiting
522 if ((now - last) > 990) {
527 *addr = 0x00FF00FF; /* reset to read mode */
534 int flash_erase(flash_info_t *info, int s_first, int s_last)
536 if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040)
537 return flash_erase8(info, s_first, s_last);
539 return flash_erase32(info, s_first, s_last);
543 * Copy memory to flash, returns:
546 * 2 - Flash not erased
548 static int write_buff8(flash_info_t *info, uchar *src, ulong addr,
555 start = get_timer(0);
557 wp = (addr & ~3); /* get lower word
561 * handle unaligned start bytes
566 for (i = 0, cp = wp; i < l; ++i, ++cp)
567 data = (data << 8) | (*(uchar *) cp);
569 for (; i < 4 && cnt > 0; ++i) {
570 data = (data << 8) | *src++;
575 for (; cnt == 0 && i < 4; ++i, ++cp)
576 data = (data << 8) | (*(uchar *) cp);
578 rc = write_word8(info, wp, data);
586 * handle word aligned part
590 for (i = 0; i < 4; ++i)
591 data = (data << 8) | *src++;
593 rc = write_word8(info, wp, data);
599 if (get_timer(start) > 1000) { /* every second */
602 start = get_timer(0);
610 * handle unaligned tail bytes
613 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
614 data = (data << 8) | *src++;
618 for (; i < 4; ++i, ++cp)
619 data = (data << 8) | (*(uchar *) cp);
621 return write_word8(info, wp, data);
624 #define FLASH_WIDTH 4 /* flash bus width in bytes */
625 static int write_buff32(flash_info_t *info, uchar *src, ulong addr,
632 start = get_timer(0);
634 if (info->flash_id == FLASH_UNKNOWN)
637 /* get lower FLASH_WIDTH aligned address */
638 wp = (addr & ~(FLASH_WIDTH - 1));
641 * handle unaligned start bytes
643 if ((l = addr - wp) != 0) {
645 for (i = 0, cp = wp; i < l; ++i, ++cp)
646 data = (data << 8) | (*(uchar *) cp);
648 for (; i < FLASH_WIDTH && cnt > 0; ++i) {
649 data = (data << 8) | *src++;
654 for (; cnt == 0 && i < FLASH_WIDTH; ++i, ++cp)
655 data = (data << 8) | (*(uchar *) cp);
657 rc = write_word32(info, wp, data);
665 * handle FLASH_WIDTH aligned part
667 while (cnt >= FLASH_WIDTH) {
669 for (i = 0; i < FLASH_WIDTH; ++i)
670 data = (data << 8) | *src++;
672 rc = write_word32(info, wp, data);
678 if (get_timer(start) > 990) { /* every second */
680 start = get_timer(0);
688 * handle unaligned tail bytes
691 for (i = 0, cp = wp; i < FLASH_WIDTH && cnt > 0; ++i, ++cp) {
692 data = (data << 8) | *src++;
696 for (; i < FLASH_WIDTH; ++i, ++cp)
697 data = (data << 8) | (*(uchar *) cp);
699 return write_word32(info, wp, data);
702 int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
706 if ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040)
707 retval = write_buff8(info, src, addr, cnt);
709 retval = write_buff32(info, src, addr, cnt);
715 * Write a word to Flash, returns:
718 * 2 - Flash not erased
721 static int write_word8(flash_info_t *info, ulong dest, ulong data)
723 volatile uchar *addr2 = (uchar *) (info->start[0]);
724 volatile uchar *dest2 = (uchar *) dest;
725 volatile uchar *data2 = (uchar *) &data;
727 int i, tcode, rcode = 0;
729 /* Check if Flash is (sufficently) erased */
730 if ((*((volatile uchar *)dest) & (uchar)data) != (uchar)data)
733 for (i = 0; i < (4 / sizeof(uchar)); i++) {
734 /* Disable interrupts which might cause a timeout here */
735 flag = disable_interrupts();
737 *(addr2 + 0x555) = (uchar) 0xAA;
738 *(addr2 + 0x2aa) = (uchar) 0x55;
739 *(addr2 + 0x555) = (uchar) 0xA0;
743 /* Wait for write to complete, up to 1ms */
744 tcode = wait_for_DQ7((ulong) &dest2[i], data2[i], 1);
746 /* re-enable interrupts if necessary */
750 /* Make sure we didn't timeout */
758 static int write_word32(flash_info_t *info, ulong dest, ulong data)
760 vu_long *addr = (vu_long *) dest;
765 /* Check if Flash is (sufficiently) erased */
766 if ((*addr & data) != data)
769 /* Disable interrupts which might cause a timeout here */
770 flag = disable_interrupts();
772 *addr = 0x00400040; /* write setup */
775 /* re-enable interrupts if necessary */
779 start = get_timer(0);
781 while (((status = *addr) & 0x00800080) != 0x00800080) {
783 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
784 *addr = 0x00FF00FF; /* restore read mode */
789 *addr = 0x00FF00FF; /* restore read mode */
794 static int _flash_protect(flash_info_t *info, long sector)
800 volatile long *addr = (long *)sector;
802 switch (info->flash_id & FLASH_TYPEMASK) {
803 case FLASH_28F320J3A:
804 case FLASH_28F640J3A:
805 case FLASH_28F128J3A:
806 /* Disable interrupts which might cause Flash to timeout */
807 flag = disable_interrupts();
810 *addr = 0x00500050L; /* Clear the status register */
811 *addr = 0x00600060L; /* Set lock bit setup */
812 *addr = 0x00010001L; /* Set lock bit confirm */
814 /* Wait for command completion */
815 for (i = 0; i < 10; i++) { /* 75us timeout, wait 100us */
817 if ((*addr & 0x00800080L) == 0x00800080L)
821 /* Not successful? */
823 if (status != 0x00800080L) {
824 printf("Protect %x sector failed: %x\n",
825 (uint) sector, (uint) status);
829 /* Restore read mode */
832 /* re-enable interrupts if necessary */
837 case FLASH_AM040: /* No soft sector protection */
841 /* Turn protection on for this sector */
842 for (i = 0; i < info->sector_count; i++) {
843 if (info->start[i] == sector) {
844 info->protect[i] = 1;
852 static int _flash_unprotect(flash_info_t *info, long sector)
858 volatile long *addr = (long *) sector;
860 switch (info->flash_id & FLASH_TYPEMASK) {
861 case FLASH_28F320J3A:
862 case FLASH_28F640J3A:
863 case FLASH_28F128J3A:
864 /* Disable interrupts which might cause Flash to timeout */
865 flag = disable_interrupts();
867 *addr = 0x00500050L; /* Clear the status register */
868 *addr = 0x00600060L; /* Clear lock bit setup */
869 *addr = 0x00D000D0L; /* Clear lock bit confirm */
871 /* Wait for command completion */
872 for (i = 0; i < 80; i++) { /* 700ms timeout, wait 800 */
873 udelay(10000); /* Delay 10ms */
874 if ((*addr & 0x00800080L) == 0x00800080L)
878 /* Not successful? */
880 if (status != 0x00800080L) {
881 printf("Un-protect %x sector failed: %x\n",
882 (uint) sector, (uint) status);
887 /* restore read mode */
890 /* re-enable interrupts if necessary */
895 case FLASH_AM040: /* No soft sector protection */
900 * Fix Intel's little red wagon. Reprotect
901 * sectors that were protected before we undid
902 * protection on a specific sector.
904 for (i = 0; i < info->sector_count; i++) {
905 if (info->start[i] != sector) {
906 if (info->protect[i]) {
907 if (_flash_protect(info, info->start[i]))
910 } else /* Turn protection off for this sector */
911 info->protect[i] = 0;
917 int flash_real_protect(flash_info_t *info, long sector, int prot)
922 rcode = _flash_protect(info, info->start[sector]);
924 rcode = _flash_unprotect(info, info->start[sector]);