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>
20 #include <dm/device-internal.h>
22 #ifndef CONFIG_SPL_BUILD
27 #ifdef CONFIG_ENV_OFFSET_REDUND
29 static ulong env_offset = CONFIG_ENV_OFFSET;
30 static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
33 #endif /* CONFIG_ENV_OFFSET_REDUND */
35 DECLARE_GLOBAL_DATA_PTR;
37 static struct spi_flash *env_flash;
39 static int setup_flash_device(void)
41 #ifdef CONFIG_DM_SPI_FLASH
45 /* speed and mode will be read from DT */
46 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
47 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE,
50 env_set_default("spi_flash_probe_bus_cs() failed", 0);
54 env_flash = dev_get_uclass_priv(new);
58 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
60 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
62 env_set_default("spi_flash_probe() failed", 0);
70 #if defined(CONFIG_ENV_OFFSET_REDUND)
72 static int env_sf_save(void)
75 char *saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
76 u32 saved_size, saved_offset, sector;
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 (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
98 saved_size = CONFIG_ENV_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, CONFIG_ENV_SECT_SIZE);
113 puts("Erasing SPI flash...");
114 ret = spi_flash_erase(env_flash, env_new_offset,
115 sector * CONFIG_ENV_SECT_SIZE);
119 puts("Writing to SPI flash...");
121 ret = spi_flash_write(env_flash, env_new_offset,
122 CONFIG_ENV_SIZE, &env_new);
126 if (CONFIG_ENV_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);
150 #endif /* CMD_SAVEENV */
152 static int env_sf_load(void)
155 int read1_fail, read2_fail;
156 env_t *tmp_env1, *tmp_env2;
158 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
160 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
162 if (!tmp_env1 || !tmp_env2) {
163 env_set_default("malloc() failed", 0);
168 ret = setup_flash_device();
172 read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
173 CONFIG_ENV_SIZE, tmp_env1);
174 read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
175 CONFIG_ENV_SIZE, tmp_env2);
177 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
180 spi_flash_free(env_flash);
190 static int env_sf_save(void)
192 u32 saved_size, saved_offset, sector;
193 char *saved_buffer = NULL;
197 ret = setup_flash_device();
201 /* Is the sector larger than the env (i.e. embedded) */
202 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
203 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
204 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
205 saved_buffer = malloc(saved_size);
209 ret = spi_flash_read(env_flash, saved_offset,
210 saved_size, saved_buffer);
215 ret = env_export(&env_new);
219 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
221 puts("Erasing SPI flash...");
222 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
223 sector * CONFIG_ENV_SECT_SIZE);
227 puts("Writing to SPI flash...");
228 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
229 CONFIG_ENV_SIZE, &env_new);
233 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
234 ret = spi_flash_write(env_flash, saved_offset,
235 saved_size, saved_buffer);
249 #endif /* CMD_SAVEENV */
251 static int env_sf_load(void)
256 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
258 env_set_default("malloc() failed", 0);
262 ret = setup_flash_device();
266 ret = spi_flash_read(env_flash,
267 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
269 env_set_default("spi_flash_read() failed", 0);
273 ret = env_import(buf, 1);
275 gd->env_valid = ENV_VALID;
278 spi_flash_free(env_flash);
287 #ifdef CONFIG_ENV_ADDR
288 __weak void *env_sf_get_env_addr(void)
290 return (void *)CONFIG_ENV_ADDR;
294 #if defined(INITENV) && defined(CONFIG_ENV_ADDR)
295 static int env_sf_init(void)
297 env_t *env_ptr = (env_t *)env_sf_get_env_addr();
299 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
300 gd->env_addr = (ulong)&(env_ptr->data);
303 gd->env_addr = (ulong)&default_environment[0];
311 U_BOOT_ENV_LOCATION(sf) = {
312 .location = ENVL_SPI_FLASH,
313 ENV_NAME("SPI Flash")
316 .save = env_save_ptr(env_sf_save),
318 #if defined(INITENV) && defined(CONFIG_ENV_ADDR)