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 /*********************************************************************/
66 /*********************************************************************/
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: */
78 /*********************************************************************/
79 unsigned long flash_init (void)
84 /* Init: no FLASHes known */
85 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; ++i) {
86 flash_info[i].flash_id = FLASH_UNKNOWN;
89 /* for now, only support the 4 MB Flash SIMM */
90 size = flash_get_size((vu_long *)CONFIG_SYS_FLASH0_BASE, &flash_info[0]);
93 * protect monitor and environment sectors
96 #if CONFIG_SYS_MONITOR_BASE >= CONFIG_SYS_FLASH0_BASE
97 flash_protect(FLAG_PROTECT_SET,
98 CONFIG_SYS_MONITOR_BASE,
99 CONFIG_SYS_MONITOR_BASE+monitor_flash_len-1,
103 #if defined(CONFIG_ENV_IS_IN_FLASH) && defined(CONFIG_ENV_ADDR)
104 # ifndef CONFIG_ENV_SIZE
105 # define CONFIG_ENV_SIZE CONFIG_ENV_SECT_SIZE
107 flash_protect(FLAG_PROTECT_SET,
109 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1,
113 return (CONFIG_SYS_FLASH0_SIZE * 1024 * 1024); /*size*/
116 /*********************************************************************/
117 /* NAME: flash_print_info() - prints flash imformation */
120 /* This function prints the flash information. */
123 /* flash_info_t *info - flash information structure */
126 /* Displays flash information to console */
131 /* RESTRICTIONS/LIMITATIONS: */
134 /*********************************************************************/
135 void flash_print_info (flash_info_t *info)
139 if (info->flash_id == FLASH_UNKNOWN) {
140 printf ("missing or unknown FLASH type\n");
144 switch ((info->flash_id >> 16) & 0xff) {
149 printf ("Unknown Vendor ");
153 switch (info->flash_id & FLASH_TYPEMASK) {
155 printf ("AM29F040B (4 Mbit)\n");
158 printf ("AM29F080B (8 Mbit)\n");
161 printf ("AM29F016D (16 Mbit)\n");
164 printf ("Unknown Chip Type\n");
168 printf (" Size: %ld MB in %d Sectors\n",
169 info->size >> 20, info->sector_count);
171 printf (" Sector Start Addresses:");
172 for (i=0; i<info->sector_count; ++i) {
177 info->protect[i] ? " (RO)" : " "
184 /*********************************************************************/
185 /* The following code cannot be run from FLASH! */
186 /*********************************************************************/
188 /*********************************************************************/
189 /* NAME: flash_get_size() - detects the flash size */
192 /* 1) Reads vendor ID and devices ID from the flash devices. */
193 /* 2) Initializes flash info struct. */
194 /* 3) Return the flash size */
197 /* vu_long *addr - pointer to start of flash */
198 /* flash_info_t *info - flash information structure */
204 /* Size of the flash in bytes, or 0 if device id is unknown. */
206 /* RESTRICTIONS/LIMITATIONS: */
207 /* Only supports the following devices: */
211 /*********************************************************************/
212 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
215 vu_long vendor, devid;
216 ulong base = (ulong)addr;
218 /*printf("addr = %08lx\n", (unsigned long)addr); */
220 /* Reset and Write auto select command: read Manufacturer ID */
221 addr[0x0000] = 0xf0f0f0f0;
222 addr[0x0555] = 0xAAAAAAAA;
223 addr[0x02AA] = 0x55555555;
224 addr[0x0555] = 0x90909090;
228 /*printf("vendor = %08lx\n", vendor); */
229 if (vendor != 0x01010101) {
235 /*printf("devid = %08lx\n", devid); */
237 if ((devid & 0xff) == AMD_ID_F080B) {
238 info->flash_id = (vendor & 0xff) << 16 | AMD_ID_F080B;
239 /* we have 16 sectors with 64KB each x 4 */
240 info->sector_count = 16;
241 info->size = 4 * info->sector_count * 64*1024;
242 } else if ((devid & 0xff) == AMD_ID_F016D){
243 info->flash_id = (vendor & 0xff) << 16 | AMD_ID_F016D;
244 /* we have 32 sectors with 64KB each x 4 */
245 info->sector_count = 32;
246 info->size = 4 * info->sector_count * 64*1024;
251 /*printf("sector count = %08x\n", info->sector_count); */
252 /* check for protected sectors */
253 for (i = 0; i < info->sector_count; i++) {
254 /* sector base address */
255 info->start[i] = base + i * (info->size / info->sector_count);
256 /* read sector protection at sector address, (A7 .. A0) = 0x02 */
257 /* D0 = 1 if protected */
258 addr = (volatile unsigned long *)(info->start[i]);
259 info->protect[i] = addr[2] & 1;
263 addr = (vu_long *)info->start[0];
266 addr[0] = 0xf0f0f0f0;
268 /*printf("size = %08x\n", info->size); */
272 /*********************************************************************/
273 /* NAME: flash_erase() - erases flash by sector */
276 /* This function erases flash sectors starting for s_first to */
280 /* flash_info_t *info - flash information structure */
281 /* int s_first - first sector to erase */
282 /* int s_last - last sector to erase */
288 /* Returns 0 for success, 1 for failure. */
290 /* RESTRICTIONS/LIMITATIONS: */
292 /*********************************************************************/
293 int flash_erase (flash_info_t *info, int s_first, int s_last)
295 vu_long *addr = (vu_long*)(info->start[0]);
296 int flag, prot, sect, l_sect;
297 ulong start, now, last;
299 if ((s_first < 0) || (s_first > s_last)) {
300 if (info->flash_id == FLASH_UNKNOWN) {
301 printf ("- missing\n");
303 printf ("- no sectors to erase\n");
309 for (sect = s_first; sect <= s_last; sect++) {
310 if (info->protect[sect]) {
316 printf ("- Warning: %d protected sectors will not be erased!\n",
324 /* Disable interrupts which might cause a timeout here */
325 flag = disable_interrupts();
327 addr[0x0555] = 0xAAAAAAAA;
328 addr[0x02AA] = 0x55555555;
329 addr[0x0555] = 0x80808080;
330 addr[0x0555] = 0xAAAAAAAA;
331 addr[0x02AA] = 0x55555555;
334 /* Start erase on unprotected sectors */
335 for (sect = s_first; sect <= s_last; sect++) {
336 if (info->protect[sect] == 0) { /* not protected */
337 addr = (vu_long*)(info->start[sect]);
338 addr[0] = 0x30303030;
343 /* re-enable interrupts if necessary */
347 /* wait at least 80us - let's wait 1 ms */
351 * We wait for the last triggered sector
356 start = get_timer (0);
358 addr = (vu_long*)(info->start[l_sect]);
359 while ((addr[0] & 0x80808080) != 0x80808080) {
360 if ((now = get_timer(start)) > CONFIG_SYS_FLASH_ERASE_TOUT) {
361 printf ("Timeout\n");
364 /* show that we're waiting */
365 if ((now - last) > 1000) { /* every second */
372 /* reset to read mode */
373 addr = (volatile unsigned long *)info->start[0];
374 addr[0] = 0xF0F0F0F0; /* reset bank */
380 /*********************************************************************/
381 /* NAME: write_buff() - writes a buffer to flash */
384 /* This function copies a buffer, *src, to flash. */
387 /* flash_info_t *info - flash information structure */
388 /* uchar *src - pointer to buffer to write to flash */
389 /* ulong addr - address to start write at */
390 /* ulong cnt - number of bytes to write to flash */
397 /* 1 - write timeout */
398 /* 2 - Flash not erased */
400 /* RESTRICTIONS/LIMITATIONS: */
402 /*********************************************************************/
403 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
408 wp = (addr & ~3); /* get lower word aligned address */
411 * handle unaligned start bytes
413 if ((l = addr - wp) != 0) {
415 for (i = 0, cp = wp; i < l; ++i, ++cp) {
416 data = (data << 8) | (*(uchar *)cp);
418 for (; (i < 4) && (cnt > 0); ++i) {
419 data = (data << 8) | *src++;
423 for (; (cnt == 0) && (i < 4); ++i, ++cp) {
424 data = (data << 8) | (*(uchar *)cp);
427 if ((rc = write_word(info, wp, data)) != 0) {
434 * handle word aligned part
438 for (i = 0; i < 4; ++i) {
439 data = (data << 8) | *src++;
441 if ((rc = write_word(info, wp, data)) != 0) {
453 * handle unaligned tail bytes
456 for (i = 0, cp = wp; (i < 4) && (cnt > 0); ++i, ++cp) {
457 data = (data << 8) | *src++;
460 for (; (i < 4); ++i, ++cp) {
461 data = (data << 8) | (*(uchar *)cp);
464 return (write_word(info, wp, data));
467 /*********************************************************************/
468 /* NAME: write_word() - writes a word to flash */
471 /* This writes a single word to flash. */
474 /* flash_info_t *info - flash information structure */
475 /* ulong dest - address to write */
476 /* ulong data - data to write */
483 /* 1 - write timeout */
484 /* 2 - Flash not erased */
486 /* RESTRICTIONS/LIMITATIONS: */
488 /*********************************************************************/
489 static int write_word (flash_info_t *info, ulong dest, ulong data)
491 vu_long *addr = (vu_long*)(info->start[0]);
495 /* Check if Flash is (sufficiently) erased */
496 if ((*((vu_long *)dest) & data) != data) {
499 /* Disable interrupts which might cause a timeout here */
500 flag = disable_interrupts();
502 addr[0x0555] = 0xAAAAAAAA;
503 addr[0x02AA] = 0x55555555;
504 addr[0x0555] = 0xA0A0A0A0;
506 *((vu_long *)dest) = data;
508 /* re-enable interrupts if necessary */
512 /* data polling for D7 */
513 start = get_timer (0);
514 while ((*((vu_long *)dest) & 0x80808080) != (data & 0x80808080)) {
515 if (get_timer(start) > CONFIG_SYS_FLASH_WRITE_TOUT) {
521 /*********************************************************************/
523 /*********************************************************************/