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/of_device.h>
18 #include <linux/platform_device.h>
23 #define TRNG_ODATA 0x50
25 #define TRNG_KEY 0x524e4700 /* RNG */
27 #define TRNG_HALFR BIT(0) /* generate RN every 168 cycles */
29 struct atmel_trng_data
{
39 static int atmel_trng_read(struct hwrng
*rng
, void *buf
, size_t max
,
42 struct atmel_trng
*trng
= container_of(rng
, struct atmel_trng
, rng
);
46 if (readl(trng
->base
+ TRNG_ISR
) & 1) {
47 *data
= readl(trng
->base
+ TRNG_ODATA
);
49 ensure data ready is only set again AFTER the next data
50 word is ready in case it got set between checking ISR
51 and reading ODATA, so we don't risk re-reading the
54 readl(trng
->base
+ TRNG_ISR
);
60 static void atmel_trng_enable(struct atmel_trng
*trng
)
62 writel(TRNG_KEY
| 1, trng
->base
+ TRNG_CR
);
65 static void atmel_trng_disable(struct atmel_trng
*trng
)
67 writel(TRNG_KEY
, trng
->base
+ TRNG_CR
);
70 static int atmel_trng_probe(struct platform_device
*pdev
)
72 struct atmel_trng
*trng
;
73 const struct atmel_trng_data
*data
;
76 trng
= devm_kzalloc(&pdev
->dev
, sizeof(*trng
), GFP_KERNEL
);
80 trng
->base
= devm_platform_ioremap_resource(pdev
, 0);
81 if (IS_ERR(trng
->base
))
82 return PTR_ERR(trng
->base
);
84 trng
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
85 if (IS_ERR(trng
->clk
))
86 return PTR_ERR(trng
->clk
);
87 data
= of_device_get_match_data(&pdev
->dev
);
91 if (data
->has_half_rate
) {
92 unsigned long rate
= clk_get_rate(trng
->clk
);
94 /* if peripheral clk is above 100MHz, set HALFR */
96 writel(TRNG_HALFR
, trng
->base
+ TRNG_MR
);
99 ret
= clk_prepare_enable(trng
->clk
);
103 atmel_trng_enable(trng
);
104 trng
->rng
.name
= pdev
->name
;
105 trng
->rng
.read
= atmel_trng_read
;
107 ret
= devm_hwrng_register(&pdev
->dev
, &trng
->rng
);
111 platform_set_drvdata(pdev
, trng
);
116 clk_disable_unprepare(trng
->clk
);
120 static int atmel_trng_remove(struct platform_device
*pdev
)
122 struct atmel_trng
*trng
= platform_get_drvdata(pdev
);
125 atmel_trng_disable(trng
);
126 clk_disable_unprepare(trng
->clk
);
132 static int atmel_trng_suspend(struct device
*dev
)
134 struct atmel_trng
*trng
= dev_get_drvdata(dev
);
136 atmel_trng_disable(trng
);
137 clk_disable_unprepare(trng
->clk
);
142 static int atmel_trng_resume(struct device
*dev
)
144 struct atmel_trng
*trng
= dev_get_drvdata(dev
);
147 ret
= clk_prepare_enable(trng
->clk
);
151 atmel_trng_enable(trng
);
156 static const struct dev_pm_ops atmel_trng_pm_ops
= {
157 .suspend
= atmel_trng_suspend
,
158 .resume
= atmel_trng_resume
,
160 #endif /* CONFIG_PM */
162 static const struct atmel_trng_data at91sam9g45_config
= {
163 .has_half_rate
= false,
166 static const struct atmel_trng_data sam9x60_config
= {
167 .has_half_rate
= true,
170 static const struct of_device_id atmel_trng_dt_ids
[] = {
172 .compatible
= "atmel,at91sam9g45-trng",
173 .data
= &at91sam9g45_config
,
175 .compatible
= "microchip,sam9x60-trng",
176 .data
= &sam9x60_config
,
181 MODULE_DEVICE_TABLE(of
, atmel_trng_dt_ids
);
183 static struct platform_driver atmel_trng_driver
= {
184 .probe
= atmel_trng_probe
,
185 .remove
= atmel_trng_remove
,
187 .name
= "atmel-trng",
189 .pm
= &atmel_trng_pm_ops
,
190 #endif /* CONFIG_PM */
191 .of_match_table
= atmel_trng_dt_ids
,
195 module_platform_driver(atmel_trng_driver
);
197 MODULE_LICENSE("GPL");
198 MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
199 MODULE_DESCRIPTION("Atmel true random number generator driver");