blob: 590d0cedd852999eec5ea485a92b265d0066401f [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>
Mike Frysinger5b3375a2008-12-11 06:23:37 -050015#include <malloc.h>
Simon Glass843c9e82014-10-13 23:41:55 -060016#include <spi.h>
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020017#include <spi_flash.h>
Wolfgang Denkea882ba2010-06-20 23:33:59 +020018#include <search.h>
19#include <errno.h>
Gong Qianyu19c31282016-01-26 15:06:42 +080020#include <dm/device-internal.h>
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020021
Simon Glass4415f1d2017-08-03 12:21:58 -060022#ifndef CONFIG_SPL_BUILD
23#define CMD_SAVEENV
Ashish Kumarb500c922017-12-14 17:37:08 +053024#define INITENV
Simon Glass4415f1d2017-08-03 12:21:58 -060025#endif
26
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020027#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass4415f1d2017-08-03 12:21:58 -060028#ifdef CMD_SAVEENV
Igor Grinbergeb58a7f2011-11-07 01:14:08 +000029static ulong env_offset = CONFIG_ENV_OFFSET;
30static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
Simon Glass4415f1d2017-08-03 12:21:58 -060031#endif
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020032
Stefano Babica3110f02010-10-27 11:06:20 +020033#endif /* CONFIG_ENV_OFFSET_REDUND */
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020034
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020035DECLARE_GLOBAL_DATA_PTR;
36
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020037static struct spi_flash *env_flash;
38
Andreas Fenkartafa81a72017-04-08 11:59:31 +020039static int setup_flash_device(void)
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020040{
Gong Qianyu19c31282016-01-26 15:06:42 +080041#ifdef CONFIG_DM_SPI_FLASH
42 struct udevice *new;
Andreas Fenkartafa81a72017-04-08 11:59:31 +020043 int ret;
Gong Qianyu19c31282016-01-26 15:06:42 +080044
Vignesh R96907c02016-07-06 10:04:28 +053045 /* speed and mode will be read from DT */
Gong Qianyu19c31282016-01-26 15:06:42 +080046 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
Konstantin Porotchkin25a17652018-08-29 16:34:52 +030047 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE,
48 &new);
Gong Qianyu19c31282016-01-26 15:06:42 +080049 if (ret) {
Simon Glass0ac7d722019-08-01 09:47:00 -060050 env_set_default("spi_flash_probe_bus_cs() failed", 0);
Simon Glassc5951992017-08-03 12:22:17 -060051 return ret;
Gong Qianyu19c31282016-01-26 15:06:42 +080052 }
53
54 env_flash = dev_get_uclass_priv(new);
55#else
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020056
57 if (!env_flash) {
Stefano Babica3110f02010-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 Glass0ac7d722019-08-01 09:47:00 -060062 env_set_default("spi_flash_probe() failed", 0);
Simon Glassc5951992017-08-03 12:22:17 -060063 return -EIO;
Stefano Babica3110f02010-10-27 11:06:20 +020064 }
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020065 }
Gong Qianyu19c31282016-01-26 15:06:42 +080066#endif
Andreas Fenkartafa81a72017-04-08 11:59:31 +020067 return 0;
68}
69
70#if defined(CONFIG_ENV_OFFSET_REDUND)
Simon Glass4415f1d2017-08-03 12:21:58 -060071#ifdef CMD_SAVEENV
Simon Glasse5bce242017-08-03 12:22:01 -060072static int env_sf_save(void)
Andreas Fenkartafa81a72017-04-08 11:59:31 +020073{
74 env_t env_new;
Simon Glassd3716dd2019-08-01 09:47:08 -060075 char *saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +020076 u32 saved_size, saved_offset, sector;
Andreas Fenkartafa81a72017-04-08 11:59:31 +020077 int ret;
78
79 ret = setup_flash_device();
80 if (ret)
81 return ret;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020082
Marek Vasut7ce15262014-03-05 19:59:50 +010083 ret = env_export(&env_new);
84 if (ret)
Simon Glassc5951992017-08-03 12:22:17 -060085 return -EIO;
Simon Glassd3716dd2019-08-01 09:47:08 -060086 env_new.flags = ENV_REDUND_ACTIVE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020087
Simon Glass203e94f2017-08-03 12:21:56 -060088 if (gd->env_valid == ENV_VALID) {
Stefano Babica3110f02010-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 Wegner7319bca2010-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 Babu7dd01742015-08-17 13:29:49 +0530100 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200101 if (!saved_buffer) {
Simon Glassc5951992017-08-03 12:22:17 -0600102 ret = -ENOMEM;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200103 goto done;
104 }
Heiko Schocher9ba5e5b2019-03-13 12:15:45 +0100105 ret = spi_flash_read(env_flash, saved_offset,
106 saved_size, saved_buffer);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200107 if (ret)
108 goto done;
109 }
110
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +0200111 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
Wolfgang Wegner7319bca2010-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 Wegner7319bca2010-04-23 17:22:55 +0200120
Stefano Babica3110f02010-10-27 11:06:20 +0200121 ret = spi_flash_write(env_flash, env_new_offset,
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400122 CONFIG_ENV_SIZE, &env_new);
Wolfgang Wegner7319bca2010-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 Grinbergeb58a7f2011-11-07 01:14:08 +0000133 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400134 sizeof(env_new.flags), &flag);
Stefano Babica3110f02010-10-27 11:06:20 +0200135 if (ret)
136 goto done;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200137
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200138 puts("done\n");
139
Simon Glass203e94f2017-08-03 12:21:56 -0600140 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
Stefano Babica3110f02010-10-27 11:06:20 +0200141
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000142 printf("Valid environment: %d\n", (int)gd->env_valid);
Stefano Babica3110f02010-10-27 11:06:20 +0200143
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200144 done:
145 if (saved_buffer)
146 free(saved_buffer);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000147
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200148 return ret;
149}
Simon Glass4415f1d2017-08-03 12:21:58 -0600150#endif /* CMD_SAVEENV */
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200151
Simon Glassc5951992017-08-03 12:22:17 -0600152static int env_sf_load(void)
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200153{
154 int ret;
Simon Goldschmidt80719932018-01-31 14:47:13 +0100155 int read1_fail, read2_fail;
156 env_t *tmp_env1, *tmp_env2;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200157
Ravi Babu7dd01742015-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 Denkea882ba2010-06-20 23:33:59 +0200162 if (!tmp_env1 || !tmp_env2) {
Simon Glass0ac7d722019-08-01 09:47:00 -0600163 env_set_default("malloc() failed", 0);
Simon Glassc5951992017-08-03 12:22:17 -0600164 ret = -EIO;
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000165 goto out;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200166 }
167
Andreas Fenkart8fee8842017-04-08 11:59:32 +0200168 ret = setup_flash_device();
169 if (ret)
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000170 goto out;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200171
Heiko Schocher9ba5e5b2019-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 Wegner7319bca2010-04-23 17:22:55 +0200176
Simon Goldschmidt80719932018-01-31 14:47:13 +0100177 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
178 read2_fail);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200179
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200180 spi_flash_free(env_flash);
181 env_flash = NULL;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200182out:
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200183 free(tmp_env1);
184 free(tmp_env2);
Simon Glassc5951992017-08-03 12:22:17 -0600185
186 return ret;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200187}
188#else
Simon Glass4415f1d2017-08-03 12:21:58 -0600189#ifdef CMD_SAVEENV
Simon Glasse5bce242017-08-03 12:22:01 -0600190static int env_sf_save(void)
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200191{
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +0200192 u32 saved_size, saved_offset, sector;
Marek Vasut7ce15262014-03-05 19:59:50 +0100193 char *saved_buffer = NULL;
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000194 int ret = 1;
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400195 env_t env_new;
Gong Qianyu19c31282016-01-26 15:06:42 +0800196
Andreas Fenkartafa81a72017-04-08 11:59:31 +0200197 ret = setup_flash_device();
198 if (ret)
199 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200200
Mike Frysinger5b3375a2008-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 Grinbergeb58a7f2011-11-07 01:14:08 +0000206 if (!saved_buffer)
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500207 goto done;
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000208
Heiko Schocher9ba5e5b2019-03-13 12:15:45 +0100209 ret = spi_flash_read(env_flash, saved_offset,
210 saved_size, saved_buffer);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500211 if (ret)
212 goto done;
213 }
214
Marek Vasut7ce15262014-03-05 19:59:50 +0100215 ret = env_export(&env_new);
216 if (ret)
Stefano Babica3110f02010-10-27 11:06:20 +0200217 goto done;
Stefano Babica3110f02010-10-27 11:06:20 +0200218
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +0200219 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
220
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200221 puts("Erasing SPI flash...");
Stefano Babica3110f02010-10-27 11:06:20 +0200222 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
223 sector * CONFIG_ENV_SECT_SIZE);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500224 if (ret)
225 goto done;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200226
227 puts("Writing to SPI flash...");
Stefano Babica3110f02010-10-27 11:06:20 +0200228 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400229 CONFIG_ENV_SIZE, &env_new);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500230 if (ret)
231 goto done;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200232
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500233 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
Stefano Babica3110f02010-10-27 11:06:20 +0200234 ret = spi_flash_write(env_flash, saved_offset,
235 saved_size, saved_buffer);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500236 if (ret)
237 goto done;
238 }
239
240 ret = 0;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200241 puts("done\n");
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500242
243 done:
244 if (saved_buffer)
245 free(saved_buffer);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000246
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500247 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200248}
Simon Glass4415f1d2017-08-03 12:21:58 -0600249#endif /* CMD_SAVEENV */
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200250
Simon Glassc5951992017-08-03 12:22:17 -0600251static int env_sf_load(void)
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200252{
253 int ret;
Ying Zhang5a89fa92014-01-24 15:50:07 +0800254 char *buf = NULL;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200255
Ravi Babu7dd01742015-08-17 13:29:49 +0530256 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
Andreas Fenkartc041c602017-04-08 11:59:33 +0200257 if (!buf) {
Simon Glass0ac7d722019-08-01 09:47:00 -0600258 env_set_default("malloc() failed", 0);
Simon Glassc5951992017-08-03 12:22:17 -0600259 return -EIO;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200260 }
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200261
Andreas Fenkartc041c602017-04-08 11:59:33 +0200262 ret = setup_flash_device();
263 if (ret)
264 goto out;
265
Heiko Schocher9ba5e5b2019-03-13 12:15:45 +0100266 ret = spi_flash_read(env_flash,
267 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200268 if (ret) {
Simon Glass0ac7d722019-08-01 09:47:00 -0600269 env_set_default("spi_flash_read() failed", 0);
Andreas Fenkartc041c602017-04-08 11:59:33 +0200270 goto err_read;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200271 }
272
273 ret = env_import(buf, 1);
Simon Goldschmidt42a18202018-01-31 14:47:10 +0100274 if (!ret)
Simon Glass203e94f2017-08-03 12:21:56 -0600275 gd->env_valid = ENV_VALID;
Andreas Fenkartc041c602017-04-08 11:59:33 +0200276
277err_read:
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200278 spi_flash_free(env_flash);
279 env_flash = NULL;
Andreas Fenkartc041c602017-04-08 11:59:33 +0200280out:
281 free(buf);
Simon Glassc5951992017-08-03 12:22:17 -0600282
283 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200284}
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200285#endif
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200286
Rajesh Bhagat119c01c22018-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 Kumarb500c922017-12-14 17:37:08 +0530294#if defined(INITENV) && defined(CONFIG_ENV_ADDR)
295static int env_sf_init(void)
296{
Rajesh Bhagat119c01c22018-11-05 18:01:15 +0000297 env_t *env_ptr = (env_t *)env_sf_get_env_addr();
Ashish Kumarb500c922017-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 Glass4415f1d2017-08-03 12:21:58 -0600311U_BOOT_ENV_LOCATION(sf) = {
312 .location = ENVL_SPI_FLASH,
Simon Glassac358be2017-08-03 12:22:03 -0600313 ENV_NAME("SPI Flash")
Simon Glasse5bce242017-08-03 12:22:01 -0600314 .load = env_sf_load,
Simon Glass4415f1d2017-08-03 12:21:58 -0600315#ifdef CMD_SAVEENV
Simon Glasse5bce242017-08-03 12:22:01 -0600316 .save = env_save_ptr(env_sf_save),
Simon Glass4415f1d2017-08-03 12:21:58 -0600317#endif
Ashish Kumarb500c922017-12-14 17:37:08 +0530318#if defined(INITENV) && defined(CONFIG_ENV_ADDR)
319 .init = env_sf_init,
320#endif
Simon Glass4415f1d2017-08-03 12:21:58 -0600321};