1795f5e2cSLuis R. Rodriguez /*
25b68138eSSujith Manoharan * Copyright (c) 2008-2011 Atheros Communications Inc.
3795f5e2cSLuis R. Rodriguez *
4795f5e2cSLuis R. Rodriguez * Permission to use, copy, modify, and/or distribute this software for any
5795f5e2cSLuis R. Rodriguez * purpose with or without fee is hereby granted, provided that the above
6795f5e2cSLuis R. Rodriguez * copyright notice and this permission notice appear in all copies.
7795f5e2cSLuis R. Rodriguez *
8795f5e2cSLuis R. Rodriguez * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9795f5e2cSLuis R. Rodriguez * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10795f5e2cSLuis R. Rodriguez * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11795f5e2cSLuis R. Rodriguez * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12795f5e2cSLuis R. Rodriguez * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13795f5e2cSLuis R. Rodriguez * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14795f5e2cSLuis R. Rodriguez * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15795f5e2cSLuis R. Rodriguez */
16795f5e2cSLuis R. Rodriguez
17795f5e2cSLuis R. Rodriguez #include "hw.h"
18795f5e2cSLuis R. Rodriguez #include "hw-ops.h"
19795f5e2cSLuis R. Rodriguez #include "ar9002_phy.h"
20795f5e2cSLuis R. Rodriguez
21795f5e2cSLuis R. Rodriguez #define AR9285_CLCAL_REDO_THRESH 1
22d8d20845SSergey Ryazanov /* AGC & I/Q calibrations time limit, ms */
23d8d20845SSergey Ryazanov #define AR9002_CAL_MAX_TIME 30000
24795f5e2cSLuis R. Rodriguez
256497827fSFelix Fietkau enum ar9002_cal_types {
266497827fSFelix Fietkau ADC_GAIN_CAL = BIT(0),
276497827fSFelix Fietkau ADC_DC_CAL = BIT(1),
286497827fSFelix Fietkau IQ_MISMATCH_CAL = BIT(2),
296497827fSFelix Fietkau };
306497827fSFelix Fietkau
ar9002_hw_is_cal_supported(struct ath_hw * ah,struct ath9k_channel * chan,enum ar9002_cal_types cal_type)3181544026SRajkumar Manoharan static bool ar9002_hw_is_cal_supported(struct ath_hw *ah,
3281544026SRajkumar Manoharan struct ath9k_channel *chan,
3381544026SRajkumar Manoharan enum ar9002_cal_types cal_type)
3481544026SRajkumar Manoharan {
3581544026SRajkumar Manoharan bool supported = false;
3681544026SRajkumar Manoharan switch (ah->supp_cals & cal_type) {
3781544026SRajkumar Manoharan case IQ_MISMATCH_CAL:
3881544026SRajkumar Manoharan supported = true;
3981544026SRajkumar Manoharan break;
4081544026SRajkumar Manoharan case ADC_GAIN_CAL:
4181544026SRajkumar Manoharan case ADC_DC_CAL:
42d6cae2bcSSergey Ryazanov /* Run even/odd ADCs calibrations for HT40 channels only */
43d6cae2bcSSergey Ryazanov if (IS_CHAN_HT40(chan))
4481544026SRajkumar Manoharan supported = true;
4581544026SRajkumar Manoharan break;
4681544026SRajkumar Manoharan }
4781544026SRajkumar Manoharan return supported;
4881544026SRajkumar Manoharan }
496497827fSFelix Fietkau
ar9002_hw_setup_calibration(struct ath_hw * ah,struct ath9k_cal_list * currCal)50795f5e2cSLuis R. Rodriguez static void ar9002_hw_setup_calibration(struct ath_hw *ah,
51795f5e2cSLuis R. Rodriguez struct ath9k_cal_list *currCal)
52795f5e2cSLuis R. Rodriguez {
53795f5e2cSLuis R. Rodriguez struct ath_common *common = ath9k_hw_common(ah);
54795f5e2cSLuis R. Rodriguez
55795f5e2cSLuis R. Rodriguez REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4(0),
56795f5e2cSLuis R. Rodriguez AR_PHY_TIMING_CTRL4_IQCAL_LOG_COUNT_MAX,
57795f5e2cSLuis R. Rodriguez currCal->calData->calCountMax);
58795f5e2cSLuis R. Rodriguez
59795f5e2cSLuis R. Rodriguez switch (currCal->calData->calType) {
60795f5e2cSLuis R. Rodriguez case IQ_MISMATCH_CAL:
61795f5e2cSLuis R. Rodriguez REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_IQ);
62d2182b69SJoe Perches ath_dbg(common, CALIBRATE,
63795f5e2cSLuis R. Rodriguez "starting IQ Mismatch Calibration\n");
64795f5e2cSLuis R. Rodriguez break;
65795f5e2cSLuis R. Rodriguez case ADC_GAIN_CAL:
66795f5e2cSLuis R. Rodriguez REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_ADC_GAIN);
67d2182b69SJoe Perches ath_dbg(common, CALIBRATE, "starting ADC Gain Calibration\n");
68795f5e2cSLuis R. Rodriguez break;
69795f5e2cSLuis R. Rodriguez case ADC_DC_CAL:
70795f5e2cSLuis R. Rodriguez REG_WRITE(ah, AR_PHY_CALMODE, AR_PHY_CALMODE_ADC_DC_PER);
71d2182b69SJoe Perches ath_dbg(common, CALIBRATE, "starting ADC DC Calibration\n");
72795f5e2cSLuis R. Rodriguez break;
73795f5e2cSLuis R. Rodriguez }
74795f5e2cSLuis R. Rodriguez
75795f5e2cSLuis R. Rodriguez REG_SET_BIT(ah, AR_PHY_TIMING_CTRL4(0),
76795f5e2cSLuis R. Rodriguez AR_PHY_TIMING_CTRL4_DO_CAL);
77795f5e2cSLuis R. Rodriguez }
78795f5e2cSLuis R. Rodriguez
ar9002_hw_per_calibration(struct ath_hw * ah,struct ath9k_channel * ichan,u8 rxchainmask,struct ath9k_cal_list * currCal)79795f5e2cSLuis R. Rodriguez static bool ar9002_hw_per_calibration(struct ath_hw *ah,
80795f5e2cSLuis R. Rodriguez struct ath9k_channel *ichan,
81795f5e2cSLuis R. Rodriguez u8 rxchainmask,
82795f5e2cSLuis R. Rodriguez struct ath9k_cal_list *currCal)
83795f5e2cSLuis R. Rodriguez {
8420bd2a09SFelix Fietkau struct ath9k_hw_cal_data *caldata = ah->caldata;
85795f5e2cSLuis R. Rodriguez bool iscaldone = false;
86795f5e2cSLuis R. Rodriguez
87795f5e2cSLuis R. Rodriguez if (currCal->calState == CAL_RUNNING) {
88795f5e2cSLuis R. Rodriguez if (!(REG_READ(ah, AR_PHY_TIMING_CTRL4(0)) &
89795f5e2cSLuis R. Rodriguez AR_PHY_TIMING_CTRL4_DO_CAL)) {
90795f5e2cSLuis R. Rodriguez
91795f5e2cSLuis R. Rodriguez currCal->calData->calCollect(ah);
92795f5e2cSLuis R. Rodriguez ah->cal_samples++;
93795f5e2cSLuis R. Rodriguez
94795f5e2cSLuis R. Rodriguez if (ah->cal_samples >=
95795f5e2cSLuis R. Rodriguez currCal->calData->calNumSamples) {
96795f5e2cSLuis R. Rodriguez int i, numChains = 0;
97795f5e2cSLuis R. Rodriguez for (i = 0; i < AR5416_MAX_CHAINS; i++) {
98795f5e2cSLuis R. Rodriguez if (rxchainmask & (1 << i))
99795f5e2cSLuis R. Rodriguez numChains++;
100795f5e2cSLuis R. Rodriguez }
101795f5e2cSLuis R. Rodriguez
102795f5e2cSLuis R. Rodriguez currCal->calData->calPostProc(ah, numChains);
10320bd2a09SFelix Fietkau caldata->CalValid |= currCal->calData->calType;
104795f5e2cSLuis R. Rodriguez currCal->calState = CAL_DONE;
105795f5e2cSLuis R. Rodriguez iscaldone = true;
106795f5e2cSLuis R. Rodriguez } else {
107795f5e2cSLuis R. Rodriguez ar9002_hw_setup_calibration(ah, currCal);
108795f5e2cSLuis R. Rodriguez }
109d8d20845SSergey Ryazanov } else if (time_after(jiffies, ah->cal_start_time +
110d8d20845SSergey Ryazanov msecs_to_jiffies(AR9002_CAL_MAX_TIME))) {
111d8d20845SSergey Ryazanov REG_CLR_BIT(ah, AR_PHY_TIMING_CTRL4(0),
112d8d20845SSergey Ryazanov AR_PHY_TIMING_CTRL4_DO_CAL);
113d8d20845SSergey Ryazanov ath_dbg(ath9k_hw_common(ah), CALIBRATE,
114d8d20845SSergey Ryazanov "calibration timeout\n");
115d8d20845SSergey Ryazanov currCal->calState = CAL_WAITING; /* Try later */
116d8d20845SSergey Ryazanov iscaldone = true;
117795f5e2cSLuis R. Rodriguez }
11820bd2a09SFelix Fietkau } else if (!(caldata->CalValid & currCal->calData->calType)) {
119795f5e2cSLuis R. Rodriguez ath9k_hw_reset_calibration(ah, currCal);
120795f5e2cSLuis R. Rodriguez }
121795f5e2cSLuis R. Rodriguez
122795f5e2cSLuis R. Rodriguez return iscaldone;
123795f5e2cSLuis R. Rodriguez }
124795f5e2cSLuis R. Rodriguez
ar9002_hw_iqcal_collect(struct ath_hw * ah)125795f5e2cSLuis R. Rodriguez static void ar9002_hw_iqcal_collect(struct ath_hw *ah)
126795f5e2cSLuis R. Rodriguez {
127795f5e2cSLuis R. Rodriguez int i;
128795f5e2cSLuis R. Rodriguez
129795f5e2cSLuis R. Rodriguez for (i = 0; i < AR5416_MAX_CHAINS; i++) {
130795f5e2cSLuis R. Rodriguez ah->totalPowerMeasI[i] +=
131795f5e2cSLuis R. Rodriguez REG_READ(ah, AR_PHY_CAL_MEAS_0(i));
132795f5e2cSLuis R. Rodriguez ah->totalPowerMeasQ[i] +=
133795f5e2cSLuis R. Rodriguez REG_READ(ah, AR_PHY_CAL_MEAS_1(i));
134795f5e2cSLuis R. Rodriguez ah->totalIqCorrMeas[i] +=
135795f5e2cSLuis R. Rodriguez (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_2(i));
136d2182b69SJoe Perches ath_dbg(ath9k_hw_common(ah), CALIBRATE,
137795f5e2cSLuis R. Rodriguez "%d: Chn %d pmi=0x%08x;pmq=0x%08x;iqcm=0x%08x;\n",
138795f5e2cSLuis R. Rodriguez ah->cal_samples, i, ah->totalPowerMeasI[i],
139795f5e2cSLuis R. Rodriguez ah->totalPowerMeasQ[i],
140795f5e2cSLuis R. Rodriguez ah->totalIqCorrMeas[i]);
141795f5e2cSLuis R. Rodriguez }
142795f5e2cSLuis R. Rodriguez }
143795f5e2cSLuis R. Rodriguez
ar9002_hw_adc_gaincal_collect(struct ath_hw * ah)144795f5e2cSLuis R. Rodriguez static void ar9002_hw_adc_gaincal_collect(struct ath_hw *ah)
145795f5e2cSLuis R. Rodriguez {
146795f5e2cSLuis R. Rodriguez int i;
147795f5e2cSLuis R. Rodriguez
148795f5e2cSLuis R. Rodriguez for (i = 0; i < AR5416_MAX_CHAINS; i++) {
149795f5e2cSLuis R. Rodriguez ah->totalAdcIOddPhase[i] +=
150795f5e2cSLuis R. Rodriguez REG_READ(ah, AR_PHY_CAL_MEAS_0(i));
151795f5e2cSLuis R. Rodriguez ah->totalAdcIEvenPhase[i] +=
152795f5e2cSLuis R. Rodriguez REG_READ(ah, AR_PHY_CAL_MEAS_1(i));
153795f5e2cSLuis R. Rodriguez ah->totalAdcQOddPhase[i] +=
154795f5e2cSLuis R. Rodriguez REG_READ(ah, AR_PHY_CAL_MEAS_2(i));
155795f5e2cSLuis R. Rodriguez ah->totalAdcQEvenPhase[i] +=
156795f5e2cSLuis R. Rodriguez REG_READ(ah, AR_PHY_CAL_MEAS_3(i));
157795f5e2cSLuis R. Rodriguez
158d2182b69SJoe Perches ath_dbg(ath9k_hw_common(ah), CALIBRATE,
159226afe68SJoe Perches "%d: Chn %d oddi=0x%08x; eveni=0x%08x; oddq=0x%08x; evenq=0x%08x;\n",
160795f5e2cSLuis R. Rodriguez ah->cal_samples, i,
161795f5e2cSLuis R. Rodriguez ah->totalAdcIOddPhase[i],
162795f5e2cSLuis R. Rodriguez ah->totalAdcIEvenPhase[i],
163795f5e2cSLuis R. Rodriguez ah->totalAdcQOddPhase[i],
164795f5e2cSLuis R. Rodriguez ah->totalAdcQEvenPhase[i]);
165795f5e2cSLuis R. Rodriguez }
166795f5e2cSLuis R. Rodriguez }
167795f5e2cSLuis R. Rodriguez
ar9002_hw_adc_dccal_collect(struct ath_hw * ah)168795f5e2cSLuis R. Rodriguez static void ar9002_hw_adc_dccal_collect(struct ath_hw *ah)
169795f5e2cSLuis R. Rodriguez {
170795f5e2cSLuis R. Rodriguez int i;
171795f5e2cSLuis R. Rodriguez
172795f5e2cSLuis R. Rodriguez for (i = 0; i < AR5416_MAX_CHAINS; i++) {
173795f5e2cSLuis R. Rodriguez ah->totalAdcDcOffsetIOddPhase[i] +=
174795f5e2cSLuis R. Rodriguez (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_0(i));
175795f5e2cSLuis R. Rodriguez ah->totalAdcDcOffsetIEvenPhase[i] +=
176795f5e2cSLuis R. Rodriguez (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_1(i));
177795f5e2cSLuis R. Rodriguez ah->totalAdcDcOffsetQOddPhase[i] +=
178795f5e2cSLuis R. Rodriguez (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_2(i));
179795f5e2cSLuis R. Rodriguez ah->totalAdcDcOffsetQEvenPhase[i] +=
180795f5e2cSLuis R. Rodriguez (int32_t) REG_READ(ah, AR_PHY_CAL_MEAS_3(i));
181795f5e2cSLuis R. Rodriguez
182d2182b69SJoe Perches ath_dbg(ath9k_hw_common(ah), CALIBRATE,
183226afe68SJoe Perches "%d: Chn %d oddi=0x%08x; eveni=0x%08x; oddq=0x%08x; evenq=0x%08x;\n",
184795f5e2cSLuis R. Rodriguez ah->cal_samples, i,
185795f5e2cSLuis R. Rodriguez ah->totalAdcDcOffsetIOddPhase[i],
186795f5e2cSLuis R. Rodriguez ah->totalAdcDcOffsetIEvenPhase[i],
187795f5e2cSLuis R. Rodriguez ah->totalAdcDcOffsetQOddPhase[i],
188795f5e2cSLuis R. Rodriguez ah->totalAdcDcOffsetQEvenPhase[i]);
189795f5e2cSLuis R. Rodriguez }
190795f5e2cSLuis R. Rodriguez }
191795f5e2cSLuis R. Rodriguez
ar9002_hw_iqcalibrate(struct ath_hw * ah,u8 numChains)192795f5e2cSLuis R. Rodriguez static void ar9002_hw_iqcalibrate(struct ath_hw *ah, u8 numChains)
193795f5e2cSLuis R. Rodriguez {
194795f5e2cSLuis R. Rodriguez struct ath_common *common = ath9k_hw_common(ah);
195795f5e2cSLuis R. Rodriguez u32 powerMeasQ, powerMeasI, iqCorrMeas;
196795f5e2cSLuis R. Rodriguez u32 qCoffDenom, iCoffDenom;
197795f5e2cSLuis R. Rodriguez int32_t qCoff, iCoff;
198795f5e2cSLuis R. Rodriguez int iqCorrNeg, i;
199795f5e2cSLuis R. Rodriguez
200795f5e2cSLuis R. Rodriguez for (i = 0; i < numChains; i++) {
201795f5e2cSLuis R. Rodriguez powerMeasI = ah->totalPowerMeasI[i];
202795f5e2cSLuis R. Rodriguez powerMeasQ = ah->totalPowerMeasQ[i];
203795f5e2cSLuis R. Rodriguez iqCorrMeas = ah->totalIqCorrMeas[i];
204795f5e2cSLuis R. Rodriguez
205d2182b69SJoe Perches ath_dbg(common, CALIBRATE,
206795f5e2cSLuis R. Rodriguez "Starting IQ Cal and Correction for Chain %d\n",
207795f5e2cSLuis R. Rodriguez i);
208795f5e2cSLuis R. Rodriguez
209d2182b69SJoe Perches ath_dbg(common, CALIBRATE,
210a9b2ce03SNikolay Martynov "Original: Chn %d iq_corr_meas = 0x%08x\n",
211795f5e2cSLuis R. Rodriguez i, ah->totalIqCorrMeas[i]);
212795f5e2cSLuis R. Rodriguez
213795f5e2cSLuis R. Rodriguez iqCorrNeg = 0;
214795f5e2cSLuis R. Rodriguez
215795f5e2cSLuis R. Rodriguez if (iqCorrMeas > 0x80000000) {
216795f5e2cSLuis R. Rodriguez iqCorrMeas = (0xffffffff - iqCorrMeas) + 1;
217795f5e2cSLuis R. Rodriguez iqCorrNeg = 1;
218795f5e2cSLuis R. Rodriguez }
219795f5e2cSLuis R. Rodriguez
220d2182b69SJoe Perches ath_dbg(common, CALIBRATE, "Chn %d pwr_meas_i = 0x%08x\n",
221d2182b69SJoe Perches i, powerMeasI);
222d2182b69SJoe Perches ath_dbg(common, CALIBRATE, "Chn %d pwr_meas_q = 0x%08x\n",
223d2182b69SJoe Perches i, powerMeasQ);
224d2182b69SJoe Perches ath_dbg(common, CALIBRATE, "iqCorrNeg is 0x%08x\n", iqCorrNeg);
225795f5e2cSLuis R. Rodriguez
226795f5e2cSLuis R. Rodriguez iCoffDenom = (powerMeasI / 2 + powerMeasQ / 2) / 128;
227795f5e2cSLuis R. Rodriguez qCoffDenom = powerMeasQ / 64;
228795f5e2cSLuis R. Rodriguez
229795f5e2cSLuis R. Rodriguez if ((powerMeasQ != 0) && (iCoffDenom != 0) &&
230795f5e2cSLuis R. Rodriguez (qCoffDenom != 0)) {
231795f5e2cSLuis R. Rodriguez iCoff = iqCorrMeas / iCoffDenom;
232795f5e2cSLuis R. Rodriguez qCoff = powerMeasI / qCoffDenom - 64;
233d2182b69SJoe Perches ath_dbg(common, CALIBRATE, "Chn %d iCoff = 0x%08x\n",
234d2182b69SJoe Perches i, iCoff);
235d2182b69SJoe Perches ath_dbg(common, CALIBRATE, "Chn %d qCoff = 0x%08x\n",
236d2182b69SJoe Perches i, qCoff);
237795f5e2cSLuis R. Rodriguez
238795f5e2cSLuis R. Rodriguez iCoff = iCoff & 0x3f;
239d2182b69SJoe Perches ath_dbg(common, CALIBRATE,
240795f5e2cSLuis R. Rodriguez "New: Chn %d iCoff = 0x%08x\n", i, iCoff);
241795f5e2cSLuis R. Rodriguez if (iqCorrNeg == 0x0)
242795f5e2cSLuis R. Rodriguez iCoff = 0x40 - iCoff;
243795f5e2cSLuis R. Rodriguez
244795f5e2cSLuis R. Rodriguez if (qCoff > 15)
245795f5e2cSLuis R. Rodriguez qCoff = 15;
246795f5e2cSLuis R. Rodriguez else if (qCoff <= -16)
24723399016SFelix Fietkau qCoff = -16;
248795f5e2cSLuis R. Rodriguez
249d2182b69SJoe Perches ath_dbg(common, CALIBRATE,
250795f5e2cSLuis R. Rodriguez "Chn %d : iCoff = 0x%x qCoff = 0x%x\n",
251795f5e2cSLuis R. Rodriguez i, iCoff, qCoff);
252795f5e2cSLuis R. Rodriguez
253795f5e2cSLuis R. Rodriguez REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4(i),
254795f5e2cSLuis R. Rodriguez AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF,
255795f5e2cSLuis R. Rodriguez iCoff);
256795f5e2cSLuis R. Rodriguez REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4(i),
257795f5e2cSLuis R. Rodriguez AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF,
258795f5e2cSLuis R. Rodriguez qCoff);
259d2182b69SJoe Perches ath_dbg(common, CALIBRATE,
260795f5e2cSLuis R. Rodriguez "IQ Cal and Correction done for Chain %d\n",
261795f5e2cSLuis R. Rodriguez i);
262795f5e2cSLuis R. Rodriguez }
263795f5e2cSLuis R. Rodriguez }
264795f5e2cSLuis R. Rodriguez
265795f5e2cSLuis R. Rodriguez REG_SET_BIT(ah, AR_PHY_TIMING_CTRL4(0),
266795f5e2cSLuis R. Rodriguez AR_PHY_TIMING_CTRL4_IQCORR_ENABLE);
267795f5e2cSLuis R. Rodriguez }
268795f5e2cSLuis R. Rodriguez
ar9002_hw_adc_gaincal_calibrate(struct ath_hw * ah,u8 numChains)269795f5e2cSLuis R. Rodriguez static void ar9002_hw_adc_gaincal_calibrate(struct ath_hw *ah, u8 numChains)
270795f5e2cSLuis R. Rodriguez {
271795f5e2cSLuis R. Rodriguez struct ath_common *common = ath9k_hw_common(ah);
272795f5e2cSLuis R. Rodriguez u32 iOddMeasOffset, iEvenMeasOffset, qOddMeasOffset, qEvenMeasOffset;
273795f5e2cSLuis R. Rodriguez u32 qGainMismatch, iGainMismatch, val, i;
274795f5e2cSLuis R. Rodriguez
275795f5e2cSLuis R. Rodriguez for (i = 0; i < numChains; i++) {
276795f5e2cSLuis R. Rodriguez iOddMeasOffset = ah->totalAdcIOddPhase[i];
277795f5e2cSLuis R. Rodriguez iEvenMeasOffset = ah->totalAdcIEvenPhase[i];
278795f5e2cSLuis R. Rodriguez qOddMeasOffset = ah->totalAdcQOddPhase[i];
279795f5e2cSLuis R. Rodriguez qEvenMeasOffset = ah->totalAdcQEvenPhase[i];
280795f5e2cSLuis R. Rodriguez
281d2182b69SJoe Perches ath_dbg(common, CALIBRATE,
282795f5e2cSLuis R. Rodriguez "Starting ADC Gain Cal for Chain %d\n", i);
283795f5e2cSLuis R. Rodriguez
284d2182b69SJoe Perches ath_dbg(common, CALIBRATE, "Chn %d pwr_meas_odd_i = 0x%08x\n",
285d2182b69SJoe Perches i, iOddMeasOffset);
286d2182b69SJoe Perches ath_dbg(common, CALIBRATE, "Chn %d pwr_meas_even_i = 0x%08x\n",
287d2182b69SJoe Perches i, iEvenMeasOffset);
288d2182b69SJoe Perches ath_dbg(common, CALIBRATE, "Chn %d pwr_meas_odd_q = 0x%08x\n",
289d2182b69SJoe Perches i, qOddMeasOffset);
290d2182b69SJoe Perches ath_dbg(common, CALIBRATE, "Chn %d pwr_meas_even_q = 0x%08x\n",
291d2182b69SJoe Perches i, qEvenMeasOffset);
292795f5e2cSLuis R. Rodriguez
293795f5e2cSLuis R. Rodriguez if (iOddMeasOffset != 0 && qEvenMeasOffset != 0) {
294795f5e2cSLuis R. Rodriguez iGainMismatch =
295795f5e2cSLuis R. Rodriguez ((iEvenMeasOffset * 32) /
296795f5e2cSLuis R. Rodriguez iOddMeasOffset) & 0x3f;
297795f5e2cSLuis R. Rodriguez qGainMismatch =
298795f5e2cSLuis R. Rodriguez ((qOddMeasOffset * 32) /
299795f5e2cSLuis R. Rodriguez qEvenMeasOffset) & 0x3f;
300795f5e2cSLuis R. Rodriguez
301d2182b69SJoe Perches ath_dbg(common, CALIBRATE,
302d2182b69SJoe Perches "Chn %d gain_mismatch_i = 0x%08x\n",
303d2182b69SJoe Perches i, iGainMismatch);
304d2182b69SJoe Perches ath_dbg(common, CALIBRATE,
305d2182b69SJoe Perches "Chn %d gain_mismatch_q = 0x%08x\n",
306d2182b69SJoe Perches i, qGainMismatch);
307795f5e2cSLuis R. Rodriguez
308795f5e2cSLuis R. Rodriguez val = REG_READ(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(i));
309795f5e2cSLuis R. Rodriguez val &= 0xfffff000;
310795f5e2cSLuis R. Rodriguez val |= (qGainMismatch) | (iGainMismatch << 6);
311795f5e2cSLuis R. Rodriguez REG_WRITE(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(i), val);
312795f5e2cSLuis R. Rodriguez
313d2182b69SJoe Perches ath_dbg(common, CALIBRATE,
314795f5e2cSLuis R. Rodriguez "ADC Gain Cal done for Chain %d\n", i);
315795f5e2cSLuis R. Rodriguez }
316795f5e2cSLuis R. Rodriguez }
317795f5e2cSLuis R. Rodriguez
318795f5e2cSLuis R. Rodriguez REG_WRITE(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(0),
319795f5e2cSLuis R. Rodriguez REG_READ(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(0)) |
320795f5e2cSLuis R. Rodriguez AR_PHY_NEW_ADC_GAIN_CORR_ENABLE);
321795f5e2cSLuis R. Rodriguez }
322795f5e2cSLuis R. Rodriguez
ar9002_hw_adc_dccal_calibrate(struct ath_hw * ah,u8 numChains)323795f5e2cSLuis R. Rodriguez static void ar9002_hw_adc_dccal_calibrate(struct ath_hw *ah, u8 numChains)
324795f5e2cSLuis R. Rodriguez {
325795f5e2cSLuis R. Rodriguez struct ath_common *common = ath9k_hw_common(ah);
326795f5e2cSLuis R. Rodriguez u32 iOddMeasOffset, iEvenMeasOffset, val, i;
327795f5e2cSLuis R. Rodriguez int32_t qOddMeasOffset, qEvenMeasOffset, qDcMismatch, iDcMismatch;
328795f5e2cSLuis R. Rodriguez const struct ath9k_percal_data *calData =
329795f5e2cSLuis R. Rodriguez ah->cal_list_curr->calData;
330795f5e2cSLuis R. Rodriguez u32 numSamples =
331795f5e2cSLuis R. Rodriguez (1 << (calData->calCountMax + 5)) * calData->calNumSamples;
332795f5e2cSLuis R. Rodriguez
333795f5e2cSLuis R. Rodriguez for (i = 0; i < numChains; i++) {
334795f5e2cSLuis R. Rodriguez iOddMeasOffset = ah->totalAdcDcOffsetIOddPhase[i];
335795f5e2cSLuis R. Rodriguez iEvenMeasOffset = ah->totalAdcDcOffsetIEvenPhase[i];
336795f5e2cSLuis R. Rodriguez qOddMeasOffset = ah->totalAdcDcOffsetQOddPhase[i];
337795f5e2cSLuis R. Rodriguez qEvenMeasOffset = ah->totalAdcDcOffsetQEvenPhase[i];
338795f5e2cSLuis R. Rodriguez
339d2182b69SJoe Perches ath_dbg(common, CALIBRATE,
340795f5e2cSLuis R. Rodriguez "Starting ADC DC Offset Cal for Chain %d\n", i);
341795f5e2cSLuis R. Rodriguez
342d2182b69SJoe Perches ath_dbg(common, CALIBRATE, "Chn %d pwr_meas_odd_i = %d\n",
343d2182b69SJoe Perches i, iOddMeasOffset);
344d2182b69SJoe Perches ath_dbg(common, CALIBRATE, "Chn %d pwr_meas_even_i = %d\n",
345d2182b69SJoe Perches i, iEvenMeasOffset);
346d2182b69SJoe Perches ath_dbg(common, CALIBRATE, "Chn %d pwr_meas_odd_q = %d\n",
347d2182b69SJoe Perches i, qOddMeasOffset);
348d2182b69SJoe Perches ath_dbg(common, CALIBRATE, "Chn %d pwr_meas_even_q = %d\n",
349d2182b69SJoe Perches i, qEvenMeasOffset);
350795f5e2cSLuis R. Rodriguez
351795f5e2cSLuis R. Rodriguez iDcMismatch = (((iEvenMeasOffset - iOddMeasOffset) * 2) /
352795f5e2cSLuis R. Rodriguez numSamples) & 0x1ff;
353795f5e2cSLuis R. Rodriguez qDcMismatch = (((qOddMeasOffset - qEvenMeasOffset) * 2) /
354795f5e2cSLuis R. Rodriguez numSamples) & 0x1ff;
355795f5e2cSLuis R. Rodriguez
356d2182b69SJoe Perches ath_dbg(common, CALIBRATE,
357d2182b69SJoe Perches "Chn %d dc_offset_mismatch_i = 0x%08x\n",
358d2182b69SJoe Perches i, iDcMismatch);
359d2182b69SJoe Perches ath_dbg(common, CALIBRATE,
360d2182b69SJoe Perches "Chn %d dc_offset_mismatch_q = 0x%08x\n",
361d2182b69SJoe Perches i, qDcMismatch);
362795f5e2cSLuis R. Rodriguez
363795f5e2cSLuis R. Rodriguez val = REG_READ(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(i));
364795f5e2cSLuis R. Rodriguez val &= 0xc0000fff;
365795f5e2cSLuis R. Rodriguez val |= (qDcMismatch << 12) | (iDcMismatch << 21);
366795f5e2cSLuis R. Rodriguez REG_WRITE(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(i), val);
367795f5e2cSLuis R. Rodriguez
368d2182b69SJoe Perches ath_dbg(common, CALIBRATE,
369795f5e2cSLuis R. Rodriguez "ADC DC Offset Cal done for Chain %d\n", i);
370795f5e2cSLuis R. Rodriguez }
371795f5e2cSLuis R. Rodriguez
372795f5e2cSLuis R. Rodriguez REG_WRITE(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(0),
373795f5e2cSLuis R. Rodriguez REG_READ(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(0)) |
374795f5e2cSLuis R. Rodriguez AR_PHY_NEW_ADC_DC_OFFSET_CORR_ENABLE);
375795f5e2cSLuis R. Rodriguez }
376795f5e2cSLuis R. Rodriguez
ar9287_hw_olc_temp_compensation(struct ath_hw * ah)377795f5e2cSLuis R. Rodriguez static void ar9287_hw_olc_temp_compensation(struct ath_hw *ah)
378795f5e2cSLuis R. Rodriguez {
379795f5e2cSLuis R. Rodriguez u32 rddata;
380795f5e2cSLuis R. Rodriguez int32_t delta, currPDADC, slope;
381795f5e2cSLuis R. Rodriguez
382795f5e2cSLuis R. Rodriguez rddata = REG_READ(ah, AR_PHY_TX_PWRCTRL4);
383795f5e2cSLuis R. Rodriguez currPDADC = MS(rddata, AR_PHY_TX_PWRCTRL_PD_AVG_OUT);
384795f5e2cSLuis R. Rodriguez
385795f5e2cSLuis R. Rodriguez if (ah->initPDADC == 0 || currPDADC == 0) {
386795f5e2cSLuis R. Rodriguez /*
387795f5e2cSLuis R. Rodriguez * Zero value indicates that no frames have been transmitted
388795f5e2cSLuis R. Rodriguez * yet, can't do temperature compensation until frames are
389795f5e2cSLuis R. Rodriguez * transmitted.
390795f5e2cSLuis R. Rodriguez */
391795f5e2cSLuis R. Rodriguez return;
392795f5e2cSLuis R. Rodriguez } else {
393795f5e2cSLuis R. Rodriguez slope = ah->eep_ops->get_eeprom(ah, EEP_TEMPSENSE_SLOPE);
394795f5e2cSLuis R. Rodriguez
395795f5e2cSLuis R. Rodriguez if (slope == 0) { /* to avoid divide by zero case */
396795f5e2cSLuis R. Rodriguez delta = 0;
397795f5e2cSLuis R. Rodriguez } else {
398795f5e2cSLuis R. Rodriguez delta = ((currPDADC - ah->initPDADC)*4) / slope;
399795f5e2cSLuis R. Rodriguez }
400795f5e2cSLuis R. Rodriguez REG_RMW_FIELD(ah, AR_PHY_CH0_TX_PWRCTRL11,
401795f5e2cSLuis R. Rodriguez AR_PHY_TX_PWRCTRL_OLPC_TEMP_COMP, delta);
402795f5e2cSLuis R. Rodriguez REG_RMW_FIELD(ah, AR_PHY_CH1_TX_PWRCTRL11,
403795f5e2cSLuis R. Rodriguez AR_PHY_TX_PWRCTRL_OLPC_TEMP_COMP, delta);
404795f5e2cSLuis R. Rodriguez }
405795f5e2cSLuis R. Rodriguez }
406795f5e2cSLuis R. Rodriguez
ar9280_hw_olc_temp_compensation(struct ath_hw * ah)407795f5e2cSLuis R. Rodriguez static void ar9280_hw_olc_temp_compensation(struct ath_hw *ah)
408795f5e2cSLuis R. Rodriguez {
409795f5e2cSLuis R. Rodriguez u32 rddata, i;
410795f5e2cSLuis R. Rodriguez int delta, currPDADC, regval;
411795f5e2cSLuis R. Rodriguez
412795f5e2cSLuis R. Rodriguez rddata = REG_READ(ah, AR_PHY_TX_PWRCTRL4);
413795f5e2cSLuis R. Rodriguez currPDADC = MS(rddata, AR_PHY_TX_PWRCTRL_PD_AVG_OUT);
414795f5e2cSLuis R. Rodriguez
415795f5e2cSLuis R. Rodriguez if (ah->initPDADC == 0 || currPDADC == 0)
416795f5e2cSLuis R. Rodriguez return;
417795f5e2cSLuis R. Rodriguez
418795f5e2cSLuis R. Rodriguez if (ah->eep_ops->get_eeprom(ah, EEP_DAC_HPWR_5G))
419795f5e2cSLuis R. Rodriguez delta = (currPDADC - ah->initPDADC + 4) / 8;
420795f5e2cSLuis R. Rodriguez else
421795f5e2cSLuis R. Rodriguez delta = (currPDADC - ah->initPDADC + 5) / 10;
422795f5e2cSLuis R. Rodriguez
423795f5e2cSLuis R. Rodriguez if (delta != ah->PDADCdelta) {
424795f5e2cSLuis R. Rodriguez ah->PDADCdelta = delta;
425795f5e2cSLuis R. Rodriguez for (i = 1; i < AR9280_TX_GAIN_TABLE_SIZE; i++) {
426795f5e2cSLuis R. Rodriguez regval = ah->originalGain[i] - delta;
427795f5e2cSLuis R. Rodriguez if (regval < 0)
428795f5e2cSLuis R. Rodriguez regval = 0;
429795f5e2cSLuis R. Rodriguez
430795f5e2cSLuis R. Rodriguez REG_RMW_FIELD(ah,
431795f5e2cSLuis R. Rodriguez AR_PHY_TX_GAIN_TBL1 + i * 4,
432795f5e2cSLuis R. Rodriguez AR_PHY_TX_GAIN, regval);
433795f5e2cSLuis R. Rodriguez }
434795f5e2cSLuis R. Rodriguez }
435795f5e2cSLuis R. Rodriguez }
436795f5e2cSLuis R. Rodriguez
ar9271_hw_pa_cal(struct ath_hw * ah,bool is_reset)437795f5e2cSLuis R. Rodriguez static void ar9271_hw_pa_cal(struct ath_hw *ah, bool is_reset)
438795f5e2cSLuis R. Rodriguez {
439795f5e2cSLuis R. Rodriguez u32 regVal;
440795f5e2cSLuis R. Rodriguez unsigned int i;
441795f5e2cSLuis R. Rodriguez u32 regList[][2] = {
4428d1cbdefSOleksij Rempel { AR9285_AN_TOP3, 0 },
4438d1cbdefSOleksij Rempel { AR9285_AN_RXTXBB1, 0 },
4448d1cbdefSOleksij Rempel { AR9285_AN_RF2G1, 0 },
4458d1cbdefSOleksij Rempel { AR9285_AN_RF2G2, 0 },
4468d1cbdefSOleksij Rempel { AR9285_AN_TOP2, 0 },
4478d1cbdefSOleksij Rempel { AR9285_AN_RF2G8, 0 },
4488d1cbdefSOleksij Rempel { AR9285_AN_RF2G7, 0 },
4498d1cbdefSOleksij Rempel { AR9285_AN_RF2G3, 0 },
450795f5e2cSLuis R. Rodriguez };
451795f5e2cSLuis R. Rodriguez
45203822782SOleksij Rempel REG_READ_ARRAY(ah, regList, ARRAY_SIZE(regList));
453795f5e2cSLuis R. Rodriguez
45493442c85SOleksij Rempel ENABLE_REG_RMW_BUFFER(ah);
4550202a553SOleksij Rempel /* 7834, b1=0 */
4560202a553SOleksij Rempel REG_CLR_BIT(ah, AR9285_AN_RF2G6, 1 << 0);
4570202a553SOleksij Rempel /* 9808, b27=1 */
4580202a553SOleksij Rempel REG_SET_BIT(ah, 0x9808, 1 << 27);
459795f5e2cSLuis R. Rodriguez /* 786c,b23,1, pwddac=1 */
4600202a553SOleksij Rempel REG_SET_BIT(ah, AR9285_AN_TOP3, AR9285_AN_TOP3_PWDDAC);
461795f5e2cSLuis R. Rodriguez /* 7854, b5,1, pdrxtxbb=1 */
4620202a553SOleksij Rempel REG_SET_BIT(ah, AR9285_AN_RXTXBB1, AR9285_AN_RXTXBB1_PDRXTXBB1);
463795f5e2cSLuis R. Rodriguez /* 7854, b7,1, pdv2i=1 */
4640202a553SOleksij Rempel REG_SET_BIT(ah, AR9285_AN_RXTXBB1, AR9285_AN_RXTXBB1_PDV2I);
465795f5e2cSLuis R. Rodriguez /* 7854, b8,1, pddacinterface=1 */
4660202a553SOleksij Rempel REG_SET_BIT(ah, AR9285_AN_RXTXBB1, AR9285_AN_RXTXBB1_PDDACIF);
467795f5e2cSLuis R. Rodriguez /* 7824,b12,0, offcal=0 */
4680202a553SOleksij Rempel REG_CLR_BIT(ah, AR9285_AN_RF2G2, AR9285_AN_RF2G2_OFFCAL);
469795f5e2cSLuis R. Rodriguez /* 7838, b1,0, pwddb=0 */
4700202a553SOleksij Rempel REG_CLR_BIT(ah, AR9285_AN_RF2G7, AR9285_AN_RF2G7_PWDDB);
471795f5e2cSLuis R. Rodriguez /* 7820,b11,0, enpacal=0 */
4720202a553SOleksij Rempel REG_CLR_BIT(ah, AR9285_AN_RF2G1, AR9285_AN_RF2G1_ENPACAL);
473795f5e2cSLuis R. Rodriguez /* 7820,b25,1, pdpadrv1=0 */
4740202a553SOleksij Rempel REG_CLR_BIT(ah, AR9285_AN_RF2G1, AR9285_AN_RF2G1_PDPADRV1);
475795f5e2cSLuis R. Rodriguez /* 7820,b24,0, pdpadrv2=0 */
4760202a553SOleksij Rempel REG_CLR_BIT(ah, AR9285_AN_RF2G1, AR9285_AN_RF2G1_PDPADRV2);
477795f5e2cSLuis R. Rodriguez /* 7820,b23,0, pdpaout=0 */
4780202a553SOleksij Rempel REG_CLR_BIT(ah, AR9285_AN_RF2G1, AR9285_AN_RF2G1_PDPAOUT);
479795f5e2cSLuis R. Rodriguez /* 783c,b14-16,7, padrvgn2tab_0=7 */
480795f5e2cSLuis R. Rodriguez REG_RMW_FIELD(ah, AR9285_AN_RF2G8, AR9285_AN_RF2G8_PADRVGN2TAB0, 7);
481795f5e2cSLuis R. Rodriguez /*
482795f5e2cSLuis R. Rodriguez * 7838,b29-31,0, padrvgn1tab_0=0
483795f5e2cSLuis R. Rodriguez * does not matter since we turn it off
484795f5e2cSLuis R. Rodriguez */
485795f5e2cSLuis R. Rodriguez REG_RMW_FIELD(ah, AR9285_AN_RF2G7, AR9285_AN_RF2G7_PADRVGN2TAB0, 0);
4868d1cbdefSOleksij Rempel /* 7828, b0-11, ccom=fff */
487795f5e2cSLuis R. Rodriguez REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9271_AN_RF2G3_CCOMP, 0xfff);
48893442c85SOleksij Rempel REG_RMW_BUFFER_FLUSH(ah);
489795f5e2cSLuis R. Rodriguez
490795f5e2cSLuis R. Rodriguez /* Set:
491795f5e2cSLuis R. Rodriguez * localmode=1,bmode=1,bmoderxtx=1,synthon=1,
492795f5e2cSLuis R. Rodriguez * txon=1,paon=1,oscon=1,synthon_force=1
493795f5e2cSLuis R. Rodriguez */
494795f5e2cSLuis R. Rodriguez REG_WRITE(ah, AR9285_AN_TOP2, 0xca0358a0);
495795f5e2cSLuis R. Rodriguez udelay(30);
496795f5e2cSLuis R. Rodriguez REG_RMW_FIELD(ah, AR9285_AN_RF2G6, AR9271_AN_RF2G6_OFFS, 0);
497795f5e2cSLuis R. Rodriguez
498795f5e2cSLuis R. Rodriguez /* find off_6_1; */
499795f5e2cSLuis R. Rodriguez for (i = 6; i > 0; i--) {
5008d1cbdefSOleksij Rempel regVal = REG_READ(ah, AR9285_AN_RF2G6);
501795f5e2cSLuis R. Rodriguez regVal |= (1 << (20 + i));
5028d1cbdefSOleksij Rempel REG_WRITE(ah, AR9285_AN_RF2G6, regVal);
503795f5e2cSLuis R. Rodriguez udelay(1);
504795f5e2cSLuis R. Rodriguez /* regVal = REG_READ(ah, 0x7834); */
505795f5e2cSLuis R. Rodriguez regVal &= (~(0x1 << (20 + i)));
5068d1cbdefSOleksij Rempel regVal |= (MS(REG_READ(ah, AR9285_AN_RF2G9),
5078d1cbdefSOleksij Rempel AR9285_AN_RXTXBB1_SPARE9)
508795f5e2cSLuis R. Rodriguez << (20 + i));
5098d1cbdefSOleksij Rempel REG_WRITE(ah, AR9285_AN_RF2G6, regVal);
510795f5e2cSLuis R. Rodriguez }
511795f5e2cSLuis R. Rodriguez
512795f5e2cSLuis R. Rodriguez regVal = (regVal >> 20) & 0x7f;
513795f5e2cSLuis R. Rodriguez
514795f5e2cSLuis R. Rodriguez /* Update PA cal info */
515795f5e2cSLuis R. Rodriguez if ((!is_reset) && (ah->pacal_info.prev_offset == regVal)) {
516795f5e2cSLuis R. Rodriguez if (ah->pacal_info.max_skipcount < MAX_PACAL_SKIPCOUNT)
517795f5e2cSLuis R. Rodriguez ah->pacal_info.max_skipcount =
518795f5e2cSLuis R. Rodriguez 2 * ah->pacal_info.max_skipcount;
519795f5e2cSLuis R. Rodriguez ah->pacal_info.skipcount = ah->pacal_info.max_skipcount;
520795f5e2cSLuis R. Rodriguez } else {
521795f5e2cSLuis R. Rodriguez ah->pacal_info.max_skipcount = 1;
522795f5e2cSLuis R. Rodriguez ah->pacal_info.skipcount = 0;
523795f5e2cSLuis R. Rodriguez ah->pacal_info.prev_offset = regVal;
524795f5e2cSLuis R. Rodriguez }
525795f5e2cSLuis R. Rodriguez
5260202a553SOleksij Rempel
52793442c85SOleksij Rempel ENABLE_REG_RMW_BUFFER(ah);
5280202a553SOleksij Rempel /* 7834, b1=1 */
5290202a553SOleksij Rempel REG_SET_BIT(ah, AR9285_AN_RF2G6, 1 << 0);
5300202a553SOleksij Rempel /* 9808, b27=0 */
5310202a553SOleksij Rempel REG_CLR_BIT(ah, 0x9808, 1 << 27);
53293442c85SOleksij Rempel REG_RMW_BUFFER_FLUSH(ah);
5330202a553SOleksij Rempel
5347d0d0df0SSujith ENABLE_REGWRITE_BUFFER(ah);
535795f5e2cSLuis R. Rodriguez for (i = 0; i < ARRAY_SIZE(regList); i++)
536795f5e2cSLuis R. Rodriguez REG_WRITE(ah, regList[i][0], regList[i][1]);
5377d0d0df0SSujith
5387d0d0df0SSujith REGWRITE_BUFFER_FLUSH(ah);
539795f5e2cSLuis R. Rodriguez }
540795f5e2cSLuis R. Rodriguez
ar9285_hw_pa_cal(struct ath_hw * ah,bool is_reset)541795f5e2cSLuis R. Rodriguez static inline void ar9285_hw_pa_cal(struct ath_hw *ah, bool is_reset)
542795f5e2cSLuis R. Rodriguez {
543795f5e2cSLuis R. Rodriguez struct ath_common *common = ath9k_hw_common(ah);
544795f5e2cSLuis R. Rodriguez u32 regVal;
545795f5e2cSLuis R. Rodriguez int i, offset, offs_6_1, offs_0;
546795f5e2cSLuis R. Rodriguez u32 ccomp_org, reg_field;
547795f5e2cSLuis R. Rodriguez u32 regList[][2] = {
548795f5e2cSLuis R. Rodriguez { 0x786c, 0 },
549795f5e2cSLuis R. Rodriguez { 0x7854, 0 },
550795f5e2cSLuis R. Rodriguez { 0x7820, 0 },
551795f5e2cSLuis R. Rodriguez { 0x7824, 0 },
552795f5e2cSLuis R. Rodriguez { 0x7868, 0 },
553795f5e2cSLuis R. Rodriguez { 0x783c, 0 },
554795f5e2cSLuis R. Rodriguez { 0x7838, 0 },
555795f5e2cSLuis R. Rodriguez };
556795f5e2cSLuis R. Rodriguez
557d2182b69SJoe Perches ath_dbg(common, CALIBRATE, "Running PA Calibration\n");
558795f5e2cSLuis R. Rodriguez
559795f5e2cSLuis R. Rodriguez /* PA CAL is not needed for high power solution */
560795f5e2cSLuis R. Rodriguez if (ah->eep_ops->get_eeprom(ah, EEP_TXGAIN_TYPE) ==
561795f5e2cSLuis R. Rodriguez AR5416_EEP_TXGAIN_HIGH_POWER)
562795f5e2cSLuis R. Rodriguez return;
563795f5e2cSLuis R. Rodriguez
564795f5e2cSLuis R. Rodriguez for (i = 0; i < ARRAY_SIZE(regList); i++)
565795f5e2cSLuis R. Rodriguez regList[i][1] = REG_READ(ah, regList[i][0]);
566795f5e2cSLuis R. Rodriguez
567795f5e2cSLuis R. Rodriguez regVal = REG_READ(ah, 0x7834);
568795f5e2cSLuis R. Rodriguez regVal &= (~(0x1));
569795f5e2cSLuis R. Rodriguez REG_WRITE(ah, 0x7834, regVal);
570795f5e2cSLuis R. Rodriguez regVal = REG_READ(ah, 0x9808);
571795f5e2cSLuis R. Rodriguez regVal |= (0x1 << 27);
572795f5e2cSLuis R. Rodriguez REG_WRITE(ah, 0x9808, regVal);
573795f5e2cSLuis R. Rodriguez
574795f5e2cSLuis R. Rodriguez REG_RMW_FIELD(ah, AR9285_AN_TOP3, AR9285_AN_TOP3_PWDDAC, 1);
575795f5e2cSLuis R. Rodriguez REG_RMW_FIELD(ah, AR9285_AN_RXTXBB1, AR9285_AN_RXTXBB1_PDRXTXBB1, 1);
576795f5e2cSLuis R. Rodriguez REG_RMW_FIELD(ah, AR9285_AN_RXTXBB1, AR9285_AN_RXTXBB1_PDV2I, 1);
577795f5e2cSLuis R. Rodriguez REG_RMW_FIELD(ah, AR9285_AN_RXTXBB1, AR9285_AN_RXTXBB1_PDDACIF, 1);
578795f5e2cSLuis R. Rodriguez REG_RMW_FIELD(ah, AR9285_AN_RF2G2, AR9285_AN_RF2G2_OFFCAL, 0);
579795f5e2cSLuis R. Rodriguez REG_RMW_FIELD(ah, AR9285_AN_RF2G7, AR9285_AN_RF2G7_PWDDB, 0);
580795f5e2cSLuis R. Rodriguez REG_RMW_FIELD(ah, AR9285_AN_RF2G1, AR9285_AN_RF2G1_ENPACAL, 0);
581795f5e2cSLuis R. Rodriguez REG_RMW_FIELD(ah, AR9285_AN_RF2G1, AR9285_AN_RF2G1_PDPADRV1, 0);
582795f5e2cSLuis R. Rodriguez REG_RMW_FIELD(ah, AR9285_AN_RF2G1, AR9285_AN_RF2G1_PDPADRV2, 0);
583795f5e2cSLuis R. Rodriguez REG_RMW_FIELD(ah, AR9285_AN_RF2G1, AR9285_AN_RF2G1_PDPAOUT, 0);
584795f5e2cSLuis R. Rodriguez REG_RMW_FIELD(ah, AR9285_AN_RF2G8, AR9285_AN_RF2G8_PADRVGN2TAB0, 7);
585795f5e2cSLuis R. Rodriguez REG_RMW_FIELD(ah, AR9285_AN_RF2G7, AR9285_AN_RF2G7_PADRVGN2TAB0, 0);
586795f5e2cSLuis R. Rodriguez ccomp_org = MS(REG_READ(ah, AR9285_AN_RF2G6), AR9285_AN_RF2G6_CCOMP);
587795f5e2cSLuis R. Rodriguez REG_RMW_FIELD(ah, AR9285_AN_RF2G6, AR9285_AN_RF2G6_CCOMP, 0xf);
588795f5e2cSLuis R. Rodriguez
589795f5e2cSLuis R. Rodriguez REG_WRITE(ah, AR9285_AN_TOP2, 0xca0358a0);
590795f5e2cSLuis R. Rodriguez udelay(30);
591795f5e2cSLuis R. Rodriguez REG_RMW_FIELD(ah, AR9285_AN_RF2G6, AR9285_AN_RF2G6_OFFS, 0);
592795f5e2cSLuis R. Rodriguez REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_PDVCCOMP, 0);
593795f5e2cSLuis R. Rodriguez
594795f5e2cSLuis R. Rodriguez for (i = 6; i > 0; i--) {
595795f5e2cSLuis R. Rodriguez regVal = REG_READ(ah, 0x7834);
596795f5e2cSLuis R. Rodriguez regVal |= (1 << (19 + i));
597795f5e2cSLuis R. Rodriguez REG_WRITE(ah, 0x7834, regVal);
598795f5e2cSLuis R. Rodriguez udelay(1);
599795f5e2cSLuis R. Rodriguez regVal = REG_READ(ah, 0x7834);
600795f5e2cSLuis R. Rodriguez regVal &= (~(0x1 << (19 + i)));
601795f5e2cSLuis R. Rodriguez reg_field = MS(REG_READ(ah, 0x7840), AR9285_AN_RXTXBB1_SPARE9);
602795f5e2cSLuis R. Rodriguez regVal |= (reg_field << (19 + i));
603795f5e2cSLuis R. Rodriguez REG_WRITE(ah, 0x7834, regVal);
604795f5e2cSLuis R. Rodriguez }
605795f5e2cSLuis R. Rodriguez
606795f5e2cSLuis R. Rodriguez REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_PDVCCOMP, 1);
607795f5e2cSLuis R. Rodriguez udelay(1);
608795f5e2cSLuis R. Rodriguez reg_field = MS(REG_READ(ah, AR9285_AN_RF2G9), AR9285_AN_RXTXBB1_SPARE9);
609795f5e2cSLuis R. Rodriguez REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_PDVCCOMP, reg_field);
610795f5e2cSLuis R. Rodriguez offs_6_1 = MS(REG_READ(ah, AR9285_AN_RF2G6), AR9285_AN_RF2G6_OFFS);
611795f5e2cSLuis R. Rodriguez offs_0 = MS(REG_READ(ah, AR9285_AN_RF2G3), AR9285_AN_RF2G3_PDVCCOMP);
612795f5e2cSLuis R. Rodriguez
613795f5e2cSLuis R. Rodriguez offset = (offs_6_1<<1) | offs_0;
614795f5e2cSLuis R. Rodriguez offset = offset - 0;
615795f5e2cSLuis R. Rodriguez offs_6_1 = offset>>1;
616795f5e2cSLuis R. Rodriguez offs_0 = offset & 1;
617795f5e2cSLuis R. Rodriguez
618795f5e2cSLuis R. Rodriguez if ((!is_reset) && (ah->pacal_info.prev_offset == offset)) {
619795f5e2cSLuis R. Rodriguez if (ah->pacal_info.max_skipcount < MAX_PACAL_SKIPCOUNT)
620795f5e2cSLuis R. Rodriguez ah->pacal_info.max_skipcount =
621795f5e2cSLuis R. Rodriguez 2 * ah->pacal_info.max_skipcount;
622795f5e2cSLuis R. Rodriguez ah->pacal_info.skipcount = ah->pacal_info.max_skipcount;
623795f5e2cSLuis R. Rodriguez } else {
624795f5e2cSLuis R. Rodriguez ah->pacal_info.max_skipcount = 1;
625795f5e2cSLuis R. Rodriguez ah->pacal_info.skipcount = 0;
626795f5e2cSLuis R. Rodriguez ah->pacal_info.prev_offset = offset;
627795f5e2cSLuis R. Rodriguez }
628795f5e2cSLuis R. Rodriguez
629795f5e2cSLuis R. Rodriguez REG_RMW_FIELD(ah, AR9285_AN_RF2G6, AR9285_AN_RF2G6_OFFS, offs_6_1);
630795f5e2cSLuis R. Rodriguez REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_PDVCCOMP, offs_0);
631795f5e2cSLuis R. Rodriguez
632795f5e2cSLuis R. Rodriguez regVal = REG_READ(ah, 0x7834);
633795f5e2cSLuis R. Rodriguez regVal |= 0x1;
634795f5e2cSLuis R. Rodriguez REG_WRITE(ah, 0x7834, regVal);
635795f5e2cSLuis R. Rodriguez regVal = REG_READ(ah, 0x9808);
636795f5e2cSLuis R. Rodriguez regVal &= (~(0x1 << 27));
637795f5e2cSLuis R. Rodriguez REG_WRITE(ah, 0x9808, regVal);
638795f5e2cSLuis R. Rodriguez
639795f5e2cSLuis R. Rodriguez for (i = 0; i < ARRAY_SIZE(regList); i++)
640795f5e2cSLuis R. Rodriguez REG_WRITE(ah, regList[i][0], regList[i][1]);
641795f5e2cSLuis R. Rodriguez
642795f5e2cSLuis R. Rodriguez REG_RMW_FIELD(ah, AR9285_AN_RF2G6, AR9285_AN_RF2G6_CCOMP, ccomp_org);
643795f5e2cSLuis R. Rodriguez }
644795f5e2cSLuis R. Rodriguez
ar9002_hw_pa_cal(struct ath_hw * ah,bool is_reset)645795f5e2cSLuis R. Rodriguez static void ar9002_hw_pa_cal(struct ath_hw *ah, bool is_reset)
646795f5e2cSLuis R. Rodriguez {
647795f5e2cSLuis R. Rodriguez if (AR_SREV_9271(ah)) {
648795f5e2cSLuis R. Rodriguez if (is_reset || !ah->pacal_info.skipcount)
649795f5e2cSLuis R. Rodriguez ar9271_hw_pa_cal(ah, is_reset);
650795f5e2cSLuis R. Rodriguez else
651795f5e2cSLuis R. Rodriguez ah->pacal_info.skipcount--;
652e17f83eaSFelix Fietkau } else if (AR_SREV_9285_12_OR_LATER(ah)) {
653795f5e2cSLuis R. Rodriguez if (is_reset || !ah->pacal_info.skipcount)
654795f5e2cSLuis R. Rodriguez ar9285_hw_pa_cal(ah, is_reset);
655795f5e2cSLuis R. Rodriguez else
656795f5e2cSLuis R. Rodriguez ah->pacal_info.skipcount--;
657795f5e2cSLuis R. Rodriguez }
658795f5e2cSLuis R. Rodriguez }
659795f5e2cSLuis R. Rodriguez
ar9002_hw_olc_temp_compensation(struct ath_hw * ah)660795f5e2cSLuis R. Rodriguez static void ar9002_hw_olc_temp_compensation(struct ath_hw *ah)
661795f5e2cSLuis R. Rodriguez {
662*b3a663f0SWenli Looi if (OLC_FOR_AR9287_10_LATER(ah))
663795f5e2cSLuis R. Rodriguez ar9287_hw_olc_temp_compensation(ah);
664*b3a663f0SWenli Looi else if (OLC_FOR_AR9280_20_LATER(ah))
665795f5e2cSLuis R. Rodriguez ar9280_hw_olc_temp_compensation(ah);
666795f5e2cSLuis R. Rodriguez }
667795f5e2cSLuis R. Rodriguez
ar9002_hw_calibrate(struct ath_hw * ah,struct ath9k_channel * chan,u8 rxchainmask,bool longcal)6687b8aaeadSFelix Fietkau static int ar9002_hw_calibrate(struct ath_hw *ah, struct ath9k_channel *chan,
6697b8aaeadSFelix Fietkau u8 rxchainmask, bool longcal)
670795f5e2cSLuis R. Rodriguez {
671795f5e2cSLuis R. Rodriguez struct ath9k_cal_list *currCal = ah->cal_list_curr;
6726339244bSFelix Fietkau bool nfcal, nfcal_pending = false, percal_pending;
6737b8aaeadSFelix Fietkau int ret;
674795f5e2cSLuis R. Rodriguez
675*b3a663f0SWenli Looi nfcal = !!(REG_READ(ah, AR_PHY_AGC_CONTROL(ah)) & AR_PHY_AGC_CONTROL_NF);
67641ba50fdSSergey Ryazanov if (ah->caldata) {
6774b9b42bfSSujith Manoharan nfcal_pending = test_bit(NFCAL_PENDING, &ah->caldata->cal_flags);
67841ba50fdSSergey Ryazanov if (longcal) /* Remember to not miss */
67941ba50fdSSergey Ryazanov set_bit(LONGCAL_PENDING, &ah->caldata->cal_flags);
68041ba50fdSSergey Ryazanov else if (test_bit(LONGCAL_PENDING, &ah->caldata->cal_flags))
68141ba50fdSSergey Ryazanov longcal = true; /* Respin a previous one */
68241ba50fdSSergey Ryazanov }
6834254bc1cSFelix Fietkau
6846339244bSFelix Fietkau percal_pending = (currCal &&
685795f5e2cSLuis R. Rodriguez (currCal->calState == CAL_RUNNING ||
6866339244bSFelix Fietkau currCal->calState == CAL_WAITING));
687795f5e2cSLuis R. Rodriguez
6886339244bSFelix Fietkau if (percal_pending && !nfcal) {
6896339244bSFelix Fietkau if (!ar9002_hw_per_calibration(ah, chan, rxchainmask, currCal))
6906339244bSFelix Fietkau return 0;
6916339244bSFelix Fietkau
692d8d20845SSergey Ryazanov /* Looking for next waiting calibration if any */
693d8d20845SSergey Ryazanov for (currCal = currCal->calNext; currCal != ah->cal_list_curr;
694d8d20845SSergey Ryazanov currCal = currCal->calNext) {
695d8d20845SSergey Ryazanov if (currCal->calState == CAL_WAITING)
696d8d20845SSergey Ryazanov break;
697d8d20845SSergey Ryazanov }
698d8d20845SSergey Ryazanov if (currCal->calState == CAL_WAITING) {
699d8d20845SSergey Ryazanov percal_pending = true;
700d8d20845SSergey Ryazanov ah->cal_list_curr = currCal;
701d8d20845SSergey Ryazanov } else {
702d8d20845SSergey Ryazanov percal_pending = false;
703d8d20845SSergey Ryazanov ah->cal_list_curr = ah->cal_list;
704d8d20845SSergey Ryazanov }
7052bb7027bSSergey Ryazanov }
7062bb7027bSSergey Ryazanov
7072bb7027bSSergey Ryazanov /* Do not start a next calibration if the longcal is in action */
7082bb7027bSSergey Ryazanov if (percal_pending && !nfcal && !longcal) {
709795f5e2cSLuis R. Rodriguez ath9k_hw_reset_calibration(ah, currCal);
7103a69dd36SFelix Fietkau
7116339244bSFelix Fietkau return 0;
712795f5e2cSLuis R. Rodriguez }
713795f5e2cSLuis R. Rodriguez
714795f5e2cSLuis R. Rodriguez /* Do NF cal only at longer intervals */
7154254bc1cSFelix Fietkau if (longcal || nfcal_pending) {
716795f5e2cSLuis R. Rodriguez /*
717795f5e2cSLuis R. Rodriguez * Get the value from the previous NF cal and update
718795f5e2cSLuis R. Rodriguez * history buffer.
719795f5e2cSLuis R. Rodriguez */
7204254bc1cSFelix Fietkau if (ath9k_hw_getnf(ah, chan)) {
721795f5e2cSLuis R. Rodriguez /*
7224254bc1cSFelix Fietkau * Load the NF from history buffer of the current
7234254bc1cSFelix Fietkau * channel.
7244254bc1cSFelix Fietkau * NF is slow time-variant, so it is OK to use a
7254254bc1cSFelix Fietkau * historical value.
726795f5e2cSLuis R. Rodriguez */
7277b8aaeadSFelix Fietkau ret = ath9k_hw_loadnf(ah, ah->curchan);
7287b8aaeadSFelix Fietkau if (ret < 0)
7297b8aaeadSFelix Fietkau return ret;
7304254bc1cSFelix Fietkau }
731795f5e2cSLuis R. Rodriguez
732811ea256SRajkumar Manoharan if (longcal) {
73341ba50fdSSergey Ryazanov if (ah->caldata)
73441ba50fdSSergey Ryazanov clear_bit(LONGCAL_PENDING,
73541ba50fdSSergey Ryazanov &ah->caldata->cal_flags);
73600c86590SFelix Fietkau ath9k_hw_start_nfcal(ah, false);
737811ea256SRajkumar Manoharan /* Do periodic PAOffset Cal */
738811ea256SRajkumar Manoharan ar9002_hw_pa_cal(ah, false);
739811ea256SRajkumar Manoharan ar9002_hw_olc_temp_compensation(ah);
740811ea256SRajkumar Manoharan }
741795f5e2cSLuis R. Rodriguez }
742795f5e2cSLuis R. Rodriguez
7436339244bSFelix Fietkau return !percal_pending;
744795f5e2cSLuis R. Rodriguez }
745795f5e2cSLuis R. Rodriguez
746795f5e2cSLuis R. Rodriguez /* Carrier leakage Calibration fix */
ar9285_hw_cl_cal(struct ath_hw * ah,struct ath9k_channel * chan)747795f5e2cSLuis R. Rodriguez static bool ar9285_hw_cl_cal(struct ath_hw *ah, struct ath9k_channel *chan)
748795f5e2cSLuis R. Rodriguez {
749795f5e2cSLuis R. Rodriguez struct ath_common *common = ath9k_hw_common(ah);
750795f5e2cSLuis R. Rodriguez
751795f5e2cSLuis R. Rodriguez REG_SET_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_CL_CAL_ENABLE);
752795f5e2cSLuis R. Rodriguez if (IS_CHAN_HT20(chan)) {
753795f5e2cSLuis R. Rodriguez REG_SET_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_PARALLEL_CAL_ENABLE);
754795f5e2cSLuis R. Rodriguez REG_SET_BIT(ah, AR_PHY_TURBO, AR_PHY_FC_DYN2040_EN);
755*b3a663f0SWenli Looi REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL(ah),
756795f5e2cSLuis R. Rodriguez AR_PHY_AGC_CONTROL_FLTR_CAL);
757795f5e2cSLuis R. Rodriguez REG_CLR_BIT(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_CAL_ENABLE);
758*b3a663f0SWenli Looi REG_SET_BIT(ah, AR_PHY_AGC_CONTROL(ah), AR_PHY_AGC_CONTROL_CAL);
759*b3a663f0SWenli Looi if (!ath9k_hw_wait(ah, AR_PHY_AGC_CONTROL(ah),
760795f5e2cSLuis R. Rodriguez AR_PHY_AGC_CONTROL_CAL, 0, AH_WAIT_TIMEOUT)) {
761d2182b69SJoe Perches ath_dbg(common, CALIBRATE,
76205005c5fSRobert Shade "offset calibration failed to complete in %d ms; noisy environment?\n",
76305005c5fSRobert Shade AH_WAIT_TIMEOUT / 1000);
764795f5e2cSLuis R. Rodriguez return false;
765795f5e2cSLuis R. Rodriguez }
766795f5e2cSLuis R. Rodriguez REG_CLR_BIT(ah, AR_PHY_TURBO, AR_PHY_FC_DYN2040_EN);
767795f5e2cSLuis R. Rodriguez REG_CLR_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_PARALLEL_CAL_ENABLE);
768795f5e2cSLuis R. Rodriguez REG_CLR_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_CL_CAL_ENABLE);
769795f5e2cSLuis R. Rodriguez }
770795f5e2cSLuis R. Rodriguez REG_CLR_BIT(ah, AR_PHY_ADC_CTL, AR_PHY_ADC_CTL_OFF_PWDADC);
771*b3a663f0SWenli Looi REG_SET_BIT(ah, AR_PHY_AGC_CONTROL(ah), AR_PHY_AGC_CONTROL_FLTR_CAL);
772795f5e2cSLuis R. Rodriguez REG_SET_BIT(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_CAL_ENABLE);
773*b3a663f0SWenli Looi REG_SET_BIT(ah, AR_PHY_AGC_CONTROL(ah), AR_PHY_AGC_CONTROL_CAL);
774*b3a663f0SWenli Looi if (!ath9k_hw_wait(ah, AR_PHY_AGC_CONTROL(ah), AR_PHY_AGC_CONTROL_CAL,
775795f5e2cSLuis R. Rodriguez 0, AH_WAIT_TIMEOUT)) {
776d2182b69SJoe Perches ath_dbg(common, CALIBRATE,
77705005c5fSRobert Shade "offset calibration failed to complete in %d ms; noisy environment?\n",
77805005c5fSRobert Shade AH_WAIT_TIMEOUT / 1000);
779795f5e2cSLuis R. Rodriguez return false;
780795f5e2cSLuis R. Rodriguez }
781795f5e2cSLuis R. Rodriguez
782795f5e2cSLuis R. Rodriguez REG_SET_BIT(ah, AR_PHY_ADC_CTL, AR_PHY_ADC_CTL_OFF_PWDADC);
783795f5e2cSLuis R. Rodriguez REG_CLR_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_CL_CAL_ENABLE);
784*b3a663f0SWenli Looi REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL(ah), AR_PHY_AGC_CONTROL_FLTR_CAL);
785795f5e2cSLuis R. Rodriguez
786795f5e2cSLuis R. Rodriguez return true;
787795f5e2cSLuis R. Rodriguez }
788795f5e2cSLuis R. Rodriguez
ar9285_hw_clc(struct ath_hw * ah,struct ath9k_channel * chan)789795f5e2cSLuis R. Rodriguez static bool ar9285_hw_clc(struct ath_hw *ah, struct ath9k_channel *chan)
790795f5e2cSLuis R. Rodriguez {
791795f5e2cSLuis R. Rodriguez int i;
792795f5e2cSLuis R. Rodriguez u_int32_t txgain_max;
793795f5e2cSLuis R. Rodriguez u_int32_t clc_gain, gain_mask = 0, clc_num = 0;
794795f5e2cSLuis R. Rodriguez u_int32_t reg_clc_I0, reg_clc_Q0;
795795f5e2cSLuis R. Rodriguez u_int32_t i0_num = 0;
796795f5e2cSLuis R. Rodriguez u_int32_t q0_num = 0;
797795f5e2cSLuis R. Rodriguez u_int32_t total_num = 0;
798795f5e2cSLuis R. Rodriguez u_int32_t reg_rf2g5_org;
799795f5e2cSLuis R. Rodriguez bool retv = true;
800795f5e2cSLuis R. Rodriguez
801795f5e2cSLuis R. Rodriguez if (!(ar9285_hw_cl_cal(ah, chan)))
802795f5e2cSLuis R. Rodriguez return false;
803795f5e2cSLuis R. Rodriguez
804795f5e2cSLuis R. Rodriguez txgain_max = MS(REG_READ(ah, AR_PHY_TX_PWRCTRL7),
805795f5e2cSLuis R. Rodriguez AR_PHY_TX_PWRCTRL_TX_GAIN_TAB_MAX);
806795f5e2cSLuis R. Rodriguez
807795f5e2cSLuis R. Rodriguez for (i = 0; i < (txgain_max+1); i++) {
808795f5e2cSLuis R. Rodriguez clc_gain = (REG_READ(ah, (AR_PHY_TX_GAIN_TBL1+(i<<2))) &
809795f5e2cSLuis R. Rodriguez AR_PHY_TX_GAIN_CLC) >> AR_PHY_TX_GAIN_CLC_S;
810795f5e2cSLuis R. Rodriguez if (!(gain_mask & (1 << clc_gain))) {
811795f5e2cSLuis R. Rodriguez gain_mask |= (1 << clc_gain);
812795f5e2cSLuis R. Rodriguez clc_num++;
813795f5e2cSLuis R. Rodriguez }
814795f5e2cSLuis R. Rodriguez }
815795f5e2cSLuis R. Rodriguez
816795f5e2cSLuis R. Rodriguez for (i = 0; i < clc_num; i++) {
817795f5e2cSLuis R. Rodriguez reg_clc_I0 = (REG_READ(ah, (AR_PHY_CLC_TBL1 + (i << 2)))
818795f5e2cSLuis R. Rodriguez & AR_PHY_CLC_I0) >> AR_PHY_CLC_I0_S;
819795f5e2cSLuis R. Rodriguez reg_clc_Q0 = (REG_READ(ah, (AR_PHY_CLC_TBL1 + (i << 2)))
820795f5e2cSLuis R. Rodriguez & AR_PHY_CLC_Q0) >> AR_PHY_CLC_Q0_S;
821795f5e2cSLuis R. Rodriguez if (reg_clc_I0 == 0)
822795f5e2cSLuis R. Rodriguez i0_num++;
823795f5e2cSLuis R. Rodriguez
824795f5e2cSLuis R. Rodriguez if (reg_clc_Q0 == 0)
825795f5e2cSLuis R. Rodriguez q0_num++;
826795f5e2cSLuis R. Rodriguez }
827795f5e2cSLuis R. Rodriguez total_num = i0_num + q0_num;
828795f5e2cSLuis R. Rodriguez if (total_num > AR9285_CLCAL_REDO_THRESH) {
829795f5e2cSLuis R. Rodriguez reg_rf2g5_org = REG_READ(ah, AR9285_RF2G5);
830795f5e2cSLuis R. Rodriguez if (AR_SREV_9285E_20(ah)) {
831795f5e2cSLuis R. Rodriguez REG_WRITE(ah, AR9285_RF2G5,
832795f5e2cSLuis R. Rodriguez (reg_rf2g5_org & AR9285_RF2G5_IC50TX) |
833795f5e2cSLuis R. Rodriguez AR9285_RF2G5_IC50TX_XE_SET);
834795f5e2cSLuis R. Rodriguez } else {
835795f5e2cSLuis R. Rodriguez REG_WRITE(ah, AR9285_RF2G5,
836795f5e2cSLuis R. Rodriguez (reg_rf2g5_org & AR9285_RF2G5_IC50TX) |
837795f5e2cSLuis R. Rodriguez AR9285_RF2G5_IC50TX_SET);
838795f5e2cSLuis R. Rodriguez }
839795f5e2cSLuis R. Rodriguez retv = ar9285_hw_cl_cal(ah, chan);
840795f5e2cSLuis R. Rodriguez REG_WRITE(ah, AR9285_RF2G5, reg_rf2g5_org);
841795f5e2cSLuis R. Rodriguez }
842795f5e2cSLuis R. Rodriguez return retv;
843795f5e2cSLuis R. Rodriguez }
844795f5e2cSLuis R. Rodriguez
ar9002_hw_init_cal(struct ath_hw * ah,struct ath9k_channel * chan)845795f5e2cSLuis R. Rodriguez static bool ar9002_hw_init_cal(struct ath_hw *ah, struct ath9k_channel *chan)
846795f5e2cSLuis R. Rodriguez {
847795f5e2cSLuis R. Rodriguez struct ath_common *common = ath9k_hw_common(ah);
848795f5e2cSLuis R. Rodriguez
849a4d6e17dSRajkumar Manoharan if (AR_SREV_9271(ah)) {
850a4d6e17dSRajkumar Manoharan if (!ar9285_hw_cl_cal(ah, chan))
851a4d6e17dSRajkumar Manoharan return false;
8521df85eceSAdrian Chadd } else if (AR_SREV_9285(ah) && AR_SREV_9285_12_OR_LATER(ah)) {
853795f5e2cSLuis R. Rodriguez if (!ar9285_hw_clc(ah, chan))
854795f5e2cSLuis R. Rodriguez return false;
855795f5e2cSLuis R. Rodriguez } else {
8567a37081eSFelix Fietkau if (AR_SREV_9280_20_OR_LATER(ah)) {
857a42acef0SFelix Fietkau if (!AR_SREV_9287_11_OR_LATER(ah))
858795f5e2cSLuis R. Rodriguez REG_CLR_BIT(ah, AR_PHY_ADC_CTL,
859795f5e2cSLuis R. Rodriguez AR_PHY_ADC_CTL_OFF_PWDADC);
860*b3a663f0SWenli Looi REG_SET_BIT(ah, AR_PHY_AGC_CONTROL(ah),
861795f5e2cSLuis R. Rodriguez AR_PHY_AGC_CONTROL_FLTR_CAL);
862795f5e2cSLuis R. Rodriguez }
863795f5e2cSLuis R. Rodriguez
864795f5e2cSLuis R. Rodriguez /* Calibrate the AGC */
865*b3a663f0SWenli Looi REG_WRITE(ah, AR_PHY_AGC_CONTROL(ah),
866*b3a663f0SWenli Looi REG_READ(ah, AR_PHY_AGC_CONTROL(ah)) |
867795f5e2cSLuis R. Rodriguez AR_PHY_AGC_CONTROL_CAL);
868795f5e2cSLuis R. Rodriguez
869795f5e2cSLuis R. Rodriguez /* Poll for offset calibration complete */
870*b3a663f0SWenli Looi if (!ath9k_hw_wait(ah, AR_PHY_AGC_CONTROL(ah),
871795f5e2cSLuis R. Rodriguez AR_PHY_AGC_CONTROL_CAL,
872795f5e2cSLuis R. Rodriguez 0, AH_WAIT_TIMEOUT)) {
873d2182b69SJoe Perches ath_dbg(common, CALIBRATE,
87405005c5fSRobert Shade "offset calibration failed to complete in %d ms; noisy environment?\n",
87505005c5fSRobert Shade AH_WAIT_TIMEOUT / 1000);
876795f5e2cSLuis R. Rodriguez return false;
877795f5e2cSLuis R. Rodriguez }
878795f5e2cSLuis R. Rodriguez
8797a37081eSFelix Fietkau if (AR_SREV_9280_20_OR_LATER(ah)) {
880a42acef0SFelix Fietkau if (!AR_SREV_9287_11_OR_LATER(ah))
881795f5e2cSLuis R. Rodriguez REG_SET_BIT(ah, AR_PHY_ADC_CTL,
882795f5e2cSLuis R. Rodriguez AR_PHY_ADC_CTL_OFF_PWDADC);
883*b3a663f0SWenli Looi REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL(ah),
884795f5e2cSLuis R. Rodriguez AR_PHY_AGC_CONTROL_FLTR_CAL);
885795f5e2cSLuis R. Rodriguez }
886795f5e2cSLuis R. Rodriguez }
887795f5e2cSLuis R. Rodriguez
888795f5e2cSLuis R. Rodriguez /* Do PA Calibration */
889795f5e2cSLuis R. Rodriguez ar9002_hw_pa_cal(ah, true);
8907b89fccfSFelix Fietkau ath9k_hw_loadnf(ah, chan);
8917b89fccfSFelix Fietkau ath9k_hw_start_nfcal(ah, true);
892795f5e2cSLuis R. Rodriguez
893795f5e2cSLuis R. Rodriguez ah->cal_list = ah->cal_list_last = ah->cal_list_curr = NULL;
894795f5e2cSLuis R. Rodriguez
895795f5e2cSLuis R. Rodriguez /* Enable IQ, ADC Gain and ADC DC offset CALs */
896795f5e2cSLuis R. Rodriguez if (AR_SREV_9100(ah) || AR_SREV_9160_10_OR_LATER(ah)) {
8976497827fSFelix Fietkau ah->supp_cals = IQ_MISMATCH_CAL;
8986497827fSFelix Fietkau
89981544026SRajkumar Manoharan if (AR_SREV_9160_10_OR_LATER(ah))
9006497827fSFelix Fietkau ah->supp_cals |= ADC_GAIN_CAL | ADC_DC_CAL;
9016497827fSFelix Fietkau
90281544026SRajkumar Manoharan if (AR_SREV_9287(ah))
90381544026SRajkumar Manoharan ah->supp_cals &= ~ADC_GAIN_CAL;
9046497827fSFelix Fietkau
90581544026SRajkumar Manoharan if (ar9002_hw_is_cal_supported(ah, chan, ADC_GAIN_CAL)) {
906795f5e2cSLuis R. Rodriguez INIT_CAL(&ah->adcgain_caldata);
907795f5e2cSLuis R. Rodriguez INSERT_CAL(ah, &ah->adcgain_caldata);
908d2182b69SJoe Perches ath_dbg(common, CALIBRATE,
909d2182b69SJoe Perches "enabling ADC Gain Calibration\n");
91081544026SRajkumar Manoharan }
9116497827fSFelix Fietkau
91281544026SRajkumar Manoharan if (ar9002_hw_is_cal_supported(ah, chan, ADC_DC_CAL)) {
913795f5e2cSLuis R. Rodriguez INIT_CAL(&ah->adcdc_caldata);
914795f5e2cSLuis R. Rodriguez INSERT_CAL(ah, &ah->adcdc_caldata);
915d2182b69SJoe Perches ath_dbg(common, CALIBRATE,
916d2182b69SJoe Perches "enabling ADC DC Calibration\n");
917795f5e2cSLuis R. Rodriguez }
9186497827fSFelix Fietkau
91981544026SRajkumar Manoharan if (ar9002_hw_is_cal_supported(ah, chan, IQ_MISMATCH_CAL)) {
920795f5e2cSLuis R. Rodriguez INIT_CAL(&ah->iq_caldata);
921795f5e2cSLuis R. Rodriguez INSERT_CAL(ah, &ah->iq_caldata);
922d2182b69SJoe Perches ath_dbg(common, CALIBRATE, "enabling IQ Calibration\n");
92381544026SRajkumar Manoharan }
924795f5e2cSLuis R. Rodriguez
925795f5e2cSLuis R. Rodriguez ah->cal_list_curr = ah->cal_list;
926795f5e2cSLuis R. Rodriguez
927795f5e2cSLuis R. Rodriguez if (ah->cal_list_curr)
928795f5e2cSLuis R. Rodriguez ath9k_hw_reset_calibration(ah, ah->cal_list_curr);
929795f5e2cSLuis R. Rodriguez }
930795f5e2cSLuis R. Rodriguez
93120bd2a09SFelix Fietkau if (ah->caldata)
93220bd2a09SFelix Fietkau ah->caldata->CalValid = 0;
933795f5e2cSLuis R. Rodriguez
934795f5e2cSLuis R. Rodriguez return true;
935795f5e2cSLuis R. Rodriguez }
936795f5e2cSLuis R. Rodriguez
937795f5e2cSLuis R. Rodriguez static const struct ath9k_percal_data iq_cal_multi_sample = {
938795f5e2cSLuis R. Rodriguez IQ_MISMATCH_CAL,
939795f5e2cSLuis R. Rodriguez MAX_CAL_SAMPLES,
940795f5e2cSLuis R. Rodriguez PER_MIN_LOG_COUNT,
941795f5e2cSLuis R. Rodriguez ar9002_hw_iqcal_collect,
942795f5e2cSLuis R. Rodriguez ar9002_hw_iqcalibrate
943795f5e2cSLuis R. Rodriguez };
944795f5e2cSLuis R. Rodriguez static const struct ath9k_percal_data iq_cal_single_sample = {
945795f5e2cSLuis R. Rodriguez IQ_MISMATCH_CAL,
946795f5e2cSLuis R. Rodriguez MIN_CAL_SAMPLES,
947795f5e2cSLuis R. Rodriguez PER_MAX_LOG_COUNT,
948795f5e2cSLuis R. Rodriguez ar9002_hw_iqcal_collect,
949795f5e2cSLuis R. Rodriguez ar9002_hw_iqcalibrate
950795f5e2cSLuis R. Rodriguez };
951795f5e2cSLuis R. Rodriguez static const struct ath9k_percal_data adc_gain_cal_multi_sample = {
952795f5e2cSLuis R. Rodriguez ADC_GAIN_CAL,
953795f5e2cSLuis R. Rodriguez MAX_CAL_SAMPLES,
954795f5e2cSLuis R. Rodriguez PER_MIN_LOG_COUNT,
955795f5e2cSLuis R. Rodriguez ar9002_hw_adc_gaincal_collect,
956795f5e2cSLuis R. Rodriguez ar9002_hw_adc_gaincal_calibrate
957795f5e2cSLuis R. Rodriguez };
958795f5e2cSLuis R. Rodriguez static const struct ath9k_percal_data adc_gain_cal_single_sample = {
959795f5e2cSLuis R. Rodriguez ADC_GAIN_CAL,
960795f5e2cSLuis R. Rodriguez MIN_CAL_SAMPLES,
961795f5e2cSLuis R. Rodriguez PER_MAX_LOG_COUNT,
962795f5e2cSLuis R. Rodriguez ar9002_hw_adc_gaincal_collect,
963795f5e2cSLuis R. Rodriguez ar9002_hw_adc_gaincal_calibrate
964795f5e2cSLuis R. Rodriguez };
965795f5e2cSLuis R. Rodriguez static const struct ath9k_percal_data adc_dc_cal_multi_sample = {
966795f5e2cSLuis R. Rodriguez ADC_DC_CAL,
967795f5e2cSLuis R. Rodriguez MAX_CAL_SAMPLES,
968795f5e2cSLuis R. Rodriguez PER_MIN_LOG_COUNT,
969795f5e2cSLuis R. Rodriguez ar9002_hw_adc_dccal_collect,
970795f5e2cSLuis R. Rodriguez ar9002_hw_adc_dccal_calibrate
971795f5e2cSLuis R. Rodriguez };
972795f5e2cSLuis R. Rodriguez static const struct ath9k_percal_data adc_dc_cal_single_sample = {
973795f5e2cSLuis R. Rodriguez ADC_DC_CAL,
974795f5e2cSLuis R. Rodriguez MIN_CAL_SAMPLES,
975795f5e2cSLuis R. Rodriguez PER_MAX_LOG_COUNT,
976795f5e2cSLuis R. Rodriguez ar9002_hw_adc_dccal_collect,
977795f5e2cSLuis R. Rodriguez ar9002_hw_adc_dccal_calibrate
978795f5e2cSLuis R. Rodriguez };
979795f5e2cSLuis R. Rodriguez
ar9002_hw_init_cal_settings(struct ath_hw * ah)980795f5e2cSLuis R. Rodriguez static void ar9002_hw_init_cal_settings(struct ath_hw *ah)
981795f5e2cSLuis R. Rodriguez {
982795f5e2cSLuis R. Rodriguez if (AR_SREV_9100(ah)) {
983795f5e2cSLuis R. Rodriguez ah->iq_caldata.calData = &iq_cal_multi_sample;
984795f5e2cSLuis R. Rodriguez ah->supp_cals = IQ_MISMATCH_CAL;
985795f5e2cSLuis R. Rodriguez return;
986795f5e2cSLuis R. Rodriguez }
987795f5e2cSLuis R. Rodriguez
988795f5e2cSLuis R. Rodriguez if (AR_SREV_9160_10_OR_LATER(ah)) {
9897a37081eSFelix Fietkau if (AR_SREV_9280_20_OR_LATER(ah)) {
990795f5e2cSLuis R. Rodriguez ah->iq_caldata.calData = &iq_cal_single_sample;
991795f5e2cSLuis R. Rodriguez ah->adcgain_caldata.calData =
992795f5e2cSLuis R. Rodriguez &adc_gain_cal_single_sample;
993795f5e2cSLuis R. Rodriguez ah->adcdc_caldata.calData =
994795f5e2cSLuis R. Rodriguez &adc_dc_cal_single_sample;
995795f5e2cSLuis R. Rodriguez } else {
996795f5e2cSLuis R. Rodriguez ah->iq_caldata.calData = &iq_cal_multi_sample;
997795f5e2cSLuis R. Rodriguez ah->adcgain_caldata.calData =
998795f5e2cSLuis R. Rodriguez &adc_gain_cal_multi_sample;
999795f5e2cSLuis R. Rodriguez ah->adcdc_caldata.calData =
1000795f5e2cSLuis R. Rodriguez &adc_dc_cal_multi_sample;
1001795f5e2cSLuis R. Rodriguez }
1002795f5e2cSLuis R. Rodriguez ah->supp_cals = ADC_GAIN_CAL | ADC_DC_CAL | IQ_MISMATCH_CAL;
100307422063SSujith Manoharan
100407422063SSujith Manoharan if (AR_SREV_9287(ah))
100507422063SSujith Manoharan ah->supp_cals &= ~ADC_GAIN_CAL;
1006795f5e2cSLuis R. Rodriguez }
1007795f5e2cSLuis R. Rodriguez }
1008795f5e2cSLuis R. Rodriguez
ar9002_hw_attach_calib_ops(struct ath_hw * ah)1009795f5e2cSLuis R. Rodriguez void ar9002_hw_attach_calib_ops(struct ath_hw *ah)
1010795f5e2cSLuis R. Rodriguez {
1011795f5e2cSLuis R. Rodriguez struct ath_hw_private_ops *priv_ops = ath9k_hw_private_ops(ah);
1012795f5e2cSLuis R. Rodriguez struct ath_hw_ops *ops = ath9k_hw_ops(ah);
1013795f5e2cSLuis R. Rodriguez
1014795f5e2cSLuis R. Rodriguez priv_ops->init_cal_settings = ar9002_hw_init_cal_settings;
1015795f5e2cSLuis R. Rodriguez priv_ops->init_cal = ar9002_hw_init_cal;
1016795f5e2cSLuis R. Rodriguez priv_ops->setup_calibration = ar9002_hw_setup_calibration;
1017795f5e2cSLuis R. Rodriguez
1018795f5e2cSLuis R. Rodriguez ops->calibrate = ar9002_hw_calibrate;
1019795f5e2cSLuis R. Rodriguez }
1020