]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/tqm8xx/flash.c
c791c305268938f82615bfaad53db2dd822dd1d5
[karo-tx-uboot.git] / board / tqm8xx / flash.c
1 /*
2  * (C) Copyright 2000-2002
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 /* #define DEBUG */
25
26 #include <common.h>
27 #include <mpc8xx.h>
28 #include <environment.h>
29
30 #ifndef CFG_ENV_ADDR
31 #define CFG_ENV_ADDR    (CFG_FLASH_BASE + CFG_ENV_OFFSET)
32 #endif
33
34 flash_info_t    flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips        */
35
36 /*-----------------------------------------------------------------------
37  * Functions
38  */
39 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
40 static int write_word (flash_info_t *info, ulong dest, ulong data);
41
42 /*-----------------------------------------------------------------------
43  */
44
45 unsigned long flash_init (void)
46 {
47         volatile immap_t     *immap  = (immap_t *)CFG_IMMR;
48         volatile memctl8xx_t *memctl = &immap->im_memctl;
49         unsigned long size_b0, size_b1;
50         int i;
51
52         /* Init: no FLASHes known */
53         for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
54                 flash_info[i].flash_id = FLASH_UNKNOWN;
55         }
56
57         /* Static FLASH Bank configuration here - FIXME XXX */
58
59         debug ("\n## Get flash bank 1 size @ 0x%08x\n",FLASH_BASE0_PRELIM);
60
61         size_b0 = flash_get_size((vu_long *)FLASH_BASE0_PRELIM, &flash_info[0]);
62
63         debug ("## Get flash bank 2 size @ 0x%08x\n",FLASH_BASE1_PRELIM);
64
65         if (flash_info[0].flash_id == FLASH_UNKNOWN) {
66                 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
67                         size_b0, size_b0<<20);
68         }
69
70         size_b1 = flash_get_size((vu_long *)FLASH_BASE1_PRELIM, &flash_info[1]);
71
72         debug ("## Prelim. Flash bank sizes: %08lx + 0x%08lx\n",size_b0,size_b1);
73
74         if (size_b1 > size_b0) {
75                 printf ("## ERROR: "
76                         "Bank 1 (0x%08lx = %ld MB) > Bank 0 (0x%08lx = %ld MB)\n",
77                         size_b1, size_b1<<20,
78                         size_b0, size_b0<<20
79                 );
80                 flash_info[0].flash_id  = FLASH_UNKNOWN;
81                 flash_info[1].flash_id  = FLASH_UNKNOWN;
82                 flash_info[0].sector_count      = -1;
83                 flash_info[1].sector_count      = -1;
84                 flash_info[0].size              = 0;
85                 flash_info[1].size              = 0;
86                 return (0);
87         }
88
89         debug  ("## Before remap: "
90                 "BR0: 0x%08x    OR0: 0x%08x    "
91                 "BR1: 0x%08x    OR1: 0x%08x\n",
92                 memctl->memc_br0, memctl->memc_or0,
93                 memctl->memc_br1, memctl->memc_or1);
94
95         /* Remap FLASH according to real size */
96         memctl->memc_or0 = CFG_OR_TIMING_FLASH | (-size_b0 & OR_AM_MSK);
97         memctl->memc_br0 = (CFG_FLASH_BASE & BR_BA_MSK) | BR_MS_GPCM | BR_V;
98
99         debug ("## BR0: 0x%08x    OR0: 0x%08x\n",
100                 memctl->memc_br0, memctl->memc_or0);
101
102         /* Re-do sizing to get full correct info */
103         size_b0 = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
104
105 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
106         /* monitor protection ON by default */
107         debug ("Protect monitor: %08lx ... %08lx\n",
108                 (ulong)CFG_MONITOR_BASE,
109                 (ulong)CFG_MONITOR_BASE + monitor_flash_len - 1);
110
111         flash_protect(FLAG_PROTECT_SET,
112                       CFG_MONITOR_BASE,
113                       CFG_MONITOR_BASE + monitor_flash_len - 1,
114                       &flash_info[0]);
115 #endif
116
117 #ifdef  CFG_ENV_IS_IN_FLASH
118         /* ENV protection ON by default */
119         debug ("Protect %senvironment: %08lx ... %08lx\n",
120 # ifdef CFG_ENV_ADDR_REDUND
121                 "primary   ",
122 # else
123                 "",
124 # endif
125                 (ulong)CFG_ENV_ADDR,
126                 (ulong)CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1);
127
128         flash_protect(FLAG_PROTECT_SET,
129                       CFG_ENV_ADDR,
130                       CFG_ENV_ADDR + CFG_ENV_SECT_SIZE - 1,
131                       &flash_info[0]);
132 #endif
133
134 #ifdef CFG_ENV_ADDR_REDUND
135         debug ("Protect redundand environment: %08lx ... %08lx\n",
136                 (ulong)CFG_ENV_ADDR_REDUND,
137                 (ulong)CFG_ENV_ADDR_REDUND + CFG_ENV_SECT_SIZE - 1);
138
139         flash_protect(FLAG_PROTECT_SET,
140                       CFG_ENV_ADDR_REDUND,
141                       CFG_ENV_ADDR_REDUND + CFG_ENV_SECT_SIZE - 1,
142                       &flash_info[0]);
143 #endif
144
145         if (size_b1) {
146                 memctl->memc_or1 = CFG_OR_TIMING_FLASH | (-size_b1 & 0xFFFF8000);
147                 memctl->memc_br1 = ((CFG_FLASH_BASE + size_b0) & BR_BA_MSK) |
148                                     BR_MS_GPCM | BR_V;
149
150                 debug ("## BR1: 0x%08x    OR1: 0x%08x\n",
151                         memctl->memc_br1, memctl->memc_or1);
152
153                 /* Re-do sizing to get full correct info */
154                 size_b1 = flash_get_size((vu_long *)(CFG_FLASH_BASE + size_b0),
155                                           &flash_info[1]);
156
157 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
158                 /* monitor protection ON by default */
159                 flash_protect(FLAG_PROTECT_SET,
160                               CFG_MONITOR_BASE,
161                               CFG_MONITOR_BASE+monitor_flash_len-1,
162                               &flash_info[1]);
163 #endif
164
165 #ifdef  CFG_ENV_IS_IN_FLASH
166                 /* ENV protection ON by default */
167                 flash_protect(FLAG_PROTECT_SET,
168                               CFG_ENV_ADDR,
169                               CFG_ENV_ADDR+CFG_ENV_SIZE-1,
170                               &flash_info[1]);
171 #endif
172         } else {
173                 memctl->memc_br1 = 0;           /* invalidate bank */
174
175                 flash_info[1].flash_id = FLASH_UNKNOWN;
176                 flash_info[1].sector_count = -1;
177                 flash_info[1].size = 0;
178
179                 debug ("## DISABLE BR1: 0x%08x    OR1: 0x%08x\n",
180                         memctl->memc_br1, memctl->memc_or1);
181         }
182
183         debug ("## Final Flash bank sizes: %08lx + 0x%08lx\n",size_b0,size_b1);
184
185         flash_info[0].size = size_b0;
186         flash_info[1].size = size_b1;
187
188         return (size_b0 + size_b1);
189 }
190
191 /*-----------------------------------------------------------------------
192  */
193 void flash_print_info  (flash_info_t *info)
194 {
195         int i;
196
197         if (info->flash_id == FLASH_UNKNOWN) {
198                 printf ("missing or unknown FLASH type\n");
199                 return;
200         }
201
202         switch (info->flash_id & FLASH_VENDMASK) {
203         case FLASH_MAN_AMD:     printf ("AMD ");                break;
204         case FLASH_MAN_FUJ:     printf ("FUJITSU ");            break;
205         default:                printf ("Unknown Vendor ");     break;
206         }
207
208         switch (info->flash_id & FLASH_TYPEMASK) {
209 #ifdef CONFIG_TQM8xxM   /* mirror bit flash */
210         case FLASH_AMLV128U:    printf ("AM29LV128ML (128Mbit, uniform sector size)\n");
211                                 break;
212 # else  /* ! TQM8xxM */
213         case FLASH_AM400B:      printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
214                                 break;
215         case FLASH_AM400T:      printf ("AM29LV400T (4 Mbit, top boot sector)\n");
216                                 break;
217         case FLASH_AM800B:      printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
218                                 break;
219         case FLASH_AM800T:      printf ("AM29LV800T (8 Mbit, top boot sector)\n");
220                                 break;
221         case FLASH_AM160B:      printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
222                                 break;
223         case FLASH_AM160T:      printf ("AM29LV160T (16 Mbit, top boot sector)\n");
224                                 break;
225         case FLASH_AM320B:      printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
226                                 break;
227         case FLASH_AM320T:      printf ("AM29LV320T (32 Mbit, top boot sector)\n");
228                                 break;
229 #endif  /* TQM8xxM */
230         default:                printf ("Unknown Chip Type\n");
231                                 break;
232         }
233
234         printf ("  Size: %ld MB in %d Sectors\n",
235                 info->size >> 20, info->sector_count);
236
237         printf ("  Sector Start Addresses:");
238         for (i=0; i<info->sector_count; ++i) {
239                 if ((i % 5) == 0)
240                         printf ("\n   ");
241                 printf (" %08lX%s",
242                         info->start[i],
243                         info->protect[i] ? " (RO)" : "     "
244                 );
245         }
246         printf ("\n");
247         return;
248 }
249
250 /*-----------------------------------------------------------------------
251  */
252
253
254 /*-----------------------------------------------------------------------
255  */
256
257 /*
258  * The following code cannot be run from FLASH!
259  */
260
261 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
262 {
263         short i;
264         ulong value;
265         ulong base = (ulong)addr;
266
267         /* Write auto select command: read Manufacturer ID */
268         addr[0x0555] = 0x00AA00AA;
269         addr[0x02AA] = 0x00550055;
270         addr[0x0555] = 0x00900090;
271
272         value = addr[0];
273
274         debug ("Manuf. ID @ 0x%08lx: 0x%08lx\n", (ulong)addr, value);
275
276         switch (value) {
277         case AMD_MANUFACT:
278                 info->flash_id = FLASH_MAN_AMD;
279                 break;
280         case FUJ_MANUFACT:
281                 info->flash_id = FLASH_MAN_FUJ;
282                 break;
283         default:
284                 info->flash_id = FLASH_UNKNOWN;
285                 info->sector_count = 0;
286                 info->size = 0;
287                 return (0);                     /* no or unknown flash  */
288         }
289
290         value = addr[1];                        /* device ID            */
291
292         debug ("Device ID @ 0x%08lx: 0x%08lx\n", (ulong)(&addr[1]), value);
293
294         switch (value) {
295 #ifdef CONFIG_TQM8xxM   /* mirror bit flash */
296         case AMD_ID_MIRROR:
297                 switch(addr[14]) {
298                 case AMD_ID_LV128U_2:
299                         if (addr[15] != AMD_ID_LV128U_3) {
300                                 info->flash_id = FLASH_UNKNOWN;
301                         }
302                         else {
303                                 info->flash_id += FLASH_AMLV128U;
304                                 info->sector_count = 256;
305                                 info->size = 0x02000000;
306                         }
307                         break;                          /* => 32 MB             */
308                 default:
309                         info->flash_id = FLASH_UNKNOWN;
310                         break;
311                 }
312                 break;
313 # else  /* ! TQM8xxM */
314         case AMD_ID_LV400T:
315                 info->flash_id += FLASH_AM400T;
316                 info->sector_count = 11;
317                 info->size = 0x00100000;
318                 break;                          /* => 1 MB              */
319
320         case AMD_ID_LV400B:
321                 info->flash_id += FLASH_AM400B;
322                 info->sector_count = 11;
323                 info->size = 0x00100000;
324                 break;                          /* => 1 MB              */
325
326         case AMD_ID_LV800T:
327                 info->flash_id += FLASH_AM800T;
328                 info->sector_count = 19;
329                 info->size = 0x00200000;
330                 break;                          /* => 2 MB              */
331
332         case AMD_ID_LV800B:
333                 info->flash_id += FLASH_AM800B;
334                 info->sector_count = 19;
335                 info->size = 0x00200000;
336                 break;                          /* => 2 MB              */
337
338         case AMD_ID_LV160T:
339                 info->flash_id += FLASH_AM160T;
340                 info->sector_count = 35;
341                 info->size = 0x00400000;
342                 break;                          /* => 4 MB              */
343
344         case AMD_ID_LV160B:
345                 info->flash_id += FLASH_AM160B;
346                 info->sector_count = 35;
347                 info->size = 0x00400000;
348                 break;                          /* => 4 MB              */
349
350         case AMD_ID_LV320T:
351                 info->flash_id += FLASH_AM320T;
352                 info->sector_count = 71;
353                 info->size = 0x00800000;
354                 break;                          /* => 8 MB              */
355
356         case AMD_ID_LV320B:
357                 info->flash_id += FLASH_AM320B;
358                 info->sector_count = 71;
359                 info->size = 0x00800000;
360                 break;                          /* => 8 MB              */
361 #endif  /* TQM8xxM */
362         default:
363                 info->flash_id = FLASH_UNKNOWN;
364                 return (0);                     /* => no or unknown flash */
365         }
366
367         /* set up sector start address table */
368         switch (value) {
369 #ifdef CONFIG_TQM8xxM   /* mirror bit flash */
370         case AMD_ID_MIRROR:
371                 switch (info->flash_id & FLASH_TYPEMASK) {
372                         /* only known types here - no default */
373                 case FLASH_AMLV128U:
374                         for (i = 0; i < info->sector_count; i++) {
375                                 info->start[i] = base;
376                                 base += 0x20000;
377                         }
378                         break;
379                 }
380                 break;
381 # else  /* ! TQM8xxM */
382         case AMD_ID_LV400B:
383         case AMD_ID_LV800B:
384         case AMD_ID_LV160B:
385                 /* set sector offsets for bottom boot block type        */
386                 info->start[0] = base + 0x00000000;
387                 info->start[1] = base + 0x00008000;
388                 info->start[2] = base + 0x0000C000;
389                 info->start[3] = base + 0x00010000;
390                 for (i = 4; i < info->sector_count; i++) {
391                         info->start[i] = base + (i * 0x00020000) - 0x00060000;
392                 }
393                 break;
394         case AMD_ID_LV400T:
395         case AMD_ID_LV800T:
396         case AMD_ID_LV160T:
397                 /* set sector offsets for top boot block type           */
398                 i = info->sector_count - 1;
399                 info->start[i--] = base + info->size - 0x00008000;
400                 info->start[i--] = base + info->size - 0x0000C000;
401                 info->start[i--] = base + info->size - 0x00010000;
402                 for (; i >= 0; i--) {
403                         info->start[i] = base + i * 0x00020000;
404                 }
405                 break;
406         case AMD_ID_LV320B:
407                 for (i = 0; i < info->sector_count; i++) {
408                         info->start[i] = base;
409                         /*
410                          * The first 8 sectors are 8 kB,
411                          * all the other ones  are 64 kB
412                          */
413                         base += (i < 8)
414                                 ?  2 * ( 8 << 10)
415                                 :  2 * (64 << 10);
416                 }
417                 break;
418         case AMD_ID_LV320T:
419                 for (i = 0; i < info->sector_count; i++) {
420                         info->start[i] = base;
421                         /*
422                          * The last 8 sectors are 8 kB,
423                          * all the other ones  are 64 kB
424                          */
425                         base += (i < (info->sector_count - 8))
426                                 ?  2 * (64 << 10)
427                                 :  2 * ( 8 << 10);
428                 }
429                 break;
430 #endif  /* TQM8xxM */
431         default:
432                 return (0);
433                 break;
434         }
435
436         /* check for protected sectors */
437         for (i = 0; i < info->sector_count; i++) {
438                 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
439                 /* D0 = 1 if protected */
440                 addr = (volatile unsigned long *)(info->start[i]);
441                 info->protect[i] = addr[2] & 1;
442         }
443
444         /*
445          * Prevent writes to uninitialized FLASH.
446          */
447         if (info->flash_id != FLASH_UNKNOWN) {
448                 addr = (volatile unsigned long *)info->start[0];
449
450                 *addr = 0x00F000F0;     /* reset bank */
451         }
452
453         return (info->size);
454 }
455
456
457 /*-----------------------------------------------------------------------
458  */
459
460 int     flash_erase (flash_info_t *info, int s_first, int s_last)
461 {
462         vu_long *addr = (vu_long*)(info->start[0]);
463         int flag, prot, sect, l_sect;
464         ulong start, now, last;
465
466         debug ("flash_erase: first: %d last: %d\n", s_first, s_last);
467
468         if ((s_first < 0) || (s_first > s_last)) {
469                 if (info->flash_id == FLASH_UNKNOWN) {
470                         printf ("- missing\n");
471                 } else {
472                         printf ("- no sectors to erase\n");
473                 }
474                 return 1;
475         }
476
477         if ((info->flash_id == FLASH_UNKNOWN) ||
478             (info->flash_id > FLASH_AMD_COMP)) {
479                 printf ("Can't erase unknown flash type %08lx - aborted\n",
480                         info->flash_id);
481                 return 1;
482         }
483
484         prot = 0;
485         for (sect=s_first; sect<=s_last; ++sect) {
486                 if (info->protect[sect]) {
487                         prot++;
488                 }
489         }
490
491         if (prot) {
492                 printf ("- Warning: %d protected sectors will not be erased!\n",
493                         prot);
494         } else {
495                 printf ("\n");
496         }
497
498         l_sect = -1;
499
500         /* Disable interrupts which might cause a timeout here */
501         flag = disable_interrupts();
502
503         addr[0x0555] = 0x00AA00AA;
504         addr[0x02AA] = 0x00550055;
505         addr[0x0555] = 0x00800080;
506         addr[0x0555] = 0x00AA00AA;
507         addr[0x02AA] = 0x00550055;
508
509         /* Start erase on unprotected sectors */
510         for (sect = s_first; sect<=s_last; sect++) {
511                 if (info->protect[sect] == 0) { /* not protected */
512                         addr = (vu_long*)(info->start[sect]);
513                         addr[0] = 0x00300030;
514                         l_sect = sect;
515                 }
516         }
517
518         /* re-enable interrupts if necessary */
519         if (flag)
520                 enable_interrupts();
521
522         /* wait at least 80us - let's wait 1 ms */
523         udelay (1000);
524
525         /*
526          * We wait for the last triggered sector
527          */
528         if (l_sect < 0)
529                 goto DONE;
530
531         start = get_timer (0);
532         last  = start;
533         addr = (vu_long*)(info->start[l_sect]);
534         while ((addr[0] & 0x00800080) != 0x00800080) {
535                 if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
536                         printf ("Timeout\n");
537                         return 1;
538                 }
539                 /* show that we're waiting */
540                 if ((now - last) > 1000) {      /* every second */
541                         putc ('.');
542                         last = now;
543                 }
544         }
545
546 DONE:
547         /* reset to read mode */
548         addr = (volatile unsigned long *)info->start[0];
549         addr[0] = 0x00F000F0;   /* reset bank */
550
551         printf (" done\n");
552         return 0;
553 }
554
555 /*-----------------------------------------------------------------------
556  * Copy memory to flash, returns:
557  * 0 - OK
558  * 1 - write timeout
559  * 2 - Flash not erased
560  */
561
562 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
563 {
564         ulong cp, wp, data;
565         int i, l, rc;
566
567         wp = (addr & ~3);       /* get lower word aligned address */
568
569         /*
570          * handle unaligned start bytes
571          */
572         if ((l = addr - wp) != 0) {
573                 data = 0;
574                 for (i=0, cp=wp; i<l; ++i, ++cp) {
575                         data = (data << 8) | (*(uchar *)cp);
576                 }
577                 for (; i<4 && cnt>0; ++i) {
578                         data = (data << 8) | *src++;
579                         --cnt;
580                         ++cp;
581                 }
582                 for (; cnt==0 && i<4; ++i, ++cp) {
583                         data = (data << 8) | (*(uchar *)cp);
584                 }
585
586                 if ((rc = write_word(info, wp, data)) != 0) {
587                         return (rc);
588                 }
589                 wp += 4;
590         }
591
592         /*
593          * handle word aligned part
594          */
595         while (cnt >= 4) {
596                 data = 0;
597                 for (i=0; i<4; ++i) {
598                         data = (data << 8) | *src++;
599                 }
600                 if ((rc = write_word(info, wp, data)) != 0) {
601                         return (rc);
602                 }
603                 wp  += 4;
604                 cnt -= 4;
605         }
606
607         if (cnt == 0) {
608                 return (0);
609         }
610
611         /*
612          * handle unaligned tail bytes
613          */
614         data = 0;
615         for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
616                 data = (data << 8) | *src++;
617                 --cnt;
618         }
619         for (; i<4; ++i, ++cp) {
620                 data = (data << 8) | (*(uchar *)cp);
621         }
622
623         return (write_word(info, wp, data));
624 }
625
626 /*-----------------------------------------------------------------------
627  * Write a word to Flash, returns:
628  * 0 - OK
629  * 1 - write timeout
630  * 2 - Flash not erased
631  */
632 static int write_word (flash_info_t *info, ulong dest, ulong data)
633 {
634         vu_long *addr = (vu_long*)(info->start[0]);
635         ulong start;
636         int flag;
637
638         /* Check if Flash is (sufficiently) erased */
639         if ((*((vu_long *)dest) & data) != data) {
640                 return (2);
641         }
642         /* Disable interrupts which might cause a timeout here */
643         flag = disable_interrupts();
644
645         addr[0x0555] = 0x00AA00AA;
646         addr[0x02AA] = 0x00550055;
647         addr[0x0555] = 0x00A000A0;
648
649         *((vu_long *)dest) = data;
650
651         /* re-enable interrupts if necessary */
652         if (flag)
653                 enable_interrupts();
654
655         /* data polling for D7 */
656         start = get_timer (0);
657         while ((*((vu_long *)dest) & 0x00800080) != (data & 0x00800080)) {
658                 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
659                         return (1);
660                 }
661         }
662         return (0);
663 }
664
665 /*-----------------------------------------------------------------------
666  */