blob: 825ed7bfd881673bfdd8bdc76901e439c093a527 [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/*
Łukasz Stelmach99c9acf2017-12-12 17:36:06 +010058 * Driver re-seeds itself with generated random numbers to hinder
59 * backtracking of the original seed.
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +020060 *
61 * Time for next re-seed in ms.
62 */
Łukasz Stelmach99c9acf2017-12-12 17:36:06 +010063#define EXYNOS_RNG_RESEED_TIME 1000
64#define EXYNOS_RNG_RESEED_BYTES 65536
65
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +020066/*
67 * In polling mode, do not wait infinitely for the engine to finish the work.
68 */
69#define EXYNOS_RNG_WAIT_RETRIES 100
70
71/* Context for crypto */
72struct exynos_rng_ctx {
73 struct exynos_rng_dev *rng;
74};
75
76/* Device associated memory */
77struct exynos_rng_dev {
78 struct device *dev;
Łukasz Stelmachf76d38b2017-12-12 17:36:04 +010079 enum exynos_prng_type type;
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +020080 void __iomem *mem;
81 struct clk *clk;
82 /* Generated numbers stored for seeding during resume */
83 u8 seed_save[EXYNOS_RNG_SEED_SIZE];
84 unsigned int seed_save_len;
85 /* Time of last seeding in jiffies */
86 unsigned long last_seeding;
Łukasz Stelmach99c9acf2017-12-12 17:36:06 +010087 /* Bytes generated since last seeding */
88 unsigned long bytes_seeding;
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +020089};
90
91static struct exynos_rng_dev *exynos_rng_dev;
92
93static u32 exynos_rng_readl(struct exynos_rng_dev *rng, u32 offset)
94{
95 return readl_relaxed(rng->mem + offset);
96}
97
98static void exynos_rng_writel(struct exynos_rng_dev *rng, u32 val, u32 offset)
99{
100 writel_relaxed(val, rng->mem + offset);
101}
102
103static int exynos_rng_set_seed(struct exynos_rng_dev *rng,
104 const u8 *seed, unsigned int slen)
105{
106 u32 val;
107 int i;
108
109 /* Round seed length because loop iterates over full register size */
110 slen = ALIGN_DOWN(slen, 4);
111
112 if (slen < EXYNOS_RNG_SEED_SIZE)
113 return -EINVAL;
114
115 for (i = 0; i < slen ; i += 4) {
116 unsigned int seed_reg = (i / 4) % EXYNOS_RNG_SEED_REGS;
117
118 val = seed[i] << 24;
119 val |= seed[i + 1] << 16;
120 val |= seed[i + 2] << 8;
121 val |= seed[i + 3] << 0;
122
123 exynos_rng_writel(rng, val, EXYNOS_RNG_SEED(seed_reg));
124 }
125
126 val = exynos_rng_readl(rng, EXYNOS_RNG_STATUS);
127 if (!(val & EXYNOS_RNG_STATUS_SEED_SETTING_DONE)) {
128 dev_warn(rng->dev, "Seed setting not finished\n");
129 return -EIO;
130 }
131
132 rng->last_seeding = jiffies;
Łukasz Stelmach99c9acf2017-12-12 17:36:06 +0100133 rng->bytes_seeding = 0;
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +0200134
135 return 0;
136}
137
138/*
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +0200139 * Start the engine and poll for finish. Then read from output registers
140 * filling the 'dst' buffer up to 'dlen' bytes or up to size of generated
141 * random data (EXYNOS_RNG_SEED_SIZE).
142 *
143 * On success: return 0 and store number of read bytes under 'read' address.
144 * On error: return -ERRNO.
145 */
146static int exynos_rng_get_random(struct exynos_rng_dev *rng,
147 u8 *dst, unsigned int dlen,
148 unsigned int *read)
149{
150 int retry = EXYNOS_RNG_WAIT_RETRIES;
151
Łukasz Stelmachf76d38b2017-12-12 17:36:04 +0100152 if (rng->type == EXYNOS_PRNG_EXYNOS4) {
153 exynos_rng_writel(rng, EXYNOS_RNG_CONTROL_START,
154 EXYNOS_RNG_CONTROL);
155 } else if (rng->type == EXYNOS_PRNG_EXYNOS5) {
156 exynos_rng_writel(rng, EXYNOS_RNG_GEN_PRNG,
157 EXYNOS_RNG_SEED_CONF);
158 }
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +0200159
160 while (!(exynos_rng_readl(rng,
161 EXYNOS_RNG_STATUS) & EXYNOS_RNG_STATUS_RNG_DONE) && --retry)
162 cpu_relax();
163
164 if (!retry)
165 return -ETIMEDOUT;
166
167 /* Clear status bit */
168 exynos_rng_writel(rng, EXYNOS_RNG_STATUS_RNG_DONE,
169 EXYNOS_RNG_STATUS);
Łukasz Stelmach3a5a5e52017-12-12 17:36:05 +0100170 *read = min_t(size_t, dlen, EXYNOS_RNG_SEED_SIZE);
171 memcpy_fromio(dst, rng->mem + EXYNOS_RNG_OUT_BASE, *read);
Łukasz Stelmach99c9acf2017-12-12 17:36:06 +0100172 rng->bytes_seeding += *read;
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +0200173
174 return 0;
175}
176
177/* Re-seed itself from time to time */
178static void exynos_rng_reseed(struct exynos_rng_dev *rng)
179{
180 unsigned long next_seeding = rng->last_seeding + \
181 msecs_to_jiffies(EXYNOS_RNG_RESEED_TIME);
182 unsigned long now = jiffies;
183 unsigned int read = 0;
184 u8 seed[EXYNOS_RNG_SEED_SIZE];
185
Łukasz Stelmach99c9acf2017-12-12 17:36:06 +0100186 if (time_before(now, next_seeding) &&
187 rng->bytes_seeding < EXYNOS_RNG_RESEED_BYTES)
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +0200188 return;
189
190 if (exynos_rng_get_random(rng, seed, sizeof(seed), &read))
191 return;
192
193 exynos_rng_set_seed(rng, seed, read);
194}
195
196static int exynos_rng_generate(struct crypto_rng *tfm,
197 const u8 *src, unsigned int slen,
198 u8 *dst, unsigned int dlen)
199{
200 struct exynos_rng_ctx *ctx = crypto_rng_ctx(tfm);
201 struct exynos_rng_dev *rng = ctx->rng;
202 unsigned int read = 0;
203 int ret;
204
205 ret = clk_prepare_enable(rng->clk);
206 if (ret)
207 return ret;
208
209 do {
210 ret = exynos_rng_get_random(rng, dst, dlen, &read);
211 if (ret)
212 break;
213
214 dlen -= read;
215 dst += read;
216
217 exynos_rng_reseed(rng);
218 } while (dlen > 0);
219
220 clk_disable_unprepare(rng->clk);
221
222 return ret;
223}
224
225static int exynos_rng_seed(struct crypto_rng *tfm, const u8 *seed,
226 unsigned int slen)
227{
228 struct exynos_rng_ctx *ctx = crypto_rng_ctx(tfm);
229 struct exynos_rng_dev *rng = ctx->rng;
230 int ret;
231
232 ret = clk_prepare_enable(rng->clk);
233 if (ret)
234 return ret;
235
236 ret = exynos_rng_set_seed(ctx->rng, seed, slen);
237
238 clk_disable_unprepare(rng->clk);
239
240 return ret;
241}
242
243static int exynos_rng_kcapi_init(struct crypto_tfm *tfm)
244{
245 struct exynos_rng_ctx *ctx = crypto_tfm_ctx(tfm);
246
247 ctx->rng = exynos_rng_dev;
248
249 return 0;
250}
251
252static struct rng_alg exynos_rng_alg = {
253 .generate = exynos_rng_generate,
254 .seed = exynos_rng_seed,
255 .seedsize = EXYNOS_RNG_SEED_SIZE,
256 .base = {
257 .cra_name = "stdrng",
258 .cra_driver_name = "exynos_rng",
Łukasz Stelmach3fc12642017-12-05 17:20:46 +0100259 .cra_priority = 300,
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +0200260 .cra_ctxsize = sizeof(struct exynos_rng_ctx),
261 .cra_module = THIS_MODULE,
262 .cra_init = exynos_rng_kcapi_init,
263 }
264};
265
266static int exynos_rng_probe(struct platform_device *pdev)
267{
268 struct exynos_rng_dev *rng;
269 struct resource *res;
270 int ret;
271
272 if (exynos_rng_dev)
273 return -EEXIST;
274
275 rng = devm_kzalloc(&pdev->dev, sizeof(*rng), GFP_KERNEL);
276 if (!rng)
277 return -ENOMEM;
278
Łukasz Stelmachf76d38b2017-12-12 17:36:04 +0100279 rng->type = (enum exynos_prng_type)of_device_get_match_data(&pdev->dev);
280
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +0200281 rng->dev = &pdev->dev;
282 rng->clk = devm_clk_get(&pdev->dev, "secss");
283 if (IS_ERR(rng->clk)) {
284 dev_err(&pdev->dev, "Couldn't get clock.\n");
285 return PTR_ERR(rng->clk);
286 }
287
288 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
289 rng->mem = devm_ioremap_resource(&pdev->dev, res);
290 if (IS_ERR(rng->mem))
291 return PTR_ERR(rng->mem);
292
293 platform_set_drvdata(pdev, rng);
294
295 exynos_rng_dev = rng;
296
297 ret = crypto_register_rng(&exynos_rng_alg);
298 if (ret) {
299 dev_err(&pdev->dev,
300 "Couldn't register rng crypto alg: %d\n", ret);
301 exynos_rng_dev = NULL;
302 }
303
304 return ret;
305}
306
307static int exynos_rng_remove(struct platform_device *pdev)
308{
309 crypto_unregister_rng(&exynos_rng_alg);
310
311 exynos_rng_dev = NULL;
312
313 return 0;
314}
315
316static int __maybe_unused exynos_rng_suspend(struct device *dev)
317{
318 struct platform_device *pdev = to_platform_device(dev);
319 struct exynos_rng_dev *rng = platform_get_drvdata(pdev);
320 int ret;
321
322 /* If we were never seeded then after resume it will be the same */
323 if (!rng->last_seeding)
324 return 0;
325
326 rng->seed_save_len = 0;
327 ret = clk_prepare_enable(rng->clk);
328 if (ret)
329 return ret;
330
331 /* Get new random numbers and store them for seeding on resume. */
332 exynos_rng_get_random(rng, rng->seed_save, sizeof(rng->seed_save),
333 &(rng->seed_save_len));
334 dev_dbg(rng->dev, "Stored %u bytes for seeding on system resume\n",
335 rng->seed_save_len);
336
337 clk_disable_unprepare(rng->clk);
338
339 return 0;
340}
341
342static int __maybe_unused exynos_rng_resume(struct device *dev)
343{
344 struct platform_device *pdev = to_platform_device(dev);
345 struct exynos_rng_dev *rng = platform_get_drvdata(pdev);
346 int ret;
347
348 /* Never seeded so nothing to do */
349 if (!rng->last_seeding)
350 return 0;
351
352 ret = clk_prepare_enable(rng->clk);
353 if (ret)
354 return ret;
355
356 ret = exynos_rng_set_seed(rng, rng->seed_save, rng->seed_save_len);
357
358 clk_disable_unprepare(rng->clk);
359
360 return ret;
361}
362
363static SIMPLE_DEV_PM_OPS(exynos_rng_pm_ops, exynos_rng_suspend,
364 exynos_rng_resume);
365
366static const struct of_device_id exynos_rng_dt_match[] = {
367 {
368 .compatible = "samsung,exynos4-rng",
Łukasz Stelmachf76d38b2017-12-12 17:36:04 +0100369 .data = (const void *)EXYNOS_PRNG_EXYNOS4,
370 }, {
371 .compatible = "samsung,exynos5250-prng",
372 .data = (const void *)EXYNOS_PRNG_EXYNOS5,
Krzysztof Kozlowskic46ea132017-04-11 20:08:35 +0200373 },
374 { },
375};
376MODULE_DEVICE_TABLE(of, exynos_rng_dt_match);
377
378static struct platform_driver exynos_rng_driver = {
379 .driver = {
380 .name = "exynos-rng",
381 .pm = &exynos_rng_pm_ops,
382 .of_match_table = exynos_rng_dt_match,
383 },
384 .probe = exynos_rng_probe,
385 .remove = exynos_rng_remove,
386};
387
388module_platform_driver(exynos_rng_driver);
389
390MODULE_DESCRIPTION("Exynos H/W Random Number Generator driver");
391MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
392MODULE_LICENSE("GPL");