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 = memalign(ARCH_DMA_MINALIGN, 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 *)memalign(ARCH_DMA_MINALIGN,
147 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
149 if (!tmp_env1 || !tmp_env2) {
150 set_default_env("!malloc() failed");
154 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
155 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
157 set_default_env("!spi_flash_probe() failed");
161 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
162 CONFIG_ENV_SIZE, tmp_env1);
164 set_default_env("!spi_flash_read() failed");
168 if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc)
171 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
172 CONFIG_ENV_SIZE, tmp_env2);
174 if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc)
178 if (!crc1_ok && !crc2_ok) {
179 set_default_env("!bad CRC");
181 } else if (crc1_ok && !crc2_ok) {
183 } else if (!crc1_ok && crc2_ok) {
185 } else if (tmp_env1->flags == ACTIVE_FLAG &&
186 tmp_env2->flags == OBSOLETE_FLAG) {
188 } else if (tmp_env1->flags == OBSOLETE_FLAG &&
189 tmp_env2->flags == ACTIVE_FLAG) {
191 } else if (tmp_env1->flags == tmp_env2->flags) {
193 } else if (tmp_env1->flags == 0xFF) {
195 } else if (tmp_env2->flags == 0xFF) {
199 * this differs from code in env_flash.c, but I think a sane
200 * default path is desirable.
205 if (gd->env_valid == 1)
210 ret = env_import((char *)ep, 0);
212 error("Cannot import environment: errno = %d\n", errno);
213 set_default_env("env_import failed");
217 spi_flash_free(env_flash);
226 u32 saved_size, saved_offset, sector = 1;
227 char *saved_buffer = NULL;
232 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
234 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
236 set_default_env("!spi_flash_probe() failed");
241 /* Is the sector larger than the env (i.e. embedded) */
242 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
243 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
244 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
245 saved_buffer = malloc(saved_size);
249 ret = spi_flash_read(env_flash, saved_offset,
250 saved_size, saved_buffer);
255 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
256 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
257 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
261 ret = env_export(&env_new);
265 puts("Erasing SPI flash...");
266 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
267 sector * CONFIG_ENV_SECT_SIZE);
271 puts("Writing to SPI flash...");
272 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
273 CONFIG_ENV_SIZE, &env_new);
277 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
278 ret = spi_flash_write(env_flash, saved_offset,
279 saved_size, saved_buffer);
294 void env_relocate_spec(void)
299 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
300 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
301 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
303 set_default_env("!spi_flash_probe() failed");
309 ret = spi_flash_read(env_flash,
310 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
312 set_default_env("!spi_flash_read() failed");
316 ret = env_import(buf, 1);
320 spi_flash_free(env_flash);
329 /* SPI flash isn't usable before relocation */
330 gd->env_addr = (ulong)&default_environment[0];