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