xref: /linux/drivers/char/hw_random/powernv-rng.c (revision da2ff527e44bf3af851c1e5d9ac82d248df35417)
166548e40SMichael Ellerman /*
266548e40SMichael Ellerman  * Copyright 2013 Michael Ellerman, Guo Chao, IBM Corp.
366548e40SMichael Ellerman  *
466548e40SMichael Ellerman  * This program is free software; you can redistribute it and/or
566548e40SMichael Ellerman  * modify it under the terms of the GNU General Public License
666548e40SMichael Ellerman  * as published by the Free Software Foundation; either version
766548e40SMichael Ellerman  * 2 of the License, or (at your option) any later version.
866548e40SMichael Ellerman  */
966548e40SMichael Ellerman 
1066548e40SMichael Ellerman #define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
1166548e40SMichael Ellerman 
1266548e40SMichael Ellerman #include <linux/module.h>
1366548e40SMichael Ellerman #include <linux/kernel.h>
1466548e40SMichael Ellerman #include <linux/platform_device.h>
1566548e40SMichael Ellerman #include <linux/random.h>
1666548e40SMichael Ellerman #include <linux/hw_random.h>
1766548e40SMichael Ellerman 
1866548e40SMichael Ellerman static int powernv_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
1966548e40SMichael Ellerman {
2066548e40SMichael Ellerman 	unsigned long *buf;
2166548e40SMichael Ellerman 	int i, len;
2266548e40SMichael Ellerman 
2366548e40SMichael Ellerman 	/* We rely on rng_buffer_size() being >= sizeof(unsigned long) */
2466548e40SMichael Ellerman 	len = max / sizeof(unsigned long);
2566548e40SMichael Ellerman 
2666548e40SMichael Ellerman 	buf = (unsigned long *)data;
2766548e40SMichael Ellerman 
2866548e40SMichael Ellerman 	for (i = 0; i < len; i++)
2966548e40SMichael Ellerman 		powernv_get_random_long(buf++);
3066548e40SMichael Ellerman 
3166548e40SMichael Ellerman 	return len * sizeof(unsigned long);
3266548e40SMichael Ellerman }
3366548e40SMichael Ellerman 
3466548e40SMichael Ellerman static struct hwrng powernv_hwrng = {
3566548e40SMichael Ellerman 	.name = "powernv-rng",
3666548e40SMichael Ellerman 	.read = powernv_rng_read,
3766548e40SMichael Ellerman };
3866548e40SMichael Ellerman 
3966548e40SMichael Ellerman static int powernv_rng_remove(struct platform_device *pdev)
4066548e40SMichael Ellerman {
4166548e40SMichael Ellerman 	hwrng_unregister(&powernv_hwrng);
4266548e40SMichael Ellerman 
4366548e40SMichael Ellerman 	return 0;
4466548e40SMichael Ellerman }
4566548e40SMichael Ellerman 
4666548e40SMichael Ellerman static int powernv_rng_probe(struct platform_device *pdev)
4766548e40SMichael Ellerman {
4866548e40SMichael Ellerman 	int rc;
4966548e40SMichael Ellerman 
5066548e40SMichael Ellerman 	rc = hwrng_register(&powernv_hwrng);
5166548e40SMichael Ellerman 	if (rc) {
5266548e40SMichael Ellerman 		/* We only register one device, ignore any others */
5366548e40SMichael Ellerman 		if (rc == -EEXIST)
5466548e40SMichael Ellerman 			rc = -ENODEV;
5566548e40SMichael Ellerman 
5666548e40SMichael Ellerman 		return rc;
5766548e40SMichael Ellerman 	}
5866548e40SMichael Ellerman 
5966548e40SMichael Ellerman 	pr_info("Registered powernv hwrng.\n");
6066548e40SMichael Ellerman 
6166548e40SMichael Ellerman 	return 0;
6266548e40SMichael Ellerman }
6366548e40SMichael Ellerman 
64*da2ff527SFabian Frederick static const struct of_device_id powernv_rng_match[] = {
6566548e40SMichael Ellerman 	{ .compatible	= "ibm,power-rng",},
6666548e40SMichael Ellerman 	{},
6766548e40SMichael Ellerman };
6866548e40SMichael Ellerman MODULE_DEVICE_TABLE(of, powernv_rng_match);
6966548e40SMichael Ellerman 
7066548e40SMichael Ellerman static struct platform_driver powernv_rng_driver = {
7166548e40SMichael Ellerman 	.driver = {
7266548e40SMichael Ellerman 		.name = "powernv_rng",
7366548e40SMichael Ellerman 		.of_match_table = powernv_rng_match,
7466548e40SMichael Ellerman 	},
7566548e40SMichael Ellerman 	.probe	= powernv_rng_probe,
7666548e40SMichael Ellerman 	.remove = powernv_rng_remove,
7766548e40SMichael Ellerman };
7866548e40SMichael Ellerman module_platform_driver(powernv_rng_driver);
7966548e40SMichael Ellerman 
8066548e40SMichael Ellerman MODULE_LICENSE("GPL");
8166548e40SMichael Ellerman MODULE_DESCRIPTION("Bare metal HWRNG driver for POWER7+ and above");
82