3 * Josh Huber <huber@mclx.com>, Mission Critical Linux, Inc.
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
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,
32 /*---------------------------------------------------------------------*/
36 #define DEBUGF(fmt,args...) printf(fmt ,##args)
38 #define DEBUGF(fmt,args...)
40 /*---------------------------------------------------------------------*/
42 flash_info_t flash_info
[];
44 static ulong
flash_get_size (ulong addr
, flash_info_t
*info
);
45 static int flash_get_offsets (ulong base
, flash_info_t
*info
);
46 static int write_word (flash_info_t
*info
, ulong dest
, ulong data
);
47 static void flash_reset (ulong addr
);
51 static void flash_to_xd(void)
57 if (flash_xd_nest
== 1)
59 DEBUGF("Flash on XD\n");
60 x
= pci_read_cfg_byte(0, 0, 0x74);
61 pci_write_cfg_byte(0, 0, 0x74, x
|1);
65 static void flash_to_mem(void)
71 if (flash_xd_nest
== 0)
73 DEBUGF("Flash on memory bus\n");
74 x
= pci_read_cfg_byte(0, 0, 0x74);
75 pci_write_cfg_byte(0, 0, 0x74, x
&0xFE);
79 unsigned long flash_init_old(void)
83 for (i
= 0; i
< CFG_MAX_FLASH_BANKS
; i
++)
85 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
86 flash_info
[i
].sector_count
= 0;
87 flash_info
[i
].size
= 0;
94 unsigned long flash_init (void)
97 unsigned long flash_size
= 0;
103 /* Init: no FLASHes known */
104 for (i
=0; i
<CFG_MAX_FLASH_BANKS
; ++i
) {
105 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
106 flash_info
[i
].sector_count
= 0;
107 flash_info
[i
].size
= 0;
110 DEBUGF("\n## Get flash size @ 0x%08x\n", CFG_FLASH_BASE
);
112 flash_size
= flash_get_size (CFG_FLASH_BASE
, flash_info
);
114 DEBUGF("## Flash bank size: %08lx\n", flash_size
);
117 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE && \
118 CFG_MONITOR_BASE < CFG_FLASH_BASE + CFG_FLASH_MAX_SIZE
119 /* monitor protection ON by default */
120 flash_protect(FLAG_PROTECT_SET
,
122 CFG_MONITOR_BASE
+ CFG_MONITOR_LEN
- 1,
126 #ifdef CFG_ENV_IS_IN_FLASH
127 /* ENV protection ON by default */
128 flash_protect(FLAG_PROTECT_SET
,
130 CFG_ENV_ADDR
+ CFG_ENV_SECT_SIZE
- 1,
135 printf ("Warning: the BOOT Flash is not initialised !");
144 * The following code cannot be run from FLASH!
146 static ulong
flash_get_size (ulong addr
, flash_info_t
*info
)
150 uchar
*x
= (uchar
*)addr
;
154 /* Write auto select command: read Manufacturer ID */
156 __asm
volatile ("sync\n eieio");
158 __asm
volatile ("sync\n eieio");
160 __asm
volatile ("sync\n eieio");
163 __asm
volatile ("sync\n eieio");
165 DEBUGF("Manuf. ID @ 0x%08lx: 0x%08x\n", (ulong
)addr
, value
);
167 switch (value
| (value
<< 16)) {
169 info
->flash_id
= FLASH_MAN_AMD
;
173 info
->flash_id
= FLASH_MAN_FUJ
;
177 info
->flash_id
= FLASH_MAN_STM
;
181 info
->flash_id
= FLASH_UNKNOWN
;
182 info
->sector_count
= 0;
189 __asm
volatile ("sync\n eieio");
191 DEBUGF("Device ID @ 0x%08lx: 0x%08x\n", addr
+1, value
);
195 DEBUGF("Am29F040B\n");
196 info
->flash_id
+= FLASH_AM040
;
197 info
->sector_count
= 8;
198 info
->size
= 0x00080000;
199 break; /* => 512 kB */
202 DEBUGF("Am29LV040B\n");
203 info
->flash_id
+= FLASH_AM040
;
204 info
->sector_count
= 8;
205 info
->size
= 0x00080000;
206 break; /* => 512 kB */
209 DEBUGF("Am29LV400T\n");
210 info
->flash_id
+= FLASH_AM400T
;
211 info
->sector_count
= 11;
212 info
->size
= 0x00100000;
216 DEBUGF("Am29LV400B\n");
217 info
->flash_id
+= FLASH_AM400B
;
218 info
->sector_count
= 11;
219 info
->size
= 0x00100000;
223 DEBUGF("Am29LV800T\n");
224 info
->flash_id
+= FLASH_AM800T
;
225 info
->sector_count
= 19;
226 info
->size
= 0x00200000;
230 DEBUGF("Am29LV400B\n");
231 info
->flash_id
+= FLASH_AM800B
;
232 info
->sector_count
= 19;
233 info
->size
= 0x00200000;
237 DEBUGF("Am29LV160T\n");
238 info
->flash_id
+= FLASH_AM160T
;
239 info
->sector_count
= 35;
240 info
->size
= 0x00400000;
244 DEBUGF("Am29LV160B\n");
245 info
->flash_id
+= FLASH_AM160B
;
246 info
->sector_count
= 35;
247 info
->size
= 0x00400000;
251 DEBUGF("Am29LV320T\n");
252 info
->flash_id
+= FLASH_AM320T
;
253 info
->sector_count
= 67;
254 info
->size
= 0x00800000;
258 /* Has the same ID as AMD_ID_LV320T, to be fixed */
260 DEBUGF("Am29LV320B\n");
261 info
->flash_id
+= FLASH_AM320B
;
262 info
->sector_count
= 67;
263 info
->size
= 0x00800000;
268 DEBUGF("Am29LV033C\n");
269 info
->flash_id
+= FLASH_AM033C
;
270 info
->sector_count
= 64;
271 info
->size
= 0x01000000;
275 DEBUGF("M29F040B\n");
276 info
->flash_id
+= FLASH_AM040
;
277 info
->sector_count
= 8;
278 info
->size
= 0x00080000;
279 break; /* => 512 kB */
282 info
->flash_id
= FLASH_UNKNOWN
;
285 return (0); /* => no or unknown flash */
289 if (info
->sector_count
> CFG_MAX_FLASH_SECT
) {
290 printf ("** ERROR: sector count %d > max (%d) **\n",
291 info
->sector_count
, CFG_MAX_FLASH_SECT
);
292 info
->sector_count
= CFG_MAX_FLASH_SECT
;
295 if (! flash_get_offsets (addr
, info
)) {
301 /* check for protected sectors */
302 for (i
= 0; i
< info
->sector_count
; i
++) {
303 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
304 /* D0 = 1 if protected */
305 value
= in8(info
->start
[i
] + 2);
307 info
->protect
[i
] = (value
& 1) != 0;
311 * Reset bank to read mode
320 static int flash_get_offsets (ulong base
, flash_info_t
*info
)
324 switch (info
->flash_id
& FLASH_TYPEMASK
) {
326 /* set sector offsets for uniform sector type */
327 for (i
= 0; i
< info
->sector_count
; i
++) {
328 info
->start
[i
] = base
+ i
* info
->size
/
339 int flash_erase (flash_info_t
*info
, int s_first
, int s_last
)
341 volatile ulong addr
= info
->start
[0];
342 int flag
, prot
, sect
, l_sect
;
343 ulong start
, now
, last
;
347 if (s_first
< 0 || s_first
> s_last
) {
348 if (info
->flash_id
== FLASH_UNKNOWN
) {
349 printf ("- missing\n");
351 printf ("- no sectors to erase\n");
357 if (info
->flash_id
== FLASH_UNKNOWN
) {
358 printf ("Can't erase unknown flash type %08lx - aborted\n",
365 for (sect
=s_first
; sect
<=s_last
; ++sect
) {
366 if (info
->protect
[sect
]) {
372 printf ("- Warning: %d protected sectors will not be erased!\n",
380 /* Disable interrupts which might cause a timeout here */
381 flag
= disable_interrupts();
383 out8(addr
+ 0x555, 0xAA);
385 out8(addr
+ 0x2AA, 0x55);
387 out8(addr
+ 0x555, 0x80);
389 out8(addr
+ 0x555, 0xAA);
391 out8(addr
+ 0x2AA, 0x55);
394 /* Start erase on unprotected sectors */
395 for (sect
= s_first
; sect
<=s_last
; sect
++) {
396 if (info
->protect
[sect
] == 0) { /* not protected */
397 addr
= info
->start
[sect
];
404 /* re-enable interrupts if necessary */
408 /* wait at least 80us - let's wait 1 ms */
412 * We wait for the last triggered sector
417 start
= get_timer (0);
419 addr
= info
->start
[l_sect
];
421 DEBUGF ("Start erase timeout: %d\n", CFG_FLASH_ERASE_TOUT
);
423 while ((in8(addr
) & 0x80) != 0x80) {
424 if ((now
= get_timer(start
)) > CFG_FLASH_ERASE_TOUT
) {
425 printf ("Timeout\n");
426 flash_reset (info
->start
[0]);
430 /* show that we're waiting */
431 if ((now
- last
) > 1000) { /* every second */
439 /* reset to read mode */
440 flash_reset (info
->start
[0]);
448 * Copy memory to flash, returns:
451 * 2 - Flash not erased
453 int write_buff (flash_info_t
*info
, uchar
*src
, ulong addr
, ulong cnt
)
461 wp
= (addr
& ~3); /* get lower word aligned address */
464 * handle unaligned start bytes
466 if ((l
= addr
- wp
) != 0) {
468 for (i
=0, cp
=wp
; i
<l
; ++i
, ++cp
) {
469 data
= (data
<< 8) | (*(uchar
*)cp
);
471 for (; i
<4 && cnt
>0; ++i
) {
472 data
= (data
<< 8) | *src
++;
476 for (; cnt
==0 && i
<4; ++i
, ++cp
) {
477 data
= (data
<< 8) | (*(uchar
*)cp
);
480 if ((rc
= write_word(info
, wp
, data
)) != 0) {
490 * handle word aligned part
499 for (i
=0; i
<4; ++i
) {
500 data
= (data
<< 8) | *src
++;
502 if ((rc
= write_word(info
, wp
, data
)) != 0) {
517 * handle unaligned tail bytes
520 for (i
=0, cp
=wp
; i
<4 && cnt
>0; ++i
, ++cp
) {
521 data
= (data
<< 8) | *src
++;
524 for (; i
<4; ++i
, ++cp
) {
525 data
= (data
<< 8) | (*(uchar
*)cp
);
529 return (write_word(info
, wp
, data
));
533 * Write a word to Flash, returns:
536 * 2 - Flash not erased
538 static int write_word (flash_info_t
*info
, ulong dest
, ulong data
)
540 volatile ulong addr
= info
->start
[0];
546 /* Check if Flash is (sufficiently) erased */
547 if ((in32(dest
) & data
) != data
) {
552 /* write each byte out */
553 for (i
= 0; i
< 4; i
++) {
554 char *data_ch
= (char *)&data
;
555 int flag
= disable_interrupts();
557 out8(addr
+ 0x555, 0xAA);
559 out8(addr
+ 0x2AA, 0x55);
561 out8(addr
+ 0x555, 0xA0);
563 out8(dest
+i
, data_ch
[i
]);
566 /* re-enable interrupts if necessary */
570 /* data polling for D7 */
571 start
= get_timer (0);
572 while ((in8(dest
+i
) & 0x80) != (data_ch
[i
] & 0x80)) {
573 if (get_timer(start
) > CFG_FLASH_WRITE_TOUT
) {
588 * Reset bank to read mode
590 static void flash_reset (ulong addr
)
593 out8(addr
, 0xF0); /* reset bank */
598 void flash_print_info (flash_info_t
*info
)
602 if (info
->flash_id
== FLASH_UNKNOWN
) {
603 printf ("missing or unknown FLASH type\n");
607 switch (info
->flash_id
& FLASH_VENDMASK
) {
608 case FLASH_MAN_AMD
: printf ("AMD "); break;
609 case FLASH_MAN_FUJ
: printf ("FUJITSU "); break;
610 case FLASH_MAN_BM
: printf ("BRIGHT MICRO "); break;
611 case FLASH_MAN_STM
: printf ("SGS THOMSON "); break;
612 default: printf ("Unknown Vendor "); break;
615 switch (info
->flash_id
& FLASH_TYPEMASK
) {
616 case FLASH_AM040
: printf ("29F040 or 29LV040 (4 Mbit, uniform sectors)\n");
618 case FLASH_AM400B
: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
620 case FLASH_AM400T
: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
622 case FLASH_AM800B
: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
624 case FLASH_AM800T
: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
626 case FLASH_AM160B
: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
628 case FLASH_AM160T
: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
630 case FLASH_AM320B
: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
632 case FLASH_AM320T
: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
634 default: printf ("Unknown Chip Type\n");
638 if (info
->size
% 0x100000 == 0) {
639 printf (" Size: %ld MB in %d Sectors\n",
640 info
->size
/ 0x100000, info
->sector_count
);
641 } else if (info
->size
% 0x400 == 0) {
642 printf (" Size: %ld KB in %d Sectors\n",
643 info
->size
/ 0x400, info
->sector_count
);
645 printf (" Size: %ld B in %d Sectors\n",
646 info
->size
, info
->sector_count
);
649 printf (" Sector Start Addresses:");
650 for (i
=0; i
<info
->sector_count
; ++i
) {
655 info
->protect
[i
] ? " (RO)" : " "