06931ce2a49785f4dda0d0a3f8deddafe74019db
[openwrt/staging/blogic.git] /
1 From bb2192123ec70470d6ea33f138846b175403a968 Mon Sep 17 00:00:00 2001
2 From: Brian Norris <computersforpeace@gmail.com>
3 Date: Thu, 4 Jan 2018 08:05:33 +0100
4 Subject: [PATCH] mtd: partitions: add of_match_table parser matching
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 Partition parsers can now provide an of_match_table to enable
10 flash<-->parser matching via device tree as documented in the
11 mtd/partition.txt.
12
13 It works by looking for a matching parser for every string in the
14 "compatibility" property (starting with the most specific one).
15
16 This support is currently limited to built-in parsers as it uses
17 request_module() and friends. This should be sufficient for most cases
18 though as compiling parsers as modules isn't a common choice.
19
20 Signed-off-by: Brian Norris <computersforpeace@gmail.com>
21 Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
22 Signed-off-by: Boris Brezillon <boris.brezillon@free-electrons.com>
23 ---
24 drivers/mtd/mtdpart.c | 59 ++++++++++++++++++++++++++++++++++++++++++
25 include/linux/mtd/partitions.h | 1 +
26 2 files changed, 60 insertions(+)
27
28 --- a/drivers/mtd/mtdpart.c
29 +++ b/drivers/mtd/mtdpart.c
30 @@ -30,6 +30,7 @@
31 #include <linux/mtd/mtd.h>
32 #include <linux/mtd/partitions.h>
33 #include <linux/err.h>
34 +#include <linux/of.h>
35
36 #include "mtdcore.h"
37
38 @@ -886,6 +887,45 @@ static int mtd_part_do_parse(struct mtd_
39 }
40
41 /**
42 + * mtd_part_get_compatible_parser - find MTD parser by a compatible string
43 + *
44 + * @compat: compatible string describing partitions in a device tree
45 + *
46 + * MTD parsers can specify supported partitions by providing a table of
47 + * compatibility strings. This function finds a parser that advertises support
48 + * for a passed value of "compatible".
49 + */
50 +static struct mtd_part_parser *mtd_part_get_compatible_parser(const char *compat)
51 +{
52 + struct mtd_part_parser *p, *ret = NULL;
53 +
54 + spin_lock(&part_parser_lock);
55 +
56 + list_for_each_entry(p, &part_parsers, list) {
57 + const struct of_device_id *matches;
58 +
59 + matches = p->of_match_table;
60 + if (!matches)
61 + continue;
62 +
63 + for (; matches->compatible[0]; matches++) {
64 + if (!strcmp(matches->compatible, compat) &&
65 + try_module_get(p->owner)) {
66 + ret = p;
67 + break;
68 + }
69 + }
70 +
71 + if (ret)
72 + break;
73 + }
74 +
75 + spin_unlock(&part_parser_lock);
76 +
77 + return ret;
78 +}
79 +
80 +/**
81 * parse_mtd_partitions - parse MTD partitions
82 * @master: the master partition (describes whole MTD device)
83 * @types: names of partition parsers to try or %NULL
84 @@ -911,8 +951,27 @@ int parse_mtd_partitions(struct mtd_info
85 struct mtd_part_parser_data *data)
86 {
87 struct mtd_part_parser *parser;
88 + struct device_node *np;
89 + struct property *prop;
90 + const char *compat;
91 int ret, err = 0;
92
93 + np = of_get_child_by_name(mtd_get_of_node(master), "partitions");
94 + of_property_for_each_string(np, "compatible", prop, compat) {
95 + parser = mtd_part_get_compatible_parser(compat);
96 + if (!parser)
97 + continue;
98 + ret = mtd_part_do_parse(parser, master, pparts, data);
99 + if (ret > 0) {
100 + of_node_put(np);
101 + return 0;
102 + }
103 + mtd_part_parser_put(parser);
104 + if (ret < 0 && !err)
105 + err = ret;
106 + }
107 + of_node_put(np);
108 +
109 if (!types)
110 types = default_mtd_part_types;
111
112 --- a/include/linux/mtd/partitions.h
113 +++ b/include/linux/mtd/partitions.h
114 @@ -77,6 +77,7 @@ struct mtd_part_parser {
115 struct list_head list;
116 struct module *owner;
117 const char *name;
118 + const struct of_device_id *of_match_table;
119 int (*parse_fn)(struct mtd_info *, const struct mtd_partition **,
120 struct mtd_part_parser_data *);
121 void (*cleanup)(const struct mtd_partition *pparts, int nr_parts);