]>
git.ipfire.org Git - people/ms/u-boot.git/blob - board/snmc/qs860t/flash.c
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
[CFG_MAX_FLASH_BANKS
]; /* info for FLASH chips */
35 #ifdef CFG_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 CFG_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
<CFG_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 (CFG_MAX_FLASH_BANKS
== 1)
85 flash_get_offsets (FLASH_BASE1_PRELIM
, &flash_info
[0]);
87 /* Monitor protection ON by default */
89 (void)flash_protect(FLAG_PROTECT_SET
,
90 FLASH_BASE1_PRELIM
-CFG_MONITOR_LEN
+size_b0
,
91 FLASH_BASE1_PRELIM
-1+size_b0
,
94 (void)flash_protect(FLAG_PROTECT_SET
,
96 CFG_MONITOR_BASE
+CFG_MONITOR_LEN
-1,
100 flash_info
[0].size
= size_b0
;
105 size_b1
= flash_get_size((volatile FLASH_WORD_SIZE
*)FLASH_BASE1_PRELIM
, &flash_info
[1]);
107 /* Re-do sizing to get full correct info */
110 mtdcr(ebccfga
, pb0cr
);
111 pbcr
= mfdcr(ebccfgd
);
112 mtdcr(ebccfga
, pb0cr
);
114 pbcr
= (pbcr
& 0x0001ffff) | base_b1
| (((size_b1
/1024/1024)-1)<<17);
115 mtdcr(ebccfgd
, pbcr
);
120 mtdcr(ebccfga
, pb1cr
);
121 pbcr
= mfdcr(ebccfgd
);
122 mtdcr(ebccfga
, pb1cr
);
123 base_b0
= base_b1
- size_b0
;
124 pbcr
= (pbcr
& 0x0001ffff) | base_b0
| (((size_b0
/1024/1024)-1)<<17);
125 mtdcr(ebccfgd
, 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_BASE1_PRELIM
-CFG_MONITOR_LEN
+size_b0
,
136 FLASH_BASE1_PRELIM
-1+size_b0
,
139 (void)flash_protect(FLAG_PROTECT_SET
,
141 CFG_MONITOR_BASE
+CFG_MONITOR_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
-CFG_MONITOR_LEN
,
156 /* monitor protection OFF by default (one is enough) */
157 (void)flash_protect(FLAG_PROTECT_CLEAR
,
158 base_b0
+size_b0
-CFG_MONITOR_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
) == INTEL_ID_28F320J3A
||
182 (info
->flash_id
& FLASH_TYPEMASK
) == INTEL_ID_28F640J3A
||
183 (info
->flash_id
& FLASH_TYPEMASK
) == INTEL_ID_28F128J3A
) {
184 for (i
= 0; i
< info
->sector_count
; i
++) {
185 info
->start
[i
] = base
+ (i
* info
->size
/info
->sector_count
);
188 else if (info
->flash_id
& FLASH_BTYPE
) {
189 if ((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_INTEL
) {
191 #ifndef CFG_FLASH_16BIT
192 /* set sector offsets for bottom boot block type */
193 info
->start
[0] = base
+ 0x00000000;
194 info
->start
[1] = base
+ 0x00004000;
195 info
->start
[2] = base
+ 0x00008000;
196 info
->start
[3] = base
+ 0x0000C000;
197 info
->start
[4] = base
+ 0x00010000;
198 info
->start
[5] = base
+ 0x00014000;
199 info
->start
[6] = base
+ 0x00018000;
200 info
->start
[7] = base
+ 0x0001C000;
201 for (i
= 8; i
< info
->sector_count
; i
++) {
202 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;
228 /* set sector offsets for bottom boot block type */
229 info
->start
[0] = base
+ 0x00000000;
230 info
->start
[1] = base
+ 0x00004000;
231 info
->start
[2] = base
+ 0x00006000;
232 info
->start
[3] = base
+ 0x00008000;
233 for (i
= 4; i
< info
->sector_count
; i
++) {
234 info
->start
[i
] = base
+ (i
* 0x00010000) - 0x00030000;
239 /* set sector offsets for top boot block type */
240 i
= info
->sector_count
- 1;
241 if ((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_INTEL
) {
243 #ifndef CFG_FLASH_16BIT
244 info
->start
[i
--] = base
+ info
->size
- 0x00004000;
245 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
246 info
->start
[i
--] = base
+ info
->size
- 0x0000C000;
247 info
->start
[i
--] = base
+ info
->size
- 0x00010000;
248 info
->start
[i
--] = base
+ info
->size
- 0x00014000;
249 info
->start
[i
--] = base
+ info
->size
- 0x00018000;
250 info
->start
[i
--] = base
+ info
->size
- 0x0001C000;
251 for (; i
>= 0; i
--) {
252 info
->start
[i
] = base
+ i
* 0x00020000;
256 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
257 info
->start
[i
--] = base
+ info
->size
- 0x0000C000;
258 info
->start
[i
--] = base
+ info
->size
- 0x00010000;
259 for (; i
>= 0; i
--) {
260 info
->start
[i
] = base
+ i
* 0x00020000;
264 info
->start
[i
--] = base
+ info
->size
- 0x00002000;
265 info
->start
[i
--] = base
+ info
->size
- 0x00004000;
266 info
->start
[i
--] = base
+ info
->size
- 0x00006000;
267 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
268 info
->start
[i
--] = base
+ info
->size
- 0x0000A000;
269 info
->start
[i
--] = base
+ info
->size
- 0x0000C000;
270 info
->start
[i
--] = base
+ info
->size
- 0x0000E000;
271 for (; i
>= 0; i
--) {
272 info
->start
[i
] = base
+ i
* 0x00010000;
275 info
->start
[i
--] = base
+ info
->size
- 0x00004000;
276 info
->start
[i
--] = base
+ info
->size
- 0x00006000;
277 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
278 for (; i
>= 0; i
--) {
279 info
->start
[i
] = base
+ i
* 0x00010000;
286 /*-----------------------------------------------------------------------
289 void flash_print_info (flash_info_t
*info
)
293 uchar botboot
[]=", bottom boot sect)\n";
294 uchar topboot
[]=", top boot sector)\n";
296 if (info
->flash_id
== FLASH_UNKNOWN
) {
297 printf ("missing or unknown FLASH type\n");
301 switch (info
->flash_id
& FLASH_VENDMASK
) {
302 case FLASH_MAN_AMD
: printf ("AMD "); break;
303 case FLASH_MAN_FUJ
: printf ("FUJITSU "); break;
304 case FLASH_MAN_SST
: printf ("SST "); break;
305 case FLASH_MAN_STM
: printf ("STM "); break;
306 case FLASH_MAN_INTEL
: printf ("INTEL "); break;
307 default: printf ("Unknown Vendor "); break;
310 if (info
->flash_id
& 0x0001 ) {
316 switch (info
->flash_id
& FLASH_TYPEMASK
) {
317 case FLASH_AM400B
: printf ("AM29LV400B (4 Mbit%s",boottype
);
319 case FLASH_AM400T
: printf ("AM29LV400T (4 Mbit%s",boottype
);
321 case FLASH_AM800B
: printf ("AM29LV800B (8 Mbit%s",boottype
);
323 case FLASH_AM800T
: printf ("AM29LV800T (8 Mbit%s",boottype
);
325 case FLASH_AM160B
: printf ("AM29LV160B (16 Mbit%s",boottype
);
327 case FLASH_AM160T
: printf ("AM29LV160T (16 Mbit%s",boottype
);
329 case FLASH_AM320B
: printf ("AM29LV320B (32 Mbit%s",boottype
);
331 case FLASH_AM320T
: printf ("AM29LV320T (32 Mbit%s",boottype
);
333 case FLASH_INTEL800B
: printf ("INTEL28F800B (8 Mbit%s",boottype
);
335 case FLASH_INTEL800T
: printf ("INTEL28F800T (8 Mbit%s",boottype
);
337 case FLASH_INTEL160B
: printf ("INTEL28F160B (16 Mbit%s",boottype
);
339 case FLASH_INTEL160T
: printf ("INTEL28F160T (16 Mbit%s",boottype
);
341 case FLASH_INTEL320B
: printf ("INTEL28F320B (32 Mbit%s",boottype
);
343 case FLASH_INTEL320T
: printf ("INTEL28F320T (32 Mbit%s",boottype
);
345 case FLASH_AMDL322T
: printf ("AM29DL322T (32 Mbit%s",boottype
);
348 #if 0 /* enable when devices are available */
350 case FLASH_INTEL640B
: printf ("INTEL28F640B (64 Mbit%s",boottype
);
352 case FLASH_INTEL640T
: printf ("INTEL28F640T (64 Mbit%s",boottype
);
355 case INTEL_ID_28F320J3A
: printf ("INTEL28F320JA3 (32 Mbit%s",boottype
);
357 case INTEL_ID_28F640J3A
: printf ("INTEL28F640JA3 (64 Mbit%s",boottype
);
359 case INTEL_ID_28F128J3A
: printf ("INTEL28F128JA3 (128 Mbit%s",boottype
);
362 default: printf ("Unknown Chip Type\n");
366 printf (" Size: %ld MB in %d Sectors\n",
367 info
->size
>> 20, info
->sector_count
);
369 printf (" Sector Start Addresses:");
370 for (i
=0; i
<info
->sector_count
; ++i
) {
375 info
->protect
[i
] ? " (RO)" : " "
383 /*-----------------------------------------------------------------------
387 /*-----------------------------------------------------------------------
391 * The following code cannot be run from FLASH!
393 ulong
flash_get_size (volatile FLASH_WORD_SIZE
*addr
, flash_info_t
*info
)
396 ulong base
= (ulong
)addr
;
397 FLASH_WORD_SIZE value
;
399 /* Write auto select command: read Manufacturer ID */
402 #ifndef CFG_FLASH_16BIT
405 * Note: if it is an AMD flash and the word at addr[0000]
406 * is 0x00890089 this routine will think it is an Intel
407 * flash device and may(most likely) cause trouble.
410 addr
[0x0000] = 0x00900090;
411 if(addr
[0x0000] != 0x00890089){
412 addr
[0x0555] = 0x00AA00AA;
413 addr
[0x02AA] = 0x00550055;
414 addr
[0x0555] = 0x00900090;
418 * Note: if it is an AMD flash and the word at addr[0000]
419 * is 0x0089 this routine will think it is an Intel
420 * flash device and may(most likely) cause trouble.
423 addr
[0x0000] = 0x0090;
425 if(addr
[0x0000] != 0x0089){
426 addr
[0x0555] = 0x00AA;
427 addr
[0x02AA] = 0x0055;
428 addr
[0x0555] = 0x0090;
434 case (AMD_MANUFACT
& FLASH_ID_MASK
):
435 info
->flash_id
= FLASH_MAN_AMD
;
437 case (FUJ_MANUFACT
& FLASH_ID_MASK
):
438 info
->flash_id
= FLASH_MAN_FUJ
;
440 case (STM_MANUFACT
& FLASH_ID_MASK
):
441 info
->flash_id
= FLASH_MAN_STM
;
443 case (SST_MANUFACT
& FLASH_ID_MASK
):
444 info
->flash_id
= FLASH_MAN_SST
;
446 case (INTEL_MANUFACT
& FLASH_ID_MASK
):
447 info
->flash_id
= FLASH_MAN_INTEL
;
450 info
->flash_id
= FLASH_UNKNOWN
;
451 info
->sector_count
= 0;
453 return (0); /* no or unknown flash */
457 value
= addr
[1]; /* device ID */
461 case (AMD_ID_LV400T
& FLASH_ID_MASK
):
462 info
->flash_id
+= FLASH_AM400T
;
463 info
->sector_count
= 11;
464 info
->size
= 0x00100000;
467 case (AMD_ID_LV400B
& FLASH_ID_MASK
):
468 info
->flash_id
+= FLASH_AM400B
;
469 info
->sector_count
= 11;
470 info
->size
= 0x00100000;
473 case (AMD_ID_LV800T
& FLASH_ID_MASK
):
474 info
->flash_id
+= FLASH_AM800T
;
475 info
->sector_count
= 19;
476 info
->size
= 0x00200000;
479 case (AMD_ID_LV800B
& FLASH_ID_MASK
):
480 info
->flash_id
+= FLASH_AM800B
;
481 info
->sector_count
= 19;
482 info
->size
= 0x00200000;
485 case (AMD_ID_LV160T
& FLASH_ID_MASK
):
486 info
->flash_id
+= FLASH_AM160T
;
487 info
->sector_count
= 35;
488 info
->size
= 0x00400000;
491 case (AMD_ID_LV160B
& FLASH_ID_MASK
):
492 info
->flash_id
+= FLASH_AM160B
;
493 info
->sector_count
= 35;
494 info
->size
= 0x00400000;
496 #if 0 /* enable when device IDs are available */
497 case (AMD_ID_LV320T
& FLASH_ID_MASK
):
498 info
->flash_id
+= FLASH_AM320T
;
499 info
->sector_count
= 67;
500 info
->size
= 0x00800000;
503 case (AMD_ID_LV320B
& FLASH_ID_MASK
):
504 info
->flash_id
+= FLASH_AM320B
;
505 info
->sector_count
= 67;
506 info
->size
= 0x00800000;
510 case (AMD_ID_DL322T
& FLASH_ID_MASK
):
511 info
->flash_id
+= FLASH_AMDL322T
;
512 info
->sector_count
= 71;
513 info
->size
= 0x00800000;
516 case (INTEL_ID_28F800B3T
& FLASH_ID_MASK
):
517 info
->flash_id
+= FLASH_INTEL800T
;
518 info
->sector_count
= 23;
519 info
->size
= 0x00200000;
522 case (INTEL_ID_28F800B3B
& FLASH_ID_MASK
):
523 info
->flash_id
+= FLASH_INTEL800B
;
524 info
->sector_count
= 23;
525 info
->size
= 0x00200000;
528 case (INTEL_ID_28F160B3T
& FLASH_ID_MASK
):
529 info
->flash_id
+= FLASH_INTEL160T
;
530 info
->sector_count
= 39;
531 info
->size
= 0x00400000;
534 case (INTEL_ID_28F160B3B
& FLASH_ID_MASK
):
535 info
->flash_id
+= FLASH_INTEL160B
;
536 info
->sector_count
= 39;
537 info
->size
= 0x00400000;
540 case (INTEL_ID_28F320B3T
& FLASH_ID_MASK
):
541 info
->flash_id
+= FLASH_INTEL320T
;
542 info
->sector_count
= 71;
543 info
->size
= 0x00800000;
546 case (INTEL_ID_28F320B3B
& FLASH_ID_MASK
):
547 info
->flash_id
+= FLASH_AM320B
;
548 info
->sector_count
= 71;
549 info
->size
= 0x00800000;
552 #if 0 /* enable when devices are available */
553 case (INTEL_ID_28F320B3T
& FLASH_ID_MASK
):
554 info
->flash_id
+= FLASH_INTEL320T
;
555 info
->sector_count
= 135;
556 info
->size
= 0x01000000;
557 break; /* => 16 MB */
559 case (INTEL_ID_28F320B3B
& FLASH_ID_MASK
):
560 info
->flash_id
+= FLASH_AM320B
;
561 info
->sector_count
= 135;
562 info
->size
= 0x01000000;
563 break; /* => 16 MB */
565 case (INTEL_ID_28F320J3A
& FLASH_ID_MASK
):
566 info
->flash_id
+= FLASH_28F320J3A
;
567 info
->sector_count
= 32;
568 info
->size
= 0x00400000;
569 break; /* => 32 MBit */
570 case (INTEL_ID_28F640J3A
& FLASH_ID_MASK
):
571 info
->flash_id
+= FLASH_28F640J3A
;
572 info
->sector_count
= 64;
573 info
->size
= 0x00800000;
574 break; /* => 64 MBit */
575 case (INTEL_ID_28F128J3A
& FLASH_ID_MASK
):
576 info
->flash_id
+= FLASH_28F128J3A
;
577 info
->sector_count
= 128;
578 info
->size
= 0x01000000;
579 break; /* => 128 MBit */
583 info
->flash_id
= FLASH_UNKNOWN
;
584 return (0); /* => no or unknown flash */
587 flash_get_offsets(base
, info
);
589 /* check for protected sectors */
590 for (i
= 0; i
< info
->sector_count
; i
++) {
591 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
592 /* D0 = 1 if protected */
593 addr
= (volatile FLASH_WORD_SIZE
*)(info
->start
[i
]);
594 info
->protect
[i
] = addr
[2] & 1;
598 * Prevent writes to uninitialized FLASH.
600 if (info
->flash_id
!= FLASH_UNKNOWN
) {
601 addr
= (volatile FLASH_WORD_SIZE
*)info
->start
[0];
602 if( (info
->flash_id
& 0xFF00) == FLASH_MAN_INTEL
){
603 *addr
= (0x00F000F0 & FLASH_ID_MASK
); /* reset bank */
605 *addr
= (0x00FF00FF & FLASH_ID_MASK
); /* reset bank */
613 /*-----------------------------------------------------------------------
616 int flash_erase (flash_info_t
*info
, int s_first
, int s_last
)
619 volatile FLASH_WORD_SIZE
*addr
=(volatile FLASH_WORD_SIZE
*)(info
->start
[0]);
620 int flag
, prot
, sect
, l_sect
, barf
;
621 ulong start
, now
, last
;
624 if ((s_first
< 0) || (s_first
> s_last
)) {
625 if (info
->flash_id
== FLASH_UNKNOWN
) {
626 printf ("- missing\n");
628 printf ("- no sectors to erase\n");
633 if ((info
->flash_id
== FLASH_UNKNOWN
) ||
634 ((info
->flash_id
> FLASH_AMD_COMP
) &&
635 ( (info
->flash_id
& FLASH_VENDMASK
) != FLASH_MAN_INTEL
) ) ){
636 printf ("Can't erase unknown flash type - aborted\n");
641 for (sect
=s_first
; sect
<=s_last
; ++sect
) {
642 if (info
->protect
[sect
]) {
648 printf ("- Warning: %d protected sectors will not be erased!\n",
656 /* Disable interrupts which might cause a timeout here */
657 flag
= disable_interrupts();
658 if(info
->flash_id
< FLASH_AMD_COMP
) {
659 #ifndef CFG_FLASH_16BIT
660 addr
[0x0555] = 0x00AA00AA;
661 addr
[0x02AA] = 0x00550055;
662 addr
[0x0555] = 0x00800080;
663 addr
[0x0555] = 0x00AA00AA;
664 addr
[0x02AA] = 0x00550055;
666 addr
[0x0555] = 0x00AA;
667 addr
[0x02AA] = 0x0055;
668 addr
[0x0555] = 0x0080;
669 addr
[0x0555] = 0x00AA;
670 addr
[0x02AA] = 0x0055;
672 /* Start erase on unprotected sectors */
673 for (sect
= s_first
; sect
<=s_last
; sect
++) {
674 if (info
->protect
[sect
] == 0) { /* not protected */
675 addr
= (volatile FLASH_WORD_SIZE
*)(info
->start
[sect
]);
676 addr
[0] = (0x00300030 & FLASH_ID_MASK
);
681 /* re-enable interrupts if necessary */
685 /* wait at least 80us - let's wait 1 ms */
689 * We wait for the last triggered sector
694 start
= get_timer (0);
696 addr
= (volatile FLASH_WORD_SIZE
*)(info
->start
[l_sect
]);
697 while ((addr
[0] & (0x00800080&FLASH_ID_MASK
)) !=
698 (0x00800080&FLASH_ID_MASK
) )
700 if ((now
= get_timer(start
)) > CFG_FLASH_ERASE_TOUT
) {
701 printf ("Timeout\n");
704 /* show that we're waiting */
705 if ((now
- last
) > 1000) { /* every second */
712 /* reset to read mode */
713 addr
= (volatile FLASH_WORD_SIZE
*)info
->start
[0];
714 addr
[0] = (0x00F000F0 & FLASH_ID_MASK
); /* reset bank */
718 for (sect
= s_first
; sect
<=s_last
; sect
++) {
719 if (info
->protect
[sect
] == 0) { /* not protected */
721 #ifndef CFG_FLASH_16BIT
722 addr
= (vu_long
*)(info
->start
[sect
]);
723 addr
[0] = 0x00200020;
724 addr
[0] = 0x00D000D0;
725 while(!(addr
[0] & 0x00800080)); /* wait for error or finish */
726 if( addr
[0] & 0x003A003A) { /* check for error */
727 barf
= addr
[0] & 0x003A0000;
731 barf
= addr
[0] & 0x0000003A;
735 addr
= (vu_short
*)(info
->start
[sect
]);
738 while(!(addr
[0] & 0x0080)); /* wait for error or finish */
739 if( addr
[0] & 0x003A) /* check for error */
740 barf
= addr
[0] & 0x003A;
743 printf("\nFlash error in sector at %lx\n",(unsigned long)addr
);
744 if(barf
& 0x0002) printf("Block locked, not erased.\n");
745 if((barf
& 0x0030) == 0x0030)
746 printf("Command Sequence error.\n");
747 if((barf
& 0x0030) == 0x0020)
748 printf("Block Erase error.\n");
749 if(barf
& 0x0008) printf("Vpp Low error.\n");
754 addr
= (volatile FLASH_WORD_SIZE
*)info
->start
[0];
755 addr
[0] = (0x00FF00FF & FLASH_ID_MASK
); /* reset bank */
764 /*-----------------------------------------------------------------------
767 /*flash_info_t *addr2info (ulong addr)
772 for (i=0, info=&flash_info[0]; i<CFG_MAX_FLASH_BANKS; ++i, ++info) {
773 if ((addr >= info->start[0]) &&
774 (addr < (info->start[0] + info->size)) ) {
782 /*-----------------------------------------------------------------------
783 * Copy memory to flash.
784 * Make sure all target addresses are within Flash bounds,
785 * and no protected sectors are hit.
789 * 2 - Flash not erased
790 * 4 - target range includes protected sectors
791 * 8 - target address not in Flash memory
794 /*int flash_write (uchar *src, ulong addr, ulong cnt)
797 ulong end = addr + cnt - 1;
798 flash_info_t *info_first = addr2info (addr);
799 flash_info_t *info_last = addr2info (end );
806 if (!info_first || !info_last) {
810 for (info = info_first; info <= info_last; ++info) {
811 ulong b_end = info->start[0] + info->size;*/ /* bank end addr */
812 /* short s_end = info->sector_count - 1;
813 for (i=0; i<info->sector_count; ++i) {
814 ulong e_addr = (i == s_end) ? b_end : info->start[i + 1];
816 if ((end >= info->start[i]) && (addr < e_addr) &&
817 (info->protect[i] != 0) ) {
823 */ /* finally write data to flash */
824 /* for (info = info_first; info <= info_last && cnt>0; ++info) {
827 len = info->start[0] + info->size - addr;
830 if ((i = write_buff(info, src, addr, len)) != 0) {
840 /*-----------------------------------------------------------------------
841 * Copy memory to flash, returns:
844 * 2 - Flash not erased
847 int write_buff (flash_info_t
*info
, uchar
*src
, ulong addr
, ulong cnt
)
849 #ifndef CFG_FLASH_16BIT
858 #ifndef CFG_FLASH_16BIT
861 wp
= (addr
& ~3); /* get lower word aligned address */
864 * handle unaligned start bytes
866 if ((l
= addr
- wp
) != 0) {
868 for (i
=0, cp
=wp
; i
<l
; ++i
, ++cp
) {
869 data
= (data
<< 8) | (*(uchar
*)cp
);
871 for (; i
<4 && cnt
>0; ++i
) {
872 data
= (data
<< 8) | *src
++;
876 for (; cnt
==0 && i
<4; ++i
, ++cp
) {
877 data
= (data
<< 8) | (*(uchar
*)cp
);
880 if ((rc
= write_word(info
, wp
, data
)) != 0) {
887 * handle word aligned part
891 for (i
=0; i
<4; ++i
) {
892 data
= (data
<< 8) | *src
++;
894 if ((rc
= write_word(info
, wp
, data
)) != 0) {
906 * handle unaligned tail bytes
909 for (i
=0, cp
=wp
; i
<4 && cnt
>0; ++i
, ++cp
) {
910 data
= (data
<< 8) | *src
++;
913 for (; i
<4; ++i
, ++cp
) {
914 data
= (data
<< 8) | (*(uchar
*)cp
);
917 return (write_word(info
, wp
, data
));
920 wp
= (addr
& ~1); /* get lower word aligned address */
923 * handle unaligned start byte
927 data
= (data
<< 8) | *src
++;
929 if ((rc
= write_short(info
, wp
, data
)) != 0) {
936 * handle word aligned part
938 /* l = 0; used for debuging */
941 for (i
=0; i
<2; ++i
) {
942 data
= (data
<< 8) | *src
++;
948 } used for debuging */
950 if ((rc
= write_short(info
, wp
, data
)) != 0) {
962 * handle unaligned tail bytes
965 for (i
=0, cp
=wp
; i
<2 && cnt
>0; ++i
, ++cp
) {
966 data
= (data
<< 8) | *src
++;
969 for (; i
<2; ++i
, ++cp
) {
970 data
= (data
<< 8) | (*(uchar
*)cp
);
973 return (write_short(info
, wp
, data
));
979 /*-----------------------------------------------------------------------
980 * Write a word to Flash, returns:
983 * 2 - Flash not erased
985 #ifndef CFG_FLASH_16BIT
986 static int write_word (flash_info_t
*info
, ulong dest
, ulong data
)
988 vu_long
*addr
= (vu_long
*)(info
->start
[0]);
993 /* Check if Flash is (sufficiently) erased */
994 if ((*((vu_long
*)dest
) & data
) != data
) {
998 /* Disable interrupts which might cause a timeout here */
999 flag
= disable_interrupts();
1001 if(info
->flash_id
> FLASH_AMD_COMP
) {
1003 addr
[0x0555] = 0x00AA00AA;
1004 addr
[0x02AA] = 0x00550055;
1005 addr
[0x0555] = 0x00A000A0;
1010 *((vu_long
*)dest
) = data
;
1012 /* re-enable interrupts if necessary */
1014 enable_interrupts();
1016 /* data polling for D7 */
1017 start
= get_timer (0);
1019 if(info
->flash_id
> FLASH_AMD_COMP
) {
1020 while ((*((vu_long
*)dest
) & 0x00800080) != (data
& 0x00800080)) {
1021 if (get_timer(start
) > CFG_FLASH_WRITE_TOUT
) {
1026 while(!(addr
[0] & 0x00800080)) { /* wait for error or finish */
1027 if (get_timer(start
) > CFG_FLASH_WRITE_TOUT
) {
1031 if( addr
[0] & 0x003A003A) { /* check for error */
1032 barf
= addr
[0] & 0x003A0000;
1036 barf
= addr
[0] & 0x0000003A;
1038 printf("\nFlash write error at address %lx\n",(unsigned long)dest
);
1039 if(barf
& 0x0002) printf("Block locked, not erased.\n");
1040 if(barf
& 0x0010) printf("Programming error.\n");
1041 if(barf
& 0x0008) printf("Vpp Low error.\n");
1051 static int write_short (flash_info_t
*info
, ulong dest
, ushort data
)
1053 vu_short
*addr
= (vu_short
*)(info
->start
[0]);
1057 /* Check if Flash is (sufficiently) erased */
1058 if ((*((vu_short
*)dest
) & data
) != data
) {
1062 /* Disable interrupts which might cause a timeout here */
1063 flag
= disable_interrupts();
1065 if(info
->flash_id
< FLASH_AMD_COMP
) {
1067 addr
[0x0555] = 0x00AA;
1068 addr
[0x02AA] = 0x0055;
1069 addr
[0x0555] = 0x00A0;
1075 *((vu_short
*)dest
) = data
;
1077 /* re-enable interrupts if necessary */
1079 enable_interrupts();
1081 /* data polling for D7 */
1082 start
= get_timer (0);
1084 if(info
->flash_id
< FLASH_AMD_COMP
) {
1086 while ((*((vu_short
*)dest
) & 0x0080) != (data
& 0x0080)) {
1087 if (get_timer(start
) > CFG_FLASH_WRITE_TOUT
) {
1094 while(!(addr
[0] & 0x0080)){ /* wait for error or finish */
1095 if (get_timer(start
) > CFG_FLASH_WRITE_TOUT
) return (1);
1098 if( addr
[0] & 0x003A) { /* check for error */
1099 barf
= addr
[0] & 0x003A;
1100 printf("\nFlash write error at address %lx\n",(unsigned long)dest
);
1101 if(barf
& 0x0002) printf("Block locked, not erased.\n");
1102 if(barf
& 0x0010) printf("Programming error.\n");
1103 if(barf
& 0x0008) printf("Vpp Low error.\n");
1108 while(!(addr
[0] & 0x0080)){ /* wait for error or finish */
1109 if (get_timer(start
) > CFG_FLASH_WRITE_TOUT
) return (1);
1119 /*-----------------------------------------------------------------------