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,
27 #ifndef CONFIG_ENV_ADDR
28 #define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
31 #define CONFIG_FLASH_16BIT
33 flash_info_t flash_info
[CONFIG_SYS_MAX_FLASH_BANKS
]; /* info for FLASH chips */
35 /*-----------------------------------------------------------------------
38 static ulong
flash_get_size (vu_long
*addr
, flash_info_t
*info
);
39 static int write_word (flash_info_t
*info
, ulong dest
, ulong data
);
41 /*-----------------------------------------------------------------------
44 unsigned long flash_init (void)
46 volatile immap_t
*immap
= (immap_t
*)CONFIG_SYS_IMMR
;
47 volatile memctl8xx_t
*memctl
= &immap
->im_memctl
;
48 unsigned long size_b0
;
51 /* Init: no FLASHes known */
52 for (i
=0; i
<CONFIG_SYS_MAX_FLASH_BANKS
; ++i
) {
53 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
56 /* Static FLASH Bank configuration here - FIXME XXX */
58 size_b0
= flash_get_size((vu_long
*)FLASH_BASE0_PRELIM
, &flash_info
[0]);
60 if (flash_info
[0].flash_id
== FLASH_UNKNOWN
) {
61 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
62 size_b0
, size_b0
<<20);
66 /* Remap FLASH according to real size */
67 memctl
->memc_or0
= CONFIG_SYS_OR_TIMING_FLASH
| (-size_b0
& OR_AM_MSK
);
68 memctl
->memc_br0
= (CONFIG_SYS_FLASH_BASE
& BR_BA_MSK
) | BR_MS_GPCM
| BR_V
| BR_PS_16
;
70 /* Re-do sizing to get full correct info */
71 size_b0
= flash_get_size((vu_long
*)CONFIG_SYS_FLASH_BASE
, &flash_info
[0]);
73 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
74 /* monitor protection ON by default */
75 flash_protect(FLAG_PROTECT_SET
,
76 CONFIG_SYS_MONITOR_BASE
,
77 CONFIG_SYS_MONITOR_BASE
+monitor_flash_len
-1,
81 #ifdef CONFIG_ENV_IS_IN_FLASH
82 /* ENV protection ON by default */
83 flash_protect(FLAG_PROTECT_SET
,
85 CONFIG_ENV_ADDR
+CONFIG_ENV_SIZE
-1,
89 flash_info
[0].size
= size_b0
;
94 /*-----------------------------------------------------------------------
96 void flash_print_info (flash_info_t
*info
)
100 if (info
->flash_id
== FLASH_UNKNOWN
) {
101 printf ("missing or unknown FLASH type\n");
105 switch (info
->flash_id
& FLASH_VENDMASK
) {
106 case FLASH_MAN_AMD
: printf ("AMD "); break;
107 case FLASH_MAN_FUJ
: printf ("FUJITSU "); break;
108 default: printf ("Unknown Vendor "); break;
111 switch (info
->flash_id
& FLASH_TYPEMASK
) {
112 case FLASH_AM400B
: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
114 case FLASH_AM400T
: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
116 case FLASH_AM800B
: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
118 case FLASH_AM800T
: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
120 case FLASH_AM160B
: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
122 case FLASH_AM160T
: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
124 case FLASH_AM320B
: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
126 case FLASH_AM320T
: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
128 default: printf ("Unknown Chip Type\n");
132 printf (" Size: %ld MB in %d Sectors\n",
133 info
->size
>> 20, info
->sector_count
);
135 printf (" Sector Start Addresses:");
136 for (i
=0; i
<info
->sector_count
; ++i
) {
141 info
->protect
[i
] ? " (RO)" : " "
148 /*-----------------------------------------------------------------------
152 /*-----------------------------------------------------------------------
156 * The following code cannot be run from FLASH!
159 static ulong
flash_get_size (vu_long
*addr
, flash_info_t
*info
)
163 ulong base
= (ulong
)addr
;
165 /* Write auto select command: read Manufacturer ID */
166 vu_short
*s_addr
=(vu_short
*)addr
;
167 s_addr
[0x5555] = 0x00AA;
168 s_addr
[0x2AAA] = 0x0055;
169 s_addr
[0x5555] = 0x0090;
172 value
= value
|(value
<<16);
176 info
->flash_id
= FLASH_MAN_AMD
;
179 info
->flash_id
= FLASH_MAN_FUJ
;
182 info
->flash_id
= FLASH_UNKNOWN
;
183 info
->sector_count
= 0;
185 return (0); /* no or unknown flash */
189 value
= value
|(value
<<16);
192 case FUJI_ID_29F800BA
:
193 info
->flash_id
+= FLASH_AM400T
;
194 info
->sector_count
= 19;
195 info
->size
= 0x00100000;
198 info
->flash_id
+= FLASH_AM800T
;
199 info
->sector_count
= 19;
200 info
->size
= 0x00100000;
203 info
->flash_id
+= FLASH_AM800B
;
204 info
->sector_count
= 19;
205 info
->size
= 0x00100000;
208 info
->flash_id
= FLASH_UNKNOWN
;
209 return (0); /* => no or unknown flash */
212 /* set up sector start address table */
213 /* set sector offsets for bottom boot block type */
214 info
->start
[0] = base
+ 0x00000000;
215 info
->start
[1] = base
+ 0x00004000;
216 info
->start
[2] = base
+ 0x00006000;
217 info
->start
[3] = base
+ 0x00008000;
218 for (i
= 4; i
< info
->sector_count
; i
++) {
219 info
->start
[i
] = base
+ (i
* 0x00010000) - 0x00030000;
223 /* check for protected sectors */
224 for (i
= 0; i
< info
->sector_count
; i
++) {
225 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
226 /* D0 = 1 if protected */
227 s_addr
= (volatile unsigned short *)(info
->start
[i
]);
228 info
->protect
[i
] = s_addr
[2] & 1;
232 * Prevent writes to uninitialized FLASH.
234 if (info
->flash_id
!= FLASH_UNKNOWN
) {
235 s_addr
= (volatile unsigned short *)info
->start
[0];
236 *s_addr
= 0x00F0; /* reset bank */
242 /*-----------------------------------------------------------------------
246 int flash_erase (flash_info_t
*info
, int s_first
, int s_last
)
248 vu_long
*addr
= (vu_long
*)(info
->start
[0]);
249 int flag
, prot
, sect
;
250 ulong start
, now
, last
;
251 #ifdef CONFIG_FLASH_16BIT
252 vu_short
*s_addr
= (vu_short
*)addr
;
255 if ((s_first
< 0) || (s_first
> s_last
)) {
256 if (info
->flash_id
== FLASH_UNKNOWN
) {
257 printf ("- missing\n");
259 printf ("- no sectors to erase\n");
263 /*#ifndef CONFIG_FLASH_16BIT
265 type = (info->flash_id & FLASH_VENDMASK);
266 if ((type != FLASH_MAN_SST) && (type != FLASH_MAN_STM)) {
267 printf ("Can't erase unknown flash type %08lx - aborted\n",
273 for (sect
=s_first
; sect
<=s_last
; ++sect
) {
274 if (info
->protect
[sect
]) {
280 printf ("- Warning: %d protected sectors will not be erased!\n",
286 start
= get_timer (0);
288 /* Start erase on unprotected sectors */
289 for (sect
= s_first
; sect
<=s_last
; sect
++) {
290 if (info
->protect
[sect
] == 0) { /* not protected */
291 #ifdef CONFIG_FLASH_16BIT
292 vu_short
*s_sect_addr
= (vu_short
*)(info
->start
[sect
]);
294 vu_long
*sect_addr
= (vu_long
*)(info
->start
[sect
]);
296 /* Disable interrupts which might cause a timeout here */
297 flag
= disable_interrupts();
299 #ifdef CONFIG_FLASH_16BIT
301 /*printf("\ns_sect_addr=%x",s_sect_addr);*/
302 s_addr
[0x5555] = 0x00AA;
303 s_addr
[0x2AAA] = 0x0055;
304 s_addr
[0x5555] = 0x0080;
305 s_addr
[0x5555] = 0x00AA;
306 s_addr
[0x2AAA] = 0x0055;
307 s_sect_addr
[0] = 0x0030;
309 addr
[0x5555] = 0x00AA00AA;
310 addr
[0x2AAA] = 0x00550055;
311 addr
[0x5555] = 0x00800080;
312 addr
[0x5555] = 0x00AA00AA;
313 addr
[0x2AAA] = 0x00550055;
314 sect_addr
[0] = 0x00300030;
316 /* re-enable interrupts if necessary */
320 /* wait at least 80us - let's wait 1 ms */
323 #ifdef CONFIG_FLASH_16BIT
324 while ((s_sect_addr
[0] & 0x0080) != 0x0080) {
326 while ((sect_addr
[0] & 0x00800080) != 0x00800080) {
328 if ((now
= get_timer(start
)) > CONFIG_SYS_FLASH_ERASE_TOUT
) {
329 printf ("Timeout\n");
332 /* show that we're waiting */
333 if ((now
- last
) > 1000) { /* every second */
341 /* reset to read mode */
342 addr
= (volatile unsigned long *)info
->start
[0];
343 #ifdef CONFIG_FLASH_16BIT
344 s_addr
[0] = 0x00F0; /* reset bank */
346 addr
[0] = 0x00F000F0; /* reset bank */
354 /*-----------------------------------------------------------------------
355 * Copy memory to flash, returns:
358 * 2 - Flash not erased
361 int write_buff (flash_info_t
*info
, uchar
*src
, ulong addr
, ulong cnt
)
366 wp
= (addr
& ~3); /* get lower word aligned address */
369 * handle unaligned start bytes
371 if ((l
= addr
- wp
) != 0) {
373 for (i
=0, cp
=wp
; i
<l
; ++i
, ++cp
) {
374 data
= (data
<< 8) | (*(uchar
*)cp
);
376 for (; i
<4 && cnt
>0; ++i
) {
377 data
= (data
<< 8) | *src
++;
381 for (; cnt
==0 && i
<4; ++i
, ++cp
) {
382 data
= (data
<< 8) | (*(uchar
*)cp
);
385 if ((rc
= write_word(info
, wp
, data
)) != 0) {
392 * handle word aligned part
396 for (i
=0; i
<4; ++i
) {
397 data
= (data
<< 8) | *src
++;
399 if ((rc
= write_word(info
, wp
, data
)) != 0) {
411 * handle unaligned tail bytes
414 for (i
=0, cp
=wp
; i
<4 && cnt
>0; ++i
, ++cp
) {
415 data
= (data
<< 8) | *src
++;
418 for (; i
<4; ++i
, ++cp
) {
419 data
= (data
<< 8) | (*(uchar
*)cp
);
422 return (write_word(info
, wp
, data
));
426 /*-----------------------------------------------------------------------
427 * Write a word to Flash, returns:
430 * 2 - Flash not erased
432 static int write_word (flash_info_t
*info
, ulong dest
, ulong data
)
434 vu_long
*addr
= (vu_long
*)(info
->start
[0]);
436 #ifdef CONFIG_FLASH_16BIT
439 vu_short
*s_addr
= (vu_short
*)addr
;
444 /* Check if Flash is (sufficiently) erased */
445 if ((*((vu_long
*)dest
) & data
) != data
) {
449 #ifdef CONFIG_FLASH_16BIT
450 /* Write the 16 higher-bits */
451 /* Disable interrupts which might cause a timeout here */
452 flag
= disable_interrupts();
454 high_data
= ((data
>>16) & 0x0000ffff);
456 s_addr
[0x5555] = 0x00AA;
457 s_addr
[0x2AAA] = 0x0055;
458 s_addr
[0x5555] = 0x00A0;
460 *((vu_short
*)dest
) = high_data
;
463 /* re-enable interrupts if necessary */
467 /* data polling for D7 */
468 start
= get_timer (0);
469 while ((*((vu_short
*)dest
) & 0x0080) != (high_data
& 0x0080)) {
470 if (get_timer(start
) > CONFIG_SYS_FLASH_WRITE_TOUT
) {
476 /* Write the 16 lower-bits */
479 /* Disable interrupts which might cause a timeout here */
480 flag
= disable_interrupts();
481 #ifdef CONFIG_FLASH_16BIT
483 low_data
= (data
& 0x0000ffff);
485 s_addr
[0x5555] = 0x00AA;
486 s_addr
[0x2AAA] = 0x0055;
487 s_addr
[0x5555] = 0x00A0;
488 *((vu_short
*)dest
) = low_data
;
491 addr
[0x5555] = 0x00AA00AA;
492 addr
[0x2AAA] = 0x00550055;
493 addr
[0x5555] = 0x00A000A0;
494 *((vu_long
*)dest
) = data
;
497 /* re-enable interrupts if necessary */
501 /* data polling for D7 */
502 start
= get_timer (0);
504 #ifdef CONFIG_FLASH_16BIT
505 while ((*((vu_short
*)dest
) & 0x0080) != (low_data
& 0x0080)) {
507 while ((*((vu_long
*)dest
) & 0x00800080) != (data
& 0x00800080)) {
510 if (get_timer(start
) > CONFIG_SYS_FLASH_WRITE_TOUT
) {