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 debug ("Protect %senvironment: %08lx ... %08lx\n",
120 # ifdef CFG_ENV_ADDR_REDUND
126 (ulong)CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1);
128 flash_protect(FLAG_PROTECT_SET,
130 CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1,
134 #ifdef CFG_ENV_ADDR_REDUND
135 debug ("Protect redundand environment: %08lx ... %08lx\n",
136 (ulong)CFG_ENV_ADDR_REDUND,
137 (ulong)CFG_ENV_ADDR_REDUND + CFG_ENV_SECT_SIZE - 1);
139 flash_protect(FLAG_PROTECT_SET,
141 CFG_ENV_ADDR_REDUND + CFG_ENV_SECT_SIZE - 1,
146 memctl->memc_or1 = CFG_OR_TIMING_FLASH | (-size_b1 & 0xFFFF8000);
147 memctl->memc_br1 = ((CFG_FLASH_BASE + size_b0) & BR_BA_MSK) |
150 debug ("## BR1: 0x%08x OR1: 0x%08x\n",
151 memctl->memc_br1, memctl->memc_or1);
153 /* Re-do sizing to get full correct info */
154 size_b1 = flash_get_size((vu_long *)(CFG_FLASH_BASE + size_b0),
157 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
158 /* monitor protection ON by default */
159 flash_protect(FLAG_PROTECT_SET,
161 CFG_MONITOR_BASE+monitor_flash_len-1,
165 #ifdef CFG_ENV_IS_IN_FLASH
166 /* ENV protection ON by default */
167 flash_protect(FLAG_PROTECT_SET,
169 CFG_ENV_ADDR+CFG_ENV_SIZE-1,
173 memctl->memc_br1 = 0; /* invalidate bank */
175 flash_info[1].flash_id = FLASH_UNKNOWN;
176 flash_info[1].sector_count = -1;
177 flash_info[1].size = 0;
179 debug ("## DISABLE BR1: 0x%08x OR1: 0x%08x\n",
180 memctl->memc_br1, memctl->memc_or1);
183 debug ("## Final Flash bank sizes: %08lx + 0x%08lx\n",size_b0,size_b1);
185 flash_info[0].size = size_b0;
186 flash_info[1].size = size_b1;
188 return (size_b0 + size_b1);
191 /*-----------------------------------------------------------------------
193 void flash_print_info (flash_info_t *info)
197 if (info->flash_id == FLASH_UNKNOWN) {
198 printf ("missing or unknown FLASH type\n");
202 switch (info->flash_id & FLASH_VENDMASK) {
203 case FLASH_MAN_AMD: printf ("AMD "); break;
204 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
205 default: printf ("Unknown Vendor "); break;
208 switch (info->flash_id & FLASH_TYPEMASK) {
209 #ifdef CONFIG_TQM8xxM /* mirror bit flash */
210 case FLASH_AMLV128U: printf ("AM29LV128ML (128Mbit, uniform sector size)\n");
212 # else /* ! TQM8xxM */
213 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
215 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit, top boot sector)\n");
217 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
219 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit, top boot sector)\n");
221 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
223 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit, top boot sector)\n");
225 case FLASH_AM320B: printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
227 case FLASH_AM320T: printf ("AM29LV320T (32 Mbit, top boot sector)\n");
230 default: printf ("Unknown Chip Type\n");
234 printf (" Size: %ld MB in %d Sectors\n",
235 info->size >> 20, info->sector_count);
237 printf (" Sector Start Addresses:");
238 for (i=0; i<info->sector_count; ++i) {
243 info->protect[i] ? " (RO)" : " "
250 /*-----------------------------------------------------------------------
254 /*-----------------------------------------------------------------------
258 * The following code cannot be run from FLASH!
261 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
265 ulong base = (ulong)addr;
267 /* Write auto select command: read Manufacturer ID */
268 addr[0x0555] = 0x00AA00AA;
269 addr[0x02AA] = 0x00550055;
270 addr[0x0555] = 0x00900090;
274 debug ("Manuf. ID @ 0x%08lx: 0x%08lx\n", (ulong)addr, value);
278 info->flash_id = FLASH_MAN_AMD;
281 info->flash_id = FLASH_MAN_FUJ;
284 info->flash_id = FLASH_UNKNOWN;
285 info->sector_count = 0;
287 return (0); /* no or unknown flash */
290 value = addr[1]; /* device ID */
292 debug ("Device ID @ 0x%08lx: 0x%08lx\n", (ulong)(&addr[1]), value);
295 #ifdef CONFIG_TQM8xxM /* mirror bit flash */
298 case AMD_ID_LV128U_2:
299 if (addr[15] != AMD_ID_LV128U_3) {
300 info->flash_id = FLASH_UNKNOWN;
303 info->flash_id += FLASH_AMLV128U;
304 info->sector_count = 256;
305 info->size = 0x02000000;
307 break; /* => 32 MB */
309 info->flash_id = FLASH_UNKNOWN;
313 # else /* ! TQM8xxM */
315 info->flash_id += FLASH_AM400T;
316 info->sector_count = 11;
317 info->size = 0x00100000;
321 info->flash_id += FLASH_AM400B;
322 info->sector_count = 11;
323 info->size = 0x00100000;
327 info->flash_id += FLASH_AM800T;
328 info->sector_count = 19;
329 info->size = 0x00200000;
333 info->flash_id += FLASH_AM800B;
334 info->sector_count = 19;
335 info->size = 0x00200000;
339 info->flash_id += FLASH_AM160T;
340 info->sector_count = 35;
341 info->size = 0x00400000;
345 info->flash_id += FLASH_AM160B;
346 info->sector_count = 35;
347 info->size = 0x00400000;
351 info->flash_id += FLASH_AM320T;
352 info->sector_count = 71;
353 info->size = 0x00800000;
357 info->flash_id += FLASH_AM320B;
358 info->sector_count = 71;
359 info->size = 0x00800000;
363 info->flash_id = FLASH_UNKNOWN;
364 return (0); /* => no or unknown flash */
367 /* set up sector start address table */
369 #ifdef CONFIG_TQM8xxM /* mirror bit flash */
371 switch (info->flash_id & FLASH_TYPEMASK) {
372 /* only known types here - no default */
374 for (i = 0; i < info->sector_count; i++) {
375 info->start[i] = base;
381 # else /* ! TQM8xxM */
385 /* set sector offsets for bottom boot block type */
386 info->start[0] = base + 0x00000000;
387 info->start[1] = base + 0x00008000;
388 info->start[2] = base + 0x0000C000;
389 info->start[3] = base + 0x00010000;
390 for (i = 4; i < info->sector_count; i++) {
391 info->start[i] = base + (i * 0x00020000) - 0x00060000;
397 /* set sector offsets for top boot block type */
398 i = info->sector_count - 1;
399 info->start[i--] = base + info->size - 0x00008000;
400 info->start[i--] = base + info->size - 0x0000C000;
401 info->start[i--] = base + info->size - 0x00010000;
402 for (; i >= 0; i--) {
403 info->start[i] = base + i * 0x00020000;
407 for (i = 0; i < info->sector_count; i++) {
408 info->start[i] = base;
410 * The first 8 sectors are 8 kB,
411 * all the other ones are 64 kB
419 for (i = 0; i < info->sector_count; i++) {
420 info->start[i] = base;
422 * The last 8 sectors are 8 kB,
423 * all the other ones are 64 kB
425 base += (i < (info->sector_count - 8))
436 /* check for protected sectors */
437 for (i = 0; i < info->sector_count; i++) {
438 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
439 /* D0 = 1 if protected */
440 addr = (volatile unsigned long *)(info->start[i]);
441 info->protect[i] = addr[2] & 1;
445 * Prevent writes to uninitialized FLASH.
447 if (info->flash_id != FLASH_UNKNOWN) {
448 addr = (volatile unsigned long *)info->start[0];
450 *addr = 0x00F000F0; /* reset bank */
457 /*-----------------------------------------------------------------------
460 int flash_erase (flash_info_t *info, int s_first, int s_last)
462 vu_long *addr = (vu_long*)(info->start[0]);
463 int flag, prot, sect, l_sect;
464 ulong start, now, last;
466 debug ("flash_erase: first: %d last: %d\n", s_first, s_last);
468 if ((s_first < 0) || (s_first > s_last)) {
469 if (info->flash_id == FLASH_UNKNOWN) {
470 printf ("- missing\n");
472 printf ("- no sectors to erase\n");
477 if ((info->flash_id == FLASH_UNKNOWN) ||
478 (info->flash_id > FLASH_AMD_COMP)) {
479 printf ("Can't erase unknown flash type %08lx - aborted\n",
485 for (sect=s_first; sect<=s_last; ++sect) {
486 if (info->protect[sect]) {
492 printf ("- Warning: %d protected sectors will not be erased!\n",
500 /* Disable interrupts which might cause a timeout here */
501 flag = disable_interrupts();
503 addr[0x0555] = 0x00AA00AA;
504 addr[0x02AA] = 0x00550055;
505 addr[0x0555] = 0x00800080;
506 addr[0x0555] = 0x00AA00AA;
507 addr[0x02AA] = 0x00550055;
509 /* Start erase on unprotected sectors */
510 for (sect = s_first; sect<=s_last; sect++) {
511 if (info->protect[sect] == 0) { /* not protected */
512 addr = (vu_long*)(info->start[sect]);
513 addr[0] = 0x00300030;
518 /* re-enable interrupts if necessary */
522 /* wait at least 80us - let's wait 1 ms */
526 * We wait for the last triggered sector
531 start = get_timer (0);
533 addr = (vu_long*)(info->start[l_sect]);
534 while ((addr[0] & 0x00800080) != 0x00800080) {
535 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
536 printf ("Timeout\n");
539 /* show that we're waiting */
540 if ((now - last) > 1000) { /* every second */
547 /* reset to read mode */
548 addr = (volatile unsigned long *)info->start[0];
549 addr[0] = 0x00F000F0; /* reset bank */
555 /*-----------------------------------------------------------------------
556 * Copy memory to flash, returns:
559 * 2 - Flash not erased
562 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
567 wp = (addr & ~3); /* get lower word aligned address */
570 * handle unaligned start bytes
572 if ((l = addr - wp) != 0) {
574 for (i=0, cp=wp; i<l; ++i, ++cp) {
575 data = (data << 8) | (*(uchar *)cp);
577 for (; i<4 && cnt>0; ++i) {
578 data = (data << 8) | *src++;
582 for (; cnt==0 && i<4; ++i, ++cp) {
583 data = (data << 8) | (*(uchar *)cp);
586 if ((rc = write_word(info, wp, data)) != 0) {
593 * handle word aligned part
597 for (i=0; i<4; ++i) {
598 data = (data << 8) | *src++;
600 if ((rc = write_word(info, wp, data)) != 0) {
612 * handle unaligned tail bytes
615 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
616 data = (data << 8) | *src++;
619 for (; i<4; ++i, ++cp) {
620 data = (data << 8) | (*(uchar *)cp);
623 return (write_word(info, wp, data));
626 /*-----------------------------------------------------------------------
627 * Write a word to Flash, returns:
630 * 2 - Flash not erased
632 static int write_word (flash_info_t *info, ulong dest, ulong data)
634 vu_long *addr = (vu_long*)(info->start[0]);
638 /* Check if Flash is (sufficiently) erased */
639 if ((*((vu_long *)dest) & data) != data) {
642 /* Disable interrupts which might cause a timeout here */
643 flag = disable_interrupts();
645 addr[0x0555] = 0x00AA00AA;
646 addr[0x02AA] = 0x00550055;
647 addr[0x0555] = 0x00A000A0;
649 *((vu_long *)dest) = data;
651 /* re-enable interrupts if necessary */
655 /* data polling for D7 */
656 start = get_timer (0);
657 while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
658 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
665 /*-----------------------------------------------------------------------