2 * (C) Copyright 2000-2010
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6 * Andreas Heppel <aheppel@sysgo.de>
8 * (C) Copyright 2008 Atmel Corporation
10 * SPDX-License-Identifier: GPL-2.0+
14 #include <environment.h>
17 #include <spi_flash.h>
20 #include <dm/device-internal.h>
22 #ifndef CONFIG_ENV_SPI_BUS
23 # define CONFIG_ENV_SPI_BUS CONFIG_SF_DEFAULT_BUS
25 #ifndef CONFIG_ENV_SPI_CS
26 # define CONFIG_ENV_SPI_CS CONFIG_SF_DEFAULT_CS
28 #ifndef CONFIG_ENV_SPI_MAX_HZ
29 # define CONFIG_ENV_SPI_MAX_HZ CONFIG_SF_DEFAULT_SPEED
31 #ifndef CONFIG_ENV_SPI_MODE
32 # define CONFIG_ENV_SPI_MODE CONFIG_SF_DEFAULT_MODE
35 #ifndef CONFIG_SPL_BUILD
40 #ifdef CONFIG_ENV_OFFSET_REDUND
42 static ulong env_offset = CONFIG_ENV_OFFSET;
43 static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
47 #define OBSOLETE_FLAG 0
48 #endif /* CONFIG_ENV_OFFSET_REDUND */
50 DECLARE_GLOBAL_DATA_PTR;
52 static struct spi_flash *env_flash;
54 static int setup_flash_device(void)
56 #ifdef CONFIG_DM_SPI_FLASH
60 /* speed and mode will be read from DT */
61 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
64 set_default_env("!spi_flash_probe_bus_cs() failed");
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");
84 #if defined(CONFIG_ENV_OFFSET_REDUND)
86 static int env_sf_save(void)
89 char *saved_buffer = NULL, flag = OBSOLETE_FLAG;
90 u32 saved_size, saved_offset, sector;
93 ret = setup_flash_device();
97 ret = env_export(&env_new);
100 env_new.flags = ACTIVE_FLAG;
102 if (gd->env_valid == ENV_VALID) {
103 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
104 env_offset = CONFIG_ENV_OFFSET;
106 env_new_offset = CONFIG_ENV_OFFSET;
107 env_offset = CONFIG_ENV_OFFSET_REDUND;
110 /* Is the sector larger than the env (i.e. embedded) */
111 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
112 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
113 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
114 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
119 ret = spi_flash_read(env_flash, saved_offset,
120 saved_size, saved_buffer);
125 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
127 puts("Erasing SPI flash...");
128 ret = spi_flash_erase(env_flash, env_new_offset,
129 sector * CONFIG_ENV_SECT_SIZE);
133 puts("Writing to SPI flash...");
135 ret = spi_flash_write(env_flash, env_new_offset,
136 CONFIG_ENV_SIZE, &env_new);
140 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
141 ret = spi_flash_write(env_flash, saved_offset,
142 saved_size, saved_buffer);
147 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
148 sizeof(env_new.flags), &flag);
154 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
156 printf("Valid environment: %d\n", (int)gd->env_valid);
164 #endif /* CMD_SAVEENV */
166 static int env_sf_load(void)
169 int read1_fail, read2_fail;
170 env_t *tmp_env1, *tmp_env2;
172 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
174 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
176 if (!tmp_env1 || !tmp_env2) {
177 set_default_env("!malloc() failed");
182 ret = setup_flash_device();
186 read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
187 CONFIG_ENV_SIZE, tmp_env1);
188 read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
189 CONFIG_ENV_SIZE, tmp_env2);
191 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
194 spi_flash_free(env_flash);
204 static int env_sf_save(void)
206 u32 saved_size, saved_offset, sector;
207 char *saved_buffer = NULL;
211 ret = setup_flash_device();
215 /* Is the sector larger than the env (i.e. embedded) */
216 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
217 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
218 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
219 saved_buffer = malloc(saved_size);
223 ret = spi_flash_read(env_flash, saved_offset,
224 saved_size, saved_buffer);
229 ret = env_export(&env_new);
233 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
235 puts("Erasing SPI flash...");
236 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
237 sector * CONFIG_ENV_SECT_SIZE);
241 puts("Writing to SPI flash...");
242 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
243 CONFIG_ENV_SIZE, &env_new);
247 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
248 ret = spi_flash_write(env_flash, saved_offset,
249 saved_size, saved_buffer);
263 #endif /* CMD_SAVEENV */
265 static int env_sf_load(void)
270 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
272 set_default_env("!malloc() failed");
276 ret = setup_flash_device();
280 ret = spi_flash_read(env_flash,
281 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
283 set_default_env("!spi_flash_read() failed");
287 ret = env_import(buf, 1);
289 gd->env_valid = ENV_VALID;
292 spi_flash_free(env_flash);
301 #if defined(INITENV) && defined(CONFIG_ENV_ADDR)
302 static int env_sf_init(void)
304 env_t *env_ptr = (env_t *)(CONFIG_ENV_ADDR);
306 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
307 gd->env_addr = (ulong)&(env_ptr->data);
310 gd->env_addr = (ulong)&default_environment[0];
318 U_BOOT_ENV_LOCATION(sf) = {
319 .location = ENVL_SPI_FLASH,
320 ENV_NAME("SPI Flash")
323 .save = env_save_ptr(env_sf_save),
325 #if defined(INITENV) && defined(CONFIG_ENV_ADDR)