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
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.
10 Signed-off-by: Christian Lamparter <chunkeey@googlemail.com>
11 Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
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
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
36 -config HW_RANDOM_PPC4XX
37 - tristate "PowerPC 4xx generic true random number generator support"
38 - depends on PPC && 4xx
41 - This driver provides the kernel-side support for the TRNG hardware
42 - found in the security function of some PowerPC 4xx SoCs.
44 - To compile this driver as a module, choose M here: the
45 - module will be called ppc4xx-rng.
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
71 - * Generic PowerPC 44x RNG driver
73 - * Copyright 2011 IBM Corporation
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.
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>
89 -#define PPC4XX_TRNG_DEV_CTRL 0x60080
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
98 -#define MODULE_NAME "ppc4xx_rng"
100 -static int ppc4xx_rng_data_present(struct hwrng *rng, int wait)
102 - void __iomem *rng_regs = (void __iomem *) rng->priv;
103 - int busy, i, present = 0;
105 - for (i = 0; i < 20; i++) {
106 - busy = (in_le32(rng_regs + PPC4XX_TRNG_STAT) & PPC4XX_TRNG_STAT_B);
107 - if (!busy || !wait) {
116 -static int ppc4xx_rng_data_read(struct hwrng *rng, u32 *data)
118 - void __iomem *rng_regs = (void __iomem *) rng->priv;
119 - *data = in_le32(rng_regs + PPC4XX_TRNG_DATA);
123 -static int ppc4xx_rng_enable(int enable)
125 - struct device_node *ctrl;
126 - void __iomem *ctrl_reg;
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");
135 - ctrl_reg = of_iomap(ctrl, 0);
141 - val = in_le32(ctrl_reg + PPC4XX_TRNG_DEV_CTRL);
144 - val |= PPC4XX_TRNGE;
146 - val = val & ~PPC4XX_TRNGE;
148 - out_le32(ctrl_reg + PPC4XX_TRNG_DEV_CTRL, val);
157 -static struct hwrng ppc4xx_rng = {
158 - .name = MODULE_NAME,
159 - .data_present = ppc4xx_rng_data_present,
160 - .data_read = ppc4xx_rng_data_read,
163 -static int ppc4xx_rng_probe(struct platform_device *dev)
165 - void __iomem *rng_regs;
168 - rng_regs = of_iomap(dev->dev.of_node, 0);
172 - err = ppc4xx_rng_enable(1);
176 - out_le32(rng_regs + PPC4XX_TRNG_CTRL, PPC4XX_TRNG_CTRL_DALM);
177 - ppc4xx_rng.priv = (unsigned long) rng_regs;
179 - err = hwrng_register(&ppc4xx_rng);
184 -static int ppc4xx_rng_remove(struct platform_device *dev)
186 - void __iomem *rng_regs = (void __iomem *) ppc4xx_rng.priv;
188 - hwrng_unregister(&ppc4xx_rng);
189 - ppc4xx_rng_enable(0);
195 -static const struct of_device_id ppc4xx_rng_match[] = {
196 - { .compatible = "ppc4xx-rng", },
197 - { .compatible = "amcc,ppc460ex-rng", },
198 - { .compatible = "amcc,ppc440epx-rng", },
201 -MODULE_DEVICE_TABLE(of, ppc4xx_rng_match);
203 -static struct platform_driver ppc4xx_rng_driver = {
205 - .name = MODULE_NAME,
206 - .of_match_table = ppc4xx_rng_match,
208 - .probe = ppc4xx_rng_probe,
209 - .remove = ppc4xx_rng_remove,
212 -module_platform_driver(ppc4xx_rng_driver);
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
223 This option allows you to have support for AMCC crypto acceleration.
225 +config HW_RANDOM_PPC4XX
226 + bool "PowerPC 4xx generic true random number generator support"
227 + depends on CRYPTO_DEV_PPC4XX && HW_RANDOM
230 + This option provides the kernel-side support for the TRNG hardware
231 + found in the security function of some PowerPC 4xx SoCs.
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
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
249 #include "crypto4xx_reg_def.h"
250 #include "crypto4xx_core.h"
251 #include "crypto4xx_sa.h"
252 +#include "crypto4xx_trng.h"
254 #define PPC4XX_SEC_VERSION_STR "0.5"
256 @@ -1225,6 +1226,7 @@ static int crypto4xx_probe(struct platform_device *ofdev)
260 + ppc4xx_trng_probe(core_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);
268 + ppc4xx_trng_remove(core_dev);
270 free_irq(core_dev->irq, dev);
271 irq_dispose_mapping(core_dev->irq);
273 @@ -1272,7 +1276,7 @@ MODULE_DEVICE_TABLE(of, crypto4xx_match);
275 static struct platform_driver crypto4xx_driver = {
277 - .name = "crypto4xx",
278 + .name = MODULE_NAME,
279 .of_match_table = crypto4xx_match,
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");
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
293 #include <crypto/internal/hash.h>
295 +#define MODULE_NAME "crypto4xx"
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 {
303 void __iomem *ce_base;
304 + void __iomem *trng_base;
306 void *pdr; /* base address of packet
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;
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
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
330 index 0000000..677ca17
332 +++ b/drivers/crypto/amcc/crypto4xx_trng.c
335 + * Generic PowerPC 44x RNG driver
337 + * Copyright 2011 IBM Corporation
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.
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>
354 +#include "crypto4xx_core.h"
355 +#include "crypto4xx_trng.h"
356 +#include "crypto4xx_reg_def.h"
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
364 +static int ppc4xx_trng_data_present(struct hwrng *rng, int wait)
366 + struct crypto4xx_device *dev = (void *)rng->priv;
367 + int busy, i, present = 0;
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) {
381 +static int ppc4xx_trng_data_read(struct hwrng *rng, u32 *data)
383 + struct crypto4xx_device *dev = (void *)rng->priv;
384 + *data = in_le32(dev->trng_base + PPC4XX_TRNG_DATA);
388 +static void ppc4xx_trng_enable(struct crypto4xx_device *dev, bool enable)
392 + device_ctrl = readl(dev->ce_base + CRYPTO4XX_DEVICE_CTRL);
394 + device_ctrl |= PPC4XX_TRNG_EN;
396 + device_ctrl &= ~PPC4XX_TRNG_EN;
397 + writel(device_ctrl, dev->ce_base + CRYPTO4XX_DEVICE_CTRL);
400 +static const struct of_device_id ppc4xx_trng_match[] = {
401 + { .compatible = "ppc4xx-rng", },
402 + { .compatible = "amcc,ppc460ex-rng", },
403 + { .compatible = "amcc,ppc440epx-rng", },
407 +void ppc4xx_trng_probe(struct crypto4xx_core_device *core_dev)
409 + struct crypto4xx_device *dev = core_dev->dev;
410 + struct device_node *trng = NULL;
411 + struct hwrng *rng = NULL;
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))
419 + dev->trng_base = of_iomap(trng, 0);
421 + if (!dev->trng_base)
424 + rng = kzalloc(sizeof(*rng), GFP_KERNEL);
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);
437 + ppc4xx_trng_enable(dev, false);
438 + dev_err(core_dev->device, "failed to register hwrng (%d).\n",
446 + iounmap(dev->trng_base);
448 + dev->trng_base = NULL;
449 + core_dev->trng = NULL;
452 +void ppc4xx_trng_remove(struct crypto4xx_core_device *core_dev)
454 + if (core_dev && core_dev->trng) {
455 + struct crypto4xx_device *dev = core_dev->dev;
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);
464 +MODULE_ALIAS("ppc4xx_rng");
465 diff --git a/drivers/crypto/amcc/crypto4xx_trng.h b/drivers/crypto/amcc/crypto4xx_trng.h
467 index 0000000..931d225
469 +++ b/drivers/crypto/amcc/crypto4xx_trng.h
472 + * AMCC SoC PPC4xx Crypto Driver
474 + * Copyright (c) 2008 Applied Micro Circuits Corporation.
475 + * All rights reserved. James Hsiao <jhsiao@amcc.com>
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.
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.
487 + * This file defines the security context
488 + * associate format.
491 +#ifndef __CRYPTO4XX_TRNG_H__
492 +#define __CRYPTO4XX_TRNG_H__
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);
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) { }