blob: 4b768542c14c0a7787c1528702a3838bd9bd43ec [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
Patrick Delaunayb1096c32021-02-09 11:48:52 +010027#define OFFSET_INVALID (~(u32)0)
28
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020029#ifdef CONFIG_ENV_OFFSET_REDUND
Patrick Delaunayb1096c32021-02-09 11:48:52 +010030#define ENV_OFFSET_REDUND CONFIG_ENV_OFFSET_REDUND
31
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000032static ulong env_offset = CONFIG_ENV_OFFSET;
33static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
Patrick Delaunayb1096c32021-02-09 11:48:52 +010034
35#else
36
37#define ENV_OFFSET_REDUND OFFSET_INVALID
38
Stefano Babic5f6c2862010-10-27 11:06:20 +020039#endif /* CONFIG_ENV_OFFSET_REDUND */
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020040
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020041DECLARE_GLOBAL_DATA_PTR;
42
Patrick Delaunay625dd912021-02-24 11:52:36 +010043static int setup_flash_device(struct spi_flash **env_flash)
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020044{
Lukasz Majewski76f442982020-06-04 23:11:53 +080045#if CONFIG_IS_ENABLED(DM_SPI_FLASH)
Gong Qianyu6d85a602016-01-26 15:06:42 +080046 struct udevice *new;
Andreas Fenkart52b94442017-04-08 11:59:31 +020047 int ret;
Gong Qianyu6d85a602016-01-26 15:06:42 +080048
Vignesh Rae569792016-07-06 10:04:28 +053049 /* speed and mode will be read from DT */
Gong Qianyu6d85a602016-01-26 15:06:42 +080050 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
Konstantin Porotchkin0be67b22018-08-29 16:34:52 +030051 &new);
Gong Qianyu6d85a602016-01-26 15:06:42 +080052 if (ret) {
Simon Glass97385862019-08-01 09:47:00 -060053 env_set_default("spi_flash_probe_bus_cs() failed", 0);
Simon Glass99778492017-08-03 12:22:17 -060054 return ret;
Gong Qianyu6d85a602016-01-26 15:06:42 +080055 }
56
Patrick Delaunay625dd912021-02-24 11:52:36 +010057 *env_flash = dev_get_uclass_priv(new);
Gong Qianyu6d85a602016-01-26 15:06:42 +080058#else
Patrick Delaunay625dd912021-02-24 11:52:36 +010059 *env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
60 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
61 if (!*env_flash) {
Jagan Teki90173092020-05-13 16:32:42 +053062 env_set_default("spi_flash_probe() failed", 0);
63 return -EIO;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020064 }
Gong Qianyu6d85a602016-01-26 15:06:42 +080065#endif
Andreas Fenkart52b94442017-04-08 11:59:31 +020066 return 0;
67}
68
69#if defined(CONFIG_ENV_OFFSET_REDUND)
Simon Glass082af922017-08-03 12:22:01 -060070static int env_sf_save(void)
Andreas Fenkart52b94442017-04-08 11:59:31 +020071{
72 env_t env_new;
Simon Glass4686d8f2019-08-01 09:47:08 -060073 char *saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
Rasmus Villemoese535f5b2021-04-14 20:51:43 +020074 u32 saved_size = 0, saved_offset = 0, sector;
Rasmus Villemoes1ee19842021-04-14 20:51:42 +020075 u32 sect_size = CONFIG_ENV_SECT_SIZE;
Andreas Fenkart52b94442017-04-08 11:59:31 +020076 int ret;
Patrick Delaunay625dd912021-02-24 11:52:36 +010077 struct spi_flash *env_flash;
Andreas Fenkart52b94442017-04-08 11:59:31 +020078
Patrick Delaunay625dd912021-02-24 11:52:36 +010079 ret = setup_flash_device(&env_flash);
Andreas Fenkart52b94442017-04-08 11:59:31 +020080 if (ret)
81 return ret;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020082
Rasmus Villemoese535f5b2021-04-14 20:51:43 +020083 if (IS_ENABLED(CONFIG_ENV_SECT_SIZE_AUTO))
84 sect_size = env_flash->mtd.erasesize;
85
Marek Vasutd73c1292014-03-05 19:59:50 +010086 ret = env_export(&env_new);
87 if (ret)
Simon Glass99778492017-08-03 12:22:17 -060088 return -EIO;
Simon Glass4686d8f2019-08-01 09:47:08 -060089 env_new.flags = ENV_REDUND_ACTIVE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020090
Simon Glass4bc2ad22017-08-03 12:21:56 -060091 if (gd->env_valid == ENV_VALID) {
Stefano Babic5f6c2862010-10-27 11:06:20 +020092 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
93 env_offset = CONFIG_ENV_OFFSET;
94 } else {
95 env_new_offset = CONFIG_ENV_OFFSET;
96 env_offset = CONFIG_ENV_OFFSET_REDUND;
97 }
98
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020099 /* Is the sector larger than the env (i.e. embedded) */
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200100 if (sect_size > CONFIG_ENV_SIZE) {
101 saved_size = sect_size - CONFIG_ENV_SIZE;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200102 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
Ravi Babu20d93fc2015-08-17 13:29:49 +0530103 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200104 if (!saved_buffer) {
Simon Glass99778492017-08-03 12:22:17 -0600105 ret = -ENOMEM;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200106 goto done;
107 }
Heiko Schocherf45775c2019-03-13 12:15:45 +0100108 ret = spi_flash_read(env_flash, saved_offset,
109 saved_size, saved_buffer);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200110 if (ret)
111 goto done;
112 }
113
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200114 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200115
116 puts("Erasing SPI flash...");
117 ret = spi_flash_erase(env_flash, env_new_offset,
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200118 sector * sect_size);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200119 if (ret)
120 goto done;
121
122 puts("Writing to SPI flash...");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200123
Stefano Babic5f6c2862010-10-27 11:06:20 +0200124 ret = spi_flash_write(env_flash, env_new_offset,
Tom Rinia840b8e2013-04-05 14:55:21 -0400125 CONFIG_ENV_SIZE, &env_new);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200126 if (ret)
127 goto done;
128
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200129 if (sect_size > CONFIG_ENV_SIZE) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200130 ret = spi_flash_write(env_flash, saved_offset,
131 saved_size, saved_buffer);
132 if (ret)
133 goto done;
134 }
135
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000136 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
Tom Rinia840b8e2013-04-05 14:55:21 -0400137 sizeof(env_new.flags), &flag);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200138 if (ret)
139 goto done;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200140
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200141 puts("done\n");
142
Simon Glass4bc2ad22017-08-03 12:21:56 -0600143 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200144
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000145 printf("Valid environment: %d\n", (int)gd->env_valid);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200146
Patrick Delaunay480b6e62021-01-04 14:41:31 +0100147done:
Patrick Delaunaya1b3eef2021-02-24 11:52:35 +0100148 spi_flash_free(env_flash);
Patrick Delaunaya1b3eef2021-02-24 11:52:35 +0100149
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200150 if (saved_buffer)
151 free(saved_buffer);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000152
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200153 return ret;
154}
155
Simon Glass99778492017-08-03 12:22:17 -0600156static int env_sf_load(void)
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200157{
158 int ret;
Simon Goldschmidt8f9ce3a2018-01-31 14:47:13 +0100159 int read1_fail, read2_fail;
160 env_t *tmp_env1, *tmp_env2;
Patrick Delaunay625dd912021-02-24 11:52:36 +0100161 struct spi_flash *env_flash;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200162
Ravi Babu20d93fc2015-08-17 13:29:49 +0530163 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
164 CONFIG_ENV_SIZE);
165 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
166 CONFIG_ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200167 if (!tmp_env1 || !tmp_env2) {
Simon Glass97385862019-08-01 09:47:00 -0600168 env_set_default("malloc() failed", 0);
Simon Glass99778492017-08-03 12:22:17 -0600169 ret = -EIO;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000170 goto out;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200171 }
172
Patrick Delaunay625dd912021-02-24 11:52:36 +0100173 ret = setup_flash_device(&env_flash);
Andreas Fenkarta2d18832017-04-08 11:59:32 +0200174 if (ret)
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000175 goto out;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200176
Heiko Schocherf45775c2019-03-13 12:15:45 +0100177 read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
178 CONFIG_ENV_SIZE, tmp_env1);
179 read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
180 CONFIG_ENV_SIZE, tmp_env2);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200181
Simon Goldschmidt8f9ce3a2018-01-31 14:47:13 +0100182 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
Marek Vasutdfe4b7e2020-07-07 20:51:35 +0200183 read2_fail, H_EXTERNAL);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200184
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200185 spi_flash_free(env_flash);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200186out:
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200187 free(tmp_env1);
188 free(tmp_env2);
Simon Glass99778492017-08-03 12:22:17 -0600189
190 return ret;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200191}
192#else
Simon Glass082af922017-08-03 12:22:01 -0600193static int env_sf_save(void)
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200194{
Rasmus Villemoese535f5b2021-04-14 20:51:43 +0200195 u32 saved_size = 0, saved_offset = 0, sector;
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200196 u32 sect_size = CONFIG_ENV_SECT_SIZE;
Marek Vasutd73c1292014-03-05 19:59:50 +0100197 char *saved_buffer = NULL;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000198 int ret = 1;
Tom Rinia840b8e2013-04-05 14:55:21 -0400199 env_t env_new;
Patrick Delaunay625dd912021-02-24 11:52:36 +0100200 struct spi_flash *env_flash;
Gong Qianyu6d85a602016-01-26 15:06:42 +0800201
Patrick Delaunay625dd912021-02-24 11:52:36 +0100202 ret = setup_flash_device(&env_flash);
Andreas Fenkart52b94442017-04-08 11:59:31 +0200203 if (ret)
204 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200205
Rasmus Villemoese535f5b2021-04-14 20:51:43 +0200206 if (IS_ENABLED(CONFIG_ENV_SECT_SIZE_AUTO))
207 sect_size = env_flash->mtd.erasesize;
208
Mike Frysinger9af10d52008-12-11 06:23:37 -0500209 /* Is the sector larger than the env (i.e. embedded) */
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200210 if (sect_size > CONFIG_ENV_SIZE) {
211 saved_size = sect_size - CONFIG_ENV_SIZE;
Mike Frysinger9af10d52008-12-11 06:23:37 -0500212 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
213 saved_buffer = malloc(saved_size);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000214 if (!saved_buffer)
Mike Frysinger9af10d52008-12-11 06:23:37 -0500215 goto done;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000216
Heiko Schocherf45775c2019-03-13 12:15:45 +0100217 ret = spi_flash_read(env_flash, saved_offset,
218 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500219 if (ret)
220 goto done;
221 }
222
Marek Vasutd73c1292014-03-05 19:59:50 +0100223 ret = env_export(&env_new);
224 if (ret)
Stefano Babic5f6c2862010-10-27 11:06:20 +0200225 goto done;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200226
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200227 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size);
Andreas Fenkartdf701912017-04-08 11:59:34 +0200228
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200229 puts("Erasing SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200230 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200231 sector * sect_size);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500232 if (ret)
233 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200234
235 puts("Writing to SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200236 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
Tom Rinia840b8e2013-04-05 14:55:21 -0400237 CONFIG_ENV_SIZE, &env_new);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500238 if (ret)
239 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200240
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200241 if (sect_size > CONFIG_ENV_SIZE) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200242 ret = spi_flash_write(env_flash, saved_offset,
243 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500244 if (ret)
245 goto done;
246 }
247
248 ret = 0;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200249 puts("done\n");
Mike Frysinger9af10d52008-12-11 06:23:37 -0500250
Patrick Delaunay480b6e62021-01-04 14:41:31 +0100251done:
Patrick Delaunaya1b3eef2021-02-24 11:52:35 +0100252 spi_flash_free(env_flash);
Patrick Delaunaya1b3eef2021-02-24 11:52:35 +0100253
Mike Frysinger9af10d52008-12-11 06:23:37 -0500254 if (saved_buffer)
255 free(saved_buffer);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000256
Mike Frysinger9af10d52008-12-11 06:23:37 -0500257 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200258}
259
Simon Glass99778492017-08-03 12:22:17 -0600260static int env_sf_load(void)
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200261{
262 int ret;
Ying Zhang354846f2014-01-24 15:50:07 +0800263 char *buf = NULL;
Patrick Delaunay625dd912021-02-24 11:52:36 +0100264 struct spi_flash *env_flash;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200265
Ravi Babu20d93fc2015-08-17 13:29:49 +0530266 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200267 if (!buf) {
Simon Glass97385862019-08-01 09:47:00 -0600268 env_set_default("malloc() failed", 0);
Simon Glass99778492017-08-03 12:22:17 -0600269 return -EIO;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200270 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200271
Patrick Delaunay625dd912021-02-24 11:52:36 +0100272 ret = setup_flash_device(&env_flash);
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200273 if (ret)
274 goto out;
275
Heiko Schocherf45775c2019-03-13 12:15:45 +0100276 ret = spi_flash_read(env_flash,
277 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200278 if (ret) {
Simon Glass97385862019-08-01 09:47:00 -0600279 env_set_default("spi_flash_read() failed", 0);
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200280 goto err_read;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200281 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200282
Marek Vasutdfe4b7e2020-07-07 20:51:35 +0200283 ret = env_import(buf, 1, H_EXTERNAL);
Simon Goldschmidt32c3eb52018-01-31 14:47:10 +0100284 if (!ret)
Simon Glass4bc2ad22017-08-03 12:21:56 -0600285 gd->env_valid = ENV_VALID;
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200286
287err_read:
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200288 spi_flash_free(env_flash);
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200289out:
290 free(buf);
Simon Glass99778492017-08-03 12:22:17 -0600291
292 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200293}
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200294#endif
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200295
Patrick Delaunayb1096c32021-02-09 11:48:52 +0100296static int env_sf_erase(void)
297{
298 int ret;
299 env_t env;
Patrick Delaunay625dd912021-02-24 11:52:36 +0100300 struct spi_flash *env_flash;
Patrick Delaunayb1096c32021-02-09 11:48:52 +0100301
Patrick Delaunay625dd912021-02-24 11:52:36 +0100302 ret = setup_flash_device(&env_flash);
Patrick Delaunayb1096c32021-02-09 11:48:52 +0100303 if (ret)
304 return ret;
305
306 memset(&env, 0, sizeof(env_t));
307 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, &env);
308 if (ret)
309 goto done;
310
311 if (ENV_OFFSET_REDUND != OFFSET_INVALID)
312 ret = spi_flash_write(env_flash, ENV_OFFSET_REDUND, CONFIG_ENV_SIZE, &env);
313
314done:
315 spi_flash_free(env_flash);
Patrick Delaunayb1096c32021-02-09 11:48:52 +0100316
317 return ret;
318}
319
Rajesh Bhagat9a90cc92018-11-05 18:01:15 +0000320__weak void *env_sf_get_env_addr(void)
321{
Pali Rohárdb64efd2022-03-02 12:47:51 +0100322#ifndef CONFIG_SPL_BUILD
Rajesh Bhagat9a90cc92018-11-05 18:01:15 +0000323 return (void *)CONFIG_ENV_ADDR;
Pali Rohárdb64efd2022-03-02 12:47:51 +0100324#else
325 return NULL;
Rajesh Bhagat9a90cc92018-11-05 18:01:15 +0000326#endif
Pali Rohárdb64efd2022-03-02 12:47:51 +0100327}
Rajesh Bhagat9a90cc92018-11-05 18:01:15 +0000328
Heiko Schocher704d21c2020-10-10 10:28:05 +0200329/*
330 * check if Environment on CONFIG_ENV_ADDR is valid.
331 */
332static int env_sf_init_addr(void)
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530333{
Rajesh Bhagat9a90cc92018-11-05 18:01:15 +0000334 env_t *env_ptr = (env_t *)env_sf_get_env_addr();
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530335
Pali Rohárdb64efd2022-03-02 12:47:51 +0100336 if (!env_ptr)
337 return -ENOENT;
338
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530339 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
Marek Behún1de1de42021-10-22 15:47:12 +0200340 gd->env_addr = (ulong)&(env_ptr->data);
Marek Behún792f2372021-10-22 15:47:13 +0200341 gd->env_valid = ENV_VALID;
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530342 } else {
Marek Behúnc33f42f2021-10-22 15:47:14 +0200343 gd->env_valid = ENV_INVALID;
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530344 }
345
346 return 0;
347}
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530348
Heiko Schocher704d21c2020-10-10 10:28:05 +0200349#if defined(CONFIG_ENV_SPI_EARLY)
350/*
351 * early load environment from SPI flash (before relocation)
352 * and check if it is valid.
353 */
354static int env_sf_init_early(void)
355{
356 int ret;
357 int read1_fail;
358 int read2_fail;
359 int crc1_ok;
360 env_t *tmp_env2 = NULL;
361 env_t *tmp_env1;
Patrick Delaunay625dd912021-02-24 11:52:36 +0100362 struct spi_flash *env_flash;
Heiko Schocher704d21c2020-10-10 10:28:05 +0200363
364 /*
365 * if malloc is not ready yet, we cannot use
366 * this part yet.
367 */
368 if (!gd->malloc_limit)
369 return -ENOENT;
370
371 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
372 CONFIG_ENV_SIZE);
373 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
374 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
375 CONFIG_ENV_SIZE);
376
377 if (!tmp_env1 || !tmp_env2)
378 goto out;
379
Patrick Delaunay625dd912021-02-24 11:52:36 +0100380 ret = setup_flash_device(&env_flash);
Heiko Schocher704d21c2020-10-10 10:28:05 +0200381 if (ret)
382 goto out;
383
384 read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
385 CONFIG_ENV_SIZE, tmp_env1);
386
387 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT)) {
388 read2_fail = spi_flash_read(env_flash,
389 CONFIG_ENV_OFFSET_REDUND,
390 CONFIG_ENV_SIZE, tmp_env2);
391 ret = env_check_redund((char *)tmp_env1, read1_fail,
392 (char *)tmp_env2, read2_fail);
393
Brandon Maier61374b02020-12-17 17:19:18 -0600394 if (ret < 0)
Heiko Schocher704d21c2020-10-10 10:28:05 +0200395 goto err_read;
396
397 if (gd->env_valid == ENV_VALID)
398 gd->env_addr = (unsigned long)&tmp_env1->data;
399 else
400 gd->env_addr = (unsigned long)&tmp_env2->data;
401 } else {
402 if (read1_fail)
403 goto err_read;
404
405 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
406 tmp_env1->crc;
407 if (!crc1_ok)
408 goto err_read;
409
410 /* if valid -> this is our env */
411 gd->env_valid = ENV_VALID;
412 gd->env_addr = (unsigned long)&tmp_env1->data;
413 }
414
Patrick Delaunaya1b3eef2021-02-24 11:52:35 +0100415 spi_flash_free(env_flash);
Patrick Delaunaya1b3eef2021-02-24 11:52:35 +0100416
Heiko Schocher704d21c2020-10-10 10:28:05 +0200417 return 0;
418err_read:
419 spi_flash_free(env_flash);
Patrick Delaunay625dd912021-02-24 11:52:36 +0100420
Heiko Schocher704d21c2020-10-10 10:28:05 +0200421 free(tmp_env1);
422 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
423 free(tmp_env2);
424out:
425 /* env is not valid. always return 0 */
426 gd->env_valid = ENV_INVALID;
427 return 0;
428}
429#endif
430
431static int env_sf_init(void)
432{
Pali Rohárdb64efd2022-03-02 12:47:51 +0100433 int ret = env_sf_init_addr();
434 if (ret != -ENOENT)
435 return ret;
436#ifdef CONFIG_ENV_SPI_EARLY
Heiko Schocher704d21c2020-10-10 10:28:05 +0200437 return env_sf_init_early();
438#endif
439 /*
Heiko Schocher8ba7b612020-11-03 15:22:36 +0100440 * return here -ENOENT, so env_init()
441 * can set the init bit and later if no
442 * other Environment storage is defined
443 * can set the default environment
Heiko Schocher704d21c2020-10-10 10:28:05 +0200444 */
Heiko Schocher8ba7b612020-11-03 15:22:36 +0100445 return -ENOENT;
Heiko Schocher704d21c2020-10-10 10:28:05 +0200446}
447
Simon Glassc10a88e2017-08-03 12:21:58 -0600448U_BOOT_ENV_LOCATION(sf) = {
449 .location = ENVL_SPI_FLASH,
Patrick Delaunaya4433de2020-07-28 11:51:16 +0200450 ENV_NAME("SPIFlash")
Simon Glass082af922017-08-03 12:22:01 -0600451 .load = env_sf_load,
Patrick Delaunay5c7c2d02021-02-09 11:48:51 +0100452 .save = ENV_SAVE_PTR(env_sf_save),
Patrick Delaunayb1096c32021-02-09 11:48:52 +0100453 .erase = ENV_ERASE_PTR(env_sf_erase),
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530454 .init = env_sf_init,
Simon Glassc10a88e2017-08-03 12:21:58 -0600455};