2 * Rockchip eFuse Driver
4 * Copyright (c) 2015 Rockchip Electronics Co. Ltd.
5 * Author: Caesar Wang <wxt@rock-chips.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 #include <linux/clk.h>
18 #include <linux/delay.h>
19 #include <linux/device.h>
21 #include <linux/module.h>
22 #include <linux/nvmem-provider.h>
23 #include <linux/slab.h>
25 #include <linux/of_platform.h>
26 #include <linux/platform_device.h>
28 #define RK3288_A_SHIFT 6
29 #define RK3288_A_MASK 0x3ff
30 #define RK3288_PGENB BIT(3)
31 #define RK3288_LOAD BIT(2)
32 #define RK3288_STROBE BIT(1)
33 #define RK3288_CSB BIT(0)
35 #define RK3328_SECURE_SIZES 96
36 #define RK3328_INT_STATUS 0x0018
37 #define RK3328_DOUT 0x0020
38 #define RK3328_AUTO_CTRL 0x0024
39 #define RK3328_INT_FINISH BIT(0)
40 #define RK3328_AUTO_ENB BIT(0)
41 #define RK3328_AUTO_RD BIT(1)
43 #define RK3399_A_SHIFT 16
44 #define RK3399_A_MASK 0x3ff
45 #define RK3399_NBYTES 4
46 #define RK3399_STROBSFTSEL BIT(9)
47 #define RK3399_RSB BIT(7)
48 #define RK3399_PD BIT(5)
49 #define RK3399_PGENB BIT(3)
50 #define RK3399_LOAD BIT(2)
51 #define RK3399_STROBE BIT(1)
52 #define RK3399_CSB BIT(0)
54 #define REG_EFUSE_CTRL 0x0000
55 #define REG_EFUSE_DOUT 0x0004
57 struct rockchip_efuse_chip
{
63 static int rockchip_rk3288_efuse_read(void *context
, unsigned int offset
,
64 void *val
, size_t bytes
)
66 struct rockchip_efuse_chip
*efuse
= context
;
70 ret
= clk_prepare_enable(efuse
->clk
);
72 dev_err(efuse
->dev
, "failed to prepare/enable efuse clk\n");
76 writel(RK3288_LOAD
| RK3288_PGENB
, efuse
->base
+ REG_EFUSE_CTRL
);
79 writel(readl(efuse
->base
+ REG_EFUSE_CTRL
) &
80 (~(RK3288_A_MASK
<< RK3288_A_SHIFT
)),
81 efuse
->base
+ REG_EFUSE_CTRL
);
82 writel(readl(efuse
->base
+ REG_EFUSE_CTRL
) |
83 ((offset
++ & RK3288_A_MASK
) << RK3288_A_SHIFT
),
84 efuse
->base
+ REG_EFUSE_CTRL
);
86 writel(readl(efuse
->base
+ REG_EFUSE_CTRL
) |
87 RK3288_STROBE
, efuse
->base
+ REG_EFUSE_CTRL
);
89 *buf
++ = readb(efuse
->base
+ REG_EFUSE_DOUT
);
90 writel(readl(efuse
->base
+ REG_EFUSE_CTRL
) &
91 (~RK3288_STROBE
), efuse
->base
+ REG_EFUSE_CTRL
);
95 /* Switch to standby mode */
96 writel(RK3288_PGENB
| RK3288_CSB
, efuse
->base
+ REG_EFUSE_CTRL
);
98 clk_disable_unprepare(efuse
->clk
);
103 static int rockchip_rk3328_efuse_read(void *context
, unsigned int offset
,
104 void *val
, size_t bytes
)
106 struct rockchip_efuse_chip
*efuse
= context
;
107 unsigned int addr_start
, addr_end
, addr_offset
, addr_len
;
108 u32 out_value
, status
;
112 ret
= clk_prepare_enable(efuse
->clk
);
114 dev_err(efuse
->dev
, "failed to prepare/enable efuse clk\n");
118 /* 128 Byte efuse, 96 Byte for secure, 32 Byte for non-secure */
119 offset
+= RK3328_SECURE_SIZES
;
120 addr_start
= rounddown(offset
, RK3399_NBYTES
) / RK3399_NBYTES
;
121 addr_end
= roundup(offset
+ bytes
, RK3399_NBYTES
) / RK3399_NBYTES
;
122 addr_offset
= offset
% RK3399_NBYTES
;
123 addr_len
= addr_end
- addr_start
;
125 buf
= kzalloc(sizeof(*buf
) * addr_len
* RK3399_NBYTES
, GFP_KERNEL
);
132 writel(RK3328_AUTO_RD
| RK3328_AUTO_ENB
|
133 ((addr_start
++ & RK3399_A_MASK
) << RK3399_A_SHIFT
),
134 efuse
->base
+ RK3328_AUTO_CTRL
);
136 status
= readl(efuse
->base
+ RK3328_INT_STATUS
);
137 if (!(status
& RK3328_INT_FINISH
)) {
141 out_value
= readl(efuse
->base
+ RK3328_DOUT
);
142 writel(RK3328_INT_FINISH
, efuse
->base
+ RK3328_INT_STATUS
);
144 memcpy(&buf
[i
], &out_value
, RK3399_NBYTES
);
148 memcpy(val
, buf
+ addr_offset
, bytes
);
152 clk_disable_unprepare(efuse
->clk
);
157 static int rockchip_rk3399_efuse_read(void *context
, unsigned int offset
,
158 void *val
, size_t bytes
)
160 struct rockchip_efuse_chip
*efuse
= context
;
161 unsigned int addr_start
, addr_end
, addr_offset
, addr_len
;
166 ret
= clk_prepare_enable(efuse
->clk
);
168 dev_err(efuse
->dev
, "failed to prepare/enable efuse clk\n");
172 addr_start
= rounddown(offset
, RK3399_NBYTES
) / RK3399_NBYTES
;
173 addr_end
= roundup(offset
+ bytes
, RK3399_NBYTES
) / RK3399_NBYTES
;
174 addr_offset
= offset
% RK3399_NBYTES
;
175 addr_len
= addr_end
- addr_start
;
177 buf
= kzalloc(sizeof(*buf
) * addr_len
* RK3399_NBYTES
, GFP_KERNEL
);
179 clk_disable_unprepare(efuse
->clk
);
183 writel(RK3399_LOAD
| RK3399_PGENB
| RK3399_STROBSFTSEL
| RK3399_RSB
,
184 efuse
->base
+ REG_EFUSE_CTRL
);
187 writel(readl(efuse
->base
+ REG_EFUSE_CTRL
) | RK3399_STROBE
|
188 ((addr_start
++ & RK3399_A_MASK
) << RK3399_A_SHIFT
),
189 efuse
->base
+ REG_EFUSE_CTRL
);
191 out_value
= readl(efuse
->base
+ REG_EFUSE_DOUT
);
192 writel(readl(efuse
->base
+ REG_EFUSE_CTRL
) & (~RK3399_STROBE
),
193 efuse
->base
+ REG_EFUSE_CTRL
);
196 memcpy(&buf
[i
], &out_value
, RK3399_NBYTES
);
200 /* Switch to standby mode */
201 writel(RK3399_PD
| RK3399_CSB
, efuse
->base
+ REG_EFUSE_CTRL
);
203 memcpy(val
, buf
+ addr_offset
, bytes
);
207 clk_disable_unprepare(efuse
->clk
);
212 static struct nvmem_config econfig
= {
213 .name
= "rockchip-efuse",
219 static const struct of_device_id rockchip_efuse_match
[] = {
220 /* deprecated but kept around for dts binding compatibility */
222 .compatible
= "rockchip,rockchip-efuse",
223 .data
= (void *)&rockchip_rk3288_efuse_read
,
226 .compatible
= "rockchip,rk3066a-efuse",
227 .data
= (void *)&rockchip_rk3288_efuse_read
,
230 .compatible
= "rockchip,rk3188-efuse",
231 .data
= (void *)&rockchip_rk3288_efuse_read
,
234 .compatible
= "rockchip,rk3228-efuse",
235 .data
= (void *)&rockchip_rk3288_efuse_read
,
238 .compatible
= "rockchip,rk3288-efuse",
239 .data
= (void *)&rockchip_rk3288_efuse_read
,
242 .compatible
= "rockchip,rk3368-efuse",
243 .data
= (void *)&rockchip_rk3288_efuse_read
,
246 .compatible
= "rockchip,rk3328-efuse",
247 .data
= (void *)&rockchip_rk3328_efuse_read
,
250 .compatible
= "rockchip,rk3399-efuse",
251 .data
= (void *)&rockchip_rk3399_efuse_read
,
255 MODULE_DEVICE_TABLE(of
, rockchip_efuse_match
);
257 static int rockchip_efuse_probe(struct platform_device
*pdev
)
259 struct resource
*res
;
260 struct nvmem_device
*nvmem
;
261 struct rockchip_efuse_chip
*efuse
;
262 const struct of_device_id
*match
;
263 struct device
*dev
= &pdev
->dev
;
265 match
= of_match_device(dev
->driver
->of_match_table
, dev
);
266 if (!match
|| !match
->data
) {
267 dev_err(dev
, "failed to get match data\n");
271 efuse
= devm_kzalloc(&pdev
->dev
, sizeof(struct rockchip_efuse_chip
),
276 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
277 efuse
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
278 if (IS_ERR(efuse
->base
))
279 return PTR_ERR(efuse
->base
);
281 efuse
->clk
= devm_clk_get(&pdev
->dev
, "pclk_efuse");
282 if (IS_ERR(efuse
->clk
))
283 return PTR_ERR(efuse
->clk
);
285 efuse
->dev
= &pdev
->dev
;
286 if (of_property_read_u32(dev
->of_node
, "rockchip,efuse-size",
288 econfig
.size
= resource_size(res
);
289 econfig
.reg_read
= match
->data
;
290 econfig
.priv
= efuse
;
291 econfig
.dev
= efuse
->dev
;
292 nvmem
= nvmem_register(&econfig
);
294 return PTR_ERR(nvmem
);
296 platform_set_drvdata(pdev
, nvmem
);
301 static int rockchip_efuse_remove(struct platform_device
*pdev
)
303 struct nvmem_device
*nvmem
= platform_get_drvdata(pdev
);
305 return nvmem_unregister(nvmem
);
308 static struct platform_driver rockchip_efuse_driver
= {
309 .probe
= rockchip_efuse_probe
,
310 .remove
= rockchip_efuse_remove
,
312 .name
= "rockchip-efuse",
313 .of_match_table
= rockchip_efuse_match
,
317 module_platform_driver(rockchip_efuse_driver
);
318 MODULE_DESCRIPTION("rockchip_efuse driver");
319 MODULE_LICENSE("GPL v2");