ARM: dts: add QorIQ DPAA 1 FMan v3 for LS1046A
[oweals/u-boot.git] / env / sf.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2000-2010
4  * 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
10  */
11 #include <common.h>
12 #include <dm.h>
13 #include <env.h>
14 #include <env_internal.h>
15 #include <malloc.h>
16 #include <spi.h>
17 #include <spi_flash.h>
18 #include <search.h>
19 #include <errno.h>
20 #include <dm/device-internal.h>
21 #include <u-boot/crc.h>
22
23 #ifndef CONFIG_SPL_BUILD
24 #define INITENV
25 #endif
26
27 #ifdef CONFIG_ENV_OFFSET_REDUND
28 static ulong env_offset         = CONFIG_ENV_OFFSET;
29 static ulong env_new_offset     = CONFIG_ENV_OFFSET_REDUND;
30 #endif /* CONFIG_ENV_OFFSET_REDUND */
31
32 DECLARE_GLOBAL_DATA_PTR;
33
34 static struct spi_flash *env_flash;
35
36 static int setup_flash_device(void)
37 {
38 #ifdef CONFIG_DM_SPI_FLASH
39         struct udevice *new;
40         int     ret;
41
42         /* speed and mode will be read from DT */
43         ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
44                                      CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE,
45                                      &new);
46         if (ret) {
47                 env_set_default("spi_flash_probe_bus_cs() failed", 0);
48                 return ret;
49         }
50
51         env_flash = dev_get_uclass_priv(new);
52 #else
53
54         if (!env_flash) {
55                 env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS,
56                         CONFIG_ENV_SPI_CS,
57                         CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
58                 if (!env_flash) {
59                         env_set_default("spi_flash_probe() failed", 0);
60                         return -EIO;
61                 }
62         }
63 #endif
64         return 0;
65 }
66
67 #if defined(CONFIG_ENV_OFFSET_REDUND)
68 static int env_sf_save(void)
69 {
70         env_t   env_new;
71         char    *saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
72         u32     saved_size, saved_offset, sector;
73         int     ret;
74
75         ret = setup_flash_device();
76         if (ret)
77                 return ret;
78
79         ret = env_export(&env_new);
80         if (ret)
81                 return -EIO;
82         env_new.flags   = ENV_REDUND_ACTIVE;
83
84         if (gd->env_valid == ENV_VALID) {
85                 env_new_offset = CONFIG_ENV_OFFSET_REDUND;
86                 env_offset = CONFIG_ENV_OFFSET;
87         } else {
88                 env_new_offset = CONFIG_ENV_OFFSET;
89                 env_offset = CONFIG_ENV_OFFSET_REDUND;
90         }
91
92         /* Is the sector larger than the env (i.e. embedded) */
93         if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
94                 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
95                 saved_offset = env_new_offset + CONFIG_ENV_SIZE;
96                 saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
97                 if (!saved_buffer) {
98                         ret = -ENOMEM;
99                         goto done;
100                 }
101                 ret = spi_flash_read(env_flash, saved_offset,
102                                         saved_size, saved_buffer);
103                 if (ret)
104                         goto done;
105         }
106
107         sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
108
109         puts("Erasing SPI flash...");
110         ret = spi_flash_erase(env_flash, env_new_offset,
111                                 sector * CONFIG_ENV_SECT_SIZE);
112         if (ret)
113                 goto done;
114
115         puts("Writing to SPI flash...");
116
117         ret = spi_flash_write(env_flash, env_new_offset,
118                 CONFIG_ENV_SIZE, &env_new);
119         if (ret)
120                 goto done;
121
122         if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
123                 ret = spi_flash_write(env_flash, saved_offset,
124                                         saved_size, saved_buffer);
125                 if (ret)
126                         goto done;
127         }
128
129         ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
130                                 sizeof(env_new.flags), &flag);
131         if (ret)
132                 goto done;
133
134         puts("done\n");
135
136         gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
137
138         printf("Valid environment: %d\n", (int)gd->env_valid);
139
140  done:
141         if (saved_buffer)
142                 free(saved_buffer);
143
144         return ret;
145 }
146
147 static int env_sf_load(void)
148 {
149         int ret;
150         int read1_fail, read2_fail;
151         env_t *tmp_env1, *tmp_env2;
152
153         tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
154                         CONFIG_ENV_SIZE);
155         tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
156                         CONFIG_ENV_SIZE);
157         if (!tmp_env1 || !tmp_env2) {
158                 env_set_default("malloc() failed", 0);
159                 ret = -EIO;
160                 goto out;
161         }
162
163         ret = setup_flash_device();
164         if (ret)
165                 goto out;
166
167         read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
168                                     CONFIG_ENV_SIZE, tmp_env1);
169         read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
170                                     CONFIG_ENV_SIZE, tmp_env2);
171
172         ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
173                                 read2_fail);
174
175         spi_flash_free(env_flash);
176         env_flash = NULL;
177 out:
178         free(tmp_env1);
179         free(tmp_env2);
180
181         return ret;
182 }
183 #else
184 static int env_sf_save(void)
185 {
186         u32     saved_size, saved_offset, sector;
187         char    *saved_buffer = NULL;
188         int     ret = 1;
189         env_t   env_new;
190
191         ret = setup_flash_device();
192         if (ret)
193                 return ret;
194
195         /* Is the sector larger than the env (i.e. embedded) */
196         if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
197                 saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
198                 saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
199                 saved_buffer = malloc(saved_size);
200                 if (!saved_buffer)
201                         goto done;
202
203                 ret = spi_flash_read(env_flash, saved_offset,
204                         saved_size, saved_buffer);
205                 if (ret)
206                         goto done;
207         }
208
209         ret = env_export(&env_new);
210         if (ret)
211                 goto done;
212
213         sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
214
215         puts("Erasing SPI flash...");
216         ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
217                 sector * CONFIG_ENV_SECT_SIZE);
218         if (ret)
219                 goto done;
220
221         puts("Writing to SPI flash...");
222         ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
223                 CONFIG_ENV_SIZE, &env_new);
224         if (ret)
225                 goto done;
226
227         if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
228                 ret = spi_flash_write(env_flash, saved_offset,
229                         saved_size, saved_buffer);
230                 if (ret)
231                         goto done;
232         }
233
234         ret = 0;
235         puts("done\n");
236
237  done:
238         if (saved_buffer)
239                 free(saved_buffer);
240
241         return ret;
242 }
243
244 static int env_sf_load(void)
245 {
246         int ret;
247         char *buf = NULL;
248
249         buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
250         if (!buf) {
251                 env_set_default("malloc() failed", 0);
252                 return -EIO;
253         }
254
255         ret = setup_flash_device();
256         if (ret)
257                 goto out;
258
259         ret = spi_flash_read(env_flash,
260                 CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
261         if (ret) {
262                 env_set_default("spi_flash_read() failed", 0);
263                 goto err_read;
264         }
265
266         ret = env_import(buf, 1);
267         if (!ret)
268                 gd->env_valid = ENV_VALID;
269
270 err_read:
271         spi_flash_free(env_flash);
272         env_flash = NULL;
273 out:
274         free(buf);
275
276         return ret;
277 }
278 #endif
279
280 #if CONFIG_ENV_ADDR != 0x0
281 __weak void *env_sf_get_env_addr(void)
282 {
283         return (void *)CONFIG_ENV_ADDR;
284 }
285 #endif
286
287 #if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0)
288 static int env_sf_init(void)
289 {
290         env_t *env_ptr = (env_t *)env_sf_get_env_addr();
291
292         if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
293                 gd->env_addr    = (ulong)&(env_ptr->data);
294                 gd->env_valid   = 1;
295         } else {
296                 gd->env_addr = (ulong)&default_environment[0];
297                 gd->env_valid = 1;
298         }
299
300         return 0;
301 }
302 #endif
303
304 U_BOOT_ENV_LOCATION(sf) = {
305         .location       = ENVL_SPI_FLASH,
306         ENV_NAME("SPI Flash")
307         .load           = env_sf_load,
308         .save           = ENV_SAVE_PTR(env_sf_save),
309 #if defined(INITENV) && (CONFIG_ENV_ADDR != 0x0)
310         .init           = env_sf_init,
311 #endif
312 };