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