Merge branch 'master' into net80211-update
[dragonfly.git] / sys / dev / netif / ath / hal / ath_hal / ar5416 / ar9280.c
1 /*
2  * Copyright (c) 2008-2009 Sam Leffler, Errno Consulting
3  * Copyright (c) 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/ar5416/ar9280.c 203882 2010-02-14 16:26:32Z rpaulo $
18  * $DragonFly$
19  */
20 #include "opt_ah.h"
21
22 /*
23  * NB: Merlin and later have a simpler RF backend.
24  */
25 #include "ah.h"
26 #include "ah_internal.h"
27
28 #include "ah_eeprom_v14.h"
29
30 #include "ar5416/ar9280.h"
31 #include "ar5416/ar5416reg.h"
32 #include "ar5416/ar5416phy.h"
33
34 #define N(a)    (sizeof(a)/sizeof(a[0]))
35
36 struct ar9280State {
37         RF_HAL_FUNCS    base;           /* public state, must be first */
38         uint16_t        pcdacTable[1];  /* XXX */
39 };
40 #define AR9280(ah)      ((struct ar9280State *) AH5212(ah)->ah_rfHal)
41
42 static HAL_BOOL ar9280GetChannelMaxMinPower(struct ath_hal *,
43         const struct ieee80211_channel *, int16_t *maxPow,int16_t *minPow);
44 int16_t ar9280GetNfAdjust(struct ath_hal *ah, const HAL_CHANNEL_INTERNAL *c);
45
46 static void
47 ar9280WriteRegs(struct ath_hal *ah, u_int modesIndex, u_int freqIndex,
48         int writes)
49 {
50         (void) ath_hal_ini_write(ah, &AH5416(ah)->ah_ini_bb_rfgain,
51                 freqIndex, writes);
52 }
53
54 /*
55  * Take the MHz channel value and set the Channel value
56  *
57  * ASSUMES: Writes enabled to analog bus
58  *
59  * Actual Expression,
60  *
61  * For 2GHz channel, 
62  * Channel Frequency = (3/4) * freq_ref * (chansel[8:0] + chanfrac[16:0]/2^17) 
63  * (freq_ref = 40MHz)
64  *
65  * For 5GHz channel,
66  * Channel Frequency = (3/2) * freq_ref * (chansel[8:0] + chanfrac[16:0]/2^10)
67  * (freq_ref = 40MHz/(24>>amodeRefSel))
68  *
69  * For 5GHz channels which are 5MHz spaced,
70  * Channel Frequency = (3/2) * freq_ref * (chansel[8:0] + chanfrac[16:0]/2^17)
71  * (freq_ref = 40MHz)
72  */
73 static HAL_BOOL
74 ar9280SetChannel(struct ath_hal *ah, const struct ieee80211_channel *chan)
75 {
76         uint16_t bMode, fracMode, aModeRefSel = 0;
77         uint32_t freq, ndiv, channelSel = 0, channelFrac = 0, reg32 = 0;
78         CHAN_CENTERS centers;
79         uint32_t refDivA = 24;
80
81         OS_MARK(ah, AH_MARK_SETCHANNEL, chan->ic_freq);
82
83         ar5416GetChannelCenters(ah, chan, &centers);
84         freq = centers.synth_center;
85
86         reg32 = OS_REG_READ(ah, AR_PHY_SYNTH_CONTROL);
87         reg32 &= 0xc0000000;
88
89         if (freq < 4800) {     /* 2 GHz, fractional mode */
90                 uint32_t txctl;
91
92                 bMode = 1;
93                 fracMode = 1;
94                 aModeRefSel = 0;       
95                 channelSel = (freq * 0x10000)/15;
96
97                 txctl = OS_REG_READ(ah, AR_PHY_CCK_TX_CTRL);
98                 if (freq == 2484) {
99                         /* Enable channel spreading for channel 14 */
100                         OS_REG_WRITE(ah, AR_PHY_CCK_TX_CTRL,
101                             txctl | AR_PHY_CCK_TX_CTRL_JAPAN);
102                 } else {
103                         OS_REG_WRITE(ah, AR_PHY_CCK_TX_CTRL,
104                             txctl &~ AR_PHY_CCK_TX_CTRL_JAPAN);
105                 }     
106         } else {
107                 bMode = 0;
108                 fracMode = 0;
109
110                 if ((freq % 20) == 0) {
111                         aModeRefSel = 3;
112                 } else if ((freq % 10) == 0) {
113                         aModeRefSel = 2;
114                 } else {
115                         aModeRefSel = 0;
116                         /* Enable 2G (fractional) mode for channels which are 5MHz spaced */
117                         fracMode = 1;
118                         refDivA = 1;
119                         channelSel = (freq * 0x8000)/15;
120
121                         /* RefDivA setting */
122                         OS_REG_RMW_FIELD(ah, AR_AN_SYNTH9,
123                             AR_AN_SYNTH9_REFDIVA, refDivA);
124                 }
125                 if (!fracMode) {
126                         ndiv = (freq * (refDivA >> aModeRefSel))/60;
127                         channelSel =  ndiv & 0x1ff;         
128                         channelFrac = (ndiv & 0xfffffe00) * 2;
129                         channelSel = (channelSel << 17) | channelFrac;
130                 }
131         }
132
133         reg32 = reg32 | (bMode << 29) | (fracMode << 28) |
134             (aModeRefSel << 26) | (channelSel);
135
136         OS_REG_WRITE(ah, AR_PHY_SYNTH_CONTROL, reg32);
137
138         AH_PRIVATE(ah)->ah_curchan = chan;
139
140         return AH_TRUE;
141 }
142
143 /*
144  * Return a reference to the requested RF Bank.
145  */
146 static uint32_t *
147 ar9280GetRfBank(struct ath_hal *ah, int bank)
148 {
149         HALDEBUG(ah, HAL_DEBUG_ANY, "%s: unknown RF Bank %d requested\n",
150             __func__, bank);
151         return AH_NULL;
152 }
153
154 /*
155  * Reads EEPROM header info from device structure and programs
156  * all rf registers
157  */
158 static HAL_BOOL
159 ar9280SetRfRegs(struct ath_hal *ah, const struct ieee80211_channel *chan,
160                 uint16_t modesIndex, uint16_t *rfXpdGain)
161 {
162         return AH_TRUE;         /* nothing to do */
163 }
164
165 /*
166  * Read the transmit power levels from the structures taken from EEPROM
167  * Interpolate read transmit power values for this channel
168  * Organize the transmit power values into a table for writing into the hardware
169  */
170
171 static HAL_BOOL
172 ar9280SetPowerTable(struct ath_hal *ah, int16_t *pPowerMin, int16_t *pPowerMax, 
173         const struct ieee80211_channel *chan, uint16_t *rfXpdGain)
174 {
175         return AH_TRUE;
176 }
177
178 #if 0
179 static int16_t
180 ar9280GetMinPower(struct ath_hal *ah, EXPN_DATA_PER_CHANNEL_5112 *data)
181 {
182     int i, minIndex;
183     int16_t minGain,minPwr,minPcdac,retVal;
184
185     /* Assume NUM_POINTS_XPD0 > 0 */
186     minGain = data->pDataPerXPD[0].xpd_gain;
187     for (minIndex=0,i=1; i<NUM_XPD_PER_CHANNEL; i++) {
188         if (data->pDataPerXPD[i].xpd_gain < minGain) {
189             minIndex = i;
190             minGain = data->pDataPerXPD[i].xpd_gain;
191         }
192     }
193     minPwr = data->pDataPerXPD[minIndex].pwr_t4[0];
194     minPcdac = data->pDataPerXPD[minIndex].pcdac[0];
195     for (i=1; i<NUM_POINTS_XPD0; i++) {
196         if (data->pDataPerXPD[minIndex].pwr_t4[i] < minPwr) {
197             minPwr = data->pDataPerXPD[minIndex].pwr_t4[i];
198             minPcdac = data->pDataPerXPD[minIndex].pcdac[i];
199         }
200     }
201     retVal = minPwr - (minPcdac*2);
202     return(retVal);
203 }
204 #endif
205
206 static HAL_BOOL
207 ar9280GetChannelMaxMinPower(struct ath_hal *ah,
208         const struct ieee80211_channel *chan,
209         int16_t *maxPow, int16_t *minPow)
210 {
211 #if 0
212     struct ath_hal_5212 *ahp = AH5212(ah);
213     int numChannels=0,i,last;
214     int totalD, totalF,totalMin;
215     EXPN_DATA_PER_CHANNEL_5112 *data=AH_NULL;
216     EEPROM_POWER_EXPN_5112 *powerArray=AH_NULL;
217
218     *maxPow = 0;
219     if (IS_CHAN_A(chan)) {
220         powerArray = ahp->ah_modePowerArray5112;
221         data = powerArray[headerInfo11A].pDataPerChannel;
222         numChannels = powerArray[headerInfo11A].numChannels;
223     } else if (IS_CHAN_G(chan) || IS_CHAN_108G(chan)) {
224         /* XXX - is this correct? Should we also use the same power for turbo G? */
225         powerArray = ahp->ah_modePowerArray5112;
226         data = powerArray[headerInfo11G].pDataPerChannel;
227         numChannels = powerArray[headerInfo11G].numChannels;
228     } else if (IS_CHAN_B(chan)) {
229         powerArray = ahp->ah_modePowerArray5112;
230         data = powerArray[headerInfo11B].pDataPerChannel;
231         numChannels = powerArray[headerInfo11B].numChannels;
232     } else {
233         return (AH_TRUE);
234     }
235     /* Make sure the channel is in the range of the TP values
236      *  (freq piers)
237      */
238     if ((numChannels < 1) ||
239         (chan->channel < data[0].channelValue) ||
240         (chan->channel > data[numChannels-1].channelValue))
241         return(AH_FALSE);
242
243     /* Linearly interpolate the power value now */
244     for (last=0,i=0;
245          (i<numChannels) && (chan->channel > data[i].channelValue);
246          last=i++);
247     totalD = data[i].channelValue - data[last].channelValue;
248     if (totalD > 0) {
249         totalF = data[i].maxPower_t4 - data[last].maxPower_t4;
250         *maxPow = (int8_t) ((totalF*(chan->channel-data[last].channelValue) + data[last].maxPower_t4*totalD)/totalD);
251
252         totalMin = ar9280GetMinPower(ah,&data[i]) - ar9280GetMinPower(ah, &data[last]);
253         *minPow = (int8_t) ((totalMin*(chan->channel-data[last].channelValue) + ar9280GetMinPower(ah, &data[last])*totalD)/totalD);
254         return (AH_TRUE);
255     } else {
256         if (chan->channel == data[i].channelValue) {
257             *maxPow = data[i].maxPower_t4;
258             *minPow = ar9280GetMinPower(ah, &data[i]);
259             return(AH_TRUE);
260         } else
261             return(AH_FALSE);
262     }
263 #else
264         *maxPow = *minPow = 0;
265         return AH_FALSE;
266 #endif
267 }
268
269 static void
270 ar9280GetNoiseFloor(struct ath_hal *ah, int16_t nfarray[])
271 {
272         int16_t nf;
273
274         nf = MS(OS_REG_READ(ah, AR_PHY_CCA), AR9280_PHY_MINCCA_PWR);
275         if (nf & 0x100)
276                 nf = 0 - ((nf ^ 0x1ff) + 1);
277         HALDEBUG(ah, HAL_DEBUG_NFCAL,
278             "NF calibrated [ctl] [chain 0] is %d\n", nf);
279         nfarray[0] = nf;
280
281         nf = MS(OS_REG_READ(ah, AR_PHY_CH1_CCA), AR9280_PHY_CH1_MINCCA_PWR);
282         if (nf & 0x100)
283                 nf = 0 - ((nf ^ 0x1ff) + 1);
284         HALDEBUG(ah, HAL_DEBUG_NFCAL,
285             "NF calibrated [ctl] [chain 1] is %d\n", nf);
286         nfarray[1] = nf;
287
288         nf = MS(OS_REG_READ(ah, AR_PHY_EXT_CCA), AR9280_PHY_EXT_MINCCA_PWR);
289         if (nf & 0x100)
290                 nf = 0 - ((nf ^ 0x1ff) + 1);
291         HALDEBUG(ah, HAL_DEBUG_NFCAL,
292             "NF calibrated [ext] [chain 0] is %d\n", nf);
293         nfarray[3] = nf;
294
295         nf = MS(OS_REG_READ(ah, AR_PHY_CH1_EXT_CCA), AR9280_PHY_CH1_EXT_MINCCA_PWR);
296         if (nf & 0x100)
297                 nf = 0 - ((nf ^ 0x1ff) + 1);
298         HALDEBUG(ah, HAL_DEBUG_NFCAL,
299             "NF calibrated [ext] [chain 1] is %d\n", nf);
300         nfarray[4] = nf;
301 }
302
303 /*
304  * Adjust NF based on statistical values for 5GHz frequencies.
305  * Stubbed:Not used by Fowl
306  */
307 int16_t
308 ar9280GetNfAdjust(struct ath_hal *ah, const HAL_CHANNEL_INTERNAL *c)
309 {
310         return 0;
311 }
312
313 /*
314  * Free memory for analog bank scratch buffers
315  */
316 static void
317 ar9280RfDetach(struct ath_hal *ah)
318 {
319         struct ath_hal_5212 *ahp = AH5212(ah);
320
321         HALASSERT(ahp->ah_rfHal != AH_NULL);
322         ath_hal_free(ahp->ah_rfHal);
323         ahp->ah_rfHal = AH_NULL;
324 }
325
326 HAL_BOOL
327 ar9280RfAttach(struct ath_hal *ah, HAL_STATUS *status)
328 {
329         struct ath_hal_5212 *ahp = AH5212(ah);
330         struct ar9280State *priv;
331
332         HALDEBUG(ah, HAL_DEBUG_ATTACH, "%s: attach AR9280 radio\n", __func__);
333
334         HALASSERT(ahp->ah_rfHal == AH_NULL);
335         priv = ath_hal_malloc(sizeof(struct ar9280State));
336         if (priv == AH_NULL) {
337                 HALDEBUG(ah, HAL_DEBUG_ANY,
338                     "%s: cannot allocate private state\n", __func__);
339                 *status = HAL_ENOMEM;           /* XXX */
340                 return AH_FALSE;
341         }
342         priv->base.rfDetach             = ar9280RfDetach;
343         priv->base.writeRegs            = ar9280WriteRegs;
344         priv->base.getRfBank            = ar9280GetRfBank;
345         priv->base.setChannel           = ar9280SetChannel;
346         priv->base.setRfRegs            = ar9280SetRfRegs;
347         priv->base.setPowerTable        = ar9280SetPowerTable;
348         priv->base.getChannelMaxMinPower = ar9280GetChannelMaxMinPower;
349         priv->base.getNfAdjust          = ar9280GetNfAdjust;
350
351         ahp->ah_pcdacTable = priv->pcdacTable;
352         ahp->ah_pcdacTableSize = sizeof(priv->pcdacTable);
353         ahp->ah_rfHal = &priv->base;
354         /*
355          * Set noise floor adjust method; we arrange a
356          * direct call instead of thunking.
357          */
358         AH_PRIVATE(ah)->ah_getNfAdjust = priv->base.getNfAdjust;
359         AH_PRIVATE(ah)->ah_getNoiseFloor = ar9280GetNoiseFloor;
360
361         return AH_TRUE;
362 }