]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/gw8260/flash.c
board/gw8260/flash.c: minimal CodingStyle cleanup
[karo-tx-uboot.git] / board / gw8260 / flash.c
1 /*
2  * (C) Copyright 2000
3  * Marius Groeger <mgroeger@sysgo.de>
4  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
5  *
6  * (C) Copyright 2000, 2001
7  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8  *
9  * (C) Copyright 2001
10  * Advent Networks, Inc. <http://www.adventnetworks.com>
11  * Oliver Brown <oliverb@alumni.utexas.net>
12  *
13  *--------------------------------------------------------------------
14  * See file CREDITS for list of people who contributed to this
15  * project.
16  *
17  * This program is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU General Public License as
19  * published by the Free Software Foundation; either version 2 of
20  * the License, or (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30  * MA 02111-1307 USA
31  */
32
33 /*********************************************************************/
34 /* DESCRIPTION:
35  *   This file contains the flash routines for the GW8260 board.
36  *
37  *
38  *
39  * MODULE DEPENDENCY:
40  *   None
41  *
42  *
43  * RESTRICTIONS/LIMITATIONS:
44  *
45  *   Only supports the following flash devices:
46  *     AMD 29F080B
47  *     AMD 29F016D
48  *
49  * Copyright (c) 2001, Advent Networks, Inc.
50  *
51  */
52 /*********************************************************************/
53
54 #include <common.h>
55 #include <mpc8260.h>
56
57 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
58
59 static ulong flash_get_size (vu_long *addr, flash_info_t *info);
60 static int write_word (flash_info_t *info, ulong dest, ulong data);
61
62 /*********************************************************************/
63 /*                    functions                                      */
64 /*********************************************************************/
65
66 /*
67  * NAME: flash_init() -  initializes flash banks
68  *
69  * DESCRIPTION:
70  *   This function initializes the flash bank(s).
71  *
72  * RETURNS:
73  *   The size in bytes of the flash
74  *
75  * RESTRICTIONS/LIMITATIONS:
76  *
77  *
78  */
79 unsigned long flash_init(void)
80 {
81         unsigned long size;
82         int i;
83
84         /* Init: no FLASHes known */
85         for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i)
86                 flash_info[i].flash_id = FLASH_UNKNOWN;
87
88         /* for now, only support the 4 MB Flash SIMM */
89         size = flash_get_size((vu_long *) CONFIG_SYS_FLASH0_BASE,
90                               &flash_info[0]);
91         /*
92          * protect monitor and environment sectors
93          */
94 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH0_BASE
95         flash_protect(FLAG_PROTECT_SET,
96                       CONFIG_SYS_MONITOR_BASE,
97                       CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
98                       &flash_info[0]);
99 #endif
100
101 #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CONFIG_ENV_ADDR)
102 #ifndef CONFIG_ENV_SIZE
103 #define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
104 #endif
105         flash_protect(FLAG_PROTECT_SET,
106                       CONFIG_ENV_ADDR,
107                       CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
108 #endif
109
110         return CONFIG_SYS_FLASH0_SIZE * 1024 * 1024;    /*size */
111 }
112
113 /*********************************************************************/
114 /* NAME: flash_print_info() - prints flash imformation               */
115 /*                                                                   */
116 /* DESCRIPTION:                                                      */
117 /*   This function prints the flash information.                     */
118 /*                                                                   */
119 /* INPUTS:                                                           */
120 /*   flash_info_t *info - flash information structure                */
121 /*                                                                   */
122 /* OUTPUTS:                                                          */
123 /*   Displays flash information to console                           */
124 /*                                                                   */
125 /* RETURNS:                                                          */
126 /*   None                                                            */
127 /*                                                                   */
128 /* RESTRICTIONS/LIMITATIONS:                                         */
129 /*                                                                   */
130 /*                                                                   */
131 /*********************************************************************/
132 void flash_print_info  (flash_info_t *info)
133 {
134     int i;
135
136     if (info->flash_id == FLASH_UNKNOWN) {
137         printf ("missing or unknown FLASH type\n");
138         return;
139     }
140
141     switch ((info->flash_id >> 16) & 0xff) {
142     case 0x1:
143         printf ("AMD ");
144         break;
145     default:
146         printf ("Unknown Vendor ");
147         break;
148     }
149
150     switch (info->flash_id & FLASH_TYPEMASK) {
151     case AMD_ID_F040B:
152         printf ("AM29F040B (4 Mbit)\n");
153         break;
154     case AMD_ID_F080B:
155         printf ("AM29F080B (8 Mbit)\n");
156         break;
157     case AMD_ID_F016D:
158         printf ("AM29F016D (16 Mbit)\n");
159         break;
160     default:
161         printf ("Unknown Chip Type\n");
162         break;
163     }
164
165     printf ("  Size: %ld MB in %d Sectors\n",
166             info->size >> 20, info->sector_count);
167
168     printf ("  Sector Start Addresses:");
169     for (i=0; i<info->sector_count; ++i) {
170         if ((i % 5) == 0)
171             printf ("\n   ");
172         printf (" %08lX%s",
173                 info->start[i],
174                 info->protect[i] ? " (RO)" : "     "
175             );
176     }
177     printf ("\n");
178     return;
179 }
180
181 /*********************************************************************/
182 /*   The following code cannot be run from FLASH!                    */
183 /*********************************************************************/
184
185 /*********************************************************************/
186 /* NAME: flash_get_size() - detects the flash size                   */
187 /*                                                                   */
188 /* DESCRIPTION:                                                      */
189 /*   1) Reads vendor ID and devices ID from the flash devices.       */
190 /*   2) Initializes flash info struct.                               */
191 /*   3) Return the flash size                                        */
192 /*                                                                   */
193 /* INPUTS:                                                           */
194 /*   vu_long *addr      - pointer to start of flash                  */
195 /*   flash_info_t *info - flash information structure                */
196 /*                                                                   */
197 /* OUTPUTS:                                                          */
198 /*   None                                                            */
199 /*                                                                   */
200 /* RETURNS:                                                          */
201 /*   Size of the flash in bytes, or 0 if device id is unknown.       */
202 /*                                                                   */
203 /* RESTRICTIONS/LIMITATIONS:                                         */
204 /*   Only supports the following devices:                            */
205 /*     AM29F080D                                                     */
206 /*     AM29F016D                                                     */
207 /*                                                                   */
208 /*********************************************************************/
209 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
210 {
211     short i;
212     vu_long vendor, devid;
213     ulong base = (ulong)addr;
214
215     /*printf("addr   = %08lx\n", (unsigned long)addr); */
216
217     /* Reset and Write auto select command: read Manufacturer ID */
218     addr[0x0000] = 0xf0f0f0f0;
219     addr[0x0555] = 0xAAAAAAAA;
220     addr[0x02AA] = 0x55555555;
221     addr[0x0555] = 0x90909090;
222     udelay (1000);
223
224     vendor = addr[0];
225     /*printf("vendor = %08lx\n", vendor); */
226     if (vendor != 0x01010101) {
227         info->size = 0;
228         goto out;
229     }
230
231     devid = addr[1];
232     /*printf("devid  = %08lx\n", devid); */
233
234     if ((devid & 0xff) == AMD_ID_F080B) {
235         info->flash_id     = (vendor & 0xff) << 16 | AMD_ID_F080B;
236         /* we have 16 sectors with 64KB each x 4 */
237         info->sector_count = 16;
238         info->size         = 4 * info->sector_count * 64*1024;
239     } else if ((devid & 0xff) == AMD_ID_F016D){
240         info->flash_id     = (vendor & 0xff) << 16 | AMD_ID_F016D;
241         /* we have 32 sectors with 64KB each x 4 */
242         info->sector_count = 32;
243         info->size         = 4 * info->sector_count * 64*1024;
244     } else {
245         info->size = 0;
246         goto out;
247     }
248     /*printf("sector count = %08x\n", info->sector_count); */
249     /* check for protected sectors */
250     for (i = 0; i < info->sector_count; i++) {
251         /* sector base address */
252         info->start[i] = base + i * (info->size / info->sector_count);
253         /* read sector protection at sector address, (A7 .. A0) = 0x02 */
254         /* D0 = 1 if protected */
255         addr = (volatile unsigned long *)(info->start[i]);
256         info->protect[i] = addr[2] & 1;
257     }
258
259     /* reset command */
260     addr = (vu_long *)info->start[0];
261
262   out:
263     addr[0] = 0xf0f0f0f0;
264
265     /*printf("size = %08x\n", info->size); */
266     return info->size;
267 }
268
269 /*********************************************************************/
270 /* NAME: flash_erase() - erases flash by sector                      */
271 /*                                                                   */
272 /* DESCRIPTION:                                                      */
273 /*   This function erases flash sectors starting for s_first to      */
274 /*   s_last.                                                         */
275 /*                                                                   */
276 /* INPUTS:                                                           */
277 /*   flash_info_t *info - flash information structure                */
278 /*   int s_first - first sector to erase                             */
279 /*   int s_last  - last sector to erase                              */
280 /*                                                                   */
281 /* OUTPUTS:                                                          */
282 /*   None                                                            */
283 /*                                                                   */
284 /* RETURNS:                                                          */
285 /*   Returns 0 for success, 1 for failure.                           */
286 /*                                                                   */
287 /* RESTRICTIONS/LIMITATIONS:                                         */
288 /*                                                                   */
289 /*********************************************************************/
290 int flash_erase (flash_info_t *info, int s_first, int s_last)
291 {
292     vu_long *addr = (vu_long*)(info->start[0]);
293     int flag, prot, sect, l_sect;
294     ulong start, now, last;
295
296     if ((s_first < 0) || (s_first > s_last)) {
297         if (info->flash_id == FLASH_UNKNOWN) {
298             printf ("- missing\n");
299         } else {
300             printf ("- no sectors to erase\n");
301         }
302         return 1;
303     }
304
305     prot = 0;
306     for (sect = s_first; sect <= s_last; sect++) {
307         if (info->protect[sect]) {
308             prot++;
309         }
310     }
311
312     if (prot) {
313         printf ("- Warning: %d protected sectors will not be erased!\n",
314                 prot);
315     } else {
316         printf ("\n");
317     }
318
319     l_sect = -1;
320
321     /* Disable interrupts which might cause a timeout here */
322     flag = disable_interrupts();
323
324     addr[0x0555] = 0xAAAAAAAA;
325     addr[0x02AA] = 0x55555555;
326     addr[0x0555] = 0x80808080;
327     addr[0x0555] = 0xAAAAAAAA;
328     addr[0x02AA] = 0x55555555;
329     udelay (100);
330
331     /* Start erase on unprotected sectors */
332     for (sect = s_first; sect <= s_last; sect++) {
333         if (info->protect[sect] == 0) { /* not protected */
334             addr = (vu_long*)(info->start[sect]);
335             addr[0] = 0x30303030;
336             l_sect = sect;
337         }
338     }
339
340     /* re-enable interrupts if necessary */
341     if (flag)
342         enable_interrupts();
343
344     /* wait at least 80us - let's wait 1 ms */
345     udelay (1000);
346
347     /*
348      * We wait for the last triggered sector
349      */
350     if (l_sect < 0)
351         goto DONE;
352
353     start = get_timer (0);
354     last  = start;
355     addr = (vu_long*)(info->start[l_sect]);
356     while ((addr[0] & 0x80808080) != 0x80808080) {
357         if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
358             printf ("Timeout\n");
359             return 1;
360         }
361         /* show that we're waiting */
362         if ((now - last) > 1000) {  /* every second */
363             serial_putc ('.');
364             last = now;
365         }
366     }
367
368   DONE:
369     /* reset to read mode */
370     addr = (volatile unsigned long *)info->start[0];
371     addr[0] = 0xF0F0F0F0;   /* reset bank */
372
373     printf (" done\n");
374     return 0;
375 }
376
377 /*********************************************************************/
378 /* NAME: write_buff() - writes a buffer to flash                     */
379 /*                                                                   */
380 /* DESCRIPTION:                                                      */
381 /*   This function copies a buffer, *src, to flash.                  */
382 /*                                                                   */
383 /* INPUTS:                                                           */
384 /*  flash_info_t *info - flash information structure                 */
385 /*  uchar *src - pointer to buffer to write to flash                 */
386 /*  ulong addr - address to start write at                           */
387 /*  ulong cnt - number of bytes to write to flash                    */
388 /*                                                                   */
389 /* OUTPUTS:                                                          */
390 /*   None                                                            */
391 /*                                                                   */
392 /* RETURNS:                                                          */
393 /*   0 - OK                                                          */
394 /*   1 - write timeout                                               */
395 /*   2 - Flash not erased                                            */
396 /*                                                                   */
397 /* RESTRICTIONS/LIMITATIONS:                                         */
398 /*                                                                   */
399 /*********************************************************************/
400 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
401 {
402     ulong cp, wp, data;
403     int i, l, rc;
404
405     wp = (addr & ~3);   /* get lower word aligned address */
406
407     /*
408      * handle unaligned start bytes
409      */
410     if ((l = addr - wp) != 0) {
411         data = 0;
412         for (i = 0, cp = wp; i < l; ++i, ++cp) {
413             data = (data << 8) | (*(uchar *)cp);
414         }
415         for (; (i < 4) && (cnt > 0); ++i) {
416             data = (data << 8) | *src++;
417             --cnt;
418             ++cp;
419         }
420         for (; (cnt == 0) && (i < 4); ++i, ++cp) {
421             data = (data << 8) | (*(uchar *)cp);
422         }
423
424         if ((rc = write_word(info, wp, data)) != 0) {
425             return (rc);
426         }
427         wp += 4;
428     }
429
430     /*
431      * handle word aligned part
432      */
433     while (cnt >= 4) {
434         data = 0;
435         for (i = 0; i < 4; ++i) {
436             data = (data << 8) | *src++;
437         }
438         if ((rc = write_word(info, wp, data)) != 0) {
439             return (rc);
440         }
441         wp  += 4;
442         cnt -= 4;
443     }
444
445     if (cnt == 0) {
446         return (0);
447     }
448
449     /*
450      * handle unaligned tail bytes
451      */
452     data = 0;
453     for (i = 0, cp = wp; (i < 4) && (cnt > 0); ++i, ++cp) {
454         data = (data << 8) | *src++;
455         --cnt;
456     }
457     for (; (i < 4); ++i, ++cp) {
458         data = (data << 8) | (*(uchar *)cp);
459     }
460
461     return (write_word(info, wp, data));
462 }
463
464 /*********************************************************************/
465 /* NAME: write_word() - writes a word to flash                       */
466 /*                                                                   */
467 /* DESCRIPTION:                                                      */
468 /*   This writes a single word to flash.                             */
469 /*                                                                   */
470 /* INPUTS:                                                           */
471 /*  flash_info_t *info - flash information structure                 */
472 /*  ulong dest - address to write                                    */
473 /*  ulong data - data to write                                       */
474 /*                                                                   */
475 /* OUTPUTS:                                                          */
476 /*   None                                                            */
477 /*                                                                   */
478 /* RETURNS:                                                          */
479 /*   0 - OK                                                          */
480 /*   1 - write timeout                                               */
481 /*   2 - Flash not erased                                            */
482 /*                                                                   */
483 /* RESTRICTIONS/LIMITATIONS:                                         */
484 /*                                                                   */
485 /*********************************************************************/
486 static int write_word (flash_info_t *info, ulong dest, ulong data)
487 {
488     vu_long *addr = (vu_long*)(info->start[0]);
489     ulong start;
490     int flag;
491
492     /* Check if Flash is (sufficiently) erased */
493     if ((*((vu_long *)dest) & data) != data) {
494         return (2);
495     }
496     /* Disable interrupts which might cause a timeout here */
497     flag = disable_interrupts();
498
499     addr[0x0555] = 0xAAAAAAAA;
500     addr[0x02AA] = 0x55555555;
501     addr[0x0555] = 0xA0A0A0A0;
502
503     *((vu_long *)dest) = data;
504
505     /* re-enable interrupts if necessary */
506     if (flag)
507         enable_interrupts();
508
509     /* data polling for D7 */
510     start = get_timer (0);
511     while ((*((vu_long *)dest) & 0x80808080) != (data & 0x80808080)) {
512         if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
513             return (1);
514         }
515     }
516     return (0);
517 }
518 /*********************************************************************/
519 /*                         End of flash.c                            */
520 /*********************************************************************/