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> 13*ac316725SRandy Dunlap #include <linux/mod_devicetable.h> 1466548e40SMichael Ellerman #include <linux/kernel.h> 1566548e40SMichael Ellerman #include <linux/platform_device.h> 1666548e40SMichael Ellerman #include <linux/random.h> 1766548e40SMichael Ellerman #include <linux/hw_random.h> 1866548e40SMichael Ellerman 1966548e40SMichael Ellerman static int powernv_rng_read(struct hwrng *rng, void *data, size_t max, bool wait) 2066548e40SMichael Ellerman { 2166548e40SMichael Ellerman unsigned long *buf; 2266548e40SMichael Ellerman int i, len; 2366548e40SMichael Ellerman 2466548e40SMichael Ellerman /* We rely on rng_buffer_size() being >= sizeof(unsigned long) */ 2566548e40SMichael Ellerman len = max / sizeof(unsigned long); 2666548e40SMichael Ellerman 2766548e40SMichael Ellerman buf = (unsigned long *)data; 2866548e40SMichael Ellerman 2966548e40SMichael Ellerman for (i = 0; i < len; i++) 3066548e40SMichael Ellerman powernv_get_random_long(buf++); 3166548e40SMichael Ellerman 3266548e40SMichael Ellerman return len * sizeof(unsigned long); 3366548e40SMichael Ellerman } 3466548e40SMichael Ellerman 3566548e40SMichael Ellerman static struct hwrng powernv_hwrng = { 3666548e40SMichael Ellerman .name = "powernv-rng", 3766548e40SMichael Ellerman .read = powernv_rng_read, 3866548e40SMichael Ellerman }; 3966548e40SMichael Ellerman 4066548e40SMichael Ellerman static int powernv_rng_remove(struct platform_device *pdev) 4166548e40SMichael Ellerman { 4266548e40SMichael Ellerman hwrng_unregister(&powernv_hwrng); 4366548e40SMichael Ellerman 4466548e40SMichael Ellerman return 0; 4566548e40SMichael Ellerman } 4666548e40SMichael Ellerman 4766548e40SMichael Ellerman static int powernv_rng_probe(struct platform_device *pdev) 4866548e40SMichael Ellerman { 4966548e40SMichael Ellerman int rc; 5066548e40SMichael Ellerman 5166548e40SMichael Ellerman rc = hwrng_register(&powernv_hwrng); 5266548e40SMichael Ellerman if (rc) { 5366548e40SMichael Ellerman /* We only register one device, ignore any others */ 5466548e40SMichael Ellerman if (rc == -EEXIST) 5566548e40SMichael Ellerman rc = -ENODEV; 5666548e40SMichael Ellerman 5766548e40SMichael Ellerman return rc; 5866548e40SMichael Ellerman } 5966548e40SMichael Ellerman 6066548e40SMichael Ellerman pr_info("Registered powernv hwrng.\n"); 6166548e40SMichael Ellerman 6266548e40SMichael Ellerman return 0; 6366548e40SMichael Ellerman } 6466548e40SMichael Ellerman 65da2ff527SFabian Frederick static const struct of_device_id powernv_rng_match[] = { 6666548e40SMichael Ellerman { .compatible = "ibm,power-rng",}, 6766548e40SMichael Ellerman {}, 6866548e40SMichael Ellerman }; 6966548e40SMichael Ellerman MODULE_DEVICE_TABLE(of, powernv_rng_match); 7066548e40SMichael Ellerman 7166548e40SMichael Ellerman static struct platform_driver powernv_rng_driver = { 7266548e40SMichael Ellerman .driver = { 7366548e40SMichael Ellerman .name = "powernv_rng", 7466548e40SMichael Ellerman .of_match_table = powernv_rng_match, 7566548e40SMichael Ellerman }, 7666548e40SMichael Ellerman .probe = powernv_rng_probe, 7766548e40SMichael Ellerman .remove = powernv_rng_remove, 7866548e40SMichael Ellerman }; 7966548e40SMichael Ellerman module_platform_driver(powernv_rng_driver); 8066548e40SMichael Ellerman 8166548e40SMichael Ellerman MODULE_LICENSE("GPL"); 8266548e40SMichael Ellerman MODULE_DESCRIPTION("Bare metal HWRNG driver for POWER7+ and above"); 83