]> Git Repo - J-u-boot.git/blame - env/sf.c
common: Drop net.h from common header
[J-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>
0ac7d722 13#include <env.h>
f3998fdc 14#include <env_internal.h>
b79fdc76 15#include <flash.h>
5b3375ac 16#include <malloc.h>
843c9e87 17#include <spi.h>
8c66497e 18#include <spi_flash.h>
ea882baf
WD
19#include <search.h>
20#include <errno.h>
b79fdc76 21#include <uuid.h>
90526e9f 22#include <asm/cache.h>
19c31285 23#include <dm/device-internal.h>
3db71108 24#include <u-boot/crc.h>
8c66497e 25
4415f1d1 26#ifndef CONFIG_SPL_BUILD
b500c92b 27#define INITENV
4415f1d1
SG
28#endif
29
7319bcaf 30#ifdef CONFIG_ENV_OFFSET_REDUND
eb58a7fc
IG
31static ulong env_offset = CONFIG_ENV_OFFSET;
32static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
a3110f01 33#endif /* CONFIG_ENV_OFFSET_REDUND */
7319bcaf 34
8c66497e
HS
35DECLARE_GLOBAL_DATA_PTR;
36
8c66497e
HS
37static struct spi_flash *env_flash;
38
afa81a77 39static int setup_flash_device(void)
7319bcaf 40{
19c31285
GQ
41#ifdef CONFIG_DM_SPI_FLASH
42 struct udevice *new;
afa81a77 43 int ret;
19c31285 44
96907c0f 45 /* speed and mode will be read from DT */
19c31285 46 ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
25a17652
KP
47 CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE,
48 &new);
19c31285 49 if (ret) {
0ac7d722 50 env_set_default("spi_flash_probe_bus_cs() failed", 0);
c5951991 51 return ret;
19c31285
GQ
52 }
53
54 env_flash = dev_get_uclass_priv(new);
55#else
7319bcaf
WW
56
57 if (!env_flash) {
a3110f01
SB
58 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) {
0ac7d722 62 env_set_default("spi_flash_probe() failed", 0);
c5951991 63 return -EIO;
a3110f01 64 }
7319bcaf 65 }
19c31285 66#endif
afa81a77
AF
67 return 0;
68}
69
70#if defined(CONFIG_ENV_OFFSET_REDUND)
e5bce247 71static int env_sf_save(void)
afa81a77
AF
72{
73 env_t env_new;
d3716dd6 74 char *saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
0b2e5bbe 75 u32 saved_size, saved_offset, sector;
afa81a77
AF
76 int ret;
77
78 ret = setup_flash_device();
79 if (ret)
80 return ret;
7319bcaf 81
7ce1526e
MV
82 ret = env_export(&env_new);
83 if (ret)
c5951991 84 return -EIO;
d3716dd6 85 env_new.flags = ENV_REDUND_ACTIVE;
ea882baf 86
203e94f6 87 if (gd->env_valid == ENV_VALID) {
a3110f01
SB
88 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
7319bcaf
WW
95 /* 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;
7dd01744 99 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
7319bcaf 100 if (!saved_buffer) {
c5951991 101 ret = -ENOMEM;
7319bcaf
WW
102 goto done;
103 }
9ba5e5bc
HS
104 ret = spi_flash_read(env_flash, saved_offset,
105 saved_size, saved_buffer);
7319bcaf
WW
106 if (ret)
107 goto done;
108 }
109
0b2e5bbe 110 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
7319bcaf
WW
111
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...");
7319bcaf 119
a3110f01 120 ret = spi_flash_write(env_flash, env_new_offset,
cd0f4fa1 121 CONFIG_ENV_SIZE, &env_new);
7319bcaf
WW
122 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
eb58a7fc 132 ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
cd0f4fa1 133 sizeof(env_new.flags), &flag);
a3110f01
SB
134 if (ret)
135 goto done;
7319bcaf 136
7319bcaf
WW
137 puts("done\n");
138
203e94f6 139 gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
a3110f01 140
2dc55d9e 141 printf("Valid environment: %d\n", (int)gd->env_valid);
a3110f01 142
7319bcaf
WW
143 done:
144 if (saved_buffer)
145 free(saved_buffer);
eb58a7fc 146
7319bcaf
WW
147 return ret;
148}
149
c5951991 150static int env_sf_load(void)
7319bcaf
WW
151{
152 int ret;
80719938
SG
153 int read1_fail, read2_fail;
154 env_t *tmp_env1, *tmp_env2;
7319bcaf 155
7dd01744
RB
156 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);
ea882baf 160 if (!tmp_env1 || !tmp_env2) {
0ac7d722 161 env_set_default("malloc() failed", 0);
c5951991 162 ret = -EIO;
2dc55d9e 163 goto out;
7319bcaf
WW
164 }
165
8fee8845
AF
166 ret = setup_flash_device();
167 if (ret)
2dc55d9e 168 goto out;
7319bcaf 169
9ba5e5bc
HS
170 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);
ea882baf 174
80719938
SG
175 ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
176 read2_fail);
7319bcaf 177
7319bcaf
WW
178 spi_flash_free(env_flash);
179 env_flash = NULL;
7319bcaf 180out:
ea882baf
WD
181 free(tmp_env1);
182 free(tmp_env2);
c5951991
SG
183
184 return ret;
7319bcaf
WW
185}
186#else
e5bce247 187static int env_sf_save(void)
8c66497e 188{
0b2e5bbe 189 u32 saved_size, saved_offset, sector;
7ce1526e 190 char *saved_buffer = NULL;
eb58a7fc 191 int ret = 1;
cd0f4fa1 192 env_t env_new;
19c31285 193
afa81a77
AF
194 ret = setup_flash_device();
195 if (ret)
196 return ret;
8c66497e 197
5b3375ac
MF
198 /* 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);
eb58a7fc 203 if (!saved_buffer)
5b3375ac 204 goto done;
eb58a7fc 205
9ba5e5bc
HS
206 ret = spi_flash_read(env_flash, saved_offset,
207 saved_size, saved_buffer);
5b3375ac
MF
208 if (ret)
209 goto done;
210 }
211
7ce1526e
MV
212 ret = env_export(&env_new);
213 if (ret)
a3110f01 214 goto done;
a3110f01 215
0b2e5bbe
AF
216 sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
217
8c66497e 218 puts("Erasing SPI flash...");
a3110f01
SB
219 ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
220 sector * CONFIG_ENV_SECT_SIZE);
5b3375ac
MF
221 if (ret)
222 goto done;
8c66497e
HS
223
224 puts("Writing to SPI flash...");
a3110f01 225 ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
cd0f4fa1 226 CONFIG_ENV_SIZE, &env_new);
5b3375ac
MF
227 if (ret)
228 goto done;
8c66497e 229
5b3375ac 230 if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
a3110f01
SB
231 ret = spi_flash_write(env_flash, saved_offset,
232 saved_size, saved_buffer);
5b3375ac
MF
233 if (ret)
234 goto done;
235 }
236
237 ret = 0;
8c66497e 238 puts("done\n");
5b3375ac
MF
239
240 done:
241 if (saved_buffer)
242 free(saved_buffer);
eb58a7fc 243
5b3375ac 244 return ret;
8c66497e
HS
245}
246
c5951991 247static int env_sf_load(void)
8c66497e
HS
248{
249 int ret;
5a89fa92 250 char *buf = NULL;
8c66497e 251
7dd01744 252 buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
c041c60c 253 if (!buf) {
0ac7d722 254 env_set_default("malloc() failed", 0);
c5951991 255 return -EIO;
ea882baf 256 }
8c66497e 257
c041c60c
AF
258 ret = setup_flash_device();
259 if (ret)
260 goto out;
261
9ba5e5bc
HS
262 ret = spi_flash_read(env_flash,
263 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
ea882baf 264 if (ret) {
0ac7d722 265 env_set_default("spi_flash_read() failed", 0);
c041c60c 266 goto err_read;
ea882baf 267 }
8c66497e 268
ea882baf 269 ret = env_import(buf, 1);
42a1820b 270 if (!ret)
203e94f6 271 gd->env_valid = ENV_VALID;
c041c60c
AF
272
273err_read:
8c66497e
HS
274 spi_flash_free(env_flash);
275 env_flash = NULL;
c041c60c
AF
276out:
277 free(buf);
c5951991
SG
278
279 return ret;
8c66497e 280}
7319bcaf 281#endif
8c66497e 282
a09fea1d 283#if CONFIG_ENV_ADDR != 0x0
119c01c2
RB
284__weak void *env_sf_get_env_addr(void)
285{
286 return (void *)CONFIG_ENV_ADDR;
287}
288#endif
289
a09fea1d 290#if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0)
b500c92b
AK
291static int env_sf_init(void)
292{
119c01c2 293 env_t *env_ptr = (env_t *)env_sf_get_env_addr();
b500c92b
AK
294
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
4415f1d1
SG
307U_BOOT_ENV_LOCATION(sf) = {
308 .location = ENVL_SPI_FLASH,
ac358beb 309 ENV_NAME("SPI Flash")
e5bce247 310 .load = env_sf_load,
6d3524c2 311 .save = CONFIG_IS_ENABLED(SAVEENV) ? ENV_SAVE_PTR(env_sf_save) : NULL,
a09fea1d 312#if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0)
b500c92b
AK
313 .init = env_sf_init,
314#endif
4415f1d1 315};
This page took 0.52361 seconds and 4 git commands to generate.