blob: 37ab13ae17842309339fa9a70a42e49b8669207e [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;
Marek Vasutd73c1292014-03-05 19:59:50 +010050 char *saved_buffer = NULL, flag = OBSOLETE_FLAG;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000051 u32 saved_size, saved_offset, sector = 1;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020052 int ret;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020053
54 if (!env_flash) {
Stefano Babic5f6c2862010-10-27 11:06:20 +020055 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
56 CONFIG_ENV_SPI_CS,
57 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
58 if (!env_flash) {
59 set_default_env("!spi_flash_probe() failed");
60 return 1;
61 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020062 }
63
Marek Vasutd73c1292014-03-05 19:59:50 +010064 ret = env_export(&env_new);
65 if (ret)
66 return ret;
Tom Rinia840b8e2013-04-05 14:55:21 -040067 env_new.flags = ACTIVE_FLAG;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020068
Stefano Babic5f6c2862010-10-27 11:06:20 +020069 if (gd->env_valid == 1) {
70 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
71 env_offset = CONFIG_ENV_OFFSET;
72 } else {
73 env_new_offset = CONFIG_ENV_OFFSET;
74 env_offset = CONFIG_ENV_OFFSET_REDUND;
75 }
76
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020077 /* Is the sector larger than the env (i.e. embedded) */
78 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
79 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
80 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
81 saved_buffer = malloc(saved_size);
82 if (!saved_buffer) {
83 ret = 1;
84 goto done;
85 }
86 ret = spi_flash_read(env_flash, saved_offset,
87 saved_size, saved_buffer);
88 if (ret)
89 goto done;
90 }
91
92 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
93 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
94 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
95 sector++;
96 }
97
98 puts("Erasing SPI flash...");
99 ret = spi_flash_erase(env_flash, env_new_offset,
100 sector * CONFIG_ENV_SECT_SIZE);
101 if (ret)
102 goto done;
103
104 puts("Writing to SPI flash...");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200105
Stefano Babic5f6c2862010-10-27 11:06:20 +0200106 ret = spi_flash_write(env_flash, env_new_offset,
Tom Rinia840b8e2013-04-05 14:55:21 -0400107 CONFIG_ENV_SIZE, &env_new);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200108 if (ret)
109 goto done;
110
111 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
112 ret = spi_flash_write(env_flash, saved_offset,
113 saved_size, saved_buffer);
114 if (ret)
115 goto done;
116 }
117
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000118 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
Tom Rinia840b8e2013-04-05 14:55:21 -0400119 sizeof(env_new.flags), &flag);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200120 if (ret)
121 goto done;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200122
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200123 puts("done\n");
124
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000125 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200126
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000127 printf("Valid environment: %d\n", (int)gd->env_valid);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200128
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200129 done:
130 if (saved_buffer)
131 free(saved_buffer);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000132
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200133 return ret;
134}
135
136void env_relocate_spec(void)
137{
138 int ret;
139 int crc1_ok = 0, crc2_ok = 0;
140 env_t *tmp_env1 = NULL;
141 env_t *tmp_env2 = NULL;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200142 env_t *ep = NULL;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200143
144 tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200145 tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200146
147 if (!tmp_env1 || !tmp_env2) {
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200148 set_default_env("!malloc() failed");
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000149 goto out;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200150 }
151
152 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
153 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200154 if (!env_flash) {
155 set_default_env("!spi_flash_probe() failed");
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000156 goto out;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200157 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200158
159 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
160 CONFIG_ENV_SIZE, tmp_env1);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200161 if (ret) {
162 set_default_env("!spi_flash_read() failed");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200163 goto err_read;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200164 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200165
166 if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc)
167 crc1_ok = 1;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200168
169 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
170 CONFIG_ENV_SIZE, tmp_env2);
171 if (!ret) {
172 if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc)
173 crc2_ok = 1;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200174 }
175
Stefano Babic5f6c2862010-10-27 11:06:20 +0200176 if (!crc1_ok && !crc2_ok) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200177 set_default_env("!bad CRC");
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000178 goto err_read;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200179 } else if (crc1_ok && !crc2_ok) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200180 gd->env_valid = 1;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200181 } else if (!crc1_ok && crc2_ok) {
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000182 gd->env_valid = 2;
183 } else if (tmp_env1->flags == ACTIVE_FLAG &&
184 tmp_env2->flags == OBSOLETE_FLAG) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200185 gd->env_valid = 1;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000186 } else if (tmp_env1->flags == OBSOLETE_FLAG &&
187 tmp_env2->flags == ACTIVE_FLAG) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200188 gd->env_valid = 2;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000189 } else if (tmp_env1->flags == tmp_env2->flags) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200190 gd->env_valid = 2;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000191 } else if (tmp_env1->flags == 0xFF) {
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200192 gd->env_valid = 2;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200193 } else {
194 /*
195 * this differs from code in env_flash.c, but I think a sane
196 * default path is desirable.
197 */
198 gd->env_valid = 2;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200199 }
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200200
Stefano Babic5f6c2862010-10-27 11:06:20 +0200201 if (gd->env_valid == 1)
202 ep = tmp_env1;
203 else
204 ep = tmp_env2;
205
206 ret = env_import((char *)ep, 0);
207 if (!ret) {
208 error("Cannot import environment: errno = %d\n", errno);
209 set_default_env("env_import failed");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200210 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200211
212err_read:
213 spi_flash_free(env_flash);
214 env_flash = NULL;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200215out:
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200216 free(tmp_env1);
217 free(tmp_env2);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200218}
219#else
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200220int saveenv(void)
221{
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000222 u32 saved_size, saved_offset, sector = 1;
Marek Vasutd73c1292014-03-05 19:59:50 +0100223 char *saved_buffer = NULL;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000224 int ret = 1;
Tom Rinia840b8e2013-04-05 14:55:21 -0400225 env_t env_new;
TsiChung Liew08792d72008-08-06 19:37:17 -0500226
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200227 if (!env_flash) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200228 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
229 CONFIG_ENV_SPI_CS,
230 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
231 if (!env_flash) {
232 set_default_env("!spi_flash_probe() failed");
233 return 1;
234 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200235 }
236
Mike Frysinger9af10d52008-12-11 06:23:37 -0500237 /* Is the sector larger than the env (i.e. embedded) */
238 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
239 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
240 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
241 saved_buffer = malloc(saved_size);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000242 if (!saved_buffer)
Mike Frysinger9af10d52008-12-11 06:23:37 -0500243 goto done;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000244
Stefano Babic5f6c2862010-10-27 11:06:20 +0200245 ret = spi_flash_read(env_flash, saved_offset,
246 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500247 if (ret)
248 goto done;
249 }
250
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200251 if (CONFIG_ENV_SIZE > CONFIG_ENV_SECT_SIZE) {
252 sector = CONFIG_ENV_SIZE / CONFIG_ENV_SECT_SIZE;
253 if (CONFIG_ENV_SIZE % CONFIG_ENV_SECT_SIZE)
TsiChung Liew08792d72008-08-06 19:37:17 -0500254 sector++;
255 }
256
Marek Vasutd73c1292014-03-05 19:59:50 +0100257 ret = env_export(&env_new);
258 if (ret)
Stefano Babic5f6c2862010-10-27 11:06:20 +0200259 goto done;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200260
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200261 puts("Erasing SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200262 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
263 sector * CONFIG_ENV_SECT_SIZE);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500264 if (ret)
265 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200266
267 puts("Writing to SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200268 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
Tom Rinia840b8e2013-04-05 14:55:21 -0400269 CONFIG_ENV_SIZE, &env_new);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500270 if (ret)
271 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200272
Mike Frysinger9af10d52008-12-11 06:23:37 -0500273 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200274 ret = spi_flash_write(env_flash, saved_offset,
275 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500276 if (ret)
277 goto done;
278 }
279
280 ret = 0;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200281 puts("done\n");
Mike Frysinger9af10d52008-12-11 06:23:37 -0500282
283 done:
284 if (saved_buffer)
285 free(saved_buffer);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000286
Mike Frysinger9af10d52008-12-11 06:23:37 -0500287 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200288}
289
290void env_relocate_spec(void)
291{
292 int ret;
Ying Zhang354846f2014-01-24 15:50:07 +0800293 char *buf = NULL;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200294
Ying Zhang354846f2014-01-24 15:50:07 +0800295 buf = (char *)malloc(CONFIG_ENV_SIZE);
Jean-Christophe PLAGNIOL-VILLARD7e1cda62008-09-10 22:48:06 +0200296 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
297 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200298 if (!env_flash) {
299 set_default_env("!spi_flash_probe() failed");
Ying Zhang354846f2014-01-24 15:50:07 +0800300 if (buf)
301 free(buf);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200302 return;
303 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200304
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200305 ret = spi_flash_read(env_flash,
306 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
307 if (ret) {
308 set_default_env("!spi_flash_read() failed");
309 goto out;
310 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200311
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200312 ret = env_import(buf, 1);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200313 if (ret)
314 gd->env_valid = 1;
315out:
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200316 spi_flash_free(env_flash);
Ying Zhang354846f2014-01-24 15:50:07 +0800317 if (buf)
318 free(buf);
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200319 env_flash = NULL;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200320}
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200321#endif
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200322
323int env_init(void)
324{
325 /* SPI flash isn't usable before relocation */
326 gd->env_addr = (ulong)&default_environment[0];
327 gd->env_valid = 1;
328
329 return 0;
330}