blob: f63f3b85dc7274681df3994ec10526674b16dcbc [file] [log] [blame]
Saurabh Gorecha87f47372020-07-09 02:20:08 +05301/*
2 * Copyright (c) 2020, The Linux Foundation. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6#include <stddef.h>
Saurabh Gorecha87f47372020-07-09 02:20:08 +05307
8#include <lib/mmio.h>
9
Manish V Badarkhed3074912020-08-17 06:44:20 +010010#include <qti_rng.h>
Saurabh Gorecha87f47372020-07-09 02:20:08 +053011#include <qti_rng_io.h>
12
13int 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}