1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2009-2012 Realtek Corporation.*/
12 #include "../rtl8192c/phy_common.h"
15 #include "../rtl8192c/dm_common.h"
16 #include "../rtl8192c/fw_common.h"
19 static bool _rtl92c_phy_config_mac_with_headerfile(struct ieee80211_hw
*hw
);
21 u32
rtl92c_phy_query_rf_reg(struct ieee80211_hw
*hw
,
22 enum radio_path rfpath
, u32 regaddr
, u32 bitmask
)
24 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
25 u32 original_value
, readback_value
, bitshift
;
26 struct rtl_phy
*rtlphy
= &(rtlpriv
->phy
);
28 RT_TRACE(rtlpriv
, COMP_RF
, DBG_TRACE
,
29 "regaddr(%#x), rfpath(%#x), bitmask(%#x)\n",
30 regaddr
, rfpath
, bitmask
);
32 spin_lock(&rtlpriv
->locks
.rf_lock
);
34 if (rtlphy
->rf_mode
!= RF_OP_BY_FW
) {
35 original_value
= _rtl92c_phy_rf_serial_read(hw
,
38 original_value
= _rtl92c_phy_fw_rf_serial_read(hw
,
42 bitshift
= _rtl92c_phy_calculate_bit_shift(bitmask
);
43 readback_value
= (original_value
& bitmask
) >> bitshift
;
45 spin_unlock(&rtlpriv
->locks
.rf_lock
);
47 RT_TRACE(rtlpriv
, COMP_RF
, DBG_TRACE
,
48 "regaddr(%#x), rfpath(%#x), bitmask(%#x), original_value(%#x)\n",
49 regaddr
, rfpath
, bitmask
, original_value
);
51 return readback_value
;
54 bool rtl92c_phy_mac_config(struct ieee80211_hw
*hw
)
56 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
57 struct rtl_hal
*rtlhal
= rtl_hal(rtl_priv(hw
));
58 bool is92c
= IS_92C_SERIAL(rtlhal
->version
);
59 bool rtstatus
= _rtl92c_phy_config_mac_with_headerfile(hw
);
62 rtl_write_byte(rtlpriv
, 0x14, 0x71);
64 rtl_write_byte(rtlpriv
, 0x04CA, 0x0A);
68 bool rtl92c_phy_bb_config(struct ieee80211_hw
*hw
)
71 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
74 u8 reg_hwparafile
= 1;
76 _rtl92c_phy_init_bb_rf_register_definition(hw
);
77 regval
= rtl_read_word(rtlpriv
, REG_SYS_FUNC_EN
);
78 rtl_write_word(rtlpriv
, REG_SYS_FUNC_EN
,
79 regval
| BIT(13) | BIT(0) | BIT(1));
80 rtl_write_byte(rtlpriv
, REG_AFE_PLL_CTRL
, 0x83);
81 rtl_write_byte(rtlpriv
, REG_AFE_PLL_CTRL
+ 1, 0xdb);
82 rtl_write_byte(rtlpriv
, REG_RF_CTRL
, RF_EN
| RF_RSTB
| RF_SDMRSTB
);
83 rtl_write_byte(rtlpriv
, REG_SYS_FUNC_EN
,
84 FEN_PPLL
| FEN_PCIEA
| FEN_DIO_PCIE
|
85 FEN_BB_GLB_RSTN
| FEN_BBRSTB
);
86 rtl_write_byte(rtlpriv
, REG_AFE_XTAL_CTRL
+ 1, 0x80);
87 regvaldw
= rtl_read_dword(rtlpriv
, REG_LEDCFG0
);
88 rtl_write_dword(rtlpriv
, REG_LEDCFG0
, regvaldw
| BIT(23));
89 if (reg_hwparafile
== 1)
90 rtstatus
= _rtl92c_phy_bb8192c_config_parafile(hw
);
94 void rtl92ce_phy_set_rf_reg(struct ieee80211_hw
*hw
,
95 enum radio_path rfpath
,
96 u32 regaddr
, u32 bitmask
, u32 data
)
98 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
99 struct rtl_phy
*rtlphy
= &(rtlpriv
->phy
);
100 u32 original_value
, bitshift
;
102 RT_TRACE(rtlpriv
, COMP_RF
, DBG_TRACE
,
103 "regaddr(%#x), bitmask(%#x), data(%#x), rfpath(%#x)\n",
104 regaddr
, bitmask
, data
, rfpath
);
106 spin_lock(&rtlpriv
->locks
.rf_lock
);
108 if (rtlphy
->rf_mode
!= RF_OP_BY_FW
) {
109 if (bitmask
!= RFREG_OFFSET_MASK
) {
110 original_value
= _rtl92c_phy_rf_serial_read(hw
,
113 bitshift
= _rtl92c_phy_calculate_bit_shift(bitmask
);
115 ((original_value
& (~bitmask
)) |
119 _rtl92c_phy_rf_serial_write(hw
, rfpath
, regaddr
, data
);
121 if (bitmask
!= RFREG_OFFSET_MASK
) {
122 original_value
= _rtl92c_phy_fw_rf_serial_read(hw
,
125 bitshift
= _rtl92c_phy_calculate_bit_shift(bitmask
);
127 ((original_value
& (~bitmask
)) |
130 _rtl92c_phy_fw_rf_serial_write(hw
, rfpath
, regaddr
, data
);
133 spin_unlock(&rtlpriv
->locks
.rf_lock
);
135 RT_TRACE(rtlpriv
, COMP_RF
, DBG_TRACE
,
136 "regaddr(%#x), bitmask(%#x), data(%#x), rfpath(%#x)\n",
137 regaddr
, bitmask
, data
, rfpath
);
140 static bool _rtl92c_phy_config_mac_with_headerfile(struct ieee80211_hw
*hw
)
142 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
147 RT_TRACE(rtlpriv
, COMP_INIT
, DBG_TRACE
, "Read Rtl819XMACPHY_Array\n");
148 arraylength
= MAC_2T_ARRAYLENGTH
;
149 ptrarray
= RTL8192CEMAC_2T_ARRAY
;
150 RT_TRACE(rtlpriv
, COMP_INIT
, DBG_TRACE
, "Img:RTL8192CEMAC_2T_ARRAY\n");
151 for (i
= 0; i
< arraylength
; i
= i
+ 2)
152 rtl_write_byte(rtlpriv
, ptrarray
[i
], (u8
) ptrarray
[i
+ 1]);
156 bool _rtl92ce_phy_config_bb_with_headerfile(struct ieee80211_hw
*hw
,
160 u32
*phy_regarray_table
;
161 u32
*agctab_array_table
;
162 u16 phy_reg_arraylen
, agctab_arraylen
;
163 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
164 struct rtl_hal
*rtlhal
= rtl_hal(rtl_priv(hw
));
166 if (IS_92C_SERIAL(rtlhal
->version
)) {
167 agctab_arraylen
= AGCTAB_2TARRAYLENGTH
;
168 agctab_array_table
= RTL8192CEAGCTAB_2TARRAY
;
169 phy_reg_arraylen
= PHY_REG_2TARRAY_LENGTH
;
170 phy_regarray_table
= RTL8192CEPHY_REG_2TARRAY
;
172 agctab_arraylen
= AGCTAB_1TARRAYLENGTH
;
173 agctab_array_table
= RTL8192CEAGCTAB_1TARRAY
;
174 phy_reg_arraylen
= PHY_REG_1TARRAY_LENGTH
;
175 phy_regarray_table
= RTL8192CEPHY_REG_1TARRAY
;
177 if (configtype
== BASEBAND_CONFIG_PHY_REG
) {
178 for (i
= 0; i
< phy_reg_arraylen
; i
= i
+ 2) {
179 rtl_addr_delay(phy_regarray_table
[i
]);
180 rtl_set_bbreg(hw
, phy_regarray_table
[i
], MASKDWORD
,
181 phy_regarray_table
[i
+ 1]);
183 RT_TRACE(rtlpriv
, COMP_INIT
, DBG_TRACE
,
184 "The phy_regarray_table[0] is %x Rtl819XPHY_REGArray[1] is %x\n",
185 phy_regarray_table
[i
],
186 phy_regarray_table
[i
+ 1]);
188 } else if (configtype
== BASEBAND_CONFIG_AGC_TAB
) {
189 for (i
= 0; i
< agctab_arraylen
; i
= i
+ 2) {
190 rtl_set_bbreg(hw
, agctab_array_table
[i
], MASKDWORD
,
191 agctab_array_table
[i
+ 1]);
193 RT_TRACE(rtlpriv
, COMP_INIT
, DBG_TRACE
,
194 "The agctab_array_table[0] is %x Rtl819XPHY_REGArray[1] is %x\n",
195 agctab_array_table
[i
],
196 agctab_array_table
[i
+ 1]);
202 bool _rtl92ce_phy_config_bb_with_pgheaderfile(struct ieee80211_hw
*hw
,
205 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
207 u32
*phy_regarray_table_pg
;
208 u16 phy_regarray_pg_len
;
210 phy_regarray_pg_len
= PHY_REG_ARRAY_PGLENGTH
;
211 phy_regarray_table_pg
= RTL8192CEPHY_REG_ARRAY_PG
;
213 if (configtype
== BASEBAND_CONFIG_PHY_REG
) {
214 for (i
= 0; i
< phy_regarray_pg_len
; i
= i
+ 3) {
215 rtl_addr_delay(phy_regarray_table_pg
[i
]);
217 _rtl92c_store_pwrindex_diffrate_offset(hw
,
218 phy_regarray_table_pg
[i
],
219 phy_regarray_table_pg
[i
+ 1],
220 phy_regarray_table_pg
[i
+ 2]);
224 RT_TRACE(rtlpriv
, COMP_SEND
, DBG_TRACE
,
225 "configtype != BaseBand_Config_PHY_REG\n");
230 bool rtl92c_phy_config_rf_with_headerfile(struct ieee80211_hw
*hw
,
231 enum radio_path rfpath
)
235 u32
*radioa_array_table
;
236 u32
*radiob_array_table
;
237 u16 radioa_arraylen
, radiob_arraylen
;
238 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
239 struct rtl_hal
*rtlhal
= rtl_hal(rtl_priv(hw
));
241 if (IS_92C_SERIAL(rtlhal
->version
)) {
242 radioa_arraylen
= RADIOA_2TARRAYLENGTH
;
243 radioa_array_table
= RTL8192CERADIOA_2TARRAY
;
244 radiob_arraylen
= RADIOB_2TARRAYLENGTH
;
245 radiob_array_table
= RTL8192CE_RADIOB_2TARRAY
;
246 RT_TRACE(rtlpriv
, COMP_INIT
, DBG_TRACE
,
247 "Radio_A:RTL8192CERADIOA_2TARRAY\n");
248 RT_TRACE(rtlpriv
, COMP_INIT
, DBG_TRACE
,
249 "Radio_B:RTL8192CE_RADIOB_2TARRAY\n");
251 radioa_arraylen
= RADIOA_1TARRAYLENGTH
;
252 radioa_array_table
= RTL8192CE_RADIOA_1TARRAY
;
253 radiob_arraylen
= RADIOB_1TARRAYLENGTH
;
254 radiob_array_table
= RTL8192CE_RADIOB_1TARRAY
;
255 RT_TRACE(rtlpriv
, COMP_INIT
, DBG_TRACE
,
256 "Radio_A:RTL8192CE_RADIOA_1TARRAY\n");
257 RT_TRACE(rtlpriv
, COMP_INIT
, DBG_TRACE
,
258 "Radio_B:RTL8192CE_RADIOB_1TARRAY\n");
260 RT_TRACE(rtlpriv
, COMP_INIT
, DBG_TRACE
, "Radio No %x\n", rfpath
);
263 for (i
= 0; i
< radioa_arraylen
; i
= i
+ 2) {
264 rtl_rfreg_delay(hw
, rfpath
, radioa_array_table
[i
],
266 radioa_array_table
[i
+ 1]);
270 for (i
= 0; i
< radiob_arraylen
; i
= i
+ 2) {
271 rtl_rfreg_delay(hw
, rfpath
, radiob_array_table
[i
],
273 radiob_array_table
[i
+ 1]);
278 pr_info("Incorrect rfpath %#x\n", rfpath
);
281 pr_info("switch case %#x not processed\n", rfpath
);
287 void rtl92ce_phy_set_bw_mode_callback(struct ieee80211_hw
*hw
)
289 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
290 struct rtl_hal
*rtlhal
= rtl_hal(rtl_priv(hw
));
291 struct rtl_phy
*rtlphy
= &(rtlpriv
->phy
);
292 struct rtl_mac
*mac
= rtl_mac(rtl_priv(hw
));
296 RT_TRACE(rtlpriv
, COMP_SCAN
, DBG_TRACE
, "Switch to %s bandwidth\n",
297 rtlphy
->current_chan_bw
== HT_CHANNEL_WIDTH_20
?
300 if (is_hal_stop(rtlhal
)) {
301 rtlphy
->set_bwmode_inprogress
= false;
305 reg_bw_opmode
= rtl_read_byte(rtlpriv
, REG_BWOPMODE
);
306 reg_prsr_rsc
= rtl_read_byte(rtlpriv
, REG_RRSR
+ 2);
308 switch (rtlphy
->current_chan_bw
) {
309 case HT_CHANNEL_WIDTH_20
:
310 reg_bw_opmode
|= BW_OPMODE_20MHZ
;
311 rtl_write_byte(rtlpriv
, REG_BWOPMODE
, reg_bw_opmode
);
313 case HT_CHANNEL_WIDTH_20_40
:
314 reg_bw_opmode
&= ~BW_OPMODE_20MHZ
;
315 rtl_write_byte(rtlpriv
, REG_BWOPMODE
, reg_bw_opmode
);
317 (reg_prsr_rsc
& 0x90) | (mac
->cur_40_prime_sc
<< 5);
318 rtl_write_byte(rtlpriv
, REG_RRSR
+ 2, reg_prsr_rsc
);
321 pr_info("unknown bandwidth: %#X\n", rtlphy
->current_chan_bw
);
325 switch (rtlphy
->current_chan_bw
) {
326 case HT_CHANNEL_WIDTH_20
:
327 rtl_set_bbreg(hw
, RFPGA0_RFMOD
, BRFMOD
, 0x0);
328 rtl_set_bbreg(hw
, RFPGA1_RFMOD
, BRFMOD
, 0x0);
329 rtl_set_bbreg(hw
, RFPGA0_ANALOGPARAMETER2
, BIT(10), 1);
331 case HT_CHANNEL_WIDTH_20_40
:
332 rtl_set_bbreg(hw
, RFPGA0_RFMOD
, BRFMOD
, 0x1);
333 rtl_set_bbreg(hw
, RFPGA1_RFMOD
, BRFMOD
, 0x1);
335 rtl_set_bbreg(hw
, RCCK0_SYSTEM
, BCCK_SIDEBAND
,
336 (mac
->cur_40_prime_sc
>> 1));
337 rtl_set_bbreg(hw
, ROFDM1_LSTF
, 0xC00, mac
->cur_40_prime_sc
);
338 rtl_set_bbreg(hw
, RFPGA0_ANALOGPARAMETER2
, BIT(10), 0);
340 rtl_set_bbreg(hw
, 0x818, (BIT(26) | BIT(27)),
341 (mac
->cur_40_prime_sc
==
342 HAL_PRIME_CHNL_OFFSET_LOWER
) ? 2 : 1);
345 pr_err("unknown bandwidth: %#X\n",
346 rtlphy
->current_chan_bw
);
349 rtl92ce_phy_rf6052_set_bandwidth(hw
, rtlphy
->current_chan_bw
);
350 rtlphy
->set_bwmode_inprogress
= false;
351 RT_TRACE(rtlpriv
, COMP_SCAN
, DBG_TRACE
, "<==\n");
354 void _rtl92ce_phy_lc_calibrate(struct ieee80211_hw
*hw
, bool is2t
)
357 u32 rf_a_mode
= 0, rf_b_mode
= 0, lc_cal
;
358 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
360 tmpreg
= rtl_read_byte(rtlpriv
, 0xd03);
362 if ((tmpreg
& 0x70) != 0)
363 rtl_write_byte(rtlpriv
, 0xd03, tmpreg
& 0x8F);
365 rtl_write_byte(rtlpriv
, REG_TXPAUSE
, 0xFF);
367 if ((tmpreg
& 0x70) != 0) {
368 rf_a_mode
= rtl_get_rfreg(hw
, RF90_PATH_A
, 0x00, MASK12BITS
);
371 rf_b_mode
= rtl_get_rfreg(hw
, RF90_PATH_B
, 0x00,
374 rtl_set_rfreg(hw
, RF90_PATH_A
, 0x00, MASK12BITS
,
375 (rf_a_mode
& 0x8FFFF) | 0x10000);
378 rtl_set_rfreg(hw
, RF90_PATH_B
, 0x00, MASK12BITS
,
379 (rf_b_mode
& 0x8FFFF) | 0x10000);
381 lc_cal
= rtl_get_rfreg(hw
, RF90_PATH_A
, 0x18, MASK12BITS
);
383 rtl_set_rfreg(hw
, RF90_PATH_A
, 0x18, MASK12BITS
, lc_cal
| 0x08000);
387 if ((tmpreg
& 0x70) != 0) {
388 rtl_write_byte(rtlpriv
, 0xd03, tmpreg
);
389 rtl_set_rfreg(hw
, RF90_PATH_A
, 0x00, MASK12BITS
, rf_a_mode
);
392 rtl_set_rfreg(hw
, RF90_PATH_B
, 0x00, MASK12BITS
,
395 rtl_write_byte(rtlpriv
, REG_TXPAUSE
, 0x00);
399 static void _rtl92ce_phy_set_rf_sleep(struct ieee80211_hw
*hw
)
403 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
405 rtl_write_byte(rtlpriv
, REG_TXPAUSE
, 0xFF);
406 rtl_set_rfreg(hw
, RF90_PATH_A
, 0x00, RFREG_OFFSET_MASK
, 0x00);
407 rtl_write_byte(rtlpriv
, REG_APSD_CTRL
, 0x40);
408 u4b_tmp
= rtl_get_rfreg(hw
, RF90_PATH_A
, 0, RFREG_OFFSET_MASK
);
409 while (u4b_tmp
!= 0 && delay
> 0) {
410 rtl_write_byte(rtlpriv
, REG_APSD_CTRL
, 0x0);
411 rtl_set_rfreg(hw
, RF90_PATH_A
, 0x00, RFREG_OFFSET_MASK
, 0x00);
412 rtl_write_byte(rtlpriv
, REG_APSD_CTRL
, 0x40);
413 u4b_tmp
= rtl_get_rfreg(hw
, RF90_PATH_A
, 0, RFREG_OFFSET_MASK
);
417 rtl_write_byte(rtlpriv
, REG_APSD_CTRL
, 0x00);
418 rtl_write_byte(rtlpriv
, REG_SYS_FUNC_EN
, 0xE2);
419 rtl_write_byte(rtlpriv
, REG_SYS_FUNC_EN
, 0xE3);
420 rtl_write_byte(rtlpriv
, REG_TXPAUSE
, 0x00);
421 RT_TRACE(rtlpriv
, COMP_POWER
, DBG_TRACE
,
422 "Switch RF timeout !!!\n");
425 rtl_write_byte(rtlpriv
, REG_SYS_FUNC_EN
, 0xE2);
426 rtl_write_byte(rtlpriv
, REG_SPS0_CTRL
, 0x22);
429 static bool _rtl92ce_phy_set_rf_power_state(struct ieee80211_hw
*hw
,
430 enum rf_pwrstate rfpwr_state
)
432 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
433 struct rtl_pci_priv
*pcipriv
= rtl_pcipriv(hw
);
434 struct rtl_mac
*mac
= rtl_mac(rtl_priv(hw
));
435 struct rtl_ps_ctl
*ppsc
= rtl_psc(rtl_priv(hw
));
438 struct rtl8192_tx_ring
*ring
= NULL
;
440 switch (rfpwr_state
) {
442 if ((ppsc
->rfpwr_state
== ERFOFF
) &&
443 RT_IN_PS_LEVEL(ppsc
, RT_RF_OFF_LEVL_HALT_NIC
)) {
445 u32 initializecount
= 0;
449 RT_TRACE(rtlpriv
, COMP_RF
, DBG_DMESG
,
450 "IPS Set eRf nic enable\n");
451 rtstatus
= rtl_ps_enable_nic(hw
);
452 } while (!rtstatus
&& (initializecount
< 10));
453 RT_CLEAR_PS_LEVEL(ppsc
,
454 RT_RF_OFF_LEVL_HALT_NIC
);
456 RT_TRACE(rtlpriv
, COMP_RF
, DBG_DMESG
,
457 "Set ERFON sleeped:%d ms\n",
458 jiffies_to_msecs(jiffies
-
460 last_sleep_jiffies
));
461 ppsc
->last_awake_jiffies
= jiffies
;
462 rtl92ce_phy_set_rf_on(hw
);
464 if (mac
->link_state
== MAC80211_LINKED
) {
465 rtlpriv
->cfg
->ops
->led_control(hw
,
468 rtlpriv
->cfg
->ops
->led_control(hw
,
474 if (ppsc
->reg_rfps_level
& RT_RF_OFF_LEVL_HALT_NIC
) {
475 RT_TRACE(rtlpriv
, COMP_RF
, DBG_DMESG
,
476 "IPS Set eRf nic disable\n");
477 rtl_ps_disable_nic(hw
);
478 RT_SET_PS_LEVEL(ppsc
, RT_RF_OFF_LEVL_HALT_NIC
);
480 if (ppsc
->rfoff_reason
== RF_CHANGE_BY_IPS
) {
481 rtlpriv
->cfg
->ops
->led_control(hw
,
484 rtlpriv
->cfg
->ops
->led_control(hw
,
491 if (ppsc
->rfpwr_state
== ERFOFF
)
493 for (queue_id
= 0, i
= 0;
494 queue_id
< RTL_PCI_MAX_TX_QUEUE_COUNT
;) {
495 ring
= &pcipriv
->dev
.tx_ring
[queue_id
];
496 if (queue_id
== BEACON_QUEUE
||
497 skb_queue_len(&ring
->queue
) == 0) {
501 RT_TRACE(rtlpriv
, COMP_ERR
, DBG_WARNING
,
502 "eRf Off/Sleep: %d times TcbBusyQueue[%d] =%d before doze!\n",
504 skb_queue_len(&ring
->queue
));
509 if (i
>= MAX_DOZE_WAITING_TIMES_9x
) {
510 RT_TRACE(rtlpriv
, COMP_ERR
, DBG_WARNING
,
511 "ERFSLEEP: %d times TcbBusyQueue[%d] = %d !\n",
512 MAX_DOZE_WAITING_TIMES_9x
,
514 skb_queue_len(&ring
->queue
));
518 RT_TRACE(rtlpriv
, COMP_RF
, DBG_DMESG
,
519 "Set ERFSLEEP awaked:%d ms\n",
520 jiffies_to_msecs(jiffies
-
521 ppsc
->last_awake_jiffies
));
522 ppsc
->last_sleep_jiffies
= jiffies
;
523 _rtl92ce_phy_set_rf_sleep(hw
);
527 pr_err("switch case %#x not processed\n",
533 ppsc
->rfpwr_state
= rfpwr_state
;
537 bool rtl92c_phy_set_rf_power_state(struct ieee80211_hw
*hw
,
538 enum rf_pwrstate rfpwr_state
)
540 struct rtl_ps_ctl
*ppsc
= rtl_psc(rtl_priv(hw
));
542 bool bresult
= false;
544 if (rfpwr_state
== ppsc
->rfpwr_state
)
546 bresult
= _rtl92ce_phy_set_rf_power_state(hw
, rfpwr_state
);