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,
30 #define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
33 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
35 /*-----------------------------------------------------------------------
38 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
39 static int write_word (flash_info_t *info, ulong dest, ulong data);
41 /*-----------------------------------------------------------------------
44 unsigned long flash_init (void)
46 volatile immap_t *immap = (immap_t *)CFG_IMMR;
47 volatile memctl8xx_t *memctl = &immap->im_memctl;
51 /* Init: no FLASHes known */
52 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
53 flash_info[i].flash_id = FLASH_UNKNOWN;
56 /* Static FLASH Bank configuration here - FIXME XXX */
58 size = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
60 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
61 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
65 /* Remap FLASH according to real size */
66 memctl->memc_or0 = CFG_OR_TIMING_FLASH | (-size & OR_AM_MSK);
67 memctl->memc_br0 = (CFG_FLASH_BASE & BR_BA_MSK) | BR_MS_GPCM | BR_V;
69 /* Re-do sizing to get full correct info */
70 size = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
72 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
73 /* monitor protection ON by default */
74 flash_protect(FLAG_PROTECT_SET,
76 CFG_MONITOR_BASE+monitor_flash_len-1,
80 #ifdef CFG_ENV_IS_IN_FLASH
81 /* ENV protection ON by default */
82 flash_protect(FLAG_PROTECT_SET,
84 CFG_ENV_ADDR+CFG_ENV_SIZE-1,
88 flash_info[0].size = size;
93 /*-----------------------------------------------------------------------
95 void flash_print_info (flash_info_t *info)
99 if (info->flash_id == FLASH_UNKNOWN) {
100 puts ("missing or unknown FLASH type\n");
104 switch (info->flash_id & FLASH_VENDMASK) {
105 case FLASH_MAN_AMD: puts ("AMD "); break;
106 case FLASH_MAN_FUJ: puts ("FUJITSU "); break;
107 default: puts ("Unknown Vendor "); break;
110 switch (info->flash_id & FLASH_TYPEMASK) {
111 case FLASH_AM400B: puts ("AM29LV400B (4 Mbit, bottom boot sect)\n");
113 case FLASH_AM400T: puts ("AM29LV400T (4 Mbit, top boot sector)\n");
115 case FLASH_AM800B: puts ("AM29LV800B (8 Mbit, bottom boot sect)\n");
117 case FLASH_AM800T: puts ("AM29LV800T (8 Mbit, top boot sector)\n");
119 case FLASH_AM160B: puts ("AM29LV160B (16 Mbit, bottom boot sect)\n");
121 case FLASH_AM160T: puts ("AM29LV160T (16 Mbit, top boot sector)\n");
123 case FLASH_AM320B: puts ("AM29LV320B (32 Mbit, bottom boot sect)\n");
125 case FLASH_AM320T: puts ("AM29LV320T (32 Mbit, top boot sector)\n");
127 default: puts ("Unknown Chip Type\n");
131 printf (" Size: %ld MB in %d Sectors\n",
132 info->size >> 20, info->sector_count);
134 puts (" Sector Start Addresses:");
135 for (i=0; i<info->sector_count; ++i) {
140 info->protect[i] ? " (RO)" : " "
147 /*-----------------------------------------------------------------------
151 /*-----------------------------------------------------------------------
155 * The following code cannot be run from FLASH!
158 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
162 ulong base = (ulong)addr;
164 /* Write auto select command: read Manufacturer ID */
165 addr[0x0555] = 0x00AA00AA;
166 addr[0x02AA] = 0x00550055;
167 addr[0x0555] = 0x00900090;
173 info->flash_id = FLASH_MAN_AMD;
176 info->flash_id = FLASH_MAN_FUJ;
179 info->flash_id = FLASH_UNKNOWN;
180 info->sector_count = 0;
182 return (0); /* no or unknown flash */
185 value = addr[1]; /* device ID */
189 info->flash_id += FLASH_AM400T;
190 info->sector_count = 11;
191 info->size = 0x00100000;
195 info->flash_id += FLASH_AM400B;
196 info->sector_count = 11;
197 info->size = 0x00100000;
201 info->flash_id += FLASH_AM800T;
202 info->sector_count = 19;
203 info->size = 0x00200000;
207 info->flash_id += FLASH_AM800B;
208 info->sector_count = 19;
209 info->size = 0x00200000;
213 info->flash_id += FLASH_AM160T;
214 info->sector_count = 35;
215 info->size = 0x00400000;
219 info->flash_id += FLASH_AM160B;
220 info->sector_count = 35;
221 info->size = 0x00400000;
224 info->flash_id += FLASH_AM320T;
225 info->sector_count = 71;
226 info->size = 0x00800000;
230 info->flash_id += FLASH_AM320B;
231 info->sector_count = 71;
232 info->size = 0x00800000;
235 info->flash_id = FLASH_UNKNOWN;
236 return (0); /* => no or unknown flash */
239 /* set up sector start address table */
244 /* set sector offsets for bottom boot block type */
245 info->start[0] = base + 0x00000000;
246 info->start[1] = base + 0x00008000;
247 info->start[2] = base + 0x0000C000;
248 info->start[3] = base + 0x00010000;
249 for (i = 4; i < info->sector_count; i++) {
250 info->start[i] = base + (i * 0x00020000) - 0x00060000;
256 /* set sector offsets for top boot block type */
257 i = info->sector_count - 1;
258 info->start[i--] = base + info->size - 0x00008000;
259 info->start[i--] = base + info->size - 0x0000C000;
260 info->start[i--] = base + info->size - 0x00010000;
261 for (; i >= 0; i--) {
262 info->start[i] = base + i * 0x00020000;
266 for (i = 0; i < info->sector_count; i++) {
267 info->start[i] = base;
269 * The first 8 sectors are 8 kB,
270 * all the other ones are 64 kB
278 for (i = 0; i < info->sector_count; i++) {
279 info->start[i] = base;
281 * The last 8 sectors are 8 kB,
282 * all the other ones are 64 kB
284 base += (i < (info->sector_count - 8))
294 /* check for protected sectors */
295 for (i = 0; i < info->sector_count; i++) {
296 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
297 /* D0 = 1 if protected */
298 addr = (volatile unsigned long *)(info->start[i]);
299 info->protect[i] = addr[2] & 1;
303 * Prevent writes to uninitialized FLASH.
305 if (info->flash_id != FLASH_UNKNOWN) {
306 addr = (volatile unsigned long *)info->start[0];
308 *addr = 0x00F000F0; /* reset bank */
315 /*-----------------------------------------------------------------------
318 int flash_erase (flash_info_t *info, int s_first, int s_last)
320 vu_long *addr = (vu_long*)(info->start[0]);
321 int flag, prot, sect, l_sect;
322 ulong start, now, last;
324 if ((s_first < 0) || (s_first > s_last)) {
325 if (info->flash_id == FLASH_UNKNOWN) {
326 puts ("- missing\n");
328 puts ("- no sectors to erase\n");
333 if ((info->flash_id == FLASH_UNKNOWN) ||
334 (info->flash_id > FLASH_AMD_COMP)) {
335 printf ("Can't erase unknown flash type %08lx - aborted\n",
341 for (sect=s_first; sect<=s_last; ++sect) {
342 if (info->protect[sect]) {
348 printf ("- Warning: %d protected sectors will not be erased!\n",
356 /* Disable interrupts which might cause a timeout here */
357 flag = disable_interrupts();
359 addr[0x0555] = 0x00AA00AA;
360 addr[0x02AA] = 0x00550055;
361 addr[0x0555] = 0x00800080;
362 addr[0x0555] = 0x00AA00AA;
363 addr[0x02AA] = 0x00550055;
365 /* Start erase on unprotected sectors */
366 for (sect = s_first; sect<=s_last; sect++) {
367 if (info->protect[sect] == 0) { /* not protected */
368 addr = (vu_long*)(info->start[sect]);
369 addr[0] = 0x00300030;
374 /* re-enable interrupts if necessary */
378 /* wait at least 80us - let's wait 1 ms */
382 * We wait for the last triggered sector
387 start = get_timer (0);
389 addr = (vu_long*)(info->start[l_sect]);
390 while ((addr[0] & 0x00800080) != 0x00800080) {
391 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
395 /* show that we're waiting */
396 if ((now - last) > 1000) { /* every second */
403 /* reset to read mode */
404 addr = (volatile unsigned long *)info->start[0];
405 addr[0] = 0x00F000F0; /* reset bank */
411 /*-----------------------------------------------------------------------
412 * Copy memory to flash, returns:
415 * 2 - Flash not erased
418 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
423 wp = (addr & ~3); /* get lower word aligned address */
426 * handle unaligned start bytes
428 if ((l = addr - wp) != 0) {
430 for (i=0, cp=wp; i<l; ++i, ++cp) {
431 data = (data << 8) | (*(uchar *)cp);
433 for (; i<4 && cnt>0; ++i) {
434 data = (data << 8) | *src++;
438 for (; cnt==0 && i<4; ++i, ++cp) {
439 data = (data << 8) | (*(uchar *)cp);
442 if ((rc = write_word(info, wp, data)) != 0) {
449 * handle word aligned part
453 for (i=0; i<4; ++i) {
454 data = (data << 8) | *src++;
456 if ((rc = write_word(info, wp, data)) != 0) {
468 * handle unaligned tail bytes
471 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
472 data = (data << 8) | *src++;
475 for (; i<4; ++i, ++cp) {
476 data = (data << 8) | (*(uchar *)cp);
479 return (write_word(info, wp, data));
482 /*-----------------------------------------------------------------------
483 * Write a word to Flash, returns:
486 * 2 - Flash not erased
488 static int write_word (flash_info_t *info, ulong dest, ulong data)
490 vu_long *addr = (vu_long*)(info->start[0]);
494 /* Check if Flash is (sufficiently) erased */
495 if ((*((vu_long *)dest) & data) != data) {
498 /* Disable interrupts which might cause a timeout here */
499 flag = disable_interrupts();
501 addr[0x0555] = 0x00AA00AA;
502 addr[0x02AA] = 0x00550055;
503 addr[0x0555] = 0x00A000A0;
505 *((vu_long *)dest) = data;
507 /* re-enable interrupts if necessary */
511 /* data polling for D7 */
512 start = get_timer (0);
513 while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
514 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
521 /*-----------------------------------------------------------------------