1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2010
6 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
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;
78 ret = setup_flash_device();
82 ret = env_export(&env_new);
85 env_new.flags = ENV_REDUND_ACTIVE;
87 if (gd->env_valid == ENV_VALID) {
88 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
89 env_offset = CONFIG_ENV_OFFSET;
91 env_new_offset = CONFIG_ENV_OFFSET;
92 env_offset = CONFIG_ENV_OFFSET_REDUND;
95 /* Is the sector larger than the env (i.e. embedded) */
96 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
97 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
98 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
99 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
104 ret = spi_flash_read(env_flash, saved_offset,
105 saved_size, saved_buffer);
110 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
112 puts("Erasing SPI flash...");
113 ret = spi_flash_erase(env_flash, env_new_offset,
114 sector * CONFIG_ENV_SECT_SIZE);
118 puts("Writing to SPI flash...");
120 ret = spi_flash_write(env_flash, env_new_offset,
121 CONFIG_ENV_SIZE, &env_new);
125 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
126 ret = spi_flash_write(env_flash, saved_offset,
127 saved_size, saved_buffer);
132 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
133 sizeof(env_new.flags), &flag);
139 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
141 printf("Valid environment: %d\n", (int)gd->env_valid);
150 static int env_sf_load(void)
153 int read1_fail, read2_fail;
154 env_t *tmp_env1, *tmp_env2;
156 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
158 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
160 if (!tmp_env1 || !tmp_env2) {
161 env_set_default("malloc() failed", 0);
166 ret = setup_flash_device();
170 read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
171 CONFIG_ENV_SIZE, tmp_env1);
172 read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
173 CONFIG_ENV_SIZE, tmp_env2);
175 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
176 read2_fail, H_EXTERNAL);
178 spi_flash_free(env_flash);
187 static int env_sf_save(void)
189 u32 saved_size, saved_offset, sector;
190 char *saved_buffer = NULL;
194 ret = setup_flash_device();
198 /* Is the sector larger than the env (i.e. embedded) */
199 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
200 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
201 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
202 saved_buffer = malloc(saved_size);
206 ret = spi_flash_read(env_flash, saved_offset,
207 saved_size, saved_buffer);
212 ret = env_export(&env_new);
216 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
218 puts("Erasing SPI flash...");
219 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
220 sector * CONFIG_ENV_SECT_SIZE);
224 puts("Writing to SPI flash...");
225 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
226 CONFIG_ENV_SIZE, &env_new);
230 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
231 ret = spi_flash_write(env_flash, saved_offset,
232 saved_size, saved_buffer);
247 static int env_sf_load(void)
252 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
254 env_set_default("malloc() failed", 0);
258 ret = setup_flash_device();
262 ret = spi_flash_read(env_flash,
263 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
265 env_set_default("spi_flash_read() failed", 0);
269 ret = env_import(buf, 1, H_EXTERNAL);
271 gd->env_valid = ENV_VALID;
274 spi_flash_free(env_flash);
283 #if CONFIG_ENV_ADDR != 0x0
284 __weak void *env_sf_get_env_addr(void)
286 return (void *)CONFIG_ENV_ADDR;
290 #if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0)
292 * check if Environment on CONFIG_ENV_ADDR is valid.
294 static int env_sf_init_addr(void)
296 env_t *env_ptr = (env_t *)env_sf_get_env_addr();
298 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
299 gd->env_addr = (ulong)&(env_ptr->data);
302 gd->env_addr = (ulong)&default_environment[0];
310 #if defined(CONFIG_ENV_SPI_EARLY)
312 * early load environment from SPI flash (before relocation)
313 * and check if it is valid.
315 static int env_sf_init_early(void)
321 env_t *tmp_env2 = NULL;
325 * if malloc is not ready yet, we cannot use
328 if (!gd->malloc_limit)
331 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
333 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
334 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
337 if (!tmp_env1 || !tmp_env2)
340 ret = setup_flash_device();
344 read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
345 CONFIG_ENV_SIZE, tmp_env1);
347 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT)) {
348 read2_fail = spi_flash_read(env_flash,
349 CONFIG_ENV_OFFSET_REDUND,
350 CONFIG_ENV_SIZE, tmp_env2);
351 ret = env_check_redund((char *)tmp_env1, read1_fail,
352 (char *)tmp_env2, read2_fail);
354 if (ret == -EIO || ret == -ENOMSG)
357 if (gd->env_valid == ENV_VALID)
358 gd->env_addr = (unsigned long)&tmp_env1->data;
360 gd->env_addr = (unsigned long)&tmp_env2->data;
365 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
370 /* if valid -> this is our env */
371 gd->env_valid = ENV_VALID;
372 gd->env_addr = (unsigned long)&tmp_env1->data;
377 spi_flash_free(env_flash);
380 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
383 /* env is not valid. always return 0 */
384 gd->env_valid = ENV_INVALID;
389 static int env_sf_init(void)
391 #if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0)
392 return env_sf_init_addr();
393 #elif defined(CONFIG_ENV_SPI_EARLY)
394 return env_sf_init_early();
397 * return here -ENOENT, so env_init()
398 * can set the init bit and later if no
399 * other Environment storage is defined
400 * can set the default environment
405 U_BOOT_ENV_LOCATION(sf) = {
406 .location = ENVL_SPI_FLASH,
409 .save = CONFIG_IS_ENABLED(SAVEENV) ? ENV_SAVE_PTR(env_sf_save) : NULL,