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,
29 #if defined(CONFIG_ENV_IS_IN_FLASH)
31 # define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
34 # define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
36 # ifndef CFG_ENV_SECT_SIZE
37 # define CFG_ENV_SECT_SIZE CFG_ENV_SIZE
41 /*---------------------------------------------------------------------*/
43 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
45 /*-----------------------------------------------------------------------
48 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
49 static int write_data (flash_info_t *info, ulong dest, ulong data);
50 #ifdef CFG_FLASH_USE_BUFFER_WRITE
51 static int write_data_buf (flash_info_t * info, ulong dest, uchar * cp, int len);
53 static void flash_get_offsets (ulong base, flash_info_t *info);
55 /*-----------------------------------------------------------------------
58 unsigned long flash_init (void)
60 volatile immap_t *immap = (immap_t *)CFG_IMMR;
61 volatile memctl8xx_t *memctl = &immap->im_memctl;
62 unsigned long size_b0, size_b1;
65 /* Init: no FLASHes known */
66 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
67 flash_info[i].flash_id = FLASH_UNKNOWN;
70 /* Static FLASH Bank configuration here - FIXME XXX */
72 debug ("\n## Get flash bank 1 size @ 0x%08x\n",FLASH_BASE0_PRELIM);
74 size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
76 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
77 printf ("## Unknown FLASH on Bank 0: "
78 "ID 0x%lx, Size = 0x%08lx = %ld MB\n",
79 flash_info[0].flash_id,
80 size_b0, size_b0<<20);
83 debug ("## Get flash bank 2 size @ 0x%08x\n",FLASH_BASE1_PRELIM);
85 size_b1 = flash_get_size((vu_long *)FLASH_BASE1_PRELIM, &flash_info[1]);
87 debug ("## Prelim. Flash bank sizes: %08lx + 0x%08lx\n",size_b0,size_b1);
89 if (size_b1 > size_b0) {
91 "Bank 1 (0x%08lx = %ld MB) > Bank 0 (0x%08lx = %ld MB)\n",
95 flash_info[0].flash_id = FLASH_UNKNOWN;
96 flash_info[1].flash_id = FLASH_UNKNOWN;
97 flash_info[0].sector_count = -1;
98 flash_info[1].sector_count = -1;
99 flash_info[0].size = 0;
100 flash_info[1].size = 0;
104 debug ("## Before remap: "
105 "BR0: 0x%08x OR0: 0x%08x "
106 "BR1: 0x%08x OR1: 0x%08x\n",
107 memctl->memc_br0, memctl->memc_or0,
108 memctl->memc_br1, memctl->memc_or1);
110 /* Remap FLASH according to real size */
111 memctl->memc_or0 = (-size_b0 & 0xFFFF8000) | CFG_OR_TIMING_FLASH |
112 OR_CSNT_SAM | OR_ACS_DIV1;
113 memctl->memc_br0 = (CFG_FLASH_BASE & BR_BA_MSK) | BR_PS_32 | BR_V;
115 debug ("## BR0: 0x%08x OR0: 0x%08x\n",
116 memctl->memc_br0, memctl->memc_or0);
118 /* Re-do sizing to get full correct info */
119 size_b0 = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
121 flash_get_offsets (CFG_FLASH_BASE, &flash_info[0]);
123 flash_info[0].size = size_b0;
125 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
126 /* monitor protection ON by default */
127 flash_protect(FLAG_PROTECT_SET,
129 CFG_MONITOR_BASE+monitor_flash_len-1,
133 #ifdef CONFIG_ENV_IS_IN_FLASH
134 /* ENV protection ON by default */
135 flash_protect(FLAG_PROTECT_SET,
137 CFG_ENV_ADDR+CFG_ENV_SECT_SIZE-1,
142 memctl->memc_or1 = (-size_b1 & 0xFFFF8000) | CFG_OR_TIMING_FLASH |
143 OR_CSNT_SAM | OR_ACS_DIV1;
144 memctl->memc_br1 = ((CFG_FLASH_BASE + size_b0) & BR_BA_MSK) |
147 debug ("## BR1: 0x%08x OR1: 0x%08x\n",
148 memctl->memc_br1, memctl->memc_or1);
150 /* Re-do sizing to get full correct info */
151 size_b1 = flash_get_size((vu_long *)(CFG_FLASH_BASE + size_b0),
154 flash_info[1].size = size_b1;
156 flash_get_offsets (CFG_FLASH_BASE + size_b0, &flash_info[1]);
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 CONFIG_ENV_IS_IN_FLASH
167 /* ENV protection ON by default */
168 flash_protect(FLAG_PROTECT_SET,
170 CFG_ENV_ADDR+CFG_ENV_SECT_SIZE-1,
174 memctl->memc_br1 = 0; /* invalidate bank */
175 memctl->memc_or1 = 0; /* invalidate bank */
177 debug ("## DISABLE BR1: 0x%08x OR1: 0x%08x\n",
178 memctl->memc_br1, memctl->memc_or1);
180 flash_info[1].flash_id = FLASH_UNKNOWN;
181 flash_info[1].sector_count = -1;
182 flash_info[1].size = 0;
185 debug ("## Final Flash bank sizes: %08lx + 0x%08lx\n",size_b0,size_b1);
187 return (size_b0 + size_b1);
190 /*-----------------------------------------------------------------------
192 static void flash_get_offsets (ulong base, flash_info_t *info)
196 if (info->flash_id == FLASH_UNKNOWN) {
200 switch (info->flash_id & FLASH_VENDMASK) {
201 case FLASH_MAN_INTEL:
202 for (i = 0; i < info->sector_count; i++) {
203 info->start[i] = base;
204 base += 0x00020000 * 2; /* 128k * 2 chips per bank */
209 printf ("Don't know sector ofsets for flash type 0x%lx\n",
215 /*-----------------------------------------------------------------------
217 void flash_print_info (flash_info_t *info)
221 if (info->flash_id == FLASH_UNKNOWN) {
222 printf ("missing or unknown FLASH type\n");
226 switch (info->flash_id & FLASH_VENDMASK) {
227 case FLASH_MAN_AMD: printf ("AMD "); break;
228 case FLASH_MAN_FUJ: printf ("Fujitsu "); break;
229 case FLASH_MAN_SST: printf ("SST "); break;
230 case FLASH_MAN_STM: printf ("STM "); break;
231 case FLASH_MAN_INTEL: printf ("Intel "); break;
232 case FLASH_MAN_MT: printf ("MT "); break;
233 default: printf ("Unknown Vendor "); break;
236 switch (info->flash_id & FLASH_TYPEMASK) {
237 case FLASH_28F320J3A: printf ("28F320J3A (32Mbit = 128K x 32)\n");
239 case FLASH_28F640J3A: printf ("28F640J3A (64Mbit = 128K x 64)\n");
241 case FLASH_28F128J3A: printf ("28F128J3A (128Mbit = 128K x 128)\n");
243 default: printf ("Unknown Chip Type\n");
247 if (info->size >= (1 << 20)) {
252 printf (" Size: %ld %cB in %d Sectors\n",
254 (i == 20) ? 'M' : 'k',
257 printf (" Sector Start Addresses:");
258 for (i=0; i<info->sector_count; ++i) {
263 info->protect[i] ? " (RO)" : " "
270 /*-----------------------------------------------------------------------
274 /*-----------------------------------------------------------------------
278 * The following code cannot be run from FLASH!
281 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
285 /* Read Manufacturer ID */
286 addr[0] = 0x00900090;
289 debug ("Manuf. ID @ 0x%08lx: 0x%08lx\n", (ulong)addr, value);
293 info->flash_id = FLASH_MAN_AMD;
296 info->flash_id = FLASH_MAN_FUJ;
299 info->flash_id = FLASH_MAN_SST;
302 info->flash_id = FLASH_MAN_STM;
305 info->flash_id = FLASH_MAN_INTEL;
308 info->flash_id = FLASH_UNKNOWN;
309 info->sector_count = 0;
311 addr[0] = 0x00FF00FF; /* restore read mode */
312 return (0); /* no or unknown flash */
315 value = addr[1]; /* device ID */
317 debug ("Device ID @ 0x%08lx: 0x%08lx\n", (ulong)(&addr[1]), value);
320 case INTEL_ID_28F320J3A:
321 info->flash_id += FLASH_28F320J3A;
322 info->sector_count = 32;
323 info->size = 0x00400000 * 2;
326 case INTEL_ID_28F640J3A:
327 info->flash_id += FLASH_28F640J3A;
328 info->sector_count = 64;
329 info->size = 0x00800000 * 2;
330 break; /* => 16 MB */
332 case INTEL_ID_28F128J3A:
333 info->flash_id += FLASH_28F128J3A;
334 info->sector_count = 128;
335 info->size = 0x01000000 * 2;
336 break; /* => 32 MB */
339 info->flash_id = FLASH_UNKNOWN;
340 addr[0] = 0x00FF00FF; /* restore read mode */
341 return (0); /* => no or unknown flash */
345 if (info->sector_count > CFG_MAX_FLASH_SECT) {
346 printf ("** ERROR: sector count %d > max (%d) **\n",
347 info->sector_count, CFG_MAX_FLASH_SECT);
348 info->sector_count = CFG_MAX_FLASH_SECT;
351 addr[0] = 0x00FF00FF; /* restore read mode */
357 /*-----------------------------------------------------------------------
360 int flash_erase (flash_info_t *info, int s_first, int s_last)
362 int flag, prot, sect;
363 ulong start, now, last;
365 debug ("flash_erase: first: %d last: %d\n", s_first, s_last);
367 if ((s_first < 0) || (s_first > s_last)) {
368 if (info->flash_id == FLASH_UNKNOWN) {
369 printf ("- missing\n");
371 printf ("- no sectors to erase\n");
376 if ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL) {
377 printf ("Can erase only Intel flash types - aborted\n");
382 for (sect=s_first; sect<=s_last; ++sect) {
383 if (info->protect[sect]) {
389 printf ("- Warning: %d protected sectors will not be erased!\n",
395 start = get_timer (0);
397 /* Start erase on unprotected sectors */
398 for (sect = s_first; sect<=s_last; sect++) {
399 if (info->protect[sect] == 0) { /* not protected */
400 vu_long *addr = (vu_long *)(info->start[sect]);
401 unsigned long status;
403 /* Disable interrupts which might cause a timeout here */
404 flag = disable_interrupts();
406 *addr = 0x00600060; /* clear lock bit setup */
407 *addr = 0x00D000D0; /* clear lock bit confirm */
410 /* This takes awfully long - up to 50 ms and more */
411 while (((status = *addr) & 0x00800080) != 0x00800080) {
412 if ((now=get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
413 printf ("Timeout\n");
414 *addr = 0x00FF00FF; /* reset to read mode */
418 /* show that we're waiting */
419 if ((now - last) > 1000) { /* every second */
423 udelay (1000); /* to trigger the watchdog */
426 *addr = 0x00500050; /* clear status register */
427 *addr = 0x00200020; /* erase setup */
428 *addr = 0x00D000D0; /* erase confirm */
430 /* re-enable interrupts if necessary */
434 /* wait at least 80us - let's wait 1 ms */
437 while (((status = *addr) & 0x00800080) != 0x00800080) {
438 if ((now=get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
439 printf ("Timeout\n");
440 *addr = 0x00B000B0; /* suspend erase */
441 *addr = 0x00FF00FF; /* reset to read mode */
445 /* show that we're waiting */
446 if ((now - last) > 1000) { /* every second */
450 udelay (1000); /* to trigger the watchdog */
453 *addr = 0x00FF00FF; /* reset to read mode */
460 /*-----------------------------------------------------------------------
461 * Copy memory to flash, returns:
464 * 2 - Flash not erased
465 * 4 - Flash not identified
468 #define FLASH_WIDTH 4 /* flash bus width in bytes */
470 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
475 if (info->flash_id == FLASH_UNKNOWN) {
479 wp = (addr & ~(FLASH_WIDTH-1)); /* get lower FLASH_WIDTH aligned address */
482 * handle unaligned start bytes
484 if ((l = addr - wp) != 0) {
486 for (i=0, cp=wp; i<l; ++i, ++cp) {
487 data = (data << 8) | (*(uchar *)cp);
489 for (; i<FLASH_WIDTH && cnt>0; ++i) {
490 data = (data << 8) | *src++;
494 for (; cnt==0 && i<FLASH_WIDTH; ++i, ++cp) {
495 data = (data << 8) | (*(uchar *)cp);
498 if ((rc = write_data(info, wp, data)) != 0) {
505 * handle FLASH_WIDTH aligned part
507 #ifdef CFG_FLASH_USE_BUFFER_WRITE
508 while(cnt >= FLASH_WIDTH) {
509 i = CFG_FLASH_BUFFER_SIZE > cnt ?
510 (cnt & ~(FLASH_WIDTH - 1)) : CFG_FLASH_BUFFER_SIZE;
511 if((rc = write_data_buf(info, wp, src,i)) != 0)
518 while (cnt >= FLASH_WIDTH) {
520 for (i=0; i<FLASH_WIDTH; ++i) {
521 data = (data << 8) | *src++;
523 if ((rc = write_data(info, wp, data)) != 0) {
529 #endif /* CFG_FLASH_USE_BUFFER_WRITE */
536 * handle unaligned tail bytes
539 for (i=0, cp=wp; i<FLASH_WIDTH && cnt>0; ++i, ++cp) {
540 data = (data << 8) | *src++;
543 for (; i<FLASH_WIDTH; ++i, ++cp) {
544 data = (data << 8) | (*(uchar *)cp);
547 return (write_data(info, wp, data));
550 /*-----------------------------------------------------------------------
551 * Check flash status, returns:
555 static int flash_status_check(vu_long *addr, ulong tout, char * prompt)
560 /* Wait for command completion */
561 start = get_timer (0);
562 while(((status = *addr) & 0x00800080) != 0x00800080) {
563 if (get_timer(start) > tout) {
564 printf("Flash %s timeout at address %p\n", prompt, addr);
565 *addr = 0x00FF00FF; /* restore read mode */
572 /*-----------------------------------------------------------------------
573 * Write a word to Flash, returns:
576 * 2 - Flash not erased
578 static int write_data (flash_info_t *info, ulong dest, ulong data)
580 vu_long *addr = (vu_long *)dest;
583 /* Check if Flash is (sufficiently) erased */
584 if ((*addr & data) != data) {
587 /* Disable interrupts which might cause a timeout here */
588 flag = disable_interrupts();
590 *addr = 0x00400040; /* write setup */
593 /* re-enable interrupts if necessary */
597 if (flash_status_check(addr, CFG_FLASH_WRITE_TOUT, "write") != 0) {
601 *addr = 0x00FF00FF; /* restore read mode */
606 #ifdef CFG_FLASH_USE_BUFFER_WRITE
607 /*-----------------------------------------------------------------------
608 * Write a buffer to Flash, returns:
612 static int write_data_buf(flash_info_t * info, ulong dest, uchar * cp, int len)
614 vu_long *addr = (vu_long *)dest;
618 vu_long * src = (vu_long *)cp;
619 vu_long * dst = (vu_long *)dest;
622 for(sector = info->sector_count - 1; sector >= 0; sector--) {
623 if(dest >= info->start[sector])
627 *addr = 0x00500050; /* clear status */
628 *addr = 0x00e800e8; /* write buffer */
630 if((retcode = flash_status_check(addr, CFG_FLASH_BUFFER_WRITE_TOUT,
631 "write to buffer")) == 0) {
632 cnt = len / FLASH_WIDTH;
633 *addr = (cnt-1) | ((cnt-1) << 16);
637 *addr = 0x00d000d0; /* write buffer confirm */
638 retcode = flash_status_check(addr, CFG_FLASH_BUFFER_WRITE_TOUT,
641 *addr = 0x00FF00FF; /* restore read mode */
642 *addr = 0x00500050; /* clear status */
645 #endif /* CFG_USE_FLASH_BUFFER_WRITE */
647 /*-----------------------------------------------------------------------