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