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 #ifdef CFG_ENV_IS_IN_FLASH
123 /* Protect monitor and environment sectors
125 flash_protect (FLAG_PROTECT_SET,
127 CFG_FLASH_BASE + monitor_flash_len - 1, &flash_info[0]);
129 flash_protect (FLAG_PROTECT_SET,
131 CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0]);
136 /*-----------------------------------------------------------------------
138 void flash_unlock(flash_info_t * info, int bank)
142 j=2; /* leave 0,1 locked for boot bank */
144 j=0; /* get the whole bank for #2 */
146 for (;j<CFG_MAX_FLASH_SECT;j++) {
147 FPWV *addr = (FPWV *) (info->start[j]);
149 printf("Warning Flash probe failed\n");
152 flash_unprotect_sectors (addr);
153 *addr = (FPW) 0x00500050;/* clear status register */
154 *addr = (FPW) 0x00FF00FF;/* resest to read mode */
158 /*-----------------------------------------------------------------------
160 static void flash_get_offsets (ulong base, flash_info_t * info)
163 volatile int r; /* gcc 3.4.0-1 strangeness, need to follow up.*/
165 if (info->flash_id == FLASH_UNKNOWN) {
169 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
170 for (i = 0; i < info->sector_count; i++) {
171 if (i > 254) { /* 255,256,257,258 */
173 info->start[i] = base + (((r-(int)255) * SZ_32K) + (255*PHYS_FLASH_SECT_SIZE));
174 info->protect[i] = 0;
176 info->start[i] = base + (i * PHYS_FLASH_SECT_SIZE);
177 info->protect[i] = 0;
183 /*-----------------------------------------------------------------------
185 void flash_print_info (flash_info_t * info)
189 if (info->flash_id == FLASH_UNKNOWN) {
190 printf ("missing or unknown FLASH type\n");
194 switch (info->flash_id & FLASH_VENDMASK) {
195 case FLASH_MAN_INTEL:
199 printf ("Unknown Vendor ");
203 switch (info->flash_id & FLASH_TYPEMASK) {
204 case FLASH_28F256L18T:
205 printf ("FLASH 28F256L18T\n");
208 printf ("Unknown Chip Type\n");
212 printf (" Size: %ld MB in %d Sectors\n",
213 info->size >> 20, info->sector_count);
215 printf (" Sector Start Addresses:");
216 for (i = 0; i < info->sector_count; ++i) {
220 info->start[i], info->protect[i] ? " (RO)" : " ");
227 * The following code cannot be run from FLASH!
229 static ulong flash_get_size (FPW * addr, flash_info_t * info)
232 /* mb(); this one makes ARM11 err go away, but I want it :) as a guide to problems */
234 /* Write auto select command: read Manufacturer ID */
235 addr[0x5555] = (FPW) 0x00AA00AA;
236 addr[0x2AAA] = (FPW) 0x00550055;
237 addr[0x5555] = (FPW) 0x00900090;
240 value = addr[0] & 0xFF; /* just looking for 89 (8989 is hw pat)*/
244 case (FPW) INTEL_MANUFACT:
245 info->flash_id = FLASH_MAN_INTEL;
249 info->flash_id = FLASH_UNKNOWN;
250 info->sector_count = 0;
252 addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
253 return(0); /* no or unknown flash */
257 value = addr[1]; /* device ID */
260 case (FPW) (INTEL_ID_28F256L18T): /* 880D */
261 info->flash_id += FLASH_28F256L18T;
262 info->sector_count = 259; /*0-258*/
264 break; /* => 32 MB */
267 info->flash_id = FLASH_UNKNOWN;
271 if (info->sector_count > CFG_MAX_FLASH_SECT) {
272 printf ("** ERROR: sector count %d > max (%d) **\n",
273 info->sector_count, CFG_MAX_FLASH_SECT);
274 info->sector_count = CFG_MAX_FLASH_SECT;
277 addr[0] = (FPW) 0x00FF00FF; /* restore read mode */
283 /* unprotects a sector for write and erase
284 * on some intel parts, this unprotects the entire chip, but it
285 * wont hurt to call this additional times per sector...
287 void flash_unprotect_sectors (FPWV * addr)
289 #define PD_FINTEL_WSMS_READY_MASK 0x0080
291 *addr = (FPW) 0x00500050; /* clear status register */
293 /* this sends the clear lock bit command */
294 *addr = (FPW) 0x00600060;
295 *addr = (FPW) 0x00D000D0;
299 /*-----------------------------------------------------------------------
302 int flash_erase (flash_info_t * info, int s_first, int s_last)
305 ulong type, start, last;
307 #ifdef CONFIG_USE_IRQ
311 if ((s_first < 0) || (s_first > s_last)) {
312 if (info->flash_id == FLASH_UNKNOWN) {
313 printf ("- missing\n");
315 printf ("- no sectors to erase\n");
320 type = (info->flash_id & FLASH_VENDMASK);
321 if ((type != FLASH_MAN_INTEL)) {
322 printf ("Can't erase unknown flash type %08lx - aborted\n",
328 for (sect = s_first; sect <= s_last; ++sect) {
329 if (info->protect[sect]) {
335 printf ("- Warning: %d protected sectors will not be erased!\n",
342 start = get_timer (0);
345 #ifdef CONFIG_USE_IRQ
346 /* Disable interrupts which might cause a timeout here */
347 iflag = disable_interrupts ();
350 /* Start erase on unprotected sectors */
351 for (sect = s_first; sect <= s_last; sect++) {
352 if (info->protect[sect] == 0) { /* not protected */
353 FPWV *addr = (FPWV *) (info->start[sect]);
356 printf ("Erasing sector %2d ... ", sect);
358 flash_unprotect_sectors (addr);
360 /* arm simple, non interrupt dependent timer */
361 reset_timer_masked ();
363 *addr = (FPW) 0x00500050;/* clear status register */
364 *addr = (FPW) 0x00200020;/* erase setup */
365 *addr = (FPW) 0x00D000D0;/* erase confirm */
368 *addr) & (FPW) 0x00800080) !=
370 if (get_timer_masked () >
371 CFG_FLASH_ERASE_TOUT) {
372 printf ("Timeout\n");
374 *addr = (FPW) 0x00B000B0;
375 /* reset to read mode */
376 *addr = (FPW) 0x00FF00FF;
382 /* clear status register cmd. */
383 *addr = (FPW) 0x00500050;
384 *addr = (FPW) 0x00FF00FF;/* resest to read mode */
388 #ifdef CONFIG_USE_IRQ
396 /*-----------------------------------------------------------------------
397 * Copy memory to flash, returns:
400 * 2 - Flash not erased
401 * 4 - Flash not identified
404 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
408 int count, i, l, rc, port_width;
410 if (info->flash_id == FLASH_UNKNOWN) {
413 /* get lower word aligned address */
414 #ifdef FLASH_PORT_WIDTH16
423 * handle unaligned start bytes
425 if ((l = addr - wp) != 0) {
427 for (i = 0, cp = wp; i < l; ++i, ++cp) {
428 data = (data << 8) | (*(uchar *) cp);
430 for (; i < port_width && cnt > 0; ++i) {
431 data = (data << 8) | *src++;
435 for (; cnt == 0 && i < port_width; ++i, ++cp) {
436 data = (data << 8) | (*(uchar *) cp);
439 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
446 * handle word aligned part
449 while (cnt >= port_width) {
451 for (i = 0; i < port_width; ++i) {
452 data = (data << 8) | *src++;
454 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
459 if (count++ > 0x800) {
470 * handle unaligned tail bytes
473 for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
474 data = (data << 8) | *src++;
477 for (; i < port_width; ++i, ++cp) {
478 data = (data << 8) | (*(uchar *) cp);
481 return(write_data (info, wp, SWAP (data)));
484 /*-----------------------------------------------------------------------
485 * Write a word or halfword to Flash, returns:
488 * 2 - Flash not erased
490 static int write_data (flash_info_t * info, ulong dest, FPW data)
492 FPWV *addr = (FPWV *) dest;
494 #ifdef CONFIG_USE_IRQ
498 /* Check if Flash is (sufficiently) erased */
499 if ((*addr & data) != data) {
500 printf ("not erased at %08lx (%x)\n", (ulong) addr, *addr);
503 /* Disable interrupts which might cause a timeout here */
504 #ifdef CONFIG_USE_IRQ
505 iflag = disable_interrupts ();
507 *addr = (FPW) 0x00400040; /* write setup */
510 /* arm simple, non interrupt dependent timer */
511 reset_timer_masked ();
513 /* wait while polling the status register */
514 while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
515 if (get_timer_masked () > CFG_FLASH_WRITE_TOUT) {
516 *addr = (FPW) 0x00FF00FF; /* restore read mode */
520 *addr = (FPW) 0x00FF00FF; /* restore read mode */
522 #ifdef CONFIG_USE_IRQ
530 void inline spin_wheel (void)
533 static char w[] = "\\/-";
535 printf ("\010%c", w[p]);
536 (++p == 3) ? (p = 0) : 0;