]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/mtd/ofpart.c
mtd: socrates_nand: use ofpart through generic parsing
[mirror_ubuntu-bionic-kernel.git] / drivers / mtd / ofpart.c
CommitLineData
9a310d21
SW
1/*
2 * Flash partitions described by the OF (or flattened) device tree
3 *
a1452a37 4 * Copyright © 2006 MontaVista Software Inc.
9a310d21
SW
5 * Author: Vitaly Wool <vwool@ru.mvista.com>
6 *
7 * Revised to handle newer style flash binding by:
a1452a37 8 * Copyright © 2007 David Gibson, IBM Corporation.
9a310d21
SW
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>
5a0e3ad6 20#include <linux/slab.h>
9a310d21
SW
21#include <linux/mtd/partitions.h>
22
d26c87d6
DES
23static int parse_ofpart_partitions(struct mtd_info *master,
24 struct mtd_partition **pparts,
25 struct mtd_part_parser_data *data)
26{
27 if (!data || !data->of_node)
28 return 0;
29
30 return of_mtd_parse_partitions(NULL, data->of_node, pparts);
31}
32
33int of_mtd_parse_partitions(struct device *dev,
9a310d21
SW
34 struct device_node *node,
35 struct mtd_partition **pparts)
36{
37 const char *partname;
38 struct device_node *pp;
39 int nr_parts, i;
40
41 /* First count the subnodes */
42 pp = NULL;
43 nr_parts = 0;
44 while ((pp = of_get_next_child(node, pp)))
45 nr_parts++;
46
47 if (nr_parts == 0)
48 return 0;
49
50 *pparts = kzalloc(nr_parts * sizeof(**pparts), GFP_KERNEL);
51 if (!*pparts)
52 return -ENOMEM;
53
54 pp = NULL;
55 i = 0;
56 while ((pp = of_get_next_child(node, pp))) {
766f271a 57 const __be32 *reg;
9a310d21
SW
58 int len;
59
ebd5a74d
BK
60 reg = of_get_property(pp, "reg", &len);
61 if (!reg) {
4b08e149
BK
62 nr_parts--;
63 continue;
64 }
65
766f271a
IM
66 (*pparts)[i].offset = be32_to_cpu(reg[0]);
67 (*pparts)[i].size = be32_to_cpu(reg[1]);
9a310d21
SW
68
69 partname = of_get_property(pp, "label", &len);
70 if (!partname)
71 partname = of_get_property(pp, "name", &len);
72 (*pparts)[i].name = (char *)partname;
73
74 if (of_get_property(pp, "read-only", &len))
75 (*pparts)[i].mask_flags = MTD_WRITEABLE;
76
77 i++;
78 }
79
ebd5a74d
BK
80 if (!i) {
81 of_node_put(pp);
d26c87d6 82 pr_err("No valid partition found on %s\n", node->full_name);
ebd5a74d
BK
83 kfree(*pparts);
84 *pparts = NULL;
85 return -EINVAL;
86 }
87
9a310d21
SW
88 return nr_parts;
89}
90EXPORT_SYMBOL(of_mtd_parse_partitions);
950bcb25 91
d26c87d6
DES
92static struct mtd_part_parser ofpart_parser = {
93 .owner = THIS_MODULE,
94 .parse_fn = parse_ofpart_partitions,
95 .name = "ofpart",
96};
97
98static int __init ofpart_parser_init(void)
99{
100 return register_mtd_parser(&ofpart_parser);
101}
102
103module_init(ofpart_parser_init);
104
950bcb25 105MODULE_LICENSE("GPL");