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/ppc4xx.h>
28 #include <asm/processor.h>
30 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
33 #ifdef CONFIG_SYS_FLASH_16BIT
34 #define FLASH_WORD_SIZE unsigned short
35 #define FLASH_ID_MASK 0xFFFF
37 #define FLASH_WORD_SIZE unsigned long
38 #define FLASH_ID_MASK 0xFFFFFFFF
41 /*-----------------------------------------------------------------------
44 /* stolen from esteem192e/flash.c */
45 ulong flash_get_size (volatile FLASH_WORD_SIZE * addr, flash_info_t * info);
47 #ifndef CONFIG_SYS_FLASH_16BIT
48 static int write_word (flash_info_t * info, ulong dest, ulong data);
50 static int write_short (flash_info_t * info, ulong dest, ushort data);
52 static void flash_get_offsets (ulong base, flash_info_t * info);
55 /*-----------------------------------------------------------------------
58 unsigned long flash_init (void)
60 unsigned long size_b0, size_b1;
63 unsigned long base_b0, base_b1;
65 /* Init: no FLASHes known */
66 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
67 flash_info[i].flash_id = FLASH_UNKNOWN;
70 /* Static FLASH Bank configuration here - FIXME XXX */
73 flash_get_size ((volatile FLASH_WORD_SIZE *) CONFIG_SYS_FLASH_BASE,
76 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
77 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n", size_b0, size_b0 << 20);
81 if (CONFIG_SYS_MAX_FLASH_BANKS == 1) {
83 flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
85 /* Monitor protection ON by default */
87 (void) flash_protect (FLAG_PROTECT_SET,
88 FLASH_BASE0_PRELIM - monitor_flash_len +
90 FLASH_BASE0_PRELIM - 1 + size_b0,
93 (void) flash_protect (FLAG_PROTECT_SET,
94 CONFIG_SYS_MONITOR_BASE,
95 CONFIG_SYS_MONITOR_BASE + monitor_flash_len -
99 flash_info[0].size = size_b0;
101 #ifdef CONFIG_SYS_FLASH_BASE_2
105 flash_get_size ((volatile FLASH_WORD_SIZE *)
106 CONFIG_SYS_FLASH_BASE_2, &flash_info[1]);
108 /* Re-do sizing to get full correct info */
111 mtdcr (EBC0_CFGADDR, PB0CR);
112 pbcr = mfdcr (EBC0_CFGDATA);
113 mtdcr (EBC0_CFGADDR, PB0CR);
115 pbcr = (pbcr & 0x0001ffff) | base_b1 |
116 (((size_b1 / 1024 / 1024) - 1) << 17);
117 mtdcr (EBC0_CFGDATA, pbcr);
118 /* printf("PB1CR = %x\n", pbcr); */
122 mtdcr (EBC0_CFGADDR, PB1CR);
123 pbcr = mfdcr (EBC0_CFGDATA);
124 mtdcr (EBC0_CFGADDR, PB1CR);
125 base_b0 = base_b1 - size_b0;
126 pbcr = (pbcr & 0x0001ffff) | base_b0 |
127 (((size_b0 / 1024 / 1024) - 1) << 17);
128 mtdcr (EBC0_CFGDATA, pbcr);
129 /* printf("PB0CR = %x\n", pbcr); */
133 flash_get_size ((volatile FLASH_WORD_SIZE *) base_b0,
136 flash_get_offsets (base_b0, &flash_info[0]);
138 /* monitor protection ON by default */
140 (void) flash_protect (FLAG_PROTECT_SET,
141 FLASH_BASE0_PRELIM - monitor_flash_len +
143 FLASH_BASE0_PRELIM - 1 + size_b0,
146 (void) flash_protect (FLAG_PROTECT_SET,
147 CONFIG_SYS_MONITOR_BASE,
148 CONFIG_SYS_MONITOR_BASE + monitor_flash_len -
153 /* Re-do sizing to get full correct info */
155 flash_get_size ((volatile FLASH_WORD_SIZE *)
156 base_b1, &flash_info[1]);
158 flash_get_offsets (base_b1, &flash_info[1]);
160 /* monitor protection ON by default */
161 (void) flash_protect (FLAG_PROTECT_SET,
164 base_b1 + size_b1 - 1,
166 /* monitor protection OFF by default (one is enough) */
167 (void) flash_protect (FLAG_PROTECT_CLEAR,
170 base_b0 + size_b0 - 1,
173 flash_info[1].flash_id = FLASH_UNKNOWN;
174 flash_info[1].sector_count = -1;
177 flash_info[0].size = size_b0;
178 flash_info[1].size = size_b1;
181 return (size_b0 + size_b1);
185 /*-----------------------------------------------------------------------
188 static void flash_get_offsets (ulong base, flash_info_t * info)
192 /* set up sector start adress table */
193 if ((info->flash_id & FLASH_TYPEMASK) == FLASH_28F320J3A ||
194 (info->flash_id & FLASH_TYPEMASK) == FLASH_28F640J3A ||
195 (info->flash_id & FLASH_TYPEMASK) == FLASH_28F128J3A) {
196 for (i = 0; i < info->sector_count; i++) {
198 base + (i * info->size / info->sector_count);
200 } else if (info->flash_id & FLASH_BTYPE) {
201 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
203 #ifndef CONFIG_SYS_FLASH_16BIT
204 /* set sector offsets for bottom boot block type */
205 info->start[0] = base + 0x00000000;
206 info->start[1] = base + 0x00004000;
207 info->start[2] = base + 0x00008000;
208 info->start[3] = base + 0x0000C000;
209 info->start[4] = base + 0x00010000;
210 info->start[5] = base + 0x00014000;
211 info->start[6] = base + 0x00018000;
212 info->start[7] = base + 0x0001C000;
213 for (i = 8; i < info->sector_count; i++) {
215 base + (i * 0x00020000) - 0x000E0000;
218 /* set sector offsets for bottom boot block type */
219 info->start[0] = base + 0x00000000;
220 info->start[1] = base + 0x00008000;
221 info->start[2] = base + 0x0000C000;
222 info->start[3] = base + 0x00010000;
223 for (i = 4; i < info->sector_count; i++) {
225 base + (i * 0x00020000) - 0x00060000;
229 /* set sector offsets for bottom boot block type */
230 info->start[0] = base + 0x00000000;
231 info->start[1] = base + 0x00002000;
232 info->start[2] = base + 0x00004000;
233 info->start[3] = base + 0x00006000;
234 info->start[4] = base + 0x00008000;
235 info->start[5] = base + 0x0000A000;
236 info->start[6] = base + 0x0000C000;
237 info->start[7] = base + 0x0000E000;
238 for (i = 8; i < info->sector_count; i++) {
240 base + (i * 0x00010000) - 0x00070000;
243 /* set sector offsets for bottom boot block type */
244 info->start[0] = base + 0x00000000;
245 info->start[1] = base + 0x00004000;
246 info->start[2] = base + 0x00006000;
247 info->start[3] = base + 0x00008000;
248 for (i = 4; i < info->sector_count; i++) {
250 base + (i * 0x00010000) - 0x00030000;
255 /* set sector offsets for top boot block type */
256 i = info->sector_count - 1;
257 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
259 #ifndef CONFIG_SYS_FLASH_16BIT
260 info->start[i--] = base + info->size - 0x00004000;
261 info->start[i--] = base + info->size - 0x00008000;
262 info->start[i--] = base + info->size - 0x0000C000;
263 info->start[i--] = base + info->size - 0x00010000;
264 info->start[i--] = base + info->size - 0x00014000;
265 info->start[i--] = base + info->size - 0x00018000;
266 info->start[i--] = base + info->size - 0x0001C000;
267 for (; i >= 0; i--) {
268 info->start[i] = base + i * 0x00020000;
273 info->start[i--] = base + info->size - 0x00008000;
274 info->start[i--] = base + info->size - 0x0000C000;
275 info->start[i--] = base + info->size - 0x00010000;
276 for (; i >= 0; i--) {
277 info->start[i] = base + i * 0x00020000;
281 info->start[i--] = base + info->size - 0x00002000;
282 info->start[i--] = base + info->size - 0x00004000;
283 info->start[i--] = base + info->size - 0x00006000;
284 info->start[i--] = base + info->size - 0x00008000;
285 info->start[i--] = base + info->size - 0x0000A000;
286 info->start[i--] = base + info->size - 0x0000C000;
287 info->start[i--] = base + info->size - 0x0000E000;
288 for (; i >= 0; i--) {
289 info->start[i] = base + i * 0x00010000;
294 info->start[i--] = base + info->size - 0x00004000;
295 info->start[i--] = base + info->size - 0x00006000;
296 info->start[i--] = base + info->size - 0x00008000;
297 for (; i >= 0; i--) {
298 info->start[i] = base + i * 0x00010000;
307 /*-----------------------------------------------------------------------
310 void flash_print_info (flash_info_t * info)
314 uchar botboot[] = ", bottom boot sect)\n";
315 uchar topboot[] = ", top boot sector)\n";
317 if (info->flash_id == FLASH_UNKNOWN) {
318 printf ("missing or unknown FLASH type\n");
322 switch (info->flash_id & FLASH_VENDMASK) {
335 case FLASH_MAN_INTEL:
339 printf ("Unknown Vendor ");
343 if (info->flash_id & 0x0001) {
349 switch (info->flash_id & FLASH_TYPEMASK) {
351 printf ("AM29LV400B (4 Mbit%s", boottype);
354 printf ("AM29LV400T (4 Mbit%s", boottype);
357 printf ("AM29LV800B (8 Mbit%s", boottype);
360 printf ("AM29LV800T (8 Mbit%s", boottype);
363 printf ("AM29LV160B (16 Mbit%s", boottype);
366 printf ("AM29LV160T (16 Mbit%s", boottype);
369 printf ("AM29LV320B (32 Mbit%s", boottype);
372 printf ("AM29LV320T (32 Mbit%s", boottype);
374 case FLASH_INTEL800B:
375 printf ("INTEL28F800B (8 Mbit%s", boottype);
377 case FLASH_INTEL800T:
378 printf ("INTEL28F800T (8 Mbit%s", boottype);
380 case FLASH_INTEL160B:
381 printf ("INTEL28F160B (16 Mbit%s", boottype);
383 case FLASH_INTEL160T:
384 printf ("INTEL28F160T (16 Mbit%s", boottype);
386 case FLASH_INTEL320B:
387 printf ("INTEL28F320B (32 Mbit%s", boottype);
389 case FLASH_INTEL320T:
390 printf ("INTEL28F320T (32 Mbit%s", boottype);
393 #if 0 /* enable when devices are available */
395 case FLASH_INTEL640B:
396 printf ("INTEL28F640B (64 Mbit%s", boottype);
398 case FLASH_INTEL640T:
399 printf ("INTEL28F640T (64 Mbit%s", boottype);
402 case FLASH_28F320J3A:
403 printf ("INTEL28F320J3A (32 Mbit%s", boottype);
405 case FLASH_28F640J3A:
406 printf ("INTEL28F640J3A (64 Mbit%s", boottype);
408 case FLASH_28F128J3A:
409 printf ("INTEL28F128J3A (128 Mbit%s", boottype);
413 printf ("Unknown Chip Type\n");
417 printf (" Size: %ld MB in %d Sectors\n",
418 info->size >> 20, info->sector_count);
420 printf (" Sector Start Addresses:");
421 for (i = 0; i < info->sector_count; ++i) {
425 info->start[i], info->protect[i] ? " (RO)" : " ");
432 /*-----------------------------------------------------------------------
436 /*-----------------------------------------------------------------------
440 * The following code cannot be run from FLASH!
442 ulong flash_get_size (volatile FLASH_WORD_SIZE * addr, flash_info_t * info)
445 ulong base = (ulong) addr;
446 FLASH_WORD_SIZE value;
448 /* Write auto select command: read Manufacturer ID */
451 #ifndef CONFIG_SYS_FLASH_16BIT
454 * Note: if it is an AMD flash and the word at addr[0000]
455 * is 0x00890089 this routine will think it is an Intel
456 * flash device and may(most likely) cause trouble.
459 addr[0x0000] = 0x00900090;
460 if (addr[0x0000] != 0x00890089) {
461 addr[0x0555] = 0x00AA00AA;
462 addr[0x02AA] = 0x00550055;
463 addr[0x0555] = 0x00900090;
467 * Note: if it is an AMD flash and the word at addr[0000]
468 * is 0x0089 this routine will think it is an Intel
469 * flash device and may(most likely) cause trouble.
472 addr[0x0000] = 0x0090;
474 if (addr[0x0000] != 0x0089) {
475 addr[0x0555] = 0x00AA;
476 addr[0x02AA] = 0x0055;
477 addr[0x0555] = 0x0090;
483 case (AMD_MANUFACT & FLASH_ID_MASK):
484 info->flash_id = FLASH_MAN_AMD;
486 case (FUJ_MANUFACT & FLASH_ID_MASK):
487 info->flash_id = FLASH_MAN_FUJ;
489 case (STM_MANUFACT & FLASH_ID_MASK):
490 info->flash_id = FLASH_MAN_STM;
492 case (SST_MANUFACT & FLASH_ID_MASK):
493 info->flash_id = FLASH_MAN_SST;
495 case (INTEL_MANUFACT & FLASH_ID_MASK):
496 info->flash_id = FLASH_MAN_INTEL;
499 info->flash_id = FLASH_UNKNOWN;
500 info->sector_count = 0;
502 return (0); /* no or unknown flash */
506 value = addr[1]; /* device ID */
510 case (AMD_ID_LV400T & FLASH_ID_MASK):
511 info->flash_id += FLASH_AM400T;
512 info->sector_count = 11;
513 info->size = 0x00100000;
516 case (AMD_ID_LV400B & FLASH_ID_MASK):
517 info->flash_id += FLASH_AM400B;
518 info->sector_count = 11;
519 info->size = 0x00100000;
522 case (AMD_ID_LV800T & FLASH_ID_MASK):
523 info->flash_id += FLASH_AM800T;
524 info->sector_count = 19;
525 info->size = 0x00200000;
528 case (AMD_ID_LV800B & FLASH_ID_MASK):
529 info->flash_id += FLASH_AM800B;
530 info->sector_count = 19;
531 info->size = 0x00200000;
534 case (AMD_ID_LV160T & FLASH_ID_MASK):
535 info->flash_id += FLASH_AM160T;
536 info->sector_count = 35;
537 info->size = 0x00400000;
540 case (AMD_ID_LV160B & FLASH_ID_MASK):
541 info->flash_id += FLASH_AM160B;
542 info->sector_count = 35;
543 info->size = 0x00400000;
545 #if 0 /* enable when device IDs are available */
546 case (AMD_ID_LV320T & FLASH_ID_MASK):
547 info->flash_id += FLASH_AM320T;
548 info->sector_count = 67;
549 info->size = 0x00800000;
552 case (AMD_ID_LV320B & FLASH_ID_MASK):
553 info->flash_id += FLASH_AM320B;
554 info->sector_count = 67;
555 info->size = 0x00800000;
559 case (INTEL_ID_28F800B3T & FLASH_ID_MASK):
560 info->flash_id += FLASH_INTEL800T;
561 info->sector_count = 23;
562 info->size = 0x00200000;
565 case (INTEL_ID_28F800B3B & FLASH_ID_MASK):
566 info->flash_id += FLASH_INTEL800B;
567 info->sector_count = 23;
568 info->size = 0x00200000;
571 case (INTEL_ID_28F160B3T & FLASH_ID_MASK):
572 info->flash_id += FLASH_INTEL160T;
573 info->sector_count = 39;
574 info->size = 0x00400000;
577 case (INTEL_ID_28F160B3B & FLASH_ID_MASK):
578 info->flash_id += FLASH_INTEL160B;
579 info->sector_count = 39;
580 info->size = 0x00400000;
583 case (INTEL_ID_28F320B3T & FLASH_ID_MASK):
584 info->flash_id += FLASH_INTEL320T;
585 info->sector_count = 71;
586 info->size = 0x00800000;
589 case (INTEL_ID_28F320B3B & FLASH_ID_MASK):
590 info->flash_id += FLASH_AM320B;
591 info->sector_count = 71;
592 info->size = 0x00800000;
595 #if 0 /* enable when devices are available */
596 case (INTEL_ID_28F320B3T & FLASH_ID_MASK):
597 info->flash_id += FLASH_INTEL320T;
598 info->sector_count = 135;
599 info->size = 0x01000000;
600 break; /* => 16 MB */
602 case (INTEL_ID_28F320B3B & FLASH_ID_MASK):
603 info->flash_id += FLASH_AM320B;
604 info->sector_count = 135;
605 info->size = 0x01000000;
606 break; /* => 16 MB */
608 case (INTEL_ID_28F320J3A & FLASH_ID_MASK):
609 info->flash_id += FLASH_28F320J3A;
610 info->sector_count = 32;
611 info->size = 0x00400000;
612 break; /* => 32 MBit */
613 case (INTEL_ID_28F640J3A & FLASH_ID_MASK):
614 info->flash_id += FLASH_28F640J3A;
615 info->sector_count = 64;
616 info->size = 0x00800000;
617 break; /* => 64 MBit */
618 case (INTEL_ID_28F128J3A & FLASH_ID_MASK):
619 info->flash_id += FLASH_28F128J3A;
620 info->sector_count = 128;
621 info->size = 0x01000000;
622 break; /* => 128 MBit */
626 info->flash_id = FLASH_UNKNOWN;
627 return (0); /* => no or unknown flash */
630 flash_get_offsets (base, info);
632 /* check for protected sectors */
633 for (i = 0; i < info->sector_count; i++) {
634 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
635 /* D0 = 1 if protected */
636 addr = (volatile FLASH_WORD_SIZE *) (info->start[i]);
637 info->protect[i] = addr[2] & 1;
641 * Prevent writes to uninitialized FLASH.
643 if (info->flash_id != FLASH_UNKNOWN) {
644 addr = (volatile FLASH_WORD_SIZE *) info->start[0];
645 if ((info->flash_id & 0xFF00) == FLASH_MAN_INTEL) {
646 *addr = (0x00F000F0 & FLASH_ID_MASK); /* reset bank */
648 *addr = (0x00FF00FF & FLASH_ID_MASK); /* reset bank */
656 /*-----------------------------------------------------------------------
659 int flash_erase (flash_info_t * info, int s_first, int s_last)
662 volatile FLASH_WORD_SIZE *addr =
663 (volatile FLASH_WORD_SIZE *) (info->start[0]);
664 int flag, prot, sect, l_sect, barf;
665 ulong start, now, last;
668 if ((s_first < 0) || (s_first > s_last)) {
669 if (info->flash_id == FLASH_UNKNOWN) {
670 printf ("- missing\n");
672 printf ("- no sectors to erase\n");
677 if ((info->flash_id == FLASH_UNKNOWN) ||
678 ((info->flash_id > FLASH_AMD_COMP) &&
679 ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL))) {
680 printf ("Can't erase unknown flash type - aborted\n");
685 for (sect = s_first; sect <= s_last; ++sect) {
686 if (info->protect[sect]) {
692 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
699 /* Disable interrupts which might cause a timeout here */
700 flag = disable_interrupts ();
701 if (info->flash_id < FLASH_AMD_COMP) {
702 #ifndef CONFIG_SYS_FLASH_16BIT
703 addr[0x0555] = 0x00AA00AA;
704 addr[0x02AA] = 0x00550055;
705 addr[0x0555] = 0x00800080;
706 addr[0x0555] = 0x00AA00AA;
707 addr[0x02AA] = 0x00550055;
709 addr[0x0555] = 0x00AA;
710 addr[0x02AA] = 0x0055;
711 addr[0x0555] = 0x0080;
712 addr[0x0555] = 0x00AA;
713 addr[0x02AA] = 0x0055;
715 /* Start erase on unprotected sectors */
716 for (sect = s_first; sect <= s_last; sect++) {
717 if (info->protect[sect] == 0) { /* not protected */
718 addr = (volatile FLASH_WORD_SIZE *) (info->
721 addr[0] = (0x00300030 & FLASH_ID_MASK);
726 /* re-enable interrupts if necessary */
728 enable_interrupts ();
730 /* wait at least 80us - let's wait 1 ms */
734 * We wait for the last triggered sector
739 start = get_timer (0);
741 addr = (volatile FLASH_WORD_SIZE *) (info->start[l_sect]);
742 while ((addr[0] & (0x00800080 & FLASH_ID_MASK)) !=
743 (0x00800080 & FLASH_ID_MASK)) {
744 if ((now = get_timer (start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
745 printf ("Timeout\n");
748 /* show that we're waiting */
749 if ((now - last) > 1000000) { /* every second */
756 /* reset to read mode */
757 addr = (volatile FLASH_WORD_SIZE *) info->start[0];
758 addr[0] = (0x00F000F0 & FLASH_ID_MASK); /* reset bank */
762 for (sect = s_first; sect <= s_last; sect++) {
763 if (info->protect[sect] == 0) { /* not protected */
765 #ifndef CONFIG_SYS_FLASH_16BIT
766 addr = (vu_long *) (info->start[sect]);
767 addr[0] = 0x00500050;
768 addr[0] = 0x00200020;
769 addr[0] = 0x00D000D0;
770 while (!(addr[0] & 0x00800080)); /* wait for error or finish */
771 if (addr[0] & 0x003A003A) { /* check for error */
772 barf = addr[0] & 0x003A0000;
776 barf = addr[0] & 0x0000003A;
780 addr = (vu_short *) (info->start[sect]);
781 addr[0] = 0x0050; /* clear status register */
784 while (!(addr[0] & 0x0080)); /* wait for error or finish */
785 if (addr[0] & 0x003A) /* check for error */
786 barf = addr[0] & 0x003A;
789 printf ("\nFlash error in sector at %lx\n", (unsigned long) addr);
791 printf ("Block locked, not erased.\n");
792 if ((barf & 0x0030) == 0x0030)
793 printf ("Command Sequence error.\n");
794 if ((barf & 0x0030) == 0x0020)
795 printf ("Block Erase error.\n");
797 printf ("Vpp Low error.\n");
803 addr = (volatile FLASH_WORD_SIZE *) info->start[0];
804 #ifndef CONFIG_SYS_FLASH_16BIT
805 addr[0] = (0x00FF00FF & FLASH_ID_MASK); /* reset bank */
807 addr[0] = (0x00FF & FLASH_ID_MASK); /* reset bank */
816 /*-----------------------------------------------------------------------
819 /*flash_info_t *addr2info (ulong addr)
824 for (i=0, info=&flash_info[0]; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
825 if ((addr >= info->start[0]) &&
826 (addr < (info->start[0] + info->size)) ) {
834 /*-----------------------------------------------------------------------
835 * Copy memory to flash.
836 * Make sure all target addresses are within Flash bounds,
837 * and no protected sectors are hit.
841 * 2 - Flash not erased
842 * 4 - target range includes protected sectors
843 * 8 - target address not in Flash memory
846 /*int flash_write (uchar *src, ulong addr, ulong cnt)
849 ulong end = addr + cnt - 1;
850 flash_info_t *info_first = addr2info (addr);
851 flash_info_t *info_last = addr2info (end );
858 if (!info_first || !info_last) {
862 for (info = info_first; info <= info_last; ++info) {
863 ulong b_end = info->start[0] + info->size;*/ /* bank end addr */
864 /* short s_end = info->sector_count - 1;
865 for (i=0; i<info->sector_count; ++i) {
866 ulong e_addr = (i == s_end) ? b_end : info->start[i + 1];
868 if ((end >= info->start[i]) && (addr < e_addr) &&
869 (info->protect[i] != 0) ) {
875 */ /* finally write data to flash */
876 /* for (info = info_first; info <= info_last && cnt>0; ++info) {
879 len = info->start[0] + info->size - addr;
882 if ((i = write_buff(info, src, addr, len)) != 0) {
892 /*-----------------------------------------------------------------------
893 * Copy memory to flash, returns:
896 * 2 - Flash not erased
899 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
901 #ifndef CONFIG_SYS_FLASH_16BIT
910 #ifndef CONFIG_SYS_FLASH_16BIT
913 wp = (addr & ~3); /* get lower word aligned address */
916 * handle unaligned start bytes
918 if ((l = addr - wp) != 0) {
920 for (i = 0, cp = wp; i < l; ++i, ++cp) {
921 data = (data << 8) | (*(uchar *) cp);
923 for (; i < 4 && cnt > 0; ++i) {
924 data = (data << 8) | *src++;
928 for (; cnt == 0 && i < 4; ++i, ++cp) {
929 data = (data << 8) | (*(uchar *) cp);
932 if ((rc = write_word (info, wp, data)) != 0) {
939 * handle word aligned part
943 for (i = 0; i < 4; ++i) {
944 data = (data << 8) | *src++;
946 if ((rc = write_word (info, wp, data)) != 0) {
958 * handle unaligned tail bytes
961 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
962 data = (data << 8) | *src++;
965 for (; i < 4; ++i, ++cp) {
966 data = (data << 8) | (*(uchar *) cp);
969 return (write_word (info, wp, data));
972 wp = (addr & ~1); /* get lower word aligned address */
975 * handle unaligned start byte
979 data = (data << 8) | *src++;
981 if ((rc = write_short (info, wp, data)) != 0) {
988 * handle word aligned part
990 /* l = 0; used for debuging */
993 for (i = 0; i < 2; ++i) {
994 data = (data << 8) | *src++;
1000 } used for debuging */
1002 if ((rc = write_short (info, wp, data)) != 0) {
1014 * handle unaligned tail bytes
1017 for (i = 0, cp = wp; i < 2 && cnt > 0; ++i, ++cp) {
1018 data = (data << 8) | *src++;
1021 for (; i < 2; ++i, ++cp) {
1022 data = (data << 8) | (*(uchar *) cp);
1025 return (write_short (info, wp, data));
1031 /*-----------------------------------------------------------------------
1032 * Write a word to Flash, returns:
1035 * 2 - Flash not erased
1037 #ifndef CONFIG_SYS_FLASH_16BIT
1038 static int write_word (flash_info_t * info, ulong dest, ulong data)
1040 vu_long *addr = (vu_long *) (info->start[0]);
1044 #if defined (__MIPSEL__)
1045 data = cpu_to_be32 (data);
1048 /* Check if Flash is (sufficiently) erased */
1049 if ((*((vu_long *) dest) & data) != data) {
1053 /* Disable interrupts which might cause a timeout here */
1054 flag = disable_interrupts ();
1056 if (info->flash_id < FLASH_AMD_COMP) {
1058 addr[0x0555] = 0x00AA00AA;
1059 addr[0x02AA] = 0x00550055;
1060 addr[0x0555] = 0x00A000A0;
1066 *((vu_long *) dest) = data;
1068 /* re-enable interrupts if necessary */
1070 enable_interrupts ();
1072 /* data polling for D7 */
1073 start = get_timer (0);
1075 if (info->flash_id < FLASH_AMD_COMP) {
1077 while ((*((vu_long *) dest) & 0x00800080) !=
1078 (data & 0x00800080)) {
1079 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
1080 printf ("timeout\n");
1087 while (!(addr[0] & 0x00800080)) { /* wait for error or finish */
1088 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
1089 printf ("timeout\n");
1094 if (addr[0] & 0x003A003A) { /* check for error */
1095 barf = addr[0] & 0x003A0000;
1099 barf = addr[0] & 0x0000003A;
1101 printf ("\nFlash write error at address %lx\n",
1102 (unsigned long) dest);
1104 printf ("Block locked, not erased.\n");
1106 printf ("Programming error.\n");
1108 printf ("Vpp Low error.\n");
1120 static int write_short (flash_info_t * info, ulong dest, ushort data)
1122 vu_short *addr = (vu_short *) (info->start[0]);
1126 #if defined (__MIPSEL__)
1127 data = cpu_to_be16 (data);
1130 /* Check if Flash is (sufficiently) erased */
1131 if ((*((vu_short *) dest) & data) != data) {
1135 /* Disable interrupts which might cause a timeout here */
1136 flag = disable_interrupts ();
1138 if (info->flash_id < FLASH_AMD_COMP) {
1140 addr[0x0555] = 0x00AA;
1141 addr[0x02AA] = 0x0055;
1142 addr[0x0555] = 0x00A0;
1148 *((vu_short *) dest) = data;
1150 /* re-enable interrupts if necessary */
1152 enable_interrupts ();
1154 /* data polling for D7 */
1155 start = get_timer (0);
1157 if (info->flash_id < FLASH_AMD_COMP) {
1159 while ((*((vu_short *) dest) & 0x0080) != (data & 0x0080)) {
1160 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
1167 while (!(addr[0] & 0x0080)) { /* wait for error or finish */
1168 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT)
1172 if (addr[0] & 0x003A) { /* check for error */
1173 barf = addr[0] & 0x003A;
1174 printf ("\nFlash write error at address %lx\n",
1175 (unsigned long) dest);
1177 printf ("Block locked, not erased.\n");
1179 printf ("Programming error.\n");
1181 printf ("Vpp Low error.\n");
1186 while (!(addr[0] & 0x0080)) { /* wait for error or finish */
1187 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT)