forgot to commit the change for covalt, too
[openwrt.git] / target / linux / brcm-2.4 / files / arch / mips / bcm947xx / nvram.c
1 /*
2 * NVRAM variable manipulation (Linux kernel half)
3 *
4 * Copyright 2006, Broadcom Corporation
5 * All Rights Reserved.
6 *
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.
11 *
12 */
13
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>
24 #include <linux/fs.h>
25 #include <linux/miscdevice.h>
26 #include <linux/mtd/mtd.h>
27 #include <asm/addrspace.h>
28 #include <asm/io.h>
29 #include <asm/uaccess.h>
30
31 #include <typedefs.h>
32 #include <osl.h>
33 #include <bcmendian.h>
34 #include <bcmnvram.h>
35 #include <sbconfig.h>
36 #include <sbchipc.h>
37 #include <sbutils.h>
38 #include <hndmips.h>
39 #include <sflash.h>
40
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)));
43
44 /* Global SB handle */
45 extern void *bcm947xx_sbh;
46 extern spinlock_t bcm947xx_sbh_lock;
47
48 static int cfe_env;
49 extern char *cfe_env_get(char *nv_buf, const char *name);
50
51 /* Convenience */
52 #define sbh bcm947xx_sbh
53 #define sbh_lock bcm947xx_sbh_lock
54
55 /* Probe for NVRAM header */
56 static void __init
57 early_nvram_init(void)
58 {
59 struct nvram_header *header;
60 chipcregs_t *cc;
61 struct sflash *info = NULL;
62 int i;
63 uint32 base, off, lim;
64 u32 *src, *dst;
65
66 if ((cc = sb_setcore(sbh, SB_CC, 0)) != NULL) {
67 base = KSEG1ADDR(SB_FLASH2);
68 switch (readl(&cc->capabilities) & CC_CAP_FLASH_MASK) {
69 case PFLASH:
70 lim = SB_FLASH2_SZ;
71 break;
72
73 case SFLASH_ST:
74 case SFLASH_AT:
75 if ((info = sflash_init(sbh,cc)) == NULL)
76 return;
77 lim = info->size;
78 break;
79
80 case FLASH_NONE:
81 default:
82 return;
83 }
84 } else {
85 /* extif assumed, Stop at 4 MB */
86 base = KSEG1ADDR(SB_FLASH1);
87 lim = SB_FLASH1_SZ;
88 }
89
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");
95
96 for (i = 0; i < 0x1ff0; i++) {
97 if (*src == 0xFFFFFFFF)
98 break;
99 *dst++ = *src++;
100 }
101 cfe_env = 1;
102 return;
103 }
104
105 off = FLASH_MIN;
106 while (off <= lim) {
107 /* Windowed flash access */
108 header = (struct nvram_header *) KSEG1ADDR(base + off - NVRAM_SPACE);
109 if (header->magic == NVRAM_MAGIC)
110 goto found;
111 off <<= 1;
112 }
113
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)
117 goto found;
118
119 header = (struct nvram_header *) KSEG1ADDR(base + 1 * 1024);
120 if (header->magic == NVRAM_MAGIC)
121 goto found;
122
123 printk("early_nvram_init: NVRAM not found\n");
124 return;
125
126 found:
127 src = (u32 *) header;
128 dst = (u32 *) nvram_buf;
129 for (i = 0; i < sizeof(struct nvram_header); i += 4)
130 *dst++ = *src++;
131 for (; i < header->len && i < NVRAM_SPACE; i += 4)
132 *dst++ = ltoh32(*src++);
133 }
134
135 /* Early (before mm or mtd) read-only access to NVRAM */
136 static char * __init
137 early_nvram_get(const char *name)
138 {
139 char *var, *value, *end, *eq;
140
141 if (!name)
142 return NULL;
143
144 /* Too early? */
145 if (sbh == NULL)
146 return NULL;
147
148 if (!nvram_buf[0])
149 early_nvram_init();
150
151 if (cfe_env)
152 return cfe_env_get(nvram_buf, name);
153
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, '=')))
160 break;
161 value = eq + 1;
162 if ((eq - var) == strlen(name) && strncmp(var, name, (eq - var)) == 0)
163 return value;
164 }
165
166 return NULL;
167 }
168
169 static int __init
170 early_nvram_getall(char *buf, int count)
171 {
172 char *var, *end;
173 int len = 0;
174
175 /* Too early? */
176 if (sbh == NULL)
177 return -1;
178
179 if (!nvram_buf[0])
180 early_nvram_init();
181
182 bzero(buf, count);
183
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))
190 break;
191 len += sprintf(buf + len, "%s", var) + 1;
192 }
193
194 return 0;
195 }
196
197
198 char *
199 nvram_get(const char *name)
200 {
201 return early_nvram_get(name);
202 }
203
204 int
205 nvram_getall(char *buf, int count)
206 {
207 unsigned long flags;
208 int ret;
209
210 return early_nvram_getall(buf, count);
211 }
212
213 /*
214 * Search the name=value vars for a specific one and return its value.
215 * Returns NULL if not found.
216 */
217 char*
218 getvar(char *vars, const char *name)
219 {
220 char *s;
221 int len;
222
223 len = strlen(name);
224
225 /* first look in vars[] */
226 for (s = vars; s && *s;) {
227 /* CSTYLED */
228 if ((memcmp(s, name, len) == 0) && (s[len] == '='))
229 return (&s[len+1]);
230
231 while (*s++);
232 }
233
234 /* then query nvram */
235 return (nvram_get(name));
236 }
237
238 /*
239 * Search the vars for a specific one and return its value as
240 * an integer. Returns 0 if not found.
241 */
242 int
243 getintvar(char *vars, const char *name)
244 {
245 char *val;
246
247 if ((val = getvar(vars, name)) == NULL)
248 return (0);
249
250 return (simple_strtoul(val, NULL, 0));
251 }
252
This page took 0.054843 seconds and 5 git commands to generate.