input: Add generic keyboard input handler
[oweals/u-boot.git] / board / evb64260 / flash.c
1 /*
2  * (C) Copyright 2001
3  * Josh Huber <huber@mclx.com>, Mission Critical Linux, Inc.
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  * flash.c - flash support for the 512k, 8bit boot flash on the GEVB
26  *           most of this file was based on the existing U-Boot
27  *           flash drivers.
28  */
29
30 #include <common.h>
31 #include <mpc8xx.h>
32 #include <galileo/gt64260R.h>
33 #include <galileo/memory.h>
34 #include "intel_flash.h"
35
36 #define FLASH_ROM       0xFFFD       /* unknown flash type                   */
37 #define FLASH_RAM       0xFFFE       /* unknown flash type                   */
38 #define FLASH_MAN_UNKNOWN 0xFFFF0000
39
40 /* #define DEBUG */
41 /* #define FLASH_ID_OVERRIDE */ /* Hack to set type to 040B if ROM emulator is installed.
42                                  * Can be used to program a ROM in circuit if a programmer
43                                  * is not available by swapping the rom out. */
44
45 /* Intel flash commands */
46 int flash_erase_intel(flash_info_t *info, int s_first, int s_last);
47 int write_word_intel(bank_addr_t addr, bank_word_t value);
48
49 flash_info_t    flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* info for FLASH chips */
50
51 /*-----------------------------------------------------------------------
52  * Functions
53  */
54 static ulong flash_get_size (int portwidth, vu_long *addr, flash_info_t *info);
55 static int write_word (flash_info_t *info, ulong dest, ulong data);
56 static void flash_get_offsets (ulong base, flash_info_t *info);
57
58 /*-----------------------------------------------------------------------
59  */
60
61 unsigned long
62 flash_init (void)
63 {
64         unsigned int i;
65         unsigned long size_b0 = 0, size_b1 = 0;
66         unsigned long base, flash_size;
67
68         /* Init: no FLASHes known */
69         for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
70                 flash_info[i].flash_id = FLASH_UNKNOWN;
71         }
72
73         /* the boot flash */
74         base = CONFIG_SYS_FLASH_BASE;
75 #ifndef CONFIG_SYS_BOOT_FLASH_WIDTH
76 #define CONFIG_SYS_BOOT_FLASH_WIDTH     1
77 #endif
78         size_b0 = flash_get_size(CONFIG_SYS_BOOT_FLASH_WIDTH, (vu_long *)base,
79                                  &flash_info[0]);
80
81 #ifndef CONFIG_P3G4
82         printf("[");
83         print_size (size_b0, "");
84         printf("@%08lX] ", base);
85 #endif
86
87         if (flash_info[0].flash_id == FLASH_UNKNOWN) {
88                 printf ("## Unknown FLASH at %08lx: Size = 0x%08lx = %ld MB\n",
89                         base, size_b0, size_b0<<20);
90         }
91
92         base = memoryGetDeviceBaseAddress(CONFIG_SYS_EXTRA_FLASH_DEVICE);
93         for(i=1;i<CONFIG_SYS_MAX_FLASH_BANKS;i++) {
94             unsigned long size = flash_get_size(CONFIG_SYS_EXTRA_FLASH_WIDTH, (vu_long *)base, &flash_info[i]);
95
96 #ifndef CONFIG_P3G4
97             printf("[");
98             print_size (size, "");
99             printf("@%08lX] ", base);
100 #endif
101
102             if (flash_info[i].flash_id == FLASH_UNKNOWN) {
103                 if(i==1) {
104                     printf ("## Unknown FLASH at %08lx: Size = 0x%08lx = %ld MB\n",
105                             base, size_b1, size_b1<<20);
106                 }
107                 break;
108             }
109             size_b1+=size;
110             base+=size;
111         }
112
113 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
114         /* monitor protection ON by default */
115         flash_protect(FLAG_PROTECT_SET,
116                       CONFIG_SYS_MONITOR_BASE,
117                       CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
118                       flash_get_info(CONFIG_SYS_MONITOR_BASE));
119 #endif
120
121 #ifdef  CONFIG_ENV_IS_IN_FLASH
122         /* ENV protection ON by default */
123         flash_protect(FLAG_PROTECT_SET,
124                       CONFIG_ENV_ADDR,
125                       CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1,
126                       flash_get_info(CONFIG_ENV_ADDR));
127 #endif
128
129         flash_size = size_b0 + size_b1;
130         return flash_size;
131 }
132
133 /*-----------------------------------------------------------------------
134  */
135 static void
136 flash_get_offsets (ulong base, flash_info_t *info)
137 {
138         int i;
139         int sector_size;
140
141         if(!info->sector_count) return;
142
143         /* set up sector start address table */
144         switch(info->flash_id & FLASH_TYPEMASK) {
145             case FLASH_AM040:
146             case FLASH_28F128J3A:
147             case FLASH_28F640J3A:
148             case FLASH_RAM:
149                 /* this chip has uniformly spaced sectors */
150                 sector_size=info->size/info->sector_count;
151                 for (i = 0; i < info->sector_count; i++)
152                         info->start[i] = base + (i * sector_size);
153                 break;
154             default:
155                 if (info->flash_id & FLASH_BTYPE) {
156                     /* set sector offsets for bottom boot block type    */
157                     info->start[0] = base + 0x00000000;
158                     info->start[1] = base + 0x00008000;
159                     info->start[2] = base + 0x0000C000;
160                     info->start[3] = base + 0x00010000;
161                     for (i = 4; i < info->sector_count; i++) {
162                             info->start[i] = base + (i * 0x00020000) - 0x00060000;
163                     }
164                 } else {
165                     /* set sector offsets for top boot block type               */
166                     i = info->sector_count - 1;
167                     info->start[i--] = base + info->size - 0x00008000;
168                     info->start[i--] = base + info->size - 0x0000C000;
169                     info->start[i--] = base + info->size - 0x00010000;
170                     for (; i >= 0; i--) {
171                             info->start[i] = base + i * 0x00020000;
172                     }
173                 }
174         }
175 }
176
177 /*-----------------------------------------------------------------------
178  */
179
180 flash_info_t *flash_get_info(ulong base)
181 {
182         int i;
183         flash_info_t * info;
184
185         for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i ++) {
186                 info = & flash_info[i];
187                 if (info->start[0] <= base && base <= info->start[0] + info->size - 1)
188                         break;
189         }
190
191         return i == CONFIG_SYS_MAX_FLASH_BANKS ? 0 : info;
192 }
193
194 /*-----------------------------------------------------------------------
195  */
196 void
197 flash_print_info  (flash_info_t *info)
198 {
199         int i;
200
201         if (info->flash_id == FLASH_UNKNOWN) {
202                 printf ("missing or unknown FLASH type\n");
203                 return;
204         }
205
206         switch (info->flash_id & FLASH_VENDMASK) {
207         case FLASH_MAN_AMD:     printf ("AMD ");                break;
208         case FLASH_MAN_FUJ:     printf ("FUJITSU ");            break;
209         case FLASH_MAN_INTEL:   printf ("INTEL ");              break;
210         default:                printf ("Unknown Vendor ");     break;
211         }
212
213         switch (info->flash_id & FLASH_TYPEMASK) {
214         case FLASH_AM040:
215                 printf ("AM29LV040B (4 Mbit, bottom boot sect)\n");
216                 break;
217         case FLASH_AM400B:
218                 printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
219                 break;
220         case FLASH_AM400T:
221                 printf ("AM29LV400T (4 Mbit, top boot sector)\n");
222                 break;
223         case FLASH_AM800B:
224                 printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
225                 break;
226         case FLASH_AM800T:
227                 printf ("AM29LV800T (8 Mbit, top boot sector)\n");
228                 break;
229         case FLASH_AM160B:
230                 printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
231                 break;
232         case FLASH_AM160T:
233                 printf ("AM29LV160T (16 Mbit, top boot sector)\n");
234                 break;
235         case FLASH_AM320B:
236                 printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
237                 break;
238         case FLASH_AM320T:
239                 printf ("AM29LV320T (32 Mbit, top boot sector)\n");
240                 break;
241         case FLASH_28F640J3A:
242                 printf ("28F640J3A (64 Mbit)\n");
243                 break;
244         case FLASH_28F128J3A:
245                 printf ("28F128J3A (128 Mbit)\n");
246                 break;
247         case FLASH_ROM:
248                 printf ("ROM\n");
249                 break;
250         case FLASH_RAM:
251                 printf ("RAM\n");
252                 break;
253         default:
254                 printf ("Unknown Chip Type\n");
255                 break;
256         }
257
258         puts ("  Size: ");
259         print_size (info->size, "");
260         printf (" in %d Sectors\n", info->sector_count);
261
262         printf ("  Sector Start Addresses:");
263         for (i=0; i<info->sector_count; ++i) {
264                 if ((i % 5) == 0)
265                         printf ("\n   ");
266                 printf (" %08lX%s",
267                         info->start[i],
268                         info->protect[i] ? " (RO)" : "     "
269                 );
270         }
271         printf ("\n");
272         return;
273 }
274
275 /*-----------------------------------------------------------------------
276  */
277
278
279 /*-----------------------------------------------------------------------
280  */
281
282 /*
283  * The following code cannot be run from FLASH!
284  */
285
286 static inline void flash_cmd(int width, volatile unsigned char *addr, int offset, unsigned char cmd)
287 {
288         /* supports 1x8, 1x16, and 2x16 */
289         /* 2x8 and 4x8 are not supported */
290         if(width==4) {
291             /* assuming chips are in 16 bit mode */
292             /* 2x16 */
293             unsigned long cmd32=(cmd<<16)|cmd;
294             *(volatile unsigned long *)(addr+offset*2)=cmd32;
295         } else if (width == 2) {
296             /* 1x16 */
297             *(volatile unsigned short *)((unsigned short*)addr+offset)=cmd;
298         } else {
299             /* 1x8 */
300             *(volatile unsigned char *)(addr+offset)=cmd;
301         }
302 }
303
304 static ulong
305 flash_get_size (int portwidth, vu_long *addr, flash_info_t *info)
306 {
307         short i;
308         volatile unsigned char *caddr = (unsigned char *)addr;
309         volatile unsigned short *saddr = (unsigned short *)addr;
310         volatile unsigned long *laddr = (unsigned long *)addr;
311         char old[2], save;
312         ulong id, manu, base = (ulong)addr;
313
314         info->portwidth=portwidth;
315
316         save = *caddr;
317
318         flash_cmd(portwidth,caddr,0,0xf0);
319         flash_cmd(portwidth,caddr,0,0xf0);
320
321         udelay(10);
322
323         old[0] = caddr[0];
324         old[1] = caddr[1];
325
326
327         if(old[0]!=0xf0) {
328             flash_cmd(portwidth,caddr,0,0xf0);
329             flash_cmd(portwidth,caddr,0,0xf0);
330
331             udelay(10);
332
333             if(*caddr==0xf0) {
334                 /* this area is ROM */
335                 *caddr=save;
336 #ifndef FLASH_ID_OVERRIDE
337                 info->flash_id = FLASH_ROM + FLASH_MAN_UNKNOWN;
338                 info->sector_count = 8;
339                 info->size = 0x80000;
340 #else
341                 info->flash_id = FLASH_MAN_AMD + FLASH_AM040;
342                 info->sector_count = 8;
343                 info->size = 0x80000;
344                 info->chipwidth=1;
345 #endif
346                 flash_get_offsets(base, info);
347                 return info->size;
348             }
349         } else {
350             *caddr=0;
351
352             udelay(10);
353
354             if(*caddr==0) {
355                 /* this area is RAM */
356                 *caddr=save;
357                 info->flash_id = FLASH_RAM + FLASH_MAN_UNKNOWN;
358                 info->sector_count = 8;
359                 info->size = 0x80000;
360                 flash_get_offsets(base, info);
361                 return info->size;
362             }
363             flash_cmd(portwidth,caddr,0,0xf0);
364
365             udelay(10);
366         }
367
368         /* Write auto select command: read Manufacturer ID */
369         flash_cmd(portwidth,caddr,0x555,0xAA);
370         flash_cmd(portwidth,caddr,0x2AA,0x55);
371         flash_cmd(portwidth,caddr,0x555,0x90);
372
373         udelay(10);
374
375         if ((caddr[0] == old[0]) &&
376             (caddr[1] == old[1])) {
377
378             /* this area is ROM */
379 #ifndef FLASH_ID_OVERRIDE
380             info->flash_id = FLASH_ROM + FLASH_MAN_UNKNOWN;
381             info->sector_count = 8;
382             info->size = 0x80000;
383 #else
384                 info->flash_id = FLASH_MAN_AMD + FLASH_AM040;
385                 info->sector_count = 8;
386                 info->size = 0x80000;
387                 info->chipwidth=1;
388 #endif
389             flash_get_offsets(base, info);
390             return info->size;
391 #ifdef DEBUG
392         } else {
393             printf("%px%d: %02x:%02x -> %02x:%02x\n",
394                     caddr, portwidth, old[0], old[1],
395                     caddr[0], caddr[1]);
396 #endif
397         }
398
399         switch(portwidth) {
400             case 1:
401                 manu = caddr[0];
402                 manu |= manu<<16;
403                 id = caddr[1];
404                 break;
405             case 2:
406                 manu = saddr[0];
407                 manu |= manu<<16;
408                 id = saddr[1];
409                 id |= id<<16;
410                 break;
411             case 4:
412                 manu = laddr[0];
413                 id = laddr[1];
414                 break;
415         default:
416                 id = manu = -1;
417                 break;
418         }
419
420 #ifdef DEBUG
421         printf("\n%08lx:%08lx:%08lx\n", base, manu, id);
422         printf("%08lx %08lx %08lx %08lx\n",
423                 laddr[0],laddr[1],laddr[2],laddr[3]);
424 #endif
425
426         switch (manu) {
427             case AMD_MANUFACT:
428                     info->flash_id = FLASH_MAN_AMD;
429                     break;
430             case FUJ_MANUFACT:
431                     info->flash_id = FLASH_MAN_FUJ;
432                     break;
433             case INTEL_MANUFACT:
434                     info->flash_id = FLASH_MAN_INTEL;
435                     break;
436             default:
437                     printf("Unknown Mfr [%08lx]:%08lx\n", manu, id);
438                     info->flash_id = FLASH_UNKNOWN;
439                     info->sector_count = 0;
440                     info->size = 0;
441                     return (0);                 /* no or unknown flash  */
442         }
443
444         switch (id) {
445             case AMD_ID_LV400T:
446                     info->flash_id += FLASH_AM400T;
447                     info->sector_count = 11;
448                     info->size = 0x00100000;
449                     info->chipwidth=1;
450                     break;                              /* => 1 MB      */
451
452             case AMD_ID_LV400B:
453                     info->flash_id += FLASH_AM400B;
454                     info->sector_count = 11;
455                     info->size = 0x00100000;
456                     info->chipwidth=1;
457                     break;                              /* => 1 MB      */
458
459             case AMD_ID_LV800T:
460                     info->flash_id += FLASH_AM800T;
461                     info->sector_count = 19;
462                     info->size = 0x00200000;
463                     info->chipwidth=1;
464                     break;                              /* => 2 MB      */
465
466             case AMD_ID_LV800B:
467                     info->flash_id += FLASH_AM800B;
468                     info->sector_count = 19;
469                     info->size = 0x00200000;
470                     info->chipwidth=1;
471                     break;                              /* => 2 MB      */
472
473             case AMD_ID_LV160T:
474                     info->flash_id += FLASH_AM160T;
475                     info->sector_count = 35;
476                     info->size = 0x00400000;
477                     info->chipwidth=1;
478                     break;                              /* => 4 MB      */
479
480             case AMD_ID_LV160B:
481                     info->flash_id += FLASH_AM160B;
482                     info->sector_count = 35;
483                     info->size = 0x00400000;
484                     info->chipwidth=1;
485                     break;                              /* => 4 MB      */
486 #if 0   /* enable when device IDs are available */
487             case AMD_ID_LV320T:
488                     info->flash_id += FLASH_AM320T;
489                     info->sector_count = 67;
490                     info->size = 0x00800000;
491                     break;                              /* => 8 MB      */
492
493             case AMD_ID_LV320B:
494                     info->flash_id += FLASH_AM320B;
495                     info->sector_count = 67;
496                     info->size = 0x00800000;
497                     break;                              /* => 8 MB      */
498 #endif
499             case AMD_ID_LV040B:
500                     info->flash_id += FLASH_AM040;
501                     info->sector_count = 8;
502                     info->size = 0x80000;
503                     info->chipwidth=1;
504                     break;
505
506             case INTEL_ID_28F640J3A:
507                     info->flash_id += FLASH_28F640J3A;
508                     info->sector_count = 64;
509                     info->size = 128*1024 * 64; /* 128kbytes x 64 blocks */
510                     info->chipwidth=2;
511                     if(portwidth==4) info->size*=2;     /* 2x16 */
512                     break;
513
514             case INTEL_ID_28F128J3A:
515                     info->flash_id += FLASH_28F128J3A;
516                     info->sector_count = 128;
517                     info->size = 128*1024 * 128; /* 128kbytes x 128 blocks */
518                     info->chipwidth=2;
519                     if(portwidth==4) info->size*=2;     /* 2x16 */
520                     break;
521
522             default:
523                     printf("Unknown id %lx:[%lx]\n", manu, id);
524                     info->flash_id = FLASH_UNKNOWN;
525                     info->chipwidth=1;
526                     return (0);                 /* => no or unknown flash */
527
528         }
529
530         flash_get_offsets(base, info);
531
532 #if 0
533         /* set up sector start address table */
534         if (info->flash_id & FLASH_AM040) {
535                 /* this chip has uniformly spaced sectors */
536                 for (i = 0; i < info->sector_count; i++)
537                         info->start[i] = base + (i * 0x00010000);
538
539         } else if (info->flash_id & FLASH_BTYPE) {
540                 /* set sector offsets for bottom boot block type        */
541                 info->start[0] = base + 0x00000000;
542                 info->start[1] = base + 0x00008000;
543                 info->start[2] = base + 0x0000C000;
544                 info->start[3] = base + 0x00010000;
545                 for (i = 4; i < info->sector_count; i++) {
546                         info->start[i] = base + (i * 0x00020000) - 0x00060000;
547                 }
548         } else {
549                 /* set sector offsets for top boot block type           */
550                 i = info->sector_count - 1;
551                 info->start[i--] = base + info->size - 0x00008000;
552                 info->start[i--] = base + info->size - 0x0000C000;
553                 info->start[i--] = base + info->size - 0x00010000;
554                 for (; i >= 0; i--) {
555                         info->start[i] = base + i * 0x00020000;
556                 }
557         }
558 #endif
559
560         /* check for protected sectors */
561         for (i = 0; i < info->sector_count; i++) {
562                 /* read sector protection at sector address, (A7 .. A0)=0x02 */
563                 /* D0 = 1 if protected */
564                 caddr = (volatile unsigned char *)(info->start[i]);
565                 saddr = (volatile unsigned short *)(info->start[i]);
566                 laddr = (volatile unsigned long *)(info->start[i]);
567                 if(portwidth==1)
568                     info->protect[i] = caddr[2] & 1;
569                 else if(portwidth==2)
570                     info->protect[i] = saddr[2] & 1;
571                 else
572                     info->protect[i] = laddr[2] & 1;
573         }
574
575         /*
576          * Prevent writes to uninitialized FLASH.
577          */
578         if (info->flash_id != FLASH_UNKNOWN) {
579                 caddr = (volatile unsigned char *)info->start[0];
580
581                 flash_cmd(portwidth,caddr,0,0xF0);      /* reset bank */
582         }
583
584         return (info->size);
585 }
586
587 /* TODO: 2x16 unsupported */
588 int
589 flash_erase (flash_info_t *info, int s_first, int s_last)
590 {
591         volatile unsigned char *addr = (uchar *)(info->start[0]);
592         int flag, prot, sect, l_sect;
593         ulong start, now, last;
594
595         /* TODO: 2x16 unsupported */
596         if(info->portwidth==4) return 1;
597
598         if((info->flash_id & FLASH_TYPEMASK) == FLASH_ROM) return 1;
599         if((info->flash_id & FLASH_TYPEMASK) == FLASH_RAM) {
600             for (sect = s_first; sect<=s_last; sect++) {
601                 int sector_size=info->size/info->sector_count;
602                 addr = (uchar *)(info->start[sect]);
603                 memset((void *)addr, 0, sector_size);
604             }
605             return 0;
606         }
607
608         if ((s_first < 0) || (s_first > s_last)) {
609                 if (info->flash_id == FLASH_UNKNOWN) {
610                         printf ("- missing\n");
611                 } else {
612                         printf ("- no sectors to erase\n");
613                 }
614                 return 1;
615         }
616
617         if ((info->flash_id&FLASH_VENDMASK) == FLASH_MAN_INTEL)  {
618                 return flash_erase_intel(info,
619                                 (unsigned short)s_first,
620                                 (unsigned short)s_last);
621         }
622
623 #if 0
624         if ((info->flash_id == FLASH_UNKNOWN) ||
625             (info->flash_id > FLASH_AMD_COMP)) {
626                 printf ("Can't erase unknown flash type %08lx - aborted\n",
627                         info->flash_id);
628                 return 1;
629         }
630 #endif
631
632         prot = 0;
633         for (sect=s_first; sect<=s_last; ++sect) {
634                 if (info->protect[sect]) {
635                         prot++;
636                 }
637         }
638
639         if (prot) {
640                 printf ("- Warning: %d protected sectors will not be erased!\n",
641                         prot);
642         } else {
643                 printf ("\n");
644         }
645
646         l_sect = -1;
647
648         /* Disable interrupts which might cause a timeout here */
649         flag = disable_interrupts();
650
651         flash_cmd(info->portwidth,addr,0x555,0xAA);
652         flash_cmd(info->portwidth,addr,0x2AA,0x55);
653         flash_cmd(info->portwidth,addr,0x555,0x80);
654         flash_cmd(info->portwidth,addr,0x555,0xAA);
655         flash_cmd(info->portwidth,addr,0x2AA,0x55);
656
657         /* Start erase on unprotected sectors */
658         for (sect = s_first; sect<=s_last; sect++) {
659                 if (info->protect[sect] == 0) { /* not protected */
660                         addr = (uchar *)(info->start[sect]);
661                         flash_cmd(info->portwidth,addr,0,0x30);
662                         l_sect = sect;
663                 }
664         }
665
666         /* re-enable interrupts if necessary */
667         if (flag)
668                 enable_interrupts();
669
670         /* wait at least 80us - let's wait 1 ms */
671         udelay (1000);
672
673         /*
674          * We wait for the last triggered sector
675          */
676         if (l_sect < 0)
677                 goto DONE;
678
679         start = get_timer (0);
680         last  = start;
681         addr = (volatile unsigned char *)(info->start[l_sect]);
682         /* broken for 2x16: TODO */
683         while ((addr[0] & 0x80) != 0x80) {
684                 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
685                         printf ("Timeout\n");
686                         return 1;
687                 }
688                 /* show that we're waiting */
689                 if ((now - last) > 1000) {      /* every second */
690                         putc ('.');
691                         last = now;
692                 }
693         }
694
695 DONE:
696         /* reset to read mode */
697         addr = (volatile unsigned char *)info->start[0];
698         flash_cmd(info->portwidth,addr,0,0xf0);
699         flash_cmd(info->portwidth,addr,0,0xf0);
700
701         printf (" done\n");
702         return 0;
703 }
704
705 /*-----------------------------------------------------------------------
706  * Copy memory to flash, returns:
707  * 0 - OK
708  * 1 - write timeout
709  * 2 - Flash not erased
710  */
711
712 /* broken for 2x16: TODO */
713 int
714 write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
715 {
716         ulong cp, wp, data;
717         int i, l, rc;
718
719         if(info->portwidth==4) return 1;
720
721         if((info->flash_id & FLASH_TYPEMASK) == FLASH_ROM) return 0;
722         if((info->flash_id & FLASH_TYPEMASK) == FLASH_RAM) {
723             memcpy((void *)addr, src, cnt);
724             return 0;
725         }
726
727         wp = (addr & ~3);       /* get lower word aligned address */
728
729         /*
730          * handle unaligned start bytes
731          */
732         if ((l = addr - wp) != 0) {
733                 data = 0;
734                 for (i=0, cp=wp; i<l; ++i, ++cp) {
735                         data = (data << 8) | (*(uchar *)cp);
736                 }
737                 for (; i<4 && cnt>0; ++i) {
738                         data = (data << 8) | *src++;
739                         --cnt;
740                         ++cp;
741                 }
742                 for (; cnt==0 && i<4; ++i, ++cp) {
743                         data = (data << 8) | (*(uchar *)cp);
744                 }
745
746                 if ((rc = write_word(info, wp, data)) != 0) {
747                         return (rc);
748                 }
749                 wp += 4;
750         }
751
752         /*
753          * handle word aligned part
754          */
755         while (cnt >= 4) {
756                 data = 0;
757                 for (i=0; i<4; ++i) {
758                         data = (data << 8) | *src++;
759                 }
760                 if ((rc = write_word(info, wp, data)) != 0) {
761                         return (rc);
762                 }
763                 wp  += 4;
764                 cnt -= 4;
765         }
766
767         if (cnt == 0) {
768                 return (0);
769         }
770
771         /*
772          * handle unaligned tail bytes
773          */
774         data = 0;
775         for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
776                 data = (data << 8) | *src++;
777                 --cnt;
778         }
779         for (; i<4; ++i, ++cp) {
780                 data = (data << 8) | (*(uchar *)cp);
781         }
782
783         return (write_word(info, wp, data));
784 }
785
786 /*-----------------------------------------------------------------------
787  * Write a word to Flash, returns:
788  * 0 - OK
789  * 1 - write timeout
790  * 2 - Flash not erased
791  */
792 /* broken for 2x16: TODO */
793 static int
794 write_word (flash_info_t *info, ulong dest, ulong data)
795 {
796         volatile unsigned char *addr = (uchar *)(info->start[0]);
797         ulong start;
798         int flag, i;
799
800         if(info->portwidth==4) return 1;
801
802         if((info->flash_id & FLASH_TYPEMASK) == FLASH_ROM) return 1;
803         if((info->flash_id & FLASH_TYPEMASK) == FLASH_RAM) {
804             *(unsigned long *)dest=data;
805             return 0;
806         }
807         if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL)  {
808                 unsigned short low = data & 0xffff;
809                 unsigned short hi  = (data >> 16) & 0xffff;
810                 int ret = write_word_intel((bank_addr_t)dest, hi);
811
812                 if (!ret) ret = write_word_intel((bank_addr_t)(dest+2), low);
813
814                 return ret;
815         }
816
817         /* Check if Flash is (sufficiently) erased */
818         if ((*((vu_long *)dest) & data) != data) {
819                 return (2);
820         }
821         /* Disable interrupts which might cause a timeout here */
822         flag = disable_interrupts();
823
824         /* first, perform an unlock bypass command to speed up flash writes */
825         addr[0x555] = 0xAA;
826         addr[0x2AA] = 0x55;
827         addr[0x555] = 0x20;
828
829         /* write each byte out */
830         for (i = 0; i < 4; i++) {
831                 char *data_ch = (char *)&data;
832                 addr[0] = 0xA0;
833                 *(((char *)dest)+i) = data_ch[i];
834                 udelay(10); /* XXX */
835         }
836
837         /* we're done, now do an unlock bypass reset */
838         addr[0] = 0x90;
839         addr[0] = 0x00;
840
841         /* re-enable interrupts if necessary */
842         if (flag)
843                 enable_interrupts();
844
845         /* data polling for D7 */
846         start = get_timer (0);
847         while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
848                 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
849                         return (1);
850                 }
851         }
852         return (0);
853 }