blob: e51b1ae189164f9a0247ca5f2b12b6d7c1776600 [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
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020051static struct spi_flash *env_flash;
52
Andreas Fenkartafa81a72017-04-08 11:59:31 +020053static int setup_flash_device(void)
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020054{
Gong Qianyu19c31282016-01-26 15:06:42 +080055#ifdef CONFIG_DM_SPI_FLASH
56 struct udevice *new;
Andreas Fenkartafa81a72017-04-08 11:59:31 +020057 int ret;
Gong Qianyu19c31282016-01-26 15:06:42 +080058
Vignesh R96907c02016-07-06 10:04:28 +053059 /* speed and mode will be read from DT */
Gong Qianyu19c31282016-01-26 15:06:42 +080060 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
Vignesh R96907c02016-07-06 10:04:28 +053061 0, 0, &new);
Gong Qianyu19c31282016-01-26 15:06:42 +080062 if (ret) {
63 set_default_env("!spi_flash_probe_bus_cs() failed");
Simon Glassc5951992017-08-03 12:22:17 -060064 return ret;
Gong Qianyu19c31282016-01-26 15:06:42 +080065 }
66
67 env_flash = dev_get_uclass_priv(new);
68#else
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020069
70 if (!env_flash) {
Stefano Babica3110f02010-10-27 11:06:20 +020071 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
72 CONFIG_ENV_SPI_CS,
73 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
74 if (!env_flash) {
75 set_default_env("!spi_flash_probe() failed");
Simon Glassc5951992017-08-03 12:22:17 -060076 return -EIO;
Stefano Babica3110f02010-10-27 11:06:20 +020077 }
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020078 }
Gong Qianyu19c31282016-01-26 15:06:42 +080079#endif
Andreas Fenkartafa81a72017-04-08 11:59:31 +020080 return 0;
81}
82
83#if defined(CONFIG_ENV_OFFSET_REDUND)
Simon Glass4415f1d2017-08-03 12:21:58 -060084#ifdef CMD_SAVEENV
Simon Glasse5bce242017-08-03 12:22:01 -060085static int env_sf_save(void)
Andreas Fenkartafa81a72017-04-08 11:59:31 +020086{
87 env_t env_new;
88 char *saved_buffer = NULL, flag = OBSOLETE_FLAG;
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +020089 u32 saved_size, saved_offset, sector;
Andreas Fenkartafa81a72017-04-08 11:59:31 +020090 int ret;
91
92 ret = setup_flash_device();
93 if (ret)
94 return ret;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020095
Marek Vasut7ce15262014-03-05 19:59:50 +010096 ret = env_export(&env_new);
97 if (ret)
Simon Glassc5951992017-08-03 12:22:17 -060098 return -EIO;
Tom Rinicd0f4fa2013-04-05 14:55:21 -040099 env_new.flags = ACTIVE_FLAG;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200100
Simon Glass203e94f2017-08-03 12:21:56 -0600101 if (gd->env_valid == ENV_VALID) {
Stefano Babica3110f02010-10-27 11:06:20 +0200102 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
103 env_offset = CONFIG_ENV_OFFSET;
104 } else {
105 env_new_offset = CONFIG_ENV_OFFSET;
106 env_offset = CONFIG_ENV_OFFSET_REDUND;
107 }
108
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200109 /* Is the sector larger than the env (i.e. embedded) */
110 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
111 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
112 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
Ravi Babu7dd01742015-08-17 13:29:49 +0530113 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200114 if (!saved_buffer) {
Simon Glassc5951992017-08-03 12:22:17 -0600115 ret = -ENOMEM;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200116 goto done;
117 }
118 ret = spi_flash_read(env_flash, saved_offset,
119 saved_size, saved_buffer);
120 if (ret)
121 goto done;
122 }
123
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +0200124 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200125
126 puts("Erasing SPI flash...");
127 ret = spi_flash_erase(env_flash, env_new_offset,
128 sector * CONFIG_ENV_SECT_SIZE);
129 if (ret)
130 goto done;
131
132 puts("Writing to SPI flash...");
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200133
Stefano Babica3110f02010-10-27 11:06:20 +0200134 ret = spi_flash_write(env_flash, env_new_offset,
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400135 CONFIG_ENV_SIZE, &env_new);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200136 if (ret)
137 goto done;
138
139 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
140 ret = spi_flash_write(env_flash, saved_offset,
141 saved_size, saved_buffer);
142 if (ret)
143 goto done;
144 }
145
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000146 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400147 sizeof(env_new.flags), &flag);
Stefano Babica3110f02010-10-27 11:06:20 +0200148 if (ret)
149 goto done;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200150
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200151 puts("done\n");
152
Simon Glass203e94f2017-08-03 12:21:56 -0600153 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
Stefano Babica3110f02010-10-27 11:06:20 +0200154
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000155 printf("Valid environment: %d\n", (int)gd->env_valid);
Stefano Babica3110f02010-10-27 11:06:20 +0200156
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200157 done:
158 if (saved_buffer)
159 free(saved_buffer);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000160
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200161 return ret;
162}
Simon Glass4415f1d2017-08-03 12:21:58 -0600163#endif /* CMD_SAVEENV */
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200164
Simon Glassc5951992017-08-03 12:22:17 -0600165static int env_sf_load(void)
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200166{
167 int ret;
168 int crc1_ok = 0, crc2_ok = 0;
169 env_t *tmp_env1 = NULL;
170 env_t *tmp_env2 = NULL;
Stefano Babica3110f02010-10-27 11:06:20 +0200171 env_t *ep = NULL;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200172
Ravi Babu7dd01742015-08-17 13:29:49 +0530173 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
174 CONFIG_ENV_SIZE);
175 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
176 CONFIG_ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200177 if (!tmp_env1 || !tmp_env2) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200178 set_default_env("!malloc() failed");
Simon Glassc5951992017-08-03 12:22:17 -0600179 ret = -EIO;
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000180 goto out;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200181 }
182
Andreas Fenkart8fee8842017-04-08 11:59:32 +0200183 ret = setup_flash_device();
184 if (ret)
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000185 goto out;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200186
187 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
188 CONFIG_ENV_SIZE, tmp_env1);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200189 if (ret) {
190 set_default_env("!spi_flash_read() failed");
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200191 goto err_read;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200192 }
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200193
194 if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc)
195 crc1_ok = 1;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200196
197 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
198 CONFIG_ENV_SIZE, tmp_env2);
199 if (!ret) {
200 if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc)
201 crc2_ok = 1;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200202 }
203
Stefano Babica3110f02010-10-27 11:06:20 +0200204 if (!crc1_ok && !crc2_ok) {
Stefano Babica3110f02010-10-27 11:06:20 +0200205 set_default_env("!bad CRC");
Simon Glassc5951992017-08-03 12:22:17 -0600206 ret = -EIO;
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) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900239 pr_err("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);
Simon Glassc5951992017-08-03 12:22:17 -0600249
250 return ret;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200251}
252#else
Simon Glass4415f1d2017-08-03 12:21:58 -0600253#ifdef CMD_SAVEENV
Simon Glasse5bce242017-08-03 12:22:01 -0600254static int env_sf_save(void)
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200255{
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +0200256 u32 saved_size, saved_offset, sector;
Marek Vasut7ce15262014-03-05 19:59:50 +0100257 char *saved_buffer = NULL;
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000258 int ret = 1;
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400259 env_t env_new;
Gong Qianyu19c31282016-01-26 15:06:42 +0800260
Andreas Fenkartafa81a72017-04-08 11:59:31 +0200261 ret = setup_flash_device();
262 if (ret)
263 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200264
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500265 /* Is the sector larger than the env (i.e. embedded) */
266 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
267 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
268 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
269 saved_buffer = malloc(saved_size);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000270 if (!saved_buffer)
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500271 goto done;
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000272
Stefano Babica3110f02010-10-27 11:06:20 +0200273 ret = spi_flash_read(env_flash, saved_offset,
274 saved_size, saved_buffer);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500275 if (ret)
276 goto done;
277 }
278
Marek Vasut7ce15262014-03-05 19:59:50 +0100279 ret = env_export(&env_new);
280 if (ret)
Stefano Babica3110f02010-10-27 11:06:20 +0200281 goto done;
Stefano Babica3110f02010-10-27 11:06:20 +0200282
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +0200283 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
284
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200285 puts("Erasing SPI flash...");
Stefano Babica3110f02010-10-27 11:06:20 +0200286 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
287 sector * CONFIG_ENV_SECT_SIZE);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500288 if (ret)
289 goto done;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200290
291 puts("Writing to SPI flash...");
Stefano Babica3110f02010-10-27 11:06:20 +0200292 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400293 CONFIG_ENV_SIZE, &env_new);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500294 if (ret)
295 goto done;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200296
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500297 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
Stefano Babica3110f02010-10-27 11:06:20 +0200298 ret = spi_flash_write(env_flash, saved_offset,
299 saved_size, saved_buffer);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500300 if (ret)
301 goto done;
302 }
303
304 ret = 0;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200305 puts("done\n");
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500306
307 done:
308 if (saved_buffer)
309 free(saved_buffer);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000310
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500311 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200312}
Simon Glass4415f1d2017-08-03 12:21:58 -0600313#endif /* CMD_SAVEENV */
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200314
Simon Glassc5951992017-08-03 12:22:17 -0600315static int env_sf_load(void)
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200316{
317 int ret;
Ying Zhang5a89fa92014-01-24 15:50:07 +0800318 char *buf = NULL;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200319
Ravi Babu7dd01742015-08-17 13:29:49 +0530320 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
Andreas Fenkartc041c602017-04-08 11:59:33 +0200321 if (!buf) {
322 set_default_env("!malloc() failed");
Simon Glassc5951992017-08-03 12:22:17 -0600323 return -EIO;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200324 }
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200325
Andreas Fenkartc041c602017-04-08 11:59:33 +0200326 ret = setup_flash_device();
327 if (ret)
328 goto out;
329
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200330 ret = spi_flash_read(env_flash,
331 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
332 if (ret) {
333 set_default_env("!spi_flash_read() failed");
Andreas Fenkartc041c602017-04-08 11:59:33 +0200334 goto err_read;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200335 }
336
337 ret = env_import(buf, 1);
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200338 if (ret)
Simon Glass203e94f2017-08-03 12:21:56 -0600339 gd->env_valid = ENV_VALID;
Andreas Fenkartc041c602017-04-08 11:59:33 +0200340
341err_read:
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200342 spi_flash_free(env_flash);
343 env_flash = NULL;
Andreas Fenkartc041c602017-04-08 11:59:33 +0200344out:
345 free(buf);
Simon Glassc5951992017-08-03 12:22:17 -0600346
347 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200348}
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200349#endif
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200350
Simon Glass4415f1d2017-08-03 12:21:58 -0600351U_BOOT_ENV_LOCATION(sf) = {
352 .location = ENVL_SPI_FLASH,
Simon Glassac358be2017-08-03 12:22:03 -0600353 ENV_NAME("SPI Flash")
Simon Glasse5bce242017-08-03 12:22:01 -0600354 .load = env_sf_load,
Simon Glass4415f1d2017-08-03 12:21:58 -0600355#ifdef CMD_SAVEENV
Simon Glasse5bce242017-08-03 12:22:01 -0600356 .save = env_save_ptr(env_sf_save),
Simon Glass4415f1d2017-08-03 12:21:58 -0600357#endif
Simon Glass4415f1d2017-08-03 12:21:58 -0600358};