1c9842929STomer Maimon // SPDX-License-Identifier: GPL-2.0
2c9842929STomer Maimon // Copyright (c) 2019 Nuvoton Technology corporation.
3c9842929STomer Maimon
4c9842929STomer Maimon #include <linux/kernel.h>
5c9842929STomer Maimon #include <linux/module.h>
6c9842929STomer Maimon #include <linux/io.h>
7c9842929STomer Maimon #include <linux/iopoll.h>
8c9842929STomer Maimon #include <linux/init.h>
9c9842929STomer Maimon #include <linux/random.h>
10c9842929STomer Maimon #include <linux/err.h>
11*0788257aSRob Herring #include <linux/of.h>
12c9842929STomer Maimon #include <linux/platform_device.h>
13c9842929STomer Maimon #include <linux/hw_random.h>
14c9842929STomer Maimon #include <linux/delay.h>
15c9842929STomer Maimon #include <linux/pm_runtime.h>
16c9842929STomer Maimon
17c9842929STomer Maimon #define NPCM_RNGCS_REG 0x00 /* Control and status register */
18c9842929STomer Maimon #define NPCM_RNGD_REG 0x04 /* Data register */
19c9842929STomer Maimon #define NPCM_RNGMODE_REG 0x08 /* Mode register */
20c9842929STomer Maimon
21f07b3e87STomer Maimon #define NPCM_RNG_CLK_SET_62_5MHZ BIT(2) /* 60-80 MHz */
22c9842929STomer Maimon #define NPCM_RNG_CLK_SET_25MHZ GENMASK(4, 3) /* 20-25 MHz */
23c9842929STomer Maimon #define NPCM_RNG_DATA_VALID BIT(1)
24c9842929STomer Maimon #define NPCM_RNG_ENABLE BIT(0)
25c9842929STomer Maimon #define NPCM_RNG_M1ROSEL BIT(1)
26c9842929STomer Maimon
27c9842929STomer Maimon #define NPCM_RNG_TIMEOUT_USEC 20000
28c9842929STomer Maimon #define NPCM_RNG_POLL_USEC 1000
29c9842929STomer Maimon
30c9842929STomer Maimon #define to_npcm_rng(p) container_of(p, struct npcm_rng, rng)
31c9842929STomer Maimon
32c9842929STomer Maimon struct npcm_rng {
33c9842929STomer Maimon void __iomem *base;
34c9842929STomer Maimon struct hwrng rng;
35f07b3e87STomer Maimon u32 clkp;
36c9842929STomer Maimon };
37c9842929STomer Maimon
npcm_rng_init(struct hwrng * rng)38c9842929STomer Maimon static int npcm_rng_init(struct hwrng *rng)
39c9842929STomer Maimon {
40c9842929STomer Maimon struct npcm_rng *priv = to_npcm_rng(rng);
41c9842929STomer Maimon
42f07b3e87STomer Maimon writel(priv->clkp | NPCM_RNG_ENABLE, priv->base + NPCM_RNGCS_REG);
43c9842929STomer Maimon
44c9842929STomer Maimon return 0;
45c9842929STomer Maimon }
46c9842929STomer Maimon
npcm_rng_cleanup(struct hwrng * rng)47c9842929STomer Maimon static void npcm_rng_cleanup(struct hwrng *rng)
48c9842929STomer Maimon {
49c9842929STomer Maimon struct npcm_rng *priv = to_npcm_rng(rng);
50c9842929STomer Maimon
51f07b3e87STomer Maimon writel(priv->clkp, priv->base + NPCM_RNGCS_REG);
52c9842929STomer Maimon }
53c9842929STomer Maimon
npcm_rng_read(struct hwrng * rng,void * buf,size_t max,bool wait)54c9842929STomer Maimon static int npcm_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
55c9842929STomer Maimon {
56c9842929STomer Maimon struct npcm_rng *priv = to_npcm_rng(rng);
57c9842929STomer Maimon int retval = 0;
58c9842929STomer Maimon int ready;
59c9842929STomer Maimon
60c9842929STomer Maimon pm_runtime_get_sync((struct device *)priv->rng.priv);
61c9842929STomer Maimon
62c2fb6446STomer Maimon while (max) {
63c9842929STomer Maimon if (wait) {
64c2fb6446STomer Maimon if (readb_poll_timeout(priv->base + NPCM_RNGCS_REG,
65c9842929STomer Maimon ready,
66c9842929STomer Maimon ready & NPCM_RNG_DATA_VALID,
67c9842929STomer Maimon NPCM_RNG_POLL_USEC,
68c9842929STomer Maimon NPCM_RNG_TIMEOUT_USEC))
69c9842929STomer Maimon break;
70c9842929STomer Maimon } else {
71c2fb6446STomer Maimon if ((readb(priv->base + NPCM_RNGCS_REG) &
72c9842929STomer Maimon NPCM_RNG_DATA_VALID) == 0)
73c9842929STomer Maimon break;
74c9842929STomer Maimon }
75c9842929STomer Maimon
76c2fb6446STomer Maimon *(u8 *)buf = readb(priv->base + NPCM_RNGD_REG);
77c2fb6446STomer Maimon retval++;
78c2fb6446STomer Maimon buf++;
79c2fb6446STomer Maimon max--;
80c9842929STomer Maimon }
81c9842929STomer Maimon
82c9842929STomer Maimon pm_runtime_mark_last_busy((struct device *)priv->rng.priv);
83c9842929STomer Maimon pm_runtime_put_sync_autosuspend((struct device *)priv->rng.priv);
84c9842929STomer Maimon
85c9842929STomer Maimon return retval || !wait ? retval : -EIO;
86c9842929STomer Maimon }
87c9842929STomer Maimon
npcm_rng_probe(struct platform_device * pdev)88c9842929STomer Maimon static int npcm_rng_probe(struct platform_device *pdev)
89c9842929STomer Maimon {
90c9842929STomer Maimon struct npcm_rng *priv;
91c9842929STomer Maimon int ret;
92c9842929STomer Maimon
93c9842929STomer Maimon priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
94c9842929STomer Maimon if (!priv)
95c9842929STomer Maimon return -ENOMEM;
96c9842929STomer Maimon
97fc963e02SYueHaibing priv->base = devm_platform_ioremap_resource(pdev, 0);
98c9842929STomer Maimon if (IS_ERR(priv->base))
99c9842929STomer Maimon return PTR_ERR(priv->base);
100c9842929STomer Maimon
101c9842929STomer Maimon dev_set_drvdata(&pdev->dev, priv);
102c9842929STomer Maimon pm_runtime_set_autosuspend_delay(&pdev->dev, 100);
103c9842929STomer Maimon pm_runtime_use_autosuspend(&pdev->dev);
104c9842929STomer Maimon pm_runtime_enable(&pdev->dev);
105c9842929STomer Maimon
106c9842929STomer Maimon #ifndef CONFIG_PM
107c9842929STomer Maimon priv->rng.init = npcm_rng_init;
108c9842929STomer Maimon priv->rng.cleanup = npcm_rng_cleanup;
109c9842929STomer Maimon #endif
110c9842929STomer Maimon priv->rng.name = pdev->name;
111c9842929STomer Maimon priv->rng.read = npcm_rng_read;
112c9842929STomer Maimon priv->rng.priv = (unsigned long)&pdev->dev;
113f07b3e87STomer Maimon priv->clkp = (u32)(uintptr_t)of_device_get_match_data(&pdev->dev);
114c9842929STomer Maimon
115c9842929STomer Maimon writel(NPCM_RNG_M1ROSEL, priv->base + NPCM_RNGMODE_REG);
116c9842929STomer Maimon
117c9842929STomer Maimon ret = devm_hwrng_register(&pdev->dev, &priv->rng);
118c9842929STomer Maimon if (ret) {
119c9842929STomer Maimon dev_err(&pdev->dev, "Failed to register rng device: %d\n",
120c9842929STomer Maimon ret);
121c9842929STomer Maimon pm_runtime_disable(&pdev->dev);
122c9842929STomer Maimon pm_runtime_set_suspended(&pdev->dev);
123c9842929STomer Maimon return ret;
124c9842929STomer Maimon }
125c9842929STomer Maimon
126c9842929STomer Maimon return 0;
127c9842929STomer Maimon }
128c9842929STomer Maimon
npcm_rng_remove(struct platform_device * pdev)129c9842929STomer Maimon static int npcm_rng_remove(struct platform_device *pdev)
130c9842929STomer Maimon {
131c9842929STomer Maimon struct npcm_rng *priv = platform_get_drvdata(pdev);
132c9842929STomer Maimon
133c9842929STomer Maimon devm_hwrng_unregister(&pdev->dev, &priv->rng);
134c9842929STomer Maimon pm_runtime_disable(&pdev->dev);
135c9842929STomer Maimon pm_runtime_set_suspended(&pdev->dev);
136c9842929STomer Maimon
137c9842929STomer Maimon return 0;
138c9842929STomer Maimon }
139c9842929STomer Maimon
140c9842929STomer Maimon #ifdef CONFIG_PM
npcm_rng_runtime_suspend(struct device * dev)141c9842929STomer Maimon static int npcm_rng_runtime_suspend(struct device *dev)
142c9842929STomer Maimon {
143c9842929STomer Maimon struct npcm_rng *priv = dev_get_drvdata(dev);
144c9842929STomer Maimon
145c9842929STomer Maimon npcm_rng_cleanup(&priv->rng);
146c9842929STomer Maimon
147c9842929STomer Maimon return 0;
148c9842929STomer Maimon }
149c9842929STomer Maimon
npcm_rng_runtime_resume(struct device * dev)150c9842929STomer Maimon static int npcm_rng_runtime_resume(struct device *dev)
151c9842929STomer Maimon {
152c9842929STomer Maimon struct npcm_rng *priv = dev_get_drvdata(dev);
153c9842929STomer Maimon
154c9842929STomer Maimon return npcm_rng_init(&priv->rng);
155c9842929STomer Maimon }
156c9842929STomer Maimon #endif
157c9842929STomer Maimon
158c9842929STomer Maimon static const struct dev_pm_ops npcm_rng_pm_ops = {
159c9842929STomer Maimon SET_RUNTIME_PM_OPS(npcm_rng_runtime_suspend,
160c9842929STomer Maimon npcm_rng_runtime_resume, NULL)
161c9842929STomer Maimon SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
162c9842929STomer Maimon pm_runtime_force_resume)
163c9842929STomer Maimon };
164c9842929STomer Maimon
1659a150af0SHerbert Xu static const struct of_device_id rng_dt_id[] __maybe_unused = {
166f07b3e87STomer Maimon { .compatible = "nuvoton,npcm750-rng",
167f07b3e87STomer Maimon .data = (void *)NPCM_RNG_CLK_SET_25MHZ },
168f07b3e87STomer Maimon { .compatible = "nuvoton,npcm845-rng",
169f07b3e87STomer Maimon .data = (void *)NPCM_RNG_CLK_SET_62_5MHZ },
170c9842929STomer Maimon {},
171c9842929STomer Maimon };
172c9842929STomer Maimon MODULE_DEVICE_TABLE(of, rng_dt_id);
173c9842929STomer Maimon
174c9842929STomer Maimon static struct platform_driver npcm_rng_driver = {
175c9842929STomer Maimon .driver = {
176c9842929STomer Maimon .name = "npcm-rng",
177c9842929STomer Maimon .pm = &npcm_rng_pm_ops,
178c9842929STomer Maimon .of_match_table = of_match_ptr(rng_dt_id),
179c9842929STomer Maimon },
180c9842929STomer Maimon .probe = npcm_rng_probe,
181c9842929STomer Maimon .remove = npcm_rng_remove,
182c9842929STomer Maimon };
183c9842929STomer Maimon
184c9842929STomer Maimon module_platform_driver(npcm_rng_driver);
185c9842929STomer Maimon
186c9842929STomer Maimon MODULE_DESCRIPTION("Nuvoton NPCM Random Number Generator Driver");
187c9842929STomer Maimon MODULE_AUTHOR("Tomer Maimon <tomer.maimon@nuvoton.com>");
188c9842929STomer Maimon MODULE_LICENSE("GPL v2");
189