blob: 23cbad5d8885c54168237dc8fcb14b719faeb4ad [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
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020021#ifndef CONFIG_ENV_SPI_BUS
Jean-Jacques Hiblot2e4e5ad2017-02-01 14:51:47 +010022# define CONFIG_ENV_SPI_BUS CONFIG_SF_DEFAULT_BUS
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020023#endif
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020024#ifndef CONFIG_ENV_SPI_CS
Jean-Jacques Hiblot2e4e5ad2017-02-01 14:51:47 +010025# define CONFIG_ENV_SPI_CS CONFIG_SF_DEFAULT_CS
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020026#endif
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020027#ifndef CONFIG_ENV_SPI_MAX_HZ
Jean-Jacques Hiblot2e4e5ad2017-02-01 14:51:47 +010028# define CONFIG_ENV_SPI_MAX_HZ CONFIG_SF_DEFAULT_SPEED
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020029#endif
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020030#ifndef CONFIG_ENV_SPI_MODE
Jean-Jacques Hiblot2e4e5ad2017-02-01 14:51:47 +010031# define CONFIG_ENV_SPI_MODE CONFIG_SF_DEFAULT_MODE
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020032#endif
33
Simon Glass4415f1d2017-08-03 12:21:58 -060034#ifndef CONFIG_SPL_BUILD
35#define CMD_SAVEENV
Ashish Kumarb500c922017-12-14 17:37:08 +053036#define INITENV
Simon Glass4415f1d2017-08-03 12:21:58 -060037#endif
38
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020039#ifdef CONFIG_ENV_OFFSET_REDUND
Simon Glass4415f1d2017-08-03 12:21:58 -060040#ifdef CMD_SAVEENV
Igor Grinbergeb58a7f2011-11-07 01:14:08 +000041static ulong env_offset = CONFIG_ENV_OFFSET;
42static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
Simon Glass4415f1d2017-08-03 12:21:58 -060043#endif
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020044
Igor Grinbergeb58a7f2011-11-07 01:14:08 +000045#define ACTIVE_FLAG 1
46#define OBSOLETE_FLAG 0
Stefano Babica3110f02010-10-27 11:06:20 +020047#endif /* CONFIG_ENV_OFFSET_REDUND */
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020048
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020049DECLARE_GLOBAL_DATA_PTR;
50
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020051static struct spi_flash *env_flash;
52
Andreas Fenkartafa81a72017-04-08 11:59:31 +020053static int setup_flash_device(void)
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020054{
Gong Qianyu19c31282016-01-26 15:06:42 +080055#ifdef CONFIG_DM_SPI_FLASH
56 struct udevice *new;
Andreas Fenkartafa81a72017-04-08 11:59:31 +020057 int ret;
Gong Qianyu19c31282016-01-26 15:06:42 +080058
Vignesh R96907c02016-07-06 10:04:28 +053059 /* speed and mode will be read from DT */
Gong Qianyu19c31282016-01-26 15:06:42 +080060 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
Konstantin Porotchkin25a17652018-08-29 16:34:52 +030061 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE,
62 &new);
Gong Qianyu19c31282016-01-26 15:06:42 +080063 if (ret) {
Yaniv Levinskyc5d548a2018-06-24 19:16:57 +030064 set_default_env("spi_flash_probe_bus_cs() failed", 0);
Simon Glassc5951992017-08-03 12:22:17 -060065 return ret;
Gong Qianyu19c31282016-01-26 15:06:42 +080066 }
67
68 env_flash = dev_get_uclass_priv(new);
69#else
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020070
71 if (!env_flash) {
Stefano Babica3110f02010-10-27 11:06:20 +020072 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
73 CONFIG_ENV_SPI_CS,
74 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
75 if (!env_flash) {
Yaniv Levinskyc5d548a2018-06-24 19:16:57 +030076 set_default_env("spi_flash_probe() failed", 0);
Simon Glassc5951992017-08-03 12:22:17 -060077 return -EIO;
Stefano Babica3110f02010-10-27 11:06:20 +020078 }
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020079 }
Gong Qianyu19c31282016-01-26 15:06:42 +080080#endif
Andreas Fenkartafa81a72017-04-08 11:59:31 +020081 return 0;
82}
83
84#if defined(CONFIG_ENV_OFFSET_REDUND)
Simon Glass4415f1d2017-08-03 12:21:58 -060085#ifdef CMD_SAVEENV
Simon Glasse5bce242017-08-03 12:22:01 -060086static int env_sf_save(void)
Andreas Fenkartafa81a72017-04-08 11:59:31 +020087{
88 env_t env_new;
89 char *saved_buffer = NULL, flag = OBSOLETE_FLAG;
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +020090 u32 saved_size, saved_offset, sector;
Andreas Fenkartafa81a72017-04-08 11:59:31 +020091 int ret;
92
93 ret = setup_flash_device();
94 if (ret)
95 return ret;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020096
Marek Vasut7ce15262014-03-05 19:59:50 +010097 ret = env_export(&env_new);
98 if (ret)
Simon Glassc5951992017-08-03 12:22:17 -060099 return -EIO;
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400100 env_new.flags = ACTIVE_FLAG;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200101
Simon Glass203e94f2017-08-03 12:21:56 -0600102 if (gd->env_valid == ENV_VALID) {
Stefano Babica3110f02010-10-27 11:06:20 +0200103 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
104 env_offset = CONFIG_ENV_OFFSET;
105 } else {
106 env_new_offset = CONFIG_ENV_OFFSET;
107 env_offset = CONFIG_ENV_OFFSET_REDUND;
108 }
109
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200110 /* Is the sector larger than the env (i.e. embedded) */
111 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
112 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
113 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
Ravi Babu7dd01742015-08-17 13:29:49 +0530114 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200115 if (!saved_buffer) {
Simon Glassc5951992017-08-03 12:22:17 -0600116 ret = -ENOMEM;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200117 goto done;
118 }
119 ret = spi_flash_read(env_flash, saved_offset,
120 saved_size, saved_buffer);
121 if (ret)
122 goto done;
123 }
124
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +0200125 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200126
127 puts("Erasing SPI flash...");
128 ret = spi_flash_erase(env_flash, env_new_offset,
129 sector * CONFIG_ENV_SECT_SIZE);
130 if (ret)
131 goto done;
132
133 puts("Writing to SPI flash...");
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200134
Stefano Babica3110f02010-10-27 11:06:20 +0200135 ret = spi_flash_write(env_flash, env_new_offset,
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400136 CONFIG_ENV_SIZE, &env_new);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200137 if (ret)
138 goto done;
139
140 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
141 ret = spi_flash_write(env_flash, saved_offset,
142 saved_size, saved_buffer);
143 if (ret)
144 goto done;
145 }
146
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000147 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400148 sizeof(env_new.flags), &flag);
Stefano Babica3110f02010-10-27 11:06:20 +0200149 if (ret)
150 goto done;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200151
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200152 puts("done\n");
153
Simon Glass203e94f2017-08-03 12:21:56 -0600154 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
Stefano Babica3110f02010-10-27 11:06:20 +0200155
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000156 printf("Valid environment: %d\n", (int)gd->env_valid);
Stefano Babica3110f02010-10-27 11:06:20 +0200157
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200158 done:
159 if (saved_buffer)
160 free(saved_buffer);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000161
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200162 return ret;
163}
Simon Glass4415f1d2017-08-03 12:21:58 -0600164#endif /* CMD_SAVEENV */
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200165
Simon Glassc5951992017-08-03 12:22:17 -0600166static int env_sf_load(void)
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200167{
168 int ret;
Simon Goldschmidt80719932018-01-31 14:47:13 +0100169 int read1_fail, read2_fail;
170 env_t *tmp_env1, *tmp_env2;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200171
Ravi Babu7dd01742015-08-17 13:29:49 +0530172 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
173 CONFIG_ENV_SIZE);
174 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
175 CONFIG_ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200176 if (!tmp_env1 || !tmp_env2) {
Yaniv Levinskyc5d548a2018-06-24 19:16:57 +0300177 set_default_env("malloc() failed", 0);
Simon Glassc5951992017-08-03 12:22:17 -0600178 ret = -EIO;
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000179 goto out;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200180 }
181
Andreas Fenkart8fee8842017-04-08 11:59:32 +0200182 ret = setup_flash_device();
183 if (ret)
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000184 goto out;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200185
Simon Goldschmidt80719932018-01-31 14:47:13 +0100186 read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
187 CONFIG_ENV_SIZE, tmp_env1);
188 read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
189 CONFIG_ENV_SIZE, tmp_env2);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200190
Simon Goldschmidt80719932018-01-31 14:47:13 +0100191 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
192 read2_fail);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200193
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200194 spi_flash_free(env_flash);
195 env_flash = NULL;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200196out:
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200197 free(tmp_env1);
198 free(tmp_env2);
Simon Glassc5951992017-08-03 12:22:17 -0600199
200 return ret;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200201}
202#else
Simon Glass4415f1d2017-08-03 12:21:58 -0600203#ifdef CMD_SAVEENV
Simon Glasse5bce242017-08-03 12:22:01 -0600204static int env_sf_save(void)
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200205{
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +0200206 u32 saved_size, saved_offset, sector;
Marek Vasut7ce15262014-03-05 19:59:50 +0100207 char *saved_buffer = NULL;
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000208 int ret = 1;
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400209 env_t env_new;
Gong Qianyu19c31282016-01-26 15:06:42 +0800210
Andreas Fenkartafa81a72017-04-08 11:59:31 +0200211 ret = setup_flash_device();
212 if (ret)
213 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200214
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500215 /* Is the sector larger than the env (i.e. embedded) */
216 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
217 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
218 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
219 saved_buffer = malloc(saved_size);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000220 if (!saved_buffer)
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500221 goto done;
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000222
Stefano Babica3110f02010-10-27 11:06:20 +0200223 ret = spi_flash_read(env_flash, saved_offset,
224 saved_size, saved_buffer);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500225 if (ret)
226 goto done;
227 }
228
Marek Vasut7ce15262014-03-05 19:59:50 +0100229 ret = env_export(&env_new);
230 if (ret)
Stefano Babica3110f02010-10-27 11:06:20 +0200231 goto done;
Stefano Babica3110f02010-10-27 11:06:20 +0200232
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +0200233 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
234
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200235 puts("Erasing SPI flash...");
Stefano Babica3110f02010-10-27 11:06:20 +0200236 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
237 sector * CONFIG_ENV_SECT_SIZE);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500238 if (ret)
239 goto done;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200240
241 puts("Writing to SPI flash...");
Stefano Babica3110f02010-10-27 11:06:20 +0200242 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400243 CONFIG_ENV_SIZE, &env_new);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500244 if (ret)
245 goto done;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200246
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500247 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
Stefano Babica3110f02010-10-27 11:06:20 +0200248 ret = spi_flash_write(env_flash, saved_offset,
249 saved_size, saved_buffer);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500250 if (ret)
251 goto done;
252 }
253
254 ret = 0;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200255 puts("done\n");
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500256
257 done:
258 if (saved_buffer)
259 free(saved_buffer);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000260
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500261 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200262}
Simon Glass4415f1d2017-08-03 12:21:58 -0600263#endif /* CMD_SAVEENV */
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200264
Simon Glassc5951992017-08-03 12:22:17 -0600265static int env_sf_load(void)
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200266{
267 int ret;
Ying Zhang5a89fa92014-01-24 15:50:07 +0800268 char *buf = NULL;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200269
Ravi Babu7dd01742015-08-17 13:29:49 +0530270 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
Andreas Fenkartc041c602017-04-08 11:59:33 +0200271 if (!buf) {
Yaniv Levinskyc5d548a2018-06-24 19:16:57 +0300272 set_default_env("malloc() failed", 0);
Simon Glassc5951992017-08-03 12:22:17 -0600273 return -EIO;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200274 }
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200275
Andreas Fenkartc041c602017-04-08 11:59:33 +0200276 ret = setup_flash_device();
277 if (ret)
278 goto out;
279
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200280 ret = spi_flash_read(env_flash,
281 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
282 if (ret) {
Yaniv Levinskyc5d548a2018-06-24 19:16:57 +0300283 set_default_env("spi_flash_read() failed", 0);
Andreas Fenkartc041c602017-04-08 11:59:33 +0200284 goto err_read;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200285 }
286
287 ret = env_import(buf, 1);
Simon Goldschmidt42a18202018-01-31 14:47:10 +0100288 if (!ret)
Simon Glass203e94f2017-08-03 12:21:56 -0600289 gd->env_valid = ENV_VALID;
Andreas Fenkartc041c602017-04-08 11:59:33 +0200290
291err_read:
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200292 spi_flash_free(env_flash);
293 env_flash = NULL;
Andreas Fenkartc041c602017-04-08 11:59:33 +0200294out:
295 free(buf);
Simon Glassc5951992017-08-03 12:22:17 -0600296
297 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200298}
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200299#endif
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200300
Rajesh Bhagat119c01c22018-11-05 18:01:15 +0000301#ifdef CONFIG_ENV_ADDR
302__weak void *env_sf_get_env_addr(void)
303{
304 return (void *)CONFIG_ENV_ADDR;
305}
306#endif
307
Ashish Kumarb500c922017-12-14 17:37:08 +0530308#if defined(INITENV) && defined(CONFIG_ENV_ADDR)
309static int env_sf_init(void)
310{
Rajesh Bhagat119c01c22018-11-05 18:01:15 +0000311 env_t *env_ptr = (env_t *)env_sf_get_env_addr();
Ashish Kumarb500c922017-12-14 17:37:08 +0530312
313 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
314 gd->env_addr = (ulong)&(env_ptr->data);
315 gd->env_valid = 1;
316 } else {
317 gd->env_addr = (ulong)&default_environment[0];
318 gd->env_valid = 1;
319 }
320
321 return 0;
322}
323#endif
324
Simon Glass4415f1d2017-08-03 12:21:58 -0600325U_BOOT_ENV_LOCATION(sf) = {
326 .location = ENVL_SPI_FLASH,
Simon Glassac358be2017-08-03 12:22:03 -0600327 ENV_NAME("SPI Flash")
Simon Glasse5bce242017-08-03 12:22:01 -0600328 .load = env_sf_load,
Simon Glass4415f1d2017-08-03 12:21:58 -0600329#ifdef CMD_SAVEENV
Simon Glasse5bce242017-08-03 12:22:01 -0600330 .save = env_save_ptr(env_sf_save),
Simon Glass4415f1d2017-08-03 12:21:58 -0600331#endif
Ashish Kumarb500c922017-12-14 17:37:08 +0530332#if defined(INITENV) && defined(CONFIG_ENV_ADDR)
333 .init = env_sf_init,
334#endif
Simon Glass4415f1d2017-08-03 12:21:58 -0600335};