kernel: add driver for virtual mtd_concat devices
[openwrt/openwrt.git] / target / linux / generic / pending-4.14 / 497-mtd-mtdconcat-add-dt-driver-for-concat-devices.patch
1 From e53f712d8eac71f54399b61038ccf87d2cee99d7 Mon Sep 17 00:00:00 2001
2 From: Bernhard Frauendienst <kernel@nospam.obeliks.de>
3 Date: Sat, 25 Aug 2018 12:35:22 +0200
4 Subject: [PATCH 497/497] mtd: mtdconcat: add dt driver for concat devices
5
6 Some mtd drivers like physmap variants have support for concatenating
7 multiple mtd devices, but there is no generic way to define such a
8 concat device from within the device tree.
9
10 This is useful for some SoC boards that use multiple flash chips as
11 memory banks of a single mtd device, with partitions spanning chip
12 borders.
13
14 This commit adds a driver for creating virtual mtd-concat devices. They
15 must have a compatible = "mtd-concat" line, and define a list of devices
16 to concat in the 'devices' property, for example:
17
18 flash {
19 compatible = "mtd-concat";
20
21 devices = <&flash0 &flash1>;
22
23 partitions {
24 ...
25 };
26 };
27
28 The driver is added to the very end of the mtd Makefile to increase the
29 likelyhood of all child devices already being loaded at the time of
30 probing, preventing unnecessary deferred probes.
31
32 Signed-off-by: Bernhard Frauendienst <kernel@nospam.obeliks.de>
33 ---
34 drivers/mtd/Kconfig | 2 +
35 drivers/mtd/Makefile | 3 +
36 drivers/mtd/composite/Kconfig | 12 +++
37 drivers/mtd/composite/Makefile | 6 ++
38 drivers/mtd/composite/virt_concat.c | 128 ++++++++++++++++++++++++++++
39 5 files changed, 151 insertions(+)
40 create mode 100644 drivers/mtd/composite/Kconfig
41 create mode 100644 drivers/mtd/composite/Makefile
42 create mode 100644 drivers/mtd/composite/virt_concat.c
43
44 diff --git a/drivers/mtd/Kconfig b/drivers/mtd/Kconfig
45 index 5a2d71729b9a..de18bc568e3e 100644
46 --- a/drivers/mtd/Kconfig
47 +++ b/drivers/mtd/Kconfig
48 @@ -342,4 +342,6 @@ source "drivers/mtd/spi-nor/Kconfig"
49
50 source "drivers/mtd/ubi/Kconfig"
51
52 +source "drivers/mtd/composite/Kconfig"
53 +
54 endif # MTD
55 diff --git a/drivers/mtd/Makefile b/drivers/mtd/Makefile
56 index d6f8f625e1ff..a6c5f134c35d 100644
57 --- a/drivers/mtd/Makefile
58 +++ b/drivers/mtd/Makefile
59 @@ -36,3 +36,6 @@ obj-y += chips/ lpddr/ maps/ devices/ nand/ onenand/ tests/
60
61 obj-$(CONFIG_MTD_SPI_NOR) += spi-nor/
62 obj-$(CONFIG_MTD_UBI) += ubi/
63 +
64 +# Composite drivers must be loaded last
65 +obj-y += composite/
66 diff --git a/drivers/mtd/composite/Kconfig b/drivers/mtd/composite/Kconfig
67 new file mode 100644
68 index 000000000000..0490fc0284bb
69 --- /dev/null
70 +++ b/drivers/mtd/composite/Kconfig
71 @@ -0,0 +1,12 @@
72 +menu "Composite MTD device drivers"
73 + depends on MTD!=n
74 +
75 +config MTD_VIRT_CONCAT
76 + tristate "Virtual concat MTD device"
77 + help
78 + This driver allows creation of a virtual MTD concat device, which
79 + concatenates multiple underlying MTD devices to a single device.
80 + This is required by some SoC boards where multiple memory banks are
81 + used as one device with partitions spanning across device boundaries.
82 +
83 +endmenu
84 diff --git a/drivers/mtd/composite/Makefile b/drivers/mtd/composite/Makefile
85 new file mode 100644
86 index 000000000000..8421a0a30606
87 --- /dev/null
88 +++ b/drivers/mtd/composite/Makefile
89 @@ -0,0 +1,6 @@
90 +# SPDX-License-Identifier: GPL-2.0
91 +#
92 +# linux/drivers/mtd/composite/Makefile
93 +#
94 +
95 +obj-$(CONFIG_MTD_VIRT_CONCAT) += virt_concat.o
96 diff --git a/drivers/mtd/composite/virt_concat.c b/drivers/mtd/composite/virt_concat.c
97 new file mode 100644
98 index 000000000000..bfd432188c35
99 --- /dev/null
100 +++ b/drivers/mtd/composite/virt_concat.c
101 @@ -0,0 +1,128 @@
102 +// SPDX-License-Identifier: GPL-2.0+
103 +/*
104 + * Virtual concat MTD device driver
105 + *
106 + * Copyright (C) 2018 Bernhard Frauendienst
107 + * Author: Bernhard Frauendienst, kernel@nospam.obeliks.de
108 + */
109 +
110 +#include <linux/module.h>
111 +#include <linux/device.h>
112 +#include <linux/mtd/concat.h>
113 +#include <linux/mtd/mtd.h>
114 +#include <linux/mtd/partitions.h>
115 +#include <linux/of.h>
116 +#include <linux/of_platform.h>
117 +#include <linux/slab.h>
118 +
119 +/*
120 + * struct of_virt_concat - platform device driver data.
121 + * @cmtd the final mtd_concat device
122 + * @num_devices the number of devices in @devices
123 + * @devices points to an array of devices already loaded
124 + */
125 +struct of_virt_concat {
126 + struct mtd_info *cmtd;
127 + int num_devices;
128 + struct mtd_info **devices;
129 +};
130 +
131 +static int virt_concat_remove(struct platform_device *pdev)
132 +{
133 + struct of_virt_concat *info;
134 + int i;
135 +
136 + info = platform_get_drvdata(pdev);
137 + if (!info)
138 + return 0;
139 +
140 + // unset data for when this is called after a probe error
141 + platform_set_drvdata(pdev, NULL);
142 +
143 + if (info->cmtd) {
144 + mtd_device_unregister(info->cmtd);
145 + mtd_concat_destroy(info->cmtd);
146 + }
147 +
148 + if (info->devices) {
149 + for (i = 0; i < info->num_devices; i++)
150 + put_mtd_device(info->devices[i]);
151 + }
152 +
153 + return 0;
154 +}
155 +
156 +static int virt_concat_probe(struct platform_device *pdev)
157 +{
158 + struct device_node *node = pdev->dev.of_node;
159 + struct of_phandle_iterator it;
160 + struct of_virt_concat *info;
161 + struct mtd_info *mtd;
162 + int err = 0, count;
163 +
164 + count = of_count_phandle_with_args(node, "devices", NULL);
165 + if (count <= 0)
166 + return -EINVAL;
167 +
168 + info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
169 + if (!info)
170 + return -ENOMEM;
171 + info->devices = devm_kcalloc(&pdev->dev, count,
172 + sizeof(*(info->devices)), GFP_KERNEL);
173 + if (!info->devices) {
174 + err = -ENOMEM;
175 + goto err_remove;
176 + }
177 +
178 + platform_set_drvdata(pdev, info);
179 +
180 + of_for_each_phandle(&it, err, node, "devices", NULL, 0) {
181 + mtd = get_mtd_device_by_node(it.node);
182 + if (IS_ERR(mtd)) {
183 + of_node_put(it.node);
184 + err = -EPROBE_DEFER;
185 + goto err_remove;
186 + }
187 +
188 + info->devices[info->num_devices++] = mtd;
189 + }
190 +
191 + info->cmtd = mtd_concat_create(info->devices, info->num_devices,
192 + dev_name(&pdev->dev));
193 + if (!info->cmtd) {
194 + err = -ENXIO;
195 + goto err_remove;
196 + }
197 +
198 + info->cmtd->dev.parent = &pdev->dev;
199 + mtd_set_of_node(info->cmtd, node);
200 + mtd_device_register(info->cmtd, NULL, 0);
201 +
202 + return 0;
203 +
204 +err_remove:
205 + virt_concat_remove(pdev);
206 +
207 + return err;
208 +}
209 +
210 +static const struct of_device_id virt_concat_of_match[] = {
211 + { .compatible = "mtd-concat", },
212 + { /* sentinel */ }
213 +};
214 +MODULE_DEVICE_TABLE(of, virt_concat_of_match);
215 +
216 +static struct platform_driver virt_concat_driver = {
217 + .probe = virt_concat_probe,
218 + .remove = virt_concat_remove,
219 + .driver = {
220 + .name = "virt-mtdconcat",
221 + .of_match_table = virt_concat_of_match,
222 + },
223 +};
224 +
225 +module_platform_driver(virt_concat_driver);
226 +
227 +MODULE_LICENSE("GPL v2");
228 +MODULE_AUTHOR("Bernhard Frauendienst <kernel@nospam.obeliks.de>");
229 +MODULE_DESCRIPTION("Virtual concat MTD device driver");
230 --
231 2.18.0
232