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 int parse_ofpart_partitions(struct mtd_info *master,
24 struct mtd_partition **pparts,
25 struct mtd_part_parser_data *data)
27 struct device_node *node;
29 struct device_node *pp;
40 /* First count the subnodes */
43 while ((pp = of_get_next_child(node, pp)))
49 *pparts = kzalloc(nr_parts * sizeof(**pparts), GFP_KERNEL);
55 while ((pp = of_get_next_child(node, pp))) {
60 reg = of_get_property(pp, "reg", &len);
66 a_cells = of_n_addr_cells(pp);
67 s_cells = of_n_size_cells(pp);
68 (*pparts)[i].offset = of_read_number(reg, a_cells);
69 (*pparts)[i].size = of_read_number(reg + a_cells, s_cells);
71 partname = of_get_property(pp, "label", &len);
73 partname = of_get_property(pp, "name", &len);
74 (*pparts)[i].name = (char *)partname;
76 if (of_get_property(pp, "read-only", &len))
77 (*pparts)[i].mask_flags |= MTD_WRITEABLE;
79 if (of_get_property(pp, "lock", &len))
80 (*pparts)[i].mask_flags |= MTD_POWERUP_LOCK;
87 pr_err("No valid partition found on %s\n", node->full_name);
96 static struct mtd_part_parser ofpart_parser = {
98 .parse_fn = parse_ofpart_partitions,
102 static int parse_ofoldpart_partitions(struct mtd_info *master,
103 struct mtd_partition **pparts,
104 struct mtd_part_parser_data *data)
106 struct device_node *dp;
107 int i, plen, nr_parts;
120 part = of_get_property(dp, "partitions", &plen);
122 return 0; /* No partitions found */
124 pr_warning("Device tree uses obsolete partition map binding: %s\n",
127 nr_parts = plen / sizeof(part[0]);
129 *pparts = kzalloc(nr_parts * sizeof(*(*pparts)), GFP_KERNEL);
133 names = of_get_property(dp, "partition-names", &plen);
135 for (i = 0; i < nr_parts; i++) {
136 (*pparts)[i].offset = be32_to_cpu(part->offset);
137 (*pparts)[i].size = be32_to_cpu(part->len) & ~1;
138 /* bit 0 set signifies read only partition */
139 if (be32_to_cpu(part->len) & 1)
140 (*pparts)[i].mask_flags = MTD_WRITEABLE;
142 if (names && (plen > 0)) {
143 int len = strlen(names) + 1;
145 (*pparts)[i].name = (char *)names;
149 (*pparts)[i].name = "unnamed";
158 static struct mtd_part_parser ofoldpart_parser = {
159 .owner = THIS_MODULE,
160 .parse_fn = parse_ofoldpart_partitions,
164 static int __init ofpart_parser_init(void)
167 rc = register_mtd_parser(&ofpart_parser);
171 rc = register_mtd_parser(&ofoldpart_parser);
175 deregister_mtd_parser(&ofoldpart_parser);
180 static void __exit ofpart_parser_exit(void)
182 deregister_mtd_parser(&ofpart_parser);
183 deregister_mtd_parser(&ofoldpart_parser);
186 module_init(ofpart_parser_init);
187 module_exit(ofpart_parser_exit);
189 MODULE_LICENSE("GPL");
190 MODULE_DESCRIPTION("Parser for MTD partitioning information in device tree");
191 MODULE_AUTHOR("Vitaly Wool, David Gibson");
193 * When MTD core cannot find the requested parser, it tries to load the module
194 * with the same name. Since we provide the ofoldpart parser, we should have
195 * the corresponding alias.
197 MODULE_ALIAS("ofoldpart");