blob: f8556a47213c8c9da8ffd0707a9727faf33fc57d [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
Simon Glassc5951992017-08-03 12:22:17 -060067static int env_eeprom_load(void)
wdenk0bc4a1a2002-09-18 11:09:59 +000068{
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +020069 char buf_env[CONFIG_ENV_SIZE];
Heiko Schocher1567b592010-01-07 08:55:44 +010070 unsigned int off = CONFIG_ENV_OFFSET;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020071
Heiko Schocher1567b592010-01-07 08:55:44 +010072#ifdef CONFIG_ENV_OFFSET_REDUND
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +020073 ulong len, crc[2], crc_tmp;
74 unsigned int off_env[2];
75 uchar rdbuf[64], flags[2];
76 int i, crc_ok[2] = {0, 0};
77
Marek Vasut354e3ed2015-11-10 20:53:31 +010078 eeprom_init(-1); /* prepare for EEPROM read/write */
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +020079
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 Rini88cd7d02021-08-17 17:59:45 -040085 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR,
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +020086 off_env[i] + offsetof(env_t, crc),
87 (uchar *)&crc[i], sizeof(ulong));
88 /* read FLAGS */
Tom Rini88cd7d02021-08-17 17:59:45 -040089 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR,
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +020090 off_env[i] + offsetof(env_t, flags),
91 (uchar *)&flags[i], sizeof(uchar));
92
93 crc_tmp = 0;
94 len = ENV_SIZE;
95 off = off_env[i] + offsetof(env_t, data);
96 while (len > 0) {
97 int n = (len > sizeof(rdbuf)) ? sizeof(rdbuf) : len;
98
Tom Rini88cd7d02021-08-17 17:59:45 -040099 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR, off,
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200100 rdbuf, n);
101
102 crc_tmp = crc32(crc_tmp, rdbuf, n);
103 len -= n;
104 off += n;
105 }
106
107 if (crc_tmp == crc[i])
108 crc_ok[i] = 1;
109 }
110
111 if (!crc_ok[0] && !crc_ok[1]) {
112 gd->env_addr = 0;
Simon Glass2d7cb5b2017-08-20 04:45:15 -0600113 gd->env_valid = ENV_INVALID;
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200114 } else if (crc_ok[0] && !crc_ok[1]) {
Simon Glass203e94f2017-08-03 12:21:56 -0600115 gd->env_valid = ENV_VALID;
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200116 } else if (!crc_ok[0] && crc_ok[1]) {
Simon Glass203e94f2017-08-03 12:21:56 -0600117 gd->env_valid = ENV_REDUND;
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200118 } else {
119 /* both ok - check serial */
Simon Glassd3716dd2019-08-01 09:47:08 -0600120 if (flags[0] == ENV_REDUND_ACTIVE &&
121 flags[1] == ENV_REDUND_OBSOLETE)
Simon Glass203e94f2017-08-03 12:21:56 -0600122 gd->env_valid = ENV_VALID;
Simon Glassd3716dd2019-08-01 09:47:08 -0600123 else if (flags[0] == ENV_REDUND_OBSOLETE &&
124 flags[1] == ENV_REDUND_ACTIVE)
Simon Glass203e94f2017-08-03 12:21:56 -0600125 gd->env_valid = ENV_REDUND;
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200126 else if (flags[0] == 0xFF && flags[1] == 0)
Simon Glass203e94f2017-08-03 12:21:56 -0600127 gd->env_valid = ENV_REDUND;
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200128 else if (flags[1] == 0xFF && flags[0] == 0)
Simon Glass203e94f2017-08-03 12:21:56 -0600129 gd->env_valid = ENV_VALID;
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200130 else /* flags are equal - almost impossible */
Simon Glass203e94f2017-08-03 12:21:56 -0600131 gd->env_valid = ENV_VALID;
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200132 }
133
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200134#else /* CONFIG_ENV_OFFSET_REDUND */
135 ulong crc, len, new;
136 uchar rdbuf[64];
137
Marek Vasut354e3ed2015-11-10 20:53:31 +0100138 eeprom_init(-1); /* prepare for EEPROM read/write */
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200139
140 /* read old CRC */
Tom Rini88cd7d02021-08-17 17:59:45 -0400141 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR,
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200142 CONFIG_ENV_OFFSET + offsetof(env_t, crc),
143 (uchar *)&crc, sizeof(ulong));
144
145 new = 0;
146 len = ENV_SIZE;
147 off = offsetof(env_t, data);
148 while (len > 0) {
149 int n = (len > sizeof(rdbuf)) ? sizeof(rdbuf) : len;
150
Tom Rini88cd7d02021-08-17 17:59:45 -0400151 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR,
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200152 CONFIG_ENV_OFFSET + off, rdbuf, n);
153 new = crc32(new, rdbuf, n);
154 len -= n;
155 off += n;
156 }
157
158 if (crc == new) {
Simon Glass2d7cb5b2017-08-20 04:45:15 -0600159 gd->env_valid = ENV_VALID;
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200160 } else {
Simon Glass2d7cb5b2017-08-20 04:45:15 -0600161 gd->env_valid = ENV_INVALID;
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200162 }
163#endif /* CONFIG_ENV_OFFSET_REDUND */
164
165 off = CONFIG_ENV_OFFSET;
166#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass203e94f2017-08-03 12:21:56 -0600167 if (gd->env_valid == ENV_REDUND)
Heiko Schocher1567b592010-01-07 08:55:44 +0100168 off = CONFIG_ENV_OFFSET_REDUND;
169#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200170
Tom Rini88cd7d02021-08-17 17:59:45 -0400171 eeprom_bus_read(CONFIG_SYS_I2C_EEPROM_ADDR,
Ludger Dreiere3cc5bc2015-10-12 13:34:24 +0200172 off, (uchar *)buf_env, CONFIG_ENV_SIZE);
173
Marek Vasut890feec2020-07-07 20:51:35 +0200174 return env_import(buf_env, 1, H_EXTERNAL);
wdenk0bc4a1a2002-09-18 11:09:59 +0000175}
176
Simon Glasse5bce242017-08-03 12:22:01 -0600177static int env_eeprom_save(void)
wdenk0bc4a1a2002-09-18 11:09:59 +0000178{
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400179 env_t env_new;
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000180 int rc;
181 unsigned int off = CONFIG_ENV_OFFSET;
Heiko Schocher1567b592010-01-07 08:55:44 +0100182#ifdef CONFIG_ENV_OFFSET_REDUND
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000183 unsigned int off_red = CONFIG_ENV_OFFSET_REDUND;
Simon Glassd3716dd2019-08-01 09:47:08 -0600184 char flag_obsolete = ENV_REDUND_OBSOLETE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200185#endif
186
Marek Vasut7ce15262014-03-05 19:59:50 +0100187 rc = env_export(&env_new);
188 if (rc)
189 return rc;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200190
191#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass203e94f2017-08-03 12:21:56 -0600192 if (gd->env_valid == ENV_VALID) {
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000193 off = CONFIG_ENV_OFFSET_REDUND;
194 off_red = CONFIG_ENV_OFFSET;
Heiko Schocher1567b592010-01-07 08:55:44 +0100195 }
196
Simon Glassd3716dd2019-08-01 09:47:08 -0600197 env_new.flags = ENV_REDUND_ACTIVE;
Heiko Schocher1567b592010-01-07 08:55:44 +0100198#endif
199
Tom Rini88cd7d02021-08-17 17:59:45 -0400200 rc = eeprom_bus_write(CONFIG_SYS_I2C_EEPROM_ADDR,
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400201 off, (uchar *)&env_new, CONFIG_ENV_SIZE);
Heiko Schocher1567b592010-01-07 08:55:44 +0100202
203#ifdef CONFIG_ENV_OFFSET_REDUND
204 if (rc == 0) {
Tom Rini88cd7d02021-08-17 17:59:45 -0400205 eeprom_bus_write(CONFIG_SYS_I2C_EEPROM_ADDR,
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000206 off_red + offsetof(env_t, flags),
207 (uchar *)&flag_obsolete, 1);
208
Simon Glass203e94f2017-08-03 12:21:56 -0600209 if (gd->env_valid == ENV_VALID)
210 gd->env_valid = ENV_REDUND;
Heiko Schocher1567b592010-01-07 08:55:44 +0100211 else
Simon Glass203e94f2017-08-03 12:21:56 -0600212 gd->env_valid = ENV_VALID;
Heiko Schocher1567b592010-01-07 08:55:44 +0100213 }
214#endif
Heiko Schocher1567b592010-01-07 08:55:44 +0100215 return rc;
wdenk0bc4a1a2002-09-18 11:09:59 +0000216}
217
Simon Glass4415f1d2017-08-03 12:21:58 -0600218U_BOOT_ENV_LOCATION(eeprom) = {
219 .location = ENVL_EEPROM,
Simon Glassac358be2017-08-03 12:22:03 -0600220 ENV_NAME("EEPROM")
Simon Glasse5bce242017-08-03 12:22:01 -0600221 .load = env_eeprom_load,
222 .save = env_save_ptr(env_eeprom_save),
Simon Glass4415f1d2017-08-03 12:21:58 -0600223};