2 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
3 * Copyright (c) 2002-2008 Atheros Communications, Inc.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * $FreeBSD: head/sys/dev/ath/ath_hal/ar5212/ar5112.c 187831 2009-01-28 18:00:22Z sam $
23 #include "ah_internal.h"
25 #include "ah_eeprom_v3.h"
27 #include "ar5212/ar5212.h"
28 #include "ar5212/ar5212reg.h"
29 #include "ar5212/ar5212phy.h"
32 #include "ar5212/ar5212.ini"
34 #define N(a) (sizeof(a)/sizeof(a[0]))
37 RF_HAL_FUNCS base; /* public state, must be first */
38 uint16_t pcdacTable[PWR_TABLE_SIZE];
40 uint32_t Bank1Data[N(ar5212Bank1_5112)];
41 uint32_t Bank2Data[N(ar5212Bank2_5112)];
42 uint32_t Bank3Data[N(ar5212Bank3_5112)];
43 uint32_t Bank6Data[N(ar5212Bank6_5112)];
44 uint32_t Bank7Data[N(ar5212Bank7_5112)];
46 #define AR5112(ah) ((struct ar5112State *) AH5212(ah)->ah_rfHal)
48 static void ar5212GetLowerUpperIndex(uint16_t v,
49 uint16_t *lp, uint16_t listSize,
50 uint32_t *vlo, uint32_t *vhi);
51 static HAL_BOOL getFullPwrTable(uint16_t numPcdacs, uint16_t *pcdacs,
52 int16_t *power, int16_t maxPower, int16_t *retVals);
53 static int16_t getPminAndPcdacTableFromPowerTable(int16_t *pwrTableT4,
55 static int16_t getPminAndPcdacTableFromTwoPowerTables(int16_t *pwrTableLXpdT4,
56 int16_t *pwrTableHXpdT4, uint16_t retVals[], int16_t *pMid);
57 static int16_t interpolate_signed(uint16_t target,
58 uint16_t srcLeft, uint16_t srcRight,
59 int16_t targetLeft, int16_t targetRight);
61 extern void ar5212ModifyRfBuffer(uint32_t *rfBuf, uint32_t reg32,
62 uint32_t numBits, uint32_t firstBit, uint32_t column);
65 ar5112WriteRegs(struct ath_hal *ah, u_int modesIndex, u_int freqIndex,
68 HAL_INI_WRITE_ARRAY(ah, ar5212Modes_5112, modesIndex, writes);
69 HAL_INI_WRITE_ARRAY(ah, ar5212Common_5112, 1, writes);
70 HAL_INI_WRITE_ARRAY(ah, ar5212BB_RfGain_5112, freqIndex, writes);
74 * Take the MHz channel value and set the Channel value
76 * ASSUMES: Writes enabled to analog bus
79 ar5112SetChannel(struct ath_hal *ah, const struct ieee80211_channel *chan)
81 uint16_t freq = ath_hal_gethwchannel(ah, chan);
82 uint32_t channelSel = 0;
83 uint32_t bModeSynth = 0;
84 uint32_t aModeRefSel = 0;
87 OS_MARK(ah, AH_MARK_SETCHANNEL, freq);
92 if (((freq - 2192) % 5) == 0) {
93 channelSel = ((freq - 672) * 2 - 3040)/10;
95 } else if (((freq - 2224) % 5) == 0) {
96 channelSel = ((freq - 704) * 2 - 3040) / 10;
99 HALDEBUG(ah, HAL_DEBUG_ANY,
100 "%s: invalid channel %u MHz\n",
105 channelSel = (channelSel << 2) & 0xff;
106 channelSel = ath_hal_reverseBits(channelSel, 8);
108 txctl = OS_REG_READ(ah, AR_PHY_CCK_TX_CTRL);
110 /* Enable channel spreading for channel 14 */
111 OS_REG_WRITE(ah, AR_PHY_CCK_TX_CTRL,
112 txctl | AR_PHY_CCK_TX_CTRL_JAPAN);
114 OS_REG_WRITE(ah, AR_PHY_CCK_TX_CTRL,
115 txctl &~ AR_PHY_CCK_TX_CTRL_JAPAN);
117 } else if (((freq % 5) == 2) && (freq <= 5435)) {
118 freq = freq - 2; /* Align to even 5MHz raster */
119 channelSel = ath_hal_reverseBits(
120 (uint32_t)(((freq - 4800)*10)/25 + 1), 8);
121 aModeRefSel = ath_hal_reverseBits(0, 2);
122 } else if ((freq % 20) == 0 && freq >= 5120) {
123 channelSel = ath_hal_reverseBits(
124 ((freq - 4800) / 20 << 2), 8);
125 aModeRefSel = ath_hal_reverseBits(3, 2);
126 } else if ((freq % 10) == 0) {
127 channelSel = ath_hal_reverseBits(
128 ((freq - 4800) / 10 << 1), 8);
129 aModeRefSel = ath_hal_reverseBits(2, 2);
130 } else if ((freq % 5) == 0) {
131 channelSel = ath_hal_reverseBits(
132 (freq - 4800) / 5, 8);
133 aModeRefSel = ath_hal_reverseBits(1, 2);
135 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: invalid channel %u MHz\n",
140 reg32 = (channelSel << 4) | (aModeRefSel << 2) | (bModeSynth << 1) |
142 OS_REG_WRITE(ah, AR_PHY(0x27), reg32 & 0xff);
145 OS_REG_WRITE(ah, AR_PHY(0x36), reg32 & 0x7f);
147 AH_PRIVATE(ah)->ah_curchan = chan;
152 * Return a reference to the requested RF Bank.
155 ar5112GetRfBank(struct ath_hal *ah, int bank)
157 struct ar5112State *priv = AR5112(ah);
159 HALASSERT(priv != AH_NULL);
161 case 1: return priv->Bank1Data;
162 case 2: return priv->Bank2Data;
163 case 3: return priv->Bank3Data;
164 case 6: return priv->Bank6Data;
165 case 7: return priv->Bank7Data;
167 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: unknown RF Bank %d requested\n",
173 * Reads EEPROM header info from device structure and programs
176 * REQUIRES: Access to the analog rf device
179 ar5112SetRfRegs(struct ath_hal *ah,
180 const struct ieee80211_channel *chan,
181 uint16_t modesIndex, uint16_t *rfXpdGain)
183 #define RF_BANK_SETUP(_priv, _ix, _col) do { \
185 for (i = 0; i < N(ar5212Bank##_ix##_5112); i++) \
186 (_priv)->Bank##_ix##Data[i] = ar5212Bank##_ix##_5112[i][_col];\
188 uint16_t freq = ath_hal_gethwchannel(ah, chan);
189 struct ath_hal_5212 *ahp = AH5212(ah);
190 const HAL_EEPROM *ee = AH_PRIVATE(ah)->ah_eeprom;
191 uint16_t rfXpdSel, gainI;
192 uint16_t ob5GHz = 0, db5GHz = 0;
193 uint16_t ob2GHz = 0, db2GHz = 0;
194 struct ar5112State *priv = AR5112(ah);
195 GAIN_VALUES *gv = &ahp->ah_gainValues;
200 HALDEBUG(ah, HAL_DEBUG_RFPARAM, "%s: chan %u/0x%x modesIndex %u\n",
201 __func__, chan->ic_freq, chan->ic_flags, modesIndex);
203 /* Setup rf parameters */
204 switch (chan->ic_flags & IEEE80211_CHAN_ALLFULL) {
205 case IEEE80211_CHAN_A:
206 if (freq > 4000 && freq < 5260) {
209 } else if (freq >= 5260 && freq < 5500) {
212 } else if (freq >= 5500 && freq < 5725) {
215 } else if (freq >= 5725) {
221 rfXpdSel = ee->ee_xpd[headerInfo11A];
222 gainI = ee->ee_gainI[headerInfo11A];
224 case IEEE80211_CHAN_B:
225 ob2GHz = ee->ee_ob2GHz[0];
226 db2GHz = ee->ee_db2GHz[0];
227 rfXpdSel = ee->ee_xpd[headerInfo11B];
228 gainI = ee->ee_gainI[headerInfo11B];
230 case IEEE80211_CHAN_G:
231 case IEEE80211_CHAN_PUREG: /* NB: really 108G */
232 ob2GHz = ee->ee_ob2GHz[1];
233 db2GHz = ee->ee_ob2GHz[1];
234 rfXpdSel = ee->ee_xpd[headerInfo11G];
235 gainI = ee->ee_gainI[headerInfo11G];
238 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: invalid channel flags 0x%x\n",
239 __func__, chan->ic_flags);
243 /* Setup Bank 1 Write */
244 RF_BANK_SETUP(priv, 1, 1);
246 /* Setup Bank 2 Write */
247 RF_BANK_SETUP(priv, 2, modesIndex);
249 /* Setup Bank 3 Write */
250 RF_BANK_SETUP(priv, 3, modesIndex);
252 /* Setup Bank 6 Write */
253 RF_BANK_SETUP(priv, 6, modesIndex);
255 ar5212ModifyRfBuffer(priv->Bank6Data, rfXpdSel, 1, 302, 0);
257 ar5212ModifyRfBuffer(priv->Bank6Data, rfXpdGain[0], 2, 270, 0);
258 ar5212ModifyRfBuffer(priv->Bank6Data, rfXpdGain[1], 2, 257, 0);
260 if (IEEE80211_IS_CHAN_OFDM(chan)) {
261 ar5212ModifyRfBuffer(priv->Bank6Data,
262 gv->currStep->paramVal[GP_PWD_138], 1, 168, 3);
263 ar5212ModifyRfBuffer(priv->Bank6Data,
264 gv->currStep->paramVal[GP_PWD_137], 1, 169, 3);
265 ar5212ModifyRfBuffer(priv->Bank6Data,
266 gv->currStep->paramVal[GP_PWD_136], 1, 170, 3);
267 ar5212ModifyRfBuffer(priv->Bank6Data,
268 gv->currStep->paramVal[GP_PWD_132], 1, 174, 3);
269 ar5212ModifyRfBuffer(priv->Bank6Data,
270 gv->currStep->paramVal[GP_PWD_131], 1, 175, 3);
271 ar5212ModifyRfBuffer(priv->Bank6Data,
272 gv->currStep->paramVal[GP_PWD_130], 1, 176, 3);
275 /* Only the 5 or 2 GHz OB/DB need to be set for a mode */
276 if (IEEE80211_IS_CHAN_2GHZ(chan)) {
277 ar5212ModifyRfBuffer(priv->Bank6Data, ob2GHz, 3, 287, 0);
278 ar5212ModifyRfBuffer(priv->Bank6Data, db2GHz, 3, 290, 0);
280 ar5212ModifyRfBuffer(priv->Bank6Data, ob5GHz, 3, 279, 0);
281 ar5212ModifyRfBuffer(priv->Bank6Data, db5GHz, 3, 282, 0);
284 /* Lower synth voltage for X112 Rev 2.0 only */
285 if (IS_RADX112_REV2(ah)) {
286 /* Non-Reversed analyg registers - so values are pre-reversed */
287 ar5212ModifyRfBuffer(priv->Bank6Data, 2, 2, 90, 2);
288 ar5212ModifyRfBuffer(priv->Bank6Data, 2, 2, 92, 2);
289 ar5212ModifyRfBuffer(priv->Bank6Data, 2, 2, 94, 2);
290 ar5212ModifyRfBuffer(priv->Bank6Data, 2, 1, 254, 2);
293 /* Decrease Power Consumption for 5312/5213 and up */
294 if (AH_PRIVATE(ah)->ah_phyRev >= AR_PHY_CHIP_ID_REV_2) {
295 ar5212ModifyRfBuffer(priv->Bank6Data, 1, 1, 281, 1);
296 ar5212ModifyRfBuffer(priv->Bank6Data, 1, 2, 1, 3);
297 ar5212ModifyRfBuffer(priv->Bank6Data, 1, 2, 3, 3);
298 ar5212ModifyRfBuffer(priv->Bank6Data, 1, 1, 139, 3);
299 ar5212ModifyRfBuffer(priv->Bank6Data, 1, 1, 140, 3);
302 /* Setup Bank 7 Setup */
303 RF_BANK_SETUP(priv, 7, modesIndex);
304 if (IEEE80211_IS_CHAN_OFDM(chan))
305 ar5212ModifyRfBuffer(priv->Bank7Data,
306 gv->currStep->paramVal[GP_MIXGAIN_OVR], 2, 37, 0);
308 ar5212ModifyRfBuffer(priv->Bank7Data, gainI, 6, 14, 0);
310 /* Adjust params for Derby TX power control */
311 if (IEEE80211_IS_CHAN_HALF(chan) || IEEE80211_IS_CHAN_QUARTER(chan)) {
312 uint32_t rfDelay, rfPeriod;
315 rfPeriod = (IEEE80211_IS_CHAN_HALF(chan)) ? 0x8 : 0xf;
316 ar5212ModifyRfBuffer(priv->Bank7Data, rfDelay, 4, 58, 0);
317 ar5212ModifyRfBuffer(priv->Bank7Data, rfPeriod, 4, 70, 0);
321 /* Analog registers are setup - EAR can modify */
322 if (ar5212IsEarEngaged(pDev, chan))
324 ar5212EarModify(pDev, EAR_LC_RF_WRITE, chan, &modifier);
326 /* Write Analog registers */
327 HAL_INI_WRITE_BANK(ah, ar5212Bank1_5112, priv->Bank1Data, regWrites);
328 HAL_INI_WRITE_BANK(ah, ar5212Bank2_5112, priv->Bank2Data, regWrites);
329 HAL_INI_WRITE_BANK(ah, ar5212Bank3_5112, priv->Bank3Data, regWrites);
330 HAL_INI_WRITE_BANK(ah, ar5212Bank6_5112, priv->Bank6Data, regWrites);
331 HAL_INI_WRITE_BANK(ah, ar5212Bank7_5112, priv->Bank7Data, regWrites);
333 /* Now that we have reprogrammed rfgain value, clear the flag. */
334 ahp->ah_rfgainState = HAL_RFGAIN_INACTIVE;
340 * Read the transmit power levels from the structures taken from EEPROM
341 * Interpolate read transmit power values for this channel
342 * Organize the transmit power values into a table for writing into the hardware
345 ar5112SetPowerTable(struct ath_hal *ah,
346 int16_t *pPowerMin, int16_t *pPowerMax,
347 const struct ieee80211_channel *chan,
350 uint16_t freq = ath_hal_gethwchannel(ah, chan);
351 struct ath_hal_5212 *ahp = AH5212(ah);
352 const HAL_EEPROM *ee = AH_PRIVATE(ah)->ah_eeprom;
353 uint32_t numXpdGain = IS_RADX112_REV2(ah) ? 2 : 1;
354 uint32_t xpdGainMask = 0;
355 int16_t powerMid, *pPowerMid = &powerMid;
357 const EXPN_DATA_PER_CHANNEL_5112 *pRawCh;
358 const EEPROM_POWER_EXPN_5112 *pPowerExpn = AH_NULL;
361 int16_t minPwr_t4, maxPwr_t4, Pmin, Pmid;
363 uint32_t chan_idx_L = 0, chan_idx_R = 0;
364 uint16_t chan_L, chan_R;
366 int16_t pwr_table0[64];
367 int16_t pwr_table1[64];
371 int16_t powTableLXPD[2][64];
372 int16_t powTableHXPD[2][64];
373 int16_t tmpPowerTable[64];
374 uint16_t xgainList[2];
377 switch (chan->ic_flags & IEEE80211_CHAN_ALLTURBOFULL) {
378 case IEEE80211_CHAN_A:
379 case IEEE80211_CHAN_ST:
380 pPowerExpn = &ee->ee_modePowerArray5112[headerInfo11A];
381 xpdGainMask = ee->ee_xgain[headerInfo11A];
383 case IEEE80211_CHAN_B:
384 pPowerExpn = &ee->ee_modePowerArray5112[headerInfo11B];
385 xpdGainMask = ee->ee_xgain[headerInfo11B];
387 case IEEE80211_CHAN_G:
388 case IEEE80211_CHAN_108G:
389 pPowerExpn = &ee->ee_modePowerArray5112[headerInfo11G];
390 xpdGainMask = ee->ee_xgain[headerInfo11G];
393 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: unknown channel flags 0x%x\n",
394 __func__, chan->ic_flags);
398 if ((xpdGainMask & pPowerExpn->xpdMask) < 1) {
399 HALDEBUG(ah, HAL_DEBUG_ANY,
400 "%s: desired xpdGainMask 0x%x not supported by "
401 "calibrated xpdMask 0x%x\n", __func__,
402 xpdGainMask, pPowerExpn->xpdMask);
406 maxPwr_t4 = (int16_t)(2*(*pPowerMax)); /* pwr_t2 -> pwr_t4 */
407 minPwr_t4 = (int16_t)(2*(*pPowerMin)); /* pwr_t2 -> pwr_t4 */
409 xgainList[0] = 0xDEAD;
410 xgainList[1] = 0xDEAD;
413 xpdMask = pPowerExpn->xpdMask;
414 for (jj = 0; jj < NUM_XPD_PER_CHANNEL; jj++) {
415 if (((xpdMask >> jj) & 1) > 0) {
417 HALDEBUG(ah, HAL_DEBUG_ANY,
418 "A maximum of 2 xpdGains supported"
419 "in pExpnPower data\n");
422 xgainList[kk++] = (uint16_t)jj;
426 ar5212GetLowerUpperIndex(freq, &pPowerExpn->pChannels[0],
427 pPowerExpn->numChannels, &chan_idx_L, &chan_idx_R);
430 for (ii = chan_idx_L; ii <= chan_idx_R; ii++) {
431 pRawCh = &(pPowerExpn->pDataPerChannel[ii]);
432 if (xgainList[1] == 0xDEAD) {
434 numPcd = pRawCh->pDataPerXPD[jj].numPcdacs;
435 OS_MEMCPY(&pcdacs[0], &pRawCh->pDataPerXPD[jj].pcdac[0],
436 numPcd * sizeof(uint16_t));
437 OS_MEMCPY(&powers[0], &pRawCh->pDataPerXPD[jj].pwr_t4[0],
438 numPcd * sizeof(int16_t));
439 if (!getFullPwrTable(numPcd, &pcdacs[0], &powers[0],
440 pRawCh->maxPower_t4, &tmpPowerTable[0])) {
443 OS_MEMCPY(&powTableLXPD[kk][0], &tmpPowerTable[0],
447 numPcd = pRawCh->pDataPerXPD[jj].numPcdacs;
448 OS_MEMCPY(&pcdacs[0], &pRawCh->pDataPerXPD[jj].pcdac[0],
449 numPcd*sizeof(uint16_t));
450 OS_MEMCPY(&powers[0],
451 &pRawCh->pDataPerXPD[jj].pwr_t4[0],
452 numPcd*sizeof(int16_t));
453 if (!getFullPwrTable(numPcd, &pcdacs[0], &powers[0],
454 pRawCh->maxPower_t4, &tmpPowerTable[0])) {
457 OS_MEMCPY(&powTableLXPD[kk][0], &tmpPowerTable[0],
458 64 * sizeof(int16_t));
461 numPcd = pRawCh->pDataPerXPD[jj].numPcdacs;
462 OS_MEMCPY(&pcdacs[0], &pRawCh->pDataPerXPD[jj].pcdac[0],
463 numPcd * sizeof(uint16_t));
464 OS_MEMCPY(&powers[0],
465 &pRawCh->pDataPerXPD[jj].pwr_t4[0],
466 numPcd * sizeof(int16_t));
467 if (!getFullPwrTable(numPcd, &pcdacs[0], &powers[0],
468 pRawCh->maxPower_t4, &tmpPowerTable[0])) {
471 OS_MEMCPY(&powTableHXPD[kk][0], &tmpPowerTable[0],
472 64 * sizeof(int16_t));
477 chan_L = pPowerExpn->pChannels[chan_idx_L];
478 chan_R = pPowerExpn->pChannels[chan_idx_R];
479 kk = chan_idx_R - chan_idx_L;
481 if (xgainList[1] == 0xDEAD) {
482 for (jj = 0; jj < 64; jj++) {
483 pwr_table0[jj] = interpolate_signed(
484 freq, chan_L, chan_R,
485 powTableLXPD[0][jj], powTableLXPD[kk][jj]);
487 Pmin = getPminAndPcdacTableFromPowerTable(&pwr_table0[0],
489 *pPowerMin = (int16_t) (Pmin / 2);
490 *pPowerMid = (int16_t) (pwr_table0[63] / 2);
491 *pPowerMax = (int16_t) (pwr_table0[63] / 2);
492 rfXpdGain[0] = xgainList[0];
493 rfXpdGain[1] = rfXpdGain[0];
495 for (jj = 0; jj < 64; jj++) {
496 pwr_table0[jj] = interpolate_signed(
497 freq, chan_L, chan_R,
498 powTableLXPD[0][jj], powTableLXPD[kk][jj]);
499 pwr_table1[jj] = interpolate_signed(
500 freq, chan_L, chan_R,
501 powTableHXPD[0][jj], powTableHXPD[kk][jj]);
503 if (numXpdGain == 2) {
504 Pmin = getPminAndPcdacTableFromTwoPowerTables(
505 &pwr_table0[0], &pwr_table1[0],
506 ahp->ah_pcdacTable, &Pmid);
507 *pPowerMin = (int16_t) (Pmin / 2);
508 *pPowerMid = (int16_t) (Pmid / 2);
509 *pPowerMax = (int16_t) (pwr_table0[63] / 2);
510 rfXpdGain[0] = xgainList[0];
511 rfXpdGain[1] = xgainList[1];
512 } else if (minPwr_t4 <= pwr_table1[63] &&
513 maxPwr_t4 <= pwr_table1[63]) {
514 Pmin = getPminAndPcdacTableFromPowerTable(
515 &pwr_table1[0], ahp->ah_pcdacTable);
516 rfXpdGain[0] = xgainList[1];
517 rfXpdGain[1] = rfXpdGain[0];
518 *pPowerMin = (int16_t) (Pmin / 2);
519 *pPowerMid = (int16_t) (pwr_table1[63] / 2);
520 *pPowerMax = (int16_t) (pwr_table1[63] / 2);
522 Pmin = getPminAndPcdacTableFromPowerTable(
523 &pwr_table0[0], ahp->ah_pcdacTable);
524 rfXpdGain[0] = xgainList[0];
525 rfXpdGain[1] = rfXpdGain[0];
526 *pPowerMin = (int16_t) (Pmin/2);
527 *pPowerMid = (int16_t) (pwr_table0[63] / 2);
528 *pPowerMax = (int16_t) (pwr_table0[63] / 2);
533 * Move 5112 rates to match power tables where the max
534 * power table entry corresponds with maxPower.
536 HALASSERT(*pPowerMax <= PCDAC_STOP);
537 ahp->ah_txPowerIndexOffset = PCDAC_STOP - *pPowerMax;
543 * Returns interpolated or the scaled up interpolated value
546 interpolate_signed(uint16_t target, uint16_t srcLeft, uint16_t srcRight,
547 int16_t targetLeft, int16_t targetRight)
551 if (srcRight != srcLeft) {
552 rv = ((target - srcLeft)*targetRight +
553 (srcRight - target)*targetLeft) / (srcRight - srcLeft);
561 * Return indices surrounding the value in sorted integer lists.
563 * NB: the input list is assumed to be sorted in ascending order
566 ar5212GetLowerUpperIndex(uint16_t v, uint16_t *lp, uint16_t listSize,
567 uint32_t *vlo, uint32_t *vhi)
570 uint16_t *ep = lp+listSize;
574 * Check first and last elements for out-of-bounds conditions.
576 if (target < lp[0]) {
580 if (target >= ep[-1]) {
581 *vlo = *vhi = listSize - 1;
585 /* look for value being near or between 2 values in list */
586 for (tp = lp; tp < ep; tp++) {
588 * If value is close to the current value of the list
589 * then target is not between values, it is one of the values
592 *vlo = *vhi = tp - lp;
596 * Look for value being between current value and next value
597 * if so return these 2 values
599 if (target < tp[1]) {
608 getFullPwrTable(uint16_t numPcdacs, uint16_t *pcdacs, int16_t *power, int16_t maxPower, int16_t *retVals)
615 HALDEBUG(AH_NULL, HAL_DEBUG_ANY,
616 "%s: at least 2 pcdac values needed [%d]\n",
617 __func__, numPcdacs);
620 for (ii = 0; ii < 64; ii++) {
621 if (ii>pcdacs[idxR] && idxR < numPcdacs-1) {
625 retVals[ii] = interpolate_signed(ii,
626 pcdacs[idxL], pcdacs[idxR], power[idxL], power[idxR]);
627 if (retVals[ii] >= maxPower) {
629 retVals[ii++] = maxPower;
636 * Takes a single calibration curve and creates a power table.
637 * Adjusts the new power table so the max power is relative
638 * to the maximum index in the power table.
640 * WARNING: rates must be adjusted for this relative power table
643 getPminAndPcdacTableFromPowerTable(int16_t *pwrTableT4, uint16_t retVals[])
645 int16_t ii, jj, jjMax;
646 int16_t pMin, currPower, pMax;
648 /* If the spread is > 31.5dB, keep the upper 31.5dB range */
649 if ((pwrTableT4[63] - pwrTableT4[0]) > 126) {
650 pMin = pwrTableT4[63] - 126;
652 pMin = pwrTableT4[0];
655 pMax = pwrTableT4[63];
658 /* Search for highest pcdac 0.25dB below maxPower */
659 while ((pwrTableT4[jjMax] > (pMax - 1) ) && (jjMax >= 0)) {
665 for (ii = 63; ii >= 0; ii--) {
666 while ((jj < 64) && (jj > 0) && (pwrTableT4[jj] >= currPower)) {
671 retVals[ii] = retVals[ii + 1];
677 currPower -= 2; // corresponds to a 0.5dB step
683 * Combines the XPD curves from two calibration sets into a single
684 * power table and adjusts the power table so the max power is relative
685 * to the maximum index in the power table
687 * WARNING: rates must be adjusted for this relative power table
690 getPminAndPcdacTableFromTwoPowerTables(int16_t *pwrTableLXpdT4,
691 int16_t *pwrTableHXpdT4, uint16_t retVals[], int16_t *pMid)
693 int16_t ii, jj, jjMax;
694 int16_t pMin, pMax, currPower;
696 uint16_t msbFlag = 0x40; // turns on the 7th bit of the pcdac
698 /* If the spread is > 31.5dB, keep the upper 31.5dB range */
699 if ((pwrTableLXpdT4[63] - pwrTableHXpdT4[0]) > 126) {
700 pMin = pwrTableLXpdT4[63] - 126;
702 pMin = pwrTableHXpdT4[0];
705 pMax = pwrTableLXpdT4[63];
707 /* Search for highest pcdac 0.25dB below maxPower */
708 while ((pwrTableLXpdT4[jjMax] > (pMax - 1) ) && (jjMax >= 0)){
712 *pMid = pwrTableHXpdT4[63];
716 pwrTableT4 = &(pwrTableLXpdT4[0]);
718 if ((currPower <= *pMid) || ( (jj == 0) && (msbFlag == 0x40))){
720 pwrTableT4 = &(pwrTableHXpdT4[0]);
723 while ((jj > 0) && (pwrTableT4[jj] >= currPower)) {
726 if ((jj == 0) && (msbFlag == 0x00)) {
728 retVals[ii] = retVals[ii+1];
733 retVals[ii] = jj | msbFlag;
734 currPower -= 2; // corresponds to a 0.5dB step
741 ar5112GetMinPower(struct ath_hal *ah, const EXPN_DATA_PER_CHANNEL_5112 *data)
744 int16_t minGain,minPwr,minPcdac,retVal;
746 /* Assume NUM_POINTS_XPD0 > 0 */
747 minGain = data->pDataPerXPD[0].xpd_gain;
748 for (minIndex=0,i=1; i<NUM_XPD_PER_CHANNEL; i++) {
749 if (data->pDataPerXPD[i].xpd_gain < minGain) {
751 minGain = data->pDataPerXPD[i].xpd_gain;
754 minPwr = data->pDataPerXPD[minIndex].pwr_t4[0];
755 minPcdac = data->pDataPerXPD[minIndex].pcdac[0];
756 for (i=1; i<NUM_POINTS_XPD0; i++) {
757 if (data->pDataPerXPD[minIndex].pwr_t4[i] < minPwr) {
758 minPwr = data->pDataPerXPD[minIndex].pwr_t4[i];
759 minPcdac = data->pDataPerXPD[minIndex].pcdac[i];
762 retVal = minPwr - (minPcdac*2);
767 ar5112GetChannelMaxMinPower(struct ath_hal *ah,
768 const struct ieee80211_channel *chan,
769 int16_t *maxPow, int16_t *minPow)
771 uint16_t freq = chan->ic_freq; /* NB: never mapped */
772 const HAL_EEPROM *ee = AH_PRIVATE(ah)->ah_eeprom;
773 int numChannels=0,i,last;
774 int totalD, totalF,totalMin;
775 const EXPN_DATA_PER_CHANNEL_5112 *data=AH_NULL;
776 const EEPROM_POWER_EXPN_5112 *powerArray=AH_NULL;
779 if (IEEE80211_IS_CHAN_A(chan)) {
780 powerArray = ee->ee_modePowerArray5112;
781 data = powerArray[headerInfo11A].pDataPerChannel;
782 numChannels = powerArray[headerInfo11A].numChannels;
783 } else if (IEEE80211_IS_CHAN_G(chan) || IEEE80211_IS_CHAN_108G(chan)) {
784 /* XXX - is this correct? Should we also use the same power for turbo G? */
785 powerArray = ee->ee_modePowerArray5112;
786 data = powerArray[headerInfo11G].pDataPerChannel;
787 numChannels = powerArray[headerInfo11G].numChannels;
788 } else if (IEEE80211_IS_CHAN_B(chan)) {
789 powerArray = ee->ee_modePowerArray5112;
790 data = powerArray[headerInfo11B].pDataPerChannel;
791 numChannels = powerArray[headerInfo11B].numChannels;
795 /* Make sure the channel is in the range of the TP values
801 if ((freq < data[0].channelValue) ||
802 (freq > data[numChannels-1].channelValue)) {
803 if (freq < data[0].channelValue) {
804 *maxPow = data[0].maxPower_t4;
805 *minPow = ar5112GetMinPower(ah, &data[0]);
808 *maxPow = data[numChannels - 1].maxPower_t4;
809 *minPow = ar5112GetMinPower(ah, &data[numChannels - 1]);
814 /* Linearly interpolate the power value now */
816 (i<numChannels) && (freq > data[i].channelValue);
818 totalD = data[i].channelValue - data[last].channelValue;
820 totalF = data[i].maxPower_t4 - data[last].maxPower_t4;
821 *maxPow = (int8_t) ((totalF*(freq-data[last].channelValue) + data[last].maxPower_t4*totalD)/totalD);
823 totalMin = ar5112GetMinPower(ah,&data[i]) - ar5112GetMinPower(ah, &data[last]);
824 *minPow = (int8_t) ((totalMin*(freq-data[last].channelValue) + ar5112GetMinPower(ah, &data[last])*totalD)/totalD);
827 if (freq == data[i].channelValue) {
828 *maxPow = data[i].maxPower_t4;
829 *minPow = ar5112GetMinPower(ah, &data[i]);
837 * Free memory for analog bank scratch buffers
840 ar5112RfDetach(struct ath_hal *ah)
842 struct ath_hal_5212 *ahp = AH5212(ah);
844 HALASSERT(ahp->ah_rfHal != AH_NULL);
845 ath_hal_free(ahp->ah_rfHal);
846 ahp->ah_rfHal = AH_NULL;
850 * Allocate memory for analog bank scratch buffers
851 * Scratch Buffer will be reinitialized every reset so no need to zero now
854 ar5112RfAttach(struct ath_hal *ah, HAL_STATUS *status)
856 struct ath_hal_5212 *ahp = AH5212(ah);
857 struct ar5112State *priv;
859 HALASSERT(ah->ah_magic == AR5212_MAGIC);
861 HALASSERT(ahp->ah_rfHal == AH_NULL);
862 priv = ath_hal_malloc(sizeof(struct ar5112State));
863 if (priv == AH_NULL) {
864 HALDEBUG(ah, HAL_DEBUG_ANY,
865 "%s: cannot allocate private state\n", __func__);
866 *status = HAL_ENOMEM; /* XXX */
869 priv->base.rfDetach = ar5112RfDetach;
870 priv->base.writeRegs = ar5112WriteRegs;
871 priv->base.getRfBank = ar5112GetRfBank;
872 priv->base.setChannel = ar5112SetChannel;
873 priv->base.setRfRegs = ar5112SetRfRegs;
874 priv->base.setPowerTable = ar5112SetPowerTable;
875 priv->base.getChannelMaxMinPower = ar5112GetChannelMaxMinPower;
876 priv->base.getNfAdjust = ar5212GetNfAdjust;
878 ahp->ah_pcdacTable = priv->pcdacTable;
879 ahp->ah_pcdacTableSize = sizeof(priv->pcdacTable);
880 ahp->ah_rfHal = &priv->base;
886 ar5112Probe(struct ath_hal *ah)
888 return IS_RAD5112(ah);
890 AH_RF(RF5112, ar5112Probe, ar5112RfAttach);