Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Flash partitions described by the OF (or flattened) device tree |
| 4 | * |
David Woodhouse | a1452a3 | 2010-08-08 20:58:20 +0100 | [diff] [blame] | 5 | * Copyright © 2006 MontaVista Software Inc. |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 6 | * Author: Vitaly Wool <vwool@ru.mvista.com> |
| 7 | * |
| 8 | * Revised to handle newer style flash binding by: |
David Woodhouse | a1452a3 | 2010-08-08 20:58:20 +0100 | [diff] [blame] | 9 | * Copyright © 2007 David Gibson, IBM Corporation. |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/of.h> |
| 15 | #include <linux/mtd/mtd.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 16 | #include <linux/slab.h> |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 17 | #include <linux/mtd/partitions.h> |
| 18 | |
Josh Wu | e79265b | 2013-08-05 19:14:38 +0800 | [diff] [blame] | 19 | static bool node_has_compatible(struct device_node *pp) |
| 20 | { |
| 21 | return of_get_property(pp, "compatible", NULL); |
| 22 | } |
| 23 | |
Rafał Miłecki | c0faf43 | 2018-03-14 13:10:43 +0100 | [diff] [blame] | 24 | static int parse_fixed_partitions(struct mtd_info *master, |
| 25 | const struct mtd_partition **pparts, |
| 26 | struct mtd_part_parser_data *data) |
Dmitry Eremin-Solenikov | d26c87d | 2011-05-29 21:32:33 +0400 | [diff] [blame] | 27 | { |
Brian Norris | c3168d2 | 2015-12-04 15:25:13 -0800 | [diff] [blame] | 28 | struct mtd_partition *parts; |
Michal Suchanek | 5cfdedb | 2015-08-18 15:34:09 +0000 | [diff] [blame] | 29 | struct device_node *mtd_node; |
| 30 | struct device_node *ofpart_node; |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 31 | const char *partname; |
| 32 | struct device_node *pp; |
Michal Suchanek | 5cfdedb | 2015-08-18 15:34:09 +0000 | [diff] [blame] | 33 | int nr_parts, i, ret = 0; |
| 34 | bool dedicated = true; |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 35 | |
Dmitry Eremin-Solenikov | 628376f | 2011-05-30 01:05:33 +0400 | [diff] [blame] | 36 | |
Brian Norris | e270bca | 2015-10-30 20:33:29 -0700 | [diff] [blame] | 37 | /* Pull of_node from the master device node */ |
| 38 | mtd_node = mtd_get_of_node(master); |
Michal Suchanek | 5cfdedb | 2015-08-18 15:34:09 +0000 | [diff] [blame] | 39 | if (!mtd_node) |
Dmitry Eremin-Solenikov | 628376f | 2011-05-30 01:05:33 +0400 | [diff] [blame] | 40 | return 0; |
| 41 | |
Michal Suchanek | 5cfdedb | 2015-08-18 15:34:09 +0000 | [diff] [blame] | 42 | ofpart_node = of_get_child_by_name(mtd_node, "partitions"); |
| 43 | if (!ofpart_node) { |
Brian Norris | 8c62b4e | 2015-12-03 14:26:52 -0800 | [diff] [blame] | 44 | /* |
| 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 Herring | 1d70607 | 2017-07-18 16:43:17 -0500 | [diff] [blame] | 49 | pr_debug("%s: 'partitions' subnode not found on %pOF. Trying to parse direct subnodes as partitions.\n", |
| 50 | master->name, mtd_node); |
Michal Suchanek | 5cfdedb | 2015-08-18 15:34:09 +0000 | [diff] [blame] | 51 | ofpart_node = mtd_node; |
| 52 | dedicated = false; |
Brian Norris | e488ca9 | 2015-12-03 14:47:32 -0800 | [diff] [blame] | 53 | } else if (!of_device_is_compatible(ofpart_node, "fixed-partitions")) { |
| 54 | /* The 'partitions' subnode might be used by another parser */ |
| 55 | return 0; |
Michal Suchanek | 5cfdedb | 2015-08-18 15:34:09 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 58 | /* First count the subnodes */ |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 59 | nr_parts = 0; |
Michal Suchanek | 5cfdedb | 2015-08-18 15:34:09 +0000 | [diff] [blame] | 60 | for_each_child_of_node(ofpart_node, pp) { |
| 61 | if (!dedicated && node_has_compatible(pp)) |
Josh Wu | e79265b | 2013-08-05 19:14:38 +0800 | [diff] [blame] | 62 | continue; |
| 63 | |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 64 | nr_parts++; |
Josh Wu | e79265b | 2013-08-05 19:14:38 +0800 | [diff] [blame] | 65 | } |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 66 | |
| 67 | if (nr_parts == 0) |
| 68 | return 0; |
| 69 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 70 | parts = kcalloc(nr_parts, sizeof(*parts), GFP_KERNEL); |
Brian Norris | c3168d2 | 2015-12-04 15:25:13 -0800 | [diff] [blame] | 71 | if (!parts) |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 72 | return -ENOMEM; |
| 73 | |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 74 | i = 0; |
Michal Suchanek | 5cfdedb | 2015-08-18 15:34:09 +0000 | [diff] [blame] | 75 | for_each_child_of_node(ofpart_node, pp) { |
Ian Munsie | 766f271 | 2010-10-01 17:06:08 +1000 | [diff] [blame] | 76 | const __be32 *reg; |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 77 | int len; |
Joe Schaack | 05ff8c2 | 2013-02-21 16:29:45 -0600 | [diff] [blame] | 78 | int a_cells, s_cells; |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 79 | |
Michal Suchanek | 5cfdedb | 2015-08-18 15:34:09 +0000 | [diff] [blame] | 80 | if (!dedicated && node_has_compatible(pp)) |
Josh Wu | e79265b | 2013-08-05 19:14:38 +0800 | [diff] [blame] | 81 | continue; |
| 82 | |
Benjamin Krill | ebd5a74 | 2009-08-25 15:52:41 +0200 | [diff] [blame] | 83 | reg = of_get_property(pp, "reg", &len); |
| 84 | if (!reg) { |
Michal Suchanek | 5cfdedb | 2015-08-18 15:34:09 +0000 | [diff] [blame] | 85 | if (dedicated) { |
Rob Herring | 1d70607 | 2017-07-18 16:43:17 -0500 | [diff] [blame] | 86 | pr_debug("%s: ofpart partition %pOF (%pOF) missing reg property.\n", |
| 87 | master->name, pp, |
| 88 | mtd_node); |
Michal Suchanek | 5cfdedb | 2015-08-18 15:34:09 +0000 | [diff] [blame] | 89 | goto ofpart_fail; |
| 90 | } else { |
| 91 | nr_parts--; |
| 92 | continue; |
| 93 | } |
Benjamin Krill | 4b08e14 | 2009-01-23 17:18:05 +0100 | [diff] [blame] | 94 | } |
| 95 | |
Joe Schaack | 05ff8c2 | 2013-02-21 16:29:45 -0600 | [diff] [blame] | 96 | a_cells = of_n_addr_cells(pp); |
| 97 | s_cells = of_n_size_cells(pp); |
Michal Suchanek | 5cfdedb | 2015-08-18 15:34:09 +0000 | [diff] [blame] | 98 | if (len / 4 != a_cells + s_cells) { |
Rob Herring | 1d70607 | 2017-07-18 16:43:17 -0500 | [diff] [blame] | 99 | pr_debug("%s: ofpart partition %pOF (%pOF) error parsing reg property.\n", |
| 100 | master->name, pp, |
| 101 | mtd_node); |
Michal Suchanek | 5cfdedb | 2015-08-18 15:34:09 +0000 | [diff] [blame] | 102 | goto ofpart_fail; |
| 103 | } |
| 104 | |
Brian Norris | c3168d2 | 2015-12-04 15:25:13 -0800 | [diff] [blame] | 105 | parts[i].offset = of_read_number(reg, a_cells); |
| 106 | parts[i].size = of_read_number(reg + a_cells, s_cells); |
Sascha Hauer | 42e9401 | 2017-02-09 11:50:24 +0100 | [diff] [blame] | 107 | parts[i].of_node = pp; |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 108 | |
| 109 | partname = of_get_property(pp, "label", &len); |
| 110 | if (!partname) |
| 111 | partname = of_get_property(pp, "name", &len); |
Brian Norris | c3168d2 | 2015-12-04 15:25:13 -0800 | [diff] [blame] | 112 | parts[i].name = partname; |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 113 | |
| 114 | if (of_get_property(pp, "read-only", &len)) |
Brian Norris | c3168d2 | 2015-12-04 15:25:13 -0800 | [diff] [blame] | 115 | parts[i].mask_flags |= MTD_WRITEABLE; |
Josh Radel | ab0b00b | 2012-11-14 14:11:32 -0800 | [diff] [blame] | 116 | |
| 117 | if (of_get_property(pp, "lock", &len)) |
Brian Norris | c3168d2 | 2015-12-04 15:25:13 -0800 | [diff] [blame] | 118 | parts[i].mask_flags |= MTD_POWERUP_LOCK; |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 119 | |
Boris Brezillon | 1998053 | 2020-05-03 17:53:39 +0200 | [diff] [blame] | 120 | if (of_property_read_bool(pp, "slc-mode")) |
| 121 | parts[i].add_flags |= MTD_SLC_ON_MLC_EMULATION; |
| 122 | |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 123 | i++; |
| 124 | } |
| 125 | |
Michal Suchanek | 5cfdedb | 2015-08-18 15:34:09 +0000 | [diff] [blame] | 126 | if (!nr_parts) |
| 127 | goto ofpart_none; |
Benjamin Krill | ebd5a74 | 2009-08-25 15:52:41 +0200 | [diff] [blame] | 128 | |
Brian Norris | c3168d2 | 2015-12-04 15:25:13 -0800 | [diff] [blame] | 129 | *pparts = parts; |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 130 | return nr_parts; |
Michal Suchanek | 5cfdedb | 2015-08-18 15:34:09 +0000 | [diff] [blame] | 131 | |
| 132 | ofpart_fail: |
Rob Herring | 1d70607 | 2017-07-18 16:43:17 -0500 | [diff] [blame] | 133 | pr_err("%s: error parsing ofpart partition %pOF (%pOF)\n", |
| 134 | master->name, pp, mtd_node); |
Michal Suchanek | 5cfdedb | 2015-08-18 15:34:09 +0000 | [diff] [blame] | 135 | ret = -EINVAL; |
| 136 | ofpart_none: |
| 137 | of_node_put(pp); |
Brian Norris | c3168d2 | 2015-12-04 15:25:13 -0800 | [diff] [blame] | 138 | kfree(parts); |
Michal Suchanek | 5cfdedb | 2015-08-18 15:34:09 +0000 | [diff] [blame] | 139 | return ret; |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 140 | } |
Adrian Bunk | 950bcb2 | 2008-04-14 17:19:46 +0300 | [diff] [blame] | 141 | |
Rafał Miłecki | 97b0c7c | 2018-03-14 13:10:44 +0100 | [diff] [blame] | 142 | static const struct of_device_id parse_ofpart_match_table[] = { |
| 143 | { .compatible = "fixed-partitions" }, |
| 144 | {}, |
| 145 | }; |
| 146 | MODULE_DEVICE_TABLE(of, parse_ofpart_match_table); |
| 147 | |
Dmitry Eremin-Solenikov | d26c87d | 2011-05-29 21:32:33 +0400 | [diff] [blame] | 148 | static struct mtd_part_parser ofpart_parser = { |
Rafał Miłecki | c0faf43 | 2018-03-14 13:10:43 +0100 | [diff] [blame] | 149 | .parse_fn = parse_fixed_partitions, |
| 150 | .name = "fixed-partitions", |
Rafał Miłecki | 97b0c7c | 2018-03-14 13:10:44 +0100 | [diff] [blame] | 151 | .of_match_table = parse_ofpart_match_table, |
Dmitry Eremin-Solenikov | d26c87d | 2011-05-29 21:32:33 +0400 | [diff] [blame] | 152 | }; |
| 153 | |
Dmitry Eremin-Solenikov | fbcf62a | 2011-05-30 01:26:17 +0400 | [diff] [blame] | 154 | static int parse_ofoldpart_partitions(struct mtd_info *master, |
Brian Norris | b9adf46 | 2015-12-04 15:25:14 -0800 | [diff] [blame] | 155 | const struct mtd_partition **pparts, |
Dmitry Eremin-Solenikov | fbcf62a | 2011-05-30 01:26:17 +0400 | [diff] [blame] | 156 | struct mtd_part_parser_data *data) |
| 157 | { |
Brian Norris | c3168d2 | 2015-12-04 15:25:13 -0800 | [diff] [blame] | 158 | struct mtd_partition *parts; |
Dmitry Eremin-Solenikov | fbcf62a | 2011-05-30 01:26:17 +0400 | [diff] [blame] | 159 | struct device_node *dp; |
| 160 | int i, plen, nr_parts; |
| 161 | const struct { |
| 162 | __be32 offset, len; |
| 163 | } *part; |
| 164 | const char *names; |
| 165 | |
Brian Norris | e270bca | 2015-10-30 20:33:29 -0700 | [diff] [blame] | 166 | /* Pull of_node from the master device node */ |
| 167 | dp = mtd_get_of_node(master); |
Dmitry Eremin-Solenikov | fbcf62a | 2011-05-30 01:26:17 +0400 | [diff] [blame] | 168 | if (!dp) |
| 169 | return 0; |
| 170 | |
| 171 | part = of_get_property(dp, "partitions", &plen); |
| 172 | if (!part) |
| 173 | return 0; /* No partitions found */ |
| 174 | |
Rob Herring | 1d70607 | 2017-07-18 16:43:17 -0500 | [diff] [blame] | 175 | pr_warn("Device tree uses obsolete partition map binding: %pOF\n", dp); |
Dmitry Eremin-Solenikov | fbcf62a | 2011-05-30 01:26:17 +0400 | [diff] [blame] | 176 | |
| 177 | nr_parts = plen / sizeof(part[0]); |
| 178 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 179 | parts = kcalloc(nr_parts, sizeof(*parts), GFP_KERNEL); |
Brian Norris | c3168d2 | 2015-12-04 15:25:13 -0800 | [diff] [blame] | 180 | if (!parts) |
Dmitry Eremin-Solenikov | fbcf62a | 2011-05-30 01:26:17 +0400 | [diff] [blame] | 181 | return -ENOMEM; |
| 182 | |
| 183 | names = of_get_property(dp, "partition-names", &plen); |
| 184 | |
| 185 | for (i = 0; i < nr_parts; i++) { |
Brian Norris | c3168d2 | 2015-12-04 15:25:13 -0800 | [diff] [blame] | 186 | parts[i].offset = be32_to_cpu(part->offset); |
| 187 | parts[i].size = be32_to_cpu(part->len) & ~1; |
Dmitry Eremin-Solenikov | fbcf62a | 2011-05-30 01:26:17 +0400 | [diff] [blame] | 188 | /* bit 0 set signifies read only partition */ |
| 189 | if (be32_to_cpu(part->len) & 1) |
Brian Norris | c3168d2 | 2015-12-04 15:25:13 -0800 | [diff] [blame] | 190 | parts[i].mask_flags = MTD_WRITEABLE; |
Dmitry Eremin-Solenikov | fbcf62a | 2011-05-30 01:26:17 +0400 | [diff] [blame] | 191 | |
| 192 | if (names && (plen > 0)) { |
| 193 | int len = strlen(names) + 1; |
| 194 | |
Brian Norris | c3168d2 | 2015-12-04 15:25:13 -0800 | [diff] [blame] | 195 | parts[i].name = names; |
Dmitry Eremin-Solenikov | fbcf62a | 2011-05-30 01:26:17 +0400 | [diff] [blame] | 196 | plen -= len; |
| 197 | names += len; |
| 198 | } else { |
Brian Norris | c3168d2 | 2015-12-04 15:25:13 -0800 | [diff] [blame] | 199 | parts[i].name = "unnamed"; |
Dmitry Eremin-Solenikov | fbcf62a | 2011-05-30 01:26:17 +0400 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | part++; |
| 203 | } |
| 204 | |
Brian Norris | c3168d2 | 2015-12-04 15:25:13 -0800 | [diff] [blame] | 205 | *pparts = parts; |
Dmitry Eremin-Solenikov | fbcf62a | 2011-05-30 01:26:17 +0400 | [diff] [blame] | 206 | return nr_parts; |
| 207 | } |
| 208 | |
| 209 | static struct mtd_part_parser ofoldpart_parser = { |
Dmitry Eremin-Solenikov | fbcf62a | 2011-05-30 01:26:17 +0400 | [diff] [blame] | 210 | .parse_fn = parse_ofoldpart_partitions, |
| 211 | .name = "ofoldpart", |
| 212 | }; |
| 213 | |
Dmitry Eremin-Solenikov | d26c87d | 2011-05-29 21:32:33 +0400 | [diff] [blame] | 214 | static int __init ofpart_parser_init(void) |
| 215 | { |
Axel Lin | 6e14a61 | 2013-12-01 19:01:06 +0800 | [diff] [blame] | 216 | register_mtd_parser(&ofpart_parser); |
| 217 | register_mtd_parser(&ofoldpart_parser); |
| 218 | return 0; |
Dmitry Eremin-Solenikov | d26c87d | 2011-05-29 21:32:33 +0400 | [diff] [blame] | 219 | } |
| 220 | |
Lubomir Rintel | 422f389 | 2013-01-16 02:12:50 +0100 | [diff] [blame] | 221 | static void __exit ofpart_parser_exit(void) |
| 222 | { |
| 223 | deregister_mtd_parser(&ofpart_parser); |
| 224 | deregister_mtd_parser(&ofoldpart_parser); |
| 225 | } |
| 226 | |
Dmitry Eremin-Solenikov | d26c87d | 2011-05-29 21:32:33 +0400 | [diff] [blame] | 227 | module_init(ofpart_parser_init); |
Lubomir Rintel | 422f389 | 2013-01-16 02:12:50 +0100 | [diff] [blame] | 228 | module_exit(ofpart_parser_exit); |
Dmitry Eremin-Solenikov | d26c87d | 2011-05-29 21:32:33 +0400 | [diff] [blame] | 229 | |
Adrian Bunk | 950bcb2 | 2008-04-14 17:19:46 +0300 | [diff] [blame] | 230 | MODULE_LICENSE("GPL"); |
Dmitry Eremin-Solenikov | d6137ba | 2011-06-27 01:02:59 +0400 | [diff] [blame] | 231 | MODULE_DESCRIPTION("Parser for MTD partitioning information in device tree"); |
| 232 | MODULE_AUTHOR("Vitaly Wool, David Gibson"); |
Dmitry Eremin-Solenikov | 9786f6e | 2011-06-27 16:34:46 +0400 | [diff] [blame] | 233 | /* |
| 234 | * When MTD core cannot find the requested parser, it tries to load the module |
| 235 | * with the same name. Since we provide the ofoldpart parser, we should have |
| 236 | * the corresponding alias. |
| 237 | */ |
Rafał Miłecki | c0faf43 | 2018-03-14 13:10:43 +0100 | [diff] [blame] | 238 | MODULE_ALIAS("fixed-partitions"); |
Dmitry Eremin-Solenikov | 9786f6e | 2011-06-27 16:34:46 +0400 | [diff] [blame] | 239 | MODULE_ALIAS("ofoldpart"); |