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
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>
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
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.
24 +config MTD_QCOM_SMEM_PARTS
25 + tristate "QCOM SMEM partitioning support"
26 + depends on QCOM_SMEM
28 + This provides partitions parser for QCOM devices using SMEM
31 comment "User Modules And Translation Layers"
35 +++ b/drivers/mtd/qcom_smem_part.c
38 + * Copyright (c) 2015, The Linux Foundation. All rights reserved.
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.
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.
50 +#include <linux/kernel.h>
51 +#include <linux/device.h>
52 +#include <linux/slab.h>
54 +#include <linux/mtd/mtd.h>
55 +#include <linux/mtd/partitions.h>
56 +#include <linux/spi/spi.h>
57 +#include <linux/module.h>
59 +#include <linux/soc/qcom/smem.h>
61 +/* Processor/host identifier for the application processor */
62 +#define SMEM_HOST_APPS 0
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
69 +/* SMEM Flash types */
70 +#define SMEM_FLASH_NAND 2
71 +#define SMEM_FLASH_SPI 6
73 +#define SMEM_PART_NAME_SZ 16
74 +#define SMEM_PARTS_MAX 32
76 +struct smem_partition {
77 + char name[SMEM_PART_NAME_SZ];
83 +struct smem_partition_table {
87 + struct smem_partition parts[SMEM_PARTS_MAX];
90 +/* SMEM Magic values in partition table */
91 +static const u8 SMEM_PTABLE_MAGIC[] = {
92 + 0xaa, 0x73, 0xee, 0x55,
93 + 0xdb, 0xbd, 0x5e, 0xe3,
96 +static int qcom_smem_get_flash_blksz(u64 **smem_blksz)
101 + ret = qcom_smem_get(SMEM_HOST_APPS, SMEM_BOOT_FLASH_BLOCK_SIZE,
102 + (void **) smem_blksz, &size);
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)
122 + ret = qcom_smem_get(SMEM_HOST_APPS, SMEM_BOOT_FLASH_TYPE,
123 + (void **) smem_flash_type, &size);
126 + pr_err("Unable to read flash type from SMEM\n");
130 + if (size != sizeof(**smem_flash_type)) {
131 + pr_err("Invalid flash type size in SMEM\n");
138 +static int qcom_smem_get_flash_partitions(struct smem_partition_table **pparts)
143 + ret = qcom_smem_get(SMEM_HOST_APPS, SMEM_AARM_PARTITION_TABLE,
144 + (void **) pparts, &size);
147 + pr_err("Unable to read partition table from SMEM\n");
154 +static int of_dev_node_match(struct device *dev, void *data)
156 + return dev->of_node == data;
159 +static bool is_spi_device(struct device_node *np)
161 + struct device *dev;
163 + dev = bus_find_device(&spi_bus_type, NULL, np, of_dev_node_match);
171 +static int parse_qcom_smem_partitions(struct mtd_info *master,
172 + struct mtd_partition **pparts,
173 + struct mtd_part_parser_data *data)
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;
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.
185 + ret = qcom_smem_get_flash_type(&smem_flash_type);
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)))
194 + * Just for sanity purpose, make sure the block size in SMEM matches the
195 + * block size of the MTD device
197 + ret = qcom_smem_get_flash_blksz(&smem_blksz);
201 + if (*smem_blksz != master->erasesize) {
202 + pr_err("SMEM block size differs from MTD block size\n");
206 + /* Get partition pointer from SMEM */
207 + ret = qcom_smem_get_flash_partitions(&smem_parts);
211 + if (memcmp(SMEM_PTABLE_MAGIC, smem_parts->magic,
212 + sizeof(SMEM_PTABLE_MAGIC))) {
213 + pr_err("SMEM partition magic invalid\n");
217 + /* Allocate and populate the mtd structures */
218 + mtd_parts = kcalloc(le32_to_cpu(smem_parts->len), sizeof(*mtd_parts),
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];
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);
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.
236 + if (m_part->offset + m_part->size > master->size)
237 + m_part->size = master->size - m_part->offset;
240 + *pparts = mtd_parts;
242 + return smem_parts->len;
245 +static struct mtd_part_parser qcom_smem_parser = {
246 + .owner = THIS_MODULE,
247 + .parse_fn = parse_qcom_smem_partitions,
248 + .name = "qcom-smem",
251 +static int __init qcom_smem_parser_init(void)
253 + register_mtd_parser(&qcom_smem_parser);
257 +static void __exit qcom_smem_parser_exit(void)
259 + deregister_mtd_parser(&qcom_smem_parser);
262 +module_init(qcom_smem_parser_init);
263 +module_exit(qcom_smem_parser_exit);
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
276 # 'Users' - code which presents functionality to userspace.
277 obj-$(CONFIG_MTD_BLKDEVS) += mtd_blkdevs.o