3 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
5 * (C) Copyright 2001-2004
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
9 * Texas Instruments, <www.ti.com>
10 * Kshitij Gupta <Kshitij@ti.com>
13 * Texas Instruments <www.ti.com>
16 * See file CREDITS for list of people who contributed to this
19 * This program is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU General Public License as
21 * published by the Free Software Foundation; either version 2 of
22 * the License, or (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
36 #include <linux/byteorder/swab.h>
38 #define PHYS_FLASH_SECT_SIZE 0x00020000 /* 256 KB sectors (x2) */
39 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
41 /* Board support for 1 or 2 flash devices */
42 #undef FLASH_PORT_WIDTH32
43 #define FLASH_PORT_WIDTH16
45 #ifdef FLASH_PORT_WIDTH16
46 #define FLASH_PORT_WIDTH ushort
47 #define FLASH_PORT_WIDTHV vu_short
48 #define SWAP(x) __swab16(x)
50 #define FLASH_PORT_WIDTH ulong
51 #define FLASH_PORT_WIDTHV vu_long
52 #define SWAP(x) __swab32(x)
55 #define FPW FLASH_PORT_WIDTH
56 #define FPWV FLASH_PORT_WIDTHV
58 #define mb() __asm__ __volatile__ ("" : : : "memory")
61 /* Flash Organization Structure */
62 typedef struct OrgDef {
63 unsigned int sector_number;
64 unsigned int sector_size;
68 /* Flash Organizations */
69 OrgDef OrgIntel_28F256L18T[] = {
70 {4, 32 * 1024}, /* 4 * 32kBytes sectors */
71 {255, 128 * 1024}, /* 255 * 128kBytes sectors */
75 /*-----------------------------------------------------------------------
78 unsigned long flash_init (void);
79 static ulong flash_get_size (FPW * addr, flash_info_t * info);
80 static int write_data (flash_info_t * info, ulong dest, FPW data);
81 static void flash_get_offsets (ulong base, flash_info_t * info);
82 void inline spin_wheel (void);
83 void flash_print_info (flash_info_t * info);
84 void flash_unprotect_sectors (FPWV * addr);
85 int flash_erase (flash_info_t * info, int s_first, int s_last);
86 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt);
88 /*-----------------------------------------------------------------------
91 unsigned long flash_init (void)
95 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
98 flash_get_size ((FPW *) PHYS_FLASH_1, &flash_info[i]);
99 flash_get_offsets (PHYS_FLASH_1, &flash_info[i]);
102 panic ("configured too many flash banks!\n");
105 size += flash_info[i].size;
108 /* Protect monitor and environment sectors
110 flash_protect (FLAG_PROTECT_SET,
112 CFG_FLASH_BASE + monitor_flash_len - 1, &flash_info[0]);
114 flash_protect (FLAG_PROTECT_SET,
116 CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0]);
121 /*-----------------------------------------------------------------------
123 static void flash_get_offsets (ulong base, flash_info_t * info)
128 pOrgDef = OrgIntel_28F256L18T;
129 if (info->flash_id == FLASH_UNKNOWN) {
133 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
134 for (i = 0; i < info->sector_count; i++) {
136 info->start[i] = base + (i * 0x8000);
137 info->protect[i] = 0;
139 info->start[i] = base +
140 (i * PHYS_FLASH_SECT_SIZE);
141 info->protect[i] = 0;
147 /*-----------------------------------------------------------------------
149 void flash_print_info (flash_info_t * info)
153 if (info->flash_id == FLASH_UNKNOWN) {
154 printf ("missing or unknown FLASH type\n");
158 switch (info->flash_id & FLASH_VENDMASK) {
159 case FLASH_MAN_INTEL:
163 printf ("Unknown Vendor ");
167 switch (info->flash_id & FLASH_TYPEMASK) {
168 case FLASH_28F256L18T:
169 printf ("FLASH 28F256L18T\n");
171 case FLASH_28F128J3A:
172 printf ("FLASH 28F128J3A\n");
175 printf ("Unknown Chip Type\n");
179 printf (" Size: %ld MB in %d Sectors\n",
180 info->size >> 20, info->sector_count);
182 printf (" Sector Start Addresses:");
183 for (i = 0; i < info->sector_count; ++i) {
187 info->start[i], info->protect[i] ? " (RO)" : " ");
194 * The following code cannot be run from FLASH!
196 static ulong flash_get_size (FPW * addr, flash_info_t * info)
200 /* Write auto select command: read Manufacturer ID */
201 addr[0x5555] = (FPW) 0x00AA00AA;
202 addr[0x2AAA] = (FPW) 0x00550055;
203 addr[0x5555] = (FPW) 0x00900090;
210 case (FPW) INTEL_MANUFACT:
211 info->flash_id = FLASH_MAN_INTEL;
213 case (FPW) MT2_MANUFACT:
214 info->flash_id = FLASH_MAN_INTEL;
218 info->flash_id = FLASH_UNKNOWN;
219 info->sector_count = 0;
221 addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
222 return (0); /* no or unknown flash */
226 value = addr[1]; /* device ID */
229 case (FPW) (INTEL_ID_28F256L18T):
230 info->flash_id += FLASH_28F256L18T;
231 info->sector_count = 259;
232 info->size = 0x02000000;
233 break; /* => 32 MB */
235 case (FPW) (INTEL_ID_28F256K3):
236 info->flash_id +=FLASH_28F256K3;
237 info->sector_count = 259;
238 info->size = 0x02000000;
239 printf ("\Intel StrataFlash 28F256K3C device initialized\n");
240 break; /* => 32 MB */
242 case (FPW) (INTEL_ID_28F128J3A):
243 info->flash_id +=FLASH_28F128J3A;
244 info->sector_count = 259;
245 info->size = 0x02000000;
246 printf ("\Micron StrataFlash MT28F128J3 device initialized\n");
247 break; /* => 32 MB */
250 info->flash_id = FLASH_UNKNOWN;
254 if (info->sector_count > CFG_MAX_FLASH_SECT) {
255 printf ("** ERROR: sector count %d > max (%d) **\n",
256 info->sector_count, CFG_MAX_FLASH_SECT);
257 info->sector_count = CFG_MAX_FLASH_SECT;
260 addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
266 /* unprotects a sector for write and erase
267 * on some intel parts, this unprotects the entire chip, but it
268 * wont hurt to call this additional times per sector...
270 void flash_unprotect_sectors (FPWV * addr)
272 #define PD_FINTEL_WSMS_READY_MASK 0x0080
276 *addr = (FPW) 0x00500050; /* clear status register */
278 /* this sends the clear lock bit command */
279 *addr = (FPW) 0x00600060;
280 *addr = (FPW) 0x00D000D0;
282 while (((status =*addr) & (FPW) 0x00800080) != (FPW) 0x00800080);
284 *addr = (FPW) 0x00FF00FF;
289 /*-----------------------------------------------------------------------
292 int flash_erase (flash_info_t * info, int s_first, int s_last)
294 int flag, prot, sect;
295 ulong type, start, last;
298 if ((s_first < 0) || (s_first > s_last)) {
299 if (info->flash_id == FLASH_UNKNOWN) {
300 printf ("- missing\n");
302 printf ("- no sectors to erase\n");
307 type = (info->flash_id & FLASH_VENDMASK);
308 if ((type != FLASH_MAN_INTEL)) {
309 printf ("Can't erase unknown flash type %08lx - aborted\n",
315 for (sect = s_first; sect <= s_last; ++sect) {
316 if (info->protect[sect]) {
322 printf ("- Warning: %d protected sectors will not be erased!\n",
329 start = get_timer (0);
332 /* Disable interrupts which might cause a timeout here */
333 flag = disable_interrupts ();
335 /* Start erase on unprotected sectors */
336 for (sect = s_first; sect <= s_last; sect++) {
337 if (info->protect[sect] == 0) { /* not protected */
338 FPWV *addr = (FPWV *) (info->start[sect]);
341 printf ("Erasing sector %2d ... ", sect);
343 flash_unprotect_sectors (addr);
345 /* arm simple, non interrupt dependent timer */
346 reset_timer_masked ();
348 *addr = (FPW) 0x00500050;/* clear status register */
349 *addr = (FPW) 0x00200020;/* erase setup */
350 *addr = (FPW) 0x00D000D0;/* erase confirm */
353 *addr) & (FPW) 0x00800080) !=
355 if (get_timer_masked () >
356 CFG_FLASH_ERASE_TOUT) {
357 printf ("Timeout\n");
359 *addr = (FPW) 0x00B000B0;
360 /* reset to read mode */
361 *addr = (FPW) 0x00FF00FF;
367 /* clear status register cmd. */
368 *addr = (FPW) 0x00500050;
369 *addr = (FPW) 0x00FF00FF;/* resest to read mode */
376 /*-----------------------------------------------------------------------
377 * Copy memory to flash, returns:
380 * 2 - Flash not erased
381 * 4 - Flash not identified
384 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
388 int count, i, l, rc, port_width;
390 if (info->flash_id == FLASH_UNKNOWN) {
393 /* get lower word aligned address */
394 #ifdef FLASH_PORT_WIDTH16
403 * handle unaligned start bytes
405 if ((l = addr - wp) != 0) {
407 for (i = 0, cp = wp; i < l; ++i, ++cp) {
408 data = (data << 8) | (*(uchar *) cp);
410 for (; i < port_width && cnt > 0; ++i) {
411 data = (data << 8) | *src++;
415 for (; cnt == 0 && i < port_width; ++i, ++cp) {
416 data = (data << 8) | (*(uchar *) cp);
419 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
426 * handle word aligned part
429 while (cnt >= port_width) {
431 for (i = 0; i < port_width; ++i) {
432 data = (data << 8) | *src++;
434 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
439 if (count++ > 0x800) {
450 * handle unaligned tail bytes
453 for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
454 data = (data << 8) | *src++;
457 for (; i < port_width; ++i, ++cp) {
458 data = (data << 8) | (*(uchar *) cp);
461 return (write_data (info, wp, SWAP (data)));
464 /*-----------------------------------------------------------------------
465 * Write a word or halfword to Flash, returns:
468 * 2 - Flash not erased
470 static int write_data (flash_info_t * info, ulong dest, FPW data)
472 FPWV *addr = (FPWV *) dest;
476 /* Check if Flash is (sufficiently) erased */
477 if ((*addr & data) != data) {
478 printf ("not erased at %08lx (%x)\n", (ulong) addr, *addr);
481 /* Disable interrupts which might cause a timeout here */
482 flag = disable_interrupts ();
483 *addr = (FPW) 0x00400040; /* write setup */
486 /* arm simple, non interrupt dependent timer */
487 reset_timer_masked ();
489 /* wait while polling the status register */
490 while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
491 if (get_timer_masked () > CFG_FLASH_WRITE_TOUT) {
492 *addr = (FPW) 0x00FF00FF; /* restore read mode */
496 *addr = (FPW) 0x00FF00FF; /* restore read mode */
500 void inline spin_wheel (void)
503 static char w[] = "\\/-";
505 printf ("\010%c", w[p]);
506 (++p == 3) ? (p = 0) : 0;