blob: 146de9489339b9534780b3abf84f0315c6f4671c [file] [log] [blame]
ZhengShunQian03a69562015-09-30 13:56:44 +01001/*
2 * Rockchip eFuse Driver
3 *
4 * Copyright (c) 2015 Rockchip Electronics Co. Ltd.
5 * Author: Caesar Wang <wxt@rock-chips.com>
6 *
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.
10 *
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
14 * more details.
15 */
16
Caesar Wangc37ff3f2015-12-14 09:43:39 +000017#include <linux/clk.h>
18#include <linux/delay.h>
ZhengShunQian03a69562015-09-30 13:56:44 +010019#include <linux/device.h>
20#include <linux/io.h>
21#include <linux/module.h>
Caesar Wangc37ff3f2015-12-14 09:43:39 +000022#include <linux/nvmem-provider.h>
23#include <linux/slab.h>
ZhengShunQian03a69562015-09-30 13:56:44 +010024#include <linux/of.h>
Finley Xiao02baff32016-09-02 10:14:27 +010025#include <linux/of_platform.h>
Caesar Wangc37ff3f2015-12-14 09:43:39 +000026#include <linux/platform_device.h>
ZhengShunQian03a69562015-09-30 13:56:44 +010027
Finley Xiao02baff32016-09-02 10:14:27 +010028#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)
ZhengShunQian03a69562015-09-30 13:56:44 +010034
Finley Xiao9a479b02017-12-15 14:06:09 +000035#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)
42
Finley Xiao02baff32016-09-02 10:14:27 +010043#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)
53
54#define REG_EFUSE_CTRL 0x0000
55#define REG_EFUSE_DOUT 0x0004
ZhengShunQian03a69562015-09-30 13:56:44 +010056
Caesar Wangc37ff3f2015-12-14 09:43:39 +000057struct rockchip_efuse_chip {
ZhengShunQian03a69562015-09-30 13:56:44 +010058 struct device *dev;
59 void __iomem *base;
Caesar Wangc37ff3f2015-12-14 09:43:39 +000060 struct clk *clk;
ZhengShunQian03a69562015-09-30 13:56:44 +010061};
62
Finley Xiao02baff32016-09-02 10:14:27 +010063static int rockchip_rk3288_efuse_read(void *context, unsigned int offset,
64 void *val, size_t bytes)
ZhengShunQian03a69562015-09-30 13:56:44 +010065{
Caesar Wangc37ff3f2015-12-14 09:43:39 +000066 struct rockchip_efuse_chip *efuse = context;
ZhengShunQian03a69562015-09-30 13:56:44 +010067 u8 *buf = val;
68 int ret;
69
Caesar Wangc37ff3f2015-12-14 09:43:39 +000070 ret = clk_prepare_enable(efuse->clk);
ZhengShunQian03a69562015-09-30 13:56:44 +010071 if (ret < 0) {
Caesar Wangc37ff3f2015-12-14 09:43:39 +000072 dev_err(efuse->dev, "failed to prepare/enable efuse clk\n");
ZhengShunQian03a69562015-09-30 13:56:44 +010073 return ret;
74 }
75
Finley Xiao02baff32016-09-02 10:14:27 +010076 writel(RK3288_LOAD | RK3288_PGENB, efuse->base + REG_EFUSE_CTRL);
ZhengShunQian03a69562015-09-30 13:56:44 +010077 udelay(1);
Srinivas Kandagatlacc907552016-04-24 20:28:11 +010078 while (bytes--) {
Caesar Wangc37ff3f2015-12-14 09:43:39 +000079 writel(readl(efuse->base + REG_EFUSE_CTRL) &
Finley Xiao02baff32016-09-02 10:14:27 +010080 (~(RK3288_A_MASK << RK3288_A_SHIFT)),
Caesar Wangc37ff3f2015-12-14 09:43:39 +000081 efuse->base + REG_EFUSE_CTRL);
82 writel(readl(efuse->base + REG_EFUSE_CTRL) |
Finley Xiao02baff32016-09-02 10:14:27 +010083 ((offset++ & RK3288_A_MASK) << RK3288_A_SHIFT),
Caesar Wangc37ff3f2015-12-14 09:43:39 +000084 efuse->base + REG_EFUSE_CTRL);
ZhengShunQian03a69562015-09-30 13:56:44 +010085 udelay(1);
Caesar Wangc37ff3f2015-12-14 09:43:39 +000086 writel(readl(efuse->base + REG_EFUSE_CTRL) |
Finley Xiao02baff32016-09-02 10:14:27 +010087 RK3288_STROBE, efuse->base + REG_EFUSE_CTRL);
ZhengShunQian03a69562015-09-30 13:56:44 +010088 udelay(1);
Caesar Wangc37ff3f2015-12-14 09:43:39 +000089 *buf++ = readb(efuse->base + REG_EFUSE_DOUT);
90 writel(readl(efuse->base + REG_EFUSE_CTRL) &
Finley Xiao02baff32016-09-02 10:14:27 +010091 (~RK3288_STROBE), efuse->base + REG_EFUSE_CTRL);
ZhengShunQian03a69562015-09-30 13:56:44 +010092 udelay(1);
ZhengShunQian03a69562015-09-30 13:56:44 +010093 }
94
95 /* Switch to standby mode */
Finley Xiao02baff32016-09-02 10:14:27 +010096 writel(RK3288_PGENB | RK3288_CSB, efuse->base + REG_EFUSE_CTRL);
97
98 clk_disable_unprepare(efuse->clk);
99
100 return 0;
101}
102
Finley Xiao9a479b02017-12-15 14:06:09 +0000103static int rockchip_rk3328_efuse_read(void *context, unsigned int offset,
104 void *val, size_t bytes)
105{
106 struct rockchip_efuse_chip *efuse = context;
107 unsigned int addr_start, addr_end, addr_offset, addr_len;
108 u32 out_value, status;
109 u8 *buf;
110 int ret, i = 0;
111
112 ret = clk_prepare_enable(efuse->clk);
113 if (ret < 0) {
114 dev_err(efuse->dev, "failed to prepare/enable efuse clk\n");
115 return ret;
116 }
117
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;
124
Kees Cook6396bb22018-06-12 14:03:40 -0700125 buf = kzalloc(array3_size(addr_len, RK3399_NBYTES, sizeof(*buf)),
126 GFP_KERNEL);
Finley Xiao9a479b02017-12-15 14:06:09 +0000127 if (!buf) {
128 ret = -ENOMEM;
129 goto nomem;
130 }
131
132 while (addr_len--) {
133 writel(RK3328_AUTO_RD | RK3328_AUTO_ENB |
134 ((addr_start++ & RK3399_A_MASK) << RK3399_A_SHIFT),
135 efuse->base + RK3328_AUTO_CTRL);
136 udelay(4);
137 status = readl(efuse->base + RK3328_INT_STATUS);
138 if (!(status & RK3328_INT_FINISH)) {
139 ret = -EIO;
140 goto err;
141 }
142 out_value = readl(efuse->base + RK3328_DOUT);
143 writel(RK3328_INT_FINISH, efuse->base + RK3328_INT_STATUS);
144
145 memcpy(&buf[i], &out_value, RK3399_NBYTES);
146 i += RK3399_NBYTES;
147 }
148
149 memcpy(val, buf + addr_offset, bytes);
150err:
151 kfree(buf);
152nomem:
153 clk_disable_unprepare(efuse->clk);
154
155 return ret;
156}
157
Finley Xiao02baff32016-09-02 10:14:27 +0100158static int rockchip_rk3399_efuse_read(void *context, unsigned int offset,
159 void *val, size_t bytes)
160{
161 struct rockchip_efuse_chip *efuse = context;
162 unsigned int addr_start, addr_end, addr_offset, addr_len;
163 u32 out_value;
164 u8 *buf;
165 int ret, i = 0;
166
167 ret = clk_prepare_enable(efuse->clk);
168 if (ret < 0) {
169 dev_err(efuse->dev, "failed to prepare/enable efuse clk\n");
170 return ret;
171 }
172
173 addr_start = rounddown(offset, RK3399_NBYTES) / RK3399_NBYTES;
174 addr_end = roundup(offset + bytes, RK3399_NBYTES) / RK3399_NBYTES;
175 addr_offset = offset % RK3399_NBYTES;
176 addr_len = addr_end - addr_start;
177
Kees Cook6396bb22018-06-12 14:03:40 -0700178 buf = kzalloc(array3_size(addr_len, RK3399_NBYTES, sizeof(*buf)),
179 GFP_KERNEL);
Finley Xiao02baff32016-09-02 10:14:27 +0100180 if (!buf) {
181 clk_disable_unprepare(efuse->clk);
182 return -ENOMEM;
183 }
184
185 writel(RK3399_LOAD | RK3399_PGENB | RK3399_STROBSFTSEL | RK3399_RSB,
186 efuse->base + REG_EFUSE_CTRL);
187 udelay(1);
188 while (addr_len--) {
189 writel(readl(efuse->base + REG_EFUSE_CTRL) | RK3399_STROBE |
190 ((addr_start++ & RK3399_A_MASK) << RK3399_A_SHIFT),
191 efuse->base + REG_EFUSE_CTRL);
192 udelay(1);
193 out_value = readl(efuse->base + REG_EFUSE_DOUT);
194 writel(readl(efuse->base + REG_EFUSE_CTRL) & (~RK3399_STROBE),
195 efuse->base + REG_EFUSE_CTRL);
196 udelay(1);
197
198 memcpy(&buf[i], &out_value, RK3399_NBYTES);
199 i += RK3399_NBYTES;
200 }
201
202 /* Switch to standby mode */
203 writel(RK3399_PD | RK3399_CSB, efuse->base + REG_EFUSE_CTRL);
204
205 memcpy(val, buf + addr_offset, bytes);
206
207 kfree(buf);
ZhengShunQian03a69562015-09-30 13:56:44 +0100208
Caesar Wangc37ff3f2015-12-14 09:43:39 +0000209 clk_disable_unprepare(efuse->clk);
ZhengShunQian03a69562015-09-30 13:56:44 +0100210
211 return 0;
212}
213
ZhengShunQian03a69562015-09-30 13:56:44 +0100214static struct nvmem_config econfig = {
215 .name = "rockchip-efuse",
Srinivas Kandagatlacc907552016-04-24 20:28:11 +0100216 .stride = 1,
217 .word_size = 1,
ZhengShunQian03a69562015-09-30 13:56:44 +0100218 .read_only = true,
219};
220
221static const struct of_device_id rockchip_efuse_match[] = {
Finley Xiao02baff32016-09-02 10:14:27 +0100222 /* deprecated but kept around for dts binding compatibility */
223 {
224 .compatible = "rockchip,rockchip-efuse",
225 .data = (void *)&rockchip_rk3288_efuse_read,
226 },
227 {
228 .compatible = "rockchip,rk3066a-efuse",
229 .data = (void *)&rockchip_rk3288_efuse_read,
230 },
231 {
232 .compatible = "rockchip,rk3188-efuse",
233 .data = (void *)&rockchip_rk3288_efuse_read,
234 },
235 {
Frank Wangd6e4bd12017-07-14 16:38:43 +0800236 .compatible = "rockchip,rk3228-efuse",
Finley Xiao820de1f2017-06-09 10:59:10 +0100237 .data = (void *)&rockchip_rk3288_efuse_read,
238 },
239 {
Finley Xiao02baff32016-09-02 10:14:27 +0100240 .compatible = "rockchip,rk3288-efuse",
241 .data = (void *)&rockchip_rk3288_efuse_read,
242 },
243 {
Romain Perier7a15cf22017-10-09 15:26:38 +0200244 .compatible = "rockchip,rk3368-efuse",
245 .data = (void *)&rockchip_rk3288_efuse_read,
246 },
247 {
Finley Xiao9a479b02017-12-15 14:06:09 +0000248 .compatible = "rockchip,rk3328-efuse",
249 .data = (void *)&rockchip_rk3328_efuse_read,
250 },
251 {
Finley Xiao02baff32016-09-02 10:14:27 +0100252 .compatible = "rockchip,rk3399-efuse",
253 .data = (void *)&rockchip_rk3399_efuse_read,
254 },
ZhengShunQian03a69562015-09-30 13:56:44 +0100255 { /* sentinel */},
256};
257MODULE_DEVICE_TABLE(of, rockchip_efuse_match);
258
kbuild test robot7e532f72015-09-30 21:46:06 +0800259static int rockchip_efuse_probe(struct platform_device *pdev)
ZhengShunQian03a69562015-09-30 13:56:44 +0100260{
ZhengShunQian03a69562015-09-30 13:56:44 +0100261 struct resource *res;
262 struct nvmem_device *nvmem;
Caesar Wangc37ff3f2015-12-14 09:43:39 +0000263 struct rockchip_efuse_chip *efuse;
Andrey Smirnov7b4e76c2018-03-09 14:47:11 +0000264 const void *data;
Finley Xiao02baff32016-09-02 10:14:27 +0100265 struct device *dev = &pdev->dev;
266
Andrey Smirnov7b4e76c2018-03-09 14:47:11 +0000267 data = of_device_get_match_data(dev);
268 if (!data) {
Finley Xiao02baff32016-09-02 10:14:27 +0100269 dev_err(dev, "failed to get match data\n");
270 return -EINVAL;
271 }
Caesar Wangc37ff3f2015-12-14 09:43:39 +0000272
Andrey Smirnove84d1f92018-03-09 14:47:13 +0000273 efuse = devm_kzalloc(dev, sizeof(struct rockchip_efuse_chip),
Caesar Wangc37ff3f2015-12-14 09:43:39 +0000274 GFP_KERNEL);
275 if (!efuse)
276 return -ENOMEM;
ZhengShunQian03a69562015-09-30 13:56:44 +0100277
278 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Andrey Smirnove84d1f92018-03-09 14:47:13 +0000279 efuse->base = devm_ioremap_resource(dev, res);
Caesar Wangc37ff3f2015-12-14 09:43:39 +0000280 if (IS_ERR(efuse->base))
281 return PTR_ERR(efuse->base);
ZhengShunQian03a69562015-09-30 13:56:44 +0100282
Andrey Smirnove84d1f92018-03-09 14:47:13 +0000283 efuse->clk = devm_clk_get(dev, "pclk_efuse");
Caesar Wangc37ff3f2015-12-14 09:43:39 +0000284 if (IS_ERR(efuse->clk))
285 return PTR_ERR(efuse->clk);
ZhengShunQian03a69562015-09-30 13:56:44 +0100286
Andrey Smirnove84d1f92018-03-09 14:47:13 +0000287 efuse->dev = dev;
Finley Xiao32277722017-12-15 14:06:08 +0000288 if (of_property_read_u32(dev->of_node, "rockchip,efuse-size",
289 &econfig.size))
290 econfig.size = resource_size(res);
Andrey Smirnov7b4e76c2018-03-09 14:47:11 +0000291 econfig.reg_read = data;
Srinivas Kandagatlacc907552016-04-24 20:28:11 +0100292 econfig.priv = efuse;
Caesar Wangc37ff3f2015-12-14 09:43:39 +0000293 econfig.dev = efuse->dev;
Andrey Smirnovf4bec712018-03-09 14:47:02 +0000294 nvmem = devm_nvmem_register(dev, &econfig);
ZhengShunQian03a69562015-09-30 13:56:44 +0100295
Andrey Smirnovf4bec712018-03-09 14:47:02 +0000296 return PTR_ERR_OR_ZERO(nvmem);
ZhengShunQian03a69562015-09-30 13:56:44 +0100297}
298
299static struct platform_driver rockchip_efuse_driver = {
300 .probe = rockchip_efuse_probe,
ZhengShunQian03a69562015-09-30 13:56:44 +0100301 .driver = {
302 .name = "rockchip-efuse",
303 .of_match_table = rockchip_efuse_match,
304 },
305};
306
307module_platform_driver(rockchip_efuse_driver);
308MODULE_DESCRIPTION("rockchip_efuse driver");
309MODULE_LICENSE("GPL v2");