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 CFG_MONITOR_BASE >= CFG_FLASH_BASE
96 /* monitor protection ON by default */
97 flash_protect(FLAG_PROTECT_SET,
99 CFG_MONITOR_BASE+monitor_flash_len-1,
102 #ifdef CFG_ENV_IS_IN_FLASH
103 /* ENV protection ON by default */
104 flash_protect(FLAG_PROTECT_SET,
106 CFG_ENV_ADDR+CFG_ENV_SECT_SIZE-1,
113 /*-----------------------------------------------------------------------
115 void flash_print_info (flash_info_t *info)
119 if (info->flash_id == FLASH_UNKNOWN) {
120 printf ("missing or unknown FLASH type\n");
124 switch (info->flash_id & FLASH_VENDMASK) {
125 case FLASH_MAN_INTEL: printf ("Intel "); break;
126 case FLASH_MAN_SHARP: printf ("Sharp "); break;
127 default: printf ("Unknown Vendor "); break;
130 switch (info->flash_id & FLASH_TYPEMASK) {
131 case FLASH_28F640C3T: printf ("28F640C3T (64 Mbit x 2, 128 x 128k)\n");
133 default: printf ("Unknown Chip Type\n");
137 printf (" Size: %ld MB in %d Sectors\n",
138 info->size >> 20, info->sector_count);
140 printf (" Sector Start Addresses:");
141 for (i=0; i<info->sector_count; ++i) {
146 info->protect[i] ? " (RO)" : " "
153 * The following code cannot be run from FLASH!
156 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
160 ulong base = (ulong)addr;
164 printf("Check flash at 0x%08x\n",(uint)addr);
166 /* Write "Intelligent Identifier" command: read Manufacturer ID */
171 value = addr[0] & 0x00FF00FF;
174 printf("manufacturer=0x%x\n",(uint)value);
177 case MT_MANUFACT: /* SHARP, MT or => Intel */
179 info->flash_id = FLASH_MAN_INTEL;
182 printf("unknown manufacturer: %x\n", (unsigned int)value);
183 info->flash_id = FLASH_UNKNOWN;
184 info->sector_count = 0;
186 return (0); /* no or unknown flash */
189 value = addr[1]; /* device ID */
192 printf("deviceID=0x%x\n",(uint)value);
196 case (INTEL_ID_28F640C3T):
197 info->flash_id += FLASH_28F640C3T;
198 info->sector_count = 135;
199 info->size = 0x01000000;
200 sector_offset = 0x20000;
201 break; /* => 2x8 MB */
204 info->flash_id = FLASH_UNKNOWN;
205 return (0); /* => no or unknown flash */
209 /* set up sector start address table
210 * The first 127 blocks are large, the last 8 are small.
212 for (i = 0; i < 127; i++) {
213 info->start[i] = base;
214 base += sector_offset;
215 /* Sectors are locked upon reset */
216 info->protect[i] = 0;
218 for (i = 127; i < 135; i++) {
219 info->start[i] = base;
221 /* Sectors are locked upon reset */
222 info->protect[i] = 0;
227 * Prevent writes to uninitialized FLASH.
229 if (info->flash_id != FLASH_UNKNOWN) {
230 addr = (vu_long *)info->start[0];
231 *addr = 0xFFFFFF; /* reset bank to read array mode */
239 /*-----------------------------------------------------------------------
242 int flash_erase (flash_info_t *info, int s_first, int s_last)
244 int flag, prot, sect;
245 ulong start, now, last;
247 if ((s_first < 0) || (s_first > s_last)) {
248 if (info->flash_id == FLASH_UNKNOWN) {
249 printf ("- missing\n");
251 printf ("- no sectors to erase\n");
256 if ( ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL)
257 && ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_SHARP) ) {
258 printf ("Can't erase unknown flash type %08lx - aborted\n",
264 for (sect=s_first; sect<=s_last; ++sect) {
265 if (info->protect[sect]) {
271 printf ("- Warning: %d protected sectors will not be erased!\n",
278 printf("\nFlash Erase:\n");
280 /* Make Sure Block Lock Bit is not set. */
281 if(clear_block_lock_bit((vu_long *)(info->start[s_first]))){
285 /* Start erase on unprotected sectors */
287 printf("Begin to erase now,s_first=0x%x s_last=0x%x...\n",s_first,s_last);
289 for (sect = s_first; sect<=s_last; sect++) {
290 if (info->protect[sect] == 0) { /* not protected */
291 vu_long *addr = (vu_long *)(info->start[sect]);
294 last = start = get_timer (0);
296 /* Disable interrupts which might cause a timeout here */
297 flag = disable_interrupts();
302 /* Clear Status Register */
305 /* Single Block Erase Command */
312 if((info->flash_id & FLASH_TYPEMASK) != FLASH_LH28F016SCT) {
313 /* Resume Command, as per errata update */
318 /* re-enable interrupts if necessary */
322 /* wait at least 80us - let's wait 1 ms */
324 while ((*addr & 0x00800080) != 0x00800080) {
325 if(*addr & 0x00200020){
326 printf("Error in Block Erase - Lock Bit may be set!\n");
327 printf("Status Register = 0x%X\n", (uint)*addr);
328 *addr = 0xFFFFFFFF; /* reset bank */
332 if ((now=get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
333 printf ("Timeout\n");
334 *addr = 0xFFFFFFFF; /* reset bank */
338 /* show that we're waiting */
339 if ((now - last) > 1000) { /* every second */
345 /* reset to read mode */
351 printf ("flash erase done\n");
355 /*-----------------------------------------------------------------------
356 * Copy memory to flash, returns:
359 * 2 - Flash not erased
362 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
367 wp = (addr & ~3); /* get lower word aligned address */
370 * handle unaligned start bytes
372 if ((l = addr - wp) != 0) {
374 for (i=0, cp=wp; i<l; ++i, ++cp) {
375 data = (data << 8) | (*(uchar *)cp);
377 for (; i<4 && cnt>0; ++i) {
378 data = (data << 8) | *src++;
382 for (; cnt==0 && i<4; ++i, ++cp) {
383 data = (data << 8) | (*(uchar *)cp);
386 if ((rc = write_word(info, wp, data)) != 0) {
393 * handle word aligned part
397 for (i=0; i<4; ++i) {
398 data = (data << 8) | *src++;
400 if ((rc = write_word(info, wp, data)) != 0) {
412 * handle unaligned tail bytes
415 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
416 data = (data << 8) | *src++;
419 for (; i<4; ++i, ++cp) {
420 data = (data << 8) | (*(uchar *)cp);
423 return (write_word(info, wp, data));
426 /*-----------------------------------------------------------------------
427 * Write a word to Flash, returns:
430 * 2 - Flash not erased
432 static int write_word (flash_info_t *info, ulong dest, ulong data)
434 vu_long *addr = (vu_long *)dest;
438 /* Check if Flash is (sufficiently) erased */
439 if ((*addr & data) != data) {
442 /* Disable interrupts which might cause a timeout here */
443 flag = disable_interrupts();
452 /* re-enable interrupts if necessary */
456 /* data polling for D7 */
457 start = get_timer (0);
460 while (((csr = *addr) & 0x00800080) != 0x00800080) {
461 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
466 if (csr & 0x40404040) {
467 printf ("CSR indicates write error (%08lx) at %08lx\n", csr, (ulong)addr);
471 /* Clear Status Registers Command */
474 /* Reset to read array mode */
481 /*-----------------------------------------------------------------------
482 * Clear Block Lock Bit, returns:
487 static int clear_block_lock_bit(vu_long * addr)
494 /* Clear Status Register */
503 start = get_timer (0);
504 while((*addr & 0x00800080) != 0x00800080){
505 if ((now=get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
506 printf ("Timeout on clearing Block Lock Bit\n");
507 *addr = 0xFFFFFFFF; /* reset bank */
515 #endif /* !CFG_NO_FLASH */