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