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