1 /*
2  * Copyright (c) 2020, The Linux Foundation. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 #include <stddef.h>
7 
8 #include <lib/mmio.h>
9 
10 #include <qti_rng.h>
11 #include <qti_rng_io.h>
12 
qti_rng_get_data(uint8_t * out,uint32_t out_len)13 int qti_rng_get_data(uint8_t *out, uint32_t out_len)
14 {
15 	uint32_t tmp_rndm = 0;
16 	uint32_t bytes_left = out_len;
17 	int i = 0;
18 
19 	if (NULL == out || 0 == out_len) {
20 		return -1;
21 	}
22 
23 	/*
24 	 * RNG HW initialized at previous boot image.
25 	 * RNG clocks are expected to be ON.
26 	 */
27 
28 	do {
29 		/* There is no data to read */
30 		if ((mmio_read_32(SEC_PRNG_STATUS) &
31 		     SEC_PRNG_STATUS_DATA_AVAIL_BMSK) == 0) {
32 			continue;
33 		}
34 
35 		while ((tmp_rndm = mmio_read_32(SEC_PRNG_DATA_OUT)) == 0) {
36 			;
37 		}
38 
39 		for (i = 0; i < 4; i++) {
40 			*out = (uint8_t) (tmp_rndm >> (8 * i));
41 
42 			out++;
43 			bytes_left--;
44 
45 			if (bytes_left == 0) {
46 				break;
47 			}
48 		}
49 
50 	} while (bytes_left != 0);
51 
52 	return 0;
53 }
54