1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2010
6 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
13 #include <env_internal.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;
24 static int eeprom_bus_read(unsigned dev_addr, unsigned offset,
25 uchar *buffer, unsigned cnt)
28 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
29 int old_bus = i2c_get_bus_num();
31 if (old_bus != CONFIG_I2C_ENV_EEPROM_BUS)
32 i2c_set_bus_num(CONFIG_I2C_ENV_EEPROM_BUS);
35 rcode = eeprom_read(dev_addr, offset, buffer, cnt);
37 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
38 i2c_set_bus_num(old_bus);
44 static int eeprom_bus_write(unsigned dev_addr, unsigned offset,
45 uchar *buffer, unsigned cnt)
48 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
49 int old_bus = i2c_get_bus_num();
51 if (old_bus != CONFIG_I2C_ENV_EEPROM_BUS)
52 i2c_set_bus_num(CONFIG_I2C_ENV_EEPROM_BUS);
55 rcode = eeprom_write(dev_addr, offset, buffer, cnt);
57 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
58 i2c_set_bus_num(old_bus);
64 /** Call this function from overridden env_get_char_spec() if you need
67 int env_eeprom_get_char(int index)
70 unsigned int off = CONFIG_ENV_OFFSET;
72 #ifdef CONFIG_ENV_OFFSET_REDUND
73 if (gd->env_valid == ENV_REDUND)
74 off = CONFIG_ENV_OFFSET_REDUND;
76 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
77 off + index + offsetof(env_t, data), &c, 1);
82 static int env_eeprom_load(void)
84 char buf_env[CONFIG_ENV_SIZE];
85 unsigned int off = CONFIG_ENV_OFFSET;
87 #ifdef CONFIG_ENV_OFFSET_REDUND
88 ulong len, crc[2], crc_tmp;
89 unsigned int off_env[2];
90 uchar rdbuf[64], flags[2];
91 int i, crc_ok[2] = {0, 0};
93 eeprom_init(-1); /* prepare for EEPROM read/write */
95 off_env[0] = CONFIG_ENV_OFFSET;
96 off_env[1] = CONFIG_ENV_OFFSET_REDUND;
98 for (i = 0; i < 2; i++) {
100 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
101 off_env[i] + offsetof(env_t, crc),
102 (uchar *)&crc[i], sizeof(ulong));
104 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
105 off_env[i] + offsetof(env_t, flags),
106 (uchar *)&flags[i], sizeof(uchar));
110 off = off_env[i] + offsetof(env_t, data);
112 int n = (len > sizeof(rdbuf)) ? sizeof(rdbuf) : len;
114 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR, off,
117 crc_tmp = crc32(crc_tmp, rdbuf, n);
122 if (crc_tmp == crc[i])
126 if (!crc_ok[0] && !crc_ok[1]) {
128 gd->env_valid = ENV_INVALID;
129 } else if (crc_ok[0] && !crc_ok[1]) {
130 gd->env_valid = ENV_VALID;
131 } else if (!crc_ok[0] && crc_ok[1]) {
132 gd->env_valid = ENV_REDUND;
134 /* both ok - check serial */
135 if (flags[0] == ENV_REDUND_ACTIVE &&
136 flags[1] == ENV_REDUND_OBSOLETE)
137 gd->env_valid = ENV_VALID;
138 else if (flags[0] == ENV_REDUND_OBSOLETE &&
139 flags[1] == ENV_REDUND_ACTIVE)
140 gd->env_valid = ENV_REDUND;
141 else if (flags[0] == 0xFF && flags[1] == 0)
142 gd->env_valid = ENV_REDUND;
143 else if (flags[1] == 0xFF && flags[0] == 0)
144 gd->env_valid = ENV_VALID;
145 else /* flags are equal - almost impossible */
146 gd->env_valid = ENV_VALID;
149 #else /* CONFIG_ENV_OFFSET_REDUND */
153 eeprom_init(-1); /* prepare for EEPROM read/write */
156 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
157 CONFIG_ENV_OFFSET + offsetof(env_t, crc),
158 (uchar *)&crc, sizeof(ulong));
162 off = offsetof(env_t, data);
164 int n = (len > sizeof(rdbuf)) ? sizeof(rdbuf) : len;
166 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
167 CONFIG_ENV_OFFSET + off, rdbuf, n);
168 new = crc32(new, rdbuf, n);
174 gd->env_valid = ENV_VALID;
176 gd->env_valid = ENV_INVALID;
178 #endif /* CONFIG_ENV_OFFSET_REDUND */
180 off = CONFIG_ENV_OFFSET;
181 #ifdef CONFIG_ENV_OFFSET_REDUND
182 if (gd->env_valid == ENV_REDUND)
183 off = CONFIG_ENV_OFFSET_REDUND;
186 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
187 off, (uchar *)buf_env, CONFIG_ENV_SIZE);
189 return env_import(buf_env, 1);
192 static int env_eeprom_save(void)
196 unsigned int off = CONFIG_ENV_OFFSET;
197 #ifdef CONFIG_ENV_OFFSET_REDUND
198 unsigned int off_red = CONFIG_ENV_OFFSET_REDUND;
199 char flag_obsolete = ENV_REDUND_OBSOLETE;
202 rc = env_export(&env_new);
206 #ifdef CONFIG_ENV_OFFSET_REDUND
207 if (gd->env_valid == ENV_VALID) {
208 off = CONFIG_ENV_OFFSET_REDUND;
209 off_red = CONFIG_ENV_OFFSET;
212 env_new.flags = ENV_REDUND_ACTIVE;
215 rc = eeprom_bus_write(CONFIG_SYS_DEF_EEPROM_ADDR,
216 off, (uchar *)&env_new, CONFIG_ENV_SIZE);
218 #ifdef CONFIG_ENV_OFFSET_REDUND
220 eeprom_bus_write(CONFIG_SYS_DEF_EEPROM_ADDR,
221 off_red + offsetof(env_t, flags),
222 (uchar *)&flag_obsolete, 1);
224 if (gd->env_valid == ENV_VALID)
225 gd->env_valid = ENV_REDUND;
227 gd->env_valid = ENV_VALID;
233 U_BOOT_ENV_LOCATION(eeprom) = {
234 .location = ENVL_EEPROM,
236 .load = env_eeprom_load,
237 .save = env_save_ptr(env_eeprom_save),