Get MPC8641HPCN flash images working.
[pandora-u-boot.git] / board / mpc8641hpcn / flash.c
1 /*
2  * (C) Copyright 2003 Motorola Inc.
3  *  Xianghua Xiao,(X.Xiao@motorola.com)
4  *
5  * (C) Copyright 2000, 2001
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         default:                printf ("Unknown Chip Type\n");
136                                 break;
137         }
138
139         printf ("  Size: %ld MB in %d Sectors\n",
140                 info->size >> 20, info->sector_count);
141
142         printf ("  Sector Start Addresses:");
143         for (i=0; i<info->sector_count; ++i) {
144                 if ((i % 5) == 0)
145                         printf ("\n   ");
146                 printf (" %08lX%s",
147                         info->start[i],
148                         info->protect[i] ? " (RO)" : "     "
149                 );
150         }
151         printf ("\n");
152 }
153
154 /*
155  * The following code cannot be run from FLASH!
156  */
157
158 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
159 {
160         short i;
161         ulong value;
162         ulong base = (ulong)addr;
163         ulong sector_offset;
164
165 #ifdef DEBUG
166         printf("Check flash at 0x%08x\n",(uint)addr);
167 #endif
168         /* Write "Intelligent Identifier" command: read Manufacturer ID */
169         *addr = 0x90909090;
170         udelay(20);
171         asm("sync");
172
173         value = addr[0] & 0x00FF00FF;
174
175 #ifdef DEBUG
176         printf("manufacturer=0x%x\n",(uint)value);
177 #endif
178         switch (value) {
179         case MT_MANUFACT:       /* SHARP, MT or => Intel */
180         case INTEL_ALT_MANU:
181                 info->flash_id = FLASH_MAN_INTEL;
182                 break;
183         default:
184 #if defined(CONFIG_MPC8641_SIM)
185           info->flash_id = FLASH_MAN_INTEL;
186           break;
187 #else
188                 printf("unknown manufacturer: %x\n", (unsigned int)value);
189                 info->flash_id = FLASH_UNKNOWN;
190                 info->sector_count = 0;
191                 info->size = 0;
192                 return (0);                     /* no or unknown flash  */
193 #endif
194         }
195
196         value = addr[1] & 0x00FF00FF;             /* device ID            */
197
198 #ifdef DEBUG
199         printf("deviceID=0x%x\n",(uint)value);
200 #endif
201         switch (value) {
202         case (INTEL_ID_28F016S):
203                 info->flash_id += FLASH_28F016SV;
204                 info->sector_count = 32;
205                 info->size = 0x00400000;
206                 sector_offset = 0x20000;
207                 break;                          /* => 2x2 MB            */
208
209         case (INTEL_ID_28F160S3):
210                 info->flash_id += FLASH_28F160S3;
211                 info->sector_count = 32;
212                 info->size = 0x00400000;
213                 sector_offset = 0x20000;
214                 break;                          /* => 2x2 MB            */
215
216         case (INTEL_ID_28F320S3):
217                 info->flash_id += FLASH_28F320S3;
218                 info->sector_count = 64;
219                 info->size = 0x00800000;
220                 sector_offset = 0x20000;
221                 break;                          /* => 2x4 MB            */
222
223         case (INTEL_ID_28F640J3A):
224                 info->flash_id += FLASH_28F640J3A;
225                 info->sector_count = 64;
226                 info->size = 0x01000000;
227                 sector_offset = 0x40000;
228                 break;                          /* => 2x8 MB             */
229
230         case SHARP_ID_28F016SCL:
231         case SHARP_ID_28F016SCZ:
232                 info->flash_id      = FLASH_MAN_SHARP | FLASH_LH28F016SCT;
233                 info->sector_count  = 32;
234                 info->size          = 0x00800000;
235                 sector_offset = 0x40000;
236                 break;                          /* => 4x2 MB            */
237
238
239         default:
240 #if defined(CONFIG_MPC8641_SIM)
241                 info->flash_id += FLASH_28F640J3A;
242                 info->sector_count = 64;
243                 info->size = 0x01000000;
244                 sector_offset = 0x40000;
245                 break;                          /* => 2x8 MB             */
246 #else
247                 info->flash_id = FLASH_UNKNOWN;
248                 return (0);                     /* => no or unknown flash */
249 #endif
250
251         }
252
253         /* set up sector start address table */
254         for (i = 0; i < info->sector_count; i++) {
255                 info->start[i] = base;
256                 base += sector_offset;
257                 /* don't know how to check sector protection */
258                 info->protect[i] = 0;
259         }
260
261         /*
262          * Prevent writes to uninitialized FLASH.
263          */
264         if (info->flash_id != FLASH_UNKNOWN) {
265                 addr = (vu_long *)info->start[0];
266                 *addr = 0xFFFFFF;       /* reset bank to read array mode */
267                 asm("sync");
268         }
269
270         return (info->size);
271 }
272
273
274 /*-----------------------------------------------------------------------
275  */
276
277 int     flash_erase (flash_info_t *info, int s_first, int s_last)
278 {
279         int flag, prot, sect;
280         ulong start, now, last;
281
282         if ((s_first < 0) || (s_first > s_last)) {
283                 if (info->flash_id == FLASH_UNKNOWN) {
284                         printf ("- missing\n");
285                 } else {
286                         printf ("- no sectors to erase\n");
287                 }
288                 return 1;
289         }
290
291         if (    ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL)
292              && ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_SHARP) ) {
293                 printf ("Can't erase unknown flash type %08lx - aborted\n",
294                         info->flash_id);
295                 return 1;
296         }
297
298         prot = 0;
299         for (sect=s_first; sect<=s_last; ++sect) {
300                 if (info->protect[sect]) {
301                         prot++;
302                 }
303         }
304
305         if (prot) {
306                 printf ("- Warning: %d protected sectors will not be erased!\n",
307                         prot);
308         } else {
309                 printf ("\n");
310         }
311
312 #ifdef DEBUG
313         printf("\nFlash Erase:\n");
314 #endif
315         /* Make Sure Block Lock Bit is not set. */
316         if(clear_block_lock_bit((vu_long *)(info->start[s_first]))){
317                 return 1;
318         }
319
320         /* Start erase on unprotected sectors */
321 #if defined(DEBUG)
322         printf("Begin to erase now,s_first=0x%x s_last=0x%x...\n",s_first,s_last);
323 #endif
324         for (sect = s_first; sect<=s_last; sect++) {
325                 if (info->protect[sect] == 0) { /* not protected */
326                         vu_long *addr = (vu_long *)(info->start[sect]);
327                         asm("sync");
328
329                         last = start = get_timer (0);
330
331                         /* Disable interrupts which might cause a timeout here */
332                         flag = disable_interrupts();
333
334                         /* Reset Array */
335                         *addr = 0xffffffff;
336                         asm("sync");
337                         /* Clear Status Register */
338                         *addr = 0x50505050;
339                         asm("sync");
340                         /* Single Block Erase Command */
341                         *addr = 0x20202020;
342                         asm("sync");
343                         /* Confirm */
344                         *addr = 0xD0D0D0D0;
345                         asm("sync");
346
347                         if((info->flash_id & FLASH_TYPEMASK) != FLASH_LH28F016SCT) {
348                             /* Resume Command, as per errata update */
349                             *addr = 0xD0D0D0D0;
350                             asm("sync");
351                         }
352
353                         /* re-enable interrupts if necessary */
354                         if (flag)
355                                 enable_interrupts();
356
357                         /* wait at least 80us - let's wait 1 ms */
358                         udelay (1000);
359                         while ((*addr & 0x00800080) != 0x00800080) {
360                                 if(*addr & 0x00200020){
361                                         printf("Error in Block Erase - Lock Bit may be set!\n");
362                                         printf("Status Register = 0x%X\n", (uint)*addr);
363                                         *addr = 0xFFFFFFFF;     /* reset bank */
364                                         asm("sync");
365                                         return 1;
366                                 }
367                                 if ((now=get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
368                                         printf ("Timeout\n");
369                                         *addr = 0xFFFFFFFF;     /* reset bank */
370                                         asm("sync");
371                                         return 1;
372                                 }
373                                 /* show that we're waiting */
374                                 if ((now - last) > 1000) {      /* every second */
375                                         putc ('.');
376                                         last = now;
377                                 }
378                         }
379
380                         /* reset to read mode */
381                         *addr = 0xFFFFFFFF;
382                         asm("sync");
383                 }
384         }
385
386         printf ("flash erase done\n");
387         return 0;
388 }
389
390 /*-----------------------------------------------------------------------
391  * Copy memory to flash, returns:
392  * 0 - OK
393  * 1 - write timeout
394  * 2 - Flash not erased
395  */
396
397 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
398 {
399         ulong cp, wp, data;
400         int i, l, rc;
401
402         wp = (addr & ~3);       /* get lower word aligned address */
403
404         /*
405          * handle unaligned start bytes
406          */
407         if ((l = addr - wp) != 0) {
408                 data = 0;
409                 for (i=0, cp=wp; i<l; ++i, ++cp) {
410                         data = (data << 8) | (*(uchar *)cp);
411                 }
412                 for (; i<4 && cnt>0; ++i) {
413                         data = (data << 8) | *src++;
414                         --cnt;
415                         ++cp;
416                 }
417                 for (; cnt==0 && i<4; ++i, ++cp) {
418                         data = (data << 8) | (*(uchar *)cp);
419                 }
420
421                 if ((rc = write_word(info, wp, data)) != 0) {
422                         return (rc);
423                 }
424                 wp += 4;
425         }
426
427         /*
428          * handle word aligned part
429          */
430         while (cnt >= 4) {
431                 data = 0;
432                 for (i=0; i<4; ++i) {
433                         data = (data << 8) | *src++;
434                 }
435                 if ((rc = write_word(info, wp, data)) != 0) {
436                         return (rc);
437                 }
438                 wp  += 4;
439                 cnt -= 4;
440         }
441
442         if (cnt == 0) {
443                 return (0);
444         }
445
446         /*
447          * handle unaligned tail bytes
448          */
449         data = 0;
450         for (i=0, cp=wp; i<4 && cnt>0; ++i, ++cp) {
451                 data = (data << 8) | *src++;
452                 --cnt;
453         }
454         for (; i<4; ++i, ++cp) {
455                 data = (data << 8) | (*(uchar *)cp);
456         }
457
458         return (write_word(info, wp, data));
459 }
460
461 /*-----------------------------------------------------------------------
462  * Write a word to Flash, returns:
463  * 0 - OK
464  * 1 - write timeout
465  * 2 - Flash not erased
466  */
467 static int write_word (flash_info_t *info, ulong dest, ulong data)
468 {
469         vu_long *addr = (vu_long *)dest;
470         ulong start, csr;
471         int flag;
472
473         /* Check if Flash is (sufficiently) erased */
474         if ((*addr & data) != data) {
475                 return (2);
476         }
477         /* Disable interrupts which might cause a timeout here */
478         flag = disable_interrupts();
479
480         /* Write Command */
481         *addr = 0x10101010;
482         asm("sync");
483
484         /* Write Data */
485         *addr = data;
486
487         /* re-enable interrupts if necessary */
488         if (flag)
489                 enable_interrupts();
490
491         /* data polling for D7 */
492         start = get_timer (0);
493         flag  = 0;
494
495         while (((csr = *addr) & 0x00800080) != 0x00800080) {
496                 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
497                         flag = 1;
498                         break;
499                 }
500         }
501         if (csr & 0x40404040) {
502                 printf ("CSR indicates write error (%08lx) at %08lx\n", csr, (ulong)addr);
503                 flag = 1;
504         }
505
506         /* Clear Status Registers Command */
507         *addr = 0x50505050;
508         asm("sync");
509         /* Reset to read array mode */
510         *addr = 0xFFFFFFFF;
511         asm("sync");
512
513         return (flag);
514 }
515
516 /*-----------------------------------------------------------------------
517  * Clear Block Lock Bit, returns:
518  * 0 - OK
519  * 1 - Timeout
520  */
521
522 static int clear_block_lock_bit(vu_long  * addr)
523 {
524         ulong start, now;
525
526         /* Reset Array */
527         *addr = 0xffffffff;
528         asm("sync");
529         /* Clear Status Register */
530         *addr = 0x50505050;
531         asm("sync");
532
533         *addr = 0x60606060;
534         asm("sync");
535         *addr = 0xd0d0d0d0;
536         asm("sync");
537
538         start = get_timer (0);
539         while((*addr & 0x00800080) != 0x00800080){
540                 if ((now=get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
541                         printf ("Timeout on clearing Block Lock Bit\n");
542                         *addr = 0xFFFFFFFF;     /* reset bank */
543                         asm("sync");
544                         return 1;
545                 }
546         }
547         return 0;
548 }
549
550 #endif /* !CFG_NO_FLASH */