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>
17 #include <spi_flash.h>
21 #include <asm/cache.h>
22 #include <asm/global_data.h>
23 #include <dm/device-internal.h>
24 #include <u-boot/crc.h>
26 #define OFFSET_INVALID (~(u32)0)
28 #ifdef CONFIG_ENV_OFFSET_REDUND
29 #define ENV_OFFSET_REDUND CONFIG_ENV_OFFSET_REDUND
31 static ulong env_offset = CONFIG_ENV_OFFSET;
32 static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
36 #define ENV_OFFSET_REDUND OFFSET_INVALID
38 #endif /* CONFIG_ENV_OFFSET_REDUND */
40 DECLARE_GLOBAL_DATA_PTR;
42 static int setup_flash_device(struct spi_flash **env_flash)
44 #if CONFIG_IS_ENABLED(DM_SPI_FLASH)
48 /* speed and mode will be read from DT */
49 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
52 env_set_default("spi_flash_probe_bus_cs() failed", 0);
56 *env_flash = dev_get_uclass_priv(new);
58 *env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
59 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
61 env_set_default("spi_flash_probe() failed", 0);
68 #if defined(CONFIG_ENV_OFFSET_REDUND)
69 static int env_sf_save(void)
72 char *saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
73 u32 saved_size = 0, saved_offset = 0, sector;
74 u32 sect_size = CONFIG_ENV_SECT_SIZE;
76 struct spi_flash *env_flash;
78 ret = setup_flash_device(&env_flash);
82 if (IS_ENABLED(CONFIG_ENV_SECT_SIZE_AUTO))
83 sect_size = env_flash->mtd.erasesize;
85 ret = env_export(&env_new);
88 env_new.flags = ENV_REDUND_ACTIVE;
90 if (gd->env_valid == ENV_VALID) {
91 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
92 env_offset = CONFIG_ENV_OFFSET;
94 env_new_offset = CONFIG_ENV_OFFSET;
95 env_offset = CONFIG_ENV_OFFSET_REDUND;
98 /* Is the sector larger than the env (i.e. embedded) */
99 if (sect_size > CONFIG_ENV_SIZE) {
100 saved_size = sect_size - CONFIG_ENV_SIZE;
101 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
102 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
107 ret = spi_flash_read(env_flash, saved_offset,
108 saved_size, saved_buffer);
113 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size);
115 puts("Erasing SPI flash...");
116 ret = spi_flash_erase(env_flash, env_new_offset,
121 puts("Writing to SPI flash...");
123 ret = spi_flash_write(env_flash, env_new_offset,
124 CONFIG_ENV_SIZE, &env_new);
128 if (sect_size > CONFIG_ENV_SIZE) {
129 ret = spi_flash_write(env_flash, saved_offset,
130 saved_size, saved_buffer);
135 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
136 sizeof(env_new.flags), &flag);
142 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
144 printf("Valid environment: %d\n", (int)gd->env_valid);
147 spi_flash_free(env_flash);
155 static int env_sf_load(void)
158 int read1_fail, read2_fail;
159 env_t *tmp_env1, *tmp_env2;
160 struct spi_flash *env_flash;
162 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
164 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
166 if (!tmp_env1 || !tmp_env2) {
167 env_set_default("malloc() failed", 0);
172 ret = setup_flash_device(&env_flash);
176 read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
177 CONFIG_ENV_SIZE, tmp_env1);
178 read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
179 CONFIG_ENV_SIZE, tmp_env2);
181 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
182 read2_fail, H_EXTERNAL);
184 spi_flash_free(env_flash);
192 static int env_sf_save(void)
194 u32 saved_size = 0, saved_offset = 0, sector;
195 u32 sect_size = CONFIG_ENV_SECT_SIZE;
196 char *saved_buffer = NULL;
199 struct spi_flash *env_flash;
201 ret = setup_flash_device(&env_flash);
205 if (IS_ENABLED(CONFIG_ENV_SECT_SIZE_AUTO))
206 sect_size = env_flash->mtd.erasesize;
208 /* Is the sector larger than the env (i.e. embedded) */
209 if (sect_size > CONFIG_ENV_SIZE) {
210 saved_size = sect_size - CONFIG_ENV_SIZE;
211 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
212 saved_buffer = malloc(saved_size);
216 ret = spi_flash_read(env_flash, saved_offset,
217 saved_size, saved_buffer);
222 ret = env_export(&env_new);
226 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size);
228 puts("Erasing SPI flash...");
229 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
234 puts("Writing to SPI flash...");
235 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
236 CONFIG_ENV_SIZE, &env_new);
240 if (sect_size > CONFIG_ENV_SIZE) {
241 ret = spi_flash_write(env_flash, saved_offset,
242 saved_size, saved_buffer);
251 spi_flash_free(env_flash);
259 static int env_sf_load(void)
263 struct spi_flash *env_flash;
265 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
267 env_set_default("malloc() failed", 0);
271 ret = setup_flash_device(&env_flash);
275 ret = spi_flash_read(env_flash,
276 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
278 env_set_default("spi_flash_read() failed", 0);
282 ret = env_import(buf, 1, H_EXTERNAL);
284 gd->env_valid = ENV_VALID;
287 spi_flash_free(env_flash);
295 static int env_sf_erase(void)
299 struct spi_flash *env_flash;
301 ret = setup_flash_device(&env_flash);
305 memset(&env, 0, sizeof(env_t));
306 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, &env);
310 if (ENV_OFFSET_REDUND != OFFSET_INVALID)
311 ret = spi_flash_write(env_flash, ENV_OFFSET_REDUND, CONFIG_ENV_SIZE, &env);
314 spi_flash_free(env_flash);
319 __weak void *env_sf_get_env_addr(void)
321 #ifndef CONFIG_SPL_BUILD
322 return (void *)CONFIG_ENV_ADDR;
329 * check if Environment on CONFIG_ENV_ADDR is valid.
331 static int env_sf_init_addr(void)
333 env_t *env_ptr = (env_t *)env_sf_get_env_addr();
338 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
339 gd->env_addr = (ulong)&(env_ptr->data);
340 gd->env_valid = ENV_VALID;
342 gd->env_valid = ENV_INVALID;
348 #if defined(CONFIG_ENV_SPI_EARLY)
350 * early load environment from SPI flash (before relocation)
351 * and check if it is valid.
353 static int env_sf_init_early(void)
359 env_t *tmp_env2 = NULL;
361 struct spi_flash *env_flash;
364 * if malloc is not ready yet, we cannot use
367 if (!gd->malloc_limit)
370 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
372 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
373 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
376 if (!tmp_env1 || !tmp_env2)
379 ret = setup_flash_device(&env_flash);
383 read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
384 CONFIG_ENV_SIZE, tmp_env1);
386 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT)) {
387 read2_fail = spi_flash_read(env_flash,
388 CONFIG_ENV_OFFSET_REDUND,
389 CONFIG_ENV_SIZE, tmp_env2);
390 ret = env_check_redund((char *)tmp_env1, read1_fail,
391 (char *)tmp_env2, read2_fail);
396 if (gd->env_valid == ENV_VALID)
397 gd->env_addr = (unsigned long)&tmp_env1->data;
399 gd->env_addr = (unsigned long)&tmp_env2->data;
404 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
409 /* if valid -> this is our env */
410 gd->env_valid = ENV_VALID;
411 gd->env_addr = (unsigned long)&tmp_env1->data;
414 spi_flash_free(env_flash);
418 spi_flash_free(env_flash);
421 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
424 /* env is not valid. always return 0 */
425 gd->env_valid = ENV_INVALID;
430 static int env_sf_init(void)
432 int ret = env_sf_init_addr();
435 #ifdef CONFIG_ENV_SPI_EARLY
436 return env_sf_init_early();
439 * return here -ENOENT, so env_init()
440 * can set the init bit and later if no
441 * other Environment storage is defined
442 * can set the default environment
447 U_BOOT_ENV_LOCATION(sf) = {
448 .location = ENVL_SPI_FLASH,
451 .save = ENV_SAVE_PTR(env_sf_save),
452 .erase = ENV_ERASE_PTR(env_sf_erase),