summaryrefslogtreecommitdiffstats
path: root/drivers/hw_random/stm32-rng.c
blob: 440b53684f71aa0613ac108113c03a20a96ce5f4 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (c) 2019, Linaro Limited
 * Copyright (c) 2020 Ahmad Fatoum, Pengutronix
 */

#define pr_fmt(fmt) "stm32-rng: " fmt

#include <common.h>
#include <clock.h>
#include <driver.h>
#include <init.h>
#include <io.h>
#include <linux/clk.h>
#include <linux/hw_random.h>
#include <linux/iopoll.h>
#include <linux/reset.h>

#define RNG_CR 0x00
#define RNG_CR_RNGEN BIT(2)
#define RNG_CR_CED BIT(5)

#define RNG_SR 0x04
#define RNG_SR_SEIS BIT(6)
#define RNG_SR_CEIS BIT(5)
#define RNG_SR_SECS BIT(2)
#define RNG_SR_DRDY BIT(0)

#define RNG_DR 0x08

struct stm32_rng {
	struct clk	*clk;
	void __iomem	*base;
	struct hwrng	hwrng;
};

static inline struct stm32_rng *to_stm32_rng(struct hwrng *hwrng)
{
	return container_of(hwrng, struct stm32_rng, hwrng);
}

static int stm32_rng_read(struct hwrng *hwrng, void *data, size_t len, bool wait)
{
	int ret = 0;
	u32 sr, count, reg;
	size_t increment;
	struct stm32_rng *rng = to_stm32_rng(hwrng);
	size_t remaining = len;

	while (remaining) {
		ret = readl_poll_timeout(rng->base + RNG_SR, sr,
					 sr & RNG_SR_DRDY, 10 * USEC_PER_MSEC);
		if (ret)
			goto out;

		if (sr & (RNG_SR_SEIS | RNG_SR_SECS)) {
			int i;
			/* As per SoC TRM */
			clrbits_le32(rng->base + RNG_SR, RNG_SR_SEIS);
			for (i = 0; i < 12; i++)
				readl(rng->base + RNG_DR);
			if (readl(rng->base + RNG_SR) & RNG_SR_SEIS) {
				pr_warn("RNG Noise");
				ret = -EIO;
				goto out;
			}

			/* start again */
			continue;
		}

		/*
		 * Once the DRDY bit is set, the RNG_DR register can
		 * be read four consecutive times.
		 */
		count = 4;
		while (remaining && count) {
			reg = readl(rng->base + RNG_DR);
			memcpy(data, &reg, min(remaining, sizeof(u32)));
			increment = min(remaining, sizeof(u32));
			data += increment;
			remaining -= increment;
			count--;
		}
	}

out:
	return len ?: ret;
}

static int stm32_rng_init(struct hwrng *hwrng)
{
	int err;
	struct stm32_rng *rng = to_stm32_rng(hwrng);

	err = clk_enable(rng->clk);
	if (err)
		return err;

	/* Disable CED */
	writel(RNG_CR_RNGEN | RNG_CR_CED, rng->base + RNG_CR);

	/* clear error indicators */
	writel(0, rng->base + RNG_SR);

	return 0;
}

static void stm32_rng_remove(struct device_d *dev)
{
	struct stm32_rng *rng = dev->priv;

	writel(0, rng->base + RNG_CR);
	clk_disable(rng->clk);
}

static int stm32_rng_probe(struct device_d *dev)
{
	struct stm32_rng *rng;
	struct resource *res;
	int ret;

	rng = xzalloc(sizeof(*rng));
	dev->priv = rng;

	res = dev_request_mem_resource(dev, 0);
	if (IS_ERR(res))
		return PTR_ERR(res);

	rng->base = IOMEM(res->start);

	rng->clk = clk_get(dev, NULL);
	if (IS_ERR(rng->clk)) {
		dev_err(dev, "Can not get clock\n");
		return PTR_ERR(rng->clk);
	}

	ret = device_reset_us(dev, 20);
	if (ret)
		return ret;

	rng->hwrng.name = dev->name;
	rng->hwrng.read = stm32_rng_read;
	rng->hwrng.init = stm32_rng_init;

	ret = hwrng_register(dev, &rng->hwrng);
	if (ret)
		stm32_rng_remove(dev);

	return ret;
}

static const struct of_device_id stm32_rng_dt_ids[] = {
	{ .compatible = "st,stm32-rng" },
	{ /* sentinel */},
};

static struct driver_d stm32_rng_driver = {
	.name = "stm32-rng",
	.probe = stm32_rng_probe,
	.remove = stm32_rng_remove,
	.of_compatible = stm32_rng_dt_ids,
};
device_platform_driver(stm32_rng_driver);