1 From 60a413ed5bc7917f1612df441240f458163b10c1 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 03/15] bcm47xx-nvram: add new nvram driver with dt support
6 This adds a new nvrm driver which uses device tree to provide nvram
7 access to other drivers. You have to specify the memory ranges where
8 the flash chip is mapped and this driver will search there for some
9 nvram and parse it. Other drivers can use this driver to access the
10 device nvram. The nvram is used to store board configurations like the
11 mac address and also for configuration values in the vendor firmware.
13 arch/mips/bcm47xx/board.c | 36 +++---
14 arch/mips/bcm47xx/nvram.c | 7 +-
15 arch/mips/bcm47xx/setup.c | 4 +-
16 arch/mips/bcm47xx/sprom.c | 4 +-
17 arch/mips/bcm47xx/time.c | 2 +-
18 drivers/misc/Kconfig | 5 +
19 drivers/misc/Makefile | 1 +
20 drivers/misc/bcm47xx-nvram.c | 211 ++++++++++++++++++++++++++++++++++
21 drivers/net/ethernet/broadcom/b44.c | 2 +-
22 drivers/net/ethernet/broadcom/bgmac.c | 4 +-
23 drivers/ssb/driver_chipcommon_pmu.c | 2 +-
24 include/linux/bcm47xx_nvram.h | 16 ++-
25 12 files changed, 259 insertions(+), 35 deletions(-)
26 create mode 100644 drivers/misc/bcm47xx-nvram.c
28 --- a/arch/mips/bcm47xx/board.c
29 +++ b/arch/mips/bcm47xx/board.c
30 @@ -196,50 +196,50 @@ static __init const struct bcm47xx_board
31 const struct bcm47xx_board_type_list2 *e2;
32 const struct bcm47xx_board_type_list3 *e3;
34 - if (bcm47xx_nvram_getenv("model_name", buf1, sizeof(buf1)) >= 0) {
35 + if (bcm47xx_nvram_getenv(NULL, "model_name", buf1, sizeof(buf1)) >= 0) {
36 for (e1 = bcm47xx_board_list_model_name; e1->value1; e1++) {
37 if (!strcmp(buf1, e1->value1))
42 - if (bcm47xx_nvram_getenv("model_no", buf1, sizeof(buf1)) >= 0) {
43 + if (bcm47xx_nvram_getenv(NULL, "model_no", buf1, sizeof(buf1)) >= 0) {
44 for (e1 = bcm47xx_board_list_model_no; e1->value1; e1++) {
45 if (strstarts(buf1, e1->value1))
50 - if (bcm47xx_nvram_getenv("machine_name", buf1, sizeof(buf1)) >= 0) {
51 + if (bcm47xx_nvram_getenv(NULL, "machine_name", buf1, sizeof(buf1)) >= 0) {
52 for (e1 = bcm47xx_board_list_machine_name; e1->value1; e1++) {
53 if (strstarts(buf1, e1->value1))
58 - if (bcm47xx_nvram_getenv("hardware_version", buf1, sizeof(buf1)) >= 0) {
59 + if (bcm47xx_nvram_getenv(NULL, "hardware_version", buf1, sizeof(buf1)) >= 0) {
60 for (e1 = bcm47xx_board_list_hardware_version; e1->value1; e1++) {
61 if (strstarts(buf1, e1->value1))
66 - if (bcm47xx_nvram_getenv("productid", buf1, sizeof(buf1)) >= 0) {
67 + if (bcm47xx_nvram_getenv(NULL, "productid", buf1, sizeof(buf1)) >= 0) {
68 for (e1 = bcm47xx_board_list_productid; e1->value1; e1++) {
69 if (!strcmp(buf1, e1->value1))
74 - if (bcm47xx_nvram_getenv("ModelId", buf1, sizeof(buf1)) >= 0) {
75 + if (bcm47xx_nvram_getenv(NULL, "ModelId", buf1, sizeof(buf1)) >= 0) {
76 for (e1 = bcm47xx_board_list_ModelId; e1->value1; e1++) {
77 if (!strcmp(buf1, e1->value1))
82 - if (bcm47xx_nvram_getenv("melco_id", buf1, sizeof(buf1)) >= 0 ||
83 - bcm47xx_nvram_getenv("buf1falo_id", buf1, sizeof(buf1)) >= 0) {
84 + if (bcm47xx_nvram_getenv(NULL, "melco_id", buf1, sizeof(buf1)) >= 0 ||
85 + bcm47xx_nvram_getenv(NULL, "buf1falo_id", buf1, sizeof(buf1)) >= 0) {
86 /* buffalo hardware, check id for specific hardware matches */
87 for (e1 = bcm47xx_board_list_melco_id; e1->value1; e1++) {
88 if (!strcmp(buf1, e1->value1))
89 @@ -247,8 +247,8 @@ static __init const struct bcm47xx_board
93 - if (bcm47xx_nvram_getenv("boot_hw_model", buf1, sizeof(buf1)) >= 0 &&
94 - bcm47xx_nvram_getenv("boot_hw_ver", buf2, sizeof(buf2)) >= 0) {
95 + if (bcm47xx_nvram_getenv(NULL, "boot_hw_model", buf1, sizeof(buf1)) >= 0 &&
96 + bcm47xx_nvram_getenv(NULL, "boot_hw_ver", buf2, sizeof(buf2)) >= 0) {
97 for (e2 = bcm47xx_board_list_boot_hw; e2->value1; e2++) {
98 if (!strcmp(buf1, e2->value1) &&
99 !strcmp(buf2, e2->value2))
100 @@ -256,16 +256,16 @@ static __init const struct bcm47xx_board
104 - if (bcm47xx_nvram_getenv("board_id", buf1, sizeof(buf1)) >= 0) {
105 + if (bcm47xx_nvram_getenv(NULL, "board_id", buf1, sizeof(buf1)) >= 0) {
106 for (e1 = bcm47xx_board_list_board_id; e1->value1; e1++) {
107 if (!strcmp(buf1, e1->value1))
112 - if (bcm47xx_nvram_getenv("boardtype", buf1, sizeof(buf1)) >= 0 &&
113 - bcm47xx_nvram_getenv("boardnum", buf2, sizeof(buf2)) >= 0 &&
114 - bcm47xx_nvram_getenv("boardrev", buf3, sizeof(buf3)) >= 0) {
115 + if (bcm47xx_nvram_getenv(NULL, "boardtype", buf1, sizeof(buf1)) >= 0 &&
116 + bcm47xx_nvram_getenv(NULL, "boardnum", buf2, sizeof(buf2)) >= 0 &&
117 + bcm47xx_nvram_getenv(NULL, "boardrev", buf3, sizeof(buf3)) >= 0) {
118 for (e3 = bcm47xx_board_list_board; e3->value1; e3++) {
119 if (!strcmp(buf1, e3->value1) &&
120 !strcmp(buf2, e3->value2) &&
121 @@ -286,7 +286,7 @@ void __init bcm47xx_board_detect(void)
124 /* check if the nvram is available */
125 - err = bcm47xx_nvram_getenv("boardtype", buf, sizeof(buf));
126 + err = bcm47xx_nvram_getenv(NULL, "boardtype", buf, sizeof(buf));
128 /* init of nvram failed, probably too early now */
130 --- a/arch/mips/bcm47xx/nvram.c
131 +++ b/arch/mips/bcm47xx/nvram.c
132 @@ -158,7 +158,8 @@ static int nvram_init(void)
136 -int bcm47xx_nvram_getenv(char *name, char *val, size_t val_len)
137 +int bcm47xx_nvram_getenv(const struct device *dev, const char *name, char *val,
140 char *var, *value, *end, *eq;
142 @@ -190,7 +191,7 @@ int bcm47xx_nvram_getenv(char *name, cha
144 EXPORT_SYMBOL(bcm47xx_nvram_getenv);
146 -int bcm47xx_nvram_gpio_pin(const char *name)
147 +int bcm47xx_nvram_gpio_pin(const struct device *dev, const char *name)
151 @@ -200,7 +201,7 @@ int bcm47xx_nvram_gpio_pin(const char *n
152 err = snprintf(nvram_var, sizeof(nvram_var), "gpio%i", i);
155 - err = bcm47xx_nvram_getenv(nvram_var, buf, sizeof(buf));
156 + err = bcm47xx_nvram_getenv(dev, nvram_var, buf, sizeof(buf));
159 if (!strcmp(name, buf))
160 --- a/arch/mips/bcm47xx/setup.c
161 +++ b/arch/mips/bcm47xx/setup.c
162 @@ -123,7 +123,7 @@ static int bcm47xx_get_invariants(struct
163 memset(&iv->sprom, 0, sizeof(struct ssb_sprom));
164 bcm47xx_fill_sprom(&iv->sprom, NULL, false);
166 - if (bcm47xx_nvram_getenv("cardbus", buf, sizeof(buf)) >= 0)
167 + if (bcm47xx_nvram_getenv(NULL, "cardbus", buf, sizeof(buf)) >= 0)
168 iv->has_cardbus_slot = !!simple_strtoul(buf, NULL, 10);
171 @@ -146,7 +146,7 @@ static void __init bcm47xx_register_ssb(
172 panic("Failed to initialize SSB bus (err %d)", err);
174 mcore = &bcm47xx_bus.ssb.mipscore;
175 - if (bcm47xx_nvram_getenv("kernel_args", buf, sizeof(buf)) >= 0) {
176 + if (bcm47xx_nvram_getenv(NULL, "kernel_args", buf, sizeof(buf)) >= 0) {
177 if (strstr(buf, "console=ttyS1")) {
178 struct ssb_serial_port port;
180 --- a/arch/mips/bcm47xx/sprom.c
181 +++ b/arch/mips/bcm47xx/sprom.c
182 @@ -50,10 +50,10 @@ static int get_nvram_var(const char *pre
184 create_key(prefix, postfix, name, key, sizeof(key));
186 - err = bcm47xx_nvram_getenv(key, buf, len);
187 + err = bcm47xx_nvram_getenv(NULL, key, buf, len);
188 if (fallback && err == -ENOENT && prefix) {
189 create_key(NULL, postfix, name, key, sizeof(key));
190 - err = bcm47xx_nvram_getenv(key, buf, len);
191 + err = bcm47xx_nvram_getenv(NULL, key, buf, len);
195 --- a/arch/mips/bcm47xx/time.c
196 +++ b/arch/mips/bcm47xx/time.c
197 @@ -61,7 +61,7 @@ void __init plat_time_init(void)
200 if (chip_id == 0x5354) {
201 - len = bcm47xx_nvram_getenv("clkfreq", buf, sizeof(buf));
202 + len = bcm47xx_nvram_getenv(NULL, "clkfreq", buf, sizeof(buf));
203 if (len >= 0 && !strncmp(buf, "200", 4))
206 --- a/drivers/misc/Kconfig
207 +++ b/drivers/misc/Kconfig
208 @@ -515,6 +515,11 @@ config SRAM
209 the genalloc API. It is supposed to be used for small on-chip SRAM
210 areas found on many SoCs.
212 +config BCM47XX_NVRAM
213 + tristate "BCM47XX nvram driver"
215 + This adds support for the brcm47xx nvram driver.
217 source "drivers/misc/c2port/Kconfig"
218 source "drivers/misc/eeprom/Kconfig"
219 source "drivers/misc/cb710/Kconfig"
220 --- a/drivers/misc/Makefile
221 +++ b/drivers/misc/Makefile
222 @@ -54,3 +54,4 @@ obj-$(CONFIG_LATTICE_ECP3_CONFIG) += lat
223 obj-$(CONFIG_SRAM) += sram.o
225 obj-$(CONFIG_GENWQE) += genwqe/
226 +obj-$(CONFIG_BCM47XX_NVRAM) += bcm47xx-nvram.o
228 +++ b/drivers/misc/bcm47xx-nvram.c
231 + * BCM947xx nvram variable access
233 + * Copyright (C) 2005 Broadcom Corporation
234 + * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
235 + * Copyright (C) 2010-2014 Hauke Mehrtens <hauke@hauke-m.de>
237 + * This program is free software; you can redistribute it and/or modify it
238 + * under the terms of the GNU General Public License as published by the
239 + * Free Software Foundation; either version 2 of the License, or (at your
240 + * option) any later version.
243 +#include <linux/types.h>
244 +#include <linux/module.h>
245 +#include <linux/kernel.h>
246 +#include <linux/string.h>
247 +#include <linux/of_address.h>
248 +#include <linux/device.h>
249 +#include <linux/platform_device.h>
250 +#include <linux/io.h>
251 +#include <linux/bcm47xx_nvram.h>
253 +struct bcm47xx_nvram {
258 +static const u32 nvram_sizes[] = {0x8000, 0xF000, 0x10000};
260 +static u32 find_nvram_size(void __iomem *end)
262 + struct nvram_header *header;
265 + for (i = 0; i < ARRAY_SIZE(nvram_sizes); i++) {
266 + header = (struct nvram_header *)(end - nvram_sizes[i]);
267 + if (header->magic == NVRAM_HEADER)
268 + return nvram_sizes[i];
274 +/* Probe for NVRAM header */
275 +static int nvram_find_and_copy(struct device *dev, void __iomem *base,
276 + size_t len, char **nvram_buf,
279 + struct nvram_header *header;
285 + /* TODO: when nvram is on nand flash check for bad blocks first. */
287 + while (off <= len) {
288 + /* Windowed flash access */
289 + size = find_nvram_size(base + off);
291 + header = (struct nvram_header *)(base + off - size);
297 + /* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
298 + header = (struct nvram_header *)(base + 4096);
299 + if (header->magic == NVRAM_HEADER) {
300 + size = NVRAM_SPACE;
304 + header = (struct nvram_header *)(base + 1024);
305 + if (header->magic == NVRAM_HEADER) {
306 + size = NVRAM_SPACE;
312 + pr_err("no nvram found\n");
316 + if (header->len > size)
317 + pr_err("The nvram size accoridng to the header seems to be bigger than the partition on flash\n");
318 + *nvram_len = min_t(u32, header->len, size);
320 + *nvram_buf = devm_kzalloc(dev, *nvram_len, GFP_KERNEL);
324 + src = (u32 *) header;
325 + dst = (u32 *) *nvram_buf;
326 + for (i = 0; i < sizeof(struct nvram_header); i += 4)
328 + for (; i < *nvram_len; i += 4)
329 + *dst++ = le32_to_cpu(*src++);
334 +int bcm47xx_nvram_getenv(const struct device *dev, const char *name, char *val, size_t val_len)
336 + char *var, *value, *end, *eq;
337 + struct bcm47xx_nvram *nvram;
342 + nvram = dev_get_drvdata(dev);
344 + if (!name || !nvram || !nvram->nvram_len)
347 + /* Look for name=value and return value */
348 + var = nvram->nvram_buf + sizeof(struct nvram_header);
349 + end = nvram->nvram_buf + nvram->nvram_len - 2;
350 + end[0] = end[1] = '\0';
351 + for (; *var; var = value + strlen(value) + 1) {
352 + eq = strchr(var, '=');
356 + if ((eq - var) == strlen(name) &&
357 + strncmp(var, name, (eq - var)) == 0) {
358 + return snprintf(val, val_len, "%s", value);
363 +EXPORT_SYMBOL(bcm47xx_nvram_getenv);
365 +int bcm47xx_nvram_gpio_pin(const struct device *dev, const char *name)
368 + char nvram_var[10];
374 + for (i = 0; i < 32; i++) {
375 + err = snprintf(nvram_var, sizeof(nvram_var), "gpio%i", i);
378 + err = bcm47xx_nvram_getenv(dev, nvram_var, buf, sizeof(buf));
381 + if (!strcmp(name, buf))
386 +EXPORT_SYMBOL(bcm47xx_nvram_gpio_pin);
388 +static int bcm47xx_nvram_probe(struct platform_device *pdev)
390 + struct device *dev = &pdev->dev;
391 + struct device_node *np = dev->of_node;
392 + struct bcm47xx_nvram *nvram;
394 + struct resource flash_mem;
395 + void __iomem *mmio;
398 + nvram = devm_kzalloc(dev, sizeof(*nvram), GFP_KERNEL);
402 + err = of_address_to_resource(np, 0, &flash_mem);
406 + mmio = ioremap_nocache(flash_mem.start, resource_size(&flash_mem));
410 + err = nvram_find_and_copy(dev, mmio, resource_size(&flash_mem), &nvram->nvram_buf, &nvram->nvram_len);
412 + goto err_unmap_mmio;
414 + platform_set_drvdata(pdev, nvram);
421 +static const struct of_device_id bcm47xx_nvram_of_match_table[] = {
422 + { .compatible = "brcm,bcm47xx-nvram", },
425 +MODULE_DEVICE_TABLE(of, mvebu_pcie_of_match_table);
427 +static struct platform_driver bcm47xx_nvram_driver = {
429 + .owner = THIS_MODULE,
430 + .name = "bcm47xx-nvram",
431 + .of_match_table = bcm47xx_nvram_of_match_table,
432 + /* driver unloading/unbinding currently not supported */
433 + .suppress_bind_attrs = true,
435 + .probe = bcm47xx_nvram_probe,
437 +module_platform_driver(bcm47xx_nvram_driver);
439 +MODULE_AUTHOR("Hauke Mehrtens <hauke@hauke-m.de>");
440 +MODULE_LICENSE("GPLv2");
441 --- a/drivers/net/ethernet/broadcom/b44.c
442 +++ b/drivers/net/ethernet/broadcom/b44.c
443 @@ -411,7 +411,7 @@ static void b44_wap54g10_workaround(stru
444 * see https://dev.openwrt.org/ticket/146
445 * check and reset bit "isolate"
447 - if (bcm47xx_nvram_getenv("boardnum", buf, sizeof(buf)) < 0)
448 + if (bcm47xx_nvram_getenv(NULL, "boardnum", buf, sizeof(buf)) < 0)
450 if (simple_strtoul(buf, NULL, 0) == 2) {
451 err = __b44_readphy(bp, 0, MII_BMCR, &val);
452 --- a/drivers/net/ethernet/broadcom/bgmac.c
453 +++ b/drivers/net/ethernet/broadcom/bgmac.c
454 @@ -974,7 +974,7 @@ static void bgmac_chip_reset(struct bgma
455 BGMAC_CHIPCTL_1_IF_TYPE_MII;
458 - if (bcm47xx_nvram_getenv("et_swtype", buf, sizeof(buf)) > 0) {
459 + if (bcm47xx_nvram_getenv(NULL, "et_swtype", buf, sizeof(buf)) > 0) {
460 if (kstrtou8(buf, 0, &et_swtype))
461 bgmac_err(bgmac, "Failed to parse et_swtype (%s)\n",
463 @@ -1534,7 +1534,7 @@ static int bgmac_probe(struct bcma_devic
466 bgmac->int_mask = BGMAC_IS_ERRMASK | BGMAC_IS_RX | BGMAC_IS_TX_MASK;
467 - if (bcm47xx_nvram_getenv("et0_no_txint", NULL, 0) == 0)
468 + if (bcm47xx_nvram_getenv(NULL, "et0_no_txint", NULL, 0) == 0)
469 bgmac->int_mask &= ~BGMAC_IS_TX_MASK;
471 /* TODO: reset the external phy. Specs are needed */
472 --- a/drivers/ssb/driver_chipcommon_pmu.c
473 +++ b/drivers/ssb/driver_chipcommon_pmu.c
474 @@ -319,7 +319,7 @@ static void ssb_pmu_pll_init(struct ssb_
476 if (bus->bustype == SSB_BUSTYPE_SSB) {
478 - if (bcm47xx_nvram_getenv("xtalfreq", buf, sizeof(buf)) >= 0)
479 + if (bcm47xx_nvram_getenv(NULL, "xtalfreq", buf, sizeof(buf)) >= 0)
480 crystalfreq = simple_strtoul(buf, NULL, 0);
483 --- a/include/linux/bcm47xx_nvram.h
484 +++ b/include/linux/bcm47xx_nvram.h
486 #include <linux/types.h>
487 #include <linux/kernel.h>
491 struct nvram_header {
494 @@ -33,17 +35,21 @@ struct nvram_header {
495 #define NVRAM_MAX_VALUE_LEN 255
496 #define NVRAM_MAX_PARAM_LEN 64
498 -#ifdef CONFIG_BCM47XX
499 -int bcm47xx_nvram_getenv(const char *name, char *val, size_t val_len);
500 +#if defined(CONFIG_BCM47XX) || defined(CONFIG_BCM47XX_NVRAM)
501 +int bcm47xx_nvram_getenv(const struct device *dev, const char *name, char *val,
504 -int bcm47xx_nvram_gpio_pin(const char *name);
505 +int bcm47xx_nvram_gpio_pin(const struct device *dev, const char *name);
507 -static inline int bcm47xx_nvram_getenv(const char *name, char *val, size_t val_len)
508 +static inline int bcm47xx_nvram_getenv(const struct device *dev,
509 + const char *name, char *val,
515 -static inline int bcm47xx_nvram_gpio_pin(const char *name)
516 +static inline int bcm47xx_nvram_gpio_pin(const struct device *dev,