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