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
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/delay.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
31 unsigned int data_avail
;
34 static void rng_io_start(void)
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.
47 static void rng_io_end(void)
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
);
69 if (rngdev
->data_avail
)
70 return rngdev
->data_avail
;
71 for (i
= 0; i
< 20; i
++) {
73 if (!(read_rng(rngdev
->base
, TX4939_RNG_RCSR
)
74 & TX4939_RNG_RCSR_ST
)) {
76 read_rng(rngdev
->base
, TX4939_RNG_ROR(0));
78 read_rng(rngdev
->base
, TX4939_RNG_ROR(1));
80 read_rng(rngdev
->base
, TX4939_RNG_ROR(2));
82 sizeof(rngdev
->databuf
) / sizeof(u32
);
84 write_rng(TX4939_RNG_RCSR_ST
,
85 rngdev
->base
, TX4939_RNG_RCSR
);
91 /* 90 bus clock cycles by default for generation */
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
);
106 static int __init
tx4939_rng_probe(struct platform_device
*dev
)
108 struct tx4939_rng
*rngdev
;
112 r
= platform_get_resource(dev
, IORESOURCE_MEM
, 0);
115 rngdev
= devm_kzalloc(&dev
->dev
, sizeof(*rngdev
), GFP_KERNEL
);
118 if (!devm_request_mem_region(&dev
->dev
, r
->start
, resource_size(r
),
119 dev_name(&dev
->dev
)))
121 rngdev
->base
= devm_ioremap(&dev
->dev
, r
->start
, resource_size(r
));
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
;
131 write_rng(TX4939_RNG_RCSR_RST
, rngdev
->base
, TX4939_RNG_RCSR
);
132 write_rng(0, rngdev
->base
, TX4939_RNG_RCSR
);
134 write_rng(TX4939_RNG_RCSR_ST
, rngdev
->base
, TX4939_RNG_RCSR
);
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
144 for (i
= 0; i
< 2; i
++) {
145 rngdev
->data_avail
= 0;
146 if (!tx4939_rng_data_present(&rngdev
->rng
, 1))
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
);
163 static struct platform_driver tx4939_rng_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");