d9ed08a78e7301dac50b19164842b25fa7162b10
[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 <flash.h>
16 #include <malloc.h>
17 #include <spi.h>
18 #include <spi_flash.h>
19 #include <search.h>
20 #include <errno.h>
21 #include <uuid.h>
22 #include <asm/cache.h>
23 #include <asm/global_data.h>
24 #include <dm/device-internal.h>
25 #include <u-boot/crc.h>
26
27 #ifndef CONFIG_SPL_BUILD
28 #define INITENV
29 #endif
30
31 #ifdef CONFIG_ENV_OFFSET_REDUND
32 static ulong env_offset         = CONFIG_ENV_OFFSET;
33 static ulong env_new_offset     = CONFIG_ENV_OFFSET_REDUND;
34 #endif /* CONFIG_ENV_OFFSET_REDUND */
35
36 DECLARE_GLOBAL_DATA_PTR;
37
38 static struct spi_flash *env_flash;
39
40 static int setup_flash_device(void)
41 {
42 #if CONFIG_IS_ENABLED(DM_SPI_FLASH)
43         struct udevice *new;
44         int     ret;
45
46         /* speed and mode will be read from DT */
47         ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
48                                      CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE,
49                                      &new);
50         if (ret) {
51                 env_set_default("spi_flash_probe_bus_cs() failed", 0);
52                 return ret;
53         }
54
55         env_flash = dev_get_uclass_priv(new);
56 #else
57         if (env_flash)
58                 spi_flash_free(env_flash);
59
60         env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
61                                     CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
62         if (!env_flash) {
63                 env_set_default("spi_flash_probe() failed", 0);
64                 return -EIO;
65         }
66 #endif
67         return 0;
68 }
69
70 #if defined(CONFIG_ENV_OFFSET_REDUND)
71 static int env_sf_save(void)
72 {
73         env_t   env_new;
74         char    *saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
75         u32     saved_size, saved_offset, sector;
76         u32     sect_size = CONFIG_ENV_SECT_SIZE;
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 (sect_size > CONFIG_ENV_SIZE) {
98                 saved_size = 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, sect_size);
112
113         puts("Erasing SPI flash...");
114         ret = spi_flash_erase(env_flash, env_new_offset,
115                                 sector * 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 (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
151 static int env_sf_load(void)
152 {
153         int ret;
154         int read1_fail, read2_fail;
155         env_t *tmp_env1, *tmp_env2;
156
157         tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
158                         CONFIG_ENV_SIZE);
159         tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
160                         CONFIG_ENV_SIZE);
161         if (!tmp_env1 || !tmp_env2) {
162                 env_set_default("malloc() failed", 0);
163                 ret = -EIO;
164                 goto out;
165         }
166
167         ret = setup_flash_device();
168         if (ret)
169                 goto out;
170
171         read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
172                                     CONFIG_ENV_SIZE, tmp_env1);
173         read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
174                                     CONFIG_ENV_SIZE, tmp_env2);
175
176         ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
177                                 read2_fail, H_EXTERNAL);
178
179         spi_flash_free(env_flash);
180         env_flash = NULL;
181 out:
182         free(tmp_env1);
183         free(tmp_env2);
184
185         return ret;
186 }
187 #else
188 static int env_sf_save(void)
189 {
190         u32     saved_size, saved_offset, sector;
191         u32     sect_size = CONFIG_ENV_SECT_SIZE;
192         char    *saved_buffer = NULL;
193         int     ret = 1;
194         env_t   env_new;
195
196         ret = setup_flash_device();
197         if (ret)
198                 return ret;
199
200         /* Is the sector larger than the env (i.e. embedded) */
201         if (sect_size > CONFIG_ENV_SIZE) {
202                 saved_size = sect_size - CONFIG_ENV_SIZE;
203                 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
204                 saved_buffer = malloc(saved_size);
205                 if (!saved_buffer)
206                         goto done;
207
208                 ret = spi_flash_read(env_flash, saved_offset,
209                         saved_size, saved_buffer);
210                 if (ret)
211                         goto done;
212         }
213
214         ret = env_export(&env_new);
215         if (ret)
216                 goto done;
217
218         sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size);
219
220         puts("Erasing SPI flash...");
221         ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
222                 sector * sect_size);
223         if (ret)
224                 goto done;
225
226         puts("Writing to SPI flash...");
227         ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
228                 CONFIG_ENV_SIZE, &env_new);
229         if (ret)
230                 goto done;
231
232         if (sect_size > CONFIG_ENV_SIZE) {
233                 ret = spi_flash_write(env_flash, saved_offset,
234                         saved_size, saved_buffer);
235                 if (ret)
236                         goto done;
237         }
238
239         ret = 0;
240         puts("done\n");
241
242 done:
243         if (saved_buffer)
244                 free(saved_buffer);
245
246         return ret;
247 }
248
249 static int env_sf_load(void)
250 {
251         int ret;
252         char *buf = NULL;
253
254         buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
255         if (!buf) {
256                 env_set_default("malloc() failed", 0);
257                 return -EIO;
258         }
259
260         ret = setup_flash_device();
261         if (ret)
262                 goto out;
263
264         ret = spi_flash_read(env_flash,
265                 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
266         if (ret) {
267                 env_set_default("spi_flash_read() failed", 0);
268                 goto err_read;
269         }
270
271         ret = env_import(buf, 1, H_EXTERNAL);
272         if (!ret)
273                 gd->env_valid = ENV_VALID;
274
275 err_read:
276         spi_flash_free(env_flash);
277         env_flash = NULL;
278 out:
279         free(buf);
280
281         return ret;
282 }
283 #endif
284
285 #if CONFIG_ENV_ADDR != 0x0
286 __weak void *env_sf_get_env_addr(void)
287 {
288         return (void *)CONFIG_ENV_ADDR;
289 }
290 #endif
291
292 #if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0)
293 /*
294  * check if Environment on CONFIG_ENV_ADDR is valid.
295  */
296 static int env_sf_init_addr(void)
297 {
298         env_t *env_ptr = (env_t *)env_sf_get_env_addr();
299
300         if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
301                 gd->env_addr    = (ulong)&(env_ptr->data);
302                 gd->env_valid   = 1;
303         } else {
304                 gd->env_addr = (ulong)&default_environment[0];
305                 gd->env_valid = 1;
306         }
307
308         return 0;
309 }
310 #endif
311
312 #if defined(CONFIG_ENV_SPI_EARLY)
313 /*
314  * early load environment from SPI flash (before relocation)
315  * and check if it is valid.
316  */
317 static int env_sf_init_early(void)
318 {
319         int ret;
320         int read1_fail;
321         int read2_fail;
322         int crc1_ok;
323         env_t *tmp_env2 = NULL;
324         env_t *tmp_env1;
325
326         /*
327          * if malloc is not ready yet, we cannot use
328          * this part yet.
329          */
330         if (!gd->malloc_limit)
331                 return -ENOENT;
332
333         tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
334                         CONFIG_ENV_SIZE);
335         if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
336                 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
337                                              CONFIG_ENV_SIZE);
338
339         if (!tmp_env1 || !tmp_env2)
340                 goto out;
341
342         ret = setup_flash_device();
343         if (ret)
344                 goto out;
345
346         read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
347                                     CONFIG_ENV_SIZE, tmp_env1);
348
349         if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT)) {
350                 read2_fail = spi_flash_read(env_flash,
351                                             CONFIG_ENV_OFFSET_REDUND,
352                                             CONFIG_ENV_SIZE, tmp_env2);
353                 ret = env_check_redund((char *)tmp_env1, read1_fail,
354                                        (char *)tmp_env2, read2_fail);
355
356                 if (ret == -EIO || ret == -ENOMSG)
357                         goto err_read;
358
359                 if (gd->env_valid == ENV_VALID)
360                         gd->env_addr = (unsigned long)&tmp_env1->data;
361                 else
362                         gd->env_addr = (unsigned long)&tmp_env2->data;
363         } else {
364                 if (read1_fail)
365                         goto err_read;
366
367                 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
368                                 tmp_env1->crc;
369                 if (!crc1_ok)
370                         goto err_read;
371
372                 /* if valid -> this is our env */
373                 gd->env_valid = ENV_VALID;
374                 gd->env_addr = (unsigned long)&tmp_env1->data;
375         }
376
377         return 0;
378 err_read:
379         spi_flash_free(env_flash);
380         env_flash = NULL;
381         free(tmp_env1);
382         if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
383                 free(tmp_env2);
384 out:
385         /* env is not valid. always return 0 */
386         gd->env_valid = ENV_INVALID;
387         return 0;
388 }
389 #endif
390
391 static int env_sf_init(void)
392 {
393 #if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0)
394         return env_sf_init_addr();
395 #elif defined(CONFIG_ENV_SPI_EARLY)
396         return env_sf_init_early();
397 #endif
398         /*
399          * return here -ENOENT, so env_init()
400          * can set the init bit and later if no
401          * other Environment storage is defined
402          * can set the default environment
403          */
404         return -ENOENT;
405 }
406
407 U_BOOT_ENV_LOCATION(sf) = {
408         .location       = ENVL_SPI_FLASH,
409         ENV_NAME("SPIFlash")
410         .load           = env_sf_load,
411         .save           = CONFIG_IS_ENABLED(SAVEENV) ? ENV_SAVE_PTR(env_sf_save) : NULL,
412         .init           = env_sf_init,
413 };