1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright 2009 Alessandro Rubini
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>
13 #include <linux/clk.h>
14 #include <linux/err.h>
16 static int nmk_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
18 void __iomem *base = (void __iomem *)rng->priv;
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.
25 *(u16 *)data = __raw_readl(base + 8) & 0xffff;
29 /* we have at most one RNG per machine, granted */
30 static struct hwrng nmk_rng = {
35 static int nmk_rng_probe(struct amba_device *dev, const struct amba_id *id)
41 rng_clk = devm_clk_get_enabled(&dev->dev, NULL);
42 if (IS_ERR(rng_clk)) {
43 dev_err(&dev->dev, "could not get rng clock\n");
44 ret = PTR_ERR(rng_clk);
48 ret = amba_request_regions(dev, dev->dev.init_name);
52 base = devm_ioremap(&dev->dev, dev->res.start,
53 resource_size(&dev->res));
56 nmk_rng.priv = (unsigned long)base;
57 ret = devm_hwrng_register(&dev->dev, &nmk_rng);
63 amba_release_regions(dev);
67 static void nmk_rng_remove(struct amba_device *dev)
69 amba_release_regions(dev);
72 static const struct amba_id nmk_rng_ids[] = {
75 .mask = 0x000fffff, /* top bits are rev and cfg: accept all */
80 MODULE_DEVICE_TABLE(amba, nmk_rng_ids);
82 static struct amba_driver nmk_rng_driver = {
87 .probe = nmk_rng_probe,
88 .remove = nmk_rng_remove,
89 .id_table = nmk_rng_ids,
92 module_amba_driver(nmk_rng_driver);
94 MODULE_LICENSE("GPL");