2 **=====================================================================
4 ** Copyright (C) 2000, 2001, 2002, 2003
5 ** The LEOX team <team@leox.org>, http://www.leox.org
7 ** LEOX.org is about the development of free hardware and software resources
10 ** Description: U-Boot port on the LEOX's ELPT860 CPU board
13 **=====================================================================
15 ** This program is free software; you can redistribute it and/or
16 ** modify it under the terms of the GNU General Public License as
17 ** published by the Free Software Foundation; either version 2 of
18 ** the License, or (at your option) any later version.
20 ** This program is distributed in the hope that it will be useful,
21 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ** GNU General Public License for more details.
25 ** You should have received a copy of the GNU General Public License
26 ** along with this program; if not, write to the Free Software
27 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 **=====================================================================
34 ** Note 1: In this file, you have to provide the following variable:
36 ** flash_info_t flash_info[CFG_MAX_FLASH_BANKS]
37 ** 'flash_info_t' structure is defined into 'include/flash.h'
38 ** and defined as extern into 'common/cmd_flash.c'
40 ** Note 2: In this file, you have to provide the following functions:
42 ** unsigned long flash_init(void)
43 ** called from 'board_init_r()' into 'common/board.c'
45 ** void flash_print_info(flash_info_t *info)
46 ** called from 'do_flinfo()' into 'common/cmd_flash.c'
48 ** int flash_erase(flash_info_t *info,
51 ** called from 'do_flerase()' & 'flash_sect_erase()' into 'common/cmd_flash.c'
53 ** int write_buff (flash_info_t *info,
57 ** called from 'flash_write()' into 'common/cmd_flash.c'
65 # define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
68 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
70 /*-----------------------------------------------------------------------
73 static void flash_get_offsets (ulong base, flash_info_t *info);
74 static ulong flash_get_size (volatile unsigned char *addr, flash_info_t *info);
76 static int write_word (flash_info_t *info, ulong dest, ulong data);
77 static int write_byte (flash_info_t *info, ulong dest, uchar data);
79 /*-----------------------------------------------------------------------
85 volatile immap_t *immap = (immap_t *)CFG_IMMR;
86 volatile memctl8xx_t *memctl = &immap->im_memctl;
87 unsigned long size_b0;
90 /* Init: no FLASHes known */
91 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i)
93 flash_info[i].flash_id = FLASH_UNKNOWN;
96 /* Static FLASH Bank configuration here - FIXME XXX */
98 size_b0 = flash_get_size ((volatile unsigned char *)FLASH_BASE0_PRELIM,
101 if ( flash_info[0].flash_id == FLASH_UNKNOWN )
103 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
104 size_b0, size_b0<<20);
107 /* Remap FLASH according to real size */
108 memctl->memc_or0 = CFG_OR_TIMING_FLASH | (-size_b0 & OR_AM_MSK);
109 memctl->memc_br0 = (CFG_FLASH_BASE & BR_BA_MSK) | BR_MS_GPCM | BR_PS_8 | BR_V;
111 /* Re-do sizing to get full correct info */
112 size_b0 = flash_get_size ((volatile unsigned char *)CFG_FLASH_BASE,
115 flash_get_offsets (CFG_FLASH_BASE, &flash_info[0]);
117 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
118 /* monitor protection ON by default */
119 flash_protect (FLAG_PROTECT_SET,
121 CFG_MONITOR_BASE + monitor_flash_len-1,
125 #ifdef CFG_ENV_IS_IN_FLASH
126 /* ENV protection ON by default */
127 flash_protect(FLAG_PROTECT_SET,
129 CFG_ENV_ADDR + CFG_ENV_SIZE-1,
133 flash_info[0].size = size_b0;
138 /*-----------------------------------------------------------------------
141 flash_get_offsets (ulong base,
146 #define SECTOR_64KB 0x00010000
148 /* set up sector start adress table */
149 for (i = 0; i < info->sector_count; i++)
151 info->start[i] = base + (i * SECTOR_64KB);
155 /*-----------------------------------------------------------------------
158 flash_print_info (flash_info_t *info)
162 if ( info->flash_id == FLASH_UNKNOWN )
164 printf ("missing or unknown FLASH type\n");
168 switch ( info->flash_id & FLASH_VENDMASK )
170 case FLASH_MAN_AMD: printf ("AMD "); break;
171 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
172 case FLASH_MAN_STM: printf ("STM (Thomson) "); break;
173 default: printf ("Unknown Vendor "); break;
176 switch ( info->flash_id & FLASH_TYPEMASK )
178 case FLASH_AM040: printf ("AM29F040 (4 Mbits)\n");
180 default: printf ("Unknown Chip Type\n");
184 printf (" Size: %ld KB in %d Sectors\n",
185 info->size >> 10, info->sector_count);
187 printf (" Sector Start Addresses:");
188 for (i=0; i<info->sector_count; ++i)
194 info->protect[i] ? " (RO)" : " "
202 /*-----------------------------------------------------------------------
206 /*-----------------------------------------------------------------------
210 * The following code cannot be run from FLASH!
214 flash_get_size (volatile unsigned char *addr,
219 ulong base = (ulong)addr;
221 /* Write auto select command: read Manufacturer ID */
230 /* case AMD_MANUFACT: */
232 info->flash_id = FLASH_MAN_AMD;
234 /* case FUJ_MANUFACT: */
236 info->flash_id = FLASH_MAN_FUJ;
238 /* case STM_MANUFACT: */
240 info->flash_id = FLASH_MAN_STM;
244 info->flash_id = FLASH_UNKNOWN;
245 info->sector_count = 0;
247 return (0); /* no or unknown flash */
250 value = addr[1]; /* device ID */
256 info->flash_id += FLASH_AM040; /* 4 Mbits = 512k * 8 */
257 info->sector_count = 8;
258 info->size = 0x00080000;
262 info->flash_id = FLASH_UNKNOWN;
263 return (0); /* => no or unknown flash */
266 /* set up sector start adress table */
267 for (i = 0; i < info->sector_count; i++)
269 info->start[i] = base + (i * 0x00010000);
272 /* check for protected sectors */
273 for (i = 0; i < info->sector_count; i++)
275 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
276 /* D0 = 1 if protected */
277 addr = (volatile unsigned char *)(info->start[i]);
278 info->protect[i] = addr[2] & 1;
282 * Prevent writes to uninitialized FLASH.
284 if ( info->flash_id != FLASH_UNKNOWN )
286 addr = (volatile unsigned char *)info->start[0];
288 *addr = 0xF0; /* reset bank */
295 /*-----------------------------------------------------------------------
299 flash_erase (flash_info_t *info,
303 volatile unsigned char *addr = (volatile unsigned char *)(info->start[0]);
304 int flag, prot, sect, l_sect;
305 ulong start, now, last;
307 if ( (s_first < 0) || (s_first > s_last) )
309 if ( info->flash_id == FLASH_UNKNOWN )
311 printf ("- missing\n");
315 printf ("- no sectors to erase\n");
320 if ( (info->flash_id == FLASH_UNKNOWN) ||
321 (info->flash_id > FLASH_AMD_COMP) )
323 printf ("Can't erase unknown flash type %08lx - aborted\n",
329 for (sect=s_first; sect<=s_last; ++sect)
331 if ( info->protect[sect] )
339 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
348 /* Disable interrupts which might cause a timeout here */
349 flag = disable_interrupts();
357 /* Start erase on unprotected sectors */
358 for (sect = s_first; sect<=s_last; sect++)
360 if (info->protect[sect] == 0) /* not protected */
362 addr = (volatile unsigned char *)(info->start[sect]);
368 /* re-enable interrupts if necessary */
372 /* wait at least 80us - let's wait 1 ms */
376 * We wait for the last triggered sector
381 start = get_timer (0);
383 addr = (volatile unsigned char *)(info->start[l_sect]);
384 while ( (addr[0] & 0x80) != 0x80 )
386 if ( (now = get_timer(start)) > CFG_FLASH_ERASE_TOUT )
388 printf ("Timeout\n");
391 /* show that we're waiting */
392 if ( (now - last) > 1000 ) /* every second */
400 /* reset to read mode */
401 addr = (volatile unsigned char *)info->start[0];
402 addr[0] = 0xF0; /* reset bank */
409 /*-----------------------------------------------------------------------
410 * Copy memory to flash, returns:
413 * 2 - Flash not erased
417 write_buff (flash_info_t *info,
426 if ( (info->flash_id & FLASH_TYPEMASK) == FLASH_AM040 )
428 /* Width of the data bus: 8 bits */
436 if ( (rc = write_byte(info, wp, bdata)) != 0 )
449 /* Width of the data bus: 32 bits */
451 wp = (addr & ~3); /* get lower word aligned address */
454 * handle unaligned start bytes
456 if ( (l = addr - wp) != 0 )
459 for (i=0, cp=wp; i<l; ++i, ++cp)
461 data = (data << 8) | (*(uchar *)cp);
463 for (; i<4 && cnt>0; ++i)
465 data = (data << 8) | *src++;
469 for (; cnt==0 && i<4; ++i, ++cp)
471 data = (data << 8) | (*(uchar *)cp);
474 if ( (rc = write_word(info, wp, data)) != 0 )
482 * handle word aligned part
489 data = (data << 8) | *src++;
491 if ( (rc = write_word(info, wp, data)) != 0 )
505 * handle unaligned tail bytes
508 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp)
510 data = (data << 8) | *src++;
513 for (; i<4; ++i, ++cp)
515 data = (data << 8) | (*(uchar *)cp);
518 return (write_word(info, wp, data));
522 /*-----------------------------------------------------------------------
523 * Write a word to Flash, returns:
526 * 2 - Flash not erased
529 write_word (flash_info_t *info,
533 vu_long *addr = (vu_long*)(info->start[0]);
537 /* Check if Flash is (sufficiently) erased */
538 if ( (*((vu_long *)dest) & data) != data )
542 /* Disable interrupts which might cause a timeout here */
543 flag = disable_interrupts();
545 addr[0x0555] = 0x00AA00AA;
546 addr[0x02AA] = 0x00550055;
547 addr[0x0555] = 0x00A000A0;
549 *((vu_long *)dest) = data;
551 /* re-enable interrupts if necessary */
555 /* data polling for D7 */
556 start = get_timer (0);
557 while ( (*((vu_long *)dest) & 0x00800080) != (data & 0x00800080) )
559 if ( get_timer(start) > CFG_FLASH_WRITE_TOUT )
568 /*-----------------------------------------------------------------------
569 * Write a byte to Flash, returns:
572 * 2 - Flash not erased
575 write_byte (flash_info_t *info,
579 volatile unsigned char *addr = (volatile unsigned char *)(info->start[0]);
583 /* Check if Flash is (sufficiently) erased */
584 if ( (*((volatile unsigned char *)dest) & data) != data )
588 /* Disable interrupts which might cause a timeout here */
589 flag = disable_interrupts();
595 *((volatile unsigned char *)dest) = data;
597 /* re-enable interrupts if necessary */
601 /* data polling for D7 */
602 start = get_timer (0);
603 while ( (*((volatile unsigned char *)dest) & 0x80) != (data & 0x80) )
605 if ( get_timer(start) > CFG_FLASH_WRITE_TOUT )
614 /*-----------------------------------------------------------------------