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;
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 static int env_eeprom_get_char(int index)
67 unsigned int off = CONFIG_ENV_OFFSET;
69 #ifdef CONFIG_ENV_OFFSET_REDUND
70 if (gd->env_valid == ENV_REDUND)
71 off = CONFIG_ENV_OFFSET_REDUND;
73 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
74 off + index + offsetof(env_t, data), &c, 1);
79 static int env_eeprom_load(void)
81 char buf_env[CONFIG_ENV_SIZE];
82 unsigned int off = CONFIG_ENV_OFFSET;
84 #ifdef CONFIG_ENV_OFFSET_REDUND
85 ulong len, crc[2], crc_tmp;
86 unsigned int off_env[2];
87 uchar rdbuf[64], flags[2];
88 int i, crc_ok[2] = {0, 0};
90 eeprom_init(-1); /* prepare for EEPROM read/write */
92 off_env[0] = CONFIG_ENV_OFFSET;
93 off_env[1] = CONFIG_ENV_OFFSET_REDUND;
95 for (i = 0; i < 2; i++) {
97 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
98 off_env[i] + offsetof(env_t, crc),
99 (uchar *)&crc[i], sizeof(ulong));
101 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
102 off_env[i] + offsetof(env_t, flags),
103 (uchar *)&flags[i], sizeof(uchar));
107 off = off_env[i] + offsetof(env_t, data);
109 int n = (len > sizeof(rdbuf)) ? sizeof(rdbuf) : len;
111 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR, off,
114 crc_tmp = crc32(crc_tmp, rdbuf, n);
119 if (crc_tmp == crc[i])
123 if (!crc_ok[0] && !crc_ok[1]) {
125 gd->env_valid = ENV_INVALID;
126 } else if (crc_ok[0] && !crc_ok[1]) {
127 gd->env_valid = ENV_VALID;
128 } else if (!crc_ok[0] && crc_ok[1]) {
129 gd->env_valid = ENV_REDUND;
131 /* both ok - check serial */
132 if (flags[0] == ACTIVE_FLAG && flags[1] == OBSOLETE_FLAG)
133 gd->env_valid = ENV_VALID;
134 else if (flags[0] == OBSOLETE_FLAG && flags[1] == ACTIVE_FLAG)
135 gd->env_valid = ENV_REDUND;
136 else if (flags[0] == 0xFF && flags[1] == 0)
137 gd->env_valid = ENV_REDUND;
138 else if (flags[1] == 0xFF && flags[0] == 0)
139 gd->env_valid = ENV_VALID;
140 else /* flags are equal - almost impossible */
141 gd->env_valid = ENV_VALID;
144 #else /* CONFIG_ENV_OFFSET_REDUND */
148 eeprom_init(-1); /* prepare for EEPROM read/write */
151 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
152 CONFIG_ENV_OFFSET + offsetof(env_t, crc),
153 (uchar *)&crc, sizeof(ulong));
157 off = offsetof(env_t, data);
159 int n = (len > sizeof(rdbuf)) ? sizeof(rdbuf) : len;
161 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
162 CONFIG_ENV_OFFSET + off, rdbuf, n);
163 new = crc32(new, rdbuf, n);
169 gd->env_valid = ENV_VALID;
171 gd->env_valid = ENV_INVALID;
173 #endif /* CONFIG_ENV_OFFSET_REDUND */
175 off = CONFIG_ENV_OFFSET;
176 #ifdef CONFIG_ENV_OFFSET_REDUND
177 if (gd->env_valid == ENV_REDUND)
178 off = CONFIG_ENV_OFFSET_REDUND;
181 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
182 off, (uchar *)buf_env, CONFIG_ENV_SIZE);
184 env_import(buf_env, 1);
189 static int env_eeprom_save(void)
193 unsigned int off = CONFIG_ENV_OFFSET;
194 #ifdef CONFIG_ENV_OFFSET_REDUND
195 unsigned int off_red = CONFIG_ENV_OFFSET_REDUND;
196 char flag_obsolete = OBSOLETE_FLAG;
199 rc = env_export(&env_new);
203 #ifdef CONFIG_ENV_OFFSET_REDUND
204 if (gd->env_valid == ENV_VALID) {
205 off = CONFIG_ENV_OFFSET_REDUND;
206 off_red = CONFIG_ENV_OFFSET;
209 env_new.flags = ACTIVE_FLAG;
212 rc = eeprom_bus_write(CONFIG_SYS_DEF_EEPROM_ADDR,
213 off, (uchar *)&env_new, CONFIG_ENV_SIZE);
215 #ifdef CONFIG_ENV_OFFSET_REDUND
217 eeprom_bus_write(CONFIG_SYS_DEF_EEPROM_ADDR,
218 off_red + offsetof(env_t, flags),
219 (uchar *)&flag_obsolete, 1);
221 if (gd->env_valid == ENV_VALID)
222 gd->env_valid = ENV_REDUND;
224 gd->env_valid = ENV_VALID;
230 U_BOOT_ENV_LOCATION(eeprom) = {
231 .location = ENVL_EEPROM,
233 .get_char = env_eeprom_get_char,
234 .load = env_eeprom_load,
235 .save = env_save_ptr(env_eeprom_save),