3 * Josh Huber <huber@mclx.com>, Mission Critical Linux, Inc.
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,
25 * flash.c - flash support for the 512k, 8bit boot flash on the GEVB
26 * most of this file was based on the existing U-Boot
32 #include <galileo/gt64260R.h>
33 #include <galileo/memory.h>
34 #include "intel_flash.h"
36 #define FLASH_ROM 0xFFFD /* unknown flash type */
37 #define FLASH_RAM 0xFFFE /* unknown flash type */
38 #define FLASH_MAN_UNKNOWN 0xFFFF0000
41 /* #define FLASH_ID_OVERRIDE */ /* Hack to set type to 040B if ROM emulator is installed.
42 * Can be used to program a ROM in circuit if a programmer
43 * is not available by swapping the rom out. */
45 /* Intel flash commands */
46 int flash_erase_intel(flash_info_t *info, int s_first, int s_last);
47 int write_word_intel(bank_addr_t addr, bank_word_t value);
49 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
51 /*-----------------------------------------------------------------------
54 static ulong flash_get_size (int portwidth, vu_long *addr, flash_info_t *info);
55 static int write_word (flash_info_t *info, ulong dest, ulong data);
56 static void flash_get_offsets (ulong base, flash_info_t *info);
57 static flash_info_t *flash_get_info(ulong base);
59 /*-----------------------------------------------------------------------
66 unsigned long size_b0 = 0, size_b1 = 0;
67 unsigned long base, flash_size;
69 /* Init: no FLASHes known */
70 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
71 flash_info[i].flash_id = FLASH_UNKNOWN;
75 base = CFG_FLASH_BASE;
76 #ifndef CFG_BOOT_FLASH_WIDTH
77 #define CFG_BOOT_FLASH_WIDTH 1
79 size_b0 = flash_get_size(CFG_BOOT_FLASH_WIDTH, (vu_long *)base,
82 printf("[%ldkB@%lx] ", size_b0/1024, base);
84 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
85 printf ("## Unknown FLASH at %08lx: Size = 0x%08lx = %ld MB\n",
86 base, size_b0, size_b0<<20);
89 base = memoryGetDeviceBaseAddress(CFG_EXTRA_FLASH_DEVICE);
90 for(i=1;i<CFG_MAX_FLASH_BANKS;i++) {
91 unsigned long size = flash_get_size(CFG_EXTRA_FLASH_WIDTH, (vu_long *)base, &flash_info[i]);
93 printf("[%ldMB@%lx] ", size>>20, base);
95 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
97 printf ("## Unknown FLASH at %08lx: Size = 0x%08lx = %ld MB\n",
98 base, size_b1, size_b1<<20);
106 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
107 /* monitor protection ON by default */
108 flash_protect(FLAG_PROTECT_SET,
110 CFG_MONITOR_BASE + monitor_flash_len - 1,
111 flash_get_info(CFG_MONITOR_BASE));
114 #ifdef CFG_ENV_IS_IN_FLASH
115 /* ENV protection ON by default */
116 flash_protect(FLAG_PROTECT_SET,
118 CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
119 flash_get_info(CFG_ENV_ADDR));
122 flash_size = size_b0 + size_b1;
126 /*-----------------------------------------------------------------------
129 flash_get_offsets (ulong base, flash_info_t *info)
134 if(!info->sector_count) return;
136 /* set up sector start address table */
137 switch(info->flash_id & FLASH_TYPEMASK) {
139 case FLASH_28F128J3A:
140 case FLASH_28F640J3A:
142 /* this chip has uniformly spaced sectors */
143 sector_size=info->size/info->sector_count;
144 for (i = 0; i < info->sector_count; i++)
145 info->start[i] = base + (i * sector_size);
148 if (info->flash_id & FLASH_BTYPE) {
149 /* set sector offsets for bottom boot block type */
150 info->start[0] = base + 0x00000000;
151 info->start[1] = base + 0x00008000;
152 info->start[2] = base + 0x0000C000;
153 info->start[3] = base + 0x00010000;
154 for (i = 4; i < info->sector_count; i++) {
155 info->start[i] = base + (i * 0x00020000) - 0x00060000;
158 /* set sector offsets for top boot block type */
159 i = info->sector_count - 1;
160 info->start[i--] = base + info->size - 0x00008000;
161 info->start[i--] = base + info->size - 0x0000C000;
162 info->start[i--] = base + info->size - 0x00010000;
163 for (; i >= 0; i--) {
164 info->start[i] = base + i * 0x00020000;
170 /*-----------------------------------------------------------------------
173 static flash_info_t *flash_get_info(ulong base)
178 for (i = 0; i < CFG_MAX_FLASH_BANKS; i ++) {
179 info = & flash_info[i];
180 if (info->start[0] <= base && base <= info->start[0] + info->size - 1)
184 return i == CFG_MAX_FLASH_BANKS ? 0 : info;
187 /*-----------------------------------------------------------------------
190 flash_print_info (flash_info_t *info)
194 if (info->flash_id == FLASH_UNKNOWN) {
195 printf ("missing or unknown FLASH type\n");
199 switch (info->flash_id & FLASH_VENDMASK) {
200 case FLASH_MAN_AMD: printf ("AMD "); break;
201 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
202 case FLASH_MAN_INTEL: printf ("INTEL "); break;
203 default: printf ("Unknown Vendor "); break;
206 switch (info->flash_id & FLASH_TYPEMASK) {
208 printf ("AM29LV040B (4 Mbit, bottom boot sect)\n");
211 printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
214 printf ("AM29LV400T (4 Mbit, top boot sector)\n");
217 printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
220 printf ("AM29LV800T (8 Mbit, top boot sector)\n");
223 printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
226 printf ("AM29LV160T (16 Mbit, top boot sector)\n");
229 printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
232 printf ("AM29LV320T (32 Mbit, top boot sector)\n");
234 case FLASH_28F640J3A:
235 printf ("28F640J3A (64 Mbit)\n");
237 case FLASH_28F128J3A:
238 printf ("28F128J3A (128 Mbit)\n");
247 printf ("Unknown Chip Type\n");
252 print_size (info->size, "");
253 printf (" in %d Sectors\n", info->sector_count);
255 printf (" Sector Start Addresses:");
256 for (i=0; i<info->sector_count; ++i) {
261 info->protect[i] ? " (RO)" : " "
268 /*-----------------------------------------------------------------------
272 /*-----------------------------------------------------------------------
276 * The following code cannot be run from FLASH!
279 static inline void flash_cmd(int width, volatile unsigned char *addr, int offset, unsigned char cmd)
281 /* supports 1x8, 1x16, and 2x16 */
282 /* 2x8 and 4x8 are not supported */
284 /* assuming chips are in 16 bit mode */
286 unsigned long cmd32=(cmd<<16)|cmd;
287 *(volatile unsigned long *)(addr+offset*2)=cmd32;
288 } else if (width == 2) {
290 *(volatile unsigned short *)((unsigned short*)addr+offset)=cmd;
293 *(volatile unsigned char *)(addr+offset)=cmd;
298 flash_get_size (int portwidth, vu_long *addr, flash_info_t *info)
301 volatile unsigned char *caddr = (unsigned char *)addr;
302 volatile unsigned short *saddr = (unsigned short *)addr;
303 volatile unsigned long *laddr = (unsigned long *)addr;
305 ulong id, manu, base = (ulong)addr;
307 info->portwidth=portwidth;
311 flash_cmd(portwidth,caddr,0,0xf0);
312 flash_cmd(portwidth,caddr,0,0xf0);
321 flash_cmd(portwidth,caddr,0,0xf0);
322 flash_cmd(portwidth,caddr,0,0xf0);
327 /* this area is ROM */
329 #ifndef FLASH_ID_OVERRIDE
330 info->flash_id = FLASH_ROM + FLASH_MAN_UNKNOWN;
331 info->sector_count = 8;
332 info->size = 0x80000;
334 info->flash_id = FLASH_MAN_AMD + FLASH_AM040;
335 info->sector_count = 8;
336 info->size = 0x80000;
339 flash_get_offsets(base, info);
348 /* this area is RAM */
350 info->flash_id = FLASH_RAM + FLASH_MAN_UNKNOWN;
351 info->sector_count = 8;
352 info->size = 0x80000;
353 flash_get_offsets(base, info);
356 flash_cmd(portwidth,caddr,0,0xf0);
361 /* Write auto select command: read Manufacturer ID */
362 flash_cmd(portwidth,caddr,0x555,0xAA);
363 flash_cmd(portwidth,caddr,0x2AA,0x55);
364 flash_cmd(portwidth,caddr,0x555,0x90);
368 if ((caddr[0] == old[0]) &&
369 (caddr[1] == old[1])) {
371 /* this area is ROM */
372 #ifndef FLASH_ID_OVERRIDE
373 info->flash_id = FLASH_ROM + FLASH_MAN_UNKNOWN;
374 info->sector_count = 8;
375 info->size = 0x80000;
377 info->flash_id = FLASH_MAN_AMD + FLASH_AM040;
378 info->sector_count = 8;
379 info->size = 0x80000;
382 flash_get_offsets(base, info);
386 printf("%px%d: %02x:%02x -> %02x:%02x\n",
387 caddr, portwidth, old[0], old[1],
414 printf("\n%08lx:%08lx:%08lx\n", base, manu, id);
415 printf("%08lx %08lx %08lx %08lx\n",
416 laddr[0],laddr[1],laddr[2],laddr[3]);
421 info->flash_id = FLASH_MAN_AMD;
424 info->flash_id = FLASH_MAN_FUJ;
427 info->flash_id = FLASH_MAN_INTEL;
430 printf("Unknown Mfr [%08lx]:%08lx\n", manu, id);
431 info->flash_id = FLASH_UNKNOWN;
432 info->sector_count = 0;
434 return (0); /* no or unknown flash */
439 info->flash_id += FLASH_AM400T;
440 info->sector_count = 11;
441 info->size = 0x00100000;
446 info->flash_id += FLASH_AM400B;
447 info->sector_count = 11;
448 info->size = 0x00100000;
453 info->flash_id += FLASH_AM800T;
454 info->sector_count = 19;
455 info->size = 0x00200000;
460 info->flash_id += FLASH_AM800B;
461 info->sector_count = 19;
462 info->size = 0x00200000;
467 info->flash_id += FLASH_AM160T;
468 info->sector_count = 35;
469 info->size = 0x00400000;
474 info->flash_id += FLASH_AM160B;
475 info->sector_count = 35;
476 info->size = 0x00400000;
479 #if 0 /* enable when device IDs are available */
481 info->flash_id += FLASH_AM320T;
482 info->sector_count = 67;
483 info->size = 0x00800000;
487 info->flash_id += FLASH_AM320B;
488 info->sector_count = 67;
489 info->size = 0x00800000;
493 info->flash_id += FLASH_AM040;
494 info->sector_count = 8;
495 info->size = 0x80000;
499 case INTEL_ID_28F640J3A:
500 info->flash_id += FLASH_28F640J3A;
501 info->sector_count = 64;
502 info->size = 128*1024 * 64; /* 128kbytes x 64 blocks */
504 if(portwidth==4) info->size*=2; /* 2x16 */
507 case INTEL_ID_28F128J3A:
508 info->flash_id += FLASH_28F128J3A;
509 info->sector_count = 128;
510 info->size = 128*1024 * 128; /* 128kbytes x 128 blocks */
512 if(portwidth==4) info->size*=2; /* 2x16 */
516 printf("Unknown id %lx:[%lx]\n", manu, id);
517 info->flash_id = FLASH_UNKNOWN;
519 return (0); /* => no or unknown flash */
523 flash_get_offsets(base, info);
526 /* set up sector start address table */
527 if (info->flash_id & FLASH_AM040) {
528 /* this chip has uniformly spaced sectors */
529 for (i = 0; i < info->sector_count; i++)
530 info->start[i] = base + (i * 0x00010000);
532 } else if (info->flash_id & FLASH_BTYPE) {
533 /* set sector offsets for bottom boot block type */
534 info->start[0] = base + 0x00000000;
535 info->start[1] = base + 0x00008000;
536 info->start[2] = base + 0x0000C000;
537 info->start[3] = base + 0x00010000;
538 for (i = 4; i < info->sector_count; i++) {
539 info->start[i] = base + (i * 0x00020000) - 0x00060000;
542 /* set sector offsets for top boot block type */
543 i = info->sector_count - 1;
544 info->start[i--] = base + info->size - 0x00008000;
545 info->start[i--] = base + info->size - 0x0000C000;
546 info->start[i--] = base + info->size - 0x00010000;
547 for (; i >= 0; i--) {
548 info->start[i] = base + i * 0x00020000;
553 /* check for protected sectors */
554 for (i = 0; i < info->sector_count; i++) {
555 /* read sector protection at sector address, (A7 .. A0)=0x02 */
556 /* D0 = 1 if protected */
557 caddr = (volatile unsigned char *)(info->start[i]);
558 saddr = (volatile unsigned short *)(info->start[i]);
559 laddr = (volatile unsigned long *)(info->start[i]);
561 info->protect[i] = caddr[2] & 1;
562 else if(portwidth==2)
563 info->protect[i] = saddr[2] & 1;
565 info->protect[i] = laddr[2] & 1;
569 * Prevent writes to uninitialized FLASH.
571 if (info->flash_id != FLASH_UNKNOWN) {
572 caddr = (volatile unsigned char *)info->start[0];
574 flash_cmd(portwidth,caddr,0,0xF0); /* reset bank */
580 /* TODO: 2x16 unsupported */
582 flash_erase (flash_info_t *info, int s_first, int s_last)
584 volatile unsigned char *addr = (char *)(info->start[0]);
585 int flag, prot, sect, l_sect;
586 ulong start, now, last;
588 /* TODO: 2x16 unsupported */
589 if(info->portwidth==4) return 1;
591 if((info->flash_id & FLASH_TYPEMASK) == FLASH_ROM) return 1;
592 if((info->flash_id & FLASH_TYPEMASK) == FLASH_RAM) {
593 for (sect = s_first; sect<=s_last; sect++) {
594 int sector_size=info->size/info->sector_count;
595 addr = (char *)(info->start[sect]);
596 memset((void *)addr, 0, sector_size);
601 if ((s_first < 0) || (s_first > s_last)) {
602 if (info->flash_id == FLASH_UNKNOWN) {
603 printf ("- missing\n");
605 printf ("- no sectors to erase\n");
610 if ((info->flash_id&FLASH_VENDMASK) == FLASH_MAN_INTEL) {
611 return flash_erase_intel(info,
612 (unsigned short)s_first,
613 (unsigned short)s_last);
617 if ((info->flash_id == FLASH_UNKNOWN) ||
618 (info->flash_id > FLASH_AMD_COMP)) {
619 printf ("Can't erase unknown flash type %08lx - aborted\n",
626 for (sect=s_first; sect<=s_last; ++sect) {
627 if (info->protect[sect]) {
633 printf ("- Warning: %d protected sectors will not be erased!\n",
641 /* Disable interrupts which might cause a timeout here */
642 flag = disable_interrupts();
644 flash_cmd(info->portwidth,addr,0x555,0xAA);
645 flash_cmd(info->portwidth,addr,0x2AA,0x55);
646 flash_cmd(info->portwidth,addr,0x555,0x80);
647 flash_cmd(info->portwidth,addr,0x555,0xAA);
648 flash_cmd(info->portwidth,addr,0x2AA,0x55);
650 /* Start erase on unprotected sectors */
651 for (sect = s_first; sect<=s_last; sect++) {
652 if (info->protect[sect] == 0) { /* not protected */
653 addr = (char *)(info->start[sect]);
654 flash_cmd(info->portwidth,addr,0,0x30);
659 /* re-enable interrupts if necessary */
663 /* wait at least 80us - let's wait 1 ms */
667 * We wait for the last triggered sector
672 start = get_timer (0);
674 addr = (volatile unsigned char *)(info->start[l_sect]);
675 /* broken for 2x16: TODO */
676 while ((addr[0] & 0x80) != 0x80) {
677 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
678 printf ("Timeout\n");
681 /* show that we're waiting */
682 if ((now - last) > 1000) { /* every second */
689 /* reset to read mode */
690 addr = (volatile unsigned char *)info->start[0];
691 flash_cmd(info->portwidth,addr,0,0xf0);
692 flash_cmd(info->portwidth,addr,0,0xf0);
698 /*-----------------------------------------------------------------------
699 * Copy memory to flash, returns:
702 * 2 - Flash not erased
705 /* broken for 2x16: TODO */
707 write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
712 if(info->portwidth==4) return 1;
714 if((info->flash_id & FLASH_TYPEMASK) == FLASH_ROM) return 0;
715 if((info->flash_id & FLASH_TYPEMASK) == FLASH_RAM) {
716 memcpy((void *)addr, src, cnt);
720 wp = (addr & ~3); /* get lower word aligned address */
723 * handle unaligned start bytes
725 if ((l = addr - wp) != 0) {
727 for (i=0, cp=wp; i<l; ++i, ++cp) {
728 data = (data << 8) | (*(uchar *)cp);
730 for (; i<4 && cnt>0; ++i) {
731 data = (data << 8) | *src++;
735 for (; cnt==0 && i<4; ++i, ++cp) {
736 data = (data << 8) | (*(uchar *)cp);
739 if ((rc = write_word(info, wp, data)) != 0) {
746 * handle word aligned part
750 for (i=0; i<4; ++i) {
751 data = (data << 8) | *src++;
753 if ((rc = write_word(info, wp, data)) != 0) {
765 * handle unaligned tail bytes
768 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
769 data = (data << 8) | *src++;
772 for (; i<4; ++i, ++cp) {
773 data = (data << 8) | (*(uchar *)cp);
776 return (write_word(info, wp, data));
779 /*-----------------------------------------------------------------------
780 * Write a word to Flash, returns:
783 * 2 - Flash not erased
785 /* broken for 2x16: TODO */
787 write_word (flash_info_t *info, ulong dest, ulong data)
789 volatile unsigned char *addr = (char *)(info->start[0]);
793 if(info->portwidth==4) return 1;
795 if((info->flash_id & FLASH_TYPEMASK) == FLASH_ROM) return 1;
796 if((info->flash_id & FLASH_TYPEMASK) == FLASH_RAM) {
797 *(unsigned long *)dest=data;
800 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
801 unsigned short low = data & 0xffff;
802 unsigned short hi = (data >> 16) & 0xffff;
803 int ret = write_word_intel((bank_addr_t)dest, hi);
805 if (!ret) ret = write_word_intel((bank_addr_t)(dest+2), low);
810 /* Check if Flash is (sufficiently) erased */
811 if ((*((vu_long *)dest) & data) != data) {
814 /* Disable interrupts which might cause a timeout here */
815 flag = disable_interrupts();
817 /* first, perform an unlock bypass command to speed up flash writes */
822 /* write each byte out */
823 for (i = 0; i < 4; i++) {
824 char *data_ch = (char *)&data;
826 *(((char *)dest)+i) = data_ch[i];
827 udelay(10); /* XXX */
830 /* we're done, now do an unlock bypass reset */
834 /* re-enable interrupts if necessary */
838 /* data polling for D7 */
839 start = get_timer (0);
840 while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
841 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {