2 * Copyright 2013 Michael Ellerman, Guo Chao, IBM Corp.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/module.h>
13 #include <linux/mod_devicetable.h>
14 #include <linux/kernel.h>
15 #include <linux/platform_device.h>
16 #include <linux/random.h>
17 #include <linux/hw_random.h>
19 static int powernv_rng_read(struct hwrng
*rng
, void *data
, size_t max
, bool wait
)
24 /* We rely on rng_buffer_size() being >= sizeof(unsigned long) */
25 len
= max
/ sizeof(unsigned long);
27 buf
= (unsigned long *)data
;
29 for (i
= 0; i
< len
; i
++)
30 powernv_get_random_long(buf
++);
32 return len
* sizeof(unsigned long);
35 static struct hwrng powernv_hwrng
= {
36 .name
= "powernv-rng",
37 .read
= powernv_rng_read
,
40 static int powernv_rng_remove(struct platform_device
*pdev
)
42 hwrng_unregister(&powernv_hwrng
);
47 static int powernv_rng_probe(struct platform_device
*pdev
)
51 rc
= hwrng_register(&powernv_hwrng
);
53 /* We only register one device, ignore any others */
60 pr_info("Registered powernv hwrng.\n");
65 static const struct of_device_id powernv_rng_match
[] = {
66 { .compatible
= "ibm,power-rng",},
69 MODULE_DEVICE_TABLE(of
, powernv_rng_match
);
71 static struct platform_driver powernv_rng_driver
= {
73 .name
= "powernv_rng",
74 .of_match_table
= powernv_rng_match
,
76 .probe
= powernv_rng_probe
,
77 .remove
= powernv_rng_remove
,
79 module_platform_driver(powernv_rng_driver
);
81 MODULE_LICENSE("GPL");
82 MODULE_DESCRIPTION("Bare metal HWRNG driver for POWER7+ and above");