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,
30 #include <environment.h>
33 #define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
36 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
38 /*-----------------------------------------------------------------------
41 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
42 static int write_word (flash_info_t *info, ulong dest, ulong data);
44 /*-----------------------------------------------------------------------
47 unsigned long flash_init (void)
49 volatile immap_t *immap = (immap_t *)CFG_IMMR;
50 volatile memctl8xx_t *memctl = &immap->im_memctl;
51 unsigned long size_b0, size_b1;
54 /* Init: no FLASHes known */
55 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
56 flash_info[i].flash_id = FLASH_UNKNOWN;
59 /* Static FLASH Bank configuration here - FIXME XXX */
61 debug ("\n## Get flash bank 1 size @ 0x%08x\n",FLASH_BASE0_PRELIM);
63 size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
65 debug ("## Get flash bank 2 size @ 0x%08x\n",FLASH_BASE1_PRELIM);
67 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 size_b1 = flash_get_size((vu_long *)FLASH_BASE1_PRELIM, &flash_info[1]);
74 debug ("## Prelim. Flash bank sizes: %08lx + 0x%08lx\n",size_b0,size_b1);
76 if (size_b1 > size_b0) {
78 "Bank 1 (0x%08lx = %ld MB) > Bank 0 (0x%08lx = %ld MB)\n",
82 flash_info[0].flash_id = FLASH_UNKNOWN;
83 flash_info[1].flash_id = FLASH_UNKNOWN;
84 flash_info[0].sector_count = -1;
85 flash_info[1].sector_count = -1;
86 flash_info[0].size = 0;
87 flash_info[1].size = 0;
91 debug ("## Before remap: "
92 "BR0: 0x%08x OR0: 0x%08x "
93 "BR1: 0x%08x OR1: 0x%08x\n",
94 memctl->memc_br0, memctl->memc_or0,
95 memctl->memc_br1, memctl->memc_or1);
97 /* Remap FLASH according to real size */
98 memctl->memc_or0 = CFG_OR_TIMING_FLASH | (-size_b0 & OR_AM_MSK);
99 memctl->memc_br0 = (CFG_FLASH_BASE & BR_BA_MSK) | BR_MS_GPCM | BR_V;
101 debug ("## BR0: 0x%08x OR0: 0x%08x\n",
102 memctl->memc_br0, memctl->memc_or0);
104 /* Re-do sizing to get full correct info */
105 size_b0 = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
107 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
108 /* monitor protection ON by default */
109 debug ("Protect monitor: %08lx ... %08lx\n",
110 (ulong)CFG_MONITOR_BASE,
111 (ulong)CFG_MONITOR_BASE + monitor_flash_len - 1);
113 flash_protect(FLAG_PROTECT_SET,
115 CFG_MONITOR_BASE + monitor_flash_len - 1,
119 #ifdef CFG_ENV_IS_IN_FLASH
120 /* ENV protection ON by default */
121 # ifdef CFG_ENV_ADDR_REDUND
122 debug ("Protect primary environment: %08lx ... %08lx\n",
124 (ulong)CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1);
126 debug ("Protect environment: %08lx ... %08lx\n",
128 (ulong)CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1);
131 flash_protect(FLAG_PROTECT_SET,
133 CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1,
137 #ifdef CFG_ENV_ADDR_REDUND
138 debug ("Protect redundand environment: %08lx ... %08lx\n",
139 (ulong)CFG_ENV_ADDR_REDUND,
140 (ulong)CFG_ENV_ADDR_REDUND + CFG_ENV_SECT_SIZE - 1);
142 flash_protect(FLAG_PROTECT_SET,
144 CFG_ENV_ADDR_REDUND + CFG_ENV_SECT_SIZE - 1,
149 memctl->memc_or1 = CFG_OR_TIMING_FLASH | (-size_b1 & 0xFFFF8000);
150 memctl->memc_br1 = ((CFG_FLASH_BASE + size_b0) & BR_BA_MSK) |
153 debug ("## BR1: 0x%08x OR1: 0x%08x\n",
154 memctl->memc_br1, memctl->memc_or1);
156 /* Re-do sizing to get full correct info */
157 size_b1 = flash_get_size((vu_long *)(CFG_FLASH_BASE + size_b0),
160 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
161 /* monitor protection ON by default */
162 flash_protect(FLAG_PROTECT_SET,
164 CFG_MONITOR_BASE+monitor_flash_len-1,
168 #ifdef CFG_ENV_IS_IN_FLASH
169 /* ENV protection ON by default */
170 flash_protect(FLAG_PROTECT_SET,
172 CFG_ENV_ADDR+CFG_ENV_SIZE-1,
176 memctl->memc_br1 = 0; /* invalidate bank */
178 flash_info[1].flash_id = FLASH_UNKNOWN;
179 flash_info[1].sector_count = -1;
180 flash_info[1].size = 0;
182 debug ("## DISABLE BR1: 0x%08x OR1: 0x%08x\n",
183 memctl->memc_br1, memctl->memc_or1);
186 debug ("## Final Flash bank sizes: %08lx + 0x%08lx\n",size_b0,size_b1);
188 flash_info[0].size = size_b0;
189 flash_info[1].size = size_b1;
191 return (size_b0 + size_b1);
194 /*-----------------------------------------------------------------------
196 void flash_print_info (flash_info_t *info)
200 if (info->flash_id == FLASH_UNKNOWN) {
201 printf ("missing or unknown FLASH type\n");
205 switch (info->flash_id & FLASH_VENDMASK) {
206 case FLASH_MAN_AMD: printf ("AMD "); break;
207 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
208 default: printf ("Unknown Vendor "); break;
211 switch (info->flash_id & FLASH_TYPEMASK) {
212 #ifdef CONFIG_TQM8xxM /* mirror bit flash */
213 case FLASH_AMLV128U: printf ("AM29LV128ML (128Mbit, uniform sector size)\n");
215 case FLASH_AMLV320U: printf ("AM29LV320ML (32Mbit, uniform sector size)\n");
217 case FLASH_AMLV640U: printf ("AM29LV640ML (64Mbit, uniform sector size)\n");
219 case FLASH_AMLV320B: printf ("AM29LV320MB (32Mbit, bottom boot sect)\n");
221 # else /* ! TQM8xxM */
222 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
224 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
226 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
228 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
230 case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
232 case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
235 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
237 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
239 case FLASH_AMDL163B: printf ("AM29DL163B (16 Mbit, bottom boot sect)\n");
241 default: printf ("Unknown Chip Type\n");
245 printf (" Size: %ld MB in %d Sectors\n",
246 info->size >> 20, info->sector_count);
248 printf (" Sector Start Addresses:");
249 for (i=0; i<info->sector_count; ++i) {
254 info->protect[i] ? " (RO)" : " "
261 /*-----------------------------------------------------------------------
265 /*-----------------------------------------------------------------------
269 * The following code cannot be run from FLASH!
272 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
276 ulong base = (ulong)addr;
278 /* Write auto select command: read Manufacturer ID */
279 addr[0x0555] = 0x00AA00AA;
280 addr[0x02AA] = 0x00550055;
281 addr[0x0555] = 0x00900090;
285 debug ("Manuf. ID @ 0x%08lx: 0x%08lx\n", (ulong)addr, value);
289 debug ("Manufacturer: AMD\n");
290 info->flash_id = FLASH_MAN_AMD;
293 debug ("Manufacturer: FUJITSU\n");
294 info->flash_id = FLASH_MAN_FUJ;
297 debug ("Manufacturer: *** unknown ***\n");
298 info->flash_id = FLASH_UNKNOWN;
299 info->sector_count = 0;
301 return (0); /* no or unknown flash */
304 value = addr[1]; /* device ID */
306 debug ("Device ID @ 0x%08lx: 0x%08lx\n", (ulong)(&addr[1]), value);
309 #ifdef CONFIG_TQM8xxM /* mirror bit flash */
311 debug ("Mirror Bit flash: addr[14] = %08lX addr[15] = %08lX\n",
313 /* Special case for AMLV320MH/L */
314 if ((addr[14] & 0x00ff00ff) == 0x001d001d &&
315 (addr[15] & 0x00ff00ff) == 0x00000000) {
316 debug ("Chip: AMLV320MH/L\n");
317 info->flash_id += FLASH_AMLV320U;
318 info->sector_count = 64;
319 info->size = 0x00800000; /* => 8 MB */
323 case AMD_ID_LV128U_2:
324 if (addr[15] != AMD_ID_LV128U_3) {
325 debug ("Chip: AMLV128U -> unknown\n");
326 info->flash_id = FLASH_UNKNOWN;
328 debug ("Chip: AMLV128U\n");
329 info->flash_id += FLASH_AMLV128U;
330 info->sector_count = 256;
331 info->size = 0x02000000;
333 break; /* => 32 MB */
334 case AMD_ID_LV640U_2:
335 if (addr[15] != AMD_ID_LV640U_3) {
336 debug ("Chip: AMLV640U -> unknown\n");
337 info->flash_id = FLASH_UNKNOWN;
339 debug ("Chip: AMLV640U\n");
340 info->flash_id += FLASH_AMLV640U;
341 info->sector_count = 128;
342 info->size = 0x01000000;
344 break; /* => 16 MB */
345 case AMD_ID_LV320B_2:
346 if (addr[15] != AMD_ID_LV320B_3) {
347 debug ("Chip: AMLV320B -> unknown\n");
348 info->flash_id = FLASH_UNKNOWN;
350 debug ("Chip: AMLV320B\n");
351 info->flash_id += FLASH_AMLV320B;
352 info->sector_count = 71;
353 info->size = 0x00800000;
357 debug ("Chip: *** unknown ***\n");
358 info->flash_id = FLASH_UNKNOWN;
362 # else /* ! TQM8xxM */
364 info->flash_id += FLASH_AM400T;
365 info->sector_count = 11;
366 info->size = 0x00100000;
370 info->flash_id += FLASH_AM400B;
371 info->sector_count = 11;
372 info->size = 0x00100000;
376 info->flash_id += FLASH_AM800T;
377 info->sector_count = 19;
378 info->size = 0x00200000;
382 info->flash_id += FLASH_AM800B;
383 info->sector_count = 19;
384 info->size = 0x00200000;
388 info->flash_id += FLASH_AM320T;
389 info->sector_count = 71;
390 info->size = 0x00800000;
394 info->flash_id += FLASH_AM320B;
395 info->sector_count = 71;
396 info->size = 0x00800000;
401 info->flash_id += FLASH_AM160T;
402 info->sector_count = 35;
403 info->size = 0x00400000;
407 info->flash_id += FLASH_AM160B;
408 info->sector_count = 35;
409 info->size = 0x00400000;
413 info->flash_id += FLASH_AMDL163B;
414 info->sector_count = 39;
415 info->size = 0x00400000;
419 info->flash_id = FLASH_UNKNOWN;
420 return (0); /* => no or unknown flash */
423 /* set up sector start address table */
425 #ifdef CONFIG_TQM8xxM /* mirror bit flash */
427 switch (info->flash_id & FLASH_TYPEMASK) {
428 /* only known types here - no default */
432 for (i = 0; i < info->sector_count; i++) {
433 info->start[i] = base;
438 for (i = 0; i < info->sector_count; i++) {
439 info->start[i] = base;
441 * The first 8 sectors are 8 kB,
442 * all the other ones are 64 kB
451 # else /* ! TQM8xxM */
454 /* set sector offsets for bottom boot block type */
455 info->start[0] = base + 0x00000000;
456 info->start[1] = base + 0x00008000;
457 info->start[2] = base + 0x0000C000;
458 info->start[3] = base + 0x00010000;
459 for (i = 4; i < info->sector_count; i++) {
460 info->start[i] = base + (i * 0x00020000) - 0x00060000;
465 /* set sector offsets for top boot block type */
466 i = info->sector_count - 1;
467 info->start[i--] = base + info->size - 0x00008000;
468 info->start[i--] = base + info->size - 0x0000C000;
469 info->start[i--] = base + info->size - 0x00010000;
470 for (; i >= 0; i--) {
471 info->start[i] = base + i * 0x00020000;
475 for (i = 0; i < info->sector_count; i++) {
476 info->start[i] = base;
478 * The first 8 sectors are 8 kB,
479 * all the other ones are 64 kB
487 for (i = 0; i < info->sector_count; i++) {
488 info->start[i] = base;
490 * The last 8 sectors are 8 kB,
491 * all the other ones are 64 kB
493 base += (i < (info->sector_count - 8))
500 /* set sector offsets for bottom boot block type */
501 info->start[0] = base + 0x00000000;
502 info->start[1] = base + 0x00008000;
503 info->start[2] = base + 0x0000C000;
504 info->start[3] = base + 0x00010000;
505 for (i = 4; i < info->sector_count; i++) {
506 info->start[i] = base + (i * 0x00020000) - 0x00060000;
510 /* set sector offsets for top boot block type */
511 i = info->sector_count - 1;
512 info->start[i--] = base + info->size - 0x00008000;
513 info->start[i--] = base + info->size - 0x0000C000;
514 info->start[i--] = base + info->size - 0x00010000;
515 for (; i >= 0; i--) {
516 info->start[i] = base + i * 0x00020000;
520 for (i = 0; i < info->sector_count; i++) {
521 info->start[i] = base;
523 * The first 8 sectors are 8 kB,
524 * all the other ones are 64 kB
537 /* check for protected sectors */
538 for (i = 0; i < info->sector_count; i++) {
539 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
540 /* D0 = 1 if protected */
541 addr = (volatile unsigned long *)(info->start[i]);
542 info->protect[i] = addr[2] & 1;
547 * Prevent writes to uninitialized FLASH.
549 if (info->flash_id != FLASH_UNKNOWN) {
550 addr = (volatile unsigned long *)info->start[0];
552 *addr = 0x00F000F0; /* reset bank */
559 /*-----------------------------------------------------------------------
562 int flash_erase (flash_info_t *info, int s_first, int s_last)
564 vu_long *addr = (vu_long*)(info->start[0]);
565 int flag, prot, sect, l_sect;
566 ulong start, now, last;
568 debug ("flash_erase: first: %d last: %d\n", s_first, s_last);
570 if ((s_first < 0) || (s_first > s_last)) {
571 if (info->flash_id == FLASH_UNKNOWN) {
572 printf ("- missing\n");
574 printf ("- no sectors to erase\n");
579 if ((info->flash_id == FLASH_UNKNOWN) ||
580 (info->flash_id > FLASH_AMD_COMP)) {
581 printf ("Can't erase unknown flash type %08lx - aborted\n",
587 for (sect=s_first; sect<=s_last; ++sect) {
588 if (info->protect[sect]) {
594 printf ("- Warning: %d protected sectors will not be erased!\n",
602 /* Disable interrupts which might cause a timeout here */
603 flag = disable_interrupts();
605 addr[0x0555] = 0x00AA00AA;
606 addr[0x02AA] = 0x00550055;
607 addr[0x0555] = 0x00800080;
608 addr[0x0555] = 0x00AA00AA;
609 addr[0x02AA] = 0x00550055;
611 /* Start erase on unprotected sectors */
612 for (sect = s_first; sect<=s_last; sect++) {
613 if (info->protect[sect] == 0) { /* not protected */
614 addr = (vu_long*)(info->start[sect]);
615 addr[0] = 0x00300030;
620 /* re-enable interrupts if necessary */
624 /* wait at least 80us - let's wait 1 ms */
628 * We wait for the last triggered sector
633 start = get_timer (0);
635 addr = (vu_long*)(info->start[l_sect]);
636 while ((addr[0] & 0x00800080) != 0x00800080) {
637 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
638 printf ("Timeout\n");
641 /* show that we're waiting */
642 if ((now - last) > 1000) { /* every second */
649 /* reset to read mode */
650 addr = (volatile unsigned long *)info->start[0];
651 addr[0] = 0x00F000F0; /* reset bank */
657 /*-----------------------------------------------------------------------
658 * Copy memory to flash, returns:
661 * 2 - Flash not erased
664 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
669 wp = (addr & ~3); /* get lower word aligned address */
672 * handle unaligned start bytes
674 if ((l = addr - wp) != 0) {
676 for (i=0, cp=wp; i<l; ++i, ++cp) {
677 data = (data << 8) | (*(uchar *)cp);
679 for (; i<4 && cnt>0; ++i) {
680 data = (data << 8) | *src++;
684 for (; cnt==0 && i<4; ++i, ++cp) {
685 data = (data << 8) | (*(uchar *)cp);
688 if ((rc = write_word(info, wp, data)) != 0) {
695 * handle word aligned part
699 for (i=0; i<4; ++i) {
700 data = (data << 8) | *src++;
702 if ((rc = write_word(info, wp, data)) != 0) {
714 * handle unaligned tail bytes
717 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
718 data = (data << 8) | *src++;
721 for (; i<4; ++i, ++cp) {
722 data = (data << 8) | (*(uchar *)cp);
725 return (write_word(info, wp, data));
728 /*-----------------------------------------------------------------------
729 * Write a word to Flash, returns:
732 * 2 - Flash not erased
734 static int write_word (flash_info_t *info, ulong dest, ulong data)
736 vu_long *addr = (vu_long*)(info->start[0]);
740 /* Check if Flash is (sufficiently) erased */
741 if ((*((vu_long *)dest) & data) != data) {
744 /* Disable interrupts which might cause a timeout here */
745 flag = disable_interrupts();
747 addr[0x0555] = 0x00AA00AA;
748 addr[0x02AA] = 0x00550055;
749 addr[0x0555] = 0x00A000A0;
751 *((vu_long *)dest) = data;
753 /* re-enable interrupts if necessary */
757 /* data polling for D7 */
758 start = get_timer (0);
759 while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
760 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
767 /*-----------------------------------------------------------------------