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 #ifndef CONFIG_SPL_BUILD
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 */
36 DECLARE_GLOBAL_DATA_PTR;
38 static struct spi_flash *env_flash;
40 static int setup_flash_device(void)
42 #if CONFIG_IS_ENABLED(DM_SPI_FLASH)
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,
51 env_set_default("spi_flash_probe_bus_cs() failed", 0);
55 env_flash = dev_get_uclass_priv(new);
58 spi_flash_free(env_flash);
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, saved_offset, sector;
76 u32 sect_size = CONFIG_ENV_SECT_SIZE;
79 ret = setup_flash_device();
83 ret = env_export(&env_new);
86 env_new.flags = ENV_REDUND_ACTIVE;
88 if (gd->env_valid == ENV_VALID) {
89 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
90 env_offset = CONFIG_ENV_OFFSET;
92 env_new_offset = CONFIG_ENV_OFFSET;
93 env_offset = CONFIG_ENV_OFFSET_REDUND;
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);
105 ret = spi_flash_read(env_flash, saved_offset,
106 saved_size, saved_buffer);
111 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size);
113 puts("Erasing SPI flash...");
114 ret = spi_flash_erase(env_flash, env_new_offset,
119 puts("Writing to SPI flash...");
121 ret = spi_flash_write(env_flash, env_new_offset,
122 CONFIG_ENV_SIZE, &env_new);
126 if (sect_size > CONFIG_ENV_SIZE) {
127 ret = spi_flash_write(env_flash, saved_offset,
128 saved_size, saved_buffer);
133 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
134 sizeof(env_new.flags), &flag);
140 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
142 printf("Valid environment: %d\n", (int)gd->env_valid);
151 static int env_sf_load(void)
154 int read1_fail, read2_fail;
155 env_t *tmp_env1, *tmp_env2;
157 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
159 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
161 if (!tmp_env1 || !tmp_env2) {
162 env_set_default("malloc() failed", 0);
167 ret = setup_flash_device();
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);
176 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
177 read2_fail, H_EXTERNAL);
179 spi_flash_free(env_flash);
188 static int env_sf_save(void)
190 u32 saved_size, saved_offset, sector;
191 u32 sect_size = CONFIG_ENV_SECT_SIZE;
192 char *saved_buffer = NULL;
196 ret = setup_flash_device();
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);
208 ret = spi_flash_read(env_flash, saved_offset,
209 saved_size, saved_buffer);
214 ret = env_export(&env_new);
218 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size);
220 puts("Erasing SPI flash...");
221 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
226 puts("Writing to SPI flash...");
227 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
228 CONFIG_ENV_SIZE, &env_new);
232 if (sect_size > CONFIG_ENV_SIZE) {
233 ret = spi_flash_write(env_flash, saved_offset,
234 saved_size, saved_buffer);
249 static int env_sf_load(void)
254 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
256 env_set_default("malloc() failed", 0);
260 ret = setup_flash_device();
264 ret = spi_flash_read(env_flash,
265 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
267 env_set_default("spi_flash_read() failed", 0);
271 ret = env_import(buf, 1, H_EXTERNAL);
273 gd->env_valid = ENV_VALID;
276 spi_flash_free(env_flash);
285 #if CONFIG_ENV_ADDR != 0x0
286 __weak void *env_sf_get_env_addr(void)
288 return (void *)CONFIG_ENV_ADDR;
292 #if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0)
294 * check if Environment on CONFIG_ENV_ADDR is valid.
296 static int env_sf_init_addr(void)
298 env_t *env_ptr = (env_t *)env_sf_get_env_addr();
300 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
301 gd->env_addr = (ulong)&(env_ptr->data);
304 gd->env_addr = (ulong)&default_environment[0];
312 #if defined(CONFIG_ENV_SPI_EARLY)
314 * early load environment from SPI flash (before relocation)
315 * and check if it is valid.
317 static int env_sf_init_early(void)
323 env_t *tmp_env2 = NULL;
327 * if malloc is not ready yet, we cannot use
330 if (!gd->malloc_limit)
333 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
335 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
336 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
339 if (!tmp_env1 || !tmp_env2)
342 ret = setup_flash_device();
346 read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
347 CONFIG_ENV_SIZE, tmp_env1);
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);
356 if (ret == -EIO || ret == -ENOMSG)
359 if (gd->env_valid == ENV_VALID)
360 gd->env_addr = (unsigned long)&tmp_env1->data;
362 gd->env_addr = (unsigned long)&tmp_env2->data;
367 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
372 /* if valid -> this is our env */
373 gd->env_valid = ENV_VALID;
374 gd->env_addr = (unsigned long)&tmp_env1->data;
379 spi_flash_free(env_flash);
382 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
385 /* env is not valid. always return 0 */
386 gd->env_valid = ENV_INVALID;
391 static int env_sf_init(void)
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();
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
407 U_BOOT_ENV_LOCATION(sf) = {
408 .location = ENVL_SPI_FLASH,
411 .save = CONFIG_IS_ENABLED(SAVEENV) ? ENV_SAVE_PTR(env_sf_save) : NULL,