]>
Commit | Line | Data |
---|---|---|
8c66497e | 1 | /* |
ea882baf | 2 | * (C) Copyright 2000-2010 |
8c66497e HS |
3 | * Wolfgang Denk, DENX Software Engineering, [email protected]. |
4 | * | |
5 | * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com> | |
6 | * Andreas Heppel <[email protected]> | |
7 | * | |
8 | * (C) Copyright 2008 Atmel Corporation | |
9 | * | |
10 | * See file CREDITS for list of people who contributed to this | |
11 | * project. | |
12 | * | |
13 | * This program is free software; you can redistribute it and/or | |
14 | * modify it under the terms of the GNU General Public License as | |
15 | * published by the Free Software Foundation; either version 2 of | |
16 | * the License, or (at your option) any later version. | |
17 | * | |
18 | * This program is distributed in the hope that it will be useful, | |
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | * GNU General Public License for more details. | |
22 | * | |
23 | * You should have received a copy of the GNU General Public License | |
24 | * along with this program; if not, write to the Free Software | |
25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, | |
26 | * MA 02111-1307 USA | |
27 | */ | |
28 | #include <common.h> | |
8c66497e | 29 | #include <environment.h> |
5b3375ac | 30 | #include <malloc.h> |
8c66497e | 31 | #include <spi_flash.h> |
ea882baf WD |
32 | #include <search.h> |
33 | #include <errno.h> | |
8c66497e | 34 | |
0e8d1586 JCPV |
35 | #ifndef CONFIG_ENV_SPI_BUS |
36 | # define CONFIG_ENV_SPI_BUS 0 | |
8c66497e | 37 | #endif |
0e8d1586 JCPV |
38 | #ifndef CONFIG_ENV_SPI_CS |
39 | # define CONFIG_ENV_SPI_CS 0 | |
8c66497e | 40 | #endif |
0e8d1586 JCPV |
41 | #ifndef CONFIG_ENV_SPI_MAX_HZ |
42 | # define CONFIG_ENV_SPI_MAX_HZ 1000000 | |
8c66497e | 43 | #endif |
0e8d1586 JCPV |
44 | #ifndef CONFIG_ENV_SPI_MODE |
45 | # define CONFIG_ENV_SPI_MODE SPI_MODE_3 | |
8c66497e HS |
46 | #endif |
47 | ||
7319bcaf WW |
48 | #ifdef CONFIG_ENV_OFFSET_REDUND |
49 | static ulong env_offset = CONFIG_ENV_OFFSET; | |
50 | static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND; | |
51 | ||
52 | #define ACTIVE_FLAG 1 | |
53 | #define OBSOLETE_FLAG 0 | |
a3110f01 | 54 | #endif /* CONFIG_ENV_OFFSET_REDUND */ |
7319bcaf | 55 | |
8c66497e HS |
56 | DECLARE_GLOBAL_DATA_PTR; |
57 | ||
58 | /* references to names in env_common.c */ | |
59 | extern uchar default_environment[]; | |
8c66497e HS |
60 | |
61 | char * env_name_spec = "SPI Flash"; | |
8c66497e HS |
62 | |
63 | static struct spi_flash *env_flash; | |
64 | ||
65 | uchar env_get_char_spec(int index) | |
66 | { | |
67 | return *((uchar *)(gd->env_addr + index)); | |
68 | } | |
69 | ||
7319bcaf | 70 | #if defined(CONFIG_ENV_OFFSET_REDUND) |
7319bcaf WW |
71 | |
72 | int saveenv(void) | |
73 | { | |
ea882baf WD |
74 | env_t env_new; |
75 | ssize_t len; | |
76 | char *res; | |
77 | u32 saved_size, saved_offset; | |
78 | char *saved_buffer = NULL; | |
79 | u32 sector = 1; | |
80 | int ret; | |
2dc55d9e | 81 | char flag = OBSOLETE_FLAG; |
7319bcaf WW |
82 | |
83 | if (!env_flash) { | |
a3110f01 SB |
84 | env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, |
85 | CONFIG_ENV_SPI_CS, | |
86 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); | |
87 | if (!env_flash) { | |
88 | set_default_env("!spi_flash_probe() failed"); | |
89 | return 1; | |
90 | } | |
7319bcaf WW |
91 | } |
92 | ||
ea882baf | 93 | res = (char *)&env_new.data; |
2eb1573f | 94 | len = hexport_r(&env_htab, '\0', &res, ENV_SIZE); |
ea882baf WD |
95 | if (len < 0) { |
96 | error("Cannot export environment: errno = %d\n", errno); | |
97 | return 1; | |
98 | } | |
99 | env_new.crc = crc32(0, env_new.data, ENV_SIZE); | |
100 | env_new.flags = ACTIVE_FLAG; | |
101 | ||
a3110f01 SB |
102 | if (gd->env_valid == 1) { |
103 | env_new_offset = CONFIG_ENV_OFFSET_REDUND; | |
104 | env_offset = CONFIG_ENV_OFFSET; | |
105 | } else { | |
106 | env_new_offset = CONFIG_ENV_OFFSET; | |
107 | env_offset = CONFIG_ENV_OFFSET_REDUND; | |
108 | } | |
109 | ||
7319bcaf WW |
110 | /* Is the sector larger than the env (i.e. embedded) */ |
111 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { | |
112 | saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE; | |
113 | saved_offset = env_new_offset + CONFIG_ENV_SIZE; | |
114 | saved_buffer = malloc(saved_size); | |
115 | if (!saved_buffer) { | |
116 | ret = 1; | |
117 | goto done; | |
118 | } | |
119 | ret = spi_flash_read(env_flash, saved_offset, | |
120 | saved_size, saved_buffer); | |
121 | if (ret) | |
122 | goto done; | |
123 | } | |
124 | ||
125 | if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) { | |
126 | sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE; | |
127 | if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE) | |
128 | sector++; | |
129 | } | |
130 | ||
131 | puts("Erasing SPI flash..."); | |
132 | ret = spi_flash_erase(env_flash, env_new_offset, | |
133 | sector * CONFIG_ENV_SECT_SIZE); | |
134 | if (ret) | |
135 | goto done; | |
136 | ||
137 | puts("Writing to SPI flash..."); | |
7319bcaf | 138 | |
a3110f01 SB |
139 | ret = spi_flash_write(env_flash, env_new_offset, |
140 | CONFIG_ENV_SIZE, &env_new); | |
7319bcaf WW |
141 | if (ret) |
142 | goto done; | |
143 | ||
144 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { | |
145 | ret = spi_flash_write(env_flash, saved_offset, | |
146 | saved_size, saved_buffer); | |
147 | if (ret) | |
148 | goto done; | |
149 | } | |
150 | ||
a3110f01 SB |
151 | ret = spi_flash_write(env_flash, |
152 | env_offset + offsetof(env_t, flags), | |
153 | sizeof(env_new.flags), &flag); | |
154 | if (ret) | |
155 | goto done; | |
7319bcaf | 156 | |
7319bcaf WW |
157 | puts("done\n"); |
158 | ||
a3110f01 SB |
159 | gd->env_valid = (gd->env_valid == 2 ? 1 : 2); |
160 | ||
2dc55d9e | 161 | printf("Valid environment: %d\n", (int)gd->env_valid); |
a3110f01 | 162 | |
7319bcaf WW |
163 | done: |
164 | if (saved_buffer) | |
165 | free(saved_buffer); | |
166 | return ret; | |
167 | } | |
168 | ||
169 | void env_relocate_spec(void) | |
170 | { | |
171 | int ret; | |
172 | int crc1_ok = 0, crc2_ok = 0; | |
173 | env_t *tmp_env1 = NULL; | |
174 | env_t *tmp_env2 = NULL; | |
a3110f01 | 175 | env_t *ep = NULL; |
7319bcaf WW |
176 | |
177 | tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE); | |
7319bcaf | 178 | tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE); |
ea882baf WD |
179 | |
180 | if (!tmp_env1 || !tmp_env2) { | |
ea882baf | 181 | set_default_env("!malloc() failed"); |
2dc55d9e | 182 | goto out; |
7319bcaf WW |
183 | } |
184 | ||
185 | env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS, | |
186 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); | |
ea882baf WD |
187 | if (!env_flash) { |
188 | set_default_env("!spi_flash_probe() failed"); | |
2dc55d9e | 189 | goto out; |
ea882baf | 190 | } |
7319bcaf WW |
191 | |
192 | ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET, | |
193 | CONFIG_ENV_SIZE, tmp_env1); | |
ea882baf WD |
194 | if (ret) { |
195 | set_default_env("!spi_flash_read() failed"); | |
7319bcaf | 196 | goto err_read; |
ea882baf | 197 | } |
7319bcaf WW |
198 | |
199 | if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc) | |
200 | crc1_ok = 1; | |
7319bcaf WW |
201 | |
202 | ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND, | |
203 | CONFIG_ENV_SIZE, tmp_env2); | |
204 | if (!ret) { | |
205 | if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc) | |
206 | crc2_ok = 1; | |
7319bcaf WW |
207 | } |
208 | ||
a3110f01 | 209 | if (!crc1_ok && !crc2_ok) { |
a3110f01 | 210 | set_default_env("!bad CRC"); |
2dc55d9e | 211 | goto err_read; |
a3110f01 | 212 | } else if (crc1_ok && !crc2_ok) { |
7319bcaf | 213 | gd->env_valid = 1; |
7319bcaf | 214 | } else if (!crc1_ok && crc2_ok) { |
2dc55d9e | 215 | gd->env_valid = 2; |
216 | } else if (tmp_env1->flags == ACTIVE_FLAG && | |
217 | tmp_env2->flags == OBSOLETE_FLAG) { | |
7319bcaf | 218 | gd->env_valid = 1; |
2dc55d9e | 219 | } else if (tmp_env1->flags == OBSOLETE_FLAG && |
220 | tmp_env2->flags == ACTIVE_FLAG) { | |
a3110f01 | 221 | gd->env_valid = 2; |
2dc55d9e | 222 | } else if (tmp_env1->flags == tmp_env2->flags) { |
7319bcaf | 223 | gd->env_valid = 2; |
2dc55d9e | 224 | } else if (tmp_env1->flags == 0xFF) { |
7319bcaf | 225 | gd->env_valid = 2; |
7319bcaf WW |
226 | } else { |
227 | /* | |
228 | * this differs from code in env_flash.c, but I think a sane | |
229 | * default path is desirable. | |
230 | */ | |
231 | gd->env_valid = 2; | |
7319bcaf | 232 | } |
ea882baf | 233 | |
a3110f01 SB |
234 | if (gd->env_valid == 1) |
235 | ep = tmp_env1; | |
236 | else | |
237 | ep = tmp_env2; | |
238 | ||
239 | ret = env_import((char *)ep, 0); | |
240 | if (!ret) { | |
241 | error("Cannot import environment: errno = %d\n", errno); | |
242 | set_default_env("env_import failed"); | |
7319bcaf | 243 | } |
7319bcaf WW |
244 | |
245 | err_read: | |
246 | spi_flash_free(env_flash); | |
247 | env_flash = NULL; | |
7319bcaf | 248 | out: |
ea882baf WD |
249 | free(tmp_env1); |
250 | free(tmp_env2); | |
a3110f01 SB |
251 | |
252 | return; | |
7319bcaf WW |
253 | } |
254 | #else | |
8c66497e HS |
255 | int saveenv(void) |
256 | { | |
5b3375ac MF |
257 | u32 saved_size, saved_offset; |
258 | char *saved_buffer = NULL; | |
07efc9e3 | 259 | u32 sector = 1; |
590084a2 | 260 | int ret = 1; |
a3110f01 SB |
261 | env_t env_new; |
262 | char *res; | |
263 | ssize_t len; | |
07efc9e3 | 264 | |
8c66497e | 265 | if (!env_flash) { |
a3110f01 SB |
266 | env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, |
267 | CONFIG_ENV_SPI_CS, | |
268 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); | |
269 | if (!env_flash) { | |
270 | set_default_env("!spi_flash_probe() failed"); | |
271 | return 1; | |
272 | } | |
8c66497e HS |
273 | } |
274 | ||
5b3375ac MF |
275 | /* Is the sector larger than the env (i.e. embedded) */ |
276 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { | |
277 | saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE; | |
278 | saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE; | |
279 | saved_buffer = malloc(saved_size); | |
280 | if (!saved_buffer) { | |
5b3375ac MF |
281 | goto done; |
282 | } | |
a3110f01 SB |
283 | ret = spi_flash_read(env_flash, saved_offset, |
284 | saved_size, saved_buffer); | |
5b3375ac MF |
285 | if (ret) |
286 | goto done; | |
287 | } | |
288 | ||
0e8d1586 JCPV |
289 | if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) { |
290 | sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE; | |
291 | if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE) | |
07efc9e3 TL |
292 | sector++; |
293 | } | |
294 | ||
a3110f01 | 295 | res = (char *)&env_new.data; |
2eb1573f | 296 | len = hexport_r(&env_htab, '\0', &res, ENV_SIZE); |
a3110f01 SB |
297 | if (len < 0) { |
298 | error("Cannot export environment: errno = %d\n", errno); | |
299 | goto done; | |
300 | } | |
301 | env_new.crc = crc32(0, env_new.data, ENV_SIZE); | |
302 | ||
8c66497e | 303 | puts("Erasing SPI flash..."); |
a3110f01 SB |
304 | ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET, |
305 | sector * CONFIG_ENV_SECT_SIZE); | |
5b3375ac MF |
306 | if (ret) |
307 | goto done; | |
8c66497e HS |
308 | |
309 | puts("Writing to SPI flash..."); | |
a3110f01 SB |
310 | ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, |
311 | CONFIG_ENV_SIZE, &env_new); | |
5b3375ac MF |
312 | if (ret) |
313 | goto done; | |
8c66497e | 314 | |
5b3375ac | 315 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
a3110f01 SB |
316 | ret = spi_flash_write(env_flash, saved_offset, |
317 | saved_size, saved_buffer); | |
5b3375ac MF |
318 | if (ret) |
319 | goto done; | |
320 | } | |
321 | ||
322 | ret = 0; | |
8c66497e | 323 | puts("done\n"); |
5b3375ac MF |
324 | |
325 | done: | |
326 | if (saved_buffer) | |
327 | free(saved_buffer); | |
328 | return ret; | |
8c66497e HS |
329 | } |
330 | ||
331 | void env_relocate_spec(void) | |
332 | { | |
ea882baf | 333 | char buf[CONFIG_ENV_SIZE]; |
8c66497e HS |
334 | int ret; |
335 | ||
0e8d1586 JCPV |
336 | env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS, |
337 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); | |
ea882baf WD |
338 | if (!env_flash) { |
339 | set_default_env("!spi_flash_probe() failed"); | |
340 | return; | |
341 | } | |
8c66497e | 342 | |
ea882baf WD |
343 | ret = spi_flash_read(env_flash, |
344 | CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf); | |
345 | if (ret) { | |
346 | set_default_env("!spi_flash_read() failed"); | |
347 | goto out; | |
348 | } | |
8c66497e | 349 | |
ea882baf | 350 | ret = env_import(buf, 1); |
8c66497e | 351 | |
ea882baf WD |
352 | if (ret) |
353 | gd->env_valid = 1; | |
354 | out: | |
8c66497e HS |
355 | spi_flash_free(env_flash); |
356 | env_flash = NULL; | |
8c66497e | 357 | } |
7319bcaf | 358 | #endif |
8c66497e HS |
359 | |
360 | int env_init(void) | |
361 | { | |
362 | /* SPI flash isn't usable before relocation */ | |
363 | gd->env_addr = (ulong)&default_environment[0]; | |
364 | gd->env_valid = 1; | |
365 | ||
366 | return 0; | |
367 | } |