1 From 8747ec2e9762ed9ae53b3a590938f454b6a1abdf Mon Sep 17 00:00:00 2001
2 From: Vincent Shih <vincent.sunplus@gmail.com>
3 Date: Wed, 23 Feb 2022 22:35:01 +0000
4 Subject: [PATCH] nvmem: Add driver for OCOTP in Sunplus SP7021
6 Add driver for OCOTP in Sunplus SP7021
8 Signed-off-by: Vincent Shih <vincent.sunplus@gmail.com>
9 Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
10 Link: https://lore.kernel.org/r/20220223223502.29454-3-srinivas.kandagatla@linaro.org
11 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
14 drivers/nvmem/Kconfig | 12 ++
15 drivers/nvmem/Makefile | 2 +
16 drivers/nvmem/sunplus-ocotp.c | 228 ++++++++++++++++++++++++++++++++++
17 4 files changed, 247 insertions(+)
18 create mode 100644 drivers/nvmem/sunplus-ocotp.c
20 --- a/drivers/nvmem/Kconfig
21 +++ b/drivers/nvmem/Kconfig
22 @@ -312,4 +312,16 @@ config NVMEM_LAYERSCAPE_SFP
23 This driver can also be built as a module. If so, the module
24 will be called layerscape-sfp.
26 +config NVMEM_SUNPLUS_OCOTP
27 + tristate "Sunplus SoC OTP support"
28 + depends on SOC_SP7021 || COMPILE_TEST
29 + depends on HAS_IOMEM
31 + This is a driver for the On-chip OTP controller (OCOTP) available
32 + on Sunplus SoCs. It provides access to 128 bytes of one-time
35 + This driver can also be built as a module. If so, the module
36 + will be called nvmem-sunplus-ocotp.
39 --- a/drivers/nvmem/Makefile
40 +++ b/drivers/nvmem/Makefile
41 @@ -63,3 +63,5 @@ obj-$(CONFIG_NVMEM_BRCM_NVRAM) += nvmem_
42 nvmem_brcm_nvram-y := brcm_nvram.o
43 obj-$(CONFIG_NVMEM_LAYERSCAPE_SFP) += nvmem-layerscape-sfp.o
44 nvmem-layerscape-sfp-y := layerscape-sfp.o
45 +obj-$(CONFIG_NVMEM_SUNPLUS_OCOTP) += nvmem_sunplus_ocotp.o
46 +nvmem_sunplus_ocotp-y := sunplus-ocotp.o
48 +++ b/drivers/nvmem/sunplus-ocotp.c
50 +// SPDX-License-Identifier: GPL-2.0
53 + * The OCOTP driver for Sunplus SP7021
55 + * Copyright (C) 2019 Sunplus Technology Inc., All rights reserved.
58 +#include <linux/bitfield.h>
59 +#include <linux/clk.h>
60 +#include <linux/delay.h>
61 +#include <linux/device.h>
62 +#include <linux/io.h>
63 +#include <linux/iopoll.h>
64 +#include <linux/module.h>
65 +#include <linux/nvmem-provider.h>
66 +#include <linux/of_device.h>
67 +#include <linux/platform_device.h>
71 + * Each bank contains 4 words (32 bits).
72 + * Bank 0 starts at offset 0 from the base.
75 +#define OTP_WORDS_PER_BANK 4
76 +#define OTP_WORD_SIZE sizeof(u32)
77 +#define OTP_BIT_ADDR_OF_BANK (8 * OTP_WORD_SIZE * OTP_WORDS_PER_BANK)
78 +#define QAC628_OTP_NUM_BANKS 8
79 +#define QAC628_OTP_SIZE (QAC628_OTP_NUM_BANKS * OTP_WORDS_PER_BANK * OTP_WORD_SIZE)
80 +#define OTP_READ_TIMEOUT_US 200000
83 +#define ADDRESS_8_DATA 0x20
86 +#define OTP_CONTROL_2 0x48
87 +#define OTP_RD_PERIOD GENMASK(15, 8)
88 +#define OTP_RD_PERIOD_MASK ~GENMASK(15, 8)
89 +#define CPU_CLOCK FIELD_PREP(OTP_RD_PERIOD, 30)
90 +#define SEL_BAK_KEY2 BIT(5)
91 +#define SEL_BAK_KEY2_MASK ~BIT(5)
92 +#define SW_TRIM_EN BIT(4)
93 +#define SW_TRIM_EN_MASK ~BIT(4)
94 +#define SEL_BAK_KEY BIT(3)
95 +#define SEL_BAK_KEY_MASK ~BIT(3)
96 +#define OTP_READ BIT(2)
97 +#define OTP_LOAD_SECURE_DATA BIT(1)
98 +#define OTP_LOAD_SECURE_DATA_MASK ~BIT(1)
99 +#define OTP_DO_CRC BIT(0)
100 +#define OTP_DO_CRC_MASK ~BIT(0)
101 +#define OTP_STATUS 0x4c
102 +#define OTP_READ_DONE BIT(4)
103 +#define OTP_READ_DONE_MASK ~BIT(4)
104 +#define OTP_LOAD_SECURE_DONE_MASK ~BIT(2)
105 +#define OTP_READ_ADDRESS 0x50
113 +struct sp_ocotp_priv {
114 + struct device *dev;
115 + void __iomem *base[BASEMAX];
119 +struct sp_ocotp_data {
123 +const struct sp_ocotp_data sp_otp_v0 = {
124 + .size = QAC628_OTP_SIZE,
127 +static int sp_otp_read_real(struct sp_ocotp_priv *otp, int addr, char *value)
129 + unsigned int addr_data;
130 + unsigned int byte_shift;
131 + unsigned int status;
134 + addr_data = addr % (OTP_WORD_SIZE * OTP_WORDS_PER_BANK);
135 + addr_data = addr_data / OTP_WORD_SIZE;
137 + byte_shift = addr % (OTP_WORD_SIZE * OTP_WORDS_PER_BANK);
138 + byte_shift = byte_shift % OTP_WORD_SIZE;
140 + addr = addr / (OTP_WORD_SIZE * OTP_WORDS_PER_BANK);
141 + addr = addr * OTP_BIT_ADDR_OF_BANK;
143 + writel(readl(otp->base[OTPRX] + OTP_STATUS) & OTP_READ_DONE_MASK &
144 + OTP_LOAD_SECURE_DONE_MASK, otp->base[OTPRX] + OTP_STATUS);
145 + writel(addr, otp->base[OTPRX] + OTP_READ_ADDRESS);
146 + writel(readl(otp->base[OTPRX] + OTP_CONTROL_2) | OTP_READ,
147 + otp->base[OTPRX] + OTP_CONTROL_2);
148 + writel(readl(otp->base[OTPRX] + OTP_CONTROL_2) & SEL_BAK_KEY2_MASK & SW_TRIM_EN_MASK
149 + & SEL_BAK_KEY_MASK & OTP_LOAD_SECURE_DATA_MASK & OTP_DO_CRC_MASK,
150 + otp->base[OTPRX] + OTP_CONTROL_2);
151 + writel((readl(otp->base[OTPRX] + OTP_CONTROL_2) & OTP_RD_PERIOD_MASK) | CPU_CLOCK,
152 + otp->base[OTPRX] + OTP_CONTROL_2);
154 + ret = readl_poll_timeout(otp->base[OTPRX] + OTP_STATUS, status,
155 + status & OTP_READ_DONE, 10, OTP_READ_TIMEOUT_US);
160 + *value = (readl(otp->base[HB_GPIO] + ADDRESS_8_DATA + addr_data * OTP_WORD_SIZE)
161 + >> (8 * byte_shift)) & 0xff;
166 +static int sp_ocotp_read(void *priv, unsigned int offset, void *value, size_t bytes)
168 + struct sp_ocotp_priv *otp = priv;
174 + ret = clk_enable(otp->clk);
179 + for (addr = offset; addr < (offset + bytes); addr++) {
180 + ret = sp_otp_read_real(otp, addr, val);
182 + dev_err(otp->dev, "OTP read fail:%d at %d", ret, addr);
190 + clk_disable(otp->clk);
195 +static struct nvmem_config sp_ocotp_nvmem_config = {
196 + .name = "sp-ocotp",
199 + .size = QAC628_OTP_SIZE,
201 + .reg_read = sp_ocotp_read,
202 + .owner = THIS_MODULE,
205 +static int sp_ocotp_probe(struct platform_device *pdev)
207 + struct device *dev = &pdev->dev;
208 + struct nvmem_device *nvmem;
209 + struct sp_ocotp_priv *otp;
210 + struct resource *res;
213 + otp = devm_kzalloc(dev, sizeof(*otp), GFP_KERNEL);
219 + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hb_gpio");
220 + otp->base[HB_GPIO] = devm_ioremap_resource(dev, res);
221 + if (IS_ERR(otp->base[HB_GPIO]))
222 + return PTR_ERR(otp->base[HB_GPIO]);
224 + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "otprx");
225 + otp->base[OTPRX] = devm_ioremap_resource(dev, res);
226 + if (IS_ERR(otp->base[OTPRX]))
227 + return PTR_ERR(otp->base[OTPRX]);
229 + otp->clk = devm_clk_get(&pdev->dev, NULL);
230 + if (IS_ERR(otp->clk))
231 + return dev_err_probe(&pdev->dev, PTR_ERR(otp->clk),
232 + "devm_clk_get fail\n");
234 + ret = clk_prepare(otp->clk);
236 + dev_err(dev, "failed to prepare clk: %d\n", ret);
240 + sp_ocotp_nvmem_config.priv = otp;
241 + sp_ocotp_nvmem_config.dev = dev;
243 + nvmem = devm_nvmem_register(dev, &sp_ocotp_nvmem_config);
245 + return dev_err_probe(&pdev->dev, PTR_ERR(nvmem),
246 + "register nvmem device fail\n");
248 + platform_set_drvdata(pdev, nvmem);
250 + dev_dbg(dev, "banks:%d x wpb:%d x wsize:%d = %d",
251 + (int)QAC628_OTP_NUM_BANKS, (int)OTP_WORDS_PER_BANK,
252 + (int)OTP_WORD_SIZE, (int)QAC628_OTP_SIZE);
254 + dev_info(dev, "by Sunplus (C) 2020");
259 +static const struct of_device_id sp_ocotp_dt_ids[] = {
260 + { .compatible = "sunplus,sp7021-ocotp", .data = &sp_otp_v0 },
263 +MODULE_DEVICE_TABLE(of, sp_ocotp_dt_ids);
265 +static struct platform_driver sp_otp_driver = {
266 + .probe = sp_ocotp_probe,
268 + .name = "sunplus,sp7021-ocotp",
269 + .of_match_table = sp_ocotp_dt_ids,
272 +module_platform_driver(sp_otp_driver);
274 +MODULE_AUTHOR("Vincent Shih <vincent.sunplus@gmail.com>");
275 +MODULE_DESCRIPTION("Sunplus On-Chip OTP driver");
276 +MODULE_LICENSE("GPL");