staging: vt6655: CARDbSetPhyParameter remove unused parameters
[firefly-linux-kernel-4.4.55.git] / drivers / staging / vt6655 / card.c
1 /*
2  * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
3  * All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * File: card.c
20  * Purpose: Provide functions to setup NIC operation mode
21  * Functions:
22  *      s_vSafeResetTx - Rest Tx
23  *      CARDvSetRSPINF - Set RSPINF
24  *      vUpdateIFS - Update slotTime,SIFS,DIFS, and EIFS
25  *      CARDvUpdateBasicTopRate - Update BasicTopRate
26  *      CARDbAddBasicRate - Add to BasicRateSet
27  *      CARDbIsOFDMinBasicRate - Check if any OFDM rate is in BasicRateSet
28  *      CARDvSetLoopbackMode - Set Loopback mode
29  *      CARDbSoftwareReset - Sortware reset NIC
30  *      CARDqGetTSFOffset - Calculate TSFOffset
31  *      CARDbGetCurrentTSF - Read Current NIC TSF counter
32  *      CARDqGetNextTBTT - Calculate Next Beacon TSF counter
33  *      CARDvSetFirstNextTBTT - Set NIC Beacon time
34  *      CARDvUpdateNextTBTT - Sync. NIC Beacon time
35  *      CARDbRadioPowerOff - Turn Off NIC Radio Power
36  *      CARDbRadioPowerOn - Turn On NIC Radio Power
37  *
38  * Revision History:
39  *      06-10-2003 Bryan YC Fan:  Re-write codes to support VT3253 spec.
40  *      08-26-2003 Kyle Hsu:      Modify the defination type of dwIoBase.
41  *      09-01-2003 Bryan YC Fan:  Add vUpdateIFS().
42  *
43  */
44
45 #include "tmacro.h"
46 #include "card.h"
47 #include "baseband.h"
48 #include "mac.h"
49 #include "desc.h"
50 #include "rf.h"
51 #include "power.h"
52
53 /*---------------------  Static Definitions -------------------------*/
54
55 #define C_SIFS_A        16      /* micro sec. */
56 #define C_SIFS_BG       10
57
58 #define C_EIFS          80      /* micro sec. */
59
60 #define C_SLOT_SHORT    9       /* micro sec. */
61 #define C_SLOT_LONG     20
62
63 #define C_CWMIN_A       15      /* slot time */
64 #define C_CWMIN_B       31
65
66 #define C_CWMAX         1023    /* slot time */
67
68 #define WAIT_BEACON_TX_DOWN_TMO         3    /* Times */
69
70 /*---------------------  Static Variables  --------------------------*/
71
72 static const unsigned short cwRXBCNTSFOff[MAX_RATE] =
73 {17, 17, 17, 17, 34, 23, 17, 11, 8, 5, 4, 3};
74
75 /*---------------------  Static Functions  --------------------------*/
76
77 static
78 void
79 s_vCalculateOFDMRParameter(
80         unsigned char byRate,
81         u8 bb_type,
82         unsigned char *pbyTxRate,
83         unsigned char *pbyRsvTime
84 );
85
86 /*---------------------  Export Functions  --------------------------*/
87
88 /*
89  * Description: Calculate TxRate and RsvTime fields for RSPINF in OFDM mode.
90  *
91  * Parameters:
92  *  In:
93  *      wRate           - Tx Rate
94  *      byPktType       - Tx Packet type
95  *  Out:
96  *      pbyTxRate       - pointer to RSPINF TxRate field
97  *      pbyRsvTime      - pointer to RSPINF RsvTime field
98  *
99  * Return Value: none
100  */
101 static
102 void
103 s_vCalculateOFDMRParameter(
104         unsigned char byRate,
105         u8 bb_type,
106         unsigned char *pbyTxRate,
107         unsigned char *pbyRsvTime
108 )
109 {
110         switch (byRate) {
111         case RATE_6M:
112                 if (bb_type == BB_TYPE_11A) { /* 5GHZ */
113                         *pbyTxRate = 0x9B;
114                         *pbyRsvTime = 44;
115                 } else {
116                         *pbyTxRate = 0x8B;
117                         *pbyRsvTime = 50;
118                 }
119                 break;
120
121         case RATE_9M:
122                 if (bb_type == BB_TYPE_11A) { /* 5GHZ */
123                         *pbyTxRate = 0x9F;
124                         *pbyRsvTime = 36;
125                 } else {
126                         *pbyTxRate = 0x8F;
127                         *pbyRsvTime = 42;
128                 }
129                 break;
130
131         case RATE_12M:
132                 if (bb_type == BB_TYPE_11A) { /* 5GHZ */
133                         *pbyTxRate = 0x9A;
134                         *pbyRsvTime = 32;
135                 } else {
136                         *pbyTxRate = 0x8A;
137                         *pbyRsvTime = 38;
138                 }
139                 break;
140
141         case RATE_18M:
142                 if (bb_type == BB_TYPE_11A) { /* 5GHZ */
143                         *pbyTxRate = 0x9E;
144                         *pbyRsvTime = 28;
145                 } else {
146                         *pbyTxRate = 0x8E;
147                         *pbyRsvTime = 34;
148                 }
149                 break;
150
151         case RATE_36M:
152                 if (bb_type == BB_TYPE_11A) { /* 5GHZ */
153                         *pbyTxRate = 0x9D;
154                         *pbyRsvTime = 24;
155                 } else {
156                         *pbyTxRate = 0x8D;
157                         *pbyRsvTime = 30;
158                 }
159                 break;
160
161         case RATE_48M:
162                 if (bb_type == BB_TYPE_11A) { /* 5GHZ */
163                         *pbyTxRate = 0x98;
164                         *pbyRsvTime = 24;
165                 } else {
166                         *pbyTxRate = 0x88;
167                         *pbyRsvTime = 30;
168                 }
169                 break;
170
171         case RATE_54M:
172                 if (bb_type == BB_TYPE_11A) { /* 5GHZ */
173                         *pbyTxRate = 0x9C;
174                         *pbyRsvTime = 24;
175                 } else {
176                         *pbyTxRate = 0x8C;
177                         *pbyRsvTime = 30;
178                 }
179                 break;
180
181         case RATE_24M:
182         default:
183                 if (bb_type == BB_TYPE_11A) { /* 5GHZ */
184                         *pbyTxRate = 0x99;
185                         *pbyRsvTime = 28;
186                 } else {
187                         *pbyTxRate = 0x89;
188                         *pbyRsvTime = 34;
189                 }
190                 break;
191         }
192 }
193
194 /*---------------------  Export Functions  --------------------------*/
195
196 /*
197  * Description: Update IFS
198  *
199  * Parameters:
200  *  In:
201  *      pDevice             - The adapter to be set
202  *  Out:
203  *      none
204  *
205  * Return Value: None.
206  */
207 bool CARDbSetPhyParameter(struct vnt_private *pDevice, u8 bb_type)
208 {
209         unsigned char byCWMaxMin = 0;
210         unsigned char bySlot = 0;
211         unsigned char bySIFS = 0;
212         unsigned char byDIFS = 0;
213         unsigned char byData;
214         int i;
215
216         /* Set SIFS, DIFS, EIFS, SlotTime, CwMin */
217         if (bb_type == BB_TYPE_11A) {
218                 if (pDevice->byRFType == RF_AIROHA7230) {
219                         /* AL7230 use single PAPE and connect to PAPE_2.4G */
220                         MACvSetBBType(pDevice->PortOffset, BB_TYPE_11G);
221                         pDevice->abyBBVGA[0] = 0x20;
222                         pDevice->abyBBVGA[2] = 0x10;
223                         pDevice->abyBBVGA[3] = 0x10;
224                         BBbReadEmbedded(pDevice, 0xE7, &byData);
225                         if (byData == 0x1C)
226                                 BBbWriteEmbedded(pDevice, 0xE7, pDevice->abyBBVGA[0]);
227
228                 } else if (pDevice->byRFType == RF_UW2452) {
229                         MACvSetBBType(pDevice->PortOffset, BB_TYPE_11A);
230                         pDevice->abyBBVGA[0] = 0x18;
231                         BBbReadEmbedded(pDevice, 0xE7, &byData);
232                         if (byData == 0x14) {
233                                 BBbWriteEmbedded(pDevice, 0xE7, pDevice->abyBBVGA[0]);
234                                 BBbWriteEmbedded(pDevice, 0xE1, 0x57);
235                         }
236                 } else {
237                         MACvSetBBType(pDevice->PortOffset, BB_TYPE_11A);
238                 }
239                 BBbWriteEmbedded(pDevice, 0x88, 0x03);
240                 bySlot = C_SLOT_SHORT;
241                 bySIFS = C_SIFS_A;
242                 byDIFS = C_SIFS_A + 2*C_SLOT_SHORT;
243                 byCWMaxMin = 0xA4;
244         } else if (bb_type == BB_TYPE_11B) {
245                 MACvSetBBType(pDevice->PortOffset, BB_TYPE_11B);
246                 if (pDevice->byRFType == RF_AIROHA7230) {
247                         pDevice->abyBBVGA[0] = 0x1C;
248                         pDevice->abyBBVGA[2] = 0x00;
249                         pDevice->abyBBVGA[3] = 0x00;
250                         BBbReadEmbedded(pDevice, 0xE7, &byData);
251                         if (byData == 0x20)
252                                 BBbWriteEmbedded(pDevice, 0xE7, pDevice->abyBBVGA[0]);
253
254                 } else if (pDevice->byRFType == RF_UW2452) {
255                         pDevice->abyBBVGA[0] = 0x14;
256                         BBbReadEmbedded(pDevice, 0xE7, &byData);
257                         if (byData == 0x18) {
258                                 BBbWriteEmbedded(pDevice, 0xE7, pDevice->abyBBVGA[0]);
259                                 BBbWriteEmbedded(pDevice, 0xE1, 0xD3);
260                         }
261                 }
262                 BBbWriteEmbedded(pDevice, 0x88, 0x02);
263                 bySlot = C_SLOT_LONG;
264                 bySIFS = C_SIFS_BG;
265                 byDIFS = C_SIFS_BG + 2*C_SLOT_LONG;
266                 byCWMaxMin = 0xA5;
267         } else { /* PK_TYPE_11GA & PK_TYPE_11GB */
268                 MACvSetBBType(pDevice->PortOffset, BB_TYPE_11G);
269                 if (pDevice->byRFType == RF_AIROHA7230) {
270                         pDevice->abyBBVGA[0] = 0x1C;
271                         pDevice->abyBBVGA[2] = 0x00;
272                         pDevice->abyBBVGA[3] = 0x00;
273                         BBbReadEmbedded(pDevice, 0xE7, &byData);
274                         if (byData == 0x20)
275                                 BBbWriteEmbedded(pDevice, 0xE7, pDevice->abyBBVGA[0]);
276
277                 } else if (pDevice->byRFType == RF_UW2452) {
278                         pDevice->abyBBVGA[0] = 0x14;
279                         BBbReadEmbedded(pDevice, 0xE7, &byData);
280                         if (byData == 0x18) {
281                                 BBbWriteEmbedded(pDevice, 0xE7, pDevice->abyBBVGA[0]);
282                                 BBbWriteEmbedded(pDevice, 0xE1, 0xD3);
283                         }
284                 }
285                 BBbWriteEmbedded(pDevice, 0x88, 0x08);
286                 bySIFS = C_SIFS_BG;
287
288                 if (pDevice->bShortSlotTime) {
289                         bySlot = C_SLOT_SHORT;
290                         byDIFS = C_SIFS_BG + 2*C_SLOT_SHORT;
291                 } else {
292                         bySlot = C_SLOT_LONG;
293                         byDIFS = C_SIFS_BG + 2*C_SLOT_LONG;
294                 }
295
296                 byCWMaxMin = 0xa4;
297
298                 for (i = RATE_54M; i >= RATE_6M; i--) {
299                         if (pDevice->basic_rates & ((u32)(0x1 << i))) {
300                                 byCWMaxMin |= 0x1;
301                                 break;
302                         }
303                 }
304         }
305
306         if (pDevice->byRFType == RF_RFMD2959) {
307                 /*
308                  * bcs TX_PE will reserve 3 us hardware's processing
309                  * time here is 2 us.
310                  */
311                 bySIFS -= 3;
312                 byDIFS -= 3;
313                 /*
314                  * TX_PE will reserve 3 us for MAX2829 A mode only, it is for
315                  * better TX throughput; MAC will need 2 us to process, so the
316                  * SIFS, DIFS can be shorter by 2 us.
317                  */
318         }
319
320         if (pDevice->bySIFS != bySIFS) {
321                 pDevice->bySIFS = bySIFS;
322                 VNSvOutPortB(pDevice->PortOffset + MAC_REG_SIFS, pDevice->bySIFS);
323         }
324         if (pDevice->byDIFS != byDIFS) {
325                 pDevice->byDIFS = byDIFS;
326                 VNSvOutPortB(pDevice->PortOffset + MAC_REG_DIFS, pDevice->byDIFS);
327         }
328         if (pDevice->byEIFS != C_EIFS) {
329                 pDevice->byEIFS = C_EIFS;
330                 VNSvOutPortB(pDevice->PortOffset + MAC_REG_EIFS, pDevice->byEIFS);
331         }
332         if (pDevice->bySlot != bySlot) {
333                 pDevice->bySlot = bySlot;
334                 VNSvOutPortB(pDevice->PortOffset + MAC_REG_SLOT, pDevice->bySlot);
335
336                 BBvSetShortSlotTime(pDevice);
337         }
338         if (pDevice->byCWMaxMin != byCWMaxMin) {
339                 pDevice->byCWMaxMin = byCWMaxMin;
340                 VNSvOutPortB(pDevice->PortOffset + MAC_REG_CWMAXMIN0, pDevice->byCWMaxMin);
341         }
342
343         pDevice->byPacketType = CARDbyGetPktType(pDevice);
344
345         CARDvSetRSPINF(pDevice, bb_type);
346
347         return true;
348 }
349
350 /*
351  * Description: Sync. TSF counter to BSS
352  *              Get TSF offset and write to HW
353  *
354  * Parameters:
355  *  In:
356  *      pDevice         - The adapter to be sync.
357  *      byRxRate        - data rate of receive beacon
358  *      qwBSSTimestamp  - Rx BCN's TSF
359  *      qwLocalTSF      - Local TSF
360  *  Out:
361  *      none
362  *
363  * Return Value: none
364  */
365 bool CARDbUpdateTSF(struct vnt_private *pDevice, unsigned char byRxRate,
366                     u64 qwBSSTimestamp, u64 qwLocalTSF)
367 {
368         u64 qwTSFOffset = 0;
369
370         if (qwBSSTimestamp != qwLocalTSF) {
371                 qwTSFOffset = CARDqGetTSFOffset(byRxRate, qwBSSTimestamp, qwLocalTSF);
372                 /* adjust TSF, HW's TSF add TSF Offset reg */
373                 VNSvOutPortD(pDevice->PortOffset + MAC_REG_TSFOFST, (u32)qwTSFOffset);
374                 VNSvOutPortD(pDevice->PortOffset + MAC_REG_TSFOFST + 4, (u32)(qwTSFOffset >> 32));
375                 MACvRegBitsOn(pDevice->PortOffset, MAC_REG_TFTCTL, TFTCTL_TSFSYNCEN);
376         }
377         return true;
378 }
379
380 /*
381  * Description: Set NIC TSF counter for first Beacon time
382  *              Get NEXTTBTT from adjusted TSF and Beacon Interval
383  *
384  * Parameters:
385  *  In:
386  *      pDevice         - The adapter to be set.
387  *      wBeaconInterval - Beacon Interval
388  *  Out:
389  *      none
390  *
391  * Return Value: true if succeed; otherwise false
392  */
393 bool CARDbSetBeaconPeriod(struct vnt_private *pDevice,
394                           unsigned short wBeaconInterval)
395 {
396         u64 qwNextTBTT = 0;
397
398         CARDbGetCurrentTSF(pDevice->PortOffset, &qwNextTBTT); /* Get Local TSF counter */
399
400         qwNextTBTT = CARDqGetNextTBTT(qwNextTBTT, wBeaconInterval);
401
402         /* set HW beacon interval */
403         VNSvOutPortW(pDevice->PortOffset + MAC_REG_BI, wBeaconInterval);
404         pDevice->wBeaconInterval = wBeaconInterval;
405         /* Set NextTBTT */
406         VNSvOutPortD(pDevice->PortOffset + MAC_REG_NEXTTBTT, (u32)qwNextTBTT);
407         VNSvOutPortD(pDevice->PortOffset + MAC_REG_NEXTTBTT + 4, (u32)(qwNextTBTT >> 32));
408         MACvRegBitsOn(pDevice->PortOffset, MAC_REG_TFTCTL, TFTCTL_TBTTSYNCEN);
409
410         return true;
411 }
412
413 /*
414  * Description: Turn off Radio power
415  *
416  * Parameters:
417  *  In:
418  *      pDevice         - The adapter to be turned off
419  *  Out:
420  *      none
421  *
422  * Return Value: true if success; otherwise false
423  */
424 bool CARDbRadioPowerOff(struct vnt_private *pDevice)
425 {
426         bool bResult = true;
427
428         if (pDevice->bRadioOff == true)
429                 return true;
430
431         switch (pDevice->byRFType) {
432         case RF_RFMD2959:
433                 MACvWordRegBitsOff(pDevice->PortOffset, MAC_REG_SOFTPWRCTL, SOFTPWRCTL_TXPEINV);
434                 MACvWordRegBitsOn(pDevice->PortOffset, MAC_REG_SOFTPWRCTL, SOFTPWRCTL_SWPE1);
435                 break;
436
437         case RF_AIROHA:
438         case RF_AL2230S:
439         case RF_AIROHA7230:
440                 MACvWordRegBitsOff(pDevice->PortOffset, MAC_REG_SOFTPWRCTL, SOFTPWRCTL_SWPE2);
441                 MACvWordRegBitsOff(pDevice->PortOffset, MAC_REG_SOFTPWRCTL, SOFTPWRCTL_SWPE3);
442                 break;
443
444         }
445
446         MACvRegBitsOff(pDevice->PortOffset, MAC_REG_HOSTCR, HOSTCR_RXON);
447
448         BBvSetDeepSleep(pDevice, pDevice->byLocalID);
449
450         pDevice->bRadioOff = true;
451         pr_debug("chester power off\n");
452         MACvRegBitsOn(pDevice->PortOffset, MAC_REG_GPIOCTL0, LED_ACTSET);  /* LED issue */
453         return bResult;
454 }
455
456 /*
457  * Description: Turn on Radio power
458  *
459  * Parameters:
460  *  In:
461  *      pDevice         - The adapter to be turned on
462  *  Out:
463  *      none
464  *
465  * Return Value: true if success; otherwise false
466  */
467 bool CARDbRadioPowerOn(struct vnt_private *pDevice)
468 {
469         bool bResult = true;
470
471         pr_debug("chester power on\n");
472         if (pDevice->bRadioControlOff == true) {
473                 if (pDevice->bHWRadioOff == true)
474                         pr_debug("chester bHWRadioOff\n");
475                 if (pDevice->bRadioControlOff == true)
476                         pr_debug("chester bRadioControlOff\n");
477                 return false; }
478
479         if (pDevice->bRadioOff == false) {
480                 pr_debug("chester pbRadioOff\n");
481                 return true; }
482
483         BBvExitDeepSleep(pDevice, pDevice->byLocalID);
484
485         MACvRegBitsOn(pDevice->PortOffset, MAC_REG_HOSTCR, HOSTCR_RXON);
486
487         switch (pDevice->byRFType) {
488         case RF_RFMD2959:
489                 MACvWordRegBitsOn(pDevice->PortOffset, MAC_REG_SOFTPWRCTL, SOFTPWRCTL_TXPEINV);
490                 MACvWordRegBitsOff(pDevice->PortOffset, MAC_REG_SOFTPWRCTL, SOFTPWRCTL_SWPE1);
491                 break;
492
493         case RF_AIROHA:
494         case RF_AL2230S:
495         case RF_AIROHA7230:
496                 MACvWordRegBitsOn(pDevice->PortOffset, MAC_REG_SOFTPWRCTL, (SOFTPWRCTL_SWPE2 |
497                                                                             SOFTPWRCTL_SWPE3));
498                 break;
499
500         }
501
502         pDevice->bRadioOff = false;
503         pr_debug("chester power on\n");
504         MACvRegBitsOff(pDevice->PortOffset, MAC_REG_GPIOCTL0, LED_ACTSET); /* LED issue */
505         return bResult;
506 }
507
508 void
509 CARDvSafeResetTx(
510         struct vnt_private *pDevice
511 )
512 {
513         unsigned int uu;
514         PSTxDesc    pCurrTD;
515
516         /* initialize TD index */
517         pDevice->apTailTD[0] = pDevice->apCurrTD[0] = &(pDevice->apTD0Rings[0]);
518         pDevice->apTailTD[1] = pDevice->apCurrTD[1] = &(pDevice->apTD1Rings[0]);
519
520         for (uu = 0; uu < TYPE_MAXTD; uu++)
521                 pDevice->iTDUsed[uu] = 0;
522
523         for (uu = 0; uu < pDevice->sOpts.nTxDescs[0]; uu++) {
524                 pCurrTD = &(pDevice->apTD0Rings[uu]);
525                 pCurrTD->m_td0TD0.f1Owner = OWNED_BY_HOST;
526                 /* init all Tx Packet pointer to NULL */
527         }
528         for (uu = 0; uu < pDevice->sOpts.nTxDescs[1]; uu++) {
529                 pCurrTD = &(pDevice->apTD1Rings[uu]);
530                 pCurrTD->m_td0TD0.f1Owner = OWNED_BY_HOST;
531                 /* init all Tx Packet pointer to NULL */
532         }
533
534         /* set MAC TD pointer */
535         MACvSetCurrTXDescAddr(TYPE_TXDMA0, pDevice->PortOffset,
536                               (pDevice->td0_pool_dma));
537
538         MACvSetCurrTXDescAddr(TYPE_AC0DMA, pDevice->PortOffset,
539                               (pDevice->td1_pool_dma));
540
541         /* set MAC Beacon TX pointer */
542         MACvSetCurrBCNTxDescAddr(pDevice->PortOffset,
543                                  (pDevice->tx_beacon_dma));
544 }
545
546 /*
547  * Description:
548  *      Reset Rx
549  *
550  * Parameters:
551  *  In:
552  *      pDevice     - Pointer to the adapter
553  *  Out:
554  *      none
555  *
556  * Return Value: none
557  */
558 void
559 CARDvSafeResetRx(
560         struct vnt_private *pDevice
561 )
562 {
563         unsigned int uu;
564         PSRxDesc    pDesc;
565
566         /* initialize RD index */
567         pDevice->pCurrRD[0] = &(pDevice->aRD0Ring[0]);
568         pDevice->pCurrRD[1] = &(pDevice->aRD1Ring[0]);
569
570         /* init state, all RD is chip's */
571         for (uu = 0; uu < pDevice->sOpts.nRxDescs0; uu++) {
572                 pDesc = &(pDevice->aRD0Ring[uu]);
573                 pDesc->m_rd0RD0.wResCount = (unsigned short)(pDevice->rx_buf_sz);
574                 pDesc->m_rd0RD0.f1Owner = OWNED_BY_NIC;
575                 pDesc->m_rd1RD1.wReqCount = (unsigned short)(pDevice->rx_buf_sz);
576         }
577
578         /* init state, all RD is chip's */
579         for (uu = 0; uu < pDevice->sOpts.nRxDescs1; uu++) {
580                 pDesc = &(pDevice->aRD1Ring[uu]);
581                 pDesc->m_rd0RD0.wResCount = (unsigned short)(pDevice->rx_buf_sz);
582                 pDesc->m_rd0RD0.f1Owner = OWNED_BY_NIC;
583                 pDesc->m_rd1RD1.wReqCount = (unsigned short)(pDevice->rx_buf_sz);
584         }
585
586         /* set perPkt mode */
587         MACvRx0PerPktMode(pDevice->PortOffset);
588         MACvRx1PerPktMode(pDevice->PortOffset);
589         /* set MAC RD pointer */
590         MACvSetCurrRx0DescAddr(pDevice->PortOffset,
591                                pDevice->rd0_pool_dma);
592
593         MACvSetCurrRx1DescAddr(pDevice->PortOffset,
594                                pDevice->rd1_pool_dma);
595 }
596
597 /*
598  * Description: Get response Control frame rate in CCK mode
599  *
600  * Parameters:
601  *  In:
602  *      pDevice             - The adapter to be set
603  *      wRateIdx            - Receiving data rate
604  *  Out:
605  *      none
606  *
607  * Return Value: response Control frame rate
608  */
609 static unsigned short CARDwGetCCKControlRate(struct vnt_private *pDevice,
610                                              unsigned short wRateIdx)
611 {
612         unsigned int ui = (unsigned int) wRateIdx;
613
614         while (ui > RATE_1M) {
615                 if (pDevice->basic_rates & ((u32)0x1 << ui))
616                         return (unsigned short)ui;
617
618                 ui--;
619         }
620         return (unsigned short)RATE_1M;
621 }
622
623 /*
624  * Description: Get response Control frame rate in OFDM mode
625  *
626  * Parameters:
627  *  In:
628  *      pDevice             - The adapter to be set
629  *      wRateIdx            - Receiving data rate
630  *  Out:
631  *      none
632  *
633  * Return Value: response Control frame rate
634  */
635 static unsigned short CARDwGetOFDMControlRate(struct vnt_private *pDevice,
636                                               unsigned short wRateIdx)
637 {
638         unsigned int ui = (unsigned int) wRateIdx;
639
640         pr_debug("BASIC RATE: %X\n", pDevice->basic_rates);
641
642         if (!CARDbIsOFDMinBasicRate((void *)pDevice)) {
643                 pr_debug("CARDwGetOFDMControlRate:(NO OFDM) %d\n", wRateIdx);
644                 if (wRateIdx > RATE_24M)
645                         wRateIdx = RATE_24M;
646                 return wRateIdx;
647         }
648         while (ui > RATE_11M) {
649                 if (pDevice->basic_rates & ((u32)0x1 << ui)) {
650                         pr_debug("CARDwGetOFDMControlRate : %d\n", ui);
651                         return (unsigned short)ui;
652                 }
653                 ui--;
654         }
655         pr_debug("CARDwGetOFDMControlRate: 6M\n");
656         return (unsigned short)RATE_24M;
657 }
658
659 /*
660  * Description: Set RSPINF
661  *
662  * Parameters:
663  *  In:
664  *      pDevice             - The adapter to be set
665  *  Out:
666  *      none
667  *
668  * Return Value: None.
669  */
670 void CARDvSetRSPINF(struct vnt_private *pDevice, u8 bb_type)
671 {
672         union vnt_phy_field_swap phy;
673         unsigned char byTxRate, byRsvTime;      /* For OFDM */
674
675         /* Set to Page1 */
676         MACvSelectPage1(pDevice->PortOffset);
677
678         /* RSPINF_b_1 */
679         vnt_get_phy_field(pDevice, 14,
680                           CARDwGetCCKControlRate(pDevice, RATE_1M),
681                           PK_TYPE_11B, &phy.field_read);
682
683          /* swap over to get correct write order */
684         swap(phy.swap[0], phy.swap[1]);
685
686         VNSvOutPortD(pDevice->PortOffset + MAC_REG_RSPINF_B_1, phy.field_write);
687
688         /* RSPINF_b_2 */
689         vnt_get_phy_field(pDevice, 14,
690                           CARDwGetCCKControlRate(pDevice, RATE_2M),
691                           PK_TYPE_11B, &phy.field_read);
692
693         swap(phy.swap[0], phy.swap[1]);
694
695         VNSvOutPortD(pDevice->PortOffset + MAC_REG_RSPINF_B_2, phy.field_write);
696
697         /* RSPINF_b_5 */
698         vnt_get_phy_field(pDevice, 14,
699                           CARDwGetCCKControlRate(pDevice, RATE_5M),
700                           PK_TYPE_11B, &phy.field_read);
701
702         swap(phy.swap[0], phy.swap[1]);
703
704         VNSvOutPortD(pDevice->PortOffset + MAC_REG_RSPINF_B_5, phy.field_write);
705
706         /* RSPINF_b_11 */
707         vnt_get_phy_field(pDevice, 14,
708                           CARDwGetCCKControlRate(pDevice, RATE_11M),
709                           PK_TYPE_11B, &phy.field_read);
710
711         swap(phy.swap[0], phy.swap[1]);
712
713         VNSvOutPortD(pDevice->PortOffset + MAC_REG_RSPINF_B_11, phy.field_write);
714
715         /* RSPINF_a_6 */
716         s_vCalculateOFDMRParameter(RATE_6M,
717                                    bb_type,
718                                    &byTxRate,
719                                    &byRsvTime);
720         VNSvOutPortW(pDevice->PortOffset + MAC_REG_RSPINF_A_6, MAKEWORD(byTxRate, byRsvTime));
721         /* RSPINF_a_9 */
722         s_vCalculateOFDMRParameter(RATE_9M,
723                                    bb_type,
724                                    &byTxRate,
725                                    &byRsvTime);
726         VNSvOutPortW(pDevice->PortOffset + MAC_REG_RSPINF_A_9, MAKEWORD(byTxRate, byRsvTime));
727         /* RSPINF_a_12 */
728         s_vCalculateOFDMRParameter(RATE_12M,
729                                    bb_type,
730                                    &byTxRate,
731                                    &byRsvTime);
732         VNSvOutPortW(pDevice->PortOffset + MAC_REG_RSPINF_A_12, MAKEWORD(byTxRate, byRsvTime));
733         /* RSPINF_a_18 */
734         s_vCalculateOFDMRParameter(RATE_18M,
735                                    bb_type,
736                                    &byTxRate,
737                                    &byRsvTime);
738         VNSvOutPortW(pDevice->PortOffset + MAC_REG_RSPINF_A_18, MAKEWORD(byTxRate, byRsvTime));
739         /* RSPINF_a_24 */
740         s_vCalculateOFDMRParameter(RATE_24M,
741                                    bb_type,
742                                    &byTxRate,
743                                    &byRsvTime);
744         VNSvOutPortW(pDevice->PortOffset + MAC_REG_RSPINF_A_24, MAKEWORD(byTxRate, byRsvTime));
745         /* RSPINF_a_36 */
746         s_vCalculateOFDMRParameter(CARDwGetOFDMControlRate((void *)pDevice, RATE_36M),
747                                    bb_type,
748                                    &byTxRate,
749                                    &byRsvTime);
750         VNSvOutPortW(pDevice->PortOffset + MAC_REG_RSPINF_A_36, MAKEWORD(byTxRate, byRsvTime));
751         /* RSPINF_a_48 */
752         s_vCalculateOFDMRParameter(CARDwGetOFDMControlRate((void *)pDevice, RATE_48M),
753                                    bb_type,
754                                    &byTxRate,
755                                    &byRsvTime);
756         VNSvOutPortW(pDevice->PortOffset + MAC_REG_RSPINF_A_48, MAKEWORD(byTxRate, byRsvTime));
757         /* RSPINF_a_54 */
758         s_vCalculateOFDMRParameter(CARDwGetOFDMControlRate((void *)pDevice, RATE_54M),
759                                    bb_type,
760                                    &byTxRate,
761                                    &byRsvTime);
762         VNSvOutPortW(pDevice->PortOffset + MAC_REG_RSPINF_A_54, MAKEWORD(byTxRate, byRsvTime));
763         /* RSPINF_a_72 */
764         s_vCalculateOFDMRParameter(CARDwGetOFDMControlRate((void *)pDevice, RATE_54M),
765                                    bb_type,
766                                    &byTxRate,
767                                    &byRsvTime);
768         VNSvOutPortW(pDevice->PortOffset + MAC_REG_RSPINF_A_72, MAKEWORD(byTxRate, byRsvTime));
769         /* Set to Page0 */
770         MACvSelectPage0(pDevice->PortOffset);
771 }
772
773 /*
774  * Description: Update IFS
775  *
776  * Parameters:
777  *  In:
778  *      pDevice             - The adapter to be set
779  *  Out:
780  *      none
781  *
782  * Return Value: None.
783  */
784 void vUpdateIFS(struct vnt_private *pDevice)
785 {
786         /* Set SIFS, DIFS, EIFS, SlotTime, CwMin */
787
788         unsigned char byMaxMin = 0;
789
790         if (pDevice->byPacketType == PK_TYPE_11A) { /*0000 0000 0000 0000,11a*/
791                 pDevice->uSlot = C_SLOT_SHORT;
792                 pDevice->uSIFS = C_SIFS_A;
793                 pDevice->uDIFS = C_SIFS_A + 2*C_SLOT_SHORT;
794                 pDevice->uCwMin = C_CWMIN_A;
795                 byMaxMin = 4;
796         } else if (pDevice->byPacketType == PK_TYPE_11B) {
797                                         /* 0000 0001 0000 0000,11b */
798                 pDevice->uSlot = C_SLOT_LONG;
799                 pDevice->uSIFS = C_SIFS_BG;
800                 pDevice->uDIFS = C_SIFS_BG + 2*C_SLOT_LONG;
801                 pDevice->uCwMin = C_CWMIN_B;
802                 byMaxMin = 5;
803         } else { /* PK_TYPE_11GA & PK_TYPE_11GB */
804                 pDevice->uSIFS = C_SIFS_BG;
805                 if (pDevice->bShortSlotTime)
806                         pDevice->uSlot = C_SLOT_SHORT;
807                 else
808                         pDevice->uSlot = C_SLOT_LONG;
809
810                 pDevice->uDIFS = C_SIFS_BG + 2*pDevice->uSlot;
811                 if (pDevice->wBasicRate & 0x0150) {
812                         /* 0000 0001 0101 0000,24M,12M,6M */
813                         pDevice->uCwMin = C_CWMIN_A;
814                         byMaxMin = 4;
815                 } else {
816                         pDevice->uCwMin = C_CWMIN_B;
817                         byMaxMin = 5;
818                 }
819         }
820
821         pDevice->uCwMax = C_CWMAX;
822         pDevice->uEIFS = C_EIFS;
823         if (pDevice->byRFType == RF_RFMD2959) {
824                 /* bcs TX_PE will reserve 3 us */
825                 VNSvOutPortB(pDevice->PortOffset + MAC_REG_SIFS, (unsigned char)(pDevice->uSIFS - 3));
826                 VNSvOutPortB(pDevice->PortOffset + MAC_REG_DIFS, (unsigned char)(pDevice->uDIFS - 3));
827         } else {
828                 VNSvOutPortB(pDevice->PortOffset + MAC_REG_SIFS, (unsigned char)pDevice->uSIFS);
829                 VNSvOutPortB(pDevice->PortOffset + MAC_REG_DIFS, (unsigned char)pDevice->uDIFS);
830         }
831         VNSvOutPortB(pDevice->PortOffset + MAC_REG_EIFS, (unsigned char)pDevice->uEIFS);
832         VNSvOutPortB(pDevice->PortOffset + MAC_REG_SLOT, (unsigned char)pDevice->uSlot);
833         byMaxMin |= 0xA0; /* 1010 1111,C_CWMAX = 1023 */
834         VNSvOutPortB(pDevice->PortOffset + MAC_REG_CWMAXMIN0, (unsigned char)byMaxMin);
835 }
836
837 void CARDvUpdateBasicTopRate(struct vnt_private *pDevice)
838 {
839         unsigned char byTopOFDM = RATE_24M, byTopCCK = RATE_1M;
840         unsigned char ii;
841
842         /* Determines the highest basic rate. */
843         for (ii = RATE_54M; ii >= RATE_6M; ii--) {
844                 if ((pDevice->basic_rates) & ((u32)(1 << ii))) {
845                         byTopOFDM = ii;
846                         break;
847                 }
848         }
849         pDevice->byTopOFDMBasicRate = byTopOFDM;
850
851         for (ii = RATE_11M;; ii--) {
852                 if ((pDevice->basic_rates) & ((u32)(1 << ii))) {
853                         byTopCCK = ii;
854                         break;
855                 }
856                 if (ii == RATE_1M)
857                         break;
858         }
859         pDevice->byTopCCKBasicRate = byTopCCK;
860 }
861
862 bool CARDbIsOFDMinBasicRate(struct vnt_private *pDevice)
863 {
864         int ii;
865
866         for (ii = RATE_54M; ii >= RATE_6M; ii--) {
867                 if ((pDevice->basic_rates) & ((u32)(1 << ii)))
868                         return true;
869         }
870         return false;
871 }
872
873 unsigned char CARDbyGetPktType(struct vnt_private *pDevice)
874 {
875
876         if (pDevice->byBBType == BB_TYPE_11A || pDevice->byBBType == BB_TYPE_11B)
877                 return (unsigned char)pDevice->byBBType;
878         else if (CARDbIsOFDMinBasicRate((void *)pDevice))
879                 return PK_TYPE_11GA;
880         else
881                 return PK_TYPE_11GB;
882 }
883
884 /*
885  * Description: Set NIC Loopback mode
886  *
887  * Parameters:
888  *  In:
889  *      pDevice         - The adapter to be set
890  *      wLoopbackMode   - Loopback mode to be set
891  *  Out:
892  *      none
893  *
894  * Return Value: none
895  */
896 void CARDvSetLoopbackMode(void __iomem *dwIoBase, unsigned short wLoopbackMode)
897 {
898         switch (wLoopbackMode) {
899         case CARD_LB_NONE:
900         case CARD_LB_MAC:
901         case CARD_LB_PHY:
902                 break;
903         default:
904                 ASSERT(false);
905                 break;
906         }
907         /* set MAC loopback */
908         MACvSetLoopbackMode(dwIoBase, LOBYTE(wLoopbackMode));
909         /* set Baseband loopback */
910 }
911
912 /*
913  * Description: Software Reset NIC
914  *
915  * Parameters:
916  *  In:
917  *      pDevice         - The adapter to be reset
918  *  Out:
919  *      none
920  *
921  * Return Value: none
922  */
923 bool CARDbSoftwareReset(struct vnt_private *pDevice)
924 {
925
926         /* reset MAC */
927         if (!MACbSafeSoftwareReset(pDevice->PortOffset))
928                 return false;
929
930         return true;
931 }
932
933 /*
934  * Description: Calculate TSF offset of two TSF input
935  *              Get TSF Offset from RxBCN's TSF and local TSF
936  *
937  * Parameters:
938  *  In:
939  *      pDevice         - The adapter to be sync.
940  *      qwTSF1          - Rx BCN's TSF
941  *      qwTSF2          - Local TSF
942  *  Out:
943  *      none
944  *
945  * Return Value: TSF Offset value
946  */
947 u64 CARDqGetTSFOffset(unsigned char byRxRate, u64 qwTSF1, u64 qwTSF2)
948 {
949         u64 qwTSFOffset = 0;
950         unsigned short wRxBcnTSFOffst = 0;
951
952         wRxBcnTSFOffst = cwRXBCNTSFOff[byRxRate%MAX_RATE];
953
954         qwTSF2 += (u64)wRxBcnTSFOffst;
955
956         qwTSFOffset = qwTSF1 - qwTSF2;
957
958         return qwTSFOffset;
959 }
960
961 /*
962  * Description: Read NIC TSF counter
963  *              Get local TSF counter
964  *
965  * Parameters:
966  *  In:
967  *      pDevice         - The adapter to be read
968  *  Out:
969  *      qwCurrTSF       - Current TSF counter
970  *
971  * Return Value: true if success; otherwise false
972  */
973 bool CARDbGetCurrentTSF(void __iomem *dwIoBase, u64 *pqwCurrTSF)
974 {
975         unsigned short ww;
976         unsigned char byData;
977
978         MACvRegBitsOn(dwIoBase, MAC_REG_TFTCTL, TFTCTL_TSFCNTRRD);
979         for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
980                 VNSvInPortB(dwIoBase + MAC_REG_TFTCTL, &byData);
981                 if (!(byData & TFTCTL_TSFCNTRRD))
982                         break;
983         }
984         if (ww == W_MAX_TIMEOUT)
985                 return false;
986         VNSvInPortD(dwIoBase + MAC_REG_TSFCNTR, (u32 *)pqwCurrTSF);
987         VNSvInPortD(dwIoBase + MAC_REG_TSFCNTR + 4, (u32 *)pqwCurrTSF + 1);
988
989         return true;
990 }
991
992 /*
993  * Description: Read NIC TSF counter
994  *              Get NEXTTBTT from adjusted TSF and Beacon Interval
995  *
996  * Parameters:
997  *  In:
998  *      qwTSF           - Current TSF counter
999  *      wbeaconInterval - Beacon Interval
1000  *  Out:
1001  *      qwCurrTSF       - Current TSF counter
1002  *
1003  * Return Value: TSF value of next Beacon
1004  */
1005 u64 CARDqGetNextTBTT(u64 qwTSF, unsigned short wBeaconInterval)
1006 {
1007         u32 beacon_int;
1008
1009         beacon_int = wBeaconInterval * 1024;
1010         if (beacon_int) {
1011                 do_div(qwTSF, beacon_int);
1012                 qwTSF += 1;
1013                 qwTSF *= beacon_int;
1014         }
1015
1016         return qwTSF;
1017 }
1018
1019 /*
1020  * Description: Set NIC TSF counter for first Beacon time
1021  *              Get NEXTTBTT from adjusted TSF and Beacon Interval
1022  *
1023  * Parameters:
1024  *  In:
1025  *      dwIoBase        - IO Base
1026  *      wBeaconInterval - Beacon Interval
1027  *  Out:
1028  *      none
1029  *
1030  * Return Value: none
1031  */
1032 void CARDvSetFirstNextTBTT(void __iomem *dwIoBase, unsigned short wBeaconInterval)
1033 {
1034         u64 qwNextTBTT = 0;
1035
1036         CARDbGetCurrentTSF(dwIoBase, &qwNextTBTT); /* Get Local TSF counter */
1037
1038         qwNextTBTT = CARDqGetNextTBTT(qwNextTBTT, wBeaconInterval);
1039         /* Set NextTBTT */
1040         VNSvOutPortD(dwIoBase + MAC_REG_NEXTTBTT, (u32)qwNextTBTT);
1041         VNSvOutPortD(dwIoBase + MAC_REG_NEXTTBTT + 4, (u32)(qwNextTBTT >> 32));
1042         MACvRegBitsOn(dwIoBase, MAC_REG_TFTCTL, TFTCTL_TBTTSYNCEN);
1043 }
1044
1045 /*
1046  * Description: Sync NIC TSF counter for Beacon time
1047  *              Get NEXTTBTT and write to HW
1048  *
1049  * Parameters:
1050  *  In:
1051  *      pDevice         - The adapter to be set
1052  *      qwTSF           - Current TSF counter
1053  *      wBeaconInterval - Beacon Interval
1054  *  Out:
1055  *      none
1056  *
1057  * Return Value: none
1058  */
1059 void CARDvUpdateNextTBTT(void __iomem *dwIoBase, u64 qwTSF, unsigned short wBeaconInterval)
1060 {
1061         qwTSF = CARDqGetNextTBTT(qwTSF, wBeaconInterval);
1062         /* Set NextTBTT */
1063         VNSvOutPortD(dwIoBase + MAC_REG_NEXTTBTT, (u32)qwTSF);
1064         VNSvOutPortD(dwIoBase + MAC_REG_NEXTTBTT + 4, (u32)(qwTSF >> 32));
1065         MACvRegBitsOn(dwIoBase, MAC_REG_TFTCTL, TFTCTL_TBTTSYNCEN);
1066         pr_debug("Card:Update Next TBTT[%8llx]\n", qwTSF);
1067 }