1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2010
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 * Andreas Heppel <aheppel@sysgo.de>
14 #include <env_internal.h>
15 #include <asm/global_data.h>
16 #include <linux/stddef.h>
17 #include <u-boot/crc.h>
18 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
23 #include <linux/compiler.h> /* for BUG_ON */
25 DECLARE_GLOBAL_DATA_PTR;
27 static int eeprom_bus_read(unsigned dev_addr, unsigned offset,
28 uchar *buffer, unsigned cnt)
31 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
32 int old_bus = i2c_get_bus_num();
34 if (old_bus != CONFIG_I2C_ENV_EEPROM_BUS)
35 i2c_set_bus_num(CONFIG_I2C_ENV_EEPROM_BUS);
38 rcode = eeprom_read(dev_addr, offset, buffer, cnt);
40 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
41 i2c_set_bus_num(old_bus);
47 static int eeprom_bus_write(unsigned dev_addr, unsigned offset,
48 uchar *buffer, unsigned cnt)
51 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
52 int old_bus = i2c_get_bus_num();
54 if (old_bus != CONFIG_I2C_ENV_EEPROM_BUS)
55 i2c_set_bus_num(CONFIG_I2C_ENV_EEPROM_BUS);
58 rcode = eeprom_write(dev_addr, offset, buffer, cnt);
60 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
61 i2c_set_bus_num(old_bus);
67 static int env_eeprom_load(void)
69 char buf_env[CONFIG_ENV_SIZE];
70 unsigned int off = CONFIG_ENV_OFFSET;
72 #ifdef CONFIG_ENV_OFFSET_REDUND
73 ulong len, crc[2], crc_tmp;
74 unsigned int off_env[2];
75 uchar rdbuf[64], flags[2];
76 int i, crc_ok[2] = {0, 0};
78 eeprom_init(-1); /* prepare for EEPROM read/write */
80 off_env[0] = CONFIG_ENV_OFFSET;
81 off_env[1] = CONFIG_ENV_OFFSET_REDUND;
83 for (i = 0; i < 2; i++) {
85 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR,
86 off_env[i] + offsetof(env_t, crc),
87 (uchar *)&crc[i], sizeof(ulong));
89 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR,
90 off_env[i] + offsetof(env_t, flags),
91 (uchar *)&flags[i], sizeof(uchar));
95 off = off_env[i] + offsetof(env_t, data);
97 int n = (len > sizeof(rdbuf)) ? sizeof(rdbuf) : len;
99 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR, off,
102 crc_tmp = crc32(crc_tmp, rdbuf, n);
107 if (crc_tmp == crc[i])
111 if (!crc_ok[0] && !crc_ok[1]) {
113 gd->env_valid = ENV_INVALID;
114 } else if (crc_ok[0] && !crc_ok[1]) {
115 gd->env_valid = ENV_VALID;
116 } else if (!crc_ok[0] && crc_ok[1]) {
117 gd->env_valid = ENV_REDUND;
119 /* both ok - check serial */
120 if (flags[0] == ENV_REDUND_ACTIVE &&
121 flags[1] == ENV_REDUND_OBSOLETE)
122 gd->env_valid = ENV_VALID;
123 else if (flags[0] == ENV_REDUND_OBSOLETE &&
124 flags[1] == ENV_REDUND_ACTIVE)
125 gd->env_valid = ENV_REDUND;
126 else if (flags[0] == 0xFF && flags[1] == 0)
127 gd->env_valid = ENV_REDUND;
128 else if (flags[1] == 0xFF && flags[0] == 0)
129 gd->env_valid = ENV_VALID;
130 else /* flags are equal - almost impossible */
131 gd->env_valid = ENV_VALID;
134 #else /* CONFIG_ENV_OFFSET_REDUND */
138 eeprom_init(-1); /* prepare for EEPROM read/write */
141 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR,
142 CONFIG_ENV_OFFSET + offsetof(env_t, crc),
143 (uchar *)&crc, sizeof(ulong));
147 off = offsetof(env_t, data);
149 int n = (len > sizeof(rdbuf)) ? sizeof(rdbuf) : len;
151 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR,
152 CONFIG_ENV_OFFSET + off, rdbuf, n);
153 new = crc32(new, rdbuf, n);
159 gd->env_valid = ENV_VALID;
161 gd->env_valid = ENV_INVALID;
163 #endif /* CONFIG_ENV_OFFSET_REDUND */
165 off = CONFIG_ENV_OFFSET;
166 #ifdef CONFIG_ENV_OFFSET_REDUND
167 if (gd->env_valid == ENV_REDUND)
168 off = CONFIG_ENV_OFFSET_REDUND;
171 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR,
172 off, (uchar *)buf_env, CONFIG_ENV_SIZE);
174 return env_import(buf_env, 1, H_EXTERNAL);
177 static int env_eeprom_save(void)
181 unsigned int off = CONFIG_ENV_OFFSET;
182 #ifdef CONFIG_ENV_OFFSET_REDUND
183 unsigned int off_red = CONFIG_ENV_OFFSET_REDUND;
184 char flag_obsolete = ENV_REDUND_OBSOLETE;
187 rc = env_export(&env_new);
191 #ifdef CONFIG_ENV_OFFSET_REDUND
192 if (gd->env_valid == ENV_VALID) {
193 off = CONFIG_ENV_OFFSET_REDUND;
194 off_red = CONFIG_ENV_OFFSET;
197 env_new.flags = ENV_REDUND_ACTIVE;
200 rc = eeprom_bus_write(CONFIG_SYS_I2C_EEPROM_ADDR,
201 off, (uchar *)&env_new, CONFIG_ENV_SIZE);
203 #ifdef CONFIG_ENV_OFFSET_REDUND
205 eeprom_bus_write(CONFIG_SYS_I2C_EEPROM_ADDR,
206 off_red + offsetof(env_t, flags),
207 (uchar *)&flag_obsolete, 1);
209 if (gd->env_valid == ENV_VALID)
210 gd->env_valid = ENV_REDUND;
212 gd->env_valid = ENV_VALID;
218 U_BOOT_ENV_LOCATION(eeprom) = {
219 .location = ENVL_EEPROM,
221 .load = env_eeprom_load,
222 .save = env_save_ptr(env_eeprom_save),