blob: a425ecc11c8e6c27606e7f684b6dd641f4abb934 [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>
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>
Simon Glass8e201882020-05-10 11:39:54 -060020#include <uuid.h>
Simon Glass274e0b02020-05-10 11:39:56 -060021#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060022#include <asm/global_data.h>
Gong Qianyu6d85a602016-01-26 15:06:42 +080023#include <dm/device-internal.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070024#include <u-boot/crc.h>
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020025
Patrick Delaunayb1096c32021-02-09 11:48:52 +010026#define OFFSET_INVALID (~(u32)0)
27
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020028#ifdef CONFIG_ENV_OFFSET_REDUND
Patrick Delaunayb1096c32021-02-09 11:48:52 +010029#define ENV_OFFSET_REDUND CONFIG_ENV_OFFSET_REDUND
30
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000031static ulong env_offset = CONFIG_ENV_OFFSET;
32static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
Patrick Delaunayb1096c32021-02-09 11:48:52 +010033
34#else
35
36#define ENV_OFFSET_REDUND OFFSET_INVALID
37
Stefano Babic5f6c2862010-10-27 11:06:20 +020038#endif /* CONFIG_ENV_OFFSET_REDUND */
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020039
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020040DECLARE_GLOBAL_DATA_PTR;
41
Patrick Delaunay625dd912021-02-24 11:52:36 +010042static int setup_flash_device(struct spi_flash **env_flash)
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020043{
Lukasz Majewski76f442982020-06-04 23:11:53 +080044#if CONFIG_IS_ENABLED(DM_SPI_FLASH)
Gong Qianyu6d85a602016-01-26 15:06:42 +080045 struct udevice *new;
Andreas Fenkart52b94442017-04-08 11:59:31 +020046 int ret;
Gong Qianyu6d85a602016-01-26 15:06:42 +080047
Vignesh Rae569792016-07-06 10:04:28 +053048 /* speed and mode will be read from DT */
Gong Qianyu6d85a602016-01-26 15:06:42 +080049 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
Konstantin Porotchkin0be67b22018-08-29 16:34:52 +030050 &new);
Gong Qianyu6d85a602016-01-26 15:06:42 +080051 if (ret) {
Simon Glass97385862019-08-01 09:47:00 -060052 env_set_default("spi_flash_probe_bus_cs() failed", 0);
Simon Glass99778492017-08-03 12:22:17 -060053 return ret;
Gong Qianyu6d85a602016-01-26 15:06:42 +080054 }
55
Patrick Delaunay625dd912021-02-24 11:52:36 +010056 *env_flash = dev_get_uclass_priv(new);
Gong Qianyu6d85a602016-01-26 15:06:42 +080057#else
Patrick Delaunay625dd912021-02-24 11:52:36 +010058 *env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
59 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
60 if (!*env_flash) {
Jagan Teki90173092020-05-13 16:32:42 +053061 env_set_default("spi_flash_probe() failed", 0);
62 return -EIO;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020063 }
Gong Qianyu6d85a602016-01-26 15:06:42 +080064#endif
Andreas Fenkart52b94442017-04-08 11:59:31 +020065 return 0;
66}
67
68#if defined(CONFIG_ENV_OFFSET_REDUND)
Simon Glass082af922017-08-03 12:22:01 -060069static int env_sf_save(void)
Andreas Fenkart52b94442017-04-08 11:59:31 +020070{
71 env_t env_new;
Simon Glass4686d8f2019-08-01 09:47:08 -060072 char *saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
Rasmus Villemoese535f5b2021-04-14 20:51:43 +020073 u32 saved_size = 0, saved_offset = 0, sector;
Rasmus Villemoes1ee19842021-04-14 20:51:42 +020074 u32 sect_size = CONFIG_ENV_SECT_SIZE;
Andreas Fenkart52b94442017-04-08 11:59:31 +020075 int ret;
Patrick Delaunay625dd912021-02-24 11:52:36 +010076 struct spi_flash *env_flash;
Andreas Fenkart52b94442017-04-08 11:59:31 +020077
Patrick Delaunay625dd912021-02-24 11:52:36 +010078 ret = setup_flash_device(&env_flash);
Andreas Fenkart52b94442017-04-08 11:59:31 +020079 if (ret)
80 return ret;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020081
Rasmus Villemoese535f5b2021-04-14 20:51:43 +020082 if (IS_ENABLED(CONFIG_ENV_SECT_SIZE_AUTO))
83 sect_size = env_flash->mtd.erasesize;
84
Marek Vasutd73c1292014-03-05 19:59:50 +010085 ret = env_export(&env_new);
86 if (ret)
Simon Glass99778492017-08-03 12:22:17 -060087 return -EIO;
Simon Glass4686d8f2019-08-01 09:47:08 -060088 env_new.flags = ENV_REDUND_ACTIVE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020089
Simon Glass4bc2ad22017-08-03 12:21:56 -060090 if (gd->env_valid == ENV_VALID) {
Stefano Babic5f6c2862010-10-27 11:06:20 +020091 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
92 env_offset = CONFIG_ENV_OFFSET;
93 } else {
94 env_new_offset = CONFIG_ENV_OFFSET;
95 env_offset = CONFIG_ENV_OFFSET_REDUND;
96 }
97
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020098 /* Is the sector larger than the env (i.e. embedded) */
Rasmus Villemoes1ee19842021-04-14 20:51:42 +020099 if (sect_size > CONFIG_ENV_SIZE) {
100 saved_size = sect_size - CONFIG_ENV_SIZE;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200101 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
Ravi Babu20d93fc2015-08-17 13:29:49 +0530102 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200103 if (!saved_buffer) {
Simon Glass99778492017-08-03 12:22:17 -0600104 ret = -ENOMEM;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200105 goto done;
106 }
Heiko Schocherf45775c2019-03-13 12:15:45 +0100107 ret = spi_flash_read(env_flash, saved_offset,
108 saved_size, saved_buffer);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200109 if (ret)
110 goto done;
111 }
112
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200113 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200114
115 puts("Erasing SPI flash...");
116 ret = spi_flash_erase(env_flash, env_new_offset,
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200117 sector * sect_size);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200118 if (ret)
119 goto done;
120
121 puts("Writing to SPI flash...");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200122
Stefano Babic5f6c2862010-10-27 11:06:20 +0200123 ret = spi_flash_write(env_flash, env_new_offset,
Tom Rinia840b8e2013-04-05 14:55:21 -0400124 CONFIG_ENV_SIZE, &env_new);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200125 if (ret)
126 goto done;
127
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200128 if (sect_size > CONFIG_ENV_SIZE) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200129 ret = spi_flash_write(env_flash, saved_offset,
130 saved_size, saved_buffer);
131 if (ret)
132 goto done;
133 }
134
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000135 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
Tom Rinia840b8e2013-04-05 14:55:21 -0400136 sizeof(env_new.flags), &flag);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200137 if (ret)
138 goto done;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200139
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200140 puts("done\n");
141
Simon Glass4bc2ad22017-08-03 12:21:56 -0600142 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200143
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000144 printf("Valid environment: %d\n", (int)gd->env_valid);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200145
Patrick Delaunay480b6e62021-01-04 14:41:31 +0100146done:
Patrick Delaunaya1b3eef2021-02-24 11:52:35 +0100147 spi_flash_free(env_flash);
Patrick Delaunaya1b3eef2021-02-24 11:52:35 +0100148
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200149 if (saved_buffer)
150 free(saved_buffer);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000151
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200152 return ret;
153}
154
Simon Glass99778492017-08-03 12:22:17 -0600155static int env_sf_load(void)
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200156{
157 int ret;
Simon Goldschmidt8f9ce3a2018-01-31 14:47:13 +0100158 int read1_fail, read2_fail;
159 env_t *tmp_env1, *tmp_env2;
Patrick Delaunay625dd912021-02-24 11:52:36 +0100160 struct spi_flash *env_flash;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200161
Ravi Babu20d93fc2015-08-17 13:29:49 +0530162 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
163 CONFIG_ENV_SIZE);
164 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
165 CONFIG_ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200166 if (!tmp_env1 || !tmp_env2) {
Simon Glass97385862019-08-01 09:47:00 -0600167 env_set_default("malloc() failed", 0);
Simon Glass99778492017-08-03 12:22:17 -0600168 ret = -EIO;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000169 goto out;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200170 }
171
Patrick Delaunay625dd912021-02-24 11:52:36 +0100172 ret = setup_flash_device(&env_flash);
Andreas Fenkarta2d18832017-04-08 11:59:32 +0200173 if (ret)
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000174 goto out;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200175
Heiko Schocherf45775c2019-03-13 12:15:45 +0100176 read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
177 CONFIG_ENV_SIZE, tmp_env1);
178 read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
179 CONFIG_ENV_SIZE, tmp_env2);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200180
Simon Goldschmidt8f9ce3a2018-01-31 14:47:13 +0100181 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
Marek Vasutdfe4b7e2020-07-07 20:51:35 +0200182 read2_fail, H_EXTERNAL);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200183
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200184 spi_flash_free(env_flash);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200185out:
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200186 free(tmp_env1);
187 free(tmp_env2);
Simon Glass99778492017-08-03 12:22:17 -0600188
189 return ret;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200190}
191#else
Simon Glass082af922017-08-03 12:22:01 -0600192static int env_sf_save(void)
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200193{
Rasmus Villemoese535f5b2021-04-14 20:51:43 +0200194 u32 saved_size = 0, saved_offset = 0, sector;
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200195 u32 sect_size = CONFIG_ENV_SECT_SIZE;
Marek Vasutd73c1292014-03-05 19:59:50 +0100196 char *saved_buffer = NULL;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000197 int ret = 1;
Tom Rinia840b8e2013-04-05 14:55:21 -0400198 env_t env_new;
Patrick Delaunay625dd912021-02-24 11:52:36 +0100199 struct spi_flash *env_flash;
Gong Qianyu6d85a602016-01-26 15:06:42 +0800200
Patrick Delaunay625dd912021-02-24 11:52:36 +0100201 ret = setup_flash_device(&env_flash);
Andreas Fenkart52b94442017-04-08 11:59:31 +0200202 if (ret)
203 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200204
Rasmus Villemoese535f5b2021-04-14 20:51:43 +0200205 if (IS_ENABLED(CONFIG_ENV_SECT_SIZE_AUTO))
206 sect_size = env_flash->mtd.erasesize;
207
Mike Frysinger9af10d52008-12-11 06:23:37 -0500208 /* Is the sector larger than the env (i.e. embedded) */
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200209 if (sect_size > CONFIG_ENV_SIZE) {
210 saved_size = sect_size - CONFIG_ENV_SIZE;
Mike Frysinger9af10d52008-12-11 06:23:37 -0500211 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
212 saved_buffer = malloc(saved_size);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000213 if (!saved_buffer)
Mike Frysinger9af10d52008-12-11 06:23:37 -0500214 goto done;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000215
Heiko Schocherf45775c2019-03-13 12:15:45 +0100216 ret = spi_flash_read(env_flash, saved_offset,
217 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500218 if (ret)
219 goto done;
220 }
221
Marek Vasutd73c1292014-03-05 19:59:50 +0100222 ret = env_export(&env_new);
223 if (ret)
Stefano Babic5f6c2862010-10-27 11:06:20 +0200224 goto done;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200225
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200226 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size);
Andreas Fenkartdf701912017-04-08 11:59:34 +0200227
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200228 puts("Erasing SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200229 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200230 sector * sect_size);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500231 if (ret)
232 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200233
234 puts("Writing to SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200235 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
Tom Rinia840b8e2013-04-05 14:55:21 -0400236 CONFIG_ENV_SIZE, &env_new);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500237 if (ret)
238 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200239
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200240 if (sect_size > CONFIG_ENV_SIZE) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200241 ret = spi_flash_write(env_flash, saved_offset,
242 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500243 if (ret)
244 goto done;
245 }
246
247 ret = 0;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200248 puts("done\n");
Mike Frysinger9af10d52008-12-11 06:23:37 -0500249
Patrick Delaunay480b6e62021-01-04 14:41:31 +0100250done:
Patrick Delaunaya1b3eef2021-02-24 11:52:35 +0100251 spi_flash_free(env_flash);
Patrick Delaunaya1b3eef2021-02-24 11:52:35 +0100252
Mike Frysinger9af10d52008-12-11 06:23:37 -0500253 if (saved_buffer)
254 free(saved_buffer);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000255
Mike Frysinger9af10d52008-12-11 06:23:37 -0500256 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200257}
258
Simon Glass99778492017-08-03 12:22:17 -0600259static int env_sf_load(void)
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200260{
261 int ret;
Ying Zhang354846f2014-01-24 15:50:07 +0800262 char *buf = NULL;
Patrick Delaunay625dd912021-02-24 11:52:36 +0100263 struct spi_flash *env_flash;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200264
Ravi Babu20d93fc2015-08-17 13:29:49 +0530265 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200266 if (!buf) {
Simon Glass97385862019-08-01 09:47:00 -0600267 env_set_default("malloc() failed", 0);
Simon Glass99778492017-08-03 12:22:17 -0600268 return -EIO;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200269 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200270
Patrick Delaunay625dd912021-02-24 11:52:36 +0100271 ret = setup_flash_device(&env_flash);
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200272 if (ret)
273 goto out;
274
Heiko Schocherf45775c2019-03-13 12:15:45 +0100275 ret = spi_flash_read(env_flash,
276 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200277 if (ret) {
Simon Glass97385862019-08-01 09:47:00 -0600278 env_set_default("spi_flash_read() failed", 0);
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200279 goto err_read;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200280 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200281
Marek Vasutdfe4b7e2020-07-07 20:51:35 +0200282 ret = env_import(buf, 1, H_EXTERNAL);
Simon Goldschmidt32c3eb52018-01-31 14:47:10 +0100283 if (!ret)
Simon Glass4bc2ad22017-08-03 12:21:56 -0600284 gd->env_valid = ENV_VALID;
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200285
286err_read:
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200287 spi_flash_free(env_flash);
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200288out:
289 free(buf);
Simon Glass99778492017-08-03 12:22:17 -0600290
291 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200292}
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200293#endif
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200294
Patrick Delaunayb1096c32021-02-09 11:48:52 +0100295static int env_sf_erase(void)
296{
297 int ret;
298 env_t env;
Patrick Delaunay625dd912021-02-24 11:52:36 +0100299 struct spi_flash *env_flash;
Patrick Delaunayb1096c32021-02-09 11:48:52 +0100300
Patrick Delaunay625dd912021-02-24 11:52:36 +0100301 ret = setup_flash_device(&env_flash);
Patrick Delaunayb1096c32021-02-09 11:48:52 +0100302 if (ret)
303 return ret;
304
305 memset(&env, 0, sizeof(env_t));
306 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, &env);
307 if (ret)
308 goto done;
309
310 if (ENV_OFFSET_REDUND != OFFSET_INVALID)
311 ret = spi_flash_write(env_flash, ENV_OFFSET_REDUND, CONFIG_ENV_SIZE, &env);
312
313done:
314 spi_flash_free(env_flash);
Patrick Delaunayb1096c32021-02-09 11:48:52 +0100315
316 return ret;
317}
318
Rajesh Bhagat9a90cc92018-11-05 18:01:15 +0000319__weak void *env_sf_get_env_addr(void)
320{
Pali Rohárdb64efd2022-03-02 12:47:51 +0100321#ifndef CONFIG_SPL_BUILD
Rajesh Bhagat9a90cc92018-11-05 18:01:15 +0000322 return (void *)CONFIG_ENV_ADDR;
Pali Rohárdb64efd2022-03-02 12:47:51 +0100323#else
324 return NULL;
Rajesh Bhagat9a90cc92018-11-05 18:01:15 +0000325#endif
Pali Rohárdb64efd2022-03-02 12:47:51 +0100326}
Rajesh Bhagat9a90cc92018-11-05 18:01:15 +0000327
Heiko Schocher704d21c2020-10-10 10:28:05 +0200328/*
329 * check if Environment on CONFIG_ENV_ADDR is valid.
330 */
331static int env_sf_init_addr(void)
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530332{
Rajesh Bhagat9a90cc92018-11-05 18:01:15 +0000333 env_t *env_ptr = (env_t *)env_sf_get_env_addr();
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530334
Pali Rohárdb64efd2022-03-02 12:47:51 +0100335 if (!env_ptr)
336 return -ENOENT;
337
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530338 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
Marek Behún1de1de42021-10-22 15:47:12 +0200339 gd->env_addr = (ulong)&(env_ptr->data);
Marek Behún792f2372021-10-22 15:47:13 +0200340 gd->env_valid = ENV_VALID;
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530341 } else {
Marek Behúnc33f42f2021-10-22 15:47:14 +0200342 gd->env_valid = ENV_INVALID;
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530343 }
344
345 return 0;
346}
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530347
Heiko Schocher704d21c2020-10-10 10:28:05 +0200348#if defined(CONFIG_ENV_SPI_EARLY)
349/*
350 * early load environment from SPI flash (before relocation)
351 * and check if it is valid.
352 */
353static int env_sf_init_early(void)
354{
355 int ret;
356 int read1_fail;
357 int read2_fail;
358 int crc1_ok;
359 env_t *tmp_env2 = NULL;
360 env_t *tmp_env1;
Patrick Delaunay625dd912021-02-24 11:52:36 +0100361 struct spi_flash *env_flash;
Heiko Schocher704d21c2020-10-10 10:28:05 +0200362
363 /*
364 * if malloc is not ready yet, we cannot use
365 * this part yet.
366 */
367 if (!gd->malloc_limit)
368 return -ENOENT;
369
370 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
371 CONFIG_ENV_SIZE);
372 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
373 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
374 CONFIG_ENV_SIZE);
375
376 if (!tmp_env1 || !tmp_env2)
377 goto out;
378
Patrick Delaunay625dd912021-02-24 11:52:36 +0100379 ret = setup_flash_device(&env_flash);
Heiko Schocher704d21c2020-10-10 10:28:05 +0200380 if (ret)
381 goto out;
382
383 read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
384 CONFIG_ENV_SIZE, tmp_env1);
385
386 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT)) {
387 read2_fail = spi_flash_read(env_flash,
388 CONFIG_ENV_OFFSET_REDUND,
389 CONFIG_ENV_SIZE, tmp_env2);
390 ret = env_check_redund((char *)tmp_env1, read1_fail,
391 (char *)tmp_env2, read2_fail);
392
Brandon Maier61374b02020-12-17 17:19:18 -0600393 if (ret < 0)
Heiko Schocher704d21c2020-10-10 10:28:05 +0200394 goto err_read;
395
396 if (gd->env_valid == ENV_VALID)
397 gd->env_addr = (unsigned long)&tmp_env1->data;
398 else
399 gd->env_addr = (unsigned long)&tmp_env2->data;
400 } else {
401 if (read1_fail)
402 goto err_read;
403
404 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
405 tmp_env1->crc;
406 if (!crc1_ok)
407 goto err_read;
408
409 /* if valid -> this is our env */
410 gd->env_valid = ENV_VALID;
411 gd->env_addr = (unsigned long)&tmp_env1->data;
412 }
413
Patrick Delaunaya1b3eef2021-02-24 11:52:35 +0100414 spi_flash_free(env_flash);
Patrick Delaunaya1b3eef2021-02-24 11:52:35 +0100415
Heiko Schocher704d21c2020-10-10 10:28:05 +0200416 return 0;
417err_read:
418 spi_flash_free(env_flash);
Patrick Delaunay625dd912021-02-24 11:52:36 +0100419
Heiko Schocher704d21c2020-10-10 10:28:05 +0200420 free(tmp_env1);
421 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
422 free(tmp_env2);
423out:
424 /* env is not valid. always return 0 */
425 gd->env_valid = ENV_INVALID;
426 return 0;
427}
428#endif
429
430static int env_sf_init(void)
431{
Pali Rohárdb64efd2022-03-02 12:47:51 +0100432 int ret = env_sf_init_addr();
433 if (ret != -ENOENT)
434 return ret;
435#ifdef CONFIG_ENV_SPI_EARLY
Heiko Schocher704d21c2020-10-10 10:28:05 +0200436 return env_sf_init_early();
437#endif
438 /*
Heiko Schocher8ba7b612020-11-03 15:22:36 +0100439 * return here -ENOENT, so env_init()
440 * can set the init bit and later if no
441 * other Environment storage is defined
442 * can set the default environment
Heiko Schocher704d21c2020-10-10 10:28:05 +0200443 */
Heiko Schocher8ba7b612020-11-03 15:22:36 +0100444 return -ENOENT;
Heiko Schocher704d21c2020-10-10 10:28:05 +0200445}
446
Simon Glassc10a88e2017-08-03 12:21:58 -0600447U_BOOT_ENV_LOCATION(sf) = {
448 .location = ENVL_SPI_FLASH,
Patrick Delaunaya4433de2020-07-28 11:51:16 +0200449 ENV_NAME("SPIFlash")
Simon Glass082af922017-08-03 12:22:01 -0600450 .load = env_sf_load,
Patrick Delaunay5c7c2d02021-02-09 11:48:51 +0100451 .save = ENV_SAVE_PTR(env_sf_save),
Patrick Delaunayb1096c32021-02-09 11:48:52 +0100452 .erase = ENV_ERASE_PTR(env_sf_erase),
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530453 .init = env_sf_init,
Simon Glassc10a88e2017-08-03 12:21:58 -0600454};