1 From 803eb124e1a64e42888542c3444bfe6dac412c7f Mon Sep 17 00:00:00 2001
2 From: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
3 Date: Mon, 4 Jan 2021 09:41:35 +0530
4 Subject: mtd: parsers: Add Qcom SMEM parser
6 NAND based Qualcomm platforms have the partition table populated in the
7 Shared Memory (SMEM). Hence, add a parser for parsing the partitions
10 Signed-off-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
11 Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
12 Link: https://lore.kernel.org/linux-mtd/20210104041137.113075-3-manivannan.sadhasivam@linaro.org
14 drivers/mtd/parsers/Kconfig | 8 ++
15 drivers/mtd/parsers/Makefile | 1 +
16 drivers/mtd/parsers/qcomsmempart.c | 170 +++++++++++++++++++++++++++++++++++++
17 3 files changed, 179 insertions(+)
18 create mode 100644 drivers/mtd/parsers/qcomsmempart.c
20 --- a/drivers/mtd/parsers/Kconfig
21 +++ b/drivers/mtd/parsers/Kconfig
22 @@ -196,6 +196,14 @@ config MTD_REDBOOT_PARTS_READONLY
24 endif # MTD_REDBOOT_PARTS
26 +config MTD_QCOMSMEM_PARTS
27 + tristate "Qualcomm SMEM NAND flash partition parser"
28 + depends on MTD_NAND_QCOM || COMPILE_TEST
29 + depends on QCOM_SMEM
31 + This provides support for parsing partitions from Shared Memory (SMEM)
32 + for NAND flash on Qualcomm platforms.
34 config MTD_ROUTERBOOT_PARTS
35 tristate "RouterBoot flash partition parser"
37 --- a/drivers/mtd/parsers/Makefile
38 +++ b/drivers/mtd/parsers/Makefile
39 @@ -13,4 +13,5 @@ obj-$(CONFIG_MTD_AFS_PARTS) += afs.o
40 obj-$(CONFIG_MTD_PARSER_TRX) += parser_trx.o
41 obj-$(CONFIG_MTD_SHARPSL_PARTS) += sharpslpart.o
42 obj-$(CONFIG_MTD_REDBOOT_PARTS) += redboot.o
43 +obj-$(CONFIG_MTD_QCOMSMEM_PARTS) += qcomsmempart.o
44 obj-$(CONFIG_MTD_ROUTERBOOT_PARTS) += routerbootpart.o
46 +++ b/drivers/mtd/parsers/qcomsmempart.c
48 +// SPDX-License-Identifier: GPL-2.0-only
50 + * Qualcomm SMEM NAND flash partition parser
52 + * Copyright (C) 2020, Linaro Ltd.
55 +#include <linux/ctype.h>
56 +#include <linux/module.h>
57 +#include <linux/mtd/mtd.h>
58 +#include <linux/mtd/partitions.h>
59 +#include <linux/slab.h>
60 +#include <linux/soc/qcom/smem.h>
62 +#define SMEM_AARM_PARTITION_TABLE 9
65 +#define SMEM_FLASH_PART_MAGIC1 0x55ee73aa
66 +#define SMEM_FLASH_PART_MAGIC2 0xe35ebddb
67 +#define SMEM_FLASH_PTABLE_V3 3
68 +#define SMEM_FLASH_PTABLE_V4 4
69 +#define SMEM_FLASH_PTABLE_MAX_PARTS_V3 16
70 +#define SMEM_FLASH_PTABLE_MAX_PARTS_V4 48
71 +#define SMEM_FLASH_PTABLE_HDR_LEN (4 * sizeof(u32))
72 +#define SMEM_FLASH_PTABLE_NAME_SIZE 16
75 + * struct smem_flash_pentry - SMEM Flash partition entry
76 + * @name: Name of the partition
77 + * @offset: Offset in blocks
78 + * @length: Length of the partition in blocks
79 + * @attr: Flags for this partition
81 +struct smem_flash_pentry {
82 + char name[SMEM_FLASH_PTABLE_NAME_SIZE];
86 +} __packed __aligned(4);
89 + * struct smem_flash_ptable - SMEM Flash partition table
90 + * @magic1: Partition table Magic 1
91 + * @magic2: Partition table Magic 2
92 + * @version: Partition table version
93 + * @numparts: Number of partitions in this ptable
94 + * @pentry: Flash partition entries belonging to this ptable
96 +struct smem_flash_ptable {
101 + struct smem_flash_pentry pentry[SMEM_FLASH_PTABLE_MAX_PARTS_V4];
102 +} __packed __aligned(4);
104 +static int parse_qcomsmem_part(struct mtd_info *mtd,
105 + const struct mtd_partition **pparts,
106 + struct mtd_part_parser_data *data)
108 + struct smem_flash_pentry *pentry;
109 + struct smem_flash_ptable *ptable;
110 + size_t len = SMEM_FLASH_PTABLE_HDR_LEN;
111 + struct mtd_partition *parts;
112 + int ret, i, numparts;
115 + pr_debug("Parsing partition table info from SMEM\n");
116 + ptable = qcom_smem_get(SMEM_APPS, SMEM_AARM_PARTITION_TABLE, &len);
117 + if (IS_ERR(ptable)) {
118 + pr_err("Error reading partition table header\n");
119 + return PTR_ERR(ptable);
122 + /* Verify ptable magic */
123 + if (le32_to_cpu(ptable->magic1) != SMEM_FLASH_PART_MAGIC1 ||
124 + le32_to_cpu(ptable->magic2) != SMEM_FLASH_PART_MAGIC2) {
125 + pr_err("Partition table magic verification failed\n");
129 + /* Ensure that # of partitions is less than the max we have allocated */
130 + numparts = le32_to_cpu(ptable->numparts);
131 + if (numparts > SMEM_FLASH_PTABLE_MAX_PARTS_V4) {
132 + pr_err("Partition numbers exceed the max limit\n");
136 + /* Find out length of partition data based on table version */
137 + if (le32_to_cpu(ptable->version) <= SMEM_FLASH_PTABLE_V3) {
138 + len = SMEM_FLASH_PTABLE_HDR_LEN + SMEM_FLASH_PTABLE_MAX_PARTS_V3 *
139 + sizeof(struct smem_flash_pentry);
140 + } else if (le32_to_cpu(ptable->version) == SMEM_FLASH_PTABLE_V4) {
141 + len = SMEM_FLASH_PTABLE_HDR_LEN + SMEM_FLASH_PTABLE_MAX_PARTS_V4 *
142 + sizeof(struct smem_flash_pentry);
144 + pr_err("Unknown ptable version (%d)", le32_to_cpu(ptable->version));
149 + * Now that the partition table header has been parsed, verified
150 + * and the length of the partition table calculated, read the
151 + * complete partition table
153 + ptable = qcom_smem_get(SMEM_APPS, SMEM_AARM_PARTITION_TABLE, &len);
154 + if (IS_ERR_OR_NULL(ptable)) {
155 + pr_err("Error reading partition table\n");
156 + return PTR_ERR(ptable);
159 + parts = kcalloc(numparts, sizeof(*parts), GFP_KERNEL);
163 + for (i = 0; i < numparts; i++) {
164 + pentry = &ptable->pentry[i];
165 + if (pentry->name[0] == '\0')
168 + name = kstrdup(pentry->name, GFP_KERNEL);
171 + goto out_free_parts;
174 + /* Convert name to lower case */
175 + for (c = name; *c != '\0'; c++)
178 + parts[i].name = name;
179 + parts[i].offset = le32_to_cpu(pentry->offset) * mtd->erasesize;
180 + parts[i].mask_flags = pentry->attr;
181 + parts[i].size = le32_to_cpu(pentry->length) * mtd->erasesize;
182 + pr_debug("%d: %s offs=0x%08x size=0x%08x attr:0x%08x\n",
183 + i, pentry->name, le32_to_cpu(pentry->offset),
184 + le32_to_cpu(pentry->length), pentry->attr);
187 + pr_debug("SMEM partition table found: ver: %d len: %d\n",
188 + le32_to_cpu(ptable->version), numparts);
195 + kfree(parts[i].name);
202 +static const struct of_device_id qcomsmem_of_match_table[] = {
203 + { .compatible = "qcom,smem-part" },
206 +MODULE_DEVICE_TABLE(of, qcomsmem_of_match_table);
208 +static struct mtd_part_parser mtd_parser_qcomsmem = {
209 + .parse_fn = parse_qcomsmem_part,
210 + .name = "qcomsmem",
211 + .of_match_table = qcomsmem_of_match_table,
213 +module_mtd_part_parser(mtd_parser_qcomsmem);
215 +MODULE_LICENSE("GPL v2");
216 +MODULE_AUTHOR("Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>");
217 +MODULE_DESCRIPTION("Qualcomm SMEM NAND flash partition parser");