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