1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Flash partitions described by the OF (or flattened) device tree
5 * Copyright © 2006 MontaVista Software Inc.
6 * Author: Vitaly Wool <vwool@ru.mvista.com>
8 * Revised to handle newer style flash binding by:
9 * Copyright © 2007 David Gibson, IBM Corporation.
12 #include <linux/module.h>
13 #include <linux/init.h>
15 #include <linux/mtd/mtd.h>
16 #include <linux/slab.h>
17 #include <linux/mtd/partitions.h>
19 static bool node_has_compatible(struct device_node
*pp
)
21 return of_get_property(pp
, "compatible", NULL
);
24 static int parse_fixed_partitions(struct mtd_info
*master
,
25 const struct mtd_partition
**pparts
,
26 struct mtd_part_parser_data
*data
)
28 struct mtd_partition
*parts
;
29 struct device_node
*mtd_node
;
30 struct device_node
*ofpart_node
;
32 struct device_node
*pp
;
33 int nr_parts
, i
, ret
= 0;
34 bool dedicated
= true;
37 /* Pull of_node from the master device node */
38 mtd_node
= mtd_get_of_node(master
);
42 ofpart_node
= of_get_child_by_name(mtd_node
, "partitions");
45 * We might get here even when ofpart isn't used at all (e.g.,
46 * when using another parser), so don't be louder than
49 pr_debug("%s: 'partitions' subnode not found on %pOF. Trying to parse direct subnodes as partitions.\n",
50 master
->name
, mtd_node
);
51 ofpart_node
= mtd_node
;
53 } else if (!of_device_is_compatible(ofpart_node
, "fixed-partitions")) {
54 /* The 'partitions' subnode might be used by another parser */
58 /* First count the subnodes */
60 for_each_child_of_node(ofpart_node
, pp
) {
61 if (!dedicated
&& node_has_compatible(pp
))
70 parts
= kcalloc(nr_parts
, sizeof(*parts
), GFP_KERNEL
);
75 for_each_child_of_node(ofpart_node
, pp
) {
80 if (!dedicated
&& node_has_compatible(pp
))
83 reg
= of_get_property(pp
, "reg", &len
);
86 pr_debug("%s: ofpart partition %pOF (%pOF) missing reg property.\n",
96 a_cells
= of_n_addr_cells(pp
);
97 s_cells
= of_n_size_cells(pp
);
98 if (len
/ 4 != a_cells
+ s_cells
) {
99 pr_debug("%s: ofpart partition %pOF (%pOF) error parsing reg property.\n",
105 parts
[i
].offset
= of_read_number(reg
, a_cells
);
106 parts
[i
].size
= of_read_number(reg
+ a_cells
, s_cells
);
107 parts
[i
].of_node
= pp
;
109 partname
= of_get_property(pp
, "label", &len
);
111 partname
= of_get_property(pp
, "name", &len
);
112 parts
[i
].name
= partname
;
114 if (of_get_property(pp
, "read-only", &len
))
115 parts
[i
].mask_flags
|= MTD_WRITEABLE
;
117 if (of_get_property(pp
, "lock", &len
))
118 parts
[i
].mask_flags
|= MTD_POWERUP_LOCK
;
120 if (of_property_read_bool(pp
, "slc-mode"))
121 parts
[i
].add_flags
|= MTD_SLC_ON_MLC_EMULATION
;
133 pr_err("%s: error parsing ofpart partition %pOF (%pOF)\n",
134 master
->name
, pp
, mtd_node
);
142 static const struct of_device_id parse_ofpart_match_table
[] = {
143 { .compatible
= "fixed-partitions" },
146 MODULE_DEVICE_TABLE(of
, parse_ofpart_match_table
);
148 static struct mtd_part_parser ofpart_parser
= {
149 .parse_fn
= parse_fixed_partitions
,
150 .name
= "fixed-partitions",
151 .of_match_table
= parse_ofpart_match_table
,
154 static int parse_ofoldpart_partitions(struct mtd_info
*master
,
155 const struct mtd_partition
**pparts
,
156 struct mtd_part_parser_data
*data
)
158 struct mtd_partition
*parts
;
159 struct device_node
*dp
;
160 int i
, plen
, nr_parts
;
166 /* Pull of_node from the master device node */
167 dp
= mtd_get_of_node(master
);
171 part
= of_get_property(dp
, "partitions", &plen
);
173 return 0; /* No partitions found */
175 pr_warn("Device tree uses obsolete partition map binding: %pOF\n", dp
);
177 nr_parts
= plen
/ sizeof(part
[0]);
179 parts
= kcalloc(nr_parts
, sizeof(*parts
), GFP_KERNEL
);
183 names
= of_get_property(dp
, "partition-names", &plen
);
185 for (i
= 0; i
< nr_parts
; i
++) {
186 parts
[i
].offset
= be32_to_cpu(part
->offset
);
187 parts
[i
].size
= be32_to_cpu(part
->len
) & ~1;
188 /* bit 0 set signifies read only partition */
189 if (be32_to_cpu(part
->len
) & 1)
190 parts
[i
].mask_flags
= MTD_WRITEABLE
;
192 if (names
&& (plen
> 0)) {
193 int len
= strlen(names
) + 1;
195 parts
[i
].name
= names
;
199 parts
[i
].name
= "unnamed";
209 static struct mtd_part_parser ofoldpart_parser
= {
210 .parse_fn
= parse_ofoldpart_partitions
,
214 static int __init
ofpart_parser_init(void)
216 register_mtd_parser(&ofpart_parser
);
217 register_mtd_parser(&ofoldpart_parser
);
221 static void __exit
ofpart_parser_exit(void)
223 deregister_mtd_parser(&ofpart_parser
);
224 deregister_mtd_parser(&ofoldpart_parser
);
227 module_init(ofpart_parser_init
);
228 module_exit(ofpart_parser_exit
);
230 MODULE_LICENSE("GPL");
231 MODULE_DESCRIPTION("Parser for MTD partitioning information in device tree");
232 MODULE_AUTHOR("Vitaly Wool, David Gibson");
234 * When MTD core cannot find the requested parser, it tries to load the module
235 * with the same name. Since we provide the ofoldpart parser, we should have
236 * the corresponding alias.
238 MODULE_ALIAS("fixed-partitions");
239 MODULE_ALIAS("ofoldpart");