Skip to content

Commit 406346d

Browse files
XBurstherbertx
authored andcommitted
hwrng: ingenic - Add hardware TRNG for Ingenic X1830
Add X1830 SoC digital true random number generator driver. Tested-by: 周正 (Zhou Zheng) <sernia.zhou@foxmail.com> Co-developed-by: 漆鹏振 (Qi Pengzhen) <aric.pzqi@ingenic.com> Signed-off-by: 漆鹏振 (Qi Pengzhen) <aric.pzqi@ingenic.com> Signed-off-by: 周琰杰 (Zhou Yanjie) <zhouyanjie@wanyeetech.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 83ef53f commit 406346d

3 files changed

Lines changed: 176 additions & 0 deletions

File tree

‎drivers/char/hw_random/Kconfig‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,20 @@ config HW_RANDOM_INGENIC_RNG
282282

283283
If unsure, say Y.
284284

285+
config HW_RANDOM_INGENIC_TRNG
286+
tristate "Ingenic True Random Number Generator support"
287+
depends on HW_RANDOM
288+
depends on MACH_X1830
289+
default HW_RANDOM
290+
help
291+
This driver provides kernel-side support for the True Random Number Generator
292+
hardware found in ingenic X1830 SoC. YSH & ATIL CU1830-Neo uses X1830 SoC.
293+
294+
To compile this driver as a module, choose M here: the
295+
module will be called ingenic-trng.
296+
297+
If unsure, say Y.
298+
285299
config HW_RANDOM_NOMADIK
286300
tristate "ST-Ericsson Nomadik Random Number Generator support"
287301
depends on ARCH_NOMADIK

