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