1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2010
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 * Andreas Heppel <aheppel@sysgo.de>
9 * (C) Copyright 2008 Atmel Corporation
14 #include <env_internal.h>
18 #include <spi_flash.h>
22 #include <asm/cache.h>
23 #include <asm/global_data.h>
24 #include <dm/device-internal.h>
25 #include <u-boot/crc.h>
27 #define OFFSET_INVALID (~(u32)0)
29 #ifdef CONFIG_ENV_OFFSET_REDUND
30 #define ENV_OFFSET_REDUND CONFIG_ENV_OFFSET_REDUND
32 static ulong env_offset = CONFIG_ENV_OFFSET;
33 static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
37 #define ENV_OFFSET_REDUND OFFSET_INVALID
39 #endif /* CONFIG_ENV_OFFSET_REDUND */
41 DECLARE_GLOBAL_DATA_PTR;
43 static int setup_flash_device(struct spi_flash **env_flash)
45 #if CONFIG_IS_ENABLED(DM_SPI_FLASH)
49 /* speed and mode will be read from DT */
50 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
51 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE,
54 env_set_default("spi_flash_probe_bus_cs() failed", 0);
58 *env_flash = dev_get_uclass_priv(new);
60 *env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
61 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
63 env_set_default("spi_flash_probe() failed", 0);
70 #if defined(CONFIG_ENV_OFFSET_REDUND)
71 static int env_sf_save(void)
74 char *saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
75 u32 saved_size = 0, saved_offset = 0, sector;
76 u32 sect_size = CONFIG_ENV_SECT_SIZE;
78 struct spi_flash *env_flash;
80 ret = setup_flash_device(&env_flash);
84 if (IS_ENABLED(CONFIG_ENV_SECT_SIZE_AUTO))
85 sect_size = env_flash->mtd.erasesize;
87 ret = env_export(&env_new);
90 env_new.flags = ENV_REDUND_ACTIVE;
92 if (gd->env_valid == ENV_VALID) {
93 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
94 env_offset = CONFIG_ENV_OFFSET;
96 env_new_offset = CONFIG_ENV_OFFSET;
97 env_offset = CONFIG_ENV_OFFSET_REDUND;
100 /* Is the sector larger than the env (i.e. embedded) */
101 if (sect_size > CONFIG_ENV_SIZE) {
102 saved_size = sect_size - CONFIG_ENV_SIZE;
103 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
104 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
109 ret = spi_flash_read(env_flash, saved_offset,
110 saved_size, saved_buffer);
115 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size);
117 puts("Erasing SPI flash...");
118 ret = spi_flash_erase(env_flash, env_new_offset,
123 puts("Writing to SPI flash...");
125 ret = spi_flash_write(env_flash, env_new_offset,
126 CONFIG_ENV_SIZE, &env_new);
130 if (sect_size > CONFIG_ENV_SIZE) {
131 ret = spi_flash_write(env_flash, saved_offset,
132 saved_size, saved_buffer);
137 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
138 sizeof(env_new.flags), &flag);
144 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
146 printf("Valid environment: %d\n", (int)gd->env_valid);
149 spi_flash_free(env_flash);
157 static int env_sf_load(void)
160 int read1_fail, read2_fail;
161 env_t *tmp_env1, *tmp_env2;
162 struct spi_flash *env_flash;
164 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
166 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
168 if (!tmp_env1 || !tmp_env2) {
169 env_set_default("malloc() failed", 0);
174 ret = setup_flash_device(&env_flash);
178 read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
179 CONFIG_ENV_SIZE, tmp_env1);
180 read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
181 CONFIG_ENV_SIZE, tmp_env2);
183 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
184 read2_fail, H_EXTERNAL);
186 spi_flash_free(env_flash);
194 static int env_sf_save(void)
196 u32 saved_size = 0, saved_offset = 0, sector;
197 u32 sect_size = CONFIG_ENV_SECT_SIZE;
198 char *saved_buffer = NULL;
201 struct spi_flash *env_flash;
203 ret = setup_flash_device(&env_flash);
207 if (IS_ENABLED(CONFIG_ENV_SECT_SIZE_AUTO))
208 sect_size = env_flash->mtd.erasesize;
210 /* Is the sector larger than the env (i.e. embedded) */
211 if (sect_size > CONFIG_ENV_SIZE) {
212 saved_size = sect_size - CONFIG_ENV_SIZE;
213 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
214 saved_buffer = malloc(saved_size);
218 ret = spi_flash_read(env_flash, saved_offset,
219 saved_size, saved_buffer);
224 ret = env_export(&env_new);
228 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size);
230 puts("Erasing SPI flash...");
231 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
236 puts("Writing to SPI flash...");
237 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
238 CONFIG_ENV_SIZE, &env_new);
242 if (sect_size > CONFIG_ENV_SIZE) {
243 ret = spi_flash_write(env_flash, saved_offset,
244 saved_size, saved_buffer);
253 spi_flash_free(env_flash);
261 static int env_sf_load(void)
265 struct spi_flash *env_flash;
267 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
269 env_set_default("malloc() failed", 0);
273 ret = setup_flash_device(&env_flash);
277 ret = spi_flash_read(env_flash,
278 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
280 env_set_default("spi_flash_read() failed", 0);
284 ret = env_import(buf, 1, H_EXTERNAL);
286 gd->env_valid = ENV_VALID;
289 spi_flash_free(env_flash);
297 static int env_sf_erase(void)
301 struct spi_flash *env_flash;
303 ret = setup_flash_device(&env_flash);
307 memset(&env, 0, sizeof(env_t));
308 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, &env);
312 if (ENV_OFFSET_REDUND != OFFSET_INVALID)
313 ret = spi_flash_write(env_flash, ENV_OFFSET_REDUND, CONFIG_ENV_SIZE, &env);
316 spi_flash_free(env_flash);
321 __weak void *env_sf_get_env_addr(void)
323 #ifndef CONFIG_SPL_BUILD
324 return (void *)CONFIG_ENV_ADDR;
331 * check if Environment on CONFIG_ENV_ADDR is valid.
333 static int env_sf_init_addr(void)
335 env_t *env_ptr = (env_t *)env_sf_get_env_addr();
340 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
341 gd->env_addr = (ulong)&(env_ptr->data);
342 gd->env_valid = ENV_VALID;
344 gd->env_valid = ENV_INVALID;
350 #if defined(CONFIG_ENV_SPI_EARLY)
352 * early load environment from SPI flash (before relocation)
353 * and check if it is valid.
355 static int env_sf_init_early(void)
361 env_t *tmp_env2 = NULL;
363 struct spi_flash *env_flash;
366 * if malloc is not ready yet, we cannot use
369 if (!gd->malloc_limit)
372 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
374 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
375 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
378 if (!tmp_env1 || !tmp_env2)
381 ret = setup_flash_device(&env_flash);
385 read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
386 CONFIG_ENV_SIZE, tmp_env1);
388 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT)) {
389 read2_fail = spi_flash_read(env_flash,
390 CONFIG_ENV_OFFSET_REDUND,
391 CONFIG_ENV_SIZE, tmp_env2);
392 ret = env_check_redund((char *)tmp_env1, read1_fail,
393 (char *)tmp_env2, read2_fail);
398 if (gd->env_valid == ENV_VALID)
399 gd->env_addr = (unsigned long)&tmp_env1->data;
401 gd->env_addr = (unsigned long)&tmp_env2->data;
406 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
411 /* if valid -> this is our env */
412 gd->env_valid = ENV_VALID;
413 gd->env_addr = (unsigned long)&tmp_env1->data;
416 spi_flash_free(env_flash);
420 spi_flash_free(env_flash);
423 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
426 /* env is not valid. always return 0 */
427 gd->env_valid = ENV_INVALID;
432 static int env_sf_init(void)
434 int ret = env_sf_init_addr();
437 #ifdef CONFIG_ENV_SPI_EARLY
438 return env_sf_init_early();
441 * return here -ENOENT, so env_init()
442 * can set the init bit and later if no
443 * other Environment storage is defined
444 * can set the default environment
449 U_BOOT_ENV_LOCATION(sf) = {
450 .location = ENVL_SPI_FLASH,
453 .save = ENV_SAVE_PTR(env_sf_save),
454 .erase = ENV_ERASE_PTR(env_sf_erase),