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.
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>
15 #include <linux/hw_random.h>
16 #include <linux/platform_device.h>
20 #define TRNG_ODATA 0x50
22 #define TRNG_KEY 0x524e4700 /* RNG */
30 static int atmel_trng_read(struct hwrng
*rng
, void *buf
, size_t max
,
33 struct atmel_trng
*trng
= container_of(rng
, struct atmel_trng
, rng
);
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
45 readl(trng
->base
+ TRNG_ISR
);
51 static int atmel_trng_probe(struct platform_device
*pdev
)
53 struct atmel_trng
*trng
;
57 trng
= devm_kzalloc(&pdev
->dev
, sizeof(*trng
), GFP_KERNEL
);
61 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
62 trng
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
63 if (IS_ERR(trng
->base
))
64 return PTR_ERR(trng
->base
);
66 trng
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
67 if (IS_ERR(trng
->clk
))
68 return PTR_ERR(trng
->clk
);
70 ret
= clk_prepare_enable(trng
->clk
);
74 writel(TRNG_KEY
| 1, trng
->base
+ TRNG_CR
);
75 trng
->rng
.name
= pdev
->name
;
76 trng
->rng
.read
= atmel_trng_read
;
78 ret
= hwrng_register(&trng
->rng
);
82 platform_set_drvdata(pdev
, trng
);
87 clk_disable(trng
->clk
);
91 static int atmel_trng_remove(struct platform_device
*pdev
)
93 struct atmel_trng
*trng
= platform_get_drvdata(pdev
);
95 hwrng_unregister(&trng
->rng
);
97 writel(TRNG_KEY
, trng
->base
+ TRNG_CR
);
98 clk_disable_unprepare(trng
->clk
);
104 static int atmel_trng_suspend(struct device
*dev
)
106 struct atmel_trng
*trng
= dev_get_drvdata(dev
);
108 clk_disable_unprepare(trng
->clk
);
113 static int atmel_trng_resume(struct device
*dev
)
115 struct atmel_trng
*trng
= dev_get_drvdata(dev
);
117 return clk_prepare_enable(trng
->clk
);
120 static const struct dev_pm_ops atmel_trng_pm_ops
= {
121 .suspend
= atmel_trng_suspend
,
122 .resume
= atmel_trng_resume
,
124 #endif /* CONFIG_PM */
126 static const struct of_device_id atmel_trng_dt_ids
[] = {
127 { .compatible
= "atmel,at91sam9g45-trng" },
130 MODULE_DEVICE_TABLE(of
, atmel_trng_dt_ids
);
132 static struct platform_driver atmel_trng_driver
= {
133 .probe
= atmel_trng_probe
,
134 .remove
= atmel_trng_remove
,
136 .name
= "atmel-trng",
138 .pm
= &atmel_trng_pm_ops
,
139 #endif /* CONFIG_PM */
140 .of_match_table
= atmel_trng_dt_ids
,
144 module_platform_driver(atmel_trng_driver
);
146 MODULE_LICENSE("GPL");
147 MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
148 MODULE_DESCRIPTION("Atmel true random number generator driver");