1 /*
2  * Copyright (c) 2016-2019 Nordic Semiconductor ASA
3  * Copyright (c) 2016 Vinayak Kariappa Chettimada
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include <soc.h>
9 
10 #include "hal/nrf5/swi.h"
11 
12 #include "util/memq.h"
13 #include "util/mayfly.h"
14 
15 #include "ll_sw/lll.h"
16 
17 #include "hal/debug.h"
18 
19 #define MAYFLY_CALL_ID_LLL    TICKER_USER_ID_LLL
20 #define MAYFLY_CALL_ID_WORKER TICKER_USER_ID_ULL_HIGH
21 #define MAYFLY_CALL_ID_JOB    TICKER_USER_ID_ULL_LOW
22 
mayfly_enable_cb(uint8_t caller_id,uint8_t callee_id,uint8_t enable)23 void mayfly_enable_cb(uint8_t caller_id, uint8_t callee_id, uint8_t enable)
24 {
25 	(void)caller_id;
26 
27 	switch (callee_id) {
28 	case MAYFLY_CALL_ID_WORKER:
29 		if (enable) {
30 			irq_enable(HAL_SWI_WORKER_IRQ);
31 		} else {
32 			irq_disable(HAL_SWI_WORKER_IRQ);
33 		}
34 		break;
35 
36 	case MAYFLY_CALL_ID_JOB:
37 		if (enable) {
38 			irq_enable(HAL_SWI_JOB_IRQ);
39 		} else {
40 			irq_disable(HAL_SWI_JOB_IRQ);
41 		}
42 		break;
43 
44 	default:
45 		LL_ASSERT(0);
46 		break;
47 	}
48 }
49 
mayfly_is_enabled(uint8_t caller_id,uint8_t callee_id)50 uint32_t mayfly_is_enabled(uint8_t caller_id, uint8_t callee_id)
51 {
52 	(void)caller_id;
53 
54 	switch (callee_id) {
55 	case MAYFLY_CALL_ID_LLL:
56 		return irq_is_enabled(HAL_SWI_RADIO_IRQ);
57 
58 	case MAYFLY_CALL_ID_WORKER:
59 		return irq_is_enabled(HAL_SWI_WORKER_IRQ);
60 
61 	case MAYFLY_CALL_ID_JOB:
62 		return irq_is_enabled(HAL_SWI_JOB_IRQ);
63 
64 	default:
65 		LL_ASSERT(0);
66 		break;
67 	}
68 
69 	return 0;
70 }
71 
mayfly_prio_is_equal(uint8_t caller_id,uint8_t callee_id)72 uint32_t mayfly_prio_is_equal(uint8_t caller_id, uint8_t callee_id)
73 {
74 	return 0 ||
75 #if defined(CONFIG_BT_CTLR_ZLI)
76 		((caller_id != MAYFLY_CALL_ID_LLL) &&
77 		 (callee_id != MAYFLY_CALL_ID_LLL) &&
78 		 (caller_id == callee_id)) ||
79 		((caller_id == MAYFLY_CALL_ID_LLL) &&
80 		 (caller_id == callee_id)) ||
81 #else /* !CONFIG_BT_CTLR_ZLI */
82 		(caller_id == callee_id) ||
83 #if (CONFIG_BT_CTLR_LLL_PRIO == CONFIG_BT_CTLR_ULL_HIGH_PRIO)
84 	       ((caller_id == MAYFLY_CALL_ID_LLL) &&
85 		(callee_id == MAYFLY_CALL_ID_WORKER)) ||
86 	       ((caller_id == MAYFLY_CALL_ID_WORKER) &&
87 		(callee_id == MAYFLY_CALL_ID_LLL)) ||
88 #endif
89 #if (CONFIG_BT_CTLR_LLL_PRIO == CONFIG_BT_CTLR_ULL_LOW_PRIO)
90 	       ((caller_id == MAYFLY_CALL_ID_LLL) &&
91 		(callee_id == MAYFLY_CALL_ID_JOB)) ||
92 	       ((caller_id == MAYFLY_CALL_ID_JOB) &&
93 		(callee_id == MAYFLY_CALL_ID_LLL)) ||
94 #endif
95 #endif /* !CONFIG_BT_CTLR_ZLI */
96 #if (CONFIG_BT_CTLR_ULL_HIGH_PRIO == CONFIG_BT_CTLR_ULL_LOW_PRIO)
97 	       ((caller_id == MAYFLY_CALL_ID_WORKER) &&
98 		(callee_id == MAYFLY_CALL_ID_JOB)) ||
99 	       ((caller_id == MAYFLY_CALL_ID_JOB) &&
100 		(callee_id == MAYFLY_CALL_ID_WORKER)) ||
101 #endif
102 	       0;
103 }
104 
mayfly_pend(uint8_t caller_id,uint8_t callee_id)105 void mayfly_pend(uint8_t caller_id, uint8_t callee_id)
106 {
107 	(void)caller_id;
108 
109 	switch (callee_id) {
110 	case MAYFLY_CALL_ID_LLL:
111 		hal_swi_lll_pend();
112 		break;
113 
114 	case MAYFLY_CALL_ID_WORKER:
115 		hal_swi_worker_pend();
116 		break;
117 
118 	case MAYFLY_CALL_ID_JOB:
119 		hal_swi_job_pend();
120 		break;
121 
122 	default:
123 		LL_ASSERT(0);
124 		break;
125 	}
126 }
127 
mayfly_is_running(void)128 uint32_t mayfly_is_running(void)
129 {
130 	return k_is_in_isr();
131 }
132