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_ofpart_partitions(struct mtd_info *master,
29 struct mtd_partition **pparts,
30 struct mtd_part_parser_data *data)
32 struct device_node *node;
34 struct device_node *pp;
45 /* First count the subnodes */
47 for_each_child_of_node(node, pp) {
48 if (node_has_compatible(pp))
57 *pparts = kzalloc(nr_parts * sizeof(**pparts), GFP_KERNEL);
62 for_each_child_of_node(node, pp) {
67 if (node_has_compatible(pp))
70 reg = of_get_property(pp, "reg", &len);
76 a_cells = of_n_addr_cells(pp);
77 s_cells = of_n_size_cells(pp);
78 (*pparts)[i].offset = of_read_number(reg, a_cells);
79 (*pparts)[i].size = of_read_number(reg + a_cells, s_cells);
81 partname = of_get_property(pp, "label", &len);
83 partname = of_get_property(pp, "name", &len);
84 (*pparts)[i].name = partname;
86 if (of_get_property(pp, "read-only", &len))
87 (*pparts)[i].mask_flags |= MTD_WRITEABLE;
89 if (of_get_property(pp, "lock", &len))
90 (*pparts)[i].mask_flags |= MTD_POWERUP_LOCK;
97 pr_err("No valid partition found on %s\n", node->full_name);
106 static struct mtd_part_parser ofpart_parser = {
107 .owner = THIS_MODULE,
108 .parse_fn = parse_ofpart_partitions,
112 static int parse_ofoldpart_partitions(struct mtd_info *master,
113 struct mtd_partition **pparts,
114 struct mtd_part_parser_data *data)
116 struct device_node *dp;
117 int i, plen, nr_parts;
130 part = of_get_property(dp, "partitions", &plen);
132 return 0; /* No partitions found */
134 pr_warning("Device tree uses obsolete partition map binding: %s\n",
137 nr_parts = plen / sizeof(part[0]);
139 *pparts = kzalloc(nr_parts * sizeof(*(*pparts)), GFP_KERNEL);
143 names = of_get_property(dp, "partition-names", &plen);
145 for (i = 0; i < nr_parts; i++) {
146 (*pparts)[i].offset = be32_to_cpu(part->offset);
147 (*pparts)[i].size = be32_to_cpu(part->len) & ~1;
148 /* bit 0 set signifies read only partition */
149 if (be32_to_cpu(part->len) & 1)
150 (*pparts)[i].mask_flags = MTD_WRITEABLE;
152 if (names && (plen > 0)) {
153 int len = strlen(names) + 1;
155 (*pparts)[i].name = names;
159 (*pparts)[i].name = "unnamed";
168 static struct mtd_part_parser ofoldpart_parser = {
169 .owner = THIS_MODULE,
170 .parse_fn = parse_ofoldpart_partitions,
174 static int __init ofpart_parser_init(void)
176 register_mtd_parser(&ofpart_parser);
177 register_mtd_parser(&ofoldpart_parser);
181 static void __exit ofpart_parser_exit(void)
183 deregister_mtd_parser(&ofpart_parser);
184 deregister_mtd_parser(&ofoldpart_parser);
187 module_init(ofpart_parser_init);
188 module_exit(ofpart_parser_exit);
190 MODULE_LICENSE("GPL");
191 MODULE_DESCRIPTION("Parser for MTD partitioning information in device tree");
192 MODULE_AUTHOR("Vitaly Wool, David Gibson");
194 * When MTD core cannot find the requested parser, it tries to load the module
195 * with the same name. Since we provide the ofoldpart parser, we should have
196 * the corresponding alias.
198 MODULE_ALIAS("ofoldpart");