blob: b0ded41eb865f56483c2fa80dbf7361832680b3c [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Alessandro Rubinibc85b252009-12-19 19:45:43 +08002/*
3 * Nomadik RNG support
4 * Copyright 2009 Alessandro Rubini
Alessandro Rubinibc85b252009-12-19 19:45:43 +08005 */
6
7#include <linux/kernel.h>
8#include <linux/module.h>
Alessandro Rubinibc85b252009-12-19 19:45:43 +08009#include <linux/device.h>
10#include <linux/amba/bus.h>
11#include <linux/hw_random.h>
12#include <linux/io.h>
Srinidhi Kasagar1944cc82010-05-19 06:49:13 +010013#include <linux/clk.h>
14#include <linux/err.h>
15
16static struct clk *rng_clk;
Alessandro Rubinibc85b252009-12-19 19:45:43 +080017
18static int nmk_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
19{
20 void __iomem *base = (void __iomem *)rng->priv;
21
22 /*
23 * The register is 32 bits and gives 16 random bits (low half).
24 * A subsequent read will delay the core for 400ns, so we just read
25 * once and accept the very unlikely very small delay, even if wait==0.
26 */
27 *(u16 *)data = __raw_readl(base + 8) & 0xffff;
28 return 2;
29}
30
31/* we have at most one RNG per machine, granted */
32static struct hwrng nmk_rng = {
33 .name = "nomadik",
34 .read = nmk_rng_read,
35};
36
Russell Kingaa25afa2011-02-19 15:55:00 +000037static int nmk_rng_probe(struct amba_device *dev, const struct amba_id *id)
Alessandro Rubinibc85b252009-12-19 19:45:43 +080038{
39 void __iomem *base;
40 int ret;
41
Jingoo Han1655c242014-02-27 14:06:31 +090042 rng_clk = devm_clk_get(&dev->dev, NULL);
Srinidhi Kasagar1944cc82010-05-19 06:49:13 +010043 if (IS_ERR(rng_clk)) {
44 dev_err(&dev->dev, "could not get rng clock\n");
45 ret = PTR_ERR(rng_clk);
46 return ret;
47 }
48
Linus Walleijbeca35d2013-06-03 02:02:09 +020049 clk_prepare_enable(rng_clk);
Srinidhi Kasagar1944cc82010-05-19 06:49:13 +010050
Alessandro Rubinibc85b252009-12-19 19:45:43 +080051 ret = amba_request_regions(dev, dev->dev.init_name);
52 if (ret)
Julia Lawall2725ae12011-06-08 21:00:55 +080053 goto out_clk;
Alessandro Rubinibc85b252009-12-19 19:45:43 +080054 ret = -ENOMEM;
Jingoo Han1655c242014-02-27 14:06:31 +090055 base = devm_ioremap(&dev->dev, dev->res.start,
56 resource_size(&dev->res));
Alessandro Rubinibc85b252009-12-19 19:45:43 +080057 if (!base)
58 goto out_release;
59 nmk_rng.priv = (unsigned long)base;
Chuhong Yuan3e752412019-07-25 16:01:55 +080060 ret = devm_hwrng_register(&dev->dev, &nmk_rng);
Alessandro Rubinibc85b252009-12-19 19:45:43 +080061 if (ret)
Jingoo Han1655c242014-02-27 14:06:31 +090062 goto out_release;
Alessandro Rubinibc85b252009-12-19 19:45:43 +080063 return 0;
64
Alessandro Rubinibc85b252009-12-19 19:45:43 +080065out_release:
66 amba_release_regions(dev);
Julia Lawall2725ae12011-06-08 21:00:55 +080067out_clk:
Srinidhi Kasagar1944cc82010-05-19 06:49:13 +010068 clk_disable(rng_clk);
Alessandro Rubinibc85b252009-12-19 19:45:43 +080069 return ret;
70}
71
72static int nmk_rng_remove(struct amba_device *dev)
73{
Alessandro Rubinibc85b252009-12-19 19:45:43 +080074 amba_release_regions(dev);
Srinidhi Kasagar1944cc82010-05-19 06:49:13 +010075 clk_disable(rng_clk);
Alessandro Rubinibc85b252009-12-19 19:45:43 +080076 return 0;
77}
78
Rikard Falkebornb2429732020-07-01 22:09:47 +020079static const struct amba_id nmk_rng_ids[] = {
Alessandro Rubinibc85b252009-12-19 19:45:43 +080080 {
81 .id = 0x000805e1,
82 .mask = 0x000fffff, /* top bits are rev and cfg: accept all */
83 },
84 {0, 0},
85};
86
Dave Martin4e3f3d52011-10-05 15:15:20 +010087MODULE_DEVICE_TABLE(amba, nmk_rng_ids);
88
Alessandro Rubinibc85b252009-12-19 19:45:43 +080089static struct amba_driver nmk_rng_driver = {
90 .drv = {
91 .owner = THIS_MODULE,
92 .name = "rng",
93 },
94 .probe = nmk_rng_probe,
95 .remove = nmk_rng_remove,
96 .id_table = nmk_rng_ids,
97};
98
viresh kumar9e5ed092012-03-15 10:40:38 +010099module_amba_driver(nmk_rng_driver);
Alessandro Rubinibc85b252009-12-19 19:45:43 +0800100
101MODULE_LICENSE("GPL");