blob: a07641b7d4973f5c35d9afec81f7a03a7447409d [file] [log] [blame]
Haavard Skinnemoen8c664972008-05-16 11:10:35 +02001/*
Wolfgang Denkea882ba2010-06-20 23:33:59 +02002 * (C) Copyright 2000-2010
Haavard Skinnemoen8c664972008-05-16 11:10:35 +02003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6 * Andreas Heppel <aheppel@sysgo.de>
7 *
8 * (C) Copyright 2008 Atmel Corporation
9 *
Jagannadha Sutradharudu Teki402ba1e2013-07-29 22:12:52 +053010 * SPDX-License-Identifier: GPL-2.0+
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020011 */
12#include <common.h>
Simon Glass9d922452017-05-17 17:18:03 -060013#include <dm.h>
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020014#include <environment.h>
Mike Frysinger5b3375a2008-12-11 06:23:37 -050015#include <malloc.h>
Simon Glass843c9e82014-10-13 23:41:55 -060016#include <spi.h>
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020017#include <spi_flash.h>
Wolfgang Denkea882ba2010-06-20 23:33:59 +020018#include <search.h>
19#include <errno.h>
Gong Qianyu19c31282016-01-26 15:06:42 +080020#include <dm/device-internal.h>
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020021
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020022#ifndef CONFIG_ENV_SPI_BUS
Jean-Jacques Hiblot2e4e5ad2017-02-01 14:51:47 +010023# define CONFIG_ENV_SPI_BUS CONFIG_SF_DEFAULT_BUS
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020024#endif
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020025#ifndef CONFIG_ENV_SPI_CS
Jean-Jacques Hiblot2e4e5ad2017-02-01 14:51:47 +010026# define CONFIG_ENV_SPI_CS CONFIG_SF_DEFAULT_CS
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020027#endif
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020028#ifndef CONFIG_ENV_SPI_MAX_HZ
Jean-Jacques Hiblot2e4e5ad2017-02-01 14:51:47 +010029# define CONFIG_ENV_SPI_MAX_HZ CONFIG_SF_DEFAULT_SPEED
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020030#endif
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020031#ifndef CONFIG_ENV_SPI_MODE
Jean-Jacques Hiblot2e4e5ad2017-02-01 14:51:47 +010032# define CONFIG_ENV_SPI_MODE CONFIG_SF_DEFAULT_MODE
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020033#endif
34
Simon Glass4415f1d2017-08-03 12:21:58 -060035#ifndef CONFIG_SPL_BUILD
36#define CMD_SAVEENV
37#endif
38
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020039#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass4415f1d2017-08-03 12:21:58 -060040#ifdef CMD_SAVEENV
Igor Grinbergeb58a7f2011-11-07 01:14:08 +000041static ulong env_offset = CONFIG_ENV_OFFSET;
42static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
Simon Glass4415f1d2017-08-03 12:21:58 -060043#endif
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020044
Igor Grinbergeb58a7f2011-11-07 01:14:08 +000045#define ACTIVE_FLAG 1
46#define OBSOLETE_FLAG 0
Stefano Babica3110f02010-10-27 11:06:20 +020047#endif /* CONFIG_ENV_OFFSET_REDUND */
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020048
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020049DECLARE_GLOBAL_DATA_PTR;
50
Igor Grinbergeb58a7f2011-11-07 01:14:08 +000051char *env_name_spec = "SPI Flash";
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020052
53static struct spi_flash *env_flash;
54
Andreas Fenkartafa81a72017-04-08 11:59:31 +020055static int setup_flash_device(void)
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020056{
Gong Qianyu19c31282016-01-26 15:06:42 +080057#ifdef CONFIG_DM_SPI_FLASH
58 struct udevice *new;
Andreas Fenkartafa81a72017-04-08 11:59:31 +020059 int ret;
Gong Qianyu19c31282016-01-26 15:06:42 +080060
Vignesh R96907c02016-07-06 10:04:28 +053061 /* speed and mode will be read from DT */
Gong Qianyu19c31282016-01-26 15:06:42 +080062 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
Vignesh R96907c02016-07-06 10:04:28 +053063 0, 0, &new);
Gong Qianyu19c31282016-01-26 15:06:42 +080064 if (ret) {
65 set_default_env("!spi_flash_probe_bus_cs() failed");
66 return 1;
67 }
68
69 env_flash = dev_get_uclass_priv(new);
70#else
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020071
72 if (!env_flash) {
Stefano Babica3110f02010-10-27 11:06:20 +020073 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
74 CONFIG_ENV_SPI_CS,
75 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
76 if (!env_flash) {
77 set_default_env("!spi_flash_probe() failed");
78 return 1;
79 }
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020080 }
Gong Qianyu19c31282016-01-26 15:06:42 +080081#endif
Andreas Fenkartafa81a72017-04-08 11:59:31 +020082 return 0;
83}
84
85#if defined(CONFIG_ENV_OFFSET_REDUND)
Simon Glass4415f1d2017-08-03 12:21:58 -060086#ifdef CMD_SAVEENV
Simon Glasse5bce242017-08-03 12:22:01 -060087static int env_sf_save(void)
Andreas Fenkartafa81a72017-04-08 11:59:31 +020088{
89 env_t env_new;
90 char *saved_buffer = NULL, flag = OBSOLETE_FLAG;
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +020091 u32 saved_size, saved_offset, sector;
Andreas Fenkartafa81a72017-04-08 11:59:31 +020092 int ret;
93
94 ret = setup_flash_device();
95 if (ret)
96 return ret;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020097
Marek Vasut7ce15262014-03-05 19:59:50 +010098 ret = env_export(&env_new);
99 if (ret)
100 return ret;
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400101 env_new.flags = ACTIVE_FLAG;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200102
Simon Glass203e94f2017-08-03 12:21:56 -0600103 if (gd->env_valid == ENV_VALID) {
Stefano Babica3110f02010-10-27 11:06:20 +0200104 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
105 env_offset = CONFIG_ENV_OFFSET;
106 } else {
107 env_new_offset = CONFIG_ENV_OFFSET;
108 env_offset = CONFIG_ENV_OFFSET_REDUND;
109 }
110
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200111 /* Is the sector larger than the env (i.e. embedded) */
112 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
113 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
114 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
Ravi Babu7dd01742015-08-17 13:29:49 +0530115 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200116 if (!saved_buffer) {
117 ret = 1;
118 goto done;
119 }
120 ret = spi_flash_read(env_flash, saved_offset,
121 saved_size, saved_buffer);
122 if (ret)
123 goto done;
124 }
125
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +0200126 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200127
128 puts("Erasing SPI flash...");
129 ret = spi_flash_erase(env_flash, env_new_offset,
130 sector * CONFIG_ENV_SECT_SIZE);
131 if (ret)
132 goto done;
133
134 puts("Writing to SPI flash...");
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200135
Stefano Babica3110f02010-10-27 11:06:20 +0200136 ret = spi_flash_write(env_flash, env_new_offset,
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400137 CONFIG_ENV_SIZE, &env_new);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200138 if (ret)
139 goto done;
140
141 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
142 ret = spi_flash_write(env_flash, saved_offset,
143 saved_size, saved_buffer);
144 if (ret)
145 goto done;
146 }
147
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000148 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400149 sizeof(env_new.flags), &flag);
Stefano Babica3110f02010-10-27 11:06:20 +0200150 if (ret)
151 goto done;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200152
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200153 puts("done\n");
154
Simon Glass203e94f2017-08-03 12:21:56 -0600155 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
Stefano Babica3110f02010-10-27 11:06:20 +0200156
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000157 printf("Valid environment: %d\n", (int)gd->env_valid);
Stefano Babica3110f02010-10-27 11:06:20 +0200158
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200159 done:
160 if (saved_buffer)
161 free(saved_buffer);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000162
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200163 return ret;
164}
Simon Glass4415f1d2017-08-03 12:21:58 -0600165#endif /* CMD_SAVEENV */
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200166
Simon Glasse5bce242017-08-03 12:22:01 -0600167static void env_sf_load(void)
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200168{
169 int ret;
170 int crc1_ok = 0, crc2_ok = 0;
171 env_t *tmp_env1 = NULL;
172 env_t *tmp_env2 = NULL;
Stefano Babica3110f02010-10-27 11:06:20 +0200173 env_t *ep = NULL;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200174
Ravi Babu7dd01742015-08-17 13:29:49 +0530175 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
176 CONFIG_ENV_SIZE);
177 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
178 CONFIG_ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200179 if (!tmp_env1 || !tmp_env2) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200180 set_default_env("!malloc() failed");
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000181 goto out;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200182 }
183
Andreas Fenkart8fee8842017-04-08 11:59:32 +0200184 ret = setup_flash_device();
185 if (ret)
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000186 goto out;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200187
188 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
189 CONFIG_ENV_SIZE, tmp_env1);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200190 if (ret) {
191 set_default_env("!spi_flash_read() failed");
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200192 goto err_read;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200193 }
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200194
195 if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc)
196 crc1_ok = 1;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200197
198 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
199 CONFIG_ENV_SIZE, tmp_env2);
200 if (!ret) {
201 if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc)
202 crc2_ok = 1;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200203 }
204
Stefano Babica3110f02010-10-27 11:06:20 +0200205 if (!crc1_ok && !crc2_ok) {
Stefano Babica3110f02010-10-27 11:06:20 +0200206 set_default_env("!bad CRC");
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000207 goto err_read;
Stefano Babica3110f02010-10-27 11:06:20 +0200208 } else if (crc1_ok && !crc2_ok) {
Simon Glass203e94f2017-08-03 12:21:56 -0600209 gd->env_valid = ENV_VALID;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200210 } else if (!crc1_ok && crc2_ok) {
Simon Glass203e94f2017-08-03 12:21:56 -0600211 gd->env_valid = ENV_REDUND;
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000212 } else if (tmp_env1->flags == ACTIVE_FLAG &&
213 tmp_env2->flags == OBSOLETE_FLAG) {
Simon Glass203e94f2017-08-03 12:21:56 -0600214 gd->env_valid = ENV_VALID;
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000215 } else if (tmp_env1->flags == OBSOLETE_FLAG &&
216 tmp_env2->flags == ACTIVE_FLAG) {
Simon Glass203e94f2017-08-03 12:21:56 -0600217 gd->env_valid = ENV_REDUND;
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000218 } else if (tmp_env1->flags == tmp_env2->flags) {
Simon Glass203e94f2017-08-03 12:21:56 -0600219 gd->env_valid = ENV_VALID;
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000220 } else if (tmp_env1->flags == 0xFF) {
Simon Glass203e94f2017-08-03 12:21:56 -0600221 gd->env_valid = ENV_VALID;
Mario Schuknecht5e8397d2015-03-24 08:59:00 +0100222 } else if (tmp_env2->flags == 0xFF) {
Simon Glass203e94f2017-08-03 12:21:56 -0600223 gd->env_valid = ENV_REDUND;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200224 } else {
225 /*
226 * this differs from code in env_flash.c, but I think a sane
227 * default path is desirable.
228 */
Simon Glass203e94f2017-08-03 12:21:56 -0600229 gd->env_valid = ENV_VALID;
Stefano Babica3110f02010-10-27 11:06:20 +0200230 }
231
Simon Glass203e94f2017-08-03 12:21:56 -0600232 if (gd->env_valid == ENV_VALID)
Stefano Babica3110f02010-10-27 11:06:20 +0200233 ep = tmp_env1;
234 else
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200235 ep = tmp_env2;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200236
Stefano Babica3110f02010-10-27 11:06:20 +0200237 ret = env_import((char *)ep, 0);
238 if (!ret) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200239 error("Cannot import environment: errno = %d\n", errno);
Peng Fan9a5d9932016-03-28 17:26:27 +0800240 set_default_env("!env_import failed");
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200241 }
242
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200243err_read:
244 spi_flash_free(env_flash);
245 env_flash = NULL;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200246out:
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200247 free(tmp_env1);
248 free(tmp_env2);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200249}
250#else
Simon Glass4415f1d2017-08-03 12:21:58 -0600251#ifdef CMD_SAVEENV
Simon Glasse5bce242017-08-03 12:22:01 -0600252static int env_sf_save(void)
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200253{
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +0200254 u32 saved_size, saved_offset, sector;
Marek Vasut7ce15262014-03-05 19:59:50 +0100255 char *saved_buffer = NULL;
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000256 int ret = 1;
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400257 env_t env_new;
Gong Qianyu19c31282016-01-26 15:06:42 +0800258
Andreas Fenkartafa81a72017-04-08 11:59:31 +0200259 ret = setup_flash_device();
260 if (ret)
261 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200262
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500263 /* Is the sector larger than the env (i.e. embedded) */
264 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
265 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
266 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
267 saved_buffer = malloc(saved_size);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000268 if (!saved_buffer)
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500269 goto done;
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000270
Stefano Babica3110f02010-10-27 11:06:20 +0200271 ret = spi_flash_read(env_flash, saved_offset,
272 saved_size, saved_buffer);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500273 if (ret)
274 goto done;
275 }
276
Marek Vasut7ce15262014-03-05 19:59:50 +0100277 ret = env_export(&env_new);
278 if (ret)
Stefano Babica3110f02010-10-27 11:06:20 +0200279 goto done;
Stefano Babica3110f02010-10-27 11:06:20 +0200280
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +0200281 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
282
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200283 puts("Erasing SPI flash...");
Stefano Babica3110f02010-10-27 11:06:20 +0200284 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
285 sector * CONFIG_ENV_SECT_SIZE);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500286 if (ret)
287 goto done;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200288
289 puts("Writing to SPI flash...");
Stefano Babica3110f02010-10-27 11:06:20 +0200290 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400291 CONFIG_ENV_SIZE, &env_new);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500292 if (ret)
293 goto done;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200294
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500295 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
Stefano Babica3110f02010-10-27 11:06:20 +0200296 ret = spi_flash_write(env_flash, saved_offset,
297 saved_size, saved_buffer);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500298 if (ret)
299 goto done;
300 }
301
302 ret = 0;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200303 puts("done\n");
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500304
305 done:
306 if (saved_buffer)
307 free(saved_buffer);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000308
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500309 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200310}
Simon Glass4415f1d2017-08-03 12:21:58 -0600311#endif /* CMD_SAVEENV */
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200312
Simon Glasse5bce242017-08-03 12:22:01 -0600313static void env_sf_load(void)
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200314{
315 int ret;
Ying Zhang5a89fa92014-01-24 15:50:07 +0800316 char *buf = NULL;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200317
Ravi Babu7dd01742015-08-17 13:29:49 +0530318 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
Andreas Fenkartc041c602017-04-08 11:59:33 +0200319 if (!buf) {
320 set_default_env("!malloc() failed");
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200321 return;
322 }
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200323
Andreas Fenkartc041c602017-04-08 11:59:33 +0200324 ret = setup_flash_device();
325 if (ret)
326 goto out;
327
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200328 ret = spi_flash_read(env_flash,
329 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
330 if (ret) {
331 set_default_env("!spi_flash_read() failed");
Andreas Fenkartc041c602017-04-08 11:59:33 +0200332 goto err_read;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200333 }
334
335 ret = env_import(buf, 1);
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200336 if (ret)
Simon Glass203e94f2017-08-03 12:21:56 -0600337 gd->env_valid = ENV_VALID;
Andreas Fenkartc041c602017-04-08 11:59:33 +0200338
339err_read:
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200340 spi_flash_free(env_flash);
341 env_flash = NULL;
Andreas Fenkartc041c602017-04-08 11:59:33 +0200342out:
343 free(buf);
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200344}
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200345#endif
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200346
Simon Glass4415f1d2017-08-03 12:21:58 -0600347U_BOOT_ENV_LOCATION(sf) = {
348 .location = ENVL_SPI_FLASH,
Simon Glasse5bce242017-08-03 12:22:01 -0600349 .load = env_sf_load,
Simon Glass4415f1d2017-08-03 12:21:58 -0600350#ifdef CMD_SAVEENV
Simon Glasse5bce242017-08-03 12:22:01 -0600351 .save = env_save_ptr(env_sf_save),
Simon Glass4415f1d2017-08-03 12:21:58 -0600352#endif
Simon Glass4415f1d2017-08-03 12:21:58 -0600353};