blob: a2e4c93631564046c1a2a28896bb2bf78f752ced [file] [log] [blame]
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +02001/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02002 * (C) Copyright 2000-2010
Haavard Skinnemoeneb8951c2008-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 Tekib1df9222013-07-29 22:12:52 +053010 * SPDX-License-Identifier: GPL-2.0+
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020011 */
12#include <common.h>
Simon Glass11c89f32017-05-17 17:18:03 -060013#include <dm.h>
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020014#include <environment.h>
Mike Frysinger9af10d52008-12-11 06:23:37 -050015#include <malloc.h>
Simon Glassdef92152014-10-13 23:41:55 -060016#include <spi.h>
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020017#include <spi_flash.h>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020018#include <search.h>
19#include <errno.h>
Gong Qianyu6d85a602016-01-26 15:06:42 +080020#include <dm/device-internal.h>
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020021
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020022#ifndef CONFIG_ENV_SPI_BUS
Jean-Jacques Hiblot464eee12017-02-01 14:51:47 +010023# define CONFIG_ENV_SPI_BUS CONFIG_SF_DEFAULT_BUS
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020024#endif
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020025#ifndef CONFIG_ENV_SPI_CS
Jean-Jacques Hiblot464eee12017-02-01 14:51:47 +010026# define CONFIG_ENV_SPI_CS CONFIG_SF_DEFAULT_CS
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020027#endif
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020028#ifndef CONFIG_ENV_SPI_MAX_HZ
Jean-Jacques Hiblot464eee12017-02-01 14:51:47 +010029# define CONFIG_ENV_SPI_MAX_HZ CONFIG_SF_DEFAULT_SPEED
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020030#endif
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020031#ifndef CONFIG_ENV_SPI_MODE
Jean-Jacques Hiblot464eee12017-02-01 14:51:47 +010032# define CONFIG_ENV_SPI_MODE CONFIG_SF_DEFAULT_MODE
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020033#endif
34
Simon Glassc10a88e2017-08-03 12:21:58 -060035#ifndef CONFIG_SPL_BUILD
36#define CMD_SAVEENV
Ashish Kumara7bf68a2017-12-14 17:37:08 +053037#define INITENV
Simon Glassc10a88e2017-08-03 12:21:58 -060038#endif
39
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020040#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glassc10a88e2017-08-03 12:21:58 -060041#ifdef CMD_SAVEENV
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000042static ulong env_offset = CONFIG_ENV_OFFSET;
43static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
Simon Glassc10a88e2017-08-03 12:21:58 -060044#endif
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020045
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000046#define ACTIVE_FLAG 1
47#define OBSOLETE_FLAG 0
Stefano Babic5f6c2862010-10-27 11:06:20 +020048#endif /* CONFIG_ENV_OFFSET_REDUND */
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020049
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020050DECLARE_GLOBAL_DATA_PTR;
51
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020052static struct spi_flash *env_flash;
53
Andreas Fenkart52b94442017-04-08 11:59:31 +020054static int setup_flash_device(void)
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020055{
Gong Qianyu6d85a602016-01-26 15:06:42 +080056#ifdef CONFIG_DM_SPI_FLASH
57 struct udevice *new;
Andreas Fenkart52b94442017-04-08 11:59:31 +020058 int ret;
Gong Qianyu6d85a602016-01-26 15:06:42 +080059
Vignesh Rae569792016-07-06 10:04:28 +053060 /* speed and mode will be read from DT */
Gong Qianyu6d85a602016-01-26 15:06:42 +080061 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
Vignesh Rae569792016-07-06 10:04:28 +053062 0, 0, &new);
Gong Qianyu6d85a602016-01-26 15:06:42 +080063 if (ret) {
64 set_default_env("!spi_flash_probe_bus_cs() failed");
Simon Glass99778492017-08-03 12:22:17 -060065 return ret;
Gong Qianyu6d85a602016-01-26 15:06:42 +080066 }
67
68 env_flash = dev_get_uclass_priv(new);
69#else
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020070
71 if (!env_flash) {
Stefano Babic5f6c2862010-10-27 11:06:20 +020072 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
73 CONFIG_ENV_SPI_CS,
74 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
75 if (!env_flash) {
76 set_default_env("!spi_flash_probe() failed");
Simon Glass99778492017-08-03 12:22:17 -060077 return -EIO;
Stefano Babic5f6c2862010-10-27 11:06:20 +020078 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020079 }
Gong Qianyu6d85a602016-01-26 15:06:42 +080080#endif
Andreas Fenkart52b94442017-04-08 11:59:31 +020081 return 0;
82}
83
84#if defined(CONFIG_ENV_OFFSET_REDUND)
Simon Glassc10a88e2017-08-03 12:21:58 -060085#ifdef CMD_SAVEENV
Simon Glass082af922017-08-03 12:22:01 -060086static int env_sf_save(void)
Andreas Fenkart52b94442017-04-08 11:59:31 +020087{
88 env_t env_new;
89 char *saved_buffer = NULL, flag = OBSOLETE_FLAG;
Andreas Fenkartdf701912017-04-08 11:59:34 +020090 u32 saved_size, saved_offset, sector;
Andreas Fenkart52b94442017-04-08 11:59:31 +020091 int ret;
92
93 ret = setup_flash_device();
94 if (ret)
95 return ret;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020096
Marek Vasutd73c1292014-03-05 19:59:50 +010097 ret = env_export(&env_new);
98 if (ret)
Simon Glass99778492017-08-03 12:22:17 -060099 return -EIO;
Tom Rinia840b8e2013-04-05 14:55:21 -0400100 env_new.flags = ACTIVE_FLAG;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200101
Simon Glass4bc2ad22017-08-03 12:21:56 -0600102 if (gd->env_valid == ENV_VALID) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200103 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
104 env_offset = CONFIG_ENV_OFFSET;
105 } else {
106 env_new_offset = CONFIG_ENV_OFFSET;
107 env_offset = CONFIG_ENV_OFFSET_REDUND;
108 }
109
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200110 /* Is the sector larger than the env (i.e. embedded) */
111 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
112 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
113 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
Ravi Babu20d93fc2015-08-17 13:29:49 +0530114 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200115 if (!saved_buffer) {
Simon Glass99778492017-08-03 12:22:17 -0600116 ret = -ENOMEM;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200117 goto done;
118 }
119 ret = spi_flash_read(env_flash, saved_offset,
120 saved_size, saved_buffer);
121 if (ret)
122 goto done;
123 }
124
Andreas Fenkartdf701912017-04-08 11:59:34 +0200125 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200126
127 puts("Erasing SPI flash...");
128 ret = spi_flash_erase(env_flash, env_new_offset,
129 sector * CONFIG_ENV_SECT_SIZE);
130 if (ret)
131 goto done;
132
133 puts("Writing to SPI flash...");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200134
Stefano Babic5f6c2862010-10-27 11:06:20 +0200135 ret = spi_flash_write(env_flash, env_new_offset,
Tom Rinia840b8e2013-04-05 14:55:21 -0400136 CONFIG_ENV_SIZE, &env_new);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200137 if (ret)
138 goto done;
139
140 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
141 ret = spi_flash_write(env_flash, saved_offset,
142 saved_size, saved_buffer);
143 if (ret)
144 goto done;
145 }
146
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000147 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
Tom Rinia840b8e2013-04-05 14:55:21 -0400148 sizeof(env_new.flags), &flag);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200149 if (ret)
150 goto done;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200151
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200152 puts("done\n");
153
Simon Glass4bc2ad22017-08-03 12:21:56 -0600154 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200155
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000156 printf("Valid environment: %d\n", (int)gd->env_valid);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200157
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200158 done:
159 if (saved_buffer)
160 free(saved_buffer);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000161
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200162 return ret;
163}
Simon Glassc10a88e2017-08-03 12:21:58 -0600164#endif /* CMD_SAVEENV */
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200165
Simon Glass99778492017-08-03 12:22:17 -0600166static int env_sf_load(void)
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200167{
168 int ret;
169 int crc1_ok = 0, crc2_ok = 0;
170 env_t *tmp_env1 = NULL;
171 env_t *tmp_env2 = NULL;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200172 env_t *ep = NULL;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200173
Ravi Babu20d93fc2015-08-17 13:29:49 +0530174 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
175 CONFIG_ENV_SIZE);
176 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
177 CONFIG_ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200178 if (!tmp_env1 || !tmp_env2) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200179 set_default_env("!malloc() failed");
Simon Glass99778492017-08-03 12:22:17 -0600180 ret = -EIO;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000181 goto out;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200182 }
183
Andreas Fenkarta2d18832017-04-08 11:59:32 +0200184 ret = setup_flash_device();
185 if (ret)
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000186 goto out;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200187
188 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
189 CONFIG_ENV_SIZE, tmp_env1);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200190 if (ret) {
191 set_default_env("!spi_flash_read() failed");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200192 goto err_read;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200193 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200194
195 if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc)
196 crc1_ok = 1;
Wolfgang Wegner0cae3d42010-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 Wegner0cae3d42010-04-23 17:22:55 +0200203 }
204
Stefano Babic5f6c2862010-10-27 11:06:20 +0200205 if (!crc1_ok && !crc2_ok) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200206 set_default_env("!bad CRC");
Simon Glass99778492017-08-03 12:22:17 -0600207 ret = -EIO;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000208 goto err_read;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200209 } else if (crc1_ok && !crc2_ok) {
Simon Glass4bc2ad22017-08-03 12:21:56 -0600210 gd->env_valid = ENV_VALID;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200211 } else if (!crc1_ok && crc2_ok) {
Simon Glass4bc2ad22017-08-03 12:21:56 -0600212 gd->env_valid = ENV_REDUND;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000213 } else if (tmp_env1->flags == ACTIVE_FLAG &&
214 tmp_env2->flags == OBSOLETE_FLAG) {
Simon Glass4bc2ad22017-08-03 12:21:56 -0600215 gd->env_valid = ENV_VALID;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000216 } else if (tmp_env1->flags == OBSOLETE_FLAG &&
217 tmp_env2->flags == ACTIVE_FLAG) {
Simon Glass4bc2ad22017-08-03 12:21:56 -0600218 gd->env_valid = ENV_REDUND;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000219 } else if (tmp_env1->flags == tmp_env2->flags) {
Simon Glass4bc2ad22017-08-03 12:21:56 -0600220 gd->env_valid = ENV_VALID;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000221 } else if (tmp_env1->flags == 0xFF) {
Simon Glass4bc2ad22017-08-03 12:21:56 -0600222 gd->env_valid = ENV_VALID;
Mario Schuknecht12f1a542015-03-24 08:59:00 +0100223 } else if (tmp_env2->flags == 0xFF) {
Simon Glass4bc2ad22017-08-03 12:21:56 -0600224 gd->env_valid = ENV_REDUND;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200225 } else {
226 /*
227 * this differs from code in env_flash.c, but I think a sane
228 * default path is desirable.
229 */
Simon Glass4bc2ad22017-08-03 12:21:56 -0600230 gd->env_valid = ENV_VALID;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200231 }
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200232
Simon Glass4bc2ad22017-08-03 12:21:56 -0600233 if (gd->env_valid == ENV_VALID)
Stefano Babic5f6c2862010-10-27 11:06:20 +0200234 ep = tmp_env1;
235 else
236 ep = tmp_env2;
237
238 ret = env_import((char *)ep, 0);
239 if (!ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900240 pr_err("Cannot import environment: errno = %d\n", errno);
Peng Fan3e21c722016-03-28 17:26:27 +0800241 set_default_env("!env_import failed");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200242 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200243
244err_read:
245 spi_flash_free(env_flash);
246 env_flash = NULL;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200247out:
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200248 free(tmp_env1);
249 free(tmp_env2);
Simon Glass99778492017-08-03 12:22:17 -0600250
251 return ret;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200252}
253#else
Simon Glassc10a88e2017-08-03 12:21:58 -0600254#ifdef CMD_SAVEENV
Simon Glass082af922017-08-03 12:22:01 -0600255static int env_sf_save(void)
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200256{
Andreas Fenkartdf701912017-04-08 11:59:34 +0200257 u32 saved_size, saved_offset, sector;
Marek Vasutd73c1292014-03-05 19:59:50 +0100258 char *saved_buffer = NULL;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000259 int ret = 1;
Tom Rinia840b8e2013-04-05 14:55:21 -0400260 env_t env_new;
Gong Qianyu6d85a602016-01-26 15:06:42 +0800261
Andreas Fenkart52b94442017-04-08 11:59:31 +0200262 ret = setup_flash_device();
263 if (ret)
264 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200265
Mike Frysinger9af10d52008-12-11 06:23:37 -0500266 /* Is the sector larger than the env (i.e. embedded) */
267 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
268 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
269 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
270 saved_buffer = malloc(saved_size);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000271 if (!saved_buffer)
Mike Frysinger9af10d52008-12-11 06:23:37 -0500272 goto done;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000273
Stefano Babic5f6c2862010-10-27 11:06:20 +0200274 ret = spi_flash_read(env_flash, saved_offset,
275 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500276 if (ret)
277 goto done;
278 }
279
Marek Vasutd73c1292014-03-05 19:59:50 +0100280 ret = env_export(&env_new);
281 if (ret)
Stefano Babic5f6c2862010-10-27 11:06:20 +0200282 goto done;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200283
Andreas Fenkartdf701912017-04-08 11:59:34 +0200284 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
285
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200286 puts("Erasing SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200287 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
288 sector * CONFIG_ENV_SECT_SIZE);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500289 if (ret)
290 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200291
292 puts("Writing to SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200293 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
Tom Rinia840b8e2013-04-05 14:55:21 -0400294 CONFIG_ENV_SIZE, &env_new);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500295 if (ret)
296 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200297
Mike Frysinger9af10d52008-12-11 06:23:37 -0500298 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200299 ret = spi_flash_write(env_flash, saved_offset,
300 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500301 if (ret)
302 goto done;
303 }
304
305 ret = 0;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200306 puts("done\n");
Mike Frysinger9af10d52008-12-11 06:23:37 -0500307
308 done:
309 if (saved_buffer)
310 free(saved_buffer);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000311
Mike Frysinger9af10d52008-12-11 06:23:37 -0500312 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200313}
Simon Glassc10a88e2017-08-03 12:21:58 -0600314#endif /* CMD_SAVEENV */
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200315
Simon Glass99778492017-08-03 12:22:17 -0600316static int env_sf_load(void)
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200317{
318 int ret;
Ying Zhang354846f2014-01-24 15:50:07 +0800319 char *buf = NULL;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200320
Ravi Babu20d93fc2015-08-17 13:29:49 +0530321 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200322 if (!buf) {
323 set_default_env("!malloc() failed");
Simon Glass99778492017-08-03 12:22:17 -0600324 return -EIO;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200325 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200326
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200327 ret = setup_flash_device();
328 if (ret)
329 goto out;
330
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200331 ret = spi_flash_read(env_flash,
332 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
333 if (ret) {
334 set_default_env("!spi_flash_read() failed");
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200335 goto err_read;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200336 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200337
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200338 ret = env_import(buf, 1);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200339 if (ret)
Simon Glass4bc2ad22017-08-03 12:21:56 -0600340 gd->env_valid = ENV_VALID;
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200341
342err_read:
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200343 spi_flash_free(env_flash);
344 env_flash = NULL;
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200345out:
346 free(buf);
Simon Glass99778492017-08-03 12:22:17 -0600347
348 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200349}
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200350#endif
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200351
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530352#if defined(INITENV) && defined(CONFIG_ENV_ADDR)
353static int env_sf_init(void)
354{
355 env_t *env_ptr = (env_t *)(CONFIG_ENV_ADDR);
356
357 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
358 gd->env_addr = (ulong)&(env_ptr->data);
359 gd->env_valid = 1;
360 } else {
361 gd->env_addr = (ulong)&default_environment[0];
362 gd->env_valid = 1;
363 }
364
365 return 0;
366}
367#endif
368
Simon Glassc10a88e2017-08-03 12:21:58 -0600369U_BOOT_ENV_LOCATION(sf) = {
370 .location = ENVL_SPI_FLASH,
Simon Glassd8273ed2017-08-03 12:22:03 -0600371 ENV_NAME("SPI Flash")
Simon Glass082af922017-08-03 12:22:01 -0600372 .load = env_sf_load,
Simon Glassc10a88e2017-08-03 12:21:58 -0600373#ifdef CMD_SAVEENV
Simon Glass082af922017-08-03 12:22:01 -0600374 .save = env_save_ptr(env_sf_save),
Simon Glassc10a88e2017-08-03 12:21:58 -0600375#endif
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530376#if defined(INITENV) && defined(CONFIG_ENV_ADDR)
377 .init = env_sf_init,
378#endif
Simon Glassc10a88e2017-08-03 12:21:58 -0600379};