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,
23 * Hacked for the Hymod board by Murray.Jensen@cmst.csiro.au, 20-Oct-00
28 #include <board/hymod/flash.h>
30 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
32 /*-----------------------------------------------------------------------
35 #define FLAG_PROTECT_SET 0x01
36 #define FLAG_PROTECT_CLEAR 0x02
38 /*-----------------------------------------------------------------------
42 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
43 static void flash_get_offsets (ulong base, flash_info_t *info);
45 static int write_word (flash_info_t *info, ulong dest, ulong data);
47 /*-----------------------------------------------------------------------
51 * probe for the existence of flash at bank word address "addr"
52 * 0 = yes, 1 = bad Manufacturer's Id, 2 = bad Device Id
55 bank_probe_word(bank_addr_t addr)
62 /* check the manufacturer id */
63 *addr = BANK_CMD_RD_ID;
64 if (*BANK_ADDR_REG_MAN(addr) != BANK_RD_ID_MAN) {
69 /* check the device id */
70 *addr = BANK_CMD_RD_ID;
71 if (*BANK_ADDR_REG_DEV(addr) != BANK_RD_ID_DEV) {
76 retval = CFG_FLASH_TYPE;
79 /* reset the flash again */
86 * probe for flash banks at address "base" and store info for any found
87 * into flash_info entry "fip". Must find at least one bank.
90 bank_probe(flash_info_t *fip, bank_addr_t base)
92 bank_addr_t addr, eaddr;
95 fip->flash_id = FLASH_UNKNOWN;
97 fip->sector_count = 0;
100 eaddr = BANK_ADDR_BASE(addr, MAX_BANKS);
103 while (addr < eaddr) {
104 bank_addr_t addrw, eaddrw, addrb;
105 int i, osc, nsc, curtype = -1;
108 eaddrw = BANK_ADDR_NEXT_WORD(addrw);
110 while (addrw < eaddrw) {
114 printf(" probing for flash at addr 0x%08lx\n",
115 (unsigned long)addrw);
117 if ((thistype = bank_probe_word(addrw++)) < 0)
123 if (thistype != curtype) {
124 printf("Differing flash type found!\n");
133 /* bank exists - append info for this bank to *fip */
134 fip->flash_id = FLASH_MAN_INTEL|curtype;
135 fip->size += BANK_SIZE;
136 osc = fip->sector_count;
137 fip->sector_count += BANK_NBLOCKS;
138 if ((nsc = fip->sector_count) >= CFG_MAX_FLASH_SECT)
139 panic("Too many sectors in flash at address 0x%08lx\n",
140 (unsigned long)base);
143 for (i = osc; i < nsc; i++) {
144 fip->start[i] = (ulong)addrb;
146 addrb = BANK_ADDR_NEXT_BLK(addrb);
149 addr = BANK_ADDR_NEXT_BANK(addr);
155 panic("ERROR: no flash found at address 0x%08lx\n",
156 (unsigned long)base);
160 bank_reset(flash_info_t *info, int sect)
162 bank_addr_t addrw, eaddrw;
164 addrw = (bank_addr_t)info->start[sect];
165 eaddrw = BANK_ADDR_NEXT_WORD(addrw);
167 while (addrw < eaddrw) {
169 printf(" writing reset cmd to addr 0x%08lx\n",
170 (unsigned long)addrw);
172 *addrw = BANK_CMD_RST;
178 bank_erase_init(flash_info_t *info, int sect)
180 bank_addr_t addrw, saddrw, eaddrw;
184 printf("0x%08lx BANK_CMD_PROG\n", BANK_CMD_PROG);
185 printf("0x%08lx BANK_CMD_ERASE1\n", BANK_CMD_ERASE1);
186 printf("0x%08lx BANK_CMD_ERASE2\n", BANK_CMD_ERASE2);
187 printf("0x%08lx BANK_CMD_CLR_STAT\n", BANK_CMD_CLR_STAT);
188 printf("0x%08lx BANK_CMD_RST\n", BANK_CMD_RST);
189 printf("0x%08lx BANK_STAT_RDY\n", BANK_STAT_RDY);
190 printf("0x%08lx BANK_STAT_ERR\n", BANK_STAT_ERR);
193 saddrw = (bank_addr_t)info->start[sect];
194 eaddrw = BANK_ADDR_NEXT_WORD(saddrw);
197 printf("erasing sector %d, start addr = 0x%08lx "
198 "(bank next word addr = 0x%08lx)\n", sect,
199 (unsigned long)saddrw, (unsigned long)eaddrw);
202 /* Disable intrs which might cause a timeout here */
203 flag = disable_interrupts();
205 for (addrw = saddrw; addrw < eaddrw; addrw++) {
207 printf(" writing erase cmd to addr 0x%08lx\n",
208 (unsigned long)addrw);
210 *addrw = BANK_CMD_ERASE1;
211 *addrw = BANK_CMD_ERASE2;
214 /* re-enable interrupts if necessary */
220 bank_erase_poll(flash_info_t *info, int sect)
222 bank_addr_t addrw, saddrw, eaddrw;
223 int sectdone, haderr;
225 saddrw = (bank_addr_t)info->start[sect];
226 eaddrw = BANK_ADDR_NEXT_WORD(saddrw);
231 for (addrw = saddrw; addrw < eaddrw; addrw++) {
232 bank_word_t stat = *addrw;
235 printf(" checking status at addr "
236 "0x%08lx [0x%08lx]\n",
237 (unsigned long)addrw, stat);
239 if ((stat & BANK_STAT_RDY) != BANK_STAT_RDY)
241 else if ((stat & BANK_STAT_ERR) != 0) {
242 printf(" failed on sector %d "
243 "(stat = 0x%08lx) at "
246 (unsigned long)addrw);
247 *addrw = BANK_CMD_CLR_STAT;
259 bank_write_word(bank_addr_t addr, bank_word_t value)
265 /* Disable interrupts which might cause a timeout here */
266 flag = disable_interrupts();
268 *addr = BANK_CMD_PROG;
272 /* re-enable interrupts if necessary */
278 /* data polling for D7 */
279 start = get_timer (0);
281 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
286 } while ((stat & BANK_STAT_RDY) != BANK_STAT_RDY);
288 if ((stat & BANK_STAT_ERR) != 0) {
289 printf("flash program failed (stat = 0x%08lx) "
290 "at address 0x%08lx\n", (ulong)stat, (ulong)addr);
291 *addr = BANK_CMD_CLR_STAT;
296 /* reset to read mode */
297 *addr = BANK_CMD_RST;
302 /*-----------------------------------------------------------------------
310 /* Init: no FLASHes known */
311 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
312 flash_info[i].flash_id = FLASH_UNKNOWN;
315 bank_probe(&flash_info[0], (bank_addr_t)CFG_FLASH_BASE);
318 * protect monitor and environment sectors
321 #if CFG_MONITOR_BASE == CFG_FLASH_BASE
322 (void)flash_protect(FLAG_PROTECT_SET,
324 CFG_MONITOR_BASE+CFG_MONITOR_LEN-1,
328 #if defined(CFG_FLASH_ENV_ADDR)
329 (void)flash_protect(FLAG_PROTECT_SET,
331 #if defined(CFG_FLASH_ENV_BUF)
332 CFG_FLASH_ENV_ADDR + CFG_FLASH_ENV_BUF - 1,
334 CFG_FLASH_ENV_ADDR + CFG_FLASH_ENV_SIZE - 1,
339 return flash_info[0].size;
342 /*-----------------------------------------------------------------------
346 flash_get_offsets(ulong base, flash_info_t *info)
350 /* set up sector start adress table */
351 if (info->flash_id & FLASH_BTYPE) {
352 /* set sector offsets for bottom boot block type */
353 info->start[0] = base + 0x00000000;
354 info->start[1] = base + 0x00008000;
355 info->start[2] = base + 0x0000C000;
356 info->start[3] = base + 0x00010000;
357 for (i = 4; i < info->sector_count; i++) {
358 info->start[i] = base + (i * 0x00020000) - 0x00060000;
361 /* set sector offsets for top boot block type */
362 i = info->sector_count - 1;
363 info->start[i--] = base + info->size - 0x00008000;
364 info->start[i--] = base + info->size - 0x0000C000;
365 info->start[i--] = base + info->size - 0x00010000;
366 for (; i >= 0; i--) {
367 info->start[i] = base + i * 0x00020000;
374 /*-----------------------------------------------------------------------
377 flash_print_info(flash_info_t *info)
381 if (info->flash_id == FLASH_UNKNOWN) {
382 printf ("missing or unknown FLASH type\n");
386 switch (info->flash_id & FLASH_VENDMASK) {
387 case FLASH_MAN_INTEL: printf ("INTEL "); break;
388 default: printf ("Unknown Vendor "); break;
391 switch (info->flash_id & FLASH_TYPEMASK) {
392 case FLASH_28F320J5: printf ("28F320J5 (32 Mbit, 2 x 16bit)\n");
394 default: printf ("Unknown Chip Type\n");
398 printf (" Size: %ld MB in %d Sectors\n",
399 info->size >> 20, info->sector_count);
401 printf (" Sector Start Addresses:");
402 for (i=0; i<info->sector_count; ++i) {
407 info->protect[i] ? " (RO)" : " "
414 /*-----------------------------------------------------------------------
418 * The following code cannot be run from FLASH!
422 flash_get_size(vu_long *addr, flash_info_t *info)
426 ulong base = (ulong)addr;
429 /* Write auto select command: read Manufacturer ID */
430 addr[0x0555] = 0x00AA00AA;
431 addr[0x02AA] = 0x00550055;
432 addr[0x0555] = 0x00900090;
438 info->flash_id = FLASH_MAN_AMD;
441 info->flash_id = FLASH_MAN_FUJ;
444 info->flash_id = FLASH_UNKNOWN;
445 info->sector_count = 0;
447 return (0); /* no or unknown flash */
450 value = addr[1]; /* device ID */
454 info->flash_id += FLASH_AM400T;
455 info->sector_count = 11;
456 info->size = 0x00100000;
460 info->flash_id += FLASH_AM400B;
461 info->sector_count = 11;
462 info->size = 0x00100000;
466 info->flash_id += FLASH_AM800T;
467 info->sector_count = 19;
468 info->size = 0x00200000;
472 info->flash_id += FLASH_AM800B;
473 info->sector_count = 19;
474 info->size = 0x00200000;
478 info->flash_id += FLASH_AM160T;
479 info->sector_count = 35;
480 info->size = 0x00400000;
484 info->flash_id += FLASH_AM160B;
485 info->sector_count = 35;
486 info->size = 0x00400000;
488 #if 0 /* enable when device IDs are available */
490 info->flash_id += FLASH_AM320T;
491 info->sector_count = 67;
492 info->size = 0x00800000;
496 info->flash_id += FLASH_AM320B;
497 info->sector_count = 67;
498 info->size = 0x00800000;
502 info->flash_id = FLASH_UNKNOWN;
503 return (0); /* => no or unknown flash */
507 /* set up sector start adress table */
508 if (info->flash_id & FLASH_BTYPE) {
509 /* set sector offsets for bottom boot block type */
510 info->start[0] = base + 0x00000000;
511 info->start[1] = base + 0x00008000;
512 info->start[2] = base + 0x0000C000;
513 info->start[3] = base + 0x00010000;
514 for (i = 4; i < info->sector_count; i++) {
515 info->start[i] = base + (i * 0x00020000) - 0x00060000;
518 /* set sector offsets for top boot block type */
519 i = info->sector_count - 1;
520 info->start[i--] = base + info->size - 0x00008000;
521 info->start[i--] = base + info->size - 0x0000C000;
522 info->start[i--] = base + info->size - 0x00010000;
523 for (; i >= 0; i--) {
524 info->start[i] = base + i * 0x00020000;
528 /* check for protected sectors */
529 for (i = 0; i < info->sector_count; i++) {
530 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
531 /* D0 = 1 if protected */
532 addr = (volatile unsigned long *)(info->start[i]);
533 info->protect[i] = addr[2] & 1;
537 * Prevent writes to uninitialized FLASH.
539 if (info->flash_id != FLASH_UNKNOWN) {
540 addr = (volatile unsigned long *)info->start[0];
542 *addr = 0x00F000F0; /* reset bank */
550 /*-----------------------------------------------------------------------
554 flash_erase(flash_info_t *info, int s_first, int s_last)
556 int prot, sect, haderr;
557 ulong start, now, last;
561 printf("\nflash_erase: erase %d sectors (%d to %d incl.) from\n"
562 " Bank # %d: ", s_last - s_first + 1, s_first, s_last,
563 (info - flash_info) + 1);
564 flash_print_info(info);
567 if ((s_first < 0) || (s_first > s_last)) {
568 if (info->flash_id == FLASH_UNKNOWN) {
569 printf ("- missing\n");
571 printf ("- no sectors to erase\n");
577 for (sect=s_first; sect<=s_last; ++sect) {
578 if (info->protect[sect]) {
584 printf("- Warning: %d protected sector%s will not be erased!\n",
585 prot, (prot > 1 ? "s" : ""));
588 start = get_timer (0);
592 for (sect = s_first; sect <= s_last; sect++) {
593 if (info->protect[sect] == 0) { /* not protected */
597 bank_erase_init(info, sect);
599 /* wait at least 80us - let's wait 1 ms */
602 estart = get_timer(start);
605 now = get_timer(start);
607 if (now - estart > CFG_FLASH_ERASE_TOUT) {
608 printf ("Timeout (sect %d)\n", sect);
615 /* show that we're waiting */
616 if ((now - last) > 1000) { /* every second */
622 sectdone = bank_erase_poll(info, sect);
638 printf (" failed\n");
642 /* reset to read mode */
643 for (sect = s_first; sect <= s_last; sect++) {
644 if (info->protect[sect] == 0) { /* not protected */
645 bank_reset(info, sect);
651 /*-----------------------------------------------------------------------
652 * Copy memory to flash, returns:
655 * 2 - Flash not erased
659 write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
664 wp = (addr & ~3); /* get lower word aligned address */
667 * handle unaligned start bytes
669 if ((l = addr - wp) != 0) {
671 for (i=0, cp=wp; i<l; ++i, ++cp) {
672 data = (data << 8) | (*(uchar *)cp);
674 for (; i<4 && cnt>0; ++i) {
675 data = (data << 8) | *src++;
679 for (; cnt==0 && i<4; ++i, ++cp) {
680 data = (data << 8) | (*(uchar *)cp);
683 if ((rc = write_word(info, wp, data)) != 0) {
690 * handle word aligned part
694 for (i=0; i<4; ++i) {
695 data = (data << 8) | *src++;
697 if ((rc = write_word(info, wp, data)) != 0) {
709 * handle unaligned tail bytes
712 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
713 data = (data << 8) | *src++;
716 for (; i<4; ++i, ++cp) {
717 data = (data << 8) | (*(uchar *)cp);
720 return (write_word(info, wp, data));
723 /*-----------------------------------------------------------------------
724 * Write a word to Flash, returns:
727 * 2 - Flash not erased
730 write_word(flash_info_t *info, ulong dest, ulong data)
734 /* Check if Flash is (sufficiently) erased */
735 if ((*(ulong *)dest & data) != data) {
739 retval = bank_write_word((bank_addr_t)dest, (bank_word_t)data);
744 /*-----------------------------------------------------------------------