netmap: d_poll -> d_kqfilter
[dragonfly.git] / sys / dev / netif / ath / hal / ath_hal / ar5212 / ar5111.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/ar5111.c 187831 2009-01-28 18:00:22Z 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_5111
31 #include "ar5212/ar5212.ini"
32
33 struct ar5111State {
34         RF_HAL_FUNCS    base;           /* public state, must be first */
35         uint16_t        pcdacTable[PWR_TABLE_SIZE];
36
37         uint32_t        Bank0Data[NELEM(ar5212Bank0_5111)];
38         uint32_t        Bank1Data[NELEM(ar5212Bank1_5111)];
39         uint32_t        Bank2Data[NELEM(ar5212Bank2_5111)];
40         uint32_t        Bank3Data[NELEM(ar5212Bank3_5111)];
41         uint32_t        Bank6Data[NELEM(ar5212Bank6_5111)];
42         uint32_t        Bank7Data[NELEM(ar5212Bank7_5111)];
43 };
44 #define AR5111(ah)      ((struct ar5111State *) AH5212(ah)->ah_rfHal)
45
46 static uint16_t ar5212GetScaledPower(uint16_t channel, uint16_t pcdacValue,
47                 const PCDACS_EEPROM *pSrcStruct);
48 static HAL_BOOL ar5212FindValueInList(uint16_t channel, uint16_t pcdacValue,
49                 const PCDACS_EEPROM *pSrcStruct, uint16_t *powerValue);
50 static void ar5212GetLowerUpperPcdacs(uint16_t pcdac, uint16_t channel,
51                 const PCDACS_EEPROM *pSrcStruct,
52                 uint16_t *pLowerPcdac, uint16_t *pUpperPcdac);
53
54 extern void ar5212GetLowerUpperValues(uint16_t value,
55                 const uint16_t *pList, uint16_t listSize,
56                 uint16_t *pLowerValue, uint16_t *pUpperValue);
57 extern  void ar5212ModifyRfBuffer(uint32_t *rfBuf, uint32_t reg32,
58                 uint32_t numBits, uint32_t firstBit, uint32_t column);
59
60 static void
61 ar5111WriteRegs(struct ath_hal *ah, u_int modesIndex, u_int freqIndex,
62         int writes)
63 {
64         HAL_INI_WRITE_ARRAY(ah, ar5212Modes_5111, modesIndex, writes);
65         HAL_INI_WRITE_ARRAY(ah, ar5212Common_5111, 1, writes);
66         HAL_INI_WRITE_ARRAY(ah, ar5212BB_RfGain_5111, freqIndex, writes);
67 }
68
69 /*
70  * Take the MHz channel value and set the Channel value
71  *
72  * ASSUMES: Writes enabled to analog bus
73  */
74 static HAL_BOOL
75 ar5111SetChannel(struct ath_hal *ah, const struct ieee80211_channel *chan)
76 {
77 #define CI_2GHZ_INDEX_CORRECTION 19
78         uint16_t freq = ath_hal_gethwchannel(ah, chan);
79         uint32_t refClk, reg32, data2111;
80         int16_t chan5111, chanIEEE;
81
82         /*
83          * Structure to hold 11b tuning information for 5111/2111
84          * 16 MHz mode, divider ratio = 198 = NP+S. N=16, S=4 or 6, P=12
85          */
86         typedef struct {
87                 uint32_t        refClkSel;      /* reference clock, 1 for 16 MHz */
88                 uint32_t        channelSelect;  /* P[7:4]S[3:0] bits */
89                 uint16_t        channel5111;    /* 11a channel for 5111 */
90         } CHAN_INFO_2GHZ;
91
92         static const CHAN_INFO_2GHZ chan2GHzData[] = {
93                 { 1, 0x46, 96  },       /* 2312 -19 */
94                 { 1, 0x46, 97  },       /* 2317 -18 */
95                 { 1, 0x46, 98  },       /* 2322 -17 */
96                 { 1, 0x46, 99  },       /* 2327 -16 */
97                 { 1, 0x46, 100 },       /* 2332 -15 */
98                 { 1, 0x46, 101 },       /* 2337 -14 */
99                 { 1, 0x46, 102 },       /* 2342 -13 */
100                 { 1, 0x46, 103 },       /* 2347 -12 */
101                 { 1, 0x46, 104 },       /* 2352 -11 */
102                 { 1, 0x46, 105 },       /* 2357 -10 */
103                 { 1, 0x46, 106 },       /* 2362  -9 */
104                 { 1, 0x46, 107 },       /* 2367  -8 */
105                 { 1, 0x46, 108 },       /* 2372  -7 */
106                 /* index -6 to 0 are pad to make this a nolookup table */
107                 { 1, 0x46, 116 },       /*       -6 */
108                 { 1, 0x46, 116 },       /*       -5 */
109                 { 1, 0x46, 116 },       /*       -4 */
110                 { 1, 0x46, 116 },       /*       -3 */
111                 { 1, 0x46, 116 },       /*       -2 */
112                 { 1, 0x46, 116 },       /*       -1 */
113                 { 1, 0x46, 116 },       /*        0 */
114                 { 1, 0x46, 116 },       /* 2412   1 */
115                 { 1, 0x46, 117 },       /* 2417   2 */
116                 { 1, 0x46, 118 },       /* 2422   3 */
117                 { 1, 0x46, 119 },       /* 2427   4 */
118                 { 1, 0x46, 120 },       /* 2432   5 */
119                 { 1, 0x46, 121 },       /* 2437   6 */
120                 { 1, 0x46, 122 },       /* 2442   7 */
121                 { 1, 0x46, 123 },       /* 2447   8 */
122                 { 1, 0x46, 124 },       /* 2452   9 */
123                 { 1, 0x46, 125 },       /* 2457  10 */
124                 { 1, 0x46, 126 },       /* 2462  11 */
125                 { 1, 0x46, 127 },       /* 2467  12 */
126                 { 1, 0x46, 128 },       /* 2472  13 */
127                 { 1, 0x44, 124 },       /* 2484  14 */
128                 { 1, 0x46, 136 },       /* 2512  15 */
129                 { 1, 0x46, 140 },       /* 2532  16 */
130                 { 1, 0x46, 144 },       /* 2552  17 */
131                 { 1, 0x46, 148 },       /* 2572  18 */
132                 { 1, 0x46, 152 },       /* 2592  19 */
133                 { 1, 0x46, 156 },       /* 2612  20 */
134                 { 1, 0x46, 160 },       /* 2632  21 */
135                 { 1, 0x46, 164 },       /* 2652  22 */
136                 { 1, 0x46, 168 },       /* 2672  23 */
137                 { 1, 0x46, 172 },       /* 2692  24 */
138                 { 1, 0x46, 176 },       /* 2712  25 */
139                 { 1, 0x46, 180 }        /* 2732  26 */
140         };
141
142         OS_MARK(ah, AH_MARK_SETCHANNEL, freq);
143
144         chanIEEE = chan->ic_ieee;
145         if (IEEE80211_IS_CHAN_2GHZ(chan)) {
146                 const CHAN_INFO_2GHZ* ci =
147                         &chan2GHzData[chanIEEE + CI_2GHZ_INDEX_CORRECTION];
148                 uint32_t txctl;
149
150                 data2111 = ((ath_hal_reverseBits(ci->channelSelect, 8) & 0xff)
151                                 << 5)
152                          | (ci->refClkSel << 4);
153                 chan5111 = ci->channel5111;
154                 txctl = OS_REG_READ(ah, AR_PHY_CCK_TX_CTRL);
155                 if (freq == 2484) {
156                         /* Enable channel spreading for channel 14 */
157                         OS_REG_WRITE(ah, AR_PHY_CCK_TX_CTRL,
158                                 txctl | AR_PHY_CCK_TX_CTRL_JAPAN);
159                 } else {
160                         OS_REG_WRITE(ah, AR_PHY_CCK_TX_CTRL,
161                                 txctl &~ AR_PHY_CCK_TX_CTRL_JAPAN);
162                 }
163         } else {
164                 chan5111 = chanIEEE;    /* no conversion needed */
165                 data2111 = 0;
166         }
167
168         /* Rest of the code is common for 5 GHz and 2.4 GHz. */
169         if (chan5111 >= 145 || (chan5111 & 0x1)) {
170                 reg32  = ath_hal_reverseBits(chan5111 - 24, 8) & 0xff;
171                 refClk = 1;
172         } else {
173                 reg32  = ath_hal_reverseBits(((chan5111 - 24)/2), 8) & 0xff;
174                 refClk = 0;
175         }
176
177         reg32 = (reg32 << 2) | (refClk << 1) | (1 << 10) | 0x1;
178         OS_REG_WRITE(ah, AR_PHY(0x27), ((data2111 & 0xff) << 8) | (reg32 & 0xff));
179         reg32 >>= 8;
180         OS_REG_WRITE(ah, AR_PHY(0x34), (data2111 & 0xff00) | (reg32 & 0xff));
181
182         AH_PRIVATE(ah)->ah_curchan = chan;
183         return AH_TRUE;
184 #undef CI_2GHZ_INDEX_CORRECTION
185 }
186
187 /*
188  * Return a reference to the requested RF Bank.
189  */
190 static uint32_t *
191 ar5111GetRfBank(struct ath_hal *ah, int bank)
192 {
193         struct ar5111State *priv = AR5111(ah);
194
195         HALASSERT(priv != AH_NULL);
196         switch (bank) {
197         case 0: return priv->Bank0Data;
198         case 1: return priv->Bank1Data;
199         case 2: return priv->Bank2Data;
200         case 3: return priv->Bank3Data;
201         case 6: return priv->Bank6Data;
202         case 7: return priv->Bank7Data;
203         }
204         HALDEBUG(ah, HAL_DEBUG_ANY, "%s: unknown RF Bank %d requested\n",
205             __func__, bank);
206         return AH_NULL;
207 }
208
209 /*
210  * Reads EEPROM header info from device structure and programs
211  * all rf registers
212  *
213  * REQUIRES: Access to the analog rf device
214  */
215 static HAL_BOOL
216 ar5111SetRfRegs(struct ath_hal *ah, const struct ieee80211_channel *chan,
217         uint16_t modesIndex, uint16_t *rfXpdGain)
218 {
219         uint16_t freq = ath_hal_gethwchannel(ah, chan);
220         struct ath_hal_5212 *ahp = AH5212(ah);
221         const HAL_EEPROM *ee = AH_PRIVATE(ah)->ah_eeprom;
222         uint16_t rfXpdGainFixed, rfPloSel, rfPwdXpd, gainI;
223         uint16_t tempOB, tempDB;
224         uint32_t ob2GHz, db2GHz, rfReg[NELEM(ar5212Bank6_5111)];
225         int i, regWrites = 0;
226
227         HALDEBUG(ah, HAL_DEBUG_RFPARAM, "%s: chan %u/0x%x modesIndex %u\n",
228             __func__, chan->ic_freq, chan->ic_flags, modesIndex);
229
230         /* Setup rf parameters */
231         switch (chan->ic_flags & IEEE80211_CHAN_ALLFULL) {
232         case IEEE80211_CHAN_A:
233                 if (4000 < freq && freq < 5260) {
234                         tempOB = ee->ee_ob1;
235                         tempDB = ee->ee_db1;
236                 } else if (5260 <= freq && freq < 5500) {
237                         tempOB = ee->ee_ob2;
238                         tempDB = ee->ee_db2;
239                 } else if (5500 <= freq && freq < 5725) {
240                         tempOB = ee->ee_ob3;
241                         tempDB = ee->ee_db3;
242                 } else if (freq >= 5725) {
243                         tempOB = ee->ee_ob4;
244                         tempDB = ee->ee_db4;
245                 } else {
246                         /* XXX when does this happen??? */
247                         tempOB = tempDB = 0;
248                 }
249                 ob2GHz = db2GHz = 0;
250
251                 rfXpdGainFixed = ee->ee_xgain[headerInfo11A];
252                 rfPloSel = ee->ee_xpd[headerInfo11A];
253                 rfPwdXpd = !ee->ee_xpd[headerInfo11A];
254                 gainI = ee->ee_gainI[headerInfo11A];
255                 break;
256         case IEEE80211_CHAN_B:
257                 tempOB = ee->ee_obFor24;
258                 tempDB = ee->ee_dbFor24;
259                 ob2GHz = ee->ee_ob2GHz[0];
260                 db2GHz = ee->ee_db2GHz[0];
261
262                 rfXpdGainFixed = ee->ee_xgain[headerInfo11B];
263                 rfPloSel = ee->ee_xpd[headerInfo11B];
264                 rfPwdXpd = !ee->ee_xpd[headerInfo11B];
265                 gainI = ee->ee_gainI[headerInfo11B];
266                 break;
267         case IEEE80211_CHAN_G:
268         case IEEE80211_CHAN_PUREG:      /* NB: really 108G */
269                 tempOB = ee->ee_obFor24g;
270                 tempDB = ee->ee_dbFor24g;
271                 ob2GHz = ee->ee_ob2GHz[1];
272                 db2GHz = ee->ee_db2GHz[1];
273
274                 rfXpdGainFixed = ee->ee_xgain[headerInfo11G];
275                 rfPloSel = ee->ee_xpd[headerInfo11G];
276                 rfPwdXpd = !ee->ee_xpd[headerInfo11G];
277                 gainI = ee->ee_gainI[headerInfo11G];
278                 break;
279         default:
280                 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: invalid channel flags 0x%x\n",
281                     __func__, chan->ic_flags);
282                 return AH_FALSE;
283         }
284
285         HALASSERT(1 <= tempOB && tempOB <= 5);
286         HALASSERT(1 <= tempDB && tempDB <= 5);
287
288         /* Bank 0 Write */
289         for (i = 0; i < NELEM(ar5212Bank0_5111); i++)
290                 rfReg[i] = ar5212Bank0_5111[i][modesIndex];
291         if (IEEE80211_IS_CHAN_2GHZ(chan)) {
292                 ar5212ModifyRfBuffer(rfReg, ob2GHz, 3, 119, 0);
293                 ar5212ModifyRfBuffer(rfReg, db2GHz, 3, 122, 0);
294         }
295         HAL_INI_WRITE_BANK(ah, ar5212Bank0_5111, rfReg, regWrites);
296
297         /* Bank 1 Write */
298         HAL_INI_WRITE_ARRAY(ah, ar5212Bank1_5111, 1, regWrites);
299
300         /* Bank 2 Write */
301         HAL_INI_WRITE_ARRAY(ah, ar5212Bank2_5111, modesIndex, regWrites);
302
303         /* Bank 3 Write */
304         HAL_INI_WRITE_ARRAY(ah, ar5212Bank3_5111, modesIndex, regWrites);
305
306         /* Bank 6 Write */
307         for (i = 0; i < NELEM(ar5212Bank6_5111); i++)
308                 rfReg[i] = ar5212Bank6_5111[i][modesIndex];
309         if (IEEE80211_IS_CHAN_A(chan)) {        /* NB: CHANNEL_A | CHANNEL_T */
310                 ar5212ModifyRfBuffer(rfReg, ee->ee_cornerCal.pd84, 1, 51, 3);
311                 ar5212ModifyRfBuffer(rfReg, ee->ee_cornerCal.pd90, 1, 45, 3);
312         }
313         ar5212ModifyRfBuffer(rfReg, rfPwdXpd, 1, 95, 0);
314         ar5212ModifyRfBuffer(rfReg, rfXpdGainFixed, 4, 96, 0);
315         /* Set 5212 OB & DB */
316         ar5212ModifyRfBuffer(rfReg, tempOB, 3, 104, 0);
317         ar5212ModifyRfBuffer(rfReg, tempDB, 3, 107, 0);
318         HAL_INI_WRITE_BANK(ah, ar5212Bank6_5111, rfReg, regWrites);
319
320         /* Bank 7 Write */
321         for (i = 0; i < NELEM(ar5212Bank7_5111); i++)
322                 rfReg[i] = ar5212Bank7_5111[i][modesIndex];
323         ar5212ModifyRfBuffer(rfReg, gainI, 6, 29, 0);   
324         ar5212ModifyRfBuffer(rfReg, rfPloSel, 1, 4, 0);   
325
326         if (IEEE80211_IS_CHAN_QUARTER(chan) || IEEE80211_IS_CHAN_HALF(chan)) {
327                 uint32_t        rfWaitI, rfWaitS, rfMaxTime;
328
329                 rfWaitS = 0x1f;
330                 rfWaitI = (IEEE80211_IS_CHAN_HALF(chan)) ?  0x10 : 0x1f;
331                 rfMaxTime = 3;
332                 ar5212ModifyRfBuffer(rfReg, rfWaitS, 5, 19, 0);
333                 ar5212ModifyRfBuffer(rfReg, rfWaitI, 5, 24, 0);
334                 ar5212ModifyRfBuffer(rfReg, rfMaxTime, 2, 49, 0);
335
336         }
337
338         HAL_INI_WRITE_BANK(ah, ar5212Bank7_5111, rfReg, regWrites);
339
340         /* Now that we have reprogrammed rfgain value, clear the flag. */
341         ahp->ah_rfgainState = HAL_RFGAIN_INACTIVE;
342
343         return AH_TRUE;
344 }
345
346 /*
347  * Returns interpolated or the scaled up interpolated value
348  */
349 static uint16_t
350 interpolate(uint16_t target, uint16_t srcLeft, uint16_t srcRight,
351         uint16_t targetLeft, uint16_t targetRight)
352 {
353         uint16_t rv;
354         int16_t lRatio;
355
356         /* to get an accurate ratio, always scale, if want to scale, then don't scale back down */
357         if ((targetLeft * targetRight) == 0)
358                 return 0;
359
360         if (srcRight != srcLeft) {
361                 /*
362                  * Note the ratio always need to be scaled,
363                  * since it will be a fraction.
364                  */
365                 lRatio = (target - srcLeft) * EEP_SCALE / (srcRight - srcLeft);
366                 if (lRatio < 0) {
367                     /* Return as Left target if value would be negative */
368                     rv = targetLeft;
369                 } else if (lRatio > EEP_SCALE) {
370                     /* Return as Right target if Ratio is greater than 100% (SCALE) */
371                     rv = targetRight;
372                 } else {
373                         rv = (lRatio * targetRight + (EEP_SCALE - lRatio) *
374                                         targetLeft) / EEP_SCALE;
375                 }
376         } else {
377                 rv = targetLeft;
378         }
379         return rv;
380 }
381
382 /*
383  * Read the transmit power levels from the structures taken from EEPROM
384  * Interpolate read transmit power values for this channel
385  * Organize the transmit power values into a table for writing into the hardware
386  */
387 static HAL_BOOL
388 ar5111SetPowerTable(struct ath_hal *ah,
389         int16_t *pMinPower, int16_t *pMaxPower,
390         const struct ieee80211_channel *chan,
391         uint16_t *rfXpdGain)
392 {
393         uint16_t freq = ath_hal_gethwchannel(ah, chan);
394         struct ath_hal_5212 *ahp = AH5212(ah);
395         const HAL_EEPROM *ee = AH_PRIVATE(ah)->ah_eeprom;
396         FULL_PCDAC_STRUCT pcdacStruct;
397         int i, j;
398
399         uint16_t     *pPcdacValues;
400         int16_t      *pScaledUpDbm;
401         int16_t      minScaledPwr;
402         int16_t      maxScaledPwr;
403         int16_t      pwr;
404         uint16_t     pcdacMin = 0;
405         uint16_t     pcdacMax = PCDAC_STOP;
406         uint16_t     pcdacTableIndex;
407         uint16_t     scaledPcdac;
408         PCDACS_EEPROM *pSrcStruct;
409         PCDACS_EEPROM eepromPcdacs;
410
411         /* setup the pcdac struct to point to the correct info, based on mode */
412         switch (chan->ic_flags & IEEE80211_CHAN_ALLTURBOFULL) {
413         case IEEE80211_CHAN_A:
414         case IEEE80211_CHAN_ST:
415                 eepromPcdacs.numChannels     = ee->ee_numChannels11a;
416                 eepromPcdacs.pChannelList    = ee->ee_channels11a;
417                 eepromPcdacs.pDataPerChannel = ee->ee_dataPerChannel11a;
418                 break;
419         case IEEE80211_CHAN_B:
420                 eepromPcdacs.numChannels     = ee->ee_numChannels2_4;
421                 eepromPcdacs.pChannelList    = ee->ee_channels11b;
422                 eepromPcdacs.pDataPerChannel = ee->ee_dataPerChannel11b;
423                 break;
424         case IEEE80211_CHAN_G:
425         case IEEE80211_CHAN_108G:
426                 eepromPcdacs.numChannels     = ee->ee_numChannels2_4;
427                 eepromPcdacs.pChannelList    = ee->ee_channels11g;
428                 eepromPcdacs.pDataPerChannel = ee->ee_dataPerChannel11g;
429                 break;
430         default:
431                 HALDEBUG(ah, HAL_DEBUG_ANY, "%s: invalid channel flags 0x%x\n",
432                     __func__, chan->ic_flags);
433                 return AH_FALSE;
434         }
435
436         pSrcStruct = &eepromPcdacs;
437
438         OS_MEMZERO(&pcdacStruct, sizeof(pcdacStruct));
439         pPcdacValues = pcdacStruct.PcdacValues;
440         pScaledUpDbm = pcdacStruct.PwrValues;
441
442         /* Initialize the pcdacs to dBM structs pcdacs to be 1 to 63 */
443         for (i = PCDAC_START, j = 0; i <= PCDAC_STOP; i+= PCDAC_STEP, j++)
444                 pPcdacValues[j] = i;
445
446         pcdacStruct.numPcdacValues = j;
447         pcdacStruct.pcdacMin = PCDAC_START;
448         pcdacStruct.pcdacMax = PCDAC_STOP;
449
450         /* Fill out the power values for this channel */
451         for (j = 0; j < pcdacStruct.numPcdacValues; j++ )
452                 pScaledUpDbm[j] = ar5212GetScaledPower(freq,
453                         pPcdacValues[j], pSrcStruct);
454
455         /* Now scale the pcdac values to fit in the 64 entry power table */
456         minScaledPwr = pScaledUpDbm[0];
457         maxScaledPwr = pScaledUpDbm[pcdacStruct.numPcdacValues - 1];
458
459         /* find minimum and make monotonic */
460         for (j = 0; j < pcdacStruct.numPcdacValues; j++) {
461                 if (minScaledPwr >= pScaledUpDbm[j]) {
462                         minScaledPwr = pScaledUpDbm[j];
463                         pcdacMin = j;
464                 }
465                 /*
466                  * Make the full_hsh monotonically increasing otherwise
467                  * interpolation algorithm will get fooled gotta start
468                  * working from the top, hence i = 63 - j.
469                  */
470                 i = (uint16_t)(pcdacStruct.numPcdacValues - 1 - j);
471                 if (i == 0)
472                         break;
473                 if (pScaledUpDbm[i-1] > pScaledUpDbm[i]) {
474                         /*
475                          * It could be a glitch, so make the power for
476                          * this pcdac the same as the power from the
477                          * next highest pcdac.
478                          */
479                         pScaledUpDbm[i - 1] = pScaledUpDbm[i];
480                 }
481         }
482
483         for (j = 0; j < pcdacStruct.numPcdacValues; j++)
484                 if (maxScaledPwr < pScaledUpDbm[j]) {
485                         maxScaledPwr = pScaledUpDbm[j];
486                         pcdacMax = j;
487                 }
488
489         /* Find the first power level with a pcdac */
490         pwr = (uint16_t)(PWR_STEP *
491                 ((minScaledPwr - PWR_MIN + PWR_STEP / 2) / PWR_STEP) + PWR_MIN);
492
493         /* Write all the first pcdac entries based off the pcdacMin */
494         pcdacTableIndex = 0;
495         for (i = 0; i < (2 * (pwr - PWR_MIN) / EEP_SCALE + 1); i++) {
496                 HALASSERT(pcdacTableIndex < PWR_TABLE_SIZE);
497                 ahp->ah_pcdacTable[pcdacTableIndex++] = pcdacMin;
498         }
499
500         i = 0;
501         while (pwr < pScaledUpDbm[pcdacStruct.numPcdacValues - 1] &&
502             pcdacTableIndex < PWR_TABLE_SIZE) {
503                 pwr += PWR_STEP;
504                 /* stop if dbM > max_power_possible */
505                 while (pwr < pScaledUpDbm[pcdacStruct.numPcdacValues - 1] &&
506                        (pwr - pScaledUpDbm[i])*(pwr - pScaledUpDbm[i+1]) > 0)
507                         i++;
508                 /* scale by 2 and add 1 to enable round up or down as needed */
509                 scaledPcdac = (uint16_t)(interpolate(pwr,
510                         pScaledUpDbm[i], pScaledUpDbm[i + 1],
511                         (uint16_t)(pPcdacValues[i] * 2),
512                         (uint16_t)(pPcdacValues[i + 1] * 2)) + 1);
513
514                 HALASSERT(pcdacTableIndex < PWR_TABLE_SIZE);
515                 ahp->ah_pcdacTable[pcdacTableIndex] = scaledPcdac / 2;
516                 if (ahp->ah_pcdacTable[pcdacTableIndex] > pcdacMax)
517                         ahp->ah_pcdacTable[pcdacTableIndex] = pcdacMax;
518                 pcdacTableIndex++;
519         }
520
521         /* Write all the last pcdac entries based off the last valid pcdac */
522         while (pcdacTableIndex < PWR_TABLE_SIZE) {
523                 ahp->ah_pcdacTable[pcdacTableIndex] =
524                         ahp->ah_pcdacTable[pcdacTableIndex - 1];
525                 pcdacTableIndex++;
526         }
527
528         /* No power table adjustment for 5111 */
529         ahp->ah_txPowerIndexOffset = 0;
530
531         return AH_TRUE;
532 }
533
534 /*
535  * Get or interpolate the pcdac value from the calibrated data.
536  */
537 static uint16_t
538 ar5212GetScaledPower(uint16_t channel, uint16_t pcdacValue,
539         const PCDACS_EEPROM *pSrcStruct)
540 {
541         uint16_t powerValue;
542         uint16_t lFreq, rFreq;          /* left and right frequency values */
543         uint16_t llPcdac, ulPcdac;      /* lower and upper left pcdac values */
544         uint16_t lrPcdac, urPcdac;      /* lower and upper right pcdac values */
545         uint16_t lPwr, uPwr;            /* lower and upper temp pwr values */
546         uint16_t lScaledPwr, rScaledPwr; /* left and right scaled power */
547
548         if (ar5212FindValueInList(channel, pcdacValue, pSrcStruct, &powerValue)) {
549                 /* value was copied from srcStruct */
550                 return powerValue;
551         }
552
553         ar5212GetLowerUpperValues(channel,
554                 pSrcStruct->pChannelList, pSrcStruct->numChannels,
555                 &lFreq, &rFreq);
556         ar5212GetLowerUpperPcdacs(pcdacValue,
557                 lFreq, pSrcStruct, &llPcdac, &ulPcdac);
558         ar5212GetLowerUpperPcdacs(pcdacValue,
559                 rFreq, pSrcStruct, &lrPcdac, &urPcdac);
560
561         /* get the power index for the pcdac value */
562         ar5212FindValueInList(lFreq, llPcdac, pSrcStruct, &lPwr);
563         ar5212FindValueInList(lFreq, ulPcdac, pSrcStruct, &uPwr);
564         lScaledPwr = interpolate(pcdacValue, llPcdac, ulPcdac, lPwr, uPwr);
565
566         ar5212FindValueInList(rFreq, lrPcdac, pSrcStruct, &lPwr);
567         ar5212FindValueInList(rFreq, urPcdac, pSrcStruct, &uPwr);
568         rScaledPwr = interpolate(pcdacValue, lrPcdac, urPcdac, lPwr, uPwr);
569
570         return interpolate(channel, lFreq, rFreq, lScaledPwr, rScaledPwr);
571 }
572
573 /*
574  * Find the value from the calibrated source data struct
575  */
576 static HAL_BOOL
577 ar5212FindValueInList(uint16_t channel, uint16_t pcdacValue,
578         const PCDACS_EEPROM *pSrcStruct, uint16_t *powerValue)
579 {
580         const DATA_PER_CHANNEL *pChannelData = pSrcStruct->pDataPerChannel;
581         int i;
582
583         for (i = 0; i < pSrcStruct->numChannels; i++ ) {
584                 if (pChannelData->channelValue == channel) {
585                         const uint16_t* pPcdac = pChannelData->PcdacValues;
586                         int j;
587
588                         for (j = 0; j < pChannelData->numPcdacValues; j++ ) {
589                                 if (*pPcdac == pcdacValue) {
590                                         *powerValue = pChannelData->PwrValues[j];
591                                         return AH_TRUE;
592                                 }
593                                 pPcdac++;
594                         }
595                 }
596                 pChannelData++;
597         }
598         return AH_FALSE;
599 }
600
601 /*
602  * Get the upper and lower pcdac given the channel and the pcdac
603  * used in the search
604  */
605 static void
606 ar5212GetLowerUpperPcdacs(uint16_t pcdac, uint16_t channel,
607         const PCDACS_EEPROM *pSrcStruct,
608         uint16_t *pLowerPcdac, uint16_t *pUpperPcdac)
609 {
610         const DATA_PER_CHANNEL *pChannelData = pSrcStruct->pDataPerChannel;
611         int i;
612
613         /* Find the channel information */
614         for (i = 0; i < pSrcStruct->numChannels; i++) {
615                 if (pChannelData->channelValue == channel)
616                         break;
617                 pChannelData++;
618         }
619         ar5212GetLowerUpperValues(pcdac, pChannelData->PcdacValues,
620                       pChannelData->numPcdacValues,
621                       pLowerPcdac, pUpperPcdac);
622 }
623
624 static HAL_BOOL
625 ar5111GetChannelMaxMinPower(struct ath_hal *ah,
626         const struct ieee80211_channel *chan,
627         int16_t *maxPow, int16_t *minPow)
628 {
629         /* XXX - Get 5111 power limits! */
630         /* NB: caller will cope */
631         return AH_FALSE;
632 }
633
634 /*
635  * Adjust NF based on statistical values for 5GHz frequencies.
636  */
637 static int16_t
638 ar5111GetNfAdjust(struct ath_hal *ah, const HAL_CHANNEL_INTERNAL *c)
639 {
640         static const struct {
641                 uint16_t freqLow;
642                 int16_t   adjust;
643         } adjust5111[] = {
644                 { 5790, 6 },    /* NB: ordered high -> low */
645                 { 5730, 4 },
646                 { 5690, 3 },
647                 { 5660, 2 },
648                 { 5610, 1 },
649                 { 5530, 0 },
650                 { 5450, 0 },
651                 { 5379, 1 },
652                 { 5209, 3 },
653                 { 3000, 5 },
654                 {    0, 0 },
655         };
656         int i;
657
658         for (i = 0; c->channel <= adjust5111[i].freqLow; i++)
659                 ;
660         return adjust5111[i].adjust;
661 }
662
663 /*
664  * Free memory for analog bank scratch buffers
665  */
666 static void
667 ar5111RfDetach(struct ath_hal *ah)
668 {
669         struct ath_hal_5212 *ahp = AH5212(ah);
670
671         HALASSERT(ahp->ah_rfHal != AH_NULL);
672         ath_hal_free(ahp->ah_rfHal);
673         ahp->ah_rfHal = AH_NULL;
674 }
675
676 /*
677  * Allocate memory for analog bank scratch buffers
678  * Scratch Buffer will be reinitialized every reset so no need to zero now
679  */
680 static HAL_BOOL
681 ar5111RfAttach(struct ath_hal *ah, HAL_STATUS *status)
682 {
683         struct ath_hal_5212 *ahp = AH5212(ah);
684         struct ar5111State *priv;
685
686         HALASSERT(ah->ah_magic == AR5212_MAGIC);
687
688         HALASSERT(ahp->ah_rfHal == AH_NULL);
689         priv = ath_hal_malloc(sizeof(struct ar5111State));
690         if (priv == AH_NULL) {
691                 HALDEBUG(ah, HAL_DEBUG_ANY,
692                     "%s: cannot allocate private state\n", __func__);
693                 *status = HAL_ENOMEM;           /* XXX */
694                 return AH_FALSE;
695         }
696         priv->base.rfDetach             = ar5111RfDetach;
697         priv->base.writeRegs            = ar5111WriteRegs;
698         priv->base.getRfBank            = ar5111GetRfBank;
699         priv->base.setChannel           = ar5111SetChannel;
700         priv->base.setRfRegs            = ar5111SetRfRegs;
701         priv->base.setPowerTable        = ar5111SetPowerTable;
702         priv->base.getChannelMaxMinPower = ar5111GetChannelMaxMinPower;
703         priv->base.getNfAdjust          = ar5111GetNfAdjust;
704
705         ahp->ah_pcdacTable = priv->pcdacTable;
706         ahp->ah_pcdacTableSize = sizeof(priv->pcdacTable);
707         ahp->ah_rfHal = &priv->base;
708
709         return AH_TRUE;
710 }
711
712 static HAL_BOOL
713 ar5111Probe(struct ath_hal *ah)
714 {
715         return IS_RAD5111(ah);
716 }
717 AH_RF(RF5111, ar5111Probe, ar5111RfAttach);