blob: 7c3334bd396e9ab8654fb35d2bfc6a654ad322b1 [file] [log] [blame]
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -04001/*
2 * Copyright (c) 2010 Atheros Communications Inc.
3 *
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.
7 *
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.
15 */
16
17#include "hw.h"
18#include "hw-ops.h"
19#include "ar9003_phy.h"
20
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -080021#define MPASS 3
22#define MAX_MEASUREMENT 8
23#define MAX_DIFFERENCE 10
24
25struct coeff {
26 int mag_coeff[AR9300_MAX_CHAINS][MAX_MEASUREMENT][MPASS];
27 int phs_coeff[AR9300_MAX_CHAINS][MAX_MEASUREMENT][MPASS];
28 int iqc_coeff[2];
29};
30
Felix Fietkau64978272010-10-03 19:07:16 +020031enum ar9003_cal_types {
32 IQ_MISMATCH_CAL = BIT(0),
33 TEMP_COMP_CAL = BIT(1),
34};
35
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -040036static void ar9003_hw_setup_calibration(struct ath_hw *ah,
37 struct ath9k_cal_list *currCal)
38{
Luis R. Rodriguez4b019312010-04-15 17:39:10 -040039 struct ath_common *common = ath9k_hw_common(ah);
40
41 /* Select calibration to run */
42 switch (currCal->calData->calType) {
43 case IQ_MISMATCH_CAL:
44 /*
45 * Start calibration with
46 * 2^(INIT_IQCAL_LOG_COUNT_MAX+1) samples
47 */
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);
52
Joe Perches226afe62010-12-02 19:12:37 -080053 ath_dbg(common, ATH_DBG_CALIBRATE,
54 "starting IQ Mismatch Calibration\n");
Luis R. Rodriguez4b019312010-04-15 17:39:10 -040055
56 /* Kick-off cal */
57 REG_SET_BIT(ah, AR_PHY_TIMING4, AR_PHY_TIMING4_DO_CAL);
58 break;
59 case TEMP_COMP_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);
64
Joe Perches226afe62010-12-02 19:12:37 -080065 ath_dbg(common, ATH_DBG_CALIBRATE,
66 "starting Temperature Compensation Calibration\n");
Luis R. Rodriguez4b019312010-04-15 17:39:10 -040067 break;
Luis R. Rodriguez4b019312010-04-15 17:39:10 -040068 }
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -040069}
70
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -040071/*
72 * Generic calibration routine.
73 * Recalibrate the lower PHY chips to account for temperature/environment
74 * changes.
75 */
76static bool ar9003_hw_per_calibration(struct ath_hw *ah,
77 struct ath9k_channel *ichan,
78 u8 rxchainmask,
79 struct ath9k_cal_list *currCal)
80{
Felix Fietkau20bd2a02010-07-31 00:12:00 +020081 struct ath9k_hw_cal_data *caldata = ah->caldata;
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -040082 /* Cal is assumed not done until explicitly set below */
83 bool iscaldone = false;
84
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)) {
89 /*
90 * Accumulate cal measures for active chains
91 */
92 currCal->calData->calCollect(ah);
93 ah->cal_samples++;
94
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))
100 numChains++;
101 }
102
103 /*
104 * Process accumulated data
105 */
106 currCal->calData->calPostProc(ah, numChains);
107
108 /* Calibration has finished. */
Felix Fietkau20bd2a02010-07-31 00:12:00 +0200109 caldata->CalValid |= currCal->calData->calType;
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400110 currCal->calState = CAL_DONE;
111 iscaldone = true;
112 } else {
113 /*
114 * Set-up collection of another sub-sample until we
115 * get desired number
116 */
117 ar9003_hw_setup_calibration(ah, currCal);
118 }
119 }
Felix Fietkau20bd2a02010-07-31 00:12:00 +0200120 } else if (!(caldata->CalValid & currCal->calData->calType)) {
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400121 /* If current cal is marked invalid in channel, kick it off */
122 ath9k_hw_reset_calibration(ah, currCal);
123 }
124
125 return iscaldone;
126}
127
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -0400128static bool ar9003_hw_calibrate(struct ath_hw *ah,
129 struct ath9k_channel *chan,
130 u8 rxchainmask,
131 bool longcal)
132{
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400133 bool iscaldone = true;
134 struct ath9k_cal_list *currCal = ah->cal_list_curr;
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -0400135
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400136 /*
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)
143 * to be run.
144 */
145 if (currCal &&
146 (currCal->calState == CAL_RUNNING ||
147 currCal->calState == CAL_WAITING)) {
148 iscaldone = ar9003_hw_per_calibration(ah, chan,
149 rxchainmask, currCal);
150 if (iscaldone) {
151 ah->cal_list_curr = currCal = currCal->calNext;
152
153 if (currCal->calState == CAL_WAITING) {
154 iscaldone = false;
155 ath9k_hw_reset_calibration(ah, currCal);
156 }
157 }
158 }
159
160 /* Do NF cal only at longer intervals */
161 if (longcal) {
162 /*
Felix Fietkau93697462010-07-30 21:02:10 +0200163 * Get the value from the previous NF cal and update
164 * history buffer.
165 */
166 ath9k_hw_getnf(ah, chan);
167
168 /*
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400169 * Load the NF from history buffer of the current channel.
170 * NF is slow time-variant, so it is OK to use a historical
171 * value.
172 */
173 ath9k_hw_loadnf(ah, ah->curchan);
174
175 /* start NF calibration, without updating BB NF register */
Felix Fietkau00c86592010-07-30 21:02:09 +0200176 ath9k_hw_start_nfcal(ah, false);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400177 }
178
179 return iscaldone;
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -0400180}
181
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400182static void ar9003_hw_iqcal_collect(struct ath_hw *ah)
183{
184 int i;
185
186 /* Accumulate IQ cal measures for active chains */
187 for (i = 0; i < AR5416_MAX_CHAINS; i++) {
188 ah->totalPowerMeasI[i] +=
189 REG_READ(ah, AR_PHY_CAL_MEAS_0(i));
190 ah->totalPowerMeasQ[i] +=
191 REG_READ(ah, AR_PHY_CAL_MEAS_1(i));
192 ah->totalIqCorrMeas[i] +=
193 (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_2(i));
Joe Perches226afe62010-12-02 19:12:37 -0800194 ath_dbg(ath9k_hw_common(ah), ATH_DBG_CALIBRATE,
195 "%d: Chn %d pmi=0x%08x;pmq=0x%08x;iqcm=0x%08x;\n",
196 ah->cal_samples, i, ah->totalPowerMeasI[i],
197 ah->totalPowerMeasQ[i],
198 ah->totalIqCorrMeas[i]);
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400199 }
200}
201
202static void ar9003_hw_iqcalibrate(struct ath_hw *ah, u8 numChains)
203{
204 struct ath_common *common = ath9k_hw_common(ah);
205 u32 powerMeasQ, powerMeasI, iqCorrMeas;
206 u32 qCoffDenom, iCoffDenom;
207 int32_t qCoff, iCoff;
208 int iqCorrNeg, i;
Joe Perches07b2fa52010-11-20 18:38:53 -0800209 static const u_int32_t offset_array[3] = {
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400210 AR_PHY_RX_IQCAL_CORR_B0,
211 AR_PHY_RX_IQCAL_CORR_B1,
212 AR_PHY_RX_IQCAL_CORR_B2,
213 };
214
215 for (i = 0; i < numChains; i++) {
216 powerMeasI = ah->totalPowerMeasI[i];
217 powerMeasQ = ah->totalPowerMeasQ[i];
218 iqCorrMeas = ah->totalIqCorrMeas[i];
219
Joe Perches226afe62010-12-02 19:12:37 -0800220 ath_dbg(common, ATH_DBG_CALIBRATE,
221 "Starting IQ Cal and Correction for Chain %d\n",
222 i);
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400223
Joe Perches226afe62010-12-02 19:12:37 -0800224 ath_dbg(common, ATH_DBG_CALIBRATE,
225 "Orignal: Chn %diq_corr_meas = 0x%08x\n",
226 i, ah->totalIqCorrMeas[i]);
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400227
228 iqCorrNeg = 0;
229
230 if (iqCorrMeas > 0x80000000) {
231 iqCorrMeas = (0xffffffff - iqCorrMeas) + 1;
232 iqCorrNeg = 1;
233 }
234
Joe Perches226afe62010-12-02 19:12:37 -0800235 ath_dbg(common, ATH_DBG_CALIBRATE,
236 "Chn %d pwr_meas_i = 0x%08x\n", i, powerMeasI);
237 ath_dbg(common, ATH_DBG_CALIBRATE,
238 "Chn %d pwr_meas_q = 0x%08x\n", i, powerMeasQ);
239 ath_dbg(common, ATH_DBG_CALIBRATE, "iqCorrNeg is 0x%08x\n",
240 iqCorrNeg);
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400241
242 iCoffDenom = (powerMeasI / 2 + powerMeasQ / 2) / 256;
243 qCoffDenom = powerMeasQ / 64;
244
245 if ((iCoffDenom != 0) && (qCoffDenom != 0)) {
246 iCoff = iqCorrMeas / iCoffDenom;
247 qCoff = powerMeasI / qCoffDenom - 64;
Joe Perches226afe62010-12-02 19:12:37 -0800248 ath_dbg(common, ATH_DBG_CALIBRATE,
249 "Chn %d iCoff = 0x%08x\n", i, iCoff);
250 ath_dbg(common, ATH_DBG_CALIBRATE,
251 "Chn %d qCoff = 0x%08x\n", i, qCoff);
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400252
253 /* Force bounds on iCoff */
254 if (iCoff >= 63)
255 iCoff = 63;
256 else if (iCoff <= -63)
257 iCoff = -63;
258
259 /* Negate iCoff if iqCorrNeg == 0 */
260 if (iqCorrNeg == 0x0)
261 iCoff = -iCoff;
262
263 /* Force bounds on qCoff */
264 if (qCoff >= 63)
265 qCoff = 63;
266 else if (qCoff <= -63)
267 qCoff = -63;
268
269 iCoff = iCoff & 0x7f;
270 qCoff = qCoff & 0x7f;
271
Joe Perches226afe62010-12-02 19:12:37 -0800272 ath_dbg(common, ATH_DBG_CALIBRATE,
273 "Chn %d : iCoff = 0x%x qCoff = 0x%x\n",
274 i, iCoff, qCoff);
275 ath_dbg(common, ATH_DBG_CALIBRATE,
276 "Register offset (0x%04x) before update = 0x%x\n",
277 offset_array[i],
278 REG_READ(ah, offset_array[i]));
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400279
280 REG_RMW_FIELD(ah, offset_array[i],
281 AR_PHY_RX_IQCAL_CORR_IQCORR_Q_I_COFF,
282 iCoff);
283 REG_RMW_FIELD(ah, offset_array[i],
284 AR_PHY_RX_IQCAL_CORR_IQCORR_Q_Q_COFF,
285 qCoff);
Joe Perches226afe62010-12-02 19:12:37 -0800286 ath_dbg(common, ATH_DBG_CALIBRATE,
287 "Register offset (0x%04x) QI COFF (bitfields 0x%08x) after update = 0x%x\n",
288 offset_array[i],
289 AR_PHY_RX_IQCAL_CORR_IQCORR_Q_I_COFF,
290 REG_READ(ah, offset_array[i]));
291 ath_dbg(common, ATH_DBG_CALIBRATE,
292 "Register offset (0x%04x) QQ COFF (bitfields 0x%08x) after update = 0x%x\n",
293 offset_array[i],
294 AR_PHY_RX_IQCAL_CORR_IQCORR_Q_Q_COFF,
295 REG_READ(ah, offset_array[i]));
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400296
Joe Perches226afe62010-12-02 19:12:37 -0800297 ath_dbg(common, ATH_DBG_CALIBRATE,
298 "IQ Cal and Correction done for Chain %d\n", i);
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400299 }
300 }
301
302 REG_SET_BIT(ah, AR_PHY_RX_IQCAL_CORR_B0,
303 AR_PHY_RX_IQCAL_CORR_IQCORR_ENABLE);
Joe Perches226afe62010-12-02 19:12:37 -0800304 ath_dbg(common, ATH_DBG_CALIBRATE,
305 "IQ Cal and Correction (offset 0x%04x) enabled (bit position 0x%08x). New Value 0x%08x\n",
306 (unsigned) (AR_PHY_RX_IQCAL_CORR_B0),
307 AR_PHY_RX_IQCAL_CORR_IQCORR_ENABLE,
308 REG_READ(ah, AR_PHY_RX_IQCAL_CORR_B0));
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400309}
310
311static const struct ath9k_percal_data iq_cal_single_sample = {
312 IQ_MISMATCH_CAL,
313 MIN_CAL_SAMPLES,
314 PER_MAX_LOG_COUNT,
315 ar9003_hw_iqcal_collect,
316 ar9003_hw_iqcalibrate
317};
318
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -0400319static void ar9003_hw_init_cal_settings(struct ath_hw *ah)
320{
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400321 ah->iq_caldata.calData = &iq_cal_single_sample;
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -0400322}
323
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400324/*
325 * solve 4x4 linear equation used in loopback iq cal.
326 */
327static bool ar9003_hw_solve_iq_cal(struct ath_hw *ah,
328 s32 sin_2phi_1,
329 s32 cos_2phi_1,
330 s32 sin_2phi_2,
331 s32 cos_2phi_2,
332 s32 mag_a0_d0,
333 s32 phs_a0_d0,
334 s32 mag_a1_d0,
335 s32 phs_a1_d0,
336 s32 solved_eq[])
Luis R. Rodriguez77d6d392010-04-15 17:39:09 -0400337{
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400338 s32 f1 = cos_2phi_1 - cos_2phi_2,
339 f3 = sin_2phi_1 - sin_2phi_2,
340 f2;
341 s32 mag_tx, phs_tx, mag_rx, phs_rx;
342 const s32 result_shift = 1 << 15;
343 struct ath_common *common = ath9k_hw_common(ah);
344
345 f2 = (f1 * f1 + f3 * f3) / result_shift;
346
347 if (!f2) {
Joe Perches226afe62010-12-02 19:12:37 -0800348 ath_dbg(common, ATH_DBG_CALIBRATE, "Divide by 0\n");
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400349 return false;
350 }
351
352 /* mag mismatch, tx */
353 mag_tx = f1 * (mag_a0_d0 - mag_a1_d0) + f3 * (phs_a0_d0 - phs_a1_d0);
354 /* phs mismatch, tx */
355 phs_tx = f3 * (-mag_a0_d0 + mag_a1_d0) + f1 * (phs_a0_d0 - phs_a1_d0);
356
357 mag_tx = (mag_tx / f2);
358 phs_tx = (phs_tx / f2);
359
360 /* mag mismatch, rx */
361 mag_rx = mag_a0_d0 - (cos_2phi_1 * mag_tx + sin_2phi_1 * phs_tx) /
362 result_shift;
363 /* phs mismatch, rx */
364 phs_rx = phs_a0_d0 + (sin_2phi_1 * mag_tx - cos_2phi_1 * phs_tx) /
365 result_shift;
366
367 solved_eq[0] = mag_tx;
368 solved_eq[1] = phs_tx;
369 solved_eq[2] = mag_rx;
370 solved_eq[3] = phs_rx;
371
372 return true;
373}
374
375static s32 ar9003_hw_find_mag_approx(struct ath_hw *ah, s32 in_re, s32 in_im)
376{
377 s32 abs_i = abs(in_re),
378 abs_q = abs(in_im),
379 max_abs, min_abs;
380
381 if (abs_i > abs_q) {
382 max_abs = abs_i;
383 min_abs = abs_q;
384 } else {
385 max_abs = abs_q;
386 min_abs = abs_i;
387 }
388
389 return max_abs - (max_abs / 32) + (min_abs / 8) + (min_abs / 4);
390}
391
392#define DELPT 32
393
394static bool ar9003_hw_calc_iq_corr(struct ath_hw *ah,
395 s32 chain_idx,
396 const s32 iq_res[],
397 s32 iqc_coeff[])
398{
399 s32 i2_m_q2_a0_d0, i2_p_q2_a0_d0, iq_corr_a0_d0,
400 i2_m_q2_a0_d1, i2_p_q2_a0_d1, iq_corr_a0_d1,
401 i2_m_q2_a1_d0, i2_p_q2_a1_d0, iq_corr_a1_d0,
402 i2_m_q2_a1_d1, i2_p_q2_a1_d1, iq_corr_a1_d1;
403 s32 mag_a0_d0, mag_a1_d0, mag_a0_d1, mag_a1_d1,
404 phs_a0_d0, phs_a1_d0, phs_a0_d1, phs_a1_d1,
405 sin_2phi_1, cos_2phi_1,
406 sin_2phi_2, cos_2phi_2;
407 s32 mag_tx, phs_tx, mag_rx, phs_rx;
408 s32 solved_eq[4], mag_corr_tx, phs_corr_tx, mag_corr_rx, phs_corr_rx,
409 q_q_coff, q_i_coff;
410 const s32 res_scale = 1 << 15;
411 const s32 delpt_shift = 1 << 8;
412 s32 mag1, mag2;
413 struct ath_common *common = ath9k_hw_common(ah);
414
415 i2_m_q2_a0_d0 = iq_res[0] & 0xfff;
416 i2_p_q2_a0_d0 = (iq_res[0] >> 12) & 0xfff;
417 iq_corr_a0_d0 = ((iq_res[0] >> 24) & 0xff) + ((iq_res[1] & 0xf) << 8);
418
419 if (i2_m_q2_a0_d0 > 0x800)
420 i2_m_q2_a0_d0 = -((0xfff - i2_m_q2_a0_d0) + 1);
421
422 if (i2_p_q2_a0_d0 > 0x800)
423 i2_p_q2_a0_d0 = -((0xfff - i2_p_q2_a0_d0) + 1);
424
425 if (iq_corr_a0_d0 > 0x800)
426 iq_corr_a0_d0 = -((0xfff - iq_corr_a0_d0) + 1);
427
428 i2_m_q2_a0_d1 = (iq_res[1] >> 4) & 0xfff;
429 i2_p_q2_a0_d1 = (iq_res[2] & 0xfff);
430 iq_corr_a0_d1 = (iq_res[2] >> 12) & 0xfff;
431
432 if (i2_m_q2_a0_d1 > 0x800)
433 i2_m_q2_a0_d1 = -((0xfff - i2_m_q2_a0_d1) + 1);
434
435 if (i2_p_q2_a0_d1 > 0x800)
436 i2_p_q2_a0_d1 = -((0xfff - i2_p_q2_a0_d1) + 1);
437
438 if (iq_corr_a0_d1 > 0x800)
439 iq_corr_a0_d1 = -((0xfff - iq_corr_a0_d1) + 1);
440
441 i2_m_q2_a1_d0 = ((iq_res[2] >> 24) & 0xff) + ((iq_res[3] & 0xf) << 8);
442 i2_p_q2_a1_d0 = (iq_res[3] >> 4) & 0xfff;
443 iq_corr_a1_d0 = iq_res[4] & 0xfff;
444
445 if (i2_m_q2_a1_d0 > 0x800)
446 i2_m_q2_a1_d0 = -((0xfff - i2_m_q2_a1_d0) + 1);
447
448 if (i2_p_q2_a1_d0 > 0x800)
449 i2_p_q2_a1_d0 = -((0xfff - i2_p_q2_a1_d0) + 1);
450
451 if (iq_corr_a1_d0 > 0x800)
452 iq_corr_a1_d0 = -((0xfff - iq_corr_a1_d0) + 1);
453
454 i2_m_q2_a1_d1 = (iq_res[4] >> 12) & 0xfff;
455 i2_p_q2_a1_d1 = ((iq_res[4] >> 24) & 0xff) + ((iq_res[5] & 0xf) << 8);
456 iq_corr_a1_d1 = (iq_res[5] >> 4) & 0xfff;
457
458 if (i2_m_q2_a1_d1 > 0x800)
459 i2_m_q2_a1_d1 = -((0xfff - i2_m_q2_a1_d1) + 1);
460
461 if (i2_p_q2_a1_d1 > 0x800)
462 i2_p_q2_a1_d1 = -((0xfff - i2_p_q2_a1_d1) + 1);
463
464 if (iq_corr_a1_d1 > 0x800)
465 iq_corr_a1_d1 = -((0xfff - iq_corr_a1_d1) + 1);
466
467 if ((i2_p_q2_a0_d0 == 0) || (i2_p_q2_a0_d1 == 0) ||
468 (i2_p_q2_a1_d0 == 0) || (i2_p_q2_a1_d1 == 0)) {
Joe Perches226afe62010-12-02 19:12:37 -0800469 ath_dbg(common, ATH_DBG_CALIBRATE,
470 "Divide by 0:\n"
471 "a0_d0=%d\n"
472 "a0_d1=%d\n"
473 "a2_d0=%d\n"
474 "a1_d1=%d\n",
475 i2_p_q2_a0_d0, i2_p_q2_a0_d1,
476 i2_p_q2_a1_d0, i2_p_q2_a1_d1);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400477 return false;
478 }
479
480 mag_a0_d0 = (i2_m_q2_a0_d0 * res_scale) / i2_p_q2_a0_d0;
481 phs_a0_d0 = (iq_corr_a0_d0 * res_scale) / i2_p_q2_a0_d0;
482
483 mag_a0_d1 = (i2_m_q2_a0_d1 * res_scale) / i2_p_q2_a0_d1;
484 phs_a0_d1 = (iq_corr_a0_d1 * res_scale) / i2_p_q2_a0_d1;
485
486 mag_a1_d0 = (i2_m_q2_a1_d0 * res_scale) / i2_p_q2_a1_d0;
487 phs_a1_d0 = (iq_corr_a1_d0 * res_scale) / i2_p_q2_a1_d0;
488
489 mag_a1_d1 = (i2_m_q2_a1_d1 * res_scale) / i2_p_q2_a1_d1;
490 phs_a1_d1 = (iq_corr_a1_d1 * res_scale) / i2_p_q2_a1_d1;
491
492 /* w/o analog phase shift */
493 sin_2phi_1 = (((mag_a0_d0 - mag_a0_d1) * delpt_shift) / DELPT);
494 /* w/o analog phase shift */
495 cos_2phi_1 = (((phs_a0_d1 - phs_a0_d0) * delpt_shift) / DELPT);
496 /* w/ analog phase shift */
497 sin_2phi_2 = (((mag_a1_d0 - mag_a1_d1) * delpt_shift) / DELPT);
498 /* w/ analog phase shift */
499 cos_2phi_2 = (((phs_a1_d1 - phs_a1_d0) * delpt_shift) / DELPT);
500
501 /*
502 * force sin^2 + cos^2 = 1;
503 * find magnitude by approximation
504 */
505 mag1 = ar9003_hw_find_mag_approx(ah, cos_2phi_1, sin_2phi_1);
506 mag2 = ar9003_hw_find_mag_approx(ah, cos_2phi_2, sin_2phi_2);
507
508 if ((mag1 == 0) || (mag2 == 0)) {
Joe Perches226afe62010-12-02 19:12:37 -0800509 ath_dbg(common, ATH_DBG_CALIBRATE,
510 "Divide by 0: mag1=%d, mag2=%d\n",
511 mag1, mag2);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400512 return false;
513 }
514
515 /* normalization sin and cos by mag */
516 sin_2phi_1 = (sin_2phi_1 * res_scale / mag1);
517 cos_2phi_1 = (cos_2phi_1 * res_scale / mag1);
518 sin_2phi_2 = (sin_2phi_2 * res_scale / mag2);
519 cos_2phi_2 = (cos_2phi_2 * res_scale / mag2);
520
521 /* calculate IQ mismatch */
522 if (!ar9003_hw_solve_iq_cal(ah,
523 sin_2phi_1, cos_2phi_1,
524 sin_2phi_2, cos_2phi_2,
525 mag_a0_d0, phs_a0_d0,
526 mag_a1_d0,
527 phs_a1_d0, solved_eq)) {
Joe Perches226afe62010-12-02 19:12:37 -0800528 ath_dbg(common, ATH_DBG_CALIBRATE,
529 "Call to ar9003_hw_solve_iq_cal() failed.\n");
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400530 return false;
531 }
532
533 mag_tx = solved_eq[0];
534 phs_tx = solved_eq[1];
535 mag_rx = solved_eq[2];
536 phs_rx = solved_eq[3];
537
Joe Perches226afe62010-12-02 19:12:37 -0800538 ath_dbg(common, ATH_DBG_CALIBRATE,
539 "chain %d: mag mismatch=%d phase mismatch=%d\n",
540 chain_idx, mag_tx/res_scale, phs_tx/res_scale);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400541
542 if (res_scale == mag_tx) {
Joe Perches226afe62010-12-02 19:12:37 -0800543 ath_dbg(common, ATH_DBG_CALIBRATE,
544 "Divide by 0: mag_tx=%d, res_scale=%d\n",
545 mag_tx, res_scale);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400546 return false;
547 }
548
549 /* calculate and quantize Tx IQ correction factor */
550 mag_corr_tx = (mag_tx * res_scale) / (res_scale - mag_tx);
551 phs_corr_tx = -phs_tx;
552
553 q_q_coff = (mag_corr_tx * 128 / res_scale);
554 q_i_coff = (phs_corr_tx * 256 / res_scale);
555
Joe Perches226afe62010-12-02 19:12:37 -0800556 ath_dbg(common, ATH_DBG_CALIBRATE,
557 "tx chain %d: mag corr=%d phase corr=%d\n",
558 chain_idx, q_q_coff, q_i_coff);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400559
560 if (q_i_coff < -63)
561 q_i_coff = -63;
562 if (q_i_coff > 63)
563 q_i_coff = 63;
564 if (q_q_coff < -63)
565 q_q_coff = -63;
566 if (q_q_coff > 63)
567 q_q_coff = 63;
568
569 iqc_coeff[0] = (q_q_coff * 128) + q_i_coff;
570
Joe Perches226afe62010-12-02 19:12:37 -0800571 ath_dbg(common, ATH_DBG_CALIBRATE,
572 "tx chain %d: iq corr coeff=%x\n",
573 chain_idx, iqc_coeff[0]);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400574
575 if (-mag_rx == res_scale) {
Joe Perches226afe62010-12-02 19:12:37 -0800576 ath_dbg(common, ATH_DBG_CALIBRATE,
577 "Divide by 0: mag_rx=%d, res_scale=%d\n",
578 mag_rx, res_scale);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400579 return false;
580 }
581
582 /* calculate and quantize Rx IQ correction factors */
583 mag_corr_rx = (-mag_rx * res_scale) / (res_scale + mag_rx);
584 phs_corr_rx = -phs_rx;
585
586 q_q_coff = (mag_corr_rx * 128 / res_scale);
587 q_i_coff = (phs_corr_rx * 256 / res_scale);
588
Joe Perches226afe62010-12-02 19:12:37 -0800589 ath_dbg(common, ATH_DBG_CALIBRATE,
590 "rx chain %d: mag corr=%d phase corr=%d\n",
591 chain_idx, q_q_coff, q_i_coff);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400592
593 if (q_i_coff < -63)
594 q_i_coff = -63;
595 if (q_i_coff > 63)
596 q_i_coff = 63;
597 if (q_q_coff < -63)
598 q_q_coff = -63;
599 if (q_q_coff > 63)
600 q_q_coff = 63;
601
602 iqc_coeff[1] = (q_q_coff * 128) + q_i_coff;
603
Joe Perches226afe62010-12-02 19:12:37 -0800604 ath_dbg(common, ATH_DBG_CALIBRATE,
605 "rx chain %d: iq corr coeff=%x\n",
606 chain_idx, iqc_coeff[1]);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400607
608 return true;
609}
610
611static void ar9003_hw_tx_iq_cal(struct ath_hw *ah)
612{
613 struct ath_common *common = ath9k_hw_common(ah);
Joe Perches07b2fa52010-11-20 18:38:53 -0800614 static const u32 txiqcal_status[AR9300_MAX_CHAINS] = {
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400615 AR_PHY_TX_IQCAL_STATUS_B0,
616 AR_PHY_TX_IQCAL_STATUS_B1,
617 AR_PHY_TX_IQCAL_STATUS_B2,
618 };
Joe Perches07b2fa52010-11-20 18:38:53 -0800619 static const u32 rx_corr[AR9300_MAX_CHAINS] = {
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400620 AR_PHY_RX_IQCAL_CORR_B0,
621 AR_PHY_RX_IQCAL_CORR_B1,
622 AR_PHY_RX_IQCAL_CORR_B2,
623 };
Joe Perches07b2fa52010-11-20 18:38:53 -0800624 static const u_int32_t chan_info_tab[] = {
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400625 AR_PHY_CHAN_INFO_TAB_0,
626 AR_PHY_CHAN_INFO_TAB_1,
627 AR_PHY_CHAN_INFO_TAB_2,
628 };
Vasanthakumar Thiagarajan31faff82010-12-06 04:27:55 -0800629 u32 tx_corr_coeff[AR9300_MAX_CHAINS];
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400630 s32 iq_res[6];
631 s32 iqc_coeff[2];
632 s32 i, j;
633 u32 num_chains = 0;
634
Vasanthakumar Thiagarajan31faff82010-12-06 04:27:55 -0800635 tx_corr_coeff[0] = AR_PHY_TX_IQCAL_CORR_COEFF_B0(0);
636 tx_corr_coeff[1] = AR_PHY_TX_IQCAL_CORR_COEFF_B1(0);
637 tx_corr_coeff[2] = AR_PHY_TX_IQCAL_CORR_COEFF_B2(0);
638
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400639 for (i = 0; i < AR9300_MAX_CHAINS; i++) {
640 if (ah->txchainmask & (1 << i))
641 num_chains++;
642 }
643
644 REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_CONTROL_1,
645 AR_PHY_TX_IQCAQL_CONTROL_1_IQCORR_I_Q_COFF_DELPT,
646 DELPT);
647 REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_START,
648 AR_PHY_TX_IQCAL_START_DO_CAL,
649 AR_PHY_TX_IQCAL_START_DO_CAL);
650
651 if (!ath9k_hw_wait(ah, AR_PHY_TX_IQCAL_START,
652 AR_PHY_TX_IQCAL_START_DO_CAL,
653 0, AH_WAIT_TIMEOUT)) {
Joe Perches226afe62010-12-02 19:12:37 -0800654 ath_dbg(common, ATH_DBG_CALIBRATE,
655 "Tx IQ Cal not complete.\n");
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400656 goto TX_IQ_CAL_FAILED;
657 }
658
659 for (i = 0; i < num_chains; i++) {
Joe Perches226afe62010-12-02 19:12:37 -0800660 ath_dbg(common, ATH_DBG_CALIBRATE,
661 "Doing Tx IQ Cal for chain %d.\n", i);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400662
663 if (REG_READ(ah, txiqcal_status[i]) &
664 AR_PHY_TX_IQCAL_STATUS_FAILED) {
Joe Perches226afe62010-12-02 19:12:37 -0800665 ath_dbg(common, ATH_DBG_CALIBRATE,
666 "Tx IQ Cal failed for chain %d.\n", i);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400667 goto TX_IQ_CAL_FAILED;
668 }
669
670 for (j = 0; j < 3; j++) {
671 u_int8_t idx = 2 * j,
672 offset = 4 * j;
673
674 REG_RMW_FIELD(ah, AR_PHY_CHAN_INFO_MEMORY,
675 AR_PHY_CHAN_INFO_TAB_S2_READ, 0);
676
677 /* 32 bits */
678 iq_res[idx] = REG_READ(ah, chan_info_tab[i] + offset);
679
680 REG_RMW_FIELD(ah, AR_PHY_CHAN_INFO_MEMORY,
681 AR_PHY_CHAN_INFO_TAB_S2_READ, 1);
682
683 /* 16 bits */
684 iq_res[idx+1] = 0xffff & REG_READ(ah,
685 chan_info_tab[i] +
686 offset);
687
Joe Perches226afe62010-12-02 19:12:37 -0800688 ath_dbg(common, ATH_DBG_CALIBRATE,
689 "IQ RES[%d]=0x%x IQ_RES[%d]=0x%x\n",
690 idx, iq_res[idx], idx+1, iq_res[idx+1]);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400691 }
692
693 if (!ar9003_hw_calc_iq_corr(ah, i, iq_res, iqc_coeff)) {
Joe Perches226afe62010-12-02 19:12:37 -0800694 ath_dbg(common, ATH_DBG_CALIBRATE,
695 "Failed in calculation of IQ correction.\n");
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400696 goto TX_IQ_CAL_FAILED;
697 }
698
Joe Perches226afe62010-12-02 19:12:37 -0800699 ath_dbg(common, ATH_DBG_CALIBRATE,
700 "IQ_COEFF[0] = 0x%x IQ_COEFF[1] = 0x%x\n",
701 iqc_coeff[0], iqc_coeff[1]);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400702
703 REG_RMW_FIELD(ah, tx_corr_coeff[i],
704 AR_PHY_TX_IQCAL_CORR_COEFF_01_COEFF_TABLE,
705 iqc_coeff[0]);
706 REG_RMW_FIELD(ah, rx_corr[i],
707 AR_PHY_RX_IQCAL_CORR_LOOPBACK_IQCORR_Q_Q_COFF,
708 iqc_coeff[1] >> 7);
709 REG_RMW_FIELD(ah, rx_corr[i],
710 AR_PHY_RX_IQCAL_CORR_LOOPBACK_IQCORR_Q_I_COFF,
711 iqc_coeff[1]);
712 }
713
714 REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_CONTROL_3,
715 AR_PHY_TX_IQCAL_CONTROL_3_IQCORR_EN, 0x1);
716 REG_RMW_FIELD(ah, AR_PHY_RX_IQCAL_CORR_B0,
717 AR_PHY_RX_IQCAL_CORR_B0_LOOPBACK_IQCORR_EN, 0x1);
718
719 return;
720
721TX_IQ_CAL_FAILED:
Joe Perches226afe62010-12-02 19:12:37 -0800722 ath_dbg(common, ATH_DBG_CALIBRATE, "Tx IQ Cal failed\n");
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400723}
724
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800725static bool ar9003_hw_compute_closest_pass_and_avg(int *mp_coeff, int *mp_avg)
726{
727 int diff[MPASS];
728
729 diff[0] = abs(mp_coeff[0] - mp_coeff[1]);
730 diff[1] = abs(mp_coeff[1] - mp_coeff[2]);
731 diff[2] = abs(mp_coeff[2] - mp_coeff[0]);
732
733 if (diff[0] > MAX_MEASUREMENT &&
734 diff[1] > MAX_MEASUREMENT &&
735 diff[2] > MAX_MEASUREMENT)
736 return false;
737
738 if (diff[0] <= diff[1] && diff[0] <= diff[2])
739 *mp_avg = (mp_coeff[0] + mp_coeff[1]) / 2;
740 else if (diff[1] <= diff[2])
741 *mp_avg = (mp_coeff[1] + mp_coeff[2]) / 2;
742 else
743 *mp_avg = (mp_coeff[2] + mp_coeff[0]) / 2;
744
745 return true;
746}
747
748static void ar9003_hw_tx_iqcal_load_avg_2_passes(struct ath_hw *ah,
749 u8 num_chains,
750 struct coeff *coeff)
751{
752 struct ath_common *common = ath9k_hw_common(ah);
753 int i, im, nmeasurement;
754 int magnitude, phase;
755 u32 tx_corr_coeff[MAX_MEASUREMENT][AR9300_MAX_CHAINS];
756
757 memset(tx_corr_coeff, 0, sizeof(tx_corr_coeff));
758 for (i = 0; i < MAX_MEASUREMENT / 2; i++) {
759 tx_corr_coeff[i * 2][0] = tx_corr_coeff[(i * 2) + 1][0] =
760 AR_PHY_TX_IQCAL_CORR_COEFF_B0(i);
761 if (!AR_SREV_9485(ah)) {
762 tx_corr_coeff[i * 2][1] =
763 tx_corr_coeff[(i * 2) + 1][1] =
764 AR_PHY_TX_IQCAL_CORR_COEFF_B1(i);
765
766 tx_corr_coeff[i * 2][2] =
767 tx_corr_coeff[(i * 2) + 1][2] =
768 AR_PHY_TX_IQCAL_CORR_COEFF_B2(i);
769 }
770 }
771
772 /* Load the average of 2 passes */
773 for (i = 0; i < num_chains; i++) {
774 if (AR_SREV_9485(ah))
775 nmeasurement = REG_READ_FIELD(ah,
776 AR_PHY_TX_IQCAL_STATUS_B0_9485,
777 AR_PHY_CALIBRATED_GAINS_0);
778 else
779 nmeasurement = REG_READ_FIELD(ah,
780 AR_PHY_TX_IQCAL_STATUS_B0,
781 AR_PHY_CALIBRATED_GAINS_0);
782
783 if (nmeasurement > MAX_MEASUREMENT)
784 nmeasurement = MAX_MEASUREMENT;
785
786 for (im = 0; im < nmeasurement; im++) {
787 /*
788 * Determine which 2 passes are closest and compute avg
789 * magnitude
790 */
791 if (!ar9003_hw_compute_closest_pass_and_avg(coeff->mag_coeff[i][im],
792 &magnitude))
793 goto disable_txiqcal;
794
795 /*
796 * Determine which 2 passes are closest and compute avg
797 * phase
798 */
799 if (!ar9003_hw_compute_closest_pass_and_avg(coeff->phs_coeff[i][im],
800 &phase))
801 goto disable_txiqcal;
802
803 coeff->iqc_coeff[0] = (magnitude & 0x7f) |
804 ((phase & 0x7f) << 7);
805
806 if ((im % 2) == 0)
807 REG_RMW_FIELD(ah, tx_corr_coeff[im][i],
808 AR_PHY_TX_IQCAL_CORR_COEFF_00_COEFF_TABLE,
809 coeff->iqc_coeff[0]);
810 else
811 REG_RMW_FIELD(ah, tx_corr_coeff[im][i],
812 AR_PHY_TX_IQCAL_CORR_COEFF_01_COEFF_TABLE,
813 coeff->iqc_coeff[0]);
814 }
815 }
816
817 REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_CONTROL_3,
818 AR_PHY_TX_IQCAL_CONTROL_3_IQCORR_EN, 0x1);
819 REG_RMW_FIELD(ah, AR_PHY_RX_IQCAL_CORR_B0,
820 AR_PHY_RX_IQCAL_CORR_B0_LOOPBACK_IQCORR_EN, 0x1);
821
822 return;
823
824disable_txiqcal:
825 REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_CONTROL_3,
826 AR_PHY_TX_IQCAL_CONTROL_3_IQCORR_EN, 0x0);
827 REG_RMW_FIELD(ah, AR_PHY_RX_IQCAL_CORR_B0,
828 AR_PHY_RX_IQCAL_CORR_B0_LOOPBACK_IQCORR_EN, 0x0);
829
830 ath_dbg(common, ATH_DBG_CALIBRATE, "TX IQ Cal disabled\n");
831}
832
833static void ar9003_hw_tx_iq_cal_run(struct ath_hw *ah)
834{
835 u8 tx_gain_forced;
836
837 REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_CONTROL_1_9485,
838 AR_PHY_TX_IQCAQL_CONTROL_1_IQCORR_I_Q_COFF_DELPT, DELPT);
839 tx_gain_forced = REG_READ_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
840 AR_PHY_TXGAIN_FORCE);
841 if (tx_gain_forced)
842 REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
843 AR_PHY_TXGAIN_FORCE, 0);
844
845 REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_START_9485,
846 AR_PHY_TX_IQCAL_START_DO_CAL_9485, 1);
847}
848
849static void ar9003_hw_tx_iq_cal_post_proc(struct ath_hw *ah)
850{
851 struct ath_common *common = ath9k_hw_common(ah);
852 const u32 txiqcal_status[AR9300_MAX_CHAINS] = {
853 AR_PHY_TX_IQCAL_STATUS_B0_9485,
854 AR_PHY_TX_IQCAL_STATUS_B1,
855 AR_PHY_TX_IQCAL_STATUS_B2,
856 };
857 const u_int32_t chan_info_tab[] = {
858 AR_PHY_CHAN_INFO_TAB_0,
859 AR_PHY_CHAN_INFO_TAB_1,
860 AR_PHY_CHAN_INFO_TAB_2,
861 };
862 struct coeff coeff;
863 s32 iq_res[6];
864 u8 num_chains = 0;
865 int i, ip, im, j;
866 int nmeasurement;
867
868 for (i = 0; i < AR9300_MAX_CHAINS; i++) {
869 if (ah->txchainmask & (1 << i))
870 num_chains++;
871 }
872
873 for (ip = 0; ip < MPASS; ip++) {
874 for (i = 0; i < num_chains; i++) {
875 nmeasurement = REG_READ_FIELD(ah,
876 AR_PHY_TX_IQCAL_STATUS_B0_9485,
877 AR_PHY_CALIBRATED_GAINS_0);
878 if (nmeasurement > MAX_MEASUREMENT)
879 nmeasurement = MAX_MEASUREMENT;
880
881 for (im = 0; im < nmeasurement; im++) {
882 ath_dbg(common, ATH_DBG_CALIBRATE,
883 "Doing Tx IQ Cal for chain %d.\n", i);
884
885 if (REG_READ(ah, txiqcal_status[i]) &
886 AR_PHY_TX_IQCAL_STATUS_FAILED) {
887 ath_dbg(common, ATH_DBG_CALIBRATE,
888 "Tx IQ Cal failed for chain %d.\n", i);
889 goto tx_iqcal_fail;
890 }
891
892 for (j = 0; j < 3; j++) {
893 u32 idx = 2 * j, offset = 4 * (3 * im + j);
894
895 REG_RMW_FIELD(ah,
896 AR_PHY_CHAN_INFO_MEMORY,
897 AR_PHY_CHAN_INFO_TAB_S2_READ,
898 0);
899
900 /* 32 bits */
901 iq_res[idx] = REG_READ(ah,
902 chan_info_tab[i] +
903 offset);
904
905 REG_RMW_FIELD(ah,
906 AR_PHY_CHAN_INFO_MEMORY,
907 AR_PHY_CHAN_INFO_TAB_S2_READ,
908 1);
909
910 /* 16 bits */
911 iq_res[idx + 1] = 0xffff & REG_READ(ah,
912 chan_info_tab[i] + offset);
913
914 ath_dbg(common, ATH_DBG_CALIBRATE,
915 "IQ RES[%d]=0x%x"
916 "IQ_RES[%d]=0x%x\n",
917 idx, iq_res[idx], idx + 1,
918 iq_res[idx + 1]);
919 }
920
921 if (!ar9003_hw_calc_iq_corr(ah, i, iq_res,
922 coeff.iqc_coeff)) {
923 ath_dbg(common, ATH_DBG_CALIBRATE,
924 "Failed in calculation of IQ correction.\n");
925 goto tx_iqcal_fail;
926 }
927
928 coeff.mag_coeff[i][im][ip] =
929 coeff.iqc_coeff[0] & 0x7f;
930 coeff.phs_coeff[i][im][ip] =
931 (coeff.iqc_coeff[0] >> 7) & 0x7f;
932
933 if (coeff.mag_coeff[i][im][ip] > 63)
934 coeff.mag_coeff[i][im][ip] -= 128;
935 if (coeff.phs_coeff[i][im][ip] > 63)
936 coeff.phs_coeff[i][im][ip] -= 128;
937 }
938 }
939 }
940 ar9003_hw_tx_iqcal_load_avg_2_passes(ah, num_chains, &coeff);
941
942 return;
943
944tx_iqcal_fail:
945 ath_dbg(common, ATH_DBG_CALIBRATE, "Tx IQ Cal failed\n");
946 return;
947}
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400948static bool ar9003_hw_init_cal(struct ath_hw *ah,
949 struct ath9k_channel *chan)
950{
951 struct ath_common *common = ath9k_hw_common(ah);
Senthil Balasubramaniana9d85fb2010-11-11 00:40:33 -0800952 int val;
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400953
Senthil Balasubramaniana9d85fb2010-11-11 00:40:33 -0800954 val = REG_READ(ah, AR_ENT_OTP);
Joe Perches226afe62010-12-02 19:12:37 -0800955 ath_dbg(common, ATH_DBG_CALIBRATE, "ath9k: AR_ENT_OTP 0x%x\n", val);
Senthil Balasubramaniana9d85fb2010-11-11 00:40:33 -0800956
Vasanthakumar Thiagarajan6559e832010-12-06 04:27:54 -0800957 if (AR_SREV_9485(ah))
958 ar9003_hw_set_chain_masks(ah, 0x1, 0x1);
959 else if (val & AR_ENT_OTP_CHAIN2_DISABLE)
Senthil Balasubramaniana9d85fb2010-11-11 00:40:33 -0800960 ar9003_hw_set_chain_masks(ah, 0x3, 0x3);
961 else
962 /*
963 * 0x7 = 0b111 , AR9003 needs to be configured for 3-chain
964 * mode before running AGC/TxIQ cals
965 */
966 ar9003_hw_set_chain_masks(ah, 0x7, 0x7);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400967
Luis R. Rodriguezc5395b62010-05-19 16:45:50 -0400968 /* Do Tx IQ Calibration */
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800969 if (AR_SREV_9485(ah))
970 ar9003_hw_tx_iq_cal_run(ah);
971 else
972 ar9003_hw_tx_iq_cal(ah);
973
Luis R. Rodriguezc5395b62010-05-19 16:45:50 -0400974 REG_WRITE(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_DIS);
975 udelay(5);
976 REG_WRITE(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_EN);
977
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400978 /* Calibrate the AGC */
979 REG_WRITE(ah, AR_PHY_AGC_CONTROL,
980 REG_READ(ah, AR_PHY_AGC_CONTROL) |
981 AR_PHY_AGC_CONTROL_CAL);
982
983 /* Poll for offset calibration complete */
984 if (!ath9k_hw_wait(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL,
985 0, AH_WAIT_TIMEOUT)) {
Joe Perches226afe62010-12-02 19:12:37 -0800986 ath_dbg(common, ATH_DBG_CALIBRATE,
987 "offset calibration failed to complete in 1ms; noisy environment?\n");
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400988 return false;
989 }
990
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800991 if (AR_SREV_9485(ah))
992 ar9003_hw_tx_iq_cal_post_proc(ah);
993
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400994 /* Revert chainmasks to their original values before NF cal */
995 ar9003_hw_set_chain_masks(ah, ah->rxchainmask, ah->txchainmask);
996
Felix Fietkau00c86592010-07-30 21:02:09 +0200997 ath9k_hw_start_nfcal(ah, true);
998
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400999 /* Initialize list pointers */
1000 ah->cal_list = ah->cal_list_last = ah->cal_list_curr = NULL;
Felix Fietkau64978272010-10-03 19:07:16 +02001001 ah->supp_cals = IQ_MISMATCH_CAL;
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -04001002
Felix Fietkau64978272010-10-03 19:07:16 +02001003 if (ah->supp_cals & IQ_MISMATCH_CAL) {
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -04001004 INIT_CAL(&ah->iq_caldata);
1005 INSERT_CAL(ah, &ah->iq_caldata);
Joe Perches226afe62010-12-02 19:12:37 -08001006 ath_dbg(common, ATH_DBG_CALIBRATE,
1007 "enabling IQ Calibration.\n");
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -04001008 }
1009
Felix Fietkau64978272010-10-03 19:07:16 +02001010 if (ah->supp_cals & TEMP_COMP_CAL) {
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -04001011 INIT_CAL(&ah->tempCompCalData);
1012 INSERT_CAL(ah, &ah->tempCompCalData);
Joe Perches226afe62010-12-02 19:12:37 -08001013 ath_dbg(common, ATH_DBG_CALIBRATE,
1014 "enabling Temperature Compensation Calibration.\n");
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -04001015 }
1016
1017 /* Initialize current pointer to first element in list */
1018 ah->cal_list_curr = ah->cal_list;
1019
1020 if (ah->cal_list_curr)
1021 ath9k_hw_reset_calibration(ah, ah->cal_list_curr);
1022
Felix Fietkau20bd2a02010-07-31 00:12:00 +02001023 if (ah->caldata)
1024 ah->caldata->CalValid = 0;
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -04001025
1026 return true;
Luis R. Rodriguez77d6d392010-04-15 17:39:09 -04001027}
1028
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -04001029void ar9003_hw_attach_calib_ops(struct ath_hw *ah)
1030{
1031 struct ath_hw_private_ops *priv_ops = ath9k_hw_private_ops(ah);
1032 struct ath_hw_ops *ops = ath9k_hw_ops(ah);
1033
1034 priv_ops->init_cal_settings = ar9003_hw_init_cal_settings;
1035 priv_ops->init_cal = ar9003_hw_init_cal;
1036 priv_ops->setup_calibration = ar9003_hw_setup_calibration;
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -04001037
1038 ops->calibrate = ar9003_hw_calibrate;
1039}