2 * phy_302_calibration.c
4 * Copyright (C) 2002, 2005 Winbond Electronics Corp.
7 * ---------------------------------------------------------------------------
8 * 0.01.001, 2003-04-16, Kevin created
12 /****************** INCLUDE FILES SECTION ***********************************/
14 #include "phy_calibration.h"
18 /****************** DEBUG CONSTANT AND MACRO SECTION ************************/
20 /****************** LOCAL CONSTANT AND MACRO SECTION ************************/
22 #define US 1000//MICROSECOND
24 #define AG_CONST 0.6072529350
25 #define FIXED(X) ((s32)((X) * 32768.0))
26 #define DEG2RAD(X) 0.017453 * (X)
28 /****************** LOCAL TYPE DEFINITION SECTION ***************************/
29 typedef s32 fixed; /* 16.16 fixed-point */
31 static const fixed Angles[]=
33 FIXED(DEG2RAD(45.0)), FIXED(DEG2RAD(26.565)), FIXED(DEG2RAD(14.0362)),
34 FIXED(DEG2RAD(7.12502)), FIXED(DEG2RAD(3.57633)), FIXED(DEG2RAD(1.78991)),
35 FIXED(DEG2RAD(0.895174)),FIXED(DEG2RAD(0.447614)),FIXED(DEG2RAD(0.223811)),
36 FIXED(DEG2RAD(0.111906)),FIXED(DEG2RAD(0.055953)),FIXED(DEG2RAD(0.027977))
39 /****************** LOCAL FUNCTION DECLARATION SECTION **********************/
40 //void _phy_rf_write_delay(struct hw_data *phw_data);
41 //void phy_init_rf(struct hw_data *phw_data);
43 /****************** FUNCTION DEFINITION SECTION *****************************/
45 s32 _s13_to_s32(u32 data)
49 val = (data & 0x0FFF);
51 if ((data & BIT(12)) != 0)
59 u32 _s32_to_s13(s32 data)
67 else if (data < -4096)
77 /****************************************************************************/
78 s32 _s4_to_s32(u32 data)
82 val = (data & 0x0007);
84 if ((data & BIT(3)) != 0)
92 u32 _s32_to_s4(s32 data)
110 /****************************************************************************/
111 s32 _s5_to_s32(u32 data)
115 val = (data & 0x000F);
117 if ((data & BIT(4)) != 0)
125 u32 _s32_to_s5(s32 data)
143 /****************************************************************************/
144 s32 _s6_to_s32(u32 data)
148 val = (data & 0x001F);
150 if ((data & BIT(5)) != 0)
158 u32 _s32_to_s6(s32 data)
176 /****************************************************************************/
177 s32 _s9_to_s32(u32 data)
183 if ((data & BIT(8)) != 0)
191 u32 _s32_to_s9(s32 data)
199 else if (data < -256)
209 /****************************************************************************/
224 /****************************************************************************/
225 // The following code is sqare-root function.
226 // sqsum is the input and the output is sq_rt;
227 // The maximum of sqsum = 2^27 -1;
232 int g0, g1, g2, g3, g4;
237 g4 = sqsum / 100000000;
238 g3 = (sqsum - g4*100000000) /1000000;
239 g2 = (sqsum - g4*100000000 - g3*1000000) /10000;
240 g1 = (sqsum - g4*100000000 - g3*1000000 - g2*10000) /100;
241 g0 = (sqsum - g4*100000000 - g3*1000000 - g2*10000 - g1*100);
246 while (((seed+1)*(step+1)) <= next)
252 sq_rt = seed * 10000;
253 next = (next-(seed*step))*100 + g3;
256 seed = 2 * seed * 10;
257 while (((seed+1)*(step+1)) <= next)
263 sq_rt = sq_rt + step * 1000;
264 next = (next - seed * step) * 100 + g2;
265 seed = (seed + step) * 10;
267 while (((seed+1)*(step+1)) <= next)
273 sq_rt = sq_rt + step * 100;
274 next = (next - seed * step) * 100 + g1;
275 seed = (seed + step) * 10;
278 while (((seed+1)*(step+1)) <= next)
284 sq_rt = sq_rt + step * 10;
285 next = (next - seed* step) * 100 + g0;
286 seed = (seed + step) * 10;
289 while (((seed+1)*(step+1)) <= next)
295 sq_rt = sq_rt + step;
300 /****************************************************************************/
301 void _sin_cos(s32 angle, s32 *sin, s32 *cos)
303 fixed X, Y, TargetAngle, CurrAngle;
306 X=FIXED(AG_CONST); // AG_CONST * cos(0)
307 Y=0; // AG_CONST * sin(0)
308 TargetAngle=abs(angle);
311 for (Step=0; Step < 12; Step++)
315 if(TargetAngle > CurrAngle)
317 NewX=X - (Y >> Step);
320 CurrAngle += Angles[Step];
324 NewX=X + (Y >> Step);
327 CurrAngle -= Angles[Step];
343 static unsigned char hal_get_dxx_reg(struct hw_data *pHwData, u16 number, u32 * pValue)
347 return Wb35Reg_ReadSync(pHwData, number, pValue);
349 #define hw_get_dxx_reg( _A, _B, _C ) hal_get_dxx_reg( _A, _B, (u32 *)_C )
351 static unsigned char hal_set_dxx_reg(struct hw_data *pHwData, u16 number, u32 value)
357 ret = Wb35Reg_WriteSync(pHwData, number, value);
360 #define hw_set_dxx_reg( _A, _B, _C ) hal_set_dxx_reg( _A, _B, (u32)_C )
363 void _reset_rx_cal(struct hw_data *phw_data)
367 hw_get_dxx_reg(phw_data, 0x54, &val);
369 if (phw_data->revision == 0x2002) // 1st-cut
378 hw_set_dxx_reg(phw_data, 0x54, val);
382 // ************for winbond calibration*********
387 // *********************************************
388 void _rxadc_dc_offset_cancellation_winbond(struct hw_data *phw_data, u32 frequency)
395 PHY_DEBUG(("[CAL] -> [1]_rxadc_dc_offset_cancellation()\n"));
396 phy_init_rf(phw_data);
398 // set calibration channel
399 if( (RF_WB_242 == phw_data->phy_type) ||
400 (RF_WB_242_1 == phw_data->phy_type) ) // 20060619.5 Add
402 if ((frequency >= 2412) && (frequency <= 2484))
404 // w89rf242 change frequency to 2390Mhz
405 PHY_DEBUG(("[CAL] W89RF242/11G/Channel=2390Mhz\n"));
406 phy_set_rf_data(phw_data, 3, (3<<24)|0x025586);
415 // reset cancel_dc_i[9:5] and cancel_dc_q[4:0] in register DC_Cancel
416 hw_get_dxx_reg(phw_data, 0x5C, &val);
418 hw_set_dxx_reg(phw_data, 0x5C, val);
420 // reset the TX and RX IQ calibration data
421 hw_set_dxx_reg(phw_data, 0x3C, 0);
422 hw_set_dxx_reg(phw_data, 0x54, 0);
424 hw_set_dxx_reg(phw_data, 0x58, 0x30303030); // IQ_Alpha Changed
427 hw_get_dxx_reg(phw_data, REG_AGC_CTRL3, ®_agc_ctrl3);
428 reg_agc_ctrl3 &= ~BIT(2);
429 reg_agc_ctrl3 |= (MASK_LNA_FIX_GAIN|MASK_AGC_FIX);
430 hw_set_dxx_reg(phw_data, REG_AGC_CTRL3, reg_agc_ctrl3);
432 hw_get_dxx_reg(phw_data, REG_AGC_CTRL5, &val);
433 val |= MASK_AGC_FIX_GAIN;
434 hw_set_dxx_reg(phw_data, REG_AGC_CTRL5, val);
437 hw_get_dxx_reg(phw_data, REG_A_ACQ_CTRL, ®_a_acq_ctrl);
438 reg_a_acq_ctrl |= MASK_AMER_OFF_REG;
439 hw_set_dxx_reg(phw_data, REG_A_ACQ_CTRL, reg_a_acq_ctrl);
441 hw_get_dxx_reg(phw_data, REG_B_ACQ_CTRL, ®_b_acq_ctrl);
442 reg_b_acq_ctrl |= MASK_BMER_OFF_REG;
443 hw_set_dxx_reg(phw_data, REG_B_ACQ_CTRL, reg_b_acq_ctrl);
445 // c. Make sure MAC is in receiving mode
446 // d. Turn ON ADC calibration
447 // - ADC calibrator is triggered by this signal rising from 0 to 1
448 hw_get_dxx_reg(phw_data, REG_MODE_CTRL, &val);
449 val &= ~MASK_ADC_DC_CAL_STR;
450 hw_set_dxx_reg(phw_data, REG_MODE_CTRL, val);
452 val |= MASK_ADC_DC_CAL_STR;
453 hw_set_dxx_reg(phw_data, REG_MODE_CTRL, val);
455 // e. The result are shown in "adc_dc_cal_i[8:0] and adc_dc_cal_q[8:0]"
457 hw_get_dxx_reg(phw_data, REG_OFFSET_READ, &val);
458 PHY_DEBUG(("[CAL] REG_OFFSET_READ = 0x%08X\n", val));
460 PHY_DEBUG(("[CAL] ** adc_dc_cal_i = %d (0x%04X)\n",
461 _s9_to_s32(val&0x000001FF), val&0x000001FF));
462 PHY_DEBUG(("[CAL] ** adc_dc_cal_q = %d (0x%04X)\n",
463 _s9_to_s32((val&0x0003FE00)>>9), (val&0x0003FE00)>>9));
466 hw_get_dxx_reg(phw_data, REG_MODE_CTRL, &val);
467 val &= ~MASK_ADC_DC_CAL_STR;
468 hw_set_dxx_reg(phw_data, REG_MODE_CTRL, val);
471 //hw_get_dxx_reg(phw_data, REG_A_ACQ_CTRL, ®_a_acq_ctrl);
472 reg_a_acq_ctrl &= ~MASK_AMER_OFF_REG;
473 hw_set_dxx_reg(phw_data, REG_A_ACQ_CTRL, reg_a_acq_ctrl);
475 //hw_get_dxx_reg(phw_data, REG_B_ACQ_CTRL, ®_b_acq_ctrl);
476 reg_b_acq_ctrl &= ~MASK_BMER_OFF_REG;
477 hw_set_dxx_reg(phw_data, REG_B_ACQ_CTRL, reg_b_acq_ctrl);
480 //hw_get_dxx_reg(phw_data, REG_AGC_CTRL3, &val);
481 reg_agc_ctrl3 |= BIT(2);
482 reg_agc_ctrl3 &= ~(MASK_LNA_FIX_GAIN|MASK_AGC_FIX);
483 hw_set_dxx_reg(phw_data, REG_AGC_CTRL3, reg_agc_ctrl3);
486 ////////////////////////////////////////////////////////
487 void _txidac_dc_offset_cancellation_winbond(struct hw_data *phw_data)
497 s32 fix_cancel_dc_i = 0;
501 PHY_DEBUG(("[CAL] -> [2]_txidac_dc_offset_cancellation()\n"));
503 // a. Set to "TX calibration mode"
505 //0x01 0xEE3FC2 ; 3B8FF ; Calibration (6a). enable TX IQ calibration loop circuits
506 phy_set_rf_data(phw_data, 1, (1<<24)|0xEE3FC2);
507 //0x0B 0x1905D6 ; 06417 ; Calibration (6b). enable TX I/Q cal loop squaring circuit
508 phy_set_rf_data(phw_data, 11, (11<<24)|0x1901D6);
509 //0x05 0x24C60A ; 09318 ; Calibration (6c). setting TX-VGA gain: TXGCH=2 & GPK=110 --> to be optimized
510 phy_set_rf_data(phw_data, 5, (5<<24)|0x24C48A);
511 //0x06 0x06880C ; 01A20 ; Calibration (6d). RXGCH=00; RXGCL=100 000 (RXVGA=32) --> to be optimized
512 phy_set_rf_data(phw_data, 6, (6<<24)|0x06890C);
513 //0x00 0xFDF1C0 ; 3F7C7 ; Calibration (6e). turn on IQ imbalance/Test mode
514 phy_set_rf_data(phw_data, 0, (0<<24)|0xFDF1C0);
516 hw_set_dxx_reg(phw_data, 0x58, 0x30303030); // IQ_Alpha Changed
519 hw_get_dxx_reg(phw_data, REG_AGC_CTRL3, ®_agc_ctrl3);
520 reg_agc_ctrl3 &= ~BIT(2);
521 reg_agc_ctrl3 |= (MASK_LNA_FIX_GAIN|MASK_AGC_FIX);
522 hw_set_dxx_reg(phw_data, REG_AGC_CTRL3, reg_agc_ctrl3);
524 hw_get_dxx_reg(phw_data, REG_AGC_CTRL5, &val);
525 val |= MASK_AGC_FIX_GAIN;
526 hw_set_dxx_reg(phw_data, REG_AGC_CTRL5, val);
528 // b. set iqcal_mode[1:0] to 0x2 and set iqcal_tone[3:2] to 0
529 hw_get_dxx_reg(phw_data, REG_MODE_CTRL, ®_mode_ctrl);
531 PHY_DEBUG(("[CAL] MODE_CTRL (read) = 0x%08X\n", reg_mode_ctrl));
532 reg_mode_ctrl &= ~(MASK_IQCAL_TONE_SEL|MASK_IQCAL_MODE);
535 //reg_mode_ctrl |= (MASK_CALIB_START|2);
538 //reg_mode_ctrl |= (MASK_CALIB_START|2|(1<<2));
541 reg_mode_ctrl |= (MASK_CALIB_START|2|(2<<2));
542 hw_set_dxx_reg(phw_data, REG_MODE_CTRL, reg_mode_ctrl);
543 PHY_DEBUG(("[CAL] MODE_CTRL (write) = 0x%08X\n", reg_mode_ctrl));
545 hw_get_dxx_reg(phw_data, 0x5C, ®_dc_cancel);
546 PHY_DEBUG(("[CAL] DC_CANCEL (read) = 0x%08X\n", reg_dc_cancel));
548 for (loop = 0; loop < LOOP_TIMES; loop++)
550 PHY_DEBUG(("[CAL] [%d.] ==================================\n", loop));
553 // reset cancel_dc_i[9:5] and cancel_dc_q[4:0] in register DC_Cancel
554 reg_dc_cancel &= ~(0x03FF);
555 PHY_DEBUG(("[CAL] DC_CANCEL (write) = 0x%08X\n", reg_dc_cancel));
556 hw_set_dxx_reg(phw_data, 0x5C, reg_dc_cancel);
558 hw_get_dxx_reg(phw_data, REG_CALIB_READ2, &val);
559 PHY_DEBUG(("[CAL] CALIB_READ2 = 0x%08X\n", val));
561 iqcal_image_i = _s13_to_s32(val & 0x00001FFF);
562 iqcal_image_q = _s13_to_s32((val & 0x03FFE000) >> 13);
563 sqsum = iqcal_image_i*iqcal_image_i + iqcal_image_q*iqcal_image_q;
564 mag_0 = (s32) _sqrt(sqsum);
565 PHY_DEBUG(("[CAL] mag_0=%d (iqcal_image_i=%d, iqcal_image_q=%d)\n",
566 mag_0, iqcal_image_i, iqcal_image_q));
569 reg_dc_cancel |= (1 << CANCEL_DC_I_SHIFT);
570 PHY_DEBUG(("[CAL] DC_CANCEL (write) = 0x%08X\n", reg_dc_cancel));
571 hw_set_dxx_reg(phw_data, 0x5C, reg_dc_cancel);
573 hw_get_dxx_reg(phw_data, REG_CALIB_READ2, &val);
574 PHY_DEBUG(("[CAL] CALIB_READ2 = 0x%08X\n", val));
576 iqcal_image_i = _s13_to_s32(val & 0x00001FFF);
577 iqcal_image_q = _s13_to_s32((val & 0x03FFE000) >> 13);
578 sqsum = iqcal_image_i*iqcal_image_i + iqcal_image_q*iqcal_image_q;
579 mag_1 = (s32) _sqrt(sqsum);
580 PHY_DEBUG(("[CAL] mag_1=%d (iqcal_image_i=%d, iqcal_image_q=%d)\n",
581 mag_1, iqcal_image_i, iqcal_image_q));
583 // e. Calculate the correct DC offset cancellation value for I
586 fix_cancel_dc_i = (mag_0*10000) / (mag_0*10000 - mag_1*10000);
592 PHY_DEBUG(("[CAL] ***** mag_0 = mag_1 !!\n"));
598 PHY_DEBUG(("[CAL] ** fix_cancel_dc_i = %d (0x%04X)\n",
599 fix_cancel_dc_i, _s32_to_s5(fix_cancel_dc_i)));
601 if ((abs(mag_1-mag_0)*6) > mag_0)
610 reg_dc_cancel &= ~(0x03FF);
611 reg_dc_cancel |= (_s32_to_s5(fix_cancel_dc_i) << CANCEL_DC_I_SHIFT);
612 hw_set_dxx_reg(phw_data, 0x5C, reg_dc_cancel);
613 PHY_DEBUG(("[CAL] DC_CANCEL (write) = 0x%08X\n", reg_dc_cancel));
616 reg_mode_ctrl &= ~MASK_CALIB_START;
617 hw_set_dxx_reg(phw_data, REG_MODE_CTRL, reg_mode_ctrl);
618 PHY_DEBUG(("[CAL] MODE_CTRL (write) = 0x%08X\n", reg_mode_ctrl));
621 ///////////////////////////////////////////////////////
622 void _txqdac_dc_offset_cacellation_winbond(struct hw_data *phw_data)
632 s32 fix_cancel_dc_q = 0;
636 PHY_DEBUG(("[CAL] -> [3]_txqdac_dc_offset_cacellation()\n"));
637 //0x01 0xEE3FC2 ; 3B8FF ; Calibration (6a). enable TX IQ calibration loop circuits
638 phy_set_rf_data(phw_data, 1, (1<<24)|0xEE3FC2);
639 //0x0B 0x1905D6 ; 06417 ; Calibration (6b). enable TX I/Q cal loop squaring circuit
640 phy_set_rf_data(phw_data, 11, (11<<24)|0x1901D6);
641 //0x05 0x24C60A ; 09318 ; Calibration (6c). setting TX-VGA gain: TXGCH=2 & GPK=110 --> to be optimized
642 phy_set_rf_data(phw_data, 5, (5<<24)|0x24C48A);
643 //0x06 0x06880C ; 01A20 ; Calibration (6d). RXGCH=00; RXGCL=100 000 (RXVGA=32) --> to be optimized
644 phy_set_rf_data(phw_data, 6, (6<<24)|0x06890C);
645 //0x00 0xFDF1C0 ; 3F7C7 ; Calibration (6e). turn on IQ imbalance/Test mode
646 phy_set_rf_data(phw_data, 0, (0<<24)|0xFDF1C0);
648 hw_set_dxx_reg(phw_data, 0x58, 0x30303030); // IQ_Alpha Changed
651 hw_get_dxx_reg(phw_data, REG_AGC_CTRL3, ®_agc_ctrl3);
652 reg_agc_ctrl3 &= ~BIT(2);
653 reg_agc_ctrl3 |= (MASK_LNA_FIX_GAIN|MASK_AGC_FIX);
654 hw_set_dxx_reg(phw_data, REG_AGC_CTRL3, reg_agc_ctrl3);
656 hw_get_dxx_reg(phw_data, REG_AGC_CTRL5, &val);
657 val |= MASK_AGC_FIX_GAIN;
658 hw_set_dxx_reg(phw_data, REG_AGC_CTRL5, val);
660 // a. set iqcal_mode[1:0] to 0x3 and set iqcal_tone[3:2] to 0
661 hw_get_dxx_reg(phw_data, REG_MODE_CTRL, ®_mode_ctrl);
662 PHY_DEBUG(("[CAL] MODE_CTRL (read) = 0x%08X\n", reg_mode_ctrl));
664 //reg_mode_ctrl &= ~(MASK_IQCAL_TONE_SEL|MASK_IQCAL_MODE);
665 reg_mode_ctrl &= ~(MASK_IQCAL_MODE);
666 reg_mode_ctrl |= (MASK_CALIB_START|3);
667 hw_set_dxx_reg(phw_data, REG_MODE_CTRL, reg_mode_ctrl);
668 PHY_DEBUG(("[CAL] MODE_CTRL (write) = 0x%08X\n", reg_mode_ctrl));
670 hw_get_dxx_reg(phw_data, 0x5C, ®_dc_cancel);
671 PHY_DEBUG(("[CAL] DC_CANCEL (read) = 0x%08X\n", reg_dc_cancel));
673 for (loop = 0; loop < LOOP_TIMES; loop++)
675 PHY_DEBUG(("[CAL] [%d.] ==================================\n", loop));
678 // reset cancel_dc_q[4:0] in register DC_Cancel
679 reg_dc_cancel &= ~(0x001F);
680 PHY_DEBUG(("[CAL] DC_CANCEL (write) = 0x%08X\n", reg_dc_cancel));
681 hw_set_dxx_reg(phw_data, 0x5C, reg_dc_cancel);
683 hw_get_dxx_reg(phw_data, REG_CALIB_READ2, &val);
684 PHY_DEBUG(("[CAL] CALIB_READ2 = 0x%08X\n", val));
686 iqcal_image_i = _s13_to_s32(val & 0x00001FFF);
687 iqcal_image_q = _s13_to_s32((val & 0x03FFE000) >> 13);
688 sqsum = iqcal_image_i*iqcal_image_i + iqcal_image_q*iqcal_image_q;
689 mag_0 = _sqrt(sqsum);
690 PHY_DEBUG(("[CAL] mag_0=%d (iqcal_image_i=%d, iqcal_image_q=%d)\n",
691 mag_0, iqcal_image_i, iqcal_image_q));
694 reg_dc_cancel |= (1 << CANCEL_DC_Q_SHIFT);
695 PHY_DEBUG(("[CAL] DC_CANCEL (write) = 0x%08X\n", reg_dc_cancel));
696 hw_set_dxx_reg(phw_data, 0x5C, reg_dc_cancel);
698 hw_get_dxx_reg(phw_data, REG_CALIB_READ2, &val);
699 PHY_DEBUG(("[CAL] CALIB_READ2 = 0x%08X\n", val));
701 iqcal_image_i = _s13_to_s32(val & 0x00001FFF);
702 iqcal_image_q = _s13_to_s32((val & 0x03FFE000) >> 13);
703 sqsum = iqcal_image_i*iqcal_image_i + iqcal_image_q*iqcal_image_q;
704 mag_1 = _sqrt(sqsum);
705 PHY_DEBUG(("[CAL] mag_1=%d (iqcal_image_i=%d, iqcal_image_q=%d)\n",
706 mag_1, iqcal_image_i, iqcal_image_q));
708 // d. Calculate the correct DC offset cancellation value for I
711 fix_cancel_dc_q = (mag_0*10000) / (mag_0*10000 - mag_1*10000);
717 PHY_DEBUG(("[CAL] ***** mag_0 = mag_1 !!\n"));
723 PHY_DEBUG(("[CAL] ** fix_cancel_dc_q = %d (0x%04X)\n",
724 fix_cancel_dc_q, _s32_to_s5(fix_cancel_dc_q)));
726 if ((abs(mag_1-mag_0)*6) > mag_0)
735 reg_dc_cancel &= ~(0x001F);
736 reg_dc_cancel |= (_s32_to_s5(fix_cancel_dc_q) << CANCEL_DC_Q_SHIFT);
737 hw_set_dxx_reg(phw_data, 0x5C, reg_dc_cancel);
738 PHY_DEBUG(("[CAL] DC_CANCEL (write) = 0x%08X\n", reg_dc_cancel));
742 reg_mode_ctrl &= ~MASK_CALIB_START;
743 hw_set_dxx_reg(phw_data, REG_MODE_CTRL, reg_mode_ctrl);
744 PHY_DEBUG(("[CAL] MODE_CTRL (write) = 0x%08X\n", reg_mode_ctrl));
747 //20060612.1.a 20060718.1 Modify
748 u8 _tx_iq_calibration_loop_winbond(struct hw_data *phw_data,
771 s32 iqcal_tone_i_avg,iqcal_tone_q_avg;
775 PHY_DEBUG(("[CAL] -> _tx_iq_calibration_loop()\n"));
776 PHY_DEBUG(("[CAL] ** a_2_threshold = %d\n", a_2_threshold));
777 PHY_DEBUG(("[CAL] ** b_2_threshold = %d\n", b_2_threshold));
781 hw_get_dxx_reg(phw_data, REG_MODE_CTRL, ®_mode_ctrl);
782 PHY_DEBUG(("[CAL] MODE_CTRL (read) = 0x%08X\n", reg_mode_ctrl));
788 PHY_DEBUG(("[CAL] [%d.] <_tx_iq_calibration_loop>\n", (LOOP_TIMES-loop+1)));
792 if( !hw_set_dxx_reg(phw_data, 0x3C, 0x00) ) // 20060718.1 modify
794 for(capture_time=0;capture_time<10;capture_time++)
796 // a. Set iqcal_mode[1:0] to 0x2 and set "calib_start" to 0x1 to
797 // enable "IQ alibration Mode II"
798 reg_mode_ctrl &= ~(MASK_IQCAL_TONE_SEL|MASK_IQCAL_MODE);
799 reg_mode_ctrl &= ~MASK_IQCAL_MODE;
800 reg_mode_ctrl |= (MASK_CALIB_START|0x02);
801 reg_mode_ctrl |= (MASK_CALIB_START|0x02|2<<2);
802 hw_set_dxx_reg(phw_data, REG_MODE_CTRL, reg_mode_ctrl);
803 PHY_DEBUG(("[CAL] MODE_CTRL (write) = 0x%08X\n", reg_mode_ctrl));
806 hw_get_dxx_reg(phw_data, REG_CALIB_READ1, &val);
807 PHY_DEBUG(("[CAL] CALIB_READ1 = 0x%08X\n", val));
809 iqcal_tone_i0 = _s13_to_s32(val & 0x00001FFF);
810 iqcal_tone_q0 = _s13_to_s32((val & 0x03FFE000) >> 13);
811 PHY_DEBUG(("[CAL] ** iqcal_tone_i0=%d, iqcal_tone_q0=%d\n",
812 iqcal_tone_i0, iqcal_tone_q0));
814 sqsum = iqcal_tone_i0*iqcal_tone_i0 +
815 iqcal_tone_q0*iqcal_tone_q0;
816 iq_mag_0_tx = (s32) _sqrt(sqsum);
817 PHY_DEBUG(("[CAL] ** iq_mag_0_tx=%d\n", iq_mag_0_tx));
819 // c. Set "calib_start" to 0x0
820 reg_mode_ctrl &= ~MASK_CALIB_START;
821 hw_set_dxx_reg(phw_data, REG_MODE_CTRL, reg_mode_ctrl);
822 PHY_DEBUG(("[CAL] MODE_CTRL (write) = 0x%08X\n", reg_mode_ctrl));
824 // d. Set iqcal_mode[1:0] to 0x3 and set "calib_start" to 0x1 to
825 // enable "IQ alibration Mode II"
826 //hw_get_dxx_reg(phw_data, REG_MODE_CTRL, &val);
827 hw_get_dxx_reg(phw_data, REG_MODE_CTRL, ®_mode_ctrl);
828 reg_mode_ctrl &= ~MASK_IQCAL_MODE;
829 reg_mode_ctrl |= (MASK_CALIB_START|0x03);
830 hw_set_dxx_reg(phw_data, REG_MODE_CTRL, reg_mode_ctrl);
831 PHY_DEBUG(("[CAL] MODE_CTRL (write) = 0x%08X\n", reg_mode_ctrl));
834 hw_get_dxx_reg(phw_data, REG_CALIB_READ1, &val);
835 PHY_DEBUG(("[CAL] CALIB_READ1 = 0x%08X\n", val));
837 iqcal_tone_i = _s13_to_s32(val & 0x00001FFF);
838 iqcal_tone_q = _s13_to_s32((val & 0x03FFE000) >> 13);
839 PHY_DEBUG(("[CAL] ** iqcal_tone_i = %d, iqcal_tone_q = %d\n",
840 iqcal_tone_i, iqcal_tone_q));
841 if( capture_time == 0)
847 iqcal_tone_i_avg=( iqcal_tone_i_avg*(capture_time-1) +iqcal_tone_i)/capture_time;
848 iqcal_tone_q_avg=( iqcal_tone_q_avg*(capture_time-1) +iqcal_tone_q)/capture_time;
852 iqcal_tone_i = iqcal_tone_i_avg;
853 iqcal_tone_q = iqcal_tone_q_avg;
856 rot_i_b = (iqcal_tone_i * iqcal_tone_i0 +
857 iqcal_tone_q * iqcal_tone_q0) / 1024;
858 rot_q_b = (iqcal_tone_i * iqcal_tone_q0 * (-1) +
859 iqcal_tone_q * iqcal_tone_i0) / 1024;
860 PHY_DEBUG(("[CAL] ** rot_i_b = %d, rot_q_b = %d\n",
864 divisor = ((iq_mag_0_tx * iq_mag_0_tx * 2)/1024 - rot_i_b) * 2;
868 PHY_DEBUG(("[CAL] ** <_tx_iq_calibration_loop> ERROR *******\n"));
869 PHY_DEBUG(("[CAL] ** divisor=0 to calculate EPS and THETA !!\n"));
870 PHY_DEBUG(("[CAL] ******************************************\n"));
874 a_2 = (rot_i_b * 32768) / divisor;
875 b_2 = (rot_q_b * (-32768)) / divisor;
876 PHY_DEBUG(("[CAL] ***** EPSILON/2 = %d\n", a_2));
877 PHY_DEBUG(("[CAL] ***** THETA/2 = %d\n", b_2));
879 phw_data->iq_rsdl_gain_tx_d2 = a_2;
880 phw_data->iq_rsdl_phase_tx_d2 = b_2;
882 //if ((abs(a_2) < 150) && (abs(b_2) < 100))
883 //if ((abs(a_2) < 200) && (abs(b_2) < 200))
884 if ((abs(a_2) < a_2_threshold) && (abs(b_2) < b_2_threshold))
888 PHY_DEBUG(("[CAL] ** <_tx_iq_calibration_loop> *************\n"));
889 PHY_DEBUG(("[CAL] ** VERIFY OK # %d !!\n", verify_count));
890 PHY_DEBUG(("[CAL] ******************************************\n"));
892 if (verify_count > 2)
894 PHY_DEBUG(("[CAL] ** <_tx_iq_calibration_loop> *********\n"));
895 PHY_DEBUG(("[CAL] ** TX_IQ_CALIBRATION (EPS,THETA) OK !!\n"));
896 PHY_DEBUG(("[CAL] **************************************\n"));
907 _sin_cos(b_2, &sin_b, &cos_b);
908 _sin_cos(b_2*2, &sin_2b, &cos_2b);
909 PHY_DEBUG(("[CAL] ** sin(b/2)=%d, cos(b/2)=%d\n", sin_b, cos_b));
910 PHY_DEBUG(("[CAL] ** sin(b)=%d, cos(b)=%d\n", sin_2b, cos_2b));
914 PHY_DEBUG(("[CAL] ** <_tx_iq_calibration_loop> ERROR *******\n"));
915 PHY_DEBUG(("[CAL] ** cos(b)=0 !!\n"));
916 PHY_DEBUG(("[CAL] ******************************************\n"));
920 // 1280 * 32768 = 41943040
921 temp1 = (41943040/cos_2b)*cos_b;
923 //temp2 = (41943040/cos_2b)*sin_b*(-1);
924 if (phw_data->revision == 0x2002) // 1st-cut
926 temp2 = (41943040/cos_2b)*sin_b*(-1);
930 temp2 = (41943040*4/cos_2b)*sin_b*(-1);
933 tx_cal_flt_b[0] = _floor(temp1/(32768+a_2));
934 tx_cal_flt_b[1] = _floor(temp2/(32768+a_2));
935 tx_cal_flt_b[2] = _floor(temp2/(32768-a_2));
936 tx_cal_flt_b[3] = _floor(temp1/(32768-a_2));
937 PHY_DEBUG(("[CAL] ** tx_cal_flt_b[0] = %d\n", tx_cal_flt_b[0]));
938 PHY_DEBUG(("[CAL] tx_cal_flt_b[1] = %d\n", tx_cal_flt_b[1]));
939 PHY_DEBUG(("[CAL] tx_cal_flt_b[2] = %d\n", tx_cal_flt_b[2]));
940 PHY_DEBUG(("[CAL] tx_cal_flt_b[3] = %d\n", tx_cal_flt_b[3]));
942 tx_cal[2] = tx_cal_flt_b[2];
943 tx_cal[2] = tx_cal[2] +3;
944 tx_cal[1] = tx_cal[2];
945 tx_cal[3] = tx_cal_flt_b[3] - 128;
946 tx_cal[0] = -tx_cal[3]+1;
948 PHY_DEBUG(("[CAL] tx_cal[0] = %d\n", tx_cal[0]));
949 PHY_DEBUG(("[CAL] tx_cal[1] = %d\n", tx_cal[1]));
950 PHY_DEBUG(("[CAL] tx_cal[2] = %d\n", tx_cal[2]));
951 PHY_DEBUG(("[CAL] tx_cal[3] = %d\n", tx_cal[3]));
953 //if ((tx_cal[0] == 0) && (tx_cal[1] == 0) &&
954 // (tx_cal[2] == 0) && (tx_cal[3] == 0))
956 // PHY_DEBUG(("[CAL] ** <_tx_iq_calibration_loop> *************\n"));
957 // PHY_DEBUG(("[CAL] ** TX_IQ_CALIBRATION COMPLETE !!\n"));
958 // PHY_DEBUG(("[CAL] ******************************************\n"));
963 if (phw_data->revision == 0x2002) // 1st-cut
965 hw_get_dxx_reg(phw_data, 0x54, &val);
966 PHY_DEBUG(("[CAL] ** 0x54 = 0x%08X\n", val));
967 tx_cal_reg[0] = _s4_to_s32((val & 0xF0000000) >> 28);
968 tx_cal_reg[1] = _s4_to_s32((val & 0x0F000000) >> 24);
969 tx_cal_reg[2] = _s4_to_s32((val & 0x00F00000) >> 20);
970 tx_cal_reg[3] = _s4_to_s32((val & 0x000F0000) >> 16);
974 hw_get_dxx_reg(phw_data, 0x3C, &val);
975 PHY_DEBUG(("[CAL] ** 0x3C = 0x%08X\n", val));
976 tx_cal_reg[0] = _s5_to_s32((val & 0xF8000000) >> 27);
977 tx_cal_reg[1] = _s6_to_s32((val & 0x07E00000) >> 21);
978 tx_cal_reg[2] = _s6_to_s32((val & 0x001F8000) >> 15);
979 tx_cal_reg[3] = _s5_to_s32((val & 0x00007C00) >> 10);
983 PHY_DEBUG(("[CAL] ** tx_cal_reg[0] = %d\n", tx_cal_reg[0]));
984 PHY_DEBUG(("[CAL] tx_cal_reg[1] = %d\n", tx_cal_reg[1]));
985 PHY_DEBUG(("[CAL] tx_cal_reg[2] = %d\n", tx_cal_reg[2]));
986 PHY_DEBUG(("[CAL] tx_cal_reg[3] = %d\n", tx_cal_reg[3]));
988 if (phw_data->revision == 0x2002) // 1st-cut
990 if (((tx_cal_reg[0]==7) || (tx_cal_reg[0]==(-8))) &&
991 ((tx_cal_reg[3]==7) || (tx_cal_reg[3]==(-8))))
993 PHY_DEBUG(("[CAL] ** <_tx_iq_calibration_loop> *********\n"));
994 PHY_DEBUG(("[CAL] ** TX_IQ_CALIBRATION SATUATION !!\n"));
995 PHY_DEBUG(("[CAL] **************************************\n"));
1001 if (((tx_cal_reg[0]==31) || (tx_cal_reg[0]==(-32))) &&
1002 ((tx_cal_reg[3]==31) || (tx_cal_reg[3]==(-32))))
1004 PHY_DEBUG(("[CAL] ** <_tx_iq_calibration_loop> *********\n"));
1005 PHY_DEBUG(("[CAL] ** TX_IQ_CALIBRATION SATUATION !!\n"));
1006 PHY_DEBUG(("[CAL] **************************************\n"));
1011 tx_cal[0] = tx_cal[0] + tx_cal_reg[0];
1012 tx_cal[1] = tx_cal[1] + tx_cal_reg[1];
1013 tx_cal[2] = tx_cal[2] + tx_cal_reg[2];
1014 tx_cal[3] = tx_cal[3] + tx_cal_reg[3];
1015 PHY_DEBUG(("[CAL] ** apply tx_cal[0] = %d\n", tx_cal[0]));
1016 PHY_DEBUG(("[CAL] apply tx_cal[1] = %d\n", tx_cal[1]));
1017 PHY_DEBUG(("[CAL] apply tx_cal[2] = %d\n", tx_cal[2]));
1018 PHY_DEBUG(("[CAL] apply tx_cal[3] = %d\n", tx_cal[3]));
1020 if (phw_data->revision == 0x2002) // 1st-cut
1023 val |= ((_s32_to_s4(tx_cal[0]) << 28)|
1024 (_s32_to_s4(tx_cal[1]) << 24)|
1025 (_s32_to_s4(tx_cal[2]) << 20)|
1026 (_s32_to_s4(tx_cal[3]) << 16));
1027 hw_set_dxx_reg(phw_data, 0x54, val);
1028 PHY_DEBUG(("[CAL] ** CALIB_DATA = 0x%08X\n", val));
1034 val |= ((_s32_to_s5(tx_cal[0]) << 27)|
1035 (_s32_to_s6(tx_cal[1]) << 21)|
1036 (_s32_to_s6(tx_cal[2]) << 15)|
1037 (_s32_to_s5(tx_cal[3]) << 10));
1038 hw_set_dxx_reg(phw_data, 0x3C, val);
1039 PHY_DEBUG(("[CAL] ** TX_IQ_CALIBRATION = 0x%08X\n", val));
1043 // i. Set "calib_start" to 0x0
1044 reg_mode_ctrl &= ~MASK_CALIB_START;
1045 hw_set_dxx_reg(phw_data, REG_MODE_CTRL, reg_mode_ctrl);
1046 PHY_DEBUG(("[CAL] MODE_CTRL (write) = 0x%08X\n", reg_mode_ctrl));
1054 void _tx_iq_calibration_winbond(struct hw_data *phw_data)
1065 PHY_DEBUG(("[CAL] -> [4]_tx_iq_calibration()\n"));
1067 //0x01 0xEE3FC2 ; 3B8FF ; Calibration (6a). enable TX IQ calibration loop circuits
1068 phy_set_rf_data(phw_data, 1, (1<<24)|0xEE3FC2);
1069 //0x0B 0x1905D6 ; 06417 ; Calibration (6b). enable TX I/Q cal loop squaring circuit
1070 phy_set_rf_data(phw_data, 11, (11<<24)|0x19BDD6); // 20060612.1.a 0x1905D6);
1071 //0x05 0x24C60A ; 09318 ; Calibration (6c). setting TX-VGA gain: TXGCH=2 & GPK=110 --> to be optimized
1072 phy_set_rf_data(phw_data, 5, (5<<24)|0x24C60A); //0x24C60A (high temperature)
1073 //0x06 0x06880C ; 01A20 ; Calibration (6d). RXGCH=00; RXGCL=100 000 (RXVGA=32) --> to be optimized
1074 phy_set_rf_data(phw_data, 6, (6<<24)|0x34880C); // 20060612.1.a 0x06890C);
1075 //0x00 0xFDF1C0 ; 3F7C7 ; Calibration (6e). turn on IQ imbalance/Test mode
1076 phy_set_rf_data(phw_data, 0, (0<<24)|0xFDF1C0);
1077 //; [BB-chip]: Calibration (6f).Send test pattern
1078 //; [BB-chip]: Calibration (6g). Search RXGCL optimal value
1079 //; [BB-chip]: Calibration (6h). Caculate TX-path IQ imbalance and setting TX path IQ compensation table
1080 //phy_set_rf_data(phw_data, 3, (3<<24)|0x025586);
1082 msleep(30); // 20060612.1.a 30ms delay. Add the follow 2 lines
1083 //To adjust TXVGA to fit iq_mag_0 range from 1250 ~ 1750
1084 adjust_TXVGA_for_iq_mag( phw_data );
1087 hw_get_dxx_reg(phw_data, REG_AGC_CTRL3, ®_agc_ctrl3);
1088 reg_agc_ctrl3 &= ~BIT(2);
1089 reg_agc_ctrl3 |= (MASK_LNA_FIX_GAIN|MASK_AGC_FIX);
1090 hw_set_dxx_reg(phw_data, REG_AGC_CTRL3, reg_agc_ctrl3);
1092 hw_get_dxx_reg(phw_data, REG_AGC_CTRL5, &val);
1093 val |= MASK_AGC_FIX_GAIN;
1094 hw_set_dxx_reg(phw_data, REG_AGC_CTRL5, val);
1096 result = _tx_iq_calibration_loop_winbond(phw_data, 150, 100);
1100 if (phw_data->revision == 0x2002) // 1st-cut
1102 hw_get_dxx_reg(phw_data, 0x54, &val);
1104 hw_set_dxx_reg(phw_data, 0x54, val);
1108 hw_get_dxx_reg(phw_data, 0x3C, &val);
1110 hw_set_dxx_reg(phw_data, 0x3C, val);
1113 result = _tx_iq_calibration_loop_winbond(phw_data, 300, 200);
1117 if (phw_data->revision == 0x2002) // 1st-cut
1119 hw_get_dxx_reg(phw_data, 0x54, &val);
1121 hw_set_dxx_reg(phw_data, 0x54, val);
1125 hw_get_dxx_reg(phw_data, 0x3C, &val);
1127 hw_set_dxx_reg(phw_data, 0x3C, val);
1130 result = _tx_iq_calibration_loop_winbond(phw_data, 500, 400);
1133 if (phw_data->revision == 0x2002) // 1st-cut
1135 hw_get_dxx_reg(phw_data, 0x54, &val);
1137 hw_set_dxx_reg(phw_data, 0x54, val);
1141 hw_get_dxx_reg(phw_data, 0x3C, &val);
1143 hw_set_dxx_reg(phw_data, 0x3C, val);
1147 result = _tx_iq_calibration_loop_winbond(phw_data, 700, 500);
1151 PHY_DEBUG(("[CAL] ** <_tx_iq_calibration> **************\n"));
1152 PHY_DEBUG(("[CAL] ** TX_IQ_CALIBRATION FAILURE !!\n"));
1153 PHY_DEBUG(("[CAL] **************************************\n"));
1155 if (phw_data->revision == 0x2002) // 1st-cut
1157 hw_get_dxx_reg(phw_data, 0x54, &val);
1159 hw_set_dxx_reg(phw_data, 0x54, val);
1163 hw_get_dxx_reg(phw_data, 0x3C, &val);
1165 hw_set_dxx_reg(phw_data, 0x3C, val);
1172 // i. Set "calib_start" to 0x0
1173 hw_get_dxx_reg(phw_data, REG_MODE_CTRL, ®_mode_ctrl);
1174 reg_mode_ctrl &= ~MASK_CALIB_START;
1175 hw_set_dxx_reg(phw_data, REG_MODE_CTRL, reg_mode_ctrl);
1176 PHY_DEBUG(("[CAL] MODE_CTRL (write) = 0x%08X\n", reg_mode_ctrl));
1179 //hw_get_dxx_reg(phw_data, REG_AGC_CTRL3, &val);
1180 reg_agc_ctrl3 |= BIT(2);
1181 reg_agc_ctrl3 &= ~(MASK_LNA_FIX_GAIN|MASK_AGC_FIX);
1182 hw_set_dxx_reg(phw_data, REG_AGC_CTRL3, reg_agc_ctrl3);
1185 if (phw_data->revision == 0x2002) // 1st-cut
1187 hw_get_dxx_reg(phw_data, 0x54, &val);
1188 PHY_DEBUG(("[CAL] ** 0x54 = 0x%08X\n", val));
1189 tx_cal_reg[0] = _s4_to_s32((val & 0xF0000000) >> 28);
1190 tx_cal_reg[1] = _s4_to_s32((val & 0x0F000000) >> 24);
1191 tx_cal_reg[2] = _s4_to_s32((val & 0x00F00000) >> 20);
1192 tx_cal_reg[3] = _s4_to_s32((val & 0x000F0000) >> 16);
1196 hw_get_dxx_reg(phw_data, 0x3C, &val);
1197 PHY_DEBUG(("[CAL] ** 0x3C = 0x%08X\n", val));
1198 tx_cal_reg[0] = _s5_to_s32((val & 0xF8000000) >> 27);
1199 tx_cal_reg[1] = _s6_to_s32((val & 0x07E00000) >> 21);
1200 tx_cal_reg[2] = _s6_to_s32((val & 0x001F8000) >> 15);
1201 tx_cal_reg[3] = _s5_to_s32((val & 0x00007C00) >> 10);
1205 PHY_DEBUG(("[CAL] ** tx_cal_reg[0] = %d\n", tx_cal_reg[0]));
1206 PHY_DEBUG(("[CAL] tx_cal_reg[1] = %d\n", tx_cal_reg[1]));
1207 PHY_DEBUG(("[CAL] tx_cal_reg[2] = %d\n", tx_cal_reg[2]));
1208 PHY_DEBUG(("[CAL] tx_cal_reg[3] = %d\n", tx_cal_reg[3]));
1213 // RF Control Override
1216 /////////////////////////////////////////////////////////////////////////////////////////
1217 u8 _rx_iq_calibration_loop_winbond(struct hw_data *phw_data, u16 factor, u32 frequency)
1228 s32 rx_cal_flt_b[4];
1242 s32 iqcal_tone_i_avg,iqcal_tone_q_avg;
1243 s32 iqcal_image_i_avg,iqcal_image_q_avg;
1246 PHY_DEBUG(("[CAL] -> [5]_rx_iq_calibration_loop()\n"));
1247 PHY_DEBUG(("[CAL] ** factor = %d\n", factor));
1250 // RF Control Override
1251 hw_get_cxx_reg(phw_data, 0x80, &val);
1253 hw_set_cxx_reg(phw_data, 0x80, val);
1256 hw_get_cxx_reg(phw_data, 0xE4, &val);
1258 hw_set_cxx_reg(phw_data, 0xE4, val);
1259 PHY_DEBUG(("[CAL] ** RF_CTRL(0xE4) = 0x%08X", val));
1261 hw_set_dxx_reg(phw_data, 0x58, 0x44444444); // IQ_Alpha
1265 hw_get_dxx_reg(phw_data, REG_MODE_CTRL, ®_mode_ctrl);
1266 PHY_DEBUG(("[CAL] MODE_CTRL (read) = 0x%08X\n", reg_mode_ctrl));
1270 //for (loop = 0; loop < 1; loop++)
1271 //for (loop = 0; loop < LOOP_TIMES; loop++)
1275 PHY_DEBUG(("[CAL] [%d.] <_rx_iq_calibration_loop>\n", (LOOP_TIMES-loop+1)));
1278 iqcal_image_i_avg=0;
1279 iqcal_image_q_avg=0;
1282 for(capture_time=0; capture_time<10; capture_time++)
1284 // i. Set "calib_start" to 0x0
1285 reg_mode_ctrl &= ~MASK_CALIB_START;
1286 if( !hw_set_dxx_reg(phw_data, REG_MODE_CTRL, reg_mode_ctrl) )//20060718.1 modify
1288 PHY_DEBUG(("[CAL] MODE_CTRL (write) = 0x%08X\n", reg_mode_ctrl));
1290 reg_mode_ctrl &= ~MASK_IQCAL_MODE;
1291 reg_mode_ctrl |= (MASK_CALIB_START|0x1);
1292 hw_set_dxx_reg(phw_data, REG_MODE_CTRL, reg_mode_ctrl);
1293 PHY_DEBUG(("[CAL] MODE_CTRL (write) = 0x%08X\n", reg_mode_ctrl));
1296 hw_get_dxx_reg(phw_data, REG_CALIB_READ1, &val);
1297 PHY_DEBUG(("[CAL] CALIB_READ1 = 0x%08X\n", val));
1299 iqcal_tone_i = _s13_to_s32(val & 0x00001FFF);
1300 iqcal_tone_q = _s13_to_s32((val & 0x03FFE000) >> 13);
1301 PHY_DEBUG(("[CAL] ** iqcal_tone_i = %d, iqcal_tone_q = %d\n",
1302 iqcal_tone_i, iqcal_tone_q));
1304 hw_get_dxx_reg(phw_data, REG_CALIB_READ2, &val);
1305 PHY_DEBUG(("[CAL] CALIB_READ2 = 0x%08X\n", val));
1307 iqcal_image_i = _s13_to_s32(val & 0x00001FFF);
1308 iqcal_image_q = _s13_to_s32((val & 0x03FFE000) >> 13);
1309 PHY_DEBUG(("[CAL] ** iqcal_image_i = %d, iqcal_image_q = %d\n",
1310 iqcal_image_i, iqcal_image_q));
1311 if( capture_time == 0)
1317 iqcal_image_i_avg=( iqcal_image_i_avg*(capture_time-1) +iqcal_image_i)/capture_time;
1318 iqcal_image_q_avg=( iqcal_image_q_avg*(capture_time-1) +iqcal_image_q)/capture_time;
1319 iqcal_tone_i_avg=( iqcal_tone_i_avg*(capture_time-1) +iqcal_tone_i)/capture_time;
1320 iqcal_tone_q_avg=( iqcal_tone_q_avg*(capture_time-1) +iqcal_tone_q)/capture_time;
1325 iqcal_image_i = iqcal_image_i_avg;
1326 iqcal_image_q = iqcal_image_q_avg;
1327 iqcal_tone_i = iqcal_tone_i_avg;
1328 iqcal_tone_q = iqcal_tone_q_avg;
1331 rot_tone_i_b = (iqcal_tone_i * iqcal_tone_i +
1332 iqcal_tone_q * iqcal_tone_q) / 1024;
1333 rot_tone_q_b = (iqcal_tone_i * iqcal_tone_q * (-1) +
1334 iqcal_tone_q * iqcal_tone_i) / 1024;
1335 rot_image_i_b = (iqcal_image_i * iqcal_tone_i -
1336 iqcal_image_q * iqcal_tone_q) / 1024;
1337 rot_image_q_b = (iqcal_image_i * iqcal_tone_q +
1338 iqcal_image_q * iqcal_tone_i) / 1024;
1340 PHY_DEBUG(("[CAL] ** rot_tone_i_b = %d\n", rot_tone_i_b));
1341 PHY_DEBUG(("[CAL] ** rot_tone_q_b = %d\n", rot_tone_q_b));
1342 PHY_DEBUG(("[CAL] ** rot_image_i_b = %d\n", rot_image_i_b));
1343 PHY_DEBUG(("[CAL] ** rot_image_q_b = %d\n", rot_image_q_b));
1346 if (rot_tone_i_b == 0)
1348 PHY_DEBUG(("[CAL] ** <_rx_iq_calibration_loop> ERROR *******\n"));
1349 PHY_DEBUG(("[CAL] ** rot_tone_i_b=0 to calculate EPS and THETA !!\n"));
1350 PHY_DEBUG(("[CAL] ******************************************\n"));
1354 a_2 = (rot_image_i_b * 32768) / rot_tone_i_b -
1355 phw_data->iq_rsdl_gain_tx_d2;
1356 b_2 = (rot_image_q_b * 32768) / rot_tone_i_b -
1357 phw_data->iq_rsdl_phase_tx_d2;
1359 PHY_DEBUG(("[CAL] ** iq_rsdl_gain_tx_d2 = %d\n", phw_data->iq_rsdl_gain_tx_d2));
1360 PHY_DEBUG(("[CAL] ** iq_rsdl_phase_tx_d2= %d\n", phw_data->iq_rsdl_phase_tx_d2));
1361 PHY_DEBUG(("[CAL] ***** EPSILON/2 = %d\n", a_2));
1362 PHY_DEBUG(("[CAL] ***** THETA/2 = %d\n", b_2));
1364 _sin_cos(b_2, &sin_b, &cos_b);
1365 _sin_cos(b_2*2, &sin_2b, &cos_2b);
1366 PHY_DEBUG(("[CAL] ** sin(b/2)=%d, cos(b/2)=%d\n", sin_b, cos_b));
1367 PHY_DEBUG(("[CAL] ** sin(b)=%d, cos(b)=%d\n", sin_2b, cos_2b));
1371 PHY_DEBUG(("[CAL] ** <_rx_iq_calibration_loop> ERROR *******\n"));
1372 PHY_DEBUG(("[CAL] ** cos(b)=0 !!\n"));
1373 PHY_DEBUG(("[CAL] ******************************************\n"));
1377 // 1280 * 32768 = 41943040
1378 temp1 = (41943040/cos_2b)*cos_b;
1380 //temp2 = (41943040/cos_2b)*sin_b*(-1);
1381 if (phw_data->revision == 0x2002) // 1st-cut
1383 temp2 = (41943040/cos_2b)*sin_b*(-1);
1387 temp2 = (41943040*4/cos_2b)*sin_b*(-1);
1390 rx_cal_flt_b[0] = _floor(temp1/(32768+a_2));
1391 rx_cal_flt_b[1] = _floor(temp2/(32768-a_2));
1392 rx_cal_flt_b[2] = _floor(temp2/(32768+a_2));
1393 rx_cal_flt_b[3] = _floor(temp1/(32768-a_2));
1395 PHY_DEBUG(("[CAL] ** rx_cal_flt_b[0] = %d\n", rx_cal_flt_b[0]));
1396 PHY_DEBUG(("[CAL] rx_cal_flt_b[1] = %d\n", rx_cal_flt_b[1]));
1397 PHY_DEBUG(("[CAL] rx_cal_flt_b[2] = %d\n", rx_cal_flt_b[2]));
1398 PHY_DEBUG(("[CAL] rx_cal_flt_b[3] = %d\n", rx_cal_flt_b[3]));
1400 rx_cal[0] = rx_cal_flt_b[0] - 128;
1401 rx_cal[1] = rx_cal_flt_b[1];
1402 rx_cal[2] = rx_cal_flt_b[2];
1403 rx_cal[3] = rx_cal_flt_b[3] - 128;
1404 PHY_DEBUG(("[CAL] ** rx_cal[0] = %d\n", rx_cal[0]));
1405 PHY_DEBUG(("[CAL] rx_cal[1] = %d\n", rx_cal[1]));
1406 PHY_DEBUG(("[CAL] rx_cal[2] = %d\n", rx_cal[2]));
1407 PHY_DEBUG(("[CAL] rx_cal[3] = %d\n", rx_cal[3]));
1410 pwr_tone = (iqcal_tone_i*iqcal_tone_i + iqcal_tone_q*iqcal_tone_q);
1411 pwr_image = (iqcal_image_i*iqcal_image_i + iqcal_image_q*iqcal_image_q)*factor;
1413 PHY_DEBUG(("[CAL] ** pwr_tone = %d\n", pwr_tone));
1414 PHY_DEBUG(("[CAL] ** pwr_image = %d\n", pwr_image));
1416 if (pwr_tone > pwr_image)
1420 PHY_DEBUG(("[CAL] ** <_rx_iq_calibration_loop> *************\n"));
1421 PHY_DEBUG(("[CAL] ** VERIFY OK # %d !!\n", verify_count));
1422 PHY_DEBUG(("[CAL] ******************************************\n"));
1424 if (verify_count > 2)
1426 PHY_DEBUG(("[CAL] ** <_rx_iq_calibration_loop> *********\n"));
1427 PHY_DEBUG(("[CAL] ** RX_IQ_CALIBRATION OK !!\n"));
1428 PHY_DEBUG(("[CAL] **************************************\n"));
1435 hw_get_dxx_reg(phw_data, 0x54, &val);
1436 PHY_DEBUG(("[CAL] ** 0x54 = 0x%08X\n", val));
1438 if (phw_data->revision == 0x2002) // 1st-cut
1440 rx_cal_reg[0] = _s4_to_s32((val & 0x0000F000) >> 12);
1441 rx_cal_reg[1] = _s4_to_s32((val & 0x00000F00) >> 8);
1442 rx_cal_reg[2] = _s4_to_s32((val & 0x000000F0) >> 4);
1443 rx_cal_reg[3] = _s4_to_s32((val & 0x0000000F));
1447 rx_cal_reg[0] = _s5_to_s32((val & 0xF8000000) >> 27);
1448 rx_cal_reg[1] = _s6_to_s32((val & 0x07E00000) >> 21);
1449 rx_cal_reg[2] = _s6_to_s32((val & 0x001F8000) >> 15);
1450 rx_cal_reg[3] = _s5_to_s32((val & 0x00007C00) >> 10);
1453 PHY_DEBUG(("[CAL] ** rx_cal_reg[0] = %d\n", rx_cal_reg[0]));
1454 PHY_DEBUG(("[CAL] rx_cal_reg[1] = %d\n", rx_cal_reg[1]));
1455 PHY_DEBUG(("[CAL] rx_cal_reg[2] = %d\n", rx_cal_reg[2]));
1456 PHY_DEBUG(("[CAL] rx_cal_reg[3] = %d\n", rx_cal_reg[3]));
1458 if (phw_data->revision == 0x2002) // 1st-cut
1460 if (((rx_cal_reg[0]==7) || (rx_cal_reg[0]==(-8))) &&
1461 ((rx_cal_reg[3]==7) || (rx_cal_reg[3]==(-8))))
1463 PHY_DEBUG(("[CAL] ** <_rx_iq_calibration_loop> *********\n"));
1464 PHY_DEBUG(("[CAL] ** RX_IQ_CALIBRATION SATUATION !!\n"));
1465 PHY_DEBUG(("[CAL] **************************************\n"));
1471 if (((rx_cal_reg[0]==31) || (rx_cal_reg[0]==(-32))) &&
1472 ((rx_cal_reg[3]==31) || (rx_cal_reg[3]==(-32))))
1474 PHY_DEBUG(("[CAL] ** <_rx_iq_calibration_loop> *********\n"));
1475 PHY_DEBUG(("[CAL] ** RX_IQ_CALIBRATION SATUATION !!\n"));
1476 PHY_DEBUG(("[CAL] **************************************\n"));
1481 rx_cal[0] = rx_cal[0] + rx_cal_reg[0];
1482 rx_cal[1] = rx_cal[1] + rx_cal_reg[1];
1483 rx_cal[2] = rx_cal[2] + rx_cal_reg[2];
1484 rx_cal[3] = rx_cal[3] + rx_cal_reg[3];
1485 PHY_DEBUG(("[CAL] ** apply rx_cal[0] = %d\n", rx_cal[0]));
1486 PHY_DEBUG(("[CAL] apply rx_cal[1] = %d\n", rx_cal[1]));
1487 PHY_DEBUG(("[CAL] apply rx_cal[2] = %d\n", rx_cal[2]));
1488 PHY_DEBUG(("[CAL] apply rx_cal[3] = %d\n", rx_cal[3]));
1490 hw_get_dxx_reg(phw_data, 0x54, &val);
1491 if (phw_data->revision == 0x2002) // 1st-cut
1494 val |= ((_s32_to_s4(rx_cal[0]) << 12)|
1495 (_s32_to_s4(rx_cal[1]) << 8)|
1496 (_s32_to_s4(rx_cal[2]) << 4)|
1497 (_s32_to_s4(rx_cal[3])));
1498 hw_set_dxx_reg(phw_data, 0x54, val);
1503 val |= ((_s32_to_s5(rx_cal[0]) << 27)|
1504 (_s32_to_s6(rx_cal[1]) << 21)|
1505 (_s32_to_s6(rx_cal[2]) << 15)|
1506 (_s32_to_s5(rx_cal[3]) << 10));
1507 hw_set_dxx_reg(phw_data, 0x54, val);
1512 PHY_DEBUG(("[CAL] ** CALIB_DATA = 0x%08X\n", val));
1520 //////////////////////////////////////////////////////////
1522 //////////////////////////////////////////////////////////////////////////
1523 void _rx_iq_calibration_winbond(struct hw_data *phw_data, u32 frequency)
1525 // figo 20050523 marked thsi flag for can't compile for relesase
1533 PHY_DEBUG(("[CAL] -> [5]_rx_iq_calibration()\n"));
1534 // a. Set RFIC to "RX calibration mode"
1535 //; ----- Calibration (7). RX path IQ imbalance calibration loop
1536 // 0x01 0xFFBFC2 ; 3FEFF ; Calibration (7a). enable RX IQ calibration loop circuits
1537 phy_set_rf_data(phw_data, 1, (1<<24)|0xEFBFC2);
1538 // 0x0B 0x1A01D6 ; 06817 ; Calibration (7b). enable RX I/Q cal loop SW1 circuit
1539 phy_set_rf_data(phw_data, 11, (11<<24)|0x1A05D6);
1540 //0x05 0x24848A ; 09212 ; Calibration (7c). setting TX-VGA gain (TXGCH) to 2 --> to be optimized
1541 phy_set_rf_data(phw_data, 5, (5<<24)| phw_data->txvga_setting_for_cal);
1542 //0x06 0x06840C ; 01A10 ; Calibration (7d). RXGCH=00; RXGCL=010 000 (RXVGA) --> to be optimized
1543 phy_set_rf_data(phw_data, 6, (6<<24)|0x06834C);
1544 //0x00 0xFFF1C0 ; 3F7C7 ; Calibration (7e). turn on IQ imbalance/Test mode
1545 phy_set_rf_data(phw_data, 0, (0<<24)|0xFFF1C0);
1547 // ; [BB-chip]: Calibration (7f). Send test pattern
1548 // ; [BB-chip]: Calibration (7g). Search RXGCL optimal value
1549 // ; [BB-chip]: Calibration (7h). Caculate RX-path IQ imbalance and setting RX path IQ compensation table
1551 result = _rx_iq_calibration_loop_winbond(phw_data, 12589, frequency);
1555 _reset_rx_cal(phw_data);
1556 result = _rx_iq_calibration_loop_winbond(phw_data, 7943, frequency);
1560 _reset_rx_cal(phw_data);
1561 result = _rx_iq_calibration_loop_winbond(phw_data, 5011, frequency);
1565 PHY_DEBUG(("[CAL] ** <_rx_iq_calibration> **************\n"));
1566 PHY_DEBUG(("[CAL] ** RX_IQ_CALIBRATION FAILURE !!\n"));
1567 PHY_DEBUG(("[CAL] **************************************\n"));
1568 _reset_rx_cal(phw_data);
1574 hw_get_dxx_reg(phw_data, 0x54, &val);
1575 PHY_DEBUG(("[CAL] ** 0x54 = 0x%08X\n", val));
1577 if (phw_data->revision == 0x2002) // 1st-cut
1579 rx_cal_reg[0] = _s4_to_s32((val & 0x0000F000) >> 12);
1580 rx_cal_reg[1] = _s4_to_s32((val & 0x00000F00) >> 8);
1581 rx_cal_reg[2] = _s4_to_s32((val & 0x000000F0) >> 4);
1582 rx_cal_reg[3] = _s4_to_s32((val & 0x0000000F));
1586 rx_cal_reg[0] = _s5_to_s32((val & 0xF8000000) >> 27);
1587 rx_cal_reg[1] = _s6_to_s32((val & 0x07E00000) >> 21);
1588 rx_cal_reg[2] = _s6_to_s32((val & 0x001F8000) >> 15);
1589 rx_cal_reg[3] = _s5_to_s32((val & 0x00007C00) >> 10);
1592 PHY_DEBUG(("[CAL] ** rx_cal_reg[0] = %d\n", rx_cal_reg[0]));
1593 PHY_DEBUG(("[CAL] rx_cal_reg[1] = %d\n", rx_cal_reg[1]));
1594 PHY_DEBUG(("[CAL] rx_cal_reg[2] = %d\n", rx_cal_reg[2]));
1595 PHY_DEBUG(("[CAL] rx_cal_reg[3] = %d\n", rx_cal_reg[3]));
1600 ////////////////////////////////////////////////////////////////////////
1601 void phy_calibration_winbond(struct hw_data *phw_data, u32 frequency)
1606 PHY_DEBUG(("[CAL] -> phy_calibration_winbond()\n"));
1608 // 20040701 1.1.25.1000 kevin
1609 hw_get_cxx_reg(phw_data, 0x80, &mac_ctrl);
1610 hw_get_cxx_reg(phw_data, 0xE4, &rf_ctrl);
1611 hw_get_dxx_reg(phw_data, 0x58, &iq_alpha);
1615 _rxadc_dc_offset_cancellation_winbond(phw_data, frequency);
1616 //_txidac_dc_offset_cancellation_winbond(phw_data);
1617 //_txqdac_dc_offset_cacellation_winbond(phw_data);
1619 _tx_iq_calibration_winbond(phw_data);
1620 _rx_iq_calibration_winbond(phw_data, frequency);
1622 //------------------------------------------------------------------------
1623 hw_get_dxx_reg(phw_data, REG_MODE_CTRL, ®_mode_ctrl);
1624 reg_mode_ctrl &= ~(MASK_IQCAL_TONE_SEL|MASK_IQCAL_MODE|MASK_CALIB_START); // set when finish
1625 hw_set_dxx_reg(phw_data, REG_MODE_CTRL, reg_mode_ctrl);
1626 PHY_DEBUG(("[CAL] MODE_CTRL (write) = 0x%08X\n", reg_mode_ctrl));
1628 // i. Set RFIC to "Normal mode"
1629 hw_set_cxx_reg(phw_data, 0x80, mac_ctrl);
1630 hw_set_cxx_reg(phw_data, 0xE4, rf_ctrl);
1631 hw_set_dxx_reg(phw_data, 0x58, iq_alpha);
1634 //------------------------------------------------------------------------
1635 phy_init_rf(phw_data);
1639 //===========================
1640 void phy_set_rf_data( struct hw_data * pHwData, u32 index, u32 value )
1644 switch( pHwData->phy_type )
1647 case RF_MAXIM_V1: // 11g Winbond 2nd BB(with Phy board (v1) + Maxim 331)
1648 ltmp = (1 << 31) | (0 << 30) | (18 << 24) | BitReverse( value, 18 );
1652 ltmp = (1 << 31) | (0 << 30) | (18 << 24) | BitReverse( value, 18 );
1656 ltmp = (1 << 31) | (0 << 30) | (18 << 24) | BitReverse( value, 18 );
1660 ltmp = (1 << 31) | (0 << 30) | (18 << 24) | BitReverse( value, 18 );
1663 case RF_AIROHA_2230:
1664 case RF_AIROHA_2230S: // 20060420 Add this
1665 ltmp = (1 << 31) | (0 << 30) | (20 << 24) | BitReverse( value, 20 );
1668 case RF_AIROHA_7230:
1669 ltmp = (1 << 31) | (0 << 30) | (24 << 24) | (value&0xffffff);
1673 case RF_WB_242_1: // 20060619.5 Add
1674 ltmp = (1 << 31) | (0 << 30) | (24 << 24) | BitReverse( value, 24 );
1678 Wb35Reg_WriteSync( pHwData, 0x0864, ltmp );
1681 // 20060717 modify as Bruce's mail
1682 unsigned char adjust_TXVGA_for_iq_mag(struct hw_data *phw_data)
1696 for( init_txvga=0; init_txvga<10; init_txvga++)
1698 current_txvga = ( 0x24C40A|(init_txvga<<6) );
1699 phy_set_rf_data(phw_data, 5, ((5<<24)|current_txvga) );
1700 phw_data->txvga_setting_for_cal = current_txvga;
1702 msleep(30); // 20060612.1.a
1704 if( !hw_get_dxx_reg(phw_data, REG_MODE_CTRL, ®_mode_ctrl) ) // 20060718.1 modify
1707 PHY_DEBUG(("[CAL] MODE_CTRL (read) = 0x%08X\n", reg_mode_ctrl));
1709 // a. Set iqcal_mode[1:0] to 0x2 and set "calib_start" to 0x1 to
1710 // enable "IQ alibration Mode II"
1711 reg_mode_ctrl &= ~(MASK_IQCAL_TONE_SEL|MASK_IQCAL_MODE);
1712 reg_mode_ctrl &= ~MASK_IQCAL_MODE;
1713 reg_mode_ctrl |= (MASK_CALIB_START|0x02);
1714 reg_mode_ctrl |= (MASK_CALIB_START|0x02|2<<2);
1715 hw_set_dxx_reg(phw_data, REG_MODE_CTRL, reg_mode_ctrl);
1716 PHY_DEBUG(("[CAL] MODE_CTRL (write) = 0x%08X\n", reg_mode_ctrl));
1718 udelay(1); // 20060612.1.a
1720 udelay(300); // 20060612.1.a
1723 hw_get_dxx_reg(phw_data, REG_CALIB_READ1, &val);
1725 PHY_DEBUG(("[CAL] CALIB_READ1 = 0x%08X\n", val));
1726 udelay(300); // 20060612.1.a
1728 iqcal_tone_i0 = _s13_to_s32(val & 0x00001FFF);
1729 iqcal_tone_q0 = _s13_to_s32((val & 0x03FFE000) >> 13);
1730 PHY_DEBUG(("[CAL] ** iqcal_tone_i0=%d, iqcal_tone_q0=%d\n",
1731 iqcal_tone_i0, iqcal_tone_q0));
1733 sqsum = iqcal_tone_i0*iqcal_tone_i0 + iqcal_tone_q0*iqcal_tone_q0;
1734 iq_mag_0_tx = (s32) _sqrt(sqsum);
1735 PHY_DEBUG(("[CAL] ** auto_adjust_txvga_for_iq_mag_0_tx=%d\n", iq_mag_0_tx));
1737 if( iq_mag_0_tx>=700 && iq_mag_0_tx<=1750 )
1739 else if(iq_mag_0_tx > 1750)
1749 if( iq_mag_0_tx>=700 && iq_mag_0_tx<=1750 )