ar5416_cal_iq.c revision 185380
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 *
17185380Ssam * $Id: ar5416_cal_iq.c,v 1.2 2008/11/11 17:43:23 sam Exp $
18185380Ssam */
19185380Ssam#include "opt_ah.h"
20185380Ssam
21185380Ssam#ifdef AH_SUPPORT_AR5416
22185380Ssam
23185380Ssam#include "ah.h"
24185380Ssam#include "ah_internal.h"
25185380Ssam#include "ah_devid.h"
26185380Ssam
27185380Ssam#include "ar5416/ar5416.h"
28185380Ssam#include "ar5416/ar5416reg.h"
29185380Ssam#include "ar5416/ar5416phy.h"
30185380Ssam
31185380Ssam/* IQ Cal aliases */
32185380Ssam#define	totalPowerMeasI(i)	caldata[0][i].u
33185380Ssam#define	totalPowerMeasQ(i)	caldata[1][i].u
34185380Ssam#define	totalIqCorrMeas(i)	caldata[2][i].s
35185380Ssam
36185380Ssam/*
37185380Ssam * Collect data from HW to later perform IQ Mismatch Calibration
38185380Ssam */
39185380Ssamvoid
40185380Ssamar5416IQCalCollect(struct ath_hal *ah)
41185380Ssam{
42185380Ssam	struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
43185380Ssam	int i;
44185380Ssam
45185380Ssam	/*
46185380Ssam	 * Accumulate IQ cal measures for active chains
47185380Ssam	 */
48185380Ssam	for (i = 0; i < AR5416_MAX_CHAINS; i++) {
49185380Ssam		cal->totalPowerMeasI(i) +=
50185380Ssam		    OS_REG_READ(ah, AR_PHY_CAL_MEAS_0(i));
51185380Ssam		cal->totalPowerMeasQ(i) +=
52185380Ssam		    OS_REG_READ(ah, AR_PHY_CAL_MEAS_1(i));
53185380Ssam		cal->totalIqCorrMeas(i) += (int32_t)
54185380Ssam		    OS_REG_READ(ah, AR_PHY_CAL_MEAS_2(i));
55185380Ssam		HALDEBUG(ah, HAL_DEBUG_PERCAL,
56185380Ssam		    "%d: Chn %d pmi=0x%08x;pmq=0x%08x;iqcm=0x%08x;\n",
57185380Ssam		    cal->calSamples, i, cal->totalPowerMeasI(i),
58185380Ssam		    cal->totalPowerMeasQ(i), cal->totalIqCorrMeas(i));
59185380Ssam	}
60185380Ssam}
61185380Ssam
62185380Ssam/*
63185380Ssam * Use HW data to do IQ Mismatch Calibration
64185380Ssam */
65185380Ssamvoid
66185380Ssamar5416IQCalibration(struct ath_hal *ah, uint8_t numChains)
67185380Ssam{
68185380Ssam	struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;
69185380Ssam	int i;
70185380Ssam
71185380Ssam	for (i = 0; i < numChains; i++) {
72185380Ssam		uint32_t powerMeasI = cal->totalPowerMeasI(i);
73185380Ssam		uint32_t powerMeasQ = cal->totalPowerMeasQ(i);
74185380Ssam		uint32_t iqCorrMeas = cal->totalIqCorrMeas(i);
75185380Ssam		uint32_t qCoffDenom, iCoffDenom;
76185380Ssam		int iqCorrNeg;
77185380Ssam
78185380Ssam		HALDEBUG(ah, HAL_DEBUG_PERCAL,
79185380Ssam		    "Start IQ Cal and Correction for Chain %d\n", i);
80185380Ssam		HALDEBUG(ah, HAL_DEBUG_PERCAL,
81185380Ssam		    "Orignal: iq_corr_meas = 0x%08x\n", iqCorrMeas);
82185380Ssam
83185380Ssam		iqCorrNeg = 0;
84185380Ssam		/* iqCorrMeas is always negative. */
85185380Ssam		if (iqCorrMeas > 0x80000000)  {
86185380Ssam			iqCorrMeas = (0xffffffff - iqCorrMeas) + 1;
87185380Ssam			iqCorrNeg = 1;
88185380Ssam		}
89185380Ssam
90185380Ssam		HALDEBUG(ah, HAL_DEBUG_PERCAL, " pwr_meas_i = 0x%08x\n",
91185380Ssam		    powerMeasI);
92185380Ssam		HALDEBUG(ah, HAL_DEBUG_PERCAL, " pwr_meas_q = 0x%08x\n",
93185380Ssam		    powerMeasQ);
94185380Ssam		HALDEBUG(ah, HAL_DEBUG_PERCAL, " iqCorrNeg is 0x%08x\n",
95185380Ssam		    iqCorrNeg);
96185380Ssam
97185380Ssam		iCoffDenom = (powerMeasI/2 + powerMeasQ/2)/ 128;
98185380Ssam		qCoffDenom = powerMeasQ / 64;
99185380Ssam		/* Protect against divide-by-0 */
100185380Ssam		if (powerMeasQ != 0) {
101185380Ssam			/* IQ corr_meas is already negated if iqcorr_neg == 1 */
102185380Ssam			int32_t iCoff = iqCorrMeas/iCoffDenom;
103185380Ssam			int32_t qCoff = powerMeasI/qCoffDenom - 64;
104185380Ssam
105185380Ssam			HALDEBUG(ah, HAL_DEBUG_PERCAL, " iCoff = 0x%08x\n",
106185380Ssam			    iCoff);
107185380Ssam			HALDEBUG(ah, HAL_DEBUG_PERCAL, " qCoff = 0x%08x\n",
108185380Ssam			    qCoff);
109185380Ssam
110185380Ssam			/* Negate iCoff if iqCorrNeg == 0 */
111185380Ssam			iCoff = iCoff & 0x3f;
112185380Ssam			HALDEBUG(ah, HAL_DEBUG_PERCAL,
113185380Ssam			    "New:  iCoff = 0x%08x\n", iCoff);
114185380Ssam
115185380Ssam			if (iqCorrNeg == 0x0)
116185380Ssam				iCoff = 0x40 - iCoff;
117185380Ssam			if (qCoff > 15)
118185380Ssam				qCoff = 15;
119185380Ssam			else if (qCoff <= -16)
120185380Ssam				qCoff = 16;
121185380Ssam			HALDEBUG(ah, HAL_DEBUG_PERCAL,
122185380Ssam			    " : iCoff = 0x%x  qCoff = 0x%x\n", iCoff, qCoff);
123185380Ssam
124185380Ssam			OS_REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4_CHAIN(i),
125185380Ssam			    AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF, iCoff);
126185380Ssam			OS_REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4_CHAIN(i),
127185380Ssam			    AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF, qCoff);
128185380Ssam			HALDEBUG(ah, HAL_DEBUG_PERCAL,
129185380Ssam			    "IQ Cal and Correction done for Chain %d\n", i);
130185380Ssam		}
131185380Ssam	}
132185380Ssam	OS_REG_SET_BIT(ah, AR_PHY_TIMING_CTRL4,
133185380Ssam	    AR_PHY_TIMING_CTRL4_IQCORR_ENABLE);
134185380Ssam}
135185380Ssam#endif /* AH_SUPPORT_AR5416 */
136