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 | { |
Dmitry Eremin-Solenikov | 628376f | 2011-05-30 01:05:33 +0400 | [diff] [blame] | 32 | struct device_node *node; |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 33 | const char *partname; |
| 34 | struct device_node *pp; |
| 35 | int nr_parts, i; |
| 36 | |
Dmitry Eremin-Solenikov | 628376f | 2011-05-30 01:05:33 +0400 | [diff] [blame] | 37 | |
| 38 | if (!data) |
| 39 | return 0; |
| 40 | |
| 41 | node = data->of_node; |
| 42 | if (!node) |
| 43 | return 0; |
| 44 | |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 45 | /* First count the subnodes */ |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 46 | nr_parts = 0; |
Wei Yongjun | 60ea89e | 2013-08-23 11:04:27 +0800 | [diff] [blame] | 47 | for_each_child_of_node(node, pp) { |
Josh Wu | e79265b | 2013-08-05 19:14:38 +0800 | [diff] [blame] | 48 | if (node_has_compatible(pp)) |
| 49 | continue; |
| 50 | |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 51 | nr_parts++; |
Josh Wu | e79265b | 2013-08-05 19:14:38 +0800 | [diff] [blame] | 52 | } |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 53 | |
| 54 | if (nr_parts == 0) |
| 55 | return 0; |
| 56 | |
| 57 | *pparts = kzalloc(nr_parts * sizeof(**pparts), GFP_KERNEL); |
| 58 | if (!*pparts) |
| 59 | return -ENOMEM; |
| 60 | |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 61 | i = 0; |
Wei Yongjun | 60ea89e | 2013-08-23 11:04:27 +0800 | [diff] [blame] | 62 | for_each_child_of_node(node, pp) { |
Ian Munsie | 766f271 | 2010-10-01 17:06:08 +1000 | [diff] [blame] | 63 | const __be32 *reg; |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 64 | int len; |
Joe Schaack | 05ff8c2 | 2013-02-21 16:29:45 -0600 | [diff] [blame] | 65 | int a_cells, s_cells; |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 66 | |
Josh Wu | e79265b | 2013-08-05 19:14:38 +0800 | [diff] [blame] | 67 | if (node_has_compatible(pp)) |
| 68 | continue; |
| 69 | |
Benjamin Krill | ebd5a74 | 2009-08-25 15:52:41 +0200 | [diff] [blame] | 70 | reg = of_get_property(pp, "reg", &len); |
| 71 | if (!reg) { |
Benjamin Krill | 4b08e14 | 2009-01-23 17:18:05 +0100 | [diff] [blame] | 72 | nr_parts--; |
| 73 | continue; |
| 74 | } |
| 75 | |
Joe Schaack | 05ff8c2 | 2013-02-21 16:29:45 -0600 | [diff] [blame] | 76 | a_cells = of_n_addr_cells(pp); |
| 77 | s_cells = of_n_size_cells(pp); |
| 78 | (*pparts)[i].offset = of_read_number(reg, a_cells); |
| 79 | (*pparts)[i].size = of_read_number(reg + a_cells, s_cells); |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 80 | |
| 81 | partname = of_get_property(pp, "label", &len); |
| 82 | if (!partname) |
| 83 | partname = of_get_property(pp, "name", &len); |
Geert Uytterhoeven | 26a6d24 | 2013-11-12 20:11:26 +0100 | [diff] [blame] | 84 | (*pparts)[i].name = partname; |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 85 | |
| 86 | if (of_get_property(pp, "read-only", &len)) |
Josh Radel | ab0b00b | 2012-11-14 14:11:32 -0800 | [diff] [blame] | 87 | (*pparts)[i].mask_flags |= MTD_WRITEABLE; |
| 88 | |
| 89 | if (of_get_property(pp, "lock", &len)) |
| 90 | (*pparts)[i].mask_flags |= MTD_POWERUP_LOCK; |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 91 | |
| 92 | i++; |
| 93 | } |
| 94 | |
Benjamin Krill | ebd5a74 | 2009-08-25 15:52:41 +0200 | [diff] [blame] | 95 | if (!i) { |
| 96 | of_node_put(pp); |
Dmitry Eremin-Solenikov | d26c87d | 2011-05-29 21:32:33 +0400 | [diff] [blame] | 97 | pr_err("No valid partition found on %s\n", node->full_name); |
Benjamin Krill | ebd5a74 | 2009-08-25 15:52:41 +0200 | [diff] [blame] | 98 | kfree(*pparts); |
| 99 | *pparts = NULL; |
| 100 | return -EINVAL; |
| 101 | } |
| 102 | |
Scott Wood | 9a310d2 | 2008-01-15 17:54:43 -0600 | [diff] [blame] | 103 | return nr_parts; |
| 104 | } |
Adrian Bunk | 950bcb2 | 2008-04-14 17:19:46 +0300 | [diff] [blame] | 105 | |
Dmitry Eremin-Solenikov | d26c87d | 2011-05-29 21:32:33 +0400 | [diff] [blame] | 106 | static struct mtd_part_parser ofpart_parser = { |
| 107 | .owner = THIS_MODULE, |
| 108 | .parse_fn = parse_ofpart_partitions, |
| 109 | .name = "ofpart", |
| 110 | }; |
| 111 | |
Dmitry Eremin-Solenikov | fbcf62a | 2011-05-30 01:26:17 +0400 | [diff] [blame] | 112 | static int parse_ofoldpart_partitions(struct mtd_info *master, |
| 113 | struct mtd_partition **pparts, |
| 114 | struct mtd_part_parser_data *data) |
| 115 | { |
| 116 | struct device_node *dp; |
| 117 | int i, plen, nr_parts; |
| 118 | const struct { |
| 119 | __be32 offset, len; |
| 120 | } *part; |
| 121 | const char *names; |
| 122 | |
| 123 | if (!data) |
| 124 | return 0; |
| 125 | |
| 126 | dp = data->of_node; |
| 127 | if (!dp) |
| 128 | return 0; |
| 129 | |
| 130 | part = of_get_property(dp, "partitions", &plen); |
| 131 | if (!part) |
| 132 | return 0; /* No partitions found */ |
| 133 | |
| 134 | pr_warning("Device tree uses obsolete partition map binding: %s\n", |
| 135 | dp->full_name); |
| 136 | |
| 137 | nr_parts = plen / sizeof(part[0]); |
| 138 | |
| 139 | *pparts = kzalloc(nr_parts * sizeof(*(*pparts)), GFP_KERNEL); |
Sachin Kamat | ecbcbc7 | 2012-09-25 15:27:13 +0530 | [diff] [blame] | 140 | if (!*pparts) |
Dmitry Eremin-Solenikov | fbcf62a | 2011-05-30 01:26:17 +0400 | [diff] [blame] | 141 | return -ENOMEM; |
| 142 | |
| 143 | names = of_get_property(dp, "partition-names", &plen); |
| 144 | |
| 145 | for (i = 0; i < nr_parts; i++) { |
| 146 | (*pparts)[i].offset = be32_to_cpu(part->offset); |
| 147 | (*pparts)[i].size = be32_to_cpu(part->len) & ~1; |
| 148 | /* bit 0 set signifies read only partition */ |
| 149 | if (be32_to_cpu(part->len) & 1) |
| 150 | (*pparts)[i].mask_flags = MTD_WRITEABLE; |
| 151 | |
| 152 | if (names && (plen > 0)) { |
| 153 | int len = strlen(names) + 1; |
| 154 | |
Geert Uytterhoeven | 26a6d24 | 2013-11-12 20:11:26 +0100 | [diff] [blame] | 155 | (*pparts)[i].name = names; |
Dmitry Eremin-Solenikov | fbcf62a | 2011-05-30 01:26:17 +0400 | [diff] [blame] | 156 | plen -= len; |
| 157 | names += len; |
| 158 | } else { |
| 159 | (*pparts)[i].name = "unnamed"; |
| 160 | } |
| 161 | |
| 162 | part++; |
| 163 | } |
| 164 | |
| 165 | return nr_parts; |
| 166 | } |
| 167 | |
| 168 | static struct mtd_part_parser ofoldpart_parser = { |
| 169 | .owner = THIS_MODULE, |
| 170 | .parse_fn = parse_ofoldpart_partitions, |
| 171 | .name = "ofoldpart", |
| 172 | }; |
| 173 | |
Dmitry Eremin-Solenikov | d26c87d | 2011-05-29 21:32:33 +0400 | [diff] [blame] | 174 | static int __init ofpart_parser_init(void) |
| 175 | { |
Axel Lin | 6e14a61 | 2013-12-01 19:01:06 +0800 | [diff] [blame] | 176 | register_mtd_parser(&ofpart_parser); |
| 177 | register_mtd_parser(&ofoldpart_parser); |
| 178 | return 0; |
Dmitry Eremin-Solenikov | d26c87d | 2011-05-29 21:32:33 +0400 | [diff] [blame] | 179 | } |
| 180 | |
Lubomir Rintel | 422f389 | 2013-01-16 02:12:50 +0100 | [diff] [blame] | 181 | static void __exit ofpart_parser_exit(void) |
| 182 | { |
| 183 | deregister_mtd_parser(&ofpart_parser); |
| 184 | deregister_mtd_parser(&ofoldpart_parser); |
| 185 | } |
| 186 | |
Dmitry Eremin-Solenikov | d26c87d | 2011-05-29 21:32:33 +0400 | [diff] [blame] | 187 | module_init(ofpart_parser_init); |
Lubomir Rintel | 422f389 | 2013-01-16 02:12:50 +0100 | [diff] [blame] | 188 | module_exit(ofpart_parser_exit); |
Dmitry Eremin-Solenikov | d26c87d | 2011-05-29 21:32:33 +0400 | [diff] [blame] | 189 | |
Adrian Bunk | 950bcb2 | 2008-04-14 17:19:46 +0300 | [diff] [blame] | 190 | MODULE_LICENSE("GPL"); |
Dmitry Eremin-Solenikov | d6137ba | 2011-06-27 01:02:59 +0400 | [diff] [blame] | 191 | MODULE_DESCRIPTION("Parser for MTD partitioning information in device tree"); |
| 192 | MODULE_AUTHOR("Vitaly Wool, David Gibson"); |
Dmitry Eremin-Solenikov | 9786f6e | 2011-06-27 16:34:46 +0400 | [diff] [blame] | 193 | /* |
| 194 | * When MTD core cannot find the requested parser, it tries to load the module |
| 195 | * with the same name. Since we provide the ofoldpart parser, we should have |
| 196 | * the corresponding alias. |
| 197 | */ |
| 198 | MODULE_ALIAS("ofoldpart"); |