]>
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 | ||
4415f1d1 SG |
35 | #ifndef CONFIG_SPL_BUILD |
36 | #define CMD_SAVEENV | |
b500c92b | 37 | #define INITENV |
4415f1d1 SG |
38 | #endif |
39 | ||
7319bcaf | 40 | #ifdef CONFIG_ENV_OFFSET_REDUND |
4415f1d1 | 41 | #ifdef CMD_SAVEENV |
eb58a7fc IG |
42 | static ulong env_offset = CONFIG_ENV_OFFSET; |
43 | static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND; | |
4415f1d1 | 44 | #endif |
7319bcaf | 45 | |
eb58a7fc IG |
46 | #define ACTIVE_FLAG 1 |
47 | #define OBSOLETE_FLAG 0 | |
a3110f01 | 48 | #endif /* CONFIG_ENV_OFFSET_REDUND */ |
7319bcaf | 49 | |
8c66497e HS |
50 | DECLARE_GLOBAL_DATA_PTR; |
51 | ||
8c66497e HS |
52 | static struct spi_flash *env_flash; |
53 | ||
afa81a77 | 54 | static int setup_flash_device(void) |
7319bcaf | 55 | { |
19c31285 GQ |
56 | #ifdef CONFIG_DM_SPI_FLASH |
57 | struct udevice *new; | |
afa81a77 | 58 | int ret; |
19c31285 | 59 | |
96907c0f | 60 | /* speed and mode will be read from DT */ |
19c31285 | 61 | ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS, |
96907c0f | 62 | 0, 0, &new); |
19c31285 GQ |
63 | if (ret) { |
64 | set_default_env("!spi_flash_probe_bus_cs() failed"); | |
c5951991 | 65 | return ret; |
19c31285 GQ |
66 | } |
67 | ||
68 | env_flash = dev_get_uclass_priv(new); | |
69 | #else | |
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"); | |
c5951991 | 77 | return -EIO; |
a3110f01 | 78 | } |
7319bcaf | 79 | } |
19c31285 | 80 | #endif |
afa81a77 AF |
81 | return 0; |
82 | } | |
83 | ||
84 | #if defined(CONFIG_ENV_OFFSET_REDUND) | |
4415f1d1 | 85 | #ifdef CMD_SAVEENV |
e5bce247 | 86 | static int env_sf_save(void) |
afa81a77 AF |
87 | { |
88 | env_t env_new; | |
89 | char *saved_buffer = NULL, flag = OBSOLETE_FLAG; | |
0b2e5bbe | 90 | u32 saved_size, saved_offset, sector; |
afa81a77 AF |
91 | int ret; |
92 | ||
93 | ret = setup_flash_device(); | |
94 | if (ret) | |
95 | return ret; | |
7319bcaf | 96 | |
7ce1526e MV |
97 | ret = env_export(&env_new); |
98 | if (ret) | |
c5951991 | 99 | return -EIO; |
cd0f4fa1 | 100 | env_new.flags = ACTIVE_FLAG; |
ea882baf | 101 | |
203e94f6 | 102 | if (gd->env_valid == ENV_VALID) { |
a3110f01 SB |
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; | |
7dd01744 | 114 | saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size); |
7319bcaf | 115 | if (!saved_buffer) { |
c5951991 | 116 | ret = -ENOMEM; |
7319bcaf WW |
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 | ||
0b2e5bbe | 125 | sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE); |
7319bcaf WW |
126 | |
127 | puts("Erasing SPI flash..."); | |
128 | ret = spi_flash_erase(env_flash, env_new_offset, | |
129 | sector * CONFIG_ENV_SECT_SIZE); | |
130 | if (ret) | |
131 | goto done; | |
132 | ||
133 | puts("Writing to SPI flash..."); | |
7319bcaf | 134 | |
a3110f01 | 135 | ret = spi_flash_write(env_flash, env_new_offset, |
cd0f4fa1 | 136 | CONFIG_ENV_SIZE, &env_new); |
7319bcaf WW |
137 | if (ret) |
138 | goto done; | |
139 | ||
140 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { | |
141 | ret = spi_flash_write(env_flash, saved_offset, | |
142 | saved_size, saved_buffer); | |
143 | if (ret) | |
144 | goto done; | |
145 | } | |
146 | ||
eb58a7fc | 147 | ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags), |
cd0f4fa1 | 148 | sizeof(env_new.flags), &flag); |
a3110f01 SB |
149 | if (ret) |
150 | goto done; | |
7319bcaf | 151 | |
7319bcaf WW |
152 | puts("done\n"); |
153 | ||
203e94f6 | 154 | gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND; |
a3110f01 | 155 | |
2dc55d9e | 156 | printf("Valid environment: %d\n", (int)gd->env_valid); |
a3110f01 | 157 | |
7319bcaf WW |
158 | done: |
159 | if (saved_buffer) | |
160 | free(saved_buffer); | |
eb58a7fc | 161 | |
7319bcaf WW |
162 | return ret; |
163 | } | |
4415f1d1 | 164 | #endif /* CMD_SAVEENV */ |
7319bcaf | 165 | |
c5951991 | 166 | static int env_sf_load(void) |
7319bcaf WW |
167 | { |
168 | int ret; | |
169 | int crc1_ok = 0, crc2_ok = 0; | |
170 | env_t *tmp_env1 = NULL; | |
171 | env_t *tmp_env2 = NULL; | |
a3110f01 | 172 | env_t *ep = NULL; |
7319bcaf | 173 | |
7dd01744 RB |
174 | tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN, |
175 | CONFIG_ENV_SIZE); | |
176 | tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN, | |
177 | CONFIG_ENV_SIZE); | |
ea882baf | 178 | if (!tmp_env1 || !tmp_env2) { |
ea882baf | 179 | set_default_env("!malloc() failed"); |
c5951991 | 180 | ret = -EIO; |
2dc55d9e | 181 | goto out; |
7319bcaf WW |
182 | } |
183 | ||
8fee8845 AF |
184 | ret = setup_flash_device(); |
185 | if (ret) | |
2dc55d9e | 186 | goto out; |
7319bcaf WW |
187 | |
188 | ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET, | |
189 | CONFIG_ENV_SIZE, tmp_env1); | |
ea882baf WD |
190 | if (ret) { |
191 | set_default_env("!spi_flash_read() failed"); | |
7319bcaf | 192 | goto err_read; |
ea882baf | 193 | } |
7319bcaf WW |
194 | |
195 | if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc) | |
196 | crc1_ok = 1; | |
7319bcaf WW |
197 | |
198 | ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND, | |
199 | CONFIG_ENV_SIZE, tmp_env2); | |
200 | if (!ret) { | |
201 | if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc) | |
202 | crc2_ok = 1; | |
7319bcaf WW |
203 | } |
204 | ||
a3110f01 | 205 | if (!crc1_ok && !crc2_ok) { |
a3110f01 | 206 | set_default_env("!bad CRC"); |
c5951991 | 207 | ret = -EIO; |
2dc55d9e | 208 | goto err_read; |
a3110f01 | 209 | } else if (crc1_ok && !crc2_ok) { |
203e94f6 | 210 | gd->env_valid = ENV_VALID; |
7319bcaf | 211 | } else if (!crc1_ok && crc2_ok) { |
203e94f6 | 212 | gd->env_valid = ENV_REDUND; |
2dc55d9e | 213 | } else if (tmp_env1->flags == ACTIVE_FLAG && |
214 | tmp_env2->flags == OBSOLETE_FLAG) { | |
203e94f6 | 215 | gd->env_valid = ENV_VALID; |
2dc55d9e | 216 | } else if (tmp_env1->flags == OBSOLETE_FLAG && |
217 | tmp_env2->flags == ACTIVE_FLAG) { | |
203e94f6 | 218 | gd->env_valid = ENV_REDUND; |
2dc55d9e | 219 | } else if (tmp_env1->flags == tmp_env2->flags) { |
203e94f6 | 220 | gd->env_valid = ENV_VALID; |
2dc55d9e | 221 | } else if (tmp_env1->flags == 0xFF) { |
203e94f6 | 222 | gd->env_valid = ENV_VALID; |
5e8397dd | 223 | } else if (tmp_env2->flags == 0xFF) { |
203e94f6 | 224 | gd->env_valid = ENV_REDUND; |
7319bcaf WW |
225 | } else { |
226 | /* | |
227 | * this differs from code in env_flash.c, but I think a sane | |
228 | * default path is desirable. | |
229 | */ | |
203e94f6 | 230 | gd->env_valid = ENV_VALID; |
7319bcaf | 231 | } |
ea882baf | 232 | |
203e94f6 | 233 | if (gd->env_valid == ENV_VALID) |
a3110f01 SB |
234 | ep = tmp_env1; |
235 | else | |
236 | ep = tmp_env2; | |
237 | ||
238 | ret = env_import((char *)ep, 0); | |
239 | if (!ret) { | |
9b643e31 | 240 | pr_err("Cannot import environment: errno = %d\n", errno); |
9a5d993e | 241 | set_default_env("!env_import failed"); |
7319bcaf | 242 | } |
7319bcaf WW |
243 | |
244 | err_read: | |
245 | spi_flash_free(env_flash); | |
246 | env_flash = NULL; | |
7319bcaf | 247 | out: |
ea882baf WD |
248 | free(tmp_env1); |
249 | free(tmp_env2); | |
c5951991 SG |
250 | |
251 | return ret; | |
7319bcaf WW |
252 | } |
253 | #else | |
4415f1d1 | 254 | #ifdef CMD_SAVEENV |
e5bce247 | 255 | static int env_sf_save(void) |
8c66497e | 256 | { |
0b2e5bbe | 257 | u32 saved_size, saved_offset, sector; |
7ce1526e | 258 | char *saved_buffer = NULL; |
eb58a7fc | 259 | int ret = 1; |
cd0f4fa1 | 260 | env_t env_new; |
19c31285 | 261 | |
afa81a77 AF |
262 | ret = setup_flash_device(); |
263 | if (ret) | |
264 | return ret; | |
8c66497e | 265 | |
5b3375ac MF |
266 | /* Is the sector larger than the env (i.e. embedded) */ |
267 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { | |
268 | saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE; | |
269 | saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE; | |
270 | saved_buffer = malloc(saved_size); | |
eb58a7fc | 271 | if (!saved_buffer) |
5b3375ac | 272 | goto done; |
eb58a7fc | 273 | |
a3110f01 SB |
274 | ret = spi_flash_read(env_flash, saved_offset, |
275 | saved_size, saved_buffer); | |
5b3375ac MF |
276 | if (ret) |
277 | goto done; | |
278 | } | |
279 | ||
7ce1526e MV |
280 | ret = env_export(&env_new); |
281 | if (ret) | |
a3110f01 | 282 | goto done; |
a3110f01 | 283 | |
0b2e5bbe AF |
284 | sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE); |
285 | ||
8c66497e | 286 | puts("Erasing SPI flash..."); |
a3110f01 SB |
287 | ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET, |
288 | sector * CONFIG_ENV_SECT_SIZE); | |
5b3375ac MF |
289 | if (ret) |
290 | goto done; | |
8c66497e HS |
291 | |
292 | puts("Writing to SPI flash..."); | |
a3110f01 | 293 | ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, |
cd0f4fa1 | 294 | CONFIG_ENV_SIZE, &env_new); |
5b3375ac MF |
295 | if (ret) |
296 | goto done; | |
8c66497e | 297 | |
5b3375ac | 298 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
a3110f01 SB |
299 | ret = spi_flash_write(env_flash, saved_offset, |
300 | saved_size, saved_buffer); | |
5b3375ac MF |
301 | if (ret) |
302 | goto done; | |
303 | } | |
304 | ||
305 | ret = 0; | |
8c66497e | 306 | puts("done\n"); |
5b3375ac MF |
307 | |
308 | done: | |
309 | if (saved_buffer) | |
310 | free(saved_buffer); | |
eb58a7fc | 311 | |
5b3375ac | 312 | return ret; |
8c66497e | 313 | } |
4415f1d1 | 314 | #endif /* CMD_SAVEENV */ |
8c66497e | 315 | |
c5951991 | 316 | static int env_sf_load(void) |
8c66497e HS |
317 | { |
318 | int ret; | |
5a89fa92 | 319 | char *buf = NULL; |
8c66497e | 320 | |
7dd01744 | 321 | buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE); |
c041c60c AF |
322 | if (!buf) { |
323 | set_default_env("!malloc() failed"); | |
c5951991 | 324 | return -EIO; |
ea882baf | 325 | } |
8c66497e | 326 | |
c041c60c AF |
327 | ret = setup_flash_device(); |
328 | if (ret) | |
329 | goto out; | |
330 | ||
ea882baf WD |
331 | ret = spi_flash_read(env_flash, |
332 | CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf); | |
333 | if (ret) { | |
334 | set_default_env("!spi_flash_read() failed"); | |
c041c60c | 335 | goto err_read; |
ea882baf | 336 | } |
8c66497e | 337 | |
ea882baf | 338 | ret = env_import(buf, 1); |
ea882baf | 339 | if (ret) |
203e94f6 | 340 | gd->env_valid = ENV_VALID; |
c041c60c AF |
341 | |
342 | err_read: | |
8c66497e HS |
343 | spi_flash_free(env_flash); |
344 | env_flash = NULL; | |
c041c60c AF |
345 | out: |
346 | free(buf); | |
c5951991 SG |
347 | |
348 | return ret; | |
8c66497e | 349 | } |
7319bcaf | 350 | #endif |
8c66497e | 351 | |
b500c92b AK |
352 | #if defined(INITENV) && defined(CONFIG_ENV_ADDR) |
353 | static int env_sf_init(void) | |
354 | { | |
355 | env_t *env_ptr = (env_t *)(CONFIG_ENV_ADDR); | |
356 | ||
357 | if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) { | |
358 | gd->env_addr = (ulong)&(env_ptr->data); | |
359 | gd->env_valid = 1; | |
360 | } else { | |
361 | gd->env_addr = (ulong)&default_environment[0]; | |
362 | gd->env_valid = 1; | |
363 | } | |
364 | ||
365 | return 0; | |
366 | } | |
367 | #endif | |
368 | ||
4415f1d1 SG |
369 | U_BOOT_ENV_LOCATION(sf) = { |
370 | .location = ENVL_SPI_FLASH, | |
ac358beb | 371 | ENV_NAME("SPI Flash") |
e5bce247 | 372 | .load = env_sf_load, |
4415f1d1 | 373 | #ifdef CMD_SAVEENV |
e5bce247 | 374 | .save = env_save_ptr(env_sf_save), |
4415f1d1 | 375 | #endif |
b500c92b AK |
376 | #if defined(INITENV) && defined(CONFIG_ENV_ADDR) |
377 | .init = env_sf_init, | |
378 | #endif | |
4415f1d1 | 379 | }; |