Path: blob/main/sys/dev/ath/ath_hal/ar5416/ar5416_cal_iq.c
39566 views
/*-1* SPDX-License-Identifier: ISC2*3* Copyright (c) 2002-2008 Sam Leffler, Errno Consulting4* Copyright (c) 2002-2008 Atheros Communications, Inc.5*6* Permission to use, copy, modify, and/or distribute this software for any7* purpose with or without fee is hereby granted, provided that the above8* copyright notice and this permission notice appear in all copies.9*10* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES11* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF12* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR13* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES14* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN15* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF16* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.17*/18#include "opt_ah.h"1920#include "ah.h"21#include "ah_internal.h"22#include "ah_devid.h"2324#include "ar5416/ar5416.h"25#include "ar5416/ar5416reg.h"26#include "ar5416/ar5416phy.h"2728/* IQ Cal aliases */29#define totalPowerMeasI(i) caldata[0][i].u30#define totalPowerMeasQ(i) caldata[1][i].u31#define totalIqCorrMeas(i) caldata[2][i].s3233/*34* Collect data from HW to later perform IQ Mismatch Calibration35*/36void37ar5416IQCalCollect(struct ath_hal *ah)38{39struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;40int i;4142/*43* Accumulate IQ cal measures for active chains44*/45for (i = 0; i < AR5416_MAX_CHAINS; i++) {46cal->totalPowerMeasI(i) +=47OS_REG_READ(ah, AR_PHY_CAL_MEAS_0(i));48cal->totalPowerMeasQ(i) +=49OS_REG_READ(ah, AR_PHY_CAL_MEAS_1(i));50cal->totalIqCorrMeas(i) += (int32_t)51OS_REG_READ(ah, AR_PHY_CAL_MEAS_2(i));52HALDEBUG(ah, HAL_DEBUG_PERCAL,53"%d: Chn %d pmi=0x%08x;pmq=0x%08x;iqcm=0x%08x;\n",54cal->calSamples, i, cal->totalPowerMeasI(i),55cal->totalPowerMeasQ(i), cal->totalIqCorrMeas(i));56}57}5859/*60* Use HW data to do IQ Mismatch Calibration61*/62void63ar5416IQCalibration(struct ath_hal *ah, uint8_t numChains)64{65struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;66int i;6768for (i = 0; i < numChains; i++) {69uint32_t powerMeasI = cal->totalPowerMeasI(i);70uint32_t powerMeasQ = cal->totalPowerMeasQ(i);71uint32_t iqCorrMeas = cal->totalIqCorrMeas(i);72uint32_t qCoffDenom, iCoffDenom;73int iqCorrNeg;7475HALDEBUG(ah, HAL_DEBUG_PERCAL,76"Start IQ Cal and Correction for Chain %d\n", i);77HALDEBUG(ah, HAL_DEBUG_PERCAL,78"Orignal: iq_corr_meas = 0x%08x\n", iqCorrMeas);7980iqCorrNeg = 0;81/* iqCorrMeas is always negative. */82if (iqCorrMeas > 0x80000000) {83iqCorrMeas = (0xffffffff - iqCorrMeas) + 1;84iqCorrNeg = 1;85}8687HALDEBUG(ah, HAL_DEBUG_PERCAL, " pwr_meas_i = 0x%08x\n",88powerMeasI);89HALDEBUG(ah, HAL_DEBUG_PERCAL, " pwr_meas_q = 0x%08x\n",90powerMeasQ);91HALDEBUG(ah, HAL_DEBUG_PERCAL, " iqCorrNeg is 0x%08x\n",92iqCorrNeg);9394iCoffDenom = (powerMeasI/2 + powerMeasQ/2)/ 128;95qCoffDenom = powerMeasQ / 64;96/* Protect against divide-by-0 */97if (powerMeasQ != 0) {98/* IQ corr_meas is already negated if iqcorr_neg == 1 */99int32_t iCoff = iqCorrMeas/iCoffDenom;100int32_t qCoff = powerMeasI/qCoffDenom - 64;101102HALDEBUG(ah, HAL_DEBUG_PERCAL, " iCoff = 0x%08x\n",103iCoff);104HALDEBUG(ah, HAL_DEBUG_PERCAL, " qCoff = 0x%08x\n",105qCoff);106107/* Negate iCoff if iqCorrNeg == 0 */108iCoff = iCoff & 0x3f;109HALDEBUG(ah, HAL_DEBUG_PERCAL,110"New: iCoff = 0x%08x\n", iCoff);111112if (iqCorrNeg == 0x0)113iCoff = 0x40 - iCoff;114if (qCoff > 15)115qCoff = 15;116else if (qCoff <= -16)117qCoff = -16;118HALDEBUG(ah, HAL_DEBUG_PERCAL,119" : iCoff = 0x%x qCoff = 0x%x\n", iCoff, qCoff);120121OS_REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4_CHAIN(i),122AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF, iCoff);123OS_REG_RMW_FIELD(ah, AR_PHY_TIMING_CTRL4_CHAIN(i),124AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF, qCoff);125HALDEBUG(ah, HAL_DEBUG_PERCAL,126"IQ Cal and Correction done for Chain %d\n", i);127}128}129OS_REG_SET_BIT(ah, AR_PHY_TIMING_CTRL4,130AR_PHY_TIMING_CTRL4_IQCORR_ENABLE);131}132133134