3 * Daniel Engström, Omicron Ceti AB, daniel@omicron.se
6 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 * Alex Zuepke <azu@sysgo.de>
9 * See file CREDITS for list of people who contributed to this
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
34 #define SC520_MAX_FLASH_BANKS 3
35 #define SC520_FLASH_BANK0_BASE 0x38000000 /* BOOTCS */
36 #define SC520_FLASH_BANK1_BASE 0x30000000 /* ROMCS0 */
37 #define SC520_FLASH_BANK2_BASE 0x28000000 /* ROMCS1 */
38 #define SC520_FLASH_BANKSIZE 0x8000000
40 #define AMD29LV016_SIZE 0x200000
41 #define AMD29LV016_SECTORS 32
43 flash_info_t flash_info
[SC520_MAX_FLASH_BANKS
];
45 #define CMD_READ_ARRAY 0x00F000F0
46 #define CMD_UNLOCK1 0x00AA00AA
47 #define CMD_UNLOCK2 0x00550055
48 #define CMD_ERASE_SETUP 0x00800080
49 #define CMD_ERASE_CONFIRM 0x00300030
50 #define CMD_PROGRAM 0x00A000A0
51 #define CMD_UNLOCK_BYPASS 0x00200020
54 #define BIT_ERASE_DONE 0x00800080
55 #define BIT_RDY_MASK 0x00800080
56 #define BIT_PROGRAM_ERROR 0x00200020
57 #define BIT_TIMEOUT 0x80000000 /* our flag */
63 /*-----------------------------------------------------------------------
66 ulong
flash_init(void)
71 for (i
= 0; i
< SC520_MAX_FLASH_BANKS
; i
++) {
75 /* FixMe: this assumes that bank 0 and 2
76 * are mapped to the two 8Mb banks */
77 flash_info
[i
].flash_id
=
78 (AMD_MANUFACT
& FLASH_VENDMASK
) |
79 (AMD_ID_LV016B
& FLASH_TYPEMASK
);
81 flash_info
[i
].size
= AMD29LV016_SIZE
*4;
82 flash_info
[i
].sector_count
= AMD29LV016_SECTORS
;
83 sectsize
= (AMD29LV016_SIZE
*4)/AMD29LV016_SECTORS
;
85 /* FixMe: this assumes that bank1 is unmapped
86 * (or mapped to the same flash bank as BOOTCS) */
87 flash_info
[i
].flash_id
= 0;
88 flash_info
[i
].size
= 0;
89 flash_info
[i
].sector_count
= 0;
92 memset(flash_info
[i
].protect
, 0, CFG_MAX_FLASH_SECT
);
95 flashbase
= SC520_FLASH_BANK0_BASE
;
98 flashbase
= SC520_FLASH_BANK1_BASE
;
101 flashbase
= SC520_FLASH_BANK0_BASE
;
104 panic("configured too many flash banks!\n");
107 for (j
= 0; j
< flash_info
[i
].sector_count
; j
++) {
108 flash_info
[i
].start
[j
] = sectsize
;
109 flash_info
[i
].start
[j
] = flashbase
+ j
* sectsize
;
111 size
+= flash_info
[i
].size
;
115 * Protect monitor and environment sectors
117 flash_protect(FLAG_PROTECT_SET
,
118 i386boot_start
-SC520_FLASH_BANK0_BASE
,
119 i386boot_end
-SC520_FLASH_BANK0_BASE
,
123 flash_protect(FLAG_PROTECT_SET
,
125 CFG_ENV_ADDR
+ CFG_ENV_SIZE
- 1,
131 /*-----------------------------------------------------------------------
133 void flash_print_info(flash_info_t
*info
)
137 switch (info
->flash_id
& FLASH_VENDMASK
) {
138 case (AMD_MANUFACT
& FLASH_VENDMASK
):
142 printf("Unknown Vendor ");
146 switch (info
->flash_id
& FLASH_TYPEMASK
) {
147 case (AMD_ID_LV016B
& FLASH_TYPEMASK
):
148 printf("4x Amd29LV016B (16Mbit)\n");
151 printf("Unknown Chip Type\n");
156 printf(" Size: %ld MB in %d Sectors\n",
157 info
->size
>> 20, info
->sector_count
);
159 printf(" Sector Start Addresses:");
160 for (i
= 0; i
< info
->sector_count
; i
++) {
164 printf (" %08lX%s", info
->start
[i
],
165 info
->protect
[i
] ? " (RO)" : " ");
172 /*-----------------------------------------------------------------------
175 int flash_erase(flash_info_t
*info
, int s_first
, int s_last
)
178 int iflag
, prot
, sect
;
182 /* first look for protection bits */
184 if (info
->flash_id
== FLASH_UNKNOWN
) {
185 return ERR_UNKNOWN_FLASH_TYPE
;
188 if ((s_first
< 0) || (s_first
> s_last
)) {
192 if ((info
->flash_id
& FLASH_VENDMASK
) !=
193 (AMD_MANUFACT
& FLASH_VENDMASK
)) {
194 return ERR_UNKNOWN_FLASH_VENDOR
;
198 for (sect
=s_first
; sect
<=s_last
; ++sect
) {
199 if (info
->protect
[sect
]) {
204 return ERR_PROTECTED
;
208 * Disable interrupts which might cause a timeout
209 * here. Remember that our exception vectors are
210 * at address 0 in the flash, and we don't want a
211 * (ticker) exception to happen while the flash
212 * chip is in programming mode.
214 iflag
= disable_interrupts();
216 /* Start erase on unprotected sectors */
217 for (sect
= s_first
; sect
<=s_last
&& !ctrlc(); sect
++) {
218 printf("Erasing sector %2d ... ", sect
);
220 /* arm simple, non interrupt dependent timer */
223 if (info
->protect
[sect
] == 0) {
225 ulong addr
= info
->start
[sect
];
227 writel(CMD_UNLOCK1
, addr
+ 1);
228 writel(CMD_UNLOCK2
, addr
+ 2);
229 writel(CMD_ERASE_SETUP
, addr
+ 1);
231 writel(CMD_UNLOCK1
, addr
+ 1);
232 writel(CMD_UNLOCK2
, addr
+ 2);
233 writel(CMD_ERASE_CONFIRM
, addr
);
236 /* wait until flash is ready */
240 result
= readl(addr
);
243 if (get_timer(0) > CFG_FLASH_ERASE_TOUT
) {
244 writel(CMD_READ_ARRAY
, addr
+ 1);
249 if (!chip1
&& (result
& 0xFFFF) & BIT_ERASE_DONE
) {
253 if (!chip1
&& (result
& 0xFFFF) & BIT_PROGRAM_ERROR
) {
257 if (!chip2
&& (result
>> 16) & BIT_ERASE_DONE
) {
261 if (!chip2
&& (result
>> 16) & BIT_PROGRAM_ERROR
) {
265 } while (!chip1
|| !chip2
);
267 writel(CMD_READ_ARRAY
, addr
+ 1);
269 if (chip1
== ERR
|| chip2
== ERR
) {
280 } else { /* it was protected */
282 printf("protected!\n");
287 printf("User Interrupt!\n");
291 /* allow flash to settle - wait 10 ms */
301 /*-----------------------------------------------------------------------
302 * Copy memory to flash
305 volatile static int write_word(flash_info_t
*info
, ulong dest
, ulong data
)
314 * Check if Flash is (sufficiently) erased
316 result
= readl(addr
);
317 if ((result
& data
) != data
) {
318 return ERR_NOT_ERASED
;
322 * Disable interrupts which might cause a timeout
323 * here. Remember that our exception vectors are
324 * at address 0 in the flash, and we don't want a
325 * (ticker) exception to happen while the flash
326 * chip is in programming mode.
328 iflag
= disable_interrupts();
330 writel(CMD_UNLOCK1
, addr
+ 1);
331 writel(CMD_UNLOCK2
, addr
+ 2);
332 writel(CMD_UNLOCK_BYPASS
, addr
+ 1);
333 writel(addr
, CMD_PROGRAM
);
336 /* arm simple, non interrupt dependent timer */
339 /* wait until flash is ready */
342 result
= readl(addr
);
345 if (get_timer(0) > CFG_FLASH_ERASE_TOUT
) {
350 if (!chip1
&& ((result
& 0x80) == (data
& 0x80))) {
354 if (!chip1
&& ((result
& 0xFFFF) & BIT_PROGRAM_ERROR
)) {
355 result
= readl(addr
);
357 if ((result
& 0x80) == (data
& 0x80)) {
364 if (!chip2
&& ((result
& (0x80 << 16)) == (data
& (0x80 << 16)))) {
368 if (!chip2
&& ((result
>> 16) & BIT_PROGRAM_ERROR
)) {
369 result
= readl(addr
);
371 if ((result
& (0x80 << 16)) == (data
& (0x80 << 16))) {
378 } while (!chip1
|| !chip2
);
380 writel(CMD_READ_ARRAY
, addr
);
382 if (chip1
== ERR
|| chip2
== ERR
|| readl(addr
) != data
) {
393 /*-----------------------------------------------------------------------
394 * Copy memory to flash.
397 int write_buff(flash_info_t
*info
, uchar
*src
, ulong addr
, ulong cnt
)
403 wp
= (addr
& ~3); /* get lower word aligned address */
406 * handle unaligned start bytes
408 if ((l
= addr
- wp
) != 0) {
410 for (i
=0, cp
=wp
; i
<l
; ++i
, ++cp
) {
411 data
= (data
>> 8) | (*(uchar
*)cp
<< 24);
413 for (; i
<4 && cnt
>0; ++i
) {
414 data
= (data
>> 8) | (*src
++ << 24);
418 for (; cnt
==0 && i
<4; ++i
, ++cp
) {
419 data
= (data
>> 8) | (*(uchar
*)cp
<< 24);
422 if ((rc
= write_word(info
, wp
, data
)) != 0) {
429 * handle word aligned part
432 data
= *((vu_long
*)src
);
433 if ((rc
= write_word(info
, wp
, data
)) != 0) {
446 * handle unaligned tail bytes
449 for (i
=0, cp
=wp
; i
<4 && cnt
>0; ++i
, ++cp
) {
450 data
= (data
>> 8) | (*src
++ << 24);
453 for (; i
<4; ++i
, ++cp
) {
454 data
= (data
>> 8) | (*(uchar
*)cp
<< 24);
457 return write_word(info
, wp
, data
);