Add support for 28F256J3A flah (=> 64 MB) on PM520 board
[oweals/u-boot.git] / board / pm520 / flash.c
1 /*
2  * (C) Copyright 2001
3  * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
4  *
5  * (C) Copyright 2001-2004
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 <linux/byteorder/swab.h>
29
30
31 flash_info_t flash_info[CFG_MAX_FLASH_BANKS];   /* info for FLASH chips    */
32
33 /* Board support for 1 or 2 flash devices */
34 #define FLASH_PORT_WIDTH32
35 #undef FLASH_PORT_WIDTH16
36
37 #ifdef FLASH_PORT_WIDTH16
38 #define FLASH_PORT_WIDTH                ushort
39 #define FLASH_PORT_WIDTHV               vu_short
40 #define SWAP(x)                         (x)
41 #else
42 #define FLASH_PORT_WIDTH                ulong
43 #define FLASH_PORT_WIDTHV               vu_long
44 #define SWAP(x)                         (x)
45 #endif
46
47 /* Intel-compatible flash ID */
48 #define INTEL_COMPAT  0x00890089
49 #define INTEL_ALT     0x00B000B0
50
51 /* Intel-compatible flash commands */
52 #define INTEL_PROGRAM 0x00100010
53 #define INTEL_ERASE   0x00200020
54 #define INTEL_CLEAR   0x00500050
55 #define INTEL_LOCKBIT 0x00600060
56 #define INTEL_PROTECT 0x00010001
57 #define INTEL_STATUS  0x00700070
58 #define INTEL_READID  0x00900090
59 #define INTEL_CONFIRM 0x00D000D0
60 #define INTEL_RESET   0xFFFFFFFF
61
62 /* Intel-compatible flash status bits */
63 #define INTEL_FINISHED 0x00800080
64 #define INTEL_OK       0x00800080
65
66 #define FPW        FLASH_PORT_WIDTH
67 #define FPWV   FLASH_PORT_WIDTHV
68
69 #define mb() __asm__ __volatile__ ("" : : : "memory")
70
71 /*-----------------------------------------------------------------------
72  * Functions
73  */
74 static ulong flash_get_size (FPW *addr, flash_info_t *info);
75 static int write_data (flash_info_t *info, ulong dest, FPW data);
76 static void flash_get_offsets (ulong base, flash_info_t *info);
77 void inline spin_wheel (void);
78 static void flash_sync_real_protect (flash_info_t * info);
79 static unsigned char intel_sector_protected (flash_info_t *info, ushort sector);
80
81 /*-----------------------------------------------------------------------
82  */
83
84 unsigned long flash_init (void)
85 {
86         int i;
87         ulong size = 0;
88         extern void flash_preinit(void);
89         extern void flash_afterinit(ulong, ulong);
90         ulong flashbase = CFG_FLASH_BASE;
91
92         flash_preinit();
93
94         for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
95                 switch (i) {
96                 case 0:
97                         memset(&flash_info[i], 0, sizeof(flash_info_t));
98                         flash_get_size ((FPW *) flashbase, &flash_info[i]);
99                         flash_get_offsets (flash_info[i].start[0], &flash_info[i]);
100                         break;
101                 default:
102                         panic ("configured to many flash banks!\n");
103                         break;
104                 }
105                 size += flash_info[i].size;
106
107                 /* get the h/w and s/w protection status in sync */
108                 flash_sync_real_protect(&flash_info[i]);
109         }
110
111         /* Protect monitor and environment sectors
112          */
113 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
114 #ifndef CONFIG_BOOT_ROM
115         flash_protect ( FLAG_PROTECT_SET,
116                         CFG_MONITOR_BASE,
117                         CFG_MONITOR_BASE + monitor_flash_len - 1,
118                         &flash_info[0] );
119 #endif
120 #endif
121
122 #ifdef  CFG_ENV_IS_IN_FLASH
123         flash_protect ( FLAG_PROTECT_SET,
124                         CFG_ENV_ADDR,
125                         CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0] );
126 #endif
127
128         flash_afterinit(flash_info[0].start[0], flash_info[0].size);
129
130         return size;
131 }
132
133 /*-----------------------------------------------------------------------
134  */
135 static void flash_get_offsets (ulong base, flash_info_t *info)
136 {
137         int i;
138
139         if (info->flash_id == FLASH_UNKNOWN) {
140                 return;
141         }
142
143         if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
144                 for (i = 0; i < info->sector_count; i++) {
145                         info->start[i] = base + (i * PHYS_FLASH_SECT_SIZE);
146                 }
147         }
148 }
149
150 /*-----------------------------------------------------------------------
151  */
152 void flash_print_info (flash_info_t *info)
153 {
154         int i;
155
156         if (info->flash_id == FLASH_UNKNOWN) {
157                 printf ("missing or unknown FLASH type\n");
158                 return;
159         }
160
161         switch (info->flash_id & FLASH_VENDMASK) {
162         case FLASH_MAN_INTEL:
163                 printf ("INTEL ");
164                 break;
165         default:
166                 printf ("Unknown Vendor ");
167                 break;
168         }
169
170         switch (info->flash_id & FLASH_TYPEMASK) {
171         case FLASH_28F256J3A:
172                 printf ("28F256J3A\n");
173                 break;
174
175         case FLASH_28F128J3A:
176                 printf ("28F128J3A\n");
177                 break;
178
179         case FLASH_28F640J3A:
180                 printf ("28F640J3A\n");
181                 break;
182
183         case FLASH_28F320J3A:
184                 printf ("28F320J3A\n");
185                 break;
186
187         default:
188                 printf ("Unknown Chip Type\n");
189                 break;
190         }
191
192         printf ("  Size: %ld MB in %d Sectors\n",
193                         info->size >> 20, info->sector_count);
194
195         printf ("  Sector Start Addresses:");
196         for (i = 0; i < info->sector_count; ++i) {
197                 if ((i % 5) == 0)
198                         printf ("\n   ");
199                 printf (" %08lX%s",
200                         info->start[i],
201                         info->protect[i] ? " (RO)" : "     ");
202         }
203         printf ("\n");
204         return;
205 }
206
207 /*
208  * The following code cannot be run from FLASH!
209  */
210 static ulong flash_get_size (FPW *addr, flash_info_t *info)
211 {
212         volatile FPW value;
213
214         /* Write auto select command: read Manufacturer ID */
215         addr[0x5555] = (FPW) 0x00AA00AA;
216         addr[0x2AAA] = (FPW) 0x00550055;
217         addr[0x5555] = (FPW) 0x00900090;
218
219         mb ();
220         udelay(100);
221
222         value = addr[0];
223
224         switch (value) {
225
226         case (FPW) INTEL_MANUFACT:
227                 info->flash_id = FLASH_MAN_INTEL;
228                 break;
229
230         default:
231                 info->flash_id = FLASH_UNKNOWN;
232                 info->sector_count = 0;
233                 info->size = 0;
234                 addr[0] = (FPW) 0x00FF00FF;     /* restore read mode */
235                 return (0);                     /* no or unknown flash  */
236         }
237
238         mb ();
239         value = addr[1];                        /* device ID        */
240
241         switch (value) {
242
243         case (FPW) INTEL_ID_28F256J3A:
244                 info->flash_id += FLASH_28F256J3A;
245                 info->sector_count = 256;
246                 info->size = 0x04000000;
247                 info->start[0] = CFG_FLASH_BASE;
248                 break;                          /* => 64 MB     */
249
250         case (FPW) INTEL_ID_28F128J3A:
251                 info->flash_id += FLASH_28F128J3A;
252                 info->sector_count = 128;
253                 info->size = 0x02000000;
254                 info->start[0] = CFG_FLASH_BASE + 0x02000000;
255                 break;                          /* => 32 MB     */
256
257         case (FPW) INTEL_ID_28F640J3A:
258                 info->flash_id += FLASH_28F640J3A;
259                 info->sector_count = 64;
260                 info->size = 0x01000000;
261                 info->start[0] = CFG_FLASH_BASE + 0x03000000;
262                 break;                          /* => 16 MB     */
263
264         case (FPW) INTEL_ID_28F320J3A:
265                 info->flash_id += FLASH_28F320J3A;
266                 info->sector_count = 32;
267                 info->size = 0x800000;
268                 info->start[0] = CFG_FLASH_BASE + 0x03800000;
269                 break;                          /* => 8 MB     */
270
271         default:
272                 info->flash_id = FLASH_UNKNOWN;
273                 break;
274         }
275
276         if (info->sector_count > CFG_MAX_FLASH_SECT) {
277                 printf ("** ERROR: sector count %d > max (%d) **\n",
278                         info->sector_count, CFG_MAX_FLASH_SECT);
279                 info->sector_count = CFG_MAX_FLASH_SECT;
280         }
281
282         addr[0] = (FPW) 0x00FF00FF;             /* restore read mode */
283
284         return (info->size);
285 }
286
287
288 /*
289  * This function gets the u-boot flash sector protection status
290  * (flash_info_t.protect[]) in sync with the sector protection
291  * status stored in hardware.
292  */
293 static void flash_sync_real_protect (flash_info_t * info)
294 {
295         int i;
296
297         switch (info->flash_id & FLASH_TYPEMASK) {
298
299         case FLASH_28F256J3A:
300         case FLASH_28F128J3A:
301         case FLASH_28F640J3A:
302         case FLASH_28F320J3A:
303                 for (i = 0; i < info->sector_count; ++i) {
304                         info->protect[i] = intel_sector_protected(info, i);
305                 }
306                 break;
307         default:
308                 /* no h/w protect support */
309                 break;
310         }
311 }
312
313
314 /*
315  * checks if "sector" in bank "info" is protected. Should work on intel
316  * strata flash chips 28FxxxJ3x in 8-bit mode.
317  * Returns 1 if sector is protected (or timed-out while trying to read
318  * protection status), 0 if it is not.
319  */
320 static unsigned char intel_sector_protected (flash_info_t *info, ushort sector)
321 {
322         FPWV *addr;
323         FPWV *lock_conf_addr;
324         ulong start;
325         unsigned char ret;
326
327         /*
328          * first, wait for the WSM to be finished. The rationale for
329          * waiting for the WSM to become idle for at most
330          * CFG_FLASH_ERASE_TOUT is as follows. The WSM can be busy
331          * because of: (1) erase, (2) program or (3) lock bit
332          * configuration. So we just wait for the longest timeout of
333          * the (1)-(3), i.e. the erase timeout.
334          */
335
336         /* wait at least 35ns (W12) before issuing Read Status Register */
337         udelay(1);
338         addr = (FPWV *) info->start[sector];
339         *addr = (FPW) INTEL_STATUS;
340
341         start = get_timer (0);
342         while ((*addr & (FPW) INTEL_FINISHED) != (FPW) INTEL_FINISHED) {
343                 if (get_timer (start) > CFG_FLASH_ERASE_TOUT) {
344                         *addr = (FPW) INTEL_RESET; /* restore read mode */
345                         printf("WSM busy too long, can't get prot status\n");
346                         return 1;
347                 }
348         }
349
350         /* issue the Read Identifier Codes command */
351         *addr = (FPW) INTEL_READID;
352
353         /* wait at least 35ns (W12) before reading */
354         udelay(1);
355
356         /* Intel example code uses offset of 2 for 16 bit flash */
357         lock_conf_addr = (FPWV *) info->start[sector] + 2;
358         ret = (*lock_conf_addr & (FPW) INTEL_PROTECT) ? 1 : 0;
359
360         /* put flash back in read mode */
361         *addr = (FPW) INTEL_RESET;
362
363         return ret;
364 }
365
366 /*-----------------------------------------------------------------------
367  */
368
369 int flash_erase (flash_info_t *info, int s_first, int s_last)
370 {
371         int flag, prot, sect;
372         ulong type, start, last;
373         int rcode = 0;
374
375         if ((s_first < 0) || (s_first > s_last)) {
376                 if (info->flash_id == FLASH_UNKNOWN) {
377                         printf ("- missing\n");
378                 } else {
379                         printf ("- no sectors to erase\n");
380                 }
381                 return 1;
382         }
383
384         type = (info->flash_id & FLASH_VENDMASK);
385         if ((type != FLASH_MAN_INTEL)) {
386                 printf ("Can't erase unknown flash type %08lx - aborted\n",
387                         info->flash_id);
388                 return 1;
389         }
390
391         prot = 0;
392         for (sect = s_first; sect <= s_last; ++sect) {
393                 if (info->protect[sect]) {
394                         prot++;
395                 }
396         }
397
398         if (prot) {
399                 printf ("- Warning: %d protected sectors will not be erased!\n",
400                         prot);
401         } else {
402                 printf ("\n");
403         }
404
405         start = get_timer (0);
406         last = start;
407
408         /* Disable interrupts which might cause a timeout here */
409         flag = disable_interrupts ();
410
411         /* Start erase on unprotected sectors */
412         for (sect = s_first; sect <= s_last; sect++) {
413                 if (info->protect[sect] == 0) { /* not protected */
414                         FPWV *addr = (FPWV *) (info->start[sect]);
415                         FPW status;
416
417                         printf ("Erasing sector %2d ... ", sect);
418
419                         /* arm simple, non interrupt dependent timer */
420                         start = get_timer(0);
421
422                         *addr = (FPW) 0x00500050;       /* clear status register */
423                         *addr = (FPW) 0x00200020;       /* erase setup */
424                         *addr = (FPW) 0x00D000D0;       /* erase confirm */
425
426                         while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
427                                 if (get_timer(start) > CFG_FLASH_ERASE_TOUT) {
428                                         printf ("Timeout\n");
429                                         *addr = (FPW) 0x00B000B0;       /* suspend erase     */
430                                         *addr = (FPW) 0x00FF00FF;       /* reset to read mode */
431                                         rcode = 1;
432                                         break;
433                                 }
434                         }
435
436                         *addr = 0x00500050;     /* clear status register cmd.   */
437                         *addr = 0x00FF00FF;     /* resest to read mode          */
438
439                         printf (" done\n");
440                 }
441         }
442         return rcode;
443 }
444
445 /*-----------------------------------------------------------------------
446  * Copy memory to flash, returns:
447  * 0 - OK
448  * 1 - write timeout
449  * 2 - Flash not erased
450  * 4 - Flash not identified
451  */
452
453 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
454 {
455         ulong cp, wp;
456         FPW data;
457         int count, i, l, rc, port_width;
458
459         if (info->flash_id == FLASH_UNKNOWN) {
460                 return 4;
461         }
462 /* get lower word aligned address */
463 #ifdef FLASH_PORT_WIDTH16
464         wp = (addr & ~1);
465         port_width = 2;
466 #else
467         wp = (addr & ~3);
468         port_width = 4;
469 #endif
470
471         /*
472          * handle unaligned start bytes
473          */
474         if ((l = addr - wp) != 0) {
475                 data = 0;
476                 for (i = 0, cp = wp; i < l; ++i, ++cp) {
477                         data = (data << 8) | (*(uchar *) cp);
478                 }
479                 for (; i < port_width && cnt > 0; ++i) {
480                         data = (data << 8) | *src++;
481                         --cnt;
482                         ++cp;
483                 }
484                 for (; cnt == 0 && i < port_width; ++i, ++cp) {
485                         data = (data << 8) | (*(uchar *) cp);
486                 }
487
488                 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
489                         return (rc);
490                 }
491                 wp += port_width;
492         }
493
494         /*
495          * handle word aligned part
496          */
497         count = 0;
498         while (cnt >= port_width) {
499                 data = 0;
500                 for (i = 0; i < port_width; ++i) {
501                         data = (data << 8) | *src++;
502                 }
503                 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
504                         return (rc);
505                 }
506                 wp += port_width;
507                 cnt -= port_width;
508                 if (count++ > 0x800) {
509                         spin_wheel ();
510                         count = 0;
511                 }
512         }
513
514         if (cnt == 0) {
515                 return (0);
516         }
517
518         /*
519          * handle unaligned tail bytes
520          */
521         data = 0;
522         for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
523                 data = (data << 8) | *src++;
524                 --cnt;
525         }
526         for (; i < port_width; ++i, ++cp) {
527                 data = (data << 8) | (*(uchar *) cp);
528         }
529
530         return (write_data (info, wp, SWAP (data)));
531 }
532
533 /*-----------------------------------------------------------------------
534  * Write a word or halfword to Flash, returns:
535  * 0 - OK
536  * 1 - write timeout
537  * 2 - Flash not erased
538  */
539 static int write_data (flash_info_t *info, ulong dest, FPW data)
540 {
541         FPWV *addr = (FPWV *) dest;
542         ulong status;
543         ulong start;
544         int flag;
545
546         /* Check if Flash is (sufficiently) erased */
547         if ((*addr & data) != data) {
548                 printf ("not erased at %08lx (%lx)\n", (ulong) addr, *addr);
549                 return (2);
550         }
551         /* Disable interrupts which might cause a timeout here */
552         flag = disable_interrupts ();
553
554         *addr = (FPW) 0x00400040;       /* write setup */
555         *addr = data;
556
557         /* arm simple, non interrupt dependent timer */
558         start = get_timer(0);
559
560         /* wait while polling the status register */
561         while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
562                 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
563                         *addr = (FPW) 0x00FF00FF;       /* restore read mode */
564                         return (1);
565                 }
566         }
567
568         *addr = (FPW) 0x00FF00FF;       /* restore read mode */
569
570         return (0);
571 }
572
573 void inline spin_wheel (void)
574 {
575         static int p = 0;
576         static char w[] = "\\/-";
577
578         printf ("\010%c", w[p]);
579         (++p == 3) ? (p = 0) : 0;
580 }
581
582 /*-----------------------------------------------------------------------
583  * Set/Clear sector's lock bit, returns:
584  * 0 - OK
585  * 1 - Error (timeout, voltage problems, etc.)
586  */
587 int flash_real_protect (flash_info_t *info, long sector, int prot)
588 {
589         ulong start;
590         int i;
591         int rc = 0;
592         vu_long *addr = (vu_long *)(info->start[sector]);
593         int flag = disable_interrupts();
594
595         *addr = INTEL_CLEAR;    /* Clear status register */
596         if (prot) {                     /* Set sector lock bit */
597                 *addr = INTEL_LOCKBIT;  /* Sector lock bit */
598                 *addr = INTEL_PROTECT;  /* set */
599         }
600         else {                          /* Clear sector lock bit */
601                 *addr = INTEL_LOCKBIT;  /* All sectors lock bits */
602                 *addr = INTEL_CONFIRM;  /* clear */
603         }
604
605         start = get_timer(0);
606
607         while ((*addr & INTEL_FINISHED) != INTEL_FINISHED) {
608                 if (get_timer(start) > CFG_FLASH_UNLOCK_TOUT) {
609                         printf("Flash lock bit operation timed out\n");
610                         rc = 1;
611                         break;
612                 }
613         }
614
615         if (*addr != INTEL_OK) {
616                 printf("Flash lock bit operation failed at %08X, CSR=%08X\n",
617                        (uint)addr, (uint)*addr);
618                 rc = 1;
619         }
620
621         if (!rc)
622                 info->protect[sector] = prot;
623
624         /*
625          * Clear lock bit command clears all sectors lock bits, so
626          * we have to restore lock bits of protected sectors.
627          * WARNING: code below re-locks sectors only for one bank (info).
628          * This causes problems on boards where several banks share
629          * the same chip, as sectors in othere banks will be unlocked
630          * but not re-locked. It works fine on pm520 though, as there
631          * is only one chip and one bank.
632          */
633         if (!prot)
634         {
635                 for (i = 0; i < info->sector_count; i++)
636                 {
637                         if (info->protect[i])
638                         {
639                                 start = get_timer(0);
640                                 addr = (vu_long *)(info->start[i]);
641                                 *addr = INTEL_LOCKBIT;  /* Sector lock bit */
642                                 *addr = INTEL_PROTECT;  /* set */
643                                 while ((*addr & INTEL_FINISHED) != INTEL_FINISHED)
644                                 {
645                                         if (get_timer(start) > CFG_FLASH_UNLOCK_TOUT)
646                                         {
647                                                 printf("Flash lock bit operation timed out\n");
648                                                 rc = 1;
649                                                 break;
650                                         }
651                                 }
652                         }
653                 }
654                 /*
655                  * get the s/w sector protection status in sync with the h/w,
656                  * in case something went wrong during the re-locking.
657                  */
658                 flash_sync_real_protect(info); /* resets flash to read  mode */
659         }
660
661         if (flag)
662                 enable_interrupts();
663
664         *addr = INTEL_RESET;            /* Reset to read array mode */
665
666         return rc;
667 }