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
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.
12 Signed-off-by: Boris BREZILLON <boris.brezillon@free-electrons.com>
13 Signed-off-by: Hans de Goede <hdegoede@redhat.com>
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
20 diff --git a/drivers/mtd/nand/ofnandpart.c b/drivers/mtd/nand/ofnandpart.c
22 index 0000000..293daee
24 +++ b/drivers/mtd/nand/ofnandpart.c
27 + * NAND Flash partitions described by the OF (or flattened) device tree
29 + * Copyright © 2014 Boris BREZILLON <b.brezillon.dev@gmail.com>
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.
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>
44 +static inline bool node_has_compatible(struct device_node *pp)
46 + return of_get_property(pp, "compatible", NULL);
49 +int ofnandpart_parse(struct mtd_info *master,
50 + const struct ofnandpart_data *data)
52 + struct device_node *node;
53 + const char *partname;
54 + struct device_node *pp;
65 + for_each_child_of_node(node, pp) {
68 + int a_cells, s_cells;
69 + uint64_t offset, size;
70 + uint32_t mask_flags = 0;
71 + struct nand_part *part;
73 + if (node_has_compatible(pp))
76 + reg = of_get_property(pp, "reg", &len);
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);
85 + partname = of_get_property(pp, "label", &len);
87 + partname = of_get_property(pp, "name", &len);
89 + if (of_get_property(pp, "read-only", &len))
90 + mask_flags |= MTD_WRITEABLE;
92 + if (of_get_property(pp, "lock", &len))
93 + mask_flags |= MTD_POWERUP_LOCK;
96 + part = data->parse(data->priv, master, pp);
98 + part = nandpart_alloc();
103 + part->offset = offset;
104 + part->master = master;
105 + part->mtd.name = partname;
106 + part->mtd.size = size;
107 + part->mtd.flags = mask_flags;
109 + if (nand_add_partition(master, part)) {
111 + part->release(part);
120 + pr_err("No valid partition found on %s\n", node->full_name);
125 +EXPORT_SYMBOL(ofnandpart_parse);
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)
139 + * struct ofnandpart_data - struct used to retrieve NAND partitions from a DT
141 + * @parse: driver specific parser function
142 + * @priv: driver private data
143 + * @node: OF node containing NAND partitions
145 +struct ofnandpart_data {
146 + struct nand_part *(*parse)(void *priv, struct mtd_info *master,
147 + struct device_node *pp);
149 + struct device_node *node;
152 +int ofnandpart_parse(struct mtd_info *master,
153 + const struct ofnandpart_data *data);
156 * struct nand_sdr_timings - SDR NAND chip timings