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 (old_bus != CONFIG_I2C_ENV_EEPROM_BUS)
37 i2c_set_bus_num(CONFIG_I2C_ENV_EEPROM_BUS);
40 rcode = eeprom_read(dev_addr, offset, buffer, cnt);
42 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
43 if (old_bus != env_eeprom_bus)
44 i2c_set_bus_num(old_bus);
50 static int eeprom_bus_write(unsigned dev_addr, unsigned offset,
51 uchar *buffer, unsigned cnt)
54 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
55 int old_bus = i2c_get_bus_num();
57 if (old_bus != CONFIG_I2C_ENV_EEPROM_BUS)
58 i2c_set_bus_num(CONFIG_I2C_ENV_EEPROM_BUS);
61 rcode = eeprom_write(dev_addr, offset, buffer, cnt);
63 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
64 i2c_set_bus_num(old_bus);
69 uchar env_get_char_spec(int index)
72 unsigned int off = CONFIG_ENV_OFFSET;
74 #ifdef CONFIG_ENV_OFFSET_REDUND
75 if (gd->env_valid == 2)
76 off = CONFIG_ENV_OFFSET_REDUND;
78 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
79 off + index + offsetof(env_t, data), &c, 1);
84 void env_relocate_spec(void)
86 char buf[CONFIG_ENV_SIZE];
87 unsigned int off = CONFIG_ENV_OFFSET;
89 #ifdef CONFIG_ENV_OFFSET_REDUND
90 if (gd->env_valid == 2)
91 off = CONFIG_ENV_OFFSET_REDUND;
93 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
94 off, (uchar *)buf, CONFIG_ENV_SIZE);
105 unsigned int off = CONFIG_ENV_OFFSET;
106 #ifdef CONFIG_ENV_OFFSET_REDUND
107 unsigned int off_red = CONFIG_ENV_OFFSET_REDUND;
108 char flag_obsolete = OBSOLETE_FLAG;
111 BUG_ON(env_ptr != NULL);
113 res = (char *)&env_new.data;
114 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
116 error("Cannot export environment: errno = %d\n", errno);
119 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
121 #ifdef CONFIG_ENV_OFFSET_REDUND
122 if (gd->env_valid == 1) {
123 off = CONFIG_ENV_OFFSET_REDUND;
124 off_red = CONFIG_ENV_OFFSET;
127 env_new.flags = ACTIVE_FLAG;
130 rc = eeprom_bus_write(CONFIG_SYS_DEF_EEPROM_ADDR,
131 off, (uchar *)&env_new, CONFIG_ENV_SIZE);
133 #ifdef CONFIG_ENV_OFFSET_REDUND
135 eeprom_bus_write(CONFIG_SYS_DEF_EEPROM_ADDR,
136 off_red + offsetof(env_t, flags),
137 (uchar *)&flag_obsolete, 1);
139 if (gd->env_valid == 1)
149 * Initialize Environment use
151 * We are still running from ROM, so data use is limited.
152 * Use a (moderately small) buffer on the stack
154 #ifdef CONFIG_ENV_OFFSET_REDUND
157 ulong len, crc[2], crc_tmp;
158 unsigned int off, off_env[2];
159 uchar buf[64], flags[2];
160 int i, crc_ok[2] = {0, 0};
162 eeprom_init(); /* prepare for EEPROM read/write */
164 off_env[0] = CONFIG_ENV_OFFSET;
165 off_env[1] = CONFIG_ENV_OFFSET_REDUND;
167 for (i = 0; i < 2; i++) {
169 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
170 off_env[i] + offsetof(env_t, crc),
171 (uchar *)&crc[i], sizeof(ulong));
173 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
174 off_env[i] + offsetof(env_t, flags),
175 (uchar *)&flags[i], sizeof(uchar));
179 off = off_env[i] + offsetof(env_t, data);
181 int n = (len > sizeof(buf)) ? sizeof(buf) : len;
183 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR, off,
186 crc_tmp = crc32(crc_tmp, buf, n);
191 if (crc_tmp == crc[i])
195 if (!crc_ok[0] && !crc_ok[1]) {
200 } else if (crc_ok[0] && !crc_ok[1]) {
202 } else if (!crc_ok[0] && crc_ok[1]) {
205 /* both ok - check serial */
206 if (flags[0] == ACTIVE_FLAG && flags[1] == OBSOLETE_FLAG)
208 else if (flags[0] == OBSOLETE_FLAG && flags[1] == ACTIVE_FLAG)
210 else if (flags[0] == 0xFF && flags[1] == 0)
212 else if (flags[1] == 0xFF && flags[0] == 0)
214 else /* flags are equal - almost impossible */
218 if (gd->env_valid == 2)
219 gd->env_addr = off_env[1] + offsetof(env_t, data);
220 else if (gd->env_valid == 1)
221 gd->env_addr = off_env[0] + offsetof(env_t, data);
232 eeprom_init(); /* prepare for EEPROM read/write */
235 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
236 CONFIG_ENV_OFFSET + offsetof(env_t, crc),
237 (uchar *)&crc, sizeof(ulong));
241 off = offsetof(env_t, data);
244 int n = (len > sizeof(buf)) ? sizeof(buf) : len;
246 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
247 CONFIG_ENV_OFFSET + off, buf, n);
248 new = crc32(new, buf, n);
254 gd->env_addr = offsetof(env_t, data);