Commit | Line | Data |
---|---|---|
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 |
23 | static bool node_has_compatible(struct device_node *pp) |
24 | { | |
25 | return of_get_property(pp, "compatible", NULL); | |
26 | } | |
27 | ||
d26c87d6 DES |
28 | static int parse_ofpart_partitions(struct mtd_info *master, |
29 | struct mtd_partition **pparts, | |
30 | struct mtd_part_parser_data *data) | |
31 | { | |
5cfdedb7 MS |
32 | struct device_node *mtd_node; |
33 | struct device_node *ofpart_node; | |
9a310d21 SW |
34 | const char *partname; |
35 | struct device_node *pp; | |
5cfdedb7 MS |
36 | int nr_parts, i, ret = 0; |
37 | bool dedicated = true; | |
9a310d21 | 38 | |
628376fb DES |
39 | |
40 | if (!data) | |
41 | return 0; | |
42 | ||
5cfdedb7 MS |
43 | mtd_node = data->of_node; |
44 | if (!mtd_node) | |
628376fb DES |
45 | return 0; |
46 | ||
5cfdedb7 MS |
47 | ofpart_node = of_get_child_by_name(mtd_node, "partitions"); |
48 | if (!ofpart_node) { | |
8c62b4e1 BN |
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); | |
5cfdedb7 MS |
56 | ofpart_node = mtd_node; |
57 | dedicated = false; | |
e488ca9f BN |
58 | } else if (!of_device_is_compatible(ofpart_node, "fixed-partitions")) { |
59 | /* The 'partitions' subnode might be used by another parser */ | |
60 | return 0; | |
5cfdedb7 MS |
61 | } |
62 | ||
9a310d21 | 63 | /* First count the subnodes */ |
9a310d21 | 64 | nr_parts = 0; |
5cfdedb7 MS |
65 | for_each_child_of_node(ofpart_node, pp) { |
66 | if (!dedicated && node_has_compatible(pp)) | |
e79265ba JW |
67 | continue; |
68 | ||
9a310d21 | 69 | nr_parts++; |
e79265ba | 70 | } |
9a310d21 SW |
71 | |
72 | if (nr_parts == 0) | |
73 | return 0; | |
74 | ||
75 | *pparts = kzalloc(nr_parts * sizeof(**pparts), GFP_KERNEL); | |
76 | if (!*pparts) | |
77 | return -ENOMEM; | |
78 | ||
9a310d21 | 79 | i = 0; |
5cfdedb7 | 80 | for_each_child_of_node(ofpart_node, pp) { |
766f271a | 81 | const __be32 *reg; |
9a310d21 | 82 | int len; |
05ff8c25 | 83 | int a_cells, s_cells; |
9a310d21 | 84 | |
5cfdedb7 | 85 | if (!dedicated && node_has_compatible(pp)) |
e79265ba JW |
86 | continue; |
87 | ||
ebd5a74d BK |
88 | reg = of_get_property(pp, "reg", &len); |
89 | if (!reg) { | |
5cfdedb7 MS |
90 | if (dedicated) { |
91 | pr_debug("%s: ofpart partition %s (%s) missing reg property.\n", | |
92 | master->name, pp->full_name, | |
93 | mtd_node->full_name); | |
94 | goto ofpart_fail; | |
95 | } else { | |
96 | nr_parts--; | |
97 | continue; | |
98 | } | |
4b08e149 BK |
99 | } |
100 | ||
05ff8c25 JS |
101 | a_cells = of_n_addr_cells(pp); |
102 | s_cells = of_n_size_cells(pp); | |
5cfdedb7 MS |
103 | if (len / 4 != a_cells + s_cells) { |
104 | pr_debug("%s: ofpart partition %s (%s) error parsing reg property.\n", | |
105 | master->name, pp->full_name, | |
106 | mtd_node->full_name); | |
107 | goto ofpart_fail; | |
108 | } | |
109 | ||
05ff8c25 JS |
110 | (*pparts)[i].offset = of_read_number(reg, a_cells); |
111 | (*pparts)[i].size = of_read_number(reg + a_cells, s_cells); | |
9a310d21 SW |
112 | |
113 | partname = of_get_property(pp, "label", &len); | |
114 | if (!partname) | |
115 | partname = of_get_property(pp, "name", &len); | |
26a6d240 | 116 | (*pparts)[i].name = partname; |
9a310d21 SW |
117 | |
118 | if (of_get_property(pp, "read-only", &len)) | |
ab0b00bc JR |
119 | (*pparts)[i].mask_flags |= MTD_WRITEABLE; |
120 | ||
121 | if (of_get_property(pp, "lock", &len)) | |
122 | (*pparts)[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 | |
9a310d21 | 130 | return nr_parts; |
5cfdedb7 MS |
131 | |
132 | ofpart_fail: | |
133 | pr_err("%s: error parsing ofpart partition %s (%s)\n", | |
134 | master->name, pp->full_name, mtd_node->full_name); | |
135 | ret = -EINVAL; | |
136 | ofpart_none: | |
137 | of_node_put(pp); | |
138 | kfree(*pparts); | |
139 | *pparts = NULL; | |
140 | return ret; | |
9a310d21 | 141 | } |
950bcb25 | 142 | |
d26c87d6 DES |
143 | static struct mtd_part_parser ofpart_parser = { |
144 | .owner = THIS_MODULE, | |
145 | .parse_fn = parse_ofpart_partitions, | |
146 | .name = "ofpart", | |
147 | }; | |
148 | ||
fbcf62a3 DES |
149 | static int parse_ofoldpart_partitions(struct mtd_info *master, |
150 | struct mtd_partition **pparts, | |
151 | struct mtd_part_parser_data *data) | |
152 | { | |
153 | struct device_node *dp; | |
154 | int i, plen, nr_parts; | |
155 | const struct { | |
156 | __be32 offset, len; | |
157 | } *part; | |
158 | const char *names; | |
159 | ||
160 | if (!data) | |
161 | return 0; | |
162 | ||
163 | dp = data->of_node; | |
164 | if (!dp) | |
165 | return 0; | |
166 | ||
167 | part = of_get_property(dp, "partitions", &plen); | |
168 | if (!part) | |
169 | return 0; /* No partitions found */ | |
170 | ||
171 | pr_warning("Device tree uses obsolete partition map binding: %s\n", | |
172 | dp->full_name); | |
173 | ||
174 | nr_parts = plen / sizeof(part[0]); | |
175 | ||
176 | *pparts = kzalloc(nr_parts * sizeof(*(*pparts)), GFP_KERNEL); | |
ecbcbc7b | 177 | if (!*pparts) |
fbcf62a3 DES |
178 | return -ENOMEM; |
179 | ||
180 | names = of_get_property(dp, "partition-names", &plen); | |
181 | ||
182 | for (i = 0; i < nr_parts; i++) { | |
183 | (*pparts)[i].offset = be32_to_cpu(part->offset); | |
184 | (*pparts)[i].size = be32_to_cpu(part->len) & ~1; | |
185 | /* bit 0 set signifies read only partition */ | |
186 | if (be32_to_cpu(part->len) & 1) | |
187 | (*pparts)[i].mask_flags = MTD_WRITEABLE; | |
188 | ||
189 | if (names && (plen > 0)) { | |
190 | int len = strlen(names) + 1; | |
191 | ||
26a6d240 | 192 | (*pparts)[i].name = names; |
fbcf62a3 DES |
193 | plen -= len; |
194 | names += len; | |
195 | } else { | |
196 | (*pparts)[i].name = "unnamed"; | |
197 | } | |
198 | ||
199 | part++; | |
200 | } | |
201 | ||
202 | return nr_parts; | |
203 | } | |
204 | ||
205 | static struct mtd_part_parser ofoldpart_parser = { | |
206 | .owner = THIS_MODULE, | |
207 | .parse_fn = parse_ofoldpart_partitions, | |
208 | .name = "ofoldpart", | |
209 | }; | |
210 | ||
d26c87d6 DES |
211 | static int __init ofpart_parser_init(void) |
212 | { | |
6e14a61d AL |
213 | register_mtd_parser(&ofpart_parser); |
214 | register_mtd_parser(&ofoldpart_parser); | |
215 | return 0; | |
d26c87d6 DES |
216 | } |
217 | ||
422f3890 LR |
218 | static void __exit ofpart_parser_exit(void) |
219 | { | |
220 | deregister_mtd_parser(&ofpart_parser); | |
221 | deregister_mtd_parser(&ofoldpart_parser); | |
222 | } | |
223 | ||
d26c87d6 | 224 | module_init(ofpart_parser_init); |
422f3890 | 225 | module_exit(ofpart_parser_exit); |
d26c87d6 | 226 | |
950bcb25 | 227 | MODULE_LICENSE("GPL"); |
d6137bad DES |
228 | MODULE_DESCRIPTION("Parser for MTD partitioning information in device tree"); |
229 | MODULE_AUTHOR("Vitaly Wool, David Gibson"); | |
9786f6e6 DES |
230 | /* |
231 | * When MTD core cannot find the requested parser, it tries to load the module | |
232 | * with the same name. Since we provide the ofoldpart parser, we should have | |
233 | * the corresponding alias. | |
234 | */ | |
235 | MODULE_ALIAS("ofoldpart"); |