wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 2 | * (C) Copyright 2000-2010 |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 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 | |
Wolfgang Denk | 3765b3e | 2013-10-07 13:07:26 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * 09-18-2001 Andreas Heppel, Sysgo RTS GmbH <aheppel@sysgo.de> |
| 13 | * |
| 14 | * It might not be possible in all cases to use 'memcpy()' to copy |
| 15 | * the environment to NVRAM, as the NVRAM might not be mapped into |
| 16 | * the memory space. (I.e. this is the case for the BAB750). In those |
| 17 | * cases it might be possible to access the NVRAM using a different |
| 18 | * method. For example, the RTC on the BAB750 is accessible in IO |
| 19 | * space using its address and data registers. To enable usage of |
| 20 | * NVRAM in those cases I invented the functions 'nvram_read()' and |
| 21 | * 'nvram_write()', which will be activated upon the configuration |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 22 | * #define CONFIG_SYS_NVRAM_ACCESS_ROUTINE. Note, that those functions are |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 23 | * strongly dependent on the used HW, and must be redefined for each |
| 24 | * board that wants to use them. |
| 25 | */ |
| 26 | |
| 27 | #include <common.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 28 | #include <command.h> |
| 29 | #include <environment.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 30 | #include <linux/stddef.h> |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 31 | #include <search.h> |
| 32 | #include <errno.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 33 | |
Jean-Christophe PLAGNIOL-VILLARD | 957a0e6 | 2008-09-10 22:47:59 +0200 | [diff] [blame] | 34 | DECLARE_GLOBAL_DATA_PTR; |
| 35 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 36 | #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 37 | extern void *nvram_read(void *dest, const long src, size_t count); |
| 38 | extern void nvram_write(long dest, const void *src, size_t count); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 39 | #else |
Jean-Christophe PLAGNIOL-VILLARD | 0e8d158 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 40 | env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 41 | #endif |
| 42 | |
Igor Grinberg | bf95df4 | 2011-12-26 03:33:10 +0000 | [diff] [blame] | 43 | #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE |
Simon Glass | 21f6394 | 2017-08-03 12:22:16 -0600 | [diff] [blame] | 44 | static int env_nvram_get_char(int index) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 45 | { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 46 | uchar c; |
| 47 | |
Igor Grinberg | 91494ca | 2011-11-07 01:14:04 +0000 | [diff] [blame] | 48 | nvram_read(&c, CONFIG_ENV_ADDR + index, 1); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 49 | |
| 50 | return c; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 51 | } |
Igor Grinberg | bf95df4 | 2011-12-26 03:33:10 +0000 | [diff] [blame] | 52 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 53 | |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 54 | static int env_nvram_load(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 55 | { |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 56 | char buf[CONFIG_ENV_SIZE]; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 57 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 58 | #if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 59 | nvram_read(buf, CONFIG_ENV_ADDR, CONFIG_ENV_SIZE); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 60 | #else |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 61 | memcpy(buf, (void *)CONFIG_ENV_ADDR, CONFIG_ENV_SIZE); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 62 | #endif |
Simon Goldschmidt | 2166ebf | 2018-01-31 14:47:12 +0100 | [diff] [blame] | 63 | return env_import(buf, 1); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 66 | static int env_nvram_save(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 67 | { |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 68 | env_t env_new; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 69 | int rcode = 0; |
| 70 | |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 71 | rcode = env_export(&env_new); |
| 72 | if (rcode) |
| 73 | return rcode; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 74 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 75 | #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 76 | nvram_write(CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE); |
| 77 | #else |
| 78 | if (memcpy((char *)CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE) == NULL) |
| 79 | rcode = 1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 80 | #endif |
| 81 | return rcode; |
| 82 | } |
| 83 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 84 | /* |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 85 | * Initialize Environment use |
| 86 | * |
| 87 | * We are still running from ROM, so data use is limited |
| 88 | */ |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 89 | static int env_nvram_init(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 90 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 91 | #if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 92 | ulong crc; |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 93 | uchar data[ENV_SIZE]; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 94 | |
| 95 | nvram_read(&crc, CONFIG_ENV_ADDR, sizeof(ulong)); |
Igor Grinberg | 91494ca | 2011-11-07 01:14:04 +0000 | [diff] [blame] | 96 | nvram_read(data, CONFIG_ENV_ADDR + sizeof(ulong), ENV_SIZE); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 97 | |
| 98 | if (crc32(0, data, ENV_SIZE) == crc) { |
Igor Grinberg | 91494ca | 2011-11-07 01:14:04 +0000 | [diff] [blame] | 99 | gd->env_addr = (ulong)CONFIG_ENV_ADDR + sizeof(long); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 100 | #else |
| 101 | if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) { |
Igor Grinberg | 91494ca | 2011-11-07 01:14:04 +0000 | [diff] [blame] | 102 | gd->env_addr = (ulong)&env_ptr->data; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 103 | #endif |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 104 | gd->env_valid = ENV_VALID; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 105 | } else { |
Igor Grinberg | 91494ca | 2011-11-07 01:14:04 +0000 | [diff] [blame] | 106 | gd->env_addr = (ulong)&default_environment[0]; |
Simon Glass | 2d7cb5b | 2017-08-20 04:45:15 -0600 | [diff] [blame] | 107 | gd->env_valid = ENV_INVALID; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 108 | } |
Igor Grinberg | 91494ca | 2011-11-07 01:14:04 +0000 | [diff] [blame] | 109 | |
| 110 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 111 | } |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 112 | |
| 113 | U_BOOT_ENV_LOCATION(nvram) = { |
| 114 | .location = ENVL_NVRAM, |
Simon Glass | ac358be | 2017-08-03 12:22:03 -0600 | [diff] [blame] | 115 | ENV_NAME("NVRAM") |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 116 | #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 117 | .get_char = env_nvram_get_char, |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 118 | #endif |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 119 | .load = env_nvram_load, |
| 120 | .save = env_save_ptr(env_nvram_save), |
| 121 | .init = env_nvram_init, |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 122 | }; |