‎drivers/char/hw_random/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ obj-$(CONFIG_HW_RANDOM_TX4939) += tx4939-rng.o
2424
obj-$(CONFIG_HW_RANDOM_MXC_RNGA) += mxc-rnga.o
2525
obj-$(CONFIG_HW_RANDOM_IMX_RNGC) += imx-rngc.o
2626
obj-$(CONFIG_HW_RANDOM_INGENIC_RNG) += ingenic-rng.o
27+
obj-$(CONFIG_HW_RANDOM_INGENIC_TRNG) += ingenic-trng.o
2728
obj-$(CONFIG_HW_RANDOM_OCTEON) += octeon-rng.o
2829
obj-$(CONFIG_HW_RANDOM_NOMADIK) += nomadik-rng.o
2930
obj-$(CONFIG_HW_RANDOM_PSERIES) += pseries-rng.o
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Ingenic True Random Number Generator driver
4+
* Copyright (c) 2019 漆鹏振 (Qi Pengzhen) <aric.pzqi@ingenic.com>
5+
* Copyright (c) 2020 周琰杰 (Zhou Yanjie) <zhouyanjie@wanyeetech.com>
6+
*/
7+
8+
#include <linux/clk.h>
9+
#include <linux/err.h>
10+
#include <linux/kernel.h>
11+
#include <linux/hw_random.h>
12+
#include <linux/io.h>
13+
#include <linux/iopoll.h>
14+
#include <linux/module.h>
15+
#include <linux/of_device.h>
16+
#include <linux/platform_device.h>
17+
#include <linux/slab.h>
18+
19+
/* DTRNG register offsets */
20+
#define TRNG_REG_CFG_OFFSET 0x00
21+
#define TRNG_REG_RANDOMNUM_OFFSET 0x04
22+
#define TRNG_REG_STATUS_OFFSET 0x08
23+
24+
/* bits within the CFG register */
25+
#define CFG_RDY_CLR BIT(12)
26+
#define CFG_INT_MASK BIT(11)
27+
#define CFG_GEN_EN BIT(0)
28+
29+
/* bits within the STATUS register */
30+
#define STATUS_RANDOM_RDY BIT(0)
31+
32+
struct ingenic_trng {
33+
void __iomem *base;
34+
struct clk *clk;
35+
struct hwrng rng;
36+
};
37+
38+
static int ingenic_trng_init(struct hwrng *rng)
39+
{
40+
struct ingenic_trng *trng = container_of(rng, struct ingenic_trng, rng);
41+
unsigned int ctrl;
42+
43+
ctrl = readl(trng->base + TRNG_REG_CFG_OFFSET);
44+
ctrl |= CFG_GEN_EN;
45+
writel(ctrl, trng->base + TRNG_REG_CFG_OFFSET);
46+
47+
return 0;
48+
}
49+
50+
static void ingenic_trng_cleanup(struct hwrng *rng)
51+
{
52+
struct ingenic_trng *trng = container_of(rng, struct ingenic_trng, rng);
53+
unsigned int ctrl;
54+
55+
ctrl = readl(trng->base + TRNG_REG_CFG_OFFSET);
56+
ctrl &= ~CFG_GEN_EN;
57+
writel(ctrl, trng->base + TRNG_REG_CFG_OFFSET);
58+
}
59+
60+
static int ingenic_trng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
61+
{
62+
struct ingenic_trng *trng = container_of(rng, struct ingenic_trng, rng);
63+
u32 *data = buf;
64+
u32 status;
65+
int ret;
66+
67+
ret = readl_poll_timeout(trng->base + TRNG_REG_STATUS_OFFSET, status,
68+
status & STATUS_RANDOM_RDY, 10, 1000);
69+
if (ret == -ETIMEDOUT) {
70+
pr_err("%s: Wait for DTRNG data ready timeout\n", __func__);
71+
return ret;
72+
}
73+
74+
*data = readl(trng->base + TRNG_REG_RANDOMNUM_OFFSET);
75+
76+
return 4;
77+
}
78+
79+
static int ingenic_trng_probe(struct platform_device *pdev)
80+
{
81+
struct ingenic_trng *trng;
82+
int ret;
83+
84+
trng = devm_kzalloc(&pdev->dev, sizeof(*trng), GFP_KERNEL);
85+
if (!trng)
86+
return -ENOMEM;
87+
88+
trng->base = devm_platform_ioremap_resource(pdev, 0);
89+
if (IS_ERR(trng->base)) {
90+
pr_err("%s: Failed to map DTRNG registers\n", __func__);
91+
ret = PTR_ERR(trng->base);
92+
return PTR_ERR(trng->base);
93+
}
94+
95+
trng->clk = devm_clk_get(&pdev->dev, NULL);
96+
if (IS_ERR(trng->clk)) {
97+
ret = PTR_ERR(trng->clk);
98+
pr_crit("%s: Cannot get DTRNG clock\n", __func__);
99+
return PTR_ERR(trng->clk);
100+
}
101+
102+
ret = clk_prepare_enable(trng->clk);
103+
if (ret) {
104+
pr_crit("%s: Unable to enable DTRNG clock\n", __func__);
105+
return ret;
106+
}
107+
108+
trng->rng.name = pdev->name;
109+
trng->rng.init = ingenic_trng_init;
110+
trng->rng.cleanup = ingenic_trng_cleanup;
111+
trng->rng.read = ingenic_trng_read;
112+
113+
ret = hwrng_register(&trng->rng);
114+
if (ret) {
115+
dev_err(&pdev->dev, "Failed to register hwrng\n");
116+
return ret;
117+
}
118+
119+
platform_set_drvdata(pdev, trng);
120+
121+
dev_info(&pdev->dev, "Ingenic DTRNG driver registered\n");
122+
return 0;
123+
}
124+
125+
static int ingenic_trng_remove(struct platform_device *pdev)
126+
{
127+
struct ingenic_trng *trng = platform_get_drvdata(pdev);
128+
unsigned int ctrl;
129+
130+
hwrng_unregister(&trng->rng);
131+
132+
ctrl = readl(trng->base + TRNG_REG_CFG_OFFSET);
133+
ctrl &= ~CFG_GEN_EN;
134+
writel(ctrl, trng->base + TRNG_REG_CFG_OFFSET);
135+
136+
clk_disable_unprepare(trng->clk);
137+
138+
return 0;
139+
}
140+
141+
static const struct of_device_id ingenic_trng_of_match[] = {
142+
{ .compatible = "ingenic,x1830-dtrng" },
143+
{ /* sentinel */ }
144+
};
145+
MODULE_DEVICE_TABLE(of, ingenic_trng_of_match);
146+
147+
static struct platform_driver ingenic_trng_driver = {
148+
.probe = ingenic_trng_probe,
149+
.remove = ingenic_trng_remove,
150+
.driver = {
151+
.name = "ingenic-trng",
152+
.of_match_table = ingenic_trng_of_match,
153+
},
154+
};
155+
156+
module_platform_driver(ingenic_trng_driver);
157+
158+
MODULE_LICENSE("GPL");
159+
MODULE_AUTHOR("漆鹏振 (Qi Pengzhen) <aric.pzqi@ingenic.com>");
160+
MODULE_AUTHOR("周琰杰 (Zhou Yanjie) <zhouyanjie@wanyeetech.com>");
161+
MODULE_DESCRIPTION("Ingenic True Random Number Generator driver");

0 commit comments

Comments
 (0)