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
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 @@ -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.
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"
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
43 # 'Users' - code which presents functionality to userspace.
44 obj-$(CONFIG_MTD_BLKDEVS) += mtd_blkdevs.o
46 +++ b/drivers/mtd/qcom_smem_part.c
49 + * Copyright (c) 2015, The Linux Foundation. All rights reserved.
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.
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.
61 +#include <linux/kernel.h>
62 +#include <linux/device.h>
63 +#include <linux/slab.h>
65 +#include <linux/mtd/mtd.h>
66 +#include <linux/mtd/partitions.h>
67 +#include <linux/spi/spi.h>
68 +#include <linux/module.h>
70 +#include <linux/soc/qcom/smem.h>
72 +/* Processor/host identifier for the application processor */
73 +#define SMEM_HOST_APPS 0
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
80 +/* SMEM Flash types */
81 +#define SMEM_FLASH_NAND 2
82 +#define SMEM_FLASH_SPI 6
84 +#define SMEM_PART_NAME_SZ 16
85 +#define SMEM_PARTS_MAX 32
87 +struct smem_partition {
88 + char name[SMEM_PART_NAME_SZ];
94 +struct smem_partition_table {
98 + struct smem_partition parts[SMEM_PARTS_MAX];
101 +/* SMEM Magic values in partition table */
102 +static const u8 SMEM_PTABLE_MAGIC[] = {
103 + 0xaa, 0x73, 0xee, 0x55,
104 + 0xdb, 0xbd, 0x5e, 0xe3,
107 +static int qcom_smem_get_flash_blksz(u64 **smem_blksz)
111 + *smem_blksz = qcom_smem_get(SMEM_HOST_APPS, SMEM_BOOT_FLASH_BLOCK_SIZE,
114 + if (IS_ERR(*smem_blksz)) {
115 + pr_err("Unable to read flash blksz from SMEM\n");
119 + if (size != sizeof(**smem_blksz)) {
120 + pr_err("Invalid flash blksz size in SMEM\n");
127 +static int qcom_smem_get_flash_type(u64 **smem_flash_type)
131 + *smem_flash_type = qcom_smem_get(SMEM_HOST_APPS, SMEM_BOOT_FLASH_TYPE,
134 + if (IS_ERR(*smem_flash_type)) {
135 + pr_err("Unable to read flash type from SMEM\n");
139 + if (size != sizeof(**smem_flash_type)) {
140 + pr_err("Invalid flash type size in SMEM\n");
147 +static int qcom_smem_get_flash_partitions(struct smem_partition_table **pparts)
151 + *pparts = qcom_smem_get(SMEM_HOST_APPS, SMEM_AARM_PARTITION_TABLE,
154 + if (IS_ERR(*pparts)) {
155 + pr_err("Unable to read partition table from SMEM\n");
162 +static int of_dev_node_match(struct device *dev, void *data)
164 + return dev->of_node == data;
167 +static bool is_spi_device(struct device_node *np)
169 + struct device *dev;
171 + dev = bus_find_device(&spi_bus_type, NULL, np, of_dev_node_match);
179 +static int parse_qcom_smem_partitions(struct mtd_info *master,
180 + struct mtd_partition **pparts,
181 + struct mtd_part_parser_data *data)
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;
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.
193 + ret = qcom_smem_get_flash_type(&smem_flash_type);
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)))
202 + * Just for sanity purpose, make sure the block size in SMEM matches the
203 + * block size of the MTD device
205 + ret = qcom_smem_get_flash_blksz(&smem_blksz);
209 + if (*smem_blksz != master->erasesize) {
210 + pr_err("SMEM block size differs from MTD block size\n");
214 + /* Get partition pointer from SMEM */
215 + ret = qcom_smem_get_flash_partitions(&smem_parts);
219 + if (memcmp(SMEM_PTABLE_MAGIC, smem_parts->magic,
220 + sizeof(SMEM_PTABLE_MAGIC))) {
221 + pr_err("SMEM partition magic invalid\n");
225 + /* Allocate and populate the mtd structures */
226 + mtd_parts = kcalloc(le32_to_cpu(smem_parts->len), sizeof(*mtd_parts),
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];
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);
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.
244 + if (m_part->offset + m_part->size > master->size)
245 + m_part->size = master->size - m_part->offset;
248 + *pparts = mtd_parts;
250 + return smem_parts->len;
253 +static struct mtd_part_parser qcom_smem_parser = {
254 + .owner = THIS_MODULE,
255 + .parse_fn = parse_qcom_smem_partitions,
256 + .name = "qcom-smem",
259 +static int __init qcom_smem_parser_init(void)
261 + register_mtd_parser(&qcom_smem_parser);
265 +static void __exit qcom_smem_parser_exit(void)
267 + deregister_mtd_parser(&qcom_smem_parser);
270 +module_init(qcom_smem_parser_init);
271 +module_exit(qcom_smem_parser_exit);
273 +MODULE_LICENSE("GPL");
274 +MODULE_AUTHOR("Mathieu Olivari <mathieu@codeaurora.org>");
275 +MODULE_DESCRIPTION("Parsing code for SMEM based partition tables");