blob: 592b87088eeeab7974d83eef18fab50d43c02e6b [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
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000039# 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
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000049static ulong env_offset = CONFIG_ENV_OFFSET;
50static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020051
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000052#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
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000058char *env_name_spec = "SPI Flash";
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020059
60static struct spi_flash *env_flash;
61
62uchar env_get_char_spec(int index)
63{
64 return *((uchar *)(gd->env_addr + index));
65}
66
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020067#if defined(CONFIG_ENV_OFFSET_REDUND)
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020068int saveenv(void)
69{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020070 env_t env_new;
71 ssize_t len;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000072 char *res, *saved_buffer = NULL, flag = OBSOLETE_FLAG;
73 u32 saved_size, saved_offset, sector = 1;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020074 int ret;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020075
76 if (!env_flash) {
Stefano Babic5f6c2862010-10-27 11:06:20 +020077 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
78 CONFIG_ENV_SPI_CS,
79 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
80 if (!env_flash) {
81 set_default_env("!spi_flash_probe() failed");
82 return 1;
83 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020084 }
85
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020086 res = (char *)&env_new.data;
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +010087 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020088 if (len < 0) {
89 error("Cannot export environment: errno = %d\n", errno);
90 return 1;
91 }
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000092 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
93 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;
107 saved_buffer = malloc(saved_size);
108 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,
133 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),
145 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
170 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200171 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200172
173 if (!tmp_env1 || !tmp_env2) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200174 set_default_env("!malloc() failed");
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000175 goto out;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200176 }
177
178 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
179 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200180 if (!env_flash) {
181 set_default_env("!spi_flash_probe() failed");
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000182 goto out;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200183 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200184
185 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
186 CONFIG_ENV_SIZE, tmp_env1);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200187 if (ret) {
188 set_default_env("!spi_flash_read() failed");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200189 goto err_read;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200190 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200191
192 if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc)
193 crc1_ok = 1;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200194
195 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
196 CONFIG_ENV_SIZE, tmp_env2);
197 if (!ret) {
198 if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc)
199 crc2_ok = 1;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200200 }
201
Stefano Babic5f6c2862010-10-27 11:06:20 +0200202 if (!crc1_ok && !crc2_ok) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200203 set_default_env("!bad CRC");
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000204 goto err_read;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200205 } else if (crc1_ok && !crc2_ok) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200206 gd->env_valid = 1;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200207 } else if (!crc1_ok && crc2_ok) {
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000208 gd->env_valid = 2;
209 } else if (tmp_env1->flags == ACTIVE_FLAG &&
210 tmp_env2->flags == OBSOLETE_FLAG) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200211 gd->env_valid = 1;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000212 } else if (tmp_env1->flags == OBSOLETE_FLAG &&
213 tmp_env2->flags == ACTIVE_FLAG) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200214 gd->env_valid = 2;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000215 } else if (tmp_env1->flags == tmp_env2->flags) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200216 gd->env_valid = 2;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000217 } else if (tmp_env1->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 */
224 gd->env_valid = 2;
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);
235 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;
249 char *res, *saved_buffer = NULL;
250 int ret = 1;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200251 env_t env_new;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200252 ssize_t len;
TsiChung Liew08792d72008-08-06 19:37:17 -0500253
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200254 if (!env_flash) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200255 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
256 CONFIG_ENV_SPI_CS,
257 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
258 if (!env_flash) {
259 set_default_env("!spi_flash_probe() failed");
260 return 1;
261 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200262 }
263
Mike Frysinger9af10d52008-12-11 06:23:37 -0500264 /* Is the sector larger than the env (i.e. embedded) */
265 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
266 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
267 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
268 saved_buffer = malloc(saved_size);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000269 if (!saved_buffer)
Mike Frysinger9af10d52008-12-11 06:23:37 -0500270 goto done;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000271
Stefano Babic5f6c2862010-10-27 11:06:20 +0200272 ret = spi_flash_read(env_flash, saved_offset,
273 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500274 if (ret)
275 goto done;
276 }
277
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200278 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
279 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
280 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
TsiChung Liew08792d72008-08-06 19:37:17 -0500281 sector++;
282 }
283
Stefano Babic5f6c2862010-10-27 11:06:20 +0200284 res = (char *)&env_new.data;
Wolfgang Denk1e3cdf32011-11-06 22:49:44 +0100285 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200286 if (len < 0) {
287 error("Cannot export environment: errno = %d\n", errno);
288 goto done;
289 }
290 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
291
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200292 puts("Erasing SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200293 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
294 sector * CONFIG_ENV_SECT_SIZE);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500295 if (ret)
296 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200297
298 puts("Writing to SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200299 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
300 CONFIG_ENV_SIZE, &env_new);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500301 if (ret)
302 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200303
Mike Frysinger9af10d52008-12-11 06:23:37 -0500304 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200305 ret = spi_flash_write(env_flash, saved_offset,
306 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500307 if (ret)
308 goto done;
309 }
310
311 ret = 0;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200312 puts("done\n");
Mike Frysinger9af10d52008-12-11 06:23:37 -0500313
314 done:
315 if (saved_buffer)
316 free(saved_buffer);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000317
Mike Frysinger9af10d52008-12-11 06:23:37 -0500318 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200319}
320
321void env_relocate_spec(void)
322{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200323 char buf[CONFIG_ENV_SIZE];
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200324 int ret;
325
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200326 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
327 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200328 if (!env_flash) {
329 set_default_env("!spi_flash_probe() failed");
330 return;
331 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200332
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200333 ret = spi_flash_read(env_flash,
334 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
335 if (ret) {
336 set_default_env("!spi_flash_read() failed");
337 goto out;
338 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200339
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200340 ret = env_import(buf, 1);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200341 if (ret)
342 gd->env_valid = 1;
343out:
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200344 spi_flash_free(env_flash);
345 env_flash = NULL;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200346}
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200347#endif
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200348
349int env_init(void)
350{
351 /* SPI flash isn't usable before relocation */
352 gd->env_addr = (ulong)&default_environment[0];
353 gd->env_valid = 1;
354
355 return 0;
356}