blob: 9944602367b9a8ae7e51533c3ff5447a7720413e [file] [log] [blame]
Haavard Skinnemoen8c664972008-05-16 11:10:35 +02001/*
Wolfgang Denkea882ba2010-06-20 23:33:59 +02002 * (C) Copyright 2000-2010
Haavard Skinnemoen8c664972008-05-16 11:10:35 +02003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6 * Andreas Heppel <aheppel@sysgo.de>
7 *
8 * (C) Copyright 2008 Atmel Corporation
9 *
Jagannadha Sutradharudu Teki402ba1e2013-07-29 22:12:52 +053010 * SPDX-License-Identifier: GPL-2.0+
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020011 */
12#include <common.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
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020034#ifdef CONFIG_ENV_OFFSET_REDUND
Igor Grinbergeb58a7f2011-11-07 01:14:08 +000035static ulong env_offset = CONFIG_ENV_OFFSET;
36static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020037
Igor Grinbergeb58a7f2011-11-07 01:14:08 +000038#define ACTIVE_FLAG 1
39#define OBSOLETE_FLAG 0
Stefano Babica3110f02010-10-27 11:06:20 +020040#endif /* CONFIG_ENV_OFFSET_REDUND */
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020041
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020042DECLARE_GLOBAL_DATA_PTR;
43
Igor Grinbergeb58a7f2011-11-07 01:14:08 +000044char *env_name_spec = "SPI Flash";
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020045
46static struct spi_flash *env_flash;
47
Andreas Fenkartafa81a72017-04-08 11:59:31 +020048static int setup_flash_device(void)
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020049{
Gong Qianyu19c31282016-01-26 15:06:42 +080050#ifdef CONFIG_DM_SPI_FLASH
51 struct udevice *new;
Andreas Fenkartafa81a72017-04-08 11:59:31 +020052 int ret;
Gong Qianyu19c31282016-01-26 15:06:42 +080053
Vignesh R96907c02016-07-06 10:04:28 +053054 /* speed and mode will be read from DT */
Gong Qianyu19c31282016-01-26 15:06:42 +080055 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
Vignesh R96907c02016-07-06 10:04:28 +053056 0, 0, &new);
Gong Qianyu19c31282016-01-26 15:06:42 +080057 if (ret) {
58 set_default_env("!spi_flash_probe_bus_cs() failed");
59 return 1;
60 }
61
62 env_flash = dev_get_uclass_priv(new);
63#else
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020064
65 if (!env_flash) {
Stefano Babica3110f02010-10-27 11:06:20 +020066 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
67 CONFIG_ENV_SPI_CS,
68 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
69 if (!env_flash) {
70 set_default_env("!spi_flash_probe() failed");
71 return 1;
72 }
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020073 }
Gong Qianyu19c31282016-01-26 15:06:42 +080074#endif
Andreas Fenkartafa81a72017-04-08 11:59:31 +020075 return 0;
76}
77
78#if defined(CONFIG_ENV_OFFSET_REDUND)
79int saveenv(void)
80{
81 env_t env_new;
82 char *saved_buffer = NULL, flag = OBSOLETE_FLAG;
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +020083 u32 saved_size, saved_offset, sector;
Andreas Fenkartafa81a72017-04-08 11:59:31 +020084 int ret;
85
86 ret = setup_flash_device();
87 if (ret)
88 return ret;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020089
Marek Vasut7ce15262014-03-05 19:59:50 +010090 ret = env_export(&env_new);
91 if (ret)
92 return ret;
Tom Rinicd0f4fa2013-04-05 14:55:21 -040093 env_new.flags = ACTIVE_FLAG;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020094
Stefano Babica3110f02010-10-27 11:06:20 +020095 if (gd->env_valid == 1) {
96 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
97 env_offset = CONFIG_ENV_OFFSET;
98 } else {
99 env_new_offset = CONFIG_ENV_OFFSET;
100 env_offset = CONFIG_ENV_OFFSET_REDUND;
101 }
102
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200103 /* Is the sector larger than the env (i.e. embedded) */
104 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
105 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
106 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
Ravi Babu7dd01742015-08-17 13:29:49 +0530107 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200108 if (!saved_buffer) {
109 ret = 1;
110 goto done;
111 }
112 ret = spi_flash_read(env_flash, saved_offset,
113 saved_size, saved_buffer);
114 if (ret)
115 goto done;
116 }
117
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +0200118 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200119
120 puts("Erasing SPI flash...");
121 ret = spi_flash_erase(env_flash, env_new_offset,
122 sector * CONFIG_ENV_SECT_SIZE);
123 if (ret)
124 goto done;
125
126 puts("Writing to SPI flash...");
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200127
Stefano Babica3110f02010-10-27 11:06:20 +0200128 ret = spi_flash_write(env_flash, env_new_offset,
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400129 CONFIG_ENV_SIZE, &env_new);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200130 if (ret)
131 goto done;
132
133 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
134 ret = spi_flash_write(env_flash, saved_offset,
135 saved_size, saved_buffer);
136 if (ret)
137 goto done;
138 }
139
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000140 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400141 sizeof(env_new.flags), &flag);
Stefano Babica3110f02010-10-27 11:06:20 +0200142 if (ret)
143 goto done;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200144
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200145 puts("done\n");
146
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000147 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
Stefano Babica3110f02010-10-27 11:06:20 +0200148
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000149 printf("Valid environment: %d\n", (int)gd->env_valid);
Stefano Babica3110f02010-10-27 11:06:20 +0200150
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200151 done:
152 if (saved_buffer)
153 free(saved_buffer);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000154
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200155 return ret;
156}
157
158void env_relocate_spec(void)
159{
160 int ret;
161 int crc1_ok = 0, crc2_ok = 0;
162 env_t *tmp_env1 = NULL;
163 env_t *tmp_env2 = NULL;
Stefano Babica3110f02010-10-27 11:06:20 +0200164 env_t *ep = NULL;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200165
Ravi Babu7dd01742015-08-17 13:29:49 +0530166 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
167 CONFIG_ENV_SIZE);
168 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
169 CONFIG_ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200170 if (!tmp_env1 || !tmp_env2) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200171 set_default_env("!malloc() failed");
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000172 goto out;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200173 }
174
Andreas Fenkart8fee8842017-04-08 11:59:32 +0200175 ret = setup_flash_device();
176 if (ret)
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000177 goto out;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200178
179 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
180 CONFIG_ENV_SIZE, tmp_env1);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200181 if (ret) {
182 set_default_env("!spi_flash_read() failed");
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200183 goto err_read;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200184 }
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200185
186 if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc)
187 crc1_ok = 1;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200188
189 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
190 CONFIG_ENV_SIZE, tmp_env2);
191 if (!ret) {
192 if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc)
193 crc2_ok = 1;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200194 }
195
Stefano Babica3110f02010-10-27 11:06:20 +0200196 if (!crc1_ok && !crc2_ok) {
Stefano Babica3110f02010-10-27 11:06:20 +0200197 set_default_env("!bad CRC");
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000198 goto err_read;
Stefano Babica3110f02010-10-27 11:06:20 +0200199 } else if (crc1_ok && !crc2_ok) {
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200200 gd->env_valid = 1;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200201 } else if (!crc1_ok && crc2_ok) {
Stefano Babica3110f02010-10-27 11:06:20 +0200202 gd->env_valid = 2;
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000203 } else if (tmp_env1->flags == ACTIVE_FLAG &&
204 tmp_env2->flags == OBSOLETE_FLAG) {
205 gd->env_valid = 1;
206 } else if (tmp_env1->flags == OBSOLETE_FLAG &&
207 tmp_env2->flags == ACTIVE_FLAG) {
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200208 gd->env_valid = 2;
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000209 } else if (tmp_env1->flags == tmp_env2->flags) {
Mario Schuknecht5e8397d2015-03-24 08:59:00 +0100210 gd->env_valid = 1;
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000211 } else if (tmp_env1->flags == 0xFF) {
Mario Schuknecht5e8397d2015-03-24 08:59:00 +0100212 gd->env_valid = 1;
213 } else if (tmp_env2->flags == 0xFF) {
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200214 gd->env_valid = 2;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200215 } else {
216 /*
217 * this differs from code in env_flash.c, but I think a sane
218 * default path is desirable.
219 */
Mario Schuknecht5e8397d2015-03-24 08:59:00 +0100220 gd->env_valid = 1;
Stefano Babica3110f02010-10-27 11:06:20 +0200221 }
222
Stefano Babica3110f02010-10-27 11:06:20 +0200223 if (gd->env_valid == 1)
224 ep = tmp_env1;
225 else
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200226 ep = tmp_env2;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200227
Stefano Babica3110f02010-10-27 11:06:20 +0200228 ret = env_import((char *)ep, 0);
229 if (!ret) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200230 error("Cannot import environment: errno = %d\n", errno);
Peng Fan9a5d9932016-03-28 17:26:27 +0800231 set_default_env("!env_import failed");
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200232 }
233
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200234err_read:
235 spi_flash_free(env_flash);
236 env_flash = NULL;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200237out:
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200238 free(tmp_env1);
239 free(tmp_env2);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200240}
241#else
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200242int saveenv(void)
243{
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +0200244 u32 saved_size, saved_offset, sector;
Marek Vasut7ce15262014-03-05 19:59:50 +0100245 char *saved_buffer = NULL;
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000246 int ret = 1;
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400247 env_t env_new;
Gong Qianyu19c31282016-01-26 15:06:42 +0800248
Andreas Fenkartafa81a72017-04-08 11:59:31 +0200249 ret = setup_flash_device();
250 if (ret)
251 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200252
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500253 /* Is the sector larger than the env (i.e. embedded) */
254 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
255 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
256 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
257 saved_buffer = malloc(saved_size);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000258 if (!saved_buffer)
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500259 goto done;
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000260
Stefano Babica3110f02010-10-27 11:06:20 +0200261 ret = spi_flash_read(env_flash, saved_offset,
262 saved_size, saved_buffer);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500263 if (ret)
264 goto done;
265 }
266
Marek Vasut7ce15262014-03-05 19:59:50 +0100267 ret = env_export(&env_new);
268 if (ret)
Stefano Babica3110f02010-10-27 11:06:20 +0200269 goto done;
Stefano Babica3110f02010-10-27 11:06:20 +0200270
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +0200271 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
272
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200273 puts("Erasing SPI flash...");
Stefano Babica3110f02010-10-27 11:06:20 +0200274 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
275 sector * CONFIG_ENV_SECT_SIZE);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500276 if (ret)
277 goto done;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200278
279 puts("Writing to SPI flash...");
Stefano Babica3110f02010-10-27 11:06:20 +0200280 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400281 CONFIG_ENV_SIZE, &env_new);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500282 if (ret)
283 goto done;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200284
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500285 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
Stefano Babica3110f02010-10-27 11:06:20 +0200286 ret = spi_flash_write(env_flash, saved_offset,
287 saved_size, saved_buffer);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500288 if (ret)
289 goto done;
290 }
291
292 ret = 0;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200293 puts("done\n");
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500294
295 done:
296 if (saved_buffer)
297 free(saved_buffer);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000298
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500299 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200300}
301
302void env_relocate_spec(void)
303{
304 int ret;
Ying Zhang5a89fa92014-01-24 15:50:07 +0800305 char *buf = NULL;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200306
Ravi Babu7dd01742015-08-17 13:29:49 +0530307 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
Andreas Fenkartc041c602017-04-08 11:59:33 +0200308 if (!buf) {
309 set_default_env("!malloc() failed");
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200310 return;
311 }
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200312
Andreas Fenkartc041c602017-04-08 11:59:33 +0200313 ret = setup_flash_device();
314 if (ret)
315 goto out;
316
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200317 ret = spi_flash_read(env_flash,
318 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
319 if (ret) {
320 set_default_env("!spi_flash_read() failed");
Andreas Fenkartc041c602017-04-08 11:59:33 +0200321 goto err_read;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200322 }
323
324 ret = env_import(buf, 1);
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200325 if (ret)
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200326 gd->env_valid = 1;
Andreas Fenkartc041c602017-04-08 11:59:33 +0200327
328err_read:
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200329 spi_flash_free(env_flash);
330 env_flash = NULL;
Andreas Fenkartc041c602017-04-08 11:59:33 +0200331out:
332 free(buf);
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200333}
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200334#endif
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200335
336int env_init(void)
337{
338 /* SPI flash isn't usable before relocation */
339 gd->env_addr = (ulong)&default_environment[0];
340 gd->env_valid = 1;
341
342 return 0;
343}