2 * (C) Copyright 2003 Motorola Inc.
5 * (C) Copyright 2000, 2001
9 * Add support the Sharp chips on the mpc8260ads.
10 * I started with board/ip860/flash.c and made changes I found in
11 * the MTD project by David Schleef.
13 * See file CREDITS for list of people who contributed to this
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation; either version 2 of
19 * the License, or (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
34 #if !defined(CFG_NO_FLASH)
36 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
38 #if defined(CFG_ENV_IS_IN_FLASH)
40 # define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
43 # define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
45 # ifndef CFG_ENV_SECT_SIZE
46 # define CFG_ENV_SECT_SIZE CFG_ENV_SIZE
52 /*-----------------------------------------------------------------------
55 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
56 static int write_word (flash_info_t *info, ulong dest, ulong data);
57 static int clear_block_lock_bit(vu_long * addr);
58 /*-----------------------------------------------------------------------
61 unsigned long flash_init (void)
66 /* Init: enable write,
67 * or we cannot even write flash commands
69 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
70 flash_info[i].flash_id = FLASH_UNKNOWN;
72 /* set the default sector offset */
75 /* Static FLASH Bank configuration here - FIXME XXX */
77 size = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
79 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
80 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
84 /* Re-do sizing to get full correct info */
85 size = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
87 flash_info[0].size = size;
89 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
90 /* monitor protection ON by default */
91 flash_protect(FLAG_PROTECT_SET,
93 CFG_MONITOR_BASE+monitor_flash_len-1,
96 #ifdef CFG_ENV_IS_IN_FLASH
97 /* ENV protection ON by default */
98 flash_protect(FLAG_PROTECT_SET,
100 CFG_ENV_ADDR+CFG_ENV_SECT_SIZE-1,
107 /*-----------------------------------------------------------------------
109 void flash_print_info (flash_info_t *info)
113 if (info->flash_id == FLASH_UNKNOWN) {
114 printf ("missing or unknown FLASH type\n");
118 switch (info->flash_id & FLASH_VENDMASK) {
119 case FLASH_MAN_INTEL: printf ("Intel "); break;
120 case FLASH_MAN_SHARP: printf ("Sharp "); break;
121 default: printf ("Unknown Vendor "); break;
124 switch (info->flash_id & FLASH_TYPEMASK) {
125 case FLASH_28F016SV: printf ("28F016SV (16 Mbit, 32 x 64k)\n");
127 case FLASH_28F160S3: printf ("28F160S3 (16 Mbit, 32 x 512K)\n");
129 case FLASH_28F320S3: printf ("28F320S3 (32 Mbit, 64 x 512K)\n");
131 case FLASH_LH28F016SCT: printf ("28F016SC (16 Mbit, 32 x 64K)\n");
133 case FLASH_28F640J3A: printf ("28F640J3A (64 Mbit, 64 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] & 0x00FF00FF; /* device ID */
194 printf("deviceID=0x%x\n",(uint)value);
197 case (INTEL_ID_28F016S):
198 info->flash_id += FLASH_28F016SV;
199 info->sector_count = 32;
200 info->size = 0x00400000;
201 sector_offset = 0x20000;
202 break; /* => 2x2 MB */
204 case (INTEL_ID_28F160S3):
205 info->flash_id += FLASH_28F160S3;
206 info->sector_count = 32;
207 info->size = 0x00400000;
208 sector_offset = 0x20000;
209 break; /* => 2x2 MB */
211 case (INTEL_ID_28F320S3):
212 info->flash_id += FLASH_28F320S3;
213 info->sector_count = 64;
214 info->size = 0x00800000;
215 sector_offset = 0x20000;
216 break; /* => 2x4 MB */
218 case (INTEL_ID_28F640J3A):
219 info->flash_id += FLASH_28F640J3A;
220 info->sector_count = 64;
221 info->size = 0x01000000;
222 sector_offset = 0x40000;
223 break; /* => 2x8 MB */
225 case SHARP_ID_28F016SCL:
226 case SHARP_ID_28F016SCZ:
227 info->flash_id = FLASH_MAN_SHARP | FLASH_LH28F016SCT;
228 info->sector_count = 32;
229 info->size = 0x00800000;
230 sector_offset = 0x40000;
231 break; /* => 4x2 MB */
235 info->flash_id = FLASH_UNKNOWN;
236 return (0); /* => no or unknown flash */
240 /* set up sector start address table */
241 for (i = 0; i < info->sector_count; i++) {
242 info->start[i] = base;
243 base += sector_offset;
244 /* don't know how to check sector protection */
245 info->protect[i] = 0;
249 * Prevent writes to uninitialized FLASH.
251 if (info->flash_id != FLASH_UNKNOWN) {
252 addr = (vu_long *)info->start[0];
253 *addr = 0xFFFFFF; /* reset bank to read array mode */
261 /*-----------------------------------------------------------------------
264 int flash_erase (flash_info_t *info, int s_first, int s_last)
266 int flag, prot, sect;
267 ulong start, now, last;
269 if ((s_first < 0) || (s_first > s_last)) {
270 if (info->flash_id == FLASH_UNKNOWN) {
271 printf ("- missing\n");
273 printf ("- no sectors to erase\n");
278 if ( ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL)
279 && ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_SHARP) ) {
280 printf ("Can't erase unknown flash type %08lx - aborted\n",
286 for (sect=s_first; sect<=s_last; ++sect) {
287 if (info->protect[sect]) {
293 printf ("- Warning: %d protected sectors will not be erased!\n",
300 printf("\nFlash Erase:\n");
302 /* Make Sure Block Lock Bit is not set. */
303 if(clear_block_lock_bit((vu_long *)(info->start[s_first]))){
307 /* Start erase on unprotected sectors */
309 printf("Begin to erase now,s_first=0x%x s_last=0x%x...\n",s_first,s_last);
311 for (sect = s_first; sect<=s_last; sect++) {
312 if (info->protect[sect] == 0) { /* not protected */
313 vu_long *addr = (vu_long *)(info->start[sect]);
316 last = start = get_timer (0);
318 /* Disable interrupts which might cause a timeout here */
319 flag = disable_interrupts();
324 /* Clear Status Register */
327 /* Single Block Erase Command */
334 if((info->flash_id & FLASH_TYPEMASK) != FLASH_LH28F016SCT) {
335 /* Resume Command, as per errata update */
340 /* re-enable interrupts if necessary */
344 /* wait at least 80us - let's wait 1 ms */
346 while ((*addr & 0x00800080) != 0x00800080) {
347 if(*addr & 0x00200020){
348 printf("Error in Block Erase - Lock Bit may be set!\n");
349 printf("Status Register = 0x%X\n", (uint)*addr);
350 *addr = 0xFFFFFFFF; /* reset bank */
354 if ((now=get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
355 printf ("Timeout\n");
356 *addr = 0xFFFFFFFF; /* reset bank */
360 /* show that we're waiting */
361 if ((now - last) > 1000) { /* every second */
367 /* reset to read mode */
373 printf ("flash erase done\n");
377 /*-----------------------------------------------------------------------
378 * Copy memory to flash, returns:
381 * 2 - Flash not erased
384 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
389 wp = (addr & ~3); /* get lower word aligned address */
392 * handle unaligned start bytes
394 if ((l = addr - wp) != 0) {
396 for (i=0, cp=wp; i<l; ++i, ++cp) {
397 data = (data << 8) | (*(uchar *)cp);
399 for (; i<4 && cnt>0; ++i) {
400 data = (data << 8) | *src++;
404 for (; cnt==0 && i<4; ++i, ++cp) {
405 data = (data << 8) | (*(uchar *)cp);
408 if ((rc = write_word(info, wp, data)) != 0) {
415 * handle word aligned part
419 for (i=0; i<4; ++i) {
420 data = (data << 8) | *src++;
422 if ((rc = write_word(info, wp, data)) != 0) {
434 * handle unaligned tail bytes
437 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
438 data = (data << 8) | *src++;
441 for (; i<4; ++i, ++cp) {
442 data = (data << 8) | (*(uchar *)cp);
445 return (write_word(info, wp, data));
448 /*-----------------------------------------------------------------------
449 * Write a word to Flash, returns:
452 * 2 - Flash not erased
454 static int write_word (flash_info_t *info, ulong dest, ulong data)
456 vu_long *addr = (vu_long *)dest;
460 /* Check if Flash is (sufficiently) erased */
461 if ((*addr & data) != data) {
464 /* Disable interrupts which might cause a timeout here */
465 flag = disable_interrupts();
474 /* re-enable interrupts if necessary */
478 /* data polling for D7 */
479 start = get_timer (0);
482 while (((csr = *addr) & 0x00800080) != 0x00800080) {
483 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
488 if (csr & 0x40404040) {
489 printf ("CSR indicates write error (%08lx) at %08lx\n", csr, (ulong)addr);
493 /* Clear Status Registers Command */
496 /* Reset to read array mode */
503 /*-----------------------------------------------------------------------
504 * Clear Block Lock Bit, returns:
509 static int clear_block_lock_bit(vu_long * addr)
516 /* Clear Status Register */
525 start = get_timer (0);
526 while((*addr & 0x00800080) != 0x00800080){
527 if ((now=get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
528 printf ("Timeout on clearing Block Lock Bit\n");
529 *addr = 0xFFFFFFFF; /* reset bank */
537 #endif /* !CFG_NO_FLASH */