aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/char
diff options
context:
space:
mode:
authorJosh Boyer <jwboyer@linux.vnet.ibm.com>2011-06-27 03:34:54 -0400
committerHerbert Xu <herbert@gondor.apana.org.au>2011-06-29 19:44:06 -0400
commit52527cf3f95d437b02f54437d5c3d7fda06474d1 (patch)
treef218c25e6cb3d1aefb6040a2d919829ae52744ce /drivers/char
parentc5697462ae94693764e468b701c616bbbd6f951c (diff)
hwrng: ppc4xx - add support for ppc4xx TRNG
Various PowerPC 4xx SoCs contain a TRNG embedded in the Security function. This adds a device driver for that TRNG. Signed-off-by: Josh Boyer <jwboyer@linux.vnet.ibm.com> Acked-by: Matt Mackall <mpm@selenic.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'drivers/char')
-rw-r--r--drivers/char/hw_random/Kconfig12
-rw-r--r--drivers/char/hw_random/Makefile1
-rw-r--r--drivers/char/hw_random/ppc4xx-rng.c156
3 files changed, 169 insertions, 0 deletions
diff --git a/drivers/char/hw_random/Kconfig b/drivers/char/hw_random/Kconfig
index a60043b3e409..1d2ebc7a4947 100644
--- a/drivers/char/hw_random/Kconfig
+++ b/drivers/char/hw_random/Kconfig
@@ -210,3 +210,15 @@ config HW_RANDOM_PICOXCELL
210 module will be called picoxcell-rng. 210 module will be called picoxcell-rng.
211 211
212 If unsure, say Y. 212 If unsure, say Y.
213
214config HW_RANDOM_PPC4XX
215 tristate "PowerPC 4xx generic true random number generator support"
216 depends on HW_RANDOM && PPC && 4xx
217 ---help---
218 This driver provides the kernel-side support for the TRNG hardware
219 found in the security function of some PowerPC 4xx SoCs.
220
221 To compile this driver as a module, choose M here: the
222 module will be called ppc4xx-rng.
223
224 If unsure, say N.
diff --git a/drivers/char/hw_random/Makefile b/drivers/char/hw_random/Makefile
index 3db4eb8b19c0..c88f244c8a71 100644
--- a/drivers/char/hw_random/Makefile
+++ b/drivers/char/hw_random/Makefile
@@ -20,3 +20,4 @@ obj-$(CONFIG_HW_RANDOM_MXC_RNGA) += mxc-rnga.o
20obj-$(CONFIG_HW_RANDOM_OCTEON) += octeon-rng.o 20obj-$(CONFIG_HW_RANDOM_OCTEON) += octeon-rng.o
21obj-$(CONFIG_HW_RANDOM_NOMADIK) += nomadik-rng.o 21obj-$(CONFIG_HW_RANDOM_NOMADIK) += nomadik-rng.o
22obj-$(CONFIG_HW_RANDOM_PICOXCELL) += picoxcell-rng.o 22obj-$(CONFIG_HW_RANDOM_PICOXCELL) += picoxcell-rng.o
23obj-$(CONFIG_HW_RANDOM_PPC4XX) += ppc4xx-rng.o
diff --git a/drivers/char/hw_random/ppc4xx-rng.c b/drivers/char/hw_random/ppc4xx-rng.c
new file mode 100644
index 000000000000..b8afa6a4ff67
--- /dev/null
+++ b/drivers/char/hw_random/ppc4xx-rng.c
@@ -0,0 +1,156 @@
1/*
2 * Generic PowerPC 44x RNG driver
3 *
4 * Copyright 2011 IBM Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; version 2 of the License.
9 */
10
11#include <linux/module.h>
12#include <linux/kernel.h>
13#include <linux/platform_device.h>
14#include <linux/hw_random.h>
15#include <linux/delay.h>
16#include <linux/of_platform.h>
17#include <asm/io.h>
18
19#define PPC4XX_TRNG_DEV_CTRL 0x60080
20
21#define PPC4XX_TRNGE 0x00020000
22#define PPC4XX_TRNG_CTRL 0x0008
23#define PPC4XX_TRNG_CTRL_DALM 0x20
24#define PPC4XX_TRNG_STAT 0x0004
25#define PPC4XX_TRNG_STAT_B 0x1
26#define PPC4XX_TRNG_DATA 0x0000
27
28#define MODULE_NAME "ppc4xx_rng"
29
30static int ppc4xx_rng_data_present(struct hwrng *rng, int wait)
31{
32 void __iomem *rng_regs = (void __iomem *) rng->priv;
33 int busy, i, present = 0;
34
35 for (i = 0; i < 20; i++) {
36 busy = (in_le32(rng_regs + PPC4XX_TRNG_STAT) & PPC4XX_TRNG_STAT_B);
37 if (!busy || !wait) {
38 present = 1;
39 break;
40 }
41 udelay(10);
42 }
43 return present;
44}
45
46static int ppc4xx_rng_data_read(struct hwrng *rng, u32 *data)
47{
48 void __iomem *rng_regs = (void __iomem *) rng->priv;
49 *data = in_le32(rng_regs + PPC4XX_TRNG_DATA);
50 return 4;
51}
52
53static int ppc4xx_rng_enable(int enable)
54{
55 struct device_node *ctrl;
56 void __iomem *ctrl_reg;
57 int err = 0;
58 u32 val;
59
60 /* Find the main crypto device node and map it to turn the TRNG on */
61 ctrl = of_find_compatible_node(NULL, NULL, "amcc,ppc4xx-crypto");
62 if (!ctrl)
63 return -ENODEV;
64
65 ctrl_reg = of_iomap(ctrl, 0);
66 if (!ctrl_reg) {
67 err = -ENODEV;
68 goto out;
69 }
70
71 val = in_le32(ctrl_reg + PPC4XX_TRNG_DEV_CTRL);
72
73 if (enable)
74 val |= PPC4XX_TRNGE;
75 else
76 val = val & ~PPC4XX_TRNGE;
77
78 out_le32(ctrl_reg + PPC4XX_TRNG_DEV_CTRL, val);
79 iounmap(ctrl_reg);
80
81out:
82 of_node_put(ctrl);
83
84 return err;
85}
86
87static struct hwrng ppc4xx_rng = {
88 .name = MODULE_NAME,
89 .data_present = ppc4xx_rng_data_present,
90 .data_read = ppc4xx_rng_data_read,
91};
92
93static int __devinit ppc4xx_rng_probe(struct platform_device *dev)
94{
95 void __iomem *rng_regs;
96 int err = 0;
97
98 rng_regs = of_iomap(dev->dev.of_node, 0);
99 if (!rng_regs)
100 return -ENODEV;
101
102 err = ppc4xx_rng_enable(1);
103 if (err)
104 return err;
105
106 out_le32(rng_regs + PPC4XX_TRNG_CTRL, PPC4XX_TRNG_CTRL_DALM);
107 ppc4xx_rng.priv = (unsigned long) rng_regs;
108
109 err = hwrng_register(&ppc4xx_rng);
110
111 return err;
112}
113
114static int __devexit ppc4xx_rng_remove(struct platform_device *dev)
115{
116 void __iomem *rng_regs = (void __iomem *) ppc4xx_rng.priv;
117
118 hwrng_unregister(&ppc4xx_rng);
119 ppc4xx_rng_enable(0);
120 iounmap(rng_regs);
121
122 return 0;
123}
124
125static struct of_device_id ppc4xx_rng_match[] = {
126 { .compatible = "ppc4xx-rng", },
127 { .compatible = "amcc,ppc460ex-rng", },
128 { .compatible = "amcc,ppc440epx-rng", },
129 {},
130};
131
132static struct platform_driver ppc4xx_rng_driver = {
133 .driver = {
134 .name = MODULE_NAME,
135 .owner = THIS_MODULE,
136 .of_match_table = ppc4xx_rng_match,
137 },
138 .probe = ppc4xx_rng_probe,
139 .remove = ppc4xx_rng_remove,
140};
141
142static int __init ppc4xx_rng_init(void)
143{
144 return platform_driver_register(&ppc4xx_rng_driver);
145}
146module_init(ppc4xx_rng_init);
147
148static void __exit ppc4xx_rng_exit(void)
149{
150 platform_driver_unregister(&ppc4xx_rng_driver);
151}
152module_exit(ppc4xx_rng_exit);
153
154MODULE_LICENSE("GPL");
155MODULE_AUTHOR("Josh Boyer <jwboyer@linux.vnet.ibm.com>");
156MODULE_DESCRIPTION("HW RNG driver for PPC 4xx processors");