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