blob: f5882bcfcf49c7f12e816ab35b276fb70d5eadb1 [file] [log] [blame]
wdenk0bc4a1a2002-09-18 11:09:59 +00001/*
Wolfgang Denkea882ba2010-06-20 23:33:59 +02002 * (C) Copyright 2000-2010
wdenk0bc4a1a2002-09-18 11:09:59 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6 * Andreas Heppel <aheppel@sysgo.de>
Wolfgang Denkea882ba2010-06-20 23:33:59 +02007 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
wdenk0bc4a1a2002-09-18 11:09:59 +00009 */
10
11#include <common.h>
wdenk0bc4a1a2002-09-18 11:09:59 +000012#include <command.h>
13#include <environment.h>
wdenk0bc4a1a2002-09-18 11:09:59 +000014#include <linux/stddef.h>
Heiko Schocher548738b2010-01-07 08:55:40 +010015#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
16#include <i2c.h>
17#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +020018#include <search.h>
19#include <errno.h>
20#include <linux/compiler.h> /* for BUG_ON */
wdenk0bc4a1a2002-09-18 11:09:59 +000021
Wolfgang Denkd87080b2006-03-31 18:32:53 +020022DECLARE_GLOBAL_DATA_PTR;
23
Igor Grinbergdd2a2332011-11-07 01:14:07 +000024env_t *env_ptr;
wdenk0bc4a1a2002-09-18 11:09:59 +000025
Wolfgang Denkea882ba2010-06-20 23:33:59 +020026char *env_name_spec = "EEPROM";
Heiko Schocher548738b2010-01-07 08:55:40 +010027int env_eeprom_bus = -1;
28
Wolfgang Denkea882ba2010-06-20 23:33:59 +020029static int eeprom_bus_read(unsigned dev_addr, unsigned offset,
Igor Grinbergdd2a2332011-11-07 01:14:07 +000030 uchar *buffer, unsigned cnt)
Heiko Schocher548738b2010-01-07 08:55:40 +010031{
32 int rcode;
33#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
34 int old_bus = i2c_get_bus_num();
35
36 if (gd->flags & GD_FLG_RELOC) {
37 if (env_eeprom_bus == -1) {
38 I2C_MUX_DEVICE *dev = NULL;
39 dev = i2c_mux_ident_muxstring(
40 (uchar *)CONFIG_I2C_ENV_EEPROM_BUS);
Wolfgang Denkea882ba2010-06-20 23:33:59 +020041 if (dev != NULL)
Heiko Schocher548738b2010-01-07 08:55:40 +010042 env_eeprom_bus = dev->busid;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020043 else
Igor Grinbergdd2a2332011-11-07 01:14:07 +000044 printf("error adding env eeprom bus.\n");
Heiko Schocher548738b2010-01-07 08:55:40 +010045 }
46 if (old_bus != env_eeprom_bus) {
47 i2c_set_bus_num(env_eeprom_bus);
48 old_bus = env_eeprom_bus;
49 }
50 } else {
51 rcode = i2c_mux_ident_muxstring_f(
52 (uchar *)CONFIG_I2C_ENV_EEPROM_BUS);
53 }
54#endif
55
Igor Grinbergdd2a2332011-11-07 01:14:07 +000056 rcode = eeprom_read(dev_addr, offset, buffer, cnt);
Wolfgang Denkea882ba2010-06-20 23:33:59 +020057
Heiko Schocher548738b2010-01-07 08:55:40 +010058#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
59 if (old_bus != env_eeprom_bus)
60 i2c_set_bus_num(old_bus);
61#endif
62 return rcode;
63}
64
Wolfgang Denkea882ba2010-06-20 23:33:59 +020065static int eeprom_bus_write(unsigned dev_addr, unsigned offset,
Igor Grinbergdd2a2332011-11-07 01:14:07 +000066 uchar *buffer, unsigned cnt)
Heiko Schocher548738b2010-01-07 08:55:40 +010067{
68 int rcode;
69#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
70 int old_bus = i2c_get_bus_num();
71
72 rcode = i2c_mux_ident_muxstring_f((uchar *)CONFIG_I2C_ENV_EEPROM_BUS);
73#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +020074 rcode = eeprom_write(dev_addr, offset, buffer, cnt);
Heiko Schocher548738b2010-01-07 08:55:40 +010075#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
76 i2c_set_bus_num(old_bus);
77#endif
78 return rcode;
79}
wdenk0bc4a1a2002-09-18 11:09:59 +000080
Igor Grinbergdd2a2332011-11-07 01:14:07 +000081uchar env_get_char_spec(int index)
wdenk0bc4a1a2002-09-18 11:09:59 +000082{
83 uchar c;
Igor Grinbergdd2a2332011-11-07 01:14:07 +000084 unsigned int off = CONFIG_ENV_OFFSET;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020085
Heiko Schocher1567b592010-01-07 08:55:44 +010086#ifdef CONFIG_ENV_OFFSET_REDUND
87 if (gd->env_valid == 2)
88 off = CONFIG_ENV_OFFSET_REDUND;
89#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +020090 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
Igor Grinbergdd2a2332011-11-07 01:14:07 +000091 off + index + offsetof(env_t, data), &c, 1);
wdenk0bc4a1a2002-09-18 11:09:59 +000092
Igor Grinbergdd2a2332011-11-07 01:14:07 +000093 return c;
wdenk0bc4a1a2002-09-18 11:09:59 +000094}
95
Igor Grinbergdd2a2332011-11-07 01:14:07 +000096void env_relocate_spec(void)
wdenk0bc4a1a2002-09-18 11:09:59 +000097{
Tom Rinicd0f4fa2013-04-05 14:55:21 -040098 char buf[CONFIG_ENV_SIZE];
Heiko Schocher1567b592010-01-07 08:55:44 +010099 unsigned int off = CONFIG_ENV_OFFSET;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200100
Heiko Schocher1567b592010-01-07 08:55:44 +0100101#ifdef CONFIG_ENV_OFFSET_REDUND
102 if (gd->env_valid == 2)
103 off = CONFIG_ENV_OFFSET_REDUND;
104#endif
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200105 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000106 off, (uchar *)buf, CONFIG_ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200107
108 env_import(buf, 1);
wdenk0bc4a1a2002-09-18 11:09:59 +0000109}
110
111int saveenv(void)
112{
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400113 env_t env_new;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200114 ssize_t len;
115 char *res;
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000116 int rc;
117 unsigned int off = CONFIG_ENV_OFFSET;
Heiko Schocher1567b592010-01-07 08:55:44 +0100118#ifdef CONFIG_ENV_OFFSET_REDUND
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000119 unsigned int off_red = CONFIG_ENV_OFFSET_REDUND;
120 char flag_obsolete = OBSOLETE_FLAG;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200121#endif
122
123 BUG_ON(env_ptr != NULL);
124
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400125 res = (char *)&env_new.data;
Joe Hershbergerbe112352012-12-11 22:16:23 -0600126 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200127 if (len < 0) {
128 error("Cannot export environment: errno = %d\n", errno);
129 return 1;
130 }
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400131 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200132
133#ifdef CONFIG_ENV_OFFSET_REDUND
Heiko Schocher1567b592010-01-07 08:55:44 +0100134 if (gd->env_valid == 1) {
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000135 off = CONFIG_ENV_OFFSET_REDUND;
136 off_red = CONFIG_ENV_OFFSET;
Heiko Schocher1567b592010-01-07 08:55:44 +0100137 }
138
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400139 env_new.flags = ACTIVE_FLAG;
Heiko Schocher1567b592010-01-07 08:55:44 +0100140#endif
141
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200142 rc = eeprom_bus_write(CONFIG_SYS_DEF_EEPROM_ADDR,
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400143 off, (uchar *)&env_new, CONFIG_ENV_SIZE);
Heiko Schocher1567b592010-01-07 08:55:44 +0100144
145#ifdef CONFIG_ENV_OFFSET_REDUND
146 if (rc == 0) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200147 eeprom_bus_write(CONFIG_SYS_DEF_EEPROM_ADDR,
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000148 off_red + offsetof(env_t, flags),
149 (uchar *)&flag_obsolete, 1);
150
Heiko Schocher1567b592010-01-07 08:55:44 +0100151 if (gd->env_valid == 1)
152 gd->env_valid = 2;
153 else
154 gd->env_valid = 1;
Heiko Schocher1567b592010-01-07 08:55:44 +0100155 }
156#endif
Heiko Schocher1567b592010-01-07 08:55:44 +0100157 return rc;
wdenk0bc4a1a2002-09-18 11:09:59 +0000158}
159
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200160/*
wdenk0bc4a1a2002-09-18 11:09:59 +0000161 * Initialize Environment use
162 *
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200163 * We are still running from ROM, so data use is limited.
wdenk0bc4a1a2002-09-18 11:09:59 +0000164 * Use a (moderately small) buffer on the stack
165 */
Heiko Schocher1567b592010-01-07 08:55:44 +0100166#ifdef CONFIG_ENV_OFFSET_REDUND
167int env_init(void)
168{
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000169 ulong len, crc[2], crc_tmp;
Heiko Schocher1567b592010-01-07 08:55:44 +0100170 unsigned int off, off_env[2];
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000171 uchar buf[64], flags[2];
172 int i, crc_ok[2] = {0, 0};
Heiko Schocher1567b592010-01-07 08:55:44 +0100173
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200174 eeprom_init(); /* prepare for EEPROM read/write */
Heiko Schocher1567b592010-01-07 08:55:44 +0100175
176 off_env[0] = CONFIG_ENV_OFFSET;
177 off_env[1] = CONFIG_ENV_OFFSET_REDUND;
178
179 for (i = 0; i < 2; i++) {
180 /* read CRC */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200181 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000182 off_env[i] + offsetof(env_t, crc),
183 (uchar *)&crc[i], sizeof(ulong));
Heiko Schocher1567b592010-01-07 08:55:44 +0100184 /* read FLAGS */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200185 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000186 off_env[i] + offsetof(env_t, flags),
187 (uchar *)&flags[i], sizeof(uchar));
Heiko Schocher1567b592010-01-07 08:55:44 +0100188
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200189 crc_tmp = 0;
Heiko Schocher1567b592010-01-07 08:55:44 +0100190 len = ENV_SIZE;
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000191 off = off_env[i] + offsetof(env_t, data);
Heiko Schocher1567b592010-01-07 08:55:44 +0100192 while (len > 0) {
193 int n = (len > sizeof(buf)) ? sizeof(buf) : len;
194
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200195 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR, off,
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000196 buf, n);
Heiko Schocher1567b592010-01-07 08:55:44 +0100197
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200198 crc_tmp = crc32(crc_tmp, buf, n);
Heiko Schocher1567b592010-01-07 08:55:44 +0100199 len -= n;
200 off += n;
201 }
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000202
Heiko Schocher1567b592010-01-07 08:55:44 +0100203 if (crc_tmp == crc[i])
204 crc_ok[i] = 1;
205 }
206
207 if (!crc_ok[0] && !crc_ok[1]) {
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000208 gd->env_addr = 0;
209 gd->env_valid = 0;
Heiko Schocher1567b592010-01-07 08:55:44 +0100210
211 return 0;
212 } else if (crc_ok[0] && !crc_ok[1]) {
213 gd->env_valid = 1;
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000214 } else if (!crc_ok[0] && crc_ok[1]) {
Heiko Schocher1567b592010-01-07 08:55:44 +0100215 gd->env_valid = 2;
216 } else {
217 /* both ok - check serial */
218 if (flags[0] == ACTIVE_FLAG && flags[1] == OBSOLETE_FLAG)
219 gd->env_valid = 1;
220 else if (flags[0] == OBSOLETE_FLAG && flags[1] == ACTIVE_FLAG)
221 gd->env_valid = 2;
222 else if (flags[0] == 0xFF && flags[1] == 0)
223 gd->env_valid = 2;
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000224 else if (flags[1] == 0xFF && flags[0] == 0)
Heiko Schocher1567b592010-01-07 08:55:44 +0100225 gd->env_valid = 1;
226 else /* flags are equal - almost impossible */
227 gd->env_valid = 1;
228 }
229
230 if (gd->env_valid == 2)
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000231 gd->env_addr = off_env[1] + offsetof(env_t, data);
Heiko Schocher1567b592010-01-07 08:55:44 +0100232 else if (gd->env_valid == 1)
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000233 gd->env_addr = off_env[0] + offsetof(env_t, data);
Heiko Schocher1567b592010-01-07 08:55:44 +0100234
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000235 return 0;
Heiko Schocher1567b592010-01-07 08:55:44 +0100236}
237#else
wdenk0bc4a1a2002-09-18 11:09:59 +0000238int env_init(void)
239{
wdenk0bc4a1a2002-09-18 11:09:59 +0000240 ulong crc, len, new;
241 unsigned off;
242 uchar buf[64];
243
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200244 eeprom_init(); /* prepare for EEPROM read/write */
wdenk0bc4a1a2002-09-18 11:09:59 +0000245
246 /* read old CRC */
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200247 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000248 CONFIG_ENV_OFFSET + offsetof(env_t, crc),
249 (uchar *)&crc, sizeof(ulong));
wdenk0bc4a1a2002-09-18 11:09:59 +0000250
251 new = 0;
252 len = ENV_SIZE;
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000253 off = offsetof(env_t, data);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200254
wdenk0bc4a1a2002-09-18 11:09:59 +0000255 while (len > 0) {
256 int n = (len > sizeof(buf)) ? sizeof(buf) : len;
257
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200258 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
Heiko Schocher548738b2010-01-07 08:55:40 +0100259 CONFIG_ENV_OFFSET + off, buf, n);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200260 new = crc32(new, buf, n);
wdenk0bc4a1a2002-09-18 11:09:59 +0000261 len -= n;
262 off += n;
263 }
264
265 if (crc == new) {
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000266 gd->env_addr = offsetof(env_t, data);
267 gd->env_valid = 1;
wdenk0bc4a1a2002-09-18 11:09:59 +0000268 } else {
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000269 gd->env_addr = 0;
270 gd->env_valid = 0;
wdenk0bc4a1a2002-09-18 11:09:59 +0000271 }
272
Igor Grinbergdd2a2332011-11-07 01:14:07 +0000273 return 0;
wdenk0bc4a1a2002-09-18 11:09:59 +0000274}
Heiko Schocher1567b592010-01-07 08:55:44 +0100275#endif