xref: /linux/drivers/char/hw_random/ingenic-trng.c (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
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/platform_device.h>
16 #include <linux/slab.h>
17 
18 /* DTRNG register offsets */
19 #define TRNG_REG_CFG_OFFSET			0x00
20 #define TRNG_REG_RANDOMNUM_OFFSET	0x04
21 #define TRNG_REG_STATUS_OFFSET		0x08
22 
23 /* bits within the CFG register */
24 #define CFG_GEN_EN					BIT(0)
25 
26 /* bits within the STATUS register */
27 #define STATUS_RANDOM_RDY			BIT(0)
28 
29 struct ingenic_trng {
30 	void __iomem *base;
31 	struct hwrng rng;
32 };
33 
34 static int ingenic_trng_init(struct hwrng *rng)
35 {
36 	struct ingenic_trng *trng = container_of(rng, struct ingenic_trng, rng);
37 	unsigned int ctrl;
38 
39 	ctrl = readl(trng->base + TRNG_REG_CFG_OFFSET);
40 	ctrl |= CFG_GEN_EN;
41 	writel(ctrl, trng->base + TRNG_REG_CFG_OFFSET);
42 
43 	return 0;
44 }
45 
46 static void ingenic_trng_cleanup(struct hwrng *rng)
47 {
48 	struct ingenic_trng *trng = container_of(rng, struct ingenic_trng, rng);
49 	unsigned int ctrl;
50 
51 	ctrl = readl(trng->base + TRNG_REG_CFG_OFFSET);
52 	ctrl &= ~CFG_GEN_EN;
53 	writel(ctrl, trng->base + TRNG_REG_CFG_OFFSET);
54 }
55 
56 static int ingenic_trng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
57 {
58 	struct ingenic_trng *trng = container_of(rng, struct ingenic_trng, rng);
59 	u32 *data = buf;
60 	u32 status;
61 	int ret;
62 
63 	ret = readl_poll_timeout(trng->base + TRNG_REG_STATUS_OFFSET, status,
64 				 status & STATUS_RANDOM_RDY, 10, 1000);
65 	if (ret == -ETIMEDOUT) {
66 		pr_err("%s: Wait for DTRNG data ready timeout\n", __func__);
67 		return ret;
68 	}
69 
70 	*data = readl(trng->base + TRNG_REG_RANDOMNUM_OFFSET);
71 
72 	return 4;
73 }
74 
75 static int ingenic_trng_probe(struct platform_device *pdev)
76 {
77 	struct ingenic_trng *trng;
78 	struct clk *clk;
79 	int ret;
80 
81 	trng = devm_kzalloc(&pdev->dev, sizeof(*trng), GFP_KERNEL);
82 	if (!trng)
83 		return -ENOMEM;
84 
85 	trng->base = devm_platform_ioremap_resource(pdev, 0);
86 	if (IS_ERR(trng->base))
87 		return dev_err_probe(&pdev->dev, PTR_ERR(trng->base),
88 				     "%s: Failed to map DTRNG registers\n", __func__);
89 
90 	clk = devm_clk_get_enabled(&pdev->dev, NULL);
91 	if (IS_ERR(clk))
92 		return dev_err_probe(&pdev->dev, PTR_ERR(clk),
93 				     "%s: Cannot get and enable DTRNG clock\n", __func__);
94 
95 	trng->rng.name = pdev->name;
96 	trng->rng.init = ingenic_trng_init;
97 	trng->rng.cleanup = ingenic_trng_cleanup;
98 	trng->rng.read = ingenic_trng_read;
99 
100 	ret = devm_hwrng_register(&pdev->dev, &trng->rng);
101 	if (ret)
102 		return dev_err_probe(&pdev->dev, ret, "Failed to register hwrng\n");
103 
104 	platform_set_drvdata(pdev, trng);
105 
106 	dev_info(&pdev->dev, "Ingenic DTRNG driver registered\n");
107 	return 0;
108 }
109 
110 static const struct of_device_id ingenic_trng_of_match[] = {
111 	{ .compatible = "ingenic,x1830-dtrng" },
112 	{ /* sentinel */ }
113 };
114 MODULE_DEVICE_TABLE(of, ingenic_trng_of_match);
115 
116 static struct platform_driver ingenic_trng_driver = {
117 	.probe		= ingenic_trng_probe,
118 	.driver		= {
119 		.name	= "ingenic-trng",
120 		.of_match_table = ingenic_trng_of_match,
121 	},
122 };
123 
124 module_platform_driver(ingenic_trng_driver);
125 
126 MODULE_LICENSE("GPL");
127 MODULE_AUTHOR("漆鹏振 (Qi Pengzhen) <aric.pzqi@ingenic.com>");
128 MODULE_AUTHOR("周琰杰 (Zhou Yanjie) <zhouyanjie@wanyeetech.com>");
129 MODULE_DESCRIPTION("Ingenic True Random Number Generator driver");
130