xref: /linux/drivers/char/hw_random/nomadik-rng.c (revision 88872790923e2d80edf29a00b4e440f1473fa8f5)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Nomadik RNG support
4  *  Copyright 2009 Alessandro Rubini
5  */
6 
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/device.h>
10 #include <linux/amba/bus.h>
11 #include <linux/hw_random.h>
12 #include <linux/io.h>
13 #include <linux/clk.h>
14 #include <linux/err.h>
15 
16 static int nmk_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
17 {
18 	void __iomem *base = (void __iomem *)rng->priv;
19 
20 	/*
21 	 * The register is 32 bits and gives 16 random bits (low half).
22 	 * A subsequent read will delay the core for 400ns, so we just read
23 	 * once and accept the very unlikely very small delay, even if wait==0.
24 	 */
25 	*(u16 *)data = __raw_readl(base + 8) & 0xffff;
26 	return 2;
27 }
28 
29 /* we have at most one RNG per machine, granted */
30 static struct hwrng nmk_rng = {
31 	.name		= "nomadik",
32 	.read		= nmk_rng_read,
33 };
34 
35 static int nmk_rng_probe(struct amba_device *dev, const struct amba_id *id)
36 {
37 	struct clk *rng_clk;
38 	void __iomem *base;
39 	int ret;
40 
41 	rng_clk = devm_clk_get_enabled(&dev->dev, NULL);
42 	if (IS_ERR(rng_clk))
43 		return dev_err_probe(&dev->dev, PTR_ERR(rng_clk), "could not get rng clock\n");
44 
45 	ret = amba_request_regions(dev, dev->dev.init_name);
46 	if (ret)
47 		return ret;
48 	ret = -ENOMEM;
49 	base = devm_ioremap(&dev->dev, dev->res.start,
50 			    resource_size(&dev->res));
51 	if (!base)
52 		goto out_release;
53 	nmk_rng.priv = (unsigned long)base;
54 	ret = devm_hwrng_register(&dev->dev, &nmk_rng);
55 	if (ret)
56 		goto out_release;
57 	return 0;
58 
59 out_release:
60 	amba_release_regions(dev);
61 	return ret;
62 }
63 
64 static void nmk_rng_remove(struct amba_device *dev)
65 {
66 	amba_release_regions(dev);
67 }
68 
69 static const struct amba_id nmk_rng_ids[] = {
70 	{
71 		.id	= 0x000805e1,
72 		.mask	= 0x000fffff, /* top bits are rev and cfg: accept all */
73 	},
74 	{0, 0},
75 };
76 
77 MODULE_DEVICE_TABLE(amba, nmk_rng_ids);
78 
79 static struct amba_driver nmk_rng_driver = {
80 	.drv = {
81 		.owner = THIS_MODULE,
82 		.name = "rng",
83 		},
84 	.probe = nmk_rng_probe,
85 	.remove = nmk_rng_remove,
86 	.id_table = nmk_rng_ids,
87 };
88 
89 module_amba_driver(nmk_rng_driver);
90 
91 MODULE_LICENSE("GPL");
92