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>
12 * See file CREDITS for list of people who contributed to this
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation; either version 2 of
18 * the License, or (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32 #include <linux/byteorder/swab.h>
34 #define PHYS_FLASH_SECT_SIZE 0x00020000 /* 256 KB sectors (x2) */
35 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
37 /* Board support for 1 or 2 flash devices */
38 #undef FLASH_PORT_WIDTH32
39 #define FLASH_PORT_WIDTH16
41 #ifdef FLASH_PORT_WIDTH16
42 #define FLASH_PORT_WIDTH ushort
43 #define FLASH_PORT_WIDTHV vu_short
44 #define SWAP(x) __swab16(x)
46 #define FLASH_PORT_WIDTH ulong
47 #define FLASH_PORT_WIDTHV vu_long
48 #define SWAP(x) __swab32(x)
51 #define FPW FLASH_PORT_WIDTH
52 #define FPWV FLASH_PORT_WIDTHV
54 #define mb() __asm__ __volatile__ ("" : : : "memory")
57 /* Flash Organization Structure */
58 typedef struct OrgDef {
59 unsigned int sector_number;
60 unsigned int sector_size;
64 /* Flash Organizations */
65 OrgDef OrgIntel_28F256L18T[] = {
66 {4, 32 * 1024}, /* 4 * 32kBytes sectors */
67 {255, 128 * 1024}, /* 255 * 128kBytes sectors */
71 /*-----------------------------------------------------------------------
74 unsigned long flash_init (void);
75 static ulong flash_get_size (FPW * addr, flash_info_t * info);
76 static int write_data (flash_info_t * info, ulong dest, FPW data);
77 static void flash_get_offsets (ulong base, flash_info_t * info);
78 void inline spin_wheel (void);
79 void flash_print_info (flash_info_t * info);
80 void flash_unprotect_sectors (FPWV * addr);
81 int flash_erase (flash_info_t * info, int s_first, int s_last);
82 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt);
83 void flash_unlock(flash_info_t * info);
85 /*-----------------------------------------------------------------------
88 unsigned long flash_init (void)
93 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
96 flash_get_size ((FPW *) CONFIG_SYS_FLASH_BASE, &flash_info[i]);
97 flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[i]);
98 /* to reset the lock bit */
99 flash_unlock(&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,
111 CONFIG_SYS_FLASH_BASE,
112 CONFIG_SYS_FLASH_BASE + monitor_flash_len - 1, &flash_info[0]);
114 flash_protect (FLAG_PROTECT_SET,
116 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
121 /*-----------------------------------------------------------------------
123 void flash_unlock(flash_info_t * info)
126 for (j=2;j<CONFIG_SYS_MAX_FLASH_SECT;j++){
127 FPWV *addr = (FPWV *) (info->start[j]);
128 flash_unprotect_sectors (addr);
129 *addr = (FPW) 0x00500050;/* clear status register */
130 *addr = (FPW) 0x00FF00FF;/* resest to read mode */
134 /*-----------------------------------------------------------------------
136 static void flash_get_offsets (ulong base, flash_info_t * info)
141 pOrgDef = OrgIntel_28F256L18T;
142 if (info->flash_id == FLASH_UNKNOWN) {
146 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
147 for (i = 0; i < info->sector_count; i++) {
149 info->start[i] = base + (i * 0x8000);
150 info->protect[i] = 0;
152 info->start[i] = base +
153 (i * PHYS_FLASH_SECT_SIZE);
154 info->protect[i] = 0;
160 /*-----------------------------------------------------------------------
162 void flash_print_info (flash_info_t * info)
166 if (info->flash_id == FLASH_UNKNOWN) {
167 printf ("missing or unknown FLASH type\n");
171 switch (info->flash_id & FLASH_VENDMASK) {
172 case FLASH_MAN_INTEL:
176 printf ("Unknown Vendor ");
180 switch (info->flash_id & FLASH_TYPEMASK) {
181 case FLASH_28F256L18T:
182 printf ("FLASH 28F256L18T\n");
185 printf ("Unknown Chip Type\n");
189 printf (" Size: %ld MB in %d Sectors\n",
190 info->size >> 20, info->sector_count);
192 printf (" Sector Start Addresses:");
193 for (i = 0; i < info->sector_count; ++i) {
197 info->start[i], info->protect[i] ? " (RO)" : " ");
204 * The following code cannot be run from FLASH!
206 static ulong flash_get_size (FPW * addr, flash_info_t * info)
210 /* Write auto select command: read Manufacturer ID */
211 addr[0x5555] = (FPW) 0x00AA00AA;
212 addr[0x2AAA] = (FPW) 0x00550055;
213 addr[0x5555] = (FPW) 0x00900090;
220 case (FPW) INTEL_MANUFACT:
221 info->flash_id = FLASH_MAN_INTEL;
225 info->flash_id = FLASH_UNKNOWN;
226 info->sector_count = 0;
228 addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
229 return (0); /* no or unknown flash */
233 value = addr[1]; /* device ID */
236 case (FPW) (INTEL_ID_28F256L18T):
237 info->flash_id += FLASH_28F256L18T;
238 info->sector_count = 259;
239 info->size = 0x02000000;
240 break; /* => 32 MB */
243 info->flash_id = FLASH_UNKNOWN;
247 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
248 printf ("** ERROR: sector count %d > max (%d) **\n",
249 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
250 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
253 addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
259 /* unprotects a sector for write and erase
260 * on some intel parts, this unprotects the entire chip, but it
261 * wont hurt to call this additional times per sector...
263 void flash_unprotect_sectors (FPWV * addr)
265 #define PD_FINTEL_WSMS_READY_MASK 0x0080
267 *addr = (FPW) 0x00500050; /* clear status register */
269 /* this sends the clear lock bit command */
270 *addr = (FPW) 0x00600060;
271 *addr = (FPW) 0x00D000D0;
275 /*-----------------------------------------------------------------------
278 int flash_erase (flash_info_t * info, int s_first, int s_last)
280 int flag, prot, sect;
281 ulong type, start, last;
284 if ((s_first < 0) || (s_first > s_last)) {
285 if (info->flash_id == FLASH_UNKNOWN) {
286 printf ("- missing\n");
288 printf ("- no sectors to erase\n");
293 type = (info->flash_id & FLASH_VENDMASK);
294 if ((type != FLASH_MAN_INTEL)) {
295 printf ("Can't erase unknown flash type %08lx - aborted\n",
301 for (sect = s_first; sect <= s_last; ++sect) {
302 if (info->protect[sect]) {
308 printf ("- Warning: %d protected sectors will not be erased!\n",
315 start = get_timer (0);
318 /* Disable interrupts which might cause a timeout here */
319 flag = disable_interrupts ();
321 /* Start erase on unprotected sectors */
322 for (sect = s_first; sect <= s_last; sect++) {
323 if (info->protect[sect] == 0) { /* not protected */
324 FPWV *addr = (FPWV *) (info->start[sect]);
327 printf ("Erasing sector %2d ... ", sect);
329 flash_unprotect_sectors (addr);
331 /* arm simple, non interrupt dependent timer */
332 reset_timer_masked ();
334 *addr = (FPW) 0x00500050;/* clear status register */
335 *addr = (FPW) 0x00200020;/* erase setup */
336 *addr = (FPW) 0x00D000D0;/* erase confirm */
339 *addr) & (FPW) 0x00800080) !=
341 if (get_timer_masked () >
342 CONFIG_SYS_FLASH_ERASE_TOUT) {
343 printf ("Timeout\n");
345 *addr = (FPW) 0x00B000B0;
346 /* reset to read mode */
347 *addr = (FPW) 0x00FF00FF;
353 /* clear status register cmd. */
354 *addr = (FPW) 0x00500050;
355 *addr = (FPW) 0x00FF00FF;/* resest to read mode */
362 /*-----------------------------------------------------------------------
363 * Copy memory to flash, returns:
366 * 2 - Flash not erased
367 * 4 - Flash not identified
370 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
374 int count, i, l, rc, port_width;
376 if (info->flash_id == FLASH_UNKNOWN) {
379 /* get lower word aligned address */
380 #ifdef FLASH_PORT_WIDTH16
389 * handle unaligned start bytes
391 if ((l = addr - wp) != 0) {
393 for (i = 0, cp = wp; i < l; ++i, ++cp) {
394 data = (data << 8) | (*(uchar *) cp);
396 for (; i < port_width && cnt > 0; ++i) {
397 data = (data << 8) | *src++;
401 for (; cnt == 0 && i < port_width; ++i, ++cp) {
402 data = (data << 8) | (*(uchar *) cp);
405 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
412 * handle word aligned part
415 while (cnt >= port_width) {
417 for (i = 0; i < port_width; ++i) {
418 data = (data << 8) | *src++;
420 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
425 if (count++ > 0x800) {
436 * handle unaligned tail bytes
439 for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
440 data = (data << 8) | *src++;
443 for (; i < port_width; ++i, ++cp) {
444 data = (data << 8) | (*(uchar *) cp);
447 return (write_data (info, wp, SWAP (data)));
450 /*-----------------------------------------------------------------------
451 * Write a word or halfword to Flash, returns:
454 * 2 - Flash not erased
456 static int write_data (flash_info_t * info, ulong dest, FPW data)
458 FPWV *addr = (FPWV *) dest;
462 /* Check if Flash is (sufficiently) erased */
463 if ((*addr & data) != data) {
464 printf ("not erased at %08lx (%x)\n", (ulong) addr, *addr);
467 /* Disable interrupts which might cause a timeout here */
468 flag = disable_interrupts ();
469 *addr = (FPW) 0x00400040; /* write setup */
472 /* arm simple, non interrupt dependent timer */
473 reset_timer_masked ();
475 /* wait while polling the status register */
476 while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
477 if (get_timer_masked () > CONFIG_SYS_FLASH_WRITE_TOUT) {
478 *addr = (FPW) 0x00FF00FF; /* restore read mode */
482 *addr = (FPW) 0x00FF00FF; /* restore read mode */
486 void inline spin_wheel (void)
489 static char w[] = "\\/-";
491 printf ("\010%c", w[p]);
492 (++p == 3) ? (p = 0) : 0;