]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
c609719b | 2 | /* |
ea882baf | 3 | * (C) Copyright 2000-2010 |
c609719b WD |
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]> | |
c609719b WD |
8 | */ |
9 | ||
10 | /* | |
11 | * 09-18-2001 Andreas Heppel, Sysgo RTS GmbH <[email protected]> | |
12 | * | |
13 | * It might not be possible in all cases to use 'memcpy()' to copy | |
14 | * the environment to NVRAM, as the NVRAM might not be mapped into | |
15 | * the memory space. (I.e. this is the case for the BAB750). In those | |
16 | * cases it might be possible to access the NVRAM using a different | |
17 | * method. For example, the RTC on the BAB750 is accessible in IO | |
18 | * space using its address and data registers. To enable usage of | |
19 | * NVRAM in those cases I invented the functions 'nvram_read()' and | |
20 | * 'nvram_write()', which will be activated upon the configuration | |
6d0f6bcf | 21 | * #define CONFIG_SYS_NVRAM_ACCESS_ROUTINE. Note, that those functions are |
c609719b WD |
22 | * strongly dependent on the used HW, and must be redefined for each |
23 | * board that wants to use them. | |
24 | */ | |
25 | ||
26 | #include <common.h> | |
c609719b WD |
27 | #include <command.h> |
28 | #include <environment.h> | |
c609719b | 29 | #include <linux/stddef.h> |
ea882baf WD |
30 | #include <search.h> |
31 | #include <errno.h> | |
c609719b | 32 | |
957a0e69 JCPV |
33 | DECLARE_GLOBAL_DATA_PTR; |
34 | ||
6d0f6bcf | 35 | #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE |
c609719b WD |
36 | extern void *nvram_read(void *dest, const long src, size_t count); |
37 | extern void nvram_write(long dest, const void *src, size_t count); | |
c609719b | 38 | #else |
0e8d1586 | 39 | env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR; |
c609719b WD |
40 | #endif |
41 | ||
bf95df44 | 42 | #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE |
b2cdef48 GS |
43 | /** Call this function from overridden env_get_char_spec() if you need |
44 | * this functionality. | |
45 | */ | |
46 | int env_nvram_get_char(int index) | |
c609719b | 47 | { |
c609719b WD |
48 | uchar c; |
49 | ||
91494ca6 | 50 | nvram_read(&c, CONFIG_ENV_ADDR + index, 1); |
c609719b WD |
51 | |
52 | return c; | |
c609719b | 53 | } |
bf95df44 | 54 | #endif |
c609719b | 55 | |
c5951991 | 56 | static int env_nvram_load(void) |
c609719b | 57 | { |
cd0f4fa1 | 58 | char buf[CONFIG_ENV_SIZE]; |
ea882baf | 59 | |
6d0f6bcf | 60 | #if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE) |
ea882baf | 61 | nvram_read(buf, CONFIG_ENV_ADDR, CONFIG_ENV_SIZE); |
c609719b | 62 | #else |
cd0f4fa1 | 63 | memcpy(buf, (void *)CONFIG_ENV_ADDR, CONFIG_ENV_SIZE); |
c609719b | 64 | #endif |
2166ebf7 | 65 | return env_import(buf, 1); |
c609719b WD |
66 | } |
67 | ||
e5bce247 | 68 | static int env_nvram_save(void) |
c609719b | 69 | { |
cd0f4fa1 | 70 | env_t env_new; |
ea882baf WD |
71 | int rcode = 0; |
72 | ||
7ce1526e MV |
73 | rcode = env_export(&env_new); |
74 | if (rcode) | |
75 | return rcode; | |
ea882baf | 76 | |
6d0f6bcf | 77 | #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE |
cd0f4fa1 TR |
78 | nvram_write(CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE); |
79 | #else | |
80 | if (memcpy((char *)CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE) == NULL) | |
81 | rcode = 1; | |
c609719b WD |
82 | #endif |
83 | return rcode; | |
84 | } | |
85 | ||
ea882baf | 86 | /* |
c609719b WD |
87 | * Initialize Environment use |
88 | * | |
89 | * We are still running from ROM, so data use is limited | |
90 | */ | |
e5bce247 | 91 | static int env_nvram_init(void) |
c609719b | 92 | { |
6d0f6bcf | 93 | #if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE) |
c609719b | 94 | ulong crc; |
cd0f4fa1 | 95 | uchar data[ENV_SIZE]; |
ea882baf WD |
96 | |
97 | nvram_read(&crc, CONFIG_ENV_ADDR, sizeof(ulong)); | |
91494ca6 | 98 | nvram_read(data, CONFIG_ENV_ADDR + sizeof(ulong), ENV_SIZE); |
c609719b WD |
99 | |
100 | if (crc32(0, data, ENV_SIZE) == crc) { | |
91494ca6 | 101 | gd->env_addr = (ulong)CONFIG_ENV_ADDR + sizeof(long); |
c609719b WD |
102 | #else |
103 | if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) { | |
91494ca6 | 104 | gd->env_addr = (ulong)&env_ptr->data; |
c609719b | 105 | #endif |
203e94f6 | 106 | gd->env_valid = ENV_VALID; |
c609719b | 107 | } else { |
91494ca6 | 108 | gd->env_addr = (ulong)&default_environment[0]; |
2d7cb5b4 | 109 | gd->env_valid = ENV_INVALID; |
c609719b | 110 | } |
91494ca6 IG |
111 | |
112 | return 0; | |
c609719b | 113 | } |
4415f1d1 SG |
114 | |
115 | U_BOOT_ENV_LOCATION(nvram) = { | |
116 | .location = ENVL_NVRAM, | |
ac358beb | 117 | ENV_NAME("NVRAM") |
e5bce247 SG |
118 | .load = env_nvram_load, |
119 | .save = env_save_ptr(env_nvram_save), | |
120 | .init = env_nvram_init, | |
4415f1d1 | 121 | }; |