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 #if defined(CONFIG_TQM8xxL) && !defined(CONFIG_TQM866M)
35 # ifndef CFG_OR_TIMING_FLASH_AT_50MHZ
36 # define CFG_OR_TIMING_FLASH_AT_50MHZ (OR_ACS_DIV1 | OR_TRLX | OR_CSNT_SAM | \
37 OR_SCY_2_CLK | OR_EHTR | OR_BI)
39 #endif /* CONFIG_TQM8xxL/M, !TQM866M */
42 #define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
45 flash_info_t flash_info
[CFG_MAX_FLASH_BANKS
]; /* info for FLASH chips */
47 /*-----------------------------------------------------------------------
50 static ulong
flash_get_size (vu_long
*addr
, flash_info_t
*info
);
51 static int write_word (flash_info_t
*info
, ulong dest
, ulong data
);
53 /*-----------------------------------------------------------------------
56 unsigned long flash_init (void)
58 volatile immap_t
*immap
= (immap_t
*)CFG_IMMR
;
59 volatile memctl8xx_t
*memctl
= &immap
->im_memctl
;
60 unsigned long size_b0
, size_b1
;
63 #ifdef CFG_OR_TIMING_FLASH_AT_50MHZ
64 int scy
, trlx
, flash_or_timing
, clk_diff
;
66 DECLARE_GLOBAL_DATA_PTR
;
68 scy
= (CFG_OR_TIMING_FLASH_AT_50MHZ
& OR_SCY_MSK
) >> 4;
69 if (CFG_OR_TIMING_FLASH_AT_50MHZ
& OR_TRLX
) {
75 /* We assume that each 10MHz of bus clock require 1-clk SCY
78 clk_diff
= (gd
->bus_clk
/ 1000000) - 50;
80 /* We need proper rounding here. This is what the "+5" and "-5"
84 scy
+= (clk_diff
+ 5) / 10;
86 scy
+= (clk_diff
- 5) / 10;
88 /* For bus frequencies above 50MHz, we want to use relaxed timing
91 if (gd
->bus_clk
>= 50000000)
104 flash_or_timing
= (scy
<< 4) | trlx
|
105 (CFG_OR_TIMING_FLASH_AT_50MHZ
& ~(OR_TRLX
| OR_SCY_MSK
));
107 /* Init: no FLASHes known */
108 for (i
=0; i
<CFG_MAX_FLASH_BANKS
; ++i
) {
109 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
112 /* Static FLASH Bank configuration here - FIXME XXX */
114 debug ("\n## Get flash bank 1 size @ 0x%08x\n",FLASH_BASE0_PRELIM
);
116 size_b0
= flash_get_size((vu_long
*)FLASH_BASE0_PRELIM
, &flash_info
[0]);
118 debug ("## Get flash bank 2 size @ 0x%08x\n",FLASH_BASE1_PRELIM
);
120 if (flash_info
[0].flash_id
== FLASH_UNKNOWN
) {
121 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
122 size_b0
, size_b0
<<20);
125 size_b1
= flash_get_size((vu_long
*)FLASH_BASE1_PRELIM
, &flash_info
[1]);
127 debug ("## Prelim. Flash bank sizes: %08lx + 0x%08lx\n",size_b0
,size_b1
);
129 if (size_b1
> size_b0
) {
131 "Bank 1 (0x%08lx = %ld MB) > Bank 0 (0x%08lx = %ld MB)\n",
132 size_b1
, size_b1
<<20,
135 flash_info
[0].flash_id
= FLASH_UNKNOWN
;
136 flash_info
[1].flash_id
= FLASH_UNKNOWN
;
137 flash_info
[0].sector_count
= -1;
138 flash_info
[1].sector_count
= -1;
139 flash_info
[0].size
= 0;
140 flash_info
[1].size
= 0;
144 debug ("## Before remap: "
145 "BR0: 0x%08x OR0: 0x%08x "
146 "BR1: 0x%08x OR1: 0x%08x\n",
147 memctl
->memc_br0
, memctl
->memc_or0
,
148 memctl
->memc_br1
, memctl
->memc_or1
);
150 /* Remap FLASH according to real size */
151 #ifndef CFG_OR_TIMING_FLASH_AT_50MHZ
152 memctl
->memc_or0
= CFG_OR_TIMING_FLASH
| (-size_b0
& OR_AM_MSK
);
154 memctl
->memc_or0
= flash_or_timing
| (-size_b0
& OR_AM_MSK
);
156 memctl
->memc_br0
= (CFG_FLASH_BASE
& BR_BA_MSK
) | BR_MS_GPCM
| BR_V
;
158 debug ("## BR0: 0x%08x OR0: 0x%08x\n",
159 memctl
->memc_br0
, memctl
->memc_or0
);
161 /* Re-do sizing to get full correct info */
162 size_b0
= flash_get_size((vu_long
*)CFG_FLASH_BASE
, &flash_info
[0]);
164 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
165 /* monitor protection ON by default */
166 debug ("Protect monitor: %08lx ... %08lx\n",
167 (ulong
)CFG_MONITOR_BASE
,
168 (ulong
)CFG_MONITOR_BASE
+ monitor_flash_len
- 1);
170 flash_protect(FLAG_PROTECT_SET
,
172 CFG_MONITOR_BASE
+ monitor_flash_len
- 1,
176 #ifdef CFG_ENV_IS_IN_FLASH
177 /* ENV protection ON by default */
178 # ifdef CFG_ENV_ADDR_REDUND
179 debug ("Protect primary environment: %08lx ... %08lx\n",
181 (ulong
)CFG_ENV_ADDR
+ CFG_ENV_SECT_SIZE
- 1);
183 debug ("Protect environment: %08lx ... %08lx\n",
185 (ulong
)CFG_ENV_ADDR
+ CFG_ENV_SECT_SIZE
- 1);
188 flash_protect(FLAG_PROTECT_SET
,
190 CFG_ENV_ADDR
+ CFG_ENV_SECT_SIZE
- 1,
194 #ifdef CFG_ENV_ADDR_REDUND
195 debug ("Protect redundand environment: %08lx ... %08lx\n",
196 (ulong
)CFG_ENV_ADDR_REDUND
,
197 (ulong
)CFG_ENV_ADDR_REDUND
+ CFG_ENV_SECT_SIZE
- 1);
199 flash_protect(FLAG_PROTECT_SET
,
201 CFG_ENV_ADDR_REDUND
+ CFG_ENV_SECT_SIZE
- 1,
206 #ifndef CFG_OR_TIMING_FLASH_AT_50MHZ
207 memctl
->memc_or1
= CFG_OR_TIMING_FLASH
| (-size_b1
& 0xFFFF8000);
209 memctl
->memc_or1
= flash_or_timing
| (-size_b1
& 0xFFFF8000);
211 memctl
->memc_br1
= ((CFG_FLASH_BASE
+ size_b0
) & BR_BA_MSK
) |
214 debug ("## BR1: 0x%08x OR1: 0x%08x\n",
215 memctl
->memc_br1
, memctl
->memc_or1
);
217 /* Re-do sizing to get full correct info */
218 size_b1
= flash_get_size((vu_long
*)(CFG_FLASH_BASE
+ size_b0
),
221 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
222 /* monitor protection ON by default */
223 flash_protect(FLAG_PROTECT_SET
,
225 CFG_MONITOR_BASE
+monitor_flash_len
-1,
229 #ifdef CFG_ENV_IS_IN_FLASH
230 /* ENV protection ON by default */
231 flash_protect(FLAG_PROTECT_SET
,
233 CFG_ENV_ADDR
+CFG_ENV_SIZE
-1,
237 memctl
->memc_br1
= 0; /* invalidate bank */
239 flash_info
[1].flash_id
= FLASH_UNKNOWN
;
240 flash_info
[1].sector_count
= -1;
241 flash_info
[1].size
= 0;
243 debug ("## DISABLE BR1: 0x%08x OR1: 0x%08x\n",
244 memctl
->memc_br1
, memctl
->memc_or1
);
247 debug ("## Final Flash bank sizes: %08lx + 0x%08lx\n",size_b0
,size_b1
);
249 flash_info
[0].size
= size_b0
;
250 flash_info
[1].size
= size_b1
;
252 return (size_b0
+ size_b1
);
255 /*-----------------------------------------------------------------------
257 void flash_print_info (flash_info_t
*info
)
261 if (info
->flash_id
== FLASH_UNKNOWN
) {
262 printf ("missing or unknown FLASH type\n");
266 switch (info
->flash_id
& FLASH_VENDMASK
) {
267 case FLASH_MAN_AMD
: printf ("AMD "); break;
268 case FLASH_MAN_FUJ
: printf ("FUJITSU "); break;
269 default: printf ("Unknown Vendor "); break;
272 switch (info
->flash_id
& FLASH_TYPEMASK
) {
273 #ifdef CONFIG_TQM8xxM /* mirror bit flash */
274 case FLASH_AMLV128U
: printf ("AM29LV128ML (128Mbit, uniform sector size)\n");
276 case FLASH_AMLV320U
: printf ("AM29LV320ML (32Mbit, uniform sector size)\n");
278 case FLASH_AMLV640U
: printf ("AM29LV640ML (64Mbit, uniform sector size)\n");
280 case FLASH_AMLV320B
: printf ("AM29LV320MB (32Mbit, bottom boot sect)\n");
282 # else /* ! TQM8xxM */
283 case FLASH_AM400B
: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
285 case FLASH_AM400T
: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
287 case FLASH_AM800B
: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
289 case FLASH_AM800T
: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
291 case FLASH_AM320B
: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
293 case FLASH_AM320T
: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
296 case FLASH_AM160B
: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
298 case FLASH_AM160T
: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
300 case FLASH_AMDL163B
: printf ("AM29DL163B (16 Mbit, bottom boot sect)\n");
302 default: printf ("Unknown Chip Type\n");
306 printf (" Size: %ld MB in %d Sectors\n",
307 info
->size
>> 20, info
->sector_count
);
309 printf (" Sector Start Addresses:");
310 for (i
=0; i
<info
->sector_count
; ++i
) {
315 info
->protect
[i
] ? " (RO)" : " "
322 /*-----------------------------------------------------------------------
326 /*-----------------------------------------------------------------------
330 * The following code cannot be run from FLASH!
333 static ulong
flash_get_size (vu_long
*addr
, flash_info_t
*info
)
337 ulong base
= (ulong
)addr
;
339 /* Write auto select command: read Manufacturer ID */
340 addr
[0x0555] = 0x00AA00AA;
341 addr
[0x02AA] = 0x00550055;
342 addr
[0x0555] = 0x00900090;
346 debug ("Manuf. ID @ 0x%08lx: 0x%08lx\n", (ulong
)addr
, value
);
350 debug ("Manufacturer: AMD\n");
351 info
->flash_id
= FLASH_MAN_AMD
;
354 debug ("Manufacturer: FUJITSU\n");
355 info
->flash_id
= FLASH_MAN_FUJ
;
358 debug ("Manufacturer: *** unknown ***\n");
359 info
->flash_id
= FLASH_UNKNOWN
;
360 info
->sector_count
= 0;
362 return (0); /* no or unknown flash */
365 value
= addr
[1]; /* device ID */
367 debug ("Device ID @ 0x%08lx: 0x%08lx\n", (ulong
)(&addr
[1]), value
);
370 #ifdef CONFIG_TQM8xxM /* mirror bit flash */
372 debug ("Mirror Bit flash: addr[14] = %08lX addr[15] = %08lX\n",
374 /* Special case for AMLV320MH/L */
375 if ((addr
[14] & 0x00ff00ff) == 0x001d001d &&
376 (addr
[15] & 0x00ff00ff) == 0x00000000) {
377 debug ("Chip: AMLV320MH/L\n");
378 info
->flash_id
+= FLASH_AMLV320U
;
379 info
->sector_count
= 64;
380 info
->size
= 0x00800000; /* => 8 MB */
384 case AMD_ID_LV128U_2
:
385 if (addr
[15] != AMD_ID_LV128U_3
) {
386 debug ("Chip: AMLV128U -> unknown\n");
387 info
->flash_id
= FLASH_UNKNOWN
;
389 debug ("Chip: AMLV128U\n");
390 info
->flash_id
+= FLASH_AMLV128U
;
391 info
->sector_count
= 256;
392 info
->size
= 0x02000000;
394 break; /* => 32 MB */
395 case AMD_ID_LV640U_2
:
396 if (addr
[15] != AMD_ID_LV640U_3
) {
397 debug ("Chip: AMLV640U -> unknown\n");
398 info
->flash_id
= FLASH_UNKNOWN
;
400 debug ("Chip: AMLV640U\n");
401 info
->flash_id
+= FLASH_AMLV640U
;
402 info
->sector_count
= 128;
403 info
->size
= 0x01000000;
405 break; /* => 16 MB */
406 case AMD_ID_LV320B_2
:
407 if (addr
[15] != AMD_ID_LV320B_3
) {
408 debug ("Chip: AMLV320B -> unknown\n");
409 info
->flash_id
= FLASH_UNKNOWN
;
411 debug ("Chip: AMLV320B\n");
412 info
->flash_id
+= FLASH_AMLV320B
;
413 info
->sector_count
= 71;
414 info
->size
= 0x00800000;
418 debug ("Chip: *** unknown ***\n");
419 info
->flash_id
= FLASH_UNKNOWN
;
423 # else /* ! TQM8xxM */
425 info
->flash_id
+= FLASH_AM400T
;
426 info
->sector_count
= 11;
427 info
->size
= 0x00100000;
431 info
->flash_id
+= FLASH_AM400B
;
432 info
->sector_count
= 11;
433 info
->size
= 0x00100000;
437 info
->flash_id
+= FLASH_AM800T
;
438 info
->sector_count
= 19;
439 info
->size
= 0x00200000;
443 info
->flash_id
+= FLASH_AM800B
;
444 info
->sector_count
= 19;
445 info
->size
= 0x00200000;
449 info
->flash_id
+= FLASH_AM320T
;
450 info
->sector_count
= 71;
451 info
->size
= 0x00800000;
455 info
->flash_id
+= FLASH_AM320B
;
456 info
->sector_count
= 71;
457 info
->size
= 0x00800000;
462 info
->flash_id
+= FLASH_AM160T
;
463 info
->sector_count
= 35;
464 info
->size
= 0x00400000;
468 info
->flash_id
+= FLASH_AM160B
;
469 info
->sector_count
= 35;
470 info
->size
= 0x00400000;
474 info
->flash_id
+= FLASH_AMDL163B
;
475 info
->sector_count
= 39;
476 info
->size
= 0x00400000;
480 info
->flash_id
= FLASH_UNKNOWN
;
481 return (0); /* => no or unknown flash */
484 /* set up sector start address table */
486 #ifdef CONFIG_TQM8xxM /* mirror bit flash */
488 switch (info
->flash_id
& FLASH_TYPEMASK
) {
489 /* only known types here - no default */
493 for (i
= 0; i
< info
->sector_count
; i
++) {
494 info
->start
[i
] = base
;
499 for (i
= 0; i
< info
->sector_count
; i
++) {
500 info
->start
[i
] = base
;
502 * The first 8 sectors are 8 kB,
503 * all the other ones are 64 kB
512 # else /* ! TQM8xxM */
515 /* set sector offsets for bottom boot block type */
516 info
->start
[0] = base
+ 0x00000000;
517 info
->start
[1] = base
+ 0x00008000;
518 info
->start
[2] = base
+ 0x0000C000;
519 info
->start
[3] = base
+ 0x00010000;
520 for (i
= 4; i
< info
->sector_count
; i
++) {
521 info
->start
[i
] = base
+ (i
* 0x00020000) - 0x00060000;
526 /* set sector offsets for top boot block type */
527 i
= info
->sector_count
- 1;
528 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
529 info
->start
[i
--] = base
+ info
->size
- 0x0000C000;
530 info
->start
[i
--] = base
+ info
->size
- 0x00010000;
531 for (; i
>= 0; i
--) {
532 info
->start
[i
] = base
+ i
* 0x00020000;
536 for (i
= 0; i
< info
->sector_count
; i
++) {
537 info
->start
[i
] = base
;
539 * The first 8 sectors are 8 kB,
540 * all the other ones are 64 kB
548 for (i
= 0; i
< info
->sector_count
; i
++) {
549 info
->start
[i
] = base
;
551 * The last 8 sectors are 8 kB,
552 * all the other ones are 64 kB
554 base
+= (i
< (info
->sector_count
- 8))
561 /* set sector offsets for bottom boot block type */
562 info
->start
[0] = base
+ 0x00000000;
563 info
->start
[1] = base
+ 0x00008000;
564 info
->start
[2] = base
+ 0x0000C000;
565 info
->start
[3] = base
+ 0x00010000;
566 for (i
= 4; i
< info
->sector_count
; i
++) {
567 info
->start
[i
] = base
+ (i
* 0x00020000) - 0x00060000;
571 /* set sector offsets for top boot block type */
572 i
= info
->sector_count
- 1;
573 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
574 info
->start
[i
--] = base
+ info
->size
- 0x0000C000;
575 info
->start
[i
--] = base
+ info
->size
- 0x00010000;
576 for (; i
>= 0; i
--) {
577 info
->start
[i
] = base
+ i
* 0x00020000;
581 for (i
= 0; i
< info
->sector_count
; i
++) {
582 info
->start
[i
] = base
;
584 * The first 8 sectors are 8 kB,
585 * all the other ones are 64 kB
598 /* check for protected sectors */
599 for (i
= 0; i
< info
->sector_count
; i
++) {
600 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
601 /* D0 = 1 if protected */
602 addr
= (volatile unsigned long *)(info
->start
[i
]);
603 info
->protect
[i
] = addr
[2] & 1;
608 * Prevent writes to uninitialized FLASH.
610 if (info
->flash_id
!= FLASH_UNKNOWN
) {
611 addr
= (volatile unsigned long *)info
->start
[0];
613 *addr
= 0x00F000F0; /* reset bank */
620 /*-----------------------------------------------------------------------
623 int flash_erase (flash_info_t
*info
, int s_first
, int s_last
)
625 vu_long
*addr
= (vu_long
*)(info
->start
[0]);
626 int flag
, prot
, sect
, l_sect
;
627 ulong start
, now
, last
;
629 debug ("flash_erase: first: %d last: %d\n", s_first
, s_last
);
631 if ((s_first
< 0) || (s_first
> s_last
)) {
632 if (info
->flash_id
== FLASH_UNKNOWN
) {
633 printf ("- missing\n");
635 printf ("- no sectors to erase\n");
640 if ((info
->flash_id
== FLASH_UNKNOWN
) ||
641 (info
->flash_id
> FLASH_AMD_COMP
)) {
642 printf ("Can't erase unknown flash type %08lx - aborted\n",
648 for (sect
=s_first
; sect
<=s_last
; ++sect
) {
649 if (info
->protect
[sect
]) {
655 printf ("- Warning: %d protected sectors will not be erased!\n",
663 /* Disable interrupts which might cause a timeout here */
664 flag
= disable_interrupts();
666 addr
[0x0555] = 0x00AA00AA;
667 addr
[0x02AA] = 0x00550055;
668 addr
[0x0555] = 0x00800080;
669 addr
[0x0555] = 0x00AA00AA;
670 addr
[0x02AA] = 0x00550055;
672 /* Start erase on unprotected sectors */
673 for (sect
= s_first
; sect
<=s_last
; sect
++) {
674 if (info
->protect
[sect
] == 0) { /* not protected */
675 addr
= (vu_long
*)(info
->start
[sect
]);
676 addr
[0] = 0x00300030;
681 /* re-enable interrupts if necessary */
685 /* wait at least 80us - let's wait 1 ms */
689 * We wait for the last triggered sector
694 start
= get_timer (0);
696 addr
= (vu_long
*)(info
->start
[l_sect
]);
697 while ((addr
[0] & 0x00800080) != 0x00800080) {
698 if ((now
= get_timer(start
)) > CFG_FLASH_ERASE_TOUT
) {
699 printf ("Timeout\n");
702 /* show that we're waiting */
703 if ((now
- last
) > 1000) { /* every second */
710 /* reset to read mode */
711 addr
= (volatile unsigned long *)info
->start
[0];
712 addr
[0] = 0x00F000F0; /* reset bank */
718 /*-----------------------------------------------------------------------
719 * Copy memory to flash, returns:
722 * 2 - Flash not erased
725 int write_buff (flash_info_t
*info
, uchar
*src
, ulong addr
, ulong cnt
)
730 wp
= (addr
& ~3); /* get lower word aligned address */
733 * handle unaligned start bytes
735 if ((l
= addr
- wp
) != 0) {
737 for (i
=0, cp
=wp
; i
<l
; ++i
, ++cp
) {
738 data
= (data
<< 8) | (*(uchar
*)cp
);
740 for (; i
<4 && cnt
>0; ++i
) {
741 data
= (data
<< 8) | *src
++;
745 for (; cnt
==0 && i
<4; ++i
, ++cp
) {
746 data
= (data
<< 8) | (*(uchar
*)cp
);
749 if ((rc
= write_word(info
, wp
, data
)) != 0) {
756 * handle word aligned part
760 for (i
=0; i
<4; ++i
) {
761 data
= (data
<< 8) | *src
++;
763 if ((rc
= write_word(info
, wp
, data
)) != 0) {
775 * handle unaligned tail bytes
778 for (i
=0, cp
=wp
; i
<4 && cnt
>0; ++i
, ++cp
) {
779 data
= (data
<< 8) | *src
++;
782 for (; i
<4; ++i
, ++cp
) {
783 data
= (data
<< 8) | (*(uchar
*)cp
);
786 return (write_word(info
, wp
, data
));
789 /*-----------------------------------------------------------------------
790 * Write a word to Flash, returns:
793 * 2 - Flash not erased
795 static int write_word (flash_info_t
*info
, ulong dest
, ulong data
)
797 vu_long
*addr
= (vu_long
*)(info
->start
[0]);
801 /* Check if Flash is (sufficiently) erased */
802 if ((*((vu_long
*)dest
) & data
) != data
) {
805 /* Disable interrupts which might cause a timeout here */
806 flag
= disable_interrupts();
808 addr
[0x0555] = 0x00AA00AA;
809 addr
[0x02AA] = 0x00550055;
810 addr
[0x0555] = 0x00A000A0;
812 *((vu_long
*)dest
) = data
;
814 /* re-enable interrupts if necessary */
818 /* data polling for D7 */
819 start
= get_timer (0);
820 while ((*((vu_long
*)dest
) & 0x00800080) != (data
& 0x00800080)) {
821 if (get_timer(start
) > CFG_FLASH_WRITE_TOUT
) {
828 /*-----------------------------------------------------------------------