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>
18 #include <spi_flash.h>
22 #include <asm/cache.h>
23 #include <dm/device-internal.h>
24 #include <u-boot/crc.h>
26 #ifndef CONFIG_SPL_BUILD
30 #ifdef CONFIG_ENV_OFFSET_REDUND
31 static ulong env_offset = CONFIG_ENV_OFFSET;
32 static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
33 #endif /* CONFIG_ENV_OFFSET_REDUND */
35 DECLARE_GLOBAL_DATA_PTR;
37 static struct spi_flash *env_flash;
39 static int setup_flash_device(void)
41 #ifdef CONFIG_DM_SPI_FLASH
45 /* speed and mode will be read from DT */
46 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
47 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE,
50 env_set_default("spi_flash_probe_bus_cs() failed", 0);
54 env_flash = dev_get_uclass_priv(new);
57 spi_flash_free(env_flash);
59 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
60 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
62 env_set_default("spi_flash_probe() failed", 0);
69 #if defined(CONFIG_ENV_OFFSET_REDUND)
70 static int env_sf_save(void)
73 char *saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
74 u32 saved_size, saved_offset, sector;
77 ret = setup_flash_device();
81 ret = env_export(&env_new);
84 env_new.flags = ENV_REDUND_ACTIVE;
86 if (gd->env_valid == ENV_VALID) {
87 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
88 env_offset = CONFIG_ENV_OFFSET;
90 env_new_offset = CONFIG_ENV_OFFSET;
91 env_offset = CONFIG_ENV_OFFSET_REDUND;
94 /* Is the sector larger than the env (i.e. embedded) */
95 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
96 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
97 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
98 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
103 ret = spi_flash_read(env_flash, saved_offset,
104 saved_size, saved_buffer);
109 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
111 puts("Erasing SPI flash...");
112 ret = spi_flash_erase(env_flash, env_new_offset,
113 sector * CONFIG_ENV_SECT_SIZE);
117 puts("Writing to SPI flash...");
119 ret = spi_flash_write(env_flash, env_new_offset,
120 CONFIG_ENV_SIZE, &env_new);
124 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
125 ret = spi_flash_write(env_flash, saved_offset,
126 saved_size, saved_buffer);
131 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
132 sizeof(env_new.flags), &flag);
138 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
140 printf("Valid environment: %d\n", (int)gd->env_valid);
149 static int env_sf_load(void)
152 int read1_fail, read2_fail;
153 env_t *tmp_env1, *tmp_env2;
155 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
157 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
159 if (!tmp_env1 || !tmp_env2) {
160 env_set_default("malloc() failed", 0);
165 ret = setup_flash_device();
169 read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
170 CONFIG_ENV_SIZE, tmp_env1);
171 read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
172 CONFIG_ENV_SIZE, tmp_env2);
174 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
177 spi_flash_free(env_flash);
186 static int env_sf_save(void)
188 u32 saved_size, saved_offset, sector;
189 char *saved_buffer = NULL;
193 ret = setup_flash_device();
197 /* Is the sector larger than the env (i.e. embedded) */
198 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
199 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
200 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
201 saved_buffer = malloc(saved_size);
205 ret = spi_flash_read(env_flash, saved_offset,
206 saved_size, saved_buffer);
211 ret = env_export(&env_new);
215 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
217 puts("Erasing SPI flash...");
218 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
219 sector * CONFIG_ENV_SECT_SIZE);
223 puts("Writing to SPI flash...");
224 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
225 CONFIG_ENV_SIZE, &env_new);
229 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
230 ret = spi_flash_write(env_flash, saved_offset,
231 saved_size, saved_buffer);
246 static int env_sf_load(void)
251 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
253 env_set_default("malloc() failed", 0);
257 ret = setup_flash_device();
261 ret = spi_flash_read(env_flash,
262 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
264 env_set_default("spi_flash_read() failed", 0);
268 ret = env_import(buf, 1);
270 gd->env_valid = ENV_VALID;
273 spi_flash_free(env_flash);
282 #if CONFIG_ENV_ADDR != 0x0
283 __weak void *env_sf_get_env_addr(void)
285 return (void *)CONFIG_ENV_ADDR;
289 #if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0)
290 static int env_sf_init(void)
292 env_t *env_ptr = (env_t *)env_sf_get_env_addr();
294 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
295 gd->env_addr = (ulong)&(env_ptr->data);
298 gd->env_addr = (ulong)&default_environment[0];
306 U_BOOT_ENV_LOCATION(sf) = {
307 .location = ENVL_SPI_FLASH,
308 ENV_NAME("SPI Flash")
310 .save = CONFIG_IS_ENABLED(SAVEENV) ? ENV_SAVE_PTR(env_sf_save) : NULL,
311 #if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0)