3f32ad9d6e09d058b46dae47892b432f891d1059
[openwrt/svn-archive/archive.git] / target / linux / brcm47xx / files / arch / mips / bcm47xx / nvram.c
1 /*
2 * BCM947xx nvram variable access
3 *
4 * Copyright 2005, Broadcom Corporation
5 * Copyright 2006, Felix Fietkau <nbd@openwrt.org>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 */
12
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/ssb/ssb.h>
16 #include <linux/kernel.h>
17 #include <linux/string.h>
18 #include <linux/interrupt.h>
19 #include <linux/spinlock.h>
20 #include <linux/slab.h>
21 #include <asm/byteorder.h>
22 #include <asm/bootinfo.h>
23 #include <asm/addrspace.h>
24 #include <asm/io.h>
25 #include <asm/uaccess.h>
26
27 #include <nvram.h>
28
29 #define MB * 1048576
30 extern struct ssb_bus ssb;
31
32 static char nvram_buf[NVRAM_SPACE];
33 static int cfe_env;
34 extern char *cfe_env_get(char *nv_buf, const char *name);
35
36 /* Probe for NVRAM header */
37 static void __init early_nvram_init(void)
38 {
39 struct ssb_mipscore *mcore = &ssb.mipscore;
40 struct nvram_header *header;
41 int i;
42 u32 base, lim, off;
43 u32 *src, *dst;
44
45 base = mcore->flash_window;
46 lim = mcore->flash_window_size;
47 cfe_env = 0;
48
49
50 /* XXX: hack for supporting the CFE environment stuff on WGT634U */
51 if (lim >= 8 MB) {
52 src = (u32 *) KSEG1ADDR(base + 8 MB - 0x2000);
53 dst = (u32 *) nvram_buf;
54
55 if ((*src & 0xff00ff) == 0x000001) {
56 printk("early_nvram_init: WGT634U NVRAM found.\n");
57
58 for (i = 0; i < 0x1ff0; i++) {
59 if (*src == 0xFFFFFFFF)
60 break;
61 *dst++ = *src++;
62 }
63 cfe_env = 1;
64 return;
65 }
66 }
67
68 off = 0x20000;
69 while (off <= lim) {
70 /* Windowed flash access */
71 header = (struct nvram_header *) KSEG1ADDR(base + off - NVRAM_SPACE);
72 if (header->magic == NVRAM_HEADER)
73 goto found;
74 off <<= 1;
75 }
76
77 /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
78 header = (struct nvram_header *) KSEG1ADDR(base + 4096);
79 if (header->magic == NVRAM_HEADER)
80 goto found;
81
82 header = (struct nvram_header *) KSEG1ADDR(base + 1024);
83 if (header->magic == NVRAM_HEADER)
84 goto found;
85
86 return;
87
88 found:
89 src = (u32 *) header;
90 dst = (u32 *) nvram_buf;
91 for (i = 0; i < sizeof(struct nvram_header); i += 4)
92 *dst++ = *src++;
93 for (; i < header->len && i < NVRAM_SPACE; i += 4)
94 *dst++ = le32_to_cpu(*src++);
95 }
96
97 char *nvram_get(const char *name)
98 {
99 char *var, *value, *end, *eq;
100
101 if (!name)
102 return NULL;
103
104 if (!nvram_buf[0])
105 early_nvram_init();
106
107 if (cfe_env)
108 return cfe_env_get(nvram_buf, name);
109
110 /* Look for name=value and return value */
111 var = &nvram_buf[sizeof(struct nvram_header)];
112 end = nvram_buf + sizeof(nvram_buf) - 2;
113 end[0] = end[1] = '\0';
114 for (; *var; var = value + strlen(value) + 1) {
115 if (!(eq = strchr(var, '=')))
116 break;
117 value = eq + 1;
118 if ((eq - var) == strlen(name) && strncmp(var, name, (eq - var)) == 0)
119 return value;
120 }
121
122 return NULL;
123 }
124
125 EXPORT_SYMBOL(nvram_get);