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 <dm.h>
12 #include <env.h>
13 #include <env_internal.h>
14 #include <malloc.h>
15 #include <spi.h>
16 #include <spi_flash.h>
17 #include <search.h>
18 #include <errno.h>
19 #include <u-boot/uuid.h>
20 #include <asm/cache.h>
21 #include <asm/global_data.h>
22 #include <dm/device-internal.h>
23 #include <u-boot/crc.h>
24 
25 #define	OFFSET_INVALID		(~(u32)0)
26 
27 #ifdef CONFIG_ENV_OFFSET_REDUND
28 #define ENV_OFFSET_REDUND	CONFIG_ENV_OFFSET_REDUND
29 
30 static ulong env_offset		= CONFIG_ENV_OFFSET;
31 static ulong env_new_offset	= CONFIG_ENV_OFFSET_REDUND;
32 
33 #else
34 
35 #define ENV_OFFSET_REDUND	OFFSET_INVALID
36 
37 #endif /* CONFIG_ENV_OFFSET_REDUND */
38 
39 DECLARE_GLOBAL_DATA_PTR;
40 
spi_get_env_dev(void)41 __weak int spi_get_env_dev(void)
42 {
43 #ifdef CONFIG_ENV_SPI_BUS
44 	return CONFIG_ENV_SPI_BUS;
45 #else
46 	return 0;
47 #endif
48 }
49 
setup_flash_device(struct spi_flash ** env_flash)50 static int setup_flash_device(struct spi_flash **env_flash)
51 {
52 #if CONFIG_IS_ENABLED(DM_SPI_FLASH)
53 	struct udevice *new;
54 	int	ret;
55 	int dev = spi_get_env_dev();
56 
57 	/* speed and mode will be read from DT */
58 	ret = spi_flash_probe_bus_cs(dev, CONFIG_ENV_SPI_CS,
59 				     &new);
60 	if (ret) {
61 		env_set_default("spi_flash_probe_bus_cs() failed", 0);
62 		return ret;
63 	}
64 
65 	*env_flash = dev_get_uclass_priv(new);
66 #else
67 	*env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
68 				     CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
69 	if (!*env_flash) {
70 		env_set_default("spi_flash_probe() failed", 0);
71 		return -EIO;
72 	}
73 #endif
74 	return 0;
75 }
76 
77 #if defined(CONFIG_ENV_OFFSET_REDUND)
env_sf_save(void)78 static int env_sf_save(void)
79 {
80 	env_t	env_new;
81 	char	*saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
82 	u32	saved_size = 0, saved_offset = 0, sector;
83 	u32	sect_size = CONFIG_ENV_SECT_SIZE;
84 	int	ret;
85 	struct spi_flash *env_flash;
86 
87 	ret = setup_flash_device(&env_flash);
88 	if (ret)
89 		return ret;
90 
91 	if (IS_ENABLED(CONFIG_ENV_SECT_SIZE_AUTO))
92 		sect_size = env_flash->mtd.erasesize;
93 
94 	ret = env_export(&env_new);
95 	if (ret)
96 		return -EIO;
97 	env_new.flags	= ENV_REDUND_ACTIVE;
98 
99 	if (gd->env_valid == ENV_VALID) {
100 		env_new_offset = CONFIG_ENV_OFFSET_REDUND;
101 		env_offset = CONFIG_ENV_OFFSET;
102 	} else {
103 		env_new_offset = CONFIG_ENV_OFFSET;
104 		env_offset = CONFIG_ENV_OFFSET_REDUND;
105 	}
106 
107 	/* Is the sector larger than the env (i.e. embedded) */
108 	if (sect_size > CONFIG_ENV_SIZE) {
109 		saved_size = sect_size - CONFIG_ENV_SIZE;
110 		saved_offset = env_new_offset + CONFIG_ENV_SIZE;
111 		saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
112 		if (!saved_buffer) {
113 			ret = -ENOMEM;
114 			goto done;
115 		}
116 		ret = spi_flash_read(env_flash, saved_offset,
117 					saved_size, saved_buffer);
118 		if (ret)
119 			goto done;
120 	}
121 
122 	sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size);
123 
124 	puts("Erasing SPI flash...");
125 	ret = spi_flash_erase(env_flash, env_new_offset,
126 				sector * sect_size);
127 	if (ret)
128 		goto done;
129 
130 	puts("Writing to SPI flash...");
131 
132 	ret = spi_flash_write(env_flash, env_new_offset,
133 		CONFIG_ENV_SIZE, &env_new);
134 	if (ret)
135 		goto done;
136 
137 	if (sect_size > CONFIG_ENV_SIZE) {
138 		ret = spi_flash_write(env_flash, saved_offset,
139 					saved_size, saved_buffer);
140 		if (ret)
141 			goto done;
142 	}
143 
144 	ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
145 				sizeof(env_new.flags), &flag);
146 	if (ret)
147 		goto done;
148 
149 	puts("done\n");
150 
151 	gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
152 
153 	printf("Valid environment: %d\n", (int)gd->env_valid);
154 
155 done:
156 	spi_flash_free(env_flash);
157 
158 	if (saved_buffer)
159 		free(saved_buffer);
160 
161 	return ret;
162 }
163 
env_sf_load(void)164 static int env_sf_load(void)
165 {
166 	int ret;
167 	int read1_fail, read2_fail;
168 	env_t *tmp_env1, *tmp_env2;
169 	struct spi_flash *env_flash;
170 
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);
175 	if (!tmp_env1 || !tmp_env2) {
176 		env_set_default("malloc() failed", 0);
177 		ret = -EIO;
178 		goto out;
179 	}
180 
181 	ret = setup_flash_device(&env_flash);
182 	if (ret)
183 		goto out;
184 
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);
189 
190 	ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
191 				read2_fail, H_EXTERNAL);
192 
193 	spi_flash_free(env_flash);
194 out:
195 	free(tmp_env1);
196 	free(tmp_env2);
197 
198 	return ret;
199 }
200 #else
env_sf_save(void)201 static int env_sf_save(void)
202 {
203 	u32	saved_size = 0, saved_offset = 0, sector;
204 	u32	sect_size = CONFIG_ENV_SECT_SIZE;
205 	char	*saved_buffer = NULL;
206 	int	ret = 1;
207 	env_t	env_new;
208 	struct spi_flash *env_flash;
209 
210 	ret = setup_flash_device(&env_flash);
211 	if (ret)
212 		return ret;
213 
214 	if (IS_ENABLED(CONFIG_ENV_SECT_SIZE_AUTO))
215 		sect_size = env_flash->mtd.erasesize;
216 
217 	/* Is the sector larger than the env (i.e. embedded) */
218 	if (sect_size > CONFIG_ENV_SIZE) {
219 		saved_size = sect_size - CONFIG_ENV_SIZE;
220 		saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
221 		saved_buffer = malloc(saved_size);
222 		if (!saved_buffer) {
223 			ret = -ENOMEM;
224 			goto done;
225 		}
226 
227 		ret = spi_flash_read(env_flash, saved_offset,
228 			saved_size, saved_buffer);
229 		if (ret)
230 			goto done;
231 	}
232 
233 	ret = env_export(&env_new);
234 	if (ret)
235 		goto done;
236 
237 	sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size);
238 
239 	puts("Erasing SPI flash...");
240 	ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
241 		sector * sect_size);
242 	if (ret)
243 		goto done;
244 
245 	puts("Writing to SPI flash...");
246 	ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
247 		CONFIG_ENV_SIZE, &env_new);
248 	if (ret)
249 		goto done;
250 
251 	if (sect_size > CONFIG_ENV_SIZE) {
252 		ret = spi_flash_write(env_flash, saved_offset,
253 			saved_size, saved_buffer);
254 		if (ret)
255 			goto done;
256 	}
257 
258 	ret = 0;
259 	puts("done\n");
260 
261 done:
262 	spi_flash_free(env_flash);
263 
264 	if (saved_buffer)
265 		free(saved_buffer);
266 
267 	return ret;
268 }
269 
env_sf_load(void)270 static int env_sf_load(void)
271 {
272 	int ret;
273 	char *buf = NULL;
274 	struct spi_flash *env_flash;
275 
276 	buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
277 	if (!buf) {
278 		env_set_default("malloc() failed", 0);
279 		return -EIO;
280 	}
281 
282 	ret = setup_flash_device(&env_flash);
283 	if (ret)
284 		goto out;
285 
286 	ret = spi_flash_read(env_flash,
287 		CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
288 	if (ret) {
289 		env_set_default("spi_flash_read() failed", 0);
290 		goto err_read;
291 	}
292 
293 	ret = env_import(buf, 1, H_EXTERNAL);
294 	if (!ret)
295 		gd->env_valid = ENV_VALID;
296 
297 err_read:
298 	spi_flash_free(env_flash);
299 out:
300 	free(buf);
301 
302 	return ret;
303 }
304 #endif
305 
env_sf_erase(void)306 static int env_sf_erase(void)
307 {
308 	int ret;
309 	env_t env;
310 	struct spi_flash *env_flash;
311 
312 	ret = setup_flash_device(&env_flash);
313 	if (ret)
314 		return ret;
315 
316 	memset(&env, 0, sizeof(env_t));
317 	ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, &env);
318 	if (ret)
319 		goto done;
320 
321 	if (ENV_OFFSET_REDUND != OFFSET_INVALID)
322 		ret = spi_flash_write(env_flash, ENV_OFFSET_REDUND, CONFIG_ENV_SIZE, &env);
323 
324 done:
325 	spi_flash_free(env_flash);
326 
327 	return ret;
328 }
329 
env_sf_get_env_addr(void)330 __weak void *env_sf_get_env_addr(void)
331 {
332 #ifndef CONFIG_XPL_BUILD
333 	return (void *)CONFIG_ENV_ADDR;
334 #else
335 	return NULL;
336 #endif
337 }
338 
339 /*
340  * check if Environment on CONFIG_ENV_ADDR is valid.
341  */
env_sf_init_addr(void)342 static int env_sf_init_addr(void)
343 {
344 	env_t *env_ptr = (env_t *)env_sf_get_env_addr();
345 
346 	if (!env_ptr)
347 		return -ENOENT;
348 
349 	if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
350 		gd->env_addr = (ulong)&(env_ptr->data);
351 		gd->env_valid = ENV_VALID;
352 	} else {
353 		gd->env_valid = ENV_INVALID;
354 	}
355 
356 	return 0;
357 }
358 
359 #if defined(CONFIG_ENV_SPI_EARLY)
360 /*
361  * early load environment from SPI flash (before relocation)
362  * and check if it is valid.
363  */
env_sf_init_early(void)364 static int env_sf_init_early(void)
365 {
366 	int ret;
367 	int read1_fail;
368 	int read2_fail;
369 	int crc1_ok;
370 	env_t *tmp_env2 = NULL;
371 	env_t *tmp_env1;
372 	struct spi_flash *env_flash;
373 
374 	/*
375 	 * if malloc is not ready yet, we cannot use
376 	 * this part yet.
377 	 */
378 	if (!gd->malloc_limit)
379 		return -ENOENT;
380 
381 	tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
382 			CONFIG_ENV_SIZE);
383 	if (IS_ENABLED(CONFIG_ENV_REDUNDANT))
384 		tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
385 					     CONFIG_ENV_SIZE);
386 
387 	if (!tmp_env1 || !tmp_env2)
388 		goto out;
389 
390 	ret = setup_flash_device(&env_flash);
391 	if (ret)
392 		goto out;
393 
394 	read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
395 				    CONFIG_ENV_SIZE, tmp_env1);
396 
397 	if (IS_ENABLED(CONFIG_ENV_REDUNDANT)) {
398 		read2_fail = spi_flash_read(env_flash,
399 					    CONFIG_ENV_OFFSET_REDUND,
400 					    CONFIG_ENV_SIZE, tmp_env2);
401 		ret = env_check_redund((char *)tmp_env1, read1_fail,
402 				       (char *)tmp_env2, read2_fail);
403 
404 		if (ret < 0)
405 			goto err_read;
406 
407 		if (gd->env_valid == ENV_VALID)
408 			gd->env_addr = (unsigned long)&tmp_env1->data;
409 		else
410 			gd->env_addr = (unsigned long)&tmp_env2->data;
411 	} else {
412 		if (read1_fail)
413 			goto err_read;
414 
415 		crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
416 				tmp_env1->crc;
417 		if (!crc1_ok)
418 			goto err_read;
419 
420 		/* if valid -> this is our env */
421 		gd->env_valid = ENV_VALID;
422 		gd->env_addr = (unsigned long)&tmp_env1->data;
423 	}
424 
425 	spi_flash_free(env_flash);
426 
427 	return 0;
428 err_read:
429 	spi_flash_free(env_flash);
430 
431 	free(tmp_env1);
432 	if (IS_ENABLED(CONFIG_ENV_REDUNDANT))
433 		free(tmp_env2);
434 out:
435 	/* env is not valid. always return 0 */
436 	gd->env_valid = ENV_INVALID;
437 	return 0;
438 }
439 #endif
440 
env_sf_init(void)441 static int env_sf_init(void)
442 {
443 	int ret = env_sf_init_addr();
444 	if (ret != -ENOENT)
445 		return ret;
446 #ifdef CONFIG_ENV_SPI_EARLY
447 	return env_sf_init_early();
448 #endif
449 	/*
450 	 * return here -ENOENT, so env_init()
451 	 * can set the init bit and later if no
452 	 * other Environment storage is defined
453 	 * can set the default environment
454 	 */
455 	return -ENOENT;
456 }
457 
458 U_BOOT_ENV_LOCATION(sf) = {
459 	.location	= ENVL_SPI_FLASH,
460 	ENV_NAME("SPIFlash")
461 	.load		= env_sf_load,
462 	.save		= ENV_SAVE_PTR(env_sf_save),
463 	.erase		= ENV_ERASE_PTR(env_sf_erase),
464 	.init		= env_sf_init,
465 };
466