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