blob: 825c09619eb8b1f100eda1ef744877a81834d8e8 [file] [log] [blame]
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +02001/*
2 * exynos-rng.c - Random Number Generator driver for the Exynos
3 *
4 * Copyright (c) 2017 Krzysztof Kozlowski <krzk@kernel.org>
5 *
6 * Loosely based on old driver from drivers/char/hw_random/exynos-rng.c:
7 * Copyright (C) 2012 Samsung Electronics
8 * Jonghwa Lee <jonghwa3.lee@samsung.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation;
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 */
19
20#include <linux/clk.h>
21#include <linux/crypto.h>
22#include <linux/err.h>
23#include <linux/io.h>
24#include <linux/module.h>
Łukasz Stelmachf76d38b2017-12-12 17:36:04 +010025#include <linux/of_device.h>
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +020026#include <linux/platform_device.h>
27
28#include <crypto/internal/rng.h>
29
30#define EXYNOS_RNG_CONTROL 0x0
31#define EXYNOS_RNG_STATUS 0x10
Łukasz Stelmachf76d38b2017-12-12 17:36:04 +010032
33#define EXYNOS_RNG_SEED_CONF 0x14
34#define EXYNOS_RNG_GEN_PRNG BIT(1)
35
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +020036#define EXYNOS_RNG_SEED_BASE 0x140
37#define EXYNOS_RNG_SEED(n) (EXYNOS_RNG_SEED_BASE + (n * 0x4))
38#define EXYNOS_RNG_OUT_BASE 0x160
39#define EXYNOS_RNG_OUT(n) (EXYNOS_RNG_OUT_BASE + (n * 0x4))
40
41/* EXYNOS_RNG_CONTROL bit fields */
42#define EXYNOS_RNG_CONTROL_START 0x18
43/* EXYNOS_RNG_STATUS bit fields */
44#define EXYNOS_RNG_STATUS_SEED_SETTING_DONE BIT(1)
45#define EXYNOS_RNG_STATUS_RNG_DONE BIT(5)
46
47/* Five seed and output registers, each 4 bytes */
48#define EXYNOS_RNG_SEED_REGS 5
49#define EXYNOS_RNG_SEED_SIZE (EXYNOS_RNG_SEED_REGS * 4)
50
Łukasz Stelmachf76d38b2017-12-12 17:36:04 +010051enum exynos_prng_type {
52 EXYNOS_PRNG_UNKNOWN = 0,
53 EXYNOS_PRNG_EXYNOS4,
54 EXYNOS_PRNG_EXYNOS5,
55};
56
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +020057/*
58 * Driver re-seeds itself with generated random numbers to increase
59 * the randomness.
60 *
61 * Time for next re-seed in ms.
62 */
63#define EXYNOS_RNG_RESEED_TIME 100
64/*
65 * In polling mode, do not wait infinitely for the engine to finish the work.
66 */
67#define EXYNOS_RNG_WAIT_RETRIES 100
68
69/* Context for crypto */
70struct exynos_rng_ctx {
71 struct exynos_rng_dev *rng;
72};
73
74/* Device associated memory */
75struct exynos_rng_dev {
76 struct device *dev;
Łukasz Stelmachf76d38b2017-12-12 17:36:04 +010077 enum exynos_prng_type type;
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +020078 void __iomem *mem;
79 struct clk *clk;
80 /* Generated numbers stored for seeding during resume */
81 u8 seed_save[EXYNOS_RNG_SEED_SIZE];
82 unsigned int seed_save_len;
83 /* Time of last seeding in jiffies */
84 unsigned long last_seeding;
85};
86
87static struct exynos_rng_dev *exynos_rng_dev;
88
89static u32 exynos_rng_readl(struct exynos_rng_dev *rng, u32 offset)
90{
91 return readl_relaxed(rng->mem + offset);
92}
93
94static void exynos_rng_writel(struct exynos_rng_dev *rng, u32 val, u32 offset)
95{
96 writel_relaxed(val, rng->mem + offset);
97}
98
99static int exynos_rng_set_seed(struct exynos_rng_dev *rng,
100 const u8 *seed, unsigned int slen)
101{
102 u32 val;
103 int i;
104
105 /* Round seed length because loop iterates over full register size */
106 slen = ALIGN_DOWN(slen, 4);
107
108 if (slen < EXYNOS_RNG_SEED_SIZE)
109 return -EINVAL;
110
111 for (i = 0; i < slen ; i += 4) {
112 unsigned int seed_reg = (i / 4) % EXYNOS_RNG_SEED_REGS;
113
114 val = seed[i] << 24;
115 val |= seed[i + 1] << 16;
116 val |= seed[i + 2] << 8;
117 val |= seed[i + 3] << 0;
118
119 exynos_rng_writel(rng, val, EXYNOS_RNG_SEED(seed_reg));
120 }
121
122 val = exynos_rng_readl(rng, EXYNOS_RNG_STATUS);
123 if (!(val & EXYNOS_RNG_STATUS_SEED_SETTING_DONE)) {
124 dev_warn(rng->dev, "Seed setting not finished\n");
125 return -EIO;
126 }
127
128 rng->last_seeding = jiffies;
129
130 return 0;
131}
132
133/*
134 * Read from output registers and put the data under 'dst' array,
135 * up to dlen bytes.
136 *
137 * Returns number of bytes actually stored in 'dst' (dlen
138 * or EXYNOS_RNG_SEED_SIZE).
139 */
140static unsigned int exynos_rng_copy_random(struct exynos_rng_dev *rng,
141 u8 *dst, unsigned int dlen)
142{
143 unsigned int cnt = 0;
144 int i, j;
145 u32 val;
146
147 for (j = 0; j < EXYNOS_RNG_SEED_REGS; j++) {
148 val = exynos_rng_readl(rng, EXYNOS_RNG_OUT(j));
149
150 for (i = 0; i < 4; i++) {
151 dst[cnt] = val & 0xff;
152 val >>= 8;
153 if (++cnt >= dlen)
154 return cnt;
155 }
156 }
157
158 return cnt;
159}
160
161/*
162 * Start the engine and poll for finish. Then read from output registers
163 * filling the 'dst' buffer up to 'dlen' bytes or up to size of generated
164 * random data (EXYNOS_RNG_SEED_SIZE).
165 *
166 * On success: return 0 and store number of read bytes under 'read' address.
167 * On error: return -ERRNO.
168 */
169static int exynos_rng_get_random(struct exynos_rng_dev *rng,
170 u8 *dst, unsigned int dlen,
171 unsigned int *read)
172{
173 int retry = EXYNOS_RNG_WAIT_RETRIES;
174
Łukasz Stelmachf76d38b2017-12-12 17:36:04 +0100175 if (rng->type == EXYNOS_PRNG_EXYNOS4) {
176 exynos_rng_writel(rng, EXYNOS_RNG_CONTROL_START,
177 EXYNOS_RNG_CONTROL);
178 } else if (rng->type == EXYNOS_PRNG_EXYNOS5) {
179 exynos_rng_writel(rng, EXYNOS_RNG_GEN_PRNG,
180 EXYNOS_RNG_SEED_CONF);
181 }
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +0200182
183 while (!(exynos_rng_readl(rng,
184 EXYNOS_RNG_STATUS) & EXYNOS_RNG_STATUS_RNG_DONE) && --retry)
185 cpu_relax();
186
187 if (!retry)
188 return -ETIMEDOUT;
189
190 /* Clear status bit */
191 exynos_rng_writel(rng, EXYNOS_RNG_STATUS_RNG_DONE,
192 EXYNOS_RNG_STATUS);
193 *read = exynos_rng_copy_random(rng, dst, dlen);
194
195 return 0;
196}
197
198/* Re-seed itself from time to time */
199static void exynos_rng_reseed(struct exynos_rng_dev *rng)
200{
201 unsigned long next_seeding = rng->last_seeding + \
202 msecs_to_jiffies(EXYNOS_RNG_RESEED_TIME);
203 unsigned long now = jiffies;
204 unsigned int read = 0;
205 u8 seed[EXYNOS_RNG_SEED_SIZE];
206
207 if (time_before(now, next_seeding))
208 return;
209
210 if (exynos_rng_get_random(rng, seed, sizeof(seed), &read))
211 return;
212
213 exynos_rng_set_seed(rng, seed, read);
214}
215
216static int exynos_rng_generate(struct crypto_rng *tfm,
217 const u8 *src, unsigned int slen,
218 u8 *dst, unsigned int dlen)
219{
220 struct exynos_rng_ctx *ctx = crypto_rng_ctx(tfm);
221 struct exynos_rng_dev *rng = ctx->rng;
222 unsigned int read = 0;
223 int ret;
224
225 ret = clk_prepare_enable(rng->clk);
226 if (ret)
227 return ret;
228
229 do {
230 ret = exynos_rng_get_random(rng, dst, dlen, &read);
231 if (ret)
232 break;
233
234 dlen -= read;
235 dst += read;
236
237 exynos_rng_reseed(rng);
238 } while (dlen > 0);
239
240 clk_disable_unprepare(rng->clk);
241
242 return ret;
243}
244
245static int exynos_rng_seed(struct crypto_rng *tfm, const u8 *seed,
246 unsigned int slen)
247{
248 struct exynos_rng_ctx *ctx = crypto_rng_ctx(tfm);
249 struct exynos_rng_dev *rng = ctx->rng;
250 int ret;
251
252 ret = clk_prepare_enable(rng->clk);
253 if (ret)
254 return ret;
255
256 ret = exynos_rng_set_seed(ctx->rng, seed, slen);
257
258 clk_disable_unprepare(rng->clk);
259
260 return ret;
261}
262
263static int exynos_rng_kcapi_init(struct crypto_tfm *tfm)
264{
265 struct exynos_rng_ctx *ctx = crypto_tfm_ctx(tfm);
266
267 ctx->rng = exynos_rng_dev;
268
269 return 0;
270}
271
272static struct rng_alg exynos_rng_alg = {
273 .generate = exynos_rng_generate,
274 .seed = exynos_rng_seed,
275 .seedsize = EXYNOS_RNG_SEED_SIZE,
276 .base = {
277 .cra_name = "stdrng",
278 .cra_driver_name = "exynos_rng",
Łukasz Stelmach3fc12642017-12-05 17:20:46 +0100279 .cra_priority = 300,
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +0200280 .cra_ctxsize = sizeof(struct exynos_rng_ctx),
281 .cra_module = THIS_MODULE,
282 .cra_init = exynos_rng_kcapi_init,
283 }
284};
285
286static int exynos_rng_probe(struct platform_device *pdev)
287{
288 struct exynos_rng_dev *rng;
289 struct resource *res;
290 int ret;
291
292 if (exynos_rng_dev)
293 return -EEXIST;
294
295 rng = devm_kzalloc(&pdev->dev, sizeof(*rng), GFP_KERNEL);
296 if (!rng)
297 return -ENOMEM;
298
Łukasz Stelmachf76d38b2017-12-12 17:36:04 +0100299 rng->type = (enum exynos_prng_type)of_device_get_match_data(&pdev->dev);
300
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +0200301 rng->dev = &pdev->dev;
302 rng->clk = devm_clk_get(&pdev->dev, "secss");
303 if (IS_ERR(rng->clk)) {
304 dev_err(&pdev->dev, "Couldn't get clock.\n");
305 return PTR_ERR(rng->clk);
306 }
307
308 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
309 rng->mem = devm_ioremap_resource(&pdev->dev, res);
310 if (IS_ERR(rng->mem))
311 return PTR_ERR(rng->mem);
312
313 platform_set_drvdata(pdev, rng);
314
315 exynos_rng_dev = rng;
316
317 ret = crypto_register_rng(&exynos_rng_alg);
318 if (ret) {
319 dev_err(&pdev->dev,
320 "Couldn't register rng crypto alg: %d\n", ret);
321 exynos_rng_dev = NULL;
322 }
323
324 return ret;
325}
326
327static int exynos_rng_remove(struct platform_device *pdev)
328{
329 crypto_unregister_rng(&exynos_rng_alg);
330
331 exynos_rng_dev = NULL;
332
333 return 0;
334}
335
336static int __maybe_unused exynos_rng_suspend(struct device *dev)
337{
338 struct platform_device *pdev = to_platform_device(dev);
339 struct exynos_rng_dev *rng = platform_get_drvdata(pdev);
340 int ret;
341
342 /* If we were never seeded then after resume it will be the same */
343 if (!rng->last_seeding)
344 return 0;
345
346 rng->seed_save_len = 0;
347 ret = clk_prepare_enable(rng->clk);
348 if (ret)
349 return ret;
350
351 /* Get new random numbers and store them for seeding on resume. */
352 exynos_rng_get_random(rng, rng->seed_save, sizeof(rng->seed_save),
353 &(rng->seed_save_len));
354 dev_dbg(rng->dev, "Stored %u bytes for seeding on system resume\n",
355 rng->seed_save_len);
356
357 clk_disable_unprepare(rng->clk);
358
359 return 0;
360}
361
362static int __maybe_unused exynos_rng_resume(struct device *dev)
363{
364 struct platform_device *pdev = to_platform_device(dev);
365 struct exynos_rng_dev *rng = platform_get_drvdata(pdev);
366 int ret;
367
368 /* Never seeded so nothing to do */
369 if (!rng->last_seeding)
370 return 0;
371
372 ret = clk_prepare_enable(rng->clk);
373 if (ret)
374 return ret;
375
376 ret = exynos_rng_set_seed(rng, rng->seed_save, rng->seed_save_len);
377
378 clk_disable_unprepare(rng->clk);
379
380 return ret;
381}
382
383static SIMPLE_DEV_PM_OPS(exynos_rng_pm_ops, exynos_rng_suspend,
384 exynos_rng_resume);
385
386static const struct of_device_id exynos_rng_dt_match[] = {
387 {
388 .compatible = "samsung,exynos4-rng",
Łukasz Stelmachf76d38b2017-12-12 17:36:04 +0100389 .data = (const void *)EXYNOS_PRNG_EXYNOS4,
390 }, {
391 .compatible = "samsung,exynos5250-prng",
392 .data = (const void *)EXYNOS_PRNG_EXYNOS5,
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +0200393 },
394 { },
395};
396MODULE_DEVICE_TABLE(of, exynos_rng_dt_match);
397
398static struct platform_driver exynos_rng_driver = {
399 .driver = {
400 .name = "exynos-rng",
401 .pm = &exynos_rng_pm_ops,
402 .of_match_table = exynos_rng_dt_match,
403 },
404 .probe = exynos_rng_probe,
405 .remove = exynos_rng_remove,
406};
407
408module_platform_driver(exynos_rng_driver);
409
410MODULE_DESCRIPTION("Exynos H/W Random Number Generator driver");
411MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
412MODULE_LICENSE("GPL");