aboutsummaryrefslogtreecommitdiff
path: root/drivers/rng/meson-rng.c
blob: e0a1e8c7e046862bea34f7bc71d31b2a6790a1ba (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright 2020, Heinrich Schuchardt <xypron.glpk@gmx.de>
 *
 * Driver for Amlogic hardware random number generator
 */

#include <common.h>
#include <clk.h>
#include <dm.h>
#include <rng.h>
#include <asm/io.h>

struct meson_rng_plat {
	fdt_addr_t base;
	struct clk clk;
};

/**
 * meson_rng_read() - fill buffer with random bytes
 *
 * @buffer:	buffer to receive data
 * @size:	size of buffer
 *
 * Return:	0
 */
static int meson_rng_read(struct udevice *dev, void *data, size_t len)
{
	struct meson_rng_plat *pdata = dev_get_plat(dev);
	char *buffer = (char *)data;

	while (len) {
		u32 rand = readl(pdata->base);
		size_t step;

		if (len >= 4)
			step = 4;
		else
			step = len;
		memcpy(buffer, &rand, step);
		buffer += step;
		len -= step;
	}
	return 0;
}

/**
 * meson_rng_probe() - probe rng device
 *
 * @dev:	device
 * Return:	0 if ok
 */
static int meson_rng_probe(struct udevice *dev)
{
	struct meson_rng_plat *pdata = dev_get_plat(dev);
	int err;

	err = clk_enable(&pdata->clk);
	if (err)
		return err;

	return 0;
}

/**
 * meson_rng_remove() - deinitialize rng device
 *
 * @dev:	device
 * Return:	0 if ok
 */
static int meson_rng_remove(struct udevice *dev)
{
	struct meson_rng_plat *pdata = dev_get_plat(dev);

	return clk_disable(&pdata->clk);
}

/**
 * meson_rng_of_to_plat() - transfer device tree data to plaform data
 *
 * @dev:	device
 * Return:	0 if ok
 */
static int meson_rng_of_to_plat(struct udevice *dev)
{
	struct meson_rng_plat *pdata = dev_get_plat(dev);
	int err;

	pdata->base = dev_read_addr(dev);
	if (!pdata->base)
		return -ENODEV;

	/* Get optional "core" clock */
	err = clk_get_by_name_optional(dev, "core", &pdata->clk);
	if (err)
		return err;

	return 0;
}

static const struct dm_rng_ops meson_rng_ops = {
	.read = meson_rng_read,
};

static const struct udevice_id meson_rng_match[] = {
	{
		.compatible = "amlogic,meson-rng",
	},
	{},
};

U_BOOT_DRIVER(meson_rng) = {
	.name = "meson-rng",
	.id = UCLASS_RNG,
	.of_match = meson_rng_match,
	.ops = &meson_rng_ops,
	.probe = meson_rng_probe,
	.remove = meson_rng_remove,
	.plat_auto	= sizeof(struct meson_rng_plat),
	.of_to_plat = meson_rng_of_to_plat,
};