mm-only debug patch...
[mmotm.git] / drivers / staging / winbond / phy_calibration.c
blob8c56962ab80cab20a03ebbdfd6d836cdb357c40e
1 /*
2 * phy_302_calibration.c
4 * Copyright (C) 2002, 2005 Winbond Electronics Corp.
6 * modification history
7 * ---------------------------------------------------------------------------
8 * 0.01.001, 2003-04-16, Kevin created
12 /****************** INCLUDE FILES SECTION ***********************************/
13 #include "sysdef.h"
14 #include "phy_calibration.h"
15 #include "wbhal_f.h"
18 /****************** DEBUG CONSTANT AND MACRO SECTION ************************/
20 /****************** LOCAL CONSTANT AND MACRO SECTION ************************/
21 #define LOOP_TIMES 20
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)
47 u32 val;
49 val = (data & 0x0FFF);
51 if ((data & BIT(12)) != 0)
53 val |= 0xFFFFF000;
56 return ((s32) val);
59 u32 _s32_to_s13(s32 data)
61 u32 val;
63 if (data > 4095)
65 data = 4095;
67 else if (data < -4096)
69 data = -4096;
72 val = data & 0x1FFF;
74 return val;
77 /****************************************************************************/
78 s32 _s4_to_s32(u32 data)
80 s32 val;
82 val = (data & 0x0007);
84 if ((data & BIT(3)) != 0)
86 val |= 0xFFFFFFF8;
89 return val;
92 u32 _s32_to_s4(s32 data)
94 u32 val;
96 if (data > 7)
98 data = 7;
100 else if (data < -8)
102 data = -8;
105 val = data & 0x000F;
107 return val;
110 /****************************************************************************/
111 s32 _s5_to_s32(u32 data)
113 s32 val;
115 val = (data & 0x000F);
117 if ((data & BIT(4)) != 0)
119 val |= 0xFFFFFFF0;
122 return val;
125 u32 _s32_to_s5(s32 data)
127 u32 val;
129 if (data > 15)
131 data = 15;
133 else if (data < -16)
135 data = -16;
138 val = data & 0x001F;
140 return val;
143 /****************************************************************************/
144 s32 _s6_to_s32(u32 data)
146 s32 val;
148 val = (data & 0x001F);
150 if ((data & BIT(5)) != 0)
152 val |= 0xFFFFFFE0;
155 return val;
158 u32 _s32_to_s6(s32 data)
160 u32 val;
162 if (data > 31)
164 data = 31;
166 else if (data < -32)
168 data = -32;
171 val = data & 0x003F;
173 return val;
176 /****************************************************************************/
177 s32 _s9_to_s32(u32 data)
179 s32 val;
181 val = data & 0x00FF;
183 if ((data & BIT(8)) != 0)
185 val |= 0xFFFFFF00;
188 return val;
191 u32 _s32_to_s9(s32 data)
193 u32 val;
195 if (data > 255)
197 data = 255;
199 else if (data < -256)
201 data = -256;
204 val = data & 0x01FF;
206 return val;
209 /****************************************************************************/
210 s32 _floor(s32 n)
212 if (n > 0)
214 n += 5;
216 else
218 n -= 5;
221 return (n/10);
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;
228 u32 _sqrt(u32 sqsum)
230 u32 sq_rt;
232 int g0, g1, g2, g3, g4;
233 int seed;
234 int next;
235 int step;
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);
243 next = g4;
244 step = 0;
245 seed = 0;
246 while (((seed+1)*(step+1)) <= next)
248 step++;
249 seed++;
252 sq_rt = seed * 10000;
253 next = (next-(seed*step))*100 + g3;
255 step = 0;
256 seed = 2 * seed * 10;
257 while (((seed+1)*(step+1)) <= next)
259 step++;
260 seed++;
263 sq_rt = sq_rt + step * 1000;
264 next = (next - seed * step) * 100 + g2;
265 seed = (seed + step) * 10;
266 step = 0;
267 while (((seed+1)*(step+1)) <= next)
269 step++;
270 seed++;
273 sq_rt = sq_rt + step * 100;
274 next = (next - seed * step) * 100 + g1;
275 seed = (seed + step) * 10;
276 step = 0;
278 while (((seed+1)*(step+1)) <= next)
280 step++;
281 seed++;
284 sq_rt = sq_rt + step * 10;
285 next = (next - seed* step) * 100 + g0;
286 seed = (seed + step) * 10;
287 step = 0;
289 while (((seed+1)*(step+1)) <= next)
291 step++;
292 seed++;
295 sq_rt = sq_rt + step;
297 return sq_rt;
300 /****************************************************************************/
301 void _sin_cos(s32 angle, s32 *sin, s32 *cos)
303 fixed X, Y, TargetAngle, CurrAngle;
304 unsigned Step;
306 X=FIXED(AG_CONST); // AG_CONST * cos(0)
307 Y=0; // AG_CONST * sin(0)
308 TargetAngle=abs(angle);
309 CurrAngle=0;
311 for (Step=0; Step < 12; Step++)
313 fixed NewX;
315 if(TargetAngle > CurrAngle)
317 NewX=X - (Y >> Step);
318 Y=(X >> Step) + Y;
319 X=NewX;
320 CurrAngle += Angles[Step];
322 else
324 NewX=X + (Y >> Step);
325 Y=-(X >> Step) + Y;
326 X=NewX;
327 CurrAngle -= Angles[Step];
331 if (angle > 0)
333 *cos = X;
334 *sin = Y;
336 else
338 *cos = X;
339 *sin = -Y;
343 static unsigned char hal_get_dxx_reg(struct hw_data *pHwData, u16 number, u32 * pValue)
345 if (number < 0x1000)
346 number += 0x1000;
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)
353 unsigned char ret;
355 if (number < 0x1000)
356 number += 0x1000;
357 ret = Wb35Reg_WriteSync(pHwData, number, value);
358 return ret;
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)
365 u32 val;
367 hw_get_dxx_reg(phw_data, 0x54, &val);
369 if (phw_data->revision == 0x2002) // 1st-cut
371 val &= 0xFFFF0000;
373 else // 2nd-cut
375 val &= 0x000003FF;
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)
390 u32 reg_agc_ctrl3;
391 u32 reg_a_acq_ctrl;
392 u32 reg_b_acq_ctrl;
393 u32 val;
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);
410 else
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);
417 val &= ~(0x03FF);
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
426 // a. Disable AGC
427 hw_get_dxx_reg(phw_data, REG_AGC_CTRL3, &reg_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);
436 // b. Turn off BB RX
437 hw_get_dxx_reg(phw_data, REG_A_ACQ_CTRL, &reg_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, &reg_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]"
456 #ifdef _DEBUG
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));
464 #endif
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);
470 // f. Turn on BB RX
471 //hw_get_dxx_reg(phw_data, REG_A_ACQ_CTRL, &reg_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, &reg_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);
479 // g. Enable AGC
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)
489 u32 reg_agc_ctrl3;
490 u32 reg_mode_ctrl;
491 u32 reg_dc_cancel;
492 s32 iqcal_image_i;
493 s32 iqcal_image_q;
494 u32 sqsum;
495 s32 mag_0;
496 s32 mag_1;
497 s32 fix_cancel_dc_i = 0;
498 u32 val;
499 int loop;
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
518 // a. Disable AGC
519 hw_get_dxx_reg(phw_data, REG_AGC_CTRL3, &reg_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, &reg_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);
534 // mode=2, tone=0
535 //reg_mode_ctrl |= (MASK_CALIB_START|2);
537 // mode=2, tone=1
538 //reg_mode_ctrl |= (MASK_CALIB_START|2|(1<<2));
540 // mode=2, tone=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, &reg_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));
552 // c.
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));
568 // d.
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
584 if (mag_0 != mag_1)
586 fix_cancel_dc_i = (mag_0*10000) / (mag_0*10000 - mag_1*10000);
588 else
590 if (mag_0 == mag_1)
592 PHY_DEBUG(("[CAL] ***** mag_0 = mag_1 !!\n"));
595 fix_cancel_dc_i = 0;
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)
603 break;
607 if ( loop >= 19 )
608 fix_cancel_dc_i = 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));
615 // g.
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)
624 u32 reg_agc_ctrl3;
625 u32 reg_mode_ctrl;
626 u32 reg_dc_cancel;
627 s32 iqcal_image_i;
628 s32 iqcal_image_q;
629 u32 sqsum;
630 s32 mag_0;
631 s32 mag_1;
632 s32 fix_cancel_dc_q = 0;
633 u32 val;
634 int loop;
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
650 // a. Disable AGC
651 hw_get_dxx_reg(phw_data, REG_AGC_CTRL3, &reg_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, &reg_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, &reg_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));
677 // b.
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));
693 // c.
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
709 if (mag_0 != mag_1)
711 fix_cancel_dc_q = (mag_0*10000) / (mag_0*10000 - mag_1*10000);
713 else
715 if (mag_0 == mag_1)
717 PHY_DEBUG(("[CAL] ***** mag_0 = mag_1 !!\n"));
720 fix_cancel_dc_q = 0;
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)
728 break;
732 if ( loop >= 19 )
733 fix_cancel_dc_q = 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));
741 // f.
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,
749 s32 a_2_threshold,
750 s32 b_2_threshold)
752 u32 reg_mode_ctrl;
753 s32 iq_mag_0_tx;
754 s32 iqcal_tone_i0;
755 s32 iqcal_tone_q0;
756 s32 iqcal_tone_i;
757 s32 iqcal_tone_q;
758 u32 sqsum;
759 s32 rot_i_b;
760 s32 rot_q_b;
761 s32 tx_cal_flt_b[4];
762 s32 tx_cal[4];
763 s32 tx_cal_reg[4];
764 s32 a_2, b_2;
765 s32 sin_b, sin_2b;
766 s32 cos_b, cos_2b;
767 s32 divisor;
768 s32 temp1, temp2;
769 u32 val;
770 u16 loop;
771 s32 iqcal_tone_i_avg,iqcal_tone_q_avg;
772 u8 verify_count;
773 int capture_time;
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));
779 verify_count = 0;
781 hw_get_dxx_reg(phw_data, REG_MODE_CTRL, &reg_mode_ctrl);
782 PHY_DEBUG(("[CAL] MODE_CTRL (read) = 0x%08X\n", reg_mode_ctrl));
784 loop = LOOP_TIMES;
786 while (loop > 0)
788 PHY_DEBUG(("[CAL] [%d.] <_tx_iq_calibration_loop>\n", (LOOP_TIMES-loop+1)));
790 iqcal_tone_i_avg=0;
791 iqcal_tone_q_avg=0;
792 if( !hw_set_dxx_reg(phw_data, 0x3C, 0x00) ) // 20060718.1 modify
793 return 0;
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));
805 // b.
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, &reg_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));
833 // e.
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)
843 continue;
845 else
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",
861 rot_i_b, rot_q_b));
863 // f.
864 divisor = ((iq_mag_0_tx * iq_mag_0_tx * 2)/1024 - rot_i_b) * 2;
866 if (divisor == 0)
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"));
871 break;
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))
886 verify_count++;
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"));
897 return 0;
900 continue;
902 else
904 verify_count = 0;
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));
912 if (cos_2b == 0)
914 PHY_DEBUG(("[CAL] ** <_tx_iq_calibration_loop> ERROR *******\n"));
915 PHY_DEBUG(("[CAL] ** cos(b)=0 !!\n"));
916 PHY_DEBUG(("[CAL] ******************************************\n"));
917 break;
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);
928 else // 2nd-cut
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"));
959 // return 0;
962 // g.
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);
972 else // 2nd-cut
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"));
996 break;
999 else // 2nd-cut
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"));
1007 break;
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
1022 val &= 0x0000FFFF;
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));
1029 return 0;
1031 else // 2nd-cut
1033 val &= 0x000003FF;
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));
1040 return 0;
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));
1048 loop--;
1051 return 1;
1054 void _tx_iq_calibration_winbond(struct hw_data *phw_data)
1056 u32 reg_agc_ctrl3;
1057 #ifdef _DEBUG
1058 s32 tx_cal_reg[4];
1060 #endif
1061 u32 reg_mode_ctrl;
1062 u32 val;
1063 u8 result;
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 );
1086 // a. Disable AGC
1087 hw_get_dxx_reg(phw_data, REG_AGC_CTRL3, &reg_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);
1098 if (result > 0)
1100 if (phw_data->revision == 0x2002) // 1st-cut
1102 hw_get_dxx_reg(phw_data, 0x54, &val);
1103 val &= 0x0000FFFF;
1104 hw_set_dxx_reg(phw_data, 0x54, val);
1106 else // 2nd-cut
1108 hw_get_dxx_reg(phw_data, 0x3C, &val);
1109 val &= 0x000003FF;
1110 hw_set_dxx_reg(phw_data, 0x3C, val);
1113 result = _tx_iq_calibration_loop_winbond(phw_data, 300, 200);
1115 if (result > 0)
1117 if (phw_data->revision == 0x2002) // 1st-cut
1119 hw_get_dxx_reg(phw_data, 0x54, &val);
1120 val &= 0x0000FFFF;
1121 hw_set_dxx_reg(phw_data, 0x54, val);
1123 else // 2nd-cut
1125 hw_get_dxx_reg(phw_data, 0x3C, &val);
1126 val &= 0x000003FF;
1127 hw_set_dxx_reg(phw_data, 0x3C, val);
1130 result = _tx_iq_calibration_loop_winbond(phw_data, 500, 400);
1131 if (result > 0)
1133 if (phw_data->revision == 0x2002) // 1st-cut
1135 hw_get_dxx_reg(phw_data, 0x54, &val);
1136 val &= 0x0000FFFF;
1137 hw_set_dxx_reg(phw_data, 0x54, val);
1139 else // 2nd-cut
1141 hw_get_dxx_reg(phw_data, 0x3C, &val);
1142 val &= 0x000003FF;
1143 hw_set_dxx_reg(phw_data, 0x3C, val);
1147 result = _tx_iq_calibration_loop_winbond(phw_data, 700, 500);
1149 if (result > 0)
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);
1158 val &= 0x0000FFFF;
1159 hw_set_dxx_reg(phw_data, 0x54, val);
1161 else // 2nd-cut
1163 hw_get_dxx_reg(phw_data, 0x3C, &val);
1164 val &= 0x000003FF;
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, &reg_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));
1178 // g. Enable AGC
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);
1184 #ifdef _DEBUG
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);
1194 else // 2nd-cut
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]));
1209 #endif
1212 // for test - BEN
1213 // RF Control Override
1216 /////////////////////////////////////////////////////////////////////////////////////////
1217 u8 _rx_iq_calibration_loop_winbond(struct hw_data *phw_data, u16 factor, u32 frequency)
1219 u32 reg_mode_ctrl;
1220 s32 iqcal_tone_i;
1221 s32 iqcal_tone_q;
1222 s32 iqcal_image_i;
1223 s32 iqcal_image_q;
1224 s32 rot_tone_i_b;
1225 s32 rot_tone_q_b;
1226 s32 rot_image_i_b;
1227 s32 rot_image_q_b;
1228 s32 rx_cal_flt_b[4];
1229 s32 rx_cal[4];
1230 s32 rx_cal_reg[4];
1231 s32 a_2, b_2;
1232 s32 sin_b, sin_2b;
1233 s32 cos_b, cos_2b;
1234 s32 temp1, temp2;
1235 u32 val;
1236 u16 loop;
1238 u32 pwr_tone;
1239 u32 pwr_image;
1240 u8 verify_count;
1242 s32 iqcal_tone_i_avg,iqcal_tone_q_avg;
1243 s32 iqcal_image_i_avg,iqcal_image_q_avg;
1244 u16 capture_time;
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);
1252 val |= BIT(19);
1253 hw_set_cxx_reg(phw_data, 0x80, val);
1255 // RF_Ctrl
1256 hw_get_cxx_reg(phw_data, 0xE4, &val);
1257 val |= BIT(0);
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
1263 // b.
1265 hw_get_dxx_reg(phw_data, REG_MODE_CTRL, &reg_mode_ctrl);
1266 PHY_DEBUG(("[CAL] MODE_CTRL (read) = 0x%08X\n", reg_mode_ctrl));
1268 verify_count = 0;
1270 //for (loop = 0; loop < 1; loop++)
1271 //for (loop = 0; loop < LOOP_TIMES; loop++)
1272 loop = LOOP_TIMES;
1273 while (loop > 0)
1275 PHY_DEBUG(("[CAL] [%d.] <_rx_iq_calibration_loop>\n", (LOOP_TIMES-loop+1)));
1276 iqcal_tone_i_avg=0;
1277 iqcal_tone_q_avg=0;
1278 iqcal_image_i_avg=0;
1279 iqcal_image_q_avg=0;
1280 capture_time=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
1287 return 0;
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));
1295 // c.
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)
1313 continue;
1315 else
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;
1330 // d.
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));
1345 // f.
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"));
1351 break;
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));
1369 if (cos_2b == 0)
1371 PHY_DEBUG(("[CAL] ** <_rx_iq_calibration_loop> ERROR *******\n"));
1372 PHY_DEBUG(("[CAL] ** cos(b)=0 !!\n"));
1373 PHY_DEBUG(("[CAL] ******************************************\n"));
1374 break;
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);
1385 else // 2nd-cut
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]));
1409 // e.
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)
1418 verify_count++;
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"));
1429 return 0;
1432 continue;
1434 // g.
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));
1445 else // 2nd-cut
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"));
1466 break;
1469 else // 2nd-cut
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"));
1477 break;
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
1493 val &= 0x0000FFFF;
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);
1500 else // 2nd-cut
1502 val &= 0x000003FF;
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);
1509 if( loop == 3 )
1510 return 0;
1512 PHY_DEBUG(("[CAL] ** CALIB_DATA = 0x%08X\n", val));
1514 loop--;
1517 return 1;
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
1526 #ifdef _DEBUG
1527 s32 rx_cal_reg[4];
1528 u32 val;
1529 #endif
1531 u8 result;
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);
1553 if (result > 0)
1555 _reset_rx_cal(phw_data);
1556 result = _rx_iq_calibration_loop_winbond(phw_data, 7943, frequency);
1558 if (result > 0)
1560 _reset_rx_cal(phw_data);
1561 result = _rx_iq_calibration_loop_winbond(phw_data, 5011, frequency);
1563 if (result > 0)
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);
1573 #ifdef _DEBUG
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));
1584 else // 2nd-cut
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]));
1596 #endif
1600 ////////////////////////////////////////////////////////////////////////
1601 void phy_calibration_winbond(struct hw_data *phw_data, u32 frequency)
1603 u32 reg_mode_ctrl;
1604 u32 iq_alpha;
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, &reg_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 )
1642 u32 ltmp=0;
1644 switch( pHwData->phy_type )
1646 case RF_MAXIM_2825:
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 );
1649 break;
1651 case RF_MAXIM_2827:
1652 ltmp = (1 << 31) | (0 << 30) | (18 << 24) | BitReverse( value, 18 );
1653 break;
1655 case RF_MAXIM_2828:
1656 ltmp = (1 << 31) | (0 << 30) | (18 << 24) | BitReverse( value, 18 );
1657 break;
1659 case RF_MAXIM_2829:
1660 ltmp = (1 << 31) | (0 << 30) | (18 << 24) | BitReverse( value, 18 );
1661 break;
1663 case RF_AIROHA_2230:
1664 case RF_AIROHA_2230S: // 20060420 Add this
1665 ltmp = (1 << 31) | (0 << 30) | (20 << 24) | BitReverse( value, 20 );
1666 break;
1668 case RF_AIROHA_7230:
1669 ltmp = (1 << 31) | (0 << 30) | (24 << 24) | (value&0xffffff);
1670 break;
1672 case RF_WB_242:
1673 case RF_WB_242_1: // 20060619.5 Add
1674 ltmp = (1 << 31) | (0 << 30) | (24 << 24) | BitReverse( value, 24 );
1675 break;
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)
1684 int init_txvga = 0;
1685 u32 reg_mode_ctrl;
1686 u32 val;
1687 s32 iqcal_tone_i0;
1688 s32 iqcal_tone_q0;
1689 u32 sqsum;
1690 s32 iq_mag_0_tx;
1691 u8 reg_state;
1692 int current_txvga;
1695 reg_state = 0;
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, &reg_mode_ctrl) ) // 20060718.1 modify
1705 return false;
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
1722 // b.
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 )
1738 break;
1739 else if(iq_mag_0_tx > 1750)
1741 init_txvga=-2;
1742 continue;
1744 else
1745 continue;
1749 if( iq_mag_0_tx>=700 && iq_mag_0_tx<=1750 )
1750 return true;
1751 else
1752 return false;