5 * SPDX-License-Identifier: GPL-2.0+
10 #ifndef CONFIG_FLASH_CFI_DRIVER
11 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
13 /* NOTE - CONFIG_FLASH_16BIT means the CPU interface is 16-bit, it
14 * has nothing to do with the flash chip being 8-bit or 16-bit.
16 #ifdef CONFIG_FLASH_16BIT
17 typedef unsigned short FLASH_PORT_WIDTH;
18 typedef volatile unsigned short FLASH_PORT_WIDTHV;
19 #define FLASH_ID_MASK 0xFFFF
21 typedef unsigned char FLASH_PORT_WIDTH;
22 typedef volatile unsigned char FLASH_PORT_WIDTHV;
23 #define FLASH_ID_MASK 0xFF
26 #define FPW FLASH_PORT_WIDTH
27 #define FPWV FLASH_PORT_WIDTHV
29 #define ORMASK(size) ((-size) & OR_AM_MSK)
31 #define FLASH_CYCLE1 0x0555
32 #define FLASH_CYCLE2 0x02aa
34 /*-----------------------------------------------------------------------
37 static ulong flash_get_size(FPWV *addr, flash_info_t *info);
38 static void flash_reset(flash_info_t *info);
39 static int write_word_amd(flash_info_t *info, FPWV *dest, FPW data);
40 static flash_info_t *flash_get_info(ulong base);
42 /*-----------------------------------------------------------------------
45 * sets up flash_info and returns size of FLASH (bytes)
47 unsigned long flash_init (void)
49 unsigned long size = 0;
51 extern void flash_preinit(void);
52 extern void flash_afterinit(ulong);
53 ulong flashbase = CONFIG_SYS_FLASH_BASE;
57 /* Init: no FLASHes known */
58 for (i=0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
59 memset(&flash_info[i], 0, sizeof(flash_info_t));
62 flash_get_size((FPW *)flashbase, &flash_info[i]);
64 size += flash_info[i].size;
65 flashbase += 0x800000;
67 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
68 /* monitor protection ON by default */
69 flash_protect(FLAG_PROTECT_SET,
70 CONFIG_SYS_MONITOR_BASE,
71 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
72 flash_get_info(CONFIG_SYS_MONITOR_BASE));
75 #ifdef CONFIG_ENV_IS_IN_FLASH
76 /* ENV protection ON by default */
77 flash_protect(FLAG_PROTECT_SET,
79 CONFIG_ENV_ADDR+CONFIG_ENV_SIZE-1,
80 flash_get_info(CONFIG_ENV_ADDR));
84 flash_afterinit(size);
85 return size ? size : 1;
88 /*-----------------------------------------------------------------------
90 static void flash_reset(flash_info_t *info)
92 FPWV *base = (FPWV *)(info->start[0]);
94 /* Put FLASH back in read mode */
95 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL)
96 *base = (FPW)0x00FF00FF; /* Intel Read Mode */
97 else if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_AMD)
98 *base = (FPW)0x00F000F0; /* AMD Read Mode */
101 /*-----------------------------------------------------------------------
104 static flash_info_t *flash_get_info(ulong base)
109 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i ++) {
110 info = & flash_info[i];
112 info->start[0] <= base && base <= info->start[0] + info->size - 1)
116 return i == CONFIG_SYS_MAX_FLASH_BANKS ? 0 : info;
119 /*-----------------------------------------------------------------------
122 void flash_print_info (flash_info_t *info)
128 uchar botbootletter[] = "B";
129 uchar topbootletter[] = "T";
130 uchar botboottype[] = "bottom boot sector";
131 uchar topboottype[] = "top boot sector";
133 if (info->flash_id == FLASH_UNKNOWN) {
134 printf ("missing or unknown FLASH type\n");
138 switch (info->flash_id & FLASH_VENDMASK) {
139 case FLASH_MAN_AMD: printf ("AMD "); break;
140 case FLASH_MAN_BM: printf ("BRIGHT MICRO "); break;
141 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
142 case FLASH_MAN_SST: printf ("SST "); break;
143 case FLASH_MAN_STM: printf ("STM "); break;
144 case FLASH_MAN_INTEL: printf ("INTEL "); break;
145 default: printf ("Unknown Vendor "); break;
148 /* check for top or bottom boot, if it applies */
149 if (info->flash_id & FLASH_BTYPE) {
150 boottype = botboottype;
151 bootletter = botbootletter;
154 boottype = topboottype;
155 bootletter = topbootletter;
158 switch (info->flash_id & FLASH_TYPEMASK) {
159 case FLASH_AMDLV065D:
160 fmt = "29LV065 (64 Mbit, uniform sectors)\n";
163 fmt = "Unknown Chip Type\n";
167 printf (fmt, bootletter, boottype);
169 printf (" Size: %ld MB in %d Sectors\n",
173 printf (" Sector Start Addresses:");
175 for (i=0; i<info->sector_count; ++i) {
180 printf (" %08lX%s", info->start[i],
181 info->protect[i] ? " (RO)" : " ");
187 /*-----------------------------------------------------------------------
191 * The following code cannot be run from FLASH!
194 ulong flash_get_size (FPWV *addr, flash_info_t *info)
199 /* Write auto select command: read Manufacturer ID */
200 /* Write auto select command sequence and test FLASH answer */
201 addr[FLASH_CYCLE1] = (FPW)0x00AA00AA; /* for AMD, Intel ignores this */
202 addr[FLASH_CYCLE2] = (FPW)0x00550055; /* for AMD, Intel ignores this */
203 addr[FLASH_CYCLE1] = (FPW)0x00900090; /* selects Intel or AMD */
205 /* The manufacturer codes are only 1 byte, so just use 1 byte.
206 * This works for any bus width and any FLASH device width.
209 switch (addr[0] & 0xff) {
211 case (uchar)AMD_MANUFACT:
212 info->flash_id = FLASH_MAN_AMD;
215 case (uchar)INTEL_MANUFACT:
216 info->flash_id = FLASH_MAN_INTEL;
220 info->flash_id = FLASH_UNKNOWN;
221 info->sector_count = 0;
226 /* Check 16 bits or 32 bits of ID so work on 32 or 16 bit bus. */
227 if (info->flash_id != FLASH_UNKNOWN) switch ((FPW)addr[1]) {
229 case (FPW)AMD_ID_LV065D:
230 info->flash_id += FLASH_AMDLV065D;
231 info->sector_count = 128;
232 info->size = 0x00800000;
233 for( i = 0; i < info->sector_count; i++ )
234 info->start[i] = (ulong)addr + (i * 0x10000);
235 break; /* => 8 or 16 MB */
238 info->flash_id = FLASH_UNKNOWN;
239 info->sector_count = 0;
241 return (0); /* => no or unknown flash */
244 /* test for real flash at bank 1 */
245 addr2 = (FPW *)((ulong)addr | 0x800000);
247 ((addr2[0] & 0xff) == (addr[0] & 0xff)) && ((FPW)addr2[1] == (FPW)addr[1])) {
248 /* Seems 2 banks are the same space (8Mb chip is installed,
249 * J24 in default position (CS0)). Disable this (first) bank.
251 info->flash_id = FLASH_UNKNOWN;
252 info->sector_count = 0;
255 /* Put FLASH back in read mode */
261 /*-----------------------------------------------------------------------
264 int flash_erase (flash_info_t *info, int s_first, int s_last)
267 int flag, prot, sect;
268 int intel = (info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL;
269 ulong start, now, last;
272 if ((s_first < 0) || (s_first > s_last)) {
273 if (info->flash_id == FLASH_UNKNOWN) {
274 printf ("- missing\n");
276 printf ("- no sectors to erase\n");
281 switch (info->flash_id & FLASH_TYPEMASK) {
282 case FLASH_AMDLV065D:
286 printf ("Can't erase unknown flash type %08lx - aborted\n",
292 for (sect=s_first; sect<=s_last; ++sect) {
293 if (info->protect[sect]) {
299 printf ("- Warning: %d protected sectors will not be erased!\n",
307 /* Start erase on unprotected sectors */
308 for (sect = s_first; sect<=s_last && rcode == 0; sect++) {
310 if (info->protect[sect] != 0) /* protected, skip it */
313 /* Disable interrupts which might cause a timeout here */
314 flag = disable_interrupts();
316 addr = (FPWV *)(info->start[sect]);
318 *addr = (FPW)0x00500050; /* clear status register */
319 *addr = (FPW)0x00200020; /* erase setup */
320 *addr = (FPW)0x00D000D0; /* erase confirm */
323 /* must be AMD style if not Intel */
324 FPWV *base; /* first address in bank */
326 base = (FPWV *)(info->start[0]);
327 base[FLASH_CYCLE1] = (FPW)0x00AA00AA; /* unlock */
328 base[FLASH_CYCLE2] = (FPW)0x00550055; /* unlock */
329 base[FLASH_CYCLE1] = (FPW)0x00800080; /* erase mode */
330 base[FLASH_CYCLE1] = (FPW)0x00AA00AA; /* unlock */
331 base[FLASH_CYCLE2] = (FPW)0x00550055; /* unlock */
332 *addr = (FPW)0x00300030; /* erase sector */
335 /* re-enable interrupts if necessary */
339 start = get_timer(0);
341 /* wait at least 50us for AMD, 80us for Intel.
346 while ((*addr & (FPW)0x00800080) != (FPW)0x00800080) {
347 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
348 printf ("Timeout\n");
352 *addr = (FPW)0x00B000B0;
355 flash_reset(info); /* reset to read mode */
356 rcode = 1; /* failed */
360 /* show that we're waiting */
361 if ((get_timer(last)) > CONFIG_SYS_HZ) {/* every second */
367 /* show that we're waiting */
368 if ((get_timer(last)) > CONFIG_SYS_HZ) { /* every second */
373 flash_reset(info); /* reset to read mode */
380 /*-----------------------------------------------------------------------
381 * Copy memory to flash, returns:
384 * 2 - Flash not erased
386 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
388 FPW data = 0; /* 16 or 32 bit word, matches flash bus width on MPC8XX */
389 int bytes; /* number of bytes to program in current word */
390 int left; /* number of bytes left to program */
393 for (left = cnt, res = 0;
394 left > 0 && res == 0;
395 addr += sizeof(data), left -= sizeof(data) - bytes) {
397 bytes = addr & (sizeof(data) - 1);
398 addr &= ~(sizeof(data) - 1);
400 /* combine source and destination data so can program
401 * an entire word of 16 or 32 bits
403 for (i = 0; i < sizeof(data); i++) {
405 if (i < bytes || i - bytes >= left )
406 data += *((uchar *)addr + i);
411 /* write one word to the flash */
412 switch (info->flash_id & FLASH_VENDMASK) {
414 res = write_word_amd(info, (FPWV *)addr, data);
417 /* unknown flash type, error! */
418 printf ("missing or unknown FLASH type\n");
419 res = 1; /* not really a timeout, but gives error */
427 /*-----------------------------------------------------------------------
428 * Write a word to Flash for AMD FLASH
429 * A word is 16 or 32 bits, whichever the bus width of the flash bank
430 * (not an individual chip) is.
435 * 2 - Flash not erased
437 static int write_word_amd (flash_info_t *info, FPWV *dest, FPW data)
441 int res = 0; /* result, assume success */
442 FPWV *base; /* first address in flash bank */
444 /* Check if Flash is (sufficiently) erased */
445 if ((*dest & data) != data) {
450 base = (FPWV *)(info->start[0]);
452 /* Disable interrupts which might cause a timeout here */
453 flag = disable_interrupts();
455 base[FLASH_CYCLE1] = (FPW)0x00AA00AA; /* unlock */
456 base[FLASH_CYCLE2] = (FPW)0x00550055; /* unlock */
457 base[FLASH_CYCLE1] = (FPW)0x00A000A0; /* selects program mode */
459 *dest = data; /* start programming the data */
461 /* re-enable interrupts if necessary */
465 start = get_timer (0);
467 /* data polling for D7 */
468 while (res == 0 && (*dest & (FPW)0x00800080) != (data & (FPW)0x00800080)) {
469 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
470 *dest = (FPW)0x00F000F0; /* reset bank */
477 #endif /*CONFIG_FLASH_CFI_DRIVER*/