1 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
2 Date: Tue, 30 Jan 2018 11:55:16 +0100
3 Subject: [PATCH V10 1/3] mtd: partitions: add of_match_table parser matching
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 In order to properly support compatibility strings as described in the
10 bindings/mtd/partition.txt "ofpart" type should be treated as an
11 indication for looking into OF. MTD should check "compatible" property
12 and search for a matching parser rather than blindly trying the one
13 supporting "fixed-partitions".
15 It also means that existing "fixed-partitions" parser should get renamed
16 to use a more meaningful name.
18 This commit achievies that aim by introducing a new mtd_part_of_parse().
19 It works by looking for a matching parser for every string in the
20 "compatibility" property (starting with the most specific one).
22 Please note that driver-specified parsers still take a precedence. It's
23 assumed that driver providing a parser type has a good reason for that
24 (e.g. having platform data with device-specific info). Also doing
25 otherwise could break existing setups. The same applies to using default
26 parsers (including "cmdlinepart") as some overwrite DT data with cmdline
29 Partition parsers can now provide an of_match_table to enable
30 flash<-->parser matching via device tree as documented in the
33 This support is currently limited to built-in parsers as it uses
34 request_module() and friends. This should be sufficient for most cases
35 though as compiling parsers as modules isn't a common choice.
37 Signed-off-by: Brian Norris <computersforpeace@gmail.com>
38 Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
39 Tested-by: Peter Rosin <peda@axentia.se>
42 --- a/drivers/mtd/mtdpart.c
43 +++ b/drivers/mtd/mtdpart.c
45 #include <linux/mtd/mtd.h>
46 #include <linux/mtd/partitions.h>
47 #include <linux/err.h>
48 +#include <linux/of.h>
52 @@ -886,6 +887,92 @@ static int mtd_part_do_parse(struct mtd_
56 + * mtd_part_get_compatible_parser - find MTD parser by a compatible string
58 + * @compat: compatible string describing partitions in a device tree
60 + * MTD parsers can specify supported partitions by providing a table of
61 + * compatibility strings. This function finds a parser that advertises support
62 + * for a passed value of "compatible".
64 +static struct mtd_part_parser *mtd_part_get_compatible_parser(const char *compat)
66 + struct mtd_part_parser *p, *ret = NULL;
68 + spin_lock(&part_parser_lock);
70 + list_for_each_entry(p, &part_parsers, list) {
71 + const struct of_device_id *matches;
73 + matches = p->of_match_table;
77 + for (; matches->compatible[0]; matches++) {
78 + if (!strcmp(matches->compatible, compat) &&
79 + try_module_get(p->owner)) {
89 + spin_unlock(&part_parser_lock);
94 +static int mtd_part_of_parse(struct mtd_info *master,
95 + struct mtd_partitions *pparts)
97 + struct mtd_part_parser *parser;
98 + struct device_node *np;
99 + struct property *prop;
100 + const char *compat;
101 + const char *fixed = "ofpart";
104 + np = of_get_child_by_name(mtd_get_of_node(master), "partitions");
105 + of_property_for_each_string(np, "compatible", prop, compat) {
106 + parser = mtd_part_get_compatible_parser(compat);
109 + ret = mtd_part_do_parse(parser, master, pparts, NULL);
114 + mtd_part_parser_put(parser);
115 + if (ret < 0 && !err)
121 + * For backward compatibility we have to try the "ofpart"
122 + * parser. It supports old DT format with partitions specified as a
123 + * direct subnodes of a flash device DT node without any compatibility
124 + * specified we could match.
126 + parser = mtd_part_parser_get(fixed);
127 + if (!parser && !request_module("%s", fixed))
128 + parser = mtd_part_parser_get(fixed);
130 + ret = mtd_part_do_parse(parser, master, pparts, NULL);
133 + mtd_part_parser_put(parser);
134 + if (ret < 0 && !err)
142 * parse_mtd_partitions - parse MTD partitions
143 * @master: the master partition (describes whole MTD device)
144 * @types: names of partition parsers to try or %NULL
145 @@ -917,19 +1004,30 @@ int parse_mtd_partitions(struct mtd_info
146 types = default_mtd_part_types;
148 for ( ; *types; types++) {
149 - pr_debug("%s: parsing partitions %s\n", master->name, *types);
150 - parser = mtd_part_parser_get(*types);
151 - if (!parser && !request_module("%s", *types))
153 + * ofpart is a special type that means OF partitioning info
154 + * should be used. It requires a bit different logic so it is
155 + * handled in a separated function.
157 + if (!strcmp(*types, "ofpart")) {
158 + ret = mtd_part_of_parse(master, pparts);
160 + pr_debug("%s: parsing partitions %s\n", master->name,
162 parser = mtd_part_parser_get(*types);
163 - pr_debug("%s: got parser %s\n", master->name,
164 - parser ? parser->name : NULL);
167 - ret = mtd_part_do_parse(parser, master, pparts, data);
168 + if (!parser && !request_module("%s", *types))
169 + parser = mtd_part_parser_get(*types);
170 + pr_debug("%s: got parser %s\n", master->name,
171 + parser ? parser->name : NULL);
174 + ret = mtd_part_do_parse(parser, master, pparts, data);
176 + mtd_part_parser_put(parser);
178 /* Found partitions! */
181 - mtd_part_parser_put(parser);
183 * Stash the first error we see; only report it if no parser
185 --- a/include/linux/mtd/partitions.h
186 +++ b/include/linux/mtd/partitions.h
187 @@ -77,6 +77,7 @@ struct mtd_part_parser {
188 struct list_head list;
189 struct module *owner;
191 + const struct of_device_id *of_match_table;
192 int (*parse_fn)(struct mtd_info *, const struct mtd_partition **,
193 struct mtd_part_parser_data *);
194 void (*cleanup)(const struct mtd_partition *pparts, int nr_parts);