3 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
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[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
33 /* Board support for 1 or 2 flash devices */
34 #undef FLASH_PORT_WIDTH32
35 #define 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 #define FPW FLASH_PORT_WIDTH
48 #define FPWV FLASH_PORT_WIDTHV
50 #define mb() __asm__ __volatile__ ("" : : : "memory")
52 /*-----------------------------------------------------------------------
55 static ulong flash_get_size (FPW *addr, flash_info_t *info);
56 static int write_data (flash_info_t *info, ulong dest, FPW data);
57 static void flash_get_offsets (ulong base, flash_info_t *info);
58 void inline spin_wheel(void);
60 /*-----------------------------------------------------------------------
63 unsigned long flash_init (void)
68 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,
90 flash_protect(FLAG_PROTECT_SET,
92 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1,
98 /*-----------------------------------------------------------------------
100 static void flash_get_offsets (ulong base, flash_info_t *info)
104 if (info->flash_id == FLASH_UNKNOWN) {
108 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
109 for (i = 0; i < info->sector_count; i++) {
110 info->start[i] = base + (i * PHYS_FLASH_SECT_SIZE);
111 info->protect[i] = 0;
116 /*-----------------------------------------------------------------------
118 void 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: printf ("INTEL "); break;
129 default: printf ("Unknown Vendor "); break;
132 switch (info->flash_id & FLASH_TYPEMASK) {
133 case FLASH_28F128J3A:
134 printf ("28F128J3A\n"); break;
135 default: printf ("Unknown Chip Type\n"); break;
138 printf (" Size: %ld MB in %d Sectors\n",
139 info->size >> 20, info->sector_count);
141 printf (" Sector Start Addresses:");
142 for (i=0; i<info->sector_count; ++i) {
147 info->protect[i] ? " (RO)" : " "
155 * The following code cannot be run from FLASH!
157 static ulong flash_get_size (FPW *addr, flash_info_t *info)
160 /* Write auto select command: read Manufacturer ID */
161 addr[0x5555] = (FPW)0x00AA00AA;
162 addr[0x2AAA] = (FPW)0x00550055;
163 addr[0x5555] = (FPW)0x00900090;
170 case (FPW)INTEL_MANUFACT:
171 info->flash_id = FLASH_MAN_INTEL;
175 info->flash_id = FLASH_UNKNOWN;
176 info->sector_count = 0;
178 addr[0] = (FPW)0x00FF00FF; /* restore read mode */
179 return (0); /* no or unknown flash */
183 value = addr[1]; /* device ID */
186 case (FPW)INTEL_ID_28F128J3A:
187 info->flash_id += FLASH_28F128J3A;
188 info->sector_count = 128;
189 info->size = 0x02000000;
190 break; /* => 16 MB */
193 info->flash_id = FLASH_UNKNOWN;
197 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
198 printf ("** ERROR: sector count %d > max (%d) **\n",
199 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
200 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
203 addr[0] = (FPW)0x00FF00FF; /* restore read mode */
209 /*-----------------------------------------------------------------------
212 int flash_erase (flash_info_t *info, int s_first, int s_last)
214 int flag, prot, sect;
218 if ((s_first < 0) || (s_first > s_last)) {
219 if (info->flash_id == FLASH_UNKNOWN) {
220 printf ("- missing\n");
222 printf ("- no sectors to erase\n");
227 type = (info->flash_id & FLASH_VENDMASK);
228 if ((type != FLASH_MAN_INTEL)) {
229 printf ("Can't erase unknown flash type %08lx - aborted\n",
235 for (sect=s_first; sect<=s_last; ++sect) {
236 if (info->protect[sect]) {
242 printf ("- Warning: %d protected sectors will not be erased!\n",
248 /* Disable interrupts which might cause a timeout here */
249 flag = disable_interrupts();
251 /* Start erase on unprotected sectors */
252 for (sect = s_first; sect<=s_last; sect++) {
253 if (info->protect[sect] == 0) { /* not protected */
254 FPWV *addr = (FPWV *)(info->start[sect]);
257 printf("Erasing sector %2d ... ", sect);
259 /* arm simple, non interrupt dependent timer */
260 start = get_timer(0);
262 *addr = (FPW)0x00500050; /* clear status register */
263 *addr = (FPW)0x00200020; /* erase setup */
264 *addr = (FPW)0x00D000D0; /* erase confirm */
266 while (((status = *addr) & (FPW)0x00800080) != (FPW)0x00800080) {
267 if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
268 printf ("Timeout\n");
269 *addr = (FPW)0x00B000B0; /* suspend erase */
270 *addr = (FPW)0x00FF00FF; /* reset to read mode */
276 *addr = (FPW)0x00500050; /* clear status register cmd. */
277 *addr = (FPW)0x00FF00FF; /* resest to read mode */
285 /*-----------------------------------------------------------------------
286 * Copy memory to flash, returns:
289 * 2 - Flash not erased
290 * 4 - Flash not identified
293 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
297 int count, i, l, rc, port_width;
299 if (info->flash_id == FLASH_UNKNOWN) {
302 /* get lower word aligned address */
303 #ifdef FLASH_PORT_WIDTH16
312 * handle unaligned start bytes
314 if ((l = addr - wp) != 0) {
316 for (i=0, cp=wp; i<l; ++i, ++cp) {
317 data = (data << 8) | (*(uchar *)cp);
319 for (; i<port_width && cnt>0; ++i) {
320 data = (data << 8) | *src++;
324 for (; cnt==0 && i<port_width; ++i, ++cp) {
325 data = (data << 8) | (*(uchar *)cp);
328 if ((rc = write_data(info, wp, SWAP(data))) != 0) {
335 * handle word aligned part
338 while (cnt >= port_width) {
340 for (i=0; i<port_width; ++i) {
341 data = (data << 8) | *src++;
343 if ((rc = write_data(info, wp, SWAP(data))) != 0) {
360 * handle unaligned tail bytes
363 for (i=0, cp=wp; i<port_width && cnt>0; ++i, ++cp) {
364 data = (data << 8) | *src++;
367 for (; i<port_width; ++i, ++cp) {
368 data = (data << 8) | (*(uchar *)cp);
371 return (write_data(info, wp, SWAP(data)));
374 /*-----------------------------------------------------------------------
375 * Write a word or halfword to Flash, returns:
378 * 2 - Flash not erased
380 static int write_data (flash_info_t *info, ulong dest, FPW data)
382 FPWV *addr = (FPWV *)dest;
387 /* Check if Flash is (sufficiently) erased */
388 if ((*addr & data) != data) {
389 printf("not erased at %08lx (%x)\n",(ulong)addr,*addr);
392 /* Disable interrupts which might cause a timeout here */
393 flag = disable_interrupts();
395 *addr = (FPW)0x00400040; /* write setup */
398 /* arm simple, non interrupt dependent timer */
399 start = get_timer(0);
401 /* wait while polling the status register */
402 while (((status = *addr) & (FPW)0x00800080) != (FPW)0x00800080) {
403 if (start = get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
404 *addr = (FPW)0x00FF00FF; /* restore read mode */
409 *addr = (FPW)0x00FF00FF; /* restore read mode */
418 static char w[] = "\\/-";
420 printf("\010%c", w[p]);
421 (++p == 3) ? (p = 0) : 0;