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,
27 flash_info_t flash_info
[CFG_MAX_FLASH_BANKS
]; /* info for FLASH chips */
29 #if defined(CFG_ENV_IS_IN_FLASH)
31 # define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
34 # define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
36 # ifndef CFG_ENV_SECT_SIZE
37 # define CFG_ENV_SECT_SIZE CFG_ENV_SIZE
41 /*-----------------------------------------------------------------------
44 static ulong
flash_get_size (vu_long
*addr
, flash_info_t
*info
);
45 static int write_word (flash_info_t
*info
, ulong dest
, ulong data
);
46 static void flash_get_offsets (ulong base
, flash_info_t
*info
);
48 /*-----------------------------------------------------------------------
51 unsigned long flash_init (void)
53 volatile immap_t
*immap
= (immap_t
*)CFG_IMMR
;
54 volatile memctl8xx_t
*memctl
= &immap
->im_memctl
;
55 unsigned long size_b0
, size_b1
;
58 /* Init: no FLASHes known */
59 for (i
=0; i
< CFG_MAX_FLASH_BANKS
; ++i
) {
60 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
63 /* Static FLASH Bank configuration here - FIXME XXX */
65 size_b0
= flash_get_size((vu_long
*)FLASH_BASE0_PRELIM
, &flash_info
[0]);
67 if (flash_info
[0].flash_id
== FLASH_UNKNOWN
) {
68 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
73 if (FLASH_BASE1_PRELIM
!= 0x0) {
74 size_b1
= flash_get_size((vu_long
*)FLASH_BASE1_PRELIM
, &flash_info
[1]);
76 if (size_b1
> size_b0
) {
77 printf ("## ERROR: Bank 1 (0x%08lx = %ld MB)"
78 " > Bank 0 (0x%08lx = %ld MB)\n",
79 size_b1
, size_b1
>> 20,
80 size_b0
, size_b0
>> 20);
82 flash_info
[0].flash_id
= FLASH_UNKNOWN
;
83 flash_info
[1].flash_id
= FLASH_UNKNOWN
;
84 flash_info
[0].sector_count
= -1;
85 flash_info
[1].sector_count
= -1;
86 flash_info
[0].size
= 0;
87 flash_info
[1].size
= 0;
94 /* Remap FLASH according to real size */
95 memctl
->memc_or0
= CFG_OR_TIMING_FLASH
| (-size_b0
& OR_AM_MSK
);
96 memctl
->memc_br0
= (CFG_FLASH_BASE
& BR_BA_MSK
) | BR_MS_GPCM
| BR_V
;
98 /* Re-do sizing to get full correct info */
99 size_b0
= flash_get_size((vu_long
*)CFG_FLASH_BASE
, &flash_info
[0]);
101 flash_get_offsets (CFG_FLASH_BASE
, &flash_info
[0]);
103 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
104 /* monitor protection ON by default */
105 flash_protect(FLAG_PROTECT_SET
,
107 CFG_MONITOR_BASE
+monitor_flash_len
-1,
111 #ifdef CFG_ENV_IS_IN_FLASH
112 /* ENV protection ON by default */
113 flash_protect(FLAG_PROTECT_SET
,
115 CFG_ENV_ADDR
+CFG_ENV_SIZE
-1,
119 /* ICU862 Board has only one Flash Bank */
120 flash_info
[1].flash_id
= FLASH_UNKNOWN
;
121 flash_info
[1].sector_count
= -1;
123 flash_info
[0].size
= size_b0
;
124 flash_info
[1].size
= size_b1
;
126 return (size_b0
+ size_b1
);
130 /*-----------------------------------------------------------------------
132 static void flash_get_offsets (ulong base
, flash_info_t
*info
)
136 /* set up sector start address table */
137 if (((info
->flash_id
& FLASH_TYPEMASK
) == FLASH_AM040
) ||
138 ((info
->flash_id
& FLASH_TYPEMASK
) == FLASH_AM033C
)) {
139 /* set sector offsets for uniform sector type */
140 for (i
= 0; i
< info
->sector_count
; i
++) {
141 info
->start
[i
] = base
+ (i
* 0x00040000);
146 /*-----------------------------------------------------------------------
148 void flash_print_info (flash_info_t
*info
)
152 if (info
->flash_id
== FLASH_UNKNOWN
) {
153 puts ("missing or unknown FLASH type\n");
157 switch (info
->flash_id
& FLASH_VENDMASK
) {
158 case FLASH_MAN_AMD
: puts ("AMD "); break;
159 case FLASH_MAN_FUJ
: puts ("FUJITSU "); break;
160 case FLASH_MAN_BM
: puts ("BRIGHT MICRO "); break;
161 default: puts ("Unknown Vendor "); break;
164 switch (info
->flash_id
& FLASH_TYPEMASK
) {
165 case FLASH_AM040
: puts ("29F040/29LV040 (4 Mbit, uniform sectors)\n");
167 case FLASH_AM400B
: puts ("AM29LV400B (4 Mbit, bottom boot sect)\n");
169 case FLASH_AM400T
: puts ("AM29LV400T (4 Mbit, top boot sector)\n");
171 case FLASH_AM800B
: puts ("AM29LV800B (8 Mbit, bottom boot sect)\n");
173 case FLASH_AM800T
: puts ("AM29LV800T (8 Mbit, top boot sector)\n");
175 case FLASH_AM160B
: puts ("AM29LV160B (16 Mbit, bottom boot sect)\n");
177 case FLASH_AM160T
: puts ("AM29LV160T (16 Mbit, top boot sector)\n");
179 case FLASH_AM320B
: puts ("AM29LV320B (32 Mbit, bottom boot sect)\n");
181 case FLASH_AM320T
: puts ("AM29LV320T (32 Mbit, top boot sector)\n");
183 case FLASH_AM033C
: puts ("AM29LV033C (32 Mbit)\n");
185 default: puts ("Unknown Chip Type\n");
189 printf (" Size: %ld MB in %d Sectors\n",
190 info
->size
>> 20, info
->sector_count
);
192 puts (" Sector Start Addresses:");
194 for (i
=0; i
<info
->sector_count
; ++i
) {
201 info
->protect
[i
] ? " (RO)" : " "
208 /*-----------------------------------------------------------------------
212 * The following code cannot be run from FLASH!
215 static ulong
flash_get_size (vu_long
*addr
, flash_info_t
*info
)
219 ulong base
= (ulong
)addr
;
223 /* Write auto select command: read Manufacturer ID */
225 addr
[0x0555] = 0x00AA00AA;
226 addr
[0x02AA] = 0x00550055;
227 addr
[0x0555] = 0x00900090;
229 addr
[0x0555] = 0xAAAAAAAA;
230 addr
[0x02AA] = 0x55555555;
231 addr
[0x0555] = 0x90909090;
236 switch (value
+ (value
<< 16)) {
238 info
->flash_id
= FLASH_MAN_AMD
;
242 info
->flash_id
= FLASH_MAN_FUJ
;
246 info
->flash_id
= FLASH_UNKNOWN
;
247 info
->sector_count
= 0;
252 value
= addr
[1]; /* device ID */
254 switch ((unsigned long)value
) {
256 info
->flash_id
+= FLASH_AM040
;
257 info
->sector_count
= 8;
258 info
->size
= 0x00200000;
262 info
->flash_id
+= FLASH_AM400T
;
263 info
->sector_count
= 11;
264 info
->size
= 0x00100000;
268 info
->flash_id
+= FLASH_AM400B
;
269 info
->sector_count
= 11;
270 info
->size
= 0x00100000;
274 info
->flash_id
+= FLASH_AM800T
;
275 info
->sector_count
= 19;
276 info
->size
= 0x00200000;
280 info
->flash_id
+= FLASH_AM800B
;
281 info
->sector_count
= 19;
282 info
->size
= 0x00200000;
286 info
->flash_id
+= FLASH_AM160T
;
287 info
->sector_count
= 35;
288 info
->size
= 0x00400000;
292 info
->flash_id
+= FLASH_AM160B
;
293 info
->sector_count
= 35;
294 info
->size
= 0x00400000;
296 #if 0 /* enable when device IDs are available */
298 info
->flash_id
+= FLASH_AM320T
;
299 info
->sector_count
= 67;
300 info
->size
= 0x00800000;
304 info
->flash_id
+= FLASH_AM320B
;
305 info
->sector_count
= 67;
306 info
->size
= 0x00800000;
310 info
->flash_id
+= FLASH_AM033C
;
311 info
->sector_count
= 64;
312 info
->size
= 0x01000000;
315 info
->flash_id
= FLASH_UNKNOWN
;
316 return (0); /* => no or unknown flash */
321 /* set up sector start address table */
322 if (info
->flash_id
& FLASH_BTYPE
) {
323 /* set sector offsets for bottom boot block type */
324 info
->start
[0] = base
+ 0x00000000;
325 info
->start
[1] = base
+ 0x00008000;
326 info
->start
[2] = base
+ 0x0000C000;
327 info
->start
[3] = base
+ 0x00010000;
328 for (i
= 4; i
< info
->sector_count
; i
++) {
329 info
->start
[i
] = base
+ (i
* 0x00020000) - 0x00060000;
332 /* set sector offsets for top boot block type */
333 i
= info
->sector_count
- 1;
334 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
335 info
->start
[i
--] = base
+ info
->size
- 0x0000C000;
336 info
->start
[i
--] = base
+ info
->size
- 0x00010000;
337 for (; i
>= 0; i
--) {
338 info
->start
[i
] = base
+ i
* 0x00020000;
342 flash_get_offsets ((ulong
)addr
, &flash_info
[0]);
345 /* check for protected sectors */
346 for (i
= 0; i
< info
->sector_count
; i
++) {
347 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
348 /* D0 = 1 if protected */
349 addr
= (volatile unsigned long *)(info
->start
[i
]);
351 /* We don't know why it happens, but on ICU Board *
352 * for AMD29033C flash we need to resend the command of *
353 * reading flash protection for upper 8 Mb of flash */
355 addr
[0x0555] = 0xAAAAAAAA;
356 addr
[0x02AA] = 0x55555555;
357 addr
[0x0555] = 0x90909090;
360 info
->protect
[i
] = addr
[2] & 1;
364 * Prevent writes to uninitialized FLASH.
366 if (info
->flash_id
!= FLASH_UNKNOWN
) {
367 addr
= (volatile unsigned long *)info
->start
[0];
369 *addr
= 0x00F000F0; /* reset bank */
371 *addr
= 0xF0F0F0F0; /* reset bank */
379 /*-----------------------------------------------------------------------
382 int flash_erase (flash_info_t
*info
, int s_first
, int s_last
)
384 vu_long
*addr
= (vu_long
*)(info
->start
[0]);
385 int flag
, prot
, sect
, l_sect
;
386 ulong start
, now
, last
;
388 if ((s_first
< 0) || (s_first
> s_last
)) {
389 if (info
->flash_id
== FLASH_UNKNOWN
) {
390 puts ("- missing\n");
392 puts ("- no sectors to erase\n");
397 if ((info
->flash_id
== FLASH_UNKNOWN
) ||
398 (info
->flash_id
> FLASH_AMD_COMP
)) {
399 puts ("Can't erase unknown flash type - aborted\n");
404 for (sect
=s_first
; sect
<=s_last
; ++sect
) {
405 if (info
->protect
[sect
]) {
411 printf ("- Warning: %d protected sectors will not be erased!\n",
419 /* Disable interrupts which might cause a timeout here */
420 flag
= disable_interrupts();
423 addr
[0x0555] = 0x00AA00AA;
424 addr
[0x02AA] = 0x00550055;
425 addr
[0x0555] = 0x00800080;
426 addr
[0x0555] = 0x00AA00AA;
427 addr
[0x02AA] = 0x00550055;
429 addr
[0x0555] = 0xAAAAAAAA;
430 addr
[0x02AA] = 0x55555555;
431 addr
[0x0555] = 0x80808080;
432 addr
[0x0555] = 0xAAAAAAAA;
433 addr
[0x02AA] = 0x55555555;
436 /* Start erase on unprotected sectors */
437 for (sect
= s_first
; sect
<=s_last
; sect
++) {
438 if (info
->protect
[sect
] == 0) { /* not protected */
439 addr
= (vu_long
*)(info
->start
[sect
]);
441 addr
[0] = 0x00300030;
443 addr
[0] = 0x30303030;
449 /* re-enable interrupts if necessary */
453 /* wait at least 80us - let's wait 1 ms */
457 * We wait for the last triggered sector
462 start
= get_timer (0);
464 addr
= (vu_long
*)(info
->start
[l_sect
]);
466 while ((addr
[0] & 0x00800080) != 0x00800080)
468 while ((addr
[0] & 0xFFFFFFFF) != 0xFFFFFFFF)
471 if ((now
= get_timer(start
)) > CFG_FLASH_ERASE_TOUT
) {
475 /* show that we're waiting */
476 if ((now
- last
) > 1000) { /* every second */
483 /* reset to read mode */
484 addr
= (volatile unsigned long *)info
->start
[0];
486 addr
[0] = 0x00F000F0; /* reset bank */
488 addr
[0] = 0xF0F0F0F0; /* reset bank */
495 /*-----------------------------------------------------------------------
496 * Copy memory to flash, returns:
499 * 2 - Flash not erased
502 int write_buff (flash_info_t
*info
, uchar
*src
, ulong addr
, ulong cnt
)
507 wp
= (addr
& ~3); /* get lower word aligned address */
510 * handle unaligned start bytes
512 if ((l
= addr
- wp
) != 0) {
514 for (i
=0, cp
=wp
; i
<l
; ++i
, ++cp
) {
515 data
= (data
<< 8) | (*(uchar
*)cp
);
517 for (; i
<4 && cnt
>0; ++i
) {
518 data
= (data
<< 8) | *src
++;
522 for (; cnt
==0 && i
<4; ++i
, ++cp
) {
523 data
= (data
<< 8) | (*(uchar
*)cp
);
526 if ((rc
= write_word(info
, wp
, data
)) != 0) {
533 * handle word aligned part
537 for (i
=0; i
<4; ++i
) {
538 data
= (data
<< 8) | *src
++;
540 if ((rc
= write_word(info
, wp
, data
)) != 0) {
552 * handle unaligned tail bytes
555 for (i
=0, cp
=wp
; i
<4 && cnt
>0; ++i
, ++cp
) {
556 data
= (data
<< 8) | *src
++;
559 for (; i
<4; ++i
, ++cp
) {
560 data
= (data
<< 8) | (*(uchar
*)cp
);
563 return (write_word(info
, wp
, data
));
566 /*-----------------------------------------------------------------------
567 * Write a word to Flash, returns:
570 * 2 - Flash not erased
572 static int write_word (flash_info_t
*info
, ulong dest
, ulong data
)
574 vu_long
*addr
= (vu_long
*)(info
->start
[0]);
578 /* Check if Flash is (sufficiently) erased */
579 if ((*((vu_long
*)dest
) & data
) != data
) {
582 /* Disable interrupts which might cause a timeout here */
583 flag
= disable_interrupts();
586 addr
[0x0555] = 0x00AA00AA;
587 addr
[0x02AA] = 0x00550055;
588 addr
[0x0555] = 0x00A000A0;
590 addr
[0x0555] = 0xAAAAAAAA;
591 addr
[0x02AA] = 0x55555555;
592 addr
[0x0555] = 0xA0A0A0A0;
595 *((vu_long
*)dest
) = data
;
597 /* re-enable interrupts if necessary */
601 /* data polling for D7 */
602 start
= get_timer (0);
604 while ((*((vu_long
*)dest
) & 0x00800080) != (data
& 0x00800080))
606 while ((*((vu_long
*)dest
) & 0x80808080) != (data
& 0x80808080))
609 if (get_timer(start
) > CFG_FLASH_WRITE_TOUT
) {
616 /*-----------------------------------------------------------------------