bcm53xx: backport NVRAM driver sent upstream
[openwrt/staging/dedeckeh.git] / target / linux / bcm53xx / files / drivers / firmware / broadcom / bcm47xx_nvram.c
1 /*
2 * BCM947xx nvram variable access
3 *
4 * Copyright (C) 2005 Broadcom Corporation
5 * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
6 * Copyright (C) 2010-2012 Hauke Mehrtens <hauke@hauke-m.de>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 */
13
14 #include <linux/types.h>
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/string.h>
18 #include <linux/mtd/mtd.h>
19 #include <linux/bcm47xx_nvram.h>
20
21 #define NVRAM_MAGIC 0x48534C46 /* 'FLSH' */
22 #define NVRAM_SPACE 0x8000
23 #define NVRAM_MAX_GPIO_ENTRIES 32
24 #define NVRAM_MAX_GPIO_VALUE_LEN 30
25
26 #define FLASH_MIN 0x00020000 /* Minimum flash size */
27
28 struct nvram_header {
29 u32 magic;
30 u32 len;
31 u32 crc_ver_init; /* 0:7 crc, 8:15 ver, 16:31 sdram_init */
32 u32 config_refresh; /* 0:15 sdram_config, 16:31 sdram_refresh */
33 u32 config_ncdl; /* ncdl values for memc */
34 };
35
36 static char nvram_buf[NVRAM_SPACE];
37 static const u32 nvram_sizes[] = {0x8000, 0xF000, 0x10000};
38
39 static u32 find_nvram_size(void __iomem *end)
40 {
41 struct nvram_header __iomem *header;
42 int i;
43
44 for (i = 0; i < ARRAY_SIZE(nvram_sizes); i++) {
45 header = (struct nvram_header *)(end - nvram_sizes[i]);
46 if (header->magic == NVRAM_MAGIC)
47 return nvram_sizes[i];
48 }
49
50 return 0;
51 }
52
53 /* Probe for NVRAM header */
54 static int nvram_find_and_copy(void __iomem *iobase, u32 lim)
55 {
56 struct nvram_header __iomem *header;
57 int i;
58 u32 off;
59 u32 *src, *dst;
60 u32 size;
61
62 if (nvram_buf[0]) {
63 pr_warn("nvram already initialized\n");
64 return -EEXIST;
65 }
66
67 /* TODO: when nvram is on nand flash check for bad blocks first. */
68 off = FLASH_MIN;
69 while (off <= lim) {
70 /* Windowed flash access */
71 size = find_nvram_size(iobase + off);
72 if (size) {
73 header = (struct nvram_header *)(iobase + off - size);
74 goto found;
75 }
76 off <<= 1;
77 }
78
79 /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
80 header = (struct nvram_header *)(iobase + 4096);
81 if (header->magic == NVRAM_MAGIC) {
82 size = NVRAM_SPACE;
83 goto found;
84 }
85
86 header = (struct nvram_header *)(iobase + 1024);
87 if (header->magic == NVRAM_MAGIC) {
88 size = NVRAM_SPACE;
89 goto found;
90 }
91
92 pr_err("no nvram found\n");
93 return -ENXIO;
94
95 found:
96 if (header->len > size)
97 pr_err("The nvram size accoridng to the header seems to be bigger than the partition on flash\n");
98 if (header->len > NVRAM_SPACE)
99 pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
100 header->len, NVRAM_SPACE);
101
102 src = (u32 *)header;
103 dst = (u32 *)nvram_buf;
104 for (i = 0; i < sizeof(struct nvram_header); i += 4)
105 *dst++ = __raw_readl(src++);
106 for (; i < header->len && i < NVRAM_SPACE && i < size; i += 4)
107 *dst++ = readl(src++);
108
109 return 0;
110 }
111
112 /*
113 * On bcm47xx we need access to the NVRAM very early, so we can't use mtd
114 * subsystem to access flash. We can't even use platform device / driver to
115 * store memory offset.
116 * To handle this we provide following symbol. It's supposed to be called as
117 * soon as we get info about flash device, before any NVRAM entry is needed.
118 */
119 int bcm47xx_nvram_init_from_mem(u32 base, u32 lim)
120 {
121 void __iomem *iobase;
122 int err;
123
124 iobase = ioremap_nocache(base, lim);
125 if (!iobase)
126 return -ENOMEM;
127
128 err = nvram_find_and_copy(iobase, lim);
129
130 iounmap(iobase);
131
132 return err;
133 }
134
135 static int nvram_init(void)
136 {
137 #ifdef CONFIG_MTD
138 struct mtd_info *mtd;
139 struct nvram_header header;
140 size_t bytes_read;
141 int err, i;
142
143 mtd = get_mtd_device_nm("nvram");
144 if (IS_ERR(mtd))
145 return -ENODEV;
146
147 for (i = 0; i < ARRAY_SIZE(nvram_sizes); i++) {
148 loff_t from = mtd->size - nvram_sizes[i];
149
150 if (from < 0)
151 continue;
152
153 err = mtd_read(mtd, from, sizeof(header), &bytes_read,
154 (uint8_t *)&header);
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
165 err = mtd_read(mtd, from, len, &bytes_read, dst);
166 if (err)
167 return err;
168
169 return 0;
170 }
171 }
172 #endif
173
174 return -ENXIO;
175 }
176
177 int bcm47xx_nvram_getenv(const char *name, char *val, size_t val_len)
178 {
179 char *var, *value, *end, *eq;
180 int data_left, err;
181
182 if (!name)
183 return -EINVAL;
184
185 if (!nvram_buf[0]) {
186 err = nvram_init();
187 if (err)
188 return err;
189 }
190
191 /* Look for name=value and return value */
192 var = &nvram_buf[sizeof(struct nvram_header)];
193 end = nvram_buf + sizeof(nvram_buf) - 2;
194 end[0] = '\0';
195 end[1] = '\0';
196 for (; *var; var = value + strlen(value) + 1) {
197 data_left = end - var;
198
199 eq = strnchr(var, data_left, '=');
200 if (!eq)
201 break;
202 value = eq + 1;
203 if (eq - var == strlen(name) &&
204 strncmp(var, name, eq - var) == 0)
205 return snprintf(val, val_len, "%s", value);
206 }
207 return -ENOENT;
208 }
209 EXPORT_SYMBOL(bcm47xx_nvram_getenv);
210
211 int bcm47xx_nvram_gpio_pin(const char *name)
212 {
213 int i, err;
214 char nvram_var[] = "gpioXX";
215 char buf[NVRAM_MAX_GPIO_VALUE_LEN];
216
217 /* TODO: Optimize it to don't call getenv so many times */
218 for (i = 0; i < NVRAM_MAX_GPIO_ENTRIES; i++) {
219 err = snprintf(nvram_var, sizeof(nvram_var), "gpio%i", i);
220 if (err <= 0)
221 continue;
222 err = bcm47xx_nvram_getenv(nvram_var, buf, sizeof(buf));
223 if (err <= 0)
224 continue;
225 if (!strcmp(name, buf))
226 return i;
227 }
228 return -ENOENT;
229 }
230 EXPORT_SYMBOL(bcm47xx_nvram_gpio_pin);
231
232 MODULE_LICENSE("GPLv2");