2 * NVRAM variable manipulation (Linux kernel half)
4 * Copyright 2006, Broadcom Corporation
7 * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
8 * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
9 * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
10 * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
14 #include <linux/config.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/string.h>
19 #include <linux/interrupt.h>
20 #include <linux/spinlock.h>
21 #include <linux/slab.h>
22 #include <linux/bootmem.h>
23 #include <linux/wrapper.h>
25 #include <linux/miscdevice.h>
26 #include <linux/mtd/mtd.h>
27 #include <asm/addrspace.h>
29 #include <asm/uaccess.h>
33 #include <bcmendian.h>
41 /* In BSS to minimize text size and page aligned so it can be mmap()-ed */
42 static char nvram_buf[NVRAM_SPACE] __attribute__((aligned(PAGE_SIZE)));
44 /* Global SB handle */
45 extern void *bcm947xx_sbh;
46 extern spinlock_t bcm947xx_sbh_lock;
49 extern char *cfe_env_get(char *nv_buf, const char *name);
52 #define sbh bcm947xx_sbh
53 #define sbh_lock bcm947xx_sbh_lock
55 /* Probe for NVRAM header */
57 early_nvram_init(void)
59 struct nvram_header *header;
61 struct sflash *info = NULL;
63 uint32 base, off, lim;
66 if ((cc = sb_setcore(sbh, SB_CC, 0)) != NULL) {
67 base = KSEG1ADDR(SB_FLASH2);
68 switch (readl(&cc->capabilities) & CC_CAP_FLASH_MASK) {
75 if ((info = sflash_init(sbh,cc)) == NULL)
85 /* extif assumed, Stop at 4 MB */
86 base = KSEG1ADDR(SB_FLASH1);
90 /* XXX: hack for supporting the CFE environment stuff on WGT634U */
91 src = (u32 *) KSEG1ADDR(base + 8 * 1024 * 1024 - 0x2000);
92 dst = (u32 *) nvram_buf;
93 if ((lim == 0x02000000) && ((*src & 0xff00ff) == 0x000001)) {
94 printk("early_nvram_init: WGT634U NVRAM found.\n");
96 for (i = 0; i < 0x1ff0; i++) {
97 if (*src == 0xFFFFFFFF)
107 /* Windowed flash access */
108 header = (struct nvram_header *) KSEG1ADDR(base + off - NVRAM_SPACE);
109 if (header->magic == NVRAM_MAGIC)
114 /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
115 header = (struct nvram_header *) KSEG1ADDR(base + 4 * 1024);
116 if (header->magic == NVRAM_MAGIC)
119 header = (struct nvram_header *) KSEG1ADDR(base + 1 * 1024);
120 if (header->magic == NVRAM_MAGIC)
123 printk("early_nvram_init: NVRAM not found\n");
127 src = (u32 *) header;
128 dst = (u32 *) nvram_buf;
129 for (i = 0; i < sizeof(struct nvram_header); i += 4)
131 for (; i < header->len && i < NVRAM_SPACE; i += 4)
132 *dst++ = ltoh32(*src++);
135 /* Early (before mm or mtd) read-only access to NVRAM */
137 early_nvram_get(const char *name)
139 char *var, *value, *end, *eq;
152 return cfe_env_get(nvram_buf, name);
154 /* Look for name=value and return value */
155 var = &nvram_buf[sizeof(struct nvram_header)];
156 end = nvram_buf + sizeof(nvram_buf) - 2;
157 end[0] = end[1] = '\0';
158 for (; *var; var = value + strlen(value) + 1) {
159 if (!(eq = strchr(var, '=')))
162 if ((eq - var) == strlen(name) && strncmp(var, name, (eq - var)) == 0)
170 early_nvram_getall(char *buf, int count)
184 /* Write name=value\0 ... \0\0 */
185 var = &nvram_buf[sizeof(struct nvram_header)];
186 end = nvram_buf + sizeof(nvram_buf) - 2;
187 end[0] = end[1] = '\0';
188 for (; *var; var += strlen(var) + 1) {
189 if ((count - len) <= (strlen(var) + 1))
191 len += sprintf(buf + len, "%s", var) + 1;
199 nvram_get(const char *name)
201 return early_nvram_get(name);
205 nvram_getall(char *buf, int count)
210 return early_nvram_getall(buf, count);
214 * Search the name=value vars for a specific one and return its value.
215 * Returns NULL if not found.
218 getvar(char *vars, const char *name)
225 /* first look in vars[] */
226 for (s = vars; s && *s;) {
228 if ((memcmp(s, name, len) == 0) && (s[len] == '='))
234 /* then query nvram */
235 return (nvram_get(name));
239 * Search the vars for a specific one and return its value as
240 * an integer. Returns 0 if not found.
243 getintvar(char *vars, const char *name)
247 if ((val = getvar(vars, name)) == NULL)
250 return (simple_strtoul(val, NULL, 0));