blob: 9f806fb090f54964566bc5bd4fef89068d0a4222 [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 *
Jagannadha Sutradharudu Tekib1df9222013-07-29 22:12:52 +053010 * SPDX-License-Identifier: GPL-2.0+
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020011 */
12#include <common.h>
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020013#include <environment.h>
Mike Frysinger9af10d52008-12-11 06:23:37 -050014#include <malloc.h>
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020015#include <spi_flash.h>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020016#include <search.h>
17#include <errno.h>
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020018
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020019#ifndef CONFIG_ENV_SPI_BUS
20# define CONFIG_ENV_SPI_BUS 0
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020021#endif
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020022#ifndef CONFIG_ENV_SPI_CS
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000023# define CONFIG_ENV_SPI_CS 0
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020024#endif
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020025#ifndef CONFIG_ENV_SPI_MAX_HZ
26# define CONFIG_ENV_SPI_MAX_HZ 1000000
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020027#endif
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +020028#ifndef CONFIG_ENV_SPI_MODE
29# define CONFIG_ENV_SPI_MODE SPI_MODE_3
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020030#endif
31
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020032#ifdef CONFIG_ENV_OFFSET_REDUND
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000033static ulong env_offset = CONFIG_ENV_OFFSET;
34static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020035
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000036#define ACTIVE_FLAG 1
37#define OBSOLETE_FLAG 0
Stefano Babic5f6c2862010-10-27 11:06:20 +020038#endif /* CONFIG_ENV_OFFSET_REDUND */
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020039
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020040DECLARE_GLOBAL_DATA_PTR;
41
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000042char *env_name_spec = "SPI Flash";
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020043
44static struct spi_flash *env_flash;
45
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020046#if defined(CONFIG_ENV_OFFSET_REDUND)
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020047int saveenv(void)
48{
Tom Rinia840b8e2013-04-05 14:55:21 -040049 env_t env_new;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020050 ssize_t len;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000051 char *res, *saved_buffer = NULL, flag = OBSOLETE_FLAG;
52 u32 saved_size, saved_offset, sector = 1;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020053 int ret;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020054
55 if (!env_flash) {
Stefano Babic5f6c2862010-10-27 11:06:20 +020056 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
57 CONFIG_ENV_SPI_CS,
58 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
59 if (!env_flash) {
60 set_default_env("!spi_flash_probe() failed");
61 return 1;
62 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020063 }
64
Tom Rinia840b8e2013-04-05 14:55:21 -040065 res = (char *)&env_new.data;
Joe Hershberger79a905e2012-12-11 22:16:23 -060066 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020067 if (len < 0) {
68 error("Cannot export environment: errno = %d\n", errno);
69 return 1;
70 }
Tom Rinia840b8e2013-04-05 14:55:21 -040071 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
72 env_new.flags = ACTIVE_FLAG;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020073
Stefano Babic5f6c2862010-10-27 11:06:20 +020074 if (gd->env_valid == 1) {
75 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
76 env_offset = CONFIG_ENV_OFFSET;
77 } else {
78 env_new_offset = CONFIG_ENV_OFFSET;
79 env_offset = CONFIG_ENV_OFFSET_REDUND;
80 }
81
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020082 /* Is the sector larger than the env (i.e. embedded) */
83 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
84 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
85 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
86 saved_buffer = malloc(saved_size);
87 if (!saved_buffer) {
88 ret = 1;
89 goto done;
90 }
91 ret = spi_flash_read(env_flash, saved_offset,
92 saved_size, saved_buffer);
93 if (ret)
94 goto done;
95 }
96
97 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
98 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
99 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
100 sector++;
101 }
102
103 puts("Erasing SPI flash...");
104 ret = spi_flash_erase(env_flash, env_new_offset,
105 sector * CONFIG_ENV_SECT_SIZE);
106 if (ret)
107 goto done;
108
109 puts("Writing to SPI flash...");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200110
Stefano Babic5f6c2862010-10-27 11:06:20 +0200111 ret = spi_flash_write(env_flash, env_new_offset,
Tom Rinia840b8e2013-04-05 14:55:21 -0400112 CONFIG_ENV_SIZE, &env_new);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200113 if (ret)
114 goto done;
115
116 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
117 ret = spi_flash_write(env_flash, saved_offset,
118 saved_size, saved_buffer);
119 if (ret)
120 goto done;
121 }
122
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000123 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
Tom Rinia840b8e2013-04-05 14:55:21 -0400124 sizeof(env_new.flags), &flag);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200125 if (ret)
126 goto done;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200127
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200128 puts("done\n");
129
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000130 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200131
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000132 printf("Valid environment: %d\n", (int)gd->env_valid);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200133
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200134 done:
135 if (saved_buffer)
136 free(saved_buffer);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000137
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200138 return ret;
139}
140
141void env_relocate_spec(void)
142{
143 int ret;
144 int crc1_ok = 0, crc2_ok = 0;
145 env_t *tmp_env1 = NULL;
146 env_t *tmp_env2 = NULL;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200147 env_t *ep = NULL;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200148
149 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200150 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200151
152 if (!tmp_env1 || !tmp_env2) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200153 set_default_env("!malloc() failed");
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000154 goto out;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200155 }
156
157 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
158 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200159 if (!env_flash) {
160 set_default_env("!spi_flash_probe() failed");
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000161 goto out;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200162 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200163
164 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
165 CONFIG_ENV_SIZE, tmp_env1);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200166 if (ret) {
167 set_default_env("!spi_flash_read() failed");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200168 goto err_read;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200169 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200170
171 if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc)
172 crc1_ok = 1;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200173
174 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
175 CONFIG_ENV_SIZE, tmp_env2);
176 if (!ret) {
177 if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc)
178 crc2_ok = 1;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200179 }
180
Stefano Babic5f6c2862010-10-27 11:06:20 +0200181 if (!crc1_ok && !crc2_ok) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200182 set_default_env("!bad CRC");
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000183 goto err_read;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200184 } else if (crc1_ok && !crc2_ok) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200185 gd->env_valid = 1;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200186 } else if (!crc1_ok && crc2_ok) {
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000187 gd->env_valid = 2;
188 } else if (tmp_env1->flags == ACTIVE_FLAG &&
189 tmp_env2->flags == OBSOLETE_FLAG) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200190 gd->env_valid = 1;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000191 } else if (tmp_env1->flags == OBSOLETE_FLAG &&
192 tmp_env2->flags == ACTIVE_FLAG) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200193 gd->env_valid = 2;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000194 } else if (tmp_env1->flags == tmp_env2->flags) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200195 gd->env_valid = 2;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000196 } else if (tmp_env1->flags == 0xFF) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200197 gd->env_valid = 2;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200198 } else {
199 /*
200 * this differs from code in env_flash.c, but I think a sane
201 * default path is desirable.
202 */
203 gd->env_valid = 2;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200204 }
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200205
Stefano Babic5f6c2862010-10-27 11:06:20 +0200206 if (gd->env_valid == 1)
207 ep = tmp_env1;
208 else
209 ep = tmp_env2;
210
211 ret = env_import((char *)ep, 0);
212 if (!ret) {
213 error("Cannot import environment: errno = %d\n", errno);
214 set_default_env("env_import failed");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200215 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200216
217err_read:
218 spi_flash_free(env_flash);
219 env_flash = NULL;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200220out:
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200221 free(tmp_env1);
222 free(tmp_env2);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200223}
224#else
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200225int saveenv(void)
226{
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000227 u32 saved_size, saved_offset, sector = 1;
228 char *res, *saved_buffer = NULL;
229 int ret = 1;
Tom Rinia840b8e2013-04-05 14:55:21 -0400230 env_t env_new;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200231 ssize_t len;
TsiChung Liew08792d72008-08-06 19:37:17 -0500232
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200233 if (!env_flash) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200234 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
235 CONFIG_ENV_SPI_CS,
236 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
237 if (!env_flash) {
238 set_default_env("!spi_flash_probe() failed");
239 return 1;
240 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200241 }
242
Mike Frysinger9af10d52008-12-11 06:23:37 -0500243 /* Is the sector larger than the env (i.e. embedded) */
244 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
245 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
246 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
247 saved_buffer = malloc(saved_size);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000248 if (!saved_buffer)
Mike Frysinger9af10d52008-12-11 06:23:37 -0500249 goto done;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000250
Stefano Babic5f6c2862010-10-27 11:06:20 +0200251 ret = spi_flash_read(env_flash, saved_offset,
252 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500253 if (ret)
254 goto done;
255 }
256
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200257 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
258 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
259 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
TsiChung Liew08792d72008-08-06 19:37:17 -0500260 sector++;
261 }
262
Tom Rinia840b8e2013-04-05 14:55:21 -0400263 res = (char *)&env_new.data;
Joe Hershberger79a905e2012-12-11 22:16:23 -0600264 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200265 if (len < 0) {
266 error("Cannot export environment: errno = %d\n", errno);
267 goto done;
268 }
Tom Rinia840b8e2013-04-05 14:55:21 -0400269 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200270
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200271 puts("Erasing SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200272 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
273 sector * CONFIG_ENV_SECT_SIZE);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500274 if (ret)
275 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200276
277 puts("Writing to SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200278 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
Tom Rinia840b8e2013-04-05 14:55:21 -0400279 CONFIG_ENV_SIZE, &env_new);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500280 if (ret)
281 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200282
Mike Frysinger9af10d52008-12-11 06:23:37 -0500283 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200284 ret = spi_flash_write(env_flash, saved_offset,
285 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500286 if (ret)
287 goto done;
288 }
289
290 ret = 0;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200291 puts("done\n");
Mike Frysinger9af10d52008-12-11 06:23:37 -0500292
293 done:
294 if (saved_buffer)
295 free(saved_buffer);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000296
Mike Frysinger9af10d52008-12-11 06:23:37 -0500297 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200298}
299
300void env_relocate_spec(void)
301{
Tom Rinia840b8e2013-04-05 14:55:21 -0400302 char buf[CONFIG_ENV_SIZE];
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200303 int ret;
304
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200305 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
306 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200307 if (!env_flash) {
308 set_default_env("!spi_flash_probe() failed");
309 return;
310 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200311
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200312 ret = spi_flash_read(env_flash,
313 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
314 if (ret) {
315 set_default_env("!spi_flash_read() failed");
316 goto out;
317 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200318
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200319 ret = env_import(buf, 1);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200320 if (ret)
321 gd->env_valid = 1;
322out:
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200323 spi_flash_free(env_flash);
324 env_flash = NULL;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200325}
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200326#endif
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200327
328int env_init(void)
329{
330 /* SPI flash isn't usable before relocation */
331 gd->env_addr = (ulong)&default_environment[0];
332 gd->env_valid = 1;
333
334 return 0;
335}