3 * Greg Ungerer, OpenGear Inc, greg.ungerer@opengear.com
6 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
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,
31 #include <linux/byteorder/swab.h>
32 #include <asm/sections.h>
35 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
37 #define mb() __asm__ __volatile__ ("" : : : "memory")
39 /*-----------------------------------------------------------------------
42 static ulong flash_get_size (unsigned char * addr, flash_info_t * info);
43 static int write_data (flash_info_t * info, ulong dest, unsigned char data);
44 static void flash_get_offsets (ulong base, flash_info_t * info);
45 void inline spin_wheel (void);
47 /*-----------------------------------------------------------------------
50 unsigned long flash_init (void)
55 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
58 flash_get_size ((unsigned char *) PHYS_FLASH_1, &flash_info[i]);
59 flash_get_offsets (PHYS_FLASH_1, &flash_info[i]);
63 flash_info[i].flash_id = FLASH_UNKNOWN;
66 panic ("configured too many flash banks!\n");
69 size += flash_info[i].size;
72 /* Protect monitor and environment sectors
74 flash_protect (FLAG_PROTECT_SET,
75 CONFIG_SYS_FLASH_BASE,
76 CONFIG_SYS_FLASH_BASE + _bss_start_ofs,
82 /*-----------------------------------------------------------------------
84 static void flash_get_offsets (ulong base, flash_info_t * info)
88 if (info->flash_id == FLASH_UNKNOWN)
91 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
92 for (i = 0; i < info->sector_count; i++) {
93 info->start[i] = base + (i * PHYS_FLASH_SECT_SIZE);
99 /*-----------------------------------------------------------------------
101 void flash_print_info (flash_info_t * info)
105 if (info->flash_id == FLASH_UNKNOWN) {
106 printf ("missing or unknown FLASH type\n");
110 switch (info->flash_id & FLASH_VENDMASK) {
111 case FLASH_MAN_INTEL:
115 printf ("Unknown Vendor ");
119 switch (info->flash_id & FLASH_TYPEMASK) {
120 case FLASH_28F128J3A:
121 printf ("28F128J3A\n");
124 printf ("Unknown Chip Type\n");
128 printf (" Size: %ld MB in %d Sectors\n",
129 info->size >> 20, info->sector_count);
131 printf (" Sector Start Addresses:");
132 for (i = 0; i < info->sector_count; ++i) {
136 info->start[i], info->protect[i] ? " (RO)" : " ");
143 * The following code cannot be run from FLASH!
145 static ulong flash_get_size (unsigned char * addr, flash_info_t * info)
147 volatile unsigned char value;
149 /* Write auto select command: read Manufacturer ID */
159 case (unsigned char)INTEL_MANUFACT:
160 info->flash_id = FLASH_MAN_INTEL;
164 info->flash_id = FLASH_UNKNOWN;
165 info->sector_count = 0;
167 addr[0] = 0xFF; /* restore read mode */
168 return (0); /* no or unknown flash */
172 value = addr[2]; /* device ID */
176 case (unsigned char)INTEL_ID_28F640J3A:
177 info->flash_id += FLASH_28F640J3A;
178 info->sector_count = 64;
179 info->size = 0x00800000;
182 case (unsigned char)INTEL_ID_28F128J3A:
183 info->flash_id += FLASH_28F128J3A;
184 info->sector_count = 128;
185 info->size = 0x01000000;
186 break; /* => 16 MB */
189 info->flash_id = FLASH_UNKNOWN;
193 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
194 printf ("** ERROR: sector count %d > max (%d) **\n",
195 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
196 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
199 addr[0] = 0xFF; /* restore read mode */
205 /*-----------------------------------------------------------------------
208 int flash_erase (flash_info_t * info, int s_first, int s_last)
215 if ((s_first < 0) || (s_first > s_last)) {
216 if (info->flash_id == FLASH_UNKNOWN) {
217 printf ("- missing\n");
219 printf ("- no sectors to erase\n");
224 type = (info->flash_id & FLASH_VENDMASK);
225 if ((type != FLASH_MAN_INTEL)) {
226 printf ("Can't erase unknown flash type %08lx - aborted\n",
232 for (sect = s_first; sect <= s_last; ++sect) {
233 if (info->protect[sect]) {
239 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
243 /* Disable interrupts which might cause a timeout here */
244 disable_interrupts();
246 /* Start erase on unprotected sectors */
247 for (sect = s_first; sect <= s_last; sect++) {
248 if (info->protect[sect] == 0) { /* not protected */
249 volatile unsigned char *addr;
250 unsigned char status;
252 printf ("Erasing sector %2d ... ", sect);
254 /* arm simple, non interrupt dependent timer */
255 start = get_timer(0);
257 addr = (volatile unsigned char *) (info->start[sect]);
258 *addr = 0x50; /* clear status register */
259 *addr = 0x20; /* erase setup */
260 *addr = 0xD0; /* erase confirm */
262 while (((status = *addr) & 0x80) != 0x80) {
263 if (get_timer(start) >
264 CONFIG_SYS_FLASH_ERASE_TOUT) {
265 printf ("Timeout\n");
266 *addr = 0xB0; /* suspend erase */
267 *addr = 0xFF; /* reset to read mode */
273 *addr = 0x50; /* clear status register cmd */
274 *addr = 0xFF; /* resest to read mode */
282 /*-----------------------------------------------------------------------
283 * Copy memory to flash, returns:
286 * 2 - Flash not erased
287 * 4 - Flash not identified
290 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
294 int count, i, l, rc, port_width;
296 if (info->flash_id == FLASH_UNKNOWN)
303 * handle unaligned start bytes
305 if ((l = addr - wp) != 0) {
307 for (i = 0, cp = wp; i < l; ++i, ++cp) {
308 data = (data << 8) | (*(uchar *) cp);
310 for (; i < port_width && cnt > 0; ++i) {
311 data = (data << 8) | *src++;
315 for (; cnt == 0 && i < port_width; ++i, ++cp) {
316 data = (data << 8) | (*(uchar *) cp);
319 if ((rc = write_data (info, wp, data)) != 0) {
326 * handle word aligned part
329 while (cnt >= port_width) {
331 for (i = 0; i < port_width; ++i) {
332 data = (data << 8) | *src++;
334 if ((rc = write_data (info, wp, data)) != 0) {
339 if (count++ > 0x800) {
350 * handle unaligned tail bytes
353 for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
354 data = (data << 8) | *src++;
357 for (; i < port_width; ++i, ++cp) {
358 data = (data << 8) | (*(uchar *) cp);
361 return (write_data (info, wp, data));
364 /*-----------------------------------------------------------------------
365 * Write a word or halfword to Flash, returns:
368 * 2 - Flash not erased
370 static int write_data (flash_info_t * info, ulong dest, unsigned char data)
372 volatile unsigned char *addr = (volatile unsigned char *) dest;
376 /* Check if Flash is (sufficiently) erased */
377 if ((*addr & data) != data) {
378 printf ("not erased at %08lx (%lx)\n", (ulong) addr,
382 /* Disable interrupts which might cause a timeout here */
383 disable_interrupts();
385 *addr = 0x40; /* write setup */
388 /* arm simple, non interrupt dependent timer */
389 start = get_timer(0);
391 /* wait while polling the status register */
392 while (((status = *addr) & 0x80) != 0x80) {
393 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
394 *addr = 0xFF; /* restore read mode */
399 *addr = 0xFF; /* restore read mode */
404 void inline spin_wheel (void)
407 static char w[] = "\\/-";
409 printf ("\010%c", w[p]);
410 (++p == 3) ? (p = 0) : 0;