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
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.
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
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:
19 compatible = "mtd-concat";
21 devices = <&flash0 &flash1>;
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.
32 Signed-off-by: Bernhard Frauendienst <kernel@nospam.obeliks.de>
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
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"
50 source "drivers/mtd/ubi/Kconfig"
52 +source "drivers/mtd/composite/Kconfig"
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/
61 obj-$(CONFIG_MTD_SPI_NOR) += spi-nor/
62 obj-$(CONFIG_MTD_UBI) += ubi/
64 +# Composite drivers must be loaded last
66 diff --git a/drivers/mtd/composite/Kconfig b/drivers/mtd/composite/Kconfig
68 index 000000000000..0490fc0284bb
70 +++ b/drivers/mtd/composite/Kconfig
72 +menu "Composite MTD device drivers"
75 +config MTD_VIRT_CONCAT
76 + tristate "Virtual concat MTD device"
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.
84 diff --git a/drivers/mtd/composite/Makefile b/drivers/mtd/composite/Makefile
86 index 000000000000..8421a0a30606
88 +++ b/drivers/mtd/composite/Makefile
90 +# SPDX-License-Identifier: GPL-2.0
92 +# linux/drivers/mtd/composite/Makefile
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
98 index 000000000000..bfd432188c35
100 +++ b/drivers/mtd/composite/virt_concat.c
102 +// SPDX-License-Identifier: GPL-2.0+
104 + * Virtual concat MTD device driver
106 + * Copyright (C) 2018 Bernhard Frauendienst
107 + * Author: Bernhard Frauendienst, kernel@nospam.obeliks.de
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>
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
125 +struct of_virt_concat {
126 + struct mtd_info *cmtd;
128 + struct mtd_info **devices;
131 +static int virt_concat_remove(struct platform_device *pdev)
133 + struct of_virt_concat *info;
136 + info = platform_get_drvdata(pdev);
140 + // unset data for when this is called after a probe error
141 + platform_set_drvdata(pdev, NULL);
144 + mtd_device_unregister(info->cmtd);
145 + mtd_concat_destroy(info->cmtd);
148 + if (info->devices) {
149 + for (i = 0; i < info->num_devices; i++)
150 + put_mtd_device(info->devices[i]);
156 +static int virt_concat_probe(struct platform_device *pdev)
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;
164 + count = of_count_phandle_with_args(node, "devices", NULL);
168 + info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
171 + info->devices = devm_kcalloc(&pdev->dev, count,
172 + sizeof(*(info->devices)), GFP_KERNEL);
173 + if (!info->devices) {
178 + platform_set_drvdata(pdev, info);
180 + of_for_each_phandle(&it, err, node, "devices", NULL, 0) {
181 + mtd = get_mtd_device_by_node(it.node);
183 + of_node_put(it.node);
184 + err = -EPROBE_DEFER;
188 + info->devices[info->num_devices++] = mtd;
191 + info->cmtd = mtd_concat_create(info->devices, info->num_devices,
192 + dev_name(&pdev->dev));
198 + info->cmtd->dev.parent = &pdev->dev;
199 + mtd_set_of_node(info->cmtd, node);
200 + mtd_device_register(info->cmtd, NULL, 0);
205 + virt_concat_remove(pdev);
210 +static const struct of_device_id virt_concat_of_match[] = {
211 + { .compatible = "mtd-concat", },
214 +MODULE_DEVICE_TABLE(of, virt_concat_of_match);
216 +static struct platform_driver virt_concat_driver = {
217 + .probe = virt_concat_probe,
218 + .remove = virt_concat_remove,
220 + .name = "virt-mtdconcat",
221 + .of_match_table = virt_concat_of_match,
225 +module_platform_driver(virt_concat_driver);
227 +MODULE_LICENSE("GPL v2");
228 +MODULE_AUTHOR("Bernhard Frauendienst <kernel@nospam.obeliks.de>");
229 +MODULE_DESCRIPTION("Virtual concat MTD device driver");