]>
git.ipfire.org Git - people/ms/u-boot.git/blob - board/eric/flash.c
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,
26 #include <asm/processor.h>
28 flash_info_t flash_info
[CFG_MAX_FLASH_BANKS
]; /* info for FLASH chips */
31 #ifdef CFG_FLASH_16BIT
32 #define FLASH_WORD_SIZE unsigned short
33 #define FLASH_ID_MASK 0xFFFF
35 #define FLASH_WORD_SIZE unsigned long
36 #define FLASH_ID_MASK 0xFFFFFFFF
39 /*-----------------------------------------------------------------------
42 /* stolen from esteem192e/flash.c */
43 ulong
flash_get_size (volatile FLASH_WORD_SIZE
*addr
, flash_info_t
*info
);
45 #ifndef CFG_FLASH_16BIT
46 static int write_word (flash_info_t
*info
, ulong dest
, ulong data
);
48 static int write_short (flash_info_t
*info
, ulong dest
, ushort data
);
50 static void flash_get_offsets (ulong base
, flash_info_t
*info
);
53 /*-----------------------------------------------------------------------
56 unsigned long flash_init (void)
58 unsigned long size_b0
, size_b1
;
61 unsigned long base_b0
, base_b1
;
63 /* Init: no FLASHes known */
64 for (i
=0; i
<CFG_MAX_FLASH_BANKS
; ++i
) {
65 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
68 /* Static FLASH Bank configuration here - FIXME XXX */
70 size_b0
= flash_get_size((volatile FLASH_WORD_SIZE
*)FLASH_BASE0_PRELIM
, &flash_info
[0]);
72 if (flash_info
[0].flash_id
== FLASH_UNKNOWN
) {
73 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
74 size_b0
, size_b0
<<20);
78 if (CFG_MAX_FLASH_BANKS
== 1)
81 flash_get_offsets (FLASH_BASE0_PRELIM
, &flash_info
[0]);
83 /* Monitor protection ON by default */
85 (void)flash_protect(FLAG_PROTECT_SET
,
86 FLASH_BASE0_PRELIM
-monitor_flash_len
+size_b0
,
87 FLASH_BASE0_PRELIM
-1+size_b0
,
90 (void)flash_protect(FLAG_PROTECT_SET
,
92 CFG_MONITOR_BASE
+monitor_flash_len
-1,
96 flash_info
[0].size
= size_b0
;
102 size_b1
= flash_get_size((volatile FLASH_WORD_SIZE
*)FLASH_BASE1_PRELIM
, &flash_info
[1]);
104 /* Re-do sizing to get full correct info */
108 mtdcr(ebccfga
, pb0cr
);
109 pbcr
= mfdcr(ebccfgd
);
110 mtdcr(ebccfga
, pb0cr
);
112 pbcr
= (pbcr
& 0x0001ffff) | base_b1
| (((size_b1
/1024/1024)-1)<<17);
113 mtdcr(ebccfgd
, pbcr
);
114 /* printf("pb1cr = %x\n", pbcr); */
119 mtdcr(ebccfga
, pb1cr
);
120 pbcr
= mfdcr(ebccfgd
);
121 mtdcr(ebccfga
, pb1cr
);
122 base_b0
= base_b1
- size_b0
;
123 pbcr
= (pbcr
& 0x0001ffff) | base_b0
| (((size_b0
/1024/1024)-1)<<17);
124 mtdcr(ebccfgd
, pbcr
);
125 /* printf("pb0cr = %x\n", pbcr); */
128 size_b0
= flash_get_size((volatile FLASH_WORD_SIZE
*)base_b0
, &flash_info
[0]);
130 flash_get_offsets (base_b0
, &flash_info
[0]);
132 /* monitor protection ON by default */
134 (void)flash_protect(FLAG_PROTECT_SET
,
135 FLASH_BASE0_PRELIM
-monitor_flash_len
+size_b0
,
136 FLASH_BASE0_PRELIM
-1+size_b0
,
139 (void)flash_protect(FLAG_PROTECT_SET
,
141 CFG_MONITOR_BASE
+monitor_flash_len
-1,
146 /* Re-do sizing to get full correct info */
147 size_b1
= flash_get_size((volatile FLASH_WORD_SIZE
*)base_b1
, &flash_info
[1]);
149 flash_get_offsets (base_b1
, &flash_info
[1]);
151 /* monitor protection ON by default */
152 (void)flash_protect(FLAG_PROTECT_SET
,
153 base_b1
+size_b1
-monitor_flash_len
,
156 /* monitor protection OFF by default (one is enough) */
157 (void)flash_protect(FLAG_PROTECT_CLEAR
,
158 base_b0
+size_b0
-monitor_flash_len
,
162 flash_info
[1].flash_id
= FLASH_UNKNOWN
;
163 flash_info
[1].sector_count
= -1;
166 flash_info
[0].size
= size_b0
;
167 flash_info
[1].size
= size_b1
;
169 return (size_b0
+ size_b1
);
173 /*-----------------------------------------------------------------------
176 static void flash_get_offsets (ulong base
, flash_info_t
*info
)
180 /* set up sector start adress table */
181 if ((info
->flash_id
& FLASH_TYPEMASK
) == FLASH_28F320J3A
||
182 (info
->flash_id
& FLASH_TYPEMASK
) == FLASH_28F640J3A
||
183 (info
->flash_id
& FLASH_TYPEMASK
) == FLASH_28F128J3A
) {
184 for (i
= 0; i
< info
->sector_count
; i
++) {
185 info
->start
[i
] = base
+ (i
* info
->size
/info
->sector_count
);
187 } else if (info
->flash_id
& FLASH_BTYPE
) {
188 if ((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_INTEL
) {
190 #ifndef CFG_FLASH_16BIT
191 /* set sector offsets for bottom boot block type */
192 info
->start
[0] = base
+ 0x00000000;
193 info
->start
[1] = base
+ 0x00004000;
194 info
->start
[2] = base
+ 0x00008000;
195 info
->start
[3] = base
+ 0x0000C000;
196 info
->start
[4] = base
+ 0x00010000;
197 info
->start
[5] = base
+ 0x00014000;
198 info
->start
[6] = base
+ 0x00018000;
199 info
->start
[7] = base
+ 0x0001C000;
200 for (i
= 8; i
< info
->sector_count
; i
++) {
201 info
->start
[i
] = base
+ (i
* 0x00020000) - 0x000E0000;
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;
215 /* set sector offsets for bottom boot block type */
216 info
->start
[0] = base
+ 0x00000000;
217 info
->start
[1] = base
+ 0x00002000;
218 info
->start
[2] = base
+ 0x00004000;
219 info
->start
[3] = base
+ 0x00006000;
220 info
->start
[4] = base
+ 0x00008000;
221 info
->start
[5] = base
+ 0x0000A000;
222 info
->start
[6] = base
+ 0x0000C000;
223 info
->start
[7] = base
+ 0x0000E000;
224 for (i
= 8; i
< info
->sector_count
; i
++) {
225 info
->start
[i
] = base
+ (i
* 0x00010000) - 0x00070000;
229 /* set sector offsets for bottom boot block type */
230 info
->start
[0] = base
+ 0x00000000;
231 info
->start
[1] = base
+ 0x00004000;
232 info
->start
[2] = base
+ 0x00006000;
233 info
->start
[3] = base
+ 0x00008000;
234 for (i
= 4; i
< info
->sector_count
; i
++) {
235 info
->start
[i
] = base
+ (i
* 0x00010000) - 0x00030000;
240 /* set sector offsets for top boot block type */
241 i
= info
->sector_count
- 1;
242 if ((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_INTEL
) {
244 #ifndef CFG_FLASH_16BIT
245 info
->start
[i
--] = base
+ info
->size
- 0x00004000;
246 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
247 info
->start
[i
--] = base
+ info
->size
- 0x0000C000;
248 info
->start
[i
--] = base
+ info
->size
- 0x00010000;
249 info
->start
[i
--] = base
+ info
->size
- 0x00014000;
250 info
->start
[i
--] = base
+ info
->size
- 0x00018000;
251 info
->start
[i
--] = base
+ info
->size
- 0x0001C000;
252 for (; i
>= 0; i
--) {
253 info
->start
[i
] = base
+ i
* 0x00020000;
258 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
259 info
->start
[i
--] = base
+ info
->size
- 0x0000C000;
260 info
->start
[i
--] = base
+ info
->size
- 0x00010000;
261 for (; i
>= 0; i
--) {
262 info
->start
[i
] = base
+ i
* 0x00020000;
266 info
->start
[i
--] = base
+ info
->size
- 0x00002000;
267 info
->start
[i
--] = base
+ info
->size
- 0x00004000;
268 info
->start
[i
--] = base
+ info
->size
- 0x00006000;
269 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
270 info
->start
[i
--] = base
+ info
->size
- 0x0000A000;
271 info
->start
[i
--] = base
+ info
->size
- 0x0000C000;
272 info
->start
[i
--] = base
+ info
->size
- 0x0000E000;
273 for (; i
>= 0; i
--) {
274 info
->start
[i
] = base
+ i
* 0x00010000;
279 info
->start
[i
--] = base
+ info
->size
- 0x00004000;
280 info
->start
[i
--] = base
+ info
->size
- 0x00006000;
281 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
282 for (; i
>= 0; i
--) {
283 info
->start
[i
] = base
+ i
* 0x00010000;
292 /*-----------------------------------------------------------------------
295 void flash_print_info (flash_info_t
*info
)
299 uchar botboot
[]=", bottom boot sect)\n";
300 uchar topboot
[]=", top boot sector)\n";
302 if (info
->flash_id
== FLASH_UNKNOWN
) {
303 printf ("missing or unknown FLASH type\n");
307 switch (info
->flash_id
& FLASH_VENDMASK
) {
308 case FLASH_MAN_AMD
: printf ("AMD "); break;
309 case FLASH_MAN_FUJ
: printf ("FUJITSU "); break;
310 case FLASH_MAN_SST
: printf ("SST "); break;
311 case FLASH_MAN_STM
: printf ("STM "); break;
312 case FLASH_MAN_INTEL
: printf ("INTEL "); break;
313 default: printf ("Unknown Vendor "); break;
316 if (info
->flash_id
& 0x0001 ) {
322 switch (info
->flash_id
& FLASH_TYPEMASK
) {
323 case FLASH_AM400B
: printf ("AM29LV400B (4 Mbit%s",boottype
);
325 case FLASH_AM400T
: printf ("AM29LV400T (4 Mbit%s",boottype
);
327 case FLASH_AM800B
: printf ("AM29LV800B (8 Mbit%s",boottype
);
329 case FLASH_AM800T
: printf ("AM29LV800T (8 Mbit%s",boottype
);
331 case FLASH_AM160B
: printf ("AM29LV160B (16 Mbit%s",boottype
);
333 case FLASH_AM160T
: printf ("AM29LV160T (16 Mbit%s",boottype
);
335 case FLASH_AM320B
: printf ("AM29LV320B (32 Mbit%s",boottype
);
337 case FLASH_AM320T
: printf ("AM29LV320T (32 Mbit%s",boottype
);
339 case FLASH_INTEL800B
: printf ("INTEL28F800B (8 Mbit%s",boottype
);
341 case FLASH_INTEL800T
: printf ("INTEL28F800T (8 Mbit%s",boottype
);
343 case FLASH_INTEL160B
: printf ("INTEL28F160B (16 Mbit%s",boottype
);
345 case FLASH_INTEL160T
: printf ("INTEL28F160T (16 Mbit%s",boottype
);
347 case FLASH_INTEL320B
: printf ("INTEL28F320B (32 Mbit%s",boottype
);
349 case FLASH_INTEL320T
: printf ("INTEL28F320T (32 Mbit%s",boottype
);
352 #if 0 /* enable when devices are available */
354 case FLASH_INTEL640B
: printf ("INTEL28F640B (64 Mbit%s",boottype
);
356 case FLASH_INTEL640T
: printf ("INTEL28F640T (64 Mbit%s",boottype
);
359 case FLASH_28F320J3A
: printf ("INTEL28F320J3A (32 Mbit%s",boottype
);
361 case FLASH_28F640J3A
: printf ("INTEL28F640J3A (64 Mbit%s",boottype
);
363 case FLASH_28F128J3A
: printf ("INTEL28F128J3A (128 Mbit%s",boottype
);
366 default: printf ("Unknown Chip Type\n");
370 printf (" Size: %ld MB in %d Sectors\n",
371 info
->size
>> 20, info
->sector_count
);
373 printf (" Sector Start Addresses:");
374 for (i
=0; i
<info
->sector_count
; ++i
) {
379 info
->protect
[i
] ? " (RO)" : " "
387 /*-----------------------------------------------------------------------
391 /*-----------------------------------------------------------------------
395 * The following code cannot be run from FLASH!
397 ulong
flash_get_size (volatile FLASH_WORD_SIZE
*addr
, flash_info_t
*info
)
400 ulong base
= (ulong
)addr
;
401 FLASH_WORD_SIZE value
;
403 /* Write auto select command: read Manufacturer ID */
406 #ifndef CFG_FLASH_16BIT
409 * Note: if it is an AMD flash and the word at addr[0000]
410 * is 0x00890089 this routine will think it is an Intel
411 * flash device and may(most likely) cause trouble.
414 addr
[0x0000] = 0x00900090;
415 if(addr
[0x0000] != 0x00890089){
416 addr
[0x0555] = 0x00AA00AA;
417 addr
[0x02AA] = 0x00550055;
418 addr
[0x0555] = 0x00900090;
422 * Note: if it is an AMD flash and the word at addr[0000]
423 * is 0x0089 this routine will think it is an Intel
424 * flash device and may(most likely) cause trouble.
427 addr
[0x0000] = 0x0090;
429 if(addr
[0x0000] != 0x0089){
430 addr
[0x0555] = 0x00AA;
431 addr
[0x02AA] = 0x0055;
432 addr
[0x0555] = 0x0090;
438 case (AMD_MANUFACT
& FLASH_ID_MASK
):
439 info
->flash_id
= FLASH_MAN_AMD
;
441 case (FUJ_MANUFACT
& FLASH_ID_MASK
):
442 info
->flash_id
= FLASH_MAN_FUJ
;
444 case (STM_MANUFACT
& FLASH_ID_MASK
):
445 info
->flash_id
= FLASH_MAN_STM
;
447 case (SST_MANUFACT
& FLASH_ID_MASK
):
448 info
->flash_id
= FLASH_MAN_SST
;
450 case (INTEL_MANUFACT
& FLASH_ID_MASK
):
451 info
->flash_id
= FLASH_MAN_INTEL
;
454 info
->flash_id
= FLASH_UNKNOWN
;
455 info
->sector_count
= 0;
457 return (0); /* no or unknown flash */
461 value
= addr
[1]; /* device ID */
465 case (AMD_ID_LV400T
& FLASH_ID_MASK
):
466 info
->flash_id
+= FLASH_AM400T
;
467 info
->sector_count
= 11;
468 info
->size
= 0x00100000;
471 case (AMD_ID_LV400B
& FLASH_ID_MASK
):
472 info
->flash_id
+= FLASH_AM400B
;
473 info
->sector_count
= 11;
474 info
->size
= 0x00100000;
477 case (AMD_ID_LV800T
& FLASH_ID_MASK
):
478 info
->flash_id
+= FLASH_AM800T
;
479 info
->sector_count
= 19;
480 info
->size
= 0x00200000;
483 case (AMD_ID_LV800B
& FLASH_ID_MASK
):
484 info
->flash_id
+= FLASH_AM800B
;
485 info
->sector_count
= 19;
486 info
->size
= 0x00200000;
489 case (AMD_ID_LV160T
& FLASH_ID_MASK
):
490 info
->flash_id
+= FLASH_AM160T
;
491 info
->sector_count
= 35;
492 info
->size
= 0x00400000;
495 case (AMD_ID_LV160B
& FLASH_ID_MASK
):
496 info
->flash_id
+= FLASH_AM160B
;
497 info
->sector_count
= 35;
498 info
->size
= 0x00400000;
500 #if 0 /* enable when device IDs are available */
501 case (AMD_ID_LV320T
& FLASH_ID_MASK
):
502 info
->flash_id
+= FLASH_AM320T
;
503 info
->sector_count
= 67;
504 info
->size
= 0x00800000;
507 case (AMD_ID_LV320B
& FLASH_ID_MASK
):
508 info
->flash_id
+= FLASH_AM320B
;
509 info
->sector_count
= 67;
510 info
->size
= 0x00800000;
514 case (INTEL_ID_28F800B3T
& FLASH_ID_MASK
):
515 info
->flash_id
+= FLASH_INTEL800T
;
516 info
->sector_count
= 23;
517 info
->size
= 0x00200000;
520 case (INTEL_ID_28F800B3B
& FLASH_ID_MASK
):
521 info
->flash_id
+= FLASH_INTEL800B
;
522 info
->sector_count
= 23;
523 info
->size
= 0x00200000;
526 case (INTEL_ID_28F160B3T
& FLASH_ID_MASK
):
527 info
->flash_id
+= FLASH_INTEL160T
;
528 info
->sector_count
= 39;
529 info
->size
= 0x00400000;
532 case (INTEL_ID_28F160B3B
& FLASH_ID_MASK
):
533 info
->flash_id
+= FLASH_INTEL160B
;
534 info
->sector_count
= 39;
535 info
->size
= 0x00400000;
538 case (INTEL_ID_28F320B3T
& FLASH_ID_MASK
):
539 info
->flash_id
+= FLASH_INTEL320T
;
540 info
->sector_count
= 71;
541 info
->size
= 0x00800000;
544 case (INTEL_ID_28F320B3B
& FLASH_ID_MASK
):
545 info
->flash_id
+= FLASH_AM320B
;
546 info
->sector_count
= 71;
547 info
->size
= 0x00800000;
550 #if 0 /* enable when devices are available */
551 case (INTEL_ID_28F320B3T
& FLASH_ID_MASK
):
552 info
->flash_id
+= FLASH_INTEL320T
;
553 info
->sector_count
= 135;
554 info
->size
= 0x01000000;
555 break; /* => 16 MB */
557 case (INTEL_ID_28F320B3B
& FLASH_ID_MASK
):
558 info
->flash_id
+= FLASH_AM320B
;
559 info
->sector_count
= 135;
560 info
->size
= 0x01000000;
561 break; /* => 16 MB */
563 case (INTEL_ID_28F320J3A
& FLASH_ID_MASK
):
564 info
->flash_id
+= FLASH_28F320J3A
;
565 info
->sector_count
= 32;
566 info
->size
= 0x00400000;
567 break; /* => 32 MBit */
568 case (INTEL_ID_28F640J3A
& FLASH_ID_MASK
):
569 info
->flash_id
+= FLASH_28F640J3A
;
570 info
->sector_count
= 64;
571 info
->size
= 0x00800000;
572 break; /* => 64 MBit */
573 case (INTEL_ID_28F128J3A
& FLASH_ID_MASK
):
574 info
->flash_id
+= FLASH_28F128J3A
;
575 info
->sector_count
= 128;
576 info
->size
= 0x01000000;
577 break; /* => 128 MBit */
581 info
->flash_id
= FLASH_UNKNOWN
;
582 return (0); /* => no or unknown flash */
585 flash_get_offsets(base
, info
);
587 /* check for protected sectors */
588 for (i
= 0; i
< info
->sector_count
; i
++) {
589 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
590 /* D0 = 1 if protected */
591 addr
= (volatile FLASH_WORD_SIZE
*)(info
->start
[i
]);
592 info
->protect
[i
] = addr
[2] & 1;
596 * Prevent writes to uninitialized FLASH.
598 if (info
->flash_id
!= FLASH_UNKNOWN
) {
599 addr
= (volatile FLASH_WORD_SIZE
*)info
->start
[0];
600 if( (info
->flash_id
& 0xFF00) == FLASH_MAN_INTEL
){
601 *addr
= (0x00F000F0 & FLASH_ID_MASK
); /* reset bank */
603 *addr
= (0x00FF00FF & FLASH_ID_MASK
); /* reset bank */
611 /*-----------------------------------------------------------------------
614 int flash_erase (flash_info_t
*info
, int s_first
, int s_last
)
617 volatile FLASH_WORD_SIZE
*addr
=(volatile FLASH_WORD_SIZE
*)(info
->start
[0]);
618 int flag
, prot
, sect
, l_sect
, barf
;
619 ulong start
, now
, last
;
622 if ((s_first
< 0) || (s_first
> s_last
)) {
623 if (info
->flash_id
== FLASH_UNKNOWN
) {
624 printf ("- missing\n");
626 printf ("- no sectors to erase\n");
631 if ((info
->flash_id
== FLASH_UNKNOWN
) ||
632 ((info
->flash_id
> FLASH_AMD_COMP
) &&
633 ( (info
->flash_id
& FLASH_VENDMASK
) != FLASH_MAN_INTEL
) ) ){
634 printf ("Can't erase unknown flash type - aborted\n");
639 for (sect
=s_first
; sect
<=s_last
; ++sect
) {
640 if (info
->protect
[sect
]) {
646 printf ("- Warning: %d protected sectors will not be erased!\n",
654 /* Disable interrupts which might cause a timeout here */
655 flag
= disable_interrupts();
656 if(info
->flash_id
< FLASH_AMD_COMP
) {
657 #ifndef CFG_FLASH_16BIT
658 addr
[0x0555] = 0x00AA00AA;
659 addr
[0x02AA] = 0x00550055;
660 addr
[0x0555] = 0x00800080;
661 addr
[0x0555] = 0x00AA00AA;
662 addr
[0x02AA] = 0x00550055;
664 addr
[0x0555] = 0x00AA;
665 addr
[0x02AA] = 0x0055;
666 addr
[0x0555] = 0x0080;
667 addr
[0x0555] = 0x00AA;
668 addr
[0x02AA] = 0x0055;
670 /* Start erase on unprotected sectors */
671 for (sect
= s_first
; sect
<=s_last
; sect
++) {
672 if (info
->protect
[sect
] == 0) { /* not protected */
673 addr
= (volatile FLASH_WORD_SIZE
*)(info
->start
[sect
]);
674 addr
[0] = (0x00300030 & FLASH_ID_MASK
);
679 /* re-enable interrupts if necessary */
683 /* wait at least 80us - let's wait 1 ms */
687 * We wait for the last triggered sector
692 start
= get_timer (0);
694 addr
= (volatile FLASH_WORD_SIZE
*)(info
->start
[l_sect
]);
695 while ((addr
[0] & (0x00800080&FLASH_ID_MASK
)) !=
696 (0x00800080&FLASH_ID_MASK
) )
698 if ((now
= get_timer(start
)) > CFG_FLASH_ERASE_TOUT
) {
699 printf ("Timeout\n");
702 /* show that we're waiting */
703 if ((now
- last
) > 1000) { /* every second */
710 /* reset to read mode */
711 addr
= (volatile FLASH_WORD_SIZE
*)info
->start
[0];
712 addr
[0] = (0x00F000F0 & FLASH_ID_MASK
); /* reset bank */
716 for (sect
= s_first
; sect
<=s_last
; sect
++) {
717 if (info
->protect
[sect
] == 0) { /* not protected */
719 #ifndef CFG_FLASH_16BIT
720 addr
= (vu_long
*)(info
->start
[sect
]);
721 addr
[0] = 0x00200020;
722 addr
[0] = 0x00D000D0;
723 while(!(addr
[0] & 0x00800080)); /* wait for error or finish */
724 if( addr
[0] & 0x003A003A) { /* check for error */
725 barf
= addr
[0] & 0x003A0000;
729 barf
= addr
[0] & 0x0000003A;
733 addr
= (vu_short
*)(info
->start
[sect
]);
736 while(!(addr
[0] & 0x0080)); /* wait for error or finish */
737 if( addr
[0] & 0x003A) /* check for error */
738 barf
= addr
[0] & 0x003A;
741 printf("\nFlash error in sector at %lx\n",(unsigned long)addr
);
742 if(barf
& 0x0002) printf("Block locked, not erased.\n");
743 if((barf
& 0x0030) == 0x0030)
744 printf("Command Sequence error.\n");
745 if((barf
& 0x0030) == 0x0020)
746 printf("Block Erase error.\n");
747 if(barf
& 0x0008) printf("Vpp Low error.\n");
752 addr
= (volatile FLASH_WORD_SIZE
*)info
->start
[0];
753 addr
[0] = (0x00FF00FF & FLASH_ID_MASK
); /* reset bank */
762 /*-----------------------------------------------------------------------
765 /*flash_info_t *addr2info (ulong addr)
770 for (i=0, info=&flash_info[0]; i<CFG_MAX_FLASH_BANKS; ++i, ++info) {
771 if ((addr >= info->start[0]) &&
772 (addr < (info->start[0] + info->size)) ) {
780 /*-----------------------------------------------------------------------
781 * Copy memory to flash.
782 * Make sure all target addresses are within Flash bounds,
783 * and no protected sectors are hit.
787 * 2 - Flash not erased
788 * 4 - target range includes protected sectors
789 * 8 - target address not in Flash memory
792 /*int flash_write (uchar *src, ulong addr, ulong cnt)
795 ulong end = addr + cnt - 1;
796 flash_info_t *info_first = addr2info (addr);
797 flash_info_t *info_last = addr2info (end );
804 if (!info_first || !info_last) {
808 for (info = info_first; info <= info_last; ++info) {
809 ulong b_end = info->start[0] + info->size;*/ /* bank end addr */
810 /* short s_end = info->sector_count - 1;
811 for (i=0; i<info->sector_count; ++i) {
812 ulong e_addr = (i == s_end) ? b_end : info->start[i + 1];
814 if ((end >= info->start[i]) && (addr < e_addr) &&
815 (info->protect[i] != 0) ) {
821 */ /* finally write data to flash */
822 /* for (info = info_first; info <= info_last && cnt>0; ++info) {
825 len = info->start[0] + info->size - addr;
828 if ((i = write_buff(info, src, addr, len)) != 0) {
838 /*-----------------------------------------------------------------------
839 * Copy memory to flash, returns:
842 * 2 - Flash not erased
845 int write_buff (flash_info_t
*info
, uchar
*src
, ulong addr
, ulong cnt
)
847 #ifndef CFG_FLASH_16BIT
856 #ifndef CFG_FLASH_16BIT
859 wp
= (addr
& ~3); /* get lower word aligned address */
862 * handle unaligned start bytes
864 if ((l
= addr
- wp
) != 0) {
866 for (i
=0, cp
=wp
; i
<l
; ++i
, ++cp
) {
867 data
= (data
<< 8) | (*(uchar
*)cp
);
869 for (; i
<4 && cnt
>0; ++i
) {
870 data
= (data
<< 8) | *src
++;
874 for (; cnt
==0 && i
<4; ++i
, ++cp
) {
875 data
= (data
<< 8) | (*(uchar
*)cp
);
878 if ((rc
= write_word(info
, wp
, data
)) != 0) {
885 * handle word aligned part
889 for (i
=0; i
<4; ++i
) {
890 data
= (data
<< 8) | *src
++;
892 if ((rc
= write_word(info
, wp
, data
)) != 0) {
904 * handle unaligned tail bytes
907 for (i
=0, cp
=wp
; i
<4 && cnt
>0; ++i
, ++cp
) {
908 data
= (data
<< 8) | *src
++;
911 for (; i
<4; ++i
, ++cp
) {
912 data
= (data
<< 8) | (*(uchar
*)cp
);
915 return (write_word(info
, wp
, data
));
918 wp
= (addr
& ~1); /* get lower word aligned address */
921 * handle unaligned start byte
925 data
= (data
<< 8) | *src
++;
927 if ((rc
= write_short(info
, wp
, data
)) != 0) {
934 * handle word aligned part
936 /* l = 0; used for debuging */
939 for (i
=0; i
<2; ++i
) {
940 data
= (data
<< 8) | *src
++;
946 } used for debuging */
948 if ((rc
= write_short(info
, wp
, data
)) != 0) {
960 * handle unaligned tail bytes
963 for (i
=0, cp
=wp
; i
<2 && cnt
>0; ++i
, ++cp
) {
964 data
= (data
<< 8) | *src
++;
967 for (; i
<2; ++i
, ++cp
) {
968 data
= (data
<< 8) | (*(uchar
*)cp
);
971 return (write_short(info
, wp
, data
));
977 /*-----------------------------------------------------------------------
978 * Write a word to Flash, returns:
981 * 2 - Flash not erased
983 #ifndef CFG_FLASH_16BIT
984 static int write_word (flash_info_t
* info
, ulong dest
, ulong data
)
986 vu_long
*addr
= (vu_long
*) (info
->start
[0]);
991 /* Check if Flash is (sufficiently) erased */
992 if ((*((vu_long
*) dest
) & data
) != data
) {
996 /* Disable interrupts which might cause a timeout here */
997 flag
= disable_interrupts ();
999 if (info
->flash_id
> FLASH_AMD_COMP
) {
1001 addr
[0x0555] = 0x00AA00AA;
1002 addr
[0x02AA] = 0x00550055;
1003 addr
[0x0555] = 0x00A000A0;
1008 *((vu_long
*) dest
) = data
;
1010 /* re-enable interrupts if necessary */
1012 enable_interrupts ();
1014 /* data polling for D7 */
1015 start
= get_timer (0);
1017 if (info
->flash_id
> FLASH_AMD_COMP
) {
1019 while ((*((vu_long
*) dest
) & 0x00800080) !=
1020 (data
& 0x00800080)) {
1021 if (get_timer (start
) > CFG_FLASH_WRITE_TOUT
) {
1028 while (!(addr
[0] & 0x00800080)) { /* wait for error or finish */
1029 if (get_timer (start
) > CFG_FLASH_WRITE_TOUT
) {
1033 if (addr
[0] & 0x003A003A) { /* check for error */
1034 barf
= addr
[0] & 0x003A0000;
1038 barf
= addr
[0] & 0x0000003A;
1040 printf ("\nFlash write error at address %lx\n",
1041 (unsigned long) dest
);
1043 printf ("Block locked, not erased.\n");
1045 printf ("Programming error.\n");
1047 printf ("Vpp Low error.\n");
1060 static int write_short (flash_info_t
* info
, ulong dest
, ushort data
)
1062 vu_short
*addr
= (vu_short
*) (info
->start
[0]);
1066 /* Check if Flash is (sufficiently) erased */
1067 if ((*((vu_short
*) dest
) & data
) != data
) {
1071 /* Disable interrupts which might cause a timeout here */
1072 flag
= disable_interrupts ();
1074 if (info
->flash_id
< FLASH_AMD_COMP
) {
1076 addr
[0x0555] = 0x00AA;
1077 addr
[0x02AA] = 0x0055;
1078 addr
[0x0555] = 0x00A0;
1084 *((vu_short
*) dest
) = data
;
1086 /* re-enable interrupts if necessary */
1088 enable_interrupts ();
1090 /* data polling for D7 */
1091 start
= get_timer (0);
1093 if (info
->flash_id
< FLASH_AMD_COMP
) {
1095 while ((*((vu_short
*) dest
) & 0x0080) != (data
& 0x0080)) {
1096 if (get_timer (start
) > CFG_FLASH_WRITE_TOUT
) {
1103 while (!(addr
[0] & 0x0080)) { /* wait for error or finish */
1104 if (get_timer (start
) > CFG_FLASH_WRITE_TOUT
)
1108 if (addr
[0] & 0x003A) { /* check for error */
1109 barf
= addr
[0] & 0x003A;
1110 printf ("\nFlash write error at address %lx\n",
1111 (unsigned long) dest
);
1113 printf ("Block locked, not erased.\n");
1115 printf ("Programming error.\n");
1117 printf ("Vpp Low error.\n");
1122 while (!(addr
[0] & 0x0080)) { /* wait for error or finish */
1123 if (get_timer (start
) > CFG_FLASH_WRITE_TOUT
)
1131 /*-----------------------------------------------------------------------*/