gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / staging / vt6656 / card.c
blobdc3ab10eb630c01800fca988fec5b644d313e896
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
4 * All rights reserved.
6 * File: card.c
7 * Purpose: Provide functions to setup NIC operation mode
8 * Functions:
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
22 * Revision History:
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().
29 #include <linux/bits.h>
30 #include "device.h"
31 #include "card.h"
32 #include "baseband.h"
33 #include "mac.h"
34 #include "desc.h"
35 #include "rf.h"
36 #include "power.h"
37 #include "key.h"
38 #include "usbpipe.h"
40 /* const u16 cw_rxbcntsf_off[MAX_RATE] =
41 * {17, 34, 96, 192, 34, 23, 17, 11, 8, 5, 4, 3};
44 static const u16 cw_rxbcntsf_off[MAX_RATE] = {
45 192, 96, 34, 17, 34, 23, 17, 11, 8, 5, 4, 3
49 * Description: Set NIC media channel
51 * Parameters:
52 * In:
53 * pDevice - The adapter to be set
54 * connection_channel - Channel to be set
55 * Out:
56 * none
58 void vnt_set_channel(struct vnt_private *priv, u32 connection_channel)
60 if (connection_channel > CB_MAX_CHANNEL || !connection_channel)
61 return;
63 /* clear NAV */
64 vnt_mac_reg_bits_on(priv, MAC_REG_MACCR, MACCR_CLRNAV);
66 /* Set Channel[7] = 0 to tell H/W channel is changing now. */
67 vnt_mac_reg_bits_off(priv, MAC_REG_CHANNEL,
68 (BIT(7) | BIT(5) | BIT(4)));
70 vnt_control_out(priv, MESSAGE_TYPE_SELECT_CHANNEL,
71 connection_channel, 0, 0, NULL);
73 vnt_control_out_u8(priv, MESSAGE_REQUEST_MACREG, MAC_REG_CHANNEL,
74 (u8)(connection_channel | 0x80));
78 * Description: Get CCK mode basic rate
80 * Parameters:
81 * In:
82 * priv - The adapter to be set
83 * rate_idx - Receiving data rate
84 * Out:
85 * none
87 * Return Value: response Control frame rate
90 static u16 vnt_get_cck_rate(struct vnt_private *priv, u16 rate_idx)
92 u16 ui = rate_idx;
94 while (ui > RATE_1M) {
95 if (priv->basic_rates & (1 << ui))
96 return ui;
97 ui--;
100 return RATE_1M;
104 * Description: Get OFDM mode basic rate
106 * Parameters:
107 * In:
108 * priv - The adapter to be set
109 * rate_idx - Receiving data rate
110 * Out:
111 * none
113 * Return Value: response Control frame rate
116 static u16 vnt_get_ofdm_rate(struct vnt_private *priv, u16 rate_idx)
118 u16 ui = rate_idx;
120 dev_dbg(&priv->usb->dev, "%s basic rate: %d\n",
121 __func__, priv->basic_rates);
123 if (!vnt_ofdm_min_rate(priv)) {
124 dev_dbg(&priv->usb->dev, "%s (NO OFDM) %d\n",
125 __func__, rate_idx);
126 if (rate_idx > RATE_24M)
127 rate_idx = RATE_24M;
128 return rate_idx;
131 while (ui > RATE_11M) {
132 if (priv->basic_rates & (1 << ui)) {
133 dev_dbg(&priv->usb->dev, "%s rate: %d\n",
134 __func__, ui);
135 return ui;
137 ui--;
140 dev_dbg(&priv->usb->dev, "%s basic rate: 24M\n", __func__);
142 return RATE_24M;
146 * Description: Calculate TxRate and RsvTime fields for RSPINF in OFDM mode.
148 * Parameters:
149 * In:
150 * rate - Tx Rate
151 * bb_type - Tx Packet type
152 * Out:
153 * tx_rate - pointer to RSPINF TxRate field
154 * rsv_time- pointer to RSPINF RsvTime field
156 * Return Value: none
159 static void vnt_calculate_ofdm_rate(u16 rate, u8 bb_type,
160 u8 *tx_rate, u8 *rsv_time)
162 switch (rate) {
163 case RATE_6M:
164 if (bb_type == BB_TYPE_11A) {
165 *tx_rate = 0x9b;
166 *rsv_time = 24;
167 } else {
168 *tx_rate = 0x8b;
169 *rsv_time = 30;
171 break;
172 case RATE_9M:
173 if (bb_type == BB_TYPE_11A) {
174 *tx_rate = 0x9f;
175 *rsv_time = 16;
176 } else {
177 *tx_rate = 0x8f;
178 *rsv_time = 22;
180 break;
181 case RATE_12M:
182 if (bb_type == BB_TYPE_11A) {
183 *tx_rate = 0x9a;
184 *rsv_time = 12;
185 } else {
186 *tx_rate = 0x8a;
187 *rsv_time = 18;
189 break;
190 case RATE_18M:
191 if (bb_type == BB_TYPE_11A) {
192 *tx_rate = 0x9e;
193 *rsv_time = 8;
194 } else {
195 *tx_rate = 0x8e;
196 *rsv_time = 14;
198 break;
199 case RATE_36M:
200 if (bb_type == BB_TYPE_11A) {
201 *tx_rate = 0x9d;
202 *rsv_time = 4;
203 } else {
204 *tx_rate = 0x8d;
205 *rsv_time = 10;
207 break;
208 case RATE_48M:
209 if (bb_type == BB_TYPE_11A) {
210 *tx_rate = 0x98;
211 *rsv_time = 4;
212 } else {
213 *tx_rate = 0x88;
214 *rsv_time = 10;
216 break;
217 case RATE_54M:
218 if (bb_type == BB_TYPE_11A) {
219 *tx_rate = 0x9c;
220 *rsv_time = 4;
221 } else {
222 *tx_rate = 0x8c;
223 *rsv_time = 10;
225 break;
226 case RATE_24M:
227 default:
228 if (bb_type == BB_TYPE_11A) {
229 *tx_rate = 0x99;
230 *rsv_time = 8;
231 } else {
232 *tx_rate = 0x89;
233 *rsv_time = 14;
235 break;
240 * Description: Set RSPINF
242 * Parameters:
243 * In:
244 * pDevice - The adapter to be set
245 * Out:
246 * none
248 * Return Value: None.
252 void vnt_set_rspinf(struct vnt_private *priv, u8 bb_type)
254 struct vnt_phy_field phy[4];
255 u8 tx_rate[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; /* For OFDM */
256 u8 rsv_time[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
257 u8 data[34];
258 int i;
260 /*RSPINF_b_1*/
261 vnt_get_phy_field(priv, 14, vnt_get_cck_rate(priv, RATE_1M),
262 PK_TYPE_11B, &phy[0]);
264 /*RSPINF_b_2*/
265 vnt_get_phy_field(priv, 14, vnt_get_cck_rate(priv, RATE_2M),
266 PK_TYPE_11B, &phy[1]);
268 /*RSPINF_b_5*/
269 vnt_get_phy_field(priv, 14, vnt_get_cck_rate(priv, RATE_5M),
270 PK_TYPE_11B, &phy[2]);
272 /*RSPINF_b_11*/
273 vnt_get_phy_field(priv, 14, vnt_get_cck_rate(priv, RATE_11M),
274 PK_TYPE_11B, &phy[3]);
276 /*RSPINF_a_6*/
277 vnt_calculate_ofdm_rate(RATE_6M, bb_type, &tx_rate[0], &rsv_time[0]);
279 /*RSPINF_a_9*/
280 vnt_calculate_ofdm_rate(RATE_9M, bb_type, &tx_rate[1], &rsv_time[1]);
282 /*RSPINF_a_12*/
283 vnt_calculate_ofdm_rate(RATE_12M, bb_type, &tx_rate[2], &rsv_time[2]);
285 /*RSPINF_a_18*/
286 vnt_calculate_ofdm_rate(RATE_18M, bb_type, &tx_rate[3], &rsv_time[3]);
288 /*RSPINF_a_24*/
289 vnt_calculate_ofdm_rate(RATE_24M, bb_type, &tx_rate[4], &rsv_time[4]);
291 /*RSPINF_a_36*/
292 vnt_calculate_ofdm_rate(vnt_get_ofdm_rate(priv, RATE_36M),
293 bb_type, &tx_rate[5], &rsv_time[5]);
295 /*RSPINF_a_48*/
296 vnt_calculate_ofdm_rate(vnt_get_ofdm_rate(priv, RATE_48M),
297 bb_type, &tx_rate[6], &rsv_time[6]);
299 /*RSPINF_a_54*/
300 vnt_calculate_ofdm_rate(vnt_get_ofdm_rate(priv, RATE_54M),
301 bb_type, &tx_rate[7], &rsv_time[7]);
303 /*RSPINF_a_72*/
304 vnt_calculate_ofdm_rate(vnt_get_ofdm_rate(priv, RATE_54M),
305 bb_type, &tx_rate[8], &rsv_time[8]);
307 put_unaligned(phy[0].len, (u16 *)&data[0]);
308 data[2] = phy[0].signal;
309 data[3] = phy[0].service;
311 put_unaligned(phy[1].len, (u16 *)&data[4]);
312 data[6] = phy[1].signal;
313 data[7] = phy[1].service;
315 put_unaligned(phy[2].len, (u16 *)&data[8]);
316 data[10] = phy[2].signal;
317 data[11] = phy[2].service;
319 put_unaligned(phy[3].len, (u16 *)&data[12]);
320 data[14] = phy[3].signal;
321 data[15] = phy[3].service;
323 for (i = 0; i < 9; i++) {
324 data[16 + i * 2] = tx_rate[i];
325 data[16 + i * 2 + 1] = rsv_time[i];
328 vnt_control_out(priv, MESSAGE_TYPE_WRITE, MAC_REG_RSPINF_B_1,
329 MESSAGE_REQUEST_MACREG, 34, &data[0]);
333 * Description: Update IFS
335 * Parameters:
336 * In:
337 * priv - The adapter to be set
338 * Out:
339 * none
341 * Return Value: None.
344 void vnt_update_ifs(struct vnt_private *priv)
346 u8 max_min = 0;
347 u8 data[4];
349 if (priv->packet_type == PK_TYPE_11A) {
350 priv->slot = C_SLOT_SHORT;
351 priv->sifs = C_SIFS_A;
352 priv->difs = C_SIFS_A + 2 * C_SLOT_SHORT;
353 max_min = 4;
354 } else {
355 priv->sifs = C_SIFS_BG;
357 if (priv->short_slot_time) {
358 priv->slot = C_SLOT_SHORT;
359 max_min = 4;
360 } else {
361 priv->slot = C_SLOT_LONG;
362 max_min = 5;
365 priv->difs = C_SIFS_BG + 2 * priv->slot;
368 priv->eifs = C_EIFS;
370 switch (priv->rf_type) {
371 case RF_VT3226D0:
372 if (priv->bb_type != BB_TYPE_11B) {
373 priv->sifs -= 1;
374 priv->difs -= 1;
375 break;
377 /* fall through */
378 case RF_AIROHA7230:
379 case RF_AL2230:
380 case RF_AL2230S:
381 if (priv->bb_type != BB_TYPE_11B)
382 break;
383 /* fall through */
384 case RF_RFMD2959:
385 case RF_VT3226:
386 case RF_VT3342A0:
387 priv->sifs -= 3;
388 priv->difs -= 3;
389 break;
390 case RF_MAXIM2829:
391 if (priv->bb_type == BB_TYPE_11A) {
392 priv->sifs -= 5;
393 priv->difs -= 5;
394 } else {
395 priv->sifs -= 2;
396 priv->difs -= 2;
399 break;
402 data[0] = (u8)priv->sifs;
403 data[1] = (u8)priv->difs;
404 data[2] = (u8)priv->eifs;
405 data[3] = (u8)priv->slot;
407 vnt_control_out(priv, MESSAGE_TYPE_WRITE, MAC_REG_SIFS,
408 MESSAGE_REQUEST_MACREG, 4, &data[0]);
410 max_min |= 0xa0;
412 vnt_control_out(priv, MESSAGE_TYPE_WRITE, MAC_REG_CWMAXMIN0,
413 MESSAGE_REQUEST_MACREG, 1, &max_min);
416 void vnt_update_top_rates(struct vnt_private *priv)
418 u8 top_ofdm = RATE_24M, top_cck = RATE_1M;
419 u8 i;
421 /*Determines the highest basic rate.*/
422 for (i = RATE_54M; i >= RATE_6M; i--) {
423 if (priv->basic_rates & (u16)(1 << i)) {
424 top_ofdm = i;
425 break;
429 priv->top_ofdm_basic_rate = top_ofdm;
431 for (i = RATE_11M;; i--) {
432 if (priv->basic_rates & (u16)(1 << i)) {
433 top_cck = i;
434 break;
436 if (i == RATE_1M)
437 break;
440 priv->top_cck_basic_rate = top_cck;
443 int vnt_ofdm_min_rate(struct vnt_private *priv)
445 int ii;
447 for (ii = RATE_54M; ii >= RATE_6M; ii--) {
448 if ((priv->basic_rates) & ((u16)BIT(ii)))
449 return true;
452 return false;
455 u8 vnt_get_pkt_type(struct vnt_private *priv)
457 if (priv->bb_type == BB_TYPE_11A || priv->bb_type == BB_TYPE_11B)
458 return (u8)priv->bb_type;
459 else if (vnt_ofdm_min_rate(priv))
460 return PK_TYPE_11GA;
461 return PK_TYPE_11GB;
465 * Description: Calculate TSF offset of two TSF input
466 * Get TSF Offset from RxBCN's TSF and local TSF
468 * Parameters:
469 * In:
470 * rx_rate - rx rate.
471 * tsf1 - Rx BCN's TSF
472 * tsf2 - Local TSF
473 * Out:
474 * none
476 * Return Value: TSF Offset value
479 u64 vnt_get_tsf_offset(u8 rx_rate, u64 tsf1, u64 tsf2)
481 return tsf1 - tsf2 - (u64)cw_rxbcntsf_off[rx_rate % MAX_RATE];
485 * Description: Sync. TSF counter to BSS
486 * Get TSF offset and write to HW
488 * Parameters:
489 * In:
490 * priv - The adapter to be sync.
491 * time_stamp - Rx BCN's TSF
492 * local_tsf - Local TSF
493 * Out:
494 * none
496 * Return Value: none
499 void vnt_adjust_tsf(struct vnt_private *priv, u8 rx_rate,
500 u64 time_stamp, u64 local_tsf)
502 u64 tsf_offset = 0;
503 u8 data[8];
505 tsf_offset = vnt_get_tsf_offset(rx_rate, time_stamp, local_tsf);
507 data[0] = (u8)tsf_offset;
508 data[1] = (u8)(tsf_offset >> 8);
509 data[2] = (u8)(tsf_offset >> 16);
510 data[3] = (u8)(tsf_offset >> 24);
511 data[4] = (u8)(tsf_offset >> 32);
512 data[5] = (u8)(tsf_offset >> 40);
513 data[6] = (u8)(tsf_offset >> 48);
514 data[7] = (u8)(tsf_offset >> 56);
516 vnt_control_out(priv, MESSAGE_TYPE_SET_TSFTBTT,
517 MESSAGE_REQUEST_TSF, 0, 8, data);
521 * Description: Read NIC TSF counter
522 * Get local TSF counter
524 * Parameters:
525 * In:
526 * priv - The adapter to be read
527 * Out:
528 * current_tsf - Current TSF counter
530 * Return Value: true if success; otherwise false
533 bool vnt_get_current_tsf(struct vnt_private *priv, u64 *current_tsf)
535 *current_tsf = priv->current_tsf;
537 return true;
541 * Description: Clear NIC TSF counter
542 * Clear local TSF counter
544 * Parameters:
545 * In:
546 * priv - The adapter to be read
548 * Return Value: true if success; otherwise false
551 bool vnt_clear_current_tsf(struct vnt_private *priv)
553 vnt_mac_reg_bits_on(priv, MAC_REG_TFTCTL, TFTCTL_TSFCNTRST);
555 priv->current_tsf = 0;
557 return true;
561 * Description: Read NIC TSF counter
562 * Get NEXTTBTT from adjusted TSF and Beacon Interval
564 * Parameters:
565 * In:
566 * tsf - Current TSF counter
567 * beacon_interval - Beacon Interval
568 * Out:
569 * tsf - Current TSF counter
571 * Return Value: TSF value of next Beacon
574 u64 vnt_get_next_tbtt(u64 tsf, u16 beacon_interval)
576 u32 beacon_int;
578 beacon_int = beacon_interval * 1024;
580 /* Next TBTT =
581 * ((local_current_TSF / beacon_interval) + 1) * beacon_interval
583 if (beacon_int) {
584 do_div(tsf, beacon_int);
585 tsf += 1;
586 tsf *= beacon_int;
589 return tsf;
593 * Description: Set NIC TSF counter for first Beacon time
594 * Get NEXTTBTT from adjusted TSF and Beacon Interval
596 * Parameters:
597 * In:
598 * dwIoBase - IO Base
599 * beacon_interval - Beacon Interval
600 * Out:
601 * none
603 * Return Value: none
606 void vnt_reset_next_tbtt(struct vnt_private *priv, u16 beacon_interval)
608 u64 next_tbtt = 0;
609 u8 data[8];
611 vnt_clear_current_tsf(priv);
613 next_tbtt = vnt_get_next_tbtt(next_tbtt, beacon_interval);
615 data[0] = (u8)next_tbtt;
616 data[1] = (u8)(next_tbtt >> 8);
617 data[2] = (u8)(next_tbtt >> 16);
618 data[3] = (u8)(next_tbtt >> 24);
619 data[4] = (u8)(next_tbtt >> 32);
620 data[5] = (u8)(next_tbtt >> 40);
621 data[6] = (u8)(next_tbtt >> 48);
622 data[7] = (u8)(next_tbtt >> 56);
624 vnt_control_out(priv, MESSAGE_TYPE_SET_TSFTBTT,
625 MESSAGE_REQUEST_TBTT, 0, 8, data);
629 * Description: Sync NIC TSF counter for Beacon time
630 * Get NEXTTBTT and write to HW
632 * Parameters:
633 * In:
634 * priv - The adapter to be set
635 * tsf - Current TSF counter
636 * beacon_interval - Beacon Interval
637 * Out:
638 * none
640 * Return Value: none
643 void vnt_update_next_tbtt(struct vnt_private *priv, u64 tsf,
644 u16 beacon_interval)
646 u8 data[8];
648 tsf = vnt_get_next_tbtt(tsf, beacon_interval);
650 data[0] = (u8)tsf;
651 data[1] = (u8)(tsf >> 8);
652 data[2] = (u8)(tsf >> 16);
653 data[3] = (u8)(tsf >> 24);
654 data[4] = (u8)(tsf >> 32);
655 data[5] = (u8)(tsf >> 40);
656 data[6] = (u8)(tsf >> 48);
657 data[7] = (u8)(tsf >> 56);
659 vnt_control_out(priv, MESSAGE_TYPE_SET_TSFTBTT,
660 MESSAGE_REQUEST_TBTT, 0, 8, data);
662 dev_dbg(&priv->usb->dev, "%s TBTT: %8llx\n", __func__, tsf);
666 * Description: Turn off Radio power
668 * Parameters:
669 * In:
670 * priv - The adapter to be turned off
671 * Out:
672 * none
674 * Return Value: true if success; otherwise false
677 int vnt_radio_power_off(struct vnt_private *priv)
679 int ret = 0;
681 switch (priv->rf_type) {
682 case RF_AL2230:
683 case RF_AL2230S:
684 case RF_AIROHA7230:
685 case RF_VT3226:
686 case RF_VT3226D0:
687 case RF_VT3342A0:
688 ret = vnt_mac_reg_bits_off(priv, MAC_REG_SOFTPWRCTL,
689 (SOFTPWRCTL_SWPE2 | SOFTPWRCTL_SWPE3));
690 break;
693 if (ret)
694 goto end;
696 ret = vnt_mac_reg_bits_off(priv, MAC_REG_HOSTCR, HOSTCR_RXON);
697 if (ret)
698 goto end;
700 ret = vnt_set_deep_sleep(priv);
701 if (ret)
702 goto end;
704 ret = vnt_mac_reg_bits_on(priv, MAC_REG_GPIOCTL1, GPIO3_INTMD);
706 end:
707 return ret;
711 * Description: Turn on Radio power
713 * Parameters:
714 * In:
715 * priv - The adapter to be turned on
716 * Out:
717 * none
719 * Return Value: true if success; otherwise false
722 int vnt_radio_power_on(struct vnt_private *priv)
724 int ret = 0;
726 vnt_exit_deep_sleep(priv);
728 vnt_mac_reg_bits_on(priv, MAC_REG_HOSTCR, HOSTCR_RXON);
730 switch (priv->rf_type) {
731 case RF_AL2230:
732 case RF_AL2230S:
733 case RF_AIROHA7230:
734 case RF_VT3226:
735 case RF_VT3226D0:
736 case RF_VT3342A0:
737 vnt_mac_reg_bits_on(priv, MAC_REG_SOFTPWRCTL,
738 (SOFTPWRCTL_SWPE2 | SOFTPWRCTL_SWPE3));
739 break;
742 vnt_mac_reg_bits_off(priv, MAC_REG_GPIOCTL1, GPIO3_INTMD);
744 return ret;
747 void vnt_set_bss_mode(struct vnt_private *priv)
749 if (priv->rf_type == RF_AIROHA7230 && priv->bb_type == BB_TYPE_11A)
750 vnt_mac_set_bb_type(priv, BB_TYPE_11G);
751 else
752 vnt_mac_set_bb_type(priv, priv->bb_type);
754 priv->packet_type = vnt_get_pkt_type(priv);
756 if (priv->bb_type == BB_TYPE_11A)
757 vnt_control_out_u8(priv, MESSAGE_REQUEST_BBREG, 0x88, 0x03);
758 else if (priv->bb_type == BB_TYPE_11B)
759 vnt_control_out_u8(priv, MESSAGE_REQUEST_BBREG, 0x88, 0x02);
760 else if (priv->bb_type == BB_TYPE_11G)
761 vnt_control_out_u8(priv, MESSAGE_REQUEST_BBREG, 0x88, 0x08);
763 vnt_update_ifs(priv);
764 vnt_set_rspinf(priv, (u8)priv->bb_type);
766 if (priv->bb_type == BB_TYPE_11A) {
767 if (priv->rf_type == RF_AIROHA7230) {
768 priv->bb_vga[0] = 0x20;
770 vnt_control_out_u8(priv, MESSAGE_REQUEST_BBREG,
771 0xe7, priv->bb_vga[0]);
774 priv->bb_vga[2] = 0x10;
775 priv->bb_vga[3] = 0x10;
776 } else {
777 if (priv->rf_type == RF_AIROHA7230) {
778 priv->bb_vga[0] = 0x1c;
780 vnt_control_out_u8(priv, MESSAGE_REQUEST_BBREG,
781 0xe7, priv->bb_vga[0]);
784 priv->bb_vga[2] = 0x0;
785 priv->bb_vga[3] = 0x0;
788 vnt_set_vga_gain_offset(priv, priv->bb_vga[0]);