1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * BCM947xx nvram variable access
5 * Copyright (C) 2005 Broadcom Corporation
11 #include <linux/types.h>
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/string.h>
15 #include <linux/mtd/mtd.h>
16 #include <linux/bcm47xx_nvram.h>
18 #define NVRAM_MAGIC 0x48534C46 /* 'FLSH' */
19 #define NVRAM_SPACE 0x10000
20 #define NVRAM_MAX_GPIO_ENTRIES 32
21 #define NVRAM_MAX_GPIO_VALUE_LEN 30
23 #define FLASH_MIN 0x00020000 /* Minimum flash size */
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 */
33 static char nvram_buf[NVRAM_SPACE];
34 static size_t nvram_len;
35 static const u32 nvram_sizes[] = {0x6000, 0x8000, 0xF000, 0x10000};
38 * bcm47xx_nvram_is_valid - check for a valid NVRAM at specified memory
40 static bool bcm47xx_nvram_is_valid(void __iomem *nvram)
42 return ((struct nvram_header *)nvram)->magic == NVRAM_MAGIC;
46 * bcm47xx_nvram_copy - copy NVRAM to internal buffer
48 static void bcm47xx_nvram_copy(void __iomem *nvram_start, size_t res_size)
50 struct nvram_header __iomem *header = nvram_start;
53 copy_size = header->len;
54 if (copy_size > res_size) {
55 pr_err("The nvram size according to the header seems to be bigger than the partition on flash\n");
58 if (copy_size >= NVRAM_SPACE) {
59 pr_err("nvram on flash (%zu bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
60 copy_size, NVRAM_SPACE - 1);
61 copy_size = NVRAM_SPACE - 1;
64 __ioread32_copy(nvram_buf, nvram_start, DIV_ROUND_UP(copy_size, 4));
65 nvram_buf[NVRAM_SPACE - 1] = '\0';
66 nvram_len = copy_size;
70 * bcm47xx_nvram_find_and_copy - find NVRAM on flash mapping & copy it
72 static int bcm47xx_nvram_find_and_copy(void __iomem *flash_start, size_t res_size)
79 pr_warn("nvram already initialized\n");
83 /* TODO: when nvram is on nand flash check for bad blocks first. */
85 /* Try every possible flash size and check for NVRAM at its end */
86 for (flash_size = FLASH_MIN; flash_size <= res_size; flash_size <<= 1) {
87 for (i = 0; i < ARRAY_SIZE(nvram_sizes); i++) {
88 offset = flash_size - nvram_sizes[i];
89 if (bcm47xx_nvram_is_valid(flash_start + offset))
94 /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
97 if (bcm47xx_nvram_is_valid(flash_start + offset))
101 if (bcm47xx_nvram_is_valid(flash_start + offset))
104 pr_err("no nvram found\n");
108 bcm47xx_nvram_copy(flash_start + offset, res_size - offset);
114 * On bcm47xx we need access to the NVRAM very early, so we can't use mtd
115 * subsystem to access flash. We can't even use platform device / driver to
116 * store memory offset.
117 * To handle this we provide following symbol. It's supposed to be called as
118 * soon as we get info about flash device, before any NVRAM entry is needed.
120 int bcm47xx_nvram_init_from_mem(u32 base, u32 lim)
122 void __iomem *iobase;
125 iobase = ioremap(base, lim);
129 err = bcm47xx_nvram_find_and_copy(iobase, lim);
136 static int nvram_init(void)
139 struct mtd_info *mtd;
140 struct nvram_header header;
144 mtd = get_mtd_device_nm("nvram");
148 err = mtd_read(mtd, 0, sizeof(header), &bytes_read, (uint8_t *)&header);
149 if (!err && header.magic == NVRAM_MAGIC &&
150 header.len > sizeof(header)) {
151 nvram_len = header.len;
152 if (nvram_len >= NVRAM_SPACE) {
153 pr_err("nvram on flash (%zu bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
154 nvram_len, NVRAM_SPACE);
155 nvram_len = NVRAM_SPACE - 1;
158 err = mtd_read(mtd, 0, nvram_len, &nvram_len,
167 int bcm47xx_nvram_getenv(const char *name, char *val, size_t val_len)
169 char *var, *value, *end, *eq;
181 /* Look for name=value and return value */
182 var = &nvram_buf[sizeof(struct nvram_header)];
183 end = nvram_buf + sizeof(nvram_buf);
184 while (var < end && *var) {
185 eq = strchr(var, '=');
189 if (eq - var == strlen(name) &&
190 strncmp(var, name, eq - var) == 0)
191 return snprintf(val, val_len, "%s", value);
192 var = value + strlen(value) + 1;
196 EXPORT_SYMBOL(bcm47xx_nvram_getenv);
198 int bcm47xx_nvram_gpio_pin(const char *name)
201 char nvram_var[] = "gpioXX";
202 char buf[NVRAM_MAX_GPIO_VALUE_LEN];
204 /* TODO: Optimize it to don't call getenv so many times */
205 for (i = 0; i < NVRAM_MAX_GPIO_ENTRIES; i++) {
206 err = snprintf(nvram_var, sizeof(nvram_var), "gpio%i", i);
209 err = bcm47xx_nvram_getenv(nvram_var, buf, sizeof(buf));
212 if (!strcmp(name, buf))
217 EXPORT_SYMBOL(bcm47xx_nvram_gpio_pin);
219 char *bcm47xx_nvram_get_contents(size_t *nvram_size)
230 *nvram_size = nvram_len - sizeof(struct nvram_header);
231 nvram = vmalloc(*nvram_size);
234 memcpy(nvram, &nvram_buf[sizeof(struct nvram_header)], *nvram_size);
238 EXPORT_SYMBOL(bcm47xx_nvram_get_contents);
240 MODULE_LICENSE("GPL v2");