blob: e8126cfe3974695787e5cd11fe00fc9860d29406 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk0bc4a1a2002-09-18 11:09:59 +00002/*
Wolfgang Denkea882ba2010-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 Glasscb3ef682019-11-14 12:57:50 -070012#include <eeprom.h>
Simon Glass7b51b572019-08-01 09:46:52 -060013#include <env.h>
Simon Glassf3998fd2019-08-02 09:44:25 -060014#include <env_internal.h>
wdenk0bc4a1a2002-09-18 11:09:59 +000015#include <linux/stddef.h>
Simon Glass3db71102019-11-14 12:57:16 -070016#include <u-boot/crc.h>
Heiko Schocher548738b2010-01-07 08:55:40 +010017#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
18#include <i2c.h>
19#endif
Wolfgang Denkea882ba2010-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 Denkd87080b2006-03-31 18:32:53 +020024DECLARE_GLOBAL_DATA_PTR;
25
Wolfgang Denkea882ba2010-06-20 23:33:59 +020026static int eeprom_bus_read(unsigned dev_addr, unsigned offset,
Igor Grinbergdd2a2332011-11-07 01:14:07 +000027 uchar *buffer, unsigned cnt)
Heiko Schocher548738b2010-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 Schocher3f4978c2012-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 Schocher548738b2010-01-07 08:55:40 +010035#endif
36
Igor Grinbergdd2a2332011-11-07 01:14:07 +000037 rcode = eeprom_read(dev_addr, offset, buffer, cnt);
Wolfgang Denkea882ba2010-06-20 23:33:59 +020038
Heiko Schocher548738b2010-01-07 08:55:40 +010039#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
Alexey Brodkin6d001e72013-12-11 18:04:40 +040040 i2c_set_bus_num(old_bus);
Heiko Schocher548738b2010-01-07 08:55:40 +010041#endif
Heiko Schocher9a2accb2012-10-25 10:32:14 +020042
Heiko Schocher548738b2010-01-07 08:55:40 +010043 return rcode;
44}
45
Wolfgang Denkea882ba2010-06-20 23:33:59 +020046static int eeprom_bus_write(unsigned dev_addr, unsigned offset,
Igor Grinbergdd2a2332011-11-07 01:14:07 +000047 uchar *buffer, unsigned cnt)
Heiko Schocher548738b2010-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 Schocher3f4978c2012-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 Schocher548738b2010-01-07 08:55:40 +010055#endif
Heiko Schocher9a2accb2012-10-25 10:32:14 +020056
Wolfgang Denkea882ba2010-06-20 23:33:59 +020057 rcode = eeprom_write(dev_addr, offset, buffer, cnt);
Heiko Schocher9a2accb2012-10-25 10:32:14 +020058
Heiko Schocher548738b2010-01-07 08:55:40 +010059#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
60 i2c_set_bus_num(old_bus);
61#endif
Alexey Brodkin6d001e72013-12-11 18:04:40 +040062
Heiko Schocher548738b2010-01-07 08:55:40 +010063 return rcode;
64}
wdenk0bc4a1a2002-09-18 11:09:59 +000065
Goldschmidt Simonb2cdef42018-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 Grinbergdd2a2332011-11-07 01:14:07 +000072 unsigned int off = CONFIG_ENV_OFFSET;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020073
Heiko Schocher1567b592010-01-07 08:55:44 +010074#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass203e94f2017-08-03 12:21:56 -060075 if (gd->env_valid == ENV_REDUND)
Heiko Schocher1567b592010-01-07 08:55:44 +010076 off = CONFIG_ENV_OFFSET_REDUND;
77#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +020078 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
Igor Grinbergdd2a2332011-11-07 01:14:07 +000079 off + index + offsetof(env_t, data), &c, 1);
wdenk0bc4a1a2002-09-18 11:09:59 +000080
Igor Grinbergdd2a2332011-11-07 01:14:07 +000081 return c;
wdenk0bc4a1a2002-09-18 11:09:59 +000082}
83
Simon Glassc5951992017-08-03 12:22:17 -060084static int env_eeprom_load(void)
wdenk0bc4a1a2002-09-18 11:09:59 +000085{
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +020086 char buf_env[CONFIG_ENV_SIZE];
Heiko Schocher1567b592010-01-07 08:55:44 +010087 unsigned int off = CONFIG_ENV_OFFSET;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020088
Heiko Schocher1567b592010-01-07 08:55:44 +010089#ifdef CONFIG_ENV_OFFSET_REDUND
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +020090 ulong len, crc[2], crc_tmp;
91 unsigned int off_env[2];
92 uchar rdbuf[64], flags[2];
93 int i, crc_ok[2] = {0, 0};
94
Marek Vasut354e3ed2015-11-10 20:53:31 +010095 eeprom_init(-1); /* prepare for EEPROM read/write */
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +020096
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 */
102 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
103 off_env[i] + offsetof(env_t, crc),
104 (uchar *)&crc[i], sizeof(ulong));
105 /* read FLAGS */
106 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
107 off_env[i] + offsetof(env_t, flags),
108 (uchar *)&flags[i], sizeof(uchar));
109
110 crc_tmp = 0;
111 len = ENV_SIZE;
112 off = off_env[i] + offsetof(env_t, data);
113 while (len > 0) {
114 int n = (len > sizeof(rdbuf)) ? sizeof(rdbuf) : len;
115
116 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR, off,
117 rdbuf, n);
118
119 crc_tmp = crc32(crc_tmp, rdbuf, n);
120 len -= n;
121 off += n;
122 }
123
124 if (crc_tmp == crc[i])
125 crc_ok[i] = 1;
126 }
127
128 if (!crc_ok[0] && !crc_ok[1]) {
129 gd->env_addr = 0;
Simon Glass2d7cb5b2017-08-20 04:45:15 -0600130 gd->env_valid = ENV_INVALID;
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200131 } else if (crc_ok[0] && !crc_ok[1]) {
Simon Glass203e94f2017-08-03 12:21:56 -0600132 gd->env_valid = ENV_VALID;
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200133 } else if (!crc_ok[0] && crc_ok[1]) {
Simon Glass203e94f2017-08-03 12:21:56 -0600134 gd->env_valid = ENV_REDUND;
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200135 } else {
136 /* both ok - check serial */
Simon Glassd3716dd2019-08-01 09:47:08 -0600137 if (flags[0] == ENV_REDUND_ACTIVE &&
138 flags[1] == ENV_REDUND_OBSOLETE)
Simon Glass203e94f2017-08-03 12:21:56 -0600139 gd->env_valid = ENV_VALID;
Simon Glassd3716dd2019-08-01 09:47:08 -0600140 else if (flags[0] == ENV_REDUND_OBSOLETE &&
141 flags[1] == ENV_REDUND_ACTIVE)
Simon Glass203e94f2017-08-03 12:21:56 -0600142 gd->env_valid = ENV_REDUND;
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200143 else if (flags[0] == 0xFF && flags[1] == 0)
Simon Glass203e94f2017-08-03 12:21:56 -0600144 gd->env_valid = ENV_REDUND;
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200145 else if (flags[1] == 0xFF && flags[0] == 0)
Simon Glass203e94f2017-08-03 12:21:56 -0600146 gd->env_valid = ENV_VALID;
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200147 else /* flags are equal - almost impossible */
Simon Glass203e94f2017-08-03 12:21:56 -0600148 gd->env_valid = ENV_VALID;
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200149 }
150
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200151#else /* CONFIG_ENV_OFFSET_REDUND */
152 ulong crc, len, new;
153 uchar rdbuf[64];
154
Marek Vasut354e3ed2015-11-10 20:53:31 +0100155 eeprom_init(-1); /* prepare for EEPROM read/write */
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200156
157 /* read old CRC */
158 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
159 CONFIG_ENV_OFFSET + offsetof(env_t, crc),
160 (uchar *)&crc, sizeof(ulong));
161
162 new = 0;
163 len = ENV_SIZE;
164 off = offsetof(env_t, data);
165 while (len > 0) {
166 int n = (len > sizeof(rdbuf)) ? sizeof(rdbuf) : len;
167
168 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
169 CONFIG_ENV_OFFSET + off, rdbuf, n);
170 new = crc32(new, rdbuf, n);
171 len -= n;
172 off += n;
173 }
174
175 if (crc == new) {
Simon Glass2d7cb5b2017-08-20 04:45:15 -0600176 gd->env_valid = ENV_VALID;
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200177 } else {
Simon Glass2d7cb5b2017-08-20 04:45:15 -0600178 gd->env_valid = ENV_INVALID;
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200179 }
180#endif /* CONFIG_ENV_OFFSET_REDUND */
181
182 off = CONFIG_ENV_OFFSET;
183#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass203e94f2017-08-03 12:21:56 -0600184 if (gd->env_valid == ENV_REDUND)
Heiko Schocher1567b592010-01-07 08:55:44 +0100185 off = CONFIG_ENV_OFFSET_REDUND;
186#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200187
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200188 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
189 off, (uchar *)buf_env, CONFIG_ENV_SIZE);
190
Simon Goldschmidt2166ebf2018-01-31 14:47:12 +0100191 return env_import(buf_env, 1);
wdenk0bc4a1a2002-09-18 11:09:59 +0000192}
193
Simon Glasse5bce242017-08-03 12:22:01 -0600194static int env_eeprom_save(void)
wdenk0bc4a1a2002-09-18 11:09:59 +0000195{
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400196 env_t env_new;
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000197 int rc;
198 unsigned int off = CONFIG_ENV_OFFSET;
Heiko Schocher1567b592010-01-07 08:55:44 +0100199#ifdef CONFIG_ENV_OFFSET_REDUND
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000200 unsigned int off_red = CONFIG_ENV_OFFSET_REDUND;
Simon Glassd3716dd2019-08-01 09:47:08 -0600201 char flag_obsolete = ENV_REDUND_OBSOLETE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200202#endif
203
Marek Vasut7ce15262014-03-05 19:59:50 +0100204 rc = env_export(&env_new);
205 if (rc)
206 return rc;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200207
208#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass203e94f2017-08-03 12:21:56 -0600209 if (gd->env_valid == ENV_VALID) {
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000210 off = CONFIG_ENV_OFFSET_REDUND;
211 off_red = CONFIG_ENV_OFFSET;
Heiko Schocher1567b592010-01-07 08:55:44 +0100212 }
213
Simon Glassd3716dd2019-08-01 09:47:08 -0600214 env_new.flags = ENV_REDUND_ACTIVE;
Heiko Schocher1567b592010-01-07 08:55:44 +0100215#endif
216
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200217 rc = eeprom_bus_write(CONFIG_SYS_DEF_EEPROM_ADDR,
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400218 off, (uchar *)&env_new, CONFIG_ENV_SIZE);
Heiko Schocher1567b592010-01-07 08:55:44 +0100219
220#ifdef CONFIG_ENV_OFFSET_REDUND
221 if (rc == 0) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200222 eeprom_bus_write(CONFIG_SYS_DEF_EEPROM_ADDR,
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000223 off_red + offsetof(env_t, flags),
224 (uchar *)&flag_obsolete, 1);
225
Simon Glass203e94f2017-08-03 12:21:56 -0600226 if (gd->env_valid == ENV_VALID)
227 gd->env_valid = ENV_REDUND;
Heiko Schocher1567b592010-01-07 08:55:44 +0100228 else
Simon Glass203e94f2017-08-03 12:21:56 -0600229 gd->env_valid = ENV_VALID;
Heiko Schocher1567b592010-01-07 08:55:44 +0100230 }
231#endif
Heiko Schocher1567b592010-01-07 08:55:44 +0100232 return rc;
wdenk0bc4a1a2002-09-18 11:09:59 +0000233}
234
Simon Glass4415f1d2017-08-03 12:21:58 -0600235U_BOOT_ENV_LOCATION(eeprom) = {
236 .location = ENVL_EEPROM,
Simon Glassac358be2017-08-03 12:22:03 -0600237 ENV_NAME("EEPROM")
Simon Glasse5bce242017-08-03 12:22:01 -0600238 .load = env_eeprom_load,
239 .save = env_save_ptr(env_eeprom_save),
Simon Glass4415f1d2017-08-03 12:21:58 -0600240};