blob: bfc8d02f85ba67c2262687c384314b64e4a51ca4 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
2 * (C) Copyright 2000-2002
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6 * Andreas Heppel <aheppel@sysgo.de>
7
8 * See file CREDITS for list of people who contributed to this
9 * project.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 * MA 02111-1307 USA
25 */
26
27/*
28 * 09-18-2001 Andreas Heppel, Sysgo RTS GmbH <aheppel@sysgo.de>
29 *
30 * It might not be possible in all cases to use 'memcpy()' to copy
31 * the environment to NVRAM, as the NVRAM might not be mapped into
32 * the memory space. (I.e. this is the case for the BAB750). In those
33 * cases it might be possible to access the NVRAM using a different
34 * method. For example, the RTC on the BAB750 is accessible in IO
35 * space using its address and data registers. To enable usage of
36 * NVRAM in those cases I invented the functions 'nvram_read()' and
37 * 'nvram_write()', which will be activated upon the configuration
38 * #define CFG_NVRAM_ACCESS_ROUTINE. Note, that those functions are
39 * strongly dependent on the used HW, and must be redefined for each
40 * board that wants to use them.
41 */
42
43#include <common.h>
44
Wolfgang Denkd87080b2006-03-31 18:32:53 +020045DECLARE_GLOBAL_DATA_PTR;
46
wdenkc6097192002-11-03 00:24:07 +000047#ifdef CFG_ENV_IS_IN_NVRAM /* Environment is in NVRAM */
48
49#include <command.h>
50#include <environment.h>
wdenkc6097192002-11-03 00:24:07 +000051#include <linux/stddef.h>
wdenkc6097192002-11-03 00:24:07 +000052
53#ifdef CFG_NVRAM_ACCESS_ROUTINE
54extern void *nvram_read(void *dest, const long src, size_t count);
55extern void nvram_write(long dest, const void *src, size_t count);
56env_t *env_ptr = NULL;
57#else
58env_t *env_ptr = (env_t *)CFG_ENV_ADDR;
59#endif
60
61char * env_name_spec = "NVRAM";
62
63extern uchar default_environment[];
64extern int default_environment_size;
65
wdenkc6097192002-11-03 00:24:07 +000066extern uchar env_get_char_memory (int index);
67
wdenk7c7a23b2002-12-07 00:20:59 +000068#ifdef CONFIG_AMIGAONEG3SE
69uchar env_get_char_spec (int index)
70{
71#ifdef CFG_NVRAM_ACCESS_ROUTINE
72 uchar c;
wdenkc6097192002-11-03 00:24:07 +000073
wdenk7c7a23b2002-12-07 00:20:59 +000074 nvram_read(&c, CFG_ENV_ADDR+index, 1);
75
76 return c;
77#else
wdenk7c7a23b2002-12-07 00:20:59 +000078 uchar retval;
79 enable_nvram();
80 retval = *((uchar *)(gd->env_addr + index));
81 disable_nvram();
82 return retval;
83#endif
84}
85#else
wdenkc6097192002-11-03 00:24:07 +000086uchar env_get_char_spec (int index)
87{
88#ifdef CFG_NVRAM_ACCESS_ROUTINE
89 uchar c;
90
91 nvram_read(&c, CFG_ENV_ADDR+index, 1);
92
93 return c;
94#else
wdenkc6097192002-11-03 00:24:07 +000095 return *((uchar *)(gd->env_addr + index));
96#endif
97}
wdenk7c7a23b2002-12-07 00:20:59 +000098#endif
wdenkc6097192002-11-03 00:24:07 +000099
100void env_relocate_spec (void)
101{
102#if defined(CFG_NVRAM_ACCESS_ROUTINE)
103 nvram_read(env_ptr, CFG_ENV_ADDR, CFG_ENV_SIZE);
104#else
105 memcpy (env_ptr, (void*)CFG_ENV_ADDR, CFG_ENV_SIZE);
106#endif
107}
108
109int saveenv (void)
110{
111 int rcode = 0;
wdenk7c7a23b2002-12-07 00:20:59 +0000112#ifdef CONFIG_AMIGAONEG3SE
113 enable_nvram();
114#endif
wdenkc6097192002-11-03 00:24:07 +0000115#ifdef CFG_NVRAM_ACCESS_ROUTINE
116 nvram_write(CFG_ENV_ADDR, env_ptr, CFG_ENV_SIZE);
117#else
118 if (memcpy ((char *)CFG_ENV_ADDR, env_ptr, CFG_ENV_SIZE) == NULL)
119 rcode = 1 ;
120#endif
wdenk7c7a23b2002-12-07 00:20:59 +0000121#ifdef CONFIG_AMIGAONEG3SE
122 udelay(10000);
123 disable_nvram();
124#endif
wdenkc6097192002-11-03 00:24:07 +0000125 return rcode;
126}
127
128
129/************************************************************************
130 * Initialize Environment use
131 *
132 * We are still running from ROM, so data use is limited
133 */
134int env_init (void)
135{
wdenk7c7a23b2002-12-07 00:20:59 +0000136#ifdef CONFIG_AMIGAONEG3SE
137 enable_nvram();
138#endif
wdenkc6097192002-11-03 00:24:07 +0000139#if defined(CFG_NVRAM_ACCESS_ROUTINE)
140 ulong crc;
141 uchar data[ENV_SIZE];
142 nvram_read (&crc, CFG_ENV_ADDR, sizeof(ulong));
143 nvram_read (data, CFG_ENV_ADDR+sizeof(ulong), ENV_SIZE);
144
145 if (crc32(0, data, ENV_SIZE) == crc) {
146 gd->env_addr = (ulong)CFG_ENV_ADDR + sizeof(long);
147#else
148 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
149 gd->env_addr = (ulong)&(env_ptr->data);
150#endif
151 gd->env_valid = 1;
152 } else {
153 gd->env_addr = (ulong)&default_environment[0];
154 gd->env_valid = 0;
155 }
wdenk7c7a23b2002-12-07 00:20:59 +0000156#ifdef CONFIG_AMIGAONEG3SE
157 disable_nvram();
158#endif
wdenkc6097192002-11-03 00:24:07 +0000159 return (0);
160}
161
162#endif /* CFG_ENV_IS_IN_NVRAM */