* Add support for IceCube board (with MGT5100 and MPC5200 CPUs)
[pandora-u-boot.git] / board / icecube / flash.c
1 /*
2  * (C) Copyright 2003
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
26 flash_info_t    flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips        */
27
28 /* NOTE - CONFIG_FLASH_16BIT means the CPU interface is 16-bit, it
29  *        has nothing to do with the flash chip being 8-bit or 16-bit.
30  */
31 #ifdef CONFIG_FLASH_16BIT
32 typedef unsigned short FLASH_PORT_WIDTH;
33 typedef volatile unsigned short FLASH_PORT_WIDTHV;
34 #define FLASH_ID_MASK   0xFFFF
35 #else
36 typedef unsigned char FLASH_PORT_WIDTH;
37 typedef volatile unsigned char FLASH_PORT_WIDTHV;
38 #define FLASH_ID_MASK   0xFF
39 #endif
40
41 #define FPW     FLASH_PORT_WIDTH
42 #define FPWV    FLASH_PORT_WIDTHV
43
44 #define ORMASK(size) ((-size) & OR_AM_MSK)
45
46 #define FLASH_CYCLE1    0x0555
47 #define FLASH_CYCLE2    0x02aa
48
49 /*-----------------------------------------------------------------------
50  * Functions
51  */
52 static ulong flash_get_size(FPWV *addr, flash_info_t *info);
53 static void flash_reset(flash_info_t *info);
54 static int write_word_amd(flash_info_t *info, FPWV *dest, FPW data);
55 static flash_info_t *flash_get_info(ulong base);
56
57 /*-----------------------------------------------------------------------
58  * flash_init()
59  *
60  * sets up flash_info and returns size of FLASH (bytes)
61  */
62 unsigned long flash_init (void)
63 {
64         unsigned long size = 0;
65         int i;
66         extern void flash_preinit(void);
67
68         flash_preinit();
69
70         /* Init: no FLASHes known */
71         for (i=0; i < CFG_MAX_FLASH_BANKS; ++i) {
72                 ulong flashbase = CFG_FLASH_BASE;
73
74                 memset(&flash_info[i], 0, sizeof(flash_info_t));
75
76                 flash_info[i].size =
77                         flash_get_size((FPW *)flashbase, &flash_info[i]);
78
79                 if (flash_info[i].flash_id == FLASH_UNKNOWN) {
80                         printf ("## Unknown FLASH on Bank %d - Size = 0x%08lx\n",
81                         i, flash_info[i].size);
82                 }
83
84                 size += flash_info[i].size;
85         }
86 #if CFG_MONITOR_BASE >= CFG_FLASH_BASE
87         /* monitor protection ON by default */
88         flash_protect(FLAG_PROTECT_SET,
89                       CFG_MONITOR_BASE,
90                       CFG_MONITOR_BASE+monitor_flash_len-1,
91                       flash_get_info(CFG_MONITOR_BASE));
92 #endif
93
94 #ifdef  CFG_ENV_IS_IN_FLASH
95         /* ENV protection ON by default */
96         flash_protect(FLAG_PROTECT_SET,
97                       CFG_ENV_ADDR,
98                       CFG_ENV_ADDR+CFG_ENV_SIZE-1,
99                       flash_get_info(CFG_ENV_ADDR));
100 #endif
101
102
103         return size ? size : 1;
104 }
105
106 /*-----------------------------------------------------------------------
107  */
108 static void flash_reset(flash_info_t *info)
109 {
110         FPWV *base = (FPWV *)(info->start[0]);
111
112         /* Put FLASH back in read mode */
113         if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL)
114                 *base = (FPW)0x00FF00FF;        /* Intel Read Mode */
115         else if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_AMD)
116                 *base = (FPW)0x00F000F0;        /* AMD Read Mode */
117 }
118
119 /*-----------------------------------------------------------------------
120  */
121
122 static flash_info_t *flash_get_info(ulong base)
123 {
124         int i;
125         flash_info_t * info;
126
127         for (i = 0; i < CFG_MAX_FLASH_BANKS; i ++) {
128                 info = & flash_info[i];
129                 if (info->start[0] <= base && base <= info->start[0] + info->size - 1)
130                         break;
131         }
132
133         return i == CFG_MAX_FLASH_BANKS ? 0 : info;
134 }
135
136 /*-----------------------------------------------------------------------
137  */
138
139 void flash_print_info (flash_info_t *info)
140 {
141         int i;
142         uchar *boottype;
143         uchar *bootletter;
144         uchar *fmt;
145         uchar botbootletter[] = "B";
146         uchar topbootletter[] = "T";
147         uchar botboottype[] = "bottom boot sector";
148         uchar topboottype[] = "top boot sector";
149
150         if (info->flash_id == FLASH_UNKNOWN) {
151                 printf ("missing or unknown FLASH type\n");
152                 return;
153         }
154
155         switch (info->flash_id & FLASH_VENDMASK) {
156         case FLASH_MAN_AMD:     printf ("AMD ");                break;
157         case FLASH_MAN_BM:      printf ("BRIGHT MICRO ");       break;
158         case FLASH_MAN_FUJ:     printf ("FUJITSU ");            break;
159         case FLASH_MAN_SST:     printf ("SST ");                break;
160         case FLASH_MAN_STM:     printf ("STM ");                break;
161         case FLASH_MAN_INTEL:   printf ("INTEL ");              break;
162         default:                printf ("Unknown Vendor ");     break;
163         }
164
165         /* check for top or bottom boot, if it applies */
166         if (info->flash_id & FLASH_BTYPE) {
167                 boottype = botboottype;
168                 bootletter = botbootletter;
169         }
170         else {
171                 boottype = topboottype;
172                 bootletter = topbootletter;
173         }
174
175         switch (info->flash_id & FLASH_TYPEMASK) {
176         case FLASH_AMDLV065D:
177                 fmt = "29LV065 (64 Mbit, uniform sectors)\n";
178                 break;
179         default:
180                 fmt = "Unknown Chip Type\n";
181                 break;
182         }
183
184         printf (fmt, bootletter, boottype);
185
186         printf ("  Size: %ld MB in %d Sectors\n",
187                 info->size >> 20,
188                 info->sector_count);
189
190         printf ("  Sector Start Addresses:");
191
192         for (i=0; i<info->sector_count; ++i) {
193                 if ((i % 5) == 0) {
194                         printf ("\n   ");
195                 }
196
197                 printf (" %08lX%s", info->start[i],
198                         info->protect[i] ? " (RO)" : "     ");
199         }
200
201         printf ("\n");
202 }
203
204 /*-----------------------------------------------------------------------
205  */
206
207 /*
208  * The following code cannot be run from FLASH!
209  */
210
211 ulong flash_get_size (FPWV *addr, flash_info_t *info)
212 {
213         int i;
214         /* Write auto select command: read Manufacturer ID */
215         /* Write auto select command sequence and test FLASH answer */
216         addr[FLASH_CYCLE1] = (FPW)0x00AA00AA;   /* for AMD, Intel ignores this */
217         addr[FLASH_CYCLE2] = (FPW)0x00550055;   /* for AMD, Intel ignores this */
218         addr[FLASH_CYCLE1] = (FPW)0x00900090;   /* selects Intel or AMD */
219
220         /* The manufacturer codes are only 1 byte, so just use 1 byte.
221          * This works for any bus width and any FLASH device width.
222          */
223         udelay(100);
224         switch (addr[0] & 0xff) {
225
226         case (uchar)AMD_MANUFACT:
227                 info->flash_id = FLASH_MAN_AMD;
228                 break;
229
230         case (uchar)INTEL_MANUFACT:
231                 info->flash_id = FLASH_MAN_INTEL;
232                 break;
233
234         default:
235                 info->flash_id = FLASH_UNKNOWN;
236                 info->sector_count = 0;
237                 info->size = 0;
238                 break;
239         }
240
241         /* Check 16 bits or 32 bits of ID so work on 32 or 16 bit bus. */
242         if (info->flash_id != FLASH_UNKNOWN) switch ((FPW)addr[1]) {
243
244         case (FPW)AMD_ID_LV065D:
245                 info->flash_id += FLASH_AMDLV065D;
246                 info->sector_count = 128;
247                 info->size = 0x00800000;
248                 for( i = 0; i < info->sector_count; i++ )
249                         info->start[i] = (ulong)addr + (i * 0x10000);
250                 break;                          /* => 8 or 16 MB        */
251
252         default:
253                 info->flash_id = FLASH_UNKNOWN;
254                 info->sector_count = 0;
255                 info->size = 0;
256                 return (0);                     /* => no or unknown flash */
257         }
258
259         /* Put FLASH back in read mode */
260         flash_reset(info);
261
262         return (info->size);
263 }
264
265 /*-----------------------------------------------------------------------
266  */
267
268 int     flash_erase (flash_info_t *info, int s_first, int s_last)
269 {
270         FPWV *addr;
271         int flag, prot, sect;
272         int intel = (info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL;
273         ulong start, now, last;
274         int rcode = 0;
275
276         if ((s_first < 0) || (s_first > s_last)) {
277                 if (info->flash_id == FLASH_UNKNOWN) {
278                         printf ("- missing\n");
279                 } else {
280                         printf ("- no sectors to erase\n");
281                 }
282                 return 1;
283         }
284
285         switch (info->flash_id & FLASH_TYPEMASK) {
286         case FLASH_AMDLV065D:
287                 break;
288         case FLASH_UNKNOWN:
289         default:
290                 printf ("Can't erase unknown flash type %08lx - aborted\n",
291                         info->flash_id);
292                 return 1;
293         }
294
295         prot = 0;
296         for (sect=s_first; sect<=s_last; ++sect) {
297                 if (info->protect[sect]) {
298                         prot++;
299                 }
300         }
301
302         if (prot) {
303                 printf ("- Warning: %d protected sectors will not be erased!\n",
304                         prot);
305         } else {
306                 printf ("\n");
307         }
308
309         last  = get_timer(0);
310
311         /* Start erase on unprotected sectors */
312         for (sect = s_first; sect<=s_last && rcode == 0; sect++) {
313
314                 if (info->protect[sect] != 0)   /* protected, skip it */
315                         continue;
316
317                 /* Disable interrupts which might cause a timeout here */
318                 flag = disable_interrupts();
319
320                 addr = (FPWV *)(info->start[sect]);
321                 if (intel) {
322                         *addr = (FPW)0x00500050; /* clear status register */
323                         *addr = (FPW)0x00200020; /* erase setup */
324                         *addr = (FPW)0x00D000D0; /* erase confirm */
325                 }
326                 else {
327                         /* must be AMD style if not Intel */
328                         FPWV *base;             /* first address in bank */
329
330                         base = (FPWV *)(info->start[0]);
331                         base[FLASH_CYCLE1] = (FPW)0x00AA00AA;   /* unlock */
332                         base[FLASH_CYCLE2] = (FPW)0x00550055;   /* unlock */
333                         base[FLASH_CYCLE1] = (FPW)0x00800080;   /* erase mode */
334                         base[FLASH_CYCLE1] = (FPW)0x00AA00AA;   /* unlock */
335                         base[FLASH_CYCLE2] = (FPW)0x00550055;   /* unlock */
336                         *addr = (FPW)0x00300030;        /* erase sector */
337                 }
338
339                 /* re-enable interrupts if necessary */
340                 if (flag)
341                         enable_interrupts();
342
343                 start = get_timer(0);
344
345                 /* wait at least 50us for AMD, 80us for Intel.
346                  * Let's wait 1 ms.
347                  */
348                 udelay (1000);
349
350                 while ((*addr & (FPW)0x00800080) != (FPW)0x00800080) {
351                         if ((now = get_timer(start)) > CFG_FLASH_ERASE_TOUT) {
352                                 printf ("Timeout\n");
353
354                                 if (intel) {
355                                         /* suspend erase        */
356                                         *addr = (FPW)0x00B000B0;
357                                 }
358
359                                 flash_reset(info);      /* reset to read mode */
360                                 rcode = 1;              /* failed */
361                                 break;
362                         }
363
364                         /* show that we're waiting */
365                         if ((get_timer(last)) > CFG_HZ) {/* every second */
366                                 putc ('.');
367                                 last = get_timer(0);
368                         }
369                 }
370
371                 /* show that we're waiting */
372                 if ((get_timer(last)) > CFG_HZ) {       /* every second */
373                         putc ('.');
374                         last = get_timer(0);
375                 }
376
377                 flash_reset(info);      /* reset to read mode   */
378         }
379
380         printf (" done\n");
381         return rcode;
382 }
383
384 /*-----------------------------------------------------------------------
385  * Copy memory to flash, returns:
386  * 0 - OK
387  * 1 - write timeout
388  * 2 - Flash not erased
389  */
390 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
391 {
392         FPW data = 0; /* 16 or 32 bit word, matches flash bus width on MPC8XX */
393         int bytes;        /* number of bytes to program in current word         */
394         int left;         /* number of bytes left to program                    */
395         int i, res;
396
397         for (left = cnt, res = 0;
398                  left > 0 && res == 0;
399                  addr += sizeof(data), left -= sizeof(data) - bytes) {
400
401                 bytes = addr & (sizeof(data) - 1);
402                 addr &= ~(sizeof(data) - 1);
403
404                 /* combine source and destination data so can program
405                  * an entire word of 16 or 32 bits
406                  */
407                 for (i = 0; i < sizeof(data); i++) {
408                         data <<= 8;
409                         if (i < bytes || i - bytes >= left )
410                                 data += *((uchar *)addr + i);
411                         else
412                                 data += *src++;
413                 }
414
415                 /* write one word to the flash */
416                 switch (info->flash_id & FLASH_VENDMASK) {
417                 case FLASH_MAN_AMD:
418                         res = write_word_amd(info, (FPWV *)addr, data);
419                         break;
420                 default:
421                         /* unknown flash type, error! */
422                         printf ("missing or unknown FLASH type\n");
423                         res = 1;        /* not really a timeout, but gives error */
424                         break;
425                 }
426         }
427
428         return (res);
429 }
430
431 /*-----------------------------------------------------------------------
432  * Write a word to Flash for AMD FLASH
433  * A word is 16 or 32 bits, whichever the bus width of the flash bank
434  * (not an individual chip) is.
435  *
436  * returns:
437  * 0 - OK
438  * 1 - write timeout
439  * 2 - Flash not erased
440  */
441 static int write_word_amd (flash_info_t *info, FPWV *dest, FPW data)
442 {
443         ulong start;
444         int flag;
445         int res = 0;    /* result, assume success       */
446         FPWV *base;             /* first address in flash bank  */
447
448         /* Check if Flash is (sufficiently) erased */
449         if ((*dest & data) != data) {
450                 return (2);
451         }
452
453
454         base = (FPWV *)(info->start[0]);
455
456         /* Disable interrupts which might cause a timeout here */
457         flag = disable_interrupts();
458
459         base[FLASH_CYCLE1] = (FPW)0x00AA00AA;   /* unlock */
460         base[FLASH_CYCLE2] = (FPW)0x00550055;   /* unlock */
461         base[FLASH_CYCLE1] = (FPW)0x00A000A0;   /* selects program mode */
462
463         *dest = data;           /* start programming the data   */
464
465         /* re-enable interrupts if necessary */
466         if (flag)
467                 enable_interrupts();
468
469         start = get_timer (0);
470
471         /* data polling for D7 */
472         while (res == 0 && (*dest & (FPW)0x00800080) != (data & (FPW)0x00800080)) {
473                 if (get_timer(start) > CFG_FLASH_WRITE_TOUT) {
474                         *dest = (FPW)0x00F000F0;        /* reset bank */
475                         res = 1;
476                 }
477         }
478
479         return (res);
480 }