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>
15 #include <spi_flash.h>
19 #ifndef CONFIG_ENV_SPI_BUS
20 # define CONFIG_ENV_SPI_BUS 0
22 #ifndef CONFIG_ENV_SPI_CS
23 # define CONFIG_ENV_SPI_CS 0
25 #ifndef CONFIG_ENV_SPI_MAX_HZ
26 # define CONFIG_ENV_SPI_MAX_HZ 1000000
28 #ifndef CONFIG_ENV_SPI_MODE
29 # define CONFIG_ENV_SPI_MODE SPI_MODE_3
32 #ifdef CONFIG_ENV_OFFSET_REDUND
33 static ulong env_offset = CONFIG_ENV_OFFSET;
34 static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
37 #define OBSOLETE_FLAG 0
38 #endif /* CONFIG_ENV_OFFSET_REDUND */
40 DECLARE_GLOBAL_DATA_PTR;
42 char *env_name_spec = "SPI Flash";
44 static struct spi_flash *env_flash;
46 #if defined(CONFIG_ENV_OFFSET_REDUND)
50 char *saved_buffer = NULL, flag = OBSOLETE_FLAG;
51 u32 saved_size, saved_offset, sector = 1;
55 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
57 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
59 set_default_env("!spi_flash_probe() failed");
64 ret = env_export(&env_new);
67 env_new.flags = ACTIVE_FLAG;
69 if (gd->env_valid == 1) {
70 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
71 env_offset = CONFIG_ENV_OFFSET;
73 env_new_offset = CONFIG_ENV_OFFSET;
74 env_offset = CONFIG_ENV_OFFSET_REDUND;
77 /* Is the sector larger than the env (i.e. embedded) */
78 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
79 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
80 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
81 saved_buffer = malloc(saved_size);
86 ret = spi_flash_read(env_flash, saved_offset,
87 saved_size, saved_buffer);
92 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
93 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
94 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
98 puts("Erasing SPI flash...");
99 ret = spi_flash_erase(env_flash, env_new_offset,
100 sector * CONFIG_ENV_SECT_SIZE);
104 puts("Writing to SPI flash...");
106 ret = spi_flash_write(env_flash, env_new_offset,
107 CONFIG_ENV_SIZE, &env_new);
111 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
112 ret = spi_flash_write(env_flash, saved_offset,
113 saved_size, saved_buffer);
118 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
119 sizeof(env_new.flags), &flag);
125 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
127 printf("Valid environment: %d\n", (int)gd->env_valid);
136 void env_relocate_spec(void)
139 int crc1_ok = 0, crc2_ok = 0;
140 env_t *tmp_env1 = NULL;
141 env_t *tmp_env2 = NULL;
144 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
145 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
147 if (!tmp_env1 || !tmp_env2) {
148 set_default_env("!malloc() failed");
152 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
153 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
155 set_default_env("!spi_flash_probe() failed");
159 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
160 CONFIG_ENV_SIZE, tmp_env1);
162 set_default_env("!spi_flash_read() failed");
166 if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc)
169 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
170 CONFIG_ENV_SIZE, tmp_env2);
172 if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc)
176 if (!crc1_ok && !crc2_ok) {
177 set_default_env("!bad CRC");
179 } else if (crc1_ok && !crc2_ok) {
181 } else if (!crc1_ok && crc2_ok) {
183 } else if (tmp_env1->flags == ACTIVE_FLAG &&
184 tmp_env2->flags == OBSOLETE_FLAG) {
186 } else if (tmp_env1->flags == OBSOLETE_FLAG &&
187 tmp_env2->flags == ACTIVE_FLAG) {
189 } else if (tmp_env1->flags == tmp_env2->flags) {
191 } else if (tmp_env1->flags == 0xFF) {
195 * this differs from code in env_flash.c, but I think a sane
196 * default path is desirable.
201 if (gd->env_valid == 1)
206 ret = env_import((char *)ep, 0);
208 error("Cannot import environment: errno = %d\n", errno);
209 set_default_env("env_import failed");
213 spi_flash_free(env_flash);
222 u32 saved_size, saved_offset, sector = 1;
223 char *saved_buffer = NULL;
228 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
230 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
232 set_default_env("!spi_flash_probe() failed");
237 /* Is the sector larger than the env (i.e. embedded) */
238 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
239 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
240 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
241 saved_buffer = malloc(saved_size);
245 ret = spi_flash_read(env_flash, saved_offset,
246 saved_size, saved_buffer);
251 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
252 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
253 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
257 ret = env_export(&env_new);
261 puts("Erasing SPI flash...");
262 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
263 sector * CONFIG_ENV_SECT_SIZE);
267 puts("Writing to SPI flash...");
268 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
269 CONFIG_ENV_SIZE, &env_new);
273 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
274 ret = spi_flash_write(env_flash, saved_offset,
275 saved_size, saved_buffer);
290 void env_relocate_spec(void)
295 buf = (char *)malloc(CONFIG_ENV_SIZE);
296 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
297 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
299 set_default_env("!spi_flash_probe() failed");
305 ret = spi_flash_read(env_flash,
306 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
308 set_default_env("!spi_flash_read() failed");
312 ret = env_import(buf, 1);
316 spi_flash_free(env_flash);
325 /* SPI flash isn't usable before relocation */
326 gd->env_addr = (ulong)&default_environment[0];