]>
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> |
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 | |
4415f1d1 SG |
22 | #ifndef CONFIG_SPL_BUILD |
23 | #define CMD_SAVEENV | |
b500c92b | 24 | #define INITENV |
4415f1d1 SG |
25 | #endif |
26 | ||
7319bcaf | 27 | #ifdef CONFIG_ENV_OFFSET_REDUND |
4415f1d1 | 28 | #ifdef CMD_SAVEENV |
eb58a7fc IG |
29 | static ulong env_offset = CONFIG_ENV_OFFSET; |
30 | static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND; | |
4415f1d1 | 31 | #endif |
7319bcaf | 32 | |
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 | { |
19c31285 GQ |
41 | #ifdef CONFIG_DM_SPI_FLASH |
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 | |
7319bcaf WW |
56 | |
57 | if (!env_flash) { | |
a3110f01 SB |
58 | env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, |
59 | CONFIG_ENV_SPI_CS, | |
60 | CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE); | |
61 | if (!env_flash) { | |
0ac7d722 | 62 | env_set_default("spi_flash_probe() failed", 0); |
c5951991 | 63 | return -EIO; |
a3110f01 | 64 | } |
7319bcaf | 65 | } |
19c31285 | 66 | #endif |
afa81a77 AF |
67 | return 0; |
68 | } | |
69 | ||
70 | #if defined(CONFIG_ENV_OFFSET_REDUND) | |
4415f1d1 | 71 | #ifdef CMD_SAVEENV |
e5bce247 | 72 | static int env_sf_save(void) |
afa81a77 AF |
73 | { |
74 | env_t env_new; | |
d3716dd6 | 75 | char *saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE; |
0b2e5bbe | 76 | u32 saved_size, saved_offset, sector; |
afa81a77 AF |
77 | int ret; |
78 | ||
79 | ret = setup_flash_device(); | |
80 | if (ret) | |
81 | return ret; | |
7319bcaf | 82 | |
7ce1526e MV |
83 | ret = env_export(&env_new); |
84 | if (ret) | |
c5951991 | 85 | return -EIO; |
d3716dd6 | 86 | env_new.flags = ENV_REDUND_ACTIVE; |
ea882baf | 87 | |
203e94f6 | 88 | if (gd->env_valid == ENV_VALID) { |
a3110f01 SB |
89 | env_new_offset = CONFIG_ENV_OFFSET_REDUND; |
90 | env_offset = CONFIG_ENV_OFFSET; | |
91 | } else { | |
92 | env_new_offset = CONFIG_ENV_OFFSET; | |
93 | env_offset = CONFIG_ENV_OFFSET_REDUND; | |
94 | } | |
95 | ||
7319bcaf WW |
96 | /* Is the sector larger than the env (i.e. embedded) */ |
97 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { | |
98 | saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE; | |
99 | saved_offset = env_new_offset + CONFIG_ENV_SIZE; | |
7dd01744 | 100 | saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size); |
7319bcaf | 101 | if (!saved_buffer) { |
c5951991 | 102 | ret = -ENOMEM; |
7319bcaf WW |
103 | goto done; |
104 | } | |
9ba5e5bc HS |
105 | ret = spi_flash_read(env_flash, saved_offset, |
106 | saved_size, saved_buffer); | |
7319bcaf WW |
107 | if (ret) |
108 | goto done; | |
109 | } | |
110 | ||
0b2e5bbe | 111 | sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE); |
7319bcaf WW |
112 | |
113 | puts("Erasing SPI flash..."); | |
114 | ret = spi_flash_erase(env_flash, env_new_offset, | |
115 | sector * CONFIG_ENV_SECT_SIZE); | |
116 | if (ret) | |
117 | goto done; | |
118 | ||
119 | puts("Writing to SPI flash..."); | |
7319bcaf | 120 | |
a3110f01 | 121 | ret = spi_flash_write(env_flash, env_new_offset, |
cd0f4fa1 | 122 | CONFIG_ENV_SIZE, &env_new); |
7319bcaf WW |
123 | if (ret) |
124 | goto done; | |
125 | ||
126 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { | |
127 | ret = spi_flash_write(env_flash, saved_offset, | |
128 | saved_size, saved_buffer); | |
129 | if (ret) | |
130 | goto done; | |
131 | } | |
132 | ||
eb58a7fc | 133 | ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags), |
cd0f4fa1 | 134 | sizeof(env_new.flags), &flag); |
a3110f01 SB |
135 | if (ret) |
136 | goto done; | |
7319bcaf | 137 | |
7319bcaf WW |
138 | puts("done\n"); |
139 | ||
203e94f6 | 140 | gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND; |
a3110f01 | 141 | |
2dc55d9e | 142 | printf("Valid environment: %d\n", (int)gd->env_valid); |
a3110f01 | 143 | |
7319bcaf WW |
144 | done: |
145 | if (saved_buffer) | |
146 | free(saved_buffer); | |
eb58a7fc | 147 | |
7319bcaf WW |
148 | return ret; |
149 | } | |
4415f1d1 | 150 | #endif /* CMD_SAVEENV */ |
7319bcaf | 151 | |
c5951991 | 152 | static int env_sf_load(void) |
7319bcaf WW |
153 | { |
154 | int ret; | |
80719938 SG |
155 | int read1_fail, read2_fail; |
156 | env_t *tmp_env1, *tmp_env2; | |
7319bcaf | 157 | |
7dd01744 RB |
158 | tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN, |
159 | CONFIG_ENV_SIZE); | |
160 | tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN, | |
161 | CONFIG_ENV_SIZE); | |
ea882baf | 162 | if (!tmp_env1 || !tmp_env2) { |
0ac7d722 | 163 | env_set_default("malloc() failed", 0); |
c5951991 | 164 | ret = -EIO; |
2dc55d9e | 165 | goto out; |
7319bcaf WW |
166 | } |
167 | ||
8fee8845 AF |
168 | ret = setup_flash_device(); |
169 | if (ret) | |
2dc55d9e | 170 | goto out; |
7319bcaf | 171 | |
9ba5e5bc HS |
172 | read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET, |
173 | CONFIG_ENV_SIZE, tmp_env1); | |
174 | read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND, | |
175 | CONFIG_ENV_SIZE, tmp_env2); | |
ea882baf | 176 | |
80719938 SG |
177 | ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2, |
178 | read2_fail); | |
7319bcaf | 179 | |
7319bcaf WW |
180 | spi_flash_free(env_flash); |
181 | env_flash = NULL; | |
7319bcaf | 182 | out: |
ea882baf WD |
183 | free(tmp_env1); |
184 | free(tmp_env2); | |
c5951991 SG |
185 | |
186 | return ret; | |
7319bcaf WW |
187 | } |
188 | #else | |
4415f1d1 | 189 | #ifdef CMD_SAVEENV |
e5bce247 | 190 | static int env_sf_save(void) |
8c66497e | 191 | { |
0b2e5bbe | 192 | u32 saved_size, saved_offset, sector; |
7ce1526e | 193 | char *saved_buffer = NULL; |
eb58a7fc | 194 | int ret = 1; |
cd0f4fa1 | 195 | env_t env_new; |
19c31285 | 196 | |
afa81a77 AF |
197 | ret = setup_flash_device(); |
198 | if (ret) | |
199 | return ret; | |
8c66497e | 200 | |
5b3375ac MF |
201 | /* Is the sector larger than the env (i.e. embedded) */ |
202 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { | |
203 | saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE; | |
204 | saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE; | |
205 | saved_buffer = malloc(saved_size); | |
eb58a7fc | 206 | if (!saved_buffer) |
5b3375ac | 207 | goto done; |
eb58a7fc | 208 | |
9ba5e5bc HS |
209 | ret = spi_flash_read(env_flash, saved_offset, |
210 | saved_size, saved_buffer); | |
5b3375ac MF |
211 | if (ret) |
212 | goto done; | |
213 | } | |
214 | ||
7ce1526e MV |
215 | ret = env_export(&env_new); |
216 | if (ret) | |
a3110f01 | 217 | goto done; |
a3110f01 | 218 | |
0b2e5bbe AF |
219 | sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE); |
220 | ||
8c66497e | 221 | puts("Erasing SPI flash..."); |
a3110f01 SB |
222 | ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET, |
223 | sector * CONFIG_ENV_SECT_SIZE); | |
5b3375ac MF |
224 | if (ret) |
225 | goto done; | |
8c66497e HS |
226 | |
227 | puts("Writing to SPI flash..."); | |
a3110f01 | 228 | ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, |
cd0f4fa1 | 229 | CONFIG_ENV_SIZE, &env_new); |
5b3375ac MF |
230 | if (ret) |
231 | goto done; | |
8c66497e | 232 | |
5b3375ac | 233 | if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { |
a3110f01 SB |
234 | ret = spi_flash_write(env_flash, saved_offset, |
235 | saved_size, saved_buffer); | |
5b3375ac MF |
236 | if (ret) |
237 | goto done; | |
238 | } | |
239 | ||
240 | ret = 0; | |
8c66497e | 241 | puts("done\n"); |
5b3375ac MF |
242 | |
243 | done: | |
244 | if (saved_buffer) | |
245 | free(saved_buffer); | |
eb58a7fc | 246 | |
5b3375ac | 247 | return ret; |
8c66497e | 248 | } |
4415f1d1 | 249 | #endif /* CMD_SAVEENV */ |
8c66497e | 250 | |
c5951991 | 251 | static int env_sf_load(void) |
8c66497e HS |
252 | { |
253 | int ret; | |
5a89fa92 | 254 | char *buf = NULL; |
8c66497e | 255 | |
7dd01744 | 256 | buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE); |
c041c60c | 257 | if (!buf) { |
0ac7d722 | 258 | env_set_default("malloc() failed", 0); |
c5951991 | 259 | return -EIO; |
ea882baf | 260 | } |
8c66497e | 261 | |
c041c60c AF |
262 | ret = setup_flash_device(); |
263 | if (ret) | |
264 | goto out; | |
265 | ||
9ba5e5bc HS |
266 | ret = spi_flash_read(env_flash, |
267 | CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf); | |
ea882baf | 268 | if (ret) { |
0ac7d722 | 269 | env_set_default("spi_flash_read() failed", 0); |
c041c60c | 270 | goto err_read; |
ea882baf | 271 | } |
8c66497e | 272 | |
ea882baf | 273 | ret = env_import(buf, 1); |
42a1820b | 274 | if (!ret) |
203e94f6 | 275 | gd->env_valid = ENV_VALID; |
c041c60c AF |
276 | |
277 | err_read: | |
8c66497e HS |
278 | spi_flash_free(env_flash); |
279 | env_flash = NULL; | |
c041c60c AF |
280 | out: |
281 | free(buf); | |
c5951991 SG |
282 | |
283 | return ret; | |
8c66497e | 284 | } |
7319bcaf | 285 | #endif |
8c66497e | 286 | |
119c01c2 RB |
287 | #ifdef CONFIG_ENV_ADDR |
288 | __weak void *env_sf_get_env_addr(void) | |
289 | { | |
290 | return (void *)CONFIG_ENV_ADDR; | |
291 | } | |
292 | #endif | |
293 | ||
b500c92b AK |
294 | #if defined(INITENV) && defined(CONFIG_ENV_ADDR) |
295 | static int env_sf_init(void) | |
296 | { | |
119c01c2 | 297 | env_t *env_ptr = (env_t *)env_sf_get_env_addr(); |
b500c92b AK |
298 | |
299 | if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) { | |
300 | gd->env_addr = (ulong)&(env_ptr->data); | |
301 | gd->env_valid = 1; | |
302 | } else { | |
303 | gd->env_addr = (ulong)&default_environment[0]; | |
304 | gd->env_valid = 1; | |
305 | } | |
306 | ||
307 | return 0; | |
308 | } | |
309 | #endif | |
310 | ||
4415f1d1 SG |
311 | U_BOOT_ENV_LOCATION(sf) = { |
312 | .location = ENVL_SPI_FLASH, | |
ac358beb | 313 | ENV_NAME("SPI Flash") |
e5bce247 | 314 | .load = env_sf_load, |
4415f1d1 | 315 | #ifdef CMD_SAVEENV |
e5bce247 | 316 | .save = env_save_ptr(env_sf_save), |
4415f1d1 | 317 | #endif |
b500c92b AK |
318 | #if defined(INITENV) && defined(CONFIG_ENV_ADDR) |
319 | .init = env_sf_init, | |
320 | #endif | |
4415f1d1 | 321 | }; |