b4b85995f14a402dafcd1266ce77ac5f2239dd1d
3 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
4 * Alex Zuepke <azu@sysgo.de>
7 * Michel Pollet <buserror@gmail.com>
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,
33 #define FLASH_BANK_SIZE PHYS_FLASH_SIZE
34 #define MAIN_SECT_SIZE 0x10000 /* 64 KB */
36 flash_info_t flash_info
[CFG_MAX_FLASH_BANKS
];
39 #define CMD_READ_ARRAY 0x000000F0
40 #define CMD_UNLOCK1 0x000000AA
41 #define CMD_UNLOCK2 0x00000055
42 #define CMD_ERASE_SETUP 0x00000080
43 #define CMD_ERASE_CONFIRM 0x00000030
44 #define CMD_PROGRAM 0x000000A0
45 #define CMD_UNLOCK_BYPASS 0x00000020
47 #define MEM_FLASH_ADDR1 (*(volatile u16 *)(CFG_FLASH_BASE + (0x00000555 << 1)))
48 #define MEM_FLASH_ADDR2 (*(volatile u16 *)(CFG_FLASH_BASE + (0x000002AA << 1)))
50 #define BIT_ERASE_DONE 0x00000080
51 #define BIT_RDY_MASK 0x00000080
52 #define BIT_PROGRAM_ERROR 0x00000020
53 #define BIT_TIMEOUT 0x80000000 /* our flag */
59 /*-----------------------------------------------------------------------
62 ulong
flash_init (void)
67 for (i
= 0; i
< CFG_MAX_FLASH_BANKS
; i
++) {
70 flash_info
[i
].flash_id
=
71 #if defined(CONFIG_AMD_LV400)
72 (AMD_MANUFACT
& FLASH_VENDMASK
) |
73 (AMD_ID_LV400B
& FLASH_TYPEMASK
);
74 #elif defined(CONFIG_AMD_LV800)
75 (AMD_MANUFACT
& FLASH_VENDMASK
) |
76 (AMD_ID_LV800B
& FLASH_TYPEMASK
);
77 #elif defined(CONFIG_SST_VF1601)
78 (SST_MANUFACT
& FLASH_VENDMASK
) |
79 (SST_ID_xF1601
& FLASH_TYPEMASK
);
81 #error "Unknown flash configured"
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
++) {
91 #ifndef CONFIG_SST_VF1601
93 /* 1st one is 16 KB */
95 flash_info
[i
].start
[j
] =
99 /* 2nd and 3rd are both 8 KB */
100 if ((j
== 1) || (j
== 2)) {
101 flash_info
[i
].start
[j
] =
102 flashbase
+ 0x4000 + (j
-
109 flash_info
[i
].start
[j
] =
113 flash_info
[i
].start
[j
] =
114 flashbase
+ (j
- 3) * MAIN_SECT_SIZE
;
117 flash_info
[i
].start
[j
] =
118 flashbase
+ (j
) * MAIN_SECT_SIZE
;
121 size
+= flash_info
[i
].size
;
124 flash_protect (FLAG_PROTECT_SET
,
126 CFG_FLASH_BASE
+ monitor_flash_len
- 1,
129 #if defined(CFG_ENV_ADDR)
130 flash_protect (FLAG_PROTECT_SET
,
132 CFG_ENV_ADDR
+ CFG_ENV_SIZE
- 1, &flash_info
[0]);
137 /*-----------------------------------------------------------------------
139 void flash_print_info (flash_info_t
* info
)
143 switch (info
->flash_id
& FLASH_VENDMASK
) {
144 case (AMD_MANUFACT
& FLASH_VENDMASK
):
147 case (SST_MANUFACT
& FLASH_VENDMASK
):
151 printf ("Unknown Vendor ");
155 switch (info
->flash_id
& FLASH_TYPEMASK
) {
156 case (AMD_ID_LV400B
& FLASH_TYPEMASK
):
157 printf ("1x Amd29LV400BB (4Mbit)\n");
159 case (AMD_ID_LV800B
& FLASH_TYPEMASK
):
160 printf ("1x Amd29LV800BB (8Mbit)\n");
162 case (SST_ID_xF1601
& FLASH_TYPEMASK
):
163 printf ("1x SST39VF1601 (2Mbit)\n");
166 printf ("Unknown Chip Type\n");
171 printf (" Size: %ld MB in %d Sectors\n",
172 info
->size
>> 20, info
->sector_count
);
174 printf (" Sector Start Addresses:");
175 for (i
= 0; i
< info
->sector_count
; i
++) {
179 printf (" %08lX%s", info
->start
[i
],
180 info
->protect
[i
] ? " (RO)" : " ");
187 /*-----------------------------------------------------------------------
190 int flash_erase (flash_info_t
* info
, int s_first
, int s_last
)
193 int iflag
, cflag
, prot
, sect
;
197 /* first look for protection bits */
199 if (info
->flash_id
== FLASH_UNKNOWN
)
200 return ERR_UNKNOWN_FLASH_TYPE
;
202 if ((s_first
< 0) || (s_first
> s_last
)) {
206 #ifdef CONFIG_SST_VF1601
207 if ((info
->flash_id
& FLASH_VENDMASK
) !=
208 (SST_MANUFACT
& FLASH_VENDMASK
)) {
209 return ERR_UNKNOWN_FLASH_VENDOR
;
212 if ((info
->flash_id
& FLASH_VENDMASK
) !=
213 (AMD_MANUFACT
& FLASH_VENDMASK
)) {
214 return ERR_UNKNOWN_FLASH_VENDOR
;
219 for (sect
= s_first
; sect
<= s_last
; ++sect
) {
220 if (info
->protect
[sect
]) {
225 return ERR_PROTECTED
;
228 * Disable interrupts which might cause a timeout
229 * here. Remember that our exception vectors are
230 * at address 0 in the flash, and we don't want a
231 * (ticker) exception to happen while the flash
232 * chip is in programming mode.
234 cflag
= icache_status ();
236 iflag
= disable_interrupts ();
238 /* Start erase on unprotected sectors */
239 for (sect
= s_first
; sect
<= s_last
&& !ctrlc (); sect
++) {
240 printf ("Erasing sector %2d ... ", sect
);
242 /* arm simple, non interrupt dependent timer */
243 reset_timer_masked ();
245 if (info
->protect
[sect
] == 0) { /* not protected */
246 vu_short
*addr
= (vu_short
*) (info
->start
[sect
]);
248 MEM_FLASH_ADDR1
= CMD_UNLOCK1
;
249 MEM_FLASH_ADDR2
= CMD_UNLOCK2
;
250 MEM_FLASH_ADDR1
= CMD_ERASE_SETUP
;
252 MEM_FLASH_ADDR1
= CMD_UNLOCK1
;
253 MEM_FLASH_ADDR2
= CMD_UNLOCK2
;
254 *addr
= CMD_ERASE_CONFIRM
;
256 #ifndef CONFIG_SST_VF1601
257 /* wait until flash is ready */
264 if (get_timer_masked () >
265 CFG_FLASH_ERASE_TOUT
) {
266 MEM_FLASH_ADDR1
= CMD_READ_ARRAY
;
272 && (result
& 0xFFFF) & BIT_ERASE_DONE
)
276 && (result
& 0xFFFF) & BIT_PROGRAM_ERROR
)
281 MEM_FLASH_ADDR1
= CMD_READ_ARRAY
;
292 /* wait until flash is ready */
295 i
= *((volatile unsigned short *)addr
)&0x40;
296 if(i
!=*((volatile unsigned short *)addr
)&0x40)
298 if(*((volatile unsigned short *)addr
)&0x80)
303 } else { /* it was protected */
305 printf ("protected!\n");
310 printf ("User Interrupt!\n");
313 /* allow flash to settle - wait 10 ms */
314 udelay_masked (10000);
317 enable_interrupts ();
325 /*-----------------------------------------------------------------------
326 * Copy memory to flash
329 volatile static int write_hword (flash_info_t
* info
, ulong dest
, ushort data
)
331 vu_short
*addr
= (vu_short
*) dest
;
338 * Check if Flash is (sufficiently) erased
341 if ((result
& data
) != data
)
342 return ERR_NOT_ERASED
;
346 * Disable interrupts which might cause a timeout
347 * here. Remember that our exception vectors are
348 * at address 0 in the flash, and we don't want a
349 * (ticker) exception to happen while the flash
350 * chip is in programming mode.
352 cflag
= icache_status ();
354 iflag
= disable_interrupts ();
356 MEM_FLASH_ADDR1
= CMD_UNLOCK1
;
357 MEM_FLASH_ADDR2
= CMD_UNLOCK2
;
358 MEM_FLASH_ADDR1
= CMD_UNLOCK_BYPASS
;
362 /* arm simple, non interrupt dependent timer */
363 reset_timer_masked ();
365 /* wait until flash is ready */
371 if (get_timer_masked () > CFG_FLASH_ERASE_TOUT
) {
375 if (!chip
&& ((result
& 0x80) == (data
& 0x80)))
378 if (!chip
&& ((result
& 0xFFFF) & BIT_PROGRAM_ERROR
)) {
381 if ((result
& 0x80) == (data
& 0x80))
389 *addr
= CMD_READ_ARRAY
;
391 if (chip
== ERR
|| *addr
!= data
)
395 enable_interrupts ();
403 /*-----------------------------------------------------------------------
404 * Copy memory to flash.
407 int write_buff (flash_info_t
* info
, uchar
* src
, ulong addr
, ulong cnt
)
414 wp
= (addr
& ~1); /* get lower word aligned address */
417 * handle unaligned start bytes
419 if ((l
= addr
- wp
) != 0) {
421 for (i
= 0, cp
= wp
; i
< l
; ++i
, ++cp
) {
422 data
= (data
>> 8) | (*(uchar
*) cp
<< 8);
424 for (; i
< 2 && cnt
> 0; ++i
) {
425 data
= (data
>> 8) | (*src
++ << 8);
429 for (; cnt
== 0 && i
< 2; ++i
, ++cp
) {
430 data
= (data
>> 8) | (*(uchar
*) cp
<< 8);
433 if ((rc
= write_hword (info
, wp
, data
)) != 0) {
440 * handle word aligned part
443 data
= *((vu_short
*) src
);
444 if ((rc
= write_hword (info
, wp
, data
)) != 0) {
457 * handle unaligned tail bytes
460 for (i
= 0, cp
= wp
; i
< 2 && cnt
> 0; ++i
, ++cp
) {
461 data
= (data
>> 8) | (*src
++ << 8);
464 for (; i
< 2; ++i
, ++cp
) {
465 data
= (data
>> 8) | (*(uchar
*) cp
<< 8);
468 return write_hword (info
, wp
, data
);