]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - drivers/mtd/ofpart.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[thirdparty/kernel/stable.git] / drivers / mtd / ofpart.c
CommitLineData
9a310d21
SW
1/*
2 * Flash partitions described by the OF (or flattened) device tree
3 *
a1452a37 4 * Copyright © 2006 MontaVista Software Inc.
9a310d21
SW
5 * Author: Vitaly Wool <vwool@ru.mvista.com>
6 *
7 * Revised to handle newer style flash binding by:
a1452a37 8 * Copyright © 2007 David Gibson, IBM Corporation.
9a310d21
SW
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>
5a0e3ad6 20#include <linux/slab.h>
9a310d21
SW
21#include <linux/mtd/partitions.h>
22
e79265ba
JW
23static bool node_has_compatible(struct device_node *pp)
24{
25 return of_get_property(pp, "compatible", NULL);
26}
27
c0faf434
RM
28static int parse_fixed_partitions(struct mtd_info *master,
29 const struct mtd_partition **pparts,
30 struct mtd_part_parser_data *data)
d26c87d6 31{
c3168d26 32 struct mtd_partition *parts;
5cfdedb7
MS
33 struct device_node *mtd_node;
34 struct device_node *ofpart_node;
9a310d21
SW
35 const char *partname;
36 struct device_node *pp;
5cfdedb7
MS
37 int nr_parts, i, ret = 0;
38 bool dedicated = true;
9a310d21 39
628376fb 40
e270bca5
BN
41 /* Pull of_node from the master device node */
42 mtd_node = mtd_get_of_node(master);
5cfdedb7 43 if (!mtd_node)
628376fb
DB
44 return 0;
45
5cfdedb7
MS
46 ofpart_node = of_get_child_by_name(mtd_node, "partitions");
47 if (!ofpart_node) {
8c62b4e1
BN
48 /*
49 * We might get here even when ofpart isn't used at all (e.g.,
50 * when using another parser), so don't be louder than
51 * KERN_DEBUG
52 */
1d706077
RH
53 pr_debug("%s: 'partitions' subnode not found on %pOF. Trying to parse direct subnodes as partitions.\n",
54 master->name, mtd_node);
5cfdedb7
MS
55 ofpart_node = mtd_node;
56 dedicated = false;
e488ca9f
BN
57 } else if (!of_device_is_compatible(ofpart_node, "fixed-partitions")) {
58 /* The 'partitions' subnode might be used by another parser */
59 return 0;
5cfdedb7
MS
60 }
61
9a310d21 62 /* First count the subnodes */
9a310d21 63 nr_parts = 0;
5cfdedb7
MS
64 for_each_child_of_node(ofpart_node, pp) {
65 if (!dedicated && node_has_compatible(pp))
e79265ba
JW
66 continue;
67
9a310d21 68 nr_parts++;
e79265ba 69 }
9a310d21
SW
70
71 if (nr_parts == 0)
72 return 0;
73
6396bb22 74 parts = kcalloc(nr_parts, sizeof(*parts), GFP_KERNEL);
c3168d26 75 if (!parts)
9a310d21
SW
76 return -ENOMEM;
77
9a310d21 78 i = 0;
5cfdedb7 79 for_each_child_of_node(ofpart_node, pp) {
766f271a 80 const __be32 *reg;
9a310d21 81 int len;
05ff8c25 82 int a_cells, s_cells;
9a310d21 83
5cfdedb7 84 if (!dedicated && node_has_compatible(pp))
e79265ba
JW
85 continue;
86
ebd5a74d
BK
87 reg = of_get_property(pp, "reg", &len);
88 if (!reg) {
5cfdedb7 89 if (dedicated) {
1d706077
RH
90 pr_debug("%s: ofpart partition %pOF (%pOF) missing reg property.\n",
91 master->name, pp,
92 mtd_node);
5cfdedb7
MS
93 goto ofpart_fail;
94 } else {
95 nr_parts--;
96 continue;
97 }
4b08e149
BK
98 }
99
05ff8c25
JS
100 a_cells = of_n_addr_cells(pp);
101 s_cells = of_n_size_cells(pp);
5cfdedb7 102 if (len / 4 != a_cells + s_cells) {
1d706077
RH
103 pr_debug("%s: ofpart partition %pOF (%pOF) error parsing reg property.\n",
104 master->name, pp,
105 mtd_node);
5cfdedb7
MS
106 goto ofpart_fail;
107 }
108
c3168d26
BN
109 parts[i].offset = of_read_number(reg, a_cells);
110 parts[i].size = of_read_number(reg + a_cells, s_cells);
42e9401b 111 parts[i].of_node = pp;
9a310d21
SW
112
113 partname = of_get_property(pp, "label", &len);
114 if (!partname)
115 partname = of_get_property(pp, "name", &len);
c3168d26 116 parts[i].name = partname;
9a310d21
SW
117
118 if (of_get_property(pp, "read-only", &len))
c3168d26 119 parts[i].mask_flags |= MTD_WRITEABLE;
ab0b00bc
JR
120
121 if (of_get_property(pp, "lock", &len))
c3168d26 122 parts[i].mask_flags |= MTD_POWERUP_LOCK;
9a310d21
SW
123
124 i++;
125 }
126
5cfdedb7
MS
127 if (!nr_parts)
128 goto ofpart_none;
ebd5a74d 129
c3168d26 130 *pparts = parts;
9a310d21 131 return nr_parts;
5cfdedb7
MS
132
133ofpart_fail:
1d706077
RH
134 pr_err("%s: error parsing ofpart partition %pOF (%pOF)\n",
135 master->name, pp, mtd_node);
5cfdedb7
MS
136 ret = -EINVAL;
137ofpart_none:
138 of_node_put(pp);
c3168d26 139 kfree(parts);
5cfdedb7 140 return ret;
9a310d21 141}
950bcb25 142
97b0c7c0
RM
143static const struct of_device_id parse_ofpart_match_table[] = {
144 { .compatible = "fixed-partitions" },
145 {},
146};
147MODULE_DEVICE_TABLE(of, parse_ofpart_match_table);
148
d26c87d6 149static struct mtd_part_parser ofpart_parser = {
c0faf434
RM
150 .parse_fn = parse_fixed_partitions,
151 .name = "fixed-partitions",
97b0c7c0 152 .of_match_table = parse_ofpart_match_table,
d26c87d6
DB
153};
154
fbcf62a3 155static int parse_ofoldpart_partitions(struct mtd_info *master,
b9adf469 156 const struct mtd_partition **pparts,
fbcf62a3
DB
157 struct mtd_part_parser_data *data)
158{
c3168d26 159 struct mtd_partition *parts;
fbcf62a3
DB
160 struct device_node *dp;
161 int i, plen, nr_parts;
162 const struct {
163 __be32 offset, len;
164 } *part;
165 const char *names;
166
e270bca5
BN
167 /* Pull of_node from the master device node */
168 dp = mtd_get_of_node(master);
fbcf62a3
DB
169 if (!dp)
170 return 0;
171
172 part = of_get_property(dp, "partitions", &plen);
173 if (!part)
174 return 0; /* No partitions found */
175
1d706077 176 pr_warn("Device tree uses obsolete partition map binding: %pOF\n", dp);
fbcf62a3
DB
177
178 nr_parts = plen / sizeof(part[0]);
179
6396bb22 180 parts = kcalloc(nr_parts, sizeof(*parts), GFP_KERNEL);
c3168d26 181 if (!parts)
fbcf62a3
DB
182 return -ENOMEM;
183
184 names = of_get_property(dp, "partition-names", &plen);
185
186 for (i = 0; i < nr_parts; i++) {
c3168d26
BN
187 parts[i].offset = be32_to_cpu(part->offset);
188 parts[i].size = be32_to_cpu(part->len) & ~1;
fbcf62a3
DB
189 /* bit 0 set signifies read only partition */
190 if (be32_to_cpu(part->len) & 1)
c3168d26 191 parts[i].mask_flags = MTD_WRITEABLE;
fbcf62a3
DB
192
193 if (names && (plen > 0)) {
194 int len = strlen(names) + 1;
195
c3168d26 196 parts[i].name = names;
fbcf62a3
DB
197 plen -= len;
198 names += len;
199 } else {
c3168d26 200 parts[i].name = "unnamed";
fbcf62a3
DB
201 }
202
203 part++;
204 }
205
c3168d26 206 *pparts = parts;
fbcf62a3
DB
207 return nr_parts;
208}
209
210static struct mtd_part_parser ofoldpart_parser = {
fbcf62a3
DB
211 .parse_fn = parse_ofoldpart_partitions,
212 .name = "ofoldpart",
213};
214
d26c87d6
DB
215static int __init ofpart_parser_init(void)
216{
6e14a61d
AL
217 register_mtd_parser(&ofpart_parser);
218 register_mtd_parser(&ofoldpart_parser);
219 return 0;
d26c87d6
DB
220}
221
422f3890
LR
222static void __exit ofpart_parser_exit(void)
223{
224 deregister_mtd_parser(&ofpart_parser);
225 deregister_mtd_parser(&ofoldpart_parser);
226}
227
d26c87d6 228module_init(ofpart_parser_init);
422f3890 229module_exit(ofpart_parser_exit);
d26c87d6 230
950bcb25 231MODULE_LICENSE("GPL");
d6137bad
DB
232MODULE_DESCRIPTION("Parser for MTD partitioning information in device tree");
233MODULE_AUTHOR("Vitaly Wool, David Gibson");
9786f6e6
DB
234/*
235 * When MTD core cannot find the requested parser, it tries to load the module
236 * with the same name. Since we provide the ofoldpart parser, we should have
237 * the corresponding alias.
238 */
c0faf434 239MODULE_ALIAS("fixed-partitions");
9786f6e6 240MODULE_ALIAS("ofoldpart");