blob: 45f441a042b5ec0caedf226c81754ba63106c041 [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 *
Jagannadha Sutradharudu Teki402ba1e2013-07-29 22:12:52 +053010 * SPDX-License-Identifier: GPL-2.0+
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020011 */
12#include <common.h>
Simon Glass9d922452017-05-17 17:18:03 -060013#include <dm.h>
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020014#include <environment.h>
Mike Frysinger5b3375a2008-12-11 06:23:37 -050015#include <malloc.h>
Simon Glass843c9e82014-10-13 23:41:55 -060016#include <spi.h>
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020017#include <spi_flash.h>
Wolfgang Denkea882ba2010-06-20 23:33:59 +020018#include <search.h>
19#include <errno.h>
Gong Qianyu19c31282016-01-26 15:06:42 +080020#include <dm/device-internal.h>
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020021
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020022#ifndef CONFIG_ENV_SPI_BUS
Jean-Jacques Hiblot2e4e5ad2017-02-01 14:51:47 +010023# define CONFIG_ENV_SPI_BUS CONFIG_SF_DEFAULT_BUS
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020024#endif
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020025#ifndef CONFIG_ENV_SPI_CS
Jean-Jacques Hiblot2e4e5ad2017-02-01 14:51:47 +010026# define CONFIG_ENV_SPI_CS CONFIG_SF_DEFAULT_CS
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020027#endif
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020028#ifndef CONFIG_ENV_SPI_MAX_HZ
Jean-Jacques Hiblot2e4e5ad2017-02-01 14:51:47 +010029# define CONFIG_ENV_SPI_MAX_HZ CONFIG_SF_DEFAULT_SPEED
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020030#endif
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020031#ifndef CONFIG_ENV_SPI_MODE
Jean-Jacques Hiblot2e4e5ad2017-02-01 14:51:47 +010032# define CONFIG_ENV_SPI_MODE CONFIG_SF_DEFAULT_MODE
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020033#endif
34
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020035#ifdef CONFIG_ENV_OFFSET_REDUND
Igor Grinbergeb58a7f2011-11-07 01:14:08 +000036static ulong env_offset = CONFIG_ENV_OFFSET;
37static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020038
Igor Grinbergeb58a7f2011-11-07 01:14:08 +000039#define ACTIVE_FLAG 1
40#define OBSOLETE_FLAG 0
Stefano Babica3110f02010-10-27 11:06:20 +020041#endif /* CONFIG_ENV_OFFSET_REDUND */
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020042
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020043DECLARE_GLOBAL_DATA_PTR;
44
Igor Grinbergeb58a7f2011-11-07 01:14:08 +000045char *env_name_spec = "SPI Flash";
Haavard Skinnemoen8c664972008-05-16 11:10:35 +020046
47static struct spi_flash *env_flash;
48
Andreas Fenkartafa81a72017-04-08 11:59:31 +020049static int setup_flash_device(void)
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020050{
Gong Qianyu19c31282016-01-26 15:06:42 +080051#ifdef CONFIG_DM_SPI_FLASH
52 struct udevice *new;
Andreas Fenkartafa81a72017-04-08 11:59:31 +020053 int ret;
Gong Qianyu19c31282016-01-26 15:06:42 +080054
Vignesh R96907c02016-07-06 10:04:28 +053055 /* speed and mode will be read from DT */
Gong Qianyu19c31282016-01-26 15:06:42 +080056 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
Vignesh R96907c02016-07-06 10:04:28 +053057 0, 0, &new);
Gong Qianyu19c31282016-01-26 15:06:42 +080058 if (ret) {
59 set_default_env("!spi_flash_probe_bus_cs() failed");
60 return 1;
61 }
62
63 env_flash = dev_get_uclass_priv(new);
64#else
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020065
66 if (!env_flash) {
Stefano Babica3110f02010-10-27 11:06:20 +020067 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
68 CONFIG_ENV_SPI_CS,
69 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
70 if (!env_flash) {
71 set_default_env("!spi_flash_probe() failed");
72 return 1;
73 }
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020074 }
Gong Qianyu19c31282016-01-26 15:06:42 +080075#endif
Andreas Fenkartafa81a72017-04-08 11:59:31 +020076 return 0;
77}
78
79#if defined(CONFIG_ENV_OFFSET_REDUND)
80int saveenv(void)
81{
82 env_t env_new;
83 char *saved_buffer = NULL, flag = OBSOLETE_FLAG;
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +020084 u32 saved_size, saved_offset, sector;
Andreas Fenkartafa81a72017-04-08 11:59:31 +020085 int ret;
86
87 ret = setup_flash_device();
88 if (ret)
89 return ret;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +020090
Marek Vasut7ce15262014-03-05 19:59:50 +010091 ret = env_export(&env_new);
92 if (ret)
93 return ret;
Tom Rinicd0f4fa2013-04-05 14:55:21 -040094 env_new.flags = ACTIVE_FLAG;
Wolfgang Denkea882ba2010-06-20 23:33:59 +020095
Stefano Babica3110f02010-10-27 11:06:20 +020096 if (gd->env_valid == 1) {
97 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
98 env_offset = CONFIG_ENV_OFFSET;
99 } else {
100 env_new_offset = CONFIG_ENV_OFFSET;
101 env_offset = CONFIG_ENV_OFFSET_REDUND;
102 }
103
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200104 /* Is the sector larger than the env (i.e. embedded) */
105 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
106 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
107 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
Ravi Babu7dd01742015-08-17 13:29:49 +0530108 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200109 if (!saved_buffer) {
110 ret = 1;
111 goto done;
112 }
113 ret = spi_flash_read(env_flash, saved_offset,
114 saved_size, saved_buffer);
115 if (ret)
116 goto done;
117 }
118
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +0200119 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200120
121 puts("Erasing SPI flash...");
122 ret = spi_flash_erase(env_flash, env_new_offset,
123 sector * CONFIG_ENV_SECT_SIZE);
124 if (ret)
125 goto done;
126
127 puts("Writing to SPI flash...");
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200128
Stefano Babica3110f02010-10-27 11:06:20 +0200129 ret = spi_flash_write(env_flash, env_new_offset,
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400130 CONFIG_ENV_SIZE, &env_new);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200131 if (ret)
132 goto done;
133
134 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
135 ret = spi_flash_write(env_flash, saved_offset,
136 saved_size, saved_buffer);
137 if (ret)
138 goto done;
139 }
140
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000141 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400142 sizeof(env_new.flags), &flag);
Stefano Babica3110f02010-10-27 11:06:20 +0200143 if (ret)
144 goto done;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200145
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200146 puts("done\n");
147
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000148 gd->env_valid = gd->env_valid == 2 ? 1 : 2;
Stefano Babica3110f02010-10-27 11:06:20 +0200149
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000150 printf("Valid environment: %d\n", (int)gd->env_valid);
Stefano Babica3110f02010-10-27 11:06:20 +0200151
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200152 done:
153 if (saved_buffer)
154 free(saved_buffer);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000155
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200156 return ret;
157}
158
159void env_relocate_spec(void)
160{
161 int ret;
162 int crc1_ok = 0, crc2_ok = 0;
163 env_t *tmp_env1 = NULL;
164 env_t *tmp_env2 = NULL;
Stefano Babica3110f02010-10-27 11:06:20 +0200165 env_t *ep = NULL;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200166
Ravi Babu7dd01742015-08-17 13:29:49 +0530167 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
168 CONFIG_ENV_SIZE);
169 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
170 CONFIG_ENV_SIZE);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200171 if (!tmp_env1 || !tmp_env2) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200172 set_default_env("!malloc() failed");
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000173 goto out;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200174 }
175
Andreas Fenkart8fee8842017-04-08 11:59:32 +0200176 ret = setup_flash_device();
177 if (ret)
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000178 goto out;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200179
180 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
181 CONFIG_ENV_SIZE, tmp_env1);
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200182 if (ret) {
183 set_default_env("!spi_flash_read() failed");
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200184 goto err_read;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200185 }
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200186
187 if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc)
188 crc1_ok = 1;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200189
190 ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
191 CONFIG_ENV_SIZE, tmp_env2);
192 if (!ret) {
193 if (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc)
194 crc2_ok = 1;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200195 }
196
Stefano Babica3110f02010-10-27 11:06:20 +0200197 if (!crc1_ok && !crc2_ok) {
Stefano Babica3110f02010-10-27 11:06:20 +0200198 set_default_env("!bad CRC");
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000199 goto err_read;
Stefano Babica3110f02010-10-27 11:06:20 +0200200 } else if (crc1_ok && !crc2_ok) {
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200201 gd->env_valid = 1;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200202 } else if (!crc1_ok && crc2_ok) {
Stefano Babica3110f02010-10-27 11:06:20 +0200203 gd->env_valid = 2;
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000204 } else if (tmp_env1->flags == ACTIVE_FLAG &&
205 tmp_env2->flags == OBSOLETE_FLAG) {
206 gd->env_valid = 1;
207 } else if (tmp_env1->flags == OBSOLETE_FLAG &&
208 tmp_env2->flags == ACTIVE_FLAG) {
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200209 gd->env_valid = 2;
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000210 } else if (tmp_env1->flags == tmp_env2->flags) {
Mario Schuknecht5e8397d2015-03-24 08:59:00 +0100211 gd->env_valid = 1;
thomas.langer@lantiq.com2dc55d92011-03-29 02:35:14 +0000212 } else if (tmp_env1->flags == 0xFF) {
Mario Schuknecht5e8397d2015-03-24 08:59:00 +0100213 gd->env_valid = 1;
214 } else if (tmp_env2->flags == 0xFF) {
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200215 gd->env_valid = 2;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200216 } else {
217 /*
218 * this differs from code in env_flash.c, but I think a sane
219 * default path is desirable.
220 */
Mario Schuknecht5e8397d2015-03-24 08:59:00 +0100221 gd->env_valid = 1;
Stefano Babica3110f02010-10-27 11:06:20 +0200222 }
223
Stefano Babica3110f02010-10-27 11:06:20 +0200224 if (gd->env_valid == 1)
225 ep = tmp_env1;
226 else
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200227 ep = tmp_env2;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200228
Stefano Babica3110f02010-10-27 11:06:20 +0200229 ret = env_import((char *)ep, 0);
230 if (!ret) {
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200231 error("Cannot import environment: errno = %d\n", errno);
Peng Fan9a5d9932016-03-28 17:26:27 +0800232 set_default_env("!env_import failed");
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200233 }
234
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200235err_read:
236 spi_flash_free(env_flash);
237 env_flash = NULL;
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200238out:
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200239 free(tmp_env1);
240 free(tmp_env2);
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200241}
242#else
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200243int saveenv(void)
244{
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +0200245 u32 saved_size, saved_offset, sector;
Marek Vasut7ce15262014-03-05 19:59:50 +0100246 char *saved_buffer = NULL;
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000247 int ret = 1;
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400248 env_t env_new;
Gong Qianyu19c31282016-01-26 15:06:42 +0800249
Andreas Fenkartafa81a72017-04-08 11:59:31 +0200250 ret = setup_flash_device();
251 if (ret)
252 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200253
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500254 /* Is the sector larger than the env (i.e. embedded) */
255 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
256 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
257 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
258 saved_buffer = malloc(saved_size);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000259 if (!saved_buffer)
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500260 goto done;
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000261
Stefano Babica3110f02010-10-27 11:06:20 +0200262 ret = spi_flash_read(env_flash, saved_offset,
263 saved_size, saved_buffer);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500264 if (ret)
265 goto done;
266 }
267
Marek Vasut7ce15262014-03-05 19:59:50 +0100268 ret = env_export(&env_new);
269 if (ret)
Stefano Babica3110f02010-10-27 11:06:20 +0200270 goto done;
Stefano Babica3110f02010-10-27 11:06:20 +0200271
Andreas Fenkart0b2e5bb2017-04-08 11:59:34 +0200272 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
273
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200274 puts("Erasing SPI flash...");
Stefano Babica3110f02010-10-27 11:06:20 +0200275 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
276 sector * CONFIG_ENV_SECT_SIZE);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500277 if (ret)
278 goto done;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200279
280 puts("Writing to SPI flash...");
Stefano Babica3110f02010-10-27 11:06:20 +0200281 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
Tom Rinicd0f4fa2013-04-05 14:55:21 -0400282 CONFIG_ENV_SIZE, &env_new);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500283 if (ret)
284 goto done;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200285
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500286 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
Stefano Babica3110f02010-10-27 11:06:20 +0200287 ret = spi_flash_write(env_flash, saved_offset,
288 saved_size, saved_buffer);
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500289 if (ret)
290 goto done;
291 }
292
293 ret = 0;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200294 puts("done\n");
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500295
296 done:
297 if (saved_buffer)
298 free(saved_buffer);
Igor Grinbergeb58a7f2011-11-07 01:14:08 +0000299
Mike Frysinger5b3375a2008-12-11 06:23:37 -0500300 return ret;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200301}
302
303void env_relocate_spec(void)
304{
305 int ret;
Ying Zhang5a89fa92014-01-24 15:50:07 +0800306 char *buf = NULL;
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200307
Ravi Babu7dd01742015-08-17 13:29:49 +0530308 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
Andreas Fenkartc041c602017-04-08 11:59:33 +0200309 if (!buf) {
310 set_default_env("!malloc() failed");
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200311 return;
312 }
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200313
Andreas Fenkartc041c602017-04-08 11:59:33 +0200314 ret = setup_flash_device();
315 if (ret)
316 goto out;
317
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200318 ret = spi_flash_read(env_flash,
319 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
320 if (ret) {
321 set_default_env("!spi_flash_read() failed");
Andreas Fenkartc041c602017-04-08 11:59:33 +0200322 goto err_read;
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200323 }
324
325 ret = env_import(buf, 1);
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200326 if (ret)
Wolfgang Denkea882ba2010-06-20 23:33:59 +0200327 gd->env_valid = 1;
Andreas Fenkartc041c602017-04-08 11:59:33 +0200328
329err_read:
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200330 spi_flash_free(env_flash);
331 env_flash = NULL;
Andreas Fenkartc041c602017-04-08 11:59:33 +0200332out:
333 free(buf);
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200334}
Wolfgang Wegner7319bca2010-04-23 17:22:55 +0200335#endif
Haavard Skinnemoen8c664972008-05-16 11:10:35 +0200336
337int env_init(void)
338{
339 /* SPI flash isn't usable before relocation */
340 gd->env_addr = (ulong)&default_environment[0];
341 gd->env_valid = 1;
342
343 return 0;
344}