2 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 * SPDX-License-Identifier: GPL-2.0+
10 #ifndef CONFIG_ENV_ADDR
11 #define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
14 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
16 /*-----------------------------------------------------------------------
19 static int write_word(flash_info_t *info, ulong dest, ulong data);
22 static int my_in_8(unsigned char *addr);
23 static void my_out_8(unsigned char *addr, int val);
25 #ifdef CONFIG_BOOT_16B
26 static int my_in_be16(unsigned short *addr);
27 static void my_out_be16(unsigned short *addr, int val);
29 #ifdef CONFIG_BOOT_32B
30 static unsigned my_in_be32(unsigned *addr);
31 static void my_out_be32(unsigned *addr, int val);
33 /*-----------------------------------------------------------------------
36 unsigned long flash_init(void)
38 volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
39 volatile memctl8xx_t *memctl = &immap->im_memctl;
40 unsigned long size_b0, size_b1;
45 /* Init: no FLASHes known */
46 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i)
47 flash_info[i].flash_id = FLASH_UNKNOWN;
49 #ifdef CONFIG_SYS_DOC_BASE
50 #ifndef CONFIG_FEL8xx_AT
52 memctl->memc_or5 = (0xffff8000 | CONFIG_SYS_OR_TIMING_DOC);
53 memctl->memc_br5 = CONFIG_SYS_DOC_BASE | 0x401;
56 memctl->memc_or3 = (0xffff8000 | CONFIG_SYS_OR_TIMING_DOC);
57 memctl->memc_br3 = CONFIG_SYS_DOC_BASE | 0x401;
60 #if defined(CONFIG_BOOT_8B)
61 size_b0 = 0x80000; /* 512 K */
63 flash_info[0].flash_id = FLASH_MAN_AMD | FLASH_AM040;
64 flash_info[0].sector_count = 8;
65 flash_info[0].size = 0x00080000;
67 /* set up sector start address table */
68 for (i = 0; i < flash_info[0].sector_count; i++)
69 flash_info[0].start[i] = 0x40000000 + (i * 0x10000);
71 /* protect all sectors */
72 for (i = 0; i < flash_info[0].sector_count; i++)
73 flash_info[0].protect[i] = 0x1;
75 #elif defined(CONFIG_BOOT_16B)
76 size_b0 = 0x400000; /* 4MB , assume AMD29LV320B */
78 flash_info[0].flash_id = FLASH_MAN_AMD | FLASH_AM320B;
79 flash_info[0].sector_count = 67;
80 flash_info[0].size = 0x00400000;
82 /* set up sector start address table */
83 flash_info[0].start[0] = 0x40000000;
84 flash_info[0].start[1] = 0x40000000 + 0x4000;
85 flash_info[0].start[2] = 0x40000000 + 0x6000;
86 flash_info[0].start[3] = 0x40000000 + 0x8000;
88 for (i = 4; i < flash_info[0].sector_count; i++) {
89 flash_info[0].start[i] =
90 0x40000000 + 0x10000 + ((i - 4) * 0x10000);
93 /* protect all sectors */
94 for (i = 0; i < flash_info[0].sector_count; i++)
95 flash_info[0].protect[i] = 0x1;
98 #ifdef CONFIG_BOOT_32B
100 /* Static FLASH Bank configuration here - FIXME XXX */
101 size_b0 = flash_get_size((vu_long *) FLASH_BASE0_PRELIM,
104 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
105 printf("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
106 size_b0, size_b0 << 20);
109 size_b1 = flash_get_size((vu_long *) FLASH_BASE1_PRELIM,
112 if (size_b1 > size_b0) {
114 "Bank 1 (0x%08lx = %ld MB) > Bank 0 (0x%08lx = %ld MB)\n",
115 size_b1, size_b1 << 20, size_b0, size_b0 << 20);
116 flash_info[0].flash_id = FLASH_UNKNOWN;
117 flash_info[1].flash_id = FLASH_UNKNOWN;
118 flash_info[0].sector_count = -1;
119 flash_info[1].sector_count = -1;
120 flash_info[0].size = 0;
121 flash_info[1].size = 0;
126 /* Remap FLASH according to real size */
127 memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH |
128 (-size_b0 & OR_AM_MSK);
129 memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) |
132 /* Re-do sizing to get full correct info */
133 size_b0 = flash_get_size((vu_long *) CONFIG_SYS_FLASH_BASE,
136 flash_get_offsets(CONFIG_SYS_FLASH_BASE, &flash_info[0]);
138 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
139 /* monitor protection ON by default */
140 flash_protect(FLAG_PROTECT_SET,
141 CONFIG_SYS_MONITOR_BASE,
142 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
146 #ifdef CONFIG_ENV_IS_IN_FLASH
147 /* ENV protection ON by default */
148 flash_protect(FLAG_PROTECT_SET,
150 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
154 memctl->memc_or1 = CONFIG_SYS_OR_TIMING_FLASH |
155 (-size_b1 & 0xFFFF8000);
156 memctl->memc_br1 = ((CONFIG_SYS_FLASH_BASE +
157 size_b0) & BR_BA_MSK) | BR_MS_GPCM | BR_V;
159 /* Re-do sizing to get full correct info */
160 size_b1 = flash_get_size((vu_long *)(CONFIG_SYS_FLASH_BASE +
161 size_b0), &flash_info[1]);
163 flash_get_offsets(CONFIG_SYS_FLASH_BASE + size_b0,
166 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
167 /* monitor protection ON by default */
168 flash_protect(FLAG_PROTECT_SET,
169 CONFIG_SYS_MONITOR_BASE,
170 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
174 #ifdef CONFIG_ENV_IS_IN_FLASH
175 /* ENV protection ON by default */
176 flash_protect(FLAG_PROTECT_SET,
178 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1,
182 memctl->memc_br1 = 0; /* invalidate bank */
184 flash_info[1].flash_id = FLASH_UNKNOWN;
185 flash_info[1].sector_count = -1;
188 flash_info[0].size = size_b0;
189 flash_info[1].size = size_b1;
192 #endif /* CONFIG_BOOT_32B */
194 return size_b0 + size_b1;
198 void flash_print_info(flash_info_t *info)
202 if (info->flash_id == FLASH_UNKNOWN) {
203 printf("missing or unknown FLASH type\n");
207 switch (info->flash_id & FLASH_VENDMASK) {
215 printf("Unknown Vendor ");
219 switch (info->flash_id & FLASH_TYPEMASK) {
221 printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
224 printf("AM29LV400T (4 Mbit, top boot sector)\n");
227 printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
230 printf("AM29LV800T (8 Mbit, top boot sector)\n");
233 printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
236 printf("AM29LV160T (16 Mbit, top boot sector)\n");
239 printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
242 printf("AM29LV320T (32 Mbit, top boot sector)\n");
245 printf("Unknown Chip Type\n");
249 printf(" Size: %ld MB in %d Sectors\n",
250 info->size >> 20, info->sector_count);
252 printf(" Sector Start Addresses:");
253 for (i = 0; i < info->sector_count; ++i) {
257 info->start[i], info->protect[i] ? " (RO)" : " ");
264 * The following code cannot be run from FLASH!
267 int flash_erase(flash_info_t *info, int s_first, int s_last)
269 vu_long *addr = (vu_long *) (info->start[0]);
270 int flag, prot, sect, l_sect, in_mid, in_did;
271 ulong start, now, last;
273 if ((s_first < 0) || (s_first > s_last)) {
274 if (info->flash_id == FLASH_UNKNOWN)
275 printf("- missing\n");
277 printf("- no sectors to erase\n");
282 if ((info->flash_id == FLASH_UNKNOWN) ||
283 (info->flash_id > FLASH_AMD_COMP)) {
284 printf("Can't erase unknown flash type %08lx - aborted\n",
290 for (sect = s_first; sect <= s_last; ++sect) {
291 if (info->protect[sect])
296 printf("- Warning: %d protected sectors will not be erased!\n",
304 /* Disable interrupts which might cause a timeout here */
305 flag = disable_interrupts();
307 #if defined(CONFIG_BOOT_8B)
308 my_out_8((unsigned char *)((ulong)addr + 0x555), 0xaa);
309 my_out_8((unsigned char *)((ulong)addr + 0x2aa), 0x55);
310 my_out_8((unsigned char *)((ulong)addr + 0x555), 0x90);
312 in_mid = my_in_8((unsigned char *)addr);
313 in_did = my_in_8((unsigned char *)((ulong)addr + 1));
315 printf(" man ID=0x%x, dev ID=0x%x.\n", in_mid, in_did);
317 my_out_8((unsigned char *)addr, 0xf0);
320 my_out_8((unsigned char *)((ulong)addr + 0x555), 0xaa);
321 my_out_8((unsigned char *)((ulong)addr + 0x2aa), 0x55);
322 my_out_8((unsigned char *)((ulong)addr + 0x555), 0x80);
323 my_out_8((unsigned char *)((ulong)addr + 0x555), 0xaa);
324 my_out_8((unsigned char *)((ulong)addr + 0x2aa), 0x55);
326 /* Start erase on unprotected sectors */
327 for (sect = s_first; sect <= s_last; sect++) {
328 if (info->protect[sect] == 0) { /* not protected */
329 addr = (vu_long *) (info->start[sect]);
330 /*addr[0] = 0x00300030; */
331 my_out_8((unsigned char *)((ulong)addr), 0x30);
335 #elif defined(CONFIG_BOOT_16B)
336 my_out_be16((unsigned short *)((ulong)addr + (0xaaa)), 0xaa);
337 my_out_be16((unsigned short *)((ulong)addr + (0x554)), 0x55);
338 my_out_be16((unsigned short *)((ulong)addr + (0xaaa)), 0x90);
339 in_mid = my_in_be16((unsigned short *)addr);
340 in_did = my_in_be16((unsigned short *)((ulong)addr + 2));
341 printf(" man ID=0x%x, dev ID=0x%x.\n", in_mid, in_did);
342 my_out_be16((unsigned short *)addr, 0xf0);
344 my_out_be16((unsigned short *)((ulong)addr + 0xaaa), 0xaa);
345 my_out_be16((unsigned short *)((ulong)addr + 0x554), 0x55);
346 my_out_be16((unsigned short *)((ulong)addr + 0xaaa), 0x80);
347 my_out_be16((unsigned short *)((ulong)addr + 0xaaa), 0xaa);
348 my_out_be16((unsigned short *)((ulong)addr + 0x554), 0x55);
349 /* Start erase on unprotected sectors */
350 for (sect = s_first; sect <= s_last; sect++) {
351 if (info->protect[sect] == 0) { /* not protected */
352 addr = (vu_long *) (info->start[sect]);
353 my_out_be16((unsigned short *)((ulong)addr), 0x30);
358 #elif defined(CONFIG_BOOT_32B)
359 my_out_be32((unsigned *)((ulong)addr + 0x1554), 0xaa);
360 my_out_be32((unsigned *)((ulong)addr + 0xaa8), 0x55);
361 my_out_be32((unsigned *)((ulong)addr + 0x1554), 0x90);
363 in_mid = my_in_be32((unsigned *)addr);
364 in_did = my_in_be32((unsigned *)((ulong)addr + 4));
366 printf(" man ID=0x%x, dev ID=0x%x.\n", in_mid, in_did);
368 my_out_be32((unsigned *) addr, 0xf0);
371 my_out_be32((unsigned *)((ulong)addr + 0x1554), 0xaa);
372 my_out_be32((unsigned *)((ulong)addr + 0xaa8), 0x55);
373 my_out_be32((unsigned *)((ulong)addr + 0x1554), 0x80);
374 my_out_be32((unsigned *)((ulong)addr + 0x1554), 0xaa);
375 my_out_be32((unsigned *)((ulong)addr + 0xaa8), 0x55);
377 /* Start erase on unprotected sectors */
378 for (sect = s_first; sect <= s_last; sect++) {
379 if (info->protect[sect] == 0) { /* not protected */
380 addr = (vu_long *) (info->start[sect]);
381 my_out_be32((unsigned *)((ulong)addr), 0x00300030);
387 #error CONFIG_BOOT_(size)B missing.
389 /* re-enable interrupts if necessary */
393 /* wait at least 80us - let's wait 1 ms */
397 * We wait for the last triggered sector
402 start = get_timer(0);
404 addr = (vu_long *) (info->start[l_sect]);
405 #if defined(CONFIG_BOOT_8B)
406 while ((my_in_8((unsigned char *) addr) & 0x80) != 0x80)
407 #elif defined(CONFIG_BOOT_16B)
408 while ((my_in_be16((unsigned short *) addr) & 0x0080) != 0x0080)
409 #elif defined(CONFIG_BOOT_32B)
410 while ((my_in_be32((unsigned *) addr) & 0x00800080) != 0x00800080)
412 #error CONFIG_BOOT_(size)B missing.
415 now = get_timer(start);
416 if (now > CONFIG_SYS_FLASH_ERASE_TOUT) {
420 /* show that we're waiting */
421 if ((now - last) > 1000) { /* every second */
427 /* reset to read mode */
428 addr = (volatile unsigned long *) info->start[0];
430 #if defined(CONFIG_BOOT_8B)
431 my_out_8((unsigned char *) addr, 0xf0);
432 #elif defined(CONFIG_BOOT_16B)
433 my_out_be16((unsigned short *) addr, 0x00f0);
434 #elif defined(CONFIG_BOOT_32B)
435 my_out_be32((unsigned *) addr, 0x00F000F0); /* reset bank */
437 #error CONFIG_BOOT_(size)B missing.
444 * Copy memory to flash, returns:
447 * 2 - Flash not erased
450 int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
455 wp = (addr & ~3); /* get lower word aligned address */
458 * handle unaligned start bytes
464 for (i = 0, cp = wp; i < l; ++i, ++cp)
465 data = (data << 8) | (*(uchar *) cp);
467 for (; i < 4 && cnt > 0; ++i) {
468 data = (data << 8) | *src++;
472 for (; cnt == 0 && i < 4; ++i, ++cp)
473 data = (data << 8) | (*(uchar *) cp);
475 rc = write_word(info, wp, data);
484 * handle word aligned part
488 for (i = 0; i < 4; ++i)
489 data = (data << 8) | *src++;
491 rc = write_word(info, wp, data);
504 * handle unaligned tail bytes
507 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
508 data = (data << 8) | *src++;
511 for (; i < 4; ++i, ++cp)
512 data = (data << 8) | (*(uchar *) cp);
514 return write_word(info, wp, data);
518 * Write a word to Flash, returns:
521 * 2 - Flash not erased
523 static int write_word(flash_info_t *info, ulong dest, ulong data)
525 ulong addr = (ulong) (info->start[0]);
531 /* Check if Flash is (sufficiently) erased */
532 if (((ulong)*(ulong *)dest & data) != data)
535 /* Disable interrupts which might cause a timeout here */
536 flag = disable_interrupts();
537 #if defined(CONFIG_BOOT_8B)
542 my_out_8((unsigned char *) (addr + 0x555), 0xaa);
543 my_out_8((unsigned char *) (addr + 0x2aa), 0x55);
544 my_out_8((unsigned char *) (addr + 0x555), 0x90);
546 in_mid = my_in_8((unsigned char *) addr);
547 in_did = my_in_8((unsigned char *) (addr + 1));
549 printf(" man ID=0x%x, dev ID=0x%x.\n", in_mid, in_did);
551 my_out_8((unsigned char *) addr, 0xf0);
558 data_ch[0] = (int) ((data >> 24) & 0xff);
559 data_ch[1] = (int) ((data >> 16) & 0xff);
560 data_ch[2] = (int) ((data >> 8) & 0xff);
561 data_ch[3] = (int) (data & 0xff);
563 for (i = 0; i < 4; i++) {
564 my_out_8((unsigned char *) (addr + 0x555), 0xaa);
565 my_out_8((unsigned char *) (addr + 0x2aa), 0x55);
566 my_out_8((unsigned char *) (addr + 0x555), 0xa0);
567 my_out_8((unsigned char *) (dest + i), data_ch[i]);
569 /* re-enable interrupts if necessary */
573 start = get_timer(0);
574 while ((my_in_8((unsigned char *)(dest + i))) !=
576 if (get_timer(start) >
577 CONFIG_SYS_FLASH_WRITE_TOUT) {
583 #elif defined(CONFIG_BOOT_16B)
584 data_short[0] = (int) (data >> 16) & 0xffff;
585 data_short[1] = (int) data & 0xffff;
586 for (i = 0; i < 2; i++) {
587 my_out_be16((unsigned short *)((ulong)addr + 0xaaa), 0xaa);
588 my_out_be16((unsigned short *)((ulong)addr + 0x554), 0x55);
589 my_out_be16((unsigned short *)((ulong)addr + 0xaaa), 0xa0);
590 my_out_be16((unsigned short *)(dest + (i * 2)),
593 /* re-enable interrupts if necessary */
597 start = get_timer(0);
598 while ((my_in_be16((unsigned short *)(dest + (i * 2)))) !=
600 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT)
604 #elif defined(CONFIG_BOOT_32B)
605 addr[0x0555] = 0x00AA00AA;
606 addr[0x02AA] = 0x00550055;
607 addr[0x0555] = 0x00A000A0;
609 *((vu_long *)dest) = data;
611 /* re-enable interrupts if necessary */
615 /* data polling for D7 */
616 start = get_timer(0);
617 while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
618 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT)
625 #ifdef CONFIG_BOOT_8B
626 static int my_in_8(unsigned char *addr)
629 __asm__ __volatile__("lbz%U1%X1 %0,%1; eieio":"=r"(ret):"m"(*addr));
634 static void my_out_8(unsigned char *addr, int val)
636 __asm__ __volatile__("stb%U0%X0 %1,%0; eieio":"=m"(*addr):"r"(val));
639 #ifdef CONFIG_BOOT_16B
640 static int my_in_be16(unsigned short *addr)
643 __asm__ __volatile__("lhz%U1%X1 %0,%1; eieio":"=r"(ret):"m"(*addr));
648 static void my_out_be16(unsigned short *addr, int val)
650 __asm__ __volatile__("sth%U0%X0 %1,%0; eieio":"=m"(*addr):"r"(val));
653 #ifdef CONFIG_BOOT_32B
654 static unsigned my_in_be32(unsigned *addr)
657 __asm__ __volatile__("lwz%U1%X1 %0,%1; eieio":"=r"(ret):"m"(*addr));
662 static void my_out_be32(unsigned *addr, int val)
664 __asm__ __volatile__("stw%U0%X0 %1,%0; eieio":"=m"(*addr):"r"(val));