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
33 #include <asm/processor.h>
35 #if CONFIG_SYS_MAX_FLASH_BANKS != 1
36 #error "CONFIG_SYS_MAX_FLASH_BANKS must be 1"
38 flash_info_t flash_info
[CONFIG_SYS_MAX_FLASH_BANKS
]; /* info for FLASH chips */
40 /*-----------------------------------------------------------------------
43 static ulong
flash_get_size (vu_long
* addr
, flash_info_t
* info
);
44 static int write_word (flash_info_t
* info
, ulong dest
, ulong data
);
45 static void flash_get_offsets (ulong base
, flash_info_t
* info
);
49 #define FLASH_WORD_SIZE unsigned char
51 /*-----------------------------------------------------------------------
54 unsigned long flash_init (void)
56 unsigned long size_b0
;
58 /* Init: no FLASHes known */
59 flash_info
[0].flash_id
= FLASH_UNKNOWN
;
61 /* Static FLASH Bank configuration here - FIXME XXX */
63 size_b0
= flash_get_size ((vu_long
*) FLASH_BASE0_PRELIM
, &flash_info
[0]);
65 if (flash_info
[0].flash_id
== FLASH_UNKNOWN
) {
66 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
67 size_b0
, size_b0
<< 20);
72 flash_get_offsets (FLASH_BASE0_PRELIM
, &flash_info
[0]);
74 /* Monitor protection ON by default */
75 (void) flash_protect (FLAG_PROTECT_SET
,
77 FLASH_BASE0_PRELIM
+ monitor_flash_len
- 1,
79 flash_info
[0].size
= size_b0
;
85 /*-----------------------------------------------------------------------
88 * This implementation assumes that the flash chips are uniform sector
89 * devices. This is true for all likely JSE devices.
91 static void flash_get_offsets (ulong base
, flash_info_t
* info
)
94 unsigned long sector_size
= info
->size
/ info
->sector_count
;
96 for (idx
= 0; idx
< info
->sector_count
; idx
+= 1) {
97 info
->start
[idx
] = base
+ (idx
* sector_size
);
101 /*-----------------------------------------------------------------------
103 void flash_print_info (flash_info_t
* info
)
109 volatile unsigned long *flash
;
111 if (info
->flash_id
== FLASH_UNKNOWN
) {
112 printf ("missing or unknown FLASH type\n");
116 switch (info
->flash_id
& FLASH_VENDMASK
) {
127 printf ("ST Micro ");
130 printf ("Unknown Vendor ");
134 /* (Reduced table of only parts expected in JSE boards.) */
135 switch (info
->flash_id
) {
136 case FLASH_MAN_AMD
| FLASH_AM040
:
137 printf ("AM29F040 (512 Kbit, uniform sector size)\n");
139 case FLASH_MAN_STM
| FLASH_AM040
:
140 printf ("MM29W040W (512 Kbit, uniform sector size)\n");
143 printf ("Unknown Chip Type\n");
147 printf (" Size: %ld KB in %d Sectors\n",
148 info
->size
>> 10, info
->sector_count
);
150 printf (" Sector Start Addresses:");
151 for (i
= 0; i
< info
->sector_count
; ++i
) {
153 * Check if whole sector is erased
155 if (i
!= (info
->sector_count
- 1))
156 size
= info
->start
[i
+ 1] - info
->start
[i
];
158 size
= info
->start
[0] + info
->size
- info
->start
[i
];
160 flash
= (volatile unsigned long *) info
->start
[i
];
161 size
= size
>> 2; /* divide by 4 for longword access */
162 for (k
= 0; k
< size
; k
++) {
163 if (*flash
++ != 0xffffffff) {
171 printf (" %08lX%s%s",
173 erased
? " E" : " ", info
->protect
[i
] ? "RO " : " "
180 /*-----------------------------------------------------------------------
184 /*-----------------------------------------------------------------------
188 * The following code cannot be run from FLASH!
190 static ulong
flash_get_size (vu_long
* addr
, flash_info_t
* info
)
193 FLASH_WORD_SIZE value
;
194 ulong base
= (ulong
) addr
;
195 volatile FLASH_WORD_SIZE
*addr2
= (FLASH_WORD_SIZE
*) addr
;
197 /* Write auto select command: read Manufacturer ID */
198 addr2
[ADDR0
] = (FLASH_WORD_SIZE
) 0x00AA00AA;
199 addr2
[ADDR1
] = (FLASH_WORD_SIZE
) 0x00550055;
200 addr2
[ADDR0
] = (FLASH_WORD_SIZE
) 0x00900090;
205 case (FLASH_WORD_SIZE
) AMD_MANUFACT
:
206 info
->flash_id
= FLASH_MAN_AMD
;
208 case (FLASH_WORD_SIZE
) FUJ_MANUFACT
:
209 info
->flash_id
= FLASH_MAN_FUJ
;
211 case (FLASH_WORD_SIZE
) SST_MANUFACT
:
212 info
->flash_id
= FLASH_MAN_SST
;
214 case (FLASH_WORD_SIZE
)STM_MANUFACT
:
215 info
->flash_id
= FLASH_MAN_STM
;
218 info
->flash_id
= FLASH_UNKNOWN
;
219 info
->sector_count
= 0;
221 printf("Unknown flash manufacturer code: 0x%x\n", value
);
222 return (0); /* no or unknown flash */
225 value
= addr2
[1]; /* device ID */
228 case (FLASH_WORD_SIZE
) AMD_ID_F040B
:
229 info
->flash_id
+= FLASH_AM040
;
230 info
->sector_count
= 8;
231 info
->size
= 0x0080000; /* => 512 ko */
233 case (FLASH_WORD_SIZE
) AMD_ID_LV040B
:
234 info
->flash_id
+= FLASH_AM040
;
235 info
->sector_count
= 8;
236 info
->size
= 0x0080000; /* => 512 ko */
238 case (FLASH_WORD_SIZE
)STM_ID_M29W040B
: /* most likele JSE chip */
239 info
->flash_id
+= FLASH_AM040
;
240 info
->sector_count
= 8;
241 info
->size
= 0x0080000; /* => 512 ko */
244 info
->flash_id
= FLASH_UNKNOWN
;
245 return (0); /* => no or unknown flash */
249 /* Calculate the sector offsets (Use JSE Optimized code). */
250 flash_get_offsets(base
, info
);
252 /* check for protected sectors */
253 for (i
= 0; i
< info
->sector_count
; i
++) {
254 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
255 /* D0 = 1 if protected */
256 addr2
= (volatile FLASH_WORD_SIZE
*) (info
->start
[i
]);
257 if ((info
->flash_id
& FLASH_VENDMASK
) == FLASH_MAN_SST
)
258 info
->protect
[i
] = 0;
260 info
->protect
[i
] = addr2
[2] & 1;
264 * Prevent writes to uninitialized FLASH.
266 if (info
->flash_id
!= FLASH_UNKNOWN
) {
267 addr2
= (FLASH_WORD_SIZE
*) info
->start
[0];
268 *addr2
= (FLASH_WORD_SIZE
) 0x00F000F0; /* reset bank */
274 int wait_for_DQ7 (flash_info_t
* info
, int sect
)
276 ulong start
, now
, last
;
277 volatile FLASH_WORD_SIZE
*addr
=
278 (FLASH_WORD_SIZE
*) (info
->start
[sect
]);
280 start
= get_timer (0);
282 while ((addr
[0] & (FLASH_WORD_SIZE
) 0x00800080) !=
283 (FLASH_WORD_SIZE
) 0x00800080) {
284 if ((now
= get_timer (start
)) > CONFIG_SYS_FLASH_ERASE_TOUT
) {
285 printf ("Timeout\n");
288 /* show that we're waiting */
289 if ((now
- last
) > 1000) { /* every second */
297 /*-----------------------------------------------------------------------
300 int flash_erase (flash_info_t
* info
, int s_first
, int s_last
)
302 volatile FLASH_WORD_SIZE
*addr
= (FLASH_WORD_SIZE
*) (info
->start
[0]);
303 volatile FLASH_WORD_SIZE
*addr2
;
304 int flag
, prot
, sect
, l_sect
;
307 if ((s_first
< 0) || (s_first
> s_last
)) {
308 if (info
->flash_id
== FLASH_UNKNOWN
) {
309 printf ("- missing\n");
311 printf ("- no sectors to erase\n");
316 if (info
->flash_id
== FLASH_UNKNOWN
) {
317 printf ("Can't erase unknown flash type - aborted\n");
322 for (sect
= s_first
; sect
<= s_last
; ++sect
) {
323 if (info
->protect
[sect
]) {
329 printf ("- Warning: %d protected sectors will not be erased!\n", prot
);
336 /* Disable interrupts which might cause a timeout here */
337 flag
= disable_interrupts ();
339 /* Start erase on unprotected sectors */
340 for (sect
= s_first
; sect
<= s_last
; sect
++) {
341 if (info
->protect
[sect
] == 0) { /* not protected */
342 addr2
= (FLASH_WORD_SIZE
*) (info
->start
[sect
]);
343 printf ("Erasing sector %p\n", addr2
); /* CLH */
345 if ((info
->flash_id
& FLASH_VENDMASK
) ==
347 addr
[ADDR0
] = (FLASH_WORD_SIZE
) 0x00AA00AA;
348 addr
[ADDR1
] = (FLASH_WORD_SIZE
) 0x00550055;
349 addr
[ADDR0
] = (FLASH_WORD_SIZE
) 0x00800080;
350 addr
[ADDR0
] = (FLASH_WORD_SIZE
) 0x00AA00AA;
351 addr
[ADDR1
] = (FLASH_WORD_SIZE
) 0x00550055;
352 addr2
[0] = (FLASH_WORD_SIZE
) 0x00500050; /* block erase */
353 for (i
= 0; i
< 50; i
++)
354 udelay (1000); /* wait 1 ms */
356 addr
[ADDR0
] = (FLASH_WORD_SIZE
) 0x00AA00AA;
357 addr
[ADDR1
] = (FLASH_WORD_SIZE
) 0x00550055;
358 addr
[ADDR0
] = (FLASH_WORD_SIZE
) 0x00800080;
359 addr
[ADDR0
] = (FLASH_WORD_SIZE
) 0x00AA00AA;
360 addr
[ADDR1
] = (FLASH_WORD_SIZE
) 0x00550055;
361 addr2
[0] = (FLASH_WORD_SIZE
) 0x00300030; /* sector erase */
365 * Wait for each sector to complete, it's more
366 * reliable. According to AMD Spec, you must
367 * issue all erase commands within a specified
368 * timeout. This has been seen to fail, especially
369 * if printf()s are included (for debug)!!
371 wait_for_DQ7 (info
, sect
);
375 /* re-enable interrupts if necessary */
377 enable_interrupts ();
379 /* wait at least 80us - let's wait 1 ms */
384 * We wait for the last triggered sector
388 wait_for_DQ7 (info
, l_sect
);
392 /* reset to read mode */
393 addr
= (FLASH_WORD_SIZE
*) info
->start
[0];
394 addr
[0] = (FLASH_WORD_SIZE
) 0x00F000F0; /* reset bank */
400 /*-----------------------------------------------------------------------
401 * Copy memory to flash, returns:
404 * 2 - Flash not erased
407 int write_buff (flash_info_t
* info
, uchar
* src
, ulong addr
, ulong cnt
)
412 wp
= (addr
& ~3); /* get lower word aligned address */
415 * handle unaligned start bytes
417 if ((l
= addr
- wp
) != 0) {
419 for (i
= 0, cp
= wp
; i
< l
; ++i
, ++cp
) {
420 data
= (data
<< 8) | (*(uchar
*) cp
);
422 for (; i
< 4 && cnt
> 0; ++i
) {
423 data
= (data
<< 8) | *src
++;
427 for (; cnt
== 0 && i
< 4; ++i
, ++cp
) {
428 data
= (data
<< 8) | (*(uchar
*) cp
);
431 if ((rc
= write_word (info
, wp
, data
)) != 0) {
438 * handle word aligned part
442 for (i
= 0; i
< 4; ++i
) {
443 data
= (data
<< 8) | *src
++;
445 if ((rc
= write_word (info
, wp
, data
)) != 0) {
457 * handle unaligned tail bytes
460 for (i
= 0, cp
= wp
; i
< 4 && cnt
> 0; ++i
, ++cp
) {
461 data
= (data
<< 8) | *src
++;
464 for (; i
< 4; ++i
, ++cp
) {
465 data
= (data
<< 8) | (*(uchar
*) cp
);
468 return (write_word (info
, wp
, data
));
471 /*-----------------------------------------------------------------------
472 * Write a word to Flash, returns:
475 * 2 - Flash not erased
477 static int write_word (flash_info_t
* info
, ulong dest
, ulong data
)
479 volatile FLASH_WORD_SIZE
*addr2
=
480 (FLASH_WORD_SIZE
*) (info
->start
[0]);
481 volatile FLASH_WORD_SIZE
*dest2
= (FLASH_WORD_SIZE
*) dest
;
482 volatile FLASH_WORD_SIZE
*data2
= (FLASH_WORD_SIZE
*) & data
;
486 /* Check if Flash is (sufficiently) erased */
487 if ((*((volatile FLASH_WORD_SIZE
*) dest
) &
488 (FLASH_WORD_SIZE
) data
) != (FLASH_WORD_SIZE
) data
) {
492 for (i
= 0; i
< 4 / sizeof (FLASH_WORD_SIZE
); i
++) {
495 /* Disable interrupts which might cause a timeout here */
496 flag
= disable_interrupts ();
498 addr2
[ADDR0
] = (FLASH_WORD_SIZE
) 0x00AA00AA;
499 addr2
[ADDR1
] = (FLASH_WORD_SIZE
) 0x00550055;
500 addr2
[ADDR0
] = (FLASH_WORD_SIZE
) 0x00A000A0;
504 /* re-enable interrupts if necessary */
506 enable_interrupts ();
508 /* data polling for D7 */
509 start
= get_timer (0);
510 while ((dest2
[i
] & (FLASH_WORD_SIZE
) 0x00800080) !=
511 (data2
[i
] & (FLASH_WORD_SIZE
) 0x00800080)) {
513 if (get_timer (start
) > CONFIG_SYS_FLASH_WRITE_TOUT
) {