blob: e4579de5d0146845727a17dd11f5600b709d5583 [file] [log] [blame]
Thomas Gleixner5b497af2019-05-29 07:18:09 -07001// SPDX-License-Identifier: GPL-2.0-only
ZhengShunQian03a69562015-09-30 13:56:44 +01002/*
3 * Rockchip eFuse Driver
4 *
5 * Copyright (c) 2015 Rockchip Electronics Co. Ltd.
6 * Author: Caesar Wang <wxt@rock-chips.com>
ZhengShunQian03a69562015-09-30 13:56:44 +01007 */
8
Caesar Wangc37ff3f2015-12-14 09:43:39 +00009#include <linux/clk.h>
10#include <linux/delay.h>
ZhengShunQian03a69562015-09-30 13:56:44 +010011#include <linux/device.h>
12#include <linux/io.h>
13#include <linux/module.h>
Caesar Wangc37ff3f2015-12-14 09:43:39 +000014#include <linux/nvmem-provider.h>
15#include <linux/slab.h>
ZhengShunQian03a69562015-09-30 13:56:44 +010016#include <linux/of.h>
Finley Xiao02baff32016-09-02 10:14:27 +010017#include <linux/of_platform.h>
Caesar Wangc37ff3f2015-12-14 09:43:39 +000018#include <linux/platform_device.h>
ZhengShunQian03a69562015-09-30 13:56:44 +010019
Finley Xiao02baff32016-09-02 10:14:27 +010020#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)
ZhengShunQian03a69562015-09-30 13:56:44 +010026
Finley Xiao9a479b02017-12-15 14:06:09 +000027#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)
34
Finley Xiao02baff32016-09-02 10:14:27 +010035#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)
45
46#define REG_EFUSE_CTRL 0x0000
47#define REG_EFUSE_DOUT 0x0004
ZhengShunQian03a69562015-09-30 13:56:44 +010048
Caesar Wangc37ff3f2015-12-14 09:43:39 +000049struct rockchip_efuse_chip {
ZhengShunQian03a69562015-09-30 13:56:44 +010050 struct device *dev;
51 void __iomem *base;
Caesar Wangc37ff3f2015-12-14 09:43:39 +000052 struct clk *clk;
ZhengShunQian03a69562015-09-30 13:56:44 +010053};
54
Finley Xiao02baff32016-09-02 10:14:27 +010055static int rockchip_rk3288_efuse_read(void *context, unsigned int offset,
56 void *val, size_t bytes)
ZhengShunQian03a69562015-09-30 13:56:44 +010057{
Caesar Wangc37ff3f2015-12-14 09:43:39 +000058 struct rockchip_efuse_chip *efuse = context;
ZhengShunQian03a69562015-09-30 13:56:44 +010059 u8 *buf = val;
60 int ret;
61
Caesar Wangc37ff3f2015-12-14 09:43:39 +000062 ret = clk_prepare_enable(efuse->clk);
ZhengShunQian03a69562015-09-30 13:56:44 +010063 if (ret < 0) {
Caesar Wangc37ff3f2015-12-14 09:43:39 +000064 dev_err(efuse->dev, "failed to prepare/enable efuse clk\n");
ZhengShunQian03a69562015-09-30 13:56:44 +010065 return ret;
66 }
67
Finley Xiao02baff32016-09-02 10:14:27 +010068 writel(RK3288_LOAD | RK3288_PGENB, efuse->base + REG_EFUSE_CTRL);
ZhengShunQian03a69562015-09-30 13:56:44 +010069 udelay(1);
Srinivas Kandagatlacc907552016-04-24 20:28:11 +010070 while (bytes--) {
Caesar Wangc37ff3f2015-12-14 09:43:39 +000071 writel(readl(efuse->base + REG_EFUSE_CTRL) &
Finley Xiao02baff32016-09-02 10:14:27 +010072 (~(RK3288_A_MASK << RK3288_A_SHIFT)),
Caesar Wangc37ff3f2015-12-14 09:43:39 +000073 efuse->base + REG_EFUSE_CTRL);
74 writel(readl(efuse->base + REG_EFUSE_CTRL) |
Finley Xiao02baff32016-09-02 10:14:27 +010075 ((offset++ & RK3288_A_MASK) << RK3288_A_SHIFT),
Caesar Wangc37ff3f2015-12-14 09:43:39 +000076 efuse->base + REG_EFUSE_CTRL);
ZhengShunQian03a69562015-09-30 13:56:44 +010077 udelay(1);
Caesar Wangc37ff3f2015-12-14 09:43:39 +000078 writel(readl(efuse->base + REG_EFUSE_CTRL) |
Finley Xiao02baff32016-09-02 10:14:27 +010079 RK3288_STROBE, efuse->base + REG_EFUSE_CTRL);
ZhengShunQian03a69562015-09-30 13:56:44 +010080 udelay(1);
Caesar Wangc37ff3f2015-12-14 09:43:39 +000081 *buf++ = readb(efuse->base + REG_EFUSE_DOUT);
82 writel(readl(efuse->base + REG_EFUSE_CTRL) &
Finley Xiao02baff32016-09-02 10:14:27 +010083 (~RK3288_STROBE), efuse->base + REG_EFUSE_CTRL);
ZhengShunQian03a69562015-09-30 13:56:44 +010084 udelay(1);
ZhengShunQian03a69562015-09-30 13:56:44 +010085 }
86
87 /* Switch to standby mode */
Finley Xiao02baff32016-09-02 10:14:27 +010088 writel(RK3288_PGENB | RK3288_CSB, efuse->base + REG_EFUSE_CTRL);
89
90 clk_disable_unprepare(efuse->clk);
91
92 return 0;
93}
94
Finley Xiao9a479b02017-12-15 14:06:09 +000095static int rockchip_rk3328_efuse_read(void *context, unsigned int offset,
96 void *val, size_t bytes)
97{
98 struct rockchip_efuse_chip *efuse = context;
99 unsigned int addr_start, addr_end, addr_offset, addr_len;
100 u32 out_value, status;
101 u8 *buf;
102 int ret, i = 0;
103
104 ret = clk_prepare_enable(efuse->clk);
105 if (ret < 0) {
106 dev_err(efuse->dev, "failed to prepare/enable efuse clk\n");
107 return ret;
108 }
109
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;
116
Kees Cook6396bb22018-06-12 14:03:40 -0700117 buf = kzalloc(array3_size(addr_len, RK3399_NBYTES, sizeof(*buf)),
118 GFP_KERNEL);
Finley Xiao9a479b02017-12-15 14:06:09 +0000119 if (!buf) {
120 ret = -ENOMEM;
121 goto nomem;
122 }
123
124 while (addr_len--) {
125 writel(RK3328_AUTO_RD | RK3328_AUTO_ENB |
126 ((addr_start++ & RK3399_A_MASK) << RK3399_A_SHIFT),
127 efuse->base + RK3328_AUTO_CTRL);
128 udelay(4);
129 status = readl(efuse->base + RK3328_INT_STATUS);
130 if (!(status & RK3328_INT_FINISH)) {
131 ret = -EIO;
132 goto err;
133 }
134 out_value = readl(efuse->base + RK3328_DOUT);
135 writel(RK3328_INT_FINISH, efuse->base + RK3328_INT_STATUS);
136
137 memcpy(&buf[i], &out_value, RK3399_NBYTES);
138 i += RK3399_NBYTES;
139 }
140
141 memcpy(val, buf + addr_offset, bytes);
142err:
143 kfree(buf);
144nomem:
145 clk_disable_unprepare(efuse->clk);
146
147 return ret;
148}
149
Finley Xiao02baff32016-09-02 10:14:27 +0100150static int rockchip_rk3399_efuse_read(void *context, unsigned int offset,
151 void *val, size_t bytes)
152{
153 struct rockchip_efuse_chip *efuse = context;
154 unsigned int addr_start, addr_end, addr_offset, addr_len;
155 u32 out_value;
156 u8 *buf;
157 int ret, i = 0;
158
159 ret = clk_prepare_enable(efuse->clk);
160 if (ret < 0) {
161 dev_err(efuse->dev, "failed to prepare/enable efuse clk\n");
162 return ret;
163 }
164
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;
169
Kees Cook6396bb22018-06-12 14:03:40 -0700170 buf = kzalloc(array3_size(addr_len, RK3399_NBYTES, sizeof(*buf)),
171 GFP_KERNEL);
Finley Xiao02baff32016-09-02 10:14:27 +0100172 if (!buf) {
173 clk_disable_unprepare(efuse->clk);
174 return -ENOMEM;
175 }
176
177 writel(RK3399_LOAD | RK3399_PGENB | RK3399_STROBSFTSEL | RK3399_RSB,
178 efuse->base + REG_EFUSE_CTRL);
179 udelay(1);
180 while (addr_len--) {
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);
184 udelay(1);
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);
188 udelay(1);
189
190 memcpy(&buf[i], &out_value, RK3399_NBYTES);
191 i += RK3399_NBYTES;
192 }
193
194 /* Switch to standby mode */
195 writel(RK3399_PD | RK3399_CSB, efuse->base + REG_EFUSE_CTRL);
196
197 memcpy(val, buf + addr_offset, bytes);
198
199 kfree(buf);
ZhengShunQian03a69562015-09-30 13:56:44 +0100200
Caesar Wangc37ff3f2015-12-14 09:43:39 +0000201 clk_disable_unprepare(efuse->clk);
ZhengShunQian03a69562015-09-30 13:56:44 +0100202
203 return 0;
204}
205
ZhengShunQian03a69562015-09-30 13:56:44 +0100206static struct nvmem_config econfig = {
207 .name = "rockchip-efuse",
Srinivas Kandagatlacc907552016-04-24 20:28:11 +0100208 .stride = 1,
209 .word_size = 1,
ZhengShunQian03a69562015-09-30 13:56:44 +0100210 .read_only = true,
211};
212
213static const struct of_device_id rockchip_efuse_match[] = {
Finley Xiao02baff32016-09-02 10:14:27 +0100214 /* deprecated but kept around for dts binding compatibility */
215 {
216 .compatible = "rockchip,rockchip-efuse",
217 .data = (void *)&rockchip_rk3288_efuse_read,
218 },
219 {
220 .compatible = "rockchip,rk3066a-efuse",
221 .data = (void *)&rockchip_rk3288_efuse_read,
222 },
223 {
224 .compatible = "rockchip,rk3188-efuse",
225 .data = (void *)&rockchip_rk3288_efuse_read,
226 },
227 {
Frank Wangd6e4bd12017-07-14 16:38:43 +0800228 .compatible = "rockchip,rk3228-efuse",
Finley Xiao820de1f2017-06-09 10:59:10 +0100229 .data = (void *)&rockchip_rk3288_efuse_read,
230 },
231 {
Finley Xiao02baff32016-09-02 10:14:27 +0100232 .compatible = "rockchip,rk3288-efuse",
233 .data = (void *)&rockchip_rk3288_efuse_read,
234 },
235 {
Romain Perier7a15cf22017-10-09 15:26:38 +0200236 .compatible = "rockchip,rk3368-efuse",
237 .data = (void *)&rockchip_rk3288_efuse_read,
238 },
239 {
Finley Xiao9a479b02017-12-15 14:06:09 +0000240 .compatible = "rockchip,rk3328-efuse",
241 .data = (void *)&rockchip_rk3328_efuse_read,
242 },
243 {
Finley Xiao02baff32016-09-02 10:14:27 +0100244 .compatible = "rockchip,rk3399-efuse",
245 .data = (void *)&rockchip_rk3399_efuse_read,
246 },
ZhengShunQian03a69562015-09-30 13:56:44 +0100247 { /* sentinel */},
248};
249MODULE_DEVICE_TABLE(of, rockchip_efuse_match);
250
kbuild test robot7e532f72015-09-30 21:46:06 +0800251static int rockchip_efuse_probe(struct platform_device *pdev)
ZhengShunQian03a69562015-09-30 13:56:44 +0100252{
ZhengShunQian03a69562015-09-30 13:56:44 +0100253 struct resource *res;
254 struct nvmem_device *nvmem;
Caesar Wangc37ff3f2015-12-14 09:43:39 +0000255 struct rockchip_efuse_chip *efuse;
Andrey Smirnov7b4e76c2018-03-09 14:47:11 +0000256 const void *data;
Finley Xiao02baff32016-09-02 10:14:27 +0100257 struct device *dev = &pdev->dev;
258
Andrey Smirnov7b4e76c2018-03-09 14:47:11 +0000259 data = of_device_get_match_data(dev);
260 if (!data) {
Finley Xiao02baff32016-09-02 10:14:27 +0100261 dev_err(dev, "failed to get match data\n");
262 return -EINVAL;
263 }
Caesar Wangc37ff3f2015-12-14 09:43:39 +0000264
Andrey Smirnove84d1f92018-03-09 14:47:13 +0000265 efuse = devm_kzalloc(dev, sizeof(struct rockchip_efuse_chip),
Caesar Wangc37ff3f2015-12-14 09:43:39 +0000266 GFP_KERNEL);
267 if (!efuse)
268 return -ENOMEM;
ZhengShunQian03a69562015-09-30 13:56:44 +0100269
270 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Andrey Smirnove84d1f92018-03-09 14:47:13 +0000271 efuse->base = devm_ioremap_resource(dev, res);
Caesar Wangc37ff3f2015-12-14 09:43:39 +0000272 if (IS_ERR(efuse->base))
273 return PTR_ERR(efuse->base);
ZhengShunQian03a69562015-09-30 13:56:44 +0100274
Andrey Smirnove84d1f92018-03-09 14:47:13 +0000275 efuse->clk = devm_clk_get(dev, "pclk_efuse");
Caesar Wangc37ff3f2015-12-14 09:43:39 +0000276 if (IS_ERR(efuse->clk))
277 return PTR_ERR(efuse->clk);
ZhengShunQian03a69562015-09-30 13:56:44 +0100278
Andrey Smirnove84d1f92018-03-09 14:47:13 +0000279 efuse->dev = dev;
Finley Xiao32277722017-12-15 14:06:08 +0000280 if (of_property_read_u32(dev->of_node, "rockchip,efuse-size",
281 &econfig.size))
282 econfig.size = resource_size(res);
Andrey Smirnov7b4e76c2018-03-09 14:47:11 +0000283 econfig.reg_read = data;
Srinivas Kandagatlacc907552016-04-24 20:28:11 +0100284 econfig.priv = efuse;
Caesar Wangc37ff3f2015-12-14 09:43:39 +0000285 econfig.dev = efuse->dev;
Andrey Smirnovf4bec712018-03-09 14:47:02 +0000286 nvmem = devm_nvmem_register(dev, &econfig);
ZhengShunQian03a69562015-09-30 13:56:44 +0100287
Andrey Smirnovf4bec712018-03-09 14:47:02 +0000288 return PTR_ERR_OR_ZERO(nvmem);
ZhengShunQian03a69562015-09-30 13:56:44 +0100289}
290
291static struct platform_driver rockchip_efuse_driver = {
292 .probe = rockchip_efuse_probe,
ZhengShunQian03a69562015-09-30 13:56:44 +0100293 .driver = {
294 .name = "rockchip-efuse",
295 .of_match_table = rockchip_efuse_match,
296 },
297};
298
299module_platform_driver(rockchip_efuse_driver);
300MODULE_DESCRIPTION("rockchip_efuse driver");
301MODULE_LICENSE("GPL v2");