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+
13 #include <environment.h>
16 #include <spi_flash.h>
19 #include <dm/device-internal.h>
21 #ifndef CONFIG_ENV_SPI_BUS
22 # define CONFIG_ENV_SPI_BUS CONFIG_SF_DEFAULT_BUS
24 #ifndef CONFIG_ENV_SPI_CS
25 # define CONFIG_ENV_SPI_CS CONFIG_SF_DEFAULT_CS
27 #ifndef CONFIG_ENV_SPI_MAX_HZ
28 # define CONFIG_ENV_SPI_MAX_HZ CONFIG_SF_DEFAULT_SPEED
30 #ifndef CONFIG_ENV_SPI_MODE
31 # define CONFIG_ENV_SPI_MODE CONFIG_SF_DEFAULT_MODE
34 #ifdef CONFIG_ENV_OFFSET_REDUND
35 static ulong env_offset = CONFIG_ENV_OFFSET;
36 static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
39 #define OBSOLETE_FLAG 0
40 #endif /* CONFIG_ENV_OFFSET_REDUND */
42 DECLARE_GLOBAL_DATA_PTR;
44 char *env_name_spec = "SPI Flash";
46 static struct spi_flash *env_flash;
48 static int setup_flash_device(void)
50 #ifdef CONFIG_DM_SPI_FLASH
54 /* speed and mode will be read from DT */
55 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
58 set_default_env("!spi_flash_probe_bus_cs() failed");
62 env_flash = dev_get_uclass_priv(new);
66 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
68 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
70 set_default_env("!spi_flash_probe() failed");
78 #if defined(CONFIG_ENV_OFFSET_REDUND)
82 char *saved_buffer = NULL, flag = OBSOLETE_FLAG;
83 u32 saved_size, saved_offset, sector = 1;
86 ret = setup_flash_device();
90 ret = env_export(&env_new);
93 env_new.flags = ACTIVE_FLAG;
95 if (gd->env_valid == 1) {
96 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
97 env_offset = CONFIG_ENV_OFFSET;
99 env_new_offset = CONFIG_ENV_OFFSET;
100 env_offset = CONFIG_ENV_OFFSET_REDUND;
103 /* Is the sector larger than the env (i.e. embedded) */
104 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
105 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
106 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
107 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
112 ret = spi_flash_read(env_flash, saved_offset,
113 saved_size, saved_buffer);
118 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
119 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
120 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
124 puts("Erasing SPI flash...");
125 ret = spi_flash_erase(env_flash, env_new_offset,
126 sector * CONFIG_ENV_SECT_SIZE);
130 puts("Writing to SPI flash...");
132 ret = spi_flash_write(env_flash, env_new_offset,
133 CONFIG_ENV_SIZE, &env_new);
137 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
138 ret = spi_flash_write(env_flash, saved_offset,
139 saved_size, saved_buffer);
144 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
145 sizeof(env_new.flags), &flag);
151 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
153 printf("Valid environment: %d\n", (int)gd->env_valid);
162 void env_relocate_spec(void)
165 int crc1_ok = 0, crc2_ok = 0;
166 env_t *tmp_env1 = NULL;
167 env_t *tmp_env2 = NULL;
170 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
172 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
174 if (!tmp_env1 || !tmp_env2) {
175 set_default_env("!malloc() failed");
179 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
180 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
182 set_default_env("!spi_flash_probe() failed");
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) {
208 } else if (!crc1_ok && crc2_ok) {
210 } else if (tmp_env1->flags == ACTIVE_FLAG &&
211 tmp_env2->flags == OBSOLETE_FLAG) {
213 } else if (tmp_env1->flags == OBSOLETE_FLAG &&
214 tmp_env2->flags == ACTIVE_FLAG) {
216 } else if (tmp_env1->flags == tmp_env2->flags) {
218 } else if (tmp_env1->flags == 0xFF) {
220 } else if (tmp_env2->flags == 0xFF) {
224 * this differs from code in env_flash.c, but I think a sane
225 * default path is desirable.
230 if (gd->env_valid == 1)
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);
251 u32 saved_size, saved_offset, sector = 1;
252 char *saved_buffer = NULL;
256 ret = setup_flash_device();
260 /* Is the sector larger than the env (i.e. embedded) */
261 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
262 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
263 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
264 saved_buffer = malloc(saved_size);
268 ret = spi_flash_read(env_flash, saved_offset,
269 saved_size, saved_buffer);
274 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
275 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
276 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
280 ret = env_export(&env_new);
284 puts("Erasing SPI flash...");
285 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
286 sector * CONFIG_ENV_SECT_SIZE);
290 puts("Writing to SPI flash...");
291 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
292 CONFIG_ENV_SIZE, &env_new);
296 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
297 ret = spi_flash_write(env_flash, saved_offset,
298 saved_size, saved_buffer);
313 void env_relocate_spec(void)
318 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
319 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
320 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
322 set_default_env("!spi_flash_probe() failed");
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);
339 spi_flash_free(env_flash);
348 /* SPI flash isn't usable before relocation */
349 gd->env_addr = (ulong)&default_environment[0];