]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/zylonite/flash.c
rename CFG_ macros to CONFIG_SYS
[karo-tx-uboot.git] / board / zylonite / flash.c
1 /*
2  * (C) Copyright 2001
3  * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
4  *
5  * (C) Copyright 2001
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[CONFIG_SYS_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)               __swab16(x)
41 #else
42 #define FLASH_PORT_WIDTH                ulong
43 #define FLASH_PORT_WIDTHV               vu_long
44 #define SWAP(x)               __swab32(x)
45 #endif
46
47 #define FPW        FLASH_PORT_WIDTH
48 #define FPWV   FLASH_PORT_WIDTHV
49
50 #define mb() __asm__ __volatile__ ("" : : : "memory")
51
52 /*-----------------------------------------------------------------------
53  * Functions
54  */
55 static ulong flash_get_size (FPW *addr, flash_info_t *info);
56 static int write_data (flash_info_t *info, ulong dest, FPW data);
57 static void flash_get_offsets (ulong base, flash_info_t *info);
58 void inline spin_wheel (void);
59
60 /*-----------------------------------------------------------------------
61  */
62
63 unsigned long flash_init (void)
64 {
65 #if 0
66         int i;
67         ulong size = 0;
68
69         for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
70                 switch (i) {
71                 case 0:
72                         flash_get_size ((FPW *) PHYS_FLASH_1, &flash_info[i]);
73                         flash_get_offsets (PHYS_FLASH_1, &flash_info[i]);
74                         break;
75                 case 1:
76                         flash_get_size ((FPW *) PHYS_FLASH_2, &flash_info[i]);
77                         flash_get_offsets (PHYS_FLASH_2, &flash_info[i]);
78                         break;
79                 default:
80                         panic ("configured too many flash banks!\n");
81                         break;
82                 }
83                 size += flash_info[i].size;
84         }
85
86         /* Protect monitor and environment sectors
87          */
88         flash_protect ( FLAG_PROTECT_SET,
89                         CONFIG_SYS_FLASH_BASE,
90                         CONFIG_SYS_FLASH_BASE + monitor_flash_len - 1,
91                         &flash_info[0] );
92
93         flash_protect ( FLAG_PROTECT_SET,
94                         CONFIG_ENV_ADDR,
95                         CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0] );
96
97         return size;
98 #endif
99         return 0;
100 }
101
102 /*-----------------------------------------------------------------------
103  */
104 static void flash_get_offsets (ulong base, flash_info_t *info)
105 {
106         int i;
107
108         if (info->flash_id == FLASH_UNKNOWN) {
109                 return;
110         }
111
112         if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
113                 for (i = 0; i < info->sector_count; i++) {
114                         info->start[i] = base + (i * PHYS_FLASH_SECT_SIZE);
115                         info->protect[i] = 0;
116                 }
117         }
118 }
119
120 /*-----------------------------------------------------------------------
121  */
122 void flash_print_info (flash_info_t *info)
123 {
124         int i;
125
126         if (info->flash_id == FLASH_UNKNOWN) {
127                 printf ("missing or unknown FLASH type\n");
128                 return;
129         }
130
131         switch (info->flash_id & FLASH_VENDMASK) {
132         case FLASH_MAN_INTEL:
133                 printf ("INTEL ");
134                 break;
135         default:
136                 printf ("Unknown Vendor ");
137                 break;
138         }
139
140         switch (info->flash_id & FLASH_TYPEMASK) {
141         case FLASH_28F128J3A:
142                 printf ("28F128J3A\n");
143                 break;
144         default:
145                 printf ("Unknown Chip Type\n");
146                 break;
147         }
148
149         printf ("  Size: %ld MB in %d Sectors\n",
150                         info->size >> 20, info->sector_count);
151
152         printf ("  Sector Start Addresses:");
153         for (i = 0; i < info->sector_count; ++i) {
154                 if ((i % 5) == 0)
155                         printf ("\n   ");
156                 printf (" %08lX%s",
157                         info->start[i],
158                         info->protect[i] ? " (RO)" : "     ");
159         }
160         printf ("\n");
161         return;
162 }
163
164 /*
165  * The following code cannot be run from FLASH!
166  */
167 static ulong flash_get_size (FPW *addr, flash_info_t *info)
168 {
169         volatile FPW value;
170
171         /* Write auto select command: read Manufacturer ID */
172         addr[0x5555] = (FPW) 0x00AA00AA;
173         addr[0x2AAA] = (FPW) 0x00550055;
174         addr[0x5555] = (FPW) 0x00900090;
175
176         mb ();
177         value = addr[0];
178
179         switch (value) {
180
181         case (FPW) INTEL_MANUFACT:
182                 info->flash_id = FLASH_MAN_INTEL;
183                 break;
184
185         default:
186                 info->flash_id = FLASH_UNKNOWN;
187                 info->sector_count = 0;
188                 info->size = 0;
189                 addr[0] = (FPW) 0x00FF00FF;     /* restore read mode */
190                 return (0);                     /* no or unknown flash  */
191         }
192
193         mb ();
194         value = addr[1];                        /* device ID        */
195
196         switch (value) {
197
198         case (FPW) INTEL_ID_28F128J3A:
199                 info->flash_id += FLASH_28F128J3A;
200                 info->sector_count = 128;
201                 info->size = 0x02000000;
202                 break;                          /* => 16 MB     */
203
204         default:
205                 info->flash_id = FLASH_UNKNOWN;
206                 break;
207         }
208
209         if (info->sector_count > CONFIG_SYS_MAX_FLASH_SECT) {
210                 printf ("** ERROR: sector count %d > max (%d) **\n",
211                         info->sector_count, CONFIG_SYS_MAX_FLASH_SECT);
212                 info->sector_count = CONFIG_SYS_MAX_FLASH_SECT;
213         }
214
215         addr[0] = (FPW) 0x00FF00FF;             /* restore read mode */
216
217         return (info->size);
218 }
219
220
221 /*-----------------------------------------------------------------------
222  */
223
224 int flash_erase (flash_info_t *info, int s_first, int s_last)
225 {
226         int flag, prot, sect;
227         ulong type, start, last;
228         int rcode = 0;
229
230         if ((s_first < 0) || (s_first > s_last)) {
231                 if (info->flash_id == FLASH_UNKNOWN) {
232                         printf ("- missing\n");
233                 } else {
234                         printf ("- no sectors to erase\n");
235                 }
236                 return 1;
237         }
238
239         type = (info->flash_id & FLASH_VENDMASK);
240         if ((type != FLASH_MAN_INTEL)) {
241                 printf ("Can't erase unknown flash type %08lx - aborted\n",
242                         info->flash_id);
243                 return 1;
244         }
245
246         prot = 0;
247         for (sect = s_first; sect <= s_last; ++sect) {
248                 if (info->protect[sect]) {
249                         prot++;
250                 }
251         }
252
253         if (prot) {
254                 printf ("- Warning: %d protected sectors will not be erased!\n",
255                         prot);
256         } else {
257                 printf ("\n");
258         }
259
260         start = get_timer (0);
261         last = start;
262
263         /* Disable interrupts which might cause a timeout here */
264         flag = disable_interrupts ();
265
266         /* Start erase on unprotected sectors */
267         for (sect = s_first; sect <= s_last; sect++) {
268                 if (info->protect[sect] == 0) { /* not protected */
269                         FPWV *addr = (FPWV *) (info->start[sect]);
270                         FPW status;
271
272                         printf ("Erasing sector %2d ... ", sect);
273
274                         /* arm simple, non interrupt dependent timer */
275                         reset_timer_masked ();
276
277                         *addr = (FPW) 0x00500050;       /* clear status register */
278                         *addr = (FPW) 0x00200020;       /* erase setup */
279                         *addr = (FPW) 0x00D000D0;       /* erase confirm */
280
281                         while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
282                                 if (get_timer_masked () > CONFIG_SYS_FLASH_ERASE_TOUT) {
283                                         printf ("Timeout\n");
284                                         *addr = (FPW) 0x00B000B0;       /* suspend erase     */
285                                         *addr = (FPW) 0x00FF00FF;       /* reset to read mode */
286                                         rcode = 1;
287                                         break;
288                                 }
289                         }
290
291                         *addr = 0x00500050;     /* clear status register cmd.   */
292                         *addr = 0x00FF00FF;     /* resest to read mode          */
293
294                         printf (" done\n");
295                 }
296         }
297         return rcode;
298 }
299
300 /*-----------------------------------------------------------------------
301  * Copy memory to flash, returns:
302  * 0 - OK
303  * 1 - write timeout
304  * 2 - Flash not erased
305  * 4 - Flash not identified
306  */
307
308 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
309 {
310         ulong cp, wp;
311         FPW data;
312         int count, i, l, rc, port_width;
313
314         if (info->flash_id == FLASH_UNKNOWN) {
315                 return 4;
316         }
317 /* get lower word aligned address */
318 #ifdef FLASH_PORT_WIDTH16
319         wp = (addr & ~1);
320         port_width = 2;
321 #else
322         wp = (addr & ~3);
323         port_width = 4;
324 #endif
325
326         /*
327          * handle unaligned start bytes
328          */
329         if ((l = addr - wp) != 0) {
330                 data = 0;
331                 for (i = 0, cp = wp; i < l; ++i, ++cp) {
332                         data = (data << 8) | (*(uchar *) cp);
333                 }
334                 for (; i < port_width && cnt > 0; ++i) {
335                         data = (data << 8) | *src++;
336                         --cnt;
337                         ++cp;
338                 }
339                 for (; cnt == 0 && i < port_width; ++i, ++cp) {
340                         data = (data << 8) | (*(uchar *) cp);
341                 }
342
343                 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
344                         return (rc);
345                 }
346                 wp += port_width;
347         }
348
349         /*
350          * handle word aligned part
351          */
352         count = 0;
353         while (cnt >= port_width) {
354                 data = 0;
355                 for (i = 0; i < port_width; ++i) {
356                         data = (data << 8) | *src++;
357                 }
358                 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
359                         return (rc);
360                 }
361                 wp += port_width;
362                 cnt -= port_width;
363                 if (count++ > 0x800) {
364                         spin_wheel ();
365                         count = 0;
366                 }
367         }
368
369         if (cnt == 0) {
370                 return (0);
371         }
372
373         /*
374          * handle unaligned tail bytes
375          */
376         data = 0;
377         for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
378                 data = (data << 8) | *src++;
379                 --cnt;
380         }
381         for (; i < port_width; ++i, ++cp) {
382                 data = (data << 8) | (*(uchar *) cp);
383         }
384
385         return (write_data (info, wp, SWAP (data)));
386 }
387
388 /*-----------------------------------------------------------------------
389  * Write a word or halfword to Flash, returns:
390  * 0 - OK
391  * 1 - write timeout
392  * 2 - Flash not erased
393  */
394 static int write_data (flash_info_t *info, ulong dest, FPW data)
395 {
396         FPWV *addr = (FPWV *) dest;
397         ulong status;
398         int flag;
399
400         /* Check if Flash is (sufficiently) erased */
401         if ((*addr & data) != data) {
402                 printf ("not erased at %08lx (%lx)\n", (ulong) addr, *addr);
403                 return (2);
404         }
405         /* Disable interrupts which might cause a timeout here */
406         flag = disable_interrupts ();
407
408         *addr = (FPW) 0x00400040;       /* write setup */
409         *addr = data;
410
411         /* arm simple, non interrupt dependent timer */
412         reset_timer_masked ();
413
414         /* wait while polling the status register */
415         while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
416                 if (get_timer_masked () > CONFIG_SYS_FLASH_WRITE_TOUT) {
417                         *addr = (FPW) 0x00FF00FF;       /* restore read mode */
418                         return (1);
419                 }
420         }
421
422         *addr = (FPW) 0x00FF00FF;       /* restore read mode */
423
424         return (0);
425 }
426
427 void inline spin_wheel (void)
428 {
429         static int p = 0;
430         static char w[] = "\\/-";
431
432         printf ("\010%c", w[p]);
433         (++p == 3) ? (p = 0) : 0;
434 }