blob: 3caeabf27987c61fa15e709a74b873392c9b3a53 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Scott Wood9a310d22008-01-15 17:54:43 -06002/*
3 * Flash partitions described by the OF (or flattened) device tree
4 *
David Woodhousea1452a32010-08-08 20:58:20 +01005 * Copyright © 2006 MontaVista Software Inc.
Scott Wood9a310d22008-01-15 17:54:43 -06006 * Author: Vitaly Wool <vwool@ru.mvista.com>
7 *
8 * Revised to handle newer style flash binding by:
David Woodhousea1452a32010-08-08 20:58:20 +01009 * Copyright © 2007 David Gibson, IBM Corporation.
Scott Wood9a310d22008-01-15 17:54:43 -060010 */
11
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/of.h>
15#include <linux/mtd/mtd.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Scott Wood9a310d22008-01-15 17:54:43 -060017#include <linux/mtd/partitions.h>
18
Josh Wue79265b2013-08-05 19:14:38 +080019static bool node_has_compatible(struct device_node *pp)
20{
21 return of_get_property(pp, "compatible", NULL);
22}
23
Rafał Miłeckic0faf432018-03-14 13:10:43 +010024static int parse_fixed_partitions(struct mtd_info *master,
25 const struct mtd_partition **pparts,
26 struct mtd_part_parser_data *data)
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +040027{
Brian Norrisc3168d22015-12-04 15:25:13 -080028 struct mtd_partition *parts;
Michal Suchanek5cfdedb2015-08-18 15:34:09 +000029 struct device_node *mtd_node;
30 struct device_node *ofpart_node;
Scott Wood9a310d22008-01-15 17:54:43 -060031 const char *partname;
32 struct device_node *pp;
Michal Suchanek5cfdedb2015-08-18 15:34:09 +000033 int nr_parts, i, ret = 0;
34 bool dedicated = true;
Scott Wood9a310d22008-01-15 17:54:43 -060035
Dmitry Eremin-Solenikov628376f2011-05-30 01:05:33 +040036
Brian Norrise270bca2015-10-30 20:33:29 -070037 /* Pull of_node from the master device node */
38 mtd_node = mtd_get_of_node(master);
Michal Suchanek5cfdedb2015-08-18 15:34:09 +000039 if (!mtd_node)
Dmitry Eremin-Solenikov628376f2011-05-30 01:05:33 +040040 return 0;
41
Michal Suchanek5cfdedb2015-08-18 15:34:09 +000042 ofpart_node = of_get_child_by_name(mtd_node, "partitions");
43 if (!ofpart_node) {
Brian Norris8c62b4e2015-12-03 14:26:52 -080044 /*
45 * We might get here even when ofpart isn't used at all (e.g.,
46 * when using another parser), so don't be louder than
47 * KERN_DEBUG
48 */
Rob Herring1d706072017-07-18 16:43:17 -050049 pr_debug("%s: 'partitions' subnode not found on %pOF. Trying to parse direct subnodes as partitions.\n",
50 master->name, mtd_node);
Michal Suchanek5cfdedb2015-08-18 15:34:09 +000051 ofpart_node = mtd_node;
52 dedicated = false;
Brian Norrise488ca92015-12-03 14:47:32 -080053 } else if (!of_device_is_compatible(ofpart_node, "fixed-partitions")) {
54 /* The 'partitions' subnode might be used by another parser */
55 return 0;
Michal Suchanek5cfdedb2015-08-18 15:34:09 +000056 }
57
Scott Wood9a310d22008-01-15 17:54:43 -060058 /* First count the subnodes */
Scott Wood9a310d22008-01-15 17:54:43 -060059 nr_parts = 0;
Michal Suchanek5cfdedb2015-08-18 15:34:09 +000060 for_each_child_of_node(ofpart_node, pp) {
61 if (!dedicated && node_has_compatible(pp))
Josh Wue79265b2013-08-05 19:14:38 +080062 continue;
63
Scott Wood9a310d22008-01-15 17:54:43 -060064 nr_parts++;
Josh Wue79265b2013-08-05 19:14:38 +080065 }
Scott Wood9a310d22008-01-15 17:54:43 -060066
67 if (nr_parts == 0)
68 return 0;
69
Kees Cook6396bb22018-06-12 14:03:40 -070070 parts = kcalloc(nr_parts, sizeof(*parts), GFP_KERNEL);
Brian Norrisc3168d22015-12-04 15:25:13 -080071 if (!parts)
Scott Wood9a310d22008-01-15 17:54:43 -060072 return -ENOMEM;
73
Scott Wood9a310d22008-01-15 17:54:43 -060074 i = 0;
Michal Suchanek5cfdedb2015-08-18 15:34:09 +000075 for_each_child_of_node(ofpart_node, pp) {
Ian Munsie766f2712010-10-01 17:06:08 +100076 const __be32 *reg;
Scott Wood9a310d22008-01-15 17:54:43 -060077 int len;
Joe Schaack05ff8c22013-02-21 16:29:45 -060078 int a_cells, s_cells;
Scott Wood9a310d22008-01-15 17:54:43 -060079
Michal Suchanek5cfdedb2015-08-18 15:34:09 +000080 if (!dedicated && node_has_compatible(pp))
Josh Wue79265b2013-08-05 19:14:38 +080081 continue;
82
Benjamin Krillebd5a742009-08-25 15:52:41 +020083 reg = of_get_property(pp, "reg", &len);
84 if (!reg) {
Michal Suchanek5cfdedb2015-08-18 15:34:09 +000085 if (dedicated) {
Rob Herring1d706072017-07-18 16:43:17 -050086 pr_debug("%s: ofpart partition %pOF (%pOF) missing reg property.\n",
87 master->name, pp,
88 mtd_node);
Michal Suchanek5cfdedb2015-08-18 15:34:09 +000089 goto ofpart_fail;
90 } else {
91 nr_parts--;
92 continue;
93 }
Benjamin Krill4b08e142009-01-23 17:18:05 +010094 }
95
Joe Schaack05ff8c22013-02-21 16:29:45 -060096 a_cells = of_n_addr_cells(pp);
97 s_cells = of_n_size_cells(pp);
Michal Suchanek5cfdedb2015-08-18 15:34:09 +000098 if (len / 4 != a_cells + s_cells) {
Rob Herring1d706072017-07-18 16:43:17 -050099 pr_debug("%s: ofpart partition %pOF (%pOF) error parsing reg property.\n",
100 master->name, pp,
101 mtd_node);
Michal Suchanek5cfdedb2015-08-18 15:34:09 +0000102 goto ofpart_fail;
103 }
104
Brian Norrisc3168d22015-12-04 15:25:13 -0800105 parts[i].offset = of_read_number(reg, a_cells);
106 parts[i].size = of_read_number(reg + a_cells, s_cells);
Sascha Hauer42e94012017-02-09 11:50:24 +0100107 parts[i].of_node = pp;
Scott Wood9a310d22008-01-15 17:54:43 -0600108
109 partname = of_get_property(pp, "label", &len);
110 if (!partname)
111 partname = of_get_property(pp, "name", &len);
Brian Norrisc3168d22015-12-04 15:25:13 -0800112 parts[i].name = partname;
Scott Wood9a310d22008-01-15 17:54:43 -0600113
114 if (of_get_property(pp, "read-only", &len))
Brian Norrisc3168d22015-12-04 15:25:13 -0800115 parts[i].mask_flags |= MTD_WRITEABLE;
Josh Radelab0b00b2012-11-14 14:11:32 -0800116
117 if (of_get_property(pp, "lock", &len))
Brian Norrisc3168d22015-12-04 15:25:13 -0800118 parts[i].mask_flags |= MTD_POWERUP_LOCK;
Scott Wood9a310d22008-01-15 17:54:43 -0600119
120 i++;
121 }
122
Michal Suchanek5cfdedb2015-08-18 15:34:09 +0000123 if (!nr_parts)
124 goto ofpart_none;
Benjamin Krillebd5a742009-08-25 15:52:41 +0200125
Brian Norrisc3168d22015-12-04 15:25:13 -0800126 *pparts = parts;
Scott Wood9a310d22008-01-15 17:54:43 -0600127 return nr_parts;
Michal Suchanek5cfdedb2015-08-18 15:34:09 +0000128
129ofpart_fail:
Rob Herring1d706072017-07-18 16:43:17 -0500130 pr_err("%s: error parsing ofpart partition %pOF (%pOF)\n",
131 master->name, pp, mtd_node);
Michal Suchanek5cfdedb2015-08-18 15:34:09 +0000132 ret = -EINVAL;
133ofpart_none:
134 of_node_put(pp);
Brian Norrisc3168d22015-12-04 15:25:13 -0800135 kfree(parts);
Michal Suchanek5cfdedb2015-08-18 15:34:09 +0000136 return ret;
Scott Wood9a310d22008-01-15 17:54:43 -0600137}
Adrian Bunk950bcb22008-04-14 17:19:46 +0300138
Rafał Miłecki97b0c7c2018-03-14 13:10:44 +0100139static const struct of_device_id parse_ofpart_match_table[] = {
140 { .compatible = "fixed-partitions" },
141 {},
142};
143MODULE_DEVICE_TABLE(of, parse_ofpart_match_table);
144
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400145static struct mtd_part_parser ofpart_parser = {
Rafał Miłeckic0faf432018-03-14 13:10:43 +0100146 .parse_fn = parse_fixed_partitions,
147 .name = "fixed-partitions",
Rafał Miłecki97b0c7c2018-03-14 13:10:44 +0100148 .of_match_table = parse_ofpart_match_table,
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400149};
150
Dmitry Eremin-Solenikovfbcf62a2011-05-30 01:26:17 +0400151static int parse_ofoldpart_partitions(struct mtd_info *master,
Brian Norrisb9adf462015-12-04 15:25:14 -0800152 const struct mtd_partition **pparts,
Dmitry Eremin-Solenikovfbcf62a2011-05-30 01:26:17 +0400153 struct mtd_part_parser_data *data)
154{
Brian Norrisc3168d22015-12-04 15:25:13 -0800155 struct mtd_partition *parts;
Dmitry Eremin-Solenikovfbcf62a2011-05-30 01:26:17 +0400156 struct device_node *dp;
157 int i, plen, nr_parts;
158 const struct {
159 __be32 offset, len;
160 } *part;
161 const char *names;
162
Brian Norrise270bca2015-10-30 20:33:29 -0700163 /* Pull of_node from the master device node */
164 dp = mtd_get_of_node(master);
Dmitry Eremin-Solenikovfbcf62a2011-05-30 01:26:17 +0400165 if (!dp)
166 return 0;
167
168 part = of_get_property(dp, "partitions", &plen);
169 if (!part)
170 return 0; /* No partitions found */
171
Rob Herring1d706072017-07-18 16:43:17 -0500172 pr_warn("Device tree uses obsolete partition map binding: %pOF\n", dp);
Dmitry Eremin-Solenikovfbcf62a2011-05-30 01:26:17 +0400173
174 nr_parts = plen / sizeof(part[0]);
175
Kees Cook6396bb22018-06-12 14:03:40 -0700176 parts = kcalloc(nr_parts, sizeof(*parts), GFP_KERNEL);
Brian Norrisc3168d22015-12-04 15:25:13 -0800177 if (!parts)
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++) {
Brian Norrisc3168d22015-12-04 15:25:13 -0800183 parts[i].offset = be32_to_cpu(part->offset);
184 parts[i].size = be32_to_cpu(part->len) & ~1;
Dmitry Eremin-Solenikovfbcf62a2011-05-30 01:26:17 +0400185 /* bit 0 set signifies read only partition */
186 if (be32_to_cpu(part->len) & 1)
Brian Norrisc3168d22015-12-04 15:25:13 -0800187 parts[i].mask_flags = MTD_WRITEABLE;
Dmitry Eremin-Solenikovfbcf62a2011-05-30 01:26:17 +0400188
189 if (names && (plen > 0)) {
190 int len = strlen(names) + 1;
191
Brian Norrisc3168d22015-12-04 15:25:13 -0800192 parts[i].name = names;
Dmitry Eremin-Solenikovfbcf62a2011-05-30 01:26:17 +0400193 plen -= len;
194 names += len;
195 } else {
Brian Norrisc3168d22015-12-04 15:25:13 -0800196 parts[i].name = "unnamed";
Dmitry Eremin-Solenikovfbcf62a2011-05-30 01:26:17 +0400197 }
198
199 part++;
200 }
201
Brian Norrisc3168d22015-12-04 15:25:13 -0800202 *pparts = parts;
Dmitry Eremin-Solenikovfbcf62a2011-05-30 01:26:17 +0400203 return nr_parts;
204}
205
206static struct mtd_part_parser ofoldpart_parser = {
Dmitry Eremin-Solenikovfbcf62a2011-05-30 01:26:17 +0400207 .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 */
Rafał Miłeckic0faf432018-03-14 13:10:43 +0100235MODULE_ALIAS("fixed-partitions");
Dmitry Eremin-Solenikov9786f6e2011-06-27 16:34:46 +0400236MODULE_ALIAS("ofoldpart");