blob: a1ff297ba7e2568b795af35ce6cd653468cf79f3 [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 *
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 Skinnemoeneb8951c2008-05-16 11:10:35 +020029#include <environment.h>
Mike Frysinger9af10d52008-12-11 06:23:37 -050030#include <malloc.h>
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020031#include <spi_flash.h>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020032#include <search.h>
33#include <errno.h>
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020034
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020035#ifndef CONFIG_ENV_SPI_BUS
36# define CONFIG_ENV_SPI_BUS 0
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020037#endif
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020038#ifndef CONFIG_ENV_SPI_CS
39# define CONFIG_ENV_SPI_CS 0
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020040#endif
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020041#ifndef CONFIG_ENV_SPI_MAX_HZ
42# define CONFIG_ENV_SPI_MAX_HZ 1000000
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020043#endif
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020044#ifndef CONFIG_ENV_SPI_MODE
45# define CONFIG_ENV_SPI_MODE SPI_MODE_3
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020046#endif
47
Wolfgang Wegner0cae3d42010-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 Babic5f6c2862010-10-27 11:06:20 +020054#endif /* CONFIG_ENV_OFFSET_REDUND */
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020055
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020056DECLARE_GLOBAL_DATA_PTR;
57
58/* references to names in env_common.c */
59extern uchar default_environment[];
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020060
61char * env_name_spec = "SPI Flash";
Haavard Skinnemoeneb8951c2008-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 Wegner0cae3d42010-04-23 17:22:55 +020070#if defined(CONFIG_ENV_OFFSET_REDUND)
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020071
72int saveenv(void)
73{
Wolfgang Denk460a9ff2010-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.com97dfdcf2011-03-29 02:35:14 +000081 char flag = OBSOLETE_FLAG;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020082
83 if (!env_flash) {
Stefano Babic5f6c2862010-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 Wegner0cae3d42010-04-23 17:22:55 +020091 }
92
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020093 res = (char *)&env_new.data;
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +010094 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
Wolfgang Denk460a9ff2010-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 Babic5f6c2862010-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 Wegner0cae3d42010-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 Wegner0cae3d42010-04-23 17:22:55 +0200138
Stefano Babic5f6c2862010-10-27 11:06:20 +0200139 ret = spi_flash_write(env_flash, env_new_offset,
140 CONFIG_ENV_SIZE, &env_new);
Wolfgang Wegner0cae3d42010-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 Babic5f6c2862010-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 Wegner0cae3d42010-04-23 17:22:55 +0200156
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200157 puts("done\n");
158
Stefano Babic5f6c2862010-10-27 11:06:20 +0200159 gd->env_valid = (gd->env_valid == 2 ? 1 : 2);
160
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000161 printf("Valid environment: %d\n", (int)gd->env_valid);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200162
Wolfgang Wegner0cae3d42010-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 Babic5f6c2862010-10-27 11:06:20 +0200175 env_t *ep = NULL;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200176
177 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200178 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200179
180 if (!tmp_env1 || !tmp_env2) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200181 set_default_env("!malloc() failed");
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000182 goto out;
Wolfgang Wegner0cae3d42010-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 Denk460a9ff2010-06-20 23:33:59 +0200187 if (!env_flash) {
188 set_default_env("!spi_flash_probe() failed");
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000189 goto out;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200190 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200191
192 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
193 CONFIG_ENV_SIZE, tmp_env1);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200194 if (ret) {
195 set_default_env("!spi_flash_read() failed");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200196 goto err_read;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200197 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200198
199 if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc)
200 crc1_ok = 1;
Wolfgang Wegner0cae3d42010-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 Wegner0cae3d42010-04-23 17:22:55 +0200207 }
208
Stefano Babic5f6c2862010-10-27 11:06:20 +0200209 if (!crc1_ok && !crc2_ok) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200210 set_default_env("!bad CRC");
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000211 goto err_read;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200212 } else if (crc1_ok && !crc2_ok) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200213 gd->env_valid = 1;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200214 } else if (!crc1_ok && crc2_ok) {
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000215 gd->env_valid = 2;
216 } else if (tmp_env1->flags == ACTIVE_FLAG &&
217 tmp_env2->flags == OBSOLETE_FLAG) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200218 gd->env_valid = 1;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000219 } else if (tmp_env1->flags == OBSOLETE_FLAG &&
220 tmp_env2->flags == ACTIVE_FLAG) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200221 gd->env_valid = 2;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000222 } else if (tmp_env1->flags == tmp_env2->flags) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200223 gd->env_valid = 2;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000224 } else if (tmp_env1->flags == 0xFF) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200225 gd->env_valid = 2;
Wolfgang Wegner0cae3d42010-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;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200232 }
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200233
Stefano Babic5f6c2862010-10-27 11:06:20 +0200234 if (gd->env_valid == 1)
235 ep = tmp_env1;
236 else
237 ep = tmp_env2;
238
239 ret = env_import((char *)ep, 0);
240 if (!ret) {
241 error("Cannot import environment: errno = %d\n", errno);
242 set_default_env("env_import failed");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200243 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200244
245err_read:
246 spi_flash_free(env_flash);
247 env_flash = NULL;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200248out:
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200249 free(tmp_env1);
250 free(tmp_env2);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200251
252 return;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200253}
254#else
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200255int saveenv(void)
256{
Mike Frysinger9af10d52008-12-11 06:23:37 -0500257 u32 saved_size, saved_offset;
258 char *saved_buffer = NULL;
TsiChung Liew08792d72008-08-06 19:37:17 -0500259 u32 sector = 1;
Stefano Babic5e26a1f2010-10-29 11:49:45 +0200260 int ret = 1;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200261 env_t env_new;
262 char *res;
263 ssize_t len;
TsiChung Liew08792d72008-08-06 19:37:17 -0500264
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200265 if (!env_flash) {
Stefano Babic5f6c2862010-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 Skinnemoeneb8951c2008-05-16 11:10:35 +0200273 }
274
Mike Frysinger9af10d52008-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 Frysinger9af10d52008-12-11 06:23:37 -0500281 goto done;
282 }
Stefano Babic5f6c2862010-10-27 11:06:20 +0200283 ret = spi_flash_read(env_flash, saved_offset,
284 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500285 if (ret)
286 goto done;
287 }
288
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-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 Liew08792d72008-08-06 19:37:17 -0500292 sector++;
293 }
294
Stefano Babic5f6c2862010-10-27 11:06:20 +0200295 res = (char *)&env_new.data;
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100296 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
Stefano Babic5f6c2862010-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 Skinnemoeneb8951c2008-05-16 11:10:35 +0200303 puts("Erasing SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200304 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
305 sector * CONFIG_ENV_SECT_SIZE);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500306 if (ret)
307 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200308
309 puts("Writing to SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200310 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
311 CONFIG_ENV_SIZE, &env_new);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500312 if (ret)
313 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200314
Mike Frysinger9af10d52008-12-11 06:23:37 -0500315 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200316 ret = spi_flash_write(env_flash, saved_offset,
317 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500318 if (ret)
319 goto done;
320 }
321
322 ret = 0;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200323 puts("done\n");
Mike Frysinger9af10d52008-12-11 06:23:37 -0500324
325 done:
326 if (saved_buffer)
327 free(saved_buffer);
328 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200329}
330
331void env_relocate_spec(void)
332{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200333 char buf[CONFIG_ENV_SIZE];
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200334 int ret;
335
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-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 Denk460a9ff2010-06-20 23:33:59 +0200338 if (!env_flash) {
339 set_default_env("!spi_flash_probe() failed");
340 return;
341 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200342
Wolfgang Denk460a9ff2010-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 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200349
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200350 ret = env_import(buf, 1);
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200351
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200352 if (ret)
353 gd->env_valid = 1;
354out:
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200355 spi_flash_free(env_flash);
356 env_flash = NULL;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200357}
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200358#endif
Haavard Skinnemoeneb8951c2008-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}