3 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
4 * Alex Zuepke <azu@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 PHYS_FLASH_SIZE
31 #define MAIN_SECT_SIZE 0x10000 /* 64 KB */
33 flash_info_t flash_info
[CFG_MAX_FLASH_BANKS
];
36 #define CMD_READ_ARRAY 0x000000F0
37 #define CMD_UNLOCK1 0x000000AA
38 #define CMD_UNLOCK2 0x00000055
39 #define CMD_ERASE_SETUP 0x00000080
40 #define CMD_ERASE_CONFIRM 0x00000030
41 #define CMD_PROGRAM 0x000000A0
42 #define CMD_UNLOCK_BYPASS 0x00000020
44 #define MEM_FLASH_ADDR1 (*(volatile u16 *)(CFG_FLASH_BASE + (0x00000555 << 1)))
45 #define MEM_FLASH_ADDR2 (*(volatile u16 *)(CFG_FLASH_BASE + (0x000002AA << 1)))
47 #define BIT_ERASE_DONE 0x00000080
48 #define BIT_RDY_MASK 0x00000080
49 #define BIT_PROGRAM_ERROR 0x00000020
50 #define BIT_TIMEOUT 0x80000000 /* our flag */
56 /*-----------------------------------------------------------------------
59 ulong
flash_init (void)
64 for (i
= 0; i
< CFG_MAX_FLASH_BANKS
; i
++) {
67 flash_info
[i
].flash_id
=
68 #if defined(CONFIG_AMD_LV400)
69 (AMD_MANUFACT
& FLASH_VENDMASK
) |
70 (AMD_ID_LV400B
& FLASH_TYPEMASK
);
71 #elif defined(CONFIG_AMD_LV800)
72 (AMD_MANUFACT
& FLASH_VENDMASK
) |
73 (AMD_ID_LV800B
& FLASH_TYPEMASK
);
74 #elif defined(CONFIG_SST_VF1601)
75 (SST_MANUFACT
& FLASH_VENDMASK
) |
76 (SST_ID_xF1601
& FLASH_TYPEMASK
);
78 #error "Unknown flash configured"
80 flash_info
[i
].size
= FLASH_BANK_SIZE
;
81 flash_info
[i
].sector_count
= CFG_MAX_FLASH_SECT
;
82 memset (flash_info
[i
].protect
, 0, CFG_MAX_FLASH_SECT
);
84 flashbase
= PHYS_FLASH_1
;
86 panic ("configured too many flash banks!\n");
87 for (j
= 0; j
< flash_info
[i
].sector_count
; j
++) {
88 #ifndef CONFIG_SST_VF1601
90 /* 1st one is 16 KB */
92 flash_info
[i
].start
[j
] =
96 /* 2nd and 3rd are both 8 KB */
97 if ((j
== 1) || (j
== 2)) {
98 flash_info
[i
].start
[j
] =
99 flashbase
+ 0x4000 + (j
-
106 flash_info
[i
].start
[j
] =
110 flash_info
[i
].start
[j
] =
111 flashbase
+ (j
- 3) * MAIN_SECT_SIZE
;
114 flash_info
[i
].start
[j
] =
115 flashbase
+ (j
) * MAIN_SECT_SIZE
;
118 size
+= flash_info
[i
].size
;
121 flash_protect (FLAG_PROTECT_SET
,
123 CFG_FLASH_BASE
+ monitor_flash_len
- 1,
126 #if defined(CFG_ENV_ADDR)
127 flash_protect (FLAG_PROTECT_SET
,
129 CFG_ENV_ADDR
+ CFG_ENV_SIZE
- 1, &flash_info
[0]);
134 /*-----------------------------------------------------------------------
136 void flash_print_info (flash_info_t
* info
)
140 switch (info
->flash_id
& FLASH_VENDMASK
) {
141 case (AMD_MANUFACT
& FLASH_VENDMASK
):
144 case (SST_MANUFACT
& FLASH_VENDMASK
):
148 printf ("Unknown Vendor ");
152 switch (info
->flash_id
& FLASH_TYPEMASK
) {
153 case (AMD_ID_LV400B
& FLASH_TYPEMASK
):
154 printf ("1x Amd29LV400BB (4Mbit)\n");
156 case (AMD_ID_LV800B
& FLASH_TYPEMASK
):
157 printf ("1x Amd29LV800BB (8Mbit)\n");
159 case (SST_ID_xF1601
& FLASH_TYPEMASK
):
160 printf ("1x SST39VF1601 (2Mbit)\n");
163 printf ("Unknown Chip Type\n");
168 printf (" Size: %ld MB in %d Sectors\n",
169 info
->size
>> 20, info
->sector_count
);
171 printf (" Sector Start Addresses:");
172 for (i
= 0; i
< info
->sector_count
; i
++) {
176 printf (" %08lX%s", info
->start
[i
],
177 info
->protect
[i
] ? " (RO)" : " ");
184 /*-----------------------------------------------------------------------
187 int flash_erase (flash_info_t
* info
, int s_first
, int s_last
)
190 int iflag
, cflag
, prot
, sect
;
194 /* first look for protection bits */
196 if (info
->flash_id
== FLASH_UNKNOWN
)
197 return ERR_UNKNOWN_FLASH_TYPE
;
199 if ((s_first
< 0) || (s_first
> s_last
)) {
203 #ifdef CONFIG_SST_VF1601
204 if ((info
->flash_id
& FLASH_VENDMASK
) !=
205 (SST_MANUFACT
& FLASH_VENDMASK
)) {
206 return ERR_UNKNOWN_FLASH_VENDOR
;
209 if ((info
->flash_id
& FLASH_VENDMASK
) !=
210 (AMD_MANUFACT
& FLASH_VENDMASK
)) {
211 return ERR_UNKNOWN_FLASH_VENDOR
;
216 for (sect
= s_first
; sect
<= s_last
; ++sect
) {
217 if (info
->protect
[sect
]) {
222 return ERR_PROTECTED
;
225 * Disable interrupts which might cause a timeout
226 * here. Remember that our exception vectors are
227 * at address 0 in the flash, and we don't want a
228 * (ticker) exception to happen while the flash
229 * chip is in programming mode.
231 cflag
= icache_status ();
233 iflag
= disable_interrupts ();
235 /* Start erase on unprotected sectors */
236 for (sect
= s_first
; sect
<= s_last
&& !ctrlc (); sect
++) {
237 printf ("Erasing sector %2d ... ", sect
);
239 /* arm simple, non interrupt dependent timer */
240 reset_timer_masked ();
242 if (info
->protect
[sect
] == 0) { /* not protected */
243 vu_short
*addr
= (vu_short
*) (info
->start
[sect
]);
245 MEM_FLASH_ADDR1
= CMD_UNLOCK1
;
246 MEM_FLASH_ADDR2
= CMD_UNLOCK2
;
247 MEM_FLASH_ADDR1
= CMD_ERASE_SETUP
;
249 MEM_FLASH_ADDR1
= CMD_UNLOCK1
;
250 MEM_FLASH_ADDR2
= CMD_UNLOCK2
;
251 *addr
= CMD_ERASE_CONFIRM
;
253 #ifndef CONFIG_SST_VF1601
254 /* wait until flash is ready */
261 if (get_timer_masked () >
262 CFG_FLASH_ERASE_TOUT
) {
263 MEM_FLASH_ADDR1
= CMD_READ_ARRAY
;
269 && (result
& 0xFFFF) & BIT_ERASE_DONE
)
273 && (result
& 0xFFFF) & BIT_PROGRAM_ERROR
)
278 MEM_FLASH_ADDR1
= CMD_READ_ARRAY
;
289 /* wait until flash is ready */
292 i
= *((volatile unsigned short *)addr
)&0x40;
293 if(i
!=*((volatile unsigned short *)addr
)&0x40)
295 if(*((volatile unsigned short *)addr
)&0x80)
300 } else { /* it was protected */
302 printf ("protected!\n");
307 printf ("User Interrupt!\n");
310 /* allow flash to settle - wait 10 ms */
311 udelay_masked (10000);
314 enable_interrupts ();
322 /*-----------------------------------------------------------------------
323 * Copy memory to flash
326 volatile static int write_hword (flash_info_t
* info
, ulong dest
, ushort data
)
328 vu_short
*addr
= (vu_short
*) dest
;
335 * Check if Flash is (sufficiently) erased
338 if ((result
& data
) != data
)
339 return ERR_NOT_ERASED
;
343 * Disable interrupts which might cause a timeout
344 * here. Remember that our exception vectors are
345 * at address 0 in the flash, and we don't want a
346 * (ticker) exception to happen while the flash
347 * chip is in programming mode.
349 cflag
= icache_status ();
351 iflag
= disable_interrupts ();
353 MEM_FLASH_ADDR1
= CMD_UNLOCK1
;
354 MEM_FLASH_ADDR2
= CMD_UNLOCK2
;
355 MEM_FLASH_ADDR1
= CMD_UNLOCK_BYPASS
;
359 /* arm simple, non interrupt dependent timer */
360 reset_timer_masked ();
362 /* wait until flash is ready */
368 if (get_timer_masked () > CFG_FLASH_ERASE_TOUT
) {
372 if (!chip
&& ((result
& 0x80) == (data
& 0x80)))
375 if (!chip
&& ((result
& 0xFFFF) & BIT_PROGRAM_ERROR
)) {
378 if ((result
& 0x80) == (data
& 0x80))
386 *addr
= CMD_READ_ARRAY
;
388 if (chip
== ERR
|| *addr
!= data
)
392 enable_interrupts ();
400 /*-----------------------------------------------------------------------
401 * Copy memory to flash.
404 int write_buff (flash_info_t
* info
, uchar
* src
, ulong addr
, ulong cnt
)
411 wp
= (addr
& ~1); /* get lower word aligned address */
414 * handle unaligned start bytes
416 if ((l
= addr
- wp
) != 0) {
418 for (i
= 0, cp
= wp
; i
< l
; ++i
, ++cp
) {
419 data
= (data
>> 8) | (*(uchar
*) cp
<< 8);
421 for (; i
< 2 && cnt
> 0; ++i
) {
422 data
= (data
>> 8) | (*src
++ << 8);
426 for (; cnt
== 0 && i
< 2; ++i
, ++cp
) {
427 data
= (data
>> 8) | (*(uchar
*) cp
<< 8);
430 if ((rc
= write_hword (info
, wp
, data
)) != 0) {
437 * handle word aligned part
440 data
= *((vu_short
*) src
);
441 if ((rc
= write_hword (info
, wp
, data
)) != 0) {
454 * handle unaligned tail bytes
457 for (i
= 0, cp
= wp
; i
< 2 && cnt
> 0; ++i
, ++cp
) {
458 data
= (data
>> 8) | (*src
++ << 8);
461 for (; i
< 2; ++i
, ++cp
) {
462 data
= (data
>> 8) | (*(uchar
*) cp
<< 8);
465 return write_hword (info
, wp
, data
);