112-mtd-add-dt-nand-partition-parser.patch 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. From 0460e9868fd82a3675db02f6ceb6edfd8501c194 Mon Sep 17 00:00:00 2001
  2. From: Boris BREZILLON <boris.brezillon@free-electrons.com>
  3. Date: Mon, 28 Jul 2014 14:31:42 +0200
  4. Subject: [PATCH] mtd: nand: Add DT NAND partition parser
  5. Add a of_nandpart_parse function to help parsing NAND partitions from DT.
  6. This function should be called from NAND controller drivers just after the
  7. nand_scan_tail in place of mtd_device_parse_register.
  8. The caller can specify a parser function to retrieve HW specific
  9. informations from the DT.
  10. Signed-off-by: Boris BREZILLON <boris.brezillon@free-electrons.com>
  11. Signed-off-by: Hans de Goede <hdegoede@redhat.com>
  12. ---
  13. drivers/mtd/nand/ofnandpart.c | 104 ++++++++++++++++++++++++++++++++++++++++++
  14. include/linux/mtd/nand.h | 17 +++++++
  15. 2 files changed, 121 insertions(+)
  16. create mode 100644 drivers/mtd/nand/ofnandpart.c
  17. --- /dev/null
  18. +++ b/drivers/mtd/nand/ofnandpart.c
  19. @@ -0,0 +1,104 @@
  20. +/*
  21. + * NAND Flash partitions described by the OF (or flattened) device tree
  22. + *
  23. + * Copyright © 2014 Boris BREZILLON <b.brezillon.dev@gmail.com>
  24. + *
  25. + * This program is free software; you can redistribute it and/or modify it
  26. + * under the terms of the GNU General Public License as published by the
  27. + * Free Software Foundation; either version 2 of the License, or (at your
  28. + * option) any later version.
  29. + */
  30. +
  31. +#include <linux/module.h>
  32. +#include <linux/init.h>
  33. +#include <linux/of.h>
  34. +#include <linux/mtd/mtd.h>
  35. +#include <linux/slab.h>
  36. +#include <linux/mtd/nand.h>
  37. +
  38. +static inline bool node_has_compatible(struct device_node *pp)
  39. +{
  40. + return of_get_property(pp, "compatible", NULL);
  41. +}
  42. +
  43. +int ofnandpart_parse(struct mtd_info *master,
  44. + const struct ofnandpart_data *data)
  45. +{
  46. + struct device_node *node;
  47. + const char *partname;
  48. + struct device_node *pp;
  49. + int i;
  50. +
  51. + if (!data)
  52. + return 0;
  53. +
  54. + node = data->node;
  55. + if (!node)
  56. + return 0;
  57. +
  58. + i = 0;
  59. + for_each_child_of_node(node, pp) {
  60. + const __be32 *reg;
  61. + int len;
  62. + int a_cells, s_cells;
  63. + uint64_t offset, size;
  64. + uint32_t mask_flags = 0;
  65. + struct nand_part *part;
  66. +
  67. + if (node_has_compatible(pp))
  68. + continue;
  69. +
  70. + reg = of_get_property(pp, "reg", &len);
  71. + if (!reg)
  72. + continue;
  73. +
  74. + a_cells = of_n_addr_cells(pp);
  75. + s_cells = of_n_size_cells(pp);
  76. + offset = of_read_number(reg, a_cells);
  77. + size = of_read_number(reg + a_cells, s_cells);
  78. +
  79. + partname = of_get_property(pp, "label", &len);
  80. + if (!partname)
  81. + partname = of_get_property(pp, "name", &len);
  82. +
  83. + if (of_get_property(pp, "read-only", &len))
  84. + mask_flags |= MTD_WRITEABLE;
  85. +
  86. + if (of_get_property(pp, "lock", &len))
  87. + mask_flags |= MTD_POWERUP_LOCK;
  88. +
  89. + if (data->parse)
  90. + part = data->parse(data->priv, master, pp);
  91. + else
  92. + part = nandpart_alloc();
  93. +
  94. + if (IS_ERR(part))
  95. + continue;
  96. +
  97. + part->offset = offset;
  98. + part->master = master;
  99. + part->mtd.name = partname;
  100. + part->mtd.size = size;
  101. + part->mtd.flags = mask_flags;
  102. +
  103. + if (nand_add_partition(master, part)) {
  104. + if (part->release)
  105. + part->release(part);
  106. + continue;
  107. + }
  108. +
  109. + i++;
  110. + }
  111. +
  112. + if (!i) {
  113. + of_node_put(pp);
  114. + pr_err("No valid partition found on %s\n", node->full_name);
  115. + }
  116. +
  117. + return i;
  118. +}
  119. +EXPORT_SYMBOL(ofnandpart_parse);
  120. +
  121. +MODULE_LICENSE("GPL");
  122. +MODULE_DESCRIPTION("Parser for NAND flash partitioning information in device tree");
  123. +MODULE_AUTHOR("Boris BREZILLON");
  124. --- a/include/linux/mtd/nand.h
  125. +++ b/include/linux/mtd/nand.h
  126. @@ -1014,6 +1014,23 @@ static inline int jedec_feature(struct n
  127. : 0;
  128. }
  129. +/**
  130. + * struct ofnandpart_data - struct used to retrieve NAND partitions from a DT
  131. + * node
  132. + * @parse: driver specific parser function
  133. + * @priv: driver private data
  134. + * @node: OF node containing NAND partitions
  135. + */
  136. +struct ofnandpart_data {
  137. + struct nand_part *(*parse)(void *priv, struct mtd_info *master,
  138. + struct device_node *pp);
  139. + void *priv;
  140. + struct device_node *node;
  141. +};
  142. +
  143. +int ofnandpart_parse(struct mtd_info *master,
  144. + const struct ofnandpart_data *data);
  145. +
  146. /*
  147. * struct nand_sdr_timings - SDR NAND chip timings
  148. *