1
0

497-mtd-mtdconcat-add-dt-driver-for-concat-devices.patch 5.7 KB

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