2 * (C) Masami Komiya <mkomiya@sonare.it> 2004
4 * (C) Copyright 2001-2004
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * See file CREDITS for list of people who contributed to this
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 #include <asm/processor.h>
29 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
32 #ifdef CONFIG_SYS_FLASH_16BIT
33 #define FLASH_WORD_SIZE unsigned short
34 #define FLASH_ID_MASK 0xFFFF
36 #define FLASH_WORD_SIZE unsigned long
37 #define FLASH_ID_MASK 0xFFFFFFFF
40 /*-----------------------------------------------------------------------
43 /* stolen from esteem192e/flash.c */
44 ulong flash_get_size (volatile FLASH_WORD_SIZE * addr, flash_info_t * info);
46 #ifndef CONFIG_SYS_FLASH_16BIT
47 static int write_word (flash_info_t * info, ulong dest, ulong data);
49 static int write_short (flash_info_t * info, ulong dest, ushort data);
51 static void flash_get_offsets (ulong base, flash_info_t * info);
54 /*-----------------------------------------------------------------------
57 unsigned long flash_init (void)
59 unsigned long size_b0, size_b1;
62 unsigned long base_b0, base_b1;
64 /* Init: no FLASHes known */
65 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
66 flash_info[i].flash_id = FLASH_UNKNOWN;
69 /* Static FLASH Bank configuration here - FIXME XXX */
72 flash_get_size ((volatile FLASH_WORD_SIZE *) CONFIG_SYS_FLASH_BASE,
75 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
76 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n", size_b0, size_b0 << 20);
80 if (CONFIG_SYS_MAX_FLASH_BANKS == 1) {
82 flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
84 /* Monitor protection ON by default */
86 (void) flash_protect (FLAG_PROTECT_SET,
87 FLASH_BASE0_PRELIM - monitor_flash_len +
89 FLASH_BASE0_PRELIM - 1 + size_b0,
92 (void) flash_protect (FLAG_PROTECT_SET,
93 CONFIG_SYS_MONITOR_BASE,
94 CONFIG_SYS_MONITOR_BASE + monitor_flash_len -
98 flash_info[0].size = size_b0;
100 #ifdef CONFIG_SYS_FLASH_BASE_2
104 flash_get_size ((volatile FLASH_WORD_SIZE *)
105 CONFIG_SYS_FLASH_BASE_2, &flash_info[1]);
107 /* Re-do sizing to get full correct info */
110 mtdcr (EBC0_CFGADDR, PB0CR);
111 pbcr = mfdcr (EBC0_CFGDATA);
112 mtdcr (EBC0_CFGADDR, PB0CR);
114 pbcr = (pbcr & 0x0001ffff) | base_b1 |
115 (((size_b1 / 1024 / 1024) - 1) << 17);
116 mtdcr (EBC0_CFGDATA, pbcr);
117 /* printf("PB1CR = %x\n", pbcr); */
121 mtdcr (EBC0_CFGADDR, PB1CR);
122 pbcr = mfdcr (EBC0_CFGDATA);
123 mtdcr (EBC0_CFGADDR, PB1CR);
124 base_b0 = base_b1 - size_b0;
125 pbcr = (pbcr & 0x0001ffff) | base_b0 |
126 (((size_b0 / 1024 / 1024) - 1) << 17);
127 mtdcr (EBC0_CFGDATA, pbcr);
128 /* printf("PB0CR = %x\n", pbcr); */
132 flash_get_size ((volatile FLASH_WORD_SIZE *) base_b0,
135 flash_get_offsets (base_b0, &flash_info[0]);
137 /* monitor protection ON by default */
139 (void) flash_protect (FLAG_PROTECT_SET,
140 FLASH_BASE0_PRELIM - monitor_flash_len +
142 FLASH_BASE0_PRELIM - 1 + size_b0,
145 (void) flash_protect (FLAG_PROTECT_SET,
146 CONFIG_SYS_MONITOR_BASE,
147 CONFIG_SYS_MONITOR_BASE + monitor_flash_len -
152 /* Re-do sizing to get full correct info */
154 flash_get_size ((volatile FLASH_WORD_SIZE *)
155 base_b1, &flash_info[1]);
157 flash_get_offsets (base_b1, &flash_info[1]);
159 /* monitor protection ON by default */
160 (void) flash_protect (FLAG_PROTECT_SET,
163 base_b1 + size_b1 - 1,
165 /* monitor protection OFF by default (one is enough) */
166 (void) flash_protect (FLAG_PROTECT_CLEAR,
169 base_b0 + size_b0 - 1,
172 flash_info[1].flash_id = FLASH_UNKNOWN;
173 flash_info[1].sector_count = -1;
176 flash_info[0].size = size_b0;
177 flash_info[1].size = size_b1;
180 return (size_b0 + size_b1);
184 /*-----------------------------------------------------------------------
187 static void flash_get_offsets (ulong base, flash_info_t * info)
191 /* set up sector start adress table */
192 if ((info->flash_id & FLASH_TYPEMASK) == FLASH_28F320J3A ||
193 (info->flash_id & FLASH_TYPEMASK) == FLASH_28F640J3A ||
194 (info->flash_id & FLASH_TYPEMASK) == FLASH_28F128J3A) {
195 for (i = 0; i < info->sector_count; i++) {
197 base + (i * info->size / info->sector_count);
199 } else if (info->flash_id & FLASH_BTYPE) {
200 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
202 #ifndef CONFIG_SYS_FLASH_16BIT
203 /* set sector offsets for bottom boot block type */
204 info->start[0] = base + 0x00000000;
205 info->start[1] = base + 0x00004000;
206 info->start[2] = base + 0x00008000;
207 info->start[3] = base + 0x0000C000;
208 info->start[4] = base + 0x00010000;
209 info->start[5] = base + 0x00014000;
210 info->start[6] = base + 0x00018000;
211 info->start[7] = base + 0x0001C000;
212 for (i = 8; i < info->sector_count; i++) {
214 base + (i * 0x00020000) - 0x000E0000;
217 /* set sector offsets for bottom boot block type */
218 info->start[0] = base + 0x00000000;
219 info->start[1] = base + 0x00008000;
220 info->start[2] = base + 0x0000C000;
221 info->start[3] = base + 0x00010000;
222 for (i = 4; i < info->sector_count; i++) {
224 base + (i * 0x00020000) - 0x00060000;
228 /* set sector offsets for bottom boot block type */
229 info->start[0] = base + 0x00000000;
230 info->start[1] = base + 0x00002000;
231 info->start[2] = base + 0x00004000;
232 info->start[3] = base + 0x00006000;
233 info->start[4] = base + 0x00008000;
234 info->start[5] = base + 0x0000A000;
235 info->start[6] = base + 0x0000C000;
236 info->start[7] = base + 0x0000E000;
237 for (i = 8; i < info->sector_count; i++) {
239 base + (i * 0x00010000) - 0x00070000;
242 /* set sector offsets for bottom boot block type */
243 info->start[0] = base + 0x00000000;
244 info->start[1] = base + 0x00004000;
245 info->start[2] = base + 0x00006000;
246 info->start[3] = base + 0x00008000;
247 for (i = 4; i < info->sector_count; i++) {
249 base + (i * 0x00010000) - 0x00030000;
254 /* set sector offsets for top boot block type */
255 i = info->sector_count - 1;
256 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
258 #ifndef CONFIG_SYS_FLASH_16BIT
259 info->start[i--] = base + info->size - 0x00004000;
260 info->start[i--] = base + info->size - 0x00008000;
261 info->start[i--] = base + info->size - 0x0000C000;
262 info->start[i--] = base + info->size - 0x00010000;
263 info->start[i--] = base + info->size - 0x00014000;
264 info->start[i--] = base + info->size - 0x00018000;
265 info->start[i--] = base + info->size - 0x0001C000;
266 for (; i >= 0; i--) {
267 info->start[i] = base + i * 0x00020000;
272 info->start[i--] = base + info->size - 0x00008000;
273 info->start[i--] = base + info->size - 0x0000C000;
274 info->start[i--] = base + info->size - 0x00010000;
275 for (; i >= 0; i--) {
276 info->start[i] = base + i * 0x00020000;
280 info->start[i--] = base + info->size - 0x00002000;
281 info->start[i--] = base + info->size - 0x00004000;
282 info->start[i--] = base + info->size - 0x00006000;
283 info->start[i--] = base + info->size - 0x00008000;
284 info->start[i--] = base + info->size - 0x0000A000;
285 info->start[i--] = base + info->size - 0x0000C000;
286 info->start[i--] = base + info->size - 0x0000E000;
287 for (; i >= 0; i--) {
288 info->start[i] = base + i * 0x00010000;
293 info->start[i--] = base + info->size - 0x00004000;
294 info->start[i--] = base + info->size - 0x00006000;
295 info->start[i--] = base + info->size - 0x00008000;
296 for (; i >= 0; i--) {
297 info->start[i] = base + i * 0x00010000;
306 /*-----------------------------------------------------------------------
309 void flash_print_info (flash_info_t * info)
313 uchar botboot[] = ", bottom boot sect)\n";
314 uchar topboot[] = ", top boot sector)\n";
316 if (info->flash_id == FLASH_UNKNOWN) {
317 printf ("missing or unknown FLASH type\n");
321 switch (info->flash_id & FLASH_VENDMASK) {
334 case FLASH_MAN_INTEL:
338 printf ("Unknown Vendor ");
342 if (info->flash_id & 0x0001) {
348 switch (info->flash_id & FLASH_TYPEMASK) {
350 printf ("AM29LV400B (4 Mbit%s", boottype);
353 printf ("AM29LV400T (4 Mbit%s", boottype);
356 printf ("AM29LV800B (8 Mbit%s", boottype);
359 printf ("AM29LV800T (8 Mbit%s", boottype);
362 printf ("AM29LV160B (16 Mbit%s", boottype);
365 printf ("AM29LV160T (16 Mbit%s", boottype);
368 printf ("AM29LV320B (32 Mbit%s", boottype);
371 printf ("AM29LV320T (32 Mbit%s", boottype);
373 case FLASH_INTEL800B:
374 printf ("INTEL28F800B (8 Mbit%s", boottype);
376 case FLASH_INTEL800T:
377 printf ("INTEL28F800T (8 Mbit%s", boottype);
379 case FLASH_INTEL160B:
380 printf ("INTEL28F160B (16 Mbit%s", boottype);
382 case FLASH_INTEL160T:
383 printf ("INTEL28F160T (16 Mbit%s", boottype);
385 case FLASH_INTEL320B:
386 printf ("INTEL28F320B (32 Mbit%s", boottype);
388 case FLASH_INTEL320T:
389 printf ("INTEL28F320T (32 Mbit%s", boottype);
392 #if 0 /* enable when devices are available */
394 case FLASH_INTEL640B:
395 printf ("INTEL28F640B (64 Mbit%s", boottype);
397 case FLASH_INTEL640T:
398 printf ("INTEL28F640T (64 Mbit%s", boottype);
401 case FLASH_28F320J3A:
402 printf ("INTEL28F320J3A (32 Mbit%s", boottype);
404 case FLASH_28F640J3A:
405 printf ("INTEL28F640J3A (64 Mbit%s", boottype);
407 case FLASH_28F128J3A:
408 printf ("INTEL28F128J3A (128 Mbit%s", boottype);
412 printf ("Unknown Chip Type\n");
416 printf (" Size: %ld MB in %d Sectors\n",
417 info->size >> 20, info->sector_count);
419 printf (" Sector Start Addresses:");
420 for (i = 0; i < info->sector_count; ++i) {
424 info->start[i], info->protect[i] ? " (RO)" : " ");
431 /*-----------------------------------------------------------------------
435 /*-----------------------------------------------------------------------
439 * The following code cannot be run from FLASH!
441 ulong flash_get_size (volatile FLASH_WORD_SIZE * addr, flash_info_t * info)
444 ulong base = (ulong) addr;
445 FLASH_WORD_SIZE value;
447 /* Write auto select command: read Manufacturer ID */
450 #ifndef CONFIG_SYS_FLASH_16BIT
453 * Note: if it is an AMD flash and the word at addr[0000]
454 * is 0x00890089 this routine will think it is an Intel
455 * flash device and may(most likely) cause trouble.
458 addr[0x0000] = 0x00900090;
459 if (addr[0x0000] != 0x00890089) {
460 addr[0x0555] = 0x00AA00AA;
461 addr[0x02AA] = 0x00550055;
462 addr[0x0555] = 0x00900090;
466 * Note: if it is an AMD flash and the word at addr[0000]
467 * is 0x0089 this routine will think it is an Intel
468 * flash device and may(most likely) cause trouble.
471 addr[0x0000] = 0x0090;
473 if (addr[0x0000] != 0x0089) {
474 addr[0x0555] = 0x00AA;
475 addr[0x02AA] = 0x0055;
476 addr[0x0555] = 0x0090;
482 case (AMD_MANUFACT & FLASH_ID_MASK):
483 info->flash_id = FLASH_MAN_AMD;
485 case (FUJ_MANUFACT & FLASH_ID_MASK):
486 info->flash_id = FLASH_MAN_FUJ;
488 case (STM_MANUFACT & FLASH_ID_MASK):
489 info->flash_id = FLASH_MAN_STM;
491 case (SST_MANUFACT & FLASH_ID_MASK):
492 info->flash_id = FLASH_MAN_SST;
494 case (INTEL_MANUFACT & FLASH_ID_MASK):
495 info->flash_id = FLASH_MAN_INTEL;
498 info->flash_id = FLASH_UNKNOWN;
499 info->sector_count = 0;
501 return (0); /* no or unknown flash */
505 value = addr[1]; /* device ID */
509 case (AMD_ID_LV400T & FLASH_ID_MASK):
510 info->flash_id += FLASH_AM400T;
511 info->sector_count = 11;
512 info->size = 0x00100000;
515 case (AMD_ID_LV400B & FLASH_ID_MASK):
516 info->flash_id += FLASH_AM400B;
517 info->sector_count = 11;
518 info->size = 0x00100000;
521 case (AMD_ID_LV800T & FLASH_ID_MASK):
522 info->flash_id += FLASH_AM800T;
523 info->sector_count = 19;
524 info->size = 0x00200000;
527 case (AMD_ID_LV800B & FLASH_ID_MASK):
528 info->flash_id += FLASH_AM800B;
529 info->sector_count = 19;
530 info->size = 0x00200000;
533 case (AMD_ID_LV160T & FLASH_ID_MASK):
534 info->flash_id += FLASH_AM160T;
535 info->sector_count = 35;
536 info->size = 0x00400000;
539 case (AMD_ID_LV160B & FLASH_ID_MASK):
540 info->flash_id += FLASH_AM160B;
541 info->sector_count = 35;
542 info->size = 0x00400000;
544 #if 0 /* enable when device IDs are available */
545 case (AMD_ID_LV320T & FLASH_ID_MASK):
546 info->flash_id += FLASH_AM320T;
547 info->sector_count = 67;
548 info->size = 0x00800000;
551 case (AMD_ID_LV320B & FLASH_ID_MASK):
552 info->flash_id += FLASH_AM320B;
553 info->sector_count = 67;
554 info->size = 0x00800000;
558 case (INTEL_ID_28F800B3T & FLASH_ID_MASK):
559 info->flash_id += FLASH_INTEL800T;
560 info->sector_count = 23;
561 info->size = 0x00200000;
564 case (INTEL_ID_28F800B3B & FLASH_ID_MASK):
565 info->flash_id += FLASH_INTEL800B;
566 info->sector_count = 23;
567 info->size = 0x00200000;
570 case (INTEL_ID_28F160B3T & FLASH_ID_MASK):
571 info->flash_id += FLASH_INTEL160T;
572 info->sector_count = 39;
573 info->size = 0x00400000;
576 case (INTEL_ID_28F160B3B & FLASH_ID_MASK):
577 info->flash_id += FLASH_INTEL160B;
578 info->sector_count = 39;
579 info->size = 0x00400000;
582 case (INTEL_ID_28F320B3T & FLASH_ID_MASK):
583 info->flash_id += FLASH_INTEL320T;
584 info->sector_count = 71;
585 info->size = 0x00800000;
588 case (INTEL_ID_28F320B3B & FLASH_ID_MASK):
589 info->flash_id += FLASH_AM320B;
590 info->sector_count = 71;
591 info->size = 0x00800000;
594 #if 0 /* enable when devices are available */
595 case (INTEL_ID_28F320B3T & FLASH_ID_MASK):
596 info->flash_id += FLASH_INTEL320T;
597 info->sector_count = 135;
598 info->size = 0x01000000;
599 break; /* => 16 MB */
601 case (INTEL_ID_28F320B3B & FLASH_ID_MASK):
602 info->flash_id += FLASH_AM320B;
603 info->sector_count = 135;
604 info->size = 0x01000000;
605 break; /* => 16 MB */
607 case (INTEL_ID_28F320J3A & FLASH_ID_MASK):
608 info->flash_id += FLASH_28F320J3A;
609 info->sector_count = 32;
610 info->size = 0x00400000;
611 break; /* => 32 MBit */
612 case (INTEL_ID_28F640J3A & FLASH_ID_MASK):
613 info->flash_id += FLASH_28F640J3A;
614 info->sector_count = 64;
615 info->size = 0x00800000;
616 break; /* => 64 MBit */
617 case (INTEL_ID_28F128J3A & FLASH_ID_MASK):
618 info->flash_id += FLASH_28F128J3A;
619 info->sector_count = 128;
620 info->size = 0x01000000;
621 break; /* => 128 MBit */
625 info->flash_id = FLASH_UNKNOWN;
626 return (0); /* => no or unknown flash */
629 flash_get_offsets (base, info);
631 /* check for protected sectors */
632 for (i = 0; i < info->sector_count; i++) {
633 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
634 /* D0 = 1 if protected */
635 addr = (volatile FLASH_WORD_SIZE *) (info->start[i]);
636 info->protect[i] = addr[2] & 1;
640 * Prevent writes to uninitialized FLASH.
642 if (info->flash_id != FLASH_UNKNOWN) {
643 addr = (volatile FLASH_WORD_SIZE *) info->start[0];
644 if ((info->flash_id & 0xFF00) == FLASH_MAN_INTEL) {
645 *addr = (0x00F000F0 & FLASH_ID_MASK); /* reset bank */
647 *addr = (0x00FF00FF & FLASH_ID_MASK); /* reset bank */
655 /*-----------------------------------------------------------------------
658 int flash_erase (flash_info_t * info, int s_first, int s_last)
661 volatile FLASH_WORD_SIZE *addr =
662 (volatile FLASH_WORD_SIZE *) (info->start[0]);
663 int flag, prot, sect, l_sect, barf;
664 ulong start, now, last;
667 if ((s_first < 0) || (s_first > s_last)) {
668 if (info->flash_id == FLASH_UNKNOWN) {
669 printf ("- missing\n");
671 printf ("- no sectors to erase\n");
676 if ((info->flash_id == FLASH_UNKNOWN) ||
677 ((info->flash_id > FLASH_AMD_COMP) &&
678 ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL))) {
679 printf ("Can't erase unknown flash type - aborted\n");
684 for (sect = s_first; sect <= s_last; ++sect) {
685 if (info->protect[sect]) {
691 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
698 /* Disable interrupts which might cause a timeout here */
699 flag = disable_interrupts ();
700 if (info->flash_id < FLASH_AMD_COMP) {
701 #ifndef CONFIG_SYS_FLASH_16BIT
702 addr[0x0555] = 0x00AA00AA;
703 addr[0x02AA] = 0x00550055;
704 addr[0x0555] = 0x00800080;
705 addr[0x0555] = 0x00AA00AA;
706 addr[0x02AA] = 0x00550055;
708 addr[0x0555] = 0x00AA;
709 addr[0x02AA] = 0x0055;
710 addr[0x0555] = 0x0080;
711 addr[0x0555] = 0x00AA;
712 addr[0x02AA] = 0x0055;
714 /* Start erase on unprotected sectors */
715 for (sect = s_first; sect <= s_last; sect++) {
716 if (info->protect[sect] == 0) { /* not protected */
717 addr = (volatile FLASH_WORD_SIZE *) (info->
720 addr[0] = (0x00300030 & FLASH_ID_MASK);
725 /* re-enable interrupts if necessary */
727 enable_interrupts ();
729 /* wait at least 80us - let's wait 1 ms */
733 * We wait for the last triggered sector
738 start = get_timer (0);
740 addr = (volatile FLASH_WORD_SIZE *) (info->start[l_sect]);
741 while ((addr[0] & (0x00800080 & FLASH_ID_MASK)) !=
742 (0x00800080 & FLASH_ID_MASK)) {
743 if ((now = get_timer (start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
744 printf ("Timeout\n");
747 /* show that we're waiting */
748 if ((now - last) > 1000000) { /* every second */
755 /* reset to read mode */
756 addr = (volatile FLASH_WORD_SIZE *) info->start[0];
757 addr[0] = (0x00F000F0 & FLASH_ID_MASK); /* reset bank */
761 for (sect = s_first; sect <= s_last; sect++) {
762 if (info->protect[sect] == 0) { /* not protected */
764 #ifndef CONFIG_SYS_FLASH_16BIT
765 addr = (vu_long *) (info->start[sect]);
766 addr[0] = 0x00500050;
767 addr[0] = 0x00200020;
768 addr[0] = 0x00D000D0;
769 while (!(addr[0] & 0x00800080)); /* wait for error or finish */
770 if (addr[0] & 0x003A003A) { /* check for error */
771 barf = addr[0] & 0x003A0000;
775 barf = addr[0] & 0x0000003A;
779 addr = (vu_short *) (info->start[sect]);
780 addr[0] = 0x0050; /* clear status register */
783 while (!(addr[0] & 0x0080)); /* wait for error or finish */
784 if (addr[0] & 0x003A) /* check for error */
785 barf = addr[0] & 0x003A;
788 printf ("\nFlash error in sector at %lx\n", (unsigned long) addr);
790 printf ("Block locked, not erased.\n");
791 if ((barf & 0x0030) == 0x0030)
792 printf ("Command Sequence error.\n");
793 if ((barf & 0x0030) == 0x0020)
794 printf ("Block Erase error.\n");
796 printf ("Vpp Low error.\n");
802 addr = (volatile FLASH_WORD_SIZE *) info->start[0];
803 #ifndef CONFIG_SYS_FLASH_16BIT
804 addr[0] = (0x00FF00FF & FLASH_ID_MASK); /* reset bank */
806 addr[0] = (0x00FF & FLASH_ID_MASK); /* reset bank */
815 /*-----------------------------------------------------------------------
818 /*flash_info_t *addr2info (ulong addr)
823 for (i=0, info=&flash_info[0]; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
824 if ((addr >= info->start[0]) &&
825 (addr < (info->start[0] + info->size)) ) {
833 /*-----------------------------------------------------------------------
834 * Copy memory to flash.
835 * Make sure all target addresses are within Flash bounds,
836 * and no protected sectors are hit.
840 * 2 - Flash not erased
841 * 4 - target range includes protected sectors
842 * 8 - target address not in Flash memory
845 /*int flash_write (uchar *src, ulong addr, ulong cnt)
848 ulong end = addr + cnt - 1;
849 flash_info_t *info_first = addr2info (addr);
850 flash_info_t *info_last = addr2info (end );
857 if (!info_first || !info_last) {
861 for (info = info_first; info <= info_last; ++info) {
862 ulong b_end = info->start[0] + info->size;*/ /* bank end addr */
863 /* short s_end = info->sector_count - 1;
864 for (i=0; i<info->sector_count; ++i) {
865 ulong e_addr = (i == s_end) ? b_end : info->start[i + 1];
867 if ((end >= info->start[i]) && (addr < e_addr) &&
868 (info->protect[i] != 0) ) {
874 */ /* finally write data to flash */
875 /* for (info = info_first; info <= info_last && cnt>0; ++info) {
878 len = info->start[0] + info->size - addr;
881 if ((i = write_buff(info, src, addr, len)) != 0) {
891 /*-----------------------------------------------------------------------
892 * Copy memory to flash, returns:
895 * 2 - Flash not erased
898 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
900 #ifndef CONFIG_SYS_FLASH_16BIT
909 #ifndef CONFIG_SYS_FLASH_16BIT
912 wp = (addr & ~3); /* get lower word aligned address */
915 * handle unaligned start bytes
917 if ((l = addr - wp) != 0) {
919 for (i = 0, cp = wp; i < l; ++i, ++cp) {
920 data = (data << 8) | (*(uchar *) cp);
922 for (; i < 4 && cnt > 0; ++i) {
923 data = (data << 8) | *src++;
927 for (; cnt == 0 && i < 4; ++i, ++cp) {
928 data = (data << 8) | (*(uchar *) cp);
931 if ((rc = write_word (info, wp, data)) != 0) {
938 * handle word aligned part
942 for (i = 0; i < 4; ++i) {
943 data = (data << 8) | *src++;
945 if ((rc = write_word (info, wp, data)) != 0) {
957 * handle unaligned tail bytes
960 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
961 data = (data << 8) | *src++;
964 for (; i < 4; ++i, ++cp) {
965 data = (data << 8) | (*(uchar *) cp);
968 return (write_word (info, wp, data));
971 wp = (addr & ~1); /* get lower word aligned address */
974 * handle unaligned start byte
978 data = (data << 8) | *src++;
980 if ((rc = write_short (info, wp, data)) != 0) {
987 * handle word aligned part
989 /* l = 0; used for debuging */
992 for (i = 0; i < 2; ++i) {
993 data = (data << 8) | *src++;
999 } used for debuging */
1001 if ((rc = write_short (info, wp, data)) != 0) {
1013 * handle unaligned tail bytes
1016 for (i = 0, cp = wp; i < 2 && cnt > 0; ++i, ++cp) {
1017 data = (data << 8) | *src++;
1020 for (; i < 2; ++i, ++cp) {
1021 data = (data << 8) | (*(uchar *) cp);
1024 return (write_short (info, wp, data));
1030 /*-----------------------------------------------------------------------
1031 * Write a word to Flash, returns:
1034 * 2 - Flash not erased
1036 #ifndef CONFIG_SYS_FLASH_16BIT
1037 static int write_word (flash_info_t * info, ulong dest, ulong data)
1039 vu_long *addr = (vu_long *) (info->start[0]);
1043 #if defined (__MIPSEL__)
1044 data = cpu_to_be32 (data);
1047 /* Check if Flash is (sufficiently) erased */
1048 if ((*((vu_long *) dest) & data) != data) {
1052 /* Disable interrupts which might cause a timeout here */
1053 flag = disable_interrupts ();
1055 if (info->flash_id < FLASH_AMD_COMP) {
1057 addr[0x0555] = 0x00AA00AA;
1058 addr[0x02AA] = 0x00550055;
1059 addr[0x0555] = 0x00A000A0;
1065 *((vu_long *) dest) = data;
1067 /* re-enable interrupts if necessary */
1069 enable_interrupts ();
1071 /* data polling for D7 */
1072 start = get_timer (0);
1074 if (info->flash_id < FLASH_AMD_COMP) {
1076 while ((*((vu_long *) dest) & 0x00800080) !=
1077 (data & 0x00800080)) {
1078 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
1079 printf ("timeout\n");
1086 while (!(addr[0] & 0x00800080)) { /* wait for error or finish */
1087 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
1088 printf ("timeout\n");
1093 if (addr[0] & 0x003A003A) { /* check for error */
1094 barf = addr[0] & 0x003A0000;
1098 barf = addr[0] & 0x0000003A;
1100 printf ("\nFlash write error at address %lx\n",
1101 (unsigned long) dest);
1103 printf ("Block locked, not erased.\n");
1105 printf ("Programming error.\n");
1107 printf ("Vpp Low error.\n");
1119 static int write_short (flash_info_t * info, ulong dest, ushort data)
1121 vu_short *addr = (vu_short *) (info->start[0]);
1125 #if defined (__MIPSEL__)
1126 data = cpu_to_be16 (data);
1129 /* Check if Flash is (sufficiently) erased */
1130 if ((*((vu_short *) dest) & data) != data) {
1134 /* Disable interrupts which might cause a timeout here */
1135 flag = disable_interrupts ();
1137 if (info->flash_id < FLASH_AMD_COMP) {
1139 addr[0x0555] = 0x00AA;
1140 addr[0x02AA] = 0x0055;
1141 addr[0x0555] = 0x00A0;
1147 *((vu_short *) dest) = data;
1149 /* re-enable interrupts if necessary */
1151 enable_interrupts ();
1153 /* data polling for D7 */
1154 start = get_timer (0);
1156 if (info->flash_id < FLASH_AMD_COMP) {
1158 while ((*((vu_short *) dest) & 0x0080) != (data & 0x0080)) {
1159 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
1166 while (!(addr[0] & 0x0080)) { /* wait for error or finish */
1167 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT)
1171 if (addr[0] & 0x003A) { /* check for error */
1172 barf = addr[0] & 0x003A;
1173 printf ("\nFlash write error at address %lx\n",
1174 (unsigned long) dest);
1176 printf ("Block locked, not erased.\n");
1178 printf ("Programming error.\n");
1180 printf ("Vpp Low error.\n");
1185 while (!(addr[0] & 0x0080)) { /* wait for error or finish */
1186 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT)