summaryrefslogtreecommitdiff
path: root/drivers/rng/turris_rwtm_rng.c
blob: ca808c457978c3e6b74aafd49e299955eba791bb (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
122
123
// SPDX-License-Identifier: GPL-2.0-or-later OR BSD-3-Clause
/*
 * Copyright (c) 2024, Max Resch
 */

#include <dm.h>
#include <malloc.h>
#include <rng.h>
#include <asm/dma-mapping.h>
#include <asm/types.h>
#include <mach/mbox.h>

/* size of entropy buffer */
#define RNG_BUFFER_SIZE	128U

struct turris_rwtm_rng_priv {
	phys_addr_t buffer;
};

static int turris_rwtm_rng_fill_entropy(phys_addr_t entropy, size_t size)
{
	u32 args[3] = { 1, (u32)entropy, size };
	int ret;

	/* flush data cache */
	flush_dcache_range(entropy, entropy + size);

	/*
	 * get entropy
	 * args[0] = 1 copies BYTES array in args[1] of length args[2]
	 */
	ret = mbox_do_cmd(MBOX_CMD_GET_RANDOM, args, 3, NULL, 0);
	if (ret < 0)
		return ret;

	/* invalidate data cache */
	invalidate_dcache_range(entropy, entropy + size);

	return 0;
}

static int turris_rwtm_rng_random_read(struct udevice *dev, void *data, size_t count)
{
	struct turris_rwtm_rng_priv *priv = dev_get_priv(dev);
	phys_addr_t phys;
	size_t size;
	int ret;

	phys = priv->buffer;

	while (count) {
		size = min_t(size_t, RNG_BUFFER_SIZE, count);

		ret = turris_rwtm_rng_fill_entropy(phys, size);
		if (ret < 0)
			return ret;

		memcpy(data, (void *)phys, size);
		count -= size;
		data = (u8 *)data + size;
	}

	return 0;
}

static int turris_rwtm_rng_probe(struct udevice *dev)
{
	struct turris_rwtm_rng_priv *priv = dev_get_priv(dev);
	u32 args[] = { 0 };
	int ret;

	/*
	 * check if the random command is supported
	 * args[0] = 0 would copy 16 DWORDS entropy to out but we ignore them
	 */
	ret = mbox_do_cmd(MBOX_CMD_GET_RANDOM, args, ARRAY_SIZE(args), NULL, 0);
	if (ret < 0)
		return ret;

	/* entropy buffer */
	priv->buffer = 0;

	/* buffer address need to be aligned */
	dma_alloc_coherent(RNG_BUFFER_SIZE, (unsigned long *)&priv->buffer);
	if (!priv->buffer)
		return -ENOMEM;

	return 0;
}

static int turris_rwtm_rng_remove(struct udevice *dev)
{
	struct turris_rwtm_rng_priv *priv = dev_get_priv(dev);
	phys_addr_t phys = priv->buffer;

	dma_free_coherent((void *)phys);

	return 0;
}

static const struct dm_rng_ops turris_rwtm_rng_ops = {
	.read = turris_rwtm_rng_random_read,
};

/*
 * only Turris MOX firmware has the RNG but allow all probable devices to be
 * probed the default firmware will just reject the probe
 */
static const struct udevice_id turris_rwtm_rng_match[] = {
	{ .compatible = "cznic,turris-mox-rwtm" },
	{ .compatible = "marvell,armada-3700-rwtm-firmware" },
	{},
};

U_BOOT_DRIVER(turris_rwtm_rng) = {
	.name	= "turris-rwtm-rng",
	.id	= UCLASS_RNG,
	.of_match = turris_rwtm_rng_match,
	.ops	= &turris_rwtm_rng_ops,
	.probe	= turris_rwtm_rng_probe,
	.remove = turris_rwtm_rng_remove,
	.priv_auto = sizeof(struct turris_rwtm_rng_priv),
};