blob: 4a06092074b9f3882a09bd0e5bc92a487f165884 [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 Stelmach25cf7f02017-12-12 17:36:07 +010025#include <linux/mutex.h>
Łukasz Stelmachf76d38b2017-12-12 17:36:04 +010026#include <linux/of_device.h>
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +020027#include <linux/platform_device.h>
28
29#include <crypto/internal/rng.h>
30
31#define EXYNOS_RNG_CONTROL 0x0
32#define EXYNOS_RNG_STATUS 0x10
Łukasz Stelmachf76d38b2017-12-12 17:36:04 +010033
34#define EXYNOS_RNG_SEED_CONF 0x14
35#define EXYNOS_RNG_GEN_PRNG BIT(1)
36
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +020037#define EXYNOS_RNG_SEED_BASE 0x140
38#define EXYNOS_RNG_SEED(n) (EXYNOS_RNG_SEED_BASE + (n * 0x4))
39#define EXYNOS_RNG_OUT_BASE 0x160
40#define EXYNOS_RNG_OUT(n) (EXYNOS_RNG_OUT_BASE + (n * 0x4))
41
42/* EXYNOS_RNG_CONTROL bit fields */
43#define EXYNOS_RNG_CONTROL_START 0x18
44/* EXYNOS_RNG_STATUS bit fields */
45#define EXYNOS_RNG_STATUS_SEED_SETTING_DONE BIT(1)
46#define EXYNOS_RNG_STATUS_RNG_DONE BIT(5)
47
48/* Five seed and output registers, each 4 bytes */
49#define EXYNOS_RNG_SEED_REGS 5
50#define EXYNOS_RNG_SEED_SIZE (EXYNOS_RNG_SEED_REGS * 4)
51
Łukasz Stelmachf76d38b2017-12-12 17:36:04 +010052enum exynos_prng_type {
53 EXYNOS_PRNG_UNKNOWN = 0,
54 EXYNOS_PRNG_EXYNOS4,
55 EXYNOS_PRNG_EXYNOS5,
56};
57
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +020058/*
Łukasz Stelmach99c9acf2017-12-12 17:36:06 +010059 * Driver re-seeds itself with generated random numbers to hinder
60 * backtracking of the original seed.
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +020061 *
62 * Time for next re-seed in ms.
63 */
Łukasz Stelmach99c9acf2017-12-12 17:36:06 +010064#define EXYNOS_RNG_RESEED_TIME 1000
65#define EXYNOS_RNG_RESEED_BYTES 65536
66
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +020067/*
68 * In polling mode, do not wait infinitely for the engine to finish the work.
69 */
70#define EXYNOS_RNG_WAIT_RETRIES 100
71
72/* Context for crypto */
73struct exynos_rng_ctx {
74 struct exynos_rng_dev *rng;
75};
76
77/* Device associated memory */
78struct exynos_rng_dev {
79 struct device *dev;
Łukasz Stelmachf76d38b2017-12-12 17:36:04 +010080 enum exynos_prng_type type;
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +020081 void __iomem *mem;
82 struct clk *clk;
Łukasz Stelmach25cf7f02017-12-12 17:36:07 +010083 struct mutex lock;
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +020084 /* Generated numbers stored for seeding during resume */
85 u8 seed_save[EXYNOS_RNG_SEED_SIZE];
86 unsigned int seed_save_len;
87 /* Time of last seeding in jiffies */
88 unsigned long last_seeding;
Łukasz Stelmach99c9acf2017-12-12 17:36:06 +010089 /* Bytes generated since last seeding */
90 unsigned long bytes_seeding;
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +020091};
92
93static struct exynos_rng_dev *exynos_rng_dev;
94
95static u32 exynos_rng_readl(struct exynos_rng_dev *rng, u32 offset)
96{
97 return readl_relaxed(rng->mem + offset);
98}
99
100static void exynos_rng_writel(struct exynos_rng_dev *rng, u32 val, u32 offset)
101{
102 writel_relaxed(val, rng->mem + offset);
103}
104
105static int exynos_rng_set_seed(struct exynos_rng_dev *rng,
106 const u8 *seed, unsigned int slen)
107{
108 u32 val;
109 int i;
110
111 /* Round seed length because loop iterates over full register size */
112 slen = ALIGN_DOWN(slen, 4);
113
114 if (slen < EXYNOS_RNG_SEED_SIZE)
115 return -EINVAL;
116
117 for (i = 0; i < slen ; i += 4) {
118 unsigned int seed_reg = (i / 4) % EXYNOS_RNG_SEED_REGS;
119
120 val = seed[i] << 24;
121 val |= seed[i + 1] << 16;
122 val |= seed[i + 2] << 8;
123 val |= seed[i + 3] << 0;
124
125 exynos_rng_writel(rng, val, EXYNOS_RNG_SEED(seed_reg));
126 }
127
128 val = exynos_rng_readl(rng, EXYNOS_RNG_STATUS);
129 if (!(val & EXYNOS_RNG_STATUS_SEED_SETTING_DONE)) {
130 dev_warn(rng->dev, "Seed setting not finished\n");
131 return -EIO;
132 }
133
134 rng->last_seeding = jiffies;
Łukasz Stelmach99c9acf2017-12-12 17:36:06 +0100135 rng->bytes_seeding = 0;
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +0200136
137 return 0;
138}
139
140/*
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +0200141 * Start the engine and poll for finish. Then read from output registers
142 * filling the 'dst' buffer up to 'dlen' bytes or up to size of generated
143 * random data (EXYNOS_RNG_SEED_SIZE).
144 *
145 * On success: return 0 and store number of read bytes under 'read' address.
146 * On error: return -ERRNO.
147 */
148static int exynos_rng_get_random(struct exynos_rng_dev *rng,
149 u8 *dst, unsigned int dlen,
150 unsigned int *read)
151{
152 int retry = EXYNOS_RNG_WAIT_RETRIES;
153
Łukasz Stelmachf76d38b2017-12-12 17:36:04 +0100154 if (rng->type == EXYNOS_PRNG_EXYNOS4) {
155 exynos_rng_writel(rng, EXYNOS_RNG_CONTROL_START,
156 EXYNOS_RNG_CONTROL);
157 } else if (rng->type == EXYNOS_PRNG_EXYNOS5) {
158 exynos_rng_writel(rng, EXYNOS_RNG_GEN_PRNG,
159 EXYNOS_RNG_SEED_CONF);
160 }
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +0200161
162 while (!(exynos_rng_readl(rng,
163 EXYNOS_RNG_STATUS) & EXYNOS_RNG_STATUS_RNG_DONE) && --retry)
164 cpu_relax();
165
166 if (!retry)
167 return -ETIMEDOUT;
168
169 /* Clear status bit */
170 exynos_rng_writel(rng, EXYNOS_RNG_STATUS_RNG_DONE,
171 EXYNOS_RNG_STATUS);
Łukasz Stelmach3a5a5e52017-12-12 17:36:05 +0100172 *read = min_t(size_t, dlen, EXYNOS_RNG_SEED_SIZE);
173 memcpy_fromio(dst, rng->mem + EXYNOS_RNG_OUT_BASE, *read);
Łukasz Stelmach99c9acf2017-12-12 17:36:06 +0100174 rng->bytes_seeding += *read;
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +0200175
176 return 0;
177}
178
179/* Re-seed itself from time to time */
180static void exynos_rng_reseed(struct exynos_rng_dev *rng)
181{
182 unsigned long next_seeding = rng->last_seeding + \
183 msecs_to_jiffies(EXYNOS_RNG_RESEED_TIME);
184 unsigned long now = jiffies;
185 unsigned int read = 0;
186 u8 seed[EXYNOS_RNG_SEED_SIZE];
187
Łukasz Stelmach99c9acf2017-12-12 17:36:06 +0100188 if (time_before(now, next_seeding) &&
189 rng->bytes_seeding < EXYNOS_RNG_RESEED_BYTES)
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +0200190 return;
191
192 if (exynos_rng_get_random(rng, seed, sizeof(seed), &read))
193 return;
194
195 exynos_rng_set_seed(rng, seed, read);
Łukasz Stelmach25cf7f02017-12-12 17:36:07 +0100196
197 /* Let others do some of their job. */
198 mutex_unlock(&rng->lock);
199 mutex_lock(&rng->lock);
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +0200200}
201
202static int exynos_rng_generate(struct crypto_rng *tfm,
203 const u8 *src, unsigned int slen,
204 u8 *dst, unsigned int dlen)
205{
206 struct exynos_rng_ctx *ctx = crypto_rng_ctx(tfm);
207 struct exynos_rng_dev *rng = ctx->rng;
208 unsigned int read = 0;
209 int ret;
210
211 ret = clk_prepare_enable(rng->clk);
212 if (ret)
213 return ret;
214
Łukasz Stelmach25cf7f02017-12-12 17:36:07 +0100215 mutex_lock(&rng->lock);
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +0200216 do {
217 ret = exynos_rng_get_random(rng, dst, dlen, &read);
218 if (ret)
219 break;
220
221 dlen -= read;
222 dst += read;
223
224 exynos_rng_reseed(rng);
225 } while (dlen > 0);
Łukasz Stelmach25cf7f02017-12-12 17:36:07 +0100226 mutex_unlock(&rng->lock);
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +0200227
228 clk_disable_unprepare(rng->clk);
229
230 return ret;
231}
232
233static int exynos_rng_seed(struct crypto_rng *tfm, const u8 *seed,
234 unsigned int slen)
235{
236 struct exynos_rng_ctx *ctx = crypto_rng_ctx(tfm);
237 struct exynos_rng_dev *rng = ctx->rng;
238 int ret;
239
240 ret = clk_prepare_enable(rng->clk);
241 if (ret)
242 return ret;
243
Łukasz Stelmach25cf7f02017-12-12 17:36:07 +0100244 mutex_lock(&rng->lock);
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +0200245 ret = exynos_rng_set_seed(ctx->rng, seed, slen);
Łukasz Stelmach25cf7f02017-12-12 17:36:07 +0100246 mutex_unlock(&rng->lock);
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +0200247
248 clk_disable_unprepare(rng->clk);
249
250 return ret;
251}
252
253static int exynos_rng_kcapi_init(struct crypto_tfm *tfm)
254{
255 struct exynos_rng_ctx *ctx = crypto_tfm_ctx(tfm);
256
257 ctx->rng = exynos_rng_dev;
258
259 return 0;
260}
261
262static struct rng_alg exynos_rng_alg = {
263 .generate = exynos_rng_generate,
264 .seed = exynos_rng_seed,
265 .seedsize = EXYNOS_RNG_SEED_SIZE,
266 .base = {
267 .cra_name = "stdrng",
268 .cra_driver_name = "exynos_rng",
Łukasz Stelmach3fc12642017-12-05 17:20:46 +0100269 .cra_priority = 300,
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +0200270 .cra_ctxsize = sizeof(struct exynos_rng_ctx),
271 .cra_module = THIS_MODULE,
272 .cra_init = exynos_rng_kcapi_init,
273 }
274};
275
276static int exynos_rng_probe(struct platform_device *pdev)
277{
278 struct exynos_rng_dev *rng;
279 struct resource *res;
280 int ret;
281
282 if (exynos_rng_dev)
283 return -EEXIST;
284
285 rng = devm_kzalloc(&pdev->dev, sizeof(*rng), GFP_KERNEL);
286 if (!rng)
287 return -ENOMEM;
288
Łukasz Stelmachf76d38b2017-12-12 17:36:04 +0100289 rng->type = (enum exynos_prng_type)of_device_get_match_data(&pdev->dev);
290
Łukasz Stelmach25cf7f02017-12-12 17:36:07 +0100291 mutex_init(&rng->lock);
292
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +0200293 rng->dev = &pdev->dev;
294 rng->clk = devm_clk_get(&pdev->dev, "secss");
295 if (IS_ERR(rng->clk)) {
296 dev_err(&pdev->dev, "Couldn't get clock.\n");
297 return PTR_ERR(rng->clk);
298 }
299
300 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
301 rng->mem = devm_ioremap_resource(&pdev->dev, res);
302 if (IS_ERR(rng->mem))
303 return PTR_ERR(rng->mem);
304
305 platform_set_drvdata(pdev, rng);
306
307 exynos_rng_dev = rng;
308
309 ret = crypto_register_rng(&exynos_rng_alg);
310 if (ret) {
311 dev_err(&pdev->dev,
312 "Couldn't register rng crypto alg: %d\n", ret);
313 exynos_rng_dev = NULL;
314 }
315
316 return ret;
317}
318
319static int exynos_rng_remove(struct platform_device *pdev)
320{
321 crypto_unregister_rng(&exynos_rng_alg);
322
323 exynos_rng_dev = NULL;
324
325 return 0;
326}
327
328static int __maybe_unused exynos_rng_suspend(struct device *dev)
329{
330 struct platform_device *pdev = to_platform_device(dev);
331 struct exynos_rng_dev *rng = platform_get_drvdata(pdev);
332 int ret;
333
334 /* If we were never seeded then after resume it will be the same */
335 if (!rng->last_seeding)
336 return 0;
337
338 rng->seed_save_len = 0;
339 ret = clk_prepare_enable(rng->clk);
340 if (ret)
341 return ret;
342
Łukasz Stelmach25cf7f02017-12-12 17:36:07 +0100343 mutex_lock(&rng->lock);
344
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +0200345 /* Get new random numbers and store them for seeding on resume. */
346 exynos_rng_get_random(rng, rng->seed_save, sizeof(rng->seed_save),
347 &(rng->seed_save_len));
Łukasz Stelmach25cf7f02017-12-12 17:36:07 +0100348
349 mutex_unlock(&rng->lock);
350
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +0200351 dev_dbg(rng->dev, "Stored %u bytes for seeding on system resume\n",
352 rng->seed_save_len);
353
354 clk_disable_unprepare(rng->clk);
355
356 return 0;
357}
358
359static int __maybe_unused exynos_rng_resume(struct device *dev)
360{
361 struct platform_device *pdev = to_platform_device(dev);
362 struct exynos_rng_dev *rng = platform_get_drvdata(pdev);
363 int ret;
364
365 /* Never seeded so nothing to do */
366 if (!rng->last_seeding)
367 return 0;
368
369 ret = clk_prepare_enable(rng->clk);
370 if (ret)
371 return ret;
372
Łukasz Stelmach25cf7f02017-12-12 17:36:07 +0100373 mutex_lock(&rng->lock);
374
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +0200375 ret = exynos_rng_set_seed(rng, rng->seed_save, rng->seed_save_len);
376
Łukasz Stelmach25cf7f02017-12-12 17:36:07 +0100377 mutex_unlock(&rng->lock);
378
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +0200379 clk_disable_unprepare(rng->clk);
380
381 return ret;
382}
383
384static SIMPLE_DEV_PM_OPS(exynos_rng_pm_ops, exynos_rng_suspend,
385 exynos_rng_resume);
386
387static const struct of_device_id exynos_rng_dt_match[] = {
388 {
389 .compatible = "samsung,exynos4-rng",
Łukasz Stelmachf76d38b2017-12-12 17:36:04 +0100390 .data = (const void *)EXYNOS_PRNG_EXYNOS4,
391 }, {
392 .compatible = "samsung,exynos5250-prng",
393 .data = (const void *)EXYNOS_PRNG_EXYNOS5,
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +0200394 },
395 { },
396};
397MODULE_DEVICE_TABLE(of, exynos_rng_dt_match);
398
399static struct platform_driver exynos_rng_driver = {
400 .driver = {
401 .name = "exynos-rng",
402 .pm = &exynos_rng_pm_ops,
403 .of_match_table = exynos_rng_dt_match,
404 },
405 .probe = exynos_rng_probe,
406 .remove = exynos_rng_remove,
407};
408
409module_platform_driver(exynos_rng_driver);
410
411MODULE_DESCRIPTION("Exynos H/W Random Number Generator driver");
412MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
413MODULE_LICENSE("GPL");