]>
Commit | Line | Data |
---|---|---|
8c66497e | 1 | /* |
ea882baf | 2 | * (C) Copyright 2000-2010 |
8c66497e HS |
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 | * (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 | |
54 | #endif /* CONFIG_ENV_ADDR_REDUND */ | |
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"; | |
62 | env_t *env_ptr; | |
63 | ||
64 | static struct spi_flash *env_flash; | |
65 | ||
66 | uchar env_get_char_spec(int index) | |
67 | { | |
68 | return *((uchar *)(gd->env_addr + index)); | |
69 | } | |
70 | ||
7319bcaf WW |
71 | #if defined(CONFIG_ENV_OFFSET_REDUND) |
72 | void swap_env(void) | |
73 | { | |
74 | ulong tmp_offset = env_offset; | |
75 | ||
76 | env_offset = env_new_offset; | |
77 | env_new_offset = tmp_offset; | |
78 | } | |
79 | ||
80 | int saveenv(void) | |
81 | { | |
ea882baf WD |
82 | env_t env_new; |
83 | ssize_t len; | |
84 | char *res; | |
85 | u32 saved_size, saved_offset; | |
86 | char *saved_buffer = NULL; | |
87 | u32 sector = 1; | |
88 | int ret; | |
89 | char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG; | |
7319bcaf WW |
90 | |
91 | if (!env_flash) { | |
92 | puts("Environment SPI flash not initialized\n"); | |
93 | return 1; | |
94 | } | |
95 | ||
ea882baf WD |
96 | res = (char *)&env_new.data; |
97 | len = hexport('\0', &res, ENV_SIZE); | |
98 | if (len < 0) { | |
99 | error("Cannot export environment: errno = %d\n", errno); | |
100 | return 1; | |
101 | } | |
102 | env_new.crc = crc32(0, env_new.data, ENV_SIZE); | |
103 | env_new.flags = ACTIVE_FLAG; | |
104 | ||
7319bcaf WW |
105 | /* Is the sector larger than the env (i.e. embedded) */ |
106 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { | |
107 | saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE; | |
108 | saved_offset = env_new_offset + CONFIG_ENV_SIZE; | |
109 | saved_buffer = malloc(saved_size); | |
110 | if (!saved_buffer) { | |
111 | ret = 1; | |
112 | goto done; | |
113 | } | |
114 | ret = spi_flash_read(env_flash, saved_offset, | |
115 | saved_size, saved_buffer); | |
116 | if (ret) | |
117 | goto done; | |
118 | } | |
119 | ||
120 | if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) { | |
121 | sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE; | |
122 | if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE) | |
123 | sector++; | |
124 | } | |
125 | ||
126 | puts("Erasing SPI flash..."); | |
127 | ret = spi_flash_erase(env_flash, env_new_offset, | |
128 | sector * CONFIG_ENV_SECT_SIZE); | |
129 | if (ret) | |
130 | goto done; | |
131 | ||
132 | puts("Writing to SPI flash..."); | |
133 | ret = spi_flash_write(env_flash, | |
134 | env_new_offset + offsetof(env_t, data), | |
ea882baf | 135 | sizeof(env_new.data), env_new.data); |
7319bcaf WW |
136 | if (ret) |
137 | goto done; | |
138 | ||
139 | ret = spi_flash_write(env_flash, | |
140 | env_new_offset + offsetof(env_t, crc), | |
ea882baf | 141 | sizeof(env_new.crc), &env_new.crc); |
7319bcaf WW |
142 | if (ret) |
143 | goto done; | |
144 | ||
145 | ret = spi_flash_write(env_flash, | |
146 | env_offset + offsetof(env_t, flags), | |
ea882baf | 147 | sizeof(env_new.flags), &flag); |
7319bcaf WW |
148 | if (ret) |
149 | goto done; | |
150 | ||
151 | ret = spi_flash_write(env_flash, | |
152 | env_new_offset + offsetof(env_t, flags), | |
ea882baf | 153 | sizeof(env_new.flags), &new_flag); |
7319bcaf WW |
154 | if (ret) |
155 | goto done; | |
156 | ||
157 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { | |
158 | ret = spi_flash_write(env_flash, saved_offset, | |
159 | saved_size, saved_buffer); | |
160 | if (ret) | |
161 | goto done; | |
162 | } | |
163 | ||
164 | swap_env(); | |
165 | ||
166 | ret = 0; | |
167 | puts("done\n"); | |
168 | ||
169 | done: | |
170 | if (saved_buffer) | |
171 | free(saved_buffer); | |
172 | return ret; | |
173 | } | |
174 | ||
175 | void env_relocate_spec(void) | |
176 | { | |
177 | int ret; | |
178 | int crc1_ok = 0, crc2_ok = 0; | |
179 | env_t *tmp_env1 = NULL; | |
180 | env_t *tmp_env2 = NULL; | |
ea882baf | 181 | env_t ep; |
7319bcaf WW |
182 | uchar flag1, flag2; |
183 | /* current_env is set only in case both areas are valid! */ | |
184 | int current_env = 0; | |
185 | ||
186 | tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE); | |
7319bcaf | 187 | tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE); |
ea882baf WD |
188 | |
189 | if (!tmp_env1 || !tmp_env2) { | |
190 | free(tmp_env1); | |
191 | free(tmp_env2); | |
192 | set_default_env("!malloc() failed"); | |
193 | return; | |
7319bcaf WW |
194 | } |
195 | ||
196 | env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS, | |
197 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); | |
ea882baf WD |
198 | if (!env_flash) { |
199 | set_default_env("!spi_flash_probe() failed"); | |
200 | return; | |
201 | } | |
7319bcaf WW |
202 | |
203 | ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET, | |
204 | CONFIG_ENV_SIZE, tmp_env1); | |
ea882baf WD |
205 | if (ret) { |
206 | set_default_env("!spi_flash_read() failed"); | |
7319bcaf | 207 | goto err_read; |
ea882baf | 208 | } |
7319bcaf WW |
209 | |
210 | if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc) | |
211 | crc1_ok = 1; | |
212 | flag1 = tmp_env1->flags; | |
213 | ||
214 | ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND, | |
215 | CONFIG_ENV_SIZE, tmp_env2); | |
216 | if (!ret) { | |
217 | if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc) | |
218 | crc2_ok = 1; | |
219 | flag2 = tmp_env2->flags; | |
220 | } | |
221 | ||
222 | if (!crc1_ok && !crc2_ok) | |
223 | goto err_crc; | |
224 | else if (crc1_ok && !crc2_ok) { | |
225 | gd->env_valid = 1; | |
ea882baf | 226 | ep = tmp_env1; |
7319bcaf WW |
227 | } else if (!crc1_ok && crc2_ok) { |
228 | gd->env_valid = 1; | |
ea882baf | 229 | ep = tmp_env2; |
7319bcaf WW |
230 | swap_env(); |
231 | } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) { | |
232 | gd->env_valid = 1; | |
ea882baf | 233 | ep = tmp_env1; |
7319bcaf WW |
234 | } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) { |
235 | gd->env_valid = 1; | |
ea882baf | 236 | ep = tmp_env2; |
7319bcaf WW |
237 | swap_env(); |
238 | } else if (flag1 == flag2) { | |
239 | gd->env_valid = 2; | |
ea882baf | 240 | ep = tmp_env1; |
7319bcaf WW |
241 | current_env = 1; |
242 | } else if (flag1 == 0xFF) { | |
243 | gd->env_valid = 2; | |
ea882baf | 244 | ep = tmp_env1; |
7319bcaf WW |
245 | current_env = 1; |
246 | } else { | |
247 | /* | |
248 | * this differs from code in env_flash.c, but I think a sane | |
249 | * default path is desirable. | |
250 | */ | |
251 | gd->env_valid = 2; | |
ea882baf | 252 | ep = tmp_env2; |
7319bcaf WW |
253 | swap_env(); |
254 | current_env = 2; | |
255 | } | |
ea882baf WD |
256 | |
257 | rc = env_import((char *)ep, 0); | |
258 | if (!rc) { | |
259 | error("Cannot import environment: errno = %d\n", errno); | |
260 | goto out; | |
261 | } | |
262 | ||
7319bcaf WW |
263 | if (current_env == 1) { |
264 | if (flag2 != OBSOLETE_FLAG) { | |
265 | flag2 = OBSOLETE_FLAG; | |
266 | spi_flash_write(env_flash, | |
267 | env_new_offset + offsetof(env_t, flags), | |
ea882baf | 268 | sizeof(env_new.flags), &flag2); |
7319bcaf WW |
269 | } |
270 | if (flag1 != ACTIVE_FLAG) { | |
271 | flag1 = ACTIVE_FLAG; | |
272 | spi_flash_write(env_flash, | |
273 | env_offset + offsetof(env_t, flags), | |
ea882baf | 274 | sizeof(env_new.flags), &flag1); |
7319bcaf WW |
275 | } |
276 | } else if (current_env == 2) { | |
277 | if (flag1 != OBSOLETE_FLAG) { | |
278 | flag1 = OBSOLETE_FLAG; | |
279 | spi_flash_write(env_flash, | |
280 | env_new_offset + offsetof(env_t, flags), | |
ea882baf | 281 | sizeof(env_new.flags), &flag1); |
7319bcaf WW |
282 | } |
283 | if (flag2 != ACTIVE_FLAG) { | |
284 | flag2 = ACTIVE_FLAG; | |
285 | spi_flash_write(env_flash, | |
286 | env_offset + offsetof(env_t, flags), | |
ea882baf | 287 | sizeof(env_new.flags), &flag2); |
7319bcaf WW |
288 | } |
289 | } | |
290 | if (gd->env_valid == 2) { | |
291 | puts("*** Warning - some problems detected " | |
292 | "reading environment; recovered successfully\n\n"); | |
293 | } | |
294 | if (tmp_env1) | |
295 | free(tmp_env1); | |
296 | if (tmp_env2) | |
297 | free(tmp_env2); | |
298 | return; | |
299 | ||
300 | err_read: | |
301 | spi_flash_free(env_flash); | |
302 | env_flash = NULL; | |
7319bcaf | 303 | out: |
ea882baf WD |
304 | free(tmp_env1); |
305 | free(tmp_env2); | |
7319bcaf WW |
306 | } |
307 | #else | |
8c66497e HS |
308 | int saveenv(void) |
309 | { | |
5b3375ac MF |
310 | u32 saved_size, saved_offset; |
311 | char *saved_buffer = NULL; | |
07efc9e3 | 312 | u32 sector = 1; |
5b3375ac | 313 | int ret; |
07efc9e3 | 314 | |
8c66497e HS |
315 | if (!env_flash) { |
316 | puts("Environment SPI flash not initialized\n"); | |
317 | return 1; | |
318 | } | |
319 | ||
5b3375ac MF |
320 | /* Is the sector larger than the env (i.e. embedded) */ |
321 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { | |
322 | saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE; | |
323 | saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE; | |
324 | saved_buffer = malloc(saved_size); | |
325 | if (!saved_buffer) { | |
326 | ret = 1; | |
327 | goto done; | |
328 | } | |
329 | ret = spi_flash_read(env_flash, saved_offset, saved_size, saved_buffer); | |
330 | if (ret) | |
331 | goto done; | |
332 | } | |
333 | ||
0e8d1586 JCPV |
334 | if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) { |
335 | sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE; | |
336 | if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE) | |
07efc9e3 TL |
337 | sector++; |
338 | } | |
339 | ||
8c66497e | 340 | puts("Erasing SPI flash..."); |
5b3375ac MF |
341 | ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET, sector * CONFIG_ENV_SECT_SIZE); |
342 | if (ret) | |
343 | goto done; | |
8c66497e HS |
344 | |
345 | puts("Writing to SPI flash..."); | |
5b3375ac MF |
346 | ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, env_ptr); |
347 | if (ret) | |
348 | goto done; | |
8c66497e | 349 | |
5b3375ac MF |
350 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
351 | ret = spi_flash_write(env_flash, saved_offset, saved_size, saved_buffer); | |
352 | if (ret) | |
353 | goto done; | |
354 | } | |
355 | ||
356 | ret = 0; | |
8c66497e | 357 | puts("done\n"); |
5b3375ac MF |
358 | |
359 | done: | |
360 | if (saved_buffer) | |
361 | free(saved_buffer); | |
362 | return ret; | |
8c66497e HS |
363 | } |
364 | ||
365 | void env_relocate_spec(void) | |
366 | { | |
ea882baf | 367 | char buf[CONFIG_ENV_SIZE]; |
8c66497e HS |
368 | int ret; |
369 | ||
0e8d1586 JCPV |
370 | env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS, |
371 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); | |
ea882baf WD |
372 | if (!env_flash) { |
373 | set_default_env("!spi_flash_probe() failed"); | |
374 | return; | |
375 | } | |
8c66497e | 376 | |
ea882baf WD |
377 | ret = spi_flash_read(env_flash, |
378 | CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf); | |
379 | if (ret) { | |
380 | set_default_env("!spi_flash_read() failed"); | |
381 | goto out; | |
382 | } | |
8c66497e | 383 | |
ea882baf | 384 | ret = env_import(buf, 1); |
8c66497e | 385 | |
ea882baf WD |
386 | if (ret) |
387 | gd->env_valid = 1; | |
388 | out: | |
8c66497e HS |
389 | spi_flash_free(env_flash); |
390 | env_flash = NULL; | |
8c66497e | 391 | } |
7319bcaf | 392 | #endif |
8c66497e HS |
393 | |
394 | int env_init(void) | |
395 | { | |
396 | /* SPI flash isn't usable before relocation */ | |
397 | gd->env_addr = (ulong)&default_environment[0]; | |
398 | gd->env_valid = 1; | |
399 | ||
400 | return 0; | |
401 | } |