brcm47xx: Fix boot problem with wgt634u.
[openwrt/svn-archive/archive.git] / target / linux / brcm47xx / patches-2.6.34 / 820-wgt634u-nvram-fix.patch
1 The Netgear wgt634u uses a different format for storing the
2 configuration. This patch is needed to read out the correct
3 configuration. The cfe_env.c file uses a different method way to read
4 out the configuration than the in kernel cfe config reader.
5
6 --- a/arch/mips/bcm47xx/Makefile
7 +++ b/arch/mips/bcm47xx/Makefile
8 @@ -3,4 +3,4 @@
9 # under Linux.
10 #
11
12 -obj-y := gpio.o irq.o nvram.o prom.o serial.o setup.o time.o
13 +obj-y := gpio.o irq.o nvram.o prom.o serial.o setup.o time.o cfe_env.o
14 --- /dev/null
15 +++ b/arch/mips/bcm47xx/cfe_env.c
16 @@ -0,0 +1,229 @@
17 +/*
18 + * CFE environment variable access
19 + *
20 + * Copyright 2001-2003, Broadcom Corporation
21 + * Copyright 2006, Felix Fietkau <nbd@openwrt.org>
22 + *
23 + * This program is free software; you can redistribute it and/or modify it
24 + * under the terms of the GNU General Public License as published by the
25 + * Free Software Foundation; either version 2 of the License, or (at your
26 + * option) any later version.
27 + */
28 +
29 +#include <linux/init.h>
30 +#include <linux/module.h>
31 +#include <linux/kernel.h>
32 +#include <linux/string.h>
33 +#include <asm/io.h>
34 +#include <asm/uaccess.h>
35 +
36 +#define NVRAM_SIZE (0x1ff0)
37 +static char _nvdata[NVRAM_SIZE];
38 +static char _valuestr[256];
39 +
40 +/*
41 + * TLV types. These codes are used in the "type-length-value"
42 + * encoding of the items stored in the NVRAM device (flash or EEPROM)
43 + *
44 + * The layout of the flash/nvram is as follows:
45 + *
46 + * <type> <length> <data ...> <type> <length> <data ...> <type_end>
47 + *
48 + * The type code of "ENV_TLV_TYPE_END" marks the end of the list.
49 + * The "length" field marks the length of the data section, not
50 + * including the type and length fields.
51 + *
52 + * Environment variables are stored as follows:
53 + *
54 + * <type_env> <length> <flags> <name> = <value>
55 + *
56 + * If bit 0 (low bit) is set, the length is an 8-bit value.
57 + * If bit 0 (low bit) is clear, the length is a 16-bit value
58 + *
59 + * Bit 7 set indicates "user" TLVs. In this case, bit 0 still
60 + * indicates the size of the length field.
61 + *
62 + * Flags are from the constants below:
63 + *
64 + */
65 +#define ENV_LENGTH_16BITS 0x00 /* for low bit */
66 +#define ENV_LENGTH_8BITS 0x01
67 +
68 +#define ENV_TYPE_USER 0x80
69 +
70 +#define ENV_CODE_SYS(n,l) (((n)<<1)|(l))
71 +#define ENV_CODE_USER(n,l) ((((n)<<1)|(l)) | ENV_TYPE_USER)
72 +
73 +/*
74 + * The actual TLV types we support
75 + */
76 +
77 +#define ENV_TLV_TYPE_END 0x00
78 +#define ENV_TLV_TYPE_ENV ENV_CODE_SYS(0,ENV_LENGTH_8BITS)
79 +
80 +/*
81 + * Environment variable flags
82 + */
83 +
84 +#define ENV_FLG_NORMAL 0x00 /* normal read/write */
85 +#define ENV_FLG_BUILTIN 0x01 /* builtin - not stored in flash */
86 +#define ENV_FLG_READONLY 0x02 /* read-only - cannot be changed */
87 +
88 +#define ENV_FLG_MASK 0xFF /* mask of attributes we keep */
89 +#define ENV_FLG_ADMIN 0x100 /* lets us internally override permissions */
90 +
91 +
92 +/* *********************************************************************
93 + * _nvram_read(buffer,offset,length)
94 + *
95 + * Read data from the NVRAM device
96 + *
97 + * Input parameters:
98 + * buffer - destination buffer
99 + * offset - offset of data to read
100 + * length - number of bytes to read
101 + *
102 + * Return value:
103 + * number of bytes read, or <0 if error occured
104 + ********************************************************************* */
105 +static int
106 +_nvram_read(unsigned char *nv_buf, unsigned char *buffer, int offset, int length)
107 +{
108 + int i;
109 + if (offset > NVRAM_SIZE)
110 + return -1;
111 +
112 + for ( i = 0; i < length; i++) {
113 + buffer[i] = ((volatile unsigned char*)nv_buf)[offset + i];
114 + }
115 + return length;
116 +}
117 +
118 +
119 +static char*
120 +_strnchr(const char *dest,int c,size_t cnt)
121 +{
122 + while (*dest && (cnt > 0)) {
123 + if (*dest == c) return (char *) dest;
124 + dest++;
125 + cnt--;
126 + }
127 + return NULL;
128 +}
129 +
130 +
131 +
132 +/*
133 + * Core support API: Externally visible.
134 + */
135 +
136 +/*
137 + * Get the value of an NVRAM variable
138 + * @param name name of variable to get
139 + * @return value of variable or NULL if undefined
140 + */
141 +
142 +char*
143 +cfe_env_get(unsigned char *nv_buf, char* name)
144 +{
145 + int size;
146 + unsigned char *buffer;
147 + unsigned char *ptr;
148 + unsigned char *envval;
149 + unsigned int reclen;
150 + unsigned int rectype;
151 + int offset;
152 + int flg;
153 +
154 + if (!strcmp(name, "nvram_type"))
155 + return "cfe";
156 +
157 + size = NVRAM_SIZE;
158 + buffer = &_nvdata[0];
159 +
160 + ptr = buffer;
161 + offset = 0;
162 +
163 + /* Read the record type and length */
164 + if (_nvram_read(nv_buf, ptr,offset,1) != 1) {
165 + goto error;
166 + }
167 +
168 + while ((*ptr != ENV_TLV_TYPE_END) && (size > 1)) {
169 +
170 + /* Adjust pointer for TLV type */
171 + rectype = *(ptr);
172 + offset++;
173 + size--;
174 +
175 + /*
176 + * Read the length. It can be either 1 or 2 bytes
177 + * depending on the code
178 + */
179 + if (rectype & ENV_LENGTH_8BITS) {
180 + /* Read the record type and length - 8 bits */
181 + if (_nvram_read(nv_buf, ptr,offset,1) != 1) {
182 + goto error;
183 + }
184 + reclen = *(ptr);
185 + size--;
186 + offset++;
187 + }
188 + else {
189 + /* Read the record type and length - 16 bits, MSB first */
190 + if (_nvram_read(nv_buf, ptr,offset,2) != 2) {
191 + goto error;
192 + }
193 + reclen = (((unsigned int) *(ptr)) << 8) + (unsigned int) *(ptr+1);
194 + size -= 2;
195 + offset += 2;
196 + }
197 +
198 + if (reclen > size)
199 + break; /* should not happen, bad NVRAM */
200 +
201 + switch (rectype) {
202 + case ENV_TLV_TYPE_ENV:
203 + /* Read the TLV data */
204 + if (_nvram_read(nv_buf, ptr,offset,reclen) != reclen)
205 + goto error;
206 + flg = *ptr++;
207 + envval = (unsigned char *) _strnchr(ptr,'=',(reclen-1));
208 + if (envval) {
209 + *envval++ = '\0';
210 + memcpy(_valuestr,envval,(reclen-1)-(envval-ptr));
211 + _valuestr[(reclen-1)-(envval-ptr)] = '\0';
212 +#if 0
213 + printk(KERN_INFO "NVRAM:%s=%s\n", ptr, _valuestr);
214 +#endif
215 + if(!strcmp(ptr, name)){
216 + return _valuestr;
217 + }
218 + if((strlen(ptr) > 1) && !strcmp(&ptr[1], name))
219 + return _valuestr;
220 + }
221 + break;
222 +
223 + default:
224 + /* Unknown TLV type, skip it. */
225 + break;
226 + }
227 +
228 + /*
229 + * Advance to next TLV
230 + */
231 +
232 + size -= (int)reclen;
233 + offset += reclen;
234 +
235 + /* Read the next record type */
236 + ptr = buffer;
237 + if (_nvram_read(nv_buf, ptr,offset,1) != 1)
238 + goto error;
239 + }
240 +
241 +error:
242 + return NULL;
243 +
244 +}
245 +
246 --- a/arch/mips/bcm47xx/nvram.c
247 +++ b/arch/mips/bcm47xx/nvram.c
248 @@ -21,6 +21,8 @@
249 #include <asm/mach-bcm47xx/bcm47xx.h>
250
251 static char nvram_buf[NVRAM_SPACE];
252 +static int cfe_env;
253 +extern char *cfe_env_get(char *nv_buf, const char *name);
254
255 /* Probe for NVRAM header */
256 static void __init early_nvram_init(void)
257 @@ -33,6 +35,25 @@ static void __init early_nvram_init(void
258
259 base = mcore->flash_window;
260 lim = mcore->flash_window_size;
261 + cfe_env = 0;
262 +
263 + /* XXX: hack for supporting the CFE environment stuff on WGT634U */
264 + if (lim >= 8 * 1024 * 1024) {
265 + src = (u32 *) KSEG1ADDR(base + 8 * 1024 * 1024 - 0x2000);
266 + dst = (u32 *) nvram_buf;
267 +
268 + if ((*src & 0xff00ff) == 0x000001) {
269 + printk("early_nvram_init: WGT634U NVRAM found.\n");
270 +
271 + for (i = 0; i < 0x1ff0; i++) {
272 + if (*src == 0xFFFFFFFF)
273 + break;
274 + *dst++ = *src++;
275 + }
276 + cfe_env = 1;
277 + return;
278 + }
279 + }
280
281 off = FLASH_MIN;
282 while (off <= lim) {
283 @@ -74,6 +95,12 @@ int nvram_getenv(char *name, char *val,
284 if (!nvram_buf[0])
285 early_nvram_init();
286
287 + if (cfe_env) {
288 + value = cfe_env_get(nvram_buf, name);
289 + snprintf(val, val_len, "%s", value);
290 + return 0;
291 + }
292 +
293 /* Look for name=value and return value */
294 var = &nvram_buf[sizeof(struct nvram_header)];
295 end = nvram_buf + sizeof(nvram_buf) - 2;
296 @@ -103,6 +130,9 @@ char *nvram_get(const char *name)
297 if (!nvram_buf[0])
298 early_nvram_init();
299
300 + if (cfe_env)
301 + return cfe_env_get(nvram_buf, name);
302 +
303 /* Look for name=value and return value */
304 var = &nvram_buf[sizeof(struct nvram_header)];
305 end = nvram_buf + sizeof(nvram_buf) - 2;