blob: 9944602367b9a8ae7e51533c3ff5447a7720413e [file] [log] [blame]
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +02001/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02002 * (C) Copyright 2000-2010
Haavard Skinnemoeneb8951c2008-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 Tekib1df9222013-07-29 22:12:52 +053010 * SPDX-License-Identifier: GPL-2.0+
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020011 */
12#include <common.h>
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020013#include <environment.h>
Mike Frysinger9af10d52008-12-11 06:23:37 -050014#include <malloc.h>
Simon Glassdef92152014-10-13 23:41:55 -060015#include <spi.h>
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020016#include <spi_flash.h>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020017#include <search.h>
18#include <errno.h>
Gong Qianyu6d85a602016-01-26 15:06:42 +080019#include <dm/device-internal.h>
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020020
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020021#ifndef CONFIG_ENV_SPI_BUS
Jean-Jacques Hiblot464eee12017-02-01 14:51:47 +010022# define CONFIG_ENV_SPI_BUS CONFIG_SF_DEFAULT_BUS
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020023#endif
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020024#ifndef CONFIG_ENV_SPI_CS
Jean-Jacques Hiblot464eee12017-02-01 14:51:47 +010025# define CONFIG_ENV_SPI_CS CONFIG_SF_DEFAULT_CS
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020026#endif
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020027#ifndef CONFIG_ENV_SPI_MAX_HZ
Jean-Jacques Hiblot464eee12017-02-01 14:51:47 +010028# define CONFIG_ENV_SPI_MAX_HZ CONFIG_SF_DEFAULT_SPEED
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020029#endif
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020030#ifndef CONFIG_ENV_SPI_MODE
Jean-Jacques Hiblot464eee12017-02-01 14:51:47 +010031# define CONFIG_ENV_SPI_MODE CONFIG_SF_DEFAULT_MODE
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020032#endif
33
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020034#ifdef CONFIG_ENV_OFFSET_REDUND
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000035static ulong env_offset = CONFIG_ENV_OFFSET;
36static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020037
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000038#define ACTIVE_FLAG 1
39#define OBSOLETE_FLAG 0
Stefano Babic5f6c2862010-10-27 11:06:20 +020040#endif /* CONFIG_ENV_OFFSET_REDUND */
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020041
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020042DECLARE_GLOBAL_DATA_PTR;
43
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000044char *env_name_spec = "SPI Flash";
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020045
46static struct spi_flash *env_flash;
47
Andreas Fenkart52b94442017-04-08 11:59:31 +020048static int setup_flash_device(void)
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020049{
Gong Qianyu6d85a602016-01-26 15:06:42 +080050#ifdef CONFIG_DM_SPI_FLASH
51 struct udevice *new;
Andreas Fenkart52b94442017-04-08 11:59:31 +020052 int ret;
Gong Qianyu6d85a602016-01-26 15:06:42 +080053
Vignesh Rae569792016-07-06 10:04:28 +053054 /* speed and mode will be read from DT */
Gong Qianyu6d85a602016-01-26 15:06:42 +080055 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
Vignesh Rae569792016-07-06 10:04:28 +053056 0, 0, &new);
Gong Qianyu6d85a602016-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 Wegner0cae3d42010-04-23 17:22:55 +020064
65 if (!env_flash) {
Stefano Babic5f6c2862010-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 Wegner0cae3d42010-04-23 17:22:55 +020073 }
Gong Qianyu6d85a602016-01-26 15:06:42 +080074#endif
Andreas Fenkart52b94442017-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 Fenkartdf701912017-04-08 11:59:34 +020083 u32 saved_size, saved_offset, sector;
Andreas Fenkart52b94442017-04-08 11:59:31 +020084 int ret;
85
86 ret = setup_flash_device();
87 if (ret)
88 return ret;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020089
Marek Vasutd73c1292014-03-05 19:59:50 +010090 ret = env_export(&env_new);
91 if (ret)
92 return ret;
Tom Rinia840b8e2013-04-05 14:55:21 -040093 env_new.flags = ACTIVE_FLAG;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020094
Stefano Babic5f6c2862010-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 Wegner0cae3d42010-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 Babu20d93fc2015-08-17 13:29:49 +0530107 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
Wolfgang Wegner0cae3d42010-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 Fenkartdf701912017-04-08 11:59:34 +0200118 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
Wolfgang Wegner0cae3d42010-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 Wegner0cae3d42010-04-23 17:22:55 +0200127
Stefano Babic5f6c2862010-10-27 11:06:20 +0200128 ret = spi_flash_write(env_flash, env_new_offset,
Tom Rinia840b8e2013-04-05 14:55:21 -0400129 CONFIG_ENV_SIZE, &env_new);
Wolfgang Wegner0cae3d42010-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 Grinbergc3c1bed2011-11-07 01:14:08 +0000140 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
Tom Rinia840b8e2013-04-05 14:55:21 -0400141 sizeof(env_new.flags), &flag);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200142 if (ret)
143 goto done;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200144
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200145 puts("done\n");
146
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000147 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200148
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000149 printf("Valid environment: %d\n", (int)gd->env_valid);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200150
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200151 done:
152 if (saved_buffer)
153 free(saved_buffer);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000154
Wolfgang Wegner0cae3d42010-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 Babic5f6c2862010-10-27 11:06:20 +0200164 env_t *ep = NULL;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200165
Ravi Babu20d93fc2015-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 Denk460a9ff2010-06-20 23:33:59 +0200170 if (!tmp_env1 || !tmp_env2) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200171 set_default_env("!malloc() failed");
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000172 goto out;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200173 }
174
Andreas Fenkarta2d18832017-04-08 11:59:32 +0200175 ret = setup_flash_device();
176 if (ret)
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000177 goto out;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200178
179 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
180 CONFIG_ENV_SIZE, tmp_env1);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200181 if (ret) {
182 set_default_env("!spi_flash_read() failed");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200183 goto err_read;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200184 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200185
186 if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc)
187 crc1_ok = 1;
Wolfgang Wegner0cae3d42010-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 Wegner0cae3d42010-04-23 17:22:55 +0200194 }
195
Stefano Babic5f6c2862010-10-27 11:06:20 +0200196 if (!crc1_ok && !crc2_ok) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200197 set_default_env("!bad CRC");
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000198 goto err_read;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200199 } else if (crc1_ok && !crc2_ok) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200200 gd->env_valid = 1;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200201 } else if (!crc1_ok && crc2_ok) {
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000202 gd->env_valid = 2;
203 } else if (tmp_env1->flags == ACTIVE_FLAG &&
204 tmp_env2->flags == OBSOLETE_FLAG) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200205 gd->env_valid = 1;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000206 } else if (tmp_env1->flags == OBSOLETE_FLAG &&
207 tmp_env2->flags == ACTIVE_FLAG) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200208 gd->env_valid = 2;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000209 } else if (tmp_env1->flags == tmp_env2->flags) {
Mario Schuknecht12f1a542015-03-24 08:59:00 +0100210 gd->env_valid = 1;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000211 } else if (tmp_env1->flags == 0xFF) {
Mario Schuknecht12f1a542015-03-24 08:59:00 +0100212 gd->env_valid = 1;
213 } else if (tmp_env2->flags == 0xFF) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200214 gd->env_valid = 2;
Wolfgang Wegner0cae3d42010-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 Schuknecht12f1a542015-03-24 08:59:00 +0100220 gd->env_valid = 1;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200221 }
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200222
Stefano Babic5f6c2862010-10-27 11:06:20 +0200223 if (gd->env_valid == 1)
224 ep = tmp_env1;
225 else
226 ep = tmp_env2;
227
228 ret = env_import((char *)ep, 0);
229 if (!ret) {
230 error("Cannot import environment: errno = %d\n", errno);
Peng Fan3e21c722016-03-28 17:26:27 +0800231 set_default_env("!env_import failed");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200232 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200233
234err_read:
235 spi_flash_free(env_flash);
236 env_flash = NULL;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200237out:
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200238 free(tmp_env1);
239 free(tmp_env2);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200240}
241#else
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200242int saveenv(void)
243{
Andreas Fenkartdf701912017-04-08 11:59:34 +0200244 u32 saved_size, saved_offset, sector;
Marek Vasutd73c1292014-03-05 19:59:50 +0100245 char *saved_buffer = NULL;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000246 int ret = 1;
Tom Rinia840b8e2013-04-05 14:55:21 -0400247 env_t env_new;
Gong Qianyu6d85a602016-01-26 15:06:42 +0800248
Andreas Fenkart52b94442017-04-08 11:59:31 +0200249 ret = setup_flash_device();
250 if (ret)
251 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200252
Mike Frysinger9af10d52008-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 Grinbergc3c1bed2011-11-07 01:14:08 +0000258 if (!saved_buffer)
Mike Frysinger9af10d52008-12-11 06:23:37 -0500259 goto done;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000260
Stefano Babic5f6c2862010-10-27 11:06:20 +0200261 ret = spi_flash_read(env_flash, saved_offset,
262 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500263 if (ret)
264 goto done;
265 }
266
Marek Vasutd73c1292014-03-05 19:59:50 +0100267 ret = env_export(&env_new);
268 if (ret)
Stefano Babic5f6c2862010-10-27 11:06:20 +0200269 goto done;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200270
Andreas Fenkartdf701912017-04-08 11:59:34 +0200271 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
272
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200273 puts("Erasing SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200274 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
275 sector * CONFIG_ENV_SECT_SIZE);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500276 if (ret)
277 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200278
279 puts("Writing to SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200280 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
Tom Rinia840b8e2013-04-05 14:55:21 -0400281 CONFIG_ENV_SIZE, &env_new);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500282 if (ret)
283 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200284
Mike Frysinger9af10d52008-12-11 06:23:37 -0500285 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200286 ret = spi_flash_write(env_flash, saved_offset,
287 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500288 if (ret)
289 goto done;
290 }
291
292 ret = 0;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200293 puts("done\n");
Mike Frysinger9af10d52008-12-11 06:23:37 -0500294
295 done:
296 if (saved_buffer)
297 free(saved_buffer);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000298
Mike Frysinger9af10d52008-12-11 06:23:37 -0500299 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200300}
301
302void env_relocate_spec(void)
303{
304 int ret;
Ying Zhang354846f2014-01-24 15:50:07 +0800305 char *buf = NULL;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200306
Ravi Babu20d93fc2015-08-17 13:29:49 +0530307 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200308 if (!buf) {
309 set_default_env("!malloc() failed");
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200310 return;
311 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200312
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200313 ret = setup_flash_device();
314 if (ret)
315 goto out;
316
Wolfgang Denk460a9ff2010-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 Fenkarta2c65852017-04-08 11:59:33 +0200321 goto err_read;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200322 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200323
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200324 ret = env_import(buf, 1);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200325 if (ret)
326 gd->env_valid = 1;
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200327
328err_read:
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200329 spi_flash_free(env_flash);
330 env_flash = NULL;
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200331out:
332 free(buf);
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200333}
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200334#endif
Haavard Skinnemoeneb8951c2008-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}