2 * Flash partitions described by the OF (or flattened) device tree
4 * Copyright © 2006 MontaVista Software Inc.
5 * Author: Vitaly Wool <vwool@ru.mvista.com>
7 * Revised to handle newer style flash binding by:
8 * Copyright © 2007 David Gibson, IBM Corporation.
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.
16 #include <linux/module.h>
17 #include <linux/init.h>
19 #include <linux/mtd/mtd.h>
20 #include <linux/slab.h>
21 #include <linux/mtd/partitions.h>
23 static bool node_has_compatible(struct device_node
*pp
)
25 return of_get_property(pp
, "compatible", NULL
);
28 static int parse_fixed_partitions(struct mtd_info
*master
,
29 const struct mtd_partition
**pparts
,
30 struct mtd_part_parser_data
*data
)
32 struct mtd_partition
*parts
;
33 struct device_node
*mtd_node
;
34 struct device_node
*ofpart_node
;
36 struct device_node
*pp
;
37 int nr_parts
, i
, ret
= 0;
38 bool dedicated
= true;
41 /* Pull of_node from the master device node */
42 mtd_node
= mtd_get_of_node(master
);
46 ofpart_node
= of_get_child_by_name(mtd_node
, "partitions");
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
53 pr_debug("%s: 'partitions' subnode not found on %pOF. Trying to parse direct subnodes as partitions.\n",
54 master
->name
, mtd_node
);
55 ofpart_node
= mtd_node
;
57 } else if (!of_device_is_compatible(ofpart_node
, "fixed-partitions")) {
58 /* The 'partitions' subnode might be used by another parser */
62 /* First count the subnodes */
64 for_each_child_of_node(ofpart_node
, pp
) {
65 if (!dedicated
&& node_has_compatible(pp
))
74 parts
= kcalloc(nr_parts
, sizeof(*parts
), GFP_KERNEL
);
79 for_each_child_of_node(ofpart_node
, pp
) {
84 if (!dedicated
&& node_has_compatible(pp
))
87 reg
= of_get_property(pp
, "reg", &len
);
90 pr_debug("%s: ofpart partition %pOF (%pOF) missing reg property.\n",
100 a_cells
= of_n_addr_cells(pp
);
101 s_cells
= of_n_size_cells(pp
);
102 if (len
/ 4 != a_cells
+ s_cells
) {
103 pr_debug("%s: ofpart partition %pOF (%pOF) error parsing reg property.\n",
109 parts
[i
].offset
= of_read_number(reg
, a_cells
);
110 parts
[i
].size
= of_read_number(reg
+ a_cells
, s_cells
);
111 parts
[i
].of_node
= pp
;
113 partname
= of_get_property(pp
, "label", &len
);
115 partname
= of_get_property(pp
, "name", &len
);
116 parts
[i
].name
= partname
;
118 if (of_get_property(pp
, "read-only", &len
))
119 parts
[i
].mask_flags
|= MTD_WRITEABLE
;
121 if (of_get_property(pp
, "lock", &len
))
122 parts
[i
].mask_flags
|= MTD_POWERUP_LOCK
;
134 pr_err("%s: error parsing ofpart partition %pOF (%pOF)\n",
135 master
->name
, pp
, mtd_node
);
143 static const struct of_device_id parse_ofpart_match_table
[] = {
144 { .compatible
= "fixed-partitions" },
147 MODULE_DEVICE_TABLE(of
, parse_ofpart_match_table
);
149 static struct mtd_part_parser ofpart_parser
= {
150 .parse_fn
= parse_fixed_partitions
,
151 .name
= "fixed-partitions",
152 .of_match_table
= parse_ofpart_match_table
,
155 static int parse_ofoldpart_partitions(struct mtd_info
*master
,
156 const struct mtd_partition
**pparts
,
157 struct mtd_part_parser_data
*data
)
159 struct mtd_partition
*parts
;
160 struct device_node
*dp
;
161 int i
, plen
, nr_parts
;
167 /* Pull of_node from the master device node */
168 dp
= mtd_get_of_node(master
);
172 part
= of_get_property(dp
, "partitions", &plen
);
174 return 0; /* No partitions found */
176 pr_warn("Device tree uses obsolete partition map binding: %pOF\n", dp
);
178 nr_parts
= plen
/ sizeof(part
[0]);
180 parts
= kcalloc(nr_parts
, sizeof(*parts
), GFP_KERNEL
);
184 names
= of_get_property(dp
, "partition-names", &plen
);
186 for (i
= 0; i
< nr_parts
; i
++) {
187 parts
[i
].offset
= be32_to_cpu(part
->offset
);
188 parts
[i
].size
= be32_to_cpu(part
->len
) & ~1;
189 /* bit 0 set signifies read only partition */
190 if (be32_to_cpu(part
->len
) & 1)
191 parts
[i
].mask_flags
= MTD_WRITEABLE
;
193 if (names
&& (plen
> 0)) {
194 int len
= strlen(names
) + 1;
196 parts
[i
].name
= names
;
200 parts
[i
].name
= "unnamed";
210 static struct mtd_part_parser ofoldpart_parser
= {
211 .parse_fn
= parse_ofoldpart_partitions
,
215 static int __init
ofpart_parser_init(void)
217 register_mtd_parser(&ofpart_parser
);
218 register_mtd_parser(&ofoldpart_parser
);
222 static void __exit
ofpart_parser_exit(void)
224 deregister_mtd_parser(&ofpart_parser
);
225 deregister_mtd_parser(&ofoldpart_parser
);
228 module_init(ofpart_parser_init
);
229 module_exit(ofpart_parser_exit
);
231 MODULE_LICENSE("GPL");
232 MODULE_DESCRIPTION("Parser for MTD partitioning information in device tree");
233 MODULE_AUTHOR("Vitaly Wool, David Gibson");
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.
239 MODULE_ALIAS("fixed-partitions");
240 MODULE_ALIAS("ofoldpart");