xref: /OK3568_Linux_fs/u-boot/env/nvram.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * (C) Copyright 2000-2010
3*4882a593Smuzhiyun  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6*4882a593Smuzhiyun  * Andreas Heppel <aheppel@sysgo.de>
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun /*
12*4882a593Smuzhiyun  * 09-18-2001 Andreas Heppel, Sysgo RTS GmbH <aheppel@sysgo.de>
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * It might not be possible in all cases to use 'memcpy()' to copy
15*4882a593Smuzhiyun  * the environment to NVRAM, as the NVRAM might not be mapped into
16*4882a593Smuzhiyun  * the memory space. (I.e. this is the case for the BAB750). In those
17*4882a593Smuzhiyun  * cases it might be possible to access the NVRAM using a different
18*4882a593Smuzhiyun  * method. For example, the RTC on the BAB750 is accessible in IO
19*4882a593Smuzhiyun  * space using its address and data registers. To enable usage of
20*4882a593Smuzhiyun  * NVRAM in those cases I invented the functions 'nvram_read()' and
21*4882a593Smuzhiyun  * 'nvram_write()', which will be activated upon the configuration
22*4882a593Smuzhiyun  * #define CONFIG_SYS_NVRAM_ACCESS_ROUTINE. Note, that those functions are
23*4882a593Smuzhiyun  * strongly dependent on the used HW, and must be redefined for each
24*4882a593Smuzhiyun  * board that wants to use them.
25*4882a593Smuzhiyun  */
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun #include <common.h>
28*4882a593Smuzhiyun #include <command.h>
29*4882a593Smuzhiyun #include <environment.h>
30*4882a593Smuzhiyun #include <linux/stddef.h>
31*4882a593Smuzhiyun #include <search.h>
32*4882a593Smuzhiyun #include <errno.h>
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun DECLARE_GLOBAL_DATA_PTR;
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
37*4882a593Smuzhiyun extern void *nvram_read(void *dest, const long src, size_t count);
38*4882a593Smuzhiyun extern void nvram_write(long dest, const void *src, size_t count);
39*4882a593Smuzhiyun #else
40*4882a593Smuzhiyun env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
41*4882a593Smuzhiyun #endif
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
env_nvram_get_char(int index)44*4882a593Smuzhiyun static int env_nvram_get_char(int index)
45*4882a593Smuzhiyun {
46*4882a593Smuzhiyun 	uchar c;
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 	nvram_read(&c, CONFIG_ENV_ADDR + index, 1);
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun 	return c;
51*4882a593Smuzhiyun }
52*4882a593Smuzhiyun #endif
53*4882a593Smuzhiyun 
env_nvram_load(void)54*4882a593Smuzhiyun static int env_nvram_load(void)
55*4882a593Smuzhiyun {
56*4882a593Smuzhiyun 	char buf[CONFIG_ENV_SIZE];
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun #if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE)
59*4882a593Smuzhiyun 	nvram_read(buf, CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
60*4882a593Smuzhiyun #else
61*4882a593Smuzhiyun 	memcpy(buf, (void *)CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
62*4882a593Smuzhiyun #endif
63*4882a593Smuzhiyun 	env_import(buf, 1);
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 	return 0;
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun 
env_nvram_save(void)68*4882a593Smuzhiyun static int env_nvram_save(void)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun 	env_t	env_new;
71*4882a593Smuzhiyun 	int	rcode = 0;
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	rcode = env_export(&env_new);
74*4882a593Smuzhiyun 	if (rcode)
75*4882a593Smuzhiyun 		return rcode;
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
78*4882a593Smuzhiyun 	nvram_write(CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE);
79*4882a593Smuzhiyun #else
80*4882a593Smuzhiyun 	if (memcpy((char *)CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE) == NULL)
81*4882a593Smuzhiyun 		rcode = 1;
82*4882a593Smuzhiyun #endif
83*4882a593Smuzhiyun 	return rcode;
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun /*
87*4882a593Smuzhiyun  * Initialize Environment use
88*4882a593Smuzhiyun  *
89*4882a593Smuzhiyun  * We are still running from ROM, so data use is limited
90*4882a593Smuzhiyun  */
env_nvram_init(void)91*4882a593Smuzhiyun static int env_nvram_init(void)
92*4882a593Smuzhiyun {
93*4882a593Smuzhiyun #if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE)
94*4882a593Smuzhiyun 	ulong crc;
95*4882a593Smuzhiyun 	uchar data[ENV_SIZE];
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	nvram_read(&crc, CONFIG_ENV_ADDR, sizeof(ulong));
98*4882a593Smuzhiyun 	nvram_read(data, CONFIG_ENV_ADDR + sizeof(ulong), ENV_SIZE);
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	if (crc32(0, data, ENV_SIZE) == crc) {
101*4882a593Smuzhiyun 		gd->env_addr	= (ulong)CONFIG_ENV_ADDR + sizeof(long);
102*4882a593Smuzhiyun #else
103*4882a593Smuzhiyun 	if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
104*4882a593Smuzhiyun 		gd->env_addr	= (ulong)&env_ptr->data;
105*4882a593Smuzhiyun #endif
106*4882a593Smuzhiyun 		gd->env_valid = ENV_VALID;
107*4882a593Smuzhiyun 	} else {
108*4882a593Smuzhiyun 		gd->env_addr	= (ulong)&default_environment[0];
109*4882a593Smuzhiyun 		gd->env_valid	= ENV_INVALID;
110*4882a593Smuzhiyun 	}
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	return 0;
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun U_BOOT_ENV_LOCATION(nvram) = {
116*4882a593Smuzhiyun 	.location	= ENVL_NVRAM,
117*4882a593Smuzhiyun 	ENV_NAME("NVRAM")
118*4882a593Smuzhiyun #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
119*4882a593Smuzhiyun 	.get_char	= env_nvram_get_char,
120*4882a593Smuzhiyun #endif
121*4882a593Smuzhiyun 	.load		= env_nvram_load,
122*4882a593Smuzhiyun 	.save		= env_save_ptr(env_nvram_save),
123*4882a593Smuzhiyun 	.init		= env_nvram_init,
124*4882a593Smuzhiyun };
125