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 * SPDX-License-Identifier: GPL-2.0+
13 #include <environment.h>
14 #include <linux/stddef.h>
15 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
20 #include <linux/compiler.h> /* for BUG_ON */
22 DECLARE_GLOBAL_DATA_PTR;
26 char *env_name_spec = "EEPROM";
27 int env_eeprom_bus = -1;
29 static int eeprom_bus_read(unsigned dev_addr, unsigned offset,
30 uchar *buffer, unsigned cnt)
33 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
34 int old_bus = i2c_get_bus_num();
36 if (gd->flags & GD_FLG_RELOC) {
37 if (env_eeprom_bus == -1) {
38 I2C_MUX_DEVICE *dev = NULL;
39 dev = i2c_mux_ident_muxstring(
40 (uchar *)CONFIG_I2C_ENV_EEPROM_BUS);
42 env_eeprom_bus = dev->busid;
44 printf("error adding env eeprom bus.\n");
46 if (old_bus != env_eeprom_bus) {
47 i2c_set_bus_num(env_eeprom_bus);
48 old_bus = env_eeprom_bus;
51 rcode = i2c_mux_ident_muxstring_f(
52 (uchar *)CONFIG_I2C_ENV_EEPROM_BUS);
56 rcode = eeprom_read(dev_addr, offset, buffer, cnt);
58 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
59 if (old_bus != env_eeprom_bus)
60 i2c_set_bus_num(old_bus);
65 static int eeprom_bus_write(unsigned dev_addr, unsigned offset,
66 uchar *buffer, unsigned cnt)
69 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
70 int old_bus = i2c_get_bus_num();
72 rcode = i2c_mux_ident_muxstring_f((uchar *)CONFIG_I2C_ENV_EEPROM_BUS);
74 rcode = eeprom_write(dev_addr, offset, buffer, cnt);
75 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
76 i2c_set_bus_num(old_bus);
81 uchar env_get_char_spec(int index)
84 unsigned int off = CONFIG_ENV_OFFSET;
86 #ifdef CONFIG_ENV_OFFSET_REDUND
87 if (gd->env_valid == 2)
88 off = CONFIG_ENV_OFFSET_REDUND;
90 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
91 off + index + offsetof(env_t, data), &c, 1);
96 void env_relocate_spec(void)
98 char buf[CONFIG_ENV_SIZE];
99 unsigned int off = CONFIG_ENV_OFFSET;
101 #ifdef CONFIG_ENV_OFFSET_REDUND
102 if (gd->env_valid == 2)
103 off = CONFIG_ENV_OFFSET_REDUND;
105 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
106 off, (uchar *)buf, CONFIG_ENV_SIZE);
117 unsigned int off = CONFIG_ENV_OFFSET;
118 #ifdef CONFIG_ENV_OFFSET_REDUND
119 unsigned int off_red = CONFIG_ENV_OFFSET_REDUND;
120 char flag_obsolete = OBSOLETE_FLAG;
123 BUG_ON(env_ptr != NULL);
125 res = (char *)&env_new.data;
126 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
128 error("Cannot export environment: errno = %d\n", errno);
131 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
133 #ifdef CONFIG_ENV_OFFSET_REDUND
134 if (gd->env_valid == 1) {
135 off = CONFIG_ENV_OFFSET_REDUND;
136 off_red = CONFIG_ENV_OFFSET;
139 env_new.flags = ACTIVE_FLAG;
142 rc = eeprom_bus_write(CONFIG_SYS_DEF_EEPROM_ADDR,
143 off, (uchar *)&env_new, CONFIG_ENV_SIZE);
145 #ifdef CONFIG_ENV_OFFSET_REDUND
147 eeprom_bus_write(CONFIG_SYS_DEF_EEPROM_ADDR,
148 off_red + offsetof(env_t, flags),
149 (uchar *)&flag_obsolete, 1);
151 if (gd->env_valid == 1)
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
166 #ifdef CONFIG_ENV_OFFSET_REDUND
169 ulong len, crc[2], crc_tmp;
170 unsigned int off, off_env[2];
171 uchar buf[64], flags[2];
172 int i, crc_ok[2] = {0, 0};
174 eeprom_init(); /* prepare for EEPROM read/write */
176 off_env[0] = CONFIG_ENV_OFFSET;
177 off_env[1] = CONFIG_ENV_OFFSET_REDUND;
179 for (i = 0; i < 2; i++) {
181 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
182 off_env[i] + offsetof(env_t, crc),
183 (uchar *)&crc[i], sizeof(ulong));
185 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
186 off_env[i] + offsetof(env_t, flags),
187 (uchar *)&flags[i], sizeof(uchar));
191 off = off_env[i] + offsetof(env_t, data);
193 int n = (len > sizeof(buf)) ? sizeof(buf) : len;
195 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR, off,
198 crc_tmp = crc32(crc_tmp, buf, n);
203 if (crc_tmp == crc[i])
207 if (!crc_ok[0] && !crc_ok[1]) {
212 } else if (crc_ok[0] && !crc_ok[1]) {
214 } else if (!crc_ok[0] && crc_ok[1]) {
217 /* both ok - check serial */
218 if (flags[0] == ACTIVE_FLAG && flags[1] == OBSOLETE_FLAG)
220 else if (flags[0] == OBSOLETE_FLAG && flags[1] == ACTIVE_FLAG)
222 else if (flags[0] == 0xFF && flags[1] == 0)
224 else if (flags[1] == 0xFF && flags[0] == 0)
226 else /* flags are equal - almost impossible */
230 if (gd->env_valid == 2)
231 gd->env_addr = off_env[1] + offsetof(env_t, data);
232 else if (gd->env_valid == 1)
233 gd->env_addr = off_env[0] + offsetof(env_t, data);
244 eeprom_init(); /* prepare for EEPROM read/write */
247 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
248 CONFIG_ENV_OFFSET + offsetof(env_t, crc),
249 (uchar *)&crc, sizeof(ulong));
253 off = offsetof(env_t, data);
256 int n = (len > sizeof(buf)) ? sizeof(buf) : len;
258 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
259 CONFIG_ENV_OFFSET + off, buf, n);
260 new = crc32(new, buf, n);
266 gd->env_addr = offsetof(env_t, data);