blob: d1fd21e9936810808c0051e185608f2d88cb51e7 [file] [log] [blame]
Jonghwa Leeb3296692012-06-29 09:43:26 +09001/*
2 * exynos-rng.c - Random Number Generator driver for the exynos
3 *
4 * Copyright (C) 2012 Samsung Electronics
5 * Jonghwa Lee <jonghwa3.lee@smasung.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation;
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include <linux/hw_random.h>
23#include <linux/kernel.h>
24#include <linux/module.h>
Jonghwa Leeb3296692012-06-29 09:43:26 +090025#include <linux/io.h>
26#include <linux/platform_device.h>
27#include <linux/clk.h>
28#include <linux/pm_runtime.h>
29#include <linux/err.h>
30
31#define EXYNOS_PRNG_STATUS_OFFSET 0x10
32#define EXYNOS_PRNG_SEED_OFFSET 0x140
33#define EXYNOS_PRNG_OUT1_OFFSET 0x160
34#define SEED_SETTING_DONE BIT(1)
35#define PRNG_START 0x18
36#define PRNG_DONE BIT(5)
37#define EXYNOS_AUTOSUSPEND_DELAY 100
38
39struct exynos_rng {
40 struct device *dev;
41 struct hwrng rng;
42 void __iomem *mem;
43 struct clk *clk;
44};
45
46static u32 exynos_rng_readl(struct exynos_rng *rng, u32 offset)
47{
48 return __raw_readl(rng->mem + offset);
49}
50
51static void exynos_rng_writel(struct exynos_rng *rng, u32 val, u32 offset)
52{
53 __raw_writel(val, rng->mem + offset);
54}
55
Krzysztof Kozlowskibd1dffb2015-10-19 13:37:41 +090056static int exynos_rng_configure(struct exynos_rng *exynos_rng)
Jonghwa Leeb3296692012-06-29 09:43:26 +090057{
Jonghwa Leeb3296692012-06-29 09:43:26 +090058 int i;
59 int ret = 0;
60
Jonghwa Leeb3296692012-06-29 09:43:26 +090061 for (i = 0 ; i < 5 ; i++)
62 exynos_rng_writel(exynos_rng, jiffies,
63 EXYNOS_PRNG_SEED_OFFSET + 4*i);
64
65 if (!(exynos_rng_readl(exynos_rng, EXYNOS_PRNG_STATUS_OFFSET)
66 & SEED_SETTING_DONE))
67 ret = -EIO;
68
Krzysztof Kozlowskibd1dffb2015-10-19 13:37:41 +090069 return ret;
70}
71
72static int exynos_init(struct hwrng *rng)
73{
74 struct exynos_rng *exynos_rng = container_of(rng,
75 struct exynos_rng, rng);
76 int ret = 0;
77
78 pm_runtime_get_sync(exynos_rng->dev);
79 ret = exynos_rng_configure(exynos_rng);
Krzysztof Kozlowskif0d947a2016-03-14 09:07:12 +090080 pm_runtime_mark_last_busy(exynos_rng->dev);
81 pm_runtime_put_autosuspend(exynos_rng->dev);
Jonghwa Leeb3296692012-06-29 09:43:26 +090082
83 return ret;
84}
85
86static int exynos_read(struct hwrng *rng, void *buf,
87 size_t max, bool wait)
88{
89 struct exynos_rng *exynos_rng = container_of(rng,
90 struct exynos_rng, rng);
91 u32 *data = buf;
Krzysztof Kozlowskid7fd6072015-10-19 13:37:40 +090092 int retry = 100;
Jonghwa Leeb3296692012-06-29 09:43:26 +090093
94 pm_runtime_get_sync(exynos_rng->dev);
95
96 exynos_rng_writel(exynos_rng, PRNG_START, 0);
97
98 while (!(exynos_rng_readl(exynos_rng,
Krzysztof Kozlowskid7fd6072015-10-19 13:37:40 +090099 EXYNOS_PRNG_STATUS_OFFSET) & PRNG_DONE) && --retry)
Jonghwa Leeb3296692012-06-29 09:43:26 +0900100 cpu_relax();
Krzysztof Kozlowskid7fd6072015-10-19 13:37:40 +0900101 if (!retry)
102 return -ETIMEDOUT;
Jonghwa Leeb3296692012-06-29 09:43:26 +0900103
104 exynos_rng_writel(exynos_rng, PRNG_DONE, EXYNOS_PRNG_STATUS_OFFSET);
105
106 *data = exynos_rng_readl(exynos_rng, EXYNOS_PRNG_OUT1_OFFSET);
107
108 pm_runtime_mark_last_busy(exynos_rng->dev);
Daniel Thompsonf02b7d02015-10-16 17:01:51 +0100109 pm_runtime_put_sync_autosuspend(exynos_rng->dev);
Jonghwa Leeb3296692012-06-29 09:43:26 +0900110
111 return 4;
112}
113
Greg Kroah-Hartmanbcd29822012-12-21 15:12:08 -0800114static int exynos_rng_probe(struct platform_device *pdev)
Jonghwa Leeb3296692012-06-29 09:43:26 +0900115{
116 struct exynos_rng *exynos_rng;
Thierry Redingc7c9e1c2013-01-21 11:08:59 +0100117 struct resource *res;
Jonghwa Leeb3296692012-06-29 09:43:26 +0900118
119 exynos_rng = devm_kzalloc(&pdev->dev, sizeof(struct exynos_rng),
120 GFP_KERNEL);
121 if (!exynos_rng)
122 return -ENOMEM;
123
124 exynos_rng->dev = &pdev->dev;
125 exynos_rng->rng.name = "exynos";
126 exynos_rng->rng.init = exynos_init;
127 exynos_rng->rng.read = exynos_read;
128 exynos_rng->clk = devm_clk_get(&pdev->dev, "secss");
129 if (IS_ERR(exynos_rng->clk)) {
130 dev_err(&pdev->dev, "Couldn't get clock.\n");
131 return -ENOENT;
132 }
133
Thierry Redingc7c9e1c2013-01-21 11:08:59 +0100134 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
135 exynos_rng->mem = devm_ioremap_resource(&pdev->dev, res);
136 if (IS_ERR(exynos_rng->mem))
137 return PTR_ERR(exynos_rng->mem);
Jonghwa Leeb3296692012-06-29 09:43:26 +0900138
139 platform_set_drvdata(pdev, exynos_rng);
140
141 pm_runtime_set_autosuspend_delay(&pdev->dev, EXYNOS_AUTOSUSPEND_DELAY);
142 pm_runtime_use_autosuspend(&pdev->dev);
143 pm_runtime_enable(&pdev->dev);
144
Dmitry Torokhov1e6e38a2015-03-12 14:00:04 -0700145 return devm_hwrng_register(&pdev->dev, &exynos_rng->rng);
Jonghwa Leeb3296692012-06-29 09:43:26 +0900146}
147
Arnd Bergmannb93f3422016-03-02 16:58:59 +0100148static int __maybe_unused exynos_rng_runtime_suspend(struct device *dev)
Jonghwa Leeb3296692012-06-29 09:43:26 +0900149{
150 struct platform_device *pdev = to_platform_device(dev);
151 struct exynos_rng *exynos_rng = platform_get_drvdata(pdev);
152
153 clk_disable_unprepare(exynos_rng->clk);
154
155 return 0;
156}
157
Arnd Bergmannb93f3422016-03-02 16:58:59 +0100158static int __maybe_unused exynos_rng_runtime_resume(struct device *dev)
Jonghwa Leeb3296692012-06-29 09:43:26 +0900159{
160 struct platform_device *pdev = to_platform_device(dev);
161 struct exynos_rng *exynos_rng = platform_get_drvdata(pdev);
162
163 return clk_prepare_enable(exynos_rng->clk);
164}
Krzysztof Kozlowskibd1dffb2015-10-19 13:37:41 +0900165
Arnd Bergmannb93f3422016-03-02 16:58:59 +0100166static int __maybe_unused exynos_rng_suspend(struct device *dev)
Krzysztof Kozlowskibd1dffb2015-10-19 13:37:41 +0900167{
168 return pm_runtime_force_suspend(dev);
169}
170
Arnd Bergmannb93f3422016-03-02 16:58:59 +0100171static int __maybe_unused exynos_rng_resume(struct device *dev)
Krzysztof Kozlowskibd1dffb2015-10-19 13:37:41 +0900172{
173 struct platform_device *pdev = to_platform_device(dev);
174 struct exynos_rng *exynos_rng = platform_get_drvdata(pdev);
175 int ret;
176
177 ret = pm_runtime_force_resume(dev);
178 if (ret)
179 return ret;
180
181 return exynos_rng_configure(exynos_rng);
182}
Jonghwa Leeb3296692012-06-29 09:43:26 +0900183
Krzysztof Kozlowskibd1dffb2015-10-19 13:37:41 +0900184static const struct dev_pm_ops exynos_rng_pm_ops = {
185 SET_SYSTEM_SLEEP_PM_OPS(exynos_rng_suspend, exynos_rng_resume)
186 SET_RUNTIME_PM_OPS(exynos_rng_runtime_suspend,
187 exynos_rng_runtime_resume, NULL)
188};
Jonghwa Leeb3296692012-06-29 09:43:26 +0900189
Krzysztof Kozlowski642c1172015-10-19 13:37:42 +0900190static const struct of_device_id exynos_rng_dt_match[] = {
191 {
192 .compatible = "samsung,exynos4-rng",
193 },
194 { },
195};
196MODULE_DEVICE_TABLE(of, exynos_rng_dt_match);
197
Jonghwa Leeb3296692012-06-29 09:43:26 +0900198static struct platform_driver exynos_rng_driver = {
199 .driver = {
200 .name = "exynos-rng",
Jonghwa Leeb3296692012-06-29 09:43:26 +0900201 .pm = &exynos_rng_pm_ops,
Krzysztof Kozlowski642c1172015-10-19 13:37:42 +0900202 .of_match_table = exynos_rng_dt_match,
Jonghwa Leeb3296692012-06-29 09:43:26 +0900203 },
204 .probe = exynos_rng_probe,
Jonghwa Leeb3296692012-06-29 09:43:26 +0900205};
206
207module_platform_driver(exynos_rng_driver);
208
209MODULE_DESCRIPTION("EXYNOS 4 H/W Random Number Generator driver");
210MODULE_AUTHOR("Jonghwa Lee <jonghwa3.lee@samsung.com>");
211MODULE_LICENSE("GPL");