blob: d2c07cd71687a4697f823e55d1f05672aa37c1c6 [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 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE,
52 &new);
Gong Qianyu6d85a602016-01-26 15:06:42 +080053 if (ret) {
Simon Glass97385862019-08-01 09:47:00 -060054 env_set_default("spi_flash_probe_bus_cs() failed", 0);
Simon Glass99778492017-08-03 12:22:17 -060055 return ret;
Gong Qianyu6d85a602016-01-26 15:06:42 +080056 }
57
Patrick Delaunay625dd912021-02-24 11:52:36 +010058 *env_flash = dev_get_uclass_priv(new);
Gong Qianyu6d85a602016-01-26 15:06:42 +080059#else
Patrick Delaunay625dd912021-02-24 11:52:36 +010060 *env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
61 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
62 if (!*env_flash) {
Jagan Teki90173092020-05-13 16:32:42 +053063 env_set_default("spi_flash_probe() failed", 0);
64 return -EIO;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020065 }
Gong Qianyu6d85a602016-01-26 15:06:42 +080066#endif
Andreas Fenkart52b94442017-04-08 11:59:31 +020067 return 0;
68}
69
70#if defined(CONFIG_ENV_OFFSET_REDUND)
Simon Glass082af922017-08-03 12:22:01 -060071static int env_sf_save(void)
Andreas Fenkart52b94442017-04-08 11:59:31 +020072{
73 env_t env_new;
Simon Glass4686d8f2019-08-01 09:47:08 -060074 char *saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
Rasmus Villemoese535f5b2021-04-14 20:51:43 +020075 u32 saved_size = 0, saved_offset = 0, sector;
Rasmus Villemoes1ee19842021-04-14 20:51:42 +020076 u32 sect_size = CONFIG_ENV_SECT_SIZE;
Andreas Fenkart52b94442017-04-08 11:59:31 +020077 int ret;
Patrick Delaunay625dd912021-02-24 11:52:36 +010078 struct spi_flash *env_flash;
Andreas Fenkart52b94442017-04-08 11:59:31 +020079
Patrick Delaunay625dd912021-02-24 11:52:36 +010080 ret = setup_flash_device(&env_flash);
Andreas Fenkart52b94442017-04-08 11:59:31 +020081 if (ret)
82 return ret;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020083
Rasmus Villemoese535f5b2021-04-14 20:51:43 +020084 if (IS_ENABLED(CONFIG_ENV_SECT_SIZE_AUTO))
85 sect_size = env_flash->mtd.erasesize;
86
Marek Vasutd73c1292014-03-05 19:59:50 +010087 ret = env_export(&env_new);
88 if (ret)
Simon Glass99778492017-08-03 12:22:17 -060089 return -EIO;
Simon Glass4686d8f2019-08-01 09:47:08 -060090 env_new.flags = ENV_REDUND_ACTIVE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020091
Simon Glass4bc2ad22017-08-03 12:21:56 -060092 if (gd->env_valid == ENV_VALID) {
Stefano Babic5f6c2862010-10-27 11:06:20 +020093 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
94 env_offset = CONFIG_ENV_OFFSET;
95 } else {
96 env_new_offset = CONFIG_ENV_OFFSET;
97 env_offset = CONFIG_ENV_OFFSET_REDUND;
98 }
99
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200100 /* Is the sector larger than the env (i.e. embedded) */
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200101 if (sect_size > CONFIG_ENV_SIZE) {
102 saved_size = sect_size - CONFIG_ENV_SIZE;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200103 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
Ravi Babu20d93fc2015-08-17 13:29:49 +0530104 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200105 if (!saved_buffer) {
Simon Glass99778492017-08-03 12:22:17 -0600106 ret = -ENOMEM;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200107 goto done;
108 }
Heiko Schocherf45775c2019-03-13 12:15:45 +0100109 ret = spi_flash_read(env_flash, saved_offset,
110 saved_size, saved_buffer);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200111 if (ret)
112 goto done;
113 }
114
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200115 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200116
117 puts("Erasing SPI flash...");
118 ret = spi_flash_erase(env_flash, env_new_offset,
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200119 sector * sect_size);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200120 if (ret)
121 goto done;
122
123 puts("Writing to SPI flash...");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200124
Stefano Babic5f6c2862010-10-27 11:06:20 +0200125 ret = spi_flash_write(env_flash, env_new_offset,
Tom Rinia840b8e2013-04-05 14:55:21 -0400126 CONFIG_ENV_SIZE, &env_new);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200127 if (ret)
128 goto done;
129
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200130 if (sect_size > CONFIG_ENV_SIZE) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200131 ret = spi_flash_write(env_flash, saved_offset,
132 saved_size, saved_buffer);
133 if (ret)
134 goto done;
135 }
136
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000137 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
Tom Rinia840b8e2013-04-05 14:55:21 -0400138 sizeof(env_new.flags), &flag);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200139 if (ret)
140 goto done;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200141
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200142 puts("done\n");
143
Simon Glass4bc2ad22017-08-03 12:21:56 -0600144 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200145
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000146 printf("Valid environment: %d\n", (int)gd->env_valid);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200147
Patrick Delaunay480b6e62021-01-04 14:41:31 +0100148done:
Patrick Delaunaya1b3eef2021-02-24 11:52:35 +0100149 spi_flash_free(env_flash);
Patrick Delaunaya1b3eef2021-02-24 11:52:35 +0100150
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200151 if (saved_buffer)
152 free(saved_buffer);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000153
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200154 return ret;
155}
156
Simon Glass99778492017-08-03 12:22:17 -0600157static int env_sf_load(void)
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200158{
159 int ret;
Simon Goldschmidt8f9ce3a2018-01-31 14:47:13 +0100160 int read1_fail, read2_fail;
161 env_t *tmp_env1, *tmp_env2;
Patrick Delaunay625dd912021-02-24 11:52:36 +0100162 struct spi_flash *env_flash;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200163
Ravi Babu20d93fc2015-08-17 13:29:49 +0530164 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
165 CONFIG_ENV_SIZE);
166 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
167 CONFIG_ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200168 if (!tmp_env1 || !tmp_env2) {
Simon Glass97385862019-08-01 09:47:00 -0600169 env_set_default("malloc() failed", 0);
Simon Glass99778492017-08-03 12:22:17 -0600170 ret = -EIO;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000171 goto out;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200172 }
173
Patrick Delaunay625dd912021-02-24 11:52:36 +0100174 ret = setup_flash_device(&env_flash);
Andreas Fenkarta2d18832017-04-08 11:59:32 +0200175 if (ret)
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000176 goto out;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200177
Heiko Schocherf45775c2019-03-13 12:15:45 +0100178 read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
179 CONFIG_ENV_SIZE, tmp_env1);
180 read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
181 CONFIG_ENV_SIZE, tmp_env2);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200182
Simon Goldschmidt8f9ce3a2018-01-31 14:47:13 +0100183 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
Marek Vasutdfe4b7e2020-07-07 20:51:35 +0200184 read2_fail, H_EXTERNAL);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200185
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200186 spi_flash_free(env_flash);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200187out:
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200188 free(tmp_env1);
189 free(tmp_env2);
Simon Glass99778492017-08-03 12:22:17 -0600190
191 return ret;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200192}
193#else
Simon Glass082af922017-08-03 12:22:01 -0600194static int env_sf_save(void)
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200195{
Rasmus Villemoese535f5b2021-04-14 20:51:43 +0200196 u32 saved_size = 0, saved_offset = 0, sector;
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200197 u32 sect_size = CONFIG_ENV_SECT_SIZE;
Marek Vasutd73c1292014-03-05 19:59:50 +0100198 char *saved_buffer = NULL;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000199 int ret = 1;
Tom Rinia840b8e2013-04-05 14:55:21 -0400200 env_t env_new;
Patrick Delaunay625dd912021-02-24 11:52:36 +0100201 struct spi_flash *env_flash;
Gong Qianyu6d85a602016-01-26 15:06:42 +0800202
Patrick Delaunay625dd912021-02-24 11:52:36 +0100203 ret = setup_flash_device(&env_flash);
Andreas Fenkart52b94442017-04-08 11:59:31 +0200204 if (ret)
205 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200206
Rasmus Villemoese535f5b2021-04-14 20:51:43 +0200207 if (IS_ENABLED(CONFIG_ENV_SECT_SIZE_AUTO))
208 sect_size = env_flash->mtd.erasesize;
209
Mike Frysinger9af10d52008-12-11 06:23:37 -0500210 /* Is the sector larger than the env (i.e. embedded) */
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200211 if (sect_size > CONFIG_ENV_SIZE) {
212 saved_size = sect_size - CONFIG_ENV_SIZE;
Mike Frysinger9af10d52008-12-11 06:23:37 -0500213 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
214 saved_buffer = malloc(saved_size);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000215 if (!saved_buffer)
Mike Frysinger9af10d52008-12-11 06:23:37 -0500216 goto done;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000217
Heiko Schocherf45775c2019-03-13 12:15:45 +0100218 ret = spi_flash_read(env_flash, saved_offset,
219 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500220 if (ret)
221 goto done;
222 }
223
Marek Vasutd73c1292014-03-05 19:59:50 +0100224 ret = env_export(&env_new);
225 if (ret)
Stefano Babic5f6c2862010-10-27 11:06:20 +0200226 goto done;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200227
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200228 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size);
Andreas Fenkartdf701912017-04-08 11:59:34 +0200229
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200230 puts("Erasing SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200231 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200232 sector * sect_size);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500233 if (ret)
234 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200235
236 puts("Writing to SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200237 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
Tom Rinia840b8e2013-04-05 14:55:21 -0400238 CONFIG_ENV_SIZE, &env_new);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500239 if (ret)
240 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200241
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200242 if (sect_size > CONFIG_ENV_SIZE) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200243 ret = spi_flash_write(env_flash, saved_offset,
244 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500245 if (ret)
246 goto done;
247 }
248
249 ret = 0;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200250 puts("done\n");
Mike Frysinger9af10d52008-12-11 06:23:37 -0500251
Patrick Delaunay480b6e62021-01-04 14:41:31 +0100252done:
Patrick Delaunaya1b3eef2021-02-24 11:52:35 +0100253 spi_flash_free(env_flash);
Patrick Delaunaya1b3eef2021-02-24 11:52:35 +0100254
Mike Frysinger9af10d52008-12-11 06:23:37 -0500255 if (saved_buffer)
256 free(saved_buffer);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000257
Mike Frysinger9af10d52008-12-11 06:23:37 -0500258 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200259}
260
Simon Glass99778492017-08-03 12:22:17 -0600261static int env_sf_load(void)
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200262{
263 int ret;
Ying Zhang354846f2014-01-24 15:50:07 +0800264 char *buf = NULL;
Patrick Delaunay625dd912021-02-24 11:52:36 +0100265 struct spi_flash *env_flash;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200266
Ravi Babu20d93fc2015-08-17 13:29:49 +0530267 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200268 if (!buf) {
Simon Glass97385862019-08-01 09:47:00 -0600269 env_set_default("malloc() failed", 0);
Simon Glass99778492017-08-03 12:22:17 -0600270 return -EIO;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200271 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200272
Patrick Delaunay625dd912021-02-24 11:52:36 +0100273 ret = setup_flash_device(&env_flash);
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200274 if (ret)
275 goto out;
276
Heiko Schocherf45775c2019-03-13 12:15:45 +0100277 ret = spi_flash_read(env_flash,
278 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200279 if (ret) {
Simon Glass97385862019-08-01 09:47:00 -0600280 env_set_default("spi_flash_read() failed", 0);
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200281 goto err_read;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200282 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200283
Marek Vasutdfe4b7e2020-07-07 20:51:35 +0200284 ret = env_import(buf, 1, H_EXTERNAL);
Simon Goldschmidt32c3eb52018-01-31 14:47:10 +0100285 if (!ret)
Simon Glass4bc2ad22017-08-03 12:21:56 -0600286 gd->env_valid = ENV_VALID;
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200287
288err_read:
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200289 spi_flash_free(env_flash);
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200290out:
291 free(buf);
Simon Glass99778492017-08-03 12:22:17 -0600292
293 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200294}
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200295#endif
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200296
Patrick Delaunayb1096c32021-02-09 11:48:52 +0100297static int env_sf_erase(void)
298{
299 int ret;
300 env_t env;
Patrick Delaunay625dd912021-02-24 11:52:36 +0100301 struct spi_flash *env_flash;
Patrick Delaunayb1096c32021-02-09 11:48:52 +0100302
Patrick Delaunay625dd912021-02-24 11:52:36 +0100303 ret = setup_flash_device(&env_flash);
Patrick Delaunayb1096c32021-02-09 11:48:52 +0100304 if (ret)
305 return ret;
306
307 memset(&env, 0, sizeof(env_t));
308 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, &env);
309 if (ret)
310 goto done;
311
312 if (ENV_OFFSET_REDUND != OFFSET_INVALID)
313 ret = spi_flash_write(env_flash, ENV_OFFSET_REDUND, CONFIG_ENV_SIZE, &env);
314
315done:
316 spi_flash_free(env_flash);
Patrick Delaunayb1096c32021-02-09 11:48:52 +0100317
318 return ret;
319}
320
Rajesh Bhagat9a90cc92018-11-05 18:01:15 +0000321__weak void *env_sf_get_env_addr(void)
322{
Pali Rohárdb64efd2022-03-02 12:47:51 +0100323#ifndef CONFIG_SPL_BUILD
Rajesh Bhagat9a90cc92018-11-05 18:01:15 +0000324 return (void *)CONFIG_ENV_ADDR;
Pali Rohárdb64efd2022-03-02 12:47:51 +0100325#else
326 return NULL;
Rajesh Bhagat9a90cc92018-11-05 18:01:15 +0000327#endif
Pali Rohárdb64efd2022-03-02 12:47:51 +0100328}
Rajesh Bhagat9a90cc92018-11-05 18:01:15 +0000329
Heiko Schocher704d21c2020-10-10 10:28:05 +0200330/*
331 * check if Environment on CONFIG_ENV_ADDR is valid.
332 */
333static int env_sf_init_addr(void)
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530334{
Rajesh Bhagat9a90cc92018-11-05 18:01:15 +0000335 env_t *env_ptr = (env_t *)env_sf_get_env_addr();
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530336
Pali Rohárdb64efd2022-03-02 12:47:51 +0100337 if (!env_ptr)
338 return -ENOENT;
339
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530340 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
Marek Behún1de1de42021-10-22 15:47:12 +0200341 gd->env_addr = (ulong)&(env_ptr->data);
Marek Behún792f2372021-10-22 15:47:13 +0200342 gd->env_valid = ENV_VALID;
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530343 } else {
Marek Behúnc33f42f2021-10-22 15:47:14 +0200344 gd->env_valid = ENV_INVALID;
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530345 }
346
347 return 0;
348}
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530349
Heiko Schocher704d21c2020-10-10 10:28:05 +0200350#if defined(CONFIG_ENV_SPI_EARLY)
351/*
352 * early load environment from SPI flash (before relocation)
353 * and check if it is valid.
354 */
355static int env_sf_init_early(void)
356{
357 int ret;
358 int read1_fail;
359 int read2_fail;
360 int crc1_ok;
361 env_t *tmp_env2 = NULL;
362 env_t *tmp_env1;
Patrick Delaunay625dd912021-02-24 11:52:36 +0100363 struct spi_flash *env_flash;
Heiko Schocher704d21c2020-10-10 10:28:05 +0200364
365 /*
366 * if malloc is not ready yet, we cannot use
367 * this part yet.
368 */
369 if (!gd->malloc_limit)
370 return -ENOENT;
371
372 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
373 CONFIG_ENV_SIZE);
374 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
375 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
376 CONFIG_ENV_SIZE);
377
378 if (!tmp_env1 || !tmp_env2)
379 goto out;
380
Patrick Delaunay625dd912021-02-24 11:52:36 +0100381 ret = setup_flash_device(&env_flash);
Heiko Schocher704d21c2020-10-10 10:28:05 +0200382 if (ret)
383 goto out;
384
385 read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
386 CONFIG_ENV_SIZE, tmp_env1);
387
388 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT)) {
389 read2_fail = spi_flash_read(env_flash,
390 CONFIG_ENV_OFFSET_REDUND,
391 CONFIG_ENV_SIZE, tmp_env2);
392 ret = env_check_redund((char *)tmp_env1, read1_fail,
393 (char *)tmp_env2, read2_fail);
394
Brandon Maier61374b02020-12-17 17:19:18 -0600395 if (ret < 0)
Heiko Schocher704d21c2020-10-10 10:28:05 +0200396 goto err_read;
397
398 if (gd->env_valid == ENV_VALID)
399 gd->env_addr = (unsigned long)&tmp_env1->data;
400 else
401 gd->env_addr = (unsigned long)&tmp_env2->data;
402 } else {
403 if (read1_fail)
404 goto err_read;
405
406 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
407 tmp_env1->crc;
408 if (!crc1_ok)
409 goto err_read;
410
411 /* if valid -> this is our env */
412 gd->env_valid = ENV_VALID;
413 gd->env_addr = (unsigned long)&tmp_env1->data;
414 }
415
Patrick Delaunaya1b3eef2021-02-24 11:52:35 +0100416 spi_flash_free(env_flash);
Patrick Delaunaya1b3eef2021-02-24 11:52:35 +0100417
Heiko Schocher704d21c2020-10-10 10:28:05 +0200418 return 0;
419err_read:
420 spi_flash_free(env_flash);
Patrick Delaunay625dd912021-02-24 11:52:36 +0100421
Heiko Schocher704d21c2020-10-10 10:28:05 +0200422 free(tmp_env1);
423 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
424 free(tmp_env2);
425out:
426 /* env is not valid. always return 0 */
427 gd->env_valid = ENV_INVALID;
428 return 0;
429}
430#endif
431
432static int env_sf_init(void)
433{
Pali Rohárdb64efd2022-03-02 12:47:51 +0100434 int ret = env_sf_init_addr();
435 if (ret != -ENOENT)
436 return ret;
437#ifdef CONFIG_ENV_SPI_EARLY
Heiko Schocher704d21c2020-10-10 10:28:05 +0200438 return env_sf_init_early();
439#endif
440 /*
Heiko Schocher8ba7b612020-11-03 15:22:36 +0100441 * return here -ENOENT, so env_init()
442 * can set the init bit and later if no
443 * other Environment storage is defined
444 * can set the default environment
Heiko Schocher704d21c2020-10-10 10:28:05 +0200445 */
Heiko Schocher8ba7b612020-11-03 15:22:36 +0100446 return -ENOENT;
Heiko Schocher704d21c2020-10-10 10:28:05 +0200447}
448
Simon Glassc10a88e2017-08-03 12:21:58 -0600449U_BOOT_ENV_LOCATION(sf) = {
450 .location = ENVL_SPI_FLASH,
Patrick Delaunaya4433de2020-07-28 11:51:16 +0200451 ENV_NAME("SPIFlash")
Simon Glass082af922017-08-03 12:22:01 -0600452 .load = env_sf_load,
Patrick Delaunay5c7c2d02021-02-09 11:48:51 +0100453 .save = ENV_SAVE_PTR(env_sf_save),
Patrick Delaunayb1096c32021-02-09 11:48:52 +0100454 .erase = ENV_ERASE_PTR(env_sf_erase),
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530455 .init = env_sf_init,
Simon Glassc10a88e2017-08-03 12:21:58 -0600456};