3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
29 #ifdef CONFIG_FLASH_16BIT
30 #define FLASH_WORD_SIZE unsigned short
31 #define FLASH_ID_MASK 0xFFFF
33 #define FLASH_WORD_SIZE unsigned long
34 #define FLASH_ID_MASK 0xFFFFFFFF
37 /*-----------------------------------------------------------------------
41 ulong flash_get_size (volatile FLASH_WORD_SIZE *addr, flash_info_t *info);
42 #ifndef CONFIG_FLASH_16BIT
43 static int write_word (flash_info_t *info, ulong dest, ulong data);
45 static int write_short (flash_info_t *info, ulong dest, ushort data);
47 /*int flash_write (uchar *, ulong, ulong); */
48 /*flash_info_t *addr2info (ulong); */
50 static void flash_get_offsets (ulong base, flash_info_t *info);
52 /*-----------------------------------------------------------------------
54 unsigned long flash_init (void)
56 volatile immap_t *immap = (immap_t *)CFG_IMMR;
57 volatile memctl8xx_t *memctl = &immap->im_memctl;
58 unsigned long size_b0, size_b1;
61 /* Init: no FLASHes known */
62 for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
63 flash_info[i].flash_id = FLASH_UNKNOWN;
66 /* Static FLASH Bank configuration here - FIXME XXX */
68 size_b0 = flash_get_size((volatile FLASH_WORD_SIZE *)FLASH_BASE0_PRELIM,
70 if (flash_info[0].flash_id == FLASH_UNKNOWN) {
71 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
72 size_b0, size_b0<<20);
75 size_b1 = flash_get_size((volatile FLASH_WORD_SIZE *)FLASH_BASE1_PRELIM,
78 if (size_b1 > size_b0) {
80 "Bank 1 (0x%08lx = %ld MB) > Bank 0 (0x%08lx = %ld MB)\n",
84 flash_info[0].flash_id = FLASH_UNKNOWN;
85 flash_info[1].flash_id = FLASH_UNKNOWN;
86 flash_info[0].sector_count = -1;
87 flash_info[1].sector_count = -1;
88 flash_info[0].size = 0;
89 flash_info[1].size = 0;
93 /* Remap FLASH according to real size */
94 memctl->memc_or0 = CFG_OR_TIMING_FLASH | (-size_b0 & 0xFFFF8000);
95 memctl->memc_br0 = CFG_FLASH_BASE | 0x00000801; /* (CFG_FLASH_BASE & BR_BA_MSK) | BR_MS_GPCM | BR_V;*/
97 /* Re-do sizing to get full correct info */
99 size_b0 = flash_get_size((volatile FLASH_WORD_SIZE *)CFG_FLASH_BASE,
101 flash_get_offsets (CFG_FLASH_BASE, &flash_info[0]);
103 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
104 /* monitor protection ON by default */
105 (void)flash_protect(FLAG_PROTECT_SET,
107 CFG_MONITOR_BASE+monitor_flash_len-1,
112 memctl->memc_or1 = CFG_OR_TIMING_FLASH | (-size_b1 & 0xFFFF8000);
113 memctl->memc_br1 = (CFG_FLASH_BASE | 0x00000801) + (size_b0 & BR_BA_MSK);
114 /*((CFG_FLASH_BASE + size_b0) & BR_BA_MSK) |
117 /* Re-do sizing to get full correct info */
118 size_b1 = flash_get_size((volatile FLASH_WORD_SIZE *)(CFG_FLASH_BASE + size_b0),
121 flash_get_offsets (CFG_FLASH_BASE + size_b0, &flash_info[1]);
123 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
124 /* monitor protection ON by default */
125 (void)flash_protect(FLAG_PROTECT_SET,
127 CFG_MONITOR_BASE+monitor_flash_len-1,
131 memctl->memc_br1 = 0; /* invalidate bank */
133 flash_info[1].flash_id = FLASH_UNKNOWN;
134 flash_info[1].sector_count = -1;
137 flash_info[0].size = size_b0;
138 flash_info[1].size = size_b1;
140 return (size_b0 + size_b1);
143 /*-----------------------------------------------------------------------
145 static void flash_get_offsets (ulong base, flash_info_t *info)
149 /* set up sector start adress table */
150 if (info->flash_id & FLASH_BTYPE) {
151 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
153 #ifndef CONFIG_FLASH_16BIT
154 /* set sector offsets for bottom boot block type */
155 info->start[0] = base + 0x00000000;
156 info->start[1] = base + 0x00004000;
157 info->start[2] = base + 0x00008000;
158 info->start[3] = base + 0x0000C000;
159 info->start[4] = base + 0x00010000;
160 info->start[5] = base + 0x00014000;
161 info->start[6] = base + 0x00018000;
162 info->start[7] = base + 0x0001C000;
163 for (i = 8; i < info->sector_count; i++) {
164 info->start[i] = base + (i * 0x00020000) - 0x000E0000;
168 /* set sector offsets for bottom boot block type */
169 info->start[0] = base + 0x00000000;
170 info->start[1] = base + 0x00008000;
171 info->start[2] = base + 0x0000C000;
172 info->start[3] = base + 0x00010000;
173 for (i = 4; i < info->sector_count; i++) {
174 info->start[i] = base + (i * 0x00020000) - 0x00060000;
178 /* set sector offsets for bottom boot block type */
179 info->start[0] = base + 0x00000000;
180 info->start[1] = base + 0x00002000;
181 info->start[2] = base + 0x00004000;
182 info->start[3] = base + 0x00006000;
183 info->start[4] = base + 0x00008000;
184 info->start[5] = base + 0x0000A000;
185 info->start[6] = base + 0x0000C000;
186 info->start[7] = base + 0x0000E000;
187 for (i = 8; i < info->sector_count; i++) {
188 info->start[i] = base + (i * 0x00010000) - 0x00070000;
192 /* set sector offsets for bottom boot block type */
193 info->start[0] = base + 0x00000000;
194 info->start[1] = base + 0x00004000;
195 info->start[2] = base + 0x00006000;
196 info->start[3] = base + 0x00008000;
197 for (i = 4; i < info->sector_count; i++) {
198 info->start[i] = base + (i * 0x00010000) - 0x00030000;
203 /* set sector offsets for top boot block type */
204 i = info->sector_count - 1;
205 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
207 #ifndef CONFIG_FLASH_16BIT
208 info->start[i--] = base + info->size - 0x00004000;
209 info->start[i--] = base + info->size - 0x00008000;
210 info->start[i--] = base + info->size - 0x0000C000;
211 info->start[i--] = base + info->size - 0x00010000;
212 info->start[i--] = base + info->size - 0x00014000;
213 info->start[i--] = base + info->size - 0x00018000;
214 info->start[i--] = base + info->size - 0x0001C000;
215 for (; i >= 0; i--) {
216 info->start[i] = base + i * 0x00020000;
221 info->start[i--] = base + info->size - 0x00008000;
222 info->start[i--] = base + info->size - 0x0000C000;
223 info->start[i--] = base + info->size - 0x00010000;
224 for (; i >= 0; i--) {
225 info->start[i] = base + i * 0x00020000;
229 info->start[i--] = base + info->size - 0x00002000;
230 info->start[i--] = base + info->size - 0x00004000;
231 info->start[i--] = base + info->size - 0x00006000;
232 info->start[i--] = base + info->size - 0x00008000;
233 info->start[i--] = base + info->size - 0x0000A000;
234 info->start[i--] = base + info->size - 0x0000C000;
235 info->start[i--] = base + info->size - 0x0000E000;
236 for (; i >= 0; i--) {
237 info->start[i] = base + i * 0x00010000;
242 info->start[i--] = base + info->size - 0x00004000;
243 info->start[i--] = base + info->size - 0x00006000;
244 info->start[i--] = base + info->size - 0x00008000;
245 for (; i >= 0; i--) {
246 info->start[i] = base + i * 0x00010000;
255 /*-----------------------------------------------------------------------
257 void flash_print_info (flash_info_t *info)
261 uchar botboot[]=", bottom boot sect)\n";
262 uchar topboot[]=", top boot sector)\n";
264 if (info->flash_id == FLASH_UNKNOWN) {
265 printf ("missing or unknown FLASH type\n");
269 switch (info->flash_id & FLASH_VENDMASK) {
270 case FLASH_MAN_AMD: printf ("AMD "); break;
271 case FLASH_MAN_FUJ: printf ("FUJITSU "); break;
272 case FLASH_MAN_SST: printf ("SST "); break;
273 case FLASH_MAN_STM: printf ("STM "); break;
274 case FLASH_MAN_INTEL: printf ("INTEL "); break;
275 default: printf ("Unknown Vendor "); break;
278 if (info->flash_id & 0x0001 ) {
284 switch (info->flash_id & FLASH_TYPEMASK) {
285 case FLASH_AM400B: printf ("AM29LV400B (4 Mbit%s",boottype);
287 case FLASH_AM400T: printf ("AM29LV400T (4 Mbit%s",boottype);
289 case FLASH_AM800B: printf ("AM29LV800B (8 Mbit%s",boottype);
291 case FLASH_AM800T: printf ("AM29LV800T (8 Mbit%s",boottype);
293 case FLASH_AM160B: printf ("AM29LV160B (16 Mbit%s",boottype);
295 case FLASH_AM160T: printf ("AM29LV160T (16 Mbit%s",boottype);
297 case FLASH_AM320B: printf ("AM29LV320B (32 Mbit%s",boottype);
299 case FLASH_AM320T: printf ("AM29LV320T (32 Mbit%s",boottype);
301 case FLASH_INTEL800B: printf ("INTEL28F800B (8 Mbit%s",boottype);
303 case FLASH_INTEL800T: printf ("INTEL28F800T (8 Mbit%s",boottype);
305 case FLASH_INTEL160B: printf ("INTEL28F160B (16 Mbit%s",boottype);
307 case FLASH_INTEL160T: printf ("INTEL28F160T (16 Mbit%s",boottype);
309 case FLASH_INTEL320B: printf ("INTEL28F320B (32 Mbit%s",boottype);
311 case FLASH_INTEL320T: printf ("INTEL28F320T (32 Mbit%s",boottype);
314 #if 0 /* enable when devices are available */
316 case FLASH_INTEL640B: printf ("INTEL28F640B (64 Mbit%s",boottype);
318 case FLASH_INTEL640T: printf ("INTEL28F640T (64 Mbit%s",boottype);
322 default: printf ("Unknown Chip Type\n");
326 printf (" Size: %ld MB in %d Sectors\n",
327 info->size >> 20, info->sector_count);
329 printf (" Sector Start Addresses:");
330 for (i=0; i<info->sector_count; ++i) {
335 info->protect[i] ? " (RO)" : " "
342 /*-----------------------------------------------------------------------
346 /*-----------------------------------------------------------------------
350 * The following code cannot be run from FLASH!
352 ulong flash_get_size (volatile FLASH_WORD_SIZE *addr, flash_info_t *info)
355 ulong base = (ulong)addr;
356 FLASH_WORD_SIZE value;
358 /* Write auto select command: read Manufacturer ID */
361 #ifndef CONFIG_FLASH_16BIT
364 * Note: if it is an AMD flash and the word at addr[0000]
365 * is 0x00890089 this routine will think it is an Intel
366 * flash device and may(most likely) cause trouble.
369 addr[0x0000] = 0x00900090;
370 if(addr[0x0000] != 0x00890089){
371 addr[0x0555] = 0x00AA00AA;
372 addr[0x02AA] = 0x00550055;
373 addr[0x0555] = 0x00900090;
377 * Note: if it is an AMD flash and the word at addr[0000]
378 * is 0x0089 this routine will think it is an Intel
379 * flash device and may(most likely) cause trouble.
382 addr[0x0000] = 0x0090;
384 if(addr[0x0000] != 0x0089){
385 addr[0x0555] = 0x00AA;
386 addr[0x02AA] = 0x0055;
387 addr[0x0555] = 0x0090;
393 case (AMD_MANUFACT & FLASH_ID_MASK):
394 info->flash_id = FLASH_MAN_AMD;
396 case (FUJ_MANUFACT & FLASH_ID_MASK):
397 info->flash_id = FLASH_MAN_FUJ;
399 case (STM_MANUFACT & FLASH_ID_MASK):
400 info->flash_id = FLASH_MAN_STM;
402 case (SST_MANUFACT & FLASH_ID_MASK):
403 info->flash_id = FLASH_MAN_SST;
405 case (INTEL_MANUFACT & FLASH_ID_MASK):
406 info->flash_id = FLASH_MAN_INTEL;
409 info->flash_id = FLASH_UNKNOWN;
410 info->sector_count = 0;
412 return (0); /* no or unknown flash */
416 value = addr[1]; /* device ID */
420 case (AMD_ID_LV400T & FLASH_ID_MASK):
421 info->flash_id += FLASH_AM400T;
422 info->sector_count = 11;
423 info->size = 0x00100000;
426 case (AMD_ID_LV400B & FLASH_ID_MASK):
427 info->flash_id += FLASH_AM400B;
428 info->sector_count = 11;
429 info->size = 0x00100000;
432 case (AMD_ID_LV800T & FLASH_ID_MASK):
433 info->flash_id += FLASH_AM800T;
434 info->sector_count = 19;
435 info->size = 0x00200000;
438 case (AMD_ID_LV800B & FLASH_ID_MASK):
439 info->flash_id += FLASH_AM800B;
440 info->sector_count = 19;
441 info->size = 0x00200000;
444 case (AMD_ID_LV160T & FLASH_ID_MASK):
445 info->flash_id += FLASH_AM160T;
446 info->sector_count = 35;
447 info->size = 0x00400000;
450 case (AMD_ID_LV160B & FLASH_ID_MASK):
451 info->flash_id += FLASH_AM160B;
452 info->sector_count = 35;
453 info->size = 0x00400000;
455 #if 0 /* enable when device IDs are available */
456 case (AMD_ID_LV320T & FLASH_ID_MASK):
457 info->flash_id += FLASH_AM320T;
458 info->sector_count = 67;
459 info->size = 0x00800000;
462 case (AMD_ID_LV320B & FLASH_ID_MASK):
463 info->flash_id += FLASH_AM320B;
464 info->sector_count = 67;
465 info->size = 0x00800000;
469 case (INTEL_ID_28F800B3T & FLASH_ID_MASK):
470 info->flash_id += FLASH_INTEL800T;
471 info->sector_count = 23;
472 info->size = 0x00200000;
475 case (INTEL_ID_28F800B3B & FLASH_ID_MASK):
476 info->flash_id += FLASH_INTEL800B;
477 info->sector_count = 23;
478 info->size = 0x00200000;
481 case (INTEL_ID_28F160B3T & FLASH_ID_MASK):
482 info->flash_id += FLASH_INTEL160T;
483 info->sector_count = 39;
484 info->size = 0x00400000;
487 case (INTEL_ID_28F160B3B & FLASH_ID_MASK):
488 info->flash_id += FLASH_INTEL160B;
489 info->sector_count = 39;
490 info->size = 0x00400000;
493 case (INTEL_ID_28F320B3T & FLASH_ID_MASK):
494 info->flash_id += FLASH_INTEL320T;
495 info->sector_count = 71;
496 info->size = 0x00800000;
499 case (INTEL_ID_28F320B3B & FLASH_ID_MASK):
500 info->flash_id += FLASH_AM320B;
501 info->sector_count = 71;
502 info->size = 0x00800000;
505 #if 0 /* enable when devices are available */
506 case (INTEL_ID_28F320B3T & FLASH_ID_MASK):
507 info->flash_id += FLASH_INTEL320T;
508 info->sector_count = 135;
509 info->size = 0x01000000;
510 break; /* => 16 MB */
512 case (INTEL_ID_28F320B3B & FLASH_ID_MASK):
513 info->flash_id += FLASH_AM320B;
514 info->sector_count = 135;
515 info->size = 0x01000000;
516 break; /* => 16 MB */
519 info->flash_id = FLASH_UNKNOWN;
520 return (0); /* => no or unknown flash */
524 /* set up sector start adress table */
525 if (info->flash_id & FLASH_BTYPE) {
526 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
528 #ifndef CONFIG_FLASH_16BIT
529 /* set sector offsets for bottom boot block type */
530 info->start[0] = base + 0x00000000;
531 info->start[1] = base + 0x00004000;
532 info->start[2] = base + 0x00008000;
533 info->start[3] = base + 0x0000C000;
534 info->start[4] = base + 0x00010000;
535 info->start[5] = base + 0x00014000;
536 info->start[6] = base + 0x00018000;
537 info->start[7] = base + 0x0001C000;
538 for (i = 8; i < info->sector_count; i++) {
539 info->start[i] = base + (i * 0x00020000) - 0x000E0000;
543 /* set sector offsets for bottom boot block type */
544 info->start[0] = base + 0x00000000;
545 info->start[1] = base + 0x00008000;
546 info->start[2] = base + 0x0000C000;
547 info->start[3] = base + 0x00010000;
548 for (i = 4; i < info->sector_count; i++) {
549 info->start[i] = base + (i * 0x00020000) - 0x00060000;
553 /* set sector offsets for bottom boot block type */
554 info->start[0] = base + 0x00000000;
555 info->start[1] = base + 0x00002000;
556 info->start[2] = base + 0x00004000;
557 info->start[3] = base + 0x00006000;
558 info->start[4] = base + 0x00008000;
559 info->start[5] = base + 0x0000A000;
560 info->start[6] = base + 0x0000C000;
561 info->start[7] = base + 0x0000E000;
562 for (i = 8; i < info->sector_count; i++) {
563 info->start[i] = base + (i * 0x00010000) - 0x00070000;
567 /* set sector offsets for bottom boot block type */
568 info->start[0] = base + 0x00000000;
569 info->start[1] = base + 0x00004000;
570 info->start[2] = base + 0x00006000;
571 info->start[3] = base + 0x00008000;
572 for (i = 4; i < info->sector_count; i++) {
573 info->start[i] = base + (i * 0x00010000) - 0x00030000;
578 /* set sector offsets for top boot block type */
579 i = info->sector_count - 1;
580 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
582 #ifndef CONFIG_FLASH_16BIT
583 info->start[i--] = base + info->size - 0x00004000;
584 info->start[i--] = base + info->size - 0x00008000;
585 info->start[i--] = base + info->size - 0x0000C000;
586 info->start[i--] = base + info->size - 0x00010000;
587 info->start[i--] = base + info->size - 0x00014000;
588 info->start[i--] = base + info->size - 0x00018000;
589 info->start[i--] = base + info->size - 0x0001C000;
590 for (; i >= 0; i--) {
591 info->start[i] = base + i * 0x00020000;
596 info->start[i--] = base + info->size - 0x00008000;
597 info->start[i--] = base + info->size - 0x0000C000;
598 info->start[i--] = base + info->size - 0x00010000;
599 for (; i >= 0; i--) {
600 info->start[i] = base + i * 0x00020000;
604 info->start[i--] = base + info->size - 0x00002000;
605 info->start[i--] = base + info->size - 0x00004000;
606 info->start[i--] = base + info->size - 0x00006000;
607 info->start[i--] = base + info->size - 0x00008000;
608 info->start[i--] = base + info->size - 0x0000A000;
609 info->start[i--] = base + info->size - 0x0000C000;
610 info->start[i--] = base + info->size - 0x0000E000;
611 for (; i >= 0; i--) {
612 info->start[i] = base + i * 0x00010000;
617 info->start[i--] = base + info->size - 0x00004000;
618 info->start[i--] = base + info->size - 0x00006000;
619 info->start[i--] = base + info->size - 0x00008000;
620 for (; i >= 0; i--) {
621 info->start[i] = base + i * 0x00010000;
627 /* check for protected sectors */
628 for (i = 0; i < info->sector_count; i++) {
629 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
630 /* D0 = 1 if protected */
631 addr = (volatile FLASH_WORD_SIZE *)(info->start[i]);
632 info->protect[i] = addr[2] & 1;
636 * Prevent writes to uninitialized FLASH.
638 if (info->flash_id != FLASH_UNKNOWN) {
639 addr = (volatile FLASH_WORD_SIZE *)info->start[0];
640 if( (info->flash_id & 0xFF00) == FLASH_MAN_INTEL){
641 *addr = (0x00F000F0 & FLASH_ID_MASK); /* reset bank */
643 *addr = (0x00FF00FF & FLASH_ID_MASK); /* reset bank */
651 /*-----------------------------------------------------------------------
654 int flash_erase (flash_info_t *info, int s_first, int s_last)
657 volatile FLASH_WORD_SIZE *addr=(volatile FLASH_WORD_SIZE*)(info->start[0]);
658 int flag, prot, sect, l_sect, barf;
659 ulong start, now, last;
662 if ((s_first < 0) || (s_first > s_last)) {
663 if (info->flash_id == FLASH_UNKNOWN) {
664 printf ("- missing\n");
666 printf ("- no sectors to erase\n");
671 if ((info->flash_id == FLASH_UNKNOWN) ||
672 ((info->flash_id > FLASH_AMD_COMP) &&
673 ( (info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL ) ) ){
674 printf ("Can't erase unknown flash type - aborted\n");
679 for (sect=s_first; sect<=s_last; ++sect) {
680 if (info->protect[sect]) {
686 printf ("- Warning: %d protected sectors will not be erased!\n",
694 /* Disable interrupts which might cause a timeout here */
695 flag = disable_interrupts();
696 if(info->flash_id < FLASH_AMD_COMP) {
697 #ifndef CONFIG_FLASH_16BIT
698 addr[0x0555] = 0x00AA00AA;
699 addr[0x02AA] = 0x00550055;
700 addr[0x0555] = 0x00800080;
701 addr[0x0555] = 0x00AA00AA;
702 addr[0x02AA] = 0x00550055;
704 addr[0x0555] = 0x00AA;
705 addr[0x02AA] = 0x0055;
706 addr[0x0555] = 0x0080;
707 addr[0x0555] = 0x00AA;
708 addr[0x02AA] = 0x0055;
710 /* Start erase on unprotected sectors */
711 for (sect = s_first; sect<=s_last; sect++) {
712 if (info->protect[sect] == 0) { /* not protected */
713 addr = (volatile FLASH_WORD_SIZE *)(info->start[sect]);
714 addr[0] = (0x00300030 & FLASH_ID_MASK);
719 /* re-enable interrupts if necessary */
723 /* wait at least 80us - let's wait 1 ms */
727 * We wait for the last triggered sector
732 start = get_timer (0);
734 addr = (volatile FLASH_WORD_SIZE*)(info->start[l_sect]);
735 while ((addr[0] & (0x00800080&FLASH_ID_MASK)) !=
736 (0x00800080&FLASH_ID_MASK) )
738 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
739 printf ("Timeout\n");
742 /* show that we're waiting */
743 if ((now - last) > 1000) { /* every second */
750 /* reset to read mode */
751 addr = (volatile FLASH_WORD_SIZE *)info->start[0];
752 addr[0] = (0x00F000F0 & FLASH_ID_MASK); /* reset bank */
756 for (sect = s_first; sect<=s_last; sect++) {
757 if (info->protect[sect] == 0) { /* not protected */
759 #ifndef CONFIG_FLASH_16BIT
760 addr = (vu_long*)(info->start[sect]);
761 addr[0] = 0x00200020;
762 addr[0] = 0x00D000D0;
763 while(!(addr[0] & 0x00800080)); /* wait for error or finish */
764 if( addr[0] & 0x003A003A) { /* check for error */
765 barf = addr[0] & 0x003A0000;
769 barf = addr[0] & 0x0000003A;
773 addr = (vu_short*)(info->start[sect]);
776 while(!(addr[0] & 0x0080)); /* wait for error or finish */
777 if( addr[0] & 0x003A) /* check for error */
778 barf = addr[0] & 0x003A;
781 printf("\nFlash error in sector at %lx\n",(unsigned long)addr);
782 if(barf & 0x0002) printf("Block locked, not erased.\n");
783 if((barf & 0x0030) == 0x0030)
784 printf("Command Sequence error.\n");
785 if((barf & 0x0030) == 0x0020)
786 printf("Block Erase error.\n");
787 if(barf & 0x0008) printf("Vpp Low error.\n");
792 addr = (volatile FLASH_WORD_SIZE *)info->start[0];
793 addr[0] = (0x00FF00FF & FLASH_ID_MASK); /* reset bank */
802 /*-----------------------------------------------------------------------
805 /*-----------------------------------------------------------------------
806 * Copy memory to flash, returns:
809 * 2 - Flash not erased
812 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
814 #ifndef CONFIG_FLASH_16BIT
823 #ifndef CONFIG_FLASH_16BIT
826 wp = (addr & ~3); /* get lower word aligned address */
829 * handle unaligned start bytes
831 if ((l = addr - wp) != 0) {
833 for (i=0, cp=wp; i<l; ++i, ++cp) {
834 data = (data << 8) | (*(uchar *)cp);
836 for (; i<4 && cnt>0; ++i) {
837 data = (data << 8) | *src++;
841 for (; cnt==0 && i<4; ++i, ++cp) {
842 data = (data << 8) | (*(uchar *)cp);
845 if ((rc = write_word(info, wp, data)) != 0) {
852 * handle word aligned part
856 for (i=0; i<4; ++i) {
857 data = (data << 8) | *src++;
859 if ((rc = write_word(info, wp, data)) != 0) {
871 * handle unaligned tail bytes
874 for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
875 data = (data << 8) | *src++;
878 for (; i<4; ++i, ++cp) {
879 data = (data << 8) | (*(uchar *)cp);
882 return (write_word(info, wp, data));
885 wp = (addr & ~1); /* get lower word aligned address */
888 * handle unaligned start byte
892 data = (data << 8) | *src++;
894 if ((rc = write_short(info, wp, data)) != 0) {
901 * handle word aligned part
903 /* l = 0; used for debuging */
906 for (i=0; i<2; ++i) {
907 data = (data << 8) | *src++;
913 } used for debuging */
915 if ((rc = write_short(info, wp, data)) != 0) {
927 * handle unaligned tail bytes
930 for (i=0, cp=wp; i<2 && cnt>0; ++i, ++cp) {
931 data = (data << 8) | *src++;
934 for (; i<2; ++i, ++cp) {
935 data = (data << 8) | (*(uchar *)cp);
938 return (write_short(info, wp, data));
944 /*-----------------------------------------------------------------------
945 * Write a word to Flash, returns:
948 * 2 - Flash not erased
950 #ifndef CONFIG_FLASH_16BIT
951 static int write_word (flash_info_t *info, ulong dest, ulong data)
953 vu_long *addr = (vu_long*)(info->start[0]);
958 /* Check if Flash is (sufficiently) erased */
959 if ((*((vu_long *)dest) & data) != data) {
963 /* Disable interrupts which might cause a timeout here */
964 flag = disable_interrupts();
966 if(info->flash_id > FLASH_AMD_COMP) {
968 addr[0x0555] = 0x00AA00AA;
969 addr[0x02AA] = 0x00550055;
970 addr[0x0555] = 0x00A000A0;
975 *((vu_long *)dest) = data;
977 /* re-enable interrupts if necessary */
981 /* data polling for D7 */
982 start = get_timer (0);
984 if(info->flash_id > FLASH_AMD_COMP) {
986 while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
987 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
994 while(!(addr[0] & 0x00800080)){ /* wait for error or finish */
995 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
999 if( addr[0] & 0x003A003A) { /* check for error */
1000 barf = addr[0] & 0x003A0000;
1004 barf = addr[0] & 0x0000003A;
1006 printf("\nFlash write error at address %lx\n",(unsigned long)dest);
1007 if(barf & 0x0002) printf("Block locked, not erased.\n");
1008 if(barf & 0x0010) printf("Programming error.\n");
1009 if(barf & 0x0008) printf("Vpp Low error.\n");
1022 static int write_short (flash_info_t *info, ulong dest, ushort data)
1024 vu_short *addr = (vu_short*)(info->start[0]);
1028 /* Check if Flash is (sufficiently) erased */
1029 if ((*((vu_short *)dest) & data) != data) {
1033 /* Disable interrupts which might cause a timeout here */
1034 flag = disable_interrupts();
1036 if(info->flash_id < FLASH_AMD_COMP) {
1038 addr[0x0555] = 0x00AA;
1039 addr[0x02AA] = 0x0055;
1040 addr[0x0555] = 0x00A0;
1046 *((vu_short *)dest) = data;
1048 /* re-enable interrupts if necessary */
1050 enable_interrupts();
1052 /* data polling for D7 */
1053 start = get_timer (0);
1055 if(info->flash_id < FLASH_AMD_COMP) {
1057 while ((*((vu_short *)dest) & 0x0080) != (data & 0x0080)) {
1058 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
1065 while(!(addr[0] & 0x0080)){ /* wait for error or finish */
1066 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) return (1);
1069 if( addr[0] & 0x003A) { /* check for error */
1070 barf = addr[0] & 0x003A;
1071 printf("\nFlash write error at address %lx\n",(unsigned long)dest);
1072 if(barf & 0x0002) printf("Block locked, not erased.\n");
1073 if(barf & 0x0010) printf("Programming error.\n");
1074 if(barf & 0x0008) printf("Vpp Low error.\n");
1079 while(!(addr[0] & 0x0080)){ /* wait for error or finish */
1080 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) return (1);
1094 /*-----------------------------------------------------------------------