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 * SPDX-License-Identifier: GPL-2.0+
11 * Ported from Ebony flash support
13 * Sandburst Corporation
16 #include <asm/ppc4xx.h>
17 #include <asm/processor.h>
22 #define DEBUGF(x...) printf(x)
28 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
30 static unsigned long flash_addr_table[8][CONFIG_SYS_MAX_FLASH_BANKS] = {
31 {0xfff80000} /* Boot Flash */
34 /*-----------------------------------------------------------------------
37 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
38 static int write_word (flash_info_t *info, ulong dest, ulong data);
43 #define FLASH_WORD_SIZE unsigned char
46 /*-----------------------------------------------------------------------
49 unsigned long flash_init (void)
51 unsigned long total_b = 0;
52 unsigned long size_b[CONFIG_SYS_MAX_FLASH_BANKS];
53 unsigned short index = 0;
58 DEBUGF("FLASH: Index: %d\n", index);
60 /* Init: no FLASHes known */
61 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
62 flash_info[i].flash_id = FLASH_UNKNOWN;
63 flash_info[i].sector_count = -1;
64 flash_info[i].size = 0;
66 /* check whether the address is 0 */
67 if (flash_addr_table[index][i] == 0) {
71 /* call flash_get_size() to initialize sector address */
72 size_b[i] = flash_get_size(
73 (vu_long *)flash_addr_table[index][i], &flash_info[i]);
74 flash_info[i].size = size_b[i];
75 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
76 printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",
77 i, size_b[i], size_b[i]<<20);
78 flash_info[i].sector_count = -1;
79 flash_info[i].size = 0;
82 total_b += flash_info[i].size;
89 /*-----------------------------------------------------------------------
91 void flash_print_info (flash_info_t *info)
97 volatile unsigned long *flash;
99 if (info->flash_id == FLASH_UNKNOWN) {
100 printf ("missing or unknown FLASH type\n");
104 switch (info->flash_id & FLASH_VENDMASK) {
105 case FLASH_MAN_AMD: printf ("AMD "); break;
106 default: printf ("Unknown Vendor "); break;
109 switch (info->flash_id & FLASH_TYPEMASK) {
110 case FLASH_AM040: printf ("AM29F040 (512 Kbit, uniform sector size)\n");
112 default: printf ("Unknown Chip Type\n");
116 printf (" Size: %ld KB in %d Sectors\n",
117 info->size >> 10, info->sector_count);
119 printf (" Sector Start Addresses:");
120 for (i=0; i<info->sector_count; ++i) {
122 * Check if whole sector is erased
124 if (i != (info->sector_count-1))
125 size = info->start[i+1] - info->start[i];
127 size = info->start[0] + info->size - info->start[i];
129 flash = (volatile unsigned long *)info->start[i];
130 size = size >> 2; /* divide by 4 for longword access */
131 for (k=0; k<size; k++)
133 if (*flash++ != 0xffffffff)
142 printf (" %08lX%s%s",
145 info->protect[i] ? "RO " : " "
152 /*-----------------------------------------------------------------------
156 /*-----------------------------------------------------------------------
160 * The following code cannot be run from FLASH!
162 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
165 FLASH_WORD_SIZE value;
166 ulong base = (ulong)addr;
167 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)addr;
169 DEBUGF("FLASH ADDR: %08x\n", (unsigned)addr );
171 /* Write auto select command: read Manufacturer ID */
173 addr2[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
175 addr2[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
177 addr2[ADDR0] = (FLASH_WORD_SIZE)0x00900090;
182 DEBUGF("FLASH MANUFACT: %x\n", value);
185 case (FLASH_WORD_SIZE)AMD_MANUFACT:
186 info->flash_id = FLASH_MAN_AMD;
189 info->flash_id = FLASH_UNKNOWN;
190 info->sector_count = 0;
192 return (0); /* no or unknown flash */
195 value = addr2[1]; /* device ID */
197 DEBUGF("\nFLASH DEVICEID: %x\n", value);
200 case (FLASH_WORD_SIZE)AMD_ID_LV040B:
201 info->flash_id += FLASH_AM040;
202 info->sector_count = 8;
203 info->size = 0x00080000; /* => 512 kb */
207 info->flash_id = FLASH_UNKNOWN;
208 return (0); /* => no or unknown flash */
212 /* set up sector start address table */
213 if (info->flash_id == FLASH_AM040) {
214 for (i = 0; i < info->sector_count; i++)
215 info->start[i] = base + (i * 0x00010000);
217 if (info->flash_id & FLASH_BTYPE) {
218 /* set sector offsets for bottom boot block type */
219 info->start[0] = base + 0x00000000;
220 info->start[1] = base + 0x00004000;
221 info->start[2] = base + 0x00006000;
222 info->start[3] = base + 0x00008000;
223 for (i = 4; i < info->sector_count; i++) {
224 info->start[i] = base + (i * 0x00010000) - 0x00030000;
227 /* set sector offsets for top boot block type */
228 i = info->sector_count - 1;
229 info->start[i--] = base + info->size - 0x00004000;
230 info->start[i--] = base + info->size - 0x00006000;
231 info->start[i--] = base + info->size - 0x00008000;
232 for (; i >= 0; i--) {
233 info->start[i] = base + i * 0x00010000;
238 /* check for protected sectors */
239 for (i = 0; i < info->sector_count; i++) {
240 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
241 /* D0 = 1 if protected */
242 addr2 = (volatile FLASH_WORD_SIZE *)(info->start[i]);
243 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
244 info->protect[i] = 0;
246 info->protect[i] = addr2[2] & 1;
249 /* reset to return to reading data */
250 addr2[0] = (FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
253 * Prevent writes to uninitialized FLASH.
255 if (info->flash_id != FLASH_UNKNOWN) {
256 addr2 = (FLASH_WORD_SIZE *)info->start[0];
257 *addr2 = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
263 int wait_for_DQ7(flash_info_t *info, int sect)
265 ulong start, now, last;
266 volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[sect]);
268 start = get_timer (0);
270 while ((addr[0] & (FLASH_WORD_SIZE)0x00800080) != (FLASH_WORD_SIZE)0x00800080) {
271 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
272 printf ("Timeout\n");
275 /* show that we're waiting */
276 if ((now - last) > 1000) { /* every second */
284 /*-----------------------------------------------------------------------
287 int flash_erase (flash_info_t *info, int s_first, int s_last)
289 volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[0]);
290 volatile FLASH_WORD_SIZE *addr2;
291 int flag, prot, sect;
294 if ((s_first < 0) || (s_first > s_last)) {
295 if (info->flash_id == FLASH_UNKNOWN) {
296 printf ("- missing\n");
298 printf ("- no sectors to erase\n");
303 if (info->flash_id == FLASH_UNKNOWN) {
304 printf ("Can't erase unknown flash type - aborted\n");
309 for (sect=s_first; sect<=s_last; ++sect) {
310 if (info->protect[sect]) {
316 printf ("- Warning: %d protected sectors will not be erased!\n",
322 /* Disable interrupts which might cause a timeout here */
323 flag = disable_interrupts();
325 /* Start erase on unprotected sectors */
326 for (sect = s_first; sect<=s_last; sect++) {
327 if (info->protect[sect] == 0) { /* not protected */
328 addr2 = (FLASH_WORD_SIZE *)(info->start[sect]);
329 DEBUGF("Erasing sector %p\n", addr2);
331 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
332 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
333 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
334 addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
335 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
336 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
337 addr2[0] = (FLASH_WORD_SIZE)0x00500050; /* block erase */
339 udelay(1000); /* wait 1 ms */
341 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
342 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
343 addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
344 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
345 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
346 addr2[0] = (FLASH_WORD_SIZE)0x00300030; /* sector erase */
349 * Wait for each sector to complete, it's more
350 * reliable. According to AMD Spec, you must
351 * issue all erase commands within a specified
352 * timeout. This has been seen to fail, especially
353 * if printf()s are included (for debug)!!
355 wait_for_DQ7(info, sect);
359 /* re-enable interrupts if necessary */
363 /* wait at least 80us - let's wait 1 ms */
366 /* reset to read mode */
367 addr = (FLASH_WORD_SIZE *)info->start[0];
368 addr[0] = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
374 /*-----------------------------------------------------------------------
375 * Copy memory to flash, returns:
378 * 2 - Flash not erased
381 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
386 wp = (addr & ~3); /* get lower word aligned address */
389 * handle unaligned start bytes
391 if ((l = addr - wp) != 0) {
393 for (i=0, cp=wp; i<l; ++i, ++cp) {
394 data = (data << 8) | (*(uchar *)cp);
396 for (; i<4 && cnt>0; ++i) {
397 data = (data << 8) | *src++;
401 for (; cnt==0 && i<4; ++i, ++cp) {
402 data = (data << 8) | (*(uchar *)cp);
405 if ((rc = write_word(info, wp, data)) != 0) {
412 * handle word aligned part
416 for (i=0; i<4; ++i) {
417 data = (data << 8) | *src++;
419 if ((rc = write_word(info, wp, data)) != 0) {
431 * handle unaligned tail bytes
434 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
435 data = (data << 8) | *src++;
438 for (; i<4; ++i, ++cp) {
439 data = (data << 8) | (*(uchar *)cp);
442 return (write_word(info, wp, data));
445 /*-----------------------------------------------------------------------
446 * Write a word to Flash, returns:
449 * 2 - Flash not erased
451 static int write_word (flash_info_t * info, ulong dest, ulong data)
453 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *) (info->start[0]);
454 volatile FLASH_WORD_SIZE *dest2 = (FLASH_WORD_SIZE *) dest;
455 volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *) & data;
459 /* Check if Flash is (sufficiently) erased */
460 if ((*((volatile FLASH_WORD_SIZE *) dest) &
461 (FLASH_WORD_SIZE) data) != (FLASH_WORD_SIZE) data) {
465 for (i = 0; i < 4 / sizeof (FLASH_WORD_SIZE); i++) {
468 /* Disable interrupts which might cause a timeout here */
469 flag = disable_interrupts ();
471 addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
472 addr2[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
473 addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00A000A0;
477 /* re-enable interrupts if necessary */
479 enable_interrupts ();
481 /* data polling for D7 */
482 start = get_timer (0);
483 while ((dest2[i] & (FLASH_WORD_SIZE) 0x00800080) !=
484 (data2[i] & (FLASH_WORD_SIZE) 0x00800080)) {
486 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {