3 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
5 * (C) Copyright 2001-2004
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,
28 #include <linux/byteorder/swab.h>
31 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
33 /* Board support for 1 or 2 flash devices */
34 #define FLASH_PORT_WIDTH32
35 #undef FLASH_PORT_WIDTH16
37 #ifdef FLASH_PORT_WIDTH16
38 #define FLASH_PORT_WIDTH ushort
39 #define FLASH_PORT_WIDTHV vu_short
40 #define SWAP(x) __swab16(x)
42 #define FLASH_PORT_WIDTH ulong
43 #define FLASH_PORT_WIDTHV vu_long
44 #define SWAP(x) __swab32(x)
47 /* Intel-compatible flash ID */
48 #define INTEL_COMPAT 0x00890089
49 #define INTEL_ALT 0x00B000B0
51 /* Intel-compatible flash commands */
52 #define INTEL_PROGRAM 0x00100010
53 #define INTEL_ERASE 0x00200020
54 #define INTEL_CLEAR 0x00500050
55 #define INTEL_LOCKBIT 0x00600060
56 #define INTEL_PROTECT 0x00010001
57 #define INTEL_STATUS 0x00700070
58 #define INTEL_READID 0x00900090
59 #define INTEL_CONFIRM 0x00D000D0
60 #define INTEL_RESET 0xFFFFFFFF
62 /* Intel-compatible flash status bits */
63 #define INTEL_FINISHED 0x00800080
64 #define INTEL_OK 0x00800080
66 #define FPW FLASH_PORT_WIDTH
67 #define FPWV FLASH_PORT_WIDTHV
69 #define mb() __asm__ __volatile__ ("" : : : "memory")
71 /*-----------------------------------------------------------------------
74 static ulong flash_get_size (FPW *addr, flash_info_t *info);
75 static int write_data (flash_info_t *info, ulong dest, FPW data);
76 static void flash_get_offsets (ulong base, flash_info_t *info);
77 void inline spin_wheel (void);
79 /*-----------------------------------------------------------------------
82 unsigned long flash_init (void)
87 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
90 flash_get_size ((FPW *) PHYS_FLASH_1, &flash_info[i]);
91 flash_get_offsets (PHYS_FLASH_1, &flash_info[i]);
94 flash_get_size ((FPW *) PHYS_FLASH_2, &flash_info[i]);
95 flash_get_offsets (PHYS_FLASH_2, &flash_info[i]);
98 panic ("configured to many flash banks!\n");
101 size += flash_info[i].size;
104 /* Protect monitor and environment sectors
106 flash_protect ( FLAG_PROTECT_SET,
108 CFG_FLASH_BASE + monitor_flash_len - 1,
111 flash_protect ( FLAG_PROTECT_SET,
113 CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0] );
118 /*-----------------------------------------------------------------------
120 static void flash_get_offsets (ulong base, flash_info_t *info)
124 if (info->flash_id == FLASH_UNKNOWN) {
128 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
129 for (i = 0; i < info->sector_count; i++) {
130 info->start[i] = base + (i * PHYS_FLASH_SECT_SIZE);
131 info->protect[i] = 0;
136 /*-----------------------------------------------------------------------
138 void flash_print_info (flash_info_t *info)
142 if (info->flash_id == FLASH_UNKNOWN) {
143 printf ("missing or unknown FLASH type\n");
147 switch (info->flash_id & FLASH_VENDMASK) {
148 case FLASH_MAN_INTEL:
152 printf ("Unknown Vendor ");
156 switch (info->flash_id & FLASH_TYPEMASK) {
157 case FLASH_28F128J3A:
158 printf ("28F128J3A\n");
161 case FLASH_28F640J3A:
162 printf ("28F640J3A\n");
165 printf ("Unknown Chip Type\n");
169 printf (" Size: %ld MB in %d Sectors\n",
170 info->size >> 20, info->sector_count);
172 printf (" Sector Start Addresses:");
173 for (i = 0; i < info->sector_count; ++i) {
178 info->protect[i] ? " (RO)" : " ");
185 * The following code cannot be run from FLASH!
187 static ulong flash_get_size (FPW *addr, flash_info_t *info)
191 /* Write auto select command: read Manufacturer ID */
192 addr[0x5555] = (FPW) 0x00AA00AA;
193 addr[0x2AAA] = (FPW) 0x00550055;
194 addr[0x5555] = (FPW) 0x00900090;
201 case (FPW) INTEL_MANUFACT:
202 info->flash_id = FLASH_MAN_INTEL;
206 info->flash_id = FLASH_UNKNOWN;
207 info->sector_count = 0;
209 addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
210 return (0); /* no or unknown flash */
214 value = addr[1]; /* device ID */
218 case (FPW) INTEL_ID_28F128J3A:
219 info->flash_id += FLASH_28F128J3A;
220 info->sector_count = 128;
221 info->size = 0x02000000;
222 break; /* => 32 MB */
224 case (FPW) INTEL_ID_28F640J3A:
225 info->flash_id += FLASH_28F640J3A;
226 info->sector_count = 64;
227 info->size = 0x01000000;
228 break; /* => 16 MB */
231 info->flash_id = FLASH_UNKNOWN;
235 if (info->sector_count > CFG_MAX_FLASH_SECT) {
236 printf ("** ERROR: sector count %d > max (%d) **\n",
237 info->sector_count, CFG_MAX_FLASH_SECT);
238 info->sector_count = CFG_MAX_FLASH_SECT;
241 addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
247 /*-----------------------------------------------------------------------
250 int flash_erase (flash_info_t *info, int s_first, int s_last)
252 int flag, prot, sect;
253 ulong type, start, last;
256 if ((s_first < 0) || (s_first > s_last)) {
257 if (info->flash_id == FLASH_UNKNOWN) {
258 printf ("- missing\n");
260 printf ("- no sectors to erase\n");
265 type = (info->flash_id & FLASH_VENDMASK);
266 if ((type != FLASH_MAN_INTEL)) {
267 printf ("Can't erase unknown flash type %08lx - aborted\n",
273 for (sect = s_first; sect <= s_last; ++sect) {
274 if (info->protect[sect]) {
280 printf ("- Warning: %d protected sectors will not be erased!\n",
286 start = get_timer (0);
289 /* Disable interrupts which might cause a timeout here */
290 flag = disable_interrupts ();
292 /* Start erase on unprotected sectors */
293 for (sect = s_first; sect <= s_last; sect++) {
294 if (info->protect[sect] == 0) { /* not protected */
295 FPWV *addr = (FPWV *) (info->start[sect]);
298 printf ("Erasing sector %2d ... ", sect);
300 /* arm simple, non interrupt dependent timer */
301 reset_timer_masked ();
303 *addr = (FPW) 0x00500050; /* clear status register */
304 *addr = (FPW) 0x00200020; /* erase setup */
305 *addr = (FPW) 0x00D000D0; /* erase confirm */
307 while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
308 if (get_timer_masked () > CFG_FLASH_ERASE_TOUT) {
309 printf ("Timeout\n");
310 *addr = (FPW) 0x00B000B0; /* suspend erase */
311 *addr = (FPW) 0x00FF00FF; /* reset to read mode */
317 *addr = 0x00500050; /* clear status register cmd. */
318 *addr = 0x00FF00FF; /* resest to read mode */
326 /*-----------------------------------------------------------------------
327 * Copy memory to flash, returns:
330 * 2 - Flash not erased
331 * 4 - Flash not identified
334 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
338 int count, i, l, rc, port_width;
340 if (info->flash_id == FLASH_UNKNOWN) {
343 /* get lower word aligned address */
344 #ifdef FLASH_PORT_WIDTH16
353 * handle unaligned start bytes
355 if ((l = addr - wp) != 0) {
357 for (i = 0, cp = wp; i < l; ++i, ++cp) {
358 data = (data << 8) | (*(uchar *) cp);
360 for (; i < port_width && cnt > 0; ++i) {
361 data = (data << 8) | *src++;
365 for (; cnt == 0 && i < port_width; ++i, ++cp) {
366 data = (data << 8) | (*(uchar *) cp);
369 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
376 * handle word aligned part
379 while (cnt >= port_width) {
381 for (i = 0; i < port_width; ++i) {
382 data = (data << 8) | *src++;
384 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
389 if (count++ > 0x800) {
400 * handle unaligned tail bytes
403 for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
404 data = (data << 8) | *src++;
407 for (; i < port_width; ++i, ++cp) {
408 data = (data << 8) | (*(uchar *) cp);
411 return (write_data (info, wp, SWAP (data)));
414 /*-----------------------------------------------------------------------
415 * Write a word or halfword to Flash, returns:
418 * 2 - Flash not erased
420 static int write_data (flash_info_t *info, ulong dest, FPW data)
422 FPWV *addr = (FPWV *) dest;
426 /* Check if Flash is (sufficiently) erased */
427 if ((*addr & data) != data) {
428 printf ("not erased at %08lx (%lx)\n", (ulong) addr, *addr);
431 /* Disable interrupts which might cause a timeout here */
432 flag = disable_interrupts ();
434 *addr = (FPW) 0x00400040; /* write setup */
437 /* arm simple, non interrupt dependent timer */
438 reset_timer_masked ();
440 /* wait while polling the status register */
441 while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
442 if (get_timer_masked () > CFG_FLASH_WRITE_TOUT) {
443 *addr = (FPW) 0x00FF00FF; /* restore read mode */
448 *addr = (FPW) 0x00FF00FF; /* restore read mode */
453 void inline spin_wheel (void)
456 static char w[] = "\\/-";
458 printf ("\010%c", w[p]);
459 (++p == 3) ? (p = 0) : 0;
462 /*-----------------------------------------------------------------------
463 * Set/Clear sector's lock bit, returns:
465 * 1 - Error (timeout, voltage problems, etc.)
467 int flash_real_protect(flash_info_t *info, long sector, int prot)
471 vu_long *addr = (vu_long *)(info->start[sector]);
472 int flag = disable_interrupts();
474 *addr = INTEL_CLEAR; /* Clear status register */
475 if (prot) { /* Set sector lock bit */
476 *addr = INTEL_LOCKBIT; /* Sector lock bit */
477 *addr = INTEL_PROTECT; /* set */
479 else { /* Clear sector lock bit */
480 *addr = INTEL_LOCKBIT; /* All sectors lock bits */
481 *addr = INTEL_CONFIRM; /* clear */
484 reset_timer_masked ();
486 while ((*addr & INTEL_FINISHED) != INTEL_FINISHED) {
487 if (get_timer_masked () > CFG_FLASH_UNLOCK_TOUT) {
488 printf("Flash lock bit operation timed out\n");
494 if (*addr != INTEL_OK) {
495 printf("Flash lock bit operation failed at %08X, CSR=%08X\n",
496 (uint)addr, (uint)*addr);
501 info->protect[sector] = prot;
504 * Clear lock bit command clears all sectors lock bits, so
505 * we have to restore lock bits of protected sectors.
509 for (i = 0; i < info->sector_count; i++)
511 if (info->protect[i])
513 reset_timer_masked ();
514 addr = (vu_long *)(info->start[i]);
515 *addr = INTEL_LOCKBIT; /* Sector lock bit */
516 *addr = INTEL_PROTECT; /* set */
517 while ((*addr & INTEL_FINISHED) != INTEL_FINISHED)
519 if (get_timer_masked () > CFG_FLASH_UNLOCK_TOUT)
521 printf("Flash lock bit operation timed out\n");
533 *addr = INTEL_RESET; /* Reset to read array mode */