blob: 9ed6038e47d210df74ec678de7cf8220797bf815 [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
Josh Wue79265b2013-08-05 19:14:38 +080023static bool node_has_compatible(struct device_node *pp)
24{
25 return of_get_property(pp, "compatible", NULL);
26}
27
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +040028static int parse_ofpart_partitions(struct mtd_info *master,
29 struct mtd_partition **pparts,
30 struct mtd_part_parser_data *data)
31{
Michal Suchanek5cfdedb2015-08-18 15:34:09 +000032 struct device_node *mtd_node;
33 struct device_node *ofpart_node;
Scott Wood9a310d22008-01-15 17:54:43 -060034 const char *partname;
35 struct device_node *pp;
Michal Suchanek5cfdedb2015-08-18 15:34:09 +000036 int nr_parts, i, ret = 0;
37 bool dedicated = true;
Scott Wood9a310d22008-01-15 17:54:43 -060038
Dmitry Eremin-Solenikov628376f2011-05-30 01:05:33 +040039
40 if (!data)
41 return 0;
42
Michal Suchanek5cfdedb2015-08-18 15:34:09 +000043 mtd_node = data->of_node;
44 if (!mtd_node)
Dmitry Eremin-Solenikov628376f2011-05-30 01:05:33 +040045 return 0;
46
Michal Suchanek5cfdedb2015-08-18 15:34:09 +000047 ofpart_node = of_get_child_by_name(mtd_node, "partitions");
48 if (!ofpart_node) {
Brian Norris8c62b4e2015-12-03 14:26:52 -080049 /*
50 * We might get here even when ofpart isn't used at all (e.g.,
51 * when using another parser), so don't be louder than
52 * KERN_DEBUG
53 */
54 pr_debug("%s: 'partitions' subnode not found on %s. Trying to parse direct subnodes as partitions.\n",
55 master->name, mtd_node->full_name);
Michal Suchanek5cfdedb2015-08-18 15:34:09 +000056 ofpart_node = mtd_node;
57 dedicated = false;
Brian Norrise488ca92015-12-03 14:47:32 -080058 } else if (!of_device_is_compatible(ofpart_node, "fixed-partitions")) {
59 /* The 'partitions' subnode might be used by another parser */
60 return 0;
Michal Suchanek5cfdedb2015-08-18 15:34:09 +000061 }
62
Scott Wood9a310d22008-01-15 17:54:43 -060063 /* First count the subnodes */
Scott Wood9a310d22008-01-15 17:54:43 -060064 nr_parts = 0;
Michal Suchanek5cfdedb2015-08-18 15:34:09 +000065 for_each_child_of_node(ofpart_node, pp) {
66 if (!dedicated && node_has_compatible(pp))
Josh Wue79265b2013-08-05 19:14:38 +080067 continue;
68
Scott Wood9a310d22008-01-15 17:54:43 -060069 nr_parts++;
Josh Wue79265b2013-08-05 19:14:38 +080070 }
Scott Wood9a310d22008-01-15 17:54:43 -060071
72 if (nr_parts == 0)
73 return 0;
74
75 *pparts = kzalloc(nr_parts * sizeof(**pparts), GFP_KERNEL);
76 if (!*pparts)
77 return -ENOMEM;
78
Scott Wood9a310d22008-01-15 17:54:43 -060079 i = 0;
Michal Suchanek5cfdedb2015-08-18 15:34:09 +000080 for_each_child_of_node(ofpart_node, pp) {
Ian Munsie766f2712010-10-01 17:06:08 +100081 const __be32 *reg;
Scott Wood9a310d22008-01-15 17:54:43 -060082 int len;
Joe Schaack05ff8c22013-02-21 16:29:45 -060083 int a_cells, s_cells;
Scott Wood9a310d22008-01-15 17:54:43 -060084
Michal Suchanek5cfdedb2015-08-18 15:34:09 +000085 if (!dedicated && node_has_compatible(pp))
Josh Wue79265b2013-08-05 19:14:38 +080086 continue;
87
Benjamin Krillebd5a742009-08-25 15:52:41 +020088 reg = of_get_property(pp, "reg", &len);
89 if (!reg) {
Michal Suchanek5cfdedb2015-08-18 15:34:09 +000090 if (dedicated) {
91 pr_debug("%s: ofpart partition %s (%s) missing reg property.\n",
92 master->name, pp->full_name,
93 mtd_node->full_name);
94 goto ofpart_fail;
95 } else {
96 nr_parts--;
97 continue;
98 }
Benjamin Krill4b08e142009-01-23 17:18:05 +010099 }
100
Joe Schaack05ff8c22013-02-21 16:29:45 -0600101 a_cells = of_n_addr_cells(pp);
102 s_cells = of_n_size_cells(pp);
Michal Suchanek5cfdedb2015-08-18 15:34:09 +0000103 if (len / 4 != a_cells + s_cells) {
104 pr_debug("%s: ofpart partition %s (%s) error parsing reg property.\n",
105 master->name, pp->full_name,
106 mtd_node->full_name);
107 goto ofpart_fail;
108 }
109
Joe Schaack05ff8c22013-02-21 16:29:45 -0600110 (*pparts)[i].offset = of_read_number(reg, a_cells);
111 (*pparts)[i].size = of_read_number(reg + a_cells, s_cells);
Scott Wood9a310d22008-01-15 17:54:43 -0600112
113 partname = of_get_property(pp, "label", &len);
114 if (!partname)
115 partname = of_get_property(pp, "name", &len);
Geert Uytterhoeven26a6d242013-11-12 20:11:26 +0100116 (*pparts)[i].name = partname;
Scott Wood9a310d22008-01-15 17:54:43 -0600117
118 if (of_get_property(pp, "read-only", &len))
Josh Radelab0b00b2012-11-14 14:11:32 -0800119 (*pparts)[i].mask_flags |= MTD_WRITEABLE;
120
121 if (of_get_property(pp, "lock", &len))
122 (*pparts)[i].mask_flags |= MTD_POWERUP_LOCK;
Scott Wood9a310d22008-01-15 17:54:43 -0600123
124 i++;
125 }
126
Michal Suchanek5cfdedb2015-08-18 15:34:09 +0000127 if (!nr_parts)
128 goto ofpart_none;
Benjamin Krillebd5a742009-08-25 15:52:41 +0200129
Scott Wood9a310d22008-01-15 17:54:43 -0600130 return nr_parts;
Michal Suchanek5cfdedb2015-08-18 15:34:09 +0000131
132ofpart_fail:
133 pr_err("%s: error parsing ofpart partition %s (%s)\n",
134 master->name, pp->full_name, mtd_node->full_name);
135 ret = -EINVAL;
136ofpart_none:
137 of_node_put(pp);
138 kfree(*pparts);
139 *pparts = NULL;
140 return ret;
Scott Wood9a310d22008-01-15 17:54:43 -0600141}
Adrian Bunk950bcb22008-04-14 17:19:46 +0300142
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400143static struct mtd_part_parser ofpart_parser = {
144 .owner = THIS_MODULE,
145 .parse_fn = parse_ofpart_partitions,
146 .name = "ofpart",
147};
148
Dmitry Eremin-Solenikovfbcf62a2011-05-30 01:26:17 +0400149static int parse_ofoldpart_partitions(struct mtd_info *master,
150 struct mtd_partition **pparts,
151 struct mtd_part_parser_data *data)
152{
153 struct device_node *dp;
154 int i, plen, nr_parts;
155 const struct {
156 __be32 offset, len;
157 } *part;
158 const char *names;
159
160 if (!data)
161 return 0;
162
163 dp = data->of_node;
164 if (!dp)
165 return 0;
166
167 part = of_get_property(dp, "partitions", &plen);
168 if (!part)
169 return 0; /* No partitions found */
170
171 pr_warning("Device tree uses obsolete partition map binding: %s\n",
172 dp->full_name);
173
174 nr_parts = plen / sizeof(part[0]);
175
176 *pparts = kzalloc(nr_parts * sizeof(*(*pparts)), GFP_KERNEL);
Sachin Kamatecbcbc72012-09-25 15:27:13 +0530177 if (!*pparts)
Dmitry Eremin-Solenikovfbcf62a2011-05-30 01:26:17 +0400178 return -ENOMEM;
179
180 names = of_get_property(dp, "partition-names", &plen);
181
182 for (i = 0; i < nr_parts; i++) {
183 (*pparts)[i].offset = be32_to_cpu(part->offset);
184 (*pparts)[i].size = be32_to_cpu(part->len) & ~1;
185 /* bit 0 set signifies read only partition */
186 if (be32_to_cpu(part->len) & 1)
187 (*pparts)[i].mask_flags = MTD_WRITEABLE;
188
189 if (names && (plen > 0)) {
190 int len = strlen(names) + 1;
191
Geert Uytterhoeven26a6d242013-11-12 20:11:26 +0100192 (*pparts)[i].name = names;
Dmitry Eremin-Solenikovfbcf62a2011-05-30 01:26:17 +0400193 plen -= len;
194 names += len;
195 } else {
196 (*pparts)[i].name = "unnamed";
197 }
198
199 part++;
200 }
201
202 return nr_parts;
203}
204
205static struct mtd_part_parser ofoldpart_parser = {
206 .owner = THIS_MODULE,
207 .parse_fn = parse_ofoldpart_partitions,
208 .name = "ofoldpart",
209};
210
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400211static int __init ofpart_parser_init(void)
212{
Axel Lin6e14a612013-12-01 19:01:06 +0800213 register_mtd_parser(&ofpart_parser);
214 register_mtd_parser(&ofoldpart_parser);
215 return 0;
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400216}
217
Lubomir Rintel422f3892013-01-16 02:12:50 +0100218static void __exit ofpart_parser_exit(void)
219{
220 deregister_mtd_parser(&ofpart_parser);
221 deregister_mtd_parser(&ofoldpart_parser);
222}
223
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400224module_init(ofpart_parser_init);
Lubomir Rintel422f3892013-01-16 02:12:50 +0100225module_exit(ofpart_parser_exit);
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400226
Adrian Bunk950bcb22008-04-14 17:19:46 +0300227MODULE_LICENSE("GPL");
Dmitry Eremin-Solenikovd6137ba2011-06-27 01:02:59 +0400228MODULE_DESCRIPTION("Parser for MTD partitioning information in device tree");
229MODULE_AUTHOR("Vitaly Wool, David Gibson");
Dmitry Eremin-Solenikov9786f6e2011-06-27 16:34:46 +0400230/*
231 * When MTD core cannot find the requested parser, it tries to load the module
232 * with the same name. Since we provide the ofoldpart parser, we should have
233 * the corresponding alias.
234 */
235MODULE_ALIAS("ofoldpart");