ofpart.c 5.8 KB
Newer Older
1 2 3
/*
 * Flash partitions described by the OF (or flattened) device tree
 *
4
 * Copyright © 2006 MontaVista Software Inc.
5 6 7
 * Author: Vitaly Wool <vwool@ru.mvista.com>
 *
 * Revised to handle newer style flash binding by:
8
 *   Copyright © 2007 David Gibson, IBM Corporation.
9 10 11 12 13 14 15 16 17 18 19
 *
 * This program is free software; you can redistribute  it and/or modify it
 * under  the terms of  the GNU General  Public License as published by the
 * Free Software Foundation;  either version 2 of the  License, or (at your
 * option) any later version.
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/of.h>
#include <linux/mtd/mtd.h>
20
#include <linux/slab.h>
21 22
#include <linux/mtd/partitions.h>

23 24 25 26 27
static bool node_has_compatible(struct device_node *pp)
{
	return of_get_property(pp, "compatible", NULL);
}

28 29 30
static int parse_fixed_partitions(struct mtd_info *master,
				  const struct mtd_partition **pparts,
				  struct mtd_part_parser_data *data)
31
{
32
	struct mtd_partition *parts;
33 34
	struct device_node *mtd_node;
	struct device_node *ofpart_node;
35 36
	const char *partname;
	struct device_node *pp;
37 38
	int nr_parts, i, ret = 0;
	bool dedicated = true;
39

40

41 42
	/* Pull of_node from the master device node */
	mtd_node = mtd_get_of_node(master);
43
	if (!mtd_node)
44 45
		return 0;

46 47
	ofpart_node = of_get_child_by_name(mtd_node, "partitions");
	if (!ofpart_node) {
48 49 50 51 52
		/*
		 * We might get here even when ofpart isn't used at all (e.g.,
		 * when using another parser), so don't be louder than
		 * KERN_DEBUG
		 */
53 54
		pr_debug("%s: 'partitions' subnode not found on %pOF. Trying to parse direct subnodes as partitions.\n",
			 master->name, mtd_node);
55 56
		ofpart_node = mtd_node;
		dedicated = false;
57 58 59
	} else if (!of_device_is_compatible(ofpart_node, "fixed-partitions")) {
		/* The 'partitions' subnode might be used by another parser */
		return 0;
60 61
	}

62 63
	/* First count the subnodes */
	nr_parts = 0;
64 65
	for_each_child_of_node(ofpart_node,  pp) {
		if (!dedicated && node_has_compatible(pp))
66 67
			continue;

68
		nr_parts++;
69
	}
70 71 72 73

	if (nr_parts == 0)
		return 0;

74
	parts = kcalloc(nr_parts, sizeof(*parts), GFP_KERNEL);
75
	if (!parts)
76 77 78
		return -ENOMEM;

	i = 0;
79
	for_each_child_of_node(ofpart_node,  pp) {
80
		const __be32 *reg;
81
		int len;
82
		int a_cells, s_cells;
83

84
		if (!dedicated && node_has_compatible(pp))
85 86
			continue;

87 88
		reg = of_get_property(pp, "reg", &len);
		if (!reg) {
89
			if (dedicated) {
90 91 92
				pr_debug("%s: ofpart partition %pOF (%pOF) missing reg property.\n",
					 master->name, pp,
					 mtd_node);
93 94 95 96 97
				goto ofpart_fail;
			} else {
				nr_parts--;
				continue;
			}
98 99
		}

100 101
		a_cells = of_n_addr_cells(pp);
		s_cells = of_n_size_cells(pp);
102
		if (len / 4 != a_cells + s_cells) {
103 104 105
			pr_debug("%s: ofpart partition %pOF (%pOF) error parsing reg property.\n",
				 master->name, pp,
				 mtd_node);
106 107 108
			goto ofpart_fail;
		}

109 110
		parts[i].offset = of_read_number(reg, a_cells);
		parts[i].size = of_read_number(reg + a_cells, s_cells);
111
		parts[i].of_node = pp;
112 113 114 115

		partname = of_get_property(pp, "label", &len);
		if (!partname)
			partname = of_get_property(pp, "name", &len);
116
		parts[i].name = partname;
117 118

		if (of_get_property(pp, "read-only", &len))
119
			parts[i].mask_flags |= MTD_WRITEABLE;
120 121

		if (of_get_property(pp, "lock", &len))
122
			parts[i].mask_flags |= MTD_POWERUP_LOCK;
123 124 125 126

		i++;
	}

