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