@@ -276,6 +276,14 @@ config MTD_PARTITIONED_MASTER
the parent of the partition device be the master device, rather than
what lies behind the master.
+config MTD_VIRT_CONCAT
+ tristate "Virtual concatenated MTD devices"
+ help
+ This driver allows creation of a virtual MTD device, which
+ concatenates multiple physical MTD devices into a single one.
+ This is useful to create partitions bigger than the underlying
+ physical chips by allowing cross-chip boundaries.
+
source "drivers/mtd/chips/Kconfig"
source "drivers/mtd/maps/Kconfig"
@@ -27,6 +27,7 @@ obj-$(CONFIG_SSFDC) += ssfdc.o
obj-$(CONFIG_SM_FTL) += sm_ftl.o
obj-$(CONFIG_MTD_OOPS) += mtdoops.o
obj-$(CONFIG_MTD_SWAP) += mtdswap.o
+obj-$(CONFIG_MTD_VIRT_CONCAT) += mtd_virt_concat.o
nftl-objs := nftlcore.o nftlmount.o
inftl-objs := inftlcore.o inftlmount.o
new file mode 100644
@@ -0,0 +1,132 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Virtual concat MTD device driver
+ *
+ * Copyright (C) 2018 Bernhard Frauendienst
+ * Author: Bernhard Frauendienst <kernel@nospam.obeliks.de>
+ */
+
+#include <linux/module.h>
+#include <linux/device.h>
+#include <linux/mtd/concat.h>
+#include <linux/mtd/mtd.h>
+#include <linux/mtd/partitions.h>
+#include <linux/of.h>
+#include <linux/of_platform.h>
+#include <linux/slab.h>
+
+/**
+ * struct mtd_virt_concat - platform device driver data.
+ * @vmtd: Virtual mtd_concat device
+ * @count: Number of physical underlaying devices in @devices
+ * @devices: Array of the physical devices used
+ */
+struct mtd_virt_concat {
+ struct mtd_info *vmtd;
+ unsigned int count;
+ struct mtd_info **devices;
+};
+
+static void mtd_virt_concat_put_devices(struct mtd_virt_concat *concat)
+{
+ int i;
+
+ for (i = 0; i < concat->count; i++)
+ put_mtd_device(concat->devices[i]);
+}
+
+static int mtd_virt_concat_probe(struct platform_device *pdev)
+{
+ struct device *dev = &pdev->dev;
+ struct device_node *node = dev->of_node;
+ struct mtd_virt_concat *concat;
+ struct of_phandle_iterator it;
+ struct mtd_info *mtd;
+ int ret, count;
+
+ count = of_count_phandle_with_args(node, "devices", NULL);
+ if (count < 2) {
+ dev_err(dev, "minimum 2 devices, given: %d\n", count);
+ return -EINVAL;
+ }
+
+ concat = devm_kzalloc(dev, sizeof(*concat), GFP_KERNEL);
+ if (!concat)
+ return -ENOMEM;
+
+ platform_set_drvdata(pdev, concat);
+
+ concat->devices = devm_kcalloc(dev, count, sizeof(*concat->devices),
+ GFP_KERNEL);
+ if (!concat->devices)
+ return -ENOMEM;
+
+ /* Aggregate the physical devices */
+ of_for_each_phandle(&it, ret, node, "devices", NULL, 0) {
+ mtd = get_mtd_device_by_node(it.node);
+ if (IS_ERR(mtd)) {
+ ret = -EPROBE_DEFER;
+ goto put_mtd_devices;
+ }
+
+ concat->devices[concat->count++] = mtd;
+ }
+
+ /* Create the virtual device */
+ concat->vmtd = mtd_concat_create(concat->devices, concat->count,
+ dev_name(dev));
+ if (!concat->vmtd) {
+ ret = -ENXIO;
+ goto put_mtd_devices;
+ }
+
+ concat->vmtd->dev.parent = dev;
+ mtd_set_of_node(concat->vmtd, node);
+
+ /* Register the platform device */
+ ret = mtd_device_register(concat->vmtd, NULL, 0);
+ if (ret)
+ goto destroy_concat;
+
+ return 0;
+
+destroy_concat:
+ mtd_concat_destroy(concat->vmtd);
+put_mtd_devices:
+ mtd_virt_concat_put_devices(concat);
+
+ return ret;
+}
+
+static int mtd_virt_concat_remove(struct platform_device *pdev)
+{
+ struct mtd_virt_concat *concat = platform_get_drvdata(pdev);
+
+ mtd_device_unregister(concat->vmtd);
+ mtd_concat_destroy(concat->vmtd);
+ mtd_virt_concat_put_devices(concat);
+
+ return 0;
+}
+
+static const struct of_device_id mtd_virt_concat_of_match[] = {
+ {
+ .compatible = "mtd-concat",
+ },
+ { /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(of, mtd_virt_concat_of_match);
+
+static struct platform_driver mtd_virt_concat_driver = {
+ .probe = mtd_virt_concat_probe,
+ .remove = mtd_virt_concat_remove,
+ .driver = {
+ .name = "mtd-virt-concat",
+ .of_match_table = mtd_virt_concat_of_match,
+ },
+};
+module_platform_driver(mtd_virt_concat_driver);
+
+MODULE_LICENSE("GPL v2");
+MODULE_AUTHOR("Bernhard Frauendienst <kernel@nospam.obeliks.de>");
+MODULE_DESCRIPTION("Virtual concat MTD device driver");