treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / char / hw_random / nomadik-rng.c
blob74ed29f42e4fe70eafa0f2e58df3922f1b26dc29
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Nomadik RNG support
4 * Copyright 2009 Alessandro Rubini
5 */
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>
16 static struct clk *rng_clk;
18 static int nmk_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
20 void __iomem *base = (void __iomem *)rng->priv;
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.
27 *(u16 *)data = __raw_readl(base + 8) & 0xffff;
28 return 2;
31 /* we have at most one RNG per machine, granted */
32 static struct hwrng nmk_rng = {
33 .name = "nomadik",
34 .read = nmk_rng_read,
37 static int nmk_rng_probe(struct amba_device *dev, const struct amba_id *id)
39 void __iomem *base;
40 int ret;
42 rng_clk = devm_clk_get(&dev->dev, NULL);
43 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;
49 clk_prepare_enable(rng_clk);
51 ret = amba_request_regions(dev, dev->dev.init_name);
52 if (ret)
53 goto out_clk;
54 ret = -ENOMEM;
55 base = devm_ioremap(&dev->dev, dev->res.start,
56 resource_size(&dev->res));
57 if (!base)
58 goto out_release;
59 nmk_rng.priv = (unsigned long)base;
60 ret = devm_hwrng_register(&dev->dev, &nmk_rng);
61 if (ret)
62 goto out_release;
63 return 0;
65 out_release:
66 amba_release_regions(dev);
67 out_clk:
68 clk_disable(rng_clk);
69 return ret;
72 static int nmk_rng_remove(struct amba_device *dev)
74 amba_release_regions(dev);
75 clk_disable(rng_clk);
76 return 0;
79 static struct amba_id nmk_rng_ids[] = {
81 .id = 0x000805e1,
82 .mask = 0x000fffff, /* top bits are rev and cfg: accept all */
84 {0, 0},
87 MODULE_DEVICE_TABLE(amba, nmk_rng_ids);
89 static struct amba_driver nmk_rng_driver = {
90 .drv = {
91 .owner = THIS_MODULE,
92 .name = "rng",
94 .probe = nmk_rng_probe,
95 .remove = nmk_rng_remove,
96 .id_table = nmk_rng_ids,
99 module_amba_driver(nmk_rng_driver);
101 MODULE_LICENSE("GPL");