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