Tom Rini | 83d290c | 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 | ea882ba | 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 | 6d0f6bc | 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 | 7b51b57 | 2019-08-01 09:46:52 -0600 | [diff] [blame] | 28 | #include <env.h> |
Simon Glass | f3998fd | 2019-08-02 09:44:25 -0600 | [diff] [blame] | 29 | #include <env_internal.h> |
Simon Glass | 401d1c4 | 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 | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 32 | #include <search.h> |
| 33 | #include <errno.h> |
Simon Glass | 3db7110 | 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 | 957a0e6 | 2008-09-10 22:47:59 +0200 | [diff] [blame] | 36 | DECLARE_GLOBAL_DATA_PTR; |
| 37 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 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 | 46d9d1c | 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 | |
Simon Glass | c595199 | 2017-08-03 12:22:17 -0600 | [diff] [blame] | 45 | static int env_nvram_load(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 46 | { |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 47 | char buf[CONFIG_ENV_SIZE]; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 48 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 49 | #if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE) |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 50 | nvram_read(buf, CONFIG_ENV_ADDR, CONFIG_ENV_SIZE); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 51 | #else |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 52 | memcpy(buf, (void *)CONFIG_ENV_ADDR, CONFIG_ENV_SIZE); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 53 | #endif |
Marek Vasut | 890feec | 2020-07-07 20:51:35 +0200 | [diff] [blame] | 54 | return env_import(buf, 1, H_EXTERNAL); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 57 | static int env_nvram_save(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 58 | { |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 59 | env_t env_new; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 60 | int rcode = 0; |
| 61 | |
Marek Vasut | 7ce1526 | 2014-03-05 19:59:50 +0100 | [diff] [blame] | 62 | rcode = env_export(&env_new); |
| 63 | if (rcode) |
| 64 | return rcode; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 65 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 66 | #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 67 | nvram_write(CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE); |
| 68 | #else |
| 69 | if (memcpy((char *)CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE) == NULL) |
| 70 | rcode = 1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 71 | #endif |
| 72 | return rcode; |
| 73 | } |
| 74 | |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 75 | /* |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 76 | * Initialize Environment use |
| 77 | * |
| 78 | * We are still running from ROM, so data use is limited |
| 79 | */ |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 80 | static int env_nvram_init(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 81 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 82 | #if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 83 | ulong crc; |
Tom Rini | cd0f4fa | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 84 | uchar data[ENV_SIZE]; |
Wolfgang Denk | ea882ba | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 85 | |
| 86 | nvram_read(&crc, CONFIG_ENV_ADDR, sizeof(ulong)); |
Igor Grinberg | 91494ca | 2011-11-07 01:14:04 +0000 | [diff] [blame] | 87 | nvram_read(data, CONFIG_ENV_ADDR + sizeof(ulong), ENV_SIZE); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 88 | |
| 89 | if (crc32(0, data, ENV_SIZE) == crc) { |
Marek Behún | a73c1f0 | 2021-10-22 15:47:17 +0200 | [diff] [blame] | 90 | gd->env_addr = (ulong)CONFIG_ENV_ADDR + sizeof(long); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 91 | #else |
| 92 | if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) { |
Marek Behún | a73c1f0 | 2021-10-22 15:47:17 +0200 | [diff] [blame] | 93 | gd->env_addr = (ulong)&env_ptr->data; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 94 | #endif |
Simon Glass | 203e94f | 2017-08-03 12:21:56 -0600 | [diff] [blame] | 95 | gd->env_valid = ENV_VALID; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 96 | } else { |
Marek Behún | 4735de4 | 2021-10-22 15:47:16 +0200 | [diff] [blame] | 97 | gd->env_valid = ENV_INVALID; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 98 | } |
Igor Grinberg | 91494ca | 2011-11-07 01:14:04 +0000 | [diff] [blame] | 99 | |
| 100 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 101 | } |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 102 | |
| 103 | U_BOOT_ENV_LOCATION(nvram) = { |
| 104 | .location = ENVL_NVRAM, |
Simon Glass | ac358be | 2017-08-03 12:22:03 -0600 | [diff] [blame] | 105 | ENV_NAME("NVRAM") |
Simon Glass | e5bce24 | 2017-08-03 12:22:01 -0600 | [diff] [blame] | 106 | .load = env_nvram_load, |
| 107 | .save = env_save_ptr(env_nvram_save), |
| 108 | .init = env_nvram_init, |
Simon Glass | 4415f1d | 2017-08-03 12:21:58 -0600 | [diff] [blame] | 109 | }; |