1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2009-2012 Realtek Corporation.*/
11 #include "../rtl8192c/phy_common.h"
14 #include "../rtl8192c/dm_common.h"
15 #include "../rtl8192c/fw_common.h"
18 u32
rtl92cu_phy_query_rf_reg(struct ieee80211_hw
*hw
,
19 enum radio_path rfpath
, u32 regaddr
, u32 bitmask
)
21 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
22 u32 original_value
, readback_value
, bitshift
;
23 struct rtl_phy
*rtlphy
= &(rtlpriv
->phy
);
25 rtl_dbg(rtlpriv
, COMP_RF
, DBG_TRACE
,
26 "regaddr(%#x), rfpath(%#x), bitmask(%#x)\n",
27 regaddr
, rfpath
, bitmask
);
28 if (rtlphy
->rf_mode
!= RF_OP_BY_FW
) {
29 original_value
= _rtl92c_phy_rf_serial_read(hw
,
32 original_value
= _rtl92c_phy_fw_rf_serial_read(hw
,
35 bitshift
= _rtl92c_phy_calculate_bit_shift(bitmask
);
36 readback_value
= (original_value
& bitmask
) >> bitshift
;
37 rtl_dbg(rtlpriv
, COMP_RF
, DBG_TRACE
,
38 "regaddr(%#x), rfpath(%#x), bitmask(%#x), original_value(%#x)\n",
39 regaddr
, rfpath
, bitmask
, original_value
);
40 return readback_value
;
43 void rtl92cu_phy_set_rf_reg(struct ieee80211_hw
*hw
,
44 enum radio_path rfpath
,
45 u32 regaddr
, u32 bitmask
, u32 data
)
47 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
48 struct rtl_phy
*rtlphy
= &(rtlpriv
->phy
);
49 u32 original_value
, bitshift
;
51 rtl_dbg(rtlpriv
, COMP_RF
, DBG_TRACE
,
52 "regaddr(%#x), bitmask(%#x), data(%#x), rfpath(%#x)\n",
53 regaddr
, bitmask
, data
, rfpath
);
54 if (rtlphy
->rf_mode
!= RF_OP_BY_FW
) {
55 if (bitmask
!= RFREG_OFFSET_MASK
) {
56 original_value
= _rtl92c_phy_rf_serial_read(hw
,
59 bitshift
= _rtl92c_phy_calculate_bit_shift(bitmask
);
61 ((original_value
& (~bitmask
)) |
64 _rtl92c_phy_rf_serial_write(hw
, rfpath
, regaddr
, data
);
66 if (bitmask
!= RFREG_OFFSET_MASK
) {
67 original_value
= _rtl92c_phy_fw_rf_serial_read(hw
,
70 bitshift
= _rtl92c_phy_calculate_bit_shift(bitmask
);
72 ((original_value
& (~bitmask
)) |
75 _rtl92c_phy_fw_rf_serial_write(hw
, rfpath
, regaddr
, data
);
77 rtl_dbg(rtlpriv
, COMP_RF
, DBG_TRACE
,
78 "regaddr(%#x), bitmask(%#x), data(%#x), rfpath(%#x)\n",
79 regaddr
, bitmask
, data
, rfpath
);
82 bool rtl92cu_phy_mac_config(struct ieee80211_hw
*hw
)
86 rtstatus
= _rtl92cu_phy_config_mac_with_headerfile(hw
);
90 bool rtl92cu_phy_bb_config(struct ieee80211_hw
*hw
)
93 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
96 u8 b_reg_hwparafile
= 1;
98 _rtl92c_phy_init_bb_rf_register_definition(hw
);
99 regval
= rtl_read_word(rtlpriv
, REG_SYS_FUNC_EN
);
100 rtl_write_word(rtlpriv
, REG_SYS_FUNC_EN
, regval
| BIT(13) |
102 rtl_write_byte(rtlpriv
, REG_AFE_PLL_CTRL
, 0x83);
103 rtl_write_byte(rtlpriv
, REG_AFE_PLL_CTRL
+ 1, 0xdb);
104 rtl_write_byte(rtlpriv
, REG_RF_CTRL
, RF_EN
| RF_RSTB
| RF_SDMRSTB
);
105 rtl_write_byte(rtlpriv
, REG_SYS_FUNC_EN
, FEN_USBA
| FEN_USBD
|
106 FEN_BB_GLB_RSTN
| FEN_BBRSTB
);
107 regval32
= rtl_read_dword(rtlpriv
, 0x87c);
108 rtl_write_dword(rtlpriv
, 0x87c, regval32
& (~BIT(31)));
109 rtl_write_byte(rtlpriv
, REG_LDOHCI12_CTRL
, 0x0f);
110 rtl_write_byte(rtlpriv
, REG_AFE_XTAL_CTRL
+ 1, 0x80);
111 if (b_reg_hwparafile
== 1)
112 rtstatus
= _rtl92c_phy_bb8192c_config_parafile(hw
);
116 bool _rtl92cu_phy_config_mac_with_headerfile(struct ieee80211_hw
*hw
)
118 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
119 struct rtl_phy
*rtlphy
= &(rtlpriv
->phy
);
124 rtl_dbg(rtlpriv
, COMP_INIT
, DBG_TRACE
, "Read Rtl819XMACPHY_ARRAY\n");
125 arraylength
= rtlphy
->hwparam_tables
[MAC_REG
].length
;
126 ptrarray
= rtlphy
->hwparam_tables
[MAC_REG
].pdata
;
127 rtl_dbg(rtlpriv
, COMP_INIT
, DBG_TRACE
, "Img:RTL8192CUMAC_2T_ARRAY\n");
128 for (i
= 0; i
< arraylength
; i
= i
+ 2)
129 rtl_write_byte(rtlpriv
, ptrarray
[i
], (u8
) ptrarray
[i
+ 1]);
133 bool _rtl92cu_phy_config_bb_with_headerfile(struct ieee80211_hw
*hw
,
137 u32
*phy_regarray_table
;
138 u32
*agctab_array_table
;
139 u16 phy_reg_arraylen
, agctab_arraylen
;
140 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
141 struct rtl_hal
*rtlhal
= rtl_hal(rtl_priv(hw
));
142 struct rtl_phy
*rtlphy
= &(rtlpriv
->phy
);
144 if (IS_92C_SERIAL(rtlhal
->version
)) {
145 agctab_arraylen
= rtlphy
->hwparam_tables
[AGCTAB_2T
].length
;
146 agctab_array_table
= rtlphy
->hwparam_tables
[AGCTAB_2T
].pdata
;
147 phy_reg_arraylen
= rtlphy
->hwparam_tables
[PHY_REG_2T
].length
;
148 phy_regarray_table
= rtlphy
->hwparam_tables
[PHY_REG_2T
].pdata
;
150 agctab_arraylen
= rtlphy
->hwparam_tables
[AGCTAB_1T
].length
;
151 agctab_array_table
= rtlphy
->hwparam_tables
[AGCTAB_1T
].pdata
;
152 phy_reg_arraylen
= rtlphy
->hwparam_tables
[PHY_REG_1T
].length
;
153 phy_regarray_table
= rtlphy
->hwparam_tables
[PHY_REG_1T
].pdata
;
155 if (configtype
== BASEBAND_CONFIG_PHY_REG
) {
156 for (i
= 0; i
< phy_reg_arraylen
; i
= i
+ 2) {
157 rtl_addr_delay(phy_regarray_table
[i
]);
158 rtl_set_bbreg(hw
, phy_regarray_table
[i
], MASKDWORD
,
159 phy_regarray_table
[i
+ 1]);
161 rtl_dbg(rtlpriv
, COMP_INIT
, DBG_TRACE
,
162 "The phy_regarray_table[0] is %x Rtl819XPHY_REGARRAY[1] is %x\n",
163 phy_regarray_table
[i
],
164 phy_regarray_table
[i
+ 1]);
166 } else if (configtype
== BASEBAND_CONFIG_AGC_TAB
) {
167 for (i
= 0; i
< agctab_arraylen
; i
= i
+ 2) {
168 rtl_set_bbreg(hw
, agctab_array_table
[i
], MASKDWORD
,
169 agctab_array_table
[i
+ 1]);
171 rtl_dbg(rtlpriv
, COMP_INIT
, DBG_TRACE
,
172 "The agctab_array_table[0] is %x Rtl819XPHY_REGARRAY[1] is %x\n",
173 agctab_array_table
[i
],
174 agctab_array_table
[i
+ 1]);
180 bool _rtl92cu_phy_config_bb_with_pgheaderfile(struct ieee80211_hw
*hw
,
183 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
184 struct rtl_phy
*rtlphy
= &(rtlpriv
->phy
);
186 u32
*phy_regarray_table_pg
;
187 u16 phy_regarray_pg_len
;
189 rtlphy
->pwrgroup_cnt
= 0;
190 phy_regarray_pg_len
= rtlphy
->hwparam_tables
[PHY_REG_PG
].length
;
191 phy_regarray_table_pg
= rtlphy
->hwparam_tables
[PHY_REG_PG
].pdata
;
192 if (configtype
== BASEBAND_CONFIG_PHY_REG
) {
193 for (i
= 0; i
< phy_regarray_pg_len
; i
= i
+ 3) {
194 rtl_addr_delay(phy_regarray_table_pg
[i
]);
195 _rtl92c_store_pwrindex_diffrate_offset(hw
,
196 phy_regarray_table_pg
[i
],
197 phy_regarray_table_pg
[i
+ 1],
198 phy_regarray_table_pg
[i
+ 2]);
201 rtl_dbg(rtlpriv
, COMP_SEND
, DBG_TRACE
,
202 "configtype != BaseBand_Config_PHY_REG\n");
207 bool rtl92cu_phy_config_rf_with_headerfile(struct ieee80211_hw
*hw
,
208 enum radio_path rfpath
)
211 u32
*radioa_array_table
;
212 u32
*radiob_array_table
;
213 u16 radioa_arraylen
, radiob_arraylen
;
214 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
215 struct rtl_hal
*rtlhal
= rtl_hal(rtl_priv(hw
));
216 struct rtl_phy
*rtlphy
= &(rtlpriv
->phy
);
218 if (IS_92C_SERIAL(rtlhal
->version
)) {
219 radioa_arraylen
= rtlphy
->hwparam_tables
[RADIOA_2T
].length
;
220 radioa_array_table
= rtlphy
->hwparam_tables
[RADIOA_2T
].pdata
;
221 radiob_arraylen
= rtlphy
->hwparam_tables
[RADIOB_2T
].length
;
222 radiob_array_table
= rtlphy
->hwparam_tables
[RADIOB_2T
].pdata
;
223 rtl_dbg(rtlpriv
, COMP_INIT
, DBG_TRACE
,
224 "Radio_A:RTL8192CURADIOA_2TARRAY\n");
225 rtl_dbg(rtlpriv
, COMP_INIT
, DBG_TRACE
,
226 "Radio_B:RTL8192CU_RADIOB_2TARRAY\n");
228 radioa_arraylen
= rtlphy
->hwparam_tables
[RADIOA_1T
].length
;
229 radioa_array_table
= rtlphy
->hwparam_tables
[RADIOA_1T
].pdata
;
230 radiob_arraylen
= rtlphy
->hwparam_tables
[RADIOB_1T
].length
;
231 radiob_array_table
= rtlphy
->hwparam_tables
[RADIOB_1T
].pdata
;
232 rtl_dbg(rtlpriv
, COMP_INIT
, DBG_TRACE
,
233 "Radio_A:RTL8192CU_RADIOA_1TARRAY\n");
234 rtl_dbg(rtlpriv
, COMP_INIT
, DBG_TRACE
,
235 "Radio_B:RTL8192CU_RADIOB_1TARRAY\n");
237 rtl_dbg(rtlpriv
, COMP_INIT
, DBG_TRACE
, "Radio No %x\n", rfpath
);
240 for (i
= 0; i
< radioa_arraylen
; i
= i
+ 2) {
241 rtl_rfreg_delay(hw
, rfpath
, radioa_array_table
[i
],
243 radioa_array_table
[i
+ 1]);
247 for (i
= 0; i
< radiob_arraylen
; i
= i
+ 2) {
248 rtl_rfreg_delay(hw
, rfpath
, radiob_array_table
[i
],
250 radiob_array_table
[i
+ 1]);
255 pr_err("switch case %#x not processed\n", rfpath
);
263 void rtl92cu_phy_set_bw_mode_callback(struct ieee80211_hw
*hw
)
265 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
266 struct rtl_hal
*rtlhal
= rtl_hal(rtl_priv(hw
));
267 struct rtl_phy
*rtlphy
= &(rtlpriv
->phy
);
268 struct rtl_mac
*mac
= rtl_mac(rtl_priv(hw
));
272 rtl_dbg(rtlpriv
, COMP_SCAN
, DBG_TRACE
, "Switch to %s bandwidth\n",
273 rtlphy
->current_chan_bw
== HT_CHANNEL_WIDTH_20
?
275 if (is_hal_stop(rtlhal
)) {
276 rtlphy
->set_bwmode_inprogress
= false;
279 reg_bw_opmode
= rtl_read_byte(rtlpriv
, REG_BWOPMODE
);
280 reg_prsr_rsc
= rtl_read_byte(rtlpriv
, REG_RRSR
+ 2);
281 switch (rtlphy
->current_chan_bw
) {
282 case HT_CHANNEL_WIDTH_20
:
283 reg_bw_opmode
|= BW_OPMODE_20MHZ
;
284 rtl_write_byte(rtlpriv
, REG_BWOPMODE
, reg_bw_opmode
);
286 case HT_CHANNEL_WIDTH_20_40
:
287 reg_bw_opmode
&= ~BW_OPMODE_20MHZ
;
288 rtl_write_byte(rtlpriv
, REG_BWOPMODE
, reg_bw_opmode
);
290 (reg_prsr_rsc
& 0x90) | (mac
->cur_40_prime_sc
<< 5);
291 rtl_write_byte(rtlpriv
, REG_RRSR
+ 2, reg_prsr_rsc
);
294 pr_err("unknown bandwidth: %#X\n",
295 rtlphy
->current_chan_bw
);
298 switch (rtlphy
->current_chan_bw
) {
299 case HT_CHANNEL_WIDTH_20
:
300 rtl_set_bbreg(hw
, RFPGA0_RFMOD
, BRFMOD
, 0x0);
301 rtl_set_bbreg(hw
, RFPGA1_RFMOD
, BRFMOD
, 0x0);
302 rtl_set_bbreg(hw
, RFPGA0_ANALOGPARAMETER2
, BIT(10), 1);
304 case HT_CHANNEL_WIDTH_20_40
:
305 rtl_set_bbreg(hw
, RFPGA0_RFMOD
, BRFMOD
, 0x1);
306 rtl_set_bbreg(hw
, RFPGA1_RFMOD
, BRFMOD
, 0x1);
307 rtl_set_bbreg(hw
, RCCK0_SYSTEM
, BCCK_SIDEBAND
,
308 (mac
->cur_40_prime_sc
>> 1));
309 rtl_set_bbreg(hw
, ROFDM1_LSTF
, 0xC00, mac
->cur_40_prime_sc
);
310 rtl_set_bbreg(hw
, RFPGA0_ANALOGPARAMETER2
, BIT(10), 0);
311 rtl_set_bbreg(hw
, 0x818, (BIT(26) | BIT(27)),
312 (mac
->cur_40_prime_sc
==
313 HAL_PRIME_CHNL_OFFSET_LOWER
) ? 2 : 1);
316 pr_err("unknown bandwidth: %#X\n",
317 rtlphy
->current_chan_bw
);
320 rtl92cu_phy_rf6052_set_bandwidth(hw
, rtlphy
->current_chan_bw
);
321 rtlphy
->set_bwmode_inprogress
= false;
322 rtl_dbg(rtlpriv
, COMP_SCAN
, DBG_TRACE
, "<==\n");
325 void rtl92cu_bb_block_on(struct ieee80211_hw
*hw
)
327 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
329 mutex_lock(&rtlpriv
->io
.bb_mutex
);
330 rtl_set_bbreg(hw
, RFPGA0_RFMOD
, BCCKEN
, 0x1);
331 rtl_set_bbreg(hw
, RFPGA0_RFMOD
, BOFDMEN
, 0x1);
332 mutex_unlock(&rtlpriv
->io
.bb_mutex
);
335 void _rtl92cu_phy_lc_calibrate(struct ieee80211_hw
*hw
, bool is2t
)
338 u32 rf_a_mode
= 0, rf_b_mode
= 0, lc_cal
;
339 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
341 tmpreg
= rtl_read_byte(rtlpriv
, 0xd03);
343 if ((tmpreg
& 0x70) != 0)
344 rtl_write_byte(rtlpriv
, 0xd03, tmpreg
& 0x8F);
346 rtl_write_byte(rtlpriv
, REG_TXPAUSE
, 0xFF);
348 if ((tmpreg
& 0x70) != 0) {
349 rf_a_mode
= rtl_get_rfreg(hw
, RF90_PATH_A
, 0x00, MASK12BITS
);
351 rf_b_mode
= rtl_get_rfreg(hw
, RF90_PATH_B
, 0x00,
353 rtl_set_rfreg(hw
, RF90_PATH_A
, 0x00, MASK12BITS
,
354 (rf_a_mode
& 0x8FFFF) | 0x10000);
356 rtl_set_rfreg(hw
, RF90_PATH_B
, 0x00, MASK12BITS
,
357 (rf_b_mode
& 0x8FFFF) | 0x10000);
359 lc_cal
= rtl_get_rfreg(hw
, RF90_PATH_A
, 0x18, MASK12BITS
);
360 rtl_set_rfreg(hw
, RF90_PATH_A
, 0x18, MASK12BITS
, lc_cal
| 0x08000);
362 if ((tmpreg
& 0x70) != 0) {
363 rtl_write_byte(rtlpriv
, 0xd03, tmpreg
);
364 rtl_set_rfreg(hw
, RF90_PATH_A
, 0x00, MASK12BITS
, rf_a_mode
);
366 rtl_set_rfreg(hw
, RF90_PATH_B
, 0x00, MASK12BITS
,
369 rtl_write_byte(rtlpriv
, REG_TXPAUSE
, 0x00);
373 static bool _rtl92cu_phy_set_rf_power_state(struct ieee80211_hw
*hw
,
374 enum rf_pwrstate rfpwr_state
)
376 struct rtl_priv
*rtlpriv
= rtl_priv(hw
);
377 struct rtl_pci_priv
*pcipriv
= rtl_pcipriv(hw
);
378 struct rtl_mac
*mac
= rtl_mac(rtl_priv(hw
));
379 struct rtl_ps_ctl
*ppsc
= rtl_psc(rtl_priv(hw
));
382 struct rtl8192_tx_ring
*ring
= NULL
;
384 switch (rfpwr_state
) {
386 if ((ppsc
->rfpwr_state
== ERFOFF
) &&
387 RT_IN_PS_LEVEL(ppsc
, RT_RF_OFF_LEVL_HALT_NIC
)) {
393 rtl_dbg(rtlpriv
, COMP_RF
, DBG_DMESG
,
394 "IPS Set eRf nic enable\n");
395 rtstatus
= rtl_ps_enable_nic(hw
);
396 } while (!rtstatus
&& (init_count
< 10));
397 RT_CLEAR_PS_LEVEL(ppsc
,
398 RT_RF_OFF_LEVL_HALT_NIC
);
400 rtl_dbg(rtlpriv
, COMP_RF
, DBG_DMESG
,
401 "Set ERFON slept:%d ms\n",
402 jiffies_to_msecs(jiffies
-
403 ppsc
->last_sleep_jiffies
));
404 ppsc
->last_awake_jiffies
= jiffies
;
405 rtl92ce_phy_set_rf_on(hw
);
407 if (mac
->link_state
== MAC80211_LINKED
) {
408 rtlpriv
->cfg
->ops
->led_control(hw
,
411 rtlpriv
->cfg
->ops
->led_control(hw
,
416 for (queue_id
= 0, i
= 0;
417 queue_id
< RTL_PCI_MAX_TX_QUEUE_COUNT
;) {
418 ring
= &pcipriv
->dev
.tx_ring
[queue_id
];
419 if (skb_queue_len(&ring
->queue
) == 0 ||
420 queue_id
== BEACON_QUEUE
) {
424 rtl_dbg(rtlpriv
, COMP_ERR
, DBG_WARNING
,
425 "eRf Off/Sleep: %d times TcbBusyQueue[%d] =%d before doze!\n",
428 skb_queue_len(&ring
->queue
));
432 if (i
>= MAX_DOZE_WAITING_TIMES_9x
) {
433 rtl_dbg(rtlpriv
, COMP_ERR
, DBG_WARNING
,
434 "ERFOFF: %d times TcbBusyQueue[%d] = %d !\n",
435 MAX_DOZE_WAITING_TIMES_9x
,
437 skb_queue_len(&ring
->queue
));
441 if (ppsc
->reg_rfps_level
& RT_RF_OFF_LEVL_HALT_NIC
) {
442 rtl_dbg(rtlpriv
, COMP_RF
, DBG_DMESG
,
443 "IPS Set eRf nic disable\n");
444 rtl_ps_disable_nic(hw
);
445 RT_SET_PS_LEVEL(ppsc
, RT_RF_OFF_LEVL_HALT_NIC
);
447 if (ppsc
->rfoff_reason
== RF_CHANGE_BY_IPS
) {
448 rtlpriv
->cfg
->ops
->led_control(hw
,
451 rtlpriv
->cfg
->ops
->led_control(hw
,
457 if (ppsc
->rfpwr_state
== ERFOFF
)
459 for (queue_id
= 0, i
= 0;
460 queue_id
< RTL_PCI_MAX_TX_QUEUE_COUNT
;) {
461 ring
= &pcipriv
->dev
.tx_ring
[queue_id
];
462 if (skb_queue_len(&ring
->queue
) == 0) {
466 rtl_dbg(rtlpriv
, COMP_ERR
, DBG_WARNING
,
467 "eRf Off/Sleep: %d times TcbBusyQueue[%d] =%d before doze!\n",
469 skb_queue_len(&ring
->queue
));
473 if (i
>= MAX_DOZE_WAITING_TIMES_9x
) {
474 rtl_dbg(rtlpriv
, COMP_ERR
, DBG_WARNING
,
475 "ERFSLEEP: %d times TcbBusyQueue[%d] = %d !\n",
476 MAX_DOZE_WAITING_TIMES_9x
,
478 skb_queue_len(&ring
->queue
));
482 rtl_dbg(rtlpriv
, COMP_RF
, DBG_DMESG
,
483 "Set ERFSLEEP awaked:%d ms\n",
484 jiffies_to_msecs(jiffies
- ppsc
->last_awake_jiffies
));
485 ppsc
->last_sleep_jiffies
= jiffies
;
486 _rtl92c_phy_set_rf_sleep(hw
);
489 pr_err("switch case %#x not processed\n",
495 ppsc
->rfpwr_state
= rfpwr_state
;
499 bool rtl92cu_phy_set_rf_power_state(struct ieee80211_hw
*hw
,
500 enum rf_pwrstate rfpwr_state
)
502 struct rtl_ps_ctl
*ppsc
= rtl_psc(rtl_priv(hw
));
503 bool bresult
= false;
505 if (rfpwr_state
== ppsc
->rfpwr_state
)
507 bresult
= _rtl92cu_phy_set_rf_power_state(hw
, rfpwr_state
);