blob: 553d6d6d560322c4bcafa44a184e6c09e864b621 [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{
Dmitry Eremin-Solenikov628376f2011-05-30 01:05:33 +040027 struct device_node *node;
Scott Wood9a310d22008-01-15 17:54:43 -060028 const char *partname;
29 struct device_node *pp;
30 int nr_parts, i;
31
Dmitry Eremin-Solenikov628376f2011-05-30 01:05:33 +040032
33 if (!data)
34 return 0;
35
36 node = data->of_node;
37 if (!node)
38 return 0;
39
Scott Wood9a310d22008-01-15 17:54:43 -060040 /* First count the subnodes */
41 pp = NULL;
42 nr_parts = 0;
43 while ((pp = of_get_next_child(node, pp)))
44 nr_parts++;
45
46 if (nr_parts == 0)
47 return 0;
48
49 *pparts = kzalloc(nr_parts * sizeof(**pparts), GFP_KERNEL);
50 if (!*pparts)
51 return -ENOMEM;
52
53 pp = NULL;
54 i = 0;
55 while ((pp = of_get_next_child(node, pp))) {
Ian Munsie766f2712010-10-01 17:06:08 +100056 const __be32 *reg;
Scott Wood9a310d22008-01-15 17:54:43 -060057 int len;
Joe Schaack05ff8c22013-02-21 16:29:45 -060058 int a_cells, s_cells;
Scott Wood9a310d22008-01-15 17:54:43 -060059
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
Joe Schaack05ff8c22013-02-21 16:29:45 -060066 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);
Scott Wood9a310d22008-01-15 17:54:43 -060070
71 partname = of_get_property(pp, "label", &len);
72 if (!partname)
73 partname = of_get_property(pp, "name", &len);
74 (*pparts)[i].name = (char *)partname;
75
76 if (of_get_property(pp, "read-only", &len))
Josh Radelab0b00b2012-11-14 14:11:32 -080077 (*pparts)[i].mask_flags |= MTD_WRITEABLE;
78
79 if (of_get_property(pp, "lock", &len))
80 (*pparts)[i].mask_flags |= MTD_POWERUP_LOCK;
Scott Wood9a310d22008-01-15 17:54:43 -060081
82 i++;
83 }
84
Benjamin Krillebd5a742009-08-25 15:52:41 +020085 if (!i) {
86 of_node_put(pp);
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +040087 pr_err("No valid partition found on %s\n", node->full_name);
Benjamin Krillebd5a742009-08-25 15:52:41 +020088 kfree(*pparts);
89 *pparts = NULL;
90 return -EINVAL;
91 }
92
Scott Wood9a310d22008-01-15 17:54:43 -060093 return nr_parts;
94}
Adrian Bunk950bcb22008-04-14 17:19:46 +030095
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +040096static struct mtd_part_parser ofpart_parser = {
97 .owner = THIS_MODULE,
98 .parse_fn = parse_ofpart_partitions,
99 .name = "ofpart",
100};
101
Dmitry Eremin-Solenikovfbcf62a2011-05-30 01:26:17 +0400102static int parse_ofoldpart_partitions(struct mtd_info *master,
103 struct mtd_partition **pparts,
104 struct mtd_part_parser_data *data)
105{
106 struct device_node *dp;
107 int i, plen, nr_parts;
108 const struct {
109 __be32 offset, len;
110 } *part;
111 const char *names;
112
113 if (!data)
114 return 0;
115
116 dp = data->of_node;
117 if (!dp)
118 return 0;
119
120 part = of_get_property(dp, "partitions", &plen);
121 if (!part)
122 return 0; /* No partitions found */
123
124 pr_warning("Device tree uses obsolete partition map binding: %s\n",
125 dp->full_name);
126
127 nr_parts = plen / sizeof(part[0]);
128
129 *pparts = kzalloc(nr_parts * sizeof(*(*pparts)), GFP_KERNEL);
Sachin Kamatecbcbc72012-09-25 15:27:13 +0530130 if (!*pparts)
Dmitry Eremin-Solenikovfbcf62a2011-05-30 01:26:17 +0400131 return -ENOMEM;
132
133 names = of_get_property(dp, "partition-names", &plen);
134
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;
141
142 if (names && (plen > 0)) {
143 int len = strlen(names) + 1;
144
145 (*pparts)[i].name = (char *)names;
146 plen -= len;
147 names += len;
148 } else {
149 (*pparts)[i].name = "unnamed";
150 }
151
152 part++;
153 }
154
155 return nr_parts;
156}
157
158static struct mtd_part_parser ofoldpart_parser = {
159 .owner = THIS_MODULE,
160 .parse_fn = parse_ofoldpart_partitions,
161 .name = "ofoldpart",
162};
163
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400164static int __init ofpart_parser_init(void)
165{
Dmitry Eremin-Solenikovfbcf62a2011-05-30 01:26:17 +0400166 int rc;
167 rc = register_mtd_parser(&ofpart_parser);
168 if (rc)
169 goto out;
170
171 rc = register_mtd_parser(&ofoldpart_parser);
172 if (!rc)
173 return 0;
174
175 deregister_mtd_parser(&ofoldpart_parser);
176out:
177 return rc;
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400178}
179
Lubomir Rintel422f3892013-01-16 02:12:50 +0100180static void __exit ofpart_parser_exit(void)
181{
182 deregister_mtd_parser(&ofpart_parser);
183 deregister_mtd_parser(&ofoldpart_parser);
184}
185
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400186module_init(ofpart_parser_init);
Lubomir Rintel422f3892013-01-16 02:12:50 +0100187module_exit(ofpart_parser_exit);
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400188
Adrian Bunk950bcb22008-04-14 17:19:46 +0300189MODULE_LICENSE("GPL");
Dmitry Eremin-Solenikovd6137ba2011-06-27 01:02:59 +0400190MODULE_DESCRIPTION("Parser for MTD partitioning information in device tree");
191MODULE_AUTHOR("Vitaly Wool, David Gibson");
Dmitry Eremin-Solenikov9786f6e2011-06-27 16:34:46 +0400192/*
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.
196 */
197MODULE_ALIAS("ofoldpart");