blob: d3b36d01053586b3f8bb155b3eade8460efecc1b [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 *
10 * See file CREDITS for list of people who contributed to this
11 * project.
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License as
15 * published by the Free Software Foundation; either version 2 of
16 * the License, or (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 * MA 02111-1307 USA
27 */
28#include <common.h>
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020029#include <environment.h>
Mike Frysinger5b3375a2008-12-11 06:23:37 -050030#include <malloc.h>
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020031#include <spi_flash.h>
Wolfgang Denkea882ba2010-06-20 23:33:59 +020032#include <search.h>
33#include <errno.h>
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020034
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020035#ifndef CONFIG_ENV_SPI_BUS
36# define CONFIG_ENV_SPI_BUS 0
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020037#endif
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020038#ifndef CONFIG_ENV_SPI_CS
39# define CONFIG_ENV_SPI_CS 0
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020040#endif
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020041#ifndef CONFIG_ENV_SPI_MAX_HZ
42# define CONFIG_ENV_SPI_MAX_HZ 1000000
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020043#endif
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020044#ifndef CONFIG_ENV_SPI_MODE
45# define CONFIG_ENV_SPI_MODE SPI_MODE_3
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020046#endif
47
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020048#ifdef CONFIG_ENV_OFFSET_REDUND
49static ulong env_offset = CONFIG_ENV_OFFSET;
50static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
51
52#define ACTIVE_FLAG 1
53#define OBSOLETE_FLAG 0
Stefano Babica3110f02010-10-27 11:06:20 +020054#endif /* CONFIG_ENV_OFFSET_REDUND */
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020055
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020056DECLARE_GLOBAL_DATA_PTR;
57
58/* references to names in env_common.c */
59extern uchar default_environment[];
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020060
61char * env_name_spec = "SPI Flash";
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020062
63static struct spi_flash *env_flash;
64
65uchar env_get_char_spec(int index)
66{
67 return *((uchar *)(gd->env_addr + index));
68}
69
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020070#if defined(CONFIG_ENV_OFFSET_REDUND)
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020071
72int saveenv(void)
73{
Wolfgang Denkea882ba2010-06-20 23:33:59 +020074 env_t env_new;
75 ssize_t len;
76 char *res;
77 u32 saved_size, saved_offset;
78 char *saved_buffer = NULL;
79 u32 sector = 1;
80 int ret;
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +000081 char flag = OBSOLETE_FLAG;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020082
83 if (!env_flash) {
Stefano Babica3110f02010-10-27 11:06:20 +020084 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
85 CONFIG_ENV_SPI_CS,
86 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
87 if (!env_flash) {
88 set_default_env("!spi_flash_probe() failed");
89 return 1;
90 }
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020091 }
92
Wolfgang Denkea882ba2010-06-20 23:33:59 +020093 res = (char *)&env_new.data;
Mike Frysinger2eb15732010-12-08 06:26:04 -050094 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +020095 if (len < 0) {
96 error("Cannot export environment: errno = %d\n", errno);
97 return 1;
98 }
99 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
100 env_new.flags = ACTIVE_FLAG;
101
Stefano Babica3110f02010-10-27 11:06:20 +0200102 if (gd->env_valid == 1) {
103 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
104 env_offset = CONFIG_ENV_OFFSET;
105 } else {
106 env_new_offset = CONFIG_ENV_OFFSET;
107 env_offset = CONFIG_ENV_OFFSET_REDUND;
108 }
109
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200110 /* Is the sector larger than the env (i.e. embedded) */
111 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
112 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
113 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
114 saved_buffer = malloc(saved_size);
115 if (!saved_buffer) {
116 ret = 1;
117 goto done;
118 }
119 ret = spi_flash_read(env_flash, saved_offset,
120 saved_size, saved_buffer);
121 if (ret)
122 goto done;
123 }
124
125 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
126 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
127 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
128 sector++;
129 }
130
131 puts("Erasing SPI flash...");
132 ret = spi_flash_erase(env_flash, env_new_offset,
133 sector * CONFIG_ENV_SECT_SIZE);
134 if (ret)
135 goto done;
136
137 puts("Writing to SPI flash...");
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200138
Stefano Babica3110f02010-10-27 11:06:20 +0200139 ret = spi_flash_write(env_flash, env_new_offset,
140 CONFIG_ENV_SIZE, &env_new);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200141 if (ret)
142 goto done;
143
144 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
145 ret = spi_flash_write(env_flash, saved_offset,
146 saved_size, saved_buffer);
147 if (ret)
148 goto done;
149 }
150
Stefano Babica3110f02010-10-27 11:06:20 +0200151 ret = spi_flash_write(env_flash,
152 env_offset + offsetof(env_t, flags),
153 sizeof(env_new.flags), &flag);
154 if (ret)
155 goto done;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200156
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200157 puts("done\n");
158
Stefano Babica3110f02010-10-27 11:06:20 +0200159 gd->env_valid = (gd->env_valid == 2 ? 1 : 2);
160
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000161 printf("Valid environment: %d\n", (int)gd->env_valid);
Stefano Babica3110f02010-10-27 11:06:20 +0200162
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200163 done:
164 if (saved_buffer)
165 free(saved_buffer);
166 return ret;
167}
168
169void env_relocate_spec(void)
170{
171 int ret;
172 int crc1_ok = 0, crc2_ok = 0;
173 env_t *tmp_env1 = NULL;
174 env_t *tmp_env2 = NULL;
Stefano Babica3110f02010-10-27 11:06:20 +0200175 env_t *ep = NULL;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200176
177 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200178 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200179
180 if (!tmp_env1 || !tmp_env2) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200181 set_default_env("!malloc() failed");
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000182 goto out;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200183 }
184
185 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
186 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200187 if (!env_flash) {
188 set_default_env("!spi_flash_probe() failed");
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000189 goto out;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200190 }
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200191
192 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
193 CONFIG_ENV_SIZE, tmp_env1);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200194 if (ret) {
195 set_default_env("!spi_flash_read() failed");
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200196 goto err_read;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200197 }
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200198
199 if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc)
200 crc1_ok = 1;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200201
202 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
203 CONFIG_ENV_SIZE, tmp_env2);
204 if (!ret) {
205 if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc)
206 crc2_ok = 1;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200207 }
208
Stefano Babica3110f02010-10-27 11:06:20 +0200209 if (!crc1_ok && !crc2_ok) {
Stefano Babica3110f02010-10-27 11:06:20 +0200210 set_default_env("!bad CRC");
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000211 goto err_read;
Stefano Babica3110f02010-10-27 11:06:20 +0200212 } else if (crc1_ok && !crc2_ok) {
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200213 gd->env_valid = 1;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200214 } else if (!crc1_ok && crc2_ok) {
Stefano Babica3110f02010-10-27 11:06:20 +0200215 gd->env_valid = 2;
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000216 } else if (tmp_env1->flags == ACTIVE_FLAG &&
217 tmp_env2->flags == OBSOLETE_FLAG) {
218 gd->env_valid = 1;
219 } else if (tmp_env1->flags == OBSOLETE_FLAG &&
220 tmp_env2->flags == ACTIVE_FLAG) {
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200221 gd->env_valid = 2;
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000222 } else if (tmp_env1->flags == tmp_env2->flags) {
223 gd->env_valid = 2;
224 } else if (tmp_env1->flags == 0xFF) {
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200225 gd->env_valid = 2;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200226 } else {
227 /*
228 * this differs from code in env_flash.c, but I think a sane
229 * default path is desirable.
230 */
231 gd->env_valid = 2;
Stefano Babica3110f02010-10-27 11:06:20 +0200232 }
233
Stefano Babica3110f02010-10-27 11:06:20 +0200234 if (gd->env_valid == 1)
235 ep = tmp_env1;
236 else
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200237 ep = tmp_env2;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200238
Stefano Babica3110f02010-10-27 11:06:20 +0200239 ret = env_import((char *)ep, 0);
240 if (!ret) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200241 error("Cannot import environment: errno = %d\n", errno);
Stefano Babica3110f02010-10-27 11:06:20 +0200242 set_default_env("env_import failed");
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200243 }
244
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200245err_read:
246 spi_flash_free(env_flash);
247 env_flash = NULL;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200248out:
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200249 free(tmp_env1);
250 free(tmp_env2);
Stefano Babica3110f02010-10-27 11:06:20 +0200251
252 return;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200253}
254#else
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200255int saveenv(void)
256{
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500257 u32 saved_size, saved_offset;
258 char *saved_buffer = NULL;
TsiChung Liew07efc9e2008-08-06 19:37:17 -0500259 u32 sector = 1;
Stefano Babic590084a2010-10-29 11:49:45 +0200260 int ret = 1;
Stefano Babica3110f02010-10-27 11:06:20 +0200261 env_t env_new;
262 char *res;
263 ssize_t len;
TsiChung Liew07efc9e2008-08-06 19:37:17 -0500264
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200265 if (!env_flash) {
Stefano Babica3110f02010-10-27 11:06:20 +0200266 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
267 CONFIG_ENV_SPI_CS,
268 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
269 if (!env_flash) {
270 set_default_env("!spi_flash_probe() failed");
271 return 1;
272 }
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200273 }
274
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500275 /* Is the sector larger than the env (i.e. embedded) */
276 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
277 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
278 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
279 saved_buffer = malloc(saved_size);
280 if (!saved_buffer) {
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500281 goto done;
282 }
Stefano Babica3110f02010-10-27 11:06:20 +0200283 ret = spi_flash_read(env_flash, saved_offset,
284 saved_size, saved_buffer);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500285 if (ret)
286 goto done;
287 }
288
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200289 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
290 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
291 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
TsiChung Liew07efc9e2008-08-06 19:37:17 -0500292 sector++;
293 }
294
Stefano Babica3110f02010-10-27 11:06:20 +0200295 res = (char *)&env_new.data;
Mike Frysinger2eb15732010-12-08 06:26:04 -0500296 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE);
Stefano Babica3110f02010-10-27 11:06:20 +0200297 if (len < 0) {
298 error("Cannot export environment: errno = %d\n", errno);
299 goto done;
300 }
301 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
302
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200303 puts("Erasing SPI flash...");
Stefano Babica3110f02010-10-27 11:06:20 +0200304 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
305 sector * CONFIG_ENV_SECT_SIZE);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500306 if (ret)
307 goto done;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200308
309 puts("Writing to SPI flash...");
Stefano Babica3110f02010-10-27 11:06:20 +0200310 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
311 CONFIG_ENV_SIZE, &env_new);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500312 if (ret)
313 goto done;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200314
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500315 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
Stefano Babica3110f02010-10-27 11:06:20 +0200316 ret = spi_flash_write(env_flash, saved_offset,
317 saved_size, saved_buffer);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500318 if (ret)
319 goto done;
320 }
321
322 ret = 0;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200323 puts("done\n");
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500324
325 done:
326 if (saved_buffer)
327 free(saved_buffer);
328 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200329}
330
331void env_relocate_spec(void)
332{
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200333 char buf[CONFIG_ENV_SIZE];
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200334 int ret;
335
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200336 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
337 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200338 if (!env_flash) {
339 set_default_env("!spi_flash_probe() failed");
340 return;
341 }
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200342
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200343 ret = spi_flash_read(env_flash,
344 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
345 if (ret) {
346 set_default_env("!spi_flash_read() failed");
347 goto out;
348 }
349
350 ret = env_import(buf, 1);
351
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200352 if (ret)
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200353 gd->env_valid = 1;
354out:
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200355 spi_flash_free(env_flash);
356 env_flash = NULL;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200357}
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200358#endif
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200359
360int env_init(void)
361{
362 /* SPI flash isn't usable before relocation */
363 gd->env_addr = (ulong)&default_environment[0];
364 gd->env_valid = 1;
365
366 return 0;
367}