2 * (C) Copyright 2002-2005
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,
27 * Ported from Ebony flash support
29 * Sandburst Corporation
32 #include <asm/ppc4xx.h>
33 #include <asm/processor.h>
38 #define DEBUGF(x...) printf(x)
44 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
46 static unsigned long flash_addr_table[8][CONFIG_SYS_MAX_FLASH_BANKS] = {
47 {0xfff80000} /* Boot Flash */
50 /*-----------------------------------------------------------------------
53 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
54 static int write_word (flash_info_t *info, ulong dest, ulong data);
59 #define FLASH_WORD_SIZE unsigned char
62 /*-----------------------------------------------------------------------
65 unsigned long flash_init (void)
67 unsigned long total_b = 0;
68 unsigned long size_b[CONFIG_SYS_MAX_FLASH_BANKS];
69 unsigned short index = 0;
74 DEBUGF("FLASH: Index: %d\n", index);
76 /* Init: no FLASHes known */
77 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
78 flash_info[i].flash_id = FLASH_UNKNOWN;
79 flash_info[i].sector_count = -1;
80 flash_info[i].size = 0;
82 /* check whether the address is 0 */
83 if (flash_addr_table[index][i] == 0) {
87 /* call flash_get_size() to initialize sector address */
88 size_b[i] = flash_get_size(
89 (vu_long *)flash_addr_table[index][i], &flash_info[i]);
90 flash_info[i].size = size_b[i];
91 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
92 printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",
93 i, size_b[i], size_b[i]<<20);
94 flash_info[i].sector_count = -1;
95 flash_info[i].size = 0;
98 total_b += flash_info[i].size;
105 /*-----------------------------------------------------------------------
107 void flash_print_info (flash_info_t *info)
113 volatile unsigned long *flash;
115 if (info->flash_id == FLASH_UNKNOWN) {
116 printf ("missing or unknown FLASH type\n");
120 switch (info->flash_id & FLASH_VENDMASK) {
121 case FLASH_MAN_AMD: printf ("AMD "); break;
122 default: printf ("Unknown Vendor "); break;
125 switch (info->flash_id & FLASH_TYPEMASK) {
126 case FLASH_AM040: printf ("AM29F040 (512 Kbit, uniform sector size)\n");
128 default: printf ("Unknown Chip Type\n");
132 printf (" Size: %ld KB in %d Sectors\n",
133 info->size >> 10, info->sector_count);
135 printf (" Sector Start Addresses:");
136 for (i=0; i<info->sector_count; ++i) {
138 * Check if whole sector is erased
140 if (i != (info->sector_count-1))
141 size = info->start[i+1] - info->start[i];
143 size = info->start[0] + info->size - info->start[i];
145 flash = (volatile unsigned long *)info->start[i];
146 size = size >> 2; /* divide by 4 for longword access */
147 for (k=0; k<size; k++)
149 if (*flash++ != 0xffffffff)
158 printf (" %08lX%s%s",
161 info->protect[i] ? "RO " : " "
168 /*-----------------------------------------------------------------------
172 /*-----------------------------------------------------------------------
176 * The following code cannot be run from FLASH!
178 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
181 FLASH_WORD_SIZE value;
182 ulong base = (ulong)addr;
183 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)addr;
185 DEBUGF("FLASH ADDR: %08x\n", (unsigned)addr );
187 /* Write auto select command: read Manufacturer ID */
189 addr2[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
191 addr2[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
193 addr2[ADDR0] = (FLASH_WORD_SIZE)0x00900090;
198 DEBUGF("FLASH MANUFACT: %x\n", value);
201 case (FLASH_WORD_SIZE)AMD_MANUFACT:
202 info->flash_id = FLASH_MAN_AMD;
205 info->flash_id = FLASH_UNKNOWN;
206 info->sector_count = 0;
208 return (0); /* no or unknown flash */
211 value = addr2[1]; /* device ID */
213 DEBUGF("\nFLASH DEVICEID: %x\n", value);
216 case (FLASH_WORD_SIZE)AMD_ID_LV040B:
217 info->flash_id += FLASH_AM040;
218 info->sector_count = 8;
219 info->size = 0x00080000; /* => 512 kb */
223 info->flash_id = FLASH_UNKNOWN;
224 return (0); /* => no or unknown flash */
228 /* set up sector start address table */
229 if (info->flash_id == FLASH_AM040) {
230 for (i = 0; i < info->sector_count; i++)
231 info->start[i] = base + (i * 0x00010000);
233 if (info->flash_id & FLASH_BTYPE) {
234 /* set sector offsets for bottom boot block type */
235 info->start[0] = base + 0x00000000;
236 info->start[1] = base + 0x00004000;
237 info->start[2] = base + 0x00006000;
238 info->start[3] = base + 0x00008000;
239 for (i = 4; i < info->sector_count; i++) {
240 info->start[i] = base + (i * 0x00010000) - 0x00030000;
243 /* set sector offsets for top boot block type */
244 i = info->sector_count - 1;
245 info->start[i--] = base + info->size - 0x00004000;
246 info->start[i--] = base + info->size - 0x00006000;
247 info->start[i--] = base + info->size - 0x00008000;
248 for (; i >= 0; i--) {
249 info->start[i] = base + i * 0x00010000;
254 /* check for protected sectors */
255 for (i = 0; i < info->sector_count; i++) {
256 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
257 /* D0 = 1 if protected */
258 addr2 = (volatile FLASH_WORD_SIZE *)(info->start[i]);
259 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
260 info->protect[i] = 0;
262 info->protect[i] = addr2[2] & 1;
265 /* reset to return to reading data */
266 addr2[0] = (FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
269 * Prevent writes to uninitialized FLASH.
271 if (info->flash_id != FLASH_UNKNOWN) {
272 addr2 = (FLASH_WORD_SIZE *)info->start[0];
273 *addr2 = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
279 int wait_for_DQ7(flash_info_t *info, int sect)
281 ulong start, now, last;
282 volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[sect]);
284 start = get_timer (0);
286 while ((addr[0] & (FLASH_WORD_SIZE)0x00800080) != (FLASH_WORD_SIZE)0x00800080) {
287 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
288 printf ("Timeout\n");
291 /* show that we're waiting */
292 if ((now - last) > 1000) { /* every second */
300 /*-----------------------------------------------------------------------
303 int flash_erase (flash_info_t *info, int s_first, int s_last)
305 volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[0]);
306 volatile FLASH_WORD_SIZE *addr2;
307 int flag, prot, sect;
310 if ((s_first < 0) || (s_first > s_last)) {
311 if (info->flash_id == FLASH_UNKNOWN) {
312 printf ("- missing\n");
314 printf ("- no sectors to erase\n");
319 if (info->flash_id == FLASH_UNKNOWN) {
320 printf ("Can't erase unknown flash type - aborted\n");
325 for (sect=s_first; sect<=s_last; ++sect) {
326 if (info->protect[sect]) {
332 printf ("- Warning: %d protected sectors will not be erased!\n",
338 /* Disable interrupts which might cause a timeout here */
339 flag = disable_interrupts();
341 /* Start erase on unprotected sectors */
342 for (sect = s_first; sect<=s_last; sect++) {
343 if (info->protect[sect] == 0) { /* not protected */
344 addr2 = (FLASH_WORD_SIZE *)(info->start[sect]);
345 DEBUGF("Erasing sector %p\n", addr2);
347 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
348 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
349 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
350 addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
351 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
352 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
353 addr2[0] = (FLASH_WORD_SIZE)0x00500050; /* block erase */
355 udelay(1000); /* wait 1 ms */
357 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
358 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
359 addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
360 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
361 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
362 addr2[0] = (FLASH_WORD_SIZE)0x00300030; /* sector erase */
365 * Wait for each sector to complete, it's more
366 * reliable. According to AMD Spec, you must
367 * issue all erase commands within a specified
368 * timeout. This has been seen to fail, especially
369 * if printf()s are included (for debug)!!
371 wait_for_DQ7(info, sect);
375 /* re-enable interrupts if necessary */
379 /* wait at least 80us - let's wait 1 ms */
382 /* reset to read mode */
383 addr = (FLASH_WORD_SIZE *)info->start[0];
384 addr[0] = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
390 /*-----------------------------------------------------------------------
391 * Copy memory to flash, returns:
394 * 2 - Flash not erased
397 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
402 wp = (addr & ~3); /* get lower word aligned address */
405 * handle unaligned start bytes
407 if ((l = addr - wp) != 0) {
409 for (i=0, cp=wp; i<l; ++i, ++cp) {
410 data = (data << 8) | (*(uchar *)cp);
412 for (; i<4 && cnt>0; ++i) {
413 data = (data << 8) | *src++;
417 for (; cnt==0 && i<4; ++i, ++cp) {
418 data = (data << 8) | (*(uchar *)cp);
421 if ((rc = write_word(info, wp, data)) != 0) {
428 * handle word aligned part
432 for (i=0; i<4; ++i) {
433 data = (data << 8) | *src++;
435 if ((rc = write_word(info, wp, data)) != 0) {
447 * handle unaligned tail bytes
450 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
451 data = (data << 8) | *src++;
454 for (; i<4; ++i, ++cp) {
455 data = (data << 8) | (*(uchar *)cp);
458 return (write_word(info, wp, data));
461 /*-----------------------------------------------------------------------
462 * Write a word to Flash, returns:
465 * 2 - Flash not erased
467 static int write_word (flash_info_t * info, ulong dest, ulong data)
469 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *) (info->start[0]);
470 volatile FLASH_WORD_SIZE *dest2 = (FLASH_WORD_SIZE *) dest;
471 volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *) & data;
475 /* Check if Flash is (sufficiently) erased */
476 if ((*((volatile FLASH_WORD_SIZE *) dest) &
477 (FLASH_WORD_SIZE) data) != (FLASH_WORD_SIZE) data) {
481 for (i = 0; i < 4 / sizeof (FLASH_WORD_SIZE); i++) {
484 /* Disable interrupts which might cause a timeout here */
485 flag = disable_interrupts ();
487 addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
488 addr2[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
489 addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00A000A0;
493 /* re-enable interrupts if necessary */
495 enable_interrupts ();
497 /* data polling for D7 */
498 start = get_timer (0);
499 while ((dest2[i] & (FLASH_WORD_SIZE) 0x00800080) !=
500 (data2[i] & (FLASH_WORD_SIZE) 0x00800080)) {
502 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {