3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 #include <asm/processor.h>
28 #if defined(CFG_ENV_IS_IN_FLASH)
30 # define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
33 # define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
35 # ifndef CFG_ENV_SECT_SIZE
36 # define CFG_ENV_SECT_SIZE CFG_ENV_SIZE
40 #define FLASH_BANK_SIZE 0x800000
41 #define MAIN_SECT_SIZE 0x40000
42 #define PARAM_SECT_SIZE 0x8000
44 #define BOARD_CTRL_REG 0xFE800013
46 flash_info_t flash_info
[CFG_MAX_FLASH_BANKS
];
48 static int write_data (flash_info_t
*info
, ulong dest
, ulong
*data
);
49 static void write_via_fpu(vu_long
*addr
, ulong
*data
);
50 static __inline__
unsigned long get_msr(void);
51 static __inline__
void set_msr(unsigned long msr
);
53 /*---------------------------------------------------------------------*/
56 /*---------------------------------------------------------------------*/
58 #define DEBUGF(fmt,args...) printf(fmt ,##args)
60 #define DEBUGF(fmt,args...)
62 /*---------------------------------------------------------------------*/
64 /*-----------------------------------------------------------------------
67 unsigned long flash_init(void)
71 volatile unsigned char *bcr
= (volatile unsigned char *)(BOARD_CTRL_REG
);
73 DEBUGF("Write protect was: 0x%02X\n", *bcr
);
74 *bcr
&= 0x1; /* FWPT must be 0 */
75 *bcr
|= 0x6; /* FWP0 = FWP1 = 1 */
76 DEBUGF("Write protect is: 0x%02X\n", *bcr
);
78 for (i
= 0; i
< CFG_MAX_FLASH_BANKS
; i
++) {
79 vu_long
*addr
= (vu_long
*)(CFG_FLASH_BASE
+ i
* FLASH_BANK_SIZE
);
83 DEBUGF ("Flash bank # %d:\n"
84 "\tManuf. ID @ 0x%08lX: 0x%08lX\n"
85 "\tDevice ID @ 0x%08lX: 0x%08lX\n",
87 (ulong
)(&addr
[0]), addr
[0],
88 (ulong
)(&addr
[2]), addr
[2]);
90 if ((addr
[0] == addr
[1]) && (addr
[0] == INTEL_MANUFACT
) &&
91 (addr
[2] == addr
[3]) && (addr
[2] == INTEL_ID_28F160F3B
))
93 flash_info
[i
].flash_id
= (FLASH_MAN_INTEL
& FLASH_VENDMASK
) |
94 (INTEL_ID_28F160F3B
& FLASH_TYPEMASK
);
96 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
101 DEBUGF ("flash_id = 0x%08lX\n", flash_info
[i
].flash_id
);
103 addr
[0] = 0xFFFFFFFF;
105 flash_info
[i
].size
= FLASH_BANK_SIZE
;
106 flash_info
[i
].sector_count
= CFG_MAX_FLASH_SECT
;
107 memset(flash_info
[i
].protect
, 0, CFG_MAX_FLASH_SECT
);
108 for (j
= 0; j
< flash_info
[i
].sector_count
; j
++) {
110 flash_info
[i
].start
[j
] = CFG_FLASH_BASE
+
111 i
* FLASH_BANK_SIZE
+
114 flash_info
[i
].start
[j
] = CFG_FLASH_BASE
+
115 i
* FLASH_BANK_SIZE
+
116 (j
- 7)*MAIN_SECT_SIZE
;
119 size
+= flash_info
[i
].size
;
122 /* Protect monitor and environment sectors
124 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
125 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE + FLASH_BANK_SIZE
126 flash_protect(FLAG_PROTECT_SET
,
128 CFG_MONITOR_BASE
+ monitor_flash_len
- 1,
131 flash_protect(FLAG_PROTECT_SET
,
133 CFG_MONITOR_BASE
+ monitor_flash_len
- 1,
138 #if (CFG_ENV_IS_IN_FLASH == 1) && defined(CFG_ENV_ADDR)
139 #if CFG_ENV_ADDR >= CFG_FLASH_BASE + FLASH_BANK_SIZE
140 flash_protect(FLAG_PROTECT_SET
,
142 CFG_ENV_ADDR
+ CFG_ENV_SIZE
- 1,
145 flash_protect(FLAG_PROTECT_SET
,
147 CFG_ENV_ADDR
+ CFG_ENV_SIZE
- 1,
156 /*-----------------------------------------------------------------------
158 void flash_print_info (flash_info_t
* info
)
162 switch ((i
= info
->flash_id
& FLASH_VENDMASK
)) {
163 case (FLASH_MAN_INTEL
& FLASH_VENDMASK
):
167 printf ("Unknown Vendor 0x%04x ", i
);
171 switch ((i
= info
->flash_id
& FLASH_TYPEMASK
)) {
172 case (INTEL_ID_28F160F3B
& FLASH_TYPEMASK
):
173 printf ("28F160F3B (16Mbit)\n");
176 printf ("Unknown Chip Type 0x%04x\n", i
);
181 printf (" Size: %ld MB in %d Sectors\n",
182 info
->size
>> 20, info
->sector_count
);
184 printf (" Sector Start Addresses:");
185 for (i
= 0; i
< info
->sector_count
; i
++) {
189 printf (" %08lX%s", info
->start
[i
],
190 info
->protect
[i
] ? " (RO)" : " ");
198 /*-----------------------------------------------------------------------
201 int flash_erase (flash_info_t
*info
, int s_first
, int s_last
)
203 int flag
, prot
, sect
;
204 ulong start
, now
, last
;
206 DEBUGF ("Erase flash bank %d sect %d ... %d\n",
207 info
- &flash_info
[0], s_first
, s_last
);
209 if ((s_first
< 0) || (s_first
> s_last
)) {
210 if (info
->flash_id
== FLASH_UNKNOWN
) {
211 printf ("- missing\n");
213 printf ("- no sectors to erase\n");
218 if ((info
->flash_id
& FLASH_VENDMASK
) !=
219 (FLASH_MAN_INTEL
& FLASH_VENDMASK
)) {
220 printf ("Can erase only Intel flash types - aborted\n");
225 for (sect
=s_first
; sect
<=s_last
; ++sect
) {
226 if (info
->protect
[sect
]) {
232 printf ("- Warning: %d protected sectors will not be erased!\n",
238 start
= get_timer (0);
240 /* Start erase on unprotected sectors */
241 for (sect
= s_first
; sect
<=s_last
; sect
++) {
242 if (info
->protect
[sect
] == 0) { /* not protected */
243 vu_long
*addr
= (vu_long
*)(info
->start
[sect
]);
245 DEBUGF ("Erase sect %d @ 0x%08lX\n",
248 /* Disable interrupts which might cause a timeout
251 flag
= disable_interrupts();
253 addr
[0] = 0x00500050; /* clear status register */
254 addr
[0] = 0x00200020; /* erase setup */
255 addr
[0] = 0x00D000D0; /* erase confirm */
257 addr
[1] = 0x00500050; /* clear status register */
258 addr
[1] = 0x00200020; /* erase setup */
259 addr
[1] = 0x00D000D0; /* erase confirm */
261 /* re-enable interrupts if necessary */
265 /* wait at least 80us - let's wait 1 ms */
268 while (((addr
[0] & 0x00800080) != 0x00800080) ||
269 ((addr
[1] & 0x00800080) != 0x00800080) ) {
270 if ((now
=get_timer(start
)) >
271 CFG_FLASH_ERASE_TOUT
) {
272 printf ("Timeout\n");
273 addr
[0] = 0x00B000B0; /* suspend erase */
274 addr
[0] = 0x00FF00FF; /* to read mode */
278 /* show that we're waiting */
279 if ((now
- last
) > 1000) { /* every second */
285 addr
[0] = 0x00FF00FF;
292 /*-----------------------------------------------------------------------
293 * Copy memory to flash, returns:
296 * 2 - Flash not erased
297 * 4 - Flash not identified
300 #define FLASH_WIDTH 8 /* flash bus width in bytes */
302 int write_buff (flash_info_t
*info
, uchar
*src
, ulong addr
, ulong cnt
)
307 ulong
*datah
= &data
[0];
308 ulong
*datal
= &data
[1];
310 DEBUGF ("Flash write_buff: @ 0x%08lx, src 0x%08lx len %ld\n",
311 addr
, (ulong
)src
, cnt
);
313 if (info
->flash_id
== FLASH_UNKNOWN
) {
318 set_msr(msr
| MSR_FP
);
320 wp
= (addr
& ~(FLASH_WIDTH
-1)); /* get lower aligned address */
323 * handle unaligned start bytes
325 if ((l
= addr
- wp
) != 0) {
328 for (i
= 0, cp
= wp
; i
< l
; i
++, cp
++) {
330 *datah
= (*datah
<< 8) |
331 ((*datal
& 0xFF000000) >> 24);
334 *datal
= (*datal
<< 8) | (*(uchar
*)cp
);
336 for (; i
< FLASH_WIDTH
&& cnt
> 0; ++i
) {
344 *datah
= (*datah
<< 8) |
345 ((*datal
& 0xFF000000) >> 24);
348 *datal
= (*datal
<< 8) | tmp
;
353 for (; cnt
== 0 && i
< FLASH_WIDTH
; ++i
, ++cp
) {
355 *datah
= (*datah
<< 8) |
356 ((*datal
& 0xFF000000) >> 24);
359 *datal
= (*datah
<< 8) | (*(uchar
*)cp
);
362 if ((rc
= write_data(info
, wp
, data
)) != 0) {
371 * handle FLASH_WIDTH aligned part
373 while (cnt
>= FLASH_WIDTH
) {
374 *datah
= *(ulong
*)src
;
375 *datal
= *(ulong
*)(src
+ 4);
376 if ((rc
= write_data(info
, wp
, data
)) != 0) {
391 * handle unaligned tail bytes
394 for (i
= 0, cp
= wp
; i
< FLASH_WIDTH
&& cnt
> 0; ++i
, ++cp
) {
402 *datah
= (*datah
<< 8) | ((*datal
& 0xFF000000) >> 24);
405 *datal
= (*datal
<< 8) | tmp
;
410 for (; i
< FLASH_WIDTH
; ++i
, ++cp
) {
412 *datah
= (*datah
<< 8) | ((*datal
& 0xFF000000) >> 24);
415 *datal
= (*datal
<< 8) | (*(uchar
*)cp
);
418 rc
= write_data(info
, wp
, data
);
424 /*-----------------------------------------------------------------------
425 * Write a word to Flash, returns:
428 * 2 - Flash not erased
430 static int write_data (flash_info_t
*info
, ulong dest
, ulong
*data
)
432 vu_long
*addr
= (vu_long
*)dest
;
436 /* Check if Flash is (sufficiently) erased */
437 if (((addr
[0] & data
[0]) != data
[0]) ||
438 ((addr
[1] & data
[1]) != data
[1]) ) {
441 /* Disable interrupts which might cause a timeout here */
442 flag
= disable_interrupts();
444 addr
[0] = 0x00400040; /* write setup */
445 write_via_fpu(addr
, data
);
447 /* re-enable interrupts if necessary */
451 start
= get_timer (0);
453 while (((addr
[0] & 0x00800080) != 0x00800080) ||
454 ((addr
[1] & 0x00800080) != 0x00800080) ) {
455 if (get_timer(start
) > CFG_FLASH_WRITE_TOUT
) {
456 addr
[0] = 0x00FF00FF; /* restore read mode */
461 addr
[0] = 0x00FF00FF; /* restore read mode */
466 /*-----------------------------------------------------------------------
468 static void write_via_fpu(vu_long
*addr
, ulong
*data
)
470 __asm__
__volatile__ ("lfd 1, 0(%0)" : : "r" (data
));
471 __asm__
__volatile__ ("stfd 1, 0(%0)" : : "r" (addr
));
473 /*-----------------------------------------------------------------------
475 static __inline__
unsigned long get_msr(void)
479 __asm__
__volatile__ ("mfmsr %0" : "=r" (msr
) :);
483 static __inline__
void set_msr(unsigned long msr
)
485 __asm__
__volatile__ ("mtmsr %0" : : "r" (msr
));