blob: fb0c39b3ce8d654ea42a62acb4e8ced6d5af5f32 [file] [log] [blame]
Haavard Skinnemoen8c664972008-05-16 11:10:35 +02001/*
Wolfgang Denkea882ba2010-06-20 23:33:59 +02002 * (C) Copyright 2000-2010
Haavard Skinnemoen8c664972008-05-16 11:10:35 +02003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6 * Andreas Heppel <aheppel@sysgo.de>
7 *
8 * (C) Copyright 2008 Atmel Corporation
9 *
10 * See file CREDITS for list of people who contributed to this
11 * project.
12 *
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.
17 *
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.
22 *
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,
26 * MA 02111-1307 USA
27 */
28#include <common.h>
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020029#include <environment.h>
Mike Frysinger5b3375a2008-12-11 06:23:37 -050030#include <malloc.h>
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020031#include <spi_flash.h>
Wolfgang Denkea882ba2010-06-20 23:33:59 +020032#include <search.h>
33#include <errno.h>
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020034
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020035#ifndef CONFIG_ENV_SPI_BUS
36# define CONFIG_ENV_SPI_BUS 0
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020037#endif
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020038#ifndef CONFIG_ENV_SPI_CS
39# define CONFIG_ENV_SPI_CS 0
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020040#endif
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020041#ifndef CONFIG_ENV_SPI_MAX_HZ
42# define CONFIG_ENV_SPI_MAX_HZ 1000000
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020043#endif
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020044#ifndef CONFIG_ENV_SPI_MODE
45# define CONFIG_ENV_SPI_MODE SPI_MODE_3
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020046#endif
47
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020048#ifdef CONFIG_ENV_OFFSET_REDUND
49static ulong env_offset = CONFIG_ENV_OFFSET;
50static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
51
52#define ACTIVE_FLAG 1
53#define OBSOLETE_FLAG 0
54#endif /* CONFIG_ENV_ADDR_REDUND */
55
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020056DECLARE_GLOBAL_DATA_PTR;
57
58/* references to names in env_common.c */
59extern uchar default_environment[];
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020060
61char * env_name_spec = "SPI Flash";
62env_t *env_ptr;
63
64static struct spi_flash *env_flash;
65
66uchar env_get_char_spec(int index)
67{
68 return *((uchar *)(gd->env_addr + index));
69}
70
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020071#if defined(CONFIG_ENV_OFFSET_REDUND)
72void swap_env(void)
73{
74 ulong tmp_offset = env_offset;
75
76 env_offset = env_new_offset;
77 env_new_offset = tmp_offset;
78}
79
80int saveenv(void)
81{
Wolfgang Denkea882ba2010-06-20 23:33:59 +020082 env_t env_new;
83 ssize_t len;
84 char *res;
85 u32 saved_size, saved_offset;
86 char *saved_buffer = NULL;
87 u32 sector = 1;
88 int ret;
89 char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020090
91 if (!env_flash) {
92 puts("Environment SPI flash not initialized\n");
93 return 1;
94 }
95
Wolfgang Denkea882ba2010-06-20 23:33:59 +020096 res = (char *)&env_new.data;
97 len = hexport('\0', &res, ENV_SIZE);
98 if (len < 0) {
99 error("Cannot export environment: errno = %d\n", errno);
100 return 1;
101 }
102 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
103 env_new.flags = ACTIVE_FLAG;
104
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200105 /* Is the sector larger than the env (i.e. embedded) */
106 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
107 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
108 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
109 saved_buffer = malloc(saved_size);
110 if (!saved_buffer) {
111 ret = 1;
112 goto done;
113 }
114 ret = spi_flash_read(env_flash, saved_offset,
115 saved_size, saved_buffer);
116 if (ret)
117 goto done;
118 }
119
120 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
121 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
122 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
123 sector++;
124 }
125
126 puts("Erasing SPI flash...");
127 ret = spi_flash_erase(env_flash, env_new_offset,
128 sector * CONFIG_ENV_SECT_SIZE);
129 if (ret)
130 goto done;
131
132 puts("Writing to SPI flash...");
133 ret = spi_flash_write(env_flash,
134 env_new_offset + offsetof(env_t, data),
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200135 sizeof(env_new.data), env_new.data);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200136 if (ret)
137 goto done;
138
139 ret = spi_flash_write(env_flash,
140 env_new_offset + offsetof(env_t, crc),
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200141 sizeof(env_new.crc), &env_new.crc);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200142 if (ret)
143 goto done;
144
145 ret = spi_flash_write(env_flash,
146 env_offset + offsetof(env_t, flags),
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200147 sizeof(env_new.flags), &flag);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200148 if (ret)
149 goto done;
150
151 ret = spi_flash_write(env_flash,
152 env_new_offset + offsetof(env_t, flags),
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200153 sizeof(env_new.flags), &new_flag);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200154 if (ret)
155 goto done;
156
157 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
158 ret = spi_flash_write(env_flash, saved_offset,
159 saved_size, saved_buffer);
160 if (ret)
161 goto done;
162 }
163
164 swap_env();
165
166 ret = 0;
167 puts("done\n");
168
169 done:
170 if (saved_buffer)
171 free(saved_buffer);
172 return ret;
173}
174
175void env_relocate_spec(void)
176{
177 int ret;
178 int crc1_ok = 0, crc2_ok = 0;
179 env_t *tmp_env1 = NULL;
180 env_t *tmp_env2 = NULL;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200181 env_t ep;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200182 uchar flag1, flag2;
183 /* current_env is set only in case both areas are valid! */
184 int current_env = 0;
185
186 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200187 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200188
189 if (!tmp_env1 || !tmp_env2) {
190 free(tmp_env1);
191 free(tmp_env2);
192 set_default_env("!malloc() failed");
193 return;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200194 }
195
196 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
197 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200198 if (!env_flash) {
199 set_default_env("!spi_flash_probe() failed");
200 return;
201 }
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200202
203 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
204 CONFIG_ENV_SIZE, tmp_env1);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200205 if (ret) {
206 set_default_env("!spi_flash_read() failed");
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200207 goto err_read;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200208 }
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200209
210 if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc)
211 crc1_ok = 1;
212 flag1 = tmp_env1->flags;
213
214 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
215 CONFIG_ENV_SIZE, tmp_env2);
216 if (!ret) {
217 if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc)
218 crc2_ok = 1;
219 flag2 = tmp_env2->flags;
220 }
221
222 if (!crc1_ok && !crc2_ok)
223 goto err_crc;
224 else if (crc1_ok && !crc2_ok) {
225 gd->env_valid = 1;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200226 ep = tmp_env1;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200227 } else if (!crc1_ok && crc2_ok) {
228 gd->env_valid = 1;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200229 ep = tmp_env2;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200230 swap_env();
231 } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) {
232 gd->env_valid = 1;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200233 ep = tmp_env1;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200234 } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) {
235 gd->env_valid = 1;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200236 ep = tmp_env2;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200237 swap_env();
238 } else if (flag1 == flag2) {
239 gd->env_valid = 2;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200240 ep = tmp_env1;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200241 current_env = 1;
242 } else if (flag1 == 0xFF) {
243 gd->env_valid = 2;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200244 ep = tmp_env1;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200245 current_env = 1;
246 } else {
247 /*
248 * this differs from code in env_flash.c, but I think a sane
249 * default path is desirable.
250 */
251 gd->env_valid = 2;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200252 ep = tmp_env2;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200253 swap_env();
254 current_env = 2;
255 }
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200256
257 rc = env_import((char *)ep, 0);
258 if (!rc) {
259 error("Cannot import environment: errno = %d\n", errno);
260 goto out;
261 }
262
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200263 if (current_env == 1) {
264 if (flag2 != OBSOLETE_FLAG) {
265 flag2 = OBSOLETE_FLAG;
266 spi_flash_write(env_flash,
267 env_new_offset + offsetof(env_t, flags),
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200268 sizeof(env_new.flags), &flag2);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200269 }
270 if (flag1 != ACTIVE_FLAG) {
271 flag1 = ACTIVE_FLAG;
272 spi_flash_write(env_flash,
273 env_offset + offsetof(env_t, flags),
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200274 sizeof(env_new.flags), &flag1);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200275 }
276 } else if (current_env == 2) {
277 if (flag1 != OBSOLETE_FLAG) {
278 flag1 = OBSOLETE_FLAG;
279 spi_flash_write(env_flash,
280 env_new_offset + offsetof(env_t, flags),
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200281 sizeof(env_new.flags), &flag1);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200282 }
283 if (flag2 != ACTIVE_FLAG) {
284 flag2 = ACTIVE_FLAG;
285 spi_flash_write(env_flash,
286 env_offset + offsetof(env_t, flags),
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200287 sizeof(env_new.flags), &flag2);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200288 }
289 }
290 if (gd->env_valid == 2) {
291 puts("*** Warning - some problems detected "
292 "reading environment; recovered successfully\n\n");
293 }
294 if (tmp_env1)
295 free(tmp_env1);
296 if (tmp_env2)
297 free(tmp_env2);
298 return;
299
300err_read:
301 spi_flash_free(env_flash);
302 env_flash = NULL;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200303out:
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200304 free(tmp_env1);
305 free(tmp_env2);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200306}
307#else
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200308int saveenv(void)
309{
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500310 u32 saved_size, saved_offset;
311 char *saved_buffer = NULL;
TsiChung Liew07efc9e2008-08-06 19:37:17 -0500312 u32 sector = 1;
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500313 int ret;
TsiChung Liew07efc9e2008-08-06 19:37:17 -0500314
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200315 if (!env_flash) {
316 puts("Environment SPI flash not initialized\n");
317 return 1;
318 }
319
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500320 /* Is the sector larger than the env (i.e. embedded) */
321 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
322 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
323 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
324 saved_buffer = malloc(saved_size);
325 if (!saved_buffer) {
326 ret = 1;
327 goto done;
328 }
329 ret = spi_flash_read(env_flash, saved_offset, saved_size, saved_buffer);
330 if (ret)
331 goto done;
332 }
333
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200334 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
335 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
336 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
TsiChung Liew07efc9e2008-08-06 19:37:17 -0500337 sector++;
338 }
339
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200340 puts("Erasing SPI flash...");
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500341 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET, sector * CONFIG_ENV_SECT_SIZE);
342 if (ret)
343 goto done;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200344
345 puts("Writing to SPI flash...");
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500346 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, env_ptr);
347 if (ret)
348 goto done;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200349
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500350 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
351 ret = spi_flash_write(env_flash, saved_offset, saved_size, saved_buffer);
352 if (ret)
353 goto done;
354 }
355
356 ret = 0;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200357 puts("done\n");
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500358
359 done:
360 if (saved_buffer)
361 free(saved_buffer);
362 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200363}
364
365void env_relocate_spec(void)
366{
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200367 char buf[CONFIG_ENV_SIZE];
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200368 int ret;
369
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200370 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
371 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200372 if (!env_flash) {
373 set_default_env("!spi_flash_probe() failed");
374 return;
375 }
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200376
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200377 ret = spi_flash_read(env_flash,
378 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
379 if (ret) {
380 set_default_env("!spi_flash_read() failed");
381 goto out;
382 }
383
384 ret = env_import(buf, 1);
385
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200386 if (ret)
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200387 gd->env_valid = 1;
388out:
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200389 spi_flash_free(env_flash);
390 env_flash = NULL;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200391}
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200392#endif
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200393
394int env_init(void)
395{
396 /* SPI flash isn't usable before relocation */
397 gd->env_addr = (ulong)&default_environment[0];
398 gd->env_valid = 1;
399
400 return 0;
401}