1 // SPDX-License-Identifier: GPL-2.0-only
3 * Rockchip eFuse Driver
5 * Copyright (c) 2015 Rockchip Electronics Co. Ltd.
6 * Author: Caesar Wang <wxt@rock-chips.com>
10 #include <linux/delay.h>
11 #include <linux/device.h>
13 #include <linux/module.h>
14 #include <linux/nvmem-provider.h>
15 #include <linux/slab.h>
17 #include <linux/of_platform.h>
18 #include <linux/platform_device.h>
20 #define RK3288_A_SHIFT 6
21 #define RK3288_A_MASK 0x3ff
22 #define RK3288_PGENB BIT(3)
23 #define RK3288_LOAD BIT(2)
24 #define RK3288_STROBE BIT(1)
25 #define RK3288_CSB BIT(0)
27 #define RK3328_SECURE_SIZES 96
28 #define RK3328_INT_STATUS 0x0018
29 #define RK3328_DOUT 0x0020
30 #define RK3328_AUTO_CTRL 0x0024
31 #define RK3328_INT_FINISH BIT(0)
32 #define RK3328_AUTO_ENB BIT(0)
33 #define RK3328_AUTO_RD BIT(1)
35 #define RK3399_A_SHIFT 16
36 #define RK3399_A_MASK 0x3ff
37 #define RK3399_NBYTES 4
38 #define RK3399_STROBSFTSEL BIT(9)
39 #define RK3399_RSB BIT(7)
40 #define RK3399_PD BIT(5)
41 #define RK3399_PGENB BIT(3)
42 #define RK3399_LOAD BIT(2)
43 #define RK3399_STROBE BIT(1)
44 #define RK3399_CSB BIT(0)
46 #define REG_EFUSE_CTRL 0x0000
47 #define REG_EFUSE_DOUT 0x0004
49 struct rockchip_efuse_chip
{
55 static int rockchip_rk3288_efuse_read(void *context
, unsigned int offset
,
56 void *val
, size_t bytes
)
58 struct rockchip_efuse_chip
*efuse
= context
;
62 ret
= clk_prepare_enable(efuse
->clk
);
64 dev_err(efuse
->dev
, "failed to prepare/enable efuse clk\n");
68 writel(RK3288_LOAD
| RK3288_PGENB
, efuse
->base
+ REG_EFUSE_CTRL
);
71 writel(readl(efuse
->base
+ REG_EFUSE_CTRL
) &
72 (~(RK3288_A_MASK
<< RK3288_A_SHIFT
)),
73 efuse
->base
+ REG_EFUSE_CTRL
);
74 writel(readl(efuse
->base
+ REG_EFUSE_CTRL
) |
75 ((offset
++ & RK3288_A_MASK
) << RK3288_A_SHIFT
),
76 efuse
->base
+ REG_EFUSE_CTRL
);
78 writel(readl(efuse
->base
+ REG_EFUSE_CTRL
) |
79 RK3288_STROBE
, efuse
->base
+ REG_EFUSE_CTRL
);
81 *buf
++ = readb(efuse
->base
+ REG_EFUSE_DOUT
);
82 writel(readl(efuse
->base
+ REG_EFUSE_CTRL
) &
83 (~RK3288_STROBE
), efuse
->base
+ REG_EFUSE_CTRL
);
87 /* Switch to standby mode */
88 writel(RK3288_PGENB
| RK3288_CSB
, efuse
->base
+ REG_EFUSE_CTRL
);
90 clk_disable_unprepare(efuse
->clk
);
95 static int rockchip_rk3328_efuse_read(void *context
, unsigned int offset
,
96 void *val
, size_t bytes
)
98 struct rockchip_efuse_chip
*efuse
= context
;
99 unsigned int addr_start
, addr_end
, addr_offset
, addr_len
;
100 u32 out_value
, status
;
104 ret
= clk_prepare_enable(efuse
->clk
);
106 dev_err(efuse
->dev
, "failed to prepare/enable efuse clk\n");
110 /* 128 Byte efuse, 96 Byte for secure, 32 Byte for non-secure */
111 offset
+= RK3328_SECURE_SIZES
;
112 addr_start
= rounddown(offset
, RK3399_NBYTES
) / RK3399_NBYTES
;
113 addr_end
= roundup(offset
+ bytes
, RK3399_NBYTES
) / RK3399_NBYTES
;
114 addr_offset
= offset
% RK3399_NBYTES
;
115 addr_len
= addr_end
- addr_start
;
117 buf
= kzalloc(array3_size(addr_len
, RK3399_NBYTES
, sizeof(*buf
)),
125 writel(RK3328_AUTO_RD
| RK3328_AUTO_ENB
|
126 ((addr_start
++ & RK3399_A_MASK
) << RK3399_A_SHIFT
),
127 efuse
->base
+ RK3328_AUTO_CTRL
);
129 status
= readl(efuse
->base
+ RK3328_INT_STATUS
);
130 if (!(status
& RK3328_INT_FINISH
)) {
134 out_value
= readl(efuse
->base
+ RK3328_DOUT
);
135 writel(RK3328_INT_FINISH
, efuse
->base
+ RK3328_INT_STATUS
);
137 memcpy(&buf
[i
], &out_value
, RK3399_NBYTES
);
141 memcpy(val
, buf
+ addr_offset
, bytes
);
145 clk_disable_unprepare(efuse
->clk
);
150 static int rockchip_rk3399_efuse_read(void *context
, unsigned int offset
,
151 void *val
, size_t bytes
)
153 struct rockchip_efuse_chip
*efuse
= context
;
154 unsigned int addr_start
, addr_end
, addr_offset
, addr_len
;
159 ret
= clk_prepare_enable(efuse
->clk
);
161 dev_err(efuse
->dev
, "failed to prepare/enable efuse clk\n");
165 addr_start
= rounddown(offset
, RK3399_NBYTES
) / RK3399_NBYTES
;
166 addr_end
= roundup(offset
+ bytes
, RK3399_NBYTES
) / RK3399_NBYTES
;
167 addr_offset
= offset
% RK3399_NBYTES
;
168 addr_len
= addr_end
- addr_start
;
170 buf
= kzalloc(array3_size(addr_len
, RK3399_NBYTES
, sizeof(*buf
)),
173 clk_disable_unprepare(efuse
->clk
);
177 writel(RK3399_LOAD
| RK3399_PGENB
| RK3399_STROBSFTSEL
| RK3399_RSB
,
178 efuse
->base
+ REG_EFUSE_CTRL
);
181 writel(readl(efuse
->base
+ REG_EFUSE_CTRL
) | RK3399_STROBE
|
182 ((addr_start
++ & RK3399_A_MASK
) << RK3399_A_SHIFT
),
183 efuse
->base
+ REG_EFUSE_CTRL
);
185 out_value
= readl(efuse
->base
+ REG_EFUSE_DOUT
);
186 writel(readl(efuse
->base
+ REG_EFUSE_CTRL
) & (~RK3399_STROBE
),
187 efuse
->base
+ REG_EFUSE_CTRL
);
190 memcpy(&buf
[i
], &out_value
, RK3399_NBYTES
);
194 /* Switch to standby mode */
195 writel(RK3399_PD
| RK3399_CSB
, efuse
->base
+ REG_EFUSE_CTRL
);
197 memcpy(val
, buf
+ addr_offset
, bytes
);
201 clk_disable_unprepare(efuse
->clk
);
206 static struct nvmem_config econfig
= {
207 .name
= "rockchip-efuse",
208 .add_legacy_fixed_of_cells
= true,
209 .type
= NVMEM_TYPE_OTP
,
215 static const struct of_device_id rockchip_efuse_match
[] = {
216 /* deprecated but kept around for dts binding compatibility */
218 .compatible
= "rockchip,rockchip-efuse",
219 .data
= (void *)&rockchip_rk3288_efuse_read
,
222 .compatible
= "rockchip,rk3066a-efuse",
223 .data
= (void *)&rockchip_rk3288_efuse_read
,
226 .compatible
= "rockchip,rk3188-efuse",
227 .data
= (void *)&rockchip_rk3288_efuse_read
,
230 .compatible
= "rockchip,rk3228-efuse",
231 .data
= (void *)&rockchip_rk3288_efuse_read
,
234 .compatible
= "rockchip,rk3288-efuse",
235 .data
= (void *)&rockchip_rk3288_efuse_read
,
238 .compatible
= "rockchip,rk3368-efuse",
239 .data
= (void *)&rockchip_rk3288_efuse_read
,
242 .compatible
= "rockchip,rk3328-efuse",
243 .data
= (void *)&rockchip_rk3328_efuse_read
,
246 .compatible
= "rockchip,rk3399-efuse",
247 .data
= (void *)&rockchip_rk3399_efuse_read
,
251 MODULE_DEVICE_TABLE(of
, rockchip_efuse_match
);
253 static int rockchip_efuse_probe(struct platform_device
*pdev
)
255 struct resource
*res
;
256 struct nvmem_device
*nvmem
;
257 struct rockchip_efuse_chip
*efuse
;
259 struct device
*dev
= &pdev
->dev
;
261 data
= of_device_get_match_data(dev
);
263 dev_err(dev
, "failed to get match data\n");
267 efuse
= devm_kzalloc(dev
, sizeof(struct rockchip_efuse_chip
),
272 efuse
->base
= devm_platform_get_and_ioremap_resource(pdev
, 0, &res
);
273 if (IS_ERR(efuse
->base
))
274 return PTR_ERR(efuse
->base
);
276 efuse
->clk
= devm_clk_get(dev
, "pclk_efuse");
277 if (IS_ERR(efuse
->clk
))
278 return PTR_ERR(efuse
->clk
);
281 if (of_property_read_u32(dev
->of_node
, "rockchip,efuse-size",
283 econfig
.size
= resource_size(res
);
284 econfig
.reg_read
= data
;
285 econfig
.priv
= efuse
;
286 econfig
.dev
= efuse
->dev
;
287 nvmem
= devm_nvmem_register(dev
, &econfig
);
289 return PTR_ERR_OR_ZERO(nvmem
);
292 static struct platform_driver rockchip_efuse_driver
= {
293 .probe
= rockchip_efuse_probe
,
295 .name
= "rockchip-efuse",
296 .of_match_table
= rockchip_efuse_match
,
300 module_platform_driver(rockchip_efuse_driver
);
301 MODULE_DESCRIPTION("rockchip_efuse driver");
302 MODULE_LICENSE("GPL v2");