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 /* references to names in env_common.c */
59 extern uchar default_environment[];
61 char * env_name_spec = "SPI Flash";
64 static struct spi_flash *env_flash;
66 uchar env_get_char_spec(int index)
68 return *((uchar *)(gd->env_addr + index));
71 #if defined(CONFIG_ENV_OFFSET_REDUND)
78 u32 saved_size, saved_offset;
79 char *saved_buffer = NULL;
82 char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG;
85 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
87 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
89 set_default_env("!spi_flash_probe() failed");
94 res = (char *)&env_new.data;
95 len = hexport('\0', &res, ENV_SIZE);
97 error("Cannot export environment: errno = %d\n", errno);
100 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
101 env_new.flags = ACTIVE_FLAG;
103 if (gd->env_valid == 1) {
104 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
105 env_offset = CONFIG_ENV_OFFSET;
107 env_new_offset = CONFIG_ENV_OFFSET;
108 env_offset = CONFIG_ENV_OFFSET_REDUND;
111 /* 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);
120 ret = spi_flash_read(env_flash, saved_offset,
121 saved_size, saved_buffer);
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)
132 puts("Erasing SPI flash...");
133 ret = spi_flash_erase(env_flash, env_new_offset,
134 sector * CONFIG_ENV_SECT_SIZE);
138 puts("Writing to SPI flash...");
140 ret = spi_flash_write(env_flash, env_new_offset,
141 CONFIG_ENV_SIZE, &env_new);
145 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
146 ret = spi_flash_write(env_flash, saved_offset,
147 saved_size, saved_buffer);
152 ret = spi_flash_write(env_flash,
153 env_offset + offsetof(env_t, flags),
154 sizeof(env_new.flags), &flag);
160 gd->env_valid = (gd->env_valid == 2 ? 1 : 2);
162 printf("Valid environment: %d\n", gd->env_valid);
170 void env_relocate_spec(void)
173 int crc1_ok = 0, crc2_ok = 0;
174 env_t *tmp_env1 = NULL;
175 env_t *tmp_env2 = NULL;
178 /* current_env is set only in case both areas are valid! */
181 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
182 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
184 if (!tmp_env1 || !tmp_env2) {
187 set_default_env("!malloc() failed");
191 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
192 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
194 set_default_env("!spi_flash_probe() failed");
198 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
199 CONFIG_ENV_SIZE, tmp_env1);
201 set_default_env("!spi_flash_read() failed");
205 if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc)
207 flag1 = tmp_env1->flags;
209 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
210 CONFIG_ENV_SIZE, tmp_env2);
212 if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc)
214 flag2 = tmp_env2->flags;
217 if (!crc1_ok && !crc2_ok) {
220 set_default_env("!bad CRC");
222 } else if (crc1_ok && !crc2_ok) {
225 } else if (!crc1_ok && crc2_ok) {
227 } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) {
229 } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) {
231 } else if (flag1 == flag2) {
233 } else if (flag1 == 0xFF) {
237 * this differs from code in env_flash.c, but I think a sane
238 * default path is desirable.
245 if (gd->env_valid == 1)
250 ret = env_import((char *)ep, 0);
252 error("Cannot import environment: errno = %d\n", errno);
253 set_default_env("env_import failed");
257 spi_flash_free(env_flash);
272 u32 saved_size, saved_offset;
273 char *saved_buffer = NULL;
281 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
283 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
285 set_default_env("!spi_flash_probe() failed");
290 /* 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);
298 ret = spi_flash_read(env_flash, saved_offset,
299 saved_size, saved_buffer);
304 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
305 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
306 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
310 res = (char *)&env_new.data;
311 len = hexport('\0', &res, ENV_SIZE);
313 error("Cannot export environment: errno = %d\n", errno);
316 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
318 puts("Erasing SPI flash...");
319 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
320 sector * CONFIG_ENV_SECT_SIZE);
324 puts("Writing to SPI flash...");
325 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
326 CONFIG_ENV_SIZE, &env_new);
330 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
331 ret = spi_flash_write(env_flash, saved_offset,
332 saved_size, saved_buffer);
346 void env_relocate_spec(void)
348 char buf[CONFIG_ENV_SIZE];
351 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
352 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
354 set_default_env("!spi_flash_probe() failed");
358 ret = spi_flash_read(env_flash,
359 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
361 set_default_env("!spi_flash_read() failed");
365 ret = env_import(buf, 1);
370 spi_flash_free(env_flash);
377 /* SPI flash isn't usable before relocation */
378 gd->env_addr = (ulong)&default_environment[0];