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 #ifdef CONFIG_ENV_OFFSET_REDUND
36 static ulong env_offset = CONFIG_ENV_OFFSET;
37 static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
40 #define OBSOLETE_FLAG 0
41 #endif /* CONFIG_ENV_OFFSET_REDUND */
43 DECLARE_GLOBAL_DATA_PTR;
45 char *env_name_spec = "SPI Flash";
47 static struct spi_flash *env_flash;
49 static int setup_flash_device(void)
51 #ifdef CONFIG_DM_SPI_FLASH
55 /* speed and mode will be read from DT */
56 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
59 set_default_env("!spi_flash_probe_bus_cs() failed");
63 env_flash = dev_get_uclass_priv(new);
67 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
69 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
71 set_default_env("!spi_flash_probe() failed");
79 #if defined(CONFIG_ENV_OFFSET_REDUND)
83 char *saved_buffer = NULL, flag = OBSOLETE_FLAG;
84 u32 saved_size, saved_offset, sector;
87 ret = setup_flash_device();
91 ret = env_export(&env_new);
94 env_new.flags = ACTIVE_FLAG;
96 if (gd->env_valid == 1) {
97 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
98 env_offset = CONFIG_ENV_OFFSET;
100 env_new_offset = CONFIG_ENV_OFFSET;
101 env_offset = CONFIG_ENV_OFFSET_REDUND;
104 /* Is the sector larger than the env (i.e. embedded) */
105 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
106 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
107 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
108 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
113 ret = spi_flash_read(env_flash, saved_offset,
114 saved_size, saved_buffer);
119 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
121 puts("Erasing SPI flash...");
122 ret = spi_flash_erase(env_flash, env_new_offset,
123 sector * CONFIG_ENV_SECT_SIZE);
127 puts("Writing to SPI flash...");
129 ret = spi_flash_write(env_flash, env_new_offset,
130 CONFIG_ENV_SIZE, &env_new);
134 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
135 ret = spi_flash_write(env_flash, saved_offset,
136 saved_size, saved_buffer);
141 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
142 sizeof(env_new.flags), &flag);
148 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
150 printf("Valid environment: %d\n", (int)gd->env_valid);
159 void env_relocate_spec(void)
162 int crc1_ok = 0, crc2_ok = 0;
163 env_t *tmp_env1 = NULL;
164 env_t *tmp_env2 = NULL;
167 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
169 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
171 if (!tmp_env1 || !tmp_env2) {
172 set_default_env("!malloc() failed");
176 ret = setup_flash_device();
180 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
181 CONFIG_ENV_SIZE, tmp_env1);
183 set_default_env("!spi_flash_read() failed");
187 if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc)
190 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
191 CONFIG_ENV_SIZE, tmp_env2);
193 if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc)
197 if (!crc1_ok && !crc2_ok) {
198 set_default_env("!bad CRC");
200 } else if (crc1_ok && !crc2_ok) {
202 } else if (!crc1_ok && crc2_ok) {
204 } else if (tmp_env1->flags == ACTIVE_FLAG &&
205 tmp_env2->flags == OBSOLETE_FLAG) {
207 } else if (tmp_env1->flags == OBSOLETE_FLAG &&
208 tmp_env2->flags == ACTIVE_FLAG) {
210 } else if (tmp_env1->flags == tmp_env2->flags) {
212 } else if (tmp_env1->flags == 0xFF) {
214 } else if (tmp_env2->flags == 0xFF) {
218 * this differs from code in env_flash.c, but I think a sane
219 * default path is desirable.
224 if (gd->env_valid == 1)
229 ret = env_import((char *)ep, 0);
231 error("Cannot import environment: errno = %d\n", errno);
232 set_default_env("!env_import failed");
236 spi_flash_free(env_flash);
245 u32 saved_size, saved_offset, sector;
246 char *saved_buffer = NULL;
250 ret = setup_flash_device();
254 /* Is the sector larger than the env (i.e. embedded) */
255 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
256 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
257 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
258 saved_buffer = malloc(saved_size);
262 ret = spi_flash_read(env_flash, saved_offset,
263 saved_size, saved_buffer);
268 ret = env_export(&env_new);
272 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
274 puts("Erasing SPI flash...");
275 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
276 sector * CONFIG_ENV_SECT_SIZE);
280 puts("Writing to SPI flash...");
281 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
282 CONFIG_ENV_SIZE, &env_new);
286 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
287 ret = spi_flash_write(env_flash, saved_offset,
288 saved_size, saved_buffer);
303 void env_relocate_spec(void)
308 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
310 set_default_env("!malloc() failed");
314 ret = setup_flash_device();
318 ret = spi_flash_read(env_flash,
319 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
321 set_default_env("!spi_flash_read() failed");
325 ret = env_import(buf, 1);
330 spi_flash_free(env_flash);
339 /* SPI flash isn't usable before relocation */
340 gd->env_addr = (ulong)&default_environment[0];