blob: 02ed846fc73d0ce892641aedaf708c530f3bd842 [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>
Gong Qianyu6d85a602016-01-26 15:06:42 +080023#include <dm/device-internal.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070024#include <u-boot/crc.h>
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020025
Simon Glassc10a88e2017-08-03 12:21:58 -060026#ifndef CONFIG_SPL_BUILD
Ashish Kumara7bf68a2017-12-14 17:37:08 +053027#define INITENV
Simon Glassc10a88e2017-08-03 12:21:58 -060028#endif
29
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020030#ifdef CONFIG_ENV_OFFSET_REDUND
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000031static ulong env_offset = CONFIG_ENV_OFFSET;
32static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
Stefano Babic5f6c2862010-10-27 11:06:20 +020033#endif /* CONFIG_ENV_OFFSET_REDUND */
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020034
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020035DECLARE_GLOBAL_DATA_PTR;
36
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020037static struct spi_flash *env_flash;
38
Andreas Fenkart52b94442017-04-08 11:59:31 +020039static int setup_flash_device(void)
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020040{
Gong Qianyu6d85a602016-01-26 15:06:42 +080041#ifdef CONFIG_DM_SPI_FLASH
42 struct udevice *new;
Andreas Fenkart52b94442017-04-08 11:59:31 +020043 int ret;
Gong Qianyu6d85a602016-01-26 15:06:42 +080044
Vignesh Rae569792016-07-06 10:04:28 +053045 /* speed and mode will be read from DT */
Gong Qianyu6d85a602016-01-26 15:06:42 +080046 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
Konstantin Porotchkin0be67b22018-08-29 16:34:52 +030047 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE,
48 &new);
Gong Qianyu6d85a602016-01-26 15:06:42 +080049 if (ret) {
Simon Glass97385862019-08-01 09:47:00 -060050 env_set_default("spi_flash_probe_bus_cs() failed", 0);
Simon Glass99778492017-08-03 12:22:17 -060051 return ret;
Gong Qianyu6d85a602016-01-26 15:06:42 +080052 }
53
54 env_flash = dev_get_uclass_priv(new);
55#else
Jagan Teki90173092020-05-13 16:32:42 +053056 if (env_flash)
57 spi_flash_free(env_flash);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020058
Jagan Teki90173092020-05-13 16:32:42 +053059 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
60 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020061 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;
Andreas Fenkartdf701912017-04-08 11:59:34 +020074 u32 saved_size, saved_offset, sector;
Andreas Fenkart52b94442017-04-08 11:59:31 +020075 int ret;
76
77 ret = setup_flash_device();
78 if (ret)
79 return ret;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020080
Marek Vasutd73c1292014-03-05 19:59:50 +010081 ret = env_export(&env_new);
82 if (ret)
Simon Glass99778492017-08-03 12:22:17 -060083 return -EIO;
Simon Glass4686d8f2019-08-01 09:47:08 -060084 env_new.flags = ENV_REDUND_ACTIVE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020085
Simon Glass4bc2ad22017-08-03 12:21:56 -060086 if (gd->env_valid == ENV_VALID) {
Stefano Babic5f6c2862010-10-27 11:06:20 +020087 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
88 env_offset = CONFIG_ENV_OFFSET;
89 } else {
90 env_new_offset = CONFIG_ENV_OFFSET;
91 env_offset = CONFIG_ENV_OFFSET_REDUND;
92 }
93
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020094 /* Is the sector larger than the env (i.e. embedded) */
95 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
96 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
97 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
Ravi Babu20d93fc2015-08-17 13:29:49 +053098 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020099 if (!saved_buffer) {
Simon Glass99778492017-08-03 12:22:17 -0600100 ret = -ENOMEM;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200101 goto done;
102 }
Heiko Schocherf45775c2019-03-13 12:15:45 +0100103 ret = spi_flash_read(env_flash, saved_offset,
104 saved_size, saved_buffer);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200105 if (ret)
106 goto done;
107 }
108
Andreas Fenkartdf701912017-04-08 11:59:34 +0200109 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200110
111 puts("Erasing SPI flash...");
112 ret = spi_flash_erase(env_flash, env_new_offset,
113 sector * CONFIG_ENV_SECT_SIZE);
114 if (ret)
115 goto done;
116
117 puts("Writing to SPI flash...");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200118
Stefano Babic5f6c2862010-10-27 11:06:20 +0200119 ret = spi_flash_write(env_flash, env_new_offset,
Tom Rinia840b8e2013-04-05 14:55:21 -0400120 CONFIG_ENV_SIZE, &env_new);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200121 if (ret)
122 goto done;
123
124 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
125 ret = spi_flash_write(env_flash, saved_offset,
126 saved_size, saved_buffer);
127 if (ret)
128 goto done;
129 }
130
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000131 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
Tom Rinia840b8e2013-04-05 14:55:21 -0400132 sizeof(env_new.flags), &flag);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200133 if (ret)
134 goto done;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200135
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200136 puts("done\n");
137
Simon Glass4bc2ad22017-08-03 12:21:56 -0600138 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200139
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000140 printf("Valid environment: %d\n", (int)gd->env_valid);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200141
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200142 done:
143 if (saved_buffer)
144 free(saved_buffer);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000145
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200146 return ret;
147}
148
Simon Glass99778492017-08-03 12:22:17 -0600149static int env_sf_load(void)
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200150{
151 int ret;
Simon Goldschmidt8f9ce3a2018-01-31 14:47:13 +0100152 int read1_fail, read2_fail;
153 env_t *tmp_env1, *tmp_env2;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200154
Ravi Babu20d93fc2015-08-17 13:29:49 +0530155 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
156 CONFIG_ENV_SIZE);
157 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
158 CONFIG_ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200159 if (!tmp_env1 || !tmp_env2) {
Simon Glass97385862019-08-01 09:47:00 -0600160 env_set_default("malloc() failed", 0);
Simon Glass99778492017-08-03 12:22:17 -0600161 ret = -EIO;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000162 goto out;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200163 }
164
Andreas Fenkarta2d18832017-04-08 11:59:32 +0200165 ret = setup_flash_device();
166 if (ret)
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000167 goto out;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200168
Heiko Schocherf45775c2019-03-13 12:15:45 +0100169 read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
170 CONFIG_ENV_SIZE, tmp_env1);
171 read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
172 CONFIG_ENV_SIZE, tmp_env2);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200173
Simon Goldschmidt8f9ce3a2018-01-31 14:47:13 +0100174 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
175 read2_fail);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200176
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200177 spi_flash_free(env_flash);
178 env_flash = NULL;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200179out:
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200180 free(tmp_env1);
181 free(tmp_env2);
Simon Glass99778492017-08-03 12:22:17 -0600182
183 return ret;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200184}
185#else
Simon Glass082af922017-08-03 12:22:01 -0600186static int env_sf_save(void)
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200187{
Andreas Fenkartdf701912017-04-08 11:59:34 +0200188 u32 saved_size, saved_offset, sector;
Marek Vasutd73c1292014-03-05 19:59:50 +0100189 char *saved_buffer = NULL;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000190 int ret = 1;
Tom Rinia840b8e2013-04-05 14:55:21 -0400191 env_t env_new;
Gong Qianyu6d85a602016-01-26 15:06:42 +0800192
Andreas Fenkart52b94442017-04-08 11:59:31 +0200193 ret = setup_flash_device();
194 if (ret)
195 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200196
Mike Frysinger9af10d52008-12-11 06:23:37 -0500197 /* Is the sector larger than the env (i.e. embedded) */
198 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
199 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
200 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
201 saved_buffer = malloc(saved_size);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000202 if (!saved_buffer)
Mike Frysinger9af10d52008-12-11 06:23:37 -0500203 goto done;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000204
Heiko Schocherf45775c2019-03-13 12:15:45 +0100205 ret = spi_flash_read(env_flash, saved_offset,
206 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500207 if (ret)
208 goto done;
209 }
210
Marek Vasutd73c1292014-03-05 19:59:50 +0100211 ret = env_export(&env_new);
212 if (ret)
Stefano Babic5f6c2862010-10-27 11:06:20 +0200213 goto done;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200214
Andreas Fenkartdf701912017-04-08 11:59:34 +0200215 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
216
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200217 puts("Erasing SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200218 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
219 sector * CONFIG_ENV_SECT_SIZE);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500220 if (ret)
221 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200222
223 puts("Writing to SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200224 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
Tom Rinia840b8e2013-04-05 14:55:21 -0400225 CONFIG_ENV_SIZE, &env_new);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500226 if (ret)
227 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200228
Mike Frysinger9af10d52008-12-11 06:23:37 -0500229 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200230 ret = spi_flash_write(env_flash, saved_offset,
231 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500232 if (ret)
233 goto done;
234 }
235
236 ret = 0;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200237 puts("done\n");
Mike Frysinger9af10d52008-12-11 06:23:37 -0500238
239 done:
240 if (saved_buffer)
241 free(saved_buffer);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000242
Mike Frysinger9af10d52008-12-11 06:23:37 -0500243 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200244}
245
Simon Glass99778492017-08-03 12:22:17 -0600246static int env_sf_load(void)
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200247{
248 int ret;
Ying Zhang354846f2014-01-24 15:50:07 +0800249 char *buf = NULL;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200250
Ravi Babu20d93fc2015-08-17 13:29:49 +0530251 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200252 if (!buf) {
Simon Glass97385862019-08-01 09:47:00 -0600253 env_set_default("malloc() failed", 0);
Simon Glass99778492017-08-03 12:22:17 -0600254 return -EIO;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200255 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200256
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200257 ret = setup_flash_device();
258 if (ret)
259 goto out;
260
Heiko Schocherf45775c2019-03-13 12:15:45 +0100261 ret = spi_flash_read(env_flash,
262 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200263 if (ret) {
Simon Glass97385862019-08-01 09:47:00 -0600264 env_set_default("spi_flash_read() failed", 0);
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200265 goto err_read;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200266 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200267
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200268 ret = env_import(buf, 1);
Simon Goldschmidt32c3eb52018-01-31 14:47:10 +0100269 if (!ret)
Simon Glass4bc2ad22017-08-03 12:21:56 -0600270 gd->env_valid = ENV_VALID;
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200271
272err_read:
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200273 spi_flash_free(env_flash);
274 env_flash = NULL;
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200275out:
276 free(buf);
Simon Glass99778492017-08-03 12:22:17 -0600277
278 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200279}
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200280#endif
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200281
Tom Rini5cd7ece2019-11-18 20:02:10 -0500282#if CONFIG_ENV_ADDR != 0x0
Rajesh Bhagat9a90cc92018-11-05 18:01:15 +0000283__weak void *env_sf_get_env_addr(void)
284{
285 return (void *)CONFIG_ENV_ADDR;
286}
287#endif
288
Tom Rini5cd7ece2019-11-18 20:02:10 -0500289#if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0)
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530290static int env_sf_init(void)
291{
Rajesh Bhagat9a90cc92018-11-05 18:01:15 +0000292 env_t *env_ptr = (env_t *)env_sf_get_env_addr();
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530293
294 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
295 gd->env_addr = (ulong)&(env_ptr->data);
296 gd->env_valid = 1;
297 } else {
298 gd->env_addr = (ulong)&default_environment[0];
299 gd->env_valid = 1;
300 }
301
302 return 0;
303}
304#endif
305
Simon Glassc10a88e2017-08-03 12:21:58 -0600306U_BOOT_ENV_LOCATION(sf) = {
307 .location = ENVL_SPI_FLASH,
Simon Glassd8273ed2017-08-03 12:22:03 -0600308 ENV_NAME("SPI Flash")
Simon Glass082af922017-08-03 12:22:01 -0600309 .load = env_sf_load,
Rasmus Villemoesfcff09a2020-03-27 00:02:00 +0100310 .save = CONFIG_IS_ENABLED(SAVEENV) ? ENV_SAVE_PTR(env_sf_save) : NULL,
Tom Rini5cd7ece2019-11-18 20:02:10 -0500311#if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0)
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530312 .init = env_sf_init,
313#endif
Simon Glassc10a88e2017-08-03 12:21:58 -0600314};