blob: 47f140ce00d2a051ccc258fe071544dc95da02bd [file] [log] [blame]
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -04001/*
Sujith Manoharan5b681382011-05-17 13:36:18 +05302 * Copyright (c) 2010-2011 Atheros Communications Inc.
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -04003 *
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"
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +053020#include "ar9003_rtt.h"
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -040021
Rajkumar Manoharan5f0c04e2011-10-13 11:00:35 +053022#define MAX_MEASUREMENT MAX_IQCAL_MEASUREMENT
Rajkumar Manoharan3782c692011-04-24 21:34:39 +053023#define MAX_MAG_DELTA 11
24#define MAX_PHS_DELTA 10
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -080025
26struct coeff {
Rajkumar Manoharan3782c692011-04-24 21:34:39 +053027 int mag_coeff[AR9300_MAX_CHAINS][MAX_MEASUREMENT];
28 int phs_coeff[AR9300_MAX_CHAINS][MAX_MEASUREMENT];
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -080029 int iqc_coeff[2];
30};
31
Felix Fietkau64978272010-10-03 19:07:16 +020032enum ar9003_cal_types {
33 IQ_MISMATCH_CAL = BIT(0),
34 TEMP_COMP_CAL = BIT(1),
35};
36
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -040037static void ar9003_hw_setup_calibration(struct ath_hw *ah,
38 struct ath9k_cal_list *currCal)
39{
Luis R. Rodriguez4b019312010-04-15 17:39:10 -040040 struct ath_common *common = ath9k_hw_common(ah);
41
42 /* Select calibration to run */
43 switch (currCal->calData->calType) {
44 case IQ_MISMATCH_CAL:
45 /*
46 * Start calibration with
47 * 2^(INIT_IQCAL_LOG_COUNT_MAX+1) samples
48 */
49 REG_RMW_FIELD(ah, AR_PHY_TIMING4,
50 AR_PHY_TIMING4_IQCAL_LOG_COUNT_MAX,
51 currCal->calData->calCountMax);
52 REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_IQ);
53
Joe Perches226afe62010-12-02 19:12:37 -080054 ath_dbg(common, ATH_DBG_CALIBRATE,
55 "starting IQ Mismatch Calibration\n");
Luis R. Rodriguez4b019312010-04-15 17:39:10 -040056
57 /* Kick-off cal */
58 REG_SET_BIT(ah, AR_PHY_TIMING4, AR_PHY_TIMING4_DO_CAL);
59 break;
60 case TEMP_COMP_CAL:
61 REG_RMW_FIELD(ah, AR_PHY_65NM_CH0_THERM,
62 AR_PHY_65NM_CH0_THERM_LOCAL, 1);
63 REG_RMW_FIELD(ah, AR_PHY_65NM_CH0_THERM,
64 AR_PHY_65NM_CH0_THERM_START, 1);
65
Joe Perches226afe62010-12-02 19:12:37 -080066 ath_dbg(common, ATH_DBG_CALIBRATE,
67 "starting Temperature Compensation Calibration\n");
Luis R. Rodriguez4b019312010-04-15 17:39:10 -040068 break;
Luis R. Rodriguez4b019312010-04-15 17:39:10 -040069 }
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -040070}
71
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -040072/*
73 * Generic calibration routine.
74 * Recalibrate the lower PHY chips to account for temperature/environment
75 * changes.
76 */
77static bool ar9003_hw_per_calibration(struct ath_hw *ah,
78 struct ath9k_channel *ichan,
79 u8 rxchainmask,
80 struct ath9k_cal_list *currCal)
81{
Felix Fietkau20bd2a02010-07-31 00:12:00 +020082 struct ath9k_hw_cal_data *caldata = ah->caldata;
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -040083 /* Cal is assumed not done until explicitly set below */
84 bool iscaldone = false;
85
86 /* Calibration in progress. */
87 if (currCal->calState == CAL_RUNNING) {
88 /* Check to see if it has finished. */
89 if (!(REG_READ(ah, AR_PHY_TIMING4) & AR_PHY_TIMING4_DO_CAL)) {
90 /*
91 * Accumulate cal measures for active chains
92 */
93 currCal->calData->calCollect(ah);
94 ah->cal_samples++;
95
96 if (ah->cal_samples >=
97 currCal->calData->calNumSamples) {
98 unsigned int i, numChains = 0;
99 for (i = 0; i < AR9300_MAX_CHAINS; i++) {
100 if (rxchainmask & (1 << i))
101 numChains++;
102 }
103
104 /*
105 * Process accumulated data
106 */
107 currCal->calData->calPostProc(ah, numChains);
108
109 /* Calibration has finished. */
Felix Fietkau20bd2a02010-07-31 00:12:00 +0200110 caldata->CalValid |= currCal->calData->calType;
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400111 currCal->calState = CAL_DONE;
112 iscaldone = true;
113 } else {
114 /*
115 * Set-up collection of another sub-sample until we
116 * get desired number
117 */
118 ar9003_hw_setup_calibration(ah, currCal);
119 }
120 }
Felix Fietkau20bd2a02010-07-31 00:12:00 +0200121 } else if (!(caldata->CalValid & currCal->calData->calType)) {
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400122 /* If current cal is marked invalid in channel, kick it off */
123 ath9k_hw_reset_calibration(ah, currCal);
124 }
125
126 return iscaldone;
127}
128
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -0400129static bool ar9003_hw_calibrate(struct ath_hw *ah,
130 struct ath9k_channel *chan,
131 u8 rxchainmask,
132 bool longcal)
133{
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400134 bool iscaldone = true;
135 struct ath9k_cal_list *currCal = ah->cal_list_curr;
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -0400136
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400137 /*
138 * For given calibration:
139 * 1. Call generic cal routine
140 * 2. When this cal is done (isCalDone) if we have more cals waiting
141 * (eg after reset), mask this to upper layers by not propagating
142 * isCalDone if it is set to TRUE.
143 * Instead, change isCalDone to FALSE and setup the waiting cal(s)
144 * to be run.
145 */
146 if (currCal &&
147 (currCal->calState == CAL_RUNNING ||
148 currCal->calState == CAL_WAITING)) {
149 iscaldone = ar9003_hw_per_calibration(ah, chan,
150 rxchainmask, currCal);
151 if (iscaldone) {
152 ah->cal_list_curr = currCal = currCal->calNext;
153
154 if (currCal->calState == CAL_WAITING) {
155 iscaldone = false;
156 ath9k_hw_reset_calibration(ah, currCal);
157 }
158 }
159 }
160
161 /* Do NF cal only at longer intervals */
162 if (longcal) {
163 /*
Felix Fietkau93697462010-07-30 21:02:10 +0200164 * Get the value from the previous NF cal and update
165 * history buffer.
166 */
167 ath9k_hw_getnf(ah, chan);
168
169 /*
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400170 * Load the NF from history buffer of the current channel.
171 * NF is slow time-variant, so it is OK to use a historical
172 * value.
173 */
174 ath9k_hw_loadnf(ah, ah->curchan);
175
176 /* start NF calibration, without updating BB NF register */
Felix Fietkau00c86592010-07-30 21:02:09 +0200177 ath9k_hw_start_nfcal(ah, false);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400178 }
179
180 return iscaldone;
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -0400181}
182
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400183static void ar9003_hw_iqcal_collect(struct ath_hw *ah)
184{
185 int i;
186
187 /* Accumulate IQ cal measures for active chains */
188 for (i = 0; i < AR5416_MAX_CHAINS; i++) {
Vasanthakumar Thiagarajan5d48ae72011-04-19 19:29:16 +0530189 if (ah->txchainmask & BIT(i)) {
190 ah->totalPowerMeasI[i] +=
191 REG_READ(ah, AR_PHY_CAL_MEAS_0(i));
192 ah->totalPowerMeasQ[i] +=
193 REG_READ(ah, AR_PHY_CAL_MEAS_1(i));
194 ah->totalIqCorrMeas[i] +=
195 (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_2(i));
196 ath_dbg(ath9k_hw_common(ah), ATH_DBG_CALIBRATE,
197 "%d: Chn %d pmi=0x%08x;pmq=0x%08x;iqcm=0x%08x;\n",
198 ah->cal_samples, i, ah->totalPowerMeasI[i],
199 ah->totalPowerMeasQ[i],
200 ah->totalIqCorrMeas[i]);
201 }
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400202 }
203}
204
205static void ar9003_hw_iqcalibrate(struct ath_hw *ah, u8 numChains)
206{
207 struct ath_common *common = ath9k_hw_common(ah);
208 u32 powerMeasQ, powerMeasI, iqCorrMeas;
209 u32 qCoffDenom, iCoffDenom;
210 int32_t qCoff, iCoff;
211 int iqCorrNeg, i;
Joe Perches07b2fa52010-11-20 18:38:53 -0800212 static const u_int32_t offset_array[3] = {
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400213 AR_PHY_RX_IQCAL_CORR_B0,
214 AR_PHY_RX_IQCAL_CORR_B1,
215 AR_PHY_RX_IQCAL_CORR_B2,
216 };
217
218 for (i = 0; i < numChains; i++) {
219 powerMeasI = ah->totalPowerMeasI[i];
220 powerMeasQ = ah->totalPowerMeasQ[i];
221 iqCorrMeas = ah->totalIqCorrMeas[i];
222
Joe Perches226afe62010-12-02 19:12:37 -0800223 ath_dbg(common, ATH_DBG_CALIBRATE,
224 "Starting IQ Cal and Correction for Chain %d\n",
225 i);
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400226
Joe Perches226afe62010-12-02 19:12:37 -0800227 ath_dbg(common, ATH_DBG_CALIBRATE,
228 "Orignal: Chn %diq_corr_meas = 0x%08x\n",
229 i, ah->totalIqCorrMeas[i]);
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400230
231 iqCorrNeg = 0;
232
233 if (iqCorrMeas > 0x80000000) {
234 iqCorrMeas = (0xffffffff - iqCorrMeas) + 1;
235 iqCorrNeg = 1;
236 }
237
Joe Perches226afe62010-12-02 19:12:37 -0800238 ath_dbg(common, ATH_DBG_CALIBRATE,
239 "Chn %d pwr_meas_i = 0x%08x\n", i, powerMeasI);
240 ath_dbg(common, ATH_DBG_CALIBRATE,
241 "Chn %d pwr_meas_q = 0x%08x\n", i, powerMeasQ);
242 ath_dbg(common, ATH_DBG_CALIBRATE, "iqCorrNeg is 0x%08x\n",
243 iqCorrNeg);
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400244
245 iCoffDenom = (powerMeasI / 2 + powerMeasQ / 2) / 256;
246 qCoffDenom = powerMeasQ / 64;
247
248 if ((iCoffDenom != 0) && (qCoffDenom != 0)) {
249 iCoff = iqCorrMeas / iCoffDenom;
250 qCoff = powerMeasI / qCoffDenom - 64;
Joe Perches226afe62010-12-02 19:12:37 -0800251 ath_dbg(common, ATH_DBG_CALIBRATE,
252 "Chn %d iCoff = 0x%08x\n", i, iCoff);
253 ath_dbg(common, ATH_DBG_CALIBRATE,
254 "Chn %d qCoff = 0x%08x\n", i, qCoff);
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400255
256 /* Force bounds on iCoff */
257 if (iCoff >= 63)
258 iCoff = 63;
259 else if (iCoff <= -63)
260 iCoff = -63;
261
262 /* Negate iCoff if iqCorrNeg == 0 */
263 if (iqCorrNeg == 0x0)
264 iCoff = -iCoff;
265
266 /* Force bounds on qCoff */
267 if (qCoff >= 63)
268 qCoff = 63;
269 else if (qCoff <= -63)
270 qCoff = -63;
271
272 iCoff = iCoff & 0x7f;
273 qCoff = qCoff & 0x7f;
274
Joe Perches226afe62010-12-02 19:12:37 -0800275 ath_dbg(common, ATH_DBG_CALIBRATE,
276 "Chn %d : iCoff = 0x%x qCoff = 0x%x\n",
277 i, iCoff, qCoff);
278 ath_dbg(common, ATH_DBG_CALIBRATE,
279 "Register offset (0x%04x) before update = 0x%x\n",
280 offset_array[i],
281 REG_READ(ah, offset_array[i]));
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400282
283 REG_RMW_FIELD(ah, offset_array[i],
284 AR_PHY_RX_IQCAL_CORR_IQCORR_Q_I_COFF,
285 iCoff);
286 REG_RMW_FIELD(ah, offset_array[i],
287 AR_PHY_RX_IQCAL_CORR_IQCORR_Q_Q_COFF,
288 qCoff);
Joe Perches226afe62010-12-02 19:12:37 -0800289 ath_dbg(common, ATH_DBG_CALIBRATE,
290 "Register offset (0x%04x) QI COFF (bitfields 0x%08x) after update = 0x%x\n",
291 offset_array[i],
292 AR_PHY_RX_IQCAL_CORR_IQCORR_Q_I_COFF,
293 REG_READ(ah, offset_array[i]));
294 ath_dbg(common, ATH_DBG_CALIBRATE,
295 "Register offset (0x%04x) QQ COFF (bitfields 0x%08x) after update = 0x%x\n",
296 offset_array[i],
297 AR_PHY_RX_IQCAL_CORR_IQCORR_Q_Q_COFF,
298 REG_READ(ah, offset_array[i]));
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400299
Joe Perches226afe62010-12-02 19:12:37 -0800300 ath_dbg(common, ATH_DBG_CALIBRATE,
301 "IQ Cal and Correction done for Chain %d\n", i);
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400302 }
303 }
304
305 REG_SET_BIT(ah, AR_PHY_RX_IQCAL_CORR_B0,
306 AR_PHY_RX_IQCAL_CORR_IQCORR_ENABLE);
Joe Perches226afe62010-12-02 19:12:37 -0800307 ath_dbg(common, ATH_DBG_CALIBRATE,
308 "IQ Cal and Correction (offset 0x%04x) enabled (bit position 0x%08x). New Value 0x%08x\n",
309 (unsigned) (AR_PHY_RX_IQCAL_CORR_B0),
310 AR_PHY_RX_IQCAL_CORR_IQCORR_ENABLE,
311 REG_READ(ah, AR_PHY_RX_IQCAL_CORR_B0));
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400312}
313
314static const struct ath9k_percal_data iq_cal_single_sample = {
315 IQ_MISMATCH_CAL,
316 MIN_CAL_SAMPLES,
317 PER_MAX_LOG_COUNT,
318 ar9003_hw_iqcal_collect,
319 ar9003_hw_iqcalibrate
320};
321
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -0400322static void ar9003_hw_init_cal_settings(struct ath_hw *ah)
323{
Luis R. Rodriguez590b7d22010-04-15 17:39:01 -0400324 ah->iq_caldata.calData = &iq_cal_single_sample;
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -0400325}
326
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400327/*
328 * solve 4x4 linear equation used in loopback iq cal.
329 */
330static bool ar9003_hw_solve_iq_cal(struct ath_hw *ah,
331 s32 sin_2phi_1,
332 s32 cos_2phi_1,
333 s32 sin_2phi_2,
334 s32 cos_2phi_2,
335 s32 mag_a0_d0,
336 s32 phs_a0_d0,
337 s32 mag_a1_d0,
338 s32 phs_a1_d0,
339 s32 solved_eq[])
Luis R. Rodriguez77d6d392010-04-15 17:39:09 -0400340{
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400341 s32 f1 = cos_2phi_1 - cos_2phi_2,
342 f3 = sin_2phi_1 - sin_2phi_2,
343 f2;
344 s32 mag_tx, phs_tx, mag_rx, phs_rx;
345 const s32 result_shift = 1 << 15;
346 struct ath_common *common = ath9k_hw_common(ah);
347
348 f2 = (f1 * f1 + f3 * f3) / result_shift;
349
350 if (!f2) {
Joe Perches226afe62010-12-02 19:12:37 -0800351 ath_dbg(common, ATH_DBG_CALIBRATE, "Divide by 0\n");
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400352 return false;
353 }
354
355 /* mag mismatch, tx */
356 mag_tx = f1 * (mag_a0_d0 - mag_a1_d0) + f3 * (phs_a0_d0 - phs_a1_d0);
357 /* phs mismatch, tx */
358 phs_tx = f3 * (-mag_a0_d0 + mag_a1_d0) + f1 * (phs_a0_d0 - phs_a1_d0);
359
360 mag_tx = (mag_tx / f2);
361 phs_tx = (phs_tx / f2);
362
363 /* mag mismatch, rx */
364 mag_rx = mag_a0_d0 - (cos_2phi_1 * mag_tx + sin_2phi_1 * phs_tx) /
365 result_shift;
366 /* phs mismatch, rx */
367 phs_rx = phs_a0_d0 + (sin_2phi_1 * mag_tx - cos_2phi_1 * phs_tx) /
368 result_shift;
369
370 solved_eq[0] = mag_tx;
371 solved_eq[1] = phs_tx;
372 solved_eq[2] = mag_rx;
373 solved_eq[3] = phs_rx;
374
375 return true;
376}
377
378static s32 ar9003_hw_find_mag_approx(struct ath_hw *ah, s32 in_re, s32 in_im)
379{
380 s32 abs_i = abs(in_re),
381 abs_q = abs(in_im),
382 max_abs, min_abs;
383
384 if (abs_i > abs_q) {
385 max_abs = abs_i;
386 min_abs = abs_q;
387 } else {
388 max_abs = abs_q;
389 min_abs = abs_i;
390 }
391
392 return max_abs - (max_abs / 32) + (min_abs / 8) + (min_abs / 4);
393}
394
395#define DELPT 32
396
397static bool ar9003_hw_calc_iq_corr(struct ath_hw *ah,
398 s32 chain_idx,
399 const s32 iq_res[],
400 s32 iqc_coeff[])
401{
402 s32 i2_m_q2_a0_d0, i2_p_q2_a0_d0, iq_corr_a0_d0,
403 i2_m_q2_a0_d1, i2_p_q2_a0_d1, iq_corr_a0_d1,
404 i2_m_q2_a1_d0, i2_p_q2_a1_d0, iq_corr_a1_d0,
405 i2_m_q2_a1_d1, i2_p_q2_a1_d1, iq_corr_a1_d1;
406 s32 mag_a0_d0, mag_a1_d0, mag_a0_d1, mag_a1_d1,
407 phs_a0_d0, phs_a1_d0, phs_a0_d1, phs_a1_d1,
408 sin_2phi_1, cos_2phi_1,
409 sin_2phi_2, cos_2phi_2;
410 s32 mag_tx, phs_tx, mag_rx, phs_rx;
411 s32 solved_eq[4], mag_corr_tx, phs_corr_tx, mag_corr_rx, phs_corr_rx,
412 q_q_coff, q_i_coff;
413 const s32 res_scale = 1 << 15;
414 const s32 delpt_shift = 1 << 8;
415 s32 mag1, mag2;
416 struct ath_common *common = ath9k_hw_common(ah);
417
418 i2_m_q2_a0_d0 = iq_res[0] & 0xfff;
419 i2_p_q2_a0_d0 = (iq_res[0] >> 12) & 0xfff;
420 iq_corr_a0_d0 = ((iq_res[0] >> 24) & 0xff) + ((iq_res[1] & 0xf) << 8);
421
422 if (i2_m_q2_a0_d0 > 0x800)
423 i2_m_q2_a0_d0 = -((0xfff - i2_m_q2_a0_d0) + 1);
424
425 if (i2_p_q2_a0_d0 > 0x800)
426 i2_p_q2_a0_d0 = -((0xfff - i2_p_q2_a0_d0) + 1);
427
428 if (iq_corr_a0_d0 > 0x800)
429 iq_corr_a0_d0 = -((0xfff - iq_corr_a0_d0) + 1);
430
431 i2_m_q2_a0_d1 = (iq_res[1] >> 4) & 0xfff;
432 i2_p_q2_a0_d1 = (iq_res[2] & 0xfff);
433 iq_corr_a0_d1 = (iq_res[2] >> 12) & 0xfff;
434
435 if (i2_m_q2_a0_d1 > 0x800)
436 i2_m_q2_a0_d1 = -((0xfff - i2_m_q2_a0_d1) + 1);
437
438 if (i2_p_q2_a0_d1 > 0x800)
439 i2_p_q2_a0_d1 = -((0xfff - i2_p_q2_a0_d1) + 1);
440
441 if (iq_corr_a0_d1 > 0x800)
442 iq_corr_a0_d1 = -((0xfff - iq_corr_a0_d1) + 1);
443
444 i2_m_q2_a1_d0 = ((iq_res[2] >> 24) & 0xff) + ((iq_res[3] & 0xf) << 8);
445 i2_p_q2_a1_d0 = (iq_res[3] >> 4) & 0xfff;
446 iq_corr_a1_d0 = iq_res[4] & 0xfff;
447
448 if (i2_m_q2_a1_d0 > 0x800)
449 i2_m_q2_a1_d0 = -((0xfff - i2_m_q2_a1_d0) + 1);
450
451 if (i2_p_q2_a1_d0 > 0x800)
452 i2_p_q2_a1_d0 = -((0xfff - i2_p_q2_a1_d0) + 1);
453
454 if (iq_corr_a1_d0 > 0x800)
455 iq_corr_a1_d0 = -((0xfff - iq_corr_a1_d0) + 1);
456
457 i2_m_q2_a1_d1 = (iq_res[4] >> 12) & 0xfff;
458 i2_p_q2_a1_d1 = ((iq_res[4] >> 24) & 0xff) + ((iq_res[5] & 0xf) << 8);
459 iq_corr_a1_d1 = (iq_res[5] >> 4) & 0xfff;
460
461 if (i2_m_q2_a1_d1 > 0x800)
462 i2_m_q2_a1_d1 = -((0xfff - i2_m_q2_a1_d1) + 1);
463
464 if (i2_p_q2_a1_d1 > 0x800)
465 i2_p_q2_a1_d1 = -((0xfff - i2_p_q2_a1_d1) + 1);
466
467 if (iq_corr_a1_d1 > 0x800)
468 iq_corr_a1_d1 = -((0xfff - iq_corr_a1_d1) + 1);
469
470 if ((i2_p_q2_a0_d0 == 0) || (i2_p_q2_a0_d1 == 0) ||
471 (i2_p_q2_a1_d0 == 0) || (i2_p_q2_a1_d1 == 0)) {
Joe Perches226afe62010-12-02 19:12:37 -0800472 ath_dbg(common, ATH_DBG_CALIBRATE,
473 "Divide by 0:\n"
474 "a0_d0=%d\n"
475 "a0_d1=%d\n"
476 "a2_d0=%d\n"
477 "a1_d1=%d\n",
478 i2_p_q2_a0_d0, i2_p_q2_a0_d1,
479 i2_p_q2_a1_d0, i2_p_q2_a1_d1);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400480 return false;
481 }
482
483 mag_a0_d0 = (i2_m_q2_a0_d0 * res_scale) / i2_p_q2_a0_d0;
484 phs_a0_d0 = (iq_corr_a0_d0 * res_scale) / i2_p_q2_a0_d0;
485
486 mag_a0_d1 = (i2_m_q2_a0_d1 * res_scale) / i2_p_q2_a0_d1;
487 phs_a0_d1 = (iq_corr_a0_d1 * res_scale) / i2_p_q2_a0_d1;
488
489 mag_a1_d0 = (i2_m_q2_a1_d0 * res_scale) / i2_p_q2_a1_d0;
490 phs_a1_d0 = (iq_corr_a1_d0 * res_scale) / i2_p_q2_a1_d0;
491
492 mag_a1_d1 = (i2_m_q2_a1_d1 * res_scale) / i2_p_q2_a1_d1;
493 phs_a1_d1 = (iq_corr_a1_d1 * res_scale) / i2_p_q2_a1_d1;
494
495 /* w/o analog phase shift */
496 sin_2phi_1 = (((mag_a0_d0 - mag_a0_d1) * delpt_shift) / DELPT);
497 /* w/o analog phase shift */
498 cos_2phi_1 = (((phs_a0_d1 - phs_a0_d0) * delpt_shift) / DELPT);
499 /* w/ analog phase shift */
500 sin_2phi_2 = (((mag_a1_d0 - mag_a1_d1) * delpt_shift) / DELPT);
501 /* w/ analog phase shift */
502 cos_2phi_2 = (((phs_a1_d1 - phs_a1_d0) * delpt_shift) / DELPT);
503
504 /*
505 * force sin^2 + cos^2 = 1;
506 * find magnitude by approximation
507 */
508 mag1 = ar9003_hw_find_mag_approx(ah, cos_2phi_1, sin_2phi_1);
509 mag2 = ar9003_hw_find_mag_approx(ah, cos_2phi_2, sin_2phi_2);
510
511 if ((mag1 == 0) || (mag2 == 0)) {
Joe Perches226afe62010-12-02 19:12:37 -0800512 ath_dbg(common, ATH_DBG_CALIBRATE,
513 "Divide by 0: mag1=%d, mag2=%d\n",
514 mag1, mag2);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400515 return false;
516 }
517
518 /* normalization sin and cos by mag */
519 sin_2phi_1 = (sin_2phi_1 * res_scale / mag1);
520 cos_2phi_1 = (cos_2phi_1 * res_scale / mag1);
521 sin_2phi_2 = (sin_2phi_2 * res_scale / mag2);
522 cos_2phi_2 = (cos_2phi_2 * res_scale / mag2);
523
524 /* calculate IQ mismatch */
525 if (!ar9003_hw_solve_iq_cal(ah,
526 sin_2phi_1, cos_2phi_1,
527 sin_2phi_2, cos_2phi_2,
528 mag_a0_d0, phs_a0_d0,
529 mag_a1_d0,
530 phs_a1_d0, solved_eq)) {
Joe Perches226afe62010-12-02 19:12:37 -0800531 ath_dbg(common, ATH_DBG_CALIBRATE,
532 "Call to ar9003_hw_solve_iq_cal() failed.\n");
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400533 return false;
534 }
535
536 mag_tx = solved_eq[0];
537 phs_tx = solved_eq[1];
538 mag_rx = solved_eq[2];
539 phs_rx = solved_eq[3];
540
Joe Perches226afe62010-12-02 19:12:37 -0800541 ath_dbg(common, ATH_DBG_CALIBRATE,
542 "chain %d: mag mismatch=%d phase mismatch=%d\n",
543 chain_idx, mag_tx/res_scale, phs_tx/res_scale);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400544
545 if (res_scale == mag_tx) {
Joe Perches226afe62010-12-02 19:12:37 -0800546 ath_dbg(common, ATH_DBG_CALIBRATE,
547 "Divide by 0: mag_tx=%d, res_scale=%d\n",
548 mag_tx, res_scale);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400549 return false;
550 }
551
552 /* calculate and quantize Tx IQ correction factor */
553 mag_corr_tx = (mag_tx * res_scale) / (res_scale - mag_tx);
554 phs_corr_tx = -phs_tx;
555
556 q_q_coff = (mag_corr_tx * 128 / res_scale);
557 q_i_coff = (phs_corr_tx * 256 / res_scale);
558
Joe Perches226afe62010-12-02 19:12:37 -0800559 ath_dbg(common, ATH_DBG_CALIBRATE,
560 "tx chain %d: mag corr=%d phase corr=%d\n",
561 chain_idx, q_q_coff, q_i_coff);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400562
563 if (q_i_coff < -63)
564 q_i_coff = -63;
565 if (q_i_coff > 63)
566 q_i_coff = 63;
567 if (q_q_coff < -63)
568 q_q_coff = -63;
569 if (q_q_coff > 63)
570 q_q_coff = 63;
571
572 iqc_coeff[0] = (q_q_coff * 128) + q_i_coff;
573
Joe Perches226afe62010-12-02 19:12:37 -0800574 ath_dbg(common, ATH_DBG_CALIBRATE,
575 "tx chain %d: iq corr coeff=%x\n",
576 chain_idx, iqc_coeff[0]);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400577
578 if (-mag_rx == res_scale) {
Joe Perches226afe62010-12-02 19:12:37 -0800579 ath_dbg(common, ATH_DBG_CALIBRATE,
580 "Divide by 0: mag_rx=%d, res_scale=%d\n",
581 mag_rx, res_scale);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400582 return false;
583 }
584
585 /* calculate and quantize Rx IQ correction factors */
586 mag_corr_rx = (-mag_rx * res_scale) / (res_scale + mag_rx);
587 phs_corr_rx = -phs_rx;
588
589 q_q_coff = (mag_corr_rx * 128 / res_scale);
590 q_i_coff = (phs_corr_rx * 256 / res_scale);
591
Joe Perches226afe62010-12-02 19:12:37 -0800592 ath_dbg(common, ATH_DBG_CALIBRATE,
593 "rx chain %d: mag corr=%d phase corr=%d\n",
594 chain_idx, q_q_coff, q_i_coff);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400595
596 if (q_i_coff < -63)
597 q_i_coff = -63;
598 if (q_i_coff > 63)
599 q_i_coff = 63;
600 if (q_q_coff < -63)
601 q_q_coff = -63;
602 if (q_q_coff > 63)
603 q_q_coff = 63;
604
605 iqc_coeff[1] = (q_q_coff * 128) + q_i_coff;
606
Joe Perches226afe62010-12-02 19:12:37 -0800607 ath_dbg(common, ATH_DBG_CALIBRATE,
608 "rx chain %d: iq corr coeff=%x\n",
609 chain_idx, iqc_coeff[1]);
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400610
611 return true;
612}
613
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530614static void ar9003_hw_detect_outlier(int *mp_coeff, int nmeasurement,
615 int max_delta)
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800616{
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530617 int mp_max = -64, max_idx = 0;
618 int mp_min = 63, min_idx = 0;
Rajkumar Manoharane948b992011-09-06 21:59:51 +0530619 int mp_avg = 0, i, outlier_idx = 0, mp_count = 0;
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800620
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530621 /* find min/max mismatch across all calibrated gains */
622 for (i = 0; i < nmeasurement; i++) {
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530623 if (mp_coeff[i] > mp_max) {
624 mp_max = mp_coeff[i];
625 max_idx = i;
626 } else if (mp_coeff[i] < mp_min) {
627 mp_min = mp_coeff[i];
628 min_idx = i;
629 }
630 }
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800631
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530632 /* find average (exclude max abs value) */
633 for (i = 0; i < nmeasurement; i++) {
634 if ((abs(mp_coeff[i]) < abs(mp_max)) ||
Rajkumar Manoharane948b992011-09-06 21:59:51 +0530635 (abs(mp_coeff[i]) < abs(mp_min))) {
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530636 mp_avg += mp_coeff[i];
Rajkumar Manoharane948b992011-09-06 21:59:51 +0530637 mp_count++;
638 }
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530639 }
Rajkumar Manoharane948b992011-09-06 21:59:51 +0530640
641 /*
642 * finding mean magnitude/phase if possible, otherwise
643 * just use the last value as the mean
644 */
645 if (mp_count)
646 mp_avg /= mp_count;
647 else
648 mp_avg = mp_coeff[nmeasurement - 1];
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800649
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530650 /* detect outlier */
651 if (abs(mp_max - mp_min) > max_delta) {
652 if (abs(mp_max - mp_avg) > abs(mp_min - mp_avg))
653 outlier_idx = max_idx;
654 else
655 outlier_idx = min_idx;
Rajkumar Manoharane9c10462011-09-15 19:02:25 +0530656
657 mp_coeff[outlier_idx] = mp_avg;
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530658 }
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800659}
660
661static void ar9003_hw_tx_iqcal_load_avg_2_passes(struct ath_hw *ah,
662 u8 num_chains,
Rajkumar Manoharan34013522011-10-13 11:00:36 +0530663 struct coeff *coeff,
664 bool is_reusable)
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800665{
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800666 int i, im, nmeasurement;
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800667 u32 tx_corr_coeff[MAX_MEASUREMENT][AR9300_MAX_CHAINS];
Rajkumar Manoharan5f0c04e2011-10-13 11:00:35 +0530668 struct ath9k_hw_cal_data *caldata = ah->caldata;
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800669
670 memset(tx_corr_coeff, 0, sizeof(tx_corr_coeff));
671 for (i = 0; i < MAX_MEASUREMENT / 2; i++) {
672 tx_corr_coeff[i * 2][0] = tx_corr_coeff[(i * 2) + 1][0] =
673 AR_PHY_TX_IQCAL_CORR_COEFF_B0(i);
674 if (!AR_SREV_9485(ah)) {
675 tx_corr_coeff[i * 2][1] =
676 tx_corr_coeff[(i * 2) + 1][1] =
677 AR_PHY_TX_IQCAL_CORR_COEFF_B1(i);
678
679 tx_corr_coeff[i * 2][2] =
680 tx_corr_coeff[(i * 2) + 1][2] =
681 AR_PHY_TX_IQCAL_CORR_COEFF_B2(i);
682 }
683 }
684
685 /* Load the average of 2 passes */
686 for (i = 0; i < num_chains; i++) {
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530687 nmeasurement = REG_READ_FIELD(ah,
688 AR_PHY_TX_IQCAL_STATUS_B0,
689 AR_PHY_CALIBRATED_GAINS_0);
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800690
691 if (nmeasurement > MAX_MEASUREMENT)
692 nmeasurement = MAX_MEASUREMENT;
693
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530694 /* detect outlier only if nmeasurement > 1 */
695 if (nmeasurement > 1) {
696 /* Detect magnitude outlier */
697 ar9003_hw_detect_outlier(coeff->mag_coeff[i],
698 nmeasurement, MAX_MAG_DELTA);
699
700 /* Detect phase outlier */
701 ar9003_hw_detect_outlier(coeff->phs_coeff[i],
702 nmeasurement, MAX_PHS_DELTA);
703 }
704
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800705 for (im = 0; im < nmeasurement; im++) {
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800706
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530707 coeff->iqc_coeff[0] = (coeff->mag_coeff[i][im] & 0x7f) |
708 ((coeff->phs_coeff[i][im] & 0x7f) << 7);
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800709
710 if ((im % 2) == 0)
711 REG_RMW_FIELD(ah, tx_corr_coeff[im][i],
712 AR_PHY_TX_IQCAL_CORR_COEFF_00_COEFF_TABLE,
713 coeff->iqc_coeff[0]);
714 else
715 REG_RMW_FIELD(ah, tx_corr_coeff[im][i],
716 AR_PHY_TX_IQCAL_CORR_COEFF_01_COEFF_TABLE,
717 coeff->iqc_coeff[0]);
Rajkumar Manoharan5f0c04e2011-10-13 11:00:35 +0530718
719 if (caldata)
720 caldata->tx_corr_coeff[im][i] =
721 coeff->iqc_coeff[0];
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800722 }
Rajkumar Manoharan5f0c04e2011-10-13 11:00:35 +0530723 if (caldata)
724 caldata->num_measures[i] = nmeasurement;
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800725 }
726
727 REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_CONTROL_3,
728 AR_PHY_TX_IQCAL_CONTROL_3_IQCORR_EN, 0x1);
729 REG_RMW_FIELD(ah, AR_PHY_RX_IQCAL_CORR_B0,
730 AR_PHY_RX_IQCAL_CORR_B0_LOOPBACK_IQCORR_EN, 0x1);
Rajkumar Manoharan34013522011-10-13 11:00:36 +0530731
Rajkumar Manoharan5f0c04e2011-10-13 11:00:35 +0530732 if (caldata)
Rajkumar Manoharan34013522011-10-13 11:00:36 +0530733 caldata->done_txiqcal_once = is_reusable;
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800734
735 return;
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800736}
737
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530738static bool ar9003_hw_tx_iq_cal_run(struct ath_hw *ah)
Vasanthakumar Thiagarajan0b2084b2010-12-15 07:30:50 -0800739{
740 struct ath_common *common = ath9k_hw_common(ah);
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800741 u8 tx_gain_forced;
742
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800743 tx_gain_forced = REG_READ_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
744 AR_PHY_TXGAIN_FORCE);
745 if (tx_gain_forced)
746 REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
747 AR_PHY_TXGAIN_FORCE, 0);
748
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530749 REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_START,
750 AR_PHY_TX_IQCAL_START_DO_CAL, 1);
751
752 if (!ath9k_hw_wait(ah, AR_PHY_TX_IQCAL_START,
753 AR_PHY_TX_IQCAL_START_DO_CAL, 0,
754 AH_WAIT_TIMEOUT)) {
755 ath_dbg(common, ATH_DBG_CALIBRATE,
756 "Tx IQ Cal is not completed.\n");
757 return false;
758 }
759 return true;
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800760}
761
Rajkumar Manoharan34013522011-10-13 11:00:36 +0530762static void ar9003_hw_tx_iq_cal_post_proc(struct ath_hw *ah, bool is_reusable)
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800763{
764 struct ath_common *common = ath9k_hw_common(ah);
765 const u32 txiqcal_status[AR9300_MAX_CHAINS] = {
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530766 AR_PHY_TX_IQCAL_STATUS_B0,
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800767 AR_PHY_TX_IQCAL_STATUS_B1,
768 AR_PHY_TX_IQCAL_STATUS_B2,
769 };
770 const u_int32_t chan_info_tab[] = {
771 AR_PHY_CHAN_INFO_TAB_0,
772 AR_PHY_CHAN_INFO_TAB_1,
773 AR_PHY_CHAN_INFO_TAB_2,
774 };
775 struct coeff coeff;
776 s32 iq_res[6];
777 u8 num_chains = 0;
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530778 int i, im, j;
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800779 int nmeasurement;
780
781 for (i = 0; i < AR9300_MAX_CHAINS; i++) {
782 if (ah->txchainmask & (1 << i))
783 num_chains++;
784 }
785
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530786 for (i = 0; i < num_chains; i++) {
787 nmeasurement = REG_READ_FIELD(ah,
788 AR_PHY_TX_IQCAL_STATUS_B0,
789 AR_PHY_CALIBRATED_GAINS_0);
790 if (nmeasurement > MAX_MEASUREMENT)
791 nmeasurement = MAX_MEASUREMENT;
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800792
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530793 for (im = 0; im < nmeasurement; im++) {
794 ath_dbg(common, ATH_DBG_CALIBRATE,
795 "Doing Tx IQ Cal for chain %d.\n", i);
796
797 if (REG_READ(ah, txiqcal_status[i]) &
798 AR_PHY_TX_IQCAL_STATUS_FAILED) {
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800799 ath_dbg(common, ATH_DBG_CALIBRATE,
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800800 "Tx IQ Cal failed for chain %d.\n", i);
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530801 goto tx_iqcal_fail;
802 }
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800803
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530804 for (j = 0; j < 3; j++) {
805 u32 idx = 2 * j, offset = 4 * (3 * im + j);
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800806
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530807 REG_RMW_FIELD(ah,
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800808 AR_PHY_CHAN_INFO_MEMORY,
809 AR_PHY_CHAN_INFO_TAB_S2_READ,
810 0);
811
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530812 /* 32 bits */
813 iq_res[idx] = REG_READ(ah,
814 chan_info_tab[i] +
815 offset);
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800816
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530817 REG_RMW_FIELD(ah,
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800818 AR_PHY_CHAN_INFO_MEMORY,
819 AR_PHY_CHAN_INFO_TAB_S2_READ,
820 1);
821
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530822 /* 16 bits */
823 iq_res[idx + 1] = 0xffff & REG_READ(ah,
824 chan_info_tab[i] + offset);
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800825
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530826 ath_dbg(common, ATH_DBG_CALIBRATE,
827 "IQ RES[%d]=0x%x"
828 "IQ_RES[%d]=0x%x\n",
829 idx, iq_res[idx], idx + 1,
830 iq_res[idx + 1]);
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800831 }
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530832
833 if (!ar9003_hw_calc_iq_corr(ah, i, iq_res,
834 coeff.iqc_coeff)) {
835 ath_dbg(common, ATH_DBG_CALIBRATE,
836 "Failed in calculation of \
837 IQ correction.\n");
838 goto tx_iqcal_fail;
839 }
840
841 coeff.mag_coeff[i][im] = coeff.iqc_coeff[0] & 0x7f;
842 coeff.phs_coeff[i][im] =
843 (coeff.iqc_coeff[0] >> 7) & 0x7f;
844
845 if (coeff.mag_coeff[i][im] > 63)
846 coeff.mag_coeff[i][im] -= 128;
847 if (coeff.phs_coeff[i][im] > 63)
848 coeff.phs_coeff[i][im] -= 128;
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800849 }
850 }
Rajkumar Manoharan34013522011-10-13 11:00:36 +0530851 ar9003_hw_tx_iqcal_load_avg_2_passes(ah, num_chains,
852 &coeff, is_reusable);
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800853
854 return;
855
856tx_iqcal_fail:
857 ath_dbg(common, ATH_DBG_CALIBRATE, "Tx IQ Cal failed\n");
858 return;
859}
Rajkumar Manoharan5f0c04e2011-10-13 11:00:35 +0530860
861static void ar9003_hw_tx_iq_cal_reload(struct ath_hw *ah)
862{
863 struct ath9k_hw_cal_data *caldata = ah->caldata;
864 u32 tx_corr_coeff[MAX_MEASUREMENT][AR9300_MAX_CHAINS];
865 int i, im;
866
867 memset(tx_corr_coeff, 0, sizeof(tx_corr_coeff));
868 for (i = 0; i < MAX_MEASUREMENT / 2; i++) {
869 tx_corr_coeff[i * 2][0] = tx_corr_coeff[(i * 2) + 1][0] =
870 AR_PHY_TX_IQCAL_CORR_COEFF_B0(i);
871 if (!AR_SREV_9485(ah)) {
872 tx_corr_coeff[i * 2][1] =
873 tx_corr_coeff[(i * 2) + 1][1] =
874 AR_PHY_TX_IQCAL_CORR_COEFF_B1(i);
875
876 tx_corr_coeff[i * 2][2] =
877 tx_corr_coeff[(i * 2) + 1][2] =
878 AR_PHY_TX_IQCAL_CORR_COEFF_B2(i);
879 }
880 }
881
882 for (i = 0; i < AR9300_MAX_CHAINS; i++) {
883 if (!(ah->txchainmask & (1 << i)))
884 continue;
885
886 for (im = 0; im < caldata->num_measures[i]; im++) {
887 if ((im % 2) == 0)
888 REG_RMW_FIELD(ah, tx_corr_coeff[im][i],
889 AR_PHY_TX_IQCAL_CORR_COEFF_00_COEFF_TABLE,
890 caldata->tx_corr_coeff[im][i]);
891 else
892 REG_RMW_FIELD(ah, tx_corr_coeff[im][i],
893 AR_PHY_TX_IQCAL_CORR_COEFF_01_COEFF_TABLE,
894 caldata->tx_corr_coeff[im][i]);
895 }
896 }
897
898 REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_CONTROL_3,
899 AR_PHY_TX_IQCAL_CONTROL_3_IQCORR_EN, 0x1);
900 REG_RMW_FIELD(ah, AR_PHY_RX_IQCAL_CORR_B0,
901 AR_PHY_RX_IQCAL_CORR_B0_LOOPBACK_IQCORR_EN, 0x1);
902}
903
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +0530904static bool ar9003_hw_rtt_restore(struct ath_hw *ah, struct ath9k_channel *chan)
905{
906 struct ath9k_rtt_hist *hist;
907 u32 *table;
908 int i;
909 bool restore;
910
911 if (!(ah->caps.hw_caps & ATH9K_HW_CAP_RTT) || !ah->caldata)
912 return false;
913
914 hist = &ah->caldata->rtt_hist;
915 ar9003_hw_rtt_enable(ah);
916 ar9003_hw_rtt_set_mask(ah, 0x10);
917 for (i = 0; i < AR9300_MAX_CHAINS; i++) {
918 if (!(ah->rxchainmask & (1 << i)))
919 continue;
920 table = &hist->table[i][hist->num_readings][0];
921 ar9003_hw_rtt_load_hist(ah, i, table);
922 }
923 restore = ar9003_hw_rtt_force_restore(ah);
924 ar9003_hw_rtt_disable(ah);
925
926 return restore;
927}
928
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400929static bool ar9003_hw_init_cal(struct ath_hw *ah,
930 struct ath9k_channel *chan)
931{
932 struct ath_common *common = ath9k_hw_common(ah);
Rajkumar Manoharan5f0c04e2011-10-13 11:00:35 +0530933 struct ath9k_hw_cal_data *caldata = ah->caldata;
Rajkumar Manoharan77a5a662011-10-13 11:00:37 +0530934 bool txiqcal_done = false, txclcal_done = false;
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +0530935 bool is_reusable = true, status = true;
936 bool run_rtt_cal = false, run_agc_cal;
937 bool rtt = !!(ah->caps.hw_caps & ATH9K_HW_CAP_RTT);
938 u32 agc_ctrl = 0, agc_supp_cals = AR_PHY_AGC_CONTROL_OFFSET_CAL |
939 AR_PHY_AGC_CONTROL_FLTR_CAL |
940 AR_PHY_AGC_CONTROL_PKDET_CAL;
Rajkumar Manoharan8ad74c42011-10-13 11:00:38 +0530941 int i, j;
Rajkumar Manoharan77a5a662011-10-13 11:00:37 +0530942 u32 cl_idx[AR9300_MAX_CHAINS] = { AR_PHY_CL_TAB_0,
943 AR_PHY_CL_TAB_1,
944 AR_PHY_CL_TAB_2 };
945
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +0530946 if (rtt) {
947 if (!ar9003_hw_rtt_restore(ah, chan))
948 run_rtt_cal = true;
949
950 ath_dbg(common, ATH_DBG_CALIBRATE, "RTT restore %s\n",
951 run_rtt_cal ? "failed" : "succeed");
952 }
953 run_agc_cal = run_rtt_cal;
954
955 if (run_rtt_cal) {
956 ar9003_hw_rtt_enable(ah);
957 ar9003_hw_rtt_set_mask(ah, 0x00);
958 ar9003_hw_rtt_clear_hist(ah);
959 }
960
961 if (rtt && !run_rtt_cal) {
962 agc_ctrl = REG_READ(ah, AR_PHY_AGC_CONTROL);
963 agc_supp_cals &= agc_ctrl;
964 agc_ctrl &= ~(AR_PHY_AGC_CONTROL_OFFSET_CAL |
965 AR_PHY_AGC_CONTROL_FLTR_CAL |
966 AR_PHY_AGC_CONTROL_PKDET_CAL);
967 REG_WRITE(ah, AR_PHY_AGC_CONTROL, agc_ctrl);
968 }
969
Rajkumar Manoharan8ad74c42011-10-13 11:00:38 +0530970 if (ah->enabled_cals & TX_CL_CAL) {
Rajkumar Manoharan77a5a662011-10-13 11:00:37 +0530971 if (caldata && caldata->done_txclcal_once)
972 REG_CLR_BIT(ah, AR_PHY_CL_CAL_CTL,
973 AR_PHY_CL_CAL_ENABLE);
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +0530974 else {
Rajkumar Manoharan77a5a662011-10-13 11:00:37 +0530975 REG_SET_BIT(ah, AR_PHY_CL_CAL_CTL,
976 AR_PHY_CL_CAL_ENABLE);
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +0530977 run_agc_cal = true;
978 }
Rajkumar Manoharan77a5a662011-10-13 11:00:37 +0530979 }
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -0400980
Rajkumar Manoharan8ad74c42011-10-13 11:00:38 +0530981 if (!(ah->enabled_cals & TX_IQ_CAL))
982 goto skip_tx_iqcal;
983
Luis R. Rodriguezc5395b62010-05-19 16:45:50 -0400984 /* Do Tx IQ Calibration */
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530985 REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_CONTROL_1,
986 AR_PHY_TX_IQCAL_CONTROL_1_IQCORR_I_Q_COFF_DELPT,
987 DELPT);
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -0800988
Rajkumar Manoharan3782c692011-04-24 21:34:39 +0530989 /*
990 * For AR9485 or later chips, TxIQ cal runs as part of
991 * AGC calibration
992 */
Rajkumar Manoharan8ad74c42011-10-13 11:00:38 +0530993 if (ah->enabled_cals & TX_IQ_ON_AGC_CAL) {
Rajkumar Manoharan5f0c04e2011-10-13 11:00:35 +0530994 if (caldata && !caldata->done_txiqcal_once)
995 REG_SET_BIT(ah, AR_PHY_TX_IQCAL_CONTROL_0,
996 AR_PHY_TX_IQCAL_CONTROL_0_ENABLE_TXIQ_CAL);
997 else
998 REG_CLR_BIT(ah, AR_PHY_TX_IQCAL_CONTROL_0,
999 AR_PHY_TX_IQCAL_CONTROL_0_ENABLE_TXIQ_CAL);
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +05301000 txiqcal_done = run_agc_cal = true;
Rajkumar Manoharan8ad74c42011-10-13 11:00:38 +05301001 goto skip_tx_iqcal;
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +05301002 } else if (caldata && !caldata->done_txiqcal_once)
1003 run_agc_cal = true;
1004
Rajkumar Manoharan8ad74c42011-10-13 11:00:38 +05301005 txiqcal_done = ar9003_hw_tx_iq_cal_run(ah);
1006 REG_WRITE(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_DIS);
1007 udelay(5);
1008 REG_WRITE(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_EN);
Luis R. Rodriguezc5395b62010-05-19 16:45:50 -04001009
Rajkumar Manoharan8ad74c42011-10-13 11:00:38 +05301010skip_tx_iqcal:
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -04001011
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +05301012 if (run_agc_cal) {
1013 /* Calibrate the AGC */
1014 REG_WRITE(ah, AR_PHY_AGC_CONTROL,
1015 REG_READ(ah, AR_PHY_AGC_CONTROL) |
1016 AR_PHY_AGC_CONTROL_CAL);
1017
1018 /* Poll for offset calibration complete */
1019 status = ath9k_hw_wait(ah, AR_PHY_AGC_CONTROL,
1020 AR_PHY_AGC_CONTROL_CAL,
1021 0, AH_WAIT_TIMEOUT);
1022 }
1023 if (rtt && !run_rtt_cal) {
1024 agc_ctrl |= agc_supp_cals;
1025 REG_WRITE(ah, AR_PHY_AGC_CONTROL, agc_ctrl);
1026 }
1027
1028 if (!status) {
1029 if (run_rtt_cal)
1030 ar9003_hw_rtt_disable(ah);
1031
Joe Perches226afe62010-12-02 19:12:37 -08001032 ath_dbg(common, ATH_DBG_CALIBRATE,
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +05301033 "offset calibration failed to complete in 1ms;"
1034 "noisy environment?\n");
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -04001035 return false;
1036 }
1037
Rajkumar Manoharan3782c692011-04-24 21:34:39 +05301038 if (txiqcal_done)
Rajkumar Manoharan34013522011-10-13 11:00:36 +05301039 ar9003_hw_tx_iq_cal_post_proc(ah, is_reusable);
Rajkumar Manoharan5f0c04e2011-10-13 11:00:35 +05301040 else if (caldata && caldata->done_txiqcal_once)
1041 ar9003_hw_tx_iq_cal_reload(ah);
Vasanthakumar Thiagarajan858b7e32010-12-06 04:27:56 -08001042
Rajkumar Manoharan77a5a662011-10-13 11:00:37 +05301043#define CL_TAB_ENTRY(reg_base) (reg_base + (4 * j))
Rajkumar Manoharan8ad74c42011-10-13 11:00:38 +05301044 if (caldata && (ah->enabled_cals & TX_CL_CAL)) {
Rajkumar Manoharan77a5a662011-10-13 11:00:37 +05301045 txclcal_done = !!(REG_READ(ah, AR_PHY_AGC_CONTROL) &
1046 AR_PHY_AGC_CONTROL_CLC_SUCCESS);
1047 if (caldata->done_txclcal_once) {
1048 for (i = 0; i < AR9300_MAX_CHAINS; i++) {
1049 if (!(ah->txchainmask & (1 << i)))
1050 continue;
1051 for (j = 0; j < MAX_CL_TAB_ENTRY; j++)
1052 REG_WRITE(ah, CL_TAB_ENTRY(cl_idx[i]),
1053 caldata->tx_clcal[i][j]);
1054 }
1055 } else if (is_reusable && txclcal_done) {
1056 for (i = 0; i < AR9300_MAX_CHAINS; i++) {
1057 if (!(ah->txchainmask & (1 << i)))
1058 continue;
1059 for (j = 0; j < MAX_CL_TAB_ENTRY; j++)
1060 caldata->tx_clcal[i][j] =
1061 REG_READ(ah,
1062 CL_TAB_ENTRY(cl_idx[i]));
1063 }
1064 caldata->done_txclcal_once = true;
1065 }
1066 }
1067#undef CL_TAB_ENTRY
1068
Rajkumar Manoharan324c74a2011-10-13 11:00:41 +05301069 if (run_rtt_cal && caldata) {
1070 struct ath9k_rtt_hist *hist = &caldata->rtt_hist;
1071 if (is_reusable && (hist->num_readings < RTT_HIST_MAX)) {
1072 u32 *table;
1073
1074 for (i = 0; i < AR9300_MAX_CHAINS; i++) {
1075 if (!(ah->rxchainmask & (1 << i)))
1076 continue;
1077 table = &hist->table[i][hist->num_readings][0];
1078 ar9003_hw_rtt_fill_hist(ah, i, table);
1079 }
1080 }
1081
1082 ar9003_hw_rtt_disable(ah);
1083 }
1084
Rajkumar Manoharan05bfe3d2011-09-15 19:02:53 +05301085 ath9k_hw_loadnf(ah, chan);
Felix Fietkau00c86592010-07-30 21:02:09 +02001086 ath9k_hw_start_nfcal(ah, true);
1087
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -04001088 /* Initialize list pointers */
1089 ah->cal_list = ah->cal_list_last = ah->cal_list_curr = NULL;
Felix Fietkau64978272010-10-03 19:07:16 +02001090 ah->supp_cals = IQ_MISMATCH_CAL;
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -04001091
Felix Fietkau64978272010-10-03 19:07:16 +02001092 if (ah->supp_cals & IQ_MISMATCH_CAL) {
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -04001093 INIT_CAL(&ah->iq_caldata);
1094 INSERT_CAL(ah, &ah->iq_caldata);
Joe Perches226afe62010-12-02 19:12:37 -08001095 ath_dbg(common, ATH_DBG_CALIBRATE,
1096 "enabling IQ Calibration.\n");
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -04001097 }
1098
Felix Fietkau64978272010-10-03 19:07:16 +02001099 if (ah->supp_cals & TEMP_COMP_CAL) {
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -04001100 INIT_CAL(&ah->tempCompCalData);
1101 INSERT_CAL(ah, &ah->tempCompCalData);
Joe Perches226afe62010-12-02 19:12:37 -08001102 ath_dbg(common, ATH_DBG_CALIBRATE,
1103 "enabling Temperature Compensation Calibration.\n");
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -04001104 }
1105
1106 /* Initialize current pointer to first element in list */
1107 ah->cal_list_curr = ah->cal_list;
1108
1109 if (ah->cal_list_curr)
1110 ath9k_hw_reset_calibration(ah, ah->cal_list_curr);
1111
Rajkumar Manoharan5f0c04e2011-10-13 11:00:35 +05301112 if (caldata)
1113 caldata->CalValid = 0;
Luis R. Rodriguezdf23aca2010-04-15 17:39:11 -04001114
1115 return true;
Luis R. Rodriguez77d6d392010-04-15 17:39:09 -04001116}
1117
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -04001118void ar9003_hw_attach_calib_ops(struct ath_hw *ah)
1119{
1120 struct ath_hw_private_ops *priv_ops = ath9k_hw_private_ops(ah);
1121 struct ath_hw_ops *ops = ath9k_hw_ops(ah);
1122
1123 priv_ops->init_cal_settings = ar9003_hw_init_cal_settings;
1124 priv_ops->init_cal = ar9003_hw_init_cal;
1125 priv_ops->setup_calibration = ar9003_hw_setup_calibration;
Luis R. Rodriguez795f5e22010-04-15 17:39:00 -04001126
1127 ops->calibrate = ar9003_hw_calibrate;
1128}