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>
18 #define TX4939_RNG_RCSR 0x00000000
19 #define TX4939_RNG_ROR(n) (0x00000018 + (n) * 8)
21 #define TX4939_RNG_RCSR_INTE 0x00000008
22 #define TX4939_RNG_RCSR_RST 0x00000004
23 #define TX4939_RNG_RCSR_FIN 0x00000002
24 #define TX4939_RNG_RCSR_ST 0x00000001
30 unsigned int data_avail
;
33 static void rng_io_start(void)
37 * readq is reading a 64-bit register using a 64-bit load. On
38 * a 32-bit kernel however interrupts or any other processor
39 * exception would clobber the upper 32-bit of the processor
40 * register so interrupts need to be disabled.
46 static void rng_io_end(void)
53 static u64
read_rng(void __iomem
*base
, unsigned int offset
)
55 return ____raw_readq(base
+ offset
);
58 static void write_rng(u64 val
, void __iomem
*base
, unsigned int offset
)
60 return ____raw_writeq(val
, base
+ offset
);
63 static int tx4939_rng_data_present(struct hwrng
*rng
, int wait
)
65 struct tx4939_rng
*rngdev
= container_of(rng
, struct tx4939_rng
, rng
);
68 if (rngdev
->data_avail
)
69 return rngdev
->data_avail
;
70 for (i
= 0; i
< 20; i
++) {
72 if (!(read_rng(rngdev
->base
, TX4939_RNG_RCSR
)
73 & TX4939_RNG_RCSR_ST
)) {
75 read_rng(rngdev
->base
, TX4939_RNG_ROR(0));
77 read_rng(rngdev
->base
, TX4939_RNG_ROR(1));
79 read_rng(rngdev
->base
, TX4939_RNG_ROR(2));
81 sizeof(rngdev
->databuf
) / sizeof(u32
);
83 write_rng(TX4939_RNG_RCSR_ST
,
84 rngdev
->base
, TX4939_RNG_RCSR
);
90 /* 90 bus clock cycles by default for generation */
93 return rngdev
->data_avail
;
96 static int tx4939_rng_data_read(struct hwrng
*rng
, u32
*buffer
)
98 struct tx4939_rng
*rngdev
= container_of(rng
, struct tx4939_rng
, rng
);
100 rngdev
->data_avail
--;
101 *buffer
= *((u32
*)&rngdev
->databuf
+ rngdev
->data_avail
);
105 static int __init
tx4939_rng_probe(struct platform_device
*dev
)
107 struct tx4939_rng
*rngdev
;
111 r
= platform_get_resource(dev
, IORESOURCE_MEM
, 0);
114 rngdev
= devm_kzalloc(&dev
->dev
, sizeof(*rngdev
), GFP_KERNEL
);
117 if (!devm_request_mem_region(&dev
->dev
, r
->start
, resource_size(r
),
118 dev_name(&dev
->dev
)))
120 rngdev
->base
= devm_ioremap(&dev
->dev
, r
->start
, resource_size(r
));
124 rngdev
->rng
.name
= dev_name(&dev
->dev
);
125 rngdev
->rng
.data_present
= tx4939_rng_data_present
;
126 rngdev
->rng
.data_read
= tx4939_rng_data_read
;
130 write_rng(TX4939_RNG_RCSR_RST
, rngdev
->base
, TX4939_RNG_RCSR
);
131 write_rng(0, rngdev
->base
, TX4939_RNG_RCSR
);
133 write_rng(TX4939_RNG_RCSR_ST
, rngdev
->base
, TX4939_RNG_RCSR
);
136 * Drop first two results. From the datasheet:
137 * The quality of the random numbers generated immediately
138 * after reset can be insufficient. Therefore, do not use
139 * random numbers obtained from the first and second
140 * generations; use the ones from the third or subsequent
143 for (i
= 0; i
< 2; i
++) {
144 rngdev
->data_avail
= 0;
145 if (!tx4939_rng_data_present(&rngdev
->rng
, 1))
149 platform_set_drvdata(dev
, rngdev
);
150 return hwrng_register(&rngdev
->rng
);
153 static int __exit
tx4939_rng_remove(struct platform_device
*dev
)
155 struct tx4939_rng
*rngdev
= platform_get_drvdata(dev
);
157 hwrng_unregister(&rngdev
->rng
);
158 platform_set_drvdata(dev
, NULL
);
162 static struct platform_driver tx4939_rng_driver
= {
164 .name
= "tx4939-rng",
165 .owner
= THIS_MODULE
,
167 .remove
= tx4939_rng_remove
,
170 static int __init
tx4939rng_init(void)
172 return platform_driver_probe(&tx4939_rng_driver
, tx4939_rng_probe
);
175 static void __exit
tx4939rng_exit(void)
177 platform_driver_unregister(&tx4939_rng_driver
);
180 module_init(tx4939rng_init
);
181 module_exit(tx4939rng_exit
);
183 MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver for TX4939");
184 MODULE_LICENSE("GPL");