Alessandro Rubini | bc85b25 | 2009-12-19 19:45:43 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Nomadik RNG support |
| 3 | * Copyright 2009 Alessandro Rubini |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/module.h> |
Alessandro Rubini | bc85b25 | 2009-12-19 19:45:43 +0800 | [diff] [blame] | 13 | #include <linux/device.h> |
| 14 | #include <linux/amba/bus.h> |
| 15 | #include <linux/hw_random.h> |
| 16 | #include <linux/io.h> |
Srinidhi Kasagar | 1944cc8 | 2010-05-19 06:49:13 +0100 | [diff] [blame] | 17 | #include <linux/clk.h> |
| 18 | #include <linux/err.h> |
| 19 | |
| 20 | static struct clk *rng_clk; |
Alessandro Rubini | bc85b25 | 2009-12-19 19:45:43 +0800 | [diff] [blame] | 21 | |
| 22 | static int nmk_rng_read(struct hwrng *rng, void *data, size_t max, bool wait) |
| 23 | { |
| 24 | void __iomem *base = (void __iomem *)rng->priv; |
| 25 | |
| 26 | /* |
| 27 | * The register is 32 bits and gives 16 random bits (low half). |
| 28 | * A subsequent read will delay the core for 400ns, so we just read |
| 29 | * once and accept the very unlikely very small delay, even if wait==0. |
| 30 | */ |
| 31 | *(u16 *)data = __raw_readl(base + 8) & 0xffff; |
| 32 | return 2; |
| 33 | } |
| 34 | |
| 35 | /* we have at most one RNG per machine, granted */ |
| 36 | static struct hwrng nmk_rng = { |
| 37 | .name = "nomadik", |
| 38 | .read = nmk_rng_read, |
| 39 | }; |
| 40 | |
Russell King | aa25afa | 2011-02-19 15:55:00 +0000 | [diff] [blame] | 41 | 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] | 42 | { |
| 43 | void __iomem *base; |
| 44 | int ret; |
| 45 | |
Jingoo Han | 1655c24 | 2014-02-27 14:06:31 +0900 | [diff] [blame] | 46 | rng_clk = devm_clk_get(&dev->dev, NULL); |
Srinidhi Kasagar | 1944cc8 | 2010-05-19 06:49:13 +0100 | [diff] [blame] | 47 | if (IS_ERR(rng_clk)) { |
| 48 | dev_err(&dev->dev, "could not get rng clock\n"); |
| 49 | ret = PTR_ERR(rng_clk); |
| 50 | return ret; |
| 51 | } |
| 52 | |
Linus Walleij | beca35d | 2013-06-03 02:02:09 +0200 | [diff] [blame] | 53 | clk_prepare_enable(rng_clk); |
Srinidhi Kasagar | 1944cc8 | 2010-05-19 06:49:13 +0100 | [diff] [blame] | 54 | |
Alessandro Rubini | bc85b25 | 2009-12-19 19:45:43 +0800 | [diff] [blame] | 55 | ret = amba_request_regions(dev, dev->dev.init_name); |
| 56 | if (ret) |
Julia Lawall | 2725ae1 | 2011-06-08 21:00:55 +0800 | [diff] [blame] | 57 | goto out_clk; |
Alessandro Rubini | bc85b25 | 2009-12-19 19:45:43 +0800 | [diff] [blame] | 58 | ret = -ENOMEM; |
Jingoo Han | 1655c24 | 2014-02-27 14:06:31 +0900 | [diff] [blame] | 59 | base = devm_ioremap(&dev->dev, dev->res.start, |
| 60 | resource_size(&dev->res)); |
Alessandro Rubini | bc85b25 | 2009-12-19 19:45:43 +0800 | [diff] [blame] | 61 | if (!base) |
| 62 | goto out_release; |
| 63 | nmk_rng.priv = (unsigned long)base; |
| 64 | ret = hwrng_register(&nmk_rng); |
| 65 | if (ret) |
Jingoo Han | 1655c24 | 2014-02-27 14:06:31 +0900 | [diff] [blame] | 66 | goto out_release; |
Alessandro Rubini | bc85b25 | 2009-12-19 19:45:43 +0800 | [diff] [blame] | 67 | return 0; |
| 68 | |
Alessandro Rubini | bc85b25 | 2009-12-19 19:45:43 +0800 | [diff] [blame] | 69 | out_release: |
| 70 | amba_release_regions(dev); |
Julia Lawall | 2725ae1 | 2011-06-08 21:00:55 +0800 | [diff] [blame] | 71 | out_clk: |
Srinidhi Kasagar | 1944cc8 | 2010-05-19 06:49:13 +0100 | [diff] [blame] | 72 | clk_disable(rng_clk); |
Alessandro Rubini | bc85b25 | 2009-12-19 19:45:43 +0800 | [diff] [blame] | 73 | return ret; |
| 74 | } |
| 75 | |
| 76 | static int nmk_rng_remove(struct amba_device *dev) |
| 77 | { |
Alessandro Rubini | bc85b25 | 2009-12-19 19:45:43 +0800 | [diff] [blame] | 78 | hwrng_unregister(&nmk_rng); |
Alessandro Rubini | bc85b25 | 2009-12-19 19:45:43 +0800 | [diff] [blame] | 79 | amba_release_regions(dev); |
Srinidhi Kasagar | 1944cc8 | 2010-05-19 06:49:13 +0100 | [diff] [blame] | 80 | clk_disable(rng_clk); |
Alessandro Rubini | bc85b25 | 2009-12-19 19:45:43 +0800 | [diff] [blame] | 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | static struct amba_id nmk_rng_ids[] = { |
| 85 | { |
| 86 | .id = 0x000805e1, |
| 87 | .mask = 0x000fffff, /* top bits are rev and cfg: accept all */ |
| 88 | }, |
| 89 | {0, 0}, |
| 90 | }; |
| 91 | |
Dave Martin | 4e3f3d5 | 2011-10-05 15:15:20 +0100 | [diff] [blame] | 92 | MODULE_DEVICE_TABLE(amba, nmk_rng_ids); |
| 93 | |
Alessandro Rubini | bc85b25 | 2009-12-19 19:45:43 +0800 | [diff] [blame] | 94 | static struct amba_driver nmk_rng_driver = { |
| 95 | .drv = { |
| 96 | .owner = THIS_MODULE, |
| 97 | .name = "rng", |
| 98 | }, |
| 99 | .probe = nmk_rng_probe, |
| 100 | .remove = nmk_rng_remove, |
| 101 | .id_table = nmk_rng_ids, |
| 102 | }; |
| 103 | |
viresh kumar | 9e5ed09 | 2012-03-15 10:40:38 +0100 | [diff] [blame] | 104 | module_amba_driver(nmk_rng_driver); |
Alessandro Rubini | bc85b25 | 2009-12-19 19:45:43 +0800 | [diff] [blame] | 105 | |
| 106 | MODULE_LICENSE("GPL"); |