]>
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 <[email protected]> |
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 | 28 | static int parse_ofpart_partitions(struct mtd_info *master, |
b9adf469 | 29 | const struct mtd_partition **pparts, |
d26c87d6 DB |
30 | struct mtd_part_parser_data *data) |
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 | */ | |
53 | pr_debug("%s: 'partitions' subnode not found on %s. Trying to parse direct subnodes as partitions.\n", | |
54 | master->name, mtd_node->full_name); | |
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 | ||
c3168d26 BN |
74 | parts = kzalloc(nr_parts * sizeof(*parts), GFP_KERNEL); |
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 MS |
89 | if (dedicated) { |
90 | pr_debug("%s: ofpart partition %s (%s) missing reg property.\n", | |
91 | master->name, pp->full_name, | |
92 | mtd_node->full_name); | |
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 MS |
102 | if (len / 4 != a_cells + s_cells) { |
103 | pr_debug("%s: ofpart partition %s (%s) error parsing reg property.\n", | |
104 | master->name, pp->full_name, | |
105 | mtd_node->full_name); | |
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); | |
9a310d21 SW |
111 | |
112 | partname = of_get_property(pp, "label", &len); | |
113 | if (!partname) | |
114 | partname = of_get_property(pp, "name", &len); | |
c3168d26 | 115 | parts[i].name = partname; |
9a310d21 SW |
116 | |
117 | if (of_get_property(pp, "read-only", &len)) | |
c3168d26 | 118 | parts[i].mask_flags |= MTD_WRITEABLE; |
ab0b00bc JR |
119 | |
120 | if (of_get_property(pp, "lock", &len)) | |
c3168d26 | 121 | parts[i].mask_flags |= MTD_POWERUP_LOCK; |
9a310d21 SW |
122 | |
123 | i++; | |
124 | } | |
125 | ||
5cfdedb7 MS |
126 | if (!nr_parts) |
127 | goto ofpart_none; | |
ebd5a74d | 128 | |
c3168d26 | 129 | *pparts = parts; |
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); | |
c3168d26 | 138 | kfree(parts); |
5cfdedb7 | 139 | return ret; |
9a310d21 | 140 | } |
950bcb25 | 141 | |
d26c87d6 | 142 | static struct mtd_part_parser ofpart_parser = { |
d26c87d6 DB |
143 | .parse_fn = parse_ofpart_partitions, |
144 | .name = "ofpart", | |
145 | }; | |
146 | ||
fbcf62a3 | 147 | static int parse_ofoldpart_partitions(struct mtd_info *master, |
b9adf469 | 148 | const struct mtd_partition **pparts, |
fbcf62a3 DB |
149 | struct mtd_part_parser_data *data) |
150 | { | |
c3168d26 | 151 | struct mtd_partition *parts; |
fbcf62a3 DB |
152 | struct device_node *dp; |
153 | int i, plen, nr_parts; | |
154 | const struct { | |
155 | __be32 offset, len; | |
156 | } *part; | |
157 | const char *names; | |
158 | ||
e270bca5 BN |
159 | /* Pull of_node from the master device node */ |
160 | dp = mtd_get_of_node(master); | |
fbcf62a3 DB |
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 | ||
c3168d26 BN |
173 | parts = kzalloc(nr_parts * sizeof(*parts), GFP_KERNEL); |
174 | if (!parts) | |
fbcf62a3 DB |
175 | return -ENOMEM; |
176 | ||
177 | names = of_get_property(dp, "partition-names", &plen); | |
178 | ||
179 | for (i = 0; i < nr_parts; i++) { | |
c3168d26 BN |
180 | parts[i].offset = be32_to_cpu(part->offset); |
181 | parts[i].size = be32_to_cpu(part->len) & ~1; | |
fbcf62a3 DB |
182 | /* bit 0 set signifies read only partition */ |
183 | if (be32_to_cpu(part->len) & 1) | |
c3168d26 | 184 | parts[i].mask_flags = MTD_WRITEABLE; |
fbcf62a3 DB |
185 | |
186 | if (names && (plen > 0)) { | |
187 | int len = strlen(names) + 1; | |
188 | ||
c3168d26 | 189 | parts[i].name = names; |
fbcf62a3 DB |
190 | plen -= len; |
191 | names += len; | |
192 | } else { | |
c3168d26 | 193 | parts[i].name = "unnamed"; |
fbcf62a3 DB |
194 | } |
195 | ||
196 | part++; | |
197 | } | |
198 | ||
c3168d26 | 199 | *pparts = parts; |
fbcf62a3 DB |
200 | return nr_parts; |
201 | } | |
202 | ||
203 | static struct mtd_part_parser ofoldpart_parser = { | |
fbcf62a3 DB |
204 | .parse_fn = parse_ofoldpart_partitions, |
205 | .name = "ofoldpart", | |
206 | }; | |
207 | ||
d26c87d6 DB |
208 | static int __init ofpart_parser_init(void) |
209 | { | |
6e14a61d AL |
210 | register_mtd_parser(&ofpart_parser); |
211 | register_mtd_parser(&ofoldpart_parser); | |
212 | return 0; | |
d26c87d6 DB |
213 | } |
214 | ||
422f3890 LR |
215 | static void __exit ofpart_parser_exit(void) |
216 | { | |
217 | deregister_mtd_parser(&ofpart_parser); | |
218 | deregister_mtd_parser(&ofoldpart_parser); | |
219 | } | |
220 | ||
d26c87d6 | 221 | module_init(ofpart_parser_init); |
422f3890 | 222 | module_exit(ofpart_parser_exit); |
d26c87d6 | 223 | |
950bcb25 | 224 | MODULE_LICENSE("GPL"); |
d6137bad DB |
225 | MODULE_DESCRIPTION("Parser for MTD partitioning information in device tree"); |
226 | MODULE_AUTHOR("Vitaly Wool, David Gibson"); | |
9786f6e6 DB |
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"); |