Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Alessandro Rubini | bc85b25 | 2009-12-19 19:45:43 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Nomadik RNG support |
| 4 | * Copyright 2009 Alessandro Rubini |
Alessandro Rubini | bc85b25 | 2009-12-19 19:45:43 +0800 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/kernel.h> |
| 8 | #include <linux/module.h> |
Alessandro Rubini | bc85b25 | 2009-12-19 19:45:43 +0800 | [diff] [blame] | 9 | #include <linux/device.h> |
| 10 | #include <linux/amba/bus.h> |
| 11 | #include <linux/hw_random.h> |
| 12 | #include <linux/io.h> |
Srinidhi Kasagar | 1944cc8 | 2010-05-19 06:49:13 +0100 | [diff] [blame] | 13 | #include <linux/clk.h> |
| 14 | #include <linux/err.h> |
| 15 | |
| 16 | static struct clk *rng_clk; |
Alessandro Rubini | bc85b25 | 2009-12-19 19:45:43 +0800 | [diff] [blame] | 17 | |
| 18 | static 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 */ |
| 32 | static struct hwrng nmk_rng = { |
| 33 | .name = "nomadik", |
| 34 | .read = nmk_rng_read, |
| 35 | }; |
| 36 | |
Russell King | aa25afa | 2011-02-19 15:55:00 +0000 | [diff] [blame] | 37 | static int nmk_rng_probe(struct amba_device *dev, const struct amba_id *id) |
Alessandro Rubini | bc85b25 | 2009-12-19 19:45:43 +0800 | [diff] [blame] | 38 | { |
| 39 | void __iomem *base; |
| 40 | int ret; |
| 41 | |
Jingoo Han | 1655c24 | 2014-02-27 14:06:31 +0900 | [diff] [blame] | 42 | rng_clk = devm_clk_get(&dev->dev, NULL); |
Srinidhi Kasagar | 1944cc8 | 2010-05-19 06:49:13 +0100 | [diff] [blame] | 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; |
| 47 | } |
| 48 | |
Linus Walleij | beca35d | 2013-06-03 02:02:09 +0200 | [diff] [blame] | 49 | clk_prepare_enable(rng_clk); |
Srinidhi Kasagar | 1944cc8 | 2010-05-19 06:49:13 +0100 | [diff] [blame] | 50 | |
Alessandro Rubini | bc85b25 | 2009-12-19 19:45:43 +0800 | [diff] [blame] | 51 | ret = amba_request_regions(dev, dev->dev.init_name); |
| 52 | if (ret) |
Julia Lawall | 2725ae1 | 2011-06-08 21:00:55 +0800 | [diff] [blame] | 53 | goto out_clk; |
Alessandro Rubini | bc85b25 | 2009-12-19 19:45:43 +0800 | [diff] [blame] | 54 | ret = -ENOMEM; |
Jingoo Han | 1655c24 | 2014-02-27 14:06:31 +0900 | [diff] [blame] | 55 | base = devm_ioremap(&dev->dev, dev->res.start, |
| 56 | resource_size(&dev->res)); |
Alessandro Rubini | bc85b25 | 2009-12-19 19:45:43 +0800 | [diff] [blame] | 57 | if (!base) |
| 58 | goto out_release; |
| 59 | nmk_rng.priv = (unsigned long)base; |
Chuhong Yuan | 3e75241 | 2019-07-25 16:01:55 +0800 | [diff] [blame] | 60 | ret = devm_hwrng_register(&dev->dev, &nmk_rng); |
Alessandro Rubini | bc85b25 | 2009-12-19 19:45:43 +0800 | [diff] [blame] | 61 | if (ret) |
Jingoo Han | 1655c24 | 2014-02-27 14:06:31 +0900 | [diff] [blame] | 62 | goto out_release; |
Alessandro Rubini | bc85b25 | 2009-12-19 19:45:43 +0800 | [diff] [blame] | 63 | return 0; |
| 64 | |
Alessandro Rubini | bc85b25 | 2009-12-19 19:45:43 +0800 | [diff] [blame] | 65 | out_release: |
| 66 | amba_release_regions(dev); |
Julia Lawall | 2725ae1 | 2011-06-08 21:00:55 +0800 | [diff] [blame] | 67 | out_clk: |
Srinidhi Kasagar | 1944cc8 | 2010-05-19 06:49:13 +0100 | [diff] [blame] | 68 | clk_disable(rng_clk); |
Alessandro Rubini | bc85b25 | 2009-12-19 19:45:43 +0800 | [diff] [blame] | 69 | return ret; |
| 70 | } |
| 71 | |
| 72 | static int nmk_rng_remove(struct amba_device *dev) |
| 73 | { |
Alessandro Rubini | bc85b25 | 2009-12-19 19:45:43 +0800 | [diff] [blame] | 74 | amba_release_regions(dev); |
Srinidhi Kasagar | 1944cc8 | 2010-05-19 06:49:13 +0100 | [diff] [blame] | 75 | clk_disable(rng_clk); |
Alessandro Rubini | bc85b25 | 2009-12-19 19:45:43 +0800 | [diff] [blame] | 76 | return 0; |
| 77 | } |
| 78 | |
Rikard Falkeborn | b242973 | 2020-07-01 22:09:47 +0200 | [diff] [blame] | 79 | static const struct amba_id nmk_rng_ids[] = { |
Alessandro Rubini | bc85b25 | 2009-12-19 19:45:43 +0800 | [diff] [blame] | 80 | { |
| 81 | .id = 0x000805e1, |
| 82 | .mask = 0x000fffff, /* top bits are rev and cfg: accept all */ |
| 83 | }, |
| 84 | {0, 0}, |
| 85 | }; |
| 86 | |
Dave Martin | 4e3f3d5 | 2011-10-05 15:15:20 +0100 | [diff] [blame] | 87 | MODULE_DEVICE_TABLE(amba, nmk_rng_ids); |
| 88 | |
Alessandro Rubini | bc85b25 | 2009-12-19 19:45:43 +0800 | [diff] [blame] | 89 | static 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 kumar | 9e5ed09 | 2012-03-15 10:40:38 +0100 | [diff] [blame] | 99 | module_amba_driver(nmk_rng_driver); |
Alessandro Rubini | bc85b25 | 2009-12-19 19:45:43 +0800 | [diff] [blame] | 100 | |
| 101 | MODULE_LICENSE("GPL"); |