OMAPDSS: VENC: fix NULL pointer dereference in DSS2 VENC sysfs debug attr on OMAP4
[zen-stable.git] / drivers / char / hw_random / tx4939-rng.c
blob0bc0cb70210b4bb04b7869c9ffd7ac198950bba7
1 /*
2 * RNG driver for TX4939 Random Number Generators (RNG)
4 * Copyright (C) 2009 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
8 * for more details.
9 */
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/delay.h>
14 #include <linux/io.h>
15 #include <linux/platform_device.h>
16 #include <linux/hw_random.h>
17 #include <linux/gfp.h>
19 #define TX4939_RNG_RCSR 0x00000000
20 #define TX4939_RNG_ROR(n) (0x00000018 + (n) * 8)
22 #define TX4939_RNG_RCSR_INTE 0x00000008
23 #define TX4939_RNG_RCSR_RST 0x00000004
24 #define TX4939_RNG_RCSR_FIN 0x00000002
25 #define TX4939_RNG_RCSR_ST 0x00000001
27 struct tx4939_rng {
28 struct hwrng rng;
29 void __iomem *base;
30 u64 databuf[3];
31 unsigned int data_avail;
34 static void rng_io_start(void)
36 #ifndef CONFIG_64BIT
38 * readq is reading a 64-bit register using a 64-bit load. On
39 * a 32-bit kernel however interrupts or any other processor
40 * exception would clobber the upper 32-bit of the processor
41 * register so interrupts need to be disabled.
43 local_irq_disable();
44 #endif
47 static void rng_io_end(void)
49 #ifndef CONFIG_64BIT
50 local_irq_enable();
51 #endif
54 static u64 read_rng(void __iomem *base, unsigned int offset)
56 return ____raw_readq(base + offset);
59 static void write_rng(u64 val, void __iomem *base, unsigned int offset)
61 return ____raw_writeq(val, base + offset);
64 static int tx4939_rng_data_present(struct hwrng *rng, int wait)
66 struct tx4939_rng *rngdev = container_of(rng, struct tx4939_rng, rng);
67 int i;
69 if (rngdev->data_avail)
70 return rngdev->data_avail;
71 for (i = 0; i < 20; i++) {
72 rng_io_start();
73 if (!(read_rng(rngdev->base, TX4939_RNG_RCSR)
74 & TX4939_RNG_RCSR_ST)) {
75 rngdev->databuf[0] =
76 read_rng(rngdev->base, TX4939_RNG_ROR(0));
77 rngdev->databuf[1] =
78 read_rng(rngdev->base, TX4939_RNG_ROR(1));
79 rngdev->databuf[2] =
80 read_rng(rngdev->base, TX4939_RNG_ROR(2));
81 rngdev->data_avail =
82 sizeof(rngdev->databuf) / sizeof(u32);
83 /* Start RNG */
84 write_rng(TX4939_RNG_RCSR_ST,
85 rngdev->base, TX4939_RNG_RCSR);
86 wait = 0;
88 rng_io_end();
89 if (!wait)
90 break;
91 /* 90 bus clock cycles by default for generation */
92 ndelay(90 * 5);
94 return rngdev->data_avail;
97 static int tx4939_rng_data_read(struct hwrng *rng, u32 *buffer)
99 struct tx4939_rng *rngdev = container_of(rng, struct tx4939_rng, rng);
101 rngdev->data_avail--;
102 *buffer = *((u32 *)&rngdev->databuf + rngdev->data_avail);
103 return sizeof(u32);
106 static int __init tx4939_rng_probe(struct platform_device *dev)
108 struct tx4939_rng *rngdev;
109 struct resource *r;
110 int i;
112 r = platform_get_resource(dev, IORESOURCE_MEM, 0);
113 if (!r)
114 return -EBUSY;
115 rngdev = devm_kzalloc(&dev->dev, sizeof(*rngdev), GFP_KERNEL);
116 if (!rngdev)
117 return -ENOMEM;
118 if (!devm_request_mem_region(&dev->dev, r->start, resource_size(r),
119 dev_name(&dev->dev)))
120 return -EBUSY;
121 rngdev->base = devm_ioremap(&dev->dev, r->start, resource_size(r));
122 if (!rngdev->base)
123 return -EBUSY;
125 rngdev->rng.name = dev_name(&dev->dev);
126 rngdev->rng.data_present = tx4939_rng_data_present;
127 rngdev->rng.data_read = tx4939_rng_data_read;
129 rng_io_start();
130 /* Reset RNG */
131 write_rng(TX4939_RNG_RCSR_RST, rngdev->base, TX4939_RNG_RCSR);
132 write_rng(0, rngdev->base, TX4939_RNG_RCSR);
133 /* Start RNG */
134 write_rng(TX4939_RNG_RCSR_ST, rngdev->base, TX4939_RNG_RCSR);
135 rng_io_end();
137 * Drop first two results. From the datasheet:
138 * The quality of the random numbers generated immediately
139 * after reset can be insufficient. Therefore, do not use
140 * random numbers obtained from the first and second
141 * generations; use the ones from the third or subsequent
142 * generation.
144 for (i = 0; i < 2; i++) {
145 rngdev->data_avail = 0;
146 if (!tx4939_rng_data_present(&rngdev->rng, 1))
147 return -EIO;
150 platform_set_drvdata(dev, rngdev);
151 return hwrng_register(&rngdev->rng);
154 static int __exit tx4939_rng_remove(struct platform_device *dev)
156 struct tx4939_rng *rngdev = platform_get_drvdata(dev);
158 hwrng_unregister(&rngdev->rng);
159 platform_set_drvdata(dev, NULL);
160 return 0;
163 static struct platform_driver tx4939_rng_driver = {
164 .driver = {
165 .name = "tx4939-rng",
166 .owner = THIS_MODULE,
168 .remove = tx4939_rng_remove,
171 static int __init tx4939rng_init(void)
173 return platform_driver_probe(&tx4939_rng_driver, tx4939_rng_probe);
176 static void __exit tx4939rng_exit(void)
178 platform_driver_unregister(&tx4939_rng_driver);
181 module_init(tx4939rng_init);
182 module_exit(tx4939rng_exit);
184 MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver for TX4939");
185 MODULE_LICENSE("GPL");