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