2 * (C) Copyright 2002-2005
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * (C) Copyright 2002 Jun Gu <jung@artesyncp.com>
6 * Add support for Am29F016D and dynamic switch setting.
8 * See file CREDITS for list of people who contributed to this
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 * Ported from Ebony flash support
29 * Sandburst Corporation
33 #include <asm/processor.h>
38 #define DEBUGF(x...) printf(x)
44 flash_info_t flash_info
[CFG_MAX_FLASH_BANKS
]; /* info for FLASH chips */
46 static unsigned long flash_addr_table
[8][CFG_MAX_FLASH_BANKS
] = {
47 {0xfff80000} /* Boot Flash */
50 /*-----------------------------------------------------------------------
53 static ulong
flash_get_size (vu_long
*addr
, flash_info_t
*info
);
54 static int write_word (flash_info_t
*info
, ulong dest
, ulong data
);
59 #define FLASH_WORD_SIZE unsigned char
62 /*-----------------------------------------------------------------------
65 unsigned long flash_init (void)
67 unsigned long total_b
= 0;
68 unsigned long size_b
[CFG_MAX_FLASH_BANKS
];
69 unsigned short index
= 0;
74 DEBUGF("FLASH: Index: %d\n", index
);
76 /* Init: no FLASHes known */
77 for (i
=0; i
<CFG_MAX_FLASH_BANKS
; ++i
) {
78 flash_info
[i
].flash_id
= FLASH_UNKNOWN
;
79 flash_info
[i
].sector_count
= -1;
80 flash_info
[i
].size
= 0;
82 /* check whether the address is 0 */
83 if (flash_addr_table
[index
][i
] == 0) {
87 /* call flash_get_size() to initialize sector address */
88 size_b
[i
] = flash_get_size(
89 (vu_long
*)flash_addr_table
[index
][i
], &flash_info
[i
]);
90 flash_info
[i
].size
= size_b
[i
];
91 if (flash_info
[i
].flash_id
== FLASH_UNKNOWN
) {
92 printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx = %ld MB\n",
93 i
, size_b
[i
], size_b
[i
]<<20);
94 flash_info
[i
].sector_count
= -1;
95 flash_info
[i
].size
= 0;
98 total_b
+= flash_info
[i
].size
;
105 /*-----------------------------------------------------------------------
107 void flash_print_info (flash_info_t
*info
)
113 volatile unsigned long *flash
;
115 if (info
->flash_id
== FLASH_UNKNOWN
) {
116 printf ("missing or unknown FLASH type\n");
120 switch (info
->flash_id
& FLASH_VENDMASK
) {
121 case FLASH_MAN_AMD
: printf ("AMD "); break;
122 default: printf ("Unknown Vendor "); break;
125 switch (info
->flash_id
& FLASH_TYPEMASK
) {
126 case FLASH_AM040
: printf ("AM29F040 (512 Kbit, uniform sector size)\n");
128 default: printf ("Unknown Chip Type\n");
132 printf (" Size: %ld KB in %d Sectors\n",
133 info
->size
>> 10, info
->sector_count
);
135 printf (" Sector Start Addresses:");
136 for (i
=0; i
<info
->sector_count
; ++i
) {
138 * Check if whole sector is erased
140 if (i
!= (info
->sector_count
-1))
141 size
= info
->start
[i
+1] - info
->start
[i
];
143 size
= info
->start
[0] + info
->size
- info
->start
[i
];
145 flash
= (volatile unsigned long *)info
->start
[i
];
146 size
= size
>> 2; /* divide by 4 for longword access */
147 for (k
=0; k
<size
; k
++)
149 if (*flash
++ != 0xffffffff)
158 printf (" %08lX%s%s",
161 info
->protect
[i
] ? "RO " : " "
168 /*-----------------------------------------------------------------------
172 /*-----------------------------------------------------------------------
176 * The following code cannot be run from FLASH!
178 static ulong
flash_get_size (vu_long
*addr
, flash_info_t
*info
)
181 FLASH_WORD_SIZE value
;
182 ulong base
= (ulong
)addr
;
183 volatile FLASH_WORD_SIZE
*addr2
= (FLASH_WORD_SIZE
*)addr
;
185 DEBUGF("FLASH ADDR: %08x\n", (unsigned)addr
);
187 /* Write auto select command: read Manufacturer ID */
189 addr2
[ADDR0
] = (FLASH_WORD_SIZE
)0x00AA00AA;
191 addr2
[ADDR1
] = (FLASH_WORD_SIZE
)0x00550055;
193 addr2
[ADDR0
] = (FLASH_WORD_SIZE
)0x00900090;
198 DEBUGF("FLASH MANUFACT: %x\n", value
);
201 case (FLASH_WORD_SIZE
)AMD_MANUFACT
:
202 info
->flash_id
= FLASH_MAN_AMD
;
205 info
->flash_id
= FLASH_UNKNOWN
;
206 info
->sector_count
= 0;
208 return (0); /* no or unknown flash */
211 value
= addr2
[1]; /* device ID */
213 DEBUGF("\nFLASH DEVICEID: %x\n", value
);
216 case (FLASH_WORD_SIZE
)AMD_ID_LV040B
:
217 info
->flash_id
+= FLASH_AM040
;
218 info
->sector_count
= 8;
219 info
->size
= 0x00080000; /* => 512 kb */
223 info
->flash_id
= FLASH_UNKNOWN
;
224 return (0); /* => no or unknown flash */
228 /* set up sector start address table */
229 if (info
->flash_id
== FLASH_AM040
) {
230 for (i
= 0; i
< info
->sector_count
; i
++)
231 info
->start
[i
] = base
+ (i
* 0x00010000);
233 if (info
->flash_id
& FLASH_BTYPE
) {
234 /* set sector offsets for bottom boot block type */
235 info
->start
[0] = base
+ 0x00000000;
236 info
->start
[1] = base
+ 0x00004000;
237 info
->start
[2] = base
+ 0x00006000;
238 info
->start
[3] = base
+ 0x00008000;
239 for (i
= 4; i
< info
->sector_count
; i
++) {
240 info
->start
[i
] = base
+ (i
* 0x00010000) - 0x00030000;
243 /* set sector offsets for top boot block type */
244 i
= info
->sector_count
- 1;
245 info
->start
[i
--] = base
+ info
->size
- 0x00004000;
246 info
->start
[i
--] = base
+ info
->size
- 0x00006000;
247 info
->start
[i
--] = base
+ info
->size
- 0x00008000;
248 for (; i
>= 0; i
--) {
249 info
->start
[i
] = base
+ i
* 0x00010000;
254 /* check for protected sectors */
255 for (i
= 0; i
< info
->sector_count
; i
++) {
256 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
257 /* D0 = 1 if protected */
258 addr2
= (volatile FLASH_WORD_SIZE
*)(info
->start
[i
]);
259 if ((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_SST
)
260 info
->protect
[i
] = 0;
262 info
->protect
[i
] = addr2
[2] & 1;
265 /* reset to return to reading data */
266 addr2
[0] = (FLASH_WORD_SIZE
) 0x00F000F0; /* reset bank */
269 * Prevent writes to uninitialized FLASH.
271 if (info
->flash_id
!= FLASH_UNKNOWN
) {
272 addr2
= (FLASH_WORD_SIZE
*)info
->start
[0];
273 *addr2
= (FLASH_WORD_SIZE
)0x00F000F0; /* reset bank */
279 int wait_for_DQ7(flash_info_t
*info
, int sect
)
281 ulong start
, now
, last
;
282 volatile FLASH_WORD_SIZE
*addr
= (FLASH_WORD_SIZE
*)(info
->start
[sect
]);
284 start
= get_timer (0);
286 while ((addr
[0] & (FLASH_WORD_SIZE
)0x00800080) != (FLASH_WORD_SIZE
)0x00800080) {
287 if ((now
= get_timer(start
)) > CFG_FLASH_ERASE_TOUT
) {
288 printf ("Timeout\n");
291 /* show that we're waiting */
292 if ((now
- last
) > 1000) { /* every second */
300 /*-----------------------------------------------------------------------
303 int flash_erase (flash_info_t
*info
, int s_first
, int s_last
)
305 volatile FLASH_WORD_SIZE
*addr
= (FLASH_WORD_SIZE
*)(info
->start
[0]);
306 volatile FLASH_WORD_SIZE
*addr2
;
307 int flag
, prot
, sect
, l_sect
;
310 if ((s_first
< 0) || (s_first
> s_last
)) {
311 if (info
->flash_id
== FLASH_UNKNOWN
) {
312 printf ("- missing\n");
314 printf ("- no sectors to erase\n");
319 if (info
->flash_id
== FLASH_UNKNOWN
) {
320 printf ("Can't erase unknown flash type - aborted\n");
325 for (sect
=s_first
; sect
<=s_last
; ++sect
) {
326 if (info
->protect
[sect
]) {
332 printf ("- Warning: %d protected sectors will not be erased!\n",
340 /* Disable interrupts which might cause a timeout here */
341 flag
= disable_interrupts();
343 /* Start erase on unprotected sectors */
344 for (sect
= s_first
; sect
<=s_last
; sect
++) {
345 if (info
->protect
[sect
] == 0) { /* not protected */
346 addr2
= (FLASH_WORD_SIZE
*)(info
->start
[sect
]);
347 DEBUGF("Erasing sector %p\n", addr2
);
349 if ((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_SST
) {
350 addr
[ADDR0
] = (FLASH_WORD_SIZE
)0x00AA00AA;
351 addr
[ADDR1
] = (FLASH_WORD_SIZE
)0x00550055;
352 addr
[ADDR0
] = (FLASH_WORD_SIZE
)0x00800080;
353 addr
[ADDR0
] = (FLASH_WORD_SIZE
)0x00AA00AA;
354 addr
[ADDR1
] = (FLASH_WORD_SIZE
)0x00550055;
355 addr2
[0] = (FLASH_WORD_SIZE
)0x00500050; /* block erase */
357 udelay(1000); /* wait 1 ms */
359 addr
[ADDR0
] = (FLASH_WORD_SIZE
)0x00AA00AA;
360 addr
[ADDR1
] = (FLASH_WORD_SIZE
)0x00550055;
361 addr
[ADDR0
] = (FLASH_WORD_SIZE
)0x00800080;
362 addr
[ADDR0
] = (FLASH_WORD_SIZE
)0x00AA00AA;
363 addr
[ADDR1
] = (FLASH_WORD_SIZE
)0x00550055;
364 addr2
[0] = (FLASH_WORD_SIZE
)0x00300030; /* sector erase */
368 * Wait for each sector to complete, it's more
369 * reliable. According to AMD Spec, you must
370 * issue all erase commands within a specified
371 * timeout. This has been seen to fail, especially
372 * if printf()s are included (for debug)!!
374 wait_for_DQ7(info
, sect
);
378 /* re-enable interrupts if necessary */
382 /* wait at least 80us - let's wait 1 ms */
385 /* reset to read mode */
386 addr
= (FLASH_WORD_SIZE
*)info
->start
[0];
387 addr
[0] = (FLASH_WORD_SIZE
)0x00F000F0; /* reset bank */
393 /*-----------------------------------------------------------------------
394 * Copy memory to flash, returns:
397 * 2 - Flash not erased
400 int write_buff (flash_info_t
*info
, uchar
*src
, ulong addr
, ulong cnt
)
405 wp
= (addr
& ~3); /* get lower word aligned address */
408 * handle unaligned start bytes
410 if ((l
= addr
- wp
) != 0) {
412 for (i
=0, cp
=wp
; i
<l
; ++i
, ++cp
) {
413 data
= (data
<< 8) | (*(uchar
*)cp
);
415 for (; i
<4 && cnt
>0; ++i
) {
416 data
= (data
<< 8) | *src
++;
420 for (; cnt
==0 && i
<4; ++i
, ++cp
) {
421 data
= (data
<< 8) | (*(uchar
*)cp
);
424 if ((rc
= write_word(info
, wp
, data
)) != 0) {
431 * handle word aligned part
435 for (i
=0; i
<4; ++i
) {
436 data
= (data
<< 8) | *src
++;
438 if ((rc
= write_word(info
, wp
, data
)) != 0) {
450 * handle unaligned tail bytes
453 for (i
=0, cp
=wp
; i
<4 && cnt
>0; ++i
, ++cp
) {
454 data
= (data
<< 8) | *src
++;
457 for (; i
<4; ++i
, ++cp
) {
458 data
= (data
<< 8) | (*(uchar
*)cp
);
461 return (write_word(info
, wp
, data
));
464 /*-----------------------------------------------------------------------
465 * Write a word to Flash, returns:
468 * 2 - Flash not erased
470 static int write_word (flash_info_t
* info
, ulong dest
, ulong data
)
472 volatile FLASH_WORD_SIZE
*addr2
= (FLASH_WORD_SIZE
*) (info
->start
[0]);
473 volatile FLASH_WORD_SIZE
*dest2
= (FLASH_WORD_SIZE
*) dest
;
474 volatile FLASH_WORD_SIZE
*data2
= (FLASH_WORD_SIZE
*) & data
;
478 /* Check if Flash is (sufficiently) erased */
479 if ((*((volatile FLASH_WORD_SIZE
*) dest
) &
480 (FLASH_WORD_SIZE
) data
) != (FLASH_WORD_SIZE
) data
) {
484 for (i
= 0; i
< 4 / sizeof (FLASH_WORD_SIZE
); i
++) {
487 /* Disable interrupts which might cause a timeout here */
488 flag
= disable_interrupts ();
490 addr2
[ADDR0
] = (FLASH_WORD_SIZE
) 0x00AA00AA;
491 addr2
[ADDR1
] = (FLASH_WORD_SIZE
) 0x00550055;
492 addr2
[ADDR0
] = (FLASH_WORD_SIZE
) 0x00A000A0;
496 /* re-enable interrupts if necessary */
498 enable_interrupts ();
500 /* data polling for D7 */
501 start
= get_timer (0);
502 while ((dest2
[i
] & (FLASH_WORD_SIZE
) 0x00800080) !=
503 (data2
[i
] & (FLASH_WORD_SIZE
) 0x00800080)) {
505 if (get_timer (start
) > CFG_FLASH_WRITE_TOUT
) {