Modified patch originates from Andy Green <andy@openmoko.com>
[u-boot-openmoko/mini2440.git] / board / ids8247 / flash.c
blob4eba4b9687a0b0a90dae5b3727d32ffe2b758b14
1 /*
2 * (C) Copyright 2005
3 * Heiko Schocher, DENX Software Engineering, <hs@denx.de>
5 * (C) Copyright 2001
6 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
8 * (C) Copyright 2001-2005
9 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
11 * See file CREDITS for list of people who contributed to this
12 * project.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 * MA 02111-1307 USA
30 #undef DEBUG
32 #include <common.h>
34 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
36 #if defined(CFG_ENV_IS_IN_FLASH)
37 # ifndef CFG_ENV_ADDR
38 # define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
39 # endif
40 # ifndef CFG_ENV_SIZE
41 # define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
42 # endif
43 # ifndef CFG_ENV_SECT_SIZE
44 # define CFG_ENV_SECT_SIZE CFG_ENV_SIZE
45 # endif
46 #endif
48 /*-----------------------------------------------------------------------
49 * Protection Flags:
51 #define FLAG_PROTECT_SET 0x01
52 #define FLAG_PROTECT_CLEAR 0x02
54 /* Board support for 1 or 2 flash devices */
55 #undef FLASH_PORT_WIDTH32
56 #undef FLASH_PORT_WIDTH16
57 #define FLASH_PORT_WIDTH8
59 #ifdef FLASH_PORT_WIDTH16
60 #define FLASH_PORT_WIDTH ushort
61 #define FLASH_PORT_WIDTHV vu_short
62 #elif FLASH_PORT_WIDTH32
63 #define FLASH_PORT_WIDTH ulong
64 #define FLASH_PORT_WIDTHV vu_long
65 #else /* FLASH_PORT_WIDTH8 */
66 #define FLASH_PORT_WIDTH uchar
67 #define FLASH_PORT_WIDTHV vu_char
68 #endif
70 #define FPW FLASH_PORT_WIDTH
71 #define FPWV FLASH_PORT_WIDTHV
73 /*-----------------------------------------------------------------------
74 * Functions
76 static ulong flash_get_size (FPWV * addr, flash_info_t * info);
77 static int write_data (flash_info_t * info, ulong dest, FPW data);
78 static void flash_get_offsets (ulong base, flash_info_t * info);
80 /*-----------------------------------------------------------------------
83 unsigned long flash_init (void)
85 unsigned long size_b0;
86 int i;
87 volatile immap_t * immr = (immap_t *)CFG_IMMR;
88 volatile memctl8260_t *memctl = &immr->im_memctl;
90 /* Init: no FLASHes known */
91 for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
92 flash_info[i].flash_id = FLASH_UNKNOWN;
95 /* Static FLASH Bank configuration here - FIXME XXX */
96 size_b0 = flash_get_size ((FPW *) CFG_FLASH0_BASE, &flash_info[0]);
98 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
99 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
100 size_b0, size_b0 << 20);
103 memctl->memc_or0 = 0xff800060;
104 memctl->memc_br0 = 0xff800801;
106 flash_get_offsets (0xff800000, &flash_info[0]);
108 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
109 /* monitor protection ON by default */
110 (void) flash_protect (FLAG_PROTECT_SET,
111 CFG_MONITOR_BASE,
112 CFG_MONITOR_BASE + monitor_flash_len - 1,
113 &flash_info[0]);
114 #endif
116 #ifdef CFG_ENV_IS_IN_FLASH
117 /* ENV protection ON by default */
118 flash_protect (FLAG_PROTECT_SET,
119 CFG_ENV_ADDR,
120 CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
121 &flash_info[0]);
122 #endif
124 flash_info[0].size = size_b0;
126 return (size_b0);
129 /*-----------------------------------------------------------------------
131 static void flash_get_offsets (ulong base, flash_info_t * info)
133 int i;
135 if (info->flash_id == FLASH_UNKNOWN) {
136 return;
139 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
140 for (i = 0; i < info->sector_count; i++) {
141 info->start[i] = base + (i * 0x00020000);
146 /*-----------------------------------------------------------------------
148 void flash_print_info (flash_info_t * info)
150 int i;
152 if (info->flash_id == FLASH_UNKNOWN) {
153 printf ("missing or unknown FLASH type\n");
154 return;
157 switch (info->flash_id & FLASH_VENDMASK) {
158 case FLASH_MAN_INTEL:
159 printf ("INTEL ");
160 break;
161 default:
162 printf ("Unknown Vendor ");
163 break;
166 switch (info->flash_id & FLASH_TYPEMASK) {
167 case FLASH_28F320J3A:
168 printf ("28F320J3A\n");
169 break;
170 case FLASH_28F640J3A:
171 printf ("28F640J3A\n");
172 break;
173 case FLASH_28F128J3A:
174 printf ("28F128J3A\n");
175 break;
176 default:
177 printf ("Unknown Chip Type\n");
178 break;
181 printf (" Size: %ld MB in %d Sectors\n",
182 info->size >> 20, info->sector_count);
184 printf (" Sector Start Addresses:");
185 for (i = 0; i < info->sector_count; ++i) {
186 if ((i % 5) == 0)
187 printf ("\n ");
188 printf (" %08lX%s",
189 info->start[i],
190 info->protect[i] ? " (RO)" : " ");
192 printf ("\n");
193 return;
196 /*-----------------------------------------------------------------------
200 /*-----------------------------------------------------------------------
204 * The following code cannot be run from FLASH!
207 static ulong flash_get_size (FPWV * addr, flash_info_t * info)
209 FPW value;
211 addr[0] = (FPW) 0x00900090;
213 value = addr[0];
215 debug ("Manuf. ID @ 0x%08lx: 0x%08lx\n", (ulong)addr, value);
217 switch (value) {
218 case (FPW) INTEL_MANUFACT:
219 info->flash_id = FLASH_MAN_INTEL;
220 break;
221 default:
222 info->flash_id = FLASH_UNKNOWN;
223 info->sector_count = 0;
224 info->size = 0;
225 addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
226 return (0); /* no or unknown flash */
229 #ifdef FLASH_PORT_WIDTH8
230 value = addr[2]; /* device ID */
231 #else
232 value = addr[1]; /* device ID */
233 #endif
235 debug ("Device ID @ 0x%08lx: 0x%08lx\n", (ulong)(&addr[1]), value);
237 switch (value) {
238 case (FPW) INTEL_ID_28F320J3A:
239 info->flash_id += FLASH_28F320J3A;
240 info->sector_count = 32;
241 info->size = 0x00400000;
242 break; /* => 4 MB */
244 case (FPW) INTEL_ID_28F640J3A:
245 info->flash_id += FLASH_28F640J3A;
246 info->sector_count = 64;
247 info->size = 0x00800000;
248 break; /* => 8 MB */
250 case (FPW) INTEL_ID_28F128J3A:
251 info->flash_id += FLASH_28F128J3A;
252 info->sector_count = 128;
253 info->size = 0x01000000;
254 break; /* => 16 MB */
256 default:
257 info->flash_id = FLASH_UNKNOWN;
258 break;
261 if (info->sector_count > CFG_MAX_FLASH_SECT) {
262 printf ("** ERROR: sector count %d > max (%d) **\n",
263 info->sector_count, CFG_MAX_FLASH_SECT);
264 info->sector_count = CFG_MAX_FLASH_SECT;
267 addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
269 return (info->size);
273 /*-----------------------------------------------------------------------
276 int flash_erase (flash_info_t * info, int s_first, int s_last)
278 int flag, prot, sect;
279 ulong type, start, now, last;
280 int rcode = 0;
282 if ((s_first < 0) || (s_first > s_last)) {
283 if (info->flash_id == FLASH_UNKNOWN) {
284 printf ("- missing\n");
285 } else {
286 printf ("- no sectors to erase\n");
288 return 1;
291 type = (info->flash_id & FLASH_VENDMASK);
292 if ((type != FLASH_MAN_INTEL)) {
293 printf ("Can't erase unknown flash type %08lx - aborted\n",
294 info->flash_id);
295 return 1;
298 prot = 0;
299 for (sect = s_first; sect <= s_last; ++sect) {
300 if (info->protect[sect]) {
301 prot++;
305 if (prot) {
306 printf ("- Warning: %d protected sectors will not be erased!\n",
307 prot);
308 } else {
309 printf ("\n");
312 start = get_timer (0);
313 last = start;
314 /* Start erase on unprotected sectors */
315 for (sect = s_first; sect <= s_last; sect++) {
316 if (info->protect[sect] == 0) { /* not protected */
317 FPWV *addr = (FPWV *) (info->start[sect]);
318 FPW status;
320 /* Disable interrupts which might cause a timeout here */
321 flag = disable_interrupts ();
323 *addr = (FPW) 0x00500050; /* clear status register */
324 *addr = (FPW) 0x00200020; /* erase setup */
325 *addr = (FPW) 0x00D000D0; /* erase confirm */
327 /* re-enable interrupts if necessary */
328 if (flag)
329 enable_interrupts ();
331 /* wait at least 80us - let's wait 1 ms */
332 udelay (1000);
334 while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
335 if ((now = get_timer (start)) > CFG_FLASH_ERASE_TOUT) {
336 printf ("Timeout\n");
337 *addr = (FPW) 0x00B000B0; /* suspend erase */
338 *addr = (FPW) 0x00FF00FF; /* reset to read mode */
339 rcode = 1;
340 break;
343 /* show that we're waiting */
344 if ((now - last) > 1000) { /* every second */
345 putc ('.');
346 last = now;
350 *addr = (FPW) 0x00FF00FF; /* reset to read mode */
353 printf (" done\n");
354 return rcode;
357 /*-----------------------------------------------------------------------
358 * Copy memory to flash, returns:
359 * 0 - OK
360 * 1 - write timeout
361 * 2 - Flash not erased
362 * 4 - Flash not identified
365 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
367 ulong cp, wp;
368 FPW data;
370 int i, l, rc, port_width;
372 if (info->flash_id == FLASH_UNKNOWN) {
373 return 4;
375 /* get lower word aligned address */
376 #ifdef FLASH_PORT_WIDTH16
377 wp = (addr & ~1);
378 port_width = 2;
379 #elif defined(FLASH_PORT_WIDTH32)
380 wp = (addr & ~3);
381 port_width = 4;
382 #else
383 wp = addr;
384 port_width = 1;
385 #endif
388 * handle unaligned start bytes
390 if ((l = addr - wp) != 0) {
391 data = 0;
392 for (i = 0, cp = wp; i < l; ++i, ++cp) {
393 data = (data << 8) | (*(uchar *) cp);
395 for (; i < port_width && cnt > 0; ++i) {
396 data = (data << 8) | *src++;
397 --cnt;
398 ++cp;
400 for (; cnt == 0 && i < port_width; ++i, ++cp) {
401 data = (data << 8) | (*(uchar *) cp);
404 if ((rc = write_data (info, wp, data)) != 0) {
405 return (rc);
407 wp += port_width;
411 * handle word aligned part
413 while (cnt >= port_width) {
414 data = 0;
415 for (i = 0; i < port_width; ++i) {
416 data = (data << 8) | *src++;
418 if ((rc = write_data (info, wp, data)) != 0) {
419 return (rc);
421 wp += port_width;
422 cnt -= port_width;
425 if (cnt == 0) {
426 return (0);
430 * handle unaligned tail bytes
432 data = 0;
433 for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
434 data = (data << 8) | *src++;
435 --cnt;
437 for (; i < port_width; ++i, ++cp) {
438 data = (data << 8) | (*(uchar *) cp);
441 return (write_data (info, wp, data));
444 /*-----------------------------------------------------------------------
445 * Write a word or halfword to Flash, returns:
446 * 0 - OK
447 * 1 - write timeout
448 * 2 - Flash not erased
450 static int write_data (flash_info_t * info, ulong dest, FPW data)
452 FPWV *addr = (FPWV *) dest;
453 ulong status;
454 ulong start;
455 int flag;
457 /* Check if Flash is (sufficiently) erased */
458 if ((*addr & data) != data) {
459 printf ("not erased at %08lx (%x)\n", (ulong) addr, *addr);
460 return (2);
462 /* Disable interrupts which might cause a timeout here */
463 flag = disable_interrupts ();
465 *addr = (FPW) 0x00400040; /* write setup */
466 *addr = data;
468 /* re-enable interrupts if necessary */
469 if (flag)
470 enable_interrupts ();
472 start = get_timer (0);
474 while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
475 if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
476 *addr = (FPW) 0x00FF00FF; /* restore read mode */
477 return (1);
481 *addr = (FPW) 0x00FF00FF; /* restore read mode */
483 return (0);