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
27 #ifdef CONFIG_ENV_OFFSET_REDUND
28 static ulong env_offset = CONFIG_ENV_OFFSET;
29 static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
30 #endif /* CONFIG_ENV_OFFSET_REDUND */
32 DECLARE_GLOBAL_DATA_PTR;
34 static struct spi_flash *env_flash;
36 static int setup_flash_device(void)
38 #ifdef CONFIG_DM_SPI_FLASH
42 /* speed and mode will be read from DT */
43 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
44 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE,
47 env_set_default("spi_flash_probe_bus_cs() failed", 0);
51 env_flash = dev_get_uclass_priv(new);
55 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
57 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
59 env_set_default("spi_flash_probe() failed", 0);
67 #if defined(CONFIG_ENV_OFFSET_REDUND)
68 static int env_sf_save(void)
71 char *saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
72 u32 saved_size, saved_offset, sector;
75 ret = setup_flash_device();
79 ret = env_export(&env_new);
82 env_new.flags = ENV_REDUND_ACTIVE;
84 if (gd->env_valid == ENV_VALID) {
85 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
86 env_offset = CONFIG_ENV_OFFSET;
88 env_new_offset = CONFIG_ENV_OFFSET;
89 env_offset = CONFIG_ENV_OFFSET_REDUND;
92 /* Is the sector larger than the env (i.e. embedded) */
93 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
94 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
95 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
96 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
101 ret = spi_flash_read(env_flash, saved_offset,
102 saved_size, saved_buffer);
107 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
109 puts("Erasing SPI flash...");
110 ret = spi_flash_erase(env_flash, env_new_offset,
111 sector * CONFIG_ENV_SECT_SIZE);
115 puts("Writing to SPI flash...");
117 ret = spi_flash_write(env_flash, env_new_offset,
118 CONFIG_ENV_SIZE, &env_new);
122 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
123 ret = spi_flash_write(env_flash, saved_offset,
124 saved_size, saved_buffer);
129 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
130 sizeof(env_new.flags), &flag);
136 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
138 printf("Valid environment: %d\n", (int)gd->env_valid);
147 static int env_sf_load(void)
150 int read1_fail, read2_fail;
151 env_t *tmp_env1, *tmp_env2;
153 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
155 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
157 if (!tmp_env1 || !tmp_env2) {
158 env_set_default("malloc() failed", 0);
163 ret = setup_flash_device();
167 read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
168 CONFIG_ENV_SIZE, tmp_env1);
169 read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
170 CONFIG_ENV_SIZE, tmp_env2);
172 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
175 spi_flash_free(env_flash);
184 static int env_sf_save(void)
186 u32 saved_size, saved_offset, sector;
187 char *saved_buffer = NULL;
191 ret = setup_flash_device();
195 /* Is the sector larger than the env (i.e. embedded) */
196 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
197 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
198 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
199 saved_buffer = malloc(saved_size);
203 ret = spi_flash_read(env_flash, saved_offset,
204 saved_size, saved_buffer);
209 ret = env_export(&env_new);
213 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
215 puts("Erasing SPI flash...");
216 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
217 sector * CONFIG_ENV_SECT_SIZE);
221 puts("Writing to SPI flash...");
222 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
223 CONFIG_ENV_SIZE, &env_new);
227 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
228 ret = spi_flash_write(env_flash, saved_offset,
229 saved_size, saved_buffer);
244 static int env_sf_load(void)
249 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
251 env_set_default("malloc() failed", 0);
255 ret = setup_flash_device();
259 ret = spi_flash_read(env_flash,
260 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
262 env_set_default("spi_flash_read() failed", 0);
266 ret = env_import(buf, 1);
268 gd->env_valid = ENV_VALID;
271 spi_flash_free(env_flash);
280 #if CONFIG_ENV_ADDR != 0x0
281 __weak void *env_sf_get_env_addr(void)
283 return (void *)CONFIG_ENV_ADDR;
287 #if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0)
288 static int env_sf_init(void)
290 env_t *env_ptr = (env_t *)env_sf_get_env_addr();
292 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
293 gd->env_addr = (ulong)&(env_ptr->data);
296 gd->env_addr = (ulong)&default_environment[0];
304 U_BOOT_ENV_LOCATION(sf) = {
305 .location = ENVL_SPI_FLASH,
306 ENV_NAME("SPI Flash")
308 .save = ENV_SAVE_PTR(env_sf_save),
309 #if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0)