3 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 * 2003 (c) MontaVista Software, Inc.
10 * See file CREDITS for list of people who contributed to this
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License as
15 * published by the Free Software Foundation; either version 2 of
16 * the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 #include <linux/byteorder/swab.h>
32 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
34 /* Board support for 1 or 2 flash devices */
35 #define FLASH_PORT_WIDTH32
36 #undef FLASH_PORT_WIDTH16
38 #ifdef FLASH_PORT_WIDTH16
39 #define FLASH_PORT_WIDTH ushort
40 #define FLASH_PORT_WIDTHV vu_short
41 #define SWAP(x) __swab16(x)
43 #define FLASH_PORT_WIDTH ulong
44 #define FLASH_PORT_WIDTHV vu_long
45 #define SWAP(x) __swab32(x)
48 #define FPW FLASH_PORT_WIDTH
49 #define FPWV FLASH_PORT_WIDTHV
51 #define mb() __asm__ __volatile__ ("" : : : "memory")
53 /*-----------------------------------------------------------------------
56 static ulong flash_get_size(FPW * addr, flash_info_t * info);
57 static int write_data(flash_info_t * info, ulong dest, FPW data);
58 static void flash_get_offsets(ulong base, flash_info_t * info);
59 void inline spin_wheel(void);
61 /*-----------------------------------------------------------------------
70 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
73 flash_get_size((FPW *) PHYS_FLASH_1, &flash_info[i]);
74 flash_get_offsets(PHYS_FLASH_1, &flash_info[i]);
77 panic("configured too many flash banks!\n");
80 size += flash_info[i].size;
83 /* Protect monitor and environment sectors
85 flash_protect(FLAG_PROTECT_SET,
86 CONFIG_SYS_FLASH_BASE,
87 CONFIG_SYS_FLASH_BASE + monitor_flash_len - 1, &flash_info[0]);
89 flash_protect(FLAG_PROTECT_SET,
91 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
96 /*-----------------------------------------------------------------------
99 flash_get_offsets(ulong base, flash_info_t * info)
103 if (info->flash_id == FLASH_UNKNOWN) {
107 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
108 for (i = 0; i < info->sector_count; i++) {
109 info->start[i] = base + (i * PHYS_FLASH_SECT_SIZE);
110 info->protect[i] = 0;
115 /*-----------------------------------------------------------------------
118 flash_print_info(flash_info_t * info)
122 if (info->flash_id == FLASH_UNKNOWN) {
123 printf("missing or unknown FLASH type\n");
127 switch (info->flash_id & FLASH_VENDMASK) {
128 case FLASH_MAN_INTEL:
132 printf("Unknown Vendor ");
136 switch (info->flash_id & FLASH_TYPEMASK) {
137 case FLASH_28F128J3A:
138 printf("28F128J3A\n");
141 printf("28F640J5\n");
144 printf("Unknown Chip Type\n");
148 printf(" Size: %ld MB in %d Sectors\n",
149 info->size >> 20, info->sector_count);
151 printf(" Sector Start Addresses:");
152 for (i = 0; i < info->sector_count; ++i) {
156 info->start[i], info->protect[i] ? " (RO)" : " ");
163 * The following code cannot be run from FLASH!
166 flash_get_size(FPW * addr, flash_info_t * info)
169 /* Write auto select command: read Manufacturer ID */
170 addr[0x5555] = (FPW) 0x00AA00AA;
171 addr[0x2AAA] = (FPW) 0x00550055;
172 addr[0x5555] = (FPW) 0x00900090;
179 case (FPW) INTEL_MANUFACT:
180 info->flash_id = FLASH_MAN_INTEL;
184 info->flash_id = FLASH_UNKNOWN;
185 info->sector_count = 0;
187 addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
188 return (0); /* no or unknown flash */
192 value = addr[1]; /* device ID */
194 case (FPW) INTEL_ID_28F128J3A:
195 info->flash_id += FLASH_28F128J3A;
196 info->sector_count = 128;
197 info->size = 0x02000000;
198 break; /* => 16 MB */
199 case (FPW) INTEL_ID_28F640J5:
200 info->flash_id += FLASH_28F640J5;
201 info->sector_count = 64;
202 info->size = 0x01000000;
203 break; /* => 16 MB */
206 info->flash_id = FLASH_UNKNOWN;
210 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
211 printf("** ERROR: sector count %d > max (%d) **\n",
212 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
213 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
216 addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
221 /*-----------------------------------------------------------------------
225 flash_erase(flash_info_t * info, int s_first, int s_last)
227 int flag, prot, sect;
231 if ((s_first < 0) || (s_first > s_last)) {
232 if (info->flash_id == FLASH_UNKNOWN) {
233 printf("- missing\n");
235 printf("- no sectors to erase\n");
240 type = (info->flash_id & FLASH_VENDMASK);
241 if ((type != FLASH_MAN_INTEL)) {
242 printf("Can't erase unknown flash type %08lx - aborted\n",
248 for (sect = s_first; sect <= s_last; ++sect) {
249 if (info->protect[sect]) {
255 printf("- Warning: %d protected sectors will not be erased!\n",
261 /* Disable interrupts which might cause a timeout here */
262 flag = disable_interrupts();
264 /* Start erase on unprotected sectors */
265 for (sect = s_first; sect <= s_last; sect++) {
266 if (info->protect[sect] == 0) { /* not protected */
267 FPWV *addr = (FPWV *) (info->start[sect]);
270 printf("Erasing sector %2d ... ", sect);
272 /* arm simple, non interrupt dependent timer */
273 start = get_timer(0);
275 *addr = (FPW) 0x00500050; /* clear status register */
276 *addr = (FPW) 0x00200020; /* erase setup */
277 *addr = (FPW) 0x00D000D0; /* erase confirm */
280 *addr) & (FPW) 0x00800080) !=
282 if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
284 *addr = (FPW) 0x00B000B0; /* suspend erase */
285 *addr = (FPW) 0x00FF00FF; /* reset to read mode */
291 *addr = (FPW) 0x00500050; /* clear status register cmd. */
292 *addr = (FPW) 0x00FF00FF; /* resest to read mode */
300 /*-----------------------------------------------------------------------
301 * Copy memory to flash, returns:
304 * 2 - Flash not erased
305 * 4 - Flash not identified
309 write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
313 int count, i, l, rc, port_width;
315 if (info->flash_id == FLASH_UNKNOWN) {
318 /* get lower word aligned address */
319 #ifdef FLASH_PORT_WIDTH16
328 * handle unaligned start bytes
330 if ((l = addr - wp) != 0) {
332 for (i = 0, cp = wp; i < l; ++i, ++cp) {
333 data = (data << 8) | (*(uchar *) cp);
335 for (; i < port_width && cnt > 0; ++i) {
336 data = (data << 8) | *src++;
340 for (; cnt == 0 && i < port_width; ++i, ++cp) {
341 data = (data << 8) | (*(uchar *) cp);
344 if ((rc = write_data(info, wp, SWAP(data))) != 0) {
351 * handle word aligned part
354 while (cnt >= port_width) {
356 for (i = 0; i < port_width; ++i) {
357 data = (data << 8) | *src++;
359 if ((rc = write_data(info, wp, SWAP(data))) != 0) {
364 if (count++ > 0x800) {
375 * handle unaligned tail bytes
378 for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
379 data = (data << 8) | *src++;
382 for (; i < port_width; ++i, ++cp) {
383 data = (data << 8) | (*(uchar *) cp);
386 return (write_data(info, wp, SWAP(data)));
389 /*-----------------------------------------------------------------------
390 * Write a word or halfword to Flash, returns:
393 * 2 - Flash not erased
396 write_data(flash_info_t * info, ulong dest, FPW data)
398 FPWV *addr = (FPWV *) dest;
403 /* Check if Flash is (sufficiently) erased */
404 if ((*addr & data) != data) {
405 printf("not erased at %08lX (%lX)\n", (ulong) addr, *addr);
408 /* Disable interrupts which might cause a timeout here */
409 flag = disable_interrupts();
411 *addr = (FPW) 0x00400040; /* write setup */
414 /* arm simple, non interrupt dependent timer */
415 start = get_timer(0);
417 /* wait while polling the status register */
418 while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
419 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
420 *addr = (FPW) 0x00FF00FF; /* restore read mode */
425 *addr = (FPW) 0x00FF00FF; /* restore read mode */
434 static char w[] = "\\/-";
436 printf("\010%c", w[p]);
437 (++p == 3) ? (p = 0) : 0;