3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
11 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
13 #ifdef CONFIG_FLASH_16BIT
14 #define FLASH_WORD_SIZE unsigned short
15 #define FLASH_ID_MASK 0xFFFF
17 #define FLASH_WORD_SIZE unsigned long
18 #define FLASH_ID_MASK 0xFFFFFFFF
21 /*-----------------------------------------------------------------------
25 ulong flash_get_size (volatile FLASH_WORD_SIZE * addr, flash_info_t * info);
27 #ifndef CONFIG_FLASH_16BIT
28 static int write_word (flash_info_t * info, ulong dest, ulong data);
30 static int write_short (flash_info_t * info, ulong dest, ushort data);
32 /*int flash_write (uchar *, ulong, ulong); */
33 /*flash_info_t *addr2info (ulong); */
35 static void flash_get_offsets (ulong base, flash_info_t * info);
37 /*-----------------------------------------------------------------------
39 unsigned long flash_init (void)
41 volatile immap_t *immap = (immap_t *) CONFIG_SYS_IMMR;
42 volatile memctl8xx_t *memctl = &immap->im_memctl;
43 unsigned long size_b0, size_b1;
46 /* Init: no FLASHes known */
47 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
48 flash_info[i].flash_id = FLASH_UNKNOWN;
51 /* Static FLASH Bank configuration here - FIXME XXX */
54 flash_get_size ((volatile FLASH_WORD_SIZE *)
55 FLASH_BASE0_PRELIM, &flash_info[0]);
56 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
57 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n", size_b0, size_b0 << 20);
61 flash_get_size ((volatile FLASH_WORD_SIZE *)
62 FLASH_BASE1_PRELIM, &flash_info[1]);
64 if (size_b1 > size_b0) {
66 "Bank 1 (0x%08lx = %ld MB) > Bank 0 (0x%08lx = %ld MB)\n",
67 size_b1, size_b1 << 20, size_b0, size_b0 << 20);
68 flash_info[0].flash_id = FLASH_UNKNOWN;
69 flash_info[1].flash_id = FLASH_UNKNOWN;
70 flash_info[0].sector_count = -1;
71 flash_info[1].sector_count = -1;
72 flash_info[0].size = 0;
73 flash_info[1].size = 0;
77 /* Remap FLASH according to real size */
78 memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b0 & 0xFFFF8000);
79 memctl->memc_br0 = CONFIG_SYS_FLASH_BASE | 0x00000801; /* (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) | BR_MS_GPCM | BR_V; */
81 /* Re-do sizing to get full correct info */
83 size_b0 = flash_get_size ((volatile FLASH_WORD_SIZE *) CONFIG_SYS_FLASH_BASE,
85 flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
87 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
88 /* monitor protection ON by default */
89 (void) flash_protect (FLAG_PROTECT_SET,
90 CONFIG_SYS_MONITOR_BASE,
91 CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
97 CONFIG_SYS_OR_TIMING_FLASH | (-size_b1 & 0xFFFF8000);
99 (CONFIG_SYS_FLASH_BASE | 0x00000801) + (size_b0 & BR_BA_MSK);
100 /*((CONFIG_SYS_FLASH_BASE + size_b0) & BR_BA_MSK) |
101 BR_MS_GPCM | BR_V; */
103 /* Re-do sizing to get full correct info */
105 flash_get_size ((volatile FLASH_WORD_SIZE
106 *) (CONFIG_SYS_FLASH_BASE + size_b0),
109 flash_get_offsets (CONFIG_SYS_FLASH_BASE + size_b0, &flash_info[1]);
111 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
112 /* monitor protection ON by default */
113 (void) flash_protect (FLAG_PROTECT_SET,
114 CONFIG_SYS_MONITOR_BASE,
115 CONFIG_SYS_MONITOR_BASE + monitor_flash_len -
119 memctl->memc_br1 = 0; /* invalidate bank */
121 flash_info[1].flash_id = FLASH_UNKNOWN;
122 flash_info[1].sector_count = -1;
125 flash_info[0].size = size_b0;
126 flash_info[1].size = size_b1;
128 return (size_b0 + size_b1);
131 /*-----------------------------------------------------------------------
133 static void flash_get_offsets (ulong base, flash_info_t * info)
137 /* set up sector start adress table */
138 if (info->flash_id & FLASH_BTYPE) {
139 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
141 #ifndef CONFIG_FLASH_16BIT
142 /* set sector offsets for bottom boot block type */
143 info->start[0] = base + 0x00000000;
144 info->start[1] = base + 0x00004000;
145 info->start[2] = base + 0x00008000;
146 info->start[3] = base + 0x0000C000;
147 info->start[4] = base + 0x00010000;
148 info->start[5] = base + 0x00014000;
149 info->start[6] = base + 0x00018000;
150 info->start[7] = base + 0x0001C000;
151 for (i = 8; i < info->sector_count; i++) {
153 base + (i * 0x00020000) - 0x000E0000;
156 /* set sector offsets for bottom boot block type */
157 info->start[0] = base + 0x00000000;
158 info->start[1] = base + 0x00008000;
159 info->start[2] = base + 0x0000C000;
160 info->start[3] = base + 0x00010000;
161 for (i = 4; i < info->sector_count; i++) {
163 base + (i * 0x00020000) - 0x00060000;
167 /* set sector offsets for bottom boot block type */
168 info->start[0] = base + 0x00000000;
169 info->start[1] = base + 0x00002000;
170 info->start[2] = base + 0x00004000;
171 info->start[3] = base + 0x00006000;
172 info->start[4] = base + 0x00008000;
173 info->start[5] = base + 0x0000A000;
174 info->start[6] = base + 0x0000C000;
175 info->start[7] = base + 0x0000E000;
176 for (i = 8; i < info->sector_count; i++) {
178 base + (i * 0x00010000) - 0x00070000;
181 /* set sector offsets for bottom boot block type */
182 info->start[0] = base + 0x00000000;
183 info->start[1] = base + 0x00004000;
184 info->start[2] = base + 0x00006000;
185 info->start[3] = base + 0x00008000;
186 for (i = 4; i < info->sector_count; i++) {
188 base + (i * 0x00010000) - 0x00030000;
193 /* set sector offsets for top boot block type */
194 i = info->sector_count - 1;
195 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
197 #ifndef CONFIG_FLASH_16BIT
198 info->start[i--] = base + info->size - 0x00004000;
199 info->start[i--] = base + info->size - 0x00008000;
200 info->start[i--] = base + info->size - 0x0000C000;
201 info->start[i--] = base + info->size - 0x00010000;
202 info->start[i--] = base + info->size - 0x00014000;
203 info->start[i--] = base + info->size - 0x00018000;
204 info->start[i--] = base + info->size - 0x0001C000;
205 for (; i >= 0; i--) {
206 info->start[i] = base + i * 0x00020000;
211 info->start[i--] = base + info->size - 0x00008000;
212 info->start[i--] = base + info->size - 0x0000C000;
213 info->start[i--] = base + info->size - 0x00010000;
214 for (; i >= 0; i--) {
215 info->start[i] = base + i * 0x00020000;
219 info->start[i--] = base + info->size - 0x00002000;
220 info->start[i--] = base + info->size - 0x00004000;
221 info->start[i--] = base + info->size - 0x00006000;
222 info->start[i--] = base + info->size - 0x00008000;
223 info->start[i--] = base + info->size - 0x0000A000;
224 info->start[i--] = base + info->size - 0x0000C000;
225 info->start[i--] = base + info->size - 0x0000E000;
226 for (; i >= 0; i--) {
227 info->start[i] = base + i * 0x00010000;
232 info->start[i--] = base + info->size - 0x00004000;
233 info->start[i--] = base + info->size - 0x00006000;
234 info->start[i--] = base + info->size - 0x00008000;
235 for (; i >= 0; i--) {
236 info->start[i] = base + i * 0x00010000;
245 /*-----------------------------------------------------------------------
247 void flash_print_info (flash_info_t * info)
251 uchar botboot[] = ", bottom boot sect)\n";
252 uchar topboot[] = ", top boot sector)\n";
254 if (info->flash_id == FLASH_UNKNOWN) {
255 printf ("missing or unknown FLASH type\n");
259 switch (info->flash_id & FLASH_VENDMASK) {
272 case FLASH_MAN_INTEL:
276 printf ("Unknown Vendor ");
280 if (info->flash_id & 0x0001) {
286 switch (info->flash_id & FLASH_TYPEMASK) {
288 printf ("AM29LV400B (4 Mbit%s", boottype);
291 printf ("AM29LV400T (4 Mbit%s", boottype);
294 printf ("AM29LV800B (8 Mbit%s", boottype);
297 printf ("AM29LV800T (8 Mbit%s", boottype);
300 printf ("AM29LV160B (16 Mbit%s", boottype);
303 printf ("AM29LV160T (16 Mbit%s", boottype);
306 printf ("AM29LV320B (32 Mbit%s", boottype);
309 printf ("AM29LV320T (32 Mbit%s", boottype);
311 case FLASH_INTEL800B:
312 printf ("INTEL28F800B (8 Mbit%s", boottype);
314 case FLASH_INTEL800T:
315 printf ("INTEL28F800T (8 Mbit%s", boottype);
317 case FLASH_INTEL160B:
318 printf ("INTEL28F160B (16 Mbit%s", boottype);
320 case FLASH_INTEL160T:
321 printf ("INTEL28F160T (16 Mbit%s", boottype);
323 case FLASH_INTEL320B:
324 printf ("INTEL28F320B (32 Mbit%s", boottype);
326 case FLASH_INTEL320T:
327 printf ("INTEL28F320T (32 Mbit%s", boottype);
330 #if 0 /* enable when devices are available */
332 case FLASH_INTEL640B:
333 printf ("INTEL28F640B (64 Mbit%s", boottype);
335 case FLASH_INTEL640T:
336 printf ("INTEL28F640T (64 Mbit%s", boottype);
341 printf ("Unknown Chip Type\n");
345 printf (" Size: %ld MB in %d Sectors\n",
346 info->size >> 20, info->sector_count);
348 printf (" Sector Start Addresses:");
349 for (i = 0; i < info->sector_count; ++i) {
353 info->start[i], info->protect[i] ? " (RO)" : " ");
359 /*-----------------------------------------------------------------------
363 /*-----------------------------------------------------------------------
367 * The following code cannot be run from FLASH!
369 ulong flash_get_size (volatile FLASH_WORD_SIZE * addr, flash_info_t * info)
372 ulong base = (ulong) addr;
373 FLASH_WORD_SIZE value;
375 /* Write auto select command: read Manufacturer ID */
378 #ifndef CONFIG_FLASH_16BIT
381 * Note: if it is an AMD flash and the word at addr[0000]
382 * is 0x00890089 this routine will think it is an Intel
383 * flash device and may(most likely) cause trouble.
386 addr[0x0000] = 0x00900090;
387 if (addr[0x0000] != 0x00890089) {
388 addr[0x0555] = 0x00AA00AA;
389 addr[0x02AA] = 0x00550055;
390 addr[0x0555] = 0x00900090;
394 * Note: if it is an AMD flash and the word at addr[0000]
395 * is 0x0089 this routine will think it is an Intel
396 * flash device and may(most likely) cause trouble.
399 addr[0x0000] = 0x0090;
401 if (addr[0x0000] != 0x0089) {
402 addr[0x0555] = 0x00AA;
403 addr[0x02AA] = 0x0055;
404 addr[0x0555] = 0x0090;
410 case (AMD_MANUFACT & FLASH_ID_MASK):
411 info->flash_id = FLASH_MAN_AMD;
413 case (FUJ_MANUFACT & FLASH_ID_MASK):
414 info->flash_id = FLASH_MAN_FUJ;
416 case (STM_MANUFACT & FLASH_ID_MASK):
417 info->flash_id = FLASH_MAN_STM;
419 case (SST_MANUFACT & FLASH_ID_MASK):
420 info->flash_id = FLASH_MAN_SST;
422 case (INTEL_MANUFACT & FLASH_ID_MASK):
423 info->flash_id = FLASH_MAN_INTEL;
426 info->flash_id = FLASH_UNKNOWN;
427 info->sector_count = 0;
429 return (0); /* no or unknown flash */
433 value = addr[1]; /* device ID */
437 case (AMD_ID_LV400T & FLASH_ID_MASK):
438 info->flash_id += FLASH_AM400T;
439 info->sector_count = 11;
440 info->size = 0x00100000;
443 case (AMD_ID_LV400B & FLASH_ID_MASK):
444 info->flash_id += FLASH_AM400B;
445 info->sector_count = 11;
446 info->size = 0x00100000;
449 case (AMD_ID_LV800T & FLASH_ID_MASK):
450 info->flash_id += FLASH_AM800T;
451 info->sector_count = 19;
452 info->size = 0x00200000;
455 case (AMD_ID_LV800B & FLASH_ID_MASK):
456 info->flash_id += FLASH_AM800B;
457 info->sector_count = 19;
458 info->size = 0x00200000;
461 case (AMD_ID_LV160T & FLASH_ID_MASK):
462 info->flash_id += FLASH_AM160T;
463 info->sector_count = 35;
464 info->size = 0x00400000;
467 case (AMD_ID_LV160B & FLASH_ID_MASK):
468 info->flash_id += FLASH_AM160B;
469 info->sector_count = 35;
470 info->size = 0x00400000;
472 #if 0 /* enable when device IDs are available */
473 case (AMD_ID_LV320T & FLASH_ID_MASK):
474 info->flash_id += FLASH_AM320T;
475 info->sector_count = 67;
476 info->size = 0x00800000;
479 case (AMD_ID_LV320B & FLASH_ID_MASK):
480 info->flash_id += FLASH_AM320B;
481 info->sector_count = 67;
482 info->size = 0x00800000;
486 case (INTEL_ID_28F800B3T & FLASH_ID_MASK):
487 info->flash_id += FLASH_INTEL800T;
488 info->sector_count = 23;
489 info->size = 0x00200000;
492 case (INTEL_ID_28F800B3B & FLASH_ID_MASK):
493 info->flash_id += FLASH_INTEL800B;
494 info->sector_count = 23;
495 info->size = 0x00200000;
498 case (INTEL_ID_28F160B3T & FLASH_ID_MASK):
499 info->flash_id += FLASH_INTEL160T;
500 info->sector_count = 39;
501 info->size = 0x00400000;
504 case (INTEL_ID_28F160B3B & FLASH_ID_MASK):
505 info->flash_id += FLASH_INTEL160B;
506 info->sector_count = 39;
507 info->size = 0x00400000;
510 case (INTEL_ID_28F320B3T & FLASH_ID_MASK):
511 info->flash_id += FLASH_INTEL320T;
512 info->sector_count = 71;
513 info->size = 0x00800000;
516 case (INTEL_ID_28F320B3B & FLASH_ID_MASK):
517 info->flash_id += FLASH_AM320B;
518 info->sector_count = 71;
519 info->size = 0x00800000;
522 #if 0 /* enable when devices are available */
523 case (INTEL_ID_28F320B3T & FLASH_ID_MASK):
524 info->flash_id += FLASH_INTEL320T;
525 info->sector_count = 135;
526 info->size = 0x01000000;
527 break; /* => 16 MB */
529 case (INTEL_ID_28F320B3B & FLASH_ID_MASK):
530 info->flash_id += FLASH_AM320B;
531 info->sector_count = 135;
532 info->size = 0x01000000;
533 break; /* => 16 MB */
536 info->flash_id = FLASH_UNKNOWN;
537 return (0); /* => no or unknown flash */
541 /* set up sector start adress table */
542 if (info->flash_id & FLASH_BTYPE) {
543 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
545 #ifndef CONFIG_FLASH_16BIT
546 /* set sector offsets for bottom boot block type */
547 info->start[0] = base + 0x00000000;
548 info->start[1] = base + 0x00004000;
549 info->start[2] = base + 0x00008000;
550 info->start[3] = base + 0x0000C000;
551 info->start[4] = base + 0x00010000;
552 info->start[5] = base + 0x00014000;
553 info->start[6] = base + 0x00018000;
554 info->start[7] = base + 0x0001C000;
555 for (i = 8; i < info->sector_count; i++) {
557 base + (i * 0x00020000) - 0x000E0000;
560 /* set sector offsets for bottom boot block type */
561 info->start[0] = base + 0x00000000;
562 info->start[1] = base + 0x00008000;
563 info->start[2] = base + 0x0000C000;
564 info->start[3] = base + 0x00010000;
565 for (i = 4; i < info->sector_count; i++) {
567 base + (i * 0x00020000) - 0x00060000;
571 /* set sector offsets for bottom boot block type */
572 info->start[0] = base + 0x00000000;
573 info->start[1] = base + 0x00002000;
574 info->start[2] = base + 0x00004000;
575 info->start[3] = base + 0x00006000;
576 info->start[4] = base + 0x00008000;
577 info->start[5] = base + 0x0000A000;
578 info->start[6] = base + 0x0000C000;
579 info->start[7] = base + 0x0000E000;
580 for (i = 8; i < info->sector_count; i++) {
582 base + (i * 0x00010000) - 0x00070000;
585 /* set sector offsets for bottom boot block type */
586 info->start[0] = base + 0x00000000;
587 info->start[1] = base + 0x00004000;
588 info->start[2] = base + 0x00006000;
589 info->start[3] = base + 0x00008000;
590 for (i = 4; i < info->sector_count; i++) {
592 base + (i * 0x00010000) - 0x00030000;
597 /* set sector offsets for top boot block type */
598 i = info->sector_count - 1;
599 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
601 #ifndef CONFIG_FLASH_16BIT
602 info->start[i--] = base + info->size - 0x00004000;
603 info->start[i--] = base + info->size - 0x00008000;
604 info->start[i--] = base + info->size - 0x0000C000;
605 info->start[i--] = base + info->size - 0x00010000;
606 info->start[i--] = base + info->size - 0x00014000;
607 info->start[i--] = base + info->size - 0x00018000;
608 info->start[i--] = base + info->size - 0x0001C000;
609 for (; i >= 0; i--) {
610 info->start[i] = base + i * 0x00020000;
615 info->start[i--] = base + info->size - 0x00008000;
616 info->start[i--] = base + info->size - 0x0000C000;
617 info->start[i--] = base + info->size - 0x00010000;
618 for (; i >= 0; i--) {
619 info->start[i] = base + i * 0x00020000;
623 info->start[i--] = base + info->size - 0x00002000;
624 info->start[i--] = base + info->size - 0x00004000;
625 info->start[i--] = base + info->size - 0x00006000;
626 info->start[i--] = base + info->size - 0x00008000;
627 info->start[i--] = base + info->size - 0x0000A000;
628 info->start[i--] = base + info->size - 0x0000C000;
629 info->start[i--] = base + info->size - 0x0000E000;
630 for (; i >= 0; i--) {
631 info->start[i] = base + i * 0x00010000;
636 info->start[i--] = base + info->size - 0x00004000;
637 info->start[i--] = base + info->size - 0x00006000;
638 info->start[i--] = base + info->size - 0x00008000;
639 for (; i >= 0; i--) {
640 info->start[i] = base + i * 0x00010000;
646 /* check for protected sectors */
647 for (i = 0; i < info->sector_count; i++) {
648 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
649 /* D0 = 1 if protected */
650 addr = (volatile FLASH_WORD_SIZE *) (info->start[i]);
651 info->protect[i] = addr[2] & 1;
655 * Prevent writes to uninitialized FLASH.
657 if (info->flash_id != FLASH_UNKNOWN) {
658 addr = (volatile FLASH_WORD_SIZE *) info->start[0];
659 if ((info->flash_id & 0xFF00) == FLASH_MAN_INTEL) {
660 *addr = (0x00F000F0 & FLASH_ID_MASK); /* reset bank */
662 *addr = (0x00FF00FF & FLASH_ID_MASK); /* reset bank */
670 /*-----------------------------------------------------------------------
673 int flash_erase (flash_info_t * info, int s_first, int s_last)
676 volatile FLASH_WORD_SIZE *addr =
677 (volatile FLASH_WORD_SIZE *) (info->start[0]);
678 int flag, prot, sect, l_sect, barf;
679 ulong start, now, last;
682 if ((s_first < 0) || (s_first > s_last)) {
683 if (info->flash_id == FLASH_UNKNOWN) {
684 printf ("- missing\n");
686 printf ("- no sectors to erase\n");
691 if ((info->flash_id == FLASH_UNKNOWN) ||
692 ((info->flash_id > FLASH_AMD_COMP) &&
693 ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL))) {
694 printf ("Can't erase unknown flash type - aborted\n");
699 for (sect = s_first; sect <= s_last; ++sect) {
700 if (info->protect[sect]) {
706 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
713 /* Disable interrupts which might cause a timeout here */
714 flag = disable_interrupts ();
715 if (info->flash_id < FLASH_AMD_COMP) {
716 #ifndef CONFIG_FLASH_16BIT
717 addr[0x0555] = 0x00AA00AA;
718 addr[0x02AA] = 0x00550055;
719 addr[0x0555] = 0x00800080;
720 addr[0x0555] = 0x00AA00AA;
721 addr[0x02AA] = 0x00550055;
723 addr[0x0555] = 0x00AA;
724 addr[0x02AA] = 0x0055;
725 addr[0x0555] = 0x0080;
726 addr[0x0555] = 0x00AA;
727 addr[0x02AA] = 0x0055;
729 /* Start erase on unprotected sectors */
730 for (sect = s_first; sect <= s_last; sect++) {
731 if (info->protect[sect] == 0) { /* not protected */
732 addr = (volatile FLASH_WORD_SIZE *) (info->start[sect]);
733 addr[0] = (0x00300030 & FLASH_ID_MASK);
738 /* re-enable interrupts if necessary */
740 enable_interrupts ();
742 /* wait at least 80us - let's wait 1 ms */
746 * We wait for the last triggered sector
751 start = get_timer (0);
753 addr = (volatile FLASH_WORD_SIZE *) (info->start[l_sect]);
754 while ((addr[0] & (0x00800080 & FLASH_ID_MASK)) !=
755 (0x00800080 & FLASH_ID_MASK)) {
756 if ((now = get_timer (start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
757 printf ("Timeout\n");
760 /* show that we're waiting */
761 if ((now - last) > 1000) { /* every second */
768 /* reset to read mode */
769 addr = (volatile FLASH_WORD_SIZE *) info->start[0];
770 addr[0] = (0x00F000F0 & FLASH_ID_MASK); /* reset bank */
774 for (sect = s_first; sect <= s_last; sect++) {
775 if (info->protect[sect] == 0) { /* not protected */
777 #ifndef CONFIG_FLASH_16BIT
778 addr = (vu_long *) (info->start[sect]);
779 addr[0] = 0x00200020;
780 addr[0] = 0x00D000D0;
781 while (!(addr[0] & 0x00800080)); /* wait for error or finish */
782 if (addr[0] & 0x003A003A) { /* check for error */
783 barf = addr[0] & 0x003A0000;
787 barf = addr[0] & 0x0000003A;
791 addr = (vu_short *) (info->start[sect]);
794 while (!(addr[0] & 0x0080)); /* wait for error or finish */
795 if (addr[0] & 0x003A) /* check for error */
796 barf = addr[0] & 0x003A;
799 printf ("\nFlash error in sector at %lx\n", (unsigned long) addr);
801 printf ("Block locked, not erased.\n");
802 if ((barf & 0x0030) == 0x0030)
803 printf ("Command Sequence error.\n");
804 if ((barf & 0x0030) == 0x0020)
805 printf ("Block Erase error.\n");
807 printf ("Vpp Low error.\n");
813 addr = (volatile FLASH_WORD_SIZE *) info->start[0];
814 addr[0] = (0x00FF00FF & FLASH_ID_MASK); /* reset bank */
823 /*-----------------------------------------------------------------------
826 /*-----------------------------------------------------------------------
827 * Copy memory to flash, returns:
830 * 2 - Flash not erased
833 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
835 #ifndef CONFIG_FLASH_16BIT
844 #ifndef CONFIG_FLASH_16BIT
847 wp = (addr & ~3); /* get lower word aligned address */
850 * handle unaligned start bytes
852 if ((l = addr - wp) != 0) {
854 for (i = 0, cp = wp; i < l; ++i, ++cp) {
855 data = (data << 8) | (*(uchar *) cp);
857 for (; i < 4 && cnt > 0; ++i) {
858 data = (data << 8) | *src++;
862 for (; cnt == 0 && i < 4; ++i, ++cp) {
863 data = (data << 8) | (*(uchar *) cp);
866 if ((rc = write_word (info, wp, data)) != 0) {
873 * handle word aligned part
877 for (i = 0; i < 4; ++i) {
878 data = (data << 8) | *src++;
880 if ((rc = write_word (info, wp, data)) != 0) {
892 * handle unaligned tail bytes
895 for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
896 data = (data << 8) | *src++;
899 for (; i < 4; ++i, ++cp) {
900 data = (data << 8) | (*(uchar *) cp);
903 return (write_word (info, wp, data));
906 wp = (addr & ~1); /* get lower word aligned address */
909 * handle unaligned start byte
913 data = (data << 8) | *src++;
915 if ((rc = write_short (info, wp, data)) != 0) {
922 * handle word aligned part
924 /* l = 0; used for debuging */
927 for (i = 0; i < 2; ++i) {
928 data = (data << 8) | *src++;
934 } used for debuging */
936 if ((rc = write_short (info, wp, data)) != 0) {
948 * handle unaligned tail bytes
951 for (i = 0, cp = wp; i < 2 && cnt > 0; ++i, ++cp) {
952 data = (data << 8) | *src++;
955 for (; i < 2; ++i, ++cp) {
956 data = (data << 8) | (*(uchar *) cp);
959 return (write_short (info, wp, data));
965 /*-----------------------------------------------------------------------
966 * Write a word to Flash, returns:
969 * 2 - Flash not erased
971 #ifndef CONFIG_FLASH_16BIT
972 static int write_word (flash_info_t * info, ulong dest, ulong data)
974 vu_long *addr = (vu_long *) (info->start[0]);
979 /* Check if Flash is (sufficiently) erased */
980 if ((*((vu_long *) dest) & data) != data) {
984 /* Disable interrupts which might cause a timeout here */
985 flag = disable_interrupts ();
987 if (info->flash_id > FLASH_AMD_COMP) {
989 addr[0x0555] = 0x00AA00AA;
990 addr[0x02AA] = 0x00550055;
991 addr[0x0555] = 0x00A000A0;
996 *((vu_long *) dest) = data;
998 /* re-enable interrupts if necessary */
1000 enable_interrupts ();
1002 /* data polling for D7 */
1003 start = get_timer (0);
1005 if (info->flash_id > FLASH_AMD_COMP) {
1007 while ((*((vu_long *) dest) & 0x00800080) !=
1008 (data & 0x00800080)) {
1009 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
1016 while (!(addr[0] & 0x00800080)) { /* wait for error or finish */
1017 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
1021 if (addr[0] & 0x003A003A) { /* check for error */
1022 barf = addr[0] & 0x003A0000;
1026 barf = addr[0] & 0x0000003A;
1028 printf ("\nFlash write error at address %lx\n", (unsigned long) dest);
1030 printf ("Block locked, not erased.\n");
1032 printf ("Programming error.\n");
1034 printf ("Vpp Low error.\n");
1047 static int write_short (flash_info_t * info, ulong dest, ushort data)
1049 vu_short *addr = (vu_short *) (info->start[0]);
1053 /* Check if Flash is (sufficiently) erased */
1054 if ((*((vu_short *) dest) & data) != data) {
1058 /* Disable interrupts which might cause a timeout here */
1059 flag = disable_interrupts ();
1061 if (info->flash_id < FLASH_AMD_COMP) {
1063 addr[0x0555] = 0x00AA;
1064 addr[0x02AA] = 0x0055;
1065 addr[0x0555] = 0x00A0;
1071 *((vu_short *) dest) = data;
1073 /* re-enable interrupts if necessary */
1075 enable_interrupts ();
1077 /* data polling for D7 */
1078 start = get_timer (0);
1080 if (info->flash_id < FLASH_AMD_COMP) {
1082 while ((*((vu_short *) dest) & 0x0080) != (data & 0x0080)) {
1083 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
1090 while (!(addr[0] & 0x0080)) { /* wait for error or finish */
1091 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT)
1095 if (addr[0] & 0x003A) { /* check for error */
1096 barf = addr[0] & 0x003A;
1097 printf ("\nFlash write error at address %lx\n",
1098 (unsigned long) dest);
1100 printf ("Block locked, not erased.\n");
1102 printf ("Programming error.\n");
1104 printf ("Vpp Low error.\n");
1109 while (!(addr[0] & 0x0080)) { /* wait for error or finish */
1110 if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT)
1119 /*-----------------------------------------------------------------------*/