]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/env_eeprom.c
cmd_eeprom: I2C updates
[people/ms/u-boot.git] / common / env_eeprom.c
CommitLineData
0bc4a1ac
WD
1/*
2 * (C) Copyright 2000-2002
3 * 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>
7
8 * See file CREDITS for list of people who contributed to this
9 * project.
10 *
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.
15 *
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.
20 *
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,
24 * MA 02111-1307 USA
25 */
26
27#include <common.h>
0bc4a1ac
WD
28#include <command.h>
29#include <environment.h>
0bc4a1ac 30#include <linux/stddef.h>
548738b4
HS
31#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
32#include <i2c.h>
33#endif
0bc4a1ac 34
d87080b7
WD
35DECLARE_GLOBAL_DATA_PTR;
36
0bc4a1ac
WD
37env_t *env_ptr = NULL;
38
39char * env_name_spec = "EEPROM";
548738b4
HS
40int env_eeprom_bus = -1;
41
42static int eeprom_bus_read (unsigned dev_addr, unsigned offset, uchar *buffer,
43 unsigned cnt)
44{
45 int rcode;
46#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
47 int old_bus = i2c_get_bus_num();
48
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);
54 if (dev != NULL) {
55 env_eeprom_bus = dev->busid;
56 } else
57 printf ("error adding env eeprom bus.\n");
58 }
59 if (old_bus != env_eeprom_bus) {
60 i2c_set_bus_num(env_eeprom_bus);
61 old_bus = env_eeprom_bus;
62 }
63 } else {
64 rcode = i2c_mux_ident_muxstring_f(
65 (uchar *)CONFIG_I2C_ENV_EEPROM_BUS);
66 }
67#endif
68
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);
73#endif
74 return rcode;
75}
76
77static int eeprom_bus_write (unsigned dev_addr, unsigned offset, uchar *buffer,
78 unsigned cnt)
79{
80 int rcode;
81#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
82 int old_bus = i2c_get_bus_num();
83
84 rcode = i2c_mux_ident_muxstring_f((uchar *)CONFIG_I2C_ENV_EEPROM_BUS);
85#endif
86 rcode = eeprom_write (dev_addr, offset, buffer, cnt);
87#if defined(CONFIG_I2C_ENV_EEPROM_BUS)
88 i2c_set_bus_num(old_bus);
89#endif
90 return rcode;
91}
0bc4a1ac 92
0bc4a1ac
WD
93uchar env_get_char_spec (int index)
94{
95 uchar c;
96
548738b4 97 eeprom_bus_read (CONFIG_SYS_DEF_EEPROM_ADDR,
0e8d1586 98 CONFIG_ENV_OFFSET+index+offsetof(env_t,data),
0bc4a1ac
WD
99 &c, 1);
100
101 return (c);
102}
103
104void env_relocate_spec (void)
105{
548738b4 106 eeprom_bus_read (CONFIG_SYS_DEF_EEPROM_ADDR,
0e8d1586 107 CONFIG_ENV_OFFSET,
0bc4a1ac 108 (uchar*)env_ptr,
0e8d1586 109 CONFIG_ENV_SIZE);
0bc4a1ac
WD
110}
111
112int saveenv(void)
113{
548738b4 114 return eeprom_bus_write (CONFIG_SYS_DEF_EEPROM_ADDR,
0e8d1586 115 CONFIG_ENV_OFFSET,
0bc4a1ac 116 (uchar *)env_ptr,
0e8d1586 117 CONFIG_ENV_SIZE);
0bc4a1ac
WD
118}
119
120/************************************************************************
121 * Initialize Environment use
122 *
123 * We are still running from ROM, so data use is limited
124 * Use a (moderately small) buffer on the stack
125 */
126int env_init(void)
127{
0bc4a1ac
WD
128 ulong crc, len, new;
129 unsigned off;
130 uchar buf[64];
131
132 eeprom_init (); /* prepare for EEPROM read/write */
133
134 /* read old CRC */
548738b4 135 eeprom_bus_read (CONFIG_SYS_DEF_EEPROM_ADDR,
0e8d1586 136 CONFIG_ENV_OFFSET+offsetof(env_t,crc),
0bc4a1ac
WD
137 (uchar *)&crc, sizeof(ulong));
138
139 new = 0;
140 len = ENV_SIZE;
141 off = offsetof(env_t,data);
142 while (len > 0) {
143 int n = (len > sizeof(buf)) ? sizeof(buf) : len;
144
548738b4
HS
145 eeprom_bus_read (CONFIG_SYS_DEF_EEPROM_ADDR,
146 CONFIG_ENV_OFFSET + off, buf, n);
0bc4a1ac
WD
147 new = crc32 (new, buf, n);
148 len -= n;
149 off += n;
150 }
151
152 if (crc == new) {
153 gd->env_addr = offsetof(env_t,data);
154 gd->env_valid = 1;
155 } else {
156 gd->env_addr = 0;
157 gd->env_valid = 0;
158 }
159
160 return (0);
161}