1 From 5b644aa012f67fd211138a067b9f351f30bdcc60 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
3 Date: Wed, 14 Mar 2018 13:10:42 +0100
4 Subject: [PATCH] mtd: partitions: add of_match_table parser matching for the
7 Content-Type: text/plain; charset=UTF-8
8 Content-Transfer-Encoding: 8bit
10 In order to properly support compatibility strings as described in the
11 bindings/mtd/partition.txt "ofpart" type should be treated as an
12 indication for looking into OF. MTD should check "compatible" property
13 and search for a matching parser rather than blindly trying the one
14 supporting "fixed-partitions".
16 It also means that existing "fixed-partitions" parser should get renamed
17 to use a more meaningful name.
19 This commit achievies that aim by introducing a new mtd_part_of_parse().
20 It works by looking for a matching parser for every string in the
21 "compatibility" property (starting with the most specific one).
23 Please note that driver-specified parsers still take a precedence. It's
24 assumed that driver providing a parser type has a good reason for that
25 (e.g. having platform data with device-specific info). Also doing
26 otherwise could break existing setups. The same applies to using default
27 parsers (including "cmdlinepart") as some overwrite DT data with cmdline
30 Partition parsers can now provide an of_match_table to enable
31 flash<-->parser matching via device tree as documented in the
34 This support is currently limited to built-in parsers as it uses
35 request_module() and friends. This should be sufficient for most cases
36 though as compiling parsers as modules isn't a common choice.
38 Signed-off-by: Brian Norris <computersforpeace@gmail.com>
39 Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
40 Tested-by: Peter Rosin <peda@axentia.se>
41 Reviewed-by: Richard Weinberger <richard@nod.at>
42 Signed-off-by: Boris Brezillon <boris.brezillon@bootlin.com>
44 drivers/mtd/mtdpart.c | 116 +++++++++++++++++++++++++++++++++++++----
45 include/linux/mtd/partitions.h | 1 +
46 2 files changed, 108 insertions(+), 9 deletions(-)
48 --- a/drivers/mtd/mtdpart.c
49 +++ b/drivers/mtd/mtdpart.c
51 #include <linux/mtd/mtd.h>
52 #include <linux/mtd/partitions.h>
53 #include <linux/err.h>
54 +#include <linux/of.h>
58 @@ -919,6 +920,92 @@ static int mtd_part_do_parse(struct mtd_
62 + * mtd_part_get_compatible_parser - find MTD parser by a compatible string
64 + * @compat: compatible string describing partitions in a device tree
66 + * MTD parsers can specify supported partitions by providing a table of
67 + * compatibility strings. This function finds a parser that advertises support
68 + * for a passed value of "compatible".
70 +static struct mtd_part_parser *mtd_part_get_compatible_parser(const char *compat)
72 + struct mtd_part_parser *p, *ret = NULL;
74 + spin_lock(&part_parser_lock);
76 + list_for_each_entry(p, &part_parsers, list) {
77 + const struct of_device_id *matches;
79 + matches = p->of_match_table;
83 + for (; matches->compatible[0]; matches++) {
84 + if (!strcmp(matches->compatible, compat) &&
85 + try_module_get(p->owner)) {
95 + spin_unlock(&part_parser_lock);
100 +static int mtd_part_of_parse(struct mtd_info *master,
101 + struct mtd_partitions *pparts)
103 + struct mtd_part_parser *parser;
104 + struct device_node *np;
105 + struct property *prop;
106 + const char *compat;
107 + const char *fixed = "ofpart";
110 + np = of_get_child_by_name(mtd_get_of_node(master), "partitions");
111 + of_property_for_each_string(np, "compatible", prop, compat) {
112 + parser = mtd_part_get_compatible_parser(compat);
115 + ret = mtd_part_do_parse(parser, master, pparts, NULL);
120 + mtd_part_parser_put(parser);
121 + if (ret < 0 && !err)
127 + * For backward compatibility we have to try the "ofpart"
128 + * parser. It supports old DT format with partitions specified as a
129 + * direct subnodes of a flash device DT node without any compatibility
130 + * specified we could match.
132 + parser = mtd_part_parser_get(fixed);
133 + if (!parser && !request_module("%s", fixed))
134 + parser = mtd_part_parser_get(fixed);
136 + ret = mtd_part_do_parse(parser, master, pparts, NULL);
139 + mtd_part_parser_put(parser);
140 + if (ret < 0 && !err)
148 * parse_mtd_partitions - parse MTD partitions
149 * @master: the master partition (describes whole MTD device)
150 * @types: names of partition parsers to try or %NULL
151 @@ -950,19 +1037,30 @@ int parse_mtd_partitions(struct mtd_info
152 types = default_mtd_part_types;
154 for ( ; *types; types++) {
155 - pr_debug("%s: parsing partitions %s\n", master->name, *types);
156 - parser = mtd_part_parser_get(*types);
157 - if (!parser && !request_module("%s", *types))
159 + * ofpart is a special type that means OF partitioning info
160 + * should be used. It requires a bit different logic so it is
161 + * handled in a separated function.
163 + if (!strcmp(*types, "ofpart")) {
164 + ret = mtd_part_of_parse(master, pparts);
166 + pr_debug("%s: parsing partitions %s\n", master->name,
168 parser = mtd_part_parser_get(*types);
169 - pr_debug("%s: got parser %s\n", master->name,
170 - parser ? parser->name : NULL);
173 - ret = mtd_part_do_parse(parser, master, pparts, data);
174 + if (!parser && !request_module("%s", *types))
175 + parser = mtd_part_parser_get(*types);
176 + pr_debug("%s: got parser %s\n", master->name,
177 + parser ? parser->name : NULL);
180 + ret = mtd_part_do_parse(parser, master, pparts, data);
182 + mtd_part_parser_put(parser);
184 /* Found partitions! */
187 - mtd_part_parser_put(parser);
189 * Stash the first error we see; only report it if no parser
191 --- a/include/linux/mtd/partitions.h
192 +++ b/include/linux/mtd/partitions.h
193 @@ -77,6 +77,7 @@ struct mtd_part_parser {
194 struct list_head list;
195 struct module *owner;
197 + const struct of_device_id *of_match_table;
198 int (*parse_fn)(struct mtd_info *, const struct mtd_partition **,
199 struct mtd_part_parser_data *);
200 void (*cleanup)(const struct mtd_partition *pparts, int nr_parts);