proc: use seq_puts()/seq_putc() where possible
[linux-2.6/next.git] / drivers / char / hw_random / pasemi-rng.c
bloba31c830ca8cd72f853298477ae55dd34192920bd
1 /*
2 * Copyright (C) 2006-2007 PA Semi, Inc
4 * Maintained by: Olof Johansson <olof@lixom.net>
6 * Driver for the PWRficient onchip rng
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/platform_device.h>
25 #include <linux/hw_random.h>
26 #include <linux/delay.h>
27 #include <linux/of_platform.h>
28 #include <asm/io.h>
30 #define SDCRNG_CTL_REG 0x00
31 #define SDCRNG_CTL_FVLD_M 0x0000f000
32 #define SDCRNG_CTL_FVLD_S 12
33 #define SDCRNG_CTL_KSZ 0x00000800
34 #define SDCRNG_CTL_RSRC_CRG 0x00000010
35 #define SDCRNG_CTL_RSRC_RRG 0x00000000
36 #define SDCRNG_CTL_CE 0x00000004
37 #define SDCRNG_CTL_RE 0x00000002
38 #define SDCRNG_CTL_DR 0x00000001
39 #define SDCRNG_CTL_SELECT_RRG_RNG (SDCRNG_CTL_RE | SDCRNG_CTL_RSRC_RRG)
40 #define SDCRNG_CTL_SELECT_CRG_RNG (SDCRNG_CTL_CE | SDCRNG_CTL_RSRC_CRG)
41 #define SDCRNG_VAL_REG 0x20
43 #define MODULE_NAME "pasemi_rng"
45 static int pasemi_rng_data_present(struct hwrng *rng, int wait)
47 void __iomem *rng_regs = (void __iomem *)rng->priv;
48 int data, i;
50 for (i = 0; i < 20; i++) {
51 data = (in_le32(rng_regs + SDCRNG_CTL_REG)
52 & SDCRNG_CTL_FVLD_M) ? 1 : 0;
53 if (data || !wait)
54 break;
55 udelay(10);
57 return data;
60 static int pasemi_rng_data_read(struct hwrng *rng, u32 *data)
62 void __iomem *rng_regs = (void __iomem *)rng->priv;
63 *data = in_le32(rng_regs + SDCRNG_VAL_REG);
64 return 4;
67 static int pasemi_rng_init(struct hwrng *rng)
69 void __iomem *rng_regs = (void __iomem *)rng->priv;
70 u32 ctl;
72 ctl = SDCRNG_CTL_DR | SDCRNG_CTL_SELECT_RRG_RNG | SDCRNG_CTL_KSZ;
73 out_le32(rng_regs + SDCRNG_CTL_REG, ctl);
74 out_le32(rng_regs + SDCRNG_CTL_REG, ctl & ~SDCRNG_CTL_DR);
76 return 0;
79 static void pasemi_rng_cleanup(struct hwrng *rng)
81 void __iomem *rng_regs = (void __iomem *)rng->priv;
82 u32 ctl;
84 ctl = SDCRNG_CTL_RE | SDCRNG_CTL_CE;
85 out_le32(rng_regs + SDCRNG_CTL_REG,
86 in_le32(rng_regs + SDCRNG_CTL_REG) & ~ctl);
89 static struct hwrng pasemi_rng = {
90 .name = MODULE_NAME,
91 .init = pasemi_rng_init,
92 .cleanup = pasemi_rng_cleanup,
93 .data_present = pasemi_rng_data_present,
94 .data_read = pasemi_rng_data_read,
97 static int __devinit rng_probe(struct platform_device *ofdev,
98 const struct of_device_id *match)
100 void __iomem *rng_regs;
101 struct device_node *rng_np = ofdev->dev.of_node;
102 struct resource res;
103 int err = 0;
105 err = of_address_to_resource(rng_np, 0, &res);
106 if (err)
107 return -ENODEV;
109 rng_regs = ioremap(res.start, 0x100);
111 if (!rng_regs)
112 return -ENOMEM;
114 pasemi_rng.priv = (unsigned long)rng_regs;
116 printk(KERN_INFO "Registering PA Semi RNG\n");
118 err = hwrng_register(&pasemi_rng);
120 if (err)
121 iounmap(rng_regs);
123 return err;
126 static int __devexit rng_remove(struct platform_device *dev)
128 void __iomem *rng_regs = (void __iomem *)pasemi_rng.priv;
130 hwrng_unregister(&pasemi_rng);
131 iounmap(rng_regs);
133 return 0;
136 static struct of_device_id rng_match[] = {
137 { .compatible = "1682m-rng", },
138 { .compatible = "pasemi,pwrficient-rng", },
139 { },
142 static struct of_platform_driver rng_driver = {
143 .driver = {
144 .name = "pasemi-rng",
145 .owner = THIS_MODULE,
146 .of_match_table = rng_match,
148 .probe = rng_probe,
149 .remove = rng_remove,
152 static int __init rng_init(void)
154 return of_register_platform_driver(&rng_driver);
156 module_init(rng_init);
158 static void __exit rng_exit(void)
160 of_unregister_platform_driver(&rng_driver);
162 module_exit(rng_exit);
164 MODULE_LICENSE("GPL");
165 MODULE_AUTHOR("Egor Martovetsky <egor@pasemi.com>");
166 MODULE_DESCRIPTION("H/W RNG driver for PA Semi processor");