Merge https://gitlab.denx.de/u-boot/custodians/u-boot-marvell
[platform/kernel/u-boot.git] / env / sf.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2000-2010
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  *
6  * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7  * Andreas Heppel <aheppel@sysgo.de>
8  *
9  * (C) Copyright 2008 Atmel Corporation
10  */
11 #include <common.h>
12 #include <dm.h>
13 #include <env.h>
14 #include <env_internal.h>
15 #include <malloc.h>
16 #include <spi.h>
17 #include <spi_flash.h>
18 #include <search.h>
19 #include <errno.h>
20 #include <dm/device-internal.h>
21
22 #ifndef CONFIG_SPL_BUILD
23 #define CMD_SAVEENV
24 #define INITENV
25 #endif
26
27 #ifdef CONFIG_ENV_OFFSET_REDUND
28 #ifdef CMD_SAVEENV
29 static ulong env_offset         = CONFIG_ENV_OFFSET;
30 static ulong env_new_offset     = CONFIG_ENV_OFFSET_REDUND;
31 #endif
32
33 #endif /* CONFIG_ENV_OFFSET_REDUND */
34
35 DECLARE_GLOBAL_DATA_PTR;
36
37 static struct spi_flash *env_flash;
38
39 static int setup_flash_device(void)
40 {
41 #ifdef CONFIG_DM_SPI_FLASH
42         struct udevice *new;
43         int     ret;
44
45         /* speed and mode will be read from DT */
46         ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
47                                      CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE,
48                                      &new);
49         if (ret) {
50                 env_set_default("spi_flash_probe_bus_cs() failed", 0);
51                 return ret;
52         }
53
54         env_flash = dev_get_uclass_priv(new);
55 #else
56
57         if (!env_flash) {
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) {
62                         env_set_default("spi_flash_probe() failed", 0);
63                         return -EIO;
64                 }
65         }
66 #endif
67         return 0;
68 }
69
70 #if defined(CONFIG_ENV_OFFSET_REDUND)
71 #ifdef CMD_SAVEENV
72 static int env_sf_save(void)
73 {
74         env_t   env_new;
75         char    *saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
76         u32     saved_size, saved_offset, sector;
77         int     ret;
78
79         ret = setup_flash_device();
80         if (ret)
81                 return ret;
82
83         ret = env_export(&env_new);
84         if (ret)
85                 return -EIO;
86         env_new.flags   = ENV_REDUND_ACTIVE;
87
88         if (gd->env_valid == ENV_VALID) {
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
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;
100                 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
101                 if (!saved_buffer) {
102                         ret = -ENOMEM;
103                         goto done;
104                 }
105                 ret = spi_flash_read(env_flash, saved_offset,
106                                         saved_size, saved_buffer);
107                 if (ret)
108                         goto done;
109         }
110
111         sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
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...");
120
121         ret = spi_flash_write(env_flash, env_new_offset,
122                 CONFIG_ENV_SIZE, &env_new);
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
133         ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
134                                 sizeof(env_new.flags), &flag);
135         if (ret)
136                 goto done;
137
138         puts("done\n");
139
140         gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
141
142         printf("Valid environment: %d\n", (int)gd->env_valid);
143
144  done:
145         if (saved_buffer)
146                 free(saved_buffer);
147
148         return ret;
149 }
150 #endif /* CMD_SAVEENV */
151
152 static int env_sf_load(void)
153 {
154         int ret;
155         int read1_fail, read2_fail;
156         env_t *tmp_env1, *tmp_env2;
157
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);
162         if (!tmp_env1 || !tmp_env2) {
163                 env_set_default("malloc() failed", 0);
164                 ret = -EIO;
165                 goto out;
166         }
167
168         ret = setup_flash_device();
169         if (ret)
170                 goto out;
171
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);
176
177         ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
178                                 read2_fail);
179
180         spi_flash_free(env_flash);
181         env_flash = NULL;
182 out:
183         free(tmp_env1);
184         free(tmp_env2);
185
186         return ret;
187 }
188 #else
189 #ifdef CMD_SAVEENV
190 static int env_sf_save(void)
191 {
192         u32     saved_size, saved_offset, sector;
193         char    *saved_buffer = NULL;
194         int     ret = 1;
195         env_t   env_new;
196
197         ret = setup_flash_device();
198         if (ret)
199                 return ret;
200
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);
206                 if (!saved_buffer)
207                         goto done;
208
209                 ret = spi_flash_read(env_flash, saved_offset,
210                         saved_size, saved_buffer);
211                 if (ret)
212                         goto done;
213         }
214
215         ret = env_export(&env_new);
216         if (ret)
217                 goto done;
218
219         sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
220
221         puts("Erasing SPI flash...");
222         ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
223                 sector * CONFIG_ENV_SECT_SIZE);
224         if (ret)
225                 goto done;
226
227         puts("Writing to SPI flash...");
228         ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
229                 CONFIG_ENV_SIZE, &env_new);
230         if (ret)
231                 goto done;
232
233         if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
234                 ret = spi_flash_write(env_flash, saved_offset,
235                         saved_size, saved_buffer);
236                 if (ret)
237                         goto done;
238         }
239
240         ret = 0;
241         puts("done\n");
242
243  done:
244         if (saved_buffer)
245                 free(saved_buffer);
246
247         return ret;
248 }
249 #endif /* CMD_SAVEENV */
250
251 static int env_sf_load(void)
252 {
253         int ret;
254         char *buf = NULL;
255
256         buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
257         if (!buf) {
258                 env_set_default("malloc() failed", 0);
259                 return -EIO;
260         }
261
262         ret = setup_flash_device();
263         if (ret)
264                 goto out;
265
266         ret = spi_flash_read(env_flash,
267                 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
268         if (ret) {
269                 env_set_default("spi_flash_read() failed", 0);
270                 goto err_read;
271         }
272
273         ret = env_import(buf, 1);
274         if (!ret)
275                 gd->env_valid = ENV_VALID;
276
277 err_read:
278         spi_flash_free(env_flash);
279         env_flash = NULL;
280 out:
281         free(buf);
282
283         return ret;
284 }
285 #endif
286
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
294 #if defined(INITENV) && defined(CONFIG_ENV_ADDR)
295 static int env_sf_init(void)
296 {
297         env_t *env_ptr = (env_t *)env_sf_get_env_addr();
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
311 U_BOOT_ENV_LOCATION(sf) = {
312         .location       = ENVL_SPI_FLASH,
313         ENV_NAME("SPI Flash")
314         .load           = env_sf_load,
315 #ifdef CMD_SAVEENV
316         .save           = env_save_ptr(env_sf_save),
317 #endif
318 #if defined(INITENV) && defined(CONFIG_ENV_ADDR)
319         .init           = env_sf_init,
320 #endif
321 };