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(CFG_FLASH_CFI_DRIVER) /* do not use if CFI driver is configured */
38 #if defined(CONFIG_TQM8xxL) && !defined(CONFIG_TQM866M) \
39 && !defined(CONFIG_TQM885D)
40 # ifndef CFG_OR_TIMING_FLASH_AT_50MHZ
41 # define CFG_OR_TIMING_FLASH_AT_50MHZ (OR_ACS_DIV1 | OR_TRLX | OR_CSNT_SAM | \
42 OR_SCY_2_CLK | OR_EHTR | OR_BI)
44 #endif /* CONFIG_TQM8xxL/M, !TQM866M, !TQM885D */
47 #define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
50 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
52 /*-----------------------------------------------------------------------
55 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
56 static int write_word (flash_info_t *info, ulong dest, ulong data);
58 /*-----------------------------------------------------------------------
61 unsigned long flash_init (void)
63 volatile immap_t *immap = (immap_t *)CFG_IMMR;
64 volatile memctl8xx_t *memctl = &immap->im_memctl;
65 unsigned long size_b0, size_b1;
68 #ifdef CFG_OR_TIMING_FLASH_AT_50MHZ
69 int scy, trlx, flash_or_timing, clk_diff;
71 scy = (CFG_OR_TIMING_FLASH_AT_50MHZ & OR_SCY_MSK) >> 4;
72 if (CFG_OR_TIMING_FLASH_AT_50MHZ & OR_TRLX) {
78 /* We assume that each 10MHz of bus clock require 1-clk SCY
81 clk_diff = (gd->bus_clk / 1000000) - 50;
83 /* We need proper rounding here. This is what the "+5" and "-5"
87 scy += (clk_diff + 5) / 10;
89 scy += (clk_diff - 5) / 10;
91 /* For bus frequencies above 50MHz, we want to use relaxed timing
94 if (gd->bus_clk >= 50000000)
107 flash_or_timing = (scy << 4) | trlx |
108 (CFG_OR_TIMING_FLASH_AT_50MHZ & ~(OR_TRLX | OR_SCY_MSK));
110 /* Init: no FLASHes known */
111 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
112 flash_info[i].flash_id = FLASH_UNKNOWN;
115 /* Static FLASH Bank configuration here - FIXME XXX */
117 debug ("\n## Get flash bank 1 size @ 0x%08x\n",FLASH_BASE0_PRELIM);
119 size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
121 debug ("## Get flash bank 2 size @ 0x%08x\n",FLASH_BASE1_PRELIM);
123 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
124 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
125 size_b0, size_b0<<20);
128 size_b1 = flash_get_size((vu_long *)FLASH_BASE1_PRELIM, &flash_info[1]);
130 debug ("## Prelim. Flash bank sizes: %08lx + 0x%08lx\n",size_b0,size_b1);
132 if (size_b1 > size_b0) {
134 "Bank 1 (0x%08lx = %ld MB) > Bank 0 (0x%08lx = %ld MB)\n",
135 size_b1, size_b1<<20,
138 flash_info[0].flash_id = FLASH_UNKNOWN;
139 flash_info[1].flash_id = FLASH_UNKNOWN;
140 flash_info[0].sector_count = -1;
141 flash_info[1].sector_count = -1;
142 flash_info[0].size = 0;
143 flash_info[1].size = 0;
147 debug ("## Before remap: "
148 "BR0: 0x%08x OR0: 0x%08x "
149 "BR1: 0x%08x OR1: 0x%08x\n",
150 memctl->memc_br0, memctl->memc_or0,
151 memctl->memc_br1, memctl->memc_or1);
153 /* Remap FLASH according to real size */
154 #ifndef CFG_OR_TIMING_FLASH_AT_50MHZ
155 memctl->memc_or0 = CFG_OR_TIMING_FLASH | (-size_b0 & OR_AM_MSK);
157 memctl->memc_or0 = flash_or_timing | (-size_b0 & OR_AM_MSK);
159 memctl->memc_br0 = (CFG_FLASH_BASE & BR_BA_MSK) | BR_MS_GPCM | BR_V;
161 debug ("## BR0: 0x%08x OR0: 0x%08x\n",
162 memctl->memc_br0, memctl->memc_or0);
164 /* Re-do sizing to get full correct info */
165 size_b0 = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
167 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
168 /* monitor protection ON by default */
169 debug ("Protect monitor: %08lx ... %08lx\n",
170 (ulong)CFG_MONITOR_BASE,
171 (ulong)CFG_MONITOR_BASE + monitor_flash_len - 1);
173 flash_protect(FLAG_PROTECT_SET,
175 CFG_MONITOR_BASE + monitor_flash_len - 1,
179 #ifdef CFG_ENV_IS_IN_FLASH
180 /* ENV protection ON by default */
181 # ifdef CFG_ENV_ADDR_REDUND
182 debug ("Protect primary environment: %08lx ... %08lx\n",
184 (ulong)CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1);
186 debug ("Protect environment: %08lx ... %08lx\n",
188 (ulong)CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1);
191 flash_protect(FLAG_PROTECT_SET,
193 CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1,
197 #ifdef CFG_ENV_ADDR_REDUND
198 debug ("Protect redundand environment: %08lx ... %08lx\n",
199 (ulong)CFG_ENV_ADDR_REDUND,
200 (ulong)CFG_ENV_ADDR_REDUND + CFG_ENV_SECT_SIZE - 1);
202 flash_protect(FLAG_PROTECT_SET,
204 CFG_ENV_ADDR_REDUND + CFG_ENV_SECT_SIZE - 1,
209 #ifndef CFG_OR_TIMING_FLASH_AT_50MHZ
210 memctl->memc_or1 = CFG_OR_TIMING_FLASH | (-size_b1 & 0xFFFF8000);
212 memctl->memc_or1 = flash_or_timing | (-size_b1 & 0xFFFF8000);
214 memctl->memc_br1 = ((CFG_FLASH_BASE + size_b0) & BR_BA_MSK) |
217 debug ("## BR1: 0x%08x OR1: 0x%08x\n",
218 memctl->memc_br1, memctl->memc_or1);
220 /* Re-do sizing to get full correct info */
221 size_b1 = flash_get_size((vu_long *)(CFG_FLASH_BASE + size_b0),
224 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
225 /* monitor protection ON by default */
226 flash_protect(FLAG_PROTECT_SET,
228 CFG_MONITOR_BASE+monitor_flash_len-1,
232 #ifdef CFG_ENV_IS_IN_FLASH
233 /* ENV protection ON by default */
234 flash_protect(FLAG_PROTECT_SET,
236 CFG_ENV_ADDR+CFG_ENV_SIZE-1,
240 memctl->memc_br1 = 0; /* invalidate bank */
242 flash_info[1].flash_id = FLASH_UNKNOWN;
243 flash_info[1].sector_count = -1;
244 flash_info[1].size = 0;
246 debug ("## DISABLE BR1: 0x%08x OR1: 0x%08x\n",
247 memctl->memc_br1, memctl->memc_or1);
250 debug ("## Final Flash bank sizes: %08lx + 0x%08lx\n",size_b0,size_b1);
252 flash_info[0].size = size_b0;
253 flash_info[1].size = size_b1;
255 return (size_b0 + size_b1);
258 /*-----------------------------------------------------------------------
260 void flash_print_info (flash_info_t *info)
264 if (info->flash_id == FLASH_UNKNOWN) {
265 printf ("missing or unknown FLASH type\n");
269 switch (info->flash_id & FLASH_VENDMASK) {
270 case FLASH_MAN_AMD: printf ("AMD "); break;
271 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
272 default: printf ("Unknown Vendor "); break;
275 switch (info->flash_id & FLASH_TYPEMASK) {
276 #ifdef CONFIG_TQM8xxM /* mirror bit flash */
277 case FLASH_AMLV128U: printf ("AM29LV128ML (128Mbit, uniform sector size)\n");
279 case FLASH_AMLV320U: printf ("AM29LV320ML (32Mbit, uniform sector size)\n");
281 case FLASH_AMLV640U: printf ("AM29LV640ML (64Mbit, uniform sector size)\n");
283 case FLASH_AMLV320B: printf ("AM29LV320MB (32Mbit, bottom boot sect)\n");
285 # else /* ! TQM8xxM */
286 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
288 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
290 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
292 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
294 case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
296 case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
299 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
301 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
303 case FLASH_AMDL163B: printf ("AM29DL163B (16 Mbit, bottom boot sect)\n");
305 default: printf ("Unknown Chip Type\n");
309 printf (" Size: %ld MB in %d Sectors\n",
310 info->size >> 20, info->sector_count);
312 printf (" Sector Start Addresses:");
313 for (i=0; i<info->sector_count; ++i) {
318 info->protect[i] ? " (RO)" : " "
325 /*-----------------------------------------------------------------------
329 /*-----------------------------------------------------------------------
333 * The following code cannot be run from FLASH!
336 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
340 ulong base = (ulong)addr;
342 /* Write auto select command: read Manufacturer ID */
343 addr[0x0555] = 0x00AA00AA;
344 addr[0x02AA] = 0x00550055;
345 addr[0x0555] = 0x00900090;
349 debug ("Manuf. ID @ 0x%08lx: 0x%08lx\n", (ulong)addr, value);
353 debug ("Manufacturer: AMD\n");
354 info->flash_id = FLASH_MAN_AMD;
357 debug ("Manufacturer: FUJITSU\n");
358 info->flash_id = FLASH_MAN_FUJ;
361 debug ("Manufacturer: *** unknown ***\n");
362 info->flash_id = FLASH_UNKNOWN;
363 info->sector_count = 0;
365 return (0); /* no or unknown flash */
368 value = addr[1]; /* device ID */
370 debug ("Device ID @ 0x%08lx: 0x%08lx\n", (ulong)(&addr[1]), value);
373 #ifdef CONFIG_TQM8xxM /* mirror bit flash */
375 debug ("Mirror Bit flash: addr[14] = %08lX addr[15] = %08lX\n",
377 /* Special case for AMLV320MH/L */
378 if ((addr[14] & 0x00ff00ff) == 0x001d001d &&
379 (addr[15] & 0x00ff00ff) == 0x00000000) {
380 debug ("Chip: AMLV320MH/L\n");
381 info->flash_id += FLASH_AMLV320U;
382 info->sector_count = 64;
383 info->size = 0x00800000; /* => 8 MB */
387 case AMD_ID_LV128U_2:
388 if (addr[15] != AMD_ID_LV128U_3) {
389 debug ("Chip: AMLV128U -> unknown\n");
390 info->flash_id = FLASH_UNKNOWN;
392 debug ("Chip: AMLV128U\n");
393 info->flash_id += FLASH_AMLV128U;
394 info->sector_count = 256;
395 info->size = 0x02000000;
397 break; /* => 32 MB */
398 case AMD_ID_LV640U_2:
399 if (addr[15] != AMD_ID_LV640U_3) {
400 debug ("Chip: AMLV640U -> unknown\n");
401 info->flash_id = FLASH_UNKNOWN;
403 debug ("Chip: AMLV640U\n");
404 info->flash_id += FLASH_AMLV640U;
405 info->sector_count = 128;
406 info->size = 0x01000000;
408 break; /* => 16 MB */
409 case AMD_ID_LV320B_2:
410 if (addr[15] != AMD_ID_LV320B_3) {
411 debug ("Chip: AMLV320B -> unknown\n");
412 info->flash_id = FLASH_UNKNOWN;
414 debug ("Chip: AMLV320B\n");
415 info->flash_id += FLASH_AMLV320B;
416 info->sector_count = 71;
417 info->size = 0x00800000;
421 debug ("Chip: *** unknown ***\n");
422 info->flash_id = FLASH_UNKNOWN;
426 # else /* ! TQM8xxM */
428 info->flash_id += FLASH_AM400T;
429 info->sector_count = 11;
430 info->size = 0x00100000;
434 info->flash_id += FLASH_AM400B;
435 info->sector_count = 11;
436 info->size = 0x00100000;
440 info->flash_id += FLASH_AM800T;
441 info->sector_count = 19;
442 info->size = 0x00200000;
446 info->flash_id += FLASH_AM800B;
447 info->sector_count = 19;
448 info->size = 0x00200000;
452 info->flash_id += FLASH_AM320T;
453 info->sector_count = 71;
454 info->size = 0x00800000;
458 info->flash_id += FLASH_AM320B;
459 info->sector_count = 71;
460 info->size = 0x00800000;
465 info->flash_id += FLASH_AM160T;
466 info->sector_count = 35;
467 info->size = 0x00400000;
471 info->flash_id += FLASH_AM160B;
472 info->sector_count = 35;
473 info->size = 0x00400000;
477 info->flash_id += FLASH_AMDL163B;
478 info->sector_count = 39;
479 info->size = 0x00400000;
483 info->flash_id = FLASH_UNKNOWN;
484 return (0); /* => no or unknown flash */
487 /* set up sector start address table */
489 #ifdef CONFIG_TQM8xxM /* mirror bit flash */
491 switch (info->flash_id & FLASH_TYPEMASK) {
492 /* only known types here - no default */
496 for (i = 0; i < info->sector_count; i++) {
497 info->start[i] = base;
502 for (i = 0; i < info->sector_count; i++) {
503 info->start[i] = base;
505 * The first 8 sectors are 8 kB,
506 * all the other ones are 64 kB
515 # else /* ! TQM8xxM */
518 /* set sector offsets for bottom boot block type */
519 info->start[0] = base + 0x00000000;
520 info->start[1] = base + 0x00008000;
521 info->start[2] = base + 0x0000C000;
522 info->start[3] = base + 0x00010000;
523 for (i = 4; i < info->sector_count; i++) {
524 info->start[i] = base + (i * 0x00020000) - 0x00060000;
529 /* set sector offsets for top boot block type */
530 i = info->sector_count - 1;
531 info->start[i--] = base + info->size - 0x00008000;
532 info->start[i--] = base + info->size - 0x0000C000;
533 info->start[i--] = base + info->size - 0x00010000;
534 for (; i >= 0; i--) {
535 info->start[i] = base + i * 0x00020000;
539 for (i = 0; i < info->sector_count; i++) {
540 info->start[i] = base;
542 * The first 8 sectors are 8 kB,
543 * all the other ones are 64 kB
551 for (i = 0; i < info->sector_count; i++) {
552 info->start[i] = base;
554 * The last 8 sectors are 8 kB,
555 * all the other ones are 64 kB
557 base += (i < (info->sector_count - 8))
564 /* set sector offsets for bottom boot block type */
565 info->start[0] = base + 0x00000000;
566 info->start[1] = base + 0x00008000;
567 info->start[2] = base + 0x0000C000;
568 info->start[3] = base + 0x00010000;
569 for (i = 4; i < info->sector_count; i++) {
570 info->start[i] = base + (i * 0x00020000) - 0x00060000;
574 /* set sector offsets for top boot block type */
575 i = info->sector_count - 1;
576 info->start[i--] = base + info->size - 0x00008000;
577 info->start[i--] = base + info->size - 0x0000C000;
578 info->start[i--] = base + info->size - 0x00010000;
579 for (; i >= 0; i--) {
580 info->start[i] = base + i * 0x00020000;
584 for (i = 0; i < info->sector_count; i++) {
585 info->start[i] = base;
587 * The first 8 sectors are 8 kB,
588 * all the other ones are 64 kB
601 /* check for protected sectors */
602 for (i = 0; i < info->sector_count; i++) {
603 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
604 /* D0 = 1 if protected */
605 addr = (volatile unsigned long *)(info->start[i]);
606 info->protect[i] = addr[2] & 1;
611 * Prevent writes to uninitialized FLASH.
613 if (info->flash_id != FLASH_UNKNOWN) {
614 addr = (volatile unsigned long *)info->start[0];
616 *addr = 0x00F000F0; /* reset bank */
623 /*-----------------------------------------------------------------------
626 int flash_erase (flash_info_t *info, int s_first, int s_last)
628 vu_long *addr = (vu_long*)(info->start[0]);
629 int flag, prot, sect, l_sect;
630 ulong start, now, last;
632 debug ("flash_erase: first: %d last: %d\n", s_first, s_last);
634 if ((s_first < 0) || (s_first > s_last)) {
635 if (info->flash_id == FLASH_UNKNOWN) {
636 printf ("- missing\n");
638 printf ("- no sectors to erase\n");
643 if ((info->flash_id == FLASH_UNKNOWN) ||
644 (info->flash_id > FLASH_AMD_COMP)) {
645 printf ("Can't erase unknown flash type %08lx - aborted\n",
651 for (sect=s_first; sect<=s_last; ++sect) {
652 if (info->protect[sect]) {
658 printf ("- Warning: %d protected sectors will not be erased!\n",
666 /* Disable interrupts which might cause a timeout here */
667 flag = disable_interrupts();
669 addr[0x0555] = 0x00AA00AA;
670 addr[0x02AA] = 0x00550055;
671 addr[0x0555] = 0x00800080;
672 addr[0x0555] = 0x00AA00AA;
673 addr[0x02AA] = 0x00550055;
675 /* Start erase on unprotected sectors */
676 for (sect = s_first; sect<=s_last; sect++) {
677 if (info->protect[sect] == 0) { /* not protected */
678 addr = (vu_long*)(info->start[sect]);
679 addr[0] = 0x00300030;
684 /* re-enable interrupts if necessary */
688 /* wait at least 80us - let's wait 1 ms */
692 * We wait for the last triggered sector
697 start = get_timer (0);
699 addr = (vu_long*)(info->start[l_sect]);
700 while ((addr[0] & 0x00800080) != 0x00800080) {
701 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
702 printf ("Timeout\n");
705 /* show that we're waiting */
706 if ((now - last) > 1000) { /* every second */
713 /* reset to read mode */
714 addr = (volatile unsigned long *)info->start[0];
715 addr[0] = 0x00F000F0; /* reset bank */
721 /*-----------------------------------------------------------------------
722 * Copy memory to flash, returns:
725 * 2 - Flash not erased
728 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
733 wp = (addr & ~3); /* get lower word aligned address */
736 * handle unaligned start bytes
738 if ((l = addr - wp) != 0) {
740 for (i=0, cp=wp; i<l; ++i, ++cp) {
741 data = (data << 8) | (*(uchar *)cp);
743 for (; i<4 && cnt>0; ++i) {
744 data = (data << 8) | *src++;
748 for (; cnt==0 && i<4; ++i, ++cp) {
749 data = (data << 8) | (*(uchar *)cp);
752 if ((rc = write_word(info, wp, data)) != 0) {
759 * handle word aligned part
763 for (i=0; i<4; ++i) {
764 data = (data << 8) | *src++;
766 if ((rc = write_word(info, wp, data)) != 0) {
778 * handle unaligned tail bytes
781 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
782 data = (data << 8) | *src++;
785 for (; i<4; ++i, ++cp) {
786 data = (data << 8) | (*(uchar *)cp);
789 return (write_word(info, wp, data));
792 /*-----------------------------------------------------------------------
793 * Write a word to Flash, returns:
796 * 2 - Flash not erased
798 static int write_word (flash_info_t *info, ulong dest, ulong data)
800 vu_long *addr = (vu_long*)(info->start[0]);
804 /* Check if Flash is (sufficiently) erased */
805 if ((*((vu_long *)dest) & data) != data) {
808 /* Disable interrupts which might cause a timeout here */
809 flag = disable_interrupts();
811 addr[0x0555] = 0x00AA00AA;
812 addr[0x02AA] = 0x00550055;
813 addr[0x0555] = 0x00A000A0;
815 *((vu_long *)dest) = data;
817 /* re-enable interrupts if necessary */
821 /* data polling for D7 */
822 start = get_timer (0);
823 while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
824 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
831 /*-----------------------------------------------------------------------
834 #endif /* !defined(CFG_FLASH_CFI_DRIVER) */