Merge branch 'master' of git://www.denx.de/git/u-boot-ppc4xx
[platform/kernel/u-boot.git] / board / exbitgen / flash.c
1 /*
2  * (C) Copyright 2003
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 /*
25  * Modified 4/5/2001
26  * Wait for completion of each sector erase command issued
27  * 4/5/2001
28  * Chris Hallinan - DS4.COM, Inc. - clh@net1plus.com
29  */
30
31 #include <asm/types.h>
32 #include <asm/u-boot.h>
33 #include <asm/processor.h>
34 #include <ppc4xx.h>
35 #include <common.h>
36
37 flash_info_t    flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips        */
38
39 /*-----------------------------------------------------------------------
40  * Functions
41  */
42 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
43 static int write_word (flash_info_t *info, ulong dest, ulong data);
44
45 #ifdef MEIGSBOARD_ONBOARD_FLASH /* onboard = 2MB */
46 #  ifdef CONFIG_EXBITGEN
47 #     define FLASH_WORD_SIZE unsigned long
48 #  endif
49 #else /* Meigsboard socket flash = 512KB */
50 #  ifdef CONFIG_EXBITGEN
51 #    define FLASH_WORD_SIZE unsigned char
52 #  endif
53 #endif
54
55 #ifdef CONFIG_EXBITGEN
56 #define ADDR0           0x5555
57 #define ADDR1           0x2aaa
58 #define FLASH_WORD_SIZE unsigned char
59 #endif
60
61 /*-----------------------------------------------------------------------
62  */
63
64 unsigned long flash_init (void)
65 {
66         unsigned long bank_size;
67         unsigned long tot_size;
68         unsigned long bank_addr;
69         int i;
70
71         /* Init: no FLASHes known */
72         for (i = 0; i < CFG_MAX_FLASH_BANKS; ++i) {
73                 flash_info[i].flash_id = FLASH_UNKNOWN;
74                 flash_info[i].size = 0;
75         }
76
77         tot_size = 0;
78
79         /* Detect Boot Flash */
80         bank_addr = CFG_FLASH0_BASE;
81         bank_size = flash_get_size((vu_long *)bank_addr, &flash_info[0]);
82         if (bank_size > 0) {
83                 (void)flash_protect(FLAG_PROTECT_CLEAR,
84                         bank_addr,
85                         bank_addr + bank_size - 1,
86                         &flash_info[0]);
87         }
88         if (flash_info[0].flash_id == FLASH_UNKNOWN) {
89                 printf ("## Unknown FLASH on Boot Flash Bank\n");
90         }
91         flash_info[0].size = bank_size;
92         tot_size += bank_size;
93
94         /* Detect Application Flash */
95         bank_addr = CFG_FLASH1_BASE;
96         for (i = 1; i < CFG_MAX_FLASH_BANKS; ++i) {
97                 bank_size = flash_get_size((vu_long *)bank_addr, &flash_info[i]);
98                 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
99                         break;
100                 }
101                 if (bank_size > 0) {
102                         (void)flash_protect(FLAG_PROTECT_CLEAR,
103                                 bank_addr,
104                                 bank_addr + bank_size - 1,
105                                 &flash_info[i]);
106                 }
107                 flash_info[i].size = bank_size;
108                 tot_size += bank_size;
109                 bank_addr += bank_size;
110         }
111         if (flash_info[1].flash_id == FLASH_UNKNOWN) {
112                 printf ("## Unknown FLASH on Application Flash Bank\n");
113         }
114
115         /* Protect monitor and environment sectors */
116 #if CFG_MONITOR_BASE >= CFG_FLASH0_BASE
117         flash_protect(FLAG_PROTECT_SET,
118                 CFG_MONITOR_BASE,
119                 CFG_MONITOR_BASE + monitor_flash_len - 1,
120                 &flash_info[0]);
121 #if 0xfffffffc >= CFG_FLASH0_BASE
122 #if 0xfffffffc <= CFG_FLASH0_BASE + CFG_FLASH0_SIZE - 1
123         flash_protect(FLAG_PROTECT_SET,
124                 0xfffffffc, 0xffffffff,
125                 &flash_info[0]);
126 #endif
127 #endif
128 #endif
129
130 #if (CFG_ENV_IS_IN_FLASH == 1) && defined(CFG_ENV_ADDR)
131         flash_protect(FLAG_PROTECT_SET,
132                 CFG_ENV_ADDR,
133                 CFG_ENV_ADDR + CFG_ENV_SIZE - 1,
134                 &flash_info[0]);
135 #endif
136
137         return tot_size;
138 }
139
140 /*-----------------------------------------------------------------------
141  */
142 void flash_print_info  (flash_info_t *info)
143 {
144         int i;
145
146         if (info->flash_id == FLASH_UNKNOWN) {
147                 printf ("missing or unknown FLASH type\n");
148                 return;
149         }
150
151         switch (info->flash_id & FLASH_VENDMASK) {
152         case FLASH_MAN_AMD:     printf ("AMD ");                break;
153         case FLASH_MAN_FUJ:     printf ("FUJITSU ");            break;
154         case FLASH_MAN_SST:     printf ("SST ");                break;
155         default:                printf ("Unknown Vendor ");     break;
156         }
157
158         switch (info->flash_id & FLASH_TYPEMASK) {
159         case FLASH_AM040:       printf ("AM29F040 (512 Kbit, uniform sector size)\n");
160                                 break;
161         case FLASH_AM400B:      printf ("AM29LV400B (4 Mbit, bottom boot sect)\n");
162                                 break;
163         case FLASH_AM400T:      printf ("AM29LV400T (4 Mbit, top boot sector)\n");
164                                 break;
165         case FLASH_AM800B:      printf ("AM29LV800B (8 Mbit, bottom boot sect)\n");
166                                 break;
167         case FLASH_AM800T:      printf ("AM29LV800T (8 Mbit, top boot sector)\n");
168                                 break;
169         case FLASH_AM160B:      printf ("AM29LV160B (16 Mbit, bottom boot sect)\n");
170                                 break;
171         case FLASH_AM160T:      printf ("AM29LV160T (16 Mbit, top boot sector)\n");
172                                 break;
173         case FLASH_AM320B:      printf ("AM29LV320B (32 Mbit, bottom boot sect)\n");
174                                 break;
175         case FLASH_AM320T:      printf ("AM29LV320T (32 Mbit, top boot sector)\n");
176                                 break;
177         case FLASH_AMDLV033C:   printf ("AM29LV033C (32 Mbit, uniform sector size)\n");
178                                 break;
179         case FLASH_AMDLV065D:   printf ("AM29LV065D (64 Mbit, uniform sector size)\n");
180                                 break;
181         case FLASH_SST800A:     printf ("SST39LF/VF800 (8 Mbit, uniform sector size)\n");
182                                 break;
183         case FLASH_SST160A:     printf ("SST39LF/VF160 (16 Mbit, uniform sector size)\n");
184                                 break;
185         case FLASH_SST040:      printf ("SST39LF/VF040 (4 Mbit, uniform sector size)\n");
186                                 break;
187         default:                printf ("Unknown Chip Type\n");
188                                 break;
189         }
190
191         printf ("  Size: %ld KB in %d Sectors\n",
192                 info->size >> 10, info->sector_count);
193
194         printf ("  Sector Start Addresses:");
195         for (i=0; i<info->sector_count; ++i) {
196                 if ((i % 5) == 0)
197                         printf ("\n   ");
198                 printf (" %08lX%s",
199                         info->start[i],
200                         info->protect[i] ? " (RO)" : "     "
201                 );
202         }
203         printf ("\n");
204 }
205
206 /*-----------------------------------------------------------------------
207  */
208
209
210 /*-----------------------------------------------------------------------
211  */
212
213 /*
214  * The following code cannot be run from FLASH!
215  */
216 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
217 {
218         short i;
219         FLASH_WORD_SIZE value;
220         ulong base = (ulong)addr;
221         volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)addr;
222
223         /* Write auto select command: read Manufacturer ID */
224         addr2[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
225         addr2[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
226         addr2[ADDR0] = (FLASH_WORD_SIZE)0x00900090;
227
228         value = addr2[0];
229
230         switch (value) {
231         case (FLASH_WORD_SIZE)AMD_MANUFACT:
232                 info->flash_id = FLASH_MAN_AMD;
233                 break;
234         case (FLASH_WORD_SIZE)FUJ_MANUFACT:
235                 info->flash_id = FLASH_MAN_FUJ;
236                 break;
237         case (FLASH_WORD_SIZE)SST_MANUFACT:
238                 info->flash_id = FLASH_MAN_SST;
239                 break;
240         default:
241                 info->flash_id = FLASH_UNKNOWN;
242                 info->sector_count = 0;
243                 info->size = 0;
244                 return (0);                     /* no or unknown flash  */
245         }
246
247         value = addr2[1];                       /* device ID            */
248
249         switch (value) {
250         case (FLASH_WORD_SIZE)AMD_ID_F040B:
251                 info->flash_id += FLASH_AM040;
252                 info->sector_count = 8;
253                 info->size = 0x0080000; /* => 512 ko */
254                 break;
255         case (FLASH_WORD_SIZE)AMD_ID_LV400T:
256                 info->flash_id += FLASH_AM400T;
257                 info->sector_count = 11;
258                 info->size = 0x00080000;
259                 break;                          /* => 0.5 MB            */
260
261         case (FLASH_WORD_SIZE)AMD_ID_LV400B:
262                 info->flash_id += FLASH_AM400B;
263                 info->sector_count = 11;
264                 info->size = 0x00080000;
265                 break;                          /* => 0.5 MB            */
266
267         case (FLASH_WORD_SIZE)AMD_ID_LV800T:
268                 info->flash_id += FLASH_AM800T;
269                 info->sector_count = 19;
270                 info->size = 0x00100000;
271                 break;                          /* => 1 MB              */
272
273         case (FLASH_WORD_SIZE)AMD_ID_LV800B:
274                 info->flash_id += FLASH_AM800B;
275                 info->sector_count = 19;
276                 info->size = 0x00100000;
277                 break;                          /* => 1 MB              */
278
279         case (FLASH_WORD_SIZE)AMD_ID_LV160T:
280                 info->flash_id += FLASH_AM160T;
281                 info->sector_count = 35;
282                 info->size = 0x00200000;
283                 break;                          /* => 2 MB              */
284
285         case (FLASH_WORD_SIZE)AMD_ID_LV160B:
286                 info->flash_id += FLASH_AM160B;
287                 info->sector_count = 35;
288                 info->size = 0x00200000;
289                 break;                          /* => 2 MB              */
290
291         case (FLASH_WORD_SIZE)AMD_ID_LV033C:
292                 info->flash_id += FLASH_AMDLV033C;
293                 info->sector_count = 64;
294                 info->size = 0x00400000;
295                 break;                          /* => 4 MB              */
296
297         case (FLASH_WORD_SIZE)AMD_ID_LV065D:
298                 info->flash_id += FLASH_AMDLV065D;
299                 info->sector_count = 128;
300                 info->size = 0x00800000;
301                 break;                          /* => 8 MB              */
302
303         case (FLASH_WORD_SIZE)AMD_ID_LV320T:
304                 info->flash_id += FLASH_AM320T;
305                 info->sector_count = 67;
306                 info->size = 0x00400000;
307                 break;                          /* => 4 MB              */
308
309         case (FLASH_WORD_SIZE)AMD_ID_LV320B:
310                 info->flash_id += FLASH_AM320B;
311                 info->sector_count = 67;
312                 info->size = 0x00400000;
313                 break;                          /* => 4 MB              */
314
315         case (FLASH_WORD_SIZE)SST_ID_xF800A:
316                 info->flash_id += FLASH_SST800A;
317                 info->sector_count = 16;
318                 info->size = 0x00100000;
319                 break;                          /* => 1 MB              */
320
321         case (FLASH_WORD_SIZE)SST_ID_xF160A:
322                 info->flash_id += FLASH_SST160A;
323                 info->sector_count = 32;
324                 info->size = 0x00200000;
325                 break;                          /* => 2 MB              */
326         case (FLASH_WORD_SIZE)SST_ID_xF040:
327                 info->flash_id += FLASH_SST040;
328                 info->sector_count = 128;
329                 info->size = 0x00080000;
330                 break;                          /* => 512KB             */
331
332         default:
333                 info->flash_id = FLASH_UNKNOWN;
334                 return (0);                     /* => no or unknown flash */
335
336         }
337
338         /* set up sector start address table */
339         if (((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST) ||
340                 (info->flash_id  == FLASH_AM040) ||
341                 (info->flash_id == FLASH_AMDLV033C) ||
342                 (info->flash_id == FLASH_AMDLV065D)) {
343                 ulong sectsize = info->size / info->sector_count;
344                 for (i = 0; i < info->sector_count; i++)
345                         info->start[i] = base + (i * sectsize);
346         } else {
347             if (info->flash_id & FLASH_BTYPE) {
348                 /* set sector offsets for bottom boot block type        */
349                 info->start[0] = base + 0x00000000;
350                 info->start[1] = base + 0x00004000;
351                 info->start[2] = base + 0x00006000;
352                 info->start[3] = base + 0x00008000;
353                 for (i = 4; i < info->sector_count; i++) {
354                         info->start[i] = base + (i * 0x00010000) - 0x00030000;
355                 }
356             } else {
357                 /* set sector offsets for top boot block type           */
358                 i = info->sector_count - 1;
359                 info->start[i--] = base + info->size - 0x00004000;
360                 info->start[i--] = base + info->size - 0x00006000;
361                 info->start[i--] = base + info->size - 0x00008000;
362                 for (; i >= 0; i--) {
363                         info->start[i] = base + i * 0x00010000;
364                 }
365             }
366         }
367
368         /* check for protected sectors */
369         for (i = 0; i < info->sector_count; i++) {
370                 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
371                 /* D0 = 1 if protected */
372
373                 addr2 = (volatile FLASH_WORD_SIZE *)(info->start[i]);
374                 if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_SST)
375                         info->protect[i] = 0;
376                 else
377                         info->protect[i] = addr2[2] & 1;
378         }
379
380         /* switch to the read mode */
381         if (info->flash_id != FLASH_UNKNOWN) {
382                 addr2 = (FLASH_WORD_SIZE *)info->start[0];
383                 *addr2 = (FLASH_WORD_SIZE)0x00F000F0;   /* reset bank */
384         }
385
386         return (info->size);
387 }
388
389 /*-----------------------------------------------------------------------
390  */
391
392 int flash_erase (flash_info_t *info, int s_first, int s_last)
393 {
394         volatile FLASH_WORD_SIZE *addr = (FLASH_WORD_SIZE *)(info->start[0]);
395         volatile FLASH_WORD_SIZE *addr2;
396         int flag, prot, sect;
397         ulong start, now, last;
398
399         if ((s_first < 0) || (s_first > s_last)) {
400                 if (info->flash_id == FLASH_UNKNOWN) {
401                         printf ("- missing\n");
402                 } else {
403                         printf ("- no sectors to erase\n");
404                 }
405                 return 1;
406         }
407
408         if (info->flash_id == FLASH_UNKNOWN) {
409                 printf ("Can't erase unknown flash type - aborted\n");
410                 return 1;
411         }
412
413         prot = 0;
414         for (sect=s_first; sect<=s_last; ++sect) {
415                 if (info->protect[sect]) {
416                         prot++;
417                 }
418         }
419
420         if (prot) {
421                 printf ("- Warning: %d protected sectors will not be erased!\n",
422                         prot);
423         } else {
424                 printf ("\n");
425         }
426
427         start = get_timer (0);
428         last  = start;
429         /* Start erase on unprotected sectors */
430         for (sect = s_first; sect <= s_last; sect++) {
431                 if (info->protect[sect] == 0) { /* not protected */
432                         addr2 = (FLASH_WORD_SIZE *)(info->start[sect]);
433
434                         /* Disable interrupts which might cause a timeout here */
435                         flag = disable_interrupts();
436
437                         addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
438                         addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
439                         addr[ADDR0] = (FLASH_WORD_SIZE)0x00800080;
440                         addr[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
441                         addr[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
442                         addr2[0] = (FLASH_WORD_SIZE)0x00300030;
443
444                         /* re-enable interrupts if necessary */
445                         if (flag)
446                                 enable_interrupts();
447
448                         /* wait at least 80us - let's wait 1 ms */
449                         udelay (1000);
450
451                         while ((addr2[0] & 0x00800080) !=
452                                 (FLASH_WORD_SIZE) 0x00800080) {
453                                 if ((now=get_timer(start)) >
454                                            CFG_FLASH_ERASE_TOUT) {
455                                         printf ("Timeout\n");
456                                         addr[0] = (FLASH_WORD_SIZE)0x00F000F0;
457                                         return 1;
458                                 }
459
460                                 /* show that we're waiting */
461                                 if ((now - last) > 1000) {  /* every second  */
462                                         putc ('.');
463                                         last = now;
464                                 }
465                         }
466
467                         addr[0] = (FLASH_WORD_SIZE)0x00F000F0;
468                 }
469         }
470
471         printf (" done\n");
472
473         return 0;
474 }
475
476 /*-----------------------------------------------------------------------
477  * Copy memory to flash, returns:
478  * 0 - OK
479  * 1 - write timeout
480  * 2 - Flash not erased
481  */
482
483 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
484 {
485         ulong cp, wp, data;
486         int i, l, rc;
487
488         wp = (addr & ~3);       /* get lower word aligned address */
489
490         /*
491          * handle unaligned start bytes
492          */
493         if ((l = addr - wp) != 0) {
494                 data = 0;
495                 for (i=0, cp=wp; i<l; ++i, ++cp) {
496                         data = (data << 8) | (*(uchar *)cp);
497                 }
498                 for (; i<4 && cnt>0; ++i) {
499                         data = (data << 8) | *src++;
500                         --cnt;
501                         ++cp;
502                 }
503                 for (; cnt==0 && i<4; ++i, ++cp) {
504                         data = (data << 8) | (*(uchar *)cp);
505                 }
506
507                 if ((rc = write_word(info, wp, data)) != 0) {
508                         return (rc);
509                 }
510                 wp += 4;
511         }
512
513         /*
514          * handle word aligned part
515          */
516         while (cnt >= 4) {
517                 data = 0;
518                 for (i=0; i<4; ++i) {
519                         data = (data << 8) | *src++;
520                 }
521                 if ((rc = write_word(info, wp, data)) != 0) {
522                         return (rc);
523                 }
524                 wp  += 4;
525                 cnt -= 4;
526         }
527
528         if (cnt == 0) {
529                 return (0);
530         }
531
532         /*
533          * handle unaligned tail bytes
534          */
535         data = 0;
536         for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
537                 data = (data << 8) | *src++;
538                 --cnt;
539         }
540         for (; i<4; ++i, ++cp) {
541                 data = (data << 8) | (*(uchar *)cp);
542         }
543
544         return (write_word(info, wp, data));
545 }
546
547 /*-----------------------------------------------------------------------
548  * Write a word to Flash, returns:
549  * 0 - OK
550  * 1 - write timeout
551  * 2 - Flash not erased
552  */
553 static int write_word (flash_info_t *info, ulong dest, ulong data)
554 {
555         volatile FLASH_WORD_SIZE *addr2 = (FLASH_WORD_SIZE *)(info->start[0]);
556         volatile FLASH_WORD_SIZE *dest2 = (FLASH_WORD_SIZE *)dest;
557         volatile FLASH_WORD_SIZE *data2 = (FLASH_WORD_SIZE *)&data;
558         ulong start;
559         int flag;
560         int i;
561
562         /* Check if Flash is (sufficiently) erased */
563         if ((*((volatile ulong *)dest) & data) != data) {
564                 printf("dest = %08lx, *dest = %08lx, data = %08lx\n",
565                         dest, *(volatile ulong *)dest, data);
566                 return 2;
567         }
568
569         for (i=0; i < 4/sizeof(FLASH_WORD_SIZE); i++) {
570                 /* Disable interrupts which might cause a timeout here */
571                 flag = disable_interrupts();
572
573                 addr2[ADDR0] = (FLASH_WORD_SIZE)0x00AA00AA;
574                 addr2[ADDR1] = (FLASH_WORD_SIZE)0x00550055;
575                 addr2[ADDR0] = (FLASH_WORD_SIZE)0x00A000A0;
576                 dest2[i] = data2[i];
577
578                 /* re-enable interrupts if necessary */
579                 if (flag)
580                         enable_interrupts();
581
582                 /* data polling for D7 */
583                 start = get_timer (0);
584                 while ((dest2[i] & 0x00800080) != (data2[i] & 0x00800080)) {
585                         if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
586                                 addr2[0] = (FLASH_WORD_SIZE)0x00F000F0;
587                                 return (1);
588                         }
589                 }
590         }
591
592         addr2[0] = (FLASH_WORD_SIZE)0x00F000F0;
593
594         return (0);
595 }
596
597 /*-----------------------------------------------------------------------
598  */