2 * (C) Copyright 2000, 2001
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * (C) Copyright 2001, Stuart Hughes, Lineo Inc, stuarth@lineo.com
6 * Add support the Sharp chips on the mpc8260ads.
7 * I started with board/ip860/flash.c and made changes I found in
8 * the MTD project by David Schleef.
10 * See file CREDITS for list of people who contributed to this
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License as
15 * published by the Free Software Foundation; either version 2 of
16 * the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
34 #if defined(CFG_ENV_IS_IN_FLASH)
36 # define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
39 # define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
41 # ifndef CFG_ENV_SECT_SIZE
42 # define CFG_ENV_SECT_SIZE CFG_ENV_SIZE
46 /*-----------------------------------------------------------------------
49 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
50 static int write_word (flash_info_t *info, ulong dest, ulong data);
51 static int clear_block_lock_bit(vu_long * addr);
53 /*-----------------------------------------------------------------------
56 unsigned long flash_init (void)
58 #ifndef CONFIG_MPC8260ADS
59 volatile immap_t *immap = (immap_t *)CFG_IMMR;
60 volatile memctl8xx_t *memctl = &immap->im_memctl;
61 volatile ip860_bcsr_t *bcsr = (ip860_bcsr_t *)BCSR_BASE;
66 /* Init: enable write,
67 * or we cannot even write flash commands
69 #ifndef CONFIG_MPC8260ADS
70 bcsr->bd_ctrl |= BD_CTRL_FLWE;
73 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
74 flash_info[i].flash_id = FLASH_UNKNOWN;
76 /* set the default sector offset */
79 /* Static FLASH Bank configuration here - FIXME XXX */
81 size = flash_get_size((vu_long *)FLASH_BASE, &flash_info[0]);
83 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
84 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
87 #ifndef CONFIG_MPC8260ADS
88 /* Remap FLASH according to real size */
89 memctl->memc_or1 = CFG_OR_TIMING_FLASH | (-size & 0xFFFF8000);
90 memctl->memc_br1 = (CFG_FLASH_BASE & BR_BA_MSK) |
91 (memctl->memc_br1 & ~(BR_BA_MSK));
94 /* Re-do sizing to get full correct info */
95 size = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
97 flash_info[0].size = size;
99 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
100 /* monitor protection ON by default */
101 flash_protect(FLAG_PROTECT_SET,
103 CFG_MONITOR_BASE+CFG_MONITOR_LEN-1,
107 #ifdef CFG_ENV_IS_IN_FLASH
108 /* ENV protection ON by default */
109 flash_protect(FLAG_PROTECT_SET,
111 CFG_ENV_ADDR+CFG_ENV_SECT_SIZE-1,
117 /*-----------------------------------------------------------------------
119 void flash_print_info (flash_info_t *info)
123 if (info->flash_id == FLASH_UNKNOWN) {
124 printf ("missing or unknown FLASH type\n");
128 switch (info->flash_id & FLASH_VENDMASK) {
129 case FLASH_MAN_INTEL: printf ("Intel "); break;
130 case FLASH_MAN_SHARP: printf ("Sharp "); break;
131 default: printf ("Unknown Vendor "); break;
134 switch (info->flash_id & FLASH_TYPEMASK) {
135 case FLASH_28F016SV: printf ("28F016SV (16 Mbit, 32 x 64k)\n");
137 case FLASH_28F160S3: printf ("28F160S3 (16 Mbit, 32 x 512K)\n");
139 case FLASH_28F320S3: printf ("28F320S3 (32 Mbit, 64 x 512K)\n");
141 case FLASH_LH28F016SCT: printf ("28F016SC (16 Mbit, 32 x 64K)\n");
143 default: printf ("Unknown Chip Type\n");
147 printf (" Size: %ld MB in %d Sectors\n",
148 info->size >> 20, info->sector_count);
150 printf (" Sector Start Addresses:");
151 for (i=0; i<info->sector_count; ++i) {
156 info->protect[i] ? " (RO)" : " "
162 /*-----------------------------------------------------------------------
166 /*-----------------------------------------------------------------------
170 * The following code cannot be run from FLASH!
173 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
177 ulong base = (ulong)addr;
180 /* Write "Intelligent Identifier" command: read Manufacturer ID */
183 value = addr[0] & 0x00FF00FF;
185 case MT_MANUFACT: /* SHARP, MT or => Intel */
187 info->flash_id = FLASH_MAN_INTEL;
190 printf("unknown manufacturer: %x\n", (unsigned int)value);
191 info->flash_id = FLASH_UNKNOWN;
192 info->sector_count = 0;
194 return (0); /* no or unknown flash */
197 value = addr[1]; /* device ID */
200 case (INTEL_ID_28F016S):
201 info->flash_id += FLASH_28F016SV;
202 info->sector_count = 32;
203 info->size = 0x00400000;
204 sector_offset = 0x20000;
205 break; /* => 2x2 MB */
207 case (INTEL_ID_28F160S3):
208 info->flash_id += FLASH_28F160S3;
209 info->sector_count = 32;
210 info->size = 0x00400000;
211 sector_offset = 0x20000;
212 break; /* => 2x2 MB */
214 case (INTEL_ID_28F320S3):
215 info->flash_id += FLASH_28F320S3;
216 info->sector_count = 64;
217 info->size = 0x00800000;
218 sector_offset = 0x20000;
219 break; /* => 2x4 MB */
221 case SHARP_ID_28F016SCL:
222 case SHARP_ID_28F016SCZ:
223 info->flash_id = FLASH_MAN_SHARP | FLASH_LH28F016SCT;
224 info->sector_count = 32;
225 info->size = 0x00800000;
226 sector_offset = 0x40000;
227 break; /* => 4x2 MB */
231 info->flash_id = FLASH_UNKNOWN;
232 return (0); /* => no or unknown flash */
236 /* set up sector start address table */
237 for (i = 0; i < info->sector_count; i++) {
238 info->start[i] = base;
239 base += sector_offset;
240 /* don't know how to check sector protection */
241 info->protect[i] = 0;
245 * Prevent writes to uninitialized FLASH.
247 if (info->flash_id != FLASH_UNKNOWN) {
248 addr = (vu_long *)info->start[0];
250 *addr = 0xFFFFFF; /* reset bank to read array mode */
257 /*-----------------------------------------------------------------------
260 int flash_erase (flash_info_t *info, int s_first, int s_last)
262 int flag, prot, sect;
263 ulong start, now, last;
265 if ((s_first < 0) || (s_first > s_last)) {
266 if (info->flash_id == FLASH_UNKNOWN) {
267 printf ("- missing\n");
269 printf ("- no sectors to erase\n");
274 if ( ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL)
275 && ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_SHARP) ) {
276 printf ("Can't erase unknown flash type %08lx - aborted\n",
282 for (sect=s_first; sect<=s_last; ++sect) {
283 if (info->protect[sect]) {
289 printf ("- Warning: %d protected sectors will not be erased!\n",
295 /* Make Sure Block Lock Bit is not set. */
296 if(clear_block_lock_bit((vu_long *)(info->start[s_first]))){
301 /* Start erase on unprotected sectors */
302 for (sect = s_first; sect<=s_last; sect++) {
303 if (info->protect[sect] == 0) { /* not protected */
304 vu_long *addr = (vu_long *)(info->start[sect]);
306 last = start = get_timer (0);
308 /* Disable interrupts which might cause a timeout here */
309 flag = disable_interrupts();
313 /* Clear Status Register */
315 /* Single Block Erase Command */
320 if((info->flash_id & FLASH_TYPEMASK) != FLASH_LH28F016SCT) {
321 /* Resume Command, as per errata update */
325 /* re-enable interrupts if necessary */
329 /* wait at least 80us - let's wait 1 ms */
331 while ((*addr & 0x80808080) != 0x80808080) {
332 if(*addr & 0x20202020){
333 printf("Error in Block Erase - Lock Bit may be set!\n");
334 printf("Status Register = 0x%X\n", (uint)*addr);
335 *addr = 0xFFFFFFFF; /* reset bank */
338 if ((now=get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
339 printf ("Timeout\n");
340 *addr = 0xFFFFFFFF; /* reset bank */
343 /* show that we're waiting */
344 if ((now - last) > 1000) { /* every second */
350 /* reset to read mode */
359 /*-----------------------------------------------------------------------
360 * Copy memory to flash, returns:
363 * 2 - Flash not erased
366 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
371 wp = (addr & ~3); /* get lower word aligned address */
374 * handle unaligned start bytes
376 if ((l = addr - wp) != 0) {
378 for (i=0, cp=wp; i<l; ++i, ++cp) {
379 data = (data << 8) | (*(uchar *)cp);
381 for (; i<4 && cnt>0; ++i) {
382 data = (data << 8) | *src++;
386 for (; cnt==0 && i<4; ++i, ++cp) {
387 data = (data << 8) | (*(uchar *)cp);
390 if ((rc = write_word(info, wp, data)) != 0) {
397 * handle word aligned part
401 for (i=0; i<4; ++i) {
402 data = (data << 8) | *src++;
404 if ((rc = write_word(info, wp, data)) != 0) {
416 * handle unaligned tail bytes
419 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
420 data = (data << 8) | *src++;
423 for (; i<4; ++i, ++cp) {
424 data = (data << 8) | (*(uchar *)cp);
427 return (write_word(info, wp, data));
430 /*-----------------------------------------------------------------------
431 * Write a word to Flash, returns:
434 * 2 - Flash not erased
436 static int write_word (flash_info_t *info, ulong dest, ulong data)
438 vu_long *addr = (vu_long *)dest;
442 /* Check if Flash is (sufficiently) erased */
443 if ((*addr & data) != data) {
446 /* Disable interrupts which might cause a timeout here */
447 flag = disable_interrupts();
455 /* re-enable interrupts if necessary */
459 /* data polling for D7 */
460 start = get_timer (0);
462 while (((csr = *addr) & 0x80808080) != 0x80808080) {
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 */
475 /* Reset to read array mode */
481 /*-----------------------------------------------------------------------
482 * Clear Block Lock Bit, returns:
487 static int clear_block_lock_bit(vu_long * addr)
493 /* Clear Status Register */
499 start = get_timer (0);
500 while(*addr != 0x80808080){
501 if ((now=get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
502 printf ("Timeout on clearing Block Lock Bit\n");
503 *addr = 0xFFFFFFFF; /* reset bank */