1649e9ea0SKent Yoder /*
2649e9ea0SKent Yoder  * Copyright (C) 2010 Michael Neuling IBM Corporation
3649e9ea0SKent Yoder  *
4649e9ea0SKent Yoder  * Driver for the pseries hardware RNG for POWER7+ and above
5649e9ea0SKent Yoder  *
6649e9ea0SKent Yoder  * This program is free software; you can redistribute it and/or modify
7649e9ea0SKent Yoder  * it under the terms of the GNU General Public License version 2 as
8649e9ea0SKent Yoder  * published by the Free Software Foundation.
9649e9ea0SKent Yoder  *
10649e9ea0SKent Yoder  * This program is distributed in the hope that it will be useful,
11649e9ea0SKent Yoder  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12649e9ea0SKent Yoder  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13649e9ea0SKent Yoder  * GNU General Public License for more details.
14649e9ea0SKent Yoder  *
15649e9ea0SKent Yoder  * You should have received a copy of the GNU General Public License
16649e9ea0SKent Yoder  * along with this program; if not, write to the Free Software
17649e9ea0SKent Yoder  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18649e9ea0SKent Yoder  */
19649e9ea0SKent Yoder 
20649e9ea0SKent Yoder #include <linux/module.h>
21649e9ea0SKent Yoder #include <linux/hw_random.h>
22649e9ea0SKent Yoder #include <asm/vio.h>
23649e9ea0SKent Yoder 
24649e9ea0SKent Yoder #define MODULE_NAME "pseries-rng"
25649e9ea0SKent Yoder 
26649e9ea0SKent Yoder static int pseries_rng_data_read(struct hwrng *rng, u32 *data)
27649e9ea0SKent Yoder {
28649e9ea0SKent Yoder 	if (plpar_hcall(H_RANDOM, (unsigned long *)data) != H_SUCCESS) {
29649e9ea0SKent Yoder 		printk(KERN_ERR "pseries rng hcall error\n");
30649e9ea0SKent Yoder 		return 0;
31649e9ea0SKent Yoder 	}
32649e9ea0SKent Yoder 	return 8;
33649e9ea0SKent Yoder }
34649e9ea0SKent Yoder 
35649e9ea0SKent Yoder /**
36649e9ea0SKent Yoder  * pseries_rng_get_desired_dma - Return desired DMA allocate for CMO operations
37649e9ea0SKent Yoder  *
38649e9ea0SKent Yoder  * This is a required function for a driver to operate in a CMO environment
39649e9ea0SKent Yoder  * but this device does not make use of DMA allocations, return 0.
40649e9ea0SKent Yoder  *
41649e9ea0SKent Yoder  * Return value:
42649e9ea0SKent Yoder  *	Number of bytes of IO data the driver will need to perform well -> 0
43649e9ea0SKent Yoder  */
44649e9ea0SKent Yoder static unsigned long pseries_rng_get_desired_dma(struct vio_dev *vdev)
45649e9ea0SKent Yoder {
46649e9ea0SKent Yoder 	return 0;
47649e9ea0SKent Yoder };
48649e9ea0SKent Yoder 
49649e9ea0SKent Yoder static struct hwrng pseries_rng = {
50649e9ea0SKent Yoder 	.name		= MODULE_NAME,
51649e9ea0SKent Yoder 	.data_read	= pseries_rng_data_read,
52649e9ea0SKent Yoder };
53649e9ea0SKent Yoder 
54649e9ea0SKent Yoder static int __init pseries_rng_probe(struct vio_dev *dev,
55649e9ea0SKent Yoder 		const struct vio_device_id *id)
56649e9ea0SKent Yoder {
57649e9ea0SKent Yoder 	return hwrng_register(&pseries_rng);
58649e9ea0SKent Yoder }
59649e9ea0SKent Yoder 
60649e9ea0SKent Yoder static int __exit pseries_rng_remove(struct vio_dev *dev)
61649e9ea0SKent Yoder {
62649e9ea0SKent Yoder 	hwrng_unregister(&pseries_rng);
63649e9ea0SKent Yoder 	return 0;
64649e9ea0SKent Yoder }
65649e9ea0SKent Yoder 
66649e9ea0SKent Yoder static struct vio_device_id pseries_rng_driver_ids[] = {
67649e9ea0SKent Yoder 	{ "ibm,random-v1", "ibm,random"},
68649e9ea0SKent Yoder 	{ "", "" }
69649e9ea0SKent Yoder };
70649e9ea0SKent Yoder MODULE_DEVICE_TABLE(vio, pseries_rng_driver_ids);
71649e9ea0SKent Yoder 
72649e9ea0SKent Yoder static struct vio_driver pseries_rng_driver = {
73649e9ea0SKent Yoder 	.name = MODULE_NAME,
74649e9ea0SKent Yoder 	.probe = pseries_rng_probe,
75649e9ea0SKent Yoder 	.remove = pseries_rng_remove,
76649e9ea0SKent Yoder 	.get_desired_dma = pseries_rng_get_desired_dma,
77649e9ea0SKent Yoder 	.id_table = pseries_rng_driver_ids
78649e9ea0SKent Yoder };
79649e9ea0SKent Yoder 
80649e9ea0SKent Yoder static int __init rng_init(void)
81649e9ea0SKent Yoder {
82649e9ea0SKent Yoder 	printk(KERN_INFO "Registering IBM pSeries RNG driver\n");
83649e9ea0SKent Yoder 	return vio_register_driver(&pseries_rng_driver);
84649e9ea0SKent Yoder }
85649e9ea0SKent Yoder 
86649e9ea0SKent Yoder module_init(rng_init);
87649e9ea0SKent Yoder 
88649e9ea0SKent Yoder static void __exit rng_exit(void)
89649e9ea0SKent Yoder {
90649e9ea0SKent Yoder 	vio_unregister_driver(&pseries_rng_driver);
91649e9ea0SKent Yoder }
92649e9ea0SKent Yoder module_exit(rng_exit);
93649e9ea0SKent Yoder 
94649e9ea0SKent Yoder MODULE_LICENSE("GPL");
95649e9ea0SKent Yoder MODULE_AUTHOR("Michael Neuling <mikey@neuling.org>");
96649e9ea0SKent Yoder MODULE_DESCRIPTION("H/W RNG driver for IBM pSeries processors");
97