3334203d8ad26e138612457d63313dfe9e04b807
[openwrt/staging/ynezz.git] /
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
4 for the "ofpart" type
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
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".
14
15 It also means that existing "fixed-partitions" parser should get renamed
16 to use a more meaningful name.
17
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).
21
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
27 argument.
28
29 Partition parsers can now provide an of_match_table to enable
30 flash<-->parser matching via device tree as documented in the
31 mtd/partition.txt.
32
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.
36
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>
40 ---
41
42 --- a/drivers/mtd/mtdpart.c
43 +++ b/drivers/mtd/mtdpart.c
44 @@ -30,6 +30,7 @@
45 #include <linux/mtd/mtd.h>
46 #include <linux/mtd/partitions.h>
47 #include <linux/err.h>
48 +#include <linux/of.h>
49
50 #include "mtdcore.h"
51
52 @@ -894,6 +895,92 @@ static int mtd_part_do_parse(struct mtd_
53 }
54
55 /**
56 + * mtd_part_get_compatible_parser - find MTD parser by a compatible string
57 + *
58 + * @compat: compatible string describing partitions in a device tree
59 + *
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".
63 + */
64 +static struct mtd_part_parser *mtd_part_get_compatible_parser(const char *compat)
65 +{
66 + struct mtd_part_parser *p, *ret = NULL;
67 +
68 + spin_lock(&part_parser_lock);
69 +
70 + list_for_each_entry(p, &part_parsers, list) {
71 + const struct of_device_id *matches;
72 +
73 + matches = p->of_match_table;
74 + if (!matches)
75 + continue;
76 +
77 + for (; matches->compatible[0]; matches++) {
78 + if (!strcmp(matches->compatible, compat) &&
79 + try_module_get(p->owner)) {
80 + ret = p;
81 + break;
82 + }
83 + }
84 +
85 + if (ret)
86 + break;
87 + }
88 +
89 + spin_unlock(&part_parser_lock);
90 +
91 + return ret;
92 +}
93 +
94 +static int mtd_part_of_parse(struct mtd_info *master,
95 + struct mtd_partitions *pparts)
96 +{
97 + struct mtd_part_parser *parser;
98 + struct device_node *np;
99 + struct property *prop;
100 + const char *compat;
101 + const char *fixed = "ofpart";
102 + int ret, err = 0;
103 +
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);
107 + if (!parser)
108 + continue;
109 + ret = mtd_part_do_parse(parser, master, pparts, NULL);
110 + if (ret > 0) {
111 + of_node_put(np);
112 + return ret;
113 + }
114 + mtd_part_parser_put(parser);
115 + if (ret < 0 && !err)
116 + err = ret;
117 + }
118 + of_node_put(np);
119 +
120 + /*
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.
125 + */
126 + parser = mtd_part_parser_get(fixed);
127 + if (!parser && !request_module("%s", fixed))
128 + parser = mtd_part_parser_get(fixed);
129 + if (parser) {
130 + ret = mtd_part_do_parse(parser, master, pparts, NULL);
131 + if (ret > 0)
132 + return ret;
133 + mtd_part_parser_put(parser);
134 + if (ret < 0 && !err)
135 + err = ret;
136 + }
137 +
138 + return err;
139 +}
140 +
141 +/**
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 @@ -925,19 +1012,30 @@ int parse_mtd_partitions(struct mtd_info
146 types = default_mtd_part_types;
147
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))
152 + /*
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.
156 + */
157 + if (!strcmp(*types, "ofpart")) {
158 + ret = mtd_part_of_parse(master, pparts);
159 + } else {
160 + pr_debug("%s: parsing partitions %s\n", master->name,
161 + *types);
162 parser = mtd_part_parser_get(*types);
163 - pr_debug("%s: got parser %s\n", master->name,
164 - parser ? parser->name : NULL);
165 - if (!parser)
166 - continue;
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);
172 + if (!parser)
173 + continue;
174 + ret = mtd_part_do_parse(parser, master, pparts, data);
175 + if (ret <= 0)
176 + mtd_part_parser_put(parser);
177 + }
178 /* Found partitions! */
179 if (ret > 0)
180 return 0;
181 - mtd_part_parser_put(parser);
182 /*
183 * Stash the first error we see; only report it if no parser
184 * succeeds
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;
190 const char *name;
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);