2 * (C) Copyright 2000, 2001
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,
25 * Derived from ../tqm8xx/flash.c
26 * [Torsten Stevens, FHG IMS; Bruno Achauer, Exet AG]
32 #if defined(CONFIG_ENV_IS_IN_FLASH)
33 # ifndef CONFIG_ENV_ADDR
34 # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
36 # ifndef CONFIG_ENV_SIZE
37 # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
39 # ifndef CONFIG_ENV_SECT_SIZE
40 # define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
44 /*---------------------------------------------------------------------*/
48 #define DEBUGF(fmt,args...) printf(fmt ,##args)
50 #define DEBUGF(fmt,args...)
52 /*---------------------------------------------------------------------*/
54 flash_info_t flash_info
[CONFIG_SYS_MAX_FLASH_BANKS
]; /* info for FLASH chips */
56 /*-----------------------------------------------------------------------
59 static ulong
flash_get_size (vu_long
*addr
, flash_info_t
*info
);
60 static int write_word (flash_info_t
*info
, ulong dest
, ulong data
);
61 static void flash_get_offsets (ulong base
, flash_info_t
*info
);
63 /*-----------------------------------------------------------------------
66 unsigned long flash_init (void)
68 volatile immap_t
*immap
= (immap_t
*)CONFIG_SYS_IMMR
;
69 volatile memctl8xx_t
*memctl
= &immap
->im_memctl
;
70 unsigned long size_b0
, size_b1
;
73 /* Init: no FLASHes known */
74 for (i
=0; i
<CONFIG_SYS_MAX_FLASH_BANKS
; ++i
) {
75 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
78 /* Static FLASH Bank configuration here - FIXME XXX */
80 DEBUGF("\n## Get flash bank 1 size @ 0x%08x\n",FLASH_BASE0_PRELIM
);
82 size_b0
= flash_get_size((vu_long
*)FLASH_BASE0_PRELIM
, &flash_info
[0]);
84 if (flash_info
[0].flash_id
== FLASH_UNKNOWN
) {
85 printf ("## Unknown FLASH on Bank 0: "
86 "ID 0x%lx, Size = 0x%08lx = %ld MB\n",
87 flash_info
[0].flash_id
,
88 size_b0
, size_b0
<<20);
91 DEBUGF("## Get flash bank 2 size @ 0x%08x\n",FLASH_BASE5_PRELIM
);
93 size_b1
= flash_get_size((vu_long
*)FLASH_BASE5_PRELIM
, &flash_info
[1]);
95 DEBUGF("## Prelim. Flash bank sizes: %08lx + 0x%08lx\n",size_b0
,size_b1
);
97 if (size_b1
> size_b0
) {
99 "Bank 1 (0x%08lx = %ld MB) > Bank 0 (0x%08lx = %ld MB)\n",
100 size_b1
, size_b1
<<20,
103 flash_info
[0].flash_id
= FLASH_UNKNOWN
;
104 flash_info
[1].flash_id
= FLASH_UNKNOWN
;
105 flash_info
[0].sector_count
= -1;
106 flash_info
[1].sector_count
= -1;
107 flash_info
[0].size
= 0;
108 flash_info
[1].size
= 0;
112 DEBUGF ("## Before remap: "
113 "BR0: 0x%08x OR0: 0x%08x "
114 "BR1: 0x%08x OR1: 0x%08x\n",
115 memctl
->memc_br0
, memctl
->memc_or0
,
116 memctl
->memc_br1
, memctl
->memc_or1
);
118 /* Remap FLASH according to real size */
119 memctl
->memc_or0
= CONFIG_SYS_OR_TIMING_FLASH
| (-size_b0
& 0xFFFF8000);
120 memctl
->memc_br0
= (CONFIG_SYS_FLASH_BASE
& BR_BA_MSK
) | \
121 BR_MS_GPCM
| BR_PS_32
| BR_V
;
123 DEBUGF("## BR0: 0x%08x OR0: 0x%08x\n",
124 memctl
->memc_br0
, memctl
->memc_or0
);
126 /* Re-do sizing to get full correct info */
127 size_b0
= flash_get_size((vu_long
*)CONFIG_SYS_FLASH_BASE
, &flash_info
[0]);
129 flash_get_offsets (CONFIG_SYS_FLASH_BASE
, &flash_info
[0]);
131 flash_info
[0].size
= size_b0
;
133 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
134 /* monitor protection ON by default */
135 flash_protect(FLAG_PROTECT_SET
,
136 CONFIG_SYS_MONITOR_BASE
,
137 CONFIG_SYS_MONITOR_BASE
+monitor_flash_len
-1,
141 #ifdef CONFIG_ENV_IS_IN_FLASH
142 /* ENV protection ON by default */
143 flash_protect(FLAG_PROTECT_SET
,
145 CONFIG_ENV_ADDR
+CONFIG_ENV_SECT_SIZE
-1,
150 memctl
->memc_or5
= CONFIG_SYS_OR_TIMING_FLASH
| (-size_b1
& 0xFFFF8000);
151 memctl
->memc_br5
= ((CONFIG_SYS_FLASH_BASE
+ size_b0
) & BR_BA_MSK
) |
152 BR_MS_GPCM
| BR_PS_32
| BR_V
;
154 DEBUGF("## BR5: 0x%08x OR5: 0x%08x\n",
155 memctl
->memc_br5
, memctl
->memc_or5
);
157 /* Re-do sizing to get full correct info */
158 size_b1
= flash_get_size((vu_long
*)(CONFIG_SYS_FLASH_BASE
+ size_b0
),
161 flash_info
[1].size
= size_b1
;
163 flash_get_offsets (CONFIG_SYS_FLASH_BASE
+ size_b0
, &flash_info
[1]);
165 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
166 /* monitor protection ON by default */
167 flash_protect(FLAG_PROTECT_SET
,
168 CONFIG_SYS_MONITOR_BASE
,
169 CONFIG_SYS_MONITOR_BASE
+monitor_flash_len
-1,
173 #ifdef CONFIG_ENV_IS_IN_FLASH
174 /* ENV protection ON by default */
175 flash_protect(FLAG_PROTECT_SET
,
177 CONFIG_ENV_ADDR
+CONFIG_ENV_SECT_SIZE
-1,
181 memctl
->memc_br5
= 0; /* invalidate bank */
182 memctl
->memc_or5
= 0; /* invalidate bank */
184 DEBUGF("## DISABLE BR5: 0x%08x OR5: 0x%08x\n",
185 memctl
->memc_br5
, memctl
->memc_or5
);
187 flash_info
[1].flash_id
= FLASH_UNKNOWN
;
188 flash_info
[1].sector_count
= -1;
189 flash_info
[1].size
= 0;
192 DEBUGF("## Final Flash bank sizes: %08lx + 0x%08lx\n",size_b0
,size_b1
);
194 return (size_b0
+ size_b1
);
197 /*-----------------------------------------------------------------------
199 static void flash_get_offsets (ulong base
, flash_info_t
*info
)
203 /* set up sector start address table */
204 if (info
->flash_id
& FLASH_BTYPE
) {
205 /* set sector offsets for bottom boot block type */
206 info
->start
[0] = base
+ 0x00000000;
207 info
->start
[1] = base
+ 0x00008000;
208 info
->start
[2] = base
+ 0x0000C000;
209 info
->start
[3] = base
+ 0x00010000;
210 for (i
= 4; i
< info
->sector_count
; i
++) {
211 info
->start
[i
] = base
+ (i
* 0x00020000) - 0x00060000;
214 /* set sector offsets for top boot block type */
215 i
= info
->sector_count
- 1;
216 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
217 info
->start
[i
--] = base
+ info
->size
- 0x0000C000;
218 info
->start
[i
--] = base
+ info
->size
- 0x00010000;
219 for (; i
>= 0; i
--) {
220 info
->start
[i
] = base
+ i
* 0x00020000;
226 /*-----------------------------------------------------------------------
228 void flash_print_info (flash_info_t
*info
)
232 if (info
->flash_id
== FLASH_UNKNOWN
) {
233 printf ("missing or unknown FLASH type\n");
237 switch (info
->flash_id
& FLASH_VENDMASK
) {
238 case FLASH_MAN_AMD
: printf ("AMD "); break;
239 case FLASH_MAN_FUJ
: printf ("FUJITSU "); break;
240 default: printf ("Unknown Vendor "); break;
243 switch (info
->flash_id
& FLASH_TYPEMASK
) {
244 case FLASH_AM400B
: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
246 case FLASH_AM400T
: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
248 case FLASH_AM800B
: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
250 case FLASH_AM800T
: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
252 case FLASH_AM160B
: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
254 case FLASH_AM160T
: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
256 case FLASH_AM320B
: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
258 case FLASH_AM320T
: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
260 default: printf ("Unknown Chip Type\n");
264 printf (" Size: %ld MB in %d Sectors\n",
265 info
->size
>> 20, info
->sector_count
);
267 printf (" Sector Start Addresses:");
268 for (i
=0; i
<info
->sector_count
; ++i
) {
273 info
->protect
[i
] ? " (RO)" : " "
280 /*-----------------------------------------------------------------------
284 /*-----------------------------------------------------------------------
288 * The following code cannot be run from FLASH!
291 static ulong
flash_get_size (vu_long
*addr
, flash_info_t
*info
)
295 ulong base
= (ulong
)addr
;
298 /* Write auto select command: read Manufacturer ID */
299 addr
[0x0555] = 0x00AA00AA;
300 addr
[0x02AA] = 0x00550055;
301 addr
[0x0555] = 0x00900090;
307 info
->flash_id
= FLASH_MAN_AMD
;
310 info
->flash_id
= FLASH_MAN_FUJ
;
313 info
->flash_id
= FLASH_UNKNOWN
;
314 info
->sector_count
= 0;
316 return (0); /* no or unknown flash */
319 value
= addr
[1]; /* device ID */
323 info
->flash_id
+= FLASH_AM400T
;
324 info
->sector_count
= 11;
325 info
->size
= 0x00100000;
329 info
->flash_id
+= FLASH_AM400B
;
330 info
->sector_count
= 11;
331 info
->size
= 0x00100000;
335 info
->flash_id
+= FLASH_AM800T
;
336 info
->sector_count
= 19;
337 info
->size
= 0x00200000;
341 info
->flash_id
+= FLASH_AM800B
;
342 info
->sector_count
= 19;
343 info
->size
= 0x00200000;
347 info
->flash_id
+= FLASH_AM160T
;
348 info
->sector_count
= 35;
349 info
->size
= 0x00400000;
353 info
->flash_id
+= FLASH_AM160B
;
354 info
->sector_count
= 35;
355 info
->size
= 0x00400000;
357 #if 0 /* enable when device IDs are available */
359 info
->flash_id
+= FLASH_AM320T
;
360 info
->sector_count
= 67;
361 info
->size
= 0x00800000;
365 info
->flash_id
+= FLASH_AM320B
;
366 info
->sector_count
= 67;
367 info
->size
= 0x00800000;
371 info
->flash_id
= FLASH_UNKNOWN
;
372 return (0); /* => no or unknown flash */
376 /* set up sector start address table */
377 if (info
->flash_id
& FLASH_BTYPE
) {
378 /* set sector offsets for bottom boot block type */
379 info
->start
[0] = base
+ 0x00000000;
380 info
->start
[1] = base
+ 0x00008000;
381 info
->start
[2] = base
+ 0x0000C000;
382 info
->start
[3] = base
+ 0x00010000;
383 for (i
= 4; i
< info
->sector_count
; i
++) {
384 info
->start
[i
] = base
+ (i
* 0x00020000) - 0x00060000;
387 /* set sector offsets for top boot block type */
388 i
= info
->sector_count
- 1;
389 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
390 info
->start
[i
--] = base
+ info
->size
- 0x0000C000;
391 info
->start
[i
--] = base
+ info
->size
- 0x00010000;
392 for (; i
>= 0; i
--) {
393 info
->start
[i
] = base
+ i
* 0x00020000;
397 /* check for protected sectors */
398 for (i
= 0; i
< info
->sector_count
; i
++) {
399 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
400 /* D0 = 1 if protected */
401 addr
= (volatile unsigned long *)(info
->start
[i
]);
402 info
->protect
[i
] = addr
[2] & 1;
406 * Prevent writes to uninitialized FLASH.
408 if (info
->flash_id
!= FLASH_UNKNOWN
) {
409 addr
= (volatile unsigned long *)info
->start
[0];
411 *addr
= 0x00F000F0; /* reset bank */
418 /*-----------------------------------------------------------------------
421 int flash_erase (flash_info_t
*info
, int s_first
, int s_last
)
423 vu_long
*addr
= (vu_long
*)(info
->start
[0]);
424 int flag
, prot
, sect
, l_sect
;
425 ulong start
, now
, last
;
427 if ((s_first
< 0) || (s_first
> s_last
)) {
428 if (info
->flash_id
== FLASH_UNKNOWN
) {
429 printf ("- missing\n");
431 printf ("- no sectors to erase\n");
436 if ((info
->flash_id
== FLASH_UNKNOWN
) ||
437 (info
->flash_id
> FLASH_AMD_COMP
)) {
438 printf ("Can't erase unknown flash type %08lx - aborted\n",
444 for (sect
=s_first
; sect
<=s_last
; ++sect
) {
445 if (info
->protect
[sect
]) {
451 printf ("- Warning: %d protected sectors will not be erased!\n",
459 /* Disable interrupts which might cause a timeout here */
460 flag
= disable_interrupts();
462 addr
[0x0555] = 0x00AA00AA;
463 addr
[0x02AA] = 0x00550055;
464 addr
[0x0555] = 0x00800080;
465 addr
[0x0555] = 0x00AA00AA;
466 addr
[0x02AA] = 0x00550055;
468 /* Start erase on unprotected sectors */
469 for (sect
= s_first
; sect
<=s_last
; sect
++) {
470 if (info
->protect
[sect
] == 0) { /* not protected */
471 addr
= (vu_long
*)(info
->start
[sect
]);
472 addr
[0] = 0x00300030;
477 /* re-enable interrupts if necessary */
481 /* wait at least 80us - let's wait 1 ms */
485 * We wait for the last triggered sector
490 start
= get_timer (0);
492 addr
= (vu_long
*)(info
->start
[l_sect
]);
493 while ((addr
[0] & 0x00800080) != 0x00800080) {
494 if ((now
= get_timer(start
)) > CONFIG_SYS_FLASH_ERASE_TOUT
) {
495 printf ("Timeout\n");
498 /* show that we're waiting */
499 if ((now
- last
) > 1000) { /* every second */
506 /* reset to read mode */
507 addr
= (volatile unsigned long *)info
->start
[0];
508 addr
[0] = 0x00F000F0; /* reset bank */
514 /*-----------------------------------------------------------------------
515 * Copy memory to flash, returns:
518 * 2 - Flash not erased
521 int write_buff (flash_info_t
*info
, uchar
*src
, ulong addr
, ulong cnt
)
526 wp
= (addr
& ~3); /* get lower word aligned address */
529 * handle unaligned start bytes
531 if ((l
= addr
- wp
) != 0) {
533 for (i
=0, cp
=wp
; i
<l
; ++i
, ++cp
) {
534 data
= (data
<< 8) | (*(uchar
*)cp
);
536 for (; i
<4 && cnt
>0; ++i
) {
537 data
= (data
<< 8) | *src
++;
541 for (; cnt
==0 && i
<4; ++i
, ++cp
) {
542 data
= (data
<< 8) | (*(uchar
*)cp
);
545 if ((rc
= write_word(info
, wp
, data
)) != 0) {
552 * handle word aligned part
556 for (i
=0; i
<4; ++i
) {
557 data
= (data
<< 8) | *src
++;
559 if ((rc
= write_word(info
, wp
, data
)) != 0) {
571 * handle unaligned tail bytes
574 for (i
=0, cp
=wp
; i
<4 && cnt
>0; ++i
, ++cp
) {
575 data
= (data
<< 8) | *src
++;
578 for (; i
<4; ++i
, ++cp
) {
579 data
= (data
<< 8) | (*(uchar
*)cp
);
582 return (write_word(info
, wp
, data
));
585 /*-----------------------------------------------------------------------
586 * Write a word to Flash, returns:
589 * 2 - Flash not erased
591 static int write_word (flash_info_t
*info
, ulong dest
, ulong data
)
593 vu_long
*addr
= (vu_long
*)(info
->start
[0]);
597 /* Check if Flash is (sufficiently) erased */
598 if ((*((vu_long
*)dest
) & data
) != data
) {
601 /* Disable interrupts which might cause a timeout here */
602 flag
= disable_interrupts();
604 addr
[0x0555] = 0x00AA00AA;
605 addr
[0x02AA] = 0x00550055;
606 addr
[0x0555] = 0x00A000A0;
608 *((vu_long
*)dest
) = data
;
610 /* re-enable interrupts if necessary */
614 /* data polling for D7 */
615 start
= get_timer (0);
616 while ((*((vu_long
*)dest
) & 0x00800080) != (data
& 0x00800080)) {
617 if (get_timer(start
) > CONFIG_SYS_FLASH_WRITE_TOUT
) {
624 /*-----------------------------------------------------------------------