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 <asm/arch/sizes.h>
33 #include <linux/byteorder/swab.h>
35 #define PHYS_FLASH_SECT_SIZE SZ_128K
36 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
38 /* Board support for 1 or 2 flash devices */
39 #undef FLASH_PORT_WIDTH32
40 #define FLASH_PORT_WIDTH16
42 #ifdef FLASH_PORT_WIDTH16
43 # define FLASH_PORT_WIDTH ushort
44 # define FLASH_PORT_WIDTHV vu_short
45 # define SWAP(x) __swab16(x)
47 # define FLASH_PORT_WIDTH ulong
48 # define FLASH_PORT_WIDTHV vu_long
49 # define SWAP(x) __swab32(x)
52 #define FPW FLASH_PORT_WIDTH
53 #define FPWV FLASH_PORT_WIDTHV
55 #define mb() __asm__ __volatile__ ("" : : : "memory")
58 /* Flash Organization Structure */
59 typedef struct OrgDef {
60 unsigned int sector_number;
61 unsigned int sector_size;
65 /* Flash Organizations */
66 OrgDef OrgIntel_28F256L18T[] = {
67 {4, SZ_32K}, /* 4 * 32kBytes sectors */
68 {255, SZ_128K}, /* 255 * 128kBytes sectors */
72 /*-----------------------------------------------------------------------
75 unsigned long flash_init (void);
76 static ulong flash_get_size (FPW * addr, flash_info_t * info);
77 static int write_data (flash_info_t * info, ulong dest, FPW data);
78 static void flash_get_offsets (ulong base, flash_info_t * info);
79 void inline spin_wheel (void);
80 void flash_print_info (flash_info_t * info);
81 void flash_unprotect_sectors (FPWV * addr);
82 int flash_erase (flash_info_t * info, int s_first, int s_last);
83 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt);
84 void flash_unlock(flash_info_t * info, int bank);
85 int flash_probe(void);
87 /*-----------------------------------------------------------------------
90 /* see if flash is ok */
93 return(flash_get_size ((FPW *) PHYS_FLASH_1, &flash_info[0]));
96 unsigned long flash_init (void)
100 for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
103 flash_get_size ((FPW *) PHYS_FLASH_1, &flash_info[i]);
104 flash_get_offsets (PHYS_FLASH_1, &flash_info[i]);
105 /* to reset the lock bit */
106 flash_unlock(&flash_info[i],i);
109 flash_get_size ((FPW *) PHYS_FLASH_2, &flash_info[i]);
110 flash_get_offsets (PHYS_FLASH_2, &flash_info[i]);
111 /* to reset the lock bit */
112 flash_unlock(&flash_info[i],i);
116 panic ("configured too many flash banks!\n");
119 size += flash_info[i].size;
122 /* Protect monitor and environment sectors
124 flash_protect (FLAG_PROTECT_SET,
126 CFG_FLASH_BASE + monitor_flash_len - 1, &flash_info[0]);
128 flash_protect (FLAG_PROTECT_SET,
130 CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0]);
135 /*-----------------------------------------------------------------------
137 void flash_unlock(flash_info_t * info, int bank)
141 j=2; /* leave 0,1 locked for boot bank */
143 j=0; /* get the whole bank for #2 */
145 for (;j<CFG_MAX_FLASH_SECT;j++) {
146 FPWV *addr = (FPWV *) (info->start[j]);
148 printf("Warning Flash probe failed\n");
151 flash_unprotect_sectors (addr);
152 *addr = (FPW) 0x00500050;/* clear status register */
153 *addr = (FPW) 0x00FF00FF;/* resest to read mode */
157 /*-----------------------------------------------------------------------
159 static void flash_get_offsets (ulong base, flash_info_t * info)
162 volatile int r; /* gcc 3.4.0-1 strangeness, need to follow up.*/
164 if (info->flash_id == FLASH_UNKNOWN) {
168 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
169 for (i = 0; i < info->sector_count; i++) {
170 if (i > 254) { /* 255,256,257,258 */
172 info->start[i] = base + (((r-(int)255) * SZ_32K) + (255*PHYS_FLASH_SECT_SIZE));
173 info->protect[i] = 0;
175 info->start[i] = base + (i * PHYS_FLASH_SECT_SIZE);
176 info->protect[i] = 0;
182 /*-----------------------------------------------------------------------
184 void flash_print_info (flash_info_t * info)
188 if (info->flash_id == FLASH_UNKNOWN) {
189 printf ("missing or unknown FLASH type\n");
193 switch (info->flash_id & FLASH_VENDMASK) {
194 case FLASH_MAN_INTEL:
198 printf ("Unknown Vendor ");
202 switch (info->flash_id & FLASH_TYPEMASK) {
203 case FLASH_28F256L18T:
204 printf ("FLASH 28F256L18T\n");
207 printf ("Unknown Chip Type\n");
211 printf (" Size: %ld MB in %d Sectors\n",
212 info->size >> 20, info->sector_count);
214 printf (" Sector Start Addresses:");
215 for (i = 0; i < info->sector_count; ++i) {
219 info->start[i], info->protect[i] ? " (RO)" : " ");
226 * The following code cannot be run from FLASH!
228 static ulong flash_get_size (FPW * addr, flash_info_t * info)
231 /* mb(); this one makes ARM11 err go away, but I want it :) as a guide to problems */
233 /* Write auto select command: read Manufacturer ID */
234 addr[0x5555] = (FPW) 0x00AA00AA;
235 addr[0x2AAA] = (FPW) 0x00550055;
236 addr[0x5555] = (FPW) 0x00900090;
239 value = addr[0] & 0xFF; /* just looking for 89 (8989 is hw pat)*/
243 case (FPW) INTEL_MANUFACT:
244 info->flash_id = FLASH_MAN_INTEL;
248 info->flash_id = FLASH_UNKNOWN;
249 info->sector_count = 0;
251 addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
252 return(0); /* no or unknown flash */
256 value = addr[1]; /* device ID */
259 case (FPW) (INTEL_ID_28F256L18T): /* 880D */
260 info->flash_id += FLASH_28F256L18T;
261 info->sector_count = 259; /*0-258*/
263 break; /* => 32 MB */
266 info->flash_id = FLASH_UNKNOWN;
270 if (info->sector_count > CFG_MAX_FLASH_SECT) {
271 printf ("** ERROR: sector count %d > max (%d) **\n",
272 info->sector_count, CFG_MAX_FLASH_SECT);
273 info->sector_count = CFG_MAX_FLASH_SECT;
276 addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
282 /* unprotects a sector for write and erase
283 * on some intel parts, this unprotects the entire chip, but it
284 * wont hurt to call this additional times per sector...
286 void flash_unprotect_sectors (FPWV * addr)
288 #define PD_FINTEL_WSMS_READY_MASK 0x0080
290 *addr = (FPW) 0x00500050; /* clear status register */
292 /* this sends the clear lock bit command */
293 *addr = (FPW) 0x00600060;
294 *addr = (FPW) 0x00D000D0;
298 /*-----------------------------------------------------------------------
301 int flash_erase (flash_info_t * info, int s_first, int s_last)
304 ulong type, start, last;
306 #ifdef CONFIG_USE_IRQ
310 if ((s_first < 0) || (s_first > s_last)) {
311 if (info->flash_id == FLASH_UNKNOWN) {
312 printf ("- missing\n");
314 printf ("- no sectors to erase\n");
319 type = (info->flash_id & FLASH_VENDMASK);
320 if ((type != FLASH_MAN_INTEL)) {
321 printf ("Can't erase unknown flash type %08lx - aborted\n",
327 for (sect = s_first; sect <= s_last; ++sect) {
328 if (info->protect[sect]) {
334 printf ("- Warning: %d protected sectors will not be erased!\n",
341 start = get_timer (0);
344 #ifdef CONFIG_USE_IRQ
345 /* Disable interrupts which might cause a timeout here */
346 iflag = disable_interrupts ();
349 /* Start erase on unprotected sectors */
350 for (sect = s_first; sect <= s_last; sect++) {
351 if (info->protect[sect] == 0) { /* not protected */
352 FPWV *addr = (FPWV *) (info->start[sect]);
355 printf ("Erasing sector %2d ... ", sect);
357 flash_unprotect_sectors (addr);
359 /* arm simple, non interrupt dependent timer */
360 reset_timer_masked ();
362 *addr = (FPW) 0x00500050;/* clear status register */
363 *addr = (FPW) 0x00200020;/* erase setup */
364 *addr = (FPW) 0x00D000D0;/* erase confirm */
367 *addr) & (FPW) 0x00800080) !=
369 if (get_timer_masked () >
370 CFG_FLASH_ERASE_TOUT) {
371 printf ("Timeout\n");
373 *addr = (FPW) 0x00B000B0;
374 /* reset to read mode */
375 *addr = (FPW) 0x00FF00FF;
381 /* clear status register cmd. */
382 *addr = (FPW) 0x00500050;
383 *addr = (FPW) 0x00FF00FF;/* resest to read mode */
387 #ifdef CONFIG_USE_IRQ
395 /*-----------------------------------------------------------------------
396 * Copy memory to flash, returns:
399 * 2 - Flash not erased
400 * 4 - Flash not identified
403 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
407 int count, i, l, rc, port_width;
409 if (info->flash_id == FLASH_UNKNOWN) {
412 /* get lower word aligned address */
413 #ifdef FLASH_PORT_WIDTH16
422 * handle unaligned start bytes
424 if ((l = addr - wp) != 0) {
426 for (i = 0, cp = wp; i < l; ++i, ++cp) {
427 data = (data << 8) | (*(uchar *) cp);
429 for (; i < port_width && cnt > 0; ++i) {
430 data = (data << 8) | *src++;
434 for (; cnt == 0 && i < port_width; ++i, ++cp) {
435 data = (data << 8) | (*(uchar *) cp);
438 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
445 * handle word aligned part
448 while (cnt >= port_width) {
450 for (i = 0; i < port_width; ++i) {
451 data = (data << 8) | *src++;
453 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
458 if (count++ > 0x800) {
469 * handle unaligned tail bytes
472 for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
473 data = (data << 8) | *src++;
476 for (; i < port_width; ++i, ++cp) {
477 data = (data << 8) | (*(uchar *) cp);
480 return(write_data (info, wp, SWAP (data)));
483 /*-----------------------------------------------------------------------
484 * Write a word or halfword to Flash, returns:
487 * 2 - Flash not erased
489 static int write_data (flash_info_t * info, ulong dest, FPW data)
491 FPWV *addr = (FPWV *) dest;
493 #ifdef CONFIG_USE_IRQ
497 /* Check if Flash is (sufficiently) erased */
498 if ((*addr & data) != data) {
499 printf ("not erased at %08lx (%x)\n", (ulong) addr, *addr);
502 /* Disable interrupts which might cause a timeout here */
503 #ifdef CONFIG_USE_IRQ
504 iflag = disable_interrupts ();
506 *addr = (FPW) 0x00400040; /* write setup */
509 /* arm simple, non interrupt dependent timer */
510 reset_timer_masked ();
512 /* wait while polling the status register */
513 while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
514 if (get_timer_masked () > CFG_FLASH_WRITE_TOUT) {
515 *addr = (FPW) 0x00FF00FF; /* restore read mode */
519 *addr = (FPW) 0x00FF00FF; /* restore read mode */
521 #ifdef CONFIG_USE_IRQ
529 void inline spin_wheel (void)
532 static char w[] = "\\/-";
534 printf ("\010%c", w[p]);
535 (++p == 3) ? (p = 0) : 0;