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, size_b1;
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 size_b1 = flash_get_size((vu_long *)FLASH_BASE1_PRELIM, &flash_info[1]);
61 if (size_b1 > size_b0) {
63 "Bank 1 (0x%08lx = %ld MB) > Bank 0 (0x%08lx = %ld MB)\n",
67 flash_info[0].flash_id = FLASH_UNKNOWN;
68 flash_info[1].flash_id = FLASH_UNKNOWN;
69 flash_info[0].sector_count = -1;
70 flash_info[1].sector_count = -1;
71 flash_info[0].size = 0;
72 flash_info[1].size = 0;
76 /* Remap FLASH according to real size */
77 memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b0 & 0xFFFF8000);
78 #ifdef CONFIG_FLASH_16BIT
79 memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) |
80 BR_MS_GPCM | BR_V | BR_PS_16; /* 16 Bit data port */
82 memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) |
86 /* Re-do sizing to get full correct info */
87 size_b0 = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE,
90 flash_get_offsets(CONFIG_SYS_FLASH_BASE, &flash_info[0]);
92 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
93 /* monitor protection ON by default */
94 flash_protect(FLAG_PROTECT_SET,
95 CONFIG_SYS_MONITOR_BASE,
96 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
101 memctl->memc_or1 = CONFIG_SYS_OR_TIMING_FLASH |
102 (-size_b1 & 0xFFFF8000);
103 #ifdef CONFIG_FLASH_16BIT
104 memctl->memc_br1 = ((CONFIG_SYS_FLASH_BASE + size_b0) &
105 BR_BA_MSK) | BR_MS_GPCM | BR_V | BR_PS_16;
107 memctl->memc_br1 = ((CONFIG_SYS_FLASH_BASE + size_b0) &
108 BR_BA_MSK) | BR_MS_GPCM | BR_V;
111 /* Re-do sizing to get full correct info */
112 size_b1 = flash_get_size((vu_long *)(CONFIG_SYS_FLASH_BASE +
113 size_b0), &flash_info[1]);
115 flash_get_offsets(CONFIG_SYS_FLASH_BASE + size_b0,
118 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
119 /* monitor protection ON by default */
120 flash_protect(FLAG_PROTECT_SET,
121 CONFIG_SYS_MONITOR_BASE,
122 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
126 memctl->memc_br1 = 0; /* invalidate bank */
128 flash_info[1].flash_id = FLASH_UNKNOWN;
129 flash_info[1].sector_count = -1;
132 flash_info[0].size = size_b0;
133 flash_info[1].size = size_b1;
135 return size_b0 + size_b1;
138 /*-----------------------------------------------------------------------
140 static void flash_get_offsets(ulong base, flash_info_t *info)
144 if (info->flash_id == FLASH_UNKNOWN)
147 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
148 for (i = 0; i < info->sector_count; i++)
149 info->start[i] = base + (i * 0x00002000);
154 /* set up sector start address table */
155 if (info->flash_id & FLASH_BTYPE) {
156 /* set sector offsets for bottom boot block type */
157 #ifdef CONFIG_FLASH_16BIT
158 info->start[0] = base + 0x00000000;
159 info->start[1] = base + 0x00004000;
160 info->start[2] = base + 0x00006000;
161 info->start[3] = base + 0x00008000;
162 for (i = 4; i < info->sector_count; i++)
163 info->start[i] = base + (i * 0x00010000) - 0x00030000;
165 info->start[0] = base + 0x00000000;
166 info->start[1] = base + 0x00008000;
167 info->start[2] = base + 0x0000C000;
168 info->start[3] = base + 0x00010000;
169 for (i = 4; i < info->sector_count; i++)
170 info->start[i] = base + (i * 0x00020000) - 0x00060000;
173 /* set sector offsets for top boot block type */
174 i = info->sector_count - 1;
175 info->start[i--] = base + info->size - 0x00008000;
176 info->start[i--] = base + info->size - 0x0000C000;
177 info->start[i--] = base + info->size - 0x00010000;
179 info->start[i] = base + i * 0x00020000;
183 /*-----------------------------------------------------------------------
185 void flash_print_info(flash_info_t *info)
189 if (info->flash_id == FLASH_UNKNOWN) {
190 printf("missing or unknown FLASH type\n");
194 switch (info->flash_id & FLASH_VENDMASK) {
208 printf("Unknown Vendor ");
212 switch (info->flash_id & FLASH_TYPEMASK) {
214 printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
217 printf("AM29LV400T (4 Mbit, top boot sector)\n");
220 printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
223 printf("AM29LV800T (8 Mbit, top boot sector)\n");
226 printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
229 printf("AM29LV160T (16 Mbit, top boot sector)\n");
232 printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
235 printf("AM29LV320T (32 Mbit, top boot sector)\n");
238 printf("39xF200A (2M = 128K x 16)\n");
241 printf("39xF400A (4M = 256K x 16)\n");
244 printf("39xF800A (8M = 512K x 16)\n");
247 printf("M29W800AB (8M = 512K x 16)\n");
250 printf("Unknown Chip Type\n");
254 printf(" Size: %ld MB in %d Sectors\n",
255 info->size >> 20, info->sector_count);
257 printf(" Sector Start Addresses:");
258 for (i = 0; i < info->sector_count; ++i) {
263 info->protect[i] ? " (RO)" : " "
271 * The following code cannot be run from FLASH!
274 static ulong flash_get_size(vu_long *addr, flash_info_t *info)
278 ulong base = (ulong)addr;
280 /* Write auto select command: read Manufacturer ID */
281 #ifdef CONFIG_FLASH_16BIT
282 vu_short *s_addr = (vu_short *)addr;
283 s_addr[0x5555] = 0x00AA;
284 s_addr[0x2AAA] = 0x0055;
285 s_addr[0x5555] = 0x0090;
287 value = value|(value<<16);
289 addr[0x5555] = 0x00AA00AA;
290 addr[0x2AAA] = 0x00550055;
291 addr[0x5555] = 0x00900090;
297 info->flash_id = FLASH_MAN_AMD;
300 info->flash_id = FLASH_MAN_FUJ;
303 info->flash_id = FLASH_MAN_SST;
306 info->flash_id = FLASH_MAN_STM;
309 info->flash_id = FLASH_UNKNOWN;
310 info->sector_count = 0;
312 return 0; /* no or unknown flash */
314 #ifdef CONFIG_FLASH_16BIT
316 value = value|(value<<16);
318 value = addr[1]; /* device ID */
323 info->flash_id += FLASH_AM400T;
324 info->sector_count = 11;
325 info->size = 0x00100000;
329 info->flash_id += FLASH_AM400B;
330 info->sector_count = 11;
331 info->size = 0x00100000;
335 info->flash_id += FLASH_AM800T;
336 info->sector_count = 19;
337 info->size = 0x00200000;
341 info->flash_id += FLASH_AM800B;
342 #ifdef CONFIG_FLASH_16BIT
343 info->sector_count = 19;
344 info->size = 0x00100000; /* => 1 MB */
346 info->sector_count = 19;
347 info->size = 0x00200000; /* => 2 MB */
352 info->flash_id += FLASH_AM160T;
353 info->sector_count = 35;
354 info->size = 0x00400000;
358 info->flash_id += FLASH_AM160B;
359 #ifdef CONFIG_FLASH_16BIT
360 info->sector_count = 35;
361 info->size = 0x00200000; /* => 2 MB */
363 info->sector_count = 35;
364 info->size = 0x00400000; /* => 4 MB */
369 info->flash_id += FLASH_SST200A;
370 info->sector_count = 64; /* 39xF200A (2M = 128K x 16) */
371 info->size = 0x00080000;
374 info->flash_id += FLASH_SST400A;
375 info->sector_count = 128; /* 39xF400A (4M = 256K x 16) */
376 info->size = 0x00100000;
379 info->flash_id += FLASH_SST800A;
380 info->sector_count = 256; /* 39xF800A (8M = 512K x 16) */
381 info->size = 0x00200000;
384 info->flash_id += FLASH_STM800AB;
385 info->sector_count = 19;
386 info->size = 0x00200000;
389 info->flash_id = FLASH_UNKNOWN;
390 return 0; /* => no or unknown flash */
394 if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
395 printf("** ERROR: sector count %d > max (%d) **\n",
396 info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
397 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
400 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
401 for (i = 0; i < info->sector_count; i++)
402 info->start[i] = base + (i * 0x00002000);
403 } else { /* AMD and Fujitsu types */
404 /* set up sector start address table */
405 if (info->flash_id & FLASH_BTYPE) {
406 /* set sector offsets for bottom boot block type */
407 #ifdef CONFIG_FLASH_16BIT
409 info->start[0] = base + 0x00000000;
410 info->start[1] = base + 0x00004000;
411 info->start[2] = base + 0x00006000;
412 info->start[3] = base + 0x00008000;
413 for (i = 4; i < info->sector_count; i++)
414 info->start[i] = base +
415 (i * 0x00010000) - 0x00030000;
417 info->start[0] = base + 0x00000000;
418 info->start[1] = base + 0x00008000;
419 info->start[2] = base + 0x0000C000;
420 info->start[3] = base + 0x00010000;
421 for (i = 4; i < info->sector_count; i++)
422 info->start[i] = base +
423 (i * 0x00020000) - 0x00060000;
426 /* set sector offsets for top boot block type */
427 i = info->sector_count - 1;
428 info->start[i--] = base + info->size - 0x00008000;
429 info->start[i--] = base + info->size - 0x0000C000;
430 info->start[i--] = base + info->size - 0x00010000;
432 info->start[i] = base + i * 0x00020000;
435 /* check for protected sectors */
436 for (i = 0; i < info->sector_count; i++) {
438 * read sector protection at sector address:
440 * D0 = 1 if protected
442 #ifdef CONFIG_FLASH_16BIT
443 s_addr = (volatile unsigned short *)(info->start[i]);
444 info->protect[i] = s_addr[2] & 1;
446 addr = (volatile unsigned long *)(info->start[i]);
447 info->protect[i] = addr[2] & 1;
453 * Prevent writes to uninitialized FLASH.
455 if (info->flash_id != FLASH_UNKNOWN) {
456 #ifdef CONFIG_FLASH_16BIT
457 s_addr = (volatile unsigned short *)(info->start[0]);
458 *s_addr = 0x00F0; /* reset bank */
460 addr = (volatile unsigned long *)info->start[0];
461 *addr = 0x00F000F0; /* reset bank */
468 int flash_erase(flash_info_t *info, int s_first, int s_last)
470 vu_long *addr = (vu_long *)(info->start[0]);
471 int flag, prot, sect;
472 ulong start, now, last;
473 #ifdef CONFIG_FLASH_16BIT
474 vu_short *s_addr = (vu_short *)addr;
477 if ((s_first < 0) || (s_first > s_last)) {
478 if (info->flash_id == FLASH_UNKNOWN)
479 printf("- missing\n");
481 printf("- no sectors to erase\n");
484 /*#ifndef CONFIG_FLASH_16BIT
486 type = (info->flash_id & FLASH_VENDMASK);
487 if ((type != FLASH_MAN_SST) && (type != FLASH_MAN_STM)) {
488 printf ("Can't erase unknown flash type %08lx - aborted\n",
494 for (sect = s_first; sect <= s_last; ++sect) {
495 if (info->protect[sect])
500 printf("- Warning: %d protected sectors will not be erased!\n",
506 start = get_timer(0);
508 /* Start erase on unprotected sectors */
509 for (sect = s_first; sect <= s_last; sect++) {
510 if (info->protect[sect] == 0) { /* not protected */
511 #ifdef CONFIG_FLASH_16BIT
512 vu_short *s_sect_addr = (vu_short *)(info->start[sect]);
514 vu_long *sect_addr = (vu_long *)(info->start[sect]);
516 /* Disable interrupts which might cause a timeout */
517 flag = disable_interrupts();
519 #ifdef CONFIG_FLASH_16BIT
521 /*printf("\ns_sect_addr=%x",s_sect_addr);*/
522 s_addr[0x5555] = 0x00AA;
523 s_addr[0x2AAA] = 0x0055;
524 s_addr[0x5555] = 0x0080;
525 s_addr[0x5555] = 0x00AA;
526 s_addr[0x2AAA] = 0x0055;
527 s_sect_addr[0] = 0x0030;
529 addr[0x5555] = 0x00AA00AA;
530 addr[0x2AAA] = 0x00550055;
531 addr[0x5555] = 0x00800080;
532 addr[0x5555] = 0x00AA00AA;
533 addr[0x2AAA] = 0x00550055;
534 sect_addr[0] = 0x00300030;
536 /* re-enable interrupts if necessary */
540 /* wait at least 80us - let's wait 1 ms */
543 #ifdef CONFIG_FLASH_16BIT
544 while ((s_sect_addr[0] & 0x0080) != 0x0080) {
546 while ((sect_addr[0] & 0x00800080) != 0x00800080) {
548 now = get_timer(start);
549 if (now > CONFIG_SYS_FLASH_ERASE_TOUT) {
553 /* show every second that we're waiting */
554 if ((now - last) > 1000) {
562 /* reset to read mode */
563 addr = (volatile unsigned long *)info->start[0];
564 #ifdef CONFIG_FLASH_16BIT
565 s_addr[0] = 0x00F0; /* reset bank */
567 addr[0] = 0x00F000F0; /* reset bank */
574 /*-----------------------------------------------------------------------
575 * Copy memory to flash, returns:
578 * 2 - Flash not erased
579 * 4 - Flash not identified
582 int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
587 if (info->flash_id == FLASH_UNKNOWN)
590 wp = (addr & ~3); /* get lower word aligned address */
593 * handle unaligned start bytes
599 for (i = 0, cp = wp; i < l; ++i, ++cp)
600 data = (data << 8) | (*(uchar *)cp);
602 for (; i < 4 && cnt > 0; ++i) {
603 data = (data << 8) | *src++;
607 for (; cnt == 0 && i < 4; ++i, ++cp)
608 data = (data << 8) | (*(uchar *)cp);
610 rc = write_word(info, wp, data);
619 * handle word aligned part
623 for (i = 0; i < 4; ++i)
624 data = (data << 8) | *src++;
626 rc = write_word(info, wp, data);
638 * handle unaligned tail bytes
641 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
642 data = (data << 8) | *src++;
645 for (; i < 4; ++i, ++cp)
646 data = (data << 8) | (*(uchar *)cp);
648 return write_word(info, wp, data);
651 /*-----------------------------------------------------------------------
652 * Write a word to Flash, returns:
655 * 2 - Flash not erased
657 static int write_word(flash_info_t *info, ulong dest, ulong data)
659 vu_long *addr = (vu_long *)(info->start[0]);
661 #ifdef CONFIG_FLASH_16BIT
664 vu_short *s_addr = (vu_short *)addr;
669 /* Check if Flash is (sufficiently) erased */
670 if ((*((vu_long *)dest) & data) != data)
673 #ifdef CONFIG_FLASH_16BIT
674 /* Write the 16 higher-bits */
675 /* Disable interrupts which might cause a timeout here */
676 flag = disable_interrupts();
678 high_data = ((data>>16) & 0x0000ffff);
680 s_addr[0x5555] = 0x00AA;
681 s_addr[0x2AAA] = 0x0055;
682 s_addr[0x5555] = 0x00A0;
684 *((vu_short *)dest) = high_data;
686 /* re-enable interrupts if necessary */
690 /* data polling for D7 */
691 start = get_timer(0);
692 while ((*((vu_short *)dest) & 0x0080) != (high_data & 0x0080)) {
693 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT)
697 /* Write the 16 lower-bits */
700 /* Disable interrupts which might cause a timeout here */
701 flag = disable_interrupts();
702 #ifdef CONFIG_FLASH_16BIT
704 low_data = (data & 0x0000ffff);
706 s_addr[0x5555] = 0x00AA;
707 s_addr[0x2AAA] = 0x0055;
708 s_addr[0x5555] = 0x00A0;
709 *((vu_short *)dest) = low_data;
712 addr[0x5555] = 0x00AA00AA;
713 addr[0x2AAA] = 0x00550055;
714 addr[0x5555] = 0x00A000A0;
715 *((vu_long *)dest) = data;
718 /* re-enable interrupts if necessary */
722 /* data polling for D7 */
723 start = get_timer(0);
725 #ifdef CONFIG_FLASH_16BIT
726 while ((*((vu_short *)dest) & 0x0080) != (low_data & 0x0080)) {
728 while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
731 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT)