Path: blob/main/sys/dev/ath/ath_hal/ar5416/ar5416_cal_adcgain.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/* Adc Gain Cal aliases */29#define totalAdcIOddPhase(i) caldata[0][i].u30#define totalAdcIEvenPhase(i) caldata[1][i].u31#define totalAdcQOddPhase(i) caldata[2][i].u32#define totalAdcQEvenPhase(i) caldata[3][i].u3334/*35* Collect data from HW to later perform ADC Gain Calibration36*/37void38ar5416AdcGainCalCollect(struct ath_hal *ah)39{40struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;41int i;4243/*44* Accumulate ADC Gain cal measures for active chains45*/46for (i = 0; i < AR5416_MAX_CHAINS; i++) {47cal->totalAdcIOddPhase(i) +=48OS_REG_READ(ah, AR_PHY_CAL_MEAS_0(i));49cal->totalAdcIEvenPhase(i) +=50OS_REG_READ(ah, AR_PHY_CAL_MEAS_1(i));51cal->totalAdcQOddPhase(i) +=52OS_REG_READ(ah, AR_PHY_CAL_MEAS_2(i));53cal->totalAdcQEvenPhase(i) +=54OS_REG_READ(ah, AR_PHY_CAL_MEAS_3(i));5556HALDEBUG(ah, HAL_DEBUG_PERCAL,57"%d: Chn %d oddi=0x%08x; eveni=0x%08x; oddq=0x%08x; evenq=0x%08x;\n",58cal->calSamples, i, cal->totalAdcIOddPhase(i),59cal->totalAdcIEvenPhase(i), cal->totalAdcQOddPhase(i),60cal->totalAdcQEvenPhase(i));61}62}6364/*65* Use HW data to do ADC Gain Calibration66*/67void68ar5416AdcGainCalibration(struct ath_hal *ah, uint8_t numChains)69{70struct ar5416PerCal *cal = &AH5416(ah)->ah_cal;71uint32_t i;7273for (i = 0; i < numChains; i++) {74uint32_t iOddMeasOffset = cal->totalAdcIOddPhase(i);75uint32_t iEvenMeasOffset = cal->totalAdcIEvenPhase(i);76uint32_t qOddMeasOffset = cal->totalAdcQOddPhase(i);77uint32_t qEvenMeasOffset = cal->totalAdcQEvenPhase(i);7879HALDEBUG(ah, HAL_DEBUG_PERCAL,80"Start ADC Gain Cal for Chain %d\n", i);81HALDEBUG(ah, HAL_DEBUG_PERCAL,82" pwr_meas_odd_i = 0x%08x\n", iOddMeasOffset);83HALDEBUG(ah, HAL_DEBUG_PERCAL,84" pwr_meas_even_i = 0x%08x\n", iEvenMeasOffset);85HALDEBUG(ah, HAL_DEBUG_PERCAL,86" pwr_meas_odd_q = 0x%08x\n", qOddMeasOffset);87HALDEBUG(ah, HAL_DEBUG_PERCAL,88" pwr_meas_even_q = 0x%08x\n", qEvenMeasOffset);8990if (iOddMeasOffset != 0 && qEvenMeasOffset != 0) {91uint32_t iGainMismatch =92((iEvenMeasOffset*32)/iOddMeasOffset) & 0x3f;93uint32_t qGainMismatch =94((qOddMeasOffset*32)/qEvenMeasOffset) & 0x3f;95uint32_t val;9697HALDEBUG(ah, HAL_DEBUG_PERCAL,98" gain_mismatch_i = 0x%08x\n",99iGainMismatch);100HALDEBUG(ah, HAL_DEBUG_PERCAL,101" gain_mismatch_q = 0x%08x\n",102qGainMismatch);103104val = OS_REG_READ(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(i));105val &= 0xfffff000;106val |= (qGainMismatch) | (iGainMismatch << 6);107OS_REG_WRITE(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(i), val);108109HALDEBUG(ah, HAL_DEBUG_PERCAL,110"ADC Gain Cal done for Chain %d\n", i);111}112}113OS_REG_SET_BIT(ah, AR_PHY_NEW_ADC_DC_GAIN_CORR(0),114AR_PHY_NEW_ADC_GAIN_CORR_ENABLE);115}116117118