2 * (C) Copyright 2000-2002
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)
35 DECLARE_GLOBAL_DATA_PTR;
37 env_t *env_ptr = NULL;
39 char * env_name_spec = "EEPROM";
40 int env_eeprom_bus = -1;
42 static int eeprom_bus_read (unsigned dev_addr, unsigned offset, uchar *buffer,
46 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
47 int old_bus = i2c_get_bus_num();
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);
55 env_eeprom_bus = dev->busid;
57 printf ("error adding env eeprom bus.\n");
59 if (old_bus != env_eeprom_bus) {
60 i2c_set_bus_num(env_eeprom_bus);
61 old_bus = env_eeprom_bus;
64 rcode = i2c_mux_ident_muxstring_f(
65 (uchar *)CONFIG_I2C_ENV_EEPROM_BUS);
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);
77 static int eeprom_bus_write (unsigned dev_addr, unsigned offset, uchar *buffer,
81 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
82 int old_bus = i2c_get_bus_num();
84 rcode = i2c_mux_ident_muxstring_f((uchar *)CONFIG_I2C_ENV_EEPROM_BUS);
86 rcode = eeprom_write (dev_addr, offset, buffer, cnt);
87 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
88 i2c_set_bus_num(old_bus);
93 uchar env_get_char_spec (int index)
97 off = CONFIG_ENV_OFFSET;
98 #ifdef CONFIG_ENV_OFFSET_REDUND
99 if (gd->env_valid == 2)
100 off = CONFIG_ENV_OFFSET_REDUND;
103 eeprom_bus_read (CONFIG_SYS_DEF_EEPROM_ADDR,
104 off + index + offsetof(env_t,data),
110 void env_relocate_spec (void)
112 unsigned int off = CONFIG_ENV_OFFSET;
113 #ifdef CONFIG_ENV_OFFSET_REDUND
114 if (gd->env_valid == 2)
115 off = CONFIG_ENV_OFFSET_REDUND;
117 eeprom_bus_read (CONFIG_SYS_DEF_EEPROM_ADDR,
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;
135 env_ptr->flags = ACTIVE_FLAG;
138 rc = eeprom_bus_write (CONFIG_SYS_DEF_EEPROM_ADDR,
143 #ifdef CONFIG_ENV_OFFSET_REDUND
145 eeprom_bus_write (CONFIG_SYS_DEF_EEPROM_ADDR,
146 off_red + offsetof(env_t,flags),
147 (uchar *)&flag_obsolete,
149 if (gd->env_valid == 1)
160 /************************************************************************
161 * Initialize Environment use
163 * We are still running from ROM, so data use is limited
164 * Use a (moderately small) buffer on the stack
167 #ifdef CONFIG_ENV_OFFSET_REDUND
171 ulong crc[2], crc_tmp;
172 unsigned int off, off_env[2];
174 int crc_ok[2] = {0,0};
175 unsigned char flags[2];
178 eeprom_init (); /* prepare for EEPROM read/write */
180 off_env[0] = CONFIG_ENV_OFFSET;
181 off_env[1] = CONFIG_ENV_OFFSET_REDUND;
183 for (i = 0; i < 2; i++) {
185 eeprom_bus_read (CONFIG_SYS_DEF_EEPROM_ADDR,
186 off_env[i] + offsetof(env_t,crc),
187 (uchar *)&crc[i], sizeof(ulong));
189 eeprom_bus_read (CONFIG_SYS_DEF_EEPROM_ADDR,
190 off_env[i] + offsetof(env_t,flags),
191 (uchar *)&flags[i], sizeof(uchar));
195 off = off_env[i] + offsetof(env_t,data);
197 int n = (len > sizeof(buf)) ? sizeof(buf) : len;
199 eeprom_bus_read (CONFIG_SYS_DEF_EEPROM_ADDR, off,
202 crc_tmp = crc32 (crc_tmp, buf, n);
206 if (crc_tmp == crc[i])
210 if (!crc_ok[0] && !crc_ok[1]) {
215 } else if (crc_ok[0] && !crc_ok[1]) {
218 else if (!crc_ok[0] && crc_ok[1]) {
221 /* both ok - check serial */
222 if (flags[0] == ACTIVE_FLAG && flags[1] == OBSOLETE_FLAG)
224 else if (flags[0] == OBSOLETE_FLAG && flags[1] == ACTIVE_FLAG)
226 else if (flags[0] == 0xFF && flags[1] == 0)
228 else if(flags[1] == 0xFF && flags[0] == 0)
230 else /* flags are equal - almost impossible */
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);
248 eeprom_init (); /* prepare for EEPROM read/write */
251 eeprom_bus_read (CONFIG_SYS_DEF_EEPROM_ADDR,
252 CONFIG_ENV_OFFSET+offsetof(env_t,crc),
253 (uchar *)&crc, sizeof(ulong));
257 off = offsetof(env_t,data);
259 int n = (len > sizeof(buf)) ? sizeof(buf) : len;
261 eeprom_bus_read (CONFIG_SYS_DEF_EEPROM_ADDR,
262 CONFIG_ENV_OFFSET + off, buf, n);
263 new = crc32 (new, buf, n);
269 gd->env_addr = offsetof(env_t,data);