* Patch by George G. Davis, 19 Aug 2003:
[platform/kernel/u-boot.git] / board / omap1610inn / 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  * (C) Copyright 2003
9  * Texas Instruments, <www.ti.com>
10  * Kshitij Gupta <Kshitij@ti.com>
11  *
12  * See file CREDITS for list of people who contributed to this
13  * project.
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License as
17  * published by the Free Software Foundation; either version 2 of
18  * the License, or (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28  * MA 02111-1307 USA
29  */
30
31 #include <common.h>
32 #include <linux/byteorder/swab.h>
33
34 #define PHYS_FLASH_SECT_SIZE    0x00020000      /* 256 KB sectors (x2) */
35 flash_info_t flash_info[CFG_MAX_FLASH_BANKS];   /* info for FLASH chips    */
36
37 /* Board support for 1 or 2 flash devices */
38 #undef FLASH_PORT_WIDTH32
39 #define FLASH_PORT_WIDTH16
40
41 #ifdef FLASH_PORT_WIDTH16
42 #define FLASH_PORT_WIDTH                ushort
43 #define FLASH_PORT_WIDTHV               vu_short
44 #define SWAP(x)                 __swab16(x)
45 #else
46 #define FLASH_PORT_WIDTH                ulong
47 #define FLASH_PORT_WIDTHV               vu_long
48 #define SWAP(x)                 __swab32(x)
49 #endif
50
51 #define FPW     FLASH_PORT_WIDTH
52 #define FPWV    FLASH_PORT_WIDTHV
53
54 #define mb() __asm__ __volatile__ ("" : : : "memory")
55
56
57
58 /* Flash Organization Structure */
59 typedef struct OrgDef {
60         unsigned int sector_number;
61         unsigned int sector_size;
62 } OrgDef;
63
64
65 /* Flash Organizations */
66 OrgDef OrgIntel_28F256L18T[] = {
67         {4, 32 * 1024},                         /* 4 * 32kBytes sectors */
68         {255, 128 * 1024},                      /* 255 * 128kBytes sectors */
69 };
70
71
72 /*-----------------------------------------------------------------------
73  * Functions
74  */
75 unsigned long flash_init (void);
76 static ulong flash_get_size (FPW * addr, flash_info_t * info);
77 static int write_data (flash_info_t * info, ulong dest, FPW data);
78 static void flash_get_offsets (ulong base, flash_info_t * info);
79 void inline spin_wheel (void);
80 void flash_print_info (flash_info_t * info);
81 void flash_unprotect_sectors (FPWV * addr);
82 int flash_erase (flash_info_t * info, int s_first, int s_last);
83 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt);
84
85 /*-----------------------------------------------------------------------
86  */
87
88 unsigned long flash_init (void)
89 {
90         int i;
91         ulong size = 0;
92         for (i = 0; i < CFG_MAX_FLASH_BANKS; i++) {
93                 switch (i) {
94                 case 0:
95                         flash_get_size ((FPW *) PHYS_FLASH_1, &flash_info[i]);
96                         flash_get_offsets (PHYS_FLASH_1, &flash_info[i]);
97                         break;
98                 default:
99                         panic ("configured to many flash banks!\n");
100                         break;
101                 }
102                 size += flash_info[i].size;
103         }
104
105         /* Protect monitor and environment sectors
106          */
107         flash_protect (FLAG_PROTECT_SET,
108                         CFG_FLASH_BASE,
109                         CFG_FLASH_BASE + monitor_flash_len - 1, &flash_info[0]);
110
111         flash_protect (FLAG_PROTECT_SET,
112                         CFG_ENV_ADDR,
113                         CFG_ENV_ADDR + CFG_ENV_SIZE - 1, &flash_info[0]);
114
115         return size;
116 }
117
118 /*-----------------------------------------------------------------------
119  */
120 static void flash_get_offsets (ulong base, flash_info_t * info)
121 {
122         int i;
123         OrgDef *pOrgDef;
124
125         pOrgDef = OrgIntel_28F256L18T;
126         if (info->flash_id == FLASH_UNKNOWN) {
127                 return;
128         }
129
130         if ((info->flash_id & FLASH_VENDMASK) == FLASH_MAN_INTEL) {
131                 for (i = 0; i < info->sector_count; i++) {
132                         if (i > 255) {
133                                 info->start[i] = base + (i * 0x8000);
134                                 info->protect[i] = 0;
135                         } else {
136                                 info->start[i] = base +
137                                                 (i * PHYS_FLASH_SECT_SIZE);
138                                 info->protect[i] = 0;
139                         }
140                 }
141         }
142 }
143
144 /*-----------------------------------------------------------------------
145  */
146 void flash_print_info (flash_info_t * info)
147 {
148         int i;
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_INTEL:
157                 printf ("INTEL ");
158                 break;
159         default:
160                 printf ("Unknown Vendor ");
161                 break;
162         }
163
164         switch (info->flash_id & FLASH_TYPEMASK) {
165         case FLASH_28F256L18T:
166                 printf ("FLASH 28F256L18T\n");
167                 break;
168         default:
169                 printf ("Unknown Chip Type\n");
170                 break;
171         }
172
173         printf ("  Size: %ld MB in %d Sectors\n",
174                         info->size >> 20, info->sector_count);
175
176         printf ("  Sector Start Addresses:");
177         for (i = 0; i < info->sector_count; ++i) {
178                 if ((i % 5) == 0)
179                         printf ("\n   ");
180                 printf (" %08lX%s",
181                         info->start[i], info->protect[i] ? " (RO)" : "     ");
182         }
183         printf ("\n");
184         return;
185 }
186
187 /*
188  * The following code cannot be run from FLASH!
189  */
190 static ulong flash_get_size (FPW * addr, flash_info_t * info)
191 {
192         volatile FPW value;
193
194         /* Write auto select command: read Manufacturer ID */
195         addr[0x5555] = (FPW) 0x00AA00AA;
196         addr[0x2AAA] = (FPW) 0x00550055;
197         addr[0x5555] = (FPW) 0x00900090;
198
199         mb ();
200         value = addr[0];
201
202         switch (value) {
203
204         case (FPW) INTEL_MANUFACT:
205                 info->flash_id = FLASH_MAN_INTEL;
206                 break;
207
208         default:
209                 info->flash_id = FLASH_UNKNOWN;
210                 info->sector_count = 0;
211                 info->size = 0;
212                 addr[0] = (FPW) 0x00FF00FF;     /* restore read mode */
213                 return (0);             /* no or unknown flash  */
214         }
215
216         mb ();
217         value = addr[1];        /* device ID        */
218         switch (value) {
219
220         case (FPW) (INTEL_ID_28F256L18T):
221                 info->flash_id += FLASH_28F256L18T;
222                 info->sector_count = 259;
223                 info->size = 0x02000000;
224                 break;                  /* => 32 MB     */
225
226         default:
227                 info->flash_id = FLASH_UNKNOWN;
228                 break;
229         }
230
231         if (info->sector_count > CFG_MAX_FLASH_SECT) {
232                 printf ("** ERROR: sector count %d > max (%d) **\n",
233                                 info->sector_count, CFG_MAX_FLASH_SECT);
234                 info->sector_count = CFG_MAX_FLASH_SECT;
235         }
236
237         addr[0] = (FPW) 0x00FF00FF;     /* restore read mode */
238
239         return (info->size);
240 }
241
242
243
244
245 /* unprotects a sector for write and erase
246  * on some intel parts, this unprotects the entire chip, but it
247  * wont hurt to call this additional times per sector...
248  */
249 void flash_unprotect_sectors (FPWV * addr)
250 {
251 #define PD_FINTEL_WSMS_READY_MASK    0x0080
252
253         *addr = (FPW) 0x00500050;       /* clear status register */
254
255         /* this sends the clear lock bit command */
256         *addr = (FPW) 0x00600060;
257         *addr = (FPW) 0x00D000D0;
258 }
259
260
261 /*-----------------------------------------------------------------------
262  */
263
264 int flash_erase (flash_info_t * info, int s_first, int s_last)
265 {
266         int flag, prot, sect;
267         ulong type, start, last;
268         int rcode = 0;
269
270         if ((s_first < 0) || (s_first > s_last)) {
271                 if (info->flash_id == FLASH_UNKNOWN) {
272                         printf ("- missing\n");
273                 } else {
274                         printf ("- no sectors to erase\n");
275                 }
276                 return 1;
277         }
278
279         type = (info->flash_id & FLASH_VENDMASK);
280         if ((type != FLASH_MAN_INTEL)) {
281                 printf ("Can't erase unknown flash type %08lx - aborted\n",
282                                 info->flash_id);
283                 return 1;
284         }
285
286         prot = 0;
287         for (sect = s_first; sect <= s_last; ++sect) {
288                 if (info->protect[sect]) {
289                         prot++;
290                 }
291         }
292
293         if (prot) {
294                 printf ("- Warning: %d protected sectors will not be erased!\n",
295                                 prot);
296         } else {
297                 printf ("\n");
298         }
299
300
301
302
303         start = get_timer (0);
304         last = start;
305
306         /* Disable interrupts which might cause a timeout here */
307         flag = disable_interrupts ();
308
309         /* Start erase on unprotected sectors */
310         for (sect = s_first; sect <= s_last; sect++) {
311                 if (info->protect[sect] == 0) { /* not protected */
312                         FPWV *addr = (FPWV *) (info->start[sect]);
313                         FPW status;
314
315                         printf ("Erasing sector %2d ... ", sect);
316
317                         flash_unprotect_sectors (addr);
318
319                         /* arm simple, non interrupt dependent timer */
320                         reset_timer_masked ();
321
322                         *addr = (FPW) 0x00500050;/* clear status register */
323                         *addr = (FPW) 0x00200020;/* erase setup */
324                         *addr = (FPW) 0x00D000D0;/* erase confirm */
325
326                         while (((status =
327                                 *addr) & (FPW) 0x00800080) !=
328                                 (FPW) 0x00800080) {
329                                         if (get_timer_masked () > 
330                                         CFG_FLASH_ERASE_TOUT) {
331                                         printf ("Timeout\n");
332                                         /* suspend erase     */
333                                         *addr = (FPW) 0x00B000B0;
334                                         /* reset to read mode */
335                                         *addr = (FPW) 0x00FF00FF;
336                                         rcode = 1;
337                                         break;
338                                 }
339                         }
340
341                         /* clear status register cmd.   */
342                         *addr = (FPW) 0x00500050;
343                         *addr = (FPW) 0x00FF00FF;/* resest to read mode */
344                         printf (" done\n");
345                 }
346         }
347         return rcode;
348 }
349
350 /*-----------------------------------------------------------------------
351  * Copy memory to flash, returns:
352  * 0 - OK
353  * 1 - write timeout
354  * 2 - Flash not erased
355  * 4 - Flash not identified
356  */
357
358 int write_buff (flash_info_t * info, uchar * src, ulong addr, ulong cnt)
359 {
360         ulong cp, wp;
361         FPW data;
362         int count, i, l, rc, port_width;
363
364         if (info->flash_id == FLASH_UNKNOWN) {
365                 return 4;
366         }
367 /* get lower word aligned address */
368 #ifdef FLASH_PORT_WIDTH16
369         wp = (addr & ~1);
370         port_width = 2;
371 #else
372         wp = (addr & ~3);
373         port_width = 4;
374 #endif
375
376         /*
377          * handle unaligned start bytes
378          */
379         if ((l = addr - wp) != 0) {
380                 data = 0;
381                 for (i = 0, cp = wp; i < l; ++i, ++cp) {
382                         data = (data << 8) | (*(uchar *) cp);
383                 }
384                 for (; i < port_width && cnt > 0; ++i) {
385                         data = (data << 8) | *src++;
386                         --cnt;
387                         ++cp;
388                 }
389                 for (; cnt == 0 && i < port_width; ++i, ++cp) {
390                         data = (data << 8) | (*(uchar *) cp);
391                 }
392
393                 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
394                         return (rc);
395                 }
396                 wp += port_width;
397         }
398
399         /*
400          * handle word aligned part
401          */
402         count = 0;
403         while (cnt >= port_width) {
404                 data = 0;
405                 for (i = 0; i < port_width; ++i) {
406                         data = (data << 8) | *src++;
407                 }
408                 if ((rc = write_data (info, wp, SWAP (data))) != 0) {
409                         return (rc);
410                 }
411                 wp += port_width;
412                 cnt -= port_width;
413                 if (count++ > 0x800) {
414                         spin_wheel ();
415                         count = 0;
416                 }
417         }
418
419         if (cnt == 0) {
420                 return (0);
421         }
422
423         /*
424          * handle unaligned tail bytes
425          */
426         data = 0;
427         for (i = 0, cp = wp; i < port_width && cnt > 0; ++i, ++cp) {
428                 data = (data << 8) | *src++;
429                 --cnt;
430         }
431         for (; i < port_width; ++i, ++cp) {
432                 data = (data << 8) | (*(uchar *) cp);
433         }
434
435         return (write_data (info, wp, SWAP (data)));
436 }
437
438 /*-----------------------------------------------------------------------
439  * Write a word or halfword to Flash, returns:
440  * 0 - OK
441  * 1 - write timeout
442  * 2 - Flash not erased
443  */
444 static int write_data (flash_info_t * info, ulong dest, FPW data)
445 {
446         FPWV *addr = (FPWV *) dest;
447         ulong status;
448         int flag;
449
450         /* Check if Flash is (sufficiently) erased */
451         if ((*addr & data) != data) {
452                 printf ("not erased at %08lx (%x)\n", (ulong) addr, *addr);
453                 return (2);
454         }
455         flash_unprotect_sectors (addr);
456         /* Disable interrupts which might cause a timeout here */
457         flag = disable_interrupts ();
458         *addr = (FPW) 0x00400040;       /* write setup */
459         *addr = data;
460
461         /* arm simple, non interrupt dependent timer */
462         reset_timer_masked ();
463
464         /* wait while polling the status register */
465         while (((status = *addr) & (FPW) 0x00800080) != (FPW) 0x00800080) {
466                 if (get_timer_masked () > CFG_FLASH_WRITE_TOUT) {
467                         *addr = (FPW) 0x00FF00FF;       /* restore read mode */
468                         return (1);
469                 }
470         }
471         *addr = (FPW) 0x00FF00FF;       /* restore read mode */
472         return (0);
473 }
474
475 void inline spin_wheel (void)
476 {
477         static int p = 0;
478         static char w[] = "\\/-";
479
480         printf ("\010%c", w[p]);
481         (++p == 3) ? (p = 0) : 0;
482 }