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,
53 env_set_default("spi_flash_probe_bus_cs() failed", 0);
57 *env_flash = dev_get_uclass_priv(new);
59 *env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
60 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
62 env_set_default("spi_flash_probe() failed", 0);
69 #if defined(CONFIG_ENV_OFFSET_REDUND)
70 static int env_sf_save(void)
73 char *saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
74 u32 saved_size = 0, saved_offset = 0, sector;
75 u32 sect_size = CONFIG_ENV_SECT_SIZE;
77 struct spi_flash *env_flash;
79 ret = setup_flash_device(&env_flash);
83 if (IS_ENABLED(CONFIG_ENV_SECT_SIZE_AUTO))
84 sect_size = env_flash->mtd.erasesize;
86 ret = env_export(&env_new);
89 env_new.flags = ENV_REDUND_ACTIVE;
91 if (gd->env_valid == ENV_VALID) {
92 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
93 env_offset = CONFIG_ENV_OFFSET;
95 env_new_offset = CONFIG_ENV_OFFSET;
96 env_offset = CONFIG_ENV_OFFSET_REDUND;
99 /* Is the sector larger than the env (i.e. embedded) */
100 if (sect_size > CONFIG_ENV_SIZE) {
101 saved_size = sect_size - CONFIG_ENV_SIZE;
102 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
103 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
108 ret = spi_flash_read(env_flash, saved_offset,
109 saved_size, saved_buffer);
114 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size);
116 puts("Erasing SPI flash...");
117 ret = spi_flash_erase(env_flash, env_new_offset,
122 puts("Writing to SPI flash...");
124 ret = spi_flash_write(env_flash, env_new_offset,
125 CONFIG_ENV_SIZE, &env_new);
129 if (sect_size > CONFIG_ENV_SIZE) {
130 ret = spi_flash_write(env_flash, saved_offset,
131 saved_size, saved_buffer);
136 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
137 sizeof(env_new.flags), &flag);
143 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
145 printf("Valid environment: %d\n", (int)gd->env_valid);
148 spi_flash_free(env_flash);
156 static int env_sf_load(void)
159 int read1_fail, read2_fail;
160 env_t *tmp_env1, *tmp_env2;
161 struct spi_flash *env_flash;
163 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
165 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
167 if (!tmp_env1 || !tmp_env2) {
168 env_set_default("malloc() failed", 0);
173 ret = setup_flash_device(&env_flash);
177 read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
178 CONFIG_ENV_SIZE, tmp_env1);
179 read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
180 CONFIG_ENV_SIZE, tmp_env2);
182 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
183 read2_fail, H_EXTERNAL);
185 spi_flash_free(env_flash);
193 static int env_sf_save(void)
195 u32 saved_size = 0, saved_offset = 0, sector;
196 u32 sect_size = CONFIG_ENV_SECT_SIZE;
197 char *saved_buffer = NULL;
200 struct spi_flash *env_flash;
202 ret = setup_flash_device(&env_flash);
206 if (IS_ENABLED(CONFIG_ENV_SECT_SIZE_AUTO))
207 sect_size = env_flash->mtd.erasesize;
209 /* Is the sector larger than the env (i.e. embedded) */
210 if (sect_size > CONFIG_ENV_SIZE) {
211 saved_size = sect_size - CONFIG_ENV_SIZE;
212 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
213 saved_buffer = malloc(saved_size);
217 ret = spi_flash_read(env_flash, saved_offset,
218 saved_size, saved_buffer);
223 ret = env_export(&env_new);
227 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size);
229 puts("Erasing SPI flash...");
230 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
235 puts("Writing to SPI flash...");
236 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
237 CONFIG_ENV_SIZE, &env_new);
241 if (sect_size > CONFIG_ENV_SIZE) {
242 ret = spi_flash_write(env_flash, saved_offset,
243 saved_size, saved_buffer);
252 spi_flash_free(env_flash);
260 static int env_sf_load(void)
264 struct spi_flash *env_flash;
266 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
268 env_set_default("malloc() failed", 0);
272 ret = setup_flash_device(&env_flash);
276 ret = spi_flash_read(env_flash,
277 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
279 env_set_default("spi_flash_read() failed", 0);
283 ret = env_import(buf, 1, H_EXTERNAL);
285 gd->env_valid = ENV_VALID;
288 spi_flash_free(env_flash);
296 static int env_sf_erase(void)
300 struct spi_flash *env_flash;
302 ret = setup_flash_device(&env_flash);
306 memset(&env, 0, sizeof(env_t));
307 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, &env);
311 if (ENV_OFFSET_REDUND != OFFSET_INVALID)
312 ret = spi_flash_write(env_flash, ENV_OFFSET_REDUND, CONFIG_ENV_SIZE, &env);
315 spi_flash_free(env_flash);
320 __weak void *env_sf_get_env_addr(void)
322 #ifndef CONFIG_SPL_BUILD
323 return (void *)CONFIG_ENV_ADDR;
330 * check if Environment on CONFIG_ENV_ADDR is valid.
332 static int env_sf_init_addr(void)
334 env_t *env_ptr = (env_t *)env_sf_get_env_addr();
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;
343 gd->env_valid = ENV_INVALID;
349 #if defined(CONFIG_ENV_SPI_EARLY)
351 * early load environment from SPI flash (before relocation)
352 * and check if it is valid.
354 static int env_sf_init_early(void)
360 env_t *tmp_env2 = NULL;
362 struct spi_flash *env_flash;
365 * if malloc is not ready yet, we cannot use
368 if (!gd->malloc_limit)
371 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
373 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
374 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
377 if (!tmp_env1 || !tmp_env2)
380 ret = setup_flash_device(&env_flash);
384 read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
385 CONFIG_ENV_SIZE, tmp_env1);
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);
397 if (gd->env_valid == ENV_VALID)
398 gd->env_addr = (unsigned long)&tmp_env1->data;
400 gd->env_addr = (unsigned long)&tmp_env2->data;
405 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
410 /* if valid -> this is our env */
411 gd->env_valid = ENV_VALID;
412 gd->env_addr = (unsigned long)&tmp_env1->data;
415 spi_flash_free(env_flash);
419 spi_flash_free(env_flash);
422 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
425 /* env is not valid. always return 0 */
426 gd->env_valid = ENV_INVALID;
431 static int env_sf_init(void)
433 int ret = env_sf_init_addr();
436 #ifdef CONFIG_ENV_SPI_EARLY
437 return env_sf_init_early();
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
448 U_BOOT_ENV_LOCATION(sf) = {
449 .location = ENVL_SPI_FLASH,
452 .save = ENV_SAVE_PTR(env_sf_save),
453 .erase = ENV_ERASE_PTR(env_sf_erase),