blob: 5e3729c2c2d62dfb201e2329081318f1657fa422 [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>
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020019
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020020#ifndef CONFIG_ENV_SPI_BUS
21# define CONFIG_ENV_SPI_BUS 0
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020022#endif
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020023#ifndef CONFIG_ENV_SPI_CS
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000024# define CONFIG_ENV_SPI_CS 0
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020025#endif
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020026#ifndef CONFIG_ENV_SPI_MAX_HZ
27# define CONFIG_ENV_SPI_MAX_HZ 1000000
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020028#endif
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020029#ifndef CONFIG_ENV_SPI_MODE
30# define CONFIG_ENV_SPI_MODE SPI_MODE_3
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020031#endif
32
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020033#ifdef CONFIG_ENV_OFFSET_REDUND
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000034static ulong env_offset = CONFIG_ENV_OFFSET;
35static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020036
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000037#define ACTIVE_FLAG 1
38#define OBSOLETE_FLAG 0
Stefano Babic5f6c2862010-10-27 11:06:20 +020039#endif /* CONFIG_ENV_OFFSET_REDUND */
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020040
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020041DECLARE_GLOBAL_DATA_PTR;
42
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000043char *env_name_spec = "SPI Flash";
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020044
45static struct spi_flash *env_flash;
46
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020047#if defined(CONFIG_ENV_OFFSET_REDUND)
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020048int saveenv(void)
49{
Tom Rinia840b8e2013-04-05 14:55:21 -040050 env_t env_new;
Marek Vasutd73c1292014-03-05 19:59:50 +010051 char *saved_buffer = NULL, flag = OBSOLETE_FLAG;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000052 u32 saved_size, saved_offset, sector = 1;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020053 int ret;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020054
55 if (!env_flash) {
Stefano Babic5f6c2862010-10-27 11:06:20 +020056 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
57 CONFIG_ENV_SPI_CS,
58 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
59 if (!env_flash) {
60 set_default_env("!spi_flash_probe() failed");
61 return 1;
62 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020063 }
64
Marek Vasutd73c1292014-03-05 19:59:50 +010065 ret = env_export(&env_new);
66 if (ret)
67 return ret;
Tom Rinia840b8e2013-04-05 14:55:21 -040068 env_new.flags = ACTIVE_FLAG;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020069
Stefano Babic5f6c2862010-10-27 11:06:20 +020070 if (gd->env_valid == 1) {
71 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
72 env_offset = CONFIG_ENV_OFFSET;
73 } else {
74 env_new_offset = CONFIG_ENV_OFFSET;
75 env_offset = CONFIG_ENV_OFFSET_REDUND;
76 }
77
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020078 /* Is the sector larger than the env (i.e. embedded) */
79 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
80 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
81 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
82 saved_buffer = malloc(saved_size);
83 if (!saved_buffer) {
84 ret = 1;
85 goto done;
86 }
87 ret = spi_flash_read(env_flash, saved_offset,
88 saved_size, saved_buffer);
89 if (ret)
90 goto done;
91 }
92
93 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
94 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
95 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
96 sector++;
97 }
98
99 puts("Erasing SPI flash...");
100 ret = spi_flash_erase(env_flash, env_new_offset,
101 sector * CONFIG_ENV_SECT_SIZE);
102 if (ret)
103 goto done;
104
105 puts("Writing to SPI flash...");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200106
Stefano Babic5f6c2862010-10-27 11:06:20 +0200107 ret = spi_flash_write(env_flash, env_new_offset,
Tom Rinia840b8e2013-04-05 14:55:21 -0400108 CONFIG_ENV_SIZE, &env_new);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200109 if (ret)
110 goto done;
111
112 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
113 ret = spi_flash_write(env_flash, saved_offset,
114 saved_size, saved_buffer);
115 if (ret)
116 goto done;
117 }
118
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000119 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
Tom Rinia840b8e2013-04-05 14:55:21 -0400120 sizeof(env_new.flags), &flag);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200121 if (ret)
122 goto done;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200123
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200124 puts("done\n");
125
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000126 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200127
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000128 printf("Valid environment: %d\n", (int)gd->env_valid);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200129
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200130 done:
131 if (saved_buffer)
132 free(saved_buffer);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000133
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200134 return ret;
135}
136
137void env_relocate_spec(void)
138{
139 int ret;
140 int crc1_ok = 0, crc2_ok = 0;
141 env_t *tmp_env1 = NULL;
142 env_t *tmp_env2 = NULL;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200143 env_t *ep = NULL;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200144
145 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200146 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200147
148 if (!tmp_env1 || !tmp_env2) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200149 set_default_env("!malloc() failed");
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000150 goto out;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200151 }
152
153 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
154 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200155 if (!env_flash) {
156 set_default_env("!spi_flash_probe() failed");
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000157 goto out;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200158 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200159
160 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
161 CONFIG_ENV_SIZE, tmp_env1);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200162 if (ret) {
163 set_default_env("!spi_flash_read() failed");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200164 goto err_read;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200165 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200166
167 if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc)
168 crc1_ok = 1;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200169
170 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
171 CONFIG_ENV_SIZE, tmp_env2);
172 if (!ret) {
173 if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc)
174 crc2_ok = 1;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200175 }
176
Stefano Babic5f6c2862010-10-27 11:06:20 +0200177 if (!crc1_ok && !crc2_ok) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200178 set_default_env("!bad CRC");
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000179 goto err_read;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200180 } else if (crc1_ok && !crc2_ok) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200181 gd->env_valid = 1;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200182 } else if (!crc1_ok && crc2_ok) {
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000183 gd->env_valid = 2;
184 } else if (tmp_env1->flags == ACTIVE_FLAG &&
185 tmp_env2->flags == OBSOLETE_FLAG) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200186 gd->env_valid = 1;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000187 } else if (tmp_env1->flags == OBSOLETE_FLAG &&
188 tmp_env2->flags == ACTIVE_FLAG) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200189 gd->env_valid = 2;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000190 } else if (tmp_env1->flags == tmp_env2->flags) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200191 gd->env_valid = 2;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000192 } else if (tmp_env1->flags == 0xFF) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200193 gd->env_valid = 2;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200194 } else {
195 /*
196 * this differs from code in env_flash.c, but I think a sane
197 * default path is desirable.
198 */
199 gd->env_valid = 2;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200200 }
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200201
Stefano Babic5f6c2862010-10-27 11:06:20 +0200202 if (gd->env_valid == 1)
203 ep = tmp_env1;
204 else
205 ep = tmp_env2;
206
207 ret = env_import((char *)ep, 0);
208 if (!ret) {
209 error("Cannot import environment: errno = %d\n", errno);
210 set_default_env("env_import failed");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200211 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200212
213err_read:
214 spi_flash_free(env_flash);
215 env_flash = NULL;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200216out:
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200217 free(tmp_env1);
218 free(tmp_env2);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200219}
220#else
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200221int saveenv(void)
222{
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000223 u32 saved_size, saved_offset, sector = 1;
Marek Vasutd73c1292014-03-05 19:59:50 +0100224 char *saved_buffer = NULL;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000225 int ret = 1;
Tom Rinia840b8e2013-04-05 14:55:21 -0400226 env_t env_new;
TsiChung Liew08792d72008-08-06 19:37:17 -0500227
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200228 if (!env_flash) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200229 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
230 CONFIG_ENV_SPI_CS,
231 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
232 if (!env_flash) {
233 set_default_env("!spi_flash_probe() failed");
234 return 1;
235 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200236 }
237
Mike Frysinger9af10d52008-12-11 06:23:37 -0500238 /* Is the sector larger than the env (i.e. embedded) */
239 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
240 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
241 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
242 saved_buffer = malloc(saved_size);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000243 if (!saved_buffer)
Mike Frysinger9af10d52008-12-11 06:23:37 -0500244 goto done;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000245
Stefano Babic5f6c2862010-10-27 11:06:20 +0200246 ret = spi_flash_read(env_flash, saved_offset,
247 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500248 if (ret)
249 goto done;
250 }
251
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200252 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
253 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
254 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
TsiChung Liew08792d72008-08-06 19:37:17 -0500255 sector++;
256 }
257
Marek Vasutd73c1292014-03-05 19:59:50 +0100258 ret = env_export(&env_new);
259 if (ret)
Stefano Babic5f6c2862010-10-27 11:06:20 +0200260 goto done;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200261
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200262 puts("Erasing SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200263 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
264 sector * CONFIG_ENV_SECT_SIZE);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500265 if (ret)
266 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200267
268 puts("Writing to SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200269 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
Tom Rinia840b8e2013-04-05 14:55:21 -0400270 CONFIG_ENV_SIZE, &env_new);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500271 if (ret)
272 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200273
Mike Frysinger9af10d52008-12-11 06:23:37 -0500274 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200275 ret = spi_flash_write(env_flash, saved_offset,
276 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500277 if (ret)
278 goto done;
279 }
280
281 ret = 0;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200282 puts("done\n");
Mike Frysinger9af10d52008-12-11 06:23:37 -0500283
284 done:
285 if (saved_buffer)
286 free(saved_buffer);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000287
Mike Frysinger9af10d52008-12-11 06:23:37 -0500288 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200289}
290
291void env_relocate_spec(void)
292{
293 int ret;
Ying Zhang354846f2014-01-24 15:50:07 +0800294 char *buf = NULL;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200295
Ying Zhang354846f2014-01-24 15:50:07 +0800296 buf = (char *)malloc(CONFIG_ENV_SIZE);
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200297 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
298 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200299 if (!env_flash) {
300 set_default_env("!spi_flash_probe() failed");
Ying Zhang354846f2014-01-24 15:50:07 +0800301 if (buf)
302 free(buf);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200303 return;
304 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200305
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200306 ret = spi_flash_read(env_flash,
307 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
308 if (ret) {
309 set_default_env("!spi_flash_read() failed");
310 goto out;
311 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200312
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200313 ret = env_import(buf, 1);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200314 if (ret)
315 gd->env_valid = 1;
316out:
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200317 spi_flash_free(env_flash);
Ying Zhang354846f2014-01-24 15:50:07 +0800318 if (buf)
319 free(buf);
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200320 env_flash = NULL;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200321}
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200322#endif
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200323
324int env_init(void)
325{
326 /* SPI flash isn't usable before relocation */
327 gd->env_addr = (ulong)&default_environment[0];
328 gd->env_valid = 1;
329
330 return 0;
331}