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