blob: e4b7ca9e0462f5771c7e0a675c29c9d46c07f676 [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
Patrick Delaunay625dd912021-02-24 11:52:36 +010047static int setup_flash_device(struct spi_flash **env_flash)
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020048{
Lukasz Majewski76f442982020-06-04 23:11:53 +080049#if CONFIG_IS_ENABLED(DM_SPI_FLASH)
Gong Qianyu6d85a602016-01-26 15:06:42 +080050 struct udevice *new;
Andreas Fenkart52b94442017-04-08 11:59:31 +020051 int ret;
Gong Qianyu6d85a602016-01-26 15:06:42 +080052
Vignesh Rae569792016-07-06 10:04:28 +053053 /* speed and mode will be read from DT */
Gong Qianyu6d85a602016-01-26 15:06:42 +080054 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
Konstantin Porotchkin0be67b22018-08-29 16:34:52 +030055 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE,
56 &new);
Gong Qianyu6d85a602016-01-26 15:06:42 +080057 if (ret) {
Simon Glass97385862019-08-01 09:47:00 -060058 env_set_default("spi_flash_probe_bus_cs() failed", 0);
Simon Glass99778492017-08-03 12:22:17 -060059 return ret;
Gong Qianyu6d85a602016-01-26 15:06:42 +080060 }
61
Patrick Delaunay625dd912021-02-24 11:52:36 +010062 *env_flash = dev_get_uclass_priv(new);
Gong Qianyu6d85a602016-01-26 15:06:42 +080063#else
Patrick Delaunay625dd912021-02-24 11:52:36 +010064 *env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
65 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
66 if (!*env_flash) {
Jagan Teki90173092020-05-13 16:32:42 +053067 env_set_default("spi_flash_probe() failed", 0);
68 return -EIO;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020069 }
Gong Qianyu6d85a602016-01-26 15:06:42 +080070#endif
Andreas Fenkart52b94442017-04-08 11:59:31 +020071 return 0;
72}
73
74#if defined(CONFIG_ENV_OFFSET_REDUND)
Simon Glass082af922017-08-03 12:22:01 -060075static int env_sf_save(void)
Andreas Fenkart52b94442017-04-08 11:59:31 +020076{
77 env_t env_new;
Simon Glass4686d8f2019-08-01 09:47:08 -060078 char *saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
Rasmus Villemoese535f5b2021-04-14 20:51:43 +020079 u32 saved_size = 0, saved_offset = 0, sector;
Rasmus Villemoes1ee19842021-04-14 20:51:42 +020080 u32 sect_size = CONFIG_ENV_SECT_SIZE;
Andreas Fenkart52b94442017-04-08 11:59:31 +020081 int ret;
Patrick Delaunay625dd912021-02-24 11:52:36 +010082 struct spi_flash *env_flash;
Andreas Fenkart52b94442017-04-08 11:59:31 +020083
Patrick Delaunay625dd912021-02-24 11:52:36 +010084 ret = setup_flash_device(&env_flash);
Andreas Fenkart52b94442017-04-08 11:59:31 +020085 if (ret)
86 return ret;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020087
Rasmus Villemoese535f5b2021-04-14 20:51:43 +020088 if (IS_ENABLED(CONFIG_ENV_SECT_SIZE_AUTO))
89 sect_size = env_flash->mtd.erasesize;
90
Marek Vasutd73c1292014-03-05 19:59:50 +010091 ret = env_export(&env_new);
92 if (ret)
Simon Glass99778492017-08-03 12:22:17 -060093 return -EIO;
Simon Glass4686d8f2019-08-01 09:47:08 -060094 env_new.flags = ENV_REDUND_ACTIVE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020095
Simon Glass4bc2ad22017-08-03 12:21:56 -060096 if (gd->env_valid == ENV_VALID) {
Stefano Babic5f6c2862010-10-27 11:06:20 +020097 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
98 env_offset = CONFIG_ENV_OFFSET;
99 } else {
100 env_new_offset = CONFIG_ENV_OFFSET;
101 env_offset = CONFIG_ENV_OFFSET_REDUND;
102 }
103
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200104 /* Is the sector larger than the env (i.e. embedded) */
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200105 if (sect_size > CONFIG_ENV_SIZE) {
106 saved_size = sect_size - CONFIG_ENV_SIZE;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200107 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
Ravi Babu20d93fc2015-08-17 13:29:49 +0530108 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200109 if (!saved_buffer) {
Simon Glass99778492017-08-03 12:22:17 -0600110 ret = -ENOMEM;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200111 goto done;
112 }
Heiko Schocherf45775c2019-03-13 12:15:45 +0100113 ret = spi_flash_read(env_flash, saved_offset,
114 saved_size, saved_buffer);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200115 if (ret)
116 goto done;
117 }
118
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200119 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200120
121 puts("Erasing SPI flash...");
122 ret = spi_flash_erase(env_flash, env_new_offset,
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200123 sector * sect_size);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200124 if (ret)
125 goto done;
126
127 puts("Writing to SPI flash...");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200128
Stefano Babic5f6c2862010-10-27 11:06:20 +0200129 ret = spi_flash_write(env_flash, env_new_offset,
Tom Rinia840b8e2013-04-05 14:55:21 -0400130 CONFIG_ENV_SIZE, &env_new);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200131 if (ret)
132 goto done;
133
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200134 if (sect_size > CONFIG_ENV_SIZE) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200135 ret = spi_flash_write(env_flash, saved_offset,
136 saved_size, saved_buffer);
137 if (ret)
138 goto done;
139 }
140
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000141 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
Tom Rinia840b8e2013-04-05 14:55:21 -0400142 sizeof(env_new.flags), &flag);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200143 if (ret)
144 goto done;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200145
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200146 puts("done\n");
147
Simon Glass4bc2ad22017-08-03 12:21:56 -0600148 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200149
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000150 printf("Valid environment: %d\n", (int)gd->env_valid);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200151
Patrick Delaunay480b6e62021-01-04 14:41:31 +0100152done:
Patrick Delaunaya1b3eef2021-02-24 11:52:35 +0100153 spi_flash_free(env_flash);
Patrick Delaunaya1b3eef2021-02-24 11:52:35 +0100154
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200155 if (saved_buffer)
156 free(saved_buffer);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000157
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200158 return ret;
159}
160
Simon Glass99778492017-08-03 12:22:17 -0600161static int env_sf_load(void)
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200162{
163 int ret;
Simon Goldschmidt8f9ce3a2018-01-31 14:47:13 +0100164 int read1_fail, read2_fail;
165 env_t *tmp_env1, *tmp_env2;
Patrick Delaunay625dd912021-02-24 11:52:36 +0100166 struct spi_flash *env_flash;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200167
Ravi Babu20d93fc2015-08-17 13:29:49 +0530168 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
169 CONFIG_ENV_SIZE);
170 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
171 CONFIG_ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200172 if (!tmp_env1 || !tmp_env2) {
Simon Glass97385862019-08-01 09:47:00 -0600173 env_set_default("malloc() failed", 0);
Simon Glass99778492017-08-03 12:22:17 -0600174 ret = -EIO;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000175 goto out;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200176 }
177
Patrick Delaunay625dd912021-02-24 11:52:36 +0100178 ret = setup_flash_device(&env_flash);
Andreas Fenkarta2d18832017-04-08 11:59:32 +0200179 if (ret)
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000180 goto out;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200181
Heiko Schocherf45775c2019-03-13 12:15:45 +0100182 read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
183 CONFIG_ENV_SIZE, tmp_env1);
184 read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
185 CONFIG_ENV_SIZE, tmp_env2);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200186
Simon Goldschmidt8f9ce3a2018-01-31 14:47:13 +0100187 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
Marek Vasutdfe4b7e2020-07-07 20:51:35 +0200188 read2_fail, H_EXTERNAL);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200189
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200190 spi_flash_free(env_flash);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200191out:
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200192 free(tmp_env1);
193 free(tmp_env2);
Simon Glass99778492017-08-03 12:22:17 -0600194
195 return ret;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200196}
197#else
Simon Glass082af922017-08-03 12:22:01 -0600198static int env_sf_save(void)
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200199{
Rasmus Villemoese535f5b2021-04-14 20:51:43 +0200200 u32 saved_size = 0, saved_offset = 0, sector;
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200201 u32 sect_size = CONFIG_ENV_SECT_SIZE;
Marek Vasutd73c1292014-03-05 19:59:50 +0100202 char *saved_buffer = NULL;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000203 int ret = 1;
Tom Rinia840b8e2013-04-05 14:55:21 -0400204 env_t env_new;
Patrick Delaunay625dd912021-02-24 11:52:36 +0100205 struct spi_flash *env_flash;
Gong Qianyu6d85a602016-01-26 15:06:42 +0800206
Patrick Delaunay625dd912021-02-24 11:52:36 +0100207 ret = setup_flash_device(&env_flash);
Andreas Fenkart52b94442017-04-08 11:59:31 +0200208 if (ret)
209 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200210
Rasmus Villemoese535f5b2021-04-14 20:51:43 +0200211 if (IS_ENABLED(CONFIG_ENV_SECT_SIZE_AUTO))
212 sect_size = env_flash->mtd.erasesize;
213
Mike Frysinger9af10d52008-12-11 06:23:37 -0500214 /* Is the sector larger than the env (i.e. embedded) */
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200215 if (sect_size > CONFIG_ENV_SIZE) {
216 saved_size = sect_size - CONFIG_ENV_SIZE;
Mike Frysinger9af10d52008-12-11 06:23:37 -0500217 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
218 saved_buffer = malloc(saved_size);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000219 if (!saved_buffer)
Mike Frysinger9af10d52008-12-11 06:23:37 -0500220 goto done;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000221
Heiko Schocherf45775c2019-03-13 12:15:45 +0100222 ret = spi_flash_read(env_flash, saved_offset,
223 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500224 if (ret)
225 goto done;
226 }
227
Marek Vasutd73c1292014-03-05 19:59:50 +0100228 ret = env_export(&env_new);
229 if (ret)
Stefano Babic5f6c2862010-10-27 11:06:20 +0200230 goto done;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200231
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200232 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size);
Andreas Fenkartdf701912017-04-08 11:59:34 +0200233
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200234 puts("Erasing SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200235 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200236 sector * sect_size);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500237 if (ret)
238 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200239
240 puts("Writing to SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200241 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
Tom Rinia840b8e2013-04-05 14:55:21 -0400242 CONFIG_ENV_SIZE, &env_new);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500243 if (ret)
244 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200245
Rasmus Villemoes1ee19842021-04-14 20:51:42 +0200246 if (sect_size > CONFIG_ENV_SIZE) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200247 ret = spi_flash_write(env_flash, saved_offset,
248 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500249 if (ret)
250 goto done;
251 }
252
253 ret = 0;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200254 puts("done\n");
Mike Frysinger9af10d52008-12-11 06:23:37 -0500255
Patrick Delaunay480b6e62021-01-04 14:41:31 +0100256done:
Patrick Delaunaya1b3eef2021-02-24 11:52:35 +0100257 spi_flash_free(env_flash);
Patrick Delaunaya1b3eef2021-02-24 11:52:35 +0100258
Mike Frysinger9af10d52008-12-11 06:23:37 -0500259 if (saved_buffer)
260 free(saved_buffer);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000261
Mike Frysinger9af10d52008-12-11 06:23:37 -0500262 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200263}
264
Simon Glass99778492017-08-03 12:22:17 -0600265static int env_sf_load(void)
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200266{
267 int ret;
Ying Zhang354846f2014-01-24 15:50:07 +0800268 char *buf = NULL;
Patrick Delaunay625dd912021-02-24 11:52:36 +0100269 struct spi_flash *env_flash;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200270
Ravi Babu20d93fc2015-08-17 13:29:49 +0530271 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200272 if (!buf) {
Simon Glass97385862019-08-01 09:47:00 -0600273 env_set_default("malloc() failed", 0);
Simon Glass99778492017-08-03 12:22:17 -0600274 return -EIO;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200275 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200276
Patrick Delaunay625dd912021-02-24 11:52:36 +0100277 ret = setup_flash_device(&env_flash);
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200278 if (ret)
279 goto out;
280
Heiko Schocherf45775c2019-03-13 12:15:45 +0100281 ret = spi_flash_read(env_flash,
282 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200283 if (ret) {
Simon Glass97385862019-08-01 09:47:00 -0600284 env_set_default("spi_flash_read() failed", 0);
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200285 goto err_read;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200286 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200287
Marek Vasutdfe4b7e2020-07-07 20:51:35 +0200288 ret = env_import(buf, 1, H_EXTERNAL);
Simon Goldschmidt32c3eb52018-01-31 14:47:10 +0100289 if (!ret)
Simon Glass4bc2ad22017-08-03 12:21:56 -0600290 gd->env_valid = ENV_VALID;
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200291
292err_read:
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200293 spi_flash_free(env_flash);
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200294out:
295 free(buf);
Simon Glass99778492017-08-03 12:22:17 -0600296
297 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200298}
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200299#endif
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200300
Patrick Delaunayb1096c32021-02-09 11:48:52 +0100301static int env_sf_erase(void)
302{
303 int ret;
304 env_t env;
Patrick Delaunay625dd912021-02-24 11:52:36 +0100305 struct spi_flash *env_flash;
Patrick Delaunayb1096c32021-02-09 11:48:52 +0100306
Patrick Delaunay625dd912021-02-24 11:52:36 +0100307 ret = setup_flash_device(&env_flash);
Patrick Delaunayb1096c32021-02-09 11:48:52 +0100308 if (ret)
309 return ret;
310
311 memset(&env, 0, sizeof(env_t));
312 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, &env);
313 if (ret)
314 goto done;
315
316 if (ENV_OFFSET_REDUND != OFFSET_INVALID)
317 ret = spi_flash_write(env_flash, ENV_OFFSET_REDUND, CONFIG_ENV_SIZE, &env);
318
319done:
320 spi_flash_free(env_flash);
Patrick Delaunayb1096c32021-02-09 11:48:52 +0100321
322 return ret;
323}
324
Tom Rini5cd7ece2019-11-18 20:02:10 -0500325#if CONFIG_ENV_ADDR != 0x0
Rajesh Bhagat9a90cc92018-11-05 18:01:15 +0000326__weak void *env_sf_get_env_addr(void)
327{
328 return (void *)CONFIG_ENV_ADDR;
329}
330#endif
331
Tom Rini5cd7ece2019-11-18 20:02:10 -0500332#if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0)
Heiko Schocher704d21c2020-10-10 10:28:05 +0200333/*
334 * check if Environment on CONFIG_ENV_ADDR is valid.
335 */
336static int env_sf_init_addr(void)
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530337{
Rajesh Bhagat9a90cc92018-11-05 18:01:15 +0000338 env_t *env_ptr = (env_t *)env_sf_get_env_addr();
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530339
340 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
341 gd->env_addr = (ulong)&(env_ptr->data);
342 gd->env_valid = 1;
343 } else {
344 gd->env_addr = (ulong)&default_environment[0];
345 gd->env_valid = 1;
346 }
347
348 return 0;
349}
350#endif
351
Heiko Schocher704d21c2020-10-10 10:28:05 +0200352#if defined(CONFIG_ENV_SPI_EARLY)
353/*
354 * early load environment from SPI flash (before relocation)
355 * and check if it is valid.
356 */
357static int env_sf_init_early(void)
358{
359 int ret;
360 int read1_fail;
361 int read2_fail;
362 int crc1_ok;
363 env_t *tmp_env2 = NULL;
364 env_t *tmp_env1;
Patrick Delaunay625dd912021-02-24 11:52:36 +0100365 struct spi_flash *env_flash;
Heiko Schocher704d21c2020-10-10 10:28:05 +0200366
367 /*
368 * if malloc is not ready yet, we cannot use
369 * this part yet.
370 */
371 if (!gd->malloc_limit)
372 return -ENOENT;
373
374 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
375 CONFIG_ENV_SIZE);
376 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
377 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
378 CONFIG_ENV_SIZE);
379
380 if (!tmp_env1 || !tmp_env2)
381 goto out;
382
Patrick Delaunay625dd912021-02-24 11:52:36 +0100383 ret = setup_flash_device(&env_flash);
Heiko Schocher704d21c2020-10-10 10:28:05 +0200384 if (ret)
385 goto out;
386
387 read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
388 CONFIG_ENV_SIZE, tmp_env1);
389
390 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT)) {
391 read2_fail = spi_flash_read(env_flash,
392 CONFIG_ENV_OFFSET_REDUND,
393 CONFIG_ENV_SIZE, tmp_env2);
394 ret = env_check_redund((char *)tmp_env1, read1_fail,
395 (char *)tmp_env2, read2_fail);
396
Brandon Maier61374b02020-12-17 17:19:18 -0600397 if (ret < 0)
Heiko Schocher704d21c2020-10-10 10:28:05 +0200398 goto err_read;
399
400 if (gd->env_valid == ENV_VALID)
401 gd->env_addr = (unsigned long)&tmp_env1->data;
402 else
403 gd->env_addr = (unsigned long)&tmp_env2->data;
404 } else {
405 if (read1_fail)
406 goto err_read;
407
408 crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
409 tmp_env1->crc;
410 if (!crc1_ok)
411 goto err_read;
412
413 /* if valid -> this is our env */
414 gd->env_valid = ENV_VALID;
415 gd->env_addr = (unsigned long)&tmp_env1->data;
416 }
417
Patrick Delaunaya1b3eef2021-02-24 11:52:35 +0100418 spi_flash_free(env_flash);
Patrick Delaunaya1b3eef2021-02-24 11:52:35 +0100419
Heiko Schocher704d21c2020-10-10 10:28:05 +0200420 return 0;
421err_read:
422 spi_flash_free(env_flash);
Patrick Delaunay625dd912021-02-24 11:52:36 +0100423
Heiko Schocher704d21c2020-10-10 10:28:05 +0200424 free(tmp_env1);
425 if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
426 free(tmp_env2);
427out:
428 /* env is not valid. always return 0 */
429 gd->env_valid = ENV_INVALID;
430 return 0;
431}
432#endif
433
434static int env_sf_init(void)
435{
436#if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0)
437 return env_sf_init_addr();
438#elif defined(CONFIG_ENV_SPI_EARLY)
439 return env_sf_init_early();
440#endif
441 /*
Heiko Schocher8ba7b612020-11-03 15:22:36 +0100442 * return here -ENOENT, so env_init()
443 * can set the init bit and later if no
444 * other Environment storage is defined
445 * can set the default environment
Heiko Schocher704d21c2020-10-10 10:28:05 +0200446 */
Heiko Schocher8ba7b612020-11-03 15:22:36 +0100447 return -ENOENT;
Heiko Schocher704d21c2020-10-10 10:28:05 +0200448}
449
Simon Glassc10a88e2017-08-03 12:21:58 -0600450U_BOOT_ENV_LOCATION(sf) = {
451 .location = ENVL_SPI_FLASH,
Patrick Delaunaya4433de2020-07-28 11:51:16 +0200452 ENV_NAME("SPIFlash")
Simon Glass082af922017-08-03 12:22:01 -0600453 .load = env_sf_load,
Patrick Delaunay5c7c2d02021-02-09 11:48:51 +0100454 .save = ENV_SAVE_PTR(env_sf_save),
Patrick Delaunayb1096c32021-02-09 11:48:52 +0100455 .erase = ENV_ERASE_PTR(env_sf_erase),
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530456 .init = env_sf_init,
Simon Glassc10a88e2017-08-03 12:21:58 -0600457};