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