2 * BCM947xx nvram variable access
4 * Copyright (C) 2005 Broadcom Corporation
5 * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
6 * Copyright (C) 2010-2012 Hauke Mehrtens <hauke@hauke-m.de>
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
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
15 #include <linux/types.h>
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/string.h>
19 #include <linux/mtd/mtd.h>
20 #include <linux/bcm47xx_nvram.h>
22 #define NVRAM_MAGIC 0x48534C46 /* 'FLSH' */
23 #define NVRAM_SPACE 0x10000
24 #define NVRAM_MAX_GPIO_ENTRIES 32
25 #define NVRAM_MAX_GPIO_VALUE_LEN 30
27 #define FLASH_MIN 0x00020000 /* Minimum flash size */
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 */
37 static char nvram_buf[NVRAM_SPACE];
38 static size_t nvram_len;
39 static const u32 nvram_sizes[] = {0x8000, 0xF000, 0x10000};
41 static u32 find_nvram_size(void __iomem *end)
43 struct nvram_header __iomem *header;
46 for (i = 0; i < ARRAY_SIZE(nvram_sizes); i++) {
47 header = (struct nvram_header *)(end - nvram_sizes[i]);
48 if (header->magic == NVRAM_MAGIC)
49 return nvram_sizes[i];
55 /* Probe for NVRAM header */
56 static int nvram_find_and_copy(void __iomem *iobase, u32 lim)
58 struct nvram_header __iomem *header;
63 pr_warn("nvram already initialized\n");
67 /* TODO: when nvram is on nand flash check for bad blocks first. */
70 /* Windowed flash access */
71 size = find_nvram_size(iobase + off);
73 header = (struct nvram_header *)(iobase + off - size);
79 /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
80 header = (struct nvram_header *)(iobase + 4096);
81 if (header->magic == NVRAM_MAGIC) {
86 header = (struct nvram_header *)(iobase + 1024);
87 if (header->magic == NVRAM_MAGIC) {
92 pr_err("no nvram found\n");
96 __ioread32_copy(nvram_buf, header, sizeof(*header) / 4);
97 header = (struct nvram_header *)nvram_buf;
98 nvram_len = header->len;
99 if (nvram_len > size) {
100 pr_err("The nvram size according to the header seems to be bigger than the partition on flash\n");
103 if (nvram_len >= NVRAM_SPACE) {
104 pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
105 header->len, NVRAM_SPACE - 1);
106 nvram_len = NVRAM_SPACE - 1;
108 /* proceed reading data after header */
109 __ioread32_copy(nvram_buf + sizeof(*header), header + 1,
110 DIV_ROUND_UP(nvram_len, 4));
111 nvram_buf[NVRAM_SPACE - 1] = '\0';
117 * On bcm47xx we need access to the NVRAM very early, so we can't use mtd
118 * subsystem to access flash. We can't even use platform device / driver to
119 * store memory offset.
120 * To handle this we provide following symbol. It's supposed to be called as
121 * soon as we get info about flash device, before any NVRAM entry is needed.
123 int bcm47xx_nvram_init_from_mem(u32 base, u32 lim)
125 void __iomem *iobase;
128 iobase = ioremap_nocache(base, lim);
132 err = nvram_find_and_copy(iobase, lim);
139 static int nvram_init(void)
142 struct mtd_info *mtd;
143 struct nvram_header header;
147 mtd = get_mtd_device_nm("nvram");
151 err = mtd_read(mtd, 0, sizeof(header), &bytes_read, (uint8_t *)&header);
152 if (!err && header.magic == NVRAM_MAGIC &&
153 header.len > sizeof(header)) {
154 nvram_len = header.len;
155 if (nvram_len >= NVRAM_SPACE) {
156 pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
157 header.len, NVRAM_SPACE);
158 nvram_len = NVRAM_SPACE - 1;
161 err = mtd_read(mtd, 0, nvram_len, &nvram_len,
170 int bcm47xx_nvram_getenv(const char *name, char *val, size_t val_len)
172 char *var, *value, *end, *eq;
184 /* Look for name=value and return value */
185 var = &nvram_buf[sizeof(struct nvram_header)];
186 end = nvram_buf + sizeof(nvram_buf);
187 while (var < end && *var) {
188 eq = strchr(var, '=');
192 if (eq - var == strlen(name) &&
193 strncmp(var, name, eq - var) == 0)
194 return snprintf(val, val_len, "%s", value);
195 var = value + strlen(value) + 1;
199 EXPORT_SYMBOL(bcm47xx_nvram_getenv);
201 int bcm47xx_nvram_gpio_pin(const char *name)
204 char nvram_var[] = "gpioXX";
205 char buf[NVRAM_MAX_GPIO_VALUE_LEN];
207 /* TODO: Optimize it to don't call getenv so many times */
208 for (i = 0; i < NVRAM_MAX_GPIO_ENTRIES; i++) {
209 err = snprintf(nvram_var, sizeof(nvram_var), "gpio%i", i);
212 err = bcm47xx_nvram_getenv(nvram_var, buf, sizeof(buf));
215 if (!strcmp(name, buf))
220 EXPORT_SYMBOL(bcm47xx_nvram_gpio_pin);
222 char *bcm47xx_nvram_get_contents(size_t *nvram_size)
233 *nvram_size = nvram_len - sizeof(struct nvram_header);
234 nvram = vmalloc(*nvram_size);
237 memcpy(nvram, &nvram_buf[sizeof(struct nvram_header)], *nvram_size);
241 EXPORT_SYMBOL(bcm47xx_nvram_get_contents);
243 MODULE_LICENSE("GPL v2");