fs: make fiemap work from compat_ioctl
[linux/fpc-iii.git] / drivers / crypto / exynos-rng.c
blob451620b475a03ab80445436fe722985349cf6159
1 /*
2 * exynos-rng.c - Random Number Generator driver for the Exynos
4 * Copyright (c) 2017 Krzysztof Kozlowski <krzk@kernel.org>
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>
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;
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.
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>
25 #include <linux/platform_device.h>
27 #include <crypto/internal/rng.h>
29 #define EXYNOS_RNG_CONTROL 0x0
30 #define EXYNOS_RNG_STATUS 0x10
31 #define EXYNOS_RNG_SEED_BASE 0x140
32 #define EXYNOS_RNG_SEED(n) (EXYNOS_RNG_SEED_BASE + (n * 0x4))
33 #define EXYNOS_RNG_OUT_BASE 0x160
34 #define EXYNOS_RNG_OUT(n) (EXYNOS_RNG_OUT_BASE + (n * 0x4))
36 /* EXYNOS_RNG_CONTROL bit fields */
37 #define EXYNOS_RNG_CONTROL_START 0x18
38 /* EXYNOS_RNG_STATUS bit fields */
39 #define EXYNOS_RNG_STATUS_SEED_SETTING_DONE BIT(1)
40 #define EXYNOS_RNG_STATUS_RNG_DONE BIT(5)
42 /* Five seed and output registers, each 4 bytes */
43 #define EXYNOS_RNG_SEED_REGS 5
44 #define EXYNOS_RNG_SEED_SIZE (EXYNOS_RNG_SEED_REGS * 4)
47 * Driver re-seeds itself with generated random numbers to increase
48 * the randomness.
50 * Time for next re-seed in ms.
52 #define EXYNOS_RNG_RESEED_TIME 100
54 * In polling mode, do not wait infinitely for the engine to finish the work.
56 #define EXYNOS_RNG_WAIT_RETRIES 100
58 /* Context for crypto */
59 struct exynos_rng_ctx {
60 struct exynos_rng_dev *rng;
63 /* Device associated memory */
64 struct exynos_rng_dev {
65 struct device *dev;
66 void __iomem *mem;
67 struct clk *clk;
68 /* Generated numbers stored for seeding during resume */
69 u8 seed_save[EXYNOS_RNG_SEED_SIZE];
70 unsigned int seed_save_len;
71 /* Time of last seeding in jiffies */
72 unsigned long last_seeding;
75 static struct exynos_rng_dev *exynos_rng_dev;
77 static u32 exynos_rng_readl(struct exynos_rng_dev *rng, u32 offset)
79 return readl_relaxed(rng->mem + offset);
82 static void exynos_rng_writel(struct exynos_rng_dev *rng, u32 val, u32 offset)
84 writel_relaxed(val, rng->mem + offset);
87 static int exynos_rng_set_seed(struct exynos_rng_dev *rng,
88 const u8 *seed, unsigned int slen)
90 u32 val;
91 int i;
93 /* Round seed length because loop iterates over full register size */
94 slen = ALIGN_DOWN(slen, 4);
96 if (slen < EXYNOS_RNG_SEED_SIZE)
97 return -EINVAL;
99 for (i = 0; i < slen ; i += 4) {
100 unsigned int seed_reg = (i / 4) % EXYNOS_RNG_SEED_REGS;
102 val = seed[i] << 24;
103 val |= seed[i + 1] << 16;
104 val |= seed[i + 2] << 8;
105 val |= seed[i + 3] << 0;
107 exynos_rng_writel(rng, val, EXYNOS_RNG_SEED(seed_reg));
110 val = exynos_rng_readl(rng, EXYNOS_RNG_STATUS);
111 if (!(val & EXYNOS_RNG_STATUS_SEED_SETTING_DONE)) {
112 dev_warn(rng->dev, "Seed setting not finished\n");
113 return -EIO;
116 rng->last_seeding = jiffies;
118 return 0;
122 * Read from output registers and put the data under 'dst' array,
123 * up to dlen bytes.
125 * Returns number of bytes actually stored in 'dst' (dlen
126 * or EXYNOS_RNG_SEED_SIZE).
128 static unsigned int exynos_rng_copy_random(struct exynos_rng_dev *rng,
129 u8 *dst, unsigned int dlen)
131 unsigned int cnt = 0;
132 int i, j;
133 u32 val;
135 for (j = 0; j < EXYNOS_RNG_SEED_REGS; j++) {
136 val = exynos_rng_readl(rng, EXYNOS_RNG_OUT(j));
138 for (i = 0; i < 4; i++) {
139 dst[cnt] = val & 0xff;
140 val >>= 8;
141 if (++cnt >= dlen)
142 return cnt;
146 return cnt;
150 * Start the engine and poll for finish. Then read from output registers
151 * filling the 'dst' buffer up to 'dlen' bytes or up to size of generated
152 * random data (EXYNOS_RNG_SEED_SIZE).
154 * On success: return 0 and store number of read bytes under 'read' address.
155 * On error: return -ERRNO.
157 static int exynos_rng_get_random(struct exynos_rng_dev *rng,
158 u8 *dst, unsigned int dlen,
159 unsigned int *read)
161 int retry = EXYNOS_RNG_WAIT_RETRIES;
163 exynos_rng_writel(rng, EXYNOS_RNG_CONTROL_START,
164 EXYNOS_RNG_CONTROL);
166 while (!(exynos_rng_readl(rng,
167 EXYNOS_RNG_STATUS) & EXYNOS_RNG_STATUS_RNG_DONE) && --retry)
168 cpu_relax();
170 if (!retry)
171 return -ETIMEDOUT;
173 /* Clear status bit */
174 exynos_rng_writel(rng, EXYNOS_RNG_STATUS_RNG_DONE,
175 EXYNOS_RNG_STATUS);
176 *read = exynos_rng_copy_random(rng, dst, dlen);
178 return 0;
181 /* Re-seed itself from time to time */
182 static void exynos_rng_reseed(struct exynos_rng_dev *rng)
184 unsigned long next_seeding = rng->last_seeding + \
185 msecs_to_jiffies(EXYNOS_RNG_RESEED_TIME);
186 unsigned long now = jiffies;
187 unsigned int read = 0;
188 u8 seed[EXYNOS_RNG_SEED_SIZE];
190 if (time_before(now, next_seeding))
191 return;
193 if (exynos_rng_get_random(rng, seed, sizeof(seed), &read))
194 return;
196 exynos_rng_set_seed(rng, seed, read);
199 static int exynos_rng_generate(struct crypto_rng *tfm,
200 const u8 *src, unsigned int slen,
201 u8 *dst, unsigned int dlen)
203 struct exynos_rng_ctx *ctx = crypto_rng_ctx(tfm);
204 struct exynos_rng_dev *rng = ctx->rng;
205 unsigned int read = 0;
206 int ret;
208 ret = clk_prepare_enable(rng->clk);
209 if (ret)
210 return ret;
212 do {
213 ret = exynos_rng_get_random(rng, dst, dlen, &read);
214 if (ret)
215 break;
217 dlen -= read;
218 dst += read;
220 exynos_rng_reseed(rng);
221 } while (dlen > 0);
223 clk_disable_unprepare(rng->clk);
225 return ret;
228 static int exynos_rng_seed(struct crypto_rng *tfm, const u8 *seed,
229 unsigned int slen)
231 struct exynos_rng_ctx *ctx = crypto_rng_ctx(tfm);
232 struct exynos_rng_dev *rng = ctx->rng;
233 int ret;
235 ret = clk_prepare_enable(rng->clk);
236 if (ret)
237 return ret;
239 ret = exynos_rng_set_seed(ctx->rng, seed, slen);
241 clk_disable_unprepare(rng->clk);
243 return ret;
246 static int exynos_rng_kcapi_init(struct crypto_tfm *tfm)
248 struct exynos_rng_ctx *ctx = crypto_tfm_ctx(tfm);
250 ctx->rng = exynos_rng_dev;
252 return 0;
255 static struct rng_alg exynos_rng_alg = {
256 .generate = exynos_rng_generate,
257 .seed = exynos_rng_seed,
258 .seedsize = EXYNOS_RNG_SEED_SIZE,
259 .base = {
260 .cra_name = "stdrng",
261 .cra_driver_name = "exynos_rng",
262 .cra_priority = 100,
263 .cra_ctxsize = sizeof(struct exynos_rng_ctx),
264 .cra_module = THIS_MODULE,
265 .cra_init = exynos_rng_kcapi_init,
269 static int exynos_rng_probe(struct platform_device *pdev)
271 struct exynos_rng_dev *rng;
272 struct resource *res;
273 int ret;
275 if (exynos_rng_dev)
276 return -EEXIST;
278 rng = devm_kzalloc(&pdev->dev, sizeof(*rng), GFP_KERNEL);
279 if (!rng)
280 return -ENOMEM;
282 rng->dev = &pdev->dev;
283 rng->clk = devm_clk_get(&pdev->dev, "secss");
284 if (IS_ERR(rng->clk)) {
285 dev_err(&pdev->dev, "Couldn't get clock.\n");
286 return PTR_ERR(rng->clk);
289 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
290 rng->mem = devm_ioremap_resource(&pdev->dev, res);
291 if (IS_ERR(rng->mem))
292 return PTR_ERR(rng->mem);
294 platform_set_drvdata(pdev, rng);
296 exynos_rng_dev = rng;
298 ret = crypto_register_rng(&exynos_rng_alg);
299 if (ret) {
300 dev_err(&pdev->dev,
301 "Couldn't register rng crypto alg: %d\n", ret);
302 exynos_rng_dev = NULL;
305 return ret;
308 static int exynos_rng_remove(struct platform_device *pdev)
310 crypto_unregister_rng(&exynos_rng_alg);
312 exynos_rng_dev = NULL;
314 return 0;
317 static int __maybe_unused exynos_rng_suspend(struct device *dev)
319 struct platform_device *pdev = to_platform_device(dev);
320 struct exynos_rng_dev *rng = platform_get_drvdata(pdev);
321 int ret;
323 /* If we were never seeded then after resume it will be the same */
324 if (!rng->last_seeding)
325 return 0;
327 rng->seed_save_len = 0;
328 ret = clk_prepare_enable(rng->clk);
329 if (ret)
330 return ret;
332 /* Get new random numbers and store them for seeding on resume. */
333 exynos_rng_get_random(rng, rng->seed_save, sizeof(rng->seed_save),
334 &(rng->seed_save_len));
335 dev_dbg(rng->dev, "Stored %u bytes for seeding on system resume\n",
336 rng->seed_save_len);
338 clk_disable_unprepare(rng->clk);
340 return 0;
343 static int __maybe_unused exynos_rng_resume(struct device *dev)
345 struct platform_device *pdev = to_platform_device(dev);
346 struct exynos_rng_dev *rng = platform_get_drvdata(pdev);
347 int ret;
349 /* Never seeded so nothing to do */
350 if (!rng->last_seeding)
351 return 0;
353 ret = clk_prepare_enable(rng->clk);
354 if (ret)
355 return ret;
357 ret = exynos_rng_set_seed(rng, rng->seed_save, rng->seed_save_len);
359 clk_disable_unprepare(rng->clk);
361 return ret;
364 static SIMPLE_DEV_PM_OPS(exynos_rng_pm_ops, exynos_rng_suspend,
365 exynos_rng_resume);
367 static const struct of_device_id exynos_rng_dt_match[] = {
369 .compatible = "samsung,exynos4-rng",
371 { },
373 MODULE_DEVICE_TABLE(of, exynos_rng_dt_match);
375 static struct platform_driver exynos_rng_driver = {
376 .driver = {
377 .name = "exynos-rng",
378 .pm = &exynos_rng_pm_ops,
379 .of_match_table = exynos_rng_dt_match,
381 .probe = exynos_rng_probe,
382 .remove = exynos_rng_remove,
385 module_platform_driver(exynos_rng_driver);
387 MODULE_DESCRIPTION("Exynos H/W Random Number Generator driver");
388 MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
389 MODULE_LICENSE("GPL");