wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 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 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 45 | DECLARE_GLOBAL_DATA_PTR; |
| 46 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 47 | #ifdef CFG_ENV_IS_IN_NVRAM /* Environment is in NVRAM */ |
| 48 | |
| 49 | #include <command.h> |
| 50 | #include <environment.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 51 | #include <linux/stddef.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 52 | |
| 53 | #ifdef CFG_NVRAM_ACCESS_ROUTINE |
| 54 | extern void *nvram_read(void *dest, const long src, size_t count); |
| 55 | extern void nvram_write(long dest, const void *src, size_t count); |
| 56 | env_t *env_ptr = NULL; |
| 57 | #else |
| 58 | env_t *env_ptr = (env_t *)CFG_ENV_ADDR; |
| 59 | #endif |
| 60 | |
| 61 | char * env_name_spec = "NVRAM"; |
| 62 | |
| 63 | extern uchar default_environment[]; |
| 64 | extern int default_environment_size; |
| 65 | |
| 66 | extern uchar (*env_get_char)(int); |
| 67 | extern uchar env_get_char_memory (int index); |
| 68 | |
wdenk | bb444c9 | 2002-12-07 00:20:59 +0000 | [diff] [blame] | 69 | #ifdef CONFIG_AMIGAONEG3SE |
| 70 | uchar env_get_char_spec (int index) |
| 71 | { |
| 72 | #ifdef CFG_NVRAM_ACCESS_ROUTINE |
| 73 | uchar c; |
| 74 | |
| 75 | nvram_read(&c, CFG_ENV_ADDR+index, 1); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 76 | |
wdenk | bb444c9 | 2002-12-07 00:20:59 +0000 | [diff] [blame] | 77 | return c; |
| 78 | #else |
wdenk | bb444c9 | 2002-12-07 00:20:59 +0000 | [diff] [blame] | 79 | uchar retval; |
| 80 | enable_nvram(); |
| 81 | retval = *((uchar *)(gd->env_addr + index)); |
| 82 | disable_nvram(); |
| 83 | return retval; |
| 84 | #endif |
| 85 | } |
| 86 | #else |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 87 | uchar env_get_char_spec (int index) |
| 88 | { |
| 89 | #ifdef CFG_NVRAM_ACCESS_ROUTINE |
| 90 | uchar c; |
| 91 | |
| 92 | nvram_read(&c, CFG_ENV_ADDR+index, 1); |
| 93 | |
| 94 | return c; |
| 95 | #else |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 96 | return *((uchar *)(gd->env_addr + index)); |
| 97 | #endif |
| 98 | } |
wdenk | bb444c9 | 2002-12-07 00:20:59 +0000 | [diff] [blame] | 99 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 100 | |
| 101 | void env_relocate_spec (void) |
| 102 | { |
| 103 | #if defined(CFG_NVRAM_ACCESS_ROUTINE) |
| 104 | nvram_read(env_ptr, CFG_ENV_ADDR, CFG_ENV_SIZE); |
| 105 | #else |
| 106 | memcpy (env_ptr, (void*)CFG_ENV_ADDR, CFG_ENV_SIZE); |
| 107 | #endif |
| 108 | } |
| 109 | |
| 110 | int saveenv (void) |
| 111 | { |
| 112 | int rcode = 0; |
wdenk | bb444c9 | 2002-12-07 00:20:59 +0000 | [diff] [blame] | 113 | #ifdef CONFIG_AMIGAONEG3SE |
| 114 | enable_nvram(); |
| 115 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 116 | #ifdef CFG_NVRAM_ACCESS_ROUTINE |
| 117 | nvram_write(CFG_ENV_ADDR, env_ptr, CFG_ENV_SIZE); |
| 118 | #else |
| 119 | if (memcpy ((char *)CFG_ENV_ADDR, env_ptr, CFG_ENV_SIZE) == NULL) |
| 120 | rcode = 1 ; |
| 121 | #endif |
wdenk | bb444c9 | 2002-12-07 00:20:59 +0000 | [diff] [blame] | 122 | #ifdef CONFIG_AMIGAONEG3SE |
| 123 | udelay(10000); |
| 124 | disable_nvram(); |
| 125 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 126 | return rcode; |
| 127 | } |
| 128 | |
| 129 | |
| 130 | /************************************************************************ |
| 131 | * Initialize Environment use |
| 132 | * |
| 133 | * We are still running from ROM, so data use is limited |
| 134 | */ |
| 135 | int env_init (void) |
| 136 | { |
wdenk | bb444c9 | 2002-12-07 00:20:59 +0000 | [diff] [blame] | 137 | #ifdef CONFIG_AMIGAONEG3SE |
| 138 | enable_nvram(); |
| 139 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 140 | #if defined(CFG_NVRAM_ACCESS_ROUTINE) |
| 141 | ulong crc; |
| 142 | uchar data[ENV_SIZE]; |
| 143 | nvram_read (&crc, CFG_ENV_ADDR, sizeof(ulong)); |
| 144 | nvram_read (data, CFG_ENV_ADDR+sizeof(ulong), ENV_SIZE); |
| 145 | |
| 146 | if (crc32(0, data, ENV_SIZE) == crc) { |
| 147 | gd->env_addr = (ulong)CFG_ENV_ADDR + sizeof(long); |
| 148 | #else |
| 149 | if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) { |
| 150 | gd->env_addr = (ulong)&(env_ptr->data); |
| 151 | #endif |
| 152 | gd->env_valid = 1; |
| 153 | } else { |
| 154 | gd->env_addr = (ulong)&default_environment[0]; |
| 155 | gd->env_valid = 0; |
| 156 | } |
wdenk | bb444c9 | 2002-12-07 00:20:59 +0000 | [diff] [blame] | 157 | #ifdef CONFIG_AMIGAONEG3SE |
| 158 | disable_nvram(); |
| 159 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 160 | return (0); |
| 161 | } |
| 162 | |
| 163 | #endif /* CFG_ENV_IS_IN_NVRAM */ |