Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 2 | /* |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 3 | * (C) Copyright 2000-2010 |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 5 | * |
| 6 | * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 7 | * Andreas Heppel <aheppel@sysgo.de> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * 09-18-2001 Andreas Heppel, Sysgo RTS GmbH <aheppel@sysgo.de> |
| 12 | * |
| 13 | * It might not be possible in all cases to use 'memcpy()' to copy |
| 14 | * the environment to NVRAM, as the NVRAM might not be mapped into |
| 15 | * the memory space. (I.e. this is the case for the BAB750). In those |
| 16 | * cases it might be possible to access the NVRAM using a different |
| 17 | * method. For example, the RTC on the BAB750 is accessible in IO |
| 18 | * space using its address and data registers. To enable usage of |
| 19 | * NVRAM in those cases I invented the functions 'nvram_read()' and |
| 20 | * 'nvram_write()', which will be activated upon the configuration |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 21 | * #define CONFIG_SYS_NVRAM_ACCESS_ROUTINE. Note, that those functions are |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 22 | * strongly dependent on the used HW, and must be redefined for each |
| 23 | * board that wants to use them. |
| 24 | */ |
| 25 | |
| 26 | #include <common.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 27 | #include <command.h> |
Simon Glass | 0af6e2d | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 28 | #include <env.h> |
Simon Glass | 9d1f619 | 2019-08-02 09:44:25 -0600 | [diff] [blame] | 29 | #include <env_internal.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 30 | #include <asm/global_data.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 31 | #include <linux/stddef.h> |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 32 | #include <search.h> |
| 33 | #include <errno.h> |
Simon Glass | 48b6c6b | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 34 | #include <u-boot/crc.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 35 | |
Jean-Christophe PLAGNIOL-VILLARD | d13dd35 | 2008-09-10 22:47:59 +0200 | [diff] [blame] | 36 | DECLARE_GLOBAL_DATA_PTR; |
| 37 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 38 | #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 39 | extern void *nvram_read(void *dest, const long src, size_t count); |
| 40 | extern void nvram_write(long dest, const void *src, size_t count); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 41 | #else |
Rasmus Villemoes | 5d18fc1 | 2020-02-18 08:54:09 +0000 | [diff] [blame] | 42 | static env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 43 | #endif |
| 44 | |
Igor Grinberg | 76c02dc | 2011-12-26 03:33:10 +0000 | [diff] [blame] | 45 | #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE |
Goldschmidt Simon | 9b0d66b | 2018-02-09 20:23:17 +0000 | [diff] [blame] | 46 | /** Call this function from overridden env_get_char_spec() if you need |
| 47 | * this functionality. |
| 48 | */ |
| 49 | int env_nvram_get_char(int index) |
wdenk | bb444c9 | 2002-12-07 00:20:59 +0000 | [diff] [blame] | 50 | { |
wdenk | bb444c9 | 2002-12-07 00:20:59 +0000 | [diff] [blame] | 51 | uchar c; |
| 52 | |
Igor Grinberg | 0448286 | 2011-11-07 01:14:04 +0000 | [diff] [blame] | 53 | nvram_read(&c, CONFIG_ENV_ADDR + index, 1); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 54 | |
wdenk | bb444c9 | 2002-12-07 00:20:59 +0000 | [diff] [blame] | 55 | return c; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 56 | } |
Igor Grinberg | 76c02dc | 2011-12-26 03:33:10 +0000 | [diff] [blame] | 57 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 58 | |
Simon Glass | 9977849 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 59 | static int env_nvram_load(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 60 | { |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 61 | char buf[CONFIG_ENV_SIZE]; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 62 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 63 | #if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE) |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 64 | nvram_read(buf, CONFIG_ENV_ADDR, CONFIG_ENV_SIZE); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 65 | #else |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 66 | memcpy(buf, (void *)CONFIG_ENV_ADDR, CONFIG_ENV_SIZE); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 67 | #endif |
Marek Vasut | dfe4b7e | 2020-07-07 20:51:35 +0200 | [diff] [blame] | 68 | return env_import(buf, 1, H_EXTERNAL); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Simon Glass | 082af92 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 71 | static int env_nvram_save(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 72 | { |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 73 | env_t env_new; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 74 | int rcode = 0; |
| 75 | |
Marek Vasut | d73c129 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 76 | rcode = env_export(&env_new); |
| 77 | if (rcode) |
| 78 | return rcode; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 79 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 80 | #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 81 | nvram_write(CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE); |
| 82 | #else |
| 83 | if (memcpy((char *)CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE) == NULL) |
| 84 | rcode = 1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 85 | #endif |
| 86 | return rcode; |
| 87 | } |
| 88 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 89 | /* |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 90 | * Initialize Environment use |
| 91 | * |
| 92 | * We are still running from ROM, so data use is limited |
| 93 | */ |
Simon Glass | 082af92 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 94 | static int env_nvram_init(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 95 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 96 | #if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 97 | ulong crc; |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 98 | uchar data[ENV_SIZE]; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 99 | |
| 100 | nvram_read(&crc, CONFIG_ENV_ADDR, sizeof(ulong)); |
Igor Grinberg | 0448286 | 2011-11-07 01:14:04 +0000 | [diff] [blame] | 101 | nvram_read(data, CONFIG_ENV_ADDR + sizeof(ulong), ENV_SIZE); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 102 | |
| 103 | if (crc32(0, data, ENV_SIZE) == crc) { |
Igor Grinberg | 0448286 | 2011-11-07 01:14:04 +0000 | [diff] [blame] | 104 | gd->env_addr = (ulong)CONFIG_ENV_ADDR + sizeof(long); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 105 | #else |
| 106 | if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) { |
Igor Grinberg | 0448286 | 2011-11-07 01:14:04 +0000 | [diff] [blame] | 107 | gd->env_addr = (ulong)&env_ptr->data; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 108 | #endif |
Simon Glass | 4bc2ad2 | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 109 | gd->env_valid = ENV_VALID; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 110 | } else { |
Igor Grinberg | 0448286 | 2011-11-07 01:14:04 +0000 | [diff] [blame] | 111 | gd->env_addr = (ulong)&default_environment[0]; |
Simon Glass | 1f39d0b | 2017-08-20 04:45:15 -0600 | [diff] [blame] | 112 | gd->env_valid = ENV_INVALID; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 113 | } |
Igor Grinberg | 0448286 | 2011-11-07 01:14:04 +0000 | [diff] [blame] | 114 | |
| 115 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 116 | } |
Simon Glass | c10a88e | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 117 | |
| 118 | U_BOOT_ENV_LOCATION(nvram) = { |
| 119 | .location = ENVL_NVRAM, |
Simon Glass | d8273ed | 2017-08-03 12:22:03 -0600 | [diff] [blame] | 120 | ENV_NAME("NVRAM") |
Simon Glass | 082af92 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 121 | .load = env_nvram_load, |
| 122 | .save = env_save_ptr(env_nvram_save), |
| 123 | .init = env_nvram_init, |
Simon Glass | c10a88e | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 124 | }; |