]>
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 | * | |
402ba1e3 | 10 | * SPDX-License-Identifier: GPL-2.0+ |
8c66497e HS |
11 | */ |
12 | #include <common.h> | |
9d922450 | 13 | #include <dm.h> |
8c66497e | 14 | #include <environment.h> |
5b3375ac | 15 | #include <malloc.h> |
843c9e87 | 16 | #include <spi.h> |
8c66497e | 17 | #include <spi_flash.h> |
ea882baf WD |
18 | #include <search.h> |
19 | #include <errno.h> | |
19c31285 | 20 | #include <dm/device-internal.h> |
8c66497e | 21 | |
0e8d1586 | 22 | #ifndef CONFIG_ENV_SPI_BUS |
2e4e5ad4 | 23 | # define CONFIG_ENV_SPI_BUS CONFIG_SF_DEFAULT_BUS |
8c66497e | 24 | #endif |
0e8d1586 | 25 | #ifndef CONFIG_ENV_SPI_CS |
2e4e5ad4 | 26 | # define CONFIG_ENV_SPI_CS CONFIG_SF_DEFAULT_CS |
8c66497e | 27 | #endif |
0e8d1586 | 28 | #ifndef CONFIG_ENV_SPI_MAX_HZ |
2e4e5ad4 | 29 | # define CONFIG_ENV_SPI_MAX_HZ CONFIG_SF_DEFAULT_SPEED |
8c66497e | 30 | #endif |
0e8d1586 | 31 | #ifndef CONFIG_ENV_SPI_MODE |
2e4e5ad4 | 32 | # define CONFIG_ENV_SPI_MODE CONFIG_SF_DEFAULT_MODE |
8c66497e HS |
33 | #endif |
34 | ||
7319bcaf | 35 | #ifdef CONFIG_ENV_OFFSET_REDUND |
eb58a7fc IG |
36 | static ulong env_offset = CONFIG_ENV_OFFSET; |
37 | static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND; | |
7319bcaf | 38 | |
eb58a7fc IG |
39 | #define ACTIVE_FLAG 1 |
40 | #define OBSOLETE_FLAG 0 | |
a3110f01 | 41 | #endif /* CONFIG_ENV_OFFSET_REDUND */ |
7319bcaf | 42 | |
8c66497e HS |
43 | DECLARE_GLOBAL_DATA_PTR; |
44 | ||
eb58a7fc | 45 | char *env_name_spec = "SPI Flash"; |
8c66497e HS |
46 | |
47 | static struct spi_flash *env_flash; | |
48 | ||
afa81a77 | 49 | static int setup_flash_device(void) |
7319bcaf | 50 | { |
19c31285 GQ |
51 | #ifdef CONFIG_DM_SPI_FLASH |
52 | struct udevice *new; | |
afa81a77 | 53 | int ret; |
19c31285 | 54 | |
96907c0f | 55 | /* speed and mode will be read from DT */ |
19c31285 | 56 | ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS, |
96907c0f | 57 | 0, 0, &new); |
19c31285 GQ |
58 | if (ret) { |
59 | set_default_env("!spi_flash_probe_bus_cs() failed"); | |
60 | return 1; | |
61 | } | |
62 | ||
63 | env_flash = dev_get_uclass_priv(new); | |
64 | #else | |
7319bcaf WW |
65 | |
66 | if (!env_flash) { | |
a3110f01 SB |
67 | env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, |
68 | CONFIG_ENV_SPI_CS, | |
69 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); | |
70 | if (!env_flash) { | |
71 | set_default_env("!spi_flash_probe() failed"); | |
72 | return 1; | |
73 | } | |
7319bcaf | 74 | } |
19c31285 | 75 | #endif |
afa81a77 AF |
76 | return 0; |
77 | } | |
78 | ||
79 | #if defined(CONFIG_ENV_OFFSET_REDUND) | |
80 | int saveenv(void) | |
81 | { | |
82 | env_t env_new; | |
83 | char *saved_buffer = NULL, flag = OBSOLETE_FLAG; | |
0b2e5bbe | 84 | u32 saved_size, saved_offset, sector; |
afa81a77 AF |
85 | int ret; |
86 | ||
87 | ret = setup_flash_device(); | |
88 | if (ret) | |
89 | return ret; | |
7319bcaf | 90 | |
7ce1526e MV |
91 | ret = env_export(&env_new); |
92 | if (ret) | |
93 | return ret; | |
cd0f4fa1 | 94 | env_new.flags = ACTIVE_FLAG; |
ea882baf | 95 | |
203e94f6 | 96 | if (gd->env_valid == ENV_VALID) { |
a3110f01 SB |
97 | env_new_offset = CONFIG_ENV_OFFSET_REDUND; |
98 | env_offset = CONFIG_ENV_OFFSET; | |
99 | } else { | |
100 | env_new_offset = CONFIG_ENV_OFFSET; | |
101 | env_offset = CONFIG_ENV_OFFSET_REDUND; | |
102 | } | |
103 | ||
7319bcaf WW |
104 | /* Is the sector larger than the env (i.e. embedded) */ |
105 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { | |
106 | saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE; | |
107 | saved_offset = env_new_offset + CONFIG_ENV_SIZE; | |
7dd01744 | 108 | saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size); |
7319bcaf WW |
109 | if (!saved_buffer) { |
110 | ret = 1; | |
111 | goto done; | |
112 | } | |
113 | ret = spi_flash_read(env_flash, saved_offset, | |
114 | saved_size, saved_buffer); | |
115 | if (ret) | |
116 | goto done; | |
117 | } | |
118 | ||
0b2e5bbe | 119 | sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE); |
7319bcaf WW |
120 | |
121 | puts("Erasing SPI flash..."); | |
122 | ret = spi_flash_erase(env_flash, env_new_offset, | |
123 | sector * CONFIG_ENV_SECT_SIZE); | |
124 | if (ret) | |
125 | goto done; | |
126 | ||
127 | puts("Writing to SPI flash..."); | |
7319bcaf | 128 | |
a3110f01 | 129 | ret = spi_flash_write(env_flash, env_new_offset, |
cd0f4fa1 | 130 | CONFIG_ENV_SIZE, &env_new); |
7319bcaf WW |
131 | if (ret) |
132 | goto done; | |
133 | ||
134 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { | |
135 | ret = spi_flash_write(env_flash, saved_offset, | |
136 | saved_size, saved_buffer); | |
137 | if (ret) | |
138 | goto done; | |
139 | } | |
140 | ||
eb58a7fc | 141 | ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags), |
cd0f4fa1 | 142 | sizeof(env_new.flags), &flag); |
a3110f01 SB |
143 | if (ret) |
144 | goto done; | |
7319bcaf | 145 | |
7319bcaf WW |
146 | puts("done\n"); |
147 | ||
203e94f6 | 148 | gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND; |
a3110f01 | 149 | |
2dc55d9e | 150 | printf("Valid environment: %d\n", (int)gd->env_valid); |
a3110f01 | 151 | |
7319bcaf WW |
152 | done: |
153 | if (saved_buffer) | |
154 | free(saved_buffer); | |
eb58a7fc | 155 | |
7319bcaf WW |
156 | return ret; |
157 | } | |
158 | ||
159 | void env_relocate_spec(void) | |
160 | { | |
161 | int ret; | |
162 | int crc1_ok = 0, crc2_ok = 0; | |
163 | env_t *tmp_env1 = NULL; | |
164 | env_t *tmp_env2 = NULL; | |
a3110f01 | 165 | env_t *ep = NULL; |
7319bcaf | 166 | |
7dd01744 RB |
167 | tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN, |
168 | CONFIG_ENV_SIZE); | |
169 | tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN, | |
170 | CONFIG_ENV_SIZE); | |
ea882baf | 171 | if (!tmp_env1 || !tmp_env2) { |
ea882baf | 172 | set_default_env("!malloc() failed"); |
2dc55d9e | 173 | goto out; |
7319bcaf WW |
174 | } |
175 | ||
8fee8845 AF |
176 | ret = setup_flash_device(); |
177 | if (ret) | |
2dc55d9e | 178 | goto out; |
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) { |
203e94f6 | 201 | gd->env_valid = ENV_VALID; |
7319bcaf | 202 | } else if (!crc1_ok && crc2_ok) { |
203e94f6 | 203 | gd->env_valid = ENV_REDUND; |
2dc55d9e | 204 | } else if (tmp_env1->flags == ACTIVE_FLAG && |
205 | tmp_env2->flags == OBSOLETE_FLAG) { | |
203e94f6 | 206 | gd->env_valid = ENV_VALID; |
2dc55d9e | 207 | } else if (tmp_env1->flags == OBSOLETE_FLAG && |
208 | tmp_env2->flags == ACTIVE_FLAG) { | |
203e94f6 | 209 | gd->env_valid = ENV_REDUND; |
2dc55d9e | 210 | } else if (tmp_env1->flags == tmp_env2->flags) { |
203e94f6 | 211 | gd->env_valid = ENV_VALID; |
2dc55d9e | 212 | } else if (tmp_env1->flags == 0xFF) { |
203e94f6 | 213 | gd->env_valid = ENV_VALID; |
5e8397dd | 214 | } else if (tmp_env2->flags == 0xFF) { |
203e94f6 | 215 | gd->env_valid = ENV_REDUND; |
7319bcaf WW |
216 | } else { |
217 | /* | |
218 | * this differs from code in env_flash.c, but I think a sane | |
219 | * default path is desirable. | |
220 | */ | |
203e94f6 | 221 | gd->env_valid = ENV_VALID; |
7319bcaf | 222 | } |
ea882baf | 223 | |
203e94f6 | 224 | if (gd->env_valid == ENV_VALID) |
a3110f01 SB |
225 | ep = tmp_env1; |
226 | else | |
227 | ep = tmp_env2; | |
228 | ||
229 | ret = env_import((char *)ep, 0); | |
230 | if (!ret) { | |
231 | error("Cannot import environment: errno = %d\n", errno); | |
9a5d993e | 232 | set_default_env("!env_import failed"); |
7319bcaf | 233 | } |
7319bcaf WW |
234 | |
235 | err_read: | |
236 | spi_flash_free(env_flash); | |
237 | env_flash = NULL; | |
7319bcaf | 238 | out: |
ea882baf WD |
239 | free(tmp_env1); |
240 | free(tmp_env2); | |
7319bcaf WW |
241 | } |
242 | #else | |
8c66497e HS |
243 | int saveenv(void) |
244 | { | |
0b2e5bbe | 245 | u32 saved_size, saved_offset, sector; |
7ce1526e | 246 | char *saved_buffer = NULL; |
eb58a7fc | 247 | int ret = 1; |
cd0f4fa1 | 248 | env_t env_new; |
19c31285 | 249 | |
afa81a77 AF |
250 | ret = setup_flash_device(); |
251 | if (ret) | |
252 | return ret; | |
8c66497e | 253 | |
5b3375ac MF |
254 | /* Is the sector larger than the env (i.e. embedded) */ |
255 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { | |
256 | saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE; | |
257 | saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE; | |
258 | saved_buffer = malloc(saved_size); | |
eb58a7fc | 259 | if (!saved_buffer) |
5b3375ac | 260 | goto done; |
eb58a7fc | 261 | |
a3110f01 SB |
262 | ret = spi_flash_read(env_flash, saved_offset, |
263 | saved_size, saved_buffer); | |
5b3375ac MF |
264 | if (ret) |
265 | goto done; | |
266 | } | |
267 | ||
7ce1526e MV |
268 | ret = env_export(&env_new); |
269 | if (ret) | |
a3110f01 | 270 | goto done; |
a3110f01 | 271 | |
0b2e5bbe AF |
272 | sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE); |
273 | ||
8c66497e | 274 | puts("Erasing SPI flash..."); |
a3110f01 SB |
275 | ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET, |
276 | sector * CONFIG_ENV_SECT_SIZE); | |
5b3375ac MF |
277 | if (ret) |
278 | goto done; | |
8c66497e HS |
279 | |
280 | puts("Writing to SPI flash..."); | |
a3110f01 | 281 | ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, |
cd0f4fa1 | 282 | CONFIG_ENV_SIZE, &env_new); |
5b3375ac MF |
283 | if (ret) |
284 | goto done; | |
8c66497e | 285 | |
5b3375ac | 286 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
a3110f01 SB |
287 | ret = spi_flash_write(env_flash, saved_offset, |
288 | saved_size, saved_buffer); | |
5b3375ac MF |
289 | if (ret) |
290 | goto done; | |
291 | } | |
292 | ||
293 | ret = 0; | |
8c66497e | 294 | puts("done\n"); |
5b3375ac MF |
295 | |
296 | done: | |
297 | if (saved_buffer) | |
298 | free(saved_buffer); | |
eb58a7fc | 299 | |
5b3375ac | 300 | return ret; |
8c66497e HS |
301 | } |
302 | ||
303 | void env_relocate_spec(void) | |
304 | { | |
305 | int ret; | |
5a89fa92 | 306 | char *buf = NULL; |
8c66497e | 307 | |
7dd01744 | 308 | buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE); |
c041c60c AF |
309 | if (!buf) { |
310 | set_default_env("!malloc() failed"); | |
ea882baf WD |
311 | return; |
312 | } | |
8c66497e | 313 | |
c041c60c AF |
314 | ret = setup_flash_device(); |
315 | if (ret) | |
316 | goto out; | |
317 | ||
ea882baf WD |
318 | ret = spi_flash_read(env_flash, |
319 | CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf); | |
320 | if (ret) { | |
321 | set_default_env("!spi_flash_read() failed"); | |
c041c60c | 322 | goto err_read; |
ea882baf | 323 | } |
8c66497e | 324 | |
ea882baf | 325 | ret = env_import(buf, 1); |
ea882baf | 326 | if (ret) |
203e94f6 | 327 | gd->env_valid = ENV_VALID; |
c041c60c AF |
328 | |
329 | err_read: | |
8c66497e HS |
330 | spi_flash_free(env_flash); |
331 | env_flash = NULL; | |
c041c60c AF |
332 | out: |
333 | free(buf); | |
8c66497e | 334 | } |
7319bcaf | 335 | #endif |
8c66497e HS |
336 | |
337 | int env_init(void) | |
338 | { | |
339 | /* SPI flash isn't usable before relocation */ | |
340 | gd->env_addr = (ulong)&default_environment[0]; | |
203e94f6 | 341 | gd->env_valid = ENV_VALID; |
8c66497e HS |
342 | |
343 | return 0; | |
344 | } |