2 * (C) Copyright 2001-2005
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * Flash Routines for Intel devices
7 *--------------------------------------------------------------------
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,
31 flash_info_t flash_info
[CFG_MAX_FLASH_BANKS
];
33 /*-----------------------------------------------------------------------
35 ulong
flash_int_get_size (volatile unsigned long *baseaddr
,
39 unsigned long flashtest_h
, flashtest_l
;
41 info
->sector_count
= info
->size
= 0;
42 info
->flash_id
= FLASH_UNKNOWN
;
44 /* Write identify command sequence and test FLASH answer
46 baseaddr
[0] = 0x00900090;
47 baseaddr
[1] = 0x00900090;
49 flashtest_h
= baseaddr
[0]; /* manufacturer ID */
50 flashtest_l
= baseaddr
[1];
52 if (flashtest_h
!= INTEL_MANUFACT
|| flashtest_l
!= INTEL_MANUFACT
)
53 return (0); /* no or unknown flash */
55 flashtest_h
= baseaddr
[2]; /* device ID */
56 flashtest_l
= baseaddr
[3];
58 if (flashtest_h
!= flashtest_l
)
61 switch (flashtest_h
) {
62 case INTEL_ID_28F160C3B
:
63 info
->flash_id
= FLASH_28F160C3B
;
64 info
->sector_count
= 39;
65 info
->size
= 0x00800000; /* 4 * 2 MB = 8 MB */
67 case INTEL_ID_28F160F3B
:
68 info
->flash_id
= FLASH_28F160F3B
;
69 info
->sector_count
= 39;
70 info
->size
= 0x00800000; /* 4 * 2 MB = 8 MB */
72 case INTEL_ID_28F640C3B
:
73 info
->flash_id
= FLASH_28F640C3B
;
74 info
->sector_count
= 135;
75 info
->size
= 0x02000000; /* 16 * 2 MB = 32 MB */
78 return (0); /* no or unknown flash */
81 info
->flash_id
|= INTEL_MANUFACT
<< 16; /* set manufacturer offset */
83 if (info
->flash_id
& FLASH_BTYPE
) {
84 volatile unsigned long *tmp
= baseaddr
;
86 /* set up sector start adress table (bottom sector type)
87 * AND unlock the sectors (if our chip is 160C3)
89 for (i
= 0; i
< info
->sector_count
; i
++) {
90 if (((info
->flash_id
& FLASH_TYPEMASK
) == FLASH_28F160C3B
) ||
91 ((info
->flash_id
& FLASH_TYPEMASK
) == FLASH_28F640C3B
)) {
97 info
->start
[i
] = (uint
) tmp
;
98 tmp
+= i
< 8 ? 0x2000 : 0x10000; /* pointer arith */
102 memset (info
->protect
, 0, info
->sector_count
);
104 baseaddr
[0] = 0x00FF00FF;
105 baseaddr
[1] = 0x00FF00FF;
110 static ulong
flash_amd_get_size (vu_char
*addr
, flash_info_t
*info
)
114 ulong base
= (ulong
)addr
;
116 /* Write auto select command: read Manufacturer ID */
124 devid
= addr
[1] & 0xff;
126 /* only support AMD */
127 if (vendor
!= 0x01) {
134 if (devid
== AMD_ID_F040B
) {
135 info
->flash_id
= vendor
<< 16 | devid
;
136 info
->sector_count
= 8;
137 info
->size
= info
->sector_count
* 0x10000;
139 else if (devid
== AMD_ID_F080B
) {
140 info
->flash_id
= vendor
<< 16 | devid
;
141 info
->sector_count
= 16;
142 info
->size
= 4 * info
->sector_count
* 0x10000;
144 else if (devid
== AMD_ID_F016D
) {
145 info
->flash_id
= vendor
<< 16 | devid
;
146 info
->sector_count
= 32;
147 info
->size
= 4 * info
->sector_count
* 0x10000;
150 printf ("## Unknown Flash Type: %02x\n", devid
);
154 /* check for protected sectors */
155 for (i
= 0; i
< info
->sector_count
; i
++) {
156 /* sector base address */
157 info
->start
[i
] = base
+ i
* (info
->size
/ info
->sector_count
);
158 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
159 /* D0 = 1 if protected */
160 addr
= (volatile unsigned char *)(info
->start
[i
]);
161 info
->protect
[i
] = addr
[2] & 1;
165 * Prevent writes to uninitialized FLASH.
167 if (info
->flash_id
!= FLASH_UNKNOWN
) {
168 addr
= (vu_char
*)info
->start
[0];
169 addr
[0] = 0xF0; /* reset bank */
176 /*-----------------------------------------------------------------------
178 unsigned long flash_init (void)
180 unsigned long size_b0
= 0;
181 unsigned long size_b1
= 0;
184 /* Init: no FLASHes known
186 for (i
= 0; i
< CFG_MAX_FLASH_BANKS
; ++i
) {
187 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
190 /* Disable flash protection */
191 CPU86_BCR
|= (CPU86_BCR_FWPT
| CPU86_BCR_FWRE
);
193 /* Static FLASH Bank configuration here (only one bank) */
195 size_b0
= flash_int_get_size ((ulong
*) CFG_FLASH_BASE
, &flash_info
[0]);
196 size_b1
= flash_amd_get_size ((uchar
*) CFG_BOOTROM_BASE
, &flash_info
[1]);
198 if (size_b0
> 0 || size_b1
> 0) {
204 print_size (size_b0
, (size_b1
> 0) ? ", " : ") ");
209 print_size (size_b1
, ") ");
213 printf ("## No FLASH found.\n");
216 /* protect monitor and environment sectors
219 #if CFG_MONITOR_BASE >= CFG_BOOTROM_BASE
221 /* If U-Boot is booted from ROM the CFG_MONITOR_BASE > CFG_FLASH_BASE
222 * but we shouldn't protect it.
225 flash_protect (FLAG_PROTECT_SET
,
227 CFG_MONITOR_BASE
+ monitor_flash_len
- 1, &flash_info
[1]
231 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
232 flash_protect (FLAG_PROTECT_SET
,
234 CFG_MONITOR_BASE
+ monitor_flash_len
- 1, &flash_info
[0]
239 #if (CFG_ENV_IS_IN_FLASH == 1) && defined(CFG_ENV_ADDR)
240 # ifndef CFG_ENV_SIZE
241 # define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
243 # if CFG_ENV_ADDR >= CFG_BOOTROM_BASE
245 flash_protect (FLAG_PROTECT_SET
,
247 CFG_ENV_ADDR
+ CFG_ENV_SIZE
- 1, &flash_info
[1]);
250 flash_protect (FLAG_PROTECT_SET
,
252 CFG_ENV_ADDR
+ CFG_ENV_SIZE
- 1, &flash_info
[0]);
256 return (size_b0
+ size_b1
);
259 /*-----------------------------------------------------------------------
261 void flash_print_info (flash_info_t
* info
)
265 if (info
->flash_id
== FLASH_UNKNOWN
) {
266 printf ("missing or unknown FLASH type\n");
270 switch ((info
->flash_id
>> 16) & 0xff) {
278 printf ("Unknown Vendor ");
282 switch (info
->flash_id
& FLASH_TYPEMASK
) {
283 case FLASH_28F160C3B
:
284 printf ("28F160C3B (16 Mbit, bottom sector)\n");
286 case FLASH_28F160F3B
:
287 printf ("28F160F3B (16 Mbit, bottom sector)\n");
289 case FLASH_28F640C3B
:
290 printf ("28F640C3B (64 M, bottom sector)\n");
293 printf ("AM29F040B (4 Mbit)\n");
296 printf ("Unknown Chip Type\n");
300 if (info
->size
< 0x100000)
301 printf (" Size: %ld KB in %d Sectors\n",
302 info
->size
>> 10, info
->sector_count
);
304 printf (" Size: %ld MB in %d Sectors\n",
305 info
->size
>> 20, info
->sector_count
);
307 printf (" Sector Start Addresses:");
308 for (i
= 0; i
< info
->sector_count
; ++i
) {
313 info
->protect
[i
] ? " (RO)" : " "
319 /*-----------------------------------------------------------------------
321 int flash_erase (flash_info_t
* info
, int s_first
, int s_last
)
323 vu_char
*addr
= (vu_char
*)(info
->start
[0]);
324 int flag
, prot
, sect
, l_sect
;
325 ulong start
, now
, last
;
327 if ((s_first
< 0) || (s_first
> s_last
)) {
328 if (info
->flash_id
== FLASH_UNKNOWN
) {
329 printf ("- missing\n");
331 printf ("- no sectors to erase\n");
337 for (sect
= s_first
; sect
<= s_last
; sect
++) {
338 if (info
->protect
[sect
])
343 printf ("- Warning: %d protected sectors will not be erased!\n",
349 /* Check the type of erased flash
351 if (info
->flash_id
>> 16 == 0x1) {
356 /* Disable interrupts which might cause a timeout here */
357 flag
= disable_interrupts();
365 /* wait at least 80us - let's wait 1 ms */
368 /* Start erase on unprotected sectors */
369 for (sect
= s_first
; sect
<=s_last
; sect
++) {
370 if (info
->protect
[sect
] == 0) { /* not protected */
371 addr
= (vu_char
*)(info
->start
[sect
]);
377 /* re-enable interrupts if necessary */
381 /* wait at least 80us - let's wait 1 ms */
385 * We wait for the last triggered sector
390 start
= get_timer (0);
392 addr
= (vu_char
*)(info
->start
[l_sect
]);
393 while ((addr
[0] & 0x80) != 0x80) {
394 if ((now
= get_timer(start
)) > CFG_FLASH_ERASE_TOUT
) {
395 printf ("Timeout\n");
398 /* show that we're waiting */
399 if ((now
- last
) > 1000) { /* every second */
406 /* reset to read mode */
407 addr
= (volatile unsigned char *)info
->start
[0];
408 addr
[0] = 0xF0; /* reset bank */
414 /* Start erase on unprotected sectors
416 for (sect
= s_first
; sect
<= s_last
; sect
++) {
417 volatile ulong
*addr
=
418 (volatile unsigned long *) info
->start
[sect
];
420 start
= get_timer (0);
422 if (info
->protect
[sect
] == 0) {
423 /* Disable interrupts which might cause a timeout here
425 flag
= disable_interrupts ();
429 addr
[0] = 0x00200020;
430 addr
[1] = 0x00200020;
431 addr
[0] = 0x00D000D0;
432 addr
[1] = 0x00D000D0;
434 /* re-enable interrupts if necessary
437 enable_interrupts ();
439 /* wait at least 80us - let's wait 1 ms
444 while ((addr
[0] & 0x00800080) != 0x00800080 ||
445 (addr
[1] & 0x00800080) != 0x00800080) {
446 if ((now
= get_timer (start
)) > CFG_FLASH_ERASE_TOUT
) {
447 printf ("Timeout (erase suspended!)\n");
450 addr
[0] = 0x00B000B0;
451 addr
[1] = 0x00B000B0;
454 /* show that we're waiting
456 if ((now
- last
) > 1000) { /* every second */
461 if (addr
[0] & 0x00220022 || addr
[1] & 0x00220022) {
462 printf ("*** ERROR: erase failed!\n");
466 /* Clear status register and reset to read mode
468 addr
[0] = 0x00500050;
469 addr
[1] = 0x00500050;
470 addr
[0] = 0x00FF00FF;
471 addr
[1] = 0x00FF00FF;
481 static int write_word (flash_info_t
*, volatile unsigned long *, ulong
);
482 static int write_byte (flash_info_t
*info
, ulong dest
, uchar data
);
484 /*-----------------------------------------------------------------------
485 * Copy memory to flash, returns:
488 * 2 - Flash not erased
490 int write_buff (flash_info_t
* info
, uchar
* src
, ulong addr
, ulong cnt
)
493 int i
, l
, rc
, cc
= cnt
, res
= 0;
495 if (info
->flash_id
>> 16 == 0x1) {
497 /* Write to AMD 8-bit flash
500 if ((rc
= write_byte(info
, addr
, *src
)) != 0) {
511 /* Write to Intel 64-bit flash
513 for (v
=0; cc
> 0; addr
+= 4, cc
-= 4 - l
) {
517 for (i
= 0; i
< 4; i
++) {
518 v
= (v
<< 8) + (i
< l
|| i
- l
>= cc
?
519 *((unsigned char *) addr
+ i
) : *src
++);
522 if ((res
= write_word (info
, (volatile unsigned long *) addr
, v
)) != 0)
530 /*-----------------------------------------------------------------------
531 * Write a word to Flash, returns:
534 * 2 - Flash not erased
536 static int write_word (flash_info_t
* info
, volatile unsigned long *addr
,
542 /* Check if Flash is (sufficiently) erased
544 if ((*addr
& data
) != data
)
547 /* Disable interrupts which might cause a timeout here
549 flag
= disable_interrupts ();
554 /* re-enable interrupts if necessary
557 enable_interrupts ();
559 start
= get_timer (0);
560 while ((*addr
& 0x00800080) != 0x00800080) {
561 if (get_timer (start
) > CFG_FLASH_WRITE_TOUT
) {
570 if (*addr
& 0x00220022) {
571 printf ("*** ERROR: program failed!\n");
576 /* Clear status register and reset to read mode
584 /*-----------------------------------------------------------------------
585 * Write a byte to Flash, returns:
588 * 2 - Flash not erased
590 static int write_byte (flash_info_t
*info
, ulong dest
, uchar data
)
592 vu_char
*addr
= (vu_char
*)(info
->start
[0]);
596 /* Check if Flash is (sufficiently) erased */
597 if ((*((vu_char
*)dest
) & data
) != data
) {
600 /* Disable interrupts which might cause a timeout here */
601 flag
= disable_interrupts();
607 *((vu_char
*)dest
) = data
;
609 /* re-enable interrupts if necessary */
613 /* data polling for D7 */
614 start
= get_timer (0);
615 while ((*((vu_char
*)dest
) & 0x80) != (data
& 0x80)) {
616 if (get_timer(start
) > CFG_FLASH_WRITE_TOUT
) {
623 /*-----------------------------------------------------------------------