1185377Ssam/* 2187831Ssam * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting 3185377Ssam * Copyright (c) 2002-2008 Atheros Communications, Inc. 4185377Ssam * 5185377Ssam * Permission to use, copy, modify, and/or distribute this software for any 6185377Ssam * purpose with or without fee is hereby granted, provided that the above 7185377Ssam * copyright notice and this permission notice appear in all copies. 8185377Ssam * 9185377Ssam * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10185377Ssam * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11185377Ssam * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12185377Ssam * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13185377Ssam * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14185377Ssam * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15185377Ssam * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16185377Ssam * 17187831Ssam * $FreeBSD: releng/10.2/sys/dev/ath/ath_hal/ar5212/ar2413.c 187831 2009-01-28 18:00:22Z sam $ 18185377Ssam */ 19185377Ssam#include "opt_ah.h" 20185377Ssam 21185377Ssam#include "ah.h" 22185377Ssam#include "ah_internal.h" 23185377Ssam 24185377Ssam#include "ar5212/ar5212.h" 25185377Ssam#include "ar5212/ar5212reg.h" 26185377Ssam#include "ar5212/ar5212phy.h" 27185377Ssam 28185377Ssam#include "ah_eeprom_v3.h" 29185377Ssam 30185377Ssam#define AH_5212_2413 31185377Ssam#include "ar5212/ar5212.ini" 32185377Ssam 33185377Ssam#define N(a) (sizeof(a)/sizeof(a[0])) 34185377Ssam 35185377Ssamstruct ar2413State { 36185377Ssam RF_HAL_FUNCS base; /* public state, must be first */ 37185377Ssam uint16_t pcdacTable[PWR_TABLE_SIZE_2413]; 38185377Ssam 39185377Ssam uint32_t Bank1Data[N(ar5212Bank1_2413)]; 40185377Ssam uint32_t Bank2Data[N(ar5212Bank2_2413)]; 41185377Ssam uint32_t Bank3Data[N(ar5212Bank3_2413)]; 42185377Ssam uint32_t Bank6Data[N(ar5212Bank6_2413)]; 43185377Ssam uint32_t Bank7Data[N(ar5212Bank7_2413)]; 44185377Ssam 45185377Ssam /* 46185377Ssam * Private state for reduced stack usage. 47185377Ssam */ 48185377Ssam /* filled out Vpd table for all pdGains (chanL) */ 49185377Ssam uint16_t vpdTable_L[MAX_NUM_PDGAINS_PER_CHANNEL] 50185377Ssam [MAX_PWR_RANGE_IN_HALF_DB]; 51185377Ssam /* filled out Vpd table for all pdGains (chanR) */ 52185377Ssam uint16_t vpdTable_R[MAX_NUM_PDGAINS_PER_CHANNEL] 53185377Ssam [MAX_PWR_RANGE_IN_HALF_DB]; 54185377Ssam /* filled out Vpd table for all pdGains (interpolated) */ 55185377Ssam uint16_t vpdTable_I[MAX_NUM_PDGAINS_PER_CHANNEL] 56185377Ssam [MAX_PWR_RANGE_IN_HALF_DB]; 57185377Ssam}; 58185377Ssam#define AR2413(ah) ((struct ar2413State *) AH5212(ah)->ah_rfHal) 59185377Ssam 60185377Ssamextern void ar5212ModifyRfBuffer(uint32_t *rfBuf, uint32_t reg32, 61185377Ssam uint32_t numBits, uint32_t firstBit, uint32_t column); 62185377Ssam 63185377Ssamstatic void 64185377Ssamar2413WriteRegs(struct ath_hal *ah, u_int modesIndex, u_int freqIndex, 65185377Ssam int writes) 66185377Ssam{ 67185377Ssam HAL_INI_WRITE_ARRAY(ah, ar5212Modes_2413, modesIndex, writes); 68185377Ssam HAL_INI_WRITE_ARRAY(ah, ar5212Common_2413, 1, writes); 69185377Ssam HAL_INI_WRITE_ARRAY(ah, ar5212BB_RfGain_2413, freqIndex, writes); 70185377Ssam} 71185377Ssam 72185377Ssam/* 73185377Ssam * Take the MHz channel value and set the Channel value 74185377Ssam * 75185377Ssam * ASSUMES: Writes enabled to analog bus 76185377Ssam */ 77185377Ssamstatic HAL_BOOL 78187831Ssamar2413SetChannel(struct ath_hal *ah, const struct ieee80211_channel *chan) 79185377Ssam{ 80187831Ssam uint16_t freq = ath_hal_gethwchannel(ah, chan); 81185377Ssam uint32_t channelSel = 0; 82185377Ssam uint32_t bModeSynth = 0; 83185377Ssam uint32_t aModeRefSel = 0; 84185377Ssam uint32_t reg32 = 0; 85185377Ssam 86187831Ssam OS_MARK(ah, AH_MARK_SETCHANNEL, freq); 87185377Ssam 88187831Ssam if (freq < 4800) { 89185377Ssam uint32_t txctl; 90185377Ssam 91187831Ssam if (((freq - 2192) % 5) == 0) { 92187831Ssam channelSel = ((freq - 672) * 2 - 3040)/10; 93185377Ssam bModeSynth = 0; 94187831Ssam } else if (((freq - 2224) % 5) == 0) { 95187831Ssam channelSel = ((freq - 704) * 2 - 3040) / 10; 96185377Ssam bModeSynth = 1; 97185377Ssam } else { 98185377Ssam HALDEBUG(ah, HAL_DEBUG_ANY, 99185377Ssam "%s: invalid channel %u MHz\n", 100187831Ssam __func__, freq); 101185377Ssam return AH_FALSE; 102185377Ssam } 103185377Ssam 104185377Ssam channelSel = (channelSel << 2) & 0xff; 105185377Ssam channelSel = ath_hal_reverseBits(channelSel, 8); 106185377Ssam 107185377Ssam txctl = OS_REG_READ(ah, AR_PHY_CCK_TX_CTRL); 108187831Ssam if (freq == 2484) { 109185377Ssam /* Enable channel spreading for channel 14 */ 110185377Ssam OS_REG_WRITE(ah, AR_PHY_CCK_TX_CTRL, 111185377Ssam txctl | AR_PHY_CCK_TX_CTRL_JAPAN); 112185377Ssam } else { 113185377Ssam OS_REG_WRITE(ah, AR_PHY_CCK_TX_CTRL, 114185377Ssam txctl &~ AR_PHY_CCK_TX_CTRL_JAPAN); 115185377Ssam } 116187831Ssam } else if (((freq % 5) == 2) && (freq <= 5435)) { 117187831Ssam freq = freq - 2; /* Align to even 5MHz raster */ 118185377Ssam channelSel = ath_hal_reverseBits( 119185377Ssam (uint32_t)(((freq - 4800)*10)/25 + 1), 8); 120185377Ssam aModeRefSel = ath_hal_reverseBits(0, 2); 121187831Ssam } else if ((freq % 20) == 0 && freq >= 5120) { 122185377Ssam channelSel = ath_hal_reverseBits( 123187831Ssam ((freq - 4800) / 20 << 2), 8); 124185377Ssam aModeRefSel = ath_hal_reverseBits(3, 2); 125187831Ssam } else if ((freq % 10) == 0) { 126185377Ssam channelSel = ath_hal_reverseBits( 127187831Ssam ((freq - 4800) / 10 << 1), 8); 128185377Ssam aModeRefSel = ath_hal_reverseBits(2, 2); 129187831Ssam } else if ((freq % 5) == 0) { 130185377Ssam channelSel = ath_hal_reverseBits( 131187831Ssam (freq - 4800) / 5, 8); 132185377Ssam aModeRefSel = ath_hal_reverseBits(1, 2); 133185377Ssam } else { 134185377Ssam HALDEBUG(ah, HAL_DEBUG_ANY, "%s: invalid channel %u MHz\n", 135187831Ssam __func__, freq); 136185377Ssam return AH_FALSE; 137185377Ssam } 138185377Ssam 139185377Ssam reg32 = (channelSel << 4) | (aModeRefSel << 2) | (bModeSynth << 1) | 140185377Ssam (1 << 12) | 0x1; 141185377Ssam OS_REG_WRITE(ah, AR_PHY(0x27), reg32 & 0xff); 142185377Ssam 143185377Ssam reg32 >>= 8; 144185377Ssam OS_REG_WRITE(ah, AR_PHY(0x36), reg32 & 0x7f); 145185377Ssam 146185377Ssam AH_PRIVATE(ah)->ah_curchan = chan; 147185377Ssam 148185377Ssam return AH_TRUE; 149185377Ssam} 150185377Ssam 151185377Ssam/* 152185377Ssam * Reads EEPROM header info from device structure and programs 153185377Ssam * all rf registers 154185377Ssam * 155185377Ssam * REQUIRES: Access to the analog rf device 156185377Ssam */ 157185377Ssamstatic HAL_BOOL 158187831Ssamar2413SetRfRegs(struct ath_hal *ah, 159187831Ssam const struct ieee80211_channel *chan, 160187831Ssam uint16_t modesIndex, uint16_t *rfXpdGain) 161185377Ssam{ 162185377Ssam#define RF_BANK_SETUP(_priv, _ix, _col) do { \ 163185377Ssam int i; \ 164185377Ssam for (i = 0; i < N(ar5212Bank##_ix##_2413); i++) \ 165185377Ssam (_priv)->Bank##_ix##Data[i] = ar5212Bank##_ix##_2413[i][_col];\ 166185377Ssam} while (0) 167185377Ssam struct ath_hal_5212 *ahp = AH5212(ah); 168185377Ssam const HAL_EEPROM *ee = AH_PRIVATE(ah)->ah_eeprom; 169185377Ssam uint16_t ob2GHz = 0, db2GHz = 0; 170185377Ssam struct ar2413State *priv = AR2413(ah); 171185377Ssam int regWrites = 0; 172185377Ssam 173187831Ssam HALDEBUG(ah, HAL_DEBUG_RFPARAM, "%s: chan %u/0x%x modesIndex %u\n", 174187831Ssam __func__, chan->ic_freq, chan->ic_flags, modesIndex); 175185377Ssam 176185377Ssam HALASSERT(priv); 177185377Ssam 178185377Ssam /* Setup rf parameters */ 179187831Ssam if (IEEE80211_IS_CHAN_B(chan)) { 180185377Ssam ob2GHz = ee->ee_obFor24; 181185377Ssam db2GHz = ee->ee_dbFor24; 182187831Ssam } else { 183185377Ssam ob2GHz = ee->ee_obFor24g; 184185377Ssam db2GHz = ee->ee_dbFor24g; 185185377Ssam } 186185377Ssam 187185377Ssam /* Bank 1 Write */ 188185377Ssam RF_BANK_SETUP(priv, 1, 1); 189185377Ssam 190185377Ssam /* Bank 2 Write */ 191185377Ssam RF_BANK_SETUP(priv, 2, modesIndex); 192185377Ssam 193185377Ssam /* Bank 3 Write */ 194185377Ssam RF_BANK_SETUP(priv, 3, modesIndex); 195185377Ssam 196185377Ssam /* Bank 6 Write */ 197185377Ssam RF_BANK_SETUP(priv, 6, modesIndex); 198185377Ssam 199185377Ssam ar5212ModifyRfBuffer(priv->Bank6Data, ob2GHz, 3, 168, 0); 200185377Ssam ar5212ModifyRfBuffer(priv->Bank6Data, db2GHz, 3, 165, 0); 201185377Ssam 202185377Ssam /* Bank 7 Setup */ 203185377Ssam RF_BANK_SETUP(priv, 7, modesIndex); 204185377Ssam 205185377Ssam /* Write Analog registers */ 206185377Ssam HAL_INI_WRITE_BANK(ah, ar5212Bank1_2413, priv->Bank1Data, regWrites); 207185377Ssam HAL_INI_WRITE_BANK(ah, ar5212Bank2_2413, priv->Bank2Data, regWrites); 208185377Ssam HAL_INI_WRITE_BANK(ah, ar5212Bank3_2413, priv->Bank3Data, regWrites); 209185377Ssam HAL_INI_WRITE_BANK(ah, ar5212Bank6_2413, priv->Bank6Data, regWrites); 210185377Ssam HAL_INI_WRITE_BANK(ah, ar5212Bank7_2413, priv->Bank7Data, regWrites); 211185377Ssam 212185377Ssam /* Now that we have reprogrammed rfgain value, clear the flag. */ 213185377Ssam ahp->ah_rfgainState = HAL_RFGAIN_INACTIVE; 214185377Ssam 215185377Ssam return AH_TRUE; 216185377Ssam#undef RF_BANK_SETUP 217185377Ssam} 218185377Ssam 219185377Ssam/* 220185377Ssam * Return a reference to the requested RF Bank. 221185377Ssam */ 222185377Ssamstatic uint32_t * 223185377Ssamar2413GetRfBank(struct ath_hal *ah, int bank) 224185377Ssam{ 225185377Ssam struct ar2413State *priv = AR2413(ah); 226185377Ssam 227185377Ssam HALASSERT(priv != AH_NULL); 228185377Ssam switch (bank) { 229185377Ssam case 1: return priv->Bank1Data; 230185377Ssam case 2: return priv->Bank2Data; 231185377Ssam case 3: return priv->Bank3Data; 232185377Ssam case 6: return priv->Bank6Data; 233185377Ssam case 7: return priv->Bank7Data; 234185377Ssam } 235185377Ssam HALDEBUG(ah, HAL_DEBUG_ANY, "%s: unknown RF Bank %d requested\n", 236185377Ssam __func__, bank); 237185377Ssam return AH_NULL; 238185377Ssam} 239185377Ssam 240185377Ssam/* 241185377Ssam * Return indices surrounding the value in sorted integer lists. 242185377Ssam * 243185377Ssam * NB: the input list is assumed to be sorted in ascending order 244185377Ssam */ 245185377Ssamstatic void 246185377SsamGetLowerUpperIndex(int16_t v, const uint16_t *lp, uint16_t listSize, 247185377Ssam uint32_t *vlo, uint32_t *vhi) 248185377Ssam{ 249185377Ssam int16_t target = v; 250185377Ssam const uint16_t *ep = lp+listSize; 251185377Ssam const uint16_t *tp; 252185377Ssam 253185377Ssam /* 254185377Ssam * Check first and last elements for out-of-bounds conditions. 255185377Ssam */ 256185377Ssam if (target < lp[0]) { 257185377Ssam *vlo = *vhi = 0; 258185377Ssam return; 259185377Ssam } 260185377Ssam if (target >= ep[-1]) { 261185377Ssam *vlo = *vhi = listSize - 1; 262185377Ssam return; 263185377Ssam } 264185377Ssam 265185377Ssam /* look for value being near or between 2 values in list */ 266185377Ssam for (tp = lp; tp < ep; tp++) { 267185377Ssam /* 268185377Ssam * If value is close to the current value of the list 269185377Ssam * then target is not between values, it is one of the values 270185377Ssam */ 271185377Ssam if (*tp == target) { 272185377Ssam *vlo = *vhi = tp - (const uint16_t *) lp; 273185377Ssam return; 274185377Ssam } 275185377Ssam /* 276185377Ssam * Look for value being between current value and next value 277185377Ssam * if so return these 2 values 278185377Ssam */ 279185377Ssam if (target < tp[1]) { 280185377Ssam *vlo = tp - (const uint16_t *) lp; 281185377Ssam *vhi = *vlo + 1; 282185377Ssam return; 283185377Ssam } 284185377Ssam } 285185377Ssam} 286185377Ssam 287185377Ssam/* 288185377Ssam * Fill the Vpdlist for indices Pmax-Pmin 289185377Ssam */ 290185377Ssamstatic HAL_BOOL 291185377Ssamar2413FillVpdTable(uint32_t pdGainIdx, int16_t Pmin, int16_t Pmax, 292185377Ssam const int16_t *pwrList, const uint16_t *VpdList, 293185377Ssam uint16_t numIntercepts, uint16_t retVpdList[][64]) 294185377Ssam{ 295185377Ssam uint16_t ii, jj, kk; 296185377Ssam int16_t currPwr = (int16_t)(2*Pmin); 297185377Ssam /* since Pmin is pwr*2 and pwrList is 4*pwr */ 298185377Ssam uint32_t idxL, idxR; 299185377Ssam 300185377Ssam ii = 0; 301185377Ssam jj = 0; 302185377Ssam 303185377Ssam if (numIntercepts < 2) 304185377Ssam return AH_FALSE; 305185377Ssam 306185377Ssam while (ii <= (uint16_t)(Pmax - Pmin)) { 307185377Ssam GetLowerUpperIndex(currPwr, (const uint16_t *) pwrList, 308185377Ssam numIntercepts, &(idxL), &(idxR)); 309185377Ssam if (idxR < 1) 310185377Ssam idxR = 1; /* extrapolate below */ 311185377Ssam if (idxL == (uint32_t)(numIntercepts - 1)) 312185377Ssam idxL = numIntercepts - 2; /* extrapolate above */ 313185377Ssam if (pwrList[idxL] == pwrList[idxR]) 314185377Ssam kk = VpdList[idxL]; 315185377Ssam else 316185377Ssam kk = (uint16_t) 317185377Ssam (((currPwr - pwrList[idxL])*VpdList[idxR]+ 318185377Ssam (pwrList[idxR] - currPwr)*VpdList[idxL])/ 319185377Ssam (pwrList[idxR] - pwrList[idxL])); 320185377Ssam retVpdList[pdGainIdx][ii] = kk; 321185377Ssam ii++; 322185377Ssam currPwr += 2; /* half dB steps */ 323185377Ssam } 324185377Ssam 325185377Ssam return AH_TRUE; 326185377Ssam} 327185377Ssam 328185377Ssam/* 329185377Ssam * Returns interpolated or the scaled up interpolated value 330185377Ssam */ 331185377Ssamstatic int16_t 332185377Ssaminterpolate_signed(uint16_t target, uint16_t srcLeft, uint16_t srcRight, 333185377Ssam int16_t targetLeft, int16_t targetRight) 334185377Ssam{ 335185377Ssam int16_t rv; 336185377Ssam 337185377Ssam if (srcRight != srcLeft) { 338185377Ssam rv = ((target - srcLeft)*targetRight + 339185377Ssam (srcRight - target)*targetLeft) / (srcRight - srcLeft); 340185377Ssam } else { 341185377Ssam rv = targetLeft; 342185377Ssam } 343185377Ssam return rv; 344185377Ssam} 345185377Ssam 346185377Ssam/* 347185377Ssam * Uses the data points read from EEPROM to reconstruct the pdadc power table 348185377Ssam * Called by ar2413SetPowerTable() 349185377Ssam */ 350185377Ssamstatic int 351185377Ssamar2413getGainBoundariesAndPdadcsForPowers(struct ath_hal *ah, uint16_t channel, 352185377Ssam const RAW_DATA_STRUCT_2413 *pRawDataset, 353185377Ssam uint16_t pdGainOverlap_t2, 354185377Ssam int16_t *pMinCalPower, uint16_t pPdGainBoundaries[], 355185377Ssam uint16_t pPdGainValues[], uint16_t pPDADCValues[]) 356185377Ssam{ 357185377Ssam struct ar2413State *priv = AR2413(ah); 358185377Ssam#define VpdTable_L priv->vpdTable_L 359185377Ssam#define VpdTable_R priv->vpdTable_R 360185377Ssam#define VpdTable_I priv->vpdTable_I 361185377Ssam uint32_t ii, jj, kk; 362185377Ssam int32_t ss;/* potentially -ve index for taking care of pdGainOverlap */ 363185377Ssam uint32_t idxL, idxR; 364185377Ssam uint32_t numPdGainsUsed = 0; 365185377Ssam /* 366185377Ssam * If desired to support -ve power levels in future, just 367185377Ssam * change pwr_I_0 to signed 5-bits. 368185377Ssam */ 369185377Ssam int16_t Pmin_t2[MAX_NUM_PDGAINS_PER_CHANNEL]; 370185377Ssam /* to accomodate -ve power levels later on. */ 371185377Ssam int16_t Pmax_t2[MAX_NUM_PDGAINS_PER_CHANNEL]; 372185377Ssam /* to accomodate -ve power levels later on */ 373185377Ssam uint16_t numVpd = 0; 374185377Ssam uint16_t Vpd_step; 375185377Ssam int16_t tmpVal ; 376185377Ssam uint32_t sizeCurrVpdTable, maxIndex, tgtIndex; 377185377Ssam 378185377Ssam /* Get upper lower index */ 379185377Ssam GetLowerUpperIndex(channel, pRawDataset->pChannels, 380185377Ssam pRawDataset->numChannels, &(idxL), &(idxR)); 381185377Ssam 382185377Ssam for (ii = 0; ii < MAX_NUM_PDGAINS_PER_CHANNEL; ii++) { 383185377Ssam jj = MAX_NUM_PDGAINS_PER_CHANNEL - ii - 1; 384185377Ssam /* work backwards 'cause highest pdGain for lowest power */ 385185377Ssam numVpd = pRawDataset->pDataPerChannel[idxL].pDataPerPDGain[jj].numVpd; 386185377Ssam if (numVpd > 0) { 387185377Ssam pPdGainValues[numPdGainsUsed] = pRawDataset->pDataPerChannel[idxL].pDataPerPDGain[jj].pd_gain; 388185377Ssam Pmin_t2[numPdGainsUsed] = pRawDataset->pDataPerChannel[idxL].pDataPerPDGain[jj].pwr_t4[0]; 389185377Ssam if (Pmin_t2[numPdGainsUsed] >pRawDataset->pDataPerChannel[idxR].pDataPerPDGain[jj].pwr_t4[0]) { 390185377Ssam Pmin_t2[numPdGainsUsed] = pRawDataset->pDataPerChannel[idxR].pDataPerPDGain[jj].pwr_t4[0]; 391185377Ssam } 392185377Ssam Pmin_t2[numPdGainsUsed] = (int16_t) 393185377Ssam (Pmin_t2[numPdGainsUsed] / 2); 394185377Ssam Pmax_t2[numPdGainsUsed] = pRawDataset->pDataPerChannel[idxL].pDataPerPDGain[jj].pwr_t4[numVpd-1]; 395185377Ssam if (Pmax_t2[numPdGainsUsed] > pRawDataset->pDataPerChannel[idxR].pDataPerPDGain[jj].pwr_t4[numVpd-1]) 396185377Ssam Pmax_t2[numPdGainsUsed] = 397185377Ssam pRawDataset->pDataPerChannel[idxR].pDataPerPDGain[jj].pwr_t4[numVpd-1]; 398185377Ssam Pmax_t2[numPdGainsUsed] = (int16_t)(Pmax_t2[numPdGainsUsed] / 2); 399185377Ssam ar2413FillVpdTable( 400185377Ssam numPdGainsUsed, Pmin_t2[numPdGainsUsed], Pmax_t2[numPdGainsUsed], 401185377Ssam &(pRawDataset->pDataPerChannel[idxL].pDataPerPDGain[jj].pwr_t4[0]), 402185377Ssam &(pRawDataset->pDataPerChannel[idxL].pDataPerPDGain[jj].Vpd[0]), numVpd, VpdTable_L 403185377Ssam ); 404185377Ssam ar2413FillVpdTable( 405185377Ssam numPdGainsUsed, Pmin_t2[numPdGainsUsed], Pmax_t2[numPdGainsUsed], 406185377Ssam &(pRawDataset->pDataPerChannel[idxR].pDataPerPDGain[jj].pwr_t4[0]), 407185377Ssam &(pRawDataset->pDataPerChannel[idxR].pDataPerPDGain[jj].Vpd[0]), numVpd, VpdTable_R 408185377Ssam ); 409185377Ssam for (kk = 0; kk < (uint16_t)(Pmax_t2[numPdGainsUsed] - Pmin_t2[numPdGainsUsed]); kk++) { 410185377Ssam VpdTable_I[numPdGainsUsed][kk] = 411185377Ssam interpolate_signed( 412185377Ssam channel, pRawDataset->pChannels[idxL], pRawDataset->pChannels[idxR], 413185377Ssam (int16_t)VpdTable_L[numPdGainsUsed][kk], (int16_t)VpdTable_R[numPdGainsUsed][kk]); 414185377Ssam } 415185377Ssam /* fill VpdTable_I for this pdGain */ 416185377Ssam numPdGainsUsed++; 417185377Ssam } 418185377Ssam /* if this pdGain is used */ 419185377Ssam } 420185377Ssam 421185377Ssam *pMinCalPower = Pmin_t2[0]; 422185377Ssam kk = 0; /* index for the final table */ 423185377Ssam for (ii = 0; ii < numPdGainsUsed; ii++) { 424185377Ssam if (ii == (numPdGainsUsed - 1)) 425185377Ssam pPdGainBoundaries[ii] = Pmax_t2[ii] + 426185377Ssam PD_GAIN_BOUNDARY_STRETCH_IN_HALF_DB; 427185377Ssam else 428185377Ssam pPdGainBoundaries[ii] = (uint16_t) 429185377Ssam ((Pmax_t2[ii] + Pmin_t2[ii+1]) / 2 ); 430185377Ssam if (pPdGainBoundaries[ii] > 63) { 431185377Ssam HALDEBUG(ah, HAL_DEBUG_ANY, 432185377Ssam "%s: clamp pPdGainBoundaries[%d] %d\n", 433185377Ssam __func__, ii, pPdGainBoundaries[ii]);/*XXX*/ 434185377Ssam pPdGainBoundaries[ii] = 63; 435185377Ssam } 436185377Ssam 437185377Ssam /* Find starting index for this pdGain */ 438185377Ssam if (ii == 0) 439185377Ssam ss = 0; /* for the first pdGain, start from index 0 */ 440185377Ssam else 441185377Ssam ss = (pPdGainBoundaries[ii-1] - Pmin_t2[ii]) - 442185377Ssam pdGainOverlap_t2; 443185377Ssam Vpd_step = (uint16_t)(VpdTable_I[ii][1] - VpdTable_I[ii][0]); 444185377Ssam Vpd_step = (uint16_t)((Vpd_step < 1) ? 1 : Vpd_step); 445185377Ssam /* 446185377Ssam *-ve ss indicates need to extrapolate data below for this pdGain 447185377Ssam */ 448185377Ssam while (ss < 0) { 449185377Ssam tmpVal = (int16_t)(VpdTable_I[ii][0] + ss*Vpd_step); 450185377Ssam pPDADCValues[kk++] = (uint16_t)((tmpVal < 0) ? 0 : tmpVal); 451185377Ssam ss++; 452185377Ssam } 453185377Ssam 454185377Ssam sizeCurrVpdTable = Pmax_t2[ii] - Pmin_t2[ii]; 455185377Ssam tgtIndex = pPdGainBoundaries[ii] + pdGainOverlap_t2 - Pmin_t2[ii]; 456185377Ssam maxIndex = (tgtIndex < sizeCurrVpdTable) ? tgtIndex : sizeCurrVpdTable; 457185377Ssam 458185377Ssam while (ss < (int16_t)maxIndex) 459185377Ssam pPDADCValues[kk++] = VpdTable_I[ii][ss++]; 460185377Ssam 461185377Ssam Vpd_step = (uint16_t)(VpdTable_I[ii][sizeCurrVpdTable-1] - 462185377Ssam VpdTable_I[ii][sizeCurrVpdTable-2]); 463185377Ssam Vpd_step = (uint16_t)((Vpd_step < 1) ? 1 : Vpd_step); 464185377Ssam /* 465185377Ssam * for last gain, pdGainBoundary == Pmax_t2, so will 466185377Ssam * have to extrapolate 467185377Ssam */ 468185377Ssam if (tgtIndex > maxIndex) { /* need to extrapolate above */ 469185377Ssam while(ss < (int16_t)tgtIndex) { 470185377Ssam tmpVal = (uint16_t) 471185377Ssam (VpdTable_I[ii][sizeCurrVpdTable-1] + 472185377Ssam (ss-maxIndex)*Vpd_step); 473185377Ssam pPDADCValues[kk++] = (tmpVal > 127) ? 474185377Ssam 127 : tmpVal; 475185377Ssam ss++; 476185377Ssam } 477185377Ssam } /* extrapolated above */ 478185377Ssam } /* for all pdGainUsed */ 479185377Ssam 480185377Ssam while (ii < MAX_NUM_PDGAINS_PER_CHANNEL) { 481185377Ssam pPdGainBoundaries[ii] = pPdGainBoundaries[ii-1]; 482185377Ssam ii++; 483185377Ssam } 484185377Ssam while (kk < 128) { 485185377Ssam pPDADCValues[kk] = pPDADCValues[kk-1]; 486185377Ssam kk++; 487185377Ssam } 488185377Ssam 489185377Ssam return numPdGainsUsed; 490185377Ssam#undef VpdTable_L 491185377Ssam#undef VpdTable_R 492185377Ssam#undef VpdTable_I 493185377Ssam} 494185377Ssam 495185377Ssamstatic HAL_BOOL 496185377Ssamar2413SetPowerTable(struct ath_hal *ah, 497187831Ssam int16_t *minPower, int16_t *maxPower, 498187831Ssam const struct ieee80211_channel *chan, 499185377Ssam uint16_t *rfXpdGain) 500185377Ssam{ 501187831Ssam uint16_t freq = ath_hal_gethwchannel(ah, chan); 502185377Ssam struct ath_hal_5212 *ahp = AH5212(ah); 503185377Ssam const HAL_EEPROM *ee = AH_PRIVATE(ah)->ah_eeprom; 504185377Ssam const RAW_DATA_STRUCT_2413 *pRawDataset = AH_NULL; 505185377Ssam uint16_t pdGainOverlap_t2; 506185377Ssam int16_t minCalPower2413_t2; 507185377Ssam uint16_t *pdadcValues = ahp->ah_pcdacTable; 508185377Ssam uint16_t gainBoundaries[4]; 509185380Ssam uint32_t reg32, regoffset; 510185380Ssam int i, numPdGainsUsed; 511185380Ssam#ifndef AH_USE_INIPDGAIN 512185380Ssam uint32_t tpcrg1; 513185380Ssam#endif 514185377Ssam 515185377Ssam HALDEBUG(ah, HAL_DEBUG_RFPARAM, "%s: chan 0x%x flag 0x%x\n", 516187831Ssam __func__, freq, chan->ic_flags); 517185377Ssam 518187831Ssam if (IEEE80211_IS_CHAN_G(chan) || IEEE80211_IS_CHAN_108G(chan)) 519185377Ssam pRawDataset = &ee->ee_rawDataset2413[headerInfo11G]; 520187831Ssam else if (IEEE80211_IS_CHAN_B(chan)) 521185377Ssam pRawDataset = &ee->ee_rawDataset2413[headerInfo11B]; 522185377Ssam else { 523185377Ssam HALDEBUG(ah, HAL_DEBUG_ANY, "%s: illegal mode\n", __func__); 524185377Ssam return AH_FALSE; 525185377Ssam } 526185377Ssam 527185377Ssam pdGainOverlap_t2 = (uint16_t) SM(OS_REG_READ(ah, AR_PHY_TPCRG5), 528185377Ssam AR_PHY_TPCRG5_PD_GAIN_OVERLAP); 529185377Ssam 530185377Ssam numPdGainsUsed = ar2413getGainBoundariesAndPdadcsForPowers(ah, 531187831Ssam freq, pRawDataset, pdGainOverlap_t2, 532185377Ssam &minCalPower2413_t2,gainBoundaries, rfXpdGain, pdadcValues); 533185377Ssam HALASSERT(1 <= numPdGainsUsed && numPdGainsUsed <= 3); 534185377Ssam 535185380Ssam#ifdef AH_USE_INIPDGAIN 536185380Ssam /* 537185380Ssam * Use pd_gains curve from eeprom; Atheros always uses 538185380Ssam * the default curve from the ini file but some vendors 539185380Ssam * (e.g. Zcomax) want to override this curve and not 540185380Ssam * honoring their settings results in tx power 5dBm low. 541185380Ssam */ 542185377Ssam OS_REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_NUM_PD_GAIN, 543185377Ssam (pRawDataset->pDataPerChannel[0].numPdGains - 1)); 544185380Ssam#else 545185377Ssam tpcrg1 = OS_REG_READ(ah, AR_PHY_TPCRG1); 546185377Ssam tpcrg1 = (tpcrg1 &~ AR_PHY_TPCRG1_NUM_PD_GAIN) 547185377Ssam | SM(numPdGainsUsed-1, AR_PHY_TPCRG1_NUM_PD_GAIN); 548185377Ssam switch (numPdGainsUsed) { 549185377Ssam case 3: 550185377Ssam tpcrg1 &= ~AR_PHY_TPCRG1_PDGAIN_SETTING3; 551185377Ssam tpcrg1 |= SM(rfXpdGain[2], AR_PHY_TPCRG1_PDGAIN_SETTING3); 552185377Ssam /* fall thru... */ 553185377Ssam case 2: 554185377Ssam tpcrg1 &= ~AR_PHY_TPCRG1_PDGAIN_SETTING2; 555185377Ssam tpcrg1 |= SM(rfXpdGain[1], AR_PHY_TPCRG1_PDGAIN_SETTING2); 556185377Ssam /* fall thru... */ 557185377Ssam case 1: 558185377Ssam tpcrg1 &= ~AR_PHY_TPCRG1_PDGAIN_SETTING1; 559185377Ssam tpcrg1 |= SM(rfXpdGain[0], AR_PHY_TPCRG1_PDGAIN_SETTING1); 560185377Ssam break; 561185377Ssam } 562185377Ssam#ifdef AH_DEBUG 563185377Ssam if (tpcrg1 != OS_REG_READ(ah, AR_PHY_TPCRG1)) 564185377Ssam HALDEBUG(ah, HAL_DEBUG_RFPARAM, "%s: using non-default " 565185377Ssam "pd_gains (default 0x%x, calculated 0x%x)\n", 566185377Ssam __func__, OS_REG_READ(ah, AR_PHY_TPCRG1), tpcrg1); 567185377Ssam#endif 568185377Ssam OS_REG_WRITE(ah, AR_PHY_TPCRG1, tpcrg1); 569185380Ssam#endif 570185377Ssam 571185377Ssam /* 572185377Ssam * Note the pdadc table may not start at 0 dBm power, could be 573185377Ssam * negative or greater than 0. Need to offset the power 574185377Ssam * values by the amount of minPower for griffin 575185377Ssam */ 576185377Ssam if (minCalPower2413_t2 != 0) 577185377Ssam ahp->ah_txPowerIndexOffset = (int16_t)(0 - minCalPower2413_t2); 578185377Ssam else 579185377Ssam ahp->ah_txPowerIndexOffset = 0; 580185377Ssam 581185377Ssam /* Finally, write the power values into the baseband power table */ 582185377Ssam regoffset = 0x9800 + (672 <<2); /* beginning of pdadc table in griffin */ 583185377Ssam for (i = 0; i < 32; i++) { 584185377Ssam reg32 = ((pdadcValues[4*i + 0] & 0xFF) << 0) | 585185377Ssam ((pdadcValues[4*i + 1] & 0xFF) << 8) | 586185377Ssam ((pdadcValues[4*i + 2] & 0xFF) << 16) | 587185377Ssam ((pdadcValues[4*i + 3] & 0xFF) << 24) ; 588185377Ssam OS_REG_WRITE(ah, regoffset, reg32); 589185377Ssam regoffset += 4; 590185377Ssam } 591185377Ssam 592185377Ssam OS_REG_WRITE(ah, AR_PHY_TPCRG5, 593185377Ssam SM(pdGainOverlap_t2, AR_PHY_TPCRG5_PD_GAIN_OVERLAP) | 594185377Ssam SM(gainBoundaries[0], AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_1) | 595185377Ssam SM(gainBoundaries[1], AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_2) | 596185377Ssam SM(gainBoundaries[2], AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_3) | 597185377Ssam SM(gainBoundaries[3], AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_4)); 598185377Ssam 599185377Ssam return AH_TRUE; 600185377Ssam} 601185377Ssam 602185377Ssamstatic int16_t 603185377Ssamar2413GetMinPower(struct ath_hal *ah, const RAW_DATA_PER_CHANNEL_2413 *data) 604185377Ssam{ 605185377Ssam uint32_t ii,jj; 606185377Ssam uint16_t Pmin=0,numVpd; 607185377Ssam 608185377Ssam for (ii = 0; ii < MAX_NUM_PDGAINS_PER_CHANNEL; ii++) { 609185377Ssam jj = MAX_NUM_PDGAINS_PER_CHANNEL - ii - 1; 610185377Ssam /* work backwards 'cause highest pdGain for lowest power */ 611185377Ssam numVpd = data->pDataPerPDGain[jj].numVpd; 612185377Ssam if (numVpd > 0) { 613185377Ssam Pmin = data->pDataPerPDGain[jj].pwr_t4[0]; 614185377Ssam return(Pmin); 615185377Ssam } 616185377Ssam } 617185377Ssam return(Pmin); 618185377Ssam} 619185377Ssam 620185377Ssamstatic int16_t 621185377Ssamar2413GetMaxPower(struct ath_hal *ah, const RAW_DATA_PER_CHANNEL_2413 *data) 622185377Ssam{ 623185377Ssam uint32_t ii; 624185377Ssam uint16_t Pmax=0,numVpd; 625185377Ssam 626185377Ssam for (ii=0; ii< MAX_NUM_PDGAINS_PER_CHANNEL; ii++) { 627185377Ssam /* work forwards cuase lowest pdGain for highest power */ 628185377Ssam numVpd = data->pDataPerPDGain[ii].numVpd; 629185377Ssam if (numVpd > 0) { 630185377Ssam Pmax = data->pDataPerPDGain[ii].pwr_t4[numVpd-1]; 631185377Ssam return(Pmax); 632185377Ssam } 633185377Ssam } 634185377Ssam return(Pmax); 635185377Ssam} 636185377Ssam 637185377Ssamstatic HAL_BOOL 638187831Ssamar2413GetChannelMaxMinPower(struct ath_hal *ah, 639187831Ssam const struct ieee80211_channel *chan, 640185377Ssam int16_t *maxPow, int16_t *minPow) 641185377Ssam{ 642187831Ssam uint16_t freq = chan->ic_freq; /* NB: never mapped */ 643185377Ssam const HAL_EEPROM *ee = AH_PRIVATE(ah)->ah_eeprom; 644185377Ssam const RAW_DATA_STRUCT_2413 *pRawDataset = AH_NULL; 645185377Ssam const RAW_DATA_PER_CHANNEL_2413 *data = AH_NULL; 646185377Ssam uint16_t numChannels; 647185377Ssam int totalD,totalF, totalMin,last, i; 648185377Ssam 649185377Ssam *maxPow = 0; 650185377Ssam 651187831Ssam if (IEEE80211_IS_CHAN_G(chan) || IEEE80211_IS_CHAN_108G(chan)) 652185377Ssam pRawDataset = &ee->ee_rawDataset2413[headerInfo11G]; 653187831Ssam else if (IEEE80211_IS_CHAN_B(chan)) 654185377Ssam pRawDataset = &ee->ee_rawDataset2413[headerInfo11B]; 655185377Ssam else 656185377Ssam return(AH_FALSE); 657185377Ssam 658185377Ssam numChannels = pRawDataset->numChannels; 659185377Ssam data = pRawDataset->pDataPerChannel; 660185377Ssam 661185377Ssam /* Make sure the channel is in the range of the TP values 662185377Ssam * (freq piers) 663185377Ssam */ 664185377Ssam if (numChannels < 1) 665185377Ssam return(AH_FALSE); 666185377Ssam 667187831Ssam if ((freq < data[0].channelValue) || 668187831Ssam (freq > data[numChannels-1].channelValue)) { 669187831Ssam if (freq < data[0].channelValue) { 670185377Ssam *maxPow = ar2413GetMaxPower(ah, &data[0]); 671185377Ssam *minPow = ar2413GetMinPower(ah, &data[0]); 672185377Ssam return(AH_TRUE); 673185377Ssam } else { 674185377Ssam *maxPow = ar2413GetMaxPower(ah, &data[numChannels - 1]); 675185377Ssam *minPow = ar2413GetMinPower(ah, &data[numChannels - 1]); 676185377Ssam return(AH_TRUE); 677185377Ssam } 678185377Ssam } 679185377Ssam 680185377Ssam /* Linearly interpolate the power value now */ 681187831Ssam for (last=0,i=0; (i<numChannels) && (freq > data[i].channelValue); 682185377Ssam last = i++); 683185377Ssam totalD = data[i].channelValue - data[last].channelValue; 684185377Ssam if (totalD > 0) { 685185377Ssam totalF = ar2413GetMaxPower(ah, &data[i]) - ar2413GetMaxPower(ah, &data[last]); 686187831Ssam *maxPow = (int8_t) ((totalF*(freq-data[last].channelValue) + 687185377Ssam ar2413GetMaxPower(ah, &data[last])*totalD)/totalD); 688185377Ssam totalMin = ar2413GetMinPower(ah, &data[i]) - ar2413GetMinPower(ah, &data[last]); 689187831Ssam *minPow = (int8_t) ((totalMin*(freq-data[last].channelValue) + 690185377Ssam ar2413GetMinPower(ah, &data[last])*totalD)/totalD); 691185377Ssam return(AH_TRUE); 692185377Ssam } else { 693187831Ssam if (freq == data[i].channelValue) { 694185377Ssam *maxPow = ar2413GetMaxPower(ah, &data[i]); 695185377Ssam *minPow = ar2413GetMinPower(ah, &data[i]); 696185377Ssam return(AH_TRUE); 697185377Ssam } else 698185377Ssam return(AH_FALSE); 699185377Ssam } 700185377Ssam} 701185377Ssam 702185377Ssam/* 703185377Ssam * Free memory for analog bank scratch buffers 704185377Ssam */ 705185377Ssamstatic void 706185377Ssamar2413RfDetach(struct ath_hal *ah) 707185377Ssam{ 708185377Ssam struct ath_hal_5212 *ahp = AH5212(ah); 709185377Ssam 710185377Ssam HALASSERT(ahp->ah_rfHal != AH_NULL); 711185377Ssam ath_hal_free(ahp->ah_rfHal); 712185377Ssam ahp->ah_rfHal = AH_NULL; 713185377Ssam} 714185377Ssam 715185377Ssam/* 716185377Ssam * Allocate memory for analog bank scratch buffers 717185377Ssam * Scratch Buffer will be reinitialized every reset so no need to zero now 718185377Ssam */ 719185406Ssamstatic HAL_BOOL 720185377Ssamar2413RfAttach(struct ath_hal *ah, HAL_STATUS *status) 721185377Ssam{ 722185377Ssam struct ath_hal_5212 *ahp = AH5212(ah); 723185377Ssam struct ar2413State *priv; 724185377Ssam 725185377Ssam HALASSERT(ah->ah_magic == AR5212_MAGIC); 726185377Ssam 727185377Ssam HALASSERT(ahp->ah_rfHal == AH_NULL); 728185377Ssam priv = ath_hal_malloc(sizeof(struct ar2413State)); 729185377Ssam if (priv == AH_NULL) { 730185377Ssam HALDEBUG(ah, HAL_DEBUG_ANY, 731185377Ssam "%s: cannot allocate private state\n", __func__); 732185377Ssam *status = HAL_ENOMEM; /* XXX */ 733185377Ssam return AH_FALSE; 734185377Ssam } 735185377Ssam priv->base.rfDetach = ar2413RfDetach; 736185377Ssam priv->base.writeRegs = ar2413WriteRegs; 737185377Ssam priv->base.getRfBank = ar2413GetRfBank; 738185377Ssam priv->base.setChannel = ar2413SetChannel; 739185377Ssam priv->base.setRfRegs = ar2413SetRfRegs; 740185377Ssam priv->base.setPowerTable = ar2413SetPowerTable; 741185377Ssam priv->base.getChannelMaxMinPower = ar2413GetChannelMaxMinPower; 742185377Ssam priv->base.getNfAdjust = ar5212GetNfAdjust; 743185377Ssam 744185377Ssam ahp->ah_pcdacTable = priv->pcdacTable; 745185377Ssam ahp->ah_pcdacTableSize = sizeof(priv->pcdacTable); 746185377Ssam ahp->ah_rfHal = &priv->base; 747185377Ssam 748185377Ssam return AH_TRUE; 749185377Ssam} 750185406Ssam 751185406Ssamstatic HAL_BOOL 752185406Ssamar2413Probe(struct ath_hal *ah) 753185406Ssam{ 754185406Ssam return IS_2413(ah); 755185406Ssam} 756185418SsamAH_RF(RF2413, ar2413Probe, ar2413RfAttach); 757