3 * Marius Groeger <mgroeger@sysgo.de>
4 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6 * (C) Copyright 2000, 2001
7 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
10 * Advent Networks, Inc. <http://www.adventnetworks.com>
11 * Oliver Brown <oliverb@alumni.utexas.net>
13 *--------------------------------------------------------------------
14 * See file CREDITS for list of people who contributed to this
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.
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.
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,
33 /*********************************************************************/
35 * This file contains the flash routines for the GW8260 board.
43 * RESTRICTIONS/LIMITATIONS:
45 * Only supports the following flash devices:
49 * Copyright (c) 2001, Advent Networks, Inc.
52 /*********************************************************************/
57 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
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);
62 /*********************************************************************/
64 /*********************************************************************/
67 * NAME: flash_init() - initializes flash banks
70 * This function initializes the flash bank(s).
73 * The size in bytes of the flash
75 * RESTRICTIONS/LIMITATIONS:
79 unsigned long flash_init(void)
83 /* Init: no FLASHes known */
84 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i)
85 flash_info[i].flash_id = FLASH_UNKNOWN;
87 /* for now, only support the 4 MB Flash SIMM */
88 (void)flash_get_size((vu_long *) CONFIG_SYS_FLASH0_BASE,
91 * protect monitor and environment sectors
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,
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
104 flash_protect(FLAG_PROTECT_SET,
106 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
109 return CONFIG_SYS_FLASH0_SIZE * 1024 * 1024; /*size */
112 /*********************************************************************/
113 /* NAME: flash_print_info() - prints flash imformation */
116 /* This function prints the flash information. */
119 /* flash_info_t *info - flash information structure */
122 /* Displays flash information to console */
127 /* RESTRICTIONS/LIMITATIONS: */
130 /*********************************************************************/
131 void flash_print_info (flash_info_t *info)
135 if (info->flash_id == FLASH_UNKNOWN) {
136 printf ("missing or unknown FLASH type\n");
140 switch ((info->flash_id >> 16) & 0xff) {
145 printf ("Unknown Vendor ");
149 switch (info->flash_id & FLASH_TYPEMASK) {
151 printf ("AM29F040B (4 Mbit)\n");
154 printf ("AM29F080B (8 Mbit)\n");
157 printf ("AM29F016D (16 Mbit)\n");
160 printf ("Unknown Chip Type\n");
164 printf (" Size: %ld MB in %d Sectors\n",
165 info->size >> 20, info->sector_count);
167 printf (" Sector Start Addresses:");
168 for (i=0; i<info->sector_count; ++i) {
173 info->protect[i] ? " (RO)" : " "
180 /*********************************************************************/
181 /* The following code cannot be run from FLASH! */
182 /*********************************************************************/
184 /*********************************************************************/
185 /* NAME: flash_get_size() - detects the flash size */
188 /* 1) Reads vendor ID and devices ID from the flash devices. */
189 /* 2) Initializes flash info struct. */
190 /* 3) Return the flash size */
193 /* vu_long *addr - pointer to start of flash */
194 /* flash_info_t *info - flash information structure */
200 /* Size of the flash in bytes, or 0 if device id is unknown. */
202 /* RESTRICTIONS/LIMITATIONS: */
203 /* Only supports the following devices: */
207 /*********************************************************************/
208 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
211 vu_long vendor, devid;
212 ulong base = (ulong)addr;
214 /*printf("addr = %08lx\n", (unsigned long)addr); */
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;
224 /*printf("vendor = %08lx\n", vendor); */
225 if (vendor != 0x01010101) {
231 /*printf("devid = %08lx\n", devid); */
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;
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;
259 addr = (vu_long *)info->start[0];
262 addr[0] = 0xf0f0f0f0;
264 /*printf("size = %08x\n", info->size); */
268 /*********************************************************************/
269 /* NAME: flash_erase() - erases flash by sector */
272 /* This function erases flash sectors starting for s_first to */
276 /* flash_info_t *info - flash information structure */
277 /* int s_first - first sector to erase */
278 /* int s_last - last sector to erase */
284 /* Returns 0 for success, 1 for failure. */
286 /* RESTRICTIONS/LIMITATIONS: */
288 /*********************************************************************/
289 int flash_erase (flash_info_t *info, int s_first, int s_last)
291 vu_long *addr = (vu_long*)(info->start[0]);
292 int flag, prot, sect, l_sect;
293 ulong start, now, last;
295 if ((s_first < 0) || (s_first > s_last)) {
296 if (info->flash_id == FLASH_UNKNOWN) {
297 printf ("- missing\n");
299 printf ("- no sectors to erase\n");
305 for (sect = s_first; sect <= s_last; sect++) {
306 if (info->protect[sect]) {
312 printf ("- Warning: %d protected sectors will not be erased!\n",
320 /* Disable interrupts which might cause a timeout here */
321 flag = disable_interrupts();
323 addr[0x0555] = 0xAAAAAAAA;
324 addr[0x02AA] = 0x55555555;
325 addr[0x0555] = 0x80808080;
326 addr[0x0555] = 0xAAAAAAAA;
327 addr[0x02AA] = 0x55555555;
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;
339 /* re-enable interrupts if necessary */
343 /* wait at least 80us - let's wait 1 ms */
347 * We wait for the last triggered sector
352 start = get_timer (0);
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");
360 /* show that we're waiting */
361 if ((now - last) > 1000) { /* every second */
368 /* reset to read mode */
369 addr = (volatile unsigned long *)info->start[0];
370 addr[0] = 0xF0F0F0F0; /* reset bank */
376 /*********************************************************************/
377 /* NAME: write_buff() - writes a buffer to flash */
380 /* This function copies a buffer, *src, to flash. */
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 */
393 /* 1 - write timeout */
394 /* 2 - Flash not erased */
396 /* RESTRICTIONS/LIMITATIONS: */
398 /*********************************************************************/
399 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
404 wp = (addr & ~3); /* get lower word aligned address */
407 * handle unaligned start bytes
409 if ((l = addr - wp) != 0) {
411 for (i = 0, cp = wp; i < l; ++i, ++cp) {
412 data = (data << 8) | (*(uchar *)cp);
414 for (; (i < 4) && (cnt > 0); ++i) {
415 data = (data << 8) | *src++;
419 for (; (cnt == 0) && (i < 4); ++i, ++cp) {
420 data = (data << 8) | (*(uchar *)cp);
423 if ((rc = write_word(info, wp, data)) != 0) {
430 * handle word aligned part
434 for (i = 0; i < 4; ++i) {
435 data = (data << 8) | *src++;
437 if ((rc = write_word(info, wp, data)) != 0) {
449 * handle unaligned tail bytes
452 for (i = 0, cp = wp; (i < 4) && (cnt > 0); ++i, ++cp) {
453 data = (data << 8) | *src++;
456 for (; (i < 4); ++i, ++cp) {
457 data = (data << 8) | (*(uchar *)cp);
460 return (write_word(info, wp, data));
463 /*********************************************************************/
464 /* NAME: write_word() - writes a word to flash */
467 /* This writes a single word to flash. */
470 /* flash_info_t *info - flash information structure */
471 /* ulong dest - address to write */
472 /* ulong data - data to write */
479 /* 1 - write timeout */
480 /* 2 - Flash not erased */
482 /* RESTRICTIONS/LIMITATIONS: */
484 /*********************************************************************/
485 static int write_word (flash_info_t *info, ulong dest, ulong data)
487 vu_long *addr = (vu_long*)(info->start[0]);
491 /* Check if Flash is (sufficiently) erased */
492 if ((*((vu_long *)dest) & data) != data) {
495 /* Disable interrupts which might cause a timeout here */
496 flag = disable_interrupts();
498 addr[0x0555] = 0xAAAAAAAA;
499 addr[0x02AA] = 0x55555555;
500 addr[0x0555] = 0xA0A0A0A0;
502 *((vu_long *)dest) = data;
504 /* re-enable interrupts if necessary */
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) {
517 /*********************************************************************/
519 /*********************************************************************/