6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 * See file CREDITS for list of people who contributed to this
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 #include <asm/u-boot.h>
30 #include <asm/processor.h>
32 flash_info_t flash_info
[CONFIG_SYS_MAX_FLASH_BANKS
]; /* info for FLASH chips */
35 #ifdef CONFIG_SYS_FLASH_16BIT
36 #define FLASH_WORD_SIZE unsigned short
37 #define FLASH_ID_MASK 0xFFFF
39 #define FLASH_WORD_SIZE unsigned long
40 #define FLASH_ID_MASK 0xFFFFFFFF
43 /*-----------------------------------------------------------------------
46 /* stolen from esteem192e/flash.c */
47 ulong
flash_get_size (volatile FLASH_WORD_SIZE
*addr
, flash_info_t
*info
);
49 #ifndef CONFIG_SYS_FLASH_16BIT
50 static int write_word (flash_info_t
*info
, ulong dest
, ulong data
);
52 static int write_short (flash_info_t
*info
, ulong dest
, ushort data
);
54 static void flash_get_offsets (ulong base
, flash_info_t
*info
);
57 /*-----------------------------------------------------------------------
60 unsigned long flash_init (void)
62 unsigned long size_b0
, size_b1
;
65 unsigned long base_b0
, base_b1
;
67 /* Init: no FLASHes known */
68 for (i
=0; i
<CONFIG_SYS_MAX_FLASH_BANKS
; ++i
) {
69 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
72 /* Static FLASH Bank configuration here - FIXME XXX */
74 size_b0
= flash_get_size((volatile FLASH_WORD_SIZE
*)FLASH_BASE1_PRELIM
, &flash_info
[0]);
76 if (flash_info
[0].flash_id
== FLASH_UNKNOWN
) {
77 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
78 size_b0
, size_b0
<<20);
82 if (CONFIG_SYS_MAX_FLASH_BANKS
== 1) {
84 flash_get_offsets (FLASH_BASE1_PRELIM
, &flash_info
[0]);
86 /* Monitor protection ON by default */
88 (void)flash_protect(FLAG_PROTECT_SET
,
89 FLASH_BASE1_PRELIM
-CONFIG_SYS_MONITOR_LEN
+size_b0
,
90 FLASH_BASE1_PRELIM
-1+size_b0
,
93 (void)flash_protect(FLAG_PROTECT_SET
,
94 CONFIG_SYS_MONITOR_BASE
,
95 CONFIG_SYS_MONITOR_BASE
+CONFIG_SYS_MONITOR_LEN
-1,
99 flash_info
[0].size
= size_b0
;
100 } else { /* 2 banks */
101 size_b1
= flash_get_size((volatile FLASH_WORD_SIZE
*)FLASH_BASE1_PRELIM
, &flash_info
[1]);
103 /* Re-do sizing to get full correct info */
105 mtdcr(EBC0_CFGADDR
, PB0CR
);
106 pbcr
= mfdcr(EBC0_CFGDATA
);
107 mtdcr(EBC0_CFGADDR
, PB0CR
);
109 pbcr
= (pbcr
& 0x0001ffff) | base_b1
| (((size_b1
/1024/1024)-1)<<17);
110 mtdcr(EBC0_CFGDATA
, pbcr
);
114 mtdcr(EBC0_CFGADDR
, PB1CR
);
115 pbcr
= mfdcr(EBC0_CFGDATA
);
116 mtdcr(EBC0_CFGADDR
, PB1CR
);
117 base_b0
= base_b1
- size_b0
;
118 pbcr
= (pbcr
& 0x0001ffff) | base_b0
| (((size_b0
/1024/1024)-1)<<17);
119 mtdcr(EBC0_CFGDATA
, pbcr
);
122 size_b0
= flash_get_size((volatile FLASH_WORD_SIZE
*)base_b0
, &flash_info
[0]);
124 flash_get_offsets (base_b0
, &flash_info
[0]);
126 /* monitor protection ON by default */
128 (void)flash_protect(FLAG_PROTECT_SET
,
129 FLASH_BASE1_PRELIM
-CONFIG_SYS_MONITOR_LEN
+size_b0
,
130 FLASH_BASE1_PRELIM
-1+size_b0
,
133 (void)flash_protect(FLAG_PROTECT_SET
,
134 CONFIG_SYS_MONITOR_BASE
,
135 CONFIG_SYS_MONITOR_BASE
+CONFIG_SYS_MONITOR_LEN
-1,
140 /* Re-do sizing to get full correct info */
141 size_b1
= flash_get_size((volatile FLASH_WORD_SIZE
*)base_b1
, &flash_info
[1]);
143 flash_get_offsets (base_b1
, &flash_info
[1]);
145 /* monitor protection ON by default */
146 (void)flash_protect(FLAG_PROTECT_SET
,
147 base_b1
+size_b1
-CONFIG_SYS_MONITOR_LEN
,
150 /* monitor protection OFF by default (one is enough) */
151 (void)flash_protect(FLAG_PROTECT_CLEAR
,
152 base_b0
+size_b0
-CONFIG_SYS_MONITOR_LEN
,
156 flash_info
[1].flash_id
= FLASH_UNKNOWN
;
157 flash_info
[1].sector_count
= -1;
160 flash_info
[0].size
= size_b0
;
161 flash_info
[1].size
= size_b1
;
163 return (size_b0
+ size_b1
);
167 /*-----------------------------------------------------------------------
170 static void flash_get_offsets (ulong base
, flash_info_t
*info
)
174 /* set up sector start adress table */
175 if ((info
->flash_id
& FLASH_TYPEMASK
) == INTEL_ID_28F320J3A
||
176 (info
->flash_id
& FLASH_TYPEMASK
) == INTEL_ID_28F640J3A
||
177 (info
->flash_id
& FLASH_TYPEMASK
) == INTEL_ID_28F128J3A
) {
178 for (i
= 0; i
< info
->sector_count
; i
++) {
179 info
->start
[i
] = base
+ (i
* info
->size
/info
->sector_count
);
182 else if (info
->flash_id
& FLASH_BTYPE
) {
183 if ((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_INTEL
) {
185 #ifndef CONFIG_SYS_FLASH_16BIT
186 /* set sector offsets for bottom boot block type */
187 info
->start
[0] = base
+ 0x00000000;
188 info
->start
[1] = base
+ 0x00004000;
189 info
->start
[2] = base
+ 0x00008000;
190 info
->start
[3] = base
+ 0x0000C000;
191 info
->start
[4] = base
+ 0x00010000;
192 info
->start
[5] = base
+ 0x00014000;
193 info
->start
[6] = base
+ 0x00018000;
194 info
->start
[7] = base
+ 0x0001C000;
195 for (i
= 8; i
< info
->sector_count
; i
++) {
196 info
->start
[i
] = base
+ (i
* 0x00020000) - 0x000E0000;
199 /* set sector offsets for bottom boot block type */
200 info
->start
[0] = base
+ 0x00000000;
201 info
->start
[1] = base
+ 0x00008000;
202 info
->start
[2] = base
+ 0x0000C000;
203 info
->start
[3] = base
+ 0x00010000;
204 for (i
= 4; i
< info
->sector_count
; i
++) {
205 info
->start
[i
] = base
+ (i
* 0x00020000) - 0x00060000;
209 /* set sector offsets for bottom boot block type */
210 info
->start
[0] = base
+ 0x00000000;
211 info
->start
[1] = base
+ 0x00002000;
212 info
->start
[2] = base
+ 0x00004000;
213 info
->start
[3] = base
+ 0x00006000;
214 info
->start
[4] = base
+ 0x00008000;
215 info
->start
[5] = base
+ 0x0000A000;
216 info
->start
[6] = base
+ 0x0000C000;
217 info
->start
[7] = base
+ 0x0000E000;
218 for (i
= 8; i
< info
->sector_count
; i
++) {
219 info
->start
[i
] = base
+ (i
* 0x00010000) - 0x00070000;
222 /* set sector offsets for bottom boot block type */
223 info
->start
[0] = base
+ 0x00000000;
224 info
->start
[1] = base
+ 0x00004000;
225 info
->start
[2] = base
+ 0x00006000;
226 info
->start
[3] = base
+ 0x00008000;
227 for (i
= 4; i
< info
->sector_count
; i
++) {
228 info
->start
[i
] = base
+ (i
* 0x00010000) - 0x00030000;
233 /* set sector offsets for top boot block type */
234 i
= info
->sector_count
- 1;
235 if ((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_INTEL
) {
237 #ifndef CONFIG_SYS_FLASH_16BIT
238 info
->start
[i
--] = base
+ info
->size
- 0x00004000;
239 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
240 info
->start
[i
--] = base
+ info
->size
- 0x0000C000;
241 info
->start
[i
--] = base
+ info
->size
- 0x00010000;
242 info
->start
[i
--] = base
+ info
->size
- 0x00014000;
243 info
->start
[i
--] = base
+ info
->size
- 0x00018000;
244 info
->start
[i
--] = base
+ info
->size
- 0x0001C000;
245 for (; i
>= 0; i
--) {
246 info
->start
[i
] = base
+ i
* 0x00020000;
250 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
251 info
->start
[i
--] = base
+ info
->size
- 0x0000C000;
252 info
->start
[i
--] = base
+ info
->size
- 0x00010000;
253 for (; i
>= 0; i
--) {
254 info
->start
[i
] = base
+ i
* 0x00020000;
258 info
->start
[i
--] = base
+ info
->size
- 0x00002000;
259 info
->start
[i
--] = base
+ info
->size
- 0x00004000;
260 info
->start
[i
--] = base
+ info
->size
- 0x00006000;
261 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
262 info
->start
[i
--] = base
+ info
->size
- 0x0000A000;
263 info
->start
[i
--] = base
+ info
->size
- 0x0000C000;
264 info
->start
[i
--] = base
+ info
->size
- 0x0000E000;
265 for (; i
>= 0; i
--) {
266 info
->start
[i
] = base
+ i
* 0x00010000;
269 info
->start
[i
--] = base
+ info
->size
- 0x00004000;
270 info
->start
[i
--] = base
+ info
->size
- 0x00006000;
271 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
272 for (; i
>= 0; i
--) {
273 info
->start
[i
] = base
+ i
* 0x00010000;
280 /*-----------------------------------------------------------------------
283 void flash_print_info (flash_info_t
*info
)
287 uchar botboot
[]=", bottom boot sect)\n";
288 uchar topboot
[]=", top boot sector)\n";
290 if (info
->flash_id
== FLASH_UNKNOWN
) {
291 printf ("missing or unknown FLASH type\n");
295 switch (info
->flash_id
& FLASH_VENDMASK
) {
296 case FLASH_MAN_AMD
: printf ("AMD "); break;
297 case FLASH_MAN_FUJ
: printf ("FUJITSU "); break;
298 case FLASH_MAN_SST
: printf ("SST "); break;
299 case FLASH_MAN_STM
: printf ("STM "); break;
300 case FLASH_MAN_INTEL
: printf ("INTEL "); break;
301 default: printf ("Unknown Vendor "); break;
304 if (info
->flash_id
& 0x0001 ) {
310 switch (info
->flash_id
& FLASH_TYPEMASK
) {
311 case FLASH_AM400B
: printf ("AM29LV400B (4 Mbit%s",boottype
);
313 case FLASH_AM400T
: printf ("AM29LV400T (4 Mbit%s",boottype
);
315 case FLASH_AM800B
: printf ("AM29LV800B (8 Mbit%s",boottype
);
317 case FLASH_AM800T
: printf ("AM29LV800T (8 Mbit%s",boottype
);
319 case FLASH_AM160B
: printf ("AM29LV160B (16 Mbit%s",boottype
);
321 case FLASH_AM160T
: printf ("AM29LV160T (16 Mbit%s",boottype
);
323 case FLASH_AM320B
: printf ("AM29LV320B (32 Mbit%s",boottype
);
325 case FLASH_AM320T
: printf ("AM29LV320T (32 Mbit%s",boottype
);
327 case FLASH_INTEL800B
: printf ("INTEL28F800B (8 Mbit%s",boottype
);
329 case FLASH_INTEL800T
: printf ("INTEL28F800T (8 Mbit%s",boottype
);
331 case FLASH_INTEL160B
: printf ("INTEL28F160B (16 Mbit%s",boottype
);
333 case FLASH_INTEL160T
: printf ("INTEL28F160T (16 Mbit%s",boottype
);
335 case FLASH_INTEL320B
: printf ("INTEL28F320B (32 Mbit%s",boottype
);
337 case FLASH_INTEL320T
: printf ("INTEL28F320T (32 Mbit%s",boottype
);
339 case FLASH_AMDL322T
: printf ("AM29DL322T (32 Mbit%s",boottype
);
342 #if 0 /* enable when devices are available */
344 case FLASH_INTEL640B
: printf ("INTEL28F640B (64 Mbit%s",boottype
);
346 case FLASH_INTEL640T
: printf ("INTEL28F640T (64 Mbit%s",boottype
);
349 case INTEL_ID_28F320J3A
: printf ("INTEL28F320JA3 (32 Mbit%s",boottype
);
351 case INTEL_ID_28F640J3A
: printf ("INTEL28F640JA3 (64 Mbit%s",boottype
);
353 case INTEL_ID_28F128J3A
: printf ("INTEL28F128JA3 (128 Mbit%s",boottype
);
356 default: printf ("Unknown Chip Type\n");
360 printf (" Size: %ld MB in %d Sectors\n",
361 info
->size
>> 20, info
->sector_count
);
363 printf (" Sector Start Addresses:");
364 for (i
=0; i
<info
->sector_count
; ++i
) {
369 info
->protect
[i
] ? " (RO)" : " "
377 /*-----------------------------------------------------------------------
381 /*-----------------------------------------------------------------------
385 * The following code cannot be run from FLASH!
387 ulong
flash_get_size (volatile FLASH_WORD_SIZE
*addr
, flash_info_t
*info
)
390 ulong base
= (ulong
)addr
;
391 FLASH_WORD_SIZE value
;
393 /* Write auto select command: read Manufacturer ID */
396 #ifndef CONFIG_SYS_FLASH_16BIT
399 * Note: if it is an AMD flash and the word at addr[0000]
400 * is 0x00890089 this routine will think it is an Intel
401 * flash device and may(most likely) cause trouble.
404 addr
[0x0000] = 0x00900090;
405 if(addr
[0x0000] != 0x00890089){
406 addr
[0x0555] = 0x00AA00AA;
407 addr
[0x02AA] = 0x00550055;
408 addr
[0x0555] = 0x00900090;
412 * Note: if it is an AMD flash and the word at addr[0000]
413 * is 0x0089 this routine will think it is an Intel
414 * flash device and may(most likely) cause trouble.
417 addr
[0x0000] = 0x0090;
419 if(addr
[0x0000] != 0x0089){
420 addr
[0x0555] = 0x00AA;
421 addr
[0x02AA] = 0x0055;
422 addr
[0x0555] = 0x0090;
428 case (AMD_MANUFACT
& FLASH_ID_MASK
):
429 info
->flash_id
= FLASH_MAN_AMD
;
431 case (FUJ_MANUFACT
& FLASH_ID_MASK
):
432 info
->flash_id
= FLASH_MAN_FUJ
;
434 case (STM_MANUFACT
& FLASH_ID_MASK
):
435 info
->flash_id
= FLASH_MAN_STM
;
437 case (SST_MANUFACT
& FLASH_ID_MASK
):
438 info
->flash_id
= FLASH_MAN_SST
;
440 case (INTEL_MANUFACT
& FLASH_ID_MASK
):
441 info
->flash_id
= FLASH_MAN_INTEL
;
444 info
->flash_id
= FLASH_UNKNOWN
;
445 info
->sector_count
= 0;
447 return (0); /* no or unknown flash */
451 value
= addr
[1]; /* device ID */
455 case (AMD_ID_LV400T
& FLASH_ID_MASK
):
456 info
->flash_id
+= FLASH_AM400T
;
457 info
->sector_count
= 11;
458 info
->size
= 0x00100000;
461 case (AMD_ID_LV400B
& FLASH_ID_MASK
):
462 info
->flash_id
+= FLASH_AM400B
;
463 info
->sector_count
= 11;
464 info
->size
= 0x00100000;
467 case (AMD_ID_LV800T
& FLASH_ID_MASK
):
468 info
->flash_id
+= FLASH_AM800T
;
469 info
->sector_count
= 19;
470 info
->size
= 0x00200000;
473 case (AMD_ID_LV800B
& FLASH_ID_MASK
):
474 info
->flash_id
+= FLASH_AM800B
;
475 info
->sector_count
= 19;
476 info
->size
= 0x00200000;
479 case (AMD_ID_LV160T
& FLASH_ID_MASK
):
480 info
->flash_id
+= FLASH_AM160T
;
481 info
->sector_count
= 35;
482 info
->size
= 0x00400000;
485 case (AMD_ID_LV160B
& FLASH_ID_MASK
):
486 info
->flash_id
+= FLASH_AM160B
;
487 info
->sector_count
= 35;
488 info
->size
= 0x00400000;
490 #if 0 /* enable when device IDs are available */
491 case (AMD_ID_LV320T
& FLASH_ID_MASK
):
492 info
->flash_id
+= FLASH_AM320T
;
493 info
->sector_count
= 67;
494 info
->size
= 0x00800000;
497 case (AMD_ID_LV320B
& FLASH_ID_MASK
):
498 info
->flash_id
+= FLASH_AM320B
;
499 info
->sector_count
= 67;
500 info
->size
= 0x00800000;
504 case (AMD_ID_DL322T
& FLASH_ID_MASK
):
505 info
->flash_id
+= FLASH_AMDL322T
;
506 info
->sector_count
= 71;
507 info
->size
= 0x00800000;
510 case (INTEL_ID_28F800B3T
& FLASH_ID_MASK
):
511 info
->flash_id
+= FLASH_INTEL800T
;
512 info
->sector_count
= 23;
513 info
->size
= 0x00200000;
516 case (INTEL_ID_28F800B3B
& FLASH_ID_MASK
):
517 info
->flash_id
+= FLASH_INTEL800B
;
518 info
->sector_count
= 23;
519 info
->size
= 0x00200000;
522 case (INTEL_ID_28F160B3T
& FLASH_ID_MASK
):
523 info
->flash_id
+= FLASH_INTEL160T
;
524 info
->sector_count
= 39;
525 info
->size
= 0x00400000;
528 case (INTEL_ID_28F160B3B
& FLASH_ID_MASK
):
529 info
->flash_id
+= FLASH_INTEL160B
;
530 info
->sector_count
= 39;
531 info
->size
= 0x00400000;
534 case (INTEL_ID_28F320B3T
& FLASH_ID_MASK
):
535 info
->flash_id
+= FLASH_INTEL320T
;
536 info
->sector_count
= 71;
537 info
->size
= 0x00800000;
540 case (INTEL_ID_28F320B3B
& FLASH_ID_MASK
):
541 info
->flash_id
+= FLASH_AM320B
;
542 info
->sector_count
= 71;
543 info
->size
= 0x00800000;
546 #if 0 /* enable when devices are available */
547 case (INTEL_ID_28F320B3T
& FLASH_ID_MASK
):
548 info
->flash_id
+= FLASH_INTEL320T
;
549 info
->sector_count
= 135;
550 info
->size
= 0x01000000;
551 break; /* => 16 MB */
553 case (INTEL_ID_28F320B3B
& FLASH_ID_MASK
):
554 info
->flash_id
+= FLASH_AM320B
;
555 info
->sector_count
= 135;
556 info
->size
= 0x01000000;
557 break; /* => 16 MB */
559 case (INTEL_ID_28F320J3A
& FLASH_ID_MASK
):
560 info
->flash_id
+= FLASH_28F320J3A
;
561 info
->sector_count
= 32;
562 info
->size
= 0x00400000;
563 break; /* => 32 MBit */
564 case (INTEL_ID_28F640J3A
& FLASH_ID_MASK
):
565 info
->flash_id
+= FLASH_28F640J3A
;
566 info
->sector_count
= 64;
567 info
->size
= 0x00800000;
568 break; /* => 64 MBit */
569 case (INTEL_ID_28F128J3A
& FLASH_ID_MASK
):
570 info
->flash_id
+= FLASH_28F128J3A
;
571 info
->sector_count
= 128;
572 info
->size
= 0x01000000;
573 break; /* => 128 MBit */
577 info
->flash_id
= FLASH_UNKNOWN
;
578 return (0); /* => no or unknown flash */
581 flash_get_offsets(base
, info
);
583 /* check for protected sectors */
584 for (i
= 0; i
< info
->sector_count
; i
++) {
585 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
586 /* D0 = 1 if protected */
587 addr
= (volatile FLASH_WORD_SIZE
*)(info
->start
[i
]);
588 info
->protect
[i
] = addr
[2] & 1;
592 * Prevent writes to uninitialized FLASH.
594 if (info
->flash_id
!= FLASH_UNKNOWN
) {
595 addr
= (volatile FLASH_WORD_SIZE
*)info
->start
[0];
596 if( (info
->flash_id
& 0xFF00) == FLASH_MAN_INTEL
){
597 *addr
= (0x00F000F0 & FLASH_ID_MASK
); /* reset bank */
599 *addr
= (0x00FF00FF & FLASH_ID_MASK
); /* reset bank */
607 /*-----------------------------------------------------------------------
610 int flash_erase (flash_info_t
* info
, int s_first
, int s_last
)
613 volatile FLASH_WORD_SIZE
*addr
=
614 (volatile FLASH_WORD_SIZE
*) (info
->start
[0]);
615 int flag
, prot
, sect
, l_sect
, barf
;
616 ulong start
, now
, last
;
619 if ((s_first
< 0) || (s_first
> s_last
)) {
620 if (info
->flash_id
== FLASH_UNKNOWN
) {
621 printf ("- missing\n");
623 printf ("- no sectors to erase\n");
628 if ((info
->flash_id
== FLASH_UNKNOWN
) ||
629 ((info
->flash_id
> FLASH_AMD_COMP
) &&
630 ((info
->flash_id
& FLASH_VENDMASK
) != FLASH_MAN_INTEL
))) {
631 printf ("Can't erase unknown flash type - aborted\n");
636 for (sect
= s_first
; sect
<= s_last
; ++sect
) {
637 if (info
->protect
[sect
]) {
643 printf ("- Warning: %d protected sectors will not be erased!\n", prot
);
650 /* Disable interrupts which might cause a timeout here */
651 flag
= disable_interrupts ();
652 if (info
->flash_id
< FLASH_AMD_COMP
) {
653 #ifndef CONFIG_SYS_FLASH_16BIT
654 addr
[0x0555] = 0x00AA00AA;
655 addr
[0x02AA] = 0x00550055;
656 addr
[0x0555] = 0x00800080;
657 addr
[0x0555] = 0x00AA00AA;
658 addr
[0x02AA] = 0x00550055;
660 addr
[0x0555] = 0x00AA;
661 addr
[0x02AA] = 0x0055;
662 addr
[0x0555] = 0x0080;
663 addr
[0x0555] = 0x00AA;
664 addr
[0x02AA] = 0x0055;
666 /* Start erase on unprotected sectors */
667 for (sect
= s_first
; sect
<= s_last
; sect
++) {
668 if (info
->protect
[sect
] == 0) { /* not protected */
669 addr
= (volatile FLASH_WORD_SIZE
*) (info
->start
[sect
]);
670 addr
[0] = (0x00300030 & FLASH_ID_MASK
);
675 /* re-enable interrupts if necessary */
677 enable_interrupts ();
679 /* wait at least 80us - let's wait 1 ms */
683 * We wait for the last triggered sector
688 start
= get_timer (0);
690 addr
= (volatile FLASH_WORD_SIZE
*) (info
->start
[l_sect
]);
691 while ((addr
[0] & (0x00800080 & FLASH_ID_MASK
)) !=
692 (0x00800080 & FLASH_ID_MASK
)) {
693 if ((now
= get_timer (start
)) > CONFIG_SYS_FLASH_ERASE_TOUT
) {
694 printf ("Timeout\n");
697 /* show that we're waiting */
698 if ((now
- last
) > 1000) { /* every second */
705 /* reset to read mode */
706 addr
= (volatile FLASH_WORD_SIZE
*) info
->start
[0];
707 addr
[0] = (0x00F000F0 & FLASH_ID_MASK
); /* reset bank */
711 for (sect
= s_first
; sect
<= s_last
; sect
++) {
712 if (info
->protect
[sect
] == 0) { /* not protected */
714 #ifndef CONFIG_SYS_FLASH_16BIT
715 addr
= (vu_long
*) (info
->start
[sect
]);
716 addr
[0] = 0x00200020;
717 addr
[0] = 0x00D000D0;
718 while (!(addr
[0] & 0x00800080)); /* wait for error or finish */
719 if (addr
[0] & 0x003A003A) { /* check for error */
720 barf
= addr
[0] & 0x003A0000;
724 barf
= addr
[0] & 0x0000003A;
728 addr
= (vu_short
*) (info
->start
[sect
]);
731 while (!(addr
[0] & 0x0080)); /* wait for error or finish */
732 if (addr
[0] & 0x003A) /* check for error */
733 barf
= addr
[0] & 0x003A;
736 printf ("\nFlash error in sector at %lx\n",
737 (unsigned long) addr
);
739 printf ("Block locked, not erased.\n");
740 if ((barf
& 0x0030) == 0x0030)
741 printf ("Command Sequence error.\n");
742 if ((barf
& 0x0030) == 0x0020)
743 printf ("Block Erase error.\n");
745 printf ("Vpp Low error.\n");
751 addr
= (volatile FLASH_WORD_SIZE
*) info
->start
[0];
752 addr
[0] = (0x00FF00FF & FLASH_ID_MASK
); /* reset bank */
761 /*-----------------------------------------------------------------------
764 /*flash_info_t *addr2info (ulong addr)
769 for (i=0, info=&flash_info[0]; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
770 if ((addr >= info->start[0]) &&
771 (addr < (info->start[0] + info->size)) ) {
779 /*-----------------------------------------------------------------------
780 * Copy memory to flash.
781 * Make sure all target addresses are within Flash bounds,
782 * and no protected sectors are hit.
786 * 2 - Flash not erased
787 * 4 - target range includes protected sectors
788 * 8 - target address not in Flash memory
791 /*int flash_write (uchar *src, ulong addr, ulong cnt)
794 ulong end = addr + cnt - 1;
795 flash_info_t *info_first = addr2info (addr);
796 flash_info_t *info_last = addr2info (end );
803 if (!info_first || !info_last) {
807 for (info = info_first; info <= info_last; ++info) {
808 ulong b_end = info->start[0] + info->size;*/ /* bank end addr */
809 /* short s_end = info->sector_count - 1;
810 for (i=0; i<info->sector_count; ++i) {
811 ulong e_addr = (i == s_end) ? b_end : info->start[i + 1];
813 if ((end >= info->start[i]) && (addr < e_addr) &&
814 (info->protect[i] != 0) ) {
820 */ /* finally write data to flash */
821 /* for (info = info_first; info <= info_last && cnt>0; ++info) {
824 len = info->start[0] + info->size - addr;
827 if ((i = write_buff(info, src, addr, len)) != 0) {
837 /*-----------------------------------------------------------------------
838 * Copy memory to flash, returns:
841 * 2 - Flash not erased
844 int write_buff (flash_info_t
*info
, uchar
*src
, ulong addr
, ulong cnt
)
846 #ifndef CONFIG_SYS_FLASH_16BIT
855 #ifndef CONFIG_SYS_FLASH_16BIT
858 wp
= (addr
& ~3); /* get lower word aligned address */
861 * handle unaligned start bytes
863 if ((l
= addr
- wp
) != 0) {
865 for (i
=0, cp
=wp
; i
<l
; ++i
, ++cp
) {
866 data
= (data
<< 8) | (*(uchar
*)cp
);
868 for (; i
<4 && cnt
>0; ++i
) {
869 data
= (data
<< 8) | *src
++;
873 for (; cnt
==0 && i
<4; ++i
, ++cp
) {
874 data
= (data
<< 8) | (*(uchar
*)cp
);
877 if ((rc
= write_word(info
, wp
, data
)) != 0) {
884 * handle word aligned part
888 for (i
=0; i
<4; ++i
) {
889 data
= (data
<< 8) | *src
++;
891 if ((rc
= write_word(info
, wp
, data
)) != 0) {
903 * handle unaligned tail bytes
906 for (i
=0, cp
=wp
; i
<4 && cnt
>0; ++i
, ++cp
) {
907 data
= (data
<< 8) | *src
++;
910 for (; i
<4; ++i
, ++cp
) {
911 data
= (data
<< 8) | (*(uchar
*)cp
);
914 return (write_word(info
, wp
, data
));
917 wp
= (addr
& ~1); /* get lower word aligned address */
920 * handle unaligned start byte
924 data
= (data
<< 8) | *src
++;
926 if ((rc
= write_short(info
, wp
, data
)) != 0) {
933 * handle word aligned part
935 /* l = 0; used for debuging */
938 for (i
=0; i
<2; ++i
) {
939 data
= (data
<< 8) | *src
++;
945 } used for debuging */
947 if ((rc
= write_short(info
, wp
, data
)) != 0) {
959 * handle unaligned tail bytes
962 for (i
=0, cp
=wp
; i
<2 && cnt
>0; ++i
, ++cp
) {
963 data
= (data
<< 8) | *src
++;
966 for (; i
<2; ++i
, ++cp
) {
967 data
= (data
<< 8) | (*(uchar
*)cp
);
970 return (write_short(info
, wp
, data
));
976 /*-----------------------------------------------------------------------
977 * Write a word to Flash, returns:
980 * 2 - Flash not erased
982 #ifndef CONFIG_SYS_FLASH_16BIT
983 static int write_word (flash_info_t
*info
, ulong dest
, ulong data
)
985 vu_long
*addr
= (vu_long
*)(info
->start
[0]);
990 /* Check if Flash is (sufficiently) erased */
991 if ((*((vu_long
*)dest
) & data
) != data
) {
995 /* Disable interrupts which might cause a timeout here */
996 flag
= disable_interrupts();
998 if(info
->flash_id
> FLASH_AMD_COMP
) {
1000 addr
[0x0555] = 0x00AA00AA;
1001 addr
[0x02AA] = 0x00550055;
1002 addr
[0x0555] = 0x00A000A0;
1007 *((vu_long
*)dest
) = data
;
1009 /* re-enable interrupts if necessary */
1011 enable_interrupts();
1013 /* data polling for D7 */
1014 start
= get_timer (0);
1016 if(info
->flash_id
> FLASH_AMD_COMP
) {
1017 while ((*((vu_long
*)dest
) & 0x00800080) != (data
& 0x00800080)) {
1018 if (get_timer(start
) > CONFIG_SYS_FLASH_WRITE_TOUT
) {
1023 while(!(addr
[0] & 0x00800080)) { /* wait for error or finish */
1024 if (get_timer(start
) > CONFIG_SYS_FLASH_WRITE_TOUT
) {
1028 if( addr
[0] & 0x003A003A) { /* check for error */
1029 barf
= addr
[0] & 0x003A0000;
1033 barf
= addr
[0] & 0x0000003A;
1035 printf("\nFlash write error at address %lx\n",(unsigned long)dest
);
1036 if(barf
& 0x0002) printf("Block locked, not erased.\n");
1037 if(barf
& 0x0010) printf("Programming error.\n");
1038 if(barf
& 0x0008) printf("Vpp Low error.\n");
1048 static int write_short (flash_info_t
*info
, ulong dest
, ushort data
)
1050 vu_short
*addr
= (vu_short
*)(info
->start
[0]);
1054 /* Check if Flash is (sufficiently) erased */
1055 if ((*((vu_short
*)dest
) & data
) != data
) {
1059 /* Disable interrupts which might cause a timeout here */
1060 flag
= disable_interrupts();
1062 if(info
->flash_id
< FLASH_AMD_COMP
) {
1064 addr
[0x0555] = 0x00AA;
1065 addr
[0x02AA] = 0x0055;
1066 addr
[0x0555] = 0x00A0;
1072 *((vu_short
*)dest
) = data
;
1074 /* re-enable interrupts if necessary */
1076 enable_interrupts();
1078 /* data polling for D7 */
1079 start
= get_timer (0);
1081 if(info
->flash_id
< FLASH_AMD_COMP
) {
1083 while ((*((vu_short
*)dest
) & 0x0080) != (data
& 0x0080)) {
1084 if (get_timer(start
) > CONFIG_SYS_FLASH_WRITE_TOUT
) {
1091 while(!(addr
[0] & 0x0080)){ /* wait for error or finish */
1092 if (get_timer(start
) > CONFIG_SYS_FLASH_WRITE_TOUT
) return (1);
1095 if( addr
[0] & 0x003A) { /* check for error */
1096 barf
= addr
[0] & 0x003A;
1097 printf("\nFlash write error at address %lx\n",(unsigned long)dest
);
1098 if(barf
& 0x0002) printf("Block locked, not erased.\n");
1099 if(barf
& 0x0010) printf("Programming error.\n");
1100 if(barf
& 0x0008) printf("Vpp Low error.\n");
1105 while(!(addr
[0] & 0x0080)){ /* wait for error or finish */
1106 if (get_timer(start
) > CONFIG_SYS_FLASH_WRITE_TOUT
) return (1);
1115 /*-----------------------------------------------------------------------*/