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 uchar env_get_char_spec(int index)
64 return *((uchar *)(gd->env_addr + index));
67 #if defined(CONFIG_ENV_OFFSET_REDUND)
72 char *res, *saved_buffer = NULL, flag = OBSOLETE_FLAG;
73 u32 saved_size, saved_offset, sector = 1;
77 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
79 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
81 set_default_env("!spi_flash_probe() failed");
86 res = (char *)&env_new.data;
87 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
89 error("Cannot export environment: errno = %d\n", errno);
92 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
93 env_new.flags = ACTIVE_FLAG;
95 if (gd->env_valid == 1) {
96 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
97 env_offset = CONFIG_ENV_OFFSET;
99 env_new_offset = CONFIG_ENV_OFFSET;
100 env_offset = CONFIG_ENV_OFFSET_REDUND;
103 /* 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);
112 ret = spi_flash_read(env_flash, saved_offset,
113 saved_size, saved_buffer);
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)
124 puts("Erasing SPI flash...");
125 ret = spi_flash_erase(env_flash, env_new_offset,
126 sector * CONFIG_ENV_SECT_SIZE);
130 puts("Writing to SPI flash...");
132 ret = spi_flash_write(env_flash, env_new_offset,
133 CONFIG_ENV_SIZE, &env_new);
137 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
138 ret = spi_flash_write(env_flash, saved_offset,
139 saved_size, saved_buffer);
144 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
145 sizeof(env_new.flags), &flag);
151 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
153 printf("Valid environment: %d\n", (int)gd->env_valid);
162 void env_relocate_spec(void)
165 int crc1_ok = 0, crc2_ok = 0;
166 env_t *tmp_env1 = NULL;
167 env_t *tmp_env2 = NULL;
170 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
171 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
173 if (!tmp_env1 || !tmp_env2) {
174 set_default_env("!malloc() failed");
178 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
179 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
181 set_default_env("!spi_flash_probe() failed");
185 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
186 CONFIG_ENV_SIZE, tmp_env1);
188 set_default_env("!spi_flash_read() failed");
192 if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc)
195 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
196 CONFIG_ENV_SIZE, tmp_env2);
198 if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc)
202 if (!crc1_ok && !crc2_ok) {
203 set_default_env("!bad CRC");
205 } else if (crc1_ok && !crc2_ok) {
207 } else if (!crc1_ok && crc2_ok) {
209 } else if (tmp_env1->flags == ACTIVE_FLAG &&
210 tmp_env2->flags == OBSOLETE_FLAG) {
212 } else if (tmp_env1->flags == OBSOLETE_FLAG &&
213 tmp_env2->flags == ACTIVE_FLAG) {
215 } else if (tmp_env1->flags == tmp_env2->flags) {
217 } else if (tmp_env1->flags == 0xFF) {
221 * this differs from code in env_flash.c, but I think a sane
222 * default path is desirable.
227 if (gd->env_valid == 1)
232 ret = env_import((char *)ep, 0);
234 error("Cannot import environment: errno = %d\n", errno);
235 set_default_env("env_import failed");
239 spi_flash_free(env_flash);
248 u32 saved_size, saved_offset, sector = 1;
249 char *res, *saved_buffer = NULL;
255 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
257 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
259 set_default_env("!spi_flash_probe() failed");
264 /* 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);
272 ret = spi_flash_read(env_flash, saved_offset,
273 saved_size, saved_buffer);
278 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)
284 res = (char *)&env_new.data;
285 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
287 error("Cannot export environment: errno = %d\n", errno);
290 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
292 puts("Erasing SPI flash...");
293 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
294 sector * CONFIG_ENV_SECT_SIZE);
298 puts("Writing to SPI flash...");
299 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
300 CONFIG_ENV_SIZE, &env_new);
304 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
305 ret = spi_flash_write(env_flash, saved_offset,
306 saved_size, saved_buffer);
321 void env_relocate_spec(void)
323 char buf[CONFIG_ENV_SIZE];
326 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
327 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
329 set_default_env("!spi_flash_probe() failed");
333 ret = spi_flash_read(env_flash,
334 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
336 set_default_env("!spi_flash_read() failed");
340 ret = env_import(buf, 1);
344 spi_flash_free(env_flash);
351 /* SPI flash isn't usable before relocation */
352 gd->env_addr = (ulong)&default_environment[0];