17be01c14f1a16d22386e9a57a2e286fc61ccd71
[openwrt/staging/xback.git] /
1 From 3fef9ed0627af30753a2404b8bd59d92cdb4c0ce Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
3 Date: Tue, 30 Mar 2021 12:12:36 +0100
4 Subject: [PATCH] nvmem: brcm_nvram: new driver exposing Broadcom's NVRAM
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 This driver provides access to Broadcom's NVRAM.
10
11 Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
12 Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
13 Link: https://lore.kernel.org/r/20210330111241.19401-6-srinivas.kandagatla@linaro.org
14 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
15 ---
16 drivers/nvmem/Kconfig | 9 +++++
17 drivers/nvmem/Makefile | 2 +
18 drivers/nvmem/brcm_nvram.c | 78 ++++++++++++++++++++++++++++++++++++++
19 3 files changed, 89 insertions(+)
20 create mode 100644 drivers/nvmem/brcm_nvram.c
21
22 --- a/drivers/nvmem/Kconfig
23 +++ b/drivers/nvmem/Kconfig
24 @@ -278,4 +278,13 @@ config NVMEM_RMEM
25
26 This driver can also be built as a module. If so, the module
27 will be called nvmem-rmem.
28 +
29 +config NVMEM_BRCM_NVRAM
30 + tristate "Broadcom's NVRAM support"
31 + depends on ARCH_BCM_5301X || COMPILE_TEST
32 + depends on HAS_IOMEM
33 + help
34 + This driver provides support for Broadcom's NVRAM that can be accessed
35 + using I/O mapping.
36 +
37 endif
38 --- a/drivers/nvmem/Makefile
39 +++ b/drivers/nvmem/Makefile
40 @@ -57,3 +57,5 @@ obj-$(CONFIG_SPRD_EFUSE) += nvmem_sprd_e
41 nvmem_sprd_efuse-y := sprd-efuse.o
42 obj-$(CONFIG_NVMEM_RMEM) += nvmem-rmem.o
43 nvmem-rmem-y := rmem.o
44 +obj-$(CONFIG_NVMEM_BRCM_NVRAM) += nvmem_brcm_nvram.o
45 +nvmem_brcm_nvram-y := brcm_nvram.o
46 --- /dev/null
47 +++ b/drivers/nvmem/brcm_nvram.c
48 @@ -0,0 +1,78 @@
49 +// SPDX-License-Identifier: GPL-2.0-only
50 +/*
51 + * Copyright (C) 2021 Rafał Miłecki <rafal@milecki.pl>
52 + */
53 +
54 +#include <linux/io.h>
55 +#include <linux/mod_devicetable.h>
56 +#include <linux/module.h>
57 +#include <linux/nvmem-provider.h>
58 +#include <linux/platform_device.h>
59 +
60 +struct brcm_nvram {
61 + struct device *dev;
62 + void __iomem *base;
63 +};
64 +
65 +static int brcm_nvram_read(void *context, unsigned int offset, void *val,
66 + size_t bytes)
67 +{
68 + struct brcm_nvram *priv = context;
69 + u8 *dst = val;
70 +
71 + while (bytes--)
72 + *dst++ = readb(priv->base + offset++);
73 +
74 + return 0;
75 +}
76 +
77 +static int brcm_nvram_probe(struct platform_device *pdev)
78 +{
79 + struct nvmem_config config = {
80 + .name = "brcm-nvram",
81 + .reg_read = brcm_nvram_read,
82 + };
83 + struct device *dev = &pdev->dev;
84 + struct resource *res;
85 + struct brcm_nvram *priv;
86 +
87 + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
88 + if (!priv)
89 + return -ENOMEM;
90 + priv->dev = dev;
91 +
92 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
93 + priv->base = devm_ioremap_resource(dev, res);
94 + if (IS_ERR(priv->base))
95 + return PTR_ERR(priv->base);
96 +
97 + config.dev = dev;
98 + config.priv = priv;
99 + config.size = resource_size(res);
100 +
101 + return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config));
102 +}
103 +
104 +static const struct of_device_id brcm_nvram_of_match_table[] = {
105 + { .compatible = "brcm,nvram", },
106 + {},
107 +};
108 +
109 +static struct platform_driver brcm_nvram_driver = {
110 + .probe = brcm_nvram_probe,
111 + .driver = {
112 + .name = "brcm_nvram",
113 + .of_match_table = brcm_nvram_of_match_table,
114 + },
115 +};
116 +
117 +static int __init brcm_nvram_init(void)
118 +{
119 + return platform_driver_register(&brcm_nvram_driver);
120 +}
121 +
122 +subsys_initcall_sync(brcm_nvram_init);
123 +
124 +MODULE_AUTHOR("Rafał Miłecki");
125 +MODULE_LICENSE("GPL");
126 +MODULE_DEVICE_TABLE(of, brcm_nvram_of_match_table);