2 * (C) Copyright 2000-2002
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)
35 DECLARE_GLOBAL_DATA_PTR;
37 env_t *env_ptr = NULL;
39 char * env_name_spec = "EEPROM";
40 int env_eeprom_bus = -1;
42 static int eeprom_bus_read (unsigned dev_addr, unsigned offset, uchar *buffer,
46 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
47 int old_bus = i2c_get_bus_num();
49 if (gd->flags & GD_FLG_RELOC) {
50 if (env_eeprom_bus == -1) {
51 I2C_MUX_DEVICE *dev = NULL;
52 dev = i2c_mux_ident_muxstring(
53 (uchar *)CONFIG_I2C_ENV_EEPROM_BUS);
55 env_eeprom_bus = dev->busid;
57 printf ("error adding env eeprom bus.\n");
59 if (old_bus != env_eeprom_bus) {
60 i2c_set_bus_num(env_eeprom_bus);
61 old_bus = env_eeprom_bus;
64 rcode = i2c_mux_ident_muxstring_f(
65 (uchar *)CONFIG_I2C_ENV_EEPROM_BUS);
69 rcode = eeprom_read (dev_addr, offset, buffer, cnt);
70 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
71 if (old_bus != env_eeprom_bus)
72 i2c_set_bus_num(old_bus);
77 static int eeprom_bus_write (unsigned dev_addr, unsigned offset, uchar *buffer,
81 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
82 int old_bus = i2c_get_bus_num();
84 rcode = i2c_mux_ident_muxstring_f((uchar *)CONFIG_I2C_ENV_EEPROM_BUS);
86 rcode = eeprom_write (dev_addr, offset, buffer, cnt);
87 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
88 i2c_set_bus_num(old_bus);
93 uchar env_get_char_spec (int index)
97 eeprom_bus_read (CONFIG_SYS_DEF_EEPROM_ADDR,
98 CONFIG_ENV_OFFSET+index+offsetof(env_t,data),
104 void env_relocate_spec (void)
106 eeprom_bus_read (CONFIG_SYS_DEF_EEPROM_ADDR,
114 return eeprom_bus_write (CONFIG_SYS_DEF_EEPROM_ADDR,
120 /************************************************************************
121 * Initialize Environment use
123 * We are still running from ROM, so data use is limited
124 * Use a (moderately small) buffer on the stack
132 eeprom_init (); /* prepare for EEPROM read/write */
135 eeprom_bus_read (CONFIG_SYS_DEF_EEPROM_ADDR,
136 CONFIG_ENV_OFFSET+offsetof(env_t,crc),
137 (uchar *)&crc, sizeof(ulong));
141 off = offsetof(env_t,data);
143 int n = (len > sizeof(buf)) ? sizeof(buf) : len;
145 eeprom_bus_read (CONFIG_SYS_DEF_EEPROM_ADDR,
146 CONFIG_ENV_OFFSET + off, buf, n);
147 new = crc32 (new, buf, n);
153 gd->env_addr = offsetof(env_t,data);