3 * Xiaogeng (Shawn) Jin, Agilent Technologies, xiaogeng_jin@agilent.com
6 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
8 * (C) Copyright 2001-2004
9 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
12 * Texas Instruments, <www.ti.com>
13 * Kshitij Gupta <Kshitij@ti.com>
15 * See file CREDITS for list of people who contributed to this
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License as
20 * published by the Free Software Foundation; either version 2 of
21 * the License, or (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
35 #include <linux/byteorder/swab.h>
39 #define PHYS_FLASH_SECT_SIZE 0x00040000 /* 256 KB sectors (x2) */
40 flash_info_t flash_info
[CFG_MAX_FLASH_BANKS
]; /* info for FLASH chips */
42 /* Board support for 1 or 2 flash devices */
43 #define FLASH_PORT_WIDTH32
44 #undef FLASH_PORT_WIDTH16
46 #ifdef FLASH_PORT_WIDTH16
47 #define FLASH_PORT_WIDTH ushort
48 #define FLASH_PORT_WIDTHV vu_short
49 #define SWAP(x) __swab16(x)
51 #define FLASH_PORT_WIDTH ulong
52 #define FLASH_PORT_WIDTHV vu_long
53 #define SWAP(x) __swab32(x)
56 #define FPW FLASH_PORT_WIDTH
57 #define FPWV FLASH_PORT_WIDTHV
59 #define mb() __asm__ __volatile__ ("" : : : "memory")
62 /* Flash Organization Structure */
63 typedef struct OrgDef
{
64 unsigned int sector_number
;
65 unsigned int sector_size
;
69 /* Flash Organizations */
70 OrgDef OrgIntel_28F256L18T
[] = {
71 {4, 32 * 1024}, /* 4 * 32kBytes sectors */
72 {255, 128 * 1024}, /* 255 * 128kBytes sectors */
75 /* CP control register base address */
76 #define CPCR_BASE 0xCB000000
77 #define CPCR_EXTRABANK 0x8
78 #define CPCR_FLASHSIZE 0x4
79 #define CPCR_FLWREN 0x2
80 #define CPCR_FLVPPEN 0x1
82 /*-----------------------------------------------------------------------
85 unsigned long flash_init (void);
86 static ulong
flash_get_size (FPW
* addr
, flash_info_t
* info
);
87 static int write_data (flash_info_t
* info
, ulong dest
, FPW data
);
88 static void flash_get_offsets (ulong base
, flash_info_t
* info
);
89 void inline spin_wheel (void);
90 void flash_print_info (flash_info_t
* info
);
91 void flash_unprotect_sectors (FPWV
* addr
);
92 int flash_erase (flash_info_t
* info
, int s_first
, int s_last
);
93 int write_buff (flash_info_t
* info
, uchar
* src
, ulong addr
, ulong cnt
);
95 /*-----------------------------------------------------------------------
97 unsigned long flash_init (void)
101 vu_long
*cpcr
= (vu_long
*)CPCR_BASE
;
103 /* Check if there is an extra bank of flash */
104 if (cpcr
[1] & CPCR_EXTRABANK
)
109 if (nbanks
> CFG_MAX_FLASH_BANKS
)
110 nbanks
= CFG_MAX_FLASH_BANKS
;
112 /* Enable flash write */
115 for (i
= 0; i
< nbanks
; i
++) {
116 flash_get_size ((FPW
*)(CFG_FLASH_BASE
+ size
), &flash_info
[i
]);
117 flash_get_offsets (CFG_FLASH_BASE
+ size
, &flash_info
[i
]);
118 size
+= flash_info
[i
].size
;
121 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
122 /* monitor protection */
123 flash_protect (FLAG_PROTECT_SET
,
125 CFG_MONITOR_BASE
+ monitor_flash_len
- 1, &flash_info
[0]);
128 #ifdef CFG_ENV_IS_IN_FLASH
129 /* ENV protection ON */
130 flash_protect(FLAG_PROTECT_SET
,
132 CFG_ENV_ADDR
+ CFG_ENV_SECT_SIZE
- 1,
136 /* Protect SIB (0x24800000) and bootMonitor (0x24c00000) */
137 flash_protect (FLAG_PROTECT_SET
,
138 flash_info
[0].start
[62],
139 flash_info
[0].start
[63] + PHYS_FLASH_SECT_SIZE
- 1,
145 /*-----------------------------------------------------------------------
147 static void flash_get_offsets (ulong base
, flash_info_t
* info
)
151 if (info
->flash_id
== FLASH_UNKNOWN
) {
155 if ((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_INTEL
) {
156 for (i
= 0; i
< info
->sector_count
; i
++) {
157 info
->start
[i
] = base
+ (i
* PHYS_FLASH_SECT_SIZE
);
158 info
->protect
[i
] = 0;
163 /*-----------------------------------------------------------------------
165 void flash_print_info (flash_info_t
* info
)
169 if (info
->flash_id
== FLASH_UNKNOWN
) {
170 printf ("missing or unknown FLASH type\n");
174 switch (info
->flash_id
& FLASH_VENDMASK
) {
175 case FLASH_MAN_INTEL
:
179 printf ("Unknown Vendor ");
183 /* Integrator CP board uses 28F640J3C or 28F128J3C parts,
184 * which have the same device id numbers as 28F640J3A or
187 switch (info
->flash_id
& FLASH_TYPEMASK
) {
188 case FLASH_28F256L18T
:
189 printf ("FLASH 28F256L18T\n");
191 case FLASH_28F640J3A
:
192 printf ("FLASH 28F640J3C\n");
194 case FLASH_28F128J3A
:
195 printf ("FLASH 28F128J3C\n");
198 printf ("Unknown Chip Type\n");
202 printf (" Size: %ld MB in %d Sectors\n",
203 info
->size
>> 20, info
->sector_count
);
205 printf (" Sector Start Addresses:");
206 for (i
= 0; i
< info
->sector_count
; ++i
) {
210 info
->start
[i
], info
->protect
[i
] ? " (RO)" : " ");
217 * The following code cannot be run from FLASH!
219 static ulong
flash_get_size (FPW
* addr
, flash_info_t
* info
)
222 vu_long
*cpcr
= (vu_long
*)CPCR_BASE
;
225 /* Check the flash size */
226 if (cpcr
[1] & CPCR_FLASHSIZE
)
231 if (nsects
> CFG_MAX_FLASH_SECT
)
232 nsects
= CFG_MAX_FLASH_SECT
;
234 /* Write auto select command: read Manufacturer ID */
235 addr
[0x5555] = (FPW
) 0x00AA00AA;
236 addr
[0x2AAA] = (FPW
) 0x00550055;
237 addr
[0x5555] = (FPW
) 0x00900090;
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
):
261 info
->flash_id
+= FLASH_28F256L18T
;
262 info
->sector_count
= 259;
263 info
->size
= 0x02000000;
264 break; /* => 32 MB */
266 case (FPW
) (INTEL_ID_28F640J3A
):
267 info
->flash_id
+= FLASH_28F640J3A
;
268 info
->sector_count
= nsects
;
269 info
->size
= nsects
* PHYS_FLASH_SECT_SIZE
;
272 case (FPW
) (INTEL_ID_28F128J3A
):
273 info
->flash_id
+= FLASH_28F128J3A
;
274 info
->sector_count
= nsects
;
275 info
->size
= nsects
* PHYS_FLASH_SECT_SIZE
;
279 info
->flash_id
= FLASH_UNKNOWN
;
283 if (info
->sector_count
> CFG_MAX_FLASH_SECT
) {
284 printf ("** ERROR: sector count %d > max (%d) **\n",
285 info
->sector_count
, CFG_MAX_FLASH_SECT
);
286 info
->sector_count
= CFG_MAX_FLASH_SECT
;
289 addr
[0] = (FPW
) 0x00FF00FF; /* restore read mode */
295 /* unprotects a sector for write and erase
296 * on some intel parts, this unprotects the entire chip, but it
297 * wont hurt to call this additional times per sector...
299 void flash_unprotect_sectors (FPWV
* addr
)
303 *addr
= (FPW
) 0x00500050; /* clear status register */
305 /* this sends the clear lock bit command */
306 *addr
= (FPW
) 0x00600060;
307 *addr
= (FPW
) 0x00D000D0;
309 reset_timer_masked();
310 while (((status
= *addr
) & (FPW
)0x00800080) != 0x00800080) {
311 if (get_timer_masked() > CFG_FLASH_ERASE_TOUT
) {
317 *addr
= (FPW
) 0x00FF00FF;
321 /*-----------------------------------------------------------------------
323 int flash_erase (flash_info_t
* info
, int s_first
, int s_last
)
325 int flag
, prot
, sect
;
329 if ((s_first
< 0) || (s_first
> s_last
)) {
330 if (info
->flash_id
== FLASH_UNKNOWN
) {
331 printf ("- missing\n");
333 printf ("- no sectors to erase\n");
338 type
= (info
->flash_id
& FLASH_VENDMASK
);
339 if ((type
!= FLASH_MAN_INTEL
)) {
340 printf ("Can't erase unknown flash type %08lx - aborted\n",
346 for (sect
= s_first
; sect
<= s_last
; ++sect
) {
347 if (info
->protect
[sect
]) {
353 printf ("- Warning: %d protected sectors will not be erased!\n",
359 /* Start erase on unprotected sectors */
360 for (sect
= s_first
; sect
<= s_last
; sect
++) {
361 if (info
->protect
[sect
] == 0) { /* not protected */
362 FPWV
*addr
= (FPWV
*) (info
->start
[sect
]);
365 printf ("Erasing sector %2d ... ", sect
);
367 /* Disable interrupts which might cause a timeout here */
368 flag
= disable_interrupts ();
370 /* flash_unprotect_sectors (addr); */
372 /* arm simple, non interrupt dependent timer */
373 reset_timer_masked ();
375 *addr
= (FPW
) 0x00500050; /* clear status register */
376 *addr
= (FPW
) 0x00200020; /* erase setup */
377 *addr
= (FPW
) 0x00D000D0; /* erase confirm */
380 udelay(1000); /* Let's wait 1 ms */
382 /* re-enable interrupts if necessary */
386 while (((status
= *addr
) & (FPW
) 0x00800080) != (FPW
) 0x00800080) {
387 if (get_timer_masked () > CFG_FLASH_ERASE_TOUT
) {
388 *addr
= (FPW
)0x00700070;
390 if ((status
& (FPW
) 0x00400040) == (FPW
) 0x00400040) {
391 /* erase suspended? Resume it */
392 reset_timer_masked();
393 *addr
= (FPW
) 0x00D000D0;
396 printf ("Timeout,0x%08x\n", status
);
401 *addr
= (FPW
) 0x00500050;
402 *addr
= (FPW
) 0x00FF00FF; /* reset to read mode */
409 *addr
= (FPW
) 0x00FF00FF; /* resest to read mode */
417 /*-----------------------------------------------------------------------
418 * Copy memory to flash, returns:
421 * 2 - Flash not erased
422 * 4 - Flash not identified
424 int write_buff (flash_info_t
* info
, uchar
* src
, ulong addr
, ulong cnt
)
428 int count
, i
, l
, rc
, port_width
;
430 if (info
->flash_id
== FLASH_UNKNOWN
) {
433 /* get lower word aligned address */
434 #ifdef FLASH_PORT_WIDTH16
443 * handle unaligned start bytes
445 if ((l
= addr
- wp
) != 0) {
447 for (i
= 0, cp
= wp
; i
< l
; ++i
, ++cp
) {
448 data
= (data
<< 8) | (*(uchar
*) cp
);
450 for (; i
< port_width
&& cnt
> 0; ++i
) {
451 data
= (data
<< 8) | *src
++;
455 for (; cnt
== 0 && i
< port_width
; ++i
, ++cp
) {
456 data
= (data
<< 8) | (*(uchar
*) cp
);
459 if ((rc
= write_data (info
, wp
, SWAP (data
))) != 0) {
466 * handle word aligned part
469 while (cnt
>= port_width
) {
471 for (i
= 0; i
< port_width
; ++i
) {
472 data
= (data
<< 8) | *src
++;
474 if ((rc
= write_data (info
, wp
, SWAP (data
))) != 0) {
479 if (count
++ > 0x800) {
490 * handle unaligned tail bytes
493 for (i
= 0, cp
= wp
; i
< port_width
&& cnt
> 0; ++i
, ++cp
) {
494 data
= (data
<< 8) | *src
++;
497 for (; i
< port_width
; ++i
, ++cp
) {
498 data
= (data
<< 8) | (*(uchar
*) cp
);
501 return (write_data (info
, wp
, SWAP (data
)));
504 /*-----------------------------------------------------------------------
505 * Write a word or halfword to Flash, returns:
508 * 2 - Flash not erased
510 static int write_data (flash_info_t
* info
, ulong dest
, FPW data
)
512 FPWV
*addr
= (FPWV
*) dest
;
516 /* Check if Flash is (sufficiently) erased */
517 if ((*addr
& data
) != data
) {
518 printf ("not erased at %08lx (%x)\n", (ulong
) addr
, *addr
);
522 /* Disable interrupts which might cause a timeout here */
523 flag
= disable_interrupts ();
525 /* flash_unprotect_sectors (addr); */
527 *addr
= (FPW
) 0x00400040; /* write setup */
532 /* re-enable interrupts if necessary */
536 /* arm simple, non interrupt dependent timer */
537 reset_timer_masked ();
539 /* wait while polling the status register */
540 while (((status
= *addr
) & (FPW
) 0x00800080) != (FPW
) 0x00800080) {
541 if (get_timer_masked () > CFG_FLASH_WRITE_TOUT
) {
543 *addr
= (FPW
) 0x00700070;
545 printf("## status=0x%08x, addr=0x%08x\n", status
, addr
);
547 *addr
= (FPW
) 0x00500050; /* clear status register cmd */
548 *addr
= (FPW
) 0x00FF00FF; /* restore read mode */
553 *addr
= (FPW
) 0x00FF00FF; /* restore read mode */
557 void inline spin_wheel (void)
560 static char w
[] = "\\/-";
562 printf ("\010%c", w
[p
]);
563 (++p
== 3) ? (p
= 0) : 0;