1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (c) 2018 Rockchip Electronics Co. Ltd.
6 * Author: Finley Xiao <finley.xiao@rock-chips.com>
10 #include <linux/delay.h>
11 #include <linux/device.h>
13 #include <linux/iopoll.h>
14 #include <linux/module.h>
15 #include <linux/nvmem-provider.h>
16 #include <linux/reset.h>
17 #include <linux/slab.h>
19 #include <linux/of_platform.h>
20 #include <linux/platform_device.h>
22 /* OTP Register Offsets */
23 #define OTPC_SBPI_CTRL 0x0020
24 #define OTPC_SBPI_CMD_VALID_PRE 0x0024
25 #define OTPC_SBPI_CS_VALID_PRE 0x0028
26 #define OTPC_SBPI_STATUS 0x002C
27 #define OTPC_USER_CTRL 0x0100
28 #define OTPC_USER_ADDR 0x0104
29 #define OTPC_USER_ENABLE 0x0108
30 #define OTPC_USER_Q 0x0124
31 #define OTPC_INT_STATUS 0x0304
32 #define OTPC_SBPI_CMD0_OFFSET 0x1000
33 #define OTPC_SBPI_CMD1_OFFSET 0x1004
35 /* OTP Register bits and masks */
36 #define OTPC_USER_ADDR_MASK GENMASK(31, 16)
37 #define OTPC_USE_USER BIT(0)
38 #define OTPC_USE_USER_MASK GENMASK(16, 16)
39 #define OTPC_USER_FSM_ENABLE BIT(0)
40 #define OTPC_USER_FSM_ENABLE_MASK GENMASK(16, 16)
41 #define OTPC_SBPI_DONE BIT(1)
42 #define OTPC_USER_DONE BIT(2)
44 #define SBPI_DAP_ADDR 0x02
45 #define SBPI_DAP_ADDR_SHIFT 8
46 #define SBPI_DAP_ADDR_MASK GENMASK(31, 24)
47 #define SBPI_CMD_VALID_MASK GENMASK(31, 16)
48 #define SBPI_DAP_CMD_WRF 0xC0
49 #define SBPI_DAP_REG_ECC 0x3A
50 #define SBPI_ECC_ENABLE 0x00
51 #define SBPI_ECC_DISABLE 0x09
52 #define SBPI_ENABLE BIT(0)
53 #define SBPI_ENABLE_MASK GENMASK(16, 16)
55 #define OTPC_TIMEOUT 10000
60 struct clk_bulk_data
*clks
;
62 struct reset_control
*rst
;
65 /* list of required clocks */
66 static const char * const rockchip_otp_clocks
[] = {
67 "otp", "apb_pclk", "phy",
70 struct rockchip_data
{
74 static int rockchip_otp_reset(struct rockchip_otp
*otp
)
78 ret
= reset_control_assert(otp
->rst
);
80 dev_err(otp
->dev
, "failed to assert otp phy %d\n", ret
);
86 ret
= reset_control_deassert(otp
->rst
);
88 dev_err(otp
->dev
, "failed to deassert otp phy %d\n", ret
);
95 static int rockchip_otp_wait_status(struct rockchip_otp
*otp
, u32 flag
)
100 ret
= readl_poll_timeout_atomic(otp
->base
+ OTPC_INT_STATUS
, status
,
101 (status
& flag
), 1, OTPC_TIMEOUT
);
105 /* clean int status */
106 writel(flag
, otp
->base
+ OTPC_INT_STATUS
);
111 static int rockchip_otp_ecc_enable(struct rockchip_otp
*otp
, bool enable
)
115 writel(SBPI_DAP_ADDR_MASK
| (SBPI_DAP_ADDR
<< SBPI_DAP_ADDR_SHIFT
),
116 otp
->base
+ OTPC_SBPI_CTRL
);
118 writel(SBPI_CMD_VALID_MASK
| 0x1, otp
->base
+ OTPC_SBPI_CMD_VALID_PRE
);
119 writel(SBPI_DAP_CMD_WRF
| SBPI_DAP_REG_ECC
,
120 otp
->base
+ OTPC_SBPI_CMD0_OFFSET
);
122 writel(SBPI_ECC_ENABLE
, otp
->base
+ OTPC_SBPI_CMD1_OFFSET
);
124 writel(SBPI_ECC_DISABLE
, otp
->base
+ OTPC_SBPI_CMD1_OFFSET
);
126 writel(SBPI_ENABLE_MASK
| SBPI_ENABLE
, otp
->base
+ OTPC_SBPI_CTRL
);
128 ret
= rockchip_otp_wait_status(otp
, OTPC_SBPI_DONE
);
130 dev_err(otp
->dev
, "timeout during ecc_enable\n");
135 static int rockchip_otp_read(void *context
, unsigned int offset
,
136 void *val
, size_t bytes
)
138 struct rockchip_otp
*otp
= context
;
142 ret
= clk_bulk_prepare_enable(otp
->num_clks
, otp
->clks
);
144 dev_err(otp
->dev
, "failed to prepare/enable clks\n");
148 ret
= rockchip_otp_reset(otp
);
150 dev_err(otp
->dev
, "failed to reset otp phy\n");
154 ret
= rockchip_otp_ecc_enable(otp
, false);
156 dev_err(otp
->dev
, "rockchip_otp_ecc_enable err\n");
160 writel(OTPC_USE_USER
| OTPC_USE_USER_MASK
, otp
->base
+ OTPC_USER_CTRL
);
163 writel(offset
++ | OTPC_USER_ADDR_MASK
,
164 otp
->base
+ OTPC_USER_ADDR
);
165 writel(OTPC_USER_FSM_ENABLE
| OTPC_USER_FSM_ENABLE_MASK
,
166 otp
->base
+ OTPC_USER_ENABLE
);
167 ret
= rockchip_otp_wait_status(otp
, OTPC_USER_DONE
);
169 dev_err(otp
->dev
, "timeout during read setup\n");
172 *buf
++ = readb(otp
->base
+ OTPC_USER_Q
);
176 writel(0x0 | OTPC_USE_USER_MASK
, otp
->base
+ OTPC_USER_CTRL
);
178 clk_bulk_disable_unprepare(otp
->num_clks
, otp
->clks
);
183 static struct nvmem_config otp_config
= {
184 .name
= "rockchip-otp",
185 .owner
= THIS_MODULE
,
189 .reg_read
= rockchip_otp_read
,
192 static const struct rockchip_data px30_data
= {
196 static const struct of_device_id rockchip_otp_match
[] = {
198 .compatible
= "rockchip,px30-otp",
199 .data
= (void *)&px30_data
,
202 .compatible
= "rockchip,rk3308-otp",
203 .data
= (void *)&px30_data
,
207 MODULE_DEVICE_TABLE(of
, rockchip_otp_match
);
209 static int rockchip_otp_probe(struct platform_device
*pdev
)
211 struct device
*dev
= &pdev
->dev
;
212 struct rockchip_otp
*otp
;
213 const struct rockchip_data
*data
;
214 struct nvmem_device
*nvmem
;
217 data
= of_device_get_match_data(dev
);
219 dev_err(dev
, "failed to get match data\n");
223 otp
= devm_kzalloc(&pdev
->dev
, sizeof(struct rockchip_otp
),
229 otp
->base
= devm_platform_ioremap_resource(pdev
, 0);
230 if (IS_ERR(otp
->base
))
231 return PTR_ERR(otp
->base
);
233 otp
->num_clks
= ARRAY_SIZE(rockchip_otp_clocks
);
234 otp
->clks
= devm_kcalloc(dev
, otp
->num_clks
,
235 sizeof(*otp
->clks
), GFP_KERNEL
);
239 for (i
= 0; i
< otp
->num_clks
; ++i
)
240 otp
->clks
[i
].id
= rockchip_otp_clocks
[i
];
242 ret
= devm_clk_bulk_get(dev
, otp
->num_clks
, otp
->clks
);
246 otp
->rst
= devm_reset_control_get(dev
, "phy");
247 if (IS_ERR(otp
->rst
))
248 return PTR_ERR(otp
->rst
);
250 otp_config
.size
= data
->size
;
251 otp_config
.priv
= otp
;
252 otp_config
.dev
= dev
;
253 nvmem
= devm_nvmem_register(dev
, &otp_config
);
255 return PTR_ERR_OR_ZERO(nvmem
);
258 static struct platform_driver rockchip_otp_driver
= {
259 .probe
= rockchip_otp_probe
,
261 .name
= "rockchip-otp",
262 .of_match_table
= rockchip_otp_match
,
266 module_platform_driver(rockchip_otp_driver
);
267 MODULE_DESCRIPTION("Rockchip OTP driver");
268 MODULE_LICENSE("GPL v2");