dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / drivers / char / hw_random / ppc4xx-rng.c
blobc0db4387d2e20e6cb75993b8aecd16a5249f47d2
1 /*
2 * Generic PowerPC 44x RNG driver
4 * Copyright 2011 IBM Corporation
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 */
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_address.h>
17 #include <linux/of_platform.h>
18 #include <asm/io.h>
20 #define PPC4XX_TRNG_DEV_CTRL 0x60080
22 #define PPC4XX_TRNGE 0x00020000
23 #define PPC4XX_TRNG_CTRL 0x0008
24 #define PPC4XX_TRNG_CTRL_DALM 0x20
25 #define PPC4XX_TRNG_STAT 0x0004
26 #define PPC4XX_TRNG_STAT_B 0x1
27 #define PPC4XX_TRNG_DATA 0x0000
29 #define MODULE_NAME "ppc4xx_rng"
31 static int ppc4xx_rng_data_present(struct hwrng *rng, int wait)
33 void __iomem *rng_regs = (void __iomem *) rng->priv;
34 int busy, i, present = 0;
36 for (i = 0; i < 20; i++) {
37 busy = (in_le32(rng_regs + PPC4XX_TRNG_STAT) & PPC4XX_TRNG_STAT_B);
38 if (!busy || !wait) {
39 present = 1;
40 break;
42 udelay(10);
44 return present;
47 static int ppc4xx_rng_data_read(struct hwrng *rng, u32 *data)
49 void __iomem *rng_regs = (void __iomem *) rng->priv;
50 *data = in_le32(rng_regs + PPC4XX_TRNG_DATA);
51 return 4;
54 static int ppc4xx_rng_enable(int enable)
56 struct device_node *ctrl;
57 void __iomem *ctrl_reg;
58 int err = 0;
59 u32 val;
61 /* Find the main crypto device node and map it to turn the TRNG on */
62 ctrl = of_find_compatible_node(NULL, NULL, "amcc,ppc4xx-crypto");
63 if (!ctrl)
64 return -ENODEV;
66 ctrl_reg = of_iomap(ctrl, 0);
67 if (!ctrl_reg) {
68 err = -ENODEV;
69 goto out;
72 val = in_le32(ctrl_reg + PPC4XX_TRNG_DEV_CTRL);
74 if (enable)
75 val |= PPC4XX_TRNGE;
76 else
77 val = val & ~PPC4XX_TRNGE;
79 out_le32(ctrl_reg + PPC4XX_TRNG_DEV_CTRL, val);
80 iounmap(ctrl_reg);
82 out:
83 of_node_put(ctrl);
85 return err;
88 static struct hwrng ppc4xx_rng = {
89 .name = MODULE_NAME,
90 .data_present = ppc4xx_rng_data_present,
91 .data_read = ppc4xx_rng_data_read,
94 static int ppc4xx_rng_probe(struct platform_device *dev)
96 void __iomem *rng_regs;
97 int err = 0;
99 rng_regs = of_iomap(dev->dev.of_node, 0);
100 if (!rng_regs)
101 return -ENODEV;
103 err = ppc4xx_rng_enable(1);
104 if (err)
105 return err;
107 out_le32(rng_regs + PPC4XX_TRNG_CTRL, PPC4XX_TRNG_CTRL_DALM);
108 ppc4xx_rng.priv = (unsigned long) rng_regs;
110 err = hwrng_register(&ppc4xx_rng);
112 return err;
115 static int ppc4xx_rng_remove(struct platform_device *dev)
117 void __iomem *rng_regs = (void __iomem *) ppc4xx_rng.priv;
119 hwrng_unregister(&ppc4xx_rng);
120 ppc4xx_rng_enable(0);
121 iounmap(rng_regs);
123 return 0;
126 static const struct of_device_id ppc4xx_rng_match[] = {
127 { .compatible = "ppc4xx-rng", },
128 { .compatible = "amcc,ppc460ex-rng", },
129 { .compatible = "amcc,ppc440epx-rng", },
132 MODULE_DEVICE_TABLE(of, ppc4xx_rng_match);
134 static struct platform_driver ppc4xx_rng_driver = {
135 .driver = {
136 .name = MODULE_NAME,
137 .of_match_table = ppc4xx_rng_match,
139 .probe = ppc4xx_rng_probe,
140 .remove = ppc4xx_rng_remove,
143 module_platform_driver(ppc4xx_rng_driver);
145 MODULE_LICENSE("GPL");
146 MODULE_AUTHOR("Josh Boyer <jwboyer@linux.vnet.ibm.com>");
147 MODULE_DESCRIPTION("HW RNG driver for PPC 4xx processors");