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