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 #define FLASH_PORT_WIDTH32
35 #undef 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 < CFG_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 flash_get_size((FPW *)PHYS_FLASH_2, &flash_info[i]);
78 flash_get_offsets(PHYS_FLASH_2, &flash_info[i]);
81 panic("configured to many flash banks!\n");
84 size += flash_info[i].size;
87 /* Protect monitor and environment sectors
89 flash_protect(FLAG_PROTECT_SET,
91 CFG_FLASH_BASE + _armboot_end_data - _armboot_start,
94 flash_protect(FLAG_PROTECT_SET,
96 CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
102 /*-----------------------------------------------------------------------
104 static void flash_get_offsets (ulong base, flash_info_t *info)
108 if (info->flash_id == FLASH_UNKNOWN) {
112 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
113 for (i = 0; i < info->sector_count; i++) {
114 info->start[i] = base + (i * PHYS_FLASH_SECT_SIZE);
115 info->protect[i] = 0;
120 /*-----------------------------------------------------------------------
122 void flash_print_info (flash_info_t *info)
126 if (info->flash_id == FLASH_UNKNOWN) {
127 printf ("missing or unknown FLASH type\n");
131 switch (info->flash_id & FLASH_VENDMASK) {
132 case FLASH_MAN_INTEL: printf ("INTEL "); break;
133 default: printf ("Unknown Vendor "); break;
136 switch (info->flash_id & FLASH_TYPEMASK) {
137 case FLASH_28F128J3A:
138 printf ("28F128J3A\n"); break;
139 default: printf ("Unknown Chip Type\n"); break;
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) {
151 info->protect[i] ? " (RO)" : " "
159 * The following code cannot be run from FLASH!
161 static ulong flash_get_size (FPW *addr, flash_info_t *info)
165 /* Write auto select command: read Manufacturer ID */
166 addr[0x5555] = (FPW)0x00AA00AA;
167 addr[0x2AAA] = (FPW)0x00550055;
168 addr[0x5555] = (FPW)0x00900090;
175 case (FPW)INTEL_MANUFACT:
176 info->flash_id = FLASH_MAN_INTEL;
180 info->flash_id = FLASH_UNKNOWN;
181 info->sector_count = 0;
183 addr[0] = (FPW)0x00FF00FF; /* restore read mode */
184 return (0); /* no or unknown flash */
188 value = addr[1]; /* device ID */
192 case (FPW)INTEL_ID_28F128J3A:
193 info->flash_id += FLASH_28F128J3A;
194 info->sector_count = 128;
195 info->size = 0x02000000;
196 break; /* => 16 MB */
199 info->flash_id = FLASH_UNKNOWN;
203 if (info->sector_count > CFG_MAX_FLASH_SECT) {
204 printf ("** ERROR: sector count %d > max (%d) **\n",
205 info->sector_count, CFG_MAX_FLASH_SECT);
206 info->sector_count = CFG_MAX_FLASH_SECT;
209 addr[0] = (FPW)0x00FF00FF; /* restore read mode */
215 /*-----------------------------------------------------------------------
218 int flash_erase (flash_info_t *info, int s_first, int s_last)
220 int flag, prot, sect;
221 ulong type, start, now, last;
224 if ((s_first < 0) || (s_first > s_last)) {
225 if (info->flash_id == FLASH_UNKNOWN) {
226 printf ("- missing\n");
228 printf ("- no sectors to erase\n");
233 type = (info->flash_id & FLASH_VENDMASK);
234 if ((type != FLASH_MAN_INTEL)) {
235 printf ("Can't erase unknown flash type %08lx - aborted\n",
241 for (sect=s_first; sect<=s_last; ++sect) {
242 if (info->protect[sect]) {
248 printf ("- Warning: %d protected sectors will not be erased!\n",
254 start = get_timer (0);
257 /* Disable interrupts which might cause a timeout here */
258 flag = disable_interrupts();
260 /* Start erase on unprotected sectors */
261 for (sect = s_first; sect<=s_last; sect++) {
262 if (info->protect[sect] == 0) { /* not protected */
263 FPWV *addr = (FPWV *)(info->start[sect]);
266 printf("Erasing sector %2d ... ", sect);
268 /* arm simple, non interrupt dependent timer */
269 reset_timer_masked();
271 *addr = (FPW)0x00500050; /* clear status register */
272 *addr = (FPW)0x00200020; /* erase setup */
273 *addr = (FPW)0x00D000D0; /* erase confirm */
275 while (((status = *addr) & (FPW)0x00800080) != (FPW)0x00800080) {
276 if (get_timer_masked() > CFG_FLASH_ERASE_TOUT) {
277 printf ("Timeout\n");
278 *addr = (FPW)0x00B000B0; /* suspend erase */
279 *addr = (FPW)0x00FF00FF; /* reset to read mode */
285 *addr = 0x00500050; /* clear status register cmd. */
286 *addr = 0x00FF00FF; /* resest to read mode */
294 /*-----------------------------------------------------------------------
295 * Copy memory to flash, returns:
298 * 2 - Flash not erased
299 * 4 - Flash not identified
302 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
306 int count, i, l, rc, port_width;
308 if (info->flash_id == FLASH_UNKNOWN) {
311 /* get lower word aligned address */
312 #ifdef FLASH_PORT_WIDTH16
321 * handle unaligned start bytes
323 if ((l = addr - wp) != 0) {
325 for (i=0, cp=wp; i<l; ++i, ++cp) {
326 data = (data << 8) | (*(uchar *)cp);
328 for (; i<port_width && cnt>0; ++i) {
329 data = (data << 8) | *src++;
333 for (; cnt==0 && i<port_width; ++i, ++cp) {
334 data = (data << 8) | (*(uchar *)cp);
337 if ((rc = write_data(info, wp, SWAP(data))) != 0) {
344 * handle word aligned part
347 while (cnt >= port_width) {
349 for (i=0; i<port_width; ++i) {
350 data = (data << 8) | *src++;
352 if ((rc = write_data(info, wp, SWAP(data))) != 0) {
369 * handle unaligned tail bytes
372 for (i=0, cp=wp; i<port_width && cnt>0; ++i, ++cp) {
373 data = (data << 8) | *src++;
376 for (; i<port_width; ++i, ++cp) {
377 data = (data << 8) | (*(uchar *)cp);
380 return (write_data(info, wp, SWAP(data)));
383 /*-----------------------------------------------------------------------
384 * Write a word or halfword to Flash, returns:
387 * 2 - Flash not erased
389 static int write_data (flash_info_t *info, ulong dest, FPW data)
391 FPWV *addr = (FPWV *)dest;
396 /* Check if Flash is (sufficiently) erased */
397 if ((*addr & data) != data) {
398 printf("not erased at %08lx (%x)\n",(ulong)addr,*addr);
401 /* Disable interrupts which might cause a timeout here */
402 flag = disable_interrupts();
404 *addr = (FPW)0x00400040; /* write setup */
407 /* arm simple, non interrupt dependent timer */
408 reset_timer_masked();
410 /* wait while polling the status register */
411 while (((status = *addr) & (FPW)0x00800080) != (FPW)0x00800080) {
412 if (get_timer_masked() > CFG_FLASH_WRITE_TOUT) {
413 *addr = (FPW)0x00FF00FF; /* restore read mode */
418 *addr = (FPW)0x00FF00FF; /* restore read mode */
427 static char w[] = "\\/-";
429 printf("\010%c", w[p]);
430 (++p == 3) ? (p = 0) : 0;