3 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
6 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 * Marius Groeger <mgroeger@sysgo.de>
10 * Robert Schwebel, Pengutronix, <r.schwebel@pengutronix.de>
12 * (C) Copyright 2003 (2 x 16 bit Flash bank patches)
13 * Rolf Peukert, IMMS gGmbH, <rolf.peukert@imms.de>
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 <asm/arch/pxa-regs.h>
37 #define FLASH_BANK_SIZE 0x02000000
38 #define MAIN_SECT_SIZE 0x40000 /* 2x16 = 256k per sector */
40 flash_info_t flash_info
[CFG_MAX_FLASH_BANKS
];
44 * flash_init: - initialize data structures for flash chips
46 * @return: size of the flash
49 ulong
flash_init(void)
54 for (i
= 0; i
< CFG_MAX_FLASH_BANKS
; i
++) {
56 flash_info
[i
].flash_id
=
57 (INTEL_MANUFACT
& FLASH_VENDMASK
) |
58 (INTEL_ID_28F128J3
& FLASH_TYPEMASK
);
59 flash_info
[i
].size
= FLASH_BANK_SIZE
;
60 flash_info
[i
].sector_count
= CFG_MAX_FLASH_SECT
;
61 memset(flash_info
[i
].protect
, 0, CFG_MAX_FLASH_SECT
);
65 flashbase
= PHYS_FLASH_1
;
68 panic("configured too many flash banks!\n");
71 for (j
= 0; j
< flash_info
[i
].sector_count
; j
++) {
72 flash_info
[i
].start
[j
] = flashbase
+ j
*MAIN_SECT_SIZE
;
74 size
+= flash_info
[i
].size
;
77 /* Protect monitor and environment sectors */
78 flash_protect(FLAG_PROTECT_SET
,
80 CFG_FLASH_BASE
+ monitor_flash_len
- 1,
83 flash_protect(FLAG_PROTECT_SET
,
85 CFG_ENV_ADDR
+ CFG_ENV_SIZE
- 1,
93 * flash_print_info: - print information about the flash situation
96 void flash_print_info (flash_info_t
*info
)
100 for (j
=0; j
<CFG_MAX_FLASH_BANKS
; j
++) {
102 switch (info
->flash_id
& FLASH_VENDMASK
) {
103 case (INTEL_MANUFACT
& FLASH_VENDMASK
):
107 printf ("Unknown Vendor ");
111 switch (info
->flash_id
& FLASH_TYPEMASK
) {
112 case (INTEL_ID_28F128J3
& FLASH_TYPEMASK
):
113 printf("28F128J3 (128Mbit)\n");
116 printf("Unknown Chip Type\n");
120 printf(" Size: %ld MB in %d Sectors\n",
121 info
->size
>> 20, info
->sector_count
);
123 printf(" Sector Start Addresses:");
124 for (i
= 0; i
< info
->sector_count
; i
++) {
125 if ((i
% 5) == 0) printf ("\n ");
127 printf (" %08lX%s", info
->start
[i
],
128 info
->protect
[i
] ? " (RO)" : " ");
137 * flash_erase: - erase flash sectors
140 int flash_erase(flash_info_t
*info
, int s_first
, int s_last
)
142 int flag
, prot
, sect
;
145 if (info
->flash_id
== FLASH_UNKNOWN
)
146 return ERR_UNKNOWN_FLASH_TYPE
;
148 if ((s_first
< 0) || (s_first
> s_last
)) {
152 if ((info
->flash_id
& FLASH_VENDMASK
) != (INTEL_MANUFACT
& FLASH_VENDMASK
))
153 return ERR_UNKNOWN_FLASH_VENDOR
;
156 for (sect
=s_first
; sect
<=s_last
; ++sect
) {
157 if (info
->protect
[sect
]) prot
++;
160 if (prot
) return ERR_PROTECTED
;
163 * Disable interrupts which might cause a timeout
164 * here. Remember that our exception vectors are
165 * at address 0 in the flash, and we don't want a
166 * (ticker) exception to happen while the flash
167 * chip is in programming mode.
170 flag
= disable_interrupts();
172 /* Start erase on unprotected sectors */
173 for (sect
= s_first
; sect
<=s_last
&& !ctrlc(); sect
++) {
175 printf("Erasing sector %2d ... ", sect
);
177 /* arm simple, non interrupt dependent timer */
178 reset_timer_masked();
180 if (info
->protect
[sect
] == 0) { /* not protected */
181 u32
* volatile addr
= (u32
* volatile)(info
->start
[sect
]);
184 /* The strata flashs are aligned side by side on */
185 /* the data bus, so we have to write the commands */
186 /* to both chips here: */
188 *addr
= 0x00200020; /* erase setup */
189 *addr
= 0x00D000D0; /* erase confirm */
191 while ((*addr
& 0x00800080) != 0x00800080) {
192 if (get_timer_masked() > CFG_FLASH_ERASE_TOUT
) {
193 *addr
= 0x00B000B0; /* suspend erase*/
194 *addr
= 0x00FF00FF; /* read mode */
199 *addr
= 0x00500050; /* clear status register cmd. */
200 *addr
= 0x00FF00FF; /* reset to read mode */
204 if (ctrlc()) printf("User Interrupt!\n");
207 /* allow flash to settle - wait 10 ms */
208 udelay_masked(10000);
210 if (flag
) enable_interrupts();
216 * write_long: - copy memory to flash, assume a bank of 2 devices with 16bit each
219 static int write_long (flash_info_t
*info
, ulong dest
, ulong data
)
221 u32
* volatile addr
= (u32
* volatile)dest
, val
;
225 /* read array command - just for the case... */
228 /* Check if Flash is (sufficiently) erased */
229 if ((*addr
& data
) != data
) return ERR_NOT_ERASED
;
232 * Disable interrupts which might cause a timeout
233 * here. Remember that our exception vectors are
234 * at address 0 in the flash, and we don't want a
235 * (ticker) exception to happen while the flash
236 * chip is in programming mode.
238 flag
= disable_interrupts();
240 /* clear status register command */
243 /* program set-up command */
246 /* latch address/data */
249 /* arm simple, non interrupt dependent timer */
250 reset_timer_masked();
252 /* wait while polling the status register */
253 while(((val
= *addr
) & 0x00800080) != 0x00800080) {
254 if (get_timer_masked() > CFG_FLASH_WRITE_TOUT
) {
256 /* suspend program command */
262 /* check for errors */
263 if(val
& 0x001A001A) {
264 printf("\nFlash write error %02x at address %08lx\n",
265 (int)val
, (unsigned long)dest
);
266 if(val
& 0x00080008) {
267 printf("Voltage range error.\n");
271 if(val
& 0x00020002) {
272 printf("Device protect error.\n");
276 if(val
& 0x00100010) {
277 printf("Programming error.\n");
286 /* read array command */
288 if (flag
) enable_interrupts();
295 * write_buf: - Copy memory to flash.
298 * @param src: source of copy transaction
299 * @param addr: where to copy to
300 * @param cnt: number of bytes to copy
305 /* "long" version, uses 32bit words */
306 int write_buff (flash_info_t
*info
, uchar
*src
, ulong addr
, ulong cnt
)
313 wp
= (addr
& ~3); /* get lower word aligned address */
316 * handle unaligned start bytes
318 if ((l
= addr
- wp
) != 0) {
320 for (i
=0, cp
=wp
; i
<l
; ++i
, ++cp
) {
321 data
= (data
>> 8) | (*(uchar
*)cp
<< 24);
323 for (; i
<4 && cnt
>0; ++i
) {
324 data
= (data
>> 8) | (*src
++ << 24);
328 for (; cnt
==0 && i
<4; ++i
, ++cp
) {
329 data
= (data
>> 8) | (*(uchar
*)cp
<< 24);
332 if ((rc
= write_long(info
, wp
, data
)) != 0) {
339 * handle word aligned part
342 data
= *((ulong
*)src
);
343 if ((rc
= write_long(info
, wp
, data
)) != 0) {
351 if (cnt
== 0) return ERR_OK
;
354 * handle unaligned tail bytes
357 for (i
=0, cp
=wp
; i
<4 && cnt
>0; ++i
, ++cp
) {
358 data
= (data
>> 8) | (*src
++ << 24);
361 for (; i
<4; ++i
, ++cp
) {
362 data
= (data
>> 8) | (*(uchar
*)cp
<< 24);
365 return write_long(info
, wp
, data
);