3 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 * See file CREDITS for list of people who contributed to this
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
32 #if defined(CFG_ENV_IS_IN_FLASH)
34 # define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
37 # define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
39 # ifndef CFG_ENV_SECT_SIZE
40 # define CFG_ENV_SECT_SIZE CFG_ENV_SIZE
44 /*-----------------------------------------------------------------------
47 #define FLAG_PROTECT_SET 0x01
48 #define FLAG_PROTECT_CLEAR 0x02
50 /* Board support for 1 or 2 flash devices */
51 #undef FLASH_PORT_WIDTH32
52 #define FLASH_PORT_WIDTH16
54 #ifdef FLASH_PORT_WIDTH16
55 #define FLASH_PORT_WIDTH ushort
56 #define FLASH_PORT_WIDTHV vu_short
58 #define FLASH_PORT_WIDTH ulong
59 #define FLASH_PORT_WIDTHV vu_long
62 #define FPW FLASH_PORT_WIDTH
63 #define FPWV FLASH_PORT_WIDTHV
65 /*-----------------------------------------------------------------------
68 static ulong flash_get_size (FPW *addr, flash_info_t *info);
69 static int write_data (flash_info_t *info, ulong dest, FPW data);
70 static void flash_get_offsets (ulong base, flash_info_t *info);
72 /*-----------------------------------------------------------------------
75 unsigned long flash_init (void)
77 volatile immap_t *immap = (immap_t *)CFG_IMMR;
78 volatile memctl8xx_t *memctl = &immap->im_memctl;
79 unsigned long size_b0;
82 /* Init: no FLASHes known */
83 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
84 flash_info[i].flash_id = FLASH_UNKNOWN;
87 /* Static FLASH Bank configuration here - FIXME XXX */
88 size_b0 = flash_get_size((FPW *)FLASH_BASE0_PRELIM, &flash_info[0]);
90 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
91 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
92 size_b0, size_b0<<20);
95 /* Remap FLASH according to real size */
96 memctl->memc_or0 = CFG_OR_TIMING_FLASH | (-size_b0 & 0xFFFF8000);
97 memctl->memc_br0 = (CFG_FLASH_BASE & BR_BA_MSK) | BR_PS_16 | BR_MS_GPCM | BR_V;
99 /* Re-do sizing to get full correct info */
100 size_b0 = flash_get_size((FPW *)CFG_FLASH_BASE, &flash_info[0]);
102 flash_get_offsets (CFG_FLASH_BASE, &flash_info[0]);
104 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
105 /* monitor protection ON by default */
106 (void)flash_protect(FLAG_PROTECT_SET,
108 CFG_FLASH_BASE+CFG_MONITOR_LEN-1,
112 #ifdef CFG_ENV_IS_IN_FLASH
113 /* ENV protection ON by default */
114 flash_protect(FLAG_PROTECT_SET,
116 CFG_ENV_ADDR+CFG_ENV_SIZE-1,
120 flash_info[0].size = size_b0;
125 /*-----------------------------------------------------------------------
127 static void flash_get_offsets (ulong base, flash_info_t *info)
131 if (info->flash_id == FLASH_UNKNOWN) {
135 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
136 for (i = 0; i < info->sector_count; i++) {
137 info->start[i] = base + (i * 0x00020000);
142 /*-----------------------------------------------------------------------
144 void flash_print_info (flash_info_t *info)
148 if (info->flash_id == FLASH_UNKNOWN) {
149 printf ("missing or unknown FLASH type\n");
153 switch (info->flash_id & FLASH_VENDMASK) {
154 case FLASH_MAN_INTEL: printf ("INTEL "); break;
155 default: printf ("Unknown Vendor "); break;
158 switch (info->flash_id & FLASH_TYPEMASK) {
159 case FLASH_28F320J3A:
160 printf ("28F320J3A\n"); break;
161 case FLASH_28F640J3A:
162 printf ("28F640J3A\n"); break;
163 case FLASH_28F128J3A:
164 printf ("28F128J3A\n"); break;
165 default: printf ("Unknown Chip Type\n"); break;
168 printf (" Size: %ld MB in %d Sectors\n",
169 info->size >> 20, info->sector_count);
171 printf (" Sector Start Addresses:");
172 for (i=0; i<info->sector_count; ++i) {
177 info->protect[i] ? " (RO)" : " "
184 /*-----------------------------------------------------------------------
188 /*-----------------------------------------------------------------------
192 * The following code cannot be run from FLASH!
195 static ulong flash_get_size (FPW *addr, flash_info_t *info)
199 /* Write auto select command: read Manufacturer ID */
200 addr[0x5555] = (FPW)0x00AA00AA;
201 addr[0x2AAA] = (FPW)0x00550055;
202 addr[0x5555] = (FPW)0x00900090;
207 case (FPW)INTEL_MANUFACT:
208 info->flash_id = FLASH_MAN_INTEL;
211 info->flash_id = FLASH_UNKNOWN;
212 info->sector_count = 0;
214 addr[0] = (FPW)0x00FF00FF; /* restore read mode */
215 return (0); /* no or unknown flash */
218 value = addr[1]; /* device ID */
221 case (FPW)INTEL_ID_28F320J3A:
222 info->flash_id += FLASH_28F320J3A;
223 info->sector_count = 32;
224 info->size = 0x00400000;
227 case (FPW)INTEL_ID_28F640J3A:
228 info->flash_id += FLASH_28F640J3A;
229 info->sector_count = 64;
230 info->size = 0x00800000;
233 case (FPW)INTEL_ID_28F128J3A:
234 info->flash_id += FLASH_28F128J3A;
235 info->sector_count = 128;
236 info->size = 0x01000000;
237 break; /* => 16 MB */
240 info->flash_id = FLASH_UNKNOWN;
244 if (info->sector_count > CFG_MAX_FLASH_SECT) {
245 printf ("** ERROR: sector count %d > max (%d) **\n",
246 info->sector_count, CFG_MAX_FLASH_SECT);
247 info->sector_count = CFG_MAX_FLASH_SECT;
250 addr[0] = (FPW)0x00FF00FF; /* restore read mode */
256 /*-----------------------------------------------------------------------
259 int flash_erase (flash_info_t *info, int s_first, int s_last)
261 int flag, prot, sect;
262 ulong type, 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 type = (info->flash_id & FLASH_VENDMASK);
275 if ((type != FLASH_MAN_INTEL)) {
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 start = get_timer (0);
297 /* Start erase on unprotected sectors */
298 for (sect = s_first; sect<=s_last; sect++) {
299 if (info->protect[sect] == 0) { /* not protected */
300 FPWV *addr = (FPWV *)(info->start[sect]);
303 /* Disable interrupts which might cause a timeout here */
304 flag = disable_interrupts();
306 *addr = (FPW)0x00500050; /* clear status register */
307 *addr = (FPW)0x00200020; /* erase setup */
308 *addr = (FPW)0x00D000D0; /* erase confirm */
310 /* re-enable interrupts if necessary */
314 /* wait at least 80us - let's wait 1 ms */
317 while (((status = *addr) & (FPW)0x00800080) != (FPW)0x00800080) {
318 if ((now=get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
319 printf ("Timeout\n");
320 *addr = (FPW)0x00B000B0; /* suspend erase */
321 *addr = (FPW)0x00FF00FF; /* reset to read mode */
326 /* show that we're waiting */
327 if ((now - last) > 1000) { /* every second */
333 *addr = (FPW)0x00FF00FF; /* reset to read mode */
340 /*-----------------------------------------------------------------------
341 * Copy memory to flash, returns:
344 * 2 - Flash not erased
345 * 4 - Flash not identified
348 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
353 int count, i, l, rc, port_width;
355 int i, l, rc, port_width;
358 if (info->flash_id == FLASH_UNKNOWN) {
361 /* get lower word aligned address */
362 #ifdef FLASH_PORT_WIDTH16
371 * handle unaligned start bytes
373 if ((l = addr - wp) != 0) {
375 for (i=0, cp=wp; i<l; ++i, ++cp) {
376 data = (data << 8) | (*(uchar *)cp);
378 for (; i<port_width && cnt>0; ++i) {
379 data = (data << 8) | *src++;
383 for (; cnt==0 && i<port_width; ++i, ++cp) {
384 data = (data << 8) | (*(uchar *)cp);
387 if ((rc = write_data(info, wp, data)) != 0) {
394 * handle word aligned part
399 while (cnt >= port_width) {
401 for (i=0; i<port_width; ++i) {
402 data = (data << 8) | *src++;
404 if ((rc = write_data(info, wp, data)) != 0) {
410 if (count++ > 0x20000)
423 * handle unaligned tail bytes
426 for (i=0, cp=wp; i<port_width && cnt>0; ++i, ++cp) {
427 data = (data << 8) | *src++;
430 for (; i<port_width; ++i, ++cp) {
431 data = (data << 8) | (*(uchar *)cp);
434 return (write_data(info, wp, data));
437 /*-----------------------------------------------------------------------
438 * Write a word or halfword to Flash, returns:
441 * 2 - Flash not erased
443 static int write_data (flash_info_t *info, ulong dest, FPW data)
445 FPWV *addr = (FPWV *)dest;
450 /* Check if Flash is (sufficiently) erased */
451 if ((*addr & data) != data) {
452 printf("not erased at %08lx (%x)\n",(ulong)addr,*addr);
455 /* Disable interrupts which might cause a timeout here */
456 flag = disable_interrupts();
458 *addr = (FPW)0x00400040; /* write setup */
461 /* re-enable interrupts if necessary */
465 start = get_timer (0);
467 while (((status = *addr) & (FPW)0x00800080) != (FPW)0x00800080) {
468 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
469 *addr = (FPW)0x00FF00FF; /* restore read mode */
474 *addr = (FPW)0x00FF00FF; /* restore read mode */