Linux 4.18.10
[linux/fpc-iii.git] / drivers / char / hw_random / atmel-rng.c
blob661c82cde0f211c0ed5912eb81a814043adc31d9
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/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>
18 #define TRNG_CR 0x00
19 #define TRNG_ISR 0x1c
20 #define TRNG_ODATA 0x50
22 #define TRNG_KEY 0x524e4700 /* RNG */
24 struct atmel_trng {
25 struct clk *clk;
26 void __iomem *base;
27 struct hwrng rng;
30 static int atmel_trng_read(struct hwrng *rng, void *buf, size_t max,
31 bool wait)
33 struct atmel_trng *trng = container_of(rng, struct atmel_trng, rng);
34 u32 *data = buf;
36 /* data ready? */
37 if (readl(trng->base + TRNG_ISR) & 1) {
38 *data = readl(trng->base + TRNG_ODATA);
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
45 readl(trng->base + TRNG_ISR);
46 return 4;
47 } else
48 return 0;
51 static void atmel_trng_enable(struct atmel_trng *trng)
53 writel(TRNG_KEY | 1, trng->base + TRNG_CR);
56 static void atmel_trng_disable(struct atmel_trng *trng)
58 writel(TRNG_KEY, trng->base + TRNG_CR);
61 static int atmel_trng_probe(struct platform_device *pdev)
63 struct atmel_trng *trng;
64 struct resource *res;
65 int ret;
67 trng = devm_kzalloc(&pdev->dev, sizeof(*trng), GFP_KERNEL);
68 if (!trng)
69 return -ENOMEM;
71 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
72 trng->base = devm_ioremap_resource(&pdev->dev, res);
73 if (IS_ERR(trng->base))
74 return PTR_ERR(trng->base);
76 trng->clk = devm_clk_get(&pdev->dev, NULL);
77 if (IS_ERR(trng->clk))
78 return PTR_ERR(trng->clk);
80 ret = clk_prepare_enable(trng->clk);
81 if (ret)
82 return ret;
84 atmel_trng_enable(trng);
85 trng->rng.name = pdev->name;
86 trng->rng.read = atmel_trng_read;
88 ret = hwrng_register(&trng->rng);
89 if (ret)
90 goto err_register;
92 platform_set_drvdata(pdev, trng);
94 return 0;
96 err_register:
97 clk_disable_unprepare(trng->clk);
98 return ret;
101 static int atmel_trng_remove(struct platform_device *pdev)
103 struct atmel_trng *trng = platform_get_drvdata(pdev);
105 hwrng_unregister(&trng->rng);
107 atmel_trng_disable(trng);
108 clk_disable_unprepare(trng->clk);
110 return 0;
113 #ifdef CONFIG_PM
114 static int atmel_trng_suspend(struct device *dev)
116 struct atmel_trng *trng = dev_get_drvdata(dev);
118 atmel_trng_disable(trng);
119 clk_disable_unprepare(trng->clk);
121 return 0;
124 static int atmel_trng_resume(struct device *dev)
126 struct atmel_trng *trng = dev_get_drvdata(dev);
127 int ret;
129 ret = clk_prepare_enable(trng->clk);
130 if (ret)
131 return ret;
133 atmel_trng_enable(trng);
135 return 0;
138 static const struct dev_pm_ops atmel_trng_pm_ops = {
139 .suspend = atmel_trng_suspend,
140 .resume = atmel_trng_resume,
142 #endif /* CONFIG_PM */
144 static const struct of_device_id atmel_trng_dt_ids[] = {
145 { .compatible = "atmel,at91sam9g45-trng" },
146 { /* sentinel */ }
148 MODULE_DEVICE_TABLE(of, atmel_trng_dt_ids);
150 static struct platform_driver atmel_trng_driver = {
151 .probe = atmel_trng_probe,
152 .remove = atmel_trng_remove,
153 .driver = {
154 .name = "atmel-trng",
155 #ifdef CONFIG_PM
156 .pm = &atmel_trng_pm_ops,
157 #endif /* CONFIG_PM */
158 .of_match_table = atmel_trng_dt_ids,
162 module_platform_driver(atmel_trng_driver);
164 MODULE_LICENSE("GPL");
165 MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
166 MODULE_DESCRIPTION("Atmel true random number generator driver");