2 * (C) Copyright 2000-2010
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6 * Andreas Heppel <aheppel@sysgo.de>
8 * See file CREDITS for list of people who contributed to this
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 #include <environment.h>
30 #include <linux/stddef.h>
31 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
36 #include <linux/compiler.h> /* for BUG_ON */
38 DECLARE_GLOBAL_DATA_PTR
;
42 char *env_name_spec
= "EEPROM";
43 int env_eeprom_bus
= -1;
45 static int eeprom_bus_read(unsigned dev_addr
, unsigned offset
,
46 uchar
*buffer
, unsigned cnt
)
49 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
50 int old_bus
= i2c_get_bus_num();
52 if (gd
->flags
& GD_FLG_RELOC
) {
53 if (env_eeprom_bus
== -1) {
54 I2C_MUX_DEVICE
*dev
= NULL
;
55 dev
= i2c_mux_ident_muxstring(
56 (uchar
*)CONFIG_I2C_ENV_EEPROM_BUS
);
58 env_eeprom_bus
= dev
->busid
;
60 printf("error adding env eeprom bus.\n");
62 if (old_bus
!= env_eeprom_bus
) {
63 i2c_set_bus_num(env_eeprom_bus
);
64 old_bus
= env_eeprom_bus
;
67 rcode
= i2c_mux_ident_muxstring_f(
68 (uchar
*)CONFIG_I2C_ENV_EEPROM_BUS
);
72 rcode
= eeprom_read(dev_addr
, offset
, buffer
, cnt
);
74 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
75 if (old_bus
!= env_eeprom_bus
)
76 i2c_set_bus_num(old_bus
);
81 static int eeprom_bus_write(unsigned dev_addr
, unsigned offset
,
82 uchar
*buffer
, unsigned cnt
)
85 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
86 int old_bus
= i2c_get_bus_num();
88 rcode
= i2c_mux_ident_muxstring_f((uchar
*)CONFIG_I2C_ENV_EEPROM_BUS
);
90 rcode
= eeprom_write(dev_addr
, offset
, buffer
, cnt
);
91 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
92 i2c_set_bus_num(old_bus
);
97 uchar
env_get_char_spec(int index
)
100 unsigned int off
= CONFIG_ENV_OFFSET
;
102 #ifdef CONFIG_ENV_OFFSET_REDUND
103 if (gd
->env_valid
== 2)
104 off
= CONFIG_ENV_OFFSET_REDUND
;
106 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR
,
107 off
+ index
+ offsetof(env_t
, data
), &c
, 1);
112 void env_relocate_spec(void)
114 char buf
[CONFIG_ENV_SIZE
];
115 unsigned int off
= CONFIG_ENV_OFFSET
;
117 #ifdef CONFIG_ENV_OFFSET_REDUND
118 if (gd
->env_valid
== 2)
119 off
= CONFIG_ENV_OFFSET_REDUND
;
121 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR
,
122 off
, (uchar
*)buf
, CONFIG_ENV_SIZE
);
133 unsigned int off
= CONFIG_ENV_OFFSET
;
134 #ifdef CONFIG_ENV_OFFSET_REDUND
135 unsigned int off_red
= CONFIG_ENV_OFFSET_REDUND
;
136 char flag_obsolete
= OBSOLETE_FLAG
;
139 BUG_ON(env_ptr
!= NULL
);
141 res
= (char *)&env_new
.data
;
142 len
= hexport_r(&env_htab
, '\0', 0, &res
, ENV_SIZE
, 0, NULL
);
144 error("Cannot export environment: errno = %d\n", errno
);
147 env_new
.crc
= crc32(0, env_new
.data
, ENV_SIZE
);
149 #ifdef CONFIG_ENV_OFFSET_REDUND
150 if (gd
->env_valid
== 1) {
151 off
= CONFIG_ENV_OFFSET_REDUND
;
152 off_red
= CONFIG_ENV_OFFSET
;
155 env_new
.flags
= ACTIVE_FLAG
;
158 rc
= eeprom_bus_write(CONFIG_SYS_DEF_EEPROM_ADDR
,
159 off
, (uchar
*)&env_new
, CONFIG_ENV_SIZE
);
161 #ifdef CONFIG_ENV_OFFSET_REDUND
163 eeprom_bus_write(CONFIG_SYS_DEF_EEPROM_ADDR
,
164 off_red
+ offsetof(env_t
, flags
),
165 (uchar
*)&flag_obsolete
, 1);
167 if (gd
->env_valid
== 1)
177 * Initialize Environment use
179 * We are still running from ROM, so data use is limited.
180 * Use a (moderately small) buffer on the stack
182 #ifdef CONFIG_ENV_OFFSET_REDUND
185 ulong len
, crc
[2], crc_tmp
;
186 unsigned int off
, off_env
[2];
187 uchar buf
[64], flags
[2];
188 int i
, crc_ok
[2] = {0, 0};
190 eeprom_init(); /* prepare for EEPROM read/write */
192 off_env
[0] = CONFIG_ENV_OFFSET
;
193 off_env
[1] = CONFIG_ENV_OFFSET_REDUND
;
195 for (i
= 0; i
< 2; i
++) {
197 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR
,
198 off_env
[i
] + offsetof(env_t
, crc
),
199 (uchar
*)&crc
[i
], sizeof(ulong
));
201 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR
,
202 off_env
[i
] + offsetof(env_t
, flags
),
203 (uchar
*)&flags
[i
], sizeof(uchar
));
207 off
= off_env
[i
] + offsetof(env_t
, data
);
209 int n
= (len
> sizeof(buf
)) ? sizeof(buf
) : len
;
211 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR
, off
,
214 crc_tmp
= crc32(crc_tmp
, buf
, n
);
219 if (crc_tmp
== crc
[i
])
223 if (!crc_ok
[0] && !crc_ok
[1]) {
228 } else if (crc_ok
[0] && !crc_ok
[1]) {
230 } else if (!crc_ok
[0] && crc_ok
[1]) {
233 /* both ok - check serial */
234 if (flags
[0] == ACTIVE_FLAG
&& flags
[1] == OBSOLETE_FLAG
)
236 else if (flags
[0] == OBSOLETE_FLAG
&& flags
[1] == ACTIVE_FLAG
)
238 else if (flags
[0] == 0xFF && flags
[1] == 0)
240 else if (flags
[1] == 0xFF && flags
[0] == 0)
242 else /* flags are equal - almost impossible */
246 if (gd
->env_valid
== 2)
247 gd
->env_addr
= off_env
[1] + offsetof(env_t
, data
);
248 else if (gd
->env_valid
== 1)
249 gd
->env_addr
= off_env
[0] + offsetof(env_t
, data
);
260 eeprom_init(); /* prepare for EEPROM read/write */
263 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR
,
264 CONFIG_ENV_OFFSET
+ offsetof(env_t
, crc
),
265 (uchar
*)&crc
, sizeof(ulong
));
269 off
= offsetof(env_t
, data
);
272 int n
= (len
> sizeof(buf
)) ? sizeof(buf
) : len
;
274 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR
,
275 CONFIG_ENV_OFFSET
+ off
, buf
, n
);
276 new = crc32(new, buf
, n
);
282 gd
->env_addr
= offsetof(env_t
, data
);