421-drivers-mtd-parsers-add-nvmem-support-to-cmdlinepart.patch 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. From 6fa9e3678eb002246df1280322b6a024853950a5 Mon Sep 17 00:00:00 2001
  2. From: Ansuel Smith <ansuelsmth@gmail.com>
  3. Date: Mon, 11 Oct 2021 00:53:14 +0200
  4. Subject: [PATCH] drivers: mtd: parsers: add nvmem support to cmdlinepart
  5. Assuming cmdlinepart is only one level deep partition scheme and that
  6. static partition are also defined in DTS, we can assign an of_node for
  7. partition declared from bootargs. cmdlinepart have priority than
  8. fiexed-partition parser so in this specific case the parser doesn't
  9. assign an of_node. Fix this by searching a defined of_node using a
  10. similar fixed_partition parser and if a partition is found with the same
  11. label, check that it has the same offset and size and return the DT
  12. of_node to correctly use NVMEM cells.
  13. Signed-off-by: Ansuel Smith <ansuelsmth@gmail.com>
  14. ---
  15. drivers/mtd/parsers/cmdlinepart.c | 71 +++++++++++++++++++++++++++++++
  16. 1 file changed, 71 insertions(+)
  17. --- a/drivers/mtd/parsers/cmdlinepart.c
  18. +++ b/drivers/mtd/parsers/cmdlinepart.c
  19. @@ -43,6 +43,7 @@
  20. #include <linux/mtd/partitions.h>
  21. #include <linux/module.h>
  22. #include <linux/err.h>
  23. +#include <linux/of.h>
  24. /* debug macro */
  25. #if 0
  26. @@ -323,6 +324,68 @@ static int mtdpart_setup_real(char *s)
  27. return 0;
  28. }
  29. +static int search_fixed_partition(struct mtd_info *master,
  30. + struct mtd_partition *target_part,
  31. + struct mtd_partition *fixed_part)
  32. +{
  33. + struct device_node *mtd_node;
  34. + struct device_node *ofpart_node;
  35. + struct device_node *pp;
  36. + struct mtd_partition part;
  37. + const char *partname;
  38. +
  39. + mtd_node = mtd_get_of_node(master);
  40. + if (!mtd_node)
  41. + return -EINVAL;
  42. +
  43. + ofpart_node = of_get_child_by_name(mtd_node, "partitions");
  44. +
  45. + for_each_child_of_node(ofpart_node, pp) {
  46. + const __be32 *reg;
  47. + int len;
  48. + int a_cells, s_cells;
  49. +
  50. + reg = of_get_property(pp, "reg", &len);
  51. + if (!reg) {
  52. + pr_debug("%s: ofpart partition %pOF (%pOF) missing reg property.\n",
  53. + master->name, pp,
  54. + mtd_node);
  55. + continue;
  56. + }
  57. +
  58. + a_cells = of_n_addr_cells(pp);
  59. + s_cells = of_n_size_cells(pp);
  60. + if (len / 4 != a_cells + s_cells) {
  61. + pr_debug("%s: ofpart partition %pOF (%pOF) error parsing reg property.\n",
  62. + master->name, pp,
  63. + mtd_node);
  64. + continue;
  65. + }
  66. +
  67. + part.offset = of_read_number(reg, a_cells);
  68. + part.size = of_read_number(reg + a_cells, s_cells);
  69. + part.of_node = pp;
  70. +
  71. + partname = of_get_property(pp, "label", &len);
  72. + if (!partname)
  73. + partname = of_get_property(pp, "name", &len);
  74. + part.name = partname;
  75. +
  76. + if (!strncmp(target_part->name, part.name, len)) {
  77. + if (part.offset != target_part->offset)
  78. + return -EINVAL;
  79. +
  80. + if (part.size != target_part->size)
  81. + return -EINVAL;
  82. +
  83. + memcpy(fixed_part, &part, sizeof(struct mtd_partition));
  84. + return 0;
  85. + }
  86. + }
  87. +
  88. + return -EINVAL;
  89. +}
  90. +
  91. /*
  92. * Main function to be called from the MTD mapping driver/device to
  93. * obtain the partitioning information. At this point the command line
  94. @@ -338,6 +401,7 @@ static int parse_cmdline_partitions(stru
  95. int i, err;
  96. struct cmdline_mtd_partition *part;
  97. const char *mtd_id = master->name;
  98. + struct mtd_partition fixed_part;
  99. /* parse command line */
  100. if (!cmdline_parsed) {
  101. @@ -382,6 +446,13 @@ static int parse_cmdline_partitions(stru
  102. sizeof(*part->parts) * (part->num_parts - i));
  103. i--;
  104. }
  105. +
  106. + err = search_fixed_partition(master, &part->parts[i], &fixed_part);
  107. + if (!err) {
  108. + part->parts[i].of_node = fixed_part.of_node;
  109. + pr_info("Found partition defined in DT for %s. Assigning OF node to support nvmem.",
  110. + part->parts[i].name);
  111. + }
  112. }
  113. *pparts = kmemdup(part->parts, sizeof(*part->parts) * part->num_parts,