blob: f8556a47213c8c9da8ffd0707a9727faf33fc57d [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
Simon Glass99778492017-08-03 12:22:17 -060067static int env_eeprom_load(void)
wdenk0bc4a1a2002-09-18 11:09:59 +000068{
Ludger Dreierbf84e642015-10-12 13:34:24 +020069 char buf_env[CONFIG_ENV_SIZE];
Heiko Schochere7193632010-01-07 08:55:44 +010070 unsigned int off = CONFIG_ENV_OFFSET;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020071
Heiko Schochere7193632010-01-07 08:55:44 +010072#ifdef CONFIG_ENV_OFFSET_REDUND
Igor Grinberg13f851f2011-11-07 01:14:07 +000073 ulong len, crc[2], crc_tmp;
Ludger Dreierbf84e642015-10-12 13:34:24 +020074 unsigned int off_env[2];
75 uchar rdbuf[64], flags[2];
Igor Grinberg13f851f2011-11-07 01:14:07 +000076 int i, crc_ok[2] = {0, 0};
Heiko Schochere7193632010-01-07 08:55:44 +010077
Marek Vasutf422ae72015-11-10 20:53:31 +010078 eeprom_init(-1); /* prepare for EEPROM read/write */
Heiko Schochere7193632010-01-07 08:55:44 +010079
80 off_env[0] = CONFIG_ENV_OFFSET;
81 off_env[1] = CONFIG_ENV_OFFSET_REDUND;
82
83 for (i = 0; i < 2; i++) {
84 /* read CRC */
Tom Rinifaed5672021-08-17 17:59:45 -040085 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR,
Igor Grinberg13f851f2011-11-07 01:14:07 +000086 off_env[i] + offsetof(env_t, crc),
87 (uchar *)&crc[i], sizeof(ulong));
Heiko Schochere7193632010-01-07 08:55:44 +010088 /* read FLAGS */
Tom Rinifaed5672021-08-17 17:59:45 -040089 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR,
Igor Grinberg13f851f2011-11-07 01:14:07 +000090 off_env[i] + offsetof(env_t, flags),
91 (uchar *)&flags[i], sizeof(uchar));
Heiko Schochere7193632010-01-07 08:55:44 +010092
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020093 crc_tmp = 0;
Heiko Schochere7193632010-01-07 08:55:44 +010094 len = ENV_SIZE;
Igor Grinberg13f851f2011-11-07 01:14:07 +000095 off = off_env[i] + offsetof(env_t, data);
Heiko Schochere7193632010-01-07 08:55:44 +010096 while (len > 0) {
Ludger Dreierbf84e642015-10-12 13:34:24 +020097 int n = (len > sizeof(rdbuf)) ? sizeof(rdbuf) : len;
Heiko Schochere7193632010-01-07 08:55:44 +010098
Tom Rinifaed5672021-08-17 17:59:45 -040099 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR, off,
Ludger Dreierbf84e642015-10-12 13:34:24 +0200100 rdbuf, n);
Heiko Schochere7193632010-01-07 08:55:44 +0100101
Ludger Dreierbf84e642015-10-12 13:34:24 +0200102 crc_tmp = crc32(crc_tmp, rdbuf, n);
Heiko Schochere7193632010-01-07 08:55:44 +0100103 len -= n;
104 off += n;
105 }
Igor Grinberg13f851f2011-11-07 01:14:07 +0000106
Heiko Schochere7193632010-01-07 08:55:44 +0100107 if (crc_tmp == crc[i])
108 crc_ok[i] = 1;
109 }
110
111 if (!crc_ok[0] && !crc_ok[1]) {
Igor Grinberg13f851f2011-11-07 01:14:07 +0000112 gd->env_addr = 0;
Simon Glass1f39d0b2017-08-20 04:45:15 -0600113 gd->env_valid = ENV_INVALID;
Heiko Schochere7193632010-01-07 08:55:44 +0100114 } else if (crc_ok[0] && !crc_ok[1]) {
Simon Glass4bc2ad22017-08-03 12:21:56 -0600115 gd->env_valid = ENV_VALID;
Igor Grinberg13f851f2011-11-07 01:14:07 +0000116 } else if (!crc_ok[0] && crc_ok[1]) {
Simon Glass4bc2ad22017-08-03 12:21:56 -0600117 gd->env_valid = ENV_REDUND;
Heiko Schochere7193632010-01-07 08:55:44 +0100118 } else {
119 /* both ok - check serial */
Simon Glass4686d8f2019-08-01 09:47:08 -0600120 if (flags[0] == ENV_REDUND_ACTIVE &&
121 flags[1] == ENV_REDUND_OBSOLETE)
Simon Glass4bc2ad22017-08-03 12:21:56 -0600122 gd->env_valid = ENV_VALID;
Simon Glass4686d8f2019-08-01 09:47:08 -0600123 else if (flags[0] == ENV_REDUND_OBSOLETE &&
124 flags[1] == ENV_REDUND_ACTIVE)
Simon Glass4bc2ad22017-08-03 12:21:56 -0600125 gd->env_valid = ENV_REDUND;
Heiko Schochere7193632010-01-07 08:55:44 +0100126 else if (flags[0] == 0xFF && flags[1] == 0)
Simon Glass4bc2ad22017-08-03 12:21:56 -0600127 gd->env_valid = ENV_REDUND;
Igor Grinberg13f851f2011-11-07 01:14:07 +0000128 else if (flags[1] == 0xFF && flags[0] == 0)
Simon Glass4bc2ad22017-08-03 12:21:56 -0600129 gd->env_valid = ENV_VALID;
Heiko Schochere7193632010-01-07 08:55:44 +0100130 else /* flags are equal - almost impossible */
Simon Glass4bc2ad22017-08-03 12:21:56 -0600131 gd->env_valid = ENV_VALID;
Heiko Schochere7193632010-01-07 08:55:44 +0100132 }
133
Ludger Dreierbf84e642015-10-12 13:34:24 +0200134#else /* CONFIG_ENV_OFFSET_REDUND */
wdenk0bc4a1a2002-09-18 11:09:59 +0000135 ulong crc, len, new;
Ludger Dreierbf84e642015-10-12 13:34:24 +0200136 uchar rdbuf[64];
wdenk0bc4a1a2002-09-18 11:09:59 +0000137
Marek Vasutf422ae72015-11-10 20:53:31 +0100138 eeprom_init(-1); /* prepare for EEPROM read/write */
wdenk0bc4a1a2002-09-18 11:09:59 +0000139
140 /* read old CRC */
Tom Rinifaed5672021-08-17 17:59:45 -0400141 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR,
Igor Grinberg13f851f2011-11-07 01:14:07 +0000142 CONFIG_ENV_OFFSET + offsetof(env_t, crc),
143 (uchar *)&crc, sizeof(ulong));
wdenk0bc4a1a2002-09-18 11:09:59 +0000144
145 new = 0;
146 len = ENV_SIZE;
Igor Grinberg13f851f2011-11-07 01:14:07 +0000147 off = offsetof(env_t, data);
wdenk0bc4a1a2002-09-18 11:09:59 +0000148 while (len > 0) {
Ludger Dreierbf84e642015-10-12 13:34:24 +0200149 int n = (len > sizeof(rdbuf)) ? sizeof(rdbuf) : len;
wdenk0bc4a1a2002-09-18 11:09:59 +0000150
Tom Rinifaed5672021-08-17 17:59:45 -0400151 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR,
Ludger Dreierbf84e642015-10-12 13:34:24 +0200152 CONFIG_ENV_OFFSET + off, rdbuf, n);
153 new = crc32(new, rdbuf, n);
wdenk0bc4a1a2002-09-18 11:09:59 +0000154 len -= n;
155 off += n;
156 }
157
158 if (crc == new) {
Simon Glass1f39d0b2017-08-20 04:45:15 -0600159 gd->env_valid = ENV_VALID;
wdenk0bc4a1a2002-09-18 11:09:59 +0000160 } else {
Simon Glass1f39d0b2017-08-20 04:45:15 -0600161 gd->env_valid = ENV_INVALID;
wdenk0bc4a1a2002-09-18 11:09:59 +0000162 }
Ludger Dreierbf84e642015-10-12 13:34:24 +0200163#endif /* CONFIG_ENV_OFFSET_REDUND */
164
165 off = CONFIG_ENV_OFFSET;
166#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass4bc2ad22017-08-03 12:21:56 -0600167 if (gd->env_valid == ENV_REDUND)
Ludger Dreierbf84e642015-10-12 13:34:24 +0200168 off = CONFIG_ENV_OFFSET_REDUND;
169#endif
170
Tom Rinifaed5672021-08-17 17:59:45 -0400171 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR,
Ludger Dreierbf84e642015-10-12 13:34:24 +0200172 off, (uchar *)buf_env, CONFIG_ENV_SIZE);
173
Marek Vasutdfe4b7e2020-07-07 20:51:35 +0200174 return env_import(buf_env, 1, H_EXTERNAL);
Ludger Dreierbf84e642015-10-12 13:34:24 +0200175}
176
Simon Glass082af922017-08-03 12:22:01 -0600177static int env_eeprom_save(void)
Ludger Dreierbf84e642015-10-12 13:34:24 +0200178{
179 env_t env_new;
180 int rc;
181 unsigned int off = CONFIG_ENV_OFFSET;
182#ifdef CONFIG_ENV_OFFSET_REDUND
183 unsigned int off_red = CONFIG_ENV_OFFSET_REDUND;
Simon Glass4686d8f2019-08-01 09:47:08 -0600184 char flag_obsolete = ENV_REDUND_OBSOLETE;
Ludger Dreierbf84e642015-10-12 13:34:24 +0200185#endif
186
Ludger Dreierbf84e642015-10-12 13:34:24 +0200187 rc = env_export(&env_new);
188 if (rc)
189 return rc;
190
191#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass4bc2ad22017-08-03 12:21:56 -0600192 if (gd->env_valid == ENV_VALID) {
Ludger Dreierbf84e642015-10-12 13:34:24 +0200193 off = CONFIG_ENV_OFFSET_REDUND;
194 off_red = CONFIG_ENV_OFFSET;
195 }
196
Simon Glass4686d8f2019-08-01 09:47:08 -0600197 env_new.flags = ENV_REDUND_ACTIVE;
Ludger Dreierbf84e642015-10-12 13:34:24 +0200198#endif
199
Tom Rinifaed5672021-08-17 17:59:45 -0400200 rc = eeprom_bus_write(CONFIG_SYS_I2C_EEPROM_ADDR,
Ludger Dreierbf84e642015-10-12 13:34:24 +0200201 off, (uchar *)&env_new, CONFIG_ENV_SIZE);
202
203#ifdef CONFIG_ENV_OFFSET_REDUND
204 if (rc == 0) {
Tom Rinifaed5672021-08-17 17:59:45 -0400205 eeprom_bus_write(CONFIG_SYS_I2C_EEPROM_ADDR,
Ludger Dreierbf84e642015-10-12 13:34:24 +0200206 off_red + offsetof(env_t, flags),
207 (uchar *)&flag_obsolete, 1);
208
Simon Glass4bc2ad22017-08-03 12:21:56 -0600209 if (gd->env_valid == ENV_VALID)
210 gd->env_valid = ENV_REDUND;
Ludger Dreierbf84e642015-10-12 13:34:24 +0200211 else
Simon Glass4bc2ad22017-08-03 12:21:56 -0600212 gd->env_valid = ENV_VALID;
Ludger Dreierbf84e642015-10-12 13:34:24 +0200213 }
214#endif
215 return rc;
216}
217
Simon Glassc10a88e2017-08-03 12:21:58 -0600218U_BOOT_ENV_LOCATION(eeprom) = {
219 .location = ENVL_EEPROM,
Simon Glassd8273ed2017-08-03 12:22:03 -0600220 ENV_NAME("EEPROM")
Simon Glass082af922017-08-03 12:22:01 -0600221 .load = env_eeprom_load,
222 .save = env_save_ptr(env_eeprom_save),
Simon Glassc10a88e2017-08-03 12:21:58 -0600223};