blob: a348c7e9aa0bbb44a360afb0f92806be6cc1560b [file] [log] [blame]
Alessandro Rubinibc85b252009-12-19 19:45:43 +08001/*
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>
13#include <linux/init.h>
14#include <linux/device.h>
15#include <linux/amba/bus.h>
16#include <linux/hw_random.h>
17#include <linux/io.h>
Srinidhi Kasagar1944cc82010-05-19 06:49:13 +010018#include <linux/clk.h>
19#include <linux/err.h>
20
21static struct clk *rng_clk;
Alessandro Rubinibc85b252009-12-19 19:45:43 +080022
23static int nmk_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
24{
25 void __iomem *base = (void __iomem *)rng->priv;
26
27 /*
28 * The register is 32 bits and gives 16 random bits (low half).
29 * A subsequent read will delay the core for 400ns, so we just read
30 * once and accept the very unlikely very small delay, even if wait==0.
31 */
32 *(u16 *)data = __raw_readl(base + 8) & 0xffff;
33 return 2;
34}
35
36/* we have at most one RNG per machine, granted */
37static struct hwrng nmk_rng = {
38 .name = "nomadik",
39 .read = nmk_rng_read,
40};
41
42static int nmk_rng_probe(struct amba_device *dev, struct amba_id *id)
43{
44 void __iomem *base;
45 int ret;
46
Srinidhi Kasagar1944cc82010-05-19 06:49:13 +010047 rng_clk = clk_get(&dev->dev, NULL);
48 if (IS_ERR(rng_clk)) {
49 dev_err(&dev->dev, "could not get rng clock\n");
50 ret = PTR_ERR(rng_clk);
51 return ret;
52 }
53
54 clk_enable(rng_clk);
55
Alessandro Rubinibc85b252009-12-19 19:45:43 +080056 ret = amba_request_regions(dev, dev->dev.init_name);
57 if (ret)
58 return ret;
59 ret = -ENOMEM;
60 base = ioremap(dev->res.start, resource_size(&dev->res));
61 if (!base)
62 goto out_release;
63 nmk_rng.priv = (unsigned long)base;
64 ret = hwrng_register(&nmk_rng);
65 if (ret)
66 goto out_unmap;
67 return 0;
68
69out_unmap:
70 iounmap(base);
71out_release:
72 amba_release_regions(dev);
Srinidhi Kasagar1944cc82010-05-19 06:49:13 +010073 clk_disable(rng_clk);
74 clk_put(rng_clk);
Alessandro Rubinibc85b252009-12-19 19:45:43 +080075 return ret;
76}
77
78static int nmk_rng_remove(struct amba_device *dev)
79{
80 void __iomem *base = (void __iomem *)nmk_rng.priv;
81 hwrng_unregister(&nmk_rng);
82 iounmap(base);
83 amba_release_regions(dev);
Srinidhi Kasagar1944cc82010-05-19 06:49:13 +010084 clk_disable(rng_clk);
85 clk_put(rng_clk);
Alessandro Rubinibc85b252009-12-19 19:45:43 +080086 return 0;
87}
88
89static struct amba_id nmk_rng_ids[] = {
90 {
91 .id = 0x000805e1,
92 .mask = 0x000fffff, /* top bits are rev and cfg: accept all */
93 },
94 {0, 0},
95};
96
97static struct amba_driver nmk_rng_driver = {
98 .drv = {
99 .owner = THIS_MODULE,
100 .name = "rng",
101 },
102 .probe = nmk_rng_probe,
103 .remove = nmk_rng_remove,
104 .id_table = nmk_rng_ids,
105};
106
107static int __init nmk_rng_init(void)
108{
109 return amba_driver_register(&nmk_rng_driver);
110}
111
112static void __devexit nmk_rng_exit(void)
113{
114 amba_driver_unregister(&nmk_rng_driver);
115}
116
117module_init(nmk_rng_init);
118module_exit(nmk_rng_exit);
119
120MODULE_LICENSE("GPL");