blob: 4f92ae0568edbb9c04a9a771f8736224f29948e9 [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>
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020013#include <environment.h>
Mike Frysinger5b3375a2008-12-11 06:23:37 -050014#include <malloc.h>
Simon Glass843c9e82014-10-13 23:41:55 -060015#include <spi.h>
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020016#include <spi_flash.h>
Wolfgang Denkea882ba2010-06-20 23:33:59 +020017#include <search.h>
18#include <errno.h>
Gong Qianyu19c31282016-01-26 15:06:42 +080019#include <dm/device-internal.h>
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020020
Simon Glass4415f1d2017-08-03 12:21:58 -060021#ifndef CONFIG_SPL_BUILD
22#define CMD_SAVEENV
Ashish Kumarb500c922017-12-14 17:37:08 +053023#define INITENV
Simon Glass4415f1d2017-08-03 12:21:58 -060024#endif
25
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020026#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass4415f1d2017-08-03 12:21:58 -060027#ifdef CMD_SAVEENV
Igor Grinbergeb58a7f2011-11-07 01:14:08 +000028static ulong env_offset = CONFIG_ENV_OFFSET;
29static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
Simon Glass4415f1d2017-08-03 12:21:58 -060030#endif
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020031
Igor Grinbergeb58a7f2011-11-07 01:14:08 +000032#define ACTIVE_FLAG 1
33#define OBSOLETE_FLAG 0
Stefano Babica3110f02010-10-27 11:06:20 +020034#endif /* CONFIG_ENV_OFFSET_REDUND */
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020035
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020036DECLARE_GLOBAL_DATA_PTR;
37
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020038static struct spi_flash *env_flash;
39
Andreas Fenkartafa81a72017-04-08 11:59:31 +020040static int setup_flash_device(void)
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020041{
Gong Qianyu19c31282016-01-26 15:06:42 +080042#ifdef CONFIG_DM_SPI_FLASH
43 struct udevice *new;
Andreas Fenkartafa81a72017-04-08 11:59:31 +020044 int ret;
Gong Qianyu19c31282016-01-26 15:06:42 +080045
Vignesh R96907c02016-07-06 10:04:28 +053046 /* speed and mode will be read from DT */
Gong Qianyu19c31282016-01-26 15:06:42 +080047 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
Konstantin Porotchkin25a17652018-08-29 16:34:52 +030048 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE,
49 &new);
Gong Qianyu19c31282016-01-26 15:06:42 +080050 if (ret) {
Yaniv Levinskyc5d548a2018-06-24 19:16:57 +030051 set_default_env("spi_flash_probe_bus_cs() failed", 0);
Simon Glassc5951992017-08-03 12:22:17 -060052 return ret;
Gong Qianyu19c31282016-01-26 15:06:42 +080053 }
54
55 env_flash = dev_get_uclass_priv(new);
56#else
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020057
58 if (!env_flash) {
Stefano Babica3110f02010-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) {
Yaniv Levinskyc5d548a2018-06-24 19:16:57 +030063 set_default_env("spi_flash_probe() failed", 0);
Simon Glassc5951992017-08-03 12:22:17 -060064 return -EIO;
Stefano Babica3110f02010-10-27 11:06:20 +020065 }
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020066 }
Gong Qianyu19c31282016-01-26 15:06:42 +080067#endif
Andreas Fenkartafa81a72017-04-08 11:59:31 +020068 return 0;
69}
70
71#if defined(CONFIG_ENV_OFFSET_REDUND)
Simon Glass4415f1d2017-08-03 12:21:58 -060072#ifdef CMD_SAVEENV
Simon Glasse5bce242017-08-03 12:22:01 -060073static int env_sf_save(void)
Andreas Fenkartafa81a72017-04-08 11:59:31 +020074{
75 env_t env_new;
76 char *saved_buffer = NULL, flag = OBSOLETE_FLAG;
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +020077 u32 saved_size, saved_offset, sector;
Andreas Fenkartafa81a72017-04-08 11:59:31 +020078 int ret;
79
80 ret = setup_flash_device();
81 if (ret)
82 return ret;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020083
Marek Vasut7ce15262014-03-05 19:59:50 +010084 ret = env_export(&env_new);
85 if (ret)
Simon Glassc5951992017-08-03 12:22:17 -060086 return -EIO;
Tom Rinicd0f4fa2013-04-05 14:55:21 -040087 env_new.flags = ACTIVE_FLAG;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020088
Simon Glass203e94f2017-08-03 12:21:56 -060089 if (gd->env_valid == ENV_VALID) {
Stefano Babica3110f02010-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 Wegner7319bca2010-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 Babu7dd01742015-08-17 13:29:49 +0530101 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200102 if (!saved_buffer) {
Simon Glassc5951992017-08-03 12:22:17 -0600103 ret = -ENOMEM;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200104 goto done;
105 }
Heiko Schocher9ba5e5b2019-03-13 12:15:45 +0100106 ret = spi_flash_read(env_flash, saved_offset,
107 saved_size, saved_buffer);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200108 if (ret)
109 goto done;
110 }
111
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +0200112 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
Wolfgang Wegner7319bca2010-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 Wegner7319bca2010-04-23 17:22:55 +0200121
Stefano Babica3110f02010-10-27 11:06:20 +0200122 ret = spi_flash_write(env_flash, env_new_offset,
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400123 CONFIG_ENV_SIZE, &env_new);
Wolfgang Wegner7319bca2010-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 Grinbergeb58a7f2011-11-07 01:14:08 +0000134 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400135 sizeof(env_new.flags), &flag);
Stefano Babica3110f02010-10-27 11:06:20 +0200136 if (ret)
137 goto done;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200138
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200139 puts("done\n");
140
Simon Glass203e94f2017-08-03 12:21:56 -0600141 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
Stefano Babica3110f02010-10-27 11:06:20 +0200142
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000143 printf("Valid environment: %d\n", (int)gd->env_valid);
Stefano Babica3110f02010-10-27 11:06:20 +0200144
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200145 done:
146 if (saved_buffer)
147 free(saved_buffer);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000148
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200149 return ret;
150}
Simon Glass4415f1d2017-08-03 12:21:58 -0600151#endif /* CMD_SAVEENV */
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200152
Simon Glassc5951992017-08-03 12:22:17 -0600153static int env_sf_load(void)
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200154{
155 int ret;
Simon Goldschmidt80719932018-01-31 14:47:13 +0100156 int read1_fail, read2_fail;
157 env_t *tmp_env1, *tmp_env2;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200158
Ravi Babu7dd01742015-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 Denkea882ba2010-06-20 23:33:59 +0200163 if (!tmp_env1 || !tmp_env2) {
Yaniv Levinskyc5d548a2018-06-24 19:16:57 +0300164 set_default_env("malloc() failed", 0);
Simon Glassc5951992017-08-03 12:22:17 -0600165 ret = -EIO;
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000166 goto out;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200167 }
168
Andreas Fenkart8fee8842017-04-08 11:59:32 +0200169 ret = setup_flash_device();
170 if (ret)
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000171 goto out;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200172
Heiko Schocher9ba5e5b2019-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 Wegner7319bca2010-04-23 17:22:55 +0200177
Simon Goldschmidt80719932018-01-31 14:47:13 +0100178 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
179 read2_fail);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200180
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200181 spi_flash_free(env_flash);
182 env_flash = NULL;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200183out:
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200184 free(tmp_env1);
185 free(tmp_env2);
Simon Glassc5951992017-08-03 12:22:17 -0600186
187 return ret;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200188}
189#else
Simon Glass4415f1d2017-08-03 12:21:58 -0600190#ifdef CMD_SAVEENV
Simon Glasse5bce242017-08-03 12:22:01 -0600191static int env_sf_save(void)
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200192{
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +0200193 u32 saved_size, saved_offset, sector;
Marek Vasut7ce15262014-03-05 19:59:50 +0100194 char *saved_buffer = NULL;
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000195 int ret = 1;
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400196 env_t env_new;
Gong Qianyu19c31282016-01-26 15:06:42 +0800197
Andreas Fenkartafa81a72017-04-08 11:59:31 +0200198 ret = setup_flash_device();
199 if (ret)
200 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200201
Mike Frysinger5b3375a2008-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 Grinbergeb58a7f2011-11-07 01:14:08 +0000207 if (!saved_buffer)
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500208 goto done;
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000209
Heiko Schocher9ba5e5b2019-03-13 12:15:45 +0100210 ret = spi_flash_read(env_flash, saved_offset,
211 saved_size, saved_buffer);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500212 if (ret)
213 goto done;
214 }
215
Marek Vasut7ce15262014-03-05 19:59:50 +0100216 ret = env_export(&env_new);
217 if (ret)
Stefano Babica3110f02010-10-27 11:06:20 +0200218 goto done;
Stefano Babica3110f02010-10-27 11:06:20 +0200219
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +0200220 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
221
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200222 puts("Erasing SPI flash...");
Stefano Babica3110f02010-10-27 11:06:20 +0200223 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
224 sector * CONFIG_ENV_SECT_SIZE);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500225 if (ret)
226 goto done;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200227
228 puts("Writing to SPI flash...");
Stefano Babica3110f02010-10-27 11:06:20 +0200229 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400230 CONFIG_ENV_SIZE, &env_new);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500231 if (ret)
232 goto done;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200233
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500234 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
Stefano Babica3110f02010-10-27 11:06:20 +0200235 ret = spi_flash_write(env_flash, saved_offset,
236 saved_size, saved_buffer);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500237 if (ret)
238 goto done;
239 }
240
241 ret = 0;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200242 puts("done\n");
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500243
244 done:
245 if (saved_buffer)
246 free(saved_buffer);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000247
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500248 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200249}
Simon Glass4415f1d2017-08-03 12:21:58 -0600250#endif /* CMD_SAVEENV */
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200251
Simon Glassc5951992017-08-03 12:22:17 -0600252static int env_sf_load(void)
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200253{
254 int ret;
Ying Zhang5a89fa92014-01-24 15:50:07 +0800255 char *buf = NULL;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200256
Ravi Babu7dd01742015-08-17 13:29:49 +0530257 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
Andreas Fenkartc041c602017-04-08 11:59:33 +0200258 if (!buf) {
Yaniv Levinskyc5d548a2018-06-24 19:16:57 +0300259 set_default_env("malloc() failed", 0);
Simon Glassc5951992017-08-03 12:22:17 -0600260 return -EIO;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200261 }
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200262
Andreas Fenkartc041c602017-04-08 11:59:33 +0200263 ret = setup_flash_device();
264 if (ret)
265 goto out;
266
Heiko Schocher9ba5e5b2019-03-13 12:15:45 +0100267 ret = spi_flash_read(env_flash,
268 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200269 if (ret) {
Heiko Schocher9ba5e5b2019-03-13 12:15:45 +0100270 set_default_env("spi_flash_read() failed", 0);
Andreas Fenkartc041c602017-04-08 11:59:33 +0200271 goto err_read;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200272 }
273
274 ret = env_import(buf, 1);
Simon Goldschmidt42a18202018-01-31 14:47:10 +0100275 if (!ret)
Simon Glass203e94f2017-08-03 12:21:56 -0600276 gd->env_valid = ENV_VALID;
Andreas Fenkartc041c602017-04-08 11:59:33 +0200277
278err_read:
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200279 spi_flash_free(env_flash);
280 env_flash = NULL;
Andreas Fenkartc041c602017-04-08 11:59:33 +0200281out:
282 free(buf);
Simon Glassc5951992017-08-03 12:22:17 -0600283
284 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200285}
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200286#endif
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200287
Rajesh Bhagat119c01c22018-11-05 18:01:15 +0000288#ifdef CONFIG_ENV_ADDR
289__weak void *env_sf_get_env_addr(void)
290{
291 return (void *)CONFIG_ENV_ADDR;
292}
293#endif
294
Ashish Kumarb500c922017-12-14 17:37:08 +0530295#if defined(INITENV) && defined(CONFIG_ENV_ADDR)
296static int env_sf_init(void)
297{
Rajesh Bhagat119c01c22018-11-05 18:01:15 +0000298 env_t *env_ptr = (env_t *)env_sf_get_env_addr();
Ashish Kumarb500c922017-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 Glass4415f1d2017-08-03 12:21:58 -0600312U_BOOT_ENV_LOCATION(sf) = {
313 .location = ENVL_SPI_FLASH,
Simon Glassac358be2017-08-03 12:22:03 -0600314 ENV_NAME("SPI Flash")
Simon Glasse5bce242017-08-03 12:22:01 -0600315 .load = env_sf_load,
Simon Glass4415f1d2017-08-03 12:21:58 -0600316#ifdef CMD_SAVEENV
Simon Glasse5bce242017-08-03 12:22:01 -0600317 .save = env_save_ptr(env_sf_save),
Simon Glass4415f1d2017-08-03 12:21:58 -0600318#endif
Ashish Kumarb500c922017-12-14 17:37:08 +0530319#if defined(INITENV) && defined(CONFIG_ENV_ADDR)
320 .init = env_sf_init,
321#endif
Simon Glass4415f1d2017-08-03 12:21:58 -0600322};