]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
8c66497e | 2 | /* |
ea882baf | 3 | * (C) Copyright 2000-2010 |
8c66497e HS |
4 | * Wolfgang Denk, DENX Software Engineering, [email protected]. |
5 | * | |
6 | * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com> | |
7 | * Andreas Heppel <[email protected]> | |
8 | * | |
9 | * (C) Copyright 2008 Atmel Corporation | |
8c66497e HS |
10 | */ |
11 | #include <common.h> | |
9d922450 | 12 | #include <dm.h> |
0ac7d722 | 13 | #include <env.h> |
f3998fdc | 14 | #include <env_internal.h> |
b79fdc76 | 15 | #include <flash.h> |
5b3375ac | 16 | #include <malloc.h> |
843c9e87 | 17 | #include <spi.h> |
8c66497e | 18 | #include <spi_flash.h> |
ea882baf WD |
19 | #include <search.h> |
20 | #include <errno.h> | |
b79fdc76 | 21 | #include <uuid.h> |
90526e9f | 22 | #include <asm/cache.h> |
19c31285 | 23 | #include <dm/device-internal.h> |
3db71108 | 24 | #include <u-boot/crc.h> |
8c66497e | 25 | |
4415f1d1 | 26 | #ifndef CONFIG_SPL_BUILD |
b500c92b | 27 | #define INITENV |
4415f1d1 SG |
28 | #endif |
29 | ||
7319bcaf | 30 | #ifdef CONFIG_ENV_OFFSET_REDUND |
eb58a7fc IG |
31 | static ulong env_offset = CONFIG_ENV_OFFSET; |
32 | static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND; | |
a3110f01 | 33 | #endif /* CONFIG_ENV_OFFSET_REDUND */ |
7319bcaf | 34 | |
8c66497e HS |
35 | DECLARE_GLOBAL_DATA_PTR; |
36 | ||
8c66497e HS |
37 | static struct spi_flash *env_flash; |
38 | ||
afa81a77 | 39 | static int setup_flash_device(void) |
7319bcaf | 40 | { |
56c40460 | 41 | #if CONFIG_IS_ENABLED(DM_SPI_FLASH) |
19c31285 | 42 | struct udevice *new; |
afa81a77 | 43 | int ret; |
19c31285 | 44 | |
96907c0f | 45 | /* speed and mode will be read from DT */ |
19c31285 | 46 | ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS, |
25a17652 KP |
47 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE, |
48 | &new); | |
19c31285 | 49 | if (ret) { |
0ac7d722 | 50 | env_set_default("spi_flash_probe_bus_cs() failed", 0); |
c5951991 | 51 | return ret; |
19c31285 GQ |
52 | } |
53 | ||
54 | env_flash = dev_get_uclass_priv(new); | |
55 | #else | |
c8cbf170 JT |
56 | if (env_flash) |
57 | spi_flash_free(env_flash); | |
7319bcaf | 58 | |
c8cbf170 JT |
59 | env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS, |
60 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); | |
7319bcaf | 61 | if (!env_flash) { |
c8cbf170 JT |
62 | env_set_default("spi_flash_probe() failed", 0); |
63 | return -EIO; | |
7319bcaf | 64 | } |
19c31285 | 65 | #endif |
afa81a77 AF |
66 | return 0; |
67 | } | |
68 | ||
69 | #if defined(CONFIG_ENV_OFFSET_REDUND) | |
e5bce247 | 70 | static int env_sf_save(void) |
afa81a77 AF |
71 | { |
72 | env_t env_new; | |
d3716dd6 | 73 | char *saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE; |
0b2e5bbe | 74 | u32 saved_size, saved_offset, sector; |
afa81a77 AF |
75 | int ret; |
76 | ||
77 | ret = setup_flash_device(); | |
78 | if (ret) | |
79 | return ret; | |
7319bcaf | 80 | |
7ce1526e MV |
81 | ret = env_export(&env_new); |
82 | if (ret) | |
c5951991 | 83 | return -EIO; |
d3716dd6 | 84 | env_new.flags = ENV_REDUND_ACTIVE; |
ea882baf | 85 | |
203e94f6 | 86 | if (gd->env_valid == ENV_VALID) { |
a3110f01 SB |
87 | env_new_offset = CONFIG_ENV_OFFSET_REDUND; |
88 | env_offset = CONFIG_ENV_OFFSET; | |
89 | } else { | |
90 | env_new_offset = CONFIG_ENV_OFFSET; | |
91 | env_offset = CONFIG_ENV_OFFSET_REDUND; | |
92 | } | |
93 | ||
7319bcaf WW |
94 | /* Is the sector larger than the env (i.e. embedded) */ |
95 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { | |
96 | saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE; | |
97 | saved_offset = env_new_offset + CONFIG_ENV_SIZE; | |
7dd01744 | 98 | saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size); |
7319bcaf | 99 | if (!saved_buffer) { |
c5951991 | 100 | ret = -ENOMEM; |
7319bcaf WW |
101 | goto done; |
102 | } | |
9ba5e5bc HS |
103 | ret = spi_flash_read(env_flash, saved_offset, |
104 | saved_size, saved_buffer); | |
7319bcaf WW |
105 | if (ret) |
106 | goto done; | |
107 | } | |
108 | ||
0b2e5bbe | 109 | sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE); |
7319bcaf WW |
110 | |
111 | puts("Erasing SPI flash..."); | |
112 | ret = spi_flash_erase(env_flash, env_new_offset, | |
113 | sector * CONFIG_ENV_SECT_SIZE); | |
114 | if (ret) | |
115 | goto done; | |
116 | ||
117 | puts("Writing to SPI flash..."); | |
7319bcaf | 118 | |
a3110f01 | 119 | ret = spi_flash_write(env_flash, env_new_offset, |
cd0f4fa1 | 120 | CONFIG_ENV_SIZE, &env_new); |
7319bcaf WW |
121 | if (ret) |
122 | goto done; | |
123 | ||
124 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { | |
125 | ret = spi_flash_write(env_flash, saved_offset, | |
126 | saved_size, saved_buffer); | |
127 | if (ret) | |
128 | goto done; | |
129 | } | |
130 | ||
eb58a7fc | 131 | ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags), |
cd0f4fa1 | 132 | sizeof(env_new.flags), &flag); |
a3110f01 SB |
133 | if (ret) |
134 | goto done; | |
7319bcaf | 135 | |
7319bcaf WW |
136 | puts("done\n"); |
137 | ||
203e94f6 | 138 | gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND; |
a3110f01 | 139 | |
2dc55d9e | 140 | printf("Valid environment: %d\n", (int)gd->env_valid); |
a3110f01 | 141 | |
7319bcaf WW |
142 | done: |
143 | if (saved_buffer) | |
144 | free(saved_buffer); | |
eb58a7fc | 145 | |
7319bcaf WW |
146 | return ret; |
147 | } | |
148 | ||
c5951991 | 149 | static int env_sf_load(void) |
7319bcaf WW |
150 | { |
151 | int ret; | |
80719938 SG |
152 | int read1_fail, read2_fail; |
153 | env_t *tmp_env1, *tmp_env2; | |
7319bcaf | 154 | |
7dd01744 RB |
155 | tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN, |
156 | CONFIG_ENV_SIZE); | |
157 | tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN, | |
158 | CONFIG_ENV_SIZE); | |
ea882baf | 159 | if (!tmp_env1 || !tmp_env2) { |
0ac7d722 | 160 | env_set_default("malloc() failed", 0); |
c5951991 | 161 | ret = -EIO; |
2dc55d9e | 162 | goto out; |
7319bcaf WW |
163 | } |
164 | ||
8fee8845 AF |
165 | ret = setup_flash_device(); |
166 | if (ret) | |
2dc55d9e | 167 | goto out; |
7319bcaf | 168 | |
9ba5e5bc HS |
169 | read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET, |
170 | CONFIG_ENV_SIZE, tmp_env1); | |
171 | read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND, | |
172 | CONFIG_ENV_SIZE, tmp_env2); | |
ea882baf | 173 | |
80719938 | 174 | ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2, |
890feeca | 175 | read2_fail, H_EXTERNAL); |
7319bcaf | 176 | |
7319bcaf WW |
177 | spi_flash_free(env_flash); |
178 | env_flash = NULL; | |
7319bcaf | 179 | out: |
ea882baf WD |
180 | free(tmp_env1); |
181 | free(tmp_env2); | |
c5951991 SG |
182 | |
183 | return ret; | |
7319bcaf WW |
184 | } |
185 | #else | |
e5bce247 | 186 | static int env_sf_save(void) |
8c66497e | 187 | { |
0b2e5bbe | 188 | u32 saved_size, saved_offset, sector; |
7ce1526e | 189 | char *saved_buffer = NULL; |
eb58a7fc | 190 | int ret = 1; |
cd0f4fa1 | 191 | env_t env_new; |
19c31285 | 192 | |
afa81a77 AF |
193 | ret = setup_flash_device(); |
194 | if (ret) | |
195 | return ret; | |
8c66497e | 196 | |
5b3375ac MF |
197 | /* Is the sector larger than the env (i.e. embedded) */ |
198 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { | |
199 | saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE; | |
200 | saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE; | |
201 | saved_buffer = malloc(saved_size); | |
eb58a7fc | 202 | if (!saved_buffer) |
5b3375ac | 203 | goto done; |
eb58a7fc | 204 | |
9ba5e5bc HS |
205 | ret = spi_flash_read(env_flash, saved_offset, |
206 | saved_size, saved_buffer); | |
5b3375ac MF |
207 | if (ret) |
208 | goto done; | |
209 | } | |
210 | ||
7ce1526e MV |
211 | ret = env_export(&env_new); |
212 | if (ret) | |
a3110f01 | 213 | goto done; |
a3110f01 | 214 | |
0b2e5bbe AF |
215 | sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE); |
216 | ||
8c66497e | 217 | puts("Erasing SPI flash..."); |
a3110f01 SB |
218 | ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET, |
219 | sector * CONFIG_ENV_SECT_SIZE); | |
5b3375ac MF |
220 | if (ret) |
221 | goto done; | |
8c66497e HS |
222 | |
223 | puts("Writing to SPI flash..."); | |
a3110f01 | 224 | ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, |
cd0f4fa1 | 225 | CONFIG_ENV_SIZE, &env_new); |
5b3375ac MF |
226 | if (ret) |
227 | goto done; | |
8c66497e | 228 | |
5b3375ac | 229 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
a3110f01 SB |
230 | ret = spi_flash_write(env_flash, saved_offset, |
231 | saved_size, saved_buffer); | |
5b3375ac MF |
232 | if (ret) |
233 | goto done; | |
234 | } | |
235 | ||
236 | ret = 0; | |
8c66497e | 237 | puts("done\n"); |
5b3375ac MF |
238 | |
239 | done: | |
240 | if (saved_buffer) | |
241 | free(saved_buffer); | |
eb58a7fc | 242 | |
5b3375ac | 243 | return ret; |
8c66497e HS |
244 | } |
245 | ||
c5951991 | 246 | static int env_sf_load(void) |
8c66497e HS |
247 | { |
248 | int ret; | |
5a89fa92 | 249 | char *buf = NULL; |
8c66497e | 250 | |
7dd01744 | 251 | buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE); |
c041c60c | 252 | if (!buf) { |
0ac7d722 | 253 | env_set_default("malloc() failed", 0); |
c5951991 | 254 | return -EIO; |
ea882baf | 255 | } |
8c66497e | 256 | |
c041c60c AF |
257 | ret = setup_flash_device(); |
258 | if (ret) | |
259 | goto out; | |
260 | ||
9ba5e5bc HS |
261 | ret = spi_flash_read(env_flash, |
262 | CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf); | |
ea882baf | 263 | if (ret) { |
0ac7d722 | 264 | env_set_default("spi_flash_read() failed", 0); |
c041c60c | 265 | goto err_read; |
ea882baf | 266 | } |
8c66497e | 267 | |
890feeca | 268 | ret = env_import(buf, 1, H_EXTERNAL); |
42a1820b | 269 | if (!ret) |
203e94f6 | 270 | gd->env_valid = ENV_VALID; |
c041c60c AF |
271 | |
272 | err_read: | |
8c66497e HS |
273 | spi_flash_free(env_flash); |
274 | env_flash = NULL; | |
c041c60c AF |
275 | out: |
276 | free(buf); | |
c5951991 SG |
277 | |
278 | return ret; | |
8c66497e | 279 | } |
7319bcaf | 280 | #endif |
8c66497e | 281 | |
a09fea1d | 282 | #if CONFIG_ENV_ADDR != 0x0 |
119c01c2 RB |
283 | __weak void *env_sf_get_env_addr(void) |
284 | { | |
285 | return (void *)CONFIG_ENV_ADDR; | |
286 | } | |
287 | #endif | |
288 | ||
a09fea1d | 289 | #if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0) |
b500c92b AK |
290 | static int env_sf_init(void) |
291 | { | |
119c01c2 | 292 | env_t *env_ptr = (env_t *)env_sf_get_env_addr(); |
b500c92b AK |
293 | |
294 | if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) { | |
295 | gd->env_addr = (ulong)&(env_ptr->data); | |
296 | gd->env_valid = 1; | |
297 | } else { | |
298 | gd->env_addr = (ulong)&default_environment[0]; | |
299 | gd->env_valid = 1; | |
300 | } | |
301 | ||
302 | return 0; | |
303 | } | |
304 | #endif | |
305 | ||
4415f1d1 SG |
306 | U_BOOT_ENV_LOCATION(sf) = { |
307 | .location = ENVL_SPI_FLASH, | |
22140d16 | 308 | ENV_NAME("SPIFlash") |
e5bce247 | 309 | .load = env_sf_load, |
6d3524c2 | 310 | .save = CONFIG_IS_ENABLED(SAVEENV) ? ENV_SAVE_PTR(env_sf_save) : NULL, |
a09fea1d | 311 | #if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0) |
b500c92b AK |
312 | .init = env_sf_init, |
313 | #endif | |
4415f1d1 | 314 | }; |