aboutsummaryrefslogtreecommitdiffstats
path: root/arch/mips/bcm47xx/nvram.c
diff options
context:
space:
mode:
Diffstat (limited to 'arch/mips/bcm47xx/nvram.c')
-rw-r--r--arch/mips/bcm47xx/nvram.c77
1 files changed, 36 insertions, 41 deletions
diff --git a/arch/mips/bcm47xx/nvram.c b/arch/mips/bcm47xx/nvram.c
index c5c381c43f17..ba632ff08a13 100644
--- a/arch/mips/bcm47xx/nvram.c
+++ b/arch/mips/bcm47xx/nvram.c
@@ -11,15 +11,18 @@
11 * option) any later version. 11 * option) any later version.
12 */ 12 */
13 13
14#include <linux/io.h>
14#include <linux/types.h> 15#include <linux/types.h>
15#include <linux/module.h> 16#include <linux/module.h>
16#include <linux/kernel.h> 17#include <linux/kernel.h>
17#include <linux/string.h> 18#include <linux/string.h>
18#include <linux/mtd/mtd.h> 19#include <linux/mtd/mtd.h>
19#include <bcm47xx_nvram.h> 20#include <linux/bcm47xx_nvram.h>
20 21
21#define NVRAM_MAGIC 0x48534C46 /* 'FLSH' */ 22#define NVRAM_MAGIC 0x48534C46 /* 'FLSH' */
22#define NVRAM_SPACE 0x8000 23#define NVRAM_SPACE 0x10000
24#define NVRAM_MAX_GPIO_ENTRIES 32
25#define NVRAM_MAX_GPIO_VALUE_LEN 30
23 26
24#define FLASH_MIN 0x00020000 /* Minimum flash size */ 27#define FLASH_MIN 0x00020000 /* Minimum flash size */
25 28
@@ -91,20 +94,18 @@ static int nvram_find_and_copy(void __iomem *iobase, u32 lim)
91 return -ENXIO; 94 return -ENXIO;
92 95
93found: 96found:
94
95 if (header->len > size) 97 if (header->len > size)
96 pr_err("The nvram size accoridng to the header seems to be bigger than the partition on flash\n"); 98 pr_err("The nvram size accoridng to the header seems to be bigger than the partition on flash\n");
97 if (header->len > NVRAM_SPACE) 99 if (header->len > NVRAM_SPACE)
98 pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n", 100 pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
99 header->len, NVRAM_SPACE); 101 header->len, NVRAM_SPACE);
100 102
101 src = (u32 *) header; 103 src = (u32 *)header;
102 dst = (u32 *) nvram_buf; 104 dst = (u32 *)nvram_buf;
103 for (i = 0; i < sizeof(struct nvram_header); i += 4) 105 for (i = 0; i < sizeof(struct nvram_header); i += 4)
104 *dst++ = *src++; 106 *dst++ = __raw_readl(src++);
105 for (; i < header->len && i < NVRAM_SPACE && i < size; i += 4) 107 for (; i < header->len && i < NVRAM_SPACE && i < size; i += 4)
106 *dst++ = le32_to_cpu(*src++); 108 *dst++ = readl(src++);
107 memset(dst, 0x0, NVRAM_SPACE - i);
108 109
109 return 0; 110 return 0;
110} 111}
@@ -138,37 +139,28 @@ static int nvram_init(void)
138 struct mtd_info *mtd; 139 struct mtd_info *mtd;
139 struct nvram_header header; 140 struct nvram_header header;
140 size_t bytes_read; 141 size_t bytes_read;
141 int err, i; 142 int err;
142 143
143 mtd = get_mtd_device_nm("nvram"); 144 mtd = get_mtd_device_nm("nvram");
144 if (IS_ERR(mtd)) 145 if (IS_ERR(mtd))
145 return -ENODEV; 146 return -ENODEV;
146 147
147 for (i = 0; i < ARRAY_SIZE(nvram_sizes); i++) { 148 err = mtd_read(mtd, 0, sizeof(header), &bytes_read, (uint8_t *)&header);
148 loff_t from = mtd->size - nvram_sizes[i]; 149 if (!err && header.magic == NVRAM_MAGIC) {
150 u8 *dst = (uint8_t *)nvram_buf;
151 size_t len = header.len;
149 152
150 if (from < 0) 153 if (header.len > NVRAM_SPACE) {
151 continue; 154 pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
152 155 header.len, NVRAM_SPACE);
153 err = mtd_read(mtd, from, sizeof(header), &bytes_read, 156 len = NVRAM_SPACE;
154 (uint8_t *)&header); 157 }
155 if (!err && header.magic == NVRAM_MAGIC) {
156 u8 *dst = (uint8_t *)nvram_buf;
157 size_t len = header.len;
158
159 if (header.len > NVRAM_SPACE) {
160 pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
161 header.len, NVRAM_SPACE);
162 len = NVRAM_SPACE;
163 }
164 158
165 err = mtd_read(mtd, from, len, &bytes_read, dst); 159 err = mtd_read(mtd, 0, len, &bytes_read, dst);
166 if (err) 160 if (err)
167 return err; 161 return err;
168 memset(dst + bytes_read, 0x0, NVRAM_SPACE - bytes_read);
169 162
170 return 0; 163 return 0;
171 }
172 } 164 }
173#endif 165#endif
174 166
@@ -178,7 +170,7 @@ static int nvram_init(void)
178int bcm47xx_nvram_getenv(const char *name, char *val, size_t val_len) 170int bcm47xx_nvram_getenv(const char *name, char *val, size_t val_len)
179{ 171{
180 char *var, *value, *end, *eq; 172 char *var, *value, *end, *eq;
181 int err; 173 int data_left, err;
182 174
183 if (!name) 175 if (!name)
184 return -EINVAL; 176 return -EINVAL;
@@ -192,16 +184,18 @@ int bcm47xx_nvram_getenv(const char *name, char *val, size_t val_len)
192 /* Look for name=value and return value */ 184 /* Look for name=value and return value */
193 var = &nvram_buf[sizeof(struct nvram_header)]; 185 var = &nvram_buf[sizeof(struct nvram_header)];
194 end = nvram_buf + sizeof(nvram_buf) - 2; 186 end = nvram_buf + sizeof(nvram_buf) - 2;
195 end[0] = end[1] = '\0'; 187 end[0] = '\0';
188 end[1] = '\0';
196 for (; *var; var = value + strlen(value) + 1) { 189 for (; *var; var = value + strlen(value) + 1) {
197 eq = strchr(var, '='); 190 data_left = end - var;
191
192 eq = strnchr(var, data_left, '=');
198 if (!eq) 193 if (!eq)
199 break; 194 break;
200 value = eq + 1; 195 value = eq + 1;
201 if ((eq - var) == strlen(name) && 196 if (eq - var == strlen(name) &&
202 strncmp(var, name, (eq - var)) == 0) { 197 strncmp(var, name, eq - var) == 0)
203 return snprintf(val, val_len, "%s", value); 198 return snprintf(val, val_len, "%s", value);
204 }
205 } 199 }
206 return -ENOENT; 200 return -ENOENT;
207} 201}
@@ -210,10 +204,11 @@ EXPORT_SYMBOL(bcm47xx_nvram_getenv);
210int bcm47xx_nvram_gpio_pin(const char *name) 204int bcm47xx_nvram_gpio_pin(const char *name)
211{ 205{
212 int i, err; 206 int i, err;
213 char nvram_var[10]; 207 char nvram_var[] = "gpioXX";
214 char buf[30]; 208 char buf[NVRAM_MAX_GPIO_VALUE_LEN];
215 209
216 for (i = 0; i < 32; i++) { 210 /* TODO: Optimize it to don't call getenv so many times */
211 for (i = 0; i < NVRAM_MAX_GPIO_ENTRIES; i++) {
217 err = snprintf(nvram_var, sizeof(nvram_var), "gpio%i", i); 212 err = snprintf(nvram_var, sizeof(nvram_var), "gpio%i", i);
218 if (err <= 0) 213 if (err <= 0)
219 continue; 214 continue;