1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
7 * Purpose: Provide functions to setup NIC operation mode
9 * vnt_set_rspinf - Set RSPINF
10 * vnt_update_ifs - Update slotTime,SIFS,DIFS, and EIFS
11 * vnt_update_top_rates - Update BasicTopRate
12 * vnt_add_basic_rate - Add to BasicRateSet
13 * vnt_ofdm_min_rate - Check if any OFDM rate is in BasicRateSet
14 * vnt_get_tsf_offset - Calculate TSFOffset
15 * vnt_get_current_tsf - Read Current NIC TSF counter
16 * vnt_get_next_tbtt - Calculate Next Beacon TSF counter
17 * vnt_reset_next_tbtt - Set NIC Beacon time
18 * vnt_update_next_tbtt - Sync. NIC Beacon time
19 * vnt_radio_power_off - Turn Off NIC Radio Power
20 * vnt_radio_power_on - Turn On NIC Radio Power
23 * 06-10-2003 Bryan YC Fan: Re-write codes to support VT3253 spec.
24 * 08-26-2003 Kyle Hsu: Modify the definition type of dwIoBase.
25 * 09-01-2003 Bryan YC Fan: Add vnt_update_ifs().
39 /* const u16 cw_rxbcntsf_off[MAX_RATE] =
40 * {17, 34, 96, 192, 34, 23, 17, 11, 8, 5, 4, 3};
43 static const u16 cw_rxbcntsf_off
[MAX_RATE
] = {
44 192, 96, 34, 17, 34, 23, 17, 11, 8, 5, 4, 3
48 * Description: Set NIC media channel
52 * pDevice - The adapter to be set
53 * connection_channel - Channel to be set
57 void vnt_set_channel(struct vnt_private
*priv
, u32 connection_channel
)
59 if (connection_channel
> CB_MAX_CHANNEL
|| !connection_channel
)
63 vnt_mac_reg_bits_on(priv
, MAC_REG_MACCR
, MACCR_CLRNAV
);
65 /* Set Channel[7] = 0 to tell H/W channel is changing now. */
66 vnt_mac_reg_bits_off(priv
, MAC_REG_CHANNEL
, 0xb0);
68 vnt_control_out(priv
, MESSAGE_TYPE_SELECT_CHANNEL
,
69 connection_channel
, 0, 0, NULL
);
71 vnt_control_out_u8(priv
, MESSAGE_REQUEST_MACREG
, MAC_REG_CHANNEL
,
72 (u8
)(connection_channel
| 0x80));
76 * Description: Get CCK mode basic rate
80 * priv - The adapter to be set
81 * rate_idx - Receiving data rate
85 * Return Value: response Control frame rate
88 static u16
vnt_get_cck_rate(struct vnt_private
*priv
, u16 rate_idx
)
92 while (ui
> RATE_1M
) {
93 if (priv
->basic_rates
& (1 << ui
))
102 * Description: Get OFDM mode basic rate
106 * priv - The adapter to be set
107 * rate_idx - Receiving data rate
111 * Return Value: response Control frame rate
114 static u16
vnt_get_ofdm_rate(struct vnt_private
*priv
, u16 rate_idx
)
118 dev_dbg(&priv
->usb
->dev
, "%s basic rate: %d\n",
119 __func__
, priv
->basic_rates
);
121 if (!vnt_ofdm_min_rate(priv
)) {
122 dev_dbg(&priv
->usb
->dev
, "%s (NO OFDM) %d\n",
124 if (rate_idx
> RATE_24M
)
129 while (ui
> RATE_11M
) {
130 if (priv
->basic_rates
& (1 << ui
)) {
131 dev_dbg(&priv
->usb
->dev
, "%s rate: %d\n",
138 dev_dbg(&priv
->usb
->dev
, "%s basic rate: 24M\n", __func__
);
144 * Description: Calculate TxRate and RsvTime fields for RSPINF in OFDM mode.
149 * bb_type - Tx Packet type
151 * tx_rate - pointer to RSPINF TxRate field
152 * rsv_time- pointer to RSPINF RsvTime field
157 static void vnt_calculate_ofdm_rate(u16 rate
, u8 bb_type
,
158 u8
*tx_rate
, u8
*rsv_time
)
162 if (bb_type
== BB_TYPE_11A
) {
171 if (bb_type
== BB_TYPE_11A
) {
180 if (bb_type
== BB_TYPE_11A
) {
189 if (bb_type
== BB_TYPE_11A
) {
198 if (bb_type
== BB_TYPE_11A
) {
207 if (bb_type
== BB_TYPE_11A
) {
216 if (bb_type
== BB_TYPE_11A
) {
226 if (bb_type
== BB_TYPE_11A
) {
238 * Description: Set RSPINF
242 * pDevice - The adapter to be set
246 * Return Value: None.
250 void vnt_set_rspinf(struct vnt_private
*priv
, u8 bb_type
)
252 struct vnt_phy_field phy
[4];
253 u8 tx_rate
[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; /* For OFDM */
254 u8 rsv_time
[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
259 vnt_get_phy_field(priv
, 14, vnt_get_cck_rate(priv
, RATE_1M
),
260 PK_TYPE_11B
, &phy
[0]);
263 vnt_get_phy_field(priv
, 14, vnt_get_cck_rate(priv
, RATE_2M
),
264 PK_TYPE_11B
, &phy
[1]);
267 vnt_get_phy_field(priv
, 14, vnt_get_cck_rate(priv
, RATE_5M
),
268 PK_TYPE_11B
, &phy
[2]);
271 vnt_get_phy_field(priv
, 14, vnt_get_cck_rate(priv
, RATE_11M
),
272 PK_TYPE_11B
, &phy
[3]);
275 vnt_calculate_ofdm_rate(RATE_6M
, bb_type
, &tx_rate
[0], &rsv_time
[0]);
278 vnt_calculate_ofdm_rate(RATE_9M
, bb_type
, &tx_rate
[1], &rsv_time
[1]);
281 vnt_calculate_ofdm_rate(RATE_12M
, bb_type
, &tx_rate
[2], &rsv_time
[2]);
284 vnt_calculate_ofdm_rate(RATE_18M
, bb_type
, &tx_rate
[3], &rsv_time
[3]);
287 vnt_calculate_ofdm_rate(RATE_24M
, bb_type
, &tx_rate
[4], &rsv_time
[4]);
290 vnt_calculate_ofdm_rate(vnt_get_ofdm_rate(priv
, RATE_36M
),
291 bb_type
, &tx_rate
[5], &rsv_time
[5]);
294 vnt_calculate_ofdm_rate(vnt_get_ofdm_rate(priv
, RATE_48M
),
295 bb_type
, &tx_rate
[6], &rsv_time
[6]);
298 vnt_calculate_ofdm_rate(vnt_get_ofdm_rate(priv
, RATE_54M
),
299 bb_type
, &tx_rate
[7], &rsv_time
[7]);
302 vnt_calculate_ofdm_rate(vnt_get_ofdm_rate(priv
, RATE_54M
),
303 bb_type
, &tx_rate
[8], &rsv_time
[8]);
305 put_unaligned(phy
[0].len
, (u16
*)&data
[0]);
306 data
[2] = phy
[0].signal
;
307 data
[3] = phy
[0].service
;
309 put_unaligned(phy
[1].len
, (u16
*)&data
[4]);
310 data
[6] = phy
[1].signal
;
311 data
[7] = phy
[1].service
;
313 put_unaligned(phy
[2].len
, (u16
*)&data
[8]);
314 data
[10] = phy
[2].signal
;
315 data
[11] = phy
[2].service
;
317 put_unaligned(phy
[3].len
, (u16
*)&data
[12]);
318 data
[14] = phy
[3].signal
;
319 data
[15] = phy
[3].service
;
321 for (i
= 0; i
< 9; i
++) {
322 data
[16 + i
* 2] = tx_rate
[i
];
323 data
[16 + i
* 2 + 1] = rsv_time
[i
];
326 vnt_control_out(priv
, MESSAGE_TYPE_WRITE
, MAC_REG_RSPINF_B_1
,
327 MESSAGE_REQUEST_MACREG
, 34, &data
[0]);
331 * Description: Update IFS
335 * priv - The adapter to be set
339 * Return Value: None.
342 void vnt_update_ifs(struct vnt_private
*priv
)
347 if (priv
->packet_type
== PK_TYPE_11A
) {
348 priv
->slot
= C_SLOT_SHORT
;
349 priv
->sifs
= C_SIFS_A
;
350 priv
->difs
= C_SIFS_A
+ 2 * C_SLOT_SHORT
;
353 priv
->sifs
= C_SIFS_BG
;
355 if (priv
->short_slot_time
) {
356 priv
->slot
= C_SLOT_SHORT
;
359 priv
->slot
= C_SLOT_LONG
;
363 priv
->difs
= C_SIFS_BG
+ 2 * priv
->slot
;
368 switch (priv
->rf_type
) {
370 if (priv
->bb_type
!= BB_TYPE_11B
) {
379 if (priv
->bb_type
!= BB_TYPE_11B
)
389 if (priv
->bb_type
== BB_TYPE_11A
) {
400 data
[0] = (u8
)priv
->sifs
;
401 data
[1] = (u8
)priv
->difs
;
402 data
[2] = (u8
)priv
->eifs
;
403 data
[3] = (u8
)priv
->slot
;
405 vnt_control_out(priv
, MESSAGE_TYPE_WRITE
, MAC_REG_SIFS
,
406 MESSAGE_REQUEST_MACREG
, 4, &data
[0]);
410 vnt_control_out(priv
, MESSAGE_TYPE_WRITE
, MAC_REG_CWMAXMIN0
,
411 MESSAGE_REQUEST_MACREG
, 1, &max_min
);
414 void vnt_update_top_rates(struct vnt_private
*priv
)
416 u8 top_ofdm
= RATE_24M
, top_cck
= RATE_1M
;
419 /*Determines the highest basic rate.*/
420 for (i
= RATE_54M
; i
>= RATE_6M
; i
--) {
421 if (priv
->basic_rates
& (u16
)(1 << i
)) {
427 priv
->top_ofdm_basic_rate
= top_ofdm
;
429 for (i
= RATE_11M
;; i
--) {
430 if (priv
->basic_rates
& (u16
)(1 << i
)) {
438 priv
->top_cck_basic_rate
= top_cck
;
441 int vnt_ofdm_min_rate(struct vnt_private
*priv
)
445 for (ii
= RATE_54M
; ii
>= RATE_6M
; ii
--) {
446 if ((priv
->basic_rates
) & ((u16
)BIT(ii
)))
453 u8
vnt_get_pkt_type(struct vnt_private
*priv
)
455 if (priv
->bb_type
== BB_TYPE_11A
|| priv
->bb_type
== BB_TYPE_11B
)
456 return (u8
)priv
->bb_type
;
457 else if (vnt_ofdm_min_rate(priv
))
463 * Description: Calculate TSF offset of two TSF input
464 * Get TSF Offset from RxBCN's TSF and local TSF
469 * tsf1 - Rx BCN's TSF
474 * Return Value: TSF Offset value
477 u64
vnt_get_tsf_offset(u8 rx_rate
, u64 tsf1
, u64 tsf2
)
479 return tsf1
- tsf2
- (u64
)cw_rxbcntsf_off
[rx_rate
% MAX_RATE
];
483 * Description: Sync. TSF counter to BSS
484 * Get TSF offset and write to HW
488 * priv - The adapter to be sync.
489 * time_stamp - Rx BCN's TSF
490 * local_tsf - Local TSF
497 void vnt_adjust_tsf(struct vnt_private
*priv
, u8 rx_rate
,
498 u64 time_stamp
, u64 local_tsf
)
503 tsf_offset
= vnt_get_tsf_offset(rx_rate
, time_stamp
, local_tsf
);
505 data
[0] = (u8
)tsf_offset
;
506 data
[1] = (u8
)(tsf_offset
>> 8);
507 data
[2] = (u8
)(tsf_offset
>> 16);
508 data
[3] = (u8
)(tsf_offset
>> 24);
509 data
[4] = (u8
)(tsf_offset
>> 32);
510 data
[5] = (u8
)(tsf_offset
>> 40);
511 data
[6] = (u8
)(tsf_offset
>> 48);
512 data
[7] = (u8
)(tsf_offset
>> 56);
514 vnt_control_out(priv
, MESSAGE_TYPE_SET_TSFTBTT
,
515 MESSAGE_REQUEST_TSF
, 0, 8, data
);
519 * Description: Read NIC TSF counter
520 * Get local TSF counter
524 * priv - The adapter to be read
526 * current_tsf - Current TSF counter
528 * Return Value: true if success; otherwise false
531 bool vnt_get_current_tsf(struct vnt_private
*priv
, u64
*current_tsf
)
533 *current_tsf
= priv
->current_tsf
;
539 * Description: Clear NIC TSF counter
540 * Clear local TSF counter
544 * priv - The adapter to be read
546 * Return Value: true if success; otherwise false
549 bool vnt_clear_current_tsf(struct vnt_private
*priv
)
551 vnt_mac_reg_bits_on(priv
, MAC_REG_TFTCTL
, TFTCTL_TSFCNTRST
);
553 priv
->current_tsf
= 0;
559 * Description: Read NIC TSF counter
560 * Get NEXTTBTT from adjusted TSF and Beacon Interval
564 * tsf - Current TSF counter
565 * beacon_interval - Beacon Interval
567 * tsf - Current TSF counter
569 * Return Value: TSF value of next Beacon
572 u64
vnt_get_next_tbtt(u64 tsf
, u16 beacon_interval
)
576 beacon_int
= beacon_interval
* 1024;
579 * ((local_current_TSF / beacon_interval) + 1) * beacon_interval
582 do_div(tsf
, beacon_int
);
591 * Description: Set NIC TSF counter for first Beacon time
592 * Get NEXTTBTT from adjusted TSF and Beacon Interval
597 * beacon_interval - Beacon Interval
604 void vnt_reset_next_tbtt(struct vnt_private
*priv
, u16 beacon_interval
)
609 vnt_clear_current_tsf(priv
);
611 next_tbtt
= vnt_get_next_tbtt(next_tbtt
, beacon_interval
);
613 data
[0] = (u8
)next_tbtt
;
614 data
[1] = (u8
)(next_tbtt
>> 8);
615 data
[2] = (u8
)(next_tbtt
>> 16);
616 data
[3] = (u8
)(next_tbtt
>> 24);
617 data
[4] = (u8
)(next_tbtt
>> 32);
618 data
[5] = (u8
)(next_tbtt
>> 40);
619 data
[6] = (u8
)(next_tbtt
>> 48);
620 data
[7] = (u8
)(next_tbtt
>> 56);
622 vnt_control_out(priv
, MESSAGE_TYPE_SET_TSFTBTT
,
623 MESSAGE_REQUEST_TBTT
, 0, 8, data
);
627 * Description: Sync NIC TSF counter for Beacon time
628 * Get NEXTTBTT and write to HW
632 * priv - The adapter to be set
633 * tsf - Current TSF counter
634 * beacon_interval - Beacon Interval
641 void vnt_update_next_tbtt(struct vnt_private
*priv
, u64 tsf
,
646 tsf
= vnt_get_next_tbtt(tsf
, beacon_interval
);
649 data
[1] = (u8
)(tsf
>> 8);
650 data
[2] = (u8
)(tsf
>> 16);
651 data
[3] = (u8
)(tsf
>> 24);
652 data
[4] = (u8
)(tsf
>> 32);
653 data
[5] = (u8
)(tsf
>> 40);
654 data
[6] = (u8
)(tsf
>> 48);
655 data
[7] = (u8
)(tsf
>> 56);
657 vnt_control_out(priv
, MESSAGE_TYPE_SET_TSFTBTT
,
658 MESSAGE_REQUEST_TBTT
, 0, 8, data
);
660 dev_dbg(&priv
->usb
->dev
, "%s TBTT: %8llx\n", __func__
, tsf
);
664 * Description: Turn off Radio power
668 * priv - The adapter to be turned off
672 * Return Value: true if success; otherwise false
675 int vnt_radio_power_off(struct vnt_private
*priv
)
679 switch (priv
->rf_type
) {
686 ret
= vnt_mac_reg_bits_off(priv
, MAC_REG_SOFTPWRCTL
,
687 (SOFTPWRCTL_SWPE2
| SOFTPWRCTL_SWPE3
));
694 ret
= vnt_mac_reg_bits_off(priv
, MAC_REG_HOSTCR
, HOSTCR_RXON
);
698 ret
= vnt_set_deep_sleep(priv
);
702 ret
= vnt_mac_reg_bits_on(priv
, MAC_REG_GPIOCTL1
, GPIO3_INTMD
);
709 * Description: Turn on Radio power
713 * priv - The adapter to be turned on
717 * Return Value: true if success; otherwise false
720 int vnt_radio_power_on(struct vnt_private
*priv
)
724 vnt_exit_deep_sleep(priv
);
726 vnt_mac_reg_bits_on(priv
, MAC_REG_HOSTCR
, HOSTCR_RXON
);
728 switch (priv
->rf_type
) {
735 vnt_mac_reg_bits_on(priv
, MAC_REG_SOFTPWRCTL
,
736 (SOFTPWRCTL_SWPE2
| SOFTPWRCTL_SWPE3
));
740 vnt_mac_reg_bits_off(priv
, MAC_REG_GPIOCTL1
, GPIO3_INTMD
);
745 void vnt_set_bss_mode(struct vnt_private
*priv
)
747 if (priv
->rf_type
== RF_AIROHA7230
&& priv
->bb_type
== BB_TYPE_11A
)
748 vnt_mac_set_bb_type(priv
, BB_TYPE_11G
);
750 vnt_mac_set_bb_type(priv
, priv
->bb_type
);
752 priv
->packet_type
= vnt_get_pkt_type(priv
);
754 if (priv
->bb_type
== BB_TYPE_11A
)
755 vnt_control_out_u8(priv
, MESSAGE_REQUEST_BBREG
, 0x88, 0x03);
756 else if (priv
->bb_type
== BB_TYPE_11B
)
757 vnt_control_out_u8(priv
, MESSAGE_REQUEST_BBREG
, 0x88, 0x02);
758 else if (priv
->bb_type
== BB_TYPE_11G
)
759 vnt_control_out_u8(priv
, MESSAGE_REQUEST_BBREG
, 0x88, 0x08);
761 vnt_update_ifs(priv
);
762 vnt_set_rspinf(priv
, (u8
)priv
->bb_type
);
764 if (priv
->bb_type
== BB_TYPE_11A
) {
765 if (priv
->rf_type
== RF_AIROHA7230
) {
766 priv
->bb_vga
[0] = 0x20;
768 vnt_control_out_u8(priv
, MESSAGE_REQUEST_BBREG
,
769 0xe7, priv
->bb_vga
[0]);
772 priv
->bb_vga
[2] = 0x10;
773 priv
->bb_vga
[3] = 0x10;
775 if (priv
->rf_type
== RF_AIROHA7230
) {
776 priv
->bb_vga
[0] = 0x1c;
778 vnt_control_out_u8(priv
, MESSAGE_REQUEST_BBREG
,
779 0xe7, priv
->bb_vga
[0]);
782 priv
->bb_vga
[2] = 0x0;
783 priv
->bb_vga
[3] = 0x0;
786 vnt_set_vga_gain_offset(priv
, priv
->bb_vga
[0]);