blob: 8fe59f8224b99c34cd738b1ad218f592cc335ec2 [file] [log] [blame]
wdenk0bc4a1a2002-09-18 11:09:59 +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#include <common.h>
wdenk0bc4a1a2002-09-18 11:09:59 +000028#include <command.h>
29#include <environment.h>
wdenk0bc4a1a2002-09-18 11:09:59 +000030#include <linux/stddef.h>
Heiko Schocher548738b2010-01-07 08:55:40 +010031#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
32#include <i2c.h>
33#endif
wdenk0bc4a1a2002-09-18 11:09:59 +000034
Wolfgang Denkd87080b2006-03-31 18:32:53 +020035DECLARE_GLOBAL_DATA_PTR;
36
wdenk0bc4a1a2002-09-18 11:09:59 +000037env_t *env_ptr = NULL;
38
39char * env_name_spec = "EEPROM";
Heiko Schocher548738b2010-01-07 08:55:40 +010040int env_eeprom_bus = -1;
41
42static int eeprom_bus_read (unsigned dev_addr, unsigned offset, uchar *buffer,
43 unsigned cnt)
44{
45 int rcode;
46#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
47 int old_bus = i2c_get_bus_num();
48
49 if (gd->flags & GD_FLG_RELOC) {
50 if (env_eeprom_bus == -1) {
51 I2C_MUX_DEVICE *dev = NULL;
52 dev = i2c_mux_ident_muxstring(
53 (uchar *)CONFIG_I2C_ENV_EEPROM_BUS);
54 if (dev != NULL) {
55 env_eeprom_bus = dev->busid;
56 } else
57 printf ("error adding env eeprom bus.\n");
58 }
59 if (old_bus != env_eeprom_bus) {
60 i2c_set_bus_num(env_eeprom_bus);
61 old_bus = env_eeprom_bus;
62 }
63 } else {
64 rcode = i2c_mux_ident_muxstring_f(
65 (uchar *)CONFIG_I2C_ENV_EEPROM_BUS);
66 }
67#endif
68
69 rcode = eeprom_read (dev_addr, offset, buffer, cnt);
70#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
71 if (old_bus != env_eeprom_bus)
72 i2c_set_bus_num(old_bus);
73#endif
74 return rcode;
75}
76
77static int eeprom_bus_write (unsigned dev_addr, unsigned offset, uchar *buffer,
78 unsigned cnt)
79{
80 int rcode;
81#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
82 int old_bus = i2c_get_bus_num();
83
84 rcode = i2c_mux_ident_muxstring_f((uchar *)CONFIG_I2C_ENV_EEPROM_BUS);
85#endif
86 rcode = eeprom_write (dev_addr, offset, buffer, cnt);
87#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
88 i2c_set_bus_num(old_bus);
89#endif
90 return rcode;
91}
wdenk0bc4a1a2002-09-18 11:09:59 +000092
wdenk0bc4a1a2002-09-18 11:09:59 +000093uchar env_get_char_spec (int index)
94{
95 uchar c;
Heiko Schocher1567b592010-01-07 08:55:44 +010096 unsigned int off;
97 off = CONFIG_ENV_OFFSET;
98#ifdef CONFIG_ENV_OFFSET_REDUND
99 if (gd->env_valid == 2)
100 off = CONFIG_ENV_OFFSET_REDUND;
101#endif
wdenk0bc4a1a2002-09-18 11:09:59 +0000102
Heiko Schocher548738b2010-01-07 08:55:40 +0100103 eeprom_bus_read (CONFIG_SYS_DEF_EEPROM_ADDR,
Heiko Schocher1567b592010-01-07 08:55:44 +0100104 off + index + offsetof(env_t,data),
wdenk0bc4a1a2002-09-18 11:09:59 +0000105 &c, 1);
106
107 return (c);
108}
109
110void env_relocate_spec (void)
111{
Heiko Schocher1567b592010-01-07 08:55:44 +0100112 unsigned int off = CONFIG_ENV_OFFSET;
113#ifdef CONFIG_ENV_OFFSET_REDUND
114 if (gd->env_valid == 2)
115 off = CONFIG_ENV_OFFSET_REDUND;
116#endif
Heiko Schocher548738b2010-01-07 08:55:40 +0100117 eeprom_bus_read (CONFIG_SYS_DEF_EEPROM_ADDR,
Heiko Schocher1567b592010-01-07 08:55:44 +0100118 off,
wdenk0bc4a1a2002-09-18 11:09:59 +0000119 (uchar*)env_ptr,
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200120 CONFIG_ENV_SIZE);
wdenk0bc4a1a2002-09-18 11:09:59 +0000121}
122
123int saveenv(void)
124{
Heiko Schocher1567b592010-01-07 08:55:44 +0100125 int rc;
126 unsigned int off = CONFIG_ENV_OFFSET;
127#ifdef CONFIG_ENV_OFFSET_REDUND
128 unsigned int off_red = CONFIG_ENV_OFFSET_REDUND;
129 char flag_obsolete = OBSOLETE_FLAG;
130 if (gd->env_valid == 1) {
131 off = CONFIG_ENV_OFFSET_REDUND;
132 off_red = CONFIG_ENV_OFFSET;
133 }
134
135 env_ptr->flags = ACTIVE_FLAG;
136#endif
137
138 rc = eeprom_bus_write (CONFIG_SYS_DEF_EEPROM_ADDR,
139 off,
wdenk0bc4a1a2002-09-18 11:09:59 +0000140 (uchar *)env_ptr,
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200141 CONFIG_ENV_SIZE);
Heiko Schocher1567b592010-01-07 08:55:44 +0100142
143#ifdef CONFIG_ENV_OFFSET_REDUND
144 if (rc == 0) {
145 eeprom_bus_write (CONFIG_SYS_DEF_EEPROM_ADDR,
146 off_red + offsetof(env_t,flags),
147 (uchar *)&flag_obsolete,
148 1);
149 if (gd->env_valid == 1)
150 gd->env_valid = 2;
151 else
152 gd->env_valid = 1;
153
154 }
155#endif
156
157 return rc;
wdenk0bc4a1a2002-09-18 11:09:59 +0000158}
159
160/************************************************************************
161 * Initialize Environment use
162 *
163 * We are still running from ROM, so data use is limited
164 * Use a (moderately small) buffer on the stack
165 */
Heiko Schocher1567b592010-01-07 08:55:44 +0100166
167#ifdef CONFIG_ENV_OFFSET_REDUND
168int env_init(void)
169{
170 ulong len;
171 ulong crc[2], crc_tmp;
172 unsigned int off, off_env[2];
173 uchar buf[64];
174 int crc_ok[2] = {0,0};
175 unsigned char flags[2];
176 int i;
177
178 eeprom_init (); /* prepare for EEPROM read/write */
179
180 off_env[0] = CONFIG_ENV_OFFSET;
181 off_env[1] = CONFIG_ENV_OFFSET_REDUND;
182
183 for (i = 0; i < 2; i++) {
184 /* read CRC */
185 eeprom_bus_read (CONFIG_SYS_DEF_EEPROM_ADDR,
186 off_env[i] + offsetof(env_t,crc),
187 (uchar *)&crc[i], sizeof(ulong));
188 /* read FLAGS */
189 eeprom_bus_read (CONFIG_SYS_DEF_EEPROM_ADDR,
190 off_env[i] + offsetof(env_t,flags),
191 (uchar *)&flags[i], sizeof(uchar));
192
193 crc_tmp= 0;
194 len = ENV_SIZE;
195 off = off_env[i] + offsetof(env_t,data);
196 while (len > 0) {
197 int n = (len > sizeof(buf)) ? sizeof(buf) : len;
198
199 eeprom_bus_read (CONFIG_SYS_DEF_EEPROM_ADDR, off,
200 buf, n);
201
202 crc_tmp = crc32 (crc_tmp, buf, n);
203 len -= n;
204 off += n;
205 }
206 if (crc_tmp == crc[i])
207 crc_ok[i] = 1;
208 }
209
210 if (!crc_ok[0] && !crc_ok[1]) {
211 gd->env_addr = 0;
212 gd->env_valid = 0;
213
214 return 0;
215 } else if (crc_ok[0] && !crc_ok[1]) {
216 gd->env_valid = 1;
217 }
218 else if (!crc_ok[0] && crc_ok[1]) {
219 gd->env_valid = 2;
220 } else {
221 /* both ok - check serial */
222 if (flags[0] == ACTIVE_FLAG && flags[1] == OBSOLETE_FLAG)
223 gd->env_valid = 1;
224 else if (flags[0] == OBSOLETE_FLAG && flags[1] == ACTIVE_FLAG)
225 gd->env_valid = 2;
226 else if (flags[0] == 0xFF && flags[1] == 0)
227 gd->env_valid = 2;
228 else if(flags[1] == 0xFF && flags[0] == 0)
229 gd->env_valid = 1;
230 else /* flags are equal - almost impossible */
231 gd->env_valid = 1;
232 }
233
234 if (gd->env_valid == 2)
235 gd->env_addr = off_env[1] + offsetof(env_t,data);
236 else if (gd->env_valid == 1)
237 gd->env_addr = off_env[0] + offsetof(env_t,data);
238
239 return (0);
240}
241#else
wdenk0bc4a1a2002-09-18 11:09:59 +0000242int env_init(void)
243{
wdenk0bc4a1a2002-09-18 11:09:59 +0000244 ulong crc, len, new;
245 unsigned off;
246 uchar buf[64];
247
248 eeprom_init (); /* prepare for EEPROM read/write */
249
250 /* read old CRC */
Heiko Schocher548738b2010-01-07 08:55:40 +0100251 eeprom_bus_read (CONFIG_SYS_DEF_EEPROM_ADDR,
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200252 CONFIG_ENV_OFFSET+offsetof(env_t,crc),
wdenk0bc4a1a2002-09-18 11:09:59 +0000253 (uchar *)&crc, sizeof(ulong));
254
255 new = 0;
256 len = ENV_SIZE;
257 off = offsetof(env_t,data);
258 while (len > 0) {
259 int n = (len > sizeof(buf)) ? sizeof(buf) : len;
260
Heiko Schocher548738b2010-01-07 08:55:40 +0100261 eeprom_bus_read (CONFIG_SYS_DEF_EEPROM_ADDR,
262 CONFIG_ENV_OFFSET + off, buf, n);
wdenk0bc4a1a2002-09-18 11:09:59 +0000263 new = crc32 (new, buf, n);
264 len -= n;
265 off += n;
266 }
267
268 if (crc == new) {
269 gd->env_addr = offsetof(env_t,data);
270 gd->env_valid = 1;
271 } else {
272 gd->env_addr = 0;
273 gd->env_valid = 0;
274 }
275
276 return (0);
277}
Heiko Schocher1567b592010-01-07 08:55:44 +0100278#endif