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;
228 ulong type, start, last;
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 start = get_timer(0);
264 /* Disable interrupts which might cause a timeout here */
265 flag = disable_interrupts();
267 /* Start erase on unprotected sectors */
268 for (sect = s_first; sect <= s_last; sect++) {
269 if (info->protect[sect] == 0) { /* not protected */
270 FPWV *addr = (FPWV *) (info->start[sect]);
273 printf("Erasing sector %2d ... ", sect);
275 /* arm simple, non interrupt dependent timer */
276 reset_timer_masked();
278 *addr = (FPW) 0x00500050; /* clear status register */
279 *addr = (FPW) 0x00200020; /* erase setup */
280 *addr = (FPW) 0x00D000D0; /* erase confirm */
283 *addr) & (FPW) 0x00800080) !=
285 if (get_timer_masked() > CONFIG_SYS_FLASH_ERASE_TOUT) {
287 *addr = (FPW) 0x00B000B0; /* suspend erase */
288 *addr = (FPW) 0x00FF00FF; /* reset to read mode */
294 *addr = (FPW) 0x00500050; /* clear status register cmd. */
295 *addr = (FPW) 0x00FF00FF; /* resest to read mode */
303 /*-----------------------------------------------------------------------
304 * Copy memory to flash, returns:
307 * 2 - Flash not erased
308 * 4 - Flash not identified
312 write_buff(flash_info_t * info, uchar * src, ulong addr, ulong cnt)
316 int count, i, l, rc, port_width;
318 if (info->flash_id == FLASH_UNKNOWN) {
321 /* get lower word aligned address */
322 #ifdef FLASH_PORT_WIDTH16
331 * handle unaligned start bytes
333 if ((l = addr - wp) != 0) {
335 for (i = 0, cp = wp; i < l; ++i, ++cp) {
336 data = (data << 8) | (*(uchar *) cp);
338 for (; i < port_width && cnt > 0; ++i) {
339 data = (data << 8) | *src++;
343 for (; cnt == 0 && i < port_width; ++i, ++cp) {
344 data = (data << 8) | (*(uchar *) cp);
347 if ((rc = write_data(info, wp, SWAP(data))) != 0) {
354 * handle word aligned part
357 while (cnt >= port_width) {
359 for (i = 0; i < port_width; ++i) {
360 data = (data << 8) | *src++;
362 if ((rc = write_data(info, wp, SWAP(data))) != 0) {
367 if (count++ > 0x800) {
378 * handle unaligned tail bytes
381 for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
382 data = (data << 8) | *src++;
385 for (; i < port_width; ++i, ++cp) {
386 data = (data << 8) | (*(uchar *) cp);
389 return (write_data(info, wp, SWAP(data)));
392 /*-----------------------------------------------------------------------
393 * Write a word or halfword to Flash, returns:
396 * 2 - Flash not erased
399 write_data(flash_info_t * info, ulong dest, FPW data)
401 FPWV *addr = (FPWV *) dest;
405 /* Check if Flash is (sufficiently) erased */
406 if ((*addr & data) != data) {
407 printf("not erased at %08lX (%lX)\n", (ulong) addr, *addr);
410 /* Disable interrupts which might cause a timeout here */
411 flag = disable_interrupts();
413 *addr = (FPW) 0x00400040; /* write setup */
416 /* arm simple, non interrupt dependent timer */
417 reset_timer_masked();
419 /* wait while polling the status register */
420 while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
421 if (get_timer_masked() > CONFIG_SYS_FLASH_WRITE_TOUT) {
422 *addr = (FPW) 0x00FF00FF; /* restore read mode */
427 *addr = (FPW) 0x00FF00FF; /* restore read mode */
436 static char w[] = "\\/-";
438 printf("\010%c", w[p]);
439 (++p == 3) ? (p = 0) : 0;