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