kernel: update 4.1 to 4.1.5
[openwrt.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 --- /dev/null
21 +++ b/drivers/mtd/nand/ofnandpart.c
22 @@ -0,0 +1,104 @@
23 +/*
24 + * NAND Flash partitions described by the OF (or flattened) device tree
25 + *
26 + * Copyright © 2014 Boris BREZILLON <b.brezillon.dev@gmail.com>
27 + *
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.
32 + */
33 +
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>
40 +
41 +static inline bool node_has_compatible(struct device_node *pp)
42 +{
43 +       return of_get_property(pp, "compatible", NULL);
44 +}
45 +
46 +int ofnandpart_parse(struct mtd_info *master,
47 +                    const struct ofnandpart_data *data)
48 +{
49 +       struct device_node *node;
50 +       const char *partname;
51 +       struct device_node *pp;
52 +       int i;
53 +
54 +       if (!data)
55 +               return 0;
56 +
57 +       node = data->node;
58 +       if (!node)
59 +               return 0;
60 +
61 +       i = 0;
62 +       for_each_child_of_node(node,  pp) {
63 +               const __be32 *reg;
64 +               int len;
65 +               int a_cells, s_cells;
66 +               uint64_t offset, size;
67 +               uint32_t mask_flags = 0;
68 +               struct nand_part *part;
69 +
70 +               if (node_has_compatible(pp))
71 +                       continue;
72 +
73 +               reg = of_get_property(pp, "reg", &len);
74 +               if (!reg)
75 +                       continue;
76 +
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);
81 +
82 +               partname = of_get_property(pp, "label", &len);
83 +               if (!partname)
84 +                       partname = of_get_property(pp, "name", &len);
85 +
86 +               if (of_get_property(pp, "read-only", &len))
87 +                       mask_flags |= MTD_WRITEABLE;
88 +
89 +               if (of_get_property(pp, "lock", &len))
90 +                       mask_flags |= MTD_POWERUP_LOCK;
91 +
92 +               if (data->parse)
93 +                       part = data->parse(data->priv, master, pp);
94 +               else
95 +                       part = nandpart_alloc();
96 +
97 +               if (IS_ERR(part))
98 +                       continue;
99 +
100 +               part->offset = offset;
101 +               part->master = master;
102 +               part->mtd.name = partname;
103 +               part->mtd.size = size;
104 +               part->mtd.flags = mask_flags;
105 +
106 +               if (nand_add_partition(master, part)) {
107 +                       if (part->release)
108 +                               part->release(part);
109 +                       continue;
110 +               }
111 +
112 +               i++;
113 +       }
114 +
115 +       if (!i) {
116 +               of_node_put(pp);
117 +               pr_err("No valid partition found on %s\n", node->full_name);
118 +       }
119 +
120 +       return i;
121 +}
122 +EXPORT_SYMBOL(ofnandpart_parse);
123 +
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
130                 : 0;
131  }
132  
133 +/**
134 + * struct ofnandpart_data - struct used to retrieve NAND partitions from a DT
135 + *                         node
136 + * @parse:             driver specific parser function
137 + * @priv:              driver private data
138 + * @node:              OF node containing NAND partitions
139 + */
140 +struct ofnandpart_data {
141 +       struct nand_part *(*parse)(void *priv, struct mtd_info *master,
142 +                                  struct device_node *pp);
143 +       void *priv;
144 +       struct device_node *node;
145 +};
146 +
147 +int ofnandpart_parse(struct mtd_info *master,
148 +                    const struct ofnandpart_data *data);
149 +
150  /*
151   * struct nand_sdr_timings - SDR NAND chip timings
152   *