No empty .Rs/.Re
[netbsd-mini2440.git] / sys / external / isc / atheros_hal / dist / ar5212 / ar2413.c
blob8b739f2d185a639c2b51cf072100557422ea174f
1 /*
2 * Copyright (c) 2002-2008 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 * $Id: ar2413.c,v 1.2 2009/01/06 06:03:57 mrg Exp $
19 #include "opt_ah.h"
21 #include "ah.h"
22 #include "ah_internal.h"
24 #include "ar5212/ar5212.h"
25 #include "ar5212/ar5212reg.h"
26 #include "ar5212/ar5212phy.h"
28 #include "ah_eeprom_v3.h"
30 #define AH_5212_2413
31 #include "ar5212/ar5212.ini"
33 #define N(a) (sizeof(a)/sizeof(a[0]))
35 struct ar2413State {
36 RF_HAL_FUNCS base; /* public state, must be first */
37 uint16_t pcdacTable[PWR_TABLE_SIZE_2413];
39 uint32_t Bank1Data[N(ar5212Bank1_2413)];
40 uint32_t Bank2Data[N(ar5212Bank2_2413)];
41 uint32_t Bank3Data[N(ar5212Bank3_2413)];
42 uint32_t Bank6Data[N(ar5212Bank6_2413)];
43 uint32_t Bank7Data[N(ar5212Bank7_2413)];
46 * Private state for reduced stack usage.
48 /* filled out Vpd table for all pdGains (chanL) */
49 uint16_t vpdTable_L[MAX_NUM_PDGAINS_PER_CHANNEL]
50 [MAX_PWR_RANGE_IN_HALF_DB];
51 /* filled out Vpd table for all pdGains (chanR) */
52 uint16_t vpdTable_R[MAX_NUM_PDGAINS_PER_CHANNEL]
53 [MAX_PWR_RANGE_IN_HALF_DB];
54 /* filled out Vpd table for all pdGains (interpolated) */
55 uint16_t vpdTable_I[MAX_NUM_PDGAINS_PER_CHANNEL]
56 [MAX_PWR_RANGE_IN_HALF_DB];
58 #define AR2413(ah) ((struct ar2413State *) AH5212(ah)->ah_rfHal)
60 extern void ar5212ModifyRfBuffer(uint32_t *rfBuf, uint32_t reg32,
61 uint32_t numBits, uint32_t firstBit, uint32_t column);
63 static void
64 ar2413WriteRegs(struct ath_hal *ah, u_int modesIndex, u_int freqIndex,
65 int writes)
67 HAL_INI_WRITE_ARRAY(ah, ar5212Modes_2413, modesIndex, writes);
68 HAL_INI_WRITE_ARRAY(ah, ar5212Common_2413, 1, writes);
69 HAL_INI_WRITE_ARRAY(ah, ar5212BB_RfGain_2413, freqIndex, writes);
73 * Take the MHz channel value and set the Channel value
75 * ASSUMES: Writes enabled to analog bus
77 static HAL_BOOL
78 ar2413SetChannel(struct ath_hal *ah, HAL_CHANNEL_INTERNAL *chan)
80 uint32_t channelSel = 0;
81 uint32_t bModeSynth = 0;
82 uint32_t aModeRefSel = 0;
83 uint32_t reg32 = 0;
84 uint16_t freq;
86 OS_MARK(ah, AH_MARK_SETCHANNEL, chan->channel);
88 if (chan->channel < 4800) {
89 uint32_t txctl;
91 if (((chan->channel - 2192) % 5) == 0) {
92 channelSel = ((chan->channel - 672) * 2 - 3040)/10;
93 bModeSynth = 0;
94 } else if (((chan->channel - 2224) % 5) == 0) {
95 channelSel = ((chan->channel - 704) * 2 - 3040) / 10;
96 bModeSynth = 1;
97 } else {
98 HALDEBUG(ah, HAL_DEBUG_ANY,
99 "%s: invalid channel %u MHz\n",
100 __func__, chan->channel);
101 return AH_FALSE;
104 channelSel = (channelSel << 2) & 0xff;
105 channelSel = ath_hal_reverseBits(channelSel, 8);
107 txctl = OS_REG_READ(ah, AR_PHY_CCK_TX_CTRL);
108 if (chan->channel == 2484) {
109 /* Enable channel spreading for channel 14 */
110 OS_REG_WRITE(ah, AR_PHY_CCK_TX_CTRL,
111 txctl | AR_PHY_CCK_TX_CTRL_JAPAN);
112 } else {
113 OS_REG_WRITE(ah, AR_PHY_CCK_TX_CTRL,
114 txctl &~ AR_PHY_CCK_TX_CTRL_JAPAN);
116 } else if (((chan->channel % 5) == 2) && (chan->channel <= 5435)) {
117 freq = chan->channel - 2; /* Align to even 5MHz raster */
118 channelSel = ath_hal_reverseBits(
119 (uint32_t)(((freq - 4800)*10)/25 + 1), 8);
120 aModeRefSel = ath_hal_reverseBits(0, 2);
121 } else if ((chan->channel % 20) == 0 && chan->channel >= 5120) {
122 channelSel = ath_hal_reverseBits(
123 ((chan->channel - 4800) / 20 << 2), 8);
124 aModeRefSel = ath_hal_reverseBits(3, 2);
125 } else if ((chan->channel % 10) == 0) {
126 channelSel = ath_hal_reverseBits(
127 ((chan->channel - 4800) / 10 << 1), 8);
128 aModeRefSel = ath_hal_reverseBits(2, 2);
129 } else if ((chan->channel % 5) == 0) {
130 channelSel = ath_hal_reverseBits(
131 (chan->channel - 4800) / 5, 8);
132 aModeRefSel = ath_hal_reverseBits(1, 2);
133 } else {
134 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: invalid channel %u MHz\n",
135 __func__, chan->channel);
136 return AH_FALSE;
139 reg32 = (channelSel << 4) | (aModeRefSel << 2) | (bModeSynth << 1) |
140 (1 << 12) | 0x1;
141 OS_REG_WRITE(ah, AR_PHY(0x27), reg32 & 0xff);
143 reg32 >>= 8;
144 OS_REG_WRITE(ah, AR_PHY(0x36), reg32 & 0x7f);
146 AH_PRIVATE(ah)->ah_curchan = chan;
148 return AH_TRUE;
152 * Reads EEPROM header info from device structure and programs
153 * all rf registers
155 * REQUIRES: Access to the analog rf device
157 static HAL_BOOL
158 ar2413SetRfRegs(struct ath_hal *ah, HAL_CHANNEL_INTERNAL *chan, uint16_t modesIndex, uint16_t *rfXpdGain)
160 #define RF_BANK_SETUP(_priv, _ix, _col) do { \
161 int i; \
162 for (i = 0; i < N(ar5212Bank##_ix##_2413); i++) \
163 (_priv)->Bank##_ix##Data[i] = ar5212Bank##_ix##_2413[i][_col];\
164 } while (0)
165 struct ath_hal_5212 *ahp = AH5212(ah);
166 const HAL_EEPROM *ee = AH_PRIVATE(ah)->ah_eeprom;
167 uint16_t ob2GHz = 0, db2GHz = 0;
168 struct ar2413State *priv = AR2413(ah);
169 int regWrites = 0;
171 HALDEBUG(ah, HAL_DEBUG_RFPARAM,
172 "%s: chan 0x%x flag 0x%x modesIndex 0x%x\n",
173 __func__, chan->channel, chan->channelFlags, modesIndex);
175 HALASSERT(priv);
177 /* Setup rf parameters */
178 switch (chan->channelFlags & CHANNEL_ALL) {
179 case CHANNEL_B:
180 ob2GHz = ee->ee_obFor24;
181 db2GHz = ee->ee_dbFor24;
182 break;
183 case CHANNEL_G:
184 case CHANNEL_108G:
185 ob2GHz = ee->ee_obFor24g;
186 db2GHz = ee->ee_dbFor24g;
187 break;
188 default:
189 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: invalid channel flags 0x%x\n",
190 __func__, chan->channelFlags);
191 return AH_FALSE;
194 /* Bank 1 Write */
195 RF_BANK_SETUP(priv, 1, 1);
197 /* Bank 2 Write */
198 RF_BANK_SETUP(priv, 2, modesIndex);
200 /* Bank 3 Write */
201 RF_BANK_SETUP(priv, 3, modesIndex);
203 /* Bank 6 Write */
204 RF_BANK_SETUP(priv, 6, modesIndex);
206 ar5212ModifyRfBuffer(priv->Bank6Data, ob2GHz, 3, 168, 0);
207 ar5212ModifyRfBuffer(priv->Bank6Data, db2GHz, 3, 165, 0);
209 /* Bank 7 Setup */
210 RF_BANK_SETUP(priv, 7, modesIndex);
212 /* Write Analog registers */
213 HAL_INI_WRITE_BANK(ah, ar5212Bank1_2413, priv->Bank1Data, regWrites);
214 HAL_INI_WRITE_BANK(ah, ar5212Bank2_2413, priv->Bank2Data, regWrites);
215 HAL_INI_WRITE_BANK(ah, ar5212Bank3_2413, priv->Bank3Data, regWrites);
216 HAL_INI_WRITE_BANK(ah, ar5212Bank6_2413, priv->Bank6Data, regWrites);
217 HAL_INI_WRITE_BANK(ah, ar5212Bank7_2413, priv->Bank7Data, regWrites);
219 /* Now that we have reprogrammed rfgain value, clear the flag. */
220 ahp->ah_rfgainState = HAL_RFGAIN_INACTIVE;
222 return AH_TRUE;
223 #undef RF_BANK_SETUP
227 * Return a reference to the requested RF Bank.
229 static uint32_t *
230 ar2413GetRfBank(struct ath_hal *ah, int bank)
232 struct ar2413State *priv = AR2413(ah);
234 HALASSERT(priv != AH_NULL);
235 switch (bank) {
236 case 1: return priv->Bank1Data;
237 case 2: return priv->Bank2Data;
238 case 3: return priv->Bank3Data;
239 case 6: return priv->Bank6Data;
240 case 7: return priv->Bank7Data;
242 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: unknown RF Bank %d requested\n",
243 __func__, bank);
244 return AH_NULL;
248 * Return indices surrounding the value in sorted integer lists.
250 * NB: the input list is assumed to be sorted in ascending order
252 static void
253 GetLowerUpperIndex(int16_t v, const uint16_t *lp, uint16_t listSize,
254 uint32_t *vlo, uint32_t *vhi)
256 int16_t target = v;
257 const uint16_t *ep = lp+listSize;
258 const uint16_t *tp;
261 * Check first and last elements for out-of-bounds conditions.
263 if (target < lp[0]) {
264 *vlo = *vhi = 0;
265 return;
267 if (target >= ep[-1]) {
268 *vlo = *vhi = listSize - 1;
269 return;
272 /* look for value being near or between 2 values in list */
273 for (tp = lp; tp < ep; tp++) {
275 * If value is close to the current value of the list
276 * then target is not between values, it is one of the values
278 if (*tp == target) {
279 *vlo = *vhi = tp - (const uint16_t *) lp;
280 return;
283 * Look for value being between current value and next value
284 * if so return these 2 values
286 if (target < tp[1]) {
287 *vlo = tp - (const uint16_t *) lp;
288 *vhi = *vlo + 1;
289 return;
295 * Fill the Vpdlist for indices Pmax-Pmin
297 static HAL_BOOL
298 ar2413FillVpdTable(uint32_t pdGainIdx, int16_t Pmin, int16_t Pmax,
299 const int16_t *pwrList, const uint16_t *VpdList,
300 uint16_t numIntercepts, uint16_t retVpdList[][64])
302 uint16_t ii, jj, kk;
303 int16_t currPwr = (int16_t)(2*Pmin);
304 /* since Pmin is pwr*2 and pwrList is 4*pwr */
305 uint32_t idxL = 0, idxR = 0;
307 ii = 0;
308 jj = 0;
310 if (numIntercepts < 2)
311 return AH_FALSE;
313 while (ii <= (uint16_t)(Pmax - Pmin)) {
314 GetLowerUpperIndex(currPwr, (const uint16_t *) pwrList,
315 numIntercepts, &(idxL), &(idxR));
316 if (idxR < 1)
317 idxR = 1; /* extrapolate below */
318 if (idxL == (uint32_t)(numIntercepts - 1))
319 idxL = numIntercepts - 2; /* extrapolate above */
320 if (pwrList[idxL] == pwrList[idxR])
321 kk = VpdList[idxL];
322 else
323 kk = (uint16_t)
324 (((currPwr - pwrList[idxL])*VpdList[idxR]+
325 (pwrList[idxR] - currPwr)*VpdList[idxL])/
326 (pwrList[idxR] - pwrList[idxL]));
327 retVpdList[pdGainIdx][ii] = kk;
328 ii++;
329 currPwr += 2; /* half dB steps */
332 return AH_TRUE;
336 * Returns interpolated or the scaled up interpolated value
338 static int16_t
339 interpolate_signed(uint16_t target, uint16_t srcLeft, uint16_t srcRight,
340 int16_t targetLeft, int16_t targetRight)
342 int16_t rv;
344 if (srcRight != srcLeft) {
345 rv = ((target - srcLeft)*targetRight +
346 (srcRight - target)*targetLeft) / (srcRight - srcLeft);
347 } else {
348 rv = targetLeft;
350 return rv;
354 * Uses the data points read from EEPROM to reconstruct the pdadc power table
355 * Called by ar2413SetPowerTable()
357 static int
358 ar2413getGainBoundariesAndPdadcsForPowers(struct ath_hal *ah, uint16_t channel,
359 const RAW_DATA_STRUCT_2413 *pRawDataset,
360 uint16_t pdGainOverlap_t2,
361 int16_t *pMinCalPower, uint16_t pPdGainBoundaries[],
362 uint16_t pPdGainValues[], uint16_t pPDADCValues[])
364 struct ar2413State *priv = AR2413(ah);
365 #define VpdTable_L priv->vpdTable_L
366 #define VpdTable_R priv->vpdTable_R
367 #define VpdTable_I priv->vpdTable_I
368 uint32_t ii, jj, kk;
369 int32_t ss;/* potentially -ve index for taking care of pdGainOverlap */
370 uint32_t idxL = 0, idxR = 0;
371 uint32_t numPdGainsUsed = 0;
373 * If desired to support -ve power levels in future, just
374 * change pwr_I_0 to signed 5-bits.
376 int16_t Pmin_t2[MAX_NUM_PDGAINS_PER_CHANNEL];
377 /* to accomodate -ve power levels later on. */
378 int16_t Pmax_t2[MAX_NUM_PDGAINS_PER_CHANNEL];
379 /* to accomodate -ve power levels later on */
380 uint16_t numVpd = 0;
381 uint16_t Vpd_step;
382 int16_t tmpVal ;
383 uint32_t sizeCurrVpdTable, maxIndex, tgtIndex;
385 /* Get upper lower index */
386 GetLowerUpperIndex(channel, pRawDataset->pChannels,
387 pRawDataset->numChannels, &(idxL), &(idxR));
389 for (ii = 0; ii < MAX_NUM_PDGAINS_PER_CHANNEL; ii++) {
390 jj = MAX_NUM_PDGAINS_PER_CHANNEL - ii - 1;
391 /* work backwards 'cause highest pdGain for lowest power */
392 numVpd = pRawDataset->pDataPerChannel[idxL].pDataPerPDGain[jj].numVpd;
393 if (numVpd > 0) {
394 pPdGainValues[numPdGainsUsed] = pRawDataset->pDataPerChannel[idxL].pDataPerPDGain[jj].pd_gain;
395 Pmin_t2[numPdGainsUsed] = pRawDataset->pDataPerChannel[idxL].pDataPerPDGain[jj].pwr_t4[0];
396 if (Pmin_t2[numPdGainsUsed] >pRawDataset->pDataPerChannel[idxR].pDataPerPDGain[jj].pwr_t4[0]) {
397 Pmin_t2[numPdGainsUsed] = pRawDataset->pDataPerChannel[idxR].pDataPerPDGain[jj].pwr_t4[0];
399 Pmin_t2[numPdGainsUsed] = (int16_t)
400 (Pmin_t2[numPdGainsUsed] / 2);
401 Pmax_t2[numPdGainsUsed] = pRawDataset->pDataPerChannel[idxL].pDataPerPDGain[jj].pwr_t4[numVpd-1];
402 if (Pmax_t2[numPdGainsUsed] > pRawDataset->pDataPerChannel[idxR].pDataPerPDGain[jj].pwr_t4[numVpd-1])
403 Pmax_t2[numPdGainsUsed] =
404 pRawDataset->pDataPerChannel[idxR].pDataPerPDGain[jj].pwr_t4[numVpd-1];
405 Pmax_t2[numPdGainsUsed] = (int16_t)(Pmax_t2[numPdGainsUsed] / 2);
406 ar2413FillVpdTable(
407 numPdGainsUsed, Pmin_t2[numPdGainsUsed], Pmax_t2[numPdGainsUsed],
408 &(pRawDataset->pDataPerChannel[idxL].pDataPerPDGain[jj].pwr_t4[0]),
409 &(pRawDataset->pDataPerChannel[idxL].pDataPerPDGain[jj].Vpd[0]), numVpd, VpdTable_L
411 ar2413FillVpdTable(
412 numPdGainsUsed, Pmin_t2[numPdGainsUsed], Pmax_t2[numPdGainsUsed],
413 &(pRawDataset->pDataPerChannel[idxR].pDataPerPDGain[jj].pwr_t4[0]),
414 &(pRawDataset->pDataPerChannel[idxR].pDataPerPDGain[jj].Vpd[0]), numVpd, VpdTable_R
416 for (kk = 0; kk < (uint16_t)(Pmax_t2[numPdGainsUsed] - Pmin_t2[numPdGainsUsed]); kk++) {
417 VpdTable_I[numPdGainsUsed][kk] =
418 interpolate_signed(
419 channel, pRawDataset->pChannels[idxL], pRawDataset->pChannels[idxR],
420 (int16_t)VpdTable_L[numPdGainsUsed][kk], (int16_t)VpdTable_R[numPdGainsUsed][kk]);
422 /* fill VpdTable_I for this pdGain */
423 numPdGainsUsed++;
425 /* if this pdGain is used */
428 *pMinCalPower = Pmin_t2[0];
429 kk = 0; /* index for the final table */
430 for (ii = 0; ii < numPdGainsUsed; ii++) {
431 if (ii == (numPdGainsUsed - 1))
432 pPdGainBoundaries[ii] = Pmax_t2[ii] +
433 PD_GAIN_BOUNDARY_STRETCH_IN_HALF_DB;
434 else
435 pPdGainBoundaries[ii] = (uint16_t)
436 ((Pmax_t2[ii] + Pmin_t2[ii+1]) / 2 );
437 if (pPdGainBoundaries[ii] > 63) {
438 HALDEBUG(ah, HAL_DEBUG_ANY,
439 "%s: clamp pPdGainBoundaries[%d] %d\n",
440 __func__, ii, pPdGainBoundaries[ii]);/*XXX*/
441 pPdGainBoundaries[ii] = 63;
444 /* Find starting index for this pdGain */
445 if (ii == 0)
446 ss = 0; /* for the first pdGain, start from index 0 */
447 else
448 ss = (pPdGainBoundaries[ii-1] - Pmin_t2[ii]) -
449 pdGainOverlap_t2;
450 Vpd_step = (uint16_t)(VpdTable_I[ii][1] - VpdTable_I[ii][0]);
451 Vpd_step = (uint16_t)((Vpd_step < 1) ? 1 : Vpd_step);
453 *-ve ss indicates need to extrapolate data below for this pdGain
455 while (ss < 0) {
456 tmpVal = (int16_t)(VpdTable_I[ii][0] + ss*Vpd_step);
457 pPDADCValues[kk++] = (uint16_t)((tmpVal < 0) ? 0 : tmpVal);
458 ss++;
461 sizeCurrVpdTable = Pmax_t2[ii] - Pmin_t2[ii];
462 tgtIndex = pPdGainBoundaries[ii] + pdGainOverlap_t2 - Pmin_t2[ii];
463 maxIndex = (tgtIndex < sizeCurrVpdTable) ? tgtIndex : sizeCurrVpdTable;
465 while (ss < (int16_t)maxIndex)
466 pPDADCValues[kk++] = VpdTable_I[ii][ss++];
468 Vpd_step = (uint16_t)(VpdTable_I[ii][sizeCurrVpdTable-1] -
469 VpdTable_I[ii][sizeCurrVpdTable-2]);
470 Vpd_step = (uint16_t)((Vpd_step < 1) ? 1 : Vpd_step);
472 * for last gain, pdGainBoundary == Pmax_t2, so will
473 * have to extrapolate
475 if (tgtIndex > maxIndex) { /* need to extrapolate above */
476 while(ss < (int16_t)tgtIndex) {
477 tmpVal = (uint16_t)
478 (VpdTable_I[ii][sizeCurrVpdTable-1] +
479 (ss-maxIndex)*Vpd_step);
480 pPDADCValues[kk++] = (tmpVal > 127) ?
481 127 : tmpVal;
482 ss++;
484 } /* extrapolated above */
485 } /* for all pdGainUsed */
487 while (ii < MAX_NUM_PDGAINS_PER_CHANNEL) {
488 pPdGainBoundaries[ii] = pPdGainBoundaries[ii-1];
489 ii++;
491 while (kk < 128) {
492 pPDADCValues[kk] = pPDADCValues[kk-1];
493 kk++;
496 return numPdGainsUsed;
497 #undef VpdTable_L
498 #undef VpdTable_R
499 #undef VpdTable_I
502 static HAL_BOOL
503 ar2413SetPowerTable(struct ath_hal *ah,
504 int16_t *minPower, int16_t *maxPower, HAL_CHANNEL_INTERNAL *chan,
505 uint16_t *rfXpdGain)
507 struct ath_hal_5212 *ahp = AH5212(ah);
508 const HAL_EEPROM *ee = AH_PRIVATE(ah)->ah_eeprom;
509 const RAW_DATA_STRUCT_2413 *pRawDataset = AH_NULL;
510 uint16_t pdGainOverlap_t2;
511 int16_t minCalPower2413_t2;
512 uint16_t *pdadcValues = ahp->ah_pcdacTable;
513 uint16_t gainBoundaries[4];
514 uint32_t reg32, regoffset;
515 int i, numPdGainsUsed;
516 #ifndef AH_USE_INIPDGAIN
517 uint32_t tpcrg1;
518 #endif
520 HALDEBUG(ah, HAL_DEBUG_RFPARAM, "%s: chan 0x%x flag 0x%x\n",
521 __func__, chan->channel,chan->channelFlags);
523 if (IS_CHAN_G(chan) || IS_CHAN_108G(chan))
524 pRawDataset = &ee->ee_rawDataset2413[headerInfo11G];
525 else if (IS_CHAN_B(chan))
526 pRawDataset = &ee->ee_rawDataset2413[headerInfo11B];
527 else {
528 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: illegal mode\n", __func__);
529 return AH_FALSE;
532 pdGainOverlap_t2 = (uint16_t) SM(OS_REG_READ(ah, AR_PHY_TPCRG5),
533 AR_PHY_TPCRG5_PD_GAIN_OVERLAP);
535 numPdGainsUsed = ar2413getGainBoundariesAndPdadcsForPowers(ah,
536 chan->channel, pRawDataset, pdGainOverlap_t2,
537 &minCalPower2413_t2,gainBoundaries, rfXpdGain, pdadcValues);
538 HALASSERT(1 <= numPdGainsUsed && numPdGainsUsed <= 3);
540 #ifdef AH_USE_INIPDGAIN
542 * Use pd_gains curve from eeprom; Atheros always uses
543 * the default curve from the ini file but some vendors
544 * (e.g. Zcomax) want to override this curve and not
545 * honoring their settings results in tx power 5dBm low.
547 OS_REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_NUM_PD_GAIN,
548 (pRawDataset->pDataPerChannel[0].numPdGains - 1));
549 #else
550 tpcrg1 = OS_REG_READ(ah, AR_PHY_TPCRG1);
551 tpcrg1 = (tpcrg1 &~ AR_PHY_TPCRG1_NUM_PD_GAIN)
552 | SM(numPdGainsUsed-1, AR_PHY_TPCRG1_NUM_PD_GAIN);
553 switch (numPdGainsUsed) {
554 case 3:
555 tpcrg1 &= ~AR_PHY_TPCRG1_PDGAIN_SETTING3;
556 tpcrg1 |= SM(rfXpdGain[2], AR_PHY_TPCRG1_PDGAIN_SETTING3);
557 /* fall thru... */
558 case 2:
559 tpcrg1 &= ~AR_PHY_TPCRG1_PDGAIN_SETTING2;
560 tpcrg1 |= SM(rfXpdGain[1], AR_PHY_TPCRG1_PDGAIN_SETTING2);
561 /* fall thru... */
562 case 1:
563 tpcrg1 &= ~AR_PHY_TPCRG1_PDGAIN_SETTING1;
564 tpcrg1 |= SM(rfXpdGain[0], AR_PHY_TPCRG1_PDGAIN_SETTING1);
565 break;
567 #ifdef AH_DEBUG
568 if (tpcrg1 != OS_REG_READ(ah, AR_PHY_TPCRG1))
569 HALDEBUG(ah, HAL_DEBUG_RFPARAM, "%s: using non-default "
570 "pd_gains (default 0x%x, calculated 0x%x)\n",
571 __func__, OS_REG_READ(ah, AR_PHY_TPCRG1), tpcrg1);
572 #endif
573 OS_REG_WRITE(ah, AR_PHY_TPCRG1, tpcrg1);
574 #endif
577 * Note the pdadc table may not start at 0 dBm power, could be
578 * negative or greater than 0. Need to offset the power
579 * values by the amount of minPower for griffin
581 if (minCalPower2413_t2 != 0)
582 ahp->ah_txPowerIndexOffset = (int16_t)(0 - minCalPower2413_t2);
583 else
584 ahp->ah_txPowerIndexOffset = 0;
586 /* Finally, write the power values into the baseband power table */
587 regoffset = 0x9800 + (672 <<2); /* beginning of pdadc table in griffin */
588 for (i = 0; i < 32; i++) {
589 reg32 = ((pdadcValues[4*i + 0] & 0xFF) << 0) |
590 ((pdadcValues[4*i + 1] & 0xFF) << 8) |
591 ((pdadcValues[4*i + 2] & 0xFF) << 16) |
592 ((pdadcValues[4*i + 3] & 0xFF) << 24) ;
593 OS_REG_WRITE(ah, regoffset, reg32);
594 regoffset += 4;
597 OS_REG_WRITE(ah, AR_PHY_TPCRG5,
598 SM(pdGainOverlap_t2, AR_PHY_TPCRG5_PD_GAIN_OVERLAP) |
599 SM(gainBoundaries[0], AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_1) |
600 SM(gainBoundaries[1], AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_2) |
601 SM(gainBoundaries[2], AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_3) |
602 SM(gainBoundaries[3], AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_4));
604 return AH_TRUE;
607 static int16_t
608 ar2413GetMinPower(struct ath_hal *ah, const RAW_DATA_PER_CHANNEL_2413 *data)
610 uint32_t ii,jj;
611 uint16_t Pmin=0,numVpd;
613 for (ii = 0; ii < MAX_NUM_PDGAINS_PER_CHANNEL; ii++) {
614 jj = MAX_NUM_PDGAINS_PER_CHANNEL - ii - 1;
615 /* work backwards 'cause highest pdGain for lowest power */
616 numVpd = data->pDataPerPDGain[jj].numVpd;
617 if (numVpd > 0) {
618 Pmin = data->pDataPerPDGain[jj].pwr_t4[0];
619 return(Pmin);
622 return(Pmin);
625 static int16_t
626 ar2413GetMaxPower(struct ath_hal *ah, const RAW_DATA_PER_CHANNEL_2413 *data)
628 uint32_t ii;
629 uint16_t Pmax=0,numVpd;
631 for (ii=0; ii< MAX_NUM_PDGAINS_PER_CHANNEL; ii++) {
632 /* work forwards cuase lowest pdGain for highest power */
633 numVpd = data->pDataPerPDGain[ii].numVpd;
634 if (numVpd > 0) {
635 Pmax = data->pDataPerPDGain[ii].pwr_t4[numVpd-1];
636 return(Pmax);
639 return(Pmax);
642 static HAL_BOOL
643 ar2413GetChannelMaxMinPower(struct ath_hal *ah, HAL_CHANNEL *chan,
644 int16_t *maxPow, int16_t *minPow)
646 const HAL_EEPROM *ee = AH_PRIVATE(ah)->ah_eeprom;
647 const RAW_DATA_STRUCT_2413 *pRawDataset = AH_NULL;
648 const RAW_DATA_PER_CHANNEL_2413 *data = AH_NULL;
649 uint16_t numChannels;
650 int totalD,totalF, totalMin,last, i;
652 *maxPow = 0;
654 if (IS_CHAN_G(chan) || IS_CHAN_108G(chan))
655 pRawDataset = &ee->ee_rawDataset2413[headerInfo11G];
656 else if (IS_CHAN_B(chan))
657 pRawDataset = &ee->ee_rawDataset2413[headerInfo11B];
658 else
659 return(AH_FALSE);
661 numChannels = pRawDataset->numChannels;
662 data = pRawDataset->pDataPerChannel;
664 /* Make sure the channel is in the range of the TP values
665 * (freq piers)
667 if (numChannels < 1)
668 return(AH_FALSE);
670 if ((chan->channel < data[0].channelValue) ||
671 (chan->channel > data[numChannels-1].channelValue)) {
672 if (chan->channel < data[0].channelValue) {
673 *maxPow = ar2413GetMaxPower(ah, &data[0]);
674 *minPow = ar2413GetMinPower(ah, &data[0]);
675 return(AH_TRUE);
676 } else {
677 *maxPow = ar2413GetMaxPower(ah, &data[numChannels - 1]);
678 *minPow = ar2413GetMinPower(ah, &data[numChannels - 1]);
679 return(AH_TRUE);
683 /* Linearly interpolate the power value now */
684 for (last=0,i=0; (i<numChannels) && (chan->channel > data[i].channelValue);
685 last = i++);
686 totalD = data[i].channelValue - data[last].channelValue;
687 if (totalD > 0) {
688 totalF = ar2413GetMaxPower(ah, &data[i]) - ar2413GetMaxPower(ah, &data[last]);
689 *maxPow = (int8_t) ((totalF*(chan->channel-data[last].channelValue) +
690 ar2413GetMaxPower(ah, &data[last])*totalD)/totalD);
691 totalMin = ar2413GetMinPower(ah, &data[i]) - ar2413GetMinPower(ah, &data[last]);
692 *minPow = (int8_t) ((totalMin*(chan->channel-data[last].channelValue) +
693 ar2413GetMinPower(ah, &data[last])*totalD)/totalD);
694 return(AH_TRUE);
695 } else {
696 if (chan->channel == data[i].channelValue) {
697 *maxPow = ar2413GetMaxPower(ah, &data[i]);
698 *minPow = ar2413GetMinPower(ah, &data[i]);
699 return(AH_TRUE);
700 } else
701 return(AH_FALSE);
706 * Free memory for analog bank scratch buffers
708 static void
709 ar2413RfDetach(struct ath_hal *ah)
711 struct ath_hal_5212 *ahp = AH5212(ah);
713 HALASSERT(ahp->ah_rfHal != AH_NULL);
714 ath_hal_free(ahp->ah_rfHal);
715 ahp->ah_rfHal = AH_NULL;
719 * Allocate memory for analog bank scratch buffers
720 * Scratch Buffer will be reinitialized every reset so no need to zero now
722 static HAL_BOOL
723 ar2413RfAttach(struct ath_hal *ah, HAL_STATUS *status)
725 struct ath_hal_5212 *ahp = AH5212(ah);
726 struct ar2413State *priv;
728 HALASSERT(ah->ah_magic == AR5212_MAGIC);
730 HALASSERT(ahp->ah_rfHal == AH_NULL);
731 priv = ath_hal_malloc(sizeof(struct ar2413State));
732 if (priv == AH_NULL) {
733 HALDEBUG(ah, HAL_DEBUG_ANY,
734 "%s: cannot allocate private state\n", __func__);
735 *status = HAL_ENOMEM; /* XXX */
736 return AH_FALSE;
738 priv->base.rfDetach = ar2413RfDetach;
739 priv->base.writeRegs = ar2413WriteRegs;
740 priv->base.getRfBank = ar2413GetRfBank;
741 priv->base.setChannel = ar2413SetChannel;
742 priv->base.setRfRegs = ar2413SetRfRegs;
743 priv->base.setPowerTable = ar2413SetPowerTable;
744 priv->base.getChannelMaxMinPower = ar2413GetChannelMaxMinPower;
745 priv->base.getNfAdjust = ar5212GetNfAdjust;
747 ahp->ah_pcdacTable = priv->pcdacTable;
748 ahp->ah_pcdacTableSize = sizeof(priv->pcdacTable);
749 ahp->ah_rfHal = &priv->base;
751 return AH_TRUE;
754 static HAL_BOOL
755 ar2413Probe(struct ath_hal *ah)
757 return IS_2413(ah);
759 AH_RF(RF2413, ar2413Probe, ar2413RfAttach);