blob: 253bdf14284a36ef918fd635c275f29d63a2eed9 [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>
Simon Glass3ba929a2020-10-30 21:38:53 -060015#include <asm/global_data.h>
wdenk0bc4a1a2002-09-18 11:09:59 +000016#include <linux/stddef.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070017#include <u-boot/crc.h>
Heiko Schocher9bb0dd52010-01-07 08:55:40 +010018#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
19#include <i2c.h>
20#endif
Wolfgang Denk460a9ff2010-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 Denk6405a152006-03-31 18:32:53 +020025DECLARE_GLOBAL_DATA_PTR;
26
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020027static int eeprom_bus_read(unsigned dev_addr, unsigned offset,
Igor Grinberg13f851f2011-11-07 01:14:07 +000028 uchar *buffer, unsigned cnt)
Heiko Schocher9bb0dd52010-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 Schochere0e55bc2012-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 Schocher9bb0dd52010-01-07 08:55:40 +010036#endif
37
Igor Grinberg13f851f2011-11-07 01:14:07 +000038 rcode = eeprom_read(dev_addr, offset, buffer, cnt);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020039
Heiko Schocher9bb0dd52010-01-07 08:55:40 +010040#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
Alexey Brodkinf234b1e2013-12-11 18:04:40 +040041 i2c_set_bus_num(old_bus);
Heiko Schocher9bb0dd52010-01-07 08:55:40 +010042#endif
Heiko Schochera4ea4452012-10-25 10:32:14 +020043
Heiko Schocher9bb0dd52010-01-07 08:55:40 +010044 return rcode;
45}
46
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020047static int eeprom_bus_write(unsigned dev_addr, unsigned offset,
Igor Grinberg13f851f2011-11-07 01:14:07 +000048 uchar *buffer, unsigned cnt)
Heiko Schocher9bb0dd52010-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 Schochere0e55bc2012-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 Schocher9bb0dd52010-01-07 08:55:40 +010056#endif
Heiko Schochera4ea4452012-10-25 10:32:14 +020057
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020058 rcode = eeprom_write(dev_addr, offset, buffer, cnt);
Heiko Schochera4ea4452012-10-25 10:32:14 +020059
Heiko Schocher9bb0dd52010-01-07 08:55:40 +010060#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
61 i2c_set_bus_num(old_bus);
62#endif
Alexey Brodkinf234b1e2013-12-11 18:04:40 +040063
Heiko Schocher9bb0dd52010-01-07 08:55:40 +010064 return rcode;
65}
wdenk0bc4a1a2002-09-18 11:09:59 +000066
Goldschmidt Simon9b0d66b2018-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 Grinberg13f851f2011-11-07 01:14:07 +000073 unsigned int off = CONFIG_ENV_OFFSET;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020074
Heiko Schochere7193632010-01-07 08:55:44 +010075#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass4bc2ad22017-08-03 12:21:56 -060076 if (gd->env_valid == ENV_REDUND)
Heiko Schochere7193632010-01-07 08:55:44 +010077 off = CONFIG_ENV_OFFSET_REDUND;
78#endif
Tom Rinifaed5672021-08-17 17:59:45 -040079 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR,
Igor Grinberg13f851f2011-11-07 01:14:07 +000080 off + index + offsetof(env_t, data), &c, 1);
wdenk0bc4a1a2002-09-18 11:09:59 +000081
Igor Grinberg13f851f2011-11-07 01:14:07 +000082 return c;
wdenk0bc4a1a2002-09-18 11:09:59 +000083}
84
Simon Glass99778492017-08-03 12:22:17 -060085static int env_eeprom_load(void)
wdenk0bc4a1a2002-09-18 11:09:59 +000086{
Ludger Dreierbf84e642015-10-12 13:34:24 +020087 char buf_env[CONFIG_ENV_SIZE];
Heiko Schochere7193632010-01-07 08:55:44 +010088 unsigned int off = CONFIG_ENV_OFFSET;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020089
Heiko Schochere7193632010-01-07 08:55:44 +010090#ifdef CONFIG_ENV_OFFSET_REDUND
Igor Grinberg13f851f2011-11-07 01:14:07 +000091 ulong len, crc[2], crc_tmp;
Ludger Dreierbf84e642015-10-12 13:34:24 +020092 unsigned int off_env[2];
93 uchar rdbuf[64], flags[2];
Igor Grinberg13f851f2011-11-07 01:14:07 +000094 int i, crc_ok[2] = {0, 0};
Heiko Schochere7193632010-01-07 08:55:44 +010095
Marek Vasutf422ae72015-11-10 20:53:31 +010096 eeprom_init(-1); /* prepare for EEPROM read/write */
Heiko Schochere7193632010-01-07 08:55:44 +010097
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 Rinifaed5672021-08-17 17:59:45 -0400103 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR,
Igor Grinberg13f851f2011-11-07 01:14:07 +0000104 off_env[i] + offsetof(env_t, crc),
105 (uchar *)&crc[i], sizeof(ulong));
Heiko Schochere7193632010-01-07 08:55:44 +0100106 /* read FLAGS */
Tom Rinifaed5672021-08-17 17:59:45 -0400107 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR,
Igor Grinberg13f851f2011-11-07 01:14:07 +0000108 off_env[i] + offsetof(env_t, flags),
109 (uchar *)&flags[i], sizeof(uchar));
Heiko Schochere7193632010-01-07 08:55:44 +0100110
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200111 crc_tmp = 0;
Heiko Schochere7193632010-01-07 08:55:44 +0100112 len = ENV_SIZE;
Igor Grinberg13f851f2011-11-07 01:14:07 +0000113 off = off_env[i] + offsetof(env_t, data);
Heiko Schochere7193632010-01-07 08:55:44 +0100114 while (len > 0) {
Ludger Dreierbf84e642015-10-12 13:34:24 +0200115 int n = (len > sizeof(rdbuf)) ? sizeof(rdbuf) : len;
Heiko Schochere7193632010-01-07 08:55:44 +0100116
Tom Rinifaed5672021-08-17 17:59:45 -0400117 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR, off,
Ludger Dreierbf84e642015-10-12 13:34:24 +0200118 rdbuf, n);
Heiko Schochere7193632010-01-07 08:55:44 +0100119
Ludger Dreierbf84e642015-10-12 13:34:24 +0200120 crc_tmp = crc32(crc_tmp, rdbuf, n);
Heiko Schochere7193632010-01-07 08:55:44 +0100121 len -= n;
122 off += n;
123 }
Igor Grinberg13f851f2011-11-07 01:14:07 +0000124
Heiko Schochere7193632010-01-07 08:55:44 +0100125 if (crc_tmp == crc[i])
126 crc_ok[i] = 1;
127 }
128
129 if (!crc_ok[0] && !crc_ok[1]) {
Igor Grinberg13f851f2011-11-07 01:14:07 +0000130 gd->env_addr = 0;
Simon Glass1f39d0b2017-08-20 04:45:15 -0600131 gd->env_valid = ENV_INVALID;
Heiko Schochere7193632010-01-07 08:55:44 +0100132 } else if (crc_ok[0] && !crc_ok[1]) {
Simon Glass4bc2ad22017-08-03 12:21:56 -0600133 gd->env_valid = ENV_VALID;
Igor Grinberg13f851f2011-11-07 01:14:07 +0000134 } else if (!crc_ok[0] && crc_ok[1]) {
Simon Glass4bc2ad22017-08-03 12:21:56 -0600135 gd->env_valid = ENV_REDUND;
Heiko Schochere7193632010-01-07 08:55:44 +0100136 } else {
137 /* both ok - check serial */
Simon Glass4686d8f2019-08-01 09:47:08 -0600138 if (flags[0] == ENV_REDUND_ACTIVE &&
139 flags[1] == ENV_REDUND_OBSOLETE)
Simon Glass4bc2ad22017-08-03 12:21:56 -0600140 gd->env_valid = ENV_VALID;
Simon Glass4686d8f2019-08-01 09:47:08 -0600141 else if (flags[0] == ENV_REDUND_OBSOLETE &&
142 flags[1] == ENV_REDUND_ACTIVE)
Simon Glass4bc2ad22017-08-03 12:21:56 -0600143 gd->env_valid = ENV_REDUND;
Heiko Schochere7193632010-01-07 08:55:44 +0100144 else if (flags[0] == 0xFF && flags[1] == 0)
Simon Glass4bc2ad22017-08-03 12:21:56 -0600145 gd->env_valid = ENV_REDUND;
Igor Grinberg13f851f2011-11-07 01:14:07 +0000146 else if (flags[1] == 0xFF && flags[0] == 0)
Simon Glass4bc2ad22017-08-03 12:21:56 -0600147 gd->env_valid = ENV_VALID;
Heiko Schochere7193632010-01-07 08:55:44 +0100148 else /* flags are equal - almost impossible */
Simon Glass4bc2ad22017-08-03 12:21:56 -0600149 gd->env_valid = ENV_VALID;
Heiko Schochere7193632010-01-07 08:55:44 +0100150 }
151
Ludger Dreierbf84e642015-10-12 13:34:24 +0200152#else /* CONFIG_ENV_OFFSET_REDUND */
wdenk0bc4a1a2002-09-18 11:09:59 +0000153 ulong crc, len, new;
Ludger Dreierbf84e642015-10-12 13:34:24 +0200154 uchar rdbuf[64];
wdenk0bc4a1a2002-09-18 11:09:59 +0000155
Marek Vasutf422ae72015-11-10 20:53:31 +0100156 eeprom_init(-1); /* prepare for EEPROM read/write */
wdenk0bc4a1a2002-09-18 11:09:59 +0000157
158 /* read old CRC */
Tom Rinifaed5672021-08-17 17:59:45 -0400159 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR,
Igor Grinberg13f851f2011-11-07 01:14:07 +0000160 CONFIG_ENV_OFFSET + offsetof(env_t, crc),
161 (uchar *)&crc, sizeof(ulong));
wdenk0bc4a1a2002-09-18 11:09:59 +0000162
163 new = 0;
164 len = ENV_SIZE;
Igor Grinberg13f851f2011-11-07 01:14:07 +0000165 off = offsetof(env_t, data);
wdenk0bc4a1a2002-09-18 11:09:59 +0000166 while (len > 0) {
Ludger Dreierbf84e642015-10-12 13:34:24 +0200167 int n = (len > sizeof(rdbuf)) ? sizeof(rdbuf) : len;
wdenk0bc4a1a2002-09-18 11:09:59 +0000168
Tom Rinifaed5672021-08-17 17:59:45 -0400169 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR,
Ludger Dreierbf84e642015-10-12 13:34:24 +0200170 CONFIG_ENV_OFFSET + off, rdbuf, n);
171 new = crc32(new, rdbuf, n);
wdenk0bc4a1a2002-09-18 11:09:59 +0000172 len -= n;
173 off += n;
174 }
175
176 if (crc == new) {
Simon Glass1f39d0b2017-08-20 04:45:15 -0600177 gd->env_valid = ENV_VALID;
wdenk0bc4a1a2002-09-18 11:09:59 +0000178 } else {
Simon Glass1f39d0b2017-08-20 04:45:15 -0600179 gd->env_valid = ENV_INVALID;
wdenk0bc4a1a2002-09-18 11:09:59 +0000180 }
Ludger Dreierbf84e642015-10-12 13:34:24 +0200181#endif /* CONFIG_ENV_OFFSET_REDUND */
182
183 off = CONFIG_ENV_OFFSET;
184#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass4bc2ad22017-08-03 12:21:56 -0600185 if (gd->env_valid == ENV_REDUND)
Ludger Dreierbf84e642015-10-12 13:34:24 +0200186 off = CONFIG_ENV_OFFSET_REDUND;
187#endif
188
Tom Rinifaed5672021-08-17 17:59:45 -0400189 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR,
Ludger Dreierbf84e642015-10-12 13:34:24 +0200190 off, (uchar *)buf_env, CONFIG_ENV_SIZE);
191
Marek Vasutdfe4b7e2020-07-07 20:51:35 +0200192 return env_import(buf_env, 1, H_EXTERNAL);
Ludger Dreierbf84e642015-10-12 13:34:24 +0200193}
194
Simon Glass082af922017-08-03 12:22:01 -0600195static int env_eeprom_save(void)
Ludger Dreierbf84e642015-10-12 13:34:24 +0200196{
197 env_t env_new;
198 int rc;
199 unsigned int off = CONFIG_ENV_OFFSET;
200#ifdef CONFIG_ENV_OFFSET_REDUND
201 unsigned int off_red = CONFIG_ENV_OFFSET_REDUND;
Simon Glass4686d8f2019-08-01 09:47:08 -0600202 char flag_obsolete = ENV_REDUND_OBSOLETE;
Ludger Dreierbf84e642015-10-12 13:34:24 +0200203#endif
204
Ludger Dreierbf84e642015-10-12 13:34:24 +0200205 rc = env_export(&env_new);
206 if (rc)
207 return rc;
208
209#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass4bc2ad22017-08-03 12:21:56 -0600210 if (gd->env_valid == ENV_VALID) {
Ludger Dreierbf84e642015-10-12 13:34:24 +0200211 off = CONFIG_ENV_OFFSET_REDUND;
212 off_red = CONFIG_ENV_OFFSET;
213 }
214
Simon Glass4686d8f2019-08-01 09:47:08 -0600215 env_new.flags = ENV_REDUND_ACTIVE;
Ludger Dreierbf84e642015-10-12 13:34:24 +0200216#endif
217
Tom Rinifaed5672021-08-17 17:59:45 -0400218 rc = eeprom_bus_write(CONFIG_SYS_I2C_EEPROM_ADDR,
Ludger Dreierbf84e642015-10-12 13:34:24 +0200219 off, (uchar *)&env_new, CONFIG_ENV_SIZE);
220
221#ifdef CONFIG_ENV_OFFSET_REDUND
222 if (rc == 0) {
Tom Rinifaed5672021-08-17 17:59:45 -0400223 eeprom_bus_write(CONFIG_SYS_I2C_EEPROM_ADDR,
Ludger Dreierbf84e642015-10-12 13:34:24 +0200224 off_red + offsetof(env_t, flags),
225 (uchar *)&flag_obsolete, 1);
226
Simon Glass4bc2ad22017-08-03 12:21:56 -0600227 if (gd->env_valid == ENV_VALID)
228 gd->env_valid = ENV_REDUND;
Ludger Dreierbf84e642015-10-12 13:34:24 +0200229 else
Simon Glass4bc2ad22017-08-03 12:21:56 -0600230 gd->env_valid = ENV_VALID;
Ludger Dreierbf84e642015-10-12 13:34:24 +0200231 }
232#endif
233 return rc;
234}
235
Simon Glassc10a88e2017-08-03 12:21:58 -0600236U_BOOT_ENV_LOCATION(eeprom) = {
237 .location = ENVL_EEPROM,
Simon Glassd8273ed2017-08-03 12:22:03 -0600238 ENV_NAME("EEPROM")
Simon Glass082af922017-08-03 12:22:01 -0600239 .load = env_eeprom_load,
240 .save = env_save_ptr(env_eeprom_save),
Simon Glassc10a88e2017-08-03 12:21:58 -0600241};