blob: 9a592ba9560c787ccba064b5583b386bb9e6cea6 [file] [log] [blame]
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +02001/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02002 * (C) Copyright 2000-2010
Haavard Skinnemoeneb8951c2008-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 Skinnemoeneb8951c2008-05-16 11:10:35 +020029#include <environment.h>
Mike Frysinger9af10d52008-12-11 06:23:37 -050030#include <malloc.h>
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020031#include <spi_flash.h>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020032#include <search.h>
33#include <errno.h>
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020034
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020035#ifndef CONFIG_ENV_SPI_BUS
36# define CONFIG_ENV_SPI_BUS 0
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020037#endif
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020038#ifndef CONFIG_ENV_SPI_CS
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000039# define CONFIG_ENV_SPI_CS 0
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020040#endif
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020041#ifndef CONFIG_ENV_SPI_MAX_HZ
42# define CONFIG_ENV_SPI_MAX_HZ 1000000
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020043#endif
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020044#ifndef CONFIG_ENV_SPI_MODE
45# define CONFIG_ENV_SPI_MODE SPI_MODE_3
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020046#endif
47
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020048#ifdef CONFIG_ENV_OFFSET_REDUND
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000049static ulong env_offset = CONFIG_ENV_OFFSET;
50static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020051
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000052#define ACTIVE_FLAG 1
53#define OBSOLETE_FLAG 0
Stefano Babic5f6c2862010-10-27 11:06:20 +020054#endif /* CONFIG_ENV_OFFSET_REDUND */
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020055
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020056DECLARE_GLOBAL_DATA_PTR;
57
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000058char *env_name_spec = "SPI Flash";
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020059
60static struct spi_flash *env_flash;
Rob Herring04cae822013-03-22 11:26:21 +000061static char env_buf[CONFIG_ENV_SIZE];
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020062
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020063#if defined(CONFIG_ENV_OFFSET_REDUND)
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020064int saveenv(void)
65{
Rob Herring04cae822013-03-22 11:26:21 +000066 env_t *env_new = (env_t *)env_buf;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020067 ssize_t len;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000068 char *res, *saved_buffer = NULL, flag = OBSOLETE_FLAG;
69 u32 saved_size, saved_offset, sector = 1;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020070 int ret;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020071
72 if (!env_flash) {
Stefano Babic5f6c2862010-10-27 11:06:20 +020073 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
74 CONFIG_ENV_SPI_CS,
75 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
76 if (!env_flash) {
77 set_default_env("!spi_flash_probe() failed");
78 return 1;
79 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020080 }
81
Rob Herring04cae822013-03-22 11:26:21 +000082 res = (char *)env_new->data;
Joe Hershberger79a905e2012-12-11 22:16:23 -060083 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020084 if (len < 0) {
85 error("Cannot export environment: errno = %d\n", errno);
86 return 1;
87 }
Rob Herring04cae822013-03-22 11:26:21 +000088 env_new->crc = crc32(0, env_new->data, ENV_SIZE);
89 env_new->flags = ACTIVE_FLAG;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020090
Stefano Babic5f6c2862010-10-27 11:06:20 +020091 if (gd->env_valid == 1) {
92 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
93 env_offset = CONFIG_ENV_OFFSET;
94 } else {
95 env_new_offset = CONFIG_ENV_OFFSET;
96 env_offset = CONFIG_ENV_OFFSET_REDUND;
97 }
98
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020099 /* Is the sector larger than the env (i.e. embedded) */
100 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
101 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
102 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
103 saved_buffer = malloc(saved_size);
104 if (!saved_buffer) {
105 ret = 1;
106 goto done;
107 }
108 ret = spi_flash_read(env_flash, saved_offset,
109 saved_size, saved_buffer);
110 if (ret)
111 goto done;
112 }
113
114 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
115 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
116 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
117 sector++;
118 }
119
120 puts("Erasing SPI flash...");
121 ret = spi_flash_erase(env_flash, env_new_offset,
122 sector * CONFIG_ENV_SECT_SIZE);
123 if (ret)
124 goto done;
125
126 puts("Writing to SPI flash...");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200127
Stefano Babic5f6c2862010-10-27 11:06:20 +0200128 ret = spi_flash_write(env_flash, env_new_offset,
Rob Herring04cae822013-03-22 11:26:21 +0000129 CONFIG_ENV_SIZE, env_new);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200130 if (ret)
131 goto done;
132
133 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
134 ret = spi_flash_write(env_flash, saved_offset,
135 saved_size, saved_buffer);
136 if (ret)
137 goto done;
138 }
139
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000140 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
Rob Herring04cae822013-03-22 11:26:21 +0000141 sizeof(env_new->flags), &flag);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200142 if (ret)
143 goto done;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200144
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200145 puts("done\n");
146
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000147 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200148
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000149 printf("Valid environment: %d\n", (int)gd->env_valid);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200150
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200151 done:
152 if (saved_buffer)
153 free(saved_buffer);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000154
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200155 return ret;
156}
157
158void env_relocate_spec(void)
159{
160 int ret;
161 int crc1_ok = 0, crc2_ok = 0;
162 env_t *tmp_env1 = NULL;
163 env_t *tmp_env2 = NULL;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200164 env_t *ep = NULL;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200165
166 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200167 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200168
169 if (!tmp_env1 || !tmp_env2) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200170 set_default_env("!malloc() failed");
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000171 goto out;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200172 }
173
174 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
175 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200176 if (!env_flash) {
177 set_default_env("!spi_flash_probe() failed");
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000178 goto out;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200179 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200180
181 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
182 CONFIG_ENV_SIZE, tmp_env1);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200183 if (ret) {
184 set_default_env("!spi_flash_read() failed");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200185 goto err_read;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200186 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200187
188 if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc)
189 crc1_ok = 1;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200190
191 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
192 CONFIG_ENV_SIZE, tmp_env2);
193 if (!ret) {
194 if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc)
195 crc2_ok = 1;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200196 }
197
Stefano Babic5f6c2862010-10-27 11:06:20 +0200198 if (!crc1_ok && !crc2_ok) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200199 set_default_env("!bad CRC");
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000200 goto err_read;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200201 } else if (crc1_ok && !crc2_ok) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200202 gd->env_valid = 1;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200203 } else if (!crc1_ok && crc2_ok) {
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000204 gd->env_valid = 2;
205 } else if (tmp_env1->flags == ACTIVE_FLAG &&
206 tmp_env2->flags == OBSOLETE_FLAG) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200207 gd->env_valid = 1;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000208 } else if (tmp_env1->flags == OBSOLETE_FLAG &&
209 tmp_env2->flags == ACTIVE_FLAG) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200210 gd->env_valid = 2;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000211 } else if (tmp_env1->flags == tmp_env2->flags) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200212 gd->env_valid = 2;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000213 } else if (tmp_env1->flags == 0xFF) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200214 gd->env_valid = 2;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200215 } else {
216 /*
217 * this differs from code in env_flash.c, but I think a sane
218 * default path is desirable.
219 */
220 gd->env_valid = 2;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200221 }
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200222
Stefano Babic5f6c2862010-10-27 11:06:20 +0200223 if (gd->env_valid == 1)
224 ep = tmp_env1;
225 else
226 ep = tmp_env2;
227
228 ret = env_import((char *)ep, 0);
229 if (!ret) {
230 error("Cannot import environment: errno = %d\n", errno);
231 set_default_env("env_import failed");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200232 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200233
234err_read:
235 spi_flash_free(env_flash);
236 env_flash = NULL;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200237out:
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200238 free(tmp_env1);
239 free(tmp_env2);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200240}
241#else
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200242int saveenv(void)
243{
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000244 u32 saved_size, saved_offset, sector = 1;
245 char *res, *saved_buffer = NULL;
246 int ret = 1;
Rob Herring04cae822013-03-22 11:26:21 +0000247 env_t *env_new = (env_t *)env_buf;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200248 ssize_t len;
TsiChung Liew08792d72008-08-06 19:37:17 -0500249
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200250 if (!env_flash) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200251 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
252 CONFIG_ENV_SPI_CS,
253 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
254 if (!env_flash) {
255 set_default_env("!spi_flash_probe() failed");
256 return 1;
257 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200258 }
259
Mike Frysinger9af10d52008-12-11 06:23:37 -0500260 /* Is the sector larger than the env (i.e. embedded) */
261 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
262 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
263 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
264 saved_buffer = malloc(saved_size);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000265 if (!saved_buffer)
Mike Frysinger9af10d52008-12-11 06:23:37 -0500266 goto done;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000267
Stefano Babic5f6c2862010-10-27 11:06:20 +0200268 ret = spi_flash_read(env_flash, saved_offset,
269 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500270 if (ret)
271 goto done;
272 }
273
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200274 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
275 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
276 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
TsiChung Liew08792d72008-08-06 19:37:17 -0500277 sector++;
278 }
279
Rob Herring04cae822013-03-22 11:26:21 +0000280 res = (char *)env_new->data;
Joe Hershberger79a905e2012-12-11 22:16:23 -0600281 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200282 if (len < 0) {
283 error("Cannot export environment: errno = %d\n", errno);
284 goto done;
285 }
Rob Herring04cae822013-03-22 11:26:21 +0000286 env_new->crc = crc32(0, env_new->data, ENV_SIZE);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200287
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200288 puts("Erasing SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200289 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
290 sector * CONFIG_ENV_SECT_SIZE);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500291 if (ret)
292 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200293
294 puts("Writing to SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200295 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
Rob Herring04cae822013-03-22 11:26:21 +0000296 CONFIG_ENV_SIZE, env_new);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500297 if (ret)
298 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200299
Mike Frysinger9af10d52008-12-11 06:23:37 -0500300 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200301 ret = spi_flash_write(env_flash, saved_offset,
302 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500303 if (ret)
304 goto done;
305 }
306
307 ret = 0;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200308 puts("done\n");
Mike Frysinger9af10d52008-12-11 06:23:37 -0500309
310 done:
311 if (saved_buffer)
312 free(saved_buffer);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000313
Mike Frysinger9af10d52008-12-11 06:23:37 -0500314 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200315}
316
317void env_relocate_spec(void)
318{
Rob Herring04cae822013-03-22 11:26:21 +0000319 char *buf = env_buf;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200320 int ret;
321
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200322 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
323 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200324 if (!env_flash) {
325 set_default_env("!spi_flash_probe() failed");
326 return;
327 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200328
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200329 ret = spi_flash_read(env_flash,
330 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
331 if (ret) {
332 set_default_env("!spi_flash_read() failed");
333 goto out;
334 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200335
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200336 ret = env_import(buf, 1);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200337 if (ret)
338 gd->env_valid = 1;
339out:
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200340 spi_flash_free(env_flash);
341 env_flash = NULL;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200342}
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200343#endif
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200344
345int env_init(void)
346{
347 /* SPI flash isn't usable before relocation */
348 gd->env_addr = (ulong)&default_environment[0];
349 gd->env_valid = 1;
350
351 return 0;
352}