2 * (C) Copyright 2001 - 2003
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(CONFIG_ENV_IS_IN_FLASH)
29 # ifndef CONFIG_ENV_ADDR
30 # define CONFIG_ENV_ADDR (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
32 # ifndef CONFIG_ENV_SIZE
33 # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
35 # ifndef CONFIG_ENV_SECT_SIZE
36 # define CONFIG_ENV_SECT_SIZE CONFIG_ENV_SIZE
40 #define FLASH_BANK_SIZE 0x800000
41 #define MAIN_SECT_SIZE 0x40000
42 #define PARAM_SECT1_SIZE 0x20000
43 #define PARAM_SECT23_SIZE 0x8000
44 #define PARAM_SECT4_SIZE 0x10000
46 flash_info_t flash_info
[CONFIG_SYS_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 #define __align__ __attribute__ ((aligned (8)))
65 static __align__ ulong precmd0
[2] = { 0x00aa00aa, 0x00aa00aa };
66 static __align__ ulong precmd1
[2] = { 0x00550055, 0x00550055 };
67 static __align__ ulong cmdid
[2] = { 0x00900090, 0x00900090 };
68 static __align__ ulong cmderase
[2] = { 0x00800080, 0x00800080 };
69 static __align__ ulong cmdersusp
[2] = { 0x00b000b0, 0x00b000b0 };
70 static __align__ ulong cmdsecter
[2] = { 0x00300030, 0x00300030 };
71 static __align__ ulong cmdprog
[2] = { 0x00a000a0, 0x00a000a0 };
72 static __align__ ulong cmdres
[2] = { 0x00f000f0, 0x00f000f0 };
74 /*-----------------------------------------------------------------------
77 unsigned long flash_init (void)
82 for (i
= 0; i
< CONFIG_SYS_MAX_FLASH_BANKS
; i
++) {
83 vu_long
*addr
= (vu_long
*) (CONFIG_SYS_FLASH_BASE
+ i
* FLASH_BANK_SIZE
);
85 write_via_fpu (&addr
[0xaaa], precmd0
);
86 write_via_fpu (&addr
[0x554], precmd1
);
87 write_via_fpu (&addr
[0xaaa], cmdid
);
89 DEBUGF ("Flash bank # %d:\n"
90 "\tManuf. ID @ 0x%08lX: 0x%08lX\n"
91 "\tDevice ID @ 0x%08lX: 0x%08lX\n",
93 (ulong
) (&addr
[0]), addr
[0],
94 (ulong
) (&addr
[2]), addr
[2]);
96 if ((addr
[0] == addr
[1]) && (addr
[0] == AMD_MANUFACT
) &&
97 (addr
[2] == addr
[3]) && (addr
[2] == AMD_ID_LV160T
)) {
98 flash_info
[i
].flash_id
= (FLASH_MAN_AMD
& FLASH_VENDMASK
) |
99 (FLASH_AM160T
& FLASH_TYPEMASK
);
101 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
102 write_via_fpu (addr
, cmdres
);
106 DEBUGF ("flash_id = 0x%08lX\n", flash_info
[i
].flash_id
);
108 write_via_fpu (addr
, cmdres
);
110 flash_info
[i
].size
= FLASH_BANK_SIZE
;
111 flash_info
[i
].sector_count
= CONFIG_SYS_MAX_FLASH_SECT
;
112 memset (flash_info
[i
].protect
, 0, CONFIG_SYS_MAX_FLASH_SECT
);
113 for (j
= 0; j
< 32; j
++) {
114 flash_info
[i
].start
[j
] = CONFIG_SYS_FLASH_BASE
+
115 i
* FLASH_BANK_SIZE
+ j
* MAIN_SECT_SIZE
;
117 flash_info
[i
].start
[32] =
118 flash_info
[i
].start
[31] + PARAM_SECT1_SIZE
;
119 flash_info
[i
].start
[33] =
120 flash_info
[i
].start
[32] + PARAM_SECT23_SIZE
;
121 flash_info
[i
].start
[34] =
122 flash_info
[i
].start
[33] + PARAM_SECT23_SIZE
;
123 size
+= flash_info
[i
].size
;
126 /* Protect monitor and environment sectors
128 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
129 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE + FLASH_BANK_SIZE
130 flash_protect ( FLAG_PROTECT_SET
,
131 CONFIG_SYS_MONITOR_BASE
,
132 CONFIG_SYS_MONITOR_BASE
+ monitor_flash_len
- 1,
135 flash_protect ( FLAG_PROTECT_SET
,
136 CONFIG_SYS_MONITOR_BASE
,
137 CONFIG_SYS_MONITOR_BASE
+ monitor_flash_len
- 1,
142 #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CONFIG_ENV_ADDR)
143 #if CONFIG_ENV_ADDR >= CONFIG_SYS_FLASH_BASE + FLASH_BANK_SIZE
144 flash_protect ( FLAG_PROTECT_SET
,
146 CONFIG_ENV_ADDR
+ CONFIG_ENV_SIZE
- 1, &flash_info
[1]);
148 flash_protect ( FLAG_PROTECT_SET
,
150 CONFIG_ENV_ADDR
+ CONFIG_ENV_SIZE
- 1, &flash_info
[0]);
158 /*-----------------------------------------------------------------------
160 void flash_print_info (flash_info_t
* info
)
164 switch ((i
= info
->flash_id
& FLASH_VENDMASK
)) {
165 case (FLASH_MAN_AMD
& FLASH_VENDMASK
):
169 printf ("Unknown Vendor 0x%04x ", i
);
173 switch ((i
= info
->flash_id
& FLASH_TYPEMASK
)) {
174 case (FLASH_AM160T
& FLASH_TYPEMASK
):
175 printf ("AM29LV160BT (16Mbit)\n");
178 printf ("Unknown Chip Type 0x%04x\n", i
);
183 printf (" Size: %ld MB in %d Sectors\n",
184 info
->size
>> 20, info
->sector_count
);
186 printf (" Sector Start Addresses:");
187 for (i
= 0; i
< info
->sector_count
; i
++) {
191 printf (" %08lX%s", info
->start
[i
],
192 info
->protect
[i
] ? " (RO)" : " ");
200 /*-----------------------------------------------------------------------
203 int flash_erase (flash_info_t
* info
, int s_first
, int s_last
)
205 int flag
, prot
, sect
;
206 ulong start
, now
, last
;
208 DEBUGF ("Erase flash bank %d sect %d ... %d\n",
209 info
- &flash_info
[0], s_first
, s_last
);
211 if ((s_first
< 0) || (s_first
> s_last
)) {
212 if (info
->flash_id
== FLASH_UNKNOWN
) {
213 printf ("- missing\n");
215 printf ("- no sectors to erase\n");
220 if ((info
->flash_id
& FLASH_VENDMASK
) !=
221 (FLASH_MAN_AMD
& FLASH_VENDMASK
)) {
222 printf ("Can erase only AMD flash types - aborted\n");
227 for (sect
= s_first
; sect
<= s_last
; ++sect
) {
228 if (info
->protect
[sect
]) {
234 printf ("- Warning: %d protected sectors will not be erased!\n",
240 start
= get_timer (0);
242 /* Start erase on unprotected sectors */
243 for (sect
= s_first
; sect
<= s_last
; sect
++) {
244 if (info
->protect
[sect
] == 0) { /* not protected */
245 vu_long
*addr
= (vu_long
*) (info
->start
[sect
]);
247 DEBUGF ("Erase sect %d @ 0x%08lX\n", sect
, (ulong
) addr
);
249 /* Disable interrupts which might cause a timeout
252 flag
= disable_interrupts ();
254 write_via_fpu (&addr
[0xaaa], precmd0
);
255 write_via_fpu (&addr
[0x554], precmd1
);
256 write_via_fpu (&addr
[0xaaa], cmderase
);
257 write_via_fpu (&addr
[0xaaa], precmd0
);
258 write_via_fpu (&addr
[0x554], precmd1
);
259 write_via_fpu (&addr
[0xaaa], cmdsecter
);
261 /* re-enable interrupts if necessary */
263 enable_interrupts ();
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
)) > CONFIG_SYS_FLASH_ERASE_TOUT
) {
271 printf ("Timeout\n");
272 write_via_fpu (addr
, cmdersusp
);
273 write_via_fpu (addr
, cmdres
);
277 /* show that we're waiting */
278 if ((now
- last
) > 1000) { /* every second */
284 write_via_fpu (addr
, cmdres
);
291 /*-----------------------------------------------------------------------
292 * Copy memory to flash, returns:
295 * 2 - Flash not erased
296 * 4 - Flash not identified
299 #define FLASH_WIDTH 8 /* flash bus width in bytes */
301 int write_buff (flash_info_t
* info
, uchar
* src
, ulong addr
, ulong cnt
)
306 ulong
*datah
= &data
[0];
307 ulong
*datal
= &data
[1];
309 DEBUGF ("Flash write_buff: @ 0x%08lx, src 0x%08lx len %ld\n",
310 addr
, (ulong
) src
, cnt
);
312 if (info
->flash_id
== FLASH_UNKNOWN
) {
317 set_msr (msr
| MSR_FP
);
319 wp
= (addr
& ~(FLASH_WIDTH
- 1)); /* get lower aligned address */
322 * handle unaligned start bytes
324 if ((l
= addr
- wp
) != 0) {
327 for (i
= 0, cp
= wp
; i
< l
; i
++, cp
++) {
329 *datah
= (*datah
<< 8) | ((*datal
& 0xFF000000) >> 24);
332 *datal
= (*datal
<< 8) | (*(uchar
*) cp
);
334 for (; i
< FLASH_WIDTH
&& cnt
> 0; ++i
) {
342 *datah
= (*datah
<< 8) | ((*datal
& 0xFF000000) >> 24);
345 *datal
= (*datal
<< 8) | tmp
;
351 for (; cnt
== 0 && i
< FLASH_WIDTH
; ++i
, ++cp
) {
353 *datah
= (*datah
<< 8) | ((*datal
& 0xFF000000) >> 24);
356 *datal
= (*datah
<< 8) | (*(uchar
*) cp
);
359 if ((rc
= write_data (info
, wp
, data
)) != 0) {
368 * handle FLASH_WIDTH aligned part
370 while (cnt
>= FLASH_WIDTH
) {
371 *datah
= *(ulong
*) src
;
372 *datal
= *(ulong
*) (src
+ 4);
373 if ((rc
= write_data (info
, wp
, data
)) != 0) {
388 * handle unaligned tail bytes
391 for (i
= 0, cp
= wp
; i
< FLASH_WIDTH
&& cnt
> 0; ++i
, ++cp
) {
399 *datah
= (*datah
<< 8) | ((*datal
& 0xFF000000) >> 24);
402 *datal
= (*datal
<< 8) | tmp
;
407 for (; i
< FLASH_WIDTH
; ++i
, ++cp
) {
409 *datah
= (*datah
<< 8) | ((*datal
& 0xFF000000) >> 24);
412 *datal
= (*datal
<< 8) | (*(uchar
*) cp
);
415 rc
= write_data (info
, wp
, data
);
421 /*-----------------------------------------------------------------------
422 * Write a word to Flash, returns:
425 * 2 - Flash not erased
427 static int write_data (flash_info_t
* info
, ulong dest
, ulong
* data
)
429 vu_long
*chip
= (vu_long
*) (info
->start
[0]);
430 vu_long
*addr
= (vu_long
*) dest
;
434 /* Check if Flash is (sufficiently) erased */
435 if (((addr
[0] & data
[0]) != data
[0]) ||
436 ((addr
[1] & data
[1]) != data
[1])) {
439 /* Disable interrupts which might cause a timeout here */
440 flag
= disable_interrupts ();
442 write_via_fpu (&chip
[0xaaa], precmd0
);
443 write_via_fpu (&chip
[0x554], precmd1
);
444 write_via_fpu (&chip
[0xaaa], cmdprog
);
445 write_via_fpu (addr
, data
);
447 /* re-enable interrupts if necessary */
449 enable_interrupts ();
451 start
= get_timer (0);
453 while (((addr
[0] & 0x00800080) != (data
[0] & 0x00800080)) ||
454 ((addr
[1] & 0x00800080) != (data
[1] & 0x00800080))) {
455 if (get_timer (start
) > CONFIG_SYS_FLASH_WRITE_TOUT
) {
456 write_via_fpu (chip
, cmdres
);
461 write_via_fpu (chip
, cmdres
);
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
));
474 /*-----------------------------------------------------------------------
476 static __inline__
unsigned long get_msr (void)
480 __asm__
__volatile__ ("mfmsr %0":"=r" (msr
):);
485 static __inline__
void set_msr (unsigned long msr
)
487 __asm__
__volatile__ ("mtmsr %0"::"r" (msr
));