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