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>
20 #ifndef CONFIG_ENV_SPI_BUS
21 # define CONFIG_ENV_SPI_BUS 0
23 #ifndef CONFIG_ENV_SPI_CS
24 # define CONFIG_ENV_SPI_CS 0
26 #ifndef CONFIG_ENV_SPI_MAX_HZ
27 # define CONFIG_ENV_SPI_MAX_HZ 1000000
29 #ifndef CONFIG_ENV_SPI_MODE
30 # define CONFIG_ENV_SPI_MODE SPI_MODE_3
33 #ifdef CONFIG_ENV_OFFSET_REDUND
34 static ulong env_offset = CONFIG_ENV_OFFSET;
35 static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
38 #define OBSOLETE_FLAG 0
39 #endif /* CONFIG_ENV_OFFSET_REDUND */
41 DECLARE_GLOBAL_DATA_PTR;
43 char *env_name_spec = "SPI Flash";
45 static struct spi_flash *env_flash;
47 #if defined(CONFIG_ENV_OFFSET_REDUND)
51 char *saved_buffer = NULL, flag = OBSOLETE_FLAG;
52 u32 saved_size, saved_offset, sector = 1;
56 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
58 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
60 set_default_env("!spi_flash_probe() failed");
65 ret = env_export(&env_new);
68 env_new.flags = ACTIVE_FLAG;
70 if (gd->env_valid == 1) {
71 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
72 env_offset = CONFIG_ENV_OFFSET;
74 env_new_offset = CONFIG_ENV_OFFSET;
75 env_offset = CONFIG_ENV_OFFSET_REDUND;
78 /* Is the sector larger than the env (i.e. embedded) */
79 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
80 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
81 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
82 saved_buffer = malloc(saved_size);
87 ret = spi_flash_read(env_flash, saved_offset,
88 saved_size, saved_buffer);
93 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
94 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
95 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
99 puts("Erasing SPI flash...");
100 ret = spi_flash_erase(env_flash, env_new_offset,
101 sector * CONFIG_ENV_SECT_SIZE);
105 puts("Writing to SPI flash...");
107 ret = spi_flash_write(env_flash, env_new_offset,
108 CONFIG_ENV_SIZE, &env_new);
112 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
113 ret = spi_flash_write(env_flash, saved_offset,
114 saved_size, saved_buffer);
119 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
120 sizeof(env_new.flags), &flag);
126 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
128 printf("Valid environment: %d\n", (int)gd->env_valid);
137 void env_relocate_spec(void)
140 int crc1_ok = 0, crc2_ok = 0;
141 env_t *tmp_env1 = NULL;
142 env_t *tmp_env2 = NULL;
145 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
146 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
148 if (!tmp_env1 || !tmp_env2) {
149 set_default_env("!malloc() failed");
153 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
154 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
156 set_default_env("!spi_flash_probe() failed");
160 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
161 CONFIG_ENV_SIZE, tmp_env1);
163 set_default_env("!spi_flash_read() failed");
167 if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc)
170 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
171 CONFIG_ENV_SIZE, tmp_env2);
173 if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc)
177 if (!crc1_ok && !crc2_ok) {
178 set_default_env("!bad CRC");
180 } else if (crc1_ok && !crc2_ok) {
182 } else if (!crc1_ok && crc2_ok) {
184 } else if (tmp_env1->flags == ACTIVE_FLAG &&
185 tmp_env2->flags == OBSOLETE_FLAG) {
187 } else if (tmp_env1->flags == OBSOLETE_FLAG &&
188 tmp_env2->flags == ACTIVE_FLAG) {
190 } else if (tmp_env1->flags == tmp_env2->flags) {
192 } else if (tmp_env1->flags == 0xFF) {
194 } else if (tmp_env2->flags == 0xFF) {
198 * this differs from code in env_flash.c, but I think a sane
199 * default path is desirable.
204 if (gd->env_valid == 1)
209 ret = env_import((char *)ep, 0);
211 error("Cannot import environment: errno = %d\n", errno);
212 set_default_env("env_import failed");
216 spi_flash_free(env_flash);
225 u32 saved_size, saved_offset, sector = 1;
226 char *saved_buffer = NULL;
231 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
233 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
235 set_default_env("!spi_flash_probe() failed");
240 /* Is the sector larger than the env (i.e. embedded) */
241 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
242 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
243 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
244 saved_buffer = malloc(saved_size);
248 ret = spi_flash_read(env_flash, saved_offset,
249 saved_size, saved_buffer);
254 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
255 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
256 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
260 ret = env_export(&env_new);
264 puts("Erasing SPI flash...");
265 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
266 sector * CONFIG_ENV_SECT_SIZE);
270 puts("Writing to SPI flash...");
271 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
272 CONFIG_ENV_SIZE, &env_new);
276 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
277 ret = spi_flash_write(env_flash, saved_offset,
278 saved_size, saved_buffer);
293 void env_relocate_spec(void)
298 buf = (char *)malloc(CONFIG_ENV_SIZE);
299 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
300 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
302 set_default_env("!spi_flash_probe() failed");
308 ret = spi_flash_read(env_flash,
309 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
311 set_default_env("!spi_flash_read() failed");
315 ret = env_import(buf, 1);
319 spi_flash_free(env_flash);
328 /* SPI flash isn't usable before relocation */
329 gd->env_addr = (ulong)&default_environment[0];