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
[CONFIG_SYS_MAX_FLASH_BANKS
]; /* info for FLASH chips */
29 #if defined(CONFIG_ENV_IS_IN_FLASH)
30 # ifndef CONFIG_ENV_ADDR
31 # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
33 # ifndef CONFIG_ENV_SIZE
34 # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
36 # ifndef CONFIG_ENV_SECT_SIZE
37 # define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
41 /*---------------------------------------------------------------------*/
45 #define DEBUGF(fmt,args...) printf(fmt ,##args)
47 #define DEBUGF(fmt,args...)
49 /*---------------------------------------------------------------------*/
51 /*-----------------------------------------------------------------------
54 static ulong
flash_get_size (vu_long
*addr
, flash_info_t
*info
);
55 static int write_word (flash_info_t
*info
, ulong dest
, ulong data
);
56 static void flash_get_offsets (ulong base
, flash_info_t
*info
);
58 /*-----------------------------------------------------------------------
61 unsigned long flash_init (void)
63 volatile immap_t
*immap
= (immap_t
*)CONFIG_SYS_IMMR
;
64 volatile memctl8xx_t
*memctl
= &immap
->im_memctl
;
65 unsigned long size_b0
, size_b1
;
68 /* Init: no FLASHes known */
69 for (i
=0; i
<CONFIG_SYS_MAX_FLASH_BANKS
; ++i
) {
70 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
73 /* Static FLASH Bank configuration here - FIXME XXX */
75 DEBUGF("\n## Get flash bank 1 size @ 0x%08x\n",FLASH_BASE0_PRELIM
);
77 size_b0
= flash_get_size((vu_long
*)FLASH_BASE0_PRELIM
, &flash_info
[0]);
79 if (flash_info
[0].flash_id
== FLASH_UNKNOWN
) {
80 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
81 size_b0
, size_b0
<<20);
84 #if defined(FLASH_BASE1_PRELIM) && (FLASH_BASE1_PRELIM != 0)
85 DEBUGF("## Get flash bank 2 size @ 0x%08x\n",FLASH_BASE1_PRELIM
);
87 size_b1
= flash_get_size((vu_long
*)FLASH_BASE1_PRELIM
, &flash_info
[1]);
89 if (size_b1
> size_b0
) {
91 "Bank 1 (0x%08lx = %ld MB) > Bank 0 (0x%08lx = %ld MB)\n",
95 flash_info
[0].flash_id
= FLASH_UNKNOWN
;
96 flash_info
[1].flash_id
= FLASH_UNKNOWN
;
97 flash_info
[0].sector_count
= -1;
98 flash_info
[1].sector_count
= -1;
99 flash_info
[0].size
= 0;
100 flash_info
[1].size
= 0;
105 #endif /* FLASH_BASE1_PRELIM */
107 DEBUGF("## Prelim. Flash bank sizes: %08lx + 0x%08lx\n",size_b0
,size_b1
);
109 DEBUGF ("## Before remap: "
110 "BR0: 0x%08x OR0: 0x%08x "
111 "BR1: 0x%08x OR1: 0x%08x\n",
112 memctl
->memc_br0
, memctl
->memc_or0
,
113 memctl
->memc_br1
, memctl
->memc_or1
);
115 /* Remap FLASH according to real size */
116 memctl
->memc_or0
= CONFIG_SYS_OR_TIMING_FLASH
| (-size_b0
& OR_AM_MSK
);
117 memctl
->memc_br0
= (CONFIG_SYS_FLASH_BASE
& BR_BA_MSK
) | BR_MS_GPCM
| BR_V
;
119 DEBUGF("## BR0: 0x%08x OR0: 0x%08x\n",
120 memctl
->memc_br0
, memctl
->memc_or0
);
122 /* Re-do sizing to get full correct info */
123 size_b0
= flash_get_size((vu_long
*)CONFIG_SYS_FLASH_BASE
, &flash_info
[0]);
125 flash_get_offsets (CONFIG_SYS_FLASH_BASE
, &flash_info
[0]);
127 flash_info
[0].size
= size_b0
;
129 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
130 /* monitor protection ON by default */
131 flash_protect(FLAG_PROTECT_SET
,
132 CONFIG_SYS_MONITOR_BASE
,
133 CONFIG_SYS_MONITOR_BASE
+monitor_flash_len
-1,
137 #ifdef CONFIG_ENV_IS_IN_FLASH
138 /* ENV protection ON by default */
139 flash_protect(FLAG_PROTECT_SET
,
141 CONFIG_ENV_ADDR
+CONFIG_ENV_SECT_SIZE
-1,
146 memctl
->memc_or1
= CONFIG_SYS_OR_TIMING_FLASH
| (-size_b1
& OR_AM_MSK
);
147 memctl
->memc_br1
= ((CONFIG_SYS_FLASH_BASE
+ size_b0
) & BR_BA_MSK
) |
150 DEBUGF("## BR1: 0x%08x OR1: 0x%08x\n",
151 memctl
->memc_br1
, memctl
->memc_or1
);
153 /* Re-do sizing to get full correct info */
154 size_b1
= flash_get_size((vu_long
*)(CONFIG_SYS_FLASH_BASE
+ size_b0
),
157 flash_info
[1].size
= size_b1
;
159 flash_get_offsets (CONFIG_SYS_FLASH_BASE
+ size_b0
, &flash_info
[1]);
161 # if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
162 /* monitor protection ON by default */
163 flash_protect(FLAG_PROTECT_SET
,
164 CONFIG_SYS_MONITOR_BASE
,
165 CONFIG_SYS_MONITOR_BASE
+monitor_flash_len
-1,
169 # ifdef CONFIG_ENV_IS_IN_FLASH
170 /* ENV protection ON by default */
171 flash_protect(FLAG_PROTECT_SET
,
173 CONFIG_ENV_ADDR
+CONFIG_ENV_SECT_SIZE
-1,
177 #ifndef CONFIG_AMX_RAM_EXT
178 memctl
->memc_br1
= 0; /* invalidate bank */
179 memctl
->memc_or1
= 0; /* invalidate bank */
182 DEBUGF("## DISABLE BR1: 0x%08x OR1: 0x%08x\n",
183 memctl
->memc_br1
, memctl
->memc_or1
);
185 flash_info
[1].flash_id
= FLASH_UNKNOWN
;
186 flash_info
[1].sector_count
= -1;
187 flash_info
[1].size
= 0;
190 DEBUGF("## Final Flash bank sizes: %08lx + 0x%08lx\n",size_b0
,size_b1
);
192 return (size_b0
+ size_b1
);
195 /*-----------------------------------------------------------------------
197 static void flash_get_offsets (ulong base
, flash_info_t
*info
)
201 /* set up sector start address table */
202 if ((info
->flash_id
& FLASH_TYPEMASK
) == FLASH_AM040
) {
203 /* set sector offsets for uniform sector type */
204 for (i
= 0; i
< info
->sector_count
; i
++) {
205 info
->start
[i
] = base
+ (i
* 0x00040000);
207 } else if (info
->flash_id
& FLASH_BTYPE
) {
208 /* set sector offsets for bottom boot block type */
209 info
->start
[0] = base
+ 0x00000000;
210 info
->start
[1] = base
+ 0x00008000;
211 info
->start
[2] = base
+ 0x0000C000;
212 info
->start
[3] = base
+ 0x00010000;
213 for (i
= 4; i
< info
->sector_count
; i
++) {
214 info
->start
[i
] = base
+ (i
* 0x00020000) - 0x00060000;
217 /* set sector offsets for top boot block type */
218 i
= info
->sector_count
- 1;
219 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
220 info
->start
[i
--] = base
+ info
->size
- 0x0000C000;
221 info
->start
[i
--] = base
+ info
->size
- 0x00010000;
222 for (; i
>= 0; i
--) {
223 info
->start
[i
] = base
+ i
* 0x00020000;
228 /*-----------------------------------------------------------------------
230 void flash_print_info (flash_info_t
*info
)
234 if (info
->flash_id
== FLASH_UNKNOWN
) {
235 printf ("missing or unknown FLASH type\n");
239 switch (info
->flash_id
& FLASH_VENDMASK
) {
240 case FLASH_MAN_AMD
: printf ("AMD "); break;
241 case FLASH_MAN_FUJ
: printf ("FUJITSU "); break;
242 case FLASH_MAN_BM
: printf ("BRIGHT MICRO "); break;
243 default: printf ("Unknown Vendor "); break;
246 switch (info
->flash_id
& FLASH_TYPEMASK
) {
247 case FLASH_AM040
: printf ("29F040 or 29LV040 (4 Mbit, uniform sectors)\n");
249 case FLASH_AM400B
: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
251 case FLASH_AM400T
: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
253 case FLASH_AM800B
: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
255 case FLASH_AM800T
: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
257 case FLASH_AM160B
: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
259 case FLASH_AM160T
: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
261 case FLASH_AM320B
: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
263 case FLASH_AM320T
: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
265 default: printf ("Unknown Chip Type\n");
269 printf (" Size: %ld MB in %d Sectors\n",
270 info
->size
>> 20, info
->sector_count
);
272 printf (" Sector Start Addresses:");
273 for (i
=0; i
<info
->sector_count
; ++i
) {
278 info
->protect
[i
] ? " (RO)" : " "
284 /*-----------------------------------------------------------------------
288 /*-----------------------------------------------------------------------
292 * The following code cannot be run from FLASH!
295 static ulong
flash_get_size (vu_long
*addr
, flash_info_t
*info
)
299 ulong base
= (ulong
)addr
;
301 /* Write auto select command: read Manufacturer ID */
302 addr
[0x0555] = 0x00AA00AA;
303 addr
[0x02AA] = 0x00550055;
304 addr
[0x0555] = 0x00900090;
308 DEBUGF("Manuf. ID @ 0x%08lx: 0x%08lx\n", (ulong
)addr
, value
);
312 info
->flash_id
= FLASH_MAN_AMD
;
315 info
->flash_id
= FLASH_MAN_FUJ
;
318 info
->flash_id
= FLASH_UNKNOWN
;
319 info
->sector_count
= 0;
321 return (0); /* no or unknown flash */
324 value
= addr
[1]; /* device ID */
326 DEBUGF("Device ID @ 0x%08lx: 0x%08lx\n", (ulong
)(&addr
[1]), value
);
330 info
->flash_id
+= FLASH_AM040
;
331 info
->sector_count
= 8;
332 info
->size
= 0x00200000;
336 info
->flash_id
+= FLASH_AM400T
;
337 info
->sector_count
= 11;
338 info
->size
= 0x00100000;
342 info
->flash_id
+= FLASH_AM400B
;
343 info
->sector_count
= 11;
344 info
->size
= 0x00100000;
348 info
->flash_id
+= FLASH_AM800T
;
349 info
->sector_count
= 19;
350 info
->size
= 0x00200000;
354 info
->flash_id
+= FLASH_AM800B
;
355 info
->sector_count
= 19;
356 info
->size
= 0x00200000;
360 info
->flash_id
+= FLASH_AM160T
;
361 info
->sector_count
= 35;
362 info
->size
= 0x00400000;
366 info
->flash_id
+= FLASH_AM160B
;
367 info
->sector_count
= 35;
368 info
->size
= 0x00400000;
370 #if 0 /* enable when device IDs are available */
372 info
->flash_id
+= FLASH_AM320T
;
373 info
->sector_count
= 67;
374 info
->size
= 0x00800000;
378 info
->flash_id
+= FLASH_AM320B
;
379 info
->sector_count
= 67;
380 info
->size
= 0x00800000;
384 info
->flash_id
= FLASH_UNKNOWN
;
385 return (0); /* => no or unknown flash */
388 /* set up sector start address table */
389 if (info
->flash_id
& FLASH_BTYPE
) {
390 /* set sector offsets for bottom boot block type */
391 info
->start
[0] = base
+ 0x00000000;
392 info
->start
[1] = base
+ 0x00008000;
393 info
->start
[2] = base
+ 0x0000C000;
394 info
->start
[3] = base
+ 0x00010000;
395 for (i
= 4; i
< info
->sector_count
; i
++) {
396 info
->start
[i
] = base
+ (i
* 0x00020000) - 0x00060000;
399 /* set sector offsets for top boot block type */
400 i
= info
->sector_count
- 1;
401 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
402 info
->start
[i
--] = base
+ info
->size
- 0x0000C000;
403 info
->start
[i
--] = base
+ info
->size
- 0x00010000;
404 for (; i
>= 0; i
--) {
405 info
->start
[i
] = base
+ i
* 0x00020000;
409 /* check for protected sectors */
410 for (i
= 0; i
< info
->sector_count
; i
++) {
411 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
412 /* D0 = 1 if protected */
413 addr
= (volatile unsigned long *)(info
->start
[i
]);
414 info
->protect
[i
] = addr
[2] & 1;
418 * Prevent writes to uninitialized FLASH.
420 if (info
->flash_id
!= FLASH_UNKNOWN
) {
421 addr
= (volatile unsigned long *)info
->start
[0];
423 *addr
= 0x00F000F0; /* reset bank */
430 /*-----------------------------------------------------------------------
433 int flash_erase (flash_info_t
*info
, int s_first
, int s_last
)
435 vu_long
*addr
= (vu_long
*)(info
->start
[0]);
436 int flag
, prot
, sect
, l_sect
;
437 ulong start
, now
, last
;
439 if ((s_first
< 0) || (s_first
> s_last
)) {
440 if (info
->flash_id
== FLASH_UNKNOWN
) {
441 printf ("- missing\n");
443 printf ("- no sectors to erase\n");
448 if ((info
->flash_id
== FLASH_UNKNOWN
) ||
449 (info
->flash_id
> FLASH_AMD_COMP
)) {
450 printf ("Can't erase unknown flash type %08lx - aborted\n",
456 for (sect
=s_first
; sect
<=s_last
; ++sect
) {
457 if (info
->protect
[sect
]) {
463 printf ("- Warning: %d protected sectors will not be erased!\n",
471 /* Disable interrupts which might cause a timeout here */
472 flag
= disable_interrupts();
474 addr
[0x0555] = 0x00AA00AA;
475 addr
[0x02AA] = 0x00550055;
476 addr
[0x0555] = 0x00800080;
477 addr
[0x0555] = 0x00AA00AA;
478 addr
[0x02AA] = 0x00550055;
480 /* Start erase on unprotected sectors */
481 for (sect
= s_first
; sect
<=s_last
; sect
++) {
482 if (info
->protect
[sect
] == 0) { /* not protected */
483 addr
= (vu_long
*)(info
->start
[sect
]);
484 addr
[0] = 0x00300030;
489 /* re-enable interrupts if necessary */
493 /* wait at least 80us - let's wait 1 ms */
497 * We wait for the last triggered sector
502 start
= get_timer (0);
504 addr
= (vu_long
*)(info
->start
[l_sect
]);
505 while ((addr
[0] & 0x00800080) != 0x00800080) {
506 if ((now
= get_timer(start
)) > CONFIG_SYS_FLASH_ERASE_TOUT
) {
507 printf ("Timeout\n");
510 /* show that we're waiting */
511 if ((now
- last
) > 1000) { /* every second */
518 /* reset to read mode */
519 addr
= (volatile unsigned long *)info
->start
[0];
520 addr
[0] = 0x00F000F0; /* reset bank */
526 /*-----------------------------------------------------------------------
527 * Copy memory to flash, returns:
530 * 2 - Flash not erased
533 int write_buff (flash_info_t
*info
, uchar
*src
, ulong addr
, ulong cnt
)
538 wp
= (addr
& ~3); /* get lower word aligned address */
541 * handle unaligned start bytes
543 if ((l
= addr
- wp
) != 0) {
545 for (i
=0, cp
=wp
; i
<l
; ++i
, ++cp
) {
546 data
= (data
<< 8) | (*(uchar
*)cp
);
548 for (; i
<4 && cnt
>0; ++i
) {
549 data
= (data
<< 8) | *src
++;
553 for (; cnt
==0 && i
<4; ++i
, ++cp
) {
554 data
= (data
<< 8) | (*(uchar
*)cp
);
557 if ((rc
= write_word(info
, wp
, data
)) != 0) {
564 * handle word aligned part
568 for (i
=0; i
<4; ++i
) {
569 data
= (data
<< 8) | *src
++;
571 if ((rc
= write_word(info
, wp
, data
)) != 0) {
583 * handle unaligned tail bytes
586 for (i
=0, cp
=wp
; i
<4 && cnt
>0; ++i
, ++cp
) {
587 data
= (data
<< 8) | *src
++;
590 for (; i
<4; ++i
, ++cp
) {
591 data
= (data
<< 8) | (*(uchar
*)cp
);
594 return (write_word(info
, wp
, data
));
597 /*-----------------------------------------------------------------------
598 * Write a word to Flash, returns:
601 * 2 - Flash not erased
603 static int write_word (flash_info_t
*info
, ulong dest
, ulong data
)
605 vu_long
*addr
= (vu_long
*)(info
->start
[0]);
609 /* Check if Flash is (sufficiently) erased */
610 if ((*((vu_long
*)dest
) & data
) != data
) {
613 /* Disable interrupts which might cause a timeout here */
614 flag
= disable_interrupts();
616 addr
[0x0555] = 0x00AA00AA;
617 addr
[0x02AA] = 0x00550055;
618 addr
[0x0555] = 0x00A000A0;
620 *((vu_long
*)dest
) = data
;
622 /* re-enable interrupts if necessary */
626 /* data polling for D7 */
627 start
= get_timer (0);
628 while ((*((vu_long
*)dest
) & 0x00800080) != (data
& 0x00800080)) {
629 if (get_timer(start
) > CONFIG_SYS_FLASH_WRITE_TOUT
) {
636 /*-----------------------------------------------------------------------