blob: 5ef405521947a62f1d52bda91a05922742f2824b [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
24#define CMD_SAVEENV
Ashish Kumara7bf68a2017-12-14 17:37:08 +053025#define INITENV
Simon Glassc10a88e2017-08-03 12:21:58 -060026#endif
27
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020028#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glassc10a88e2017-08-03 12:21:58 -060029#ifdef CMD_SAVEENV
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000030static ulong env_offset = CONFIG_ENV_OFFSET;
31static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
Simon Glassc10a88e2017-08-03 12:21:58 -060032#endif
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020033
Stefano Babic5f6c2862010-10-27 11:06:20 +020034#endif /* CONFIG_ENV_OFFSET_REDUND */
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020035
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020036DECLARE_GLOBAL_DATA_PTR;
37
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020038static struct spi_flash *env_flash;
39
Andreas Fenkart52b94442017-04-08 11:59:31 +020040static int setup_flash_device(void)
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020041{
Gong Qianyu6d85a602016-01-26 15:06:42 +080042#ifdef CONFIG_DM_SPI_FLASH
43 struct udevice *new;
Andreas Fenkart52b94442017-04-08 11:59:31 +020044 int ret;
Gong Qianyu6d85a602016-01-26 15:06:42 +080045
Vignesh Rae569792016-07-06 10:04:28 +053046 /* speed and mode will be read from DT */
Gong Qianyu6d85a602016-01-26 15:06:42 +080047 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
Konstantin Porotchkin0be67b22018-08-29 16:34:52 +030048 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE,
49 &new);
Gong Qianyu6d85a602016-01-26 15:06:42 +080050 if (ret) {
Simon Glass97385862019-08-01 09:47:00 -060051 env_set_default("spi_flash_probe_bus_cs() failed", 0);
Simon Glass99778492017-08-03 12:22:17 -060052 return ret;
Gong Qianyu6d85a602016-01-26 15:06:42 +080053 }
54
55 env_flash = dev_get_uclass_priv(new);
56#else
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020057
58 if (!env_flash) {
Stefano Babic5f6c2862010-10-27 11:06:20 +020059 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
60 CONFIG_ENV_SPI_CS,
61 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
62 if (!env_flash) {
Simon Glass97385862019-08-01 09:47:00 -060063 env_set_default("spi_flash_probe() failed", 0);
Simon Glass99778492017-08-03 12:22:17 -060064 return -EIO;
Stefano Babic5f6c2862010-10-27 11:06:20 +020065 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020066 }
Gong Qianyu6d85a602016-01-26 15:06:42 +080067#endif
Andreas Fenkart52b94442017-04-08 11:59:31 +020068 return 0;
69}
70
71#if defined(CONFIG_ENV_OFFSET_REDUND)
Simon Glassc10a88e2017-08-03 12:21:58 -060072#ifdef CMD_SAVEENV
Simon Glass082af922017-08-03 12:22:01 -060073static int env_sf_save(void)
Andreas Fenkart52b94442017-04-08 11:59:31 +020074{
75 env_t env_new;
Simon Glass4686d8f2019-08-01 09:47:08 -060076 char *saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
Andreas Fenkartdf701912017-04-08 11:59:34 +020077 u32 saved_size, saved_offset, sector;
Andreas Fenkart52b94442017-04-08 11:59:31 +020078 int ret;
79
80 ret = setup_flash_device();
81 if (ret)
82 return ret;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020083
Marek Vasutd73c1292014-03-05 19:59:50 +010084 ret = env_export(&env_new);
85 if (ret)
Simon Glass99778492017-08-03 12:22:17 -060086 return -EIO;
Simon Glass4686d8f2019-08-01 09:47:08 -060087 env_new.flags = ENV_REDUND_ACTIVE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020088
Simon Glass4bc2ad22017-08-03 12:21:56 -060089 if (gd->env_valid == ENV_VALID) {
Stefano Babic5f6c2862010-10-27 11:06:20 +020090 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
91 env_offset = CONFIG_ENV_OFFSET;
92 } else {
93 env_new_offset = CONFIG_ENV_OFFSET;
94 env_offset = CONFIG_ENV_OFFSET_REDUND;
95 }
96
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020097 /* Is the sector larger than the env (i.e. embedded) */
98 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
99 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
100 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
Ravi Babu20d93fc2015-08-17 13:29:49 +0530101 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200102 if (!saved_buffer) {
Simon Glass99778492017-08-03 12:22:17 -0600103 ret = -ENOMEM;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200104 goto done;
105 }
Heiko Schocherf45775c2019-03-13 12:15:45 +0100106 ret = spi_flash_read(env_flash, saved_offset,
107 saved_size, saved_buffer);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200108 if (ret)
109 goto done;
110 }
111
Andreas Fenkartdf701912017-04-08 11:59:34 +0200112 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200113
114 puts("Erasing SPI flash...");
115 ret = spi_flash_erase(env_flash, env_new_offset,
116 sector * CONFIG_ENV_SECT_SIZE);
117 if (ret)
118 goto done;
119
120 puts("Writing to SPI flash...");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200121
Stefano Babic5f6c2862010-10-27 11:06:20 +0200122 ret = spi_flash_write(env_flash, env_new_offset,
Tom Rinia840b8e2013-04-05 14:55:21 -0400123 CONFIG_ENV_SIZE, &env_new);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200124 if (ret)
125 goto done;
126
127 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
128 ret = spi_flash_write(env_flash, saved_offset,
129 saved_size, saved_buffer);
130 if (ret)
131 goto done;
132 }
133
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000134 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
Tom Rinia840b8e2013-04-05 14:55:21 -0400135 sizeof(env_new.flags), &flag);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200136 if (ret)
137 goto done;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200138
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200139 puts("done\n");
140
Simon Glass4bc2ad22017-08-03 12:21:56 -0600141 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200142
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000143 printf("Valid environment: %d\n", (int)gd->env_valid);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200144
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200145 done:
146 if (saved_buffer)
147 free(saved_buffer);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000148
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200149 return ret;
150}
Simon Glassc10a88e2017-08-03 12:21:58 -0600151#endif /* CMD_SAVEENV */
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200152
Simon Glass99778492017-08-03 12:22:17 -0600153static int env_sf_load(void)
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200154{
155 int ret;
Simon Goldschmidt8f9ce3a2018-01-31 14:47:13 +0100156 int read1_fail, read2_fail;
157 env_t *tmp_env1, *tmp_env2;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200158
Ravi Babu20d93fc2015-08-17 13:29:49 +0530159 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
160 CONFIG_ENV_SIZE);
161 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
162 CONFIG_ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200163 if (!tmp_env1 || !tmp_env2) {
Simon Glass97385862019-08-01 09:47:00 -0600164 env_set_default("malloc() failed", 0);
Simon Glass99778492017-08-03 12:22:17 -0600165 ret = -EIO;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000166 goto out;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200167 }
168
Andreas Fenkarta2d18832017-04-08 11:59:32 +0200169 ret = setup_flash_device();
170 if (ret)
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000171 goto out;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200172
Heiko Schocherf45775c2019-03-13 12:15:45 +0100173 read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
174 CONFIG_ENV_SIZE, tmp_env1);
175 read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
176 CONFIG_ENV_SIZE, tmp_env2);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200177
Simon Goldschmidt8f9ce3a2018-01-31 14:47:13 +0100178 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
179 read2_fail);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200180
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200181 spi_flash_free(env_flash);
182 env_flash = NULL;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200183out:
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200184 free(tmp_env1);
185 free(tmp_env2);
Simon Glass99778492017-08-03 12:22:17 -0600186
187 return ret;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200188}
189#else
Simon Glassc10a88e2017-08-03 12:21:58 -0600190#ifdef CMD_SAVEENV
Simon Glass082af922017-08-03 12:22:01 -0600191static int env_sf_save(void)
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200192{
Andreas Fenkartdf701912017-04-08 11:59:34 +0200193 u32 saved_size, saved_offset, sector;
Marek Vasutd73c1292014-03-05 19:59:50 +0100194 char *saved_buffer = NULL;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000195 int ret = 1;
Tom Rinia840b8e2013-04-05 14:55:21 -0400196 env_t env_new;
Gong Qianyu6d85a602016-01-26 15:06:42 +0800197
Andreas Fenkart52b94442017-04-08 11:59:31 +0200198 ret = setup_flash_device();
199 if (ret)
200 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200201
Mike Frysinger9af10d52008-12-11 06:23:37 -0500202 /* Is the sector larger than the env (i.e. embedded) */
203 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
204 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
205 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
206 saved_buffer = malloc(saved_size);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000207 if (!saved_buffer)
Mike Frysinger9af10d52008-12-11 06:23:37 -0500208 goto done;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000209
Heiko Schocherf45775c2019-03-13 12:15:45 +0100210 ret = spi_flash_read(env_flash, saved_offset,
211 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500212 if (ret)
213 goto done;
214 }
215
Marek Vasutd73c1292014-03-05 19:59:50 +0100216 ret = env_export(&env_new);
217 if (ret)
Stefano Babic5f6c2862010-10-27 11:06:20 +0200218 goto done;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200219
Andreas Fenkartdf701912017-04-08 11:59:34 +0200220 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
221
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200222 puts("Erasing SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200223 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
224 sector * CONFIG_ENV_SECT_SIZE);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500225 if (ret)
226 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200227
228 puts("Writing to SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200229 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
Tom Rinia840b8e2013-04-05 14:55:21 -0400230 CONFIG_ENV_SIZE, &env_new);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500231 if (ret)
232 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200233
Mike Frysinger9af10d52008-12-11 06:23:37 -0500234 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200235 ret = spi_flash_write(env_flash, saved_offset,
236 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500237 if (ret)
238 goto done;
239 }
240
241 ret = 0;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200242 puts("done\n");
Mike Frysinger9af10d52008-12-11 06:23:37 -0500243
244 done:
245 if (saved_buffer)
246 free(saved_buffer);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000247
Mike Frysinger9af10d52008-12-11 06:23:37 -0500248 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200249}
Simon Glassc10a88e2017-08-03 12:21:58 -0600250#endif /* CMD_SAVEENV */
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200251
Simon Glass99778492017-08-03 12:22:17 -0600252static int env_sf_load(void)
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200253{
254 int ret;
Ying Zhang354846f2014-01-24 15:50:07 +0800255 char *buf = NULL;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200256
Ravi Babu20d93fc2015-08-17 13:29:49 +0530257 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200258 if (!buf) {
Simon Glass97385862019-08-01 09:47:00 -0600259 env_set_default("malloc() failed", 0);
Simon Glass99778492017-08-03 12:22:17 -0600260 return -EIO;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200261 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200262
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200263 ret = setup_flash_device();
264 if (ret)
265 goto out;
266
Heiko Schocherf45775c2019-03-13 12:15:45 +0100267 ret = spi_flash_read(env_flash,
268 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200269 if (ret) {
Simon Glass97385862019-08-01 09:47:00 -0600270 env_set_default("spi_flash_read() failed", 0);
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200271 goto err_read;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200272 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200273
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200274 ret = env_import(buf, 1);
Simon Goldschmidt32c3eb52018-01-31 14:47:10 +0100275 if (!ret)
Simon Glass4bc2ad22017-08-03 12:21:56 -0600276 gd->env_valid = ENV_VALID;
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200277
278err_read:
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200279 spi_flash_free(env_flash);
280 env_flash = NULL;
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200281out:
282 free(buf);
Simon Glass99778492017-08-03 12:22:17 -0600283
284 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200285}
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200286#endif
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200287
Tom Rini5cd7ece2019-11-18 20:02:10 -0500288#if CONFIG_ENV_ADDR != 0x0
Rajesh Bhagat9a90cc92018-11-05 18:01:15 +0000289__weak void *env_sf_get_env_addr(void)
290{
291 return (void *)CONFIG_ENV_ADDR;
292}
293#endif
294
Tom Rini5cd7ece2019-11-18 20:02:10 -0500295#if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0)
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530296static int env_sf_init(void)
297{
Rajesh Bhagat9a90cc92018-11-05 18:01:15 +0000298 env_t *env_ptr = (env_t *)env_sf_get_env_addr();
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530299
300 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
301 gd->env_addr = (ulong)&(env_ptr->data);
302 gd->env_valid = 1;
303 } else {
304 gd->env_addr = (ulong)&default_environment[0];
305 gd->env_valid = 1;
306 }
307
308 return 0;
309}
310#endif
311
Simon Glassc10a88e2017-08-03 12:21:58 -0600312U_BOOT_ENV_LOCATION(sf) = {
313 .location = ENVL_SPI_FLASH,
Simon Glassd8273ed2017-08-03 12:22:03 -0600314 ENV_NAME("SPI Flash")
Simon Glass082af922017-08-03 12:22:01 -0600315 .load = env_sf_load,
Simon Glassc10a88e2017-08-03 12:21:58 -0600316#ifdef CMD_SAVEENV
Simon Glass082af922017-08-03 12:22:01 -0600317 .save = env_save_ptr(env_sf_save),
Simon Glassc10a88e2017-08-03 12:21:58 -0600318#endif
Tom Rini5cd7ece2019-11-18 20:02:10 -0500319#if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0)
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530320 .init = env_sf_init,
321#endif
Simon Glassc10a88e2017-08-03 12:21:58 -0600322};