3 #include "r819xU_phy.h"
4 #include "r819xU_phyreg.h"
5 #include "r8190_rtl8256.h"
7 #include "r819xU_firmware_img.h"
10 #include <linux/bitops.h>
12 static u32 RF_CHANNEL_TABLE_ZEBRA[] = {
31 #define rtl819XPHY_REG_1T2RArray Rtl8192UsbPHY_REG_1T2RArray
32 #define rtl819XMACPHY_Array_PG Rtl8192UsbMACPHY_Array_PG
33 #define rtl819XMACPHY_Array Rtl8192UsbMACPHY_Array
34 #define rtl819XRadioA_Array Rtl8192UsbRadioA_Array
35 #define rtl819XRadioB_Array Rtl8192UsbRadioB_Array
36 #define rtl819XRadioC_Array Rtl8192UsbRadioC_Array
37 #define rtl819XRadioD_Array Rtl8192UsbRadioD_Array
38 #define rtl819XAGCTAB_Array Rtl8192UsbAGCTAB_Array
40 /******************************************************************************
41 * function: This function reads BB parameters from header file we generate,
42 * and does register read/write
43 * input: u32 bitmask //taget bit pos in the addr to be modified
45 * return: u32 return the shift bit position of the mask
46 ******************************************************************************/
47 static u32 rtl8192_CalculateBitShift(u32 bitmask)
55 /******************************************************************************
56 * function: This function checks different RF type to execute legal judgement.
57 * If RF Path is illegal, we will return false.
58 * input: net_device *dev
61 * return: 0(illegal, false), 1(legal, true)
62 *****************************************************************************/
63 u8 rtl8192_phy_CheckIsLegalRFPath(struct net_device *dev, u32 eRFPath)
66 struct r8192_priv *priv = ieee80211_priv(dev);
68 if (priv->rf_type == RF_2T4R) {
70 } else if (priv->rf_type == RF_1T2R) {
71 if (eRFPath == RF90_PATH_A || eRFPath == RF90_PATH_B)
73 else if (eRFPath == RF90_PATH_C || eRFPath == RF90_PATH_D)
79 /******************************************************************************
80 * function: This function sets specific bits to BB register
81 * input: net_device *dev
82 * u32 reg_addr //target addr to be modified
83 * u32 bitmask //taget bit pos to be modified
84 * u32 data //value to be write
88 ******************************************************************************/
89 void rtl8192_setBBreg(struct net_device *dev, u32 reg_addr, u32 bitmask,
95 if (bitmask != bMaskDWord) {
96 read_nic_dword(dev, reg_addr, ®);
97 bitshift = rtl8192_CalculateBitShift(bitmask);
99 reg |= data << bitshift;
100 write_nic_dword(dev, reg_addr, reg);
102 write_nic_dword(dev, reg_addr, data);
107 /******************************************************************************
108 * function: This function reads specific bits from BB register
109 * input: net_device *dev
110 * u32 reg_addr //target addr to be readback
111 * u32 bitmask //taget bit pos to be readback
113 * return: u32 data //the readback register value
115 ******************************************************************************/
116 u32 rtl8192_QueryBBReg(struct net_device *dev, u32 reg_addr, u32 bitmask)
120 read_nic_dword(dev, reg_addr, ®);
121 bitshift = rtl8192_CalculateBitShift(bitmask);
123 return (reg & bitmask) >> bitshift;
126 static u32 phy_FwRFSerialRead(struct net_device *dev, RF90_RADIO_PATH_E eRFPath,
129 static void phy_FwRFSerialWrite(struct net_device *dev,
130 RF90_RADIO_PATH_E eRFPath, u32 offset,
133 /******************************************************************************
134 * function: This function reads register from RF chip
135 * input: net_device *dev
136 * RF90_RADIO_PATH_E eRFPath //radio path of A/B/C/D
137 * u32 offset //target address to be read
139 * return: u32 readback value
140 * notice: There are three types of serial operations:
141 * (1) Software serial write.
142 * (2)Hardware LSSI-Low Speed Serial Interface.
143 * (3)Hardware HSSI-High speed serial write.
144 * Driver here need to implement (1) and (2)
145 * ---need more spec for this information.
146 ******************************************************************************/
147 static u32 rtl8192_phy_RFSerialRead(struct net_device *dev,
148 RF90_RADIO_PATH_E eRFPath, u32 offset)
150 struct r8192_priv *priv = ieee80211_priv(dev);
153 BB_REGISTER_DEFINITION_T *pPhyReg = &priv->PHYRegDef[eRFPath];
155 rtl8192_setBBreg(dev, pPhyReg->rfLSSIReadBack, bLSSIReadBackData, 0);
156 /* Make sure RF register offset is correct */
159 /* Switch page for 8256 RF IC */
160 if (priv->rf_chip == RF_8256) {
162 priv->RfReg0Value[eRFPath] |= 0x140;
163 /* Switch to Reg_Mode2 for Reg 31-45 */
164 rtl8192_setBBreg(dev, pPhyReg->rf3wireOffset,
166 priv->RfReg0Value[eRFPath]<<16);
168 new_offset = offset - 30;
169 } else if (offset >= 16) {
170 priv->RfReg0Value[eRFPath] |= 0x100;
171 priv->RfReg0Value[eRFPath] &= (~0x40);
172 /* Switch to Reg_Mode1 for Reg16-30 */
173 rtl8192_setBBreg(dev, pPhyReg->rf3wireOffset,
175 priv->RfReg0Value[eRFPath]<<16);
177 new_offset = offset - 15;
182 RT_TRACE((COMP_PHY|COMP_ERR),
183 "check RF type here, need to be 8256\n");
186 /* Put desired read addr to LSSI control Register */
187 rtl8192_setBBreg(dev, pPhyReg->rfHSSIPara2, bLSSIReadAddress,
189 /* Issue a posedge trigger */
190 rtl8192_setBBreg(dev, pPhyReg->rfHSSIPara2, bLSSIReadEdge, 0x0);
191 rtl8192_setBBreg(dev, pPhyReg->rfHSSIPara2, bLSSIReadEdge, 0x1);
194 /* TODO: we should not delay such a long time. Ask for help from SD3 */
195 usleep_range(1000, 1000);
197 ret = rtl8192_QueryBBReg(dev, pPhyReg->rfLSSIReadBack,
201 /* Switch back to Reg_Mode0 */
202 if (priv->rf_chip == RF_8256) {
203 priv->RfReg0Value[eRFPath] &= 0xebf;
205 rtl8192_setBBreg(dev, pPhyReg->rf3wireOffset, bMaskDWord,
206 priv->RfReg0Value[eRFPath] << 16);
212 /******************************************************************************
213 * function: This function writes data to RF register
214 * input: net_device *dev
215 * RF90_RADIO_PATH_E eRFPath //radio path of A/B/C/D
216 * u32 offset //target address to be written
217 * u32 data //the new register data to be written
220 * notice: For RF8256 only.
221 * ===========================================================================
222 * Reg Mode RegCTL[1] RegCTL[0] Note
223 * (Reg00[12]) (Reg00[10])
224 * ===========================================================================
225 * Reg_Mode0 0 x Reg 0 ~ 15(0x0 ~ 0xf)
226 * ---------------------------------------------------------------------------
227 * Reg_Mode1 1 0 Reg 16 ~ 30(0x1 ~ 0xf)
228 * ---------------------------------------------------------------------------
229 * Reg_Mode2 1 1 Reg 31 ~ 45(0x1 ~ 0xf)
230 * ---------------------------------------------------------------------------
231 *****************************************************************************/
232 static void rtl8192_phy_RFSerialWrite(struct net_device *dev,
233 RF90_RADIO_PATH_E eRFPath, u32 offset,
236 struct r8192_priv *priv = ieee80211_priv(dev);
237 u32 DataAndAddr = 0, new_offset = 0;
238 BB_REGISTER_DEFINITION_T *pPhyReg = &priv->PHYRegDef[eRFPath];
241 if (priv->rf_chip == RF_8256) {
244 priv->RfReg0Value[eRFPath] |= 0x140;
245 rtl8192_setBBreg(dev, pPhyReg->rf3wireOffset,
247 priv->RfReg0Value[eRFPath] << 16);
248 new_offset = offset - 30;
249 } else if (offset >= 16) {
250 priv->RfReg0Value[eRFPath] |= 0x100;
251 priv->RfReg0Value[eRFPath] &= (~0x40);
252 rtl8192_setBBreg(dev, pPhyReg->rf3wireOffset,
254 priv->RfReg0Value[eRFPath]<<16);
255 new_offset = offset - 15;
260 RT_TRACE((COMP_PHY|COMP_ERR),
261 "check RF type here, need to be 8256\n");
265 /* Put write addr in [5:0] and write data in [31:16] */
266 DataAndAddr = (data<<16) | (new_offset&0x3f);
268 /* Write operation */
269 rtl8192_setBBreg(dev, pPhyReg->rf3wireOffset, bMaskDWord, DataAndAddr);
273 priv->RfReg0Value[eRFPath] = data;
275 /* Switch back to Reg_Mode0 */
276 if (priv->rf_chip == RF_8256) {
278 priv->RfReg0Value[eRFPath] &= 0xebf;
279 rtl8192_setBBreg(dev, pPhyReg->rf3wireOffset,
281 priv->RfReg0Value[eRFPath] << 16);
287 /******************************************************************************
288 * function: This function set specific bits to RF register
289 * input: net_device dev
290 * RF90_RADIO_PATH_E eRFPath //radio path of A/B/C/D
291 * u32 reg_addr //target addr to be modified
292 * u32 bitmask //taget bit pos to be modified
293 * u32 data //value to be written
297 *****************************************************************************/
298 void rtl8192_phy_SetRFReg(struct net_device *dev, RF90_RADIO_PATH_E eRFPath,
299 u32 reg_addr, u32 bitmask, u32 data)
301 struct r8192_priv *priv = ieee80211_priv(dev);
304 if (!rtl8192_phy_CheckIsLegalRFPath(dev, eRFPath))
307 if (priv->Rf_Mode == RF_OP_By_FW) {
308 if (bitmask != bMask12Bits) {
309 /* RF data is 12 bits only */
310 reg = phy_FwRFSerialRead(dev, eRFPath, reg_addr);
311 bitshift = rtl8192_CalculateBitShift(bitmask);
313 reg |= data << bitshift;
315 phy_FwRFSerialWrite(dev, eRFPath, reg_addr, reg);
317 phy_FwRFSerialWrite(dev, eRFPath, reg_addr, data);
323 if (bitmask != bMask12Bits) {
324 /* RF data is 12 bits only */
325 reg = rtl8192_phy_RFSerialRead(dev, eRFPath, reg_addr);
326 bitshift = rtl8192_CalculateBitShift(bitmask);
328 reg |= data << bitshift;
330 rtl8192_phy_RFSerialWrite(dev, eRFPath, reg_addr, reg);
332 rtl8192_phy_RFSerialWrite(dev, eRFPath, reg_addr, data);
338 /******************************************************************************
339 * function: This function reads specific bits from RF register
340 * input: net_device *dev
341 * u32 reg_addr //target addr to be readback
342 * u32 bitmask //taget bit pos to be readback
344 * return: u32 data //the readback register value
346 *****************************************************************************/
347 u32 rtl8192_phy_QueryRFReg(struct net_device *dev, RF90_RADIO_PATH_E eRFPath,
348 u32 reg_addr, u32 bitmask)
351 struct r8192_priv *priv = ieee80211_priv(dev);
354 if (!rtl8192_phy_CheckIsLegalRFPath(dev, eRFPath))
356 if (priv->Rf_Mode == RF_OP_By_FW) {
357 reg = phy_FwRFSerialRead(dev, eRFPath, reg_addr);
358 bitshift = rtl8192_CalculateBitShift(bitmask);
359 reg = (reg & bitmask) >> bitshift;
363 reg = rtl8192_phy_RFSerialRead(dev, eRFPath, reg_addr);
364 bitshift = rtl8192_CalculateBitShift(bitmask);
365 reg = (reg & bitmask) >> bitshift;
370 /******************************************************************************
371 * function: We support firmware to execute RF-R/W.
372 * input: net_device *dev
373 * RF90_RADIO_PATH_E eRFPath
378 ****************************************************************************/
379 static u32 phy_FwRFSerialRead(struct net_device *dev, RF90_RADIO_PATH_E eRFPath,
387 /* Firmware RF Write control.
388 * We can not execute the scheme in the initial step.
389 * Otherwise, RF-R/W will waste much time.
390 * This is only for site survey. */
391 /* 1. Read operation need not insert data. bit 0-11 */
392 /* 2. Write RF register address. bit 12-19 */
393 data |= ((offset&0xFF)<<12);
394 /* 3. Write RF path. bit 20-21 */
395 data |= ((eRFPath&0x3)<<20);
396 /* 4. Set RF read indicator. bit 22=0 */
397 /* 5. Trigger Fw to operate the command. bit 31 */
399 /* 6. We can not execute read operation if bit 31 is 1. */
400 read_nic_dword(dev, QPNR, &tmp);
401 while (tmp & 0x80000000) {
402 /* If FW can not finish RF-R/W for more than ?? times.
406 read_nic_dword(dev, QPNR, &tmp);
411 /* 7. Execute read operation. */
412 write_nic_dword(dev, QPNR, data);
413 /* 8. Check if firmware send back RF content. */
414 read_nic_dword(dev, QPNR, &tmp);
415 while (tmp & 0x80000000) {
416 /* If FW can not finish RF-R/W for more than ?? times.
420 read_nic_dword(dev, QPNR, &tmp);
425 read_nic_dword(dev, RF_DATA, ®);
430 /******************************************************************************
431 * function: We support firmware to execute RF-R/W.
432 * input: net_device *dev
433 * RF90_RADIO_PATH_E eRFPath
439 ****************************************************************************/
440 static void phy_FwRFSerialWrite(struct net_device *dev,
441 RF90_RADIO_PATH_E eRFPath, u32 offset, u32 data)
446 /* Firmware RF Write control.
447 * We can not execute the scheme in the initial step.
448 * Otherwise, RF-R/W will waste much time.
449 * This is only for site survey. */
451 /* 1. Set driver write bit and 12 bit data. bit 0-11 */
452 /* 2. Write RF register address. bit 12-19 */
453 data |= ((offset&0xFF)<<12);
454 /* 3. Write RF path. bit 20-21 */
455 data |= ((eRFPath&0x3)<<20);
456 /* 4. Set RF write indicator. bit 22=1 */
458 /* 5. Trigger Fw to operate the command. bit 31=1 */
461 /* 6. Write operation. We can not write if bit 31 is 1. */
462 read_nic_dword(dev, QPNR, &tmp);
463 while (tmp & 0x80000000) {
464 /* If FW can not finish RF-R/W for more than ?? times.
468 read_nic_dword(dev, QPNR, &tmp);
473 /* 7. No matter check bit. We always force the write.
474 Because FW will not accept the command. */
475 write_nic_dword(dev, QPNR, data);
476 /* According to test, we must delay 20us to wait firmware
477 to finish RF write operation. */
478 /* We support delay in firmware side now. */
481 /******************************************************************************
482 * function: This function reads BB parameters from header file we generate,
483 * and do register read/write
484 * input: net_device *dev
487 * notice: BB parameters may change all the time, so please make
488 * sure it has been synced with the newest.
489 *****************************************************************************/
490 void rtl8192_phy_configmac(struct net_device *dev)
492 u32 dwArrayLen = 0, i;
493 u32 *pdwArray = NULL;
494 struct r8192_priv *priv = ieee80211_priv(dev);
496 if (priv->btxpowerdata_readfromEEPORM) {
497 RT_TRACE(COMP_PHY, "Rtl819XMACPHY_Array_PG\n");
498 dwArrayLen = MACPHY_Array_PGLength;
499 pdwArray = rtl819XMACPHY_Array_PG;
502 RT_TRACE(COMP_PHY, "Rtl819XMACPHY_Array\n");
503 dwArrayLen = MACPHY_ArrayLength;
504 pdwArray = rtl819XMACPHY_Array;
506 for (i = 0; i < dwArrayLen; i = i+3) {
507 if (pdwArray[i] == 0x318) {
508 pdwArray[i+2] = 0x00000800;
512 "Rtl8190MACPHY_Array[0]=%x Rtl8190MACPHY_Array[1]=%x Rtl8190MACPHY_Array[2]=%x\n",
513 pdwArray[i], pdwArray[i+1], pdwArray[i+2]);
514 rtl8192_setBBreg(dev, pdwArray[i], pdwArray[i+1],
520 /******************************************************************************
521 * function: This function does dirty work
522 * input: net_device *dev
526 * notice: BB parameters may change all the time, so please make
527 * sure it has been synced with the newest.
528 *****************************************************************************/
529 void rtl8192_phyConfigBB(struct net_device *dev, u8 ConfigType)
534 u32 *rtl8192PhyRegArrayTable = NULL, *rtl8192AgcTabArrayTable = NULL;
536 if (Adapter->bInHctTest) {
537 PHY_REGArrayLen = PHY_REGArrayLengthDTM;
538 AGCTAB_ArrayLen = AGCTAB_ArrayLengthDTM;
539 Rtl8190PHY_REGArray_Table = Rtl819XPHY_REGArrayDTM;
540 Rtl8190AGCTAB_Array_Table = Rtl819XAGCTAB_ArrayDTM;
543 if (ConfigType == BaseBand_Config_PHY_REG) {
544 for (i = 0; i < PHY_REG_1T2RArrayLength; i += 2) {
545 rtl8192_setBBreg(dev, rtl819XPHY_REG_1T2RArray[i],
547 rtl819XPHY_REG_1T2RArray[i+1]);
549 "i: %x, Rtl819xUsbPHY_REGArray[0]=%x Rtl819xUsbPHY_REGArray[1]=%x\n",
550 i, rtl819XPHY_REG_1T2RArray[i],
551 rtl819XPHY_REG_1T2RArray[i+1]);
553 } else if (ConfigType == BaseBand_Config_AGC_TAB) {
554 for (i = 0; i < AGCTAB_ArrayLength; i += 2) {
555 rtl8192_setBBreg(dev, rtl819XAGCTAB_Array[i],
556 bMaskDWord, rtl819XAGCTAB_Array[i+1]);
558 "i: %x, rtl819XAGCTAB_Array[0]=%x rtl819XAGCTAB_Array[1]=%x\n",
559 i, rtl819XAGCTAB_Array[i],
560 rtl819XAGCTAB_Array[i+1]);
566 /******************************************************************************
567 * function: This function initializes Register definition offset for
569 * input: net_device *dev
572 * notice: Initialization value here is constant and it should never
574 *****************************************************************************/
575 static void rtl8192_InitBBRFRegDef(struct net_device *dev)
577 struct r8192_priv *priv = ieee80211_priv(dev);
579 /* RF Interface Software Control */
580 /* 16 LSBs if read 32-bit from 0x870 */
581 priv->PHYRegDef[RF90_PATH_A].rfintfs = rFPGA0_XAB_RFInterfaceSW;
582 /* 16 MSBs if read 32-bit from 0x870 (16-bit for 0x872) */
583 priv->PHYRegDef[RF90_PATH_B].rfintfs = rFPGA0_XAB_RFInterfaceSW;
584 /* 16 LSBs if read 32-bit from 0x874 */
585 priv->PHYRegDef[RF90_PATH_C].rfintfs = rFPGA0_XCD_RFInterfaceSW;
586 /* 16 MSBs if read 32-bit from 0x874 (16-bit for 0x876) */
587 priv->PHYRegDef[RF90_PATH_D].rfintfs = rFPGA0_XCD_RFInterfaceSW;
589 /* RF Interface Readback Value */
590 /* 16 LSBs if read 32-bit from 0x8E0 */
591 priv->PHYRegDef[RF90_PATH_A].rfintfi = rFPGA0_XAB_RFInterfaceRB;
592 /* 16 MSBs if read 32-bit from 0x8E0 (16-bit for 0x8E2) */
593 priv->PHYRegDef[RF90_PATH_B].rfintfi = rFPGA0_XAB_RFInterfaceRB;
594 /* 16 LSBs if read 32-bit from 0x8E4 */
595 priv->PHYRegDef[RF90_PATH_C].rfintfi = rFPGA0_XCD_RFInterfaceRB;
596 /* 16 MSBs if read 32-bit from 0x8E4 (16-bit for 0x8E6) */
597 priv->PHYRegDef[RF90_PATH_D].rfintfi = rFPGA0_XCD_RFInterfaceRB;
599 /* RF Interface Output (and Enable) */
600 /* 16 LSBs if read 32-bit from 0x860 */
601 priv->PHYRegDef[RF90_PATH_A].rfintfo = rFPGA0_XA_RFInterfaceOE;
602 /* 16 LSBs if read 32-bit from 0x864 */
603 priv->PHYRegDef[RF90_PATH_B].rfintfo = rFPGA0_XB_RFInterfaceOE;
604 /* 16 LSBs if read 32-bit from 0x868 */
605 priv->PHYRegDef[RF90_PATH_C].rfintfo = rFPGA0_XC_RFInterfaceOE;
606 /* 16 LSBs if read 32-bit from 0x86C */
607 priv->PHYRegDef[RF90_PATH_D].rfintfo = rFPGA0_XD_RFInterfaceOE;
609 /* RF Interface (Output and) Enable */
610 /* 16 MSBs if read 32-bit from 0x860 (16-bit for 0x862) */
611 priv->PHYRegDef[RF90_PATH_A].rfintfe = rFPGA0_XA_RFInterfaceOE;
612 /* 16 MSBs if read 32-bit from 0x864 (16-bit for 0x866) */
613 priv->PHYRegDef[RF90_PATH_B].rfintfe = rFPGA0_XB_RFInterfaceOE;
614 /* 16 MSBs if read 32-bit from 0x86A (16-bit for 0x86A) */
615 priv->PHYRegDef[RF90_PATH_C].rfintfe = rFPGA0_XC_RFInterfaceOE;
616 /* 16 MSBs if read 32-bit from 0x86C (16-bit for 0x86E) */
617 priv->PHYRegDef[RF90_PATH_D].rfintfe = rFPGA0_XD_RFInterfaceOE;
619 /* Addr of LSSI. Write RF register by driver */
620 priv->PHYRegDef[RF90_PATH_A].rf3wireOffset = rFPGA0_XA_LSSIParameter;
621 priv->PHYRegDef[RF90_PATH_B].rf3wireOffset = rFPGA0_XB_LSSIParameter;
622 priv->PHYRegDef[RF90_PATH_C].rf3wireOffset = rFPGA0_XC_LSSIParameter;
623 priv->PHYRegDef[RF90_PATH_D].rf3wireOffset = rFPGA0_XD_LSSIParameter;
627 priv->PHYRegDef[RF90_PATH_A].rfLSSI_Select = rFPGA0_XAB_RFParameter;
628 priv->PHYRegDef[RF90_PATH_B].rfLSSI_Select = rFPGA0_XAB_RFParameter;
629 priv->PHYRegDef[RF90_PATH_C].rfLSSI_Select = rFPGA0_XCD_RFParameter;
630 priv->PHYRegDef[RF90_PATH_D].rfLSSI_Select = rFPGA0_XCD_RFParameter;
632 /* Tx AGC Gain Stage (same for all path. Should we remove this?) */
633 priv->PHYRegDef[RF90_PATH_A].rfTxGainStage = rFPGA0_TxGainStage;
634 priv->PHYRegDef[RF90_PATH_B].rfTxGainStage = rFPGA0_TxGainStage;
635 priv->PHYRegDef[RF90_PATH_C].rfTxGainStage = rFPGA0_TxGainStage;
636 priv->PHYRegDef[RF90_PATH_D].rfTxGainStage = rFPGA0_TxGainStage;
638 /* Tranceiver A~D HSSI Parameter-1 */
639 /* wire control parameter1 */
640 priv->PHYRegDef[RF90_PATH_A].rfHSSIPara1 = rFPGA0_XA_HSSIParameter1;
641 priv->PHYRegDef[RF90_PATH_B].rfHSSIPara1 = rFPGA0_XB_HSSIParameter1;
642 priv->PHYRegDef[RF90_PATH_C].rfHSSIPara1 = rFPGA0_XC_HSSIParameter1;
643 priv->PHYRegDef[RF90_PATH_D].rfHSSIPara1 = rFPGA0_XD_HSSIParameter1;
645 /* Tranceiver A~D HSSI Parameter-2 */
646 /* wire control parameter2 */
647 priv->PHYRegDef[RF90_PATH_A].rfHSSIPara2 = rFPGA0_XA_HSSIParameter2;
648 priv->PHYRegDef[RF90_PATH_B].rfHSSIPara2 = rFPGA0_XB_HSSIParameter2;
649 priv->PHYRegDef[RF90_PATH_C].rfHSSIPara2 = rFPGA0_XC_HSSIParameter2;
650 priv->PHYRegDef[RF90_PATH_D].rfHSSIPara2 = rFPGA0_XD_HSSIParameter2;
652 /* RF Switch Control */
653 /* TR/Ant switch control */
654 priv->PHYRegDef[RF90_PATH_A].rfSwitchControl = rFPGA0_XAB_SwitchControl;
655 priv->PHYRegDef[RF90_PATH_B].rfSwitchControl = rFPGA0_XAB_SwitchControl;
656 priv->PHYRegDef[RF90_PATH_C].rfSwitchControl = rFPGA0_XCD_SwitchControl;
657 priv->PHYRegDef[RF90_PATH_D].rfSwitchControl = rFPGA0_XCD_SwitchControl;
660 priv->PHYRegDef[RF90_PATH_A].rfAGCControl1 = rOFDM0_XAAGCCore1;
661 priv->PHYRegDef[RF90_PATH_B].rfAGCControl1 = rOFDM0_XBAGCCore1;
662 priv->PHYRegDef[RF90_PATH_C].rfAGCControl1 = rOFDM0_XCAGCCore1;
663 priv->PHYRegDef[RF90_PATH_D].rfAGCControl1 = rOFDM0_XDAGCCore1;
666 priv->PHYRegDef[RF90_PATH_A].rfAGCControl2 = rOFDM0_XAAGCCore2;
667 priv->PHYRegDef[RF90_PATH_B].rfAGCControl2 = rOFDM0_XBAGCCore2;
668 priv->PHYRegDef[RF90_PATH_C].rfAGCControl2 = rOFDM0_XCAGCCore2;
669 priv->PHYRegDef[RF90_PATH_D].rfAGCControl2 = rOFDM0_XDAGCCore2;
671 /* RX AFE control 1 */
672 priv->PHYRegDef[RF90_PATH_A].rfRxIQImbalance = rOFDM0_XARxIQImbalance;
673 priv->PHYRegDef[RF90_PATH_B].rfRxIQImbalance = rOFDM0_XBRxIQImbalance;
674 priv->PHYRegDef[RF90_PATH_C].rfRxIQImbalance = rOFDM0_XCRxIQImbalance;
675 priv->PHYRegDef[RF90_PATH_D].rfRxIQImbalance = rOFDM0_XDRxIQImbalance;
677 /* RX AFE control 1 */
678 priv->PHYRegDef[RF90_PATH_A].rfRxAFE = rOFDM0_XARxAFE;
679 priv->PHYRegDef[RF90_PATH_B].rfRxAFE = rOFDM0_XBRxAFE;
680 priv->PHYRegDef[RF90_PATH_C].rfRxAFE = rOFDM0_XCRxAFE;
681 priv->PHYRegDef[RF90_PATH_D].rfRxAFE = rOFDM0_XDRxAFE;
683 /* Tx AFE control 1 */
684 priv->PHYRegDef[RF90_PATH_A].rfTxIQImbalance = rOFDM0_XATxIQImbalance;
685 priv->PHYRegDef[RF90_PATH_B].rfTxIQImbalance = rOFDM0_XBTxIQImbalance;
686 priv->PHYRegDef[RF90_PATH_C].rfTxIQImbalance = rOFDM0_XCTxIQImbalance;
687 priv->PHYRegDef[RF90_PATH_D].rfTxIQImbalance = rOFDM0_XDTxIQImbalance;
689 /* Tx AFE control 2 */
690 priv->PHYRegDef[RF90_PATH_A].rfTxAFE = rOFDM0_XATxAFE;
691 priv->PHYRegDef[RF90_PATH_B].rfTxAFE = rOFDM0_XBTxAFE;
692 priv->PHYRegDef[RF90_PATH_C].rfTxAFE = rOFDM0_XCTxAFE;
693 priv->PHYRegDef[RF90_PATH_D].rfTxAFE = rOFDM0_XDTxAFE;
695 /* Tranceiver LSSI Readback */
696 priv->PHYRegDef[RF90_PATH_A].rfLSSIReadBack = rFPGA0_XA_LSSIReadBack;
697 priv->PHYRegDef[RF90_PATH_B].rfLSSIReadBack = rFPGA0_XB_LSSIReadBack;
698 priv->PHYRegDef[RF90_PATH_C].rfLSSIReadBack = rFPGA0_XC_LSSIReadBack;
699 priv->PHYRegDef[RF90_PATH_D].rfLSSIReadBack = rFPGA0_XD_LSSIReadBack;
702 /******************************************************************************
703 * function: This function is to write register and then readback to make
704 * sure whether BB and RF is OK
705 * input: net_device *dev
706 * HW90_BLOCK_E CheckBlock
707 * RF90_RADIO_PATH_E eRFPath //only used when checkblock is
710 * return: return whether BB and RF is ok (0:OK, 1:Fail)
711 * notice: This function may be removed in the ASIC
712 ******************************************************************************/
713 u8 rtl8192_phy_checkBBAndRF(struct net_device *dev, HW90_BLOCK_E CheckBlock,
714 RF90_RADIO_PATH_E eRFPath)
717 u32 i, CheckTimes = 4, reg = 0;
719 u32 WriteData[] = {0xfffff027, 0xaa55a02f, 0x00000027, 0x55aa502f};
721 /* Initialize register address offset to be checked */
722 WriteAddr[HW90_BLOCK_MAC] = 0x100;
723 WriteAddr[HW90_BLOCK_PHY0] = 0x900;
724 WriteAddr[HW90_BLOCK_PHY1] = 0x800;
725 WriteAddr[HW90_BLOCK_RF] = 0x3;
726 RT_TRACE(COMP_PHY, "%s(), CheckBlock: %d\n", __func__, CheckBlock);
727 for (i = 0; i < CheckTimes; i++) {
729 /* Write data to register and readback */
730 switch (CheckBlock) {
733 "PHY_CheckBBRFOK(): Never Write 0x100 here!\n");
736 case HW90_BLOCK_PHY0:
737 case HW90_BLOCK_PHY1:
738 write_nic_dword(dev, WriteAddr[CheckBlock],
740 read_nic_dword(dev, WriteAddr[CheckBlock], ®);
744 WriteData[i] &= 0xfff;
745 rtl8192_phy_SetRFReg(dev, eRFPath,
746 WriteAddr[HW90_BLOCK_RF],
747 bMask12Bits, WriteData[i]);
748 /* TODO: we should not delay for such a long time.
750 usleep_range(1000, 1000);
751 reg = rtl8192_phy_QueryRFReg(dev, eRFPath,
752 WriteAddr[HW90_BLOCK_RF],
754 usleep_range(1000, 1000);
763 /* Check whether readback data is correct */
764 if (reg != WriteData[i]) {
765 RT_TRACE((COMP_PHY|COMP_ERR),
766 "error reg: %x, WriteData: %x\n",
776 /******************************************************************************
777 * function: This function initializes BB&RF
778 * input: net_device *dev
781 * notice: Initialization value may change all the time, so please make
782 * sure it has been synced with the newest.
783 ******************************************************************************/
784 static void rtl8192_BB_Config_ParaFile(struct net_device *dev)
786 struct r8192_priv *priv = ieee80211_priv(dev);
787 u8 reg_u8 = 0, eCheckItem = 0, status = 0;
790 /**************************************
791 * <1> Initialize BaseBand
792 *************************************/
794 /* --set BB Global Reset-- */
795 read_nic_byte(dev, BB_GLOBAL_RESET, ®_u8);
796 write_nic_byte(dev, BB_GLOBAL_RESET, (reg_u8|BB_GLOBAL_RESET_BIT));
798 /* ---set BB reset Active--- */
799 read_nic_dword(dev, CPU_GEN, ®_u32);
800 write_nic_dword(dev, CPU_GEN, (reg_u32&(~CPU_GEN_BB_RST)));
802 /* ----Ckeck FPGAPHY0 and PHY1 board is OK---- */
803 /* TODO: this function should be removed on ASIC */
804 for (eCheckItem = (HW90_BLOCK_E)HW90_BLOCK_PHY0;
805 eCheckItem <= HW90_BLOCK_PHY1; eCheckItem++) {
806 /* don't care RF path */
807 status = rtl8192_phy_checkBBAndRF(dev, (HW90_BLOCK_E)eCheckItem,
808 (RF90_RADIO_PATH_E)0);
810 RT_TRACE((COMP_ERR | COMP_PHY),
811 "PHY_RF8256_Config(): Check PHY%d Fail!!\n",
816 /* ---- Set CCK and OFDM Block "OFF"---- */
817 rtl8192_setBBreg(dev, rFPGA0_RFMOD, bCCKEn|bOFDMEn, 0x0);
818 /* ----BB Register Initilazation---- */
819 /* ==m==>Set PHY REG From Header<==m== */
820 rtl8192_phyConfigBB(dev, BaseBand_Config_PHY_REG);
822 /* ----Set BB reset de-Active---- */
823 read_nic_dword(dev, CPU_GEN, ®_u32);
824 write_nic_dword(dev, CPU_GEN, (reg_u32|CPU_GEN_BB_RST));
826 /* ----BB AGC table Initialization---- */
827 /* ==m==>Set PHY REG From Header<==m== */
828 rtl8192_phyConfigBB(dev, BaseBand_Config_AGC_TAB);
830 /* ----Enable XSTAL ---- */
831 write_nic_byte_E(dev, 0x5e, 0x00);
832 if (priv->card_8192_version == (u8)VERSION_819xU_A) {
833 /* Antenna gain offset from B/C/D to A */
834 reg_u32 = (priv->AntennaTxPwDiff[1]<<4 |
835 priv->AntennaTxPwDiff[0]);
836 rtl8192_setBBreg(dev, rFPGA0_TxGainStage, (bXBTxAGC|bXCTxAGC),
840 reg_u32 = priv->CrystalCap & 0xf;
841 rtl8192_setBBreg(dev, rFPGA0_AnalogParameter1, bXtalCap,
845 /* Check if the CCK HighPower is turned ON.
846 This is used to calculate PWDB. */
847 priv->bCckHighPower = (u8)rtl8192_QueryBBReg(dev,
848 rFPGA0_XA_HSSIParameter2,
853 /******************************************************************************
854 * function: This function initializes BB&RF
855 * input: net_device *dev
858 * notice: Initialization value may change all the time, so please make
859 * sure it has been synced with the newest.
860 *****************************************************************************/
861 void rtl8192_BBConfig(struct net_device *dev)
863 rtl8192_InitBBRFRegDef(dev);
864 /* config BB&RF. As hardCode based initialization has not been well
865 * implemented, so use file first.
866 * FIXME: should implement it for hardcode? */
867 rtl8192_BB_Config_ParaFile(dev);
872 /******************************************************************************
873 * function: This function obtains the initialization value of Tx power Level
875 * input: net_device *dev
878 *****************************************************************************/
879 void rtl8192_phy_getTxPower(struct net_device *dev)
881 struct r8192_priv *priv = ieee80211_priv(dev);
884 read_nic_dword(dev, rTxAGC_Rate18_06,
885 &priv->MCSTxPowerLevelOriginalOffset[0]);
886 read_nic_dword(dev, rTxAGC_Rate54_24,
887 &priv->MCSTxPowerLevelOriginalOffset[1]);
888 read_nic_dword(dev, rTxAGC_Mcs03_Mcs00,
889 &priv->MCSTxPowerLevelOriginalOffset[2]);
890 read_nic_dword(dev, rTxAGC_Mcs07_Mcs04,
891 &priv->MCSTxPowerLevelOriginalOffset[3]);
892 read_nic_dword(dev, rTxAGC_Mcs11_Mcs08,
893 &priv->MCSTxPowerLevelOriginalOffset[4]);
894 read_nic_dword(dev, rTxAGC_Mcs15_Mcs12,
895 &priv->MCSTxPowerLevelOriginalOffset[5]);
897 /* Read rx initial gain */
898 read_nic_byte(dev, rOFDM0_XAAGCCore1, &priv->DefaultInitialGain[0]);
899 read_nic_byte(dev, rOFDM0_XBAGCCore1, &priv->DefaultInitialGain[1]);
900 read_nic_byte(dev, rOFDM0_XCAGCCore1, &priv->DefaultInitialGain[2]);
901 read_nic_byte(dev, rOFDM0_XDAGCCore1, &priv->DefaultInitialGain[3]);
903 "Default initial gain (c50=0x%x, c58=0x%x, c60=0x%x, c68=0x%x)\n",
904 priv->DefaultInitialGain[0], priv->DefaultInitialGain[1],
905 priv->DefaultInitialGain[2], priv->DefaultInitialGain[3]);
908 read_nic_byte(dev, rOFDM0_RxDetector3, &priv->framesync);
909 read_nic_byte(dev, rOFDM0_RxDetector2, &tmp);
910 priv->framesyncC34 = tmp;
911 RT_TRACE(COMP_INIT, "Default framesync (0x%x) = 0x%x \n",
912 rOFDM0_RxDetector3, priv->framesync);
914 /* Read SIFS (save the value read fome MACPHY_REG.txt) */
915 read_nic_word(dev, SIFS, &priv->SifsTime);
920 /******************************************************************************
921 * function: This function sets the initialization value of Tx power Level
923 * input: net_device *dev
927 ******************************************************************************/
928 void rtl8192_phy_setTxPower(struct net_device *dev, u8 channel)
930 struct r8192_priv *priv = ieee80211_priv(dev);
931 u8 powerlevel = priv->TxPowerLevelCCK[channel-1];
932 u8 powerlevelOFDM24G = priv->TxPowerLevelOFDM24G[channel-1];
934 switch (priv->rf_chip) {
936 /* need further implement */
937 PHY_SetRF8256CCKTxPower(dev, powerlevel);
938 PHY_SetRF8256OFDMTxPower(dev, powerlevelOFDM24G);
941 RT_TRACE((COMP_PHY|COMP_ERR),
942 "error RF chipID(8225 or 8258) in function %s()\n",
949 /******************************************************************************
950 * function: This function checks Rf chip to do RF config
951 * input: net_device *dev
953 * return: only 8256 is supported
954 ******************************************************************************/
955 void rtl8192_phy_RFConfig(struct net_device *dev)
957 struct r8192_priv *priv = ieee80211_priv(dev);
959 switch (priv->rf_chip) {
961 PHY_RF8256_Config(dev);
964 RT_TRACE(COMP_ERR, "error chip id\n");
970 /******************************************************************************
971 * function: This function updates Initial gain
972 * input: net_device *dev
974 * return: As Windows has not implemented this, wait for complement
975 ******************************************************************************/
976 void rtl8192_phy_updateInitGain(struct net_device *dev)
981 /******************************************************************************
982 * function: This function read RF parameters from general head file,
984 * input: net_device *dev
985 * RF90_RADIO_PATH_E eRFPath
987 * return: return code show if RF configuration is successful(0:pass, 1:fail)
988 * notice: Delay may be required for RF configuration
989 *****************************************************************************/
990 u8 rtl8192_phy_ConfigRFWithHeaderFile(struct net_device *dev,
991 RF90_RADIO_PATH_E eRFPath)
999 for (i = 0; i < RadioA_ArrayLength; i = i+2) {
1001 if (rtl819XRadioA_Array[i] == 0xfe) {
1005 rtl8192_phy_SetRFReg(dev, eRFPath,
1006 rtl819XRadioA_Array[i],
1008 rtl819XRadioA_Array[i+1]);
1014 for (i = 0; i < RadioB_ArrayLength; i = i+2) {
1016 if (rtl819XRadioB_Array[i] == 0xfe) {
1020 rtl8192_phy_SetRFReg(dev, eRFPath,
1021 rtl819XRadioB_Array[i],
1023 rtl819XRadioB_Array[i+1]);
1029 for (i = 0; i < RadioC_ArrayLength; i = i+2) {
1031 if (rtl819XRadioC_Array[i] == 0xfe) {
1035 rtl8192_phy_SetRFReg(dev, eRFPath,
1036 rtl819XRadioC_Array[i],
1038 rtl819XRadioC_Array[i+1]);
1044 for (i = 0; i < RadioD_ArrayLength; i = i+2) {
1046 if (rtl819XRadioD_Array[i] == 0xfe) {
1050 rtl8192_phy_SetRFReg(dev, eRFPath,
1051 rtl819XRadioD_Array[i],
1053 rtl819XRadioD_Array[i+1]);
1066 /******************************************************************************
1067 * function: This function sets Tx Power of the channel
1068 * input: net_device *dev
1073 ******************************************************************************/
1074 static void rtl8192_SetTxPowerLevel(struct net_device *dev, u8 channel)
1076 struct r8192_priv *priv = ieee80211_priv(dev);
1077 u8 powerlevel = priv->TxPowerLevelCCK[channel-1];
1078 u8 powerlevelOFDM24G = priv->TxPowerLevelOFDM24G[channel-1];
1080 switch (priv->rf_chip) {
1083 PHY_SetRF8225CckTxPower(Adapter, powerlevel);
1084 PHY_SetRF8225OfdmTxPower(Adapter, powerlevelOFDM24G);
1089 PHY_SetRF8256CCKTxPower(dev, powerlevel);
1090 PHY_SetRF8256OFDMTxPower(dev, powerlevelOFDM24G);
1096 RT_TRACE(COMP_ERR, "unknown rf chip ID in %s()\n", __func__);
1102 /******************************************************************************
1103 * function: This function sets RF state on or off
1104 * input: net_device *dev
1105 * RT_RF_POWER_STATE eRFPowerState //Power State to set
1109 *****************************************************************************/
1110 bool rtl8192_SetRFPowerState(struct net_device *dev,
1111 RT_RF_POWER_STATE eRFPowerState)
1113 bool bResult = true;
1114 struct r8192_priv *priv = ieee80211_priv(dev);
1116 if (eRFPowerState == priv->ieee80211->eRFPowerState)
1119 if (priv->SetRFPowerStateInProgress == true)
1122 priv->SetRFPowerStateInProgress = true;
1124 switch (priv->rf_chip) {
1126 switch (eRFPowerState) {
1129 /* enable RF-Chip A/B - 0x860[4] */
1130 rtl8192_setBBreg(dev, rFPGA0_XA_RFInterfaceOE, BIT4,
1132 /* analog to digital on - 0x88c[9:8] */
1133 rtl8192_setBBreg(dev, rFPGA0_AnalogParameter4, 0x300,
1135 /* digital to analog on - 0x880[4:3] */
1136 rtl8192_setBBreg(dev, rFPGA0_AnalogParameter1, 0x18,
1138 /* rx antenna on - 0xc04[1:0] */
1139 rtl8192_setBBreg(dev, rOFDM0_TRxPathEnable, 0x3, 0x3);
1140 /* rx antenna on - 0xd04[1:0] */
1141 rtl8192_setBBreg(dev, rOFDM1_TRxPathEnable, 0x3, 0x3);
1142 /* analog to digital part2 on - 0x880[6:5] */
1143 rtl8192_setBBreg(dev, rFPGA0_AnalogParameter1, 0x60,
1154 /* disable RF-Chip A/B - 0x860[4] */
1155 rtl8192_setBBreg(dev, rFPGA0_XA_RFInterfaceOE, BIT4,
1157 /* analog to digital off, for power save */
1158 rtl8192_setBBreg(dev, rFPGA0_AnalogParameter4, 0xf00,
1159 0x0); /* 0x88c[11:8] */
1160 /* digital to analog off, for power save - 0x880[4:3] */
1161 rtl8192_setBBreg(dev, rFPGA0_AnalogParameter1, 0x18,
1163 /* rx antenna off - 0xc04[3:0] */
1164 rtl8192_setBBreg(dev, rOFDM0_TRxPathEnable, 0xf, 0x0);
1165 /* rx antenna off - 0xd04[3:0] */
1166 rtl8192_setBBreg(dev, rOFDM1_TRxPathEnable, 0xf, 0x0);
1167 /* analog to digital part2 off, for power save */
1168 rtl8192_setBBreg(dev, rFPGA0_AnalogParameter1, 0x60,
1169 0x0); /* 0x880[6:5] */
1175 RT_TRACE(COMP_ERR, "%s(): unknown state to set: 0x%X\n",
1176 __func__, eRFPowerState);
1181 RT_TRACE(COMP_ERR, "Not support rf_chip(%x)\n", priv->rf_chip);
1186 /* Update current RF state variable. */
1187 pHalData->eRFPowerState = eRFPowerState;
1188 switch (pHalData->RFChipID) {
1190 switch (pHalData->eRFPowerState) {
1192 /* If Rf off reason is from IPS,
1193 LED should blink with no link */
1194 if (pMgntInfo->RfOffReason == RF_CHANGE_BY_IPS)
1195 Adapter->HalFunc.LedControlHandler(Adapter, LED_CTL_NO_LINK);
1197 /* Turn off LED if RF is not ON. */
1198 Adapter->HalFunc.LedControlHandler(Adapter, LED_CTL_POWER_OFF);
1202 /* Turn on RF we are still linked, which might
1203 happen when we quickly turn off and on HW RF.
1205 if (pMgntInfo->bMediaConnect == TRUE)
1206 Adapter->HalFunc.LedControlHandler(Adapter, LED_CTL_LINK);
1208 /* Turn off LED if RF is not ON. */
1209 Adapter->HalFunc.LedControlHandler(Adapter, LED_CTL_NO_LINK);
1218 RT_TRACE(COMP_RF, DBG_LOUD, "%s(): Unknown RF type\n",
1225 priv->SetRFPowerStateInProgress = false;
1230 /******************************************************************************
1231 * function: This function sets command table variable (struct SwChnlCmd).
1232 * input: SwChnlCmd *CmdTable //table to be set
1233 * u32 CmdTableIdx //variable index in table to be set
1234 * u32 CmdTableSz //table size
1235 * SwChnlCmdID CmdID //command ID to set
1240 * return: true if finished, false otherwise
1242 ******************************************************************************/
1243 static u8 rtl8192_phy_SetSwChnlCmdArray(SwChnlCmd *CmdTable, u32 CmdTableIdx,
1244 u32 CmdTableSz, SwChnlCmdID CmdID,
1245 u32 Para1, u32 Para2, u32 msDelay)
1249 if (CmdTable == NULL) {
1250 RT_TRACE(COMP_ERR, "%s(): CmdTable cannot be NULL\n", __func__);
1253 if (CmdTableIdx >= CmdTableSz) {
1254 RT_TRACE(COMP_ERR, "%s(): Access invalid index, please check size of the table, CmdTableIdx:%d, CmdTableSz:%d\n",
1255 __func__, CmdTableIdx, CmdTableSz);
1259 pCmd = CmdTable + CmdTableIdx;
1260 pCmd->CmdID = CmdID;
1261 pCmd->Para1 = Para1;
1262 pCmd->Para2 = Para2;
1263 pCmd->msDelay = msDelay;
1268 /******************************************************************************
1269 * function: This function sets channel step by step
1270 * input: net_device *dev
1272 * u8 *stage //3 stages
1274 * u32 *delay //whether need to delay
1275 * output: store new stage, step and delay for next step
1276 * (combine with function above)
1277 * return: true if finished, false otherwise
1278 * notice: Wait for simpler function to replace it
1279 *****************************************************************************/
1280 static u8 rtl8192_phy_SwChnlStepByStep(struct net_device *dev, u8 channel,
1281 u8 *stage, u8 *step, u32 *delay)
1283 struct r8192_priv *priv = ieee80211_priv(dev);
1284 SwChnlCmd PreCommonCmd[MAX_PRECMD_CNT];
1285 u32 PreCommonCmdCnt;
1286 SwChnlCmd PostCommonCmd[MAX_POSTCMD_CNT];
1287 u32 PostCommonCmdCnt;
1288 SwChnlCmd RfDependCmd[MAX_RFDEPENDCMD_CNT];
1290 SwChnlCmd *CurrentCmd = NULL;
1293 RT_TRACE(COMP_CH, "%s() stage: %d, step: %d, channel: %d\n",
1294 __func__, *stage, *step, channel);
1295 if (!IsLegalChannel(priv->ieee80211, channel)) {
1296 RT_TRACE(COMP_ERR, "set to illegal channel: %d\n", channel);
1297 /* return true to tell upper caller function this channel
1298 setting is finished! Or it will in while loop. */
1301 /* FIXME: need to check whether channel is legal or not here */
1304 /* <1> Fill up pre common command. */
1305 PreCommonCmdCnt = 0;
1306 rtl8192_phy_SetSwChnlCmdArray(PreCommonCmd, PreCommonCmdCnt++,
1307 MAX_PRECMD_CNT, CmdID_SetTxPowerLevel,
1309 rtl8192_phy_SetSwChnlCmdArray(PreCommonCmd, PreCommonCmdCnt++,
1310 MAX_PRECMD_CNT, CmdID_End, 0, 0, 0);
1312 /* <2> Fill up post common command. */
1313 PostCommonCmdCnt = 0;
1315 rtl8192_phy_SetSwChnlCmdArray(PostCommonCmd, PostCommonCmdCnt++,
1316 MAX_POSTCMD_CNT, CmdID_End, 0, 0, 0);
1318 /* <3> Fill up RF dependent command. */
1320 switch (priv->rf_chip) {
1322 if (!(channel >= 1 && channel <= 14)) {
1324 "illegal channel for Zebra 8225: %d\n",
1328 rtl8192_phy_SetSwChnlCmdArray(RfDependCmd, RfDependCmdCnt++,
1329 MAX_RFDEPENDCMD_CNT,
1332 RF_CHANNEL_TABLE_ZEBRA[channel],
1334 rtl8192_phy_SetSwChnlCmdArray(RfDependCmd, RfDependCmdCnt++,
1335 MAX_RFDEPENDCMD_CNT,
1336 CmdID_End, 0, 0, 0);
1340 /* TEST!! This is not the table for 8256!! */
1341 if (!(channel >= 1 && channel <= 14)) {
1343 "illegal channel for Zebra 8256: %d\n",
1347 rtl8192_phy_SetSwChnlCmdArray(RfDependCmd, RfDependCmdCnt++,
1348 MAX_RFDEPENDCMD_CNT,
1350 rZebra1_Channel, channel, 10);
1351 rtl8192_phy_SetSwChnlCmdArray(RfDependCmd, RfDependCmdCnt++,
1352 MAX_RFDEPENDCMD_CNT,
1353 CmdID_End, 0, 0, 0);
1360 RT_TRACE(COMP_ERR, "Unknown RFChipID: %d\n", priv->rf_chip);
1369 CurrentCmd = &PreCommonCmd[*step];
1372 CurrentCmd = &RfDependCmd[*step];
1375 CurrentCmd = &PostCommonCmd[*step];
1379 if (CurrentCmd->CmdID == CmdID_End) {
1380 if ((*stage) == 2) {
1381 (*delay) = CurrentCmd->msDelay;
1390 switch (CurrentCmd->CmdID) {
1391 case CmdID_SetTxPowerLevel:
1392 if (priv->card_8192_version == (u8)VERSION_819xU_A)
1393 /* consider it later! */
1394 rtl8192_SetTxPowerLevel(dev, channel);
1396 case CmdID_WritePortUlong:
1397 write_nic_dword(dev, CurrentCmd->Para1,
1400 case CmdID_WritePortUshort:
1401 write_nic_word(dev, CurrentCmd->Para1,
1402 (u16)CurrentCmd->Para2);
1404 case CmdID_WritePortUchar:
1405 write_nic_byte(dev, CurrentCmd->Para1,
1406 (u8)CurrentCmd->Para2);
1408 case CmdID_RF_WriteReg:
1409 for (eRFPath = 0; eRFPath < RF90_PATH_MAX; eRFPath++) {
1410 rtl8192_phy_SetRFReg(dev,
1411 (RF90_RADIO_PATH_E)eRFPath,
1424 (*delay) = CurrentCmd->msDelay;
1429 /******************************************************************************
1430 * function: This function does actually set channel work
1431 * input: net_device *dev
1435 * notice: We should not call this function directly
1436 *****************************************************************************/
1437 static void rtl8192_phy_FinishSwChnlNow(struct net_device *dev, u8 channel)
1439 struct r8192_priv *priv = ieee80211_priv(dev);
1442 while (!rtl8192_phy_SwChnlStepByStep(dev, channel, &priv->SwChnlStage,
1443 &priv->SwChnlStep, &delay)) {
1449 /******************************************************************************
1450 * function: Callback routine of the work item for switch channel.
1451 * input: net_device *dev
1455 *****************************************************************************/
1456 void rtl8192_SwChnl_WorkItem(struct net_device *dev)
1459 struct r8192_priv *priv = ieee80211_priv(dev);
1461 RT_TRACE(COMP_CH, "==> SwChnlCallback819xUsbWorkItem(), chan:%d\n",
1465 rtl8192_phy_FinishSwChnlNow(dev, priv->chan);
1467 RT_TRACE(COMP_CH, "<== SwChnlCallback819xUsbWorkItem()\n");
1470 /******************************************************************************
1471 * function: This function scheduled actual work item to set channel
1472 * input: net_device *dev
1473 * u8 channel //channel to set
1475 * return: return code show if workitem is scheduled (1:pass, 0:fail)
1476 * notice: Delay may be required for RF configuration
1477 ******************************************************************************/
1478 u8 rtl8192_phy_SwChnl(struct net_device *dev, u8 channel)
1480 struct r8192_priv *priv = ieee80211_priv(dev);
1481 RT_TRACE(COMP_CH, "%s(), SwChnlInProgress: %d\n", __func__,
1482 priv->SwChnlInProgress);
1485 if (priv->SwChnlInProgress)
1488 /* -------------------------------------------- */
1489 switch (priv->ieee80211->mode) {
1490 case WIRELESS_MODE_A:
1491 case WIRELESS_MODE_N_5G:
1492 if (channel <= 14) {
1493 RT_TRACE(COMP_ERR, "WIRELESS_MODE_A but channel<=14\n");
1497 case WIRELESS_MODE_B:
1499 RT_TRACE(COMP_ERR, "WIRELESS_MODE_B but channel>14\n");
1503 case WIRELESS_MODE_G:
1504 case WIRELESS_MODE_N_24G:
1506 RT_TRACE(COMP_ERR, "WIRELESS_MODE_G but channel>14\n");
1511 /* -------------------------------------------- */
1513 priv->SwChnlInProgress = true;
1517 priv->chan = channel;
1519 priv->SwChnlStage = 0;
1520 priv->SwChnlStep = 0;
1522 rtl8192_SwChnl_WorkItem(dev);
1524 priv->SwChnlInProgress = false;
1528 /******************************************************************************
1529 * function: Callback routine of the work item for set bandwidth mode.
1530 * input: net_device *dev
1533 * notice: I doubt whether SetBWModeInProgress flag is necessary as we can
1534 * test whether current work in the queue or not.//do I?
1535 *****************************************************************************/
1536 void rtl8192_SetBWModeWorkItem(struct net_device *dev)
1539 struct r8192_priv *priv = ieee80211_priv(dev);
1542 RT_TRACE(COMP_SWBW, "%s() Switch to %s bandwidth\n", __func__,
1543 priv->CurrentChannelBW == HT_CHANNEL_WIDTH_20?"20MHz":"40MHz");
1546 if (priv->rf_chip == RF_PSEUDO_11N) {
1547 priv->SetBWModeInProgress = false;
1551 /* <1> Set MAC register */
1552 read_nic_byte(dev, BW_OPMODE, ®BwOpMode);
1554 switch (priv->CurrentChannelBW) {
1555 case HT_CHANNEL_WIDTH_20:
1556 regBwOpMode |= BW_OPMODE_20MHZ;
1557 /* We have not verify whether this register works */
1558 write_nic_byte(dev, BW_OPMODE, regBwOpMode);
1561 case HT_CHANNEL_WIDTH_20_40:
1562 regBwOpMode &= ~BW_OPMODE_20MHZ;
1563 /* We have not verify whether this register works */
1564 write_nic_byte(dev, BW_OPMODE, regBwOpMode);
1569 "SetChannelBandwidth819xUsb(): unknown Bandwidth: %#X\n",
1570 priv->CurrentChannelBW);
1574 /* <2> Set PHY related register */
1575 switch (priv->CurrentChannelBW) {
1576 case HT_CHANNEL_WIDTH_20:
1577 rtl8192_setBBreg(dev, rFPGA0_RFMOD, bRFMOD, 0x0);
1578 rtl8192_setBBreg(dev, rFPGA1_RFMOD, bRFMOD, 0x0);
1579 rtl8192_setBBreg(dev, rFPGA0_AnalogParameter1,
1582 /* Correct the tx power for CCK rate in 20M. */
1583 priv->cck_present_attentuation =
1584 priv->cck_present_attentuation_20Mdefault +
1585 priv->cck_present_attentuation_difference;
1587 if (priv->cck_present_attentuation > 22)
1588 priv->cck_present_attentuation = 22;
1589 if (priv->cck_present_attentuation < 0)
1590 priv->cck_present_attentuation = 0;
1592 "20M, pHalData->CCKPresentAttentuation = %d\n",
1593 priv->cck_present_attentuation);
1595 if (priv->chan == 14 && !priv->bcck_in_ch14) {
1596 priv->bcck_in_ch14 = TRUE;
1597 dm_cck_txpower_adjust(dev, priv->bcck_in_ch14);
1598 } else if (priv->chan != 14 && priv->bcck_in_ch14) {
1599 priv->bcck_in_ch14 = FALSE;
1600 dm_cck_txpower_adjust(dev, priv->bcck_in_ch14);
1602 dm_cck_txpower_adjust(dev, priv->bcck_in_ch14);
1606 case HT_CHANNEL_WIDTH_20_40:
1607 rtl8192_setBBreg(dev, rFPGA0_RFMOD, bRFMOD, 0x1);
1608 rtl8192_setBBreg(dev, rFPGA1_RFMOD, bRFMOD, 0x1);
1609 rtl8192_setBBreg(dev, rCCK0_System, bCCKSideBand,
1610 priv->nCur40MhzPrimeSC>>1);
1611 rtl8192_setBBreg(dev, rFPGA0_AnalogParameter1, 0x00100000, 0);
1612 rtl8192_setBBreg(dev, rOFDM1_LSTF, 0xC00,
1613 priv->nCur40MhzPrimeSC);
1614 priv->cck_present_attentuation =
1615 priv->cck_present_attentuation_40Mdefault +
1616 priv->cck_present_attentuation_difference;
1618 if (priv->cck_present_attentuation > 22)
1619 priv->cck_present_attentuation = 22;
1620 if (priv->cck_present_attentuation < 0)
1621 priv->cck_present_attentuation = 0;
1624 "40M, pHalData->CCKPresentAttentuation = %d\n",
1625 priv->cck_present_attentuation);
1626 if (priv->chan == 14 && !priv->bcck_in_ch14) {
1627 priv->bcck_in_ch14 = true;
1628 dm_cck_txpower_adjust(dev, priv->bcck_in_ch14);
1629 } else if (priv->chan != 14 && priv->bcck_in_ch14) {
1630 priv->bcck_in_ch14 = false;
1631 dm_cck_txpower_adjust(dev, priv->bcck_in_ch14);
1633 dm_cck_txpower_adjust(dev, priv->bcck_in_ch14);
1639 "SetChannelBandwidth819xUsb(): unknown Bandwidth: %#X\n",
1640 priv->CurrentChannelBW);
1644 /* Skip over setting of J-mode in BB register here.
1645 Default value is "None J mode". */
1647 /* <3> Set RF related register */
1648 switch (priv->rf_chip) {
1651 PHY_SetRF8225Bandwidth(Adapter, pHalData->CurrentChannelBW);
1656 PHY_SetRF8256Bandwidth(dev, priv->CurrentChannelBW);
1666 RT_TRACE(COMP_ERR, "Unknown RFChipID: %d\n", priv->rf_chip);
1669 priv->SetBWModeInProgress = false;
1671 RT_TRACE(COMP_SWBW, "<==SetBWMode819xUsb(), %d\n",
1672 atomic_read(&priv->ieee80211->atm_swbw));
1675 /******************************************************************************
1676 * function: This function schedules bandwidth switch work.
1677 * input: struct net_deviceq *dev
1678 * HT_CHANNEL_WIDTH bandwidth //20M or 40M
1679 * HT_EXTCHNL_OFFSET offset //Upper, Lower, or Don't care
1682 * notice: I doubt whether SetBWModeInProgress flag is necessary as we can
1683 * test whether current work in the queue or not.//do I?
1684 *****************************************************************************/
1685 void rtl8192_SetBWMode(struct net_device *dev, HT_CHANNEL_WIDTH bandwidth,
1686 HT_EXTCHNL_OFFSET offset)
1688 struct r8192_priv *priv = ieee80211_priv(dev);
1690 if (priv->SetBWModeInProgress)
1692 priv->SetBWModeInProgress = true;
1694 priv->CurrentChannelBW = bandwidth;
1696 if (offset == HT_EXTCHNL_OFFSET_LOWER)
1697 priv->nCur40MhzPrimeSC = HAL_PRIME_CHNL_OFFSET_UPPER;
1698 else if (offset == HT_EXTCHNL_OFFSET_UPPER)
1699 priv->nCur40MhzPrimeSC = HAL_PRIME_CHNL_OFFSET_LOWER;
1701 priv->nCur40MhzPrimeSC = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
1703 rtl8192_SetBWModeWorkItem(dev);
1707 void InitialGain819xUsb(struct net_device *dev, u8 Operation)
1709 struct r8192_priv *priv = ieee80211_priv(dev);
1711 priv->InitialGainOperateType = Operation;
1714 queue_delayed_work(priv->priv_wq, &priv->initialgain_operate_wq, 0);
1717 void InitialGainOperateWorkItemCallBack(struct work_struct *work)
1719 struct delayed_work *dwork = container_of(work, struct delayed_work,
1721 struct r8192_priv *priv = container_of(dwork, struct r8192_priv,
1722 initialgain_operate_wq);
1723 struct net_device *dev = priv->ieee80211->dev;
1724 #define SCAN_RX_INITIAL_GAIN 0x17
1725 #define POWER_DETECTION_TH 0x08
1730 Operation = priv->InitialGainOperateType;
1732 switch (Operation) {
1734 RT_TRACE(COMP_SCAN, "IG_Backup, backup the initial gain.\n");
1735 initial_gain = SCAN_RX_INITIAL_GAIN;
1736 bitmask = bMaskByte0;
1737 if (dm_digtable.dig_algorithm == DIG_ALGO_BY_FALSE_ALARM)
1739 rtl8192_setBBreg(dev, UFWP, bMaskByte1, 0x8);
1740 priv->initgain_backup.xaagccore1 =
1741 (u8)rtl8192_QueryBBReg(dev, rOFDM0_XAAGCCore1, bitmask);
1742 priv->initgain_backup.xbagccore1 =
1743 (u8)rtl8192_QueryBBReg(dev, rOFDM0_XBAGCCore1, bitmask);
1744 priv->initgain_backup.xcagccore1 =
1745 (u8)rtl8192_QueryBBReg(dev, rOFDM0_XCAGCCore1, bitmask);
1746 priv->initgain_backup.xdagccore1 =
1747 (u8)rtl8192_QueryBBReg(dev, rOFDM0_XDAGCCore1, bitmask);
1748 bitmask = bMaskByte2;
1749 priv->initgain_backup.cca =
1750 (u8)rtl8192_QueryBBReg(dev, rCCK0_CCA, bitmask);
1752 RT_TRACE(COMP_SCAN, "Scan InitialGainBackup 0xc50 is %x\n",
1753 priv->initgain_backup.xaagccore1);
1754 RT_TRACE(COMP_SCAN, "Scan InitialGainBackup 0xc58 is %x\n",
1755 priv->initgain_backup.xbagccore1);
1756 RT_TRACE(COMP_SCAN, "Scan InitialGainBackup 0xc60 is %x\n",
1757 priv->initgain_backup.xcagccore1);
1758 RT_TRACE(COMP_SCAN, "Scan InitialGainBackup 0xc68 is %x\n",
1759 priv->initgain_backup.xdagccore1);
1760 RT_TRACE(COMP_SCAN, "Scan InitialGainBackup 0xa0a is %x\n",
1761 priv->initgain_backup.cca);
1763 RT_TRACE(COMP_SCAN, "Write scan initial gain = 0x%x \n",
1765 write_nic_byte(dev, rOFDM0_XAAGCCore1, initial_gain);
1766 write_nic_byte(dev, rOFDM0_XBAGCCore1, initial_gain);
1767 write_nic_byte(dev, rOFDM0_XCAGCCore1, initial_gain);
1768 write_nic_byte(dev, rOFDM0_XDAGCCore1, initial_gain);
1769 RT_TRACE(COMP_SCAN, "Write scan 0xa0a = 0x%x \n",
1770 POWER_DETECTION_TH);
1771 write_nic_byte(dev, 0xa0a, POWER_DETECTION_TH);
1774 RT_TRACE(COMP_SCAN, "IG_Restore, restore the initial gain.\n");
1775 bitmask = 0x7f; /* Bit0 ~ Bit6 */
1776 if (dm_digtable.dig_algorithm == DIG_ALGO_BY_FALSE_ALARM)
1778 rtl8192_setBBreg(dev, UFWP, bMaskByte1, 0x8);
1780 rtl8192_setBBreg(dev, rOFDM0_XAAGCCore1, bitmask,
1781 (u32)priv->initgain_backup.xaagccore1);
1782 rtl8192_setBBreg(dev, rOFDM0_XBAGCCore1, bitmask,
1783 (u32)priv->initgain_backup.xbagccore1);
1784 rtl8192_setBBreg(dev, rOFDM0_XCAGCCore1, bitmask,
1785 (u32)priv->initgain_backup.xcagccore1);
1786 rtl8192_setBBreg(dev, rOFDM0_XDAGCCore1, bitmask,
1787 (u32)priv->initgain_backup.xdagccore1);
1788 bitmask = bMaskByte2;
1789 rtl8192_setBBreg(dev, rCCK0_CCA, bitmask,
1790 (u32)priv->initgain_backup.cca);
1792 RT_TRACE(COMP_SCAN, "Scan BBInitialGainRestore 0xc50 is %x\n",
1793 priv->initgain_backup.xaagccore1);
1794 RT_TRACE(COMP_SCAN, "Scan BBInitialGainRestore 0xc58 is %x\n",
1795 priv->initgain_backup.xbagccore1);
1796 RT_TRACE(COMP_SCAN, "Scan BBInitialGainRestore 0xc60 is %x\n",
1797 priv->initgain_backup.xcagccore1);
1798 RT_TRACE(COMP_SCAN, "Scan BBInitialGainRestore 0xc68 is %x\n",
1799 priv->initgain_backup.xdagccore1);
1800 RT_TRACE(COMP_SCAN, "Scan BBInitialGainRestore 0xa0a is %x\n",
1801 priv->initgain_backup.cca);
1803 rtl8192_phy_setTxPower(dev, priv->ieee80211->current_network.channel);
1805 if (dm_digtable.dig_algorithm == DIG_ALGO_BY_FALSE_ALARM)
1807 rtl8192_setBBreg(dev, UFWP, bMaskByte1, 0x1);
1810 RT_TRACE(COMP_SCAN, "Unknown IG Operation. \n");