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
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 char *env_name_spec = "SPI Flash";
53 static struct spi_flash *env_flash;
55 static int setup_flash_device(void)
57 #ifdef CONFIG_DM_SPI_FLASH
61 /* speed and mode will be read from DT */
62 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
65 set_default_env("!spi_flash_probe_bus_cs() failed");
69 env_flash = dev_get_uclass_priv(new);
73 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
75 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
77 set_default_env("!spi_flash_probe() failed");
85 #if defined(CONFIG_ENV_OFFSET_REDUND)
87 static int env_sf_save(void)
90 char *saved_buffer = NULL, flag = OBSOLETE_FLAG;
91 u32 saved_size, saved_offset, sector;
94 ret = setup_flash_device();
98 ret = env_export(&env_new);
101 env_new.flags = ACTIVE_FLAG;
103 if (gd->env_valid == ENV_VALID) {
104 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
105 env_offset = CONFIG_ENV_OFFSET;
107 env_new_offset = CONFIG_ENV_OFFSET;
108 env_offset = CONFIG_ENV_OFFSET_REDUND;
111 /* Is the sector larger than the env (i.e. embedded) */
112 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
113 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
114 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
115 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
120 ret = spi_flash_read(env_flash, saved_offset,
121 saved_size, saved_buffer);
126 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
128 puts("Erasing SPI flash...");
129 ret = spi_flash_erase(env_flash, env_new_offset,
130 sector * CONFIG_ENV_SECT_SIZE);
134 puts("Writing to SPI flash...");
136 ret = spi_flash_write(env_flash, env_new_offset,
137 CONFIG_ENV_SIZE, &env_new);
141 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
142 ret = spi_flash_write(env_flash, saved_offset,
143 saved_size, saved_buffer);
148 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
149 sizeof(env_new.flags), &flag);
155 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
157 printf("Valid environment: %d\n", (int)gd->env_valid);
165 #endif /* CMD_SAVEENV */
167 static void env_sf_load(void)
170 int crc1_ok = 0, crc2_ok = 0;
171 env_t *tmp_env1 = NULL;
172 env_t *tmp_env2 = NULL;
175 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
177 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
179 if (!tmp_env1 || !tmp_env2) {
180 set_default_env("!malloc() failed");
184 ret = setup_flash_device();
188 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
189 CONFIG_ENV_SIZE, tmp_env1);
191 set_default_env("!spi_flash_read() failed");
195 if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc)
198 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
199 CONFIG_ENV_SIZE, tmp_env2);
201 if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc)
205 if (!crc1_ok && !crc2_ok) {
206 set_default_env("!bad CRC");
208 } else if (crc1_ok && !crc2_ok) {
209 gd->env_valid = ENV_VALID;
210 } else if (!crc1_ok && crc2_ok) {
211 gd->env_valid = ENV_REDUND;
212 } else if (tmp_env1->flags == ACTIVE_FLAG &&
213 tmp_env2->flags == OBSOLETE_FLAG) {
214 gd->env_valid = ENV_VALID;
215 } else if (tmp_env1->flags == OBSOLETE_FLAG &&
216 tmp_env2->flags == ACTIVE_FLAG) {
217 gd->env_valid = ENV_REDUND;
218 } else if (tmp_env1->flags == tmp_env2->flags) {
219 gd->env_valid = ENV_VALID;
220 } else if (tmp_env1->flags == 0xFF) {
221 gd->env_valid = ENV_VALID;
222 } else if (tmp_env2->flags == 0xFF) {
223 gd->env_valid = ENV_REDUND;
226 * this differs from code in env_flash.c, but I think a sane
227 * default path is desirable.
229 gd->env_valid = ENV_VALID;
232 if (gd->env_valid == ENV_VALID)
237 ret = env_import((char *)ep, 0);
239 error("Cannot import environment: errno = %d\n", errno);
240 set_default_env("!env_import failed");
244 spi_flash_free(env_flash);
252 static int env_sf_save(void)
254 u32 saved_size, saved_offset, sector;
255 char *saved_buffer = NULL;
259 ret = setup_flash_device();
263 /* Is the sector larger than the env (i.e. embedded) */
264 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
265 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
266 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
267 saved_buffer = malloc(saved_size);
271 ret = spi_flash_read(env_flash, saved_offset,
272 saved_size, saved_buffer);
277 ret = env_export(&env_new);
281 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
283 puts("Erasing SPI flash...");
284 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
285 sector * CONFIG_ENV_SECT_SIZE);
289 puts("Writing to SPI flash...");
290 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
291 CONFIG_ENV_SIZE, &env_new);
295 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
296 ret = spi_flash_write(env_flash, saved_offset,
297 saved_size, saved_buffer);
311 #endif /* CMD_SAVEENV */
313 static void env_sf_load(void)
318 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
320 set_default_env("!malloc() failed");
324 ret = setup_flash_device();
328 ret = spi_flash_read(env_flash,
329 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
331 set_default_env("!spi_flash_read() failed");
335 ret = env_import(buf, 1);
337 gd->env_valid = ENV_VALID;
340 spi_flash_free(env_flash);
347 U_BOOT_ENV_LOCATION(sf) = {
348 .location = ENVL_SPI_FLASH,
351 .save = env_save_ptr(env_sf_save),