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