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