blob: 45f441a042b5ec0caedf226c81754ba63106c041 [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>
Simon Glass11c89f32017-05-17 17:18:03 -060013#include <dm.h>
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020014#include <environment.h>
Mike Frysinger9af10d52008-12-11 06:23:37 -050015#include <malloc.h>
Simon Glassdef92152014-10-13 23:41:55 -060016#include <spi.h>
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020017#include <spi_flash.h>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020018#include <search.h>
19#include <errno.h>
Gong Qianyu6d85a602016-01-26 15:06:42 +080020#include <dm/device-internal.h>
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020021
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020022#ifndef CONFIG_ENV_SPI_BUS
Jean-Jacques Hiblot464eee12017-02-01 14:51:47 +010023# define CONFIG_ENV_SPI_BUS CONFIG_SF_DEFAULT_BUS
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020024#endif
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020025#ifndef CONFIG_ENV_SPI_CS
Jean-Jacques Hiblot464eee12017-02-01 14:51:47 +010026# define CONFIG_ENV_SPI_CS CONFIG_SF_DEFAULT_CS
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020027#endif
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020028#ifndef CONFIG_ENV_SPI_MAX_HZ
Jean-Jacques Hiblot464eee12017-02-01 14:51:47 +010029# define CONFIG_ENV_SPI_MAX_HZ CONFIG_SF_DEFAULT_SPEED
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020030#endif
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020031#ifndef CONFIG_ENV_SPI_MODE
Jean-Jacques Hiblot464eee12017-02-01 14:51:47 +010032# define CONFIG_ENV_SPI_MODE CONFIG_SF_DEFAULT_MODE
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020033#endif
34
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020035#ifdef CONFIG_ENV_OFFSET_REDUND
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000036static ulong env_offset = CONFIG_ENV_OFFSET;
37static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020038
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000039#define ACTIVE_FLAG 1
40#define OBSOLETE_FLAG 0
Stefano Babic5f6c2862010-10-27 11:06:20 +020041#endif /* CONFIG_ENV_OFFSET_REDUND */
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020042
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020043DECLARE_GLOBAL_DATA_PTR;
44
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000045char *env_name_spec = "SPI Flash";
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020046
47static struct spi_flash *env_flash;
48
Andreas Fenkart52b94442017-04-08 11:59:31 +020049static int setup_flash_device(void)
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020050{
Gong Qianyu6d85a602016-01-26 15:06:42 +080051#ifdef CONFIG_DM_SPI_FLASH
52 struct udevice *new;
Andreas Fenkart52b94442017-04-08 11:59:31 +020053 int ret;
Gong Qianyu6d85a602016-01-26 15:06:42 +080054
Vignesh Rae569792016-07-06 10:04:28 +053055 /* speed and mode will be read from DT */
Gong Qianyu6d85a602016-01-26 15:06:42 +080056 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
Vignesh Rae569792016-07-06 10:04:28 +053057 0, 0, &new);
Gong Qianyu6d85a602016-01-26 15:06:42 +080058 if (ret) {
59 set_default_env("!spi_flash_probe_bus_cs() failed");
60 return 1;
61 }
62
63 env_flash = dev_get_uclass_priv(new);
64#else
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020065
66 if (!env_flash) {
Stefano Babic5f6c2862010-10-27 11:06:20 +020067 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
68 CONFIG_ENV_SPI_CS,
69 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
70 if (!env_flash) {
71 set_default_env("!spi_flash_probe() failed");
72 return 1;
73 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020074 }
Gong Qianyu6d85a602016-01-26 15:06:42 +080075#endif
Andreas Fenkart52b94442017-04-08 11:59:31 +020076 return 0;
77}
78
79#if defined(CONFIG_ENV_OFFSET_REDUND)
80int saveenv(void)
81{
82 env_t env_new;
83 char *saved_buffer = NULL, flag = OBSOLETE_FLAG;
Andreas Fenkartdf701912017-04-08 11:59:34 +020084 u32 saved_size, saved_offset, sector;
Andreas Fenkart52b94442017-04-08 11:59:31 +020085 int ret;
86
87 ret = setup_flash_device();
88 if (ret)
89 return ret;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020090
Marek Vasutd73c1292014-03-05 19:59:50 +010091 ret = env_export(&env_new);
92 if (ret)
93 return ret;
Tom Rinia840b8e2013-04-05 14:55:21 -040094 env_new.flags = ACTIVE_FLAG;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020095
Stefano Babic5f6c2862010-10-27 11:06:20 +020096 if (gd->env_valid == 1) {
97 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
98 env_offset = CONFIG_ENV_OFFSET;
99 } else {
100 env_new_offset = CONFIG_ENV_OFFSET;
101 env_offset = CONFIG_ENV_OFFSET_REDUND;
102 }
103
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200104 /* Is the sector larger than the env (i.e. embedded) */
105 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
106 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
107 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
Ravi Babu20d93fc2015-08-17 13:29:49 +0530108 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200109 if (!saved_buffer) {
110 ret = 1;
111 goto done;
112 }
113 ret = spi_flash_read(env_flash, saved_offset,
114 saved_size, saved_buffer);
115 if (ret)
116 goto done;
117 }
118
Andreas Fenkartdf701912017-04-08 11:59:34 +0200119 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200120
121 puts("Erasing SPI flash...");
122 ret = spi_flash_erase(env_flash, env_new_offset,
123 sector * CONFIG_ENV_SECT_SIZE);
124 if (ret)
125 goto done;
126
127 puts("Writing to SPI flash...");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200128
Stefano Babic5f6c2862010-10-27 11:06:20 +0200129 ret = spi_flash_write(env_flash, env_new_offset,
Tom Rinia840b8e2013-04-05 14:55:21 -0400130 CONFIG_ENV_SIZE, &env_new);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200131 if (ret)
132 goto done;
133
134 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
135 ret = spi_flash_write(env_flash, saved_offset,
136 saved_size, saved_buffer);
137 if (ret)
138 goto done;
139 }
140
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000141 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
Tom Rinia840b8e2013-04-05 14:55:21 -0400142 sizeof(env_new.flags), &flag);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200143 if (ret)
144 goto done;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200145
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200146 puts("done\n");
147
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000148 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200149
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000150 printf("Valid environment: %d\n", (int)gd->env_valid);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200151
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200152 done:
153 if (saved_buffer)
154 free(saved_buffer);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000155
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200156 return ret;
157}
158
159void env_relocate_spec(void)
160{
161 int ret;
162 int crc1_ok = 0, crc2_ok = 0;
163 env_t *tmp_env1 = NULL;
164 env_t *tmp_env2 = NULL;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200165 env_t *ep = NULL;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200166
Ravi Babu20d93fc2015-08-17 13:29:49 +0530167 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
168 CONFIG_ENV_SIZE);
169 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
170 CONFIG_ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200171 if (!tmp_env1 || !tmp_env2) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200172 set_default_env("!malloc() failed");
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000173 goto out;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200174 }
175
Andreas Fenkarta2d18832017-04-08 11:59:32 +0200176 ret = setup_flash_device();
177 if (ret)
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000178 goto out;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200179
180 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
181 CONFIG_ENV_SIZE, tmp_env1);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200182 if (ret) {
183 set_default_env("!spi_flash_read() failed");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200184 goto err_read;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200185 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200186
187 if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc)
188 crc1_ok = 1;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200189
190 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
191 CONFIG_ENV_SIZE, tmp_env2);
192 if (!ret) {
193 if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc)
194 crc2_ok = 1;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200195 }
196
Stefano Babic5f6c2862010-10-27 11:06:20 +0200197 if (!crc1_ok && !crc2_ok) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200198 set_default_env("!bad CRC");
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000199 goto err_read;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200200 } else if (crc1_ok && !crc2_ok) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200201 gd->env_valid = 1;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200202 } else if (!crc1_ok && crc2_ok) {
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000203 gd->env_valid = 2;
204 } else if (tmp_env1->flags == ACTIVE_FLAG &&
205 tmp_env2->flags == OBSOLETE_FLAG) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200206 gd->env_valid = 1;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000207 } else if (tmp_env1->flags == OBSOLETE_FLAG &&
208 tmp_env2->flags == ACTIVE_FLAG) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200209 gd->env_valid = 2;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000210 } else if (tmp_env1->flags == tmp_env2->flags) {
Mario Schuknecht12f1a542015-03-24 08:59:00 +0100211 gd->env_valid = 1;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000212 } else if (tmp_env1->flags == 0xFF) {
Mario Schuknecht12f1a542015-03-24 08:59:00 +0100213 gd->env_valid = 1;
214 } else if (tmp_env2->flags == 0xFF) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200215 gd->env_valid = 2;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200216 } else {
217 /*
218 * this differs from code in env_flash.c, but I think a sane
219 * default path is desirable.
220 */
Mario Schuknecht12f1a542015-03-24 08:59:00 +0100221 gd->env_valid = 1;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200222 }
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200223
Stefano Babic5f6c2862010-10-27 11:06:20 +0200224 if (gd->env_valid == 1)
225 ep = tmp_env1;
226 else
227 ep = tmp_env2;
228
229 ret = env_import((char *)ep, 0);
230 if (!ret) {
231 error("Cannot import environment: errno = %d\n", errno);
Peng Fan3e21c722016-03-28 17:26:27 +0800232 set_default_env("!env_import failed");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200233 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200234
235err_read:
236 spi_flash_free(env_flash);
237 env_flash = NULL;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200238out:
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200239 free(tmp_env1);
240 free(tmp_env2);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200241}
242#else
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200243int saveenv(void)
244{
Andreas Fenkartdf701912017-04-08 11:59:34 +0200245 u32 saved_size, saved_offset, sector;
Marek Vasutd73c1292014-03-05 19:59:50 +0100246 char *saved_buffer = NULL;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000247 int ret = 1;
Tom Rinia840b8e2013-04-05 14:55:21 -0400248 env_t env_new;
Gong Qianyu6d85a602016-01-26 15:06:42 +0800249
Andreas Fenkart52b94442017-04-08 11:59:31 +0200250 ret = setup_flash_device();
251 if (ret)
252 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200253
Mike Frysinger9af10d52008-12-11 06:23:37 -0500254 /* Is the sector larger than the env (i.e. embedded) */
255 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
256 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
257 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
258 saved_buffer = malloc(saved_size);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000259 if (!saved_buffer)
Mike Frysinger9af10d52008-12-11 06:23:37 -0500260 goto done;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000261
Stefano Babic5f6c2862010-10-27 11:06:20 +0200262 ret = spi_flash_read(env_flash, saved_offset,
263 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500264 if (ret)
265 goto done;
266 }
267
Marek Vasutd73c1292014-03-05 19:59:50 +0100268 ret = env_export(&env_new);
269 if (ret)
Stefano Babic5f6c2862010-10-27 11:06:20 +0200270 goto done;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200271
Andreas Fenkartdf701912017-04-08 11:59:34 +0200272 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
273
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200274 puts("Erasing SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200275 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
276 sector * CONFIG_ENV_SECT_SIZE);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500277 if (ret)
278 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200279
280 puts("Writing to SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200281 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
Tom Rinia840b8e2013-04-05 14:55:21 -0400282 CONFIG_ENV_SIZE, &env_new);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500283 if (ret)
284 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200285
Mike Frysinger9af10d52008-12-11 06:23:37 -0500286 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200287 ret = spi_flash_write(env_flash, saved_offset,
288 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500289 if (ret)
290 goto done;
291 }
292
293 ret = 0;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200294 puts("done\n");
Mike Frysinger9af10d52008-12-11 06:23:37 -0500295
296 done:
297 if (saved_buffer)
298 free(saved_buffer);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000299
Mike Frysinger9af10d52008-12-11 06:23:37 -0500300 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200301}
302
303void env_relocate_spec(void)
304{
305 int ret;
Ying Zhang354846f2014-01-24 15:50:07 +0800306 char *buf = NULL;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200307
Ravi Babu20d93fc2015-08-17 13:29:49 +0530308 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200309 if (!buf) {
310 set_default_env("!malloc() failed");
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200311 return;
312 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200313
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200314 ret = setup_flash_device();
315 if (ret)
316 goto out;
317
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200318 ret = spi_flash_read(env_flash,
319 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
320 if (ret) {
321 set_default_env("!spi_flash_read() failed");
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200322 goto err_read;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200323 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200324
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200325 ret = env_import(buf, 1);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200326 if (ret)
327 gd->env_valid = 1;
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200328
329err_read:
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200330 spi_flash_free(env_flash);
331 env_flash = NULL;
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200332out:
333 free(buf);
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200334}
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200335#endif
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200336
337int env_init(void)
338{
339 /* SPI flash isn't usable before relocation */
340 gd->env_addr = (ulong)&default_environment[0];
341 gd->env_valid = 1;
342
343 return 0;
344}