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)
84 /* Init: no FLASHes known */
85 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; ++i)
86 flash_info[i].flash_id = FLASH_UNKNOWN;
88 /* for now, only support the 4 MB Flash SIMM */
89 size = flash_get_size((vu_long *) CONFIG_SYS_FLASH0_BASE,
92 * protect monitor and environment sectors
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,
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
105 flash_protect(FLAG_PROTECT_SET,
107 CONFIG_ENV_ADDR + CONFIG_ENV_SIZE - 1, &flash_info[0]);
110 return CONFIG_SYS_FLASH0_SIZE * 1024 * 1024; /*size */
113 /*********************************************************************/
114 /* NAME: flash_print_info() - prints flash imformation */
117 /* This function prints the flash information. */
120 /* flash_info_t *info - flash information structure */
123 /* Displays flash information to console */
128 /* RESTRICTIONS/LIMITATIONS: */
131 /*********************************************************************/
132 void flash_print_info (flash_info_t *info)
136 if (info->flash_id == FLASH_UNKNOWN) {
137 printf ("missing or unknown FLASH type\n");
141 switch ((info->flash_id >> 16) & 0xff) {
146 printf ("Unknown Vendor ");
150 switch (info->flash_id & FLASH_TYPEMASK) {
152 printf ("AM29F040B (4 Mbit)\n");
155 printf ("AM29F080B (8 Mbit)\n");
158 printf ("AM29F016D (16 Mbit)\n");
161 printf ("Unknown Chip Type\n");
165 printf (" Size: %ld MB in %d Sectors\n",
166 info->size >> 20, info->sector_count);
168 printf (" Sector Start Addresses:");
169 for (i=0; i<info->sector_count; ++i) {
174 info->protect[i] ? " (RO)" : " "
181 /*********************************************************************/
182 /* The following code cannot be run from FLASH! */
183 /*********************************************************************/
185 /*********************************************************************/
186 /* NAME: flash_get_size() - detects the flash size */
189 /* 1) Reads vendor ID and devices ID from the flash devices. */
190 /* 2) Initializes flash info struct. */
191 /* 3) Return the flash size */
194 /* vu_long *addr - pointer to start of flash */
195 /* flash_info_t *info - flash information structure */
201 /* Size of the flash in bytes, or 0 if device id is unknown. */
203 /* RESTRICTIONS/LIMITATIONS: */
204 /* Only supports the following devices: */
208 /*********************************************************************/
209 static ulong flash_get_size (vu_long *addr, flash_info_t *info)
212 vu_long vendor, devid;
213 ulong base = (ulong)addr;
215 /*printf("addr = %08lx\n", (unsigned long)addr); */
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;
225 /*printf("vendor = %08lx\n", vendor); */
226 if (vendor != 0x01010101) {
232 /*printf("devid = %08lx\n", devid); */
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;
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;
260 addr = (vu_long *)info->start[0];
263 addr[0] = 0xf0f0f0f0;
265 /*printf("size = %08x\n", info->size); */
269 /*********************************************************************/
270 /* NAME: flash_erase() - erases flash by sector */
273 /* This function erases flash sectors starting for s_first to */
277 /* flash_info_t *info - flash information structure */
278 /* int s_first - first sector to erase */
279 /* int s_last - last sector to erase */
285 /* Returns 0 for success, 1 for failure. */
287 /* RESTRICTIONS/LIMITATIONS: */
289 /*********************************************************************/
290 int flash_erase (flash_info_t *info, int s_first, int s_last)
292 vu_long *addr = (vu_long*)(info->start[0]);
293 int flag, prot, sect, l_sect;
294 ulong start, now, last;
296 if ((s_first < 0) || (s_first > s_last)) {
297 if (info->flash_id == FLASH_UNKNOWN) {
298 printf ("- missing\n");
300 printf ("- no sectors to erase\n");
306 for (sect = s_first; sect <= s_last; sect++) {
307 if (info->protect[sect]) {
313 printf ("- Warning: %d protected sectors will not be erased!\n",
321 /* Disable interrupts which might cause a timeout here */
322 flag = disable_interrupts();
324 addr[0x0555] = 0xAAAAAAAA;
325 addr[0x02AA] = 0x55555555;
326 addr[0x0555] = 0x80808080;
327 addr[0x0555] = 0xAAAAAAAA;
328 addr[0x02AA] = 0x55555555;
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;
340 /* re-enable interrupts if necessary */
344 /* wait at least 80us - let's wait 1 ms */
348 * We wait for the last triggered sector
353 start = get_timer (0);
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");
361 /* show that we're waiting */
362 if ((now - last) > 1000) { /* every second */
369 /* reset to read mode */
370 addr = (volatile unsigned long *)info->start[0];
371 addr[0] = 0xF0F0F0F0; /* reset bank */
377 /*********************************************************************/
378 /* NAME: write_buff() - writes a buffer to flash */
381 /* This function copies a buffer, *src, to flash. */
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 */
394 /* 1 - write timeout */
395 /* 2 - Flash not erased */
397 /* RESTRICTIONS/LIMITATIONS: */
399 /*********************************************************************/
400 int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt)
405 wp = (addr & ~3); /* get lower word aligned address */
408 * handle unaligned start bytes
410 if ((l = addr - wp) != 0) {
412 for (i = 0, cp = wp; i < l; ++i, ++cp) {
413 data = (data << 8) | (*(uchar *)cp);
415 for (; (i < 4) && (cnt > 0); ++i) {
416 data = (data << 8) | *src++;
420 for (; (cnt == 0) && (i < 4); ++i, ++cp) {
421 data = (data << 8) | (*(uchar *)cp);
424 if ((rc = write_word(info, wp, data)) != 0) {
431 * handle word aligned part
435 for (i = 0; i < 4; ++i) {
436 data = (data << 8) | *src++;
438 if ((rc = write_word(info, wp, data)) != 0) {
450 * handle unaligned tail bytes
453 for (i = 0, cp = wp; (i < 4) && (cnt > 0); ++i, ++cp) {
454 data = (data << 8) | *src++;
457 for (; (i < 4); ++i, ++cp) {
458 data = (data << 8) | (*(uchar *)cp);
461 return (write_word(info, wp, data));
464 /*********************************************************************/
465 /* NAME: write_word() - writes a word to flash */
468 /* This writes a single word to flash. */
471 /* flash_info_t *info - flash information structure */
472 /* ulong dest - address to write */
473 /* ulong data - data to write */
480 /* 1 - write timeout */
481 /* 2 - Flash not erased */
483 /* RESTRICTIONS/LIMITATIONS: */
485 /*********************************************************************/
486 static int write_word (flash_info_t *info, ulong dest, ulong data)
488 vu_long *addr = (vu_long*)(info->start[0]);
492 /* Check if Flash is (sufficiently) erased */
493 if ((*((vu_long *)dest) & data) != data) {
496 /* Disable interrupts which might cause a timeout here */
497 flag = disable_interrupts();
499 addr[0x0555] = 0xAAAAAAAA;
500 addr[0x02AA] = 0x55555555;
501 addr[0x0555] = 0xA0A0A0A0;
503 *((vu_long *)dest) = data;
505 /* re-enable interrupts if necessary */
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) {
518 /*********************************************************************/
520 /*********************************************************************/