2 * (C) Copyright 2002-2004
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * (C) Copyright 2002 Jun Gu <jung@artesyncp.com>
6 * Add support for Am29F016D and dynamic switch setting.
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,
29 * Wait for completion of each sector erase command issued
31 * Chris Hallinan - DS4.COM, Inc. - clh@net1plus.com
35 * Ported to XPedite1000, 1/2 mb boot flash only
36 * Travis B. Sawyer, <travis.sawyer@sandburst.com>
41 #include <asm/processor.h>
46 #define DEBUGF(x...) printf(x)
51 #define BOOT_SMALL_FLASH 32 /* 00100000 */
52 #define FLASH_ONBD_N 2 /* 00000010 */
53 #define FLASH_SRAM_SEL 1 /* 00000001 */
55 #define BOOT_SMALL_FLASH_VAL 4
56 #define FLASH_ONBD_N_VAL 2
57 #define FLASH_SRAM_SEL_VAL 1
60 flash_info_t flash_info
[CONFIG_SYS_MAX_FLASH_BANKS
]; /* info for FLASH chips */
62 static unsigned long flash_addr_table
[8][CONFIG_SYS_MAX_FLASH_BANKS
] = {
63 {0xfff80000}, /* 0:000: configuraton 3 */
64 {0xfff90000}, /* 1:001: configuraton 4 */
65 {0xfffa0000}, /* 2:010: configuraton 7 */
66 {0xfffb0000}, /* 3:011: configuraton 8 */
67 {0xfffc0000}, /* 4:100: configuraton 1 */
68 {0xfffd0000}, /* 5:101: configuraton 2 */
69 {0xfffe0000}, /* 6:110: configuraton 5 */
70 {0xffff0000} /* 7:111: configuraton 6 */
73 /*-----------------------------------------------------------------------
76 static ulong
flash_get_size (vu_long
*addr
, flash_info_t
*info
);
77 static int write_word (flash_info_t
*info
, ulong dest
, ulong data
);
80 #ifdef CONFIG_XPEDITE1K
83 #define FLASH_WORD_SIZE unsigned char
86 /*-----------------------------------------------------------------------
89 unsigned long flash_init (void)
91 unsigned long total_b
= 0;
92 unsigned long size_b
[CONFIG_SYS_MAX_FLASH_BANKS
];
93 unsigned short index
= 0;
98 DEBUGF("FLASH: Index: %d\n", index
);
100 /* Init: no FLASHes known */
101 for (i
=0; i
<CONFIG_SYS_MAX_FLASH_BANKS
; ++i
) {
102 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
103 flash_info
[i
].sector_count
= -1;
104 flash_info
[i
].size
= 0;
106 /* check whether the address is 0 */
107 if (flash_addr_table
[index
][i
] == 0) {
111 /* call flash_get_size() to initialize sector address */
112 size_b
[i
] = flash_get_size(
113 (vu_long
*)flash_addr_table
[index
][i
], &flash_info
[i
]);
114 flash_info
[i
].size
= size_b
[i
];
115 if (flash_info
[i
].flash_id
== FLASH_UNKNOWN
) {
116 printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",
117 i
, size_b
[i
], size_b
[i
]<<20);
118 flash_info
[i
].sector_count
= -1;
119 flash_info
[i
].size
= 0;
122 total_b
+= flash_info
[i
].size
;
129 /*-----------------------------------------------------------------------
131 void flash_print_info (flash_info_t
*info
)
137 volatile unsigned long *flash
;
139 if (info
->flash_id
== FLASH_UNKNOWN
) {
140 printf ("missing or unknown FLASH type\n");
144 switch (info
->flash_id
& FLASH_VENDMASK
) {
145 case FLASH_MAN_AMD
: printf ("AMD "); break;
146 case FLASH_MAN_FUJ
: printf ("FUJITSU "); break;
147 case FLASH_MAN_SST
: printf ("SST "); break;
148 default: printf ("Unknown Vendor "); break;
151 switch (info
->flash_id
& FLASH_TYPEMASK
) {
152 case FLASH_AMD016
: printf ("AM29F016D (16 Mbit, uniform sector size)\n");
154 case FLASH_AM040
: printf ("AM29F040 (512 Kbit, uniform sector size)\n");
156 case FLASH_AM400B
: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
158 case FLASH_AM400T
: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
160 case FLASH_AM800B
: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
162 case FLASH_AM800T
: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
164 case FLASH_AM160B
: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
166 case FLASH_AM160T
: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
168 case FLASH_AM320B
: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
170 case FLASH_AM320T
: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
172 case FLASH_SST800A
: printf ("SST39LF/VF800 (8 Mbit, uniform sector size)\n");
174 case FLASH_SST160A
: printf ("SST39LF/VF160 (16 Mbit, uniform sector size)\n");
176 default: printf ("Unknown Chip Type\n");
180 printf (" Size: %ld KB in %d Sectors\n",
181 info
->size
>> 10, info
->sector_count
);
183 printf (" Sector Start Addresses:");
184 for (i
=0; i
<info
->sector_count
; ++i
) {
186 * Check if whole sector is erased
188 if (i
!= (info
->sector_count
-1))
189 size
= info
->start
[i
+1] - info
->start
[i
];
191 size
= info
->start
[0] + info
->size
- info
->start
[i
];
193 flash
= (volatile unsigned long *)info
->start
[i
];
194 size
= size
>> 2; /* divide by 4 for longword access */
195 for (k
=0; k
<size
; k
++)
197 if (*flash
++ != 0xffffffff)
206 printf (" %08lX%s%s",
209 info
->protect
[i
] ? "RO " : " "
216 /*-----------------------------------------------------------------------
220 /*-----------------------------------------------------------------------
224 * The following code cannot be run from FLASH!
226 static ulong
flash_get_size (vu_long
*addr
, flash_info_t
*info
)
229 FLASH_WORD_SIZE value
;
230 ulong base
= (ulong
)addr
;
231 volatile FLASH_WORD_SIZE
*addr2
= (FLASH_WORD_SIZE
*)addr
;
233 DEBUGF("FLASH ADDR: %08x\n", (unsigned)addr
);
235 /* Write auto select command: read Manufacturer ID */
237 addr2
[ADDR0
] = (FLASH_WORD_SIZE
)0x00AA00AA;
239 addr2
[ADDR1
] = (FLASH_WORD_SIZE
)0x00550055;
241 addr2
[ADDR0
] = (FLASH_WORD_SIZE
)0x00900090;
250 DEBUGF("FLASH MANUFACT: %x\n", value
);
253 case (FLASH_WORD_SIZE
)AMD_MANUFACT
:
254 info
->flash_id
= FLASH_MAN_AMD
;
256 case (FLASH_WORD_SIZE
)FUJ_MANUFACT
:
257 info
->flash_id
= FLASH_MAN_FUJ
;
259 case (FLASH_WORD_SIZE
)SST_MANUFACT
:
260 info
->flash_id
= FLASH_MAN_SST
;
262 case (FLASH_WORD_SIZE
)STM_MANUFACT
:
263 info
->flash_id
= FLASH_MAN_STM
;
266 info
->flash_id
= FLASH_UNKNOWN
;
267 info
->sector_count
= 0;
269 return (0); /* no or unknown flash */
273 value
= addr2
[0]; /* device ID */
274 debug ("\ndev_code=%x\n", value
);
276 value
= addr2
[1]; /* device ID */
279 DEBUGF("\nFLASH DEVICEID: %x\n", value
);
282 case (FLASH_WORD_SIZE
)AMD_ID_LV040B
:
283 info
->flash_id
+= FLASH_AM040
;
284 info
->sector_count
= 8;
285 info
->size
= 0x00080000; /* => 512 kb */
289 info
->flash_id
= FLASH_UNKNOWN
;
290 return (0); /* => no or unknown flash */
294 /* set up sector start address table */
295 if (((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_SST
) ||
296 (info
->flash_id
== FLASH_AM040
) ||
297 (info
->flash_id
== FLASH_AMD016
)) {
298 for (i
= 0; i
< info
->sector_count
; i
++)
299 info
->start
[i
] = base
+ (i
* 0x00010000);
301 if (info
->flash_id
& FLASH_BTYPE
) {
302 /* set sector offsets for bottom boot block type */
303 info
->start
[0] = base
+ 0x00000000;
304 info
->start
[1] = base
+ 0x00004000;
305 info
->start
[2] = base
+ 0x00006000;
306 info
->start
[3] = base
+ 0x00008000;
307 for (i
= 4; i
< info
->sector_count
; i
++) {
308 info
->start
[i
] = base
+ (i
* 0x00010000) - 0x00030000;
311 /* set sector offsets for top boot block type */
312 i
= info
->sector_count
- 1;
313 info
->start
[i
--] = base
+ info
->size
- 0x00004000;
314 info
->start
[i
--] = base
+ info
->size
- 0x00006000;
315 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
316 for (; i
>= 0; i
--) {
317 info
->start
[i
] = base
+ i
* 0x00010000;
322 /* check for protected sectors */
323 for (i
= 0; i
< info
->sector_count
; i
++) {
324 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
325 /* D0 = 1 if protected */
327 addr2
= (volatile FLASH_WORD_SIZE
*)(info
->start
[i
]);
328 info
->protect
[i
] = addr2
[4] & 1;
330 addr2
= (volatile FLASH_WORD_SIZE
*)(info
->start
[i
]);
331 if ((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_SST
)
332 info
->protect
[i
] = 0;
334 info
->protect
[i
] = addr2
[2] & 1;
339 * Prevent writes to uninitialized FLASH.
341 if (info
->flash_id
!= FLASH_UNKNOWN
) {
342 #if 0 /* test-only */
344 addr2
= (volatile unsigned char *)info
->start
[0];
347 addr2
[ADDR0
] = 0xF0; /* reset bank */
349 addr2
= (FLASH_WORD_SIZE
*)info
->start
[0];
350 *addr2
= (FLASH_WORD_SIZE
)0x00F000F0; /* reset bank */
352 #else /* test-only */
353 addr2
= (FLASH_WORD_SIZE
*)info
->start
[0];
354 *addr2
= (FLASH_WORD_SIZE
)0x00F000F0; /* reset bank */
355 #endif /* test-only */
361 int wait_for_DQ7(flash_info_t
*info
, int sect
)
363 ulong start
, now
, last
;
364 volatile FLASH_WORD_SIZE
*addr
= (FLASH_WORD_SIZE
*)(info
->start
[sect
]);
366 start
= get_timer (0);
368 while ((addr
[0] & (FLASH_WORD_SIZE
)0x00800080) != (FLASH_WORD_SIZE
)0x00800080) {
369 if ((now
= get_timer(start
)) > CONFIG_SYS_FLASH_ERASE_TOUT
) {
370 printf ("Timeout\n");
373 /* show that we're waiting */
374 if ((now
- last
) > 1000) { /* every second */
382 /*-----------------------------------------------------------------------
385 int flash_erase (flash_info_t
*info
, int s_first
, int s_last
)
387 volatile FLASH_WORD_SIZE
*addr
= (FLASH_WORD_SIZE
*)(info
->start
[0]);
388 volatile FLASH_WORD_SIZE
*addr2
;
389 int flag
, prot
, sect
, l_sect
;
392 if ((s_first
< 0) || (s_first
> s_last
)) {
393 if (info
->flash_id
== FLASH_UNKNOWN
) {
394 printf ("- missing\n");
396 printf ("- no sectors to erase\n");
401 if (info
->flash_id
== FLASH_UNKNOWN
) {
402 printf ("Can't erase unknown flash type - aborted\n");
407 for (sect
=s_first
; sect
<=s_last
; ++sect
) {
408 if (info
->protect
[sect
]) {
414 printf ("- Warning: %d protected sectors will not be erased!\n",
422 /* Disable interrupts which might cause a timeout here */
423 flag
= disable_interrupts();
425 /* Start erase on unprotected sectors */
426 for (sect
= s_first
; sect
<=s_last
; sect
++) {
427 if (info
->protect
[sect
] == 0) { /* not protected */
428 addr2
= (FLASH_WORD_SIZE
*)(info
->start
[sect
]);
429 printf("Erasing sector %p\n", addr2
);
431 if ((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_SST
) {
432 addr
[ADDR0
] = (FLASH_WORD_SIZE
)0x00AA00AA;
433 addr
[ADDR1
] = (FLASH_WORD_SIZE
)0x00550055;
434 addr
[ADDR0
] = (FLASH_WORD_SIZE
)0x00800080;
435 addr
[ADDR0
] = (FLASH_WORD_SIZE
)0x00AA00AA;
436 addr
[ADDR1
] = (FLASH_WORD_SIZE
)0x00550055;
437 addr2
[0] = (FLASH_WORD_SIZE
)0x00500050; /* block erase */
439 udelay(1000); /* wait 1 ms */
441 addr
[ADDR0
] = (FLASH_WORD_SIZE
)0x00AA00AA;
442 addr
[ADDR1
] = (FLASH_WORD_SIZE
)0x00550055;
443 addr
[ADDR0
] = (FLASH_WORD_SIZE
)0x00800080;
444 addr
[ADDR0
] = (FLASH_WORD_SIZE
)0x00AA00AA;
445 addr
[ADDR1
] = (FLASH_WORD_SIZE
)0x00550055;
446 addr2
[0] = (FLASH_WORD_SIZE
)0x00300030; /* sector erase */
450 * Wait for each sector to complete, it's more
451 * reliable. According to AMD Spec, you must
452 * issue all erase commands within a specified
453 * timeout. This has been seen to fail, especially
454 * if printf()s are included (for debug)!!
456 wait_for_DQ7(info
, sect
);
460 /* re-enable interrupts if necessary */
464 /* wait at least 80us - let's wait 1 ms */
469 * We wait for the last triggered sector
473 wait_for_DQ7(info
, l_sect
);
477 /* reset to read mode */
478 addr
= (FLASH_WORD_SIZE
*)info
->start
[0];
479 addr
[0] = (FLASH_WORD_SIZE
)0x00F000F0; /* reset bank */
485 /*-----------------------------------------------------------------------
486 * Copy memory to flash, returns:
489 * 2 - Flash not erased
492 int write_buff (flash_info_t
*info
, uchar
*src
, ulong addr
, ulong cnt
)
497 wp
= (addr
& ~3); /* get lower word aligned address */
500 * handle unaligned start bytes
502 if ((l
= addr
- wp
) != 0) {
504 for (i
=0, cp
=wp
; i
<l
; ++i
, ++cp
) {
505 data
= (data
<< 8) | (*(uchar
*)cp
);
507 for (; i
<4 && cnt
>0; ++i
) {
508 data
= (data
<< 8) | *src
++;
512 for (; cnt
==0 && i
<4; ++i
, ++cp
) {
513 data
= (data
<< 8) | (*(uchar
*)cp
);
516 if ((rc
= write_word(info
, wp
, data
)) != 0) {
523 * handle word aligned part
527 for (i
=0; i
<4; ++i
) {
528 data
= (data
<< 8) | *src
++;
530 if ((rc
= write_word(info
, wp
, data
)) != 0) {
542 * handle unaligned tail bytes
545 for (i
=0, cp
=wp
; i
<4 && cnt
>0; ++i
, ++cp
) {
546 data
= (data
<< 8) | *src
++;
549 for (; i
<4; ++i
, ++cp
) {
550 data
= (data
<< 8) | (*(uchar
*)cp
);
553 return (write_word(info
, wp
, data
));
556 /*-----------------------------------------------------------------------
557 * Write a word to Flash, returns:
560 * 2 - Flash not erased
562 static int write_word (flash_info_t
* info
, ulong dest
, ulong data
)
564 volatile FLASH_WORD_SIZE
*addr2
= (FLASH_WORD_SIZE
*) (info
->start
[0]);
565 volatile FLASH_WORD_SIZE
*dest2
= (FLASH_WORD_SIZE
*) dest
;
566 volatile FLASH_WORD_SIZE
*data2
= (FLASH_WORD_SIZE
*) & data
;
570 /* Check if Flash is (sufficiently) erased */
571 if ((*((volatile FLASH_WORD_SIZE
*) dest
) &
572 (FLASH_WORD_SIZE
) data
) != (FLASH_WORD_SIZE
) data
) {
576 for (i
= 0; i
< 4 / sizeof (FLASH_WORD_SIZE
); i
++) {
579 /* Disable interrupts which might cause a timeout here */
580 flag
= disable_interrupts ();
582 addr2
[ADDR0
] = (FLASH_WORD_SIZE
) 0x00AA00AA;
583 addr2
[ADDR1
] = (FLASH_WORD_SIZE
) 0x00550055;
584 addr2
[ADDR0
] = (FLASH_WORD_SIZE
) 0x00A000A0;
588 /* re-enable interrupts if necessary */
590 enable_interrupts ();
592 /* data polling for D7 */
593 start
= get_timer (0);
594 while ((dest2
[i
] & (FLASH_WORD_SIZE
) 0x00800080) !=
595 (data2
[i
] & (FLASH_WORD_SIZE
) 0x00800080)) {
597 if (get_timer (start
) > CONFIG_SYS_FLASH_WRITE_TOUT
) {
606 /*-----------------------------------------------------------------------