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)
51 char *res, *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 res = (char *)&env_new.data;
66 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
68 error("Cannot export environment: errno = %d\n", errno);
71 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
72 env_new.flags = ACTIVE_FLAG;
74 if (gd->env_valid == 1) {
75 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
76 env_offset = CONFIG_ENV_OFFSET;
78 env_new_offset = CONFIG_ENV_OFFSET;
79 env_offset = CONFIG_ENV_OFFSET_REDUND;
82 /* Is the sector larger than the env (i.e. embedded) */
83 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
84 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
85 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
86 saved_buffer = malloc(saved_size);
91 ret = spi_flash_read(env_flash, saved_offset,
92 saved_size, saved_buffer);
97 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
98 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
99 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
103 puts("Erasing SPI flash...");
104 ret = spi_flash_erase(env_flash, env_new_offset,
105 sector * CONFIG_ENV_SECT_SIZE);
109 puts("Writing to SPI flash...");
111 ret = spi_flash_write(env_flash, env_new_offset,
112 CONFIG_ENV_SIZE, &env_new);
116 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
117 ret = spi_flash_write(env_flash, saved_offset,
118 saved_size, saved_buffer);
123 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
124 sizeof(env_new.flags), &flag);
130 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
132 printf("Valid environment: %d\n", (int)gd->env_valid);
141 void env_relocate_spec(void)
144 int crc1_ok = 0, crc2_ok = 0;
145 env_t *tmp_env1 = NULL;
146 env_t *tmp_env2 = NULL;
149 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
150 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
152 if (!tmp_env1 || !tmp_env2) {
153 set_default_env("!malloc() failed");
157 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
158 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
160 set_default_env("!spi_flash_probe() failed");
164 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
165 CONFIG_ENV_SIZE, tmp_env1);
167 set_default_env("!spi_flash_read() failed");
171 if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc)
174 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
175 CONFIG_ENV_SIZE, tmp_env2);
177 if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc)
181 if (!crc1_ok && !crc2_ok) {
182 set_default_env("!bad CRC");
184 } else if (crc1_ok && !crc2_ok) {
186 } else if (!crc1_ok && crc2_ok) {
188 } else if (tmp_env1->flags == ACTIVE_FLAG &&
189 tmp_env2->flags == OBSOLETE_FLAG) {
191 } else if (tmp_env1->flags == OBSOLETE_FLAG &&
192 tmp_env2->flags == ACTIVE_FLAG) {
194 } else if (tmp_env1->flags == tmp_env2->flags) {
196 } else if (tmp_env1->flags == 0xFF) {
200 * this differs from code in env_flash.c, but I think a sane
201 * default path is desirable.
206 if (gd->env_valid == 1)
211 ret = env_import((char *)ep, 0);
213 error("Cannot import environment: errno = %d\n", errno);
214 set_default_env("env_import failed");
218 spi_flash_free(env_flash);
227 u32 saved_size, saved_offset, sector = 1;
228 char *res, *saved_buffer = NULL;
234 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
236 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
238 set_default_env("!spi_flash_probe() failed");
243 /* Is the sector larger than the env (i.e. embedded) */
244 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
245 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
246 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
247 saved_buffer = malloc(saved_size);
251 ret = spi_flash_read(env_flash, saved_offset,
252 saved_size, saved_buffer);
257 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
258 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
259 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
263 res = (char *)&env_new.data;
264 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
266 error("Cannot export environment: errno = %d\n", errno);
269 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
271 puts("Erasing SPI flash...");
272 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
273 sector * CONFIG_ENV_SECT_SIZE);
277 puts("Writing to SPI flash...");
278 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
279 CONFIG_ENV_SIZE, &env_new);
283 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
284 ret = spi_flash_write(env_flash, saved_offset,
285 saved_size, saved_buffer);
300 void env_relocate_spec(void)
305 buf = (char *)malloc(CONFIG_ENV_SIZE);
306 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
307 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
309 set_default_env("!spi_flash_probe() failed");
315 ret = spi_flash_read(env_flash,
316 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
318 set_default_env("!spi_flash_read() failed");
322 ret = env_import(buf, 1);
326 spi_flash_free(env_flash);
335 /* SPI flash isn't usable before relocation */
336 gd->env_addr = (ulong)&default_environment[0];