blob: 592b87088eeeab7974d83eef18fab50d43c02e6b [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
62uchar env_get_char_spec(int index)
63{
64 return *((uchar *)(gd->env_addr + index));
65}
66
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020067#if defined(CONFIG_ENV_OFFSET_REDUND)
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020068int saveenv(void)
69{
Wolfgang Denkea882ba2010-06-20 23:33:59 +020070 env_t env_new;
71 ssize_t len;
Igor Grinbergeb58a7f2011-11-07 01:14:08 +000072 char *res, *saved_buffer = NULL, flag = OBSOLETE_FLAG;
73 u32 saved_size, saved_offset, sector = 1;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020074 int ret;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020075
76 if (!env_flash) {
Stefano Babica3110f02010-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 Wegner7319bca2010-04-23 17:22:55 +020084 }
85
Wolfgang Denkea882ba2010-06-20 23:33:59 +020086 res = (char *)&env_new.data;
Wolfgang Denk37f2fe72011-11-06 22:49:44 +010087 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
Wolfgang Denkea882ba2010-06-20 23:33:59 +020088 if (len < 0) {
89 error("Cannot export environment: errno = %d\n", errno);
90 return 1;
91 }
Igor Grinbergeb58a7f2011-11-07 01:14:08 +000092 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
93 env_new.flags = ACTIVE_FLAG;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020094
Stefano Babica3110f02010-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 Wegner7319bca2010-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 Wegner7319bca2010-04-23 17:22:55 +0200131
Stefano Babica3110f02010-10-27 11:06:20 +0200132 ret = spi_flash_write(env_flash, env_new_offset,
133 CONFIG_ENV_SIZE, &env_new);
Wolfgang Wegner7319bca2010-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 Grinbergeb58a7f2011-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 Babica3110f02010-10-27 11:06:20 +0200146 if (ret)
147 goto done;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200148
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200149 puts("done\n");
150
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000151 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
Stefano Babica3110f02010-10-27 11:06:20 +0200152
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000153 printf("Valid environment: %d\n", (int)gd->env_valid);
Stefano Babica3110f02010-10-27 11:06:20 +0200154
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200155 done:
156 if (saved_buffer)
157 free(saved_buffer);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000158
Wolfgang Wegner7319bca2010-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 Babica3110f02010-10-27 11:06:20 +0200168 env_t *ep = NULL;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200169
170 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200171 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200172
173 if (!tmp_env1 || !tmp_env2) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200174 set_default_env("!malloc() failed");
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000175 goto out;
Wolfgang Wegner7319bca2010-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 Denkea882ba2010-06-20 23:33:59 +0200180 if (!env_flash) {
181 set_default_env("!spi_flash_probe() failed");
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000182 goto out;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200183 }
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200184
185 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
186 CONFIG_ENV_SIZE, tmp_env1);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200187 if (ret) {
188 set_default_env("!spi_flash_read() failed");
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200189 goto err_read;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200190 }
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200191
192 if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc)
193 crc1_ok = 1;
Wolfgang Wegner7319bca2010-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 Wegner7319bca2010-04-23 17:22:55 +0200200 }
201
Stefano Babica3110f02010-10-27 11:06:20 +0200202 if (!crc1_ok && !crc2_ok) {
Stefano Babica3110f02010-10-27 11:06:20 +0200203 set_default_env("!bad CRC");
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000204 goto err_read;
Stefano Babica3110f02010-10-27 11:06:20 +0200205 } else if (crc1_ok && !crc2_ok) {
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200206 gd->env_valid = 1;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200207 } else if (!crc1_ok && crc2_ok) {
Stefano Babica3110f02010-10-27 11:06:20 +0200208 gd->env_valid = 2;
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000209 } else if (tmp_env1->flags == ACTIVE_FLAG &&
210 tmp_env2->flags == OBSOLETE_FLAG) {
211 gd->env_valid = 1;
212 } else if (tmp_env1->flags == OBSOLETE_FLAG &&
213 tmp_env2->flags == ACTIVE_FLAG) {
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200214 gd->env_valid = 2;
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000215 } else if (tmp_env1->flags == tmp_env2->flags) {
216 gd->env_valid = 2;
217 } else if (tmp_env1->flags == 0xFF) {
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200218 gd->env_valid = 2;
Wolfgang Wegner7319bca2010-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;
Stefano Babica3110f02010-10-27 11:06:20 +0200225 }
226
Stefano Babica3110f02010-10-27 11:06:20 +0200227 if (gd->env_valid == 1)
228 ep = tmp_env1;
229 else
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200230 ep = tmp_env2;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200231
Stefano Babica3110f02010-10-27 11:06:20 +0200232 ret = env_import((char *)ep, 0);
233 if (!ret) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200234 error("Cannot import environment: errno = %d\n", errno);
Stefano Babica3110f02010-10-27 11:06:20 +0200235 set_default_env("env_import failed");
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200236 }
237
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200238err_read:
239 spi_flash_free(env_flash);
240 env_flash = NULL;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200241out:
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200242 free(tmp_env1);
243 free(tmp_env2);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200244}
245#else
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200246int saveenv(void)
247{
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000248 u32 saved_size, saved_offset, sector = 1;
249 char *res, *saved_buffer = NULL;
250 int ret = 1;
Stefano Babica3110f02010-10-27 11:06:20 +0200251 env_t env_new;
Stefano Babica3110f02010-10-27 11:06:20 +0200252 ssize_t len;
TsiChung Liew07efc9e2008-08-06 19:37:17 -0500253
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200254 if (!env_flash) {
Stefano Babica3110f02010-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 Skinnemoen8c664972008-05-16 11:10:35 +0200262 }
263
Mike Frysinger5b3375a2008-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 Grinbergeb58a7f2011-11-07 01:14:08 +0000269 if (!saved_buffer)
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500270 goto done;
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000271
Stefano Babica3110f02010-10-27 11:06:20 +0200272 ret = spi_flash_read(env_flash, saved_offset,
273 saved_size, saved_buffer);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500274 if (ret)
275 goto done;
276 }
277
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-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 Liew07efc9e2008-08-06 19:37:17 -0500281 sector++;
282 }
283
Stefano Babica3110f02010-10-27 11:06:20 +0200284 res = (char *)&env_new.data;
Wolfgang Denk37f2fe72011-11-06 22:49:44 +0100285 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
Stefano Babica3110f02010-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 Skinnemoen8c664972008-05-16 11:10:35 +0200292 puts("Erasing SPI flash...");
Stefano Babica3110f02010-10-27 11:06:20 +0200293 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
294 sector * CONFIG_ENV_SECT_SIZE);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500295 if (ret)
296 goto done;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200297
298 puts("Writing to SPI flash...");
Stefano Babica3110f02010-10-27 11:06:20 +0200299 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
300 CONFIG_ENV_SIZE, &env_new);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500301 if (ret)
302 goto done;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200303
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500304 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
Stefano Babica3110f02010-10-27 11:06:20 +0200305 ret = spi_flash_write(env_flash, saved_offset,
306 saved_size, saved_buffer);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500307 if (ret)
308 goto done;
309 }
310
311 ret = 0;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200312 puts("done\n");
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500313
314 done:
315 if (saved_buffer)
316 free(saved_buffer);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000317
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500318 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200319}
320
321void env_relocate_spec(void)
322{
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200323 char buf[CONFIG_ENV_SIZE];
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200324 int ret;
325
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-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 Denkea882ba2010-06-20 23:33:59 +0200328 if (!env_flash) {
329 set_default_env("!spi_flash_probe() failed");
330 return;
331 }
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200332
Wolfgang Denkea882ba2010-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 }
339
340 ret = env_import(buf, 1);
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200341 if (ret)
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200342 gd->env_valid = 1;
343out:
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200344 spi_flash_free(env_flash);
345 env_flash = NULL;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200346}
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200347#endif
Haavard Skinnemoen8c664972008-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}