kernel: bump 5.4 to 5.4.117
[openwrt/staging/dedeckeh.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 --- a/drivers/mtd/parsers/Kconfig
21 +++ b/drivers/mtd/parsers/Kconfig
22 @@ -196,6 +196,14 @@ config MTD_REDBOOT_PARTS_READONLY
23
24 endif # MTD_REDBOOT_PARTS
25
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
30 + help
31 + This provides support for parsing partitions from Shared Memory (SMEM)
32 + for NAND flash on Qualcomm platforms.
33 +
34 config MTD_ROUTERBOOT_PARTS
35 tristate "RouterBoot flash partition parser"
36 depends on MTD && OF
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
45 --- /dev/null
46 +++ b/drivers/mtd/parsers/qcomsmempart.c
47 @@ -0,0 +1,170 @@
48 +// SPDX-License-Identifier: GPL-2.0-only
49 +/*
50 + * Qualcomm SMEM NAND flash partition parser
51 + *
52 + * Copyright (C) 2020, Linaro Ltd.
53 + */
54 +
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>
61 +
62 +#define SMEM_AARM_PARTITION_TABLE 9
63 +#define SMEM_APPS 0
64 +
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
73 +
74 +/**
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
80 + */
81 +struct smem_flash_pentry {
82 + char name[SMEM_FLASH_PTABLE_NAME_SIZE];
83 + __le32 offset;
84 + __le32 length;
85 + u8 attr;
86 +} __packed __aligned(4);
87 +
88 +/**
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
95 + */
96 +struct smem_flash_ptable {
97 + __le32 magic1;
98 + __le32 magic2;
99 + __le32 version;
100 + __le32 numparts;
101 + struct smem_flash_pentry pentry[SMEM_FLASH_PTABLE_MAX_PARTS_V4];
102 +} __packed __aligned(4);
103 +
104 +static int parse_qcomsmem_part(struct mtd_info *mtd,
105 + const struct mtd_partition **pparts,
106 + struct mtd_part_parser_data *data)
107 +{
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;
113 + char *name, *c;
114 +
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);
120 + }
121 +
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");
126 + return -EINVAL;
127 + }
128 +
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");
133 + return -EINVAL;
134 + }
135 +
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);
143 + } else {
144 + pr_err("Unknown ptable version (%d)", le32_to_cpu(ptable->version));
145 + return -EINVAL;
146 + }
147 +
148 + /*
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
152 + */
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);
157 + }
158 +
159 + parts = kcalloc(numparts, sizeof(*parts), GFP_KERNEL);
160 + if (!parts)
161 + return -ENOMEM;
162 +
163 + for (i = 0; i < numparts; i++) {
164 + pentry = &ptable->pentry[i];
165 + if (pentry->name[0] == '\0')
166 + continue;
167 +
168 + name = kstrdup(pentry->name, GFP_KERNEL);
169 + if (!name) {
170 + ret = -ENOMEM;
171 + goto out_free_parts;
172 + }
173 +
174 + /* Convert name to lower case */
175 + for (c = name; *c != '\0'; c++)
176 + *c = tolower(*c);
177 +
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);
185 + }
186 +
187 + pr_debug("SMEM partition table found: ver: %d len: %d\n",
188 + le32_to_cpu(ptable->version), numparts);
189 + *pparts = parts;
190 +
191 + return numparts;
192 +
193 +out_free_parts:
194 + while (--i >= 0)
195 + kfree(parts[i].name);
196 + kfree(parts);
197 + *pparts = NULL;
198 +
199 + return ret;
200 +}
201 +
202 +static const struct of_device_id qcomsmem_of_match_table[] = {
203 + { .compatible = "qcom,smem-part" },
204 + {},
205 +};
206 +MODULE_DEVICE_TABLE(of, qcomsmem_of_match_table);
207 +
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,
212 +};
213 +module_mtd_part_parser(mtd_parser_qcomsmem);
214 +
215 +MODULE_LICENSE("GPL v2");
216 +MODULE_AUTHOR("Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>");
217 +MODULE_DESCRIPTION("Qualcomm SMEM NAND flash partition parser");