xref: /optee_os/core/drivers/hi16xx_rng.c (revision 817466cb476de705a8e3dabe1ef165fe27a18c2f)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2016, Linaro Limited
4  */
5 
6 /* Driver for the internal Random Number Generator of HiSilicon P660/Hi16xx */
7 
8 #include <initcall.h>
9 #include <io.h>
10 #include <kernel/mutex.h>
11 #include <kernel/tee_time.h>
12 #include <mm/core_memprot.h>
13 #include <mm/core_mmu.h>
14 #include <platform_config.h>
15 #include <rng_support.h>
16 #include <trace.h>
17 #include <types_ext.h>
18 #include <util.h>
19 
20 /* ALG sub-controller registers */
21 
22 #define ALG_SC_RNG_RESET_DREQ	0xAB4	/* RNG reset cancel */
23 #  define ALG_SC_SRST_DREQ_RNG	BIT(0)
24 
25 /* RNG registers */
26 
27 #define	RNG_SEED	0x0	/* Initial seed */
28 #define RNG_CTRL	0x4	/* Control register */
29 #  define RNG_SEED_SEL	BIT(2)	/* Re-seed source: 1: ring osc., 0: LFSR */
30 #  define RNG_RING_EN	BIT(1)	/* Enable ring oscillator */
31 #  define RNG_EN	BIT(0)	/* Enable RNG */
32 #define RNG_NUM		0x10	/* Random number output */
33 #define RNG_PHY_SEED	0x14	/* Ring oscillator output */
34 
35 register_phys_mem(MEM_AREA_IO_SEC, ALG_SC_BASE, ALG_SC_REG_SIZE);
36 register_phys_mem(MEM_AREA_IO_SEC, RNG_BASE, RNG_REG_SIZE);
37 
38 static struct mutex rng_mutex = MUTEX_INITIALIZER;
39 
40 static TEE_Result hi16xx_rng_init(void)
41 {
42 	vaddr_t alg = (vaddr_t)phys_to_virt(ALG_SC_BASE, MEM_AREA_IO_SEC);
43 	vaddr_t rng = (vaddr_t)phys_to_virt(RNG_BASE, MEM_AREA_IO_SEC);
44 	TEE_Time time;
45 
46 	/* ALG sub-controller must allow RNG out of reset */
47 	write32(ALG_SC_SRST_DREQ_RNG, alg + ALG_SC_RNG_RESET_DREQ);
48 
49 	/* Set initial seed */
50 	tee_time_get_sys_time(&time);
51 	write32(time.seconds * 1000 + time.millis, rng + RNG_SEED);
52 
53 	/*
54 	 * Enable RNG and configure it to re-seed automatically from the
55 	 * internal ring oscillator
56 	 */
57 	write32(RNG_EN | RNG_RING_EN | RNG_SEED_SEL, rng + RNG_CTRL);
58 
59 	IMSG("Hi16xx RNG initialized");
60 	return TEE_SUCCESS;
61 }
62 
63 uint8_t hw_get_random_byte(void)
64 {
65 	static vaddr_t r;
66 	static int pos;
67 	static union {
68 		uint32_t val;
69 		uint8_t byte[4];
70 	} random;
71 	uint8_t ret;
72 
73 	mutex_lock(&rng_mutex);
74 
75 	if (!r)
76 		r = (vaddr_t)phys_to_virt(RNG_BASE, MEM_AREA_IO_SEC) + RNG_NUM;
77 
78 	if (!pos)
79 		random.val = read32(r);
80 
81 	ret = random.byte[pos++];
82 
83 	if (pos == 4)
84 		pos = 0;
85 
86 	mutex_unlock(&rng_mutex);
87 
88 	return ret;
89 }
90 
91 driver_init(hi16xx_rng_init);
92