ath9k: Rename ar9003_hw_tx_iqcal_load_avg_2_passes
[pandora-kernel.git] / drivers / net / wireless / ath / ath9k / ar9003_calib.c
1 /*
2  * Copyright (c) 2010-2011 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 #include "ar9003_rtt.h"
21 #include "ar9003_mci.h"
22
23 #define MAX_MEASUREMENT MAX_IQCAL_MEASUREMENT
24 #define MAX_MAG_DELTA   11
25 #define MAX_PHS_DELTA   10
26
27 struct coeff {
28         int mag_coeff[AR9300_MAX_CHAINS][MAX_MEASUREMENT];
29         int phs_coeff[AR9300_MAX_CHAINS][MAX_MEASUREMENT];
30         int iqc_coeff[2];
31 };
32
33 enum ar9003_cal_types {
34         IQ_MISMATCH_CAL = BIT(0),
35 };
36
37 static void ar9003_hw_setup_calibration(struct ath_hw *ah,
38                                         struct ath9k_cal_list *currCal)
39 {
40         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
54                 ath_dbg(common, CALIBRATE,
55                         "starting IQ Mismatch Calibration\n");
56
57                 /* Kick-off cal */
58                 REG_SET_BIT(ah, AR_PHY_TIMING4, AR_PHY_TIMING4_DO_CAL);
59                 break;
60         default:
61                 ath_err(common, "Invalid calibration type\n");
62                 break;
63         }
64 }
65
66 /*
67  * Generic calibration routine.
68  * Recalibrate the lower PHY chips to account for temperature/environment
69  * changes.
70  */
71 static bool ar9003_hw_per_calibration(struct ath_hw *ah,
72                                       struct ath9k_channel *ichan,
73                                       u8 rxchainmask,
74                                       struct ath9k_cal_list *currCal)
75 {
76         struct ath9k_hw_cal_data *caldata = ah->caldata;
77         /* Cal is assumed not done until explicitly set below */
78         bool iscaldone = false;
79
80         /* Calibration in progress. */
81         if (currCal->calState == CAL_RUNNING) {
82                 /* Check to see if it has finished. */
83                 if (!(REG_READ(ah, AR_PHY_TIMING4) & AR_PHY_TIMING4_DO_CAL)) {
84                         /*
85                         * Accumulate cal measures for active chains
86                         */
87                         currCal->calData->calCollect(ah);
88                         ah->cal_samples++;
89
90                         if (ah->cal_samples >=
91                             currCal->calData->calNumSamples) {
92                                 unsigned int i, numChains = 0;
93                                 for (i = 0; i < AR9300_MAX_CHAINS; i++) {
94                                         if (rxchainmask & (1 << i))
95                                                 numChains++;
96                                 }
97
98                                 /*
99                                 * Process accumulated data
100                                 */
101                                 currCal->calData->calPostProc(ah, numChains);
102
103                                 /* Calibration has finished. */
104                                 caldata->CalValid |= currCal->calData->calType;
105                                 currCal->calState = CAL_DONE;
106                                 iscaldone = true;
107                         } else {
108                         /*
109                          * Set-up collection of another sub-sample until we
110                          * get desired number
111                          */
112                         ar9003_hw_setup_calibration(ah, currCal);
113                         }
114                 }
115         } else if (!(caldata->CalValid & currCal->calData->calType)) {
116                 /* If current cal is marked invalid in channel, kick it off */
117                 ath9k_hw_reset_calibration(ah, currCal);
118         }
119
120         return iscaldone;
121 }
122
123 static bool ar9003_hw_calibrate(struct ath_hw *ah,
124                                 struct ath9k_channel *chan,
125                                 u8 rxchainmask,
126                                 bool longcal)
127 {
128         bool iscaldone = true;
129         struct ath9k_cal_list *currCal = ah->cal_list_curr;
130
131         /*
132          * For given calibration:
133          * 1. Call generic cal routine
134          * 2. When this cal is done (isCalDone) if we have more cals waiting
135          *    (eg after reset), mask this to upper layers by not propagating
136          *    isCalDone if it is set to TRUE.
137          *    Instead, change isCalDone to FALSE and setup the waiting cal(s)
138          *    to be run.
139          */
140         if (currCal &&
141             (currCal->calState == CAL_RUNNING ||
142              currCal->calState == CAL_WAITING)) {
143                 iscaldone = ar9003_hw_per_calibration(ah, chan,
144                                                       rxchainmask, currCal);
145                 if (iscaldone) {
146                         ah->cal_list_curr = currCal = currCal->calNext;
147
148                         if (currCal->calState == CAL_WAITING) {
149                                 iscaldone = false;
150                                 ath9k_hw_reset_calibration(ah, currCal);
151                         }
152                 }
153         }
154
155         /*
156          * Do NF cal only at longer intervals. Get the value from
157          * the previous NF cal and update history buffer.
158          */
159         if (longcal && ath9k_hw_getnf(ah, chan)) {
160                 /*
161                  * Load the NF from history buffer of the current channel.
162                  * NF is slow time-variant, so it is OK to use a historical
163                  * value.
164                  */
165                 ath9k_hw_loadnf(ah, ah->curchan);
166
167                 /* start NF calibration, without updating BB NF register */
168                 ath9k_hw_start_nfcal(ah, false);
169         }
170
171         return iscaldone;
172 }
173
174 static void ar9003_hw_iqcal_collect(struct ath_hw *ah)
175 {
176         int i;
177
178         /* Accumulate IQ cal measures for active chains */
179         for (i = 0; i < AR5416_MAX_CHAINS; i++) {
180                 if (ah->txchainmask & BIT(i)) {
181                         ah->totalPowerMeasI[i] +=
182                                 REG_READ(ah, AR_PHY_CAL_MEAS_0(i));
183                         ah->totalPowerMeasQ[i] +=
184                                 REG_READ(ah, AR_PHY_CAL_MEAS_1(i));
185                         ah->totalIqCorrMeas[i] +=
186                                 (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_2(i));
187                         ath_dbg(ath9k_hw_common(ah), CALIBRATE,
188                                 "%d: Chn %d pmi=0x%08x;pmq=0x%08x;iqcm=0x%08x;\n",
189                                 ah->cal_samples, i, ah->totalPowerMeasI[i],
190                                 ah->totalPowerMeasQ[i],
191                                 ah->totalIqCorrMeas[i]);
192                 }
193         }
194 }
195
196 static void ar9003_hw_iqcalibrate(struct ath_hw *ah, u8 numChains)
197 {
198         struct ath_common *common = ath9k_hw_common(ah);
199         u32 powerMeasQ, powerMeasI, iqCorrMeas;
200         u32 qCoffDenom, iCoffDenom;
201         int32_t qCoff, iCoff;
202         int iqCorrNeg, i;
203         static const u_int32_t offset_array[3] = {
204                 AR_PHY_RX_IQCAL_CORR_B0,
205                 AR_PHY_RX_IQCAL_CORR_B1,
206                 AR_PHY_RX_IQCAL_CORR_B2,
207         };
208
209         for (i = 0; i < numChains; i++) {
210                 powerMeasI = ah->totalPowerMeasI[i];
211                 powerMeasQ = ah->totalPowerMeasQ[i];
212                 iqCorrMeas = ah->totalIqCorrMeas[i];
213
214                 ath_dbg(common, CALIBRATE,
215                         "Starting IQ Cal and Correction for Chain %d\n", i);
216
217                 ath_dbg(common, CALIBRATE,
218                         "Original: Chn %d iq_corr_meas = 0x%08x\n",
219                         i, ah->totalIqCorrMeas[i]);
220
221                 iqCorrNeg = 0;
222
223                 if (iqCorrMeas > 0x80000000) {
224                         iqCorrMeas = (0xffffffff - iqCorrMeas) + 1;
225                         iqCorrNeg = 1;
226                 }
227
228                 ath_dbg(common, CALIBRATE, "Chn %d pwr_meas_i = 0x%08x\n",
229                         i, powerMeasI);
230                 ath_dbg(common, CALIBRATE, "Chn %d pwr_meas_q = 0x%08x\n",
231                         i, powerMeasQ);
232                 ath_dbg(common, CALIBRATE, "iqCorrNeg is 0x%08x\n", iqCorrNeg);
233
234                 iCoffDenom = (powerMeasI / 2 + powerMeasQ / 2) / 256;
235                 qCoffDenom = powerMeasQ / 64;
236
237                 if ((iCoffDenom != 0) && (qCoffDenom != 0)) {
238                         iCoff = iqCorrMeas / iCoffDenom;
239                         qCoff = powerMeasI / qCoffDenom - 64;
240                         ath_dbg(common, CALIBRATE, "Chn %d iCoff = 0x%08x\n",
241                                 i, iCoff);
242                         ath_dbg(common, CALIBRATE, "Chn %d qCoff = 0x%08x\n",
243                                 i, qCoff);
244
245                         /* Force bounds on iCoff */
246                         if (iCoff >= 63)
247                                 iCoff = 63;
248                         else if (iCoff <= -63)
249                                 iCoff = -63;
250
251                         /* Negate iCoff if iqCorrNeg == 0 */
252                         if (iqCorrNeg == 0x0)
253                                 iCoff = -iCoff;
254
255                         /* Force bounds on qCoff */
256                         if (qCoff >= 63)
257                                 qCoff = 63;
258                         else if (qCoff <= -63)
259                                 qCoff = -63;
260
261                         iCoff = iCoff & 0x7f;
262                         qCoff = qCoff & 0x7f;
263
264                         ath_dbg(common, CALIBRATE,
265                                 "Chn %d : iCoff = 0x%x  qCoff = 0x%x\n",
266                                 i, iCoff, qCoff);
267                         ath_dbg(common, CALIBRATE,
268                                 "Register offset (0x%04x) before update = 0x%x\n",
269                                 offset_array[i],
270                                 REG_READ(ah, offset_array[i]));
271
272                         if (AR_SREV_9565(ah) &&
273                             (iCoff == 63 || qCoff == 63 ||
274                              iCoff == -63 || qCoff == -63))
275                                 return;
276
277                         REG_RMW_FIELD(ah, offset_array[i],
278                                       AR_PHY_RX_IQCAL_CORR_IQCORR_Q_I_COFF,
279                                       iCoff);
280                         REG_RMW_FIELD(ah, offset_array[i],
281                                       AR_PHY_RX_IQCAL_CORR_IQCORR_Q_Q_COFF,
282                                       qCoff);
283                         ath_dbg(common, CALIBRATE,
284                                 "Register offset (0x%04x) QI COFF (bitfields 0x%08x) after update = 0x%x\n",
285                                 offset_array[i],
286                                 AR_PHY_RX_IQCAL_CORR_IQCORR_Q_I_COFF,
287                                 REG_READ(ah, offset_array[i]));
288                         ath_dbg(common, CALIBRATE,
289                                 "Register offset (0x%04x) QQ COFF (bitfields 0x%08x) after update = 0x%x\n",
290                                 offset_array[i],
291                                 AR_PHY_RX_IQCAL_CORR_IQCORR_Q_Q_COFF,
292                                 REG_READ(ah, offset_array[i]));
293
294                         ath_dbg(common, CALIBRATE,
295                                 "IQ Cal and Correction done for Chain %d\n", i);
296                 }
297         }
298
299         REG_SET_BIT(ah, AR_PHY_RX_IQCAL_CORR_B0,
300                     AR_PHY_RX_IQCAL_CORR_IQCORR_ENABLE);
301         ath_dbg(common, CALIBRATE,
302                 "IQ Cal and Correction (offset 0x%04x) enabled (bit position 0x%08x). New Value 0x%08x\n",
303                 (unsigned) (AR_PHY_RX_IQCAL_CORR_B0),
304                 AR_PHY_RX_IQCAL_CORR_IQCORR_ENABLE,
305                 REG_READ(ah, AR_PHY_RX_IQCAL_CORR_B0));
306 }
307
308 static const struct ath9k_percal_data iq_cal_single_sample = {
309         IQ_MISMATCH_CAL,
310         MIN_CAL_SAMPLES,
311         PER_MAX_LOG_COUNT,
312         ar9003_hw_iqcal_collect,
313         ar9003_hw_iqcalibrate
314 };
315
316 static void ar9003_hw_init_cal_settings(struct ath_hw *ah)
317 {
318         ah->iq_caldata.calData = &iq_cal_single_sample;
319
320         if (AR_SREV_9300_20_OR_LATER(ah)) {
321                 ah->enabled_cals |= TX_IQ_CAL;
322                 if (AR_SREV_9485_OR_LATER(ah) && !AR_SREV_9340(ah))
323                         ah->enabled_cals |= TX_IQ_ON_AGC_CAL;
324         }
325
326         ah->supp_cals = IQ_MISMATCH_CAL;
327 }
328
329 #define OFF_UPPER_LT 24
330 #define OFF_LOWER_LT 7
331
332 static bool ar9003_hw_dynamic_osdac_selection(struct ath_hw *ah,
333                                               bool txiqcal_done)
334 {
335         struct ath_common *common = ath9k_hw_common(ah);
336         int ch0_done, osdac_ch0, dc_off_ch0_i1, dc_off_ch0_q1, dc_off_ch0_i2,
337                 dc_off_ch0_q2, dc_off_ch0_i3, dc_off_ch0_q3;
338         int ch1_done, osdac_ch1, dc_off_ch1_i1, dc_off_ch1_q1, dc_off_ch1_i2,
339                 dc_off_ch1_q2, dc_off_ch1_i3, dc_off_ch1_q3;
340         int ch2_done, osdac_ch2, dc_off_ch2_i1, dc_off_ch2_q1, dc_off_ch2_i2,
341                 dc_off_ch2_q2, dc_off_ch2_i3, dc_off_ch2_q3;
342         bool status;
343         u32 temp, val;
344
345         /*
346          * Clear offset and IQ calibration, run AGC cal.
347          */
348         REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
349                     AR_PHY_AGC_CONTROL_OFFSET_CAL);
350         REG_CLR_BIT(ah, AR_PHY_TX_IQCAL_CONTROL_0,
351                     AR_PHY_TX_IQCAL_CONTROL_0_ENABLE_TXIQ_CAL);
352         REG_WRITE(ah, AR_PHY_AGC_CONTROL,
353                   REG_READ(ah, AR_PHY_AGC_CONTROL) | AR_PHY_AGC_CONTROL_CAL);
354
355         status = ath9k_hw_wait(ah, AR_PHY_AGC_CONTROL,
356                                AR_PHY_AGC_CONTROL_CAL,
357                                0, AH_WAIT_TIMEOUT);
358         if (!status) {
359                 ath_dbg(common, CALIBRATE,
360                         "AGC cal without offset cal failed to complete in 1ms");
361                 return false;
362         }
363
364         /*
365          * Allow only offset calibration and disable the others
366          * (Carrier Leak calibration, TX Filter calibration and
367          *  Peak Detector offset calibration).
368          */
369         REG_SET_BIT(ah, AR_PHY_AGC_CONTROL,
370                     AR_PHY_AGC_CONTROL_OFFSET_CAL);
371         REG_CLR_BIT(ah, AR_PHY_CL_CAL_CTL,
372                     AR_PHY_CL_CAL_ENABLE);
373         REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
374                     AR_PHY_AGC_CONTROL_FLTR_CAL);
375         REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
376                     AR_PHY_AGC_CONTROL_PKDET_CAL);
377
378         ch0_done = 0;
379         ch1_done = 0;
380         ch2_done = 0;
381
382         while ((ch0_done == 0) || (ch1_done == 0) || (ch2_done == 0)) {
383                 osdac_ch0 = (REG_READ(ah, AR_PHY_65NM_CH0_BB1) >> 30) & 0x3;
384                 osdac_ch1 = (REG_READ(ah, AR_PHY_65NM_CH1_BB1) >> 30) & 0x3;
385                 osdac_ch2 = (REG_READ(ah, AR_PHY_65NM_CH2_BB1) >> 30) & 0x3;
386
387                 REG_SET_BIT(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_EN);
388
389                 REG_WRITE(ah, AR_PHY_AGC_CONTROL,
390                           REG_READ(ah, AR_PHY_AGC_CONTROL) | AR_PHY_AGC_CONTROL_CAL);
391
392                 status = ath9k_hw_wait(ah, AR_PHY_AGC_CONTROL,
393                                        AR_PHY_AGC_CONTROL_CAL,
394                                        0, AH_WAIT_TIMEOUT);
395                 if (!status) {
396                         ath_dbg(common, CALIBRATE,
397                                 "DC offset cal failed to complete in 1ms");
398                         return false;
399                 }
400
401                 REG_CLR_BIT(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_EN);
402
403                 /*
404                  * High gain.
405                  */
406                 REG_WRITE(ah, AR_PHY_65NM_CH0_BB3,
407                           ((REG_READ(ah, AR_PHY_65NM_CH0_BB3) & 0xfffffcff) | (1 << 8)));
408                 REG_WRITE(ah, AR_PHY_65NM_CH1_BB3,
409                           ((REG_READ(ah, AR_PHY_65NM_CH1_BB3) & 0xfffffcff) | (1 << 8)));
410                 REG_WRITE(ah, AR_PHY_65NM_CH2_BB3,
411                           ((REG_READ(ah, AR_PHY_65NM_CH2_BB3) & 0xfffffcff) | (1 << 8)));
412
413                 temp = REG_READ(ah, AR_PHY_65NM_CH0_BB3);
414                 dc_off_ch0_i1 = (temp >> 26) & 0x1f;
415                 dc_off_ch0_q1 = (temp >> 21) & 0x1f;
416
417                 temp = REG_READ(ah, AR_PHY_65NM_CH1_BB3);
418                 dc_off_ch1_i1 = (temp >> 26) & 0x1f;
419                 dc_off_ch1_q1 = (temp >> 21) & 0x1f;
420
421                 temp = REG_READ(ah, AR_PHY_65NM_CH2_BB3);
422                 dc_off_ch2_i1 = (temp >> 26) & 0x1f;
423                 dc_off_ch2_q1 = (temp >> 21) & 0x1f;
424
425                 /*
426                  * Low gain.
427                  */
428                 REG_WRITE(ah, AR_PHY_65NM_CH0_BB3,
429                           ((REG_READ(ah, AR_PHY_65NM_CH0_BB3) & 0xfffffcff) | (2 << 8)));
430                 REG_WRITE(ah, AR_PHY_65NM_CH1_BB3,
431                           ((REG_READ(ah, AR_PHY_65NM_CH1_BB3) & 0xfffffcff) | (2 << 8)));
432                 REG_WRITE(ah, AR_PHY_65NM_CH2_BB3,
433                           ((REG_READ(ah, AR_PHY_65NM_CH2_BB3) & 0xfffffcff) | (2 << 8)));
434
435                 temp = REG_READ(ah, AR_PHY_65NM_CH0_BB3);
436                 dc_off_ch0_i2 = (temp >> 26) & 0x1f;
437                 dc_off_ch0_q2 = (temp >> 21) & 0x1f;
438
439                 temp = REG_READ(ah, AR_PHY_65NM_CH1_BB3);
440                 dc_off_ch1_i2 = (temp >> 26) & 0x1f;
441                 dc_off_ch1_q2 = (temp >> 21) & 0x1f;
442
443                 temp = REG_READ(ah, AR_PHY_65NM_CH2_BB3);
444                 dc_off_ch2_i2 = (temp >> 26) & 0x1f;
445                 dc_off_ch2_q2 = (temp >> 21) & 0x1f;
446
447                 /*
448                  * Loopback.
449                  */
450                 REG_WRITE(ah, AR_PHY_65NM_CH0_BB3,
451                           ((REG_READ(ah, AR_PHY_65NM_CH0_BB3) & 0xfffffcff) | (3 << 8)));
452                 REG_WRITE(ah, AR_PHY_65NM_CH1_BB3,
453                           ((REG_READ(ah, AR_PHY_65NM_CH1_BB3) & 0xfffffcff) | (3 << 8)));
454                 REG_WRITE(ah, AR_PHY_65NM_CH2_BB3,
455                           ((REG_READ(ah, AR_PHY_65NM_CH2_BB3) & 0xfffffcff) | (3 << 8)));
456
457                 temp = REG_READ(ah, AR_PHY_65NM_CH0_BB3);
458                 dc_off_ch0_i3 = (temp >> 26) & 0x1f;
459                 dc_off_ch0_q3 = (temp >> 21) & 0x1f;
460
461                 temp = REG_READ(ah, AR_PHY_65NM_CH1_BB3);
462                 dc_off_ch1_i3 = (temp >> 26) & 0x1f;
463                 dc_off_ch1_q3 = (temp >> 21) & 0x1f;
464
465                 temp = REG_READ(ah, AR_PHY_65NM_CH2_BB3);
466                 dc_off_ch2_i3 = (temp >> 26) & 0x1f;
467                 dc_off_ch2_q3 = (temp >> 21) & 0x1f;
468
469                 if ((dc_off_ch0_i1 > OFF_UPPER_LT) || (dc_off_ch0_i1 < OFF_LOWER_LT) ||
470                     (dc_off_ch0_i2 > OFF_UPPER_LT) || (dc_off_ch0_i2 < OFF_LOWER_LT) ||
471                     (dc_off_ch0_i3 > OFF_UPPER_LT) || (dc_off_ch0_i3 < OFF_LOWER_LT) ||
472                     (dc_off_ch0_q1 > OFF_UPPER_LT) || (dc_off_ch0_q1 < OFF_LOWER_LT) ||
473                     (dc_off_ch0_q2 > OFF_UPPER_LT) || (dc_off_ch0_q2 < OFF_LOWER_LT) ||
474                     (dc_off_ch0_q3 > OFF_UPPER_LT) || (dc_off_ch0_q3 < OFF_LOWER_LT)) {
475                         if (osdac_ch0 == 3) {
476                                 ch0_done = 1;
477                         } else {
478                                 osdac_ch0++;
479
480                                 val = REG_READ(ah, AR_PHY_65NM_CH0_BB1) & 0x3fffffff;
481                                 val |= (osdac_ch0 << 30);
482                                 REG_WRITE(ah, AR_PHY_65NM_CH0_BB1, val);
483
484                                 ch0_done = 0;
485                         }
486                 } else {
487                         ch0_done = 1;
488                 }
489
490                 if ((dc_off_ch1_i1 > OFF_UPPER_LT) || (dc_off_ch1_i1 < OFF_LOWER_LT) ||
491                     (dc_off_ch1_i2 > OFF_UPPER_LT) || (dc_off_ch1_i2 < OFF_LOWER_LT) ||
492                     (dc_off_ch1_i3 > OFF_UPPER_LT) || (dc_off_ch1_i3 < OFF_LOWER_LT) ||
493                     (dc_off_ch1_q1 > OFF_UPPER_LT) || (dc_off_ch1_q1 < OFF_LOWER_LT) ||
494                     (dc_off_ch1_q2 > OFF_UPPER_LT) || (dc_off_ch1_q2 < OFF_LOWER_LT) ||
495                     (dc_off_ch1_q3 > OFF_UPPER_LT) || (dc_off_ch1_q3 < OFF_LOWER_LT)) {
496                         if (osdac_ch1 == 3) {
497                                 ch1_done = 1;
498                         } else {
499                                 osdac_ch1++;
500
501                                 val = REG_READ(ah, AR_PHY_65NM_CH1_BB1) & 0x3fffffff;
502                                 val |= (osdac_ch1 << 30);
503                                 REG_WRITE(ah, AR_PHY_65NM_CH1_BB1, val);
504
505                                 ch1_done = 0;
506                         }
507                 } else {
508                         ch1_done = 1;
509                 }
510
511                 if ((dc_off_ch2_i1 > OFF_UPPER_LT) || (dc_off_ch2_i1 < OFF_LOWER_LT) ||
512                     (dc_off_ch2_i2 > OFF_UPPER_LT) || (dc_off_ch2_i2 < OFF_LOWER_LT) ||
513                     (dc_off_ch2_i3 > OFF_UPPER_LT) || (dc_off_ch2_i3 < OFF_LOWER_LT) ||
514                     (dc_off_ch2_q1 > OFF_UPPER_LT) || (dc_off_ch2_q1 < OFF_LOWER_LT) ||
515                     (dc_off_ch2_q2 > OFF_UPPER_LT) || (dc_off_ch2_q2 < OFF_LOWER_LT) ||
516                     (dc_off_ch2_q3 > OFF_UPPER_LT) || (dc_off_ch2_q3 < OFF_LOWER_LT)) {
517                         if (osdac_ch2 == 3) {
518                                 ch2_done = 1;
519                         } else {
520                                 osdac_ch2++;
521
522                                 val = REG_READ(ah, AR_PHY_65NM_CH2_BB1) & 0x3fffffff;
523                                 val |= (osdac_ch2 << 30);
524                                 REG_WRITE(ah, AR_PHY_65NM_CH2_BB1, val);
525
526                                 ch2_done = 0;
527                         }
528                 } else {
529                         ch2_done = 1;
530                 }
531         }
532
533         REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
534                     AR_PHY_AGC_CONTROL_OFFSET_CAL);
535         REG_SET_BIT(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_EN);
536
537         /*
538          * We don't need to check txiqcal_done here since it is always
539          * set for AR9550.
540          */
541         REG_SET_BIT(ah, AR_PHY_TX_IQCAL_CONTROL_0,
542                     AR_PHY_TX_IQCAL_CONTROL_0_ENABLE_TXIQ_CAL);
543
544         return true;
545 }
546
547 /*
548  * solve 4x4 linear equation used in loopback iq cal.
549  */
550 static bool ar9003_hw_solve_iq_cal(struct ath_hw *ah,
551                                    s32 sin_2phi_1,
552                                    s32 cos_2phi_1,
553                                    s32 sin_2phi_2,
554                                    s32 cos_2phi_2,
555                                    s32 mag_a0_d0,
556                                    s32 phs_a0_d0,
557                                    s32 mag_a1_d0,
558                                    s32 phs_a1_d0,
559                                    s32 solved_eq[])
560 {
561         s32 f1 = cos_2phi_1 - cos_2phi_2,
562             f3 = sin_2phi_1 - sin_2phi_2,
563             f2;
564         s32 mag_tx, phs_tx, mag_rx, phs_rx;
565         const s32 result_shift = 1 << 15;
566         struct ath_common *common = ath9k_hw_common(ah);
567
568         f2 = ((f1 >> 3) * (f1 >> 3) + (f3 >> 3) * (f3 >> 3)) >> 9;
569
570         if (!f2) {
571                 ath_dbg(common, CALIBRATE, "Divide by 0\n");
572                 return false;
573         }
574
575         /* mag mismatch, tx */
576         mag_tx = f1 * (mag_a0_d0  - mag_a1_d0) + f3 * (phs_a0_d0 - phs_a1_d0);
577         /* phs mismatch, tx */
578         phs_tx = f3 * (-mag_a0_d0 + mag_a1_d0) + f1 * (phs_a0_d0 - phs_a1_d0);
579
580         mag_tx = (mag_tx / f2);
581         phs_tx = (phs_tx / f2);
582
583         /* mag mismatch, rx */
584         mag_rx = mag_a0_d0 - (cos_2phi_1 * mag_tx + sin_2phi_1 * phs_tx) /
585                  result_shift;
586         /* phs mismatch, rx */
587         phs_rx = phs_a0_d0 + (sin_2phi_1 * mag_tx - cos_2phi_1 * phs_tx) /
588                  result_shift;
589
590         solved_eq[0] = mag_tx;
591         solved_eq[1] = phs_tx;
592         solved_eq[2] = mag_rx;
593         solved_eq[3] = phs_rx;
594
595         return true;
596 }
597
598 static s32 ar9003_hw_find_mag_approx(struct ath_hw *ah, s32 in_re, s32 in_im)
599 {
600         s32 abs_i = abs(in_re),
601             abs_q = abs(in_im),
602             max_abs, min_abs;
603
604         if (abs_i > abs_q) {
605                 max_abs = abs_i;
606                 min_abs = abs_q;
607         } else {
608                 max_abs = abs_q;
609                 min_abs = abs_i;
610         }
611
612         return max_abs - (max_abs / 32) + (min_abs / 8) + (min_abs / 4);
613 }
614
615 #define DELPT 32
616
617 static bool ar9003_hw_calc_iq_corr(struct ath_hw *ah,
618                                    s32 chain_idx,
619                                    const s32 iq_res[],
620                                    s32 iqc_coeff[])
621 {
622         s32 i2_m_q2_a0_d0, i2_p_q2_a0_d0, iq_corr_a0_d0,
623             i2_m_q2_a0_d1, i2_p_q2_a0_d1, iq_corr_a0_d1,
624             i2_m_q2_a1_d0, i2_p_q2_a1_d0, iq_corr_a1_d0,
625             i2_m_q2_a1_d1, i2_p_q2_a1_d1, iq_corr_a1_d1;
626         s32 mag_a0_d0, mag_a1_d0, mag_a0_d1, mag_a1_d1,
627             phs_a0_d0, phs_a1_d0, phs_a0_d1, phs_a1_d1,
628             sin_2phi_1, cos_2phi_1,
629             sin_2phi_2, cos_2phi_2;
630         s32 mag_tx, phs_tx, mag_rx, phs_rx;
631         s32 solved_eq[4], mag_corr_tx, phs_corr_tx, mag_corr_rx, phs_corr_rx,
632             q_q_coff, q_i_coff;
633         const s32 res_scale = 1 << 15;
634         const s32 delpt_shift = 1 << 8;
635         s32 mag1, mag2;
636         struct ath_common *common = ath9k_hw_common(ah);
637
638         i2_m_q2_a0_d0 = iq_res[0] & 0xfff;
639         i2_p_q2_a0_d0 = (iq_res[0] >> 12) & 0xfff;
640         iq_corr_a0_d0 = ((iq_res[0] >> 24) & 0xff) + ((iq_res[1] & 0xf) << 8);
641
642         if (i2_m_q2_a0_d0 > 0x800)
643                 i2_m_q2_a0_d0 = -((0xfff - i2_m_q2_a0_d0) + 1);
644
645         if (i2_p_q2_a0_d0 > 0x800)
646                 i2_p_q2_a0_d0 = -((0xfff - i2_p_q2_a0_d0) + 1);
647
648         if (iq_corr_a0_d0 > 0x800)
649                 iq_corr_a0_d0 = -((0xfff - iq_corr_a0_d0) + 1);
650
651         i2_m_q2_a0_d1 = (iq_res[1] >> 4) & 0xfff;
652         i2_p_q2_a0_d1 = (iq_res[2] & 0xfff);
653         iq_corr_a0_d1 = (iq_res[2] >> 12) & 0xfff;
654
655         if (i2_m_q2_a0_d1 > 0x800)
656                 i2_m_q2_a0_d1 = -((0xfff - i2_m_q2_a0_d1) + 1);
657
658         if (iq_corr_a0_d1 > 0x800)
659                 iq_corr_a0_d1 = -((0xfff - iq_corr_a0_d1) + 1);
660
661         i2_m_q2_a1_d0 = ((iq_res[2] >> 24) & 0xff) + ((iq_res[3] & 0xf) << 8);
662         i2_p_q2_a1_d0 = (iq_res[3] >> 4) & 0xfff;
663         iq_corr_a1_d0 = iq_res[4] & 0xfff;
664
665         if (i2_m_q2_a1_d0 > 0x800)
666                 i2_m_q2_a1_d0 = -((0xfff - i2_m_q2_a1_d0) + 1);
667
668         if (i2_p_q2_a1_d0 > 0x800)
669                 i2_p_q2_a1_d0 = -((0xfff - i2_p_q2_a1_d0) + 1);
670
671         if (iq_corr_a1_d0 > 0x800)
672                 iq_corr_a1_d0 = -((0xfff - iq_corr_a1_d0) + 1);
673
674         i2_m_q2_a1_d1 = (iq_res[4] >> 12) & 0xfff;
675         i2_p_q2_a1_d1 = ((iq_res[4] >> 24) & 0xff) + ((iq_res[5] & 0xf) << 8);
676         iq_corr_a1_d1 = (iq_res[5] >> 4) & 0xfff;
677
678         if (i2_m_q2_a1_d1 > 0x800)
679                 i2_m_q2_a1_d1 = -((0xfff - i2_m_q2_a1_d1) + 1);
680
681         if (i2_p_q2_a1_d1 > 0x800)
682                 i2_p_q2_a1_d1 = -((0xfff - i2_p_q2_a1_d1) + 1);
683
684         if (iq_corr_a1_d1 > 0x800)
685                 iq_corr_a1_d1 = -((0xfff - iq_corr_a1_d1) + 1);
686
687         if ((i2_p_q2_a0_d0 == 0) || (i2_p_q2_a0_d1 == 0) ||
688             (i2_p_q2_a1_d0 == 0) || (i2_p_q2_a1_d1 == 0)) {
689                 ath_dbg(common, CALIBRATE,
690                         "Divide by 0:\n"
691                         "a0_d0=%d\n"
692                         "a0_d1=%d\n"
693                         "a2_d0=%d\n"
694                         "a1_d1=%d\n",
695                         i2_p_q2_a0_d0, i2_p_q2_a0_d1,
696                         i2_p_q2_a1_d0, i2_p_q2_a1_d1);
697                 return false;
698         }
699
700         if ((i2_p_q2_a0_d0 < 1024) || (i2_p_q2_a0_d0 > 2047) ||
701             (i2_p_q2_a1_d0 < 0) || (i2_p_q2_a1_d1 < 0) ||
702             (i2_p_q2_a0_d0 <= i2_m_q2_a0_d0) ||
703             (i2_p_q2_a0_d0 <= iq_corr_a0_d0) ||
704             (i2_p_q2_a0_d1 <= i2_m_q2_a0_d1) ||
705             (i2_p_q2_a0_d1 <= iq_corr_a0_d1) ||
706             (i2_p_q2_a1_d0 <= i2_m_q2_a1_d0) ||
707             (i2_p_q2_a1_d0 <= iq_corr_a1_d0) ||
708             (i2_p_q2_a1_d1 <= i2_m_q2_a1_d1) ||
709             (i2_p_q2_a1_d1 <= iq_corr_a1_d1)) {
710                 return false;
711         }
712
713         mag_a0_d0 = (i2_m_q2_a0_d0 * res_scale) / i2_p_q2_a0_d0;
714         phs_a0_d0 = (iq_corr_a0_d0 * res_scale) / i2_p_q2_a0_d0;
715
716         mag_a0_d1 = (i2_m_q2_a0_d1 * res_scale) / i2_p_q2_a0_d1;
717         phs_a0_d1 = (iq_corr_a0_d1 * res_scale) / i2_p_q2_a0_d1;
718
719         mag_a1_d0 = (i2_m_q2_a1_d0 * res_scale) / i2_p_q2_a1_d0;
720         phs_a1_d0 = (iq_corr_a1_d0 * res_scale) / i2_p_q2_a1_d0;
721
722         mag_a1_d1 = (i2_m_q2_a1_d1 * res_scale) / i2_p_q2_a1_d1;
723         phs_a1_d1 = (iq_corr_a1_d1 * res_scale) / i2_p_q2_a1_d1;
724
725         /* w/o analog phase shift */
726         sin_2phi_1 = (((mag_a0_d0 - mag_a0_d1) * delpt_shift) / DELPT);
727         /* w/o analog phase shift */
728         cos_2phi_1 = (((phs_a0_d1 - phs_a0_d0) * delpt_shift) / DELPT);
729         /* w/  analog phase shift */
730         sin_2phi_2 = (((mag_a1_d0 - mag_a1_d1) * delpt_shift) / DELPT);
731         /* w/  analog phase shift */
732         cos_2phi_2 = (((phs_a1_d1 - phs_a1_d0) * delpt_shift) / DELPT);
733
734         /*
735          * force sin^2 + cos^2 = 1;
736          * find magnitude by approximation
737          */
738         mag1 = ar9003_hw_find_mag_approx(ah, cos_2phi_1, sin_2phi_1);
739         mag2 = ar9003_hw_find_mag_approx(ah, cos_2phi_2, sin_2phi_2);
740
741         if ((mag1 == 0) || (mag2 == 0)) {
742                 ath_dbg(common, CALIBRATE, "Divide by 0: mag1=%d, mag2=%d\n",
743                         mag1, mag2);
744                 return false;
745         }
746
747         /* normalization sin and cos by mag */
748         sin_2phi_1 = (sin_2phi_1 * res_scale / mag1);
749         cos_2phi_1 = (cos_2phi_1 * res_scale / mag1);
750         sin_2phi_2 = (sin_2phi_2 * res_scale / mag2);
751         cos_2phi_2 = (cos_2phi_2 * res_scale / mag2);
752
753         /* calculate IQ mismatch */
754         if (!ar9003_hw_solve_iq_cal(ah,
755                              sin_2phi_1, cos_2phi_1,
756                              sin_2phi_2, cos_2phi_2,
757                              mag_a0_d0, phs_a0_d0,
758                              mag_a1_d0,
759                              phs_a1_d0, solved_eq)) {
760                 ath_dbg(common, CALIBRATE,
761                         "Call to ar9003_hw_solve_iq_cal() failed\n");
762                 return false;
763         }
764
765         mag_tx = solved_eq[0];
766         phs_tx = solved_eq[1];
767         mag_rx = solved_eq[2];
768         phs_rx = solved_eq[3];
769
770         ath_dbg(common, CALIBRATE,
771                 "chain %d: mag mismatch=%d phase mismatch=%d\n",
772                 chain_idx, mag_tx/res_scale, phs_tx/res_scale);
773
774         if (res_scale == mag_tx) {
775                 ath_dbg(common, CALIBRATE,
776                         "Divide by 0: mag_tx=%d, res_scale=%d\n",
777                         mag_tx, res_scale);
778                 return false;
779         }
780
781         /* calculate and quantize Tx IQ correction factor */
782         mag_corr_tx = (mag_tx * res_scale) / (res_scale - mag_tx);
783         phs_corr_tx = -phs_tx;
784
785         q_q_coff = (mag_corr_tx * 128 / res_scale);
786         q_i_coff = (phs_corr_tx * 256 / res_scale);
787
788         ath_dbg(common, CALIBRATE, "tx chain %d: mag corr=%d  phase corr=%d\n",
789                 chain_idx, q_q_coff, q_i_coff);
790
791         if (q_i_coff < -63)
792                 q_i_coff = -63;
793         if (q_i_coff > 63)
794                 q_i_coff = 63;
795         if (q_q_coff < -63)
796                 q_q_coff = -63;
797         if (q_q_coff > 63)
798                 q_q_coff = 63;
799
800         iqc_coeff[0] = (q_q_coff * 128) + q_i_coff;
801
802         ath_dbg(common, CALIBRATE, "tx chain %d: iq corr coeff=%x\n",
803                 chain_idx, iqc_coeff[0]);
804
805         if (-mag_rx == res_scale) {
806                 ath_dbg(common, CALIBRATE,
807                         "Divide by 0: mag_rx=%d, res_scale=%d\n",
808                         mag_rx, res_scale);
809                 return false;
810         }
811
812         /* calculate and quantize Rx IQ correction factors */
813         mag_corr_rx = (-mag_rx * res_scale) / (res_scale + mag_rx);
814         phs_corr_rx = -phs_rx;
815
816         q_q_coff = (mag_corr_rx * 128 / res_scale);
817         q_i_coff = (phs_corr_rx * 256 / res_scale);
818
819         ath_dbg(common, CALIBRATE, "rx chain %d: mag corr=%d  phase corr=%d\n",
820                 chain_idx, q_q_coff, q_i_coff);
821
822         if (q_i_coff < -63)
823                 q_i_coff = -63;
824         if (q_i_coff > 63)
825                 q_i_coff = 63;
826         if (q_q_coff < -63)
827                 q_q_coff = -63;
828         if (q_q_coff > 63)
829                 q_q_coff = 63;
830
831         iqc_coeff[1] = (q_q_coff * 128) + q_i_coff;
832
833         ath_dbg(common, CALIBRATE, "rx chain %d: iq corr coeff=%x\n",
834                 chain_idx, iqc_coeff[1]);
835
836         return true;
837 }
838
839 static void ar9003_hw_detect_outlier(int *mp_coeff, int nmeasurement,
840                                      int max_delta)
841 {
842         int mp_max = -64, max_idx = 0;
843         int mp_min = 63, min_idx = 0;
844         int mp_avg = 0, i, outlier_idx = 0, mp_count = 0;
845
846         /* find min/max mismatch across all calibrated gains */
847         for (i = 0; i < nmeasurement; i++) {
848                 if (mp_coeff[i] > mp_max) {
849                         mp_max = mp_coeff[i];
850                         max_idx = i;
851                 } else if (mp_coeff[i] < mp_min) {
852                         mp_min = mp_coeff[i];
853                         min_idx = i;
854                 }
855         }
856
857         /* find average (exclude max abs value) */
858         for (i = 0; i < nmeasurement; i++) {
859                 if ((abs(mp_coeff[i]) < abs(mp_max)) ||
860                     (abs(mp_coeff[i]) < abs(mp_min))) {
861                         mp_avg += mp_coeff[i];
862                         mp_count++;
863                 }
864         }
865
866         /*
867          * finding mean magnitude/phase if possible, otherwise
868          * just use the last value as the mean
869          */
870         if (mp_count)
871                 mp_avg /= mp_count;
872         else
873                 mp_avg = mp_coeff[nmeasurement - 1];
874
875         /* detect outlier */
876         if (abs(mp_max - mp_min) > max_delta) {
877                 if (abs(mp_max - mp_avg) > abs(mp_min - mp_avg))
878                         outlier_idx = max_idx;
879                 else
880                         outlier_idx = min_idx;
881
882                 mp_coeff[outlier_idx] = mp_avg;
883         }
884 }
885
886 static void ar9003_hw_tx_iq_cal_outlier_detection(struct ath_hw *ah,
887                                                   struct coeff *coeff,
888                                                   bool is_reusable)
889 {
890         int i, im, nmeasurement;
891         u32 tx_corr_coeff[MAX_MEASUREMENT][AR9300_MAX_CHAINS];
892         struct ath9k_hw_cal_data *caldata = ah->caldata;
893
894         memset(tx_corr_coeff, 0, sizeof(tx_corr_coeff));
895         for (i = 0; i < MAX_MEASUREMENT / 2; i++) {
896                 tx_corr_coeff[i * 2][0] = tx_corr_coeff[(i * 2) + 1][0] =
897                                         AR_PHY_TX_IQCAL_CORR_COEFF_B0(i);
898                 if (!AR_SREV_9485(ah)) {
899                         tx_corr_coeff[i * 2][1] =
900                         tx_corr_coeff[(i * 2) + 1][1] =
901                                         AR_PHY_TX_IQCAL_CORR_COEFF_B1(i);
902
903                         tx_corr_coeff[i * 2][2] =
904                         tx_corr_coeff[(i * 2) + 1][2] =
905                                         AR_PHY_TX_IQCAL_CORR_COEFF_B2(i);
906                 }
907         }
908
909         /* Load the average of 2 passes */
910         for (i = 0; i < AR9300_MAX_CHAINS; i++) {
911                 if (!(ah->txchainmask & (1 << i)))
912                         continue;
913                 nmeasurement = REG_READ_FIELD(ah,
914                                 AR_PHY_TX_IQCAL_STATUS_B0,
915                                 AR_PHY_CALIBRATED_GAINS_0);
916
917                 if (nmeasurement > MAX_MEASUREMENT)
918                         nmeasurement = MAX_MEASUREMENT;
919
920                 /* detect outlier only if nmeasurement > 1 */
921                 if (nmeasurement > 1) {
922                         /* Detect magnitude outlier */
923                         ar9003_hw_detect_outlier(coeff->mag_coeff[i],
924                                         nmeasurement, MAX_MAG_DELTA);
925
926                         /* Detect phase outlier */
927                         ar9003_hw_detect_outlier(coeff->phs_coeff[i],
928                                         nmeasurement, MAX_PHS_DELTA);
929                 }
930
931                 for (im = 0; im < nmeasurement; im++) {
932
933                         coeff->iqc_coeff[0] = (coeff->mag_coeff[i][im] & 0x7f) |
934                                 ((coeff->phs_coeff[i][im] & 0x7f) << 7);
935
936                         if ((im % 2) == 0)
937                                 REG_RMW_FIELD(ah, tx_corr_coeff[im][i],
938                                         AR_PHY_TX_IQCAL_CORR_COEFF_00_COEFF_TABLE,
939                                         coeff->iqc_coeff[0]);
940                         else
941                                 REG_RMW_FIELD(ah, tx_corr_coeff[im][i],
942                                         AR_PHY_TX_IQCAL_CORR_COEFF_01_COEFF_TABLE,
943                                         coeff->iqc_coeff[0]);
944
945                         if (caldata)
946                                 caldata->tx_corr_coeff[im][i] =
947                                         coeff->iqc_coeff[0];
948                 }
949                 if (caldata)
950                         caldata->num_measures[i] = nmeasurement;
951         }
952
953         REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_CONTROL_3,
954                       AR_PHY_TX_IQCAL_CONTROL_3_IQCORR_EN, 0x1);
955         REG_RMW_FIELD(ah, AR_PHY_RX_IQCAL_CORR_B0,
956                       AR_PHY_RX_IQCAL_CORR_B0_LOOPBACK_IQCORR_EN, 0x1);
957
958         if (caldata) {
959                 if (is_reusable)
960                         set_bit(TXIQCAL_DONE, &caldata->cal_flags);
961                 else
962                         clear_bit(TXIQCAL_DONE, &caldata->cal_flags);
963         }
964
965         return;
966 }
967
968 static bool ar9003_hw_tx_iq_cal_run(struct ath_hw *ah)
969 {
970         struct ath_common *common = ath9k_hw_common(ah);
971         u8 tx_gain_forced;
972
973         tx_gain_forced = REG_READ_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
974                                         AR_PHY_TXGAIN_FORCE);
975         if (tx_gain_forced)
976                 REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
977                               AR_PHY_TXGAIN_FORCE, 0);
978
979         REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_START,
980                       AR_PHY_TX_IQCAL_START_DO_CAL, 1);
981
982         if (!ath9k_hw_wait(ah, AR_PHY_TX_IQCAL_START,
983                         AR_PHY_TX_IQCAL_START_DO_CAL, 0,
984                         AH_WAIT_TIMEOUT)) {
985                 ath_dbg(common, CALIBRATE, "Tx IQ Cal is not completed\n");
986                 return false;
987         }
988         return true;
989 }
990
991 static void ar9003_hw_tx_iq_cal_post_proc(struct ath_hw *ah, bool is_reusable)
992 {
993         struct ath_common *common = ath9k_hw_common(ah);
994         const u32 txiqcal_status[AR9300_MAX_CHAINS] = {
995                 AR_PHY_TX_IQCAL_STATUS_B0,
996                 AR_PHY_TX_IQCAL_STATUS_B1,
997                 AR_PHY_TX_IQCAL_STATUS_B2,
998         };
999         const u_int32_t chan_info_tab[] = {
1000                 AR_PHY_CHAN_INFO_TAB_0,
1001                 AR_PHY_CHAN_INFO_TAB_1,
1002                 AR_PHY_CHAN_INFO_TAB_2,
1003         };
1004         struct coeff coeff;
1005         s32 iq_res[6];
1006         int i, im, j;
1007         int nmeasurement;
1008
1009         for (i = 0; i < AR9300_MAX_CHAINS; i++) {
1010                 if (!(ah->txchainmask & (1 << i)))
1011                         continue;
1012
1013                 nmeasurement = REG_READ_FIELD(ah,
1014                                 AR_PHY_TX_IQCAL_STATUS_B0,
1015                                 AR_PHY_CALIBRATED_GAINS_0);
1016                 if (nmeasurement > MAX_MEASUREMENT)
1017                         nmeasurement = MAX_MEASUREMENT;
1018
1019                 for (im = 0; im < nmeasurement; im++) {
1020                         ath_dbg(common, CALIBRATE,
1021                                 "Doing Tx IQ Cal for chain %d\n", i);
1022
1023                         if (REG_READ(ah, txiqcal_status[i]) &
1024                                         AR_PHY_TX_IQCAL_STATUS_FAILED) {
1025                                 ath_dbg(common, CALIBRATE,
1026                                         "Tx IQ Cal failed for chain %d\n", i);
1027                                 goto tx_iqcal_fail;
1028                         }
1029
1030                         for (j = 0; j < 3; j++) {
1031                                 u32 idx = 2 * j, offset = 4 * (3 * im + j);
1032
1033                                 REG_RMW_FIELD(ah,
1034                                                 AR_PHY_CHAN_INFO_MEMORY,
1035                                                 AR_PHY_CHAN_INFO_TAB_S2_READ,
1036                                                 0);
1037
1038                                 /* 32 bits */
1039                                 iq_res[idx] = REG_READ(ah,
1040                                                 chan_info_tab[i] +
1041                                                 offset);
1042
1043                                 REG_RMW_FIELD(ah,
1044                                                 AR_PHY_CHAN_INFO_MEMORY,
1045                                                 AR_PHY_CHAN_INFO_TAB_S2_READ,
1046                                                 1);
1047
1048                                 /* 16 bits */
1049                                 iq_res[idx + 1] = 0xffff & REG_READ(ah,
1050                                                 chan_info_tab[i] + offset);
1051
1052                                 ath_dbg(common, CALIBRATE,
1053                                         "IQ_RES[%d]=0x%x IQ_RES[%d]=0x%x\n",
1054                                         idx, iq_res[idx], idx + 1,
1055                                         iq_res[idx + 1]);
1056                         }
1057
1058                         if (!ar9003_hw_calc_iq_corr(ah, i, iq_res,
1059                                                 coeff.iqc_coeff)) {
1060                                 ath_dbg(common, CALIBRATE,
1061                                         "Failed in calculation of IQ correction\n");
1062                                 goto tx_iqcal_fail;
1063                         }
1064
1065                         coeff.mag_coeff[i][im] = coeff.iqc_coeff[0] & 0x7f;
1066                         coeff.phs_coeff[i][im] =
1067                                 (coeff.iqc_coeff[0] >> 7) & 0x7f;
1068
1069                         if (coeff.mag_coeff[i][im] > 63)
1070                                 coeff.mag_coeff[i][im] -= 128;
1071                         if (coeff.phs_coeff[i][im] > 63)
1072                                 coeff.phs_coeff[i][im] -= 128;
1073                 }
1074         }
1075         ar9003_hw_tx_iq_cal_outlier_detection(ah, &coeff, is_reusable);
1076
1077         return;
1078
1079 tx_iqcal_fail:
1080         ath_dbg(common, CALIBRATE, "Tx IQ Cal failed\n");
1081         return;
1082 }
1083
1084 static void ar9003_hw_tx_iq_cal_reload(struct ath_hw *ah)
1085 {
1086         struct ath9k_hw_cal_data *caldata = ah->caldata;
1087         u32 tx_corr_coeff[MAX_MEASUREMENT][AR9300_MAX_CHAINS];
1088         int i, im;
1089
1090         memset(tx_corr_coeff, 0, sizeof(tx_corr_coeff));
1091         for (i = 0; i < MAX_MEASUREMENT / 2; i++) {
1092                 tx_corr_coeff[i * 2][0] = tx_corr_coeff[(i * 2) + 1][0] =
1093                                         AR_PHY_TX_IQCAL_CORR_COEFF_B0(i);
1094                 if (!AR_SREV_9485(ah)) {
1095                         tx_corr_coeff[i * 2][1] =
1096                         tx_corr_coeff[(i * 2) + 1][1] =
1097                                         AR_PHY_TX_IQCAL_CORR_COEFF_B1(i);
1098
1099                         tx_corr_coeff[i * 2][2] =
1100                         tx_corr_coeff[(i * 2) + 1][2] =
1101                                         AR_PHY_TX_IQCAL_CORR_COEFF_B2(i);
1102                 }
1103         }
1104
1105         for (i = 0; i < AR9300_MAX_CHAINS; i++) {
1106                 if (!(ah->txchainmask & (1 << i)))
1107                         continue;
1108
1109                 for (im = 0; im < caldata->num_measures[i]; im++) {
1110                         if ((im % 2) == 0)
1111                                 REG_RMW_FIELD(ah, tx_corr_coeff[im][i],
1112                                      AR_PHY_TX_IQCAL_CORR_COEFF_00_COEFF_TABLE,
1113                                      caldata->tx_corr_coeff[im][i]);
1114                         else
1115                                 REG_RMW_FIELD(ah, tx_corr_coeff[im][i],
1116                                      AR_PHY_TX_IQCAL_CORR_COEFF_01_COEFF_TABLE,
1117                                      caldata->tx_corr_coeff[im][i]);
1118                 }
1119         }
1120
1121         REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_CONTROL_3,
1122                       AR_PHY_TX_IQCAL_CONTROL_3_IQCORR_EN, 0x1);
1123         REG_RMW_FIELD(ah, AR_PHY_RX_IQCAL_CORR_B0,
1124                       AR_PHY_RX_IQCAL_CORR_B0_LOOPBACK_IQCORR_EN, 0x1);
1125 }
1126
1127 static void ar9003_hw_manual_peak_cal(struct ath_hw *ah, u8 chain, bool is_2g)
1128 {
1129         int offset[8] = {0}, total = 0, test;
1130         int agc_out, i;
1131
1132         REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_GAINSTAGES(chain),
1133                       AR_PHY_65NM_RXRF_GAINSTAGES_RX_OVERRIDE, 0x1);
1134         REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_GAINSTAGES(chain),
1135                       AR_PHY_65NM_RXRF_GAINSTAGES_LNAON_CALDC, 0x0);
1136         if (is_2g)
1137                 REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_GAINSTAGES(chain),
1138                               AR_PHY_65NM_RXRF_GAINSTAGES_LNA2G_GAIN_OVR, 0x0);
1139         else
1140                 REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_GAINSTAGES(chain),
1141                               AR_PHY_65NM_RXRF_GAINSTAGES_LNA5G_GAIN_OVR, 0x0);
1142
1143         REG_RMW_FIELD(ah, AR_PHY_65NM_RXTX2(chain),
1144                       AR_PHY_65NM_RXTX2_RXON_OVR, 0x1);
1145         REG_RMW_FIELD(ah, AR_PHY_65NM_RXTX2(chain),
1146                       AR_PHY_65NM_RXTX2_RXON, 0x0);
1147
1148         REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(chain),
1149                       AR_PHY_65NM_RXRF_AGC_AGC_OVERRIDE, 0x1);
1150         REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(chain),
1151                       AR_PHY_65NM_RXRF_AGC_AGC_ON_OVR, 0x1);
1152         REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(chain),
1153                       AR_PHY_65NM_RXRF_AGC_AGC_CAL_OVR, 0x1);
1154
1155         if (AR_SREV_9330_11(ah)) {
1156                 REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(chain),
1157                               AR_PHY_65NM_RXRF_AGC_AGC2G_CALDAC_OVR, 0x0);
1158         } else {
1159                 if (is_2g)
1160                         REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(chain),
1161                                       AR_PHY_65NM_RXRF_AGC_AGC2G_DBDAC_OVR, 0x0);
1162                 else
1163                         REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(chain),
1164                                       AR_PHY_65NM_RXRF_AGC_AGC5G_DBDAC_OVR, 0x0);
1165         }
1166
1167         for (i = 6; i > 0; i--) {
1168                 offset[i] = BIT(i - 1);
1169                 test = total + offset[i];
1170
1171                 if (is_2g)
1172                         REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(chain),
1173                                       AR_PHY_65NM_RXRF_AGC_AGC2G_CALDAC_OVR,
1174                                       test);
1175                 else
1176                         REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(chain),
1177                                       AR_PHY_65NM_RXRF_AGC_AGC5G_CALDAC_OVR,
1178                                       test);
1179                 udelay(100);
1180                 agc_out = REG_READ_FIELD(ah, AR_PHY_65NM_RXRF_AGC(chain),
1181                                          AR_PHY_65NM_RXRF_AGC_AGC_OUT);
1182                 offset[i] = (agc_out) ? 0 : 1;
1183                 total += (offset[i] << (i - 1));
1184         }
1185
1186         if (is_2g)
1187                 REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(chain),
1188                               AR_PHY_65NM_RXRF_AGC_AGC2G_CALDAC_OVR, total);
1189         else
1190                 REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(chain),
1191                               AR_PHY_65NM_RXRF_AGC_AGC5G_CALDAC_OVR, total);
1192
1193         REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_GAINSTAGES(chain),
1194                       AR_PHY_65NM_RXRF_GAINSTAGES_RX_OVERRIDE, 0);
1195         REG_RMW_FIELD(ah, AR_PHY_65NM_RXTX2(chain),
1196                       AR_PHY_65NM_RXTX2_RXON_OVR, 0);
1197         REG_RMW_FIELD(ah, AR_PHY_65NM_RXRF_AGC(chain),
1198                       AR_PHY_65NM_RXRF_AGC_AGC_CAL_OVR, 0);
1199 }
1200
1201 static void ar9003_hw_do_pcoem_manual_peak_cal(struct ath_hw *ah,
1202                                                struct ath9k_channel *chan,
1203                                                bool run_rtt_cal)
1204 {
1205         struct ath9k_hw_cal_data *caldata = ah->caldata;
1206         int i;
1207
1208         if (!AR_SREV_9462(ah) && !AR_SREV_9565(ah) && !AR_SREV_9485(ah))
1209                 return;
1210
1211         if ((ah->caps.hw_caps & ATH9K_HW_CAP_RTT) && !run_rtt_cal)
1212                 return;
1213
1214         for (i = 0; i < AR9300_MAX_CHAINS; i++) {
1215                 if (!(ah->rxchainmask & (1 << i)))
1216                         continue;
1217                 ar9003_hw_manual_peak_cal(ah, i, IS_CHAN_2GHZ(chan));
1218         }
1219
1220         if (caldata)
1221                 set_bit(SW_PKDET_DONE, &caldata->cal_flags);
1222
1223         if ((ah->caps.hw_caps & ATH9K_HW_CAP_RTT) && caldata) {
1224                 if (IS_CHAN_2GHZ(chan)){
1225                         caldata->caldac[0] = REG_READ_FIELD(ah,
1226                                                     AR_PHY_65NM_RXRF_AGC(0),
1227                                                     AR_PHY_65NM_RXRF_AGC_AGC2G_CALDAC_OVR);
1228                         caldata->caldac[1] = REG_READ_FIELD(ah,
1229                                                     AR_PHY_65NM_RXRF_AGC(1),
1230                                                     AR_PHY_65NM_RXRF_AGC_AGC2G_CALDAC_OVR);
1231                 } else {
1232                         caldata->caldac[0] = REG_READ_FIELD(ah,
1233                                                     AR_PHY_65NM_RXRF_AGC(0),
1234                                                     AR_PHY_65NM_RXRF_AGC_AGC5G_CALDAC_OVR);
1235                         caldata->caldac[1] = REG_READ_FIELD(ah,
1236                                                     AR_PHY_65NM_RXRF_AGC(1),
1237                                                     AR_PHY_65NM_RXRF_AGC_AGC5G_CALDAC_OVR);
1238                 }
1239         }
1240 }
1241
1242 static void ar9003_hw_cl_cal_post_proc(struct ath_hw *ah, bool is_reusable)
1243 {
1244         u32 cl_idx[AR9300_MAX_CHAINS] = { AR_PHY_CL_TAB_0,
1245                                           AR_PHY_CL_TAB_1,
1246                                           AR_PHY_CL_TAB_2 };
1247         struct ath9k_hw_cal_data *caldata = ah->caldata;
1248         bool txclcal_done = false;
1249         int i, j;
1250
1251         if (!caldata || !(ah->enabled_cals & TX_CL_CAL))
1252                 return;
1253
1254         txclcal_done = !!(REG_READ(ah, AR_PHY_AGC_CONTROL) &
1255                           AR_PHY_AGC_CONTROL_CLC_SUCCESS);
1256
1257         if (test_bit(TXCLCAL_DONE, &caldata->cal_flags)) {
1258                 for (i = 0; i < AR9300_MAX_CHAINS; i++) {
1259                         if (!(ah->txchainmask & (1 << i)))
1260                                 continue;
1261                         for (j = 0; j < MAX_CL_TAB_ENTRY; j++)
1262                                 REG_WRITE(ah, CL_TAB_ENTRY(cl_idx[i]),
1263                                           caldata->tx_clcal[i][j]);
1264                 }
1265         } else if (is_reusable && txclcal_done) {
1266                 for (i = 0; i < AR9300_MAX_CHAINS; i++) {
1267                         if (!(ah->txchainmask & (1 << i)))
1268                                 continue;
1269                         for (j = 0; j < MAX_CL_TAB_ENTRY; j++)
1270                                 caldata->tx_clcal[i][j] =
1271                                         REG_READ(ah, CL_TAB_ENTRY(cl_idx[i]));
1272                 }
1273                 set_bit(TXCLCAL_DONE, &caldata->cal_flags);
1274         }
1275 }
1276
1277 static bool ar9003_hw_init_cal_pcoem(struct ath_hw *ah,
1278                                      struct ath9k_channel *chan)
1279 {
1280         struct ath_common *common = ath9k_hw_common(ah);
1281         struct ath9k_hw_cal_data *caldata = ah->caldata;
1282         bool txiqcal_done = false;
1283         bool is_reusable = true, status = true;
1284         bool run_rtt_cal = false, run_agc_cal;
1285         bool rtt = !!(ah->caps.hw_caps & ATH9K_HW_CAP_RTT);
1286         u32 rx_delay = 0;
1287         u32 agc_ctrl = 0, agc_supp_cals = AR_PHY_AGC_CONTROL_OFFSET_CAL |
1288                                           AR_PHY_AGC_CONTROL_FLTR_CAL   |
1289                                           AR_PHY_AGC_CONTROL_PKDET_CAL;
1290
1291         /* Use chip chainmask only for calibration */
1292         ar9003_hw_set_chain_masks(ah, ah->caps.rx_chainmask, ah->caps.tx_chainmask);
1293
1294         if (rtt) {
1295                 if (!ar9003_hw_rtt_restore(ah, chan))
1296                         run_rtt_cal = true;
1297
1298                 if (run_rtt_cal)
1299                         ath_dbg(common, CALIBRATE, "RTT calibration to be done\n");
1300         }
1301
1302         run_agc_cal = run_rtt_cal;
1303
1304         if (run_rtt_cal) {
1305                 ar9003_hw_rtt_enable(ah);
1306                 ar9003_hw_rtt_set_mask(ah, 0x00);
1307                 ar9003_hw_rtt_clear_hist(ah);
1308         }
1309
1310         if (rtt) {
1311                 if (!run_rtt_cal) {
1312                         agc_ctrl = REG_READ(ah, AR_PHY_AGC_CONTROL);
1313                         agc_supp_cals &= agc_ctrl;
1314                         agc_ctrl &= ~(AR_PHY_AGC_CONTROL_OFFSET_CAL |
1315                                       AR_PHY_AGC_CONTROL_FLTR_CAL |
1316                                       AR_PHY_AGC_CONTROL_PKDET_CAL);
1317                         REG_WRITE(ah, AR_PHY_AGC_CONTROL, agc_ctrl);
1318                 } else {
1319                         if (ah->ah_flags & AH_FASTCC)
1320                                 run_agc_cal = true;
1321                 }
1322         }
1323
1324         if (ah->enabled_cals & TX_CL_CAL) {
1325                 if (caldata && test_bit(TXCLCAL_DONE, &caldata->cal_flags))
1326                         REG_CLR_BIT(ah, AR_PHY_CL_CAL_CTL,
1327                                     AR_PHY_CL_CAL_ENABLE);
1328                 else {
1329                         REG_SET_BIT(ah, AR_PHY_CL_CAL_CTL,
1330                                     AR_PHY_CL_CAL_ENABLE);
1331                         run_agc_cal = true;
1332                 }
1333         }
1334
1335         if ((IS_CHAN_HALF_RATE(chan) || IS_CHAN_QUARTER_RATE(chan)) ||
1336             !(ah->enabled_cals & TX_IQ_CAL))
1337                 goto skip_tx_iqcal;
1338
1339         /* Do Tx IQ Calibration */
1340         REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_CONTROL_1,
1341                       AR_PHY_TX_IQCAL_CONTROL_1_IQCORR_I_Q_COFF_DELPT,
1342                       DELPT);
1343
1344         /*
1345          * For AR9485 or later chips, TxIQ cal runs as part of
1346          * AGC calibration
1347          */
1348         if (ah->enabled_cals & TX_IQ_ON_AGC_CAL) {
1349                 if (caldata && !test_bit(TXIQCAL_DONE, &caldata->cal_flags))
1350                         REG_SET_BIT(ah, AR_PHY_TX_IQCAL_CONTROL_0,
1351                                     AR_PHY_TX_IQCAL_CONTROL_0_ENABLE_TXIQ_CAL);
1352                 else
1353                         REG_CLR_BIT(ah, AR_PHY_TX_IQCAL_CONTROL_0,
1354                                     AR_PHY_TX_IQCAL_CONTROL_0_ENABLE_TXIQ_CAL);
1355                 txiqcal_done = run_agc_cal = true;
1356         }
1357
1358 skip_tx_iqcal:
1359         if (ath9k_hw_mci_is_enabled(ah) && IS_CHAN_2GHZ(chan) && run_agc_cal)
1360                 ar9003_mci_init_cal_req(ah, &is_reusable);
1361
1362         if (REG_READ(ah, AR_PHY_CL_CAL_CTL) & AR_PHY_CL_CAL_ENABLE) {
1363                 rx_delay = REG_READ(ah, AR_PHY_RX_DELAY);
1364                 /* Disable BB_active */
1365                 REG_WRITE(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_DIS);
1366                 udelay(5);
1367                 REG_WRITE(ah, AR_PHY_RX_DELAY, AR_PHY_RX_DELAY_DELAY);
1368                 REG_WRITE(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_EN);
1369         }
1370
1371         if (run_agc_cal || !(ah->ah_flags & AH_FASTCC)) {
1372                 /* Calibrate the AGC */
1373                 REG_WRITE(ah, AR_PHY_AGC_CONTROL,
1374                           REG_READ(ah, AR_PHY_AGC_CONTROL) |
1375                           AR_PHY_AGC_CONTROL_CAL);
1376
1377                 /* Poll for offset calibration complete */
1378                 status = ath9k_hw_wait(ah, AR_PHY_AGC_CONTROL,
1379                                        AR_PHY_AGC_CONTROL_CAL,
1380                                        0, AH_WAIT_TIMEOUT);
1381
1382                 ar9003_hw_do_pcoem_manual_peak_cal(ah, chan, run_rtt_cal);
1383         }
1384
1385         if (REG_READ(ah, AR_PHY_CL_CAL_CTL) & AR_PHY_CL_CAL_ENABLE) {
1386                 REG_WRITE(ah, AR_PHY_RX_DELAY, rx_delay);
1387                 udelay(5);
1388         }
1389
1390         if (ath9k_hw_mci_is_enabled(ah) && IS_CHAN_2GHZ(chan) && run_agc_cal)
1391                 ar9003_mci_init_cal_done(ah);
1392
1393         if (rtt && !run_rtt_cal) {
1394                 agc_ctrl |= agc_supp_cals;
1395                 REG_WRITE(ah, AR_PHY_AGC_CONTROL, agc_ctrl);
1396         }
1397
1398         if (!status) {
1399                 if (run_rtt_cal)
1400                         ar9003_hw_rtt_disable(ah);
1401
1402                 ath_dbg(common, CALIBRATE,
1403                         "offset calibration failed to complete in %d ms; noisy environment?\n",
1404                         AH_WAIT_TIMEOUT / 1000);
1405                 return false;
1406         }
1407
1408         if (txiqcal_done)
1409                 ar9003_hw_tx_iq_cal_post_proc(ah, is_reusable);
1410         else if (caldata && test_bit(TXIQCAL_DONE, &caldata->cal_flags))
1411                 ar9003_hw_tx_iq_cal_reload(ah);
1412
1413         ar9003_hw_cl_cal_post_proc(ah, is_reusable);
1414
1415         if (run_rtt_cal && caldata) {
1416                 if (is_reusable) {
1417                         if (!ath9k_hw_rfbus_req(ah)) {
1418                                 ath_err(ath9k_hw_common(ah),
1419                                         "Could not stop baseband\n");
1420                         } else {
1421                                 ar9003_hw_rtt_fill_hist(ah);
1422
1423                                 if (test_bit(SW_PKDET_DONE, &caldata->cal_flags))
1424                                         ar9003_hw_rtt_load_hist(ah);
1425                         }
1426
1427                         ath9k_hw_rfbus_done(ah);
1428                 }
1429
1430                 ar9003_hw_rtt_disable(ah);
1431         }
1432
1433         /* Revert chainmask to runtime parameters */
1434         ar9003_hw_set_chain_masks(ah, ah->rxchainmask, ah->txchainmask);
1435
1436         /* Initialize list pointers */
1437         ah->cal_list = ah->cal_list_last = ah->cal_list_curr = NULL;
1438
1439         INIT_CAL(&ah->iq_caldata);
1440         INSERT_CAL(ah, &ah->iq_caldata);
1441         ath_dbg(common, CALIBRATE, "enabling IQ Calibration\n");
1442
1443         /* Initialize current pointer to first element in list */
1444         ah->cal_list_curr = ah->cal_list;
1445
1446         if (ah->cal_list_curr)
1447                 ath9k_hw_reset_calibration(ah, ah->cal_list_curr);
1448
1449         if (caldata)
1450                 caldata->CalValid = 0;
1451
1452         return true;
1453 }
1454
1455 static bool ar9003_hw_init_cal_soc(struct ath_hw *ah,
1456                                    struct ath9k_channel *chan)
1457 {
1458         struct ath_common *common = ath9k_hw_common(ah);
1459         struct ath9k_hw_cal_data *caldata = ah->caldata;
1460         bool txiqcal_done = false;
1461         bool status = true;
1462         bool run_agc_cal = false, sep_iq_cal = false;
1463
1464         /* Use chip chainmask only for calibration */
1465         ar9003_hw_set_chain_masks(ah, ah->caps.rx_chainmask, ah->caps.tx_chainmask);
1466
1467         if (ah->enabled_cals & TX_CL_CAL) {
1468                 REG_SET_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_CL_CAL_ENABLE);
1469                 run_agc_cal = true;
1470         }
1471
1472         if (IS_CHAN_HALF_RATE(chan) || IS_CHAN_QUARTER_RATE(chan))
1473                 goto skip_tx_iqcal;
1474
1475         /* Do Tx IQ Calibration */
1476         REG_RMW_FIELD(ah, AR_PHY_TX_IQCAL_CONTROL_1,
1477                       AR_PHY_TX_IQCAL_CONTROL_1_IQCORR_I_Q_COFF_DELPT,
1478                       DELPT);
1479
1480         /*
1481          * For AR9485 or later chips, TxIQ cal runs as part of
1482          * AGC calibration. Specifically, AR9550 in SoC chips.
1483          */
1484         if (ah->enabled_cals & TX_IQ_ON_AGC_CAL) {
1485                 if (REG_READ_FIELD(ah, AR_PHY_TX_IQCAL_CONTROL_0,
1486                                    AR_PHY_TX_IQCAL_CONTROL_0_ENABLE_TXIQ_CAL)) {
1487                                 txiqcal_done = true;
1488                 } else {
1489                         txiqcal_done = false;
1490                 }
1491                 run_agc_cal = true;
1492         } else {
1493                 sep_iq_cal = true;
1494                 run_agc_cal = true;
1495         }
1496
1497         /*
1498          * In the SoC family, this will run for AR9300, AR9331 and AR9340.
1499          */
1500         if (sep_iq_cal) {
1501                 txiqcal_done = ar9003_hw_tx_iq_cal_run(ah);
1502                 REG_WRITE(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_DIS);
1503                 udelay(5);
1504                 REG_WRITE(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_EN);
1505         }
1506
1507         if (AR_SREV_9550(ah) && IS_CHAN_2GHZ(chan)) {
1508                 if (!ar9003_hw_dynamic_osdac_selection(ah, txiqcal_done))
1509                         return false;
1510         }
1511
1512 skip_tx_iqcal:
1513         if (run_agc_cal || !(ah->ah_flags & AH_FASTCC)) {
1514                 if (AR_SREV_9330_11(ah))
1515                         ar9003_hw_manual_peak_cal(ah, 0, IS_CHAN_2GHZ(chan));
1516
1517                 /* Calibrate the AGC */
1518                 REG_WRITE(ah, AR_PHY_AGC_CONTROL,
1519                           REG_READ(ah, AR_PHY_AGC_CONTROL) |
1520                           AR_PHY_AGC_CONTROL_CAL);
1521
1522                 /* Poll for offset calibration complete */
1523                 status = ath9k_hw_wait(ah, AR_PHY_AGC_CONTROL,
1524                                        AR_PHY_AGC_CONTROL_CAL,
1525                                        0, AH_WAIT_TIMEOUT);
1526         }
1527
1528         if (!status) {
1529                 ath_dbg(common, CALIBRATE,
1530                         "offset calibration failed to complete in %d ms; noisy environment?\n",
1531                         AH_WAIT_TIMEOUT / 1000);
1532                 return false;
1533         }
1534
1535         if (txiqcal_done)
1536                 ar9003_hw_tx_iq_cal_post_proc(ah, false);
1537
1538         /* Revert chainmask to runtime parameters */
1539         ar9003_hw_set_chain_masks(ah, ah->rxchainmask, ah->txchainmask);
1540
1541         /* Initialize list pointers */
1542         ah->cal_list = ah->cal_list_last = ah->cal_list_curr = NULL;
1543
1544         INIT_CAL(&ah->iq_caldata);
1545         INSERT_CAL(ah, &ah->iq_caldata);
1546         ath_dbg(common, CALIBRATE, "enabling IQ Calibration\n");
1547
1548         /* Initialize current pointer to first element in list */
1549         ah->cal_list_curr = ah->cal_list;
1550
1551         if (ah->cal_list_curr)
1552                 ath9k_hw_reset_calibration(ah, ah->cal_list_curr);
1553
1554         if (caldata)
1555                 caldata->CalValid = 0;
1556
1557         return true;
1558 }
1559
1560 void ar9003_hw_attach_calib_ops(struct ath_hw *ah)
1561 {
1562         struct ath_hw_private_ops *priv_ops = ath9k_hw_private_ops(ah);
1563         struct ath_hw_ops *ops = ath9k_hw_ops(ah);
1564
1565         if (AR_SREV_9485(ah) || AR_SREV_9462(ah) || AR_SREV_9565(ah))
1566                 priv_ops->init_cal = ar9003_hw_init_cal_pcoem;
1567         else
1568                 priv_ops->init_cal = ar9003_hw_init_cal_soc;
1569
1570         priv_ops->init_cal_settings = ar9003_hw_init_cal_settings;
1571         priv_ops->setup_calibration = ar9003_hw_setup_calibration;
1572
1573         ops->calibrate = ar9003_hw_calibrate;
1574 }