2 * exynos-rng.c - Random Number Generator driver for the exynos
4 * Copyright (C) 2012 Samsung Electronics
5 * Jonghwa Lee <jonghwa3.lee@smasung.com>
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;
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.
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
22 #include <linux/hw_random.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
26 #include <linux/platform_device.h>
27 #include <linux/clk.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/err.h>
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
46 static u32
exynos_rng_readl(struct exynos_rng
*rng
, u32 offset
)
48 return __raw_readl(rng
->mem
+ offset
);
51 static void exynos_rng_writel(struct exynos_rng
*rng
, u32 val
, u32 offset
)
53 __raw_writel(val
, rng
->mem
+ offset
);
56 static int exynos_init(struct hwrng
*rng
)
58 struct exynos_rng
*exynos_rng
= container_of(rng
,
59 struct exynos_rng
, rng
);
63 pm_runtime_get_sync(exynos_rng
->dev
);
65 for (i
= 0 ; i
< 5 ; i
++)
66 exynos_rng_writel(exynos_rng
, jiffies
,
67 EXYNOS_PRNG_SEED_OFFSET
+ 4*i
);
69 if (!(exynos_rng_readl(exynos_rng
, EXYNOS_PRNG_STATUS_OFFSET
)
73 pm_runtime_put_noidle(exynos_rng
->dev
);
78 static int exynos_read(struct hwrng
*rng
, void *buf
,
79 size_t max
, bool wait
)
81 struct exynos_rng
*exynos_rng
= container_of(rng
,
82 struct exynos_rng
, rng
);
85 pm_runtime_get_sync(exynos_rng
->dev
);
87 exynos_rng_writel(exynos_rng
, PRNG_START
, 0);
89 while (!(exynos_rng_readl(exynos_rng
,
90 EXYNOS_PRNG_STATUS_OFFSET
) & PRNG_DONE
))
93 exynos_rng_writel(exynos_rng
, PRNG_DONE
, EXYNOS_PRNG_STATUS_OFFSET
);
95 *data
= exynos_rng_readl(exynos_rng
, EXYNOS_PRNG_OUT1_OFFSET
);
97 pm_runtime_mark_last_busy(exynos_rng
->dev
);
98 pm_runtime_autosuspend(exynos_rng
->dev
);
103 static int exynos_rng_probe(struct platform_device
*pdev
)
105 struct exynos_rng
*exynos_rng
;
106 struct resource
*res
;
108 exynos_rng
= devm_kzalloc(&pdev
->dev
, sizeof(struct exynos_rng
),
113 exynos_rng
->dev
= &pdev
->dev
;
114 exynos_rng
->rng
.name
= "exynos";
115 exynos_rng
->rng
.init
= exynos_init
;
116 exynos_rng
->rng
.read
= exynos_read
;
117 exynos_rng
->clk
= devm_clk_get(&pdev
->dev
, "secss");
118 if (IS_ERR(exynos_rng
->clk
)) {
119 dev_err(&pdev
->dev
, "Couldn't get clock.\n");
123 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
124 exynos_rng
->mem
= devm_ioremap_resource(&pdev
->dev
, res
);
125 if (IS_ERR(exynos_rng
->mem
))
126 return PTR_ERR(exynos_rng
->mem
);
128 platform_set_drvdata(pdev
, exynos_rng
);
130 pm_runtime_set_autosuspend_delay(&pdev
->dev
, EXYNOS_AUTOSUSPEND_DELAY
);
131 pm_runtime_use_autosuspend(&pdev
->dev
);
132 pm_runtime_enable(&pdev
->dev
);
134 return devm_hwrng_register(&pdev
->dev
, &exynos_rng
->rng
);
138 static int exynos_rng_runtime_suspend(struct device
*dev
)
140 struct platform_device
*pdev
= to_platform_device(dev
);
141 struct exynos_rng
*exynos_rng
= platform_get_drvdata(pdev
);
143 clk_disable_unprepare(exynos_rng
->clk
);
148 static int exynos_rng_runtime_resume(struct device
*dev
)
150 struct platform_device
*pdev
= to_platform_device(dev
);
151 struct exynos_rng
*exynos_rng
= platform_get_drvdata(pdev
);
153 return clk_prepare_enable(exynos_rng
->clk
);
157 static UNIVERSAL_DEV_PM_OPS(exynos_rng_pm_ops
, exynos_rng_runtime_suspend
,
158 exynos_rng_runtime_resume
, NULL
);
160 static struct platform_driver exynos_rng_driver
= {
162 .name
= "exynos-rng",
163 .pm
= &exynos_rng_pm_ops
,
165 .probe
= exynos_rng_probe
,
168 module_platform_driver(exynos_rng_driver
);
170 MODULE_DESCRIPTION("EXYNOS 4 H/W Random Number Generator driver");
171 MODULE_AUTHOR("Jonghwa Lee <jonghwa3.lee@samsung.com>");
172 MODULE_LICENSE("GPL");