forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entropy_cc13xx_cc26xx.c
354 lines (292 loc) · 8.54 KB
/
entropy_cc13xx_cc26xx.c
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
* Copyright (c) 2019 Brett Witherspoon
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT ti_cc13xx_cc26xx_trng
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/drivers/entropy.h>
#include <zephyr/irq.h>
#include <zephyr/pm/policy.h>
#include <zephyr/pm/device.h>
#include <zephyr/sys/ring_buffer.h>
#include <zephyr/sys/sys_io.h>
#include <driverlib/prcm.h>
#include <driverlib/trng.h>
#include <ti/drivers/Power.h>
#include <ti/drivers/power/PowerCC26X2.h>
#define CPU_FREQ DT_PROP(DT_PATH(cpus, cpu_0), clock_frequency)
#define US_PER_SAMPLE (1000000ULL * \
CONFIG_ENTROPY_CC13XX_CC26XX_SAMPLES_PER_CYCLE / CPU_FREQ + 1ULL)
struct entropy_cc13xx_cc26xx_data {
struct k_sem lock;
struct k_sem sync;
struct ring_buf pool;
uint8_t data[CONFIG_ENTROPY_CC13XX_CC26XX_POOL_SIZE];
#ifdef CONFIG_PM
Power_NotifyObj post_notify;
bool constrained;
#endif
};
static void start_trng(struct entropy_cc13xx_cc26xx_data *data)
{
/* Initialization as described in TRM section 18.6.1.2 */
TRNGReset();
while (sys_read32(TRNG_BASE + TRNG_O_SWRESET)) {
continue;
}
/* Set samples per cycle */
TRNGConfigure(0, CONFIG_ENTROPY_CC13XX_CC26XX_SAMPLES_PER_CYCLE,
0);
/* De-tune FROs */
sys_write32(TRNG_FRODETUNE_FRO_MASK_M, TRNG_BASE +
TRNG_O_FRODETUNE);
/* Enable FROs */
sys_write32(TRNG_FROEN_FRO_MASK_M, TRNG_BASE + TRNG_O_FROEN);
/* Set shutdown and alarm thresholds */
sys_write32((CONFIG_ENTROPY_CC13XX_CC26XX_SHUTDOWN_THRESHOLD
<< 16) | CONFIG_ENTROPY_CC13XX_CC26XX_ALARM_THRESHOLD,
TRNG_BASE + TRNG_O_ALARMCNT);
TRNGEnable();
TRNGIntEnable(TRNG_NUMBER_READY | TRNG_FRO_SHUTDOWN);
}
#ifdef CONFIG_PM_DEVICE
static void stop_trng(struct entropy_cc13xx_cc26xx_data *data)
{
TRNGDisable();
TRNGIntClear(TRNG_NUMBER_READY | TRNG_FRO_SHUTDOWN);
TRNGIntDisable(TRNG_NUMBER_READY | TRNG_FRO_SHUTDOWN);
}
#endif
static void handle_shutdown_ovf(void)
{
uint32_t off;
/* Clear shutdown */
TRNGIntClear(TRNG_FRO_SHUTDOWN);
/* Disabled FROs */
off = sys_read32(TRNG_BASE + TRNG_O_ALARMSTOP);
/* Clear alarms */
sys_write32(0, TRNG_BASE + TRNG_O_ALARMMASK);
sys_write32(0, TRNG_BASE + TRNG_O_ALARMSTOP);
/* De-tune FROs */
sys_write32(off, TRNG_BASE + TRNG_O_FRODETUNE);
/* Re-enable FROs */
sys_write32(off, TRNG_BASE + TRNG_O_FROEN);
}
static int entropy_cc13xx_cc26xx_get_entropy(const struct device *dev,
uint8_t *buf,
uint16_t len)
{
struct entropy_cc13xx_cc26xx_data *data = dev->data;
uint32_t cnt;
#ifdef CONFIG_PM
unsigned int key = irq_lock();
if (!data->constrained) {
pm_policy_state_lock_get(PM_STATE_STANDBY, PM_ALL_SUBSTATES);
data->constrained = true;
}
irq_unlock(key);
#endif
TRNGIntEnable(TRNG_NUMBER_READY);
while (len) {
k_sem_take(&data->lock, K_FOREVER);
cnt = ring_buf_get(&data->pool, buf, len);
k_sem_give(&data->lock);
if (cnt) {
buf += cnt;
len -= cnt;
} else {
k_sem_take(&data->sync, K_FOREVER);
}
}
return 0;
}
static void entropy_cc13xx_cc26xx_isr(const struct device *dev)
{
struct entropy_cc13xx_cc26xx_data *data = dev->data;
uint32_t src = 0;
uint32_t cnt;
uint32_t num[2];
/* Interrupt service routine as described in TRM section 18.6.1.3.2 */
src = TRNGStatusGet();
if (src & TRNG_NUMBER_READY) {
/* This function acknowledges the ready status */
num[1] = TRNGNumberGet(TRNG_HI_WORD);
num[0] = TRNGNumberGet(TRNG_LOW_WORD);
cnt = ring_buf_put(&data->pool, (uint8_t *)num, sizeof(num));
/* When pool is full disable interrupt and stop reading numbers */
if (cnt != sizeof(num)) {
#ifdef CONFIG_PM
if (data->constrained) {
pm_policy_state_lock_put(
PM_STATE_STANDBY,
PM_ALL_SUBSTATES);
data->constrained = false;
}
#endif
TRNGIntDisable(TRNG_NUMBER_READY);
}
k_sem_give(&data->sync);
}
/* Change the shutdown FROs' oscillating frequency in an attempt to
* prevent further locking on to the sampling clock frequency.
*/
if (src & TRNG_FRO_SHUTDOWN) {
handle_shutdown_ovf();
}
}
static int entropy_cc13xx_cc26xx_get_entropy_isr(const struct device *dev,
uint8_t *buf, uint16_t len,
uint32_t flags)
{
struct entropy_cc13xx_cc26xx_data *data = dev->data;
uint16_t cnt;
uint16_t read = len;
uint32_t src;
uint32_t num[2];
unsigned int key;
key = irq_lock();
cnt = ring_buf_get(&data->pool, buf, len);
irq_unlock(key);
if ((cnt == len) || ((flags & ENTROPY_BUSYWAIT) == 0U)) {
read = cnt;
} else {
buf += cnt;
len -= cnt;
/* Allowed to busy-wait. We should use a polling approach */
while (len) {
key = irq_lock();
src = TRNGStatusGet();
if (src & TRNG_NUMBER_READY) {
/*
* This function acknowledges the ready
* status
*/
num[1] = TRNGNumberGet(TRNG_HI_WORD);
num[0] = TRNGNumberGet(TRNG_LOW_WORD);
ring_buf_put(&data->pool, (uint8_t *)num,
sizeof(num));
}
/*
* If interrupts were enabled during busy wait, this
* would allow us to pick up anything that has been put
* in by the ISR as well.
*/
cnt = ring_buf_get(&data->pool, buf, len);
if (src & TRNG_FRO_SHUTDOWN) {
handle_shutdown_ovf();
}
irq_unlock(key);
if (cnt) {
buf += cnt;
len -= cnt;
} else {
k_busy_wait(US_PER_SAMPLE);
}
}
}
return read;
}
#ifdef CONFIG_PM
/*
* ======== post_notify_fxn ========
* Called by Power module when waking up the CPU from Standby. The TRNG needs
* to be reconfigured afterwards, unless Zephyr's device PM turned it off, in
* which case it'd be responsible for turning it back on and reconfiguring it.
*/
static int post_notify_fxn(unsigned int eventType, uintptr_t eventArg,
uintptr_t clientArg)
{
const struct device *dev = (const struct device *)clientArg;
int ret = Power_NOTIFYDONE;
int16_t res_id;
/* Reconfigure the hardware if returning from sleep */
if (eventType == PowerCC26XX_AWAKE_STANDBY) {
res_id = PowerCC26XX_PERIPH_TRNG;
if (Power_getDependencyCount(res_id) != 0) {
/* Reconfigure and enable TRNG only if powered */
start_trng(dev->data);
}
}
return (ret);
}
#endif
#ifdef CONFIG_PM_DEVICE
static int entropy_cc13xx_cc26xx_pm_action(const struct device *dev,
enum pm_device_action action)
{
struct entropy_cc13xx_cc26xx_data *data = dev->data;
switch (action) {
case PM_DEVICE_ACTION_RESUME:
Power_setDependency(PowerCC26XX_PERIPH_TRNG);
start_trng(data);
break;
case PM_DEVICE_ACTION_SUSPEND:
stop_trng(data);
Power_releaseDependency(PowerCC26XX_PERIPH_TRNG);
break;
default:
return -ENOTSUP;
}
return 0;
}
#endif /* CONFIG_PM_DEVICE */
static int entropy_cc13xx_cc26xx_init(const struct device *dev)
{
struct entropy_cc13xx_cc26xx_data *data = dev->data;
/* Initialize driver data */
ring_buf_init(&data->pool, sizeof(data->data), data->data);
#if defined(CONFIG_PM)
Power_setDependency(PowerCC26XX_PERIPH_TRNG);
/* Stay out of standby until buffer is filled with entropy */
pm_policy_state_lock_get(PM_STATE_STANDBY, PM_ALL_SUBSTATES);
data->constrained = true;
/* Register notification function */
Power_registerNotify(&data->post_notify,
PowerCC26XX_AWAKE_STANDBY,
post_notify_fxn, (uintptr_t)dev);
#else
/* Power TRNG domain */
PRCMPowerDomainOn(PRCM_DOMAIN_PERIPH);
/* Enable TRNG peripheral clocks */
PRCMPeripheralRunEnable(PRCM_PERIPH_TRNG);
/* Enabled the TRNG while in sleep mode to keep the entropy pool full. After
* the pool is full the TRNG will enter idle mode when random numbers are no
* longer being read. */
PRCMPeripheralSleepEnable(PRCM_PERIPH_TRNG);
PRCMPeripheralDeepSleepEnable(PRCM_PERIPH_TRNG);
/* Load PRCM settings */
PRCMLoadSet();
while (!PRCMLoadGet()) {
continue;
}
/* Peripherals should not be accessed until power domain is on. */
while (PRCMPowerDomainsAllOn(PRCM_DOMAIN_PERIPH) !=
PRCM_DOMAIN_POWER_ON) {
continue;
}
#endif
start_trng(data);
IRQ_CONNECT(DT_INST_IRQN(0),
DT_INST_IRQ(0, priority),
entropy_cc13xx_cc26xx_isr,
DEVICE_DT_INST_GET(0), 0);
irq_enable(DT_INST_IRQN(0));
return 0;
}
static const struct entropy_driver_api entropy_cc13xx_cc26xx_driver_api = {
.get_entropy = entropy_cc13xx_cc26xx_get_entropy,
.get_entropy_isr = entropy_cc13xx_cc26xx_get_entropy_isr,
};
static struct entropy_cc13xx_cc26xx_data entropy_cc13xx_cc26xx_data = {
.lock = Z_SEM_INITIALIZER(entropy_cc13xx_cc26xx_data.lock, 1, 1),
.sync = Z_SEM_INITIALIZER(entropy_cc13xx_cc26xx_data.sync, 0, 1),
};
PM_DEVICE_DT_INST_DEFINE(0, entropy_cc13xx_cc26xx_pm_action);
DEVICE_DT_INST_DEFINE(0,
entropy_cc13xx_cc26xx_init,
PM_DEVICE_DT_INST_GET(0),
&entropy_cc13xx_cc26xx_data, NULL,
PRE_KERNEL_1, CONFIG_ENTROPY_INIT_PRIORITY,
&entropy_cc13xx_cc26xx_driver_api);