2 * (C) Copyright 2000-2010
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6 * Andreas Heppel <aheppel@sysgo.de>
8 * (C) Copyright 2008 Atmel Corporation
10 * See file CREDITS for list of people who contributed to this
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.
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.
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,
29 #include <environment.h>
31 #include <spi_flash.h>
35 #ifndef CONFIG_ENV_SPI_BUS
36 # define CONFIG_ENV_SPI_BUS 0
38 #ifndef CONFIG_ENV_SPI_CS
39 # define CONFIG_ENV_SPI_CS 0
41 #ifndef CONFIG_ENV_SPI_MAX_HZ
42 # define CONFIG_ENV_SPI_MAX_HZ 1000000
44 #ifndef CONFIG_ENV_SPI_MODE
45 # define CONFIG_ENV_SPI_MODE SPI_MODE_3
48 #ifdef CONFIG_ENV_OFFSET_REDUND
49 static ulong env_offset = CONFIG_ENV_OFFSET;
50 static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
53 #define OBSOLETE_FLAG 0
54 #endif /* CONFIG_ENV_OFFSET_REDUND */
56 DECLARE_GLOBAL_DATA_PTR;
58 char *env_name_spec = "SPI Flash";
60 static struct spi_flash *env_flash;
62 #if defined(CONFIG_ENV_OFFSET_REDUND)
67 char *res, *saved_buffer = NULL, flag = OBSOLETE_FLAG;
68 u32 saved_size, saved_offset, sector = 1;
72 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
74 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
76 set_default_env("!spi_flash_probe() failed");
81 res = (char *)&env_new.data;
82 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
84 error("Cannot export environment: errno = %d\n", errno);
87 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
88 env_new.flags = ACTIVE_FLAG;
90 if (gd->env_valid == 1) {
91 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
92 env_offset = CONFIG_ENV_OFFSET;
94 env_new_offset = CONFIG_ENV_OFFSET;
95 env_offset = CONFIG_ENV_OFFSET_REDUND;
98 /* 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);
107 ret = spi_flash_read(env_flash, saved_offset,
108 saved_size, saved_buffer);
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)
119 puts("Erasing SPI flash...");
120 ret = spi_flash_erase(env_flash, env_new_offset,
121 sector * CONFIG_ENV_SECT_SIZE);
125 puts("Writing to SPI flash...");
127 ret = spi_flash_write(env_flash, env_new_offset,
128 CONFIG_ENV_SIZE, &env_new);
132 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
133 ret = spi_flash_write(env_flash, saved_offset,
134 saved_size, saved_buffer);
139 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
140 sizeof(env_new.flags), &flag);
146 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
148 printf("Valid environment: %d\n", (int)gd->env_valid);
157 void env_relocate_spec(void)
160 int crc1_ok = 0, crc2_ok = 0;
161 env_t *tmp_env1 = NULL;
162 env_t *tmp_env2 = NULL;
165 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
166 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
168 if (!tmp_env1 || !tmp_env2) {
169 set_default_env("!malloc() failed");
173 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
174 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
176 set_default_env("!spi_flash_probe() failed");
180 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
181 CONFIG_ENV_SIZE, tmp_env1);
183 set_default_env("!spi_flash_read() failed");
187 if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc)
190 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
191 CONFIG_ENV_SIZE, tmp_env2);
193 if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc)
197 if (!crc1_ok && !crc2_ok) {
198 set_default_env("!bad CRC");
200 } else if (crc1_ok && !crc2_ok) {
202 } else if (!crc1_ok && crc2_ok) {
204 } else if (tmp_env1->flags == ACTIVE_FLAG &&
205 tmp_env2->flags == OBSOLETE_FLAG) {
207 } else if (tmp_env1->flags == OBSOLETE_FLAG &&
208 tmp_env2->flags == ACTIVE_FLAG) {
210 } else if (tmp_env1->flags == tmp_env2->flags) {
212 } else if (tmp_env1->flags == 0xFF) {
216 * this differs from code in env_flash.c, but I think a sane
217 * default path is desirable.
222 if (gd->env_valid == 1)
227 ret = env_import((char *)ep, 0);
229 error("Cannot import environment: errno = %d\n", errno);
230 set_default_env("env_import failed");
234 spi_flash_free(env_flash);
243 u32 saved_size, saved_offset, sector = 1;
244 char *res, *saved_buffer = NULL;
250 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
252 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
254 set_default_env("!spi_flash_probe() failed");
259 /* 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);
267 ret = spi_flash_read(env_flash, saved_offset,
268 saved_size, saved_buffer);
273 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)
279 res = (char *)&env_new.data;
280 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
282 error("Cannot export environment: errno = %d\n", errno);
285 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
287 puts("Erasing SPI flash...");
288 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
289 sector * CONFIG_ENV_SECT_SIZE);
293 puts("Writing to SPI flash...");
294 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
295 CONFIG_ENV_SIZE, &env_new);
299 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
300 ret = spi_flash_write(env_flash, saved_offset,
301 saved_size, saved_buffer);
316 void env_relocate_spec(void)
318 char buf[CONFIG_ENV_SIZE];
321 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
322 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
324 set_default_env("!spi_flash_probe() failed");
328 ret = spi_flash_read(env_flash,
329 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
331 set_default_env("!spi_flash_read() failed");
335 ret = env_import(buf, 1);
339 spi_flash_free(env_flash);
346 /* SPI flash isn't usable before relocation */
347 gd->env_addr = (ulong)&default_environment[0];