1/*
2 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
3 * Copyright (c) 2002-2008 Atheros Communications, Inc.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 *
17 * $FreeBSD$
18 */
19#include "opt_ah.h"
20
21#include "ah.h"
22#include "ah_internal.h"
23#include "ah_devid.h"
24
25#include "ar5416/ar5416.h"
26#include "ar5416/ar5416reg.h"
27#include "ar5416/ar5416phy.h"
28
29/* IQ Cal aliases */
30#define	totalPowerMeasI(i)	caldata[0][i].u
31#define	totalPowerMeasQ(i)	caldata[1][i].u
32#define	totalIqCorrMeas(i)	caldata[2][i].s
33
34/*
35 * Collect data from HW to later perform IQ Mismatch Calibration
36 */
37void
38ar5416IQCalCollect(struct ath_hal *ah)
39{
40	struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
41	int i;
42
43	/*
44	 * Accumulate IQ cal measures for active chains
45	 */
46	for (i = 0; i < AR5416_MAX_CHAINS; i++) {
47		cal->totalPowerMeasI(i) +=
48		    OS_REG_READ(ah, AR_PHY_CAL_MEAS_0(i));
49		cal->totalPowerMeasQ(i) +=
50		    OS_REG_READ(ah, AR_PHY_CAL_MEAS_1(i));
51		cal->totalIqCorrMeas(i) += (int32_t)
52		    OS_REG_READ(ah, AR_PHY_CAL_MEAS_2(i));
53		HALDEBUG(ah, HAL_DEBUG_PERCAL,
54		    "%d: Chn %d pmi=0x%08x;pmq=0x%08x;iqcm=0x%08x;\n",
55		    cal->calSamples, i, cal->totalPowerMeasI(i),
56		    cal->totalPowerMeasQ(i), cal->totalIqCorrMeas(i));
57	}
58}
59
60/*
61 * Use HW data to do IQ Mismatch Calibration
62 */
63void
64ar5416IQCalibration(struct ath_hal *ah, uint8_t numChains)
65{
66	struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
67	int i;
68
69	for (i = 0; i < numChains; i++) {
70		uint32_t powerMeasI = cal->totalPowerMeasI(i);
71		uint32_t powerMeasQ = cal->totalPowerMeasQ(i);
72		uint32_t iqCorrMeas = cal->totalIqCorrMeas(i);
73		uint32_t qCoffDenom, iCoffDenom;
74		int iqCorrNeg;
75
76		HALDEBUG(ah, HAL_DEBUG_PERCAL,
77		    "Start IQ Cal and Correction for Chain %d\n", i);
78		HALDEBUG(ah, HAL_DEBUG_PERCAL,
79		    "Orignal: iq_corr_meas = 0x%08x\n", iqCorrMeas);
80
81		iqCorrNeg = 0;
82		/* iqCorrMeas is always negative. */
83		if (iqCorrMeas > 0x80000000)  {
84			iqCorrMeas = (0xffffffff - iqCorrMeas) + 1;
85			iqCorrNeg = 1;
86		}
87
88		HALDEBUG(ah, HAL_DEBUG_PERCAL, " pwr_meas_i = 0x%08x\n",
89		    powerMeasI);
90		HALDEBUG(ah, HAL_DEBUG_PERCAL, " pwr_meas_q = 0x%08x\n",
91		    powerMeasQ);
92		HALDEBUG(ah, HAL_DEBUG_PERCAL, " iqCorrNeg is 0x%08x\n",
93		    iqCorrNeg);
94
95		iCoffDenom = (powerMeasI/2 + powerMeasQ/2)/ 128;
96		qCoffDenom = powerMeasQ / 64;
97		/* Protect against divide-by-0 */
98		if (powerMeasQ != 0) {
99			/* IQ corr_meas is already negated if iqcorr_neg == 1 */
100			int32_t iCoff = iqCorrMeas/iCoffDenom;
101			int32_t qCoff = powerMeasI/qCoffDenom - 64;
102
103			HALDEBUG(ah, HAL_DEBUG_PERCAL, " iCoff = 0x%08x\n",
104			    iCoff);
105			HALDEBUG(ah, HAL_DEBUG_PERCAL, " qCoff = 0x%08x\n",
106			    qCoff);
107
108			/* Negate iCoff if iqCorrNeg == 0 */
109			iCoff = iCoff & 0x3f;
110			HALDEBUG(ah, HAL_DEBUG_PERCAL,
111			    "New:  iCoff = 0x%08x\n", iCoff);
112
113			if (iqCorrNeg == 0x0)
114				iCoff = 0x40 - iCoff;
115			if (qCoff > 15)
116				qCoff = 15;
117			else if (qCoff <= -16)
118				qCoff = -16;
119			HALDEBUG(ah, HAL_DEBUG_PERCAL,
120			    " : iCoff = 0x%x  qCoff = 0x%x\n", iCoff, qCoff);
121
122			OS_REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4_CHAIN(i),
123			    AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF, iCoff);
124			OS_REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4_CHAIN(i),
125			    AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF, qCoff);
126			HALDEBUG(ah, HAL_DEBUG_PERCAL,
127			    "IQ Cal and Correction done for Chain %d\n", i);
128		}
129	}
130	OS_REG_SET_BIT(ah, AR_PHY_TIMING_CTRL4,
131	    AR_PHY_TIMING_CTRL4_IQCORR_ENABLE);
132}
133