127 128
	if (!nr_parts)
		goto ofpart_none;
129

130
	*pparts = parts;
131
	return nr_parts;
132 133

ofpart_fail:
134 135
	pr_err("%s: error parsing ofpart partition %pOF (%pOF)\n",
	       master->name, pp, mtd_node);
136 137 138
	ret = -EINVAL;
ofpart_none:
	of_node_put(pp);
139
	kfree(parts);
140
	return ret;
141
}
142

143 144 145 146 147 148
static const struct of_device_id parse_ofpart_match_table[] = {
	{ .compatible = "fixed-partitions" },
	{},
};
MODULE_DEVICE_TABLE(of, parse_ofpart_match_table);

149
static struct mtd_part_parser ofpart_parser = {
150 151
	.parse_fn = parse_fixed_partitions,
	.name = "fixed-partitions",
152
	.of_match_table = parse_ofpart_match_table,
153 154
};

155
static int parse_ofoldpart_partitions(struct mtd_info *master,
156
				      const struct mtd_partition **pparts,
157 158
				      struct mtd_part_parser_data *data)
{
159
	struct mtd_partition *parts;
160 161 162 163 164 165 166
	struct device_node *dp;
	int i, plen, nr_parts;
	const struct {
		__be32 offset, len;
	} *part;
	const char *names;

167 168
	/* Pull of_node from the master device node */
	dp = mtd_get_of_node(master);
169 170 171 172 173 174 175
	if (!dp)
		return 0;

	part = of_get_property(dp, "partitions", &plen);
	if (!part)
		return 0; /* No partitions found */

176
	pr_warn("Device tree uses obsolete partition map binding: %pOF\n", dp);
177 178 179

	nr_parts = plen / sizeof(part[0]);

180
	parts = kcalloc(nr_parts, sizeof(*parts), GFP_KERNEL);
181
	if (!parts)
182 183 184 185 186
		return -ENOMEM;

	names = of_get_property(dp, "partition-names", &plen);

	for (i = 0; i < nr_parts; i++) {
187 188
		parts[i].offset = be32_to_cpu(part->offset);
		parts[i].size   = be32_to_cpu(part->len) & ~1;
189 190
		/* bit 0 set signifies read only partition */
		if (be32_to_cpu(part->len) & 1)
191
			parts[i].mask_flags = MTD_WRITEABLE;
192 193 194 195

		if (names && (plen > 0)) {
			int len = strlen(names) + 1;

196
			parts[i].name = names;
197 198 199
			plen -= len;
			names += len;
		} else {
200
			parts[i].name = "unnamed";
201 202 203 204 205
		}

		part++;
	}

206
	*pparts = parts;
207 208 209 210 211 212 213 214
	return nr_parts;
}

static struct mtd_part_parser ofoldpart_parser = {
	.parse_fn = parse_ofoldpart_partitions,
	.name = "ofoldpart",
};

215 216
static int __init ofpart_parser_init(void)
{
217 218 219
	register_mtd_parser(&ofpart_parser);
	register_mtd_parser(&ofoldpart_parser);
	return 0;
220 221
}

222 223 224 225 226 227
static void __exit ofpart_parser_exit(void)
{
	deregister_mtd_parser(&ofpart_parser);
	deregister_mtd_parser(&ofoldpart_parser);
}

228
module_init(ofpart_parser_init);
229
module_exit(ofpart_parser_exit);
230

231
MODULE_LICENSE("GPL");
232 233
MODULE_DESCRIPTION("Parser for MTD partitioning information in device tree");
MODULE_AUTHOR("Vitaly Wool, David Gibson");
234 235 236 237 238
/*
 * When MTD core cannot find the requested parser, it tries to load the module
 * with the same name. Since we provide the ofoldpart parser, we should have
 * the corresponding alias.
 */
239
MODULE_ALIAS("fixed-partitions");
240
MODULE_ALIAS("ofoldpart");