Path: blob/main/sys/dev/ath/ath_hal/ar9002/ar9285_cal.c
39566 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2008-2010 Atheros Communications Inc.4* Copyright (c) 2011 Adrian Chadd, Xenion Pty Ltd.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/27#include "opt_ah.h"28#include "ah.h"29#include "ah_internal.h"3031#include "ah_eeprom_v4k.h"3233#include "ar9002/ar9285.h"34#include "ar5416/ar5416reg.h"35#include "ar5416/ar5416phy.h"36#include "ar9002/ar9002phy.h"37#include "ar9002/ar9285phy.h"38#include "ar9002/ar9285an.h"3940#include "ar9002/ar9285_cal.h"4142#define AR9285_CLCAL_REDO_THRESH 143#define MAX_PACAL_SKIPCOUNT 84445#define N(a) (sizeof (a) / sizeof (a[0]))4647static void48ar9285_hw_pa_cal(struct ath_hal *ah, HAL_BOOL is_reset)49{50uint32_t regVal;51int i, offset, offs_6_1, offs_0;52uint32_t ccomp_org, reg_field;53uint32_t regList[][2] = {54{ 0x786c, 0 },55{ 0x7854, 0 },56{ 0x7820, 0 },57{ 0x7824, 0 },58{ 0x7868, 0 },59{ 0x783c, 0 },60{ 0x7838, 0 },61};6263/* PA CAL is not needed for high power solution */64if (ath_hal_eepromGet(ah, AR_EEP_TXGAIN_TYPE, AH_NULL) ==65AR5416_EEP_TXGAIN_HIGH_POWER)66return;6768HALDEBUG(ah, HAL_DEBUG_PERCAL, "Running PA Calibration\n");6970for (i = 0; i < N(regList); i++)71regList[i][1] = OS_REG_READ(ah, regList[i][0]);7273regVal = OS_REG_READ(ah, 0x7834);74regVal &= (~(0x1));75OS_REG_WRITE(ah, 0x7834, regVal);76regVal = OS_REG_READ(ah, 0x9808);77regVal |= (0x1 << 27);78OS_REG_WRITE(ah, 0x9808, regVal);7980OS_REG_RMW_FIELD(ah, AR9285_AN_TOP3, AR9285_AN_TOP3_PWDDAC, 1);81OS_REG_RMW_FIELD(ah, AR9285_AN_RXTXBB1, AR9285_AN_RXTXBB1_PDRXTXBB1, 1);82OS_REG_RMW_FIELD(ah, AR9285_AN_RXTXBB1, AR9285_AN_RXTXBB1_PDV2I, 1);83OS_REG_RMW_FIELD(ah, AR9285_AN_RXTXBB1, AR9285_AN_RXTXBB1_PDDACIF, 1);84OS_REG_RMW_FIELD(ah, AR9285_AN_RF2G2, AR9285_AN_RF2G2_OFFCAL, 0);85OS_REG_RMW_FIELD(ah, AR9285_AN_RF2G7, AR9285_AN_RF2G7_PWDDB, 0);86OS_REG_RMW_FIELD(ah, AR9285_AN_RF2G1, AR9285_AN_RF2G1_ENPACAL, 0);87OS_REG_RMW_FIELD(ah, AR9285_AN_RF2G1, AR9285_AN_RF2G1_PDPADRV1, 0);88OS_REG_RMW_FIELD(ah, AR9285_AN_RF2G1, AR9285_AN_RF2G1_PDPADRV2, 0);89OS_REG_RMW_FIELD(ah, AR9285_AN_RF2G1, AR9285_AN_RF2G1_PDPAOUT, 0);90OS_REG_RMW_FIELD(ah, AR9285_AN_RF2G8, AR9285_AN_RF2G8_PADRVGN2TAB0, 7);91OS_REG_RMW_FIELD(ah, AR9285_AN_RF2G7, AR9285_AN_RF2G7_PADRVGN2TAB0, 0);92ccomp_org = MS(OS_REG_READ(ah, AR9285_AN_RF2G6), AR9285_AN_RF2G6_CCOMP);93OS_REG_RMW_FIELD(ah, AR9285_AN_RF2G6, AR9285_AN_RF2G6_CCOMP, 0xf);9495OS_REG_WRITE(ah, AR9285_AN_TOP2, 0xca0358a0);96OS_DELAY(30);97OS_REG_RMW_FIELD(ah, AR9285_AN_RF2G6, AR9285_AN_RF2G6_OFFS, 0);98OS_REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_PDVCCOMP, 0);99100for (i = 6; i > 0; i--) {101regVal = OS_REG_READ(ah, 0x7834);102regVal |= (1 << (19 + i));103OS_REG_WRITE(ah, 0x7834, regVal);104OS_DELAY(1);105regVal = OS_REG_READ(ah, 0x7834);106regVal &= (~(0x1 << (19 + i)));107reg_field = MS(OS_REG_READ(ah, 0x7840), AR9285_AN_RXTXBB1_SPARE9);108regVal |= (reg_field << (19 + i));109OS_REG_WRITE(ah, 0x7834, regVal);110}111112OS_REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_PDVCCOMP, 1);113OS_DELAY(1);114reg_field = MS(OS_REG_READ(ah, AR9285_AN_RF2G9), AR9285_AN_RXTXBB1_SPARE9);115OS_REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_PDVCCOMP, reg_field);116offs_6_1 = MS(OS_REG_READ(ah, AR9285_AN_RF2G6), AR9285_AN_RF2G6_OFFS);117offs_0 = MS(OS_REG_READ(ah, AR9285_AN_RF2G3), AR9285_AN_RF2G3_PDVCCOMP);118119offset = (offs_6_1<<1) | offs_0;120offset = offset - 0;121offs_6_1 = offset>>1;122offs_0 = offset & 1;123124if ((!is_reset) && (AH9285(ah)->pacal_info.prev_offset == offset)) {125if (AH9285(ah)->pacal_info.max_skipcount < MAX_PACAL_SKIPCOUNT)126AH9285(ah)->pacal_info.max_skipcount =1272 * AH9285(ah)->pacal_info.max_skipcount;128AH9285(ah)->pacal_info.skipcount = AH9285(ah)->pacal_info.max_skipcount;129} else {130AH9285(ah)->pacal_info.max_skipcount = 1;131AH9285(ah)->pacal_info.skipcount = 0;132AH9285(ah)->pacal_info.prev_offset = offset;133}134135OS_REG_RMW_FIELD(ah, AR9285_AN_RF2G6, AR9285_AN_RF2G6_OFFS, offs_6_1);136OS_REG_RMW_FIELD(ah, AR9285_AN_RF2G3, AR9285_AN_RF2G3_PDVCCOMP, offs_0);137138regVal = OS_REG_READ(ah, 0x7834);139regVal |= 0x1;140OS_REG_WRITE(ah, 0x7834, regVal);141regVal = OS_REG_READ(ah, 0x9808);142regVal &= (~(0x1 << 27));143OS_REG_WRITE(ah, 0x9808, regVal);144145for (i = 0; i < N(regList); i++)146OS_REG_WRITE(ah, regList[i][0], regList[i][1]);147148OS_REG_RMW_FIELD(ah, AR9285_AN_RF2G6, AR9285_AN_RF2G6_CCOMP, ccomp_org);149}150151void152ar9002_hw_pa_cal(struct ath_hal *ah, HAL_BOOL is_reset)153{154if (AR_SREV_KITE_11_OR_LATER(ah)) {155if (is_reset || !AH9285(ah)->pacal_info.skipcount)156ar9285_hw_pa_cal(ah, is_reset);157else158AH9285(ah)->pacal_info.skipcount--;159}160}161162/* Carrier leakage Calibration fix */163static HAL_BOOL164ar9285_hw_cl_cal(struct ath_hal *ah, const struct ieee80211_channel *chan)165{166OS_REG_SET_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_CL_CAL_ENABLE);167if (IEEE80211_IS_CHAN_HT20(chan)) {168OS_REG_SET_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_PARALLEL_CAL_ENABLE);169OS_REG_SET_BIT(ah, AR_PHY_TURBO, AR_PHY_FC_DYN2040_EN);170OS_REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,171AR_PHY_AGC_CONTROL_FLTR_CAL);172OS_REG_CLR_BIT(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_CAL_ENABLE);173OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL);174if (!ath_hal_wait(ah, AR_PHY_AGC_CONTROL,175AR_PHY_AGC_CONTROL_CAL, 0)) {176HALDEBUG(ah, HAL_DEBUG_PERCAL,177"offset calibration failed to complete in 1ms; noisy environment?\n");178return AH_FALSE;179}180OS_REG_CLR_BIT(ah, AR_PHY_TURBO, AR_PHY_FC_DYN2040_EN);181OS_REG_CLR_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_PARALLEL_CAL_ENABLE);182OS_REG_CLR_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_CL_CAL_ENABLE);183}184OS_REG_CLR_BIT(ah, AR_PHY_ADC_CTL, AR_PHY_ADC_CTL_OFF_PWDADC);185OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_FLTR_CAL);186OS_REG_SET_BIT(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_CAL_ENABLE);187OS_REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL);188if (!ath_hal_wait(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL,1890)) {190HALDEBUG(ah, HAL_DEBUG_PERCAL,191"offset calibration failed to complete in 1ms; noisy environment?\n");192return AH_FALSE;193}194195OS_REG_SET_BIT(ah, AR_PHY_ADC_CTL, AR_PHY_ADC_CTL_OFF_PWDADC);196OS_REG_CLR_BIT(ah, AR_PHY_CL_CAL_CTL, AR_PHY_CL_CAL_ENABLE);197OS_REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_FLTR_CAL);198199return AH_TRUE;200}201202static HAL_BOOL203ar9285_hw_clc(struct ath_hal *ah, const struct ieee80211_channel *chan)204{205int i;206uint32_t txgain_max;207uint32_t clc_gain, gain_mask = 0, clc_num = 0;208uint32_t reg_clc_I0, reg_clc_Q0;209uint32_t i0_num = 0;210uint32_t q0_num = 0;211uint32_t total_num = 0;212uint32_t reg_rf2g5_org;213HAL_BOOL retv = AH_TRUE;214215if (!(ar9285_hw_cl_cal(ah, chan)))216return AH_FALSE;217218txgain_max = MS(OS_REG_READ(ah, AR_PHY_TX_PWRCTRL7),219AR_PHY_TX_PWRCTRL_TX_GAIN_TAB_MAX);220221for (i = 0; i < (txgain_max+1); i++) {222clc_gain = (OS_REG_READ(ah, (AR_PHY_TX_GAIN_TBL1+(i<<2))) &223AR_PHY_TX_GAIN_CLC) >> AR_PHY_TX_GAIN_CLC_S;224if (!(gain_mask & (1 << clc_gain))) {225gain_mask |= (1 << clc_gain);226clc_num++;227}228}229230for (i = 0; i < clc_num; i++) {231reg_clc_I0 = (OS_REG_READ(ah, (AR_PHY_CLC_TBL1 + (i << 2)))232& AR_PHY_CLC_I0) >> AR_PHY_CLC_I0_S;233reg_clc_Q0 = (OS_REG_READ(ah, (AR_PHY_CLC_TBL1 + (i << 2)))234& AR_PHY_CLC_Q0) >> AR_PHY_CLC_Q0_S;235if (reg_clc_I0 == 0)236i0_num++;237238if (reg_clc_Q0 == 0)239q0_num++;240}241total_num = i0_num + q0_num;242if (total_num > AR9285_CLCAL_REDO_THRESH) {243reg_rf2g5_org = OS_REG_READ(ah, AR9285_RF2G5);244if (AR_SREV_9285E_20(ah)) {245OS_REG_WRITE(ah, AR9285_RF2G5,246(reg_rf2g5_org & AR9285_RF2G5_IC50TX) |247AR9285_RF2G5_IC50TX_XE_SET);248} else {249OS_REG_WRITE(ah, AR9285_RF2G5,250(reg_rf2g5_org & AR9285_RF2G5_IC50TX) |251AR9285_RF2G5_IC50TX_SET);252}253retv = ar9285_hw_cl_cal(ah, chan);254OS_REG_WRITE(ah, AR9285_RF2G5, reg_rf2g5_org);255}256return retv;257}258259HAL_BOOL260ar9285InitCalHardware(struct ath_hal *ah,261const struct ieee80211_channel *chan)262{263if (AR_SREV_KITE(ah) && AR_SREV_KITE_10_OR_LATER(ah) &&264(! ar9285_hw_clc(ah, chan)))265return AH_FALSE;266267return AH_TRUE;268}269270271