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 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,
63 set_default_env("!spi_flash_probe_bus_cs() failed");
67 env_flash = dev_get_uclass_priv(new);
71 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
73 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
75 set_default_env("!spi_flash_probe() failed");
83 #if defined(CONFIG_ENV_OFFSET_REDUND)
85 static int env_sf_save(void)
88 char *saved_buffer = NULL, flag = OBSOLETE_FLAG;
89 u32 saved_size, saved_offset, sector;
92 ret = setup_flash_device();
96 ret = env_export(&env_new);
99 env_new.flags = ACTIVE_FLAG;
101 if (gd->env_valid == ENV_VALID) {
102 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
103 env_offset = CONFIG_ENV_OFFSET;
105 env_new_offset = CONFIG_ENV_OFFSET;
106 env_offset = CONFIG_ENV_OFFSET_REDUND;
109 /* Is the sector larger than the env (i.e. embedded) */
110 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
111 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
112 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
113 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
118 ret = spi_flash_read(env_flash, saved_offset,
119 saved_size, saved_buffer);
124 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
126 puts("Erasing SPI flash...");
127 ret = spi_flash_erase(env_flash, env_new_offset,
128 sector * CONFIG_ENV_SECT_SIZE);
132 puts("Writing to SPI flash...");
134 ret = spi_flash_write(env_flash, env_new_offset,
135 CONFIG_ENV_SIZE, &env_new);
139 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
140 ret = spi_flash_write(env_flash, saved_offset,
141 saved_size, saved_buffer);
146 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
147 sizeof(env_new.flags), &flag);
153 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
155 printf("Valid environment: %d\n", (int)gd->env_valid);
163 #endif /* CMD_SAVEENV */
165 static void env_sf_load(void)
168 int crc1_ok = 0, crc2_ok = 0;
169 env_t *tmp_env1 = NULL;
170 env_t *tmp_env2 = NULL;
173 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
175 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
177 if (!tmp_env1 || !tmp_env2) {
178 set_default_env("!malloc() failed");
182 ret = setup_flash_device();
186 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
187 CONFIG_ENV_SIZE, tmp_env1);
189 set_default_env("!spi_flash_read() failed");
193 if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc)
196 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
197 CONFIG_ENV_SIZE, tmp_env2);
199 if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc)
203 if (!crc1_ok && !crc2_ok) {
204 set_default_env("!bad CRC");
206 } else if (crc1_ok && !crc2_ok) {
207 gd->env_valid = ENV_VALID;
208 } else if (!crc1_ok && crc2_ok) {
209 gd->env_valid = ENV_REDUND;
210 } else if (tmp_env1->flags == ACTIVE_FLAG &&
211 tmp_env2->flags == OBSOLETE_FLAG) {
212 gd->env_valid = ENV_VALID;
213 } else if (tmp_env1->flags == OBSOLETE_FLAG &&
214 tmp_env2->flags == ACTIVE_FLAG) {
215 gd->env_valid = ENV_REDUND;
216 } else if (tmp_env1->flags == tmp_env2->flags) {
217 gd->env_valid = ENV_VALID;
218 } else if (tmp_env1->flags == 0xFF) {
219 gd->env_valid = ENV_VALID;
220 } else if (tmp_env2->flags == 0xFF) {
221 gd->env_valid = ENV_REDUND;
224 * this differs from code in env_flash.c, but I think a sane
225 * default path is desirable.
227 gd->env_valid = ENV_VALID;
230 if (gd->env_valid == ENV_VALID)
235 ret = env_import((char *)ep, 0);
237 error("Cannot import environment: errno = %d\n", errno);
238 set_default_env("!env_import failed");
242 spi_flash_free(env_flash);
250 static int env_sf_save(void)
252 u32 saved_size, saved_offset, sector;
253 char *saved_buffer = NULL;
257 ret = setup_flash_device();
261 /* Is the sector larger than the env (i.e. embedded) */
262 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
263 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
264 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
265 saved_buffer = malloc(saved_size);
269 ret = spi_flash_read(env_flash, saved_offset,
270 saved_size, saved_buffer);
275 ret = env_export(&env_new);
279 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
281 puts("Erasing SPI flash...");
282 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
283 sector * CONFIG_ENV_SECT_SIZE);
287 puts("Writing to SPI flash...");
288 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
289 CONFIG_ENV_SIZE, &env_new);
293 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
294 ret = spi_flash_write(env_flash, saved_offset,
295 saved_size, saved_buffer);
309 #endif /* CMD_SAVEENV */
311 static void env_sf_load(void)
316 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
318 set_default_env("!malloc() failed");
322 ret = setup_flash_device();
326 ret = spi_flash_read(env_flash,
327 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
329 set_default_env("!spi_flash_read() failed");
333 ret = env_import(buf, 1);
335 gd->env_valid = ENV_VALID;
338 spi_flash_free(env_flash);
345 U_BOOT_ENV_LOCATION(sf) = {
346 .location = ENVL_SPI_FLASH,
347 ENV_NAME("SPI Flash")
350 .save = env_save_ptr(env_sf_save),