f3ed563d20166a2a226112ff0e12fd92aff8f266
[openwrt/staging/xback.git] /
1 From 5a3fa75a4d9cb6bcfc9081ef224a4cdcd4b3eafe Mon Sep 17 00:00:00 2001
2 From: Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
3 Date: Fri, 29 Jan 2021 17:14:29 +0000
4 Subject: [PATCH] nvmem: Add driver to expose reserved memory as nvmem
5
6 Firmware/co-processors might use reserved memory areas in order to pass
7 data stemming from an nvmem device otherwise non accessible to Linux.
8 For example an EEPROM memory only physically accessible to firmware, or
9 data only accessible early at boot time.
10
11 In order to expose this data to other drivers and user-space, the driver
12 models the reserved memory area as an nvmem device.
13
14 Tested-by: Tim Gover <tim.gover@raspberrypi.com>
15 Reviewed-by: Rob Herring <robh@kernel.org>
16 Signed-off-by: Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
17 Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
18 Link: https://lore.kernel.org/r/20210129171430.11328-5-srinivas.kandagatla@linaro.org
19 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
20 ---
21 drivers/nvmem/Kconfig | 8 ++++
22 drivers/nvmem/Makefile | 2 +
23 drivers/nvmem/rmem.c | 97 ++++++++++++++++++++++++++++++++++++++++++
24 drivers/of/platform.c | 1 +
25 4 files changed, 108 insertions(+)
26 create mode 100644 drivers/nvmem/rmem.c
27
28 --- a/drivers/nvmem/Kconfig
29 +++ b/drivers/nvmem/Kconfig
30 @@ -270,4 +270,12 @@ config SPRD_EFUSE
31 This driver can also be built as a module. If so, the module
32 will be called nvmem-sprd-efuse.
33
34 +config NVMEM_RMEM
35 + tristate "Reserved Memory Based Driver Support"
36 + help
37 + This drivers maps reserved memory into an nvmem device. It might be
38 + useful to expose information left by firmware in memory.
39 +
40 + This driver can also be built as a module. If so, the module
41 + will be called nvmem-rmem.
42 endif
43 --- a/drivers/nvmem/Makefile
44 +++ b/drivers/nvmem/Makefile
45 @@ -55,3 +55,5 @@ obj-$(CONFIG_NVMEM_ZYNQMP) += nvmem_zynq
46 nvmem_zynqmp_nvmem-y := zynqmp_nvmem.o
47 obj-$(CONFIG_SPRD_EFUSE) += nvmem_sprd_efuse.o
48 nvmem_sprd_efuse-y := sprd-efuse.o
49 +obj-$(CONFIG_NVMEM_RMEM) += nvmem-rmem.o
50 +nvmem-rmem-y := rmem.o
51 --- /dev/null
52 +++ b/drivers/nvmem/rmem.c
53 @@ -0,0 +1,97 @@
54 +// SPDX-License-Identifier: GPL-2.0+
55 +/*
56 + * Copyright (C) 2020 Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
57 + */
58 +
59 +#include <linux/io.h>
60 +#include <linux/module.h>
61 +#include <linux/nvmem-provider.h>
62 +#include <linux/of_reserved_mem.h>
63 +#include <linux/platform_device.h>
64 +
65 +struct rmem {
66 + struct device *dev;
67 + struct nvmem_device *nvmem;
68 + struct reserved_mem *mem;
69 +
70 + phys_addr_t size;
71 +};
72 +
73 +static int rmem_read(void *context, unsigned int offset,
74 + void *val, size_t bytes)
75 +{
76 + struct rmem *priv = context;
77 + size_t available = priv->mem->size;
78 + loff_t off = offset;
79 + void *addr;
80 + int count;
81 +
82 + /*
83 + * Only map the reserved memory at this point to avoid potential rogue
84 + * kernel threads inadvertently modifying it. Based on the current
85 + * uses-cases for this driver, the performance hit isn't a concern.
86 + * Nor is likely to be, given the nature of the subsystem. Most nvmem
87 + * devices operate over slow buses to begin with.
88 + *
89 + * An alternative would be setting the memory as RO, set_memory_ro(),
90 + * but as of Dec 2020 this isn't possible on arm64.
91 + */
92 + addr = memremap(priv->mem->base, available, MEMREMAP_WB);
93 + if (IS_ERR(addr)) {
94 + dev_err(priv->dev, "Failed to remap memory region\n");
95 + return PTR_ERR(addr);
96 + }
97 +
98 + count = memory_read_from_buffer(val, bytes, &off, addr, available);
99 +
100 + memunmap(addr);
101 +
102 + return count;
103 +}
104 +
105 +static int rmem_probe(struct platform_device *pdev)
106 +{
107 + struct nvmem_config config = { };
108 + struct device *dev = &pdev->dev;
109 + struct reserved_mem *mem;
110 + struct rmem *priv;
111 +
112 + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
113 + if (!priv)
114 + return -ENOMEM;
115 + priv->dev = dev;
116 +
117 + mem = of_reserved_mem_lookup(dev->of_node);
118 + if (!mem) {
119 + dev_err(dev, "Failed to lookup reserved memory\n");
120 + return -EINVAL;
121 + }
122 + priv->mem = mem;
123 +
124 + config.dev = dev;
125 + config.priv = priv;
126 + config.name = "rmem";
127 + config.size = mem->size;
128 + config.reg_read = rmem_read;
129 +
130 + return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config));
131 +}
132 +
133 +static const struct of_device_id rmem_match[] = {
134 + { .compatible = "nvmem-rmem", },
135 + { /* sentinel */ },
136 +};
137 +MODULE_DEVICE_TABLE(of, rmem_match);
138 +
139 +static struct platform_driver rmem_driver = {
140 + .probe = rmem_probe,
141 + .driver = {
142 + .name = "rmem",
143 + .of_match_table = rmem_match,
144 + },
145 +};
146 +module_platform_driver(rmem_driver);
147 +
148 +MODULE_AUTHOR("Nicolas Saenz Julienne <nsaenzjulienne@suse.de>");
149 +MODULE_DESCRIPTION("Reserved Memory Based nvmem Driver");
150 +MODULE_LICENSE("GPL");
151 --- a/drivers/of/platform.c
152 +++ b/drivers/of/platform.c
153 @@ -511,6 +511,7 @@ static const struct of_device_id reserve
154 { .compatible = "qcom,rmtfs-mem" },
155 { .compatible = "qcom,cmd-db" },
156 { .compatible = "ramoops" },
157 + { .compatible = "nvmem-rmem" },
158 {}
159 };
160