1 // SPDX-License-Identifier: GPL-2.0
3 * Ingenic Random Number Generator driver
4 * Copyright (c) 2017 PrasannaKumar Muralidharan <prasannatsmkumar@gmail.com>
5 * Copyright (c) 2020 周琰杰 (Zhou Yanjie) <zhouyanjie@wanyeetech.com>
9 #include <linux/kernel.h>
10 #include <linux/hw_random.h>
12 #include <linux/iopoll.h>
13 #include <linux/module.h>
14 #include <linux/of_device.h>
15 #include <linux/platform_device.h>
16 #include <linux/slab.h>
18 /* RNG register offsets */
19 #define RNG_REG_ERNG_OFFSET 0x0
20 #define RNG_REG_RNG_OFFSET 0x4
22 /* bits within the ERND register */
23 #define ERNG_READY BIT(31)
24 #define ERNG_ENABLE BIT(0)
26 enum ingenic_rng_version
{
31 /* Device associated memory */
33 enum ingenic_rng_version version
;
39 static int ingenic_rng_init(struct hwrng
*rng
)
41 struct ingenic_rng
*priv
= container_of(rng
, struct ingenic_rng
, rng
);
43 writel(ERNG_ENABLE
, priv
->base
+ RNG_REG_ERNG_OFFSET
);
48 static void ingenic_rng_cleanup(struct hwrng
*rng
)
50 struct ingenic_rng
*priv
= container_of(rng
, struct ingenic_rng
, rng
);
52 writel(0, priv
->base
+ RNG_REG_ERNG_OFFSET
);
55 static int ingenic_rng_read(struct hwrng
*rng
, void *buf
, size_t max
, bool wait
)
57 struct ingenic_rng
*priv
= container_of(rng
, struct ingenic_rng
, rng
);
62 if (priv
->version
>= ID_X1000
) {
63 ret
= readl_poll_timeout(priv
->base
+ RNG_REG_ERNG_OFFSET
, status
,
64 status
& ERNG_READY
, 10, 1000);
65 if (ret
== -ETIMEDOUT
) {
66 pr_err("%s: Wait for RNG data ready timeout\n", __func__
);
71 * A delay is required so that the current RNG data is not bit shifted
72 * version of previous RNG data which could happen if random data is
73 * read continuously from this device.
78 *data
= readl(priv
->base
+ RNG_REG_RNG_OFFSET
);
83 static int ingenic_rng_probe(struct platform_device
*pdev
)
85 struct ingenic_rng
*priv
;
88 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
92 priv
->base
= devm_platform_ioremap_resource(pdev
, 0);
93 if (IS_ERR(priv
->base
)) {
94 pr_err("%s: Failed to map RNG registers\n", __func__
);
95 return PTR_ERR(priv
->base
);
98 priv
->version
= (enum ingenic_rng_version
)of_device_get_match_data(&pdev
->dev
);
100 priv
->rng
.name
= pdev
->name
;
101 priv
->rng
.init
= ingenic_rng_init
;
102 priv
->rng
.cleanup
= ingenic_rng_cleanup
;
103 priv
->rng
.read
= ingenic_rng_read
;
105 ret
= hwrng_register(&priv
->rng
);
107 dev_err(&pdev
->dev
, "Failed to register hwrng\n");
111 platform_set_drvdata(pdev
, priv
);
113 dev_info(&pdev
->dev
, "Ingenic RNG driver registered\n");
117 static int ingenic_rng_remove(struct platform_device
*pdev
)
119 struct ingenic_rng
*priv
= platform_get_drvdata(pdev
);
121 hwrng_unregister(&priv
->rng
);
123 writel(0, priv
->base
+ RNG_REG_ERNG_OFFSET
);
128 static const struct of_device_id ingenic_rng_of_match
[] = {
129 { .compatible
= "ingenic,jz4780-rng", .data
= (void *) ID_JZ4780
},
130 { .compatible
= "ingenic,x1000-rng", .data
= (void *) ID_X1000
},
133 MODULE_DEVICE_TABLE(of
, ingenic_rng_of_match
);
135 static struct platform_driver ingenic_rng_driver
= {
136 .probe
= ingenic_rng_probe
,
137 .remove
= ingenic_rng_remove
,
139 .name
= "ingenic-rng",
140 .of_match_table
= ingenic_rng_of_match
,
144 module_platform_driver(ingenic_rng_driver
);
146 MODULE_LICENSE("GPL");
147 MODULE_AUTHOR("PrasannaKumar Muralidharan <prasannatsmkumar@gmail.com>");
148 MODULE_AUTHOR("周琰杰 (Zhou Yanjie) <zhouyanjie@wanyeetech.com>");
149 MODULE_DESCRIPTION("Ingenic Random Number Generator driver");