12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 266548e40SMichael Ellerman /* 366548e40SMichael Ellerman * Copyright 2013 Michael Ellerman, Guo Chao, IBM Corp. 466548e40SMichael Ellerman */ 566548e40SMichael Ellerman 666548e40SMichael Ellerman #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 766548e40SMichael Ellerman 866548e40SMichael Ellerman #include <linux/module.h> 9ac316725SRandy Dunlap #include <linux/mod_devicetable.h> 1066548e40SMichael Ellerman #include <linux/kernel.h> 1166548e40SMichael Ellerman #include <linux/platform_device.h> 1266548e40SMichael Ellerman #include <linux/random.h> 1366548e40SMichael Ellerman #include <linux/hw_random.h> 14*6bb20c15SJason A. Donenfeld #include <asm/archrandom.h> 1566548e40SMichael Ellerman 1666548e40SMichael Ellerman static int powernv_rng_read(struct hwrng *rng, void *data, size_t max, bool wait) 1766548e40SMichael Ellerman { 1866548e40SMichael Ellerman unsigned long *buf; 1966548e40SMichael Ellerman int i, len; 2066548e40SMichael Ellerman 2166548e40SMichael Ellerman /* We rely on rng_buffer_size() being >= sizeof(unsigned long) */ 2266548e40SMichael Ellerman len = max / sizeof(unsigned long); 2366548e40SMichael Ellerman 2466548e40SMichael Ellerman buf = (unsigned long *)data; 2566548e40SMichael Ellerman 2666548e40SMichael Ellerman for (i = 0; i < len; i++) 27978030f0SJason A. Donenfeld pnv_get_random_long(buf++); 2866548e40SMichael Ellerman 2966548e40SMichael Ellerman return len * sizeof(unsigned long); 3066548e40SMichael Ellerman } 3166548e40SMichael Ellerman 3266548e40SMichael Ellerman static struct hwrng powernv_hwrng = { 3366548e40SMichael Ellerman .name = "powernv-rng", 3466548e40SMichael Ellerman .read = powernv_rng_read, 3566548e40SMichael Ellerman }; 3666548e40SMichael Ellerman 3766548e40SMichael Ellerman static int powernv_rng_probe(struct platform_device *pdev) 3866548e40SMichael Ellerman { 3966548e40SMichael Ellerman int rc; 4066548e40SMichael Ellerman 413e75241bSChuhong Yuan rc = devm_hwrng_register(&pdev->dev, &powernv_hwrng); 4266548e40SMichael Ellerman if (rc) { 4366548e40SMichael Ellerman /* We only register one device, ignore any others */ 4466548e40SMichael Ellerman if (rc == -EEXIST) 4566548e40SMichael Ellerman rc = -ENODEV; 4666548e40SMichael Ellerman 4766548e40SMichael Ellerman return rc; 4866548e40SMichael Ellerman } 4966548e40SMichael Ellerman 5066548e40SMichael Ellerman pr_info("Registered powernv hwrng.\n"); 5166548e40SMichael Ellerman 5266548e40SMichael Ellerman return 0; 5366548e40SMichael Ellerman } 5466548e40SMichael Ellerman 55da2ff527SFabian Frederick static const struct of_device_id powernv_rng_match[] = { 5666548e40SMichael Ellerman { .compatible = "ibm,power-rng",}, 5766548e40SMichael Ellerman {}, 5866548e40SMichael Ellerman }; 5966548e40SMichael Ellerman MODULE_DEVICE_TABLE(of, powernv_rng_match); 6066548e40SMichael Ellerman 6166548e40SMichael Ellerman static struct platform_driver powernv_rng_driver = { 6266548e40SMichael Ellerman .driver = { 6366548e40SMichael Ellerman .name = "powernv_rng", 6466548e40SMichael Ellerman .of_match_table = powernv_rng_match, 6566548e40SMichael Ellerman }, 6666548e40SMichael Ellerman .probe = powernv_rng_probe, 6766548e40SMichael Ellerman }; 6866548e40SMichael Ellerman module_platform_driver(powernv_rng_driver); 6966548e40SMichael Ellerman 7066548e40SMichael Ellerman MODULE_LICENSE("GPL"); 7166548e40SMichael Ellerman MODULE_DESCRIPTION("Bare metal HWRNG driver for POWER7+ and above"); 72