1677d3e2fSPeter Korsgaard /*
2677d3e2fSPeter Korsgaard * Copyright (c) 2011 Peter Korsgaard <jacmet@sunsite.dk>
3677d3e2fSPeter Korsgaard *
4677d3e2fSPeter Korsgaard * This file is licensed under the terms of the GNU General Public
5677d3e2fSPeter Korsgaard * License version 2. This program is licensed "as is" without any
6677d3e2fSPeter Korsgaard * warranty of any kind, whether express or implied.
7677d3e2fSPeter Korsgaard */
8677d3e2fSPeter Korsgaard
9677d3e2fSPeter Korsgaard #include <linux/kernel.h>
10677d3e2fSPeter Korsgaard #include <linux/module.h>
11ac316725SRandy Dunlap #include <linux/mod_devicetable.h>
12677d3e2fSPeter Korsgaard #include <linux/slab.h>
13677d3e2fSPeter Korsgaard #include <linux/err.h>
14677d3e2fSPeter Korsgaard #include <linux/clk.h>
15677d3e2fSPeter Korsgaard #include <linux/io.h>
160934683dSClaudiu Beznea #include <linux/iopoll.h>
17677d3e2fSPeter Korsgaard #include <linux/hw_random.h>
18*0788257aSRob Herring #include <linux/of.h>
19677d3e2fSPeter Korsgaard #include <linux/platform_device.h>
20c4f51eabSClaudiu Beznea #include <linux/pm_runtime.h>
21677d3e2fSPeter Korsgaard
22677d3e2fSPeter Korsgaard #define TRNG_CR 0x00
235c49645cSCodrin Ciubotariu #define TRNG_MR 0x04
24677d3e2fSPeter Korsgaard #define TRNG_ISR 0x1c
250934683dSClaudiu Beznea #define TRNG_ISR_DATRDY BIT(0)
26677d3e2fSPeter Korsgaard #define TRNG_ODATA 0x50
27677d3e2fSPeter Korsgaard
28677d3e2fSPeter Korsgaard #define TRNG_KEY 0x524e4700 /* RNG */
29677d3e2fSPeter Korsgaard
305c49645cSCodrin Ciubotariu #define TRNG_HALFR BIT(0) /* generate RN every 168 cycles */
315c49645cSCodrin Ciubotariu
325c49645cSCodrin Ciubotariu struct atmel_trng_data {
335c49645cSCodrin Ciubotariu bool has_half_rate;
345c49645cSCodrin Ciubotariu };
355c49645cSCodrin Ciubotariu
36677d3e2fSPeter Korsgaard struct atmel_trng {
37677d3e2fSPeter Korsgaard struct clk *clk;
38677d3e2fSPeter Korsgaard void __iomem *base;
39677d3e2fSPeter Korsgaard struct hwrng rng;
409fbd8b30SClaudiu Beznea bool has_half_rate;
41677d3e2fSPeter Korsgaard };
42677d3e2fSPeter Korsgaard
atmel_trng_wait_ready(struct atmel_trng * trng,bool wait)430934683dSClaudiu Beznea static bool atmel_trng_wait_ready(struct atmel_trng *trng, bool wait)
440934683dSClaudiu Beznea {
450934683dSClaudiu Beznea int ready;
460934683dSClaudiu Beznea
470934683dSClaudiu Beznea ready = readl(trng->base + TRNG_ISR) & TRNG_ISR_DATRDY;
480934683dSClaudiu Beznea if (!ready && wait)
490934683dSClaudiu Beznea readl_poll_timeout(trng->base + TRNG_ISR, ready,
500934683dSClaudiu Beznea ready & TRNG_ISR_DATRDY, 1000, 20000);
510934683dSClaudiu Beznea
520934683dSClaudiu Beznea return !!ready;
530934683dSClaudiu Beznea }
540934683dSClaudiu Beznea
atmel_trng_read(struct hwrng * rng,void * buf,size_t max,bool wait)55677d3e2fSPeter Korsgaard static int atmel_trng_read(struct hwrng *rng, void *buf, size_t max,
56677d3e2fSPeter Korsgaard bool wait)
57677d3e2fSPeter Korsgaard {
58677d3e2fSPeter Korsgaard struct atmel_trng *trng = container_of(rng, struct atmel_trng, rng);
59677d3e2fSPeter Korsgaard u32 *data = buf;
600934683dSClaudiu Beznea int ret;
61677d3e2fSPeter Korsgaard
62c4f51eabSClaudiu Beznea ret = pm_runtime_get_sync((struct device *)trng->rng.priv);
63c4f51eabSClaudiu Beznea if (ret < 0) {
64c4f51eabSClaudiu Beznea pm_runtime_put_sync((struct device *)trng->rng.priv);
65c4f51eabSClaudiu Beznea return ret;
66c4f51eabSClaudiu Beznea }
67c4f51eabSClaudiu Beznea
680934683dSClaudiu Beznea ret = atmel_trng_wait_ready(trng, wait);
690934683dSClaudiu Beznea if (!ret)
700934683dSClaudiu Beznea goto out;
710934683dSClaudiu Beznea
72677d3e2fSPeter Korsgaard *data = readl(trng->base + TRNG_ODATA);
73121daad8SPeter Korsgaard /*
740934683dSClaudiu Beznea * ensure data ready is only set again AFTER the next data word is ready
750934683dSClaudiu Beznea * in case it got set between checking ISR and reading ODATA, so we
760934683dSClaudiu Beznea * don't risk re-reading the same word
77121daad8SPeter Korsgaard */
78121daad8SPeter Korsgaard readl(trng->base + TRNG_ISR);
790934683dSClaudiu Beznea ret = 4;
800934683dSClaudiu Beznea
810934683dSClaudiu Beznea out:
82c4f51eabSClaudiu Beznea pm_runtime_mark_last_busy((struct device *)trng->rng.priv);
83c4f51eabSClaudiu Beznea pm_runtime_put_sync_autosuspend((struct device *)trng->rng.priv);
840934683dSClaudiu Beznea return ret;
85677d3e2fSPeter Korsgaard }
86677d3e2fSPeter Korsgaard
atmel_trng_init(struct atmel_trng * trng)879fbd8b30SClaudiu Beznea static int atmel_trng_init(struct atmel_trng *trng)
88a1fa98d8SWenyou Yang {
899fbd8b30SClaudiu Beznea unsigned long rate;
909fbd8b30SClaudiu Beznea int ret;
919fbd8b30SClaudiu Beznea
929fbd8b30SClaudiu Beznea ret = clk_prepare_enable(trng->clk);
939fbd8b30SClaudiu Beznea if (ret)
949fbd8b30SClaudiu Beznea return ret;
959fbd8b30SClaudiu Beznea
969fbd8b30SClaudiu Beznea if (trng->has_half_rate) {
979fbd8b30SClaudiu Beznea rate = clk_get_rate(trng->clk);
989fbd8b30SClaudiu Beznea
999fbd8b30SClaudiu Beznea /* if peripheral clk is above 100MHz, set HALFR */
1009fbd8b30SClaudiu Beznea if (rate > 100000000)
1019fbd8b30SClaudiu Beznea writel(TRNG_HALFR, trng->base + TRNG_MR);
1029fbd8b30SClaudiu Beznea }
1039fbd8b30SClaudiu Beznea
104a1fa98d8SWenyou Yang writel(TRNG_KEY | 1, trng->base + TRNG_CR);
1059fbd8b30SClaudiu Beznea
1069fbd8b30SClaudiu Beznea return 0;
107a1fa98d8SWenyou Yang }
108a1fa98d8SWenyou Yang
atmel_trng_cleanup(struct atmel_trng * trng)109f14b0208SClaudiu Beznea static void atmel_trng_cleanup(struct atmel_trng *trng)
110a1fa98d8SWenyou Yang {
111a1fa98d8SWenyou Yang writel(TRNG_KEY, trng->base + TRNG_CR);
1129fbd8b30SClaudiu Beznea clk_disable_unprepare(trng->clk);
113a1fa98d8SWenyou Yang }
114a1fa98d8SWenyou Yang
atmel_trng_probe(struct platform_device * pdev)115677d3e2fSPeter Korsgaard static int atmel_trng_probe(struct platform_device *pdev)
116677d3e2fSPeter Korsgaard {
117677d3e2fSPeter Korsgaard struct atmel_trng *trng;
1185c49645cSCodrin Ciubotariu const struct atmel_trng_data *data;
119677d3e2fSPeter Korsgaard int ret;
120677d3e2fSPeter Korsgaard
121677d3e2fSPeter Korsgaard trng = devm_kzalloc(&pdev->dev, sizeof(*trng), GFP_KERNEL);
122677d3e2fSPeter Korsgaard if (!trng)
123677d3e2fSPeter Korsgaard return -ENOMEM;
124677d3e2fSPeter Korsgaard
125bc49534dSYueHaibing trng->base = devm_platform_ioremap_resource(pdev, 0);
126bfaff75bSJingoo Han if (IS_ERR(trng->base))
127bfaff75bSJingoo Han return PTR_ERR(trng->base);
128677d3e2fSPeter Korsgaard
1290c0becd0SJingoo Han trng->clk = devm_clk_get(&pdev->dev, NULL);
130677d3e2fSPeter Korsgaard if (IS_ERR(trng->clk))
131677d3e2fSPeter Korsgaard return PTR_ERR(trng->clk);
1325c49645cSCodrin Ciubotariu data = of_device_get_match_data(&pdev->dev);
1335c49645cSCodrin Ciubotariu if (!data)
1345c49645cSCodrin Ciubotariu return -ENODEV;
1355c49645cSCodrin Ciubotariu
1369fbd8b30SClaudiu Beznea trng->has_half_rate = data->has_half_rate;
137677d3e2fSPeter Korsgaard trng->rng.name = pdev->name;
138677d3e2fSPeter Korsgaard trng->rng.read = atmel_trng_read;
139c4f51eabSClaudiu Beznea trng->rng.priv = (unsigned long)&pdev->dev;
140c4f51eabSClaudiu Beznea platform_set_drvdata(pdev, trng);
141677d3e2fSPeter Korsgaard
142c4f51eabSClaudiu Beznea #ifndef CONFIG_PM
1439fbd8b30SClaudiu Beznea ret = atmel_trng_init(trng);
1449fbd8b30SClaudiu Beznea if (ret)
1459fbd8b30SClaudiu Beznea return ret;
146c4f51eabSClaudiu Beznea #endif
147c4f51eabSClaudiu Beznea
148c4f51eabSClaudiu Beznea pm_runtime_set_autosuspend_delay(&pdev->dev, 100);
149c4f51eabSClaudiu Beznea pm_runtime_use_autosuspend(&pdev->dev);
150c4f51eabSClaudiu Beznea pm_runtime_enable(&pdev->dev);
1519fbd8b30SClaudiu Beznea
1523e75241bSChuhong Yuan ret = devm_hwrng_register(&pdev->dev, &trng->rng);
153c4f51eabSClaudiu Beznea if (ret) {
154c4f51eabSClaudiu Beznea pm_runtime_disable(&pdev->dev);
155c4f51eabSClaudiu Beznea pm_runtime_set_suspended(&pdev->dev);
156c4f51eabSClaudiu Beznea #ifndef CONFIG_PM
157f14b0208SClaudiu Beznea atmel_trng_cleanup(trng);
158c4f51eabSClaudiu Beznea #endif
159c4f51eabSClaudiu Beznea }
160c4f51eabSClaudiu Beznea
161677d3e2fSPeter Korsgaard return ret;
162677d3e2fSPeter Korsgaard }
163677d3e2fSPeter Korsgaard
atmel_trng_remove(struct platform_device * pdev)16439af33fcSBill Pemberton static int atmel_trng_remove(struct platform_device *pdev)
165677d3e2fSPeter Korsgaard {
166677d3e2fSPeter Korsgaard struct atmel_trng *trng = platform_get_drvdata(pdev);
167677d3e2fSPeter Korsgaard
168f14b0208SClaudiu Beznea atmel_trng_cleanup(trng);
169c4f51eabSClaudiu Beznea pm_runtime_disable(&pdev->dev);
170c4f51eabSClaudiu Beznea pm_runtime_set_suspended(&pdev->dev);
171677d3e2fSPeter Korsgaard
172677d3e2fSPeter Korsgaard return 0;
173677d3e2fSPeter Korsgaard }
174677d3e2fSPeter Korsgaard
atmel_trng_runtime_suspend(struct device * dev)175c4f51eabSClaudiu Beznea static int __maybe_unused atmel_trng_runtime_suspend(struct device *dev)
176677d3e2fSPeter Korsgaard {
177677d3e2fSPeter Korsgaard struct atmel_trng *trng = dev_get_drvdata(dev);
178677d3e2fSPeter Korsgaard
179f14b0208SClaudiu Beznea atmel_trng_cleanup(trng);
180677d3e2fSPeter Korsgaard
181677d3e2fSPeter Korsgaard return 0;
182677d3e2fSPeter Korsgaard }
183677d3e2fSPeter Korsgaard
atmel_trng_runtime_resume(struct device * dev)184c4f51eabSClaudiu Beznea static int __maybe_unused atmel_trng_runtime_resume(struct device *dev)
185677d3e2fSPeter Korsgaard {
186677d3e2fSPeter Korsgaard struct atmel_trng *trng = dev_get_drvdata(dev);
187677d3e2fSPeter Korsgaard
1889fbd8b30SClaudiu Beznea return atmel_trng_init(trng);
189677d3e2fSPeter Korsgaard }
190677d3e2fSPeter Korsgaard
191b9531885SClaudiu Beznea static const struct dev_pm_ops __maybe_unused atmel_trng_pm_ops = {
192c4f51eabSClaudiu Beznea SET_RUNTIME_PM_OPS(atmel_trng_runtime_suspend,
193c4f51eabSClaudiu Beznea atmel_trng_runtime_resume, NULL)
194c4f51eabSClaudiu Beznea SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
195c4f51eabSClaudiu Beznea pm_runtime_force_resume)
196677d3e2fSPeter Korsgaard };
197677d3e2fSPeter Korsgaard
1985c49645cSCodrin Ciubotariu static const struct atmel_trng_data at91sam9g45_config = {
1995c49645cSCodrin Ciubotariu .has_half_rate = false,
2005c49645cSCodrin Ciubotariu };
2015c49645cSCodrin Ciubotariu
2025c49645cSCodrin Ciubotariu static const struct atmel_trng_data sam9x60_config = {
2035c49645cSCodrin Ciubotariu .has_half_rate = true,
2045c49645cSCodrin Ciubotariu };
2055c49645cSCodrin Ciubotariu
2064951db7eSBoris Brezillon static const struct of_device_id atmel_trng_dt_ids[] = {
2075c49645cSCodrin Ciubotariu {
2085c49645cSCodrin Ciubotariu .compatible = "atmel,at91sam9g45-trng",
2095c49645cSCodrin Ciubotariu .data = &at91sam9g45_config,
2105c49645cSCodrin Ciubotariu }, {
2115c49645cSCodrin Ciubotariu .compatible = "microchip,sam9x60-trng",
2125c49645cSCodrin Ciubotariu .data = &sam9x60_config,
2135c49645cSCodrin Ciubotariu }, {
2145c49645cSCodrin Ciubotariu /* sentinel */
2155c49645cSCodrin Ciubotariu }
2164951db7eSBoris Brezillon };
2174951db7eSBoris Brezillon MODULE_DEVICE_TABLE(of, atmel_trng_dt_ids);
2184951db7eSBoris Brezillon
219677d3e2fSPeter Korsgaard static struct platform_driver atmel_trng_driver = {
220677d3e2fSPeter Korsgaard .probe = atmel_trng_probe,
221bcd2982aSGreg Kroah-Hartman .remove = atmel_trng_remove,
222677d3e2fSPeter Korsgaard .driver = {
223677d3e2fSPeter Korsgaard .name = "atmel-trng",
224b9531885SClaudiu Beznea .pm = pm_ptr(&atmel_trng_pm_ops),
2254951db7eSBoris Brezillon .of_match_table = atmel_trng_dt_ids,
226677d3e2fSPeter Korsgaard },
227677d3e2fSPeter Korsgaard };
228677d3e2fSPeter Korsgaard
229b21cb324SAxel Lin module_platform_driver(atmel_trng_driver);
230677d3e2fSPeter Korsgaard
231677d3e2fSPeter Korsgaard MODULE_LICENSE("GPL");
232677d3e2fSPeter Korsgaard MODULE_AUTHOR("Peter Korsgaard <jacmet@sunsite.dk>");
233677d3e2fSPeter Korsgaard MODULE_DESCRIPTION("Atmel true random number generator driver");
234