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