3 * Greg Ungerer, OpenGear Inc, greg.ungerer@opengear.com
6 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
9 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
11 * See file CREDITS for list of people who contributed to this
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
31 #include <linux/byteorder/swab.h>
34 flash_info_t flash_info
[CFG_MAX_FLASH_BANKS
]; /* info for FLASH chips */
36 #define mb() __asm__ __volatile__ ("" : : : "memory")
38 /*-----------------------------------------------------------------------
41 static ulong
flash_get_size (unsigned char * addr
, flash_info_t
* info
);
42 static int write_data (flash_info_t
* info
, ulong dest
, unsigned char data
);
43 static void flash_get_offsets (ulong base
, flash_info_t
* info
);
44 void inline spin_wheel (void);
46 /*-----------------------------------------------------------------------
49 unsigned long flash_init (void)
54 for (i
= 0; i
< CFG_MAX_FLASH_BANKS
; i
++) {
57 flash_get_size ((unsigned char *) PHYS_FLASH_1
, &flash_info
[i
]);
58 flash_get_offsets (PHYS_FLASH_1
, &flash_info
[i
]);
62 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
65 panic ("configured too many flash banks!\n");
68 size
+= flash_info
[i
].size
;
71 /* Protect monitor and environment sectors
73 flash_protect (FLAG_PROTECT_SET
,
75 CFG_FLASH_BASE
+ _bss_start
- _armboot_start
,
81 /*-----------------------------------------------------------------------
83 static void flash_get_offsets (ulong base
, flash_info_t
* info
)
87 if (info
->flash_id
== FLASH_UNKNOWN
)
90 if ((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_INTEL
) {
91 for (i
= 0; i
< info
->sector_count
; i
++) {
92 info
->start
[i
] = base
+ (i
* PHYS_FLASH_SECT_SIZE
);
98 /*-----------------------------------------------------------------------
100 void flash_print_info (flash_info_t
* info
)
104 if (info
->flash_id
== FLASH_UNKNOWN
) {
105 printf ("missing or unknown FLASH type\n");
109 switch (info
->flash_id
& FLASH_VENDMASK
) {
110 case FLASH_MAN_INTEL
:
114 printf ("Unknown Vendor ");
118 switch (info
->flash_id
& FLASH_TYPEMASK
) {
119 case FLASH_28F128J3A
:
120 printf ("28F128J3A\n");
123 printf ("Unknown Chip Type\n");
127 printf (" Size: %ld MB in %d Sectors\n",
128 info
->size
>> 20, info
->sector_count
);
130 printf (" Sector Start Addresses:");
131 for (i
= 0; i
< info
->sector_count
; ++i
) {
135 info
->start
[i
], info
->protect
[i
] ? " (RO)" : " ");
142 * The following code cannot be run from FLASH!
144 static ulong
flash_get_size (unsigned char * addr
, flash_info_t
* info
)
146 volatile unsigned char value
;
148 /* Write auto select command: read Manufacturer ID */
158 case (unsigned char)INTEL_MANUFACT
:
159 info
->flash_id
= FLASH_MAN_INTEL
;
163 info
->flash_id
= FLASH_UNKNOWN
;
164 info
->sector_count
= 0;
166 addr
[0] = 0xFF; /* restore read mode */
167 return (0); /* no or unknown flash */
171 value
= addr
[2]; /* device ID */
175 case (unsigned char)INTEL_ID_28F640J3A
:
176 info
->flash_id
+= FLASH_28F640J3A
;
177 info
->sector_count
= 64;
178 info
->size
= 0x00800000;
181 case (unsigned char)INTEL_ID_28F128J3A
:
182 info
->flash_id
+= FLASH_28F128J3A
;
183 info
->sector_count
= 128;
184 info
->size
= 0x01000000;
185 break; /* => 16 MB */
188 info
->flash_id
= FLASH_UNKNOWN
;
192 if (info
->sector_count
> CFG_MAX_FLASH_SECT
) {
193 printf ("** ERROR: sector count %d > max (%d) **\n",
194 info
->sector_count
, CFG_MAX_FLASH_SECT
);
195 info
->sector_count
= CFG_MAX_FLASH_SECT
;
198 addr
[0] = 0xFF; /* restore read mode */
204 /*-----------------------------------------------------------------------
207 int flash_erase (flash_info_t
* info
, int s_first
, int s_last
)
209 int flag
, prot
, sect
;
213 if ((s_first
< 0) || (s_first
> s_last
)) {
214 if (info
->flash_id
== FLASH_UNKNOWN
) {
215 printf ("- missing\n");
217 printf ("- no sectors to erase\n");
222 type
= (info
->flash_id
& FLASH_VENDMASK
);
223 if ((type
!= FLASH_MAN_INTEL
)) {
224 printf ("Can't erase unknown flash type %08lx - aborted\n",
230 for (sect
= s_first
; sect
<= s_last
; ++sect
) {
231 if (info
->protect
[sect
]) {
237 printf ("- Warning: %d protected sectors will not be erased!\n", prot
);
241 /* Disable interrupts which might cause a timeout here */
242 flag
= disable_interrupts ();
244 /* Start erase on unprotected sectors */
245 for (sect
= s_first
; sect
<= s_last
; sect
++) {
246 if (info
->protect
[sect
] == 0) { /* not protected */
247 volatile unsigned char *addr
;
248 unsigned char status
;
250 printf ("Erasing sector %2d ... ", sect
);
252 /* arm simple, non interrupt dependent timer */
253 reset_timer_masked ();
255 addr
= (volatile unsigned char *) (info
->start
[sect
]);
256 *addr
= 0x50; /* clear status register */
257 *addr
= 0x20; /* erase setup */
258 *addr
= 0xD0; /* erase confirm */
260 while (((status
= *addr
) & 0x80) != 0x80) {
261 if (get_timer_masked () >
262 CFG_FLASH_ERASE_TOUT
) {
263 printf ("Timeout\n");
264 *addr
= 0xB0; /* suspend erase */
265 *addr
= 0xFF; /* reset to read mode */
271 *addr
= 0x50; /* clear status register cmd */
272 *addr
= 0xFF; /* resest to read mode */
280 /*-----------------------------------------------------------------------
281 * Copy memory to flash, returns:
284 * 2 - Flash not erased
285 * 4 - Flash not identified
288 int write_buff (flash_info_t
* info
, uchar
* src
, ulong addr
, ulong cnt
)
292 int count
, i
, l
, rc
, port_width
;
294 if (info
->flash_id
== FLASH_UNKNOWN
)
301 * handle unaligned start bytes
303 if ((l
= addr
- wp
) != 0) {
305 for (i
= 0, cp
= wp
; i
< l
; ++i
, ++cp
) {
306 data
= (data
<< 8) | (*(uchar
*) cp
);
308 for (; i
< port_width
&& cnt
> 0; ++i
) {
309 data
= (data
<< 8) | *src
++;
313 for (; cnt
== 0 && i
< port_width
; ++i
, ++cp
) {
314 data
= (data
<< 8) | (*(uchar
*) cp
);
317 if ((rc
= write_data (info
, wp
, data
)) != 0) {
324 * handle word aligned part
327 while (cnt
>= port_width
) {
329 for (i
= 0; i
< port_width
; ++i
) {
330 data
= (data
<< 8) | *src
++;
332 if ((rc
= write_data (info
, wp
, data
)) != 0) {
337 if (count
++ > 0x800) {
348 * handle unaligned tail bytes
351 for (i
= 0, cp
= wp
; i
< port_width
&& cnt
> 0; ++i
, ++cp
) {
352 data
= (data
<< 8) | *src
++;
355 for (; i
< port_width
; ++i
, ++cp
) {
356 data
= (data
<< 8) | (*(uchar
*) cp
);
359 return (write_data (info
, wp
, data
));
362 /*-----------------------------------------------------------------------
363 * Write a word or halfword to Flash, returns:
366 * 2 - Flash not erased
368 static int write_data (flash_info_t
* info
, ulong dest
, unsigned char data
)
370 volatile unsigned char *addr
= (volatile unsigned char *) dest
;
374 /* Check if Flash is (sufficiently) erased */
375 if ((*addr
& data
) != data
) {
376 printf ("not erased at %08lx (%lx)\n", (ulong
) addr
,
380 /* Disable interrupts which might cause a timeout here */
381 flag
= disable_interrupts ();
383 *addr
= 0x40; /* write setup */
386 /* arm simple, non interrupt dependent timer */
387 reset_timer_masked ();
389 /* wait while polling the status register */
390 while (((status
= *addr
) & 0x80) != 0x80) {
391 if (get_timer_masked () > CFG_FLASH_WRITE_TOUT
) {
392 *addr
= 0xFF; /* restore read mode */
397 *addr
= 0xFF; /* restore read mode */
402 void inline spin_wheel (void)
405 static char w
[] = "\\/-";
407 printf ("\010%c", w
[p
]);
408 (++p
== 3) ? (p
= 0) : 0;