blob: 562edd0499c01db799c9851b59d355af7910aa37 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
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
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020038 * #define CONFIG_SYS_NVRAM_ACCESS_ROUTINE. Note, that those functions are
wdenkc6097192002-11-03 00:24:07 +000039 * 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>
wdenkc6097192002-11-03 00:24:07 +000044#include <command.h>
45#include <environment.h>
wdenkc6097192002-11-03 00:24:07 +000046#include <linux/stddef.h>
wdenkc6097192002-11-03 00:24:07 +000047
Jean-Christophe PLAGNIOL-VILLARD957a0e62008-09-10 22:47:59 +020048DECLARE_GLOBAL_DATA_PTR;
49
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020050#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
wdenkc6097192002-11-03 00:24:07 +000051extern void *nvram_read(void *dest, const long src, size_t count);
52extern void nvram_write(long dest, const void *src, size_t count);
53env_t *env_ptr = NULL;
54#else
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020055env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
wdenkc6097192002-11-03 00:24:07 +000056#endif
57
58char * env_name_spec = "NVRAM";
59
60extern uchar default_environment[];
61extern int default_environment_size;
62
wdenk7c7a23b2002-12-07 00:20:59 +000063#ifdef CONFIG_AMIGAONEG3SE
64uchar env_get_char_spec (int index)
65{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020066#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
wdenk7c7a23b2002-12-07 00:20:59 +000067 uchar c;
wdenkc6097192002-11-03 00:24:07 +000068
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020069 nvram_read(&c, CONFIG_ENV_ADDR+index, 1);
wdenk7c7a23b2002-12-07 00:20:59 +000070
71 return c;
72#else
wdenk7c7a23b2002-12-07 00:20:59 +000073 uchar retval;
74 enable_nvram();
75 retval = *((uchar *)(gd->env_addr + index));
76 disable_nvram();
77 return retval;
78#endif
79}
80#else
wdenkc6097192002-11-03 00:24:07 +000081uchar env_get_char_spec (int index)
82{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020083#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
wdenkc6097192002-11-03 00:24:07 +000084 uchar c;
85
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020086 nvram_read(&c, CONFIG_ENV_ADDR+index, 1);
wdenkc6097192002-11-03 00:24:07 +000087
88 return c;
89#else
wdenkc6097192002-11-03 00:24:07 +000090 return *((uchar *)(gd->env_addr + index));
91#endif
92}
wdenk7c7a23b2002-12-07 00:20:59 +000093#endif
wdenkc6097192002-11-03 00:24:07 +000094
95void env_relocate_spec (void)
96{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020097#if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE)
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +020098 nvram_read(env_ptr, CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
wdenkc6097192002-11-03 00:24:07 +000099#else
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200100 memcpy (env_ptr, (void*)CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
wdenkc6097192002-11-03 00:24:07 +0000101#endif
102}
103
104int saveenv (void)
105{
106 int rcode = 0;
wdenk7c7a23b2002-12-07 00:20:59 +0000107#ifdef CONFIG_AMIGAONEG3SE
108 enable_nvram();
109#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200110#ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200111 nvram_write(CONFIG_ENV_ADDR, env_ptr, CONFIG_ENV_SIZE);
wdenkc6097192002-11-03 00:24:07 +0000112#else
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200113 if (memcpy ((char *)CONFIG_ENV_ADDR, env_ptr, CONFIG_ENV_SIZE) == NULL)
wdenkc6097192002-11-03 00:24:07 +0000114 rcode = 1 ;
115#endif
wdenk7c7a23b2002-12-07 00:20:59 +0000116#ifdef CONFIG_AMIGAONEG3SE
117 udelay(10000);
118 disable_nvram();
119#endif
wdenkc6097192002-11-03 00:24:07 +0000120 return rcode;
121}
122
123
124/************************************************************************
125 * Initialize Environment use
126 *
127 * We are still running from ROM, so data use is limited
128 */
129int env_init (void)
130{
wdenk7c7a23b2002-12-07 00:20:59 +0000131#ifdef CONFIG_AMIGAONEG3SE
132 enable_nvram();
133#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200134#if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE)
wdenkc6097192002-11-03 00:24:07 +0000135 ulong crc;
136 uchar data[ENV_SIZE];
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200137 nvram_read (&crc, CONFIG_ENV_ADDR, sizeof(ulong));
138 nvram_read (data, CONFIG_ENV_ADDR+sizeof(ulong), ENV_SIZE);
wdenkc6097192002-11-03 00:24:07 +0000139
140 if (crc32(0, data, ENV_SIZE) == crc) {
Jean-Christophe PLAGNIOL-VILLARD0e8d1582008-09-10 22:48:06 +0200141 gd->env_addr = (ulong)CONFIG_ENV_ADDR + sizeof(long);
wdenkc6097192002-11-03 00:24:07 +0000142#else
143 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
144 gd->env_addr = (ulong)&(env_ptr->data);
145#endif
146 gd->env_valid = 1;
147 } else {
148 gd->env_addr = (ulong)&default_environment[0];
149 gd->env_valid = 0;
150 }
wdenk7c7a23b2002-12-07 00:20:59 +0000151#ifdef CONFIG_AMIGAONEG3SE
152 disable_nvram();
153#endif
wdenkc6097192002-11-03 00:24:07 +0000154 return (0);
155}