1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Ingenic Random Number Generator driver
4  * Copyright (c) 2017 PrasannaKumar Muralidharan <prasannatsmkumar@gmail.com>
5  * Copyright (c) 2020 周琰杰 (Zhou Yanjie) <zhouyanjie@wanyeetech.com>
6  */
7 
8 #include <linux/err.h>
9 #include <linux/kernel.h>
10 #include <linux/hw_random.h>
11 #include <linux/io.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>
17 
18 /* RNG register offsets */
19 #define RNG_REG_ERNG_OFFSET		0x0
20 #define RNG_REG_RNG_OFFSET		0x4
21 
22 /* bits within the ERND register */
23 #define ERNG_READY				BIT(31)
24 #define ERNG_ENABLE				BIT(0)
25 
26 enum ingenic_rng_version {
27 	ID_JZ4780,
28 	ID_X1000,
29 };
30 
31 /* Device associated memory */
32 struct ingenic_rng {
33 	enum ingenic_rng_version version;
34 
35 	void __iomem *base;
36 	struct hwrng rng;
37 };
38 
39 static int ingenic_rng_init(struct hwrng *rng)
40 {
41 	struct ingenic_rng *priv = container_of(rng, struct ingenic_rng, rng);
42 
43 	writel(ERNG_ENABLE, priv->base + RNG_REG_ERNG_OFFSET);
44 
45 	return 0;
46 }
47 
48 static void ingenic_rng_cleanup(struct hwrng *rng)
49 {
50 	struct ingenic_rng *priv = container_of(rng, struct ingenic_rng, rng);
51 
52 	writel(0, priv->base + RNG_REG_ERNG_OFFSET);
53 }
54 
55 static int ingenic_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
56 {
57 	struct ingenic_rng *priv = container_of(rng, struct ingenic_rng, rng);
58 	u32 *data = buf;
59 	u32 status;
60 	int ret;
61 
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__);
67 			return ret;
68 		}
69 	} else {
70 		/*
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.
74 		 */
75 		udelay(20);
76 	}
77 
78 	*data = readl(priv->base + RNG_REG_RNG_OFFSET);
79 
80 	return 4;
81 }
82 
83 static int ingenic_rng_probe(struct platform_device *pdev)
84 {
85 	struct ingenic_rng *priv;
86 	int ret;
87 
88 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
89 	if (!priv)
90 		return -ENOMEM;
91 
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);
96 	}
97 
98 	priv->version = (enum ingenic_rng_version)(uintptr_t)of_device_get_match_data(&pdev->dev);
99 
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;
104 
105 	ret = hwrng_register(&priv->rng);
106 	if (ret) {
107 		dev_err(&pdev->dev, "Failed to register hwrng\n");
108 		return ret;
109 	}
110 
111 	platform_set_drvdata(pdev, priv);
112 
113 	dev_info(&pdev->dev, "Ingenic RNG driver registered\n");
114 	return 0;
115 }
116 
117 static int ingenic_rng_remove(struct platform_device *pdev)
118 {
119 	struct ingenic_rng *priv = platform_get_drvdata(pdev);
120 
121 	hwrng_unregister(&priv->rng);
122 
123 	writel(0, priv->base + RNG_REG_ERNG_OFFSET);
124 
125 	return 0;
126 }
127 
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 },
131 	{ /* sentinel */ }
132 };
133 MODULE_DEVICE_TABLE(of, ingenic_rng_of_match);
134 
135 static struct platform_driver ingenic_rng_driver = {
136 	.probe		= ingenic_rng_probe,
137 	.remove		= ingenic_rng_remove,
138 	.driver		= {
139 		.name	= "ingenic-rng",
140 		.of_match_table = ingenic_rng_of_match,
141 	},
142 };
143 
144 module_platform_driver(ingenic_rng_driver);
145 
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");
150