]> git.karo-electronics.de Git - karo-tx-uboot.git/blob - board/tb0229/flash.c
MIPS: tb0229: fix compile errors
[karo-tx-uboot.git] / board / tb0229 / flash.c
1 /*
2  * (C) Masami Komiya <mkomiya@sonare.it> 2004
3  *
4  * (C) Copyright 2001-2004
5  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6  *
7  * See file CREDITS for list of people who contributed to this
8  * project.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License as
12  * published by the Free Software Foundation; either version 2 of
13  * the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23  * MA 02111-1307 USA
24  */
25
26 #include <common.h>
27 #include <asm/processor.h>
28
29 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];    /* info for FLASH chips        */
30
31
32 #ifdef CONFIG_SYS_FLASH_16BIT
33 #define FLASH_WORD_SIZE unsigned short
34 #define FLASH_ID_MASK   0xFFFF
35 #else
36 #define FLASH_WORD_SIZE unsigned long
37 #define FLASH_ID_MASK   0xFFFFFFFF
38 #endif
39
40 /*-----------------------------------------------------------------------
41  * Functions
42  */
43 /* stolen from esteem192e/flash.c */
44 ulong flash_get_size (volatile FLASH_WORD_SIZE * addr, flash_info_t * info);
45
46 #ifndef CONFIG_SYS_FLASH_16BIT
47 static int write_word (flash_info_t * info, ulong dest, ulong data);
48 #else
49 static int write_short (flash_info_t * info, ulong dest, ushort data);
50 #endif
51 static void flash_get_offsets (ulong base, flash_info_t * info);
52
53
54 /*-----------------------------------------------------------------------
55  */
56
57 unsigned long flash_init (void)
58 {
59         unsigned long size_b0, size_b1;
60         int i;
61         uint pbcr;
62         unsigned long base_b0, base_b1;
63
64         /* Init: no FLASHes known */
65         for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
66                 flash_info[i].flash_id = FLASH_UNKNOWN;
67         }
68
69         /* Static FLASH Bank configuration here - FIXME XXX */
70
71         size_b0 =
72                 flash_get_size ((volatile FLASH_WORD_SIZE *) CONFIG_SYS_FLASH_BASE,
73                                 &flash_info[0]);
74
75         if (flash_info[0].flash_id == FLASH_UNKNOWN) {
76                 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n", size_b0, size_b0 << 20);
77         }
78
79         /* Only one bank */
80         if (CONFIG_SYS_MAX_FLASH_BANKS == 1) {
81                 /* Setup offsets */
82                 flash_get_offsets (CONFIG_SYS_FLASH_BASE, &flash_info[0]);
83
84                 /* Monitor protection ON by default */
85 #if 0                           /* sand: */
86                 (void) flash_protect (FLAG_PROTECT_SET,
87                                       FLASH_BASE0_PRELIM - monitor_flash_len +
88                                       size_b0,
89                                       FLASH_BASE0_PRELIM - 1 + size_b0,
90                                       &flash_info[0]);
91 #else
92                 (void) flash_protect (FLAG_PROTECT_SET,
93                                       CONFIG_SYS_MONITOR_BASE,
94                                       CONFIG_SYS_MONITOR_BASE + monitor_flash_len -
95                                       1, &flash_info[0]);
96 #endif
97                 size_b1 = 0;
98                 flash_info[0].size = size_b0;
99         }
100 #ifdef CONFIG_SYS_FLASH_BASE_2
101         /* 2 banks */
102         else {
103                 size_b1 =
104                         flash_get_size ((volatile FLASH_WORD_SIZE *)
105                                         CONFIG_SYS_FLASH_BASE_2, &flash_info[1]);
106
107                 /* Re-do sizing to get full correct info */
108
109                 if (size_b1) {
110                         mtdcr (EBC0_CFGADDR, PB0CR);
111                         pbcr = mfdcr (EBC0_CFGDATA);
112                         mtdcr (EBC0_CFGADDR, PB0CR);
113                         base_b1 = -size_b1;
114                         pbcr = (pbcr & 0x0001ffff) | base_b1 |
115                                 (((size_b1 / 1024 / 1024) - 1) << 17);
116                         mtdcr (EBC0_CFGDATA, pbcr);
117                         /*          printf("PB1CR = %x\n", pbcr); */
118                 }
119
120                 if (size_b0) {
121                         mtdcr (EBC0_CFGADDR, PB1CR);
122                         pbcr = mfdcr (EBC0_CFGDATA);
123                         mtdcr (EBC0_CFGADDR, PB1CR);
124                         base_b0 = base_b1 - size_b0;
125                         pbcr = (pbcr & 0x0001ffff) | base_b0 |
126                                 (((size_b0 / 1024 / 1024) - 1) << 17);
127                         mtdcr (EBC0_CFGDATA, pbcr);
128                         /*            printf("PB0CR = %x\n", pbcr); */
129                 }
130
131                 size_b0 =
132                         flash_get_size ((volatile FLASH_WORD_SIZE *) base_b0,
133                                         &flash_info[0]);
134
135                 flash_get_offsets (base_b0, &flash_info[0]);
136
137                 /* monitor protection ON by default */
138 #if 0                           /* sand: */
139                 (void) flash_protect (FLAG_PROTECT_SET,
140                                       FLASH_BASE0_PRELIM - monitor_flash_len +
141                                       size_b0,
142                                       FLASH_BASE0_PRELIM - 1 + size_b0,
143                                       &flash_info[0]);
144 #else
145                 (void) flash_protect (FLAG_PROTECT_SET,
146                                       CONFIG_SYS_MONITOR_BASE,
147                                       CONFIG_SYS_MONITOR_BASE + monitor_flash_len -
148                                       1, &flash_info[0]);
149 #endif
150
151                 if (size_b1) {
152                         /* Re-do sizing to get full correct info */
153                         size_b1 =
154                                 flash_get_size ((volatile FLASH_WORD_SIZE *)
155                                                 base_b1, &flash_info[1]);
156
157                         flash_get_offsets (base_b1, &flash_info[1]);
158
159                         /* monitor protection ON by default */
160                         (void) flash_protect (FLAG_PROTECT_SET,
161                                               base_b1 + size_b1 -
162                                               monitor_flash_len,
163                                               base_b1 + size_b1 - 1,
164                                               &flash_info[1]);
165                         /* monitor protection OFF by default (one is enough) */
166                         (void) flash_protect (FLAG_PROTECT_CLEAR,
167                                               base_b0 + size_b0 -
168                                               monitor_flash_len,
169                                               base_b0 + size_b0 - 1,
170                                               &flash_info[0]);
171                 } else {
172                         flash_info[1].flash_id = FLASH_UNKNOWN;
173                         flash_info[1].sector_count = -1;
174                 }
175
176                 flash_info[0].size = size_b0;
177                 flash_info[1].size = size_b1;
178         }                       /* else 2 banks */
179 #endif
180         return (size_b0 + size_b1);
181 }
182
183
184 /*-----------------------------------------------------------------------
185  */
186
187 static void flash_get_offsets (ulong base, flash_info_t * info)
188 {
189         int i;
190
191         /* set up sector start adress table */
192         if ((info->flash_id & FLASH_TYPEMASK) == FLASH_28F320J3A ||
193             (info->flash_id & FLASH_TYPEMASK) == FLASH_28F640J3A ||
194             (info->flash_id & FLASH_TYPEMASK) == FLASH_28F128J3A) {
195                 for (i = 0; i < info->sector_count; i++) {
196                         info->start[i] =
197                                 base + (i * info->size / info->sector_count);
198                 }
199         } else if (info->flash_id & FLASH_BTYPE) {
200                 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
201
202 #ifndef CONFIG_SYS_FLASH_16BIT
203                         /* set sector offsets for bottom boot block type        */
204                         info->start[0] = base + 0x00000000;
205                         info->start[1] = base + 0x00004000;
206                         info->start[2] = base + 0x00008000;
207                         info->start[3] = base + 0x0000C000;
208                         info->start[4] = base + 0x00010000;
209                         info->start[5] = base + 0x00014000;
210                         info->start[6] = base + 0x00018000;
211                         info->start[7] = base + 0x0001C000;
212                         for (i = 8; i < info->sector_count; i++) {
213                                 info->start[i] =
214                                         base + (i * 0x00020000) - 0x000E0000;
215                         }
216                 } else {
217                         /* set sector offsets for bottom boot block type        */
218                         info->start[0] = base + 0x00000000;
219                         info->start[1] = base + 0x00008000;
220                         info->start[2] = base + 0x0000C000;
221                         info->start[3] = base + 0x00010000;
222                         for (i = 4; i < info->sector_count; i++) {
223                                 info->start[i] =
224                                         base + (i * 0x00020000) - 0x00060000;
225                         }
226                 }
227 #else
228                         /* set sector offsets for bottom boot block type        */
229                         info->start[0] = base + 0x00000000;
230                         info->start[1] = base + 0x00002000;
231                         info->start[2] = base + 0x00004000;
232                         info->start[3] = base + 0x00006000;
233                         info->start[4] = base + 0x00008000;
234                         info->start[5] = base + 0x0000A000;
235                         info->start[6] = base + 0x0000C000;
236                         info->start[7] = base + 0x0000E000;
237                         for (i = 8; i < info->sector_count; i++) {
238                                 info->start[i] =
239                                         base + (i * 0x00010000) - 0x00070000;
240                         }
241                 } else {
242                         /* set sector offsets for bottom boot block type        */
243                         info->start[0] = base + 0x00000000;
244                         info->start[1] = base + 0x00004000;
245                         info->start[2] = base + 0x00006000;
246                         info->start[3] = base + 0x00008000;
247                         for (i = 4; i < info->sector_count; i++) {
248                                 info->start[i] =
249                                         base + (i * 0x00010000) - 0x00030000;
250                         }
251                 }
252 #endif
253         } else {
254                 /* set sector offsets for top boot block type           */
255                 i = info->sector_count - 1;
256                 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
257
258 #ifndef CONFIG_SYS_FLASH_16BIT
259                         info->start[i--] = base + info->size - 0x00004000;
260                         info->start[i--] = base + info->size - 0x00008000;
261                         info->start[i--] = base + info->size - 0x0000C000;
262                         info->start[i--] = base + info->size - 0x00010000;
263                         info->start[i--] = base + info->size - 0x00014000;
264                         info->start[i--] = base + info->size - 0x00018000;
265                         info->start[i--] = base + info->size - 0x0001C000;
266                         for (; i >= 0; i--) {
267                                 info->start[i] = base + i * 0x00020000;
268                         }
269
270                 } else {
271
272                         info->start[i--] = base + info->size - 0x00008000;
273                         info->start[i--] = base + info->size - 0x0000C000;
274                         info->start[i--] = base + info->size - 0x00010000;
275                         for (; i >= 0; i--) {
276                                 info->start[i] = base + i * 0x00020000;
277                         }
278                 }
279 #else
280                         info->start[i--] = base + info->size - 0x00002000;
281                         info->start[i--] = base + info->size - 0x00004000;
282                         info->start[i--] = base + info->size - 0x00006000;
283                         info->start[i--] = base + info->size - 0x00008000;
284                         info->start[i--] = base + info->size - 0x0000A000;
285                         info->start[i--] = base + info->size - 0x0000C000;
286                         info->start[i--] = base + info->size - 0x0000E000;
287                         for (; i >= 0; i--) {
288                                 info->start[i] = base + i * 0x00010000;
289                         }
290
291                 } else {
292
293                         info->start[i--] = base + info->size - 0x00004000;
294                         info->start[i--] = base + info->size - 0x00006000;
295                         info->start[i--] = base + info->size - 0x00008000;
296                         for (; i >= 0; i--) {
297                                 info->start[i] = base + i * 0x00010000;
298                         }
299                 }
300 #endif
301         }
302
303
304 }
305
306 /*-----------------------------------------------------------------------
307  */
308
309 void flash_print_info (flash_info_t * info)
310 {
311         int i;
312         uchar *boottype;
313         uchar botboot[] = ", bottom boot sect)\n";
314         uchar topboot[] = ", top boot sector)\n";
315
316         if (info->flash_id == FLASH_UNKNOWN) {
317                 printf ("missing or unknown FLASH type\n");
318                 return;
319         }
320
321         switch (info->flash_id & FLASH_VENDMASK) {
322         case FLASH_MAN_AMD:
323                 printf ("AMD ");
324                 break;
325         case FLASH_MAN_FUJ:
326                 printf ("FUJITSU ");
327                 break;
328         case FLASH_MAN_SST:
329                 printf ("SST ");
330                 break;
331         case FLASH_MAN_STM:
332                 printf ("STM ");
333                 break;
334         case FLASH_MAN_INTEL:
335                 printf ("INTEL ");
336                 break;
337         default:
338                 printf ("Unknown Vendor ");
339                 break;
340         }
341
342         if (info->flash_id & 0x0001) {
343                 boottype = botboot;
344         } else {
345                 boottype = topboot;
346         }
347
348         switch (info->flash_id & FLASH_TYPEMASK) {
349         case FLASH_AM400B:
350                 printf ("AM29LV400B (4 Mbit%s", boottype);
351                 break;
352         case FLASH_AM400T:
353                 printf ("AM29LV400T (4 Mbit%s", boottype);
354                 break;
355         case FLASH_AM800B:
356                 printf ("AM29LV800B (8 Mbit%s", boottype);
357                 break;
358         case FLASH_AM800T:
359                 printf ("AM29LV800T (8 Mbit%s", boottype);
360                 break;
361         case FLASH_AM160B:
362                 printf ("AM29LV160B (16 Mbit%s", boottype);
363                 break;
364         case FLASH_AM160T:
365                 printf ("AM29LV160T (16 Mbit%s", boottype);
366                 break;
367         case FLASH_AM320B:
368                 printf ("AM29LV320B (32 Mbit%s", boottype);
369                 break;
370         case FLASH_AM320T:
371                 printf ("AM29LV320T (32 Mbit%s", boottype);
372                 break;
373         case FLASH_INTEL800B:
374                 printf ("INTEL28F800B (8 Mbit%s", boottype);
375                 break;
376         case FLASH_INTEL800T:
377                 printf ("INTEL28F800T (8 Mbit%s", boottype);
378                 break;
379         case FLASH_INTEL160B:
380                 printf ("INTEL28F160B (16 Mbit%s", boottype);
381                 break;
382         case FLASH_INTEL160T:
383                 printf ("INTEL28F160T (16 Mbit%s", boottype);
384                 break;
385         case FLASH_INTEL320B:
386                 printf ("INTEL28F320B (32 Mbit%s", boottype);
387                 break;
388         case FLASH_INTEL320T:
389                 printf ("INTEL28F320T (32 Mbit%s", boottype);
390                 break;
391
392 #if 0                           /* enable when devices are available */
393
394         case FLASH_INTEL640B:
395                 printf ("INTEL28F640B (64 Mbit%s", boottype);
396                 break;
397         case FLASH_INTEL640T:
398                 printf ("INTEL28F640T (64 Mbit%s", boottype);
399                 break;
400 #endif
401         case FLASH_28F320J3A:
402                 printf ("INTEL28F320J3A (32 Mbit%s", boottype);
403                 break;
404         case FLASH_28F640J3A:
405                 printf ("INTEL28F640J3A (64 Mbit%s", boottype);
406                 break;
407         case FLASH_28F128J3A:
408                 printf ("INTEL28F128J3A (128 Mbit%s", boottype);
409                 break;
410
411         default:
412                 printf ("Unknown Chip Type\n");
413                 break;
414         }
415
416         printf ("  Size: %ld MB in %d Sectors\n",
417                 info->size >> 20, info->sector_count);
418
419         printf ("  Sector Start Addresses:");
420         for (i = 0; i < info->sector_count; ++i) {
421                 if ((i % 5) == 0)
422                         printf ("\n   ");
423                 printf (" %08lX%s",
424                         info->start[i], info->protect[i] ? " (RO)" : "     ");
425         }
426         printf ("\n");
427         return;
428 }
429
430
431 /*-----------------------------------------------------------------------
432  */
433
434
435 /*-----------------------------------------------------------------------
436  */
437
438 /*
439  * The following code cannot be run from FLASH!
440  */
441 ulong flash_get_size (volatile FLASH_WORD_SIZE * addr, flash_info_t * info)
442 {
443         short i;
444         ulong base = (ulong) addr;
445         FLASH_WORD_SIZE value;
446
447         /* Write auto select command: read Manufacturer ID */
448
449
450 #ifndef CONFIG_SYS_FLASH_16BIT
451
452         /*
453          * Note: if it is an AMD flash and the word at addr[0000]
454          * is 0x00890089 this routine will think it is an Intel
455          * flash device and may(most likely) cause trouble.
456          */
457
458         addr[0x0000] = 0x00900090;
459         if (addr[0x0000] != 0x00890089) {
460                 addr[0x0555] = 0x00AA00AA;
461                 addr[0x02AA] = 0x00550055;
462                 addr[0x0555] = 0x00900090;
463 #else
464
465         /*
466          * Note: if it is an AMD flash and the word at addr[0000]
467          * is 0x0089 this routine will think it is an Intel
468          * flash device and may(most likely) cause trouble.
469          */
470
471         addr[0x0000] = 0x0090;
472
473         if (addr[0x0000] != 0x0089) {
474                 addr[0x0555] = 0x00AA;
475                 addr[0x02AA] = 0x0055;
476                 addr[0x0555] = 0x0090;
477 #endif
478         }
479         value = addr[0];
480
481         switch (value) {
482         case (AMD_MANUFACT & FLASH_ID_MASK):
483                 info->flash_id = FLASH_MAN_AMD;
484                 break;
485         case (FUJ_MANUFACT & FLASH_ID_MASK):
486                 info->flash_id = FLASH_MAN_FUJ;
487                 break;
488         case (STM_MANUFACT & FLASH_ID_MASK):
489                 info->flash_id = FLASH_MAN_STM;
490                 break;
491         case (SST_MANUFACT & FLASH_ID_MASK):
492                 info->flash_id = FLASH_MAN_SST;
493                 break;
494         case (INTEL_MANUFACT & FLASH_ID_MASK):
495                 info->flash_id = FLASH_MAN_INTEL;
496                 break;
497         default:
498                 info->flash_id = FLASH_UNKNOWN;
499                 info->sector_count = 0;
500                 info->size = 0;
501                 return (0);     /* no or unknown flash  */
502
503         }
504
505         value = addr[1];        /* device ID            */
506
507         switch (value) {
508
509         case (AMD_ID_LV400T & FLASH_ID_MASK):
510                 info->flash_id += FLASH_AM400T;
511                 info->sector_count = 11;
512                 info->size = 0x00100000;
513                 break;          /* => 1 MB              */
514
515         case (AMD_ID_LV400B & FLASH_ID_MASK):
516                 info->flash_id += FLASH_AM400B;
517                 info->sector_count = 11;
518                 info->size = 0x00100000;
519                 break;          /* => 1 MB              */
520
521         case (AMD_ID_LV800T & FLASH_ID_MASK):
522                 info->flash_id += FLASH_AM800T;
523                 info->sector_count = 19;
524                 info->size = 0x00200000;
525                 break;          /* => 2 MB              */
526
527         case (AMD_ID_LV800B & FLASH_ID_MASK):
528                 info->flash_id += FLASH_AM800B;
529                 info->sector_count = 19;
530                 info->size = 0x00200000;
531                 break;          /* => 2 MB              */
532
533         case (AMD_ID_LV160T & FLASH_ID_MASK):
534                 info->flash_id += FLASH_AM160T;
535                 info->sector_count = 35;
536                 info->size = 0x00400000;
537                 break;          /* => 4 MB              */
538
539         case (AMD_ID_LV160B & FLASH_ID_MASK):
540                 info->flash_id += FLASH_AM160B;
541                 info->sector_count = 35;
542                 info->size = 0x00400000;
543                 break;          /* => 4 MB              */
544 #if 0                           /* enable when device IDs are available */
545         case (AMD_ID_LV320T & FLASH_ID_MASK):
546                 info->flash_id += FLASH_AM320T;
547                 info->sector_count = 67;
548                 info->size = 0x00800000;
549                 break;          /* => 8 MB              */
550
551         case (AMD_ID_LV320B & FLASH_ID_MASK):
552                 info->flash_id += FLASH_AM320B;
553                 info->sector_count = 67;
554                 info->size = 0x00800000;
555                 break;          /* => 8 MB              */
556 #endif
557
558         case (INTEL_ID_28F800B3T & FLASH_ID_MASK):
559                 info->flash_id += FLASH_INTEL800T;
560                 info->sector_count = 23;
561                 info->size = 0x00200000;
562                 break;          /* => 2 MB              */
563
564         case (INTEL_ID_28F800B3B & FLASH_ID_MASK):
565                 info->flash_id += FLASH_INTEL800B;
566                 info->sector_count = 23;
567                 info->size = 0x00200000;
568                 break;          /* => 2 MB              */
569
570         case (INTEL_ID_28F160B3T & FLASH_ID_MASK):
571                 info->flash_id += FLASH_INTEL160T;
572                 info->sector_count = 39;
573                 info->size = 0x00400000;
574                 break;          /* => 4 MB              */
575
576         case (INTEL_ID_28F160B3B & FLASH_ID_MASK):
577                 info->flash_id += FLASH_INTEL160B;
578                 info->sector_count = 39;
579                 info->size = 0x00400000;
580                 break;          /* => 4 MB              */
581
582         case (INTEL_ID_28F320B3T & FLASH_ID_MASK):
583                 info->flash_id += FLASH_INTEL320T;
584                 info->sector_count = 71;
585                 info->size = 0x00800000;
586                 break;          /* => 8 MB              */
587
588         case (INTEL_ID_28F320B3B & FLASH_ID_MASK):
589                 info->flash_id += FLASH_AM320B;
590                 info->sector_count = 71;
591                 info->size = 0x00800000;
592                 break;          /* => 8 MB              */
593
594 #if 0                           /* enable when devices are available */
595         case (INTEL_ID_28F320B3T & FLASH_ID_MASK):
596                 info->flash_id += FLASH_INTEL320T;
597                 info->sector_count = 135;
598                 info->size = 0x01000000;
599                 break;          /* => 16 MB             */
600
601         case (INTEL_ID_28F320B3B & FLASH_ID_MASK):
602                 info->flash_id += FLASH_AM320B;
603                 info->sector_count = 135;
604                 info->size = 0x01000000;
605                 break;          /* => 16 MB             */
606 #endif
607         case (INTEL_ID_28F320J3A & FLASH_ID_MASK):
608                 info->flash_id += FLASH_28F320J3A;
609                 info->sector_count = 32;
610                 info->size = 0x00400000;
611                 break;          /* => 32 MBit   */
612         case (INTEL_ID_28F640J3A & FLASH_ID_MASK):
613                 info->flash_id += FLASH_28F640J3A;
614                 info->sector_count = 64;
615                 info->size = 0x00800000;
616                 break;          /* => 64 MBit   */
617         case (INTEL_ID_28F128J3A & FLASH_ID_MASK):
618                 info->flash_id += FLASH_28F128J3A;
619                 info->sector_count = 128;
620                 info->size = 0x01000000;
621                 break;          /* => 128 MBit          */
622
623         default:
624                 /* FIXME */
625                 info->flash_id = FLASH_UNKNOWN;
626                 return (0);     /* => no or unknown flash */
627         }
628
629         flash_get_offsets (base, info);
630
631         /* check for protected sectors */
632         for (i = 0; i < info->sector_count; i++) {
633                 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
634                 /* D0 = 1 if protected */
635                 addr = (volatile FLASH_WORD_SIZE *) (info->start[i]);
636                 info->protect[i] = addr[2] & 1;
637         }
638
639         /*
640          * Prevent writes to uninitialized FLASH.
641          */
642         if (info->flash_id != FLASH_UNKNOWN) {
643                 addr = (volatile FLASH_WORD_SIZE *) info->start[0];
644                 if ((info->flash_id & 0xFF00) == FLASH_MAN_INTEL) {
645                         *addr = (0x00F000F0 & FLASH_ID_MASK);   /* reset bank */
646                 } else {
647                         *addr = (0x00FF00FF & FLASH_ID_MASK);   /* reset bank */
648                 }
649         }
650
651         return (info->size);
652 }
653
654
655 /*-----------------------------------------------------------------------
656  */
657
658 int flash_erase (flash_info_t * info, int s_first, int s_last)
659 {
660
661         volatile FLASH_WORD_SIZE *addr =
662                 (volatile FLASH_WORD_SIZE *) (info->start[0]);
663         int flag, prot, sect, l_sect, barf;
664         ulong start, now, last;
665         int rcode = 0;
666
667         if ((s_first < 0) || (s_first > s_last)) {
668                 if (info->flash_id == FLASH_UNKNOWN) {
669                         printf ("- missing\n");
670                 } else {
671                         printf ("- no sectors to erase\n");
672                 }
673                 return 1;
674         }
675
676         if ((info->flash_id == FLASH_UNKNOWN) ||
677             ((info->flash_id > FLASH_AMD_COMP) &&
678              ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL))) {
679                 printf ("Can't erase unknown flash type - aborted\n");
680                 return 1;
681         }
682
683         prot = 0;
684         for (sect = s_first; sect <= s_last; ++sect) {
685                 if (info->protect[sect]) {
686                         prot++;
687                 }
688         }
689
690         if (prot) {
691                 printf ("- Warning: %d protected sectors will not be erased!\n", prot);
692         } else {
693                 printf ("\n");
694         }
695
696         l_sect = -1;
697
698         /* Disable interrupts which might cause a timeout here */
699         flag = disable_interrupts ();
700         if (info->flash_id < FLASH_AMD_COMP) {
701 #ifndef CONFIG_SYS_FLASH_16BIT
702                 addr[0x0555] = 0x00AA00AA;
703                 addr[0x02AA] = 0x00550055;
704                 addr[0x0555] = 0x00800080;
705                 addr[0x0555] = 0x00AA00AA;
706                 addr[0x02AA] = 0x00550055;
707 #else
708                 addr[0x0555] = 0x00AA;
709                 addr[0x02AA] = 0x0055;
710                 addr[0x0555] = 0x0080;
711                 addr[0x0555] = 0x00AA;
712                 addr[0x02AA] = 0x0055;
713 #endif
714                 /* Start erase on unprotected sectors */
715                 for (sect = s_first; sect <= s_last; sect++) {
716                         if (info->protect[sect] == 0) { /* not protected */
717                                 addr = (volatile FLASH_WORD_SIZE *) (info->
718                                                                      start
719                                                                      [sect]);
720                                 addr[0] = (0x00300030 & FLASH_ID_MASK);
721                                 l_sect = sect;
722                         }
723                 }
724
725                 /* re-enable interrupts if necessary */
726                 if (flag)
727                         enable_interrupts ();
728
729                 /* wait at least 80us - let's wait 1 ms */
730                 udelay (1000);
731
732                 /*
733                  * We wait for the last triggered sector
734                  */
735                 if (l_sect < 0)
736                         goto DONE;
737
738                 start = get_timer (0);
739                 last = start;
740                 addr = (volatile FLASH_WORD_SIZE *) (info->start[l_sect]);
741                 while ((addr[0] & (0x00800080 & FLASH_ID_MASK)) !=
742                        (0x00800080 & FLASH_ID_MASK)) {
743                         if ((now = get_timer (start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
744                                 printf ("Timeout\n");
745                                 return 1;
746                         }
747                         /* show that we're waiting */
748                         if ((now - last) > 1000000) {   /* every second */
749                                 serial_putc ('.');
750                                 last = now;
751                         }
752                 }
753
754               DONE:
755                 /* reset to read mode */
756                 addr = (volatile FLASH_WORD_SIZE *) info->start[0];
757                 addr[0] = (0x00F000F0 & FLASH_ID_MASK); /* reset bank */
758         } else {
759
760
761                 for (sect = s_first; sect <= s_last; sect++) {
762                         if (info->protect[sect] == 0) { /* not protected */
763                                 barf = 0;
764 #ifndef CONFIG_SYS_FLASH_16BIT
765                                 addr = (vu_long *) (info->start[sect]);
766                                 addr[0] = 0x00500050;
767                                 addr[0] = 0x00200020;
768                                 addr[0] = 0x00D000D0;
769                                 while (!(addr[0] & 0x00800080));        /* wait for error or finish */
770                                 if (addr[0] & 0x003A003A) {     /* check for error */
771                                         barf = addr[0] & 0x003A0000;
772                                         if (barf) {
773                                                 barf >>= 16;
774                                         } else {
775                                                 barf = addr[0] & 0x0000003A;
776                                         }
777                                 }
778 #else
779                                 addr = (vu_short *) (info->start[sect]);
780                                 addr[0] = 0x0050;       /* clear status register */
781                                 addr[0] = 0x0020;
782                                 addr[0] = 0x00D0;
783                                 while (!(addr[0] & 0x0080));    /* wait for error or finish */
784                                 if (addr[0] & 0x003A)   /* check for error */
785                                         barf = addr[0] & 0x003A;
786 #endif
787                                 if (barf) {
788                                         printf ("\nFlash error in sector at %lx\n", (unsigned long) addr);
789                                         if (barf & 0x0002)
790                                                 printf ("Block locked, not erased.\n");
791                                         if ((barf & 0x0030) == 0x0030)
792                                                 printf ("Command Sequence error.\n");
793                                         if ((barf & 0x0030) == 0x0020)
794                                                 printf ("Block Erase error.\n");
795                                         if (barf & 0x0008)
796                                                 printf ("Vpp Low error.\n");
797                                         rcode = 1;
798                                 } else
799                                         printf (".");
800                                 l_sect = sect;
801                         }
802                         addr = (volatile FLASH_WORD_SIZE *) info->start[0];
803 #ifndef CONFIG_SYS_FLASH_16BIT
804                         addr[0] = (0x00FF00FF & FLASH_ID_MASK); /* reset bank */
805 #else
806                         addr[0] = (0x00FF & FLASH_ID_MASK);     /* reset bank */
807 #endif
808                 }
809
810         }
811         printf (" done\n");
812         return rcode;
813 }
814
815 /*-----------------------------------------------------------------------
816  */
817
818 /*flash_info_t *addr2info (ulong addr)
819 {
820         flash_info_t *info;
821         int i;
822
823         for (i=0, info=&flash_info[0]; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
824                 if ((addr >= info->start[0]) &&
825                     (addr < (info->start[0] + info->size)) ) {
826                         return (info);
827                 }
828         }
829
830         return (NULL);
831 }
832 */
833 /*-----------------------------------------------------------------------
834  * Copy memory to flash.
835  * Make sure all target addresses are within Flash bounds,
836  * and no protected sectors are hit.
837  * Returns:
838  * 0 - OK
839  * 1 - write timeout
840  * 2 - Flash not erased
841  * 4 - target range includes protected sectors
842  * 8 - target address not in Flash memory
843  */
844
845 /*int flash_write (uchar *src, ulong addr, ulong cnt)
846 {
847         int i;
848         ulong         end        = addr + cnt - 1;
849         flash_info_t *info_first = addr2info (addr);
850         flash_info_t *info_last  = addr2info (end );
851         flash_info_t *info;
852
853         if (cnt == 0) {
854                 return (0);
855         }
856
857         if (!info_first || !info_last) {
858                 return (8);
859         }
860
861         for (info = info_first; info <= info_last; ++info) {
862                                                                                 ulong b_end = info->start[0] + info->size;*/ /* bank end addr */
863 /*              short s_end = info->sector_count - 1;
864                 for (i=0; i<info->sector_count; ++i) {
865                         ulong e_addr = (i == s_end) ? b_end : info->start[i + 1];
866
867                         if ((end >= info->start[i]) && (addr < e_addr) &&
868                             (info->protect[i] != 0) ) {
869                                 return (4);
870                         }
871                 }
872         }
873
874 */ /* finally write data to flash */
875 /*      for (info = info_first; info <= info_last && cnt>0; ++info) {
876                 ulong len;
877
878                 len = info->start[0] + info->size - addr;
879                 if (len > cnt)
880                         len = cnt;
881                 if ((i = write_buff(info, src, addr, len)) != 0) {
882                         return (i);
883                 }
884                 cnt  -= len;
885                 addr += len;
886                 src  += len;
887         }
888         return (0);
889 }
890 */
891 /*-----------------------------------------------------------------------
892  * Copy memory to flash, returns:
893  * 0 - OK
894  * 1 - write timeout
895  * 2 - Flash not erased
896  */
897
898 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
899 {
900 #ifndef CONFIG_SYS_FLASH_16BIT
901         ulong cp, wp, data;
902         int l;
903 #else
904         ulong cp, wp;
905         ushort data;
906 #endif
907         int i, rc;
908
909 #ifndef CONFIG_SYS_FLASH_16BIT
910
911
912         wp = (addr & ~3);       /* get lower word aligned address */
913
914         /*
915          * handle unaligned start bytes
916          */
917         if ((l = addr - wp) != 0) {
918                 data = 0;
919                 for (i = 0, cp = wp; i < l; ++i, ++cp) {
920                         data = (data << 8) | (*(uchar *) cp);
921                 }
922                 for (; i < 4 && cnt > 0; ++i) {
923                         data = (data << 8) | *src++;
924                         --cnt;
925                         ++cp;
926                 }
927                 for (; cnt == 0 && i < 4; ++i, ++cp) {
928                         data = (data << 8) | (*(uchar *) cp);
929                 }
930
931                 if ((rc = write_word (info, wp, data)) != 0) {
932                         return (rc);
933                 }
934                 wp += 4;
935         }
936
937         /*
938          * handle word aligned part
939          */
940         while (cnt >= 4) {
941                 data = 0;
942                 for (i = 0; i < 4; ++i) {
943                         data = (data << 8) | *src++;
944                 }
945                 if ((rc = write_word (info, wp, data)) != 0) {
946                         return (rc);
947                 }
948                 wp += 4;
949                 cnt -= 4;
950         }
951
952         if (cnt == 0) {
953                 return (0);
954         }
955
956         /*
957          * handle unaligned tail bytes
958          */
959         data = 0;
960         for (i = 0, cp = wp; i < 4 && cnt > 0; ++i, ++cp) {
961                 data = (data << 8) | *src++;
962                 --cnt;
963         }
964         for (; i < 4; ++i, ++cp) {
965                 data = (data << 8) | (*(uchar *) cp);
966         }
967
968         return (write_word (info, wp, data));
969
970 #else
971         wp = (addr & ~1);       /* get lower word aligned address */
972
973         /*
974          * handle unaligned start byte
975          */
976         if (addr - wp) {
977                 data = 0;
978                 data = (data << 8) | *src++;
979                 --cnt;
980                 if ((rc = write_short (info, wp, data)) != 0) {
981                         return (rc);
982                 }
983                 wp += 2;
984         }
985
986         /*
987          * handle word aligned part
988          */
989 /*      l = 0; used for debuging  */
990         while (cnt >= 2) {
991                 data = 0;
992                 for (i = 0; i < 2; ++i) {
993                         data = (data << 8) | *src++;
994                 }
995
996 /*              if(!l){
997                         printf("%x",data);
998                         l = 1;
999                 }  used for debuging */
1000
1001                 if ((rc = write_short (info, wp, data)) != 0) {
1002                         return (rc);
1003                 }
1004                 wp += 2;
1005                 cnt -= 2;
1006         }
1007
1008         if (cnt == 0) {
1009                 return (0);
1010         }
1011
1012         /*
1013          * handle unaligned tail bytes
1014          */
1015         data = 0;
1016         for (i = 0, cp = wp; i < 2 && cnt > 0; ++i, ++cp) {
1017                 data = (data << 8) | *src++;
1018                 --cnt;
1019         }
1020         for (; i < 2; ++i, ++cp) {
1021                 data = (data << 8) | (*(uchar *) cp);
1022         }
1023
1024         return (write_short (info, wp, data));
1025
1026
1027 #endif
1028 }
1029
1030 /*-----------------------------------------------------------------------
1031  * Write a word to Flash, returns:
1032  * 0 - OK
1033  * 1 - write timeout
1034  * 2 - Flash not erased
1035  */
1036 #ifndef CONFIG_SYS_FLASH_16BIT
1037 static int write_word (flash_info_t * info, ulong dest, ulong data)
1038 {
1039         vu_long *addr = (vu_long *) (info->start[0]);
1040         ulong start, barf;
1041         int flag;
1042
1043 #if defined (__MIPSEL__)
1044         data = cpu_to_be32 (data);
1045 #endif
1046
1047         /* Check if Flash is (sufficiently) erased */
1048         if ((*((vu_long *) dest) & data) != data) {
1049                 return (2);
1050         }
1051
1052         /* Disable interrupts which might cause a timeout here */
1053         flag = disable_interrupts ();
1054
1055         if (info->flash_id < FLASH_AMD_COMP) {
1056                 /* AMD stuff */
1057                 addr[0x0555] = 0x00AA00AA;
1058                 addr[0x02AA] = 0x00550055;
1059                 addr[0x0555] = 0x00A000A0;
1060         } else {
1061                 /* intel stuff */
1062                 *addr = 0x00400040;
1063         }
1064
1065         *((vu_long *) dest) = data;
1066
1067         /* re-enable interrupts if necessary */
1068         if (flag)
1069                 enable_interrupts ();
1070
1071         /* data polling for D7 */
1072         start = get_timer (0);
1073
1074         if (info->flash_id < FLASH_AMD_COMP) {
1075
1076                 while ((*((vu_long *) dest) & 0x00800080) !=
1077                        (data & 0x00800080)) {
1078                         if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
1079                                 printf ("timeout\n");
1080                                 return (1);
1081                         }
1082                 }
1083
1084         } else {
1085
1086                 while (!(addr[0] & 0x00800080)) {       /* wait for error or finish */
1087                         if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
1088                                 printf ("timeout\n");
1089                                 return (1);
1090                         }
1091                 }
1092
1093                 if (addr[0] & 0x003A003A) {     /* check for error */
1094                         barf = addr[0] & 0x003A0000;
1095                         if (barf) {
1096                                 barf >>= 16;
1097                         } else {
1098                                 barf = addr[0] & 0x0000003A;
1099                         }
1100                         printf ("\nFlash write error at address %lx\n",
1101                                 (unsigned long) dest);
1102                         if (barf & 0x0002)
1103                                 printf ("Block locked, not erased.\n");
1104                         if (barf & 0x0010)
1105                                 printf ("Programming error.\n");
1106                         if (barf & 0x0008)
1107                                 printf ("Vpp Low error.\n");
1108                         return (2);
1109                 }
1110
1111
1112         }
1113
1114         return (0);
1115 }
1116
1117 #else
1118
1119 static int write_short (flash_info_t * info, ulong dest, ushort data)
1120 {
1121         vu_short *addr = (vu_short *) (info->start[0]);
1122         ulong start, barf;
1123         int flag;
1124
1125 #if defined (__MIPSEL__)
1126         data = cpu_to_be16 (data);
1127 #endif
1128
1129         /* Check if Flash is (sufficiently) erased */
1130         if ((*((vu_short *) dest) & data) != data) {
1131                 return (2);
1132         }
1133
1134         /* Disable interrupts which might cause a timeout here */
1135         flag = disable_interrupts ();
1136
1137         if (info->flash_id < FLASH_AMD_COMP) {
1138                 /* AMD stuff */
1139                 addr[0x0555] = 0x00AA;
1140                 addr[0x02AA] = 0x0055;
1141                 addr[0x0555] = 0x00A0;
1142         } else {
1143                 /* intel stuff */
1144                 *addr = 0x00D0;
1145                 *addr = 0x0040;
1146         }
1147         *((vu_short *) dest) = data;
1148
1149         /* re-enable interrupts if necessary */
1150         if (flag)
1151                 enable_interrupts ();
1152
1153         /* data polling for D7 */
1154         start = get_timer (0);
1155
1156         if (info->flash_id < FLASH_AMD_COMP) {
1157                 /* AMD stuff */
1158                 while ((*((vu_short *) dest) & 0x0080) != (data & 0x0080)) {
1159                         if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
1160                                 return (1);
1161                         }
1162                 }
1163
1164         } else {
1165                 /* intel stuff */
1166                 while (!(addr[0] & 0x0080)) {   /* wait for error or finish */
1167                         if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT)
1168                                 return (1);
1169                 }
1170
1171                 if (addr[0] & 0x003A) { /* check for error */
1172                         barf = addr[0] & 0x003A;
1173                         printf ("\nFlash write error at address %lx\n",
1174                                 (unsigned long) dest);
1175                         if (barf & 0x0002)
1176                                 printf ("Block locked, not erased.\n");
1177                         if (barf & 0x0010)
1178                                 printf ("Programming error.\n");
1179                         if (barf & 0x0008)
1180                                 printf ("Vpp Low error.\n");
1181                         return (2);
1182                 }
1183                 *addr = 0x00B0;
1184                 *addr = 0x0070;
1185                 while (!(addr[0] & 0x0080)) {   /* wait for error or finish */
1186                         if (get_timer (start) > CONFIG_SYS_FLASH_WRITE_TOUT)
1187                                 return (1);
1188                 }
1189
1190                 *addr = 0x00FF;
1191
1192         }
1193
1194         return (0);
1195
1196 }
1197 #endif