Initial revision
[platform/kernel/u-boot.git] / board / bmw / 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 #include <common.h>
25 #include <mpc824x.h>
26 #include <asm/processor.h>
27 #include <asm/pci_io.h>
28
29 #define ROM_CS0_START   0xFF800000
30 #define ROM_CS1_START   0xFF000000
31
32 flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips    */
33
34 #if defined(CFG_ENV_IS_IN_FLASH)
35 # ifndef  CFG_ENV_ADDR
36 #  define CFG_ENV_ADDR  (CFG_FLASH_BASE + CFG_ENV_OFFSET)
37 # endif
38 # ifndef  CFG_ENV_SIZE
39 #  define CFG_ENV_SIZE  CFG_ENV_SECT_SIZE
40 # endif
41 # ifndef  CFG_ENV_SECT_SIZE
42 #  define CFG_ENV_SECT_SIZE  CFG_ENV_SIZE
43 # endif
44 #endif
45
46 /*-----------------------------------------------------------------------
47  * Functions
48  */
49 static int write_word (flash_info_t *info, ulong dest, ulong data);
50 #if 0
51 static void flash_get_offsets (ulong base, flash_info_t *info);
52 #endif /* 0 */
53
54 /*flash command address offsets*/
55
56 #if 0
57 #define ADDR0           (0x555)
58 #define ADDR1           (0x2AA)
59 #define ADDR3           (0x001)
60 #else
61 #define ADDR0           (0xAAA)
62 #define ADDR1           (0x555)
63 #define ADDR3           (0x001)
64 #endif
65
66 #define FLASH_WORD_SIZE unsigned char
67
68 /*-----------------------------------------------------------------------
69  */
70
71 #if 0
72 static int byte_parity_odd(unsigned char x) __attribute__ ((const));
73 #endif /* 0 */
74 static unsigned long flash_id(unsigned char mfct, unsigned char chip) __attribute__ ((const));
75
76 typedef struct
77 {
78   FLASH_WORD_SIZE extval;
79   unsigned short intval;
80 } map_entry;
81
82 #if 0
83 static int
84 byte_parity_odd(unsigned char x)
85 {
86   x ^= x >> 4;
87   x ^= x >> 2;
88   x ^= x >> 1;
89   return (x & 0x1) != 0;
90 }
91 #endif /* 0 */
92
93
94
95 static unsigned long
96 flash_id(unsigned char mfct, unsigned char chip)
97 {
98   static const map_entry mfct_map[] =
99     {
100       {(FLASH_WORD_SIZE) AMD_MANUFACT,  (unsigned short) ((unsigned long) FLASH_MAN_AMD >> 16)},
101       {(FLASH_WORD_SIZE) FUJ_MANUFACT,  (unsigned short) ((unsigned long) FLASH_MAN_FUJ >> 16)},
102       {(FLASH_WORD_SIZE) STM_MANUFACT,  (unsigned short) ((unsigned long) FLASH_MAN_STM >> 16)},
103       {(FLASH_WORD_SIZE) MT_MANUFACT,   (unsigned short) ((unsigned long) FLASH_MAN_MT >> 16)},
104       {(FLASH_WORD_SIZE) INTEL_MANUFACT,(unsigned short) ((unsigned long) FLASH_MAN_INTEL >> 16)},
105       {(FLASH_WORD_SIZE) INTEL_ALT_MANU,(unsigned short) ((unsigned long) FLASH_MAN_INTEL >> 16)}
106     };
107
108   static const map_entry chip_map[] =
109   {
110     {AMD_ID_F040B,      FLASH_AM040},
111     {(FLASH_WORD_SIZE) STM_ID_x800AB,   FLASH_STM800AB}
112   };
113
114   const map_entry *p;
115   unsigned long result = FLASH_UNKNOWN;
116
117   /* find chip id */
118   for(p = &chip_map[0]; p < &chip_map[sizeof chip_map / sizeof chip_map[0]]; p++)
119     if(p->extval == chip)
120     {
121       result = FLASH_VENDMASK | p->intval;
122       break;
123     }
124
125   /* find vendor id */
126   for(p = &mfct_map[0]; p < &mfct_map[sizeof mfct_map / sizeof mfct_map[0]]; p++)
127     if(p->extval == mfct)
128     {
129       result &= ~FLASH_VENDMASK;
130       result |= (unsigned long) p->intval << 16;
131       break;
132     }
133
134   return result;
135 }
136
137
138
139 unsigned long
140 flash_init(void)
141 {
142   unsigned long i;
143   unsigned char j;
144   static const ulong flash_banks[] = CFG_FLASH_BANKS;
145
146   /* Init: no FLASHes known */
147   for (i = 0; i < CFG_MAX_FLASH_BANKS; i++)
148   {
149     flash_info_t * const pflinfo = &flash_info[i];
150     pflinfo->flash_id = FLASH_UNKNOWN;
151     pflinfo->size = 0;
152     pflinfo->sector_count = 0;
153   }
154
155   for(i = 0; i < sizeof flash_banks / sizeof flash_banks[0]; i++)
156   {
157     flash_info_t * const pflinfo = &flash_info[i];
158     const unsigned long base_address = flash_banks[i];
159     volatile FLASH_WORD_SIZE * const flash = (FLASH_WORD_SIZE *) base_address;
160 #if 0
161     volatile FLASH_WORD_SIZE * addr2;
162 #endif
163 #if 0
164     /* write autoselect sequence */
165     flash[0x5555] = 0xaa;
166     flash[0x2aaa] = 0x55;
167     flash[0x5555] = 0x90;
168 #else
169     flash[0xAAA << (3 * i)] = 0xaa;
170     flash[0x555 << (3 * i)] = 0x55;
171     flash[0xAAA << (3 * i)] = 0x90;
172 #endif
173     __asm__ __volatile__("sync");
174
175 #if 0
176     pflinfo->flash_id = flash_id(flash[0x0], flash[0x1]);
177 #else
178     pflinfo->flash_id = flash_id(flash[0x0], flash[0x2 + 14 * i]);
179 #endif
180
181     switch(pflinfo->flash_id & FLASH_TYPEMASK)
182     {
183       case FLASH_AM040:
184         pflinfo->size = 0x00080000;
185         pflinfo->sector_count = 8;
186         for(j = 0; j < 8; j++)
187         {
188           pflinfo->start[j] = base_address + 0x00010000 * j;
189           pflinfo->protect[j] = flash[(j << 16) | 0x2];
190         }
191         break;
192       case FLASH_STM800AB:
193         pflinfo->size = 0x00100000;
194         pflinfo->sector_count = 19;
195         pflinfo->start[0] = base_address;
196         pflinfo->start[1] = base_address + 0x4000;
197         pflinfo->start[2] = base_address + 0x6000;
198         pflinfo->start[3] = base_address + 0x8000;
199         for(j = 1; j < 16; j++)
200         {
201           pflinfo->start[j+3] = base_address + 0x00010000 * j;
202         }
203 #if 0
204         /* check for protected sectors */
205         for (j = 0; j < pflinfo->sector_count; j++) {
206           /* read sector protection at sector address, (A7 .. A0) = 0x02 */
207           /* D0 = 1 if protected */
208           addr2 = (volatile FLASH_WORD_SIZE *)(pflinfo->start[j]);
209             if (pflinfo->flash_id & FLASH_MAN_SST)
210               pflinfo->protect[j] = 0;
211             else
212               pflinfo->protect[j] = addr2[2] & 1;
213         }
214 #endif
215         break;
216     }
217     /* Protect monitor and environment sectors
218      */
219 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
220     flash_protect(FLAG_PROTECT_SET,
221                 CFG_MONITOR_BASE,
222                 CFG_MONITOR_BASE + CFG_MONITOR_LEN - 1,
223                 &flash_info[0]);
224 #endif
225
226 #if (CFG_ENV_IS_IN_FLASH == 1) && defined(CFG_ENV_ADDR)
227     flash_protect(FLAG_PROTECT_SET,
228                 CFG_ENV_ADDR,
229                 CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
230                 &flash_info[0]);
231 #endif
232
233     /* reset device to read mode */
234     flash[0x0000] = 0xf0;
235     __asm__ __volatile__("sync");
236   }
237
238     return flash_info[0].size + flash_info[1].size;
239 }
240
241 #if 0
242 static void
243 flash_get_offsets (ulong base, flash_info_t *info)
244 {
245     int i;
246
247     /* set up sector start address table */
248         if (info->flash_id & FLASH_MAN_SST)
249           {
250             for (i = 0; i < info->sector_count; i++)
251               info->start[i] = base + (i * 0x00010000);
252           }
253         else
254     if (info->flash_id & FLASH_BTYPE) {
255         /* set sector offsets for bottom boot block type    */
256         info->start[0] = base + 0x00000000;
257         info->start[1] = base + 0x00004000;
258         info->start[2] = base + 0x00006000;
259         info->start[3] = base + 0x00008000;
260         for (i = 4; i < info->sector_count; i++) {
261             info->start[i] = base + (i * 0x00010000) - 0x00030000;
262         }
263     } else {
264         /* set sector offsets for top boot block type       */
265         i = info->sector_count - 1;
266         info->start[i--] = base + info->size - 0x00004000;
267         info->start[i--] = base + info->size - 0x00006000;
268         info->start[i--] = base + info->size - 0x00008000;
269         for (; i >= 0; i--) {
270             info->start[i] = base + i * 0x00010000;
271         }
272     }
273
274 }
275 #endif /* 0 */
276
277 /*-----------------------------------------------------------------------
278  */
279 void
280 flash_print_info(flash_info_t *info)
281 {
282   static const char unk[] = "Unknown";
283   const char *mfct = unk, *type = unk;
284   unsigned int i;
285
286   if(info->flash_id != FLASH_UNKNOWN)
287   {
288     switch(info->flash_id & FLASH_VENDMASK)
289     {
290       case FLASH_MAN_AMD:       mfct = "AMD";                           break;
291       case FLASH_MAN_FUJ:       mfct = "FUJITSU";                       break;
292       case FLASH_MAN_STM:       mfct = "STM";                           break;
293       case FLASH_MAN_SST:       mfct = "SST";                           break;
294       case FLASH_MAN_BM:        mfct = "Bright Microelectonics";        break;
295       case FLASH_MAN_INTEL:     mfct = "Intel";                         break;
296     }
297
298     switch(info->flash_id & FLASH_TYPEMASK)
299     {
300       case FLASH_AM040:         type = "AM29F040B (512K * 8, uniform sector size)";     break;
301       case FLASH_AM400B:        type = "AM29LV400B (4 Mbit, bottom boot sect)";         break;
302       case FLASH_AM400T:        type = "AM29LV400T (4 Mbit, top boot sector)";          break;
303       case FLASH_AM800B:        type = "AM29LV800B (8 Mbit, bottom boot sect)";         break;
304       case FLASH_AM800T:        type = "AM29LV800T (8 Mbit, top boot sector)";          break;
305       case FLASH_AM160T:        type = "AM29LV160T (16 Mbit, top boot sector)";         break;
306       case FLASH_AM320B:        type = "AM29LV320B (32 Mbit, bottom boot sect)";        break;
307       case FLASH_AM320T:        type = "AM29LV320T (32 Mbit, top boot sector)";         break;
308       case FLASH_STM800AB:      type = "M29W800AB (8 Mbit, bottom boot sect)";          break;
309       case FLASH_SST800A:       type = "SST39LF/VF800 (8 Mbit, uniform sector size)";   break;
310       case FLASH_SST160A:       type = "SST39LF/VF160 (16 Mbit, uniform sector size)";  break;
311     }
312   }
313
314   printf(
315     "\n  Brand: %s Type: %s\n"
316     "  Size: %lu KB in %d Sectors\n",
317     mfct,
318     type,
319     info->size >> 10,
320     info->sector_count
321   );
322
323   printf ("  Sector Start Addresses:");
324
325   for (i = 0; i < info->sector_count; i++)
326   {
327     unsigned long size;
328     unsigned int erased;
329     unsigned long * flash = (unsigned long *) info->start[i];
330
331     /*
332      * Check if whole sector is erased
333      */
334     size =
335       (i != (info->sector_count - 1)) ?
336       (info->start[i + 1] - info->start[i]) >> 2 :
337       (info->start[0] + info->size - info->start[i]) >> 2;
338
339     for(
340       flash = (unsigned long *) info->start[i], erased = 1;
341       (flash != (unsigned long *) info->start[i] + size) && erased;
342       flash++
343     )
344       erased = *flash == ~0x0UL;
345
346     printf(
347       "%s %08lX %s %s",
348       (i % 5) ? "" : "\n   ",
349       info->start[i],
350       erased ? "E" : " ",
351       info->protect[i] ? "RO" : "  "
352     );
353   }
354
355   puts("\n");
356   return;
357 }
358
359 #if 0
360
361 /*
362  * The following code cannot be run from FLASH!
363  */
364 ulong
365 flash_get_size (vu_long *addr, flash_info_t *info)
366 {
367    short i;
368     FLASH_WORD_SIZE value;
369     ulong base = (ulong)addr;
370         volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)addr;
371
372     printf("flash_get_size: \n");
373     /* Write auto select command: read Manufacturer ID */
374     eieio();
375     addr2[ADDR0] = (FLASH_WORD_SIZE)0xAA;
376     addr2[ADDR1] = (FLASH_WORD_SIZE)0x55;
377     addr2[ADDR0] = (FLASH_WORD_SIZE)0x90;
378     value = addr2[0];
379
380     switch (value) {
381     case (FLASH_WORD_SIZE)AMD_MANUFACT:
382         info->flash_id = FLASH_MAN_AMD;
383         break;
384     case (FLASH_WORD_SIZE)FUJ_MANUFACT:
385         info->flash_id = FLASH_MAN_FUJ;
386         break;
387     case (FLASH_WORD_SIZE)SST_MANUFACT:
388         info->flash_id = FLASH_MAN_SST;
389         break;
390     default:
391         info->flash_id = FLASH_UNKNOWN;
392         info->sector_count = 0;
393         info->size = 0;
394         return (0);         /* no or unknown flash  */
395     }
396     printf("recognised manufacturer");
397
398     value = addr2[ADDR3];          /* device ID        */
399         debug ("\ndev_code=%x\n", value);
400
401     switch (value) {
402     case (FLASH_WORD_SIZE)AMD_ID_LV400T:
403         info->flash_id += FLASH_AM400T;
404         info->sector_count = 11;
405         info->size = 0x00080000;
406         break;              /* => 0.5 MB        */
407
408     case (FLASH_WORD_SIZE)AMD_ID_LV400B:
409         info->flash_id += FLASH_AM400B;
410         info->sector_count = 11;
411         info->size = 0x00080000;
412         break;              /* => 0.5 MB        */
413
414     case (FLASH_WORD_SIZE)AMD_ID_LV800T:
415         info->flash_id += FLASH_AM800T;
416         info->sector_count = 19;
417         info->size = 0x00100000;
418         break;              /* => 1 MB      */
419
420     case (FLASH_WORD_SIZE)AMD_ID_LV800B:
421         info->flash_id += FLASH_AM800B;
422         info->sector_count = 19;
423         info->size = 0x00100000;
424         break;              /* => 1 MB      */
425
426     case (FLASH_WORD_SIZE)AMD_ID_LV160T:
427         info->flash_id += FLASH_AM160T;
428         info->sector_count = 35;
429         info->size = 0x00200000;
430         break;              /* => 2 MB      */
431
432     case (FLASH_WORD_SIZE)AMD_ID_LV160B:
433         info->flash_id += FLASH_AM160B;
434         info->sector_count = 35;
435         info->size = 0x00200000;
436         break;              /* => 2 MB      */
437
438     case (FLASH_WORD_SIZE)SST_ID_xF800A:
439         info->flash_id += FLASH_SST800A;
440         info->sector_count = 16;
441         info->size = 0x00100000;
442         break;              /* => 1 MB      */
443
444     case (FLASH_WORD_SIZE)SST_ID_xF160A:
445         info->flash_id += FLASH_SST160A;
446         info->sector_count = 32;
447         info->size = 0x00200000;
448         break;              /* => 2 MB      */
449
450     case (FLASH_WORD_SIZE)AMD_ID_F040B:
451         info->flash_id += FLASH_AM040;
452         info->sector_count = 8;
453         info->size = 0x00080000;
454         break;              /* => 0.5 MB      */
455
456     default:
457         info->flash_id = FLASH_UNKNOWN;
458         return (0);         /* => no or unknown flash */
459
460     }
461
462     printf("flash id %lx; sector count %x, size %lx\n", info->flash_id,info->sector_count,info->size);
463     /* set up sector start address table */
464         if (info->flash_id & FLASH_MAN_SST)
465           {
466             for (i = 0; i < info->sector_count; i++)
467               info->start[i] = base + (i * 0x00010000);
468           }
469         else
470     if (info->flash_id & FLASH_BTYPE) {
471         /* set sector offsets for bottom boot block type    */
472         info->start[0] = base + 0x00000000;
473         info->start[1] = base + 0x00004000;
474         info->start[2] = base + 0x00006000;
475         info->start[3] = base + 0x00008000;
476         for (i = 4; i < info->sector_count; i++) {
477             info->start[i] = base + (i * 0x00010000) - 0x00030000;
478         }
479     } else {
480         /* set sector offsets for top boot block type       */
481         i = info->sector_count - 1;
482         info->start[i--] = base + info->size - 0x00004000;
483         info->start[i--] = base + info->size - 0x00006000;
484         info->start[i--] = base + info->size - 0x00008000;
485         for (; i >= 0; i--) {
486             info->start[i] = base + i * 0x00010000;
487         }
488     }
489
490     /* check for protected sectors */
491     for (i = 0; i < info->sector_count; i++) {
492         /* read sector protection at sector address, (A7 .. A0) = 0x02 */
493         /* D0 = 1 if protected */
494         addr2 = (volatile FLASH_WORD_SIZE *)(info->start[i]);
495                 if (info->flash_id & FLASH_MAN_SST)
496                   info->protect[i] = 0;
497                 else
498                   info->protect[i] = addr2[2] & 1;
499     }
500
501     /*
502      * Prevent writes to uninitialized FLASH.
503      */
504     if (info->flash_id != FLASH_UNKNOWN) {
505        addr2 = (FLASH_WORD_SIZE *)info->start[0];
506         *addr2 = (FLASH_WORD_SIZE)0x00F000F0;   /* reset bank */
507     }
508
509     return (info->size);
510 }
511
512 #endif
513
514
515 int
516 flash_erase(flash_info_t *info, int s_first, int s_last)
517 {
518     volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[0]);
519     int flag, prot, sect, l_sect;
520     ulong start, now, last;
521     unsigned char sh8b;
522
523     if ((s_first < 0) || (s_first > s_last)) {
524         if (info->flash_id == FLASH_UNKNOWN) {
525             printf ("- missing\n");
526         } else {
527             printf ("- no sectors to erase\n");
528         }
529         return 1;
530     }
531
532     if ((info->flash_id == FLASH_UNKNOWN) ||
533         (info->flash_id > (FLASH_MAN_STM | FLASH_AMD_COMP))) {
534         printf ("Can't erase unknown flash type - aborted\n");
535         return 1;
536     }
537
538     prot = 0;
539     for (sect=s_first; sect<=s_last; ++sect) {
540         if (info->protect[sect]) {
541             prot++;
542         }
543     }
544
545     if (prot) {
546         printf ("- Warning: %d protected sectors will not be erased!\n",
547             prot);
548     } else {
549         printf ("\n");
550     }
551
552     l_sect = -1;
553
554     /* Check the ROM CS */
555     if ((info->start[0] >= ROM_CS1_START) && (info->start[0] < ROM_CS0_START))
556       sh8b = 3;
557     else
558       sh8b = 0;
559
560     /* Disable interrupts which might cause a timeout here */
561     flag = disable_interrupts();
562
563     addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
564     addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
565     addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00800080;
566     addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
567     addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
568
569     /* Start erase on unprotected sectors */
570     for (sect = s_first; sect<=s_last; sect++) {
571         if (info->protect[sect] == 0) { /* not protected */
572             addr = (FLASH_WORD_SIZE *)(info->start[0] + (
573                                 (info->start[sect] - info->start[0]) << sh8b));
574                         if (info->flash_id & FLASH_MAN_SST)
575                           {
576                             addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
577                             addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
578                             addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00800080;
579                             addr[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
580                             addr[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
581                             addr[0] = (FLASH_WORD_SIZE)0x00500050;  /* block erase */
582                             udelay(30000);  /* wait 30 ms */
583                           }
584                         else
585                           addr[0] = (FLASH_WORD_SIZE)0x00300030;  /* sector erase */
586             l_sect = sect;
587         }
588     }
589
590     /* re-enable interrupts if necessary */
591     if (flag)
592         enable_interrupts();
593
594     /* wait at least 80us - let's wait 1 ms */
595     udelay (1000);
596
597     /*
598      * We wait for the last triggered sector
599      */
600     if (l_sect < 0)
601         goto DONE;
602
603     start = get_timer (0);
604     last  = start;
605     addr = (FLASH_WORD_SIZE *)(info->start[0] + (
606                         (info->start[l_sect] - info->start[0]) << sh8b));
607     while ((addr[0] & (FLASH_WORD_SIZE)0x00800080) != (FLASH_WORD_SIZE)0x00800080) {
608         if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
609             printf ("Timeout\n");
610             return 1;
611         }
612         /* show that we're waiting */
613         if ((now - last) > 1000) {  /* every second */
614             serial_putc ('.');
615             last = now;
616         }
617     }
618
619 DONE:
620     /* reset to read mode */
621     addr = (FLASH_WORD_SIZE *)info->start[0];
622     addr[0] = (FLASH_WORD_SIZE)0x00F000F0;  /* reset bank */
623
624     printf (" done\n");
625     return 0;
626 }
627
628 /*-----------------------------------------------------------------------
629  * Copy memory to flash, returns:
630  * 0 - OK
631  * 1 - write timeout
632  * 2 - Flash not erased
633  */
634
635 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
636 {
637     ulong cp, wp, data;
638     int i, l, rc;
639
640     wp = (addr & ~3);   /* get lower word aligned address */
641
642     /*
643      * handle unaligned start bytes
644      */
645     if ((l = addr - wp) != 0) {
646         data = 0;
647         for (i=0, cp=wp; i<l; ++i, ++cp) {
648             data = (data << 8) | (*(uchar *)cp);
649         }
650         for (; i<4 && cnt>0; ++i) {
651             data = (data << 8) | *src++;
652             --cnt;
653             ++cp;
654         }
655         for (; cnt==0 && i<4; ++i, ++cp) {
656             data = (data << 8) | (*(uchar *)cp);
657         }
658
659         if ((rc = write_word(info, wp, data)) != 0) {
660             return (rc);
661         }
662         wp += 4;
663     }
664
665     /*
666      * handle word aligned part
667      */
668     while (cnt >= 4) {
669         data = 0;
670         for (i=0; i<4; ++i) {
671             data = (data << 8) | *src++;
672         }
673         if ((rc = write_word(info, wp, data)) != 0) {
674             return (rc);
675         }
676         wp  += 4;
677         cnt -= 4;
678     }
679
680     if (cnt == 0) {
681         return (0);
682     }
683
684     /*
685      * handle unaligned tail bytes
686      */
687     data = 0;
688     for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
689         data = (data << 8) | *src++;
690         --cnt;
691     }
692     for (; i<4; ++i, ++cp) {
693         data = (data << 8) | (*(uchar *)cp);
694     }
695
696     return (write_word(info, wp, data));
697 }
698
699 /*-----------------------------------------------------------------------
700  * Write a word to Flash, returns:
701  * 0 - OK
702  * 1 - write timeout
703  * 2 - Flash not erased
704  */
705 static int write_word (flash_info_t *info, ulong dest, ulong data)
706 {
707         volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)info->start[0];
708         volatile FLASH_WORD_SIZE *dest2;
709         volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *)&data;
710     ulong start;
711     int flag;
712         int i;
713     unsigned char sh8b;
714
715     /* Check the ROM CS */
716     if ((info->start[0] >= ROM_CS1_START) && (info->start[0] < ROM_CS0_START))
717       sh8b = 3;
718     else
719       sh8b = 0;
720
721     dest2 = (FLASH_WORD_SIZE *)(((dest - info->start[0]) << sh8b) +
722                                 info->start[0]);
723
724     /* Check if Flash is (sufficiently) erased */
725     if ((*dest2 & (FLASH_WORD_SIZE)data) != (FLASH_WORD_SIZE)data) {
726         return (2);
727     }
728     /* Disable interrupts which might cause a timeout here */
729     flag = disable_interrupts();
730
731         for (i=0; i<4/sizeof(FLASH_WORD_SIZE); i++)
732           {
733             addr2[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00AA00AA;
734             addr2[ADDR1 << sh8b] = (FLASH_WORD_SIZE)0x00550055;
735             addr2[ADDR0 << sh8b] = (FLASH_WORD_SIZE)0x00A000A0;
736
737             dest2[i << sh8b] = data2[i];
738
739             /* re-enable interrupts if necessary */
740             if (flag)
741               enable_interrupts();
742
743             /* data polling for D7 */
744             start = get_timer (0);
745             while ((dest2[i << sh8b] & (FLASH_WORD_SIZE)0x00800080) !=
746                    (data2[i] & (FLASH_WORD_SIZE)0x00800080)) {
747               if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
748                 return (1);
749               }
750             }
751           }
752
753     return (0);
754 }
755
756 /*-----------------------------------------------------------------------
757  */