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 crc1_ok = 0, crc2_ok = 0;
170 env_t *tmp_env1 = NULL;
171 env_t *tmp_env2 = NULL;
174 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
176 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
178 if (!tmp_env1 || !tmp_env2) {
179 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");
209 } else if (crc1_ok && !crc2_ok) {
210 gd->env_valid = ENV_VALID;
211 } else if (!crc1_ok && crc2_ok) {
212 gd->env_valid = ENV_REDUND;
213 } else if (tmp_env1->flags == ACTIVE_FLAG &&
214 tmp_env2->flags == OBSOLETE_FLAG) {
215 gd->env_valid = ENV_VALID;
216 } else if (tmp_env1->flags == OBSOLETE_FLAG &&
217 tmp_env2->flags == ACTIVE_FLAG) {
218 gd->env_valid = ENV_REDUND;
219 } else if (tmp_env1->flags == tmp_env2->flags) {
220 gd->env_valid = ENV_VALID;
221 } else if (tmp_env1->flags == 0xFF) {
222 gd->env_valid = ENV_VALID;
223 } else if (tmp_env2->flags == 0xFF) {
224 gd->env_valid = ENV_REDUND;
227 * this differs from code in env_flash.c, but I think a sane
228 * default path is desirable.
230 gd->env_valid = ENV_VALID;
233 if (gd->env_valid == ENV_VALID)
238 ret = env_import((char *)ep, 0);
240 pr_err("Cannot import environment: errno = %d\n", errno);
241 set_default_env("!env_import failed");
245 spi_flash_free(env_flash);
255 static int env_sf_save(void)
257 u32 saved_size, saved_offset, sector;
258 char *saved_buffer = NULL;
262 ret = setup_flash_device();
266 /* Is the sector larger than the env (i.e. embedded) */
267 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
268 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
269 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
270 saved_buffer = malloc(saved_size);
274 ret = spi_flash_read(env_flash, saved_offset,
275 saved_size, saved_buffer);
280 ret = env_export(&env_new);
284 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
286 puts("Erasing SPI flash...");
287 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
288 sector * CONFIG_ENV_SECT_SIZE);
292 puts("Writing to SPI flash...");
293 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
294 CONFIG_ENV_SIZE, &env_new);
298 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
299 ret = spi_flash_write(env_flash, saved_offset,
300 saved_size, saved_buffer);
314 #endif /* CMD_SAVEENV */
316 static int env_sf_load(void)
321 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
323 set_default_env("!malloc() failed");
327 ret = setup_flash_device();
331 ret = spi_flash_read(env_flash,
332 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
334 set_default_env("!spi_flash_read() failed");
338 ret = env_import(buf, 1);
340 gd->env_valid = ENV_VALID;
343 spi_flash_free(env_flash);
352 #if defined(INITENV) && defined(CONFIG_ENV_ADDR)
353 static int env_sf_init(void)
355 env_t *env_ptr = (env_t *)(CONFIG_ENV_ADDR);
357 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
358 gd->env_addr = (ulong)&(env_ptr->data);
361 gd->env_addr = (ulong)&default_environment[0];
369 U_BOOT_ENV_LOCATION(sf) = {
370 .location = ENVL_SPI_FLASH,
371 ENV_NAME("SPI Flash")
374 .save = env_save_ptr(env_sf_save),
376 #if defined(INITENV) && defined(CONFIG_ENV_ADDR)