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
21 +++ b/drivers/mtd/nand/ofnandpart.c
24 + * NAND Flash partitions described by the OF (or flattened) device tree
26 + * Copyright © 2014 Boris BREZILLON <b.brezillon.dev@gmail.com>
28 + * This program is free software; you can redistribute it and/or modify it
29 + * under the terms of the GNU General Public License as published by the
30 + * Free Software Foundation; either version 2 of the License, or (at your
31 + * option) any later version.
34 +#include <linux/module.h>
35 +#include <linux/init.h>
36 +#include <linux/of.h>
37 +#include <linux/mtd/mtd.h>
38 +#include <linux/slab.h>
39 +#include <linux/mtd/nand.h>
41 +static inline bool node_has_compatible(struct device_node *pp)
43 + return of_get_property(pp, "compatible", NULL);
46 +int ofnandpart_parse(struct mtd_info *master,
47 + const struct ofnandpart_data *data)
49 + struct device_node *node;
50 + const char *partname;
51 + struct device_node *pp;
62 + for_each_child_of_node(node, pp) {
65 + int a_cells, s_cells;
66 + uint64_t offset, size;
67 + uint32_t mask_flags = 0;
68 + struct nand_part *part;
70 + if (node_has_compatible(pp))
73 + reg = of_get_property(pp, "reg", &len);
77 + a_cells = of_n_addr_cells(pp);
78 + s_cells = of_n_size_cells(pp);
79 + offset = of_read_number(reg, a_cells);
80 + size = of_read_number(reg + a_cells, s_cells);
82 + partname = of_get_property(pp, "label", &len);
84 + partname = of_get_property(pp, "name", &len);
86 + if (of_get_property(pp, "read-only", &len))
87 + mask_flags |= MTD_WRITEABLE;
89 + if (of_get_property(pp, "lock", &len))
90 + mask_flags |= MTD_POWERUP_LOCK;
93 + part = data->parse(data->priv, master, pp);
95 + part = nandpart_alloc();
100 + part->offset = offset;
101 + part->master = master;
102 + part->mtd.name = partname;
103 + part->mtd.size = size;
104 + part->mtd.flags = mask_flags;
106 + if (nand_add_partition(master, part)) {
108 + part->release(part);
117 + pr_err("No valid partition found on %s\n", node->full_name);
122 +EXPORT_SYMBOL(ofnandpart_parse);
124 +MODULE_LICENSE("GPL");
125 +MODULE_DESCRIPTION("Parser for NAND flash partitioning information in device tree");
126 +MODULE_AUTHOR("Boris BREZILLON");
127 --- a/include/linux/mtd/nand.h
128 +++ b/include/linux/mtd/nand.h
129 @@ -1014,6 +1014,23 @@ static inline int jedec_feature(struct n
134 + * struct ofnandpart_data - struct used to retrieve NAND partitions from a DT
136 + * @parse: driver specific parser function
137 + * @priv: driver private data
138 + * @node: OF node containing NAND partitions
140 +struct ofnandpart_data {
141 + struct nand_part *(*parse)(void *priv, struct mtd_info *master,
142 + struct device_node *pp);
144 + struct device_node *node;
147 +int ofnandpart_parse(struct mtd_info *master,
148 + const struct ofnandpart_data *data);
151 * struct nand_sdr_timings - SDR NAND chip timings