3 * Heiko Schocher, DENX Software Engineering, <hs@denx.de>
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
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,
34 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
36 #if defined(CFG_ENV_IS_IN_FLASH)
38 # define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
41 # define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
43 # ifndef CFG_ENV_SECT_SIZE
44 # define CFG_ENV_SECT_SIZE CFG_ENV_SIZE
48 /*-----------------------------------------------------------------------
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
70 #define FPW FLASH_PORT_WIDTH
71 #define FPWV FLASH_PORT_WIDTHV
73 /*-----------------------------------------------------------------------
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;
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,
112 CFG_MONITOR_BASE + monitor_flash_len - 1,
116 #ifdef CFG_ENV_IS_IN_FLASH
117 /* ENV protection ON by default */
118 flash_protect (FLAG_PROTECT_SET,
120 CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
124 flash_info[0].size = size_b0;
129 /*-----------------------------------------------------------------------
131 static void flash_get_offsets (ulong base, flash_info_t * info)
135 if (info->flash_id == FLASH_UNKNOWN) {
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)
152 if (info->flash_id == FLASH_UNKNOWN) {
153 printf ("missing or unknown FLASH type\n");
157 switch (info->flash_id & FLASH_VENDMASK) {
158 case FLASH_MAN_INTEL:
162 printf ("Unknown Vendor ");
166 switch (info->flash_id & FLASH_TYPEMASK) {
167 case FLASH_28F320J3A:
168 printf ("28F320J3A\n");
170 case FLASH_28F640J3A:
171 printf ("28F640J3A\n");
173 case FLASH_28F128J3A:
174 printf ("28F128J3A\n");
177 printf ("Unknown Chip Type\n");
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) {
190 info->protect[i] ? " (RO)" : " ");
196 /*-----------------------------------------------------------------------
200 /*-----------------------------------------------------------------------
204 * The following code cannot be run from FLASH!
207 static ulong flash_get_size (FPWV * addr, flash_info_t * info)
211 addr[0] = (FPW) 0x00900090;
215 debug ("Manuf. ID @ 0x%08lx: 0x%08lx\n", (ulong)addr, value);
218 case (FPW) INTEL_MANUFACT:
219 info->flash_id = FLASH_MAN_INTEL;
222 info->flash_id = FLASH_UNKNOWN;
223 info->sector_count = 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 */
232 value = addr[1]; /* device ID */
235 debug ("Device ID @ 0x%08lx: 0x%08lx\n", (ulong)(&addr[1]), value);
238 case (FPW) INTEL_ID_28F320J3A:
239 info->flash_id += FLASH_28F320J3A;
240 info->sector_count = 32;
241 info->size = 0x00400000;
244 case (FPW) INTEL_ID_28F640J3A:
245 info->flash_id += FLASH_28F640J3A;
246 info->sector_count = 64;
247 info->size = 0x00800000;
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 */
257 info->flash_id = FLASH_UNKNOWN;
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 */
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;
282 if ((s_first < 0) || (s_first > s_last)) {
283 if (info->flash_id == FLASH_UNKNOWN) {
284 printf ("- missing\n");
286 printf ("- no sectors to erase\n");
291 type = (info->flash_id & FLASH_VENDMASK);
292 if ((type != FLASH_MAN_INTEL)) {
293 printf ("Can't erase unknown flash type %08lx - aborted\n",
299 for (sect = s_first; sect <= s_last; ++sect) {
300 if (info->protect[sect]) {
306 printf ("- Warning: %d protected sectors will not be erased!\n",
312 start = get_timer (0);
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]);
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 */
329 enable_interrupts ();
331 /* wait at least 80us - let's wait 1 ms */
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 */
343 /* show that we're waiting */
344 if ((now - last) > 1000) { /* every second */
350 *addr = (FPW) 0x00FF00FF; /* reset to read mode */
357 /*-----------------------------------------------------------------------
358 * Copy memory to flash, returns:
361 * 2 - Flash not erased
362 * 4 - Flash not identified
365 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
370 int i, l, rc, port_width;
372 if (info->flash_id == FLASH_UNKNOWN) {
375 /* get lower word aligned address */
376 #ifdef FLASH_PORT_WIDTH16
379 #elif defined(FLASH_PORT_WIDTH32)
388 * handle unaligned start bytes
390 if ((l = addr - wp) != 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++;
400 for (; cnt == 0 && i < port_width; ++i, ++cp) {
401 data = (data << 8) | (*(uchar *) cp);
404 if ((rc = write_data (info, wp, data)) != 0) {
411 * handle word aligned part
413 while (cnt >= port_width) {
415 for (i = 0; i < port_width; ++i) {
416 data = (data << 8) | *src++;
418 if ((rc = write_data (info, wp, data)) != 0) {
430 * handle unaligned tail bytes
433 for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
434 data = (data << 8) | *src++;
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:
448 * 2 - Flash not erased
450 static int write_data (flash_info_t * info, ulong dest, FPW data)
452 FPWV *addr = (FPWV *) dest;
457 /* Check if Flash is (sufficiently) erased */
458 if ((*addr & data) != data) {
459 printf ("not erased at %08lx (%x)\n", (ulong) addr, *addr);
462 /* Disable interrupts which might cause a timeout here */
463 flag = disable_interrupts ();
465 *addr = (FPW) 0x00400040; /* write setup */
468 /* re-enable interrupts if necessary */
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 */
481 *addr = (FPW) 0x00FF00FF; /* restore read mode */