blob: eb4c8d5ec95162f4c9ba9e0a92b1b508f74cbea9 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Haavard Skinnemoen8c664972008-05-16 11:10:35 +02002/*
Wolfgang Denkea882ba2010-06-20 23:33:59 +02003 * (C) Copyright 2000-2010
Haavard Skinnemoen8c664972008-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 Skinnemoen8c664972008-05-16 11:10:35 +020010 */
Simon Glass9d922452017-05-17 17:18:03 -060011#include <dm.h>
Simon Glass0ac7d722019-08-01 09:47:00 -060012#include <env.h>
Simon Glassf3998fd2019-08-02 09:44:25 -060013#include <env_internal.h>
Mike Frysinger5b3375a2008-12-11 06:23:37 -050014#include <malloc.h>
Simon Glass843c9e82014-10-13 23:41:55 -060015#include <spi.h>
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020016#include <spi_flash.h>
Wolfgang Denkea882ba2010-06-20 23:33:59 +020017#include <search.h>
18#include <errno.h>
Caleb Connolly58d825f2024-08-30 13:34:37 +010019#include <u-boot/uuid.h>
Simon Glass90526e92020-05-10 11:39:56 -060020#include <asm/cache.h>
Simon Glass401d1c42020-10-30 21:38:53 -060021#include <asm/global_data.h>
Gong Qianyu19c31282016-01-26 15:06:42 +080022#include <dm/device-internal.h>
Simon Glass3db71102019-11-14 12:57:16 -070023#include <u-boot/crc.h>
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020024
Patrick Delaunay25d90ad2021-02-09 11:48:52 +010025#define OFFSET_INVALID (~(u32)0)
26
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020027#ifdef CONFIG_ENV_OFFSET_REDUND
Patrick Delaunay25d90ad2021-02-09 11:48:52 +010028#define ENV_OFFSET_REDUND CONFIG_ENV_OFFSET_REDUND
29
Igor Grinbergeb58a7f2011-11-07 01:14:08 +000030static ulong env_offset = CONFIG_ENV_OFFSET;
31static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
Patrick Delaunay25d90ad2021-02-09 11:48:52 +010032
33#else
34
35#define ENV_OFFSET_REDUND OFFSET_INVALID
36
Stefano Babica3110f02010-10-27 11:06:20 +020037#endif /* CONFIG_ENV_OFFSET_REDUND */
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020038
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020039DECLARE_GLOBAL_DATA_PTR;
40
Venkatesh Yadav Abbarapu7d84ad12024-06-14 18:18:10 +053041__weak int spi_get_env_dev(void)
42{
43#ifdef CONFIG_ENV_SPI_BUS
44 return CONFIG_ENV_SPI_BUS;
45#else
46 return 0;
47#endif
48}
49
Patrick Delaunayecf15442021-02-24 11:52:36 +010050static int setup_flash_device(struct spi_flash **env_flash)
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020051{
Lukasz Majewski56c40462020-06-04 23:11:53 +080052#if CONFIG_IS_ENABLED(DM_SPI_FLASH)
Gong Qianyu19c31282016-01-26 15:06:42 +080053 struct udevice *new;
Andreas Fenkartafa81a72017-04-08 11:59:31 +020054 int ret;
Venkatesh Yadav Abbarapu7d84ad12024-06-14 18:18:10 +053055 int dev = spi_get_env_dev();
Gong Qianyu19c31282016-01-26 15:06:42 +080056
Vignesh R96907c02016-07-06 10:04:28 +053057 /* speed and mode will be read from DT */
Venkatesh Yadav Abbarapu7d84ad12024-06-14 18:18:10 +053058 ret = spi_flash_probe_bus_cs(dev, CONFIG_ENV_SPI_CS,
Konstantin Porotchkin25a17652018-08-29 16:34:52 +030059 &new);
Gong Qianyu19c31282016-01-26 15:06:42 +080060 if (ret) {
Simon Glass0ac7d722019-08-01 09:47:00 -060061 env_set_default("spi_flash_probe_bus_cs() failed", 0);
Simon Glassc5951992017-08-03 12:22:17 -060062 return ret;
Gong Qianyu19c31282016-01-26 15:06:42 +080063 }
64
Patrick Delaunayecf15442021-02-24 11:52:36 +010065 *env_flash = dev_get_uclass_priv(new);
Gong Qianyu19c31282016-01-26 15:06:42 +080066#else
Patrick Delaunayecf15442021-02-24 11:52:36 +010067 *env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
68 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
69 if (!*env_flash) {
Jagan Tekic8cbf172020-05-13 16:32:42 +053070 env_set_default("spi_flash_probe() failed", 0);
71 return -EIO;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020072 }
Gong Qianyu19c31282016-01-26 15:06:42 +080073#endif
Andreas Fenkartafa81a72017-04-08 11:59:31 +020074 return 0;
75}
76
77#if defined(CONFIG_ENV_OFFSET_REDUND)
Simon Glasse5bce242017-08-03 12:22:01 -060078static int env_sf_save(void)
Andreas Fenkartafa81a72017-04-08 11:59:31 +020079{
80 env_t env_new;
Simon Glassd3716dd2019-08-01 09:47:08 -060081 char *saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
Rasmus Villemoesbcb44f62021-04-14 20:51:43 +020082 u32 saved_size = 0, saved_offset = 0, sector;
Rasmus Villemoes4da73472021-04-14 20:51:42 +020083 u32 sect_size = CONFIG_ENV_SECT_SIZE;
Andreas Fenkartafa81a72017-04-08 11:59:31 +020084 int ret;
Patrick Delaunayecf15442021-02-24 11:52:36 +010085 struct spi_flash *env_flash;
Andreas Fenkartafa81a72017-04-08 11:59:31 +020086
Patrick Delaunayecf15442021-02-24 11:52:36 +010087 ret = setup_flash_device(&env_flash);
Andreas Fenkartafa81a72017-04-08 11:59:31 +020088 if (ret)
89 return ret;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020090
Rasmus Villemoesbcb44f62021-04-14 20:51:43 +020091 if (IS_ENABLED(CONFIG_ENV_SECT_SIZE_AUTO))
92 sect_size = env_flash->mtd.erasesize;
93
Marek Vasut7ce15262014-03-05 19:59:50 +010094 ret = env_export(&env_new);
95 if (ret)
Simon Glassc5951992017-08-03 12:22:17 -060096 return -EIO;
Simon Glassd3716dd2019-08-01 09:47:08 -060097 env_new.flags = ENV_REDUND_ACTIVE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020098
Simon Glass203e94f2017-08-03 12:21:56 -060099 if (gd->env_valid == ENV_VALID) {
Stefano Babica3110f02010-10-27 11:06:20 +0200100 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
101 env_offset = CONFIG_ENV_OFFSET;
102 } else {
103 env_new_offset = CONFIG_ENV_OFFSET;
104 env_offset = CONFIG_ENV_OFFSET_REDUND;
105 }
106
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200107 /* Is the sector larger than the env (i.e. embedded) */
Rasmus Villemoes4da73472021-04-14 20:51:42 +0200108 if (sect_size > CONFIG_ENV_SIZE) {
109 saved_size = sect_size - CONFIG_ENV_SIZE;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200110 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
Ravi Babu7dd01742015-08-17 13:29:49 +0530111 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200112 if (!saved_buffer) {
Simon Glassc5951992017-08-03 12:22:17 -0600113 ret = -ENOMEM;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200114 goto done;
115 }
Heiko Schocher9ba5e5b2019-03-13 12:15:45 +0100116 ret = spi_flash_read(env_flash, saved_offset,
117 saved_size, saved_buffer);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200118 if (ret)
119 goto done;
120 }
121
Rasmus Villemoes4da73472021-04-14 20:51:42 +0200122 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200123
124 puts("Erasing SPI flash...");
125 ret = spi_flash_erase(env_flash, env_new_offset,
Rasmus Villemoes4da73472021-04-14 20:51:42 +0200126 sector * sect_size);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200127 if (ret)
128 goto done;
129
130 puts("Writing to SPI flash...");
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200131
Stefano Babica3110f02010-10-27 11:06:20 +0200132 ret = spi_flash_write(env_flash, env_new_offset,
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400133 CONFIG_ENV_SIZE, &env_new);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200134 if (ret)
135 goto done;
136
Rasmus Villemoes4da73472021-04-14 20:51:42 +0200137 if (sect_size > CONFIG_ENV_SIZE) {
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200138 ret = spi_flash_write(env_flash, saved_offset,
139 saved_size, saved_buffer);
140 if (ret)
141 goto done;
142 }
143
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000144 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400145 sizeof(env_new.flags), &flag);
Stefano Babica3110f02010-10-27 11:06:20 +0200146 if (ret)
147 goto done;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200148
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200149 puts("done\n");
150
Simon Glass203e94f2017-08-03 12:21:56 -0600151 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
Stefano Babica3110f02010-10-27 11:06:20 +0200152
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000153 printf("Valid environment: %d\n", (int)gd->env_valid);
Stefano Babica3110f02010-10-27 11:06:20 +0200154
Patrick Delaunayea5641e2021-01-04 14:41:31 +0100155done:
Patrick Delaunayc2d00362021-02-24 11:52:35 +0100156 spi_flash_free(env_flash);
Patrick Delaunayc2d00362021-02-24 11:52:35 +0100157
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200158 if (saved_buffer)
159 free(saved_buffer);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000160
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200161 return ret;
162}
163
Simon Glassc5951992017-08-03 12:22:17 -0600164static int env_sf_load(void)
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200165{
166 int ret;
Simon Goldschmidt80719932018-01-31 14:47:13 +0100167 int read1_fail, read2_fail;
168 env_t *tmp_env1, *tmp_env2;
Patrick Delaunayecf15442021-02-24 11:52:36 +0100169 struct spi_flash *env_flash;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200170
Ravi Babu7dd01742015-08-17 13:29:49 +0530171 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
172 CONFIG_ENV_SIZE);
173 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
174 CONFIG_ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200175 if (!tmp_env1 || !tmp_env2) {
Simon Glass0ac7d722019-08-01 09:47:00 -0600176 env_set_default("malloc() failed", 0);
Simon Glassc5951992017-08-03 12:22:17 -0600177 ret = -EIO;
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000178 goto out;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200179 }
180
Patrick Delaunayecf15442021-02-24 11:52:36 +0100181 ret = setup_flash_device(&env_flash);
Andreas Fenkart8fee8842017-04-08 11:59:32 +0200182 if (ret)
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000183 goto out;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200184
Heiko Schocher9ba5e5b2019-03-13 12:15:45 +0100185 read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
186 CONFIG_ENV_SIZE, tmp_env1);
187 read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
188 CONFIG_ENV_SIZE, tmp_env2);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200189
Simon Goldschmidt80719932018-01-31 14:47:13 +0100190 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
Marek Vasut890feec2020-07-07 20:51:35 +0200191 read2_fail, H_EXTERNAL);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200192
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200193 spi_flash_free(env_flash);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200194out:
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200195 free(tmp_env1);
196 free(tmp_env2);
Simon Glassc5951992017-08-03 12:22:17 -0600197
198 return ret;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200199}
200#else
Simon Glasse5bce242017-08-03 12:22:01 -0600201static int env_sf_save(void)
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200202{
Rasmus Villemoesbcb44f62021-04-14 20:51:43 +0200203 u32 saved_size = 0, saved_offset = 0, sector;
Rasmus Villemoes4da73472021-04-14 20:51:42 +0200204 u32 sect_size = CONFIG_ENV_SECT_SIZE;
Marek Vasut7ce15262014-03-05 19:59:50 +0100205 char *saved_buffer = NULL;
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000206 int ret = 1;
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400207 env_t env_new;
Patrick Delaunayecf15442021-02-24 11:52:36 +0100208 struct spi_flash *env_flash;
Gong Qianyu19c31282016-01-26 15:06:42 +0800209
Patrick Delaunayecf15442021-02-24 11:52:36 +0100210 ret = setup_flash_device(&env_flash);
Andreas Fenkartafa81a72017-04-08 11:59:31 +0200211 if (ret)
212 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200213
Rasmus Villemoesbcb44f62021-04-14 20:51:43 +0200214 if (IS_ENABLED(CONFIG_ENV_SECT_SIZE_AUTO))
215 sect_size = env_flash->mtd.erasesize;
216
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500217 /* Is the sector larger than the env (i.e. embedded) */
Rasmus Villemoes4da73472021-04-14 20:51:42 +0200218 if (sect_size > CONFIG_ENV_SIZE) {
219 saved_size = sect_size - CONFIG_ENV_SIZE;
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500220 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
221 saved_buffer = malloc(saved_size);
Ralph Siemsen04add62e2024-01-19 16:32:17 -0500222 if (!saved_buffer) {
223 ret = -ENOMEM;
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500224 goto done;
Ralph Siemsen04add62e2024-01-19 16:32:17 -0500225 }
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000226
Heiko Schocher9ba5e5b2019-03-13 12:15:45 +0100227 ret = spi_flash_read(env_flash, saved_offset,
228 saved_size, saved_buffer);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500229 if (ret)
230 goto done;
231 }
232
Marek Vasut7ce15262014-03-05 19:59:50 +0100233 ret = env_export(&env_new);
234 if (ret)
Stefano Babica3110f02010-10-27 11:06:20 +0200235 goto done;
Stefano Babica3110f02010-10-27 11:06:20 +0200236
Rasmus Villemoes4da73472021-04-14 20:51:42 +0200237 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size);
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +0200238
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200239 puts("Erasing SPI flash...");
Stefano Babica3110f02010-10-27 11:06:20 +0200240 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
Rasmus Villemoes4da73472021-04-14 20:51:42 +0200241 sector * sect_size);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500242 if (ret)
243 goto done;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200244
245 puts("Writing to SPI flash...");
Stefano Babica3110f02010-10-27 11:06:20 +0200246 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400247 CONFIG_ENV_SIZE, &env_new);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500248 if (ret)
249 goto done;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200250
Rasmus Villemoes4da73472021-04-14 20:51:42 +0200251 if (sect_size > CONFIG_ENV_SIZE) {
Stefano Babica3110f02010-10-27 11:06:20 +0200252 ret = spi_flash_write(env_flash, saved_offset,
253 saved_size, saved_buffer);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500254 if (ret)
255 goto done;
256 }
257
258 ret = 0;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200259 puts("done\n");
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500260
Patrick Delaunayea5641e2021-01-04 14:41:31 +0100261done:
Patrick Delaunayc2d00362021-02-24 11:52:35 +0100262 spi_flash_free(env_flash);
Patrick Delaunayc2d00362021-02-24 11:52:35 +0100263
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500264 if (saved_buffer)
265 free(saved_buffer);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000266
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500267 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200268}
269
Simon Glassc5951992017-08-03 12:22:17 -0600270static int env_sf_load(void)
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200271{
272 int ret;
Ying Zhang5a89fa92014-01-24 15:50:07 +0800273 char *buf = NULL;
Patrick Delaunayecf15442021-02-24 11:52:36 +0100274 struct spi_flash *env_flash;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200275
Ravi Babu7dd01742015-08-17 13:29:49 +0530276 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
Andreas Fenkartc041c602017-04-08 11:59:33 +0200277 if (!buf) {
Simon Glass0ac7d722019-08-01 09:47:00 -0600278 env_set_default("malloc() failed", 0);
Simon Glassc5951992017-08-03 12:22:17 -0600279 return -EIO;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200280 }
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200281
Patrick Delaunayecf15442021-02-24 11:52:36 +0100282 ret = setup_flash_device(&env_flash);
Andreas Fenkartc041c602017-04-08 11:59:33 +0200283 if (ret)
284 goto out;
285
Heiko Schocher9ba5e5b2019-03-13 12:15:45 +0100286 ret = spi_flash_read(env_flash,
287 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200288 if (ret) {
Simon Glass0ac7d722019-08-01 09:47:00 -0600289 env_set_default("spi_flash_read() failed", 0);
Andreas Fenkartc041c602017-04-08 11:59:33 +0200290 goto err_read;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200291 }
292
Marek Vasut890feec2020-07-07 20:51:35 +0200293 ret = env_import(buf, 1, H_EXTERNAL);
Simon Goldschmidt42a18202018-01-31 14:47:10 +0100294 if (!ret)
Simon Glass203e94f2017-08-03 12:21:56 -0600295 gd->env_valid = ENV_VALID;
Andreas Fenkartc041c602017-04-08 11:59:33 +0200296
297err_read:
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200298 spi_flash_free(env_flash);
Andreas Fenkartc041c602017-04-08 11:59:33 +0200299out:
300 free(buf);
Simon Glassc5951992017-08-03 12:22:17 -0600301
302 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200303}
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200304#endif
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200305
Patrick Delaunay25d90ad2021-02-09 11:48:52 +0100306static int env_sf_erase(void)
307{
308 int ret;
309 env_t env;
Patrick Delaunayecf15442021-02-24 11:52:36 +0100310 struct spi_flash *env_flash;
Patrick Delaunay25d90ad2021-02-09 11:48:52 +0100311
Patrick Delaunayecf15442021-02-24 11:52:36 +0100312 ret = setup_flash_device(&env_flash);
Patrick Delaunay25d90ad2021-02-09 11:48:52 +0100313 if (ret)
314 return ret;
315
316 memset(&env, 0, sizeof(env_t));
317 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, &env);
318 if (ret)
319 goto done;
320
321 if (ENV_OFFSET_REDUND != OFFSET_INVALID)
322 ret = spi_flash_write(env_flash, ENV_OFFSET_REDUND, CONFIG_ENV_SIZE, &env);
323
324done:
325 spi_flash_free(env_flash);
Patrick Delaunay25d90ad2021-02-09 11:48:52 +0100326
327 return ret;
328}
329
Rajesh Bhagat119c01c22018-11-05 18:01:15 +0000330__weak void *env_sf_get_env_addr(void)
331{
Simon Glass1d6132e2024-09-29 19:49:50 -0600332#ifndef CONFIG_XPL_BUILD
Rajesh Bhagat119c01c22018-11-05 18:01:15 +0000333 return (void *)CONFIG_ENV_ADDR;
Pali Rohárf4bf81b2022-03-02 12:47:51 +0100334#else
335 return NULL;
Rajesh Bhagat119c01c22018-11-05 18:01:15 +0000336#endif
Pali Rohárf4bf81b2022-03-02 12:47:51 +0100337}
Rajesh Bhagat119c01c22018-11-05 18:01:15 +0000338
Heiko Schocher92765f452020-10-10 10:28:05 +0200339/*
340 * check if Environment on CONFIG_ENV_ADDR is valid.
341 */
342static int env_sf_init_addr(void)
Ashish Kumarb500c922017-12-14 17:37:08 +0530343{
Rajesh Bhagat119c01c22018-11-05 18:01:15 +0000344 env_t *env_ptr = (env_t *)env_sf_get_env_addr();
Ashish Kumarb500c922017-12-14 17:37:08 +0530345
Pali Rohárf4bf81b2022-03-02 12:47:51 +0100346 if (!env_ptr)
347 return -ENOENT;
348
Ashish Kumarb500c922017-12-14 17:37:08 +0530349 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
Marek Behún9ab30d22021-10-22 15:47:12 +0200350 gd->env_addr = (ulong)&(env_ptr->data);
Marek Behún87221bd2021-10-22 15:47:13 +0200351 gd->env_valid = ENV_VALID;
Ashish Kumarb500c922017-12-14 17:37:08 +0530352 } else {
Marek Behúnaf8149e2021-10-22 15:47:14 +0200353 gd->env_valid = ENV_INVALID;
Ashish Kumarb500c922017-12-14 17:37:08 +0530354 }
355
356 return 0;
357}
Ashish Kumarb500c922017-12-14 17:37:08 +0530358
Heiko Schocher92765f452020-10-10 10:28:05 +0200359#if defined(CONFIG_ENV_SPI_EARLY)
360/*
361 * early load environment from SPI flash (before relocation)
362 * and check if it is valid.
363 */
364static int env_sf_init_early(void)
365{
366 int ret;
367 int read1_fail;
368 int read2_fail;
369 int crc1_ok;
370 env_t *tmp_env2 = NULL;
371 env_t *tmp_env1;
Patrick Delaunayecf15442021-02-24 11:52:36 +0100372 struct spi_flash *env_flash;
Heiko Schocher92765f452020-10-10 10:28:05 +0200373
374 /*
375 * if malloc is not ready yet, we cannot use
376 * this part yet.
377 */
378 if (!gd->malloc_limit)
379 return -ENOENT;
380
381 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
382 CONFIG_ENV_SIZE);
383 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
384 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
385 CONFIG_ENV_SIZE);
386
387 if (!tmp_env1 || !tmp_env2)
388 goto out;
389
Patrick Delaunayecf15442021-02-24 11:52:36 +0100390 ret = setup_flash_device(&env_flash);
Heiko Schocher92765f452020-10-10 10:28:05 +0200391 if (ret)
392 goto out;
393
394 read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
395 CONFIG_ENV_SIZE, tmp_env1);
396
397 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT)) {
398 read2_fail = spi_flash_read(env_flash,
399 CONFIG_ENV_OFFSET_REDUND,
400 CONFIG_ENV_SIZE, tmp_env2);
401 ret = env_check_redund((char *)tmp_env1, read1_fail,
402 (char *)tmp_env2, read2_fail);
403
Brandon Maierb9c30522020-12-17 17:19:18 -0600404 if (ret < 0)
Heiko Schocher92765f452020-10-10 10:28:05 +0200405 goto err_read;
406
407 if (gd->env_valid == ENV_VALID)
408 gd->env_addr = (unsigned long)&tmp_env1->data;
409 else
410 gd->env_addr = (unsigned long)&tmp_env2->data;
411 } else {
412 if (read1_fail)
413 goto err_read;
414
415 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
416 tmp_env1->crc;
417 if (!crc1_ok)
418 goto err_read;
419
420 /* if valid -> this is our env */
421 gd->env_valid = ENV_VALID;
422 gd->env_addr = (unsigned long)&tmp_env1->data;
423 }
424
Patrick Delaunayc2d00362021-02-24 11:52:35 +0100425 spi_flash_free(env_flash);
Patrick Delaunayc2d00362021-02-24 11:52:35 +0100426
Heiko Schocher92765f452020-10-10 10:28:05 +0200427 return 0;
428err_read:
429 spi_flash_free(env_flash);
Patrick Delaunayecf15442021-02-24 11:52:36 +0100430
Heiko Schocher92765f452020-10-10 10:28:05 +0200431 free(tmp_env1);
432 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
433 free(tmp_env2);
434out:
435 /* env is not valid. always return 0 */
436 gd->env_valid = ENV_INVALID;
437 return 0;
438}
439#endif
440
441static int env_sf_init(void)
442{
Pali Rohárf4bf81b2022-03-02 12:47:51 +0100443 int ret = env_sf_init_addr();
444 if (ret != -ENOENT)
445 return ret;
446#ifdef CONFIG_ENV_SPI_EARLY
Heiko Schocher92765f452020-10-10 10:28:05 +0200447 return env_sf_init_early();
448#endif
449 /*
Heiko Schocher46ce9e72020-11-03 15:22:36 +0100450 * return here -ENOENT, so env_init()
451 * can set the init bit and later if no
452 * other Environment storage is defined
453 * can set the default environment
Heiko Schocher92765f452020-10-10 10:28:05 +0200454 */
Heiko Schocher46ce9e72020-11-03 15:22:36 +0100455 return -ENOENT;
Heiko Schocher92765f452020-10-10 10:28:05 +0200456}
457
Simon Glass4415f1d2017-08-03 12:21:58 -0600458U_BOOT_ENV_LOCATION(sf) = {
459 .location = ENVL_SPI_FLASH,
Patrick Delaunay22140d12020-07-28 11:51:16 +0200460 ENV_NAME("SPIFlash")
Simon Glasse5bce242017-08-03 12:22:01 -0600461 .load = env_sf_load,
Patrick Delaunaye41f55b2021-02-09 11:48:51 +0100462 .save = ENV_SAVE_PTR(env_sf_save),
Patrick Delaunay25d90ad2021-02-09 11:48:52 +0100463 .erase = ENV_ERASE_PTR(env_sf_erase),
Ashish Kumarb500c922017-12-14 17:37:08 +0530464 .init = env_sf_init,
Simon Glass4415f1d2017-08-03 12:21:58 -0600465};