3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
10 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
12 /* NOTE - CONFIG_FLASH_16BIT means the CPU interface is 16-bit, it
13 * has nothing to do with the flash chip being 8-bit or 16-bit.
15 #ifdef CONFIG_FLASH_16BIT
16 typedef unsigned short FLASH_PORT_WIDTH;
17 typedef volatile unsigned short FLASH_PORT_WIDTHV;
18 #define FLASH_ID_MASK 0xFFFF
20 typedef unsigned long FLASH_PORT_WIDTH;
21 typedef volatile unsigned long FLASH_PORT_WIDTHV;
22 #define FLASH_ID_MASK 0xFFFFFFFF
25 #define FPW FLASH_PORT_WIDTH
26 #define FPWV FLASH_PORT_WIDTHV
28 #define ORMASK(size) ((-size) & OR_AM_MSK)
30 #define FLASH_CYCLE1 0x0555
31 #define FLASH_CYCLE2 0x02aa
33 /*-----------------------------------------------------------------------
36 static ulong flash_get_size(FPWV *addr, flash_info_t *info);
37 static void flash_reset(flash_info_t *info);
38 static int write_word_intel(flash_info_t *info, FPWV *dest, FPW data);
39 static int write_word_amd(flash_info_t *info, FPWV *dest, FPW data);
40 static void flash_get_offsets(ulong base, flash_info_t *info);
41 static flash_info_t *flash_get_info(ulong base);
43 /*-----------------------------------------------------------------------
46 * sets up flash_info and returns size of FLASH (bytes)
48 unsigned long flash_init (void)
50 unsigned long size = 0;
53 /* Init: no FLASHes known */
54 for (i=0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
56 ulong flashbase = (i == 0) ? PHYS_FLASH_1 : PHYS_FLASH_2;
58 ulong flashbase = CONFIG_SYS_FLASH_BASE;
61 memset(&flash_info[i], 0, sizeof(flash_info_t));
64 flash_get_size((FPW *)flashbase, &flash_info[i]);
66 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
67 printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx\n",
68 i, flash_info[i].size);
71 size += flash_info[i].size;
74 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
75 /* monitor protection ON by default */
76 flash_protect(FLAG_PROTECT_SET,
77 CONFIG_SYS_MONITOR_BASE,
78 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
79 flash_get_info(CONFIG_SYS_MONITOR_BASE));
82 #ifdef CONFIG_ENV_IS_IN_FLASH
83 /* ENV protection ON by default */
84 flash_protect(FLAG_PROTECT_SET,
86 CONFIG_ENV_ADDR+CONFIG_ENV_SIZE-1,
87 flash_get_info(CONFIG_ENV_ADDR));
91 return size ? size : 1;
94 /*-----------------------------------------------------------------------
96 static void flash_reset(flash_info_t *info)
98 FPWV *base = (FPWV *)(info->start[0]);
100 /* Put FLASH back in read mode */
101 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL)
102 *base = (FPW)0x00FF00FF; /* Intel Read Mode */
103 else if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_AMD)
104 *base = (FPW)0x00F000F0; /* AMD Read Mode */
107 /*-----------------------------------------------------------------------
109 static void flash_get_offsets (ulong base, flash_info_t *info)
113 /* set up sector start address table */
114 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL
115 && (info->flash_id & FLASH_BTYPE)) {
116 int bootsect_size; /* number of bytes/boot sector */
117 int sect_size; /* number of bytes/regular sector */
119 bootsect_size = 0x00002000 * (sizeof(FPW)/2);
120 sect_size = 0x00010000 * (sizeof(FPW)/2);
122 /* set sector offsets for bottom boot block type */
123 for (i = 0; i < 8; ++i) {
124 info->start[i] = base + (i * bootsect_size);
126 for (i = 8; i < info->sector_count; i++) {
127 info->start[i] = base + ((i - 7) * sect_size);
130 else if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_AMD
131 && (info->flash_id & FLASH_TYPEMASK) == FLASH_AM640U) {
133 int sect_size; /* number of bytes/sector */
135 sect_size = 0x00010000 * (sizeof(FPW)/2);
137 /* set up sector start address table (uniform sector type) */
138 for( i = 0; i < info->sector_count; i++ )
139 info->start[i] = base + (i * sect_size);
143 /*-----------------------------------------------------------------------
146 static flash_info_t *flash_get_info(ulong base)
151 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i ++) {
152 info = & flash_info[i];
153 if (info->start[0] <= base && base < info->start[0] + info->size)
157 return i == CONFIG_SYS_MAX_FLASH_BANKS ? 0 : info;
160 /*-----------------------------------------------------------------------
163 void flash_print_info (flash_info_t *info)
169 uchar botbootletter[] = "B";
170 uchar topbootletter[] = "T";
171 uchar botboottype[] = "bottom boot sector";
172 uchar topboottype[] = "top boot sector";
174 if (info->flash_id == FLASH_UNKNOWN) {
175 printf ("missing or unknown FLASH type\n");
179 switch (info->flash_id & FLASH_VENDMASK) {
180 case FLASH_MAN_AMD: printf ("AMD "); break;
181 case FLASH_MAN_BM: printf ("BRIGHT MICRO "); break;
182 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
183 case FLASH_MAN_SST: printf ("SST "); break;
184 case FLASH_MAN_STM: printf ("STM "); break;
185 case FLASH_MAN_INTEL: printf ("INTEL "); break;
186 default: printf ("Unknown Vendor "); break;
189 /* check for top or bottom boot, if it applies */
190 if (info->flash_id & FLASH_BTYPE) {
191 boottype = botboottype;
192 bootletter = botbootletter;
195 boottype = topboottype;
196 bootletter = topbootletter;
199 switch (info->flash_id & FLASH_TYPEMASK) {
201 fmt = "29LV641D (64 Mbit, uniform sectors)\n";
203 case FLASH_28F800C3B:
204 case FLASH_28F800C3T:
205 fmt = "28F800C3%s (8 Mbit, %s)\n";
207 case FLASH_INTEL800B:
208 case FLASH_INTEL800T:
209 fmt = "28F800B3%s (8 Mbit, %s)\n";
211 case FLASH_28F160C3B:
212 case FLASH_28F160C3T:
213 fmt = "28F160C3%s (16 Mbit, %s)\n";
215 case FLASH_INTEL160B:
216 case FLASH_INTEL160T:
217 fmt = "28F160B3%s (16 Mbit, %s)\n";
219 case FLASH_28F320C3B:
220 case FLASH_28F320C3T:
221 fmt = "28F320C3%s (32 Mbit, %s)\n";
223 case FLASH_INTEL320B:
224 case FLASH_INTEL320T:
225 fmt = "28F320B3%s (32 Mbit, %s)\n";
227 case FLASH_28F640C3B:
228 case FLASH_28F640C3T:
229 fmt = "28F640C3%s (64 Mbit, %s)\n";
231 case FLASH_INTEL640B:
232 case FLASH_INTEL640T:
233 fmt = "28F640B3%s (64 Mbit, %s)\n";
236 fmt = "Unknown Chip Type\n";
240 printf (fmt, bootletter, boottype);
242 printf (" Size: %ld MB in %d Sectors\n",
246 printf (" Sector Start Addresses:");
248 for (i=0; i<info->sector_count; ++i) {
253 printf (" %08lX%s", info->start[i],
254 info->protect[i] ? " (RO)" : " ");
260 /*-----------------------------------------------------------------------
264 * The following code cannot be run from FLASH!
267 ulong flash_get_size (FPWV *addr, flash_info_t *info)
269 /* Write auto select command: read Manufacturer ID */
271 /* Write auto select command sequence and test FLASH answer */
272 addr[FLASH_CYCLE1] = (FPW)0x00AA00AA; /* for AMD, Intel ignores this */
273 addr[FLASH_CYCLE2] = (FPW)0x00550055; /* for AMD, Intel ignores this */
274 addr[FLASH_CYCLE1] = (FPW)0x00900090; /* selects Intel or AMD */
276 /* The manufacturer codes are only 1 byte, so just use 1 byte.
277 * This works for any bus width and any FLASH device width.
280 switch (addr[0] & 0xff) {
282 case (uchar)AMD_MANUFACT:
283 info->flash_id = FLASH_MAN_AMD;
286 case (uchar)INTEL_MANUFACT:
287 info->flash_id = FLASH_MAN_INTEL;
291 info->flash_id = FLASH_UNKNOWN;
292 info->sector_count = 0;
297 /* Check 16 bits or 32 bits of ID so work on 32 or 16 bit bus. */
298 if (info->flash_id != FLASH_UNKNOWN) switch (addr[1]) {
300 case (FPW)AMD_ID_LV640U: /* 29LV640 and 29LV641 have same ID */
301 info->flash_id += FLASH_AM640U;
302 info->sector_count = 128;
303 info->size = 0x00800000 * (sizeof(FPW)/2);
304 break; /* => 8 or 16 MB */
306 case (FPW)INTEL_ID_28F800C3B:
307 info->flash_id += FLASH_28F800C3B;
308 info->sector_count = 23;
309 info->size = 0x00100000 * (sizeof(FPW)/2);
310 break; /* => 1 or 2 MB */
312 case (FPW)INTEL_ID_28F800B3B:
313 info->flash_id += FLASH_INTEL800B;
314 info->sector_count = 23;
315 info->size = 0x00100000 * (sizeof(FPW)/2);
316 break; /* => 1 or 2 MB */
318 case (FPW)INTEL_ID_28F160C3B:
319 info->flash_id += FLASH_28F160C3B;
320 info->sector_count = 39;
321 info->size = 0x00200000 * (sizeof(FPW)/2);
322 break; /* => 2 or 4 MB */
324 case (FPW)INTEL_ID_28F160B3B:
325 info->flash_id += FLASH_INTEL160B;
326 info->sector_count = 39;
327 info->size = 0x00200000 * (sizeof(FPW)/2);
328 break; /* => 2 or 4 MB */
330 case (FPW)INTEL_ID_28F320C3B:
331 info->flash_id += FLASH_28F320C3B;
332 info->sector_count = 71;
333 info->size = 0x00400000 * (sizeof(FPW)/2);
334 break; /* => 4 or 8 MB */
336 case (FPW)INTEL_ID_28F320B3B:
337 info->flash_id += FLASH_INTEL320B;
338 info->sector_count = 71;
339 info->size = 0x00400000 * (sizeof(FPW)/2);
340 break; /* => 4 or 8 MB */
342 case (FPW)INTEL_ID_28F640C3B:
343 info->flash_id += FLASH_28F640C3B;
344 info->sector_count = 135;
345 info->size = 0x00800000 * (sizeof(FPW)/2);
346 break; /* => 8 or 16 MB */
348 case (FPW)INTEL_ID_28F640B3B:
349 info->flash_id += FLASH_INTEL640B;
350 info->sector_count = 135;
351 info->size = 0x00800000 * (sizeof(FPW)/2);
352 break; /* => 8 or 16 MB */
355 info->flash_id = FLASH_UNKNOWN;
356 info->sector_count = 0;
358 return (0); /* => no or unknown flash */
361 flash_get_offsets((ulong)addr, info);
363 /* Put FLASH back in read mode */
369 /*-----------------------------------------------------------------------
372 int flash_erase (flash_info_t *info, int s_first, int s_last)
375 int flag, prot, sect;
376 int intel = (info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL;
377 ulong start, now, last;
380 if ((s_first < 0) || (s_first > s_last)) {
381 if (info->flash_id == FLASH_UNKNOWN) {
382 printf ("- missing\n");
384 printf ("- no sectors to erase\n");
389 switch (info->flash_id & FLASH_TYPEMASK) {
390 case FLASH_INTEL800B:
391 case FLASH_INTEL160B:
392 case FLASH_INTEL320B:
393 case FLASH_INTEL640B:
394 case FLASH_28F800C3B:
395 case FLASH_28F160C3B:
396 case FLASH_28F320C3B:
397 case FLASH_28F640C3B:
402 printf ("Can't erase unknown flash type %08lx - aborted\n",
408 for (sect=s_first; sect<=s_last; ++sect) {
409 if (info->protect[sect]) {
415 printf ("- Warning: %d protected sectors will not be erased!\n",
423 /* Start erase on unprotected sectors */
424 for (sect = s_first; sect<=s_last && rcode == 0; sect++) {
426 if (info->protect[sect] != 0) /* protected, skip it */
429 /* Disable interrupts which might cause a timeout here */
430 flag = disable_interrupts();
432 addr = (FPWV *)(info->start[sect]);
434 *addr = (FPW)0x00500050; /* clear status register */
435 *addr = (FPW)0x00200020; /* erase setup */
436 *addr = (FPW)0x00D000D0; /* erase confirm */
439 /* must be AMD style if not Intel */
440 FPWV *base; /* first address in bank */
442 base = (FPWV *)(info->start[0]);
443 base[FLASH_CYCLE1] = (FPW)0x00AA00AA; /* unlock */
444 base[FLASH_CYCLE2] = (FPW)0x00550055; /* unlock */
445 base[FLASH_CYCLE1] = (FPW)0x00800080; /* erase mode */
446 base[FLASH_CYCLE1] = (FPW)0x00AA00AA; /* unlock */
447 base[FLASH_CYCLE2] = (FPW)0x00550055; /* unlock */
448 *addr = (FPW)0x00300030; /* erase sector */
451 /* re-enable interrupts if necessary */
455 start = get_timer(0);
457 /* wait at least 50us for AMD, 80us for Intel.
462 while ((*addr & (FPW)0x00800080) != (FPW)0x00800080) {
463 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
464 printf ("Timeout\n");
468 *addr = (FPW)0x00B000B0;
471 flash_reset(info); /* reset to read mode */
472 rcode = 1; /* failed */
476 /* show that we're waiting */
477 if ((get_timer(last)) > CONFIG_SYS_HZ) {/* every second */
483 /* show that we're waiting */
484 if ((get_timer(last)) > CONFIG_SYS_HZ) { /* every second */
489 flash_reset(info); /* reset to read mode */
496 /*-----------------------------------------------------------------------
497 * Copy memory to flash, returns:
500 * 2 - Flash not erased
502 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
504 FPW data = 0; /* 16 or 32 bit word, matches flash bus width on MPC8XX */
505 int bytes; /* number of bytes to program in current word */
506 int left; /* number of bytes left to program */
509 for (left = cnt, res = 0;
510 left > 0 && res == 0;
511 addr += sizeof(data), left -= sizeof(data) - bytes) {
513 bytes = addr & (sizeof(data) - 1);
514 addr &= ~(sizeof(data) - 1);
516 /* combine source and destination data so can program
517 * an entire word of 16 or 32 bits
519 for (i = 0; i < sizeof(data); i++) {
521 if (i < bytes || i - bytes >= left )
522 data += *((uchar *)addr + i);
527 /* write one word to the flash */
528 switch (info->flash_id & FLASH_VENDMASK) {
530 res = write_word_amd(info, (FPWV *)addr, data);
532 case FLASH_MAN_INTEL:
533 res = write_word_intel(info, (FPWV *)addr, data);
536 /* unknown flash type, error! */
537 printf ("missing or unknown FLASH type\n");
538 res = 1; /* not really a timeout, but gives error */
546 /*-----------------------------------------------------------------------
547 * Write a word to Flash for AMD FLASH
548 * A word is 16 or 32 bits, whichever the bus width of the flash bank
549 * (not an individual chip) is.
554 * 2 - Flash not erased
556 static int write_word_amd (flash_info_t *info, FPWV *dest, FPW data)
560 int res = 0; /* result, assume success */
561 FPWV *base; /* first address in flash bank */
563 /* Check if Flash is (sufficiently) erased */
564 if ((*dest & data) != data) {
569 base = (FPWV *)(info->start[0]);
571 /* Disable interrupts which might cause a timeout here */
572 flag = disable_interrupts();
574 base[FLASH_CYCLE1] = (FPW)0x00AA00AA; /* unlock */
575 base[FLASH_CYCLE2] = (FPW)0x00550055; /* unlock */
576 base[FLASH_CYCLE1] = (FPW)0x00A000A0; /* selects program mode */
578 *dest = data; /* start programming the data */
580 /* re-enable interrupts if necessary */
584 start = get_timer (0);
586 /* data polling for D7 */
587 while (res == 0 && (*dest & (FPW)0x00800080) != (data & (FPW)0x00800080)) {
588 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
589 *dest = (FPW)0x00F000F0; /* reset bank */
597 /*-----------------------------------------------------------------------
598 * Write a word to Flash for Intel FLASH
599 * A word is 16 or 32 bits, whichever the bus width of the flash bank
600 * (not an individual chip) is.
605 * 2 - Flash not erased
607 static int write_word_intel (flash_info_t *info, FPWV *dest, FPW data)
611 int res = 0; /* result, assume success */
613 /* Check if Flash is (sufficiently) erased */
614 if ((*dest & data) != data) {
618 /* Disable interrupts which might cause a timeout here */
619 flag = disable_interrupts();
621 *dest = (FPW)0x00500050; /* clear status register */
622 *dest = (FPW)0x00FF00FF; /* make sure in read mode */
623 *dest = (FPW)0x00400040; /* program setup */
625 *dest = data; /* start programming the data */
627 /* re-enable interrupts if necessary */
631 start = get_timer (0);
633 while (res == 0 && (*dest & (FPW)0x00800080) != (FPW)0x00800080) {
634 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
635 *dest = (FPW)0x00B000B0; /* Suspend program */
640 if (res == 0 && (*dest & (FPW)0x00100010))
641 res = 1; /* write failed, time out error is close enough */
643 *dest = (FPW)0x00500050; /* clear status register */
644 *dest = (FPW)0x00FF00FF; /* make sure in read mode */