blob: 4644701224931dd2860d8d332b6a1ea5adc01fa3 [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,
Brian Norrisb9adf462015-12-04 15:25:14 -080029 const struct mtd_partition **pparts,
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +040030 struct mtd_part_parser_data *data)
31{
Brian Norrisc3168d22015-12-04 15:25:13 -080032 struct mtd_partition *parts;
Michal Suchanek5cfdedb2015-08-18 15:34:09 +000033 struct device_node *mtd_node;
34 struct device_node *ofpart_node;
Scott Wood9a310d22008-01-15 17:54:43 -060035 const char *partname;
36 struct device_node *pp;
Michal Suchanek5cfdedb2015-08-18 15:34:09 +000037 int nr_parts, i, ret = 0;
38 bool dedicated = true;
Scott Wood9a310d22008-01-15 17:54:43 -060039
Dmitry Eremin-Solenikov628376f2011-05-30 01:05:33 +040040
Brian Norrise270bca2015-10-30 20:33:29 -070041 /* Pull of_node from the master device node */
42 mtd_node = mtd_get_of_node(master);
Michal Suchanek5cfdedb2015-08-18 15:34:09 +000043 if (!mtd_node)
Dmitry Eremin-Solenikov628376f2011-05-30 01:05:33 +040044 return 0;
45
Michal Suchanek5cfdedb2015-08-18 15:34:09 +000046 ofpart_node = of_get_child_by_name(mtd_node, "partitions");
47 if (!ofpart_node) {
Brian Norris8c62b4e2015-12-03 14:26:52 -080048 /*
49 * We might get here even when ofpart isn't used at all (e.g.,
50 * when using another parser), so don't be louder than
51 * KERN_DEBUG
52 */
53 pr_debug("%s: 'partitions' subnode not found on %s. Trying to parse direct subnodes as partitions.\n",
54 master->name, mtd_node->full_name);
Michal Suchanek5cfdedb2015-08-18 15:34:09 +000055 ofpart_node = mtd_node;
56 dedicated = false;
Brian Norrise488ca92015-12-03 14:47:32 -080057 } else if (!of_device_is_compatible(ofpart_node, "fixed-partitions")) {
58 /* The 'partitions' subnode might be used by another parser */
59 return 0;
Michal Suchanek5cfdedb2015-08-18 15:34:09 +000060 }
61
Scott Wood9a310d22008-01-15 17:54:43 -060062 /* First count the subnodes */
Scott Wood9a310d22008-01-15 17:54:43 -060063 nr_parts = 0;
Michal Suchanek5cfdedb2015-08-18 15:34:09 +000064 for_each_child_of_node(ofpart_node, pp) {
65 if (!dedicated && node_has_compatible(pp))
Josh Wue79265b2013-08-05 19:14:38 +080066 continue;
67
Scott Wood9a310d22008-01-15 17:54:43 -060068 nr_parts++;
Josh Wue79265b2013-08-05 19:14:38 +080069 }
Scott Wood9a310d22008-01-15 17:54:43 -060070
71 if (nr_parts == 0)
72 return 0;
73
Brian Norrisc3168d22015-12-04 15:25:13 -080074 parts = kzalloc(nr_parts * sizeof(*parts), GFP_KERNEL);
75 if (!parts)
Scott Wood9a310d22008-01-15 17:54:43 -060076 return -ENOMEM;
77
Scott Wood9a310d22008-01-15 17:54:43 -060078 i = 0;
Michal Suchanek5cfdedb2015-08-18 15:34:09 +000079 for_each_child_of_node(ofpart_node, pp) {
Ian Munsie766f2712010-10-01 17:06:08 +100080 const __be32 *reg;
Scott Wood9a310d22008-01-15 17:54:43 -060081 int len;
Joe Schaack05ff8c22013-02-21 16:29:45 -060082 int a_cells, s_cells;
Scott Wood9a310d22008-01-15 17:54:43 -060083
Michal Suchanek5cfdedb2015-08-18 15:34:09 +000084 if (!dedicated && node_has_compatible(pp))
Josh Wue79265b2013-08-05 19:14:38 +080085 continue;
86
Benjamin Krillebd5a742009-08-25 15:52:41 +020087 reg = of_get_property(pp, "reg", &len);
88 if (!reg) {
Michal Suchanek5cfdedb2015-08-18 15:34:09 +000089 if (dedicated) {
90 pr_debug("%s: ofpart partition %s (%s) missing reg property.\n",
91 master->name, pp->full_name,
92 mtd_node->full_name);
93 goto ofpart_fail;
94 } else {
95 nr_parts--;
96 continue;
97 }
Benjamin Krill4b08e142009-01-23 17:18:05 +010098 }
99
Joe Schaack05ff8c22013-02-21 16:29:45 -0600100 a_cells = of_n_addr_cells(pp);
101 s_cells = of_n_size_cells(pp);
Michal Suchanek5cfdedb2015-08-18 15:34:09 +0000102 if (len / 4 != a_cells + s_cells) {
103 pr_debug("%s: ofpart partition %s (%s) error parsing reg property.\n",
104 master->name, pp->full_name,
105 mtd_node->full_name);
106 goto ofpart_fail;
107 }
108
Brian Norrisc3168d22015-12-04 15:25:13 -0800109 parts[i].offset = of_read_number(reg, a_cells);
110 parts[i].size = of_read_number(reg + a_cells, s_cells);
Sascha Hauer42e94012017-02-09 11:50:24 +0100111 parts[i].of_node = pp;
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);
Brian Norrisc3168d22015-12-04 15:25:13 -0800116 parts[i].name = partname;
Scott Wood9a310d22008-01-15 17:54:43 -0600117
118 if (of_get_property(pp, "read-only", &len))
Brian Norrisc3168d22015-12-04 15:25:13 -0800119 parts[i].mask_flags |= MTD_WRITEABLE;
Josh Radelab0b00b2012-11-14 14:11:32 -0800120
121 if (of_get_property(pp, "lock", &len))
Brian Norrisc3168d22015-12-04 15:25:13 -0800122 parts[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
Brian Norrisc3168d22015-12-04 15:25:13 -0800130 *pparts = parts;
Scott Wood9a310d22008-01-15 17:54:43 -0600131 return nr_parts;
Michal Suchanek5cfdedb2015-08-18 15:34:09 +0000132
133ofpart_fail:
134 pr_err("%s: error parsing ofpart partition %s (%s)\n",
135 master->name, pp->full_name, mtd_node->full_name);
136 ret = -EINVAL;
137ofpart_none:
138 of_node_put(pp);
Brian Norrisc3168d22015-12-04 15:25:13 -0800139 kfree(parts);
Michal Suchanek5cfdedb2015-08-18 15:34:09 +0000140 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 = {
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400144 .parse_fn = parse_ofpart_partitions,
145 .name = "ofpart",
146};
147
Dmitry Eremin-Solenikovfbcf62a2011-05-30 01:26:17 +0400148static int parse_ofoldpart_partitions(struct mtd_info *master,
Brian Norrisb9adf462015-12-04 15:25:14 -0800149 const struct mtd_partition **pparts,
Dmitry Eremin-Solenikovfbcf62a2011-05-30 01:26:17 +0400150 struct mtd_part_parser_data *data)
151{
Brian Norrisc3168d22015-12-04 15:25:13 -0800152 struct mtd_partition *parts;
Dmitry Eremin-Solenikovfbcf62a2011-05-30 01:26:17 +0400153 struct device_node *dp;
154 int i, plen, nr_parts;
155 const struct {
156 __be32 offset, len;
157 } *part;
158 const char *names;
159
Brian Norrise270bca2015-10-30 20:33:29 -0700160 /* Pull of_node from the master device node */
161 dp = mtd_get_of_node(master);
Dmitry Eremin-Solenikovfbcf62a2011-05-30 01:26:17 +0400162 if (!dp)
163 return 0;
164
165 part = of_get_property(dp, "partitions", &plen);
166 if (!part)
167 return 0; /* No partitions found */
168
169 pr_warning("Device tree uses obsolete partition map binding: %s\n",
170 dp->full_name);
171
172 nr_parts = plen / sizeof(part[0]);
173
Brian Norrisc3168d22015-12-04 15:25:13 -0800174 parts = kzalloc(nr_parts * sizeof(*parts), GFP_KERNEL);
175 if (!parts)
Dmitry Eremin-Solenikovfbcf62a2011-05-30 01:26:17 +0400176 return -ENOMEM;
177
178 names = of_get_property(dp, "partition-names", &plen);
179
180 for (i = 0; i < nr_parts; i++) {
Brian Norrisc3168d22015-12-04 15:25:13 -0800181 parts[i].offset = be32_to_cpu(part->offset);
182 parts[i].size = be32_to_cpu(part->len) & ~1;
Dmitry Eremin-Solenikovfbcf62a2011-05-30 01:26:17 +0400183 /* bit 0 set signifies read only partition */
184 if (be32_to_cpu(part->len) & 1)
Brian Norrisc3168d22015-12-04 15:25:13 -0800185 parts[i].mask_flags = MTD_WRITEABLE;
Dmitry Eremin-Solenikovfbcf62a2011-05-30 01:26:17 +0400186
187 if (names && (plen > 0)) {
188 int len = strlen(names) + 1;
189
Brian Norrisc3168d22015-12-04 15:25:13 -0800190 parts[i].name = names;
Dmitry Eremin-Solenikovfbcf62a2011-05-30 01:26:17 +0400191 plen -= len;
192 names += len;
193 } else {
Brian Norrisc3168d22015-12-04 15:25:13 -0800194 parts[i].name = "unnamed";
Dmitry Eremin-Solenikovfbcf62a2011-05-30 01:26:17 +0400195 }
196
197 part++;
198 }
199
Brian Norrisc3168d22015-12-04 15:25:13 -0800200 *pparts = parts;
Dmitry Eremin-Solenikovfbcf62a2011-05-30 01:26:17 +0400201 return nr_parts;
202}
203
204static struct mtd_part_parser ofoldpart_parser = {
Dmitry Eremin-Solenikovfbcf62a2011-05-30 01:26:17 +0400205 .parse_fn = parse_ofoldpart_partitions,
206 .name = "ofoldpart",
207};
208
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400209static int __init ofpart_parser_init(void)
210{
Axel Lin6e14a612013-12-01 19:01:06 +0800211 register_mtd_parser(&ofpart_parser);
212 register_mtd_parser(&ofoldpart_parser);
213 return 0;
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400214}
215
Lubomir Rintel422f3892013-01-16 02:12:50 +0100216static void __exit ofpart_parser_exit(void)
217{
218 deregister_mtd_parser(&ofpart_parser);
219 deregister_mtd_parser(&ofoldpart_parser);
220}
221
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400222module_init(ofpart_parser_init);
Lubomir Rintel422f3892013-01-16 02:12:50 +0100223module_exit(ofpart_parser_exit);
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400224
Adrian Bunk950bcb22008-04-14 17:19:46 +0300225MODULE_LICENSE("GPL");
Dmitry Eremin-Solenikovd6137ba2011-06-27 01:02:59 +0400226MODULE_DESCRIPTION("Parser for MTD partitioning information in device tree");
227MODULE_AUTHOR("Vitaly Wool, David Gibson");
Dmitry Eremin-Solenikov9786f6e2011-06-27 16:34:46 +0400228/*
229 * When MTD core cannot find the requested parser, it tries to load the module
230 * with the same name. Since we provide the ofoldpart parser, we should have
231 * the corresponding alias.
232 */
233MODULE_ALIAS("ofoldpart");