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