blob: 88ec1108b681c79496ec4b383b5b6030655c3daf [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Haavard Skinnemoen8c664972008-05-16 11:10:35 +02002/*
Wolfgang Denkea882ba2010-06-20 23:33:59 +02003 * (C) Copyright 2000-2010
Haavard Skinnemoen8c664972008-05-16 11:10:35 +02004 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 *
6 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 * Andreas Heppel <aheppel@sysgo.de>
8 *
9 * (C) Copyright 2008 Atmel Corporation
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020010 */
11#include <common.h>
Simon Glass9d922452017-05-17 17:18:03 -060012#include <dm.h>
Simon Glass0ac7d722019-08-01 09:47:00 -060013#include <env.h>
Simon Glassf3998fd2019-08-02 09:44:25 -060014#include <env_internal.h>
Simon Glassb79fdc72020-05-10 11:39:54 -060015#include <flash.h>
Mike Frysinger5b3375a2008-12-11 06:23:37 -050016#include <malloc.h>
Simon Glass843c9e82014-10-13 23:41:55 -060017#include <spi.h>
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020018#include <spi_flash.h>
Wolfgang Denkea882ba2010-06-20 23:33:59 +020019#include <search.h>
20#include <errno.h>
Simon Glassb79fdc72020-05-10 11:39:54 -060021#include <uuid.h>
Simon Glass90526e92020-05-10 11:39:56 -060022#include <asm/cache.h>
Simon Glass401d1c42020-10-30 21:38:53 -060023#include <asm/global_data.h>
Gong Qianyu19c31282016-01-26 15:06:42 +080024#include <dm/device-internal.h>
Simon Glass3db71102019-11-14 12:57:16 -070025#include <u-boot/crc.h>
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020026
Simon Glass4415f1d2017-08-03 12:21:58 -060027#ifndef CONFIG_SPL_BUILD
Ashish Kumarb500c922017-12-14 17:37:08 +053028#define INITENV
Simon Glass4415f1d2017-08-03 12:21:58 -060029#endif
30
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020031#ifdef CONFIG_ENV_OFFSET_REDUND
Igor Grinbergeb58a7f2011-11-07 01:14:08 +000032static ulong env_offset = CONFIG_ENV_OFFSET;
33static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
Stefano Babica3110f02010-10-27 11:06:20 +020034#endif /* CONFIG_ENV_OFFSET_REDUND */
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020035
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020036DECLARE_GLOBAL_DATA_PTR;
37
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020038static struct spi_flash *env_flash;
39
Andreas Fenkartafa81a72017-04-08 11:59:31 +020040static int setup_flash_device(void)
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020041{
Lukasz Majewski56c40462020-06-04 23:11:53 +080042#if CONFIG_IS_ENABLED(DM_SPI_FLASH)
Gong Qianyu19c31282016-01-26 15:06:42 +080043 struct udevice *new;
Andreas Fenkartafa81a72017-04-08 11:59:31 +020044 int ret;
Gong Qianyu19c31282016-01-26 15:06:42 +080045
Vignesh R96907c02016-07-06 10:04:28 +053046 /* speed and mode will be read from DT */
Gong Qianyu19c31282016-01-26 15:06:42 +080047 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
Konstantin Porotchkin25a17652018-08-29 16:34:52 +030048 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE,
49 &new);
Gong Qianyu19c31282016-01-26 15:06:42 +080050 if (ret) {
Simon Glass0ac7d722019-08-01 09:47:00 -060051 env_set_default("spi_flash_probe_bus_cs() failed", 0);
Simon Glassc5951992017-08-03 12:22:17 -060052 return ret;
Gong Qianyu19c31282016-01-26 15:06:42 +080053 }
54
55 env_flash = dev_get_uclass_priv(new);
56#else
Jagan Tekic8cbf172020-05-13 16:32:42 +053057 if (env_flash)
58 spi_flash_free(env_flash);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020059
Jagan Tekic8cbf172020-05-13 16:32:42 +053060 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
61 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020062 if (!env_flash) {
Jagan Tekic8cbf172020-05-13 16:32:42 +053063 env_set_default("spi_flash_probe() failed", 0);
64 return -EIO;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020065 }
Gong Qianyu19c31282016-01-26 15:06:42 +080066#endif
Andreas Fenkartafa81a72017-04-08 11:59:31 +020067 return 0;
68}
69
70#if defined(CONFIG_ENV_OFFSET_REDUND)
Simon Glasse5bce242017-08-03 12:22:01 -060071static int env_sf_save(void)
Andreas Fenkartafa81a72017-04-08 11:59:31 +020072{
73 env_t env_new;
Simon Glassd3716dd2019-08-01 09:47:08 -060074 char *saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +020075 u32 saved_size, saved_offset, sector;
Andreas Fenkartafa81a72017-04-08 11:59:31 +020076 int ret;
77
78 ret = setup_flash_device();
79 if (ret)
80 return ret;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020081
Marek Vasut7ce15262014-03-05 19:59:50 +010082 ret = env_export(&env_new);
83 if (ret)
Simon Glassc5951992017-08-03 12:22:17 -060084 return -EIO;
Simon Glassd3716dd2019-08-01 09:47:08 -060085 env_new.flags = ENV_REDUND_ACTIVE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020086
Simon Glass203e94f2017-08-03 12:21:56 -060087 if (gd->env_valid == ENV_VALID) {
Stefano Babica3110f02010-10-27 11:06:20 +020088 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
89 env_offset = CONFIG_ENV_OFFSET;
90 } else {
91 env_new_offset = CONFIG_ENV_OFFSET;
92 env_offset = CONFIG_ENV_OFFSET_REDUND;
93 }
94
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020095 /* Is the sector larger than the env (i.e. embedded) */
96 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
97 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
98 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
Ravi Babu7dd01742015-08-17 13:29:49 +053099 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200100 if (!saved_buffer) {
Simon Glassc5951992017-08-03 12:22:17 -0600101 ret = -ENOMEM;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200102 goto done;
103 }
Heiko Schocher9ba5e5b2019-03-13 12:15:45 +0100104 ret = spi_flash_read(env_flash, saved_offset,
105 saved_size, saved_buffer);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200106 if (ret)
107 goto done;
108 }
109
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +0200110 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200111
112 puts("Erasing SPI flash...");
113 ret = spi_flash_erase(env_flash, env_new_offset,
114 sector * CONFIG_ENV_SECT_SIZE);
115 if (ret)
116 goto done;
117
118 puts("Writing to SPI flash...");
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200119
Stefano Babica3110f02010-10-27 11:06:20 +0200120 ret = spi_flash_write(env_flash, env_new_offset,
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400121 CONFIG_ENV_SIZE, &env_new);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200122 if (ret)
123 goto done;
124
125 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
126 ret = spi_flash_write(env_flash, saved_offset,
127 saved_size, saved_buffer);
128 if (ret)
129 goto done;
130 }
131
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000132 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400133 sizeof(env_new.flags), &flag);
Stefano Babica3110f02010-10-27 11:06:20 +0200134 if (ret)
135 goto done;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200136
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200137 puts("done\n");
138
Simon Glass203e94f2017-08-03 12:21:56 -0600139 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
Stefano Babica3110f02010-10-27 11:06:20 +0200140
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000141 printf("Valid environment: %d\n", (int)gd->env_valid);
Stefano Babica3110f02010-10-27 11:06:20 +0200142
Patrick Delaunayea5641e2021-01-04 14:41:31 +0100143done:
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200144 if (saved_buffer)
145 free(saved_buffer);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000146
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200147 return ret;
148}
149
Simon Glassc5951992017-08-03 12:22:17 -0600150static int env_sf_load(void)
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200151{
152 int ret;
Simon Goldschmidt80719932018-01-31 14:47:13 +0100153 int read1_fail, read2_fail;
154 env_t *tmp_env1, *tmp_env2;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200155
Ravi Babu7dd01742015-08-17 13:29:49 +0530156 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
157 CONFIG_ENV_SIZE);
158 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
159 CONFIG_ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200160 if (!tmp_env1 || !tmp_env2) {
Simon Glass0ac7d722019-08-01 09:47:00 -0600161 env_set_default("malloc() failed", 0);
Simon Glassc5951992017-08-03 12:22:17 -0600162 ret = -EIO;
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000163 goto out;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200164 }
165
Andreas Fenkart8fee8842017-04-08 11:59:32 +0200166 ret = setup_flash_device();
167 if (ret)
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000168 goto out;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200169
Heiko Schocher9ba5e5b2019-03-13 12:15:45 +0100170 read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
171 CONFIG_ENV_SIZE, tmp_env1);
172 read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
173 CONFIG_ENV_SIZE, tmp_env2);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200174
Simon Goldschmidt80719932018-01-31 14:47:13 +0100175 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
Marek Vasut890feec2020-07-07 20:51:35 +0200176 read2_fail, H_EXTERNAL);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200177
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200178 spi_flash_free(env_flash);
179 env_flash = NULL;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200180out:
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200181 free(tmp_env1);
182 free(tmp_env2);
Simon Glassc5951992017-08-03 12:22:17 -0600183
184 return ret;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200185}
186#else
Simon Glasse5bce242017-08-03 12:22:01 -0600187static int env_sf_save(void)
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200188{
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +0200189 u32 saved_size, saved_offset, sector;
Marek Vasut7ce15262014-03-05 19:59:50 +0100190 char *saved_buffer = NULL;
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000191 int ret = 1;
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400192 env_t env_new;
Gong Qianyu19c31282016-01-26 15:06:42 +0800193
Andreas Fenkartafa81a72017-04-08 11:59:31 +0200194 ret = setup_flash_device();
195 if (ret)
196 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200197
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500198 /* Is the sector larger than the env (i.e. embedded) */
199 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
200 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
201 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
202 saved_buffer = malloc(saved_size);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000203 if (!saved_buffer)
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500204 goto done;
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000205
Heiko Schocher9ba5e5b2019-03-13 12:15:45 +0100206 ret = spi_flash_read(env_flash, saved_offset,
207 saved_size, saved_buffer);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500208 if (ret)
209 goto done;
210 }
211
Marek Vasut7ce15262014-03-05 19:59:50 +0100212 ret = env_export(&env_new);
213 if (ret)
Stefano Babica3110f02010-10-27 11:06:20 +0200214 goto done;
Stefano Babica3110f02010-10-27 11:06:20 +0200215
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +0200216 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
217
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200218 puts("Erasing SPI flash...");
Stefano Babica3110f02010-10-27 11:06:20 +0200219 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
220 sector * CONFIG_ENV_SECT_SIZE);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500221 if (ret)
222 goto done;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200223
224 puts("Writing to SPI flash...");
Stefano Babica3110f02010-10-27 11:06:20 +0200225 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400226 CONFIG_ENV_SIZE, &env_new);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500227 if (ret)
228 goto done;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200229
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500230 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
Stefano Babica3110f02010-10-27 11:06:20 +0200231 ret = spi_flash_write(env_flash, saved_offset,
232 saved_size, saved_buffer);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500233 if (ret)
234 goto done;
235 }
236
237 ret = 0;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200238 puts("done\n");
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500239
Patrick Delaunayea5641e2021-01-04 14:41:31 +0100240done:
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500241 if (saved_buffer)
242 free(saved_buffer);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000243
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500244 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200245}
246
Simon Glassc5951992017-08-03 12:22:17 -0600247static int env_sf_load(void)
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200248{
249 int ret;
Ying Zhang5a89fa92014-01-24 15:50:07 +0800250 char *buf = NULL;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200251
Ravi Babu7dd01742015-08-17 13:29:49 +0530252 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
Andreas Fenkartc041c602017-04-08 11:59:33 +0200253 if (!buf) {
Simon Glass0ac7d722019-08-01 09:47:00 -0600254 env_set_default("malloc() failed", 0);
Simon Glassc5951992017-08-03 12:22:17 -0600255 return -EIO;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200256 }
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200257
Andreas Fenkartc041c602017-04-08 11:59:33 +0200258 ret = setup_flash_device();
259 if (ret)
260 goto out;
261
Heiko Schocher9ba5e5b2019-03-13 12:15:45 +0100262 ret = spi_flash_read(env_flash,
263 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200264 if (ret) {
Simon Glass0ac7d722019-08-01 09:47:00 -0600265 env_set_default("spi_flash_read() failed", 0);
Andreas Fenkartc041c602017-04-08 11:59:33 +0200266 goto err_read;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200267 }
268
Marek Vasut890feec2020-07-07 20:51:35 +0200269 ret = env_import(buf, 1, H_EXTERNAL);
Simon Goldschmidt42a18202018-01-31 14:47:10 +0100270 if (!ret)
Simon Glass203e94f2017-08-03 12:21:56 -0600271 gd->env_valid = ENV_VALID;
Andreas Fenkartc041c602017-04-08 11:59:33 +0200272
273err_read:
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200274 spi_flash_free(env_flash);
275 env_flash = NULL;
Andreas Fenkartc041c602017-04-08 11:59:33 +0200276out:
277 free(buf);
Simon Glassc5951992017-08-03 12:22:17 -0600278
279 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200280}
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200281#endif
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200282
Tom Rinia09fea12019-11-18 20:02:10 -0500283#if CONFIG_ENV_ADDR != 0x0
Rajesh Bhagat119c01c22018-11-05 18:01:15 +0000284__weak void *env_sf_get_env_addr(void)
285{
286 return (void *)CONFIG_ENV_ADDR;
287}
288#endif
289
Tom Rinia09fea12019-11-18 20:02:10 -0500290#if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0)
Heiko Schocher92765f452020-10-10 10:28:05 +0200291/*
292 * check if Environment on CONFIG_ENV_ADDR is valid.
293 */
294static int env_sf_init_addr(void)
Ashish Kumarb500c922017-12-14 17:37:08 +0530295{
Rajesh Bhagat119c01c22018-11-05 18:01:15 +0000296 env_t *env_ptr = (env_t *)env_sf_get_env_addr();
Ashish Kumarb500c922017-12-14 17:37:08 +0530297
298 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
299 gd->env_addr = (ulong)&(env_ptr->data);
300 gd->env_valid = 1;
301 } else {
302 gd->env_addr = (ulong)&default_environment[0];
303 gd->env_valid = 1;
304 }
305
306 return 0;
307}
308#endif
309
Heiko Schocher92765f452020-10-10 10:28:05 +0200310#if defined(CONFIG_ENV_SPI_EARLY)
311/*
312 * early load environment from SPI flash (before relocation)
313 * and check if it is valid.
314 */
315static int env_sf_init_early(void)
316{
317 int ret;
318 int read1_fail;
319 int read2_fail;
320 int crc1_ok;
321 env_t *tmp_env2 = NULL;
322 env_t *tmp_env1;
323
324 /*
325 * if malloc is not ready yet, we cannot use
326 * this part yet.
327 */
328 if (!gd->malloc_limit)
329 return -ENOENT;
330
331 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
332 CONFIG_ENV_SIZE);
333 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
334 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
335 CONFIG_ENV_SIZE);
336
337 if (!tmp_env1 || !tmp_env2)
338 goto out;
339
340 ret = setup_flash_device();
341 if (ret)
342 goto out;
343
344 read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
345 CONFIG_ENV_SIZE, tmp_env1);
346
347 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT)) {
348 read2_fail = spi_flash_read(env_flash,
349 CONFIG_ENV_OFFSET_REDUND,
350 CONFIG_ENV_SIZE, tmp_env2);
351 ret = env_check_redund((char *)tmp_env1, read1_fail,
352 (char *)tmp_env2, read2_fail);
353
354 if (ret == -EIO || ret == -ENOMSG)
355 goto err_read;
356
357 if (gd->env_valid == ENV_VALID)
358 gd->env_addr = (unsigned long)&tmp_env1->data;
359 else
360 gd->env_addr = (unsigned long)&tmp_env2->data;
361 } else {
362 if (read1_fail)
363 goto err_read;
364
365 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
366 tmp_env1->crc;
367 if (!crc1_ok)
368 goto err_read;
369
370 /* if valid -> this is our env */
371 gd->env_valid = ENV_VALID;
372 gd->env_addr = (unsigned long)&tmp_env1->data;
373 }
374
375 return 0;
376err_read:
377 spi_flash_free(env_flash);
378 env_flash = NULL;
379 free(tmp_env1);
380 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
381 free(tmp_env2);
382out:
383 /* env is not valid. always return 0 */
384 gd->env_valid = ENV_INVALID;
385 return 0;
386}
387#endif
388
389static int env_sf_init(void)
390{
391#if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0)
392 return env_sf_init_addr();
393#elif defined(CONFIG_ENV_SPI_EARLY)
394 return env_sf_init_early();
395#endif
396 /*
Heiko Schocher46ce9e72020-11-03 15:22:36 +0100397 * return here -ENOENT, so env_init()
398 * can set the init bit and later if no
399 * other Environment storage is defined
400 * can set the default environment
Heiko Schocher92765f452020-10-10 10:28:05 +0200401 */
Heiko Schocher46ce9e72020-11-03 15:22:36 +0100402 return -ENOENT;
Heiko Schocher92765f452020-10-10 10:28:05 +0200403}
404
Simon Glass4415f1d2017-08-03 12:21:58 -0600405U_BOOT_ENV_LOCATION(sf) = {
406 .location = ENVL_SPI_FLASH,
Patrick Delaunay22140d12020-07-28 11:51:16 +0200407 ENV_NAME("SPIFlash")
Simon Glasse5bce242017-08-03 12:22:01 -0600408 .load = env_sf_load,
Rasmus Villemoes6d3524c2020-03-27 00:02:00 +0100409 .save = CONFIG_IS_ENABLED(SAVEENV) ? ENV_SAVE_PTR(env_sf_save) : NULL,
Ashish Kumarb500c922017-12-14 17:37:08 +0530410 .init = env_sf_init,
Simon Glass4415f1d2017-08-03 12:21:58 -0600411};