summaryrefslogtreecommitdiffstats
path: root/drivers/char/hw_random/powernv-rng.c
blob: 8da1d7917bdc45d78f2b0afef6c69cd31a6c2746 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright 2013 Michael Ellerman, Guo Chao, IBM Corp.
 */

#define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt

#include <linux/module.h>
#include <linux/mod_devicetable.h>
#include <linux/kernel.h>
#include <linux/platform_device.h>
#include <linux/random.h>
#include <linux/hw_random.h>

static int powernv_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
{
	unsigned long *buf;
	int i, len;

	/* We rely on rng_buffer_size() being >= sizeof(unsigned long) */
	len = max / sizeof(unsigned long);

	buf = (unsigned long *)data;

	for (i = 0; i < len; i++)
		powernv_get_random_long(buf++);

	return len * sizeof(unsigned long);
}

static struct hwrng powernv_hwrng = {
	.name = "powernv-rng",
	.read = powernv_rng_read,
};

static int powernv_rng_probe(struct platform_device *pdev)
{
	int rc;

	rc = devm_hwrng_register(&pdev->dev, &powernv_hwrng);
	if (rc) {
		/* We only register one device, ignore any others */
		if (rc == -EEXIST)
			rc = -ENODEV;

		return rc;
	}

	pr_info("Registered powernv hwrng.\n");

	return 0;
}

static const struct of_device_id powernv_rng_match[] = {
	{ .compatible	= "ibm,power-rng",},
	{},
};
MODULE_DEVICE_TABLE(of, powernv_rng_match);

static struct platform_driver powernv_rng_driver = {
	.driver = {
		.name = "powernv_rng",
		.of_match_table = powernv_rng_match,
	},
	.probe	= powernv_rng_probe,
};
module_platform_driver(powernv_rng_driver);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Bare metal HWRNG driver for POWER7+ and above");