xref: /optee_os/core/drivers/stm32_rng.c (revision c99311c825e540aa02656ab983026a53bdd3d790)
1f3c22059SEtienne Carriere // SPDX-License-Identifier: BSD-3-Clause
2f3c22059SEtienne Carriere /*
3f3c22059SEtienne Carriere  * Copyright (c) 2018-2019, STMicroelectronics
4f3c22059SEtienne Carriere  */
5f3c22059SEtienne Carriere 
6f3c22059SEtienne Carriere #include <assert.h>
7d7a1a7d2SEtienne Carriere #include <drivers/clk.h>
8d7a1a7d2SEtienne Carriere #include <drivers/clk_dt.h>
9f3c22059SEtienne Carriere #include <drivers/stm32_rng.h>
10f3c22059SEtienne Carriere #include <io.h>
11f3c22059SEtienne Carriere #include <kernel/delay.h>
12a2fc83d1SJerome Forissier #include <kernel/dt.h>
1365401337SJens Wiklander #include <kernel/boot.h>
14f3c22059SEtienne Carriere #include <kernel/panic.h>
1565b5ada4SMarouene Boubakri #include <kernel/thread.h>
16a2fc83d1SJerome Forissier #include <libfdt.h>
17f3c22059SEtienne Carriere #include <mm/core_memprot.h>
18f3c22059SEtienne Carriere #include <stdbool.h>
19f3c22059SEtienne Carriere #include <stm32_util.h>
20f3c22059SEtienne Carriere #include <string.h>
21f3c22059SEtienne Carriere 
22f3c22059SEtienne Carriere #define DT_RNG_COMPAT		"st,stm32-rng"
23f3c22059SEtienne Carriere #define RNG_CR			0x00U
24f3c22059SEtienne Carriere #define RNG_SR			0x04U
25f3c22059SEtienne Carriere #define RNG_DR			0x08U
26f3c22059SEtienne Carriere 
27f3c22059SEtienne Carriere #define RNG_CR_RNGEN		BIT(2)
28f3c22059SEtienne Carriere #define RNG_CR_IE		BIT(3)
29f3c22059SEtienne Carriere #define RNG_CR_CED		BIT(5)
30f3c22059SEtienne Carriere 
31f3c22059SEtienne Carriere #define RNG_SR_DRDY		BIT(0)
32f3c22059SEtienne Carriere #define RNG_SR_CECS		BIT(1)
33f3c22059SEtienne Carriere #define RNG_SR_SECS		BIT(2)
34f3c22059SEtienne Carriere #define RNG_SR_CEIS		BIT(5)
35f3c22059SEtienne Carriere #define RNG_SR_SEIS		BIT(6)
36f3c22059SEtienne Carriere 
37*c99311c8SEtienne Carriere #define RNG_TIMEOUT_US		U(100000)
38f3c22059SEtienne Carriere 
39f3c22059SEtienne Carriere struct stm32_rng_instance {
40f3c22059SEtienne Carriere 	struct io_pa_va base;
41d7a1a7d2SEtienne Carriere 	struct clk *clock;
42f3c22059SEtienne Carriere 	unsigned int lock;
43f3c22059SEtienne Carriere 	unsigned int refcount;
44d8682c4cSEtienne Carriere 	bool release_post_boot;
45f3c22059SEtienne Carriere };
46f3c22059SEtienne Carriere 
47f3c22059SEtienne Carriere static struct stm32_rng_instance *stm32_rng;
48f3c22059SEtienne Carriere 
49f3c22059SEtienne Carriere /*
50f3c22059SEtienne Carriere  * Extracts from the STM32 RNG specification:
51f3c22059SEtienne Carriere  *
52f3c22059SEtienne Carriere  * When a noise source (or seed) error occurs, the RNG stops generating
53f3c22059SEtienne Carriere  * random numbers and sets to “1” both SEIS and SECS bits to indicate
54f3c22059SEtienne Carriere  * that a seed error occurred. (...)
55f3c22059SEtienne Carriere 
56f3c22059SEtienne Carriere  * The following sequence shall be used to fully recover from a seed
57f3c22059SEtienne Carriere  * error after the RNG initialization:
58f3c22059SEtienne Carriere  * 1. Clear the SEIS bit by writing it to “0”.
59f3c22059SEtienne Carriere  * 2. Read out 12 words from the RNG_DR register, and discard each of
60f3c22059SEtienne Carriere  * them in order to clean the pipeline.
61f3c22059SEtienne Carriere  * 3. Confirm that SEIS is still cleared. Random number generation is
62f3c22059SEtienne Carriere  * back to normal.
63f3c22059SEtienne Carriere  */
64f3c22059SEtienne Carriere static void conceal_seed_error(vaddr_t rng_base)
65f3c22059SEtienne Carriere {
66f3c22059SEtienne Carriere 	if (io_read32(rng_base + RNG_SR) & (RNG_SR_SECS | RNG_SR_SEIS)) {
67f3c22059SEtienne Carriere 		size_t i = 0;
68f3c22059SEtienne Carriere 
69f3c22059SEtienne Carriere 		io_mask32(rng_base + RNG_SR, 0, RNG_SR_SEIS);
70f3c22059SEtienne Carriere 
71f3c22059SEtienne Carriere 		for (i = 12; i != 0; i--)
72f3c22059SEtienne Carriere 			(void)io_read32(rng_base + RNG_DR);
73f3c22059SEtienne Carriere 
74f3c22059SEtienne Carriere 		if (io_read32(rng_base + RNG_SR) & RNG_SR_SEIS)
75f3c22059SEtienne Carriere 			panic("RNG noise");
76f3c22059SEtienne Carriere 	}
77f3c22059SEtienne Carriere }
78f3c22059SEtienne Carriere 
79f3c22059SEtienne Carriere #define RNG_FIFO_BYTE_DEPTH		16u
80f3c22059SEtienne Carriere 
81*c99311c8SEtienne Carriere static TEE_Result read_available(vaddr_t rng_base, uint8_t *out, size_t *size)
82f3c22059SEtienne Carriere {
83*c99311c8SEtienne Carriere 	uint8_t *buf = NULL;
84*c99311c8SEtienne Carriere 	size_t req_size = 0;
85*c99311c8SEtienne Carriere 	size_t len = 0;
86f3c22059SEtienne Carriere 
87f3c22059SEtienne Carriere 	conceal_seed_error(rng_base);
88f3c22059SEtienne Carriere 
89*c99311c8SEtienne Carriere 	if (!(io_read32(rng_base + RNG_SR) & RNG_SR_DRDY))
90*c99311c8SEtienne Carriere 		return TEE_ERROR_NO_DATA;
91f3c22059SEtienne Carriere 
92*c99311c8SEtienne Carriere 	if (io_read32(rng_base + RNG_SR) & RNG_SR_SEIS)
93*c99311c8SEtienne Carriere 		return TEE_ERROR_NO_DATA;
94*c99311c8SEtienne Carriere 
95*c99311c8SEtienne Carriere 	buf = out;
96*c99311c8SEtienne Carriere 	req_size = MIN(RNG_FIFO_BYTE_DEPTH, *size);
97*c99311c8SEtienne Carriere 	len = req_size;
98f3c22059SEtienne Carriere 
99f3c22059SEtienne Carriere 	/* RNG is ready: read up to 4 32bit words */
100f3c22059SEtienne Carriere 	while (len) {
101f3c22059SEtienne Carriere 		uint32_t data32 = io_read32(rng_base + RNG_DR);
102f3c22059SEtienne Carriere 		size_t sz = MIN(len, sizeof(uint32_t));
103f3c22059SEtienne Carriere 
104f3c22059SEtienne Carriere 		memcpy(buf, &data32, sz);
105f3c22059SEtienne Carriere 		buf += sz;
106f3c22059SEtienne Carriere 		len -= sz;
107f3c22059SEtienne Carriere 	}
108*c99311c8SEtienne Carriere 
109f3c22059SEtienne Carriere 	*size = req_size;
110f3c22059SEtienne Carriere 
111*c99311c8SEtienne Carriere 	return TEE_SUCCESS;
112f3c22059SEtienne Carriere }
113f3c22059SEtienne Carriere 
114f3c22059SEtienne Carriere static void gate_rng(bool enable, struct stm32_rng_instance *dev)
115f3c22059SEtienne Carriere {
116c2e4eb43SAnton Rybakov 	vaddr_t rng_cr = io_pa_or_va(&dev->base, 1) + RNG_CR;
117f3c22059SEtienne Carriere 	uint32_t exceptions = may_spin_lock(&dev->lock);
118f3c22059SEtienne Carriere 
119f3c22059SEtienne Carriere 	if (enable) {
120f3c22059SEtienne Carriere 		/* incr_refcnt return non zero if resource shall be enabled */
121f3c22059SEtienne Carriere 		if (incr_refcnt(&dev->refcount)) {
122d7a1a7d2SEtienne Carriere 			clk_enable(dev->clock);
123f3c22059SEtienne Carriere 			io_write32(rng_cr, 0);
124f3c22059SEtienne Carriere 			io_write32(rng_cr, RNG_CR_RNGEN | RNG_CR_CED);
125f3c22059SEtienne Carriere 		}
126f3c22059SEtienne Carriere 	} else {
127f3c22059SEtienne Carriere 		/* decr_refcnt return non zero if resource shall be disabled */
128f3c22059SEtienne Carriere 		if (decr_refcnt(&dev->refcount)) {
129f3c22059SEtienne Carriere 			io_write32(rng_cr, 0);
130d7a1a7d2SEtienne Carriere 			clk_disable(dev->clock);
131f3c22059SEtienne Carriere 		}
132f3c22059SEtienne Carriere 	}
133f3c22059SEtienne Carriere 
134f3c22059SEtienne Carriere 	may_spin_unlock(&dev->lock, exceptions);
135f3c22059SEtienne Carriere }
136f3c22059SEtienne Carriere 
137f3c22059SEtienne Carriere TEE_Result stm32_rng_read(uint8_t *out, size_t size)
138f3c22059SEtienne Carriere {
139*c99311c8SEtienne Carriere 	TEE_Result rc = TEE_ERROR_GENERIC;
140*c99311c8SEtienne Carriere 	bool burst_timeout = false;
141*c99311c8SEtienne Carriere 	uint64_t timeout_ref = 0;
142f3c22059SEtienne Carriere 	uint32_t exceptions = 0;
143f3c22059SEtienne Carriere 	uint8_t *out_ptr = out;
144*c99311c8SEtienne Carriere 	vaddr_t rng_base = 0;
145f3c22059SEtienne Carriere 	size_t out_size = 0;
146f3c22059SEtienne Carriere 
147f3c22059SEtienne Carriere 	if (!stm32_rng) {
148f3c22059SEtienne Carriere 		DMSG("No RNG");
149f3c22059SEtienne Carriere 		return TEE_ERROR_NOT_SUPPORTED;
150f3c22059SEtienne Carriere 	}
151f3c22059SEtienne Carriere 
152f3c22059SEtienne Carriere 	gate_rng(true, stm32_rng);
153*c99311c8SEtienne Carriere 	rng_base = io_pa_or_va(&stm32_rng->base, 1);
154*c99311c8SEtienne Carriere 
155*c99311c8SEtienne Carriere 	/* Arm timeout */
156*c99311c8SEtienne Carriere 	timeout_ref = timeout_init_us(RNG_TIMEOUT_US);
157*c99311c8SEtienne Carriere 	burst_timeout = false;
158f3c22059SEtienne Carriere 
159f3c22059SEtienne Carriere 	while (out_size < size) {
160f3c22059SEtienne Carriere 		/* Read by chunks of the size the RNG FIFO depth */
161f3c22059SEtienne Carriere 		size_t sz = size - out_size;
162f3c22059SEtienne Carriere 
163f3c22059SEtienne Carriere 		exceptions = may_spin_lock(&stm32_rng->lock);
164f3c22059SEtienne Carriere 
165*c99311c8SEtienne Carriere 		rc = read_available(rng_base, out_ptr, &sz);
166*c99311c8SEtienne Carriere 
167*c99311c8SEtienne Carriere 		/* Raise timeout only if we failed to get some samples */
168*c99311c8SEtienne Carriere 		assert(!rc || rc == TEE_ERROR_NO_DATA);
169*c99311c8SEtienne Carriere 		if (rc)
170*c99311c8SEtienne Carriere 			burst_timeout = timeout_elapsed(timeout_ref);
171f3c22059SEtienne Carriere 
172f3c22059SEtienne Carriere 		may_spin_unlock(&stm32_rng->lock, exceptions);
173f3c22059SEtienne Carriere 
174*c99311c8SEtienne Carriere 		if (burst_timeout) {
175*c99311c8SEtienne Carriere 			rc = TEE_ERROR_GENERIC;
176*c99311c8SEtienne Carriere 			goto out;
177f3c22059SEtienne Carriere 		}
178f3c22059SEtienne Carriere 
179*c99311c8SEtienne Carriere 		if (!rc) {
180*c99311c8SEtienne Carriere 			out_size += sz;
181*c99311c8SEtienne Carriere 			out_ptr += sz;
182*c99311c8SEtienne Carriere 			/* Re-arm timeout */
183*c99311c8SEtienne Carriere 			timeout_ref = timeout_init_us(RNG_TIMEOUT_US);
184*c99311c8SEtienne Carriere 			burst_timeout = false;
185*c99311c8SEtienne Carriere 		}
186*c99311c8SEtienne Carriere 	}
187*c99311c8SEtienne Carriere 
188*c99311c8SEtienne Carriere out:
189*c99311c8SEtienne Carriere 	assert(!rc || rc == TEE_ERROR_GENERIC);
190f3c22059SEtienne Carriere 	gate_rng(false, stm32_rng);
191f3c22059SEtienne Carriere 
192f3c22059SEtienne Carriere 	return rc;
193f3c22059SEtienne Carriere }
194f3c22059SEtienne Carriere 
195f3c22059SEtienne Carriere #ifdef CFG_EMBED_DTB
196f3c22059SEtienne Carriere static TEE_Result stm32_rng_init(void)
197f3c22059SEtienne Carriere {
198f3c22059SEtienne Carriere 	void *fdt = NULL;
199f3c22059SEtienne Carriere 	int node = -1;
200f3c22059SEtienne Carriere 	struct dt_node_info dt_info;
201d7a1a7d2SEtienne Carriere 	TEE_Result res = TEE_ERROR_GENERIC;
202f3c22059SEtienne Carriere 
203f3c22059SEtienne Carriere 	memset(&dt_info, 0, sizeof(dt_info));
204f3c22059SEtienne Carriere 
205f3c22059SEtienne Carriere 	fdt = get_embedded_dt();
206f3c22059SEtienne Carriere 	if (!fdt)
207f3c22059SEtienne Carriere 		panic();
208f3c22059SEtienne Carriere 
209f3c22059SEtienne Carriere 	while (true) {
210f3c22059SEtienne Carriere 		node = fdt_node_offset_by_compatible(fdt, node, DT_RNG_COMPAT);
211f3c22059SEtienne Carriere 		if (node < 0)
212f3c22059SEtienne Carriere 			break;
213f3c22059SEtienne Carriere 
214f3c22059SEtienne Carriere 		_fdt_fill_device_info(fdt, &dt_info, node);
215f3c22059SEtienne Carriere 
216f3c22059SEtienne Carriere 		if (!(dt_info.status & DT_STATUS_OK_SEC))
217f3c22059SEtienne Carriere 			continue;
218f3c22059SEtienne Carriere 
219f3c22059SEtienne Carriere 		if (stm32_rng)
220f3c22059SEtienne Carriere 			panic();
221f3c22059SEtienne Carriere 
222f3c22059SEtienne Carriere 		stm32_rng = calloc(1, sizeof(*stm32_rng));
223f3c22059SEtienne Carriere 		if (!stm32_rng)
224f3c22059SEtienne Carriere 			panic();
225f3c22059SEtienne Carriere 
226f3c22059SEtienne Carriere 		assert(dt_info.clock != DT_INFO_INVALID_CLOCK &&
227aabd492eSLionel Debieve 		       dt_info.reg != DT_INFO_INVALID_REG &&
228aabd492eSLionel Debieve 		       dt_info.reg_size != DT_INFO_INVALID_REG_SIZE);
229f3c22059SEtienne Carriere 
23028f25d8dSEtienne Carriere 		if (dt_info.status & DT_STATUS_OK_NSEC) {
23128f25d8dSEtienne Carriere 			stm32mp_register_non_secure_periph_iomem(dt_info.reg);
232d8682c4cSEtienne Carriere 			stm32_rng->release_post_boot = true;
23328f25d8dSEtienne Carriere 		} else {
23428f25d8dSEtienne Carriere 			stm32mp_register_secure_periph_iomem(dt_info.reg);
23528f25d8dSEtienne Carriere 		}
23628f25d8dSEtienne Carriere 
237f3c22059SEtienne Carriere 		stm32_rng->base.pa = dt_info.reg;
238d8682c4cSEtienne Carriere 		if (!io_pa_or_va_secure(&stm32_rng->base, dt_info.reg_size))
239d8682c4cSEtienne Carriere 			panic();
24068c4a16bSEtienne Carriere 
241d7a1a7d2SEtienne Carriere 		res = clk_dt_get_by_index(fdt, node, 0, &stm32_rng->clock);
242d7a1a7d2SEtienne Carriere 		if (res)
243d7a1a7d2SEtienne Carriere 			return res;
244d7a1a7d2SEtienne Carriere 
245d7a1a7d2SEtienne Carriere 		assert(stm32_rng->clock);
246f3c22059SEtienne Carriere 
247f3c22059SEtienne Carriere 		DMSG("RNG init");
248f3c22059SEtienne Carriere 	}
249f3c22059SEtienne Carriere 
250f3c22059SEtienne Carriere 	return TEE_SUCCESS;
251f3c22059SEtienne Carriere }
252f3c22059SEtienne Carriere 
253d8682c4cSEtienne Carriere early_init_late(stm32_rng_init);
254d8682c4cSEtienne Carriere 
255d8682c4cSEtienne Carriere static TEE_Result stm32_rng_release(void)
256d8682c4cSEtienne Carriere {
257d8682c4cSEtienne Carriere 	if (stm32_rng && stm32_rng->release_post_boot) {
258d8682c4cSEtienne Carriere 		DMSG("Release RNG driver");
259d8682c4cSEtienne Carriere 		assert(!stm32_rng->refcount);
260d8682c4cSEtienne Carriere 		free(stm32_rng);
261d8682c4cSEtienne Carriere 		stm32_rng = NULL;
262d8682c4cSEtienne Carriere 	}
263d8682c4cSEtienne Carriere 
264d8682c4cSEtienne Carriere 	return TEE_SUCCESS;
265d8682c4cSEtienne Carriere }
266d8682c4cSEtienne Carriere 
267d8682c4cSEtienne Carriere release_init_resource(stm32_rng_release);
268f3c22059SEtienne Carriere #endif /*CFG_EMBED_DTB*/
269