• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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>
25 #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 
39 struct exynos_rng {
40 	struct device *dev;
41 	struct hwrng rng;
42 	void __iomem *mem;
43 	struct clk *clk;
44 };
45 
exynos_rng_readl(struct exynos_rng * rng,u32 offset)46 static u32 exynos_rng_readl(struct exynos_rng *rng, u32 offset)
47 {
48 	return	__raw_readl(rng->mem + offset);
49 }
50 
exynos_rng_writel(struct exynos_rng * rng,u32 val,u32 offset)51 static void exynos_rng_writel(struct exynos_rng *rng, u32 val, u32 offset)
52 {
53 	__raw_writel(val, rng->mem + offset);
54 }
55 
exynos_rng_configure(struct exynos_rng * exynos_rng)56 static int exynos_rng_configure(struct exynos_rng *exynos_rng)
57 {
58 	int i;
59 	int ret = 0;
60 
61 	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 
69 	return ret;
70 }
71 
exynos_init(struct hwrng * rng)72 static 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);
80 	pm_runtime_put_noidle(exynos_rng->dev);
81 
82 	return ret;
83 }
84 
exynos_read(struct hwrng * rng,void * buf,size_t max,bool wait)85 static int exynos_read(struct hwrng *rng, void *buf,
86 					size_t max, bool wait)
87 {
88 	struct exynos_rng *exynos_rng = container_of(rng,
89 						struct exynos_rng, rng);
90 	u32 *data = buf;
91 	int retry = 100;
92 	int ret = 4;
93 
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,
99 			EXYNOS_PRNG_STATUS_OFFSET) & PRNG_DONE) && --retry)
100 		cpu_relax();
101 	if (!retry) {
102 		ret = -ETIMEDOUT;
103 		goto out;
104 	}
105 
106 	exynos_rng_writel(exynos_rng, PRNG_DONE, EXYNOS_PRNG_STATUS_OFFSET);
107 
108 	*data = exynos_rng_readl(exynos_rng, EXYNOS_PRNG_OUT1_OFFSET);
109 
110 out:
111 	pm_runtime_mark_last_busy(exynos_rng->dev);
112 	pm_runtime_put_sync_autosuspend(exynos_rng->dev);
113 
114 	return ret;
115 }
116 
exynos_rng_probe(struct platform_device * pdev)117 static int exynos_rng_probe(struct platform_device *pdev)
118 {
119 	struct exynos_rng *exynos_rng;
120 	struct resource *res;
121 	int ret;
122 
123 	exynos_rng = devm_kzalloc(&pdev->dev, sizeof(struct exynos_rng),
124 					GFP_KERNEL);
125 	if (!exynos_rng)
126 		return -ENOMEM;
127 
128 	exynos_rng->dev = &pdev->dev;
129 	exynos_rng->rng.name = "exynos";
130 	exynos_rng->rng.init =	exynos_init;
131 	exynos_rng->rng.read = exynos_read;
132 	exynos_rng->clk = devm_clk_get(&pdev->dev, "secss");
133 	if (IS_ERR(exynos_rng->clk)) {
134 		dev_err(&pdev->dev, "Couldn't get clock.\n");
135 		return -ENOENT;
136 	}
137 
138 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
139 	exynos_rng->mem = devm_ioremap_resource(&pdev->dev, res);
140 	if (IS_ERR(exynos_rng->mem))
141 		return PTR_ERR(exynos_rng->mem);
142 
143 	platform_set_drvdata(pdev, exynos_rng);
144 
145 	pm_runtime_set_autosuspend_delay(&pdev->dev, EXYNOS_AUTOSUSPEND_DELAY);
146 	pm_runtime_use_autosuspend(&pdev->dev);
147 	pm_runtime_enable(&pdev->dev);
148 
149 	ret = devm_hwrng_register(&pdev->dev, &exynos_rng->rng);
150 	if (ret) {
151 		pm_runtime_dont_use_autosuspend(&pdev->dev);
152 		pm_runtime_disable(&pdev->dev);
153 	}
154 
155 	return ret;
156 }
157 
exynos_rng_remove(struct platform_device * pdev)158 static int exynos_rng_remove(struct platform_device *pdev)
159 {
160 	pm_runtime_dont_use_autosuspend(&pdev->dev);
161 	pm_runtime_disable(&pdev->dev);
162 
163 	return 0;
164 }
165 
exynos_rng_runtime_suspend(struct device * dev)166 static int __maybe_unused exynos_rng_runtime_suspend(struct device *dev)
167 {
168 	struct platform_device *pdev = to_platform_device(dev);
169 	struct exynos_rng *exynos_rng = platform_get_drvdata(pdev);
170 
171 	clk_disable_unprepare(exynos_rng->clk);
172 
173 	return 0;
174 }
175 
exynos_rng_runtime_resume(struct device * dev)176 static int __maybe_unused exynos_rng_runtime_resume(struct device *dev)
177 {
178 	struct platform_device *pdev = to_platform_device(dev);
179 	struct exynos_rng *exynos_rng = platform_get_drvdata(pdev);
180 
181 	return clk_prepare_enable(exynos_rng->clk);
182 }
183 
exynos_rng_suspend(struct device * dev)184 static int __maybe_unused exynos_rng_suspend(struct device *dev)
185 {
186 	return pm_runtime_force_suspend(dev);
187 }
188 
exynos_rng_resume(struct device * dev)189 static int __maybe_unused exynos_rng_resume(struct device *dev)
190 {
191 	struct platform_device *pdev = to_platform_device(dev);
192 	struct exynos_rng *exynos_rng = platform_get_drvdata(pdev);
193 	int ret;
194 
195 	ret = pm_runtime_force_resume(dev);
196 	if (ret)
197 		return ret;
198 
199 	return exynos_rng_configure(exynos_rng);
200 }
201 
202 static const struct dev_pm_ops exynos_rng_pm_ops = {
203 	SET_SYSTEM_SLEEP_PM_OPS(exynos_rng_suspend, exynos_rng_resume)
204 	SET_RUNTIME_PM_OPS(exynos_rng_runtime_suspend,
205 			   exynos_rng_runtime_resume, NULL)
206 };
207 
208 static const struct of_device_id exynos_rng_dt_match[] = {
209 	{
210 		.compatible = "samsung,exynos4-rng",
211 	},
212 	{ },
213 };
214 MODULE_DEVICE_TABLE(of, exynos_rng_dt_match);
215 
216 static struct platform_driver exynos_rng_driver = {
217 	.driver		= {
218 		.name	= "exynos-rng",
219 		.pm	= &exynos_rng_pm_ops,
220 		.of_match_table = exynos_rng_dt_match,
221 	},
222 	.probe		= exynos_rng_probe,
223 	.remove		= exynos_rng_remove,
224 };
225 
226 module_platform_driver(exynos_rng_driver);
227 
228 MODULE_DESCRIPTION("EXYNOS 4 H/W Random Number Generator driver");
229 MODULE_AUTHOR("Jonghwa Lee <jonghwa3.lee@samsung.com>");
230 MODULE_LICENSE("GPL");
231