summaryrefslogtreecommitdiff
path: root/FreeRTOS/Demo/T-HEAD_CB2201_CDK/csi/csi_driver/csky/common/trng/ck_trng.c
blob: 33be9eff1d654d83edaeff414e1dfa4aafffaada (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
 * Copyright (C) 2017 C-SKY Microsystems Co., Ltd. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/******************************************************************************
 * @file     ck_trng.c
 * @brief    CSI Source File for TRNG Driver
 * @version  V1.0
 * @date     02. June 2017
 ******************************************************************************/
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "drv_trng.h"
#include "ck_trng.h"


#define ERR_TRNG(errno) (CSI_DRV_ERRNO_TRNG_BASE | errno)
#define TRNG_NULL_PARAM_CHK(para)                         \
        do {                                        \
            if (para == NULL) {                     \
                return ERR_TRNG(EDRV_PARAMETER);   \
            }                                       \
        } while (0)

typedef struct {
    uint32_t base;
    trng_event_cb_t cb;
    trng_status_t status;
} ck_trng_priv_t;

extern int32_t target_get_trng_count(void);
extern int32_t target_get_trng(int32_t idx, uint32_t *base);

static ck_trng_priv_t trng_handle[CONFIG_TRNG_NUM];

/* Driver Capabilities */
static const trng_capabilities_t driver_capabilities = {
    .lowper_mode = 1 /* low power mode */
};

extern int32_t target_get_trng(int32_t idx, uint32_t *base);
extern int32_t target_get_trng_count(void);
//
// Functions
//

ck_trng_reg_t *trng_reg = NULL;

static int32_t trng_enable(void)
{
    trng_reg->TCR |= TRNG_EN;
    return 0;
}

static int32_t trng_get_data(void)
{
    int data = trng_reg->TDR;
    return data;
}

static int32_t trng_data_is_ready(void)
{
    int flag = (trng_reg->TCR & TRNG_DATA_READY);
    return flag;
}

/**
  \brief       get trng handle count.
  \return      trng handle count
*/
int32_t csi_trng_get_instance_count(void)
{
    return target_get_trng_count();
}

/**
  \brief       Initialize TRNG Interface. 1. Initializes the resources needed for the TRNG interface 2.registers event callback function
  \param[in]   idx  must not exceed return value of csi_trng_get_instance_count()
  \param[in]   cb_event  Pointer to \ref trng_event_cb_t
  \return      pointer to trng handle
*/
trng_handle_t csi_trng_initialize(int32_t idx, trng_event_cb_t cb_event)
{

    if (idx < 0 || idx >= CONFIG_TRNG_NUM) {
        return NULL;
    }

    /* obtain the trng information */
    uint32_t base = 0u;
    int32_t real_idx = target_get_trng(idx, &base);

    if (real_idx != idx) {
        return NULL;
    }

    ck_trng_priv_t *trng_priv = &trng_handle[idx];
    trng_priv->base = base;

    /* initialize the trng context */
    trng_reg = (ck_trng_reg_t *)(trng_priv->base);
    trng_priv->cb = cb_event;
    trng_priv->status.busy = 0;
    trng_priv->status.data_valid = 0;

    return (trng_handle_t)trng_priv;
}

/**
  \brief       De-initialize TRNG Interface. stops operation and releases the software resources used by the interface
  \param[in]   handle  trng handle to operate.
  \return      error code
*/
int32_t csi_trng_uninitialize(trng_handle_t handle)
{
    TRNG_NULL_PARAM_CHK(handle);

    ck_trng_priv_t *trng_priv = handle;
    trng_priv->cb = NULL;

    return 0;
}

/**
  \brief       Get driver capabilities.
  \param[in]   trng handle to operate.
  \return      \ref trng_capabilities_t
*/
trng_capabilities_t csi_trng_get_capabilities(trng_handle_t handle)
{
    return driver_capabilities;
}

/**
  \brief       Get data from the TRNG.
  \param[in]   handle  trng handle to operate.
  \param[out]  data  Pointer to buffer with data get from TRNG
  \param[in]   num   Number of data items to obtain
  \return      error code
*/
int32_t csi_trng_get_data(trng_handle_t handle, void *data, uint32_t num)
{
    TRNG_NULL_PARAM_CHK(handle);
    TRNG_NULL_PARAM_CHK(data);
    TRNG_NULL_PARAM_CHK(num);

    ck_trng_priv_t *trng_priv = handle;

    trng_priv->status.busy = 1U;
    trng_priv->status.data_valid = 0U;

    uint8_t left_len = (uint32_t)data & 0x3;
    uint32_t result = 0;

    /* if the data addr is not aligned by word */
    if (left_len) {
        trng_enable();
        while (!trng_data_is_ready());
        result = trng_get_data();
        /* wait the data is ready */
        while (trng_data_is_ready());

        if (num > (4 - left_len)) {
            memcpy(data, &result, 4 - left_len);
        } else {
            memcpy(data, &result, num);
            trng_priv->status.busy = 0U;
            trng_priv->status.data_valid = 1U;

            if (trng_priv->cb) {
                trng_priv->cb(TRNG_EVENT_DATA_GENERATE_COMPLETE);
            }
            return 0;
        }
        num -= (4 - left_len);
        data += (4 - left_len);
    }

    uint32_t word_len = num >> 2;
    left_len = num & 0x3;

    /* obtain the data by word */
    while (word_len--) {
        trng_enable();
        while (!trng_data_is_ready());
        result = trng_get_data();
        while (trng_data_is_ready());
        *(uint32_t *)data = result;
        data = (void *)((uint32_t)data + 4);
    }

    /* if the num is not aligned by word */
    if (left_len) {
        trng_enable();
        while (!trng_data_is_ready());
        result = trng_get_data();
        while (trng_data_is_ready());
        memcpy(data, &result, left_len);
    }

    trng_priv->status.busy = 0U;
    trng_priv->status.data_valid = 1U;

    if (trng_priv->cb) {
        trng_priv->cb(TRNG_EVENT_DATA_GENERATE_COMPLETE);
    }

    return 0;
}

/**
  \brief       Get TRNG status.
  \param[in]   handle  trng handle to operate.
  \return      TRNG status \ref trng_status_t
*/
trng_status_t csi_trng_get_status(trng_handle_t handle)
{
    ck_trng_priv_t *trng_priv = handle;
    return trng_priv->status;
}