blob: e2a0faadc2b064b52d30aaac38cc9db47f1e0e16 [file] [log] [blame]
Chaoming Li9fe255e2011-05-03 09:47:55 -05001/******************************************************************************
2 *
Larry Fingerca742cd2012-01-07 20:46:47 -06003 * Copyright(c) 2009-2012 Realtek Corporation.
Chaoming Li9fe255e2011-05-03 09:47:55 -05004 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
20 *
21 * Contact Information:
22 * wlanfae <wlanfae@realtek.com>
23 * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
24 * Hsinchu 300, Taiwan.
25 *
26 * Larry Finger <Larry.Finger@lwfinger.net>
27 *
28 *****************************************************************************/
29
30#include "../wifi.h"
31#include "../base.h"
32#include "reg.h"
33#include "def.h"
34#include "phy.h"
35#include "dm.h"
36#include "fw.h"
37
Chaoming Li9fe255e2011-05-03 09:47:55 -050038static const u32 edca_setting_dl[PEER_MAX] = {
39 0xa44f, /* 0 UNKNOWN */
40 0x5ea44f, /* 1 REALTEK_90 */
41 0x5ea44f, /* 2 REALTEK_92SE */
42 0xa630, /* 3 BROAD */
43 0xa44f, /* 4 RAL */
44 0xa630, /* 5 ATH */
45 0xa630, /* 6 CISCO */
46 0xa42b, /* 7 MARV */
47};
48
49static const u32 edca_setting_dl_gmode[PEER_MAX] = {
50 0x4322, /* 0 UNKNOWN */
51 0xa44f, /* 1 REALTEK_90 */
52 0x5ea44f, /* 2 REALTEK_92SE */
53 0xa42b, /* 3 BROAD */
54 0x5e4322, /* 4 RAL */
55 0x4322, /* 5 ATH */
56 0xa430, /* 6 CISCO */
57 0x5ea44f, /* 7 MARV */
58};
59
60static const u32 edca_setting_ul[PEER_MAX] = {
61 0x5e4322, /* 0 UNKNOWN */
62 0xa44f, /* 1 REALTEK_90 */
63 0x5ea44f, /* 2 REALTEK_92SE */
64 0x5ea322, /* 3 BROAD */
65 0x5ea422, /* 4 RAL */
66 0x5ea322, /* 5 ATH */
67 0x3ea44f, /* 6 CISCO */
68 0x5ea44f, /* 7 MARV */
69};
70
71static void _rtl92s_dm_check_edca_turbo(struct ieee80211_hw *hw)
72{
73 struct rtl_priv *rtlpriv = rtl_priv(hw);
74 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
75
76 static u64 last_txok_cnt;
77 static u64 last_rxok_cnt;
78 u64 cur_txok_cnt = 0;
79 u64 cur_rxok_cnt = 0;
80
81 u32 edca_be_ul = edca_setting_ul[mac->vendor];
82 u32 edca_be_dl = edca_setting_dl[mac->vendor];
83 u32 edca_gmode = edca_setting_dl_gmode[mac->vendor];
84
85 if (mac->link_state != MAC80211_LINKED) {
86 rtlpriv->dm.current_turbo_edca = false;
87 goto dm_checkedcaturbo_exit;
88 }
89
90 if ((!rtlpriv->dm.is_any_nonbepkts) &&
91 (!rtlpriv->dm.disable_framebursting)) {
92 cur_txok_cnt = rtlpriv->stats.txbytesunicast - last_txok_cnt;
93 cur_rxok_cnt = rtlpriv->stats.rxbytesunicast - last_rxok_cnt;
94
95 if (rtlpriv->phy.rf_type == RF_1T2R) {
96 if (cur_txok_cnt > 4 * cur_rxok_cnt) {
97 /* Uplink TP is present. */
98 if (rtlpriv->dm.is_cur_rdlstate ||
99 !rtlpriv->dm.current_turbo_edca) {
100 rtl_write_dword(rtlpriv, EDCAPARA_BE,
101 edca_be_ul);
102 rtlpriv->dm.is_cur_rdlstate = false;
103 }
104 } else {/* Balance TP is present. */
105 if (!rtlpriv->dm.is_cur_rdlstate ||
106 !rtlpriv->dm.current_turbo_edca) {
107 if (mac->mode == WIRELESS_MODE_G ||
108 mac->mode == WIRELESS_MODE_B)
109 rtl_write_dword(rtlpriv,
110 EDCAPARA_BE,
111 edca_gmode);
112 else
113 rtl_write_dword(rtlpriv,
114 EDCAPARA_BE,
115 edca_be_dl);
116 rtlpriv->dm.is_cur_rdlstate = true;
117 }
118 }
119 rtlpriv->dm.current_turbo_edca = true;
120 } else {
121 if (cur_rxok_cnt > 4 * cur_txok_cnt) {
122 if (!rtlpriv->dm.is_cur_rdlstate ||
123 !rtlpriv->dm.current_turbo_edca) {
124 if (mac->mode == WIRELESS_MODE_G ||
125 mac->mode == WIRELESS_MODE_B)
126 rtl_write_dword(rtlpriv,
127 EDCAPARA_BE,
128 edca_gmode);
129 else
130 rtl_write_dword(rtlpriv,
131 EDCAPARA_BE,
132 edca_be_dl);
133 rtlpriv->dm.is_cur_rdlstate = true;
134 }
135 } else {
136 if (rtlpriv->dm.is_cur_rdlstate ||
137 !rtlpriv->dm.current_turbo_edca) {
138 rtl_write_dword(rtlpriv, EDCAPARA_BE,
139 edca_be_ul);
140 rtlpriv->dm.is_cur_rdlstate = false;
141 }
142 }
143 rtlpriv->dm.current_turbo_edca = true;
144 }
145 } else {
146 if (rtlpriv->dm.current_turbo_edca) {
147 u8 tmp = AC0_BE;
148 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_AC_PARAM,
Joe Perches2c208892012-06-04 12:44:17 +0000149 &tmp);
Chaoming Li9fe255e2011-05-03 09:47:55 -0500150 rtlpriv->dm.current_turbo_edca = false;
151 }
152 }
153
154dm_checkedcaturbo_exit:
155 rtlpriv->dm.is_any_nonbepkts = false;
156 last_txok_cnt = rtlpriv->stats.txbytesunicast;
157 last_rxok_cnt = rtlpriv->stats.rxbytesunicast;
158}
159
160static void _rtl92s_dm_txpowertracking_callback_thermalmeter(
161 struct ieee80211_hw *hw)
162{
163 struct rtl_priv *rtlpriv = rtl_priv(hw);
164 struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
165 u8 thermalvalue = 0;
Larry Finger2455c922013-03-24 22:06:38 -0500166 u32 fw_cmd = 0;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500167
168 rtlpriv->dm.txpower_trackinginit = true;
169
170 thermalvalue = (u8)rtl_get_rfreg(hw, RF90_PATH_A, RF_T_METER, 0x1f);
171
172 RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
Joe Perchesf30d7502012-01-04 19:40:41 -0800173 "Readback Thermal Meter = 0x%x pre thermal meter 0x%x eeprom_thermal meter 0x%x\n",
174 thermalvalue,
175 rtlpriv->dm.thermalvalue, rtlefuse->eeprom_thermalmeter);
Chaoming Li9fe255e2011-05-03 09:47:55 -0500176
177 if (thermalvalue) {
178 rtlpriv->dm.thermalvalue = thermalvalue;
Larry Finger2455c922013-03-24 22:06:38 -0500179 if (hal_get_firmwareversion(rtlpriv) >= 0x35) {
180 rtl92s_phy_set_fw_cmd(hw, FW_CMD_TXPWR_TRACK_THERMAL);
181 } else {
182 fw_cmd = (FW_TXPWR_TRACK_THERMAL |
183 (rtlpriv->efuse.thermalmeter[0] << 8) |
184 (thermalvalue << 16));
185
186 RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
187 "Write to FW Thermal Val = 0x%x\n", fw_cmd);
188
189 rtl_write_dword(rtlpriv, WFM5, fw_cmd);
190 rtl92s_phy_chk_fwcmd_iodone(hw);
191 }
Chaoming Li9fe255e2011-05-03 09:47:55 -0500192 }
193
194 rtlpriv->dm.txpowercount = 0;
195}
196
197static void _rtl92s_dm_check_txpowertracking_thermalmeter(
198 struct ieee80211_hw *hw)
199{
200 struct rtl_priv *rtlpriv = rtl_priv(hw);
201 struct rtl_phy *rtlphy = &(rtlpriv->phy);
202 static u8 tm_trigger;
203 u8 tx_power_checkcnt = 5;
204
205 /* 2T2R TP issue */
206 if (rtlphy->rf_type == RF_2T2R)
207 return;
208
209 if (!rtlpriv->dm.txpower_tracking)
210 return;
211
212 if (rtlpriv->dm.txpowercount <= tx_power_checkcnt) {
213 rtlpriv->dm.txpowercount++;
214 return;
215 }
216
217 if (!tm_trigger) {
218 rtl_set_rfreg(hw, RF90_PATH_A, RF_T_METER,
219 RFREG_OFFSET_MASK, 0x60);
220 tm_trigger = 1;
221 } else {
222 _rtl92s_dm_txpowertracking_callback_thermalmeter(hw);
223 tm_trigger = 0;
224 }
225}
226
227static void _rtl92s_dm_refresh_rateadaptive_mask(struct ieee80211_hw *hw)
228{
229 struct rtl_priv *rtlpriv = rtl_priv(hw);
230 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
231 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
232 struct rate_adaptive *ra = &(rtlpriv->ra);
Larry Finger2455c922013-03-24 22:06:38 -0500233 struct ieee80211_sta *sta = NULL;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500234 u32 low_rssi_thresh = 0;
235 u32 middle_rssi_thresh = 0;
236 u32 high_rssi_thresh = 0;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500237
238 if (is_hal_stop(rtlhal))
239 return;
240
241 if (!rtlpriv->dm.useramask)
242 return;
243
Larry Finger2455c922013-03-24 22:06:38 -0500244 if (hal_get_firmwareversion(rtlpriv) >= 61 &&
245 !rtlpriv->dm.inform_fw_driverctrldm) {
Chaoming Li9fe255e2011-05-03 09:47:55 -0500246 rtl92s_phy_set_fw_cmd(hw, FW_CMD_CTRL_DM_BY_DRIVER);
247 rtlpriv->dm.inform_fw_driverctrldm = true;
248 }
249
Chaoming Li9fe255e2011-05-03 09:47:55 -0500250 if ((mac->link_state == MAC80211_LINKED) &&
251 (mac->opmode == NL80211_IFTYPE_STATION)) {
252 switch (ra->pre_ratr_state) {
253 case DM_RATR_STA_HIGH:
254 high_rssi_thresh = 40;
255 middle_rssi_thresh = 30;
256 low_rssi_thresh = 20;
257 break;
258 case DM_RATR_STA_MIDDLE:
259 high_rssi_thresh = 44;
260 middle_rssi_thresh = 30;
261 low_rssi_thresh = 20;
262 break;
263 case DM_RATR_STA_LOW:
264 high_rssi_thresh = 44;
265 middle_rssi_thresh = 34;
266 low_rssi_thresh = 20;
267 break;
268 case DM_RATR_STA_ULTRALOW:
269 high_rssi_thresh = 44;
270 middle_rssi_thresh = 34;
271 low_rssi_thresh = 24;
272 break;
273 default:
274 high_rssi_thresh = 44;
275 middle_rssi_thresh = 34;
276 low_rssi_thresh = 24;
277 break;
278 }
279
Larry Fingerda17fcf2012-10-25 13:46:31 -0500280 if (rtlpriv->dm.undec_sm_pwdb > (long)high_rssi_thresh) {
Chaoming Li9fe255e2011-05-03 09:47:55 -0500281 ra->ratr_state = DM_RATR_STA_HIGH;
Larry Fingerda17fcf2012-10-25 13:46:31 -0500282 } else if (rtlpriv->dm.undec_sm_pwdb >
Chaoming Li9fe255e2011-05-03 09:47:55 -0500283 (long)middle_rssi_thresh) {
284 ra->ratr_state = DM_RATR_STA_LOW;
Larry Fingerda17fcf2012-10-25 13:46:31 -0500285 } else if (rtlpriv->dm.undec_sm_pwdb >
Chaoming Li9fe255e2011-05-03 09:47:55 -0500286 (long)low_rssi_thresh) {
287 ra->ratr_state = DM_RATR_STA_LOW;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500288 } else {
289 ra->ratr_state = DM_RATR_STA_ULTRALOW;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500290 }
291
292 if (ra->pre_ratr_state != ra->ratr_state) {
Joe Perchesf30d7502012-01-04 19:40:41 -0800293 RT_TRACE(rtlpriv, COMP_RATE, DBG_LOUD,
294 "RSSI = %ld RSSI_LEVEL = %d PreState = %d, CurState = %d\n",
Larry Fingerda17fcf2012-10-25 13:46:31 -0500295 rtlpriv->dm.undec_sm_pwdb, ra->ratr_state,
Joe Perchesf30d7502012-01-04 19:40:41 -0800296 ra->pre_ratr_state, ra->ratr_state);
Chaoming Li9fe255e2011-05-03 09:47:55 -0500297
Larry Finger2455c922013-03-24 22:06:38 -0500298 rcu_read_lock();
299 sta = rtl_find_sta(hw, mac->bssid);
300 if (sta)
301 rtlpriv->cfg->ops->update_rate_tbl(hw, sta,
Chaoming Li9fe255e2011-05-03 09:47:55 -0500302 ra->ratr_state);
Larry Finger2455c922013-03-24 22:06:38 -0500303 rcu_read_unlock();
304
Chaoming Li9fe255e2011-05-03 09:47:55 -0500305 ra->pre_ratr_state = ra->ratr_state;
306 }
307 }
Chaoming Li9fe255e2011-05-03 09:47:55 -0500308}
309
310static void _rtl92s_dm_switch_baseband_mrc(struct ieee80211_hw *hw)
311{
312 struct rtl_priv *rtlpriv = rtl_priv(hw);
313 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
314 struct rtl_phy *rtlphy = &(rtlpriv->phy);
315 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
316 bool current_mrc;
317 bool enable_mrc = true;
318 long tmpentry_maxpwdb = 0;
319 u8 rssi_a = 0;
320 u8 rssi_b = 0;
321
322 if (is_hal_stop(rtlhal))
323 return;
324
325 if ((rtlphy->rf_type == RF_1T1R) || (rtlphy->rf_type == RF_2T2R))
326 return;
327
328 rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_MRC, (u8 *)(&current_mrc));
329
330 if (mac->link_state >= MAC80211_LINKED) {
Larry Fingerda17fcf2012-10-25 13:46:31 -0500331 if (rtlpriv->dm.undec_sm_pwdb > tmpentry_maxpwdb) {
Chaoming Li9fe255e2011-05-03 09:47:55 -0500332 rssi_a = rtlpriv->stats.rx_rssi_percentage[RF90_PATH_A];
333 rssi_b = rtlpriv->stats.rx_rssi_percentage[RF90_PATH_B];
334 }
335 }
336
337 /* MRC settings would NOT affect TP on Wireless B mode. */
338 if (mac->mode != WIRELESS_MODE_B) {
339 if ((rssi_a == 0) && (rssi_b == 0)) {
340 enable_mrc = true;
341 } else if (rssi_b > 30) {
342 /* Turn on B-Path */
343 enable_mrc = true;
344 } else if (rssi_b < 5) {
345 /* Turn off B-path */
346 enable_mrc = false;
347 /* Take care of RSSI differentiation. */
348 } else if (rssi_a > 15 && (rssi_a >= rssi_b)) {
349 if ((rssi_a - rssi_b) > 15)
350 /* Turn off B-path */
351 enable_mrc = false;
352 else if ((rssi_a - rssi_b) < 10)
353 /* Turn on B-Path */
354 enable_mrc = true;
355 else
356 enable_mrc = current_mrc;
357 } else {
358 /* Turn on B-Path */
359 enable_mrc = true;
360 }
361 }
362
363 /* Update MRC settings if needed. */
364 if (enable_mrc != current_mrc)
365 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_MRC,
366 (u8 *)&enable_mrc);
367
368}
369
370void rtl92s_dm_init_edca_turbo(struct ieee80211_hw *hw)
371{
372 struct rtl_priv *rtlpriv = rtl_priv(hw);
373
374 rtlpriv->dm.current_turbo_edca = false;
375 rtlpriv->dm.is_any_nonbepkts = false;
376 rtlpriv->dm.is_cur_rdlstate = false;
377}
378
379static void _rtl92s_dm_init_rate_adaptive_mask(struct ieee80211_hw *hw)
380{
381 struct rtl_priv *rtlpriv = rtl_priv(hw);
382 struct rate_adaptive *ra = &(rtlpriv->ra);
383
384 ra->ratr_state = DM_RATR_STA_MAX;
385 ra->pre_ratr_state = DM_RATR_STA_MAX;
386
Larry Finger2455c922013-03-24 22:06:38 -0500387 if (rtlpriv->dm.dm_type == DM_TYPE_BYDRIVER &&
388 hal_get_firmwareversion(rtlpriv) >= 60)
Chaoming Li9fe255e2011-05-03 09:47:55 -0500389 rtlpriv->dm.useramask = true;
390 else
391 rtlpriv->dm.useramask = false;
392
393 rtlpriv->dm.useramask = false;
394 rtlpriv->dm.inform_fw_driverctrldm = false;
395}
396
397static void _rtl92s_dm_init_txpowertracking_thermalmeter(
398 struct ieee80211_hw *hw)
399{
400 struct rtl_priv *rtlpriv = rtl_priv(hw);
401
402 rtlpriv->dm.txpower_tracking = true;
403 rtlpriv->dm.txpowercount = 0;
404 rtlpriv->dm.txpower_trackinginit = false;
405}
406
407static void _rtl92s_dm_false_alarm_counter_statistics(struct ieee80211_hw *hw)
408{
409 struct rtl_priv *rtlpriv = rtl_priv(hw);
410 struct false_alarm_statistics *falsealm_cnt = &(rtlpriv->falsealm_cnt);
411 u32 ret_value;
412
413 ret_value = rtl_get_bbreg(hw, ROFDM_PHYCOUNTER1, MASKDWORD);
414 falsealm_cnt->cnt_parity_fail = ((ret_value & 0xffff0000) >> 16);
415
416 ret_value = rtl_get_bbreg(hw, ROFDM_PHYCOUNTER2, MASKDWORD);
417 falsealm_cnt->cnt_rate_illegal = (ret_value & 0xffff);
418 falsealm_cnt->cnt_crc8_fail = ((ret_value & 0xffff0000) >> 16);
419 ret_value = rtl_get_bbreg(hw, ROFDM_PHYCOUNTER3, MASKDWORD);
420 falsealm_cnt->cnt_mcs_fail = (ret_value & 0xffff);
421
422 falsealm_cnt->cnt_ofdm_fail = falsealm_cnt->cnt_parity_fail +
423 falsealm_cnt->cnt_rate_illegal + falsealm_cnt->cnt_crc8_fail +
424 falsealm_cnt->cnt_mcs_fail;
425
426 /* read CCK false alarm */
427 ret_value = rtl_get_bbreg(hw, 0xc64, MASKDWORD);
428 falsealm_cnt->cnt_cck_fail = (ret_value & 0xffff);
429 falsealm_cnt->cnt_all = falsealm_cnt->cnt_ofdm_fail +
430 falsealm_cnt->cnt_cck_fail;
431}
432
433static void rtl92s_backoff_enable_flag(struct ieee80211_hw *hw)
434{
435 struct rtl_priv *rtlpriv = rtl_priv(hw);
Larry Fingere0602752012-04-19 16:32:44 -0500436 struct dig_t *digtable = &rtlpriv->dm_digtable;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500437 struct false_alarm_statistics *falsealm_cnt = &(rtlpriv->falsealm_cnt);
438
Larry Fingere0602752012-04-19 16:32:44 -0500439 if (falsealm_cnt->cnt_all > digtable->fa_highthresh) {
Larry Fingerda17fcf2012-10-25 13:46:31 -0500440 if ((digtable->back_val - 6) <
Larry Fingere0602752012-04-19 16:32:44 -0500441 digtable->backoffval_range_min)
Larry Fingerda17fcf2012-10-25 13:46:31 -0500442 digtable->back_val = digtable->backoffval_range_min;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500443 else
Larry Fingerda17fcf2012-10-25 13:46:31 -0500444 digtable->back_val -= 6;
Larry Fingere0602752012-04-19 16:32:44 -0500445 } else if (falsealm_cnt->cnt_all < digtable->fa_lowthresh) {
Larry Fingerda17fcf2012-10-25 13:46:31 -0500446 if ((digtable->back_val + 6) >
Larry Fingere0602752012-04-19 16:32:44 -0500447 digtable->backoffval_range_max)
Larry Fingerda17fcf2012-10-25 13:46:31 -0500448 digtable->back_val =
Larry Fingere0602752012-04-19 16:32:44 -0500449 digtable->backoffval_range_max;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500450 else
Larry Fingerda17fcf2012-10-25 13:46:31 -0500451 digtable->back_val += 6;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500452 }
453}
454
455static void _rtl92s_dm_initial_gain_sta_beforeconnect(struct ieee80211_hw *hw)
456{
457 struct rtl_priv *rtlpriv = rtl_priv(hw);
Larry Fingere0602752012-04-19 16:32:44 -0500458 struct dig_t *digtable = &rtlpriv->dm_digtable;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500459 struct false_alarm_statistics *falsealm_cnt = &(rtlpriv->falsealm_cnt);
460 static u8 initialized, force_write;
461 u8 initial_gain = 0;
462
Larry Fingerda17fcf2012-10-25 13:46:31 -0500463 if ((digtable->pre_sta_cstate == digtable->cur_sta_cstate) ||
464 (digtable->cur_sta_cstate == DIG_STA_BEFORE_CONNECT)) {
465 if (digtable->cur_sta_cstate == DIG_STA_BEFORE_CONNECT) {
Chaoming Li9fe255e2011-05-03 09:47:55 -0500466 if (rtlpriv->psc.rfpwr_state != ERFON)
467 return;
468
Larry Fingere0602752012-04-19 16:32:44 -0500469 if (digtable->backoff_enable_flag)
Chaoming Li9fe255e2011-05-03 09:47:55 -0500470 rtl92s_backoff_enable_flag(hw);
471 else
Larry Fingerda17fcf2012-10-25 13:46:31 -0500472 digtable->back_val = DM_DIG_BACKOFF;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500473
Larry Fingerda17fcf2012-10-25 13:46:31 -0500474 if ((digtable->rssi_val + 10 - digtable->back_val) >
Larry Fingere0602752012-04-19 16:32:44 -0500475 digtable->rx_gain_range_max)
476 digtable->cur_igvalue =
477 digtable->rx_gain_range_max;
Larry Fingerda17fcf2012-10-25 13:46:31 -0500478 else if ((digtable->rssi_val + 10 - digtable->back_val)
Larry Fingere0602752012-04-19 16:32:44 -0500479 < digtable->rx_gain_range_min)
480 digtable->cur_igvalue =
481 digtable->rx_gain_range_min;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500482 else
Daniel Stamerdac67972012-11-29 17:09:26 +0100483 digtable->cur_igvalue = digtable->rssi_val + 10
484 - digtable->back_val;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500485
486 if (falsealm_cnt->cnt_all > 10000)
Larry Fingere0602752012-04-19 16:32:44 -0500487 digtable->cur_igvalue =
488 (digtable->cur_igvalue > 0x33) ?
489 digtable->cur_igvalue : 0x33;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500490
491 if (falsealm_cnt->cnt_all > 16000)
Larry Fingere0602752012-04-19 16:32:44 -0500492 digtable->cur_igvalue =
493 digtable->rx_gain_range_max;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500494 /* connected -> connected or disconnected -> disconnected */
495 } else {
496 /* Firmware control DIG, do nothing in driver dm */
497 return;
498 }
499 /* disconnected -> connected or connected ->
500 * disconnected or beforeconnect->(dis)connected */
501 } else {
502 /* Enable FW DIG */
Larry Fingere0602752012-04-19 16:32:44 -0500503 digtable->dig_ext_port_stage = DIG_EXT_PORT_STAGE_MAX;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500504 rtl92s_phy_set_fw_cmd(hw, FW_CMD_DIG_ENABLE);
505
Larry Fingerda17fcf2012-10-25 13:46:31 -0500506 digtable->back_val = DM_DIG_BACKOFF;
Larry Fingere0602752012-04-19 16:32:44 -0500507 digtable->cur_igvalue = rtlpriv->phy.default_initialgain[0];
508 digtable->pre_igvalue = 0;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500509 return;
510 }
511
512 /* Forced writing to prevent from fw-dig overwriting. */
Larry Fingere0602752012-04-19 16:32:44 -0500513 if (digtable->pre_igvalue != rtl_get_bbreg(hw, ROFDM0_XAAGCCORE1,
Chaoming Li9fe255e2011-05-03 09:47:55 -0500514 MASKBYTE0))
515 force_write = 1;
516
Larry Fingere0602752012-04-19 16:32:44 -0500517 if ((digtable->pre_igvalue != digtable->cur_igvalue) ||
Chaoming Li9fe255e2011-05-03 09:47:55 -0500518 !initialized || force_write) {
519 /* Disable FW DIG */
520 rtl92s_phy_set_fw_cmd(hw, FW_CMD_DIG_DISABLE);
521
Larry Fingere0602752012-04-19 16:32:44 -0500522 initial_gain = (u8)digtable->cur_igvalue;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500523
524 /* Set initial gain. */
525 rtl_set_bbreg(hw, ROFDM0_XAAGCCORE1, MASKBYTE0, initial_gain);
526 rtl_set_bbreg(hw, ROFDM0_XBAGCCORE1, MASKBYTE0, initial_gain);
Larry Fingere0602752012-04-19 16:32:44 -0500527 digtable->pre_igvalue = digtable->cur_igvalue;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500528 initialized = 1;
529 force_write = 0;
530 }
531}
532
533static void _rtl92s_dm_ctrl_initgain_bytwoport(struct ieee80211_hw *hw)
534{
535 struct rtl_priv *rtlpriv = rtl_priv(hw);
Daniel Stamerdac67972012-11-29 17:09:26 +0100536 struct dig_t *dig = &rtlpriv->dm_digtable;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500537
538 if (rtlpriv->mac80211.act_scanning)
539 return;
540
541 /* Decide the current status and if modify initial gain or not */
542 if (rtlpriv->mac80211.link_state >= MAC80211_LINKED ||
543 rtlpriv->mac80211.opmode == NL80211_IFTYPE_ADHOC)
Daniel Stamerdac67972012-11-29 17:09:26 +0100544 dig->cur_sta_cstate = DIG_STA_CONNECT;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500545 else
Daniel Stamerdac67972012-11-29 17:09:26 +0100546 dig->cur_sta_cstate = DIG_STA_DISCONNECT;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500547
Daniel Stamerdac67972012-11-29 17:09:26 +0100548 dig->rssi_val = rtlpriv->dm.undec_sm_pwdb;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500549
550 /* Change dig mode to rssi */
Daniel Stamerdac67972012-11-29 17:09:26 +0100551 if (dig->cur_sta_cstate != DIG_STA_DISCONNECT) {
552 if (dig->dig_twoport_algorithm ==
Chaoming Li9fe255e2011-05-03 09:47:55 -0500553 DIG_TWO_PORT_ALGO_FALSE_ALARM) {
Daniel Stamerdac67972012-11-29 17:09:26 +0100554 dig->dig_twoport_algorithm = DIG_TWO_PORT_ALGO_RSSI;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500555 rtl92s_phy_set_fw_cmd(hw, FW_CMD_DIG_MODE_SS);
556 }
557 }
558
559 _rtl92s_dm_false_alarm_counter_statistics(hw);
560 _rtl92s_dm_initial_gain_sta_beforeconnect(hw);
561
Daniel Stamerdac67972012-11-29 17:09:26 +0100562 dig->pre_sta_cstate = dig->cur_sta_cstate;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500563}
564
565static void _rtl92s_dm_ctrl_initgain_byrssi(struct ieee80211_hw *hw)
566{
567 struct rtl_priv *rtlpriv = rtl_priv(hw);
568 struct rtl_phy *rtlphy = &(rtlpriv->phy);
Larry Fingere0602752012-04-19 16:32:44 -0500569 struct dig_t *digtable = &rtlpriv->dm_digtable;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500570
571 /* 2T2R TP issue */
572 if (rtlphy->rf_type == RF_2T2R)
573 return;
574
575 if (!rtlpriv->dm.dm_initialgain_enable)
576 return;
577
Larry Fingere0602752012-04-19 16:32:44 -0500578 if (digtable->dig_enable_flag == false)
Chaoming Li9fe255e2011-05-03 09:47:55 -0500579 return;
580
581 _rtl92s_dm_ctrl_initgain_bytwoport(hw);
582}
583
584static void _rtl92s_dm_dynamic_txpower(struct ieee80211_hw *hw)
585{
586 struct rtl_priv *rtlpriv = rtl_priv(hw);
587 struct rtl_phy *rtlphy = &(rtlpriv->phy);
588 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
Larry Fingerda17fcf2012-10-25 13:46:31 -0500589 long undec_sm_pwdb;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500590 long txpwr_threshold_lv1, txpwr_threshold_lv2;
591
592 /* 2T2R TP issue */
593 if (rtlphy->rf_type == RF_2T2R)
594 return;
595
596 if (!rtlpriv->dm.dynamic_txpower_enable ||
597 rtlpriv->dm.dm_flag & HAL_DM_HIPWR_DISABLE) {
598 rtlpriv->dm.dynamic_txhighpower_lvl = TX_HIGHPWR_LEVEL_NORMAL;
599 return;
600 }
601
602 if ((mac->link_state < MAC80211_LINKED) &&
Larry Fingerda17fcf2012-10-25 13:46:31 -0500603 (rtlpriv->dm.entry_min_undec_sm_pwdb == 0)) {
Chaoming Li9fe255e2011-05-03 09:47:55 -0500604 RT_TRACE(rtlpriv, COMP_POWER, DBG_TRACE,
Joe Perchesf30d7502012-01-04 19:40:41 -0800605 "Not connected to any\n");
Chaoming Li9fe255e2011-05-03 09:47:55 -0500606
607 rtlpriv->dm.dynamic_txhighpower_lvl = TX_HIGHPWR_LEVEL_NORMAL;
608
609 rtlpriv->dm.last_dtp_lvl = TX_HIGHPWR_LEVEL_NORMAL;
610 return;
611 }
612
613 if (mac->link_state >= MAC80211_LINKED) {
614 if (mac->opmode == NL80211_IFTYPE_ADHOC) {
Larry Fingerda17fcf2012-10-25 13:46:31 -0500615 undec_sm_pwdb = rtlpriv->dm.entry_min_undec_sm_pwdb;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500616 RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
Joe Perchesf30d7502012-01-04 19:40:41 -0800617 "AP Client PWDB = 0x%lx\n",
Larry Fingerda17fcf2012-10-25 13:46:31 -0500618 undec_sm_pwdb);
Chaoming Li9fe255e2011-05-03 09:47:55 -0500619 } else {
Larry Fingerda17fcf2012-10-25 13:46:31 -0500620 undec_sm_pwdb = rtlpriv->dm.undec_sm_pwdb;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500621 RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
Joe Perchesf30d7502012-01-04 19:40:41 -0800622 "STA Default Port PWDB = 0x%lx\n",
Larry Fingerda17fcf2012-10-25 13:46:31 -0500623 undec_sm_pwdb);
Chaoming Li9fe255e2011-05-03 09:47:55 -0500624 }
625 } else {
Larry Fingerda17fcf2012-10-25 13:46:31 -0500626 undec_sm_pwdb = rtlpriv->dm.entry_min_undec_sm_pwdb;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500627
628 RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
Joe Perchesf30d7502012-01-04 19:40:41 -0800629 "AP Ext Port PWDB = 0x%lx\n",
Larry Fingerda17fcf2012-10-25 13:46:31 -0500630 undec_sm_pwdb);
Chaoming Li9fe255e2011-05-03 09:47:55 -0500631 }
632
633 txpwr_threshold_lv2 = TX_POWER_NEAR_FIELD_THRESH_LVL2;
634 txpwr_threshold_lv1 = TX_POWER_NEAR_FIELD_THRESH_LVL1;
635
636 if (rtl_get_bbreg(hw, 0xc90, MASKBYTE0) == 1)
637 rtlpriv->dm.dynamic_txhighpower_lvl = TX_HIGHPWR_LEVEL_NORMAL;
Larry Fingerda17fcf2012-10-25 13:46:31 -0500638 else if (undec_sm_pwdb >= txpwr_threshold_lv2)
Chaoming Li9fe255e2011-05-03 09:47:55 -0500639 rtlpriv->dm.dynamic_txhighpower_lvl = TX_HIGHPWR_LEVEL_NORMAL2;
Larry Fingerda17fcf2012-10-25 13:46:31 -0500640 else if ((undec_sm_pwdb < (txpwr_threshold_lv2 - 3)) &&
641 (undec_sm_pwdb >= txpwr_threshold_lv1))
Chaoming Li9fe255e2011-05-03 09:47:55 -0500642 rtlpriv->dm.dynamic_txhighpower_lvl = TX_HIGHPWR_LEVEL_NORMAL1;
Larry Fingerda17fcf2012-10-25 13:46:31 -0500643 else if (undec_sm_pwdb < (txpwr_threshold_lv1 - 3))
Chaoming Li9fe255e2011-05-03 09:47:55 -0500644 rtlpriv->dm.dynamic_txhighpower_lvl = TX_HIGHPWR_LEVEL_NORMAL;
645
646 if ((rtlpriv->dm.dynamic_txhighpower_lvl != rtlpriv->dm.last_dtp_lvl))
647 rtl92s_phy_set_txpower(hw, rtlphy->current_channel);
648
649 rtlpriv->dm.last_dtp_lvl = rtlpriv->dm.dynamic_txhighpower_lvl;
650}
651
652static void _rtl92s_dm_init_dig(struct ieee80211_hw *hw)
653{
654 struct rtl_priv *rtlpriv = rtl_priv(hw);
Larry Fingere0602752012-04-19 16:32:44 -0500655 struct dig_t *digtable = &rtlpriv->dm_digtable;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500656
657 /* Disable DIG scheme now.*/
Larry Fingere0602752012-04-19 16:32:44 -0500658 digtable->dig_enable_flag = true;
659 digtable->backoff_enable_flag = true;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500660
661 if ((rtlpriv->dm.dm_type == DM_TYPE_BYDRIVER) &&
662 (hal_get_firmwareversion(rtlpriv) >= 0x3c))
Larry Fingere0602752012-04-19 16:32:44 -0500663 digtable->dig_algorithm = DIG_ALGO_BY_TOW_PORT;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500664 else
Larry Fingere0602752012-04-19 16:32:44 -0500665 digtable->dig_algorithm =
Chaoming Li9fe255e2011-05-03 09:47:55 -0500666 DIG_ALGO_BEFORE_CONNECT_BY_RSSI_AND_ALARM;
667
Larry Fingere0602752012-04-19 16:32:44 -0500668 digtable->dig_twoport_algorithm = DIG_TWO_PORT_ALGO_RSSI;
669 digtable->dig_ext_port_stage = DIG_EXT_PORT_STAGE_MAX;
670 /* off=by real rssi value, on=by digtable->rssi_val for new dig */
671 digtable->dig_dbgmode = DM_DBG_OFF;
672 digtable->dig_slgorithm_switch = 0;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500673
674 /* 2007/10/04 MH Define init gain threshol. */
Larry Fingere0602752012-04-19 16:32:44 -0500675 digtable->dig_state = DM_STA_DIG_MAX;
676 digtable->dig_highpwrstate = DM_STA_DIG_MAX;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500677
Larry Fingerda17fcf2012-10-25 13:46:31 -0500678 digtable->cur_sta_cstate = DIG_STA_DISCONNECT;
679 digtable->pre_sta_cstate = DIG_STA_DISCONNECT;
680 digtable->cur_ap_cstate = DIG_AP_DISCONNECT;
681 digtable->pre_ap_cstate = DIG_AP_DISCONNECT;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500682
Larry Fingere0602752012-04-19 16:32:44 -0500683 digtable->rssi_lowthresh = DM_DIG_THRESH_LOW;
684 digtable->rssi_highthresh = DM_DIG_THRESH_HIGH;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500685
Larry Fingere0602752012-04-19 16:32:44 -0500686 digtable->fa_lowthresh = DM_FALSEALARM_THRESH_LOW;
687 digtable->fa_highthresh = DM_FALSEALARM_THRESH_HIGH;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500688
Larry Fingere0602752012-04-19 16:32:44 -0500689 digtable->rssi_highpower_lowthresh = DM_DIG_HIGH_PWR_THRESH_LOW;
690 digtable->rssi_highpower_highthresh = DM_DIG_HIGH_PWR_THRESH_HIGH;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500691
692 /* for dig debug rssi value */
Larry Fingere0602752012-04-19 16:32:44 -0500693 digtable->rssi_val = 50;
Larry Fingerda17fcf2012-10-25 13:46:31 -0500694 digtable->back_val = DM_DIG_BACKOFF;
Larry Fingere0602752012-04-19 16:32:44 -0500695 digtable->rx_gain_range_max = DM_DIG_MAX;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500696
Larry Fingere0602752012-04-19 16:32:44 -0500697 digtable->rx_gain_range_min = DM_DIG_MIN;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500698
Larry Fingere0602752012-04-19 16:32:44 -0500699 digtable->backoffval_range_max = DM_DIG_BACKOFF_MAX;
700 digtable->backoffval_range_min = DM_DIG_BACKOFF_MIN;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500701}
702
703static void _rtl92s_dm_init_dynamic_txpower(struct ieee80211_hw *hw)
704{
705 struct rtl_priv *rtlpriv = rtl_priv(hw);
706
707 if ((hal_get_firmwareversion(rtlpriv) >= 60) &&
708 (rtlpriv->dm.dm_type == DM_TYPE_BYDRIVER))
709 rtlpriv->dm.dynamic_txpower_enable = true;
710 else
711 rtlpriv->dm.dynamic_txpower_enable = false;
712
713 rtlpriv->dm.last_dtp_lvl = TX_HIGHPWR_LEVEL_NORMAL;
714 rtlpriv->dm.dynamic_txhighpower_lvl = TX_HIGHPWR_LEVEL_NORMAL;
715}
716
717void rtl92s_dm_init(struct ieee80211_hw *hw)
718{
719 struct rtl_priv *rtlpriv = rtl_priv(hw);
720
721 rtlpriv->dm.dm_type = DM_TYPE_BYDRIVER;
Larry Fingerda17fcf2012-10-25 13:46:31 -0500722 rtlpriv->dm.undec_sm_pwdb = -1;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500723
724 _rtl92s_dm_init_dynamic_txpower(hw);
725 rtl92s_dm_init_edca_turbo(hw);
726 _rtl92s_dm_init_rate_adaptive_mask(hw);
727 _rtl92s_dm_init_txpowertracking_thermalmeter(hw);
728 _rtl92s_dm_init_dig(hw);
729
730 rtl_write_dword(rtlpriv, WFM5, FW_CCA_CHK_ENABLE);
731}
732
733void rtl92s_dm_watchdog(struct ieee80211_hw *hw)
734{
735 _rtl92s_dm_check_edca_turbo(hw);
736 _rtl92s_dm_check_txpowertracking_thermalmeter(hw);
737 _rtl92s_dm_ctrl_initgain_byrssi(hw);
738 _rtl92s_dm_dynamic_txpower(hw);
739 _rtl92s_dm_refresh_rateadaptive_mask(hw);
740 _rtl92s_dm_switch_baseband_mrc(hw);
741}
742