2 * (C) Copyright 2000-2004
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 * Wait for completion of each sector erase command issued
28 * Chris Hallinan - DS4.COM, Inc. - clh@net1plus.com
33 #include <asm/processor.h>
35 #if CFG_MAX_FLASH_BANKS != 1
36 #error "CFG_MAX_FLASH_BANKS must be 1"
38 flash_info_t flash_info
[CFG_MAX_FLASH_BANKS
]; /* info for FLASH chips */
40 /*-----------------------------------------------------------------------
43 static ulong
flash_get_size (vu_long
* addr
, flash_info_t
* info
);
44 static int write_word (flash_info_t
* info
, ulong dest
, ulong data
);
45 static void flash_get_offsets (ulong base
, flash_info_t
* info
);
49 #define FLASH_WORD_SIZE unsigned char
51 /*-----------------------------------------------------------------------
54 unsigned long flash_init (void)
56 unsigned long size_b0
;
58 /* Init: no FLASHes known */
59 flash_info
[0].flash_id
= FLASH_UNKNOWN
;
61 /* Static FLASH Bank configuration here - FIXME XXX */
63 size_b0
= flash_get_size ((vu_long
*) FLASH_BASE0_PRELIM
, &flash_info
[0]);
65 if (flash_info
[0].flash_id
== FLASH_UNKNOWN
) {
66 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
67 size_b0
, size_b0
<< 20);
72 flash_get_offsets (FLASH_BASE0_PRELIM
, &flash_info
[0]);
74 /* Monitor protection ON by default */
75 (void) flash_protect (FLAG_PROTECT_SET
,
77 FLASH_BASE0_PRELIM
+ monitor_flash_len
- 1,
79 flash_info
[0].size
= size_b0
;
85 /*-----------------------------------------------------------------------
87 static void flash_get_offsets (ulong base
, flash_info_t
* info
)
91 /* set up sector start address table */
92 if (((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_SST
) ||
93 (info
->flash_id
== FLASH_AM040
)) {
94 for (i
= 0; i
< info
->sector_count
; i
++)
95 info
->start
[i
] = base
+ (i
* 0x00010000);
97 if (info
->flash_id
& FLASH_BTYPE
) {
98 /* set sector offsets for bottom boot block type */
99 info
->start
[0] = base
+ 0x00000000;
100 info
->start
[1] = base
+ 0x00004000;
101 info
->start
[2] = base
+ 0x00006000;
102 info
->start
[3] = base
+ 0x00008000;
103 for (i
= 4; i
< info
->sector_count
; i
++) {
105 base
+ (i
* 0x00010000) - 0x00030000;
108 /* set sector offsets for top boot block type */
109 i
= info
->sector_count
- 1;
110 info
->start
[i
--] = base
+ info
->size
- 0x00004000;
111 info
->start
[i
--] = base
+ info
->size
- 0x00006000;
112 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
113 for (; i
>= 0; i
--) {
114 info
->start
[i
] = base
+ i
* 0x00010000;
120 /*-----------------------------------------------------------------------
122 void flash_print_info (flash_info_t
* info
)
128 volatile unsigned long *flash
;
130 if (info
->flash_id
== FLASH_UNKNOWN
) {
131 printf ("missing or unknown FLASH type\n");
135 switch (info
->flash_id
& FLASH_VENDMASK
) {
146 printf ("Unknown Vendor ");
150 switch (info
->flash_id
& FLASH_TYPEMASK
) {
152 printf ("AM29F040 (512 Kbit, uniform sector size)\n");
155 printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
158 printf ("AM29LV400T (4 Mbit, top boot sector)\n");
161 printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
164 printf ("AM29LV800T (8 Mbit, top boot sector)\n");
167 printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
170 printf ("AM29LV160T (16 Mbit, top boot sector)\n");
173 printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
176 printf ("AM29LV320T (32 Mbit, top boot sector)\n");
179 printf ("SST39LF/VF800 (8 Mbit, uniform sector size)\n");
182 printf ("SST39LF/VF160 (16 Mbit, uniform sector size)\n");
185 printf ("Unknown Chip Type\n");
189 printf (" Size: %ld KB in %d Sectors\n",
190 info
->size
>> 10, info
->sector_count
);
192 printf (" Sector Start Addresses:");
193 for (i
= 0; i
< info
->sector_count
; ++i
) {
195 * Check if whole sector is erased
197 if (i
!= (info
->sector_count
- 1))
198 size
= info
->start
[i
+ 1] - info
->start
[i
];
200 size
= info
->start
[0] + info
->size
- info
->start
[i
];
202 flash
= (volatile unsigned long *) info
->start
[i
];
203 size
= size
>> 2; /* divide by 4 for longword access */
204 for (k
= 0; k
< size
; k
++) {
205 if (*flash
++ != 0xffffffff) {
213 printf (" %08lX%s%s",
215 erased
? " E" : " ", info
->protect
[i
] ? "RO " : " "
222 /*-----------------------------------------------------------------------
226 /*-----------------------------------------------------------------------
230 * The following code cannot be run from FLASH!
232 static ulong
flash_get_size (vu_long
* addr
, flash_info_t
* info
)
235 FLASH_WORD_SIZE value
;
236 ulong base
= (ulong
) addr
;
237 volatile FLASH_WORD_SIZE
*addr2
= (FLASH_WORD_SIZE
*) addr
;
239 /* Write auto select command: read Manufacturer ID */
240 addr2
[ADDR0
] = (FLASH_WORD_SIZE
) 0x00AA00AA;
241 addr2
[ADDR1
] = (FLASH_WORD_SIZE
) 0x00550055;
242 addr2
[ADDR0
] = (FLASH_WORD_SIZE
) 0x00900090;
247 case (FLASH_WORD_SIZE
) AMD_MANUFACT
:
248 info
->flash_id
= FLASH_MAN_AMD
;
250 case (FLASH_WORD_SIZE
) FUJ_MANUFACT
:
251 info
->flash_id
= FLASH_MAN_FUJ
;
253 case (FLASH_WORD_SIZE
) SST_MANUFACT
:
254 info
->flash_id
= FLASH_MAN_SST
;
257 info
->flash_id
= FLASH_UNKNOWN
;
258 info
->sector_count
= 0;
260 return (0); /* no or unknown flash */
263 value
= addr2
[1]; /* device ID */
266 case (FLASH_WORD_SIZE
) AMD_ID_F040B
:
267 info
->flash_id
+= FLASH_AM040
;
268 info
->sector_count
= 8;
269 info
->size
= 0x0080000; /* => 512 ko */
271 case (FLASH_WORD_SIZE
) AMD_ID_LV040B
:
272 info
->flash_id
+= FLASH_AM040
;
273 info
->sector_count
= 8;
274 info
->size
= 0x0080000; /* => 512 ko */
276 case (FLASH_WORD_SIZE
) AMD_ID_LV400T
:
277 info
->flash_id
+= FLASH_AM400T
;
278 info
->sector_count
= 11;
279 info
->size
= 0x00080000;
280 break; /* => 0.5 MB */
282 case (FLASH_WORD_SIZE
) AMD_ID_LV400B
:
283 info
->flash_id
+= FLASH_AM400B
;
284 info
->sector_count
= 11;
285 info
->size
= 0x00080000;
286 break; /* => 0.5 MB */
288 case (FLASH_WORD_SIZE
) AMD_ID_LV800T
:
289 info
->flash_id
+= FLASH_AM800T
;
290 info
->sector_count
= 19;
291 info
->size
= 0x00100000;
294 case (FLASH_WORD_SIZE
) AMD_ID_LV800B
:
295 info
->flash_id
+= FLASH_AM800B
;
296 info
->sector_count
= 19;
297 info
->size
= 0x00100000;
300 case (FLASH_WORD_SIZE
) AMD_ID_LV160T
:
301 info
->flash_id
+= FLASH_AM160T
;
302 info
->sector_count
= 35;
303 info
->size
= 0x00200000;
306 case (FLASH_WORD_SIZE
) AMD_ID_LV160B
:
307 info
->flash_id
+= FLASH_AM160B
;
308 info
->sector_count
= 35;
309 info
->size
= 0x00200000;
311 #if 0 /* enable when device IDs are available */
312 case (FLASH_WORD_SIZE
) AMD_ID_LV320T
:
313 info
->flash_id
+= FLASH_AM320T
;
314 info
->sector_count
= 67;
315 info
->size
= 0x00400000;
318 case (FLASH_WORD_SIZE
) AMD_ID_LV320B
:
319 info
->flash_id
+= FLASH_AM320B
;
320 info
->sector_count
= 67;
321 info
->size
= 0x00400000;
324 case (FLASH_WORD_SIZE
) SST_ID_xF800A
:
325 info
->flash_id
+= FLASH_SST800A
;
326 info
->sector_count
= 16;
327 info
->size
= 0x00100000;
330 case (FLASH_WORD_SIZE
) SST_ID_xF160A
:
331 info
->flash_id
+= FLASH_SST160A
;
332 info
->sector_count
= 32;
333 info
->size
= 0x00200000;
337 info
->flash_id
= FLASH_UNKNOWN
;
338 return (0); /* => no or unknown flash */
342 /* set up sector start address table */
343 if (((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_SST
) ||
344 (info
->flash_id
== FLASH_AM040
)) {
345 for (i
= 0; i
< info
->sector_count
; i
++)
346 info
->start
[i
] = base
+ (i
* 0x00010000);
348 if (info
->flash_id
& FLASH_BTYPE
) {
349 /* set sector offsets for bottom boot block type */
350 info
->start
[0] = base
+ 0x00000000;
351 info
->start
[1] = base
+ 0x00004000;
352 info
->start
[2] = base
+ 0x00006000;
353 info
->start
[3] = base
+ 0x00008000;
354 for (i
= 4; i
< info
->sector_count
; i
++) {
356 base
+ (i
* 0x00010000) - 0x00030000;
359 /* set sector offsets for top boot block type */
360 i
= info
->sector_count
- 1;
361 info
->start
[i
--] = base
+ info
->size
- 0x00004000;
362 info
->start
[i
--] = base
+ info
->size
- 0x00006000;
363 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
364 for (; i
>= 0; i
--) {
365 info
->start
[i
] = base
+ i
* 0x00010000;
370 /* check for protected sectors */
371 for (i
= 0; i
< info
->sector_count
; i
++) {
372 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
373 /* D0 = 1 if protected */
374 addr2
= (volatile FLASH_WORD_SIZE
*) (info
->start
[i
]);
375 if ((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_SST
)
376 info
->protect
[i
] = 0;
378 info
->protect
[i
] = addr2
[2] & 1;
382 * Prevent writes to uninitialized FLASH.
384 if (info
->flash_id
!= FLASH_UNKNOWN
) {
385 addr2
= (FLASH_WORD_SIZE
*) info
->start
[0];
386 *addr2
= (FLASH_WORD_SIZE
) 0x00F000F0; /* reset bank */
392 int wait_for_DQ7 (flash_info_t
* info
, int sect
)
394 ulong start
, now
, last
;
395 volatile FLASH_WORD_SIZE
*addr
=
396 (FLASH_WORD_SIZE
*) (info
->start
[sect
]);
398 start
= get_timer (0);
400 while ((addr
[0] & (FLASH_WORD_SIZE
) 0x00800080) !=
401 (FLASH_WORD_SIZE
) 0x00800080) {
402 if ((now
= get_timer (start
)) > CFG_FLASH_ERASE_TOUT
) {
403 printf ("Timeout\n");
406 /* show that we're waiting */
407 if ((now
- last
) > 1000) { /* every second */
415 /*-----------------------------------------------------------------------
418 int flash_erase (flash_info_t
* info
, int s_first
, int s_last
)
420 volatile FLASH_WORD_SIZE
*addr
= (FLASH_WORD_SIZE
*) (info
->start
[0]);
421 volatile FLASH_WORD_SIZE
*addr2
;
422 int flag
, prot
, sect
, l_sect
;
425 if ((s_first
< 0) || (s_first
> s_last
)) {
426 if (info
->flash_id
== FLASH_UNKNOWN
) {
427 printf ("- missing\n");
429 printf ("- no sectors to erase\n");
434 if (info
->flash_id
== FLASH_UNKNOWN
) {
435 printf ("Can't erase unknown flash type - aborted\n");
440 for (sect
= s_first
; sect
<= s_last
; ++sect
) {
441 if (info
->protect
[sect
]) {
447 printf ("- Warning: %d protected sectors will not be erased!\n", prot
);
454 /* Disable interrupts which might cause a timeout here */
455 flag
= disable_interrupts ();
457 /* Start erase on unprotected sectors */
458 for (sect
= s_first
; sect
<= s_last
; sect
++) {
459 if (info
->protect
[sect
] == 0) { /* not protected */
460 addr2
= (FLASH_WORD_SIZE
*) (info
->start
[sect
]);
461 printf ("Erasing sector %p\n", addr2
); /* CLH */
463 if ((info
->flash_id
& FLASH_VENDMASK
) ==
465 addr
[ADDR0
] = (FLASH_WORD_SIZE
) 0x00AA00AA;
466 addr
[ADDR1
] = (FLASH_WORD_SIZE
) 0x00550055;
467 addr
[ADDR0
] = (FLASH_WORD_SIZE
) 0x00800080;
468 addr
[ADDR0
] = (FLASH_WORD_SIZE
) 0x00AA00AA;
469 addr
[ADDR1
] = (FLASH_WORD_SIZE
) 0x00550055;
470 addr2
[0] = (FLASH_WORD_SIZE
) 0x00500050; /* block erase */
471 for (i
= 0; i
< 50; i
++)
472 udelay (1000); /* wait 1 ms */
474 addr
[ADDR0
] = (FLASH_WORD_SIZE
) 0x00AA00AA;
475 addr
[ADDR1
] = (FLASH_WORD_SIZE
) 0x00550055;
476 addr
[ADDR0
] = (FLASH_WORD_SIZE
) 0x00800080;
477 addr
[ADDR0
] = (FLASH_WORD_SIZE
) 0x00AA00AA;
478 addr
[ADDR1
] = (FLASH_WORD_SIZE
) 0x00550055;
479 addr2
[0] = (FLASH_WORD_SIZE
) 0x00300030; /* sector erase */
483 * Wait for each sector to complete, it's more
484 * reliable. According to AMD Spec, you must
485 * issue all erase commands within a specified
486 * timeout. This has been seen to fail, especially
487 * if printf()s are included (for debug)!!
489 wait_for_DQ7 (info
, sect
);
493 /* re-enable interrupts if necessary */
495 enable_interrupts ();
497 /* wait at least 80us - let's wait 1 ms */
502 * We wait for the last triggered sector
506 wait_for_DQ7 (info
, l_sect
);
510 /* reset to read mode */
511 addr
= (FLASH_WORD_SIZE
*) info
->start
[0];
512 addr
[0] = (FLASH_WORD_SIZE
) 0x00F000F0; /* reset bank */
518 /*-----------------------------------------------------------------------
519 * Copy memory to flash, returns:
522 * 2 - Flash not erased
525 int write_buff (flash_info_t
* info
, uchar
* src
, ulong addr
, ulong cnt
)
530 wp
= (addr
& ~3); /* get lower word aligned address */
533 * handle unaligned start bytes
535 if ((l
= addr
- wp
) != 0) {
537 for (i
= 0, cp
= wp
; i
< l
; ++i
, ++cp
) {
538 data
= (data
<< 8) | (*(uchar
*) cp
);
540 for (; i
< 4 && cnt
> 0; ++i
) {
541 data
= (data
<< 8) | *src
++;
545 for (; cnt
== 0 && i
< 4; ++i
, ++cp
) {
546 data
= (data
<< 8) | (*(uchar
*) cp
);
549 if ((rc
= write_word (info
, wp
, data
)) != 0) {
556 * handle word aligned part
560 for (i
= 0; i
< 4; ++i
) {
561 data
= (data
<< 8) | *src
++;
563 if ((rc
= write_word (info
, wp
, data
)) != 0) {
575 * handle unaligned tail bytes
578 for (i
= 0, cp
= wp
; i
< 4 && cnt
> 0; ++i
, ++cp
) {
579 data
= (data
<< 8) | *src
++;
582 for (; i
< 4; ++i
, ++cp
) {
583 data
= (data
<< 8) | (*(uchar
*) cp
);
586 return (write_word (info
, wp
, data
));
589 /*-----------------------------------------------------------------------
590 * Write a word to Flash, returns:
593 * 2 - Flash not erased
595 static int write_word (flash_info_t
* info
, ulong dest
, ulong data
)
597 volatile FLASH_WORD_SIZE
*addr2
=
598 (FLASH_WORD_SIZE
*) (info
->start
[0]);
599 volatile FLASH_WORD_SIZE
*dest2
= (FLASH_WORD_SIZE
*) dest
;
600 volatile FLASH_WORD_SIZE
*data2
= (FLASH_WORD_SIZE
*) & data
;
604 /* Check if Flash is (sufficiently) erased */
605 if ((*((volatile FLASH_WORD_SIZE
*) dest
) &
606 (FLASH_WORD_SIZE
) data
) != (FLASH_WORD_SIZE
) data
) {
610 for (i
= 0; i
< 4 / sizeof (FLASH_WORD_SIZE
); i
++) {
613 /* Disable interrupts which might cause a timeout here */
614 flag
= disable_interrupts ();
616 addr2
[ADDR0
] = (FLASH_WORD_SIZE
) 0x00AA00AA;
617 addr2
[ADDR1
] = (FLASH_WORD_SIZE
) 0x00550055;
618 addr2
[ADDR0
] = (FLASH_WORD_SIZE
) 0x00A000A0;
622 /* re-enable interrupts if necessary */
624 enable_interrupts ();
626 /* data polling for D7 */
627 start
= get_timer (0);
628 while ((dest2
[i
] & (FLASH_WORD_SIZE
) 0x00800080) !=
629 (data2
[i
] & (FLASH_WORD_SIZE
) 0x00800080)) {
631 if (get_timer (start
) > CFG_FLASH_WRITE_TOUT
) {