dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / char / hw_random / atmel-rng.c
blob433426242b875c7297573e7985afabcdb569d25c
1 /*
2 * Copyright (c) 2011 Peter Korsgaard <jacmet@sunsite.dk>
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 */
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/mod_devicetable.h>
12 #include <linux/slab.h>
13 #include <linux/err.h>
14 #include <linux/clk.h>
15 #include <linux/io.h>
16 #include <linux/hw_random.h>
17 #include <linux/platform_device.h>
19 #define TRNG_CR 0x00
20 #define TRNG_ISR 0x1c
21 #define TRNG_ODATA 0x50
23 #define TRNG_KEY 0x524e4700 /* RNG */
25 struct atmel_trng {
26 struct clk *clk;
27 void __iomem *base;
28 struct hwrng rng;
31 static int atmel_trng_read(struct hwrng *rng, void *buf, size_t max,
32 bool wait)
34 struct atmel_trng *trng = container_of(rng, struct atmel_trng, rng);
35 u32 *data = buf;
37 /* data ready? */
38 if (readl(trng->base + TRNG_ISR) & 1) {
39 *data = readl(trng->base + TRNG_ODATA);
41 ensure data ready is only set again AFTER the next data
42 word is ready in case it got set between checking ISR
43 and reading ODATA, so we don't risk re-reading the
44 same word
46 readl(trng->base + TRNG_ISR);
47 return 4;
48 } else
49 return 0;
52 static void atmel_trng_enable(struct atmel_trng *trng)
54 writel(TRNG_KEY | 1, trng->base + TRNG_CR);
57 static void atmel_trng_disable(struct atmel_trng *trng)
59 writel(TRNG_KEY, trng->base + TRNG_CR);
62 static int atmel_trng_probe(struct platform_device *pdev)
64 struct atmel_trng *trng;
65 struct resource *res;
66 int ret;
68 trng = devm_kzalloc(&pdev->dev, sizeof(*trng), GFP_KERNEL);
69 if (!trng)
70 return -ENOMEM;
72 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
73 trng->base = devm_ioremap_resource(&pdev->dev, res);
74 if (IS_ERR(trng->base))
75 return PTR_ERR(trng->base);
77 trng->clk = devm_clk_get(&pdev->dev, NULL);
78 if (IS_ERR(trng->clk))
79 return PTR_ERR(trng->clk);
81 ret = clk_prepare_enable(trng->clk);
82 if (ret)
83 return ret;
85 atmel_trng_enable(trng);
86 trng->rng.name = pdev->name;
87 trng->rng.read = atmel_trng_read;
89 ret = hwrng_register(&trng->rng);
90 if (ret)
91 goto err_register;
93 platform_set_drvdata(pdev, trng);
95 return 0;
97 err_register:
98 clk_disable_unprepare(trng->clk);
99 return ret;
102 static int atmel_trng_remove(struct platform_device *pdev)
104 struct atmel_trng *trng = platform_get_drvdata(pdev);
106 hwrng_unregister(&trng->rng);
108 atmel_trng_disable(trng);
109 clk_disable_unprepare(trng->clk);
111 return 0;
114 #ifdef CONFIG_PM
115 static int atmel_trng_suspend(struct device *dev)
117 struct atmel_trng *trng = dev_get_drvdata(dev);
119 atmel_trng_disable(trng);
120 clk_disable_unprepare(trng->clk);
122 return 0;
125 static int atmel_trng_resume(struct device *dev)
127 struct atmel_trng *trng = dev_get_drvdata(dev);
128 int ret;
130 ret = clk_prepare_enable(trng->clk);
131 if (ret)
132 return ret;
134 atmel_trng_enable(trng);
136 return 0;
139 static const struct dev_pm_ops atmel_trng_pm_ops = {
140 .suspend = atmel_trng_suspend,
141 .resume = atmel_trng_resume,
143 #endif /* CONFIG_PM */
145 static const struct of_device_id atmel_trng_dt_ids[] = {
146 { .compatible = "atmel,at91sam9g45-trng" },
147 { /* sentinel */ }
149 MODULE_DEVICE_TABLE(of, atmel_trng_dt_ids);
151 static struct platform_driver atmel_trng_driver = {
152 .probe = atmel_trng_probe,
153 .remove = atmel_trng_remove,
154 .driver = {
155 .name = "atmel-trng",
156 #ifdef CONFIG_PM
157 .pm = &atmel_trng_pm_ops,
158 #endif /* CONFIG_PM */
159 .of_match_table = atmel_trng_dt_ids,
163 module_platform_driver(atmel_trng_driver);
165 MODULE_LICENSE("GPL");
166 MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
167 MODULE_DESCRIPTION("Atmel true random number generator driver");