blob: e4b7ca9e0462f5771c7e0a675c29c9d46c07f676 [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 */
11#include <common.h>
Simon Glass9d922452017-05-17 17:18:03 -060012#include <dm.h>
Simon Glass0ac7d722019-08-01 09:47:00 -060013#include <env.h>
Simon Glassf3998fd2019-08-02 09:44:25 -060014#include <env_internal.h>
Simon Glassb79fdc72020-05-10 11:39:54 -060015#include <flash.h>
Mike Frysinger5b3375a2008-12-11 06:23:37 -050016#include <malloc.h>
Simon Glass843c9e82014-10-13 23:41:55 -060017#include <spi.h>
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020018#include <spi_flash.h>
Wolfgang Denkea882ba2010-06-20 23:33:59 +020019#include <search.h>
20#include <errno.h>
Simon Glassb79fdc72020-05-10 11:39:54 -060021#include <uuid.h>
Simon Glass90526e92020-05-10 11:39:56 -060022#include <asm/cache.h>
Simon Glass401d1c42020-10-30 21:38:53 -060023#include <asm/global_data.h>
Gong Qianyu19c31282016-01-26 15:06:42 +080024#include <dm/device-internal.h>
Simon Glass3db71102019-11-14 12:57:16 -070025#include <u-boot/crc.h>
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020026
Simon Glass4415f1d2017-08-03 12:21:58 -060027#ifndef CONFIG_SPL_BUILD
Ashish Kumarb500c922017-12-14 17:37:08 +053028#define INITENV
Simon Glass4415f1d2017-08-03 12:21:58 -060029#endif
30
Patrick Delaunay25d90ad2021-02-09 11:48:52 +010031#define OFFSET_INVALID (~(u32)0)
32
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020033#ifdef CONFIG_ENV_OFFSET_REDUND
Patrick Delaunay25d90ad2021-02-09 11:48:52 +010034#define ENV_OFFSET_REDUND CONFIG_ENV_OFFSET_REDUND
35
Igor Grinbergeb58a7f2011-11-07 01:14:08 +000036static ulong env_offset = CONFIG_ENV_OFFSET;
37static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
Patrick Delaunay25d90ad2021-02-09 11:48:52 +010038
39#else
40
41#define ENV_OFFSET_REDUND OFFSET_INVALID
42
Stefano Babica3110f02010-10-27 11:06:20 +020043#endif /* CONFIG_ENV_OFFSET_REDUND */
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020044
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020045DECLARE_GLOBAL_DATA_PTR;
46
Patrick Delaunayecf15442021-02-24 11:52:36 +010047static int setup_flash_device(struct spi_flash **env_flash)
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020048{
Lukasz Majewski56c40462020-06-04 23:11:53 +080049#if CONFIG_IS_ENABLED(DM_SPI_FLASH)
Gong Qianyu19c31282016-01-26 15:06:42 +080050 struct udevice *new;
Andreas Fenkartafa81a72017-04-08 11:59:31 +020051 int ret;
Gong Qianyu19c31282016-01-26 15:06:42 +080052
Vignesh R96907c02016-07-06 10:04:28 +053053 /* speed and mode will be read from DT */
Gong Qianyu19c31282016-01-26 15:06:42 +080054 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
Konstantin Porotchkin25a17652018-08-29 16:34:52 +030055 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE,
56 &new);
Gong Qianyu19c31282016-01-26 15:06:42 +080057 if (ret) {
Simon Glass0ac7d722019-08-01 09:47:00 -060058 env_set_default("spi_flash_probe_bus_cs() failed", 0);
Simon Glassc5951992017-08-03 12:22:17 -060059 return ret;
Gong Qianyu19c31282016-01-26 15:06:42 +080060 }
61
Patrick Delaunayecf15442021-02-24 11:52:36 +010062 *env_flash = dev_get_uclass_priv(new);
Gong Qianyu19c31282016-01-26 15:06:42 +080063#else
Patrick Delaunayecf15442021-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 Tekic8cbf172020-05-13 16:32:42 +053067 env_set_default("spi_flash_probe() failed", 0);
68 return -EIO;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020069 }
Gong Qianyu19c31282016-01-26 15:06:42 +080070#endif
Andreas Fenkartafa81a72017-04-08 11:59:31 +020071 return 0;
72}
73
74#if defined(CONFIG_ENV_OFFSET_REDUND)
Simon Glasse5bce242017-08-03 12:22:01 -060075static int env_sf_save(void)
Andreas Fenkartafa81a72017-04-08 11:59:31 +020076{
77 env_t env_new;
Simon Glassd3716dd2019-08-01 09:47:08 -060078 char *saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
Rasmus Villemoesbcb44f62021-04-14 20:51:43 +020079 u32 saved_size = 0, saved_offset = 0, sector;
Rasmus Villemoes4da73472021-04-14 20:51:42 +020080 u32 sect_size = CONFIG_ENV_SECT_SIZE;
Andreas Fenkartafa81a72017-04-08 11:59:31 +020081 int ret;
Patrick Delaunayecf15442021-02-24 11:52:36 +010082 struct spi_flash *env_flash;
Andreas Fenkartafa81a72017-04-08 11:59:31 +020083
Patrick Delaunayecf15442021-02-24 11:52:36 +010084 ret = setup_flash_device(&env_flash);
Andreas Fenkartafa81a72017-04-08 11:59:31 +020085 if (ret)
86 return ret;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020087
Rasmus Villemoesbcb44f62021-04-14 20:51:43 +020088 if (IS_ENABLED(CONFIG_ENV_SECT_SIZE_AUTO))
89 sect_size = env_flash->mtd.erasesize;
90
Marek Vasut7ce15262014-03-05 19:59:50 +010091 ret = env_export(&env_new);
92 if (ret)
Simon Glassc5951992017-08-03 12:22:17 -060093 return -EIO;
Simon Glassd3716dd2019-08-01 09:47:08 -060094 env_new.flags = ENV_REDUND_ACTIVE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020095
Simon Glass203e94f2017-08-03 12:21:56 -060096 if (gd->env_valid == ENV_VALID) {
Stefano Babica3110f02010-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 Wegner7319bca2010-04-23 17:22:55 +0200104 /* Is the sector larger than the env (i.e. embedded) */
Rasmus Villemoes4da73472021-04-14 20:51:42 +0200105 if (sect_size > CONFIG_ENV_SIZE) {
106 saved_size = sect_size - CONFIG_ENV_SIZE;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200107 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
Ravi Babu7dd01742015-08-17 13:29:49 +0530108 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200109 if (!saved_buffer) {
Simon Glassc5951992017-08-03 12:22:17 -0600110 ret = -ENOMEM;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200111 goto done;
112 }
Heiko Schocher9ba5e5b2019-03-13 12:15:45 +0100113 ret = spi_flash_read(env_flash, saved_offset,
114 saved_size, saved_buffer);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200115 if (ret)
116 goto done;
117 }
118
Rasmus Villemoes4da73472021-04-14 20:51:42 +0200119 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200120
121 puts("Erasing SPI flash...");
122 ret = spi_flash_erase(env_flash, env_new_offset,
Rasmus Villemoes4da73472021-04-14 20:51:42 +0200123 sector * sect_size);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200124 if (ret)
125 goto done;
126
127 puts("Writing to SPI flash...");
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200128
Stefano Babica3110f02010-10-27 11:06:20 +0200129 ret = spi_flash_write(env_flash, env_new_offset,
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400130 CONFIG_ENV_SIZE, &env_new);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200131 if (ret)
132 goto done;
133
Rasmus Villemoes4da73472021-04-14 20:51:42 +0200134 if (sect_size > CONFIG_ENV_SIZE) {
Wolfgang Wegner7319bca2010-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 Grinbergeb58a7f2011-11-07 01:14:08 +0000141 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400142 sizeof(env_new.flags), &flag);
Stefano Babica3110f02010-10-27 11:06:20 +0200143 if (ret)
144 goto done;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200145
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200146 puts("done\n");
147
Simon Glass203e94f2017-08-03 12:21:56 -0600148 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
Stefano Babica3110f02010-10-27 11:06:20 +0200149
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000150 printf("Valid environment: %d\n", (int)gd->env_valid);
Stefano Babica3110f02010-10-27 11:06:20 +0200151
Patrick Delaunayea5641e2021-01-04 14:41:31 +0100152done:
Patrick Delaunayc2d00362021-02-24 11:52:35 +0100153 spi_flash_free(env_flash);
Patrick Delaunayc2d00362021-02-24 11:52:35 +0100154
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200155 if (saved_buffer)
156 free(saved_buffer);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000157
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200158 return ret;
159}
160
Simon Glassc5951992017-08-03 12:22:17 -0600161static int env_sf_load(void)
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200162{
163 int ret;
Simon Goldschmidt80719932018-01-31 14:47:13 +0100164 int read1_fail, read2_fail;
165 env_t *tmp_env1, *tmp_env2;
Patrick Delaunayecf15442021-02-24 11:52:36 +0100166 struct spi_flash *env_flash;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200167
Ravi Babu7dd01742015-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 Denkea882ba2010-06-20 23:33:59 +0200172 if (!tmp_env1 || !tmp_env2) {
Simon Glass0ac7d722019-08-01 09:47:00 -0600173 env_set_default("malloc() failed", 0);
Simon Glassc5951992017-08-03 12:22:17 -0600174 ret = -EIO;
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000175 goto out;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200176 }
177
Patrick Delaunayecf15442021-02-24 11:52:36 +0100178 ret = setup_flash_device(&env_flash);
Andreas Fenkart8fee8842017-04-08 11:59:32 +0200179 if (ret)
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000180 goto out;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200181
Heiko Schocher9ba5e5b2019-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 Wegner7319bca2010-04-23 17:22:55 +0200186
Simon Goldschmidt80719932018-01-31 14:47:13 +0100187 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
Marek Vasut890feec2020-07-07 20:51:35 +0200188 read2_fail, H_EXTERNAL);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200189
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200190 spi_flash_free(env_flash);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200191out:
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200192 free(tmp_env1);
193 free(tmp_env2);
Simon Glassc5951992017-08-03 12:22:17 -0600194
195 return ret;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200196}
197#else
Simon Glasse5bce242017-08-03 12:22:01 -0600198static int env_sf_save(void)
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200199{
Rasmus Villemoesbcb44f62021-04-14 20:51:43 +0200200 u32 saved_size = 0, saved_offset = 0, sector;
Rasmus Villemoes4da73472021-04-14 20:51:42 +0200201 u32 sect_size = CONFIG_ENV_SECT_SIZE;
Marek Vasut7ce15262014-03-05 19:59:50 +0100202 char *saved_buffer = NULL;
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000203 int ret = 1;
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400204 env_t env_new;
Patrick Delaunayecf15442021-02-24 11:52:36 +0100205 struct spi_flash *env_flash;
Gong Qianyu19c31282016-01-26 15:06:42 +0800206
Patrick Delaunayecf15442021-02-24 11:52:36 +0100207 ret = setup_flash_device(&env_flash);
Andreas Fenkartafa81a72017-04-08 11:59:31 +0200208 if (ret)
209 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200210
Rasmus Villemoesbcb44f62021-04-14 20:51:43 +0200211 if (IS_ENABLED(CONFIG_ENV_SECT_SIZE_AUTO))
212 sect_size = env_flash->mtd.erasesize;
213
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500214 /* Is the sector larger than the env (i.e. embedded) */
Rasmus Villemoes4da73472021-04-14 20:51:42 +0200215 if (sect_size > CONFIG_ENV_SIZE) {
216 saved_size = sect_size - CONFIG_ENV_SIZE;
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500217 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
218 saved_buffer = malloc(saved_size);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000219 if (!saved_buffer)
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500220 goto done;
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000221
Heiko Schocher9ba5e5b2019-03-13 12:15:45 +0100222 ret = spi_flash_read(env_flash, saved_offset,
223 saved_size, saved_buffer);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500224 if (ret)
225 goto done;
226 }
227
Marek Vasut7ce15262014-03-05 19:59:50 +0100228 ret = env_export(&env_new);
229 if (ret)
Stefano Babica3110f02010-10-27 11:06:20 +0200230 goto done;
Stefano Babica3110f02010-10-27 11:06:20 +0200231
Rasmus Villemoes4da73472021-04-14 20:51:42 +0200232 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size);
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +0200233
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200234 puts("Erasing SPI flash...");
Stefano Babica3110f02010-10-27 11:06:20 +0200235 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
Rasmus Villemoes4da73472021-04-14 20:51:42 +0200236 sector * sect_size);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500237 if (ret)
238 goto done;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200239
240 puts("Writing to SPI flash...");
Stefano Babica3110f02010-10-27 11:06:20 +0200241 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400242 CONFIG_ENV_SIZE, &env_new);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500243 if (ret)
244 goto done;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200245
Rasmus Villemoes4da73472021-04-14 20:51:42 +0200246 if (sect_size > CONFIG_ENV_SIZE) {
Stefano Babica3110f02010-10-27 11:06:20 +0200247 ret = spi_flash_write(env_flash, saved_offset,
248 saved_size, saved_buffer);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500249 if (ret)
250 goto done;
251 }
252
253 ret = 0;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200254 puts("done\n");
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500255
Patrick Delaunayea5641e2021-01-04 14:41:31 +0100256done:
Patrick Delaunayc2d00362021-02-24 11:52:35 +0100257 spi_flash_free(env_flash);
Patrick Delaunayc2d00362021-02-24 11:52:35 +0100258
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500259 if (saved_buffer)
260 free(saved_buffer);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000261
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500262 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200263}
264
Simon Glassc5951992017-08-03 12:22:17 -0600265static int env_sf_load(void)
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200266{
267 int ret;
Ying Zhang5a89fa92014-01-24 15:50:07 +0800268 char *buf = NULL;
Patrick Delaunayecf15442021-02-24 11:52:36 +0100269 struct spi_flash *env_flash;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200270
Ravi Babu7dd01742015-08-17 13:29:49 +0530271 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
Andreas Fenkartc041c602017-04-08 11:59:33 +0200272 if (!buf) {
Simon Glass0ac7d722019-08-01 09:47:00 -0600273 env_set_default("malloc() failed", 0);
Simon Glassc5951992017-08-03 12:22:17 -0600274 return -EIO;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200275 }
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200276
Patrick Delaunayecf15442021-02-24 11:52:36 +0100277 ret = setup_flash_device(&env_flash);
Andreas Fenkartc041c602017-04-08 11:59:33 +0200278 if (ret)
279 goto out;
280
Heiko Schocher9ba5e5b2019-03-13 12:15:45 +0100281 ret = spi_flash_read(env_flash,
282 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200283 if (ret) {
Simon Glass0ac7d722019-08-01 09:47:00 -0600284 env_set_default("spi_flash_read() failed", 0);
Andreas Fenkartc041c602017-04-08 11:59:33 +0200285 goto err_read;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200286 }
287
Marek Vasut890feec2020-07-07 20:51:35 +0200288 ret = env_import(buf, 1, H_EXTERNAL);
Simon Goldschmidt42a18202018-01-31 14:47:10 +0100289 if (!ret)
Simon Glass203e94f2017-08-03 12:21:56 -0600290 gd->env_valid = ENV_VALID;
Andreas Fenkartc041c602017-04-08 11:59:33 +0200291
292err_read:
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200293 spi_flash_free(env_flash);
Andreas Fenkartc041c602017-04-08 11:59:33 +0200294out:
295 free(buf);
Simon Glassc5951992017-08-03 12:22:17 -0600296
297 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200298}
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200299#endif
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200300
Patrick Delaunay25d90ad2021-02-09 11:48:52 +0100301static int env_sf_erase(void)
302{
303 int ret;
304 env_t env;
Patrick Delaunayecf15442021-02-24 11:52:36 +0100305 struct spi_flash *env_flash;
Patrick Delaunay25d90ad2021-02-09 11:48:52 +0100306
Patrick Delaunayecf15442021-02-24 11:52:36 +0100307 ret = setup_flash_device(&env_flash);
Patrick Delaunay25d90ad2021-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 Delaunay25d90ad2021-02-09 11:48:52 +0100321
322 return ret;
323}
324
Tom Rinia09fea12019-11-18 20:02:10 -0500325#if CONFIG_ENV_ADDR != 0x0
Rajesh Bhagat119c01c22018-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 Rinia09fea12019-11-18 20:02:10 -0500332#if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0)
Heiko Schocher92765f452020-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 Kumarb500c922017-12-14 17:37:08 +0530337{
Rajesh Bhagat119c01c22018-11-05 18:01:15 +0000338 env_t *env_ptr = (env_t *)env_sf_get_env_addr();
Ashish Kumarb500c922017-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 Schocher92765f452020-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 Delaunayecf15442021-02-24 11:52:36 +0100365 struct spi_flash *env_flash;
Heiko Schocher92765f452020-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 Delaunayecf15442021-02-24 11:52:36 +0100383 ret = setup_flash_device(&env_flash);
Heiko Schocher92765f452020-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 Maierb9c30522020-12-17 17:19:18 -0600397 if (ret < 0)
Heiko Schocher92765f452020-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 Delaunayc2d00362021-02-24 11:52:35 +0100418 spi_flash_free(env_flash);
Patrick Delaunayc2d00362021-02-24 11:52:35 +0100419
Heiko Schocher92765f452020-10-10 10:28:05 +0200420 return 0;
421err_read:
422 spi_flash_free(env_flash);
Patrick Delaunayecf15442021-02-24 11:52:36 +0100423
Heiko Schocher92765f452020-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 Schocher46ce9e72020-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 Schocher92765f452020-10-10 10:28:05 +0200446 */
Heiko Schocher46ce9e72020-11-03 15:22:36 +0100447 return -ENOENT;
Heiko Schocher92765f452020-10-10 10:28:05 +0200448}
449
Simon Glass4415f1d2017-08-03 12:21:58 -0600450U_BOOT_ENV_LOCATION(sf) = {
451 .location = ENVL_SPI_FLASH,
Patrick Delaunay22140d12020-07-28 11:51:16 +0200452 ENV_NAME("SPIFlash")
Simon Glasse5bce242017-08-03 12:22:01 -0600453 .load = env_sf_load,
Patrick Delaunaye41f55b2021-02-09 11:48:51 +0100454 .save = ENV_SAVE_PTR(env_sf_save),
Patrick Delaunay25d90ad2021-02-09 11:48:52 +0100455 .erase = ENV_ERASE_PTR(env_sf_erase),
Ashish Kumarb500c922017-12-14 17:37:08 +0530456 .init = env_sf_init,
Simon Glass4415f1d2017-08-03 12:21:58 -0600457};