blob: a597b24dcb35213c00eabedc73481219015bffe8 [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";
62env_t *env_ptr;
63
64static struct spi_flash *env_flash;
65
66uchar env_get_char_spec(int index)
67{
68 return *((uchar *)(gd->env_addr + index));
69}
70
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020071#if defined(CONFIG_ENV_OFFSET_REDUND)
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020072
73int saveenv(void)
74{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020075 env_t env_new;
76 ssize_t len;
77 char *res;
78 u32 saved_size, saved_offset;
79 char *saved_buffer = NULL;
80 u32 sector = 1;
81 int ret;
82 char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020083
84 if (!env_flash) {
Stefano Babic5f6c2862010-10-27 11:06:20 +020085 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
86 CONFIG_ENV_SPI_CS,
87 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
88 if (!env_flash) {
89 set_default_env("!spi_flash_probe() failed");
90 return 1;
91 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020092 }
93
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020094 res = (char *)&env_new.data;
95 len = hexport('\0', &res, ENV_SIZE);
96 if (len < 0) {
97 error("Cannot export environment: errno = %d\n", errno);
98 return 1;
99 }
100 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
101 env_new.flags = ACTIVE_FLAG;
102
Stefano Babic5f6c2862010-10-27 11:06:20 +0200103 if (gd->env_valid == 1) {
104 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
105 env_offset = CONFIG_ENV_OFFSET;
106 } else {
107 env_new_offset = CONFIG_ENV_OFFSET;
108 env_offset = CONFIG_ENV_OFFSET_REDUND;
109 }
110
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200111 /* Is the sector larger than the env (i.e. embedded) */
112 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
113 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
114 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
115 saved_buffer = malloc(saved_size);
116 if (!saved_buffer) {
117 ret = 1;
118 goto done;
119 }
120 ret = spi_flash_read(env_flash, saved_offset,
121 saved_size, saved_buffer);
122 if (ret)
123 goto done;
124 }
125
126 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
127 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
128 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
129 sector++;
130 }
131
132 puts("Erasing SPI flash...");
133 ret = spi_flash_erase(env_flash, env_new_offset,
134 sector * CONFIG_ENV_SECT_SIZE);
135 if (ret)
136 goto done;
137
138 puts("Writing to SPI flash...");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200139
Stefano Babic5f6c2862010-10-27 11:06:20 +0200140 ret = spi_flash_write(env_flash, env_new_offset,
141 CONFIG_ENV_SIZE, &env_new);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200142 if (ret)
143 goto done;
144
145 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
146 ret = spi_flash_write(env_flash, saved_offset,
147 saved_size, saved_buffer);
148 if (ret)
149 goto done;
150 }
151
Stefano Babic5f6c2862010-10-27 11:06:20 +0200152 ret = spi_flash_write(env_flash,
153 env_offset + offsetof(env_t, flags),
154 sizeof(env_new.flags), &flag);
155 if (ret)
156 goto done;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200157
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200158 puts("done\n");
159
Stefano Babic5f6c2862010-10-27 11:06:20 +0200160 gd->env_valid = (gd->env_valid == 2 ? 1 : 2);
161
162 printf("Valid environment: %d\n", gd->env_valid);
163
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200164 done:
165 if (saved_buffer)
166 free(saved_buffer);
167 return ret;
168}
169
170void env_relocate_spec(void)
171{
172 int ret;
173 int crc1_ok = 0, crc2_ok = 0;
174 env_t *tmp_env1 = NULL;
175 env_t *tmp_env2 = NULL;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200176 env_t *ep = NULL;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200177 uchar flag1, flag2;
178 /* current_env is set only in case both areas are valid! */
179 int current_env = 0;
180
181 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200182 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200183
184 if (!tmp_env1 || !tmp_env2) {
185 free(tmp_env1);
186 free(tmp_env2);
187 set_default_env("!malloc() failed");
188 return;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200189 }
190
191 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
192 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200193 if (!env_flash) {
194 set_default_env("!spi_flash_probe() failed");
195 return;
196 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200197
198 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
199 CONFIG_ENV_SIZE, tmp_env1);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200200 if (ret) {
201 set_default_env("!spi_flash_read() failed");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200202 goto err_read;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200203 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200204
205 if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc)
206 crc1_ok = 1;
207 flag1 = tmp_env1->flags;
208
209 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
210 CONFIG_ENV_SIZE, tmp_env2);
211 if (!ret) {
212 if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc)
213 crc2_ok = 1;
214 flag2 = tmp_env2->flags;
215 }
216
Stefano Babic5f6c2862010-10-27 11:06:20 +0200217 if (!crc1_ok && !crc2_ok) {
218 free(tmp_env1);
219 free(tmp_env2);
220 set_default_env("!bad CRC");
221 return;
222 } else if (crc1_ok && !crc2_ok) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200223 gd->env_valid = 1;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200224 ep = tmp_env1;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200225 } else if (!crc1_ok && crc2_ok) {
226 gd->env_valid = 1;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200227 } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) {
228 gd->env_valid = 1;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200229 } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200230 gd->env_valid = 2;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200231 } else if (flag1 == flag2) {
232 gd->env_valid = 2;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200233 } else if (flag1 == 0xFF) {
234 gd->env_valid = 2;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200235 } else {
236 /*
237 * this differs from code in env_flash.c, but I think a sane
238 * default path is desirable.
239 */
240 gd->env_valid = 2;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200241 }
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200242
Stefano Babic5f6c2862010-10-27 11:06:20 +0200243 free(env_ptr);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200244
Stefano Babic5f6c2862010-10-27 11:06:20 +0200245 if (gd->env_valid == 1)
246 ep = tmp_env1;
247 else
248 ep = tmp_env2;
249
250 ret = env_import((char *)ep, 0);
251 if (!ret) {
252 error("Cannot import environment: errno = %d\n", errno);
253 set_default_env("env_import failed");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200254 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200255
256err_read:
257 spi_flash_free(env_flash);
258 env_flash = NULL;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200259out:
Stefano Babic5f6c2862010-10-27 11:06:20 +0200260 if (tmp_env1)
261 free(tmp_env1);
262 if (tmp_env2)
263 free(tmp_env2);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200264 free(tmp_env1);
265 free(tmp_env2);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200266
267 return;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200268}
269#else
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200270int saveenv(void)
271{
Mike Frysinger9af10d52008-12-11 06:23:37 -0500272 u32 saved_size, saved_offset;
273 char *saved_buffer = NULL;
TsiChung Liew08792d72008-08-06 19:37:17 -0500274 u32 sector = 1;
Mike Frysinger9af10d52008-12-11 06:23:37 -0500275 int ret;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200276 env_t env_new;
277 char *res;
278 ssize_t len;
TsiChung Liew08792d72008-08-06 19:37:17 -0500279
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200280 if (!env_flash) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200281 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
282 CONFIG_ENV_SPI_CS,
283 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
284 if (!env_flash) {
285 set_default_env("!spi_flash_probe() failed");
286 return 1;
287 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200288 }
289
Mike Frysinger9af10d52008-12-11 06:23:37 -0500290 /* Is the sector larger than the env (i.e. embedded) */
291 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
292 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
293 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
294 saved_buffer = malloc(saved_size);
295 if (!saved_buffer) {
296 ret = 1;
297 goto done;
298 }
Stefano Babic5f6c2862010-10-27 11:06:20 +0200299 ret = spi_flash_read(env_flash, saved_offset,
300 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500301 if (ret)
302 goto done;
303 }
304
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200305 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
306 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
307 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
TsiChung Liew08792d72008-08-06 19:37:17 -0500308 sector++;
309 }
310
Stefano Babic5f6c2862010-10-27 11:06:20 +0200311 res = (char *)&env_new.data;
312 len = hexport('\0', &res, ENV_SIZE);
313 if (len < 0) {
314 error("Cannot export environment: errno = %d\n", errno);
315 goto done;
316 }
317 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
318
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200319 puts("Erasing SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200320 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
321 sector * CONFIG_ENV_SECT_SIZE);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500322 if (ret)
323 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200324
325 puts("Writing to SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200326 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
327 CONFIG_ENV_SIZE, &env_new);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500328 if (ret)
329 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200330
Mike Frysinger9af10d52008-12-11 06:23:37 -0500331 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200332 ret = spi_flash_write(env_flash, saved_offset,
333 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500334 if (ret)
335 goto done;
336 }
337
338 ret = 0;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200339 puts("done\n");
Mike Frysinger9af10d52008-12-11 06:23:37 -0500340
341 done:
342 if (saved_buffer)
343 free(saved_buffer);
344 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200345}
346
347void env_relocate_spec(void)
348{
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200349 char buf[CONFIG_ENV_SIZE];
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200350 int ret;
351
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200352 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
353 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200354 if (!env_flash) {
355 set_default_env("!spi_flash_probe() failed");
356 return;
357 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200358
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200359 ret = spi_flash_read(env_flash,
360 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
361 if (ret) {
362 set_default_env("!spi_flash_read() failed");
363 goto out;
364 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200365
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200366 ret = env_import(buf, 1);
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200367
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200368 if (ret)
369 gd->env_valid = 1;
370out:
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200371 spi_flash_free(env_flash);
372 env_flash = NULL;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200373}
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200374#endif
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200375
376int env_init(void)
377{
378 /* SPI flash isn't usable before relocation */
379 gd->env_addr = (ulong)&default_environment[0];
380 gd->env_valid = 1;
381
382 return 0;
383}