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