blob: 23de5a3495b9a40227b41b2d1b4a2387bd077929 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +02002/*
Wolfgang Denk460a9ff2010-06-20 23:33:59 +02003 * (C) Copyright 2000-2010
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +02004 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 *
6 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 * Andreas Heppel <aheppel@sysgo.de>
8 *
9 * (C) Copyright 2008 Atmel Corporation
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020010 */
11#include <common.h>
Simon Glass11c89f32017-05-17 17:18:03 -060012#include <dm.h>
Simon Glass97385862019-08-01 09:47:00 -060013#include <env.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060014#include <env_internal.h>
Simon Glass8e201882020-05-10 11:39:54 -060015#include <flash.h>
Mike Frysinger9af10d52008-12-11 06:23:37 -050016#include <malloc.h>
Simon Glassdef92152014-10-13 23:41:55 -060017#include <spi.h>
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020018#include <spi_flash.h>
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020019#include <search.h>
20#include <errno.h>
Simon Glass8e201882020-05-10 11:39:54 -060021#include <uuid.h>
Simon Glass274e0b02020-05-10 11:39:56 -060022#include <asm/cache.h>
Gong Qianyu6d85a602016-01-26 15:06:42 +080023#include <dm/device-internal.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070024#include <u-boot/crc.h>
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020025
Simon Glassc10a88e2017-08-03 12:21:58 -060026#ifndef CONFIG_SPL_BUILD
Ashish Kumara7bf68a2017-12-14 17:37:08 +053027#define INITENV
Simon Glassc10a88e2017-08-03 12:21:58 -060028#endif
29
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020030#ifdef CONFIG_ENV_OFFSET_REDUND
Igor Grinbergc3c1bed2011-11-07 01:14:08 +000031static ulong env_offset = CONFIG_ENV_OFFSET;
32static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
Stefano Babic5f6c2862010-10-27 11:06:20 +020033#endif /* CONFIG_ENV_OFFSET_REDUND */
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020034
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020035DECLARE_GLOBAL_DATA_PTR;
36
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +020037static struct spi_flash *env_flash;
38
Andreas Fenkart52b94442017-04-08 11:59:31 +020039static int setup_flash_device(void)
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020040{
Gong Qianyu6d85a602016-01-26 15:06:42 +080041#ifdef CONFIG_DM_SPI_FLASH
42 struct udevice *new;
Andreas Fenkart52b94442017-04-08 11:59:31 +020043 int ret;
Gong Qianyu6d85a602016-01-26 15:06:42 +080044
Vignesh Rae569792016-07-06 10:04:28 +053045 /* speed and mode will be read from DT */
Gong Qianyu6d85a602016-01-26 15:06:42 +080046 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
Konstantin Porotchkin0be67b22018-08-29 16:34:52 +030047 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE,
48 &new);
Gong Qianyu6d85a602016-01-26 15:06:42 +080049 if (ret) {
Simon Glass97385862019-08-01 09:47:00 -060050 env_set_default("spi_flash_probe_bus_cs() failed", 0);
Simon Glass99778492017-08-03 12:22:17 -060051 return ret;
Gong Qianyu6d85a602016-01-26 15:06:42 +080052 }
53
54 env_flash = dev_get_uclass_priv(new);
55#else
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020056
57 if (!env_flash) {
Stefano Babic5f6c2862010-10-27 11:06:20 +020058 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
59 CONFIG_ENV_SPI_CS,
60 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
61 if (!env_flash) {
Simon Glass97385862019-08-01 09:47:00 -060062 env_set_default("spi_flash_probe() failed", 0);
Simon Glass99778492017-08-03 12:22:17 -060063 return -EIO;
Stefano Babic5f6c2862010-10-27 11:06:20 +020064 }
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020065 }
Gong Qianyu6d85a602016-01-26 15:06:42 +080066#endif
Andreas Fenkart52b94442017-04-08 11:59:31 +020067 return 0;
68}
69
70#if defined(CONFIG_ENV_OFFSET_REDUND)
Simon Glass082af922017-08-03 12:22:01 -060071static int env_sf_save(void)
Andreas Fenkart52b94442017-04-08 11:59:31 +020072{
73 env_t env_new;
Simon Glass4686d8f2019-08-01 09:47:08 -060074 char *saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
Andreas Fenkartdf701912017-04-08 11:59:34 +020075 u32 saved_size, saved_offset, sector;
Andreas Fenkart52b94442017-04-08 11:59:31 +020076 int ret;
77
78 ret = setup_flash_device();
79 if (ret)
80 return ret;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020081
Marek Vasutd73c1292014-03-05 19:59:50 +010082 ret = env_export(&env_new);
83 if (ret)
Simon Glass99778492017-08-03 12:22:17 -060084 return -EIO;
Simon Glass4686d8f2019-08-01 09:47:08 -060085 env_new.flags = ENV_REDUND_ACTIVE;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +020086
Simon Glass4bc2ad22017-08-03 12:21:56 -060087 if (gd->env_valid == ENV_VALID) {
Stefano Babic5f6c2862010-10-27 11:06:20 +020088 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
89 env_offset = CONFIG_ENV_OFFSET;
90 } else {
91 env_new_offset = CONFIG_ENV_OFFSET;
92 env_offset = CONFIG_ENV_OFFSET_REDUND;
93 }
94
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +020095 /* Is the sector larger than the env (i.e. embedded) */
96 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
97 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
98 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
Ravi Babu20d93fc2015-08-17 13:29:49 +053099 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200100 if (!saved_buffer) {
Simon Glass99778492017-08-03 12:22:17 -0600101 ret = -ENOMEM;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200102 goto done;
103 }
Heiko Schocherf45775c2019-03-13 12:15:45 +0100104 ret = spi_flash_read(env_flash, saved_offset,
105 saved_size, saved_buffer);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200106 if (ret)
107 goto done;
108 }
109
Andreas Fenkartdf701912017-04-08 11:59:34 +0200110 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200111
112 puts("Erasing SPI flash...");
113 ret = spi_flash_erase(env_flash, env_new_offset,
114 sector * CONFIG_ENV_SECT_SIZE);
115 if (ret)
116 goto done;
117
118 puts("Writing to SPI flash...");
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200119
Stefano Babic5f6c2862010-10-27 11:06:20 +0200120 ret = spi_flash_write(env_flash, env_new_offset,
Tom Rinia840b8e2013-04-05 14:55:21 -0400121 CONFIG_ENV_SIZE, &env_new);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200122 if (ret)
123 goto done;
124
125 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
126 ret = spi_flash_write(env_flash, saved_offset,
127 saved_size, saved_buffer);
128 if (ret)
129 goto done;
130 }
131
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000132 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
Tom Rinia840b8e2013-04-05 14:55:21 -0400133 sizeof(env_new.flags), &flag);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200134 if (ret)
135 goto done;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200136
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200137 puts("done\n");
138
Simon Glass4bc2ad22017-08-03 12:21:56 -0600139 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200140
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000141 printf("Valid environment: %d\n", (int)gd->env_valid);
Stefano Babic5f6c2862010-10-27 11:06:20 +0200142
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200143 done:
144 if (saved_buffer)
145 free(saved_buffer);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000146
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200147 return ret;
148}
149
Simon Glass99778492017-08-03 12:22:17 -0600150static int env_sf_load(void)
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200151{
152 int ret;
Simon Goldschmidt8f9ce3a2018-01-31 14:47:13 +0100153 int read1_fail, read2_fail;
154 env_t *tmp_env1, *tmp_env2;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200155
Ravi Babu20d93fc2015-08-17 13:29:49 +0530156 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
157 CONFIG_ENV_SIZE);
158 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
159 CONFIG_ENV_SIZE);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200160 if (!tmp_env1 || !tmp_env2) {
Simon Glass97385862019-08-01 09:47:00 -0600161 env_set_default("malloc() failed", 0);
Simon Glass99778492017-08-03 12:22:17 -0600162 ret = -EIO;
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000163 goto out;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200164 }
165
Andreas Fenkarta2d18832017-04-08 11:59:32 +0200166 ret = setup_flash_device();
167 if (ret)
thomas.langer@lantiq.com97dfdcf2011-03-29 02:35:14 +0000168 goto out;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200169
Heiko Schocherf45775c2019-03-13 12:15:45 +0100170 read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
171 CONFIG_ENV_SIZE, tmp_env1);
172 read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
173 CONFIG_ENV_SIZE, tmp_env2);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200174
Simon Goldschmidt8f9ce3a2018-01-31 14:47:13 +0100175 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
176 read2_fail);
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200177
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200178 spi_flash_free(env_flash);
179 env_flash = NULL;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200180out:
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200181 free(tmp_env1);
182 free(tmp_env2);
Simon Glass99778492017-08-03 12:22:17 -0600183
184 return ret;
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200185}
186#else
Simon Glass082af922017-08-03 12:22:01 -0600187static int env_sf_save(void)
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200188{
Andreas Fenkartdf701912017-04-08 11:59:34 +0200189 u32 saved_size, saved_offset, sector;
Marek Vasutd73c1292014-03-05 19:59:50 +0100190 char *saved_buffer = NULL;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000191 int ret = 1;
Tom Rinia840b8e2013-04-05 14:55:21 -0400192 env_t env_new;
Gong Qianyu6d85a602016-01-26 15:06:42 +0800193
Andreas Fenkart52b94442017-04-08 11:59:31 +0200194 ret = setup_flash_device();
195 if (ret)
196 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200197
Mike Frysinger9af10d52008-12-11 06:23:37 -0500198 /* Is the sector larger than the env (i.e. embedded) */
199 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
200 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
201 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
202 saved_buffer = malloc(saved_size);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000203 if (!saved_buffer)
Mike Frysinger9af10d52008-12-11 06:23:37 -0500204 goto done;
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000205
Heiko Schocherf45775c2019-03-13 12:15:45 +0100206 ret = spi_flash_read(env_flash, saved_offset,
207 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500208 if (ret)
209 goto done;
210 }
211
Marek Vasutd73c1292014-03-05 19:59:50 +0100212 ret = env_export(&env_new);
213 if (ret)
Stefano Babic5f6c2862010-10-27 11:06:20 +0200214 goto done;
Stefano Babic5f6c2862010-10-27 11:06:20 +0200215
Andreas Fenkartdf701912017-04-08 11:59:34 +0200216 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
217
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200218 puts("Erasing SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200219 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
220 sector * CONFIG_ENV_SECT_SIZE);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500221 if (ret)
222 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200223
224 puts("Writing to SPI flash...");
Stefano Babic5f6c2862010-10-27 11:06:20 +0200225 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
Tom Rinia840b8e2013-04-05 14:55:21 -0400226 CONFIG_ENV_SIZE, &env_new);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500227 if (ret)
228 goto done;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200229
Mike Frysinger9af10d52008-12-11 06:23:37 -0500230 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
Stefano Babic5f6c2862010-10-27 11:06:20 +0200231 ret = spi_flash_write(env_flash, saved_offset,
232 saved_size, saved_buffer);
Mike Frysinger9af10d52008-12-11 06:23:37 -0500233 if (ret)
234 goto done;
235 }
236
237 ret = 0;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200238 puts("done\n");
Mike Frysinger9af10d52008-12-11 06:23:37 -0500239
240 done:
241 if (saved_buffer)
242 free(saved_buffer);
Igor Grinbergc3c1bed2011-11-07 01:14:08 +0000243
Mike Frysinger9af10d52008-12-11 06:23:37 -0500244 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200245}
246
Simon Glass99778492017-08-03 12:22:17 -0600247static int env_sf_load(void)
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200248{
249 int ret;
Ying Zhang354846f2014-01-24 15:50:07 +0800250 char *buf = NULL;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200251
Ravi Babu20d93fc2015-08-17 13:29:49 +0530252 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200253 if (!buf) {
Simon Glass97385862019-08-01 09:47:00 -0600254 env_set_default("malloc() failed", 0);
Simon Glass99778492017-08-03 12:22:17 -0600255 return -EIO;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200256 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200257
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200258 ret = setup_flash_device();
259 if (ret)
260 goto out;
261
Heiko Schocherf45775c2019-03-13 12:15:45 +0100262 ret = spi_flash_read(env_flash,
263 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200264 if (ret) {
Simon Glass97385862019-08-01 09:47:00 -0600265 env_set_default("spi_flash_read() failed", 0);
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200266 goto err_read;
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200267 }
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200268
Wolfgang Denk460a9ff2010-06-20 23:33:59 +0200269 ret = env_import(buf, 1);
Simon Goldschmidt32c3eb52018-01-31 14:47:10 +0100270 if (!ret)
Simon Glass4bc2ad22017-08-03 12:21:56 -0600271 gd->env_valid = ENV_VALID;
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200272
273err_read:
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200274 spi_flash_free(env_flash);
275 env_flash = NULL;
Andreas Fenkarta2c65852017-04-08 11:59:33 +0200276out:
277 free(buf);
Simon Glass99778492017-08-03 12:22:17 -0600278
279 return ret;
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200280}
Wolfgang Wegner0cae3d42010-04-23 17:22:55 +0200281#endif
Haavard Skinnemoeneb8951c2008-05-16 11:10:35 +0200282
Tom Rini5cd7ece2019-11-18 20:02:10 -0500283#if CONFIG_ENV_ADDR != 0x0
Rajesh Bhagat9a90cc92018-11-05 18:01:15 +0000284__weak void *env_sf_get_env_addr(void)
285{
286 return (void *)CONFIG_ENV_ADDR;
287}
288#endif
289
Tom Rini5cd7ece2019-11-18 20:02:10 -0500290#if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0)
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530291static int env_sf_init(void)
292{
Rajesh Bhagat9a90cc92018-11-05 18:01:15 +0000293 env_t *env_ptr = (env_t *)env_sf_get_env_addr();
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530294
295 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
296 gd->env_addr = (ulong)&(env_ptr->data);
297 gd->env_valid = 1;
298 } else {
299 gd->env_addr = (ulong)&default_environment[0];
300 gd->env_valid = 1;
301 }
302
303 return 0;
304}
305#endif
306
Simon Glassc10a88e2017-08-03 12:21:58 -0600307U_BOOT_ENV_LOCATION(sf) = {
308 .location = ENVL_SPI_FLASH,
Simon Glassd8273ed2017-08-03 12:22:03 -0600309 ENV_NAME("SPI Flash")
Simon Glass082af922017-08-03 12:22:01 -0600310 .load = env_sf_load,
Rasmus Villemoesfcff09a2020-03-27 00:02:00 +0100311 .save = CONFIG_IS_ENABLED(SAVEENV) ? ENV_SAVE_PTR(env_sf_save) : NULL,
Tom Rini5cd7ece2019-11-18 20:02:10 -0500312#if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0)
Ashish Kumara7bf68a2017-12-14 17:37:08 +0530313 .init = env_sf_init,
314#endif
Simon Glassc10a88e2017-08-03 12:21:58 -0600315};