xref: /OK3568_Linux_fs/kernel/drivers/net/wireless/rockchip_wlan/rtl8723ds/hal/btc/halbtc8723d1ant.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /******************************************************************************
3  *
4  * Copyright(c) 2016 - 2017 Realtek Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of version 2 of the GNU General Public License as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  *****************************************************************************/
16 
17 /* ************************************************************
18  * Description:
19  *
20  * This file is for RTL8723D Co-exist mechanism
21  *
22  * History
23  * 2012/11/15 Cosa first check in.
24  *
25  * ************************************************************ */
26 
27 /* ************************************************************
28  * include files
29  * ************************************************************ */
30 #include "mp_precomp.h"
31 
32 #if (BT_SUPPORT == 1 && COEX_SUPPORT == 1)
33 
34 #if (RTL8723D_SUPPORT == 1)
35 /* ************************************************************
36  * Global variables, these are static variables
37  * ************************************************************ */
38 static u8	*trace_buf = &gl_btc_trace_buf[0];
39 static struct  coex_dm_8723d_1ant		glcoex_dm_8723d_1ant;
40 static struct  coex_dm_8723d_1ant	*coex_dm = &glcoex_dm_8723d_1ant;
41 static struct  coex_sta_8723d_1ant		glcoex_sta_8723d_1ant;
42 static struct  coex_sta_8723d_1ant	*coex_sta = &glcoex_sta_8723d_1ant;
43 static struct  psdscan_sta_8723d_1ant	gl_psd_scan_8723d_1ant;
44 static struct  psdscan_sta_8723d_1ant *psd_scan = &gl_psd_scan_8723d_1ant;
45 
46 
47 const char *const glbt_info_src_8723d_1ant[] = {
48 	"BT Info[wifi fw]",
49 	"BT Info[bt rsp]",
50 	"BT Info[bt auto report]",
51 };
52 
53 u32	glcoex_ver_date_8723d_1ant = 20200103;
54 u32	glcoex_ver_8723d_1ant = 0x35;
55 u32	glcoex_ver_btdesired_8723d_1ant = 0x35;
56 
57 #if 0
58 static
59 void halbtc8723d1ant_update_ra_mask(IN struct btc_coexist *btcoexist,
60 				    IN boolean force_exec, IN u32 dis_rate_mask)
61 {
62 	coex_dm->cur_ra_mask = dis_rate_mask;
63 
64 	if (force_exec || (coex_dm->pre_ra_mask != coex_dm->cur_ra_mask))
65 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_UPDATE_RAMASK,
66 				   &coex_dm->cur_ra_mask);
67 	coex_dm->pre_ra_mask = coex_dm->cur_ra_mask;
68 }
69 
70 static
71 void halbtc8723d1ant_auto_rate_fallback_retry(IN struct btc_coexist *btcoexist,
72 		IN boolean force_exec, IN u8 type)
73 {
74 	boolean	wifi_under_b_mode = FALSE;
75 
76 	coex_dm->cur_arfr_type = type;
77 
78 	if (force_exec || (coex_dm->pre_arfr_type != coex_dm->cur_arfr_type)) {
79 		switch (coex_dm->cur_arfr_type) {
80 		case 0:	/* normal mode */
81 			btcoexist->btc_write_4byte(btcoexist, 0x430,
82 						   coex_dm->backup_arfr_cnt1);
83 			btcoexist->btc_write_4byte(btcoexist, 0x434,
84 						   coex_dm->backup_arfr_cnt2);
85 			break;
86 		case 1:
87 			btcoexist->btc_get(btcoexist,
88 					   BTC_GET_BL_WIFI_UNDER_B_MODE,
89 					   &wifi_under_b_mode);
90 			if (wifi_under_b_mode) {
91 				btcoexist->btc_write_4byte(btcoexist,
92 							   0x430, 0x0);
93 				btcoexist->btc_write_4byte(btcoexist,
94 							   0x434, 0x01010101);
95 			} else {
96 				btcoexist->btc_write_4byte(btcoexist,
97 							   0x430, 0x0);
98 				btcoexist->btc_write_4byte(btcoexist,
99 							   0x434, 0x04030201);
100 			}
101 			break;
102 		default:
103 			break;
104 		}
105 	}
106 
107 	coex_dm->pre_arfr_type = coex_dm->cur_arfr_type;
108 }
109 
110 static
111 void halbtc8723d1ant_retry_limit(IN struct btc_coexist *btcoexist,
112 				 IN boolean force_exec, IN u8 type)
113 {
114 	coex_dm->cur_retry_limit_type = type;
115 
116 	if (force_exec ||
117 	    (coex_dm->pre_retry_limit_type !=
118 	     coex_dm->cur_retry_limit_type)) {
119 		switch (coex_dm->cur_retry_limit_type) {
120 		case 0:	/* normal mode */
121 			btcoexist->btc_write_2byte(btcoexist, 0x42a,
122 						   coex_dm->backup_retry_limit);
123 			break;
124 		case 1:	/* retry limit=8 */
125 			btcoexist->btc_write_2byte(btcoexist, 0x42a,
126 						   0x0808);
127 			break;
128 		default:
129 			break;
130 		}
131 	}
132 
133 	coex_dm->pre_retry_limit_type = coex_dm->cur_retry_limit_type;
134 }
135 
136 static
137 void halbtc8723d1ant_ampdu_max_time(IN struct btc_coexist *btcoexist,
138 				    IN boolean force_exec, IN u8 type)
139 {
140 	coex_dm->cur_ampdu_time_type = type;
141 
142 	if (force_exec ||
143 	    (coex_dm->pre_ampdu_time_type != coex_dm->cur_ampdu_time_type)) {
144 		switch (coex_dm->cur_ampdu_time_type) {
145 		case 0:	/* normal mode */
146 			btcoexist->btc_write_1byte(btcoexist, 0x455,
147 					   coex_dm->backup_ampdu_max_time);
148 			break;
149 		case 1:	/* AMPDU timw = 0x38 * 32us */
150 			btcoexist->btc_write_1byte(btcoexist, 0x455,
151 						   0x38);
152 			break;
153 		default:
154 			break;
155 		}
156 	}
157 
158 	coex_dm->pre_ampdu_time_type = coex_dm->cur_ampdu_time_type;
159 }
160 #endif
161 
162 static void
halbtc8723d1ant_limited_tx(struct btc_coexist * btcoexist,boolean force_exec,boolean tx_limit_en,boolean ampdu_limit_en)163 halbtc8723d1ant_limited_tx(struct btc_coexist *btcoexist, boolean force_exec,
164 			   boolean tx_limit_en,  boolean ampdu_limit_en)
165 {
166 	boolean wifi_under_b_mode = FALSE;
167 	u32	wifi_link_status = 0, num_of_wifi_link = 0;
168 
169 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
170 			   &wifi_link_status);
171 
172 	num_of_wifi_link = wifi_link_status >> 16;
173 
174 	/* Force Max Tx retry limit = 8*/
175 	if (!coex_sta->wl_tx_limit_en) {
176 		coex_sta->wl_0x430_backup =
177 			btcoexist->btc_read_4byte(btcoexist, 0x430);
178 		coex_sta->wl_0x434_backup =
179 			btcoexist->btc_read_4byte(btcoexist, 0x434);
180 		coex_sta->wl_0x42a_backup =
181 			btcoexist->btc_read_2byte(btcoexist, 0x42a);
182 	}
183 
184 	if (!coex_sta->wl_ampdu_limit_en)
185 		coex_sta->wl_0x456_backup = btcoexist->btc_read_1byte(btcoexist,
186 								      0x456);
187 
188 	if (!force_exec && tx_limit_en == coex_sta->wl_tx_limit_en &&
189 	    ampdu_limit_en == coex_sta->wl_ampdu_limit_en)
190 		return;
191 
192 	coex_sta->wl_tx_limit_en = tx_limit_en;
193 	coex_sta->wl_ampdu_limit_en = ampdu_limit_en;
194 
195 	if (tx_limit_en) {
196 		/* Set BT polluted packet on for Tx rate adaptive not
197 		 *including Tx retry break by PTA, 0x45c[19] =1
198 		 *
199 		 * Set queue life time to avoid can't reach tx retry limit
200 		 * if tx is always break by GNT_BT.
201 		 */
202 		if ((wifi_link_status & WIFI_STA_CONNECTED) &&
203 		     num_of_wifi_link == 1) {
204 			btcoexist->btc_write_1byte_bitmask(btcoexist, 0x45e, 0x8, 0x1);
205 			btcoexist->btc_write_1byte_bitmask(btcoexist, 0x426, 0xf, 0xf);
206 		}
207 
208 		/* Max Tx retry limit = 8*/
209 		btcoexist->btc_write_2byte(btcoexist, 0x42a, 0x0808);
210 
211 		/* AMPDU duration limit*/
212 		btcoexist->btc_write_1byte(btcoexist, 0x456, 0x20);
213 
214 		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_B_MODE,
215 				   &wifi_under_b_mode);
216 
217 		/* Auto rate fallback step within 8 retry*/
218 		if (wifi_under_b_mode) {
219 			btcoexist->btc_write_4byte(btcoexist, 0x430, 0x1000000);
220 			btcoexist->btc_write_4byte(btcoexist, 0x434, 0x1010101);
221 		} else {
222 			btcoexist->btc_write_4byte(btcoexist, 0x430, 0x1000000);
223 			btcoexist->btc_write_4byte(btcoexist, 0x434, 0x4030201);
224 		}
225 	} else {
226 		/* Set BT polluted packet on for Tx rate adaptive not
227 		 *including Tx retry break by PTA, 0x45c[19] =1
228 		 */
229 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x45e, 0x8, 0x0);
230 
231 		/* Set queue life time to avoid can't reach tx retry limit
232 		 * if tx is always break by GNT_BT.
233 		 */
234 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x426, 0xf, 0x0);
235 
236 		/* Recovery Max Tx retry limit*/
237 		btcoexist->btc_write_2byte(btcoexist, 0x42a,
238 					   coex_sta->wl_0x42a_backup);
239 		btcoexist->btc_write_4byte(btcoexist, 0x430,
240 					   coex_sta->wl_0x430_backup);
241 		btcoexist->btc_write_4byte(btcoexist, 0x434,
242 					   coex_sta->wl_0x434_backup);
243 	}
244 
245 	if (ampdu_limit_en)
246 		btcoexist->btc_write_1byte(btcoexist, 0x456, 0x20);
247 	else
248 		btcoexist->btc_write_1byte(btcoexist, 0x456,
249 					   coex_sta->wl_0x456_backup);
250 }
251 
252 static void
halbtc8723d1ant_limited_rx(struct btc_coexist * btcoexist,boolean force_exec,boolean rej_ap_agg_pkt,boolean bt_ctrl_agg_buf_size,u8 agg_buf_size)253 halbtc8723d1ant_limited_rx(struct btc_coexist *btcoexist, boolean force_exec,
254 			   boolean rej_ap_agg_pkt, boolean bt_ctrl_agg_buf_size,
255 			   u8 agg_buf_size)
256 {
257 #if 0
258 	boolean reject_rx_agg = rej_ap_agg_pkt;
259 	boolean bt_ctrl_rx_agg_size = bt_ctrl_agg_buf_size;
260 	u8 rx_agg_size = agg_buf_size;
261 
262 	if (!force_exec &&
263 	    bt_ctrl_agg_buf_size == coex_sta->wl_rxagg_limit_en &&
264 	    agg_buf_size == coex_sta->wl_rxagg_size)
265 		return;
266 
267 	coex_sta->wl_rxagg_limit_en = bt_ctrl_agg_buf_size;
268 	coex_sta->wl_rxagg_size = agg_buf_size;
269 
270 	/*btc->btc_set(btcoexist, BTC_SET_BL_TO_REJ_AP_AGG_PKT,
271 	 *&reject_rx_agg);
272 	 */
273 	/* decide BT control aggregation buf size or not */
274 	btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_CTRL_AGG_SIZE,
275 			   &bt_ctrl_rx_agg_size);
276 	/* aggregation buf size, only work
277 	 * when BT control Rx aggregation size
278 	 */
279 	btcoexist->btc_set(btcoexist, BTC_SET_U1_AGG_BUF_SIZE, &rx_agg_size);
280 	/* real update aggregation setting */
281 	btcoexist->btc_set(btcoexist, BTC_SET_ACT_AGGREGATE_CTRL, NULL);
282 #endif
283 }
284 static void
halbtc8723d1ant_set_tdma_timer_base(struct btc_coexist * btc,u8 type)285 halbtc8723d1ant_set_tdma_timer_base(struct btc_coexist *btc, u8 type)
286 {
287 	u16 tbtt_interval = 100;
288 	u8 h2c_para[2] = {0xb, 0x1};
289 
290 	btc->btc_get(btc, BTC_GET_U2_BEACON_PERIOD, &tbtt_interval);
291 
292 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
293 		"[BTCoex], tbtt_interval = %d\n", tbtt_interval);
294 	BTC_TRACE(trace_buf);
295 
296 	/* Add for JIRA coex-256 */
297 	if (type == 3 && tbtt_interval < 120) { /* 4-slot  */
298 		if (coex_sta->tdma_timer_base == 3)
299 			return;
300 
301 		h2c_para[1] = 0xc1; /* 4-slot */
302 		coex_sta->tdma_timer_base = 3;
303 	} else if (tbtt_interval < 80 && tbtt_interval > 0) {
304 		if (coex_sta->tdma_timer_base == 2)
305 			return;
306 		h2c_para[1] = (100 / tbtt_interval);
307 
308 		if (100 % tbtt_interval != 0)
309 			h2c_para[1] = h2c_para[1] + 1;
310 
311 		h2c_para[1] = h2c_para[1] & 0x3f;
312 		coex_sta->tdma_timer_base = 2;
313 	} else if (tbtt_interval >= 180) {
314 		if (coex_sta->tdma_timer_base == 1)
315 			return;
316 		h2c_para[1] = (tbtt_interval / 100);
317 
318 		if (tbtt_interval % 100 <= 80)
319 			h2c_para[1] = h2c_para[1] - 1;
320 
321 		h2c_para[1] = h2c_para[1] & 0x3f;
322 		h2c_para[1] = h2c_para[1] | 0x80;
323 		coex_sta->tdma_timer_base = 1;
324 	} else {
325 		if (coex_sta->tdma_timer_base == 0)
326 			return;
327 		h2c_para[1] = 0x1;
328 		coex_sta->tdma_timer_base = 0;
329 	}
330 
331 	btc->btc_fill_h2c(btc, 0x69, 2, h2c_para);
332 
333 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
334 		"[BTCoex], %s() h2c_0x69 = 0x%x\n", __func__, h2c_para[1]);
335 	BTC_TRACE(trace_buf);
336 
337 #if 0
338 	/* no 5ms_wl_slot_extend for 4-slot mode  */
339 	if (coex_sta->tdma_timer_base == 3)
340 		halbtc8822b2ant_ccklock_action(btc);
341 #endif
342 }
343 
344 static
halbtc8723d1ant_set_fw_low_penalty_ra(IN struct btc_coexist * btcoexist,IN boolean low_penalty_ra)345 void halbtc8723d1ant_set_fw_low_penalty_ra(IN struct btc_coexist *btcoexist,
346 					   IN boolean low_penalty_ra)
347 {
348 	u8  h2c_parameter[6] = {0};
349 
350 	h2c_parameter[0] = 0x6;	/* op_code, 0x6= Retry_Penalty */
351 
352 	if (low_penalty_ra) {
353 		h2c_parameter[1] |= BIT(0);
354 
355 		/* normal rate except MCS7/6/5, OFDM54/48/36 */
356 		h2c_parameter[2] = 0x00;
357 		h2c_parameter[3] = 0xf7;  /* MCS7 or OFDM54 */
358 		h2c_parameter[4] = 0xf8;  /* MCS6 or OFDM48 */
359 		h2c_parameter[5] = 0xf9;  /* MCS5 or OFDM36 */
360 	}
361 
362 	btcoexist->btc_fill_h2c(btcoexist, 0x69, 6, h2c_parameter);
363 }
364 
365 static
halbtc8723d1ant_low_penalty_ra(IN struct btc_coexist * btcoexist,IN boolean force_exec,IN boolean low_penalty_ra)366 void halbtc8723d1ant_low_penalty_ra(IN struct btc_coexist *btcoexist,
367 				    IN boolean force_exec,
368 				    IN boolean low_penalty_ra)
369 {
370 	coex_dm->cur_low_penalty_ra = low_penalty_ra;
371 
372 	if (!force_exec) {
373 		if (coex_dm->pre_low_penalty_ra == coex_dm->cur_low_penalty_ra)
374 			return;
375 	}
376 
377 	halbtc8723d1ant_set_fw_low_penalty_ra(btcoexist,
378 					      coex_dm->cur_low_penalty_ra);
379 
380 #if 0
381 	if (low_penalty_ra)
382 		btcoexist->btc_phydm_modify_RA_PCR_threshold(btcoexist, 0, 15);
383 	else
384 		btcoexist->btc_phydm_modify_RA_PCR_threshold(btcoexist, 0, 0);
385 #endif
386 	coex_dm->pre_low_penalty_ra = coex_dm->cur_low_penalty_ra;
387 }
388 
389 static
halbtc8723d1ant_query_bt_info(IN struct btc_coexist * btcoexist)390 void halbtc8723d1ant_query_bt_info(IN struct btc_coexist *btcoexist)
391 {
392 	u8			h2c_parameter[1] = {0};
393 
394 	h2c_parameter[0] |= BIT(0);	/* trigger */
395 
396 	btcoexist->btc_fill_h2c(btcoexist, 0x61, 1, h2c_parameter);
397 
398 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
399 		    "[BTCoex], WL query BT info!!\n");
400 	BTC_TRACE(trace_buf);
401 }
402 
403 static
halbtc8723d1ant_monitor_bt_ctr(IN struct btc_coexist * btcoexist)404 boolean halbtc8723d1ant_monitor_bt_ctr(IN struct btc_coexist *btcoexist)
405 {
406 	u32 reg_hp_txrx, reg_lp_txrx, u32tmp, cnt_bt_all;
407 	u32 reg_hp_tx = 0, reg_hp_rx = 0, reg_lp_tx = 0, reg_lp_rx = 0;
408 	static u8	num_of_bt_counter_chk = 0, cnt_overhead = 0,
409 		cnt_autoslot_hang = 0;
410 	struct	btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
411 	static u32 cnt_bt_pre = 0;
412 	boolean is_run_coex = FALSE;
413 
414 	/* to avoid 0x76e[3] = 1 (WLAN_Act control by PTA) during IPS */
415 	/* if (! (btcoexist->btc_read_1byte(btcoexist, 0x76e) & 0x8) ) */
416 
417 	reg_hp_txrx = 0x770;
418 	reg_lp_txrx = 0x774;
419 
420 	u32tmp = btcoexist->btc_read_4byte(btcoexist, reg_hp_txrx);
421 	reg_hp_tx = u32tmp & MASKLWORD;
422 	reg_hp_rx = (u32tmp & MASKHWORD) >> 16;
423 
424 	u32tmp = btcoexist->btc_read_4byte(btcoexist, reg_lp_txrx);
425 	reg_lp_tx = u32tmp & MASKLWORD;
426 	reg_lp_rx = (u32tmp & MASKHWORD) >> 16;
427 
428 	coex_sta->high_priority_tx = reg_hp_tx;
429 	coex_sta->high_priority_rx = reg_hp_rx;
430 	coex_sta->low_priority_tx = reg_lp_tx;
431 	coex_sta->low_priority_rx = reg_lp_rx;
432 
433 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
434 				"[BTCoex], Hi-Pri Rx/Tx: %d/%d, Lo-Pri Rx/Tx: %d/%d\n",
435 				reg_hp_rx, reg_hp_tx, reg_lp_rx, reg_lp_tx);
436 
437 	BTC_TRACE(trace_buf);
438 
439 	/* reset counter */
440 	btcoexist->btc_write_1byte(btcoexist, 0x76e, 0xc);
441 
442 	if (coex_sta->under_lps || coex_sta->under_ips ||
443 	    (coex_sta->high_priority_tx == 65535 &&
444 	     coex_sta->high_priority_rx == 65535 &&
445 	     coex_sta->low_priority_tx == 65535 &&
446 	     coex_sta->low_priority_rx == 65535))
447 		coex_sta->bt_ctr_ok = FALSE;
448 	else
449 		coex_sta->bt_ctr_ok = TRUE;
450 
451 	if (!coex_sta->bt_ctr_ok)
452 		return FALSE;
453 
454 	if (coex_dm->bt_status == BT_8723D_1ANT_BT_STATUS_NON_CONNECTED_IDLE) {
455 		if (coex_sta->high_priority_rx >= 15) {
456 			if (cnt_overhead < 3)
457 				cnt_overhead++;
458 
459 			if (cnt_overhead == 3)
460 				coex_sta->is_hipri_rx_overhead = TRUE;
461 		} else {
462 			if (cnt_overhead > 0)
463 				cnt_overhead--;
464 
465 			if (cnt_overhead == 0)
466 				coex_sta->is_hipri_rx_overhead = FALSE;
467 		}
468 	} else {
469 		coex_sta->is_hipri_rx_overhead = FALSE;
470 	}
471 
472 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
473 		    "[BTCoex], Hi-Pri Rx/Tx: %d/%d, Lo-Pri Rx/Tx: %d/%d\n",
474 		    reg_hp_rx, reg_hp_tx, reg_lp_rx, reg_lp_tx);
475 
476 	BTC_TRACE(trace_buf);
477 
478 	if (coex_sta->low_priority_tx > 1150 &&
479 	    !coex_sta->c2h_bt_inquiry_page)
480 		coex_sta->pop_event_cnt++;
481 
482 	if (coex_sta->is_tdma_btautoslot) {
483 		if (coex_sta->low_priority_tx >= 1300 &&
484 		    coex_sta->low_priority_rx <= 150) {
485 			if (cnt_autoslot_hang >= 2) {
486 				coex_sta->is_tdma_btautoslot_hang = TRUE;
487 				cnt_autoslot_hang = 2;
488 			} else
489 				cnt_autoslot_hang++;
490 		} else {
491 			if (cnt_autoslot_hang == 0)	{
492 				coex_sta->is_tdma_btautoslot_hang = FALSE;
493 				cnt_autoslot_hang = 0;
494 			} else
495 				cnt_autoslot_hang--;
496 		}
497 	}
498 
499 	if (bt_link_info->hid_only) {
500 		if (coex_sta->low_priority_tx > 50)
501 			coex_sta->is_hid_low_pri_tx_overhead = true;
502 		else
503 			coex_sta->is_hid_low_pri_tx_overhead = false;
504 	}
505 
506 	if (!coex_sta->bt_disabled) {
507 		if (coex_sta->high_priority_tx == 0 &&
508 		    coex_sta->high_priority_rx == 0 &&
509 		    coex_sta->low_priority_tx == 0 &&
510 		    coex_sta->low_priority_rx == 0) {
511 			num_of_bt_counter_chk++;
512 			if (num_of_bt_counter_chk >= 3) {
513 				halbtc8723d1ant_query_bt_info(btcoexist);
514 				num_of_bt_counter_chk = 0;
515 			}
516 		}
517 	}
518 
519 	cnt_bt_all = coex_sta->high_priority_tx +
520 		     coex_sta->high_priority_rx +
521 		     coex_sta->low_priority_tx +
522 		     coex_sta->low_priority_rx;
523 
524 	if ((cnt_bt_pre > (cnt_bt_all + 50) ||
525 	    cnt_bt_all > (cnt_bt_pre + 50)) &&
526 	    coex_dm->bt_status == BT_8723D_1ANT_BT_STATUS_NON_CONNECTED_IDLE)
527 		is_run_coex = TRUE;
528 
529 	cnt_bt_pre = cnt_bt_all;
530 
531 	return is_run_coex;
532 }
533 
534 static
halbtc8723d1ant_monitor_wifi_ctr(IN struct btc_coexist * btcoexist)535 void halbtc8723d1ant_monitor_wifi_ctr(IN struct btc_coexist *btcoexist)
536 {
537 	s32 wifi_rssi = 0;
538 	boolean wifi_busy = FALSE, wifi_under_b_mode = FALSE,
539 			wifi_scan = FALSE, wifi_connected = FALSE;
540 	boolean bt_idle = FALSE, wl_idle = FALSE, is_cck_deadlock = FALSE;
541 	static u8 cck_lock_counter = 0, wl_noisy_count0 = 0,
542 		  wl_noisy_count1 = 3, wl_noisy_count2 = 0;
543 	u32 total_cnt, reg_val1, reg_val2, cnt_cck;
544 	u32 cnt_crcok = 0, cnt_crcerr = 0;
545 	static u8 cnt = 0, cnt_ccklocking;
546 	u8	h2c_parameter[1] = {0};
547 	struct	btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
548 
549 	/*send h2c to query WL FW dbg info	*/
550 	if (coex_dm->cur_ps_tdma_on) {
551 		h2c_parameter[0] = 0x8;
552 		btcoexist->btc_fill_h2c(btcoexist, 0x69, 1, h2c_parameter);
553 	}
554 
555 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
556 	btcoexist->btc_get(btcoexist, BTC_GET_S4_WIFI_RSSI, &wifi_rssi);
557 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_B_MODE,
558 			   &wifi_under_b_mode);
559 
560 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &wifi_scan);
561 
562 	coex_sta->crc_ok_cck = btcoexist->btc_phydm_query_PHY_counter(
563 						btcoexist,
564 						PHYDM_INFO_CRC32_OK_CCK);
565 	coex_sta->crc_ok_11g = btcoexist->btc_phydm_query_PHY_counter(
566 						btcoexist,
567 						PHYDM_INFO_CRC32_OK_LEGACY);
568 	coex_sta->crc_ok_11n = btcoexist->btc_phydm_query_PHY_counter(
569 						btcoexist,
570 						PHYDM_INFO_CRC32_OK_HT);
571 	coex_sta->crc_ok_11n_vht = btcoexist->btc_phydm_query_PHY_counter(
572 						btcoexist,
573 						PHYDM_INFO_CRC32_OK_VHT);
574 
575 	coex_sta->crc_err_cck = btcoexist->btc_phydm_query_PHY_counter(
576 						btcoexist, PHYDM_INFO_CRC32_ERROR_CCK);
577 	coex_sta->crc_err_11g =  btcoexist->btc_phydm_query_PHY_counter(
578 						btcoexist, PHYDM_INFO_CRC32_ERROR_LEGACY);
579 	coex_sta->crc_err_11n = btcoexist->btc_phydm_query_PHY_counter(
580 						btcoexist, PHYDM_INFO_CRC32_ERROR_HT);
581 	coex_sta->crc_err_11n_vht = btcoexist->btc_phydm_query_PHY_counter(
582 						btcoexist,
583 						PHYDM_INFO_CRC32_ERROR_VHT);
584 
585 	cnt_crcok =  coex_sta->crc_ok_cck + coex_sta->crc_ok_11g
586 				+ coex_sta->crc_ok_11n
587 				+ coex_sta->crc_ok_11n_vht;
588 
589 	cnt_crcerr =  coex_sta->crc_err_cck + coex_sta->crc_err_11g
590 				+ coex_sta->crc_err_11n
591 				+ coex_sta->crc_err_11n_vht;
592 
593 	/*	CCK lock identification	*/
594 	if (coex_sta->cck_lock)
595 		cnt_ccklocking++;
596 	else if (cnt_ccklocking != 0)
597 		cnt_ccklocking--;
598 
599 	if (cnt_ccklocking >= 3) {
600 		cnt_ccklocking = 3;
601 		coex_sta->cck_lock_ever = TRUE;
602 	}
603 
604 	/* WiFi environment noisy identification */
605 	cnt_cck = coex_sta->crc_ok_cck + coex_sta->crc_err_cck;
606 
607 	if ((!wifi_busy) && (!coex_sta->cck_lock)) {
608 		if (cnt_cck > 250) {
609 			if (wl_noisy_count2 < 3)
610 				wl_noisy_count2++;
611 
612 			if (wl_noisy_count2 == 3) {
613 				wl_noisy_count0 = 0;
614 				wl_noisy_count1 = 0;
615 			}
616 
617 		} else if (cnt_cck < 50) {
618 			if (wl_noisy_count0 < 3)
619 				wl_noisy_count0++;
620 
621 			if (wl_noisy_count0 == 3) {
622 				wl_noisy_count1 = 0;
623 				wl_noisy_count2 = 0;
624 			}
625 
626 		} else {
627 			if (wl_noisy_count1 < 3)
628 				wl_noisy_count1++;
629 
630 			if (wl_noisy_count1 == 3) {
631 				wl_noisy_count0 = 0;
632 				wl_noisy_count2 = 0;
633 			}
634 	}
635 
636 		if (wl_noisy_count2 == 3)
637 			coex_sta->wl_noisy_level = 2;
638 		else if (wl_noisy_count1 == 3)
639 			coex_sta->wl_noisy_level = 1;
640 		else
641 			coex_sta->wl_noisy_level = 0;
642 	}
643 
644 }
645 
646 static
halbtc8723d1ant_update_bt_link_info(IN struct btc_coexist * btcoexist)647 void halbtc8723d1ant_update_bt_link_info(IN struct btc_coexist *btcoexist)
648 {
649 	struct btc_bt_link_info	*bt_link_info = &btcoexist->bt_link_info;
650 	boolean		bt_hs_on = FALSE, bt_busy = FALSE;
651 	u32		val = 0, wifi_link_status = 0, num_of_wifi_link = 0;
652 	static u8		pre_num_of_profile, cur_num_of_profile, cnt;
653 	static boolean	pre_ble_scan_en;
654 
655 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
656 			   &wifi_link_status);
657 
658 	num_of_wifi_link = wifi_link_status >> 16;
659 
660 	if (coex_sta->is_ble_scan_en && !pre_ble_scan_en) {
661 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
662 					"[BTCoex], BT ext info bit4 check, query BLE Scan type!!\n");
663 				BTC_TRACE(trace_buf);
664 		coex_sta->bt_ble_scan_type = btcoexist->btc_get_ble_scan_type_from_bt(btcoexist);
665 
666 		if ((coex_sta->bt_ble_scan_type & 0x1) == 0x1)
667 			coex_sta->bt_ble_scan_para[0]  =
668 				btcoexist->btc_get_ble_scan_para_from_bt(btcoexist, 0x1);
669 		if ((coex_sta->bt_ble_scan_type & 0x2) == 0x2)
670 			coex_sta->bt_ble_scan_para[1]  =
671 				btcoexist->btc_get_ble_scan_para_from_bt(btcoexist, 0x2);
672 		if ((coex_sta->bt_ble_scan_type & 0x4) == 0x4)
673 			coex_sta->bt_ble_scan_para[2]  =
674 				btcoexist->btc_get_ble_scan_para_from_bt(btcoexist, 0x4);
675 	}
676 
677 	pre_ble_scan_en = coex_sta->is_ble_scan_en;
678 	coex_sta->num_of_profile = 0;
679 
680 	/* set link exist status */
681 	if (!(coex_sta->bt_info & BT_INFO_8723D_1ANT_B_CONNECTION)) {
682 		coex_sta->bt_link_exist = FALSE;
683 		coex_sta->pan_exist = FALSE;
684 		coex_sta->a2dp_exist = FALSE;
685 		coex_sta->hid_exist = FALSE;
686 		coex_sta->sco_exist = FALSE;
687 	} else {	/* connection exists */
688 		coex_sta->bt_link_exist = TRUE;
689 		if (coex_sta->bt_info & BT_INFO_8723D_1ANT_B_FTP) {
690 			coex_sta->pan_exist = TRUE;
691 			coex_sta->num_of_profile++;
692 		} else
693 			coex_sta->pan_exist = FALSE;
694 
695 		if (coex_sta->bt_info & BT_INFO_8723D_1ANT_B_A2DP) {
696 			coex_sta->a2dp_exist = TRUE;
697 			coex_sta->num_of_profile++;
698 		} else
699 			coex_sta->a2dp_exist = FALSE;
700 
701 		if (coex_sta->bt_info & BT_INFO_8723D_1ANT_B_HID) {
702 			coex_sta->hid_exist = TRUE;
703 			coex_sta->num_of_profile++;
704 		} else
705 			coex_sta->hid_exist = FALSE;
706 
707 		if (coex_sta->bt_info & BT_INFO_8723D_1ANT_B_SCO_ESCO) {
708 			coex_sta->sco_exist = TRUE;
709 			coex_sta->num_of_profile++;
710 		} else
711 			coex_sta->sco_exist = FALSE;
712 
713 	}
714 
715 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
716 
717 	bt_link_info->bt_link_exist = coex_sta->bt_link_exist;
718 	bt_link_info->sco_exist = coex_sta->sco_exist;
719 	bt_link_info->a2dp_exist = coex_sta->a2dp_exist;
720 	bt_link_info->pan_exist = coex_sta->pan_exist;
721 	bt_link_info->hid_exist = coex_sta->hid_exist;
722 	bt_link_info->acl_busy = coex_sta->acl_busy;
723 
724 	/* work around for HS mode. */
725 	if (bt_hs_on) {
726 		bt_link_info->pan_exist = TRUE;
727 		bt_link_info->bt_link_exist = TRUE;
728 	}
729 
730 	/* check if Sco only */
731 	if (bt_link_info->sco_exist &&
732 	    !bt_link_info->a2dp_exist &&
733 	    !bt_link_info->pan_exist &&
734 	    !bt_link_info->hid_exist)
735 		bt_link_info->sco_only = TRUE;
736 	else
737 		bt_link_info->sco_only = FALSE;
738 
739 	/* check if A2dp only */
740 	if (!bt_link_info->sco_exist &&
741 	    bt_link_info->a2dp_exist &&
742 	    !bt_link_info->pan_exist &&
743 	    !bt_link_info->hid_exist)
744 		bt_link_info->a2dp_only = TRUE;
745 	else
746 		bt_link_info->a2dp_only = FALSE;
747 
748 	/* check if Pan only */
749 	if (!bt_link_info->sco_exist &&
750 	    !bt_link_info->a2dp_exist &&
751 	    bt_link_info->pan_exist &&
752 	    !bt_link_info->hid_exist)
753 		bt_link_info->pan_only = TRUE;
754 	else
755 		bt_link_info->pan_only = FALSE;
756 
757 	/* check if Hid only */
758 	if (!bt_link_info->sco_exist &&
759 	    !bt_link_info->a2dp_exist &&
760 	    !bt_link_info->pan_exist &&
761 	    bt_link_info->hid_exist)
762 		bt_link_info->hid_only = TRUE;
763 	else
764 		bt_link_info->hid_only = FALSE;
765 
766 	if (coex_sta->bt_info & BT_INFO_8723D_1ANT_B_INQ_PAGE) {
767 		coex_dm->bt_status = BT_8723D_1ANT_BT_STATUS_INQ_PAGE;
768 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
769 			"[BTCoex], BtInfoNotify(), BT Inq/page!!!\n");
770 	} else if (!(coex_sta->bt_info & BT_INFO_8723D_1ANT_B_CONNECTION)) {
771 		coex_dm->bt_status = BT_8723D_1ANT_BT_STATUS_NON_CONNECTED_IDLE;
772 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
773 			"[BTCoex], BtInfoNotify(), BT Non-Connected idle!!!\n");
774 	} else if (coex_sta->bt_info == BT_INFO_8723D_1ANT_B_CONNECTION) {
775 		/* connection exists but no busy */
776 		coex_dm->bt_status = BT_8723D_1ANT_BT_STATUS_CONNECTED_IDLE;
777 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
778 			    "[BTCoex], BtInfoNotify(), BT Connected-idle!!!\n");
779 	} else if (((coex_sta->bt_info & BT_INFO_8723D_1ANT_B_SCO_ESCO) ||
780 		    (coex_sta->bt_info & BT_INFO_8723D_1ANT_B_SCO_BUSY)) &&
781 		   (coex_sta->bt_info & BT_INFO_8723D_1ANT_B_ACL_BUSY)) {
782 		coex_dm->bt_status = BT_8723D_1ANT_BT_STATUS_ACL_SCO_BUSY;
783 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
784 			    "[BTCoex], BtInfoNotify(), BT ACL SCO busy!!!\n");
785 	} else if ((coex_sta->bt_info & BT_INFO_8723D_1ANT_B_SCO_ESCO) ||
786 		   (coex_sta->bt_info & BT_INFO_8723D_1ANT_B_SCO_BUSY)) {
787 		coex_dm->bt_status = BT_8723D_1ANT_BT_STATUS_SCO_BUSY;
788 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
789 			    "[BTCoex], BtInfoNotify(), BT SCO busy!!!\n");
790 	} else if (coex_sta->bt_info & BT_INFO_8723D_1ANT_B_ACL_BUSY) {
791 		coex_dm->bt_status = BT_8723D_1ANT_BT_STATUS_ACL_BUSY;
792 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
793 			    "[BTCoex], BtInfoNotify(), BT ACL busy!!!\n");
794 	} else {
795 		coex_dm->bt_status = BT_8723D_1ANT_BT_STATUS_MAX;
796 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
797 			"[BTCoex], BtInfoNotify(), BT Non-Defined state!!!\n");
798 	}
799 
800 	BTC_TRACE(trace_buf);
801 
802 	if ((BT_8723D_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) ||
803 	    (BT_8723D_1ANT_BT_STATUS_SCO_BUSY == coex_dm->bt_status) ||
804 	    (BT_8723D_1ANT_BT_STATUS_ACL_SCO_BUSY == coex_dm->bt_status))
805 		bt_busy = TRUE;
806 	else
807 		bt_busy = FALSE;
808 
809 	btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_TRAFFIC_BUSY, &bt_busy);
810 
811 	cur_num_of_profile = coex_sta->num_of_profile;
812 
813 	if (cur_num_of_profile != pre_num_of_profile)
814 		cnt = 2;
815 
816 	if (bt_link_info->a2dp_exist) {
817 
818 		if (((coex_sta->bt_a2dp_vendor_id == 0) &&
819 			(coex_sta->bt_a2dp_device_name == 0)) ||
820 			(cur_num_of_profile != pre_num_of_profile)) {
821 
822 			btcoexist->btc_get(btcoexist, BTC_GET_U4_BT_DEVICE_INFO, &val);
823 
824 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
825 				    "[BTCoex], BtInfoNotify(), get BT DEVICE_INFO = %x\n",
826 				    val);
827 			BTC_TRACE(trace_buf);
828 
829 			coex_sta->bt_a2dp_vendor_id = (u8)(val & 0xff);
830 			coex_sta->bt_a2dp_device_name = (val & 0xffffff00) >> 8;
831 		}
832 
833 		if (((coex_sta->legacy_forbidden_slot == 0) &&
834 			(coex_sta->le_forbidden_slot == 0)) ||
835 			(cur_num_of_profile != pre_num_of_profile) ||
836 			(cnt > 0)) {
837 
838 			if (cnt > 0)
839 				cnt--;
840 
841 			btcoexist->btc_get(btcoexist,
842 					   BTC_GET_U4_BT_FORBIDDEN_SLOT_VAL,
843 					   &val);
844 
845 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
846 				    "[BTCoex], BtInfoNotify(), get BT FORBIDDEN_SLOT_VAL = %x, cnt = %d\n",
847 				    val, cnt);
848 			BTC_TRACE(trace_buf);
849 
850 			coex_sta->legacy_forbidden_slot = (u16)(val & 0xffff);
851 			coex_sta->le_forbidden_slot = (u16)((val & 0xffff0000) >> 16);
852 		}
853 	}
854 
855 	pre_num_of_profile = coex_sta->num_of_profile;
856 
857 	if (btcoexist->manual_control || btcoexist->stop_coex_dm)
858 		return;
859 
860 	if (num_of_wifi_link == 0 ||
861 	    coex_dm->bt_status == BT_8723D_1ANT_BT_STATUS_NON_CONNECTED_IDLE) {
862 		halbtc8723d1ant_low_penalty_ra(btcoexist, NORMAL_EXEC, FALSE);
863 		halbtc8723d1ant_limited_tx(btcoexist, NORMAL_EXEC, FALSE,
864 					   FALSE);
865 		halbtc8723d1ant_limited_rx(btcoexist, NM_EXCU, FALSE, TRUE, 64);
866 	} else if (wifi_link_status & WIFI_P2P_GO_CONNECTED ||
867 		   wifi_link_status & WIFI_P2P_GC_CONNECTED) {
868 		halbtc8723d1ant_low_penalty_ra(btcoexist, NORMAL_EXEC, TRUE);
869 		halbtc8723d1ant_limited_tx(btcoexist, NM_EXCU, TRUE, TRUE);
870 		halbtc8723d1ant_limited_rx(btcoexist, NM_EXCU, FALSE, TRUE, 16);
871 	} else {
872 		halbtc8723d1ant_low_penalty_ra(btcoexist, NORMAL_EXEC, TRUE);
873 
874 		if (bt_link_info->hid_exist || coex_sta->hid_pair_cnt > 0 ||
875 		    bt_link_info->sco_exist) {
876 			halbtc8723d1ant_limited_tx(btcoexist, NM_EXCU, TRUE,
877 						   TRUE);
878 			halbtc8723d1ant_limited_rx(btcoexist, NM_EXCU, FALSE,
879 						   TRUE, 16);
880 		} else {
881 			halbtc8723d1ant_limited_tx(btcoexist, NM_EXCU, TRUE,
882 						   FALSE);
883 			halbtc8723d1ant_limited_rx(btcoexist, NM_EXCU, FALSE,
884 						   TRUE, 64);
885 		}
886 	}
887 }
888 
889 static
halbtc8723d1ant_update_wifi_channel_info(IN struct btc_coexist * btcoexist,IN u8 type)890 void halbtc8723d1ant_update_wifi_channel_info(IN struct btc_coexist *btcoexist,
891 		IN u8 type)
892 {
893 	u8			h2c_parameter[3] = {0};
894 	u32			wifi_bw;
895 	u8			wifi_central_chnl;
896 
897 	/* only 2.4G we need to inform bt the chnl mask */
898 	btcoexist->btc_get(btcoexist, BTC_GET_U1_WIFI_CENTRAL_CHNL,
899 			   &wifi_central_chnl);
900 	if ((BTC_MEDIA_CONNECT == type) &&
901 	    (wifi_central_chnl <= 14)) {
902 		h2c_parameter[0] =
903 			0x1;  /* enable BT AFH skip WL channel for 8723d because BT Rx LO interference */
904 		/* h2c_parameter[0] = 0x0; */
905 		h2c_parameter[1] = wifi_central_chnl;
906 		btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
907 		if (BTC_WIFI_BW_HT40 == wifi_bw)
908 			h2c_parameter[2] = 0x30;
909 		else
910 			h2c_parameter[2] = 0x20;
911 	}
912 
913 	coex_dm->wifi_chnl_info[0] = h2c_parameter[0];
914 	coex_dm->wifi_chnl_info[1] = h2c_parameter[1];
915 	coex_dm->wifi_chnl_info[2] = h2c_parameter[2];
916 
917 	btcoexist->btc_fill_h2c(btcoexist, 0x66, 3, h2c_parameter);
918 
919 }
920 
921 static
halbtc8723d1ant_action_algorithm(IN struct btc_coexist * btcoexist)922 u8 halbtc8723d1ant_action_algorithm(IN struct btc_coexist *btcoexist)
923 {
924 	struct  btc_bt_link_info	*bt_link_info = &btcoexist->bt_link_info;
925 	boolean				bt_hs_on = FALSE;
926 	u8				algorithm = BT_8723D_1ANT_COEX_ALGO_UNDEFINED;
927 	u8				num_of_diff_profile = 0;
928 
929 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
930 
931 	if (!bt_link_info->bt_link_exist) {
932 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
933 			    "[BTCoex], No BT link exists!!!\n");
934 		BTC_TRACE(trace_buf);
935 		return algorithm;
936 	}
937 
938 	if (bt_link_info->sco_exist)
939 		num_of_diff_profile++;
940 	if (bt_link_info->hid_exist)
941 		num_of_diff_profile++;
942 	if (bt_link_info->pan_exist)
943 		num_of_diff_profile++;
944 	if (bt_link_info->a2dp_exist)
945 		num_of_diff_profile++;
946 
947 	if (num_of_diff_profile == 1) {
948 		if (bt_link_info->sco_exist) {
949 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
950 				    "[BTCoex], BT Profile = SCO only\n");
951 			BTC_TRACE(trace_buf);
952 			algorithm = BT_8723D_1ANT_COEX_ALGO_SCO;
953 		} else {
954 			if (bt_link_info->hid_exist) {
955 				BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
956 					"[BTCoex], BT Profile = HID only\n");
957 				BTC_TRACE(trace_buf);
958 				algorithm = BT_8723D_1ANT_COEX_ALGO_HID;
959 			} else if (bt_link_info->a2dp_exist) {
960 				BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
961 					"[BTCoex], BT Profile = A2DP only\n");
962 				BTC_TRACE(trace_buf);
963 				algorithm = BT_8723D_1ANT_COEX_ALGO_A2DP;
964 			} else if (bt_link_info->pan_exist) {
965 				if (bt_hs_on) {
966 					BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
967 						"[BTCoex], BT Profile = PAN(HS) only\n");
968 					BTC_TRACE(trace_buf);
969 					algorithm =
970 						BT_8723D_1ANT_COEX_ALGO_PANHS;
971 				} else {
972 					BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
973 						"[BTCoex], BT Profile = PAN(EDR) only\n");
974 					BTC_TRACE(trace_buf);
975 					algorithm =
976 						BT_8723D_1ANT_COEX_ALGO_PANEDR;
977 				}
978 			}
979 		}
980 	} else if (num_of_diff_profile == 2) {
981 		if (bt_link_info->sco_exist) {
982 			if (bt_link_info->hid_exist) {
983 				BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
984 					"[BTCoex], BT Profile = SCO + HID\n");
985 				BTC_TRACE(trace_buf);
986 				algorithm = BT_8723D_1ANT_COEX_ALGO_HID;
987 			} else if (bt_link_info->a2dp_exist) {
988 				BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
989 					"[BTCoex], BT Profile = SCO + A2DP ==> SCO\n");
990 				BTC_TRACE(trace_buf);
991 				algorithm = BT_8723D_1ANT_COEX_ALGO_SCO;
992 			} else if (bt_link_info->pan_exist) {
993 				if (bt_hs_on) {
994 					BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
995 						"[BTCoex], BT Profile = SCO + PAN(HS)\n");
996 					BTC_TRACE(trace_buf);
997 					algorithm = BT_8723D_1ANT_COEX_ALGO_SCO;
998 				} else {
999 					BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1000 						"[BTCoex], BT Profile = SCO + PAN(EDR)\n");
1001 					BTC_TRACE(trace_buf);
1002 					algorithm =
1003 						BT_8723D_1ANT_COEX_ALGO_PANEDR_HID;
1004 				}
1005 			}
1006 		} else {
1007 			if (bt_link_info->hid_exist &&
1008 			    bt_link_info->a2dp_exist) {
1009 				BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1010 					"[BTCoex], BT Profile = HID + A2DP\n");
1011 				BTC_TRACE(trace_buf);
1012 				algorithm = BT_8723D_1ANT_COEX_ALGO_HID_A2DP;
1013 			} else if (bt_link_info->hid_exist &&
1014 				   bt_link_info->pan_exist) {
1015 				if (bt_hs_on) {
1016 					BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1017 						"[BTCoex], BT Profile = HID + PAN(HS)\n");
1018 					BTC_TRACE(trace_buf);
1019 					algorithm =
1020 						BT_8723D_1ANT_COEX_ALGO_HID_A2DP;
1021 				} else {
1022 					BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1023 						"[BTCoex], BT Profile = HID + PAN(EDR)\n");
1024 					BTC_TRACE(trace_buf);
1025 					algorithm =
1026 						BT_8723D_1ANT_COEX_ALGO_PANEDR_HID;
1027 				}
1028 			} else if (bt_link_info->pan_exist &&
1029 				   bt_link_info->a2dp_exist) {
1030 				if (bt_hs_on) {
1031 					BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1032 						"[BTCoex], BT Profile = A2DP + PAN(HS)\n");
1033 					BTC_TRACE(trace_buf);
1034 					algorithm =
1035 						BT_8723D_1ANT_COEX_ALGO_A2DP_PANHS;
1036 				} else {
1037 					BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1038 						"[BTCoex], BT Profile = A2DP + PAN(EDR)\n");
1039 					BTC_TRACE(trace_buf);
1040 					algorithm =
1041 						BT_8723D_1ANT_COEX_ALGO_PANEDR_A2DP;
1042 				}
1043 			}
1044 		}
1045 	} else if (num_of_diff_profile == 3) {
1046 		if (bt_link_info->sco_exist) {
1047 			if (bt_link_info->hid_exist &&
1048 			    bt_link_info->a2dp_exist) {
1049 				BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1050 					"[BTCoex], BT Profile = SCO + HID + A2DP ==> HID\n");
1051 				BTC_TRACE(trace_buf);
1052 				algorithm = BT_8723D_1ANT_COEX_ALGO_HID;
1053 			} else if (bt_link_info->hid_exist &&
1054 				   bt_link_info->pan_exist) {
1055 				if (bt_hs_on) {
1056 					BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1057 						"[BTCoex], BT Profile = SCO + HID + PAN(HS)\n");
1058 					BTC_TRACE(trace_buf);
1059 					algorithm =
1060 						BT_8723D_1ANT_COEX_ALGO_HID_A2DP;
1061 				} else {
1062 					BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1063 						"[BTCoex], BT Profile = SCO + HID + PAN(EDR)\n");
1064 					BTC_TRACE(trace_buf);
1065 					algorithm =
1066 						BT_8723D_1ANT_COEX_ALGO_PANEDR_HID;
1067 				}
1068 			} else if (bt_link_info->pan_exist &&
1069 				   bt_link_info->a2dp_exist) {
1070 				if (bt_hs_on) {
1071 					BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1072 						"[BTCoex], BT Profile = SCO + A2DP + PAN(HS)\n");
1073 					BTC_TRACE(trace_buf);
1074 					algorithm = BT_8723D_1ANT_COEX_ALGO_SCO;
1075 				} else {
1076 					BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1077 						"[BTCoex], BT Profile = SCO + A2DP + PAN(EDR) ==> HID\n");
1078 					BTC_TRACE(trace_buf);
1079 					algorithm =
1080 						BT_8723D_1ANT_COEX_ALGO_PANEDR_HID;
1081 				}
1082 			}
1083 		} else {
1084 			if (bt_link_info->hid_exist &&
1085 			    bt_link_info->pan_exist &&
1086 			    bt_link_info->a2dp_exist) {
1087 				if (bt_hs_on) {
1088 					BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1089 						"[BTCoex], BT Profile = HID + A2DP + PAN(HS)\n");
1090 					BTC_TRACE(trace_buf);
1091 					algorithm =
1092 						BT_8723D_1ANT_COEX_ALGO_HID_A2DP;
1093 				} else {
1094 					BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1095 						"[BTCoex], BT Profile = HID + A2DP + PAN(EDR)\n");
1096 					BTC_TRACE(trace_buf);
1097 					algorithm =
1098 						BT_8723D_1ANT_COEX_ALGO_HID_A2DP_PANEDR;
1099 				}
1100 			}
1101 		}
1102 	} else if (num_of_diff_profile >= 3) {
1103 		if (bt_link_info->sco_exist) {
1104 			if (bt_link_info->hid_exist &&
1105 			    bt_link_info->pan_exist &&
1106 			    bt_link_info->a2dp_exist) {
1107 				if (bt_hs_on) {
1108 					BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1109 						"[BTCoex], Error!!! BT Profile = SCO + HID + A2DP + PAN(HS)\n");
1110 					BTC_TRACE(trace_buf);
1111 
1112 				} else {
1113 					BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1114 						"[BTCoex], BT Profile = SCO + HID + A2DP + PAN(EDR)==>PAN(EDR)+HID\n");
1115 					BTC_TRACE(trace_buf);
1116 					algorithm =
1117 						BT_8723D_1ANT_COEX_ALGO_PANEDR_HID;
1118 				}
1119 			}
1120 		}
1121 	}
1122 
1123 	return algorithm;
1124 }
1125 
1126 static
halbtc8723d1ant_write_score_board(IN struct btc_coexist * btcoexist,IN u16 bitpos,IN boolean state)1127 void halbtc8723d1ant_write_score_board(
1128 	IN	struct  btc_coexist		*btcoexist,
1129 	IN	u16				bitpos,
1130 	IN	boolean		state
1131 )
1132 {
1133 
1134 	static u16 originalval = 0x8002, preval = 0x0;
1135 
1136 	if (state)
1137 		originalval = originalval | bitpos;
1138 	else
1139 		originalval = originalval & (~bitpos);
1140 
1141 	coex_sta->score_board_WB = originalval;
1142 
1143 	if (originalval != preval) {
1144 
1145 		preval = originalval;
1146 	btcoexist->btc_write_2byte(btcoexist, 0xaa, originalval);
1147 	} else {
1148 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1149 		"[BTCoex], halbtc8723d1ant_write_score_board: return for nochange\n");
1150 		BTC_TRACE(trace_buf);
1151 	}
1152 }
1153 
1154 static
halbtc8723d1ant_read_score_board(IN struct btc_coexist * btcoexist,IN u16 * score_board_val)1155 void halbtc8723d1ant_read_score_board(
1156 	IN	struct  btc_coexist		*btcoexist,
1157 	IN   u16				*score_board_val
1158 )
1159 {
1160 
1161 	*score_board_val = (btcoexist->btc_read_2byte(btcoexist,
1162 			    0xaa)) & 0x7fff;
1163 }
1164 
1165 static
halbtc8723d1ant_post_state_to_bt(IN struct btc_coexist * btcoexist,IN u16 type,IN boolean state)1166 void halbtc8723d1ant_post_state_to_bt(
1167 	IN	struct  btc_coexist		*btcoexist,
1168 	IN	u16						type,
1169 	IN  boolean                 state
1170 )
1171 {
1172 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1173 		"[BTCoex], halbtc8723d1ant_post_state_to_bt: type = %d, state =%d\n",
1174 		type, state);
1175 	BTC_TRACE(trace_buf);
1176 
1177 	halbtc8723d1ant_write_score_board(btcoexist, (u16) type, state);
1178 }
1179 
1180 static
halbtc8723d1ant_is_wifibt_status_changed(IN struct btc_coexist * btcoexist)1181 boolean halbtc8723d1ant_is_wifibt_status_changed(IN struct btc_coexist
1182 		*btcoexist)
1183 {
1184 	static boolean	pre_wifi_busy = FALSE, pre_under_4way = FALSE,
1185 			pre_bt_hs_on = FALSE, pre_bt_off = FALSE,
1186 			pre_bt_slave = FALSE, pre_hid_low_pri_tx_overhead = FALSE,
1187 			pre_wifi_under_lps = FALSE, pre_bt_setup_link = FALSE,
1188 			pre_cck_lock = FALSE, pre_cck_lock_warn = FALSE;
1189 	static u8 pre_hid_busy_num = 0, pre_wl_noisy_level = 0;
1190 	boolean wifi_busy = FALSE, under_4way = FALSE, bt_hs_on = FALSE;
1191 	boolean wifi_connected = FALSE;
1192 	struct  btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1193 
1194 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
1195 			   &wifi_connected);
1196 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
1197 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
1198 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_4_WAY_PROGRESS,
1199 			   &under_4way);
1200 
1201 	if (coex_sta->bt_disabled != pre_bt_off) {
1202 		pre_bt_off = coex_sta->bt_disabled;
1203 
1204 		if (coex_sta->bt_disabled)
1205 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1206 				    "[BTCoex], BT is disabled !!\n");
1207 		else
1208 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1209 				    "[BTCoex], BT is enabled !!\n");
1210 
1211 		BTC_TRACE(trace_buf);
1212 
1213 		coex_sta->bt_coex_supported_feature = 0;
1214 		coex_sta->bt_coex_supported_version = 0;
1215 		coex_sta->bt_ble_scan_type = 0;
1216 		coex_sta->bt_ble_scan_para[0] = 0;
1217 		coex_sta->bt_ble_scan_para[1] = 0;
1218 		coex_sta->bt_ble_scan_para[2] = 0;
1219 		coex_sta->bt_reg_vendor_ac = 0xffff;
1220 		coex_sta->bt_reg_vendor_ae = 0xffff;
1221 		coex_sta->legacy_forbidden_slot = 0;
1222 		coex_sta->le_forbidden_slot = 0;
1223 		coex_sta->bt_a2dp_vendor_id = 0;
1224 		coex_sta->bt_a2dp_device_name = 0;
1225 		return TRUE;
1226 	}
1227 
1228 	if (wifi_connected) {
1229 		if (wifi_busy != pre_wifi_busy) {
1230 			pre_wifi_busy = wifi_busy;
1231 
1232 			if (wifi_busy)
1233 				halbtc8723d1ant_post_state_to_bt(btcoexist,
1234 						BT_8723D_1ANT_SCOREBOARD_UNDERTEST, TRUE);
1235 			else
1236 				halbtc8723d1ant_post_state_to_bt(btcoexist,
1237 						BT_8723D_1ANT_SCOREBOARD_UNDERTEST, FALSE);
1238 			return TRUE;
1239 		}
1240 		if (under_4way != pre_under_4way) {
1241 			pre_under_4way = under_4way;
1242 			return TRUE;
1243 		}
1244 		if (bt_hs_on != pre_bt_hs_on) {
1245 			pre_bt_hs_on = bt_hs_on;
1246 			return TRUE;
1247 		}
1248 		if (coex_sta->wl_noisy_level != pre_wl_noisy_level) {
1249 			pre_wl_noisy_level = coex_sta->wl_noisy_level;
1250 			return TRUE;
1251 		}
1252 		if (coex_sta->under_lps != pre_wifi_under_lps) {
1253 			pre_wifi_under_lps = coex_sta->under_lps;
1254 			if (coex_sta->under_lps == TRUE)
1255 				return TRUE;
1256 		}
1257 		if (coex_sta->cck_lock != pre_cck_lock) {
1258 			pre_cck_lock = coex_sta->cck_lock;
1259 			return TRUE;
1260 		}
1261 		if (coex_sta->cck_lock_warn != pre_cck_lock_warn) {
1262 			pre_cck_lock_warn = coex_sta->cck_lock_warn;
1263 			return TRUE;
1264 		}
1265 	}
1266 
1267 	if (!coex_sta->bt_disabled) {
1268 		if (coex_sta->hid_busy_num != pre_hid_busy_num) {
1269 			pre_hid_busy_num = coex_sta->hid_busy_num;
1270 			return TRUE;
1271 		}
1272 
1273 		if (bt_link_info->slave_role != pre_bt_slave) {
1274 			pre_bt_slave = bt_link_info->slave_role;
1275 			return TRUE;
1276 		}
1277 
1278 		if (pre_hid_low_pri_tx_overhead != coex_sta->is_hid_low_pri_tx_overhead) {
1279 			pre_hid_low_pri_tx_overhead = coex_sta->is_hid_low_pri_tx_overhead;
1280 			return TRUE;
1281 		}
1282 
1283 		if (pre_bt_setup_link != coex_sta->is_setup_link) {
1284 			pre_bt_setup_link = coex_sta->is_setup_link;
1285 			return TRUE;
1286 		}
1287 	}
1288 
1289 	return FALSE;
1290 }
1291 
1292 static
halbtc8723d1ant_monitor_bt_enable_disable(IN struct btc_coexist * btcoexist)1293 void halbtc8723d1ant_monitor_bt_enable_disable(IN struct btc_coexist *btcoexist)
1294 {
1295 	static u32		bt_disable_cnt = 0;
1296 	boolean		bt_active = TRUE, bt_disabled = FALSE;
1297 	u16		u16tmp;
1298 
1299 	/* This function check if bt is disabled
1300 	 * Read BT on/off status from scoreboard[1],
1301 	 *enable this only if BT patch support this feature
1302 	 */
1303 	halbtc8723d1ant_read_score_board(btcoexist, &u16tmp);
1304 	bt_active = u16tmp & BIT(1);
1305 
1306 	if (bt_active) {
1307 		bt_disable_cnt = 0;
1308 		bt_disabled = FALSE;
1309 		btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_DISABLE,
1310 				   &bt_disabled);
1311 	} else {
1312 
1313 		bt_disable_cnt++;
1314 		if (bt_disable_cnt >= 2) {
1315 			bt_disabled = TRUE;
1316 			bt_disable_cnt = 2;
1317 		}
1318 
1319 		btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_DISABLE,
1320 				   &bt_disabled);
1321 	}
1322 
1323 	if (coex_sta->bt_disabled != bt_disabled) {
1324 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1325 			    "[BTCoex], BT is from %s to %s!!\n",
1326 			    (coex_sta->bt_disabled ? "disabled" : "enabled"),
1327 			    (bt_disabled ? "disabled" : "enabled"));
1328 		BTC_TRACE(trace_buf);
1329 		coex_sta->bt_disabled = bt_disabled;
1330 	}
1331 
1332 }
1333 
1334 static
halbtc8723d1ant_enable_gnt_to_gpio(IN struct btc_coexist * btcoexist,boolean isenable)1335 void halbtc8723d1ant_enable_gnt_to_gpio(IN struct btc_coexist *btcoexist,
1336 					boolean isenable)
1337 {
1338 #if BT_8723D_1ANT_COEX_DBG
1339 	if (isenable) {
1340 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x73, 0x8, 0x1);
1341 
1342 		/* enable GNT_BT to GPIO debug */
1343 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x4e, 0x40, 0x0);
1344 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x67, 0x1, 0x0);
1345 
1346 		/* 0x48[20] = 0  for GPIO14 =  GNT_WL*/
1347 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x4a, 0x10, 0x0);
1348 		/* 0x40[17] = 0  for GPIO14 =  GNT_WL*/
1349 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x42, 0x02, 0x0);
1350 
1351 		/* 0x66[9] = 0   for GPIO15 =  GNT_B T*/
1352 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x67, 0x02, 0x0);
1353 		/* 0x66[7] = 0
1354 		for GPIO15 =  GNT_BT*/
1355 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x66, 0x80, 0x0);
1356 		/* 0x8[8] = 0    for GPIO15 =  GNT_BT*/
1357 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x9, 0x1, 0x0);
1358 
1359 		/* BT Vendor Reg 0x76[0] = 0  for GPIO15 =  GNT_BT, this is not set here*/
1360 	} else {
1361 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x73, 0x8, 0x0);
1362 
1363 		/* Disable GNT_BT debug to GPIO, and enable chip_wakeup_host */
1364 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x4e, 0x40, 0x1);
1365 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x67, 0x1, 0x1);
1366 
1367 		/* 0x48[20] = 0  for GPIO14 =  GNT_WL*/
1368 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x4a, 0x10, 0x1);
1369 	}
1370 
1371 #endif
1372 }
1373 
1374 static
halbtc8723d1ant_ltecoex_indirect_read_reg(IN struct btc_coexist * btcoexist,IN u16 reg_addr)1375 u32 halbtc8723d1ant_ltecoex_indirect_read_reg(IN struct btc_coexist *btcoexist,
1376 		IN u16 reg_addr)
1377 {
1378 	u32 j = 0, delay_count = 0;
1379 
1380 	while (1) {
1381 		if ((btcoexist->btc_read_1byte(btcoexist, 0x7c3)&BIT(5)) == 0) {
1382 			delay_ms(10);
1383 			delay_count++;
1384 			if (delay_count >= 10) {
1385 				delay_count = 0;
1386 				break;
1387 			}
1388 		} else
1389 			break;
1390 	}
1391 
1392 	/* wait for ready bit before access 0x7c0		 */
1393 	btcoexist->btc_write_4byte(btcoexist, 0x7c0, 0x800F0000 | reg_addr);
1394 
1395 	return btcoexist->btc_read_4byte(btcoexist,
1396 					 0x7c8);  /* get read data */
1397 
1398 }
1399 
1400 static
halbtc8723d1ant_ltecoex_indirect_write_reg(IN struct btc_coexist * btcoexist,IN u16 reg_addr,IN u32 bit_mask,IN u32 reg_value)1401 void halbtc8723d1ant_ltecoex_indirect_write_reg(IN struct btc_coexist
1402 		*btcoexist,
1403 		IN u16 reg_addr, IN u32 bit_mask, IN u32 reg_value)
1404 {
1405 	u32 val, i = 0, j = 0, bitpos = 0, delay_count = 0;
1406 
1407 
1408 	if (bit_mask == 0x0)
1409 		return;
1410 	if (bit_mask == 0xffffffff) {
1411 		/* wait for ready bit before access 0x7c0/0x7c4 */
1412 		while (1) {
1413 			if ((btcoexist->btc_read_1byte(btcoexist, 0x7c3)&BIT(5)) == 0) {
1414 				delay_ms(10);
1415 				delay_count++;
1416 				if (delay_count >= 10) {
1417 					delay_count = 0;
1418 					break;
1419 				}
1420 			} else
1421 				break;
1422 		}
1423 
1424 		btcoexist->btc_write_4byte(btcoexist, 0x7c4,
1425 					   reg_value); /* put write data */
1426 
1427 		btcoexist->btc_write_4byte(btcoexist, 0x7c0,
1428 					   0xc00F0000 | reg_addr);
1429 	} else {
1430 		for (i = 0; i <= 31; i++) {
1431 			if (((bit_mask >> i) & 0x1) == 0x1) {
1432 				bitpos = i;
1433 				break;
1434 			}
1435 		}
1436 
1437 		/* read back register value before write */
1438 		val = halbtc8723d1ant_ltecoex_indirect_read_reg(btcoexist,
1439 				reg_addr);
1440 		val = (val & (~bit_mask)) | (reg_value << bitpos);
1441 
1442 		/* wait for ready bit before access 0x7c0/0x7c4 */
1443 		while (1) {
1444 			if ((btcoexist->btc_read_1byte(btcoexist, 0x7c3)&BIT(5)) == 0) {
1445 				delay_ms(50);
1446 				delay_count++;
1447 				if (delay_count >= 10) {
1448 					delay_count = 0;
1449 					break;
1450 				}
1451 			} else
1452 				break;
1453 		}
1454 
1455 		btcoexist->btc_write_4byte(btcoexist, 0x7c4,
1456 					   val); /* put write data */
1457 
1458 		btcoexist->btc_write_4byte(btcoexist, 0x7c0,
1459 					   0xc00F0000 | reg_addr);
1460 
1461 	}
1462 
1463 }
1464 
1465 static
halbtc8723d1ant_ltecoex_enable(IN struct btc_coexist * btcoexist,IN boolean enable)1466 void halbtc8723d1ant_ltecoex_enable(IN struct btc_coexist *btcoexist,
1467 				    IN boolean enable)
1468 {
1469 	u8 val;
1470 
1471 	val = (enable) ? 1 : 0;
1472 	halbtc8723d1ant_ltecoex_indirect_write_reg(btcoexist, 0x38, 0x80,
1473 			val);  /* 0x38[7] */
1474 
1475 }
1476 
1477 static
halbtc8723d1ant_coex_ctrl_owner(IN struct btc_coexist * btcoexist,IN boolean wifi_control)1478 void halbtc8723d1ant_coex_ctrl_owner(IN struct btc_coexist *btcoexist,
1479 				     IN boolean wifi_control)
1480 {
1481 	u8 val;
1482 
1483 	val = (wifi_control) ? 1 : 0;
1484 	btcoexist->btc_write_1byte_bitmask(btcoexist, 0x73, 0x4,
1485 					   val); /* 0x70[26] */
1486 
1487 }
1488 
1489 static
halbtc8723d1ant_ltecoex_set_gnt_bt(IN struct btc_coexist * btcoexist,IN u8 control_block,IN boolean sw_control,IN u8 state)1490 void halbtc8723d1ant_ltecoex_set_gnt_bt(IN struct btc_coexist *btcoexist,
1491 			IN u8 control_block, IN boolean sw_control, IN u8 state)
1492 {
1493 	u32 val = 0, val_orig = 0;
1494 
1495 	if (!sw_control)
1496 		val = 0x0;
1497 	else if (state & 0x1)
1498 		val = 0x3;
1499 	else
1500 		val = 0x1;
1501 
1502 	val_orig = halbtc8723d1ant_ltecoex_indirect_read_reg(btcoexist,
1503 				0x38);
1504 
1505 	switch (control_block) {
1506 	case BT_8723D_1ANT_GNT_BLOCK_RFC_BB:
1507 	default:
1508 		val = ((val << 14) | (val << 10)) | (val_orig & 0xffff33ff);
1509 		break;
1510 	case BT_8723D_1ANT_GNT_BLOCK_RFC:
1511 		val = (val << 14) | (val_orig & 0xffff3fff);
1512 		break;
1513 	case BT_8723D_1ANT_GNT_BLOCK_BB:
1514 		val = (val << 10) | (val_orig & 0xfffff3ff);
1515 		break;
1516 	}
1517 
1518 	halbtc8723d1ant_ltecoex_indirect_write_reg(btcoexist,
1519 				0x38, 0xffffffff, val);
1520 }
1521 
1522 static
halbtc8723d1ant_ltecoex_set_gnt_wl(IN struct btc_coexist * btcoexist,IN u8 control_block,IN boolean sw_control,IN u8 state)1523 void halbtc8723d1ant_ltecoex_set_gnt_wl(IN struct btc_coexist *btcoexist,
1524 			IN u8 control_block, IN boolean sw_control, IN u8 state)
1525 {
1526 	u32 val = 0, val_orig = 0;
1527 
1528 	if (!sw_control)
1529 		val = 0x0;
1530 	else if (state & 0x1)
1531 		val = 0x3;
1532 	else
1533 		val = 0x1;
1534 
1535 	val_orig = halbtc8723d1ant_ltecoex_indirect_read_reg(btcoexist,
1536 				0x38);
1537 
1538 	switch (control_block) {
1539 	case BT_8723D_1ANT_GNT_BLOCK_RFC_BB:
1540 	default:
1541 		val = ((val << 12) | (val << 8)) | (val_orig & 0xffffccff);
1542 		break;
1543 	case BT_8723D_1ANT_GNT_BLOCK_RFC:
1544 		val = (val << 12) | (val_orig & 0xffffcfff);
1545 		break;
1546 	case BT_8723D_1ANT_GNT_BLOCK_BB:
1547 		val = (val << 8) | (val_orig & 0xfffffcff);
1548 		break;
1549 	}
1550 
1551 	halbtc8723d1ant_ltecoex_indirect_write_reg(btcoexist, 0x38,
1552 		0xffffffff, val);
1553 }
1554 
1555 static
halbtc8723d1ant_ltecoex_set_coex_table(IN struct btc_coexist * btcoexist,IN u8 table_type,IN u16 table_content)1556 void halbtc8723d1ant_ltecoex_set_coex_table(IN struct btc_coexist *btcoexist,
1557 		IN u8 table_type, IN u16 table_content)
1558 {
1559 	u16 reg_addr = 0x0000;
1560 
1561 	switch (table_type) {
1562 	case BT_8723D_1ANT_CTT_WL_VS_LTE:
1563 		reg_addr = 0xa0;
1564 		break;
1565 	case BT_8723D_1ANT_CTT_BT_VS_LTE:
1566 		reg_addr = 0xa4;
1567 		break;
1568 	}
1569 
1570 	if (reg_addr != 0x0000)
1571 		halbtc8723d1ant_ltecoex_indirect_write_reg(btcoexist, reg_addr,
1572 			0xffff, table_content); /* 0xa0[15:0] or 0xa4[15:0] */
1573 
1574 
1575 }
1576 
1577 static
halbtc8723d1ant_coex_table(IN struct btc_coexist * btcoexist,IN boolean force_exec,IN u32 val0x6c0,IN u32 val0x6c4,IN u32 val0x6c8,IN u8 val0x6cc)1578 void halbtc8723d1ant_coex_table(IN struct btc_coexist *btcoexist,
1579 				IN boolean force_exec, IN u32 val0x6c0,
1580 				IN u32 val0x6c4, IN u32 val0x6c8,
1581 				IN u8 val0x6cc)
1582 {
1583 	if (!force_exec) {
1584 		if (val0x6c0 == coex_dm->cur_val0x6c0 &&
1585 		    val0x6c4 == coex_dm->cur_val0x6c4 &&
1586 		    val0x6c8 == coex_dm->cur_val0x6c8 &&
1587 		    val0x6cc == coex_dm->cur_val0x6cc)
1588 			return;
1589 	}
1590 
1591 	btcoexist->btc_write_4byte(btcoexist, 0x6c0, val0x6c0);
1592 	btcoexist->btc_write_4byte(btcoexist, 0x6c4, val0x6c4);
1593 	btcoexist->btc_write_4byte(btcoexist, 0x6c8, val0x6c8);
1594 	btcoexist->btc_write_1byte(btcoexist, 0x6cc, val0x6cc);
1595 
1596 	coex_dm->cur_val0x6c0 = val0x6c0;
1597 	coex_dm->cur_val0x6c4 = val0x6c4;
1598 	coex_dm->cur_val0x6c8 = val0x6c8;
1599 	coex_dm->cur_val0x6cc = val0x6cc;
1600 
1601 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1602 		    "[BTCoex], 0x6c0 = 0x%x, 0x6c4 = 0x%x, 0x6c8 = 0x%x, 0x6cc = 0x%x,\n",
1603 		    coex_dm->cur_val0x6c0, coex_dm->cur_val0x6c4,
1604 		    coex_dm->cur_val0x6c8, coex_dm->cur_val0x6cc);
1605 	BTC_TRACE(trace_buf);
1606 
1607 }
1608 
1609 static
halbtc8723d1ant_coex_table_with_type(IN struct btc_coexist * btcoexist,IN boolean force_exec,IN u8 type)1610 void halbtc8723d1ant_coex_table_with_type(IN struct btc_coexist *btcoexist,
1611 		IN boolean force_exec, IN u8 type)
1612 {
1613 	u32	break_table;
1614 	u8	select_table;
1615 
1616 	coex_sta->coex_table_type = type;
1617 
1618 	if (coex_sta->concurrent_rx_mode_on == TRUE) {
1619 		break_table = 0xf0ffffff;  /* set WL hi-pri can break BT */
1620 		/* set Tx response = Hi-Pri (ex: Transmitting ACK,BA,CTS) */
1621 		select_table = 0xb;
1622 	} else {
1623 		break_table = 0xffffff;
1624 		select_table = 0x3;
1625 	}
1626 
1627 	switch (type) {
1628 	case 0:
1629 		halbtc8723d1ant_coex_table(btcoexist, force_exec,
1630 					   0x55555555, 0x55555555, break_table,
1631 					   select_table);
1632 		break;
1633 	case 1:
1634 		halbtc8723d1ant_coex_table(btcoexist, force_exec,
1635 					   0xa5555555, 0xaa5a5a5a, break_table,
1636 					   select_table);
1637 		break;
1638 	case 2:
1639 		halbtc8723d1ant_coex_table(btcoexist, force_exec,
1640 					   0xaa5a5a5a, 0xaa5a5a5a, break_table,
1641 					   select_table);
1642 		break;
1643 	case 3:
1644 		halbtc8723d1ant_coex_table(btcoexist, force_exec,
1645 					   0x55555555, 0x5a5a5a5a, break_table,
1646 					   select_table);
1647 		break;
1648 	case 4:
1649 		halbtc8723d1ant_coex_table(btcoexist, force_exec,
1650 					   0xa5555555, 0xaa5a5a5a, break_table,
1651 					   select_table);
1652 		break;
1653 	case 5:
1654 		halbtc8723d1ant_coex_table(btcoexist, force_exec,
1655 					   0x5a5a5a5a, 0x5a5a5a5a, break_table,
1656 					   select_table);
1657 		break;
1658 	case 6:
1659 		halbtc8723d1ant_coex_table(btcoexist, force_exec,
1660 					   0xa5555555, 0xaa5a5a5a, break_table,
1661 					   select_table);
1662 		break;
1663 	case 7:
1664 		halbtc8723d1ant_coex_table(btcoexist, force_exec,
1665 					   0xaa555555, 0xaa555555, break_table,
1666 					   select_table);
1667 		break;
1668 	case 8:
1669 		halbtc8723d1ant_coex_table(btcoexist, force_exec,
1670 					   0xa5555555, 0xaaaa5aaa, break_table,
1671 					   select_table);
1672 		break;
1673 	case 9:
1674 		halbtc8723d1ant_coex_table(btcoexist, force_exec,
1675 					   0x5a5a5a5a, 0xaaaa5aaa, break_table,
1676 					   select_table);
1677 		break;
1678 	case 10:
1679 		halbtc8723d1ant_coex_table(btcoexist, force_exec,
1680 					   0xaaaaaaaa, 0xaaaaaaaa, break_table,
1681 					   select_table);
1682 		break;
1683 	case 11:
1684 		halbtc8723d1ant_coex_table(btcoexist, force_exec,
1685 					   0xa5a55555, 0xaaaa5a5a, break_table,
1686 					   select_table);
1687 		break;
1688 	case 12:
1689 		halbtc8723d1ant_coex_table(btcoexist, force_exec,
1690 					   0xa5555555, 0xaaaa5a5a, break_table,
1691 					   select_table);
1692 		break;
1693 	case 13:
1694 		halbtc8723d1ant_coex_table(btcoexist, force_exec,
1695 					   0x65555555, 0xaa5a5a5a, break_table,
1696 					   select_table);
1697 		break;
1698 	case 14:
1699 		halbtc8723d1ant_coex_table(btcoexist, force_exec,
1700 					   0x65555555, 0x5a5a5a5a, break_table,
1701 					   select_table);
1702 		break;
1703 	case 15:
1704 		halbtc8723d1ant_coex_table(btcoexist, force_exec,
1705 					   0xaaaaaaaa, 0x5a5a5a5a, break_table,
1706 					   select_table);
1707 		break;
1708 	case 16:
1709 		halbtc8723d1ant_coex_table(btcoexist, force_exec,
1710 					   0x55555555, 0xaaaaaaaa, break_table,
1711 					   select_table);
1712 		break;
1713 	case 17:
1714 		halbtc8723d1ant_coex_table(btcoexist, force_exec,
1715 					   0x65555555, 0x65555555, break_table,
1716 					   select_table);
1717 		break;
1718 	case 18:
1719 		halbtc8723d1ant_coex_table(btcoexist, force_exec,
1720 					   0x65555555, 0xaaaaaaaa, break_table,
1721 					   select_table);
1722 		break;
1723 	case 19:
1724 		halbtc8723d1ant_coex_table(btcoexist, force_exec,
1725 					   0x66555555, 0x6a5a5a5a, break_table,
1726 					   select_table);
1727 		break;
1728 	default:
1729 		break;
1730 	}
1731 }
1732 
1733 static
halbtc8723d1ant_set_fw_ignore_wlan_act(IN struct btc_coexist * btcoexist,IN boolean enable)1734 void halbtc8723d1ant_set_fw_ignore_wlan_act(IN struct btc_coexist *btcoexist,
1735 		IN boolean enable)
1736 {
1737 	u8			h2c_parameter[1] = {0};
1738 
1739 	if (enable) {
1740 		h2c_parameter[0] |= BIT(0);		/* function enable */
1741 	}
1742 
1743 	btcoexist->btc_fill_h2c(btcoexist, 0x63, 1, h2c_parameter);
1744 }
1745 
1746 static
halbtc8723d1ant_ignore_wlan_act(IN struct btc_coexist * btcoexist,IN boolean force_exec,IN boolean enable)1747 void halbtc8723d1ant_ignore_wlan_act(IN struct btc_coexist *btcoexist,
1748 				     IN boolean force_exec, IN boolean enable)
1749 {
1750 	coex_dm->cur_ignore_wlan_act = enable;
1751 
1752 	if (!force_exec) {
1753 		if (coex_dm->pre_ignore_wlan_act ==
1754 		    coex_dm->cur_ignore_wlan_act)
1755 			return;
1756 	}
1757 	halbtc8723d1ant_set_fw_ignore_wlan_act(btcoexist, enable);
1758 
1759 	coex_dm->pre_ignore_wlan_act = coex_dm->cur_ignore_wlan_act;
1760 }
1761 
1762 static
halbtc8723d1ant_set_lps_rpwm(IN struct btc_coexist * btcoexist,IN u8 lps_val,IN u8 rpwm_val)1763 void halbtc8723d1ant_set_lps_rpwm(IN struct btc_coexist *btcoexist,
1764 				  IN u8 lps_val, IN u8 rpwm_val)
1765 {
1766 	u8	lps = lps_val;
1767 	u8	rpwm = rpwm_val;
1768 
1769 	btcoexist->btc_set(btcoexist, BTC_SET_U1_LPS_VAL, &lps);
1770 	btcoexist->btc_set(btcoexist, BTC_SET_U1_RPWM_VAL, &rpwm);
1771 }
1772 
1773 static
halbtc8723d1ant_lps_rpwm(IN struct btc_coexist * btcoexist,IN boolean force_exec,IN u8 lps_val,IN u8 rpwm_val)1774 void halbtc8723d1ant_lps_rpwm(IN struct btc_coexist *btcoexist,
1775 		      IN boolean force_exec, IN u8 lps_val, IN u8 rpwm_val)
1776 {
1777 	coex_dm->cur_lps = lps_val;
1778 	coex_dm->cur_rpwm = rpwm_val;
1779 
1780 	if (!force_exec) {
1781 		if ((coex_dm->pre_lps == coex_dm->cur_lps) &&
1782 		    (coex_dm->pre_rpwm == coex_dm->cur_rpwm))
1783 			return;
1784 	}
1785 	halbtc8723d1ant_set_lps_rpwm(btcoexist, lps_val, rpwm_val);
1786 
1787 	coex_dm->pre_lps = coex_dm->cur_lps;
1788 	coex_dm->pre_rpwm = coex_dm->cur_rpwm;
1789 }
1790 
1791 static
halbtc8723d1ant_ps_tdma_check_for_power_save_state(IN struct btc_coexist * btcoexist,IN boolean new_ps_state)1792 void halbtc8723d1ant_ps_tdma_check_for_power_save_state(
1793 	IN struct btc_coexist *btcoexist, IN boolean new_ps_state)
1794 {
1795 	u8	lps_mode = 0x0;
1796 	u8	h2c_parameter[5] = {0x8, 0, 0, 0, 0};
1797 
1798 	btcoexist->btc_get(btcoexist, BTC_GET_U1_LPS_MODE, &lps_mode);
1799 
1800 	if (lps_mode) {	/* already under LPS state */
1801 		if (new_ps_state) {
1802 			/* keep state under LPS, do nothing. */
1803 		} else {
1804 			/* will leave LPS state, turn off psTdma first */
1805 			btcoexist->btc_fill_h2c(btcoexist, 0x60, 5,
1806 						h2c_parameter);
1807 		}
1808 	} else {					/* NO PS state */
1809 		if (new_ps_state) {
1810 			/* will enter LPS state, turn off psTdma first */
1811 			btcoexist->btc_fill_h2c(btcoexist, 0x60, 5,
1812 						h2c_parameter);
1813 		} else {
1814 			/* keep state under NO PS state, do nothing. */
1815 		}
1816 	}
1817 }
1818 
1819 static
halbtc8723d1ant_power_save_state(IN struct btc_coexist * btcoexist,IN u8 ps_type,IN u8 lps_val,IN u8 rpwm_val)1820 void halbtc8723d1ant_power_save_state(IN struct btc_coexist *btcoexist,
1821 			      IN u8 ps_type, IN u8 lps_val, IN u8 rpwm_val)
1822 {
1823 	boolean		low_pwr_disable = FALSE;
1824 
1825 	switch (ps_type) {
1826 	case BTC_PS_WIFI_NATIVE:
1827 		/* recover to original 32k low power setting */
1828 		coex_sta->force_lps_ctrl = FALSE;
1829 		low_pwr_disable = FALSE;
1830 		/* btcoexist->btc_set(btcoexist,
1831 				   BTC_SET_ACT_DISABLE_LOW_POWER,
1832 				   &low_pwr_disable); */
1833 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_PRE_NORMAL_LPS,
1834 				   NULL);
1835 
1836 		break;
1837 	case BTC_PS_LPS_ON:
1838 		coex_sta->force_lps_ctrl = TRUE;
1839 		halbtc8723d1ant_ps_tdma_check_for_power_save_state(
1840 			btcoexist, TRUE);
1841 		halbtc8723d1ant_lps_rpwm(btcoexist, NORMAL_EXEC,
1842 					 lps_val, rpwm_val);
1843 		/* when coex force to enter LPS, do not enter 32k low power. */
1844 		low_pwr_disable = TRUE;
1845 		btcoexist->btc_set(btcoexist,
1846 				   BTC_SET_ACT_DISABLE_LOW_POWER,
1847 				   &low_pwr_disable);
1848 		/* power save must executed before psTdma.*/
1849 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_ENTER_LPS,
1850 				   NULL);
1851 
1852 		break;
1853 	case BTC_PS_LPS_OFF:
1854 		coex_sta->force_lps_ctrl = TRUE;
1855 		halbtc8723d1ant_ps_tdma_check_for_power_save_state(
1856 			btcoexist, FALSE);
1857 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_LEAVE_LPS,
1858 				   NULL);
1859 
1860 		break;
1861 	default:
1862 		break;
1863 	}
1864 }
1865 
1866 static
halbtc8723d1ant_set_fw_pstdma(IN struct btc_coexist * btcoexist,IN u8 byte1,IN u8 byte2,IN u8 byte3,IN u8 byte4,IN u8 byte5)1867 void halbtc8723d1ant_set_fw_pstdma(IN struct btc_coexist *btcoexist,
1868 	   IN u8 byte1, IN u8 byte2, IN u8 byte3, IN u8 byte4, IN u8 byte5)
1869 {
1870 	u8			h2c_parameter[5] = {0};
1871 	u8			real_byte1 = byte1, real_byte5 = byte5;
1872 	boolean			ap_enable = FALSE;
1873 	struct  btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1874 	u8		ps_type = BTC_PS_WIFI_NATIVE;
1875 
1876 	if (byte5 & BIT(2))
1877 		coex_sta->is_tdma_btautoslot = TRUE;
1878 	else
1879 		coex_sta->is_tdma_btautoslot = FALSE;
1880 
1881 	/* release bt-auto slot for auto-slot hang is detected!! */
1882 	if (coex_sta->is_tdma_btautoslot)
1883 		if ((coex_sta->is_tdma_btautoslot_hang) ||
1884 			(bt_link_info->slave_role))
1885 			byte5 = byte5 & 0xfb;
1886 
1887 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_AP_MODE_ENABLE,
1888 			   &ap_enable);
1889 
1890 	if ((ap_enable) && (byte1 & BIT(4) && !(byte1 & BIT(5)))) {
1891 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1892 			    "[BTCoex], FW for AP mode\n");
1893 		BTC_TRACE(trace_buf);
1894 		real_byte1 &= ~BIT(4);
1895 		real_byte1 |= BIT(5);
1896 
1897 		real_byte5 |= BIT(5);
1898 		real_byte5 &= ~BIT(6);
1899 
1900 		ps_type = BTC_PS_WIFI_NATIVE;
1901 		halbtc8723d1ant_power_save_state(btcoexist,
1902 					ps_type, 0x0,
1903 					0x0);
1904 	} else if (byte1 & BIT(4) && !(byte1 & BIT(5))) {
1905 
1906 		ps_type = BTC_PS_LPS_ON;
1907 		halbtc8723d1ant_power_save_state(
1908 			btcoexist, ps_type, 0x50,
1909 			0x4);
1910 	} else {
1911 		ps_type = BTC_PS_WIFI_NATIVE;
1912 		halbtc8723d1ant_power_save_state(btcoexist, ps_type,
1913 						 0x0,
1914 						 0x0);
1915 	}
1916 
1917 	h2c_parameter[0] = real_byte1;
1918 	h2c_parameter[1] = byte2;
1919 	h2c_parameter[2] = byte3;
1920 	h2c_parameter[3] = byte4;
1921 	h2c_parameter[4] = real_byte5;
1922 
1923 	coex_dm->ps_tdma_para[0] = real_byte1;
1924 	coex_dm->ps_tdma_para[1] = byte2;
1925 	coex_dm->ps_tdma_para[2] = byte3;
1926 	coex_dm->ps_tdma_para[3] = byte4;
1927 	coex_dm->ps_tdma_para[4] = real_byte5;
1928 
1929 	btcoexist->btc_fill_h2c(btcoexist, 0x60, 5, h2c_parameter);
1930 
1931 	if (ps_type == BTC_PS_WIFI_NATIVE)
1932 		btcoexist->btc_set(btcoexist, BTC_SET_ACT_POST_NORMAL_LPS, NULL);
1933 }
1934 
1935 static
halbtc8723d1ant_ps_tdma(IN struct btc_coexist * btcoexist,IN boolean force_exec,IN boolean turn_on,IN u8 tcase)1936 void halbtc8723d1ant_ps_tdma(IN struct btc_coexist *btcoexist,
1937 		     IN boolean force_exec, IN boolean turn_on, IN u8 tcase)
1938 {
1939 	struct  btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
1940 	struct  btc_board_info	*board_info = &btcoexist->board_info;
1941 	boolean	wifi_busy = FALSE;
1942 	static u8 tdma_byte4_modify, pre_tdma_byte4_modify, type;
1943 	static boolean	 pre_wifi_busy = FALSE;
1944 
1945 	btcoexist->btc_set_atomic(btcoexist, &coex_dm->setting_tdma, TRUE);
1946 
1947 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
1948 
1949 	/* tcase: bit0~7 --> tdma case index
1950 	 *        bit8   --> for 4-slot (50ms) mode
1951 	 */
1952 	if (tcase & TDMA_4SLOT)/* 4-slot (50ms) mode */
1953 		halbtc8723d1ant_set_tdma_timer_base(btcoexist, 3);
1954 	else
1955 		halbtc8723d1ant_set_tdma_timer_base(btcoexist, 0);
1956 
1957 	type = tcase & 0xff;
1958 
1959 	if (wifi_busy != pre_wifi_busy) {
1960 		force_exec = TRUE;
1961 		pre_wifi_busy = wifi_busy;
1962 	}
1963 
1964 	/* 0x778 = 0x1 at wifi slot (no blocking BT Low-Pri pkts) */
1965 	if (bt_link_info->slave_role && bt_link_info->a2dp_exist)
1966 		tdma_byte4_modify = 0x1;
1967 	else
1968 		tdma_byte4_modify = 0x0;
1969 
1970 	if (pre_tdma_byte4_modify != tdma_byte4_modify) {
1971 		force_exec = TRUE;
1972 		pre_tdma_byte4_modify = tdma_byte4_modify;
1973 	}
1974 
1975 	if (!force_exec) {
1976 		if (turn_on == coex_dm->cur_ps_tdma_on &&
1977 		    type == coex_dm->cur_ps_tdma) {
1978 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1979 				    "[BTCoex], Skip TDMA because no change TDMA(%s, %d)\n",
1980 				    (coex_dm->cur_ps_tdma_on ? "on" : "off"),
1981 				    coex_dm->cur_ps_tdma);
1982 			BTC_TRACE(trace_buf);
1983 
1984 			btcoexist->btc_set_atomic(btcoexist,
1985 						  &coex_dm->setting_tdma,
1986 						  FALSE);
1987 			return;
1988 		}
1989 	}
1990 
1991 	if (turn_on) {
1992 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
1993 			    "[BTCoex], ********** TDMA(on, %d)\n", type);
1994 		BTC_TRACE(trace_buf);
1995 
1996 		if (coex_sta->a2dp_exist && coex_sta->bt_inq_page_remain)
1997 			halbtc8723d1ant_post_state_to_bt(btcoexist,
1998 						 BT_8723D_1ANT_SCOREBOARD_TDMA,
1999 						 FALSE);
2000 		else
2001 			halbtc8723d1ant_post_state_to_bt(btcoexist,
2002 						 BT_8723D_1ANT_SCOREBOARD_TDMA,
2003 						 TRUE);
2004 
2005 		/* enable TBTT interrupt */
2006 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x550, 0x8, 0x1);
2007 	} else {
2008 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2009 			    "[BTCoex], ********** TDMA(off, %d)\n", type);
2010 		BTC_TRACE(trace_buf);
2011 
2012 		halbtc8723d1ant_post_state_to_bt(btcoexist,
2013 						 BT_8723D_1ANT_SCOREBOARD_TDMA,
2014 						 FALSE);
2015 	}
2016 
2017 
2018 	if (turn_on) {
2019 		switch (type) {
2020 		default:
2021 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0x61,
2022 						      0x35, 0x03, 0x11, 0x11);
2023 			break;
2024 		case 3:
2025 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0x51,
2026 						      0x30, 0x03, 0x10, 0x50);
2027 			break;
2028 		case 4:
2029 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0x51,
2030 						      0x21, 0x03, 0x10, 0x50);
2031 			break;
2032 		case 5:
2033 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0x61,
2034 						      0x3a, 0x03, 0x11, 0x11);
2035 			break;
2036 		case 6:
2037 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0x61,
2038 						      0x20, 0x03, 0x11, 0x11);
2039 			break;
2040 		case 7:
2041 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0x51,
2042 						      0x10, 0x03, 0x10,  0x54 |
2043 						      tdma_byte4_modify);
2044 			break;
2045 		case 8:
2046 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0x51,
2047 						      0x10, 0x03, 0x10,  0x54 |
2048 						      tdma_byte4_modify);
2049 			break;
2050 		case 9:
2051 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0x51,
2052 						      0x10, 0x03, 0x10,  0x54 |
2053 						      tdma_byte4_modify);
2054 			break;
2055 		case 10:
2056 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0x61,
2057 						      0x30, 0x03, 0x11, 0x10);
2058 			break;
2059 		case 11:
2060 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0x61,
2061 						      0x25, 0x03, 0x11,  0x11 |
2062 						      tdma_byte4_modify);
2063 			break;
2064 		case 12:
2065 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0x51,
2066 						      0x35, 0x03, 0x10,  0x50 |
2067 						      tdma_byte4_modify);
2068 			break;
2069 		case 13:
2070 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0x51,
2071 						      0x10, 0x07, 0x10,  0x54 |
2072 						      tdma_byte4_modify);
2073 			break;
2074 		case 14:
2075 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0x51,
2076 						      0x15, 0x03, 0x10,  0x50 |
2077 						      tdma_byte4_modify);
2078 			break;
2079 		case 15:
2080 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0x51,
2081 						      0x10, 0x03, 0x10,  0x50);
2082 			break;
2083 		case 16:
2084 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0x61,
2085 						      0x10, 0x03, 0x11,  0x15 |
2086 						      tdma_byte4_modify);
2087 			break;
2088 		case 17:
2089 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0x61,
2090 						      0x10, 0x03, 0x11, 0x14);
2091 			break;
2092 		case 18:
2093 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0x51,
2094 						      0x30, 0x03, 0x10,  0x50 |
2095 						      tdma_byte4_modify);
2096 			break;
2097 		case 19:
2098 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0x61,
2099 						      0x15, 0x03, 0x11, 0x10);
2100 			break;
2101 		case 20:
2102 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0x61,
2103 						      0x30, 0x03, 0x11, 0x10);
2104 			break;
2105 		case 21:
2106 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0x61,
2107 						      0x30, 0x03, 0x11, 0x10);
2108 			break;
2109 		case 22:
2110 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0x61,
2111 						      0x25, 0x03, 0x11, 0x10);
2112 			break;
2113 		case 23:
2114 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0x61,
2115 						      0x10, 0x03, 0x11, 0x10);
2116 			break;
2117 		case 24:
2118 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0x51,
2119 						      0x08, 0x03, 0x10,  0x54 |
2120 						      tdma_byte4_modify);
2121 			break;
2122 		case 25:
2123 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0x51,
2124 						      0x3a, 0x03, 0x11, 0x50);
2125 			break;
2126 		case 26:
2127 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0x51,
2128 						      0x10, 0x03, 0x10,  0x55);
2129 			break;
2130 		case 27:
2131 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0x61,
2132 						      0x10, 0x03, 0x11, 0x15);
2133 			break;
2134 		case 28:
2135 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0x51,
2136 						      0x10, 0x0b, 0x10,  0x54);
2137 			break;
2138 		case 29:
2139 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0x51,
2140 						      0x0c, 0x03, 0x10,  0x54);
2141 			break;
2142 		case 32:
2143 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0x61,
2144 						      0x35, 0x03, 0x11, 0x11);
2145 			break;
2146 		case 33:
2147 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0x61,
2148 						      0x35, 0x03, 0x11, 0x10);
2149 			break;
2150 		case 34:
2151 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0x61,
2152 						      0x3f, 0x03, 0x11, 0x10);
2153 			break;
2154 		case 36:
2155 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0x61,
2156 						      0x48, 0x03, 0x11, 0x10);
2157 			break;
2158 		case 57:
2159 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0x51,
2160 						      0x10, 0x03, 0x10,  0x50 |
2161 						      tdma_byte4_modify);
2162 			break;
2163 		case 58:
2164 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0x51,
2165 						      0x10, 0x03, 0x10,  0x51);
2166 			break;
2167 		case 67:
2168 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0x61,
2169 						      0x10, 0x03, 0x11,  0x10 |
2170 						      tdma_byte4_modify);
2171 			break;
2172 		/*     1-Ant to 2-Ant      TDMA case */
2173 		case 103:
2174 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0xd3,
2175 						      0x3a, 0x03, 0x70, 0x10);
2176 			break;
2177 		case 104:
2178 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0xd3,
2179 						      0x21, 0x03, 0x70, 0x10);
2180 			break;
2181 		case 105:
2182 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0xe3,
2183 						      0x15, 0x03, 0x71, 0x11);
2184 			break;
2185 		case 106:
2186 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0xe3,
2187 						      0x20, 0x03, 0x71, 0x11);
2188 			break;
2189 		case 107:
2190 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0xd3,
2191 						      0x10, 0x03, 0x70,  0x14 |
2192 						      tdma_byte4_modify);
2193 			break;
2194 		case 108:
2195 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0xd3,
2196 						      0x10, 0x03, 0x70,  0x14 |
2197 						      tdma_byte4_modify);
2198 			break;
2199 		case 113:
2200 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0xd3,
2201 						      0x25, 0x03, 0x70,  0x10 |
2202 						      tdma_byte4_modify);
2203 			break;
2204 		case 114:
2205 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0xd3,
2206 						      0x15, 0x03, 0x70,  0x10 |
2207 						      tdma_byte4_modify);
2208 			break;
2209 		case 115:
2210 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0xd3,
2211 						      0x20, 0x03, 0x70,  0x10 |
2212 						      tdma_byte4_modify);
2213 			break;
2214 		case 117:
2215 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0xe3,
2216 						      0x10, 0x03, 0x71,  0x14 |
2217 						      tdma_byte4_modify);
2218 			break;
2219 		case 119:
2220 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0xe3,
2221 						      0x15, 0x03, 0x71, 0x10);
2222 			break;
2223 		case 120:
2224 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0xe3,
2225 						      0x30, 0x03, 0x71, 0x10);
2226 			break;
2227 		case 121:
2228 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0xe3,
2229 						      0x30, 0x03, 0x71, 0x10);
2230 			break;
2231 		case 122:
2232 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0xe3,
2233 						      0x25, 0x03, 0x71, 0x10);
2234 			break;
2235 		case 132:
2236 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0xe3,
2237 						      0x35, 0x03, 0x71, 0x11);
2238 			break;
2239 		case 133:
2240 			halbtc8723d1ant_set_fw_pstdma(btcoexist, 0xe3,
2241 						      0x35, 0x03, 0x71, 0x10);
2242 			break;
2243 		}
2244 	} else {
2245 
2246 		/* disable PS tdma */
2247 		switch (type) {
2248 		case 8: /* PTA Control */
2249 			halbtc8723d1ant_set_fw_pstdma(btcoexist,
2250 						      0x8, 0x0, 0x0, 0x0, 0x0);
2251 			break;
2252 		case 0:
2253 		default:  /* Software control, Antenna at BT side */
2254 			halbtc8723d1ant_set_fw_pstdma(btcoexist,
2255 						      0x0, 0x0, 0x0, 0x0, 0x0);
2256 			break;
2257 		case 1: /* 2-Ant, 0x778=3, antenna control by antenna diversity */
2258 			halbtc8723d1ant_set_fw_pstdma(btcoexist,
2259 						      0x0, 0x0, 0x0, 0x48, 0x0);
2260 			break;
2261 		}
2262 	}
2263 
2264 	/* update pre state */
2265 	coex_dm->cur_ps_tdma_on = turn_on;
2266 	coex_dm->cur_ps_tdma = type;
2267 
2268 	btcoexist->btc_set_atomic(btcoexist, &coex_dm->setting_tdma, FALSE);
2269 }
2270 
2271 static
halbtc8723d1ant_set_ant_path(IN struct btc_coexist * btcoexist,IN u8 ant_pos_type,IN boolean force_exec,IN u8 phase)2272 void halbtc8723d1ant_set_ant_path(IN struct btc_coexist *btcoexist,
2273 				  IN u8 ant_pos_type, IN boolean force_exec,
2274 				  IN u8 phase)
2275 {
2276 	struct  btc_board_info *board_info = &btcoexist->board_info;
2277 	u32			cnt_bt_cal_chk = 0;
2278 	boolean			is_in_mp_mode = FALSE, is_hw_ant_div_on = FALSE;
2279 	u8			u8tmp0 = 0, u8tmp1 = 0;
2280 	u32			u32tmp1 = 0, u32tmp2 = 0, u32tmp3 = 0;
2281 	u16			u16tmp0, u16tmp1 = 0;
2282 
2283 #if 0
2284 
2285 	if (ant_pos_type == BTC_ANT_PATH_PTA) {
2286 		if ((board_info->btdm_ant_det_finish) &&
2287 		    (board_info->btdm_ant_num_by_ant_det == 2)) {
2288 			if (board_info->btdm_ant_pos ==
2289 			    BTC_ANTENNA_AT_MAIN_PORT)
2290 				ant_pos_type = BTC_ANT_PATH_WIFI;
2291 			else
2292 				ant_pos_type = BTC_ANT_PATH_BT;
2293 		}
2294 	}
2295 
2296 #endif
2297 
2298 #if BT_8723D_1ANT_COEX_DBG
2299 
2300 	u32tmp1 = halbtc8723d1ant_ltecoex_indirect_read_reg(btcoexist, 0x38);
2301 
2302 	/* To avoid indirect access fail	*/
2303 	if (((u32tmp1 & 0xf000) >> 12) != ((u32tmp1 & 0x0f00) >> 8)) {
2304 		force_exec = TRUE;
2305 		coex_sta->gnt_error_cnt++;
2306 	}
2307 
2308 	u32tmp2 = halbtc8723d1ant_ltecoex_indirect_read_reg(btcoexist, 0x54);
2309 	u16tmp0 = btcoexist->btc_read_2byte(btcoexist, 0xaa);
2310 	u16tmp1 = btcoexist->btc_read_2byte(btcoexist, 0x948);
2311 	u8tmp1  = btcoexist->btc_read_1byte(btcoexist, 0x73);
2312 	u8tmp0 =  btcoexist->btc_read_1byte(btcoexist, 0x67);
2313 
2314 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2315 		"[BTCoex], ********** 0x67 = 0x%x, 0x948 = 0x%x, 0x73 = 0x%x(Before Set Ant Pat)\n",
2316 		    u8tmp0, u16tmp1, u8tmp1);
2317 	BTC_TRACE(trace_buf);
2318 
2319 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2320 		"[BTCoex], **********0x38= 0x%x, 0x54= 0x%x, 0xaa = 0x%x(Before Set Ant Path)\n",
2321 		    u32tmp1, u32tmp2, u16tmp0);
2322 	BTC_TRACE(trace_buf);
2323 #endif
2324 
2325 	coex_dm->cur_ant_pos_type = ant_pos_type;
2326 
2327 	if (!force_exec) {
2328 		if (coex_dm->cur_ant_pos_type == coex_dm->pre_ant_pos_type) {
2329 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2330 				"[BTCoex], Skip Antenna Path Setup because no change!!\n");
2331 			BTC_TRACE(trace_buf);
2332 			return;
2333 		}
2334 	}
2335 
2336 	coex_dm->pre_ant_pos_type = coex_dm->cur_ant_pos_type;
2337 
2338 
2339 	switch (phase) {
2340 	case BT_8723D_1ANT_PHASE_COEX_POWERON:
2341 		/* Set Path control to WL */
2342 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x67,
2343 						   0x80, 0x0);
2344 
2345 		/* set Path control owner to WL at initial step */
2346 		halbtc8723d1ant_coex_ctrl_owner(btcoexist,
2347 						BT_8723D_1ANT_PCO_BTSIDE);
2348 
2349 		if (BTC_ANT_PATH_AUTO == ant_pos_type)
2350 			ant_pos_type = BTC_ANT_PATH_BT;
2351 
2352 		coex_sta->run_time_state = FALSE;
2353 
2354 		break;
2355 	case BT_8723D_1ANT_PHASE_COEX_INIT:
2356 		/* Disable LTE Coex Function in WiFi side
2357 		 *(this should be on if LTE coex is required)
2358 		 */
2359 		halbtc8723d1ant_ltecoex_enable(btcoexist, 0x0);
2360 
2361 		/* GNT_WL_LTE always = 1
2362 		 *(this should be config if LTE coex is required)
2363 		 */
2364 		halbtc8723d1ant_ltecoex_set_coex_table(btcoexist,
2365 				       BT_8723D_1ANT_CTT_WL_VS_LTE, 0xffff);
2366 
2367 		/* GNT_BT_LTE always = 1
2368 		 *(this should be config if LTE coex is required)
2369 		 */
2370 		halbtc8723d1ant_ltecoex_set_coex_table(btcoexist,
2371 				       BT_8723D_1ANT_CTT_BT_VS_LTE, 0xffff);
2372 
2373 		/* Wait If BT IQK running, because Path control owner is
2374 		 *at BT during BT IQK (setup by WiFi firmware)
2375 		 */
2376 		while (cnt_bt_cal_chk <= 20) {
2377 			u8tmp0 = btcoexist->btc_read_1byte(btcoexist,
2378 							   0x49d);
2379 			cnt_bt_cal_chk++;
2380 			if (u8tmp0 & BIT(0)) {
2381 				BTC_SPRINTF(trace_buf,
2382 					    BT_TMP_BUF_SIZE,
2383 					    "[BTCoex],  BT is calibrating (wait cnt=%d)\n",
2384 					    cnt_bt_cal_chk);
2385 				BTC_TRACE(trace_buf);
2386 				delay_ms(50);
2387 			} else {
2388 				BTC_SPRINTF(trace_buf,
2389 					    BT_TMP_BUF_SIZE,
2390 					    "[BTCoex], WL is NOT calibrating (wait cnt=%d)\n",
2391 					    cnt_bt_cal_chk);
2392 				BTC_TRACE(trace_buf);
2393 				break;
2394 			}
2395 		}
2396 
2397 		/* Set Path control to WL */
2398 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x67, 0x80, 0x1);
2399 
2400 		/* set Path control owner to WL at initial step */
2401 		halbtc8723d1ant_coex_ctrl_owner(btcoexist,
2402 						BT_8723D_1ANT_PCO_WLSIDE);
2403 
2404 		/* set GNT_BT to SW high */
2405 		halbtc8723d1ant_ltecoex_set_gnt_bt(btcoexist,
2406 					   BT_8723D_1ANT_GNT_BLOCK_RFC_BB,
2407 					   BT_8723D_1ANT_GNT_TYPE_CTRL_BY_SW,
2408 					   BT_8723D_1ANT_SIG_STA_SET_TO_HIGH);
2409 		/* Set GNT_WL to SW low */
2410 		halbtc8723d1ant_ltecoex_set_gnt_wl(btcoexist,
2411 					   BT_8723D_1ANT_GNT_BLOCK_RFC_BB,
2412 					   BT_8723D_1ANT_GNT_TYPE_CTRL_BY_SW,
2413 					   BT_8723D_1ANT_SIG_STA_SET_TO_HIGH);
2414 
2415 		if (BTC_ANT_PATH_AUTO == ant_pos_type)
2416 			ant_pos_type = BTC_ANT_PATH_BT;
2417 
2418 		coex_sta->run_time_state = FALSE;
2419 		break;
2420 	case BT_8723D_1ANT_PHASE_WLANONLY_INIT:
2421 		/* Disable LTE Coex Function in WiFi side
2422 		 *(this should be on if LTE coex is required)
2423 		 */
2424 		halbtc8723d1ant_ltecoex_enable(btcoexist, 0x0);
2425 
2426 		/* GNT_WL_LTE always = 1
2427 		 *(this should be config if LTE coex is required)
2428 		 */
2429 		halbtc8723d1ant_ltecoex_set_coex_table(btcoexist,
2430 				       BT_8723D_1ANT_CTT_WL_VS_LTE, 0xffff);
2431 
2432 		/* GNT_BT_LTE always = 1
2433 		 *(this should be config if LTE coex is required)
2434 		 */
2435 		halbtc8723d1ant_ltecoex_set_coex_table(btcoexist,
2436 				       BT_8723D_1ANT_CTT_BT_VS_LTE, 0xffff);
2437 
2438 		/* Set Path control to WL */
2439 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x67, 0x80, 0x1);
2440 
2441 		/* set Path control owner to WL at initial step */
2442 		halbtc8723d1ant_coex_ctrl_owner(btcoexist,
2443 						BT_8723D_1ANT_PCO_WLSIDE);
2444 
2445 		/* set GNT_BT to SW low */
2446 		halbtc8723d1ant_ltecoex_set_gnt_bt(btcoexist,
2447 					   BT_8723D_1ANT_GNT_BLOCK_RFC_BB,
2448 					   BT_8723D_1ANT_GNT_TYPE_CTRL_BY_SW,
2449 					   BT_8723D_1ANT_SIG_STA_SET_TO_LOW);
2450 		/* Set GNT_WL to SW high */
2451 		halbtc8723d1ant_ltecoex_set_gnt_wl(btcoexist,
2452 					   BT_8723D_1ANT_GNT_BLOCK_RFC_BB,
2453 					   BT_8723D_1ANT_GNT_TYPE_CTRL_BY_SW,
2454 					   BT_8723D_1ANT_SIG_STA_SET_TO_HIGH);
2455 
2456 		if (BTC_ANT_PATH_AUTO == ant_pos_type)
2457 			ant_pos_type = BTC_ANT_PATH_WIFI;
2458 
2459 		coex_sta->run_time_state = FALSE;
2460 		break;
2461 	case BT_8723D_1ANT_PHASE_WLAN_OFF:
2462 		/* Disable LTE Coex Function in WiFi side */
2463 		halbtc8723d1ant_ltecoex_enable(btcoexist, 0x0);
2464 
2465 		/* Set Path control to BT */
2466 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x67,
2467 						   0x80, 0x0);
2468 
2469 		/* set Path control owner to BT */
2470 		halbtc8723d1ant_coex_ctrl_owner(btcoexist,
2471 						BT_8723D_1ANT_PCO_BTSIDE);
2472 
2473 		/* halbtc8723d1ant_ignore_wlan_act
2474 		 * (btcoexist, FORCE_EXEC, TRUE);
2475 		 */
2476 
2477 		if (BTC_ANT_PATH_AUTO == ant_pos_type)
2478 			ant_pos_type = BTC_ANT_PATH_BT;
2479 
2480 		coex_sta->run_time_state = FALSE;
2481 		break;
2482 	case BT_8723D_1ANT_PHASE_2G_RUNTIME:
2483 
2484 		/* wait for WL/BT IQK finish, keep 0x38 = 0xff00 for WL IQK */
2485 		while (cnt_bt_cal_chk <= 20) {
2486 			u8tmp0 = btcoexist->btc_read_1byte(btcoexist, 0x1e6);
2487 
2488 			u8tmp1 = btcoexist->btc_read_1byte(btcoexist, 0x49d);
2489 
2490 			cnt_bt_cal_chk++;
2491 			if ((u8tmp0 & BIT(0)) || (u8tmp1 & BIT(0))) {
2492 				BTC_SPRINTF(trace_buf,
2493 					    BT_TMP_BUF_SIZE,
2494 					    "[BTCoex], ########### WL or BT is IQK (wait cnt=%d)\n",
2495 					    cnt_bt_cal_chk);
2496 				BTC_TRACE(trace_buf);
2497 				delay_ms(50);
2498 			} else {
2499 				BTC_SPRINTF(trace_buf,
2500 					    BT_TMP_BUF_SIZE,
2501 					    "[BTCoex], ********** WL and BT is NOT IQK (wait cnt=%d)\n",
2502 					    cnt_bt_cal_chk);
2503 				BTC_TRACE(trace_buf);
2504 				break;
2505 			}
2506 		}
2507 
2508 
2509 		/* Set Path control to WL */
2510 		/* btcoexist->btc_write_1byte_bitmask(btcoexist, 0x67, 0x80, 0x1); */
2511 
2512 		halbtc8723d1ant_coex_ctrl_owner(btcoexist,
2513 						BT_8723D_1ANT_PCO_WLSIDE);
2514 
2515 		/* set GNT_BT to PTA */
2516 		halbtc8723d1ant_ltecoex_set_gnt_bt(btcoexist,
2517 					   BT_8723D_1ANT_GNT_BLOCK_RFC_BB,
2518 					   BT_8723D_1ANT_GNT_TYPE_CTRL_BY_PTA,
2519 					   BT_8723D_1ANT_SIG_STA_SET_BY_HW);
2520 		/* Set GNT_WL to PTA */
2521 		halbtc8723d1ant_ltecoex_set_gnt_wl(btcoexist,
2522 					   BT_8723D_1ANT_GNT_BLOCK_RFC_BB,
2523 					   BT_8723D_1ANT_GNT_TYPE_CTRL_BY_PTA,
2524 					   BT_8723D_1ANT_SIG_STA_SET_BY_HW);
2525 
2526 		if (BTC_ANT_PATH_AUTO == ant_pos_type)
2527 			ant_pos_type = BTC_ANT_PATH_PTA;
2528 
2529 		coex_sta->run_time_state = TRUE;
2530 		break;
2531 	case BT_8723D_1ANT_PHASE_BTMPMODE:
2532 		halbtc8723d1ant_coex_ctrl_owner(btcoexist,
2533 						BT_8723D_1ANT_PCO_WLSIDE);
2534 
2535 		/* Set Path control to WL */
2536 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x67, 0x80, 0x1);
2537 
2538 		/* set GNT_BT to high */
2539 		halbtc8723d1ant_ltecoex_set_gnt_bt(btcoexist,
2540 					   BT_8723D_1ANT_GNT_BLOCK_RFC_BB,
2541 					   BT_8723D_1ANT_GNT_TYPE_CTRL_BY_SW,
2542 					   BT_8723D_1ANT_SIG_STA_SET_TO_HIGH);
2543 		/* Set GNT_WL to low */
2544 		halbtc8723d1ant_ltecoex_set_gnt_wl(btcoexist,
2545 					   BT_8723D_1ANT_GNT_BLOCK_RFC_BB,
2546 					   BT_8723D_1ANT_GNT_TYPE_CTRL_BY_SW,
2547 					   BT_8723D_1ANT_SIG_STA_SET_TO_LOW);
2548 
2549 		if (BTC_ANT_PATH_AUTO == ant_pos_type)
2550 			ant_pos_type = BTC_ANT_PATH_BT;
2551 
2552 		coex_sta->run_time_state = FALSE;
2553 		break;
2554 	case BT_8723D_1ANT_PHASE_ANTENNA_DET:
2555 		halbtc8723d1ant_coex_ctrl_owner(btcoexist,
2556 						BT_8723D_1ANT_PCO_WLSIDE);
2557 
2558 		/* Set Path control to WL */
2559 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x67, 0x80, 0x1);
2560 
2561 		/* set GNT_BT to high */
2562 		halbtc8723d1ant_ltecoex_set_gnt_bt(btcoexist,
2563 					   BT_8723D_1ANT_GNT_BLOCK_RFC_BB,
2564 					   BT_8723D_1ANT_GNT_TYPE_CTRL_BY_SW,
2565 					   BT_8723D_1ANT_SIG_STA_SET_TO_HIGH);
2566 		/* Set GNT_WL to high */
2567 		halbtc8723d1ant_ltecoex_set_gnt_wl(btcoexist,
2568 					   BT_8723D_1ANT_GNT_BLOCK_RFC_BB,
2569 					   BT_8723D_1ANT_GNT_TYPE_CTRL_BY_SW,
2570 					   BT_8723D_1ANT_SIG_STA_SET_TO_HIGH);
2571 
2572 		if (BTC_ANT_PATH_AUTO == ant_pos_type)
2573 			ant_pos_type = BTC_ANT_PATH_BT;
2574 
2575 		coex_sta->run_time_state = FALSE;
2576 
2577 		break;
2578 	}
2579 
2580 
2581 	is_hw_ant_div_on = board_info->ant_div_cfg;
2582 
2583 	if ((is_hw_ant_div_on) && (phase != BT_8723D_1ANT_PHASE_ANTENNA_DET))
2584 
2585 		if (board_info->btdm_ant_pos == BTC_ANTENNA_AT_MAIN_PORT)
2586 			/* 0x948 = 0x200, 0x0 while antenna diversity */
2587 			btcoexist->btc_write_2byte(btcoexist, 0x948, 0x100);
2588 		else /* 0x948 = 0x80, 0x0 while antenna diversity */
2589 			btcoexist->btc_write_2byte(btcoexist, 0x948, 0x40);
2590 
2591 	else if ((is_hw_ant_div_on == FALSE) &&
2592 		(phase != BT_8723D_1ANT_PHASE_WLAN_OFF)) {
2593 		/* internal switch setting */
2594 
2595 		switch (ant_pos_type) {
2596 
2597 		case BTC_ANT_PATH_WIFI:
2598 			if (board_info->btdm_ant_pos ==
2599 			    BTC_ANTENNA_AT_MAIN_PORT)
2600 
2601 				btcoexist->btc_write_2byte(btcoexist,
2602 							   0x948, 0x0);
2603 			else
2604 				btcoexist->btc_write_2byte(btcoexist,
2605 							   0x948, 0x280);
2606 
2607 			break;
2608 		case BTC_ANT_PATH_BT:
2609 			if (board_info->btdm_ant_pos ==
2610 			    BTC_ANTENNA_AT_MAIN_PORT)
2611 				btcoexist->btc_write_2byte(btcoexist,
2612 							   0x948, 0x280);
2613 			else
2614 				btcoexist->btc_write_2byte(btcoexist,
2615 							   0x948, 0x0);
2616 
2617 			break;
2618 		default:
2619 		case BTC_ANT_PATH_PTA:
2620 			if (board_info->btdm_ant_pos ==
2621 			    BTC_ANTENNA_AT_MAIN_PORT)
2622 				btcoexist->btc_write_2byte(btcoexist,
2623 							   0x948, 0x200);
2624 			else
2625 				btcoexist->btc_write_2byte(btcoexist,
2626 							   0x948, 0x80);
2627 			break;
2628 		}
2629 	}
2630 
2631 
2632 #if BT_8723D_1ANT_COEX_DBG
2633 	u32tmp1 = halbtc8723d1ant_ltecoex_indirect_read_reg(btcoexist, 0x38);
2634 	u32tmp2 = halbtc8723d1ant_ltecoex_indirect_read_reg(btcoexist, 0x54);
2635 	u16tmp0 = btcoexist->btc_read_2byte(btcoexist, 0xaa);
2636 	u16tmp1 = btcoexist->btc_read_2byte(btcoexist, 0x948);
2637 	u8tmp1  = btcoexist->btc_read_1byte(btcoexist, 0x73);
2638 	u8tmp0 =  btcoexist->btc_read_1byte(btcoexist, 0x67);
2639 
2640 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2641 		    "[BTCoex], 0x67 = 0x%x, 0x948 = 0x%x, 0x73 = 0x%x(After Set Ant Pat)\n",
2642 		    u8tmp0, u16tmp1, u8tmp1);
2643 	BTC_TRACE(trace_buf);
2644 
2645 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2646 		    "[BTCoex], 0x38= 0x%x, 0x54= 0x%x, 0xaa = 0x%x(After Set Ant Path)\n",
2647 		    u32tmp1, u32tmp2, u16tmp0);
2648 	BTC_TRACE(trace_buf);
2649 #endif
2650 
2651 }
2652 
2653 static
halbtc8723d1ant_is_common_action(IN struct btc_coexist * btcoexist)2654 boolean halbtc8723d1ant_is_common_action(IN struct btc_coexist *btcoexist)
2655 {
2656 	boolean			common = FALSE, wifi_connected = FALSE, wifi_busy = FALSE;
2657 
2658 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
2659 			   &wifi_connected);
2660 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
2661 
2662 	if (!wifi_connected &&
2663 	    BT_8723D_1ANT_BT_STATUS_NON_CONNECTED_IDLE ==
2664 	    coex_dm->bt_status) {
2665 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2666 			"[BTCoex], Wifi non connected-idle + BT non connected-idle!!\n");
2667 		BTC_TRACE(trace_buf);
2668 		common = TRUE;
2669 	} else if (wifi_connected &&
2670 		   (BT_8723D_1ANT_BT_STATUS_NON_CONNECTED_IDLE ==
2671 		    coex_dm->bt_status)) {
2672 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2673 			"[BTCoex], Wifi connected + BT non connected-idle!!\n");
2674 		BTC_TRACE(trace_buf);
2675 		common = TRUE;
2676 	} else if (!wifi_connected &&
2677 		   (BT_8723D_1ANT_BT_STATUS_CONNECTED_IDLE ==
2678 		    coex_dm->bt_status)) {
2679 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2680 			"[BTCoex], Wifi non connected-idle + BT connected-idle!!\n");
2681 		BTC_TRACE(trace_buf);
2682 		common = TRUE;
2683 	} else if (wifi_connected &&
2684 		   (BT_8723D_1ANT_BT_STATUS_CONNECTED_IDLE ==
2685 		    coex_dm->bt_status)) {
2686 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2687 			    "[BTCoex], Wifi connected + BT connected-idle!!\n");
2688 		BTC_TRACE(trace_buf);
2689 		common = TRUE;
2690 	} else if (!wifi_connected &&
2691 		   (BT_8723D_1ANT_BT_STATUS_CONNECTED_IDLE !=
2692 		    coex_dm->bt_status)) {
2693 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2694 			    "[BTCoex], Wifi non connected-idle + BT Busy!!\n");
2695 		BTC_TRACE(trace_buf);
2696 		common = TRUE;
2697 	} else {
2698 		if (wifi_busy) {
2699 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2700 				"[BTCoex], Wifi Connected-Busy + BT Busy!!\n");
2701 			BTC_TRACE(trace_buf);
2702 		} else {
2703 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2704 				"[BTCoex], Wifi Connected-Idle + BT Busy!!\n");
2705 			BTC_TRACE(trace_buf);
2706 		}
2707 
2708 		common = FALSE;
2709 	}
2710 
2711 	return common;
2712 }
2713 
2714 
2715 /* *********************************************
2716  *
2717  *	Non-Software Coex Mechanism start
2718  *
2719  * ********************************************* */
2720 static
halbtc8723d1ant_action_bt_whql_test(IN struct btc_coexist * btcoexist)2721 void halbtc8723d1ant_action_bt_whql_test(IN struct btc_coexist *btcoexist)
2722 {
2723 	halbtc8723d1ant_set_ant_path(btcoexist, BTC_ANT_PATH_AUTO, NORMAL_EXEC,
2724 				     BT_8723D_1ANT_PHASE_2G_RUNTIME);
2725 	halbtc8723d1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
2726 	halbtc8723d1ant_ps_tdma(btcoexist, NORMAL_EXEC, FALSE, 8);
2727 }
2728 
2729 static
halbtc8723d1ant_action_bt_hs(IN struct btc_coexist * btcoexist)2730 void halbtc8723d1ant_action_bt_hs(IN struct btc_coexist *btcoexist)
2731 {
2732 	halbtc8723d1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 2);
2733 	halbtc8723d1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE, 5);
2734 }
2735 
2736 static
halbtc8723d1ant_action_bt_relink(IN struct btc_coexist * btcoexist)2737 void halbtc8723d1ant_action_bt_relink(IN struct btc_coexist *btcoexist)
2738 {
2739 	struct  btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
2740 
2741 	if ((!coex_sta->is_bt_multi_link && !bt_link_info->pan_exist) ||
2742 	    (bt_link_info->a2dp_exist && bt_link_info->hid_exist)) {
2743 		halbtc8723d1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
2744 		halbtc8723d1ant_ps_tdma(btcoexist, NORMAL_EXEC, FALSE, 8);
2745 	}
2746 }
2747 
2748 static
halbtc8723d1ant_action_bt_idle(IN struct btc_coexist * btcoexist)2749 void halbtc8723d1ant_action_bt_idle(IN struct btc_coexist *btcoexist)
2750 {
2751 	boolean wifi_busy = FALSE;
2752 
2753 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
2754 
2755 	if (!wifi_busy) {
2756 		halbtc8723d1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 3);
2757 		halbtc8723d1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE, 6);
2758 	} else {
2759 		/* if wl busy and not support mesh mode*/
2760 		if (!coex_sta->is_bt_mesh_ver) {
2761 			if (BT_8723D_1ANT_BT_STATUS_NON_CONNECTED_IDLE == coex_dm->bt_status) {
2762 				halbtc8723d1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 8);
2763 				halbtc8723d1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE, 34);
2764 			} else {
2765 				halbtc8723d1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 8);
2766 				halbtc8723d1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE, 32);
2767 			}
2768 		} else {
2769 		/*Support BT mesh*/
2770 			if (coex_sta->bt_mesh_on) {
2771 				halbtc8723d1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 3);
2772 				halbtc8723d1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE, 19);
2773 			} else {
2774 				halbtc8723d1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 3);
2775 				halbtc8723d1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE, 33);
2776 			}
2777 		}
2778 	}
2779 }
2780 
2781 static
halbtc8723d1ant_action_bt_inquiry(IN struct btc_coexist * btcoexist)2782 void halbtc8723d1ant_action_bt_inquiry(IN struct btc_coexist *btcoexist)
2783 {
2784 	struct	btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
2785 	boolean wifi_connected = FALSE, wifi_busy = FALSE, bt_busy = FALSE;
2786 	boolean wifi_scan = FALSE, wifi_link = FALSE, wifi_roam = FALSE;
2787 
2788 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
2789 			   &wifi_connected);
2790 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
2791 	btcoexist->btc_set(btcoexist, BTC_SET_BL_BT_TRAFFIC_BUSY, &bt_busy);
2792 
2793 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &wifi_scan);
2794 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &wifi_link);
2795 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &wifi_roam);
2796 
2797 	if (coex_sta->bt_create_connection &&
2798 	    (coex_sta->wifi_is_high_pri_task || coex_sta->wifi_in_scan_task)) {
2799 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2800 			"[BTCoex], Wifi Scan/hi-pri-task + BT Inq/Page!!\n");
2801 		BTC_TRACE(trace_buf);
2802 
2803 		if (bt_link_info->a2dp_exist && !bt_link_info->pan_exist) {
2804 			halbtc8723d1ant_coex_table_with_type(btcoexist,
2805 							     NORMAL_EXEC, 1);
2806 			halbtc8723d1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE, 17);
2807 		} else if (coex_sta->wifi_is_high_pri_task) {
2808 			halbtc8723d1ant_coex_table_with_type(btcoexist,
2809 							     NORMAL_EXEC, 16);
2810 			halbtc8723d1ant_ps_tdma(btcoexist,
2811 						NORMAL_EXEC, TRUE, 36);
2812 		} else if (coex_sta->wifi_in_scan_task) {
2813 			halbtc8723d1ant_coex_table_with_type(btcoexist,
2814 							     NORMAL_EXEC, 1);
2815 			halbtc8723d1ant_ps_tdma(btcoexist,
2816 						NORMAL_EXEC, TRUE, 33);
2817 		}
2818 	} else if (!wifi_connected && !wifi_scan) {
2819 		halbtc8723d1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
2820 
2821 		halbtc8723d1ant_ps_tdma(btcoexist, NORMAL_EXEC, FALSE, 8);
2822 	} else if (bt_link_info->pan_exist) {
2823 		halbtc8723d1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
2824 
2825 		halbtc8723d1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE, 22);
2826 	} else if (bt_link_info->a2dp_exist) {
2827 		halbtc8723d1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
2828 
2829 		halbtc8723d1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE, 16);
2830 	} else {
2831 		halbtc8723d1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
2832 
2833 		if (coex_sta->wifi_in_scan_task ||
2834 		    coex_sta->wifi_is_high_pri_task)
2835 			halbtc8723d1ant_ps_tdma(btcoexist,
2836 						NORMAL_EXEC, TRUE, 21);
2837 		else
2838 			halbtc8723d1ant_ps_tdma(btcoexist,
2839 						NORMAL_EXEC, TRUE, 23);
2840 	}
2841 }
2842 
2843 static
halbtc8723d1ant_action_bt_sco_hid_only_busy(IN struct btc_coexist * btcoexist)2844 void halbtc8723d1ant_action_bt_sco_hid_only_busy(IN struct btc_coexist
2845 		*btcoexist)
2846 {
2847 	struct  btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
2848 	boolean	wifi_connected = FALSE, wifi_busy = FALSE, wifi_cckdeadlock_ap = FALSE;
2849 	u32  wifi_bw = 1;
2850 	u8	iot_peer = BTC_IOT_PEER_UNKNOWN;
2851 
2852 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED, &wifi_connected);
2853 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
2854 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
2855 	btcoexist->btc_get(btcoexist, BTC_GET_U1_IOT_PEER, &iot_peer);
2856 
2857 	if ((iot_peer == BTC_IOT_PEER_ATHEROS) && (coex_sta->cck_lock_ever))
2858 		wifi_cckdeadlock_ap = TRUE;
2859 
2860 	if (bt_link_info->sco_exist) {
2861 		if (coex_sta->is_bt_multi_link) {
2862 			if (coex_sta->specific_pkt_period_cnt > 0)
2863 				halbtc8723d1ant_coex_table_with_type(btcoexist,
2864 								     FALSE,
2865 								     15);
2866 			else
2867 				halbtc8723d1ant_coex_table_with_type(btcoexist,
2868 								     FALSE,
2869 								     3);
2870 			halbtc8723d1ant_ps_tdma(btcoexist,
2871 						NORMAL_EXEC, TRUE, 25);
2872 		} else {
2873 			halbtc8723d1ant_coex_table_with_type(btcoexist,
2874 							     NORMAL_EXEC, 1);
2875 			halbtc8723d1ant_ps_tdma(btcoexist,
2876 						NORMAL_EXEC, TRUE, 5);
2877 		}
2878 	} else if (coex_sta->is_hid_rcu) {
2879 		halbtc8723d1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 3);
2880 
2881 		if (wifi_busy)
2882 			halbtc8723d1ant_ps_tdma(btcoexist,
2883 						NORMAL_EXEC, TRUE, 36);
2884 		else
2885 			halbtc8723d1ant_ps_tdma(btcoexist,
2886 						NORMAL_EXEC, TRUE, 6);
2887 	} else {
2888 
2889 		if (wifi_cckdeadlock_ap &&
2890 		    coex_sta->is_hid_low_pri_tx_overhead) {
2891 			if (coex_sta->hid_busy_num < 2)
2892 				halbtc8723d1ant_coex_table_with_type(btcoexist,
2893 								     FALSE,
2894 								     14);
2895 			else
2896 				halbtc8723d1ant_coex_table_with_type(btcoexist,
2897 								     FALSE,
2898 								     13);
2899 			halbtc8723d1ant_ps_tdma(btcoexist,
2900 						NORMAL_EXEC, TRUE, 18);
2901 		} else if (coex_sta->is_hid_low_pri_tx_overhead) {
2902 			if (coex_sta->hid_busy_num < 2)
2903 				halbtc8723d1ant_coex_table_with_type(btcoexist,
2904 								     FALSE,
2905 								     3);
2906 			else
2907 				halbtc8723d1ant_coex_table_with_type(btcoexist,
2908 								     FALSE,
2909 								     6);
2910 			halbtc8723d1ant_ps_tdma(btcoexist,
2911 						NORMAL_EXEC, TRUE, 18);
2912 		} else if (coex_sta->hid_busy_num < 2) {
2913 			halbtc8723d1ant_coex_table_with_type(btcoexist,
2914 							     NORMAL_EXEC, 3);
2915 			halbtc8723d1ant_ps_tdma(btcoexist,
2916 						NORMAL_EXEC, TRUE, 11);
2917 		} else if (wifi_bw == 0) { /* if 11bg mode */
2918 			halbtc8723d1ant_coex_table_with_type(btcoexist,
2919 							     NORMAL_EXEC, 11);
2920 			halbtc8723d1ant_ps_tdma(btcoexist,
2921 						NORMAL_EXEC, TRUE, 11);
2922 		} else {
2923 			halbtc8723d1ant_coex_table_with_type(btcoexist,
2924 							     NORMAL_EXEC, 6);
2925 			halbtc8723d1ant_ps_tdma(btcoexist,
2926 						NORMAL_EXEC, TRUE, 11);
2927 		}
2928 	}
2929 }
2930 
2931 static
halbtc8723d1ant_action_wifi_only(IN struct btc_coexist * btcoexist)2932 void halbtc8723d1ant_action_wifi_only(IN struct btc_coexist *btcoexist)
2933 {
2934 	halbtc8723d1ant_set_ant_path(btcoexist, BTC_ANT_PATH_AUTO, FORCE_EXEC,
2935 				     BT_8723D_1ANT_PHASE_2G_RUNTIME);
2936 	halbtc8723d1ant_coex_table_with_type(btcoexist, FORCE_EXEC, 10);
2937 	halbtc8723d1ant_ps_tdma(btcoexist, FORCE_EXEC, FALSE, 8);
2938 }
2939 
2940 static
halbtc8723d1ant_action_wifi_native_lps(IN struct btc_coexist * btcoexist)2941 void halbtc8723d1ant_action_wifi_native_lps(IN struct btc_coexist *btcoexist)
2942 {
2943 	struct  btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
2944 
2945 	if (bt_link_info->pan_exist)
2946 		halbtc8723d1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 5);
2947 	else
2948 		halbtc8723d1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
2949 	halbtc8723d1ant_ps_tdma(btcoexist, NORMAL_EXEC, FALSE, 8);
2950 }
2951 
2952 static
halbtc8723d1ant_action_wifi_multi_port(IN struct btc_coexist * btcoexist)2953 void halbtc8723d1ant_action_wifi_multi_port(IN struct btc_coexist *btcoexist)
2954 {
2955 	struct  btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
2956 	struct btc_wifi_link_info *link_info = &btcoexist->wifi_link_info;
2957 	u8 multi_port_type;
2958 	u32 traffic_dir, wifi_link_status;
2959 	static u8	prewifi_rssi_state = BTC_RSSI_STATE_LOW;
2960 
2961 	halbtc8723d1ant_set_ant_path(btcoexist, BTC_ANT_PATH_AUTO, NORMAL_EXEC,
2962 				     BT_8723D_1ANT_PHASE_2G_RUNTIME);
2963 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_TRAFFIC_DIRECTION,
2964 			   &traffic_dir);
2965 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
2966 			   &wifi_link_status);
2967 
2968 	if (coex_sta->bt_disabled) {
2969 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2970 			    "[BTCoex], %s(): BT Disable!!\n", __func__);
2971 		BTC_TRACE(trace_buf);
2972 
2973 		halbtc8723d1ant_coex_table_with_type(btcoexist, FALSE, 10);
2974 		halbtc8723d1ant_ps_tdma(btcoexist, NORMAL_EXEC, FALSE, 8);
2975 	} else if (coex_sta->is_setup_link) {
2976 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2977 			    "[BTCoex], %s(): BT Relink!!\n", __func__);
2978 		BTC_TRACE(trace_buf);
2979 
2980 		halbtc8723d1ant_coex_table_with_type(btcoexist, FALSE, 0);
2981 		halbtc8723d1ant_ps_tdma(btcoexist, NORMAL_EXEC, FALSE, 8);
2982 	} else if (coex_sta->c2h_bt_inquiry_page) {
2983 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2984 			    "[BTCoex], %s(): BT Inq-Page!!\n", __func__);
2985 		BTC_TRACE(trace_buf);
2986 
2987 		halbtc8723d1ant_coex_table_with_type(btcoexist, FALSE, 18);
2988 		halbtc8723d1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE, 5);
2989 	 } else if (coex_sta->num_of_profile == 0) {
2990 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
2991 			    "[BTCoex], %s(): BT idle!!\n", __func__);
2992 		BTC_TRACE(trace_buf);
2993 
2994 		if (btcoexist->chip_interface == BTC_INTF_PCI &&
2995 			(link_info->link_mode == BTC_LINK_ONLY_GO ||
2996 			 link_info->link_mode == BTC_LINK_ONLY_GC))
2997 			halbtc8723d1ant_coex_table_with_type(btcoexist, FALSE,
2998 							     10);
2999 		else
3000 			halbtc8723d1ant_coex_table_with_type(btcoexist, FALSE,
3001 							     17);
3002 		halbtc8723d1ant_ps_tdma(btcoexist, NORMAL_EXEC, FALSE, 8);
3003 	} else {
3004 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3005 			    "[BTCoex], %s(): BT busy!!\n", __func__);
3006 		BTC_TRACE(trace_buf);
3007 
3008 		switch (link_info->link_mode) {
3009 		case BTC_LINK_ONLY_GO:
3010 		case BTC_LINK_ONLY_GC:
3011 			if (btcoexist->chip_interface == BTC_INTF_PCI &&
3012 			    bt_link_info->a2dp_exist && !coex_sta->is_bt_multi_link)
3013 				halbtc8723d1ant_coex_table_with_type(btcoexist,
3014 								FALSE, 10);
3015 			else
3016 				halbtc8723d1ant_coex_table_with_type(btcoexist,
3017 								FALSE, 17);
3018 
3019 			halbtc8723d1ant_ps_tdma(btcoexist, NORMAL_EXEC, FALSE, 8);
3020 			break;
3021 		default:
3022 			halbtc8723d1ant_coex_table_with_type(btcoexist, FALSE, 17);
3023 			halbtc8723d1ant_ps_tdma(btcoexist, NORMAL_EXEC, FALSE, 8);
3024 			break;
3025 		}
3026 	}
3027 }
3028 
3029 static
halbtc8723d1ant_action_wifi_linkscan_process(IN struct btc_coexist * btcoexist)3030 void halbtc8723d1ant_action_wifi_linkscan_process(IN struct btc_coexist
3031 		*btcoexist)
3032 {
3033 	struct  btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
3034 
3035 	halbtc8723d1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
3036 
3037 	if (bt_link_info->pan_exist)
3038 		halbtc8723d1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE, 22);
3039 	else if (bt_link_info->a2dp_exist)
3040 		halbtc8723d1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE, 27);
3041 	else
3042 		halbtc8723d1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE, 21);
3043 }
3044 
3045 static
halbtc8723d1ant_action_wifi_connected_bt_acl_busy(IN struct btc_coexist * btcoexist)3046 void halbtc8723d1ant_action_wifi_connected_bt_acl_busy(IN struct btc_coexist
3047 		*btcoexist)
3048 {
3049 	struct	btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
3050 	boolean wifi_busy = FALSE, wifi_turbo = FALSE, wifi_cckdeadlock_ap = FALSE;
3051 	u32	wifi_bw = 1;
3052 	u8	iot_peer = BTC_IOT_PEER_UNKNOWN, table_case, tdma_case;
3053 
3054 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
3055 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY, &wifi_busy);
3056 	btcoexist->btc_get(btcoexist, BTC_GET_U1_AP_NUM,
3057 			   &coex_sta->scan_ap_num);
3058 	btcoexist->btc_get(btcoexist, BTC_GET_U1_IOT_PEER, &iot_peer);
3059 
3060 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3061 		    "############# [BTCoex],  scan_ap_num = %d, wl_noisy_level = %d\n",
3062 		    coex_sta->scan_ap_num, coex_sta->wl_noisy_level);
3063 	BTC_TRACE(trace_buf);
3064 
3065 	if (wifi_busy && coex_sta->wl_noisy_level == 0)
3066 		wifi_turbo = TRUE;
3067 
3068 	if (coex_sta->cck_lock_ever)
3069 		wifi_cckdeadlock_ap = TRUE;
3070 
3071 	if (bt_link_info->a2dp_exist && coex_sta->is_bt_a2dp_sink) {
3072 		if (wifi_cckdeadlock_ap) {
3073 			table_case = 13;
3074 			tdma_case = 15;
3075 		}
3076 		else if(wifi_busy) {
3077 			table_case = 3;
3078 			tdma_case = 15;
3079 		}
3080 		else {
3081 			table_case = 3;
3082 			tdma_case = 58;
3083 		}
3084 		halbtc8723d1ant_coex_table_with_type(btcoexist,
3085 						     NORMAL_EXEC, table_case);
3086 		halbtc8723d1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE, tdma_case);
3087 	} else if (bt_link_info->a2dp_only) { /* A2DP		 */
3088 
3089 		if (wifi_cckdeadlock_ap)
3090 			table_case = 13;
3091 		else if (wifi_turbo)
3092 			table_case = 8;
3093 		else
3094 			table_case = 4;
3095 
3096 		halbtc8723d1ant_coex_table_with_type(btcoexist,
3097 						     NORMAL_EXEC, table_case);
3098 
3099 		if (coex_sta->connect_ap_period_cnt > 0)
3100 			halbtc8723d1ant_ps_tdma(btcoexist,
3101 						NORMAL_EXEC, TRUE, 26);
3102 		else
3103 			halbtc8723d1ant_ps_tdma(btcoexist,
3104 						NORMAL_EXEC, TRUE, 7);
3105 
3106 	} else if ((bt_link_info->a2dp_exist &&
3107 			bt_link_info->pan_exist) ||
3108 			(bt_link_info->hid_exist && bt_link_info->a2dp_exist &&
3109 			bt_link_info->pan_exist)) {
3110 		/* A2DP+PAN(OPP,FTP), HID+A2DP+PAN(OPP,FTP) */
3111 		if (wifi_cckdeadlock_ap) {
3112 			if (bt_link_info->hid_exist &&
3113 			    coex_sta->hid_busy_num < 2)
3114 				table_case = 14;
3115 			else
3116 				table_case = 13;
3117 		} else if (bt_link_info->hid_exist) {
3118 			if (coex_sta->hid_busy_num < 2)
3119 				table_case = 3;
3120 			else
3121 				table_case = 1;
3122 		} else if (wifi_turbo) {
3123 			table_case = 8;
3124 		} else {
3125 			table_case = 4;
3126 		}
3127 
3128 		halbtc8723d1ant_coex_table_with_type(btcoexist, NORMAL_EXEC,
3129 						     table_case);
3130 		if (wifi_busy)
3131 			halbtc8723d1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE,
3132 						13);
3133 		else
3134 			halbtc8723d1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE,
3135 						14);
3136 	} else if (bt_link_info->hid_exist &&
3137 		   bt_link_info->a2dp_exist) { /* HID+A2DP */
3138 
3139 		if (wifi_cckdeadlock_ap) {
3140 			if (coex_sta->hid_busy_num < 2)
3141 				table_case = 14;
3142 			else
3143 				table_case = 13;
3144 
3145 			halbtc8723d1ant_coex_table_with_type(btcoexist,
3146 							     NORMAL_EXEC,
3147 							     table_case);
3148 
3149 			if (coex_sta->hid_pair_cnt > 1)
3150 				halbtc8723d1ant_ps_tdma(btcoexist,
3151 							NORMAL_EXEC, TRUE, 24);
3152 			else
3153 				halbtc8723d1ant_ps_tdma(btcoexist,
3154 							NORMAL_EXEC, TRUE, 8);
3155 		} else {
3156 			if (coex_sta->hid_busy_num < 2) /* 2/18 HID */
3157 				table_case = 3;
3158 			else if (wifi_bw == 0)/* if 11bg mode */
3159 				table_case = 12;
3160 			else
3161 				table_case = 1;
3162 
3163 			halbtc8723d1ant_coex_table_with_type(btcoexist,
3164 							     NORMAL_EXEC,
3165 							     table_case);
3166 
3167 			if (coex_sta->hid_pair_cnt > 1)
3168 				halbtc8723d1ant_ps_tdma(btcoexist,
3169 							NORMAL_EXEC, TRUE, 24);
3170 			else
3171 				halbtc8723d1ant_ps_tdma(btcoexist,
3172 							NORMAL_EXEC, TRUE, 8);
3173 		}
3174 	} else if (bt_link_info->pan_only
3175 		   || (bt_link_info->hid_exist && bt_link_info->pan_exist)) {
3176 			/* PAN(OPP,FTP), HID+PAN(OPP,FTP) */
3177 
3178 		if (coex_sta->cck_lock_ever) {
3179 			if (bt_link_info->hid_exist &&
3180 			    coex_sta->hid_busy_num < 2)
3181 				table_case = 14;
3182 			else
3183 				table_case = 13;
3184 		} else if (bt_link_info->hid_exist) {
3185 			if (coex_sta->hid_busy_num < 2)
3186 				table_case = 3;
3187 			else
3188 				table_case = 1;
3189 		} else if (wifi_turbo) {
3190 			table_case = 8;
3191 		} else {
3192 			table_case = 4;
3193 		}
3194 
3195 		halbtc8723d1ant_coex_table_with_type(btcoexist,
3196 						     NORMAL_EXEC, table_case);
3197 		if (!wifi_busy)
3198 			halbtc8723d1ant_ps_tdma(btcoexist,
3199 						NORMAL_EXEC, TRUE, 4);
3200 		else
3201 			halbtc8723d1ant_ps_tdma(btcoexist,
3202 						NORMAL_EXEC, TRUE, 3);
3203 	} else {
3204 		/* BT no-profile busy (0x9) */
3205 		halbtc8723d1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 4);
3206 		halbtc8723d1ant_ps_tdma(btcoexist, NORMAL_EXEC, TRUE, 33);
3207 	}
3208 
3209 }
3210 
3211 static
halbtc8723d1ant_action_wifi_not_connected(IN struct btc_coexist * btcoexist)3212 void halbtc8723d1ant_action_wifi_not_connected(IN struct btc_coexist *btcoexist)
3213 {
3214 	halbtc8723d1ant_set_ant_path(btcoexist, BTC_ANT_PATH_AUTO, NORMAL_EXEC,
3215 				     BT_8723D_1ANT_PHASE_2G_RUNTIME);
3216 	/* tdma and coex table */
3217 	halbtc8723d1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
3218 	halbtc8723d1ant_ps_tdma(btcoexist, FORCE_EXEC, FALSE, 8);
3219 }
3220 
3221 static
halbtc8723d1ant_action_wifi_connected(IN struct btc_coexist * btcoexist)3222 void halbtc8723d1ant_action_wifi_connected(IN struct btc_coexist *btcoexist)
3223 {
3224 	struct	btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
3225 	boolean wifi_busy = FALSE;
3226 
3227 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3228 			"[BTCoex], CoexForWifiConnect()===>\n");
3229 	BTC_TRACE(trace_buf);
3230 
3231 	halbtc8723d1ant_set_ant_path(btcoexist, BTC_ANT_PATH_AUTO,
3232 					 NORMAL_EXEC,
3233 					 BT_8723D_1ANT_PHASE_2G_RUNTIME);
3234 
3235 	if ((coex_dm->bt_status == BT_8723D_1ANT_BT_STATUS_ACL_BUSY) ||
3236 		(coex_dm->bt_status == BT_8723D_1ANT_BT_STATUS_ACL_SCO_BUSY)) {
3237 
3238 		if (bt_link_info->hid_only)  /* HID only */
3239 		   halbtc8723d1ant_action_bt_sco_hid_only_busy(btcoexist);
3240 		else
3241 			halbtc8723d1ant_action_wifi_connected_bt_acl_busy(btcoexist);
3242 
3243 	} else if (coex_dm->bt_status == BT_8723D_1ANT_BT_STATUS_SCO_BUSY)
3244 		halbtc8723d1ant_action_bt_sco_hid_only_busy(btcoexist);
3245 	else
3246 		halbtc8723d1ant_action_bt_idle(btcoexist);
3247 }
3248 
3249 static
halbtc8723d1ant_run_sw_coexist_mechanism(IN struct btc_coexist * btcoexist)3250 void halbtc8723d1ant_run_sw_coexist_mechanism(IN struct btc_coexist *btcoexist)
3251 {
3252 	u8				algorithm = 0;
3253 
3254 	algorithm = halbtc8723d1ant_action_algorithm(btcoexist);
3255 	coex_dm->cur_algorithm = algorithm;
3256 
3257 	if (halbtc8723d1ant_is_common_action(btcoexist)) {
3258 
3259 	} else {
3260 		switch (coex_dm->cur_algorithm) {
3261 		case BT_8723D_1ANT_COEX_ALGO_SCO:
3262 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3263 				    "[BTCoex], Action algorithm = SCO.\n");
3264 			BTC_TRACE(trace_buf);
3265 			break;
3266 		case BT_8723D_1ANT_COEX_ALGO_HID:
3267 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3268 				    "[BTCoex], Action algorithm = HID.\n");
3269 			BTC_TRACE(trace_buf);
3270 			break;
3271 		case BT_8723D_1ANT_COEX_ALGO_A2DP:
3272 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3273 				    "[BTCoex], Action algorithm = A2DP.\n");
3274 			BTC_TRACE(trace_buf);
3275 			break;
3276 		case BT_8723D_1ANT_COEX_ALGO_A2DP_PANHS:
3277 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3278 				    "[BTCoex], Action algorithm = A2DP+PAN(HS).\n");
3279 			BTC_TRACE(trace_buf);
3280 			break;
3281 		case BT_8723D_1ANT_COEX_ALGO_PANEDR:
3282 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3283 				    "[BTCoex], Action algorithm = PAN(EDR).\n");
3284 			BTC_TRACE(trace_buf);
3285 			break;
3286 		case BT_8723D_1ANT_COEX_ALGO_PANHS:
3287 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3288 				    "[BTCoex], Action algorithm = HS mode.\n");
3289 			BTC_TRACE(trace_buf);
3290 			break;
3291 		case BT_8723D_1ANT_COEX_ALGO_PANEDR_A2DP:
3292 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3293 				    "[BTCoex], Action algorithm = PAN+A2DP.\n");
3294 			BTC_TRACE(trace_buf);
3295 			break;
3296 		case BT_8723D_1ANT_COEX_ALGO_PANEDR_HID:
3297 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3298 				    "[BTCoex], Action algorithm = PAN(EDR)+HID.\n");
3299 			BTC_TRACE(trace_buf);
3300 			break;
3301 		case BT_8723D_1ANT_COEX_ALGO_HID_A2DP_PANEDR:
3302 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3303 				    "[BTCoex], Action algorithm = HID+A2DP+PAN.\n");
3304 			BTC_TRACE(trace_buf);
3305 			break;
3306 		case BT_8723D_1ANT_COEX_ALGO_HID_A2DP:
3307 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3308 				    "[BTCoex], Action algorithm = HID+A2DP.\n");
3309 			BTC_TRACE(trace_buf);
3310 			break;
3311 		default:
3312 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3313 				    "[BTCoex], Action algorithm = coexist All Off!!\n");
3314 			BTC_TRACE(trace_buf);
3315 			break;
3316 		}
3317 		coex_dm->pre_algorithm = coex_dm->cur_algorithm;
3318 	}
3319 }
3320 
3321 static
halbtc8723d1ant_run_coexist_mechanism(IN struct btc_coexist * btcoexist)3322 void halbtc8723d1ant_run_coexist_mechanism(IN struct btc_coexist *btcoexist)
3323 {
3324 	struct	btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
3325 	struct	btc_wifi_link_info linfo;
3326 	boolean wifi_connected = FALSE, bt_hs_on = FALSE;
3327 	boolean increase_scan_dev_num = FALSE;
3328 	boolean bt_ctrl_agg_buf_size = FALSE;
3329 	boolean miracast_plus_bt = FALSE, wifi_under_5g = FALSE;
3330 	u8	agg_buf_size = 5, iot_peer = BTC_IOT_PEER_UNKNOWN;
3331 	u32	wifi_link_status = 0, num_of_wifi_link = 0, wifi_bw;
3332 	boolean scan = FALSE, link = FALSE, roam = FALSE, under_4way = FALSE;
3333 
3334 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &scan);
3335 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &link);
3336 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &roam);
3337 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_4_WAY_PROGRESS,
3338 			   &under_4way);
3339 	btcoexist->btc_set(btcoexist, BTC_SET_BL_INC_SCAN_DEV_NUM,
3340 				   &increase_scan_dev_num);
3341 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK_INFO, &linfo);
3342 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_LINK_STATUS,
3343 				   &wifi_link_status);
3344 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_BW, &wifi_bw);
3345 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
3346 			   &wifi_connected);
3347 	num_of_wifi_link = wifi_link_status >> 16;
3348 	btcoexist->wifi_link_info = linfo;
3349 
3350 	/* coex-276  P2P-Go beacon request can't release issue
3351 	* Only PCIe can set 0x454[6] = 1 to solve this issue,
3352 	* WL SDIO/USB interface need driver support.
3353 	*/
3354 #ifdef PLATFORM_WINDOWS
3355 	if (btcoexist->chip_interface != BTC_INTF_SDIO)
3356 #else
3357 	if (btcoexist->chip_interface == BTC_INTF_PCI)
3358 #endif
3359 		btcoexist->btc_write_1byte_bitmask(btcoexist, REG_CCK_CHECK,
3360 					     BIT_EN_BCN_PKT_REL, 0x1);
3361 	else
3362 		btcoexist->btc_write_1byte_bitmask(btcoexist, REG_CCK_CHECK,
3363 					     BIT_EN_BCN_PKT_REL, 0x0);
3364 
3365 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3366 			"[BTCoex], RunCoexistMechanism()===>\n");
3367 	BTC_TRACE(trace_buf);
3368 
3369 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3370 			"[BTCoex], under_lps = %d, force_lps_ctrl = %d, acl_busy = %d!!!\n",
3371 			coex_sta->under_lps, coex_sta->force_lps_ctrl, coex_sta->acl_busy);
3372 	BTC_TRACE(trace_buf);
3373 
3374 	if (btcoexist->manual_control) {
3375 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3376 			"[BTCoex], RunCoexistMechanism(), return for Manual CTRL <===\n");
3377 		BTC_TRACE(trace_buf);
3378 		return;
3379 	}
3380 
3381 	if (btcoexist->stop_coex_dm) {
3382 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3383 			"[BTCoex], RunCoexistMechanism(), return for Stop Coex DM <===\n");
3384 		BTC_TRACE(trace_buf);
3385 		return;
3386 	}
3387 
3388 	if (coex_sta->under_ips) {
3389 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3390 				"[BTCoex], wifi is under IPS !!!\n");
3391 		BTC_TRACE(trace_buf);
3392 		return;
3393 	}
3394 
3395 	if (!coex_sta->run_time_state) {
3396 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3397 			"[BTCoex], return for run_time_state = FALSE !!!\n");
3398 		BTC_TRACE(trace_buf);
3399 		return;
3400 	}
3401 
3402 	if (coex_sta->freeze_coexrun_by_btinfo && !coex_sta->is_setup_link) {
3403 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3404 			"[BTCoex], return for freeze_coexrun_by_btinfo\n");
3405 		BTC_TRACE(trace_buf);
3406 		return;
3407 	}
3408 
3409 	if (coex_sta->under_lps && !coex_sta->force_lps_ctrl) {
3410 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3411 				"[BTCoex], RunCoexistMechanism(), wifi is under LPS !!!\n");
3412 		BTC_TRACE(trace_buf);
3413 		halbtc8723d1ant_action_wifi_native_lps(btcoexist);
3414 		return;
3415 	}
3416 
3417 	if (coex_sta->bt_whck_test) {
3418 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3419 				"[BTCoex], BT is under WHCK TEST!!!\n");
3420 		BTC_TRACE(trace_buf);
3421 		halbtc8723d1ant_action_bt_whql_test(btcoexist);
3422 		return;
3423 	}
3424 
3425 	if (coex_sta->bt_disabled) {
3426 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3427 				"[BTCoex], BT is disabled !!!\n");
3428 		halbtc8723d1ant_action_wifi_only(btcoexist);
3429 		return;
3430 	}
3431 
3432 	if (coex_sta->is_setup_link || coex_sta->bt_relink_downcount != 0) {
3433 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3434 			"[BTCoex], BT is re-link !!!\n");
3435 		halbtc8723d1ant_action_bt_relink(btcoexist);
3436 		return;
3437 	}
3438 
3439 	if (coex_sta->c2h_bt_inquiry_page) {
3440 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3441 			    "[BTCoex], BT is under inquiry/page scan !!\n");
3442 		BTC_TRACE(trace_buf);
3443 		halbtc8723d1ant_action_bt_inquiry(btcoexist);
3444 		return;
3445 	}
3446 
3447 	if ((BT_8723D_1ANT_BT_STATUS_ACL_BUSY == coex_dm->bt_status) ||
3448 		(BT_8723D_1ANT_BT_STATUS_SCO_BUSY == coex_dm->bt_status) ||
3449 		(BT_8723D_1ANT_BT_STATUS_ACL_SCO_BUSY == coex_dm->bt_status))
3450 		increase_scan_dev_num = TRUE;
3451 
3452 	if ((num_of_wifi_link >= 2) ||
3453 		(wifi_link_status & WIFI_P2P_GO_CONNECTED) ||
3454 		(linfo.link_mode == BTC_LINK_ONLY_GO) ||
3455 		(linfo.link_mode == BTC_LINK_ONLY_GC) ) {
3456 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3457 			"############# [BTCoex],  Multi-Port num_of_wifi_link = %d, wifi_link_status = 0x%x\n",
3458 				num_of_wifi_link, wifi_link_status);
3459 		BTC_TRACE(trace_buf);
3460 
3461 		if (scan || link || roam || under_4way) {
3462 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3463 				"[BTCoex], scan = %d, link = %d, roam = %d 4way = %d!!!\n",
3464 				    scan, link, roam, under_4way);
3465 			BTC_TRACE(trace_buf);
3466 
3467 			if (bt_link_info->bt_link_exist)
3468 				miracast_plus_bt = TRUE;
3469 			else
3470 				miracast_plus_bt = FALSE;
3471 
3472 			btcoexist->btc_set(btcoexist, BTC_SET_BL_MIRACAST_PLUS_BT,
3473 				   &miracast_plus_bt);
3474 
3475 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3476 				"[BTCoex], wifi is under linkscan process + Multi-Port !!\n");
3477 			BTC_TRACE(trace_buf);
3478 
3479 			halbtc8723d1ant_action_wifi_linkscan_process(btcoexist);
3480 		} else
3481 
3482 			halbtc8723d1ant_action_wifi_multi_port(btcoexist);
3483 
3484 		return;
3485 	}
3486 
3487 #if 0
3488 	if ((bt_link_info->bt_link_exist) && (wifi_connected)) {
3489 
3490 		btcoexist->btc_get(btcoexist, BTC_GET_U1_IOT_PEER, &iot_peer);
3491 
3492 		if (BTC_IOT_PEER_CISCO == iot_peer) {
3493 
3494 			if (BTC_WIFI_BW_HT40 == wifi_bw)
3495 				halbtc8723d1ant_limited_rx(btcoexist,
3496 						NORMAL_EXEC, FALSE, TRUE, 0x10);
3497 			else
3498 				halbtc8723d1ant_limited_rx(btcoexist,
3499 						NORMAL_EXEC, FALSE, TRUE, 0x8);
3500 			}
3501 	}
3502 #endif
3503 	halbtc8723d1ant_run_sw_coexist_mechanism(
3504 			btcoexist);  /* just print debug message */
3505 
3506 	btcoexist->btc_get(btcoexist, BTC_GET_BL_HS_OPERATION, &bt_hs_on);
3507 
3508 	if (bt_hs_on) {
3509 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3510 				"############# [BTCoex],  BT Is hs\n");
3511 		BTC_TRACE(trace_buf);
3512 		halbtc8723d1ant_action_bt_hs(btcoexist);
3513 		return;
3514 	}
3515 
3516 	if ((coex_dm->bt_status == BT_8723D_1ANT_BT_STATUS_NON_CONNECTED_IDLE ||
3517 	     coex_dm->bt_status == BT_8723D_1ANT_BT_STATUS_CONNECTED_IDLE)
3518 	     && wifi_connected) {
3519 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3520 				"############# [BTCoex],  BT Is idle\n");
3521 		BTC_TRACE(trace_buf);
3522 		halbtc8723d1ant_action_bt_idle(btcoexist);
3523 		return;
3524 	}
3525 
3526 	if (scan || link || roam || under_4way) {
3527 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3528 		   "[BTCoex], scan = %d, link = %d, roam = %d 4way = %d!!!\n",
3529 					scan, link, roam, under_4way);
3530 		BTC_TRACE(trace_buf);
3531 
3532 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3533 		   "[BTCoex], wifi is under linkscan process!!\n");
3534 		BTC_TRACE(trace_buf);
3535 
3536 		halbtc8723d1ant_action_wifi_linkscan_process(btcoexist);
3537 	} else if (wifi_connected) {
3538 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3539 		   "[BTCoex], wifi is under connected!!\n");
3540 		BTC_TRACE(trace_buf);
3541 
3542 		halbtc8723d1ant_action_wifi_connected(btcoexist);
3543 	} else {
3544 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3545 		   "[BTCoex], wifi is under not-connected!!\n");
3546 		BTC_TRACE(trace_buf);
3547 
3548 		halbtc8723d1ant_action_wifi_not_connected(btcoexist);
3549 	 }
3550 }
3551 
3552 static
halbtc8723d1ant_init_coex_dm(IN struct btc_coexist * btcoexist)3553 void halbtc8723d1ant_init_coex_dm(IN struct btc_coexist *btcoexist)
3554 {
3555 	/* force to reset coex mechanism */
3556 	halbtc8723d1ant_low_penalty_ra(btcoexist, NORMAL_EXEC, FALSE);
3557 
3558 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3559 		    "[BTCoex], Coex Mechanism Init!!\n");
3560 	BTC_TRACE(trace_buf);
3561 
3562 	coex_sta->pop_event_cnt = 0;
3563 	coex_sta->cnt_remotenamereq = 0;
3564 	coex_sta->cnt_reinit = 0;
3565 	coex_sta->cnt_setuplink = 0;
3566 	coex_sta->cnt_ignwlanact = 0;
3567 	coex_sta->cnt_page = 0;
3568 	coex_sta->cnt_roleswitch = 0;
3569 
3570 	halbtc8723d1ant_query_bt_info(btcoexist);
3571 }
3572 
3573 static
halbtc8723d1ant_init_hw_config(IN struct btc_coexist * btcoexist,IN boolean back_up,IN boolean wifi_only)3574 void halbtc8723d1ant_init_hw_config(IN struct btc_coexist *btcoexist,
3575 				    IN boolean back_up, IN boolean wifi_only)
3576 {
3577 	u32			u32tmp1 = 0, u32tmp2 = 0;
3578 	u16			u16tmp1 = 0;
3579 	u8	u8tmp0 = 0, u8tmp1 = 0;
3580 	struct  btc_board_info *board_info = &btcoexist->board_info;
3581 	u8 i = 0;
3582 	boolean tmp;
3583 
3584 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3585 		    "[BTCoex], 1Ant Init HW Config!!\n");
3586 	BTC_TRACE(trace_buf);
3587 
3588 #if BT_8723D_1ANT_COEX_DBG
3589 	u32tmp1 = halbtc8723d1ant_ltecoex_indirect_read_reg(btcoexist,
3590 			0x38);
3591 	u32tmp2 = halbtc8723d1ant_ltecoex_indirect_read_reg(btcoexist,
3592 			0x54);
3593 	u16tmp1 = btcoexist->btc_read_2byte(btcoexist, 0x948);
3594 	u8tmp1	= btcoexist->btc_read_1byte(btcoexist, 0x73);
3595 	u8tmp0 =  btcoexist->btc_read_1byte(btcoexist, 0x67);
3596 
3597 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3598 		"[BTCoex], ********** 0x67 = 0x%x, 0x948 = 0x%x, 0x73 = 0x%x(Before init_hw_config)\n",
3599 		    u8tmp0, u16tmp1, u8tmp1);
3600 	BTC_TRACE(trace_buf);
3601 
3602 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3603 		"[BTCoex], **********0x38= 0x%x, 0x54= 0x%x (Before init_hw_config)\n",
3604 		    u32tmp1, u32tmp2);
3605 	BTC_TRACE(trace_buf);
3606 #endif
3607 
3608 	coex_sta->bt_coex_supported_feature = 0;
3609 	coex_sta->bt_coex_supported_version = 0;
3610 	coex_sta->bt_ble_scan_type = 0;
3611 	coex_sta->bt_ble_scan_para[0] = 0;
3612 	coex_sta->bt_ble_scan_para[1] = 0;
3613 	coex_sta->bt_ble_scan_para[2] = 0;
3614 	coex_sta->bt_reg_vendor_ac = 0xffff;
3615 	coex_sta->bt_reg_vendor_ae = 0xffff;
3616 	coex_sta->isolation_btween_wb = BT_8723D_1ANT_DEFAULT_ISOLATION;
3617 	coex_sta->gnt_error_cnt = 0;
3618 	coex_sta->bt_relink_downcount = 0;
3619 	coex_sta->wl_rx_rate = BTC_UNKNOWN;
3620 
3621 	for (i = 0; i <= 9; i++)
3622 		coex_sta->bt_afh_map[i] = 0;
3623 
3624 	/* 0xf0[15:12] --> kt_ver */
3625 	coex_sta->kt_ver = (btcoexist->btc_read_1byte(btcoexist,
3626 						      0xf1) & 0xf0) >> 4;
3627 
3628 	if (coex_sta->kt_ver >= 0x3)
3629 		tmp =  TRUE;
3630 	else
3631 		tmp =  FALSE;
3632 
3633 	halbtc8723d1ant_post_state_to_bt(btcoexist,
3634 					 BT_8723D_1ANT_SCOREBOARD_DKTOPP2M,
3635 					 tmp);
3636 	/* enable TBTT nterrupt */
3637 	btcoexist->btc_write_1byte_bitmask(btcoexist, 0x550, 0x8, 0x1);
3638 
3639 	/* BT report packet sample rate	 */
3640 	btcoexist->btc_write_1byte(btcoexist, 0x790, 0x5);
3641 
3642 	/* Init 0x778 = 0x1 for 1-Ant */
3643 	btcoexist->btc_write_1byte(btcoexist, 0x778, 0x1);
3644 
3645 	/* Enable PTA (3-wire function form BT side) */
3646 	btcoexist->btc_write_1byte_bitmask(btcoexist, 0x40, 0x20, 0x1);
3647 	btcoexist->btc_write_1byte_bitmask(btcoexist, 0x41, 0x02, 0x1);
3648 
3649 	/* Enable PTA (tx/rx signal form WiFi side) */
3650 	btcoexist->btc_write_1byte_bitmask(btcoexist, 0x4c6, 0x10, 0x1);
3651 
3652 	halbtc8723d1ant_enable_gnt_to_gpio(btcoexist, TRUE);
3653 
3654 	/* PTA parameter */
3655 	halbtc8723d1ant_coex_table_with_type(btcoexist, FORCE_EXEC, 0);
3656 
3657 	halbtc8723d1ant_ps_tdma(btcoexist, FORCE_EXEC, FALSE, 8);
3658 
3659 	psd_scan->ant_det_is_ant_det_available = TRUE;
3660 
3661 	/* Antenna config */
3662 	if (coex_sta->is_rf_state_off) {
3663 
3664 		halbtc8723d1ant_set_ant_path(btcoexist, BTC_ANT_PATH_AUTO,
3665 					     FORCE_EXEC,
3666 					     BT_8723D_1ANT_PHASE_WLAN_OFF);
3667 
3668 		btcoexist->stop_coex_dm = TRUE;
3669 
3670 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3671 			"[BTCoex], **********  halbtc8723d1ant_init_hw_config (RF Off)**********\n");
3672 		BTC_TRACE(trace_buf);
3673 	} else if (wifi_only) {
3674 		coex_sta->concurrent_rx_mode_on = FALSE;
3675 		halbtc8723d1ant_set_ant_path(btcoexist, BTC_ANT_PATH_WIFI,
3676 					     FORCE_EXEC,
3677 					     BT_8723D_1ANT_PHASE_WLANONLY_INIT);
3678 
3679 		btcoexist->stop_coex_dm = TRUE;
3680 	} else {
3681 		coex_sta->concurrent_rx_mode_on = TRUE;
3682 		btcoexist->btc_write_1byte_bitmask(btcoexist, 0x953, 0x2, 0x1);
3683 		/* RF 0x1[0] = 0->Set GNT_WL_RF_Rx always = 1 for con-current Rx */
3684 		btcoexist->btc_set_rf_reg(btcoexist, BTC_RF_A, 0x1, 0x1, 0x0);
3685 		halbtc8723d1ant_set_ant_path(btcoexist, BTC_ANT_PATH_AUTO,
3686 					     FORCE_EXEC,
3687 					     BT_8723D_1ANT_PHASE_COEX_INIT);
3688 
3689 		btcoexist->stop_coex_dm = FALSE;
3690 	}
3691 
3692 	if (board_info->btdm_ant_pos == BTC_ANTENNA_AT_MAIN_PORT) {
3693 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3694 			"[BTCoex], **********  Single Antenna, Antenna at Main Port: S1**********\n");
3695 		BTC_TRACE(trace_buf);
3696 	} else {
3697 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3698 			"[BTCoex], **********  Single Antenna, Antenna at Aux Port: S0**********\n");
3699 		BTC_TRACE(trace_buf);
3700 	}
3701 
3702 }
3703 
3704 #ifdef PLATFORM_WINDOWS
3705 #pragma optimize("", off)
3706 #endif
3707 
3708 /* ************************************************************
3709  * work around function start with wa_halbtc8723d1ant_
3710  * ************************************************************
3711  * ************************************************************
3712  * extern function start with ex_halbtc8723d1ant_
3713  * ************************************************************ */
ex_halbtc8723d1ant_power_on_setting(IN struct btc_coexist * btcoexist)3714 void ex_halbtc8723d1ant_power_on_setting(IN struct btc_coexist *btcoexist)
3715 {
3716 	struct  btc_board_info	*board_info = &btcoexist->board_info;
3717 	u8 u8tmp = 0x0;
3718 	u16 u16tmp = 0x0;
3719 	u32	value = 0;
3720 
3721 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3722 		"xxxxxxxxxxxxxxxx Execute 8723d 1-Ant PowerOn Setting xxxxxxxxxxxxxxxx!!\n");
3723 	BTC_TRACE(trace_buf);
3724 
3725 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3726 		    "Ant Det Finish = %s, Ant Det Number  = %d\n",
3727 		    (board_info->btdm_ant_det_finish ? "Yes" : "No"),
3728 		    board_info->btdm_ant_num_by_ant_det);
3729 	BTC_TRACE(trace_buf);
3730 
3731 	btcoexist->stop_coex_dm = TRUE;
3732 	coex_sta->is_rf_state_off = FALSE;
3733 	psd_scan->ant_det_is_ant_det_available = FALSE;
3734 
3735 	/* enable BB, REG_SYS_FUNC_EN such that we can write BB Register correctly. */
3736 	u16tmp = btcoexist->btc_read_2byte(btcoexist, 0x2);
3737 	btcoexist->btc_write_2byte(btcoexist, 0x2, u16tmp | BIT(0) | BIT(1));
3738 
3739 	/* Local setting bit define */
3740 	/*	BIT0: "0" for no antenna inverse; "1" for antenna inverse  */
3741 	/*	BIT1: "0" for internal switch; "1" for external switch */
3742 	/*	BIT2: "0" for one antenna; "1" for two antenna */
3743 	/* NOTE: here default all internal switch and 1-antenna ==> BIT1=0 and BIT2=0 */
3744 
3745 	/* Set Antenna Path to BT side */
3746 	/* Check efuse 0xc3[6] for Single Antenna Path */
3747 	if (board_info->single_ant_path == 0) {
3748 		/* set to S1 */
3749 		board_info->btdm_ant_pos = BTC_ANTENNA_AT_MAIN_PORT;
3750 		u8tmp = 0;
3751 		value = 1;
3752 	} else if (board_info->single_ant_path == 1) {
3753 		/* set to S0 */
3754 		board_info->btdm_ant_pos = BTC_ANTENNA_AT_AUX_PORT;
3755 		u8tmp = 1;
3756 		value = 0;
3757 	}
3758 
3759 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3760 		"[BTCoex], ********** (Power On) single_ant_path  = %d, btdm_ant_pos = %d **********\n",
3761 		    board_info->single_ant_path , board_info->btdm_ant_pos);
3762 	BTC_TRACE(trace_buf);
3763 
3764 	/* Set Antenna Path to BT side */
3765 	halbtc8723d1ant_set_ant_path(btcoexist,
3766 				     BTC_ANT_PATH_AUTO,
3767 				     FORCE_EXEC,
3768 				     BT_8723D_1ANT_PHASE_COEX_POWERON);
3769 
3770 	/* Write Single Antenna Position to Registry to tell BT for 8723d. This line can be removed
3771 	since BT EFuse also add "single antenna position" in EFuse for 8723d*/
3772 	btcoexist->btc_set(btcoexist, BTC_SET_ACT_ANTPOSREGRISTRY_CTRL,
3773 			   &value);
3774 
3775 	/* Save"single antenna position" info in Local register setting for FW reading, because FW may not ready at  power on */
3776 	if (btcoexist->chip_interface == BTC_INTF_PCI)
3777 		btcoexist->btc_write_local_reg_1byte(btcoexist, 0x3e0, u8tmp);
3778 	else if (btcoexist->chip_interface == BTC_INTF_USB)
3779 		btcoexist->btc_write_local_reg_1byte(btcoexist, 0xfe08, u8tmp);
3780 	else if (btcoexist->chip_interface == BTC_INTF_SDIO)
3781 		btcoexist->btc_write_local_reg_1byte(btcoexist, 0x60, u8tmp);
3782 
3783 	/* enable GNT_WL/GNT_BT debug signal to GPIO14/15 */
3784 	halbtc8723d1ant_enable_gnt_to_gpio(btcoexist, TRUE);
3785 
3786 #if BT_8723D_1ANT_COEX_DBG
3787 
3788 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3789 		"[BTCoex], **********  LTE coex Reg 0x38 (Power-On) = 0x%x**********\n",
3790 		    halbtc8723d1ant_ltecoex_indirect_read_reg(btcoexist, 0x38));
3791 	BTC_TRACE(trace_buf);
3792 
3793 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
3794 		"[BTCoex], **********  MAC Reg 0x70/ BB Reg 0x948 (Power-On) = 0x%x / 0x%x**********\n",
3795 		    btcoexist->btc_read_4byte(btcoexist, 0x70),
3796 		    btcoexist->btc_read_2byte(btcoexist, 0x948));
3797 	BTC_TRACE(trace_buf);
3798 
3799 #endif
3800 
3801 }
3802 
ex_halbtc8723d1ant_pre_load_firmware(IN struct btc_coexist * btcoexist)3803 void ex_halbtc8723d1ant_pre_load_firmware(IN struct btc_coexist *btcoexist)
3804 {
3805 }
3806 
ex_halbtc8723d1ant_init_hw_config(IN struct btc_coexist * btcoexist,IN boolean wifi_only)3807 void ex_halbtc8723d1ant_init_hw_config(IN struct btc_coexist *btcoexist,
3808 				       IN boolean wifi_only)
3809 {
3810 	halbtc8723d1ant_init_hw_config(btcoexist, TRUE, wifi_only);
3811 }
3812 
ex_halbtc8723d1ant_init_coex_dm(IN struct btc_coexist * btcoexist)3813 void ex_halbtc8723d1ant_init_coex_dm(IN struct btc_coexist *btcoexist)
3814 {
3815 	halbtc8723d1ant_init_coex_dm(btcoexist);
3816 }
3817 
ex_halbtc8723d1ant_display_coex_info(IN struct btc_coexist * btcoexist)3818 void ex_halbtc8723d1ant_display_coex_info(IN struct btc_coexist *btcoexist)
3819 {
3820 	struct  btc_board_info		*board_info = &btcoexist->board_info;
3821 	struct  btc_stack_info		*stack_info = &btcoexist->stack_info;
3822 	struct  btc_bt_link_info	*bt_link_info = &btcoexist->bt_link_info;
3823 	u8				*cli_buf = btcoexist->cli_buf;
3824 	u8				u8tmp[4], i, ps_tdma_case = 0;
3825 	u16				u16tmp[4];
3826 	u32				u32tmp[4];
3827 	u32				fa_ofdm, fa_cck, cca_ofdm, cca_cck;
3828 	u32				fw_ver = 0, bt_patch_ver = 0, bt_coex_ver = 0;
3829 	static u8			pop_report_in_10s = 0, cnt = 0;
3830 	u32			phyver = 0;
3831 	boolean			lte_coex_on = FALSE;
3832 
3833 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
3834 		   "\r\n ============[BT Coexist info 8723D]============");
3835 	CL_PRINTF(cli_buf);
3836 
3837 	if (btcoexist->manual_control) {
3838 		CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
3839 			"\r\n ============[Under Manual Control]============");
3840 		CL_PRINTF(cli_buf);
3841 		CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
3842 			   "\r\n ==========================================");
3843 		CL_PRINTF(cli_buf);
3844 	}
3845 	if (btcoexist->stop_coex_dm) {
3846 		CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
3847 			   "\r\n ============[Coex is STOPPED]============");
3848 		CL_PRINTF(cli_buf);
3849 		CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
3850 			   "\r\n ==========================================");
3851 		CL_PRINTF(cli_buf);
3852 	}
3853 
3854 	if (!coex_sta->bt_disabled) {
3855 		if (coex_sta->bt_coex_supported_feature == 0) {
3856 			btcoexist->btc_get(btcoexist, BTC_GET_U4_SUPPORTED_FEATURE,
3857 						&coex_sta->bt_coex_supported_feature);
3858 
3859 			if (coex_sta->bt_coex_supported_feature & BIT(13))
3860 				coex_sta->is_bt_mesh_ver = TRUE;
3861 		}
3862 
3863 		if ((coex_sta->bt_coex_supported_version == 0) ||
3864 			 (coex_sta->bt_coex_supported_version == 0xffff))
3865 			btcoexist->btc_get(btcoexist, BTC_GET_U4_SUPPORTED_VERSION,
3866 						&coex_sta->bt_coex_supported_version);
3867 
3868 		if (coex_sta->bt_reg_vendor_ac == 0xffff)
3869 			coex_sta->bt_reg_vendor_ac = (u16)(
3870 						btcoexist->btc_get_bt_reg(btcoexist, 3,
3871 						0xac) & 0xffff);
3872 
3873 		if (coex_sta->bt_reg_vendor_ae == 0xffff)
3874 			coex_sta->bt_reg_vendor_ae = (u16)(
3875 						btcoexist->btc_get_bt_reg(btcoexist, 3,
3876 						0xae) & 0xffff);
3877 
3878 		btcoexist->btc_get(btcoexist, BTC_GET_U4_BT_PATCH_VER,
3879 						&bt_patch_ver);
3880 		btcoexist->bt_info.bt_get_fw_ver = bt_patch_ver;
3881 
3882 		if (coex_sta->num_of_profile > 0) {
3883 			cnt++;
3884 
3885 			if (cnt >= 3) {
3886 				btcoexist->btc_get_bt_afh_map_from_bt(btcoexist, 0,
3887 					&coex_sta->bt_afh_map[0]);
3888 				cnt = 0;
3889 			}
3890 		}
3891 	}
3892 
3893 	if (psd_scan->ant_det_try_count == 0) {
3894 		CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %s/ %s",
3895 			   "Ant PG Num/ Mech/ Pos",
3896 			   board_info->pg_ant_num,
3897 			   (board_info->btdm_ant_num == 1 ?
3898 			   "Shared" : "Non-Shared"),
3899 			   (board_info->btdm_ant_pos == 1 ?
3900 			   "S1" : "S0"));
3901 		CL_PRINTF(cli_buf);
3902 	} else {
3903 		CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
3904 			   "\r\n %-35s = %d/ %d/ %s  (%d/%d/%d)",
3905 			   "Ant PG Num/ Mech(Ant_Det)/ Pos",
3906 			   board_info->pg_ant_num,
3907 			   board_info->btdm_ant_num_by_ant_det,
3908 			   (board_info->btdm_ant_pos == 1 ? "S1" : "S0"),
3909 			   psd_scan->ant_det_try_count,
3910 			   psd_scan->ant_det_fail_count,
3911 			   psd_scan->ant_det_result);
3912 		CL_PRINTF(cli_buf);
3913 
3914 		if (board_info->btdm_ant_det_finish) {
3915 
3916 			if (psd_scan->ant_det_result != 12)
3917 				CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
3918 					   "\r\n %-35s = %s",
3919 					   "Ant Det PSD Value",
3920 					   psd_scan->ant_det_peak_val);
3921 			else
3922 				CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
3923 					   "\r\n %-35s = %d",
3924 					   "Ant Det PSD Value",
3925 					   psd_scan->ant_det_psd_scan_peak_val
3926 					   / 100);
3927 			CL_PRINTF(cli_buf);
3928 		}
3929 	}
3930 
3931 	if (board_info->ant_det_result_five_complete) {
3932 		CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
3933 			   "\r\n %-35s = %d/ %d",
3934 			   "AntDet(Registry) Num/PSD Value",
3935 			   board_info->btdm_ant_num_by_ant_det,
3936 			   (board_info->antdetval & 0x7f));
3937 		CL_PRINTF(cli_buf);
3938 	}
3939 
3940 	bt_patch_ver = btcoexist->bt_info.bt_get_fw_ver;
3941 	btcoexist->btc_get(btcoexist, BTC_GET_U4_WIFI_FW_VER, &fw_ver);
3942 	phyver = btcoexist->btc_get_bt_phydm_version(btcoexist);
3943 
3944 	bt_coex_ver = ((coex_sta->bt_coex_supported_version & 0xff00) >> 8);
3945 
3946 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
3947 				"\r\n %-35s = %d_%02x/ 0x%02x/ 0x%02x (%s)",
3948 				"CoexVer WL/  BT_Desired/ BT_Report",
3949 				glcoex_ver_date_8723d_1ant, glcoex_ver_8723d_1ant,
3950 				glcoex_ver_btdesired_8723d_1ant,
3951 				bt_coex_ver,
3952 				(bt_coex_ver == 0xff ? "Unknown" :
3953 				(coex_sta->bt_disabled ? "BT-disable" :
3954 				(bt_coex_ver >= glcoex_ver_btdesired_8723d_1ant ?
3955 				"Match" : "Mis-Match"))));
3956 	CL_PRINTF(cli_buf);
3957 
3958 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
3959 		   "\r\n %-35s = 0x%x/ 0x%x/ v%d/ %c",
3960 		   "W_FW/ B_FW/ Phy/ Kt",
3961 		   fw_ver, bt_patch_ver, phyver,
3962 		   coex_sta->kt_ver + 65);
3963 	CL_PRINTF(cli_buf);
3964 
3965 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %02x %02x %02x ",
3966 		   "Wifi channel informed to BT",
3967 		   coex_dm->wifi_chnl_info[0], coex_dm->wifi_chnl_info[1],
3968 		   coex_dm->wifi_chnl_info[2]);
3969 	CL_PRINTF(cli_buf);
3970 
3971 	/* wifi status */
3972 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s",
3973 		   "============[Wifi Status]============");
3974 	CL_PRINTF(cli_buf);
3975 	btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_WIFI_STATUS);
3976 
3977 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s",
3978 		   "============[BT Status]============");
3979 	CL_PRINTF(cli_buf);
3980 
3981 	pop_report_in_10s++;
3982 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
3983 		   "\r\n %-35s = %s/ %ddBm/ %d/ %d",
3984 		   "BT status/ rssi/ retryCnt/ popCnt",
3985 		   ((coex_sta->bt_disabled) ? ("disabled") :	((
3986 			   coex_sta->c2h_bt_inquiry_page) ? ("inquiry-page")
3987 			   : ((BT_8723D_1ANT_BT_STATUS_NON_CONNECTED_IDLE ==
3988 			       coex_dm->bt_status) ? "non-connected-idle" :
3989 		((BT_8723D_1ANT_BT_STATUS_CONNECTED_IDLE == coex_dm->bt_status)
3990 				       ? "connected-idle" : "busy")))),
3991 		   coex_sta->bt_rssi - 100, coex_sta->bt_retry_cnt,
3992 		   coex_sta->pop_event_cnt);
3993 	CL_PRINTF(cli_buf);
3994 
3995 	if (pop_report_in_10s >= 5) {
3996 		coex_sta->pop_event_cnt = 0;
3997 		pop_report_in_10s = 0;
3998 	}
3999 
4000 	if (coex_sta->num_of_profile != 0)
4001 		CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4002 				"\r\n %-35s = %s%s%s%s%s (multilink = %d)",
4003 				"Profiles",
4004 				((bt_link_info->a2dp_exist) ?
4005 				((coex_sta->is_bt_a2dp_sink) ? "A2DP sink," :
4006 				"A2DP,") : ""),
4007 				((bt_link_info->sco_exist) ?  "HFP," : ""),
4008 				((bt_link_info->hid_exist) ?
4009 				((coex_sta->is_hid_rcu) ? "HID(RCU)" :
4010 				((coex_sta->hid_busy_num >= 2) ? "HID(4/18)," :
4011 				"HID(2/18),")) : ""),
4012 				((bt_link_info->pan_exist) ?
4013 				((coex_sta->is_bt_opp_exist) ? "OPP," : "PAN,") : ""),
4014 				((coex_sta->voice_over_HOGP) ? "Voice" : ""),
4015 				coex_sta->is_bt_multi_link);
4016 	else
4017 		CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4018 			   "\r\n %-35s = None", "Profiles");
4019 
4020 	CL_PRINTF(cli_buf);
4021 
4022 	if (bt_link_info->a2dp_exist) {
4023 		CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4024 			   "\r\n %-35s = %s/ %d/ 0x%x/ 0x%x",
4025 			   "CQDDR/Bitpool/V_ID/D_name",
4026 			   ((coex_sta->is_A2DP_3M) ? "On" : "Off"),
4027 			   coex_sta->a2dp_bit_pool,
4028 			   coex_sta->bt_a2dp_vendor_id,
4029 			   coex_sta->bt_a2dp_device_name);
4030 		CL_PRINTF(cli_buf);
4031 	}
4032 
4033 	if (bt_link_info->hid_exist) {
4034 		CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d",
4035 			   "HID PairNum/Forbid_Slot",
4036 			   coex_sta->hid_pair_cnt
4037 			  );
4038 		CL_PRINTF(cli_buf);
4039 	}
4040 
4041 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s/ %d/ %s/ 0x%x",
4042 				"Role/RoleSwCnt/IgnWlact/Feature",
4043 				((bt_link_info->slave_role) ? "Slave" : "Master"),
4044 				coex_sta->cnt_roleswitch,
4045 				((coex_dm->cur_ignore_wlan_act) ? "Yes" : "No"),
4046 				coex_sta->bt_coex_supported_feature);
4047 	CL_PRINTF(cli_buf);
4048 
4049 	if ((coex_sta->bt_ble_scan_type & 0x7) != 0x0) {
4050 			CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4051 				"\r\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x",
4052 				"BLEScan Type/TV/Init/Ble",
4053 				coex_sta->bt_ble_scan_type,
4054 				(coex_sta->bt_ble_scan_type & 0x1 ?
4055 				coex_sta->bt_ble_scan_para[0] : 0x0),
4056 				(coex_sta->bt_ble_scan_type & 0x2 ?
4057 				coex_sta->bt_ble_scan_para[1] : 0x0),
4058 				(coex_sta->bt_ble_scan_type & 0x4 ?
4059 				coex_sta->bt_ble_scan_para[2] : 0x0));
4060 			CL_PRINTF(cli_buf);
4061 	}
4062 
4063 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d/ %d/ %d/ %d",
4064 		   "ReInit/ReLink/IgnWlact/Page/NameReq",
4065 		   coex_sta->cnt_reinit,
4066 		   coex_sta->cnt_setuplink,
4067 		   coex_sta->cnt_ignwlanact,
4068 		   coex_sta->cnt_page,
4069 		   coex_sta->cnt_remotenamereq
4070 		  );
4071 	CL_PRINTF(cli_buf);
4072 
4073 	halbtc8723d1ant_read_score_board(btcoexist,	&u16tmp[0]);
4074 
4075 	if ((coex_sta->bt_reg_vendor_ae == 0xffff) ||
4076 	    (coex_sta->bt_reg_vendor_ac == 0xffff))
4077 		CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = x/ x/ 0x%04x",
4078 			   "0xae[4]/0xac[1:0]/Scoreboard(B->W)", u16tmp[0]);
4079 	else
4080 		CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4081 			   "\r\n %-35s = 0x%x/ 0x%x/ 0x%04x",
4082 			   "0xae[4]/0xac[1:0]/Scoreboard(B->W)",
4083 			   (int)((coex_sta->bt_reg_vendor_ae & BIT(4)) >> 4),
4084 			   coex_sta->bt_reg_vendor_ac & 0x3, u16tmp[0]);
4085 	CL_PRINTF(cli_buf);
4086 
4087 	if (coex_sta->num_of_profile > 0) {
4088 
4089 		CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4090 			"\r\n %-35s = %02x%02x%02x%02x %02x%02x%02x%02x %02x %02x",
4091 			"AFH MAP",
4092 			coex_sta->bt_afh_map[0],
4093 			coex_sta->bt_afh_map[1],
4094 			coex_sta->bt_afh_map[2],
4095 			coex_sta->bt_afh_map[3],
4096 			coex_sta->bt_afh_map[4],
4097 			coex_sta->bt_afh_map[5],
4098 			coex_sta->bt_afh_map[6],
4099 			coex_sta->bt_afh_map[7],
4100 			coex_sta->bt_afh_map[8],
4101 			coex_sta->bt_afh_map[9]
4102 			   );
4103 		CL_PRINTF(cli_buf);
4104 	}
4105 
4106 	for (i = 0; i < BT_INFO_SRC_8723D_1ANT_MAX; i++) {
4107 		if (coex_sta->bt_info_c2h_cnt[i]) {
4108 			CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4109 				"\r\n %-35s = %02x %02x %02x %02x %02x %02x %02x (%d)",
4110 				   glbt_info_src_8723d_1ant[i],
4111 				   coex_sta->bt_info_c2h[i][0],
4112 				   coex_sta->bt_info_c2h[i][1],
4113 				   coex_sta->bt_info_c2h[i][2],
4114 				   coex_sta->bt_info_c2h[i][3],
4115 				   coex_sta->bt_info_c2h[i][4],
4116 				   coex_sta->bt_info_c2h[i][5],
4117 				   coex_sta->bt_info_c2h[i][6],
4118 				   coex_sta->bt_info_c2h_cnt[i]);
4119 			CL_PRINTF(cli_buf);
4120 		}
4121 	}
4122 
4123 
4124 	if (btcoexist->manual_control)
4125 		CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s",
4126 			"============[mechanisms] (before Manual)============");
4127 	else
4128 		CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s",
4129 			   "============[Mechanisms]============");
4130 
4131 	CL_PRINTF(cli_buf);
4132 
4133 	ps_tdma_case = coex_dm->cur_ps_tdma;
4134 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4135 		   "\r\n %-35s = %02x %02x %02x %02x %02x (case-%d, %s, Timer:%d)",
4136 		   "TDMA",
4137 		   coex_dm->ps_tdma_para[0], coex_dm->ps_tdma_para[1],
4138 		   coex_dm->ps_tdma_para[2], coex_dm->ps_tdma_para[3],
4139 		   coex_dm->ps_tdma_para[4], ps_tdma_case,
4140 		   (coex_dm->cur_ps_tdma_on ? "TDMA On" : "TDMA Off"),
4141 		   coex_sta->tdma_timer_base);
4142 
4143 	CL_PRINTF(cli_buf);
4144 
4145 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x6c0);
4146 	u32tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0x6c4);
4147 	u32tmp[2] = btcoexist->btc_read_4byte(btcoexist, 0x6c8);
4148 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4149 		   "\r\n %-35s = %d/ 0x%x/ 0x%x/ 0x%x",
4150 		   "Table/0x6c0/0x6c4/0x6c8",
4151 		   coex_sta->coex_table_type, u32tmp[0], u32tmp[1], u32tmp[2]);
4152 	CL_PRINTF(cli_buf);
4153 
4154 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x778);
4155 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x6cc);
4156 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4157 		   "\r\n %-35s = 0x%x/ 0x%x/ 0x%04x",
4158 		   "0x778/0x6cc/Scoreboard(W->B)",
4159 		   u8tmp[0], u32tmp[0], coex_sta->score_board_WB);
4160 	CL_PRINTF(cli_buf);
4161 
4162 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s/ %s",
4163 		   "AntDiv/ ForceLPS",
4164 		   ((board_info->ant_div_cfg) ? "On" : "Off"),
4165 		   ((coex_sta->force_lps_ctrl) ? "On" : "Off"));
4166 	CL_PRINTF(cli_buf);
4167 
4168 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d",
4169 		   "BT_Empty/BT_Late",
4170 		   coex_sta->wl_fw_dbg_info[4],
4171 		   coex_sta->wl_fw_dbg_info[5]);
4172 	CL_PRINTF(cli_buf);
4173 
4174 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d/ %s",
4175 		   "TDMA_Togg_cnt/WL5ms_cnt/WL5ms_off",
4176 		   coex_sta->wl_fw_dbg_info[6], coex_sta->wl_fw_dbg_info[7],
4177 		   ((coex_sta->is_no_wl_5ms_extend) ? "Yes" : "No"));
4178 	CL_PRINTF(cli_buf);
4179 
4180 	u32tmp[0] = halbtc8723d1ant_ltecoex_indirect_read_reg(btcoexist, 0x38);
4181 	lte_coex_on = ((u32tmp[0] & BIT(7)) >> 7) ?  TRUE : FALSE;
4182 
4183 	if (lte_coex_on) {
4184 
4185 		u32tmp[0] = halbtc8723d1ant_ltecoex_indirect_read_reg(btcoexist,
4186 				0xa0);
4187 		u32tmp[1] = halbtc8723d1ant_ltecoex_indirect_read_reg(btcoexist,
4188 				0xa4);
4189 
4190 		CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/ 0x%x",
4191 			   "LTE Coex  Table W_L/B_L",
4192 			   u32tmp[0] & 0xffff, u32tmp[1] & 0xffff);
4193 		CL_PRINTF(cli_buf);
4194 
4195 		u32tmp[0] = halbtc8723d1ant_ltecoex_indirect_read_reg(btcoexist,
4196 				0xa8);
4197 		u32tmp[1] = halbtc8723d1ant_ltecoex_indirect_read_reg(btcoexist,
4198 				0xac);
4199 		u32tmp[2] = halbtc8723d1ant_ltecoex_indirect_read_reg(btcoexist,
4200 				0xb0);
4201 		u32tmp[3] = halbtc8723d1ant_ltecoex_indirect_read_reg(btcoexist,
4202 				0xb4);
4203 
4204 		CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4205 			   "\r\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x",
4206 			   "LTE Break Table W_L/B_L/L_W/L_B",
4207 			   u32tmp[0] & 0xffff, u32tmp[1] & 0xffff,
4208 			   u32tmp[2] & 0xffff, u32tmp[3] & 0xffff);
4209 		CL_PRINTF(cli_buf);
4210 
4211 	}
4212 
4213 	/* Hw setting		 */
4214 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s",
4215 		   "============[Hw setting]============");
4216 	CL_PRINTF(cli_buf);
4217 
4218 	u32tmp[0] = halbtc8723d1ant_ltecoex_indirect_read_reg(btcoexist, 0x38);
4219 	u32tmp[1] = halbtc8723d1ant_ltecoex_indirect_read_reg(btcoexist, 0x54);
4220 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x73);
4221 
4222 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s/ %s",
4223 		   "LTE Coex/Path Owner",
4224 		   ((lte_coex_on) ? "On" : "Off") ,
4225 		   ((u8tmp[0] & BIT(2)) ? "WL" : "BT"));
4226 	CL_PRINTF(cli_buf);
4227 
4228 	if (lte_coex_on) {
4229 		CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4230 			   "\r\n %-35s = %d/ %d/ %d/ %d",
4231 			   "LTE 3Wire/OPMode/UART/UARTMode",
4232 			   (int)((u32tmp[0] & BIT(6)) >> 6),
4233 			   (int)((u32tmp[0] & (BIT(5) | BIT(4))) >> 4),
4234 			   (int)((u32tmp[0] & BIT(3)) >> 3),
4235 			   (int)(u32tmp[0] & (BIT(2) | BIT(1) | BIT(0))));
4236 		CL_PRINTF(cli_buf);
4237 
4238 		CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d",
4239 			   "LTE_Busy/UART_Busy",
4240 			(int)((u32tmp[1] & BIT(1)) >> 1), (int)(u32tmp[1] & BIT(0)));
4241 		CL_PRINTF(cli_buf);
4242 	}
4243 
4244 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4245 			   "\r\n %-35s = %s (BB:%s)/ %s (BB:%s)/ %s (gnt_err = %d)",
4246 			   "GNT_WL_Ctrl/GNT_BT_Ctrl/Dbg",
4247 			   ((u32tmp[0] & BIT(12)) ? "SW" : "HW"),
4248 			   ((u32tmp[0] & BIT(8)) ?	"SW" : "HW"),
4249 			   ((u32tmp[0] & BIT(14)) ? "SW" : "HW"),
4250 			   ((u32tmp[0] & BIT(10)) ?  "SW" : "HW"),
4251 			   ((u8tmp[0] & BIT(3)) ? "On" : "Off"),
4252 			   coex_sta->gnt_error_cnt);
4253 	CL_PRINTF(cli_buf);
4254 
4255 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d",
4256 		   "GNT_WL/GNT_BT",
4257 		   (int)((u32tmp[1] & BIT(2)) >> 2),
4258 		   (int)((u32tmp[1] & BIT(3)) >> 3));
4259 	CL_PRINTF(cli_buf);
4260 
4261 	u16tmp[0] = btcoexist->btc_read_2byte(btcoexist, 0x948);
4262 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x67);
4263 
4264 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/ 0x%x",
4265 		   "0x948/0x67[7]",
4266 		   u16tmp[0], (int)((u8tmp[0] & BIT(7)) >> 7));
4267 	CL_PRINTF(cli_buf);
4268 
4269 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x964);
4270 	u8tmp[1] = btcoexist->btc_read_1byte(btcoexist, 0x864);
4271 	u8tmp[2] = btcoexist->btc_read_1byte(btcoexist, 0xab7);
4272 	u8tmp[3] = btcoexist->btc_read_1byte(btcoexist, 0xa01);
4273 
4274 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4275 		   "\r\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x",
4276 		   "0x964[1]/0x864[0]/0xab7[5]/0xa01[7]",
4277 		   (int)((u8tmp[0] & BIT(1)) >> 1), (int)((u8tmp[1] & BIT(0))),
4278 		   (int)((u8tmp[2] & BIT(3)) >> 3),
4279 		   (int)((u8tmp[3] & BIT(7)) >> 7));
4280 	CL_PRINTF(cli_buf);
4281 
4282 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x430);
4283 	u32tmp[1] = btcoexist->btc_read_4byte(btcoexist, 0x434);
4284 	u16tmp[0] = btcoexist->btc_read_2byte(btcoexist, 0x42a);
4285 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x426);
4286 	u8tmp[1] = btcoexist->btc_read_1byte(btcoexist, 0x45e);
4287 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4288 		   "\r\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x/ 0x%x",
4289 		   "0x430/0x434/0x42a/0x426/0x45e[3]",
4290 		   u32tmp[0], u32tmp[1], u16tmp[0], u8tmp[0],
4291 		   (int)((u8tmp[1] & BIT(3)) >> 3));
4292 	CL_PRINTF(cli_buf);
4293 
4294 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x4c6);
4295 	u16tmp[0] = btcoexist->btc_read_2byte(btcoexist, 0x40);
4296 
4297 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4298 		   "\r\n %-35s = 0x%x/ 0x%x", "0x4c6[4]/0x40[5]",
4299 		   (int)((u8tmp[0] & BIT(4)) >> 4),
4300 		   (int)((u16tmp[0] & BIT(5)) >> 5));
4301 	CL_PRINTF(cli_buf);
4302 
4303 	u32tmp[0] = btcoexist->btc_read_4byte(btcoexist, 0x550);
4304 	u8tmp[0] = btcoexist->btc_read_1byte(btcoexist, 0x522);
4305 	u8tmp[1] = btcoexist->btc_read_1byte(btcoexist, 0x953);
4306 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = 0x%x/ 0x%x/ %s",
4307 		   "0x550/0x522/4-RxAGC",
4308 		   u32tmp[0], u8tmp[0], (u8tmp[1] & 0x2) ? "On" : "Off");
4309 	CL_PRINTF(cli_buf);
4310 
4311 	fa_ofdm = btcoexist->btc_phydm_query_PHY_counter(btcoexist, PHYDM_INFO_FA_OFDM);
4312 	fa_cck = btcoexist->btc_phydm_query_PHY_counter(btcoexist, PHYDM_INFO_FA_CCK);
4313 	cca_ofdm = btcoexist->btc_phydm_query_PHY_counter(btcoexist, PHYDM_INFO_CCA_OFDM);
4314 	cca_cck = btcoexist->btc_phydm_query_PHY_counter(btcoexist, PHYDM_INFO_CCA_CCK);
4315 
4316 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE,
4317 		   "\r\n %-35s = 0x%x/ 0x%x/ 0x%x/ 0x%x",
4318 		   "CCK-CCA/CCK-FA/OFDM-CCA/OFDM-FA",
4319 		   cca_cck, fa_cck, cca_ofdm, fa_ofdm);
4320 	CL_PRINTF(cli_buf);
4321 
4322 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d/ %d/ %d (Rx_rate Data/RTS= %d/%d)",
4323 		   "CRC_OK CCK/11g/11n/11ac",
4324 		   coex_sta->crc_ok_cck, coex_sta->crc_ok_11g,
4325 		   coex_sta->crc_ok_11n, coex_sta->crc_ok_11n_vht,
4326 		   coex_sta->wl_rx_rate, coex_sta->wl_rts_rx_rate);
4327 	CL_PRINTF(cli_buf);
4328 
4329 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d/ %d/ %d",
4330 		   "CRC_Err CCK/11g/11n/11n-agg",
4331 		   coex_sta->crc_err_cck, coex_sta->crc_err_11g,
4332 		   coex_sta->crc_err_11n, coex_sta->crc_err_11n_vht);
4333 	CL_PRINTF(cli_buf);
4334 
4335 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %s/ %s/ %s/ %d",
4336 		   "WlHiPri/ Locking/ Locked/ Noisy",
4337 		   (coex_sta->wifi_is_high_pri_task ? "Yes" : "No"),
4338 		   (coex_sta->cck_lock ? "Yes" : "No"),
4339 		   (coex_sta->cck_lock_ever ? "Yes" : "No"),
4340 		   coex_sta->wl_noisy_level);
4341 	CL_PRINTF(cli_buf);
4342 
4343 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d %s",
4344 		   "0x770(Hi-pri rx/tx)",
4345 		   coex_sta->high_priority_rx, coex_sta->high_priority_tx,
4346 		   (coex_sta->is_hipri_rx_overhead ? "(scan overhead!!)" : ""));
4347 	CL_PRINTF(cli_buf);
4348 
4349 	CL_SPRINTF(cli_buf, BT_TMP_BUF_SIZE, "\r\n %-35s = %d/ %d %s",
4350 		   "0x774(Lo-pri rx/tx)",
4351 		   coex_sta->low_priority_rx, coex_sta->low_priority_tx,
4352 		   (bt_link_info->slave_role ? "(Slave!!)" : (
4353 		   coex_sta->is_tdma_btautoslot_hang ? "(auto-slot hang!!)" : "")));
4354 	CL_PRINTF(cli_buf);
4355 
4356 	btcoexist->btc_disp_dbg_msg(btcoexist, BTC_DBG_DISP_COEX_STATISTICS);
4357 }
4358 
4359 
ex_halbtc8723d1ant_ips_notify(IN struct btc_coexist * btcoexist,IN u8 type)4360 void ex_halbtc8723d1ant_ips_notify(IN struct btc_coexist *btcoexist, IN u8 type)
4361 {
4362 	if (btcoexist->manual_control ||	btcoexist->stop_coex_dm)
4363 		return;
4364 
4365 	if (BTC_IPS_ENTER == type) {
4366 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4367 			    "[BTCoex], IPS ENTER notify\n");
4368 		BTC_TRACE(trace_buf);
4369 		coex_sta->under_ips = TRUE;
4370 
4371 		/* Write WL "Active" in Score-board for LPS off */
4372 		halbtc8723d1ant_post_state_to_bt(btcoexist,
4373 				BT_8723D_1ANT_SCOREBOARD_ACTIVE |
4374 				BT_8723D_1ANT_SCOREBOARD_ONOFF |
4375 				BT_8723D_1ANT_SCOREBOARD_SCAN |
4376 				BT_8723D_1ANT_SCOREBOARD_UNDERTEST,
4377 				FALSE);
4378 
4379 		halbtc8723d1ant_set_ant_path(btcoexist, BTC_ANT_PATH_AUTO,
4380 					     FORCE_EXEC,
4381 					     BT_8723D_1ANT_PHASE_WLAN_OFF);
4382 
4383 		halbtc8723d1ant_coex_table_with_type(btcoexist, NORMAL_EXEC, 0);
4384 
4385 		halbtc8723d1ant_ps_tdma(btcoexist, NORMAL_EXEC, FALSE, 0);
4386 
4387 	} else if (BTC_IPS_LEAVE == type) {
4388 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4389 			    "[BTCoex], IPS LEAVE notify\n");
4390 		BTC_TRACE(trace_buf);
4391 #if 0
4392 		halbtc8723d1ant_post_state_to_bt(btcoexist,
4393 					 BT_8723D_1ANT_SCOREBOARD_ACTIVE, TRUE);
4394 
4395 		halbtc8723d1ant_post_state_to_bt(btcoexist,
4396 				 BT_8723D_1ANT_SCOREBOARD_ONOFF, TRUE);
4397 #endif
4398 
4399 		halbtc8723d1ant_init_hw_config(btcoexist, FALSE, FALSE);
4400 		halbtc8723d1ant_init_coex_dm(btcoexist);
4401 
4402 		coex_sta->under_ips = FALSE;
4403 	}
4404 }
4405 
ex_halbtc8723d1ant_lps_notify(IN struct btc_coexist * btcoexist,IN u8 type)4406 void ex_halbtc8723d1ant_lps_notify(IN struct btc_coexist *btcoexist, IN u8 type)
4407 {
4408 	static boolean  pre_force_lps_on = FALSE;
4409 
4410 	if (btcoexist->manual_control || btcoexist->stop_coex_dm)
4411 		return;
4412 
4413 	if (BTC_LPS_ENABLE == type) {
4414 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4415 			    "[BTCoex], LPS ENABLE notify\n");
4416 		BTC_TRACE(trace_buf);
4417 		coex_sta->under_lps = TRUE;
4418 
4419 		if (coex_sta->force_lps_ctrl == TRUE) { /* LPS No-32K */
4420 			/* Write WL "Active" in Score-board for PS-TDMA */
4421 			pre_force_lps_on = TRUE;
4422 			halbtc8723d1ant_post_state_to_bt(btcoexist,
4423 					 BT_8723D_1ANT_SCOREBOARD_ACTIVE, TRUE);
4424 
4425 		} else { /* LPS-32K, need check if this h2c 0x71 can work?? (2015/08/28) */
4426 			/* Write WL "Non-Active" in Score-board for Native-PS */
4427 			pre_force_lps_on = FALSE;
4428 			halbtc8723d1ant_post_state_to_bt(btcoexist,
4429 				 BT_8723D_1ANT_SCOREBOARD_ACTIVE, FALSE);
4430 
4431 			halbtc8723d1ant_action_wifi_native_lps(btcoexist);
4432 		}
4433 	} else if (BTC_LPS_DISABLE == type) {
4434 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4435 			    "[BTCoex], LPS DISABLE notify\n");
4436 		BTC_TRACE(trace_buf);
4437 		coex_sta->under_lps = FALSE;
4438 
4439 		/* Write WL "Active" in Score-board for LPS off */
4440 		halbtc8723d1ant_post_state_to_bt(btcoexist,
4441 					 BT_8723D_1ANT_SCOREBOARD_ACTIVE, TRUE);
4442 
4443 		if ((!pre_force_lps_on) && (!coex_sta->force_lps_ctrl))
4444 			halbtc8723d1ant_query_bt_info(btcoexist);
4445 	}
4446 }
4447 
ex_halbtc8723d1ant_scan_notify(IN struct btc_coexist * btcoexist,IN u8 type)4448 void ex_halbtc8723d1ant_scan_notify(IN struct btc_coexist *btcoexist,
4449 				    IN u8 type)
4450 {
4451 	boolean wifi_connected = FALSE;
4452 
4453 	if (btcoexist->manual_control ||
4454 	    btcoexist->stop_coex_dm)
4455 		return;
4456 
4457 	coex_sta->freeze_coexrun_by_btinfo = FALSE;
4458 
4459 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
4460 			   &wifi_connected);
4461 
4462 	if (BTC_SCAN_START == type) {
4463 
4464 		if (!wifi_connected)
4465 			coex_sta->wifi_in_scan_task = TRUE;
4466 
4467 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4468 			    "[BTCoex], SCAN START notify\n");
4469 		BTC_TRACE(trace_buf);
4470 
4471 		halbtc8723d1ant_post_state_to_bt(btcoexist,
4472 					BT_8723D_1ANT_SCOREBOARD_ACTIVE |
4473 					BT_8723D_1ANT_SCOREBOARD_SCAN |
4474 					BT_8723D_1ANT_SCOREBOARD_ONOFF,
4475 					TRUE);
4476 
4477 		/*halbtc8723d1ant_query_bt_info(btcoexist);*/
4478 
4479 		/* Force antenna setup for no scan result issue */
4480 		halbtc8723d1ant_set_ant_path(btcoexist, BTC_ANT_PATH_AUTO,
4481 					     FORCE_EXEC,
4482 					     BT_8723D_1ANT_PHASE_2G_RUNTIME);
4483 
4484 		halbtc8723d1ant_run_coexist_mechanism(btcoexist);
4485 
4486 	} else {
4487 
4488 		coex_sta->wifi_in_scan_task = FALSE;
4489 
4490 		btcoexist->btc_get(btcoexist, BTC_GET_U1_AP_NUM,
4491 				   &coex_sta->scan_ap_num);
4492 
4493 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4494 			    "[BTCoex], SCAN FINISH notify  (Scan-AP = %d)\n",
4495 			    coex_sta->scan_ap_num);
4496 		BTC_TRACE(trace_buf);
4497 
4498 		halbtc8723d1ant_run_coexist_mechanism(btcoexist);
4499 	}
4500 
4501 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4502 		    "[BTCoex], SCAN Notify() end\n");
4503 	BTC_TRACE(trace_buf);
4504 
4505 }
4506 
ex_halbtc8723d1ant_connect_notify(IN struct btc_coexist * btcoexist,IN u8 type)4507 void ex_halbtc8723d1ant_connect_notify(IN struct btc_coexist *btcoexist,
4508 				       IN u8 type)
4509 {
4510 	boolean	wifi_connected = FALSE;
4511 
4512 	if (btcoexist->manual_control ||
4513 	    btcoexist->stop_coex_dm)
4514 		return;
4515 
4516 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
4517 			   &wifi_connected);
4518 
4519 	if (BTC_ASSOCIATE_START == type) {
4520 
4521 		coex_sta->wifi_is_high_pri_task = TRUE;
4522 
4523 		halbtc8723d1ant_post_state_to_bt(btcoexist,
4524 					 BT_8723D_1ANT_SCOREBOARD_ACTIVE |
4525 					 BT_8723D_1ANT_SCOREBOARD_SCAN |
4526 					 BT_8723D_1ANT_SCOREBOARD_ONOFF,
4527 					 TRUE);
4528 
4529 		/* Force antenna setup for no scan result issue */
4530 		halbtc8723d1ant_set_ant_path(btcoexist, BTC_ANT_PATH_AUTO,
4531 					     FORCE_EXEC,
4532 					     BT_8723D_1ANT_PHASE_2G_RUNTIME);
4533 
4534 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4535 			    "[BTCoex], CONNECT START notify\n");
4536 		BTC_TRACE(trace_buf);
4537 
4538 		coex_dm->arp_cnt = 0;
4539 		coex_sta->connect_ap_period_cnt = 2;
4540 
4541 		halbtc8723d1ant_run_coexist_mechanism(btcoexist);
4542 
4543 		/* To keep TDMA case during connect process,
4544 		to avoid changed by Btinfo and runcoexmechanism */
4545 		coex_sta->freeze_coexrun_by_btinfo = TRUE;
4546 	} else {
4547 
4548 		coex_sta->wifi_is_high_pri_task = FALSE;
4549 		coex_sta->freeze_coexrun_by_btinfo = FALSE;
4550 
4551 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4552 			    "[BTCoex], CONNECT FINISH notify\n");
4553 		BTC_TRACE(trace_buf);
4554 
4555 		halbtc8723d1ant_run_coexist_mechanism(btcoexist);
4556 	}
4557 
4558 }
4559 
ex_halbtc8723d1ant_media_status_notify(IN struct btc_coexist * btcoexist,IN u8 type)4560 void ex_halbtc8723d1ant_media_status_notify(IN struct btc_coexist *btcoexist,
4561 		IN u8 type)
4562 {
4563 	boolean			wifi_under_b_mode = FALSE;
4564 
4565 	if (btcoexist->manual_control ||
4566 	    btcoexist->stop_coex_dm)
4567 		return;
4568 
4569 	if (BTC_MEDIA_CONNECT == type) {
4570 
4571 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4572 			    "[BTCoex], MEDIA connect notify\n");
4573 		BTC_TRACE(trace_buf);
4574 
4575 		halbtc8723d1ant_post_state_to_bt(btcoexist,
4576 					 BT_8723D_1ANT_SCOREBOARD_ACTIVE |
4577 					 BT_8723D_1ANT_SCOREBOARD_ONOFF,
4578 					 TRUE);
4579 
4580 		/* Force antenna setup for no scan result issue */
4581 		halbtc8723d1ant_set_ant_path(btcoexist, BTC_ANT_PATH_AUTO,
4582 					     FORCE_EXEC,
4583 					     BT_8723D_1ANT_PHASE_2G_RUNTIME);
4584 
4585 		btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_UNDER_B_MODE,
4586 				   &wifi_under_b_mode);
4587 
4588 		/* Set CCK Tx/Rx high Pri except 11b mode */
4589 		if (wifi_under_b_mode) {
4590 			btcoexist->btc_write_1byte(btcoexist, 0x6cd,
4591 						   0x00); /* CCK Tx */
4592 			btcoexist->btc_write_1byte(btcoexist, 0x6cf,
4593 						   0x00); /* CCK Rx */
4594 		} else {
4595 			/* btcoexist->btc_write_1byte(btcoexist, 0x6cd, 0x10); */ /*CCK Tx */
4596 			/* btcoexist->btc_write_1byte(btcoexist, 0x6cf, 0x10); */ /*CCK Rx */
4597 			btcoexist->btc_write_1byte(btcoexist, 0x6cd,
4598 						   0x00); /* CCK Tx */
4599 			btcoexist->btc_write_1byte(btcoexist, 0x6cf,
4600 						   0x10); /* CCK Rx */
4601 		}
4602 	} else {
4603 
4604 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4605 			    "[BTCoex], MEDIA disconnect notify\n");
4606 		BTC_TRACE(trace_buf);
4607 
4608 		halbtc8723d1ant_post_state_to_bt(btcoexist,
4609 				 BT_8723D_1ANT_SCOREBOARD_ACTIVE, FALSE);
4610 
4611 		btcoexist->btc_write_1byte(btcoexist, 0x6cd, 0x0); /* CCK Tx */
4612 		btcoexist->btc_write_1byte(btcoexist, 0x6cf, 0x0); /* CCK Rx */
4613 
4614 		coex_sta->cck_lock_ever = FALSE;
4615 	}
4616 	btcoexist->btc_get(btcoexist, BTC_GET_U1_IOT_PEER, &coex_sta->wl_iot_peer);
4617 	halbtc8723d1ant_update_wifi_channel_info(btcoexist, type);
4618 
4619 }
4620 
ex_halbtc8723d1ant_specific_packet_notify(IN struct btc_coexist * btcoexist,IN u8 type)4621 void ex_halbtc8723d1ant_specific_packet_notify(IN struct btc_coexist *btcoexist,
4622 		IN u8 type)
4623 {
4624 	boolean	under_4way = FALSE;
4625 	struct  btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
4626 
4627 	if (btcoexist->manual_control ||
4628 	    btcoexist->stop_coex_dm)
4629 		return;
4630 
4631 
4632 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_4_WAY_PROGRESS,
4633 			   &under_4way);
4634 
4635 	if (under_4way) {
4636 
4637 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4638 			    "[BTCoex], specific Packet ---- under_4way!!\n");
4639 		BTC_TRACE(trace_buf);
4640 
4641 		coex_sta->wifi_is_high_pri_task = TRUE;
4642 		coex_sta->specific_pkt_period_cnt = 2;
4643 	} else if (BTC_PACKET_ARP == type) {
4644 
4645 		coex_dm->arp_cnt++;
4646 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4647 			    "[BTCoex], specific Packet ARP notify -cnt = %d\n",
4648 			    coex_dm->arp_cnt);
4649 		BTC_TRACE(trace_buf);
4650 
4651 		coex_sta->specific_pkt_period_cnt = 1;
4652 
4653 		if (bt_link_info->sco_exist)
4654 			halbtc8723d1ant_run_coexist_mechanism(btcoexist);
4655 	} else {
4656 
4657 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4658 			"[BTCoex], specific Packet DHCP or EAPOL notify [Type = %d]\n",
4659 			    type);
4660 		BTC_TRACE(trace_buf);
4661 
4662 		coex_sta->wifi_is_high_pri_task = TRUE;
4663 		coex_sta->specific_pkt_period_cnt = 2;
4664 	}
4665 
4666 	if (coex_sta->wifi_is_high_pri_task || coex_sta->wifi_in_scan_task) {
4667 		halbtc8723d1ant_post_state_to_bt(btcoexist,
4668 						 BT_8723D_1ANT_SCOREBOARD_SCAN,
4669 						 TRUE);
4670 		halbtc8723d1ant_run_coexist_mechanism(btcoexist);
4671 	}
4672 }
4673 
ex_halbtc8723d1ant_bt_info_notify(IN struct btc_coexist * btcoexist,IN u8 * tmp_buf,IN u8 length)4674 void ex_halbtc8723d1ant_bt_info_notify(IN struct btc_coexist *btcoexist,
4675 				       IN u8 *tmp_buf, IN u8 length)
4676 {
4677 	struct btc_bt_link_info *bt_link_info = &btcoexist->bt_link_info;
4678 	u8 i, rsp_source = 0;
4679 	boolean	wifi_connected = FALSE;
4680 	boolean	wifi_scan = FALSE, wifi_link = FALSE, wifi_roam = FALSE,
4681 		wifi_busy = FALSE;
4682 	static boolean is_scoreboard_scan = FALSE;
4683 
4684 	if (psd_scan->is_antdet_running) {
4685 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4686 			"[BTCoex], bt_info_notify return for AntDet is running\n");
4687 		BTC_TRACE(trace_buf);
4688 		return;
4689 	}
4690 
4691 	rsp_source = tmp_buf[0] & 0xf;
4692 	if (rsp_source >= BT_INFO_SRC_8723D_1ANT_MAX)
4693 		rsp_source = BT_INFO_SRC_8723D_1ANT_WIFI_FW;
4694 	coex_sta->bt_info_c2h_cnt[rsp_source]++;
4695 
4696 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4697 		    "[BTCoex], Bt_info[%d], len=%d, data=[", rsp_source,
4698 		    length);
4699 	BTC_TRACE(trace_buf);
4700 
4701 	for (i = 0; i < length; i++) {
4702 		coex_sta->bt_info_c2h[rsp_source][i] = tmp_buf[i];
4703 
4704 		if (i == length - 1) {
4705 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE, "0x%02x]\n",
4706 				    tmp_buf[i]);
4707 			BTC_TRACE(trace_buf);
4708 		} else {
4709 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE, "0x%02x, ",
4710 				    tmp_buf[i]);
4711 			BTC_TRACE(trace_buf);
4712 		}
4713 	}
4714 
4715 	coex_sta->bt_info = coex_sta->bt_info_c2h[rsp_source][1];
4716 	coex_sta->bt_info_ext = coex_sta->bt_info_c2h[rsp_source][4];
4717 	coex_sta->bt_info_ext2 = coex_sta->bt_info_c2h[rsp_source][5];
4718 
4719 	if (BT_INFO_SRC_8723D_1ANT_WIFI_FW != rsp_source) {
4720 
4721 		/* if 0xff, it means BT is under WHCK test */
4722 		coex_sta->bt_whck_test = ((coex_sta->bt_info == 0xff) ? TRUE :
4723 					  FALSE);
4724 
4725 		coex_sta->bt_create_connection = ((
4726 			coex_sta->bt_info_c2h[rsp_source][2] & 0x80) ? TRUE :
4727 						  FALSE);
4728 
4729 		/* unit: %, value-100 to translate to unit: dBm */
4730 		coex_sta->bt_rssi = coex_sta->bt_info_c2h[rsp_source][3] * 2 +
4731 				    10;
4732 
4733 		coex_sta->c2h_bt_remote_name_req = ((
4734 			coex_sta->bt_info_c2h[rsp_source][2] & 0x20) ? TRUE :
4735 						    FALSE);
4736 
4737 		coex_sta->is_A2DP_3M = ((coex_sta->bt_info_c2h[rsp_source][2] &
4738 					 0x10) ? TRUE : FALSE);
4739 
4740 		coex_sta->acl_busy = ((coex_sta->bt_info_c2h[rsp_source][1] &
4741 				       0x8) ? TRUE : FALSE);
4742 
4743 		coex_sta->voice_over_HOGP = ((coex_sta->bt_info_ext & 0x10) ?
4744 					     TRUE : FALSE);
4745 
4746 		coex_sta->c2h_bt_inquiry_page = ((coex_sta->bt_info &
4747 			  BT_INFO_8723D_1ANT_B_INQ_PAGE) ? TRUE : FALSE);
4748 
4749 		if (coex_sta->bt_inq_page_pre != coex_sta->c2h_bt_inquiry_page) {
4750 			coex_sta->bt_inq_page_pre = coex_sta->c2h_bt_inquiry_page;
4751 			coex_sta->bt_inq_page_remain = TRUE;
4752 
4753 			if (!coex_sta->c2h_bt_inquiry_page)
4754 				coex_sta->bt_inq_page_downcount = 2;
4755 		}
4756 
4757 		coex_sta->a2dp_bit_pool = (((
4758 			coex_sta->bt_info_c2h[rsp_source][1] & 0x49) == 0x49) ?
4759 			(coex_sta->bt_info_c2h[rsp_source][6]  & 0x7f) : 0);
4760 
4761 		coex_sta->bt_mesh_on = ((coex_sta->bt_info_c2h[rsp_source][2]
4762 					   & 0x40) ? TRUE : FALSE);
4763 
4764 		coex_sta->is_bt_a2dp_sink = (coex_sta->bt_info_c2h[rsp_source][6] & 0x80) ?
4765 									TRUE : FALSE;
4766 
4767 		coex_sta->bt_retry_cnt = coex_sta->bt_info_c2h[rsp_source][2] &
4768 					 0xf;
4769 
4770 		bt_link_info->slave_role  = coex_sta->bt_info_ext2 & 0x8;
4771 
4772 		coex_sta->forbidden_slot = coex_sta->bt_info_ext2 & 0x7;
4773 
4774 		coex_sta->hid_busy_num = (coex_sta->bt_info_ext2 & 0x30) >> 4;
4775 
4776 		coex_sta->hid_pair_cnt = (coex_sta->bt_info_ext2 & 0xc0) >> 6;
4777 
4778 		coex_sta->is_bt_opp_exist = (coex_sta->bt_info_ext2 & 0x1) ? TRUE : FALSE;
4779 
4780 		if (coex_sta->bt_retry_cnt >= 1)
4781 			coex_sta->pop_event_cnt++;
4782 
4783 		if (coex_sta->c2h_bt_remote_name_req)
4784 			coex_sta->cnt_remotenamereq++;
4785 
4786 		if (coex_sta->bt_info_ext & BIT(1))
4787 			coex_sta->cnt_reinit++;
4788 
4789 		if (coex_sta->bt_info_ext & BIT(2) ||
4790 		    (coex_sta->bt_create_connection &&
4791 		    coex_sta->pnp_awake_period_cnt > 0)) {
4792 			coex_sta->cnt_setuplink++;
4793 			coex_sta->is_setup_link = TRUE;
4794 			coex_sta->bt_relink_downcount = 2;
4795 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4796 				    "[BTCoex], Re-Link start in BT info!!\n");
4797 			BTC_TRACE(trace_buf);
4798 		}
4799 
4800 		if (coex_sta->bt_info_ext & BIT(3))
4801 			coex_sta->cnt_ignwlanact++;
4802 
4803 		if (coex_sta->bt_info_ext & BIT(6))
4804 			coex_sta->cnt_roleswitch++;
4805 
4806 		if (coex_sta->bt_info_ext & BIT(7))
4807 			coex_sta->is_bt_multi_link = TRUE;
4808 		else
4809 			coex_sta->is_bt_multi_link = FALSE;
4810 
4811 		if (coex_sta->bt_info_ext & BIT(0))
4812 			coex_sta->is_hid_rcu = TRUE;
4813 		else
4814 			coex_sta->is_hid_rcu = FALSE;
4815 
4816 		if (coex_sta->bt_info_ext & BIT(5))
4817 			coex_sta->is_ble_scan_en = TRUE;
4818 		else
4819 			coex_sta->is_ble_scan_en = FALSE;
4820 
4821 		if (coex_sta->bt_create_connection) {
4822 			coex_sta->cnt_page++;
4823 
4824 			btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_BUSY,
4825 					   &wifi_busy);
4826 
4827 			btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_SCAN, &wifi_scan);
4828 			btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_LINK, &wifi_link);
4829 			btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_ROAM, &wifi_roam);
4830 
4831 			if (wifi_link || wifi_roam ||
4832 			    coex_sta->wifi_in_scan_task ||
4833 			    coex_sta->wifi_is_high_pri_task || wifi_busy) {
4834 
4835 				is_scoreboard_scan = TRUE;
4836 				halbtc8723d1ant_post_state_to_bt(btcoexist,
4837 					 BT_8723D_1ANT_SCOREBOARD_SCAN, TRUE);
4838 
4839 			} else
4840 				halbtc8723d1ant_post_state_to_bt(btcoexist,
4841 					 BT_8723D_1ANT_SCOREBOARD_SCAN, FALSE);
4842 
4843 		} else {
4844 				if (is_scoreboard_scan) {
4845 					halbtc8723d1ant_post_state_to_bt(btcoexist,
4846 						 BT_8723D_1ANT_SCOREBOARD_SCAN, FALSE);
4847 					is_scoreboard_scan = FALSE;
4848 				}
4849 		}
4850 
4851 		/* Here we need to resend some wifi info to BT */
4852 		/* because bt is reset and loss of the info. */
4853 
4854 		if ((!btcoexist->manual_control) &&
4855 		    (!btcoexist->stop_coex_dm)) {
4856 
4857 			btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
4858 					   &wifi_connected);
4859 
4860 			/*	Re-Init */
4861 			if ((coex_sta->bt_info_ext & BIT(1))) {
4862 				BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4863 					"[BTCoex], BT ext info bit1 check, send wifi BW&Chnl to BT!!\n");
4864 				BTC_TRACE(trace_buf);
4865 				if (wifi_connected)
4866 					halbtc8723d1ant_update_wifi_channel_info(
4867 						btcoexist, BTC_MEDIA_CONNECT);
4868 				else
4869 					halbtc8723d1ant_update_wifi_channel_info(
4870 						btcoexist,
4871 						BTC_MEDIA_DISCONNECT);
4872 			}
4873 
4874 
4875 	  /*	If Ignore_WLanAct && not SetUp_Link or Role_Switch */
4876 			if ((coex_sta->bt_info_ext & BIT(3)) &&
4877 				(!(coex_sta->bt_info_ext & BIT(2))) &&
4878 				(!(coex_sta->bt_info_ext & BIT(6)))) {
4879 
4880 				BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4881 					"[BTCoex], BT ext info bit3 check, set BT NOT to ignore Wlan active!!\n");
4882 				BTC_TRACE(trace_buf);
4883 				halbtc8723d1ant_ignore_wlan_act(btcoexist,
4884 							FORCE_EXEC, FALSE);
4885 			} else {
4886 				if (coex_sta->bt_info_ext & BIT(2)) {
4887 					BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4888 					"[BTCoex], BT ignore Wlan active because Re-link!!\n");
4889 					BTC_TRACE(trace_buf);
4890 				} else if (coex_sta->bt_info_ext & BIT(6)) {
4891 					BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4892 					"[BTCoex], BT ignore Wlan active because Role-Switch!!\n");
4893 					BTC_TRACE(trace_buf);
4894 				}
4895 			}
4896 		}
4897 
4898 	}
4899 
4900 	halbtc8723d1ant_update_bt_link_info(btcoexist);
4901 
4902 	halbtc8723d1ant_run_coexist_mechanism(btcoexist);
4903 }
4904 
ex_halbtc8723d1ant_wl_fwdbginfo_notify(IN struct btc_coexist * btcoexist,IN u8 * tmp_buf,IN u8 length)4905 void ex_halbtc8723d1ant_wl_fwdbginfo_notify(IN struct btc_coexist *btcoexist,
4906 				       IN u8 *tmp_buf, IN u8 length)
4907 {
4908 	u8 i = 0;
4909 	static u8 tmp_buf_pre[10], cnt;
4910 	u8 h2c_parameter[2] = {0};
4911 
4912 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4913 		    "[BTCoex], WiFi Fw Dbg info = %d %d %d %d %d %d %d %d (len = %d)\n",
4914 		    tmp_buf[0], tmp_buf[1], tmp_buf[2], tmp_buf[3], tmp_buf[4],
4915 		    tmp_buf[5], tmp_buf[6], tmp_buf[7], length);
4916 	BTC_TRACE(trace_buf);
4917 
4918 	if (tmp_buf[0] == 0x8) {
4919 		for (i = 1; i <= 7; i++) {
4920 			coex_sta->wl_fw_dbg_info[i] =
4921 				(tmp_buf[i] >= tmp_buf_pre[i]) ?
4922 					(tmp_buf[i] - tmp_buf_pre[i]) :
4923 					(255 - tmp_buf_pre[i] + tmp_buf[i]);
4924 
4925 			tmp_buf_pre[i] = tmp_buf[i];
4926 		}
4927 	}
4928 
4929 	if (!coex_sta->is_no_wl_5ms_extend && coex_sta->force_lps_ctrl &&
4930 	    !coex_sta->cck_lock_ever) {
4931 		if (coex_sta->wl_fw_dbg_info[7] <= 5)
4932 			cnt++;
4933 		else
4934 			cnt = 0;
4935 
4936 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4937 			    "[BTCoex], 5ms WL slot extend cnt = %d!!\n", cnt);
4938 		BTC_TRACE(trace_buf);
4939 
4940 		if (cnt == 7) {
4941 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4942 				    "[BTCoex], set h2c 0x69 opcode 12 to turn off 5ms WL slot extend!!\n");
4943 			BTC_TRACE(trace_buf);
4944 
4945 			h2c_parameter[0] = 0xc;
4946 			h2c_parameter[1] = 0x1;
4947 			btcoexist->btc_fill_h2c(btcoexist, 0x69, 2,
4948 						h2c_parameter);
4949 			coex_sta->is_no_wl_5ms_extend = TRUE;
4950 			cnt = 0;
4951 		}
4952 	}
4953 
4954 	if (coex_sta->is_no_wl_5ms_extend && coex_sta->cck_lock) {
4955 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4956 			    "[BTCoex], set h2c 0x69 opcode 12 to turn on 5ms WL slot extend!!\n");
4957 		BTC_TRACE(trace_buf);
4958 
4959 		h2c_parameter[0] = 0xc;
4960 		h2c_parameter[1] = 0x0;
4961 		btcoexist->btc_fill_h2c(btcoexist, 0x69, 2, h2c_parameter);
4962 		coex_sta->is_no_wl_5ms_extend = FALSE;
4963 	}
4964 }
4965 
ex_halbtc8723d1ant_rx_rate_change_notify(IN struct btc_coexist * btcoexist,IN BOOLEAN is_data_frame,IN u8 btc_rate_id)4966 void ex_halbtc8723d1ant_rx_rate_change_notify(IN struct btc_coexist *btcoexist,
4967 		IN BOOLEAN is_data_frame, IN u8 btc_rate_id)
4968 {
4969 	BOOLEAN wifi_connected = FALSE;
4970 
4971 	btcoexist->btc_get(btcoexist, BTC_GET_BL_WIFI_CONNECTED,
4972 			   &wifi_connected);
4973 
4974 	if (is_data_frame) {
4975 		coex_sta->wl_rx_rate = btc_rate_id;
4976 
4977 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4978 			"[BTCoex], rx_rate_change_notify data rate id = %d, RTS_Rate = %d\n",
4979 			coex_sta->wl_rx_rate, coex_sta->wl_rts_rx_rate);
4980 		BTC_TRACE(trace_buf);
4981 	} else {
4982 		coex_sta->wl_rts_rx_rate = btc_rate_id;
4983 
4984 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
4985 			"[BTCoex], rts_rate_change_notify RTS rate id = %d, RTS_Rate = %d\n",
4986 			coex_sta->wl_rts_rx_rate, coex_sta->wl_rts_rx_rate);
4987 		BTC_TRACE(trace_buf);
4988 	}
4989 
4990 	if (wifi_connected &&
4991 	    (coex_dm->bt_status ==  BT_8723D_1ANT_BT_STATUS_ACL_BUSY ||
4992 	     coex_dm->bt_status ==  BT_8723D_1ANT_BT_STATUS_ACL_SCO_BUSY ||
4993 	     coex_dm->bt_status == BT_8723D_1ANT_BT_STATUS_SCO_BUSY)) {
4994 
4995 		if (coex_sta->wl_rx_rate == BTC_CCK_5_5 ||
4996 		    coex_sta->wl_rx_rate == BTC_OFDM_6 ||
4997 		    coex_sta->wl_rx_rate == BTC_MCS_0) {
4998 
4999 			coex_sta->cck_lock_warn = TRUE;
5000 
5001 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5002 				"[BTCoex], cck lock warning...\n");
5003 			BTC_TRACE(trace_buf);
5004 		} else if (coex_sta->wl_rx_rate == BTC_CCK_1 ||
5005 			   coex_sta->wl_rx_rate == BTC_CCK_2 ||
5006 			   coex_sta->wl_rts_rx_rate == BTC_CCK_1 ||
5007 			   coex_sta->wl_rts_rx_rate == BTC_CCK_2) {
5008 
5009 			coex_sta->cck_lock = TRUE;
5010 			coex_sta->cck_lock_ever = TRUE;
5011 
5012 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5013 				"[BTCoex], cck locking...\n");
5014 			BTC_TRACE(trace_buf);
5015 		} else {
5016 			coex_sta->cck_lock_warn = FALSE;
5017 			coex_sta->cck_lock = FALSE;
5018 
5019 			BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5020 				"[BTCoex], cck unlock...\n");
5021 			BTC_TRACE(trace_buf);
5022 		}
5023 	} else {
5024 		if (coex_dm->bt_status ==
5025 		    BT_8723D_1ANT_BT_STATUS_CONNECTED_IDLE ||
5026 		    coex_dm->bt_status ==
5027 		    BT_8723D_1ANT_BT_STATUS_NON_CONNECTED_IDLE) {
5028 			coex_sta->cck_lock_warn = FALSE;
5029 			coex_sta->cck_lock = FALSE;
5030 		}
5031 	}
5032 
5033 }
5034 
5035 
5036 
ex_halbtc8723d1ant_rf_status_notify(IN struct btc_coexist * btcoexist,IN u8 type)5037 void ex_halbtc8723d1ant_rf_status_notify(IN struct btc_coexist *btcoexist,
5038 		IN u8 type)
5039 {
5040 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE, "[BTCoex], RF Status notify\n");
5041 	BTC_TRACE(trace_buf);
5042 
5043 	if (BTC_RF_ON == type) {
5044 
5045 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5046 			    "[BTCoex], RF is turned ON!!\n");
5047 		BTC_TRACE(trace_buf);
5048 
5049 		btcoexist->stop_coex_dm = FALSE;
5050 		coex_sta->is_rf_state_off = FALSE;
5051 #if 0
5052 		halbtc8723d1ant_post_state_to_bt(btcoexist,
5053 					 BT_8723D_1ANT_SCOREBOARD_ACTIVE, TRUE);
5054 		halbtc8723d1ant_post_state_to_bt(btcoexist,
5055 					 BT_8723D_1ANT_SCOREBOARD_ONOFF, TRUE);
5056 #endif
5057 
5058 	} else if (BTC_RF_OFF == type) {
5059 
5060 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5061 			    "[BTCoex], RF is turned OFF!!\n");
5062 		BTC_TRACE(trace_buf);
5063 
5064 		halbtc8723d1ant_post_state_to_bt(btcoexist,
5065 				BT_8723D_1ANT_SCOREBOARD_ACTIVE |
5066 				BT_8723D_1ANT_SCOREBOARD_ONOFF |
5067 				BT_8723D_1ANT_SCOREBOARD_SCAN |
5068 				BT_8723D_1ANT_SCOREBOARD_UNDERTEST,
5069 				FALSE);
5070 
5071 		halbtc8723d1ant_set_ant_path(btcoexist, BTC_ANT_PATH_AUTO,
5072 					     FORCE_EXEC,
5073 					     BT_8723D_1ANT_PHASE_WLAN_OFF);
5074 
5075 		halbtc8723d1ant_ps_tdma(btcoexist, FORCE_EXEC, FALSE, 0);
5076 
5077 		btcoexist->stop_coex_dm = TRUE;
5078 		coex_sta->is_rf_state_off = TRUE;
5079 	}
5080 }
5081 
ex_halbtc8723d1ant_halt_notify(IN struct btc_coexist * btcoexist)5082 void ex_halbtc8723d1ant_halt_notify(IN struct btc_coexist *btcoexist)
5083 {
5084 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE, "[BTCoex], Halt notify\n");
5085 	BTC_TRACE(trace_buf);
5086 
5087 	halbtc8723d1ant_post_state_to_bt(btcoexist,
5088 				BT_8723D_1ANT_SCOREBOARD_ACTIVE |
5089 				BT_8723D_1ANT_SCOREBOARD_ONOFF |
5090 				BT_8723D_1ANT_SCOREBOARD_SCAN |
5091 				BT_8723D_1ANT_SCOREBOARD_UNDERTEST,
5092 				FALSE);
5093 
5094 	halbtc8723d1ant_set_ant_path(btcoexist, BTC_ANT_PATH_AUTO, FORCE_EXEC,
5095 				     BT_8723D_1ANT_PHASE_WLAN_OFF);
5096 
5097 	/*halbtc8723d1ant_ignore_wlan_act(btcoexist, FORCE_EXEC, TRUE);*/
5098 
5099 	ex_halbtc8723d1ant_media_status_notify(btcoexist, BTC_MEDIA_DISCONNECT);
5100 
5101 	halbtc8723d1ant_ps_tdma(btcoexist, FORCE_EXEC, FALSE, 0);
5102 
5103 	btcoexist->stop_coex_dm = TRUE;
5104 }
5105 
ex_halbtc8723d1ant_pnp_notify(IN struct btc_coexist * btcoexist,IN u8 pnp_state)5106 void ex_halbtc8723d1ant_pnp_notify(IN struct btc_coexist *btcoexist,
5107 				   IN u8 pnp_state)
5108 {
5109 	static u8 pre_pnp_state;
5110 
5111 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE, "[BTCoex], Pnp notify\n");
5112 	BTC_TRACE(trace_buf);
5113 
5114 	if ((BTC_WIFI_PNP_SLEEP == pnp_state) ||
5115 	    (BTC_WIFI_PNP_SLEEP_KEEP_ANT == pnp_state)) {
5116 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5117 			    "[BTCoex], Pnp notify to SLEEP\n");
5118 		BTC_TRACE(trace_buf);
5119 
5120 		coex_sta->under_ips = FALSE;
5121 		coex_sta->under_lps = FALSE;
5122 
5123 		halbtc8723d1ant_post_state_to_bt(btcoexist,
5124 				BT_8723D_1ANT_SCOREBOARD_ACTIVE |
5125 				BT_8723D_1ANT_SCOREBOARD_ONOFF |
5126 				BT_8723D_1ANT_SCOREBOARD_SCAN |
5127 				BT_8723D_1ANT_SCOREBOARD_UNDERTEST,
5128 				FALSE);
5129 
5130 		if (BTC_WIFI_PNP_SLEEP_KEEP_ANT == pnp_state) {
5131 
5132 			halbtc8723d1ant_set_ant_path(btcoexist, BTC_ANT_PATH_AUTO,
5133 						     FORCE_EXEC,
5134 					     BT_8723D_1ANT_PHASE_2G_RUNTIME);
5135 		} else {
5136 
5137 			halbtc8723d1ant_set_ant_path(btcoexist, BTC_ANT_PATH_AUTO,
5138 						     FORCE_EXEC,
5139 					     BT_8723D_1ANT_PHASE_WLAN_OFF);
5140 		}
5141 
5142 		btcoexist->stop_coex_dm = TRUE;
5143 	} else {
5144 
5145 		BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5146 			    "[BTCoex], Pnp notify to WAKE UP\n");
5147 		BTC_TRACE(trace_buf);
5148 
5149 		coex_sta->pnp_awake_period_cnt = 3;
5150 
5151 		/*WoWLAN*/
5152 		if (pre_pnp_state == BTC_WIFI_PNP_SLEEP_KEEP_ANT ||
5153 		    pnp_state == BTC_WIFI_PNP_WOWLAN) {
5154 			coex_sta->run_time_state = TRUE;
5155 			btcoexist->stop_coex_dm = FALSE;
5156 			halbtc8723d1ant_run_coexist_mechanism(btcoexist);
5157 		}
5158 #if 0
5159 		halbtc8723d1ant_post_state_to_bt(btcoexist,
5160 					 BT_8723D_1ANT_SCOREBOARD_ACTIVE, TRUE);
5161 		halbtc8723d1ant_post_state_to_bt(btcoexist,
5162 					 BT_8723D_1ANT_SCOREBOARD_ONOFF, TRUE);
5163 #endif
5164 		/*btcoexist->stop_coex_dm = FALSE;*/
5165 	}
5166 
5167 	pre_pnp_state = pnp_state;
5168 }
5169 
5170 
ex_halbtc8723d1ant_coex_dm_reset(IN struct btc_coexist * btcoexist)5171 void ex_halbtc8723d1ant_coex_dm_reset(IN struct btc_coexist *btcoexist)
5172 {
5173 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5174 		"[BTCoex], *****************Coex DM Reset*****************\n");
5175 	BTC_TRACE(trace_buf);
5176 
5177 	halbtc8723d1ant_init_hw_config(btcoexist, FALSE, FALSE);
5178 	halbtc8723d1ant_init_coex_dm(btcoexist);
5179 }
5180 
ex_halbtc8723d1ant_periodical(IN struct btc_coexist * btcoexist)5181 void ex_halbtc8723d1ant_periodical(IN struct btc_coexist *btcoexist)
5182 {
5183 
5184 	struct  btc_board_info	*board_info = &btcoexist->board_info;
5185 	boolean wifi_busy = FALSE, bt_ctr_change = FALSE;
5186 	u4Byte	value = 0;
5187 	u32	bt_patch_ver;
5188 	static u8 cnt = 0;
5189 	boolean bt_relink_finish = FALSE, special_pkt_finish = FALSE;
5190 
5191 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5192 		    "[BTCoex], ************* Periodical *************\n");
5193 	BTC_TRACE(trace_buf);
5194 
5195 #if (BT_AUTO_REPORT_ONLY_8723D_1ANT == 0)
5196 	halbtc8723d1ant_query_bt_info(btcoexist);
5197 
5198 #endif
5199 
5200 	bt_ctr_change = halbtc8723d1ant_monitor_bt_ctr(btcoexist);
5201 	halbtc8723d1ant_monitor_wifi_ctr(btcoexist);
5202 
5203 	halbtc8723d1ant_monitor_bt_enable_disable(btcoexist);
5204 
5205 	if (coex_sta->bt_relink_downcount != 0) {
5206 		coex_sta->bt_relink_downcount--;
5207 
5208 		if (coex_sta->bt_relink_downcount == 0) {
5209 			coex_sta->is_setup_link = FALSE;
5210 			bt_relink_finish = TRUE;
5211 		}
5212 	}
5213 
5214 	if (coex_sta->bt_inq_page_downcount != 0) {
5215 		coex_sta->bt_inq_page_downcount--;
5216 		if (coex_sta->bt_relink_downcount == 0)
5217 			coex_sta->bt_inq_page_remain = FALSE;
5218 	}
5219 
5220 	/* for 4-way, DHCP, EAPOL packet */
5221 	if (coex_sta->specific_pkt_period_cnt > 0) {
5222 
5223 		coex_sta->specific_pkt_period_cnt--;
5224 
5225 		if ((coex_sta->specific_pkt_period_cnt == 0) &&
5226 		    (coex_sta->wifi_is_high_pri_task)) {
5227 			coex_sta->wifi_is_high_pri_task = FALSE;
5228 			special_pkt_finish = TRUE;
5229 		}
5230 	}
5231 
5232 	if (coex_sta->pnp_awake_period_cnt > 0)
5233 		coex_sta->pnp_awake_period_cnt--;
5234 
5235 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5236 		    "[BTCoex], pnp_awake_period_cnt = %d\n",
5237 				coex_sta->pnp_awake_period_cnt);
5238 	BTC_TRACE(trace_buf);
5239 
5240 	/*for A2DP glitch during connecting AP*/
5241 	if (coex_sta->connect_ap_period_cnt > 0)
5242 		coex_sta->connect_ap_period_cnt--;
5243 
5244 	if (halbtc8723d1ant_is_wifibt_status_changed(btcoexist) ||
5245 	    bt_relink_finish || special_pkt_finish || bt_ctr_change)
5246 		halbtc8723d1ant_run_coexist_mechanism(btcoexist);
5247 }
5248 
ex_halbtc8723d1ant_set_antenna_notify(IN struct btc_coexist * btcoexist,IN u8 type)5249 void ex_halbtc8723d1ant_set_antenna_notify(IN struct btc_coexist *btcoexist,
5250 		IN u8 type)
5251 {
5252 	struct  btc_board_info	*board_info = &btcoexist->board_info;
5253 
5254 	if (btcoexist->manual_control || btcoexist->stop_coex_dm)
5255 		return;
5256 
5257 	if (type == 2) { /* two antenna */
5258 		board_info->ant_div_cfg = TRUE;
5259 		halbtc8723d1ant_set_ant_path(btcoexist, BTC_ANT_PATH_WIFI,
5260 					     FORCE_EXEC,
5261 					     BT_8723D_1ANT_PHASE_2G_RUNTIME);
5262 	} else { /* one antenna */
5263 		halbtc8723d1ant_set_ant_path(btcoexist, BTC_ANT_PATH_AUTO,
5264 					     FORCE_EXEC,
5265 					     BT_8723D_1ANT_PHASE_2G_RUNTIME);
5266 	}
5267 }
5268 
5269 #ifdef PLATFORM_WINDOWS
5270 #pragma optimize("", off)
5271 #endif
ex_halbtc8723d1ant_antenna_detection(IN struct btc_coexist * btcoexist,IN u32 cent_freq,IN u32 offset,IN u32 span,IN u32 seconds)5272 void ex_halbtc8723d1ant_antenna_detection(IN struct btc_coexist *btcoexist,
5273 		IN u32 cent_freq, IN u32 offset, IN u32 span, IN u32 seconds)
5274 {
5275 
5276 	static u32 ant_det_count = 0, ant_det_fail_count = 0;
5277 	struct  btc_board_info	*board_info = &btcoexist->board_info;
5278 	u16		u16tmp;
5279 	u8			AntDetval = 0;
5280 
5281 	BTC_SPRINTF(trace_buf, BT_TMP_BUF_SIZE,
5282 		    "xxxxxxxxxxxxxxxx Ext Call AntennaDetect()!!\n");
5283 	BTC_TRACE(trace_buf);
5284 }
5285 
5286 
ex_halbtc8723d1ant_display_ant_detection(IN struct btc_coexist * btcoexist)5287 void ex_halbtc8723d1ant_display_ant_detection(IN struct btc_coexist *btcoexist)
5288 {
5289 #if 0
5290 	struct  btc_board_info	*board_info = &btcoexist->board_info;
5291 
5292 	if (psd_scan->ant_det_try_count != 0)	{
5293 		halbtc8723d1ant_psd_show_antenna_detect_result(btcoexist);
5294 
5295 		if (board_info->btdm_ant_det_finish)
5296 			halbtc8723d1ant_psd_showdata(btcoexist);
5297 	}
5298 #endif
5299 
5300 }
5301 
ex_halbtc8723d1ant_antenna_isolation(IN struct btc_coexist * btcoexist,IN u32 cent_freq,IN u32 offset,IN u32 span,IN u32 seconds)5302 void ex_halbtc8723d1ant_antenna_isolation(IN struct btc_coexist *btcoexist,
5303 		IN u32 cent_freq, IN u32 offset, IN u32 span, IN u32 seconds)
5304 {
5305 
5306 
5307 }
5308 
ex_halbtc8723d1ant_psd_scan(IN struct btc_coexist * btcoexist,IN u32 cent_freq,IN u32 offset,IN u32 span,IN u32 seconds)5309 void ex_halbtc8723d1ant_psd_scan(IN struct btc_coexist *btcoexist,
5310 		 IN u32 cent_freq, IN u32 offset, IN u32 span, IN u32 seconds)
5311 {
5312 
5313 
5314 }
5315 
5316 
5317 #endif
5318 
5319 #endif	/* #if (BT_SUPPORT == 1 && COEX_SUPPORT == 1) */
5320 
5321