mtd: ofpart: don't complain about missing 'partitions' node too loudly
[firefly-linux-kernel-4.4.55.git] / drivers / mtd / ofpart.c
1 /*
2  * Flash partitions described by the OF (or flattened) device tree
3  *
4  * Copyright © 2006 MontaVista Software Inc.
5  * Author: Vitaly Wool <vwool@ru.mvista.com>
6  *
7  * Revised to handle newer style flash binding by:
8  *   Copyright © 2007 David Gibson, IBM Corporation.
9  *
10  * This program is free software; you can redistribute  it and/or modify it
11  * under  the terms of  the GNU General  Public License as published by the
12  * Free Software Foundation;  either version 2 of the  License, or (at your
13  * option) any later version.
14  */
15
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/of.h>
19 #include <linux/mtd/mtd.h>
20 #include <linux/slab.h>
21 #include <linux/mtd/partitions.h>
22
23 static bool node_has_compatible(struct device_node *pp)
24 {
25         return of_get_property(pp, "compatible", NULL);
26 }
27
28 static int parse_ofpart_partitions(struct mtd_info *master,
29                                    struct mtd_partition **pparts,
30                                    struct mtd_part_parser_data *data)
31 {
32         struct device_node *mtd_node;
33         struct device_node *ofpart_node;
34         const char *partname;
35         struct device_node *pp;
36         int nr_parts, i, ret = 0;
37         bool dedicated = true;
38
39
40         if (!data)
41                 return 0;
42
43         mtd_node = data->of_node;
44         if (!mtd_node)
45                 return 0;
46
47         ofpart_node = of_get_child_by_name(mtd_node, "partitions");
48         if (!ofpart_node) {
49                 /*
50                  * We might get here even when ofpart isn't used at all (e.g.,
51                  * when using another parser), so don't be louder than
52                  * KERN_DEBUG
53                  */
54                 pr_debug("%s: 'partitions' subnode not found on %s. Trying to parse direct subnodes as partitions.\n",
55                          master->name, mtd_node->full_name);
56                 ofpart_node = mtd_node;
57                 dedicated = false;
58         }
59
60         /* First count the subnodes */
61         nr_parts = 0;
62         for_each_child_of_node(ofpart_node,  pp) {
63                 if (!dedicated && node_has_compatible(pp))
64                         continue;
65
66                 nr_parts++;
67         }
68
69         if (nr_parts == 0)
70                 return 0;
71
72         *pparts = kzalloc(nr_parts * sizeof(**pparts), GFP_KERNEL);
73         if (!*pparts)
74                 return -ENOMEM;
75
76         i = 0;
77         for_each_child_of_node(ofpart_node,  pp) {
78                 const __be32 *reg;
79                 int len;
80                 int a_cells, s_cells;
81
82                 if (!dedicated && node_has_compatible(pp))
83                         continue;
84
85                 reg = of_get_property(pp, "reg", &len);
86                 if (!reg) {
87                         if (dedicated) {
88                                 pr_debug("%s: ofpart partition %s (%s) missing reg property.\n",
89                                          master->name, pp->full_name,
90                                          mtd_node->full_name);
91                                 goto ofpart_fail;
92                         } else {
93                                 nr_parts--;
94                                 continue;
95                         }
96                 }
97
98                 a_cells = of_n_addr_cells(pp);
99                 s_cells = of_n_size_cells(pp);
100                 if (len / 4 != a_cells + s_cells) {
101                         pr_debug("%s: ofpart partition %s (%s) error parsing reg property.\n",
102                                  master->name, pp->full_name,
103                                  mtd_node->full_name);
104                         goto ofpart_fail;
105                 }
106
107                 (*pparts)[i].offset = of_read_number(reg, a_cells);
108                 (*pparts)[i].size = of_read_number(reg + a_cells, s_cells);
109
110                 partname = of_get_property(pp, "label", &len);
111                 if (!partname)
112                         partname = of_get_property(pp, "name", &len);
113                 (*pparts)[i].name = partname;
114
115                 if (of_get_property(pp, "read-only", &len))
116                         (*pparts)[i].mask_flags |= MTD_WRITEABLE;
117
118                 if (of_get_property(pp, "lock", &len))
119                         (*pparts)[i].mask_flags |= MTD_POWERUP_LOCK;
120
121                 i++;
122         }
123
124         if (!nr_parts)
125                 goto ofpart_none;
126
127         return nr_parts;
128
129 ofpart_fail:
130         pr_err("%s: error parsing ofpart partition %s (%s)\n",
131                master->name, pp->full_name, mtd_node->full_name);
132         ret = -EINVAL;
133 ofpart_none:
134         of_node_put(pp);
135         kfree(*pparts);
136         *pparts = NULL;
137         return ret;
138 }
139
140 static struct mtd_part_parser ofpart_parser = {
141         .owner = THIS_MODULE,
142         .parse_fn = parse_ofpart_partitions,
143         .name = "ofpart",
144 };
145
146 static int parse_ofoldpart_partitions(struct mtd_info *master,
147                                       struct mtd_partition **pparts,
148                                       struct mtd_part_parser_data *data)
149 {
150         struct device_node *dp;
151         int i, plen, nr_parts;
152         const struct {
153                 __be32 offset, len;
154         } *part;
155         const char *names;
156
157         if (!data)
158                 return 0;
159
160         dp = data->of_node;
161         if (!dp)
162                 return 0;
163
164         part = of_get_property(dp, "partitions", &plen);
165         if (!part)
166                 return 0; /* No partitions found */
167
168         pr_warning("Device tree uses obsolete partition map binding: %s\n",
169                         dp->full_name);
170
171         nr_parts = plen / sizeof(part[0]);
172
173         *pparts = kzalloc(nr_parts * sizeof(*(*pparts)), GFP_KERNEL);
174         if (!*pparts)
175                 return -ENOMEM;
176
177         names = of_get_property(dp, "partition-names", &plen);
178
179         for (i = 0; i < nr_parts; i++) {
180                 (*pparts)[i].offset = be32_to_cpu(part->offset);
181                 (*pparts)[i].size   = be32_to_cpu(part->len) & ~1;
182                 /* bit 0 set signifies read only partition */
183                 if (be32_to_cpu(part->len) & 1)
184                         (*pparts)[i].mask_flags = MTD_WRITEABLE;
185
186                 if (names && (plen > 0)) {
187                         int len = strlen(names) + 1;
188
189                         (*pparts)[i].name = names;
190                         plen -= len;
191                         names += len;
192                 } else {
193                         (*pparts)[i].name = "unnamed";
194                 }
195
196                 part++;
197         }
198
199         return nr_parts;
200 }
201
202 static struct mtd_part_parser ofoldpart_parser = {
203         .owner = THIS_MODULE,
204         .parse_fn = parse_ofoldpart_partitions,
205         .name = "ofoldpart",
206 };
207
208 static int __init ofpart_parser_init(void)
209 {
210         register_mtd_parser(&ofpart_parser);
211         register_mtd_parser(&ofoldpart_parser);
212         return 0;
213 }
214
215 static void __exit ofpart_parser_exit(void)
216 {
217         deregister_mtd_parser(&ofpart_parser);
218         deregister_mtd_parser(&ofoldpart_parser);
219 }
220
221 module_init(ofpart_parser_init);
222 module_exit(ofpart_parser_exit);
223
224 MODULE_LICENSE("GPL");
225 MODULE_DESCRIPTION("Parser for MTD partitioning information in device tree");
226 MODULE_AUTHOR("Vitaly Wool, David Gibson");
227 /*
228  * When MTD core cannot find the requested parser, it tries to load the module
229  * with the same name. Since we provide the ofoldpart parser, we should have
230  * the corresponding alias.
231  */
232 MODULE_ALIAS("ofoldpart");