kernel: Move append-dtb to common image-commands
[librecmc/librecmc.git] / target / linux / ipq806x / patches-4.4 / 037-mtd-add-SMEM-parser-for-QCOM-platforms.patch
1 From 61e8e1b1af77f24339da3f0822a76fa65ed635c6 Mon Sep 17 00:00:00 2001
2 From: Mathieu Olivari <mathieu@codeaurora.org>
3 Date: Thu, 13 Aug 2015 09:53:14 -0700
4 Subject: [PATCH] mtd: add SMEM parser for QCOM platforms
5
6 On QCOM platforms using MTD devices storage (such as IPQ806x), SMEM is
7 used to store partition layout. This new parser can now be used to read
8 SMEM and use it to register an MTD layout according to its content.
9
10 Signed-off-by: Mathieu Olivari <mathieu@codeaurora.org>
11 Signed-off-by: Ram Chandra Jangir <rjangi@codeaurora.org>
12 ---
13  drivers/mtd/Kconfig          |   7 ++
14  drivers/mtd/Makefile         |   1 +
15  drivers/mtd/qcom_smem_part.c | 228 +++++++++++++++++++++++++++++++++++++++++++
16  3 files changed, 236 insertions(+)
17  create mode 100644 drivers/mtd/qcom_smem_part.c
18
19 diff --git a/drivers/mtd/Kconfig b/drivers/mtd/Kconfig
20 index a03ad29..debc887 100644
21 --- a/drivers/mtd/Kconfig
22 +++ b/drivers/mtd/Kconfig
23 @@ -190,6 +190,13 @@
24           You will still need the parsing functions to be called by the driver
25           for your particular device. It won't happen automatically.
26  
27 +config MTD_QCOM_SMEM_PARTS
28 +       tristate "QCOM SMEM partitioning support"
29 +       depends on QCOM_SMEM
30 +       help
31 +         This provides partitions parser for QCOM devices using SMEM
32 +         such as IPQ806x.
33 +
34  comment "User Modules And Translation Layers"
35  
36  #
37 diff --git a/drivers/mtd/Makefile b/drivers/mtd/Makefile
38 index 99bb9a1..2a44a64 100644
39 --- a/drivers/mtd/Makefile
40 +++ b/drivers/mtd/Makefile
41 @@ -16,6 +16,7 @@
42  obj-$(CONFIG_MTD_BCM63XX_PARTS)        += bcm63xxpart.o
43  obj-$(CONFIG_MTD_BCM47XX_PARTS)        += bcm47xxpart.o
44  obj-$(CONFIG_MTD_MYLOADER_PARTS) += myloader.o
45 +obj-$(CONFIG_MTD_QCOM_SMEM_PARTS) += qcom_smem_part.o
46  
47  # 'Users' - code which presents functionality to userspace.
48  obj-$(CONFIG_MTD_BLKDEVS)      += mtd_blkdevs.o
49 diff --git a/drivers/mtd/qcom_smem_part.c b/drivers/mtd/qcom_smem_part.c
50 new file mode 100644
51 index 0000000..f9c1bca
52 --- /dev/null
53 +++ b/drivers/mtd/qcom_smem_part.c
54 @@ -0,0 +1,228 @@
55 +/*
56 + * Copyright (c) 2015, The Linux Foundation. All rights reserved.
57 + *
58 + * This program is free software; you can redistribute it and/or modify
59 + * it under the terms of the GNU General Public License version 2 and
60 + * only version 2 as published by the Free Software Foundation.
61 + *
62 + * This program is distributed in the hope that it will be useful,
63 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
64 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
65 + * GNU General Public License for more details.
66 + */
67 +
68 +#include <linux/kernel.h>
69 +#include <linux/device.h>
70 +#include <linux/slab.h>
71 +
72 +#include <linux/mtd/mtd.h>
73 +#include <linux/mtd/partitions.h>
74 +#include <linux/spi/spi.h>
75 +#include <linux/module.h>
76 +
77 +#include <linux/soc/qcom/smem.h>
78 +
79 +/* Processor/host identifier for the application processor */
80 +#define SMEM_HOST_APPS                 0
81 +
82 +/* SMEM items index */
83 +#define SMEM_AARM_PARTITION_TABLE      9
84 +#define SMEM_BOOT_FLASH_TYPE           421
85 +#define SMEM_BOOT_FLASH_BLOCK_SIZE     424
86 +
87 +/* SMEM Flash types */
88 +#define SMEM_FLASH_NAND                        2
89 +#define SMEM_FLASH_SPI                 6
90 +
91 +#define SMEM_PART_NAME_SZ              16
92 +#define SMEM_PARTS_MAX                 32
93 +
94 +struct smem_partition {
95 +       char name[SMEM_PART_NAME_SZ];
96 +       __le32 start;
97 +       __le32 size;
98 +       __le32 attr;
99 +};
100 +
101 +struct smem_partition_table {
102 +       u8 magic[8];
103 +       __le32 version;
104 +       __le32 len;
105 +       struct smem_partition parts[SMEM_PARTS_MAX];
106 +};
107 +
108 +/* SMEM Magic values in partition table */
109 +static const u8 SMEM_PTABLE_MAGIC[] = {
110 +       0xaa, 0x73, 0xee, 0x55,
111 +       0xdb, 0xbd, 0x5e, 0xe3,
112 +};
113 +
114 +static int qcom_smem_get_flash_blksz(u64 **smem_blksz)
115 +{
116 +       size_t size;
117 +
118 +       *smem_blksz = qcom_smem_get(SMEM_HOST_APPS, SMEM_BOOT_FLASH_BLOCK_SIZE,
119 +                                   &size);
120 +
121 +       if (IS_ERR(*smem_blksz)) {
122 +               pr_err("Unable to read flash blksz from SMEM\n");
123 +               return -ENOENT;
124 +       }
125 +
126 +       if (size != sizeof(**smem_blksz)) {
127 +               pr_err("Invalid flash blksz size in SMEM\n");
128 +               return -EINVAL;
129 +       }
130 +
131 +       return 0;
132 +}
133 +
134 +static int qcom_smem_get_flash_type(u64 **smem_flash_type)
135 +{
136 +       size_t size;
137 +
138 +       *smem_flash_type = qcom_smem_get(SMEM_HOST_APPS, SMEM_BOOT_FLASH_TYPE,
139 +                                       &size);
140 +
141 +       if (IS_ERR(*smem_flash_type)) {
142 +               pr_err("Unable to read flash type from SMEM\n");
143 +               return -ENOENT;
144 +       }
145 +
146 +       if (size != sizeof(**smem_flash_type)) {
147 +               pr_err("Invalid flash type size in SMEM\n");
148 +               return -EINVAL;
149 +       }
150 +
151 +       return 0;
152 +}
153 +
154 +static int qcom_smem_get_flash_partitions(struct smem_partition_table **pparts)
155 +{
156 +       size_t size;
157 +
158 +       *pparts = qcom_smem_get(SMEM_HOST_APPS, SMEM_AARM_PARTITION_TABLE,
159 +                               &size);
160 +
161 +       if (IS_ERR(*pparts)) {
162 +               pr_err("Unable to read partition table from SMEM\n");
163 +               return -ENOENT;
164 +       }
165 +
166 +       return 0;
167 +}
168 +
169 +static int of_dev_node_match(struct device *dev, void *data)
170 +{
171 +       return dev->of_node == data;
172 +}
173 +
174 +static bool is_spi_device(struct device_node *np)
175 +{
176 +       struct device *dev;
177 +
178 +       dev = bus_find_device(&spi_bus_type, NULL, np, of_dev_node_match);
179 +       if (!dev)
180 +               return false;
181 +
182 +       put_device(dev);
183 +       return true;
184 +}
185 +
186 +static int parse_qcom_smem_partitions(struct mtd_info *master,
187 +                                     struct mtd_partition **pparts,
188 +                                     struct mtd_part_parser_data *data)
189 +{
190 +       struct smem_partition_table *smem_parts;
191 +       u64 *smem_flash_type, *smem_blksz;
192 +       struct mtd_partition *mtd_parts;
193 +       struct device_node *of_node = data->of_node;
194 +       int i, ret;
195 +
196 +       /*
197 +        * SMEM will only store the partition table of the boot device.
198 +        * If this is not the boot device, do not return any partition.
199 +        */
200 +       ret = qcom_smem_get_flash_type(&smem_flash_type);
201 +       if (ret < 0)
202 +               return ret;
203 +
204 +       if ((*smem_flash_type == SMEM_FLASH_NAND && !mtd_type_is_nand(master))
205 +           || (*smem_flash_type == SMEM_FLASH_SPI && !is_spi_device(of_node)))
206 +               return 0;
207 +
208 +       /*
209 +        * Just for sanity purpose, make sure the block size in SMEM matches the
210 +        * block size of the MTD device
211 +        */
212 +       ret = qcom_smem_get_flash_blksz(&smem_blksz);
213 +       if (ret < 0)
214 +               return ret;
215 +
216 +       if (*smem_blksz != master->erasesize) {
217 +               pr_err("SMEM block size differs from MTD block size\n");
218 +               return -EINVAL;
219 +       }
220 +
221 +       /* Get partition pointer from SMEM */
222 +       ret = qcom_smem_get_flash_partitions(&smem_parts);
223 +       if (ret < 0)
224 +               return ret;
225 +
226 +       if (memcmp(SMEM_PTABLE_MAGIC, smem_parts->magic,
227 +                  sizeof(SMEM_PTABLE_MAGIC))) {
228 +               pr_err("SMEM partition magic invalid\n");
229 +               return -EINVAL;
230 +       }
231 +
232 +       /* Allocate and populate the mtd structures */
233 +       mtd_parts = kcalloc(le32_to_cpu(smem_parts->len), sizeof(*mtd_parts),
234 +                           GFP_KERNEL);
235 +       if (!mtd_parts)
236 +               return -ENOMEM;
237 +
238 +       for (i = 0; i < smem_parts->len; i++) {
239 +               struct smem_partition *s_part = &smem_parts->parts[i];
240 +               struct mtd_partition *m_part = &mtd_parts[i];
241 +
242 +               m_part->name = s_part->name;
243 +               m_part->size = le32_to_cpu(s_part->size) * (*smem_blksz);
244 +               m_part->offset = le32_to_cpu(s_part->start) * (*smem_blksz);
245 +
246 +               /*
247 +                * The last SMEM partition may have its size marked as
248 +                * something like 0xffffffff, which means "until the end of the
249 +                * flash device". In this case, truncate it.
250 +                */
251 +               if (m_part->offset + m_part->size > master->size)
252 +                       m_part->size = master->size - m_part->offset;
253 +       }
254 +
255 +       *pparts = mtd_parts;
256 +
257 +       return smem_parts->len;
258 +}
259 +
260 +static struct mtd_part_parser qcom_smem_parser = {
261 +       .owner = THIS_MODULE,
262 +       .parse_fn = parse_qcom_smem_partitions,
263 +       .name = "qcom-smem",
264 +};
265 +
266 +static int __init qcom_smem_parser_init(void)
267 +{
268 +       register_mtd_parser(&qcom_smem_parser);
269 +       return 0;
270 +}
271 +
272 +static void __exit qcom_smem_parser_exit(void)
273 +{
274 +       deregister_mtd_parser(&qcom_smem_parser);
275 +}
276 +
277 +module_init(qcom_smem_parser_init);
278 +module_exit(qcom_smem_parser_exit);
279 +
280 +MODULE_LICENSE("GPL");
281 +MODULE_AUTHOR("Mathieu Olivari <mathieu@codeaurora.org>");
282 +MODULE_DESCRIPTION("Parsing code for SMEM based partition tables");