1 /*
2  * Copyright (c) 2017 Oticon A/S
3  * Copyright (c) 2023 Nordic Semiconductor ASA
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  *
7  * Note that the function prototypes are taken from the NRFx HAL
8  */
9 #include "hal/nrf_ccm.h"
10 #include "bs_tracing.h"
11 #include "NHW_AES_CCM.h"
12 
nrf_ccm_task_trigger(NRF_CCM_Type * p_reg,nrf_ccm_task_t task)13 void nrf_ccm_task_trigger(NRF_CCM_Type * p_reg, nrf_ccm_task_t task)
14 {
15   *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)task)) = 0x1UL;
16 
17   if ( task == NRF_CCM_TASK_KSGEN ) {
18     nhw_CCM_regw_sideeffects_TASKS_KSGEN();
19   } else if ( task == NRF_CCM_TASK_CRYPT ) {
20     nhw_CCM_regw_sideeffects_TASKS_CRYPT();
21   } else if ( task == NRF_CCM_TASK_STOP ) {
22     nhw_CCM_regw_sideeffects_TASKS_STOP();
23   } else {
24     bs_trace_error_line_time("Not supported task started in nrf_ccm\n");
25   }
26 }
27 
nrf_ccm_int_enable(NRF_CCM_Type * p_reg,uint32_t mask)28 void nrf_ccm_int_enable(NRF_CCM_Type * p_reg, uint32_t mask)
29 {
30   p_reg->INTENSET = mask;
31   nhw_CCM_regw_sideeffects_INTENSET();
32 }
33 
nrf_ccm_int_disable(NRF_CCM_Type * p_reg,uint32_t mask)34 void nrf_ccm_int_disable(NRF_CCM_Type * p_reg, uint32_t mask)
35 {
36   p_reg->INTENCLR = mask;
37   nhw_CCM_regw_sideeffects_INTENCLR();
38 }
39 
nrf_ccm_event_clear(NRF_CCM_Type * p_reg,nrf_ccm_event_t event)40 void nrf_ccm_event_clear(NRF_CCM_Type * p_reg, nrf_ccm_event_t event)
41 {
42   *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event)) = 0x0UL;
43   nhw_CCM_regw_sideeffects_EVENTS_all(0);
44 }
45 
46 #if defined(DPPI_PRESENT)
47 
nrf_ccm_subscribe_common(NRF_CCM_Type * p_reg,nrf_ccm_task_t task)48 static void nrf_ccm_subscribe_common(NRF_CCM_Type * p_reg,
49                                      nrf_ccm_task_t task)
50 {
51   if (task == NRF_CCM_TASK_KSGEN) {
52       nhw_CCM_regw_sideeffects_SUBSCRIBE_KSGEN(0);
53   } else if ( task == NRF_CCM_TASK_CRYPT ) {
54       nhw_CCM_regw_sideeffects_SUBSCRIBE_CRYPT(0);
55   } else if ( task == NRF_CCM_TASK_STOP ) {
56       nhw_CCM_regw_sideeffects_SUBSCRIBE_STOP(0);
57   } else if ( task == NRF_CCM_TASK_RATEOVERRIDE ) {
58       nhw_CCM_regw_sideeffects_SUBSCRIBE_RATEOVERRIDE(0);
59   } else {
60       bs_trace_error_line_time("Attempted to subscribe to an not-supported task in the nrf_ccm (%i)\n",
61                                task);
62   }
63 }
64 
nrf_ccm_subscribe_set(NRF_CCM_Type * p_reg,nrf_ccm_task_t task,uint8_t channel)65 void nrf_ccm_subscribe_set(NRF_CCM_Type * p_reg,
66                            nrf_ccm_task_t task,
67                            uint8_t        channel)
68 {
69     *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) task + 0x80uL)) =
70             ((uint32_t)channel | NRF_SUBSCRIBE_PUBLISH_ENABLE);
71     nrf_ccm_subscribe_common(p_reg, task);
72 }
73 
nrf_ccm_subscribe_clear(NRF_CCM_Type * p_reg,nrf_ccm_task_t task)74 void nrf_ccm_subscribe_clear(NRF_CCM_Type * p_reg,
75                              nrf_ccm_task_t task)
76 {
77     *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) task + 0x80uL)) = 0;
78     nrf_ccm_subscribe_common(p_reg, task);
79 }
80 
81 #endif /* defined(DPPI_PRESENT) */
82