6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8 * SPDX-License-Identifier: GPL-2.0+
12 #include <asm/ppc4xx.h>
13 #include <asm/u-boot.h>
14 #include <asm/processor.h>
16 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
19 #ifdef CONFIG_SYS_FLASH_16BIT
20 #define FLASH_WORD_SIZE unsigned short
21 #define FLASH_ID_MASK 0xFFFF
23 #define FLASH_WORD_SIZE unsigned long
24 #define FLASH_ID_MASK 0xFFFFFFFF
27 /*-----------------------------------------------------------------------
30 /* stolen from esteem192e/flash.c */
31 ulong flash_get_size (volatile FLASH_WORD_SIZE *addr, flash_info_t *info);
33 #ifndef CONFIG_SYS_FLASH_16BIT
34 static int write_word (flash_info_t *info, ulong dest, ulong data);
36 static int write_short (flash_info_t *info, ulong dest, ushort data);
38 static void flash_get_offsets (ulong base, flash_info_t *info);
41 /*-----------------------------------------------------------------------
44 unsigned long flash_init (void)
46 unsigned long size_b0, size_b1;
49 unsigned long base_b0, base_b1;
51 /* Init: no FLASHes known */
52 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
53 flash_info[i].flash_id = FLASH_UNKNOWN;
56 /* Static FLASH Bank configuration here - FIXME XXX */
58 size_b0 = flash_get_size((volatile FLASH_WORD_SIZE *)FLASH_BASE1_PRELIM, &flash_info[0]);
60 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
61 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
62 size_b0, size_b0<<20);
66 if (CONFIG_SYS_MAX_FLASH_BANKS == 1) {
68 flash_get_offsets (FLASH_BASE1_PRELIM, &flash_info[0]);
70 /* Monitor protection ON by default */
72 (void)flash_protect(FLAG_PROTECT_SET,
73 FLASH_BASE1_PRELIM-CONFIG_SYS_MONITOR_LEN+size_b0,
74 FLASH_BASE1_PRELIM-1+size_b0,
77 (void)flash_protect(FLAG_PROTECT_SET,
78 CONFIG_SYS_MONITOR_BASE,
79 CONFIG_SYS_MONITOR_BASE+CONFIG_SYS_MONITOR_LEN-1,
83 flash_info[0].size = size_b0;
84 } else { /* 2 banks */
85 size_b1 = flash_get_size((volatile FLASH_WORD_SIZE *)FLASH_BASE1_PRELIM, &flash_info[1]);
87 /* Re-do sizing to get full correct info */
89 mtdcr(EBC0_CFGADDR, PB0CR);
90 pbcr = mfdcr(EBC0_CFGDATA);
91 mtdcr(EBC0_CFGADDR, PB0CR);
93 pbcr = (pbcr & 0x0001ffff) | base_b1 | (((size_b1/1024/1024)-1)<<17);
94 mtdcr(EBC0_CFGDATA, pbcr);
98 mtdcr(EBC0_CFGADDR, PB1CR);
99 pbcr = mfdcr(EBC0_CFGDATA);
100 mtdcr(EBC0_CFGADDR, PB1CR);
101 base_b0 = base_b1 - size_b0;
102 pbcr = (pbcr & 0x0001ffff) | base_b0 | (((size_b0/1024/1024)-1)<<17);
103 mtdcr(EBC0_CFGDATA, pbcr);
106 size_b0 = flash_get_size((volatile FLASH_WORD_SIZE *)base_b0, &flash_info[0]);
108 flash_get_offsets (base_b0, &flash_info[0]);
110 /* monitor protection ON by default */
112 (void)flash_protect(FLAG_PROTECT_SET,
113 FLASH_BASE1_PRELIM-CONFIG_SYS_MONITOR_LEN+size_b0,
114 FLASH_BASE1_PRELIM-1+size_b0,
117 (void)flash_protect(FLAG_PROTECT_SET,
118 CONFIG_SYS_MONITOR_BASE,
119 CONFIG_SYS_MONITOR_BASE+CONFIG_SYS_MONITOR_LEN-1,
124 /* Re-do sizing to get full correct info */
125 size_b1 = flash_get_size((volatile FLASH_WORD_SIZE *)base_b1, &flash_info[1]);
127 flash_get_offsets (base_b1, &flash_info[1]);
129 /* monitor protection ON by default */
130 (void)flash_protect(FLAG_PROTECT_SET,
131 base_b1+size_b1-CONFIG_SYS_MONITOR_LEN,
134 /* monitor protection OFF by default (one is enough) */
135 (void)flash_protect(FLAG_PROTECT_CLEAR,
136 base_b0+size_b0-CONFIG_SYS_MONITOR_LEN,
140 flash_info[1].flash_id = FLASH_UNKNOWN;
141 flash_info[1].sector_count = -1;
144 flash_info[0].size = size_b0;
145 flash_info[1].size = size_b1;
147 return (size_b0 + size_b1);
151 /*-----------------------------------------------------------------------
154 static void flash_get_offsets (ulong base, flash_info_t *info)
158 /* set up sector start adress table */
159 if ((info->flash_id & FLASH_TYPEMASK) == INTEL_ID_28F320J3A ||
160 (info->flash_id & FLASH_TYPEMASK) == INTEL_ID_28F640J3A ||
161 (info->flash_id & FLASH_TYPEMASK) == INTEL_ID_28F128J3A) {
162 for (i = 0; i < info->sector_count; i++) {
163 info->start[i] = base + (i * info->size/info->sector_count);
166 else if (info->flash_id & FLASH_BTYPE) {
167 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
169 #ifndef CONFIG_SYS_FLASH_16BIT
170 /* set sector offsets for bottom boot block type */
171 info->start[0] = base + 0x00000000;
172 info->start[1] = base + 0x00004000;
173 info->start[2] = base + 0x00008000;
174 info->start[3] = base + 0x0000C000;
175 info->start[4] = base + 0x00010000;
176 info->start[5] = base + 0x00014000;
177 info->start[6] = base + 0x00018000;
178 info->start[7] = base + 0x0001C000;
179 for (i = 8; i < info->sector_count; i++) {
180 info->start[i] = base + (i * 0x00020000) - 0x000E0000;
183 /* set sector offsets for bottom boot block type */
184 info->start[0] = base + 0x00000000;
185 info->start[1] = base + 0x00008000;
186 info->start[2] = base + 0x0000C000;
187 info->start[3] = base + 0x00010000;
188 for (i = 4; i < info->sector_count; i++) {
189 info->start[i] = base + (i * 0x00020000) - 0x00060000;
193 /* set sector offsets for bottom boot block type */
194 info->start[0] = base + 0x00000000;
195 info->start[1] = base + 0x00002000;
196 info->start[2] = base + 0x00004000;
197 info->start[3] = base + 0x00006000;
198 info->start[4] = base + 0x00008000;
199 info->start[5] = base + 0x0000A000;
200 info->start[6] = base + 0x0000C000;
201 info->start[7] = base + 0x0000E000;
202 for (i = 8; i < info->sector_count; i++) {
203 info->start[i] = base + (i * 0x00010000) - 0x00070000;
206 /* set sector offsets for bottom boot block type */
207 info->start[0] = base + 0x00000000;
208 info->start[1] = base + 0x00004000;
209 info->start[2] = base + 0x00006000;
210 info->start[3] = base + 0x00008000;
211 for (i = 4; i < info->sector_count; i++) {
212 info->start[i] = base + (i * 0x00010000) - 0x00030000;
217 /* set sector offsets for top boot block type */
218 i = info->sector_count - 1;
219 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
221 #ifndef CONFIG_SYS_FLASH_16BIT
222 info->start[i--] = base + info->size - 0x00004000;
223 info->start[i--] = base + info->size - 0x00008000;
224 info->start[i--] = base + info->size - 0x0000C000;
225 info->start[i--] = base + info->size - 0x00010000;
226 info->start[i--] = base + info->size - 0x00014000;
227 info->start[i--] = base + info->size - 0x00018000;
228 info->start[i--] = base + info->size - 0x0001C000;
229 for (; i >= 0; i--) {
230 info->start[i] = base + i * 0x00020000;
234 info->start[i--] = base + info->size - 0x00008000;
235 info->start[i--] = base + info->size - 0x0000C000;
236 info->start[i--] = base + info->size - 0x00010000;
237 for (; i >= 0; i--) {
238 info->start[i] = base + i * 0x00020000;
242 info->start[i--] = base + info->size - 0x00002000;
243 info->start[i--] = base + info->size - 0x00004000;
244 info->start[i--] = base + info->size - 0x00006000;
245 info->start[i--] = base + info->size - 0x00008000;
246 info->start[i--] = base + info->size - 0x0000A000;
247 info->start[i--] = base + info->size - 0x0000C000;
248 info->start[i--] = base + info->size - 0x0000E000;
249 for (; i >= 0; i--) {
250 info->start[i] = base + i * 0x00010000;
253 info->start[i--] = base + info->size - 0x00004000;
254 info->start[i--] = base + info->size - 0x00006000;
255 info->start[i--] = base + info->size - 0x00008000;
256 for (; i >= 0; i--) {
257 info->start[i] = base + i * 0x00010000;
264 /*-----------------------------------------------------------------------
267 void flash_print_info (flash_info_t *info)
271 uchar botboot[]=", bottom boot sect)\n";
272 uchar topboot[]=", top boot sector)\n";
274 if (info->flash_id == FLASH_UNKNOWN) {
275 printf ("missing or unknown FLASH type\n");
279 switch (info->flash_id & FLASH_VENDMASK) {
280 case FLASH_MAN_AMD: printf ("AMD "); break;
281 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
282 case FLASH_MAN_SST: printf ("SST "); break;
283 case FLASH_MAN_STM: printf ("STM "); break;
284 case FLASH_MAN_INTEL: printf ("INTEL "); break;
285 default: printf ("Unknown Vendor "); break;
288 if (info->flash_id & 0x0001 ) {
294 switch (info->flash_id & FLASH_TYPEMASK) {
295 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit%s",boottype);
297 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit%s",boottype);
299 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit%s",boottype);
301 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit%s",boottype);
303 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit%s",boottype);
305 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit%s",boottype);
307 case FLASH_AM320B: printf ("AM29LV320B (32 Mbit%s",boottype);
309 case FLASH_AM320T: printf ("AM29LV320T (32 Mbit%s",boottype);
311 case FLASH_INTEL800B: printf ("INTEL28F800B (8 Mbit%s",boottype);
313 case FLASH_INTEL800T: printf ("INTEL28F800T (8 Mbit%s",boottype);
315 case FLASH_INTEL160B: printf ("INTEL28F160B (16 Mbit%s",boottype);
317 case FLASH_INTEL160T: printf ("INTEL28F160T (16 Mbit%s",boottype);
319 case FLASH_INTEL320B: printf ("INTEL28F320B (32 Mbit%s",boottype);
321 case FLASH_INTEL320T: printf ("INTEL28F320T (32 Mbit%s",boottype);
323 case FLASH_AMDL322T: printf ("AM29DL322T (32 Mbit%s",boottype);
326 #if 0 /* enable when devices are available */
328 case FLASH_INTEL640B: printf ("INTEL28F640B (64 Mbit%s",boottype);
330 case FLASH_INTEL640T: printf ("INTEL28F640T (64 Mbit%s",boottype);
333 case INTEL_ID_28F320J3A: printf ("INTEL28F320JA3 (32 Mbit%s",boottype);
335 case INTEL_ID_28F640J3A: printf ("INTEL28F640JA3 (64 Mbit%s",boottype);
337 case INTEL_ID_28F128J3A: printf ("INTEL28F128JA3 (128 Mbit%s",boottype);
340 default: printf ("Unknown Chip Type\n");
344 printf (" Size: %ld MB in %d Sectors\n",
345 info->size >> 20, info->sector_count);
347 printf (" Sector Start Addresses:");
348 for (i=0; i<info->sector_count; ++i) {
353 info->protect[i] ? " (RO)" : " "
361 /*-----------------------------------------------------------------------
365 /*-----------------------------------------------------------------------
369 * The following code cannot be run from FLASH!
371 ulong flash_get_size (volatile FLASH_WORD_SIZE *addr, flash_info_t *info)
374 ulong base = (ulong)addr;
375 FLASH_WORD_SIZE value;
377 /* Write auto select command: read Manufacturer ID */
380 #ifndef CONFIG_SYS_FLASH_16BIT
383 * Note: if it is an AMD flash and the word at addr[0000]
384 * is 0x00890089 this routine will think it is an Intel
385 * flash device and may(most likely) cause trouble.
388 addr[0x0000] = 0x00900090;
389 if(addr[0x0000] != 0x00890089){
390 addr[0x0555] = 0x00AA00AA;
391 addr[0x02AA] = 0x00550055;
392 addr[0x0555] = 0x00900090;
396 * Note: if it is an AMD flash and the word at addr[0000]
397 * is 0x0089 this routine will think it is an Intel
398 * flash device and may(most likely) cause trouble.
401 addr[0x0000] = 0x0090;
403 if(addr[0x0000] != 0x0089){
404 addr[0x0555] = 0x00AA;
405 addr[0x02AA] = 0x0055;
406 addr[0x0555] = 0x0090;
412 case (AMD_MANUFACT & FLASH_ID_MASK):
413 info->flash_id = FLASH_MAN_AMD;
415 case (FUJ_MANUFACT & FLASH_ID_MASK):
416 info->flash_id = FLASH_MAN_FUJ;
418 case (STM_MANUFACT & FLASH_ID_MASK):
419 info->flash_id = FLASH_MAN_STM;
421 case (SST_MANUFACT & FLASH_ID_MASK):
422 info->flash_id = FLASH_MAN_SST;
424 case (INTEL_MANUFACT & FLASH_ID_MASK):
425 info->flash_id = FLASH_MAN_INTEL;
428 info->flash_id = FLASH_UNKNOWN;
429 info->sector_count = 0;
431 return (0); /* no or unknown flash */
435 value = addr[1]; /* device ID */
439 case (AMD_ID_LV400T & FLASH_ID_MASK):
440 info->flash_id += FLASH_AM400T;
441 info->sector_count = 11;
442 info->size = 0x00100000;
445 case (AMD_ID_LV400B & FLASH_ID_MASK):
446 info->flash_id += FLASH_AM400B;
447 info->sector_count = 11;
448 info->size = 0x00100000;
451 case (AMD_ID_LV800T & FLASH_ID_MASK):
452 info->flash_id += FLASH_AM800T;
453 info->sector_count = 19;
454 info->size = 0x00200000;
457 case (AMD_ID_LV800B & FLASH_ID_MASK):
458 info->flash_id += FLASH_AM800B;
459 info->sector_count = 19;
460 info->size = 0x00200000;
463 case (AMD_ID_LV160T & FLASH_ID_MASK):
464 info->flash_id += FLASH_AM160T;
465 info->sector_count = 35;
466 info->size = 0x00400000;
469 case (AMD_ID_LV160B & FLASH_ID_MASK):
470 info->flash_id += FLASH_AM160B;
471 info->sector_count = 35;
472 info->size = 0x00400000;
474 #if 0 /* enable when device IDs are available */
475 case (AMD_ID_LV320T & FLASH_ID_MASK):
476 info->flash_id += FLASH_AM320T;
477 info->sector_count = 67;
478 info->size = 0x00800000;
481 case (AMD_ID_LV320B & FLASH_ID_MASK):
482 info->flash_id += FLASH_AM320B;
483 info->sector_count = 67;
484 info->size = 0x00800000;
488 case (AMD_ID_DL322T & FLASH_ID_MASK):
489 info->flash_id += FLASH_AMDL322T;
490 info->sector_count = 71;
491 info->size = 0x00800000;
494 case (INTEL_ID_28F800B3T & FLASH_ID_MASK):
495 info->flash_id += FLASH_INTEL800T;
496 info->sector_count = 23;
497 info->size = 0x00200000;
500 case (INTEL_ID_28F800B3B & FLASH_ID_MASK):
501 info->flash_id += FLASH_INTEL800B;
502 info->sector_count = 23;
503 info->size = 0x00200000;
506 case (INTEL_ID_28F160B3T & FLASH_ID_MASK):
507 info->flash_id += FLASH_INTEL160T;
508 info->sector_count = 39;
509 info->size = 0x00400000;
512 case (INTEL_ID_28F160B3B & FLASH_ID_MASK):
513 info->flash_id += FLASH_INTEL160B;
514 info->sector_count = 39;
515 info->size = 0x00400000;
518 case (INTEL_ID_28F320B3T & FLASH_ID_MASK):
519 info->flash_id += FLASH_INTEL320T;
520 info->sector_count = 71;
521 info->size = 0x00800000;
524 case (INTEL_ID_28F320B3B & FLASH_ID_MASK):
525 info->flash_id += FLASH_AM320B;
526 info->sector_count = 71;
527 info->size = 0x00800000;
530 #if 0 /* enable when devices are available */
531 case (INTEL_ID_28F320B3T & FLASH_ID_MASK):
532 info->flash_id += FLASH_INTEL320T;
533 info->sector_count = 135;
534 info->size = 0x01000000;
535 break; /* => 16 MB */
537 case (INTEL_ID_28F320B3B & FLASH_ID_MASK):
538 info->flash_id += FLASH_AM320B;
539 info->sector_count = 135;
540 info->size = 0x01000000;
541 break; /* => 16 MB */
543 case (INTEL_ID_28F320J3A & FLASH_ID_MASK):
544 info->flash_id += FLASH_28F320J3A;
545 info->sector_count = 32;
546 info->size = 0x00400000;
547 break; /* => 32 MBit */
548 case (INTEL_ID_28F640J3A & FLASH_ID_MASK):
549 info->flash_id += FLASH_28F640J3A;
550 info->sector_count = 64;
551 info->size = 0x00800000;
552 break; /* => 64 MBit */
553 case (INTEL_ID_28F128J3A & FLASH_ID_MASK):
554 info->flash_id += FLASH_28F128J3A;
555 info->sector_count = 128;
556 info->size = 0x01000000;
557 break; /* => 128 MBit */
561 info->flash_id = FLASH_UNKNOWN;
562 return (0); /* => no or unknown flash */
565 flash_get_offsets(base, info);
567 /* check for protected sectors */
568 for (i = 0; i < info->sector_count; i++) {
569 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
570 /* D0 = 1 if protected */
571 addr = (volatile FLASH_WORD_SIZE *)(info->start[i]);
572 info->protect[i] = addr[2] & 1;
576 * Prevent writes to uninitialized FLASH.
578 if (info->flash_id != FLASH_UNKNOWN) {
579 addr = (volatile FLASH_WORD_SIZE *)info->start[0];
580 if( (info->flash_id & 0xFF00) == FLASH_MAN_INTEL){
581 *addr = (0x00F000F0 & FLASH_ID_MASK); /* reset bank */
583 *addr = (0x00FF00FF & FLASH_ID_MASK); /* reset bank */
591 /*-----------------------------------------------------------------------
594 int flash_erase (flash_info_t * info, int s_first, int s_last)
597 volatile FLASH_WORD_SIZE *addr =
598 (volatile FLASH_WORD_SIZE *) (info->start[0]);
599 int flag, prot, sect, l_sect, barf;
600 ulong start, now, last;
603 if ((s_first < 0) || (s_first > s_last)) {
604 if (info->flash_id == FLASH_UNKNOWN) {
605 printf ("- missing\n");
607 printf ("- no sectors to erase\n");
612 if ((info->flash_id == FLASH_UNKNOWN) ||
613 ((info->flash_id > FLASH_AMD_COMP) &&
614 ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL))) {
615 printf ("Can't erase unknown flash type - aborted\n");
620 for (sect = s_first; sect <= s_last; ++sect) {
621 if (info->protect[sect]) {
627 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
634 /* Disable interrupts which might cause a timeout here */
635 flag = disable_interrupts ();
636 if (info->flash_id < FLASH_AMD_COMP) {
637 #ifndef CONFIG_SYS_FLASH_16BIT
638 addr[0x0555] = 0x00AA00AA;
639 addr[0x02AA] = 0x00550055;
640 addr[0x0555] = 0x00800080;
641 addr[0x0555] = 0x00AA00AA;
642 addr[0x02AA] = 0x00550055;
644 addr[0x0555] = 0x00AA;
645 addr[0x02AA] = 0x0055;
646 addr[0x0555] = 0x0080;
647 addr[0x0555] = 0x00AA;
648 addr[0x02AA] = 0x0055;
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 = (volatile FLASH_WORD_SIZE *) (info->start[sect]);
654 addr[0] = (0x00300030 & FLASH_ID_MASK);
659 /* re-enable interrupts if necessary */
661 enable_interrupts ();
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 FLASH_WORD_SIZE *) (info->start[l_sect]);
675 while ((addr[0] & (0x00800080 & FLASH_ID_MASK)) !=
676 (0x00800080 & FLASH_ID_MASK)) {
677 if ((now = get_timer (start)) > CONFIG_SYS_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 FLASH_WORD_SIZE *) info->start[0];
691 addr[0] = (0x00F000F0 & FLASH_ID_MASK); /* reset bank */
695 for (sect = s_first; sect <= s_last; sect++) {
696 if (info->protect[sect] == 0) { /* not protected */
698 #ifndef CONFIG_SYS_FLASH_16BIT
699 addr = (vu_long *) (info->start[sect]);
700 addr[0] = 0x00200020;
701 addr[0] = 0x00D000D0;
702 while (!(addr[0] & 0x00800080)); /* wait for error or finish */
703 if (addr[0] & 0x003A003A) { /* check for error */
704 barf = addr[0] & 0x003A0000;
708 barf = addr[0] & 0x0000003A;
712 addr = (vu_short *) (info->start[sect]);
715 while (!(addr[0] & 0x0080)); /* wait for error or finish */
716 if (addr[0] & 0x003A) /* check for error */
717 barf = addr[0] & 0x003A;
720 printf ("\nFlash error in sector at %lx\n",
721 (unsigned long) addr);
723 printf ("Block locked, not erased.\n");
724 if ((barf & 0x0030) == 0x0030)
725 printf ("Command Sequence error.\n");
726 if ((barf & 0x0030) == 0x0020)
727 printf ("Block Erase error.\n");
729 printf ("Vpp Low error.\n");
735 addr = (volatile FLASH_WORD_SIZE *) info->start[0];
736 addr[0] = (0x00FF00FF & FLASH_ID_MASK); /* reset bank */
745 /*-----------------------------------------------------------------------
748 /*flash_info_t *addr2info (ulong addr)
753 for (i=0, info=&flash_info[0]; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
754 if ((addr >= info->start[0]) &&
755 (addr < (info->start[0] + info->size)) ) {
763 /*-----------------------------------------------------------------------
764 * Copy memory to flash.
765 * Make sure all target addresses are within Flash bounds,
766 * and no protected sectors are hit.
770 * 2 - Flash not erased
771 * 4 - target range includes protected sectors
772 * 8 - target address not in Flash memory
775 /*int flash_write (uchar *src, ulong addr, ulong cnt)
778 ulong end = addr + cnt - 1;
779 flash_info_t *info_first = addr2info (addr);
780 flash_info_t *info_last = addr2info (end );
787 if (!info_first || !info_last) {
791 for (info = info_first; info <= info_last; ++info) {
792 ulong b_end = info->start[0] + info->size;*/ /* bank end addr */
793 /* short s_end = info->sector_count - 1;
794 for (i=0; i<info->sector_count; ++i) {
795 ulong e_addr = (i == s_end) ? b_end : info->start[i + 1];
797 if ((end >= info->start[i]) && (addr < e_addr) &&
798 (info->protect[i] != 0) ) {
804 */ /* finally write data to flash */
805 /* for (info = info_first; info <= info_last && cnt>0; ++info) {
808 len = info->start[0] + info->size - addr;
811 if ((i = write_buff(info, src, addr, len)) != 0) {
821 /*-----------------------------------------------------------------------
822 * Copy memory to flash, returns:
825 * 2 - Flash not erased
828 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
830 #ifndef CONFIG_SYS_FLASH_16BIT
839 #ifndef CONFIG_SYS_FLASH_16BIT
842 wp = (addr & ~3); /* get lower word aligned address */
845 * handle unaligned start bytes
847 if ((l = addr - wp) != 0) {
849 for (i=0, cp=wp; i<l; ++i, ++cp) {
850 data = (data << 8) | (*(uchar *)cp);
852 for (; i<4 && cnt>0; ++i) {
853 data = (data << 8) | *src++;
857 for (; cnt==0 && i<4; ++i, ++cp) {
858 data = (data << 8) | (*(uchar *)cp);
861 if ((rc = write_word(info, wp, data)) != 0) {
868 * handle word aligned part
872 for (i=0; i<4; ++i) {
873 data = (data << 8) | *src++;
875 if ((rc = write_word(info, wp, data)) != 0) {
887 * handle unaligned tail bytes
890 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
891 data = (data << 8) | *src++;
894 for (; i<4; ++i, ++cp) {
895 data = (data << 8) | (*(uchar *)cp);
898 return (write_word(info, wp, data));
901 wp = (addr & ~1); /* get lower word aligned address */
904 * handle unaligned start byte
908 data = (data << 8) | *src++;
910 if ((rc = write_short(info, wp, data)) != 0) {
917 * handle word aligned part
919 /* l = 0; used for debuging */
922 for (i=0; i<2; ++i) {
923 data = (data << 8) | *src++;
929 } used for debuging */
931 if ((rc = write_short(info, wp, data)) != 0) {
943 * handle unaligned tail bytes
946 for (i=0, cp=wp; i<2 && cnt>0; ++i, ++cp) {
947 data = (data << 8) | *src++;
950 for (; i<2; ++i, ++cp) {
951 data = (data << 8) | (*(uchar *)cp);
954 return (write_short(info, wp, data));
960 /*-----------------------------------------------------------------------
961 * Write a word to Flash, returns:
964 * 2 - Flash not erased
966 #ifndef CONFIG_SYS_FLASH_16BIT
967 static int write_word (flash_info_t *info, ulong dest, ulong data)
969 vu_long *addr = (vu_long*)(info->start[0]);
974 /* Check if Flash is (sufficiently) erased */
975 if ((*((vu_long *)dest) & data) != data) {
979 /* Disable interrupts which might cause a timeout here */
980 flag = disable_interrupts();
982 if(info->flash_id > FLASH_AMD_COMP) {
984 addr[0x0555] = 0x00AA00AA;
985 addr[0x02AA] = 0x00550055;
986 addr[0x0555] = 0x00A000A0;
991 *((vu_long *)dest) = data;
993 /* re-enable interrupts if necessary */
997 /* data polling for D7 */
998 start = get_timer (0);
1000 if(info->flash_id > FLASH_AMD_COMP) {
1001 while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
1002 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
1007 while(!(addr[0] & 0x00800080)) { /* wait for error or finish */
1008 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
1012 if( addr[0] & 0x003A003A) { /* check for error */
1013 barf = addr[0] & 0x003A0000;
1017 barf = addr[0] & 0x0000003A;
1019 printf("\nFlash write error at address %lx\n",(unsigned long)dest);
1020 if(barf & 0x0002) printf("Block locked, not erased.\n");
1021 if(barf & 0x0010) printf("Programming error.\n");
1022 if(barf & 0x0008) printf("Vpp Low error.\n");
1032 static int write_short (flash_info_t *info, ulong dest, ushort data)
1034 vu_short *addr = (vu_short*)(info->start[0]);
1038 /* Check if Flash is (sufficiently) erased */
1039 if ((*((vu_short *)dest) & data) != data) {
1043 /* Disable interrupts which might cause a timeout here */
1044 flag = disable_interrupts();
1046 if(info->flash_id < FLASH_AMD_COMP) {
1048 addr[0x0555] = 0x00AA;
1049 addr[0x02AA] = 0x0055;
1050 addr[0x0555] = 0x00A0;
1056 *((vu_short *)dest) = data;
1058 /* re-enable interrupts if necessary */
1060 enable_interrupts();
1062 /* data polling for D7 */
1063 start = get_timer (0);
1065 if(info->flash_id < FLASH_AMD_COMP) {
1067 while ((*((vu_short *)dest) & 0x0080) != (data & 0x0080)) {
1068 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
1075 while(!(addr[0] & 0x0080)){ /* wait for error or finish */
1076 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) return (1);
1079 if( addr[0] & 0x003A) { /* check for error */
1080 barf = addr[0] & 0x003A;
1081 printf("\nFlash write error at address %lx\n",(unsigned long)dest);
1082 if(barf & 0x0002) printf("Block locked, not erased.\n");
1083 if(barf & 0x0010) printf("Programming error.\n");
1084 if(barf & 0x0008) printf("Vpp Low error.\n");
1089 while(!(addr[0] & 0x0080)){ /* wait for error or finish */
1090 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) return (1);
1099 /*-----------------------------------------------------------------------*/