]> git.sur5r.net Git - u-boot/blob - board/gw8260/flash.c
powerpc/83xx/km: make local functions and structs static
[u-boot] / 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         int i;
82
83         /* Init: no FLASHes known */
84         for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i)
85                 flash_info[i].flash_id = FLASH_UNKNOWN;
86
87         /* for now, only support the 4 MB Flash SIMM */
88         (void)flash_get_size((vu_long *) CONFIG_SYS_FLASH0_BASE,
89                               &flash_info[0]);
90         /*
91          * protect monitor and environment sectors
92          */
93 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH0_BASE
94         flash_protect(FLAG_PROTECT_SET,
95                       CONFIG_SYS_MONITOR_BASE,
96                       CONFIG_SYS_MONITOR_BASE + monitor_flash_len - 1,
97                       &flash_info[0]);
98 #endif
99
100 #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CONFIG_ENV_ADDR)
101 #ifndef CONFIG_ENV_SIZE
102 #define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
103 #endif
104         flash_protect(FLAG_PROTECT_SET,
105                       CONFIG_ENV_ADDR,
106                       CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
107 #endif
108
109         return CONFIG_SYS_FLASH0_SIZE * 1024 * 1024;    /*size */
110 }
111
112 /*********************************************************************/
113 /* NAME: flash_print_info() - prints flash imformation               */
114 /*                                                                   */
115 /* DESCRIPTION:                                                      */
116 /*   This function prints the flash information.                     */
117 /*                                                                   */
118 /* INPUTS:                                                           */
119 /*   flash_info_t *info - flash information structure                */
120 /*                                                                   */
121 /* OUTPUTS:                                                          */
122 /*   Displays flash information to console                           */
123 /*                                                                   */
124 /* RETURNS:                                                          */
125 /*   None                                                            */
126 /*                                                                   */
127 /* RESTRICTIONS/LIMITATIONS:                                         */
128 /*                                                                   */
129 /*                                                                   */
130 /*********************************************************************/
131 void flash_print_info  (flash_info_t *info)
132 {
133     int i;
134
135     if (info->flash_id == FLASH_UNKNOWN) {
136         printf ("missing or unknown FLASH type\n");
137         return;
138     }
139
140     switch ((info->flash_id >> 16) & 0xff) {
141     case 0x1:
142         printf ("AMD ");
143         break;
144     default:
145         printf ("Unknown Vendor ");
146         break;
147     }
148
149     switch (info->flash_id & FLASH_TYPEMASK) {
150     case AMD_ID_F040B:
151         printf ("AM29F040B (4 Mbit)\n");
152         break;
153     case AMD_ID_F080B:
154         printf ("AM29F080B (8 Mbit)\n");
155         break;
156     case AMD_ID_F016D:
157         printf ("AM29F016D (16 Mbit)\n");
158         break;
159     default:
160         printf ("Unknown Chip Type\n");
161         break;
162     }
163
164     printf ("  Size: %ld MB in %d Sectors\n",
165             info->size >> 20, info->sector_count);
166
167     printf ("  Sector Start Addresses:");
168     for (i=0; i<info->sector_count; ++i) {
169         if ((i % 5) == 0)
170             printf ("\n   ");
171         printf (" %08lX%s",
172                 info->start[i],
173                 info->protect[i] ? " (RO)" : "     "
174             );
175     }
176     printf ("\n");
177     return;
178 }
179
180 /*********************************************************************/
181 /*   The following code cannot be run from FLASH!                    */
182 /*********************************************************************/
183
184 /*********************************************************************/
185 /* NAME: flash_get_size() - detects the flash size                   */
186 /*                                                                   */
187 /* DESCRIPTION:                                                      */
188 /*   1) Reads vendor ID and devices ID from the flash devices.       */
189 /*   2) Initializes flash info struct.                               */
190 /*   3) Return the flash size                                        */
191 /*                                                                   */
192 /* INPUTS:                                                           */
193 /*   vu_long *addr      - pointer to start of flash                  */
194 /*   flash_info_t *info - flash information structure                */
195 /*                                                                   */
196 /* OUTPUTS:                                                          */
197 /*   None                                                            */
198 /*                                                                   */
199 /* RETURNS:                                                          */
200 /*   Size of the flash in bytes, or 0 if device id is unknown.       */
201 /*                                                                   */
202 /* RESTRICTIONS/LIMITATIONS:                                         */
203 /*   Only supports the following devices:                            */
204 /*     AM29F080D                                                     */
205 /*     AM29F016D                                                     */
206 /*                                                                   */
207 /*********************************************************************/
208 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
209 {
210     short i;
211     vu_long vendor, devid;
212     ulong base = (ulong)addr;
213
214     /*printf("addr   = %08lx\n", (unsigned long)addr); */
215
216     /* Reset and Write auto select command: read Manufacturer ID */
217     addr[0x0000] = 0xf0f0f0f0;
218     addr[0x0555] = 0xAAAAAAAA;
219     addr[0x02AA] = 0x55555555;
220     addr[0x0555] = 0x90909090;
221     udelay (1000);
222
223     vendor = addr[0];
224     /*printf("vendor = %08lx\n", vendor); */
225     if (vendor != 0x01010101) {
226         info->size = 0;
227         goto out;
228     }
229
230     devid = addr[1];
231     /*printf("devid  = %08lx\n", devid); */
232
233     if ((devid & 0xff) == AMD_ID_F080B) {
234         info->flash_id     = (vendor & 0xff) << 16 | AMD_ID_F080B;
235         /* we have 16 sectors with 64KB each x 4 */
236         info->sector_count = 16;
237         info->size         = 4 * info->sector_count * 64*1024;
238     } else if ((devid & 0xff) == AMD_ID_F016D){
239         info->flash_id     = (vendor & 0xff) << 16 | AMD_ID_F016D;
240         /* we have 32 sectors with 64KB each x 4 */
241         info->sector_count = 32;
242         info->size         = 4 * info->sector_count * 64*1024;
243     } else {
244         info->size = 0;
245         goto out;
246     }
247     /*printf("sector count = %08x\n", info->sector_count); */
248     /* check for protected sectors */
249     for (i = 0; i < info->sector_count; i++) {
250         /* sector base address */
251         info->start[i] = base + i * (info->size / info->sector_count);
252         /* read sector protection at sector address, (A7 .. A0) = 0x02 */
253         /* D0 = 1 if protected */
254         addr = (volatile unsigned long *)(info->start[i]);
255         info->protect[i] = addr[2] & 1;
256     }
257
258     /* reset command */
259     addr = (vu_long *)info->start[0];
260
261   out:
262     addr[0] = 0xf0f0f0f0;
263
264     /*printf("size = %08x\n", info->size); */
265     return info->size;
266 }
267
268 /*********************************************************************/
269 /* NAME: flash_erase() - erases flash by sector                      */
270 /*                                                                   */
271 /* DESCRIPTION:                                                      */
272 /*   This function erases flash sectors starting for s_first to      */
273 /*   s_last.                                                         */
274 /*                                                                   */
275 /* INPUTS:                                                           */
276 /*   flash_info_t *info - flash information structure                */
277 /*   int s_first - first sector to erase                             */
278 /*   int s_last  - last sector to erase                              */
279 /*                                                                   */
280 /* OUTPUTS:                                                          */
281 /*   None                                                            */
282 /*                                                                   */
283 /* RETURNS:                                                          */
284 /*   Returns 0 for success, 1 for failure.                           */
285 /*                                                                   */
286 /* RESTRICTIONS/LIMITATIONS:                                         */
287 /*                                                                   */
288 /*********************************************************************/
289 int flash_erase (flash_info_t *info, int s_first, int s_last)
290 {
291     vu_long *addr = (vu_long*)(info->start[0]);
292     int flag, prot, sect, l_sect;
293     ulong start, now, last;
294
295     if ((s_first < 0) || (s_first > s_last)) {
296         if (info->flash_id == FLASH_UNKNOWN) {
297             printf ("- missing\n");
298         } else {
299             printf ("- no sectors to erase\n");
300         }
301         return 1;
302     }
303
304     prot = 0;
305     for (sect = s_first; sect <= s_last; sect++) {
306         if (info->protect[sect]) {
307             prot++;
308         }
309     }
310
311     if (prot) {
312         printf ("- Warning: %d protected sectors will not be erased!\n",
313                 prot);
314     } else {
315         printf ("\n");
316     }
317
318     l_sect = -1;
319
320     /* Disable interrupts which might cause a timeout here */
321     flag = disable_interrupts();
322
323     addr[0x0555] = 0xAAAAAAAA;
324     addr[0x02AA] = 0x55555555;
325     addr[0x0555] = 0x80808080;
326     addr[0x0555] = 0xAAAAAAAA;
327     addr[0x02AA] = 0x55555555;
328     udelay (100);
329
330     /* Start erase on unprotected sectors */
331     for (sect = s_first; sect <= s_last; sect++) {
332         if (info->protect[sect] == 0) { /* not protected */
333             addr = (vu_long*)(info->start[sect]);
334             addr[0] = 0x30303030;
335             l_sect = sect;
336         }
337     }
338
339     /* re-enable interrupts if necessary */
340     if (flag)
341         enable_interrupts();
342
343     /* wait at least 80us - let's wait 1 ms */
344     udelay (1000);
345
346     /*
347      * We wait for the last triggered sector
348      */
349     if (l_sect < 0)
350         goto DONE;
351
352     start = get_timer (0);
353     last  = start;
354     addr = (vu_long*)(info->start[l_sect]);
355     while ((addr[0] & 0x80808080) != 0x80808080) {
356         if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
357             printf ("Timeout\n");
358             return 1;
359         }
360         /* show that we're waiting */
361         if ((now - last) > 1000) {  /* every second */
362             serial_putc ('.');
363             last = now;
364         }
365     }
366
367   DONE:
368     /* reset to read mode */
369     addr = (volatile unsigned long *)info->start[0];
370     addr[0] = 0xF0F0F0F0;   /* reset bank */
371
372     printf (" done\n");
373     return 0;
374 }
375
376 /*********************************************************************/
377 /* NAME: write_buff() - writes a buffer to flash                     */
378 /*                                                                   */
379 /* DESCRIPTION:                                                      */
380 /*   This function copies a buffer, *src, to flash.                  */
381 /*                                                                   */
382 /* INPUTS:                                                           */
383 /*  flash_info_t *info - flash information structure                 */
384 /*  uchar *src - pointer to buffer to write to flash                 */
385 /*  ulong addr - address to start write at                           */
386 /*  ulong cnt - number of bytes to write to flash                    */
387 /*                                                                   */
388 /* OUTPUTS:                                                          */
389 /*   None                                                            */
390 /*                                                                   */
391 /* RETURNS:                                                          */
392 /*   0 - OK                                                          */
393 /*   1 - write timeout                                               */
394 /*   2 - Flash not erased                                            */
395 /*                                                                   */
396 /* RESTRICTIONS/LIMITATIONS:                                         */
397 /*                                                                   */
398 /*********************************************************************/
399 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
400 {
401     ulong cp, wp, data;
402     int i, l, rc;
403
404     wp = (addr & ~3);   /* get lower word aligned address */
405
406     /*
407      * handle unaligned start bytes
408      */
409     if ((l = addr - wp) != 0) {
410         data = 0;
411         for (i = 0, cp = wp; i < l; ++i, ++cp) {
412             data = (data << 8) | (*(uchar *)cp);
413         }
414         for (; (i < 4) && (cnt > 0); ++i) {
415             data = (data << 8) | *src++;
416             --cnt;
417             ++cp;
418         }
419         for (; (cnt == 0) && (i < 4); ++i, ++cp) {
420             data = (data << 8) | (*(uchar *)cp);
421         }
422
423         if ((rc = write_word(info, wp, data)) != 0) {
424             return (rc);
425         }
426         wp += 4;
427     }
428
429     /*
430      * handle word aligned part
431      */
432     while (cnt >= 4) {
433         data = 0;
434         for (i = 0; i < 4; ++i) {
435             data = (data << 8) | *src++;
436         }
437         if ((rc = write_word(info, wp, data)) != 0) {
438             return (rc);
439         }
440         wp  += 4;
441         cnt -= 4;
442     }
443
444     if (cnt == 0) {
445         return (0);
446     }
447
448     /*
449      * handle unaligned tail bytes
450      */
451     data = 0;
452     for (i = 0, cp = wp; (i < 4) && (cnt > 0); ++i, ++cp) {
453         data = (data << 8) | *src++;
454         --cnt;
455     }
456     for (; (i < 4); ++i, ++cp) {
457         data = (data << 8) | (*(uchar *)cp);
458     }
459
460     return (write_word(info, wp, data));
461 }
462
463 /*********************************************************************/
464 /* NAME: write_word() - writes a word to flash                       */
465 /*                                                                   */
466 /* DESCRIPTION:                                                      */
467 /*   This writes a single word to flash.                             */
468 /*                                                                   */
469 /* INPUTS:                                                           */
470 /*  flash_info_t *info - flash information structure                 */
471 /*  ulong dest - address to write                                    */
472 /*  ulong data - data to write                                       */
473 /*                                                                   */
474 /* OUTPUTS:                                                          */
475 /*   None                                                            */
476 /*                                                                   */
477 /* RETURNS:                                                          */
478 /*   0 - OK                                                          */
479 /*   1 - write timeout                                               */
480 /*   2 - Flash not erased                                            */
481 /*                                                                   */
482 /* RESTRICTIONS/LIMITATIONS:                                         */
483 /*                                                                   */
484 /*********************************************************************/
485 static int write_word (flash_info_t *info, ulong dest, ulong data)
486 {
487     vu_long *addr = (vu_long*)(info->start[0]);
488     ulong start;
489     int flag;
490
491     /* Check if Flash is (sufficiently) erased */
492     if ((*((vu_long *)dest) & data) != data) {
493         return (2);
494     }
495     /* Disable interrupts which might cause a timeout here */
496     flag = disable_interrupts();
497
498     addr[0x0555] = 0xAAAAAAAA;
499     addr[0x02AA] = 0x55555555;
500     addr[0x0555] = 0xA0A0A0A0;
501
502     *((vu_long *)dest) = data;
503
504     /* re-enable interrupts if necessary */
505     if (flag)
506         enable_interrupts();
507
508     /* data polling for D7 */
509     start = get_timer (0);
510     while ((*((vu_long *)dest) & 0x80808080) != (data & 0x80808080)) {
511         if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
512             return (1);
513         }
514     }
515     return (0);
516 }
517 /*********************************************************************/
518 /*                         End of flash.c                            */
519 /*********************************************************************/