2 * (C) Copyright 2003, Dan Malek, Embedded Edge, LLC. <dan@embeddededge.com>
4 * Updated to support the Silicon Tx GP3 8560. We should only find
5 * two Intel 28F640 parts in 16-bit mode (i.e. 32-bit wide flash),
6 * but I left other code here in case people order custom boards.
8 * (C) Copyright 2003 Motorola Inc.
9 * Xianghua Xiao,(X.Xiao@motorola.com)
11 * (C) Copyright 2000, 2001
12 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
14 * (C) Copyright 2001, Stuart Hughes, Lineo Inc, stuarth@lineo.com
15 * Add support the Sharp chips on the mpc8260ads.
16 * I started with board/ip860/flash.c and made changes I found in
17 * the MTD project by David Schleef.
19 * See file CREDITS for list of people who contributed to this
22 * This program is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU General Public License as
24 * published by the Free Software Foundation; either version 2 of
25 * the License, or (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
40 #if !defined(CFG_NO_FLASH)
42 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
44 #if defined(CFG_ENV_IS_IN_FLASH)
46 # define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
49 # define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
51 # ifndef CFG_ENV_SECT_SIZE
52 # define CFG_ENV_SECT_SIZE CFG_ENV_SIZE
58 /*-----------------------------------------------------------------------
61 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
62 static int write_word (flash_info_t *info, ulong dest, ulong data);
63 static int clear_block_lock_bit(vu_long * addr);
64 /*-----------------------------------------------------------------------
67 unsigned long flash_init (void)
72 /* Init: enable write,
73 * or we cannot even write flash commands
75 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
76 flash_info[i].flash_id = FLASH_UNKNOWN;
78 /* set the default sector offset */
81 /* Static FLASH Bank configuration here - FIXME XXX */
83 size = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
85 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
86 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
90 /* Re-do sizing to get full correct info */
91 size = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
93 flash_info[0].size = size;
95 #if !defined(CONFIG_RAM_AS_FLASH)
96 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
97 /* monitor protection ON by default */
98 flash_protect(FLAG_PROTECT_SET,
100 CFG_MONITOR_BASE+monitor_flash_len-1,
104 #ifdef CFG_ENV_IS_IN_FLASH
105 /* ENV protection ON by default */
106 flash_protect(FLAG_PROTECT_SET,
108 CFG_ENV_ADDR+CFG_ENV_SECT_SIZE-1,
115 /*-----------------------------------------------------------------------
117 void flash_print_info (flash_info_t *info)
121 if (info->flash_id == FLASH_UNKNOWN) {
122 printf ("missing or unknown FLASH type\n");
126 switch (info->flash_id & FLASH_VENDMASK) {
127 case FLASH_MAN_INTEL: printf ("Intel "); break;
128 case FLASH_MAN_SHARP: printf ("Sharp "); break;
129 default: printf ("Unknown Vendor "); break;
132 switch (info->flash_id & FLASH_TYPEMASK) {
133 case FLASH_28F640C3T: printf ("28F640C3T (64 Mbit x 2, 128 x 128k)\n");
135 default: printf ("Unknown Chip Type\n");
139 printf (" Size: %ld MB in %d Sectors\n",
140 info->size >> 20, info->sector_count);
142 printf (" Sector Start Addresses:");
143 for (i=0; i<info->sector_count; ++i) {
148 info->protect[i] ? " (RO)" : " "
155 * The following code cannot be run from FLASH!
158 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
162 ulong base = (ulong)addr;
166 printf("Check flash at 0x%08x\n",(uint)addr);
168 /* Write "Intelligent Identifier" command: read Manufacturer ID */
173 value = addr[0] & 0x00FF00FF;
176 printf("manufacturer=0x%x\n",(uint)value);
179 case MT_MANUFACT: /* SHARP, MT or => Intel */
181 info->flash_id = FLASH_MAN_INTEL;
184 printf("unknown manufacturer: %x\n", (unsigned int)value);
185 info->flash_id = FLASH_UNKNOWN;
186 info->sector_count = 0;
188 return (0); /* no or unknown flash */
191 value = addr[1]; /* device ID */
194 printf("deviceID=0x%x\n",(uint)value);
198 case (INTEL_ID_28F640C3T):
199 info->flash_id += FLASH_28F640C3T;
200 info->sector_count = 135;
201 info->size = 0x01000000;
202 sector_offset = 0x20000;
203 break; /* => 2x8 MB */
206 info->flash_id = FLASH_UNKNOWN;
207 return (0); /* => no or unknown flash */
211 /* set up sector start address table
212 * The first 127 blocks are large, the last 8 are small.
214 for (i = 0; i < 127; i++) {
215 info->start[i] = base;
216 base += sector_offset;
217 /* Sectors are locked upon reset */
218 info->protect[i] = 0;
220 for (i = 127; i < 135; i++) {
221 info->start[i] = base;
223 /* Sectors are locked upon reset */
224 info->protect[i] = 0;
229 * Prevent writes to uninitialized FLASH.
231 if (info->flash_id != FLASH_UNKNOWN) {
232 addr = (vu_long *)info->start[0];
233 *addr = 0xFFFFFF; /* reset bank to read array mode */
241 /*-----------------------------------------------------------------------
244 int flash_erase (flash_info_t *info, int s_first, int s_last)
246 int flag, prot, sect;
247 ulong start, now, last;
249 if ((s_first < 0) || (s_first > s_last)) {
250 if (info->flash_id == FLASH_UNKNOWN) {
251 printf ("- missing\n");
253 printf ("- no sectors to erase\n");
258 if ( ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL)
259 && ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_SHARP) ) {
260 printf ("Can't erase unknown flash type %08lx - aborted\n",
266 for (sect=s_first; sect<=s_last; ++sect) {
267 if (info->protect[sect]) {
273 printf ("- Warning: %d protected sectors will not be erased!\n",
280 printf("\nFlash Erase:\n");
282 /* Make Sure Block Lock Bit is not set. */
283 if(clear_block_lock_bit((vu_long *)(info->start[s_first]))){
287 /* Start erase on unprotected sectors */
289 printf("Begin to erase now,s_first=0x%x s_last=0x%x...\n",s_first,s_last);
291 for (sect = s_first; sect<=s_last; sect++) {
292 if (info->protect[sect] == 0) { /* not protected */
293 vu_long *addr = (vu_long *)(info->start[sect]);
296 last = start = get_timer (0);
298 /* Disable interrupts which might cause a timeout here */
299 flag = disable_interrupts();
304 /* Clear Status Register */
307 /* Single Block Erase Command */
314 if((info->flash_id & FLASH_TYPEMASK) != FLASH_LH28F016SCT) {
315 /* Resume Command, as per errata update */
320 /* re-enable interrupts if necessary */
324 /* wait at least 80us - let's wait 1 ms */
326 while ((*addr & 0x00800080) != 0x00800080) {
327 if(*addr & 0x00200020){
328 printf("Error in Block Erase - Lock Bit may be set!\n");
329 printf("Status Register = 0x%X\n", (uint)*addr);
330 *addr = 0xFFFFFFFF; /* reset bank */
334 if ((now=get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
335 printf ("Timeout\n");
336 *addr = 0xFFFFFFFF; /* reset bank */
340 /* show that we're waiting */
341 if ((now - last) > 1000) { /* every second */
347 /* reset to read mode */
353 printf ("flash erase done\n");
357 /*-----------------------------------------------------------------------
358 * Copy memory to flash, returns:
361 * 2 - Flash not erased
364 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
369 wp = (addr & ~3); /* get lower word aligned address */
372 * handle unaligned start bytes
374 if ((l = addr - wp) != 0) {
376 for (i=0, cp=wp; i<l; ++i, ++cp) {
377 data = (data << 8) | (*(uchar *)cp);
379 for (; i<4 && cnt>0; ++i) {
380 data = (data << 8) | *src++;
384 for (; cnt==0 && i<4; ++i, ++cp) {
385 data = (data << 8) | (*(uchar *)cp);
388 if ((rc = write_word(info, wp, data)) != 0) {
395 * handle word aligned part
399 for (i=0; i<4; ++i) {
400 data = (data << 8) | *src++;
402 if ((rc = write_word(info, wp, data)) != 0) {
414 * handle unaligned tail bytes
417 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
418 data = (data << 8) | *src++;
421 for (; i<4; ++i, ++cp) {
422 data = (data << 8) | (*(uchar *)cp);
425 return (write_word(info, wp, data));
428 /*-----------------------------------------------------------------------
429 * Write a word to Flash, returns:
432 * 2 - Flash not erased
434 static int write_word (flash_info_t *info, ulong dest, ulong data)
436 vu_long *addr = (vu_long *)dest;
440 /* Check if Flash is (sufficiently) erased */
441 if ((*addr & data) != data) {
444 /* Disable interrupts which might cause a timeout here */
445 flag = disable_interrupts();
454 /* re-enable interrupts if necessary */
458 /* data polling for D7 */
459 start = get_timer (0);
462 while (((csr = *addr) & 0x00800080) != 0x00800080) {
463 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
468 if (csr & 0x40404040) {
469 printf ("CSR indicates write error (%08lx) at %08lx\n", csr, (ulong)addr);
473 /* Clear Status Registers Command */
476 /* Reset to read array mode */
483 /*-----------------------------------------------------------------------
484 * Clear Block Lock Bit, returns:
489 static int clear_block_lock_bit(vu_long * addr)
496 /* Clear Status Register */
505 start = get_timer (0);
506 while((*addr & 0x00800080) != 0x00800080){
507 if ((now=get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
508 printf ("Timeout on clearing Block Lock Bit\n");
509 *addr = 0xFFFFFFFF; /* reset bank */
517 #endif /* !CFG_NO_FLASH */