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