Merge remote-tracking branches 'asoc/topic/fsl', 'asoc/topic/fsl-card', 'asoc/topic...
[linux-2.6-block.git] / drivers / mtd / ofpart.c
CommitLineData
9a310d21
SW
1/*
2 * Flash partitions described by the OF (or flattened) device tree
3 *
a1452a37 4 * Copyright © 2006 MontaVista Software Inc.
9a310d21
SW
5 * Author: Vitaly Wool <vwool@ru.mvista.com>
6 *
7 * Revised to handle newer style flash binding by:
a1452a37 8 * Copyright © 2007 David Gibson, IBM Corporation.
9a310d21
SW
9 *
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>
5a0e3ad6 20#include <linux/slab.h>
9a310d21
SW
21#include <linux/mtd/partitions.h>
22
e79265ba
JW
23static bool node_has_compatible(struct device_node *pp)
24{
25 return of_get_property(pp, "compatible", NULL);
26}
27
d26c87d6
DES
28static int parse_ofpart_partitions(struct mtd_info *master,
29 struct mtd_partition **pparts,
30 struct mtd_part_parser_data *data)
31{
628376fb 32 struct device_node *node;
9a310d21
SW
33 const char *partname;
34 struct device_node *pp;
35 int nr_parts, i;
36
628376fb
DES
37
38 if (!data)
39 return 0;
40
41 node = data->of_node;
42 if (!node)
43 return 0;
44
9a310d21 45 /* First count the subnodes */
9a310d21 46 nr_parts = 0;
60ea89e2 47 for_each_child_of_node(node, pp) {
e79265ba
JW
48 if (node_has_compatible(pp))
49 continue;
50
9a310d21 51 nr_parts++;
e79265ba 52 }
9a310d21
SW
53
54 if (nr_parts == 0)
55 return 0;
56
57 *pparts = kzalloc(nr_parts * sizeof(**pparts), GFP_KERNEL);
58 if (!*pparts)
59 return -ENOMEM;
60
9a310d21 61 i = 0;
60ea89e2 62 for_each_child_of_node(node, pp) {
766f271a 63 const __be32 *reg;
9a310d21 64 int len;
05ff8c25 65 int a_cells, s_cells;
9a310d21 66
e79265ba
JW
67 if (node_has_compatible(pp))
68 continue;
69
ebd5a74d
BK
70 reg = of_get_property(pp, "reg", &len);
71 if (!reg) {
4b08e149
BK
72 nr_parts--;
73 continue;
74 }
75
05ff8c25
JS
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);
9a310d21
SW
80
81 partname = of_get_property(pp, "label", &len);
82 if (!partname)
83 partname = of_get_property(pp, "name", &len);
26a6d240 84 (*pparts)[i].name = partname;
9a310d21
SW
85
86 if (of_get_property(pp, "read-only", &len))
ab0b00bc
JR
87 (*pparts)[i].mask_flags |= MTD_WRITEABLE;
88
89 if (of_get_property(pp, "lock", &len))
90 (*pparts)[i].mask_flags |= MTD_POWERUP_LOCK;
9a310d21
SW
91
92 i++;
93 }
94
ebd5a74d
BK
95 if (!i) {
96 of_node_put(pp);
d26c87d6 97 pr_err("No valid partition found on %s\n", node->full_name);
ebd5a74d
BK
98 kfree(*pparts);
99 *pparts = NULL;
100 return -EINVAL;
101 }
102
9a310d21
SW
103 return nr_parts;
104}
950bcb25 105
d26c87d6
DES
106static struct mtd_part_parser ofpart_parser = {
107 .owner = THIS_MODULE,
108 .parse_fn = parse_ofpart_partitions,
109 .name = "ofpart",
110};
111
fbcf62a3
DES
112static int parse_ofoldpart_partitions(struct mtd_info *master,
113 struct mtd_partition **pparts,
114 struct mtd_part_parser_data *data)
115{
116 struct device_node *dp;
117 int i, plen, nr_parts;
118 const struct {
119 __be32 offset, len;
120 } *part;
121 const char *names;
122
123 if (!data)
124 return 0;
125
126 dp = data->of_node;
127 if (!dp)
128 return 0;
129
130 part = of_get_property(dp, "partitions", &plen);
131 if (!part)
132 return 0; /* No partitions found */
133
134 pr_warning("Device tree uses obsolete partition map binding: %s\n",
135 dp->full_name);
136
137 nr_parts = plen / sizeof(part[0]);
138
139 *pparts = kzalloc(nr_parts * sizeof(*(*pparts)), GFP_KERNEL);
ecbcbc7b 140 if (!*pparts)
fbcf62a3
DES
141 return -ENOMEM;
142
143 names = of_get_property(dp, "partition-names", &plen);
144
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;
151
152 if (names && (plen > 0)) {
153 int len = strlen(names) + 1;
154
26a6d240 155 (*pparts)[i].name = names;
fbcf62a3
DES
156 plen -= len;
157 names += len;
158 } else {
159 (*pparts)[i].name = "unnamed";
160 }
161
162 part++;
163 }
164
165 return nr_parts;
166}
167
168static struct mtd_part_parser ofoldpart_parser = {
169 .owner = THIS_MODULE,
170 .parse_fn = parse_ofoldpart_partitions,
171 .name = "ofoldpart",
172};
173
d26c87d6
DES
174static int __init ofpart_parser_init(void)
175{
6e14a61d
AL
176 register_mtd_parser(&ofpart_parser);
177 register_mtd_parser(&ofoldpart_parser);
178 return 0;
d26c87d6
DES
179}
180
422f3890
LR
181static void __exit ofpart_parser_exit(void)
182{
183 deregister_mtd_parser(&ofpart_parser);
184 deregister_mtd_parser(&ofoldpart_parser);
185}
186
d26c87d6 187module_init(ofpart_parser_init);
422f3890 188module_exit(ofpart_parser_exit);
d26c87d6 189
950bcb25 190MODULE_LICENSE("GPL");
d6137bad
DES
191MODULE_DESCRIPTION("Parser for MTD partitioning information in device tree");
192MODULE_AUTHOR("Vitaly Wool, David Gibson");
9786f6e6
DES
193/*
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.
197 */
198MODULE_ALIAS("ofoldpart");