1185380Ssam/*
2185380Ssam * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
3185380Ssam * Copyright (c) 2002-2008 Atheros Communications, Inc.
4185380Ssam *
5185380Ssam * Permission to use, copy, modify, and/or distribute this software for any
6185380Ssam * purpose with or without fee is hereby granted, provided that the above
7185380Ssam * copyright notice and this permission notice appear in all copies.
8185380Ssam *
9185380Ssam * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10185380Ssam * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11185380Ssam * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12185380Ssam * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13185380Ssam * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14185380Ssam * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15185380Ssam * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16185380Ssam *
17203158Srpaulo * $FreeBSD: releng/10.3/sys/dev/ath/ath_hal/ar5416/ar5416_cal_iq.c 211306 2010-08-14 15:28:15Z adrian $
18185380Ssam */
19185380Ssam#include "opt_ah.h"
20185380Ssam
21185380Ssam#include "ah.h"
22185380Ssam#include "ah_internal.h"
23185380Ssam#include "ah_devid.h"
24185380Ssam
25185380Ssam#include "ar5416/ar5416.h"
26185380Ssam#include "ar5416/ar5416reg.h"
27185380Ssam#include "ar5416/ar5416phy.h"
28185380Ssam
29185380Ssam/* IQ Cal aliases */
30185380Ssam#define	totalPowerMeasI(i)	caldata[0][i].u
31185380Ssam#define	totalPowerMeasQ(i)	caldata[1][i].u
32185380Ssam#define	totalIqCorrMeas(i)	caldata[2][i].s
33185380Ssam
34185380Ssam/*
35185380Ssam * Collect data from HW to later perform IQ Mismatch Calibration
36185380Ssam */
37185380Ssamvoid
38185380Ssamar5416IQCalCollect(struct ath_hal *ah)
39185380Ssam{
40185380Ssam	struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
41185380Ssam	int i;
42185380Ssam
43185380Ssam	/*
44185380Ssam	 * Accumulate IQ cal measures for active chains
45185380Ssam	 */
46185380Ssam	for (i = 0; i < AR5416_MAX_CHAINS; i++) {
47185380Ssam		cal->totalPowerMeasI(i) +=
48185380Ssam		    OS_REG_READ(ah, AR_PHY_CAL_MEAS_0(i));
49185380Ssam		cal->totalPowerMeasQ(i) +=
50185380Ssam		    OS_REG_READ(ah, AR_PHY_CAL_MEAS_1(i));
51185380Ssam		cal->totalIqCorrMeas(i) += (int32_t)
52185380Ssam		    OS_REG_READ(ah, AR_PHY_CAL_MEAS_2(i));
53185380Ssam		HALDEBUG(ah, HAL_DEBUG_PERCAL,
54185380Ssam		    "%d: Chn %d pmi=0x%08x;pmq=0x%08x;iqcm=0x%08x;\n",
55185380Ssam		    cal->calSamples, i, cal->totalPowerMeasI(i),
56185380Ssam		    cal->totalPowerMeasQ(i), cal->totalIqCorrMeas(i));
57185380Ssam	}
58185380Ssam}
59185380Ssam
60185380Ssam/*
61185380Ssam * Use HW data to do IQ Mismatch Calibration
62185380Ssam */
63185380Ssamvoid
64185380Ssamar5416IQCalibration(struct ath_hal *ah, uint8_t numChains)
65185380Ssam{
66185380Ssam	struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
67185380Ssam	int i;
68185380Ssam
69185380Ssam	for (i = 0; i < numChains; i++) {
70185380Ssam		uint32_t powerMeasI = cal->totalPowerMeasI(i);
71185380Ssam		uint32_t powerMeasQ = cal->totalPowerMeasQ(i);
72185380Ssam		uint32_t iqCorrMeas = cal->totalIqCorrMeas(i);
73185380Ssam		uint32_t qCoffDenom, iCoffDenom;
74185380Ssam		int iqCorrNeg;
75185380Ssam
76185380Ssam		HALDEBUG(ah, HAL_DEBUG_PERCAL,
77185380Ssam		    "Start IQ Cal and Correction for Chain %d\n", i);
78185380Ssam		HALDEBUG(ah, HAL_DEBUG_PERCAL,
79185380Ssam		    "Orignal: iq_corr_meas = 0x%08x\n", iqCorrMeas);
80185380Ssam
81185380Ssam		iqCorrNeg = 0;
82185380Ssam		/* iqCorrMeas is always negative. */
83185380Ssam		if (iqCorrMeas > 0x80000000)  {
84185380Ssam			iqCorrMeas = (0xffffffff - iqCorrMeas) + 1;
85185380Ssam			iqCorrNeg = 1;
86185380Ssam		}
87185380Ssam
88185380Ssam		HALDEBUG(ah, HAL_DEBUG_PERCAL, " pwr_meas_i = 0x%08x\n",
89185380Ssam		    powerMeasI);
90185380Ssam		HALDEBUG(ah, HAL_DEBUG_PERCAL, " pwr_meas_q = 0x%08x\n",
91185380Ssam		    powerMeasQ);
92185380Ssam		HALDEBUG(ah, HAL_DEBUG_PERCAL, " iqCorrNeg is 0x%08x\n",
93185380Ssam		    iqCorrNeg);
94185380Ssam
95185380Ssam		iCoffDenom = (powerMeasI/2 + powerMeasQ/2)/ 128;
96185380Ssam		qCoffDenom = powerMeasQ / 64;
97185380Ssam		/* Protect against divide-by-0 */
98185380Ssam		if (powerMeasQ != 0) {
99185380Ssam			/* IQ corr_meas is already negated if iqcorr_neg == 1 */
100185380Ssam			int32_t iCoff = iqCorrMeas/iCoffDenom;
101185380Ssam			int32_t qCoff = powerMeasI/qCoffDenom - 64;
102185380Ssam
103185380Ssam			HALDEBUG(ah, HAL_DEBUG_PERCAL, " iCoff = 0x%08x\n",
104185380Ssam			    iCoff);
105185380Ssam			HALDEBUG(ah, HAL_DEBUG_PERCAL, " qCoff = 0x%08x\n",
106185380Ssam			    qCoff);
107185380Ssam
108185380Ssam			/* Negate iCoff if iqCorrNeg == 0 */
109185380Ssam			iCoff = iCoff & 0x3f;
110185380Ssam			HALDEBUG(ah, HAL_DEBUG_PERCAL,
111185380Ssam			    "New:  iCoff = 0x%08x\n", iCoff);
112185380Ssam
113185380Ssam			if (iqCorrNeg == 0x0)
114185380Ssam				iCoff = 0x40 - iCoff;
115185380Ssam			if (qCoff > 15)
116185380Ssam				qCoff = 15;
117185380Ssam			else if (qCoff <= -16)
118211306Sadrian				qCoff = -16;
119185380Ssam			HALDEBUG(ah, HAL_DEBUG_PERCAL,
120185380Ssam			    " : iCoff = 0x%x  qCoff = 0x%x\n", iCoff, qCoff);
121185380Ssam
122185380Ssam			OS_REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4_CHAIN(i),
123185380Ssam			    AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF, iCoff);
124185380Ssam			OS_REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4_CHAIN(i),
125185380Ssam			    AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF, qCoff);
126185380Ssam			HALDEBUG(ah, HAL_DEBUG_PERCAL,
127185380Ssam			    "IQ Cal and Correction done for Chain %d\n", i);
128185380Ssam		}
129185380Ssam	}
130185380Ssam	OS_REG_SET_BIT(ah, AR_PHY_TIMING_CTRL4,
131185380Ssam	    AR_PHY_TIMING_CTRL4_IQCORR_ENABLE);
132185380Ssam}
133