blob: 590d0cedd852999eec5ea485a92b265d0066401f [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>
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020021
Simon Glassc10a88e2017-08-03 12:21:58 -060022#ifndef CONFIG_SPL_BUILD
23#define CMD_SAVEENV
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
Simon Glassc10a88e2017-08-03 12:21:58 -060028#ifdef CMD_SAVEENV
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000029static ulong env_offset = CONFIG_ENV_OFFSET;
30static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
Simon Glassc10a88e2017-08-03 12:21:58 -060031#endif
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020032
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
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020056
57 if (!env_flash) {
Stefano Babic5f6c2862010-10-27 11:06:20 +020058 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
59 CONFIG_ENV_SPI_CS,
60 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
61 if (!env_flash) {
Simon Glass97385862019-08-01 09:47:00 -060062 env_set_default("spi_flash_probe() failed", 0);
Simon Glass99778492017-08-03 12:22:17 -060063 return -EIO;
Stefano Babic5f6c2862010-10-27 11:06:20 +020064 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020065 }
Gong Qianyu6d85a602016-01-26 15:06:42 +080066#endif
Andreas Fenkart52b94442017-04-08 11:59:31 +020067 return 0;
68}
69
70#if defined(CONFIG_ENV_OFFSET_REDUND)
Simon Glassc10a88e2017-08-03 12:21:58 -060071#ifdef CMD_SAVEENV
Simon Glass082af922017-08-03 12:22:01 -060072static int env_sf_save(void)
Andreas Fenkart52b94442017-04-08 11:59:31 +020073{
74 env_t env_new;
Simon Glass4686d8f2019-08-01 09:47:08 -060075 char *saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
Andreas Fenkartdf701912017-04-08 11:59:34 +020076 u32 saved_size, saved_offset, sector;
Andreas Fenkart52b94442017-04-08 11:59:31 +020077 int ret;
78
79 ret = setup_flash_device();
80 if (ret)
81 return ret;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020082
Marek Vasutd73c1292014-03-05 19:59:50 +010083 ret = env_export(&env_new);
84 if (ret)
Simon Glass99778492017-08-03 12:22:17 -060085 return -EIO;
Simon Glass4686d8f2019-08-01 09:47:08 -060086 env_new.flags = ENV_REDUND_ACTIVE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020087
Simon Glass4bc2ad22017-08-03 12:21:56 -060088 if (gd->env_valid == ENV_VALID) {
Stefano Babic5f6c2862010-10-27 11:06:20 +020089 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
90 env_offset = CONFIG_ENV_OFFSET;
91 } else {
92 env_new_offset = CONFIG_ENV_OFFSET;
93 env_offset = CONFIG_ENV_OFFSET_REDUND;
94 }
95
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020096 /* Is the sector larger than the env (i.e. embedded) */
97 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
98 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
99 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
Ravi Babu20d93fc2015-08-17 13:29:49 +0530100 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200101 if (!saved_buffer) {
Simon Glass99778492017-08-03 12:22:17 -0600102 ret = -ENOMEM;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200103 goto done;
104 }
Heiko Schocherf45775c2019-03-13 12:15:45 +0100105 ret = spi_flash_read(env_flash, saved_offset,
106 saved_size, saved_buffer);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200107 if (ret)
108 goto done;
109 }
110
Andreas Fenkartdf701912017-04-08 11:59:34 +0200111 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200112
113 puts("Erasing SPI flash...");
114 ret = spi_flash_erase(env_flash, env_new_offset,
115 sector * CONFIG_ENV_SECT_SIZE);
116 if (ret)
117 goto done;
118
119 puts("Writing to SPI flash...");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200120
Stefano Babic5f6c2862010-10-27 11:06:20 +0200121 ret = spi_flash_write(env_flash, env_new_offset,
Tom Rinia840b8e2013-04-05 14:55:21 -0400122 CONFIG_ENV_SIZE, &env_new);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200123 if (ret)
124 goto done;
125
126 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
127 ret = spi_flash_write(env_flash, saved_offset,
128 saved_size, saved_buffer);
129 if (ret)
130 goto done;
131 }
132
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000133 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
Tom Rinia840b8e2013-04-05 14:55:21 -0400134 sizeof(env_new.flags), &flag);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200135 if (ret)
136 goto done;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200137
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200138 puts("done\n");
139
Simon Glass4bc2ad22017-08-03 12:21:56 -0600140 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200141
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000142 printf("Valid environment: %d\n", (int)gd->env_valid);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200143
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200144 done:
145 if (saved_buffer)
146 free(saved_buffer);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000147
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200148 return ret;
149}
Simon Glassc10a88e2017-08-03 12:21:58 -0600150#endif /* CMD_SAVEENV */
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200151
Simon Glass99778492017-08-03 12:22:17 -0600152static int env_sf_load(void)
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200153{
154 int ret;
Simon Goldschmidt8f9ce3a2018-01-31 14:47:13 +0100155 int read1_fail, read2_fail;
156 env_t *tmp_env1, *tmp_env2;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200157
Ravi Babu20d93fc2015-08-17 13:29:49 +0530158 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
159 CONFIG_ENV_SIZE);
160 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
161 CONFIG_ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200162 if (!tmp_env1 || !tmp_env2) {
Simon Glass97385862019-08-01 09:47:00 -0600163 env_set_default("malloc() failed", 0);
Simon Glass99778492017-08-03 12:22:17 -0600164 ret = -EIO;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000165 goto out;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200166 }
167
Andreas Fenkarta2d18832017-04-08 11:59:32 +0200168 ret = setup_flash_device();
169 if (ret)
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000170 goto out;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200171
Heiko Schocherf45775c2019-03-13 12:15:45 +0100172 read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
173 CONFIG_ENV_SIZE, tmp_env1);
174 read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
175 CONFIG_ENV_SIZE, tmp_env2);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200176
Simon Goldschmidt8f9ce3a2018-01-31 14:47:13 +0100177 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
178 read2_fail);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200179
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200180 spi_flash_free(env_flash);
181 env_flash = NULL;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200182out:
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200183 free(tmp_env1);
184 free(tmp_env2);
Simon Glass99778492017-08-03 12:22:17 -0600185
186 return ret;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200187}
188#else
Simon Glassc10a88e2017-08-03 12:21:58 -0600189#ifdef CMD_SAVEENV
Simon Glass082af922017-08-03 12:22:01 -0600190static int env_sf_save(void)
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200191{
Andreas Fenkartdf701912017-04-08 11:59:34 +0200192 u32 saved_size, saved_offset, sector;
Marek Vasutd73c1292014-03-05 19:59:50 +0100193 char *saved_buffer = NULL;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000194 int ret = 1;
Tom Rinia840b8e2013-04-05 14:55:21 -0400195 env_t env_new;
Gong Qianyu6d85a602016-01-26 15:06:42 +0800196
Andreas Fenkart52b94442017-04-08 11:59:31 +0200197 ret = setup_flash_device();
198 if (ret)
199 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200200
Mike Frysinger9af10d52008-12-11 06:23:37 -0500201 /* Is the sector larger than the env (i.e. embedded) */
202 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
203 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
204 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
205 saved_buffer = malloc(saved_size);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000206 if (!saved_buffer)
Mike Frysinger9af10d52008-12-11 06:23:37 -0500207 goto done;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000208
Heiko Schocherf45775c2019-03-13 12:15:45 +0100209 ret = spi_flash_read(env_flash, saved_offset,
210 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500211 if (ret)
212 goto done;
213 }
214
Marek Vasutd73c1292014-03-05 19:59:50 +0100215 ret = env_export(&env_new);
216 if (ret)
Stefano Babic5f6c2862010-10-27 11:06:20 +0200217 goto done;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200218
Andreas Fenkartdf701912017-04-08 11:59:34 +0200219 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
220
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200221 puts("Erasing SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200222 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
223 sector * CONFIG_ENV_SECT_SIZE);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500224 if (ret)
225 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200226
227 puts("Writing to SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200228 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
Tom Rinia840b8e2013-04-05 14:55:21 -0400229 CONFIG_ENV_SIZE, &env_new);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500230 if (ret)
231 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200232
Mike Frysinger9af10d52008-12-11 06:23:37 -0500233 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200234 ret = spi_flash_write(env_flash, saved_offset,
235 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500236 if (ret)
237 goto done;
238 }
239
240 ret = 0;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200241 puts("done\n");
Mike Frysinger9af10d52008-12-11 06:23:37 -0500242
243 done:
244 if (saved_buffer)
245 free(saved_buffer);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000246
Mike Frysinger9af10d52008-12-11 06:23:37 -0500247 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200248}
Simon Glassc10a88e2017-08-03 12:21:58 -0600249#endif /* CMD_SAVEENV */
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200250
Simon Glass99778492017-08-03 12:22:17 -0600251static int env_sf_load(void)
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200252{
253 int ret;
Ying Zhang354846f2014-01-24 15:50:07 +0800254 char *buf = NULL;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200255
Ravi Babu20d93fc2015-08-17 13:29:49 +0530256 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200257 if (!buf) {
Simon Glass97385862019-08-01 09:47:00 -0600258 env_set_default("malloc() failed", 0);
Simon Glass99778492017-08-03 12:22:17 -0600259 return -EIO;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200260 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200261
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200262 ret = setup_flash_device();
263 if (ret)
264 goto out;
265
Heiko Schocherf45775c2019-03-13 12:15:45 +0100266 ret = spi_flash_read(env_flash,
267 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200268 if (ret) {
Simon Glass97385862019-08-01 09:47:00 -0600269 env_set_default("spi_flash_read() failed", 0);
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200270 goto err_read;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200271 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200272
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200273 ret = env_import(buf, 1);
Simon Goldschmidt32c3eb52018-01-31 14:47:10 +0100274 if (!ret)
Simon Glass4bc2ad22017-08-03 12:21:56 -0600275 gd->env_valid = ENV_VALID;
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200276
277err_read:
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200278 spi_flash_free(env_flash);
279 env_flash = NULL;
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200280out:
281 free(buf);
Simon Glass99778492017-08-03 12:22:17 -0600282
283 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200284}
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200285#endif
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200286
Rajesh Bhagat9a90cc92018-11-05 18:01:15 +0000287#ifdef CONFIG_ENV_ADDR
288__weak void *env_sf_get_env_addr(void)
289{
290 return (void *)CONFIG_ENV_ADDR;
291}
292#endif
293
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530294#if defined(INITENV) && defined(CONFIG_ENV_ADDR)
295static int env_sf_init(void)
296{
Rajesh Bhagat9a90cc92018-11-05 18:01:15 +0000297 env_t *env_ptr = (env_t *)env_sf_get_env_addr();
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530298
299 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
300 gd->env_addr = (ulong)&(env_ptr->data);
301 gd->env_valid = 1;
302 } else {
303 gd->env_addr = (ulong)&default_environment[0];
304 gd->env_valid = 1;
305 }
306
307 return 0;
308}
309#endif
310
Simon Glassc10a88e2017-08-03 12:21:58 -0600311U_BOOT_ENV_LOCATION(sf) = {
312 .location = ENVL_SPI_FLASH,
Simon Glassd8273ed2017-08-03 12:22:03 -0600313 ENV_NAME("SPI Flash")
Simon Glass082af922017-08-03 12:22:01 -0600314 .load = env_sf_load,
Simon Glassc10a88e2017-08-03 12:21:58 -0600315#ifdef CMD_SAVEENV
Simon Glass082af922017-08-03 12:22:01 -0600316 .save = env_save_ptr(env_sf_save),
Simon Glassc10a88e2017-08-03 12:21:58 -0600317#endif
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530318#if defined(INITENV) && defined(CONFIG_ENV_ADDR)
319 .init = env_sf_init,
320#endif
Simon Glassc10a88e2017-08-03 12:21:58 -0600321};