Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / devs / networks / atheros5000 / hal / ar5212 / ar2413.c
blobceb63e611d37f74694e772be157fdd8d7102d268
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$
19 #include "opt_ah.h"
21 #ifdef AH_SUPPORT_2413
23 #include "ah.h"
24 #include "ah_internal.h"
26 #include "ar5212/ar5212.h"
27 #include "ar5212/ar5212reg.h"
28 #include "ar5212/ar5212phy.h"
30 #include "ah_eeprom_v3.h"
32 #define AH_5212_2413
33 #include "ar5212/ar5212.ini"
35 #define N(a) (sizeof(a)/sizeof(a[0]))
37 struct ar2413State {
38 RF_HAL_FUNCS base; /* public state, must be first */
39 uint16_t pcdacTable[PWR_TABLE_SIZE_2413];
41 uint32_t Bank1Data[N(ar5212Bank1_2413)];
42 uint32_t Bank2Data[N(ar5212Bank2_2413)];
43 uint32_t Bank3Data[N(ar5212Bank3_2413)];
44 uint32_t Bank6Data[N(ar5212Bank6_2413)];
45 uint32_t Bank7Data[N(ar5212Bank7_2413)];
48 * Private state for reduced stack usage.
50 /* filled out Vpd table for all pdGains (chanL) */
51 uint16_t vpdTable_L[MAX_NUM_PDGAINS_PER_CHANNEL]
52 [MAX_PWR_RANGE_IN_HALF_DB];
53 /* filled out Vpd table for all pdGains (chanR) */
54 uint16_t vpdTable_R[MAX_NUM_PDGAINS_PER_CHANNEL]
55 [MAX_PWR_RANGE_IN_HALF_DB];
56 /* filled out Vpd table for all pdGains (interpolated) */
57 uint16_t vpdTable_I[MAX_NUM_PDGAINS_PER_CHANNEL]
58 [MAX_PWR_RANGE_IN_HALF_DB];
60 #define AR2413(ah) ((struct ar2413State *) AH5212(ah)->ah_rfHal)
62 extern void ar5212ModifyRfBuffer(uint32_t *rfBuf, uint32_t reg32,
63 uint32_t numBits, uint32_t firstBit, uint32_t column);
65 static void
66 ar2413WriteRegs(struct ath_hal *ah, u_int modesIndex, u_int freqIndex,
67 int writes)
69 HAL_INI_WRITE_ARRAY(ah, ar5212Modes_2413, modesIndex, writes);
70 HAL_INI_WRITE_ARRAY(ah, ar5212Common_2413, 1, writes);
71 HAL_INI_WRITE_ARRAY(ah, ar5212BB_RfGain_2413, freqIndex, writes);
75 * Take the MHz channel value and set the Channel value
77 * ASSUMES: Writes enabled to analog bus
79 static HAL_BOOL
80 ar2413SetChannel(struct ath_hal *ah, HAL_CHANNEL_INTERNAL *chan)
82 uint32_t channelSel = 0;
83 uint32_t bModeSynth = 0;
84 uint32_t aModeRefSel = 0;
85 uint32_t reg32 = 0;
86 uint16_t freq;
88 OS_MARK(ah, AH_MARK_SETCHANNEL, chan->channel);
90 if (chan->channel < 4800) {
91 uint32_t txctl;
93 if (((chan->channel - 2192) % 5) == 0) {
94 channelSel = ((chan->channel - 672) * 2 - 3040)/10;
95 bModeSynth = 0;
96 } else if (((chan->channel - 2224) % 5) == 0) {
97 channelSel = ((chan->channel - 704) * 2 - 3040) / 10;
98 bModeSynth = 1;
99 } else {
100 HALDEBUG(ah, HAL_DEBUG_ANY,
101 "%s: invalid channel %u MHz\n",
102 __func__, chan->channel);
103 return AH_FALSE;
106 channelSel = (channelSel << 2) & 0xff;
107 channelSel = ath_hal_reverseBits(channelSel, 8);
109 txctl = OS_REG_READ(ah, AR_PHY_CCK_TX_CTRL);
110 if (chan->channel == 2484) {
111 /* Enable channel spreading for channel 14 */
112 OS_REG_WRITE(ah, AR_PHY_CCK_TX_CTRL,
113 txctl | AR_PHY_CCK_TX_CTRL_JAPAN);
114 } else {
115 OS_REG_WRITE(ah, AR_PHY_CCK_TX_CTRL,
116 txctl &~ AR_PHY_CCK_TX_CTRL_JAPAN);
118 } else if (((chan->channel % 5) == 2) && (chan->channel <= 5435)) {
119 freq = chan->channel - 2; /* Align to even 5MHz raster */
120 channelSel = ath_hal_reverseBits(
121 (uint32_t)(((freq - 4800)*10)/25 + 1), 8);
122 aModeRefSel = ath_hal_reverseBits(0, 2);
123 } else if ((chan->channel % 20) == 0 && chan->channel >= 5120) {
124 channelSel = ath_hal_reverseBits(
125 ((chan->channel - 4800) / 20 << 2), 8);
126 aModeRefSel = ath_hal_reverseBits(3, 2);
127 } else if ((chan->channel % 10) == 0) {
128 channelSel = ath_hal_reverseBits(
129 ((chan->channel - 4800) / 10 << 1), 8);
130 aModeRefSel = ath_hal_reverseBits(2, 2);
131 } else if ((chan->channel % 5) == 0) {
132 channelSel = ath_hal_reverseBits(
133 (chan->channel - 4800) / 5, 8);
134 aModeRefSel = ath_hal_reverseBits(1, 2);
135 } else {
136 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: invalid channel %u MHz\n",
137 __func__, chan->channel);
138 return AH_FALSE;
141 reg32 = (channelSel << 4) | (aModeRefSel << 2) | (bModeSynth << 1) |
142 (1 << 12) | 0x1;
143 OS_REG_WRITE(ah, AR_PHY(0x27), reg32 & 0xff);
145 reg32 >>= 8;
146 OS_REG_WRITE(ah, AR_PHY(0x36), reg32 & 0x7f);
148 AH_PRIVATE(ah)->ah_curchan = chan;
150 return AH_TRUE;
154 * Reads EEPROM header info from device structure and programs
155 * all rf registers
157 * REQUIRES: Access to the analog rf device
159 static HAL_BOOL
160 ar2413SetRfRegs(struct ath_hal *ah, HAL_CHANNEL_INTERNAL *chan, uint16_t modesIndex, uint16_t *rfXpdGain)
162 #define RF_BANK_SETUP(_priv, _ix, _col) do { \
163 int i; \
164 for (i = 0; i < N(ar5212Bank##_ix##_2413); i++) \
165 (_priv)->Bank##_ix##Data[i] = ar5212Bank##_ix##_2413[i][_col];\
166 } while (0)
167 struct ath_hal_5212 *ahp = AH5212(ah);
168 const HAL_EEPROM *ee = AH_PRIVATE(ah)->ah_eeprom;
169 uint16_t ob2GHz = 0, db2GHz = 0;
170 struct ar2413State *priv = AR2413(ah);
171 int regWrites = 0;
173 HALDEBUG(ah, HAL_DEBUG_RFPARAM,
174 "%s: chan 0x%x flag 0x%x modesIndex 0x%x\n",
175 __func__, chan->channel, chan->channelFlags, modesIndex);
177 HALASSERT(priv);
179 /* Setup rf parameters */
180 switch (chan->channelFlags & CHANNEL_ALL) {
181 case CHANNEL_B:
182 ob2GHz = ee->ee_obFor24;
183 db2GHz = ee->ee_dbFor24;
184 break;
185 case CHANNEL_G:
186 case CHANNEL_108G:
187 ob2GHz = ee->ee_obFor24g;
188 db2GHz = ee->ee_dbFor24g;
189 break;
190 default:
191 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: invalid channel flags 0x%x\n",
192 __func__, chan->channelFlags);
193 return AH_FALSE;
196 /* Bank 1 Write */
197 RF_BANK_SETUP(priv, 1, 1);
199 /* Bank 2 Write */
200 RF_BANK_SETUP(priv, 2, modesIndex);
202 /* Bank 3 Write */
203 RF_BANK_SETUP(priv, 3, modesIndex);
205 /* Bank 6 Write */
206 RF_BANK_SETUP(priv, 6, modesIndex);
208 ar5212ModifyRfBuffer(priv->Bank6Data, ob2GHz, 3, 168, 0);
209 ar5212ModifyRfBuffer(priv->Bank6Data, db2GHz, 3, 165, 0);
211 /* Bank 7 Setup */
212 RF_BANK_SETUP(priv, 7, modesIndex);
214 /* Write Analog registers */
215 HAL_INI_WRITE_BANK(ah, ar5212Bank1_2413, priv->Bank1Data, regWrites);
216 HAL_INI_WRITE_BANK(ah, ar5212Bank2_2413, priv->Bank2Data, regWrites);
217 HAL_INI_WRITE_BANK(ah, ar5212Bank3_2413, priv->Bank3Data, regWrites);
218 HAL_INI_WRITE_BANK(ah, ar5212Bank6_2413, priv->Bank6Data, regWrites);
219 HAL_INI_WRITE_BANK(ah, ar5212Bank7_2413, priv->Bank7Data, regWrites);
221 /* Now that we have reprogrammed rfgain value, clear the flag. */
222 ahp->ah_rfgainState = HAL_RFGAIN_INACTIVE;
224 return AH_TRUE;
225 #undef RF_BANK_SETUP
229 * Return a reference to the requested RF Bank.
231 static uint32_t *
232 ar2413GetRfBank(struct ath_hal *ah, int bank)
234 struct ar2413State *priv = AR2413(ah);
236 HALASSERT(priv != AH_NULL);
237 switch (bank) {
238 case 1: return priv->Bank1Data;
239 case 2: return priv->Bank2Data;
240 case 3: return priv->Bank3Data;
241 case 6: return priv->Bank6Data;
242 case 7: return priv->Bank7Data;
244 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: unknown RF Bank %d requested\n",
245 __func__, bank);
246 return AH_NULL;
250 * Return indices surrounding the value in sorted integer lists.
252 * NB: the input list is assumed to be sorted in ascending order
254 static void
255 GetLowerUpperIndex(int16_t v, const uint16_t *lp, uint16_t listSize,
256 uint32_t *vlo, uint32_t *vhi)
258 int16_t target = v;
259 const uint16_t *ep = lp+listSize;
260 const uint16_t *tp;
263 * Check first and last elements for out-of-bounds conditions.
265 if (target < lp[0]) {
266 *vlo = *vhi = 0;
267 return;
269 if (target >= ep[-1]) {
270 *vlo = *vhi = listSize - 1;
271 return;
274 /* look for value being near or between 2 values in list */
275 for (tp = lp; tp < ep; tp++) {
277 * If value is close to the current value of the list
278 * then target is not between values, it is one of the values
280 if (*tp == target) {
281 *vlo = *vhi = tp - (const uint16_t *) lp;
282 return;
285 * Look for value being between current value and next value
286 * if so return these 2 values
288 if (target < tp[1]) {
289 *vlo = tp - (const uint16_t *) lp;
290 *vhi = *vlo + 1;
291 return;
297 * Fill the Vpdlist for indices Pmax-Pmin
299 static HAL_BOOL
300 ar2413FillVpdTable(uint32_t pdGainIdx, int16_t Pmin, int16_t Pmax,
301 const int16_t *pwrList, const uint16_t *VpdList,
302 uint16_t numIntercepts, uint16_t retVpdList[][64])
304 uint16_t ii, kk;
305 int16_t currPwr = (int16_t)(2*Pmin);
306 /* since Pmin is pwr*2 and pwrList is 4*pwr */
307 uint32_t idxL, idxR;
309 ii = 0;
311 if (numIntercepts < 2)
312 return AH_FALSE;
314 while (ii <= (uint16_t)(Pmax - Pmin)) {
315 GetLowerUpperIndex(currPwr, (const uint16_t *) pwrList,
316 numIntercepts, &(idxL), &(idxR));
317 if (idxR < 1)
318 idxR = 1; /* extrapolate below */
319 if (idxL == (uint32_t)(numIntercepts - 1))
320 idxL = numIntercepts - 2; /* extrapolate above */
321 if (pwrList[idxL] == pwrList[idxR])
322 kk = VpdList[idxL];
323 else
324 kk = (uint16_t)
325 (((currPwr - pwrList[idxL])*VpdList[idxR]+
326 (pwrList[idxR] - currPwr)*VpdList[idxL])/
327 (pwrList[idxR] - pwrList[idxL]));
328 retVpdList[pdGainIdx][ii] = kk;
329 ii++;
330 currPwr += 2; /* half dB steps */
333 return AH_TRUE;
337 * Returns interpolated or the scaled up interpolated value
339 static int16_t
340 interpolate_signed(uint16_t target, uint16_t srcLeft, uint16_t srcRight,
341 int16_t targetLeft, int16_t targetRight)
343 int16_t rv;
345 if (srcRight != srcLeft) {
346 rv = ((target - srcLeft)*targetRight +
347 (srcRight - target)*targetLeft) / (srcRight - srcLeft);
348 } else {
349 rv = targetLeft;
351 return rv;
355 * Uses the data points read from EEPROM to reconstruct the pdadc power table
356 * Called by ar2413SetPowerTable()
358 static int
359 ar2413getGainBoundariesAndPdadcsForPowers(struct ath_hal *ah, uint16_t channel,
360 const RAW_DATA_STRUCT_2413 *pRawDataset,
361 uint16_t pdGainOverlap_t2,
362 int16_t *pMinCalPower, uint16_t pPdGainBoundaries[],
363 uint16_t pPdGainValues[], uint16_t pPDADCValues[])
365 struct ar2413State *priv = AR2413(ah);
366 #define VpdTable_L priv->vpdTable_L
367 #define VpdTable_R priv->vpdTable_R
368 #define VpdTable_I priv->vpdTable_I
369 uint32_t ii, jj, kk;
370 int32_t ss;/* potentially -ve index for taking care of pdGainOverlap */
371 uint32_t idxL, idxR;
372 uint32_t numPdGainsUsed = 0;
374 * If desired to support -ve power levels in future, just
375 * change pwr_I_0 to signed 5-bits.
377 int16_t Pmin_t2[MAX_NUM_PDGAINS_PER_CHANNEL];
378 /* to accomodate -ve power levels later on. */
379 int16_t Pmax_t2[MAX_NUM_PDGAINS_PER_CHANNEL];
380 /* to accomodate -ve power levels later on */
381 uint16_t numVpd = 0;
382 uint16_t Vpd_step;
383 int16_t tmpVal ;
384 uint32_t sizeCurrVpdTable, maxIndex, tgtIndex;
386 /* Get upper lower index */
387 GetLowerUpperIndex(channel, pRawDataset->pChannels,
388 pRawDataset->numChannels, &(idxL), &(idxR));
390 for (ii = 0; ii < MAX_NUM_PDGAINS_PER_CHANNEL; ii++) {
391 jj = MAX_NUM_PDGAINS_PER_CHANNEL - ii - 1;
392 /* work backwards 'cause highest pdGain for lowest power */
393 numVpd = pRawDataset->pDataPerChannel[idxL].pDataPerPDGain[jj].numVpd;
394 if (numVpd > 0) {
395 pPdGainValues[numPdGainsUsed] = pRawDataset->pDataPerChannel[idxL].pDataPerPDGain[jj].pd_gain;
396 Pmin_t2[numPdGainsUsed] = pRawDataset->pDataPerChannel[idxL].pDataPerPDGain[jj].pwr_t4[0];
397 if (Pmin_t2[numPdGainsUsed] >pRawDataset->pDataPerChannel[idxR].pDataPerPDGain[jj].pwr_t4[0]) {
398 Pmin_t2[numPdGainsUsed] = pRawDataset->pDataPerChannel[idxR].pDataPerPDGain[jj].pwr_t4[0];
400 Pmin_t2[numPdGainsUsed] = (int16_t)
401 (Pmin_t2[numPdGainsUsed] / 2);
402 Pmax_t2[numPdGainsUsed] = pRawDataset->pDataPerChannel[idxL].pDataPerPDGain[jj].pwr_t4[numVpd-1];
403 if (Pmax_t2[numPdGainsUsed] > pRawDataset->pDataPerChannel[idxR].pDataPerPDGain[jj].pwr_t4[numVpd-1])
404 Pmax_t2[numPdGainsUsed] =
405 pRawDataset->pDataPerChannel[idxR].pDataPerPDGain[jj].pwr_t4[numVpd-1];
406 Pmax_t2[numPdGainsUsed] = (int16_t)(Pmax_t2[numPdGainsUsed] / 2);
407 ar2413FillVpdTable(
408 numPdGainsUsed, Pmin_t2[numPdGainsUsed], Pmax_t2[numPdGainsUsed],
409 &(pRawDataset->pDataPerChannel[idxL].pDataPerPDGain[jj].pwr_t4[0]),
410 &(pRawDataset->pDataPerChannel[idxL].pDataPerPDGain[jj].Vpd[0]), numVpd, VpdTable_L
412 ar2413FillVpdTable(
413 numPdGainsUsed, Pmin_t2[numPdGainsUsed], Pmax_t2[numPdGainsUsed],
414 &(pRawDataset->pDataPerChannel[idxR].pDataPerPDGain[jj].pwr_t4[0]),
415 &(pRawDataset->pDataPerChannel[idxR].pDataPerPDGain[jj].Vpd[0]), numVpd, VpdTable_R
417 for (kk = 0; kk < (uint16_t)(Pmax_t2[numPdGainsUsed] - Pmin_t2[numPdGainsUsed]); kk++) {
418 VpdTable_I[numPdGainsUsed][kk] =
419 interpolate_signed(
420 channel, pRawDataset->pChannels[idxL], pRawDataset->pChannels[idxR],
421 (int16_t)VpdTable_L[numPdGainsUsed][kk], (int16_t)VpdTable_R[numPdGainsUsed][kk]);
423 /* fill VpdTable_I for this pdGain */
424 numPdGainsUsed++;
426 /* if this pdGain is used */
429 *pMinCalPower = Pmin_t2[0];
430 kk = 0; /* index for the final table */
431 for (ii = 0; ii < numPdGainsUsed; ii++) {
432 if (ii == (numPdGainsUsed - 1))
433 pPdGainBoundaries[ii] = Pmax_t2[ii] +
434 PD_GAIN_BOUNDARY_STRETCH_IN_HALF_DB;
435 else
436 pPdGainBoundaries[ii] = (uint16_t)
437 ((Pmax_t2[ii] + Pmin_t2[ii+1]) / 2 );
438 if (pPdGainBoundaries[ii] > 63) {
439 HALDEBUG(ah, HAL_DEBUG_ANY,
440 "%s: clamp pPdGainBoundaries[%d] %d\n",
441 __func__, ii, pPdGainBoundaries[ii]);/*XXX*/
442 pPdGainBoundaries[ii] = 63;
445 /* Find starting index for this pdGain */
446 if (ii == 0)
447 ss = 0; /* for the first pdGain, start from index 0 */
448 else
449 ss = (pPdGainBoundaries[ii-1] - Pmin_t2[ii]) -
450 pdGainOverlap_t2;
451 Vpd_step = (uint16_t)(VpdTable_I[ii][1] - VpdTable_I[ii][0]);
452 Vpd_step = (uint16_t)((Vpd_step < 1) ? 1 : Vpd_step);
454 *-ve ss indicates need to extrapolate data below for this pdGain
456 while (ss < 0) {
457 tmpVal = (int16_t)(VpdTable_I[ii][0] + ss*Vpd_step);
458 pPDADCValues[kk++] = (uint16_t)((tmpVal < 0) ? 0 : tmpVal);
459 ss++;
462 sizeCurrVpdTable = Pmax_t2[ii] - Pmin_t2[ii];
463 tgtIndex = pPdGainBoundaries[ii] + pdGainOverlap_t2 - Pmin_t2[ii];
464 maxIndex = (tgtIndex < sizeCurrVpdTable) ? tgtIndex : sizeCurrVpdTable;
466 while (ss < (int16_t)maxIndex)
467 pPDADCValues[kk++] = VpdTable_I[ii][ss++];
469 Vpd_step = (uint16_t)(VpdTable_I[ii][sizeCurrVpdTable-1] -
470 VpdTable_I[ii][sizeCurrVpdTable-2]);
471 Vpd_step = (uint16_t)((Vpd_step < 1) ? 1 : Vpd_step);
473 * for last gain, pdGainBoundary == Pmax_t2, so will
474 * have to extrapolate
476 if (tgtIndex > maxIndex) { /* need to extrapolate above */
477 while(ss < (int16_t)tgtIndex) {
478 tmpVal = (uint16_t)
479 (VpdTable_I[ii][sizeCurrVpdTable-1] +
480 (ss-maxIndex)*Vpd_step);
481 pPDADCValues[kk++] = (tmpVal > 127) ?
482 127 : tmpVal;
483 ss++;
485 } /* extrapolated above */
486 } /* for all pdGainUsed */
488 while (ii < MAX_NUM_PDGAINS_PER_CHANNEL) {
489 pPdGainBoundaries[ii] = pPdGainBoundaries[ii-1];
490 ii++;
492 while (kk < 128) {
493 pPDADCValues[kk] = pPDADCValues[kk-1];
494 kk++;
497 return numPdGainsUsed;
498 #undef VpdTable_L
499 #undef VpdTable_R
500 #undef VpdTable_I
503 static HAL_BOOL
504 ar2413SetPowerTable(struct ath_hal *ah,
505 int16_t *minPower, int16_t *maxPower, HAL_CHANNEL_INTERNAL *chan,
506 uint16_t *rfXpdGain)
508 struct ath_hal_5212 *ahp = AH5212(ah);
509 const HAL_EEPROM *ee = AH_PRIVATE(ah)->ah_eeprom;
510 const RAW_DATA_STRUCT_2413 *pRawDataset = AH_NULL;
511 uint16_t pdGainOverlap_t2;
512 int16_t minCalPower2413_t2;
513 uint16_t *pdadcValues = ahp->ah_pcdacTable;
514 uint16_t gainBoundaries[4];
515 uint32_t i, reg32, regoffset, tpcrg1;
516 int numPdGainsUsed;
518 HALDEBUG(ah, HAL_DEBUG_RFPARAM, "%s: chan 0x%x flag 0x%x\n",
519 __func__, chan->channel,chan->channelFlags);
521 if (IS_CHAN_G(chan) || IS_CHAN_108G(chan))
522 pRawDataset = &ee->ee_rawDataset2413[headerInfo11G];
523 else if (IS_CHAN_B(chan))
524 pRawDataset = &ee->ee_rawDataset2413[headerInfo11B];
525 else {
526 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: illegal mode\n", __func__);
527 return AH_FALSE;
530 pdGainOverlap_t2 = (uint16_t) SM(OS_REG_READ(ah, AR_PHY_TPCRG5),
531 AR_PHY_TPCRG5_PD_GAIN_OVERLAP);
533 numPdGainsUsed = ar2413getGainBoundariesAndPdadcsForPowers(ah,
534 chan->channel, pRawDataset, pdGainOverlap_t2,
535 &minCalPower2413_t2,gainBoundaries, rfXpdGain, pdadcValues);
536 HALASSERT(1 <= numPdGainsUsed && numPdGainsUsed <= 3);
538 #if 0
539 OS_REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_NUM_PD_GAIN,
540 (pRawDataset->pDataPerChannel[0].numPdGains - 1));
541 #endif
542 tpcrg1 = OS_REG_READ(ah, AR_PHY_TPCRG1);
543 tpcrg1 = (tpcrg1 &~ AR_PHY_TPCRG1_NUM_PD_GAIN)
544 | SM(numPdGainsUsed-1, AR_PHY_TPCRG1_NUM_PD_GAIN);
545 switch (numPdGainsUsed) {
546 case 3:
547 tpcrg1 &= ~AR_PHY_TPCRG1_PDGAIN_SETTING3;
548 tpcrg1 |= SM(rfXpdGain[2], AR_PHY_TPCRG1_PDGAIN_SETTING3);
549 /* fall thru... */
550 case 2:
551 tpcrg1 &= ~AR_PHY_TPCRG1_PDGAIN_SETTING2;
552 tpcrg1 |= SM(rfXpdGain[1], AR_PHY_TPCRG1_PDGAIN_SETTING2);
553 /* fall thru... */
554 case 1:
555 tpcrg1 &= ~AR_PHY_TPCRG1_PDGAIN_SETTING1;
556 tpcrg1 |= SM(rfXpdGain[0], AR_PHY_TPCRG1_PDGAIN_SETTING1);
557 break;
559 #ifdef AH_DEBUG
560 if (tpcrg1 != OS_REG_READ(ah, AR_PHY_TPCRG1))
561 HALDEBUG(ah, HAL_DEBUG_RFPARAM, "%s: using non-default "
562 "pd_gains (default 0x%x, calculated 0x%x)\n",
563 __func__, OS_REG_READ(ah, AR_PHY_TPCRG1), tpcrg1);
564 #endif
565 OS_REG_WRITE(ah, AR_PHY_TPCRG1, tpcrg1);
568 * Note the pdadc table may not start at 0 dBm power, could be
569 * negative or greater than 0. Need to offset the power
570 * values by the amount of minPower for griffin
572 if (minCalPower2413_t2 != 0)
573 ahp->ah_txPowerIndexOffset = (int16_t)(0 - minCalPower2413_t2);
574 else
575 ahp->ah_txPowerIndexOffset = 0;
577 /* Finally, write the power values into the baseband power table */
578 regoffset = 0x9800 + (672 <<2); /* beginning of pdadc table in griffin */
579 for (i = 0; i < 32; i++) {
580 reg32 = ((pdadcValues[4*i + 0] & 0xFF) << 0) |
581 ((pdadcValues[4*i + 1] & 0xFF) << 8) |
582 ((pdadcValues[4*i + 2] & 0xFF) << 16) |
583 ((pdadcValues[4*i + 3] & 0xFF) << 24) ;
584 OS_REG_WRITE(ah, regoffset, reg32);
585 regoffset += 4;
588 OS_REG_WRITE(ah, AR_PHY_TPCRG5,
589 SM(pdGainOverlap_t2, AR_PHY_TPCRG5_PD_GAIN_OVERLAP) |
590 SM(gainBoundaries[0], AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_1) |
591 SM(gainBoundaries[1], AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_2) |
592 SM(gainBoundaries[2], AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_3) |
593 SM(gainBoundaries[3], AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_4));
595 return AH_TRUE;
598 static int16_t
599 ar2413GetMinPower(struct ath_hal *ah, const RAW_DATA_PER_CHANNEL_2413 *data)
601 uint32_t ii,jj;
602 uint16_t Pmin=0,numVpd;
604 for (ii = 0; ii < MAX_NUM_PDGAINS_PER_CHANNEL; ii++) {
605 jj = MAX_NUM_PDGAINS_PER_CHANNEL - ii - 1;
606 /* work backwards 'cause highest pdGain for lowest power */
607 numVpd = data->pDataPerPDGain[jj].numVpd;
608 if (numVpd > 0) {
609 Pmin = data->pDataPerPDGain[jj].pwr_t4[0];
610 return(Pmin);
613 return(Pmin);
616 static int16_t
617 ar2413GetMaxPower(struct ath_hal *ah, const RAW_DATA_PER_CHANNEL_2413 *data)
619 uint32_t ii;
620 uint16_t Pmax=0,numVpd;
622 for (ii=0; ii< MAX_NUM_PDGAINS_PER_CHANNEL; ii++) {
623 /* work forwards cuase lowest pdGain for highest power */
624 numVpd = data->pDataPerPDGain[ii].numVpd;
625 if (numVpd > 0) {
626 Pmax = data->pDataPerPDGain[ii].pwr_t4[numVpd-1];
627 return(Pmax);
630 return(Pmax);
633 static HAL_BOOL
634 ar2413GetChannelMaxMinPower(struct ath_hal *ah, HAL_CHANNEL *chan,
635 int16_t *maxPow, int16_t *minPow)
637 const HAL_EEPROM *ee = AH_PRIVATE(ah)->ah_eeprom;
638 const RAW_DATA_STRUCT_2413 *pRawDataset = AH_NULL;
639 const RAW_DATA_PER_CHANNEL_2413 *data = AH_NULL;
640 uint16_t numChannels;
641 int totalD,totalF, totalMin,last, i;
643 *maxPow = 0;
645 if (IS_CHAN_G(chan) || IS_CHAN_108G(chan))
646 pRawDataset = &ee->ee_rawDataset2413[headerInfo11G];
647 else if (IS_CHAN_B(chan))
648 pRawDataset = &ee->ee_rawDataset2413[headerInfo11B];
649 else
650 return(AH_FALSE);
652 numChannels = pRawDataset->numChannels;
653 data = pRawDataset->pDataPerChannel;
655 /* Make sure the channel is in the range of the TP values
656 * (freq piers)
658 if (numChannels < 1)
659 return(AH_FALSE);
661 if ((chan->channel < data[0].channelValue) ||
662 (chan->channel > data[numChannels-1].channelValue)) {
663 if (chan->channel < data[0].channelValue) {
664 *maxPow = ar2413GetMaxPower(ah, &data[0]);
665 *minPow = ar2413GetMinPower(ah, &data[0]);
666 return(AH_TRUE);
667 } else {
668 *maxPow = ar2413GetMaxPower(ah, &data[numChannels - 1]);
669 *minPow = ar2413GetMinPower(ah, &data[numChannels - 1]);
670 return(AH_TRUE);
674 /* Linearly interpolate the power value now */
675 for (last=0,i=0; (i<numChannels) && (chan->channel > data[i].channelValue);
676 last = i++);
677 totalD = data[i].channelValue - data[last].channelValue;
678 if (totalD > 0) {
679 totalF = ar2413GetMaxPower(ah, &data[i]) - ar2413GetMaxPower(ah, &data[last]);
680 *maxPow = (int8_t) ((totalF*(chan->channel-data[last].channelValue) +
681 ar2413GetMaxPower(ah, &data[last])*totalD)/totalD);
682 totalMin = ar2413GetMinPower(ah, &data[i]) - ar2413GetMinPower(ah, &data[last]);
683 *minPow = (int8_t) ((totalMin*(chan->channel-data[last].channelValue) +
684 ar2413GetMinPower(ah, &data[last])*totalD)/totalD);
685 return(AH_TRUE);
686 } else {
687 if (chan->channel == data[i].channelValue) {
688 *maxPow = ar2413GetMaxPower(ah, &data[i]);
689 *minPow = ar2413GetMinPower(ah, &data[i]);
690 return(AH_TRUE);
691 } else
692 return(AH_FALSE);
697 * Free memory for analog bank scratch buffers
699 static void
700 ar2413RfDetach(struct ath_hal *ah)
702 struct ath_hal_5212 *ahp = AH5212(ah);
704 HALASSERT(ahp->ah_rfHal != AH_NULL);
705 ath_hal_free(ahp->ah_rfHal);
706 ahp->ah_rfHal = AH_NULL;
710 * Allocate memory for analog bank scratch buffers
711 * Scratch Buffer will be reinitialized every reset so no need to zero now
713 HAL_BOOL
714 ar2413RfAttach(struct ath_hal *ah, HAL_STATUS *status)
716 struct ath_hal_5212 *ahp = AH5212(ah);
717 struct ar2413State *priv;
719 HALASSERT(ah->ah_magic == AR5212_MAGIC);
721 HALASSERT(ahp->ah_rfHal == AH_NULL);
722 priv = ath_hal_malloc(sizeof(struct ar2413State));
723 if (priv == AH_NULL) {
724 HALDEBUG(ah, HAL_DEBUG_ANY,
725 "%s: cannot allocate private state\n", __func__);
726 *status = HAL_ENOMEM; /* XXX */
727 return AH_FALSE;
729 priv->base.rfDetach = ar2413RfDetach;
730 priv->base.writeRegs = ar2413WriteRegs;
731 priv->base.getRfBank = ar2413GetRfBank;
732 priv->base.setChannel = ar2413SetChannel;
733 priv->base.setRfRegs = ar2413SetRfRegs;
734 priv->base.setPowerTable = ar2413SetPowerTable;
735 priv->base.getChannelMaxMinPower = ar2413GetChannelMaxMinPower;
736 priv->base.getNfAdjust = ar5212GetNfAdjust;
738 ahp->ah_pcdacTable = priv->pcdacTable;
739 ahp->ah_pcdacTableSize = sizeof(priv->pcdacTable);
740 ahp->ah_rfHal = &priv->base;
742 return AH_TRUE;
744 #endif /* AH_SUPPORT_2413 */