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
[CFG_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 #define FLAG_PROTECT_SET 0x01
40 #define FLAG_PROTECT_CLEAR 0x02
42 /*-----------------------------------------------------------------------
45 unsigned long flash_init (void)
47 volatile immap_t
*immap
= (immap_t
*)CFG_IMMR
;
48 volatile memctl8xx_t
*memctl
= &immap
->im_memctl
;
49 unsigned long size_b0
, size_b1
;
56 /* Init: no FLASHes known */
57 for (i
=0; i
< CFG_MAX_FLASH_BANKS
; ++i
)
59 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
62 /* Static FLASH Bank configuration here - FIXME XXX */
64 size_b0
= flash_get_size((vu_long
*)FLASH_BASE0_PRELIM
, &flash_info
[0]);
66 if (flash_info
[0].flash_id
== FLASH_UNKNOWN
)
68 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
69 size_b0
, size_b0
<<20);
73 if (FLASH_BASE1_PRELIM
!= 0x0) {
74 size_b1
= flash_get_size((vu_long
*)FLASH_BASE1_PRELIM
, &flash_info
[1]);
76 if (size_b1
> size_b0
) {
77 printf ("## ERROR: Bank 1 (0x%08lx = %ld MB) > Bank 0 (0x%08lx = %ld MB)\n",
78 size_b1
, size_b1
<<20,size_b0
, size_b0
<<20);
80 flash_info
[0].flash_id
= FLASH_UNKNOWN
;
81 flash_info
[1].flash_id
= FLASH_UNKNOWN
;
82 flash_info
[0].sector_count
= -1;
83 flash_info
[1].sector_count
= -1;
84 flash_info
[0].size
= 0;
85 flash_info
[1].size
= 0;
92 /* Remap FLASH according to real size */
93 memctl
->memc_or0
= CFG_OR0_PRELIM
;
94 memctl
->memc_br0
= CFG_BR0_PRELIM
;
96 /* Re-do sizing to get full correct info */
97 size_b0
= flash_get_size((vu_long
*)CFG_FLASH_BASE
, &flash_info
[0]);
99 flash_get_offsets (CFG_FLASH_BASE
, &flash_info
[0]);
101 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
102 /* monitor protection ON by default */
103 (void)flash_protect(FLAG_PROTECT_SET
,
105 CFG_MONITOR_BASE
+monitor_flash_len
-1,
111 /* memctl->memc_or1 = CFG_OR1_PRELIM;
112 memctl->memc_br1 = CFG_BR1_PRELIM; */
114 /* Re-do sizing to get full correct info */
115 size_b1
= flash_get_size((vu_long
*)(CFG_FLASH_BASE
+ size_b0
),
118 flash_get_offsets (CFG_FLASH_BASE
+ size_b0
, &flash_info
[1]);
120 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
121 /* monitor protection ON by default */
122 (void)flash_protect(FLAG_PROTECT_SET
,
124 CFG_MONITOR_BASE
+monitor_flash_len
-1,
130 /* memctl->memc_or1 = CFG_OR1_PRELIM;
131 FIXME memctl->memc_br1 = CFG_BR1_PRELIM; */
133 flash_info
[1].flash_id
= FLASH_UNKNOWN
;
134 flash_info
[1].sector_count
= -1;
137 flash_info
[0].size
= size_b0
;
138 flash_info
[1].size
= size_b1
;
140 return (size_b0
+ size_b1
);
144 static void flash_get_offsets (ulong base
, flash_info_t
*info
)
148 /* set up sector start adress table */
149 if (info
->flash_id
& FLASH_BTYPE
)
151 /* set sector offsets for bottom boot block type */
152 for (i
= 0; i
< info
->sector_count
; i
++)
154 info
->start
[i
] = base
+ (i
* 0x00040000);
159 /* set sector offsets for top boot block type */
160 i
= info
->sector_count
- 1;
163 info
->start
[i
] = base
+ i
* 0x00040000;
169 /*-----------------------------------------------------------------------
171 void flash_print_info (flash_info_t
*info
)
175 if (info
->flash_id
== FLASH_UNKNOWN
) {
176 printf ("missing or unknown FLASH type\n");
180 switch (info
->flash_id
& FLASH_VENDMASK
) {
181 case FLASH_MAN_AMD
: printf ("AMD "); break;
182 case FLASH_MAN_FUJ
: printf ("FUJITSU "); break;
183 default: printf ("Unknown Vendor "); break;
186 switch (info
->flash_id
& FLASH_TYPEMASK
) {
190 printf ("AM29F040B (4 Mbit, bottom boot sect)\n");
193 printf ("AM29F040T (4 Mbit, top boot sect)\n");
197 printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
200 printf ("AM29LV400T (4 Mbit, top boot sector)\n");
203 printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
206 printf ("AM29LV800T (8 Mbit, top boot sector)\n");
209 printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
212 printf ("AM29LV160T (16 Mbit, top boot sector)\n");
215 printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
218 printf ("AM29LV320T (32 Mbit, top boot sector)\n");
221 printf ("Unknown Chip Type\n");
225 printf (" Size: %ld MB in %d Sectors\n",
229 printf (" Sector Start Addresses:");
231 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
;
266 /* Write auto select command: read Manufacturer ID */
268 addr
[0x0555] = 0x00AA00AA;
269 addr
[0x02AA] = 0x00550055;
270 addr
[0x0555] = 0x00900090;
272 addr
[0x0555] = 0xAAAAAAAA;
273 addr
[0x02AA] = 0x55555555;
274 addr
[0x0555] = 0x90909090;
282 info
->flash_id
= FLASH_MAN_AMD
;
286 info
->flash_id
= FLASH_MAN_FUJ
;
290 info
->flash_id
= FLASH_UNKNOWN
;
291 info
->sector_count
= 0;
296 value
= addr
[1]; /* device ID */
302 info
->flash_id
+= FLASH_AM040B
;
303 info
->sector_count
= 8;
304 info
->size
= 0x00200000;
308 info
->flash_id
+= FLASH_AM400T
;
309 info
->sector_count
= 11;
310 info
->size
= 0x00100000;
314 info
->flash_id
+= FLASH_AM400B
;
315 info
->sector_count
= 11;
316 info
->size
= 0x00100000;
320 info
->flash_id
+= FLASH_AM800T
;
321 info
->sector_count
= 19;
322 info
->size
= 0x00200000;
326 info
->flash_id
+= FLASH_AM800B
;
327 info
->sector_count
= 19;
328 info
->size
= 0x00200000;
332 info
->flash_id
+= FLASH_AM160T
;
333 info
->sector_count
= 35;
334 info
->size
= 0x00400000;
338 info
->flash_id
+= FLASH_AM160B
;
339 info
->sector_count
= 35;
340 info
->size
= 0x00400000;
342 #if 0 /* enable when device IDs are available */
344 info
->flash_id
+= FLASH_AM320T
;
345 info
->sector_count
= 67;
346 info
->size
= 0x00800000;
350 info
->flash_id
+= FLASH_AM320B
;
351 info
->sector_count
= 67;
352 info
->size
= 0x00800000;
356 info
->flash_id
= FLASH_UNKNOWN
;
357 return (0); /* => no or unknown flash */
362 /* set up sector start adress table */
363 if (info
->flash_id
& FLASH_BTYPE
) {
364 /* set sector offsets for bottom boot block type */
365 info
->start
[0] = base
+ 0x00000000;
366 info
->start
[1] = base
+ 0x00008000;
367 info
->start
[2] = base
+ 0x0000C000;
368 info
->start
[3] = base
+ 0x00010000;
369 for (i
= 4; i
< info
->sector_count
; i
++) {
370 info
->start
[i
] = base
+ (i
* 0x00020000) - 0x00060000;
373 /* set sector offsets for top boot block type */
374 i
= info
->sector_count
- 1;
375 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
376 info
->start
[i
--] = base
+ info
->size
- 0x0000C000;
377 info
->start
[i
--] = base
+ info
->size
- 0x00010000;
378 for (; i
>= 0; i
--) {
379 info
->start
[i
] = base
+ i
* 0x00020000;
383 flash_get_offsets ((ulong
)addr
, &flash_info
[0]);
386 /* check for protected sectors */
387 for (i
= 0; i
< info
->sector_count
; i
++)
389 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
390 /* D0 = 1 if protected */
391 addr
= (volatile unsigned long *)(info
->start
[i
]);
392 info
->protect
[i
] = addr
[2] & 1;
396 * Prevent writes to uninitialized FLASH.
398 if (info
->flash_id
!= FLASH_UNKNOWN
)
400 addr
= (volatile unsigned long *)info
->start
[0];
402 *addr
= 0x00F000F0; /* reset bank */
404 *addr
= 0xF0F0F0F0; /* reset bank */
412 /*-----------------------------------------------------------------------
415 int flash_erase (flash_info_t
*info
, int s_first
, int s_last
)
417 vu_long
*addr
= (vu_long
*)(info
->start
[0]);
418 int flag
, prot
, sect
, l_sect
;
419 ulong start
, now
, last
;
421 if ((s_first
< 0) || (s_first
> s_last
)) {
422 if (info
->flash_id
== FLASH_UNKNOWN
) {
423 printf ("- missing\n");
425 printf ("- no sectors to erase\n");
430 if ((info
->flash_id
== FLASH_UNKNOWN
) ||
431 (info
->flash_id
> FLASH_AMD_COMP
)) {
432 printf ("Can't erase unknown flash type - aborted\n");
437 for (sect
=s_first
; sect
<=s_last
; ++sect
) {
438 if (info
->protect
[sect
]) {
444 printf ("- Warning: %d protected sectors will not be erased!\n",
452 /* Disable interrupts which might cause a timeout here */
453 flag
= disable_interrupts();
456 addr
[0x0555] = 0x00AA00AA;
457 addr
[0x02AA] = 0x00550055;
458 addr
[0x0555] = 0x00800080;
459 addr
[0x0555] = 0x00AA00AA;
460 addr
[0x02AA] = 0x00550055;
462 addr
[0x0555] = 0xAAAAAAAA;
463 addr
[0x02AA] = 0x55555555;
464 addr
[0x0555] = 0x80808080;
465 addr
[0x0555] = 0xAAAAAAAA;
466 addr
[0x02AA] = 0x55555555;
469 /* Start erase on unprotected sectors */
470 for (sect
= s_first
; sect
<=s_last
; sect
++) {
471 if (info
->protect
[sect
] == 0) { /* not protected */
472 addr
= (vu_long
*)(info
->start
[sect
]);
474 addr
[0] = 0x00300030;
476 addr
[0] = 0x30303030;
482 /* re-enable interrupts if necessary */
486 /* wait at least 80us - let's wait 1 ms */
490 * We wait for the last triggered sector
495 start
= get_timer (0);
497 addr
= (vu_long
*)(info
->start
[l_sect
]);
499 while ((addr
[0] & 0x00800080) != 0x00800080)
501 while ((addr
[0] & 0xFFFFFFFF) != 0xFFFFFFFF)
504 if ((now
= get_timer(start
)) > CFG_FLASH_ERASE_TOUT
) {
505 printf ("Timeout\n");
508 /* show that we're waiting */
509 if ((now
- last
) > 1000) { /* every second */
516 /* reset to read mode */
517 addr
= (volatile unsigned long *)info
->start
[0];
519 addr
[0] = 0x00F000F0; /* reset bank */
521 addr
[0] = 0xF0F0F0F0; /* reset bank */
528 /*-----------------------------------------------------------------------
529 * Copy memory to flash, returns:
532 * 2 - Flash not erased
535 int write_buff (flash_info_t
*info
, uchar
*src
, ulong addr
, ulong cnt
)
540 wp
= (addr
& ~3); /* get lower word aligned address */
543 * handle unaligned start bytes
545 if ((l
= addr
- wp
) != 0) {
547 for (i
=0, cp
=wp
; i
<l
; ++i
, ++cp
) {
548 data
= (data
<< 8) | (*(uchar
*)cp
);
550 for (; i
<4 && cnt
>0; ++i
) {
551 data
= (data
<< 8) | *src
++;
555 for (; cnt
==0 && i
<4; ++i
, ++cp
) {
556 data
= (data
<< 8) | (*(uchar
*)cp
);
559 if ((rc
= write_word(info
, wp
, data
)) != 0) {
566 * handle word aligned part
570 for (i
=0; i
<4; ++i
) {
571 data
= (data
<< 8) | *src
++;
573 if ((rc
= write_word(info
, wp
, data
)) != 0) {
585 * handle unaligned tail bytes
588 for (i
=0, cp
=wp
; i
<4 && cnt
>0; ++i
, ++cp
) {
589 data
= (data
<< 8) | *src
++;
592 for (; i
<4; ++i
, ++cp
) {
593 data
= (data
<< 8) | (*(uchar
*)cp
);
596 return (write_word(info
, wp
, data
));
598 /*-----------------------------------------------------------------------
599 * Write a word to Flash, returns:
602 * 2 - Flash not erased
604 static int write_word (flash_info_t
*info
, ulong dest
, ulong data
)
606 vu_long
*addr
= (vu_long
*)(info
->start
[0]);
610 /* Check if Flash is (sufficiently) erased */
611 if ((*((vu_long
*)dest
) & data
) != data
) {
614 /* Disable interrupts which might cause a timeout here */
615 flag
= disable_interrupts();
618 addr
[0x0555] = 0x00AA00AA;
619 addr
[0x02AA] = 0x00550055;
620 addr
[0x0555] = 0x00A000A0;
622 addr
[0x0555] = 0xAAAAAAAA;
623 addr
[0x02AA] = 0x55555555;
624 addr
[0x0555] = 0xA0A0A0A0;
627 *((vu_long
*)dest
) = data
;
629 /* re-enable interrupts if necessary */
633 /* data polling for D7 */
634 start
= get_timer (0);
636 while ((*((vu_long
*)dest
) & 0x00800080) != (data
& 0x00800080))
638 while ((*((vu_long
*)dest
) & 0x80808080) != (data
& 0x80808080))
641 if (get_timer(start
) > CFG_FLASH_WRITE_TOUT
) {
648 /*-----------------------------------------------------------------------