blob: c117463642e34de73372e0e837c45f4289a8c90a [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
56static int exynos_init(struct hwrng *rng)
57{
58 struct exynos_rng *exynos_rng = container_of(rng,
59 struct exynos_rng, rng);
60 int i;
61 int ret = 0;
62
63 pm_runtime_get_sync(exynos_rng->dev);
64
65 for (i = 0 ; i < 5 ; i++)
66 exynos_rng_writel(exynos_rng, jiffies,
67 EXYNOS_PRNG_SEED_OFFSET + 4*i);
68
69 if (!(exynos_rng_readl(exynos_rng, EXYNOS_PRNG_STATUS_OFFSET)
70 & SEED_SETTING_DONE))
71 ret = -EIO;
72
73 pm_runtime_put_noidle(exynos_rng->dev);
74
75 return ret;
76}
77
78static int exynos_read(struct hwrng *rng, void *buf,
79 size_t max, bool wait)
80{
81 struct exynos_rng *exynos_rng = container_of(rng,
82 struct exynos_rng, rng);
83 u32 *data = buf;
Krzysztof Kozlowskid7fd6072015-10-19 13:37:40 +090084 int retry = 100;
Jonghwa Leeb3296692012-06-29 09:43:26 +090085
86 pm_runtime_get_sync(exynos_rng->dev);
87
88 exynos_rng_writel(exynos_rng, PRNG_START, 0);
89
90 while (!(exynos_rng_readl(exynos_rng,
Krzysztof Kozlowskid7fd6072015-10-19 13:37:40 +090091 EXYNOS_PRNG_STATUS_OFFSET) & PRNG_DONE) && --retry)
Jonghwa Leeb3296692012-06-29 09:43:26 +090092 cpu_relax();
Krzysztof Kozlowskid7fd6072015-10-19 13:37:40 +090093 if (!retry)
94 return -ETIMEDOUT;
Jonghwa Leeb3296692012-06-29 09:43:26 +090095
96 exynos_rng_writel(exynos_rng, PRNG_DONE, EXYNOS_PRNG_STATUS_OFFSET);
97
98 *data = exynos_rng_readl(exynos_rng, EXYNOS_PRNG_OUT1_OFFSET);
99
100 pm_runtime_mark_last_busy(exynos_rng->dev);
Daniel Thompsonf02b7d02015-10-16 17:01:51 +0100101 pm_runtime_put_sync_autosuspend(exynos_rng->dev);
Jonghwa Leeb3296692012-06-29 09:43:26 +0900102
103 return 4;
104}
105
Greg Kroah-Hartmanbcd29822012-12-21 15:12:08 -0800106static int exynos_rng_probe(struct platform_device *pdev)
Jonghwa Leeb3296692012-06-29 09:43:26 +0900107{
108 struct exynos_rng *exynos_rng;
Thierry Redingc7c9e1c2013-01-21 11:08:59 +0100109 struct resource *res;
Jonghwa Leeb3296692012-06-29 09:43:26 +0900110
111 exynos_rng = devm_kzalloc(&pdev->dev, sizeof(struct exynos_rng),
112 GFP_KERNEL);
113 if (!exynos_rng)
114 return -ENOMEM;
115
116 exynos_rng->dev = &pdev->dev;
117 exynos_rng->rng.name = "exynos";
118 exynos_rng->rng.init = exynos_init;
119 exynos_rng->rng.read = exynos_read;
120 exynos_rng->clk = devm_clk_get(&pdev->dev, "secss");
121 if (IS_ERR(exynos_rng->clk)) {
122 dev_err(&pdev->dev, "Couldn't get clock.\n");
123 return -ENOENT;
124 }
125
Thierry Redingc7c9e1c2013-01-21 11:08:59 +0100126 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
127 exynos_rng->mem = devm_ioremap_resource(&pdev->dev, res);
128 if (IS_ERR(exynos_rng->mem))
129 return PTR_ERR(exynos_rng->mem);
Jonghwa Leeb3296692012-06-29 09:43:26 +0900130
131 platform_set_drvdata(pdev, exynos_rng);
132
133 pm_runtime_set_autosuspend_delay(&pdev->dev, EXYNOS_AUTOSUSPEND_DELAY);
134 pm_runtime_use_autosuspend(&pdev->dev);
135 pm_runtime_enable(&pdev->dev);
136
Dmitry Torokhov1e6e38a2015-03-12 14:00:04 -0700137 return devm_hwrng_register(&pdev->dev, &exynos_rng->rng);
Jonghwa Leeb3296692012-06-29 09:43:26 +0900138}
139
Rafael J. Wysocki2d3867d2014-12-04 01:02:18 +0100140#ifdef CONFIG_PM
Jonghwa Leeb3296692012-06-29 09:43:26 +0900141static int exynos_rng_runtime_suspend(struct device *dev)
142{
143 struct platform_device *pdev = to_platform_device(dev);
144 struct exynos_rng *exynos_rng = platform_get_drvdata(pdev);
145
146 clk_disable_unprepare(exynos_rng->clk);
147
148 return 0;
149}
150
151static int exynos_rng_runtime_resume(struct device *dev)
152{
153 struct platform_device *pdev = to_platform_device(dev);
154 struct exynos_rng *exynos_rng = platform_get_drvdata(pdev);
155
156 return clk_prepare_enable(exynos_rng->clk);
157}
Jingoo Hana80c5422013-03-12 14:46:22 +0900158#endif
Jonghwa Leeb3296692012-06-29 09:43:26 +0900159
Fengguang Wuef4458a2013-01-19 23:37:19 +0800160static UNIVERSAL_DEV_PM_OPS(exynos_rng_pm_ops, exynos_rng_runtime_suspend,
Jonghwa Leeb3296692012-06-29 09:43:26 +0900161 exynos_rng_runtime_resume, NULL);
162
163static struct platform_driver exynos_rng_driver = {
164 .driver = {
165 .name = "exynos-rng",
Jonghwa Leeb3296692012-06-29 09:43:26 +0900166 .pm = &exynos_rng_pm_ops,
167 },
168 .probe = exynos_rng_probe,
Jonghwa Leeb3296692012-06-29 09:43:26 +0900169};
170
171module_platform_driver(exynos_rng_driver);
172
173MODULE_DESCRIPTION("EXYNOS 4 H/W Random Number Generator driver");
174MODULE_AUTHOR("Jonghwa Lee <jonghwa3.lee@samsung.com>");
175MODULE_LICENSE("GPL");