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
13 #include <environment.h>
16 #include <spi_flash.h>
19 #include <dm/device-internal.h>
21 #ifndef CONFIG_ENV_SPI_BUS
22 # define CONFIG_ENV_SPI_BUS CONFIG_SF_DEFAULT_BUS
24 #ifndef CONFIG_ENV_SPI_CS
25 # define CONFIG_ENV_SPI_CS CONFIG_SF_DEFAULT_CS
27 #ifndef CONFIG_ENV_SPI_MAX_HZ
28 # define CONFIG_ENV_SPI_MAX_HZ CONFIG_SF_DEFAULT_SPEED
30 #ifndef CONFIG_ENV_SPI_MODE
31 # define CONFIG_ENV_SPI_MODE CONFIG_SF_DEFAULT_MODE
34 #ifndef CONFIG_SPL_BUILD
39 #ifdef CONFIG_ENV_OFFSET_REDUND
41 static ulong env_offset = CONFIG_ENV_OFFSET;
42 static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
46 #define OBSOLETE_FLAG 0
47 #endif /* CONFIG_ENV_OFFSET_REDUND */
49 DECLARE_GLOBAL_DATA_PTR;
51 static struct spi_flash *env_flash;
53 static int setup_flash_device(void)
55 #ifdef CONFIG_DM_SPI_FLASH
59 /* speed and mode will be read from DT */
60 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
61 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE,
64 set_default_env("spi_flash_probe_bus_cs() failed", 0);
68 env_flash = dev_get_uclass_priv(new);
72 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
74 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
76 set_default_env("spi_flash_probe() failed", 0);
84 static int is_end(const char *addr, size_t size)
86 /* The end of env variables is marked by '\0\0' */
89 for (i = 0; i < size - 1; ++i)
90 if (addr[i] == 0x0 && addr[i + 1] == 0x0)
95 static int spi_flash_read_env(struct spi_flash *flash, u32 offset, size_t len,
99 u32 page_size = flash->page_size;
101 memset(buf, 0x0, len);
102 for (int i = 0; i < len / page_size; ++i) {
103 int ret = spi_flash_read(flash, offset, page_size,
104 &((char *)buf)[addr]);
109 if (is_end(&((char *)buf)[addr], page_size))
118 #if defined(CONFIG_ENV_OFFSET_REDUND)
120 static int env_sf_save(void)
123 char *saved_buffer = NULL, flag = OBSOLETE_FLAG;
124 u32 saved_size, saved_offset, sector;
127 ret = setup_flash_device();
131 ret = env_export(&env_new);
134 env_new.flags = ACTIVE_FLAG;
136 if (gd->env_valid == ENV_VALID) {
137 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
138 env_offset = CONFIG_ENV_OFFSET;
140 env_new_offset = CONFIG_ENV_OFFSET;
141 env_offset = CONFIG_ENV_OFFSET_REDUND;
144 /* Is the sector larger than the env (i.e. embedded) */
145 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
146 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
147 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
148 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
153 ret = spi_flash_read_env(env_flash, saved_offset,
154 saved_size, saved_buffer);
159 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
161 puts("Erasing SPI flash...");
162 ret = spi_flash_erase(env_flash, env_new_offset,
163 sector * CONFIG_ENV_SECT_SIZE);
167 puts("Writing to SPI flash...");
169 ret = spi_flash_write(env_flash, env_new_offset,
170 CONFIG_ENV_SIZE, &env_new);
174 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
175 ret = spi_flash_write(env_flash, saved_offset,
176 saved_size, saved_buffer);
181 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
182 sizeof(env_new.flags), &flag);
188 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
190 printf("Valid environment: %d\n", (int)gd->env_valid);
198 #endif /* CMD_SAVEENV */
200 static int env_sf_load(void)
203 int read1_fail, read2_fail;
204 env_t *tmp_env1, *tmp_env2;
206 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
208 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
210 if (!tmp_env1 || !tmp_env2) {
211 set_default_env("malloc() failed", 0);
216 ret = setup_flash_device();
220 read1_fail = spi_flash_read_env(env_flash, CONFIG_ENV_OFFSET,
221 CONFIG_ENV_SIZE, tmp_env1);
222 read2_fail = spi_flash_read_env(env_flash, CONFIG_ENV_OFFSET_REDUND,
223 CONFIG_ENV_SIZE, tmp_env2);
225 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
228 spi_flash_free(env_flash);
238 static int env_sf_save(void)
240 u32 saved_size, saved_offset, sector;
241 char *saved_buffer = NULL;
245 ret = setup_flash_device();
249 /* Is the sector larger than the env (i.e. embedded) */
250 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
251 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
252 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
253 saved_buffer = malloc(saved_size);
257 ret = spi_flash_read_env(env_flash, saved_offset,
258 saved_size, saved_buffer);
263 ret = env_export(&env_new);
267 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
269 puts("Erasing SPI flash...");
270 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
271 sector * CONFIG_ENV_SECT_SIZE);
275 puts("Writing to SPI flash...");
276 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
277 CONFIG_ENV_SIZE, &env_new);
281 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
282 ret = spi_flash_write(env_flash, saved_offset,
283 saved_size, saved_buffer);
297 #endif /* CMD_SAVEENV */
299 static int env_sf_load(void)
304 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
306 set_default_env("malloc() failed", 0);
310 ret = setup_flash_device();
314 ret = spi_flash_read_env(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE,
317 set_default_env("spi_flash_read_env() failed", 0);
321 ret = env_import(buf, 1);
323 gd->env_valid = ENV_VALID;
326 spi_flash_free(env_flash);
335 #ifdef CONFIG_ENV_ADDR
336 __weak void *env_sf_get_env_addr(void)
338 return (void *)CONFIG_ENV_ADDR;
342 #if defined(INITENV) && defined(CONFIG_ENV_ADDR)
343 static int env_sf_init(void)
345 env_t *env_ptr = (env_t *)env_sf_get_env_addr();
347 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
348 gd->env_addr = (ulong)&(env_ptr->data);
351 gd->env_addr = (ulong)&default_environment[0];
359 U_BOOT_ENV_LOCATION(sf) = {
360 .location = ENVL_SPI_FLASH,
361 ENV_NAME("SPI Flash")
364 .save = env_save_ptr(env_sf_save),
366 #if defined(INITENV) && defined(CONFIG_ENV_ADDR)