5 * SPDX-License-Identifier: GPL-2.0+
11 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
13 #if defined(CONFIG_ENV_IS_IN_FLASH)
14 # ifndef CONFIG_ENV_ADDR
15 # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
17 # ifndef CONFIG_ENV_SIZE
18 # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
20 # ifndef CONFIG_ENV_SECT_SIZE
21 # define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
25 /*-----------------------------------------------------------------------
28 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
29 static int write_word (flash_info_t *info, ulong dest, ulong data);
30 static void flash_get_offsets (ulong base, flash_info_t *info);
32 /*-----------------------------------------------------------------------
35 unsigned long flash_init (void)
37 volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
38 volatile memctl8xx_t *memctl = &immap->im_memctl;
39 unsigned long size_b0;
42 /* Init: no FLASHes known */
43 for (i=0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i)
44 flash_info[i].flash_id = FLASH_UNKNOWN;
46 /* Static FLASH Bank configuration here - FIXME XXX */
48 size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
50 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
51 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
56 /* Remap FLASH according to real size */
57 memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b0 & OR_AM_MSK);
58 memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | BR_MS_GPCM | BR_V;
60 /* Re-do sizing to get full correct info */
61 size_b0 = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE, &flash_info[0]);
63 flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
65 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
66 /* monitor protection ON by default */
67 flash_protect(FLAG_PROTECT_SET,
68 CONFIG_SYS_MONITOR_BASE,
69 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
73 #ifdef CONFIG_ENV_IS_IN_FLASH
74 /* ENV protection ON by default */
75 flash_protect(FLAG_PROTECT_SET,
77 CONFIG_ENV_ADDR+CONFIG_ENV_SIZE-1,
81 /* ICU862 Board has only one Flash Bank */
82 flash_info[0].size = size_b0;
88 /*-----------------------------------------------------------------------
90 static void flash_get_offsets (ulong base, flash_info_t *info)
94 /* set up sector start address table */
95 if (((info->flash_id & FLASH_TYPEMASK) == FLASH_AM040) ||
96 ((info->flash_id & FLASH_TYPEMASK) == FLASH_AM033C)) {
97 /* set sector offsets for uniform sector type */
98 for (i = 0; i < info->sector_count; i++) {
99 info->start[i] = base + (i * 0x00040000);
104 /*-----------------------------------------------------------------------
106 void flash_print_info (flash_info_t *info)
110 if (info->flash_id == FLASH_UNKNOWN) {
111 puts ("missing or unknown FLASH type\n");
115 switch (info->flash_id & FLASH_VENDMASK) {
116 case FLASH_MAN_AMD: puts ("AMD "); break;
117 case FLASH_MAN_FUJ: puts ("FUJITSU "); break;
118 case FLASH_MAN_BM: puts ("BRIGHT MICRO "); break;
119 default: puts ("Unknown Vendor "); break;
122 switch (info->flash_id & FLASH_TYPEMASK) {
123 case FLASH_AM040: puts ("29F040/29LV040 (4 Mbit, uniform sectors)\n");
125 case FLASH_AM400B: puts ("AM29LV400B (4 Mbit, bottom boot sect)\n");
127 case FLASH_AM400T: puts ("AM29LV400T (4 Mbit, top boot sector)\n");
129 case FLASH_AM800B: puts ("AM29LV800B (8 Mbit, bottom boot sect)\n");
131 case FLASH_AM800T: puts ("AM29LV800T (8 Mbit, top boot sector)\n");
133 case FLASH_AM160B: puts ("AM29LV160B (16 Mbit, bottom boot sect)\n");
135 case FLASH_AM160T: puts ("AM29LV160T (16 Mbit, top boot sector)\n");
137 case FLASH_AM320B: puts ("AM29LV320B (32 Mbit, bottom boot sect)\n");
139 case FLASH_AM320T: puts ("AM29LV320T (32 Mbit, top boot sector)\n");
141 case FLASH_AM033C: puts ("AM29LV033C (32 Mbit)\n");
143 default: puts ("Unknown Chip Type\n");
147 printf (" Size: %ld MB in %d Sectors\n",
148 info->size >> 20, info->sector_count);
150 puts (" Sector Start Addresses:");
152 for (i=0; i<info->sector_count; ++i) {
159 info->protect[i] ? " (RO)" : " "
166 /*-----------------------------------------------------------------------
170 * The following code cannot be run from FLASH!
173 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
177 ulong base = (ulong)addr;
181 /* Write auto select command: read Manufacturer ID */
183 addr[0x0555] = 0x00AA00AA;
184 addr[0x02AA] = 0x00550055;
185 addr[0x0555] = 0x00900090;
187 addr[0x0555] = 0xAAAAAAAA;
188 addr[0x02AA] = 0x55555555;
189 addr[0x0555] = 0x90909090;
194 switch (value + (value << 16)) {
196 info->flash_id = FLASH_MAN_AMD;
200 info->flash_id = FLASH_MAN_FUJ;
204 info->flash_id = FLASH_UNKNOWN;
205 info->sector_count = 0;
210 value = addr[1]; /* device ID */
212 switch ((unsigned long)value) {
214 info->flash_id += FLASH_AM040;
215 info->sector_count = 8;
216 info->size = 0x00200000;
220 info->flash_id += FLASH_AM400T;
221 info->sector_count = 11;
222 info->size = 0x00100000;
226 info->flash_id += FLASH_AM400B;
227 info->sector_count = 11;
228 info->size = 0x00100000;
232 info->flash_id += FLASH_AM800T;
233 info->sector_count = 19;
234 info->size = 0x00200000;
238 info->flash_id += FLASH_AM800B;
239 info->sector_count = 19;
240 info->size = 0x00200000;
244 info->flash_id += FLASH_AM160T;
245 info->sector_count = 35;
246 info->size = 0x00400000;
250 info->flash_id += FLASH_AM160B;
251 info->sector_count = 35;
252 info->size = 0x00400000;
254 #if 0 /* enable when device IDs are available */
256 info->flash_id += FLASH_AM320T;
257 info->sector_count = 67;
258 info->size = 0x00800000;
262 info->flash_id += FLASH_AM320B;
263 info->sector_count = 67;
264 info->size = 0x00800000;
268 info->flash_id += FLASH_AM033C;
269 info->sector_count = 64;
270 info->size = 0x01000000;
273 info->flash_id = FLASH_UNKNOWN;
274 return (0); /* => no or unknown flash */
279 /* set up sector start address table */
280 if (info->flash_id & FLASH_BTYPE) {
281 /* set sector offsets for bottom boot block type */
282 info->start[0] = base + 0x00000000;
283 info->start[1] = base + 0x00008000;
284 info->start[2] = base + 0x0000C000;
285 info->start[3] = base + 0x00010000;
286 for (i = 4; i < info->sector_count; i++) {
287 info->start[i] = base + (i * 0x00020000) - 0x00060000;
290 /* set sector offsets for top boot block type */
291 i = info->sector_count - 1;
292 info->start[i--] = base + info->size - 0x00008000;
293 info->start[i--] = base + info->size - 0x0000C000;
294 info->start[i--] = base + info->size - 0x00010000;
295 for (; i >= 0; i--) {
296 info->start[i] = base + i * 0x00020000;
300 flash_get_offsets ((ulong)addr, &flash_info[0]);
303 /* check for protected sectors */
304 for (i = 0; i < info->sector_count; i++) {
305 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
306 /* D0 = 1 if protected */
307 addr = (volatile unsigned long *)(info->start[i]);
309 /* We don't know why it happens, but on ICU Board *
310 * for AMD29033C flash we need to resend the command of *
311 * reading flash protection for upper 8 Mb of flash */
313 addr[0x0555] = 0xAAAAAAAA;
314 addr[0x02AA] = 0x55555555;
315 addr[0x0555] = 0x90909090;
318 info->protect[i] = addr[2] & 1;
322 * Prevent writes to uninitialized FLASH.
324 if (info->flash_id != FLASH_UNKNOWN) {
325 addr = (volatile unsigned long *)info->start[0];
327 *addr = 0x00F000F0; /* reset bank */
329 *addr = 0xF0F0F0F0; /* reset bank */
337 /*-----------------------------------------------------------------------
340 int flash_erase (flash_info_t *info, int s_first, int s_last)
342 vu_long *addr = (vu_long*)(info->start[0]);
343 int flag, prot, sect, l_sect;
344 ulong start, now, last;
346 if ((s_first < 0) || (s_first > s_last)) {
347 if (info->flash_id == FLASH_UNKNOWN) {
348 puts ("- missing\n");
350 puts ("- no sectors to erase\n");
355 if ((info->flash_id == FLASH_UNKNOWN) ||
356 (info->flash_id > FLASH_AMD_COMP)) {
357 puts ("Can't erase unknown flash type - aborted\n");
362 for (sect=s_first; sect<=s_last; ++sect) {
363 if (info->protect[sect]) {
369 printf ("- Warning: %d protected sectors will not be erased!\n",
377 /* Disable interrupts which might cause a timeout here */
378 flag = disable_interrupts();
381 addr[0x0555] = 0x00AA00AA;
382 addr[0x02AA] = 0x00550055;
383 addr[0x0555] = 0x00800080;
384 addr[0x0555] = 0x00AA00AA;
385 addr[0x02AA] = 0x00550055;
387 addr[0x0555] = 0xAAAAAAAA;
388 addr[0x02AA] = 0x55555555;
389 addr[0x0555] = 0x80808080;
390 addr[0x0555] = 0xAAAAAAAA;
391 addr[0x02AA] = 0x55555555;
394 /* Start erase on unprotected sectors */
395 for (sect = s_first; sect<=s_last; sect++) {
396 if (info->protect[sect] == 0) { /* not protected */
397 addr = (vu_long*)(info->start[sect]);
399 addr[0] = 0x00300030;
401 addr[0] = 0x30303030;
407 /* re-enable interrupts if necessary */
411 /* wait at least 80us - let's wait 1 ms */
415 * We wait for the last triggered sector
420 start = get_timer (0);
422 addr = (vu_long*)(info->start[l_sect]);
424 while ((addr[0] & 0x00800080) != 0x00800080)
426 while ((addr[0] & 0xFFFFFFFF) != 0xFFFFFFFF)
429 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
433 /* show that we're waiting */
434 if ((now - last) > 1000) { /* every second */
441 /* reset to read mode */
442 addr = (volatile unsigned long *)info->start[0];
444 addr[0] = 0x00F000F0; /* reset bank */
446 addr[0] = 0xF0F0F0F0; /* reset bank */
453 /*-----------------------------------------------------------------------
454 * Copy memory to flash, returns:
457 * 2 - Flash not erased
460 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
465 wp = (addr & ~3); /* get lower word aligned address */
468 * handle unaligned start bytes
470 if ((l = addr - wp) != 0) {
472 for (i=0, cp=wp; i<l; ++i, ++cp) {
473 data = (data << 8) | (*(uchar *)cp);
475 for (; i<4 && cnt>0; ++i) {
476 data = (data << 8) | *src++;
480 for (; cnt==0 && i<4; ++i, ++cp) {
481 data = (data << 8) | (*(uchar *)cp);
484 if ((rc = write_word(info, wp, data)) != 0) {
491 * handle word aligned part
495 for (i=0; i<4; ++i) {
496 data = (data << 8) | *src++;
498 if ((rc = write_word(info, wp, data)) != 0) {
510 * handle unaligned tail bytes
513 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
514 data = (data << 8) | *src++;
517 for (; i<4; ++i, ++cp) {
518 data = (data << 8) | (*(uchar *)cp);
521 return (write_word(info, wp, data));
524 /*-----------------------------------------------------------------------
525 * Write a word to Flash, returns:
528 * 2 - Flash not erased
530 static int write_word (flash_info_t *info, ulong dest, ulong data)
532 vu_long *addr = (vu_long*)(info->start[0]);
536 /* Check if Flash is (sufficiently) erased */
537 if ((*((vu_long *)dest) & data) != data) {
540 /* Disable interrupts which might cause a timeout here */
541 flag = disable_interrupts();
544 addr[0x0555] = 0x00AA00AA;
545 addr[0x02AA] = 0x00550055;
546 addr[0x0555] = 0x00A000A0;
548 addr[0x0555] = 0xAAAAAAAA;
549 addr[0x02AA] = 0x55555555;
550 addr[0x0555] = 0xA0A0A0A0;
553 *((vu_long *)dest) = data;
555 /* re-enable interrupts if necessary */
559 /* data polling for D7 */
560 start = get_timer (0);
562 while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080))
564 while ((*((vu_long *)dest) & 0x80808080) != (data & 0x80808080))
567 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
574 /*-----------------------------------------------------------------------