blob: de473ef3882bad54f192f59dcd9ae59200920ca0 [file] [log] [blame]
Atsushi Nemoto049a9472009-06-02 23:54:21 +09001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/gfp.h>
Atsushi Nemoto049a9472009-06-02 23:54:21 +090018
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
27struct tx4939_rng {
28 struct hwrng rng;
29 void __iomem *base;
30 u64 databuf[3];
31 unsigned int data_avail;
32};
33
34static 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
47static void rng_io_end(void)
48{
49#ifndef CONFIG_64BIT
50 local_irq_enable();
51#endif
52}
53
54static u64 read_rng(void __iomem *base, unsigned int offset)
55{
56 return ____raw_readq(base + offset);
57}
58
59static void write_rng(u64 val, void __iomem *base, unsigned int offset)
60{
61 return ____raw_writeq(val, base + offset);
62}
63
64static 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
97static 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
106static 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;
Julia Lawalld0b03c52011-12-27 15:01:30 +0100118 rngdev->base = devm_request_and_ioremap(&dev->dev, r);
Atsushi Nemoto049a9472009-06-02 23:54:21 +0900119 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
151static 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
160static struct platform_driver tx4939_rng_driver = {
161 .driver = {
162 .name = "tx4939-rng",
163 .owner = THIS_MODULE,
164 },
165 .remove = tx4939_rng_remove,
166};
167
168static int __init tx4939rng_init(void)
169{
170 return platform_driver_probe(&tx4939_rng_driver, tx4939_rng_probe);
171}
172
173static void __exit tx4939rng_exit(void)
174{
175 platform_driver_unregister(&tx4939_rng_driver);
176}
177
178module_init(tx4939rng_init);
179module_exit(tx4939rng_exit);
180
181MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver for TX4939");
182MODULE_LICENSE("GPL");