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,
27 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
29 /*-----------------------------------------------------------------------
32 static ulong flash_get_size(vu_long *addr, flash_info_t *info);
33 static int write_word(flash_info_t *info, ulong dest, ulong data);
34 static void flash_get_offsets(ulong base, flash_info_t *info);
36 /*-----------------------------------------------------------------------
39 unsigned long flash_init(void)
41 volatile immap_t *immap = (immap_t *)CONFIG_SYS_IMMR;
42 volatile memctl8xx_t *memctl = &immap->im_memctl;
43 unsigned long size_b0;
46 /* Init: no FLASHes known */
47 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i)
48 flash_info[i].flash_id = FLASH_UNKNOWN;
50 /* Static FLASH Bank configuration here - FIXME XXX */
52 size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
54 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
55 printf("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
56 size_b0, size_b0<<20);
59 /* Remap FLASH according to real size */
60 memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b0 & 0xFFFF8000);
61 #ifdef CONFIG_FLASH_16BIT
62 memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) |
63 BR_MS_GPCM | BR_V | BR_PS_16; /* 16 Bit data port */
65 memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) |
69 /* Re-do sizing to get full correct info */
70 size_b0 = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE,
73 flash_get_offsets(CONFIG_SYS_FLASH_BASE, &flash_info[0]);
75 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
76 /* monitor protection ON by default */
77 flash_protect(FLAG_PROTECT_SET,
78 CONFIG_SYS_MONITOR_BASE,
79 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
83 memctl->memc_br1 = 0; /* invalidate bank 1 */
85 flash_info[0].size = size_b0;
90 /*-----------------------------------------------------------------------
92 static void flash_get_offsets(ulong base, flash_info_t *info)
96 if (info->flash_id == FLASH_UNKNOWN)
99 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
100 for (i = 0; i < info->sector_count; i++)
101 info->start[i] = base + (i * 0x00002000);
106 /* set up sector start address table */
107 if (info->flash_id & FLASH_BTYPE) {
108 /* set sector offsets for bottom boot block type */
109 #ifdef CONFIG_FLASH_16BIT
110 info->start[0] = base + 0x00000000;
111 info->start[1] = base + 0x00004000;
112 info->start[2] = base + 0x00006000;
113 info->start[3] = base + 0x00008000;
114 for (i = 4; i < info->sector_count; i++)
115 info->start[i] = base + (i * 0x00010000) - 0x00030000;
117 info->start[0] = base + 0x00000000;
118 info->start[1] = base + 0x00008000;
119 info->start[2] = base + 0x0000C000;
120 info->start[3] = base + 0x00010000;
121 for (i = 4; i < info->sector_count; i++)
122 info->start[i] = base + (i * 0x00020000) - 0x00060000;
125 /* set sector offsets for top boot block type */
126 i = info->sector_count - 1;
127 info->start[i--] = base + info->size - 0x00008000;
128 info->start[i--] = base + info->size - 0x0000C000;
129 info->start[i--] = base + info->size - 0x00010000;
131 info->start[i] = base + i * 0x00020000;
135 /*-----------------------------------------------------------------------
137 void flash_print_info(flash_info_t *info)
141 if (info->flash_id == FLASH_UNKNOWN) {
142 printf("missing or unknown FLASH type\n");
146 switch (info->flash_id & FLASH_VENDMASK) {
160 printf("Unknown Vendor ");
164 switch (info->flash_id & FLASH_TYPEMASK) {
166 printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
169 printf("AM29LV400T (4 Mbit, top boot sector)\n");
172 printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
175 printf("AM29LV800T (8 Mbit, top boot sector)\n");
178 printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
181 printf("AM29LV160T (16 Mbit, top boot sector)\n");
184 printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
187 printf("AM29LV320T (32 Mbit, top boot sector)\n");
190 printf("39xF200A (2M = 128K x 16)\n");
193 printf("39xF400A (4M = 256K x 16)\n");
196 printf("39xF800A (8M = 512K x 16)\n");
199 printf("M29W800AB (8M = 512K x 16)\n");
202 printf("Unknown Chip Type\n");
206 printf(" Size: %ld MB in %d Sectors\n",
207 info->size >> 20, info->sector_count);
209 printf(" Sector Start Addresses:");
210 for (i = 0; i < info->sector_count; ++i) {
215 info->protect[i] ? " (RO)" : " "
223 * The following code cannot be run from FLASH!
226 static ulong flash_get_size(vu_long *addr, flash_info_t *info)
230 ulong base = (ulong)addr;
232 /* Write auto select command: read Manufacturer ID */
233 #ifdef CONFIG_FLASH_16BIT
234 vu_short *s_addr = (vu_short *)addr;
235 s_addr[0x5555] = 0x00AA;
236 s_addr[0x2AAA] = 0x0055;
237 s_addr[0x5555] = 0x0090;
239 value = value|(value<<16);
241 addr[0x5555] = 0x00AA00AA;
242 addr[0x2AAA] = 0x00550055;
243 addr[0x5555] = 0x00900090;
249 info->flash_id = FLASH_MAN_AMD;
252 info->flash_id = FLASH_MAN_FUJ;
255 info->flash_id = FLASH_MAN_SST;
258 info->flash_id = FLASH_MAN_STM;
261 info->flash_id = FLASH_UNKNOWN;
262 info->sector_count = 0;
264 return 0; /* no or unknown flash */
266 #ifdef CONFIG_FLASH_16BIT
268 value = value|(value<<16);
270 value = addr[1]; /* device ID */
275 info->flash_id += FLASH_AM400T;
276 info->sector_count = 11;
277 info->size = 0x00100000;
281 info->flash_id += FLASH_AM400B;
282 info->sector_count = 11;
283 info->size = 0x00100000;
287 info->flash_id += FLASH_AM800T;
288 info->sector_count = 19;
289 info->size = 0x00200000;
293 info->flash_id += FLASH_AM800B;
294 #ifdef CONFIG_FLASH_16BIT
295 info->sector_count = 19;
296 info->size = 0x00100000; /* => 1 MB */
298 info->sector_count = 19;
299 info->size = 0x00200000; /* => 2 MB */
304 info->flash_id += FLASH_AM160T;
305 info->sector_count = 35;
306 info->size = 0x00400000;
310 info->flash_id += FLASH_AM160B;
311 #ifdef CONFIG_FLASH_16BIT
312 info->sector_count = 35;
313 info->size = 0x00200000; /* => 2 MB */
315 info->sector_count = 35;
316 info->size = 0x00400000; /* => 4 MB */
321 info->flash_id += FLASH_SST200A;
322 info->sector_count = 64; /* 39xF200A (2M = 128K x 16) */
323 info->size = 0x00080000;
326 info->flash_id += FLASH_SST400A;
327 info->sector_count = 128; /* 39xF400A (4M = 256K x 16) */
328 info->size = 0x00100000;
331 info->flash_id += FLASH_SST800A;
332 info->sector_count = 256; /* 39xF800A (8M = 512K x 16) */
333 info->size = 0x00200000;
336 info->flash_id += FLASH_STM800AB;
337 info->sector_count = 19;
338 info->size = 0x00200000;
341 info->flash_id = FLASH_UNKNOWN;
342 return 0; /* => no or unknown flash */
346 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
347 printf("** ERROR: sector count %d > max (%d) **\n",
348 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
349 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
352 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
353 for (i = 0; i < info->sector_count; i++)
354 info->start[i] = base + (i * 0x00002000);
355 } else { /* AMD and Fujitsu types */
356 /* set up sector start address table */
357 if (info->flash_id & FLASH_BTYPE) {
358 /* set sector offsets for bottom boot block type */
359 #ifdef CONFIG_FLASH_16BIT
361 info->start[0] = base + 0x00000000;
362 info->start[1] = base + 0x00004000;
363 info->start[2] = base + 0x00006000;
364 info->start[3] = base + 0x00008000;
365 for (i = 4; i < info->sector_count; i++)
366 info->start[i] = base +
367 (i * 0x00010000) - 0x00030000;
369 info->start[0] = base + 0x00000000;
370 info->start[1] = base + 0x00008000;
371 info->start[2] = base + 0x0000C000;
372 info->start[3] = base + 0x00010000;
373 for (i = 4; i < info->sector_count; i++)
374 info->start[i] = base +
375 (i * 0x00020000) - 0x00060000;
378 /* set sector offsets for top boot block type */
379 i = info->sector_count - 1;
380 info->start[i--] = base + info->size - 0x00008000;
381 info->start[i--] = base + info->size - 0x0000C000;
382 info->start[i--] = base + info->size - 0x00010000;
384 info->start[i] = base + i * 0x00020000;
387 /* check for protected sectors */
388 for (i = 0; i < info->sector_count; i++) {
390 * read sector protection at sector address:
392 * D0 = 1 if protected
394 #ifdef CONFIG_FLASH_16BIT
395 s_addr = (volatile unsigned short *)(info->start[i]);
396 info->protect[i] = s_addr[2] & 1;
398 addr = (volatile unsigned long *)(info->start[i]);
399 info->protect[i] = addr[2] & 1;
405 * Prevent writes to uninitialized FLASH.
407 if (info->flash_id != FLASH_UNKNOWN) {
408 #ifdef CONFIG_FLASH_16BIT
409 s_addr = (volatile unsigned short *)(info->start[0]);
410 *s_addr = 0x00F0; /* reset bank */
412 addr = (volatile unsigned long *)info->start[0];
413 *addr = 0x00F000F0; /* reset bank */
420 int flash_erase(flash_info_t *info, int s_first, int s_last)
422 vu_long *addr = (vu_long *)(info->start[0]);
423 int flag, prot, sect;
424 ulong start, now, last;
425 #ifdef CONFIG_FLASH_16BIT
426 vu_short *s_addr = (vu_short *)addr;
429 if ((s_first < 0) || (s_first > s_last)) {
430 if (info->flash_id == FLASH_UNKNOWN)
431 printf("- missing\n");
433 printf("- no sectors to erase\n");
436 /*#ifndef CONFIG_FLASH_16BIT
438 type = (info->flash_id & FLASH_VENDMASK);
439 if ((type != FLASH_MAN_SST) && (type != FLASH_MAN_STM)) {
440 printf ("Can't erase unknown flash type %08lx - aborted\n",
446 for (sect = s_first; sect <= s_last; ++sect) {
447 if (info->protect[sect])
452 printf("- Warning: %d protected sectors will not be erased!\n",
458 start = get_timer(0);
460 /* Start erase on unprotected sectors */
461 for (sect = s_first; sect <= s_last; sect++) {
462 if (info->protect[sect] == 0) { /* not protected */
463 #ifdef CONFIG_FLASH_16BIT
464 vu_short *s_sect_addr = (vu_short *)(info->start[sect]);
466 vu_long *sect_addr = (vu_long *)(info->start[sect]);
468 /* Disable interrupts which might cause a timeout */
469 flag = disable_interrupts();
471 #ifdef CONFIG_FLASH_16BIT
473 /*printf("\ns_sect_addr=%x",s_sect_addr);*/
474 s_addr[0x5555] = 0x00AA;
475 s_addr[0x2AAA] = 0x0055;
476 s_addr[0x5555] = 0x0080;
477 s_addr[0x5555] = 0x00AA;
478 s_addr[0x2AAA] = 0x0055;
479 s_sect_addr[0] = 0x0030;
481 addr[0x5555] = 0x00AA00AA;
482 addr[0x2AAA] = 0x00550055;
483 addr[0x5555] = 0x00800080;
484 addr[0x5555] = 0x00AA00AA;
485 addr[0x2AAA] = 0x00550055;
486 sect_addr[0] = 0x00300030;
488 /* re-enable interrupts if necessary */
492 /* wait at least 80us - let's wait 1 ms */
495 #ifdef CONFIG_FLASH_16BIT
496 while ((s_sect_addr[0] & 0x0080) != 0x0080) {
498 while ((sect_addr[0] & 0x00800080) != 0x00800080) {
500 now = get_timer(start);
501 if (now > CONFIG_SYS_FLASH_ERASE_TOUT) {
505 /* show every second that we're waiting */
506 if ((now - last) > 1000) {
514 /* reset to read mode */
515 addr = (volatile unsigned long *)info->start[0];
516 #ifdef CONFIG_FLASH_16BIT
517 s_addr[0] = 0x00F0; /* reset bank */
519 addr[0] = 0x00F000F0; /* reset bank */
526 /*-----------------------------------------------------------------------
527 * Copy memory to flash, returns:
530 * 2 - Flash not erased
531 * 4 - Flash not identified
534 int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
539 if (info->flash_id == FLASH_UNKNOWN)
542 wp = (addr & ~3); /* get lower word aligned address */
545 * handle unaligned start bytes
551 for (i = 0, cp = wp; i < l; ++i, ++cp)
552 data = (data << 8) | (*(uchar *)cp);
554 for (; i < 4 && cnt > 0; ++i) {
555 data = (data << 8) | *src++;
559 for (; cnt == 0 && i < 4; ++i, ++cp)
560 data = (data << 8) | (*(uchar *)cp);
562 rc = write_word(info, wp, data);
571 * handle word aligned part
575 for (i = 0; i < 4; ++i)
576 data = (data << 8) | *src++;
578 rc = write_word(info, wp, data);
590 * handle unaligned tail bytes
593 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
594 data = (data << 8) | *src++;
597 for (; i < 4; ++i, ++cp)
598 data = (data << 8) | (*(uchar *)cp);
600 return write_word(info, wp, data);
603 /*-----------------------------------------------------------------------
604 * Write a word to Flash, returns:
607 * 2 - Flash not erased
609 static int write_word(flash_info_t *info, ulong dest, ulong data)
611 vu_long *addr = (vu_long *)(info->start[0]);
613 #ifdef CONFIG_FLASH_16BIT
616 vu_short *s_addr = (vu_short *)addr;
621 /* Check if Flash is (sufficiently) erased */
622 if ((*((vu_long *)dest) & data) != data)
625 #ifdef CONFIG_FLASH_16BIT
626 /* Write the 16 higher-bits */
627 /* Disable interrupts which might cause a timeout here */
628 flag = disable_interrupts();
630 high_data = ((data>>16) & 0x0000ffff);
632 s_addr[0x5555] = 0x00AA;
633 s_addr[0x2AAA] = 0x0055;
634 s_addr[0x5555] = 0x00A0;
636 *((vu_short *)dest) = high_data;
638 /* re-enable interrupts if necessary */
642 /* data polling for D7 */
643 start = get_timer(0);
644 while ((*((vu_short *)dest) & 0x0080) != (high_data & 0x0080)) {
645 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT)
649 /* Write the 16 lower-bits */
652 /* Disable interrupts which might cause a timeout here */
653 flag = disable_interrupts();
654 #ifdef CONFIG_FLASH_16BIT
656 low_data = (data & 0x0000ffff);
658 s_addr[0x5555] = 0x00AA;
659 s_addr[0x2AAA] = 0x0055;
660 s_addr[0x5555] = 0x00A0;
661 *((vu_short *)dest) = low_data;
664 addr[0x5555] = 0x00AA00AA;
665 addr[0x2AAA] = 0x00550055;
666 addr[0x5555] = 0x00A000A0;
667 *((vu_long *)dest) = data;
670 /* re-enable interrupts if necessary */
674 /* data polling for D7 */
675 start = get_timer(0);
677 #ifdef CONFIG_FLASH_16BIT
678 while ((*((vu_short *)dest) & 0x0080) != (low_data & 0x0080)) {
680 while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
683 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT)