ipq806x: backport mtd adm and smem driver
[openwrt/staging/zorun.git] / target / linux / ipq806x / patches-5.4 / 101-5.12-mtd-parsers-Add-Qcom-SMEM-parser.patch
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
5
6 NAND based Qualcomm platforms have the partition table populated in the
7 Shared Memory (SMEM). Hence, add a parser for parsing the partitions
8 from it.
9
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
13 ---
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
19
20 diff --git a/drivers/mtd/parsers/Kconfig b/drivers/mtd/parsers/Kconfig
21 index e72354322f628..d90c302290522 100644
22 --- a/drivers/mtd/parsers/Kconfig
23 +++ b/drivers/mtd/parsers/Kconfig
24 @@ -160,6 +160,14 @@ config MTD_REDBOOT_PARTS_READONLY
25 'FIS directory' images, enable this option.
26
27 endif # MTD_REDBOOT_PARTS
28 +
29 +config MTD_QCOMSMEM_PARTS
30 + tristate "Qualcomm SMEM NAND flash partition parser"
31 + depends on MTD_NAND_QCOM || COMPILE_TEST
32 + depends on QCOM_SMEM
33 + help
34 + This provides support for parsing partitions from Shared Memory (SMEM)
35 + for NAND flash on Qualcomm platforms.
36
37 config MTD_ROUTERBOOT_PARTS
38 tristate "RouterBoot flash partition parser"
39 diff --git a/drivers/mtd/parsers/Makefile b/drivers/mtd/parsers/Makefile
40 index b0c5f62f9e858..50eb0b0a22105 100644
41 --- a/drivers/mtd/parsers/Makefile
42 +++ b/drivers/mtd/parsers/Makefile
43 @@ -9,4 +9,5 @@ obj-$(CONFIG_MTD_AFS_PARTS) += afs.o
44 obj-$(CONFIG_MTD_PARSER_TRX) += parser_trx.o
45 obj-$(CONFIG_MTD_SHARPSL_PARTS) += sharpslpart.o
46 obj-$(CONFIG_MTD_REDBOOT_PARTS) += redboot.o
47 +obj-$(CONFIG_MTD_QCOMSMEM_PARTS) += qcomsmempart.o
48 obj-$(CONFIG_MTD_ROUTERBOOT_PARTS) += routerbootpart.o
49 diff --git a/drivers/mtd/parsers/qcomsmempart.c b/drivers/mtd/parsers/qcomsmempart.c
50 new file mode 100644
51 index 0000000000000..808cb33d71f8e
52 --- /dev/null
53 +++ b/drivers/mtd/parsers/qcomsmempart.c
54 @@ -0,0 +1,170 @@
55 +// SPDX-License-Identifier: GPL-2.0-only
56 +/*
57 + * Qualcomm SMEM NAND flash partition parser
58 + *
59 + * Copyright (C) 2020, Linaro Ltd.
60 + */
61 +
62 +#include <linux/ctype.h>
63 +#include <linux/module.h>
64 +#include <linux/mtd/mtd.h>
65 +#include <linux/mtd/partitions.h>
66 +#include <linux/slab.h>
67 +#include <linux/soc/qcom/smem.h>
68 +
69 +#define SMEM_AARM_PARTITION_TABLE 9
70 +#define SMEM_APPS 0
71 +
72 +#define SMEM_FLASH_PART_MAGIC1 0x55ee73aa
73 +#define SMEM_FLASH_PART_MAGIC2 0xe35ebddb
74 +#define SMEM_FLASH_PTABLE_V3 3
75 +#define SMEM_FLASH_PTABLE_V4 4
76 +#define SMEM_FLASH_PTABLE_MAX_PARTS_V3 16
77 +#define SMEM_FLASH_PTABLE_MAX_PARTS_V4 48
78 +#define SMEM_FLASH_PTABLE_HDR_LEN (4 * sizeof(u32))
79 +#define SMEM_FLASH_PTABLE_NAME_SIZE 16
80 +
81 +/**
82 + * struct smem_flash_pentry - SMEM Flash partition entry
83 + * @name: Name of the partition
84 + * @offset: Offset in blocks
85 + * @length: Length of the partition in blocks
86 + * @attr: Flags for this partition
87 + */
88 +struct smem_flash_pentry {
89 + char name[SMEM_FLASH_PTABLE_NAME_SIZE];
90 + __le32 offset;
91 + __le32 length;
92 + u8 attr;
93 +} __packed __aligned(4);
94 +
95 +/**
96 + * struct smem_flash_ptable - SMEM Flash partition table
97 + * @magic1: Partition table Magic 1
98 + * @magic2: Partition table Magic 2
99 + * @version: Partition table version
100 + * @numparts: Number of partitions in this ptable
101 + * @pentry: Flash partition entries belonging to this ptable
102 + */
103 +struct smem_flash_ptable {
104 + __le32 magic1;
105 + __le32 magic2;
106 + __le32 version;
107 + __le32 numparts;
108 + struct smem_flash_pentry pentry[SMEM_FLASH_PTABLE_MAX_PARTS_V4];
109 +} __packed __aligned(4);
110 +
111 +static int parse_qcomsmem_part(struct mtd_info *mtd,
112 + const struct mtd_partition **pparts,
113 + struct mtd_part_parser_data *data)
114 +{
115 + struct smem_flash_pentry *pentry;
116 + struct smem_flash_ptable *ptable;
117 + size_t len = SMEM_FLASH_PTABLE_HDR_LEN;
118 + struct mtd_partition *parts;
119 + int ret, i, numparts;
120 + char *name, *c;
121 +
122 + pr_debug("Parsing partition table info from SMEM\n");
123 + ptable = qcom_smem_get(SMEM_APPS, SMEM_AARM_PARTITION_TABLE, &len);
124 + if (IS_ERR(ptable)) {
125 + pr_err("Error reading partition table header\n");
126 + return PTR_ERR(ptable);
127 + }
128 +
129 + /* Verify ptable magic */
130 + if (le32_to_cpu(ptable->magic1) != SMEM_FLASH_PART_MAGIC1 ||
131 + le32_to_cpu(ptable->magic2) != SMEM_FLASH_PART_MAGIC2) {
132 + pr_err("Partition table magic verification failed\n");
133 + return -EINVAL;
134 + }
135 +
136 + /* Ensure that # of partitions is less than the max we have allocated */
137 + numparts = le32_to_cpu(ptable->numparts);
138 + if (numparts > SMEM_FLASH_PTABLE_MAX_PARTS_V4) {
139 + pr_err("Partition numbers exceed the max limit\n");
140 + return -EINVAL;
141 + }
142 +
143 + /* Find out length of partition data based on table version */
144 + if (le32_to_cpu(ptable->version) <= SMEM_FLASH_PTABLE_V3) {
145 + len = SMEM_FLASH_PTABLE_HDR_LEN + SMEM_FLASH_PTABLE_MAX_PARTS_V3 *
146 + sizeof(struct smem_flash_pentry);
147 + } else if (le32_to_cpu(ptable->version) == SMEM_FLASH_PTABLE_V4) {
148 + len = SMEM_FLASH_PTABLE_HDR_LEN + SMEM_FLASH_PTABLE_MAX_PARTS_V4 *
149 + sizeof(struct smem_flash_pentry);
150 + } else {
151 + pr_err("Unknown ptable version (%d)", le32_to_cpu(ptable->version));
152 + return -EINVAL;
153 + }
154 +
155 + /*
156 + * Now that the partition table header has been parsed, verified
157 + * and the length of the partition table calculated, read the
158 + * complete partition table
159 + */
160 + ptable = qcom_smem_get(SMEM_APPS, SMEM_AARM_PARTITION_TABLE, &len);
161 + if (IS_ERR_OR_NULL(ptable)) {
162 + pr_err("Error reading partition table\n");
163 + return PTR_ERR(ptable);
164 + }
165 +
166 + parts = kcalloc(numparts, sizeof(*parts), GFP_KERNEL);
167 + if (!parts)
168 + return -ENOMEM;
169 +
170 + for (i = 0; i < numparts; i++) {
171 + pentry = &ptable->pentry[i];
172 + if (pentry->name[0] == '\0')
173 + continue;
174 +
175 + name = kstrdup(pentry->name, GFP_KERNEL);
176 + if (!name) {
177 + ret = -ENOMEM;
178 + goto out_free_parts;
179 + }
180 +
181 + /* Convert name to lower case */
182 + for (c = name; *c != '\0'; c++)
183 + *c = tolower(*c);
184 +
185 + parts[i].name = name;
186 + parts[i].offset = le32_to_cpu(pentry->offset) * mtd->erasesize;
187 + parts[i].mask_flags = pentry->attr;
188 + parts[i].size = le32_to_cpu(pentry->length) * mtd->erasesize;
189 + pr_debug("%d: %s offs=0x%08x size=0x%08x attr:0x%08x\n",
190 + i, pentry->name, le32_to_cpu(pentry->offset),
191 + le32_to_cpu(pentry->length), pentry->attr);
192 + }
193 +
194 + pr_debug("SMEM partition table found: ver: %d len: %d\n",
195 + le32_to_cpu(ptable->version), numparts);
196 + *pparts = parts;
197 +
198 + return numparts;
199 +
200 +out_free_parts:
201 + while (--i >= 0)
202 + kfree(parts[i].name);
203 + kfree(parts);
204 + *pparts = NULL;
205 +
206 + return ret;
207 +}
208 +
209 +static const struct of_device_id qcomsmem_of_match_table[] = {
210 + { .compatible = "qcom,smem-part" },
211 + {},
212 +};
213 +MODULE_DEVICE_TABLE(of, qcomsmem_of_match_table);
214 +
215 +static struct mtd_part_parser mtd_parser_qcomsmem = {
216 + .parse_fn = parse_qcomsmem_part,
217 + .name = "qcomsmem",
218 + .of_match_table = qcomsmem_of_match_table,
219 +};
220 +module_mtd_part_parser(mtd_parser_qcomsmem);
221 +
222 +MODULE_LICENSE("GPL v2");
223 +MODULE_AUTHOR("Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>");
224 +MODULE_DESCRIPTION("Qualcomm SMEM NAND flash partition parser");
225 --
226 cgit 1.2.3-1.el7
227