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[CFG_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
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 < CFG_MAX_FLASH_BANKS; i++) {
71 flash_get_size ((FPW *) PHYS_FLASH_1, &flash_info[i]);
72 flash_get_offsets (PHYS_FLASH_1, &flash_info[i]);
75 panic ("configured too many flash banks!\n");
78 size += flash_info[i].size;
81 /* Protect monitor and environment sectors
83 flash_protect (FLAG_PROTECT_SET,
85 CFG_FLASH_BASE + _bss_start - _armboot_start,
88 flash_protect (FLAG_PROTECT_SET,
90 CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0]);
95 /*-----------------------------------------------------------------------
97 static void flash_get_offsets (ulong base, flash_info_t * info)
101 if (info->flash_id == FLASH_UNKNOWN) {
105 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
106 for (i = 0; i < info->sector_count; i++) {
107 info->start[i] = base + (i * PHYS_FLASH_SECT_SIZE);
108 info->protect[i] = 0;
113 /*-----------------------------------------------------------------------
115 void flash_print_info (flash_info_t * info)
119 if (info->flash_id == FLASH_UNKNOWN) {
120 printf ("missing or unknown FLASH type\n");
124 switch (info->flash_id & FLASH_VENDMASK) {
125 case FLASH_MAN_INTEL:
129 printf ("Unknown Vendor ");
133 switch (info->flash_id & FLASH_TYPEMASK) {
134 case FLASH_28F128J3A:
135 printf ("28F128J3A\n");
138 printf ("Unknown Chip Type\n");
142 printf (" Size: %ld MB in %d Sectors\n",
143 info->size >> 20, info->sector_count);
145 printf (" Sector Start Addresses:");
146 for (i = 0; i < info->sector_count; ++i) {
150 info->start[i], info->protect[i] ? " (RO)" : " ");
157 * The following code cannot be run from FLASH!
159 static ulong flash_get_size (FPW * addr, flash_info_t * info)
163 /* Write auto select command: read Manufacturer ID */
164 addr[0x5555] = (FPW) 0x00AA00AA;
165 addr[0x2AAA] = (FPW) 0x00550055;
166 addr[0x5555] = (FPW) 0x00900090;
173 case (FPW) INTEL_MANUFACT:
174 info->flash_id = FLASH_MAN_INTEL;
178 info->flash_id = FLASH_UNKNOWN;
179 info->sector_count = 0;
181 addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
182 return (0); /* no or unknown flash */
186 value = addr[1]; /* device ID */
190 case (FPW) INTEL_ID_28F128J3A:
191 info->flash_id += FLASH_28F128J3A;
192 info->sector_count = 128;
193 info->size = 0x02000000;
194 break; /* => 16 MB */
197 info->flash_id = FLASH_UNKNOWN;
201 if (info->sector_count > CFG_MAX_FLASH_SECT) {
202 printf ("** ERROR: sector count %d > max (%d) **\n",
203 info->sector_count, CFG_MAX_FLASH_SECT);
204 info->sector_count = CFG_MAX_FLASH_SECT;
207 addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
213 /*-----------------------------------------------------------------------
216 int flash_erase (flash_info_t * info, int s_first, int s_last)
218 int flag, prot, sect;
222 if ((s_first < 0) || (s_first > s_last)) {
223 if (info->flash_id == FLASH_UNKNOWN) {
224 printf ("- missing\n");
226 printf ("- no sectors to erase\n");
231 type = (info->flash_id & FLASH_VENDMASK);
232 if ((type != FLASH_MAN_INTEL)) {
233 printf ("Can't erase unknown flash type %08lx - aborted\n",
239 for (sect = s_first; sect <= s_last; ++sect) {
240 if (info->protect[sect]) {
246 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
251 /* Disable interrupts which might cause a timeout here */
252 flag = disable_interrupts ();
254 /* Start erase on unprotected sectors */
255 for (sect = s_first; sect <= s_last; sect++) {
256 if (info->protect[sect] == 0) { /* not protected */
257 FPWV *addr = (FPWV *) (info->start[sect]);
260 printf ("Erasing sector %2d ... ", sect);
262 /* arm simple, non interrupt dependent timer */
263 reset_timer_masked ();
265 *addr = (FPW) 0x00500050; /* clear status register */
266 *addr = (FPW) 0x00200020; /* erase setup */
267 *addr = (FPW) 0x00D000D0; /* erase confirm */
270 *addr) & (FPW) 0x00800080) !=
272 if (get_timer_masked () >
273 CFG_FLASH_ERASE_TOUT) {
274 printf ("Timeout\n");
275 *addr = (FPW) 0x00B000B0; /* suspend erase */
276 *addr = (FPW) 0x00FF00FF; /* reset to read mode */
282 *addr = (FPW) 0x00500050; /* clear status register cmd. */
283 *addr = (FPW) 0x00FF00FF; /* resest to read mode */
291 /*-----------------------------------------------------------------------
292 * Copy memory to flash, returns:
295 * 2 - Flash not erased
296 * 4 - Flash not identified
299 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
303 int count, i, l, rc, port_width;
305 if (info->flash_id == FLASH_UNKNOWN) {
309 /* get lower word aligned address */
310 #ifdef FLASH_PORT_WIDTH16
319 * handle unaligned start bytes
321 if ((l = addr - wp) != 0) {
323 for (i = 0, cp = wp; i < l; ++i, ++cp) {
324 data = (data << 8) | (*(uchar *) cp);
326 for (; i < port_width && cnt > 0; ++i) {
327 data = (data << 8) | *src++;
331 for (; cnt == 0 && i < port_width; ++i, ++cp) {
332 data = (data << 8) | (*(uchar *) cp);
335 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
342 * handle word aligned part
345 while (cnt >= port_width) {
347 for (i = 0; i < port_width; ++i) {
348 data = (data << 8) | *src++;
350 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
355 if (count++ > 0x800) {
366 * handle unaligned tail bytes
369 for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
370 data = (data << 8) | *src++;
373 for (; i < port_width; ++i, ++cp) {
374 data = (data << 8) | (*(uchar *) cp);
377 return (write_data (info, wp, SWAP (data)));
380 /*-----------------------------------------------------------------------
381 * Write a word or halfword to Flash, returns:
384 * 2 - Flash not erased
386 static int write_data (flash_info_t * info, ulong dest, FPW data)
388 FPWV *addr = (FPWV *) dest;
392 /* Check if Flash is (sufficiently) erased */
393 if ((*addr & data) != data) {
394 printf ("not erased at %08lx (%lx)\n", (ulong) addr,
398 /* Disable interrupts which might cause a timeout here */
399 flag = disable_interrupts ();
401 *addr = (FPW) 0x00400040; /* write setup */
404 /* arm simple, non interrupt dependent timer */
405 reset_timer_masked ();
407 /* wait while polling the status register */
408 while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
409 if (get_timer_masked () > CFG_FLASH_WRITE_TOUT) {
410 *addr = (FPW) 0x00FF00FF; /* restore read mode */
415 *addr = (FPW) 0x00FF00FF; /* restore read mode */
420 void inline spin_wheel (void)
423 static char w[] = "\\/-";
425 printf ("\010%c", w[p]);
426 (++p == 3) ? (p = 0) : 0;