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