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(CFG_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 static void flash_get_offsets (ulong base, flash_info_t *info);
52 /*-----------------------------------------------------------------------
55 unsigned long flash_init (void)
57 volatile immap_t *immap = (immap_t *)CFG_IMMR;
58 volatile memctl8xx_t *memctl = &immap->im_memctl;
59 unsigned long size_b0, size_b1;
62 /* Init: no FLASHes known */
63 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
64 flash_info[i].flash_id = FLASH_UNKNOWN;
67 /* Static FLASH Bank configuration here - FIXME XXX */
69 debug ("\n## Get flash bank 1 size @ 0x%08x\n",FLASH_BASE0_PRELIM);
71 size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
73 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
74 printf ("## Unknown FLASH on Bank 0: "
75 "ID 0x%lx, Size = 0x%08lx = %ld MB\n",
76 flash_info[0].flash_id,
77 size_b0, size_b0<<20);
80 debug ("## Get flash bank 2 size @ 0x%08x\n",FLASH_BASE1_PRELIM);
82 size_b1 = flash_get_size((vu_long *)FLASH_BASE1_PRELIM, &flash_info[1]);
84 debug ("## Prelim. Flash bank sizes: %08lx + 0x%08lx\n",size_b0,size_b1);
86 if (size_b1 > size_b0) {
88 "Bank 1 (0x%08lx = %ld MB) > Bank 0 (0x%08lx = %ld MB)\n",
92 flash_info[0].flash_id = FLASH_UNKNOWN;
93 flash_info[1].flash_id = FLASH_UNKNOWN;
94 flash_info[0].sector_count = -1;
95 flash_info[1].sector_count = -1;
96 flash_info[0].size = 0;
97 flash_info[1].size = 0;
101 debug ("## Before remap: "
102 "BR0: 0x%08x OR0: 0x%08x "
103 "BR1: 0x%08x OR1: 0x%08x\n",
104 memctl->memc_br0, memctl->memc_or0,
105 memctl->memc_br1, memctl->memc_or1);
107 /* Remap FLASH according to real size */
108 memctl->memc_or0 = (-size_b0 & 0xFFFF8000) | CFG_OR_TIMING_FLASH |
109 OR_CSNT_SAM | OR_ACS_DIV1;
110 memctl->memc_br0 = (CFG_FLASH_BASE & BR_BA_MSK) | BR_PS_32 | BR_V;
112 debug ("## BR0: 0x%08x OR0: 0x%08x\n",
113 memctl->memc_br0, memctl->memc_or0);
115 /* Re-do sizing to get full correct info */
116 size_b0 = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
118 flash_get_offsets (CFG_FLASH_BASE, &flash_info[0]);
120 flash_info[0].size = size_b0;
122 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
123 /* monitor protection ON by default */
124 flash_protect(FLAG_PROTECT_SET,
126 CFG_MONITOR_BASE+monitor_flash_len-1,
130 #ifdef CFG_ENV_IS_IN_FLASH
131 /* ENV protection ON by default */
132 flash_protect(FLAG_PROTECT_SET,
134 CFG_ENV_ADDR+CFG_ENV_SECT_SIZE-1,
139 memctl->memc_or1 = (-size_b1 & 0xFFFF8000) | CFG_OR_TIMING_FLASH |
140 OR_CSNT_SAM | OR_ACS_DIV1;
141 memctl->memc_br1 = ((CFG_FLASH_BASE + size_b0) & BR_BA_MSK) |
144 debug ("## BR1: 0x%08x OR1: 0x%08x\n",
145 memctl->memc_br1, memctl->memc_or1);
147 /* Re-do sizing to get full correct info */
148 size_b1 = flash_get_size((vu_long *)(CFG_FLASH_BASE + size_b0),
151 flash_info[1].size = size_b1;
153 flash_get_offsets (CFG_FLASH_BASE + size_b0, &flash_info[1]);
155 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
156 /* monitor protection ON by default */
157 flash_protect(FLAG_PROTECT_SET,
159 CFG_MONITOR_BASE+monitor_flash_len-1,
163 #ifdef CFG_ENV_IS_IN_FLASH
164 /* ENV protection ON by default */
165 flash_protect(FLAG_PROTECT_SET,
167 CFG_ENV_ADDR+CFG_ENV_SECT_SIZE-1,
171 memctl->memc_br1 = 0; /* invalidate bank */
172 memctl->memc_or1 = 0; /* invalidate bank */
174 debug ("## DISABLE BR1: 0x%08x OR1: 0x%08x\n",
175 memctl->memc_br1, memctl->memc_or1);
177 flash_info[1].flash_id = FLASH_UNKNOWN;
178 flash_info[1].sector_count = -1;
179 flash_info[1].size = 0;
182 debug ("## Final Flash bank sizes: %08lx + 0x%08lx\n",size_b0,size_b1);
184 return (size_b0 + size_b1);
187 /*-----------------------------------------------------------------------
189 static void flash_get_offsets (ulong base, flash_info_t *info)
193 if (info->flash_id == FLASH_UNKNOWN) {
197 switch (info->flash_id & FLASH_VENDMASK) {
198 case FLASH_MAN_INTEL:
199 for (i = 0; i < info->sector_count; i++) {
200 info->start[i] = base;
201 base += 0x00020000 * 2; /* 128k * 2 chips per bank */
206 printf ("Don't know sector ofsets for flash type 0x%lx\n",
212 /*-----------------------------------------------------------------------
214 void flash_print_info (flash_info_t *info)
218 if (info->flash_id == FLASH_UNKNOWN) {
219 printf ("missing or unknown FLASH type\n");
223 switch (info->flash_id & FLASH_VENDMASK) {
224 case FLASH_MAN_AMD: printf ("AMD "); break;
225 case FLASH_MAN_FUJ: printf ("Fujitsu "); break;
226 case FLASH_MAN_SST: printf ("SST "); break;
227 case FLASH_MAN_STM: printf ("STM "); break;
228 case FLASH_MAN_INTEL: printf ("Intel "); break;
229 case FLASH_MAN_MT: printf ("MT "); break;
230 default: printf ("Unknown Vendor "); break;
233 switch (info->flash_id & FLASH_TYPEMASK) {
234 case FLASH_28F320J3A: printf ("28F320J3A (32Mbit = 128K x 32)\n");
236 case FLASH_28F640J3A: printf ("28F640J3A (64Mbit = 128K x 64)\n");
238 case FLASH_28F128J3A: printf ("28F128J3A (128Mbit = 128K x 128)\n");
240 default: printf ("Unknown Chip Type\n");
244 if (info->size >= (1 << 20)) {
249 printf (" Size: %ld %cB in %d Sectors\n",
251 (i == 20) ? 'M' : 'k',
254 printf (" Sector Start Addresses:");
255 for (i=0; i<info->sector_count; ++i) {
260 info->protect[i] ? " (RO)" : " "
267 /*-----------------------------------------------------------------------
271 /*-----------------------------------------------------------------------
275 * The following code cannot be run from FLASH!
278 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
282 /* Read Manufacturer ID */
283 addr[0] = 0x00900090;
286 debug ("Manuf. ID @ 0x%08lx: 0x%08lx\n", (ulong)addr, value);
290 info->flash_id = FLASH_MAN_AMD;
293 info->flash_id = FLASH_MAN_FUJ;
296 info->flash_id = FLASH_MAN_SST;
299 info->flash_id = FLASH_MAN_STM;
302 info->flash_id = FLASH_MAN_INTEL;
305 info->flash_id = FLASH_UNKNOWN;
306 info->sector_count = 0;
308 addr[0] = 0x00FF00FF; /* restore read mode */
309 return (0); /* no or unknown flash */
312 value = addr[1]; /* device ID */
314 debug ("Device ID @ 0x%08lx: 0x%08lx\n", (ulong)(&addr[1]), value);
317 case INTEL_ID_28F320J3A:
318 info->flash_id += FLASH_28F320J3A;
319 info->sector_count = 32;
320 info->size = 0x00400000 * 2;
323 case INTEL_ID_28F640J3A:
324 info->flash_id += FLASH_28F640J3A;
325 info->sector_count = 64;
326 info->size = 0x00800000 * 2;
327 break; /* => 16 MB */
329 case INTEL_ID_28F128J3A:
330 info->flash_id += FLASH_28F128J3A;
331 info->sector_count = 128;
332 info->size = 0x01000000 * 2;
333 break; /* => 32 MB */
336 info->flash_id = FLASH_UNKNOWN;
337 addr[0] = 0x00FF00FF; /* restore read mode */
338 return (0); /* => no or unknown flash */
342 if (info->sector_count > CFG_MAX_FLASH_SECT) {
343 printf ("** ERROR: sector count %d > max (%d) **\n",
344 info->sector_count, CFG_MAX_FLASH_SECT);
345 info->sector_count = CFG_MAX_FLASH_SECT;
348 addr[0] = 0x00FF00FF; /* restore read mode */
354 /*-----------------------------------------------------------------------
357 int flash_erase (flash_info_t *info, int s_first, int s_last)
359 int flag, prot, sect;
360 ulong start, now, last;
362 debug ("flash_erase: first: %d last: %d\n", s_first, s_last);
364 if ((s_first < 0) || (s_first > s_last)) {
365 if (info->flash_id == FLASH_UNKNOWN) {
366 printf ("- missing\n");
368 printf ("- no sectors to erase\n");
373 if ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL) {
374 printf ("Can erase only Intel flash types - aborted\n");
379 for (sect=s_first; sect<=s_last; ++sect) {
380 if (info->protect[sect]) {
386 printf ("- Warning: %d protected sectors will not be erased!\n",
392 start = get_timer (0);
394 /* Start erase on unprotected sectors */
395 for (sect = s_first; sect<=s_last; sect++) {
396 if (info->protect[sect] == 0) { /* not protected */
397 vu_long *addr = (vu_long *)(info->start[sect]);
398 unsigned long status;
400 /* Disable interrupts which might cause a timeout here */
401 flag = disable_interrupts();
403 *addr = 0x00500050; /* clear status register */
404 *addr = 0x00200020; /* erase setup */
405 *addr = 0x00D000D0; /* erase confirm */
407 /* re-enable interrupts if necessary */
411 /* wait at least 80us - let's wait 1 ms */
414 while (((status = *addr) & 0x00800080) != 0x00800080) {
415 if ((now=get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
416 printf ("Timeout\n");
417 *addr = 0x00B000B0; /* suspend erase */
418 *addr = 0x00FF00FF; /* reset to read mode */
422 /* show that we're waiting */
423 if ((now - last) > 1000) { /* every second */
429 *addr = 0x00FF00FF; /* reset to read mode */
436 /*-----------------------------------------------------------------------
437 * Copy memory to flash, returns:
440 * 2 - Flash not erased
441 * 4 - Flash not identified
444 #define FLASH_WIDTH 4 /* flash bus width in bytes */
446 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
451 if (info->flash_id == FLASH_UNKNOWN) {
455 wp = (addr & ~(FLASH_WIDTH-1)); /* get lower FLASH_WIDTH aligned address */
458 * handle unaligned start bytes
460 if ((l = addr - wp) != 0) {
462 for (i=0, cp=wp; i<l; ++i, ++cp) {
463 data = (data << 8) | (*(uchar *)cp);
465 for (; i<FLASH_WIDTH && cnt>0; ++i) {
466 data = (data << 8) | *src++;
470 for (; cnt==0 && i<FLASH_WIDTH; ++i, ++cp) {
471 data = (data << 8) | (*(uchar *)cp);
474 if ((rc = write_data(info, wp, data)) != 0) {
481 * handle FLASH_WIDTH aligned part
483 while (cnt >= FLASH_WIDTH) {
485 for (i=0; i<FLASH_WIDTH; ++i) {
486 data = (data << 8) | *src++;
488 if ((rc = write_data(info, wp, data)) != 0) {
500 * handle unaligned tail bytes
503 for (i=0, cp=wp; i<FLASH_WIDTH && cnt>0; ++i, ++cp) {
504 data = (data << 8) | *src++;
507 for (; i<FLASH_WIDTH; ++i, ++cp) {
508 data = (data << 8) | (*(uchar *)cp);
511 return (write_data(info, wp, data));
514 /*-----------------------------------------------------------------------
515 * Write a word to Flash, returns:
518 * 2 - Flash not erased
520 static int write_data (flash_info_t *info, ulong dest, ulong data)
522 vu_long *addr = (vu_long *)dest;
527 /* Check if Flash is (sufficiently) erased */
528 if ((*addr & data) != data) {
531 /* Disable interrupts which might cause a timeout here */
532 flag = disable_interrupts();
534 *addr = 0x00400040; /* write setup */
537 /* re-enable interrupts if necessary */
541 start = get_timer (0);
543 while (((status = *addr) & 0x00800080) != 0x00800080) {
544 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
545 *addr = 0x00FF00FF; /* restore read mode */
550 *addr = 0x00FF00FF; /* restore read mode */
555 /*-----------------------------------------------------------------------