]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/pm854/flash.c
Patches by Josef Wagner, 29 Oct 2004:
[karo-tx-uboot.git] / board / pm854 / flash.c
1 /*
2  * (C) Copyright 2003 Motorola Inc.
3  *  Xianghua Xiao,(X.Xiao@motorola.com)
4  *
5  * (C) Copyright 2000-2005
6  *  Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7  *
8  * (C) Copyright 2001, Stuart Hughes, Lineo Inc, stuarth@lineo.com
9  * Add support the Sharp chips on the mpc8260ads.
10  * I started with board/ip860/flash.c and made changes I found in
11  * the MTD project by David Schleef.
12  *
13  * See file CREDITS for list of people who contributed to this
14  * project.
15  *
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License as
18  * published by the Free Software Foundation; either version 2 of
19  * the License, or (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29  * MA 02111-1307 USA
30  */
31
32 #include <common.h>
33
34 #if !defined(CFG_NO_FLASH)
35
36 flash_info_t    flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips        */
37
38 #if defined(CFG_ENV_IS_IN_FLASH)
39 # ifndef  CFG_ENV_ADDR
40 #  define CFG_ENV_ADDR  (CFG_FLASH_BASE + CFG_ENV_OFFSET)
41 # endif
42 # ifndef  CFG_ENV_SIZE
43 #  define CFG_ENV_SIZE  CFG_ENV_SECT_SIZE
44 # endif
45 # ifndef  CFG_ENV_SECT_SIZE
46 #  define CFG_ENV_SECT_SIZE  CFG_ENV_SIZE
47 # endif
48 #endif
49
50 #undef DEBUG
51
52 /*-----------------------------------------------------------------------
53  * Functions
54  */
55 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
56 static int write_word (flash_info_t *info, ulong dest, ulong data);
57 static int clear_block_lock_bit(vu_long * addr);
58 /*-----------------------------------------------------------------------
59  */
60
61 unsigned long flash_init (void)
62 {
63         unsigned long size;
64         int i;
65
66         /* Init: enable write,
67          * or we cannot even write flash commands
68          */
69         for (i=0; i<CFG_MAX_FLASH_BANKS; ++i) {
70                 flash_info[i].flash_id = FLASH_UNKNOWN;
71
72                 /* set the default sector offset */
73         }
74
75         /* Static FLASH Bank configuration here - FIXME XXX */
76
77         size = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
78
79         if (flash_info[0].flash_id == FLASH_UNKNOWN) {
80                 printf ("## Unknown FLASH on Bank 0 - Size = 0x%08lx = %ld MB\n",
81                         size, size<<20);
82         }
83
84         /* Re-do sizing to get full correct info */
85         size = flash_get_size((vu_long *)CFG_FLASH_BASE, &flash_info[0]);
86
87         flash_info[0].size = size;
88
89 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
90         /* monitor protection ON by default */
91         flash_protect(FLAG_PROTECT_SET,
92                       CFG_MONITOR_BASE,
93                       CFG_MONITOR_BASE+monitor_flash_len-1,
94                       &flash_info[0]);
95
96 #ifdef  CFG_ENV_IS_IN_FLASH
97         /* ENV protection ON by default */
98         flash_protect(FLAG_PROTECT_SET,
99                       CFG_ENV_ADDR,
100                       CFG_ENV_ADDR+CFG_ENV_SECT_SIZE-1,
101                       &flash_info[0]);
102 #endif
103 #endif
104         return (size);
105 }
106
107 /*-----------------------------------------------------------------------
108  */
109 void flash_print_info  (flash_info_t *info)
110 {
111         int i;
112
113         if (info->flash_id == FLASH_UNKNOWN) {
114                 printf ("missing or unknown FLASH type\n");
115                 return;
116         }
117
118         switch (info->flash_id & FLASH_VENDMASK) {
119         case FLASH_MAN_INTEL:   printf ("Intel ");              break;
120         case FLASH_MAN_SHARP:   printf ("Sharp ");              break;
121         default:                printf ("Unknown Vendor ");     break;
122         }
123
124         switch (info->flash_id & FLASH_TYPEMASK) {
125         case FLASH_28F016SV:    printf ("28F016SV (16 Mbit, 32 x 64k)\n");
126                                 break;
127         case FLASH_28F160S3:    printf ("28F160S3 (16 Mbit, 32 x 512K)\n");
128                                 break;
129         case FLASH_28F320S3:    printf ("28F320S3 (32 Mbit, 64 x 512K)\n");
130                                 break;
131         case FLASH_LH28F016SCT: printf ("28F016SC (16 Mbit, 32 x 64K)\n");
132                                 break;
133         case FLASH_28F640J3A:   printf ("28F640J3A (64 Mbit, 64 x 128K)\n");
134                                 break;
135         case FLASH_28F128J3A:   printf ("28F128J3A (128 Mbit, 128 x 128K)\n");
136                                 break;
137         
138         default:                printf ("Unknown Chip Type\n");
139                                 break;
140         }
141
142         printf ("  Size: %ld MB in %d Sectors\n",
143                 info->size >> 20, info->sector_count);
144
145         printf ("  Sector Start Addresses:");
146         for (i=0; i<info->sector_count; ++i) {
147                 if ((i % 5) == 0)
148                         printf ("\n   ");
149                 printf (" %08lX%s",
150                         info->start[i],
151                         info->protect[i] ? " (RO)" : "     "
152                 );
153         }
154         printf ("\n");
155 }
156
157 /*
158  * The following code cannot be run from FLASH!
159  */
160
161 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
162 {
163         short i;
164         ulong value;
165         ulong base = (ulong)addr;
166         ulong sector_offset;
167
168 #ifdef DEBUG
169         printf("Check flash at 0x%08x\n",(uint)addr);
170 #endif
171         /* Write "Intelligent Identifier" command: read Manufacturer ID */
172         *addr = 0x90909090;
173         udelay(20);
174         asm("sync");
175
176         value = addr[0] & 0x00FF00FF;
177
178 #ifdef DEBUG
179         printf("manufacturer=0x%x\n",(uint)value);
180 #endif
181         switch (value) {
182         case MT_MANUFACT:       /* SHARP, MT or => Intel */
183         case INTEL_ALT_MANU:
184                 info->flash_id = FLASH_MAN_INTEL;
185                 break;
186         default:
187                 printf("unknown manufacturer: %x\n", (unsigned int)value);
188                 info->flash_id = FLASH_UNKNOWN;
189                 info->sector_count = 0;
190                 info->size = 0;
191                 return (0);                     /* no or unknown flash  */
192         }
193
194         value = addr[1] & 0x00FF00FF;             /* device ID            */
195
196 #ifdef DEBUG
197         printf("deviceID=0x%x\n",(uint)value);
198 #endif
199         switch (value) {
200         case (INTEL_ID_28F016S):
201                 info->flash_id += FLASH_28F016SV;
202                 info->sector_count = 32;
203                 info->size = 0x00400000;
204                 sector_offset = 0x20000;
205                 break;                          /* => 2x2 MB            */
206
207         case (INTEL_ID_28F160S3):
208                 info->flash_id += FLASH_28F160S3;
209                 info->sector_count = 32;
210                 info->size = 0x00400000;
211                 sector_offset = 0x20000;
212                 break;                          /* => 2x2 MB            */
213
214         case (INTEL_ID_28F320S3):
215                 info->flash_id += FLASH_28F320S3;
216                 info->sector_count = 64;
217                 info->size = 0x00800000;
218                 sector_offset = 0x20000;
219                 break;                          /* => 2x4 MB            */
220
221         case (INTEL_ID_28F640J3A):
222                 info->flash_id += FLASH_28F640J3A;
223                 info->sector_count = 64;
224                 info->size = 0x01000000;
225                 sector_offset = 0x40000;
226                 break;                          /* => 2x8 MB             */
227         
228         case (INTEL_ID_28F128J3A):
229                 info->flash_id += FLASH_28F128J3A;
230                 info->sector_count = 128;
231                 info->size = 0x02000000;
232                 sector_offset = 0x40000;
233                 break;                          /* => 2x16 MB             */
234         
235
236         case SHARP_ID_28F016SCL:
237         case SHARP_ID_28F016SCZ:
238                 info->flash_id      = FLASH_MAN_SHARP | FLASH_LH28F016SCT;
239                 info->sector_count  = 32;
240                 info->size          = 0x00800000;
241                 sector_offset = 0x40000;
242                 break;                          /* => 4x2 MB            */
243
244
245         default:
246                 info->flash_id = FLASH_UNKNOWN;
247                 return (0);                     /* => no or unknown flash */
248
249         }
250
251         /* set up sector start address table */
252         for (i = 0; i < info->sector_count; i++) {
253                 info->start[i] = base;
254                 base += sector_offset;
255                 /* don't know how to check sector protection */
256                 info->protect[i] = 0;
257         }
258
259         /*
260          * Prevent writes to uninitialized FLASH.
261          */
262         if (info->flash_id != FLASH_UNKNOWN) {
263                 addr = (vu_long *)info->start[0];
264                 *addr = 0xFFFFFF;       /* reset bank to read array mode */
265                 asm("sync");
266         }
267
268         return (info->size);
269 }
270
271
272 /*-----------------------------------------------------------------------
273  */
274
275 int     flash_erase (flash_info_t *info, int s_first, int s_last)
276 {
277         int flag, prot, sect;
278         ulong start, now, last;
279
280         if ((s_first < 0) || (s_first > s_last)) {
281                 if (info->flash_id == FLASH_UNKNOWN) {
282                         printf ("- missing\n");
283                 } else {
284                         printf ("- no sectors to erase\n");
285                 }
286                 return 1;
287         }
288
289         if (    ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL)
290              && ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_SHARP) ) {
291                 printf ("Can't erase unknown flash type %08lx - aborted\n",
292                         info->flash_id);
293                 return 1;
294         }
295
296         prot = 0;
297         for (sect=s_first; sect<=s_last; ++sect) {
298                 if (info->protect[sect]) {
299                         prot++;
300                 }
301         }
302
303         if (prot) {
304                 printf ("- Warning: %d protected sectors will not be erased!\n",
305                         prot);
306         } else {
307                 printf ("\n");
308         }
309
310 #ifdef DEBUG
311         printf("\nFlash Erase:\n");
312 #endif
313         /* Make Sure Block Lock Bit is not set. */
314         if(clear_block_lock_bit((vu_long *)(info->start[s_first]))){
315                 return 1;
316         }
317
318         /* Start erase on unprotected sectors */
319 #if defined(DEBUG)
320         printf("Begin to erase now,s_first=0x%x s_last=0x%x...\n",s_first,s_last);
321 #endif
322         for (sect = s_first; sect<=s_last; sect++) {
323                 if (info->protect[sect] == 0) { /* not protected */
324                         vu_long *addr = (vu_long *)(info->start[sect]);
325                         asm("sync");
326
327                         last = start = get_timer (0);
328                         
329                         /* Disable interrupts which might cause a timeout here */
330                         flag = disable_interrupts();
331
332                         /* Reset Array */
333                         *addr = 0xffffffff;
334                         asm("sync");
335                         /* Clear Status Register */
336                         *addr = 0x50505050;
337                         asm("sync");
338                         /* Single Block Erase Command */
339                         *addr = 0x20202020;
340                         asm("sync");
341                         /* Confirm */
342                         *addr = 0xD0D0D0D0;
343                         asm("sync");
344
345                         if((info->flash_id & FLASH_TYPEMASK) != FLASH_LH28F016SCT) {
346                             /* Resume Command, as per errata update */
347                             *addr = 0xD0D0D0D0;
348                             asm("sync");
349                         }
350
351                         /* re-enable interrupts if necessary */
352                         if (flag)
353                                 enable_interrupts();
354
355                         /* wait at least 80us - let's wait 1 ms */
356                         udelay (1000);
357                         while ((*addr & 0x00800080) != 0x00800080) {
358                                 if ((now=get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
359                                         printf ("Timeout\n");
360                                         *addr = 0xFFFFFFFF;     /* reset bank */
361                                         asm("sync");
362                                         return 1;
363                                 }
364                                 /* show that we're waiting */
365                                 if ((now - last) > 1000) {      /* every second */
366                                         putc ('.');
367                                         last = now;
368                                 }
369                         }
370                         
371                         /* reset to read mode */
372                         *addr = 0xFFFFFFFF;
373                         asm("sync");
374                 }
375         }
376
377         printf ("flash erase done\n");
378         return 0;
379 }
380
381 /*-----------------------------------------------------------------------
382  * Copy memory to flash, returns:
383  * 0 - OK
384  * 1 - write timeout
385  * 2 - Flash not erased
386  */
387
388 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
389 {
390         ulong cp, wp, data;
391         int i, l, rc;
392
393         wp = (addr & ~3);       /* get lower word aligned address */
394
395         /*
396          * handle unaligned start bytes
397          */
398         if ((l = addr - wp) != 0) {
399                 data = 0;
400                 for (i=0, cp=wp; i<l; ++i, ++cp) {
401                         data = (data << 8) | (*(uchar *)cp);
402                 }
403                 for (; i<4 && cnt>0; ++i) {
404                         data = (data << 8) | *src++;
405                         --cnt;
406                         ++cp;
407                 }
408                 for (; cnt==0 && i<4; ++i, ++cp) {
409                         data = (data << 8) | (*(uchar *)cp);
410                 }
411
412                 if ((rc = write_word(info, wp, data)) != 0) {
413                         return (rc);
414                 }
415                 wp += 4;
416         }
417
418         /*
419          * handle word aligned part
420          */
421         while (cnt >= 4) {
422                 data = 0;
423                 for (i=0; i<4; ++i) {
424                         data = (data << 8) | *src++;
425                 }
426                 if ((rc = write_word(info, wp, data)) != 0) {
427                         return (rc);
428                 }
429                 wp  += 4;
430                 cnt -= 4;
431         }
432
433         if (cnt == 0) {
434                 return (0);
435         }
436
437         /*
438          * handle unaligned tail bytes
439          */
440         data = 0;
441         for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
442                 data = (data << 8) | *src++;
443                 --cnt;
444         }
445         for (; i<4; ++i, ++cp) {
446                 data = (data << 8) | (*(uchar *)cp);
447         }
448
449         return (write_word(info, wp, data));
450 }
451
452 /*-----------------------------------------------------------------------
453  * Write a word to Flash, returns:
454  * 0 - OK
455  * 1 - write timeout
456  * 2 - Flash not erased
457  */
458 static int write_word (flash_info_t *info, ulong dest, ulong data)
459 {
460         vu_long *addr = (vu_long *)dest;
461         ulong start, csr;
462         int flag;
463
464         /* Check if Flash is (sufficiently) erased */
465         if ((*addr & data) != data) {
466                 return (2);
467         }
468         /* Disable interrupts which might cause a timeout here */
469         flag = disable_interrupts();
470
471         /* Write Command */
472         *addr = 0x10101010;
473         asm("sync");
474
475         /* Write Data */
476         *addr = data;
477
478         /* re-enable interrupts if necessary */
479         if (flag)
480                 enable_interrupts();
481
482         /* data polling for D7 */
483         start = get_timer (0);
484         flag  = 0;
485
486         while (((csr = *addr) & 0x00800080) != 0x00800080) {
487                 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
488                         flag = 1;
489                         break;
490                 }
491         }
492         if (csr & 0x40404040) {
493                 printf ("CSR indicates write error (%08lx) at %08lx\n", csr, (ulong)addr);
494                 flag = 1;
495         }
496
497         /* Clear Status Registers Command */
498         *addr = 0x50505050;
499         asm("sync");
500         /* Reset to read array mode */
501         *addr = 0xFFFFFFFF;
502         asm("sync");
503
504         return (flag);
505 }
506
507 /*-----------------------------------------------------------------------
508  * Clear Block Lock Bit, returns:
509  * 0 - OK
510  * 1 - Timeout
511  */
512
513 static int clear_block_lock_bit(vu_long  * addr)
514 {
515         ulong start, now;
516
517         /* Reset Array */
518         *addr = 0xffffffff;
519         asm("sync");
520         /* Clear Status Register */
521         *addr = 0x50505050;
522         asm("sync");
523
524         *addr = 0x60606060;
525         asm("sync");
526         *addr = 0xd0d0d0d0;
527         asm("sync");
528
529         start = get_timer (0);
530         while((*addr & 0x00800080) != 0x00800080){
531                 if ((now=get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
532                         printf ("Timeout on clearing Block Lock Bit\n");
533                         *addr = 0xFFFFFFFF;     /* reset bank */
534                         asm("sync");
535                         return 1;
536                 }
537         }
538         return 0;
539 }
540
541 #endif /* !CFG_NO_FLASH */