2 * (C) Copyright 2000-2004
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 * Wait for completion of each sector erase command issued
28 * Chris Hallinan - DS4.COM, Inc. - clh@net1plus.com
32 * Niklaus Giger, Netstal Maschinen, niklaus.giger@netstal.com
38 #include <asm/processor.h>
40 #if CFG_MAX_FLASH_BANKS != 1
41 #error "CFG_MAX_FLASH_BANKS must be 1"
43 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
45 /*-----------------------------------------------------------------------
48 static ulong flash_get_size (vu_long * addr, flash_info_t * info);
49 static int write_word (flash_info_t * info, ulong dest, ulong data);
50 static void flash_get_offsets (ulong base, flash_info_t * info);
54 #define FLASH_WORD_SIZE unsigned char
56 /*-----------------------------------------------------------------------*/
58 unsigned long flash_init (void)
60 unsigned long size_b0;
62 /* Init: no FLASHes known */
63 flash_info[0].flash_id = FLASH_UNKNOWN;
65 /* Static FLASH Bank configuration here - FIXME XXX */
67 size_b0 = flash_get_size ((vu_long *) FLASH_BASE0_PRELIM,
70 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
71 printf ("## Unknown FLASH on Bank 0- Size=0x%08lx=%ld MB\n",
72 size_b0, size_b0 << 20);
77 flash_get_offsets (FLASH_BASE0_PRELIM, &flash_info[0]);
79 /* Monitor protection ON by default */
80 (void) flash_protect (FLAG_PROTECT_SET,
82 FLASH_BASE0_PRELIM + monitor_flash_len - 1,
84 flash_info[0].size = size_b0;
90 /*-----------------------------------------------------------------------*/
92 * This implementation assumes that the flash chips are uniform sector
93 * devices. This is true for all likely flash devices on a HCUx.
95 static void flash_get_offsets (ulong base, flash_info_t * info)
98 unsigned long sector_size = info->size / info->sector_count;
100 for (idx = 0; idx < info->sector_count; idx += 1) {
101 info->start[idx] = base + (idx * sector_size);
105 /*-----------------------------------------------------------------------*/
106 void flash_print_info (flash_info_t * info)
112 volatile unsigned long *flash;
114 if (info->flash_id == FLASH_UNKNOWN) {
115 printf ("missing or unknown FLASH type\n");
119 switch (info->flash_id & FLASH_VENDMASK) {
130 printf ("ST Micro ");
133 printf ("Unknown Vendor ");
137 /* (Reduced table of only parts expected in HCUx boards.) */
138 switch (info->flash_id) {
139 case FLASH_MAN_AMD | FLASH_AM040:
140 printf ("AM29F040 (512 Kbit, uniform sector size)\n");
142 case FLASH_MAN_STM | FLASH_AM040:
143 printf ("MM29W040W (512 Kbit, uniform sector size)\n");
146 printf ("Unknown Chip Type\n");
150 printf (" Size: %ld KB in %d Sectors\n",
151 info->size >> 10, info->sector_count);
153 printf (" Sector Start Addresses:");
154 for (i = 0; i < info->sector_count; ++i) {
156 * Check if whole sector is erased
158 if (i != (info->sector_count - 1))
159 size = info->start[i + 1] - info->start[i];
161 size = info->start[0] + info->size - info->start[i];
163 flash = (volatile unsigned long *) info->start[i];
164 size = size >> 2; /* divide by 4 for longword access */
165 for (k = 0; k < size; k++) {
166 if (*flash++ != 0xffffffff) {
174 printf (" %08lX%s%s",
176 erased ? " E" : " ", info->protect[i] ? "RO " : " "
183 /*-----------------------------------------------------------------------*/
186 * The following code cannot be run from FLASH!
188 static ulong flash_get_size (vu_long * addr, flash_info_t * info)
191 FLASH_WORD_SIZE value;
192 ulong base = (ulong) addr;
193 volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *) addr;
195 /* Write auto select command: read Manufacturer ID */
197 addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
199 addr2[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
201 addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00900090;
208 case (FLASH_WORD_SIZE) AMD_MANUFACT:
209 info->flash_id = FLASH_MAN_AMD;
211 case (FLASH_WORD_SIZE) FUJ_MANUFACT:
212 info->flash_id = FLASH_MAN_FUJ;
214 case (FLASH_WORD_SIZE) SST_MANUFACT:
215 info->flash_id = FLASH_MAN_SST;
217 case (FLASH_WORD_SIZE)STM_MANUFACT:
218 info->flash_id = FLASH_MAN_STM;
221 info->flash_id = FLASH_UNKNOWN;
222 info->sector_count = 0;
224 printf("Unknown flash manufacturer code: 0x%x at %p\n",
226 addr2[ADDR0] = (FLASH_WORD_SIZE) 0;
227 return (0); /* no or unknown flash */
230 value = addr2[1]; /* device ID */
233 case (FLASH_WORD_SIZE) AMD_ID_F040B:
234 info->flash_id += FLASH_AM040;
235 info->sector_count = 8;
236 info->size = 0x0080000; /* => 512 ko */
238 case (FLASH_WORD_SIZE) AMD_ID_LV040B:
239 info->flash_id += FLASH_AM040;
240 info->sector_count = 8;
241 info->size = 0x0080000; /* => 512 ko */
243 case (FLASH_WORD_SIZE)STM_ID_M29W040B: /* most likele HCU5 chip */
244 info->flash_id += FLASH_AM040;
245 info->sector_count = 8;
246 info->size = 0x0080000; /* => 512 ko */
249 info->flash_id = FLASH_UNKNOWN;
250 return (0); /* => no or unknown flash */
254 /* Calculate the sector offsets (Use HCUx Optimized code). */
255 flash_get_offsets(base, info);
257 /* check for protected sectors */
258 for (i = 0; i < info->sector_count; i++) {
259 /* read sector protection at sector address,
261 * D0 = 1 if protected
263 addr2 = (volatile FLASH_WORD_SIZE *) (info->start[i]);
264 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
265 info->protect[i] = 0;
267 info->protect[i] = addr2[2] & 1;
271 * Prevent writes to uninitialized FLASH.
273 if (info->flash_id != FLASH_UNKNOWN) {
274 addr2 = (FLASH_WORD_SIZE *) info->start[0];
275 *addr2 = (FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
281 int wait_for_DQ7 (flash_info_t * info, int sect)
283 ulong start, now, last;
284 volatile FLASH_WORD_SIZE *addr =
285 (FLASH_WORD_SIZE *) (info->start[sect]);
287 start = get_timer (0);
289 while ((addr[0] & (FLASH_WORD_SIZE) 0x00800080) !=
290 (FLASH_WORD_SIZE) 0x00800080) {
291 if ((now = get_timer (start)) > CFG_FLASH_ERASE_TOUT) {
292 printf ("Timeout\n");
295 /* show that we're waiting */
296 if ((now - last) > 1000) { /* every second */
304 /*-----------------------------------------------------------------------*/
306 int flash_erase (flash_info_t * info, int s_first, int s_last)
308 volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *) (info->start[0]);
309 volatile FLASH_WORD_SIZE *addr2;
310 int flag, prot, sect, l_sect;
313 if ((s_first < 0) || (s_first > s_last)) {
314 if (info->flash_id == FLASH_UNKNOWN) {
315 printf ("- missing\n");
317 printf ("- no sectors to erase\n");
322 if (info->flash_id == FLASH_UNKNOWN) {
323 printf ("Can't erase unknown flash type - aborted\n");
328 for (sect = s_first; sect <= s_last; ++sect) {
329 if (info->protect[sect]) {
335 printf ("- Warning: %d protected sectors not erased!\n",
343 /* Disable interrupts which might cause a timeout here */
344 flag = disable_interrupts ();
346 /* Start erase on unprotected sectors */
347 for (sect = s_first; sect <= s_last; sect++) {
348 if (info->protect[sect] == 0) { /* not protected */
349 addr2 = (FLASH_WORD_SIZE *) (info->start[sect]);
350 printf ("Erasing sector %p\n", addr2); /* CLH */
352 if ((info->flash_id & FLASH_VENDMASK) ==
354 addr[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
355 addr[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
356 addr[ADDR0] = (FLASH_WORD_SIZE) 0x00800080;
357 addr[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
358 addr[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
360 addr2[0] = (FLASH_WORD_SIZE) 0x00500050;
361 for (i = 0; i < 50; i++) udelay (1000);
363 addr[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
364 addr[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
365 addr[ADDR0] = (FLASH_WORD_SIZE) 0x00800080;
366 addr[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
367 addr[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
369 addr2[0] = (FLASH_WORD_SIZE) 0x00300030;
373 * Wait for each sector to complete, it's more
374 * reliable. According to AMD Spec, you must
375 * issue all erase commands within a specified
376 * timeout. This has been seen to fail, especially
377 * if printf()s are included (for debug)!!
379 wait_for_DQ7 (info, sect);
383 /* re-enable interrupts if necessary */
385 enable_interrupts ();
387 /* wait at least 80us - let's wait 1 ms */
392 * We wait for the last triggered sector
396 wait_for_DQ7 (info, l_sect);
400 /* reset to read mode */
401 addr = (FLASH_WORD_SIZE *) info->start[0];
402 addr[0] = (FLASH_WORD_SIZE) 0x00F000F0; /* reset bank */
408 /*-----------------------------------------------------------------------
409 * Copy memory to flash, returns:
412 * 2 - Flash not erased
415 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
420 wp = (addr & ~3); /* get lower word aligned address */
423 * handle unaligned start bytes
425 if ((l = addr - wp) != 0) {
427 for (i = 0, cp = wp; i < l; ++i, ++cp) {
428 data = (data << 8) | (*(uchar *) cp);
430 for (; i < 4 && cnt > 0; ++i) {
431 data = (data << 8) | *src++;
435 for (; cnt == 0 && i < 4; ++i, ++cp) {
436 data = (data << 8) | (*(uchar *) cp);
439 if ((rc = write_word (info, wp, data)) != 0) {
446 * handle word aligned part
450 for (i = 0; i < 4; ++i) {
451 data = (data << 8) | *src++;
453 if ((rc = write_word (info, wp, data)) != 0) {
465 * handle unaligned tail bytes
468 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
469 data = (data << 8) | *src++;
472 for (; i < 4; ++i, ++cp) {
473 data = (data << 8) | (*(uchar *) cp);
476 return (write_word (info, wp, data));
479 /*-----------------------------------------------------------------------
480 * Write a word to Flash, returns:
483 * 2 - Flash not erased
485 static int write_word (flash_info_t * info, ulong dest, ulong data)
487 volatile FLASH_WORD_SIZE *addr2 =
488 (FLASH_WORD_SIZE *) (info->start[0]);
489 volatile FLASH_WORD_SIZE *dest2 = (FLASH_WORD_SIZE *) dest;
490 volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *) & data;
494 /* Check if Flash is (sufficiently) erased */
495 if ((*((volatile FLASH_WORD_SIZE *) dest) &
496 (FLASH_WORD_SIZE) data) != (FLASH_WORD_SIZE) data) {
500 for (i = 0; i < 4 / sizeof (FLASH_WORD_SIZE); i++) {
503 /* Disable interrupts which might cause a timeout here */
504 flag = disable_interrupts ();
506 addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
507 addr2[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
508 addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00A000A0;
512 /* re-enable interrupts if necessary */
514 enable_interrupts ();
516 /* data polling for D7 */
517 start = get_timer (0);
518 while ((dest2[i] & (FLASH_WORD_SIZE) 0x00800080) !=
519 (data2[i] & (FLASH_WORD_SIZE) 0x00800080)) {
521 if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {