blob: ee052e39593dd242ec3513a98bfb1e4a5a376142 [file] [log] [blame]
wdenkaffae2b2002-08-17 09:36:01 +00001/*
2 * (C) Copyright 2000
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
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.
12 *
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.
17 *
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,
21 * MA 02111-1307 USA
22 *
23 * Hacked for the Hymod board by Murray.Jensen@cmst.csiro.au, 20-Oct-00
24 */
25
26#include <common.h>
27#include <mpc8260.h>
28#include <board/hymod/flash.h>
29
30flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
31
32/*-----------------------------------------------------------------------
33 * Protection Flags:
34 */
35#define FLAG_PROTECT_SET 0x01
36#define FLAG_PROTECT_CLEAR 0x02
37
38/*-----------------------------------------------------------------------
39 * Functions
40 */
41#if 0
42static ulong flash_get_size (vu_long *addr, flash_info_t *info);
43static void flash_get_offsets (ulong base, flash_info_t *info);
44#endif
45static int write_word (flash_info_t *info, ulong dest, ulong data);
46
47/*-----------------------------------------------------------------------
48 */
49
50/*
51 * probe for the existence of flash at bank word address "addr"
52 * 0 = yes, 1 = bad Manufacturer's Id, 2 = bad Device Id
53 */
54static int
55bank_probe_word(bank_addr_t addr)
56{
57 int retval;
58
59 /* reset the flash */
60 *addr = BANK_CMD_RST;
61
62 /* check the manufacturer id */
63 *addr = BANK_CMD_RD_ID;
64 if (*BANK_ADDR_REG_MAN(addr) != BANK_RD_ID_MAN) {
65 retval = -1;
66 goto out;
67 }
68
69 /* check the device id */
70 *addr = BANK_CMD_RD_ID;
71 if (*BANK_ADDR_REG_DEV(addr) != BANK_RD_ID_DEV) {
72 retval = -2;
73 goto out;
74 }
75
76 retval = CFG_FLASH_TYPE;
77
78out:
79 /* reset the flash again */
80 *addr = BANK_CMD_RST;
81
82 return retval;
83}
84
85/*
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.
88 */
89static void
90bank_probe(flash_info_t *fip, bank_addr_t base)
91{
92 bank_addr_t addr, eaddr;
93 int nbanks;
94
95 fip->flash_id = FLASH_UNKNOWN;
96 fip->size = 0L;
97 fip->sector_count = 0;
98
99 addr = base;
100 eaddr = BANK_ADDR_BASE(addr, MAX_BANKS);
101 nbanks = 0;
102
103 while (addr < eaddr) {
104 bank_addr_t addrw, eaddrw, addrb;
105 int i, osc, nsc, curtype = -1;
106
107 addrw = addr;
108 eaddrw = BANK_ADDR_NEXT_WORD(addrw);
109
110 while (addrw < eaddrw) {
111 int thistype;
112
113#ifdef FLASH_DEBUG
114 printf(" probing for flash at addr 0x%08lx\n",
115 (unsigned long)addrw);
116#endif
117 if ((thistype = bank_probe_word(addrw++)) < 0)
118 goto out;
119
120 if (curtype < 0)
121 curtype = thistype;
122 else {
123 if (thistype != curtype) {
124 printf("Differing flash type found!\n");
125 goto out;
126 }
127 }
128 }
129
130 if (curtype < 0)
131 goto out;
132
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);
141
142 addrb = addr;
143 for (i = osc; i < nsc; i++) {
144 fip->start[i] = (ulong)addrb;
145 fip->protect[i] = 0;
146 addrb = BANK_ADDR_NEXT_BLK(addrb);
147 }
148
149 addr = BANK_ADDR_NEXT_BANK(addr);
150 nbanks++;
151 }
152
153out:
154 if (nbanks == 0)
155 panic("ERROR: no flash found at address 0x%08lx\n",
156 (unsigned long)base);
157}
158
159static void
160bank_reset(flash_info_t *info, int sect)
161{
162 bank_addr_t addrw, eaddrw;
163
164 addrw = (bank_addr_t)info->start[sect];
165 eaddrw = BANK_ADDR_NEXT_WORD(addrw);
166
167 while (addrw < eaddrw) {
168#ifdef FLASH_DEBUG
169 printf(" writing reset cmd to addr 0x%08lx\n",
170 (unsigned long)addrw);
171#endif
172 *addrw = BANK_CMD_RST;
173 addrw++;
174 }
175}
176
177static void
178bank_erase_init(flash_info_t *info, int sect)
179{
180 bank_addr_t addrw, saddrw, eaddrw;
181 int flag;
182
183#ifdef FLASH_DEBUG
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);
191#endif
192
193 saddrw = (bank_addr_t)info->start[sect];
194 eaddrw = BANK_ADDR_NEXT_WORD(saddrw);
195
196#ifdef FLASH_DEBUG
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);
200#endif
201
202 /* Disable intrs which might cause a timeout here */
203 flag = disable_interrupts();
204
205 for (addrw = saddrw; addrw < eaddrw; addrw++) {
206#ifdef FLASH_DEBUG
207 printf(" writing erase cmd to addr 0x%08lx\n",
208 (unsigned long)addrw);
209#endif
210 *addrw = BANK_CMD_ERASE1;
211 *addrw = BANK_CMD_ERASE2;
212 }
213
214 /* re-enable interrupts if necessary */
215 if (flag)
216 enable_interrupts();
217}
218
219static int
220bank_erase_poll(flash_info_t *info, int sect)
221{
222 bank_addr_t addrw, saddrw, eaddrw;
223 int sectdone, haderr;
224
225 saddrw = (bank_addr_t)info->start[sect];
226 eaddrw = BANK_ADDR_NEXT_WORD(saddrw);
227
228 sectdone = 1;
229 haderr = 0;
230
231 for (addrw = saddrw; addrw < eaddrw; addrw++) {
232 bank_word_t stat = *addrw;
233
234#ifdef FLASH_DEBUG
235 printf(" checking status at addr "
236 "0x%08lx [0x%08lx]\n",
237 (unsigned long)addrw, stat);
238#endif
239 if ((stat & BANK_STAT_RDY) != BANK_STAT_RDY)
240 sectdone = 0;
241 else if ((stat & BANK_STAT_ERR) != 0) {
242 printf(" failed on sector %d "
243 "(stat = 0x%08lx) at "
244 "address 0x%08lx\n",
245 sect, stat,
246 (unsigned long)addrw);
247 *addrw = BANK_CMD_CLR_STAT;
248 haderr = 1;
249 }
250 }
251
252 if (haderr)
253 return (-1);
254 else
255 return (sectdone);
256}
257
258static int
259bank_write_word(bank_addr_t addr, bank_word_t value)
260{
261 bank_word_t stat;
262 ulong start;
263 int flag, retval;
264
265 /* Disable interrupts which might cause a timeout here */
266 flag = disable_interrupts();
267
268 *addr = BANK_CMD_PROG;
269
270 *addr = value;
271
272 /* re-enable interrupts if necessary */
273 if (flag)
274 enable_interrupts();
275
276 retval = 0;
277
278 /* data polling for D7 */
279 start = get_timer (0);
280 do {
281 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
282 retval = 1;
283 goto done;
284 }
285 stat = *addr;
286 } while ((stat & BANK_STAT_RDY) != BANK_STAT_RDY);
287
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;
292 retval = 3;
293 }
294
295done:
296 /* reset to read mode */
297 *addr = BANK_CMD_RST;
298
299 return (retval);
300}
301
302/*-----------------------------------------------------------------------
303 */
304
305unsigned long
306flash_init(void)
307{
308 int i;
309
310 /* Init: no FLASHes known */
311 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
312 flash_info[i].flash_id = FLASH_UNKNOWN;
313 }
314
315 bank_probe(&flash_info[0], (bank_addr_t)CFG_FLASH_BASE);
316
317 /*
318 * protect monitor and environment sectors
319 */
320
321#if CFG_MONITOR_BASE == CFG_FLASH_BASE
322 (void)flash_protect(FLAG_PROTECT_SET,
323 CFG_MONITOR_BASE,
324 CFG_MONITOR_BASE+CFG_MONITOR_LEN-1,
325 &flash_info[0]);
326#endif
327
328#if defined(CFG_FLASH_ENV_ADDR)
329 (void)flash_protect(FLAG_PROTECT_SET,
330 CFG_FLASH_ENV_ADDR,
331#if defined(CFG_FLASH_ENV_BUF)
332 CFG_FLASH_ENV_ADDR + CFG_FLASH_ENV_BUF - 1,
333#else
334 CFG_FLASH_ENV_ADDR + CFG_FLASH_ENV_SIZE - 1,
335#endif
336 &flash_info[0]);
337#endif
338
339 return flash_info[0].size;
340}
341
342/*-----------------------------------------------------------------------
343 */
344#if 0
345static void
346flash_get_offsets(ulong base, flash_info_t *info)
347{
348 int i;
349
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;
359 }
360 } else {
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;
368 }
369 }
370
371}
372#endif /* 0 */
373
374/*-----------------------------------------------------------------------
375 */
376void
377flash_print_info(flash_info_t *info)
378{
379 int i;
380
381 if (info->flash_id == FLASH_UNKNOWN) {
382 printf ("missing or unknown FLASH type\n");
383 return;
384 }
385
386 switch (info->flash_id & FLASH_VENDMASK) {
387 case FLASH_MAN_INTEL: printf ("INTEL "); break;
388 default: printf ("Unknown Vendor "); break;
389 }
390
391 switch (info->flash_id & FLASH_TYPEMASK) {
392 case FLASH_28F320J5: printf ("28F320J5 (32 Mbit, 2 x 16bit)\n");
393 break;
394 default: printf ("Unknown Chip Type\n");
395 break;
396 }
397
398 printf (" Size: %ld MB in %d Sectors\n",
399 info->size >> 20, info->sector_count);
400
401 printf (" Sector Start Addresses:");
402 for (i=0; i<info->sector_count; ++i) {
403 if ((i % 5) == 0)
404 printf ("\n ");
405 printf (" %08lX%s",
406 info->start[i],
407 info->protect[i] ? " (RO)" : " "
408 );
409 }
410 printf ("\n");
411 return;
412}
413
414/*-----------------------------------------------------------------------
415 */
416
417/*
418 * The following code cannot be run from FLASH!
419 */
420#if 0
421static ulong
422flash_get_size(vu_long *addr, flash_info_t *info)
423{
424 short i;
425 ulong value;
426 ulong base = (ulong)addr;
427
428
429 /* Write auto select command: read Manufacturer ID */
430 addr[0x0555] = 0x00AA00AA;
431 addr[0x02AA] = 0x00550055;
432 addr[0x0555] = 0x00900090;
433
434 value = addr[0];
435
436 switch (value) {
437 case AMD_MANUFACT:
438 info->flash_id = FLASH_MAN_AMD;
439 break;
440 case FUJ_MANUFACT:
441 info->flash_id = FLASH_MAN_FUJ;
442 break;
443 default:
444 info->flash_id = FLASH_UNKNOWN;
445 info->sector_count = 0;
446 info->size = 0;
447 return (0); /* no or unknown flash */
448 }
449
450 value = addr[1]; /* device ID */
451
452 switch (value) {
453 case AMD_ID_LV400T:
454 info->flash_id += FLASH_AM400T;
455 info->sector_count = 11;
456 info->size = 0x00100000;
457 break; /* => 1 MB */
458
459 case AMD_ID_LV400B:
460 info->flash_id += FLASH_AM400B;
461 info->sector_count = 11;
462 info->size = 0x00100000;
463 break; /* => 1 MB */
464
465 case AMD_ID_LV800T:
466 info->flash_id += FLASH_AM800T;
467 info->sector_count = 19;
468 info->size = 0x00200000;
469 break; /* => 2 MB */
470
471 case AMD_ID_LV800B:
472 info->flash_id += FLASH_AM800B;
473 info->sector_count = 19;
474 info->size = 0x00200000;
475 break; /* => 2 MB */
476
477 case AMD_ID_LV160T:
478 info->flash_id += FLASH_AM160T;
479 info->sector_count = 35;
480 info->size = 0x00400000;
481 break; /* => 4 MB */
482
483 case AMD_ID_LV160B:
484 info->flash_id += FLASH_AM160B;
485 info->sector_count = 35;
486 info->size = 0x00400000;
487 break; /* => 4 MB */
488#if 0 /* enable when device IDs are available */
489 case AMD_ID_LV320T:
490 info->flash_id += FLASH_AM320T;
491 info->sector_count = 67;
492 info->size = 0x00800000;
493 break; /* => 8 MB */
494
495 case AMD_ID_LV320B:
496 info->flash_id += FLASH_AM320B;
497 info->sector_count = 67;
498 info->size = 0x00800000;
499 break; /* => 8 MB */
500#endif
501 default:
502 info->flash_id = FLASH_UNKNOWN;
503 return (0); /* => no or unknown flash */
504
505 }
506
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;
516 }
517 } else {
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;
525 }
526 }
527
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;
534 }
535
536 /*
537 * Prevent writes to uninitialized FLASH.
538 */
539 if (info->flash_id != FLASH_UNKNOWN) {
540 addr = (volatile unsigned long *)info->start[0];
541
542 *addr = 0x00F000F0; /* reset bank */
543 }
544
545 return (info->size);
546}
547#endif /* 0 */
548
549
550/*-----------------------------------------------------------------------
551 */
552
553int
554flash_erase(flash_info_t *info, int s_first, int s_last)
555{
556 int prot, sect, haderr;
557 ulong start, now, last;
558 int rcode = 0;
559
560#ifdef FLASH_DEBUG
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);
565#endif
566
567 if ((s_first < 0) || (s_first > s_last)) {
568 if (info->flash_id == FLASH_UNKNOWN) {
569 printf ("- missing\n");
570 } else {
571 printf ("- no sectors to erase\n");
572 }
573 return 1;
574 }
575
576 prot = 0;
577 for (sect=s_first; sect<=s_last; ++sect) {
578 if (info->protect[sect]) {
579 prot++;
580 }
581 }
582
583 if (prot) {
584 printf("- Warning: %d protected sector%s will not be erased!\n",
585 prot, (prot > 1 ? "s" : ""));
586 }
587
588 start = get_timer (0);
589 last = 0;
590 haderr = 0;
591
592 for (sect = s_first; sect <= s_last; sect++) {
593 if (info->protect[sect] == 0) { /* not protected */
594 ulong estart;
595 int sectdone;
596
597 bank_erase_init(info, sect);
598
599 /* wait at least 80us - let's wait 1 ms */
600 udelay (1000);
601
602 estart = get_timer(start);
603
604 do {
605 now = get_timer(start);
606
607 if (now - estart > CFG_FLASH_ERASE_TOUT) {
608 printf ("Timeout (sect %d)\n", sect);
609 haderr = 1;
610 rcode = 1;
611 break;
612 }
613
614#ifndef FLASH_DEBUG
615 /* show that we're waiting */
616 if ((now - last) > 1000) { /* every second */
617 putc ('.');
618 last = now;
619 }
620#endif
621
622 sectdone = bank_erase_poll(info, sect);
623
624 if (sectdone < 0) {
625 haderr = 1;
626 rcode = 1;
627 break;
628 }
629
630 } while (!sectdone);
631
632 if (haderr)
633 break;
634 }
635 }
636
637 if (haderr > 0)
638 printf (" failed\n");
639 else
640 printf (" done\n");
641
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);
646 }
647 }
648 return rcode;
649}
650
651/*-----------------------------------------------------------------------
652 * Copy memory to flash, returns:
653 * 0 - OK
654 * 1 - write timeout
655 * 2 - Flash not erased
656 */
657
658int
659write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
660{
661 ulong cp, wp, data;
662 int i, l, rc;
663
664 wp = (addr & ~3); /* get lower word aligned address */
665
666 /*
667 * handle unaligned start bytes
668 */
669 if ((l = addr - wp) != 0) {
670 data = 0;
671 for (i=0, cp=wp; i<l; ++i, ++cp) {
672 data = (data << 8) | (*(uchar *)cp);
673 }
674 for (; i<4 && cnt>0; ++i) {
675 data = (data << 8) | *src++;
676 --cnt;
677 ++cp;
678 }
679 for (; cnt==0 && i<4; ++i, ++cp) {
680 data = (data << 8) | (*(uchar *)cp);
681 }
682
683 if ((rc = write_word(info, wp, data)) != 0) {
684 return (rc);
685 }
686 wp += 4;
687 }
688
689 /*
690 * handle word aligned part
691 */
692 while (cnt >= 4) {
693 data = 0;
694 for (i=0; i<4; ++i) {
695 data = (data << 8) | *src++;
696 }
697 if ((rc = write_word(info, wp, data)) != 0) {
698 return (rc);
699 }
700 wp += 4;
701 cnt -= 4;
702 }
703
704 if (cnt == 0) {
705 return (0);
706 }
707
708 /*
709 * handle unaligned tail bytes
710 */
711 data = 0;
712 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
713 data = (data << 8) | *src++;
714 --cnt;
715 }
716 for (; i<4; ++i, ++cp) {
717 data = (data << 8) | (*(uchar *)cp);
718 }
719
720 return (write_word(info, wp, data));
721}
722
723/*-----------------------------------------------------------------------
724 * Write a word to Flash, returns:
725 * 0 - OK
726 * 1 - write timeout
727 * 2 - Flash not erased
728 */
729static int
730write_word(flash_info_t *info, ulong dest, ulong data)
731{
732 int retval;
733
734 /* Check if Flash is (sufficiently) erased */
735 if ((*(ulong *)dest & data) != data) {
736 return (2);
737 }
738
739 retval = bank_write_word((bank_addr_t)dest, (bank_word_t)data);
740
741 return (retval);
742}
743
744/*-----------------------------------------------------------------------
745 */