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 /*-----------------------------------------------------------------------
32 static ulong
flash_get_size (vu_long
*addr
, flash_info_t
*info
);
33 static int write_word (flash_info_t
*info
, ulong dest
, ulong data
);
34 static void flash_get_offsets (ulong base
, flash_info_t
*info
);
36 /*-----------------------------------------------------------------------
39 unsigned long flash_init (void)
41 volatile immap_t
*immap
= (immap_t
*)CONFIG_SYS_IMMR
;
42 volatile memctl8xx_t
*memctl
= &immap
->im_memctl
;
43 unsigned long size_b0
, size_b1
;
46 /* Init: no FLASHes known */
47 for (i
=0; i
<CONFIG_SYS_MAX_FLASH_BANKS
; ++i
) {
48 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
51 /* Static FLASH Bank configuration here - FIXME XXX */
53 size_b0
= flash_get_size((vu_long
*)FLASH_BASE0_PRELIM
, &flash_info
[0]);
55 if (flash_info
[0].flash_id
== FLASH_UNKNOWN
) {
56 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
57 size_b0
, size_b0
<<20);
60 size_b1
= flash_get_size((vu_long
*)FLASH_BASE1_PRELIM
, &flash_info
[1]);
62 if (size_b1
> size_b0
) {
64 "Bank 1 (0x%08lx = %ld MB) > Bank 0 (0x%08lx = %ld MB)\n",
68 flash_info
[0].flash_id
= FLASH_UNKNOWN
;
69 flash_info
[1].flash_id
= FLASH_UNKNOWN
;
70 flash_info
[0].sector_count
= -1;
71 flash_info
[1].sector_count
= -1;
72 flash_info
[0].size
= 0;
73 flash_info
[1].size
= 0;
77 /* Remap FLASH according to real size */
78 memctl
->memc_or0
= CONFIG_SYS_OR_TIMING_FLASH
| (-size_b0
& 0xFFFF8000);
79 #ifdef CONFIG_FLASH_16BIT
80 memctl
->memc_br0
= (CONFIG_SYS_FLASH_BASE
& BR_BA_MSK
) | BR_MS_GPCM
| BR_V
| BR_PS_16
; /* 16 Bit data port */
82 memctl
->memc_br0
= (CONFIG_SYS_FLASH_BASE
& BR_BA_MSK
) | BR_MS_GPCM
| BR_V
;
85 /* Re-do sizing to get full correct info */
86 size_b0
= flash_get_size((vu_long
*)CONFIG_SYS_FLASH_BASE
, &flash_info
[0]);
88 flash_get_offsets (CONFIG_SYS_FLASH_BASE
, &flash_info
[0]);
90 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
91 /* monitor protection ON by default */
92 flash_protect(FLAG_PROTECT_SET
,
93 CONFIG_SYS_MONITOR_BASE
,
94 CONFIG_SYS_MONITOR_BASE
+monitor_flash_len
-1,
99 memctl
->memc_or1
= CONFIG_SYS_OR_TIMING_FLASH
| (-size_b1
& 0xFFFF8000);
100 #ifdef CONFIG_FLASH_16BIT
101 memctl
->memc_br1
= ((CONFIG_SYS_FLASH_BASE
+ size_b0
) & BR_BA_MSK
) |
102 BR_MS_GPCM
| BR_V
| BR_PS_16
;
104 memctl
->memc_br1
= ((CONFIG_SYS_FLASH_BASE
+ size_b0
) & BR_BA_MSK
) |
108 /* Re-do sizing to get full correct info */
109 size_b1
= flash_get_size((vu_long
*)(CONFIG_SYS_FLASH_BASE
+ size_b0
),
112 flash_get_offsets (CONFIG_SYS_FLASH_BASE
+ size_b0
, &flash_info
[1]);
114 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
115 /* monitor protection ON by default */
116 flash_protect(FLAG_PROTECT_SET
,
117 CONFIG_SYS_MONITOR_BASE
,
118 CONFIG_SYS_MONITOR_BASE
+monitor_flash_len
-1,
122 memctl
->memc_br1
= 0; /* invalidate bank */
124 flash_info
[1].flash_id
= FLASH_UNKNOWN
;
125 flash_info
[1].sector_count
= -1;
128 flash_info
[0].size
= size_b0
;
129 flash_info
[1].size
= size_b1
;
131 return (size_b0
+ size_b1
);
134 /*-----------------------------------------------------------------------
136 static void flash_get_offsets (ulong base
, flash_info_t
*info
)
140 if (info
->flash_id
== FLASH_UNKNOWN
) {
144 if ((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_SST
) {
145 for (i
= 0; i
< info
->sector_count
; i
++) {
146 info
->start
[i
] = base
+ (i
* 0x00002000);
151 /* set up sector start address table */
152 if (info
->flash_id
& FLASH_BTYPE
) {
153 /* set sector offsets for bottom boot block type */
154 #ifdef CONFIG_FLASH_16BIT
155 info
->start
[0] = base
+ 0x00000000;
156 info
->start
[1] = base
+ 0x00004000;
157 info
->start
[2] = base
+ 0x00006000;
158 info
->start
[3] = base
+ 0x00008000;
159 for (i
= 4; i
< info
->sector_count
; i
++) {
160 info
->start
[i
] = base
+ (i
* 0x00010000) - 0x00030000;
162 info
->start
[0] = base
+ 0x00000000;
163 info
->start
[1] = base
+ 0x00008000;
164 info
->start
[2] = base
+ 0x0000C000;
165 info
->start
[3] = base
+ 0x00010000;
166 for (i
= 4; i
< info
->sector_count
; i
++) {
167 info
->start
[i
] = base
+ (i
* 0x00020000) - 0x00060000;
171 /* set sector offsets for top boot block type */
172 i
= info
->sector_count
- 1;
173 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
174 info
->start
[i
--] = base
+ info
->size
- 0x0000C000;
175 info
->start
[i
--] = base
+ info
->size
- 0x00010000;
176 for (; i
>= 0; i
--) {
177 info
->start
[i
] = base
+ i
* 0x00020000;
183 /*-----------------------------------------------------------------------
185 void flash_print_info (flash_info_t
*info
)
189 if (info
->flash_id
== FLASH_UNKNOWN
) {
190 printf ("missing or unknown FLASH type\n");
194 switch (info
->flash_id
& FLASH_VENDMASK
) {
195 case FLASH_MAN_AMD
: printf ("AMD "); break;
196 case FLASH_MAN_FUJ
: printf ("FUJITSU "); break;
197 case FLASH_MAN_SST
: printf ("SST "); break;
198 case FLASH_MAN_STM
: printf ("STM "); break;
199 default: printf ("Unknown Vendor "); break;
202 switch (info
->flash_id
& FLASH_TYPEMASK
) {
203 case FLASH_AM400B
: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
205 case FLASH_AM400T
: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
207 case FLASH_AM800B
: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
209 case FLASH_AM800T
: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
211 case FLASH_AM160B
: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
213 case FLASH_AM160T
: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
215 case FLASH_AM320B
: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
217 case FLASH_AM320T
: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
219 case FLASH_SST200A
: printf ("39xF200A (2M = 128K x 16)\n");
221 case FLASH_SST400A
: printf ("39xF400A (4M = 256K x 16)\n");
223 case FLASH_SST800A
: printf ("39xF800A (8M = 512K x 16)\n");
225 case FLASH_STM800AB
: printf ("M29W800AB (8M = 512K x 16)\n");
227 default: printf ("Unknown Chip Type\n");
231 printf (" Size: %ld MB in %d Sectors\n",
232 info
->size
>> 20, info
->sector_count
);
234 printf (" Sector Start Addresses:");
235 for (i
=0; i
<info
->sector_count
; ++i
) {
240 info
->protect
[i
] ? " (RO)" : " "
247 /*-----------------------------------------------------------------------
251 /*-----------------------------------------------------------------------
255 * The following code cannot be run from FLASH!
258 static ulong
flash_get_size (vu_long
*addr
, flash_info_t
*info
)
262 ulong base
= (ulong
)addr
;
264 /* Write auto select command: read Manufacturer ID */
265 #ifdef CONFIG_FLASH_16BIT
266 vu_short
*s_addr
= (vu_short
*)addr
;
267 s_addr
[0x5555] = 0x00AA;
268 s_addr
[0x2AAA] = 0x0055;
269 s_addr
[0x5555] = 0x0090;
271 value
= value
|(value
<<16);
273 addr
[0x5555] = 0x00AA00AA;
274 addr
[0x2AAA] = 0x00550055;
275 addr
[0x5555] = 0x00900090;
281 info
->flash_id
= FLASH_MAN_AMD
;
284 info
->flash_id
= FLASH_MAN_FUJ
;
287 info
->flash_id
= FLASH_MAN_SST
;
290 info
->flash_id
= FLASH_MAN_STM
;
293 info
->flash_id
= FLASH_UNKNOWN
;
294 info
->sector_count
= 0;
296 return (0); /* no or unknown flash */
298 #ifdef CONFIG_FLASH_16BIT
300 value
= value
|(value
<<16);
302 value
= addr
[1]; /* device ID */
307 info
->flash_id
+= FLASH_AM400T
;
308 info
->sector_count
= 11;
309 info
->size
= 0x00100000;
313 info
->flash_id
+= FLASH_AM400B
;
314 info
->sector_count
= 11;
315 info
->size
= 0x00100000;
319 info
->flash_id
+= FLASH_AM800T
;
320 info
->sector_count
= 19;
321 info
->size
= 0x00200000;
325 info
->flash_id
+= FLASH_AM800B
;
326 #ifdef CONFIG_FLASH_16BIT
327 info
->sector_count
= 19;
328 info
->size
= 0x00100000; /* => 1 MB */
330 info
->sector_count
= 19;
331 info
->size
= 0x00200000; /* => 2 MB */
336 info
->flash_id
+= FLASH_AM160T
;
337 info
->sector_count
= 35;
338 info
->size
= 0x00400000;
342 info
->flash_id
+= FLASH_AM160B
;
343 #ifdef CONFIG_FLASH_16BIT
344 info
->sector_count
= 35;
345 info
->size
= 0x00200000; /* => 2 MB */
347 info
->sector_count
= 35;
348 info
->size
= 0x00400000; /* => 4 MB */
352 #if 0 /* enable when device IDs are available */
354 info
->flash_id
+= FLASH_AM320T
;
355 info
->sector_count
= 67;
356 info
->size
= 0x00800000;
360 info
->flash_id
+= FLASH_AM320B
;
361 info
->sector_count
= 67;
362 info
->size
= 0x00800000;
366 info
->flash_id
+= FLASH_SST200A
;
367 info
->sector_count
= 64; /* 39xF200A ID ( 2M = 128K x 16 ) */
368 info
->size
= 0x00080000;
371 info
->flash_id
+= FLASH_SST400A
;
372 info
->sector_count
= 128; /* 39xF400A ID ( 4M = 256K x 16 ) */
373 info
->size
= 0x00100000;
376 info
->flash_id
+= FLASH_SST800A
;
377 info
->sector_count
= 256; /* 39xF800A ID ( 8M = 512K x 16 ) */
378 info
->size
= 0x00200000;
381 info
->flash_id
+= FLASH_STM800AB
;
382 info
->sector_count
= 19;
383 info
->size
= 0x00200000;
386 info
->flash_id
= FLASH_UNKNOWN
;
387 return (0); /* => no or unknown flash */
391 if (info
->sector_count
> CONFIG_SYS_MAX_FLASH_SECT
) {
392 printf ("** ERROR: sector count %d > max (%d) **\n",
393 info
->sector_count
, CONFIG_SYS_MAX_FLASH_SECT
);
394 info
->sector_count
= CONFIG_SYS_MAX_FLASH_SECT
;
397 if ((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_SST
) {
398 for (i
= 0; i
< info
->sector_count
; i
++) {
399 info
->start
[i
] = base
+ (i
* 0x00002000);
401 } else { /* AMD and Fujitsu types */
402 /* set up sector start address table */
403 if (info
->flash_id
& FLASH_BTYPE
) {
404 /* set sector offsets for bottom boot block type */
405 #ifdef CONFIG_FLASH_16BIT
407 info
->start
[0] = base
+ 0x00000000;
408 info
->start
[1] = base
+ 0x00004000;
409 info
->start
[2] = base
+ 0x00006000;
410 info
->start
[3] = base
+ 0x00008000;
411 for (i
= 4; i
< info
->sector_count
; i
++) {
412 info
->start
[i
] = base
+ (i
* 0x00010000) - 0x00030000;
414 info
->start
[0] = base
+ 0x00000000;
415 info
->start
[1] = base
+ 0x00008000;
416 info
->start
[2] = base
+ 0x0000C000;
417 info
->start
[3] = base
+ 0x00010000;
418 for (i
= 4; i
< info
->sector_count
; i
++) {
419 info
->start
[i
] = base
+ (i
* 0x00020000) - 0x00060000;
423 /* set sector offsets for top boot block type */
424 i
= info
->sector_count
- 1;
425 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
426 info
->start
[i
--] = base
+ info
->size
- 0x0000C000;
427 info
->start
[i
--] = base
+ info
->size
- 0x00010000;
428 for (; i
>= 0; i
--) {
429 info
->start
[i
] = base
+ i
* 0x00020000;
433 /* check for protected sectors */
434 for (i
= 0; i
< info
->sector_count
; i
++) {
435 /* read sector protection at sector address:
437 * D0 = 1 if protected
439 #ifdef CONFIG_FLASH_16BIT
440 s_addr
= (volatile unsigned short *)(info
->start
[i
]);
441 info
->protect
[i
] = s_addr
[2] & 1;
443 addr
= (volatile unsigned long *)(info
->start
[i
]);
444 info
->protect
[i
] = addr
[2] & 1;
450 * Prevent writes to uninitialized FLASH.
452 if (info
->flash_id
!= FLASH_UNKNOWN
) {
453 #ifdef CONFIG_FLASH_16BIT
454 s_addr
= (volatile unsigned short *)(info
->start
[0]);
455 *s_addr
= 0x00F0; /* reset bank */
457 addr
= (volatile unsigned long *)info
->start
[0];
458 *addr
= 0x00F000F0; /* reset bank */
466 /*-----------------------------------------------------------------------
469 int flash_erase (flash_info_t
*info
, int s_first
, int s_last
)
471 vu_long
*addr
= (vu_long
*)(info
->start
[0]);
472 int flag
, prot
, sect
;
473 ulong start
, now
, last
;
474 #ifdef CONFIG_FLASH_16BIT
475 vu_short
*s_addr
= (vu_short
*)addr
;
478 if ((s_first
< 0) || (s_first
> s_last
)) {
479 if (info
->flash_id
== FLASH_UNKNOWN
) {
480 printf ("- missing\n");
482 printf ("- no sectors to erase\n");
486 /*#ifndef CONFIG_FLASH_16BIT
488 type = (info->flash_id & FLASH_VENDMASK);
489 if ((type != FLASH_MAN_SST) && (type != FLASH_MAN_STM)) {
490 printf ("Can't erase unknown flash type %08lx - aborted\n",
496 for (sect
=s_first
; sect
<=s_last
; ++sect
) {
497 if (info
->protect
[sect
]) {
503 printf ("- Warning: %d protected sectors will not be erased!\n",
509 start
= get_timer (0);
511 /* Start erase on unprotected sectors */
512 for (sect
= s_first
; sect
<=s_last
; sect
++) {
513 if (info
->protect
[sect
] == 0) { /* not protected */
514 #ifdef CONFIG_FLASH_16BIT
515 vu_short
*s_sect_addr
= (vu_short
*)(info
->start
[sect
]);
517 vu_long
*sect_addr
= (vu_long
*)(info
->start
[sect
]);
519 /* Disable interrupts which might cause a timeout here */
520 flag
= disable_interrupts();
522 #ifdef CONFIG_FLASH_16BIT
524 /*printf("\ns_sect_addr=%x",s_sect_addr);*/
525 s_addr
[0x5555] = 0x00AA;
526 s_addr
[0x2AAA] = 0x0055;
527 s_addr
[0x5555] = 0x0080;
528 s_addr
[0x5555] = 0x00AA;
529 s_addr
[0x2AAA] = 0x0055;
530 s_sect_addr
[0] = 0x0030;
532 addr
[0x5555] = 0x00AA00AA;
533 addr
[0x2AAA] = 0x00550055;
534 addr
[0x5555] = 0x00800080;
535 addr
[0x5555] = 0x00AA00AA;
536 addr
[0x2AAA] = 0x00550055;
537 sect_addr
[0] = 0x00300030;
539 /* re-enable interrupts if necessary */
543 /* wait at least 80us - let's wait 1 ms */
546 #ifdef CONFIG_FLASH_16BIT
547 while ((s_sect_addr
[0] & 0x0080) != 0x0080) {
549 while ((sect_addr
[0] & 0x00800080) != 0x00800080) {
551 if ((now
= get_timer(start
)) > CONFIG_SYS_FLASH_ERASE_TOUT
) {
552 printf ("Timeout\n");
555 /* show that we're waiting */
556 if ((now
- last
) > 1000) { /* every second */
564 /* reset to read mode */
565 addr
= (volatile unsigned long *)info
->start
[0];
566 #ifdef CONFIG_FLASH_16BIT
567 s_addr
[0] = 0x00F0; /* reset bank */
569 addr
[0] = 0x00F000F0; /* reset bank */
576 /*-----------------------------------------------------------------------
577 * Copy memory to flash, returns:
580 * 2 - Flash not erased
581 * 4 - Flash not identified
584 int write_buff (flash_info_t
*info
, uchar
*src
, ulong addr
, ulong cnt
)
589 if (info
->flash_id
== FLASH_UNKNOWN
) {
593 wp
= (addr
& ~3); /* get lower word aligned address */
596 * handle unaligned start bytes
598 if ((l
= addr
- wp
) != 0) {
600 for (i
=0, cp
=wp
; i
<l
; ++i
, ++cp
) {
601 data
= (data
<< 8) | (*(uchar
*)cp
);
603 for (; i
<4 && cnt
>0; ++i
) {
604 data
= (data
<< 8) | *src
++;
608 for (; cnt
==0 && i
<4; ++i
, ++cp
) {
609 data
= (data
<< 8) | (*(uchar
*)cp
);
612 if ((rc
= write_word(info
, wp
, data
)) != 0) {
619 * handle word aligned part
623 for (i
=0; i
<4; ++i
) {
624 data
= (data
<< 8) | *src
++;
626 if ((rc
= write_word(info
, wp
, data
)) != 0) {
638 * handle unaligned tail bytes
641 for (i
=0, cp
=wp
; i
<4 && cnt
>0; ++i
, ++cp
) {
642 data
= (data
<< 8) | *src
++;
645 for (; i
<4; ++i
, ++cp
) {
646 data
= (data
<< 8) | (*(uchar
*)cp
);
649 return (write_word(info
, wp
, data
));
652 /*-----------------------------------------------------------------------
653 * Write a word to Flash, returns:
656 * 2 - Flash not erased
658 static int write_word (flash_info_t
*info
, ulong dest
, ulong data
)
660 vu_long
*addr
= (vu_long
*)(info
->start
[0]);
662 #ifdef CONFIG_FLASH_16BIT
665 vu_short
*s_addr
= (vu_short
*)addr
;
670 /* Check if Flash is (sufficiently) erased */
671 if ((*((vu_long
*)dest
) & data
) != data
) {
675 #ifdef CONFIG_FLASH_16BIT
676 /* Write the 16 higher-bits */
677 /* Disable interrupts which might cause a timeout here */
678 flag
= disable_interrupts();
680 high_data
= ((data
>>16) & 0x0000ffff);
682 s_addr
[0x5555] = 0x00AA;
683 s_addr
[0x2AAA] = 0x0055;
684 s_addr
[0x5555] = 0x00A0;
686 *((vu_short
*)dest
) = high_data
;
689 /* re-enable interrupts if necessary */
693 /* data polling for D7 */
694 start
= get_timer (0);
695 while ((*((vu_short
*)dest
) & 0x0080) != (high_data
& 0x0080)) {
696 if (get_timer(start
) > CONFIG_SYS_FLASH_WRITE_TOUT
) {
702 /* Write the 16 lower-bits */
705 /* Disable interrupts which might cause a timeout here */
706 flag
= disable_interrupts();
707 #ifdef CONFIG_FLASH_16BIT
709 low_data
= (data
& 0x0000ffff);
711 s_addr
[0x5555] = 0x00AA;
712 s_addr
[0x2AAA] = 0x0055;
713 s_addr
[0x5555] = 0x00A0;
714 *((vu_short
*)dest
) = low_data
;
717 addr
[0x5555] = 0x00AA00AA;
718 addr
[0x2AAA] = 0x00550055;
719 addr
[0x5555] = 0x00A000A0;
720 *((vu_long
*)dest
) = data
;
723 /* re-enable interrupts if necessary */
727 /* data polling for D7 */
728 start
= get_timer (0);
730 #ifdef CONFIG_FLASH_16BIT
731 while ((*((vu_short
*)dest
) & 0x0080) != (low_data
& 0x0080)) {
733 while ((*((vu_long
*)dest
) & 0x00800080) != (data
& 0x00800080)) {
736 if (get_timer(start
) > CONFIG_SYS_FLASH_WRITE_TOUT
) {
743 /*-----------------------------------------------------------------------