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) {
196 * this differs from code in env_flash.c, but I think a sane
197 * default path is desirable.
202 if (gd->env_valid == 1)
207 ret = env_import((char *)ep, 0);
209 error("Cannot import environment: errno = %d\n", errno);
210 set_default_env("env_import failed");
214 spi_flash_free(env_flash);
223 u32 saved_size, saved_offset, sector = 1;
224 char *saved_buffer = NULL;
229 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
231 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
233 set_default_env("!spi_flash_probe() failed");
238 /* Is the sector larger than the env (i.e. embedded) */
239 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
240 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
241 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
242 saved_buffer = malloc(saved_size);
246 ret = spi_flash_read(env_flash, saved_offset,
247 saved_size, saved_buffer);
252 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
253 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
254 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
258 ret = env_export(&env_new);
262 puts("Erasing SPI flash...");
263 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
264 sector * CONFIG_ENV_SECT_SIZE);
268 puts("Writing to SPI flash...");
269 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
270 CONFIG_ENV_SIZE, &env_new);
274 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
275 ret = spi_flash_write(env_flash, saved_offset,
276 saved_size, saved_buffer);
291 void env_relocate_spec(void)
296 buf = (char *)malloc(CONFIG_ENV_SIZE);
297 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
298 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
300 set_default_env("!spi_flash_probe() failed");
306 ret = spi_flash_read(env_flash,
307 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
309 set_default_env("!spi_flash_read() failed");
313 ret = env_import(buf, 1);
317 spi_flash_free(env_flash);
326 /* SPI flash isn't usable before relocation */
327 gd->env_addr = (ulong)&default_environment[0];