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