1 From 737c0c8d07b5f671c0a33cec95965fcb2d2ea893 Mon Sep 17 00:00:00 2001
2 From: Praveen Teja Kundanala <praveen.teja.kundanala@amd.com>
3 Date: Sat, 24 Feb 2024 11:45:12 +0000
4 Subject: [PATCH] nvmem: zynqmp_nvmem: Add support to access efuse
6 Add support to read/write efuse memory map of ZynqMP.
7 Below are the offsets of ZynqMP efuse memory map
8 0 - SOC version(read only)
9 0xC - 0xFC -ZynqMP specific purpose efuses
10 0x100 - 0x17F - Physical Unclonable Function(PUF)
11 efuses repurposed as user efuses
13 Signed-off-by: Praveen Teja Kundanala <praveen.teja.kundanala@amd.com>
14 Acked-by: Kalyani Akula <Kalyani.akula@amd.com>
15 Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
16 Link: https://lore.kernel.org/r/20240224114516.86365-8-srinivas.kandagatla@linaro.org
17 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
19 drivers/nvmem/zynqmp_nvmem.c | 186 +++++++++++++++++++++++++++++++++--
20 1 file changed, 176 insertions(+), 10 deletions(-)
22 --- a/drivers/nvmem/zynqmp_nvmem.c
23 +++ b/drivers/nvmem/zynqmp_nvmem.c
25 * Copyright (C) 2022 - 2023, Advanced Micro Devices, Inc.
28 +#include <linux/dma-mapping.h>
29 #include <linux/module.h>
30 #include <linux/nvmem-provider.h>
33 #include <linux/firmware/xlnx-zynqmp.h>
35 #define SILICON_REVISION_MASK 0xF
36 +#define P_USER_0_64_UPPER_MASK GENMASK(31, 16)
37 +#define P_USER_127_LOWER_4_BIT_MASK GENMASK(3, 0)
38 +#define WORD_INBYTES 4
39 +#define SOC_VER_SIZE 0x4
40 +#define EFUSE_MEMORY_SIZE 0x177
41 +#define UNUSED_SPACE 0x8
42 +#define ZYNQMP_NVMEM_SIZE (SOC_VER_SIZE + UNUSED_SPACE + \
44 +#define SOC_VERSION_OFFSET 0x0
45 +#define EFUSE_START_OFFSET 0xC
46 +#define EFUSE_END_OFFSET 0xFC
47 +#define EFUSE_PUF_START_OFFSET 0x100
48 +#define EFUSE_PUF_MID_OFFSET 0x140
49 +#define EFUSE_PUF_END_OFFSET 0x17F
50 +#define EFUSE_NOT_ENABLED 29
61 + * struct xilinx_efuse - the basic structure
62 + * @src: address of the buffer to store the data to be write/read
63 + * @size: read/write word count
64 + * @offset: read/write offset
65 + * @flag: 0 - represents efuse read and 1- represents efuse write
66 + * @pufuserfuse:0 - represents non-puf efuses, offset is used for read/write
67 + * 1 - represents puf user fuse row number.
69 + * this structure stores all the required details to
70 + * read/write efuse memory.
72 +struct xilinx_efuse {
76 + enum efuse_access flag;
80 +static int zynqmp_efuse_access(void *context, unsigned int offset,
81 + void *val, size_t bytes, enum efuse_access flag,
82 + unsigned int pufflag)
84 + struct device *dev = context;
85 + struct xilinx_efuse *efuse;
86 + dma_addr_t dma_addr;
88 + size_t words = bytes / WORD_INBYTES;
93 -static int zynqmp_nvmem_read(void *context, unsigned int offset,
94 - void *val, size_t bytes)
95 + if (bytes % WORD_INBYTES != 0) {
96 + dev_err(dev, "Bytes requested should be word aligned\n");
100 + if (pufflag == 0 && offset % WORD_INBYTES) {
101 + dev_err(dev, "Offset requested should be word aligned\n");
102 + return -EOPNOTSUPP;
105 + if (pufflag == 1 && flag == EFUSE_WRITE) {
106 + memcpy(&value, val, bytes);
107 + if ((offset == EFUSE_PUF_START_OFFSET ||
108 + offset == EFUSE_PUF_MID_OFFSET) &&
109 + value & P_USER_0_64_UPPER_MASK) {
110 + dev_err(dev, "Only lower 4 bytes are allowed to be programmed in P_USER_0 & P_USER_64\n");
111 + return -EOPNOTSUPP;
114 + if (offset == EFUSE_PUF_END_OFFSET &&
115 + (value & P_USER_127_LOWER_4_BIT_MASK)) {
116 + dev_err(dev, "Only MSB 28 bits are allowed to be programmed for P_USER_127\n");
117 + return -EOPNOTSUPP;
121 + efuse = dma_alloc_coherent(dev, sizeof(struct xilinx_efuse),
122 + &dma_addr, GFP_KERNEL);
126 + data = dma_alloc_coherent(dev, sizeof(bytes),
127 + &dma_buf, GFP_KERNEL);
130 + goto efuse_data_fail;
133 + if (flag == EFUSE_WRITE) {
134 + memcpy(data, val, bytes);
135 + efuse->flag = EFUSE_WRITE;
137 + efuse->flag = EFUSE_READ;
140 + efuse->src = dma_buf;
141 + efuse->size = words;
142 + efuse->offset = offset;
143 + efuse->pufuserfuse = pufflag;
145 + zynqmp_pm_efuse_access(dma_addr, (u32 *)&ret);
147 + if (ret == EFUSE_NOT_ENABLED) {
148 + dev_err(dev, "efuse access is not enabled\n");
151 + dev_err(dev, "Error in efuse read %x\n", ret);
154 + goto efuse_access_err;
157 + if (flag == EFUSE_READ)
158 + memcpy(val, data, bytes);
160 + dma_free_coherent(dev, sizeof(bytes),
163 + dma_free_coherent(dev, sizeof(struct xilinx_efuse),
169 +static int zynqmp_nvmem_read(void *context, unsigned int offset, void *val, size_t bytes)
171 struct device *dev = context;
177 - ret = zynqmp_pm_get_chipid(&idcode, &version);
180 + if (offset >= EFUSE_PUF_START_OFFSET && offset <= EFUSE_PUF_END_OFFSET)
184 + /* Soc version offset is zero */
185 + case SOC_VERSION_OFFSET:
186 + if (bytes != SOC_VER_SIZE)
187 + return -EOPNOTSUPP;
189 + ret = zynqmp_pm_get_chipid((u32 *)&idcode, (u32 *)&version);
193 + dev_dbg(dev, "Read chipid val %x %x\n", idcode, version);
194 + *(int *)val = version & SILICON_REVISION_MASK;
196 + /* Efuse offset starts from 0xc */
197 + case EFUSE_START_OFFSET ... EFUSE_END_OFFSET:
198 + case EFUSE_PUF_START_OFFSET ... EFUSE_PUF_END_OFFSET:
199 + ret = zynqmp_efuse_access(context, offset, val,
200 + bytes, EFUSE_READ, pufflag);
203 + *(u32 *)val = 0xDEADBEEF;
211 +static int zynqmp_nvmem_write(void *context,
212 + unsigned int offset, void *val, size_t bytes)
216 + if (offset < EFUSE_START_OFFSET || offset > EFUSE_PUF_END_OFFSET)
217 + return -EOPNOTSUPP;
219 - dev_dbg(dev, "Read chipid val %x %x\n", idcode, version);
220 - *(int *)val = version & SILICON_REVISION_MASK;
221 + if (offset >= EFUSE_PUF_START_OFFSET && offset <= EFUSE_PUF_END_OFFSET)
225 + return zynqmp_efuse_access(context, offset,
226 + val, bytes, EFUSE_WRITE, pufflag);
229 static const struct of_device_id zynqmp_nvmem_match[] = {
230 @@ -45,11 +211,11 @@ static int zynqmp_nvmem_probe(struct pla
231 econfig.name = "zynqmp-nvmem";
232 econfig.owner = THIS_MODULE;
233 econfig.word_size = 1;
235 + econfig.size = ZYNQMP_NVMEM_SIZE;
237 econfig.add_legacy_fixed_of_cells = true;
238 - econfig.read_only = true;
239 econfig.reg_read = zynqmp_nvmem_read;
240 + econfig.reg_write = zynqmp_nvmem_write;
242 return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &econfig));