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