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/mod_devicetable.h>
12 #include <linux/slab.h>
13 #include <linux/err.h>
14 #include <linux/clk.h>
16 #include <linux/hw_random.h>
17 #include <linux/platform_device.h>
21 #define TRNG_ODATA 0x50
23 #define TRNG_KEY 0x524e4700 /* RNG */
31 static int atmel_trng_read(struct hwrng
*rng
, void *buf
, size_t max
,
34 struct atmel_trng
*trng
= container_of(rng
, struct atmel_trng
, rng
);
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
46 readl(trng
->base
+ TRNG_ISR
);
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
;
68 trng
= devm_kzalloc(&pdev
->dev
, sizeof(*trng
), GFP_KERNEL
);
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
);
85 atmel_trng_enable(trng
);
86 trng
->rng
.name
= pdev
->name
;
87 trng
->rng
.read
= atmel_trng_read
;
89 ret
= hwrng_register(&trng
->rng
);
93 platform_set_drvdata(pdev
, trng
);
98 clk_disable_unprepare(trng
->clk
);
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
);
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
);
125 static int atmel_trng_resume(struct device
*dev
)
127 struct atmel_trng
*trng
= dev_get_drvdata(dev
);
130 ret
= clk_prepare_enable(trng
->clk
);
134 atmel_trng_enable(trng
);
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" },
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
,
155 .name
= "atmel-trng",
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");