to make u-boot work for fat32 filesystem
[jz_uboot.git] / board / gth / flash.c
blob41a5c50b0fac45e5bcb988e6189bcb054443e299
1 /*
2 * (C) Copyright 2000
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
6 * project.
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,
21 * MA 02111-1307 USA
24 #include <common.h>
25 #include <mpc8xx.h>
27 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
29 /*-----------------------------------------------------------------------
30 * Functions
32 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
33 static int write_word (flash_info_t *info, ulong dest, ulong data);
34 static void flash_get_offsets (ulong base, flash_info_t *info);
36 /*-----------------------------------------------------------------------
37 * Protection Flags:
39 #define FLAG_PROTECT_SET 0x01
40 #define FLAG_PROTECT_CLEAR 0x02
42 /*-----------------------------------------------------------------------
45 unsigned long flash_init (void)
47 volatile immap_t *immap = (immap_t *)CFG_IMMR;
48 volatile memctl8xx_t *memctl = &immap->im_memctl;
49 unsigned long size_b0, size_b1;
50 int i;
52 /*printf("faking");*/
54 return(0x1fffff);
56 /* Init: no FLASHes known */
57 for (i=0; i < CFG_MAX_FLASH_BANKS; ++i)
59 flash_info[i].flash_id = FLASH_UNKNOWN;
62 /* Static FLASH Bank configuration here - FIXME XXX */
64 size_b0 = 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",
69 size_b0, size_b0<<20);
72 #if 0
73 if (FLASH_BASE1_PRELIM != 0x0) {
74 size_b1 = flash_get_size((vu_long *)FLASH_BASE1_PRELIM, &flash_info[1]);
76 if (size_b1 > size_b0) {
77 printf ("## ERROR: Bank 1 (0x%08lx = %ld MB) > Bank 0 (0x%08lx = %ld MB)\n",
78 size_b1, size_b1<<20,size_b0, size_b0<<20);
80 flash_info[0].flash_id = FLASH_UNKNOWN;
81 flash_info[1].flash_id = FLASH_UNKNOWN;
82 flash_info[0].sector_count = -1;
83 flash_info[1].sector_count = -1;
84 flash_info[0].size = 0;
85 flash_info[1].size = 0;
86 return (0);
88 } else {
89 #endif
90 size_b1 = 0;
92 /* Remap FLASH according to real size */
93 memctl->memc_or0 = CFG_OR0_PRELIM;
94 memctl->memc_br0 = CFG_BR0_PRELIM;
96 /* Re-do sizing to get full correct info */
97 size_b0 = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
99 flash_get_offsets (CFG_FLASH_BASE, &flash_info[0]);
101 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
102 /* monitor protection ON by default */
103 (void)flash_protect(FLAG_PROTECT_SET,
104 CFG_MONITOR_BASE,
105 CFG_MONITOR_BASE+monitor_flash_len-1,
106 &flash_info[0]);
107 #endif
109 if (size_b1)
111 /* memctl->memc_or1 = CFG_OR1_PRELIM;
112 memctl->memc_br1 = CFG_BR1_PRELIM; */
114 /* Re-do sizing to get full correct info */
115 size_b1 = flash_get_size((vu_long *)(CFG_FLASH_BASE + size_b0),
116 &flash_info[1]);
118 flash_get_offsets (CFG_FLASH_BASE + size_b0, &flash_info[1]);
120 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
121 /* monitor protection ON by default */
122 (void)flash_protect(FLAG_PROTECT_SET,
123 CFG_MONITOR_BASE,
124 CFG_MONITOR_BASE+monitor_flash_len-1,
125 &flash_info[1]);
126 #endif
128 else
130 /* memctl->memc_or1 = CFG_OR1_PRELIM;
131 FIXME memctl->memc_br1 = CFG_BR1_PRELIM; */
133 flash_info[1].flash_id = FLASH_UNKNOWN;
134 flash_info[1].sector_count = -1;
137 flash_info[0].size = size_b0;
138 flash_info[1].size = size_b1;
140 return (size_b0 + size_b1);
144 static void flash_get_offsets (ulong base, flash_info_t *info)
146 int i;
148 /* set up sector start adress table */
149 if (info->flash_id & FLASH_BTYPE)
151 /* set sector offsets for bottom boot block type */
152 for (i = 0; i < info->sector_count; i++)
154 info->start[i] = base + (i * 0x00040000);
157 else
159 /* set sector offsets for top boot block type */
160 i = info->sector_count - 1;
161 for (; i >= 0; i--)
163 info->start[i] = base + i * 0x00040000;
169 /*-----------------------------------------------------------------------
171 void flash_print_info (flash_info_t *info)
173 int i;
175 if (info->flash_id == FLASH_UNKNOWN) {
176 printf ("missing or unknown FLASH type\n");
177 return;
180 switch (info->flash_id & FLASH_VENDMASK) {
181 case FLASH_MAN_AMD: printf ("AMD "); break;
182 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
183 default: printf ("Unknown Vendor "); break;
186 switch (info->flash_id & FLASH_TYPEMASK) {
188 #if 0
189 case FLASH_AM040B:
190 printf ("AM29F040B (4 Mbit, bottom boot sect)\n");
191 break;
192 case FLASH_AM040T:
193 printf ("AM29F040T (4 Mbit, top boot sect)\n");
194 break;
195 #endif
196 case FLASH_AM400B:
197 printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
198 break;
199 case FLASH_AM400T:
200 printf ("AM29LV400T (4 Mbit, top boot sector)\n");
201 break;
202 case FLASH_AM800B:
203 printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
204 break;
205 case FLASH_AM800T:
206 printf ("AM29LV800T (8 Mbit, top boot sector)\n");
207 break;
208 case FLASH_AM160B:
209 printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
210 break;
211 case FLASH_AM160T:
212 printf ("AM29LV160T (16 Mbit, top boot sector)\n");
213 break;
214 case FLASH_AM320B:
215 printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
216 break;
217 case FLASH_AM320T:
218 printf ("AM29LV320T (32 Mbit, top boot sector)\n");
219 break;
220 default:
221 printf ("Unknown Chip Type\n");
222 break;
225 printf (" Size: %ld MB in %d Sectors\n",
226 info->size >> 20,
227 info->sector_count);
229 printf (" Sector Start Addresses:");
231 for (i=0; i<info->sector_count; ++i)
233 if ((i % 5) == 0)
235 printf ("\n ");
238 printf (" %08lX%s",
239 info->start[i],
240 info->protect[i] ? " (RO)" : " ");
243 printf ("\n");
244 return;
247 /*-----------------------------------------------------------------------
251 /*-----------------------------------------------------------------------
255 * The following code cannot be run from FLASH!
258 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
260 short i;
261 #if 0
262 ulong base = (ulong)addr;
263 #endif
264 ulong value;
266 /* Write auto select command: read Manufacturer ID */
267 #if 0
268 addr[0x0555] = 0x00AA00AA;
269 addr[0x02AA] = 0x00550055;
270 addr[0x0555] = 0x00900090;
271 #else
272 addr[0x0555] = 0xAAAAAAAA;
273 addr[0x02AA] = 0x55555555;
274 addr[0x0555] = 0x90909090;
275 #endif
277 value = addr[0];
279 switch (value)
281 case AMD_MANUFACT:
282 info->flash_id = FLASH_MAN_AMD;
283 break;
285 case FUJ_MANUFACT:
286 info->flash_id = FLASH_MAN_FUJ;
287 break;
289 default:
290 info->flash_id = FLASH_UNKNOWN;
291 info->sector_count = 0;
292 info->size = 0;
293 break;
296 value = addr[1]; /* device ID */
298 switch (value)
300 #if 0
301 case AMD_ID_F040B:
302 info->flash_id += FLASH_AM040B;
303 info->sector_count = 8;
304 info->size = 0x00200000;
305 break; /* => 2 MB */
306 #endif
307 case AMD_ID_LV400T:
308 info->flash_id += FLASH_AM400T;
309 info->sector_count = 11;
310 info->size = 0x00100000;
311 break; /* => 1 MB */
313 case AMD_ID_LV400B:
314 info->flash_id += FLASH_AM400B;
315 info->sector_count = 11;
316 info->size = 0x00100000;
317 break; /* => 1 MB */
319 case AMD_ID_LV800T:
320 info->flash_id += FLASH_AM800T;
321 info->sector_count = 19;
322 info->size = 0x00200000;
323 break; /* => 2 MB */
325 case AMD_ID_LV800B:
326 info->flash_id += FLASH_AM800B;
327 info->sector_count = 19;
328 info->size = 0x00200000;
329 break; /* => 2 MB */
331 case AMD_ID_LV160T:
332 info->flash_id += FLASH_AM160T;
333 info->sector_count = 35;
334 info->size = 0x00400000;
335 break; /* => 4 MB */
337 case AMD_ID_LV160B:
338 info->flash_id += FLASH_AM160B;
339 info->sector_count = 35;
340 info->size = 0x00400000;
341 break; /* => 4 MB */
342 #if 0 /* enable when device IDs are available */
343 case AMD_ID_LV320T:
344 info->flash_id += FLASH_AM320T;
345 info->sector_count = 67;
346 info->size = 0x00800000;
347 break; /* => 8 MB */
349 case AMD_ID_LV320B:
350 info->flash_id += FLASH_AM320B;
351 info->sector_count = 67;
352 info->size = 0x00800000;
353 break; /* => 8 MB */
354 #endif
355 default:
356 info->flash_id = FLASH_UNKNOWN;
357 return (0); /* => no or unknown flash */
361 #if 0
362 /* set up sector start adress table */
363 if (info->flash_id & FLASH_BTYPE) {
364 /* set sector offsets for bottom boot block type */
365 info->start[0] = base + 0x00000000;
366 info->start[1] = base + 0x00008000;
367 info->start[2] = base + 0x0000C000;
368 info->start[3] = base + 0x00010000;
369 for (i = 4; i < info->sector_count; i++) {
370 info->start[i] = base + (i * 0x00020000) - 0x00060000;
372 } else {
373 /* set sector offsets for top boot block type */
374 i = info->sector_count - 1;
375 info->start[i--] = base + info->size - 0x00008000;
376 info->start[i--] = base + info->size - 0x0000C000;
377 info->start[i--] = base + info->size - 0x00010000;
378 for (; i >= 0; i--) {
379 info->start[i] = base + i * 0x00020000;
382 #else
383 flash_get_offsets ((ulong)addr, &flash_info[0]);
384 #endif
386 /* check for protected sectors */
387 for (i = 0; i < info->sector_count; i++)
389 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
390 /* D0 = 1 if protected */
391 addr = (volatile unsigned long *)(info->start[i]);
392 info->protect[i] = addr[2] & 1;
396 * Prevent writes to uninitialized FLASH.
398 if (info->flash_id != FLASH_UNKNOWN)
400 addr = (volatile unsigned long *)info->start[0];
401 #if 0
402 *addr = 0x00F000F0; /* reset bank */
403 #else
404 *addr = 0xF0F0F0F0; /* reset bank */
405 #endif
408 return (info->size);
412 /*-----------------------------------------------------------------------
415 int flash_erase (flash_info_t *info, int s_first, int s_last)
417 vu_long *addr = (vu_long*)(info->start[0]);
418 int flag, prot, sect, l_sect;
419 ulong start, now, last;
421 if ((s_first < 0) || (s_first > s_last)) {
422 if (info->flash_id == FLASH_UNKNOWN) {
423 printf ("- missing\n");
424 } else {
425 printf ("- no sectors to erase\n");
427 return 1;
430 if ((info->flash_id == FLASH_UNKNOWN) ||
431 (info->flash_id > FLASH_AMD_COMP)) {
432 printf ("Can't erase unknown flash type - aborted\n");
433 return 1;
436 prot = 0;
437 for (sect=s_first; sect<=s_last; ++sect) {
438 if (info->protect[sect]) {
439 prot++;
443 if (prot) {
444 printf ("- Warning: %d protected sectors will not be erased!\n",
445 prot);
446 } else {
447 printf ("\n");
450 l_sect = -1;
452 /* Disable interrupts which might cause a timeout here */
453 flag = disable_interrupts();
455 #if 0
456 addr[0x0555] = 0x00AA00AA;
457 addr[0x02AA] = 0x00550055;
458 addr[0x0555] = 0x00800080;
459 addr[0x0555] = 0x00AA00AA;
460 addr[0x02AA] = 0x00550055;
461 #else
462 addr[0x0555] = 0xAAAAAAAA;
463 addr[0x02AA] = 0x55555555;
464 addr[0x0555] = 0x80808080;
465 addr[0x0555] = 0xAAAAAAAA;
466 addr[0x02AA] = 0x55555555;
467 #endif
469 /* Start erase on unprotected sectors */
470 for (sect = s_first; sect<=s_last; sect++) {
471 if (info->protect[sect] == 0) { /* not protected */
472 addr = (vu_long*)(info->start[sect]);
473 #if 0
474 addr[0] = 0x00300030;
475 #else
476 addr[0] = 0x30303030;
477 #endif
478 l_sect = sect;
482 /* re-enable interrupts if necessary */
483 if (flag)
484 enable_interrupts();
486 /* wait at least 80us - let's wait 1 ms */
487 udelay (1000);
490 * We wait for the last triggered sector
492 if (l_sect < 0)
493 goto DONE;
495 start = get_timer (0);
496 last = start;
497 addr = (vu_long*)(info->start[l_sect]);
498 #if 0
499 while ((addr[0] & 0x00800080) != 0x00800080)
500 #else
501 while ((addr[0] & 0xFFFFFFFF) != 0xFFFFFFFF)
502 #endif
504 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
505 printf ("Timeout\n");
506 return 1;
508 /* show that we're waiting */
509 if ((now - last) > 1000) { /* every second */
510 putc ('.');
511 last = now;
515 DONE:
516 /* reset to read mode */
517 addr = (volatile unsigned long *)info->start[0];
518 #if 0
519 addr[0] = 0x00F000F0; /* reset bank */
520 #else
521 addr[0] = 0xF0F0F0F0; /* reset bank */
522 #endif
524 printf (" done\n");
525 return 0;
528 /*-----------------------------------------------------------------------
529 * Copy memory to flash, returns:
530 * 0 - OK
531 * 1 - write timeout
532 * 2 - Flash not erased
535 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
537 ulong cp, wp, data;
538 int i, l, rc;
540 wp = (addr & ~3); /* get lower word aligned address */
543 * handle unaligned start bytes
545 if ((l = addr - wp) != 0) {
546 data = 0;
547 for (i=0, cp=wp; i<l; ++i, ++cp) {
548 data = (data << 8) | (*(uchar *)cp);
550 for (; i<4 && cnt>0; ++i) {
551 data = (data << 8) | *src++;
552 --cnt;
553 ++cp;
555 for (; cnt==0 && i<4; ++i, ++cp) {
556 data = (data << 8) | (*(uchar *)cp);
559 if ((rc = write_word(info, wp, data)) != 0) {
560 return (rc);
562 wp += 4;
566 * handle word aligned part
568 while (cnt >= 4) {
569 data = 0;
570 for (i=0; i<4; ++i) {
571 data = (data << 8) | *src++;
573 if ((rc = write_word(info, wp, data)) != 0) {
574 return (rc);
576 wp += 4;
577 cnt -= 4;
580 if (cnt == 0) {
581 return (0);
585 * handle unaligned tail bytes
587 data = 0;
588 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
589 data = (data << 8) | *src++;
590 --cnt;
592 for (; i<4; ++i, ++cp) {
593 data = (data << 8) | (*(uchar *)cp);
596 return (write_word(info, wp, data));
598 /*-----------------------------------------------------------------------
599 * Write a word to Flash, returns:
600 * 0 - OK
601 * 1 - write timeout
602 * 2 - Flash not erased
604 static int write_word (flash_info_t *info, ulong dest, ulong data)
606 vu_long *addr = (vu_long*)(info->start[0]);
607 ulong start;
608 int flag;
610 /* Check if Flash is (sufficiently) erased */
611 if ((*((vu_long *)dest) & data) != data) {
612 return (2);
614 /* Disable interrupts which might cause a timeout here */
615 flag = disable_interrupts();
617 #if 0
618 addr[0x0555] = 0x00AA00AA;
619 addr[0x02AA] = 0x00550055;
620 addr[0x0555] = 0x00A000A0;
621 #else
622 addr[0x0555] = 0xAAAAAAAA;
623 addr[0x02AA] = 0x55555555;
624 addr[0x0555] = 0xA0A0A0A0;
625 #endif
627 *((vu_long *)dest) = data;
629 /* re-enable interrupts if necessary */
630 if (flag)
631 enable_interrupts();
633 /* data polling for D7 */
634 start = get_timer (0);
635 #if 0
636 while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080))
637 #else
638 while ((*((vu_long *)dest) & 0x80808080) != (data & 0x80808080))
639 #endif
641 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
642 return (1);
645 return (0);
648 /*-----------------------------------------------------------------------