]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/env_eeprom.c
i2c, multibus: get rid of CONFIG_I2C_MUX
[karo-tx-uboot.git] / common / env_eeprom.c
1 /*
2  * (C) Copyright 2000-2010
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>
28 #include <command.h>
29 #include <environment.h>
30 #include <linux/stddef.h>
31 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
32 #include <i2c.h>
33 #endif
34 #include <search.h>
35 #include <errno.h>
36 #include <linux/compiler.h>     /* for BUG_ON */
37
38 DECLARE_GLOBAL_DATA_PTR;
39
40 env_t *env_ptr;
41
42 char *env_name_spec = "EEPROM";
43 int env_eeprom_bus = -1;
44
45 static int eeprom_bus_read(unsigned dev_addr, unsigned offset,
46                            uchar *buffer, unsigned cnt)
47 {
48         int rcode;
49 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
50         int old_bus = i2c_get_bus_num();
51
52         if (old_bus != CONFIG_I2C_ENV_EEPROM_BUS)
53                 i2c_set_bus_num(CONFIG_I2C_ENV_EEPROM_BUS);
54 #endif
55
56         rcode = eeprom_read(dev_addr, offset, buffer, cnt);
57
58 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
59         if (old_bus != env_eeprom_bus)
60                 i2c_set_bus_num(old_bus);
61 #endif
62
63         return rcode;
64 }
65
66 static int eeprom_bus_write(unsigned dev_addr, unsigned offset,
67                             uchar *buffer, unsigned cnt)
68 {
69         int rcode;
70 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
71         int old_bus = i2c_get_bus_num();
72
73         if (old_bus != CONFIG_I2C_ENV_EEPROM_BUS)
74                 i2c_set_bus_num(CONFIG_I2C_ENV_EEPROM_BUS);
75 #endif
76
77         rcode = eeprom_write(dev_addr, offset, buffer, cnt);
78
79 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
80         i2c_set_bus_num(old_bus);
81 #endif
82         return rcode;
83 }
84
85 uchar env_get_char_spec(int index)
86 {
87         uchar c;
88         unsigned int off = CONFIG_ENV_OFFSET;
89
90 #ifdef CONFIG_ENV_OFFSET_REDUND
91         if (gd->env_valid == 2)
92                 off = CONFIG_ENV_OFFSET_REDUND;
93 #endif
94         eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
95                         off + index + offsetof(env_t, data), &c, 1);
96
97         return c;
98 }
99
100 void env_relocate_spec(void)
101 {
102         char buf[CONFIG_ENV_SIZE];
103         unsigned int off = CONFIG_ENV_OFFSET;
104
105 #ifdef CONFIG_ENV_OFFSET_REDUND
106         if (gd->env_valid == 2)
107                 off = CONFIG_ENV_OFFSET_REDUND;
108 #endif
109         eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
110                         off, (uchar *)buf, CONFIG_ENV_SIZE);
111
112         env_import(buf, 1);
113 }
114
115 int saveenv(void)
116 {
117         env_t   env_new;
118         ssize_t len;
119         char    *res;
120         int     rc;
121         unsigned int off        = CONFIG_ENV_OFFSET;
122 #ifdef CONFIG_ENV_OFFSET_REDUND
123         unsigned int off_red    = CONFIG_ENV_OFFSET_REDUND;
124         char flag_obsolete      = OBSOLETE_FLAG;
125 #endif
126
127         BUG_ON(env_ptr != NULL);
128
129         res = (char *)&env_new.data;
130         len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
131         if (len < 0) {
132                 error("Cannot export environment: errno = %d\n", errno);
133                 return 1;
134         }
135         env_new.crc = crc32(0, env_new.data, ENV_SIZE);
136
137 #ifdef CONFIG_ENV_OFFSET_REDUND
138         if (gd->env_valid == 1) {
139                 off     = CONFIG_ENV_OFFSET_REDUND;
140                 off_red = CONFIG_ENV_OFFSET;
141         }
142
143         env_new.flags = ACTIVE_FLAG;
144 #endif
145
146         rc = eeprom_bus_write(CONFIG_SYS_DEF_EEPROM_ADDR,
147                               off, (uchar *)&env_new, CONFIG_ENV_SIZE);
148
149 #ifdef CONFIG_ENV_OFFSET_REDUND
150         if (rc == 0) {
151                 eeprom_bus_write(CONFIG_SYS_DEF_EEPROM_ADDR,
152                                  off_red + offsetof(env_t, flags),
153                                  (uchar *)&flag_obsolete, 1);
154
155                 if (gd->env_valid == 1)
156                         gd->env_valid = 2;
157                 else
158                         gd->env_valid = 1;
159         }
160 #endif
161         return rc;
162 }
163
164 /*
165  * Initialize Environment use
166  *
167  * We are still running from ROM, so data use is limited.
168  * Use a (moderately small) buffer on the stack
169  */
170 #ifdef CONFIG_ENV_OFFSET_REDUND
171 int env_init(void)
172 {
173         ulong len, crc[2], crc_tmp;
174         unsigned int off, off_env[2];
175         uchar buf[64], flags[2];
176         int i, crc_ok[2] = {0, 0};
177
178         eeprom_init();  /* prepare for EEPROM read/write */
179
180         off_env[0] = CONFIG_ENV_OFFSET;
181         off_env[1] = CONFIG_ENV_OFFSET_REDUND;
182
183         for (i = 0; i < 2; i++) {
184                 /* read CRC */
185                 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
186                                 off_env[i] + offsetof(env_t, crc),
187                                 (uchar *)&crc[i], sizeof(ulong));
188                 /* read FLAGS */
189                 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
190                                 off_env[i] + offsetof(env_t, flags),
191                                 (uchar *)&flags[i], sizeof(uchar));
192
193                 crc_tmp = 0;
194                 len = ENV_SIZE;
195                 off = off_env[i] + offsetof(env_t, data);
196                 while (len > 0) {
197                         int n = (len > sizeof(buf)) ? sizeof(buf) : len;
198
199                         eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR, off,
200                                         buf, n);
201
202                         crc_tmp = crc32(crc_tmp, buf, n);
203                         len -= n;
204                         off += n;
205                 }
206
207                 if (crc_tmp == crc[i])
208                         crc_ok[i] = 1;
209         }
210
211         if (!crc_ok[0] && !crc_ok[1]) {
212                 gd->env_addr    = 0;
213                 gd->env_valid   = 0;
214
215                 return 0;
216         } else if (crc_ok[0] && !crc_ok[1]) {
217                 gd->env_valid = 1;
218         } else if (!crc_ok[0] && crc_ok[1]) {
219                 gd->env_valid = 2;
220         } else {
221                 /* both ok - check serial */
222                 if (flags[0] == ACTIVE_FLAG && flags[1] == OBSOLETE_FLAG)
223                         gd->env_valid = 1;
224                 else if (flags[0] == OBSOLETE_FLAG && flags[1] == ACTIVE_FLAG)
225                         gd->env_valid = 2;
226                 else if (flags[0] == 0xFF && flags[1] == 0)
227                         gd->env_valid = 2;
228                 else if (flags[1] == 0xFF && flags[0] == 0)
229                         gd->env_valid = 1;
230                 else /* flags are equal - almost impossible */
231                         gd->env_valid = 1;
232         }
233
234         if (gd->env_valid == 2)
235                 gd->env_addr = off_env[1] + offsetof(env_t, data);
236         else if (gd->env_valid == 1)
237                 gd->env_addr = off_env[0] + offsetof(env_t, data);
238
239         return 0;
240 }
241 #else
242 int env_init(void)
243 {
244         ulong crc, len, new;
245         unsigned off;
246         uchar buf[64];
247
248         eeprom_init();  /* prepare for EEPROM read/write */
249
250         /* read old CRC */
251         eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
252                         CONFIG_ENV_OFFSET + offsetof(env_t, crc),
253                         (uchar *)&crc, sizeof(ulong));
254
255         new = 0;
256         len = ENV_SIZE;
257         off = offsetof(env_t, data);
258
259         while (len > 0) {
260                 int n = (len > sizeof(buf)) ? sizeof(buf) : len;
261
262                 eeprom_bus_read(CONFIG_SYS_DEF_EEPROM_ADDR,
263                                 CONFIG_ENV_OFFSET + off, buf, n);
264                 new = crc32(new, buf, n);
265                 len -= n;
266                 off += n;
267         }
268
269         if (crc == new) {
270                 gd->env_addr    = offsetof(env_t, data);
271                 gd->env_valid   = 1;
272         } else {
273                 gd->env_addr    = 0;
274                 gd->env_valid   = 0;
275         }
276
277         return 0;
278 }
279 #endif