]> Git Repo - J-u-boot.git/blob - env/sf.c
Merge patch series "Add AM64x Support to PRUSS and PRU_RPROC driver"
[J-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, [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
10  */
11 #include <dm.h>
12 #include <env.h>
13 #include <env_internal.h>
14 #include <malloc.h>
15 #include <spi.h>
16 #include <spi_flash.h>
17 #include <search.h>
18 #include <errno.h>
19 #include <uuid.h>
20 #include <asm/cache.h>
21 #include <asm/global_data.h>
22 #include <dm/device-internal.h>
23 #include <u-boot/crc.h>
24
25 #define OFFSET_INVALID          (~(u32)0)
26
27 #ifdef CONFIG_ENV_OFFSET_REDUND
28 #define ENV_OFFSET_REDUND       CONFIG_ENV_OFFSET_REDUND
29
30 static ulong env_offset         = CONFIG_ENV_OFFSET;
31 static ulong env_new_offset     = CONFIG_ENV_OFFSET_REDUND;
32
33 #else
34
35 #define ENV_OFFSET_REDUND       OFFSET_INVALID
36
37 #endif /* CONFIG_ENV_OFFSET_REDUND */
38
39 DECLARE_GLOBAL_DATA_PTR;
40
41 static int setup_flash_device(struct spi_flash **env_flash)
42 {
43 #if CONFIG_IS_ENABLED(DM_SPI_FLASH)
44         struct udevice *new;
45         int     ret;
46
47         /* speed and mode will be read from DT */
48         ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
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         *env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
58                                      CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
59         if (!*env_flash) {
60                 env_set_default("spi_flash_probe() failed", 0);
61                 return -EIO;
62         }
63 #endif
64         return 0;
65 }
66
67 #if defined(CONFIG_ENV_OFFSET_REDUND)
68 static int env_sf_save(void)
69 {
70         env_t   env_new;
71         char    *saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
72         u32     saved_size = 0, saved_offset = 0, sector;
73         u32     sect_size = CONFIG_ENV_SECT_SIZE;
74         int     ret;
75         struct spi_flash *env_flash;
76
77         ret = setup_flash_device(&env_flash);
78         if (ret)
79                 return ret;
80
81         if (IS_ENABLED(CONFIG_ENV_SECT_SIZE_AUTO))
82                 sect_size = env_flash->mtd.erasesize;
83
84         ret = env_export(&env_new);
85         if (ret)
86                 return -EIO;
87         env_new.flags   = ENV_REDUND_ACTIVE;
88
89         if (gd->env_valid == ENV_VALID) {
90                 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
91                 env_offset = CONFIG_ENV_OFFSET;
92         } else {
93                 env_new_offset = CONFIG_ENV_OFFSET;
94                 env_offset = CONFIG_ENV_OFFSET_REDUND;
95         }
96
97         /* Is the sector larger than the env (i.e. embedded) */
98         if (sect_size > CONFIG_ENV_SIZE) {
99                 saved_size = sect_size - CONFIG_ENV_SIZE;
100                 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
101                 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
102                 if (!saved_buffer) {
103                         ret = -ENOMEM;
104                         goto done;
105                 }
106                 ret = spi_flash_read(env_flash, saved_offset,
107                                         saved_size, saved_buffer);
108                 if (ret)
109                         goto done;
110         }
111
112         sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size);
113
114         puts("Erasing SPI flash...");
115         ret = spi_flash_erase(env_flash, env_new_offset,
116                                 sector * sect_size);
117         if (ret)
118                 goto done;
119
120         puts("Writing to SPI flash...");
121
122         ret = spi_flash_write(env_flash, env_new_offset,
123                 CONFIG_ENV_SIZE, &env_new);
124         if (ret)
125                 goto done;
126
127         if (sect_size > CONFIG_ENV_SIZE) {
128                 ret = spi_flash_write(env_flash, saved_offset,
129                                         saved_size, saved_buffer);
130                 if (ret)
131                         goto done;
132         }
133
134         ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
135                                 sizeof(env_new.flags), &flag);
136         if (ret)
137                 goto done;
138
139         puts("done\n");
140
141         gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
142
143         printf("Valid environment: %d\n", (int)gd->env_valid);
144
145 done:
146         spi_flash_free(env_flash);
147
148         if (saved_buffer)
149                 free(saved_buffer);
150
151         return ret;
152 }
153
154 static int env_sf_load(void)
155 {
156         int ret;
157         int read1_fail, read2_fail;
158         env_t *tmp_env1, *tmp_env2;
159         struct spi_flash *env_flash;
160
161         tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
162                         CONFIG_ENV_SIZE);
163         tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
164                         CONFIG_ENV_SIZE);
165         if (!tmp_env1 || !tmp_env2) {
166                 env_set_default("malloc() failed", 0);
167                 ret = -EIO;
168                 goto out;
169         }
170
171         ret = setup_flash_device(&env_flash);
172         if (ret)
173                 goto out;
174
175         read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
176                                     CONFIG_ENV_SIZE, tmp_env1);
177         read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
178                                     CONFIG_ENV_SIZE, tmp_env2);
179
180         ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
181                                 read2_fail, H_EXTERNAL);
182
183         spi_flash_free(env_flash);
184 out:
185         free(tmp_env1);
186         free(tmp_env2);
187
188         return ret;
189 }
190 #else
191 static int env_sf_save(void)
192 {
193         u32     saved_size = 0, saved_offset = 0, sector;
194         u32     sect_size = CONFIG_ENV_SECT_SIZE;
195         char    *saved_buffer = NULL;
196         int     ret = 1;
197         env_t   env_new;
198         struct spi_flash *env_flash;
199
200         ret = setup_flash_device(&env_flash);
201         if (ret)
202                 return ret;
203
204         if (IS_ENABLED(CONFIG_ENV_SECT_SIZE_AUTO))
205                 sect_size = env_flash->mtd.erasesize;
206
207         /* Is the sector larger than the env (i.e. embedded) */
208         if (sect_size > CONFIG_ENV_SIZE) {
209                 saved_size = sect_size - CONFIG_ENV_SIZE;
210                 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
211                 saved_buffer = malloc(saved_size);
212                 if (!saved_buffer) {
213                         ret = -ENOMEM;
214                         goto done;
215                 }
216
217                 ret = spi_flash_read(env_flash, saved_offset,
218                         saved_size, saved_buffer);
219                 if (ret)
220                         goto done;
221         }
222
223         ret = env_export(&env_new);
224         if (ret)
225                 goto done;
226
227         sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size);
228
229         puts("Erasing SPI flash...");
230         ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
231                 sector * sect_size);
232         if (ret)
233                 goto done;
234
235         puts("Writing to SPI flash...");
236         ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
237                 CONFIG_ENV_SIZE, &env_new);
238         if (ret)
239                 goto done;
240
241         if (sect_size > CONFIG_ENV_SIZE) {
242                 ret = spi_flash_write(env_flash, saved_offset,
243                         saved_size, saved_buffer);
244                 if (ret)
245                         goto done;
246         }
247
248         ret = 0;
249         puts("done\n");
250
251 done:
252         spi_flash_free(env_flash);
253
254         if (saved_buffer)
255                 free(saved_buffer);
256
257         return ret;
258 }
259
260 static int env_sf_load(void)
261 {
262         int ret;
263         char *buf = NULL;
264         struct spi_flash *env_flash;
265
266         buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
267         if (!buf) {
268                 env_set_default("malloc() failed", 0);
269                 return -EIO;
270         }
271
272         ret = setup_flash_device(&env_flash);
273         if (ret)
274                 goto out;
275
276         ret = spi_flash_read(env_flash,
277                 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
278         if (ret) {
279                 env_set_default("spi_flash_read() failed", 0);
280                 goto err_read;
281         }
282
283         ret = env_import(buf, 1, H_EXTERNAL);
284         if (!ret)
285                 gd->env_valid = ENV_VALID;
286
287 err_read:
288         spi_flash_free(env_flash);
289 out:
290         free(buf);
291
292         return ret;
293 }
294 #endif
295
296 static int env_sf_erase(void)
297 {
298         int ret;
299         env_t env;
300         struct spi_flash *env_flash;
301
302         ret = setup_flash_device(&env_flash);
303         if (ret)
304                 return ret;
305
306         memset(&env, 0, sizeof(env_t));
307         ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, &env);
308         if (ret)
309                 goto done;
310
311         if (ENV_OFFSET_REDUND != OFFSET_INVALID)
312                 ret = spi_flash_write(env_flash, ENV_OFFSET_REDUND, CONFIG_ENV_SIZE, &env);
313
314 done:
315         spi_flash_free(env_flash);
316
317         return ret;
318 }
319
320 __weak void *env_sf_get_env_addr(void)
321 {
322 #ifndef CONFIG_SPL_BUILD
323         return (void *)CONFIG_ENV_ADDR;
324 #else
325         return NULL;
326 #endif
327 }
328
329 /*
330  * check if Environment on CONFIG_ENV_ADDR is valid.
331  */
332 static int env_sf_init_addr(void)
333 {
334         env_t *env_ptr = (env_t *)env_sf_get_env_addr();
335
336         if (!env_ptr)
337                 return -ENOENT;
338
339         if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
340                 gd->env_addr = (ulong)&(env_ptr->data);
341                 gd->env_valid = ENV_VALID;
342         } else {
343                 gd->env_valid = ENV_INVALID;
344         }
345
346         return 0;
347 }
348
349 #if defined(CONFIG_ENV_SPI_EARLY)
350 /*
351  * early load environment from SPI flash (before relocation)
352  * and check if it is valid.
353  */
354 static int env_sf_init_early(void)
355 {
356         int ret;
357         int read1_fail;
358         int read2_fail;
359         int crc1_ok;
360         env_t *tmp_env2 = NULL;
361         env_t *tmp_env1;
362         struct spi_flash *env_flash;
363
364         /*
365          * if malloc is not ready yet, we cannot use
366          * this part yet.
367          */
368         if (!gd->malloc_limit)
369                 return -ENOENT;
370
371         tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
372                         CONFIG_ENV_SIZE);
373         if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
374                 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
375                                              CONFIG_ENV_SIZE);
376
377         if (!tmp_env1 || !tmp_env2)
378                 goto out;
379
380         ret = setup_flash_device(&env_flash);
381         if (ret)
382                 goto out;
383
384         read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
385                                     CONFIG_ENV_SIZE, tmp_env1);
386
387         if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT)) {
388                 read2_fail = spi_flash_read(env_flash,
389                                             CONFIG_ENV_OFFSET_REDUND,
390                                             CONFIG_ENV_SIZE, tmp_env2);
391                 ret = env_check_redund((char *)tmp_env1, read1_fail,
392                                        (char *)tmp_env2, read2_fail);
393
394                 if (ret < 0)
395                         goto err_read;
396
397                 if (gd->env_valid == ENV_VALID)
398                         gd->env_addr = (unsigned long)&tmp_env1->data;
399                 else
400                         gd->env_addr = (unsigned long)&tmp_env2->data;
401         } else {
402                 if (read1_fail)
403                         goto err_read;
404
405                 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
406                                 tmp_env1->crc;
407                 if (!crc1_ok)
408                         goto err_read;
409
410                 /* if valid -> this is our env */
411                 gd->env_valid = ENV_VALID;
412                 gd->env_addr = (unsigned long)&tmp_env1->data;
413         }
414
415         spi_flash_free(env_flash);
416
417         return 0;
418 err_read:
419         spi_flash_free(env_flash);
420
421         free(tmp_env1);
422         if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
423                 free(tmp_env2);
424 out:
425         /* env is not valid. always return 0 */
426         gd->env_valid = ENV_INVALID;
427         return 0;
428 }
429 #endif
430
431 static int env_sf_init(void)
432 {
433         int ret = env_sf_init_addr();
434         if (ret != -ENOENT)
435                 return ret;
436 #ifdef CONFIG_ENV_SPI_EARLY
437         return env_sf_init_early();
438 #endif
439         /*
440          * return here -ENOENT, so env_init()
441          * can set the init bit and later if no
442          * other Environment storage is defined
443          * can set the default environment
444          */
445         return -ENOENT;
446 }
447
448 U_BOOT_ENV_LOCATION(sf) = {
449         .location       = ENVL_SPI_FLASH,
450         ENV_NAME("SPIFlash")
451         .load           = env_sf_load,
452         .save           = ENV_SAVE_PTR(env_sf_save),
453         .erase          = ENV_ERASE_PTR(env_sf_erase),
454         .init           = env_sf_init,
455 };
This page took 0.04543 seconds and 4 git commands to generate.