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 * (C) Copyright 2003 Arabella Software Ltd.
11 * Yuli Barcohen <yuli@arabellasw.com>
12 * Re-written to support multi-bank flash SIMMs.
13 * Added support for real protection and JFFS2.
15 * See file CREDITS for list of people who contributed to this
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License as
20 * published by the Free Software Foundation; either version 2 of
21 * the License, or (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
36 /* Intel-compatible flash ID */
37 #define INTEL_COMPAT 0x89898989
38 #define INTEL_ALT 0xB0B0B0B0
40 /* Intel-compatible flash commands */
41 #define INTEL_PROGRAM 0x10101010
42 #define INTEL_ERASE 0x20202020
43 #define INTEL_CLEAR 0x50505050
44 #define INTEL_LOCKBIT 0x60606060
45 #define INTEL_PROTECT 0x01010101
46 #define INTEL_STATUS 0x70707070
47 #define INTEL_READID 0x90909090
48 #define INTEL_CONFIRM 0xD0D0D0D0
49 #define INTEL_RESET 0xFFFFFFFF
51 /* Intel-compatible flash status bits */
52 #define INTEL_FINISHED 0x80808080
53 #define INTEL_OK 0x80808080
55 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
57 /*-----------------------------------------------------------------------
58 * This board supports 32-bit wide flash SIMMs (4x8-bit configuration.)
59 * Up to 32MB of flash supported (up to 4 banks.)
60 * BCSR is used for flash presence detect (page 4-65 of the User's Manual)
62 * The following code can not run from flash!
64 unsigned long flash_init (void)
66 ulong size = 0, sect_start, sect_size = 0, bank_size;
67 ushort sect_count = 0;
69 vu_long *addr = (vu_long *)CONFIG_SYS_FLASH_BASE;
70 vu_long *bcsr = (vu_long *)CONFIG_SYS_BCSR;
72 switch (bcsr[2] & 0xF) {
82 default: /* Unsupported configurations */
83 nbanks = CONFIG_SYS_MAX_FLASH_BANKS;
86 if (nbanks > CONFIG_SYS_MAX_FLASH_BANKS)
87 nbanks = CONFIG_SYS_MAX_FLASH_BANKS;
89 for (i = 0; i < nbanks; i++) {
90 *addr = INTEL_READID; /* Read Intelligent Identifier */
91 if ((addr[0] == INTEL_COMPAT) || (addr[0] == INTEL_ALT)) {
93 case SHARP_ID_28F016SCL:
94 case SHARP_ID_28F016SCZ:
95 flash_info[i].flash_id = FLASH_MAN_SHARP | FLASH_LH28F016SCT;
100 flash_info[i].flash_id = FLASH_UNKNOWN;
101 sect_count = CONFIG_SYS_MAX_FLASH_SECT;
103 CONFIG_SYS_FLASH_SIZE / CONFIG_SYS_MAX_FLASH_BANKS / CONFIG_SYS_MAX_FLASH_SECT;
107 flash_info[i].flash_id = FLASH_UNKNOWN;
108 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
109 printf("### Unknown flash ID %08lX %08lX at address %08lX ###\n",
110 addr[0], addr[1], (ulong)addr);
112 *addr = INTEL_RESET; /* Reset bank to Read Array mode */
115 flash_info[i].sector_count = sect_count;
116 flash_info[i].size = bank_size = sect_size * sect_count;
118 sect_start = (ulong)addr;
119 for (j = 0; j < sect_count; j++) {
120 addr = (vu_long *)sect_start;
121 flash_info[i].start[j] = sect_start;
122 flash_info[i].protect[j] = (addr[2] == 0x01010101);
123 sect_start += sect_size;
125 *addr = INTEL_RESET; /* Reset bank to Read Array mode */
126 addr = (vu_long *)sect_start;
129 if (size == 0) { /* Unknown flash, fill with hard-coded values */
130 sect_start = CONFIG_SYS_FLASH_BASE;
131 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
132 flash_info[i].flash_id = FLASH_UNKNOWN;
133 flash_info[i].size = CONFIG_SYS_FLASH_SIZE / CONFIG_SYS_MAX_FLASH_BANKS;
134 flash_info[i].sector_count = sect_count;
135 for (j = 0; j < sect_count; j++) {
136 flash_info[i].start[j] = sect_start;
137 flash_info[i].protect[j] = 0;
138 sect_start += sect_size;
141 size = CONFIG_SYS_FLASH_SIZE;
144 for (i = nbanks; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
145 flash_info[i].flash_id = FLASH_UNKNOWN;
146 flash_info[i].size = 0;
147 flash_info[i].sector_count = 0;
150 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
151 /* monitor protection ON by default */
152 flash_protect(FLAG_PROTECT_SET,
153 CONFIG_SYS_MONITOR_BASE,
154 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
158 #ifdef CONFIG_ENV_IS_IN_FLASH
159 /* ENV protection ON by default */
160 flash_protect(FLAG_PROTECT_SET,
162 CONFIG_ENV_ADDR+CONFIG_ENV_SECT_SIZE-1,
168 /*-----------------------------------------------------------------------
170 void flash_print_info (flash_info_t *info)
174 if (info->flash_id == FLASH_UNKNOWN) {
175 printf ("missing or unknown FLASH type\n");
179 switch (info->flash_id & FLASH_VENDMASK) {
180 case FLASH_MAN_INTEL: printf ("Intel "); break;
181 case FLASH_MAN_SHARP: printf ("Sharp "); break;
182 default: printf ("Unknown Vendor "); break;
185 switch (info->flash_id & FLASH_TYPEMASK) {
186 case FLASH_28F016SV: printf ("28F016SV (16 Mbit, 32 x 64k)\n");
188 case FLASH_28F160S3: printf ("28F160S3 (16 Mbit, 32 x 512K)\n");
190 case FLASH_28F320S3: printf ("28F320S3 (32 Mbit, 64 x 512K)\n");
192 case FLASH_LH28F016SCT: printf ("28F016SC (16 Mbit, 32 x 64K)\n");
194 default: printf ("Unknown Chip Type\n");
198 printf (" Size: %ld MB in %d Sectors\n",
199 info->size >> 20, info->sector_count);
201 printf (" Sector Start Addresses:");
202 for (i=0; i<info->sector_count; ++i) {
207 info->protect[i] ? " (RO)" : " "
213 /*-----------------------------------------------------------------------
215 int flash_erase (flash_info_t *info, int s_first, int s_last)
217 int flag, prot, sect;
218 ulong start, now, last;
220 if ((s_first < 0) || (s_first > s_last)) {
221 if (info->flash_id == FLASH_UNKNOWN) {
222 printf ("- missing\n");
224 printf ("- no sectors to erase\n");
229 if ( ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL)
230 && ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_SHARP) ) {
231 printf ("Can't erase unknown flash type %08lx - aborted\n",
237 for (sect=s_first; sect<=s_last; ++sect) {
238 if (info->protect[sect]) {
244 printf ("- Warning: %d protected sectors will not be erased!\n",
250 /* Start erase on unprotected sectors */
251 for (sect = s_first; sect<=s_last; sect++) {
252 if (info->protect[sect] == 0) { /* not protected */
253 vu_long *addr = (vu_long *)(info->start[sect]);
255 last = start = get_timer (0);
257 /* Disable interrupts which might cause a timeout here */
258 flag = disable_interrupts();
260 /* Clear Status Register */
262 /* Single Block Erase Command */
265 *addr = INTEL_CONFIRM;
267 if((info->flash_id & FLASH_TYPEMASK) != FLASH_LH28F016SCT) {
268 /* Resume Command, as per errata update */
269 *addr = INTEL_CONFIRM;
272 /* re-enable interrupts if necessary */
276 while ((*addr & INTEL_FINISHED) != INTEL_FINISHED) {
277 if ((now=get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
278 printf ("Timeout\n");
279 *addr = INTEL_RESET; /* reset bank */
282 /* show that we're waiting */
283 if ((now - last) > 1000) { /* every second */
289 if (*addr != INTEL_OK) {
290 printf("Block erase failed at %08X, CSR=%08X\n",
291 (uint)addr, (uint)*addr);
292 *addr = INTEL_RESET; /* reset bank */
296 /* reset to read mode */
305 /*-----------------------------------------------------------------------
306 * Write a word to Flash, returns:
309 * 2 - Flash not erased
311 static int write_word (flash_info_t *info, ulong dest, ulong data)
316 vu_long *addr = (vu_long *)dest;
318 /* Check if Flash is (sufficiently) erased */
319 if ((*addr & data) != data) {
323 *addr = INTEL_CLEAR; /* Clear status register */
325 /* Disable interrupts which might cause a timeout here */
326 flag = disable_interrupts();
329 *addr = INTEL_PROGRAM;
334 /* re-enable interrupts if necessary */
338 /* data polling for D7 */
339 start = get_timer (0);
340 while ((*addr & INTEL_FINISHED) != INTEL_FINISHED) {
341 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
342 printf("Write timed out\n");
347 if (*addr != INTEL_OK) {
348 printf ("Write failed at %08X, CSR=%08X\n", (uint)addr, (uint)*addr);
352 *addr = INTEL_RESET; /* Reset to read array mode */
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 */
371 *(vu_long *)wp = INTEL_RESET; /* Reset to read array mode */
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 rc = write_word(info, wp, data);
432 /*-----------------------------------------------------------------------
433 * Set/Clear sector's lock bit, returns:
435 * 1 - Error (timeout, voltage problems, etc.)
437 int flash_real_protect(flash_info_t *info, long sector, int prot)
442 vu_long *addr = (vu_long *)(info->start[sector]);
443 int flag = disable_interrupts();
445 *addr = INTEL_CLEAR; /* Clear status register */
446 if (prot) { /* Set sector lock bit */
447 *addr = INTEL_LOCKBIT; /* Sector lock bit */
448 *addr = INTEL_PROTECT; /* set */
450 else { /* Clear sector lock bit */
451 *addr = INTEL_LOCKBIT; /* All sectors lock bits */
452 *addr = INTEL_CONFIRM; /* clear */
455 start = get_timer(0);
456 while ((*addr & INTEL_FINISHED) != INTEL_FINISHED) {
457 if (get_timer(start) > CONFIG_SYS_FLASH_UNLOCK_TOUT) {
458 printf("Flash lock bit operation timed out\n");
464 if (*addr != INTEL_OK) {
465 printf("Flash lock bit operation failed at %08X, CSR=%08X\n",
466 (uint)addr, (uint)*addr);
471 info->protect[sector] = prot;
474 * Clear lock bit command clears all sectors lock bits, so
475 * we have to restore lock bits of protected sectors.
478 for (i = 0; i < info->sector_count; i++)
479 if (info->protect[i]) {
480 addr = (vu_long *)(info->start[i]);
481 *addr = INTEL_LOCKBIT; /* Sector lock bit */
482 *addr = INTEL_PROTECT; /* set */
483 udelay(CONFIG_SYS_FLASH_LOCK_TOUT * 1000);
489 *addr = INTEL_RESET; /* Reset to read array mode */