blob: be270f21bcf6577ae44bd1d394817c5eb609fb01 [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>
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020015#include <spi_flash.h>
Wolfgang Denkea882ba2010-06-20 23:33:59 +020016#include <search.h>
17#include <errno.h>
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020018
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020019#ifndef CONFIG_ENV_SPI_BUS
20# define CONFIG_ENV_SPI_BUS 0
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020021#endif
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020022#ifndef CONFIG_ENV_SPI_CS
Igor Grinbergeb58a7f2011-11-07 01:14:08 +000023# define CONFIG_ENV_SPI_CS 0
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020024#endif
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020025#ifndef CONFIG_ENV_SPI_MAX_HZ
26# define CONFIG_ENV_SPI_MAX_HZ 1000000
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020027#endif
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020028#ifndef CONFIG_ENV_SPI_MODE
29# define CONFIG_ENV_SPI_MODE SPI_MODE_3
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020030#endif
31
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020032#ifdef CONFIG_ENV_OFFSET_REDUND
Igor Grinbergeb58a7f2011-11-07 01:14:08 +000033static ulong env_offset = CONFIG_ENV_OFFSET;
34static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020035
Igor Grinbergeb58a7f2011-11-07 01:14:08 +000036#define ACTIVE_FLAG 1
37#define OBSOLETE_FLAG 0
Stefano Babica3110f02010-10-27 11:06:20 +020038#endif /* CONFIG_ENV_OFFSET_REDUND */
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020039
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020040DECLARE_GLOBAL_DATA_PTR;
41
Igor Grinbergeb58a7f2011-11-07 01:14:08 +000042char *env_name_spec = "SPI Flash";
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020043
44static struct spi_flash *env_flash;
45
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020046#if defined(CONFIG_ENV_OFFSET_REDUND)
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020047int saveenv(void)
48{
Tom Rinicd0f4fa2013-04-05 14:55:21 -040049 env_t env_new;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020050 ssize_t len;
Igor Grinbergeb58a7f2011-11-07 01:14:08 +000051 char *res, *saved_buffer = NULL, flag = OBSOLETE_FLAG;
52 u32 saved_size, saved_offset, sector = 1;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020053 int ret;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020054
55 if (!env_flash) {
Stefano Babica3110f02010-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 Wegner7319bca2010-04-23 17:22:55 +020063 }
64
Tom Rinicd0f4fa2013-04-05 14:55:21 -040065 res = (char *)&env_new.data;
Joe Hershbergerbe112352012-12-11 22:16:23 -060066 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
Wolfgang Denkea882ba2010-06-20 23:33:59 +020067 if (len < 0) {
68 error("Cannot export environment: errno = %d\n", errno);
69 return 1;
70 }
Tom Rinicd0f4fa2013-04-05 14:55:21 -040071 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
72 env_new.flags = ACTIVE_FLAG;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020073
Stefano Babica3110f02010-10-27 11:06:20 +020074 if (gd->env_valid == 1) {
75 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
76 env_offset = CONFIG_ENV_OFFSET;
77 } else {
78 env_new_offset = CONFIG_ENV_OFFSET;
79 env_offset = CONFIG_ENV_OFFSET_REDUND;
80 }
81
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020082 /* Is the sector larger than the env (i.e. embedded) */
83 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
84 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
85 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
86 saved_buffer = malloc(saved_size);
87 if (!saved_buffer) {
88 ret = 1;
89 goto done;
90 }
91 ret = spi_flash_read(env_flash, saved_offset,
92 saved_size, saved_buffer);
93 if (ret)
94 goto done;
95 }
96
97 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
98 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
99 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
100 sector++;
101 }
102
103 puts("Erasing SPI flash...");
104 ret = spi_flash_erase(env_flash, env_new_offset,
105 sector * CONFIG_ENV_SECT_SIZE);
106 if (ret)
107 goto done;
108
109 puts("Writing to SPI flash...");
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200110
Stefano Babica3110f02010-10-27 11:06:20 +0200111 ret = spi_flash_write(env_flash, env_new_offset,
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400112 CONFIG_ENV_SIZE, &env_new);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200113 if (ret)
114 goto done;
115
116 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
117 ret = spi_flash_write(env_flash, saved_offset,
118 saved_size, saved_buffer);
119 if (ret)
120 goto done;
121 }
122
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000123 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400124 sizeof(env_new.flags), &flag);
Stefano Babica3110f02010-10-27 11:06:20 +0200125 if (ret)
126 goto done;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200127
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200128 puts("done\n");
129
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000130 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
Stefano Babica3110f02010-10-27 11:06:20 +0200131
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000132 printf("Valid environment: %d\n", (int)gd->env_valid);
Stefano Babica3110f02010-10-27 11:06:20 +0200133
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200134 done:
135 if (saved_buffer)
136 free(saved_buffer);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000137
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200138 return ret;
139}
140
141void env_relocate_spec(void)
142{
143 int ret;
144 int crc1_ok = 0, crc2_ok = 0;
145 env_t *tmp_env1 = NULL;
146 env_t *tmp_env2 = NULL;
Stefano Babica3110f02010-10-27 11:06:20 +0200147 env_t *ep = NULL;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200148
149 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200150 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200151
152 if (!tmp_env1 || !tmp_env2) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200153 set_default_env("!malloc() failed");
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000154 goto out;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200155 }
156
157 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
158 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200159 if (!env_flash) {
160 set_default_env("!spi_flash_probe() failed");
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000161 goto out;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200162 }
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200163
164 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
165 CONFIG_ENV_SIZE, tmp_env1);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200166 if (ret) {
167 set_default_env("!spi_flash_read() failed");
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200168 goto err_read;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200169 }
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200170
171 if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc)
172 crc1_ok = 1;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200173
174 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
175 CONFIG_ENV_SIZE, tmp_env2);
176 if (!ret) {
177 if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc)
178 crc2_ok = 1;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200179 }
180
Stefano Babica3110f02010-10-27 11:06:20 +0200181 if (!crc1_ok && !crc2_ok) {
Stefano Babica3110f02010-10-27 11:06:20 +0200182 set_default_env("!bad CRC");
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000183 goto err_read;
Stefano Babica3110f02010-10-27 11:06:20 +0200184 } else if (crc1_ok && !crc2_ok) {
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200185 gd->env_valid = 1;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200186 } else if (!crc1_ok && crc2_ok) {
Stefano Babica3110f02010-10-27 11:06:20 +0200187 gd->env_valid = 2;
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000188 } else if (tmp_env1->flags == ACTIVE_FLAG &&
189 tmp_env2->flags == OBSOLETE_FLAG) {
190 gd->env_valid = 1;
191 } else if (tmp_env1->flags == OBSOLETE_FLAG &&
192 tmp_env2->flags == ACTIVE_FLAG) {
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200193 gd->env_valid = 2;
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000194 } else if (tmp_env1->flags == tmp_env2->flags) {
195 gd->env_valid = 2;
196 } else if (tmp_env1->flags == 0xFF) {
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200197 gd->env_valid = 2;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200198 } else {
199 /*
200 * this differs from code in env_flash.c, but I think a sane
201 * default path is desirable.
202 */
203 gd->env_valid = 2;
Stefano Babica3110f02010-10-27 11:06:20 +0200204 }
205
Stefano Babica3110f02010-10-27 11:06:20 +0200206 if (gd->env_valid == 1)
207 ep = tmp_env1;
208 else
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200209 ep = tmp_env2;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200210
Stefano Babica3110f02010-10-27 11:06:20 +0200211 ret = env_import((char *)ep, 0);
212 if (!ret) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200213 error("Cannot import environment: errno = %d\n", errno);
Stefano Babica3110f02010-10-27 11:06:20 +0200214 set_default_env("env_import failed");
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200215 }
216
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200217err_read:
218 spi_flash_free(env_flash);
219 env_flash = NULL;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200220out:
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200221 free(tmp_env1);
222 free(tmp_env2);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200223}
224#else
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200225int saveenv(void)
226{
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000227 u32 saved_size, saved_offset, sector = 1;
228 char *res, *saved_buffer = NULL;
229 int ret = 1;
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400230 env_t env_new;
Stefano Babica3110f02010-10-27 11:06:20 +0200231 ssize_t len;
TsiChung Liew07efc9e2008-08-06 19:37:17 -0500232
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200233 if (!env_flash) {
Stefano Babica3110f02010-10-27 11:06:20 +0200234 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
235 CONFIG_ENV_SPI_CS,
236 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
237 if (!env_flash) {
238 set_default_env("!spi_flash_probe() failed");
239 return 1;
240 }
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200241 }
242
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500243 /* Is the sector larger than the env (i.e. embedded) */
244 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
245 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
246 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
247 saved_buffer = malloc(saved_size);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000248 if (!saved_buffer)
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500249 goto done;
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000250
Stefano Babica3110f02010-10-27 11:06:20 +0200251 ret = spi_flash_read(env_flash, saved_offset,
252 saved_size, saved_buffer);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500253 if (ret)
254 goto done;
255 }
256
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200257 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
258 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
259 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
TsiChung Liew07efc9e2008-08-06 19:37:17 -0500260 sector++;
261 }
262
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400263 res = (char *)&env_new.data;
Joe Hershbergerbe112352012-12-11 22:16:23 -0600264 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
Stefano Babica3110f02010-10-27 11:06:20 +0200265 if (len < 0) {
266 error("Cannot export environment: errno = %d\n", errno);
267 goto done;
268 }
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400269 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
Stefano Babica3110f02010-10-27 11:06:20 +0200270
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200271 puts("Erasing SPI flash...");
Stefano Babica3110f02010-10-27 11:06:20 +0200272 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
273 sector * CONFIG_ENV_SECT_SIZE);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500274 if (ret)
275 goto done;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200276
277 puts("Writing to SPI flash...");
Stefano Babica3110f02010-10-27 11:06:20 +0200278 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400279 CONFIG_ENV_SIZE, &env_new);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500280 if (ret)
281 goto done;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200282
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500283 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
Stefano Babica3110f02010-10-27 11:06:20 +0200284 ret = spi_flash_write(env_flash, saved_offset,
285 saved_size, saved_buffer);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500286 if (ret)
287 goto done;
288 }
289
290 ret = 0;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200291 puts("done\n");
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500292
293 done:
294 if (saved_buffer)
295 free(saved_buffer);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000296
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500297 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200298}
299
300void env_relocate_spec(void)
301{
302 int ret;
Ying Zhang5a89fa92014-01-24 15:50:07 +0800303 char *buf = NULL;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200304
Ying Zhang5a89fa92014-01-24 15:50:07 +0800305 buf = (char *)malloc(CONFIG_ENV_SIZE);
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200306 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
307 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200308 if (!env_flash) {
309 set_default_env("!spi_flash_probe() failed");
Ying Zhang5a89fa92014-01-24 15:50:07 +0800310 if (buf)
311 free(buf);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200312 return;
313 }
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200314
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200315 ret = spi_flash_read(env_flash,
316 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
317 if (ret) {
318 set_default_env("!spi_flash_read() failed");
319 goto out;
320 }
321
322 ret = env_import(buf, 1);
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200323 if (ret)
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200324 gd->env_valid = 1;
325out:
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200326 spi_flash_free(env_flash);
Ying Zhang5a89fa92014-01-24 15:50:07 +0800327 if (buf)
328 free(buf);
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200329 env_flash = NULL;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200330}
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200331#endif
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200332
333int env_init(void)
334{
335 /* SPI flash isn't usable before relocation */
336 gd->env_addr = (ulong)&default_environment[0];
337 gd->env_valid = 1;
338
339 return 0;
340}