blob: e8126cfe3974695787e5cd11fe00fc9860d29406 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk0bc4a1a2002-09-18 11:09:59 +00002/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02003 * (C) Copyright 2000-2010
wdenk0bc4a1a2002-09-18 11:09:59 +00004 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 *
6 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 * Andreas Heppel <aheppel@sysgo.de>
wdenk0bc4a1a2002-09-18 11:09:59 +00008 */
9
10#include <common.h>
wdenk0bc4a1a2002-09-18 11:09:59 +000011#include <command.h>
Simon Glasseba6b8d2019-11-14 12:57:50 -070012#include <eeprom.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060013#include <env.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060014#include <env_internal.h>
wdenk0bc4a1a2002-09-18 11:09:59 +000015#include <linux/stddef.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070016#include <u-boot/crc.h>
Heiko Schocher9bb0dd52010-01-07 08:55:40 +010017#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
18#include <i2c.h>
19#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020020#include <search.h>
21#include <errno.h>
22#include <linux/compiler.h> /* for BUG_ON */
wdenk0bc4a1a2002-09-18 11:09:59 +000023
Wolfgang Denk6405a152006-03-31 18:32:53 +020024DECLARE_GLOBAL_DATA_PTR;
25
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020026static int eeprom_bus_read(unsigned dev_addr, unsigned offset,
Igor Grinberg13f851f2011-11-07 01:14:07 +000027 uchar *buffer, unsigned cnt)
Heiko Schocher9bb0dd52010-01-07 08:55:40 +010028{
29 int rcode;
30#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
31 int old_bus = i2c_get_bus_num();
32
Heiko Schochere0e55bc2012-01-16 21:12:24 +000033 if (old_bus != CONFIG_I2C_ENV_EEPROM_BUS)
34 i2c_set_bus_num(CONFIG_I2C_ENV_EEPROM_BUS);
Heiko Schocher9bb0dd52010-01-07 08:55:40 +010035#endif
36
Igor Grinberg13f851f2011-11-07 01:14:07 +000037 rcode = eeprom_read(dev_addr, offset, buffer, cnt);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020038
Heiko Schocher9bb0dd52010-01-07 08:55:40 +010039#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
Alexey Brodkinf234b1e2013-12-11 18:04:40 +040040 i2c_set_bus_num(old_bus);
Heiko Schocher9bb0dd52010-01-07 08:55:40 +010041#endif
Heiko Schochera4ea4452012-10-25 10:32:14 +020042
Heiko Schocher9bb0dd52010-01-07 08:55:40 +010043 return rcode;
44}
45
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020046static int eeprom_bus_write(unsigned dev_addr, unsigned offset,
Igor Grinberg13f851f2011-11-07 01:14:07 +000047 uchar *buffer, unsigned cnt)
Heiko Schocher9bb0dd52010-01-07 08:55:40 +010048{
49 int rcode;
50#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
51 int old_bus = i2c_get_bus_num();
52
Heiko Schochere0e55bc2012-01-16 21:12:24 +000053 if (old_bus != CONFIG_I2C_ENV_EEPROM_BUS)
54 i2c_set_bus_num(CONFIG_I2C_ENV_EEPROM_BUS);
Heiko Schocher9bb0dd52010-01-07 08:55:40 +010055#endif
Heiko Schochera4ea4452012-10-25 10:32:14 +020056
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020057 rcode = eeprom_write(dev_addr, offset, buffer, cnt);
Heiko Schochera4ea4452012-10-25 10:32:14 +020058
Heiko Schocher9bb0dd52010-01-07 08:55:40 +010059#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
60 i2c_set_bus_num(old_bus);
61#endif
Alexey Brodkinf234b1e2013-12-11 18:04:40 +040062
Heiko Schocher9bb0dd52010-01-07 08:55:40 +010063 return rcode;
64}
wdenk0bc4a1a2002-09-18 11:09:59 +000065
Goldschmidt Simon9b0d66b2018-02-09 20:23:17 +000066/** Call this function from overridden env_get_char_spec() if you need
67 * this functionality.
68 */
69int env_eeprom_get_char(int index)
wdenk0bc4a1a2002-09-18 11:09:59 +000070{
71 uchar c;
Igor Grinberg13f851f2011-11-07 01:14:07 +000072 unsigned int off = CONFIG_ENV_OFFSET;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020073
Heiko Schochere7193632010-01-07 08:55:44 +010074#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass4bc2ad22017-08-03 12:21:56 -060075 if (gd->env_valid == ENV_REDUND)
Heiko Schochere7193632010-01-07 08:55:44 +010076 off = CONFIG_ENV_OFFSET_REDUND;
77#endif
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020078 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
Igor Grinberg13f851f2011-11-07 01:14:07 +000079 off + index + offsetof(env_t, data), &c, 1);
wdenk0bc4a1a2002-09-18 11:09:59 +000080
Igor Grinberg13f851f2011-11-07 01:14:07 +000081 return c;
wdenk0bc4a1a2002-09-18 11:09:59 +000082}
83
Simon Glass99778492017-08-03 12:22:17 -060084static int env_eeprom_load(void)
wdenk0bc4a1a2002-09-18 11:09:59 +000085{
Ludger Dreierbf84e642015-10-12 13:34:24 +020086 char buf_env[CONFIG_ENV_SIZE];
Heiko Schochere7193632010-01-07 08:55:44 +010087 unsigned int off = CONFIG_ENV_OFFSET;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020088
Heiko Schochere7193632010-01-07 08:55:44 +010089#ifdef CONFIG_ENV_OFFSET_REDUND
Igor Grinberg13f851f2011-11-07 01:14:07 +000090 ulong len, crc[2], crc_tmp;
Ludger Dreierbf84e642015-10-12 13:34:24 +020091 unsigned int off_env[2];
92 uchar rdbuf[64], flags[2];
Igor Grinberg13f851f2011-11-07 01:14:07 +000093 int i, crc_ok[2] = {0, 0};
Heiko Schochere7193632010-01-07 08:55:44 +010094
Marek Vasutf422ae72015-11-10 20:53:31 +010095 eeprom_init(-1); /* prepare for EEPROM read/write */
Heiko Schochere7193632010-01-07 08:55:44 +010096
97 off_env[0] = CONFIG_ENV_OFFSET;
98 off_env[1] = CONFIG_ENV_OFFSET_REDUND;
99
100 for (i = 0; i < 2; i++) {
101 /* read CRC */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200102 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
Igor Grinberg13f851f2011-11-07 01:14:07 +0000103 off_env[i] + offsetof(env_t, crc),
104 (uchar *)&crc[i], sizeof(ulong));
Heiko Schochere7193632010-01-07 08:55:44 +0100105 /* read FLAGS */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200106 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
Igor Grinberg13f851f2011-11-07 01:14:07 +0000107 off_env[i] + offsetof(env_t, flags),
108 (uchar *)&flags[i], sizeof(uchar));
Heiko Schochere7193632010-01-07 08:55:44 +0100109
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200110 crc_tmp = 0;
Heiko Schochere7193632010-01-07 08:55:44 +0100111 len = ENV_SIZE;
Igor Grinberg13f851f2011-11-07 01:14:07 +0000112 off = off_env[i] + offsetof(env_t, data);
Heiko Schochere7193632010-01-07 08:55:44 +0100113 while (len > 0) {
Ludger Dreierbf84e642015-10-12 13:34:24 +0200114 int n = (len > sizeof(rdbuf)) ? sizeof(rdbuf) : len;
Heiko Schochere7193632010-01-07 08:55:44 +0100115
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200116 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR, off,
Ludger Dreierbf84e642015-10-12 13:34:24 +0200117 rdbuf, n);
Heiko Schochere7193632010-01-07 08:55:44 +0100118
Ludger Dreierbf84e642015-10-12 13:34:24 +0200119 crc_tmp = crc32(crc_tmp, rdbuf, n);
Heiko Schochere7193632010-01-07 08:55:44 +0100120 len -= n;
121 off += n;
122 }
Igor Grinberg13f851f2011-11-07 01:14:07 +0000123
Heiko Schochere7193632010-01-07 08:55:44 +0100124 if (crc_tmp == crc[i])
125 crc_ok[i] = 1;
126 }
127
128 if (!crc_ok[0] && !crc_ok[1]) {
Igor Grinberg13f851f2011-11-07 01:14:07 +0000129 gd->env_addr = 0;
Simon Glass1f39d0b2017-08-20 04:45:15 -0600130 gd->env_valid = ENV_INVALID;
Heiko Schochere7193632010-01-07 08:55:44 +0100131 } else if (crc_ok[0] && !crc_ok[1]) {
Simon Glass4bc2ad22017-08-03 12:21:56 -0600132 gd->env_valid = ENV_VALID;
Igor Grinberg13f851f2011-11-07 01:14:07 +0000133 } else if (!crc_ok[0] && crc_ok[1]) {
Simon Glass4bc2ad22017-08-03 12:21:56 -0600134 gd->env_valid = ENV_REDUND;
Heiko Schochere7193632010-01-07 08:55:44 +0100135 } else {
136 /* both ok - check serial */
Simon Glass4686d8f2019-08-01 09:47:08 -0600137 if (flags[0] == ENV_REDUND_ACTIVE &&
138 flags[1] == ENV_REDUND_OBSOLETE)
Simon Glass4bc2ad22017-08-03 12:21:56 -0600139 gd->env_valid = ENV_VALID;
Simon Glass4686d8f2019-08-01 09:47:08 -0600140 else if (flags[0] == ENV_REDUND_OBSOLETE &&
141 flags[1] == ENV_REDUND_ACTIVE)
Simon Glass4bc2ad22017-08-03 12:21:56 -0600142 gd->env_valid = ENV_REDUND;
Heiko Schochere7193632010-01-07 08:55:44 +0100143 else if (flags[0] == 0xFF && flags[1] == 0)
Simon Glass4bc2ad22017-08-03 12:21:56 -0600144 gd->env_valid = ENV_REDUND;
Igor Grinberg13f851f2011-11-07 01:14:07 +0000145 else if (flags[1] == 0xFF && flags[0] == 0)
Simon Glass4bc2ad22017-08-03 12:21:56 -0600146 gd->env_valid = ENV_VALID;
Heiko Schochere7193632010-01-07 08:55:44 +0100147 else /* flags are equal - almost impossible */
Simon Glass4bc2ad22017-08-03 12:21:56 -0600148 gd->env_valid = ENV_VALID;
Heiko Schochere7193632010-01-07 08:55:44 +0100149 }
150
Ludger Dreierbf84e642015-10-12 13:34:24 +0200151#else /* CONFIG_ENV_OFFSET_REDUND */
wdenk0bc4a1a2002-09-18 11:09:59 +0000152 ulong crc, len, new;
Ludger Dreierbf84e642015-10-12 13:34:24 +0200153 uchar rdbuf[64];
wdenk0bc4a1a2002-09-18 11:09:59 +0000154
Marek Vasutf422ae72015-11-10 20:53:31 +0100155 eeprom_init(-1); /* prepare for EEPROM read/write */
wdenk0bc4a1a2002-09-18 11:09:59 +0000156
157 /* read old CRC */
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200158 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
Igor Grinberg13f851f2011-11-07 01:14:07 +0000159 CONFIG_ENV_OFFSET + offsetof(env_t, crc),
160 (uchar *)&crc, sizeof(ulong));
wdenk0bc4a1a2002-09-18 11:09:59 +0000161
162 new = 0;
163 len = ENV_SIZE;
Igor Grinberg13f851f2011-11-07 01:14:07 +0000164 off = offsetof(env_t, data);
wdenk0bc4a1a2002-09-18 11:09:59 +0000165 while (len > 0) {
Ludger Dreierbf84e642015-10-12 13:34:24 +0200166 int n = (len > sizeof(rdbuf)) ? sizeof(rdbuf) : len;
wdenk0bc4a1a2002-09-18 11:09:59 +0000167
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200168 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
Ludger Dreierbf84e642015-10-12 13:34:24 +0200169 CONFIG_ENV_OFFSET + off, rdbuf, n);
170 new = crc32(new, rdbuf, n);
wdenk0bc4a1a2002-09-18 11:09:59 +0000171 len -= n;
172 off += n;
173 }
174
175 if (crc == new) {
Simon Glass1f39d0b2017-08-20 04:45:15 -0600176 gd->env_valid = ENV_VALID;
wdenk0bc4a1a2002-09-18 11:09:59 +0000177 } else {
Simon Glass1f39d0b2017-08-20 04:45:15 -0600178 gd->env_valid = ENV_INVALID;
wdenk0bc4a1a2002-09-18 11:09:59 +0000179 }
Ludger Dreierbf84e642015-10-12 13:34:24 +0200180#endif /* CONFIG_ENV_OFFSET_REDUND */
181
182 off = CONFIG_ENV_OFFSET;
183#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass4bc2ad22017-08-03 12:21:56 -0600184 if (gd->env_valid == ENV_REDUND)
Ludger Dreierbf84e642015-10-12 13:34:24 +0200185 off = CONFIG_ENV_OFFSET_REDUND;
186#endif
187
188 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
189 off, (uchar *)buf_env, CONFIG_ENV_SIZE);
190
Simon Goldschmidt35b4c322018-01-31 14:47:12 +0100191 return env_import(buf_env, 1);
Ludger Dreierbf84e642015-10-12 13:34:24 +0200192}
193
Simon Glass082af922017-08-03 12:22:01 -0600194static int env_eeprom_save(void)
Ludger Dreierbf84e642015-10-12 13:34:24 +0200195{
196 env_t env_new;
197 int rc;
198 unsigned int off = CONFIG_ENV_OFFSET;
199#ifdef CONFIG_ENV_OFFSET_REDUND
200 unsigned int off_red = CONFIG_ENV_OFFSET_REDUND;
Simon Glass4686d8f2019-08-01 09:47:08 -0600201 char flag_obsolete = ENV_REDUND_OBSOLETE;
Ludger Dreierbf84e642015-10-12 13:34:24 +0200202#endif
203
Ludger Dreierbf84e642015-10-12 13:34:24 +0200204 rc = env_export(&env_new);
205 if (rc)
206 return rc;
207
208#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass4bc2ad22017-08-03 12:21:56 -0600209 if (gd->env_valid == ENV_VALID) {
Ludger Dreierbf84e642015-10-12 13:34:24 +0200210 off = CONFIG_ENV_OFFSET_REDUND;
211 off_red = CONFIG_ENV_OFFSET;
212 }
213
Simon Glass4686d8f2019-08-01 09:47:08 -0600214 env_new.flags = ENV_REDUND_ACTIVE;
Ludger Dreierbf84e642015-10-12 13:34:24 +0200215#endif
216
217 rc = eeprom_bus_write(CONFIG_SYS_DEF_EEPROM_ADDR,
218 off, (uchar *)&env_new, CONFIG_ENV_SIZE);
219
220#ifdef CONFIG_ENV_OFFSET_REDUND
221 if (rc == 0) {
222 eeprom_bus_write(CONFIG_SYS_DEF_EEPROM_ADDR,
223 off_red + offsetof(env_t, flags),
224 (uchar *)&flag_obsolete, 1);
225
Simon Glass4bc2ad22017-08-03 12:21:56 -0600226 if (gd->env_valid == ENV_VALID)
227 gd->env_valid = ENV_REDUND;
Ludger Dreierbf84e642015-10-12 13:34:24 +0200228 else
Simon Glass4bc2ad22017-08-03 12:21:56 -0600229 gd->env_valid = ENV_VALID;
Ludger Dreierbf84e642015-10-12 13:34:24 +0200230 }
231#endif
232 return rc;
233}
234
Simon Glassc10a88e2017-08-03 12:21:58 -0600235U_BOOT_ENV_LOCATION(eeprom) = {
236 .location = ENVL_EEPROM,
Simon Glassd8273ed2017-08-03 12:22:03 -0600237 ENV_NAME("EEPROM")
Simon Glass082af922017-08-03 12:22:01 -0600238 .load = env_eeprom_load,
239 .save = env_save_ptr(env_eeprom_save),
Simon Glassc10a88e2017-08-03 12:21:58 -0600240};