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