1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2010
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 * Andreas Heppel <aheppel@sysgo.de>
9 * (C) Copyright 2008 Atmel Corporation
14 #include <env_internal.h>
17 #include <spi_flash.h>
20 #include <dm/device-internal.h>
21 #include <u-boot/crc.h>
23 #ifndef CONFIG_SPL_BUILD
28 #ifdef CONFIG_ENV_OFFSET_REDUND
30 static ulong env_offset = CONFIG_ENV_OFFSET;
31 static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
34 #endif /* CONFIG_ENV_OFFSET_REDUND */
36 DECLARE_GLOBAL_DATA_PTR;
38 static struct spi_flash *env_flash;
40 static int setup_flash_device(void)
42 #ifdef CONFIG_DM_SPI_FLASH
46 /* speed and mode will be read from DT */
47 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
48 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE,
51 env_set_default("spi_flash_probe_bus_cs() failed", 0);
55 env_flash = dev_get_uclass_priv(new);
59 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
61 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
63 env_set_default("spi_flash_probe() failed", 0);
71 #if defined(CONFIG_ENV_OFFSET_REDUND)
73 static int env_sf_save(void)
76 char *saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
77 u32 saved_size, saved_offset, sector;
80 ret = setup_flash_device();
84 ret = env_export(&env_new);
87 env_new.flags = ENV_REDUND_ACTIVE;
89 if (gd->env_valid == ENV_VALID) {
90 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
91 env_offset = CONFIG_ENV_OFFSET;
93 env_new_offset = CONFIG_ENV_OFFSET;
94 env_offset = CONFIG_ENV_OFFSET_REDUND;
97 /* Is the sector larger than the env (i.e. embedded) */
98 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
99 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
100 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
101 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
106 ret = spi_flash_read(env_flash, saved_offset,
107 saved_size, saved_buffer);
112 sector = DIV_ROUND_UP(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 == ENV_REDUND ? ENV_VALID : ENV_REDUND;
143 printf("Valid environment: %d\n", (int)gd->env_valid);
151 #endif /* CMD_SAVEENV */
153 static int env_sf_load(void)
156 int read1_fail, read2_fail;
157 env_t *tmp_env1, *tmp_env2;
159 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
161 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
163 if (!tmp_env1 || !tmp_env2) {
164 env_set_default("malloc() failed", 0);
169 ret = setup_flash_device();
173 read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
174 CONFIG_ENV_SIZE, tmp_env1);
175 read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
176 CONFIG_ENV_SIZE, tmp_env2);
178 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
181 spi_flash_free(env_flash);
191 static int env_sf_save(void)
193 u32 saved_size, saved_offset, sector;
194 char *saved_buffer = NULL;
198 ret = setup_flash_device();
202 /* Is the sector larger than the env (i.e. embedded) */
203 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
204 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
205 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
206 saved_buffer = malloc(saved_size);
210 ret = spi_flash_read(env_flash, saved_offset,
211 saved_size, saved_buffer);
216 ret = env_export(&env_new);
220 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
222 puts("Erasing SPI flash...");
223 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
224 sector * CONFIG_ENV_SECT_SIZE);
228 puts("Writing to SPI flash...");
229 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
230 CONFIG_ENV_SIZE, &env_new);
234 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
235 ret = spi_flash_write(env_flash, saved_offset,
236 saved_size, saved_buffer);
250 #endif /* CMD_SAVEENV */
252 static int env_sf_load(void)
257 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
259 env_set_default("malloc() failed", 0);
263 ret = setup_flash_device();
267 ret = spi_flash_read(env_flash,
268 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
270 env_set_default("spi_flash_read() failed", 0);
274 ret = env_import(buf, 1);
276 gd->env_valid = ENV_VALID;
279 spi_flash_free(env_flash);
288 #if CONFIG_ENV_ADDR != 0x0
289 __weak void *env_sf_get_env_addr(void)
291 return (void *)CONFIG_ENV_ADDR;
295 #if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0)
296 static int env_sf_init(void)
298 env_t *env_ptr = (env_t *)env_sf_get_env_addr();
300 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
301 gd->env_addr = (ulong)&(env_ptr->data);
304 gd->env_addr = (ulong)&default_environment[0];
312 U_BOOT_ENV_LOCATION(sf) = {
313 .location = ENVL_SPI_FLASH,
314 ENV_NAME("SPI Flash")
317 .save = env_save_ptr(env_sf_save),
319 #if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0)