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 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
24 You will still need the parsing functions to be called by the driver
25 for your particular device. It won't happen automatically.
27 +config MTD_QCOM_SMEM_PARTS
28 + tristate "QCOM SMEM partitioning support"
29 + depends on QCOM_SMEM
31 + This provides partitions parser for QCOM devices using SMEM
34 comment "User Modules And Translation Layers"
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
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
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
51 index 0000000..f9c1bca
53 +++ b/drivers/mtd/qcom_smem_part.c
56 + * Copyright (c) 2015, The Linux Foundation. All rights reserved.
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.
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.
68 +#include <linux/kernel.h>
69 +#include <linux/device.h>
70 +#include <linux/slab.h>
72 +#include <linux/mtd/mtd.h>
73 +#include <linux/mtd/partitions.h>
74 +#include <linux/spi/spi.h>
75 +#include <linux/module.h>
77 +#include <linux/soc/qcom/smem.h>
79 +/* Processor/host identifier for the application processor */
80 +#define SMEM_HOST_APPS 0
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
87 +/* SMEM Flash types */
88 +#define SMEM_FLASH_NAND 2
89 +#define SMEM_FLASH_SPI 6
91 +#define SMEM_PART_NAME_SZ 16
92 +#define SMEM_PARTS_MAX 32
94 +struct smem_partition {
95 + char name[SMEM_PART_NAME_SZ];
101 +struct smem_partition_table {
105 + struct smem_partition parts[SMEM_PARTS_MAX];
108 +/* SMEM Magic values in partition table */
109 +static const u8 SMEM_PTABLE_MAGIC[] = {
110 + 0xaa, 0x73, 0xee, 0x55,
111 + 0xdb, 0xbd, 0x5e, 0xe3,
114 +static int qcom_smem_get_flash_blksz(u64 **smem_blksz)
118 + *smem_blksz = qcom_smem_get(SMEM_HOST_APPS, SMEM_BOOT_FLASH_BLOCK_SIZE,
121 + if (IS_ERR(*smem_blksz)) {
122 + pr_err("Unable to read flash blksz from SMEM\n");
126 + if (size != sizeof(**smem_blksz)) {
127 + pr_err("Invalid flash blksz size in SMEM\n");
134 +static int qcom_smem_get_flash_type(u64 **smem_flash_type)
138 + *smem_flash_type = qcom_smem_get(SMEM_HOST_APPS, SMEM_BOOT_FLASH_TYPE,
141 + if (IS_ERR(*smem_flash_type)) {
142 + pr_err("Unable to read flash type from SMEM\n");
146 + if (size != sizeof(**smem_flash_type)) {
147 + pr_err("Invalid flash type size in SMEM\n");
154 +static int qcom_smem_get_flash_partitions(struct smem_partition_table **pparts)
158 + *pparts = qcom_smem_get(SMEM_HOST_APPS, SMEM_AARM_PARTITION_TABLE,
161 + if (IS_ERR(*pparts)) {
162 + pr_err("Unable to read partition table from SMEM\n");
169 +static int of_dev_node_match(struct device *dev, void *data)
171 + return dev->of_node == data;
174 +static bool is_spi_device(struct device_node *np)
176 + struct device *dev;
178 + dev = bus_find_device(&spi_bus_type, NULL, np, of_dev_node_match);
186 +static int parse_qcom_smem_partitions(struct mtd_info *master,
187 + struct mtd_partition **pparts,
188 + struct mtd_part_parser_data *data)
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;
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.
200 + ret = qcom_smem_get_flash_type(&smem_flash_type);
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)))
209 + * Just for sanity purpose, make sure the block size in SMEM matches the
210 + * block size of the MTD device
212 + ret = qcom_smem_get_flash_blksz(&smem_blksz);
216 + if (*smem_blksz != master->erasesize) {
217 + pr_err("SMEM block size differs from MTD block size\n");
221 + /* Get partition pointer from SMEM */
222 + ret = qcom_smem_get_flash_partitions(&smem_parts);
226 + if (memcmp(SMEM_PTABLE_MAGIC, smem_parts->magic,
227 + sizeof(SMEM_PTABLE_MAGIC))) {
228 + pr_err("SMEM partition magic invalid\n");
232 + /* Allocate and populate the mtd structures */
233 + mtd_parts = kcalloc(le32_to_cpu(smem_parts->len), sizeof(*mtd_parts),
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];
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);
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.
251 + if (m_part->offset + m_part->size > master->size)
252 + m_part->size = master->size - m_part->offset;
255 + *pparts = mtd_parts;
257 + return smem_parts->len;
260 +static struct mtd_part_parser qcom_smem_parser = {
261 + .owner = THIS_MODULE,
262 + .parse_fn = parse_qcom_smem_partitions,
263 + .name = "qcom-smem",
266 +static int __init qcom_smem_parser_init(void)
268 + register_mtd_parser(&qcom_smem_parser);
272 +static void __exit qcom_smem_parser_exit(void)
274 + deregister_mtd_parser(&qcom_smem_parser);
277 +module_init(qcom_smem_parser_init);
278 +module_exit(qcom_smem_parser_exit);
280 +MODULE_LICENSE("GPL");
281 +MODULE_AUTHOR("Mathieu Olivari <mathieu@codeaurora.org>");
282 +MODULE_DESCRIPTION("Parsing code for SMEM based partition tables");