blob: a52fb734c85c6a061cd0631b6c7a0296448ccb12 [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;
83 u32 saved_size, saved_offset, sector = 1;
84 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
118 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
119 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
120 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
121 sector++;
122 }
123
124 puts("Erasing SPI flash...");
125 ret = spi_flash_erase(env_flash, env_new_offset,
126 sector * CONFIG_ENV_SECT_SIZE);
127 if (ret)
128 goto done;
129
130 puts("Writing to SPI flash...");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200131
Stefano Babic5f6c2862010-10-27 11:06:20 +0200132 ret = spi_flash_write(env_flash, env_new_offset,
Tom Rinia840b8e2013-04-05 14:55:21 -0400133 CONFIG_ENV_SIZE, &env_new);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200134 if (ret)
135 goto done;
136
137 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
138 ret = spi_flash_write(env_flash, saved_offset,
139 saved_size, saved_buffer);
140 if (ret)
141 goto done;
142 }
143
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000144 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
Tom Rinia840b8e2013-04-05 14:55:21 -0400145 sizeof(env_new.flags), &flag);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200146 if (ret)
147 goto done;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200148
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200149 puts("done\n");
150
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000151 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200152
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000153 printf("Valid environment: %d\n", (int)gd->env_valid);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200154
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200155 done:
156 if (saved_buffer)
157 free(saved_buffer);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000158
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200159 return ret;
160}
161
162void env_relocate_spec(void)
163{
164 int ret;
165 int crc1_ok = 0, crc2_ok = 0;
166 env_t *tmp_env1 = NULL;
167 env_t *tmp_env2 = NULL;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200168 env_t *ep = NULL;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200169
Ravi Babu20d93fc2015-08-17 13:29:49 +0530170 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
171 CONFIG_ENV_SIZE);
172 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
173 CONFIG_ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200174 if (!tmp_env1 || !tmp_env2) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200175 set_default_env("!malloc() failed");
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000176 goto out;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200177 }
178
Andreas Fenkarta2d18832017-04-08 11:59:32 +0200179 ret = setup_flash_device();
180 if (ret)
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000181 goto out;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200182
183 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
184 CONFIG_ENV_SIZE, tmp_env1);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200185 if (ret) {
186 set_default_env("!spi_flash_read() failed");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200187 goto err_read;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200188 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200189
190 if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc)
191 crc1_ok = 1;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200192
193 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
194 CONFIG_ENV_SIZE, tmp_env2);
195 if (!ret) {
196 if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc)
197 crc2_ok = 1;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200198 }
199
Stefano Babic5f6c2862010-10-27 11:06:20 +0200200 if (!crc1_ok && !crc2_ok) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200201 set_default_env("!bad CRC");
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000202 goto err_read;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200203 } else if (crc1_ok && !crc2_ok) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200204 gd->env_valid = 1;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200205 } else if (!crc1_ok && crc2_ok) {
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000206 gd->env_valid = 2;
207 } else if (tmp_env1->flags == ACTIVE_FLAG &&
208 tmp_env2->flags == OBSOLETE_FLAG) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200209 gd->env_valid = 1;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000210 } else if (tmp_env1->flags == OBSOLETE_FLAG &&
211 tmp_env2->flags == ACTIVE_FLAG) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200212 gd->env_valid = 2;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000213 } else if (tmp_env1->flags == tmp_env2->flags) {
Mario Schuknecht12f1a542015-03-24 08:59:00 +0100214 gd->env_valid = 1;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000215 } else if (tmp_env1->flags == 0xFF) {
Mario Schuknecht12f1a542015-03-24 08:59:00 +0100216 gd->env_valid = 1;
217 } else if (tmp_env2->flags == 0xFF) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200218 gd->env_valid = 2;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200219 } else {
220 /*
221 * this differs from code in env_flash.c, but I think a sane
222 * default path is desirable.
223 */
Mario Schuknecht12f1a542015-03-24 08:59:00 +0100224 gd->env_valid = 1;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200225 }
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200226
Stefano Babic5f6c2862010-10-27 11:06:20 +0200227 if (gd->env_valid == 1)
228 ep = tmp_env1;
229 else
230 ep = tmp_env2;
231
232 ret = env_import((char *)ep, 0);
233 if (!ret) {
234 error("Cannot import environment: errno = %d\n", errno);
Peng Fan3e21c722016-03-28 17:26:27 +0800235 set_default_env("!env_import failed");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200236 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200237
238err_read:
239 spi_flash_free(env_flash);
240 env_flash = NULL;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200241out:
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200242 free(tmp_env1);
243 free(tmp_env2);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200244}
245#else
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200246int saveenv(void)
247{
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000248 u32 saved_size, saved_offset, sector = 1;
Marek Vasutd73c1292014-03-05 19:59:50 +0100249 char *saved_buffer = NULL;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000250 int ret = 1;
Tom Rinia840b8e2013-04-05 14:55:21 -0400251 env_t env_new;
Gong Qianyu6d85a602016-01-26 15:06:42 +0800252
Andreas Fenkart52b94442017-04-08 11:59:31 +0200253 ret = setup_flash_device();
254 if (ret)
255 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200256
Mike Frysinger9af10d52008-12-11 06:23:37 -0500257 /* Is the sector larger than the env (i.e. embedded) */
258 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
259 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
260 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
261 saved_buffer = malloc(saved_size);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000262 if (!saved_buffer)
Mike Frysinger9af10d52008-12-11 06:23:37 -0500263 goto done;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000264
Stefano Babic5f6c2862010-10-27 11:06:20 +0200265 ret = spi_flash_read(env_flash, saved_offset,
266 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500267 if (ret)
268 goto done;
269 }
270
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200271 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
272 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
273 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
TsiChung Liew08792d72008-08-06 19:37:17 -0500274 sector++;
275 }
276
Marek Vasutd73c1292014-03-05 19:59:50 +0100277 ret = env_export(&env_new);
278 if (ret)
Stefano Babic5f6c2862010-10-27 11:06:20 +0200279 goto done;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200280
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200281 puts("Erasing SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200282 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
283 sector * CONFIG_ENV_SECT_SIZE);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500284 if (ret)
285 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200286
287 puts("Writing to SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200288 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
Tom Rinia840b8e2013-04-05 14:55:21 -0400289 CONFIG_ENV_SIZE, &env_new);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500290 if (ret)
291 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200292
Mike Frysinger9af10d52008-12-11 06:23:37 -0500293 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200294 ret = spi_flash_write(env_flash, saved_offset,
295 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500296 if (ret)
297 goto done;
298 }
299
300 ret = 0;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200301 puts("done\n");
Mike Frysinger9af10d52008-12-11 06:23:37 -0500302
303 done:
304 if (saved_buffer)
305 free(saved_buffer);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000306
Mike Frysinger9af10d52008-12-11 06:23:37 -0500307 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200308}
309
310void env_relocate_spec(void)
311{
312 int ret;
Ying Zhang354846f2014-01-24 15:50:07 +0800313 char *buf = NULL;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200314
Ravi Babu20d93fc2015-08-17 13:29:49 +0530315 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
Andreas Fenkarta2d18832017-04-08 11:59:32 +0200316
317 ret = setup_flash_device();
318 if (ret) {
Ying Zhang354846f2014-01-24 15:50:07 +0800319 if (buf)
320 free(buf);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200321 return;
322 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200323
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200324 ret = spi_flash_read(env_flash,
325 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
326 if (ret) {
327 set_default_env("!spi_flash_read() failed");
328 goto out;
329 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200330
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200331 ret = env_import(buf, 1);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200332 if (ret)
333 gd->env_valid = 1;
334out:
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200335 spi_flash_free(env_flash);
Ying Zhang354846f2014-01-24 15:50:07 +0800336 if (buf)
337 free(buf);
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200338 env_flash = NULL;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200339}
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200340#endif
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200341
342int env_init(void)
343{
344 /* SPI flash isn't usable before relocation */
345 gd->env_addr = (ulong)&default_environment[0];
346 gd->env_valid = 1;
347
348 return 0;
349}