]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/walnut405/flash.c
Initial revision
[karo-tx-uboot.git] / board / walnut405 / flash.c
1 /*
2  * (C) Copyright 2000
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
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.
12  *
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.
17  *
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,
21  * MA 02111-1307 USA
22  */
23
24 /*
25  * Modified 4/5/2001
26  * Wait for completion of each sector erase command issued
27  * 4/5/2001
28  * Chris Hallinan - DS4.COM, Inc. - clh@net1plus.com
29  */
30
31 #include <common.h>
32 #include <ppc4xx.h>
33 #include <asm/processor.h>
34
35 flash_info_t    flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips        */
36
37 /*-----------------------------------------------------------------------
38  * Functions
39  */
40 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
41 static int write_word (flash_info_t *info, ulong dest, ulong data);
42 static void flash_get_offsets (ulong base, flash_info_t *info);
43
44 #ifdef CONFIG_ADCIOP
45 #define ADDR0           0x0aa9
46 #define ADDR1           0x0556
47 #define FLASH_WORD_SIZE unsigned char
48 #endif
49
50 #ifdef CONFIG_CPCI405
51 #define ADDR0           0x5555
52 #define ADDR1           0x2aaa
53 #define FLASH_WORD_SIZE unsigned short
54 #endif
55
56 #ifdef CONFIG_WALNUT405
57 #define ADDR0           0x5555
58 #define ADDR1           0x2aaa
59 #define FLASH_WORD_SIZE unsigned char
60 #endif
61
62 /*-----------------------------------------------------------------------
63  */
64
65 unsigned long flash_init (void)
66 {
67         unsigned long size_b0, size_b1;
68         int i;
69         uint pbcr;
70         unsigned long base_b0, base_b1;
71
72         /* Init: no FLASHes known */
73         for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
74                 flash_info[i].flash_id = FLASH_UNKNOWN;
75         }
76
77         /* Static FLASH Bank configuration here - FIXME XXX */
78
79         size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
80
81         if (flash_info[0].flash_id == FLASH_UNKNOWN) {
82                 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
83                         size_b0, size_b0<<20);
84         }
85
86         /* Only one bank */
87         if (CFG_MAX_FLASH_BANKS == 1)
88           {
89             /* Setup offsets */
90             flash_get_offsets (FLASH_BASE0_PRELIM, &flash_info[0]);
91
92             /* Monitor protection ON by default */
93             (void)flash_protect(FLAG_PROTECT_SET,
94                                 FLASH_BASE0_PRELIM,
95                                 FLASH_BASE0_PRELIM+CFG_MONITOR_LEN-1,
96                                 &flash_info[0]);
97             size_b1 = 0 ;
98             flash_info[0].size = size_b0;
99           }
100
101         /* 2 banks */
102         else
103           {
104             size_b1 = flash_get_size((vu_long *)FLASH_BASE1_PRELIM, &flash_info[1]);
105
106             /* Re-do sizing to get full correct info */
107
108             if (size_b1)
109               {
110                 mtdcr(ebccfga, pb0cr);
111                 pbcr = mfdcr(ebccfgd);
112                 mtdcr(ebccfga, pb0cr);
113                 base_b1 = -size_b1;
114                 pbcr = (pbcr & 0x0001ffff) | base_b1 | (((size_b1/1024/1024)-1)<<17);
115                 mtdcr(ebccfgd, pbcr);
116                 /*          printf("pb1cr = %x\n", pbcr); */
117               }
118
119             if (size_b0)
120               {
121                 mtdcr(ebccfga, pb1cr);
122                 pbcr = mfdcr(ebccfgd);
123                 mtdcr(ebccfga, pb1cr);
124                 base_b0 = base_b1 - size_b0;
125                 pbcr = (pbcr & 0x0001ffff) | base_b0 | (((size_b0/1024/1024)-1)<<17);
126                 mtdcr(ebccfgd, pbcr);
127                 /*            printf("pb0cr = %x\n", pbcr); */
128               }
129
130             size_b0 = flash_get_size((vu_long *)base_b0, &flash_info[0]);
131
132             flash_get_offsets (base_b0, &flash_info[0]);
133
134             /* monitor protection ON by default */
135             (void)flash_protect(FLAG_PROTECT_SET,
136                                 base_b0+size_b0-CFG_MONITOR_LEN,
137                                 base_b0+size_b0-1,
138                                 &flash_info[0]);
139
140             if (size_b1) {
141               /* Re-do sizing to get full correct info */
142               size_b1 = flash_get_size((vu_long *)base_b1, &flash_info[1]);
143
144               flash_get_offsets (base_b1, &flash_info[1]);
145
146               /* monitor protection ON by default */
147               (void)flash_protect(FLAG_PROTECT_SET,
148                                   base_b1+size_b1-CFG_MONITOR_LEN,
149                                   base_b1+size_b1-1,
150                                   &flash_info[1]);
151               /* monitor protection OFF by default (one is enough) */
152               (void)flash_protect(FLAG_PROTECT_CLEAR,
153                                   base_b0+size_b0-CFG_MONITOR_LEN,
154                                   base_b0+size_b0-1,
155                                   &flash_info[0]);
156             } else {
157               flash_info[1].flash_id = FLASH_UNKNOWN;
158               flash_info[1].sector_count = -1;
159             }
160
161             flash_info[0].size = size_b0;
162             flash_info[1].size = size_b1;
163           }/* else 2 banks */
164         return (size_b0 + size_b1);
165 }
166
167
168
169 /*-----------------------------------------------------------------------
170  */
171 static void flash_get_offsets (ulong base, flash_info_t *info)
172 {
173         int i;
174
175         /* set up sector start address table */
176         if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
177             (info->flash_id  == FLASH_AM040)){
178             for (i = 0; i < info->sector_count; i++)
179                 info->start[i] = base + (i * 0x00010000);
180         } else {
181             if (info->flash_id & FLASH_BTYPE) {
182                 /* set sector offsets for bottom boot block type        */
183                 info->start[0] = base + 0x00000000;
184                 info->start[1] = base + 0x00004000;
185                 info->start[2] = base + 0x00006000;
186                 info->start[3] = base + 0x00008000;
187                 for (i = 4; i < info->sector_count; i++) {
188                         info->start[i] = base + (i * 0x00010000) - 0x00030000;
189                 }
190             } else {
191                 /* set sector offsets for top boot block type           */
192                 i = info->sector_count - 1;
193                 info->start[i--] = base + info->size - 0x00004000;
194                 info->start[i--] = base + info->size - 0x00006000;
195                 info->start[i--] = base + info->size - 0x00008000;
196                 for (; i >= 0; i--) {
197                         info->start[i] = base + i * 0x00010000;
198                 }
199             }
200         }
201 }
202
203 /*-----------------------------------------------------------------------
204  */
205 void flash_print_info  (flash_info_t *info)
206 {
207         int i;
208         int k;
209         int size;
210         int erased;
211         volatile unsigned long *flash;
212
213         if (info->flash_id == FLASH_UNKNOWN) {
214                 printf ("missing or unknown FLASH type\n");
215                 return;
216         }
217
218         switch (info->flash_id & FLASH_VENDMASK) {
219         case FLASH_MAN_AMD:     printf ("AMD ");                break;
220         case FLASH_MAN_FUJ:     printf ("FUJITSU ");            break;
221         case FLASH_MAN_SST:     printf ("SST ");                break;
222         default:                printf ("Unknown Vendor ");     break;
223         }
224
225         switch (info->flash_id & FLASH_TYPEMASK) {
226         case FLASH_AM040:       printf ("AM29F040 (512 Kbit, uniform sector size)\n");
227                                 break;
228         case FLASH_AM400B:      printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
229                                 break;
230         case FLASH_AM400T:      printf ("AM29LV400T (4 Mbit, top boot sector)\n");
231                                 break;
232         case FLASH_AM800B:      printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
233                                 break;
234         case FLASH_AM800T:      printf ("AM29LV800T (8 Mbit, top boot sector)\n");
235                                 break;
236         case FLASH_AM160B:      printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
237                                 break;
238         case FLASH_AM160T:      printf ("AM29LV160T (16 Mbit, top boot sector)\n");
239                                 break;
240         case FLASH_AM320B:      printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
241                                 break;
242         case FLASH_AM320T:      printf ("AM29LV320T (32 Mbit, top boot sector)\n");
243                                 break;
244         case FLASH_SST800A:     printf ("SST39LF/VF800 (8 Mbit, uniform sector size)\n");
245                                 break;
246         case FLASH_SST160A:     printf ("SST39LF/VF160 (16 Mbit, uniform sector size)\n");
247                                 break;
248         default:                printf ("Unknown Chip Type\n");
249                                 break;
250         }
251
252         printf ("  Size: %ld KB in %d Sectors\n",
253                 info->size >> 10, info->sector_count);
254
255         printf ("  Sector Start Addresses:");
256         for (i=0; i<info->sector_count; ++i) {
257                 /*
258                  * Check if whole sector is erased
259                  */
260                 if (i != (info->sector_count-1))
261                   size = info->start[i+1] - info->start[i];
262                 else
263                   size = info->start[0] + info->size - info->start[i];
264                 erased = 1;
265                 flash = (volatile unsigned long *)info->start[i];
266                 size = size >> 2;        /* divide by 4 for longword access */
267                 for (k=0; k<size; k++)
268                   {
269                     if (*flash++ != 0xffffffff)
270                       {
271                         erased = 0;
272                         break;
273                       }
274                   }
275
276                 if ((i % 5) == 0)
277                         printf ("\n   ");
278 #if 0 /* test-only */
279                 printf (" %08lX%s",
280                         info->start[i],
281                         info->protect[i] ? " (RO)" : "     "
282 #else
283                 printf (" %08lX%s%s",
284                         info->start[i],
285                         erased ? " E" : "  ",
286                         info->protect[i] ? "RO " : "   "
287 #endif
288                 );
289         }
290         printf ("\n");
291         return;
292 }
293
294 /*-----------------------------------------------------------------------
295  */
296
297
298 /*-----------------------------------------------------------------------
299  */
300
301 /*
302  * The following code cannot be run from FLASH!
303  */
304 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
305 {
306         short i;
307         FLASH_WORD_SIZE value;
308         ulong base = (ulong)addr;
309         volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)addr;
310
311         /* Write auto select command: read Manufacturer ID */
312         addr2[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
313         addr2[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
314         addr2[ADDR0] = (FLASH_WORD_SIZE)0x00900090;
315
316 #ifdef CONFIG_ADCIOP
317         value = addr2[2];
318 #else
319         value = addr2[0];
320 #endif
321
322         switch (value) {
323         case (FLASH_WORD_SIZE)AMD_MANUFACT:
324                 info->flash_id = FLASH_MAN_AMD;
325                 break;
326         case (FLASH_WORD_SIZE)FUJ_MANUFACT:
327                 info->flash_id = FLASH_MAN_FUJ;
328                 break;
329         case (FLASH_WORD_SIZE)SST_MANUFACT:
330                 info->flash_id = FLASH_MAN_SST;
331                 break;
332         default:
333                 info->flash_id = FLASH_UNKNOWN;
334                 info->sector_count = 0;
335                 info->size = 0;
336                 return (0);                     /* no or unknown flash  */
337         }
338
339 #ifdef CONFIG_ADCIOP
340         value = addr2[0];                       /* device ID            */
341         /*        printf("\ndev_code=%x\n", value); */
342 #else
343         value = addr2[1];                       /* device ID            */
344 #endif
345
346         switch (value) {
347         case (FLASH_WORD_SIZE)AMD_ID_F040B:
348                 info->flash_id += FLASH_AM040;
349                 info->sector_count = 8;
350                 info->size = 0x0080000; /* => 512 ko */
351                 break;
352         case (FLASH_WORD_SIZE)AMD_ID_LV400T:
353                 info->flash_id += FLASH_AM400T;
354                 info->sector_count = 11;
355                 info->size = 0x00080000;
356                 break;                          /* => 0.5 MB            */
357
358         case (FLASH_WORD_SIZE)AMD_ID_LV400B:
359                 info->flash_id += FLASH_AM400B;
360                 info->sector_count = 11;
361                 info->size = 0x00080000;
362                 break;                          /* => 0.5 MB            */
363
364         case (FLASH_WORD_SIZE)AMD_ID_LV800T:
365                 info->flash_id += FLASH_AM800T;
366                 info->sector_count = 19;
367                 info->size = 0x00100000;
368                 break;                          /* => 1 MB              */
369
370         case (FLASH_WORD_SIZE)AMD_ID_LV800B:
371                 info->flash_id += FLASH_AM800B;
372                 info->sector_count = 19;
373                 info->size = 0x00100000;
374                 break;                          /* => 1 MB              */
375
376         case (FLASH_WORD_SIZE)AMD_ID_LV160T:
377                 info->flash_id += FLASH_AM160T;
378                 info->sector_count = 35;
379                 info->size = 0x00200000;
380                 break;                          /* => 2 MB              */
381
382         case (FLASH_WORD_SIZE)AMD_ID_LV160B:
383                 info->flash_id += FLASH_AM160B;
384                 info->sector_count = 35;
385                 info->size = 0x00200000;
386                 break;                          /* => 2 MB              */
387 #if 0   /* enable when device IDs are available */
388         case (FLASH_WORD_SIZE)AMD_ID_LV320T:
389                 info->flash_id += FLASH_AM320T;
390                 info->sector_count = 67;
391                 info->size = 0x00400000;
392                 break;                          /* => 4 MB              */
393
394         case (FLASH_WORD_SIZE)AMD_ID_LV320B:
395                 info->flash_id += FLASH_AM320B;
396                 info->sector_count = 67;
397                 info->size = 0x00400000;
398                 break;                          /* => 4 MB              */
399 #endif
400         case (FLASH_WORD_SIZE)SST_ID_xF800A:
401                 info->flash_id += FLASH_SST800A;
402                 info->sector_count = 16;
403                 info->size = 0x00100000;
404                 break;                          /* => 1 MB              */
405
406         case (FLASH_WORD_SIZE)SST_ID_xF160A:
407                 info->flash_id += FLASH_SST160A;
408                 info->sector_count = 32;
409                 info->size = 0x00200000;
410                 break;                          /* => 2 MB              */
411
412         default:
413                 info->flash_id = FLASH_UNKNOWN;
414                 return (0);                     /* => no or unknown flash */
415
416         }
417
418         /* set up sector start address table */
419         if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
420             (info->flash_id  == FLASH_AM040)){
421             for (i = 0; i < info->sector_count; i++)
422                 info->start[i] = base + (i * 0x00010000);
423         } else {
424             if (info->flash_id & FLASH_BTYPE) {
425                 /* set sector offsets for bottom boot block type        */
426                 info->start[0] = base + 0x00000000;
427                 info->start[1] = base + 0x00004000;
428                 info->start[2] = base + 0x00006000;
429                 info->start[3] = base + 0x00008000;
430                 for (i = 4; i < info->sector_count; i++) {
431                         info->start[i] = base + (i * 0x00010000) - 0x00030000;
432                 }
433             } else {
434                 /* set sector offsets for top boot block type           */
435                 i = info->sector_count - 1;
436                 info->start[i--] = base + info->size - 0x00004000;
437                 info->start[i--] = base + info->size - 0x00006000;
438                 info->start[i--] = base + info->size - 0x00008000;
439                 for (; i >= 0; i--) {
440                         info->start[i] = base + i * 0x00010000;
441                 }
442             }
443         }
444
445         /* check for protected sectors */
446         for (i = 0; i < info->sector_count; i++) {
447                 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
448                 /* D0 = 1 if protected */
449 #ifdef CONFIG_ADCIOP
450                 addr2 = (volatile FLASH_WORD_SIZE *)(info->start[i]);
451                 info->protect[i] = addr2[4] & 1;
452 #else
453                 addr2 = (volatile FLASH_WORD_SIZE *)(info->start[i]);
454                 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
455                   info->protect[i] = 0;
456                 else
457                   info->protect[i] = addr2[2] & 1;
458 #endif
459         }
460
461         /*
462          * Prevent writes to uninitialized FLASH.
463          */
464         if (info->flash_id != FLASH_UNKNOWN) {
465 #if 0 /* test-only */
466 #ifdef CONFIG_ADCIOP
467                 addr2 = (volatile unsigned char *)info->start[0];
468                 addr2[ADDR0] = 0xAA;
469                 addr2[ADDR1] = 0x55;
470                 addr2[ADDR0] = 0xF0;  /* reset bank */
471 #else
472                 addr2 = (FLASH_WORD_SIZE *)info->start[0];
473                 *addr2 = (FLASH_WORD_SIZE)0x00F000F0;   /* reset bank */
474 #endif
475 #else /* test-only */
476                 addr2 = (FLASH_WORD_SIZE *)info->start[0];
477                 *addr2 = (FLASH_WORD_SIZE)0x00F000F0;   /* reset bank */
478 #endif /* test-only */
479         }
480
481         return (info->size);
482 }
483
484 int wait_for_DQ7(flash_info_t *info, int sect)
485 {
486         ulong start, now, last;
487         volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[sect]);
488
489         start = get_timer (0);
490     last  = start;
491     while ((addr[0] & (FLASH_WORD_SIZE)0x00800080) != (FLASH_WORD_SIZE)0x00800080) {
492         if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
493             printf ("Timeout\n");
494             return -1;
495         }
496         /* show that we're waiting */
497         if ((now - last) > 1000) {  /* every second */
498             putc ('.');
499             last = now;
500         }
501     }
502         return 0;
503 }
504
505 /*-----------------------------------------------------------------------
506  */
507
508 int     flash_erase (flash_info_t *info, int s_first, int s_last)
509 {
510         volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[0]);
511         volatile FLASH_WORD_SIZE *addr2;
512         int flag, prot, sect, l_sect;
513         int i;
514
515         if ((s_first < 0) || (s_first > s_last)) {
516                 if (info->flash_id == FLASH_UNKNOWN) {
517                         printf ("- missing\n");
518                 } else {
519                         printf ("- no sectors to erase\n");
520                 }
521                 return 1;
522         }
523
524         if (info->flash_id == FLASH_UNKNOWN) {
525                 printf ("Can't erase unknown flash type - aborted\n");
526                 return 1;
527         }
528
529         prot = 0;
530         for (sect=s_first; sect<=s_last; ++sect) {
531                 if (info->protect[sect]) {
532                         prot++;
533                 }
534         }
535
536         if (prot) {
537                 printf ("- Warning: %d protected sectors will not be erased!\n",
538                         prot);
539         } else {
540                 printf ("\n");
541         }
542
543         l_sect = -1;
544
545         /* Disable interrupts which might cause a timeout here */
546         flag = disable_interrupts();
547
548         /* Start erase on unprotected sectors */
549         for (sect = s_first; sect<=s_last; sect++) {
550                 if (info->protect[sect] == 0) { /* not protected */
551                     addr2 = (FLASH_WORD_SIZE *)(info->start[sect]);
552                     printf("Erasing sector %p\n", addr2);       /* CLH */
553
554                     if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
555                         addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
556                         addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
557                         addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
558                         addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
559                         addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
560                         addr2[0] = (FLASH_WORD_SIZE)0x00500050;  /* block erase */
561                         for (i=0; i<50; i++)
562                                 udelay(1000);  /* wait 1 ms */
563                     } else {
564                         addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
565                         addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
566                         addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
567                         addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
568                         addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
569                         addr2[0] = (FLASH_WORD_SIZE)0x00300030;  /* sector erase */
570                     }
571                     l_sect = sect;
572                     /*
573                      * Wait for each sector to complete, it's more
574                      * reliable.  According to AMD Spec, you must
575                      * issue all erase commands within a specified
576                      * timeout.  This has been seen to fail, especially
577                      * if printf()s are included (for debug)!!
578                      */
579                     wait_for_DQ7(info, sect);
580                 }
581         }
582
583         /* re-enable interrupts if necessary */
584         if (flag)
585                 enable_interrupts();
586
587         /* wait at least 80us - let's wait 1 ms */
588         udelay (1000);
589
590 #if 0
591         /*
592          * We wait for the last triggered sector
593          */
594         if (l_sect < 0)
595                 goto DONE;
596         wait_for_DQ7(info, l_sect);
597
598 DONE:
599 #endif
600         /* reset to read mode */
601         addr = (FLASH_WORD_SIZE *)info->start[0];
602         addr[0] = (FLASH_WORD_SIZE)0x00F000F0;  /* reset bank */
603
604         printf (" done\n");
605         return 0;
606 }
607
608 /*-----------------------------------------------------------------------
609  * Copy memory to flash, returns:
610  * 0 - OK
611  * 1 - write timeout
612  * 2 - Flash not erased
613  */
614
615 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
616 {
617         ulong cp, wp, data;
618         int i, l, rc;
619
620         wp = (addr & ~3);       /* get lower word aligned address */
621
622         /*
623          * handle unaligned start bytes
624          */
625         if ((l = addr - wp) != 0) {
626                 data = 0;
627                 for (i=0, cp=wp; i<l; ++i, ++cp) {
628                         data = (data << 8) | (*(uchar *)cp);
629                 }
630                 for (; i<4 && cnt>0; ++i) {
631                         data = (data << 8) | *src++;
632                         --cnt;
633                         ++cp;
634                 }
635                 for (; cnt==0 && i<4; ++i, ++cp) {
636                         data = (data << 8) | (*(uchar *)cp);
637                 }
638
639                 if ((rc = write_word(info, wp, data)) != 0) {
640                         return (rc);
641                 }
642                 wp += 4;
643         }
644
645         /*
646          * handle word aligned part
647          */
648         while (cnt >= 4) {
649                 data = 0;
650                 for (i=0; i<4; ++i) {
651                         data = (data << 8) | *src++;
652                 }
653                 if ((rc = write_word(info, wp, data)) != 0) {
654                         return (rc);
655                 }
656                 wp  += 4;
657                 cnt -= 4;
658         }
659
660         if (cnt == 0) {
661                 return (0);
662         }
663
664         /*
665          * handle unaligned tail bytes
666          */
667         data = 0;
668         for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
669                 data = (data << 8) | *src++;
670                 --cnt;
671         }
672         for (; i<4; ++i, ++cp) {
673                 data = (data << 8) | (*(uchar *)cp);
674         }
675
676         return (write_word(info, wp, data));
677 }
678
679 /*-----------------------------------------------------------------------
680  * Write a word to Flash, returns:
681  * 0 - OK
682  * 1 - write timeout
683  * 2 - Flash not erased
684  */
685 static int write_word (flash_info_t * info, ulong dest, ulong data)
686 {
687         volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *) (info->start[0]);
688         volatile FLASH_WORD_SIZE *dest2 = (FLASH_WORD_SIZE *) dest;
689         volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *) & data;
690         ulong start;
691         int i;
692
693         /* Check if Flash is (sufficiently) erased */
694         if ((*((volatile FLASH_WORD_SIZE *) dest) &
695             (FLASH_WORD_SIZE) data) != (FLASH_WORD_SIZE) data) {
696                 return (2);
697         }
698
699         for (i = 0; i < 4 / sizeof (FLASH_WORD_SIZE); i++) {
700                 int flag;
701
702                 /* Disable interrupts which might cause a timeout here */
703                 flag = disable_interrupts ();
704
705                 addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00AA00AA;
706                 addr2[ADDR1] = (FLASH_WORD_SIZE) 0x00550055;
707                 addr2[ADDR0] = (FLASH_WORD_SIZE) 0x00A000A0;
708
709                 dest2[i] = data2[i];
710
711                 /* re-enable interrupts if necessary */
712                 if (flag)
713                         enable_interrupts ();
714
715                 /* data polling for D7 */
716                 start = get_timer (0);
717                 while ((dest2[i] & (FLASH_WORD_SIZE) 0x00800080) !=
718                        (data2[i] & (FLASH_WORD_SIZE) 0x00800080)) {
719
720                         if (get_timer (start) > CFG_FLASH_WRITE_TOUT) {
721                                 return (1);
722                         }
723                 }
724         }
725
726         return (0);
727 }
728
729 /*-----------------------------------------------------------------------
730  */