]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/etx094/flash.c
c1bd13c29cc798c29d71233564955898a51779d9
[karo-tx-uboot.git] / board / etx094 / flash.c
1 /*
2  * (C) Copyright 2000
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23
24 #include <common.h>
25 #include <mpc8xx.h>
26
27 flash_info_t    flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
28
29 /*-----------------------------------------------------------------------
30  * Functions
31  */
32 static ulong flash_get_size(vu_long *addr, flash_info_t *info);
33 static int write_word(flash_info_t *info, ulong dest, ulong data);
34 static void flash_get_offsets(ulong base, flash_info_t *info);
35
36 /*-----------------------------------------------------------------------
37  */
38
39 unsigned long flash_init(void)
40 {
41         volatile immap_t     *immap  = (immap_t *)CONFIG_SYS_IMMR;
42         volatile memctl8xx_t *memctl = &immap->im_memctl;
43         unsigned long size_b0, size_b1;
44         int i;
45
46         /* Init: no FLASHes known */
47         for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i)
48                 flash_info[i].flash_id = FLASH_UNKNOWN;
49
50         /* Static FLASH Bank configuration here - FIXME XXX */
51
52         size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
53
54         if (flash_info[0].flash_id == FLASH_UNKNOWN) {
55                 printf("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
56                         size_b0, size_b0<<20);
57         }
58
59         size_b1 = flash_get_size((vu_long *)FLASH_BASE1_PRELIM, &flash_info[1]);
60
61         if (size_b1 > size_b0) {
62                 printf("## ERROR: "
63                         "Bank 1 (0x%08lx = %ld MB) > Bank 0 (0x%08lx = %ld MB)\n",
64                         size_b1, size_b1<<20,
65                         size_b0, size_b0<<20
66                 );
67                 flash_info[0].flash_id  = FLASH_UNKNOWN;
68                 flash_info[1].flash_id  = FLASH_UNKNOWN;
69                 flash_info[0].sector_count      = -1;
70                 flash_info[1].sector_count      = -1;
71                 flash_info[0].size              = 0;
72                 flash_info[1].size              = 0;
73                 return 0;
74         }
75
76         /* Remap FLASH according to real size */
77         memctl->memc_or0 = CONFIG_SYS_OR_TIMING_FLASH | (-size_b0 & 0xFFFF8000);
78 #ifdef CONFIG_FLASH_16BIT
79         memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) |
80                 BR_MS_GPCM | BR_V | BR_PS_16; /* 16 Bit data port */
81 #else
82         memctl->memc_br0 = (CONFIG_SYS_FLASH_BASE & BR_BA_MSK) |
83                 BR_MS_GPCM | BR_V;
84 #endif
85
86         /* Re-do sizing to get full correct info */
87         size_b0 = flash_get_size((vu_long *)CONFIG_SYS_FLASH_BASE,
88                         &flash_info[0]);
89
90         flash_get_offsets(CONFIG_SYS_FLASH_BASE, &flash_info[0]);
91
92 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
93         /* monitor protection ON by default */
94         flash_protect(FLAG_PROTECT_SET,
95                       CONFIG_SYS_MONITOR_BASE,
96                       CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
97                       &flash_info[0]);
98 #endif
99
100         if (size_b1) {
101                 memctl->memc_or1 = CONFIG_SYS_OR_TIMING_FLASH |
102                         (-size_b1 & 0xFFFF8000);
103 #ifdef CONFIG_FLASH_16BIT
104                 memctl->memc_br1 = ((CONFIG_SYS_FLASH_BASE + size_b0) &
105                         BR_BA_MSK) | BR_MS_GPCM | BR_V | BR_PS_16;
106 #else
107                 memctl->memc_br1 = ((CONFIG_SYS_FLASH_BASE + size_b0) &
108                         BR_BA_MSK) | BR_MS_GPCM | BR_V;
109 #endif
110
111                 /* Re-do sizing to get full correct info */
112                 size_b1 = flash_get_size((vu_long *)(CONFIG_SYS_FLASH_BASE +
113                                 size_b0), &flash_info[1]);
114
115                 flash_get_offsets(CONFIG_SYS_FLASH_BASE + size_b0,
116                                 &flash_info[1]);
117
118 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
119                 /* monitor protection ON by default */
120                 flash_protect(FLAG_PROTECT_SET,
121                               CONFIG_SYS_MONITOR_BASE,
122                               CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
123                               &flash_info[1]);
124 #endif
125         } else {
126                 memctl->memc_br1 = 0;           /* invalidate bank */
127
128                 flash_info[1].flash_id = FLASH_UNKNOWN;
129                 flash_info[1].sector_count = -1;
130         }
131
132         flash_info[0].size = size_b0;
133         flash_info[1].size = size_b1;
134
135         return size_b0 + size_b1;
136 }
137
138 /*-----------------------------------------------------------------------
139  */
140 static void flash_get_offsets(ulong base, flash_info_t *info)
141 {
142         int i;
143
144         if (info->flash_id == FLASH_UNKNOWN)
145                 return;
146
147         if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
148                 for (i = 0; i < info->sector_count; i++)
149                         info->start[i] = base + (i * 0x00002000);
150
151                 return;
152         }
153
154         /* set up sector start address table */
155         if (info->flash_id & FLASH_BTYPE) {
156                 /* set sector offsets for bottom boot block type        */
157 #ifdef CONFIG_FLASH_16BIT
158                 info->start[0] = base + 0x00000000;
159                 info->start[1] = base + 0x00004000;
160                 info->start[2] = base + 0x00006000;
161                 info->start[3] = base + 0x00008000;
162                 for (i = 4; i < info->sector_count; i++)
163                         info->start[i] = base + (i * 0x00010000) - 0x00030000;
164 #else
165                 info->start[0] = base + 0x00000000;
166                 info->start[1] = base + 0x00008000;
167                 info->start[2] = base + 0x0000C000;
168                 info->start[3] = base + 0x00010000;
169                 for (i = 4; i < info->sector_count; i++)
170                         info->start[i] = base + (i * 0x00020000) - 0x00060000;
171 #endif
172         } else {
173                 /* set sector offsets for top boot block type           */
174                 i = info->sector_count - 1;
175                 info->start[i--] = base + info->size - 0x00008000;
176                 info->start[i--] = base + info->size - 0x0000C000;
177                 info->start[i--] = base + info->size - 0x00010000;
178                 for (; i >= 0; i--)
179                         info->start[i] = base + i * 0x00020000;
180         }
181 }
182
183 /*-----------------------------------------------------------------------
184  */
185 void flash_print_info(flash_info_t *info)
186 {
187         int i;
188
189         if (info->flash_id == FLASH_UNKNOWN) {
190                 printf("missing or unknown FLASH type\n");
191                 return;
192         }
193
194         switch (info->flash_id & FLASH_VENDMASK) {
195         case FLASH_MAN_AMD:
196                 printf("AMD ");
197                 break;
198         case FLASH_MAN_FUJ:
199                 printf("FUJITSU ");
200                 break;
201         case FLASH_MAN_SST:
202                 printf("SST ");
203                 break;
204         case FLASH_MAN_STM:
205                 printf("STM ");
206                 break;
207         default:
208                 printf("Unknown Vendor ");
209                 break;
210         }
211
212         switch (info->flash_id & FLASH_TYPEMASK) {
213         case FLASH_AM400B:
214                 printf("AM29LV400B (4 Mbit, bottom boot sect)\n");
215                 break;
216         case FLASH_AM400T:
217                 printf("AM29LV400T (4 Mbit, top boot sector)\n");
218                 break;
219         case FLASH_AM800B:
220                 printf("AM29LV800B (8 Mbit, bottom boot sect)\n");
221                 break;
222         case FLASH_AM800T:
223                 printf("AM29LV800T (8 Mbit, top boot sector)\n");
224                 break;
225         case FLASH_AM160B:
226                 printf("AM29LV160B (16 Mbit, bottom boot sect)\n");
227                 break;
228         case FLASH_AM160T:
229                 printf("AM29LV160T (16 Mbit, top boot sector)\n");
230                 break;
231         case FLASH_AM320B:
232                 printf("AM29LV320B (32 Mbit, bottom boot sect)\n");
233                 break;
234         case FLASH_AM320T:
235                 printf("AM29LV320T (32 Mbit, top boot sector)\n");
236                 break;
237         case FLASH_SST200A:
238                 printf("39xF200A (2M = 128K x 16)\n");
239                 break;
240         case FLASH_SST400A:
241                 printf("39xF400A (4M = 256K x 16)\n");
242                 break;
243         case FLASH_SST800A:
244                 printf("39xF800A (8M = 512K x 16)\n");
245                 break;
246         case FLASH_STM800AB:
247                 printf("M29W800AB (8M = 512K x 16)\n");
248                 break;
249         default:
250                 printf("Unknown Chip Type\n");
251                 break;
252         }
253
254         printf("  Size: %ld MB in %d Sectors\n",
255                 info->size >> 20, info->sector_count);
256
257         printf("  Sector Start Addresses:");
258         for (i = 0; i < info->sector_count; ++i) {
259                 if ((i % 5) == 0)
260                         printf("\n   ");
261                 printf(" %08lX%s",
262                         info->start[i],
263                         info->protect[i] ? " (RO)" : "     "
264                 );
265         }
266         printf("\n");
267         return;
268 }
269
270 /*
271  * The following code cannot be run from FLASH!
272  */
273
274 static ulong flash_get_size(vu_long *addr, flash_info_t *info)
275 {
276         short i;
277         ulong value;
278         ulong base = (ulong)addr;
279
280         /* Write auto select command: read Manufacturer ID */
281 #ifdef CONFIG_FLASH_16BIT
282         vu_short *s_addr = (vu_short *)addr;
283         s_addr[0x5555] = 0x00AA;
284         s_addr[0x2AAA] = 0x0055;
285         s_addr[0x5555] = 0x0090;
286         value = s_addr[0];
287         value = value|(value<<16);
288 #else
289         addr[0x5555] = 0x00AA00AA;
290         addr[0x2AAA] = 0x00550055;
291         addr[0x5555] = 0x00900090;
292         value = addr[0];
293 #endif
294
295         switch (value) {
296         case AMD_MANUFACT:
297                 info->flash_id = FLASH_MAN_AMD;
298                 break;
299         case FUJ_MANUFACT:
300                 info->flash_id = FLASH_MAN_FUJ;
301                 break;
302         case SST_MANUFACT:
303                 info->flash_id = FLASH_MAN_SST;
304                 break;
305         case STM_MANUFACT:
306                 info->flash_id = FLASH_MAN_STM;
307                 break;
308         default:
309                 info->flash_id = FLASH_UNKNOWN;
310                 info->sector_count = 0;
311                 info->size = 0;
312                 return 0;                       /* no or unknown flash  */
313         }
314 #ifdef CONFIG_FLASH_16BIT
315         value = s_addr[1];
316         value = value|(value<<16);
317 #else
318         value = addr[1];                        /* device ID            */
319 #endif
320
321         switch (value) {
322         case AMD_ID_LV400T:
323                 info->flash_id += FLASH_AM400T;
324                 info->sector_count = 11;
325                 info->size = 0x00100000;
326                 break;                          /* => 1 MB              */
327
328         case AMD_ID_LV400B:
329                 info->flash_id += FLASH_AM400B;
330                 info->sector_count = 11;
331                 info->size = 0x00100000;
332                 break;                          /* => 1 MB              */
333
334         case AMD_ID_LV800T:
335                 info->flash_id += FLASH_AM800T;
336                 info->sector_count = 19;
337                 info->size = 0x00200000;
338                 break;                          /* => 2 MB              */
339
340         case AMD_ID_LV800B:
341                 info->flash_id += FLASH_AM800B;
342 #ifdef CONFIG_FLASH_16BIT
343                 info->sector_count = 19;
344                 info->size = 0x00100000;        /* => 1 MB */
345 #else
346                 info->sector_count = 19;
347                 info->size = 0x00200000;        /* => 2 MB      */
348 #endif
349                 break;
350
351         case AMD_ID_LV160T:
352                 info->flash_id += FLASH_AM160T;
353                 info->sector_count = 35;
354                 info->size = 0x00400000;
355                 break;                          /* => 4 MB              */
356
357         case AMD_ID_LV160B:
358                 info->flash_id += FLASH_AM160B;
359 #ifdef CONFIG_FLASH_16BIT
360                 info->sector_count = 35;
361                 info->size = 0x00200000;        /* => 2 MB      */
362 #else
363                 info->sector_count = 35;
364                 info->size = 0x00400000;        /* => 4 MB      */
365 #endif
366
367                 break;
368         case SST_ID_xF200A:
369                 info->flash_id += FLASH_SST200A;
370                 info->sector_count = 64;        /* 39xF200A (2M = 128K x 16) */
371                 info->size = 0x00080000;
372                 break;
373         case SST_ID_xF400A:
374                 info->flash_id += FLASH_SST400A;
375                 info->sector_count = 128;       /* 39xF400A (4M = 256K x 16) */
376                 info->size = 0x00100000;
377                 break;
378         case SST_ID_xF800A:
379                 info->flash_id += FLASH_SST800A;
380                 info->sector_count = 256;       /* 39xF800A (8M = 512K x 16) */
381                 info->size = 0x00200000;
382                 break;                          /* => 2 MB              */
383         case STM_ID_x800AB:
384                 info->flash_id += FLASH_STM800AB;
385                 info->sector_count = 19;
386                 info->size = 0x00200000;
387                 break;                          /* => 2 MB              */
388         default:
389                 info->flash_id = FLASH_UNKNOWN;
390                 return 0;                       /* => no or unknown flash */
391
392         }
393
394         if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
395                 printf("** ERROR: sector count %d > max (%d) **\n",
396                         info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
397                 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
398         }
399
400         if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) {
401                 for (i = 0; i < info->sector_count; i++)
402                         info->start[i] = base + (i * 0x00002000);
403         } else {        /* AMD and Fujitsu types */
404                 /* set up sector start address table */
405                 if (info->flash_id & FLASH_BTYPE) {
406                         /* set sector offsets for bottom boot block type */
407 #ifdef CONFIG_FLASH_16BIT
408
409                         info->start[0] = base + 0x00000000;
410                         info->start[1] = base + 0x00004000;
411                         info->start[2] = base + 0x00006000;
412                         info->start[3] = base + 0x00008000;
413                         for (i = 4; i < info->sector_count; i++)
414                                 info->start[i] = base +
415                                         (i * 0x00010000) - 0x00030000;
416 #else
417                         info->start[0] = base + 0x00000000;
418                         info->start[1] = base + 0x00008000;
419                         info->start[2] = base + 0x0000C000;
420                         info->start[3] = base + 0x00010000;
421                         for (i = 4; i < info->sector_count; i++)
422                                 info->start[i] = base +
423                                         (i * 0x00020000) - 0x00060000;
424 #endif
425                 } else {
426                         /* set sector offsets for top boot block type   */
427                         i = info->sector_count - 1;
428                         info->start[i--] = base + info->size - 0x00008000;
429                         info->start[i--] = base + info->size - 0x0000C000;
430                         info->start[i--] = base + info->size - 0x00010000;
431                         for (; i >= 0; i--)
432                                 info->start[i] = base + i * 0x00020000;
433                 }
434
435                 /* check for protected sectors */
436                 for (i = 0; i < info->sector_count; i++) {
437                         /*
438                          * read sector protection at sector address:
439                          * (A7 .. A0) = 0x02
440                          * D0 = 1 if protected
441                          */
442 #ifdef CONFIG_FLASH_16BIT
443                         s_addr = (volatile unsigned short *)(info->start[i]);
444                         info->protect[i] = s_addr[2] & 1;
445 #else
446                         addr = (volatile unsigned long *)(info->start[i]);
447                         info->protect[i] = addr[2] & 1;
448 #endif
449                 }
450         }
451
452         /*
453          * Prevent writes to uninitialized FLASH.
454          */
455         if (info->flash_id != FLASH_UNKNOWN) {
456 #ifdef CONFIG_FLASH_16BIT
457                 s_addr = (volatile unsigned short *)(info->start[0]);
458                 *s_addr = 0x00F0;       /* reset bank */
459 #else
460                 addr = (volatile unsigned long *)info->start[0];
461                 *addr = 0x00F000F0;     /* reset bank */
462 #endif
463
464         }
465         return info->size;
466 }
467
468 int     flash_erase(flash_info_t *info, int s_first, int s_last)
469 {
470         vu_long *addr = (vu_long *)(info->start[0]);
471         int flag, prot, sect;
472         ulong start, now, last;
473 #ifdef CONFIG_FLASH_16BIT
474         vu_short *s_addr = (vu_short *)addr;
475 #endif
476
477         if ((s_first < 0) || (s_first > s_last)) {
478                 if (info->flash_id == FLASH_UNKNOWN)
479                         printf("- missing\n");
480                 else
481                         printf("- no sectors to erase\n");
482                 return 1;
483         }
484 /*#ifndef CONFIG_FLASH_16BIT
485         ulong type;
486         type = (info->flash_id & FLASH_VENDMASK);
487         if ((type != FLASH_MAN_SST) && (type != FLASH_MAN_STM)) {
488                 printf ("Can't erase unknown flash type %08lx - aborted\n",
489                         info->flash_id);
490                 return;
491         }
492 #endif*/
493         prot = 0;
494         for (sect = s_first; sect <= s_last; ++sect) {
495                 if (info->protect[sect])
496                         prot++;
497         }
498
499         if (prot) {
500                 printf("- Warning: %d protected sectors will not be erased!\n",
501                         prot);
502         } else {
503                 printf("\n");
504         }
505
506         start = get_timer(0);
507         last  = start;
508         /* Start erase on unprotected sectors */
509         for (sect = s_first; sect <= s_last; sect++) {
510                 if (info->protect[sect] == 0) { /* not protected */
511 #ifdef CONFIG_FLASH_16BIT
512                         vu_short *s_sect_addr = (vu_short *)(info->start[sect]);
513 #else
514                         vu_long *sect_addr = (vu_long *)(info->start[sect]);
515 #endif
516                         /* Disable interrupts which might cause a timeout */
517                         flag = disable_interrupts();
518
519 #ifdef CONFIG_FLASH_16BIT
520
521                         /*printf("\ns_sect_addr=%x",s_sect_addr);*/
522                         s_addr[0x5555] = 0x00AA;
523                         s_addr[0x2AAA] = 0x0055;
524                         s_addr[0x5555] = 0x0080;
525                         s_addr[0x5555] = 0x00AA;
526                         s_addr[0x2AAA] = 0x0055;
527                         s_sect_addr[0] = 0x0030;
528 #else
529                         addr[0x5555] = 0x00AA00AA;
530                         addr[0x2AAA] = 0x00550055;
531                         addr[0x5555] = 0x00800080;
532                         addr[0x5555] = 0x00AA00AA;
533                         addr[0x2AAA] = 0x00550055;
534                         sect_addr[0] = 0x00300030;
535 #endif
536                         /* re-enable interrupts if necessary */
537                         if (flag)
538                                 enable_interrupts();
539
540                         /* wait at least 80us - let's wait 1 ms */
541                         udelay(1000);
542
543 #ifdef CONFIG_FLASH_16BIT
544                         while ((s_sect_addr[0] & 0x0080) != 0x0080) {
545 #else
546                         while ((sect_addr[0] & 0x00800080) != 0x00800080) {
547 #endif
548                                 now = get_timer(start);
549                                 if (now > CONFIG_SYS_FLASH_ERASE_TOUT) {
550                                         printf("Timeout\n");
551                                         return 1;
552                                 }
553                                 /* show every second that we're waiting */
554                                 if ((now - last) > 1000) {
555                                         putc('.');
556                                         last = now;
557                                 }
558                         }
559                 }
560         }
561
562         /* reset to read mode */
563         addr = (volatile unsigned long *)info->start[0];
564 #ifdef CONFIG_FLASH_16BIT
565         s_addr[0] = 0x00F0;     /* reset bank */
566 #else
567         addr[0] = 0x00F000F0;   /* reset bank */
568 #endif
569
570         printf(" done\n");
571         return 0;
572 }
573
574 /*-----------------------------------------------------------------------
575  * Copy memory to flash, returns:
576  * 0 - OK
577  * 1 - write timeout
578  * 2 - Flash not erased
579  * 4 - Flash not identified
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_UNKNOWN)
588                 return 4;
589
590         wp = (addr & ~3);       /* get lower word aligned address */
591
592         /*
593          * handle unaligned start bytes
594          */
595         l = addr - wp;
596
597         if (l != 0) {
598                 data = 0;
599                 for (i = 0, cp = wp; i < l; ++i, ++cp)
600                         data = (data << 8) | (*(uchar *)cp);
601
602                 for (; i < 4 && cnt > 0; ++i) {
603                         data = (data << 8) | *src++;
604                         --cnt;
605                         ++cp;
606                 }
607                 for (; cnt == 0 && i < 4; ++i, ++cp)
608                         data = (data << 8) | (*(uchar *)cp);
609
610                 rc = write_word(info, wp, data);
611
612                 if (rc != 0)
613                         return rc;
614
615                 wp += 4;
616         }
617
618         /*
619          * handle word aligned part
620          */
621         while (cnt >= 4) {
622                 data = 0;
623                 for (i = 0; i < 4; ++i)
624                         data = (data << 8) | *src++;
625
626                 rc = write_word(info, wp, data);
627                 if (rc != 0)
628                         return rc;
629
630                 wp  += 4;
631                 cnt -= 4;
632         }
633
634         if (cnt == 0)
635                 return 0;
636
637         /*
638          * handle unaligned tail bytes
639          */
640         data = 0;
641         for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
642                 data = (data << 8) | *src++;
643                 --cnt;
644         }
645         for (; i < 4; ++i, ++cp)
646                 data = (data << 8) | (*(uchar *)cp);
647
648         return write_word(info, wp, data);
649 }
650
651 /*-----------------------------------------------------------------------
652  * Write a word to Flash, returns:
653  * 0 - OK
654  * 1 - write timeout
655  * 2 - Flash not erased
656  */
657 static int write_word(flash_info_t *info, ulong dest, ulong data)
658 {
659         vu_long *addr = (vu_long *)(info->start[0]);
660
661 #ifdef CONFIG_FLASH_16BIT
662         vu_short high_data;
663         vu_short low_data;
664         vu_short *s_addr = (vu_short *)addr;
665 #endif
666         ulong start;
667         int flag;
668
669         /* Check if Flash is (sufficiently) erased */
670         if ((*((vu_long *)dest) & data) != data)
671                 return 2;
672
673 #ifdef CONFIG_FLASH_16BIT
674         /* Write the 16 higher-bits */
675         /* Disable interrupts which might cause a timeout here */
676         flag = disable_interrupts();
677
678         high_data = ((data>>16) & 0x0000ffff);
679
680         s_addr[0x5555] = 0x00AA;
681         s_addr[0x2AAA] = 0x0055;
682         s_addr[0x5555] = 0x00A0;
683
684         *((vu_short *)dest) = high_data;
685
686         /* re-enable interrupts if necessary */
687         if (flag)
688                 enable_interrupts();
689
690         /* data polling for D7 */
691         start = get_timer(0);
692         while ((*((vu_short *)dest) & 0x0080) != (high_data & 0x0080)) {
693                 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT)
694                         return 1;
695         }
696
697         /* Write the 16 lower-bits */
698 #endif
699
700         /* Disable interrupts which might cause a timeout here */
701         flag = disable_interrupts();
702 #ifdef CONFIG_FLASH_16BIT
703         dest += 0x2;
704         low_data = (data & 0x0000ffff);
705
706         s_addr[0x5555] = 0x00AA;
707         s_addr[0x2AAA] = 0x0055;
708         s_addr[0x5555] = 0x00A0;
709         *((vu_short *)dest) = low_data;
710
711 #else
712         addr[0x5555] = 0x00AA00AA;
713         addr[0x2AAA] = 0x00550055;
714         addr[0x5555] = 0x00A000A0;
715         *((vu_long *)dest) = data;
716 #endif
717
718         /* re-enable interrupts if necessary */
719         if (flag)
720                 enable_interrupts();
721
722         /* data polling for D7 */
723         start = get_timer(0);
724
725 #ifdef CONFIG_FLASH_16BIT
726         while ((*((vu_short *)dest) & 0x0080) != (low_data & 0x0080)) {
727 #else
728         while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
729 #endif
730
731                 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT)
732                         return 1;
733         }
734         return 0;
735 }