Ajoute le support easygate
[openwrt-10.03/.git] / target / linux / easygate-2.6 / files / arch / mips / bcm963xx / 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/kernel.h>
16 #include <linux/string.h>
17 #include <linux/interrupt.h>
18 #include <linux/spinlock.h>
19 #include <linux/slab.h>
20 #include <asm/byteorder.h>
21 #include <asm/bootinfo.h>
22 #include <asm/addrspace.h>
23 #include <asm/io.h>
24 #include <asm/uaccess.h>
25
26 #include <nvram.h>
27
28 #define MB * 1048576
29
30 #define WINDOW_ADDR     0x1F000000
31 #define WINDOW_SIZE     0x800000
32
33 static char nvram_buf[NVRAM_SPACE];
34 static int cfe_env;
35 extern char *cfe_env_get(char *nv_buf, const char *name);
36                 
37 /* Probe for NVRAM header */
38 void __init early_nvram_init(void)
39 {
40         struct nvram_header *header;
41         int i;
42         u32 base, lim, off;
43         u32 *src, *dst;
44         
45         base = WINDOW_ADDR;
46         lim = 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 - 0x20000);
53                 dst = (u32 *) nvram_buf;
54
55                 /* We are looking for the String gOGoBrCm */
56                 if ((*src) == 0x674f476f && (*(src + 1)) == 0x4272436d) {
57                         printk("early_nvram_init: EasyGate NVRAM found.\n");
58
59                         for (i = 0; i < 0x1ff0; i++) {
60                                 if (*src == 0xFFFFFFFF)
61                                         break;
62                                 *dst++ = *src++;
63                         }
64                         cfe_env = 1;
65                         return;
66                 }
67         }
68
69         off = 0x20000;
70         while (off <= lim) {
71                 /* Windowed flash access */
72                 header = (struct nvram_header *) KSEG1ADDR(base + off - NVRAM_SPACE);
73                 if (header->magic == NVRAM_HEADER)
74                         goto found;
75                 off <<= 1;
76         }
77
78         /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
79         header = (struct nvram_header *) KSEG1ADDR(base + 4096);
80         if (header->magic == NVRAM_HEADER)
81                 goto found;
82         
83         header = (struct nvram_header *) KSEG1ADDR(base + 1024);
84         if (header->magic == NVRAM_HEADER)
85                 goto found;
86         
87         printk("Found no CFE environnment\n");
88         return; 
89
90 found:
91         printk("Found CFE environnment\n");
92         src = (u32 *) header;
93         dst = (u32 *) nvram_buf;
94         for (i = 0; i < sizeof(struct nvram_header); i += 4)
95                 *dst++ = *src++;
96         for (; i < header->len && i < NVRAM_SPACE; i += 4)
97                 //*dst++ = le32_to_cpu(*src++);
98                 *dst++ = be32_to_cpu(*src++);
99 }
100
101 char *nvram_get(const char *name)
102 {
103         char *var, *value, *end, *eq;
104
105         if (!name)
106                 return NULL;
107
108         if (!nvram_buf[0])
109                 early_nvram_init();
110
111         if (cfe_env)
112                 return cfe_env_get(nvram_buf, name);
113
114         /* Look for name=value and return value */
115         var = &nvram_buf[sizeof(struct nvram_header)];
116         end = nvram_buf + sizeof(nvram_buf) - 2;
117         end[0] = end[1] = '\0';
118         for (; *var; var = value + strlen(value) + 1) {
119                 if (!(eq = strchr(var, '=')))
120                         break;
121                 value = eq + 1;
122                 if ((eq - var) == strlen(name) && strncmp(var, name, (eq - var)) == 0)
123                         return value;
124         }
125
126         return NULL;
127 }
128
129 EXPORT_SYMBOL(nvram_get);