2 * (C) Copyright 2000-2010
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6 * Andreas Heppel <aheppel@sysgo.de>
8 * See file CREDITS for list of people who contributed to this
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.
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.
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,
29 #include <environment.h>
30 #include <linux/stddef.h>
31 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
36 #include <linux/compiler.h> /* for BUG_ON */
38 DECLARE_GLOBAL_DATA_PTR;
41 static char env_buf[CONFIG_ENV_SIZE];
43 char *env_name_spec = "EEPROM";
44 int env_eeprom_bus = -1;
46 static int eeprom_bus_read(unsigned dev_addr, unsigned offset,
47 uchar *buffer, unsigned cnt)
50 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
51 int old_bus = i2c_get_bus_num();
53 if (gd->flags & GD_FLG_RELOC) {
54 if (env_eeprom_bus == -1) {
55 I2C_MUX_DEVICE *dev = NULL;
56 dev = i2c_mux_ident_muxstring(
57 (uchar *)CONFIG_I2C_ENV_EEPROM_BUS);
59 env_eeprom_bus = dev->busid;
61 printf("error adding env eeprom bus.\n");
63 if (old_bus != env_eeprom_bus) {
64 i2c_set_bus_num(env_eeprom_bus);
65 old_bus = env_eeprom_bus;
68 rcode = i2c_mux_ident_muxstring_f(
69 (uchar *)CONFIG_I2C_ENV_EEPROM_BUS);
73 rcode = eeprom_read(dev_addr, offset, buffer, cnt);
75 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
76 if (old_bus != env_eeprom_bus)
77 i2c_set_bus_num(old_bus);
82 static int eeprom_bus_write(unsigned dev_addr, unsigned offset,
83 uchar *buffer, unsigned cnt)
86 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
87 int old_bus = i2c_get_bus_num();
89 rcode = i2c_mux_ident_muxstring_f((uchar *)CONFIG_I2C_ENV_EEPROM_BUS);
91 rcode = eeprom_write(dev_addr, offset, buffer, cnt);
92 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
93 i2c_set_bus_num(old_bus);
98 uchar env_get_char_spec(int index)
101 unsigned int off = CONFIG_ENV_OFFSET;
103 #ifdef CONFIG_ENV_OFFSET_REDUND
104 if (gd->env_valid == 2)
105 off = CONFIG_ENV_OFFSET_REDUND;
107 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
108 off + index + offsetof(env_t, data), &c, 1);
113 void env_relocate_spec(void)
116 unsigned int off = CONFIG_ENV_OFFSET;
118 #ifdef CONFIG_ENV_OFFSET_REDUND
119 if (gd->env_valid == 2)
120 off = CONFIG_ENV_OFFSET_REDUND;
122 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
123 off, (uchar *)buf, CONFIG_ENV_SIZE);
130 env_t *env_new = (env_t *)env_buf;
134 unsigned int off = CONFIG_ENV_OFFSET;
135 #ifdef CONFIG_ENV_OFFSET_REDUND
136 unsigned int off_red = CONFIG_ENV_OFFSET_REDUND;
137 char flag_obsolete = OBSOLETE_FLAG;
140 BUG_ON(env_ptr != NULL);
142 res = (char *)env_new->data;
143 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
145 error("Cannot export environment: errno = %d\n", errno);
148 env_new->crc = crc32(0, env_new->data, ENV_SIZE);
150 #ifdef CONFIG_ENV_OFFSET_REDUND
151 if (gd->env_valid == 1) {
152 off = CONFIG_ENV_OFFSET_REDUND;
153 off_red = CONFIG_ENV_OFFSET;
156 env_new->flags = ACTIVE_FLAG;
159 rc = eeprom_bus_write(CONFIG_SYS_DEF_EEPROM_ADDR,
160 off, (uchar *)env_new, CONFIG_ENV_SIZE);
162 #ifdef CONFIG_ENV_OFFSET_REDUND
164 eeprom_bus_write(CONFIG_SYS_DEF_EEPROM_ADDR,
165 off_red + offsetof(env_t, flags),
166 (uchar *)&flag_obsolete, 1);
168 if (gd->env_valid == 1)
178 * Initialize Environment use
180 * We are still running from ROM, so data use is limited.
181 * Use a (moderately small) buffer on the stack
183 #ifdef CONFIG_ENV_OFFSET_REDUND
186 ulong len, crc[2], crc_tmp;
187 unsigned int off, off_env[2];
188 uchar buf[64], flags[2];
189 int i, crc_ok[2] = {0, 0};
191 eeprom_init(); /* prepare for EEPROM read/write */
193 off_env[0] = CONFIG_ENV_OFFSET;
194 off_env[1] = CONFIG_ENV_OFFSET_REDUND;
196 for (i = 0; i < 2; i++) {
198 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
199 off_env[i] + offsetof(env_t, crc),
200 (uchar *)&crc[i], sizeof(ulong));
202 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
203 off_env[i] + offsetof(env_t, flags),
204 (uchar *)&flags[i], sizeof(uchar));
208 off = off_env[i] + offsetof(env_t, data);
210 int n = (len > sizeof(buf)) ? sizeof(buf) : len;
212 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR, off,
215 crc_tmp = crc32(crc_tmp, buf, n);
220 if (crc_tmp == crc[i])
224 if (!crc_ok[0] && !crc_ok[1]) {
229 } else if (crc_ok[0] && !crc_ok[1]) {
231 } else if (!crc_ok[0] && crc_ok[1]) {
234 /* both ok - check serial */
235 if (flags[0] == ACTIVE_FLAG && flags[1] == OBSOLETE_FLAG)
237 else if (flags[0] == OBSOLETE_FLAG && flags[1] == ACTIVE_FLAG)
239 else if (flags[0] == 0xFF && flags[1] == 0)
241 else if (flags[1] == 0xFF && flags[0] == 0)
243 else /* flags are equal - almost impossible */
247 if (gd->env_valid == 2)
248 gd->env_addr = off_env[1] + offsetof(env_t, data);
249 else if (gd->env_valid == 1)
250 gd->env_addr = off_env[0] + offsetof(env_t, data);
261 eeprom_init(); /* prepare for EEPROM read/write */
264 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
265 CONFIG_ENV_OFFSET + offsetof(env_t, crc),
266 (uchar *)&crc, sizeof(ulong));
270 off = offsetof(env_t, data);
273 int n = (len > sizeof(buf)) ? sizeof(buf) : len;
275 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
276 CONFIG_ENV_OFFSET + off, buf, n);
277 new = crc32(new, buf, n);
283 gd->env_addr = offsetof(env_t, data);