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 * SPDX-License-Identifier: GPL-2.0+
20 /* Intel-compatible flash ID */
21 #define INTEL_COMPAT 0x89898989
22 #define INTEL_ALT 0xB0B0B0B0
24 /* Intel-compatible flash commands */
25 #define INTEL_PROGRAM 0x10101010
26 #define INTEL_ERASE 0x20202020
27 #define INTEL_CLEAR 0x50505050
28 #define INTEL_LOCKBIT 0x60606060
29 #define INTEL_PROTECT 0x01010101
30 #define INTEL_STATUS 0x70707070
31 #define INTEL_READID 0x90909090
32 #define INTEL_CONFIRM 0xD0D0D0D0
33 #define INTEL_RESET 0xFFFFFFFF
35 /* Intel-compatible flash status bits */
36 #define INTEL_FINISHED 0x80808080
37 #define INTEL_OK 0x80808080
39 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
41 /*-----------------------------------------------------------------------
42 * This board supports 32-bit wide flash SIMMs (4x8-bit configuration.)
43 * Up to 32MB of flash supported (up to 4 banks.)
44 * BCSR is used for flash presence detect (page 4-65 of the User's Manual)
46 * The following code can not run from flash!
48 unsigned long flash_init (void)
50 ulong size = 0, sect_start, sect_size = 0, bank_size;
51 ushort sect_count = 0;
53 vu_long *addr = (vu_long *)CONFIG_SYS_FLASH_BASE;
54 vu_long *bcsr = (vu_long *)CONFIG_SYS_BCSR;
56 switch (bcsr[2] & 0xF) {
66 default: /* Unsupported configurations */
67 nbanks = CONFIG_SYS_MAX_FLASH_BANKS;
70 if (nbanks > CONFIG_SYS_MAX_FLASH_BANKS)
71 nbanks = CONFIG_SYS_MAX_FLASH_BANKS;
73 for (i = 0; i < nbanks; i++) {
74 *addr = INTEL_READID; /* Read Intelligent Identifier */
75 if ((addr[0] == INTEL_COMPAT) || (addr[0] == INTEL_ALT)) {
77 case SHARP_ID_28F016SCL:
78 case SHARP_ID_28F016SCZ:
79 flash_info[i].flash_id = FLASH_MAN_SHARP | FLASH_LH28F016SCT;
84 flash_info[i].flash_id = FLASH_UNKNOWN;
85 sect_count = CONFIG_SYS_MAX_FLASH_SECT;
87 CONFIG_SYS_FLASH_SIZE / CONFIG_SYS_MAX_FLASH_BANKS / CONFIG_SYS_MAX_FLASH_SECT;
91 flash_info[i].flash_id = FLASH_UNKNOWN;
92 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
93 printf("### Unknown flash ID %08lX %08lX at address %08lX ###\n",
94 addr[0], addr[1], (ulong)addr);
96 *addr = INTEL_RESET; /* Reset bank to Read Array mode */
99 flash_info[i].sector_count = sect_count;
100 flash_info[i].size = bank_size = sect_size * sect_count;
102 sect_start = (ulong)addr;
103 for (j = 0; j < sect_count; j++) {
104 addr = (vu_long *)sect_start;
105 flash_info[i].start[j] = sect_start;
106 flash_info[i].protect[j] = (addr[2] == 0x01010101);
107 sect_start += sect_size;
109 *addr = INTEL_RESET; /* Reset bank to Read Array mode */
110 addr = (vu_long *)sect_start;
113 if (size == 0) { /* Unknown flash, fill with hard-coded values */
114 sect_start = CONFIG_SYS_FLASH_BASE;
115 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
116 flash_info[i].flash_id = FLASH_UNKNOWN;
117 flash_info[i].size = CONFIG_SYS_FLASH_SIZE / CONFIG_SYS_MAX_FLASH_BANKS;
118 flash_info[i].sector_count = sect_count;
119 for (j = 0; j < sect_count; j++) {
120 flash_info[i].start[j] = sect_start;
121 flash_info[i].protect[j] = 0;
122 sect_start += sect_size;
125 size = CONFIG_SYS_FLASH_SIZE;
128 for (i = nbanks; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
129 flash_info[i].flash_id = FLASH_UNKNOWN;
130 flash_info[i].size = 0;
131 flash_info[i].sector_count = 0;
134 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
135 /* monitor protection ON by default */
136 flash_protect(FLAG_PROTECT_SET,
137 CONFIG_SYS_MONITOR_BASE,
138 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
142 #ifdef CONFIG_ENV_IS_IN_FLASH
143 /* ENV protection ON by default */
144 flash_protect(FLAG_PROTECT_SET,
146 CONFIG_ENV_ADDR+CONFIG_ENV_SECT_SIZE-1,
152 /*-----------------------------------------------------------------------
154 void flash_print_info (flash_info_t *info)
158 if (info->flash_id == FLASH_UNKNOWN) {
159 printf ("missing or unknown FLASH type\n");
163 switch (info->flash_id & FLASH_VENDMASK) {
164 case FLASH_MAN_INTEL: printf ("Intel "); break;
165 case FLASH_MAN_SHARP: printf ("Sharp "); break;
166 default: printf ("Unknown Vendor "); break;
169 switch (info->flash_id & FLASH_TYPEMASK) {
170 case FLASH_28F016SV: printf ("28F016SV (16 Mbit, 32 x 64k)\n");
172 case FLASH_28F160S3: printf ("28F160S3 (16 Mbit, 32 x 512K)\n");
174 case FLASH_28F320S3: printf ("28F320S3 (32 Mbit, 64 x 512K)\n");
176 case FLASH_LH28F016SCT: printf ("28F016SC (16 Mbit, 32 x 64K)\n");
178 default: printf ("Unknown Chip Type\n");
182 printf (" Size: %ld MB in %d Sectors\n",
183 info->size >> 20, info->sector_count);
185 printf (" Sector Start Addresses:");
186 for (i=0; i<info->sector_count; ++i) {
191 info->protect[i] ? " (RO)" : " "
197 /*-----------------------------------------------------------------------
199 int flash_erase (flash_info_t *info, int s_first, int s_last)
201 int flag, prot, sect;
202 ulong start, now, last;
204 if ((s_first < 0) || (s_first > s_last)) {
205 if (info->flash_id == FLASH_UNKNOWN) {
206 printf ("- missing\n");
208 printf ("- no sectors to erase\n");
213 if ( ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL)
214 && ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_SHARP) ) {
215 printf ("Can't erase unknown flash type %08lx - aborted\n",
221 for (sect=s_first; sect<=s_last; ++sect) {
222 if (info->protect[sect]) {
228 printf ("- Warning: %d protected sectors will not be erased!\n",
234 /* Start erase on unprotected sectors */
235 for (sect = s_first; sect<=s_last; sect++) {
236 if (info->protect[sect] == 0) { /* not protected */
237 vu_long *addr = (vu_long *)(info->start[sect]);
239 last = start = get_timer (0);
241 /* Disable interrupts which might cause a timeout here */
242 flag = disable_interrupts();
244 /* Clear Status Register */
246 /* Single Block Erase Command */
249 *addr = INTEL_CONFIRM;
251 if((info->flash_id & FLASH_TYPEMASK) != FLASH_LH28F016SCT) {
252 /* Resume Command, as per errata update */
253 *addr = INTEL_CONFIRM;
256 /* re-enable interrupts if necessary */
260 while ((*addr & INTEL_FINISHED) != INTEL_FINISHED) {
261 if ((now=get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
262 printf ("Timeout\n");
263 *addr = INTEL_RESET; /* reset bank */
266 /* show that we're waiting */
267 if ((now - last) > 1000) { /* every second */
273 if (*addr != INTEL_OK) {
274 printf("Block erase failed at %08X, CSR=%08X\n",
275 (uint)addr, (uint)*addr);
276 *addr = INTEL_RESET; /* reset bank */
280 /* reset to read mode */
289 /*-----------------------------------------------------------------------
290 * Write a word to Flash, returns:
293 * 2 - Flash not erased
295 static int write_word (flash_info_t *info, ulong dest, ulong data)
300 vu_long *addr = (vu_long *)dest;
302 /* Check if Flash is (sufficiently) erased */
303 if ((*addr & data) != data) {
307 *addr = INTEL_CLEAR; /* Clear status register */
309 /* Disable interrupts which might cause a timeout here */
310 flag = disable_interrupts();
313 *addr = INTEL_PROGRAM;
318 /* re-enable interrupts if necessary */
322 /* data polling for D7 */
323 start = get_timer (0);
324 while ((*addr & INTEL_FINISHED) != INTEL_FINISHED) {
325 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
326 printf("Write timed out\n");
331 if (*addr != INTEL_OK) {
332 printf ("Write failed at %08X, CSR=%08X\n", (uint)addr, (uint)*addr);
336 *addr = INTEL_RESET; /* Reset to read array mode */
341 /*-----------------------------------------------------------------------
342 * Copy memory to flash, returns:
345 * 2 - Flash not erased
348 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
353 wp = (addr & ~3); /* get lower word aligned address */
355 *(vu_long *)wp = INTEL_RESET; /* Reset to read array mode */
358 * handle unaligned start bytes
360 if ((l = addr - wp) != 0) {
362 for (i=0, cp=wp; i<l; ++i, ++cp) {
363 data = (data << 8) | (*(uchar *)cp);
365 for (; i<4 && cnt>0; ++i) {
366 data = (data << 8) | *src++;
370 for (; cnt==0 && i<4; ++i, ++cp) {
371 data = (data << 8) | (*(uchar *)cp);
374 if ((rc = write_word(info, wp, data)) != 0) {
381 * handle word aligned part
385 for (i=0; i<4; ++i) {
386 data = (data << 8) | *src++;
388 if ((rc = write_word(info, wp, data)) != 0) {
400 * handle unaligned tail bytes
403 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
404 data = (data << 8) | *src++;
407 for (; i<4; ++i, ++cp) {
408 data = (data << 8) | (*(uchar *)cp);
411 rc = write_word(info, wp, data);
416 /*-----------------------------------------------------------------------
417 * Set/Clear sector's lock bit, returns:
419 * 1 - Error (timeout, voltage problems, etc.)
421 int flash_real_protect(flash_info_t *info, long sector, int prot)
426 vu_long *addr = (vu_long *)(info->start[sector]);
427 int flag = disable_interrupts();
429 *addr = INTEL_CLEAR; /* Clear status register */
430 if (prot) { /* Set sector lock bit */
431 *addr = INTEL_LOCKBIT; /* Sector lock bit */
432 *addr = INTEL_PROTECT; /* set */
434 else { /* Clear sector lock bit */
435 *addr = INTEL_LOCKBIT; /* All sectors lock bits */
436 *addr = INTEL_CONFIRM; /* clear */
439 start = get_timer(0);
440 while ((*addr & INTEL_FINISHED) != INTEL_FINISHED) {
441 if (get_timer(start) > CONFIG_SYS_FLASH_UNLOCK_TOUT) {
442 printf("Flash lock bit operation timed out\n");
448 if (*addr != INTEL_OK) {
449 printf("Flash lock bit operation failed at %08X, CSR=%08X\n",
450 (uint)addr, (uint)*addr);
455 info->protect[sector] = prot;
458 * Clear lock bit command clears all sectors lock bits, so
459 * we have to restore lock bits of protected sectors.
462 for (i = 0; i < info->sector_count; i++)
463 if (info->protect[i]) {
464 addr = (vu_long *)(info->start[i]);
465 *addr = INTEL_LOCKBIT; /* Sector lock bit */
466 *addr = INTEL_PROTECT; /* set */
467 udelay(CONFIG_SYS_FLASH_LOCK_TOUT * 1000);
473 *addr = INTEL_RESET; /* Reset to read array mode */