Path: blob/main/sys/dev/ath/ath_hal/ar9002/ar9287.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/ar9287.h"29#include "ar5416/ar5416reg.h"30#include "ar5416/ar5416phy.h"3132#define N(a) (sizeof(a)/sizeof(a[0]))3334struct ar9287State {35RF_HAL_FUNCS base; /* public state, must be first */36uint16_t pcdacTable[1]; /* XXX */37};38#define AR9287(ah) ((struct ar9287State *) AH5212(ah)->ah_rfHal)3940static HAL_BOOL ar9287GetChannelMaxMinPower(struct ath_hal *,41const struct ieee80211_channel *, int16_t *maxPow,int16_t *minPow);42int16_t ar9287GetNfAdjust(struct ath_hal *ah, const HAL_CHANNEL_INTERNAL *c);4344static void45ar9287WriteRegs(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_BOOL72ar9287SetChannel(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;7879OS_MARK(ah, AH_MARK_SETCHANNEL, chan->ic_freq);8081ar5416GetChannelCenters(ah, chan, ¢ers);82freq = centers.synth_center;8384reg32 = OS_REG_READ(ah, AR_PHY_SYNTH_CONTROL);85reg32 &= 0xc0000000;8687if (freq < 4800) { /* 2 GHz, fractional mode */88uint32_t txctl;89int regWrites = 0;9091bMode = 1;92fracMode = 1;93aModeRefSel = 0;94channelSel = (freq * 0x10000)/15;9596if (AR_SREV_KIWI_11_OR_LATER(ah)) {97if (freq == 2484) {98ath_hal_ini_write(ah,99&AH9287(ah)->ah_ini_cckFirJapan2484, 1,100regWrites);101} else {102ath_hal_ini_write(ah,103&AH9287(ah)->ah_ini_cckFirNormal, 1,104regWrites);105}106}107108txctl = OS_REG_READ(ah, AR_PHY_CCK_TX_CTRL);109if (freq == 2484) {110/* Enable channel spreading for channel 14 */111OS_REG_WRITE(ah, AR_PHY_CCK_TX_CTRL,112txctl | AR_PHY_CCK_TX_CTRL_JAPAN);113} else {114OS_REG_WRITE(ah, AR_PHY_CCK_TX_CTRL,115txctl &~ AR_PHY_CCK_TX_CTRL_JAPAN);116}117} else {118bMode = 0;119fracMode = 0;120121if ((freq % 20) == 0) {122aModeRefSel = 3;123} else if ((freq % 10) == 0) {124aModeRefSel = 2;125} else {126aModeRefSel = 0;127/*128* Enable 2G (fractional) mode for channels which129* are 5MHz spaced130*/131fracMode = 1;132refDivA = 1;133channelSel = (freq * 0x8000)/15;134135/* RefDivA setting */136OS_A_REG_RMW_FIELD(ah, AR_AN_SYNTH9,137AR_AN_SYNTH9_REFDIVA, refDivA);138}139if (!fracMode) {140ndiv = (freq * (refDivA >> aModeRefSel))/60;141channelSel = ndiv & 0x1ff;142channelFrac = (ndiv & 0xfffffe00) * 2;143channelSel = (channelSel << 17) | channelFrac;144}145}146147reg32 = reg32 | (bMode << 29) | (fracMode << 28) |148(aModeRefSel << 26) | (channelSel);149150OS_REG_WRITE(ah, AR_PHY_SYNTH_CONTROL, reg32);151152AH_PRIVATE(ah)->ah_curchan = chan;153154return AH_TRUE;155}156157/*158* Return a reference to the requested RF Bank.159*/160static uint32_t *161ar9287GetRfBank(struct ath_hal *ah, int bank)162{163HALDEBUG(ah, HAL_DEBUG_ANY, "%s: unknown RF Bank %d requested\n",164__func__, bank);165return AH_NULL;166}167168/*169* Reads EEPROM header info from device structure and programs170* all rf registers171*/172static HAL_BOOL173ar9287SetRfRegs(struct ath_hal *ah, const struct ieee80211_channel *chan,174uint16_t modesIndex, uint16_t *rfXpdGain)175{176return AH_TRUE; /* nothing to do */177}178179/*180* Read the transmit power levels from the structures taken from EEPROM181* Interpolate read transmit power values for this channel182* Organize the transmit power values into a table for writing into the hardware183*/184185static HAL_BOOL186ar9287SetPowerTable(struct ath_hal *ah, int16_t *pPowerMin, int16_t *pPowerMax,187const struct ieee80211_channel *chan, uint16_t *rfXpdGain)188{189return AH_TRUE;190}191192#if 0193static int16_t194ar9287GetMinPower(struct ath_hal *ah, EXPN_DATA_PER_CHANNEL_5112 *data)195{196int i, minIndex;197int16_t minGain,minPwr,minPcdac,retVal;198199/* Assume NUM_POINTS_XPD0 > 0 */200minGain = data->pDataPerXPD[0].xpd_gain;201for (minIndex=0,i=1; i<NUM_XPD_PER_CHANNEL; i++) {202if (data->pDataPerXPD[i].xpd_gain < minGain) {203minIndex = i;204minGain = data->pDataPerXPD[i].xpd_gain;205}206}207minPwr = data->pDataPerXPD[minIndex].pwr_t4[0];208minPcdac = data->pDataPerXPD[minIndex].pcdac[0];209for (i=1; i<NUM_POINTS_XPD0; i++) {210if (data->pDataPerXPD[minIndex].pwr_t4[i] < minPwr) {211minPwr = data->pDataPerXPD[minIndex].pwr_t4[i];212minPcdac = data->pDataPerXPD[minIndex].pcdac[i];213}214}215retVal = minPwr - (minPcdac*2);216return(retVal);217}218#endif219220static HAL_BOOL221ar9287GetChannelMaxMinPower(struct ath_hal *ah,222const struct ieee80211_channel *chan,223int16_t *maxPow, int16_t *minPow)224{225#if 0226struct ath_hal_5212 *ahp = AH5212(ah);227int numChannels=0,i,last;228int totalD, totalF,totalMin;229EXPN_DATA_PER_CHANNEL_5112 *data=AH_NULL;230EEPROM_POWER_EXPN_5112 *powerArray=AH_NULL;231232*maxPow = 0;233if (IS_CHAN_A(chan)) {234powerArray = ahp->ah_modePowerArray5112;235data = powerArray[headerInfo11A].pDataPerChannel;236numChannels = powerArray[headerInfo11A].numChannels;237} else if (IS_CHAN_G(chan) || IS_CHAN_108G(chan)) {238/* XXX - is this correct? Should we also use the same power for turbo G? */239powerArray = ahp->ah_modePowerArray5112;240data = powerArray[headerInfo11G].pDataPerChannel;241numChannels = powerArray[headerInfo11G].numChannels;242} else if (IS_CHAN_B(chan)) {243powerArray = ahp->ah_modePowerArray5112;244data = powerArray[headerInfo11B].pDataPerChannel;245numChannels = powerArray[headerInfo11B].numChannels;246} else {247return (AH_TRUE);248}249/* Make sure the channel is in the range of the TP values250* (freq piers)251*/252if ((numChannels < 1) ||253(chan->channel < data[0].channelValue) ||254(chan->channel > data[numChannels-1].channelValue))255return(AH_FALSE);256257/* Linearly interpolate the power value now */258for (last=0,i=0;259(i<numChannels) && (chan->channel > data[i].channelValue);260last=i++);261totalD = data[i].channelValue - data[last].channelValue;262if (totalD > 0) {263totalF = data[i].maxPower_t4 - data[last].maxPower_t4;264*maxPow = (int8_t) ((totalF*(chan->channel-data[last].channelValue) + data[last].maxPower_t4*totalD)/totalD);265266totalMin = ar9287GetMinPower(ah,&data[i]) - ar9287GetMinPower(ah, &data[last]);267*minPow = (int8_t) ((totalMin*(chan->channel-data[last].channelValue) + ar9287GetMinPower(ah, &data[last])*totalD)/totalD);268return (AH_TRUE);269} else {270if (chan->channel == data[i].channelValue) {271*maxPow = data[i].maxPower_t4;272*minPow = ar9287GetMinPower(ah, &data[i]);273return(AH_TRUE);274} else275return(AH_FALSE);276}277#else278*maxPow = *minPow = 0;279return AH_FALSE;280#endif281}282283/*284* The ordering of nfarray is thus:285*286* nfarray[0]: Chain 0 ctl287* nfarray[1]: Chain 1 ctl288* nfarray[2]: Chain 2 ctl289* nfarray[3]: Chain 0 ext290* nfarray[4]: Chain 1 ext291* nfarray[5]: Chain 2 ext292*/293static void294ar9287GetNoiseFloor(struct ath_hal *ah, int16_t nfarray[])295{296int16_t nf;297298nf = MS(OS_REG_READ(ah, AR_PHY_CCA), AR9280_PHY_MINCCA_PWR);299if (nf & 0x100)300nf = 0 - ((nf ^ 0x1ff) + 1);301HALDEBUG(ah, HAL_DEBUG_NFCAL,302"NF calibrated [ctl] [chain 0] is %d\n", nf);303nfarray[0] = nf;304305nf = MS(OS_REG_READ(ah, AR_PHY_CH1_CCA), AR9280_PHY_CH1_MINCCA_PWR);306if (nf & 0x100)307nf = 0 - ((nf ^ 0x1ff) + 1);308HALDEBUG(ah, HAL_DEBUG_NFCAL,309"NF calibrated [ctl] [chain 1] is %d\n", nf);310nfarray[1] = nf;311312nf = MS(OS_REG_READ(ah, AR_PHY_EXT_CCA), AR9280_PHY_EXT_MINCCA_PWR);313if (nf & 0x100)314nf = 0 - ((nf ^ 0x1ff) + 1);315HALDEBUG(ah, HAL_DEBUG_NFCAL,316"NF calibrated [ext] [chain 0] is %d\n", nf);317nfarray[3] = nf;318319nf = MS(OS_REG_READ(ah, AR_PHY_CH1_EXT_CCA), AR9280_PHY_CH1_EXT_MINCCA_PWR);320if (nf & 0x100)321nf = 0 - ((nf ^ 0x1ff) + 1);322HALDEBUG(ah, HAL_DEBUG_NFCAL,323"NF calibrated [ext] [chain 1] is %d\n", nf);324nfarray[4] = nf;325326/* Chain 2 - invalid */327nfarray[2] = 0;328nfarray[5] = 0;329330}331332/*333* Adjust NF based on statistical values for 5GHz frequencies.334* Stubbed:Not used by Fowl335*/336int16_t337ar9287GetNfAdjust(struct ath_hal *ah, const HAL_CHANNEL_INTERNAL *c)338{339return 0;340}341342/*343* Free memory for analog bank scratch buffers344*/345static void346ar9287RfDetach(struct ath_hal *ah)347{348struct ath_hal_5212 *ahp = AH5212(ah);349350HALASSERT(ahp->ah_rfHal != AH_NULL);351ath_hal_free(ahp->ah_rfHal);352ahp->ah_rfHal = AH_NULL;353}354355HAL_BOOL356ar9287RfAttach(struct ath_hal *ah, HAL_STATUS *status)357{358struct ath_hal_5212 *ahp = AH5212(ah);359struct ar9287State *priv;360361HALDEBUG(ah, HAL_DEBUG_ATTACH, "%s: attach AR9280 radio\n", __func__);362363HALASSERT(ahp->ah_rfHal == AH_NULL);364priv = ath_hal_malloc(sizeof(struct ar9287State));365if (priv == AH_NULL) {366HALDEBUG(ah, HAL_DEBUG_ANY,367"%s: cannot allocate private state\n", __func__);368*status = HAL_ENOMEM; /* XXX */369return AH_FALSE;370}371priv->base.rfDetach = ar9287RfDetach;372priv->base.writeRegs = ar9287WriteRegs;373priv->base.getRfBank = ar9287GetRfBank;374priv->base.setChannel = ar9287SetChannel;375priv->base.setRfRegs = ar9287SetRfRegs;376priv->base.setPowerTable = ar9287SetPowerTable;377priv->base.getChannelMaxMinPower = ar9287GetChannelMaxMinPower;378priv->base.getNfAdjust = ar9287GetNfAdjust;379380ahp->ah_pcdacTable = priv->pcdacTable;381ahp->ah_pcdacTableSize = sizeof(priv->pcdacTable);382ahp->ah_rfHal = &priv->base;383/*384* Set noise floor adjust method; we arrange a385* direct call instead of thunking.386*/387AH_PRIVATE(ah)->ah_getNfAdjust = priv->base.getNfAdjust;388AH_PRIVATE(ah)->ah_getNoiseFloor = ar9287GetNoiseFloor;389390return AH_TRUE;391}392393static HAL_BOOL394ar9287RfProbe(struct ath_hal *ah)395{396return (AR_SREV_KIWI(ah));397}398399AH_RF(RF9287, ar9287RfProbe, ar9287RfAttach);400401402