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