kernel: Use NELEM() where we can.
[dragonfly.git] / sys / dev / netif / ath / hal / ath_hal / ar5212 / ar2316.c
1 /*
2  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
3  * Copyright (c) 2002-2008 Atheros Communications, Inc.
4  *
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.
8  *
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.
16  *
17  * $FreeBSD: head/sys/dev/ath/ath_hal/ar5212/ar2316.c 187831 2009-01-28 18:00:22Z sam $
18  * $DragonFly$
19  */
20 #include "opt_ah.h"
21
22 #include "ah.h"
23 #include "ah_internal.h"
24
25 #include "ar5212/ar5212.h"
26 #include "ar5212/ar5212reg.h"
27 #include "ar5212/ar5212phy.h"
28
29 #include "ah_eeprom_v3.h"
30
31 #define AH_5212_2316
32 #include "ar5212/ar5212.ini"
33
34 typedef RAW_DATA_STRUCT_2413 RAW_DATA_STRUCT_2316;
35 typedef RAW_DATA_PER_CHANNEL_2413 RAW_DATA_PER_CHANNEL_2316;
36 #define PWR_TABLE_SIZE_2316 PWR_TABLE_SIZE_2413
37
38 struct ar2316State {
39         RF_HAL_FUNCS    base;           /* public state, must be first */
40         uint16_t        pcdacTable[PWR_TABLE_SIZE_2316];
41
42         uint32_t        Bank1Data[NELEM(ar5212Bank1_2316)];
43         uint32_t        Bank2Data[NELEM(ar5212Bank2_2316)];
44         uint32_t        Bank3Data[NELEM(ar5212Bank3_2316)];
45         uint32_t        Bank6Data[NELEM(ar5212Bank6_2316)];
46         uint32_t        Bank7Data[NELEM(ar5212Bank7_2316)];
47
48         /*
49          * Private state for reduced stack usage.
50          */
51         /* filled out Vpd table for all pdGains (chanL) */
52         uint16_t vpdTable_L[MAX_NUM_PDGAINS_PER_CHANNEL]
53                             [MAX_PWR_RANGE_IN_HALF_DB];
54         /* filled out Vpd table for all pdGains (chanR) */
55         uint16_t vpdTable_R[MAX_NUM_PDGAINS_PER_CHANNEL]
56                             [MAX_PWR_RANGE_IN_HALF_DB];
57         /* filled out Vpd table for all pdGains (interpolated) */
58         uint16_t vpdTable_I[MAX_NUM_PDGAINS_PER_CHANNEL]
59                             [MAX_PWR_RANGE_IN_HALF_DB];
60 };
61 #define AR2316(ah)      ((struct ar2316State *) AH5212(ah)->ah_rfHal)
62
63 extern  void ar5212ModifyRfBuffer(uint32_t *rfBuf, uint32_t reg32,
64                 uint32_t numBits, uint32_t firstBit, uint32_t column);
65
66 static void
67 ar2316WriteRegs(struct ath_hal *ah, u_int modesIndex, u_int freqIndex,
68         int regWrites)
69 {
70         struct ath_hal_5212 *ahp = AH5212(ah);
71
72         HAL_INI_WRITE_ARRAY(ah, ar5212Modes_2316, modesIndex, regWrites);
73         HAL_INI_WRITE_ARRAY(ah, ar5212Common_2316, 1, regWrites);
74         HAL_INI_WRITE_ARRAY(ah, ar5212BB_RfGain_2316, freqIndex, regWrites);
75
76         /* For AP51 */
77         if (!ahp->ah_cwCalRequire) {
78                 OS_REG_WRITE(ah, 0xa358, (OS_REG_READ(ah, 0xa358) & ~0x2));
79         } else {
80                 ahp->ah_cwCalRequire = AH_FALSE;
81         }
82 }
83
84 /*
85  * Take the MHz channel value and set the Channel value
86  *
87  * ASSUMES: Writes enabled to analog bus
88  */
89 static HAL_BOOL
90 ar2316SetChannel(struct ath_hal *ah,  struct ieee80211_channel *chan)
91 {
92         uint16_t freq = ath_hal_gethwchannel(ah, chan);
93         uint32_t channelSel  = 0;
94         uint32_t bModeSynth  = 0;
95         uint32_t aModeRefSel = 0;
96         uint32_t reg32       = 0;
97
98         OS_MARK(ah, AH_MARK_SETCHANNEL, freq);
99
100         if (freq < 4800) {
101                 uint32_t txctl;
102
103                 if (((freq - 2192) % 5) == 0) {
104                         channelSel = ((freq - 672) * 2 - 3040)/10;
105                         bModeSynth = 0;
106                 } else if (((freq - 2224) % 5) == 0) {
107                         channelSel = ((freq - 704) * 2 - 3040) / 10;
108                         bModeSynth = 1;
109                 } else {
110                         HALDEBUG(ah, HAL_DEBUG_ANY,
111                             "%s: invalid channel %u MHz\n",
112                             __func__, freq);
113                         return AH_FALSE;
114                 }
115
116                 channelSel = (channelSel << 2) & 0xff;
117                 channelSel = ath_hal_reverseBits(channelSel, 8);
118
119                 txctl = OS_REG_READ(ah, AR_PHY_CCK_TX_CTRL);
120                 if (freq == 2484) {
121                         /* Enable channel spreading for channel 14 */
122                         OS_REG_WRITE(ah, AR_PHY_CCK_TX_CTRL,
123                                 txctl | AR_PHY_CCK_TX_CTRL_JAPAN);
124                 } else {
125                         OS_REG_WRITE(ah, AR_PHY_CCK_TX_CTRL,
126                                 txctl &~ AR_PHY_CCK_TX_CTRL_JAPAN);
127                 }
128         } else if ((freq % 20) == 0 && freq >= 5120) {
129                 channelSel = ath_hal_reverseBits(
130                         ((freq - 4800) / 20 << 2), 8);
131                 aModeRefSel = ath_hal_reverseBits(3, 2);
132         } else if ((freq % 10) == 0) {
133                 channelSel = ath_hal_reverseBits(
134                         ((freq - 4800) / 10 << 1), 8);
135                 aModeRefSel = ath_hal_reverseBits(2, 2);
136         } else if ((freq % 5) == 0) {
137                 channelSel = ath_hal_reverseBits(
138                         (freq - 4800) / 5, 8);
139                 aModeRefSel = ath_hal_reverseBits(1, 2);
140         } else {
141                 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: invalid channel %u MHz\n",
142                     __func__, freq);
143                 return AH_FALSE;
144         }
145
146         reg32 = (channelSel << 4) | (aModeRefSel << 2) | (bModeSynth << 1) |
147                         (1 << 12) | 0x1;
148         OS_REG_WRITE(ah, AR_PHY(0x27), reg32 & 0xff);
149
150         reg32 >>= 8;
151         OS_REG_WRITE(ah, AR_PHY(0x36), reg32 & 0x7f);
152
153         AH_PRIVATE(ah)->ah_curchan = chan;
154         return AH_TRUE;
155 }
156
157 /*
158  * Reads EEPROM header info from device structure and programs
159  * all rf registers
160  *
161  * REQUIRES: Access to the analog rf device
162  */
163 static HAL_BOOL
164 ar2316SetRfRegs(struct ath_hal *ah, const struct ieee80211_channel *chan,
165         uint16_t modesIndex, uint16_t *rfXpdGain)
166 {
167 #define RF_BANK_SETUP(_priv, _ix, _col) do {                                \
168         int i;                                                              \
169         for (i = 0; i < NELEM(ar5212Bank##_ix##_2316); i++)                 \
170                 (_priv)->Bank##_ix##Data[i] = ar5212Bank##_ix##_2316[i][_col];\
171 } while (0)
172         struct ath_hal_5212 *ahp = AH5212(ah);
173         const HAL_EEPROM *ee = AH_PRIVATE(ah)->ah_eeprom;
174         uint16_t ob2GHz = 0, db2GHz = 0;
175         struct ar2316State *priv = AR2316(ah);
176         int regWrites = 0;
177
178         HALDEBUG(ah, HAL_DEBUG_RFPARAM, "%s: chan %u/0x%x modesIndex %u\n",
179             __func__, chan->ic_freq, chan->ic_flags, modesIndex);
180
181         HALASSERT(priv != AH_NULL);
182
183         /* Setup rf parameters */
184         if (IEEE80211_IS_CHAN_B(chan)) {
185                 ob2GHz = ee->ee_obFor24;
186                 db2GHz = ee->ee_dbFor24;
187         } else {
188                 ob2GHz = ee->ee_obFor24g;
189                 db2GHz = ee->ee_dbFor24g;
190         }
191
192         /* Bank 1 Write */
193         RF_BANK_SETUP(priv, 1, 1);
194
195         /* Bank 2 Write */
196         RF_BANK_SETUP(priv, 2, modesIndex);
197
198         /* Bank 3 Write */
199         RF_BANK_SETUP(priv, 3, modesIndex);
200
201         /* Bank 6 Write */
202         RF_BANK_SETUP(priv, 6, modesIndex);
203
204         ar5212ModifyRfBuffer(priv->Bank6Data, ob2GHz,   3, 178, 0);
205         ar5212ModifyRfBuffer(priv->Bank6Data, db2GHz,   3, 175, 0);
206
207         /* Bank 7 Setup */
208         RF_BANK_SETUP(priv, 7, modesIndex);
209
210         /* Write Analog registers */
211         HAL_INI_WRITE_BANK(ah, ar5212Bank1_2316, priv->Bank1Data, regWrites);
212         HAL_INI_WRITE_BANK(ah, ar5212Bank2_2316, priv->Bank2Data, regWrites);
213         HAL_INI_WRITE_BANK(ah, ar5212Bank3_2316, priv->Bank3Data, regWrites);
214         HAL_INI_WRITE_BANK(ah, ar5212Bank6_2316, priv->Bank6Data, regWrites);
215         HAL_INI_WRITE_BANK(ah, ar5212Bank7_2316, priv->Bank7Data, regWrites);
216
217         /* Now that we have reprogrammed rfgain value, clear the flag. */
218         ahp->ah_rfgainState = HAL_RFGAIN_INACTIVE;
219
220         return AH_TRUE;
221 #undef  RF_BANK_SETUP
222 }
223
224 /*
225  * Return a reference to the requested RF Bank.
226  */
227 static uint32_t *
228 ar2316GetRfBank(struct ath_hal *ah, int bank)
229 {
230         struct ar2316State *priv = AR2316(ah);
231
232         HALASSERT(priv != AH_NULL);
233         switch (bank) {
234         case 1: return priv->Bank1Data;
235         case 2: return priv->Bank2Data;
236         case 3: return priv->Bank3Data;
237         case 6: return priv->Bank6Data;
238         case 7: return priv->Bank7Data;
239         }
240         HALDEBUG(ah, HAL_DEBUG_ANY, "%s: unknown RF Bank %d requested\n",
241             __func__, bank);
242         return AH_NULL;
243 }
244
245 /*
246  * Return indices surrounding the value in sorted integer lists.
247  *
248  * NB: the input list is assumed to be sorted in ascending order
249  */
250 static void
251 GetLowerUpperIndex(int16_t v, const uint16_t *lp, uint16_t listSize,
252                           uint32_t *vlo, uint32_t *vhi)
253 {
254         int16_t target = v;
255         const int16_t *ep = lp+listSize;
256         const int16_t *tp;
257
258         /*
259          * Check first and last elements for out-of-bounds conditions.
260          */
261         if (target < lp[0]) {
262                 *vlo = *vhi = 0;
263                 return;
264         }
265         if (target >= ep[-1]) {
266                 *vlo = *vhi = listSize - 1;
267                 return;
268         }
269
270         /* look for value being near or between 2 values in list */
271         for (tp = lp; tp < ep; tp++) {
272                 /*
273                  * If value is close to the current value of the list
274                  * then target is not between values, it is one of the values
275                  */
276                 if (*tp == target) {
277                         *vlo = *vhi = tp - (const int16_t *) lp;
278                         return;
279                 }
280                 /*
281                  * Look for value being between current value and next value
282                  * if so return these 2 values
283                  */
284                 if (target < tp[1]) {
285                         *vlo = tp - (const int16_t *) lp;
286                         *vhi = *vlo + 1;
287                         return;
288                 }
289         }
290 }
291
292 /*
293  * Fill the Vpdlist for indices Pmax-Pmin
294  */
295 static HAL_BOOL
296 ar2316FillVpdTable(uint32_t pdGainIdx, int16_t Pmin, int16_t  Pmax,
297                    const int16_t *pwrList, const int16_t *VpdList,
298                    uint16_t numIntercepts, uint16_t retVpdList[][64])
299 {
300         uint16_t ii, jj, kk;
301         int16_t currPwr = (int16_t)(2*Pmin);
302         /* since Pmin is pwr*2 and pwrList is 4*pwr */
303         uint32_t  idxL, idxR;
304
305         ii = 0;
306         jj = 0;
307
308         if (numIntercepts < 2)
309                 return AH_FALSE;
310
311         while (ii <= (uint16_t)(Pmax - Pmin)) {
312                 GetLowerUpperIndex(currPwr, pwrList, numIntercepts, 
313                                          &(idxL), &(idxR));
314                 if (idxR < 1)
315                         idxR = 1;                       /* extrapolate below */
316                 if (idxL == (uint32_t)(numIntercepts - 1))
317                         idxL = numIntercepts - 2;       /* extrapolate above */
318                 if (pwrList[idxL] == pwrList[idxR])
319                         kk = VpdList[idxL];
320                 else
321                         kk = (uint16_t)
322                                 (((currPwr - pwrList[idxL])*VpdList[idxR]+ 
323                                   (pwrList[idxR] - currPwr)*VpdList[idxL])/
324                                  (pwrList[idxR] - pwrList[idxL]));
325                 retVpdList[pdGainIdx][ii] = kk;
326                 ii++;
327                 currPwr += 2;                           /* half dB steps */
328         }
329
330         return AH_TRUE;
331 }
332
333 /*
334  * Returns interpolated or the scaled up interpolated value
335  */
336 static int16_t
337 interpolate_signed(uint16_t target, uint16_t srcLeft, uint16_t srcRight,
338         int16_t targetLeft, int16_t targetRight)
339 {
340         int16_t rv;
341
342         if (srcRight != srcLeft) {
343                 rv = ((target - srcLeft)*targetRight +
344                       (srcRight - target)*targetLeft) / (srcRight - srcLeft);
345         } else {
346                 rv = targetLeft;
347         }
348         return rv;
349 }
350
351 /*
352  * Uses the data points read from EEPROM to reconstruct the pdadc power table
353  * Called by ar2316SetPowerTable()
354  */
355 static int 
356 ar2316getGainBoundariesAndPdadcsForPowers(struct ath_hal *ah, uint16_t channel,
357                 const RAW_DATA_STRUCT_2316 *pRawDataset,
358                 uint16_t pdGainOverlap_t2, 
359                 int16_t  *pMinCalPower, uint16_t pPdGainBoundaries[], 
360                 uint16_t pPdGainValues[], uint16_t pPDADCValues[]) 
361 {
362         struct ar2316State *priv = AR2316(ah);
363 #define VpdTable_L      priv->vpdTable_L
364 #define VpdTable_R      priv->vpdTable_R
365 #define VpdTable_I      priv->vpdTable_I
366         uint32_t ii, jj, kk;
367         int32_t ss;/* potentially -ve index for taking care of pdGainOverlap */
368         uint32_t idxL, idxR;
369         uint32_t numPdGainsUsed = 0;
370         /* 
371          * If desired to support -ve power levels in future, just
372          * change pwr_I_0 to signed 5-bits.
373          */
374         int16_t Pmin_t2[MAX_NUM_PDGAINS_PER_CHANNEL];
375         /* to accomodate -ve power levels later on. */
376         int16_t Pmax_t2[MAX_NUM_PDGAINS_PER_CHANNEL];
377         /* to accomodate -ve power levels later on */
378         uint16_t numVpd = 0;
379         uint16_t Vpd_step;
380         int16_t tmpVal ; 
381         uint32_t sizeCurrVpdTable, maxIndex, tgtIndex;
382
383         /* Get upper lower index */
384         GetLowerUpperIndex(channel, pRawDataset->pChannels,
385                                  pRawDataset->numChannels, &(idxL), &(idxR));
386
387         for (ii = 0; ii < MAX_NUM_PDGAINS_PER_CHANNEL; ii++) {
388                 jj = MAX_NUM_PDGAINS_PER_CHANNEL - ii - 1;
389                 /* work backwards 'cause highest pdGain for lowest power */
390                 numVpd = pRawDataset->pDataPerChannel[idxL].pDataPerPDGain[jj].numVpd;
391                 if (numVpd > 0) {
392                         pPdGainValues[numPdGainsUsed] = pRawDataset->pDataPerChannel[idxL].pDataPerPDGain[jj].pd_gain;
393                         Pmin_t2[numPdGainsUsed] = pRawDataset->pDataPerChannel[idxL].pDataPerPDGain[jj].pwr_t4[0];
394                         if (Pmin_t2[numPdGainsUsed] >pRawDataset->pDataPerChannel[idxR].pDataPerPDGain[jj].pwr_t4[0]) {
395                                 Pmin_t2[numPdGainsUsed] = pRawDataset->pDataPerChannel[idxR].pDataPerPDGain[jj].pwr_t4[0];
396                         }
397                         Pmin_t2[numPdGainsUsed] = (int16_t)
398                                 (Pmin_t2[numPdGainsUsed] / 2);
399                         Pmax_t2[numPdGainsUsed] = pRawDataset->pDataPerChannel[idxL].pDataPerPDGain[jj].pwr_t4[numVpd-1];
400                         if (Pmax_t2[numPdGainsUsed] > pRawDataset->pDataPerChannel[idxR].pDataPerPDGain[jj].pwr_t4[numVpd-1])
401                                 Pmax_t2[numPdGainsUsed] = 
402                                         pRawDataset->pDataPerChannel[idxR].pDataPerPDGain[jj].pwr_t4[numVpd-1];
403                         Pmax_t2[numPdGainsUsed] = (int16_t)(Pmax_t2[numPdGainsUsed] / 2);
404                         ar2316FillVpdTable(
405                                            numPdGainsUsed, Pmin_t2[numPdGainsUsed], Pmax_t2[numPdGainsUsed], 
406                                            &(pRawDataset->pDataPerChannel[idxL].pDataPerPDGain[jj].pwr_t4[0]), 
407                                            &(pRawDataset->pDataPerChannel[idxL].pDataPerPDGain[jj].Vpd[0]), numVpd, VpdTable_L
408                                            );
409                         ar2316FillVpdTable(
410                                            numPdGainsUsed, Pmin_t2[numPdGainsUsed], Pmax_t2[numPdGainsUsed], 
411                                            &(pRawDataset->pDataPerChannel[idxR].pDataPerPDGain[jj].pwr_t4[0]),
412                                            &(pRawDataset->pDataPerChannel[idxR].pDataPerPDGain[jj].Vpd[0]), numVpd, VpdTable_R
413                                            );
414                         for (kk = 0; kk < (uint16_t)(Pmax_t2[numPdGainsUsed] - Pmin_t2[numPdGainsUsed]); kk++) {
415                                 VpdTable_I[numPdGainsUsed][kk] = 
416                                         interpolate_signed(
417                                                            channel, pRawDataset->pChannels[idxL], pRawDataset->pChannels[idxR],
418                                                            (int16_t)VpdTable_L[numPdGainsUsed][kk], (int16_t)VpdTable_R[numPdGainsUsed][kk]);
419                         }
420                         /* fill VpdTable_I for this pdGain */
421                         numPdGainsUsed++;
422                 }
423                 /* if this pdGain is used */
424         }
425
426         *pMinCalPower = Pmin_t2[0];
427         kk = 0; /* index for the final table */
428         for (ii = 0; ii < numPdGainsUsed; ii++) {
429                 if (ii == (numPdGainsUsed - 1))
430                         pPdGainBoundaries[ii] = Pmax_t2[ii] +
431                                 PD_GAIN_BOUNDARY_STRETCH_IN_HALF_DB;
432                 else 
433                         pPdGainBoundaries[ii] = (uint16_t)
434                                 ((Pmax_t2[ii] + Pmin_t2[ii+1]) / 2 );
435                 if (pPdGainBoundaries[ii] > 63) {
436                         HALDEBUG(ah, HAL_DEBUG_ANY,
437                             "%s: clamp pPdGainBoundaries[%d] %d\n",
438                             __func__, ii, pPdGainBoundaries[ii]);/*XXX*/
439                         pPdGainBoundaries[ii] = 63;
440                 }
441
442                 /* Find starting index for this pdGain */
443                 if (ii == 0) 
444                         ss = 0; /* for the first pdGain, start from index 0 */
445                 else 
446                         ss = (pPdGainBoundaries[ii-1] - Pmin_t2[ii]) - 
447                                 pdGainOverlap_t2;
448                 Vpd_step = (uint16_t)(VpdTable_I[ii][1] - VpdTable_I[ii][0]);
449                 Vpd_step = (uint16_t)((Vpd_step < 1) ? 1 : Vpd_step);
450                 /*
451                  *-ve ss indicates need to extrapolate data below for this pdGain
452                  */
453                 while (ss < 0) {
454                         tmpVal = (int16_t)(VpdTable_I[ii][0] + ss*Vpd_step);
455                         pPDADCValues[kk++] = (uint16_t)((tmpVal < 0) ? 0 : tmpVal);
456                         ss++;
457                 }
458
459                 sizeCurrVpdTable = Pmax_t2[ii] - Pmin_t2[ii];
460                 tgtIndex = pPdGainBoundaries[ii] + pdGainOverlap_t2 - Pmin_t2[ii];
461                 maxIndex = (tgtIndex < sizeCurrVpdTable) ? tgtIndex : sizeCurrVpdTable;
462
463                 while (ss < (int16_t)maxIndex)
464                         pPDADCValues[kk++] = VpdTable_I[ii][ss++];
465
466                 Vpd_step = (uint16_t)(VpdTable_I[ii][sizeCurrVpdTable-1] -
467                                        VpdTable_I[ii][sizeCurrVpdTable-2]);
468                 Vpd_step = (uint16_t)((Vpd_step < 1) ? 1 : Vpd_step);           
469                 /*
470                  * for last gain, pdGainBoundary == Pmax_t2, so will 
471                  * have to extrapolate
472                  */
473                 if (tgtIndex > maxIndex) {      /* need to extrapolate above */
474                         while(ss < (int16_t)tgtIndex) {
475                                 tmpVal = (uint16_t)
476                                         (VpdTable_I[ii][sizeCurrVpdTable-1] + 
477                                          (ss-maxIndex)*Vpd_step);
478                                 pPDADCValues[kk++] = (tmpVal > 127) ? 
479                                         127 : tmpVal;
480                                 ss++;
481                         }
482                 }                               /* extrapolated above */
483         }                                       /* for all pdGainUsed */
484
485         while (ii < MAX_NUM_PDGAINS_PER_CHANNEL) {
486                 pPdGainBoundaries[ii] = pPdGainBoundaries[ii-1];
487                 ii++;
488         }
489         while (kk < 128) {
490                 pPDADCValues[kk] = pPDADCValues[kk-1];
491                 kk++;
492         }
493
494         return numPdGainsUsed;
495 #undef VpdTable_L
496 #undef VpdTable_R
497 #undef VpdTable_I
498 }
499
500 static HAL_BOOL
501 ar2316SetPowerTable(struct ath_hal *ah,
502         int16_t *minPower, int16_t *maxPower,
503         const struct ieee80211_channel *chan, 
504         uint16_t *rfXpdGain)
505 {
506         struct ath_hal_5212 *ahp = AH5212(ah);
507         const HAL_EEPROM *ee = AH_PRIVATE(ah)->ah_eeprom;
508         const RAW_DATA_STRUCT_2316 *pRawDataset = AH_NULL;
509         uint16_t pdGainOverlap_t2;
510         int16_t minCalPower2316_t2;
511         uint16_t *pdadcValues = ahp->ah_pcdacTable;
512         uint16_t gainBoundaries[4];
513         uint32_t reg32, regoffset;
514         int i, numPdGainsUsed;
515 #ifndef AH_USE_INIPDGAIN
516         uint32_t tpcrg1;
517 #endif
518
519         HALDEBUG(ah, HAL_DEBUG_RFPARAM, "%s: chan 0x%x flag 0x%x\n",
520             __func__, chan->ic_freq, chan->ic_flags);
521
522         if (IEEE80211_IS_CHAN_G(chan) || IEEE80211_IS_CHAN_108G(chan))
523                 pRawDataset = &ee->ee_rawDataset2413[headerInfo11G];
524         else if (IEEE80211_IS_CHAN_B(chan))
525                 pRawDataset = &ee->ee_rawDataset2413[headerInfo11B];
526         else {
527                 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: illegal mode\n", __func__);
528                 return AH_FALSE;
529         }
530
531         pdGainOverlap_t2 = (uint16_t) SM(OS_REG_READ(ah, AR_PHY_TPCRG5),
532                                           AR_PHY_TPCRG5_PD_GAIN_OVERLAP);
533     
534         numPdGainsUsed = ar2316getGainBoundariesAndPdadcsForPowers(ah,
535                 chan->channel, pRawDataset, pdGainOverlap_t2,
536                 &minCalPower2316_t2,gainBoundaries, rfXpdGain, pdadcValues);
537         HALASSERT(1 <= numPdGainsUsed && numPdGainsUsed <= 3);
538
539 #ifdef AH_USE_INIPDGAIN
540         /*
541          * Use pd_gains curve from eeprom; Atheros always uses
542          * the default curve from the ini file but some vendors
543          * (e.g. Zcomax) want to override this curve and not
544          * honoring their settings results in tx power 5dBm low.
545          */
546         OS_REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_NUM_PD_GAIN, 
547                          (pRawDataset->pDataPerChannel[0].numPdGains - 1));
548 #else
549         tpcrg1 = OS_REG_READ(ah, AR_PHY_TPCRG1);
550         tpcrg1 = (tpcrg1 &~ AR_PHY_TPCRG1_NUM_PD_GAIN)
551                   | SM(numPdGainsUsed-1, AR_PHY_TPCRG1_NUM_PD_GAIN);
552         switch (numPdGainsUsed) {
553         case 3:
554                 tpcrg1 &= ~AR_PHY_TPCRG1_PDGAIN_SETTING3;
555                 tpcrg1 |= SM(rfXpdGain[2], AR_PHY_TPCRG1_PDGAIN_SETTING3);
556                 /* fall thru... */
557         case 2:
558                 tpcrg1 &= ~AR_PHY_TPCRG1_PDGAIN_SETTING2;
559                 tpcrg1 |= SM(rfXpdGain[1], AR_PHY_TPCRG1_PDGAIN_SETTING2);
560                 /* fall thru... */
561         case 1:
562                 tpcrg1 &= ~AR_PHY_TPCRG1_PDGAIN_SETTING1;
563                 tpcrg1 |= SM(rfXpdGain[0], AR_PHY_TPCRG1_PDGAIN_SETTING1);
564                 break;
565         }
566 #ifdef AH_DEBUG
567         if (tpcrg1 != OS_REG_READ(ah, AR_PHY_TPCRG1))
568                 HALDEBUG(ah, HAL_DEBUG_RFPARAM, "%s: using non-default "
569                     "pd_gains (default 0x%x, calculated 0x%x)\n",
570                     __func__, OS_REG_READ(ah, AR_PHY_TPCRG1), tpcrg1);
571 #endif
572         OS_REG_WRITE(ah, AR_PHY_TPCRG1, tpcrg1);
573 #endif
574
575         /*
576          * Note the pdadc table may not start at 0 dBm power, could be
577          * negative or greater than 0.  Need to offset the power
578          * values by the amount of minPower for griffin
579          */
580         if (minCalPower2316_t2 != 0)
581                 ahp->ah_txPowerIndexOffset = (int16_t)(0 - minCalPower2316_t2);
582         else
583                 ahp->ah_txPowerIndexOffset = 0;
584
585         /* Finally, write the power values into the baseband power table */
586         regoffset = 0x9800 + (672 <<2); /* beginning of pdadc table in griffin */
587         for (i = 0; i < 32; i++) {
588                 reg32 = ((pdadcValues[4*i + 0] & 0xFF) << 0)  | 
589                         ((pdadcValues[4*i + 1] & 0xFF) << 8)  |
590                         ((pdadcValues[4*i + 2] & 0xFF) << 16) |
591                         ((pdadcValues[4*i + 3] & 0xFF) << 24) ;        
592                 OS_REG_WRITE(ah, regoffset, reg32);
593                 regoffset += 4;
594         }
595
596         OS_REG_WRITE(ah, AR_PHY_TPCRG5, 
597                      SM(pdGainOverlap_t2, AR_PHY_TPCRG5_PD_GAIN_OVERLAP) | 
598                      SM(gainBoundaries[0], AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_1) |
599                      SM(gainBoundaries[1], AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_2) |
600                      SM(gainBoundaries[2], AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_3) |
601                      SM(gainBoundaries[3], AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_4));
602
603         return AH_TRUE;
604 }
605
606 static int16_t
607 ar2316GetMinPower(struct ath_hal *ah, const RAW_DATA_PER_CHANNEL_2316 *data)
608 {
609         uint32_t ii,jj;
610         uint16_t Pmin=0,numVpd;
611
612         for (ii = 0; ii < MAX_NUM_PDGAINS_PER_CHANNEL; ii++) {
613                 jj = MAX_NUM_PDGAINS_PER_CHANNEL - ii - 1;
614                 /* work backwards 'cause highest pdGain for lowest power */
615                 numVpd = data->pDataPerPDGain[jj].numVpd;
616                 if (numVpd > 0) {
617                         Pmin = data->pDataPerPDGain[jj].pwr_t4[0];
618                         return(Pmin);
619                 }
620         }
621         return(Pmin);
622 }
623
624 static int16_t
625 ar2316GetMaxPower(struct ath_hal *ah, const RAW_DATA_PER_CHANNEL_2316 *data)
626 {
627         uint32_t ii;
628         uint16_t Pmax=0,numVpd;
629         
630         for (ii=0; ii< MAX_NUM_PDGAINS_PER_CHANNEL; ii++) {
631                 /* work forwards cuase lowest pdGain for highest power */
632                 numVpd = data->pDataPerPDGain[ii].numVpd;
633                 if (numVpd > 0) {
634                         Pmax = data->pDataPerPDGain[ii].pwr_t4[numVpd-1];
635                         return(Pmax);
636                 }
637         }
638         return(Pmax);
639 }
640
641 static HAL_BOOL
642 ar2316GetChannelMaxMinPower(struct ath_hal *ah,
643         const struct ieee80211_channel *chan,
644         int16_t *maxPow, int16_t *minPow)
645 {
646         uint16_t freq = chan->ic_freq;          /* NB: never mapped */
647         const HAL_EEPROM *ee = AH_PRIVATE(ah)->ah_eeprom;
648         const RAW_DATA_STRUCT_2316 *pRawDataset = AH_NULL;
649         const RAW_DATA_PER_CHANNEL_2316 *data=AH_NULL;
650         uint16_t numChannels;
651         int totalD,totalF, totalMin,last, i;
652
653         *maxPow = 0;
654
655         if (IEEE80211_IS_CHAN_G(chan) || IEEE80211_IS_CHAN_108G(chan))
656                 pRawDataset = &ee->ee_rawDataset2413[headerInfo11G];
657         else if (IEEE80211_IS_CHAN_B(chan))
658                 pRawDataset = &ee->ee_rawDataset2413[headerInfo11B];
659         else
660                 return(AH_FALSE);
661
662         numChannels = pRawDataset->numChannels;
663         data = pRawDataset->pDataPerChannel;
664         
665         /* Make sure the channel is in the range of the TP values 
666          *  (freq piers)
667          */
668         if (numChannels < 1)
669                 return(AH_FALSE);
670
671         if ((freq < data[0].channelValue) ||
672             (freq > data[numChannels-1].channelValue)) {
673                 if (freq < data[0].channelValue) {
674                         *maxPow = ar2316GetMaxPower(ah, &data[0]);
675                         *minPow = ar2316GetMinPower(ah, &data[0]);
676                         return(AH_TRUE);
677                 } else {
678                         *maxPow = ar2316GetMaxPower(ah, &data[numChannels - 1]);
679                         *minPow = ar2316GetMinPower(ah, &data[numChannels - 1]);
680                         return(AH_TRUE);
681                 }
682         }
683
684         /* Linearly interpolate the power value now */
685         for (last=0,i=0; (i<numChannels) && (freq > data[i].channelValue);
686              last = i++);
687         totalD = data[i].channelValue - data[last].channelValue;
688         if (totalD > 0) {
689                 totalF = ar2316GetMaxPower(ah, &data[i]) - ar2316GetMaxPower(ah, &data[last]);
690                 *maxPow = (int8_t) ((totalF*(freq-data[last].channelValue) + 
691                                      ar2316GetMaxPower(ah, &data[last])*totalD)/totalD);
692                 totalMin = ar2316GetMinPower(ah, &data[i]) - ar2316GetMinPower(ah, &data[last]);
693                 *minPow = (int8_t) ((totalMin*(freq-data[last].channelValue) +
694                                      ar2316GetMinPower(ah, &data[last])*totalD)/totalD);
695                 return(AH_TRUE);
696         } else {
697                 if (freq == data[i].channelValue) {
698                         *maxPow = ar2316GetMaxPower(ah, &data[i]);
699                         *minPow = ar2316GetMinPower(ah, &data[i]);
700                         return(AH_TRUE);
701                 } else
702                         return(AH_FALSE);
703         }
704 }
705
706 /*
707  * Free memory for analog bank scratch buffers
708  */
709 static void
710 ar2316RfDetach(struct ath_hal *ah)
711 {
712         struct ath_hal_5212 *ahp = AH5212(ah);
713
714         HALASSERT(ahp->ah_rfHal != AH_NULL);
715         ath_hal_free(ahp->ah_rfHal);
716         ahp->ah_rfHal = AH_NULL;
717 }
718
719 /*
720  * Allocate memory for private state.
721  * Scratch Buffer will be reinitialized every reset so no need to zero now
722  */
723 static HAL_BOOL
724 ar2316RfAttach(struct ath_hal *ah, HAL_STATUS *status)
725 {
726         struct ath_hal_5212 *ahp = AH5212(ah);
727         struct ar2316State *priv;
728
729         HALASSERT(ah->ah_magic == AR5212_MAGIC);
730
731         HALASSERT(ahp->ah_rfHal == AH_NULL);
732         priv = ath_hal_malloc(sizeof(struct ar2316State));
733         if (priv == AH_NULL) {
734                 HALDEBUG(ah, HAL_DEBUG_ANY,
735                     "%s: cannot allocate private state\n", __func__);
736                 *status = HAL_ENOMEM;           /* XXX */
737                 return AH_FALSE;
738         }
739         priv->base.rfDetach             = ar2316RfDetach;
740         priv->base.writeRegs            = ar2316WriteRegs;
741         priv->base.getRfBank            = ar2316GetRfBank;
742         priv->base.setChannel           = ar2316SetChannel;
743         priv->base.setRfRegs            = ar2316SetRfRegs;
744         priv->base.setPowerTable        = ar2316SetPowerTable;
745         priv->base.getChannelMaxMinPower = ar2316GetChannelMaxMinPower;
746         priv->base.getNfAdjust          = ar5212GetNfAdjust;
747
748         ahp->ah_pcdacTable = priv->pcdacTable;
749         ahp->ah_pcdacTableSize = sizeof(priv->pcdacTable);
750         ahp->ah_rfHal = &priv->base;
751
752         ahp->ah_cwCalRequire = AH_TRUE;         /* force initial cal */
753
754         return AH_TRUE;
755 }
756
757 static HAL_BOOL
758 ar2316Probe(struct ath_hal *ah)
759 {
760         return IS_2316(ah);
761 }
762 AH_RF(RF2316, ar2316Probe, ar2316RfAttach);