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