2 * Copyright (c) 2010-2011 Atheros Communications Inc.
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include "ar9003_phy.h"
21 #define MAX_MEASUREMENT 8
22 #define MAX_MAG_DELTA 11
23 #define MAX_PHS_DELTA 10
26 int mag_coeff
[AR9300_MAX_CHAINS
][MAX_MEASUREMENT
];
27 int phs_coeff
[AR9300_MAX_CHAINS
][MAX_MEASUREMENT
];
31 enum ar9003_cal_types
{
32 IQ_MISMATCH_CAL
= BIT(0),
33 TEMP_COMP_CAL
= BIT(1),
36 static void ar9003_hw_setup_calibration(struct ath_hw
*ah
,
37 struct ath9k_cal_list
*currCal
)
39 struct ath_common
*common
= ath9k_hw_common(ah
);
41 /* Select calibration to run */
42 switch (currCal
->calData
->calType
) {
45 * Start calibration with
46 * 2^(INIT_IQCAL_LOG_COUNT_MAX+1) samples
48 REG_RMW_FIELD(ah
, AR_PHY_TIMING4
,
49 AR_PHY_TIMING4_IQCAL_LOG_COUNT_MAX
,
50 currCal
->calData
->calCountMax
);
51 REG_WRITE(ah
, AR_PHY_CALMODE
, AR_PHY_CALMODE_IQ
);
53 ath_dbg(common
, ATH_DBG_CALIBRATE
,
54 "starting IQ Mismatch Calibration\n");
57 REG_SET_BIT(ah
, AR_PHY_TIMING4
, AR_PHY_TIMING4_DO_CAL
);
60 REG_RMW_FIELD(ah
, AR_PHY_65NM_CH0_THERM
,
61 AR_PHY_65NM_CH0_THERM_LOCAL
, 1);
62 REG_RMW_FIELD(ah
, AR_PHY_65NM_CH0_THERM
,
63 AR_PHY_65NM_CH0_THERM_START
, 1);
65 ath_dbg(common
, ATH_DBG_CALIBRATE
,
66 "starting Temperature Compensation Calibration\n");
72 * Generic calibration routine.
73 * Recalibrate the lower PHY chips to account for temperature/environment
76 static bool ar9003_hw_per_calibration(struct ath_hw
*ah
,
77 struct ath9k_channel
*ichan
,
79 struct ath9k_cal_list
*currCal
)
81 struct ath9k_hw_cal_data
*caldata
= ah
->caldata
;
82 /* Cal is assumed not done until explicitly set below */
83 bool iscaldone
= false;
85 /* Calibration in progress. */
86 if (currCal
->calState
== CAL_RUNNING
) {
87 /* Check to see if it has finished. */
88 if (!(REG_READ(ah
, AR_PHY_TIMING4
) & AR_PHY_TIMING4_DO_CAL
)) {
90 * Accumulate cal measures for active chains
92 currCal
->calData
->calCollect(ah
);
95 if (ah
->cal_samples
>=
96 currCal
->calData
->calNumSamples
) {
97 unsigned int i
, numChains
= 0;
98 for (i
= 0; i
< AR9300_MAX_CHAINS
; i
++) {
99 if (rxchainmask
& (1 << i
))
104 * Process accumulated data
106 currCal
->calData
->calPostProc(ah
, numChains
);
108 /* Calibration has finished. */
109 caldata
->CalValid
|= currCal
->calData
->calType
;
110 currCal
->calState
= CAL_DONE
;
114 * Set-up collection of another sub-sample until we
117 ar9003_hw_setup_calibration(ah
, currCal
);
120 } else if (!(caldata
->CalValid
& currCal
->calData
->calType
)) {
121 /* If current cal is marked invalid in channel, kick it off */
122 ath9k_hw_reset_calibration(ah
, currCal
);
128 static bool ar9003_hw_calibrate(struct ath_hw
*ah
,
129 struct ath9k_channel
*chan
,
133 bool iscaldone
= true;
134 struct ath9k_cal_list
*currCal
= ah
->cal_list_curr
;
137 * For given calibration:
138 * 1. Call generic cal routine
139 * 2. When this cal is done (isCalDone) if we have more cals waiting
140 * (eg after reset), mask this to upper layers by not propagating
141 * isCalDone if it is set to TRUE.
142 * Instead, change isCalDone to FALSE and setup the waiting cal(s)
146 (currCal
->calState
== CAL_RUNNING
||
147 currCal
->calState
== CAL_WAITING
)) {
148 iscaldone
= ar9003_hw_per_calibration(ah
, chan
,
149 rxchainmask
, currCal
);
151 ah
->cal_list_curr
= currCal
= currCal
->calNext
;
153 if (currCal
->calState
== CAL_WAITING
) {
155 ath9k_hw_reset_calibration(ah
, currCal
);
160 /* Do NF cal only at longer intervals */
163 * Get the value from the previous NF cal and update
166 ath9k_hw_getnf(ah
, chan
);
169 * Load the NF from history buffer of the current channel.
170 * NF is slow time-variant, so it is OK to use a historical
173 ath9k_hw_loadnf(ah
, ah
->curchan
);
175 /* start NF calibration, without updating BB NF register */
176 ath9k_hw_start_nfcal(ah
, false);
182 static void ar9003_hw_iqcal_collect(struct ath_hw
*ah
)
186 /* Accumulate IQ cal measures for active chains */
187 for (i
= 0; i
< AR5416_MAX_CHAINS
; i
++) {
188 if (ah
->txchainmask
& BIT(i
)) {
189 ah
->totalPowerMeasI
[i
] +=
190 REG_READ(ah
, AR_PHY_CAL_MEAS_0(i
));
191 ah
->totalPowerMeasQ
[i
] +=
192 REG_READ(ah
, AR_PHY_CAL_MEAS_1(i
));
193 ah
->totalIqCorrMeas
[i
] +=
194 (int32_t) REG_READ(ah
, AR_PHY_CAL_MEAS_2(i
));
195 ath_dbg(ath9k_hw_common(ah
), ATH_DBG_CALIBRATE
,
196 "%d: Chn %d pmi=0x%08x;pmq=0x%08x;iqcm=0x%08x;\n",
197 ah
->cal_samples
, i
, ah
->totalPowerMeasI
[i
],
198 ah
->totalPowerMeasQ
[i
],
199 ah
->totalIqCorrMeas
[i
]);
204 static void ar9003_hw_iqcalibrate(struct ath_hw
*ah
, u8 numChains
)
206 struct ath_common
*common
= ath9k_hw_common(ah
);
207 u32 powerMeasQ
, powerMeasI
, iqCorrMeas
;
208 u32 qCoffDenom
, iCoffDenom
;
209 int32_t qCoff
, iCoff
;
211 static const u_int32_t offset_array
[3] = {
212 AR_PHY_RX_IQCAL_CORR_B0
,
213 AR_PHY_RX_IQCAL_CORR_B1
,
214 AR_PHY_RX_IQCAL_CORR_B2
,
217 for (i
= 0; i
< numChains
; i
++) {
218 powerMeasI
= ah
->totalPowerMeasI
[i
];
219 powerMeasQ
= ah
->totalPowerMeasQ
[i
];
220 iqCorrMeas
= ah
->totalIqCorrMeas
[i
];
222 ath_dbg(common
, ATH_DBG_CALIBRATE
,
223 "Starting IQ Cal and Correction for Chain %d\n",
226 ath_dbg(common
, ATH_DBG_CALIBRATE
,
227 "Orignal: Chn %diq_corr_meas = 0x%08x\n",
228 i
, ah
->totalIqCorrMeas
[i
]);
232 if (iqCorrMeas
> 0x80000000) {
233 iqCorrMeas
= (0xffffffff - iqCorrMeas
) + 1;
237 ath_dbg(common
, ATH_DBG_CALIBRATE
,
238 "Chn %d pwr_meas_i = 0x%08x\n", i
, powerMeasI
);
239 ath_dbg(common
, ATH_DBG_CALIBRATE
,
240 "Chn %d pwr_meas_q = 0x%08x\n", i
, powerMeasQ
);
241 ath_dbg(common
, ATH_DBG_CALIBRATE
, "iqCorrNeg is 0x%08x\n",
244 iCoffDenom
= (powerMeasI
/ 2 + powerMeasQ
/ 2) / 256;
245 qCoffDenom
= powerMeasQ
/ 64;
247 if ((iCoffDenom
!= 0) && (qCoffDenom
!= 0)) {
248 iCoff
= iqCorrMeas
/ iCoffDenom
;
249 qCoff
= powerMeasI
/ qCoffDenom
- 64;
250 ath_dbg(common
, ATH_DBG_CALIBRATE
,
251 "Chn %d iCoff = 0x%08x\n", i
, iCoff
);
252 ath_dbg(common
, ATH_DBG_CALIBRATE
,
253 "Chn %d qCoff = 0x%08x\n", i
, qCoff
);
255 /* Force bounds on iCoff */
258 else if (iCoff
<= -63)
261 /* Negate iCoff if iqCorrNeg == 0 */
262 if (iqCorrNeg
== 0x0)
265 /* Force bounds on qCoff */
268 else if (qCoff
<= -63)
271 iCoff
= iCoff
& 0x7f;
272 qCoff
= qCoff
& 0x7f;
274 ath_dbg(common
, ATH_DBG_CALIBRATE
,
275 "Chn %d : iCoff = 0x%x qCoff = 0x%x\n",
277 ath_dbg(common
, ATH_DBG_CALIBRATE
,
278 "Register offset (0x%04x) before update = 0x%x\n",
280 REG_READ(ah
, offset_array
[i
]));
282 REG_RMW_FIELD(ah
, offset_array
[i
],
283 AR_PHY_RX_IQCAL_CORR_IQCORR_Q_I_COFF
,
285 REG_RMW_FIELD(ah
, offset_array
[i
],
286 AR_PHY_RX_IQCAL_CORR_IQCORR_Q_Q_COFF
,
288 ath_dbg(common
, ATH_DBG_CALIBRATE
,
289 "Register offset (0x%04x) QI COFF (bitfields 0x%08x) after update = 0x%x\n",
291 AR_PHY_RX_IQCAL_CORR_IQCORR_Q_I_COFF
,
292 REG_READ(ah
, offset_array
[i
]));
293 ath_dbg(common
, ATH_DBG_CALIBRATE
,
294 "Register offset (0x%04x) QQ COFF (bitfields 0x%08x) after update = 0x%x\n",
296 AR_PHY_RX_IQCAL_CORR_IQCORR_Q_Q_COFF
,
297 REG_READ(ah
, offset_array
[i
]));
299 ath_dbg(common
, ATH_DBG_CALIBRATE
,
300 "IQ Cal and Correction done for Chain %d\n", i
);
304 REG_SET_BIT(ah
, AR_PHY_RX_IQCAL_CORR_B0
,
305 AR_PHY_RX_IQCAL_CORR_IQCORR_ENABLE
);
306 ath_dbg(common
, ATH_DBG_CALIBRATE
,
307 "IQ Cal and Correction (offset 0x%04x) enabled (bit position 0x%08x). New Value 0x%08x\n",
308 (unsigned) (AR_PHY_RX_IQCAL_CORR_B0
),
309 AR_PHY_RX_IQCAL_CORR_IQCORR_ENABLE
,
310 REG_READ(ah
, AR_PHY_RX_IQCAL_CORR_B0
));
313 static const struct ath9k_percal_data iq_cal_single_sample
= {
317 ar9003_hw_iqcal_collect
,
318 ar9003_hw_iqcalibrate
321 static void ar9003_hw_init_cal_settings(struct ath_hw
*ah
)
323 ah
->iq_caldata
.calData
= &iq_cal_single_sample
;
327 * solve 4x4 linear equation used in loopback iq cal.
329 static bool ar9003_hw_solve_iq_cal(struct ath_hw
*ah
,
340 s32 f1
= cos_2phi_1
- cos_2phi_2
,
341 f3
= sin_2phi_1
- sin_2phi_2
,
343 s32 mag_tx
, phs_tx
, mag_rx
, phs_rx
;
344 const s32 result_shift
= 1 << 15;
345 struct ath_common
*common
= ath9k_hw_common(ah
);
347 f2
= (f1
* f1
+ f3
* f3
) / result_shift
;
350 ath_dbg(common
, ATH_DBG_CALIBRATE
, "Divide by 0\n");
354 /* mag mismatch, tx */
355 mag_tx
= f1
* (mag_a0_d0
- mag_a1_d0
) + f3
* (phs_a0_d0
- phs_a1_d0
);
356 /* phs mismatch, tx */
357 phs_tx
= f3
* (-mag_a0_d0
+ mag_a1_d0
) + f1
* (phs_a0_d0
- phs_a1_d0
);
359 mag_tx
= (mag_tx
/ f2
);
360 phs_tx
= (phs_tx
/ f2
);
362 /* mag mismatch, rx */
363 mag_rx
= mag_a0_d0
- (cos_2phi_1
* mag_tx
+ sin_2phi_1
* phs_tx
) /
365 /* phs mismatch, rx */
366 phs_rx
= phs_a0_d0
+ (sin_2phi_1
* mag_tx
- cos_2phi_1
* phs_tx
) /
369 solved_eq
[0] = mag_tx
;
370 solved_eq
[1] = phs_tx
;
371 solved_eq
[2] = mag_rx
;
372 solved_eq
[3] = phs_rx
;
377 static s32
ar9003_hw_find_mag_approx(struct ath_hw
*ah
, s32 in_re
, s32 in_im
)
379 s32 abs_i
= abs(in_re
),
391 return max_abs
- (max_abs
/ 32) + (min_abs
/ 8) + (min_abs
/ 4);
396 static bool ar9003_hw_calc_iq_corr(struct ath_hw
*ah
,
401 s32 i2_m_q2_a0_d0
, i2_p_q2_a0_d0
, iq_corr_a0_d0
,
402 i2_m_q2_a0_d1
, i2_p_q2_a0_d1
, iq_corr_a0_d1
,
403 i2_m_q2_a1_d0
, i2_p_q2_a1_d0
, iq_corr_a1_d0
,
404 i2_m_q2_a1_d1
, i2_p_q2_a1_d1
, iq_corr_a1_d1
;
405 s32 mag_a0_d0
, mag_a1_d0
, mag_a0_d1
, mag_a1_d1
,
406 phs_a0_d0
, phs_a1_d0
, phs_a0_d1
, phs_a1_d1
,
407 sin_2phi_1
, cos_2phi_1
,
408 sin_2phi_2
, cos_2phi_2
;
409 s32 mag_tx
, phs_tx
, mag_rx
, phs_rx
;
410 s32 solved_eq
[4], mag_corr_tx
, phs_corr_tx
, mag_corr_rx
, phs_corr_rx
,
412 const s32 res_scale
= 1 << 15;
413 const s32 delpt_shift
= 1 << 8;
415 struct ath_common
*common
= ath9k_hw_common(ah
);
417 i2_m_q2_a0_d0
= iq_res
[0] & 0xfff;
418 i2_p_q2_a0_d0
= (iq_res
[0] >> 12) & 0xfff;
419 iq_corr_a0_d0
= ((iq_res
[0] >> 24) & 0xff) + ((iq_res
[1] & 0xf) << 8);
421 if (i2_m_q2_a0_d0
> 0x800)
422 i2_m_q2_a0_d0
= -((0xfff - i2_m_q2_a0_d0
) + 1);
424 if (i2_p_q2_a0_d0
> 0x800)
425 i2_p_q2_a0_d0
= -((0xfff - i2_p_q2_a0_d0
) + 1);
427 if (iq_corr_a0_d0
> 0x800)
428 iq_corr_a0_d0
= -((0xfff - iq_corr_a0_d0
) + 1);
430 i2_m_q2_a0_d1
= (iq_res
[1] >> 4) & 0xfff;
431 i2_p_q2_a0_d1
= (iq_res
[2] & 0xfff);
432 iq_corr_a0_d1
= (iq_res
[2] >> 12) & 0xfff;
434 if (i2_m_q2_a0_d1
> 0x800)
435 i2_m_q2_a0_d1
= -((0xfff - i2_m_q2_a0_d1
) + 1);
437 if (i2_p_q2_a0_d1
> 0x800)
438 i2_p_q2_a0_d1
= -((0xfff - i2_p_q2_a0_d1
) + 1);
440 if (iq_corr_a0_d1
> 0x800)
441 iq_corr_a0_d1
= -((0xfff - iq_corr_a0_d1
) + 1);
443 i2_m_q2_a1_d0
= ((iq_res
[2] >> 24) & 0xff) + ((iq_res
[3] & 0xf) << 8);
444 i2_p_q2_a1_d0
= (iq_res
[3] >> 4) & 0xfff;
445 iq_corr_a1_d0
= iq_res
[4] & 0xfff;
447 if (i2_m_q2_a1_d0
> 0x800)
448 i2_m_q2_a1_d0
= -((0xfff - i2_m_q2_a1_d0
) + 1);
450 if (i2_p_q2_a1_d0
> 0x800)
451 i2_p_q2_a1_d0
= -((0xfff - i2_p_q2_a1_d0
) + 1);
453 if (iq_corr_a1_d0
> 0x800)
454 iq_corr_a1_d0
= -((0xfff - iq_corr_a1_d0
) + 1);
456 i2_m_q2_a1_d1
= (iq_res
[4] >> 12) & 0xfff;
457 i2_p_q2_a1_d1
= ((iq_res
[4] >> 24) & 0xff) + ((iq_res
[5] & 0xf) << 8);
458 iq_corr_a1_d1
= (iq_res
[5] >> 4) & 0xfff;
460 if (i2_m_q2_a1_d1
> 0x800)
461 i2_m_q2_a1_d1
= -((0xfff - i2_m_q2_a1_d1
) + 1);
463 if (i2_p_q2_a1_d1
> 0x800)
464 i2_p_q2_a1_d1
= -((0xfff - i2_p_q2_a1_d1
) + 1);
466 if (iq_corr_a1_d1
> 0x800)
467 iq_corr_a1_d1
= -((0xfff - iq_corr_a1_d1
) + 1);
469 if ((i2_p_q2_a0_d0
== 0) || (i2_p_q2_a0_d1
== 0) ||
470 (i2_p_q2_a1_d0
== 0) || (i2_p_q2_a1_d1
== 0)) {
471 ath_dbg(common
, ATH_DBG_CALIBRATE
,
477 i2_p_q2_a0_d0
, i2_p_q2_a0_d1
,
478 i2_p_q2_a1_d0
, i2_p_q2_a1_d1
);
482 mag_a0_d0
= (i2_m_q2_a0_d0
* res_scale
) / i2_p_q2_a0_d0
;
483 phs_a0_d0
= (iq_corr_a0_d0
* res_scale
) / i2_p_q2_a0_d0
;
485 mag_a0_d1
= (i2_m_q2_a0_d1
* res_scale
) / i2_p_q2_a0_d1
;
486 phs_a0_d1
= (iq_corr_a0_d1
* res_scale
) / i2_p_q2_a0_d1
;
488 mag_a1_d0
= (i2_m_q2_a1_d0
* res_scale
) / i2_p_q2_a1_d0
;
489 phs_a1_d0
= (iq_corr_a1_d0
* res_scale
) / i2_p_q2_a1_d0
;
491 mag_a1_d1
= (i2_m_q2_a1_d1
* res_scale
) / i2_p_q2_a1_d1
;
492 phs_a1_d1
= (iq_corr_a1_d1
* res_scale
) / i2_p_q2_a1_d1
;
494 /* w/o analog phase shift */
495 sin_2phi_1
= (((mag_a0_d0
- mag_a0_d1
) * delpt_shift
) / DELPT
);
496 /* w/o analog phase shift */
497 cos_2phi_1
= (((phs_a0_d1
- phs_a0_d0
) * delpt_shift
) / DELPT
);
498 /* w/ analog phase shift */
499 sin_2phi_2
= (((mag_a1_d0
- mag_a1_d1
) * delpt_shift
) / DELPT
);
500 /* w/ analog phase shift */
501 cos_2phi_2
= (((phs_a1_d1
- phs_a1_d0
) * delpt_shift
) / DELPT
);
504 * force sin^2 + cos^2 = 1;
505 * find magnitude by approximation
507 mag1
= ar9003_hw_find_mag_approx(ah
, cos_2phi_1
, sin_2phi_1
);
508 mag2
= ar9003_hw_find_mag_approx(ah
, cos_2phi_2
, sin_2phi_2
);
510 if ((mag1
== 0) || (mag2
== 0)) {
511 ath_dbg(common
, ATH_DBG_CALIBRATE
,
512 "Divide by 0: mag1=%d, mag2=%d\n",
517 /* normalization sin and cos by mag */
518 sin_2phi_1
= (sin_2phi_1
* res_scale
/ mag1
);
519 cos_2phi_1
= (cos_2phi_1
* res_scale
/ mag1
);
520 sin_2phi_2
= (sin_2phi_2
* res_scale
/ mag2
);
521 cos_2phi_2
= (cos_2phi_2
* res_scale
/ mag2
);
523 /* calculate IQ mismatch */
524 if (!ar9003_hw_solve_iq_cal(ah
,
525 sin_2phi_1
, cos_2phi_1
,
526 sin_2phi_2
, cos_2phi_2
,
527 mag_a0_d0
, phs_a0_d0
,
529 phs_a1_d0
, solved_eq
)) {
530 ath_dbg(common
, ATH_DBG_CALIBRATE
,
531 "Call to ar9003_hw_solve_iq_cal() failed.\n");
535 mag_tx
= solved_eq
[0];
536 phs_tx
= solved_eq
[1];
537 mag_rx
= solved_eq
[2];
538 phs_rx
= solved_eq
[3];
540 ath_dbg(common
, ATH_DBG_CALIBRATE
,
541 "chain %d: mag mismatch=%d phase mismatch=%d\n",
542 chain_idx
, mag_tx
/res_scale
, phs_tx
/res_scale
);
544 if (res_scale
== mag_tx
) {
545 ath_dbg(common
, ATH_DBG_CALIBRATE
,
546 "Divide by 0: mag_tx=%d, res_scale=%d\n",
551 /* calculate and quantize Tx IQ correction factor */
552 mag_corr_tx
= (mag_tx
* res_scale
) / (res_scale
- mag_tx
);
553 phs_corr_tx
= -phs_tx
;
555 q_q_coff
= (mag_corr_tx
* 128 / res_scale
);
556 q_i_coff
= (phs_corr_tx
* 256 / res_scale
);
558 ath_dbg(common
, ATH_DBG_CALIBRATE
,
559 "tx chain %d: mag corr=%d phase corr=%d\n",
560 chain_idx
, q_q_coff
, q_i_coff
);
571 iqc_coeff
[0] = (q_q_coff
* 128) + q_i_coff
;
573 ath_dbg(common
, ATH_DBG_CALIBRATE
,
574 "tx chain %d: iq corr coeff=%x\n",
575 chain_idx
, iqc_coeff
[0]);
577 if (-mag_rx
== res_scale
) {
578 ath_dbg(common
, ATH_DBG_CALIBRATE
,
579 "Divide by 0: mag_rx=%d, res_scale=%d\n",
584 /* calculate and quantize Rx IQ correction factors */
585 mag_corr_rx
= (-mag_rx
* res_scale
) / (res_scale
+ mag_rx
);
586 phs_corr_rx
= -phs_rx
;
588 q_q_coff
= (mag_corr_rx
* 128 / res_scale
);
589 q_i_coff
= (phs_corr_rx
* 256 / res_scale
);
591 ath_dbg(common
, ATH_DBG_CALIBRATE
,
592 "rx chain %d: mag corr=%d phase corr=%d\n",
593 chain_idx
, q_q_coff
, q_i_coff
);
604 iqc_coeff
[1] = (q_q_coff
* 128) + q_i_coff
;
606 ath_dbg(common
, ATH_DBG_CALIBRATE
,
607 "rx chain %d: iq corr coeff=%x\n",
608 chain_idx
, iqc_coeff
[1]);
613 static void ar9003_hw_detect_outlier(int *mp_coeff
, int nmeasurement
,
616 int mp_max
= -64, max_idx
= 0;
617 int mp_min
= 63, min_idx
= 0;
618 int mp_avg
= 0, i
, outlier_idx
= 0, mp_count
= 0;
620 /* find min/max mismatch across all calibrated gains */
621 for (i
= 0; i
< nmeasurement
; i
++) {
622 if (mp_coeff
[i
] > mp_max
) {
623 mp_max
= mp_coeff
[i
];
625 } else if (mp_coeff
[i
] < mp_min
) {
626 mp_min
= mp_coeff
[i
];
631 /* find average (exclude max abs value) */
632 for (i
= 0; i
< nmeasurement
; i
++) {
633 if ((abs(mp_coeff
[i
]) < abs(mp_max
)) ||
634 (abs(mp_coeff
[i
]) < abs(mp_min
))) {
635 mp_avg
+= mp_coeff
[i
];
641 * finding mean magnitude/phase if possible, otherwise
642 * just use the last value as the mean
647 mp_avg
= mp_coeff
[nmeasurement
- 1];
650 if (abs(mp_max
- mp_min
) > max_delta
) {
651 if (abs(mp_max
- mp_avg
) > abs(mp_min
- mp_avg
))
652 outlier_idx
= max_idx
;
654 outlier_idx
= min_idx
;
656 mp_coeff
[outlier_idx
] = mp_avg
;
660 static void ar9003_hw_tx_iqcal_load_avg_2_passes(struct ath_hw
*ah
,
664 int i
, im
, nmeasurement
;
665 u32 tx_corr_coeff
[MAX_MEASUREMENT
][AR9300_MAX_CHAINS
];
667 memset(tx_corr_coeff
, 0, sizeof(tx_corr_coeff
));
668 for (i
= 0; i
< MAX_MEASUREMENT
/ 2; i
++) {
669 tx_corr_coeff
[i
* 2][0] = tx_corr_coeff
[(i
* 2) + 1][0] =
670 AR_PHY_TX_IQCAL_CORR_COEFF_B0(i
);
671 if (!AR_SREV_9485(ah
)) {
672 tx_corr_coeff
[i
* 2][1] =
673 tx_corr_coeff
[(i
* 2) + 1][1] =
674 AR_PHY_TX_IQCAL_CORR_COEFF_B1(i
);
676 tx_corr_coeff
[i
* 2][2] =
677 tx_corr_coeff
[(i
* 2) + 1][2] =
678 AR_PHY_TX_IQCAL_CORR_COEFF_B2(i
);
682 /* Load the average of 2 passes */
683 for (i
= 0; i
< num_chains
; i
++) {
684 nmeasurement
= REG_READ_FIELD(ah
,
685 AR_PHY_TX_IQCAL_STATUS_B0
,
686 AR_PHY_CALIBRATED_GAINS_0
);
688 if (nmeasurement
> MAX_MEASUREMENT
)
689 nmeasurement
= MAX_MEASUREMENT
;
691 /* detect outlier only if nmeasurement > 1 */
692 if (nmeasurement
> 1) {
693 /* Detect magnitude outlier */
694 ar9003_hw_detect_outlier(coeff
->mag_coeff
[i
],
695 nmeasurement
, MAX_MAG_DELTA
);
697 /* Detect phase outlier */
698 ar9003_hw_detect_outlier(coeff
->phs_coeff
[i
],
699 nmeasurement
, MAX_PHS_DELTA
);
702 for (im
= 0; im
< nmeasurement
; im
++) {
704 coeff
->iqc_coeff
[0] = (coeff
->mag_coeff
[i
][im
] & 0x7f) |
705 ((coeff
->phs_coeff
[i
][im
] & 0x7f) << 7);
708 REG_RMW_FIELD(ah
, tx_corr_coeff
[im
][i
],
709 AR_PHY_TX_IQCAL_CORR_COEFF_00_COEFF_TABLE
,
710 coeff
->iqc_coeff
[0]);
712 REG_RMW_FIELD(ah
, tx_corr_coeff
[im
][i
],
713 AR_PHY_TX_IQCAL_CORR_COEFF_01_COEFF_TABLE
,
714 coeff
->iqc_coeff
[0]);
718 REG_RMW_FIELD(ah
, AR_PHY_TX_IQCAL_CONTROL_3
,
719 AR_PHY_TX_IQCAL_CONTROL_3_IQCORR_EN
, 0x1);
720 REG_RMW_FIELD(ah
, AR_PHY_RX_IQCAL_CORR_B0
,
721 AR_PHY_RX_IQCAL_CORR_B0_LOOPBACK_IQCORR_EN
, 0x1);
727 static bool ar9003_hw_tx_iq_cal_run(struct ath_hw
*ah
)
729 struct ath_common
*common
= ath9k_hw_common(ah
);
732 tx_gain_forced
= REG_READ_FIELD(ah
, AR_PHY_TX_FORCED_GAIN
,
733 AR_PHY_TXGAIN_FORCE
);
735 REG_RMW_FIELD(ah
, AR_PHY_TX_FORCED_GAIN
,
736 AR_PHY_TXGAIN_FORCE
, 0);
738 REG_RMW_FIELD(ah
, AR_PHY_TX_IQCAL_START
,
739 AR_PHY_TX_IQCAL_START_DO_CAL
, 1);
741 if (!ath9k_hw_wait(ah
, AR_PHY_TX_IQCAL_START
,
742 AR_PHY_TX_IQCAL_START_DO_CAL
, 0,
744 ath_dbg(common
, ATH_DBG_CALIBRATE
,
745 "Tx IQ Cal is not completed.\n");
751 static void ar9003_hw_tx_iq_cal_post_proc(struct ath_hw
*ah
)
753 struct ath_common
*common
= ath9k_hw_common(ah
);
754 const u32 txiqcal_status
[AR9300_MAX_CHAINS
] = {
755 AR_PHY_TX_IQCAL_STATUS_B0
,
756 AR_PHY_TX_IQCAL_STATUS_B1
,
757 AR_PHY_TX_IQCAL_STATUS_B2
,
759 const u_int32_t chan_info_tab
[] = {
760 AR_PHY_CHAN_INFO_TAB_0
,
761 AR_PHY_CHAN_INFO_TAB_1
,
762 AR_PHY_CHAN_INFO_TAB_2
,
770 for (i
= 0; i
< AR9300_MAX_CHAINS
; i
++) {
771 if (ah
->txchainmask
& (1 << i
))
775 for (i
= 0; i
< num_chains
; i
++) {
776 nmeasurement
= REG_READ_FIELD(ah
,
777 AR_PHY_TX_IQCAL_STATUS_B0
,
778 AR_PHY_CALIBRATED_GAINS_0
);
779 if (nmeasurement
> MAX_MEASUREMENT
)
780 nmeasurement
= MAX_MEASUREMENT
;
782 for (im
= 0; im
< nmeasurement
; im
++) {
783 ath_dbg(common
, ATH_DBG_CALIBRATE
,
784 "Doing Tx IQ Cal for chain %d.\n", i
);
786 if (REG_READ(ah
, txiqcal_status
[i
]) &
787 AR_PHY_TX_IQCAL_STATUS_FAILED
) {
788 ath_dbg(common
, ATH_DBG_CALIBRATE
,
789 "Tx IQ Cal failed for chain %d.\n", i
);
793 for (j
= 0; j
< 3; j
++) {
794 u32 idx
= 2 * j
, offset
= 4 * (3 * im
+ j
);
797 AR_PHY_CHAN_INFO_MEMORY
,
798 AR_PHY_CHAN_INFO_TAB_S2_READ
,
802 iq_res
[idx
] = REG_READ(ah
,
807 AR_PHY_CHAN_INFO_MEMORY
,
808 AR_PHY_CHAN_INFO_TAB_S2_READ
,
812 iq_res
[idx
+ 1] = 0xffff & REG_READ(ah
,
813 chan_info_tab
[i
] + offset
);
815 ath_dbg(common
, ATH_DBG_CALIBRATE
,
818 idx
, iq_res
[idx
], idx
+ 1,
822 if (!ar9003_hw_calc_iq_corr(ah
, i
, iq_res
,
824 ath_dbg(common
, ATH_DBG_CALIBRATE
,
825 "Failed in calculation of \
830 coeff
.mag_coeff
[i
][im
] = coeff
.iqc_coeff
[0] & 0x7f;
831 coeff
.phs_coeff
[i
][im
] =
832 (coeff
.iqc_coeff
[0] >> 7) & 0x7f;
834 if (coeff
.mag_coeff
[i
][im
] > 63)
835 coeff
.mag_coeff
[i
][im
] -= 128;
836 if (coeff
.phs_coeff
[i
][im
] > 63)
837 coeff
.phs_coeff
[i
][im
] -= 128;
840 ar9003_hw_tx_iqcal_load_avg_2_passes(ah
, num_chains
, &coeff
);
845 ath_dbg(common
, ATH_DBG_CALIBRATE
, "Tx IQ Cal failed\n");
848 static bool ar9003_hw_init_cal(struct ath_hw
*ah
,
849 struct ath9k_channel
*chan
)
851 struct ath_common
*common
= ath9k_hw_common(ah
);
852 bool txiqcal_done
= false;
854 /* Do Tx IQ Calibration */
855 REG_RMW_FIELD(ah
, AR_PHY_TX_IQCAL_CONTROL_1
,
856 AR_PHY_TX_IQCAL_CONTROL_1_IQCORR_I_Q_COFF_DELPT
,
860 * For AR9485 or later chips, TxIQ cal runs as part of
863 if (AR_SREV_9485_OR_LATER(ah
))
866 txiqcal_done
= ar9003_hw_tx_iq_cal_run(ah
);
867 REG_WRITE(ah
, AR_PHY_ACTIVE
, AR_PHY_ACTIVE_DIS
);
869 REG_WRITE(ah
, AR_PHY_ACTIVE
, AR_PHY_ACTIVE_EN
);
872 /* Calibrate the AGC */
873 REG_WRITE(ah
, AR_PHY_AGC_CONTROL
,
874 REG_READ(ah
, AR_PHY_AGC_CONTROL
) |
875 AR_PHY_AGC_CONTROL_CAL
);
877 /* Poll for offset calibration complete */
878 if (!ath9k_hw_wait(ah
, AR_PHY_AGC_CONTROL
, AR_PHY_AGC_CONTROL_CAL
,
879 0, AH_WAIT_TIMEOUT
)) {
880 ath_dbg(common
, ATH_DBG_CALIBRATE
,
881 "offset calibration failed to complete in 1ms; noisy environment?\n");
886 ar9003_hw_tx_iq_cal_post_proc(ah
);
888 ath9k_hw_loadnf(ah
, chan
);
889 ath9k_hw_start_nfcal(ah
, true);
891 /* Initialize list pointers */
892 ah
->cal_list
= ah
->cal_list_last
= ah
->cal_list_curr
= NULL
;
893 ah
->supp_cals
= IQ_MISMATCH_CAL
;
895 if (ah
->supp_cals
& IQ_MISMATCH_CAL
) {
896 INIT_CAL(&ah
->iq_caldata
);
897 INSERT_CAL(ah
, &ah
->iq_caldata
);
898 ath_dbg(common
, ATH_DBG_CALIBRATE
,
899 "enabling IQ Calibration.\n");
902 if (ah
->supp_cals
& TEMP_COMP_CAL
) {
903 INIT_CAL(&ah
->tempCompCalData
);
904 INSERT_CAL(ah
, &ah
->tempCompCalData
);
905 ath_dbg(common
, ATH_DBG_CALIBRATE
,
906 "enabling Temperature Compensation Calibration.\n");
909 /* Initialize current pointer to first element in list */
910 ah
->cal_list_curr
= ah
->cal_list
;
912 if (ah
->cal_list_curr
)
913 ath9k_hw_reset_calibration(ah
, ah
->cal_list_curr
);
916 ah
->caldata
->CalValid
= 0;
921 void ar9003_hw_attach_calib_ops(struct ath_hw
*ah
)
923 struct ath_hw_private_ops
*priv_ops
= ath9k_hw_private_ops(ah
);
924 struct ath_hw_ops
*ops
= ath9k_hw_ops(ah
);
926 priv_ops
->init_cal_settings
= ar9003_hw_init_cal_settings
;
927 priv_ops
->init_cal
= ar9003_hw_init_cal
;
928 priv_ops
->setup_calibration
= ar9003_hw_setup_calibration
;
930 ops
->calibrate
= ar9003_hw_calibrate
;