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 ***********************************/
13 #include "os_common.h"
14 #include "phy_calibration.h"
17 /****************** DEBUG CONSTANT AND MACRO SECTION ************************/
19 /****************** LOCAL CONSTANT AND MACRO SECTION ************************/
21 #define US 1000//MICROSECOND
23 #define AG_CONST 0.6072529350
24 #define FIXED(X) ((s32)((X) * 32768.0))
25 #define DEG2RAD(X) 0.017453 * (X)
27 /****************** LOCAL TYPE DEFINITION SECTION ***************************/
28 typedef s32 fixed; /* 16.16 fixed-point */
30 static const fixed Angles[]=
32 FIXED(DEG2RAD(45.0)), FIXED(DEG2RAD(26.565)), FIXED(DEG2RAD(14.0362)),
33 FIXED(DEG2RAD(7.12502)), FIXED(DEG2RAD(3.57633)), FIXED(DEG2RAD(1.78991)),
34 FIXED(DEG2RAD(0.895174)),FIXED(DEG2RAD(0.447614)),FIXED(DEG2RAD(0.223811)),
35 FIXED(DEG2RAD(0.111906)),FIXED(DEG2RAD(0.055953)),FIXED(DEG2RAD(0.027977))
38 /****************** LOCAL FUNCTION DECLARATION SECTION **********************/
39 //void _phy_rf_write_delay(hw_data_t *phw_data);
40 //void phy_init_rf(hw_data_t *phw_data);
42 /****************** FUNCTION DEFINITION SECTION *****************************/
44 s32 _s13_to_s32(u32 data)
48 val = (data & 0x0FFF);
50 if ((data & BIT(12)) != 0)
58 u32 _s32_to_s13(s32 data)
66 else if (data < -4096)
76 /****************************************************************************/
77 s32 _s4_to_s32(u32 data)
81 val = (data & 0x0007);
83 if ((data & BIT(3)) != 0)
91 u32 _s32_to_s4(s32 data)
109 /****************************************************************************/
110 s32 _s5_to_s32(u32 data)
114 val = (data & 0x000F);
116 if ((data & BIT(4)) != 0)
124 u32 _s32_to_s5(s32 data)
142 /****************************************************************************/
143 s32 _s6_to_s32(u32 data)
147 val = (data & 0x001F);
149 if ((data & BIT(5)) != 0)
157 u32 _s32_to_s6(s32 data)
175 /****************************************************************************/
176 s32 _s9_to_s32(u32 data)
182 if ((data & BIT(8)) != 0)
190 u32 _s32_to_s9(s32 data)
198 else if (data < -256)
208 /****************************************************************************/
223 /****************************************************************************/
224 // The following code is sqare-root function.
225 // sqsum is the input and the output is sq_rt;
226 // The maximum of sqsum = 2^27 -1;
231 int g0, g1, g2, g3, g4;
236 g4 = sqsum / 100000000;
237 g3 = (sqsum - g4*100000000) /1000000;
238 g2 = (sqsum - g4*100000000 - g3*1000000) /10000;
239 g1 = (sqsum - g4*100000000 - g3*1000000 - g2*10000) /100;
240 g0 = (sqsum - g4*100000000 - g3*1000000 - g2*10000 - g1*100);
245 while (((seed+1)*(step+1)) <= next)
251 sq_rt = seed * 10000;
252 next = (next-(seed*step))*100 + g3;
255 seed = 2 * seed * 10;
256 while (((seed+1)*(step+1)) <= next)
262 sq_rt = sq_rt + step * 1000;
263 next = (next - seed * step) * 100 + g2;
264 seed = (seed + step) * 10;
266 while (((seed+1)*(step+1)) <= next)
272 sq_rt = sq_rt + step * 100;
273 next = (next - seed * step) * 100 + g1;
274 seed = (seed + step) * 10;
277 while (((seed+1)*(step+1)) <= next)
283 sq_rt = sq_rt + step * 10;
284 next = (next - seed* step) * 100 + g0;
285 seed = (seed + step) * 10;
288 while (((seed+1)*(step+1)) <= next)
294 sq_rt = sq_rt + step;
299 /****************************************************************************/
300 void _sin_cos(s32 angle, s32 *sin, s32 *cos)
302 fixed X, Y, TargetAngle, CurrAngle;
305 X=FIXED(AG_CONST); // AG_CONST * cos(0)
306 Y=0; // AG_CONST * sin(0)
307 TargetAngle=abs(angle);
310 for (Step=0; Step < 12; Step++)
314 if(TargetAngle > CurrAngle)
316 NewX=X - (Y >> Step);
319 CurrAngle += Angles[Step];
323 NewX=X + (Y >> Step);
326 CurrAngle -= Angles[Step];
343 void _reset_rx_cal(hw_data_t *phw_data)
347 hw_get_dxx_reg(phw_data, 0x54, &val);
349 if (phw_data->revision == 0x2002) // 1st-cut
358 hw_set_dxx_reg(phw_data, 0x54, val);
362 // ************for winbond calibration*********
367 // *********************************************
368 void _rxadc_dc_offset_cancellation_winbond(hw_data_t *phw_data, u32 frequency)
375 PHY_DEBUG(("[CAL] -> [1]_rxadc_dc_offset_cancellation()\n"));
376 phy_init_rf(phw_data);
378 // set calibration channel
379 if( (RF_WB_242 == phw_data->phy_type) ||
380 (RF_WB_242_1 == phw_data->phy_type) ) // 20060619.5 Add
382 if ((frequency >= 2412) && (frequency <= 2484))
384 // w89rf242 change frequency to 2390Mhz
385 PHY_DEBUG(("[CAL] W89RF242/11G/Channel=2390Mhz\n"));
386 phy_set_rf_data(phw_data, 3, (3<<24)|0x025586);
395 // reset cancel_dc_i[9:5] and cancel_dc_q[4:0] in register DC_Cancel
396 hw_get_dxx_reg(phw_data, 0x5C, &val);
398 hw_set_dxx_reg(phw_data, 0x5C, val);
400 // reset the TX and RX IQ calibration data
401 hw_set_dxx_reg(phw_data, 0x3C, 0);
402 hw_set_dxx_reg(phw_data, 0x54, 0);
404 hw_set_dxx_reg(phw_data, 0x58, 0x30303030); // IQ_Alpha Changed
407 hw_get_dxx_reg(phw_data, REG_AGC_CTRL3, ®_agc_ctrl3);
408 reg_agc_ctrl3 &= ~BIT(2);
409 reg_agc_ctrl3 |= (MASK_LNA_FIX_GAIN|MASK_AGC_FIX);
410 hw_set_dxx_reg(phw_data, REG_AGC_CTRL3, reg_agc_ctrl3);
412 hw_get_dxx_reg(phw_data, REG_AGC_CTRL5, &val);
413 val |= MASK_AGC_FIX_GAIN;
414 hw_set_dxx_reg(phw_data, REG_AGC_CTRL5, val);
417 hw_get_dxx_reg(phw_data, REG_A_ACQ_CTRL, ®_a_acq_ctrl);
418 reg_a_acq_ctrl |= MASK_AMER_OFF_REG;
419 hw_set_dxx_reg(phw_data, REG_A_ACQ_CTRL, reg_a_acq_ctrl);
421 hw_get_dxx_reg(phw_data, REG_B_ACQ_CTRL, ®_b_acq_ctrl);
422 reg_b_acq_ctrl |= MASK_BMER_OFF_REG;
423 hw_set_dxx_reg(phw_data, REG_B_ACQ_CTRL, reg_b_acq_ctrl);
425 // c. Make sure MAC is in receiving mode
426 // d. Turn ON ADC calibration
427 // - ADC calibrator is triggered by this signal rising from 0 to 1
428 hw_get_dxx_reg(phw_data, REG_MODE_CTRL, &val);
429 val &= ~MASK_ADC_DC_CAL_STR;
430 hw_set_dxx_reg(phw_data, REG_MODE_CTRL, val);
432 val |= MASK_ADC_DC_CAL_STR;
433 hw_set_dxx_reg(phw_data, REG_MODE_CTRL, val);
435 // e. The result are shown in "adc_dc_cal_i[8:0] and adc_dc_cal_q[8:0]"
437 hw_get_dxx_reg(phw_data, REG_OFFSET_READ, &val);
438 PHY_DEBUG(("[CAL] REG_OFFSET_READ = 0x%08X\n", val));
440 PHY_DEBUG(("[CAL] ** adc_dc_cal_i = %d (0x%04X)\n",
441 _s9_to_s32(val&0x000001FF), val&0x000001FF));
442 PHY_DEBUG(("[CAL] ** adc_dc_cal_q = %d (0x%04X)\n",
443 _s9_to_s32((val&0x0003FE00)>>9), (val&0x0003FE00)>>9));
446 hw_get_dxx_reg(phw_data, REG_MODE_CTRL, &val);
447 val &= ~MASK_ADC_DC_CAL_STR;
448 hw_set_dxx_reg(phw_data, REG_MODE_CTRL, val);
451 //hw_get_dxx_reg(phw_data, REG_A_ACQ_CTRL, ®_a_acq_ctrl);
452 reg_a_acq_ctrl &= ~MASK_AMER_OFF_REG;
453 hw_set_dxx_reg(phw_data, REG_A_ACQ_CTRL, reg_a_acq_ctrl);
455 //hw_get_dxx_reg(phw_data, REG_B_ACQ_CTRL, ®_b_acq_ctrl);
456 reg_b_acq_ctrl &= ~MASK_BMER_OFF_REG;
457 hw_set_dxx_reg(phw_data, REG_B_ACQ_CTRL, reg_b_acq_ctrl);
460 //hw_get_dxx_reg(phw_data, REG_AGC_CTRL3, &val);
461 reg_agc_ctrl3 |= BIT(2);
462 reg_agc_ctrl3 &= ~(MASK_LNA_FIX_GAIN|MASK_AGC_FIX);
463 hw_set_dxx_reg(phw_data, REG_AGC_CTRL3, reg_agc_ctrl3);
466 ////////////////////////////////////////////////////////
467 void _txidac_dc_offset_cancellation_winbond(hw_data_t *phw_data)
477 s32 fix_cancel_dc_i = 0;
481 PHY_DEBUG(("[CAL] -> [2]_txidac_dc_offset_cancellation()\n"));
483 // a. Set to "TX calibration mode"
485 //0x01 0xEE3FC2 ; 3B8FF ; Calibration (6a). enable TX IQ calibration loop circuits
486 phy_set_rf_data(phw_data, 1, (1<<24)|0xEE3FC2);
487 //0x0B 0x1905D6 ; 06417 ; Calibration (6b). enable TX I/Q cal loop squaring circuit
488 phy_set_rf_data(phw_data, 11, (11<<24)|0x1901D6);
489 //0x05 0x24C60A ; 09318 ; Calibration (6c). setting TX-VGA gain: TXGCH=2 & GPK=110 --> to be optimized
490 phy_set_rf_data(phw_data, 5, (5<<24)|0x24C48A);
491 //0x06 0x06880C ; 01A20 ; Calibration (6d). RXGCH=00; RXGCL=100 000 (RXVGA=32) --> to be optimized
492 phy_set_rf_data(phw_data, 6, (6<<24)|0x06890C);
493 //0x00 0xFDF1C0 ; 3F7C7 ; Calibration (6e). turn on IQ imbalance/Test mode
494 phy_set_rf_data(phw_data, 0, (0<<24)|0xFDF1C0);
496 hw_set_dxx_reg(phw_data, 0x58, 0x30303030); // IQ_Alpha Changed
499 hw_get_dxx_reg(phw_data, REG_AGC_CTRL3, ®_agc_ctrl3);
500 reg_agc_ctrl3 &= ~BIT(2);
501 reg_agc_ctrl3 |= (MASK_LNA_FIX_GAIN|MASK_AGC_FIX);
502 hw_set_dxx_reg(phw_data, REG_AGC_CTRL3, reg_agc_ctrl3);
504 hw_get_dxx_reg(phw_data, REG_AGC_CTRL5, &val);
505 val |= MASK_AGC_FIX_GAIN;
506 hw_set_dxx_reg(phw_data, REG_AGC_CTRL5, val);
508 // b. set iqcal_mode[1:0] to 0x2 and set iqcal_tone[3:2] to 0
509 hw_get_dxx_reg(phw_data, REG_MODE_CTRL, ®_mode_ctrl);
511 PHY_DEBUG(("[CAL] MODE_CTRL (read) = 0x%08X\n", reg_mode_ctrl));
512 reg_mode_ctrl &= ~(MASK_IQCAL_TONE_SEL|MASK_IQCAL_MODE);
515 //reg_mode_ctrl |= (MASK_CALIB_START|2);
518 //reg_mode_ctrl |= (MASK_CALIB_START|2|(1<<2));
521 reg_mode_ctrl |= (MASK_CALIB_START|2|(2<<2));
522 hw_set_dxx_reg(phw_data, REG_MODE_CTRL, reg_mode_ctrl);
523 PHY_DEBUG(("[CAL] MODE_CTRL (write) = 0x%08X\n", reg_mode_ctrl));
525 hw_get_dxx_reg(phw_data, 0x5C, ®_dc_cancel);
526 PHY_DEBUG(("[CAL] DC_CANCEL (read) = 0x%08X\n", reg_dc_cancel));
528 for (loop = 0; loop < LOOP_TIMES; loop++)
530 PHY_DEBUG(("[CAL] [%d.] ==================================\n", loop));
533 // reset cancel_dc_i[9:5] and cancel_dc_q[4:0] in register DC_Cancel
534 reg_dc_cancel &= ~(0x03FF);
535 PHY_DEBUG(("[CAL] DC_CANCEL (write) = 0x%08X\n", reg_dc_cancel));
536 hw_set_dxx_reg(phw_data, 0x5C, reg_dc_cancel);
538 hw_get_dxx_reg(phw_data, REG_CALIB_READ2, &val);
539 PHY_DEBUG(("[CAL] CALIB_READ2 = 0x%08X\n", val));
541 iqcal_image_i = _s13_to_s32(val & 0x00001FFF);
542 iqcal_image_q = _s13_to_s32((val & 0x03FFE000) >> 13);
543 sqsum = iqcal_image_i*iqcal_image_i + iqcal_image_q*iqcal_image_q;
544 mag_0 = (s32) _sqrt(sqsum);
545 PHY_DEBUG(("[CAL] mag_0=%d (iqcal_image_i=%d, iqcal_image_q=%d)\n",
546 mag_0, iqcal_image_i, iqcal_image_q));
549 reg_dc_cancel |= (1 << CANCEL_DC_I_SHIFT);
550 PHY_DEBUG(("[CAL] DC_CANCEL (write) = 0x%08X\n", reg_dc_cancel));
551 hw_set_dxx_reg(phw_data, 0x5C, reg_dc_cancel);
553 hw_get_dxx_reg(phw_data, REG_CALIB_READ2, &val);
554 PHY_DEBUG(("[CAL] CALIB_READ2 = 0x%08X\n", val));
556 iqcal_image_i = _s13_to_s32(val & 0x00001FFF);
557 iqcal_image_q = _s13_to_s32((val & 0x03FFE000) >> 13);
558 sqsum = iqcal_image_i*iqcal_image_i + iqcal_image_q*iqcal_image_q;
559 mag_1 = (s32) _sqrt(sqsum);
560 PHY_DEBUG(("[CAL] mag_1=%d (iqcal_image_i=%d, iqcal_image_q=%d)\n",
561 mag_1, iqcal_image_i, iqcal_image_q));
563 // e. Calculate the correct DC offset cancellation value for I
566 fix_cancel_dc_i = (mag_0*10000) / (mag_0*10000 - mag_1*10000);
572 PHY_DEBUG(("[CAL] ***** mag_0 = mag_1 !!\n"));
578 PHY_DEBUG(("[CAL] ** fix_cancel_dc_i = %d (0x%04X)\n",
579 fix_cancel_dc_i, _s32_to_s5(fix_cancel_dc_i)));
581 if ((abs(mag_1-mag_0)*6) > mag_0)
590 reg_dc_cancel &= ~(0x03FF);
591 reg_dc_cancel |= (_s32_to_s5(fix_cancel_dc_i) << CANCEL_DC_I_SHIFT);
592 hw_set_dxx_reg(phw_data, 0x5C, reg_dc_cancel);
593 PHY_DEBUG(("[CAL] DC_CANCEL (write) = 0x%08X\n", reg_dc_cancel));
596 reg_mode_ctrl &= ~MASK_CALIB_START;
597 hw_set_dxx_reg(phw_data, REG_MODE_CTRL, reg_mode_ctrl);
598 PHY_DEBUG(("[CAL] MODE_CTRL (write) = 0x%08X\n", reg_mode_ctrl));
601 ///////////////////////////////////////////////////////
602 void _txqdac_dc_offset_cacellation_winbond(hw_data_t *phw_data)
612 s32 fix_cancel_dc_q = 0;
616 PHY_DEBUG(("[CAL] -> [3]_txqdac_dc_offset_cacellation()\n"));
617 //0x01 0xEE3FC2 ; 3B8FF ; Calibration (6a). enable TX IQ calibration loop circuits
618 phy_set_rf_data(phw_data, 1, (1<<24)|0xEE3FC2);
619 //0x0B 0x1905D6 ; 06417 ; Calibration (6b). enable TX I/Q cal loop squaring circuit
620 phy_set_rf_data(phw_data, 11, (11<<24)|0x1901D6);
621 //0x05 0x24C60A ; 09318 ; Calibration (6c). setting TX-VGA gain: TXGCH=2 & GPK=110 --> to be optimized
622 phy_set_rf_data(phw_data, 5, (5<<24)|0x24C48A);
623 //0x06 0x06880C ; 01A20 ; Calibration (6d). RXGCH=00; RXGCL=100 000 (RXVGA=32) --> to be optimized
624 phy_set_rf_data(phw_data, 6, (6<<24)|0x06890C);
625 //0x00 0xFDF1C0 ; 3F7C7 ; Calibration (6e). turn on IQ imbalance/Test mode
626 phy_set_rf_data(phw_data, 0, (0<<24)|0xFDF1C0);
628 hw_set_dxx_reg(phw_data, 0x58, 0x30303030); // IQ_Alpha Changed
631 hw_get_dxx_reg(phw_data, REG_AGC_CTRL3, ®_agc_ctrl3);
632 reg_agc_ctrl3 &= ~BIT(2);
633 reg_agc_ctrl3 |= (MASK_LNA_FIX_GAIN|MASK_AGC_FIX);
634 hw_set_dxx_reg(phw_data, REG_AGC_CTRL3, reg_agc_ctrl3);
636 hw_get_dxx_reg(phw_data, REG_AGC_CTRL5, &val);
637 val |= MASK_AGC_FIX_GAIN;
638 hw_set_dxx_reg(phw_data, REG_AGC_CTRL5, val);
640 // a. set iqcal_mode[1:0] to 0x3 and set iqcal_tone[3:2] to 0
641 hw_get_dxx_reg(phw_data, REG_MODE_CTRL, ®_mode_ctrl);
642 PHY_DEBUG(("[CAL] MODE_CTRL (read) = 0x%08X\n", reg_mode_ctrl));
644 //reg_mode_ctrl &= ~(MASK_IQCAL_TONE_SEL|MASK_IQCAL_MODE);
645 reg_mode_ctrl &= ~(MASK_IQCAL_MODE);
646 reg_mode_ctrl |= (MASK_CALIB_START|3);
647 hw_set_dxx_reg(phw_data, REG_MODE_CTRL, reg_mode_ctrl);
648 PHY_DEBUG(("[CAL] MODE_CTRL (write) = 0x%08X\n", reg_mode_ctrl));
650 hw_get_dxx_reg(phw_data, 0x5C, ®_dc_cancel);
651 PHY_DEBUG(("[CAL] DC_CANCEL (read) = 0x%08X\n", reg_dc_cancel));
653 for (loop = 0; loop < LOOP_TIMES; loop++)
655 PHY_DEBUG(("[CAL] [%d.] ==================================\n", loop));
658 // reset cancel_dc_q[4:0] in register DC_Cancel
659 reg_dc_cancel &= ~(0x001F);
660 PHY_DEBUG(("[CAL] DC_CANCEL (write) = 0x%08X\n", reg_dc_cancel));
661 hw_set_dxx_reg(phw_data, 0x5C, reg_dc_cancel);
663 hw_get_dxx_reg(phw_data, REG_CALIB_READ2, &val);
664 PHY_DEBUG(("[CAL] CALIB_READ2 = 0x%08X\n", val));
666 iqcal_image_i = _s13_to_s32(val & 0x00001FFF);
667 iqcal_image_q = _s13_to_s32((val & 0x03FFE000) >> 13);
668 sqsum = iqcal_image_i*iqcal_image_i + iqcal_image_q*iqcal_image_q;
669 mag_0 = _sqrt(sqsum);
670 PHY_DEBUG(("[CAL] mag_0=%d (iqcal_image_i=%d, iqcal_image_q=%d)\n",
671 mag_0, iqcal_image_i, iqcal_image_q));
674 reg_dc_cancel |= (1 << CANCEL_DC_Q_SHIFT);
675 PHY_DEBUG(("[CAL] DC_CANCEL (write) = 0x%08X\n", reg_dc_cancel));
676 hw_set_dxx_reg(phw_data, 0x5C, reg_dc_cancel);
678 hw_get_dxx_reg(phw_data, REG_CALIB_READ2, &val);
679 PHY_DEBUG(("[CAL] CALIB_READ2 = 0x%08X\n", val));
681 iqcal_image_i = _s13_to_s32(val & 0x00001FFF);
682 iqcal_image_q = _s13_to_s32((val & 0x03FFE000) >> 13);
683 sqsum = iqcal_image_i*iqcal_image_i + iqcal_image_q*iqcal_image_q;
684 mag_1 = _sqrt(sqsum);
685 PHY_DEBUG(("[CAL] mag_1=%d (iqcal_image_i=%d, iqcal_image_q=%d)\n",
686 mag_1, iqcal_image_i, iqcal_image_q));
688 // d. Calculate the correct DC offset cancellation value for I
691 fix_cancel_dc_q = (mag_0*10000) / (mag_0*10000 - mag_1*10000);
697 PHY_DEBUG(("[CAL] ***** mag_0 = mag_1 !!\n"));
703 PHY_DEBUG(("[CAL] ** fix_cancel_dc_q = %d (0x%04X)\n",
704 fix_cancel_dc_q, _s32_to_s5(fix_cancel_dc_q)));
706 if ((abs(mag_1-mag_0)*6) > mag_0)
715 reg_dc_cancel &= ~(0x001F);
716 reg_dc_cancel |= (_s32_to_s5(fix_cancel_dc_q) << CANCEL_DC_Q_SHIFT);
717 hw_set_dxx_reg(phw_data, 0x5C, reg_dc_cancel);
718 PHY_DEBUG(("[CAL] DC_CANCEL (write) = 0x%08X\n", reg_dc_cancel));
722 reg_mode_ctrl &= ~MASK_CALIB_START;
723 hw_set_dxx_reg(phw_data, REG_MODE_CTRL, reg_mode_ctrl);
724 PHY_DEBUG(("[CAL] MODE_CTRL (write) = 0x%08X\n", reg_mode_ctrl));
727 //20060612.1.a 20060718.1 Modify
728 u8 _tx_iq_calibration_loop_winbond(hw_data_t *phw_data,
751 s32 iqcal_tone_i_avg,iqcal_tone_q_avg;
755 PHY_DEBUG(("[CAL] -> _tx_iq_calibration_loop()\n"));
756 PHY_DEBUG(("[CAL] ** a_2_threshold = %d\n", a_2_threshold));
757 PHY_DEBUG(("[CAL] ** b_2_threshold = %d\n", b_2_threshold));
761 hw_get_dxx_reg(phw_data, REG_MODE_CTRL, ®_mode_ctrl);
762 PHY_DEBUG(("[CAL] MODE_CTRL (read) = 0x%08X\n", reg_mode_ctrl));
768 PHY_DEBUG(("[CAL] [%d.] <_tx_iq_calibration_loop>\n", (LOOP_TIMES-loop+1)));
772 if( !hw_set_dxx_reg(phw_data, 0x3C, 0x00) ) // 20060718.1 modify
774 for(capture_time=0;capture_time<10;capture_time++)
776 // a. Set iqcal_mode[1:0] to 0x2 and set "calib_start" to 0x1 to
777 // enable "IQ alibration Mode II"
778 reg_mode_ctrl &= ~(MASK_IQCAL_TONE_SEL|MASK_IQCAL_MODE);
779 reg_mode_ctrl &= ~MASK_IQCAL_MODE;
780 reg_mode_ctrl |= (MASK_CALIB_START|0x02);
781 reg_mode_ctrl |= (MASK_CALIB_START|0x02|2<<2);
782 hw_set_dxx_reg(phw_data, REG_MODE_CTRL, reg_mode_ctrl);
783 PHY_DEBUG(("[CAL] MODE_CTRL (write) = 0x%08X\n", reg_mode_ctrl));
786 hw_get_dxx_reg(phw_data, REG_CALIB_READ1, &val);
787 PHY_DEBUG(("[CAL] CALIB_READ1 = 0x%08X\n", val));
789 iqcal_tone_i0 = _s13_to_s32(val & 0x00001FFF);
790 iqcal_tone_q0 = _s13_to_s32((val & 0x03FFE000) >> 13);
791 PHY_DEBUG(("[CAL] ** iqcal_tone_i0=%d, iqcal_tone_q0=%d\n",
792 iqcal_tone_i0, iqcal_tone_q0));
794 sqsum = iqcal_tone_i0*iqcal_tone_i0 +
795 iqcal_tone_q0*iqcal_tone_q0;
796 iq_mag_0_tx = (s32) _sqrt(sqsum);
797 PHY_DEBUG(("[CAL] ** iq_mag_0_tx=%d\n", iq_mag_0_tx));
799 // c. Set "calib_start" to 0x0
800 reg_mode_ctrl &= ~MASK_CALIB_START;
801 hw_set_dxx_reg(phw_data, REG_MODE_CTRL, reg_mode_ctrl);
802 PHY_DEBUG(("[CAL] MODE_CTRL (write) = 0x%08X\n", reg_mode_ctrl));
804 // d. Set iqcal_mode[1:0] to 0x3 and set "calib_start" to 0x1 to
805 // enable "IQ alibration Mode II"
806 //hw_get_dxx_reg(phw_data, REG_MODE_CTRL, &val);
807 hw_get_dxx_reg(phw_data, REG_MODE_CTRL, ®_mode_ctrl);
808 reg_mode_ctrl &= ~MASK_IQCAL_MODE;
809 reg_mode_ctrl |= (MASK_CALIB_START|0x03);
810 hw_set_dxx_reg(phw_data, REG_MODE_CTRL, reg_mode_ctrl);
811 PHY_DEBUG(("[CAL] MODE_CTRL (write) = 0x%08X\n", reg_mode_ctrl));
814 hw_get_dxx_reg(phw_data, REG_CALIB_READ1, &val);
815 PHY_DEBUG(("[CAL] CALIB_READ1 = 0x%08X\n", val));
817 iqcal_tone_i = _s13_to_s32(val & 0x00001FFF);
818 iqcal_tone_q = _s13_to_s32((val & 0x03FFE000) >> 13);
819 PHY_DEBUG(("[CAL] ** iqcal_tone_i = %d, iqcal_tone_q = %d\n",
820 iqcal_tone_i, iqcal_tone_q));
821 if( capture_time == 0)
827 iqcal_tone_i_avg=( iqcal_tone_i_avg*(capture_time-1) +iqcal_tone_i)/capture_time;
828 iqcal_tone_q_avg=( iqcal_tone_q_avg*(capture_time-1) +iqcal_tone_q)/capture_time;
832 iqcal_tone_i = iqcal_tone_i_avg;
833 iqcal_tone_q = iqcal_tone_q_avg;
836 rot_i_b = (iqcal_tone_i * iqcal_tone_i0 +
837 iqcal_tone_q * iqcal_tone_q0) / 1024;
838 rot_q_b = (iqcal_tone_i * iqcal_tone_q0 * (-1) +
839 iqcal_tone_q * iqcal_tone_i0) / 1024;
840 PHY_DEBUG(("[CAL] ** rot_i_b = %d, rot_q_b = %d\n",
844 divisor = ((iq_mag_0_tx * iq_mag_0_tx * 2)/1024 - rot_i_b) * 2;
848 PHY_DEBUG(("[CAL] ** <_tx_iq_calibration_loop> ERROR *******\n"));
849 PHY_DEBUG(("[CAL] ** divisor=0 to calculate EPS and THETA !!\n"));
850 PHY_DEBUG(("[CAL] ******************************************\n"));
854 a_2 = (rot_i_b * 32768) / divisor;
855 b_2 = (rot_q_b * (-32768)) / divisor;
856 PHY_DEBUG(("[CAL] ***** EPSILON/2 = %d\n", a_2));
857 PHY_DEBUG(("[CAL] ***** THETA/2 = %d\n", b_2));
859 phw_data->iq_rsdl_gain_tx_d2 = a_2;
860 phw_data->iq_rsdl_phase_tx_d2 = b_2;
862 //if ((abs(a_2) < 150) && (abs(b_2) < 100))
863 //if ((abs(a_2) < 200) && (abs(b_2) < 200))
864 if ((abs(a_2) < a_2_threshold) && (abs(b_2) < b_2_threshold))
868 PHY_DEBUG(("[CAL] ** <_tx_iq_calibration_loop> *************\n"));
869 PHY_DEBUG(("[CAL] ** VERIFY OK # %d !!\n", verify_count));
870 PHY_DEBUG(("[CAL] ******************************************\n"));
872 if (verify_count > 2)
874 PHY_DEBUG(("[CAL] ** <_tx_iq_calibration_loop> *********\n"));
875 PHY_DEBUG(("[CAL] ** TX_IQ_CALIBRATION (EPS,THETA) OK !!\n"));
876 PHY_DEBUG(("[CAL] **************************************\n"));
887 _sin_cos(b_2, &sin_b, &cos_b);
888 _sin_cos(b_2*2, &sin_2b, &cos_2b);
889 PHY_DEBUG(("[CAL] ** sin(b/2)=%d, cos(b/2)=%d\n", sin_b, cos_b));
890 PHY_DEBUG(("[CAL] ** sin(b)=%d, cos(b)=%d\n", sin_2b, cos_2b));
894 PHY_DEBUG(("[CAL] ** <_tx_iq_calibration_loop> ERROR *******\n"));
895 PHY_DEBUG(("[CAL] ** cos(b)=0 !!\n"));
896 PHY_DEBUG(("[CAL] ******************************************\n"));
900 // 1280 * 32768 = 41943040
901 temp1 = (41943040/cos_2b)*cos_b;
903 //temp2 = (41943040/cos_2b)*sin_b*(-1);
904 if (phw_data->revision == 0x2002) // 1st-cut
906 temp2 = (41943040/cos_2b)*sin_b*(-1);
910 temp2 = (41943040*4/cos_2b)*sin_b*(-1);
913 tx_cal_flt_b[0] = _floor(temp1/(32768+a_2));
914 tx_cal_flt_b[1] = _floor(temp2/(32768+a_2));
915 tx_cal_flt_b[2] = _floor(temp2/(32768-a_2));
916 tx_cal_flt_b[3] = _floor(temp1/(32768-a_2));
917 PHY_DEBUG(("[CAL] ** tx_cal_flt_b[0] = %d\n", tx_cal_flt_b[0]));
918 PHY_DEBUG(("[CAL] tx_cal_flt_b[1] = %d\n", tx_cal_flt_b[1]));
919 PHY_DEBUG(("[CAL] tx_cal_flt_b[2] = %d\n", tx_cal_flt_b[2]));
920 PHY_DEBUG(("[CAL] tx_cal_flt_b[3] = %d\n", tx_cal_flt_b[3]));
922 tx_cal[2] = tx_cal_flt_b[2];
923 tx_cal[2] = tx_cal[2] +3;
924 tx_cal[1] = tx_cal[2];
925 tx_cal[3] = tx_cal_flt_b[3] - 128;
926 tx_cal[0] = -tx_cal[3]+1;
928 PHY_DEBUG(("[CAL] tx_cal[0] = %d\n", tx_cal[0]));
929 PHY_DEBUG(("[CAL] tx_cal[1] = %d\n", tx_cal[1]));
930 PHY_DEBUG(("[CAL] tx_cal[2] = %d\n", tx_cal[2]));
931 PHY_DEBUG(("[CAL] tx_cal[3] = %d\n", tx_cal[3]));
933 //if ((tx_cal[0] == 0) && (tx_cal[1] == 0) &&
934 // (tx_cal[2] == 0) && (tx_cal[3] == 0))
936 // PHY_DEBUG(("[CAL] ** <_tx_iq_calibration_loop> *************\n"));
937 // PHY_DEBUG(("[CAL] ** TX_IQ_CALIBRATION COMPLETE !!\n"));
938 // PHY_DEBUG(("[CAL] ******************************************\n"));
943 if (phw_data->revision == 0x2002) // 1st-cut
945 hw_get_dxx_reg(phw_data, 0x54, &val);
946 PHY_DEBUG(("[CAL] ** 0x54 = 0x%08X\n", val));
947 tx_cal_reg[0] = _s4_to_s32((val & 0xF0000000) >> 28);
948 tx_cal_reg[1] = _s4_to_s32((val & 0x0F000000) >> 24);
949 tx_cal_reg[2] = _s4_to_s32((val & 0x00F00000) >> 20);
950 tx_cal_reg[3] = _s4_to_s32((val & 0x000F0000) >> 16);
954 hw_get_dxx_reg(phw_data, 0x3C, &val);
955 PHY_DEBUG(("[CAL] ** 0x3C = 0x%08X\n", val));
956 tx_cal_reg[0] = _s5_to_s32((val & 0xF8000000) >> 27);
957 tx_cal_reg[1] = _s6_to_s32((val & 0x07E00000) >> 21);
958 tx_cal_reg[2] = _s6_to_s32((val & 0x001F8000) >> 15);
959 tx_cal_reg[3] = _s5_to_s32((val & 0x00007C00) >> 10);
963 PHY_DEBUG(("[CAL] ** tx_cal_reg[0] = %d\n", tx_cal_reg[0]));
964 PHY_DEBUG(("[CAL] tx_cal_reg[1] = %d\n", tx_cal_reg[1]));
965 PHY_DEBUG(("[CAL] tx_cal_reg[2] = %d\n", tx_cal_reg[2]));
966 PHY_DEBUG(("[CAL] tx_cal_reg[3] = %d\n", tx_cal_reg[3]));
968 if (phw_data->revision == 0x2002) // 1st-cut
970 if (((tx_cal_reg[0]==7) || (tx_cal_reg[0]==(-8))) &&
971 ((tx_cal_reg[3]==7) || (tx_cal_reg[3]==(-8))))
973 PHY_DEBUG(("[CAL] ** <_tx_iq_calibration_loop> *********\n"));
974 PHY_DEBUG(("[CAL] ** TX_IQ_CALIBRATION SATUATION !!\n"));
975 PHY_DEBUG(("[CAL] **************************************\n"));
981 if (((tx_cal_reg[0]==31) || (tx_cal_reg[0]==(-32))) &&
982 ((tx_cal_reg[3]==31) || (tx_cal_reg[3]==(-32))))
984 PHY_DEBUG(("[CAL] ** <_tx_iq_calibration_loop> *********\n"));
985 PHY_DEBUG(("[CAL] ** TX_IQ_CALIBRATION SATUATION !!\n"));
986 PHY_DEBUG(("[CAL] **************************************\n"));
991 tx_cal[0] = tx_cal[0] + tx_cal_reg[0];
992 tx_cal[1] = tx_cal[1] + tx_cal_reg[1];
993 tx_cal[2] = tx_cal[2] + tx_cal_reg[2];
994 tx_cal[3] = tx_cal[3] + tx_cal_reg[3];
995 PHY_DEBUG(("[CAL] ** apply tx_cal[0] = %d\n", tx_cal[0]));
996 PHY_DEBUG(("[CAL] apply tx_cal[1] = %d\n", tx_cal[1]));
997 PHY_DEBUG(("[CAL] apply tx_cal[2] = %d\n", tx_cal[2]));
998 PHY_DEBUG(("[CAL] apply tx_cal[3] = %d\n", tx_cal[3]));
1000 if (phw_data->revision == 0x2002) // 1st-cut
1003 val |= ((_s32_to_s4(tx_cal[0]) << 28)|
1004 (_s32_to_s4(tx_cal[1]) << 24)|
1005 (_s32_to_s4(tx_cal[2]) << 20)|
1006 (_s32_to_s4(tx_cal[3]) << 16));
1007 hw_set_dxx_reg(phw_data, 0x54, val);
1008 PHY_DEBUG(("[CAL] ** CALIB_DATA = 0x%08X\n", val));
1014 val |= ((_s32_to_s5(tx_cal[0]) << 27)|
1015 (_s32_to_s6(tx_cal[1]) << 21)|
1016 (_s32_to_s6(tx_cal[2]) << 15)|
1017 (_s32_to_s5(tx_cal[3]) << 10));
1018 hw_set_dxx_reg(phw_data, 0x3C, val);
1019 PHY_DEBUG(("[CAL] ** TX_IQ_CALIBRATION = 0x%08X\n", val));
1023 // i. Set "calib_start" to 0x0
1024 reg_mode_ctrl &= ~MASK_CALIB_START;
1025 hw_set_dxx_reg(phw_data, REG_MODE_CTRL, reg_mode_ctrl);
1026 PHY_DEBUG(("[CAL] MODE_CTRL (write) = 0x%08X\n", reg_mode_ctrl));
1034 void _tx_iq_calibration_winbond(hw_data_t *phw_data)
1045 PHY_DEBUG(("[CAL] -> [4]_tx_iq_calibration()\n"));
1047 //0x01 0xEE3FC2 ; 3B8FF ; Calibration (6a). enable TX IQ calibration loop circuits
1048 phy_set_rf_data(phw_data, 1, (1<<24)|0xEE3FC2);
1049 //0x0B 0x1905D6 ; 06417 ; Calibration (6b). enable TX I/Q cal loop squaring circuit
1050 phy_set_rf_data(phw_data, 11, (11<<24)|0x19BDD6); // 20060612.1.a 0x1905D6);
1051 //0x05 0x24C60A ; 09318 ; Calibration (6c). setting TX-VGA gain: TXGCH=2 & GPK=110 --> to be optimized
1052 phy_set_rf_data(phw_data, 5, (5<<24)|0x24C60A); //0x24C60A (high temperature)
1053 //0x06 0x06880C ; 01A20 ; Calibration (6d). RXGCH=00; RXGCL=100 000 (RXVGA=32) --> to be optimized
1054 phy_set_rf_data(phw_data, 6, (6<<24)|0x34880C); // 20060612.1.a 0x06890C);
1055 //0x00 0xFDF1C0 ; 3F7C7 ; Calibration (6e). turn on IQ imbalance/Test mode
1056 phy_set_rf_data(phw_data, 0, (0<<24)|0xFDF1C0);
1057 //; [BB-chip]: Calibration (6f).Send test pattern
1058 //; [BB-chip]: Calibration (6g). Search RXGCL optimal value
1059 //; [BB-chip]: Calibration (6h). Caculate TX-path IQ imbalance and setting TX path IQ compensation table
1060 //phy_set_rf_data(phw_data, 3, (3<<24)|0x025586);
1062 msleep(30); // 20060612.1.a 30ms delay. Add the follow 2 lines
1063 //To adjust TXVGA to fit iq_mag_0 range from 1250 ~ 1750
1064 adjust_TXVGA_for_iq_mag( phw_data );
1067 hw_get_dxx_reg(phw_data, REG_AGC_CTRL3, ®_agc_ctrl3);
1068 reg_agc_ctrl3 &= ~BIT(2);
1069 reg_agc_ctrl3 |= (MASK_LNA_FIX_GAIN|MASK_AGC_FIX);
1070 hw_set_dxx_reg(phw_data, REG_AGC_CTRL3, reg_agc_ctrl3);
1072 hw_get_dxx_reg(phw_data, REG_AGC_CTRL5, &val);
1073 val |= MASK_AGC_FIX_GAIN;
1074 hw_set_dxx_reg(phw_data, REG_AGC_CTRL5, val);
1076 result = _tx_iq_calibration_loop_winbond(phw_data, 150, 100);
1080 if (phw_data->revision == 0x2002) // 1st-cut
1082 hw_get_dxx_reg(phw_data, 0x54, &val);
1084 hw_set_dxx_reg(phw_data, 0x54, val);
1088 hw_get_dxx_reg(phw_data, 0x3C, &val);
1090 hw_set_dxx_reg(phw_data, 0x3C, val);
1093 result = _tx_iq_calibration_loop_winbond(phw_data, 300, 200);
1097 if (phw_data->revision == 0x2002) // 1st-cut
1099 hw_get_dxx_reg(phw_data, 0x54, &val);
1101 hw_set_dxx_reg(phw_data, 0x54, val);
1105 hw_get_dxx_reg(phw_data, 0x3C, &val);
1107 hw_set_dxx_reg(phw_data, 0x3C, val);
1110 result = _tx_iq_calibration_loop_winbond(phw_data, 500, 400);
1113 if (phw_data->revision == 0x2002) // 1st-cut
1115 hw_get_dxx_reg(phw_data, 0x54, &val);
1117 hw_set_dxx_reg(phw_data, 0x54, val);
1121 hw_get_dxx_reg(phw_data, 0x3C, &val);
1123 hw_set_dxx_reg(phw_data, 0x3C, val);
1127 result = _tx_iq_calibration_loop_winbond(phw_data, 700, 500);
1131 PHY_DEBUG(("[CAL] ** <_tx_iq_calibration> **************\n"));
1132 PHY_DEBUG(("[CAL] ** TX_IQ_CALIBRATION FAILURE !!\n"));
1133 PHY_DEBUG(("[CAL] **************************************\n"));
1135 if (phw_data->revision == 0x2002) // 1st-cut
1137 hw_get_dxx_reg(phw_data, 0x54, &val);
1139 hw_set_dxx_reg(phw_data, 0x54, val);
1143 hw_get_dxx_reg(phw_data, 0x3C, &val);
1145 hw_set_dxx_reg(phw_data, 0x3C, val);
1152 // i. Set "calib_start" to 0x0
1153 hw_get_dxx_reg(phw_data, REG_MODE_CTRL, ®_mode_ctrl);
1154 reg_mode_ctrl &= ~MASK_CALIB_START;
1155 hw_set_dxx_reg(phw_data, REG_MODE_CTRL, reg_mode_ctrl);
1156 PHY_DEBUG(("[CAL] MODE_CTRL (write) = 0x%08X\n", reg_mode_ctrl));
1159 //hw_get_dxx_reg(phw_data, REG_AGC_CTRL3, &val);
1160 reg_agc_ctrl3 |= BIT(2);
1161 reg_agc_ctrl3 &= ~(MASK_LNA_FIX_GAIN|MASK_AGC_FIX);
1162 hw_set_dxx_reg(phw_data, REG_AGC_CTRL3, reg_agc_ctrl3);
1165 if (phw_data->revision == 0x2002) // 1st-cut
1167 hw_get_dxx_reg(phw_data, 0x54, &val);
1168 PHY_DEBUG(("[CAL] ** 0x54 = 0x%08X\n", val));
1169 tx_cal_reg[0] = _s4_to_s32((val & 0xF0000000) >> 28);
1170 tx_cal_reg[1] = _s4_to_s32((val & 0x0F000000) >> 24);
1171 tx_cal_reg[2] = _s4_to_s32((val & 0x00F00000) >> 20);
1172 tx_cal_reg[3] = _s4_to_s32((val & 0x000F0000) >> 16);
1176 hw_get_dxx_reg(phw_data, 0x3C, &val);
1177 PHY_DEBUG(("[CAL] ** 0x3C = 0x%08X\n", val));
1178 tx_cal_reg[0] = _s5_to_s32((val & 0xF8000000) >> 27);
1179 tx_cal_reg[1] = _s6_to_s32((val & 0x07E00000) >> 21);
1180 tx_cal_reg[2] = _s6_to_s32((val & 0x001F8000) >> 15);
1181 tx_cal_reg[3] = _s5_to_s32((val & 0x00007C00) >> 10);
1185 PHY_DEBUG(("[CAL] ** tx_cal_reg[0] = %d\n", tx_cal_reg[0]));
1186 PHY_DEBUG(("[CAL] tx_cal_reg[1] = %d\n", tx_cal_reg[1]));
1187 PHY_DEBUG(("[CAL] tx_cal_reg[2] = %d\n", tx_cal_reg[2]));
1188 PHY_DEBUG(("[CAL] tx_cal_reg[3] = %d\n", tx_cal_reg[3]));
1193 // RF Control Override
1196 /////////////////////////////////////////////////////////////////////////////////////////
1197 u8 _rx_iq_calibration_loop_winbond(hw_data_t *phw_data, u16 factor, u32 frequency)
1208 s32 rx_cal_flt_b[4];
1222 s32 iqcal_tone_i_avg,iqcal_tone_q_avg;
1223 s32 iqcal_image_i_avg,iqcal_image_q_avg;
1226 PHY_DEBUG(("[CAL] -> [5]_rx_iq_calibration_loop()\n"));
1227 PHY_DEBUG(("[CAL] ** factor = %d\n", factor));
1230 // RF Control Override
1231 hw_get_cxx_reg(phw_data, 0x80, &val);
1233 hw_set_cxx_reg(phw_data, 0x80, val);
1236 hw_get_cxx_reg(phw_data, 0xE4, &val);
1238 hw_set_cxx_reg(phw_data, 0xE4, val);
1239 PHY_DEBUG(("[CAL] ** RF_CTRL(0xE4) = 0x%08X", val));
1241 hw_set_dxx_reg(phw_data, 0x58, 0x44444444); // IQ_Alpha
1245 hw_get_dxx_reg(phw_data, REG_MODE_CTRL, ®_mode_ctrl);
1246 PHY_DEBUG(("[CAL] MODE_CTRL (read) = 0x%08X\n", reg_mode_ctrl));
1250 //for (loop = 0; loop < 1; loop++)
1251 //for (loop = 0; loop < LOOP_TIMES; loop++)
1255 PHY_DEBUG(("[CAL] [%d.] <_rx_iq_calibration_loop>\n", (LOOP_TIMES-loop+1)));
1258 iqcal_image_i_avg=0;
1259 iqcal_image_q_avg=0;
1262 for(capture_time=0; capture_time<10; capture_time++)
1264 // i. Set "calib_start" to 0x0
1265 reg_mode_ctrl &= ~MASK_CALIB_START;
1266 if( !hw_set_dxx_reg(phw_data, REG_MODE_CTRL, reg_mode_ctrl) )//20060718.1 modify
1268 PHY_DEBUG(("[CAL] MODE_CTRL (write) = 0x%08X\n", reg_mode_ctrl));
1270 reg_mode_ctrl &= ~MASK_IQCAL_MODE;
1271 reg_mode_ctrl |= (MASK_CALIB_START|0x1);
1272 hw_set_dxx_reg(phw_data, REG_MODE_CTRL, reg_mode_ctrl);
1273 PHY_DEBUG(("[CAL] MODE_CTRL (write) = 0x%08X\n", reg_mode_ctrl));
1276 hw_get_dxx_reg(phw_data, REG_CALIB_READ1, &val);
1277 PHY_DEBUG(("[CAL] CALIB_READ1 = 0x%08X\n", val));
1279 iqcal_tone_i = _s13_to_s32(val & 0x00001FFF);
1280 iqcal_tone_q = _s13_to_s32((val & 0x03FFE000) >> 13);
1281 PHY_DEBUG(("[CAL] ** iqcal_tone_i = %d, iqcal_tone_q = %d\n",
1282 iqcal_tone_i, iqcal_tone_q));
1284 hw_get_dxx_reg(phw_data, REG_CALIB_READ2, &val);
1285 PHY_DEBUG(("[CAL] CALIB_READ2 = 0x%08X\n", val));
1287 iqcal_image_i = _s13_to_s32(val & 0x00001FFF);
1288 iqcal_image_q = _s13_to_s32((val & 0x03FFE000) >> 13);
1289 PHY_DEBUG(("[CAL] ** iqcal_image_i = %d, iqcal_image_q = %d\n",
1290 iqcal_image_i, iqcal_image_q));
1291 if( capture_time == 0)
1297 iqcal_image_i_avg=( iqcal_image_i_avg*(capture_time-1) +iqcal_image_i)/capture_time;
1298 iqcal_image_q_avg=( iqcal_image_q_avg*(capture_time-1) +iqcal_image_q)/capture_time;
1299 iqcal_tone_i_avg=( iqcal_tone_i_avg*(capture_time-1) +iqcal_tone_i)/capture_time;
1300 iqcal_tone_q_avg=( iqcal_tone_q_avg*(capture_time-1) +iqcal_tone_q)/capture_time;
1305 iqcal_image_i = iqcal_image_i_avg;
1306 iqcal_image_q = iqcal_image_q_avg;
1307 iqcal_tone_i = iqcal_tone_i_avg;
1308 iqcal_tone_q = iqcal_tone_q_avg;
1311 rot_tone_i_b = (iqcal_tone_i * iqcal_tone_i +
1312 iqcal_tone_q * iqcal_tone_q) / 1024;
1313 rot_tone_q_b = (iqcal_tone_i * iqcal_tone_q * (-1) +
1314 iqcal_tone_q * iqcal_tone_i) / 1024;
1315 rot_image_i_b = (iqcal_image_i * iqcal_tone_i -
1316 iqcal_image_q * iqcal_tone_q) / 1024;
1317 rot_image_q_b = (iqcal_image_i * iqcal_tone_q +
1318 iqcal_image_q * iqcal_tone_i) / 1024;
1320 PHY_DEBUG(("[CAL] ** rot_tone_i_b = %d\n", rot_tone_i_b));
1321 PHY_DEBUG(("[CAL] ** rot_tone_q_b = %d\n", rot_tone_q_b));
1322 PHY_DEBUG(("[CAL] ** rot_image_i_b = %d\n", rot_image_i_b));
1323 PHY_DEBUG(("[CAL] ** rot_image_q_b = %d\n", rot_image_q_b));
1326 if (rot_tone_i_b == 0)
1328 PHY_DEBUG(("[CAL] ** <_rx_iq_calibration_loop> ERROR *******\n"));
1329 PHY_DEBUG(("[CAL] ** rot_tone_i_b=0 to calculate EPS and THETA !!\n"));
1330 PHY_DEBUG(("[CAL] ******************************************\n"));
1334 a_2 = (rot_image_i_b * 32768) / rot_tone_i_b -
1335 phw_data->iq_rsdl_gain_tx_d2;
1336 b_2 = (rot_image_q_b * 32768) / rot_tone_i_b -
1337 phw_data->iq_rsdl_phase_tx_d2;
1339 PHY_DEBUG(("[CAL] ** iq_rsdl_gain_tx_d2 = %d\n", phw_data->iq_rsdl_gain_tx_d2));
1340 PHY_DEBUG(("[CAL] ** iq_rsdl_phase_tx_d2= %d\n", phw_data->iq_rsdl_phase_tx_d2));
1341 PHY_DEBUG(("[CAL] ***** EPSILON/2 = %d\n", a_2));
1342 PHY_DEBUG(("[CAL] ***** THETA/2 = %d\n", b_2));
1344 _sin_cos(b_2, &sin_b, &cos_b);
1345 _sin_cos(b_2*2, &sin_2b, &cos_2b);
1346 PHY_DEBUG(("[CAL] ** sin(b/2)=%d, cos(b/2)=%d\n", sin_b, cos_b));
1347 PHY_DEBUG(("[CAL] ** sin(b)=%d, cos(b)=%d\n", sin_2b, cos_2b));
1351 PHY_DEBUG(("[CAL] ** <_rx_iq_calibration_loop> ERROR *******\n"));
1352 PHY_DEBUG(("[CAL] ** cos(b)=0 !!\n"));
1353 PHY_DEBUG(("[CAL] ******************************************\n"));
1357 // 1280 * 32768 = 41943040
1358 temp1 = (41943040/cos_2b)*cos_b;
1360 //temp2 = (41943040/cos_2b)*sin_b*(-1);
1361 if (phw_data->revision == 0x2002) // 1st-cut
1363 temp2 = (41943040/cos_2b)*sin_b*(-1);
1367 temp2 = (41943040*4/cos_2b)*sin_b*(-1);
1370 rx_cal_flt_b[0] = _floor(temp1/(32768+a_2));
1371 rx_cal_flt_b[1] = _floor(temp2/(32768-a_2));
1372 rx_cal_flt_b[2] = _floor(temp2/(32768+a_2));
1373 rx_cal_flt_b[3] = _floor(temp1/(32768-a_2));
1375 PHY_DEBUG(("[CAL] ** rx_cal_flt_b[0] = %d\n", rx_cal_flt_b[0]));
1376 PHY_DEBUG(("[CAL] rx_cal_flt_b[1] = %d\n", rx_cal_flt_b[1]));
1377 PHY_DEBUG(("[CAL] rx_cal_flt_b[2] = %d\n", rx_cal_flt_b[2]));
1378 PHY_DEBUG(("[CAL] rx_cal_flt_b[3] = %d\n", rx_cal_flt_b[3]));
1380 rx_cal[0] = rx_cal_flt_b[0] - 128;
1381 rx_cal[1] = rx_cal_flt_b[1];
1382 rx_cal[2] = rx_cal_flt_b[2];
1383 rx_cal[3] = rx_cal_flt_b[3] - 128;
1384 PHY_DEBUG(("[CAL] ** rx_cal[0] = %d\n", rx_cal[0]));
1385 PHY_DEBUG(("[CAL] rx_cal[1] = %d\n", rx_cal[1]));
1386 PHY_DEBUG(("[CAL] rx_cal[2] = %d\n", rx_cal[2]));
1387 PHY_DEBUG(("[CAL] rx_cal[3] = %d\n", rx_cal[3]));
1390 pwr_tone = (iqcal_tone_i*iqcal_tone_i + iqcal_tone_q*iqcal_tone_q);
1391 pwr_image = (iqcal_image_i*iqcal_image_i + iqcal_image_q*iqcal_image_q)*factor;
1393 PHY_DEBUG(("[CAL] ** pwr_tone = %d\n", pwr_tone));
1394 PHY_DEBUG(("[CAL] ** pwr_image = %d\n", pwr_image));
1396 if (pwr_tone > pwr_image)
1400 PHY_DEBUG(("[CAL] ** <_rx_iq_calibration_loop> *************\n"));
1401 PHY_DEBUG(("[CAL] ** VERIFY OK # %d !!\n", verify_count));
1402 PHY_DEBUG(("[CAL] ******************************************\n"));
1404 if (verify_count > 2)
1406 PHY_DEBUG(("[CAL] ** <_rx_iq_calibration_loop> *********\n"));
1407 PHY_DEBUG(("[CAL] ** RX_IQ_CALIBRATION OK !!\n"));
1408 PHY_DEBUG(("[CAL] **************************************\n"));
1415 hw_get_dxx_reg(phw_data, 0x54, &val);
1416 PHY_DEBUG(("[CAL] ** 0x54 = 0x%08X\n", val));
1418 if (phw_data->revision == 0x2002) // 1st-cut
1420 rx_cal_reg[0] = _s4_to_s32((val & 0x0000F000) >> 12);
1421 rx_cal_reg[1] = _s4_to_s32((val & 0x00000F00) >> 8);
1422 rx_cal_reg[2] = _s4_to_s32((val & 0x000000F0) >> 4);
1423 rx_cal_reg[3] = _s4_to_s32((val & 0x0000000F));
1427 rx_cal_reg[0] = _s5_to_s32((val & 0xF8000000) >> 27);
1428 rx_cal_reg[1] = _s6_to_s32((val & 0x07E00000) >> 21);
1429 rx_cal_reg[2] = _s6_to_s32((val & 0x001F8000) >> 15);
1430 rx_cal_reg[3] = _s5_to_s32((val & 0x00007C00) >> 10);
1433 PHY_DEBUG(("[CAL] ** rx_cal_reg[0] = %d\n", rx_cal_reg[0]));
1434 PHY_DEBUG(("[CAL] rx_cal_reg[1] = %d\n", rx_cal_reg[1]));
1435 PHY_DEBUG(("[CAL] rx_cal_reg[2] = %d\n", rx_cal_reg[2]));
1436 PHY_DEBUG(("[CAL] rx_cal_reg[3] = %d\n", rx_cal_reg[3]));
1438 if (phw_data->revision == 0x2002) // 1st-cut
1440 if (((rx_cal_reg[0]==7) || (rx_cal_reg[0]==(-8))) &&
1441 ((rx_cal_reg[3]==7) || (rx_cal_reg[3]==(-8))))
1443 PHY_DEBUG(("[CAL] ** <_rx_iq_calibration_loop> *********\n"));
1444 PHY_DEBUG(("[CAL] ** RX_IQ_CALIBRATION SATUATION !!\n"));
1445 PHY_DEBUG(("[CAL] **************************************\n"));
1451 if (((rx_cal_reg[0]==31) || (rx_cal_reg[0]==(-32))) &&
1452 ((rx_cal_reg[3]==31) || (rx_cal_reg[3]==(-32))))
1454 PHY_DEBUG(("[CAL] ** <_rx_iq_calibration_loop> *********\n"));
1455 PHY_DEBUG(("[CAL] ** RX_IQ_CALIBRATION SATUATION !!\n"));
1456 PHY_DEBUG(("[CAL] **************************************\n"));
1461 rx_cal[0] = rx_cal[0] + rx_cal_reg[0];
1462 rx_cal[1] = rx_cal[1] + rx_cal_reg[1];
1463 rx_cal[2] = rx_cal[2] + rx_cal_reg[2];
1464 rx_cal[3] = rx_cal[3] + rx_cal_reg[3];
1465 PHY_DEBUG(("[CAL] ** apply rx_cal[0] = %d\n", rx_cal[0]));
1466 PHY_DEBUG(("[CAL] apply rx_cal[1] = %d\n", rx_cal[1]));
1467 PHY_DEBUG(("[CAL] apply rx_cal[2] = %d\n", rx_cal[2]));
1468 PHY_DEBUG(("[CAL] apply rx_cal[3] = %d\n", rx_cal[3]));
1470 hw_get_dxx_reg(phw_data, 0x54, &val);
1471 if (phw_data->revision == 0x2002) // 1st-cut
1474 val |= ((_s32_to_s4(rx_cal[0]) << 12)|
1475 (_s32_to_s4(rx_cal[1]) << 8)|
1476 (_s32_to_s4(rx_cal[2]) << 4)|
1477 (_s32_to_s4(rx_cal[3])));
1478 hw_set_dxx_reg(phw_data, 0x54, val);
1483 val |= ((_s32_to_s5(rx_cal[0]) << 27)|
1484 (_s32_to_s6(rx_cal[1]) << 21)|
1485 (_s32_to_s6(rx_cal[2]) << 15)|
1486 (_s32_to_s5(rx_cal[3]) << 10));
1487 hw_set_dxx_reg(phw_data, 0x54, val);
1492 PHY_DEBUG(("[CAL] ** CALIB_DATA = 0x%08X\n", val));
1500 //////////////////////////////////////////////////////////
1502 //////////////////////////////////////////////////////////////////////////
1503 void _rx_iq_calibration_winbond(hw_data_t *phw_data, u32 frequency)
1505 // figo 20050523 marked thsi flag for can't compile for relesase
1513 PHY_DEBUG(("[CAL] -> [5]_rx_iq_calibration()\n"));
1514 // a. Set RFIC to "RX calibration mode"
1515 //; ----- Calibration (7). RX path IQ imbalance calibration loop
1516 // 0x01 0xFFBFC2 ; 3FEFF ; Calibration (7a). enable RX IQ calibration loop circuits
1517 phy_set_rf_data(phw_data, 1, (1<<24)|0xEFBFC2);
1518 // 0x0B 0x1A01D6 ; 06817 ; Calibration (7b). enable RX I/Q cal loop SW1 circuit
1519 phy_set_rf_data(phw_data, 11, (11<<24)|0x1A05D6);
1520 //0x05 0x24848A ; 09212 ; Calibration (7c). setting TX-VGA gain (TXGCH) to 2 --> to be optimized
1521 phy_set_rf_data(phw_data, 5, (5<<24)| phw_data->txvga_setting_for_cal);
1522 //0x06 0x06840C ; 01A10 ; Calibration (7d). RXGCH=00; RXGCL=010 000 (RXVGA) --> to be optimized
1523 phy_set_rf_data(phw_data, 6, (6<<24)|0x06834C);
1524 //0x00 0xFFF1C0 ; 3F7C7 ; Calibration (7e). turn on IQ imbalance/Test mode
1525 phy_set_rf_data(phw_data, 0, (0<<24)|0xFFF1C0);
1527 // ; [BB-chip]: Calibration (7f). Send test pattern
1528 // ; [BB-chip]: Calibration (7g). Search RXGCL optimal value
1529 // ; [BB-chip]: Calibration (7h). Caculate RX-path IQ imbalance and setting RX path IQ compensation table
1531 result = _rx_iq_calibration_loop_winbond(phw_data, 12589, frequency);
1535 _reset_rx_cal(phw_data);
1536 result = _rx_iq_calibration_loop_winbond(phw_data, 7943, frequency);
1540 _reset_rx_cal(phw_data);
1541 result = _rx_iq_calibration_loop_winbond(phw_data, 5011, frequency);
1545 PHY_DEBUG(("[CAL] ** <_rx_iq_calibration> **************\n"));
1546 PHY_DEBUG(("[CAL] ** RX_IQ_CALIBRATION FAILURE !!\n"));
1547 PHY_DEBUG(("[CAL] **************************************\n"));
1548 _reset_rx_cal(phw_data);
1554 hw_get_dxx_reg(phw_data, 0x54, &val);
1555 PHY_DEBUG(("[CAL] ** 0x54 = 0x%08X\n", val));
1557 if (phw_data->revision == 0x2002) // 1st-cut
1559 rx_cal_reg[0] = _s4_to_s32((val & 0x0000F000) >> 12);
1560 rx_cal_reg[1] = _s4_to_s32((val & 0x00000F00) >> 8);
1561 rx_cal_reg[2] = _s4_to_s32((val & 0x000000F0) >> 4);
1562 rx_cal_reg[3] = _s4_to_s32((val & 0x0000000F));
1566 rx_cal_reg[0] = _s5_to_s32((val & 0xF8000000) >> 27);
1567 rx_cal_reg[1] = _s6_to_s32((val & 0x07E00000) >> 21);
1568 rx_cal_reg[2] = _s6_to_s32((val & 0x001F8000) >> 15);
1569 rx_cal_reg[3] = _s5_to_s32((val & 0x00007C00) >> 10);
1572 PHY_DEBUG(("[CAL] ** rx_cal_reg[0] = %d\n", rx_cal_reg[0]));
1573 PHY_DEBUG(("[CAL] rx_cal_reg[1] = %d\n", rx_cal_reg[1]));
1574 PHY_DEBUG(("[CAL] rx_cal_reg[2] = %d\n", rx_cal_reg[2]));
1575 PHY_DEBUG(("[CAL] rx_cal_reg[3] = %d\n", rx_cal_reg[3]));
1580 ////////////////////////////////////////////////////////////////////////
1581 void phy_calibration_winbond(hw_data_t *phw_data, u32 frequency)
1586 PHY_DEBUG(("[CAL] -> phy_calibration_winbond()\n"));
1588 // 20040701 1.1.25.1000 kevin
1589 hw_get_cxx_reg(phw_data, 0x80, &mac_ctrl);
1590 hw_get_cxx_reg(phw_data, 0xE4, &rf_ctrl);
1591 hw_get_dxx_reg(phw_data, 0x58, &iq_alpha);
1595 _rxadc_dc_offset_cancellation_winbond(phw_data, frequency);
1596 //_txidac_dc_offset_cancellation_winbond(phw_data);
1597 //_txqdac_dc_offset_cacellation_winbond(phw_data);
1599 _tx_iq_calibration_winbond(phw_data);
1600 _rx_iq_calibration_winbond(phw_data, frequency);
1602 //------------------------------------------------------------------------
1603 hw_get_dxx_reg(phw_data, REG_MODE_CTRL, ®_mode_ctrl);
1604 reg_mode_ctrl &= ~(MASK_IQCAL_TONE_SEL|MASK_IQCAL_MODE|MASK_CALIB_START); // set when finish
1605 hw_set_dxx_reg(phw_data, REG_MODE_CTRL, reg_mode_ctrl);
1606 PHY_DEBUG(("[CAL] MODE_CTRL (write) = 0x%08X\n", reg_mode_ctrl));
1608 // i. Set RFIC to "Normal mode"
1609 hw_set_cxx_reg(phw_data, 0x80, mac_ctrl);
1610 hw_set_cxx_reg(phw_data, 0xE4, rf_ctrl);
1611 hw_set_dxx_reg(phw_data, 0x58, iq_alpha);
1614 //------------------------------------------------------------------------
1615 phy_init_rf(phw_data);
1619 //===========================
1620 void phy_set_rf_data( phw_data_t pHwData, u32 index, u32 value )
1624 switch( pHwData->phy_type )
1627 case RF_MAXIM_V1: // 11g Winbond 2nd BB(with Phy board (v1) + Maxim 331)
1628 ltmp = (1 << 31) | (0 << 30) | (18 << 24) | BitReverse( value, 18 );
1632 ltmp = (1 << 31) | (0 << 30) | (18 << 24) | BitReverse( value, 18 );
1636 ltmp = (1 << 31) | (0 << 30) | (18 << 24) | BitReverse( value, 18 );
1640 ltmp = (1 << 31) | (0 << 30) | (18 << 24) | BitReverse( value, 18 );
1643 case RF_AIROHA_2230:
1644 case RF_AIROHA_2230S: // 20060420 Add this
1645 ltmp = (1 << 31) | (0 << 30) | (20 << 24) | BitReverse( value, 20 );
1648 case RF_AIROHA_7230:
1649 ltmp = (1 << 31) | (0 << 30) | (24 << 24) | (value&0xffffff);
1653 case RF_WB_242_1: // 20060619.5 Add
1654 ltmp = (1 << 31) | (0 << 30) | (24 << 24) | BitReverse( value, 24 );
1658 Wb35Reg_WriteSync( pHwData, 0x0864, ltmp );
1661 // 20060717 modify as Bruce's mail
1662 unsigned char adjust_TXVGA_for_iq_mag(hw_data_t *phw_data)
1676 for( init_txvga=0; init_txvga<10; init_txvga++)
1678 current_txvga = ( 0x24C40A|(init_txvga<<6) );
1679 phy_set_rf_data(phw_data, 5, ((5<<24)|current_txvga) );
1680 phw_data->txvga_setting_for_cal = current_txvga;
1682 msleep(30); // 20060612.1.a
1684 if( !hw_get_dxx_reg(phw_data, REG_MODE_CTRL, ®_mode_ctrl) ) // 20060718.1 modify
1687 PHY_DEBUG(("[CAL] MODE_CTRL (read) = 0x%08X\n", reg_mode_ctrl));
1689 // a. Set iqcal_mode[1:0] to 0x2 and set "calib_start" to 0x1 to
1690 // enable "IQ alibration Mode II"
1691 reg_mode_ctrl &= ~(MASK_IQCAL_TONE_SEL|MASK_IQCAL_MODE);
1692 reg_mode_ctrl &= ~MASK_IQCAL_MODE;
1693 reg_mode_ctrl |= (MASK_CALIB_START|0x02);
1694 reg_mode_ctrl |= (MASK_CALIB_START|0x02|2<<2);
1695 hw_set_dxx_reg(phw_data, REG_MODE_CTRL, reg_mode_ctrl);
1696 PHY_DEBUG(("[CAL] MODE_CTRL (write) = 0x%08X\n", reg_mode_ctrl));
1698 udelay(1); // 20060612.1.a
1700 udelay(300); // 20060612.1.a
1703 hw_get_dxx_reg(phw_data, REG_CALIB_READ1, &val);
1705 PHY_DEBUG(("[CAL] CALIB_READ1 = 0x%08X\n", val));
1706 udelay(300); // 20060612.1.a
1708 iqcal_tone_i0 = _s13_to_s32(val & 0x00001FFF);
1709 iqcal_tone_q0 = _s13_to_s32((val & 0x03FFE000) >> 13);
1710 PHY_DEBUG(("[CAL] ** iqcal_tone_i0=%d, iqcal_tone_q0=%d\n",
1711 iqcal_tone_i0, iqcal_tone_q0));
1713 sqsum = iqcal_tone_i0*iqcal_tone_i0 + iqcal_tone_q0*iqcal_tone_q0;
1714 iq_mag_0_tx = (s32) _sqrt(sqsum);
1715 PHY_DEBUG(("[CAL] ** auto_adjust_txvga_for_iq_mag_0_tx=%d\n", iq_mag_0_tx));
1717 if( iq_mag_0_tx>=700 && iq_mag_0_tx<=1750 )
1719 else if(iq_mag_0_tx > 1750)
1729 if( iq_mag_0_tx>=700 && iq_mag_0_tx<=1750 )