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>
19 #include <dm/device-internal.h>
21 #ifndef CONFIG_ENV_SPI_BUS
22 # define CONFIG_ENV_SPI_BUS 0
24 #ifndef CONFIG_ENV_SPI_CS
25 # define CONFIG_ENV_SPI_CS 0
27 #ifndef CONFIG_ENV_SPI_MAX_HZ
28 # define CONFIG_ENV_SPI_MAX_HZ 1000000
30 #ifndef CONFIG_ENV_SPI_MODE
31 # define CONFIG_ENV_SPI_MODE SPI_MODE_3
34 #ifdef CONFIG_ENV_OFFSET_REDUND
35 static ulong env_offset = CONFIG_ENV_OFFSET;
36 static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
39 #define OBSOLETE_FLAG 0
40 #endif /* CONFIG_ENV_OFFSET_REDUND */
42 DECLARE_GLOBAL_DATA_PTR;
44 char *env_name_spec = "SPI Flash";
46 static struct spi_flash *env_flash;
48 #if defined(CONFIG_ENV_OFFSET_REDUND)
52 char *saved_buffer = NULL, flag = OBSOLETE_FLAG;
53 u32 saved_size, saved_offset, sector = 1;
55 #ifdef CONFIG_DM_SPI_FLASH
58 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
59 CONFIG_ENV_SPI_MAX_HZ,
60 CONFIG_ENV_SPI_MODE, &new);
62 set_default_env("!spi_flash_probe_bus_cs() failed");
66 env_flash = dev_get_uclass_priv(new);
70 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
72 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
74 set_default_env("!spi_flash_probe() failed");
80 ret = env_export(&env_new);
83 env_new.flags = ACTIVE_FLAG;
85 if (gd->env_valid == 1) {
86 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
87 env_offset = CONFIG_ENV_OFFSET;
89 env_new_offset = CONFIG_ENV_OFFSET;
90 env_offset = CONFIG_ENV_OFFSET_REDUND;
93 /* Is the sector larger than the env (i.e. embedded) */
94 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
95 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
96 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
97 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
102 ret = spi_flash_read(env_flash, saved_offset,
103 saved_size, saved_buffer);
108 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
109 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
110 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
114 puts("Erasing SPI flash...");
115 ret = spi_flash_erase(env_flash, env_new_offset,
116 sector * CONFIG_ENV_SECT_SIZE);
120 puts("Writing to SPI flash...");
122 ret = spi_flash_write(env_flash, env_new_offset,
123 CONFIG_ENV_SIZE, &env_new);
127 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
128 ret = spi_flash_write(env_flash, saved_offset,
129 saved_size, saved_buffer);
134 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
135 sizeof(env_new.flags), &flag);
141 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
143 printf("Valid environment: %d\n", (int)gd->env_valid);
152 void env_relocate_spec(void)
155 int crc1_ok = 0, crc2_ok = 0;
156 env_t *tmp_env1 = NULL;
157 env_t *tmp_env2 = NULL;
160 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
162 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
164 if (!tmp_env1 || !tmp_env2) {
165 set_default_env("!malloc() failed");
169 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
170 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
172 set_default_env("!spi_flash_probe() failed");
176 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
177 CONFIG_ENV_SIZE, tmp_env1);
179 set_default_env("!spi_flash_read() failed");
183 if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc)
186 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
187 CONFIG_ENV_SIZE, tmp_env2);
189 if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc)
193 if (!crc1_ok && !crc2_ok) {
194 set_default_env("!bad CRC");
196 } else if (crc1_ok && !crc2_ok) {
198 } else if (!crc1_ok && crc2_ok) {
200 } else if (tmp_env1->flags == ACTIVE_FLAG &&
201 tmp_env2->flags == OBSOLETE_FLAG) {
203 } else if (tmp_env1->flags == OBSOLETE_FLAG &&
204 tmp_env2->flags == ACTIVE_FLAG) {
206 } else if (tmp_env1->flags == tmp_env2->flags) {
208 } else if (tmp_env1->flags == 0xFF) {
210 } else if (tmp_env2->flags == 0xFF) {
214 * this differs from code in env_flash.c, but I think a sane
215 * default path is desirable.
220 if (gd->env_valid == 1)
225 ret = env_import((char *)ep, 0);
227 error("Cannot import environment: errno = %d\n", errno);
228 set_default_env("!env_import failed");
232 spi_flash_free(env_flash);
241 u32 saved_size, saved_offset, sector = 1;
242 char *saved_buffer = NULL;
245 #ifdef CONFIG_DM_SPI_FLASH
248 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
249 CONFIG_ENV_SPI_MAX_HZ,
250 CONFIG_ENV_SPI_MODE, &new);
252 set_default_env("!spi_flash_probe_bus_cs() failed");
256 env_flash = dev_get_uclass_priv(new);
260 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
262 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
264 set_default_env("!spi_flash_probe() failed");
270 /* Is the sector larger than the env (i.e. embedded) */
271 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
272 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
273 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
274 saved_buffer = malloc(saved_size);
278 ret = spi_flash_read(env_flash, saved_offset,
279 saved_size, saved_buffer);
284 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
285 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
286 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
290 ret = env_export(&env_new);
294 puts("Erasing SPI flash...");
295 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
296 sector * CONFIG_ENV_SECT_SIZE);
300 puts("Writing to SPI flash...");
301 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
302 CONFIG_ENV_SIZE, &env_new);
306 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
307 ret = spi_flash_write(env_flash, saved_offset,
308 saved_size, saved_buffer);
323 void env_relocate_spec(void)
328 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
329 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
330 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
332 set_default_env("!spi_flash_probe() failed");
338 ret = spi_flash_read(env_flash,
339 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
341 set_default_env("!spi_flash_read() failed");
345 ret = env_import(buf, 1);
349 spi_flash_free(env_flash);
358 /* SPI flash isn't usable before relocation */
359 gd->env_addr = (ulong)&default_environment[0];