2 * (C) Copyright 2000-2004
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 #include <environment.h>
32 #include <asm/processor.h>
34 DECLARE_GLOBAL_DATA_PTR;
36 #if defined(CONFIG_TQM8xxL) && !defined(CONFIG_TQM866M) \
37 && !defined(CONFIG_TQM885D)
38 # ifndef CFG_OR_TIMING_FLASH_AT_50MHZ
39 # define CFG_OR_TIMING_FLASH_AT_50MHZ (OR_ACS_DIV1 | OR_TRLX | OR_CSNT_SAM | \
40 OR_SCY_2_CLK | OR_EHTR | OR_BI)
42 #endif /* CONFIG_TQM8xxL/M, !TQM866M, !TQM885D */
45 #define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
48 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
50 /*-----------------------------------------------------------------------
53 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
54 static int write_word (flash_info_t *info, ulong dest, ulong data);
56 /*-----------------------------------------------------------------------
59 unsigned long flash_init (void)
61 volatile immap_t *immap = (immap_t *)CFG_IMMR;
62 volatile memctl8xx_t *memctl = &immap->im_memctl;
63 unsigned long size_b0, size_b1;
66 #ifdef CFG_OR_TIMING_FLASH_AT_50MHZ
67 int scy, trlx, flash_or_timing, clk_diff;
69 scy = (CFG_OR_TIMING_FLASH_AT_50MHZ & OR_SCY_MSK) >> 4;
70 if (CFG_OR_TIMING_FLASH_AT_50MHZ & OR_TRLX) {
76 /* We assume that each 10MHz of bus clock require 1-clk SCY
79 clk_diff = (gd->bus_clk / 1000000) - 50;
81 /* We need proper rounding here. This is what the "+5" and "-5"
85 scy += (clk_diff + 5) / 10;
87 scy += (clk_diff - 5) / 10;
89 /* For bus frequencies above 50MHz, we want to use relaxed timing
92 if (gd->bus_clk >= 50000000)
105 flash_or_timing = (scy << 4) | trlx |
106 (CFG_OR_TIMING_FLASH_AT_50MHZ & ~(OR_TRLX | OR_SCY_MSK));
108 /* Init: no FLASHes known */
109 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
110 flash_info[i].flash_id = FLASH_UNKNOWN;
113 /* Static FLASH Bank configuration here - FIXME XXX */
115 debug ("\n## Get flash bank 1 size @ 0x%08x\n",FLASH_BASE0_PRELIM);
117 size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
119 debug ("## Get flash bank 2 size @ 0x%08x\n",FLASH_BASE1_PRELIM);
121 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
122 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
123 size_b0, size_b0<<20);
126 size_b1 = flash_get_size((vu_long *)FLASH_BASE1_PRELIM, &flash_info[1]);
128 debug ("## Prelim. Flash bank sizes: %08lx + 0x%08lx\n",size_b0,size_b1);
130 if (size_b1 > size_b0) {
132 "Bank 1 (0x%08lx = %ld MB) > Bank 0 (0x%08lx = %ld MB)\n",
133 size_b1, size_b1<<20,
136 flash_info[0].flash_id = FLASH_UNKNOWN;
137 flash_info[1].flash_id = FLASH_UNKNOWN;
138 flash_info[0].sector_count = -1;
139 flash_info[1].sector_count = -1;
140 flash_info[0].size = 0;
141 flash_info[1].size = 0;
145 debug ("## Before remap: "
146 "BR0: 0x%08x OR0: 0x%08x "
147 "BR1: 0x%08x OR1: 0x%08x\n",
148 memctl->memc_br0, memctl->memc_or0,
149 memctl->memc_br1, memctl->memc_or1);
151 /* Remap FLASH according to real size */
152 #ifndef CFG_OR_TIMING_FLASH_AT_50MHZ
153 memctl->memc_or0 = CFG_OR_TIMING_FLASH | (-size_b0 & OR_AM_MSK);
155 memctl->memc_or0 = flash_or_timing | (-size_b0 & OR_AM_MSK);
157 memctl->memc_br0 = (CFG_FLASH_BASE & BR_BA_MSK) | BR_MS_GPCM | BR_V;
159 debug ("## BR0: 0x%08x OR0: 0x%08x\n",
160 memctl->memc_br0, memctl->memc_or0);
162 /* Re-do sizing to get full correct info */
163 size_b0 = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
165 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
166 /* monitor protection ON by default */
167 debug ("Protect monitor: %08lx ... %08lx\n",
168 (ulong)CFG_MONITOR_BASE,
169 (ulong)CFG_MONITOR_BASE + monitor_flash_len - 1);
171 flash_protect(FLAG_PROTECT_SET,
173 CFG_MONITOR_BASE + monitor_flash_len - 1,
177 #ifdef CFG_ENV_IS_IN_FLASH
178 /* ENV protection ON by default */
179 # ifdef CFG_ENV_ADDR_REDUND
180 debug ("Protect primary environment: %08lx ... %08lx\n",
182 (ulong)CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1);
184 debug ("Protect environment: %08lx ... %08lx\n",
186 (ulong)CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1);
189 flash_protect(FLAG_PROTECT_SET,
191 CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1,
195 #ifdef CFG_ENV_ADDR_REDUND
196 debug ("Protect redundand environment: %08lx ... %08lx\n",
197 (ulong)CFG_ENV_ADDR_REDUND,
198 (ulong)CFG_ENV_ADDR_REDUND + CFG_ENV_SECT_SIZE - 1);
200 flash_protect(FLAG_PROTECT_SET,
202 CFG_ENV_ADDR_REDUND + CFG_ENV_SECT_SIZE - 1,
207 #ifndef CFG_OR_TIMING_FLASH_AT_50MHZ
208 memctl->memc_or1 = CFG_OR_TIMING_FLASH | (-size_b1 & 0xFFFF8000);
210 memctl->memc_or1 = flash_or_timing | (-size_b1 & 0xFFFF8000);
212 memctl->memc_br1 = ((CFG_FLASH_BASE + size_b0) & BR_BA_MSK) |
215 debug ("## BR1: 0x%08x OR1: 0x%08x\n",
216 memctl->memc_br1, memctl->memc_or1);
218 /* Re-do sizing to get full correct info */
219 size_b1 = flash_get_size((vu_long *)(CFG_FLASH_BASE + size_b0),
222 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
223 /* monitor protection ON by default */
224 flash_protect(FLAG_PROTECT_SET,
226 CFG_MONITOR_BASE+monitor_flash_len-1,
230 #ifdef CFG_ENV_IS_IN_FLASH
231 /* ENV protection ON by default */
232 flash_protect(FLAG_PROTECT_SET,
234 CFG_ENV_ADDR+CFG_ENV_SIZE-1,
238 memctl->memc_br1 = 0; /* invalidate bank */
240 flash_info[1].flash_id = FLASH_UNKNOWN;
241 flash_info[1].sector_count = -1;
242 flash_info[1].size = 0;
244 debug ("## DISABLE BR1: 0x%08x OR1: 0x%08x\n",
245 memctl->memc_br1, memctl->memc_or1);
248 debug ("## Final Flash bank sizes: %08lx + 0x%08lx\n",size_b0,size_b1);
250 flash_info[0].size = size_b0;
251 flash_info[1].size = size_b1;
253 return (size_b0 + size_b1);
256 /*-----------------------------------------------------------------------
258 void flash_print_info (flash_info_t *info)
262 if (info->flash_id == FLASH_UNKNOWN) {
263 printf ("missing or unknown FLASH type\n");
267 switch (info->flash_id & FLASH_VENDMASK) {
268 case FLASH_MAN_AMD: printf ("AMD "); break;
269 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
270 default: printf ("Unknown Vendor "); break;
273 switch (info->flash_id & FLASH_TYPEMASK) {
274 #ifdef CONFIG_TQM8xxM /* mirror bit flash */
275 case FLASH_AMLV128U: printf ("AM29LV128ML (128Mbit, uniform sector size)\n");
277 case FLASH_AMLV320U: printf ("AM29LV320ML (32Mbit, uniform sector size)\n");
279 case FLASH_AMLV640U: printf ("AM29LV640ML (64Mbit, uniform sector size)\n");
281 case FLASH_AMLV320B: printf ("AM29LV320MB (32Mbit, bottom boot sect)\n");
283 # else /* ! TQM8xxM */
284 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
286 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
288 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
290 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
292 case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
294 case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
297 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
299 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
301 case FLASH_AMDL163B: printf ("AM29DL163B (16 Mbit, bottom boot sect)\n");
303 default: printf ("Unknown Chip Type\n");
307 printf (" Size: %ld MB in %d Sectors\n",
308 info->size >> 20, info->sector_count);
310 printf (" Sector Start Addresses:");
311 for (i=0; i<info->sector_count; ++i) {
316 info->protect[i] ? " (RO)" : " "
323 /*-----------------------------------------------------------------------
327 /*-----------------------------------------------------------------------
331 * The following code cannot be run from FLASH!
334 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
338 ulong base = (ulong)addr;
340 /* Write auto select command: read Manufacturer ID */
341 addr[0x0555] = 0x00AA00AA;
342 addr[0x02AA] = 0x00550055;
343 addr[0x0555] = 0x00900090;
347 debug ("Manuf. ID @ 0x%08lx: 0x%08lx\n", (ulong)addr, value);
351 debug ("Manufacturer: AMD\n");
352 info->flash_id = FLASH_MAN_AMD;
355 debug ("Manufacturer: FUJITSU\n");
356 info->flash_id = FLASH_MAN_FUJ;
359 debug ("Manufacturer: *** unknown ***\n");
360 info->flash_id = FLASH_UNKNOWN;
361 info->sector_count = 0;
363 return (0); /* no or unknown flash */
366 value = addr[1]; /* device ID */
368 debug ("Device ID @ 0x%08lx: 0x%08lx\n", (ulong)(&addr[1]), value);
371 #ifdef CONFIG_TQM8xxM /* mirror bit flash */
373 debug ("Mirror Bit flash: addr[14] = %08lX addr[15] = %08lX\n",
375 /* Special case for AMLV320MH/L */
376 if ((addr[14] & 0x00ff00ff) == 0x001d001d &&
377 (addr[15] & 0x00ff00ff) == 0x00000000) {
378 debug ("Chip: AMLV320MH/L\n");
379 info->flash_id += FLASH_AMLV320U;
380 info->sector_count = 64;
381 info->size = 0x00800000; /* => 8 MB */
385 case AMD_ID_LV128U_2:
386 if (addr[15] != AMD_ID_LV128U_3) {
387 debug ("Chip: AMLV128U -> unknown\n");
388 info->flash_id = FLASH_UNKNOWN;
390 debug ("Chip: AMLV128U\n");
391 info->flash_id += FLASH_AMLV128U;
392 info->sector_count = 256;
393 info->size = 0x02000000;
395 break; /* => 32 MB */
396 case AMD_ID_LV640U_2:
397 if (addr[15] != AMD_ID_LV640U_3) {
398 debug ("Chip: AMLV640U -> unknown\n");
399 info->flash_id = FLASH_UNKNOWN;
401 debug ("Chip: AMLV640U\n");
402 info->flash_id += FLASH_AMLV640U;
403 info->sector_count = 128;
404 info->size = 0x01000000;
406 break; /* => 16 MB */
407 case AMD_ID_LV320B_2:
408 if (addr[15] != AMD_ID_LV320B_3) {
409 debug ("Chip: AMLV320B -> unknown\n");
410 info->flash_id = FLASH_UNKNOWN;
412 debug ("Chip: AMLV320B\n");
413 info->flash_id += FLASH_AMLV320B;
414 info->sector_count = 71;
415 info->size = 0x00800000;
419 debug ("Chip: *** unknown ***\n");
420 info->flash_id = FLASH_UNKNOWN;
424 # else /* ! TQM8xxM */
426 info->flash_id += FLASH_AM400T;
427 info->sector_count = 11;
428 info->size = 0x00100000;
432 info->flash_id += FLASH_AM400B;
433 info->sector_count = 11;
434 info->size = 0x00100000;
438 info->flash_id += FLASH_AM800T;
439 info->sector_count = 19;
440 info->size = 0x00200000;
444 info->flash_id += FLASH_AM800B;
445 info->sector_count = 19;
446 info->size = 0x00200000;
450 info->flash_id += FLASH_AM320T;
451 info->sector_count = 71;
452 info->size = 0x00800000;
456 info->flash_id += FLASH_AM320B;
457 info->sector_count = 71;
458 info->size = 0x00800000;
463 info->flash_id += FLASH_AM160T;
464 info->sector_count = 35;
465 info->size = 0x00400000;
469 info->flash_id += FLASH_AM160B;
470 info->sector_count = 35;
471 info->size = 0x00400000;
475 info->flash_id += FLASH_AMDL163B;
476 info->sector_count = 39;
477 info->size = 0x00400000;
481 info->flash_id = FLASH_UNKNOWN;
482 return (0); /* => no or unknown flash */
485 /* set up sector start address table */
487 #ifdef CONFIG_TQM8xxM /* mirror bit flash */
489 switch (info->flash_id & FLASH_TYPEMASK) {
490 /* only known types here - no default */
494 for (i = 0; i < info->sector_count; i++) {
495 info->start[i] = base;
500 for (i = 0; i < info->sector_count; i++) {
501 info->start[i] = base;
503 * The first 8 sectors are 8 kB,
504 * all the other ones are 64 kB
513 # else /* ! TQM8xxM */
516 /* set sector offsets for bottom boot block type */
517 info->start[0] = base + 0x00000000;
518 info->start[1] = base + 0x00008000;
519 info->start[2] = base + 0x0000C000;
520 info->start[3] = base + 0x00010000;
521 for (i = 4; i < info->sector_count; i++) {
522 info->start[i] = base + (i * 0x00020000) - 0x00060000;
527 /* set sector offsets for top boot block type */
528 i = info->sector_count - 1;
529 info->start[i--] = base + info->size - 0x00008000;
530 info->start[i--] = base + info->size - 0x0000C000;
531 info->start[i--] = base + info->size - 0x00010000;
532 for (; i >= 0; i--) {
533 info->start[i] = base + i * 0x00020000;
537 for (i = 0; i < info->sector_count; i++) {
538 info->start[i] = base;
540 * The first 8 sectors are 8 kB,
541 * all the other ones are 64 kB
549 for (i = 0; i < info->sector_count; i++) {
550 info->start[i] = base;
552 * The last 8 sectors are 8 kB,
553 * all the other ones are 64 kB
555 base += (i < (info->sector_count - 8))
562 /* set sector offsets for bottom boot block type */
563 info->start[0] = base + 0x00000000;
564 info->start[1] = base + 0x00008000;
565 info->start[2] = base + 0x0000C000;
566 info->start[3] = base + 0x00010000;
567 for (i = 4; i < info->sector_count; i++) {
568 info->start[i] = base + (i * 0x00020000) - 0x00060000;
572 /* set sector offsets for top boot block type */
573 i = info->sector_count - 1;
574 info->start[i--] = base + info->size - 0x00008000;
575 info->start[i--] = base + info->size - 0x0000C000;
576 info->start[i--] = base + info->size - 0x00010000;
577 for (; i >= 0; i--) {
578 info->start[i] = base + i * 0x00020000;
582 for (i = 0; i < info->sector_count; i++) {
583 info->start[i] = base;
585 * The first 8 sectors are 8 kB,
586 * all the other ones are 64 kB
599 /* check for protected sectors */
600 for (i = 0; i < info->sector_count; i++) {
601 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
602 /* D0 = 1 if protected */
603 addr = (volatile unsigned long *)(info->start[i]);
604 info->protect[i] = addr[2] & 1;
609 * Prevent writes to uninitialized FLASH.
611 if (info->flash_id != FLASH_UNKNOWN) {
612 addr = (volatile unsigned long *)info->start[0];
614 *addr = 0x00F000F0; /* reset bank */
621 /*-----------------------------------------------------------------------
624 int flash_erase (flash_info_t *info, int s_first, int s_last)
626 vu_long *addr = (vu_long*)(info->start[0]);
627 int flag, prot, sect, l_sect;
628 ulong start, now, last;
630 debug ("flash_erase: first: %d last: %d\n", s_first, s_last);
632 if ((s_first < 0) || (s_first > s_last)) {
633 if (info->flash_id == FLASH_UNKNOWN) {
634 printf ("- missing\n");
636 printf ("- no sectors to erase\n");
641 if ((info->flash_id == FLASH_UNKNOWN) ||
642 (info->flash_id > FLASH_AMD_COMP)) {
643 printf ("Can't erase unknown flash type %08lx - aborted\n",
649 for (sect=s_first; sect<=s_last; ++sect) {
650 if (info->protect[sect]) {
656 printf ("- Warning: %d protected sectors will not be erased!\n",
664 /* Disable interrupts which might cause a timeout here */
665 flag = disable_interrupts();
667 addr[0x0555] = 0x00AA00AA;
668 addr[0x02AA] = 0x00550055;
669 addr[0x0555] = 0x00800080;
670 addr[0x0555] = 0x00AA00AA;
671 addr[0x02AA] = 0x00550055;
673 /* Start erase on unprotected sectors */
674 for (sect = s_first; sect<=s_last; sect++) {
675 if (info->protect[sect] == 0) { /* not protected */
676 addr = (vu_long*)(info->start[sect]);
677 addr[0] = 0x00300030;
682 /* re-enable interrupts if necessary */
686 /* wait at least 80us - let's wait 1 ms */
690 * We wait for the last triggered sector
695 start = get_timer (0);
697 addr = (vu_long*)(info->start[l_sect]);
698 while ((addr[0] & 0x00800080) != 0x00800080) {
699 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
700 printf ("Timeout\n");
703 /* show that we're waiting */
704 if ((now - last) > 1000) { /* every second */
711 /* reset to read mode */
712 addr = (volatile unsigned long *)info->start[0];
713 addr[0] = 0x00F000F0; /* reset bank */
719 /*-----------------------------------------------------------------------
720 * Copy memory to flash, returns:
723 * 2 - Flash not erased
726 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
731 wp = (addr & ~3); /* get lower word aligned address */
734 * handle unaligned start bytes
736 if ((l = addr - wp) != 0) {
738 for (i=0, cp=wp; i<l; ++i, ++cp) {
739 data = (data << 8) | (*(uchar *)cp);
741 for (; i<4 && cnt>0; ++i) {
742 data = (data << 8) | *src++;
746 for (; cnt==0 && i<4; ++i, ++cp) {
747 data = (data << 8) | (*(uchar *)cp);
750 if ((rc = write_word(info, wp, data)) != 0) {
757 * handle word aligned part
761 for (i=0; i<4; ++i) {
762 data = (data << 8) | *src++;
764 if ((rc = write_word(info, wp, data)) != 0) {
776 * handle unaligned tail bytes
779 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
780 data = (data << 8) | *src++;
783 for (; i<4; ++i, ++cp) {
784 data = (data << 8) | (*(uchar *)cp);
787 return (write_word(info, wp, data));
790 /*-----------------------------------------------------------------------
791 * Write a word to Flash, returns:
794 * 2 - Flash not erased
796 static int write_word (flash_info_t *info, ulong dest, ulong data)
798 vu_long *addr = (vu_long*)(info->start[0]);
802 /* Check if Flash is (sufficiently) erased */
803 if ((*((vu_long *)dest) & data) != data) {
806 /* Disable interrupts which might cause a timeout here */
807 flag = disable_interrupts();
809 addr[0x0555] = 0x00AA00AA;
810 addr[0x02AA] = 0x00550055;
811 addr[0x0555] = 0x00A000A0;
813 *((vu_long *)dest) = data;
815 /* re-enable interrupts if necessary */
819 /* data polling for D7 */
820 start = get_timer (0);
821 while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
822 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
829 /*-----------------------------------------------------------------------