1 /*
2 * RNG driver for TX4939 Random Number Generators (RNG)
3 *
4 * Copyright (C) 2009 Atsushi Nemoto <anemo@mba.ocn.ne.jp>
5 *
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>
18
19 #define TX4939_RNG_RCSR 0x00000000
20 #define TX4939_RNG_ROR(n) (0x00000018 + (n) * 8)
21
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
26
27 struct tx4939_rng {
28 struct hwrng rng;
29 void __iomem *base;
30 u64 databuf[3];
31 unsigned int data_avail;
32 };
33
rng_io_start(void)34 static void rng_io_start(void)
35 {
36 #ifndef CONFIG_64BIT
37 /*
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.
42 */
43 local_irq_disable();
44 #endif
45 }
46
rng_io_end(void)47 static void rng_io_end(void)
48 {
49 #ifndef CONFIG_64BIT
50 local_irq_enable();
51 #endif
52 }
53
read_rng(void __iomem * base,unsigned int offset)54 static u64 read_rng(void __iomem *base, unsigned int offset)
55 {
56 return ____raw_readq(base + offset);
57 }
58
write_rng(u64 val,void __iomem * base,unsigned int offset)59 static void write_rng(u64 val, void __iomem *base, unsigned int offset)
60 {
61 return ____raw_writeq(val, base + offset);
62 }
63
tx4939_rng_data_present(struct hwrng * rng,int wait)64 static int tx4939_rng_data_present(struct hwrng *rng, int wait)
65 {
66 struct tx4939_rng *rngdev = container_of(rng, struct tx4939_rng, rng);
67 int i;
68
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;
87 }
88 rng_io_end();
89 if (!wait)
90 break;
91 /* 90 bus clock cycles by default for generation */
92 ndelay(90 * 5);
93 }
94 return rngdev->data_avail;
95 }
96
tx4939_rng_data_read(struct hwrng * rng,u32 * buffer)97 static int tx4939_rng_data_read(struct hwrng *rng, u32 *buffer)
98 {
99 struct tx4939_rng *rngdev = container_of(rng, struct tx4939_rng, rng);
100
101 rngdev->data_avail--;
102 *buffer = *((u32 *)&rngdev->databuf + rngdev->data_avail);
103 return sizeof(u32);
104 }
105
tx4939_rng_probe(struct platform_device * dev)106 static int __init tx4939_rng_probe(struct platform_device *dev)
107 {
108 struct tx4939_rng *rngdev;
109 struct resource *r;
110 int i;
111
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 rngdev->base = devm_request_and_ioremap(&dev->dev, r);
119 if (!rngdev->base)
120 return -EBUSY;
121
122 rngdev->rng.name = dev_name(&dev->dev);
123 rngdev->rng.data_present = tx4939_rng_data_present;
124 rngdev->rng.data_read = tx4939_rng_data_read;
125
126 rng_io_start();
127 /* Reset RNG */
128 write_rng(TX4939_RNG_RCSR_RST, rngdev->base, TX4939_RNG_RCSR);
129 write_rng(0, rngdev->base, TX4939_RNG_RCSR);
130 /* Start RNG */
131 write_rng(TX4939_RNG_RCSR_ST, rngdev->base, TX4939_RNG_RCSR);
132 rng_io_end();
133 /*
134 * Drop first two results. From the datasheet:
135 * The quality of the random numbers generated immediately
136 * after reset can be insufficient. Therefore, do not use
137 * random numbers obtained from the first and second
138 * generations; use the ones from the third or subsequent
139 * generation.
140 */
141 for (i = 0; i < 2; i++) {
142 rngdev->data_avail = 0;
143 if (!tx4939_rng_data_present(&rngdev->rng, 1))
144 return -EIO;
145 }
146
147 platform_set_drvdata(dev, rngdev);
148 return hwrng_register(&rngdev->rng);
149 }
150
tx4939_rng_remove(struct platform_device * dev)151 static int __exit tx4939_rng_remove(struct platform_device *dev)
152 {
153 struct tx4939_rng *rngdev = platform_get_drvdata(dev);
154
155 hwrng_unregister(&rngdev->rng);
156 platform_set_drvdata(dev, NULL);
157 return 0;
158 }
159
160 static struct platform_driver tx4939_rng_driver = {
161 .driver = {
162 .name = "tx4939-rng",
163 .owner = THIS_MODULE,
164 },
165 .remove = tx4939_rng_remove,
166 };
167
tx4939rng_init(void)168 static int __init tx4939rng_init(void)
169 {
170 return platform_driver_probe(&tx4939_rng_driver, tx4939_rng_probe);
171 }
172
tx4939rng_exit(void)173 static void __exit tx4939rng_exit(void)
174 {
175 platform_driver_unregister(&tx4939_rng_driver);
176 }
177
178 module_init(tx4939rng_init);
179 module_exit(tx4939rng_exit);
180
181 MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver for TX4939");
182 MODULE_LICENSE("GPL");
183