blob: 22b70ad3197fade01b4365623c175d3f9e592dff [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>
Simon Glass3db71102019-11-14 12:57:16 -070021#include <u-boot/crc.h>
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020022
Simon Glass4415f1d2017-08-03 12:21:58 -060023#ifndef CONFIG_SPL_BUILD
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
Igor Grinbergeb58a7f2011-11-07 01:14:08 +000028static ulong env_offset = CONFIG_ENV_OFFSET;
29static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
Stefano Babica3110f02010-10-27 11:06:20 +020030#endif /* CONFIG_ENV_OFFSET_REDUND */
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020031
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020032DECLARE_GLOBAL_DATA_PTR;
33
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020034static struct spi_flash *env_flash;
35
Andreas Fenkartafa81a72017-04-08 11:59:31 +020036static int setup_flash_device(void)
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020037{
Gong Qianyu19c31282016-01-26 15:06:42 +080038#ifdef CONFIG_DM_SPI_FLASH
39 struct udevice *new;
Andreas Fenkartafa81a72017-04-08 11:59:31 +020040 int ret;
Gong Qianyu19c31282016-01-26 15:06:42 +080041
Vignesh R96907c02016-07-06 10:04:28 +053042 /* speed and mode will be read from DT */
Gong Qianyu19c31282016-01-26 15:06:42 +080043 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
Konstantin Porotchkin25a17652018-08-29 16:34:52 +030044 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE,
45 &new);
Gong Qianyu19c31282016-01-26 15:06:42 +080046 if (ret) {
Simon Glass0ac7d722019-08-01 09:47:00 -060047 env_set_default("spi_flash_probe_bus_cs() failed", 0);
Simon Glassc5951992017-08-03 12:22:17 -060048 return ret;
Gong Qianyu19c31282016-01-26 15:06:42 +080049 }
50
51 env_flash = dev_get_uclass_priv(new);
52#else
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020053
54 if (!env_flash) {
Stefano Babica3110f02010-10-27 11:06:20 +020055 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
56 CONFIG_ENV_SPI_CS,
57 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
58 if (!env_flash) {
Simon Glass0ac7d722019-08-01 09:47:00 -060059 env_set_default("spi_flash_probe() failed", 0);
Simon Glassc5951992017-08-03 12:22:17 -060060 return -EIO;
Stefano Babica3110f02010-10-27 11:06:20 +020061 }
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020062 }
Gong Qianyu19c31282016-01-26 15:06:42 +080063#endif
Andreas Fenkartafa81a72017-04-08 11:59:31 +020064 return 0;
65}
66
67#if defined(CONFIG_ENV_OFFSET_REDUND)
Simon Glasse5bce242017-08-03 12:22:01 -060068static int env_sf_save(void)
Andreas Fenkartafa81a72017-04-08 11:59:31 +020069{
70 env_t env_new;
Simon Glassd3716dd2019-08-01 09:47:08 -060071 char *saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +020072 u32 saved_size, saved_offset, sector;
Andreas Fenkartafa81a72017-04-08 11:59:31 +020073 int ret;
74
75 ret = setup_flash_device();
76 if (ret)
77 return ret;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020078
Marek Vasut7ce15262014-03-05 19:59:50 +010079 ret = env_export(&env_new);
80 if (ret)
Simon Glassc5951992017-08-03 12:22:17 -060081 return -EIO;
Simon Glassd3716dd2019-08-01 09:47:08 -060082 env_new.flags = ENV_REDUND_ACTIVE;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020083
Simon Glass203e94f2017-08-03 12:21:56 -060084 if (gd->env_valid == ENV_VALID) {
Stefano Babica3110f02010-10-27 11:06:20 +020085 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
86 env_offset = CONFIG_ENV_OFFSET;
87 } else {
88 env_new_offset = CONFIG_ENV_OFFSET;
89 env_offset = CONFIG_ENV_OFFSET_REDUND;
90 }
91
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020092 /* Is the sector larger than the env (i.e. embedded) */
93 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
94 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
95 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
Ravi Babu7dd01742015-08-17 13:29:49 +053096 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020097 if (!saved_buffer) {
Simon Glassc5951992017-08-03 12:22:17 -060098 ret = -ENOMEM;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020099 goto done;
100 }
Heiko Schocher9ba5e5b2019-03-13 12:15:45 +0100101 ret = spi_flash_read(env_flash, saved_offset,
102 saved_size, saved_buffer);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200103 if (ret)
104 goto done;
105 }
106
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +0200107 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200108
109 puts("Erasing SPI flash...");
110 ret = spi_flash_erase(env_flash, env_new_offset,
111 sector * CONFIG_ENV_SECT_SIZE);
112 if (ret)
113 goto done;
114
115 puts("Writing to SPI flash...");
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200116
Stefano Babica3110f02010-10-27 11:06:20 +0200117 ret = spi_flash_write(env_flash, env_new_offset,
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400118 CONFIG_ENV_SIZE, &env_new);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200119 if (ret)
120 goto done;
121
122 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
123 ret = spi_flash_write(env_flash, saved_offset,
124 saved_size, saved_buffer);
125 if (ret)
126 goto done;
127 }
128
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000129 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400130 sizeof(env_new.flags), &flag);
Stefano Babica3110f02010-10-27 11:06:20 +0200131 if (ret)
132 goto done;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200133
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200134 puts("done\n");
135
Simon Glass203e94f2017-08-03 12:21:56 -0600136 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
Stefano Babica3110f02010-10-27 11:06:20 +0200137
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000138 printf("Valid environment: %d\n", (int)gd->env_valid);
Stefano Babica3110f02010-10-27 11:06:20 +0200139
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200140 done:
141 if (saved_buffer)
142 free(saved_buffer);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000143
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200144 return ret;
145}
146
Simon Glassc5951992017-08-03 12:22:17 -0600147static int env_sf_load(void)
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200148{
149 int ret;
Simon Goldschmidt80719932018-01-31 14:47:13 +0100150 int read1_fail, read2_fail;
151 env_t *tmp_env1, *tmp_env2;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200152
Ravi Babu7dd01742015-08-17 13:29:49 +0530153 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
154 CONFIG_ENV_SIZE);
155 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
156 CONFIG_ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200157 if (!tmp_env1 || !tmp_env2) {
Simon Glass0ac7d722019-08-01 09:47:00 -0600158 env_set_default("malloc() failed", 0);
Simon Glassc5951992017-08-03 12:22:17 -0600159 ret = -EIO;
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000160 goto out;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200161 }
162
Andreas Fenkart8fee8842017-04-08 11:59:32 +0200163 ret = setup_flash_device();
164 if (ret)
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000165 goto out;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200166
Heiko Schocher9ba5e5b2019-03-13 12:15:45 +0100167 read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
168 CONFIG_ENV_SIZE, tmp_env1);
169 read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
170 CONFIG_ENV_SIZE, tmp_env2);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200171
Simon Goldschmidt80719932018-01-31 14:47:13 +0100172 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
173 read2_fail);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200174
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200175 spi_flash_free(env_flash);
176 env_flash = NULL;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200177out:
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200178 free(tmp_env1);
179 free(tmp_env2);
Simon Glassc5951992017-08-03 12:22:17 -0600180
181 return ret;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200182}
183#else
Simon Glasse5bce242017-08-03 12:22:01 -0600184static int env_sf_save(void)
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200185{
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +0200186 u32 saved_size, saved_offset, sector;
Marek Vasut7ce15262014-03-05 19:59:50 +0100187 char *saved_buffer = NULL;
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000188 int ret = 1;
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400189 env_t env_new;
Gong Qianyu19c31282016-01-26 15:06:42 +0800190
Andreas Fenkartafa81a72017-04-08 11:59:31 +0200191 ret = setup_flash_device();
192 if (ret)
193 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200194
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500195 /* Is the sector larger than the env (i.e. embedded) */
196 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
197 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
198 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
199 saved_buffer = malloc(saved_size);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000200 if (!saved_buffer)
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500201 goto done;
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000202
Heiko Schocher9ba5e5b2019-03-13 12:15:45 +0100203 ret = spi_flash_read(env_flash, saved_offset,
204 saved_size, saved_buffer);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500205 if (ret)
206 goto done;
207 }
208
Marek Vasut7ce15262014-03-05 19:59:50 +0100209 ret = env_export(&env_new);
210 if (ret)
Stefano Babica3110f02010-10-27 11:06:20 +0200211 goto done;
Stefano Babica3110f02010-10-27 11:06:20 +0200212
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +0200213 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
214
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200215 puts("Erasing SPI flash...");
Stefano Babica3110f02010-10-27 11:06:20 +0200216 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
217 sector * CONFIG_ENV_SECT_SIZE);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500218 if (ret)
219 goto done;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200220
221 puts("Writing to SPI flash...");
Stefano Babica3110f02010-10-27 11:06:20 +0200222 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400223 CONFIG_ENV_SIZE, &env_new);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500224 if (ret)
225 goto done;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200226
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500227 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
Stefano Babica3110f02010-10-27 11:06:20 +0200228 ret = spi_flash_write(env_flash, saved_offset,
229 saved_size, saved_buffer);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500230 if (ret)
231 goto done;
232 }
233
234 ret = 0;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200235 puts("done\n");
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500236
237 done:
238 if (saved_buffer)
239 free(saved_buffer);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000240
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500241 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200242}
243
Simon Glassc5951992017-08-03 12:22:17 -0600244static int env_sf_load(void)
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200245{
246 int ret;
Ying Zhang5a89fa92014-01-24 15:50:07 +0800247 char *buf = NULL;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200248
Ravi Babu7dd01742015-08-17 13:29:49 +0530249 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
Andreas Fenkartc041c602017-04-08 11:59:33 +0200250 if (!buf) {
Simon Glass0ac7d722019-08-01 09:47:00 -0600251 env_set_default("malloc() failed", 0);
Simon Glassc5951992017-08-03 12:22:17 -0600252 return -EIO;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200253 }
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200254
Andreas Fenkartc041c602017-04-08 11:59:33 +0200255 ret = setup_flash_device();
256 if (ret)
257 goto out;
258
Heiko Schocher9ba5e5b2019-03-13 12:15:45 +0100259 ret = spi_flash_read(env_flash,
260 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200261 if (ret) {
Simon Glass0ac7d722019-08-01 09:47:00 -0600262 env_set_default("spi_flash_read() failed", 0);
Andreas Fenkartc041c602017-04-08 11:59:33 +0200263 goto err_read;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200264 }
265
266 ret = env_import(buf, 1);
Simon Goldschmidt42a18202018-01-31 14:47:10 +0100267 if (!ret)
Simon Glass203e94f2017-08-03 12:21:56 -0600268 gd->env_valid = ENV_VALID;
Andreas Fenkartc041c602017-04-08 11:59:33 +0200269
270err_read:
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200271 spi_flash_free(env_flash);
272 env_flash = NULL;
Andreas Fenkartc041c602017-04-08 11:59:33 +0200273out:
274 free(buf);
Simon Glassc5951992017-08-03 12:22:17 -0600275
276 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200277}
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200278#endif
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200279
Tom Rinia09fea12019-11-18 20:02:10 -0500280#if CONFIG_ENV_ADDR != 0x0
Rajesh Bhagat119c01c22018-11-05 18:01:15 +0000281__weak void *env_sf_get_env_addr(void)
282{
283 return (void *)CONFIG_ENV_ADDR;
284}
285#endif
286
Tom Rinia09fea12019-11-18 20:02:10 -0500287#if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0)
Ashish Kumarb500c922017-12-14 17:37:08 +0530288static int env_sf_init(void)
289{
Rajesh Bhagat119c01c22018-11-05 18:01:15 +0000290 env_t *env_ptr = (env_t *)env_sf_get_env_addr();
Ashish Kumarb500c922017-12-14 17:37:08 +0530291
292 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
293 gd->env_addr = (ulong)&(env_ptr->data);
294 gd->env_valid = 1;
295 } else {
296 gd->env_addr = (ulong)&default_environment[0];
297 gd->env_valid = 1;
298 }
299
300 return 0;
301}
302#endif
303
Simon Glass4415f1d2017-08-03 12:21:58 -0600304U_BOOT_ENV_LOCATION(sf) = {
305 .location = ENVL_SPI_FLASH,
Simon Glassac358be2017-08-03 12:22:03 -0600306 ENV_NAME("SPI Flash")
Simon Glasse5bce242017-08-03 12:22:01 -0600307 .load = env_sf_load,
Rasmus Villemoes9e3c94d2020-02-19 09:47:43 +0000308 .save = ENV_SAVE_PTR(env_sf_save),
Tom Rinia09fea12019-11-18 20:02:10 -0500309#if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0)
Ashish Kumarb500c922017-12-14 17:37:08 +0530310 .init = env_sf_init,
311#endif
Simon Glass4415f1d2017-08-03 12:21:58 -0600312};