]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/char/hw_random/atmel-rng.c
Merge branch 'lpc32xx/core2' of git://git.antcom.de/linux-2.6 into next/soc
[karo-tx-linux.git] / drivers / char / hw_random / atmel-rng.c
1 /*
2  * Copyright (c) 2011 Peter Korsgaard <jacmet@sunsite.dk>
3  *
4  * This file is licensed under  the terms of the GNU General Public
5  * License version 2. This program is licensed "as is" without any
6  * warranty of any kind, whether express or implied.
7  */
8
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/err.h>
13 #include <linux/clk.h>
14 #include <linux/io.h>
15 #include <linux/hw_random.h>
16 #include <linux/platform_device.h>
17
18 #define TRNG_CR         0x00
19 #define TRNG_ISR        0x1c
20 #define TRNG_ODATA      0x50
21
22 #define TRNG_KEY        0x524e4700 /* RNG */
23
24 struct atmel_trng {
25         struct clk *clk;
26         void __iomem *base;
27         struct hwrng rng;
28 };
29
30 static int atmel_trng_read(struct hwrng *rng, void *buf, size_t max,
31                            bool wait)
32 {
33         struct atmel_trng *trng = container_of(rng, struct atmel_trng, rng);
34         u32 *data = buf;
35
36         /* data ready? */
37         if (readl(trng->base + TRNG_ISR) & 1) {
38                 *data = readl(trng->base + TRNG_ODATA);
39                 /*
40                   ensure data ready is only set again AFTER the next data
41                   word is ready in case it got set between checking ISR
42                   and reading ODATA, so we don't risk re-reading the
43                   same word
44                 */
45                 readl(trng->base + TRNG_ISR);
46                 return 4;
47         } else
48                 return 0;
49 }
50
51 static int atmel_trng_probe(struct platform_device *pdev)
52 {
53         struct atmel_trng *trng;
54         struct resource *res;
55         int ret;
56
57         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
58         if (!res)
59                 return -EINVAL;
60
61         trng = devm_kzalloc(&pdev->dev, sizeof(*trng), GFP_KERNEL);
62         if (!trng)
63                 return -ENOMEM;
64
65         if (!devm_request_mem_region(&pdev->dev, res->start,
66                                      resource_size(res), pdev->name))
67                 return -EBUSY;
68
69         trng->base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
70         if (!trng->base)
71                 return -EBUSY;
72
73         trng->clk = clk_get(&pdev->dev, NULL);
74         if (IS_ERR(trng->clk))
75                 return PTR_ERR(trng->clk);
76
77         ret = clk_enable(trng->clk);
78         if (ret)
79                 goto err_enable;
80
81         writel(TRNG_KEY | 1, trng->base + TRNG_CR);
82         trng->rng.name = pdev->name;
83         trng->rng.read = atmel_trng_read;
84
85         ret = hwrng_register(&trng->rng);
86         if (ret)
87                 goto err_register;
88
89         platform_set_drvdata(pdev, trng);
90
91         return 0;
92
93 err_register:
94         clk_disable(trng->clk);
95 err_enable:
96         clk_put(trng->clk);
97
98         return ret;
99 }
100
101 static int __devexit atmel_trng_remove(struct platform_device *pdev)
102 {
103         struct atmel_trng *trng = platform_get_drvdata(pdev);
104
105         hwrng_unregister(&trng->rng);
106
107         writel(TRNG_KEY, trng->base + TRNG_CR);
108         clk_disable(trng->clk);
109         clk_put(trng->clk);
110
111         platform_set_drvdata(pdev, NULL);
112
113         return 0;
114 }
115
116 #ifdef CONFIG_PM
117 static int atmel_trng_suspend(struct device *dev)
118 {
119         struct atmel_trng *trng = dev_get_drvdata(dev);
120
121         clk_disable(trng->clk);
122
123         return 0;
124 }
125
126 static int atmel_trng_resume(struct device *dev)
127 {
128         struct atmel_trng *trng = dev_get_drvdata(dev);
129
130         return clk_enable(trng->clk);
131 }
132
133 static const struct dev_pm_ops atmel_trng_pm_ops = {
134         .suspend        = atmel_trng_suspend,
135         .resume         = atmel_trng_resume,
136 };
137 #endif /* CONFIG_PM */
138
139 static struct platform_driver atmel_trng_driver = {
140         .probe          = atmel_trng_probe,
141         .remove         = __devexit_p(atmel_trng_remove),
142         .driver         = {
143                 .name   = "atmel-trng",
144                 .owner  = THIS_MODULE,
145 #ifdef CONFIG_PM
146                 .pm     = &atmel_trng_pm_ops,
147 #endif /* CONFIG_PM */
148         },
149 };
150
151 module_platform_driver(atmel_trng_driver);
152
153 MODULE_LICENSE("GPL");
154 MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
155 MODULE_DESCRIPTION("Atmel true random number generator driver");