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,
25 * 07-10-2002 Frank Gottschling: added 29F032 flash (ELPPC).
26 * fixed monitor protection part
28 * 09-18-2001 Andreas Heppel: Reduced the code in here to the usage
29 * of AMD's 29F040 and 29F016 flashes, since the BAB7xx does use
34 #include <asm/processor.h>
35 #include <asm/pci_io.h>
37 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
39 ulong flash_get_size (vu_long *addr, flash_info_t *info);
40 static int write_word (flash_info_t *info, ulong dest, ulong data);
42 /*flash command address offsets*/
48 #define FLASH_WORD_SIZE unsigned char
50 /*----------------------------------------------------------------------------*/
52 unsigned long flash_init (void)
54 unsigned long size1, size2;
57 /* Init: no FLASHes known */
58 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i)
60 flash_info[i].flash_id = FLASH_UNKNOWN;
63 /* initialise 1st flash */
64 size1 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
66 if (flash_info[0].flash_id == FLASH_UNKNOWN)
68 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
72 /* initialise 2nd flash */
73 size2 = flash_get_size((vu_long *)FLASH_BASE1_PRELIM, &flash_info[1]);
75 if (flash_info[1].flash_id == FLASH_UNKNOWN)
77 printf ("## Unknown FLASH on Bank 1 - Size = 0x%08lx = %ld MB\n",
81 /* monitor protection ON by default */
82 if (size1 == 512*1024)
84 (void)flash_protect(FLAG_PROTECT_SET,
86 FLASH_BASE0_PRELIM+monitor_flash_len-1,
89 if (size2 == 512*1024)
91 (void)flash_protect(FLAG_PROTECT_SET,
93 FLASH_BASE1_PRELIM+monitor_flash_len-1,
96 if (size2 == 4*1024*1024)
98 (void)flash_protect(FLAG_PROTECT_SET,
100 CFG_FLASH_BASE+monitor_flash_len-1,
104 return (size1 + size2);
107 /*----------------------------------------------------------------------------*/
109 void flash_print_info (flash_info_t *info)
115 volatile unsigned long *flash;
117 if (info->flash_id == FLASH_UNKNOWN) {
118 printf ("missing or unknown FLASH type\n");
122 if (info->flash_id == FLASH_UNKNOWN) {
123 printf ("missing or unknown FLASH type\n");
127 switch (info->flash_id & FLASH_VENDMASK) {
132 printf ("Unknown Vendor ");
136 switch (info->flash_id & FLASH_TYPEMASK) {
138 printf ("AM29F040B (4 Mbit)\n");
141 printf ("AM29F016D (16 Mbit)\n");
144 printf ("AM29F032B (32 Mbit)\n");
147 printf ("Unknown Chip Type\n");
151 if (info->size >= (1 << 20)) {
152 printf (" Size: %ld MB in %d Sectors\n", info->size >> 20, info->sector_count);
154 printf (" Size: %ld kB in %d Sectors\n", info->size >> 10, info->sector_count);
157 printf (" Sector Start Addresses:");
158 for (i=0; i<info->sector_count; ++i) {
160 * Check if whole sector is erased
162 if (i != (info->sector_count-1))
163 size = info->start[i+1] - info->start[i];
165 size = info->start[0] + info->size - info->start[i];
168 flash = (volatile unsigned long *)info->start[i];
169 size = size >> 2; /* divide by 4 for longword access */
170 for (k=0; k<size; k++) {
171 if (*flash++ != 0xffffffff) {
180 printf (" %08lX%s%s",
183 info->protect[i] ? "RO " : " ");
188 /*----------------------------------------------------------------------------*/
190 * The following code cannot be run from FLASH!
192 ulong flash_get_size (vu_long *addr, flash_info_t *info)
196 ulong base = (ulong)addr;
197 volatile unsigned char *caddr = (unsigned char *)addr;
200 printf("flash_get_size for address 0x%lx: \n", (unsigned long)caddr);
203 /* Write auto select command: read Manufacturer ID */
204 caddr[0] = 0xF0; /* reset bank */
220 printf("Manufacturer: 0x%lx\n", vendor);
226 /* We accept only two AMD types */
228 case (FLASH_WORD_SIZE)AMD_MANUFACT:
229 info->flash_id = FLASH_MAN_AMD;
232 info->flash_id = FLASH_UNKNOWN;
233 info->sector_count = 0;
235 return (0); /* no or unknown flash */
239 case (FLASH_WORD_SIZE)AMD_ID_F040B:
240 info->flash_id |= AMD_ID_F040B;
241 info->sector_count = 8;
242 info->size = 0x00080000;
243 break; /* => 0.5 MB */
245 case (FLASH_WORD_SIZE)AMD_ID_F016D:
246 info->flash_id |= AMD_ID_F016D;
247 info->sector_count = 32;
248 info->size = 0x00200000;
251 case (FLASH_WORD_SIZE)AMD_ID_F032B:
252 info->flash_id |= AMD_ID_F032B;
253 info->sector_count = 64;
254 info->size = 0x00400000;
258 info->flash_id = FLASH_UNKNOWN;
259 return (0); /* => no or unknown flash */
264 printf("flash id 0x%lx; sector count 0x%x, size 0x%lx\n", info->flash_id, info->sector_count, info->size);
267 /* check for protected sectors */
268 for (i = 0; i < info->sector_count; i++) {
269 /* sector base address */
270 info->start[i] = base + i * (info->size / info->sector_count);
271 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
272 /* D0 = 1 if protected */
273 caddr = (volatile unsigned char *)(info->start[i]);
274 info->protect[i] = caddr[2] & 1;
278 * Prevent writes to uninitialized FLASH.
280 if (info->flash_id != FLASH_UNKNOWN) {
281 caddr = (volatile unsigned char *)info->start[0];
282 caddr[0] = 0xF0; /* reset bank */
288 /*----------------------------------------------------------------------------*/
290 int flash_erase (flash_info_t *info, int s_first, int s_last)
292 volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[0]);
293 int flag, prot, sect, l_sect;
294 ulong start, now, last;
297 if ((s_first < 0) || (s_first > s_last)) {
298 if (info->flash_id == FLASH_UNKNOWN) {
299 printf ("- missing\n");
301 printf ("- no sectors to erase\n");
306 if ((info->flash_id == FLASH_UNKNOWN) ||
307 (info->flash_id > FLASH_AMD_COMP)) {
308 printf ("Can't erase unknown flash type - aborted\n");
313 for (sect=s_first; sect<=s_last; ++sect) {
314 if (info->protect[sect]) {
320 printf ("- Warning: %d protected sectors will not be erased!\n",
328 /* Disable interrupts which might cause a timeout here */
329 flag = disable_interrupts();
331 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
332 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
333 addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
334 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
335 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
337 /* Start erase on unprotected sectors */
338 for (sect = s_first; sect<=s_last; sect++) {
339 if (info->protect[sect] == 0) { /* not protected */
340 addr = (FLASH_WORD_SIZE *)(info->start[sect]);
341 if (info->flash_id & FLASH_MAN_SST) {
342 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
343 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
344 addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
345 addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
346 addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
347 addr[0] = (FLASH_WORD_SIZE)0x00500050; /* block erase */
348 udelay(30000); /* wait 30 ms */
351 addr[0] = (FLASH_WORD_SIZE)0x00300030; /* sector erase */
356 /* re-enable interrupts if necessary */
360 /* wait at least 80us - let's wait 1 ms */
364 * We wait for the last triggered sector
369 start = get_timer (0);
371 addr = (FLASH_WORD_SIZE *)(info->start[l_sect]);
372 while ((addr[0] & (FLASH_WORD_SIZE)0x00800080) != (FLASH_WORD_SIZE)0x00800080) {
373 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
374 printf ("Timeout\n");
377 /* show that we're waiting */
378 if ((now - last) > 1000) { /* every second */
385 /* reset to read mode */
386 addr = (FLASH_WORD_SIZE *)info->start[0];
387 addr[0] = (FLASH_WORD_SIZE)0x00F000F0; /* reset bank */
393 /*----------------------------------------------------------------------------*/
395 * Copy memory to flash, returns:
398 * 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;
479 /* Check if Flash is (sufficiently) erased */
480 if ((*((volatile FLASH_WORD_SIZE *)dest) &
481 (FLASH_WORD_SIZE)data) != (FLASH_WORD_SIZE)data) {
484 /* Disable interrupts which might cause a timeout here */
485 flag = disable_interrupts();
487 for (i=0; i<4/sizeof(FLASH_WORD_SIZE); i++)
489 addr2[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
490 addr2[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
491 addr2[ADDR0] = (FLASH_WORD_SIZE)0x00A000A0;
495 /* re-enable interrupts if necessary */
499 /* data polling for D7 */
500 start = get_timer (0);
501 while ((dest2[i] & (FLASH_WORD_SIZE)0x00800080) !=
502 (data2[i] & (FLASH_WORD_SIZE)0x00800080)) {
503 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
512 /*----------------------------------------------------------------------------*/