1 // Copyright (C) 2022 Beken Corporation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #pragma once
16
17 #include <soc/soc.h>
18 #include "mailbox_hw.h"
19 #include "hal_port.h"
20 #include "icu_driver.h"
21
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25
26 #define MAILBOX_LL_REG_BASE(id) (MAILBOX_BASE + 0x8000 * (id))
27
28 typedef struct {
29 s_mailbox_t *hw;
30 mailbox_id_t id;
31 } mailbox_hal_t;
32
33 typedef enum {
34 MAILBOX_BOX0 = 0,
35 MAILBOX_BOX1 = 1,
36 MAILBOX_NONE,
37 } mailbox_box_num_t;
38
mailbox_ll_addr_init(mailbox_hal_t * hal)39 static inline void mailbox_ll_addr_init(mailbox_hal_t *hal)
40 {
41 hal->hw = (s_mailbox_t *)MAILBOX_LL_REG_BASE(hal->id);
42 }
43
mailbox_ll_set_identity(s_mailbox_t * hw,uint32_t id)44 static inline void mailbox_ll_set_identity(s_mailbox_t *hw, uint32_t id)
45 {
46 switch (id) {
47 case 0:
48 {
49 hw->sender.cpu0_sender = 1;
50 hw->receiver.cpu0_receiver = 0;
51 hw->sender.cpu1_sender = 0;
52 hw->receiver.cpu1_receiver = 1;
53 break;
54 }
55 case 1:
56 {
57 hw->sender.cpu1_sender = 1;
58 hw->receiver.cpu1_receiver = 0;
59 hw->sender.cpu0_sender = 0;
60 hw->receiver.cpu0_receiver = 1;
61 break;
62 }
63 default:
64 break;
65 }
66 }
67
mailbox_ll_get_sender(s_mailbox_t * hw,uint32_t id)68 static inline uint32_t mailbox_ll_get_sender(s_mailbox_t *hw, uint32_t id)
69 {
70 switch (id) {
71 case 0:
72 return hw->sender.cpu0_sender;
73 break;
74 case 1:
75 return hw->sender.cpu1_sender;
76 break;
77 default:
78 break;
79 }
80 return BK_FAIL;
81 }
82
mailbox_ll_get_receiver(s_mailbox_t * hw,uint32_t id)83 static inline uint32_t mailbox_ll_get_receiver(s_mailbox_t *hw, uint32_t id)
84 {
85 switch (id) {
86 case 0:
87 return hw->receiver.cpu0_receiver;
88 break;
89 case 1:
90 return hw->receiver.cpu1_receiver;
91 break;
92 default:
93 break;
94 }
95 return BK_FAIL;
96 }
97
mailbox_ll_box0_init(s_mailbox_t * hw)98 static inline void mailbox_ll_box0_init(s_mailbox_t *hw)
99 {
100 hw->box0_param0 = 0;
101 hw->box0_param1 = 0;
102 hw->box0_param2 = 0;
103 hw->box0_param3 = 0;
104 }
105
mailbox_ll_box1_init(s_mailbox_t * hw)106 static inline void mailbox_ll_box1_init(s_mailbox_t *hw)
107 {
108 hw->box1_param0 = 0;
109 hw->box1_param1 = 0;
110 hw->box1_param2 = 0;
111 hw->box1_param3 = 0;
112
113 }
114
mailbox_ll_write_param0(s_mailbox_t * hw,uint32_t param0,uint32_t box)115 static inline void mailbox_ll_write_param0(s_mailbox_t *hw, uint32_t param0, uint32_t box)
116 {
117 if (box == 0)
118 hw->box0_param0 = param0;
119 else
120 hw->box1_param0 = param0;
121 }
mailbox_ll_write_param1(s_mailbox_t * hw,uint32_t param1,uint32_t box)122 static inline void mailbox_ll_write_param1(s_mailbox_t *hw, uint32_t param1, uint32_t box)
123 {
124 if (box == 0)
125 hw->box0_param1 = param1;
126 else
127 hw->box1_param1 = param1;
128 }
mailbox_ll_write_param2(s_mailbox_t * hw,uint32_t param2,uint32_t box)129 static inline void mailbox_ll_write_param2(s_mailbox_t *hw, uint32_t param2, uint32_t box)
130 {
131 if (box == 0)
132 hw->box0_param2 = param2;
133 else
134 hw->box1_param2 = param2;
135 }
mailbox_ll_write_param3(s_mailbox_t * hw,uint32_t param3,uint32_t box)136 static inline void mailbox_ll_write_param3(s_mailbox_t *hw, uint32_t param3, uint32_t box)
137 {
138 if (box == 0)
139 hw->box0_param3 = param3;
140 else
141 hw->box1_param3 = param3;
142 }
143
mailbox_ll_read_param0(s_mailbox_t * hw,uint32_t box)144 static inline uint32_t mailbox_ll_read_param0(s_mailbox_t *hw, uint32_t box)
145 {
146 if (box == 0)
147 return hw->box0_param0;
148 else
149 return hw->box1_param0;
150 }
mailbox_ll_read_param1(s_mailbox_t * hw,uint32_t box)151 static inline uint32_t mailbox_ll_read_param1(s_mailbox_t *hw, uint32_t box)
152 {
153 if (box == 0)
154 return hw->box0_param1;
155 else
156 return hw->box1_param1;
157 }
mailbox_ll_read_param2(s_mailbox_t * hw,uint32_t box)158 static inline uint32_t mailbox_ll_read_param2(s_mailbox_t *hw, uint32_t box)
159 {
160 if (box == 0)
161 return hw->box0_param2;
162 else
163 return hw->box1_param2;
164
165 }
mailbox_ll_read_param3(s_mailbox_t * hw,uint32_t box)166 static inline uint32_t mailbox_ll_read_param3(s_mailbox_t *hw, uint32_t box)
167 {
168 if (box == 0)
169 return hw->box0_param3;
170 else
171 return hw->box1_param3;
172
173 }
174
mailbox_ll_box_trigger_ready(s_mailbox_t * hw,uint32_t box)175 static inline void mailbox_ll_box_trigger_ready(s_mailbox_t *hw, uint32_t box)
176 {
177 if (box == 0)
178 hw->box_ready.box0_ready = 0;
179 else
180 hw->box_ready.box1_ready = 0;
181 }
182
mailbox_ll_box_trigger(s_mailbox_t * hw,uint32_t box)183 static inline void mailbox_ll_box_trigger(s_mailbox_t *hw, uint32_t box)
184 {
185 if (box == 0)
186 hw->box_ready.box0_ready = 1;
187 else
188 hw->box_ready.box1_ready = 1;
189 }
190
mailbox_ll_read_box_ready(s_mailbox_t * hw,uint32_t box)191 static inline uint32_t mailbox_ll_read_box_ready(s_mailbox_t *hw, uint32_t box)
192 {
193 if (box == 0)
194 return hw->box_ready.box0_ready;
195 else
196 return hw->box_ready.box1_ready;
197 }
198
mailbox_ll_box_clear(s_mailbox_t * hw,uint32_t box)199 static inline void mailbox_ll_box_clear(s_mailbox_t *hw, uint32_t box)
200 {
201 if (box == 0)
202 hw->box_clear.box0_clear = 1;
203 else
204 hw->box_clear.box1_clear = 1;
205 }
206
mailbox_ll_box_clear_ready(s_mailbox_t * hw,uint32_t box)207 static inline void mailbox_ll_box_clear_ready(s_mailbox_t *hw, uint32_t box)
208 {
209 if (box == 0)
210 hw->box_clear.box0_clear = 0;
211 else
212 hw->box_clear.box1_clear = 0;
213 }
214
mailbox_ll_read_box_erasure(s_mailbox_t * hw,uint32_t box)215 static inline uint32_t mailbox_ll_read_box_erasure(s_mailbox_t *hw, uint32_t box)
216 {
217 if (box == 0)
218 return hw->box_clear.box0_clear;
219 else
220 return hw->box_clear.box1_clear;
221
222 }
223
mailbox_ll_box_init(s_mailbox_t * hw)224 static inline void mailbox_ll_box_init(s_mailbox_t *hw)
225 {
226 mailbox_ll_box0_init(hw);
227 mailbox_ll_box1_init(hw);
228 mailbox_ll_box_trigger_ready(hw, MAILBOX_BOX0);
229 mailbox_ll_box_trigger_ready(hw, MAILBOX_BOX1);
230 mailbox_ll_box_clear_ready(hw, MAILBOX_BOX0);
231 mailbox_ll_box_clear_ready(hw, MAILBOX_BOX1);
232 }
233
234 #ifdef __cplusplus
235 }
236 #endif
237