blob: d9e9085461bb6759b0b0ee0cc2f717d6653ede99 [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
Igor Grinbergeb58a7f2011-11-07 01:14:08 +000039# 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
Igor Grinbergeb58a7f2011-11-07 01:14:08 +000049static ulong env_offset = CONFIG_ENV_OFFSET;
50static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020051
Igor Grinbergeb58a7f2011-11-07 01:14:08 +000052#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
Igor Grinbergeb58a7f2011-11-07 01:14:08 +000058char *env_name_spec = "SPI Flash";
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020059
60static struct spi_flash *env_flash;
61
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020062#if defined(CONFIG_ENV_OFFSET_REDUND)
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020063int saveenv(void)
64{
Wolfgang Denkea882ba2010-06-20 23:33:59 +020065 env_t env_new;
66 ssize_t len;
Igor Grinbergeb58a7f2011-11-07 01:14:08 +000067 char *res, *saved_buffer = NULL, flag = OBSOLETE_FLAG;
68 u32 saved_size, saved_offset, sector = 1;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020069 int ret;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020070
71 if (!env_flash) {
Stefano Babica3110f02010-10-27 11:06:20 +020072 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
73 CONFIG_ENV_SPI_CS,
74 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
75 if (!env_flash) {
76 set_default_env("!spi_flash_probe() failed");
77 return 1;
78 }
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020079 }
80
Wolfgang Denkea882ba2010-06-20 23:33:59 +020081 res = (char *)&env_new.data;
Joe Hershbergerbe112352012-12-11 22:16:23 -060082 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
Wolfgang Denkea882ba2010-06-20 23:33:59 +020083 if (len < 0) {
84 error("Cannot export environment: errno = %d\n", errno);
85 return 1;
86 }
Igor Grinbergeb58a7f2011-11-07 01:14:08 +000087 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
88 env_new.flags = ACTIVE_FLAG;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020089
Stefano Babica3110f02010-10-27 11:06:20 +020090 if (gd->env_valid == 1) {
91 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
92 env_offset = CONFIG_ENV_OFFSET;
93 } else {
94 env_new_offset = CONFIG_ENV_OFFSET;
95 env_offset = CONFIG_ENV_OFFSET_REDUND;
96 }
97
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020098 /* Is the sector larger than the env (i.e. embedded) */
99 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
100 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
101 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
102 saved_buffer = malloc(saved_size);
103 if (!saved_buffer) {
104 ret = 1;
105 goto done;
106 }
107 ret = spi_flash_read(env_flash, saved_offset,
108 saved_size, saved_buffer);
109 if (ret)
110 goto done;
111 }
112
113 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
114 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
115 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
116 sector++;
117 }
118
119 puts("Erasing SPI flash...");
120 ret = spi_flash_erase(env_flash, env_new_offset,
121 sector * CONFIG_ENV_SECT_SIZE);
122 if (ret)
123 goto done;
124
125 puts("Writing to SPI flash...");
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200126
Stefano Babica3110f02010-10-27 11:06:20 +0200127 ret = spi_flash_write(env_flash, env_new_offset,
128 CONFIG_ENV_SIZE, &env_new);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200129 if (ret)
130 goto done;
131
132 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
133 ret = spi_flash_write(env_flash, saved_offset,
134 saved_size, saved_buffer);
135 if (ret)
136 goto done;
137 }
138
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000139 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
140 sizeof(env_new.flags), &flag);
Stefano Babica3110f02010-10-27 11:06:20 +0200141 if (ret)
142 goto done;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200143
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200144 puts("done\n");
145
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000146 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
Stefano Babica3110f02010-10-27 11:06:20 +0200147
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000148 printf("Valid environment: %d\n", (int)gd->env_valid);
Stefano Babica3110f02010-10-27 11:06:20 +0200149
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200150 done:
151 if (saved_buffer)
152 free(saved_buffer);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000153
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200154 return ret;
155}
156
157void env_relocate_spec(void)
158{
159 int ret;
160 int crc1_ok = 0, crc2_ok = 0;
161 env_t *tmp_env1 = NULL;
162 env_t *tmp_env2 = NULL;
Stefano Babica3110f02010-10-27 11:06:20 +0200163 env_t *ep = NULL;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200164
165 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200166 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200167
168 if (!tmp_env1 || !tmp_env2) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200169 set_default_env("!malloc() failed");
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000170 goto out;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200171 }
172
173 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
174 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200175 if (!env_flash) {
176 set_default_env("!spi_flash_probe() failed");
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000177 goto out;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200178 }
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200179
180 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
181 CONFIG_ENV_SIZE, tmp_env1);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200182 if (ret) {
183 set_default_env("!spi_flash_read() failed");
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200184 goto err_read;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200185 }
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200186
187 if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc)
188 crc1_ok = 1;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200189
190 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
191 CONFIG_ENV_SIZE, tmp_env2);
192 if (!ret) {
193 if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc)
194 crc2_ok = 1;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200195 }
196
Stefano Babica3110f02010-10-27 11:06:20 +0200197 if (!crc1_ok && !crc2_ok) {
Stefano Babica3110f02010-10-27 11:06:20 +0200198 set_default_env("!bad CRC");
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000199 goto err_read;
Stefano Babica3110f02010-10-27 11:06:20 +0200200 } else if (crc1_ok && !crc2_ok) {
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200201 gd->env_valid = 1;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200202 } else if (!crc1_ok && crc2_ok) {
Stefano Babica3110f02010-10-27 11:06:20 +0200203 gd->env_valid = 2;
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000204 } else if (tmp_env1->flags == ACTIVE_FLAG &&
205 tmp_env2->flags == OBSOLETE_FLAG) {
206 gd->env_valid = 1;
207 } else if (tmp_env1->flags == OBSOLETE_FLAG &&
208 tmp_env2->flags == ACTIVE_FLAG) {
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200209 gd->env_valid = 2;
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000210 } else if (tmp_env1->flags == tmp_env2->flags) {
211 gd->env_valid = 2;
212 } else if (tmp_env1->flags == 0xFF) {
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200213 gd->env_valid = 2;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200214 } else {
215 /*
216 * this differs from code in env_flash.c, but I think a sane
217 * default path is desirable.
218 */
219 gd->env_valid = 2;
Stefano Babica3110f02010-10-27 11:06:20 +0200220 }
221
Stefano Babica3110f02010-10-27 11:06:20 +0200222 if (gd->env_valid == 1)
223 ep = tmp_env1;
224 else
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200225 ep = tmp_env2;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200226
Stefano Babica3110f02010-10-27 11:06:20 +0200227 ret = env_import((char *)ep, 0);
228 if (!ret) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200229 error("Cannot import environment: errno = %d\n", errno);
Stefano Babica3110f02010-10-27 11:06:20 +0200230 set_default_env("env_import failed");
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200231 }
232
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200233err_read:
234 spi_flash_free(env_flash);
235 env_flash = NULL;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200236out:
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200237 free(tmp_env1);
238 free(tmp_env2);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200239}
240#else
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200241int saveenv(void)
242{
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000243 u32 saved_size, saved_offset, sector = 1;
244 char *res, *saved_buffer = NULL;
245 int ret = 1;
Stefano Babica3110f02010-10-27 11:06:20 +0200246 env_t env_new;
Stefano Babica3110f02010-10-27 11:06:20 +0200247 ssize_t len;
TsiChung Liew07efc9e2008-08-06 19:37:17 -0500248
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200249 if (!env_flash) {
Stefano Babica3110f02010-10-27 11:06:20 +0200250 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
251 CONFIG_ENV_SPI_CS,
252 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
253 if (!env_flash) {
254 set_default_env("!spi_flash_probe() failed");
255 return 1;
256 }
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200257 }
258
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500259 /* Is the sector larger than the env (i.e. embedded) */
260 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
261 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
262 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
263 saved_buffer = malloc(saved_size);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000264 if (!saved_buffer)
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500265 goto done;
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000266
Stefano Babica3110f02010-10-27 11:06:20 +0200267 ret = spi_flash_read(env_flash, saved_offset,
268 saved_size, saved_buffer);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500269 if (ret)
270 goto done;
271 }
272
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200273 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
274 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
275 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
TsiChung Liew07efc9e2008-08-06 19:37:17 -0500276 sector++;
277 }
278
Stefano Babica3110f02010-10-27 11:06:20 +0200279 res = (char *)&env_new.data;
Joe Hershbergerbe112352012-12-11 22:16:23 -0600280 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
Stefano Babica3110f02010-10-27 11:06:20 +0200281 if (len < 0) {
282 error("Cannot export environment: errno = %d\n", errno);
283 goto done;
284 }
285 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
286
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200287 puts("Erasing SPI flash...");
Stefano Babica3110f02010-10-27 11:06:20 +0200288 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
289 sector * CONFIG_ENV_SECT_SIZE);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500290 if (ret)
291 goto done;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200292
293 puts("Writing to SPI flash...");
Stefano Babica3110f02010-10-27 11:06:20 +0200294 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
295 CONFIG_ENV_SIZE, &env_new);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500296 if (ret)
297 goto done;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200298
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500299 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
Stefano Babica3110f02010-10-27 11:06:20 +0200300 ret = spi_flash_write(env_flash, saved_offset,
301 saved_size, saved_buffer);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500302 if (ret)
303 goto done;
304 }
305
306 ret = 0;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200307 puts("done\n");
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500308
309 done:
310 if (saved_buffer)
311 free(saved_buffer);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000312
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500313 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200314}
315
316void env_relocate_spec(void)
317{
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200318 char buf[CONFIG_ENV_SIZE];
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200319 int ret;
320
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200321 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
322 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200323 if (!env_flash) {
324 set_default_env("!spi_flash_probe() failed");
325 return;
326 }
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200327
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200328 ret = spi_flash_read(env_flash,
329 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
330 if (ret) {
331 set_default_env("!spi_flash_read() failed");
332 goto out;
333 }
334
335 ret = env_import(buf, 1);
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200336 if (ret)
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200337 gd->env_valid = 1;
338out:
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200339 spi_flash_free(env_flash);
340 env_flash = NULL;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200341}
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200342#endif
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200343
344int env_init(void)
345{
346 /* SPI flash isn't usable before relocation */
347 gd->env_addr = (ulong)&default_environment[0];
348 gd->env_valid = 1;
349
350 return 0;
351}