38925cf346784cabeec66910e0a0d113d65103f8
[openwrt.git] / target / linux / bcm53xx / patches-3.18 / 111-bcm47xx-nvram-add-new-nvram-driver-with-dt-support.patch
1 From 71a6bff8656a1713615ffdd9139a83d65ba46c6d Mon Sep 17 00:00:00 2001
2 From: Hauke Mehrtens <hauke@hauke-m.de>
3 Date: Sat, 3 May 2014 22:54:59 +0200
4 Subject: [PATCH 02/17] bcm47xx-nvram: add new broadcom nvram driver with dt
5  support
6
7 This adds a new driver which searches at a given memory range for a
8 nvram like it is used on the bcm47xx and bcm53xx SoCs with ARM and MIPS
9 CPUs. This driver provides acces to this nvram to other device in the
10 device tree. You have to specify the memory ranges where the content of
11 the flash chip is memory mapped and this driver will search there for
12 some nvram and parse it. Other drivers can use this driver to access the
13 device nvram. The nvram is used to store board configurations like the
14 mac addresses, the switch configuration and the calibration data for
15 the wifi devices.
16
17 This was copied from arch/mips/bcm47xx/nvram.c and modified to interact
18 with device tree. My plan is to make the MIPS bcm47xx also use this new
19 driver some time later.
20
21 Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
22 ---
23  .../devicetree/bindings/misc/bcm47xx-nvram.txt     |  19 ++
24  arch/mips/bcm47xx/board.c                          |  40 ++--
25  arch/mips/bcm47xx/nvram.c                          |   7 +-
26  arch/mips/bcm47xx/setup.c                          |   4 +-
27  arch/mips/bcm47xx/sprom.c                          |   4 +-
28  arch/mips/bcm47xx/time.c                           |   2 +-
29  drivers/misc/Kconfig                               |   5 +
30  drivers/misc/Makefile                              |   1 +
31  drivers/misc/bcm47xx-nvram.c                       | 215 +++++++++++++++++++++
32  drivers/net/ethernet/broadcom/b44.c                |   2 +-
33  drivers/net/ethernet/broadcom/bgmac.c              |   5 +-
34  drivers/ssb/driver_chipcommon_pmu.c                |   3 +-
35  include/linux/bcm47xx_nvram.h                      |  17 +-
36  13 files changed, 286 insertions(+), 38 deletions(-)
37  create mode 100644 Documentation/devicetree/bindings/misc/bcm47xx-nvram.txt
38  create mode 100644 drivers/misc/bcm47xx-nvram.c
39
40 --- /dev/null
41 +++ b/Documentation/devicetree/bindings/misc/bcm47xx-nvram.txt
42 @@ -0,0 +1,19 @@
43 +Broadcom bcm47xx/bcm53xx nvram access driver
44 +
45 +This driver provides access to the nvram for other drivers.
46 +
47 +Required properties:
48 +
49 +- compatible : brcm,bcm47xx-nvram
50 +
51 +- reg : iomem address range
52 +
53 +On NorthStar ARM SoCs the NAND flash is available at 0x1c000000 and the
54 +NOR flash is at 0x1e000000
55 +
56 +Example:
57 +
58 +nvram0: nvram@0 {
59 +       compatible = "brcm,bcm47xx-nvram";
60 +       reg = <0x1c000000 0x01000000>;
61 +};
62 --- a/arch/mips/bcm47xx/board.c
63 +++ b/arch/mips/bcm47xx/board.c
64 @@ -218,36 +218,36 @@ static __init const struct bcm47xx_board
65         const struct bcm47xx_board_type_list2 *e2;
66         const struct bcm47xx_board_type_list3 *e3;
67  
68 -       if (bcm47xx_nvram_getenv("model_name", buf1, sizeof(buf1)) >= 0) {
69 +       if (bcm47xx_nvram_getenv(NULL, "model_name", buf1, sizeof(buf1)) >= 0) {
70                 for (e1 = bcm47xx_board_list_model_name; e1->value1; e1++) {
71                         if (!strcmp(buf1, e1->value1))
72                                 return &e1->board;
73                 }
74         }
75  
76 -       if (bcm47xx_nvram_getenv("model_no", buf1, sizeof(buf1)) >= 0) {
77 +       if (bcm47xx_nvram_getenv(NULL, "model_no", buf1, sizeof(buf1)) >= 0) {
78                 for (e1 = bcm47xx_board_list_model_no; e1->value1; e1++) {
79                         if (strstarts(buf1, e1->value1))
80                                 return &e1->board;
81                 }
82         }
83  
84 -       if (bcm47xx_nvram_getenv("machine_name", buf1, sizeof(buf1)) >= 0) {
85 +       if (bcm47xx_nvram_getenv(NULL, "machine_name", buf1, sizeof(buf1)) >= 0) {
86                 for (e1 = bcm47xx_board_list_machine_name; e1->value1; e1++) {
87                         if (strstarts(buf1, e1->value1))
88                                 return &e1->board;
89                 }
90         }
91  
92 -       if (bcm47xx_nvram_getenv("hardware_version", buf1, sizeof(buf1)) >= 0) {
93 +       if (bcm47xx_nvram_getenv(NULL, "hardware_version", buf1, sizeof(buf1)) >= 0) {
94                 for (e1 = bcm47xx_board_list_hardware_version; e1->value1; e1++) {
95                         if (strstarts(buf1, e1->value1))
96                                 return &e1->board;
97                 }
98         }
99  
100 -       if (bcm47xx_nvram_getenv("hardware_version", buf1, sizeof(buf1)) >= 0 &&
101 -           bcm47xx_nvram_getenv("boardtype", buf2, sizeof(buf2)) >= 0) {
102 +       if (bcm47xx_nvram_getenv(NULL, "hardware_version", buf1, sizeof(buf1)) >= 0 &&
103 +           bcm47xx_nvram_getenv(NULL, "boardtype", buf2, sizeof(buf2)) >= 0) {
104                 for (e2 = bcm47xx_board_list_boot_hw; e2->value1; e2++) {
105                         if (!strstarts(buf1, e2->value1) &&
106                             !strcmp(buf2, e2->value2))
107 @@ -255,22 +255,22 @@ static __init const struct bcm47xx_board
108                 }
109         }
110  
111 -       if (bcm47xx_nvram_getenv("productid", buf1, sizeof(buf1)) >= 0) {
112 +       if (bcm47xx_nvram_getenv(NULL, "productid", buf1, sizeof(buf1)) >= 0) {
113                 for (e1 = bcm47xx_board_list_productid; e1->value1; e1++) {
114                         if (!strcmp(buf1, e1->value1))
115                                 return &e1->board;
116                 }
117         }
118  
119 -       if (bcm47xx_nvram_getenv("ModelId", buf1, sizeof(buf1)) >= 0) {
120 +       if (bcm47xx_nvram_getenv(NULL, "ModelId", buf1, sizeof(buf1)) >= 0) {
121                 for (e1 = bcm47xx_board_list_ModelId; e1->value1; e1++) {
122                         if (!strcmp(buf1, e1->value1))
123                                 return &e1->board;
124                 }
125         }
126  
127 -       if (bcm47xx_nvram_getenv("melco_id", buf1, sizeof(buf1)) >= 0 ||
128 -           bcm47xx_nvram_getenv("buf1falo_id", buf1, sizeof(buf1)) >= 0) {
129 +       if (bcm47xx_nvram_getenv(NULL, "melco_id", buf1, sizeof(buf1)) >= 0 ||
130 +           bcm47xx_nvram_getenv(NULL, "buf1falo_id", buf1, sizeof(buf1)) >= 0) {
131                 /* buffalo hardware, check id for specific hardware matches */
132                 for (e1 = bcm47xx_board_list_melco_id; e1->value1; e1++) {
133                         if (!strcmp(buf1, e1->value1))
134 @@ -278,8 +278,8 @@ static __init const struct bcm47xx_board
135                 }
136         }
137  
138 -       if (bcm47xx_nvram_getenv("boot_hw_model", buf1, sizeof(buf1)) >= 0 &&
139 -           bcm47xx_nvram_getenv("boot_hw_ver", buf2, sizeof(buf2)) >= 0) {
140 +       if (bcm47xx_nvram_getenv(NULL, "boot_hw_model", buf1, sizeof(buf1)) >= 0 &&
141 +           bcm47xx_nvram_getenv(NULL, "boot_hw_ver", buf2, sizeof(buf2)) >= 0) {
142                 for (e2 = bcm47xx_board_list_boot_hw; e2->value1; e2++) {
143                         if (!strcmp(buf1, e2->value1) &&
144                             !strcmp(buf2, e2->value2))
145 @@ -287,16 +287,16 @@ static __init const struct bcm47xx_board
146                 }
147         }
148  
149 -       if (bcm47xx_nvram_getenv("board_id", buf1, sizeof(buf1)) >= 0) {
150 +       if (bcm47xx_nvram_getenv(NULL, "board_id", buf1, sizeof(buf1)) >= 0) {
151                 for (e1 = bcm47xx_board_list_board_id; e1->value1; e1++) {
152                         if (!strcmp(buf1, e1->value1))
153                                 return &e1->board;
154                 }
155         }
156  
157 -       if (bcm47xx_nvram_getenv("boardtype", buf1, sizeof(buf1)) >= 0 &&
158 -           bcm47xx_nvram_getenv("boardnum", buf2, sizeof(buf2)) >= 0 &&
159 -           bcm47xx_nvram_getenv("boardrev", buf3, sizeof(buf3)) >= 0) {
160 +       if (bcm47xx_nvram_getenv(NULL, "boardtype", buf1, sizeof(buf1)) >= 0 &&
161 +           bcm47xx_nvram_getenv(NULL, "boardnum", buf2, sizeof(buf2)) >= 0 &&
162 +           bcm47xx_nvram_getenv(NULL, "boardrev", buf3, sizeof(buf3)) >= 0) {
163                 for (e3 = bcm47xx_board_list_board; e3->value1; e3++) {
164                         if (!strcmp(buf1, e3->value1) &&
165                             !strcmp(buf2, e3->value2) &&
166 @@ -305,9 +305,9 @@ static __init const struct bcm47xx_board
167                 }
168         }
169  
170 -       if (bcm47xx_nvram_getenv("boardtype", buf1, sizeof(buf1)) >= 0 &&
171 -           bcm47xx_nvram_getenv("boardrev", buf2, sizeof(buf2)) >= 0 &&
172 -           bcm47xx_nvram_getenv("boardnum", buf3, sizeof(buf3)) ==  -ENOENT) {
173 +       if (bcm47xx_nvram_getenv(NULL, "boardtype", buf1, sizeof(buf1)) >= 0 &&
174 +           bcm47xx_nvram_getenv(NULL, "boardrev", buf2, sizeof(buf2)) >= 0 &&
175 +           bcm47xx_nvram_getenv(NULL, "boardnum", buf3, sizeof(buf3)) ==  -ENOENT) {
176                 for (e2 = bcm47xx_board_list_board_type_rev; e2->value1; e2++) {
177                         if (!strcmp(buf1, e2->value1) &&
178                             !strcmp(buf2, e2->value2))
179 @@ -327,7 +327,7 @@ void __init bcm47xx_board_detect(void)
180                 return;
181  
182         /* check if the nvram is available */
183 -       err = bcm47xx_nvram_getenv("boardtype", buf, sizeof(buf));
184 +       err = bcm47xx_nvram_getenv(NULL, "boardtype", buf, sizeof(buf));
185  
186         /* init of nvram failed, probably too early now */
187         if (err == -ENXIO) {
188 --- a/arch/mips/bcm47xx/nvram.c
189 +++ b/arch/mips/bcm47xx/nvram.c
190 @@ -158,7 +158,8 @@ static int nvram_init(void)
191         return -ENXIO;
192  }
193  
194 -int bcm47xx_nvram_getenv(char *name, char *val, size_t val_len)
195 +int bcm47xx_nvram_getenv(const struct device *dev, const char *name, char *val,
196 +                        size_t val_len)
197  {
198         char *var, *value, *end, *eq;
199         int err;
200 @@ -190,7 +191,7 @@ int bcm47xx_nvram_getenv(char *name, cha
201  }
202  EXPORT_SYMBOL(bcm47xx_nvram_getenv);
203  
204 -int bcm47xx_nvram_gpio_pin(const char *name)
205 +int bcm47xx_nvram_gpio_pin(const struct device *dev, const char *name)
206  {
207         int i, err;
208         char nvram_var[10];
209 @@ -200,7 +201,7 @@ int bcm47xx_nvram_gpio_pin(const char *n
210                 err = snprintf(nvram_var, sizeof(nvram_var), "gpio%i", i);
211                 if (err <= 0)
212                         continue;
213 -               err = bcm47xx_nvram_getenv(nvram_var, buf, sizeof(buf));
214 +               err = bcm47xx_nvram_getenv(dev, nvram_var, buf, sizeof(buf));
215                 if (err <= 0)
216                         continue;
217                 if (!strcmp(name, buf))
218 --- a/arch/mips/bcm47xx/setup.c
219 +++ b/arch/mips/bcm47xx/setup.c
220 @@ -132,7 +132,7 @@ static int bcm47xx_get_invariants(struct
221         memset(&iv->sprom, 0, sizeof(struct ssb_sprom));
222         bcm47xx_fill_sprom(&iv->sprom, NULL, false);
223  
224 -       if (bcm47xx_nvram_getenv("cardbus", buf, sizeof(buf)) >= 0)
225 +       if (bcm47xx_nvram_getenv(NULL, "cardbus", buf, sizeof(buf)) >= 0)
226                 iv->has_cardbus_slot = !!simple_strtoul(buf, NULL, 10);
227  
228         return 0;
229 @@ -155,7 +155,7 @@ static void __init bcm47xx_register_ssb(
230                 panic("Failed to initialize SSB bus (err %d)", err);
231  
232         mcore = &bcm47xx_bus.ssb.mipscore;
233 -       if (bcm47xx_nvram_getenv("kernel_args", buf, sizeof(buf)) >= 0) {
234 +       if (bcm47xx_nvram_getenv(NULL, "kernel_args", buf, sizeof(buf)) >= 0) {
235                 if (strstr(buf, "console=ttyS1")) {
236                         struct ssb_serial_port port;
237  
238 --- a/arch/mips/bcm47xx/sprom.c
239 +++ b/arch/mips/bcm47xx/sprom.c
240 @@ -52,10 +52,10 @@ static int get_nvram_var(const char *pre
241  
242         create_key(prefix, postfix, name, key, sizeof(key));
243  
244 -       err = bcm47xx_nvram_getenv(key, buf, len);
245 +       err = bcm47xx_nvram_getenv(NULL, key, buf, len);
246         if (fallback && err == -ENOENT && prefix) {
247                 create_key(NULL, postfix, name, key, sizeof(key));
248 -               err = bcm47xx_nvram_getenv(key, buf, len);
249 +               err = bcm47xx_nvram_getenv(NULL, key, buf, len);
250         }
251         return err;
252  }
253 --- a/arch/mips/bcm47xx/time.c
254 +++ b/arch/mips/bcm47xx/time.c
255 @@ -61,7 +61,7 @@ void __init plat_time_init(void)
256         }
257  
258         if (chip_id == 0x5354) {
259 -               len = bcm47xx_nvram_getenv("clkfreq", buf, sizeof(buf));
260 +               len = bcm47xx_nvram_getenv(NULL, "clkfreq", buf, sizeof(buf));
261                 if (len >= 0 && !strncmp(buf, "200", 4))
262                         hz = 100000000;
263         }
264 --- a/drivers/misc/Kconfig
265 +++ b/drivers/misc/Kconfig
266 @@ -515,6 +515,11 @@ config VEXPRESS_SYSCFG
267           bus. System Configuration interface is one of the possible means
268           of generating transactions on this bus.
269  
270 +config BCM47XX_NVRAM
271 +       tristate "BCM47XX nvram driver"
272 +       help
273 +               This adds support for the brcm47xx nvram driver.
274 +
275  source "drivers/misc/c2port/Kconfig"
276  source "drivers/misc/eeprom/Kconfig"
277  source "drivers/misc/cb710/Kconfig"
278 --- a/drivers/misc/Makefile
279 +++ b/drivers/misc/Makefile
280 @@ -56,3 +56,4 @@ obj-$(CONFIG_GENWQE)          += genwqe/
281  obj-$(CONFIG_ECHO)             += echo/
282  obj-$(CONFIG_VEXPRESS_SYSCFG)  += vexpress-syscfg.o
283  obj-$(CONFIG_CXL_BASE)         += cxl/
284 +obj-$(CONFIG_BCM47XX_NVRAM)    += bcm47xx-nvram.o
285 --- a/drivers/net/ethernet/broadcom/b44.c
286 +++ b/drivers/net/ethernet/broadcom/b44.c
287 @@ -411,7 +411,7 @@ static void b44_wap54g10_workaround(stru
288          * see https://dev.openwrt.org/ticket/146
289          * check and reset bit "isolate"
290          */
291 -       if (bcm47xx_nvram_getenv("boardnum", buf, sizeof(buf)) < 0)
292 +       if (bcm47xx_nvram_getenv(NULL, "boardnum", buf, sizeof(buf)) < 0)
293                 return;
294         if (simple_strtoul(buf, NULL, 0) == 2) {
295                 err = __b44_readphy(bp, 0, MII_BMCR, &val);
296 --- a/drivers/net/ethernet/broadcom/bgmac.c
297 +++ b/drivers/net/ethernet/broadcom/bgmac.c
298 @@ -974,7 +974,8 @@ static void bgmac_chip_reset(struct bgma
299                              BGMAC_CHIPCTL_1_IF_TYPE_MII;
300                 char buf[4];
301  
302 -               if (bcm47xx_nvram_getenv("et_swtype", buf, sizeof(buf)) > 0) {
303 +               if (bcm47xx_nvram_getenv(NULL, "et_swtype", buf,
304 +                                        sizeof(buf)) > 0) {
305                         if (kstrtou8(buf, 0, &et_swtype))
306                                 bgmac_err(bgmac, "Failed to parse et_swtype (%s)\n",
307                                           buf);
308 @@ -1534,7 +1535,7 @@ static int bgmac_probe(struct bcma_devic
309         }
310  
311         bgmac->int_mask = BGMAC_IS_ERRMASK | BGMAC_IS_RX | BGMAC_IS_TX_MASK;
312 -       if (bcm47xx_nvram_getenv("et0_no_txint", NULL, 0) == 0)
313 +       if (bcm47xx_nvram_getenv(NULL, "et0_no_txint", NULL, 0) == 0)
314                 bgmac->int_mask &= ~BGMAC_IS_TX_MASK;
315  
316         /* TODO: reset the external phy. Specs are needed */
317 --- a/drivers/ssb/driver_chipcommon_pmu.c
318 +++ b/drivers/ssb/driver_chipcommon_pmu.c
319 @@ -319,7 +319,8 @@ static void ssb_pmu_pll_init(struct ssb_
320  
321         if (bus->bustype == SSB_BUSTYPE_SSB) {
322                 char buf[20];
323 -               if (bcm47xx_nvram_getenv("xtalfreq", buf, sizeof(buf)) >= 0)
324 +               if (bcm47xx_nvram_getenv(NULL, "xtalfreq", buf,
325 +                                        sizeof(buf)) >= 0)
326                         crystalfreq = simple_strtoul(buf, NULL, 0);
327         }
328  
329 --- a/include/linux/bcm47xx_nvram.h
330 +++ b/include/linux/bcm47xx_nvram.h
331 @@ -15,9 +15,11 @@
332  #include <linux/types.h>
333  #include <linux/kernel.h>
334  
335 +struct device;
336 +
337  struct nvram_header {
338         u32 magic;
339 -       u32 len;
340 +       __le32 len;
341         u32 crc_ver_init;       /* 0:7 crc, 8:15 ver, 16:31 sdram_init */
342         u32 config_refresh;     /* 0:15 sdram_config, 16:31 sdram_refresh */
343         u32 config_ncdl;        /* ncdl values for memc */
344 @@ -33,18 +35,21 @@ struct nvram_header {
345  #define NVRAM_MAX_VALUE_LEN 255
346  #define NVRAM_MAX_PARAM_LEN 64
347  
348 -#ifdef CONFIG_BCM47XX
349 -int bcm47xx_nvram_getenv(const char *name, char *val, size_t val_len);
350 +#if defined(CONFIG_BCM47XX) || defined(CONFIG_BCM47XX_NVRAM)
351 +int bcm47xx_nvram_getenv(const struct device *dev, const char *name, char *val,
352 +                        size_t val_len);
353  
354 -int bcm47xx_nvram_gpio_pin(const char *name);
355 +int bcm47xx_nvram_gpio_pin(const struct device *dev, const char *name);
356  #else
357 -static inline int bcm47xx_nvram_getenv(const char *name, char *val,
358 +static inline int bcm47xx_nvram_getenv(const struct device *dev,
359 +                                      const char *name, char *val,
360                                        size_t val_len)
361  {
362         return -ENXIO;
363  }
364  
365 -static inline int bcm47xx_nvram_gpio_pin(const char *name)
366 +static inline int bcm47xx_nvram_gpio_pin(const struct device *dev,
367 +                                        const char *name)
368  {
369         return -ENXIO;
370  }