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