blob: 41cc00aeabb141d83f37039af054136944a7ad6a [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
Stefano Babica3110f02010-10-27 11:06:20 +020054#endif /* CONFIG_ENV_OFFSET_REDUND */
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020055
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)
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020072
73int saveenv(void)
74{
Wolfgang Denkea882ba2010-06-20 23:33:59 +020075 env_t env_new;
76 ssize_t len;
77 char *res;
78 u32 saved_size, saved_offset;
79 char *saved_buffer = NULL;
80 u32 sector = 1;
81 int ret;
82 char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020083
84 if (!env_flash) {
Stefano Babica3110f02010-10-27 11:06:20 +020085 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
86 CONFIG_ENV_SPI_CS,
87 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
88 if (!env_flash) {
89 set_default_env("!spi_flash_probe() failed");
90 return 1;
91 }
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020092 }
93
Wolfgang Denkea882ba2010-06-20 23:33:59 +020094 res = (char *)&env_new.data;
Mike Frysinger2eb15732010-12-08 06:26:04 -050095 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +020096 if (len < 0) {
97 error("Cannot export environment: errno = %d\n", errno);
98 return 1;
99 }
100 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
101 env_new.flags = ACTIVE_FLAG;
102
Stefano Babica3110f02010-10-27 11:06:20 +0200103 if (gd->env_valid == 1) {
104 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
105 env_offset = CONFIG_ENV_OFFSET;
106 } else {
107 env_new_offset = CONFIG_ENV_OFFSET;
108 env_offset = CONFIG_ENV_OFFSET_REDUND;
109 }
110
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200111 /* 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);
116 if (!saved_buffer) {
117 ret = 1;
118 goto done;
119 }
120 ret = spi_flash_read(env_flash, saved_offset,
121 saved_size, saved_buffer);
122 if (ret)
123 goto done;
124 }
125
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)
129 sector++;
130 }
131
132 puts("Erasing SPI flash...");
133 ret = spi_flash_erase(env_flash, env_new_offset,
134 sector * CONFIG_ENV_SECT_SIZE);
135 if (ret)
136 goto done;
137
138 puts("Writing to SPI flash...");
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200139
Stefano Babica3110f02010-10-27 11:06:20 +0200140 ret = spi_flash_write(env_flash, env_new_offset,
141 CONFIG_ENV_SIZE, &env_new);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200142 if (ret)
143 goto done;
144
145 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
146 ret = spi_flash_write(env_flash, saved_offset,
147 saved_size, saved_buffer);
148 if (ret)
149 goto done;
150 }
151
Stefano Babica3110f02010-10-27 11:06:20 +0200152 ret = spi_flash_write(env_flash,
153 env_offset + offsetof(env_t, flags),
154 sizeof(env_new.flags), &flag);
155 if (ret)
156 goto done;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200157
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200158 puts("done\n");
159
Stefano Babica3110f02010-10-27 11:06:20 +0200160 gd->env_valid = (gd->env_valid == 2 ? 1 : 2);
161
162 printf("Valid environment: %d\n", gd->env_valid);
163
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200164 done:
165 if (saved_buffer)
166 free(saved_buffer);
167 return ret;
168}
169
170void env_relocate_spec(void)
171{
172 int ret;
173 int crc1_ok = 0, crc2_ok = 0;
174 env_t *tmp_env1 = NULL;
175 env_t *tmp_env2 = NULL;
Stefano Babica3110f02010-10-27 11:06:20 +0200176 env_t *ep = NULL;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200177 uchar flag1, flag2;
178 /* current_env is set only in case both areas are valid! */
179 int current_env = 0;
180
181 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200182 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200183
184 if (!tmp_env1 || !tmp_env2) {
185 free(tmp_env1);
186 free(tmp_env2);
187 set_default_env("!malloc() failed");
188 return;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200189 }
190
191 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
192 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200193 if (!env_flash) {
194 set_default_env("!spi_flash_probe() failed");
195 return;
196 }
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200197
198 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
199 CONFIG_ENV_SIZE, tmp_env1);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200200 if (ret) {
201 set_default_env("!spi_flash_read() failed");
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200202 goto err_read;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200203 }
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200204
205 if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc)
206 crc1_ok = 1;
207 flag1 = tmp_env1->flags;
208
209 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
210 CONFIG_ENV_SIZE, tmp_env2);
211 if (!ret) {
212 if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc)
213 crc2_ok = 1;
214 flag2 = tmp_env2->flags;
215 }
216
Stefano Babica3110f02010-10-27 11:06:20 +0200217 if (!crc1_ok && !crc2_ok) {
218 free(tmp_env1);
219 free(tmp_env2);
220 set_default_env("!bad CRC");
221 return;
222 } else if (crc1_ok && !crc2_ok) {
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200223 gd->env_valid = 1;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200224 ep = tmp_env1;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200225 } else if (!crc1_ok && crc2_ok) {
226 gd->env_valid = 1;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200227 } else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) {
228 gd->env_valid = 1;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200229 } else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) {
Stefano Babica3110f02010-10-27 11:06:20 +0200230 gd->env_valid = 2;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200231 } else if (flag1 == flag2) {
232 gd->env_valid = 2;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200233 } else if (flag1 == 0xFF) {
234 gd->env_valid = 2;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200235 } else {
236 /*
237 * this differs from code in env_flash.c, but I think a sane
238 * default path is desirable.
239 */
240 gd->env_valid = 2;
Stefano Babica3110f02010-10-27 11:06:20 +0200241 }
242
243 free(env_ptr);
244
245 if (gd->env_valid == 1)
246 ep = tmp_env1;
247 else
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200248 ep = tmp_env2;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200249
Stefano Babica3110f02010-10-27 11:06:20 +0200250 ret = env_import((char *)ep, 0);
251 if (!ret) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200252 error("Cannot import environment: errno = %d\n", errno);
Stefano Babica3110f02010-10-27 11:06:20 +0200253 set_default_env("env_import failed");
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200254 }
255
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200256err_read:
257 spi_flash_free(env_flash);
258 env_flash = NULL;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200259out:
Stefano Babica3110f02010-10-27 11:06:20 +0200260 if (tmp_env1)
261 free(tmp_env1);
262 if (tmp_env2)
263 free(tmp_env2);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200264 free(tmp_env1);
265 free(tmp_env2);
Stefano Babica3110f02010-10-27 11:06:20 +0200266
267 return;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200268}
269#else
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200270int saveenv(void)
271{
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500272 u32 saved_size, saved_offset;
273 char *saved_buffer = NULL;
TsiChung Liew07efc9e2008-08-06 19:37:17 -0500274 u32 sector = 1;
Stefano Babic590084a2010-10-29 11:49:45 +0200275 int ret = 1;
Stefano Babica3110f02010-10-27 11:06:20 +0200276 env_t env_new;
277 char *res;
278 ssize_t len;
TsiChung Liew07efc9e2008-08-06 19:37:17 -0500279
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200280 if (!env_flash) {
Stefano Babica3110f02010-10-27 11:06:20 +0200281 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
282 CONFIG_ENV_SPI_CS,
283 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
284 if (!env_flash) {
285 set_default_env("!spi_flash_probe() failed");
286 return 1;
287 }
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200288 }
289
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500290 /* 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);
295 if (!saved_buffer) {
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500296 goto done;
297 }
Stefano Babica3110f02010-10-27 11:06:20 +0200298 ret = spi_flash_read(env_flash, saved_offset,
299 saved_size, saved_buffer);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500300 if (ret)
301 goto done;
302 }
303
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200304 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)
TsiChung Liew07efc9e2008-08-06 19:37:17 -0500307 sector++;
308 }
309
Stefano Babica3110f02010-10-27 11:06:20 +0200310 res = (char *)&env_new.data;
Mike Frysinger2eb15732010-12-08 06:26:04 -0500311 len = hexport_r(&env_htab, '\0', &res, ENV_SIZE);
Stefano Babica3110f02010-10-27 11:06:20 +0200312 if (len < 0) {
313 error("Cannot export environment: errno = %d\n", errno);
314 goto done;
315 }
316 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
317
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200318 puts("Erasing SPI flash...");
Stefano Babica3110f02010-10-27 11:06:20 +0200319 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
320 sector * CONFIG_ENV_SECT_SIZE);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500321 if (ret)
322 goto done;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200323
324 puts("Writing to SPI flash...");
Stefano Babica3110f02010-10-27 11:06:20 +0200325 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
326 CONFIG_ENV_SIZE, &env_new);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500327 if (ret)
328 goto done;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200329
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500330 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
Stefano Babica3110f02010-10-27 11:06:20 +0200331 ret = spi_flash_write(env_flash, saved_offset,
332 saved_size, saved_buffer);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500333 if (ret)
334 goto done;
335 }
336
337 ret = 0;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200338 puts("done\n");
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500339
340 done:
341 if (saved_buffer)
342 free(saved_buffer);
343 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200344}
345
346void env_relocate_spec(void)
347{
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200348 char buf[CONFIG_ENV_SIZE];
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200349 int ret;
350
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200351 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
352 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200353 if (!env_flash) {
354 set_default_env("!spi_flash_probe() failed");
355 return;
356 }
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200357
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200358 ret = spi_flash_read(env_flash,
359 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
360 if (ret) {
361 set_default_env("!spi_flash_read() failed");
362 goto out;
363 }
364
365 ret = env_import(buf, 1);
366
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200367 if (ret)
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200368 gd->env_valid = 1;
369out:
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200370 spi_flash_free(env_flash);
371 env_flash = NULL;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200372}
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200373#endif
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200374
375int env_init(void)
376{
377 /* SPI flash isn't usable before relocation */
378 gd->env_addr = (ulong)&default_environment[0];
379 gd->env_valid = 1;
380
381 return 0;
382}