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/err.h>
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/delay.h>
16 #include <linux/platform_device.h>
17 #include <linux/hw_random.h>
18 #include <linux/gfp.h>
20 #define TX4939_RNG_RCSR 0x00000000
21 #define TX4939_RNG_ROR(n) (0x00000018 + (n) * 8)
23 #define TX4939_RNG_RCSR_INTE 0x00000008
24 #define TX4939_RNG_RCSR_RST 0x00000004
25 #define TX4939_RNG_RCSR_FIN 0x00000002
26 #define TX4939_RNG_RCSR_ST 0x00000001
32 unsigned int data_avail
;
35 static void rng_io_start(void)
39 * readq is reading a 64-bit register using a 64-bit load. On
40 * a 32-bit kernel however interrupts or any other processor
41 * exception would clobber the upper 32-bit of the processor
42 * register so interrupts need to be disabled.
48 static void rng_io_end(void)
55 static u64
read_rng(void __iomem
*base
, unsigned int offset
)
57 return ____raw_readq(base
+ offset
);
60 static void write_rng(u64 val
, void __iomem
*base
, unsigned int offset
)
62 return ____raw_writeq(val
, base
+ offset
);
65 static int tx4939_rng_data_present(struct hwrng
*rng
, int wait
)
67 struct tx4939_rng
*rngdev
= container_of(rng
, struct tx4939_rng
, rng
);
70 if (rngdev
->data_avail
)
71 return rngdev
->data_avail
;
72 for (i
= 0; i
< 20; i
++) {
74 if (!(read_rng(rngdev
->base
, TX4939_RNG_RCSR
)
75 & TX4939_RNG_RCSR_ST
)) {
77 read_rng(rngdev
->base
, TX4939_RNG_ROR(0));
79 read_rng(rngdev
->base
, TX4939_RNG_ROR(1));
81 read_rng(rngdev
->base
, TX4939_RNG_ROR(2));
83 sizeof(rngdev
->databuf
) / sizeof(u32
);
85 write_rng(TX4939_RNG_RCSR_ST
,
86 rngdev
->base
, TX4939_RNG_RCSR
);
92 /* 90 bus clock cycles by default for generation */
95 return rngdev
->data_avail
;
98 static int tx4939_rng_data_read(struct hwrng
*rng
, u32
*buffer
)
100 struct tx4939_rng
*rngdev
= container_of(rng
, struct tx4939_rng
, rng
);
102 rngdev
->data_avail
--;
103 *buffer
= *((u32
*)&rngdev
->databuf
+ rngdev
->data_avail
);
107 static int __init
tx4939_rng_probe(struct platform_device
*dev
)
109 struct tx4939_rng
*rngdev
;
112 rngdev
= devm_kzalloc(&dev
->dev
, sizeof(*rngdev
), GFP_KERNEL
);
115 rngdev
->base
= devm_platform_ioremap_resource(dev
, 0);
116 if (IS_ERR(rngdev
->base
))
117 return PTR_ERR(rngdev
->base
);
119 rngdev
->rng
.name
= dev_name(&dev
->dev
);
120 rngdev
->rng
.data_present
= tx4939_rng_data_present
;
121 rngdev
->rng
.data_read
= tx4939_rng_data_read
;
125 write_rng(TX4939_RNG_RCSR_RST
, rngdev
->base
, TX4939_RNG_RCSR
);
126 write_rng(0, rngdev
->base
, TX4939_RNG_RCSR
);
128 write_rng(TX4939_RNG_RCSR_ST
, rngdev
->base
, TX4939_RNG_RCSR
);
131 * Drop first two results. From the datasheet:
132 * The quality of the random numbers generated immediately
133 * after reset can be insufficient. Therefore, do not use
134 * random numbers obtained from the first and second
135 * generations; use the ones from the third or subsequent
138 for (i
= 0; i
< 2; i
++) {
139 rngdev
->data_avail
= 0;
140 if (!tx4939_rng_data_present(&rngdev
->rng
, 1))
144 platform_set_drvdata(dev
, rngdev
);
145 return devm_hwrng_register(&dev
->dev
, &rngdev
->rng
);
148 static struct platform_driver tx4939_rng_driver
= {
150 .name
= "tx4939-rng",
154 module_platform_driver_probe(tx4939_rng_driver
, tx4939_rng_probe
);
156 MODULE_DESCRIPTION("H/W Random Number Generator (RNG) driver for TX4939");
157 MODULE_LICENSE("GPL");