3 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
4 * Marius Groeger <mgroeger@sysgo.de>
6 * See file CREDITS for list of people who contributed to this
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 #define FLASH_BANK_SIZE 0x800000
31 #define MAIN_SECT_SIZE 0x20000
32 #define PARAM_SECT_SIZE 0x4000
34 /* puzzle magic for lart
35 * data_*_flash are def'd in flashasm.S
38 extern u32
data_from_flash(u32
);
39 extern u32
data_to_flash(u32
);
41 #define PUZZLE_FROM_FLASH(x) data_from_flash((x))
42 #define PUZZLE_TO_FLASH(x) data_to_flash((x))
44 flash_info_t flash_info
[CFG_MAX_FLASH_BANKS
];
47 #define CMD_READ_ARRAY 0x00FF00FF
48 #define CMD_IDENTIFY 0x00900090
49 #define CMD_ERASE_SETUP 0x00200020
50 #define CMD_ERASE_CONFIRM 0x00D000D0
51 #define CMD_PROGRAM 0x00400040
52 #define CMD_RESUME 0x00D000D0
53 #define CMD_SUSPEND 0x00B000B0
54 #define CMD_STATUS_READ 0x00700070
55 #define CMD_STATUS_RESET 0x00500050
57 #define BIT_BUSY 0x00800080
58 #define BIT_ERASE_SUSPEND 0x00400040
59 #define BIT_ERASE_ERROR 0x00200020
60 #define BIT_PROGRAM_ERROR 0x00100010
61 #define BIT_VPP_RANGE_ERROR 0x00080008
62 #define BIT_PROGRAM_SUSPEND 0x00040004
63 #define BIT_PROTECT_ERROR 0x00020002
64 #define BIT_UNDEFINED 0x00010001
66 #define BIT_SEQUENCE_ERROR 0x00300030
67 #define BIT_TIMEOUT 0x80000000
69 /*-----------------------------------------------------------------------
72 ulong
flash_init(void)
77 for (i
= 0; i
< CFG_MAX_FLASH_BANKS
; i
++)
80 flash_info
[i
].flash_id
=
81 (INTEL_MANUFACT
& FLASH_VENDMASK
) |
82 (INTEL_ID_28F160F3B
& FLASH_TYPEMASK
);
83 flash_info
[i
].size
= FLASH_BANK_SIZE
;
84 flash_info
[i
].sector_count
= CFG_MAX_FLASH_SECT
;
85 memset(flash_info
[i
].protect
, 0, CFG_MAX_FLASH_SECT
);
87 flashbase
= PHYS_FLASH_1
;
89 panic("configured too many flash banks!\n");
90 for (j
= 0; j
< flash_info
[i
].sector_count
; j
++)
94 flash_info
[i
].start
[j
] = flashbase
+ j
* PARAM_SECT_SIZE
;
98 flash_info
[i
].start
[j
] = flashbase
+ (j
- 7)*MAIN_SECT_SIZE
;
101 size
+= flash_info
[i
].size
;
104 /* Protect monitor and environment sectors
106 flash_protect(FLAG_PROTECT_SET
,
108 CFG_FLASH_BASE
+ monitor_flash_len
- 1,
111 flash_protect(FLAG_PROTECT_SET
,
113 CFG_ENV_ADDR
+ CFG_ENV_SIZE
- 1,
119 /*-----------------------------------------------------------------------
121 void flash_print_info (flash_info_t
*info
)
125 switch (info
->flash_id
& FLASH_VENDMASK
)
127 case (INTEL_MANUFACT
& FLASH_VENDMASK
):
131 printf("Unknown Vendor ");
135 switch (info
->flash_id
& FLASH_TYPEMASK
)
137 case (INTEL_ID_28F160F3B
& FLASH_TYPEMASK
):
138 printf("2x 28F160F3B (16Mbit)\n");
141 printf("Unknown Chip Type\n");
146 printf(" Size: %ld MB in %d Sectors\n",
147 info
->size
>> 20, info
->sector_count
);
149 printf(" Sector Start Addresses:");
150 for (i
= 0; i
< info
->sector_count
; i
++)
156 printf (" %08lX%s", info
->start
[i
],
157 info
->protect
[i
] ? " (RO)" : " ");
165 /*-----------------------------------------------------------------------
168 int flash_error (ulong code
)
170 /* Check bit patterns */
171 /* SR.7=0 is busy, SR.7=1 is ready */
172 /* all other flags indicate error on 1 */
173 /* SR.0 is undefined */
174 /* Timeout is our faked flag */
176 /* sequence is described in Intel 290644-005 document */
179 if (code
& BIT_TIMEOUT
)
181 printf ("Timeout\n");
185 /* check Busy, SR.7 */
186 if (~code
& BIT_BUSY
)
189 return ERR_PROG_ERROR
;
192 /* check Vpp low, SR.3 */
193 if (code
& BIT_VPP_RANGE_ERROR
)
195 printf ("Vpp range error\n");
196 return ERR_PROG_ERROR
;
199 /* check Device Protect Error, SR.1 */
200 if (code
& BIT_PROTECT_ERROR
)
202 printf ("Device protect error\n");
203 return ERR_PROG_ERROR
;
206 /* check Command Seq Error, SR.4 & SR.5 */
207 if (code
& BIT_SEQUENCE_ERROR
)
209 printf ("Command seqence error\n");
210 return ERR_PROG_ERROR
;
213 /* check Block Erase Error, SR.5 */
214 if (code
& BIT_ERASE_ERROR
)
216 printf ("Block erase error\n");
217 return ERR_PROG_ERROR
;
220 /* check Program Error, SR.4 */
221 if (code
& BIT_PROGRAM_ERROR
)
223 printf ("Program error\n");
224 return ERR_PROG_ERROR
;
227 /* check Block Erase Suspended, SR.6 */
228 if (code
& BIT_ERASE_SUSPEND
)
230 printf ("Block erase suspended\n");
231 return ERR_PROG_ERROR
;
234 /* check Program Suspended, SR.2 */
235 if (code
& BIT_PROGRAM_SUSPEND
)
237 printf ("Program suspended\n");
238 return ERR_PROG_ERROR
;
245 /*-----------------------------------------------------------------------
248 int flash_erase (flash_info_t
*info
, int s_first
, int s_last
)
251 int iflag
, cflag
, prot
, sect
;
254 /* first look for protection bits */
256 if (info
->flash_id
== FLASH_UNKNOWN
)
257 return ERR_UNKNOWN_FLASH_TYPE
;
259 if ((s_first
< 0) || (s_first
> s_last
)) {
263 if ((info
->flash_id
& FLASH_VENDMASK
) !=
264 (INTEL_MANUFACT
& FLASH_VENDMASK
)) {
265 return ERR_UNKNOWN_FLASH_VENDOR
;
269 for (sect
=s_first
; sect
<=s_last
; ++sect
) {
270 if (info
->protect
[sect
]) {
275 return ERR_PROTECTED
;
278 * Disable interrupts which might cause a timeout
279 * here. Remember that our exception vectors are
280 * at address 0 in the flash, and we don't want a
281 * (ticker) exception to happen while the flash
282 * chip is in programming mode.
284 cflag
= icache_status();
286 iflag
= disable_interrupts();
288 /* Start erase on unprotected sectors */
289 for (sect
= s_first
; sect
<=s_last
&& !ctrlc(); sect
++)
291 printf("Erasing sector %2d ... ", sect
);
293 /* arm simple, non interrupt dependent timer */
294 reset_timer_masked();
296 if (info
->protect
[sect
] == 0)
297 { /* not protected */
298 vu_long
*addr
= (vu_long
*)(info
->start
[sect
]);
300 *addr
= PUZZLE_TO_FLASH(CMD_STATUS_RESET
);
301 *addr
= PUZZLE_TO_FLASH(CMD_ERASE_SETUP
);
302 *addr
= PUZZLE_TO_FLASH(CMD_ERASE_CONFIRM
);
304 /* wait until flash is ready */
308 if (get_timer_masked() > CFG_FLASH_ERASE_TOUT
)
310 *addr
= PUZZLE_TO_FLASH(CMD_SUSPEND
);
311 result
= BIT_TIMEOUT
;
315 result
= PUZZLE_FROM_FLASH(*addr
);
316 } while (~result
& BIT_BUSY
);
318 *addr
= PUZZLE_TO_FLASH(CMD_READ_ARRAY
);
320 if ((rc
= flash_error(result
)) != ERR_OK
)
325 else /* it was protected */
327 printf("protected!\n");
332 printf("User Interrupt!\n");
335 /* allow flash to settle - wait 10 ms */
336 udelay_masked(10000);
347 /*-----------------------------------------------------------------------
348 * Copy memory to flash
351 static int write_word (flash_info_t
*info
, ulong dest
, ulong data
)
353 vu_long
*addr
= (vu_long
*)dest
;
358 /* Check if Flash is (sufficiently) erased
360 result
= PUZZLE_FROM_FLASH(*addr
);
361 if ((result
& data
) != data
)
362 return ERR_NOT_ERASED
;
365 * Disable interrupts which might cause a timeout
366 * here. Remember that our exception vectors are
367 * at address 0 in the flash, and we don't want a
368 * (ticker) exception to happen while the flash
369 * chip is in programming mode.
371 cflag
= icache_status();
373 iflag
= disable_interrupts();
375 *addr
= PUZZLE_TO_FLASH(CMD_STATUS_RESET
);
376 *addr
= PUZZLE_TO_FLASH(CMD_PROGRAM
);
379 /* arm simple, non interrupt dependent timer */
380 reset_timer_masked();
382 /* wait until flash is ready */
386 if (get_timer_masked() > CFG_FLASH_ERASE_TOUT
)
388 *addr
= PUZZLE_TO_FLASH(CMD_SUSPEND
);
389 result
= BIT_TIMEOUT
;
393 result
= PUZZLE_FROM_FLASH(*addr
);
394 } while (~result
& BIT_BUSY
);
396 *addr
= PUZZLE_TO_FLASH(CMD_READ_ARRAY
);
398 rc
= flash_error(result
);
409 /*-----------------------------------------------------------------------
410 * Copy memory to flash.
413 int write_buff (flash_info_t
*info
, uchar
*src
, ulong addr
, ulong cnt
)
419 wp
= (addr
& ~3); /* get lower word aligned address */
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
<< 24);
429 for (; i
<4 && cnt
>0; ++i
) {
430 data
= (data
>> 8) | (*src
++ << 24);
434 for (; cnt
==0 && i
<4; ++i
, ++cp
) {
435 data
= (data
>> 8) | (*(uchar
*)cp
<< 24);
438 if ((rc
= write_word(info
, wp
, data
)) != 0) {
445 * handle word aligned part
448 data
= *((vu_long
*)src
);
449 if ((rc
= write_word(info
, wp
, data
)) != 0) {
462 * handle unaligned tail bytes
465 for (i
=0, cp
=wp
; i
<4 && cnt
>0; ++i
, ++cp
) {
466 data
= (data
>> 8) | (*src
++ << 24);
469 for (; i
<4; ++i
, ++cp
) {
470 data
= (data
>> 8) | (*(uchar
*)cp
<< 24);
473 return write_word(info
, wp
, data
);