2 * (C) Copyright 2000-2002
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,
28 #include <environment.h>
31 #define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
34 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
36 /*-----------------------------------------------------------------------
39 static 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 /*-----------------------------------------------------------------------
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;
52 /* Init: no FLASHes known */
53 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
54 flash_info[i].flash_id = FLASH_UNKNOWN;
57 /* Static FLASH Bank configuration here - FIXME XXX */
59 debug ("\n## Get flash bank 1 size @ 0x%08x\n",FLASH_BASE0_PRELIM);
61 size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
63 debug ("## Get flash bank 2 size @ 0x%08x\n",FLASH_BASE1_PRELIM);
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);
70 size_b1 = flash_get_size((vu_long *)FLASH_BASE1_PRELIM, &flash_info[1]);
72 debug ("## Prelim. Flash bank sizes: %08lx + 0x%08lx\n",size_b0,size_b1);
74 if (size_b1 > size_b0) {
76 "Bank 1 (0x%08lx = %ld MB) > Bank 0 (0x%08lx = %ld MB)\n",
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;
89 debug ("## Before remap: "
90 "BR0: 0x%08x OR0: 0x%08x "
91 "BR1: 0x%08x OR1: 0x%08x\n",
92 memctl->memc_br0, memctl->memc_or0,
93 memctl->memc_br1, memctl->memc_or1);
95 /* Remap FLASH according to real size */
96 memctl->memc_or0 = CFG_OR_TIMING_FLASH | (-size_b0 & OR_AM_MSK);
97 memctl->memc_br0 = (CFG_FLASH_BASE & BR_BA_MSK) | BR_MS_GPCM | BR_V;
99 debug ("## BR0: 0x%08x OR0: 0x%08x\n",
100 memctl->memc_br0, memctl->memc_or0);
102 /* Re-do sizing to get full correct info */
103 size_b0 = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
105 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
106 /* monitor protection ON by default */
107 debug ("Protect monitor: %08lx ... %08lx\n",
108 (ulong)CFG_MONITOR_BASE,
109 (ulong)CFG_MONITOR_BASE + monitor_flash_len - 1);
111 flash_protect(FLAG_PROTECT_SET,
113 CFG_MONITOR_BASE + monitor_flash_len - 1,
117 #ifdef CFG_ENV_IS_IN_FLASH
118 /* ENV protection ON by default */
119 # ifdef CFG_ENV_ADDR_REDUND
120 debug ("Protect primary environment: %08lx ... %08lx\n",
122 (ulong)CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1);
124 debug ("Protect environment: %08lx ... %08lx\n",
126 (ulong)CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1);
129 flash_protect(FLAG_PROTECT_SET,
131 CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1,
135 #ifdef CFG_ENV_ADDR_REDUND
136 debug ("Protect redundand environment: %08lx ... %08lx\n",
137 (ulong)CFG_ENV_ADDR_REDUND,
138 (ulong)CFG_ENV_ADDR_REDUND + CFG_ENV_SECT_SIZE - 1);
140 flash_protect(FLAG_PROTECT_SET,
142 CFG_ENV_ADDR_REDUND + CFG_ENV_SECT_SIZE - 1,
147 memctl->memc_or1 = CFG_OR_TIMING_FLASH | (-size_b1 & 0xFFFF8000);
148 memctl->memc_br1 = ((CFG_FLASH_BASE + size_b0) & BR_BA_MSK) |
151 debug ("## BR1: 0x%08x OR1: 0x%08x\n",
152 memctl->memc_br1, memctl->memc_or1);
154 /* Re-do sizing to get full correct info */
155 size_b1 = flash_get_size((vu_long *)(CFG_FLASH_BASE + size_b0),
158 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
159 /* monitor protection ON by default */
160 flash_protect(FLAG_PROTECT_SET,
162 CFG_MONITOR_BASE+monitor_flash_len-1,
166 #ifdef CFG_ENV_IS_IN_FLASH
167 /* ENV protection ON by default */
168 flash_protect(FLAG_PROTECT_SET,
170 CFG_ENV_ADDR+CFG_ENV_SIZE-1,
174 memctl->memc_br1 = 0; /* invalidate bank */
176 flash_info[1].flash_id = FLASH_UNKNOWN;
177 flash_info[1].sector_count = -1;
178 flash_info[1].size = 0;
180 debug ("## DISABLE BR1: 0x%08x OR1: 0x%08x\n",
181 memctl->memc_br1, memctl->memc_or1);
184 debug ("## Final Flash bank sizes: %08lx + 0x%08lx\n",size_b0,size_b1);
186 flash_info[0].size = size_b0;
187 flash_info[1].size = size_b1;
189 return (size_b0 + size_b1);
192 /*-----------------------------------------------------------------------
194 void flash_print_info (flash_info_t *info)
198 if (info->flash_id == FLASH_UNKNOWN) {
199 printf ("missing or unknown FLASH type\n");
203 switch (info->flash_id & FLASH_VENDMASK) {
204 case FLASH_MAN_AMD: printf ("AMD "); break;
205 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
206 default: printf ("Unknown Vendor "); break;
209 switch (info->flash_id & FLASH_TYPEMASK) {
210 #ifdef CONFIG_TQM8xxM /* mirror bit flash */
211 case FLASH_AMLV128U: printf ("AM29LV128ML (128Mbit, uniform sector size)\n");
213 case FLASH_AMLV320U: printf ("AM29LV320ML (32Mbit, uniform sector size)\n");
215 case FLASH_AMLV640U: printf ("AM29LV640ML (64Mbit, uniform sector size)\n");
217 # else /* ! TQM8xxM */
218 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
220 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
222 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
224 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
226 case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
228 case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
231 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
233 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
235 default: printf ("Unknown Chip Type\n");
239 printf (" Size: %ld MB in %d Sectors\n",
240 info->size >> 20, info->sector_count);
242 printf (" Sector Start Addresses:");
243 for (i=0; i<info->sector_count; ++i) {
248 info->protect[i] ? " (RO)" : " "
255 /*-----------------------------------------------------------------------
259 /*-----------------------------------------------------------------------
263 * The following code cannot be run from FLASH!
266 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
270 ulong base = (ulong)addr;
272 /* Write auto select command: read Manufacturer ID */
273 addr[0x0555] = 0x00AA00AA;
274 addr[0x02AA] = 0x00550055;
275 addr[0x0555] = 0x00900090;
279 debug ("Manuf. ID @ 0x%08lx: 0x%08lx\n", (ulong)addr, value);
283 info->flash_id = FLASH_MAN_AMD;
286 info->flash_id = FLASH_MAN_FUJ;
289 info->flash_id = FLASH_UNKNOWN;
290 info->sector_count = 0;
292 return (0); /* no or unknown flash */
295 value = addr[1]; /* device ID */
297 debug ("Device ID @ 0x%08lx: 0x%08lx\n", (ulong)(&addr[1]), value);
300 #ifdef CONFIG_TQM8xxM /* mirror bit flash */
302 /* Special case for AMLV320MH/L */
303 if ((addr[14] & 0x00ff00ff) == 0x001d001d &&
304 (addr[15] & 0x00ff00ff) == 0x00000000) {
305 info->flash_id += FLASH_AMLV320U;
306 info->sector_count = 64;
307 info->size = 0x00800000; /* => 8 MB */
311 case AMD_ID_LV128U_2:
312 if (addr[15] != AMD_ID_LV128U_3) {
313 info->flash_id = FLASH_UNKNOWN;
316 info->flash_id += FLASH_AMLV128U;
317 info->sector_count = 256;
318 info->size = 0x02000000;
320 break; /* => 32 MB */
321 case AMD_ID_LV640U_2:
322 if (addr[15] != AMD_ID_LV640U_3) {
323 info->flash_id = FLASH_UNKNOWN;
326 info->flash_id += FLASH_AMLV640U;
327 info->sector_count = 128;
328 info->size = 0x01000000;
330 break; /* => 16 MB */
332 info->flash_id = FLASH_UNKNOWN;
336 # else /* ! TQM8xxM */
338 info->flash_id += FLASH_AM400T;
339 info->sector_count = 11;
340 info->size = 0x00100000;
344 info->flash_id += FLASH_AM400B;
345 info->sector_count = 11;
346 info->size = 0x00100000;
350 info->flash_id += FLASH_AM800T;
351 info->sector_count = 19;
352 info->size = 0x00200000;
356 info->flash_id += FLASH_AM800B;
357 info->sector_count = 19;
358 info->size = 0x00200000;
362 info->flash_id += FLASH_AM320T;
363 info->sector_count = 71;
364 info->size = 0x00800000;
368 info->flash_id += FLASH_AM320B;
369 info->sector_count = 71;
370 info->size = 0x00800000;
375 info->flash_id += FLASH_AM160T;
376 info->sector_count = 35;
377 info->size = 0x00400000;
381 info->flash_id += FLASH_AM160B;
382 info->sector_count = 35;
383 info->size = 0x00400000;
387 info->flash_id = FLASH_UNKNOWN;
388 return (0); /* => no or unknown flash */
391 /* set up sector start address table */
393 #ifdef CONFIG_TQM8xxM /* mirror bit flash */
395 switch (info->flash_id & FLASH_TYPEMASK) {
396 /* only known types here - no default */
400 for (i = 0; i < info->sector_count; i++) {
401 info->start[i] = base;
407 # else /* ! TQM8xxM */
410 /* set sector offsets for bottom boot block type */
411 info->start[0] = base + 0x00000000;
412 info->start[1] = base + 0x00008000;
413 info->start[2] = base + 0x0000C000;
414 info->start[3] = base + 0x00010000;
415 for (i = 4; i < info->sector_count; i++) {
416 info->start[i] = base + (i * 0x00020000) - 0x00060000;
421 /* set sector offsets for top boot block type */
422 i = info->sector_count - 1;
423 info->start[i--] = base + info->size - 0x00008000;
424 info->start[i--] = base + info->size - 0x0000C000;
425 info->start[i--] = base + info->size - 0x00010000;
426 for (; i >= 0; i--) {
427 info->start[i] = base + i * 0x00020000;
431 for (i = 0; i < info->sector_count; i++) {
432 info->start[i] = base;
434 * The first 8 sectors are 8 kB,
435 * all the other ones are 64 kB
443 for (i = 0; i < info->sector_count; i++) {
444 info->start[i] = base;
446 * The last 8 sectors are 8 kB,
447 * all the other ones are 64 kB
449 base += (i < (info->sector_count - 8))
456 /* set sector offsets for bottom boot block type */
457 info->start[0] = base + 0x00000000;
458 info->start[1] = base + 0x00008000;
459 info->start[2] = base + 0x0000C000;
460 info->start[3] = base + 0x00010000;
461 for (i = 4; i < info->sector_count; i++) {
462 info->start[i] = base + (i * 0x00020000) - 0x00060000;
466 /* set sector offsets for top boot block type */
467 i = info->sector_count - 1;
468 info->start[i--] = base + info->size - 0x00008000;
469 info->start[i--] = base + info->size - 0x0000C000;
470 info->start[i--] = base + info->size - 0x00010000;
471 for (; i >= 0; i--) {
472 info->start[i] = base + i * 0x00020000;
480 /* check for protected sectors */
481 for (i = 0; i < info->sector_count; i++) {
482 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
483 /* D0 = 1 if protected */
484 addr = (volatile unsigned long *)(info->start[i]);
485 info->protect[i] = addr[2] & 1;
489 * Prevent writes to uninitialized FLASH.
491 if (info->flash_id != FLASH_UNKNOWN) {
492 addr = (volatile unsigned long *)info->start[0];
494 *addr = 0x00F000F0; /* reset bank */
501 /*-----------------------------------------------------------------------
504 int flash_erase (flash_info_t *info, int s_first, int s_last)
506 vu_long *addr = (vu_long*)(info->start[0]);
507 int flag, prot, sect, l_sect;
508 ulong start, now, last;
510 debug ("flash_erase: first: %d last: %d\n", s_first, s_last);
512 if ((s_first < 0) || (s_first > s_last)) {
513 if (info->flash_id == FLASH_UNKNOWN) {
514 printf ("- missing\n");
516 printf ("- no sectors to erase\n");
521 if ((info->flash_id == FLASH_UNKNOWN) ||
522 (info->flash_id > FLASH_AMD_COMP)) {
523 printf ("Can't erase unknown flash type %08lx - aborted\n",
529 for (sect=s_first; sect<=s_last; ++sect) {
530 if (info->protect[sect]) {
536 printf ("- Warning: %d protected sectors will not be erased!\n",
544 /* Disable interrupts which might cause a timeout here */
545 flag = disable_interrupts();
547 addr[0x0555] = 0x00AA00AA;
548 addr[0x02AA] = 0x00550055;
549 addr[0x0555] = 0x00800080;
550 addr[0x0555] = 0x00AA00AA;
551 addr[0x02AA] = 0x00550055;
553 /* Start erase on unprotected sectors */
554 for (sect = s_first; sect<=s_last; sect++) {
555 if (info->protect[sect] == 0) { /* not protected */
556 addr = (vu_long*)(info->start[sect]);
557 addr[0] = 0x00300030;
562 /* re-enable interrupts if necessary */
566 /* wait at least 80us - let's wait 1 ms */
570 * We wait for the last triggered sector
575 start = get_timer (0);
577 addr = (vu_long*)(info->start[l_sect]);
578 while ((addr[0] & 0x00800080) != 0x00800080) {
579 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
580 printf ("Timeout\n");
583 /* show that we're waiting */
584 if ((now - last) > 1000) { /* every second */
591 /* reset to read mode */
592 addr = (volatile unsigned long *)info->start[0];
593 addr[0] = 0x00F000F0; /* reset bank */
599 /*-----------------------------------------------------------------------
600 * Copy memory to flash, returns:
603 * 2 - Flash not erased
606 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
611 wp = (addr & ~3); /* get lower word aligned address */
614 * handle unaligned start bytes
616 if ((l = addr - wp) != 0) {
618 for (i=0, cp=wp; i<l; ++i, ++cp) {
619 data = (data << 8) | (*(uchar *)cp);
621 for (; i<4 && cnt>0; ++i) {
622 data = (data << 8) | *src++;
626 for (; cnt==0 && i<4; ++i, ++cp) {
627 data = (data << 8) | (*(uchar *)cp);
630 if ((rc = write_word(info, wp, data)) != 0) {
637 * handle word aligned part
641 for (i=0; i<4; ++i) {
642 data = (data << 8) | *src++;
644 if ((rc = write_word(info, wp, data)) != 0) {
656 * handle unaligned tail bytes
659 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
660 data = (data << 8) | *src++;
663 for (; i<4; ++i, ++cp) {
664 data = (data << 8) | (*(uchar *)cp);
667 return (write_word(info, wp, data));
670 /*-----------------------------------------------------------------------
671 * Write a word to Flash, returns:
674 * 2 - Flash not erased
676 static int write_word (flash_info_t *info, ulong dest, ulong data)
678 vu_long *addr = (vu_long*)(info->start[0]);
682 /* Check if Flash is (sufficiently) erased */
683 if ((*((vu_long *)dest) & data) != data) {
686 /* Disable interrupts which might cause a timeout here */
687 flag = disable_interrupts();
689 addr[0x0555] = 0x00AA00AA;
690 addr[0x02AA] = 0x00550055;
691 addr[0x0555] = 0x00A000A0;
693 *((vu_long *)dest) = data;
695 /* re-enable interrupts if necessary */
699 /* data polling for D7 */
700 start = get_timer (0);
701 while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
702 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
709 /*-----------------------------------------------------------------------