]> Git Repo - u-boot.git/blame - env/sf.c
common: command: Add support for $ auto-completion
[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,
25a17652
KP
61 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE,
62 &new);
19c31285 63 if (ret) {
c5d548a9 64 set_default_env("spi_flash_probe_bus_cs() failed", 0);
c5951991 65 return ret;
19c31285
GQ
66 }
67
68 env_flash = dev_get_uclass_priv(new);
69#else
7319bcaf
WW
70
71 if (!env_flash) {
a3110f01
SB
72 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
73 CONFIG_ENV_SPI_CS,
74 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
75 if (!env_flash) {
c5d548a9 76 set_default_env("spi_flash_probe() failed", 0);
c5951991 77 return -EIO;
a3110f01 78 }
7319bcaf 79 }
19c31285 80#endif
afa81a77
AF
81 return 0;
82}
83
84#if defined(CONFIG_ENV_OFFSET_REDUND)
4415f1d1 85#ifdef CMD_SAVEENV
e5bce247 86static int env_sf_save(void)
afa81a77
AF
87{
88 env_t env_new;
89 char *saved_buffer = NULL, flag = OBSOLETE_FLAG;
0b2e5bbe 90 u32 saved_size, saved_offset, sector;
afa81a77
AF
91 int ret;
92
93 ret = setup_flash_device();
94 if (ret)
95 return ret;
7319bcaf 96
7ce1526e
MV
97 ret = env_export(&env_new);
98 if (ret)
c5951991 99 return -EIO;
cd0f4fa1 100 env_new.flags = ACTIVE_FLAG;
ea882baf 101
203e94f6 102 if (gd->env_valid == ENV_VALID) {
a3110f01
SB
103 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
104 env_offset = CONFIG_ENV_OFFSET;
105 } else {
106 env_new_offset = CONFIG_ENV_OFFSET;
107 env_offset = CONFIG_ENV_OFFSET_REDUND;
108 }
109
7319bcaf
WW
110 /* Is the sector larger than the env (i.e. embedded) */
111 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
112 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
113 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
7dd01744 114 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
7319bcaf 115 if (!saved_buffer) {
c5951991 116 ret = -ENOMEM;
7319bcaf
WW
117 goto done;
118 }
119 ret = spi_flash_read(env_flash, saved_offset,
120 saved_size, saved_buffer);
121 if (ret)
122 goto done;
123 }
124
0b2e5bbe 125 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
7319bcaf
WW
126
127 puts("Erasing SPI flash...");
128 ret = spi_flash_erase(env_flash, env_new_offset,
129 sector * CONFIG_ENV_SECT_SIZE);
130 if (ret)
131 goto done;
132
133 puts("Writing to SPI flash...");
7319bcaf 134
a3110f01 135 ret = spi_flash_write(env_flash, env_new_offset,
cd0f4fa1 136 CONFIG_ENV_SIZE, &env_new);
7319bcaf
WW
137 if (ret)
138 goto done;
139
140 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
141 ret = spi_flash_write(env_flash, saved_offset,
142 saved_size, saved_buffer);
143 if (ret)
144 goto done;
145 }
146
eb58a7fc 147 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
cd0f4fa1 148 sizeof(env_new.flags), &flag);
a3110f01
SB
149 if (ret)
150 goto done;
7319bcaf 151
7319bcaf
WW
152 puts("done\n");
153
203e94f6 154 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
a3110f01 155
2dc55d9e 156 printf("Valid environment: %d\n", (int)gd->env_valid);
a3110f01 157
7319bcaf
WW
158 done:
159 if (saved_buffer)
160 free(saved_buffer);
eb58a7fc 161
7319bcaf
WW
162 return ret;
163}
4415f1d1 164#endif /* CMD_SAVEENV */
7319bcaf 165
c5951991 166static int env_sf_load(void)
7319bcaf
WW
167{
168 int ret;
80719938
SG
169 int read1_fail, read2_fail;
170 env_t *tmp_env1, *tmp_env2;
7319bcaf 171
7dd01744
RB
172 tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
173 CONFIG_ENV_SIZE);
174 tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
175 CONFIG_ENV_SIZE);
ea882baf 176 if (!tmp_env1 || !tmp_env2) {
c5d548a9 177 set_default_env("malloc() failed", 0);
c5951991 178 ret = -EIO;
2dc55d9e 179 goto out;
7319bcaf
WW
180 }
181
8fee8845
AF
182 ret = setup_flash_device();
183 if (ret)
2dc55d9e 184 goto out;
7319bcaf 185
80719938
SG
186 read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
187 CONFIG_ENV_SIZE, tmp_env1);
188 read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
189 CONFIG_ENV_SIZE, tmp_env2);
ea882baf 190
80719938
SG
191 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
192 read2_fail);
7319bcaf 193
7319bcaf
WW
194 spi_flash_free(env_flash);
195 env_flash = NULL;
7319bcaf 196out:
ea882baf
WD
197 free(tmp_env1);
198 free(tmp_env2);
c5951991
SG
199
200 return ret;
7319bcaf
WW
201}
202#else
4415f1d1 203#ifdef CMD_SAVEENV
e5bce247 204static int env_sf_save(void)
8c66497e 205{
0b2e5bbe 206 u32 saved_size, saved_offset, sector;
7ce1526e 207 char *saved_buffer = NULL;
eb58a7fc 208 int ret = 1;
cd0f4fa1 209 env_t env_new;
19c31285 210
afa81a77
AF
211 ret = setup_flash_device();
212 if (ret)
213 return ret;
8c66497e 214
5b3375ac
MF
215 /* Is the sector larger than the env (i.e. embedded) */
216 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
217 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
218 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
219 saved_buffer = malloc(saved_size);
eb58a7fc 220 if (!saved_buffer)
5b3375ac 221 goto done;
eb58a7fc 222
a3110f01
SB
223 ret = spi_flash_read(env_flash, saved_offset,
224 saved_size, saved_buffer);
5b3375ac
MF
225 if (ret)
226 goto done;
227 }
228
7ce1526e
MV
229 ret = env_export(&env_new);
230 if (ret)
a3110f01 231 goto done;
a3110f01 232
0b2e5bbe
AF
233 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
234
8c66497e 235 puts("Erasing SPI flash...");
a3110f01
SB
236 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
237 sector * CONFIG_ENV_SECT_SIZE);
5b3375ac
MF
238 if (ret)
239 goto done;
8c66497e
HS
240
241 puts("Writing to SPI flash...");
a3110f01 242 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
cd0f4fa1 243 CONFIG_ENV_SIZE, &env_new);
5b3375ac
MF
244 if (ret)
245 goto done;
8c66497e 246
5b3375ac 247 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
a3110f01
SB
248 ret = spi_flash_write(env_flash, saved_offset,
249 saved_size, saved_buffer);
5b3375ac
MF
250 if (ret)
251 goto done;
252 }
253
254 ret = 0;
8c66497e 255 puts("done\n");
5b3375ac
MF
256
257 done:
258 if (saved_buffer)
259 free(saved_buffer);
eb58a7fc 260
5b3375ac 261 return ret;
8c66497e 262}
4415f1d1 263#endif /* CMD_SAVEENV */
8c66497e 264
c5951991 265static int env_sf_load(void)
8c66497e
HS
266{
267 int ret;
5a89fa92 268 char *buf = NULL;
8c66497e 269
7dd01744 270 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
c041c60c 271 if (!buf) {
c5d548a9 272 set_default_env("malloc() failed", 0);
c5951991 273 return -EIO;
ea882baf 274 }
8c66497e 275
c041c60c
AF
276 ret = setup_flash_device();
277 if (ret)
278 goto out;
279
ea882baf
WD
280 ret = spi_flash_read(env_flash,
281 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
282 if (ret) {
c5d548a9 283 set_default_env("spi_flash_read() failed", 0);
c041c60c 284 goto err_read;
ea882baf 285 }
8c66497e 286
ea882baf 287 ret = env_import(buf, 1);
42a1820b 288 if (!ret)
203e94f6 289 gd->env_valid = ENV_VALID;
c041c60c
AF
290
291err_read:
8c66497e
HS
292 spi_flash_free(env_flash);
293 env_flash = NULL;
c041c60c
AF
294out:
295 free(buf);
c5951991
SG
296
297 return ret;
8c66497e 298}
7319bcaf 299#endif
8c66497e 300
119c01c2
RB
301#ifdef CONFIG_ENV_ADDR
302__weak void *env_sf_get_env_addr(void)
303{
304 return (void *)CONFIG_ENV_ADDR;
305}
306#endif
307
b500c92b
AK
308#if defined(INITENV) && defined(CONFIG_ENV_ADDR)
309static int env_sf_init(void)
310{
119c01c2 311 env_t *env_ptr = (env_t *)env_sf_get_env_addr();
b500c92b
AK
312
313 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
314 gd->env_addr = (ulong)&(env_ptr->data);
315 gd->env_valid = 1;
316 } else {
317 gd->env_addr = (ulong)&default_environment[0];
318 gd->env_valid = 1;
319 }
320
321 return 0;
322}
323#endif
324
4415f1d1
SG
325U_BOOT_ENV_LOCATION(sf) = {
326 .location = ENVL_SPI_FLASH,
ac358beb 327 ENV_NAME("SPI Flash")
e5bce247 328 .load = env_sf_load,
4415f1d1 329#ifdef CMD_SAVEENV
e5bce247 330 .save = env_save_ptr(env_sf_save),
4415f1d1 331#endif
b500c92b
AK
332#if defined(INITENV) && defined(CONFIG_ENV_ADDR)
333 .init = env_sf_init,
334#endif
4415f1d1 335};
This page took 0.35153 seconds and 4 git commands to generate.