]> Git Repo - u-boot.git/blame - env/sf.c
u-boot: remove driver lookup loop from env_save()
[u-boot.git] / env / sf.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
8c66497e 2/*
ea882baf 3 * (C) Copyright 2000-2010
8c66497e
HS
4 * Wolfgang Denk, DENX Software Engineering, [email protected].
5 *
6 * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 * Andreas Heppel <[email protected]>
8 *
9 * (C) Copyright 2008 Atmel Corporation
8c66497e
HS
10 */
11#include <common.h>
9d922450 12#include <dm.h>
8c66497e 13#include <environment.h>
5b3375ac 14#include <malloc.h>
843c9e87 15#include <spi.h>
8c66497e 16#include <spi_flash.h>
ea882baf
WD
17#include <search.h>
18#include <errno.h>
19c31285 19#include <dm/device-internal.h>
8c66497e 20
0e8d1586 21#ifndef CONFIG_ENV_SPI_BUS
2e4e5ad4 22# define CONFIG_ENV_SPI_BUS CONFIG_SF_DEFAULT_BUS
8c66497e 23#endif
0e8d1586 24#ifndef CONFIG_ENV_SPI_CS
2e4e5ad4 25# define CONFIG_ENV_SPI_CS CONFIG_SF_DEFAULT_CS
8c66497e 26#endif
0e8d1586 27#ifndef CONFIG_ENV_SPI_MAX_HZ
2e4e5ad4 28# define CONFIG_ENV_SPI_MAX_HZ CONFIG_SF_DEFAULT_SPEED
8c66497e 29#endif
0e8d1586 30#ifndef CONFIG_ENV_SPI_MODE
2e4e5ad4 31# define CONFIG_ENV_SPI_MODE CONFIG_SF_DEFAULT_MODE
8c66497e
HS
32#endif
33
4415f1d1
SG
34#ifndef CONFIG_SPL_BUILD
35#define CMD_SAVEENV
b500c92b 36#define INITENV
4415f1d1
SG
37#endif
38
7319bcaf 39#ifdef CONFIG_ENV_OFFSET_REDUND
4415f1d1 40#ifdef CMD_SAVEENV
eb58a7fc
IG
41static ulong env_offset = CONFIG_ENV_OFFSET;
42static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
4415f1d1 43#endif
7319bcaf 44
eb58a7fc
IG
45#define ACTIVE_FLAG 1
46#define OBSOLETE_FLAG 0
a3110f01 47#endif /* CONFIG_ENV_OFFSET_REDUND */
7319bcaf 48
8c66497e
HS
49DECLARE_GLOBAL_DATA_PTR;
50
8c66497e
HS
51static struct spi_flash *env_flash;
52
afa81a77 53static int setup_flash_device(void)
7319bcaf 54{
19c31285
GQ
55#ifdef CONFIG_DM_SPI_FLASH
56 struct udevice *new;
afa81a77 57 int ret;
19c31285 58
96907c0f 59 /* speed and mode will be read from DT */
19c31285 60 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
96907c0f 61 0, 0, &new);
19c31285 62 if (ret) {
c5d548a9 63 set_default_env("spi_flash_probe_bus_cs() failed", 0);
c5951991 64 return ret;
19c31285
GQ
65 }
66
67 env_flash = dev_get_uclass_priv(new);
68#else
7319bcaf
WW
69
70 if (!env_flash) {
a3110f01
SB
71 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
72 CONFIG_ENV_SPI_CS,
73 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
74 if (!env_flash) {
c5d548a9 75 set_default_env("spi_flash_probe() failed", 0);
c5951991 76 return -EIO;
a3110f01 77 }
7319bcaf 78 }
19c31285 79#endif
afa81a77
AF
80 return 0;
81}
82
83#if defined(CONFIG_ENV_OFFSET_REDUND)
4415f1d1 84#ifdef CMD_SAVEENV
e5bce247 85static int env_sf_save(void)
afa81a77
AF
86{
87 env_t env_new;
88 char *saved_buffer = NULL, flag = OBSOLETE_FLAG;
0b2e5bbe 89 u32 saved_size, saved_offset, sector;
afa81a77
AF
90 int ret;
91
92 ret = setup_flash_device();
93 if (ret)
94 return ret;
7319bcaf 95
7ce1526e
MV
96 ret = env_export(&env_new);
97 if (ret)
c5951991 98 return -EIO;
cd0f4fa1 99 env_new.flags = ACTIVE_FLAG;
ea882baf 100
203e94f6 101 if (gd->env_valid == ENV_VALID) {
a3110f01
SB
102 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
103 env_offset = CONFIG_ENV_OFFSET;
104 } else {
105 env_new_offset = CONFIG_ENV_OFFSET;
106 env_offset = CONFIG_ENV_OFFSET_REDUND;
107 }
108
7319bcaf
WW
109 /* Is the sector larger than the env (i.e. embedded) */
110 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
111 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
112 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
7dd01744 113 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
7319bcaf 114 if (!saved_buffer) {
c5951991 115 ret = -ENOMEM;
7319bcaf
WW
116 goto done;
117 }
118 ret = spi_flash_read(env_flash, saved_offset,
119 saved_size, saved_buffer);
120 if (ret)
121 goto done;
122 }
123
0b2e5bbe 124 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
7319bcaf
WW
125
126 puts("Erasing SPI flash...");
127 ret = spi_flash_erase(env_flash, env_new_offset,
128 sector * CONFIG_ENV_SECT_SIZE);
129 if (ret)
130 goto done;
131
132 puts("Writing to SPI flash...");
7319bcaf 133
a3110f01 134 ret = spi_flash_write(env_flash, env_new_offset,
cd0f4fa1 135 CONFIG_ENV_SIZE, &env_new);
7319bcaf
WW
136 if (ret)
137 goto done;
138
139 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
140 ret = spi_flash_write(env_flash, saved_offset,
141 saved_size, saved_buffer);
142 if (ret)
143 goto done;
144 }
145
eb58a7fc 146 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
cd0f4fa1 147 sizeof(env_new.flags), &flag);
a3110f01
SB
148 if (ret)
149 goto done;
7319bcaf 150
7319bcaf
WW
151 puts("done\n");
152
203e94f6 153 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
a3110f01 154
2dc55d9e 155 printf("Valid environment: %d\n", (int)gd->env_valid);
a3110f01 156
7319bcaf
WW
157 done:
158 if (saved_buffer)
159 free(saved_buffer);
eb58a7fc 160
7319bcaf
WW
161 return ret;
162}
4415f1d1 163#endif /* CMD_SAVEENV */
7319bcaf 164
c5951991 165static int env_sf_load(void)
7319bcaf
WW
166{
167 int ret;
80719938
SG
168 int read1_fail, read2_fail;
169 env_t *tmp_env1, *tmp_env2;
7319bcaf 170
7dd01744
RB
171 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
172 CONFIG_ENV_SIZE);
173 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
174 CONFIG_ENV_SIZE);
ea882baf 175 if (!tmp_env1 || !tmp_env2) {
c5d548a9 176 set_default_env("malloc() failed", 0);
c5951991 177 ret = -EIO;
2dc55d9e 178 goto out;
7319bcaf
WW
179 }
180
8fee8845
AF
181 ret = setup_flash_device();
182 if (ret)
2dc55d9e 183 goto out;
7319bcaf 184
80719938
SG
185 read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
186 CONFIG_ENV_SIZE, tmp_env1);
187 read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
188 CONFIG_ENV_SIZE, tmp_env2);
ea882baf 189
80719938
SG
190 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
191 read2_fail);
7319bcaf 192
7319bcaf
WW
193 spi_flash_free(env_flash);
194 env_flash = NULL;
7319bcaf 195out:
ea882baf
WD
196 free(tmp_env1);
197 free(tmp_env2);
c5951991
SG
198
199 return ret;
7319bcaf
WW
200}
201#else
4415f1d1 202#ifdef CMD_SAVEENV
e5bce247 203static int env_sf_save(void)
8c66497e 204{
0b2e5bbe 205 u32 saved_size, saved_offset, sector;
7ce1526e 206 char *saved_buffer = NULL;
eb58a7fc 207 int ret = 1;
cd0f4fa1 208 env_t env_new;
19c31285 209
afa81a77
AF
210 ret = setup_flash_device();
211 if (ret)
212 return ret;
8c66497e 213
5b3375ac
MF
214 /* Is the sector larger than the env (i.e. embedded) */
215 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
216 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
217 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
218 saved_buffer = malloc(saved_size);
eb58a7fc 219 if (!saved_buffer)
5b3375ac 220 goto done;
eb58a7fc 221
a3110f01
SB
222 ret = spi_flash_read(env_flash, saved_offset,
223 saved_size, saved_buffer);
5b3375ac
MF
224 if (ret)
225 goto done;
226 }
227
7ce1526e
MV
228 ret = env_export(&env_new);
229 if (ret)
a3110f01 230 goto done;
a3110f01 231
0b2e5bbe
AF
232 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
233
8c66497e 234 puts("Erasing SPI flash...");
a3110f01
SB
235 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
236 sector * CONFIG_ENV_SECT_SIZE);
5b3375ac
MF
237 if (ret)
238 goto done;
8c66497e
HS
239
240 puts("Writing to SPI flash...");
a3110f01 241 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
cd0f4fa1 242 CONFIG_ENV_SIZE, &env_new);
5b3375ac
MF
243 if (ret)
244 goto done;
8c66497e 245
5b3375ac 246 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
a3110f01
SB
247 ret = spi_flash_write(env_flash, saved_offset,
248 saved_size, saved_buffer);
5b3375ac
MF
249 if (ret)
250 goto done;
251 }
252
253 ret = 0;
8c66497e 254 puts("done\n");
5b3375ac
MF
255
256 done:
257 if (saved_buffer)
258 free(saved_buffer);
eb58a7fc 259
5b3375ac 260 return ret;
8c66497e 261}
4415f1d1 262#endif /* CMD_SAVEENV */
8c66497e 263
c5951991 264static int env_sf_load(void)
8c66497e
HS
265{
266 int ret;
5a89fa92 267 char *buf = NULL;
8c66497e 268
7dd01744 269 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
c041c60c 270 if (!buf) {
c5d548a9 271 set_default_env("malloc() failed", 0);
c5951991 272 return -EIO;
ea882baf 273 }
8c66497e 274
c041c60c
AF
275 ret = setup_flash_device();
276 if (ret)
277 goto out;
278
ea882baf
WD
279 ret = spi_flash_read(env_flash,
280 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
281 if (ret) {
c5d548a9 282 set_default_env("spi_flash_read() failed", 0);
c041c60c 283 goto err_read;
ea882baf 284 }
8c66497e 285
ea882baf 286 ret = env_import(buf, 1);
42a1820b 287 if (!ret)
203e94f6 288 gd->env_valid = ENV_VALID;
c041c60c
AF
289
290err_read:
8c66497e
HS
291 spi_flash_free(env_flash);
292 env_flash = NULL;
c041c60c
AF
293out:
294 free(buf);
c5951991
SG
295
296 return ret;
8c66497e 297}
7319bcaf 298#endif
8c66497e 299
b500c92b
AK
300#if defined(INITENV) && defined(CONFIG_ENV_ADDR)
301static int env_sf_init(void)
302{
303 env_t *env_ptr = (env_t *)(CONFIG_ENV_ADDR);
304
305 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
306 gd->env_addr = (ulong)&(env_ptr->data);
307 gd->env_valid = 1;
308 } else {
309 gd->env_addr = (ulong)&default_environment[0];
310 gd->env_valid = 1;
311 }
312
313 return 0;
314}
315#endif
316
4415f1d1
SG
317U_BOOT_ENV_LOCATION(sf) = {
318 .location = ENVL_SPI_FLASH,
ac358beb 319 ENV_NAME("SPI Flash")
e5bce247 320 .load = env_sf_load,
4415f1d1 321#ifdef CMD_SAVEENV
e5bce247 322 .save = env_save_ptr(env_sf_save),
4415f1d1 323#endif
b500c92b
AK
324#if defined(INITENV) && defined(CONFIG_ENV_ADDR)
325 .init = env_sf_init,
326#endif
4415f1d1 327};
This page took 0.322069 seconds and 4 git commands to generate.