Merge git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs-unstable
[linux-2.6/mini2440.git] / drivers / mtd / ofpart.c
blob3e164f0c9295c7bc779b0b0c72c798f76176c09e
1 /*
2 * Flash partitions described by the OF (or flattened) device tree
4 * Copyright (C) 2006 MontaVista Software Inc.
5 * Author: Vitaly Wool <vwool@ru.mvista.com>
7 * Revised to handle newer style flash binding by:
8 * Copyright (C) 2007 David Gibson, IBM Corporation.
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.
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/of.h>
19 #include <linux/mtd/mtd.h>
20 #include <linux/mtd/partitions.h>
22 int __devinit of_mtd_parse_partitions(struct device *dev,
23 struct device_node *node,
24 struct mtd_partition **pparts)
26 const char *partname;
27 struct device_node *pp;
28 int nr_parts, i;
30 /* First count the subnodes */
31 pp = NULL;
32 nr_parts = 0;
33 while ((pp = of_get_next_child(node, pp)))
34 nr_parts++;
36 if (nr_parts == 0)
37 return 0;
39 *pparts = kzalloc(nr_parts * sizeof(**pparts), GFP_KERNEL);
40 if (!*pparts)
41 return -ENOMEM;
43 pp = NULL;
44 i = 0;
45 while ((pp = of_get_next_child(node, pp))) {
46 const u32 *reg;
47 int len;
49 /* check if this is a partition node */
50 partname = of_get_property(pp, "name", &len);
51 if (strcmp(partname, "partition") != 0) {
52 nr_parts--;
53 continue;
56 reg = of_get_property(pp, "reg", &len);
57 if (!reg || (len != 2 * sizeof(u32))) {
58 of_node_put(pp);
59 dev_err(dev, "Invalid 'reg' on %s\n", node->full_name);
60 kfree(*pparts);
61 *pparts = NULL;
62 return -EINVAL;
64 (*pparts)[i].offset = reg[0];
65 (*pparts)[i].size = reg[1];
67 partname = of_get_property(pp, "label", &len);
68 if (!partname)
69 partname = of_get_property(pp, "name", &len);
70 (*pparts)[i].name = (char *)partname;
72 if (of_get_property(pp, "read-only", &len))
73 (*pparts)[i].mask_flags = MTD_WRITEABLE;
75 i++;
78 return nr_parts;
80 EXPORT_SYMBOL(of_mtd_parse_partitions);
82 MODULE_LICENSE("GPL");