blob: ba5f7919c170ad47620df40997c57a9f5980d781 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +02002/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02003 * (C) Copyright 2000-2010
Haavard Skinnemoeneb8951c2008-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 Skinnemoeneb8951c2008-05-16 11:10:35 +020010 */
11#include <common.h>
Simon Glass11c89f32017-05-17 17:18:03 -060012#include <dm.h>
Simon Glass97385862019-08-01 09:47:00 -060013#include <env.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060014#include <env_internal.h>
Simon Glass8e201882020-05-10 11:39:54 -060015#include <flash.h>
Mike Frysinger9af10d52008-12-11 06:23:37 -050016#include <malloc.h>
Simon Glassdef92152014-10-13 23:41:55 -060017#include <spi.h>
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020018#include <spi_flash.h>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020019#include <search.h>
20#include <errno.h>
Simon Glass8e201882020-05-10 11:39:54 -060021#include <uuid.h>
Simon Glass274e0b02020-05-10 11:39:56 -060022#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060023#include <asm/global_data.h>
Gong Qianyu6d85a602016-01-26 15:06:42 +080024#include <dm/device-internal.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070025#include <u-boot/crc.h>
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020026
Simon Glassc10a88e2017-08-03 12:21:58 -060027#ifndef CONFIG_SPL_BUILD
Ashish Kumara7bf68a2017-12-14 17:37:08 +053028#define INITENV
Simon Glassc10a88e2017-08-03 12:21:58 -060029#endif
30
Patrick Delaunayb1096c32021-02-09 11:48:52 +010031#define OFFSET_INVALID (~(u32)0)
32
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020033#ifdef CONFIG_ENV_OFFSET_REDUND
Patrick Delaunayb1096c32021-02-09 11:48:52 +010034#define ENV_OFFSET_REDUND CONFIG_ENV_OFFSET_REDUND
35
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000036static ulong env_offset = CONFIG_ENV_OFFSET;
37static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
Patrick Delaunayb1096c32021-02-09 11:48:52 +010038
39#else
40
41#define ENV_OFFSET_REDUND OFFSET_INVALID
42
Stefano Babic5f6c2862010-10-27 11:06:20 +020043#endif /* CONFIG_ENV_OFFSET_REDUND */
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020044
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020045DECLARE_GLOBAL_DATA_PTR;
46
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020047static struct spi_flash *env_flash;
48
Andreas Fenkart52b94442017-04-08 11:59:31 +020049static int setup_flash_device(void)
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020050{
Lukasz Majewski76f442982020-06-04 23:11:53 +080051#if CONFIG_IS_ENABLED(DM_SPI_FLASH)
Gong Qianyu6d85a602016-01-26 15:06:42 +080052 struct udevice *new;
Andreas Fenkart52b94442017-04-08 11:59:31 +020053 int ret;
Gong Qianyu6d85a602016-01-26 15:06:42 +080054
Vignesh Rae569792016-07-06 10:04:28 +053055 /* speed and mode will be read from DT */
Gong Qianyu6d85a602016-01-26 15:06:42 +080056 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
Konstantin Porotchkin0be67b22018-08-29 16:34:52 +030057 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE,
58 &new);
Gong Qianyu6d85a602016-01-26 15:06:42 +080059 if (ret) {
Simon Glass97385862019-08-01 09:47:00 -060060 env_set_default("spi_flash_probe_bus_cs() failed", 0);
Simon Glass99778492017-08-03 12:22:17 -060061 return ret;
Gong Qianyu6d85a602016-01-26 15:06:42 +080062 }
63
64 env_flash = dev_get_uclass_priv(new);
65#else
Jagan Teki90173092020-05-13 16:32:42 +053066 if (env_flash)
67 spi_flash_free(env_flash);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020068
Jagan Teki90173092020-05-13 16:32:42 +053069 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
70 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020071 if (!env_flash) {
Jagan Teki90173092020-05-13 16:32:42 +053072 env_set_default("spi_flash_probe() failed", 0);
73 return -EIO;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020074 }
Gong Qianyu6d85a602016-01-26 15:06:42 +080075#endif
Andreas Fenkart52b94442017-04-08 11:59:31 +020076 return 0;
77}
78
79#if defined(CONFIG_ENV_OFFSET_REDUND)
Simon Glass082af922017-08-03 12:22:01 -060080static int env_sf_save(void)
Andreas Fenkart52b94442017-04-08 11:59:31 +020081{
82 env_t env_new;
Simon Glass4686d8f2019-08-01 09:47:08 -060083 char *saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
Rasmus Villemoese535f5b2021-04-14 20:51:43 +020084 u32 saved_size = 0, saved_offset = 0, sector;
Rasmus Villemoes1ee19842021-04-14 20:51:42 +020085 u32 sect_size = CONFIG_ENV_SECT_SIZE;
Andreas Fenkart52b94442017-04-08 11:59:31 +020086 int ret;
87
88 ret = setup_flash_device();
89 if (ret)
90 return ret;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020091
Rasmus Villemoese535f5b2021-04-14 20:51:43 +020092 if (IS_ENABLED(CONFIG_ENV_SECT_SIZE_AUTO))
93 sect_size = env_flash->mtd.erasesize;
94
Marek Vasutd73c1292014-03-05 19:59:50 +010095 ret = env_export(&env_new);
96 if (ret)
Simon Glass99778492017-08-03 12:22:17 -060097 return -EIO;
Simon Glass4686d8f2019-08-01 09:47:08 -060098 env_new.flags = ENV_REDUND_ACTIVE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020099
Simon Glass4bc2ad22017-08-03 12:21:56 -0600100 if (gd->env_valid == ENV_VALID) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200101 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
102 env_offset = CONFIG_ENV_OFFSET;
103 } else {
104 env_new_offset = CONFIG_ENV_OFFSET;
105 env_offset = CONFIG_ENV_OFFSET_REDUND;
106 }
107
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200108 /* Is the sector larger than the env (i.e. embedded) */
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200109 if (sect_size > CONFIG_ENV_SIZE) {
110 saved_size = sect_size - CONFIG_ENV_SIZE;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200111 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
Ravi Babu20d93fc2015-08-17 13:29:49 +0530112 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200113 if (!saved_buffer) {
Simon Glass99778492017-08-03 12:22:17 -0600114 ret = -ENOMEM;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200115 goto done;
116 }
Heiko Schocherf45775c2019-03-13 12:15:45 +0100117 ret = spi_flash_read(env_flash, saved_offset,
118 saved_size, saved_buffer);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200119 if (ret)
120 goto done;
121 }
122
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200123 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200124
125 puts("Erasing SPI flash...");
126 ret = spi_flash_erase(env_flash, env_new_offset,
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200127 sector * sect_size);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200128 if (ret)
129 goto done;
130
131 puts("Writing to SPI flash...");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200132
Stefano Babic5f6c2862010-10-27 11:06:20 +0200133 ret = spi_flash_write(env_flash, env_new_offset,
Tom Rinia840b8e2013-04-05 14:55:21 -0400134 CONFIG_ENV_SIZE, &env_new);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200135 if (ret)
136 goto done;
137
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200138 if (sect_size > CONFIG_ENV_SIZE) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200139 ret = spi_flash_write(env_flash, saved_offset,
140 saved_size, saved_buffer);
141 if (ret)
142 goto done;
143 }
144
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000145 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
Tom Rinia840b8e2013-04-05 14:55:21 -0400146 sizeof(env_new.flags), &flag);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200147 if (ret)
148 goto done;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200149
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200150 puts("done\n");
151
Simon Glass4bc2ad22017-08-03 12:21:56 -0600152 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200153
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000154 printf("Valid environment: %d\n", (int)gd->env_valid);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200155
Patrick Delaunay480b6e62021-01-04 14:41:31 +0100156done:
Patrick Delaunaya1b3eef2021-02-24 11:52:35 +0100157 spi_flash_free(env_flash);
158 env_flash = NULL;
159
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200160 if (saved_buffer)
161 free(saved_buffer);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000162
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200163 return ret;
164}
165
Simon Glass99778492017-08-03 12:22:17 -0600166static int env_sf_load(void)
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200167{
168 int ret;
Simon Goldschmidt8f9ce3a2018-01-31 14:47:13 +0100169 int read1_fail, read2_fail;
170 env_t *tmp_env1, *tmp_env2;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200171
Ravi Babu20d93fc2015-08-17 13:29:49 +0530172 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
173 CONFIG_ENV_SIZE);
174 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
175 CONFIG_ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200176 if (!tmp_env1 || !tmp_env2) {
Simon Glass97385862019-08-01 09:47:00 -0600177 env_set_default("malloc() failed", 0);
Simon Glass99778492017-08-03 12:22:17 -0600178 ret = -EIO;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000179 goto out;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200180 }
181
Andreas Fenkarta2d18832017-04-08 11:59:32 +0200182 ret = setup_flash_device();
183 if (ret)
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000184 goto out;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200185
Heiko Schocherf45775c2019-03-13 12:15:45 +0100186 read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
187 CONFIG_ENV_SIZE, tmp_env1);
188 read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
189 CONFIG_ENV_SIZE, tmp_env2);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200190
Simon Goldschmidt8f9ce3a2018-01-31 14:47:13 +0100191 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
Marek Vasutdfe4b7e2020-07-07 20:51:35 +0200192 read2_fail, H_EXTERNAL);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200193
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200194 spi_flash_free(env_flash);
195 env_flash = NULL;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200196out:
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200197 free(tmp_env1);
198 free(tmp_env2);
Simon Glass99778492017-08-03 12:22:17 -0600199
200 return ret;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200201}
202#else
Simon Glass082af922017-08-03 12:22:01 -0600203static int env_sf_save(void)
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200204{
Rasmus Villemoese535f5b2021-04-14 20:51:43 +0200205 u32 saved_size = 0, saved_offset = 0, sector;
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200206 u32 sect_size = CONFIG_ENV_SECT_SIZE;
Marek Vasutd73c1292014-03-05 19:59:50 +0100207 char *saved_buffer = NULL;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000208 int ret = 1;
Tom Rinia840b8e2013-04-05 14:55:21 -0400209 env_t env_new;
Gong Qianyu6d85a602016-01-26 15:06:42 +0800210
Andreas Fenkart52b94442017-04-08 11:59:31 +0200211 ret = setup_flash_device();
212 if (ret)
213 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200214
Rasmus Villemoese535f5b2021-04-14 20:51:43 +0200215 if (IS_ENABLED(CONFIG_ENV_SECT_SIZE_AUTO))
216 sect_size = env_flash->mtd.erasesize;
217
Mike Frysinger9af10d52008-12-11 06:23:37 -0500218 /* Is the sector larger than the env (i.e. embedded) */
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200219 if (sect_size > CONFIG_ENV_SIZE) {
220 saved_size = sect_size - CONFIG_ENV_SIZE;
Mike Frysinger9af10d52008-12-11 06:23:37 -0500221 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
222 saved_buffer = malloc(saved_size);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000223 if (!saved_buffer)
Mike Frysinger9af10d52008-12-11 06:23:37 -0500224 goto done;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000225
Heiko Schocherf45775c2019-03-13 12:15:45 +0100226 ret = spi_flash_read(env_flash, saved_offset,
227 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500228 if (ret)
229 goto done;
230 }
231
Marek Vasutd73c1292014-03-05 19:59:50 +0100232 ret = env_export(&env_new);
233 if (ret)
Stefano Babic5f6c2862010-10-27 11:06:20 +0200234 goto done;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200235
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200236 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size);
Andreas Fenkartdf701912017-04-08 11:59:34 +0200237
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200238 puts("Erasing SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200239 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200240 sector * sect_size);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500241 if (ret)
242 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200243
244 puts("Writing to SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200245 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
Tom Rinia840b8e2013-04-05 14:55:21 -0400246 CONFIG_ENV_SIZE, &env_new);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500247 if (ret)
248 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200249
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200250 if (sect_size > CONFIG_ENV_SIZE) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200251 ret = spi_flash_write(env_flash, saved_offset,
252 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500253 if (ret)
254 goto done;
255 }
256
257 ret = 0;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200258 puts("done\n");
Mike Frysinger9af10d52008-12-11 06:23:37 -0500259
Patrick Delaunay480b6e62021-01-04 14:41:31 +0100260done:
Patrick Delaunaya1b3eef2021-02-24 11:52:35 +0100261 spi_flash_free(env_flash);
262 env_flash = NULL;
263
Mike Frysinger9af10d52008-12-11 06:23:37 -0500264 if (saved_buffer)
265 free(saved_buffer);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000266
Mike Frysinger9af10d52008-12-11 06:23:37 -0500267 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200268}
269
Simon Glass99778492017-08-03 12:22:17 -0600270static int env_sf_load(void)
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200271{
272 int ret;
Ying Zhang354846f2014-01-24 15:50:07 +0800273 char *buf = NULL;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200274
Ravi Babu20d93fc2015-08-17 13:29:49 +0530275 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200276 if (!buf) {
Simon Glass97385862019-08-01 09:47:00 -0600277 env_set_default("malloc() failed", 0);
Simon Glass99778492017-08-03 12:22:17 -0600278 return -EIO;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200279 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200280
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200281 ret = setup_flash_device();
282 if (ret)
283 goto out;
284
Heiko Schocherf45775c2019-03-13 12:15:45 +0100285 ret = spi_flash_read(env_flash,
286 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200287 if (ret) {
Simon Glass97385862019-08-01 09:47:00 -0600288 env_set_default("spi_flash_read() failed", 0);
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200289 goto err_read;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200290 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200291
Marek Vasutdfe4b7e2020-07-07 20:51:35 +0200292 ret = env_import(buf, 1, H_EXTERNAL);
Simon Goldschmidt32c3eb52018-01-31 14:47:10 +0100293 if (!ret)
Simon Glass4bc2ad22017-08-03 12:21:56 -0600294 gd->env_valid = ENV_VALID;
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200295
296err_read:
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200297 spi_flash_free(env_flash);
298 env_flash = NULL;
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200299out:
300 free(buf);
Simon Glass99778492017-08-03 12:22:17 -0600301
302 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200303}
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200304#endif
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200305
Patrick Delaunayb1096c32021-02-09 11:48:52 +0100306static int env_sf_erase(void)
307{
308 int ret;
309 env_t env;
310
311 ret = setup_flash_device();
312 if (ret)
313 return ret;
314
315 memset(&env, 0, sizeof(env_t));
316 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, &env);
317 if (ret)
318 goto done;
319
320 if (ENV_OFFSET_REDUND != OFFSET_INVALID)
321 ret = spi_flash_write(env_flash, ENV_OFFSET_REDUND, CONFIG_ENV_SIZE, &env);
322
323done:
324 spi_flash_free(env_flash);
325 env_flash = NULL;
326
327 return ret;
328}
329
Tom Rini5cd7ece2019-11-18 20:02:10 -0500330#if CONFIG_ENV_ADDR != 0x0
Rajesh Bhagat9a90cc92018-11-05 18:01:15 +0000331__weak void *env_sf_get_env_addr(void)
332{
333 return (void *)CONFIG_ENV_ADDR;
334}
335#endif
336
Tom Rini5cd7ece2019-11-18 20:02:10 -0500337#if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0)
Heiko Schocher704d21c2020-10-10 10:28:05 +0200338/*
339 * check if Environment on CONFIG_ENV_ADDR is valid.
340 */
341static int env_sf_init_addr(void)
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530342{
Rajesh Bhagat9a90cc92018-11-05 18:01:15 +0000343 env_t *env_ptr = (env_t *)env_sf_get_env_addr();
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530344
345 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
346 gd->env_addr = (ulong)&(env_ptr->data);
347 gd->env_valid = 1;
348 } else {
349 gd->env_addr = (ulong)&default_environment[0];
350 gd->env_valid = 1;
351 }
352
353 return 0;
354}
355#endif
356
Heiko Schocher704d21c2020-10-10 10:28:05 +0200357#if defined(CONFIG_ENV_SPI_EARLY)
358/*
359 * early load environment from SPI flash (before relocation)
360 * and check if it is valid.
361 */
362static int env_sf_init_early(void)
363{
364 int ret;
365 int read1_fail;
366 int read2_fail;
367 int crc1_ok;
368 env_t *tmp_env2 = NULL;
369 env_t *tmp_env1;
370
371 /*
372 * if malloc is not ready yet, we cannot use
373 * this part yet.
374 */
375 if (!gd->malloc_limit)
376 return -ENOENT;
377
378 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
379 CONFIG_ENV_SIZE);
380 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
381 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
382 CONFIG_ENV_SIZE);
383
384 if (!tmp_env1 || !tmp_env2)
385 goto out;
386
387 ret = setup_flash_device();
388 if (ret)
389 goto out;
390
391 read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
392 CONFIG_ENV_SIZE, tmp_env1);
393
394 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT)) {
395 read2_fail = spi_flash_read(env_flash,
396 CONFIG_ENV_OFFSET_REDUND,
397 CONFIG_ENV_SIZE, tmp_env2);
398 ret = env_check_redund((char *)tmp_env1, read1_fail,
399 (char *)tmp_env2, read2_fail);
400
Brandon Maier61374b02020-12-17 17:19:18 -0600401 if (ret < 0)
Heiko Schocher704d21c2020-10-10 10:28:05 +0200402 goto err_read;
403
404 if (gd->env_valid == ENV_VALID)
405 gd->env_addr = (unsigned long)&tmp_env1->data;
406 else
407 gd->env_addr = (unsigned long)&tmp_env2->data;
408 } else {
409 if (read1_fail)
410 goto err_read;
411
412 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
413 tmp_env1->crc;
414 if (!crc1_ok)
415 goto err_read;
416
417 /* if valid -> this is our env */
418 gd->env_valid = ENV_VALID;
419 gd->env_addr = (unsigned long)&tmp_env1->data;
420 }
421
Patrick Delaunaya1b3eef2021-02-24 11:52:35 +0100422 spi_flash_free(env_flash);
423 env_flash = NULL;
424
Heiko Schocher704d21c2020-10-10 10:28:05 +0200425 return 0;
426err_read:
427 spi_flash_free(env_flash);
428 env_flash = NULL;
429 free(tmp_env1);
430 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
431 free(tmp_env2);
432out:
433 /* env is not valid. always return 0 */
434 gd->env_valid = ENV_INVALID;
435 return 0;
436}
437#endif
438
439static int env_sf_init(void)
440{
441#if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0)
442 return env_sf_init_addr();
443#elif defined(CONFIG_ENV_SPI_EARLY)
444 return env_sf_init_early();
445#endif
446 /*
Heiko Schocher8ba7b612020-11-03 15:22:36 +0100447 * return here -ENOENT, so env_init()
448 * can set the init bit and later if no
449 * other Environment storage is defined
450 * can set the default environment
Heiko Schocher704d21c2020-10-10 10:28:05 +0200451 */
Heiko Schocher8ba7b612020-11-03 15:22:36 +0100452 return -ENOENT;
Heiko Schocher704d21c2020-10-10 10:28:05 +0200453}
454
Simon Glassc10a88e2017-08-03 12:21:58 -0600455U_BOOT_ENV_LOCATION(sf) = {
456 .location = ENVL_SPI_FLASH,
Patrick Delaunaya4433de2020-07-28 11:51:16 +0200457 ENV_NAME("SPIFlash")
Simon Glass082af922017-08-03 12:22:01 -0600458 .load = env_sf_load,
Patrick Delaunay5c7c2d02021-02-09 11:48:51 +0100459 .save = ENV_SAVE_PTR(env_sf_save),
Patrick Delaunayb1096c32021-02-09 11:48:52 +0100460 .erase = ENV_ERASE_PTR(env_sf_erase),
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530461 .init = env_sf_init,
Simon Glassc10a88e2017-08-03 12:21:58 -0600462};