2 * (C) Copyright 2000-2002
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>
33 #ifndef CONFIG_ENV_SPI_BUS
34 # define CONFIG_ENV_SPI_BUS 0
36 #ifndef CONFIG_ENV_SPI_CS
37 # define CONFIG_ENV_SPI_CS 0
39 #ifndef CONFIG_ENV_SPI_MAX_HZ
40 # define CONFIG_ENV_SPI_MAX_HZ 1000000
42 #ifndef CONFIG_ENV_SPI_MODE
43 # define CONFIG_ENV_SPI_MODE SPI_MODE_3
46 #ifdef CONFIG_ENV_OFFSET_REDUND
47 static ulong env_offset = CONFIG_ENV_OFFSET;
48 static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
51 #define OBSOLETE_FLAG 0
52 #endif /* CONFIG_ENV_ADDR_REDUND */
54 DECLARE_GLOBAL_DATA_PTR;
56 /* references to names in env_common.c */
57 extern uchar default_environment[];
59 char * env_name_spec = "SPI Flash";
62 static struct spi_flash *env_flash;
64 uchar env_get_char_spec(int index)
66 return *((uchar *)(gd->env_addr + index));
69 #if defined(CONFIG_ENV_OFFSET_REDUND)
72 ulong tmp_offset = env_offset;
74 env_offset = env_new_offset;
75 env_new_offset = tmp_offset;
80 u32 saved_size, saved_offset;
81 char *saved_buffer = NULL;
84 char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG;
87 puts("Environment SPI flash not initialized\n");
91 /* Is the sector larger than the env (i.e. embedded) */
92 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
93 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
94 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
95 saved_buffer = malloc(saved_size);
100 ret = spi_flash_read(env_flash, saved_offset,
101 saved_size, saved_buffer);
106 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
107 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
108 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
112 puts("Erasing SPI flash...");
113 ret = spi_flash_erase(env_flash, env_new_offset,
114 sector * CONFIG_ENV_SECT_SIZE);
118 puts("Writing to SPI flash...");
119 ret = spi_flash_write(env_flash,
120 env_new_offset + offsetof(env_t, data),
121 sizeof(env_ptr->data), env_ptr->data);
125 ret = spi_flash_write(env_flash,
126 env_new_offset + offsetof(env_t, crc),
127 sizeof(env_ptr->crc), &env_ptr->crc);
131 ret = spi_flash_write(env_flash,
132 env_offset + offsetof(env_t, flags),
133 sizeof(env_ptr->flags), &flag);
137 ret = spi_flash_write(env_flash,
138 env_new_offset + offsetof(env_t, flags),
139 sizeof(env_ptr->flags), &new_flag);
143 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
144 ret = spi_flash_write(env_flash, saved_offset,
145 saved_size, saved_buffer);
161 void env_relocate_spec(void)
164 int crc1_ok = 0, crc2_ok = 0;
165 env_t *tmp_env1 = NULL;
166 env_t *tmp_env2 = NULL;
168 /* current_env is set only in case both areas are valid! */
171 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
173 puts("*** Warning: could not init environment,"
174 " using defaults\n\n");
178 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
180 puts("*** Warning: could not init environment,"
181 " using defaults\n\n");
185 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
186 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
190 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
191 CONFIG_ENV_SIZE, tmp_env1);
195 if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc)
197 flag1 = tmp_env1->flags;
199 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
200 CONFIG_ENV_SIZE, tmp_env2);
202 if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc)
204 flag2 = tmp_env2->flags;
207 if (!crc1_ok && !crc2_ok)
209 else if (crc1_ok && !crc2_ok) {
211 memcpy(env_ptr, tmp_env1, CONFIG_ENV_SIZE);
212 } else if (!crc1_ok && crc2_ok) {
214 memcpy(env_ptr, tmp_env2, CONFIG_ENV_SIZE);
216 } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) {
218 memcpy(env_ptr, tmp_env1, CONFIG_ENV_SIZE);
219 } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) {
221 memcpy(env_ptr, tmp_env2, CONFIG_ENV_SIZE);
223 } else if (flag1 == flag2) {
225 memcpy(env_ptr, tmp_env1, CONFIG_ENV_SIZE);
227 } else if (flag1 == 0xFF) {
229 memcpy(env_ptr, tmp_env1, CONFIG_ENV_SIZE);
233 * this differs from code in env_flash.c, but I think a sane
234 * default path is desirable.
237 memcpy(env_ptr, tmp_env2, CONFIG_ENV_SIZE);
241 if (current_env == 1) {
242 if (flag2 != OBSOLETE_FLAG) {
243 flag2 = OBSOLETE_FLAG;
244 spi_flash_write(env_flash,
245 env_new_offset + offsetof(env_t, flags),
246 sizeof(env_ptr->flags), &flag2);
248 if (flag1 != ACTIVE_FLAG) {
250 spi_flash_write(env_flash,
251 env_offset + offsetof(env_t, flags),
252 sizeof(env_ptr->flags), &flag1);
254 } else if (current_env == 2) {
255 if (flag1 != OBSOLETE_FLAG) {
256 flag1 = OBSOLETE_FLAG;
257 spi_flash_write(env_flash,
258 env_new_offset + offsetof(env_t, flags),
259 sizeof(env_ptr->flags), &flag1);
261 if (flag2 != ACTIVE_FLAG) {
263 spi_flash_write(env_flash,
264 env_offset + offsetof(env_t, flags),
265 sizeof(env_ptr->flags), &flag2);
268 if (gd->env_valid == 2) {
269 puts("*** Warning - some problems detected "
270 "reading environment; recovered successfully\n\n");
279 spi_flash_free(env_flash);
283 puts("*** Warning - bad CRC, using default environment\n\n");
294 u32 saved_size, saved_offset;
295 char *saved_buffer = NULL;
300 puts("Environment SPI flash not initialized\n");
304 /* Is the sector larger than the env (i.e. embedded) */
305 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
306 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
307 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
308 saved_buffer = malloc(saved_size);
313 ret = spi_flash_read(env_flash, saved_offset, saved_size, saved_buffer);
318 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
319 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
320 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
324 puts("Erasing SPI flash...");
325 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET, sector * CONFIG_ENV_SECT_SIZE);
329 puts("Writing to SPI flash...");
330 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, env_ptr);
334 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
335 ret = spi_flash_write(env_flash, saved_offset, saved_size, saved_buffer);
349 void env_relocate_spec(void)
353 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
354 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
358 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, env_ptr);
362 if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc)
370 spi_flash_free(env_flash);
374 puts("*** Warning - bad CRC, using default environment\n\n");
382 /* SPI flash isn't usable before relocation */
383 gd->env_addr = (ulong)&default_environment[0];