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 #ifdef CONFIG_FLASH_16BIT
30 #define FLASH_WORD_SIZE unsigned short
31 #define FLASH_ID_MASK 0xFFFF
33 #define FLASH_WORD_SIZE unsigned long
34 #define FLASH_ID_MASK 0xFFFFFFFF
37 /*-----------------------------------------------------------------------
41 ulong
flash_get_size (volatile FLASH_WORD_SIZE
* addr
, flash_info_t
* info
);
43 #ifndef CONFIG_FLASH_16BIT
44 static int write_word (flash_info_t
* info
, ulong dest
, ulong data
);
46 static int write_short (flash_info_t
* info
, ulong dest
, ushort data
);
48 /*int flash_write (uchar *, ulong, ulong); */
49 /*flash_info_t *addr2info (ulong); */
51 static void flash_get_offsets (ulong base
, flash_info_t
* info
);
53 /*-----------------------------------------------------------------------
55 unsigned long flash_init (void)
57 volatile immap_t
*immap
= (immap_t
*) CONFIG_SYS_IMMR
;
58 volatile memctl8xx_t
*memctl
= &immap
->im_memctl
;
59 unsigned long size_b0
, size_b1
;
62 /* Init: no FLASHes known */
63 for (i
= 0; i
< CONFIG_SYS_MAX_FLASH_BANKS
; ++i
) {
64 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
67 /* Static FLASH Bank configuration here - FIXME XXX */
70 flash_get_size ((volatile FLASH_WORD_SIZE
*)
71 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", size_b0
, size_b0
<< 20);
77 flash_get_size ((volatile FLASH_WORD_SIZE
*)
78 FLASH_BASE1_PRELIM
, &flash_info
[1]);
80 if (size_b1
> size_b0
) {
82 "Bank 1 (0x%08lx = %ld MB) > Bank 0 (0x%08lx = %ld MB)\n",
83 size_b1
, size_b1
<< 20, size_b0
, size_b0
<< 20);
84 flash_info
[0].flash_id
= FLASH_UNKNOWN
;
85 flash_info
[1].flash_id
= FLASH_UNKNOWN
;
86 flash_info
[0].sector_count
= -1;
87 flash_info
[1].sector_count
= -1;
88 flash_info
[0].size
= 0;
89 flash_info
[1].size
= 0;
93 /* Remap FLASH according to real size */
94 memctl
->memc_or0
= CONFIG_SYS_OR_TIMING_FLASH
| (-size_b0
& 0xFFFF8000);
95 memctl
->memc_br0
= CONFIG_SYS_FLASH_BASE
| 0x00000801; /* (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | BR_MS_GPCM | BR_V; */
97 /* Re-do sizing to get full correct info */
99 size_b0
= flash_get_size ((volatile FLASH_WORD_SIZE
*) CONFIG_SYS_FLASH_BASE
,
101 flash_get_offsets (CONFIG_SYS_FLASH_BASE
, &flash_info
[0]);
103 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
104 /* monitor protection ON by default */
105 (void) flash_protect (FLAG_PROTECT_SET
,
106 CONFIG_SYS_MONITOR_BASE
,
107 CONFIG_SYS_MONITOR_BASE
+ monitor_flash_len
- 1,
113 CONFIG_SYS_OR_TIMING_FLASH
| (-size_b1
& 0xFFFF8000);
115 (CONFIG_SYS_FLASH_BASE
| 0x00000801) + (size_b0
& BR_BA_MSK
);
116 /*((CONFIG_SYS_FLASH_BASE + size_b0) & BR_BA_MSK) |
117 BR_MS_GPCM | BR_V; */
119 /* Re-do sizing to get full correct info */
121 flash_get_size ((volatile FLASH_WORD_SIZE
122 *) (CONFIG_SYS_FLASH_BASE
+ size_b0
),
125 flash_get_offsets (CONFIG_SYS_FLASH_BASE
+ size_b0
, &flash_info
[1]);
127 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
128 /* monitor protection ON by default */
129 (void) flash_protect (FLAG_PROTECT_SET
,
130 CONFIG_SYS_MONITOR_BASE
,
131 CONFIG_SYS_MONITOR_BASE
+ monitor_flash_len
-
135 memctl
->memc_br1
= 0; /* invalidate bank */
137 flash_info
[1].flash_id
= FLASH_UNKNOWN
;
138 flash_info
[1].sector_count
= -1;
141 flash_info
[0].size
= size_b0
;
142 flash_info
[1].size
= size_b1
;
144 return (size_b0
+ size_b1
);
147 /*-----------------------------------------------------------------------
149 static void flash_get_offsets (ulong base
, flash_info_t
* info
)
153 /* set up sector start adress table */
154 if (info
->flash_id
& FLASH_BTYPE
) {
155 if ((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_INTEL
) {
157 #ifndef CONFIG_FLASH_16BIT
158 /* set sector offsets for bottom boot block type */
159 info
->start
[0] = base
+ 0x00000000;
160 info
->start
[1] = base
+ 0x00004000;
161 info
->start
[2] = base
+ 0x00008000;
162 info
->start
[3] = base
+ 0x0000C000;
163 info
->start
[4] = base
+ 0x00010000;
164 info
->start
[5] = base
+ 0x00014000;
165 info
->start
[6] = base
+ 0x00018000;
166 info
->start
[7] = base
+ 0x0001C000;
167 for (i
= 8; i
< info
->sector_count
; i
++) {
169 base
+ (i
* 0x00020000) - 0x000E0000;
172 /* set sector offsets for bottom boot block type */
173 info
->start
[0] = base
+ 0x00000000;
174 info
->start
[1] = base
+ 0x00008000;
175 info
->start
[2] = base
+ 0x0000C000;
176 info
->start
[3] = base
+ 0x00010000;
177 for (i
= 4; i
< info
->sector_count
; i
++) {
179 base
+ (i
* 0x00020000) - 0x00060000;
183 /* set sector offsets for bottom boot block type */
184 info
->start
[0] = base
+ 0x00000000;
185 info
->start
[1] = base
+ 0x00002000;
186 info
->start
[2] = base
+ 0x00004000;
187 info
->start
[3] = base
+ 0x00006000;
188 info
->start
[4] = base
+ 0x00008000;
189 info
->start
[5] = base
+ 0x0000A000;
190 info
->start
[6] = base
+ 0x0000C000;
191 info
->start
[7] = base
+ 0x0000E000;
192 for (i
= 8; i
< info
->sector_count
; i
++) {
194 base
+ (i
* 0x00010000) - 0x00070000;
197 /* set sector offsets for bottom boot block type */
198 info
->start
[0] = base
+ 0x00000000;
199 info
->start
[1] = base
+ 0x00004000;
200 info
->start
[2] = base
+ 0x00006000;
201 info
->start
[3] = base
+ 0x00008000;
202 for (i
= 4; i
< info
->sector_count
; i
++) {
204 base
+ (i
* 0x00010000) - 0x00030000;
209 /* set sector offsets for top boot block type */
210 i
= info
->sector_count
- 1;
211 if ((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_INTEL
) {
213 #ifndef CONFIG_FLASH_16BIT
214 info
->start
[i
--] = base
+ info
->size
- 0x00004000;
215 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
216 info
->start
[i
--] = base
+ info
->size
- 0x0000C000;
217 info
->start
[i
--] = base
+ info
->size
- 0x00010000;
218 info
->start
[i
--] = base
+ info
->size
- 0x00014000;
219 info
->start
[i
--] = base
+ info
->size
- 0x00018000;
220 info
->start
[i
--] = base
+ info
->size
- 0x0001C000;
221 for (; i
>= 0; i
--) {
222 info
->start
[i
] = base
+ i
* 0x00020000;
227 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
228 info
->start
[i
--] = base
+ info
->size
- 0x0000C000;
229 info
->start
[i
--] = base
+ info
->size
- 0x00010000;
230 for (; i
>= 0; i
--) {
231 info
->start
[i
] = base
+ i
* 0x00020000;
235 info
->start
[i
--] = base
+ info
->size
- 0x00002000;
236 info
->start
[i
--] = base
+ info
->size
- 0x00004000;
237 info
->start
[i
--] = base
+ info
->size
- 0x00006000;
238 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
239 info
->start
[i
--] = base
+ info
->size
- 0x0000A000;
240 info
->start
[i
--] = base
+ info
->size
- 0x0000C000;
241 info
->start
[i
--] = base
+ info
->size
- 0x0000E000;
242 for (; i
>= 0; i
--) {
243 info
->start
[i
] = base
+ i
* 0x00010000;
248 info
->start
[i
--] = base
+ info
->size
- 0x00004000;
249 info
->start
[i
--] = base
+ info
->size
- 0x00006000;
250 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
251 for (; i
>= 0; i
--) {
252 info
->start
[i
] = base
+ i
* 0x00010000;
261 /*-----------------------------------------------------------------------
263 void flash_print_info (flash_info_t
* info
)
267 uchar botboot
[] = ", bottom boot sect)\n";
268 uchar topboot
[] = ", top boot sector)\n";
270 if (info
->flash_id
== FLASH_UNKNOWN
) {
271 printf ("missing or unknown FLASH type\n");
275 switch (info
->flash_id
& FLASH_VENDMASK
) {
288 case FLASH_MAN_INTEL
:
292 printf ("Unknown Vendor ");
296 if (info
->flash_id
& 0x0001) {
302 switch (info
->flash_id
& FLASH_TYPEMASK
) {
304 printf ("AM29LV400B (4 Mbit%s", boottype
);
307 printf ("AM29LV400T (4 Mbit%s", boottype
);
310 printf ("AM29LV800B (8 Mbit%s", boottype
);
313 printf ("AM29LV800T (8 Mbit%s", boottype
);
316 printf ("AM29LV160B (16 Mbit%s", boottype
);
319 printf ("AM29LV160T (16 Mbit%s", boottype
);
322 printf ("AM29LV320B (32 Mbit%s", boottype
);
325 printf ("AM29LV320T (32 Mbit%s", boottype
);
327 case FLASH_INTEL800B
:
328 printf ("INTEL28F800B (8 Mbit%s", boottype
);
330 case FLASH_INTEL800T
:
331 printf ("INTEL28F800T (8 Mbit%s", boottype
);
333 case FLASH_INTEL160B
:
334 printf ("INTEL28F160B (16 Mbit%s", boottype
);
336 case FLASH_INTEL160T
:
337 printf ("INTEL28F160T (16 Mbit%s", boottype
);
339 case FLASH_INTEL320B
:
340 printf ("INTEL28F320B (32 Mbit%s", boottype
);
342 case FLASH_INTEL320T
:
343 printf ("INTEL28F320T (32 Mbit%s", boottype
);
346 #if 0 /* enable when devices are available */
348 case FLASH_INTEL640B
:
349 printf ("INTEL28F640B (64 Mbit%s", boottype
);
351 case FLASH_INTEL640T
:
352 printf ("INTEL28F640T (64 Mbit%s", boottype
);
357 printf ("Unknown Chip Type\n");
361 printf (" Size: %ld MB in %d Sectors\n",
362 info
->size
>> 20, info
->sector_count
);
364 printf (" Sector Start Addresses:");
365 for (i
= 0; i
< info
->sector_count
; ++i
) {
369 info
->start
[i
], info
->protect
[i
] ? " (RO)" : " ");
375 /*-----------------------------------------------------------------------
379 /*-----------------------------------------------------------------------
383 * The following code cannot be run from FLASH!
385 ulong
flash_get_size (volatile FLASH_WORD_SIZE
* addr
, flash_info_t
* info
)
388 ulong base
= (ulong
) addr
;
389 FLASH_WORD_SIZE value
;
391 /* Write auto select command: read Manufacturer ID */
394 #ifndef CONFIG_FLASH_16BIT
397 * Note: if it is an AMD flash and the word at addr[0000]
398 * is 0x00890089 this routine will think it is an Intel
399 * flash device and may(most likely) cause trouble.
402 addr
[0x0000] = 0x00900090;
403 if (addr
[0x0000] != 0x00890089) {
404 addr
[0x0555] = 0x00AA00AA;
405 addr
[0x02AA] = 0x00550055;
406 addr
[0x0555] = 0x00900090;
410 * Note: if it is an AMD flash and the word at addr[0000]
411 * is 0x0089 this routine will think it is an Intel
412 * flash device and may(most likely) cause trouble.
415 addr
[0x0000] = 0x0090;
417 if (addr
[0x0000] != 0x0089) {
418 addr
[0x0555] = 0x00AA;
419 addr
[0x02AA] = 0x0055;
420 addr
[0x0555] = 0x0090;
426 case (AMD_MANUFACT
& FLASH_ID_MASK
):
427 info
->flash_id
= FLASH_MAN_AMD
;
429 case (FUJ_MANUFACT
& FLASH_ID_MASK
):
430 info
->flash_id
= FLASH_MAN_FUJ
;
432 case (STM_MANUFACT
& FLASH_ID_MASK
):
433 info
->flash_id
= FLASH_MAN_STM
;
435 case (SST_MANUFACT
& FLASH_ID_MASK
):
436 info
->flash_id
= FLASH_MAN_SST
;
438 case (INTEL_MANUFACT
& FLASH_ID_MASK
):
439 info
->flash_id
= FLASH_MAN_INTEL
;
442 info
->flash_id
= FLASH_UNKNOWN
;
443 info
->sector_count
= 0;
445 return (0); /* no or unknown flash */
449 value
= addr
[1]; /* device ID */
453 case (AMD_ID_LV400T
& FLASH_ID_MASK
):
454 info
->flash_id
+= FLASH_AM400T
;
455 info
->sector_count
= 11;
456 info
->size
= 0x00100000;
459 case (AMD_ID_LV400B
& FLASH_ID_MASK
):
460 info
->flash_id
+= FLASH_AM400B
;
461 info
->sector_count
= 11;
462 info
->size
= 0x00100000;
465 case (AMD_ID_LV800T
& FLASH_ID_MASK
):
466 info
->flash_id
+= FLASH_AM800T
;
467 info
->sector_count
= 19;
468 info
->size
= 0x00200000;
471 case (AMD_ID_LV800B
& FLASH_ID_MASK
):
472 info
->flash_id
+= FLASH_AM800B
;
473 info
->sector_count
= 19;
474 info
->size
= 0x00200000;
477 case (AMD_ID_LV160T
& FLASH_ID_MASK
):
478 info
->flash_id
+= FLASH_AM160T
;
479 info
->sector_count
= 35;
480 info
->size
= 0x00400000;
483 case (AMD_ID_LV160B
& FLASH_ID_MASK
):
484 info
->flash_id
+= FLASH_AM160B
;
485 info
->sector_count
= 35;
486 info
->size
= 0x00400000;
488 #if 0 /* enable when device IDs are available */
489 case (AMD_ID_LV320T
& FLASH_ID_MASK
):
490 info
->flash_id
+= FLASH_AM320T
;
491 info
->sector_count
= 67;
492 info
->size
= 0x00800000;
495 case (AMD_ID_LV320B
& FLASH_ID_MASK
):
496 info
->flash_id
+= FLASH_AM320B
;
497 info
->sector_count
= 67;
498 info
->size
= 0x00800000;
502 case (INTEL_ID_28F800B3T
& FLASH_ID_MASK
):
503 info
->flash_id
+= FLASH_INTEL800T
;
504 info
->sector_count
= 23;
505 info
->size
= 0x00200000;
508 case (INTEL_ID_28F800B3B
& FLASH_ID_MASK
):
509 info
->flash_id
+= FLASH_INTEL800B
;
510 info
->sector_count
= 23;
511 info
->size
= 0x00200000;
514 case (INTEL_ID_28F160B3T
& FLASH_ID_MASK
):
515 info
->flash_id
+= FLASH_INTEL160T
;
516 info
->sector_count
= 39;
517 info
->size
= 0x00400000;
520 case (INTEL_ID_28F160B3B
& FLASH_ID_MASK
):
521 info
->flash_id
+= FLASH_INTEL160B
;
522 info
->sector_count
= 39;
523 info
->size
= 0x00400000;
526 case (INTEL_ID_28F320B3T
& FLASH_ID_MASK
):
527 info
->flash_id
+= FLASH_INTEL320T
;
528 info
->sector_count
= 71;
529 info
->size
= 0x00800000;
532 case (INTEL_ID_28F320B3B
& FLASH_ID_MASK
):
533 info
->flash_id
+= FLASH_AM320B
;
534 info
->sector_count
= 71;
535 info
->size
= 0x00800000;
538 #if 0 /* enable when devices are available */
539 case (INTEL_ID_28F320B3T
& FLASH_ID_MASK
):
540 info
->flash_id
+= FLASH_INTEL320T
;
541 info
->sector_count
= 135;
542 info
->size
= 0x01000000;
543 break; /* => 16 MB */
545 case (INTEL_ID_28F320B3B
& FLASH_ID_MASK
):
546 info
->flash_id
+= FLASH_AM320B
;
547 info
->sector_count
= 135;
548 info
->size
= 0x01000000;
549 break; /* => 16 MB */
552 info
->flash_id
= FLASH_UNKNOWN
;
553 return (0); /* => no or unknown flash */
557 /* set up sector start adress table */
558 if (info
->flash_id
& FLASH_BTYPE
) {
559 if ((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_INTEL
) {
561 #ifndef CONFIG_FLASH_16BIT
562 /* set sector offsets for bottom boot block type */
563 info
->start
[0] = base
+ 0x00000000;
564 info
->start
[1] = base
+ 0x00004000;
565 info
->start
[2] = base
+ 0x00008000;
566 info
->start
[3] = base
+ 0x0000C000;
567 info
->start
[4] = base
+ 0x00010000;
568 info
->start
[5] = base
+ 0x00014000;
569 info
->start
[6] = base
+ 0x00018000;
570 info
->start
[7] = base
+ 0x0001C000;
571 for (i
= 8; i
< info
->sector_count
; i
++) {
573 base
+ (i
* 0x00020000) - 0x000E0000;
576 /* set sector offsets for bottom boot block type */
577 info
->start
[0] = base
+ 0x00000000;
578 info
->start
[1] = base
+ 0x00008000;
579 info
->start
[2] = base
+ 0x0000C000;
580 info
->start
[3] = base
+ 0x00010000;
581 for (i
= 4; i
< info
->sector_count
; i
++) {
583 base
+ (i
* 0x00020000) - 0x00060000;
587 /* set sector offsets for bottom boot block type */
588 info
->start
[0] = base
+ 0x00000000;
589 info
->start
[1] = base
+ 0x00002000;
590 info
->start
[2] = base
+ 0x00004000;
591 info
->start
[3] = base
+ 0x00006000;
592 info
->start
[4] = base
+ 0x00008000;
593 info
->start
[5] = base
+ 0x0000A000;
594 info
->start
[6] = base
+ 0x0000C000;
595 info
->start
[7] = base
+ 0x0000E000;
596 for (i
= 8; i
< info
->sector_count
; i
++) {
598 base
+ (i
* 0x00010000) - 0x00070000;
601 /* set sector offsets for bottom boot block type */
602 info
->start
[0] = base
+ 0x00000000;
603 info
->start
[1] = base
+ 0x00004000;
604 info
->start
[2] = base
+ 0x00006000;
605 info
->start
[3] = base
+ 0x00008000;
606 for (i
= 4; i
< info
->sector_count
; i
++) {
608 base
+ (i
* 0x00010000) - 0x00030000;
613 /* set sector offsets for top boot block type */
614 i
= info
->sector_count
- 1;
615 if ((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_INTEL
) {
617 #ifndef CONFIG_FLASH_16BIT
618 info
->start
[i
--] = base
+ info
->size
- 0x00004000;
619 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
620 info
->start
[i
--] = base
+ info
->size
- 0x0000C000;
621 info
->start
[i
--] = base
+ info
->size
- 0x00010000;
622 info
->start
[i
--] = base
+ info
->size
- 0x00014000;
623 info
->start
[i
--] = base
+ info
->size
- 0x00018000;
624 info
->start
[i
--] = base
+ info
->size
- 0x0001C000;
625 for (; i
>= 0; i
--) {
626 info
->start
[i
] = base
+ i
* 0x00020000;
631 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
632 info
->start
[i
--] = base
+ info
->size
- 0x0000C000;
633 info
->start
[i
--] = base
+ info
->size
- 0x00010000;
634 for (; i
>= 0; i
--) {
635 info
->start
[i
] = base
+ i
* 0x00020000;
639 info
->start
[i
--] = base
+ info
->size
- 0x00002000;
640 info
->start
[i
--] = base
+ info
->size
- 0x00004000;
641 info
->start
[i
--] = base
+ info
->size
- 0x00006000;
642 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
643 info
->start
[i
--] = base
+ info
->size
- 0x0000A000;
644 info
->start
[i
--] = base
+ info
->size
- 0x0000C000;
645 info
->start
[i
--] = base
+ info
->size
- 0x0000E000;
646 for (; i
>= 0; i
--) {
647 info
->start
[i
] = base
+ i
* 0x00010000;
652 info
->start
[i
--] = base
+ info
->size
- 0x00004000;
653 info
->start
[i
--] = base
+ info
->size
- 0x00006000;
654 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
655 for (; i
>= 0; i
--) {
656 info
->start
[i
] = base
+ i
* 0x00010000;
662 /* check for protected sectors */
663 for (i
= 0; i
< info
->sector_count
; i
++) {
664 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
665 /* D0 = 1 if protected */
666 addr
= (volatile FLASH_WORD_SIZE
*) (info
->start
[i
]);
667 info
->protect
[i
] = addr
[2] & 1;
671 * Prevent writes to uninitialized FLASH.
673 if (info
->flash_id
!= FLASH_UNKNOWN
) {
674 addr
= (volatile FLASH_WORD_SIZE
*) info
->start
[0];
675 if ((info
->flash_id
& 0xFF00) == FLASH_MAN_INTEL
) {
676 *addr
= (0x00F000F0 & FLASH_ID_MASK
); /* reset bank */
678 *addr
= (0x00FF00FF & FLASH_ID_MASK
); /* reset bank */
686 /*-----------------------------------------------------------------------
689 int flash_erase (flash_info_t
* info
, int s_first
, int s_last
)
692 volatile FLASH_WORD_SIZE
*addr
=
693 (volatile FLASH_WORD_SIZE
*) (info
->start
[0]);
694 int flag
, prot
, sect
, l_sect
, barf
;
695 ulong start
, now
, last
;
698 if ((s_first
< 0) || (s_first
> s_last
)) {
699 if (info
->flash_id
== FLASH_UNKNOWN
) {
700 printf ("- missing\n");
702 printf ("- no sectors to erase\n");
707 if ((info
->flash_id
== FLASH_UNKNOWN
) ||
708 ((info
->flash_id
> FLASH_AMD_COMP
) &&
709 ((info
->flash_id
& FLASH_VENDMASK
) != FLASH_MAN_INTEL
))) {
710 printf ("Can't erase unknown flash type - aborted\n");
715 for (sect
= s_first
; sect
<= s_last
; ++sect
) {
716 if (info
->protect
[sect
]) {
722 printf ("- Warning: %d protected sectors will not be erased!\n", prot
);
729 /* Disable interrupts which might cause a timeout here */
730 flag
= disable_interrupts ();
731 if (info
->flash_id
< FLASH_AMD_COMP
) {
732 #ifndef CONFIG_FLASH_16BIT
733 addr
[0x0555] = 0x00AA00AA;
734 addr
[0x02AA] = 0x00550055;
735 addr
[0x0555] = 0x00800080;
736 addr
[0x0555] = 0x00AA00AA;
737 addr
[0x02AA] = 0x00550055;
739 addr
[0x0555] = 0x00AA;
740 addr
[0x02AA] = 0x0055;
741 addr
[0x0555] = 0x0080;
742 addr
[0x0555] = 0x00AA;
743 addr
[0x02AA] = 0x0055;
745 /* Start erase on unprotected sectors */
746 for (sect
= s_first
; sect
<= s_last
; sect
++) {
747 if (info
->protect
[sect
] == 0) { /* not protected */
748 addr
= (volatile FLASH_WORD_SIZE
*) (info
->start
[sect
]);
749 addr
[0] = (0x00300030 & FLASH_ID_MASK
);
754 /* re-enable interrupts if necessary */
756 enable_interrupts ();
758 /* wait at least 80us - let's wait 1 ms */
762 * We wait for the last triggered sector
767 start
= get_timer (0);
769 addr
= (volatile FLASH_WORD_SIZE
*) (info
->start
[l_sect
]);
770 while ((addr
[0] & (0x00800080 & FLASH_ID_MASK
)) !=
771 (0x00800080 & FLASH_ID_MASK
)) {
772 if ((now
= get_timer (start
)) > CONFIG_SYS_FLASH_ERASE_TOUT
) {
773 printf ("Timeout\n");
776 /* show that we're waiting */
777 if ((now
- last
) > 1000) { /* every second */
784 /* reset to read mode */
785 addr
= (volatile FLASH_WORD_SIZE
*) info
->start
[0];
786 addr
[0] = (0x00F000F0 & FLASH_ID_MASK
); /* reset bank */
790 for (sect
= s_first
; sect
<= s_last
; sect
++) {
791 if (info
->protect
[sect
] == 0) { /* not protected */
793 #ifndef CONFIG_FLASH_16BIT
794 addr
= (vu_long
*) (info
->start
[sect
]);
795 addr
[0] = 0x00200020;
796 addr
[0] = 0x00D000D0;
797 while (!(addr
[0] & 0x00800080)); /* wait for error or finish */
798 if (addr
[0] & 0x003A003A) { /* check for error */
799 barf
= addr
[0] & 0x003A0000;
803 barf
= addr
[0] & 0x0000003A;
807 addr
= (vu_short
*) (info
->start
[sect
]);
810 while (!(addr
[0] & 0x0080)); /* wait for error or finish */
811 if (addr
[0] & 0x003A) /* check for error */
812 barf
= addr
[0] & 0x003A;
815 printf ("\nFlash error in sector at %lx\n", (unsigned long) addr
);
817 printf ("Block locked, not erased.\n");
818 if ((barf
& 0x0030) == 0x0030)
819 printf ("Command Sequence error.\n");
820 if ((barf
& 0x0030) == 0x0020)
821 printf ("Block Erase error.\n");
823 printf ("Vpp Low error.\n");
829 addr
= (volatile FLASH_WORD_SIZE
*) info
->start
[0];
830 addr
[0] = (0x00FF00FF & FLASH_ID_MASK
); /* reset bank */
839 /*-----------------------------------------------------------------------
842 /*-----------------------------------------------------------------------
843 * Copy memory to flash, returns:
846 * 2 - Flash not erased
849 int write_buff (flash_info_t
* info
, uchar
* src
, ulong addr
, ulong cnt
)
851 #ifndef CONFIG_FLASH_16BIT
860 #ifndef CONFIG_FLASH_16BIT
863 wp
= (addr
& ~3); /* get lower word aligned address */
866 * handle unaligned start bytes
868 if ((l
= addr
- wp
) != 0) {
870 for (i
= 0, cp
= wp
; i
< l
; ++i
, ++cp
) {
871 data
= (data
<< 8) | (*(uchar
*) cp
);
873 for (; i
< 4 && cnt
> 0; ++i
) {
874 data
= (data
<< 8) | *src
++;
878 for (; cnt
== 0 && i
< 4; ++i
, ++cp
) {
879 data
= (data
<< 8) | (*(uchar
*) cp
);
882 if ((rc
= write_word (info
, wp
, data
)) != 0) {
889 * handle word aligned part
893 for (i
= 0; i
< 4; ++i
) {
894 data
= (data
<< 8) | *src
++;
896 if ((rc
= write_word (info
, wp
, data
)) != 0) {
908 * handle unaligned tail bytes
911 for (i
= 0, cp
= wp
; i
< 4 && cnt
> 0; ++i
, ++cp
) {
912 data
= (data
<< 8) | *src
++;
915 for (; i
< 4; ++i
, ++cp
) {
916 data
= (data
<< 8) | (*(uchar
*) cp
);
919 return (write_word (info
, wp
, data
));
922 wp
= (addr
& ~1); /* get lower word aligned address */
925 * handle unaligned start byte
929 data
= (data
<< 8) | *src
++;
931 if ((rc
= write_short (info
, wp
, data
)) != 0) {
938 * handle word aligned part
940 /* l = 0; used for debuging */
943 for (i
= 0; i
< 2; ++i
) {
944 data
= (data
<< 8) | *src
++;
950 } used for debuging */
952 if ((rc
= write_short (info
, wp
, data
)) != 0) {
964 * handle unaligned tail bytes
967 for (i
= 0, cp
= wp
; i
< 2 && cnt
> 0; ++i
, ++cp
) {
968 data
= (data
<< 8) | *src
++;
971 for (; i
< 2; ++i
, ++cp
) {
972 data
= (data
<< 8) | (*(uchar
*) cp
);
975 return (write_short (info
, wp
, data
));
981 /*-----------------------------------------------------------------------
982 * Write a word to Flash, returns:
985 * 2 - Flash not erased
987 #ifndef CONFIG_FLASH_16BIT
988 static int write_word (flash_info_t
* info
, ulong dest
, ulong data
)
990 vu_long
*addr
= (vu_long
*) (info
->start
[0]);
995 /* Check if Flash is (sufficiently) erased */
996 if ((*((vu_long
*) dest
) & data
) != data
) {
1000 /* Disable interrupts which might cause a timeout here */
1001 flag
= disable_interrupts ();
1003 if (info
->flash_id
> FLASH_AMD_COMP
) {
1005 addr
[0x0555] = 0x00AA00AA;
1006 addr
[0x02AA] = 0x00550055;
1007 addr
[0x0555] = 0x00A000A0;
1012 *((vu_long
*) dest
) = data
;
1014 /* re-enable interrupts if necessary */
1016 enable_interrupts ();
1018 /* data polling for D7 */
1019 start
= get_timer (0);
1021 if (info
->flash_id
> FLASH_AMD_COMP
) {
1023 while ((*((vu_long
*) dest
) & 0x00800080) !=
1024 (data
& 0x00800080)) {
1025 if (get_timer (start
) > CONFIG_SYS_FLASH_WRITE_TOUT
) {
1032 while (!(addr
[0] & 0x00800080)) { /* wait for error or finish */
1033 if (get_timer (start
) > CONFIG_SYS_FLASH_WRITE_TOUT
) {
1037 if (addr
[0] & 0x003A003A) { /* check for error */
1038 barf
= addr
[0] & 0x003A0000;
1042 barf
= addr
[0] & 0x0000003A;
1044 printf ("\nFlash write error at address %lx\n", (unsigned long) dest
);
1046 printf ("Block locked, not erased.\n");
1048 printf ("Programming error.\n");
1050 printf ("Vpp Low error.\n");
1063 static int write_short (flash_info_t
* info
, ulong dest
, ushort data
)
1065 vu_short
*addr
= (vu_short
*) (info
->start
[0]);
1069 /* Check if Flash is (sufficiently) erased */
1070 if ((*((vu_short
*) dest
) & data
) != data
) {
1074 /* Disable interrupts which might cause a timeout here */
1075 flag
= disable_interrupts ();
1077 if (info
->flash_id
< FLASH_AMD_COMP
) {
1079 addr
[0x0555] = 0x00AA;
1080 addr
[0x02AA] = 0x0055;
1081 addr
[0x0555] = 0x00A0;
1087 *((vu_short
*) dest
) = data
;
1089 /* re-enable interrupts if necessary */
1091 enable_interrupts ();
1093 /* data polling for D7 */
1094 start
= get_timer (0);
1096 if (info
->flash_id
< FLASH_AMD_COMP
) {
1098 while ((*((vu_short
*) dest
) & 0x0080) != (data
& 0x0080)) {
1099 if (get_timer (start
) > CONFIG_SYS_FLASH_WRITE_TOUT
) {
1106 while (!(addr
[0] & 0x0080)) { /* wait for error or finish */
1107 if (get_timer (start
) > CONFIG_SYS_FLASH_WRITE_TOUT
)
1111 if (addr
[0] & 0x003A) { /* check for error */
1112 barf
= addr
[0] & 0x003A;
1113 printf ("\nFlash write error at address %lx\n",
1114 (unsigned long) dest
);
1116 printf ("Block locked, not erased.\n");
1118 printf ("Programming error.\n");
1120 printf ("Vpp Low error.\n");
1125 while (!(addr
[0] & 0x0080)) { /* wait for error or finish */
1126 if (get_timer (start
) > CONFIG_SYS_FLASH_WRITE_TOUT
)
1135 /*-----------------------------------------------------------------------*/