blob: 7843a4491217954f7549524a52ed2e1b133302e9 [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{
Dmitry Eremin-Solenikov628376f2011-05-30 01:05:33 +040032 struct device_node *node;
Scott Wood9a310d22008-01-15 17:54:43 -060033 const char *partname;
34 struct device_node *pp;
35 int nr_parts, i;
36
Dmitry Eremin-Solenikov628376f2011-05-30 01:05:33 +040037
38 if (!data)
39 return 0;
40
41 node = data->of_node;
42 if (!node)
43 return 0;
44
Scott Wood9a310d22008-01-15 17:54:43 -060045 /* First count the subnodes */
46 pp = NULL;
47 nr_parts = 0;
Josh Wue79265b2013-08-05 19:14:38 +080048 while ((pp = of_get_next_child(node, pp))) {
49 if (node_has_compatible(pp))
50 continue;
51
Scott Wood9a310d22008-01-15 17:54:43 -060052 nr_parts++;
Josh Wue79265b2013-08-05 19:14:38 +080053 }
Scott Wood9a310d22008-01-15 17:54:43 -060054
55 if (nr_parts == 0)
56 return 0;
57
58 *pparts = kzalloc(nr_parts * sizeof(**pparts), GFP_KERNEL);
59 if (!*pparts)
60 return -ENOMEM;
61
62 pp = NULL;
63 i = 0;
64 while ((pp = of_get_next_child(node, pp))) {
Ian Munsie766f2712010-10-01 17:06:08 +100065 const __be32 *reg;
Scott Wood9a310d22008-01-15 17:54:43 -060066 int len;
Joe Schaack05ff8c22013-02-21 16:29:45 -060067 int a_cells, s_cells;
Scott Wood9a310d22008-01-15 17:54:43 -060068
Josh Wue79265b2013-08-05 19:14:38 +080069 if (node_has_compatible(pp))
70 continue;
71
Benjamin Krillebd5a742009-08-25 15:52:41 +020072 reg = of_get_property(pp, "reg", &len);
73 if (!reg) {
Benjamin Krill4b08e142009-01-23 17:18:05 +010074 nr_parts--;
75 continue;
76 }
77
Joe Schaack05ff8c22013-02-21 16:29:45 -060078 a_cells = of_n_addr_cells(pp);
79 s_cells = of_n_size_cells(pp);
80 (*pparts)[i].offset = of_read_number(reg, a_cells);
81 (*pparts)[i].size = of_read_number(reg + a_cells, s_cells);
Scott Wood9a310d22008-01-15 17:54:43 -060082
83 partname = of_get_property(pp, "label", &len);
84 if (!partname)
85 partname = of_get_property(pp, "name", &len);
86 (*pparts)[i].name = (char *)partname;
87
88 if (of_get_property(pp, "read-only", &len))
Josh Radelab0b00b2012-11-14 14:11:32 -080089 (*pparts)[i].mask_flags |= MTD_WRITEABLE;
90
91 if (of_get_property(pp, "lock", &len))
92 (*pparts)[i].mask_flags |= MTD_POWERUP_LOCK;
Scott Wood9a310d22008-01-15 17:54:43 -060093
94 i++;
95 }
96
Benjamin Krillebd5a742009-08-25 15:52:41 +020097 if (!i) {
98 of_node_put(pp);
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +040099 pr_err("No valid partition found on %s\n", node->full_name);
Benjamin Krillebd5a742009-08-25 15:52:41 +0200100 kfree(*pparts);
101 *pparts = NULL;
102 return -EINVAL;
103 }
104
Scott Wood9a310d22008-01-15 17:54:43 -0600105 return nr_parts;
106}
Adrian Bunk950bcb22008-04-14 17:19:46 +0300107
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400108static struct mtd_part_parser ofpart_parser = {
109 .owner = THIS_MODULE,
110 .parse_fn = parse_ofpart_partitions,
111 .name = "ofpart",
112};
113
Dmitry Eremin-Solenikovfbcf62a2011-05-30 01:26:17 +0400114static int parse_ofoldpart_partitions(struct mtd_info *master,
115 struct mtd_partition **pparts,
116 struct mtd_part_parser_data *data)
117{
118 struct device_node *dp;
119 int i, plen, nr_parts;
120 const struct {
121 __be32 offset, len;
122 } *part;
123 const char *names;
124
125 if (!data)
126 return 0;
127
128 dp = data->of_node;
129 if (!dp)
130 return 0;
131
132 part = of_get_property(dp, "partitions", &plen);
133 if (!part)
134 return 0; /* No partitions found */
135
136 pr_warning("Device tree uses obsolete partition map binding: %s\n",
137 dp->full_name);
138
139 nr_parts = plen / sizeof(part[0]);
140
141 *pparts = kzalloc(nr_parts * sizeof(*(*pparts)), GFP_KERNEL);
Sachin Kamatecbcbc72012-09-25 15:27:13 +0530142 if (!*pparts)
Dmitry Eremin-Solenikovfbcf62a2011-05-30 01:26:17 +0400143 return -ENOMEM;
144
145 names = of_get_property(dp, "partition-names", &plen);
146
147 for (i = 0; i < nr_parts; i++) {
148 (*pparts)[i].offset = be32_to_cpu(part->offset);
149 (*pparts)[i].size = be32_to_cpu(part->len) & ~1;
150 /* bit 0 set signifies read only partition */
151 if (be32_to_cpu(part->len) & 1)
152 (*pparts)[i].mask_flags = MTD_WRITEABLE;
153
154 if (names && (plen > 0)) {
155 int len = strlen(names) + 1;
156
157 (*pparts)[i].name = (char *)names;
158 plen -= len;
159 names += len;
160 } else {
161 (*pparts)[i].name = "unnamed";
162 }
163
164 part++;
165 }
166
167 return nr_parts;
168}
169
170static struct mtd_part_parser ofoldpart_parser = {
171 .owner = THIS_MODULE,
172 .parse_fn = parse_ofoldpart_partitions,
173 .name = "ofoldpart",
174};
175
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400176static int __init ofpart_parser_init(void)
177{
Dmitry Eremin-Solenikovfbcf62a2011-05-30 01:26:17 +0400178 int rc;
179 rc = register_mtd_parser(&ofpart_parser);
180 if (rc)
181 goto out;
182
183 rc = register_mtd_parser(&ofoldpart_parser);
184 if (!rc)
185 return 0;
186
187 deregister_mtd_parser(&ofoldpart_parser);
188out:
189 return rc;
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400190}
191
Lubomir Rintel422f3892013-01-16 02:12:50 +0100192static void __exit ofpart_parser_exit(void)
193{
194 deregister_mtd_parser(&ofpart_parser);
195 deregister_mtd_parser(&ofoldpart_parser);
196}
197
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400198module_init(ofpart_parser_init);
Lubomir Rintel422f3892013-01-16 02:12:50 +0100199module_exit(ofpart_parser_exit);
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400200
Adrian Bunk950bcb22008-04-14 17:19:46 +0300201MODULE_LICENSE("GPL");
Dmitry Eremin-Solenikovd6137ba2011-06-27 01:02:59 +0400202MODULE_DESCRIPTION("Parser for MTD partitioning information in device tree");
203MODULE_AUTHOR("Vitaly Wool, David Gibson");
Dmitry Eremin-Solenikov9786f6e2011-06-27 16:34:46 +0400204/*
205 * When MTD core cannot find the requested parser, it tries to load the module
206 * with the same name. Since we provide the ofoldpart parser, we should have
207 * the corresponding alias.
208 */
209MODULE_ALIAS("ofoldpart");