sunxi: add support for 4.1
[openwrt/staging/wigyori.git] / target / linux / sunxi / patches-4.1 / 112-mtd-add-dt-nand-partition-parser.patch
1 From 0460e9868fd82a3675db02f6ceb6edfd8501c194 Mon Sep 17 00:00:00 2001
2 From: Boris BREZILLON <boris.brezillon@free-electrons.com>
3 Date: Mon, 28 Jul 2014 14:31:42 +0200
4 Subject: [PATCH] mtd: nand: Add DT NAND partition parser
5
6 Add a of_nandpart_parse function to help parsing NAND partitions from DT.
7 This function should be called from NAND controller drivers just after the
8 nand_scan_tail in place of mtd_device_parse_register.
9 The caller can specify a parser function to retrieve HW specific
10 informations from the DT.
11
12 Signed-off-by: Boris BREZILLON <boris.brezillon@free-electrons.com>
13 Signed-off-by: Hans de Goede <hdegoede@redhat.com>
14 ---
15 drivers/mtd/nand/ofnandpart.c | 104 ++++++++++++++++++++++++++++++++++++++++++
16 include/linux/mtd/nand.h | 17 +++++++
17 2 files changed, 121 insertions(+)
18 create mode 100644 drivers/mtd/nand/ofnandpart.c
19
20 diff --git a/drivers/mtd/nand/ofnandpart.c b/drivers/mtd/nand/ofnandpart.c
21 new file mode 100644
22 index 0000000..293daee
23 --- /dev/null
24 +++ b/drivers/mtd/nand/ofnandpart.c
25 @@ -0,0 +1,104 @@
26 +/*
27 + * NAND Flash partitions described by the OF (or flattened) device tree
28 + *
29 + * Copyright © 2014 Boris BREZILLON <b.brezillon.dev@gmail.com>
30 + *
31 + * This program is free software; you can redistribute it and/or modify it
32 + * under the terms of the GNU General Public License as published by the
33 + * Free Software Foundation; either version 2 of the License, or (at your
34 + * option) any later version.
35 + */
36 +
37 +#include <linux/module.h>
38 +#include <linux/init.h>
39 +#include <linux/of.h>
40 +#include <linux/mtd/mtd.h>
41 +#include <linux/slab.h>
42 +#include <linux/mtd/nand.h>
43 +
44 +static inline bool node_has_compatible(struct device_node *pp)
45 +{
46 + return of_get_property(pp, "compatible", NULL);
47 +}
48 +
49 +int ofnandpart_parse(struct mtd_info *master,
50 + const struct ofnandpart_data *data)
51 +{
52 + struct device_node *node;
53 + const char *partname;
54 + struct device_node *pp;
55 + int i;
56 +
57 + if (!data)
58 + return 0;
59 +
60 + node = data->node;
61 + if (!node)
62 + return 0;
63 +
64 + i = 0;
65 + for_each_child_of_node(node, pp) {
66 + const __be32 *reg;
67 + int len;
68 + int a_cells, s_cells;
69 + uint64_t offset, size;
70 + uint32_t mask_flags = 0;
71 + struct nand_part *part;
72 +
73 + if (node_has_compatible(pp))
74 + continue;
75 +
76 + reg = of_get_property(pp, "reg", &len);
77 + if (!reg)
78 + continue;
79 +
80 + a_cells = of_n_addr_cells(pp);
81 + s_cells = of_n_size_cells(pp);
82 + offset = of_read_number(reg, a_cells);
83 + size = of_read_number(reg + a_cells, s_cells);
84 +
85 + partname = of_get_property(pp, "label", &len);
86 + if (!partname)
87 + partname = of_get_property(pp, "name", &len);
88 +
89 + if (of_get_property(pp, "read-only", &len))
90 + mask_flags |= MTD_WRITEABLE;
91 +
92 + if (of_get_property(pp, "lock", &len))
93 + mask_flags |= MTD_POWERUP_LOCK;
94 +
95 + if (data->parse)
96 + part = data->parse(data->priv, master, pp);
97 + else
98 + part = nandpart_alloc();
99 +
100 + if (IS_ERR(part))
101 + continue;
102 +
103 + part->offset = offset;
104 + part->master = master;
105 + part->mtd.name = partname;
106 + part->mtd.size = size;
107 + part->mtd.flags = mask_flags;
108 +
109 + if (nand_add_partition(master, part)) {
110 + if (part->release)
111 + part->release(part);
112 + continue;
113 + }
114 +
115 + i++;
116 + }
117 +
118 + if (!i) {
119 + of_node_put(pp);
120 + pr_err("No valid partition found on %s\n", node->full_name);
121 + }
122 +
123 + return i;
124 +}
125 +EXPORT_SYMBOL(ofnandpart_parse);
126 +
127 +MODULE_LICENSE("GPL");
128 +MODULE_DESCRIPTION("Parser for NAND flash partitioning information in device tree");
129 +MODULE_AUTHOR("Boris BREZILLON");
130 diff --git a/include/linux/mtd/nand.h b/include/linux/mtd/nand.h
131 index 510e09b..5616f51 100644
132 --- a/include/linux/mtd/nand.h
133 +++ b/include/linux/mtd/nand.h
134 @@ -1013,6 +1013,23 @@ static inline int jedec_feature(struct nand_chip *chip)
135 : 0;
136 }
137
138 +/**
139 + * struct ofnandpart_data - struct used to retrieve NAND partitions from a DT
140 + * node
141 + * @parse: driver specific parser function
142 + * @priv: driver private data
143 + * @node: OF node containing NAND partitions
144 + */
145 +struct ofnandpart_data {
146 + struct nand_part *(*parse)(void *priv, struct mtd_info *master,
147 + struct device_node *pp);
148 + void *priv;
149 + struct device_node *node;
150 +};
151 +
152 +int ofnandpart_parse(struct mtd_info *master,
153 + const struct ofnandpart_data *data);
154 +
155 /*
156 * struct nand_sdr_timings - SDR NAND chip timings
157 *