]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/cmi/flash.c
karo: tx6: add support for TX6 HW Rev. 3
[karo-tx-uboot.git] / board / cmi / flash.c
1 /*
2  * (C) Copyright 2003
3  * Martin Winistoerfer, martinwinistoerfer@gmx.ch.
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7
8 /*
9  * File:                flash.c
10  *
11  * Discription:         This Driver is for 28F320J3A, 28F640J3A and
12  *                      28F128J3A Intel flashs working in 16 Bit mode.
13  *                      They are single bank flashs.
14  *
15  *                      Most of this code is taken from existing u-boot
16  *                      source code.
17  */
18
19
20 #include <common.h>
21 #include <mpc5xx.h>
22
23 #if defined(CONFIG_ENV_IS_IN_FLASH)
24 # ifndef  CONFIG_ENV_ADDR
25 #  define CONFIG_ENV_ADDR       (CONFIG_SYS_FLASH_BASE + CONFIG_ENV_OFFSET)
26 # endif
27 # ifndef  CONFIG_ENV_SIZE
28 #  define CONFIG_ENV_SIZE       CONFIG_ENV_SECT_SIZE
29 # endif
30 # ifndef  CONFIG_ENV_SECT_SIZE
31 #  define CONFIG_ENV_SECT_SIZE  CONFIG_ENV_SIZE
32 # endif
33 #endif
34
35 #define FLASH_ID_MASK                   0xFFFF
36 #define FLASH_BLOCK_SIZE                0x00010000
37 #define FLASH_CMD_READ_ID               0x0090
38 #define FLASH_CMD_RESET                 0x00ff
39 #define FLASH_CMD_BLOCK_ERASE           0x0020
40 #define FLASH_CMD_ERASE_CONFIRM         0x00D0
41 #define FLASH_CMD_CLEAR_STATUS          0x0050
42 #define FLASH_CMD_SUSPEND_ERASE         0x00B0
43 #define FLASH_CMD_WRITE                 0x0040
44 #define FLASH_CMD_PROTECT               0x0060
45 #define FLASH_CMD_PROTECT_SET           0x0001
46 #define FLASH_CMD_PROTECT_CLEAR         0x00D0
47 #define FLASH_STATUS_DONE               0x0080
48
49 flash_info_t    flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
50
51 /*
52  * Local function prototypes
53  */
54 static ulong    flash_get_size          (vu_short *addr, flash_info_t *info);
55 static int      write_short             (flash_info_t *info, ulong dest, ushort data);
56 static void     flash_get_offsets       (ulong base, flash_info_t *info);
57
58 /*
59  * Initialize flash
60  */
61
62 unsigned long flash_init (void)
63 {
64         unsigned long size_b0;
65         int i;
66
67         /* Init: no FLASHes known */
68         for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
69                 flash_info[i].flash_id = FLASH_UNKNOWN;
70         }
71
72         /* Static FLASH Bank configuration here - FIXME XXX */
73 #if 1
74         debug ("\n## Get flash bank 1 size @ 0x%08x\n",FLASH_BASE0_PRELIM);
75 #endif
76         size_b0 = flash_get_size((vu_short *)FLASH_BASE0_PRELIM, &flash_info[0]);
77
78         if (flash_info[0].flash_id == FLASH_UNKNOWN) {
79                 printf ("## Unknown FLASH on Bank 0: "
80                         "ID 0x%lx, Size = 0x%08lx = %ld MB\n",
81                         flash_info[0].flash_id,
82                         size_b0, size_b0<<20);
83         }
84
85         flash_get_offsets (FLASH_BASE0_PRELIM, &flash_info[0]);
86
87         flash_info[0].size = size_b0;
88
89 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH_BASE
90         /* monitor protection ON by default */
91         flash_protect(FLAG_PROTECT_SET,
92                       CONFIG_SYS_MONITOR_BASE,
93                       CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
94                       &flash_info[0]);
95 #endif
96
97 #ifdef  CONFIG_ENV_IS_IN_FLASH
98         /* ENV protection ON by default */
99         flash_protect(FLAG_PROTECT_SET,
100                       CONFIG_ENV_ADDR,
101                       CONFIG_ENV_ADDR+CONFIG_ENV_SECT_SIZE-1,
102                       &flash_info[0]);
103 #endif
104
105         return size_b0;
106 }
107
108 /*
109  * Compute start adress of each sector (block)
110  */
111
112 static void flash_get_offsets (ulong base, flash_info_t *info)
113 {
114         int i;
115
116         if (info->flash_id == FLASH_UNKNOWN) {
117                 return;
118         }
119
120         switch (info->flash_id & FLASH_VENDMASK) {
121         case FLASH_MAN_INTEL:
122             for (i = 0; i < info->sector_count; i++) {
123                 info->start[i] = base + i * FLASH_BLOCK_SIZE;
124             }
125             return;
126
127         default:
128             printf ("Don't know sector offsets for flash type 0x%lx\n",
129                 info->flash_id);
130             return;
131         }
132 }
133
134 /*
135  * Print flash information
136  */
137 void flash_print_info  (flash_info_t *info)
138 {
139         int i;
140
141         if (info->flash_id == FLASH_UNKNOWN) {
142                 printf ("missing or unknown FLASH type\n");
143                 return;
144         }
145
146         switch (info->flash_id & FLASH_VENDMASK) {
147         case FLASH_MAN_AMD:     printf ("AMD ");                break;
148         case FLASH_MAN_FUJ:     printf ("Fujitsu ");            break;
149         case FLASH_MAN_SST:     printf ("SST ");                break;
150         case FLASH_MAN_STM:     printf ("STM ");                break;
151         case FLASH_MAN_INTEL:   printf ("Intel ");              break;
152         case FLASH_MAN_MT:      printf ("MT ");                 break;
153         default:                printf ("Unknown Vendor ");     break;
154         }
155
156         switch (info->flash_id & FLASH_TYPEMASK) {
157         case FLASH_28F320J3A:   printf ("28F320J3A (32Mbit) 16-Bit\n");
158                                 break;
159         case FLASH_28F640J3A:   printf ("28F640J3A (64Mbit) 16-Bit\n");
160                                 break;
161         case FLASH_28F128J3A:   printf ("28F128J3A (128Mbit) 16-Bit\n");
162                                 break;
163         default:                printf ("Unknown Chip Type\n");
164                                 break;
165         }
166
167         if (info->size >= (1 << 20)) {
168                 i = 20;
169         } else {
170                 i = 10;
171         }
172         printf ("  Size: %ld %cB in %d Sectors\n",
173                 info->size >> i,
174                 (i == 20) ? 'M' : 'k',
175                 info->sector_count);
176
177         printf ("  Sector Start Addresses:");
178         for (i=0; i<info->sector_count; ++i) {
179                 if ((i % 5) == 0)
180                         printf ("\n   ");
181                 printf (" %08lX%s",
182                         info->start[i],
183                         info->protect[i] ? " (RO)" : "     "
184                 );
185         }
186         printf ("\n");
187         return;
188 }
189
190 /*
191  * Get size of flash in bytes.
192  * The following code cannot be run from FLASH!
193  */
194
195 static ulong flash_get_size (vu_short *addr, flash_info_t *info)
196 {
197         vu_short value;
198
199         /* Read Manufacturer ID */
200         addr[0] = FLASH_CMD_READ_ID;
201         value = addr[0];
202
203         switch (value) {
204         case (AMD_MANUFACT & FLASH_ID_MASK):
205                 info->flash_id = FLASH_MAN_AMD;
206                 break;
207         case (FUJ_MANUFACT & FLASH_ID_MASK):
208                 info->flash_id = FLASH_MAN_FUJ;
209                 break;
210         case (SST_MANUFACT & FLASH_ID_MASK):
211                 info->flash_id = FLASH_MAN_SST;
212                 break;
213         case (STM_MANUFACT & FLASH_ID_MASK):
214                 info->flash_id = FLASH_MAN_STM;
215                 break;
216         case (INTEL_MANUFACT & FLASH_ID_MASK):
217                 info->flash_id = FLASH_MAN_INTEL;
218                 break;
219         default:
220                 info->flash_id = FLASH_UNKNOWN;
221                 info->sector_count = 0;
222                 info->size = 0;
223                 addr[0] = FLASH_CMD_RESET;      /* restore read mode */
224                 return (0);                     /* no or unknown flash  */
225         }
226
227         value = addr[1];                        /* device ID            */
228
229         switch (value) {
230         case (INTEL_ID_28F320J3A  & FLASH_ID_MASK):
231                 info->flash_id += FLASH_28F320J3A;
232                 info->sector_count = 32;
233                 info->size = 0x00400000;
234                 break;                          /* =>  32 MBit          */
235
236         case (INTEL_ID_28F640J3A & FLASH_ID_MASK):
237                 info->flash_id += FLASH_28F640J3A;
238                 info->sector_count = 64;
239                 info->size = 0x00800000;
240                 break;                          /* => 64 MBit           */
241
242         case (INTEL_ID_28F128J3A & FLASH_ID_MASK):
243                 info->flash_id += FLASH_28F128J3A;
244                 info->sector_count = 128;
245                 info->size = 0x01000000;
246                 break;                          /* => 128 MBit          */
247
248         default:
249                 info->flash_id = FLASH_UNKNOWN;
250                 addr[0] = FLASH_CMD_RESET;      /* restore read mode */
251                 return (0);                     /* => no or unknown flash */
252
253         }
254
255         if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
256                 printf ("** ERROR: sector count %d > max (%d) **\n",
257                         info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
258                 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
259         }
260
261         addr[0] = FLASH_CMD_RESET;              /* restore read mode */
262
263         return (info->size);
264 }
265
266
267 /*
268  * Erase unprotected sectors
269  */
270
271 int flash_erase (flash_info_t *info, int s_first, int s_last)
272 {
273         int flag, prot, sect;
274         ulong start, now, last;
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         if ((info->flash_id & FLASH_VENDMASK) != FLASH_MAN_INTEL) {
286                 printf ("Can erase only Intel flash types - aborted\n");
287                 return 1;
288         }
289
290         prot = 0;
291         for (sect=s_first; sect<=s_last; ++sect) {
292                 if (info->protect[sect]) {
293                         prot++;
294                 }
295         }
296
297         if (prot) {
298                 printf ("- Warning: %d protected sectors will not be erased!\n",
299                         prot);
300         } else {
301                 printf ("\n");
302         }
303
304         start = get_timer (0);
305         last  = start;
306
307         /* Start erase on unprotected sectors */
308         for (sect = s_first; sect<=s_last; sect++) {
309                 if (info->protect[sect] == 0) { /* not protected */
310                         vu_short *addr = (vu_short *)(info->start[sect]);
311                         unsigned long status;
312
313                         /* Disable interrupts which might cause a timeout here */
314                         flag = disable_interrupts();
315
316 #ifdef DEBUG
317                         printf("Erase sector %d at start addr 0x%08X", sect, (unsigned int)info->start[sect]);
318 #endif
319
320                         *addr = FLASH_CMD_CLEAR_STATUS;
321                         *addr = FLASH_CMD_BLOCK_ERASE;
322                         *addr = FLASH_CMD_ERASE_CONFIRM;
323
324                         /* re-enable interrupts if necessary */
325                         if (flag)
326                                 enable_interrupts();
327
328                         /* wait at least 80us - let's wait 1 ms */
329                         udelay (1000);
330
331                         while (((status = *addr) & FLASH_STATUS_DONE) != FLASH_STATUS_DONE) {
332                                 if ((now=get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
333                                         printf("Flash erase timeout at address %lx\n", info->start[sect]);
334                                         *addr = FLASH_CMD_SUSPEND_ERASE;
335                                         *addr = FLASH_CMD_RESET;
336                                         return 1;
337                                 }
338
339                                 /* show that we're waiting */
340                                 if ((now - last) > 1000) {      /* every second */
341                                         putc ('.');
342                                         last = now;
343                                 }
344                         }
345                         *addr = FLASH_CMD_RESET;
346                 }
347         }
348         printf (" done\n");
349         return 0;
350 }
351
352 /*
353  * Copy memory to flash, returns:
354  * 0 - OK
355  * 1 - write timeout
356  * 2 - Flash not erased
357  * 4 - Flash not identified
358  */
359
360 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
361 {
362         ulong cp, wp;
363         ushort data;
364         int i, rc;
365
366         if (info->flash_id == FLASH_UNKNOWN) {
367                 return 4;
368         }
369
370         wp = (addr & ~1);       /* get lower word aligned address */
371
372         /*
373          * handle unaligned start byte
374          */
375
376         if (addr - wp) {
377                 data = 0;
378                 data = (data << 8) | *src++;
379                 --cnt;
380                 if ((rc = write_short(info, wp, data)) != 0) {
381                         return (rc);
382                 }
383                 wp += 2;
384         }
385
386         /*
387          * handle word aligned part
388          */
389
390         while (cnt >= 2) {
391                 data = 0;
392                 for (i=0; i<2; ++i) {
393                         data = (data << 8) | *src++;
394                 }
395
396                 if ((rc = write_short(info, wp, data)) != 0) {
397                         return (rc);
398                 }
399                 wp  += 2;
400                 cnt -= 2;
401         }
402
403         if (cnt == 0) {
404                 return (0);
405         }
406
407         /*
408          * handle unaligned tail bytes
409          */
410
411         data = 0;
412         for (i=0, cp=wp; i<2 && cnt>0; ++i, ++cp) {
413                 data = (data << 8) | *src++;
414                 --cnt;
415         }
416         for (; i<2; ++i, ++cp) {
417                 data = (data << 8) | (*(uchar *)cp);
418         }
419
420         return (write_short(info, wp, data));
421
422 }
423
424 /*
425  * Write 16 bit (short) to flash
426  */
427
428 static int write_short (flash_info_t *info, ulong dest, ushort data)
429 {
430         vu_short *addr = (vu_short*)(info->start[0]);
431         ulong start;
432         int flag;
433
434         /* Check if Flash is (sufficiently) erased */
435         if ((*((vu_short *)dest) & data) != data) {
436                 return (2);
437         }
438
439         /* Disable interrupts which might cause a timeout here */
440         flag = disable_interrupts();
441
442         if (!(info->flash_id & FLASH_VENDMASK)) {
443                 return 4;
444         }
445         *addr = FLASH_CMD_ERASE_CONFIRM;
446         *addr = FLASH_CMD_WRITE;
447
448         *((vu_short *)dest) = data;
449
450         /* re-enable interrupts if necessary */
451         if (flag) {
452                 enable_interrupts();
453         }
454
455         /* data polling for D7 */
456         start = get_timer (0);
457
458         /* wait for error or finish */
459         while(!(addr[0] & FLASH_STATUS_DONE)){
460                 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
461                         addr[0] = FLASH_CMD_RESET;
462                         return (1);
463                 }
464         }
465
466         *addr = FLASH_CMD_RESET;
467         return (0);
468 }
469
470 /*
471  * Protects a flash sector
472  */
473
474 int flash_real_protect(flash_info_t *info, long sector, int prot)
475 {
476         vu_short *addr = (vu_short*)(info->start[sector]);
477         ulong start;
478
479         *addr = FLASH_CMD_CLEAR_STATUS;
480         *addr = FLASH_CMD_PROTECT;
481
482         if(prot) {
483                 *addr = FLASH_CMD_PROTECT_SET;
484         } else {
485                 *addr = FLASH_CMD_PROTECT_CLEAR;
486         }
487
488         /* wait for error or finish */
489         start = get_timer (0);
490         while(!(addr[0] & FLASH_STATUS_DONE)){
491                 if (get_timer(start) > CONFIG_SYS_FLASH_ERASE_TOUT) {
492                         printf("Flash protect timeout at address %lx\n",  info->start[sector]);
493                         addr[0] = FLASH_CMD_RESET;
494                         return (1);
495                 }
496         }
497         /* Set software protect flag */
498         info->protect[sector] = prot;
499         *addr = FLASH_CMD_RESET;
500         return (0);
501 }