Path: blob/main/sys/dev/ath/ath_hal/ar9002/ar9280.c
39566 views
/*-1* SPDX-License-Identifier: ISC2*3* Copyright (c) 2008-2009 Sam Leffler, Errno Consulting4* Copyright (c) 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/*21* NB: Merlin and later have a simpler RF backend.22*/23#include "ah.h"24#include "ah_internal.h"2526#include "ah_eeprom_v14.h"2728#include "ar9002/ar9280.h"29#include "ar5416/ar5416reg.h"30#include "ar5416/ar5416phy.h"3132#define N(a) (sizeof(a)/sizeof(a[0]))3334struct ar9280State {35RF_HAL_FUNCS base; /* public state, must be first */36uint16_t pcdacTable[1]; /* XXX */37};38#define AR9280(ah) ((struct ar9280State *) AH5212(ah)->ah_rfHal)3940static HAL_BOOL ar9280GetChannelMaxMinPower(struct ath_hal *,41const struct ieee80211_channel *, int16_t *maxPow,int16_t *minPow);42int16_t ar9280GetNfAdjust(struct ath_hal *ah, const HAL_CHANNEL_INTERNAL *c);4344static void45ar9280WriteRegs(struct ath_hal *ah, u_int modesIndex, u_int freqIndex,46int writes)47{48(void) ath_hal_ini_write(ah, &AH5416(ah)->ah_ini_bb_rfgain,49freqIndex, writes);50}5152/*53* Take the MHz channel value and set the Channel value54*55* ASSUMES: Writes enabled to analog bus56*57* Actual Expression,58*59* For 2GHz channel,60* Channel Frequency = (3/4) * freq_ref * (chansel[8:0] + chanfrac[16:0]/2^17)61* (freq_ref = 40MHz)62*63* For 5GHz channel,64* Channel Frequency = (3/2) * freq_ref * (chansel[8:0] + chanfrac[16:0]/2^10)65* (freq_ref = 40MHz/(24>>amodeRefSel))66*67* For 5GHz channels which are 5MHz spaced,68* Channel Frequency = (3/2) * freq_ref * (chansel[8:0] + chanfrac[16:0]/2^17)69* (freq_ref = 40MHz)70*/71static HAL_BOOL72ar9280SetChannel(struct ath_hal *ah, const struct ieee80211_channel *chan)73{74uint16_t bMode, fracMode, aModeRefSel = 0;75uint32_t freq, ndiv, channelSel = 0, channelFrac = 0, reg32 = 0;76CHAN_CENTERS centers;77uint32_t refDivA = 24;78uint8_t frac_n_5g;7980OS_MARK(ah, AH_MARK_SETCHANNEL, chan->ic_freq);8182ar5416GetChannelCenters(ah, chan, ¢ers);83freq = centers.synth_center;8485reg32 = OS_REG_READ(ah, AR_PHY_SYNTH_CONTROL);86reg32 &= 0xc0000000;8788if (ath_hal_eepromGet(ah, AR_EEP_FRAC_N_5G, &frac_n_5g) != HAL_OK)89frac_n_5g = 0;9091if (freq < 4800) { /* 2 GHz, fractional mode */92uint32_t txctl;9394bMode = 1;95fracMode = 1;96aModeRefSel = 0;97channelSel = (freq * 0x10000)/15;9899txctl = OS_REG_READ(ah, AR_PHY_CCK_TX_CTRL);100if (freq == 2484) {101/* Enable channel spreading for channel 14 */102OS_REG_WRITE(ah, AR_PHY_CCK_TX_CTRL,103txctl | AR_PHY_CCK_TX_CTRL_JAPAN);104} else {105OS_REG_WRITE(ah, AR_PHY_CCK_TX_CTRL,106txctl &~ AR_PHY_CCK_TX_CTRL_JAPAN);107}108} else {109bMode = 0;110fracMode = 0;111112switch (frac_n_5g) {113case 0:114/*115* Enable fractional mode for half/quarter rate116* channels.117*118* This is from the Linux ath9k code, rather than119* the Atheros HAL code.120*/121if (IEEE80211_IS_CHAN_QUARTER(chan) ||122IEEE80211_IS_CHAN_HALF(chan))123aModeRefSel = 0;124else if ((freq % 20) == 0) {125aModeRefSel = 3;126} else if ((freq % 10) == 0) {127aModeRefSel = 2;128}129if (aModeRefSel) break;130case 1:131default:132aModeRefSel = 0;133/* Enable 2G (fractional) mode for channels which are 5MHz spaced */134135/*136* Workaround for talking on PSB non-5MHz channels;137* the pre-Merlin chips only had a 2.5MHz channel138* spacing so some channels aren't reachable.139140*141* This interoperates on the quarter rate channels142* with the AR5112 and later RF synths. Please note143* that the synthesiser isn't able to completely144* accurately represent these frequencies (as the145* resolution in this reference is 2.5MHz) and thus146* it will be slightly "off centre." This matches147* the same slightly incorrect centre frequency148* behaviour that the AR5112 and later channel149* selection code has.150*151* This also interoperates with the AR5416152* synthesiser modification for programming153* fractional frequencies in 5GHz mode. However154* that modification is also disabled by default.155*156* This is disabled because it hasn't been tested for157* regulatory compliance and neither have the NICs158* which would use it. So if you enable this code,159* you must first ensure that you've re-certified the160* NICs in question beforehand or you will be161* violating your local regulatory rules and breaking162* the law.163*/164#if 0165if (freq % 5 == 0) {166#endif167/* Normal */168fracMode = 1;169refDivA = 1;170channelSel = (freq * 0x8000)/15;171#if 0172} else {173/* Offset by 500KHz */174uint32_t f, ch, ch2;175176fracMode = 1;177refDivA = 1;178179/* Calculate the "adjusted" frequency */180f = freq - 2;181ch = (((f - 4800) * 10) / 25) + 1;182183ch2 = ((ch * 25) / 5) + 9600;184channelSel = (ch2 * 0x4000) / 15;185//ath_hal_printf(ah,186// "%s: freq=%d, ch=%d, ch2=%d, "187// "channelSel=%d\n",188// __func__, freq, ch, ch2, channelSel);189}190#endif191192/* RefDivA setting */193OS_A_REG_RMW_FIELD(ah, AR_AN_SYNTH9,194AR_AN_SYNTH9_REFDIVA, refDivA);195}196197if (!fracMode) {198ndiv = (freq * (refDivA >> aModeRefSel))/60;199channelSel = ndiv & 0x1ff;200channelFrac = (ndiv & 0xfffffe00) * 2;201channelSel = (channelSel << 17) | channelFrac;202}203}204205reg32 = reg32 | (bMode << 29) | (fracMode << 28) |206(aModeRefSel << 26) | (channelSel);207208OS_REG_WRITE(ah, AR_PHY_SYNTH_CONTROL, reg32);209210AH_PRIVATE(ah)->ah_curchan = chan;211212return AH_TRUE;213}214215/*216* Return a reference to the requested RF Bank.217*/218static uint32_t *219ar9280GetRfBank(struct ath_hal *ah, int bank)220{221HALDEBUG(ah, HAL_DEBUG_ANY, "%s: unknown RF Bank %d requested\n",222__func__, bank);223return AH_NULL;224}225226/*227* Reads EEPROM header info from device structure and programs228* all rf registers229*/230static HAL_BOOL231ar9280SetRfRegs(struct ath_hal *ah, const struct ieee80211_channel *chan,232uint16_t modesIndex, uint16_t *rfXpdGain)233{234return AH_TRUE; /* nothing to do */235}236237/*238* Read the transmit power levels from the structures taken from EEPROM239* Interpolate read transmit power values for this channel240* Organize the transmit power values into a table for writing into the hardware241*/242243static HAL_BOOL244ar9280SetPowerTable(struct ath_hal *ah, int16_t *pPowerMin, int16_t *pPowerMax,245const struct ieee80211_channel *chan, uint16_t *rfXpdGain)246{247return AH_TRUE;248}249250#if 0251static int16_t252ar9280GetMinPower(struct ath_hal *ah, EXPN_DATA_PER_CHANNEL_5112 *data)253{254int i, minIndex;255int16_t minGain,minPwr,minPcdac,retVal;256257/* Assume NUM_POINTS_XPD0 > 0 */258minGain = data->pDataPerXPD[0].xpd_gain;259for (minIndex=0,i=1; i<NUM_XPD_PER_CHANNEL; i++) {260if (data->pDataPerXPD[i].xpd_gain < minGain) {261minIndex = i;262minGain = data->pDataPerXPD[i].xpd_gain;263}264}265minPwr = data->pDataPerXPD[minIndex].pwr_t4[0];266minPcdac = data->pDataPerXPD[minIndex].pcdac[0];267for (i=1; i<NUM_POINTS_XPD0; i++) {268if (data->pDataPerXPD[minIndex].pwr_t4[i] < minPwr) {269minPwr = data->pDataPerXPD[minIndex].pwr_t4[i];270minPcdac = data->pDataPerXPD[minIndex].pcdac[i];271}272}273retVal = minPwr - (minPcdac*2);274return(retVal);275}276#endif277278static HAL_BOOL279ar9280GetChannelMaxMinPower(struct ath_hal *ah,280const struct ieee80211_channel *chan,281int16_t *maxPow, int16_t *minPow)282{283#if 0284struct ath_hal_5212 *ahp = AH5212(ah);285int numChannels=0,i,last;286int totalD, totalF,totalMin;287EXPN_DATA_PER_CHANNEL_5112 *data=AH_NULL;288EEPROM_POWER_EXPN_5112 *powerArray=AH_NULL;289290*maxPow = 0;291if (IS_CHAN_A(chan)) {292powerArray = ahp->ah_modePowerArray5112;293data = powerArray[headerInfo11A].pDataPerChannel;294numChannels = powerArray[headerInfo11A].numChannels;295} else if (IS_CHAN_G(chan) || IS_CHAN_108G(chan)) {296/* XXX - is this correct? Should we also use the same power for turbo G? */297powerArray = ahp->ah_modePowerArray5112;298data = powerArray[headerInfo11G].pDataPerChannel;299numChannels = powerArray[headerInfo11G].numChannels;300} else if (IS_CHAN_B(chan)) {301powerArray = ahp->ah_modePowerArray5112;302data = powerArray[headerInfo11B].pDataPerChannel;303numChannels = powerArray[headerInfo11B].numChannels;304} else {305return (AH_TRUE);306}307/* Make sure the channel is in the range of the TP values308* (freq piers)309*/310if ((numChannels < 1) ||311(chan->channel < data[0].channelValue) ||312(chan->channel > data[numChannels-1].channelValue))313return(AH_FALSE);314315/* Linearly interpolate the power value now */316for (last=0,i=0;317(i<numChannels) && (chan->channel > data[i].channelValue);318last=i++);319totalD = data[i].channelValue - data[last].channelValue;320if (totalD > 0) {321totalF = data[i].maxPower_t4 - data[last].maxPower_t4;322*maxPow = (int8_t) ((totalF*(chan->channel-data[last].channelValue) + data[last].maxPower_t4*totalD)/totalD);323324totalMin = ar9280GetMinPower(ah,&data[i]) - ar9280GetMinPower(ah, &data[last]);325*minPow = (int8_t) ((totalMin*(chan->channel-data[last].channelValue) + ar9280GetMinPower(ah, &data[last])*totalD)/totalD);326return (AH_TRUE);327} else {328if (chan->channel == data[i].channelValue) {329*maxPow = data[i].maxPower_t4;330*minPow = ar9280GetMinPower(ah, &data[i]);331return(AH_TRUE);332} else333return(AH_FALSE);334}335#else336*maxPow = *minPow = 0;337return AH_FALSE;338#endif339}340341/*342* The ordering of nfarray is thus:343*344* nfarray[0]: Chain 0 ctl345* nfarray[1]: Chain 1 ctl346* nfarray[2]: Chain 2 ctl347* nfarray[3]: Chain 0 ext348* nfarray[4]: Chain 1 ext349* nfarray[5]: Chain 2 ext350*/351static void352ar9280GetNoiseFloor(struct ath_hal *ah, int16_t nfarray[])353{354int16_t nf;355356nf = MS(OS_REG_READ(ah, AR_PHY_CCA), AR9280_PHY_MINCCA_PWR);357if (nf & 0x100)358nf = 0 - ((nf ^ 0x1ff) + 1);359HALDEBUG(ah, HAL_DEBUG_NFCAL,360"NF calibrated [ctl] [chain 0] is %d\n", nf);361nfarray[0] = nf;362363nf = MS(OS_REG_READ(ah, AR_PHY_CH1_CCA), AR9280_PHY_CH1_MINCCA_PWR);364if (nf & 0x100)365nf = 0 - ((nf ^ 0x1ff) + 1);366HALDEBUG(ah, HAL_DEBUG_NFCAL,367"NF calibrated [ctl] [chain 1] is %d\n", nf);368nfarray[1] = nf;369370nf = MS(OS_REG_READ(ah, AR_PHY_EXT_CCA), AR9280_PHY_EXT_MINCCA_PWR);371if (nf & 0x100)372nf = 0 - ((nf ^ 0x1ff) + 1);373HALDEBUG(ah, HAL_DEBUG_NFCAL,374"NF calibrated [ext] [chain 0] is %d\n", nf);375nfarray[3] = nf;376377nf = MS(OS_REG_READ(ah, AR_PHY_CH1_EXT_CCA), AR9280_PHY_CH1_EXT_MINCCA_PWR);378if (nf & 0x100)379nf = 0 - ((nf ^ 0x1ff) + 1);380HALDEBUG(ah, HAL_DEBUG_NFCAL,381"NF calibrated [ext] [chain 1] is %d\n", nf);382nfarray[4] = nf;383384/* Chain 2 - invalid */385nfarray[2] = 0;386nfarray[5] = 0;387388}389390/*391* Adjust NF based on statistical values for 5GHz frequencies.392* Stubbed:Not used by Fowl393*/394int16_t395ar9280GetNfAdjust(struct ath_hal *ah, const HAL_CHANNEL_INTERNAL *c)396{397return 0;398}399400/*401* Free memory for analog bank scratch buffers402*/403static void404ar9280RfDetach(struct ath_hal *ah)405{406struct ath_hal_5212 *ahp = AH5212(ah);407408HALASSERT(ahp->ah_rfHal != AH_NULL);409ath_hal_free(ahp->ah_rfHal);410ahp->ah_rfHal = AH_NULL;411}412413HAL_BOOL414ar9280RfAttach(struct ath_hal *ah, HAL_STATUS *status)415{416struct ath_hal_5212 *ahp = AH5212(ah);417struct ar9280State *priv;418419HALDEBUG(ah, HAL_DEBUG_ATTACH, "%s: attach AR9280 radio\n", __func__);420421HALASSERT(ahp->ah_rfHal == AH_NULL);422priv = ath_hal_malloc(sizeof(struct ar9280State));423if (priv == AH_NULL) {424HALDEBUG(ah, HAL_DEBUG_ANY,425"%s: cannot allocate private state\n", __func__);426*status = HAL_ENOMEM; /* XXX */427return AH_FALSE;428}429priv->base.rfDetach = ar9280RfDetach;430priv->base.writeRegs = ar9280WriteRegs;431priv->base.getRfBank = ar9280GetRfBank;432priv->base.setChannel = ar9280SetChannel;433priv->base.setRfRegs = ar9280SetRfRegs;434priv->base.setPowerTable = ar9280SetPowerTable;435priv->base.getChannelMaxMinPower = ar9280GetChannelMaxMinPower;436priv->base.getNfAdjust = ar9280GetNfAdjust;437438ahp->ah_pcdacTable = priv->pcdacTable;439ahp->ah_pcdacTableSize = sizeof(priv->pcdacTable);440ahp->ah_rfHal = &priv->base;441/*442* Set noise floor adjust method; we arrange a443* direct call instead of thunking.444*/445AH_PRIVATE(ah)->ah_getNfAdjust = priv->base.getNfAdjust;446AH_PRIVATE(ah)->ah_getNoiseFloor = ar9280GetNoiseFloor;447448return AH_TRUE;449}450451static HAL_BOOL452ar9280RfProbe(struct ath_hal *ah)453{454return (AR_SREV_MERLIN(ah));455}456457AH_RF(RF9280, ar9280RfProbe, ar9280RfAttach);458459460