3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
10 /* environment.h defines the various CONFIG_ENV_... values in terms
11 * of whichever ones are given in the configuration file.
13 #include <environment.h>
15 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
17 /* NOTE - CONFIG_FLASH_16BIT means the CPU interface is 16-bit, it
18 * has nothing to do with the flash chip being 8-bit or 16-bit.
20 #ifdef CONFIG_FLASH_16BIT
21 typedef unsigned short FLASH_PORT_WIDTH;
22 typedef volatile unsigned short FLASH_PORT_WIDTHV;
23 #define FLASH_ID_MASK 0xFFFF
25 typedef unsigned long FLASH_PORT_WIDTH;
26 typedef volatile unsigned long FLASH_PORT_WIDTHV;
27 #define FLASH_ID_MASK 0xFFFFFFFF
30 #define FPW FLASH_PORT_WIDTH
31 #define FPWV FLASH_PORT_WIDTHV
33 #define ORMASK(size) ((-size) & OR_AM_MSK)
35 /*-----------------------------------------------------------------------
38 static ulong flash_get_size(FPWV *addr, flash_info_t *info);
39 static void flash_reset(flash_info_t *info);
40 static int write_word_intel(flash_info_t *info, FPWV *dest, FPW data);
41 static int write_word_amd(flash_info_t *info, FPWV *dest, FPW data);
42 static void flash_get_offsets(ulong base, flash_info_t *info);
43 #ifdef CONFIG_SYS_FLASH_PROTECTION
44 static void flash_sync_real_protect(flash_info_t *info);
47 /*-----------------------------------------------------------------------
50 * sets up flash_info and returns size of FLASH (bytes)
52 unsigned long flash_init (void)
54 volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
55 volatile memctl8xx_t *memctl = &immap->im_memctl;
59 /* Init: no FLASHes known */
60 for (i=0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
61 flash_info[i].flash_id = FLASH_UNKNOWN;
64 size_b = flash_get_size((FPW *)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
66 flash_info[0].size = size_b;
68 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
69 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx\n",size_b);
72 /* Remap FLASH according to real size, so only at proper address */
73 memctl->memc_or0 = (memctl->memc_or0 & ~OR_AM_MSK) | ORMASK(size_b);
75 /* Do this again (was done already in flast_get_size), just
76 * in case we move it when remap the FLASH.
78 flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
80 #ifdef CONFIG_SYS_FLASH_PROTECTION
81 /* read the hardware protection status (if any) into the
82 * protection array in flash_info.
84 flash_sync_real_protect(&flash_info[0]);
87 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
88 /* monitor protection ON by default */
89 flash_protect(FLAG_PROTECT_SET,
90 CONFIG_SYS_MONITOR_BASE,
91 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
95 #ifdef CONFIG_ENV_ADDR
96 flash_protect ( FLAG_PROTECT_SET,
98 CONFIG_ENV_ADDR + CONFIG_ENV_SECT_SIZE - 1, &flash_info[0]);
101 #ifdef CONFIG_ENV_ADDR_REDUND
102 flash_protect ( FLAG_PROTECT_SET,
103 CONFIG_ENV_ADDR_REDUND,
104 CONFIG_ENV_ADDR_REDUND + CONFIG_ENV_SECT_SIZE - 1,
111 /*-----------------------------------------------------------------------
113 static void flash_reset(flash_info_t *info)
115 FPWV *base = (FPWV *)(info->start[0]);
117 /* Put FLASH back in read mode */
118 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL)
119 *base = (FPW)0x00FF00FF; /* Intel Read Mode */
120 else if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_AMD)
121 *base = (FPW)0x00F000F0; /* AMD Read Mode */
124 /*-----------------------------------------------------------------------
126 static void flash_get_offsets (ulong base, flash_info_t *info)
130 /* set up sector start address table */
131 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL
132 && (info->flash_id & FLASH_BTYPE)) {
133 int bootsect_size; /* number of bytes/boot sector */
134 int sect_size; /* number of bytes/regular sector */
136 bootsect_size = 0x00002000 * (sizeof(FPW)/2);
137 sect_size = 0x00010000 * (sizeof(FPW)/2);
139 /* set sector offsets for bottom boot block type */
140 for (i = 0; i < 8; ++i) {
141 info->start[i] = base + (i * bootsect_size);
143 for (i = 8; i < info->sector_count; i++) {
144 info->start[i] = base + ((i - 7) * sect_size);
147 else if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_AMD
148 && (info->flash_id & FLASH_TYPEMASK) == FLASH_AM640U) {
150 int sect_size; /* number of bytes/sector */
152 sect_size = 0x00010000 * (sizeof(FPW)/2);
154 /* set up sector start address table (uniform sector type) */
155 for( i = 0; i < info->sector_count; i++ )
156 info->start[i] = base + (i * sect_size);
158 else if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_AMD
159 && (info->flash_id & FLASH_TYPEMASK) == FLASH_AM800T) {
161 int sect_size; /* number of bytes/sector */
163 sect_size = 0x00010000 * (sizeof(FPW)/2);
165 /* set up sector start address table (top boot sector type) */
166 for (i = 0; i < info->sector_count - 3; i++)
167 info->start[i] = base + (i * sect_size);
168 i = info->sector_count - 1;
169 info->start[i--] = base + (info->size - 0x00004000) * (sizeof(FPW)/2);
170 info->start[i--] = base + (info->size - 0x00006000) * (sizeof(FPW)/2);
171 info->start[i--] = base + (info->size - 0x00008000) * (sizeof(FPW)/2);
175 /*-----------------------------------------------------------------------
178 void flash_print_info (flash_info_t *info)
184 uchar botbootletter[] = "B";
185 uchar topbootletter[] = "T";
186 uchar botboottype[] = "bottom boot sector";
187 uchar topboottype[] = "top boot sector";
189 if (info->flash_id == FLASH_UNKNOWN) {
190 printf ("missing or unknown FLASH type\n");
194 switch (info->flash_id & FLASH_VENDMASK) {
195 case FLASH_MAN_AMD: printf ("AMD "); break;
196 case FLASH_MAN_BM: printf ("BRIGHT MICRO "); break;
197 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
198 case FLASH_MAN_SST: printf ("SST "); break;
199 case FLASH_MAN_STM: printf ("STM "); break;
200 case FLASH_MAN_INTEL: printf ("INTEL "); break;
201 default: printf ("Unknown Vendor "); break;
204 /* check for top or bottom boot, if it applies */
205 if (info->flash_id & FLASH_BTYPE) {
206 boottype = botboottype;
207 bootletter = botbootletter;
210 boottype = topboottype;
211 bootletter = topbootletter;
214 switch (info->flash_id & FLASH_TYPEMASK) {
216 fmt = "29LV800B%s (8 Mbit, %s)\n";
219 fmt = "29LV641D (64 Mbit, uniform sectors)\n";
221 case FLASH_28F800C3B:
222 case FLASH_28F800C3T:
223 fmt = "28F800C3%s (8 Mbit, %s)\n";
225 case FLASH_INTEL800B:
226 case FLASH_INTEL800T:
227 fmt = "28F800B3%s (8 Mbit, %s)\n";
229 case FLASH_28F160C3B:
230 case FLASH_28F160C3T:
231 fmt = "28F160C3%s (16 Mbit, %s)\n";
233 case FLASH_INTEL160B:
234 case FLASH_INTEL160T:
235 fmt = "28F160B3%s (16 Mbit, %s)\n";
237 case FLASH_28F320C3B:
238 case FLASH_28F320C3T:
239 fmt = "28F320C3%s (32 Mbit, %s)\n";
241 case FLASH_INTEL320B:
242 case FLASH_INTEL320T:
243 fmt = "28F320B3%s (32 Mbit, %s)\n";
245 case FLASH_28F640C3B:
246 case FLASH_28F640C3T:
247 fmt = "28F640C3%s (64 Mbit, %s)\n";
249 case FLASH_INTEL640B:
250 case FLASH_INTEL640T:
251 fmt = "28F640B3%s (64 Mbit, %s)\n";
254 fmt = "Unknown Chip Type\n";
258 printf (fmt, bootletter, boottype);
260 printf (" Size: %ld MB in %d Sectors\n",
264 printf (" Sector Start Addresses:");
266 for (i=0; i<info->sector_count; ++i) {
271 printf (" %08lX%s", info->start[i],
272 info->protect[i] ? " (RO)" : " ");
278 /*-----------------------------------------------------------------------
282 * The following code cannot be run from FLASH!
285 ulong flash_get_size (FPWV *addr, flash_info_t *info)
287 /* Write auto select command: read Manufacturer ID */
289 /* Write auto select command sequence and test FLASH answer */
290 addr[0x0555] = (FPW)0x00AA00AA; /* for AMD, Intel ignores this */
291 addr[0x02AA] = (FPW)0x00550055; /* for AMD, Intel ignores this */
292 addr[0x0555] = (FPW)0x00900090; /* selects Intel or AMD */
294 /* The manufacturer codes are only 1 byte, so just use 1 byte.
295 * This works for any bus width and any FLASH device width.
297 switch (addr[0] & 0xff) {
299 case (uchar)AMD_MANUFACT:
300 info->flash_id = FLASH_MAN_AMD;
303 case (uchar)INTEL_MANUFACT:
304 info->flash_id = FLASH_MAN_INTEL;
308 info->flash_id = FLASH_UNKNOWN;
309 info->sector_count = 0;
314 /* Check 16 bits or 32 bits of ID so work on 32 or 16 bit bus. */
315 if (info->flash_id != FLASH_UNKNOWN) switch (addr[1]) {
317 case (FPW)AMD_ID_LV800T:
318 info->flash_id += FLASH_AM800T;
319 info->sector_count = 19;
320 info->size = 0x00100000 * (sizeof(FPW)/2);
321 break; /* => 1 or 2 MiB */
323 case (FPW)AMD_ID_LV640U: /* 29LV640 and 29LV641 have same ID */
324 info->flash_id += FLASH_AM640U;
325 info->sector_count = 128;
326 info->size = 0x00800000 * (sizeof(FPW)/2);
327 break; /* => 8 or 16 MB */
329 case (FPW)INTEL_ID_28F800C3B:
330 info->flash_id += FLASH_28F800C3B;
331 info->sector_count = 23;
332 info->size = 0x00100000 * (sizeof(FPW)/2);
333 break; /* => 1 or 2 MB */
335 case (FPW)INTEL_ID_28F800B3B:
336 info->flash_id += FLASH_INTEL800B;
337 info->sector_count = 23;
338 info->size = 0x00100000 * (sizeof(FPW)/2);
339 break; /* => 1 or 2 MB */
341 case (FPW)INTEL_ID_28F160C3B:
342 info->flash_id += FLASH_28F160C3B;
343 info->sector_count = 39;
344 info->size = 0x00200000 * (sizeof(FPW)/2);
345 break; /* => 2 or 4 MB */
347 case (FPW)INTEL_ID_28F160B3B:
348 info->flash_id += FLASH_INTEL160B;
349 info->sector_count = 39;
350 info->size = 0x00200000 * (sizeof(FPW)/2);
351 break; /* => 2 or 4 MB */
353 case (FPW)INTEL_ID_28F320C3B:
354 info->flash_id += FLASH_28F320C3B;
355 info->sector_count = 71;
356 info->size = 0x00400000 * (sizeof(FPW)/2);
357 break; /* => 4 or 8 MB */
359 case (FPW)INTEL_ID_28F320B3B:
360 info->flash_id += FLASH_INTEL320B;
361 info->sector_count = 71;
362 info->size = 0x00400000 * (sizeof(FPW)/2);
363 break; /* => 4 or 8 MB */
365 case (FPW)INTEL_ID_28F640C3B:
366 info->flash_id += FLASH_28F640C3B;
367 info->sector_count = 135;
368 info->size = 0x00800000 * (sizeof(FPW)/2);
369 break; /* => 8 or 16 MB */
371 case (FPW)INTEL_ID_28F640B3B:
372 info->flash_id += FLASH_INTEL640B;
373 info->sector_count = 135;
374 info->size = 0x00800000 * (sizeof(FPW)/2);
375 break; /* => 8 or 16 MB */
378 info->flash_id = FLASH_UNKNOWN;
379 info->sector_count = 0;
381 return (0); /* => no or unknown flash */
384 flash_get_offsets((ulong)addr, info);
386 /* Put FLASH back in read mode */
392 #ifdef CONFIG_SYS_FLASH_PROTECTION
393 /*-----------------------------------------------------------------------
396 static void flash_sync_real_protect(flash_info_t *info)
398 FPWV *addr = (FPWV *)(info->start[0]);
402 switch (info->flash_id & FLASH_TYPEMASK) {
403 case FLASH_28F800C3B:
404 case FLASH_28F800C3T:
405 case FLASH_28F160C3B:
406 case FLASH_28F160C3T:
407 case FLASH_28F320C3B:
408 case FLASH_28F320C3T:
409 case FLASH_28F640C3B:
410 case FLASH_28F640C3T:
411 /* check for protected sectors */
412 *addr = (FPW)0x00900090;
413 for (i = 0; i < info->sector_count; i++) {
414 /* read sector protection at sector address, (A7 .. A0) = 0x02.
415 * D0 = 1 for each device if protected.
416 * If at least one device is protected the sector is marked
417 * protected, but mixed protected and unprotected devices
418 * within a sector should never happen.
420 sect = (FPWV *)(info->start[i]);
421 info->protect[i] = (sect[2] & (FPW)(0x00010001)) ? 1 : 0;
424 /* Put FLASH back in read mode */
431 /* no hardware protect that we support */
437 /*-----------------------------------------------------------------------
440 int flash_erase (flash_info_t *info, int s_first, int s_last)
443 int flag, prot, sect;
444 int intel = (info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL;
445 ulong start, now, last;
448 if ((s_first < 0) || (s_first > s_last)) {
449 if (info->flash_id == FLASH_UNKNOWN) {
450 printf ("- missing\n");
452 printf ("- no sectors to erase\n");
457 switch (info->flash_id & FLASH_TYPEMASK) {
458 case FLASH_INTEL800B:
459 case FLASH_INTEL160B:
460 case FLASH_INTEL320B:
461 case FLASH_INTEL640B:
462 case FLASH_28F800C3B:
463 case FLASH_28F160C3B:
464 case FLASH_28F320C3B:
465 case FLASH_28F640C3B:
471 printf ("Can't erase unknown flash type %08lx - aborted\n",
477 for (sect=s_first; sect<=s_last; ++sect) {
478 if (info->protect[sect]) {
484 printf ("- Warning: %d protected sectors will not be erased!\n",
490 start = get_timer(0);
493 /* Start erase on unprotected sectors */
494 for (sect = s_first; sect<=s_last && rcode == 0; sect++) {
496 if (info->protect[sect] != 0) /* protected, skip it */
499 /* Disable interrupts which might cause a timeout here */
500 flag = disable_interrupts();
502 addr = (FPWV *)(info->start[sect]);
504 *addr = (FPW)0x00500050; /* clear status register */
505 *addr = (FPW)0x00200020; /* erase setup */
506 *addr = (FPW)0x00D000D0; /* erase confirm */
509 /* must be AMD style if not Intel */
510 FPWV *base; /* first address in bank */
512 base = (FPWV *)(info->start[0]);
513 base[0x0555] = (FPW)0x00AA00AA; /* unlock */
514 base[0x02AA] = (FPW)0x00550055; /* unlock */
515 base[0x0555] = (FPW)0x00800080; /* erase mode */
516 base[0x0555] = (FPW)0x00AA00AA; /* unlock */
517 base[0x02AA] = (FPW)0x00550055; /* unlock */
518 *addr = (FPW)0x00300030; /* erase sector */
521 /* re-enable interrupts if necessary */
525 /* wait at least 50us for AMD, 80us for Intel.
530 while ((*addr & (FPW)0x00800080) != (FPW)0x00800080) {
531 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
532 printf ("Timeout\n");
536 *addr = (FPW)0x00B000B0;
539 flash_reset(info); /* reset to read mode */
540 rcode = 1; /* failed */
544 /* show that we're waiting */
545 if ((now - last) > 1000) { /* every second */
551 flash_reset(info); /* reset to read mode */
558 /*-----------------------------------------------------------------------
559 * Copy memory to flash, returns:
562 * 2 - Flash not erased
564 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
566 FPW data = 0; /* 16 or 32 bit word, matches flash bus width on MPC8XX */
567 int bytes; /* number of bytes to program in current word */
568 int left; /* number of bytes left to program */
571 for (left = cnt, res = 0;
572 left > 0 && res == 0;
573 addr += sizeof(data), left -= sizeof(data) - bytes) {
575 bytes = addr & (sizeof(data) - 1);
576 addr &= ~(sizeof(data) - 1);
578 /* combine source and destination data so can program
579 * an entire word of 16 or 32 bits
581 for (i = 0; i < sizeof(data); i++) {
583 if (i < bytes || i - bytes >= left )
584 data += *((uchar *)addr + i);
589 /* write one word to the flash */
590 switch (info->flash_id & FLASH_VENDMASK) {
592 res = write_word_amd(info, (FPWV *)addr, data);
594 case FLASH_MAN_INTEL:
595 res = write_word_intel(info, (FPWV *)addr, data);
598 /* unknown flash type, error! */
599 printf ("missing or unknown FLASH type\n");
600 res = 1; /* not really a timeout, but gives error */
608 /*-----------------------------------------------------------------------
609 * Write a word to Flash for AMD FLASH
610 * A word is 16 or 32 bits, whichever the bus width of the flash bank
611 * (not an individual chip) is.
616 * 2 - Flash not erased
618 static int write_word_amd (flash_info_t *info, FPWV *dest, FPW data)
622 int res = 0; /* result, assume success */
623 FPWV *base; /* first address in flash bank */
625 /* Check if Flash is (sufficiently) erased */
626 if ((*dest & data) != data) {
631 base = (FPWV *)(info->start[0]);
633 /* Disable interrupts which might cause a timeout here */
634 flag = disable_interrupts();
636 base[0x0555] = (FPW)0x00AA00AA; /* unlock */
637 base[0x02AA] = (FPW)0x00550055; /* unlock */
638 base[0x0555] = (FPW)0x00A000A0; /* selects program mode */
640 *dest = data; /* start programming the data */
642 /* re-enable interrupts if necessary */
646 start = get_timer (0);
648 /* data polling for D7 */
649 while (res == 0 && (*dest & (FPW)0x00800080) != (data & (FPW)0x00800080)) {
650 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
651 *dest = (FPW)0x00F000F0; /* reset bank */
659 /*-----------------------------------------------------------------------
660 * Write a word to Flash for Intel FLASH
661 * A word is 16 or 32 bits, whichever the bus width of the flash bank
662 * (not an individual chip) is.
667 * 2 - Flash not erased
669 static int write_word_intel (flash_info_t *info, FPWV *dest, FPW data)
673 int res = 0; /* result, assume success */
675 /* Check if Flash is (sufficiently) erased */
676 if ((*dest & data) != data) {
680 /* Disable interrupts which might cause a timeout here */
681 flag = disable_interrupts();
683 *dest = (FPW)0x00500050; /* clear status register */
684 *dest = (FPW)0x00FF00FF; /* make sure in read mode */
685 *dest = (FPW)0x00400040; /* program setup */
687 *dest = data; /* start programming the data */
689 /* re-enable interrupts if necessary */
693 start = get_timer (0);
695 while (res == 0 && (*dest & (FPW)0x00800080) != (FPW)0x00800080) {
696 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
697 *dest = (FPW)0x00B000B0; /* Suspend program */
702 if (res == 0 && (*dest & (FPW)0x00100010))
703 res = 1; /* write failed, time out error is close enough */
705 *dest = (FPW)0x00500050; /* clear status register */
706 *dest = (FPW)0x00FF00FF; /* make sure in read mode */
711 #ifdef CONFIG_SYS_FLASH_PROTECTION
712 /*-----------------------------------------------------------------------
714 int flash_real_protect (flash_info_t * info, long sector, int prot)
716 int rcode = 0; /* assume success */
717 FPWV *addr; /* address of sector */
720 addr = (FPWV *) (info->start[sector]);
722 switch (info->flash_id & FLASH_TYPEMASK) {
723 case FLASH_28F800C3B:
724 case FLASH_28F800C3T:
725 case FLASH_28F160C3B:
726 case FLASH_28F160C3T:
727 case FLASH_28F320C3B:
728 case FLASH_28F320C3T:
729 case FLASH_28F640C3B:
730 case FLASH_28F640C3T:
731 flash_reset (info); /* make sure in read mode */
732 *addr = (FPW) 0x00600060L; /* lock command setup */
734 *addr = (FPW) 0x00010001L; /* lock sector */
736 *addr = (FPW) 0x00D000D0L; /* unlock sector */
737 flash_reset (info); /* reset to read mode */
739 /* now see if it really is locked/unlocked as requested */
740 *addr = (FPW) 0x00900090;
741 /* read sector protection at sector address, (A7 .. A0) = 0x02.
742 * D0 = 1 for each device if protected.
743 * If at least one device is protected the sector is marked
744 * protected, but return failure. Mixed protected and
745 * unprotected devices within a sector should never happen.
747 value = addr[2] & (FPW) 0x00010001;
749 info->protect[sector] = 0;
750 else if (value == (FPW) 0x00010001)
751 info->protect[sector] = 1;
753 /* error, mixed protected and unprotected */
755 info->protect[sector] = 1;
757 if (info->protect[sector] != prot)
758 rcode = 1; /* failed to protect/unprotect as requested */
760 /* reload all protection bits from hardware for now */
761 flash_sync_real_protect (info);
767 /* no hardware protect that we support */
768 info->protect[sector] = prot;