apm821xx: add support for the apm821xx device target
[openwrt/staging/lynxis/omap.git] / target / linux / apm821xx / patches-4.4 / 001-crypto4xx-integrate-ppc4xx-rng-into-crypto4xx.patch
1 From 5343e674f32fb82b7a80a24b5a84eee62d3fe624 Mon Sep 17 00:00:00 2001
2 From: Christian Lamparter <chunkeey@googlemail.com>
3 Date: Mon, 18 Apr 2016 12:57:41 +0200
4 Subject: [PATCH] crypto4xx: integrate ppc4xx-rng into crypto4xx
5
6 This patch integrates the ppc4xx-rng driver into the existing
7 crypto4xx. This is because the true random number generator
8 is controlled and part of the security core.
9
10 Signed-off-by: Christian Lamparter <chunkeey@googlemail.com>
11 Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
12 ---
13 drivers/char/hw_random/Kconfig | 13 ---
14 drivers/char/hw_random/Makefile | 1 -
15 drivers/char/hw_random/ppc4xx-rng.c | 147 --------------------------------
16 drivers/crypto/Kconfig | 8 ++
17 drivers/crypto/amcc/Makefile | 1 +
18 drivers/crypto/amcc/crypto4xx_core.c | 7 +-
19 drivers/crypto/amcc/crypto4xx_core.h | 4 +
20 drivers/crypto/amcc/crypto4xx_reg_def.h | 1 +
21 drivers/crypto/amcc/crypto4xx_trng.c | 131 ++++++++++++++++++++++++++++
22 drivers/crypto/amcc/crypto4xx_trng.h | 34 ++++++++
23 10 files changed, 184 insertions(+), 163 deletions(-)
24 delete mode 100644 drivers/char/hw_random/ppc4xx-rng.c
25 create mode 100644 drivers/crypto/amcc/crypto4xx_trng.c
26 create mode 100644 drivers/crypto/amcc/crypto4xx_trng.h
27
28 diff --git a/drivers/char/hw_random/Kconfig b/drivers/char/hw_random/Kconfig
29 index c76a88d..ac51149 100644
30 --- a/drivers/char/hw_random/Kconfig
31 +++ b/drivers/char/hw_random/Kconfig
32 @@ -268,19 +268,6 @@ config HW_RANDOM_NOMADIK
33
34 If unsure, say Y.
35
36 -config HW_RANDOM_PPC4XX
37 - tristate "PowerPC 4xx generic true random number generator support"
38 - depends on PPC && 4xx
39 - default HW_RANDOM
40 - ---help---
41 - This driver provides the kernel-side support for the TRNG hardware
42 - found in the security function of some PowerPC 4xx SoCs.
43 -
44 - To compile this driver as a module, choose M here: the
45 - module will be called ppc4xx-rng.
46 -
47 - If unsure, say N.
48 -
49 config HW_RANDOM_PSERIES
50 tristate "pSeries HW Random Number Generator support"
51 depends on PPC64 && IBMVIO
52 diff --git a/drivers/char/hw_random/Makefile b/drivers/char/hw_random/Makefile
53 index e09305b..63022b4 100644
54 --- a/drivers/char/hw_random/Makefile
55 +++ b/drivers/char/hw_random/Makefile
56 @@ -22,7 +22,6 @@ obj-$(CONFIG_HW_RANDOM_TX4939) += tx4939-rng.o
57 obj-$(CONFIG_HW_RANDOM_MXC_RNGA) += mxc-rnga.o
58 obj-$(CONFIG_HW_RANDOM_OCTEON) += octeon-rng.o
59 obj-$(CONFIG_HW_RANDOM_NOMADIK) += nomadik-rng.o
60 -obj-$(CONFIG_HW_RANDOM_PPC4XX) += ppc4xx-rng.o
61 obj-$(CONFIG_HW_RANDOM_PSERIES) += pseries-rng.o
62 obj-$(CONFIG_HW_RANDOM_POWERNV) += powernv-rng.o
63 obj-$(CONFIG_HW_RANDOM_EXYNOS) += exynos-rng.o
64 diff --git a/drivers/char/hw_random/ppc4xx-rng.c b/drivers/char/hw_random/ppc4xx-rng.c
65 deleted file mode 100644
66 index c0db438..0000000
67 --- a/drivers/char/hw_random/ppc4xx-rng.c
68 +++ /dev/null
69 @@ -1,147 +0,0 @@
70 -/*
71 - * Generic PowerPC 44x RNG driver
72 - *
73 - * Copyright 2011 IBM Corporation
74 - *
75 - * This program is free software; you can redistribute it and/or modify it
76 - * under the terms of the GNU General Public License as published by the
77 - * Free Software Foundation; version 2 of the License.
78 - */
79 -
80 -#include <linux/module.h>
81 -#include <linux/kernel.h>
82 -#include <linux/platform_device.h>
83 -#include <linux/hw_random.h>
84 -#include <linux/delay.h>
85 -#include <linux/of_address.h>
86 -#include <linux/of_platform.h>
87 -#include <asm/io.h>
88 -
89 -#define PPC4XX_TRNG_DEV_CTRL 0x60080
90 -
91 -#define PPC4XX_TRNGE 0x00020000
92 -#define PPC4XX_TRNG_CTRL 0x0008
93 -#define PPC4XX_TRNG_CTRL_DALM 0x20
94 -#define PPC4XX_TRNG_STAT 0x0004
95 -#define PPC4XX_TRNG_STAT_B 0x1
96 -#define PPC4XX_TRNG_DATA 0x0000
97 -
98 -#define MODULE_NAME "ppc4xx_rng"
99 -
100 -static int ppc4xx_rng_data_present(struct hwrng *rng, int wait)
101 -{
102 - void __iomem *rng_regs = (void __iomem *) rng->priv;
103 - int busy, i, present = 0;
104 -
105 - for (i = 0; i < 20; i++) {
106 - busy = (in_le32(rng_regs + PPC4XX_TRNG_STAT) & PPC4XX_TRNG_STAT_B);
107 - if (!busy || !wait) {
108 - present = 1;
109 - break;
110 - }
111 - udelay(10);
112 - }
113 - return present;
114 -}
115 -
116 -static int ppc4xx_rng_data_read(struct hwrng *rng, u32 *data)
117 -{
118 - void __iomem *rng_regs = (void __iomem *) rng->priv;
119 - *data = in_le32(rng_regs + PPC4XX_TRNG_DATA);
120 - return 4;
121 -}
122 -
123 -static int ppc4xx_rng_enable(int enable)
124 -{
125 - struct device_node *ctrl;
126 - void __iomem *ctrl_reg;
127 - int err = 0;
128 - u32 val;
129 -
130 - /* Find the main crypto device node and map it to turn the TRNG on */
131 - ctrl = of_find_compatible_node(NULL, NULL, "amcc,ppc4xx-crypto");
132 - if (!ctrl)
133 - return -ENODEV;
134 -
135 - ctrl_reg = of_iomap(ctrl, 0);
136 - if (!ctrl_reg) {
137 - err = -ENODEV;
138 - goto out;
139 - }
140 -
141 - val = in_le32(ctrl_reg + PPC4XX_TRNG_DEV_CTRL);
142 -
143 - if (enable)
144 - val |= PPC4XX_TRNGE;
145 - else
146 - val = val & ~PPC4XX_TRNGE;
147 -
148 - out_le32(ctrl_reg + PPC4XX_TRNG_DEV_CTRL, val);
149 - iounmap(ctrl_reg);
150 -
151 -out:
152 - of_node_put(ctrl);
153 -
154 - return err;
155 -}
156 -
157 -static struct hwrng ppc4xx_rng = {
158 - .name = MODULE_NAME,
159 - .data_present = ppc4xx_rng_data_present,
160 - .data_read = ppc4xx_rng_data_read,
161 -};
162 -
163 -static int ppc4xx_rng_probe(struct platform_device *dev)
164 -{
165 - void __iomem *rng_regs;
166 - int err = 0;
167 -
168 - rng_regs = of_iomap(dev->dev.of_node, 0);
169 - if (!rng_regs)
170 - return -ENODEV;
171 -
172 - err = ppc4xx_rng_enable(1);
173 - if (err)
174 - return err;
175 -
176 - out_le32(rng_regs + PPC4XX_TRNG_CTRL, PPC4XX_TRNG_CTRL_DALM);
177 - ppc4xx_rng.priv = (unsigned long) rng_regs;
178 -
179 - err = hwrng_register(&ppc4xx_rng);
180 -
181 - return err;
182 -}
183 -
184 -static int ppc4xx_rng_remove(struct platform_device *dev)
185 -{
186 - void __iomem *rng_regs = (void __iomem *) ppc4xx_rng.priv;
187 -
188 - hwrng_unregister(&ppc4xx_rng);
189 - ppc4xx_rng_enable(0);
190 - iounmap(rng_regs);
191 -
192 - return 0;
193 -}
194 -
195 -static const struct of_device_id ppc4xx_rng_match[] = {
196 - { .compatible = "ppc4xx-rng", },
197 - { .compatible = "amcc,ppc460ex-rng", },
198 - { .compatible = "amcc,ppc440epx-rng", },
199 - {},
200 -};
201 -MODULE_DEVICE_TABLE(of, ppc4xx_rng_match);
202 -
203 -static struct platform_driver ppc4xx_rng_driver = {
204 - .driver = {
205 - .name = MODULE_NAME,
206 - .of_match_table = ppc4xx_rng_match,
207 - },
208 - .probe = ppc4xx_rng_probe,
209 - .remove = ppc4xx_rng_remove,
210 -};
211 -
212 -module_platform_driver(ppc4xx_rng_driver);
213 -
214 -MODULE_LICENSE("GPL");
215 -MODULE_AUTHOR("Josh Boyer <jwboyer@linux.vnet.ibm.com>");
216 -MODULE_DESCRIPTION("HW RNG driver for PPC 4xx processors");
217 diff --git a/drivers/crypto/Kconfig b/drivers/crypto/Kconfig
218 index 0a22ac7..12fd499 100644
219 --- a/drivers/crypto/Kconfig
220 +++ b/drivers/crypto/Kconfig
221 @@ -279,6 +279,14 @@ config CRYPTO_DEV_PPC4XX
222 help
223 This option allows you to have support for AMCC crypto acceleration.
224
225 +config HW_RANDOM_PPC4XX
226 + bool "PowerPC 4xx generic true random number generator support"
227 + depends on CRYPTO_DEV_PPC4XX && HW_RANDOM
228 + default y
229 + ---help---
230 + This option provides the kernel-side support for the TRNG hardware
231 + found in the security function of some PowerPC 4xx SoCs.
232 +
233 config CRYPTO_DEV_OMAP_SHAM
234 tristate "Support for OMAP MD5/SHA1/SHA2 hw accelerator"
235 depends on ARCH_OMAP2PLUS
236 diff --git a/drivers/crypto/amcc/Makefile b/drivers/crypto/amcc/Makefile
237 index 5c0c62b..b955399 100644
238 --- a/drivers/crypto/amcc/Makefile
239 +++ b/drivers/crypto/amcc/Makefile
240 @@ -1,2 +1,3 @@
241 obj-$(CONFIG_CRYPTO_DEV_PPC4XX) += crypto4xx.o
242 crypto4xx-y := crypto4xx_core.o crypto4xx_alg.o crypto4xx_sa.o
243 +crypto4xx-$(CONFIG_HW_RANDOM_PPC4XX) += crypto4xx_trng.o
244 diff --git a/drivers/crypto/amcc/crypto4xx_core.c b/drivers/crypto/amcc/crypto4xx_core.c
245 index 62134c8..dae1e39 100644
246 --- a/drivers/crypto/amcc/crypto4xx_core.c
247 +++ b/drivers/crypto/amcc/crypto4xx_core.c
248 @@ -40,6 +40,7 @@
249 #include "crypto4xx_reg_def.h"
250 #include "crypto4xx_core.h"
251 #include "crypto4xx_sa.h"
252 +#include "crypto4xx_trng.h"
253
254 #define PPC4XX_SEC_VERSION_STR "0.5"
255
256 @@ -1225,6 +1226,7 @@ static int crypto4xx_probe(struct platform_device *ofdev)
257 if (rc)
258 goto err_start_dev;
259
260 + ppc4xx_trng_probe(core_dev);
261 return 0;
262
263 err_start_dev:
264 @@ -1252,6 +1254,8 @@ static int crypto4xx_remove(struct platform_device *ofdev)
265 struct device *dev = &ofdev->dev;
266 struct crypto4xx_core_device *core_dev = dev_get_drvdata(dev);
267
268 + ppc4xx_trng_remove(core_dev);
269 +
270 free_irq(core_dev->irq, dev);
271 irq_dispose_mapping(core_dev->irq);
272
273 @@ -1272,7 +1276,7 @@ MODULE_DEVICE_TABLE(of, crypto4xx_match);
274
275 static struct platform_driver crypto4xx_driver = {
276 .driver = {
277 - .name = "crypto4xx",
278 + .name = MODULE_NAME,
279 .of_match_table = crypto4xx_match,
280 },
281 .probe = crypto4xx_probe,
282 @@ -1284,4 +1288,3 @@ module_platform_driver(crypto4xx_driver);
283 MODULE_LICENSE("GPL");
284 MODULE_AUTHOR("James Hsiao <jhsiao@amcc.com>");
285 MODULE_DESCRIPTION("Driver for AMCC PPC4xx crypto accelerator");
286 -
287 diff --git a/drivers/crypto/amcc/crypto4xx_core.h b/drivers/crypto/amcc/crypto4xx_core.h
288 index bac0bde..ecfdcfe 100644
289 --- a/drivers/crypto/amcc/crypto4xx_core.h
290 +++ b/drivers/crypto/amcc/crypto4xx_core.h
291 @@ -24,6 +24,8 @@
292
293 #include <crypto/internal/hash.h>
294
295 +#define MODULE_NAME "crypto4xx"
296 +
297 #define PPC460SX_SDR0_SRST 0x201
298 #define PPC405EX_SDR0_SRST 0x200
299 #define PPC460EX_SDR0_SRST 0x201
300 @@ -72,6 +74,7 @@ struct crypto4xx_device {
301 char *name;
302 u64 ce_phy_address;
303 void __iomem *ce_base;
304 + void __iomem *trng_base;
305
306 void *pdr; /* base address of packet
307 descriptor ring */
308 @@ -106,6 +109,7 @@ struct crypto4xx_core_device {
309 struct device *device;
310 struct platform_device *ofdev;
311 struct crypto4xx_device *dev;
312 + struct hwrng *trng;
313 u32 int_status;
314 u32 irq;
315 struct tasklet_struct tasklet;
316 diff --git a/drivers/crypto/amcc/crypto4xx_reg_def.h b/drivers/crypto/amcc/crypto4xx_reg_def.h
317 index 5f5fbc0..46fe57c 100644
318 --- a/drivers/crypto/amcc/crypto4xx_reg_def.h
319 +++ b/drivers/crypto/amcc/crypto4xx_reg_def.h
320 @@ -125,6 +125,7 @@
321 #define PPC4XX_INTERRUPT_CLR 0x3ffff
322 #define PPC4XX_PRNG_CTRL_AUTO_EN 0x3
323 #define PPC4XX_DC_3DES_EN 1
324 +#define PPC4XX_TRNG_EN 0x00020000
325 #define PPC4XX_INT_DESCR_CNT 4
326 #define PPC4XX_INT_TIMEOUT_CNT 0
327 #define PPC4XX_INT_CFG 1
328 diff --git a/drivers/crypto/amcc/crypto4xx_trng.c b/drivers/crypto/amcc/crypto4xx_trng.c
329 new file mode 100644
330 index 0000000..677ca17
331 --- /dev/null
332 +++ b/drivers/crypto/amcc/crypto4xx_trng.c
333 @@ -0,0 +1,131 @@
334 +/*
335 + * Generic PowerPC 44x RNG driver
336 + *
337 + * Copyright 2011 IBM Corporation
338 + *
339 + * This program is free software; you can redistribute it and/or modify it
340 + * under the terms of the GNU General Public License as published by the
341 + * Free Software Foundation; version 2 of the License.
342 + */
343 +
344 +#include <linux/module.h>
345 +#include <linux/kernel.h>
346 +#include <linux/interrupt.h>
347 +#include <linux/platform_device.h>
348 +#include <linux/hw_random.h>
349 +#include <linux/delay.h>
350 +#include <linux/of_address.h>
351 +#include <linux/of_platform.h>
352 +#include <linux/io.h>
353 +
354 +#include "crypto4xx_core.h"
355 +#include "crypto4xx_trng.h"
356 +#include "crypto4xx_reg_def.h"
357 +
358 +#define PPC4XX_TRNG_CTRL 0x0008
359 +#define PPC4XX_TRNG_CTRL_DALM 0x20
360 +#define PPC4XX_TRNG_STAT 0x0004
361 +#define PPC4XX_TRNG_STAT_B 0x1
362 +#define PPC4XX_TRNG_DATA 0x0000
363 +
364 +static int ppc4xx_trng_data_present(struct hwrng *rng, int wait)
365 +{
366 + struct crypto4xx_device *dev = (void *)rng->priv;
367 + int busy, i, present = 0;
368 +
369 + for (i = 0; i < 20; i++) {
370 + busy = (in_le32(dev->trng_base + PPC4XX_TRNG_STAT) &
371 + PPC4XX_TRNG_STAT_B);
372 + if (!busy || !wait) {
373 + present = 1;
374 + break;
375 + }
376 + udelay(10);
377 + }
378 + return present;
379 +}
380 +
381 +static int ppc4xx_trng_data_read(struct hwrng *rng, u32 *data)
382 +{
383 + struct crypto4xx_device *dev = (void *)rng->priv;
384 + *data = in_le32(dev->trng_base + PPC4XX_TRNG_DATA);
385 + return 4;
386 +}
387 +
388 +static void ppc4xx_trng_enable(struct crypto4xx_device *dev, bool enable)
389 +{
390 + u32 device_ctrl;
391 +
392 + device_ctrl = readl(dev->ce_base + CRYPTO4XX_DEVICE_CTRL);
393 + if (enable)
394 + device_ctrl |= PPC4XX_TRNG_EN;
395 + else
396 + device_ctrl &= ~PPC4XX_TRNG_EN;
397 + writel(device_ctrl, dev->ce_base + CRYPTO4XX_DEVICE_CTRL);
398 +}
399 +
400 +static const struct of_device_id ppc4xx_trng_match[] = {
401 + { .compatible = "ppc4xx-rng", },
402 + { .compatible = "amcc,ppc460ex-rng", },
403 + { .compatible = "amcc,ppc440epx-rng", },
404 + {},
405 +};
406 +
407 +void ppc4xx_trng_probe(struct crypto4xx_core_device *core_dev)
408 +{
409 + struct crypto4xx_device *dev = core_dev->dev;
410 + struct device_node *trng = NULL;
411 + struct hwrng *rng = NULL;
412 + int err;
413 +
414 + /* Find the TRNG device node and map it */
415 + trng = of_find_matching_node(NULL, ppc4xx_trng_match);
416 + if (!trng || !of_device_is_available(trng))
417 + return;
418 +
419 + dev->trng_base = of_iomap(trng, 0);
420 + of_node_put(trng);
421 + if (!dev->trng_base)
422 + goto err_out;
423 +
424 + rng = kzalloc(sizeof(*rng), GFP_KERNEL);
425 + if (!rng)
426 + goto err_out;
427 +
428 + rng->name = MODULE_NAME;
429 + rng->data_present = ppc4xx_trng_data_present;
430 + rng->data_read = ppc4xx_trng_data_read;
431 + rng->priv = (unsigned long) dev;
432 + core_dev->trng = rng;
433 + ppc4xx_trng_enable(dev, true);
434 + out_le32(dev->trng_base + PPC4XX_TRNG_CTRL, PPC4XX_TRNG_CTRL_DALM);
435 + err = devm_hwrng_register(core_dev->device, core_dev->trng);
436 + if (err) {
437 + ppc4xx_trng_enable(dev, false);
438 + dev_err(core_dev->device, "failed to register hwrng (%d).\n",
439 + err);
440 + goto err_out;
441 + }
442 + return;
443 +
444 +err_out:
445 + of_node_put(trng);
446 + iounmap(dev->trng_base);
447 + kfree(rng);
448 + dev->trng_base = NULL;
449 + core_dev->trng = NULL;
450 +}
451 +
452 +void ppc4xx_trng_remove(struct crypto4xx_core_device *core_dev)
453 +{
454 + if (core_dev && core_dev->trng) {
455 + struct crypto4xx_device *dev = core_dev->dev;
456 +
457 + devm_hwrng_unregister(core_dev->device, core_dev->trng);
458 + ppc4xx_trng_enable(dev, false);
459 + iounmap(dev->trng_base);
460 + kfree(core_dev->trng);
461 + }
462 +}
463 +
464 +MODULE_ALIAS("ppc4xx_rng");
465 diff --git a/drivers/crypto/amcc/crypto4xx_trng.h b/drivers/crypto/amcc/crypto4xx_trng.h
466 new file mode 100644
467 index 0000000..931d225
468 --- /dev/null
469 +++ b/drivers/crypto/amcc/crypto4xx_trng.h
470 @@ -0,0 +1,34 @@
471 +/**
472 + * AMCC SoC PPC4xx Crypto Driver
473 + *
474 + * Copyright (c) 2008 Applied Micro Circuits Corporation.
475 + * All rights reserved. James Hsiao <jhsiao@amcc.com>
476 + *
477 + * This program is free software; you can redistribute it and/or modify
478 + * it under the terms of the GNU General Public License as published by
479 + * the Free Software Foundation; either version 2 of the License, or
480 + * (at your option) any later version.
481 + *
482 + * This program is distributed in the hope that it will be useful,
483 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
484 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
485 + * GNU General Public License for more details.
486 + *
487 + * This file defines the security context
488 + * associate format.
489 + */
490 +
491 +#ifndef __CRYPTO4XX_TRNG_H__
492 +#define __CRYPTO4XX_TRNG_H__
493 +
494 +#ifdef CONFIG_HW_RANDOM_PPC4XX
495 +void ppc4xx_trng_probe(struct crypto4xx_core_device *core_dev);
496 +void ppc4xx_trng_remove(struct crypto4xx_core_device *core_dev);
497 +#else
498 +static inline void ppc4xx_trng_probe(
499 + struct crypto4xx_device *dev __maybe_unused) { }
500 +static inline void ppc4xx_trng_remove(
501 + struct crypto4xx_device *dev __maybe_unused) { }
502 +#endif
503 +
504 +#endif
505 --
506 2.8.1
507