blob: 22b70ad3197fade01b4365623c175d3f9e592dff [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>
Mike Frysinger9af10d52008-12-11 06:23:37 -050015#include <malloc.h>
Simon Glassdef92152014-10-13 23:41:55 -060016#include <spi.h>
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020017#include <spi_flash.h>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020018#include <search.h>
19#include <errno.h>
Gong Qianyu6d85a602016-01-26 15:06:42 +080020#include <dm/device-internal.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070021#include <u-boot/crc.h>
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020022
Simon Glassc10a88e2017-08-03 12:21:58 -060023#ifndef CONFIG_SPL_BUILD
Ashish Kumara7bf68a2017-12-14 17:37:08 +053024#define INITENV
Simon Glassc10a88e2017-08-03 12:21:58 -060025#endif
26
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020027#ifdef CONFIG_ENV_OFFSET_REDUND
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000028static ulong env_offset = CONFIG_ENV_OFFSET;
29static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
Stefano Babic5f6c2862010-10-27 11:06:20 +020030#endif /* CONFIG_ENV_OFFSET_REDUND */
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020031
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020032DECLARE_GLOBAL_DATA_PTR;
33
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020034static struct spi_flash *env_flash;
35
Andreas Fenkart52b94442017-04-08 11:59:31 +020036static int setup_flash_device(void)
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020037{
Gong Qianyu6d85a602016-01-26 15:06:42 +080038#ifdef CONFIG_DM_SPI_FLASH
39 struct udevice *new;
Andreas Fenkart52b94442017-04-08 11:59:31 +020040 int ret;
Gong Qianyu6d85a602016-01-26 15:06:42 +080041
Vignesh Rae569792016-07-06 10:04:28 +053042 /* speed and mode will be read from DT */
Gong Qianyu6d85a602016-01-26 15:06:42 +080043 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
Konstantin Porotchkin0be67b22018-08-29 16:34:52 +030044 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE,
45 &new);
Gong Qianyu6d85a602016-01-26 15:06:42 +080046 if (ret) {
Simon Glass97385862019-08-01 09:47:00 -060047 env_set_default("spi_flash_probe_bus_cs() failed", 0);
Simon Glass99778492017-08-03 12:22:17 -060048 return ret;
Gong Qianyu6d85a602016-01-26 15:06:42 +080049 }
50
51 env_flash = dev_get_uclass_priv(new);
52#else
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020053
54 if (!env_flash) {
Stefano Babic5f6c2862010-10-27 11:06:20 +020055 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
56 CONFIG_ENV_SPI_CS,
57 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
58 if (!env_flash) {
Simon Glass97385862019-08-01 09:47:00 -060059 env_set_default("spi_flash_probe() failed", 0);
Simon Glass99778492017-08-03 12:22:17 -060060 return -EIO;
Stefano Babic5f6c2862010-10-27 11:06:20 +020061 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020062 }
Gong Qianyu6d85a602016-01-26 15:06:42 +080063#endif
Andreas Fenkart52b94442017-04-08 11:59:31 +020064 return 0;
65}
66
67#if defined(CONFIG_ENV_OFFSET_REDUND)
Simon Glass082af922017-08-03 12:22:01 -060068static int env_sf_save(void)
Andreas Fenkart52b94442017-04-08 11:59:31 +020069{
70 env_t env_new;
Simon Glass4686d8f2019-08-01 09:47:08 -060071 char *saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
Andreas Fenkartdf701912017-04-08 11:59:34 +020072 u32 saved_size, saved_offset, sector;
Andreas Fenkart52b94442017-04-08 11:59:31 +020073 int ret;
74
75 ret = setup_flash_device();
76 if (ret)
77 return ret;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020078
Marek Vasutd73c1292014-03-05 19:59:50 +010079 ret = env_export(&env_new);
80 if (ret)
Simon Glass99778492017-08-03 12:22:17 -060081 return -EIO;
Simon Glass4686d8f2019-08-01 09:47:08 -060082 env_new.flags = ENV_REDUND_ACTIVE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020083
Simon Glass4bc2ad22017-08-03 12:21:56 -060084 if (gd->env_valid == ENV_VALID) {
Stefano Babic5f6c2862010-10-27 11:06:20 +020085 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
86 env_offset = CONFIG_ENV_OFFSET;
87 } else {
88 env_new_offset = CONFIG_ENV_OFFSET;
89 env_offset = CONFIG_ENV_OFFSET_REDUND;
90 }
91
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020092 /* Is the sector larger than the env (i.e. embedded) */
93 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
94 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
95 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
Ravi Babu20d93fc2015-08-17 13:29:49 +053096 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020097 if (!saved_buffer) {
Simon Glass99778492017-08-03 12:22:17 -060098 ret = -ENOMEM;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020099 goto done;
100 }
Heiko Schocherf45775c2019-03-13 12:15:45 +0100101 ret = spi_flash_read(env_flash, saved_offset,
102 saved_size, saved_buffer);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200103 if (ret)
104 goto done;
105 }
106
Andreas Fenkartdf701912017-04-08 11:59:34 +0200107 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200108
109 puts("Erasing SPI flash...");
110 ret = spi_flash_erase(env_flash, env_new_offset,
111 sector * CONFIG_ENV_SECT_SIZE);
112 if (ret)
113 goto done;
114
115 puts("Writing to SPI flash...");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200116
Stefano Babic5f6c2862010-10-27 11:06:20 +0200117 ret = spi_flash_write(env_flash, env_new_offset,
Tom Rinia840b8e2013-04-05 14:55:21 -0400118 CONFIG_ENV_SIZE, &env_new);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200119 if (ret)
120 goto done;
121
122 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
123 ret = spi_flash_write(env_flash, saved_offset,
124 saved_size, saved_buffer);
125 if (ret)
126 goto done;
127 }
128
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000129 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
Tom Rinia840b8e2013-04-05 14:55:21 -0400130 sizeof(env_new.flags), &flag);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200131 if (ret)
132 goto done;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200133
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200134 puts("done\n");
135
Simon Glass4bc2ad22017-08-03 12:21:56 -0600136 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200137
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000138 printf("Valid environment: %d\n", (int)gd->env_valid);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200139
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200140 done:
141 if (saved_buffer)
142 free(saved_buffer);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000143
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200144 return ret;
145}
146
Simon Glass99778492017-08-03 12:22:17 -0600147static int env_sf_load(void)
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200148{
149 int ret;
Simon Goldschmidt8f9ce3a2018-01-31 14:47:13 +0100150 int read1_fail, read2_fail;
151 env_t *tmp_env1, *tmp_env2;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200152
Ravi Babu20d93fc2015-08-17 13:29:49 +0530153 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
154 CONFIG_ENV_SIZE);
155 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
156 CONFIG_ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200157 if (!tmp_env1 || !tmp_env2) {
Simon Glass97385862019-08-01 09:47:00 -0600158 env_set_default("malloc() failed", 0);
Simon Glass99778492017-08-03 12:22:17 -0600159 ret = -EIO;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000160 goto out;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200161 }
162
Andreas Fenkarta2d18832017-04-08 11:59:32 +0200163 ret = setup_flash_device();
164 if (ret)
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000165 goto out;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200166
Heiko Schocherf45775c2019-03-13 12:15:45 +0100167 read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
168 CONFIG_ENV_SIZE, tmp_env1);
169 read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
170 CONFIG_ENV_SIZE, tmp_env2);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200171
Simon Goldschmidt8f9ce3a2018-01-31 14:47:13 +0100172 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
173 read2_fail);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200174
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200175 spi_flash_free(env_flash);
176 env_flash = NULL;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200177out:
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200178 free(tmp_env1);
179 free(tmp_env2);
Simon Glass99778492017-08-03 12:22:17 -0600180
181 return ret;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200182}
183#else
Simon Glass082af922017-08-03 12:22:01 -0600184static int env_sf_save(void)
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200185{
Andreas Fenkartdf701912017-04-08 11:59:34 +0200186 u32 saved_size, saved_offset, sector;
Marek Vasutd73c1292014-03-05 19:59:50 +0100187 char *saved_buffer = NULL;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000188 int ret = 1;
Tom Rinia840b8e2013-04-05 14:55:21 -0400189 env_t env_new;
Gong Qianyu6d85a602016-01-26 15:06:42 +0800190
Andreas Fenkart52b94442017-04-08 11:59:31 +0200191 ret = setup_flash_device();
192 if (ret)
193 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200194
Mike Frysinger9af10d52008-12-11 06:23:37 -0500195 /* Is the sector larger than the env (i.e. embedded) */
196 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
197 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
198 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
199 saved_buffer = malloc(saved_size);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000200 if (!saved_buffer)
Mike Frysinger9af10d52008-12-11 06:23:37 -0500201 goto done;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000202
Heiko Schocherf45775c2019-03-13 12:15:45 +0100203 ret = spi_flash_read(env_flash, saved_offset,
204 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500205 if (ret)
206 goto done;
207 }
208
Marek Vasutd73c1292014-03-05 19:59:50 +0100209 ret = env_export(&env_new);
210 if (ret)
Stefano Babic5f6c2862010-10-27 11:06:20 +0200211 goto done;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200212
Andreas Fenkartdf701912017-04-08 11:59:34 +0200213 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
214
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200215 puts("Erasing SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200216 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
217 sector * CONFIG_ENV_SECT_SIZE);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500218 if (ret)
219 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200220
221 puts("Writing to SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200222 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
Tom Rinia840b8e2013-04-05 14:55:21 -0400223 CONFIG_ENV_SIZE, &env_new);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500224 if (ret)
225 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200226
Mike Frysinger9af10d52008-12-11 06:23:37 -0500227 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200228 ret = spi_flash_write(env_flash, saved_offset,
229 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500230 if (ret)
231 goto done;
232 }
233
234 ret = 0;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200235 puts("done\n");
Mike Frysinger9af10d52008-12-11 06:23:37 -0500236
237 done:
238 if (saved_buffer)
239 free(saved_buffer);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000240
Mike Frysinger9af10d52008-12-11 06:23:37 -0500241 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200242}
243
Simon Glass99778492017-08-03 12:22:17 -0600244static int env_sf_load(void)
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200245{
246 int ret;
Ying Zhang354846f2014-01-24 15:50:07 +0800247 char *buf = NULL;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200248
Ravi Babu20d93fc2015-08-17 13:29:49 +0530249 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200250 if (!buf) {
Simon Glass97385862019-08-01 09:47:00 -0600251 env_set_default("malloc() failed", 0);
Simon Glass99778492017-08-03 12:22:17 -0600252 return -EIO;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200253 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200254
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200255 ret = setup_flash_device();
256 if (ret)
257 goto out;
258
Heiko Schocherf45775c2019-03-13 12:15:45 +0100259 ret = spi_flash_read(env_flash,
260 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200261 if (ret) {
Simon Glass97385862019-08-01 09:47:00 -0600262 env_set_default("spi_flash_read() failed", 0);
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200263 goto err_read;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200264 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200265
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200266 ret = env_import(buf, 1);
Simon Goldschmidt32c3eb52018-01-31 14:47:10 +0100267 if (!ret)
Simon Glass4bc2ad22017-08-03 12:21:56 -0600268 gd->env_valid = ENV_VALID;
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200269
270err_read:
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200271 spi_flash_free(env_flash);
272 env_flash = NULL;
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200273out:
274 free(buf);
Simon Glass99778492017-08-03 12:22:17 -0600275
276 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200277}
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200278#endif
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200279
Tom Rini5cd7ece2019-11-18 20:02:10 -0500280#if CONFIG_ENV_ADDR != 0x0
Rajesh Bhagat9a90cc92018-11-05 18:01:15 +0000281__weak void *env_sf_get_env_addr(void)
282{
283 return (void *)CONFIG_ENV_ADDR;
284}
285#endif
286
Tom Rini5cd7ece2019-11-18 20:02:10 -0500287#if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0)
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530288static int env_sf_init(void)
289{
Rajesh Bhagat9a90cc92018-11-05 18:01:15 +0000290 env_t *env_ptr = (env_t *)env_sf_get_env_addr();
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530291
292 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
293 gd->env_addr = (ulong)&(env_ptr->data);
294 gd->env_valid = 1;
295 } else {
296 gd->env_addr = (ulong)&default_environment[0];
297 gd->env_valid = 1;
298 }
299
300 return 0;
301}
302#endif
303
Simon Glassc10a88e2017-08-03 12:21:58 -0600304U_BOOT_ENV_LOCATION(sf) = {
305 .location = ENVL_SPI_FLASH,
Simon Glassd8273ed2017-08-03 12:22:03 -0600306 ENV_NAME("SPI Flash")
Simon Glass082af922017-08-03 12:22:01 -0600307 .load = env_sf_load,
Rasmus Villemoes4886ebc2020-02-19 09:47:43 +0000308 .save = ENV_SAVE_PTR(env_sf_save),
Tom Rini5cd7ece2019-11-18 20:02:10 -0500309#if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0)
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530310 .init = env_sf_init,
311#endif
Simon Glassc10a88e2017-08-03 12:21:58 -0600312};