TEXT_BASE is in board/sandpoint/config.mk so say so...
[u-boot.git] / common / env_nvram.c
1 /*
2  * (C) Copyright 2000-2002
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
6  * Andreas Heppel <aheppel@sysgo.de>
7
8  * See file CREDITS for list of people who contributed to this
9  * project.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License as
13  * published by the Free Software Foundation; either version 2 of
14  * the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24  * MA 02111-1307 USA
25  */
26
27 /*
28  * 09-18-2001 Andreas Heppel, Sysgo RTS GmbH <aheppel@sysgo.de>
29  *
30  * It might not be possible in all cases to use 'memcpy()' to copy
31  * the environment to NVRAM, as the NVRAM might not be mapped into
32  * the memory space. (I.e. this is the case for the BAB750). In those
33  * cases it might be possible to access the NVRAM using a different
34  * method. For example, the RTC on the BAB750 is accessible in IO
35  * space using its address and data registers. To enable usage of
36  * NVRAM in those cases I invented the functions 'nvram_read()' and
37  * 'nvram_write()', which will be activated upon the configuration
38  * #define CFG_NVRAM_ACCESS_ROUTINE. Note, that those functions are
39  * strongly dependent on the used HW, and must be redefined for each
40  * board that wants to use them.
41  */
42
43 #include <common.h>
44
45 #ifdef CFG_ENV_IS_IN_NVRAM /* Environment is in NVRAM */
46
47 #include <command.h>
48 #include <environment.h>
49 #include <linux/stddef.h>
50
51 #ifdef CFG_NVRAM_ACCESS_ROUTINE
52 extern void *nvram_read(void *dest, const long src, size_t count);
53 extern void nvram_write(long dest, const void *src, size_t count);
54 env_t *env_ptr = NULL;
55 #else
56 env_t *env_ptr = (env_t *)CFG_ENV_ADDR;
57 #endif
58
59 char * env_name_spec = "NVRAM";
60
61 extern uchar default_environment[];
62 extern int default_environment_size;
63
64 extern uchar (*env_get_char)(int);
65 extern uchar env_get_char_memory (int index);
66
67 #ifdef CONFIG_AMIGAONEG3SE
68 uchar env_get_char_spec (int index)
69 {
70 #ifdef CFG_NVRAM_ACCESS_ROUTINE
71         uchar c;
72
73         nvram_read(&c, CFG_ENV_ADDR+index, 1);
74
75         return c;
76 #else
77         DECLARE_GLOBAL_DATA_PTR;
78         uchar retval;
79         enable_nvram();
80         retval = *((uchar *)(gd->env_addr + index));
81         disable_nvram();
82         return retval;
83 #endif
84 }
85 #else
86 uchar env_get_char_spec (int index)
87 {
88 #ifdef CFG_NVRAM_ACCESS_ROUTINE
89         uchar c;
90
91         nvram_read(&c, CFG_ENV_ADDR+index, 1);
92
93         return c;
94 #else
95         DECLARE_GLOBAL_DATA_PTR;
96
97         return *((uchar *)(gd->env_addr + index));
98 #endif
99 }
100 #endif
101
102 void env_relocate_spec (void)
103 {
104 #if defined(CFG_NVRAM_ACCESS_ROUTINE)
105         nvram_read(env_ptr, CFG_ENV_ADDR, CFG_ENV_SIZE);
106 #else
107         memcpy (env_ptr, (void*)CFG_ENV_ADDR, CFG_ENV_SIZE);
108 #endif
109 }
110
111 int saveenv (void)
112 {
113         int rcode = 0;
114 #ifdef CONFIG_AMIGAONEG3SE
115         enable_nvram();
116 #endif
117 #ifdef CFG_NVRAM_ACCESS_ROUTINE
118         nvram_write(CFG_ENV_ADDR, env_ptr, CFG_ENV_SIZE);
119 #else
120         if (memcpy ((char *)CFG_ENV_ADDR, env_ptr, CFG_ENV_SIZE) == NULL)
121                     rcode = 1 ;
122 #endif
123 #ifdef CONFIG_AMIGAONEG3SE
124         udelay(10000);
125         disable_nvram();
126 #endif
127         return rcode;
128 }
129
130
131 /************************************************************************
132  * Initialize Environment use
133  *
134  * We are still running from ROM, so data use is limited
135  */
136 int env_init (void)
137 {
138         DECLARE_GLOBAL_DATA_PTR;
139 #ifdef CONFIG_AMIGAONEG3SE
140         enable_nvram();
141 #endif
142 #if defined(CFG_NVRAM_ACCESS_ROUTINE)
143         ulong crc;
144         uchar data[ENV_SIZE];
145         nvram_read (&crc, CFG_ENV_ADDR, sizeof(ulong));
146         nvram_read (data, CFG_ENV_ADDR+sizeof(ulong), ENV_SIZE);
147
148         if (crc32(0, data, ENV_SIZE) == crc) {
149                 gd->env_addr  = (ulong)CFG_ENV_ADDR + sizeof(long);
150 #else
151         if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
152                 gd->env_addr  = (ulong)&(env_ptr->data);
153 #endif
154                 gd->env_valid = 1;
155         } else {
156                 gd->env_addr  = (ulong)&default_environment[0];
157                 gd->env_valid = 0;
158         }
159 #ifdef CONFIG_AMIGAONEG3SE
160         disable_nvram();
161 #endif
162         return (0);
163 }
164
165 #endif /* CFG_ENV_IS_IN_NVRAM */