Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 2 | /* |
| 3 | * BCM947xx nvram variable access |
| 4 | * |
| 5 | * Copyright (C) 2005 Broadcom Corporation |
| 6 | * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org> |
Hauke Mehrtens | f36738d | 2012-12-26 19:51:13 +0000 | [diff] [blame] | 7 | * Copyright (C) 2010-2012 Hauke Mehrtens <hauke@hauke-m.de> |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 8 | */ |
| 9 | |
Rafał Miłecki | 50c9791 | 2015-04-01 08:23:03 +0200 | [diff] [blame] | 10 | #include <linux/io.h> |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 11 | #include <linux/types.h> |
| 12 | #include <linux/module.h> |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 13 | #include <linux/kernel.h> |
| 14 | #include <linux/string.h> |
Rafał Miłecki | 9d1d086 | 2014-10-29 10:05:06 +0100 | [diff] [blame] | 15 | #include <linux/mtd/mtd.h> |
Rafał Miłecki | 138173d | 2014-12-01 07:58:18 +0100 | [diff] [blame] | 16 | #include <linux/bcm47xx_nvram.h> |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 17 | |
Rafał Miłecki | d548ca6 | 2014-12-10 17:38:26 +0100 | [diff] [blame] | 18 | #define NVRAM_MAGIC 0x48534C46 /* 'FLSH' */ |
Rafał Miłecki | 6ab7c29 | 2015-04-01 08:23:04 +0200 | [diff] [blame] | 19 | #define NVRAM_SPACE 0x10000 |
Rafał Miłecki | d548ca6 | 2014-12-10 17:38:26 +0100 | [diff] [blame] | 20 | #define NVRAM_MAX_GPIO_ENTRIES 32 |
| 21 | #define NVRAM_MAX_GPIO_VALUE_LEN 30 |
Rafał Miłecki | 341097f | 2014-10-30 12:50:03 +0100 | [diff] [blame] | 22 | |
| 23 | #define FLASH_MIN 0x00020000 /* Minimum flash size */ |
| 24 | |
| 25 | struct nvram_header { |
| 26 | u32 magic; |
| 27 | u32 len; |
| 28 | u32 crc_ver_init; /* 0:7 crc, 8:15 ver, 16:31 sdram_init */ |
| 29 | u32 config_refresh; /* 0:15 sdram_config, 16:31 sdram_refresh */ |
| 30 | u32 config_ncdl; /* ncdl values for memc */ |
| 31 | }; |
| 32 | |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 33 | static char nvram_buf[NVRAM_SPACE]; |
Rafał Miłecki | 94a0535 | 2015-06-06 23:16:23 +0200 | [diff] [blame] | 34 | static size_t nvram_len; |
Rafał Miłecki | 3bc6505 | 2018-04-08 22:57:33 +0200 | [diff] [blame] | 35 | static const u32 nvram_sizes[] = {0x6000, 0x8000, 0xF000, 0x10000}; |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 36 | |
Rafał Miłecki | 8d602dd | 2014-09-03 22:51:06 +0200 | [diff] [blame] | 37 | static u32 find_nvram_size(void __iomem *end) |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 38 | { |
Rafał Miłecki | 8d602dd | 2014-09-03 22:51:06 +0200 | [diff] [blame] | 39 | struct nvram_header __iomem *header; |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 40 | int i; |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 41 | |
Hauke Mehrtens | f36738d | 2012-12-26 19:51:13 +0000 | [diff] [blame] | 42 | for (i = 0; i < ARRAY_SIZE(nvram_sizes); i++) { |
Rafał Miłecki | 8d602dd | 2014-09-03 22:51:06 +0200 | [diff] [blame] | 43 | header = (struct nvram_header *)(end - nvram_sizes[i]); |
Rafał Miłecki | 341097f | 2014-10-30 12:50:03 +0100 | [diff] [blame] | 44 | if (header->magic == NVRAM_MAGIC) |
Hauke Mehrtens | f36738d | 2012-12-26 19:51:13 +0000 | [diff] [blame] | 45 | return nvram_sizes[i]; |
Hauke Mehrtens | 08ccf572 | 2011-07-23 01:20:12 +0200 | [diff] [blame] | 46 | } |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 47 | |
Hauke Mehrtens | f36738d | 2012-12-26 19:51:13 +0000 | [diff] [blame] | 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | /* Probe for NVRAM header */ |
Rafał Miłecki | 8d602dd | 2014-09-03 22:51:06 +0200 | [diff] [blame] | 52 | static int nvram_find_and_copy(void __iomem *iobase, u32 lim) |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 53 | { |
Rafał Miłecki | 8d602dd | 2014-09-03 22:51:06 +0200 | [diff] [blame] | 54 | struct nvram_header __iomem *header; |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 55 | u32 off; |
Hauke Mehrtens | f36738d | 2012-12-26 19:51:13 +0000 | [diff] [blame] | 56 | u32 size; |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 57 | |
Rafał Miłecki | 94a0535 | 2015-06-06 23:16:23 +0200 | [diff] [blame] | 58 | if (nvram_len) { |
Rafał Miłecki | 8d602dd | 2014-09-03 22:51:06 +0200 | [diff] [blame] | 59 | pr_warn("nvram already initialized\n"); |
| 60 | return -EEXIST; |
| 61 | } |
| 62 | |
Hauke Mehrtens | c4485671 | 2012-12-26 19:51:11 +0000 | [diff] [blame] | 63 | /* TODO: when nvram is on nand flash check for bad blocks first. */ |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 64 | off = FLASH_MIN; |
| 65 | while (off <= lim) { |
| 66 | /* Windowed flash access */ |
Rafał Miłecki | 8d602dd | 2014-09-03 22:51:06 +0200 | [diff] [blame] | 67 | size = find_nvram_size(iobase + off); |
Hauke Mehrtens | f36738d | 2012-12-26 19:51:13 +0000 | [diff] [blame] | 68 | if (size) { |
Rafał Miłecki | 8d602dd | 2014-09-03 22:51:06 +0200 | [diff] [blame] | 69 | header = (struct nvram_header *)(iobase + off - size); |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 70 | goto found; |
Hauke Mehrtens | f36738d | 2012-12-26 19:51:13 +0000 | [diff] [blame] | 71 | } |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 72 | off <<= 1; |
| 73 | } |
| 74 | |
| 75 | /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */ |
Rafał Miłecki | 8d602dd | 2014-09-03 22:51:06 +0200 | [diff] [blame] | 76 | header = (struct nvram_header *)(iobase + 4096); |
Rafał Miłecki | 341097f | 2014-10-30 12:50:03 +0100 | [diff] [blame] | 77 | if (header->magic == NVRAM_MAGIC) { |
Hauke Mehrtens | f36738d | 2012-12-26 19:51:13 +0000 | [diff] [blame] | 78 | size = NVRAM_SPACE; |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 79 | goto found; |
Hauke Mehrtens | f36738d | 2012-12-26 19:51:13 +0000 | [diff] [blame] | 80 | } |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 81 | |
Rafał Miłecki | 8d602dd | 2014-09-03 22:51:06 +0200 | [diff] [blame] | 82 | header = (struct nvram_header *)(iobase + 1024); |
Rafał Miłecki | 341097f | 2014-10-30 12:50:03 +0100 | [diff] [blame] | 83 | if (header->magic == NVRAM_MAGIC) { |
Hauke Mehrtens | f36738d | 2012-12-26 19:51:13 +0000 | [diff] [blame] | 84 | size = NVRAM_SPACE; |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 85 | goto found; |
Hauke Mehrtens | f36738d | 2012-12-26 19:51:13 +0000 | [diff] [blame] | 86 | } |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 87 | |
Hauke Mehrtens | f36738d | 2012-12-26 19:51:13 +0000 | [diff] [blame] | 88 | pr_err("no nvram found\n"); |
Hauke Mehrtens | cc4403e | 2012-12-26 19:51:10 +0000 | [diff] [blame] | 89 | return -ENXIO; |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 90 | |
| 91 | found: |
Stephen Boyd | 1f330c3 | 2016-01-20 14:58:41 -0800 | [diff] [blame] | 92 | __ioread32_copy(nvram_buf, header, sizeof(*header) / 4); |
Aaro Koskinen | 4c11e55 | 2016-03-17 14:17:20 -0700 | [diff] [blame] | 93 | nvram_len = ((struct nvram_header *)(nvram_buf))->len; |
Rafał Miłecki | 94a0535 | 2015-06-06 23:16:23 +0200 | [diff] [blame] | 94 | if (nvram_len > size) { |
Hante Meuleman | 55cab93b | 2015-05-21 15:27:23 +0200 | [diff] [blame] | 95 | pr_err("The nvram size according to the header seems to be bigger than the partition on flash\n"); |
Rafał Miłecki | 94a0535 | 2015-06-06 23:16:23 +0200 | [diff] [blame] | 96 | nvram_len = size; |
Hante Meuleman | 55cab93b | 2015-05-21 15:27:23 +0200 | [diff] [blame] | 97 | } |
Rafał Miłecki | 94a0535 | 2015-06-06 23:16:23 +0200 | [diff] [blame] | 98 | if (nvram_len >= NVRAM_SPACE) { |
Florian Fainelli | feb4eb0 | 2019-05-14 10:38:14 -0700 | [diff] [blame] | 99 | pr_err("nvram on flash (%zu bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n", |
Aaro Koskinen | 4c11e55 | 2016-03-17 14:17:20 -0700 | [diff] [blame] | 100 | nvram_len, NVRAM_SPACE - 1); |
Rafał Miłecki | 94a0535 | 2015-06-06 23:16:23 +0200 | [diff] [blame] | 101 | nvram_len = NVRAM_SPACE - 1; |
Hante Meuleman | 55cab93b | 2015-05-21 15:27:23 +0200 | [diff] [blame] | 102 | } |
| 103 | /* proceed reading data after header */ |
Stephen Boyd | 1f330c3 | 2016-01-20 14:58:41 -0800 | [diff] [blame] | 104 | __ioread32_copy(nvram_buf + sizeof(*header), header + 1, |
| 105 | DIV_ROUND_UP(nvram_len, 4)); |
Rafał Miłecki | d28c9a5 | 2015-05-12 18:46:11 +0200 | [diff] [blame] | 106 | nvram_buf[NVRAM_SPACE - 1] = '\0'; |
Hauke Mehrtens | cc4403e | 2012-12-26 19:51:10 +0000 | [diff] [blame] | 107 | |
| 108 | return 0; |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Rafał Miłecki | 21400f2 | 2014-09-03 22:59:45 +0200 | [diff] [blame] | 111 | /* |
| 112 | * On bcm47xx we need access to the NVRAM very early, so we can't use mtd |
| 113 | * subsystem to access flash. We can't even use platform device / driver to |
| 114 | * store memory offset. |
| 115 | * To handle this we provide following symbol. It's supposed to be called as |
| 116 | * soon as we get info about flash device, before any NVRAM entry is needed. |
| 117 | */ |
| 118 | int bcm47xx_nvram_init_from_mem(u32 base, u32 lim) |
Rafał Miłecki | 8d602dd | 2014-09-03 22:51:06 +0200 | [diff] [blame] | 119 | { |
| 120 | void __iomem *iobase; |
| 121 | int err; |
| 122 | |
Christoph Hellwig | 4bdc0d6 | 2020-01-06 09:43:50 +0100 | [diff] [blame] | 123 | iobase = ioremap(base, lim); |
Rafał Miłecki | 8d602dd | 2014-09-03 22:51:06 +0200 | [diff] [blame] | 124 | if (!iobase) |
| 125 | return -ENOMEM; |
| 126 | |
| 127 | err = nvram_find_and_copy(iobase, lim); |
| 128 | |
| 129 | iounmap(iobase); |
| 130 | |
| 131 | return err; |
| 132 | } |
| 133 | |
Hauke Mehrtens | e58da16 | 2012-12-26 19:51:12 +0000 | [diff] [blame] | 134 | static int nvram_init(void) |
Rafał Miłecki | bb76563 | 2012-12-26 08:29:17 +0000 | [diff] [blame] | 135 | { |
Rafał Miłecki | 9d1d086 | 2014-10-29 10:05:06 +0100 | [diff] [blame] | 136 | #ifdef CONFIG_MTD |
| 137 | struct mtd_info *mtd; |
| 138 | struct nvram_header header; |
| 139 | size_t bytes_read; |
Rafał Miłecki | 40d1217 | 2015-04-01 08:23:05 +0200 | [diff] [blame] | 140 | int err; |
Rafał Miłecki | 9d1d086 | 2014-10-29 10:05:06 +0100 | [diff] [blame] | 141 | |
| 142 | mtd = get_mtd_device_nm("nvram"); |
| 143 | if (IS_ERR(mtd)) |
| 144 | return -ENODEV; |
| 145 | |
Rafał Miłecki | 40d1217 | 2015-04-01 08:23:05 +0200 | [diff] [blame] | 146 | err = mtd_read(mtd, 0, sizeof(header), &bytes_read, (uint8_t *)&header); |
Hante Meuleman | 55cab93b | 2015-05-21 15:27:23 +0200 | [diff] [blame] | 147 | if (!err && header.magic == NVRAM_MAGIC && |
| 148 | header.len > sizeof(header)) { |
Rafał Miłecki | 94a0535 | 2015-06-06 23:16:23 +0200 | [diff] [blame] | 149 | nvram_len = header.len; |
| 150 | if (nvram_len >= NVRAM_SPACE) { |
Florian Fainelli | feb4eb0 | 2019-05-14 10:38:14 -0700 | [diff] [blame] | 151 | pr_err("nvram on flash (%zu bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n", |
Linus Torvalds | 5d4156a | 2019-09-22 11:31:15 -0700 | [diff] [blame] | 152 | nvram_len, NVRAM_SPACE); |
Rafał Miłecki | 94a0535 | 2015-06-06 23:16:23 +0200 | [diff] [blame] | 153 | nvram_len = NVRAM_SPACE - 1; |
Rafał Miłecki | 9d1d086 | 2014-10-29 10:05:06 +0100 | [diff] [blame] | 154 | } |
Rafał Miłecki | 40d1217 | 2015-04-01 08:23:05 +0200 | [diff] [blame] | 155 | |
Rafał Miłecki | 94a0535 | 2015-06-06 23:16:23 +0200 | [diff] [blame] | 156 | err = mtd_read(mtd, 0, nvram_len, &nvram_len, |
Hante Meuleman | 55cab93b | 2015-05-21 15:27:23 +0200 | [diff] [blame] | 157 | (u8 *)nvram_buf); |
Rafał Miłecki | 94a0535 | 2015-06-06 23:16:23 +0200 | [diff] [blame] | 158 | return err; |
Rafał Miłecki | 9d1d086 | 2014-10-29 10:05:06 +0100 | [diff] [blame] | 159 | } |
| 160 | #endif |
Rafał Miłecki | 7177efc | 2014-10-28 13:30:23 +0100 | [diff] [blame] | 161 | |
Hauke Mehrtens | cc4403e | 2012-12-26 19:51:10 +0000 | [diff] [blame] | 162 | return -ENXIO; |
Rafał Miłecki | bb76563 | 2012-12-26 08:29:17 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Rafał Miłecki | 341097f | 2014-10-30 12:50:03 +0100 | [diff] [blame] | 165 | int bcm47xx_nvram_getenv(const char *name, char *val, size_t val_len) |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 166 | { |
| 167 | char *var, *value, *end, *eq; |
Rafał Miłecki | 5bfad4e | 2015-05-12 18:46:12 +0200 | [diff] [blame] | 168 | int err; |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 169 | |
| 170 | if (!name) |
Hauke Mehrtens | ee7e2f3 | 2012-12-26 19:51:09 +0000 | [diff] [blame] | 171 | return -EINVAL; |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 172 | |
Rafał Miłecki | 94a0535 | 2015-06-06 23:16:23 +0200 | [diff] [blame] | 173 | if (!nvram_len) { |
Hauke Mehrtens | e58da16 | 2012-12-26 19:51:12 +0000 | [diff] [blame] | 174 | err = nvram_init(); |
Hauke Mehrtens | cc4403e | 2012-12-26 19:51:10 +0000 | [diff] [blame] | 175 | if (err) |
| 176 | return err; |
| 177 | } |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 178 | |
| 179 | /* Look for name=value and return value */ |
| 180 | var = &nvram_buf[sizeof(struct nvram_header)]; |
Rafał Miłecki | 5bfad4e | 2015-05-12 18:46:12 +0200 | [diff] [blame] | 181 | end = nvram_buf + sizeof(nvram_buf); |
| 182 | while (var < end && *var) { |
| 183 | eq = strchr(var, '='); |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 184 | if (!eq) |
| 185 | break; |
| 186 | value = eq + 1; |
Rafał Miłecki | d548ca6 | 2014-12-10 17:38:26 +0100 | [diff] [blame] | 187 | if (eq - var == strlen(name) && |
| 188 | strncmp(var, name, eq - var) == 0) |
Hauke Mehrtens | 44d4b2a | 2012-02-28 00:56:11 +0100 | [diff] [blame] | 189 | return snprintf(val, val_len, "%s", value); |
Rafał Miłecki | 5bfad4e | 2015-05-12 18:46:12 +0200 | [diff] [blame] | 190 | var = value + strlen(value) + 1; |
Rafał Miłecki | 50c9791 | 2015-04-01 08:23:03 +0200 | [diff] [blame] | 191 | } |
Hauke Mehrtens | ee7e2f3 | 2012-12-26 19:51:09 +0000 | [diff] [blame] | 192 | return -ENOENT; |
Waldemar Brodkorb | 121915c | 2010-06-08 19:06:01 +0200 | [diff] [blame] | 193 | } |
Hauke Mehrtens | 111bd98 | 2012-12-26 19:51:14 +0000 | [diff] [blame] | 194 | EXPORT_SYMBOL(bcm47xx_nvram_getenv); |
Hauke Mehrtens | 62cf3bc | 2013-09-18 13:31:15 +0200 | [diff] [blame] | 195 | |
| 196 | int bcm47xx_nvram_gpio_pin(const char *name) |
| 197 | { |
| 198 | int i, err; |
Rafał Miłecki | d548ca6 | 2014-12-10 17:38:26 +0100 | [diff] [blame] | 199 | char nvram_var[] = "gpioXX"; |
| 200 | char buf[NVRAM_MAX_GPIO_VALUE_LEN]; |
Hauke Mehrtens | 62cf3bc | 2013-09-18 13:31:15 +0200 | [diff] [blame] | 201 | |
Rafał Miłecki | d548ca6 | 2014-12-10 17:38:26 +0100 | [diff] [blame] | 202 | /* TODO: Optimize it to don't call getenv so many times */ |
| 203 | for (i = 0; i < NVRAM_MAX_GPIO_ENTRIES; i++) { |
Hauke Mehrtens | 62cf3bc | 2013-09-18 13:31:15 +0200 | [diff] [blame] | 204 | err = snprintf(nvram_var, sizeof(nvram_var), "gpio%i", i); |
| 205 | if (err <= 0) |
| 206 | continue; |
| 207 | err = bcm47xx_nvram_getenv(nvram_var, buf, sizeof(buf)); |
| 208 | if (err <= 0) |
| 209 | continue; |
| 210 | if (!strcmp(name, buf)) |
| 211 | return i; |
| 212 | } |
| 213 | return -ENOENT; |
| 214 | } |
| 215 | EXPORT_SYMBOL(bcm47xx_nvram_gpio_pin); |
Hante Meuleman | 55cab93b | 2015-05-21 15:27:23 +0200 | [diff] [blame] | 216 | |
| 217 | char *bcm47xx_nvram_get_contents(size_t *nvram_size) |
| 218 | { |
| 219 | int err; |
| 220 | char *nvram; |
Hante Meuleman | 55cab93b | 2015-05-21 15:27:23 +0200 | [diff] [blame] | 221 | |
Rafał Miłecki | 94a0535 | 2015-06-06 23:16:23 +0200 | [diff] [blame] | 222 | if (!nvram_len) { |
Hante Meuleman | 55cab93b | 2015-05-21 15:27:23 +0200 | [diff] [blame] | 223 | err = nvram_init(); |
| 224 | if (err) |
| 225 | return NULL; |
| 226 | } |
| 227 | |
Rafał Miłecki | 94a0535 | 2015-06-06 23:16:23 +0200 | [diff] [blame] | 228 | *nvram_size = nvram_len - sizeof(struct nvram_header); |
Hante Meuleman | 55cab93b | 2015-05-21 15:27:23 +0200 | [diff] [blame] | 229 | nvram = vmalloc(*nvram_size); |
| 230 | if (!nvram) |
| 231 | return NULL; |
| 232 | memcpy(nvram, &nvram_buf[sizeof(struct nvram_header)], *nvram_size); |
| 233 | |
| 234 | return nvram; |
| 235 | } |
| 236 | EXPORT_SYMBOL(bcm47xx_nvram_get_contents); |
Rafał Miłecki | f6e734a | 2015-06-10 23:05:08 +0200 | [diff] [blame] | 237 | |
Axel Lin | 72ccc89 | 2015-08-19 10:32:23 +0800 | [diff] [blame] | 238 | MODULE_LICENSE("GPL v2"); |