1 From d8eeb4de90e968ba32d956728c866f20752cf2c3 Mon Sep 17 00:00:00 2001
2 From: Mathieu Olivari <mathieu@codeaurora.org>
3 Date: Thu, 9 Mar 2017 08:18:08 +0100
4 Subject: [PATCH 31/69] mtd: add SMEM parser for QCOM platforms
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.
10 Signed-off-by: Mathieu Olivari <mathieu@codeaurora.org>
11 Signed-off-by: Ram Chandra Jangir <rjangi@codeaurora.org>
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
19 --- a/drivers/mtd/Kconfig
20 +++ b/drivers/mtd/Kconfig
21 @@ -193,6 +193,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.
25 +config MTD_QCOM_SMEM_PARTS
26 + tristate "QCOM SMEM partitioning support"
27 + depends on QCOM_SMEM
29 + This provides partitions parser for QCOM devices using SMEM
32 comment "User Modules And Translation Layers"
36 +++ b/drivers/mtd/qcom_smem_part.c
39 + * Copyright (c) 2015, The Linux Foundation. All rights reserved.
41 + * This program is free software; you can redistribute it and/or modify
42 + * it under the terms of the GNU General Public License version 2 and
43 + * only version 2 as published by the Free Software Foundation.
45 + * This program is distributed in the hope that it will be useful,
46 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
47 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
48 + * GNU General Public License for more details.
51 +#include <linux/kernel.h>
52 +#include <linux/device.h>
53 +#include <linux/slab.h>
55 +#include <linux/mtd/mtd.h>
56 +#include <linux/mtd/partitions.h>
57 +#include <linux/spi/spi.h>
58 +#include <linux/module.h>
60 +#include <linux/soc/qcom/smem.h>
62 +/* Processor/host identifier for the application processor */
63 +#define SMEM_HOST_APPS 0
65 +/* SMEM items index */
66 +#define SMEM_AARM_PARTITION_TABLE 9
67 +#define SMEM_BOOT_FLASH_TYPE 421
68 +#define SMEM_BOOT_FLASH_BLOCK_SIZE 424
70 +/* SMEM Flash types */
71 +#define SMEM_FLASH_NAND 2
72 +#define SMEM_FLASH_SPI 6
74 +#define SMEM_PART_NAME_SZ 16
75 +#define SMEM_PARTS_MAX 32
77 +struct smem_partition {
78 + char name[SMEM_PART_NAME_SZ];
84 +struct smem_partition_table {
88 + struct smem_partition parts[SMEM_PARTS_MAX];
91 +/* SMEM Magic values in partition table */
92 +static const u8 SMEM_PTABLE_MAGIC[] = {
93 + 0xaa, 0x73, 0xee, 0x55,
94 + 0xdb, 0xbd, 0x5e, 0xe3,
97 +static int qcom_smem_get_flash_blksz(u64 **smem_blksz)
101 + *smem_blksz = qcom_smem_get(SMEM_HOST_APPS, SMEM_BOOT_FLASH_BLOCK_SIZE,
104 + if (IS_ERR(*smem_blksz)) {
105 + pr_err("Unable to read flash blksz from SMEM\n");
109 + if (size != sizeof(**smem_blksz)) {
110 + pr_err("Invalid flash blksz size in SMEM\n");
117 +static int qcom_smem_get_flash_type(u64 **smem_flash_type)
121 + *smem_flash_type = qcom_smem_get(SMEM_HOST_APPS, SMEM_BOOT_FLASH_TYPE,
124 + if (IS_ERR(*smem_flash_type)) {
125 + pr_err("Unable to read flash type from SMEM\n");
129 + if (size != sizeof(**smem_flash_type)) {
130 + pr_err("Invalid flash type size in SMEM\n");
137 +static int qcom_smem_get_flash_partitions(struct smem_partition_table **pparts)
141 + *pparts = qcom_smem_get(SMEM_HOST_APPS, SMEM_AARM_PARTITION_TABLE,
144 + if (IS_ERR(*pparts)) {
145 + pr_err("Unable to read partition table from SMEM\n");
152 +static int of_dev_node_match(struct device *dev, void *data)
154 + return dev->of_node == data;
157 +static bool is_spi_device(struct device_node *np)
159 + struct device *dev;
161 + dev = bus_find_device(&spi_bus_type, NULL, np, of_dev_node_match);
169 +static int parse_qcom_smem_partitions(struct mtd_info *master,
170 + const struct mtd_partition **pparts,
171 + struct mtd_part_parser_data *data)
173 + struct smem_partition_table *smem_parts;
174 + u64 *smem_flash_type, *smem_blksz;
175 + struct mtd_partition *mtd_parts;
176 + struct device_node *of_node = master->dev.of_node;
180 + * SMEM will only store the partition table of the boot device.
181 + * If this is not the boot device, do not return any partition.
183 + ret = qcom_smem_get_flash_type(&smem_flash_type);
187 + if ((*smem_flash_type == SMEM_FLASH_NAND && !mtd_type_is_nand(master))
188 + || (*smem_flash_type == SMEM_FLASH_SPI && !is_spi_device(of_node)))
192 + * Just for sanity purpose, make sure the block size in SMEM matches the
193 + * block size of the MTD device
195 + ret = qcom_smem_get_flash_blksz(&smem_blksz);
199 + if (*smem_blksz != master->erasesize) {
200 + pr_err("SMEM block size differs from MTD block size\n");
204 + /* Get partition pointer from SMEM */
205 + ret = qcom_smem_get_flash_partitions(&smem_parts);
209 + if (memcmp(SMEM_PTABLE_MAGIC, smem_parts->magic,
210 + sizeof(SMEM_PTABLE_MAGIC))) {
211 + pr_err("SMEM partition magic invalid\n");
215 + /* Allocate and populate the mtd structures */
216 + mtd_parts = kcalloc(le32_to_cpu(smem_parts->len), sizeof(*mtd_parts),
221 + for (i = 0; i < smem_parts->len; i++) {
222 + struct smem_partition *s_part = &smem_parts->parts[i];
223 + struct mtd_partition *m_part = &mtd_parts[i];
225 + m_part->name = s_part->name;
226 + m_part->size = le32_to_cpu(s_part->size) * (*smem_blksz);
227 + m_part->offset = le32_to_cpu(s_part->start) * (*smem_blksz);
230 + * The last SMEM partition may have its size marked as
231 + * something like 0xffffffff, which means "until the end of the
232 + * flash device". In this case, truncate it.
234 + if (m_part->offset + m_part->size > master->size)
235 + m_part->size = master->size - m_part->offset;
238 + *pparts = mtd_parts;
240 + return smem_parts->len;
243 +static const struct of_device_id qcom_smem_of_match_table[] = {
244 + { .compatible = "qcom,smem" },
247 +MODULE_DEVICE_TABLE(of, qcom_smem_of_match_table);
249 +static struct mtd_part_parser qcom_smem_parser = {
250 + .owner = THIS_MODULE,
251 + .parse_fn = parse_qcom_smem_partitions,
252 + .name = "qcom-smem",
253 + .of_match_table = qcom_smem_of_match_table,
256 +static int __init qcom_smem_parser_init(void)
258 + register_mtd_parser(&qcom_smem_parser);
262 +static void __exit qcom_smem_parser_exit(void)
264 + deregister_mtd_parser(&qcom_smem_parser);
267 +module_init(qcom_smem_parser_init);
268 +module_exit(qcom_smem_parser_exit);
270 +MODULE_LICENSE("GPL");
271 +MODULE_AUTHOR("Mathieu Olivari <mathieu@codeaurora.org>");
272 +MODULE_DESCRIPTION("Parsing code for SMEM based partition tables");
273 --- a/drivers/mtd/Makefile
274 +++ b/drivers/mtd/Makefile
275 @@ -17,6 +17,7 @@ obj-$(CONFIG_MTD_AR7_PARTS) += ar7part.o
276 obj-$(CONFIG_MTD_BCM63XX_PARTS) += bcm63xxpart.o
277 obj-$(CONFIG_MTD_BCM47XX_PARTS) += bcm47xxpart.o
278 obj-$(CONFIG_MTD_MYLOADER_PARTS) += myloader.o
279 +obj-$(CONFIG_MTD_QCOM_SMEM_PARTS) += qcom_smem_part.o
282 # 'Users' - code which presents functionality to userspace.