blob: 253bdf14284a36ef918fd635c275f29d63a2eed9 [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>
Simon Glass401d1c42020-10-30 21:38:53 -060015#include <asm/global_data.h>
wdenk0bc4a1a2002-09-18 11:09:59 +000016#include <linux/stddef.h>
Simon Glass3db71102019-11-14 12:57:16 -070017#include <u-boot/crc.h>
Heiko Schocher548738b2010-01-07 08:55:40 +010018#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
19#include <i2c.h>
20#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +020021#include <search.h>
22#include <errno.h>
23#include <linux/compiler.h> /* for BUG_ON */
wdenk0bc4a1a2002-09-18 11:09:59 +000024
Wolfgang Denkd87080b2006-03-31 18:32:53 +020025DECLARE_GLOBAL_DATA_PTR;
26
Wolfgang Denkea882ba2010-06-20 23:33:59 +020027static int eeprom_bus_read(unsigned dev_addr, unsigned offset,
Igor Grinbergdd2a2332011-11-07 01:14:07 +000028 uchar *buffer, unsigned cnt)
Heiko Schocher548738b2010-01-07 08:55:40 +010029{
30 int rcode;
31#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
32 int old_bus = i2c_get_bus_num();
33
Heiko Schocher3f4978c2012-01-16 21:12:24 +000034 if (old_bus != CONFIG_I2C_ENV_EEPROM_BUS)
35 i2c_set_bus_num(CONFIG_I2C_ENV_EEPROM_BUS);
Heiko Schocher548738b2010-01-07 08:55:40 +010036#endif
37
Igor Grinbergdd2a2332011-11-07 01:14:07 +000038 rcode = eeprom_read(dev_addr, offset, buffer, cnt);
Wolfgang Denkea882ba2010-06-20 23:33:59 +020039
Heiko Schocher548738b2010-01-07 08:55:40 +010040#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
Alexey Brodkin6d001e72013-12-11 18:04:40 +040041 i2c_set_bus_num(old_bus);
Heiko Schocher548738b2010-01-07 08:55:40 +010042#endif
Heiko Schocher9a2accb2012-10-25 10:32:14 +020043
Heiko Schocher548738b2010-01-07 08:55:40 +010044 return rcode;
45}
46
Wolfgang Denkea882ba2010-06-20 23:33:59 +020047static int eeprom_bus_write(unsigned dev_addr, unsigned offset,
Igor Grinbergdd2a2332011-11-07 01:14:07 +000048 uchar *buffer, unsigned cnt)
Heiko Schocher548738b2010-01-07 08:55:40 +010049{
50 int rcode;
51#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
52 int old_bus = i2c_get_bus_num();
53
Heiko Schocher3f4978c2012-01-16 21:12:24 +000054 if (old_bus != CONFIG_I2C_ENV_EEPROM_BUS)
55 i2c_set_bus_num(CONFIG_I2C_ENV_EEPROM_BUS);
Heiko Schocher548738b2010-01-07 08:55:40 +010056#endif
Heiko Schocher9a2accb2012-10-25 10:32:14 +020057
Wolfgang Denkea882ba2010-06-20 23:33:59 +020058 rcode = eeprom_write(dev_addr, offset, buffer, cnt);
Heiko Schocher9a2accb2012-10-25 10:32:14 +020059
Heiko Schocher548738b2010-01-07 08:55:40 +010060#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
61 i2c_set_bus_num(old_bus);
62#endif
Alexey Brodkin6d001e72013-12-11 18:04:40 +040063
Heiko Schocher548738b2010-01-07 08:55:40 +010064 return rcode;
65}
wdenk0bc4a1a2002-09-18 11:09:59 +000066
Goldschmidt Simonb2cdef42018-02-09 20:23:17 +000067/** Call this function from overridden env_get_char_spec() if you need
68 * this functionality.
69 */
70int env_eeprom_get_char(int index)
wdenk0bc4a1a2002-09-18 11:09:59 +000071{
72 uchar c;
Igor Grinbergdd2a2332011-11-07 01:14:07 +000073 unsigned int off = CONFIG_ENV_OFFSET;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020074
Heiko Schocher1567b592010-01-07 08:55:44 +010075#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass203e94f2017-08-03 12:21:56 -060076 if (gd->env_valid == ENV_REDUND)
Heiko Schocher1567b592010-01-07 08:55:44 +010077 off = CONFIG_ENV_OFFSET_REDUND;
78#endif
Tom Rini88cd7d02021-08-17 17:59:45 -040079 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR,
Igor Grinbergdd2a2332011-11-07 01:14:07 +000080 off + index + offsetof(env_t, data), &c, 1);
wdenk0bc4a1a2002-09-18 11:09:59 +000081
Igor Grinbergdd2a2332011-11-07 01:14:07 +000082 return c;
wdenk0bc4a1a2002-09-18 11:09:59 +000083}
84
Simon Glassc5951992017-08-03 12:22:17 -060085static int env_eeprom_load(void)
wdenk0bc4a1a2002-09-18 11:09:59 +000086{
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +020087 char buf_env[CONFIG_ENV_SIZE];
Heiko Schocher1567b592010-01-07 08:55:44 +010088 unsigned int off = CONFIG_ENV_OFFSET;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020089
Heiko Schocher1567b592010-01-07 08:55:44 +010090#ifdef CONFIG_ENV_OFFSET_REDUND
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +020091 ulong len, crc[2], crc_tmp;
92 unsigned int off_env[2];
93 uchar rdbuf[64], flags[2];
94 int i, crc_ok[2] = {0, 0};
95
Marek Vasut354e3ed2015-11-10 20:53:31 +010096 eeprom_init(-1); /* prepare for EEPROM read/write */
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +020097
98 off_env[0] = CONFIG_ENV_OFFSET;
99 off_env[1] = CONFIG_ENV_OFFSET_REDUND;
100
101 for (i = 0; i < 2; i++) {
102 /* read CRC */
Tom Rini88cd7d02021-08-17 17:59:45 -0400103 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR,
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200104 off_env[i] + offsetof(env_t, crc),
105 (uchar *)&crc[i], sizeof(ulong));
106 /* read FLAGS */
Tom Rini88cd7d02021-08-17 17:59:45 -0400107 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR,
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200108 off_env[i] + offsetof(env_t, flags),
109 (uchar *)&flags[i], sizeof(uchar));
110
111 crc_tmp = 0;
112 len = ENV_SIZE;
113 off = off_env[i] + offsetof(env_t, data);
114 while (len > 0) {
115 int n = (len > sizeof(rdbuf)) ? sizeof(rdbuf) : len;
116
Tom Rini88cd7d02021-08-17 17:59:45 -0400117 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR, off,
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200118 rdbuf, n);
119
120 crc_tmp = crc32(crc_tmp, rdbuf, n);
121 len -= n;
122 off += n;
123 }
124
125 if (crc_tmp == crc[i])
126 crc_ok[i] = 1;
127 }
128
129 if (!crc_ok[0] && !crc_ok[1]) {
130 gd->env_addr = 0;
Simon Glass2d7cb5b2017-08-20 04:45:15 -0600131 gd->env_valid = ENV_INVALID;
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200132 } else if (crc_ok[0] && !crc_ok[1]) {
Simon Glass203e94f2017-08-03 12:21:56 -0600133 gd->env_valid = ENV_VALID;
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200134 } else if (!crc_ok[0] && crc_ok[1]) {
Simon Glass203e94f2017-08-03 12:21:56 -0600135 gd->env_valid = ENV_REDUND;
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200136 } else {
137 /* both ok - check serial */
Simon Glassd3716dd2019-08-01 09:47:08 -0600138 if (flags[0] == ENV_REDUND_ACTIVE &&
139 flags[1] == ENV_REDUND_OBSOLETE)
Simon Glass203e94f2017-08-03 12:21:56 -0600140 gd->env_valid = ENV_VALID;
Simon Glassd3716dd2019-08-01 09:47:08 -0600141 else if (flags[0] == ENV_REDUND_OBSOLETE &&
142 flags[1] == ENV_REDUND_ACTIVE)
Simon Glass203e94f2017-08-03 12:21:56 -0600143 gd->env_valid = ENV_REDUND;
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200144 else if (flags[0] == 0xFF && flags[1] == 0)
Simon Glass203e94f2017-08-03 12:21:56 -0600145 gd->env_valid = ENV_REDUND;
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200146 else if (flags[1] == 0xFF && flags[0] == 0)
Simon Glass203e94f2017-08-03 12:21:56 -0600147 gd->env_valid = ENV_VALID;
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200148 else /* flags are equal - almost impossible */
Simon Glass203e94f2017-08-03 12:21:56 -0600149 gd->env_valid = ENV_VALID;
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200150 }
151
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200152#else /* CONFIG_ENV_OFFSET_REDUND */
153 ulong crc, len, new;
154 uchar rdbuf[64];
155
Marek Vasut354e3ed2015-11-10 20:53:31 +0100156 eeprom_init(-1); /* prepare for EEPROM read/write */
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200157
158 /* read old CRC */
Tom Rini88cd7d02021-08-17 17:59:45 -0400159 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR,
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200160 CONFIG_ENV_OFFSET + offsetof(env_t, crc),
161 (uchar *)&crc, sizeof(ulong));
162
163 new = 0;
164 len = ENV_SIZE;
165 off = offsetof(env_t, data);
166 while (len > 0) {
167 int n = (len > sizeof(rdbuf)) ? sizeof(rdbuf) : len;
168
Tom Rini88cd7d02021-08-17 17:59:45 -0400169 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR,
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200170 CONFIG_ENV_OFFSET + off, rdbuf, n);
171 new = crc32(new, rdbuf, n);
172 len -= n;
173 off += n;
174 }
175
176 if (crc == new) {
Simon Glass2d7cb5b2017-08-20 04:45:15 -0600177 gd->env_valid = ENV_VALID;
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200178 } else {
Simon Glass2d7cb5b2017-08-20 04:45:15 -0600179 gd->env_valid = ENV_INVALID;
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200180 }
181#endif /* CONFIG_ENV_OFFSET_REDUND */
182
183 off = CONFIG_ENV_OFFSET;
184#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass203e94f2017-08-03 12:21:56 -0600185 if (gd->env_valid == ENV_REDUND)
Heiko Schocher1567b592010-01-07 08:55:44 +0100186 off = CONFIG_ENV_OFFSET_REDUND;
187#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200188
Tom Rini88cd7d02021-08-17 17:59:45 -0400189 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR,
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200190 off, (uchar *)buf_env, CONFIG_ENV_SIZE);
191
Marek Vasut890feec2020-07-07 20:51:35 +0200192 return env_import(buf_env, 1, H_EXTERNAL);
wdenk0bc4a1a2002-09-18 11:09:59 +0000193}
194
Simon Glasse5bce242017-08-03 12:22:01 -0600195static int env_eeprom_save(void)
wdenk0bc4a1a2002-09-18 11:09:59 +0000196{
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400197 env_t env_new;
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000198 int rc;
199 unsigned int off = CONFIG_ENV_OFFSET;
Heiko Schocher1567b592010-01-07 08:55:44 +0100200#ifdef CONFIG_ENV_OFFSET_REDUND
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000201 unsigned int off_red = CONFIG_ENV_OFFSET_REDUND;
Simon Glassd3716dd2019-08-01 09:47:08 -0600202 char flag_obsolete = ENV_REDUND_OBSOLETE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200203#endif
204
Marek Vasut7ce15262014-03-05 19:59:50 +0100205 rc = env_export(&env_new);
206 if (rc)
207 return rc;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200208
209#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass203e94f2017-08-03 12:21:56 -0600210 if (gd->env_valid == ENV_VALID) {
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000211 off = CONFIG_ENV_OFFSET_REDUND;
212 off_red = CONFIG_ENV_OFFSET;
Heiko Schocher1567b592010-01-07 08:55:44 +0100213 }
214
Simon Glassd3716dd2019-08-01 09:47:08 -0600215 env_new.flags = ENV_REDUND_ACTIVE;
Heiko Schocher1567b592010-01-07 08:55:44 +0100216#endif
217
Tom Rini88cd7d02021-08-17 17:59:45 -0400218 rc = eeprom_bus_write(CONFIG_SYS_I2C_EEPROM_ADDR,
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400219 off, (uchar *)&env_new, CONFIG_ENV_SIZE);
Heiko Schocher1567b592010-01-07 08:55:44 +0100220
221#ifdef CONFIG_ENV_OFFSET_REDUND
222 if (rc == 0) {
Tom Rini88cd7d02021-08-17 17:59:45 -0400223 eeprom_bus_write(CONFIG_SYS_I2C_EEPROM_ADDR,
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000224 off_red + offsetof(env_t, flags),
225 (uchar *)&flag_obsolete, 1);
226
Simon Glass203e94f2017-08-03 12:21:56 -0600227 if (gd->env_valid == ENV_VALID)
228 gd->env_valid = ENV_REDUND;
Heiko Schocher1567b592010-01-07 08:55:44 +0100229 else
Simon Glass203e94f2017-08-03 12:21:56 -0600230 gd->env_valid = ENV_VALID;
Heiko Schocher1567b592010-01-07 08:55:44 +0100231 }
232#endif
Heiko Schocher1567b592010-01-07 08:55:44 +0100233 return rc;
wdenk0bc4a1a2002-09-18 11:09:59 +0000234}
235
Simon Glass4415f1d2017-08-03 12:21:58 -0600236U_BOOT_ENV_LOCATION(eeprom) = {
237 .location = ENVL_EEPROM,
Simon Glassac358be2017-08-03 12:22:03 -0600238 ENV_NAME("EEPROM")
Simon Glasse5bce242017-08-03 12:22:01 -0600239 .load = env_eeprom_load,
240 .save = env_save_ptr(env_eeprom_save),
Simon Glass4415f1d2017-08-03 12:21:58 -0600241};