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