blob: 7c2c926e0bd7a6a9da8f73c704b08314de63f053 [file] [log] [blame]
Scott Wood9a310d22008-01-15 17:54:43 -06001/*
2 * Flash partitions described by the OF (or flattened) device tree
3 *
David Woodhousea1452a32010-08-08 20:58:20 +01004 * Copyright © 2006 MontaVista Software Inc.
Scott Wood9a310d22008-01-15 17:54:43 -06005 * Author: Vitaly Wool <vwool@ru.mvista.com>
6 *
7 * Revised to handle newer style flash binding by:
David Woodhousea1452a32010-08-08 20:58:20 +01008 * Copyright © 2007 David Gibson, IBM Corporation.
Scott Wood9a310d22008-01-15 17:54:43 -06009 *
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Scott Wood9a310d22008-01-15 17:54:43 -060021#include <linux/mtd/partitions.h>
22
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +040023static int parse_ofpart_partitions(struct mtd_info *master,
24 struct mtd_partition **pparts,
25 struct mtd_part_parser_data *data)
26{
27 if (!data || !data->of_node)
28 return 0;
29
30 return of_mtd_parse_partitions(NULL, data->of_node, pparts);
31}
32
33int of_mtd_parse_partitions(struct device *dev,
Scott Wood9a310d22008-01-15 17:54:43 -060034 struct device_node *node,
35 struct mtd_partition **pparts)
36{
37 const char *partname;
38 struct device_node *pp;
39 int nr_parts, i;
40
41 /* First count the subnodes */
42 pp = NULL;
43 nr_parts = 0;
44 while ((pp = of_get_next_child(node, pp)))
45 nr_parts++;
46
47 if (nr_parts == 0)
48 return 0;
49
50 *pparts = kzalloc(nr_parts * sizeof(**pparts), GFP_KERNEL);
51 if (!*pparts)
52 return -ENOMEM;
53
54 pp = NULL;
55 i = 0;
56 while ((pp = of_get_next_child(node, pp))) {
Ian Munsie766f2712010-10-01 17:06:08 +100057 const __be32 *reg;
Scott Wood9a310d22008-01-15 17:54:43 -060058 int len;
59
Benjamin Krillebd5a742009-08-25 15:52:41 +020060 reg = of_get_property(pp, "reg", &len);
61 if (!reg) {
Benjamin Krill4b08e142009-01-23 17:18:05 +010062 nr_parts--;
63 continue;
64 }
65
Ian Munsie766f2712010-10-01 17:06:08 +100066 (*pparts)[i].offset = be32_to_cpu(reg[0]);
67 (*pparts)[i].size = be32_to_cpu(reg[1]);
Scott Wood9a310d22008-01-15 17:54:43 -060068
69 partname = of_get_property(pp, "label", &len);
70 if (!partname)
71 partname = of_get_property(pp, "name", &len);
72 (*pparts)[i].name = (char *)partname;
73
74 if (of_get_property(pp, "read-only", &len))
75 (*pparts)[i].mask_flags = MTD_WRITEABLE;
76
77 i++;
78 }
79
Benjamin Krillebd5a742009-08-25 15:52:41 +020080 if (!i) {
81 of_node_put(pp);
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +040082 pr_err("No valid partition found on %s\n", node->full_name);
Benjamin Krillebd5a742009-08-25 15:52:41 +020083 kfree(*pparts);
84 *pparts = NULL;
85 return -EINVAL;
86 }
87
Scott Wood9a310d22008-01-15 17:54:43 -060088 return nr_parts;
89}
90EXPORT_SYMBOL(of_mtd_parse_partitions);
Adrian Bunk950bcb22008-04-14 17:19:46 +030091
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +040092static struct mtd_part_parser ofpart_parser = {
93 .owner = THIS_MODULE,
94 .parse_fn = parse_ofpart_partitions,
95 .name = "ofpart",
96};
97
98static int __init ofpart_parser_init(void)
99{
100 return register_mtd_parser(&ofpart_parser);
101}
102
103module_init(ofpart_parser_init);
104
Adrian Bunk950bcb22008-04-14 17:19:46 +0300105MODULE_LICENSE("GPL");