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";
63 static struct spi_flash *env_flash;
65 uchar env_get_char_spec(int index)
67 return *((uchar *)(gd->env_addr + index));
70 #if defined(CONFIG_ENV_OFFSET_REDUND)
77 u32 saved_size, saved_offset;
78 char *saved_buffer = NULL;
81 char flag = OBSOLETE_FLAG;
84 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
86 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
88 set_default_env("!spi_flash_probe() failed");
93 res = (char *)&env_new.data;
94 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
96 error("Cannot export environment: errno = %d\n", errno);
99 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
100 env_new.flags = ACTIVE_FLAG;
102 if (gd->env_valid == 1) {
103 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
104 env_offset = CONFIG_ENV_OFFSET;
106 env_new_offset = CONFIG_ENV_OFFSET;
107 env_offset = CONFIG_ENV_OFFSET_REDUND;
110 /* Is the sector larger than the env (i.e. embedded) */
111 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
112 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
113 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
114 saved_buffer = malloc(saved_size);
119 ret = spi_flash_read(env_flash, saved_offset,
120 saved_size, saved_buffer);
125 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
126 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
127 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
131 puts("Erasing SPI flash...");
132 ret = spi_flash_erase(env_flash, env_new_offset,
133 sector * CONFIG_ENV_SECT_SIZE);
137 puts("Writing to SPI flash...");
139 ret = spi_flash_write(env_flash, env_new_offset,
140 CONFIG_ENV_SIZE, &env_new);
144 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
145 ret = spi_flash_write(env_flash, saved_offset,
146 saved_size, saved_buffer);
151 ret = spi_flash_write(env_flash,
152 env_offset + offsetof(env_t, flags),
153 sizeof(env_new.flags), &flag);
159 gd->env_valid = (gd->env_valid == 2 ? 1 : 2);
161 printf("Valid environment: %d\n", (int)gd->env_valid);
169 void env_relocate_spec(void)
172 int crc1_ok = 0, crc2_ok = 0;
173 env_t *tmp_env1 = NULL;
174 env_t *tmp_env2 = NULL;
177 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
178 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
180 if (!tmp_env1 || !tmp_env2) {
181 set_default_env("!malloc() failed");
185 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
186 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
188 set_default_env("!spi_flash_probe() failed");
192 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
193 CONFIG_ENV_SIZE, tmp_env1);
195 set_default_env("!spi_flash_read() failed");
199 if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc)
202 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
203 CONFIG_ENV_SIZE, tmp_env2);
205 if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc)
209 if (!crc1_ok && !crc2_ok) {
210 set_default_env("!bad CRC");
212 } else if (crc1_ok && !crc2_ok) {
214 } else if (!crc1_ok && crc2_ok) {
216 } else if (tmp_env1->flags == ACTIVE_FLAG &&
217 tmp_env2->flags == OBSOLETE_FLAG) {
219 } else if (tmp_env1->flags == OBSOLETE_FLAG &&
220 tmp_env2->flags == ACTIVE_FLAG) {
222 } else if (tmp_env1->flags == tmp_env2->flags) {
224 } else if (tmp_env1->flags == 0xFF) {
228 * this differs from code in env_flash.c, but I think a sane
229 * default path is desirable.
234 if (gd->env_valid == 1)
239 ret = env_import((char *)ep, 0);
241 error("Cannot import environment: errno = %d\n", errno);
242 set_default_env("env_import failed");
246 spi_flash_free(env_flash);
257 u32 saved_size, saved_offset;
258 char *saved_buffer = NULL;
266 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
268 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
270 set_default_env("!spi_flash_probe() failed");
275 /* Is the sector larger than the env (i.e. embedded) */
276 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
277 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
278 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
279 saved_buffer = malloc(saved_size);
283 ret = spi_flash_read(env_flash, saved_offset,
284 saved_size, saved_buffer);
289 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
290 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
291 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
295 res = (char *)&env_new.data;
296 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE, 0, NULL);
298 error("Cannot export environment: errno = %d\n", errno);
301 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
303 puts("Erasing SPI flash...");
304 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
305 sector * CONFIG_ENV_SECT_SIZE);
309 puts("Writing to SPI flash...");
310 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
311 CONFIG_ENV_SIZE, &env_new);
315 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
316 ret = spi_flash_write(env_flash, saved_offset,
317 saved_size, saved_buffer);
331 void env_relocate_spec(void)
333 char buf[CONFIG_ENV_SIZE];
336 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
337 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
339 set_default_env("!spi_flash_probe() failed");
343 ret = spi_flash_read(env_flash,
344 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
346 set_default_env("!spi_flash_read() failed");
350 ret = env_import(buf, 1);
355 spi_flash_free(env_flash);
362 /* SPI flash isn't usable before relocation */
363 gd->env_addr = (ulong)&default_environment[0];