arn9280.c revision 1.2 1 1.2 christos /* $NetBSD: arn9280.c,v 1.2 2013/04/03 14:20:02 christos Exp $ */
2 1.1 christos /* $OpenBSD: ar9280.c,v 1.18 2012/06/10 21:23:36 kettenis Exp $ */
3 1.1 christos
4 1.1 christos /*-
5 1.1 christos * Copyright (c) 2009 Damien Bergamini <damien.bergamini (at) free.fr>
6 1.1 christos * Copyright (c) 2008-2009 Atheros Communications Inc.
7 1.1 christos *
8 1.1 christos * Permission to use, copy, modify, and/or distribute this software for any
9 1.1 christos * purpose with or without fee is hereby granted, provided that the above
10 1.1 christos * copyright notice and this permission notice appear in all copies.
11 1.1 christos *
12 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 1.1 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 1.1 christos * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 1.1 christos */
20 1.1 christos
21 1.1 christos /*
22 1.1 christos * Driver for Atheros 802.11a/g/n chipsets.
23 1.1 christos * Routines for AR9220, AR9223, AR9280 and AR9281 chipsets.
24 1.1 christos */
25 1.1 christos
26 1.1 christos #include <sys/cdefs.h>
27 1.2 christos __KERNEL_RCSID(0, "$NetBSD: arn9280.c,v 1.2 2013/04/03 14:20:02 christos Exp $");
28 1.1 christos
29 1.1 christos #include <sys/param.h>
30 1.1 christos #include <sys/sockio.h>
31 1.1 christos #include <sys/mbuf.h>
32 1.1 christos #include <sys/kernel.h>
33 1.1 christos #include <sys/socket.h>
34 1.1 christos #include <sys/systm.h>
35 1.1 christos #include <sys/malloc.h>
36 1.1 christos #include <sys/queue.h>
37 1.1 christos #include <sys/callout.h>
38 1.1 christos #include <sys/conf.h>
39 1.1 christos #include <sys/device.h>
40 1.1 christos
41 1.1 christos #include <sys/bus.h>
42 1.1 christos #include <sys/endian.h>
43 1.1 christos #include <sys/intr.h>
44 1.1 christos
45 1.1 christos #include <net/bpf.h>
46 1.1 christos #include <net/if.h>
47 1.1 christos #include <net/if_arp.h>
48 1.1 christos #include <net/if_dl.h>
49 1.2 christos #include <net/if_ether.h>
50 1.1 christos #include <net/if_media.h>
51 1.1 christos #include <net/if_types.h>
52 1.1 christos
53 1.1 christos #include <netinet/in.h>
54 1.1 christos #include <netinet/in_systm.h>
55 1.1 christos #include <netinet/in_var.h>
56 1.1 christos #include <netinet/ip.h>
57 1.1 christos
58 1.1 christos #include <net80211/ieee80211_var.h>
59 1.1 christos #include <net80211/ieee80211_amrr.h>
60 1.1 christos #include <net80211/ieee80211_radiotap.h>
61 1.1 christos
62 1.1 christos #include <dev/ic/athnreg.h>
63 1.1 christos #include <dev/ic/athnvar.h>
64 1.1 christos
65 1.1 christos #include <dev/ic/arn5008reg.h>
66 1.1 christos #include <dev/ic/arn5008.h>
67 1.1 christos #include <dev/ic/arn5416reg.h> /* We share the ROM layout. */
68 1.1 christos #include <dev/ic/arn5416.h> /* We share the ROM layout. */
69 1.1 christos #include <dev/ic/arn9280reg.h>
70 1.1 christos #include <dev/ic/arn9280.h>
71 1.1 christos
72 1.1 christos #define Static static
73 1.1 christos
74 1.1 christos Static void ar9280_init_from_rom(struct athn_softc *,
75 1.1 christos struct ieee80211_channel *, struct ieee80211_channel *);
76 1.1 christos Static void ar9280_olpc_init(struct athn_softc *);
77 1.1 christos Static void ar9280_olpc_temp_compensation(struct athn_softc *);
78 1.1 christos Static void ar9280_setup(struct athn_softc *);
79 1.1 christos
80 1.1 christos PUBLIC int
81 1.1 christos ar9280_attach(struct athn_softc *sc)
82 1.1 christos {
83 1.1 christos
84 1.1 christos sc->sc_eep_base = AR5416_EEP_START_LOC;
85 1.1 christos sc->sc_eep_size = sizeof(struct ar5416_eeprom);
86 1.1 christos sc->sc_def_nf = AR9280_PHY_CCA_MAX_GOOD_VALUE;
87 1.1 christos sc->sc_ngpiopins = (sc->sc_flags & ATHN_FLAG_USB) ? 16 : 10;
88 1.1 christos sc->sc_led_pin = 1;
89 1.1 christos sc->sc_workaround = AR9280_WA_DEFAULT;
90 1.1 christos sc->sc_ops.setup = ar9280_setup;
91 1.1 christos sc->sc_ops.swap_rom = ar5416_swap_rom;
92 1.1 christos sc->sc_ops.init_from_rom = ar9280_init_from_rom;
93 1.1 christos sc->sc_ops.set_txpower = ar5416_set_txpower;
94 1.1 christos sc->sc_ops.set_synth = ar9280_set_synth;
95 1.1 christos sc->sc_ops.spur_mitigate = ar9280_spur_mitigate;
96 1.1 christos sc->sc_ops.get_spur_chans = ar5416_get_spur_chans;
97 1.1 christos sc->sc_ops.olpc_init = ar9280_olpc_init;
98 1.1 christos sc->sc_ops.olpc_temp_compensation = ar9280_olpc_temp_compensation;
99 1.1 christos sc->sc_ini = &ar9280_2_0_ini;
100 1.1 christos sc->sc_serdes = &ar9280_2_0_serdes;
101 1.1 christos
102 1.1 christos return ar5008_attach(sc);
103 1.1 christos }
104 1.1 christos
105 1.1 christos Static void
106 1.1 christos ar9280_setup(struct athn_softc *sc)
107 1.1 christos {
108 1.1 christos const struct ar5416_eeprom *eep = sc->sc_eep;
109 1.1 christos uint8_t type;
110 1.1 christos
111 1.1 christos /* Determine if open loop power control should be used. */
112 1.1 christos if (sc->sc_eep_rev >= AR_EEP_MINOR_VER_19 &&
113 1.1 christos eep->baseEepHeader.openLoopPwrCntl)
114 1.1 christos sc->sc_flags |= ATHN_FLAG_OLPC;
115 1.1 christos
116 1.1 christos /* Determine if fast PLL clock is supported. */
117 1.1 christos if (AR_SREV_9280_20(sc) &&
118 1.1 christos (sc->sc_eep_rev <= AR_EEP_MINOR_VER_16 ||
119 1.1 christos eep->baseEepHeader.fastClk5g))
120 1.1 christos sc->sc_flags |= ATHN_FLAG_FAST_PLL_CLOCK;
121 1.1 christos
122 1.1 christos /*
123 1.1 christos * Determine if initialization value for AR_AN_TOP2 must be fixed.
124 1.1 christos * This is required for some AR9220 devices such as Ubiquiti SR71-12.
125 1.1 christos */
126 1.1 christos if (AR_SREV_9280_20(sc) &&
127 1.1 christos sc->sc_eep_rev > AR_EEP_MINOR_VER_10 &&
128 1.1 christos !eep->baseEepHeader.pwdclkind) {
129 1.1 christos DPRINTFN(DBG_INIT, sc, "AR_AN_TOP2 fixup required\n");
130 1.1 christos sc->sc_flags |= ATHN_FLAG_AN_TOP2_FIXUP;
131 1.1 christos }
132 1.1 christos
133 1.1 christos if (AR_SREV_9280_20(sc)) {
134 1.1 christos /* Check if we have a valid rxGainType field in ROM. */
135 1.1 christos if (sc->sc_eep_rev >= AR_EEP_MINOR_VER_17) {
136 1.1 christos /* Select initialization values based on ROM. */
137 1.1 christos type = eep->baseEepHeader.rxGainType;
138 1.1 christos DPRINTFN(DBG_INIT, sc, "Rx gain type=0x%x\n", type);
139 1.1 christos if (type == AR5416_EEP_RXGAIN_23DB_BACKOFF)
140 1.1 christos sc->sc_rx_gain = &ar9280_2_0_rx_gain_23db_backoff;
141 1.1 christos else if (type == AR5416_EEP_RXGAIN_13DB_BACKOFF)
142 1.1 christos sc->sc_rx_gain = &ar9280_2_0_rx_gain_13db_backoff;
143 1.1 christos else
144 1.1 christos sc->sc_rx_gain = &ar9280_2_0_rx_gain;
145 1.1 christos }
146 1.1 christos else
147 1.1 christos sc->sc_rx_gain = &ar9280_2_0_rx_gain;
148 1.1 christos
149 1.1 christos /* Check if we have a valid txGainType field in ROM. */
150 1.1 christos if (sc->sc_eep_rev >= AR_EEP_MINOR_VER_19) {
151 1.1 christos /* Select initialization values based on ROM. */
152 1.1 christos type = eep->baseEepHeader.txGainType;
153 1.1 christos DPRINTFN(DBG_INIT, sc, "Tx gain type=0x%x\n", type);
154 1.1 christos if (type == AR_EEP_TXGAIN_HIGH_POWER)
155 1.1 christos sc->sc_tx_gain = &ar9280_2_0_tx_gain_high_power;
156 1.1 christos else
157 1.1 christos sc->sc_tx_gain = &ar9280_2_0_tx_gain;
158 1.1 christos }
159 1.1 christos else
160 1.1 christos sc->sc_tx_gain = &ar9280_2_0_tx_gain;
161 1.1 christos }
162 1.1 christos }
163 1.1 christos
164 1.1 christos PUBLIC int
165 1.1 christos ar9280_set_synth(struct athn_softc *sc, struct ieee80211_channel *c,
166 1.1 christos struct ieee80211_channel *extc)
167 1.1 christos {
168 1.1 christos uint32_t phy, reg, ndiv = 0;
169 1.1 christos uint32_t freq = c->ic_freq;
170 1.1 christos
171 1.1 christos phy = AR_READ(sc, AR9280_PHY_SYNTH_CONTROL) & ~0x3fffffff;
172 1.1 christos
173 1.1 christos if (IEEE80211_IS_CHAN_2GHZ(c)) {
174 1.1 christos phy |= (freq << 16) / 15;
175 1.1 christos phy |= AR9280_BMODE | AR9280_FRACMODE;
176 1.1 christos
177 1.1 christos if (AR_SREV_9287_11_OR_LATER(sc)) {
178 1.1 christos /* NB: Magic values from the Linux driver. */
179 1.1 christos if (freq == 2484) { /* Channel 14. */
180 1.1 christos /* Japanese regulatory requirements. */
181 1.1 christos AR_WRITE(sc, AR_PHY(637), 0x00000000);
182 1.1 christos AR_WRITE(sc, AR_PHY(638), 0xefff0301);
183 1.1 christos AR_WRITE(sc, AR_PHY(639), 0xca9228ee);
184 1.1 christos }
185 1.1 christos else {
186 1.1 christos AR_WRITE(sc, AR_PHY(637), 0x00fffeff);
187 1.1 christos AR_WRITE(sc, AR_PHY(638), 0x00f5f9ff);
188 1.1 christos AR_WRITE(sc, AR_PHY(639), 0xb79f6427);
189 1.1 christos }
190 1.1 christos }
191 1.1 christos else {
192 1.1 christos reg = AR_READ(sc, AR_PHY_CCK_TX_CTRL);
193 1.1 christos if (freq == 2484) /* Channel 14. */
194 1.1 christos reg |= AR_PHY_CCK_TX_CTRL_JAPAN;
195 1.1 christos else
196 1.1 christos reg &= ~AR_PHY_CCK_TX_CTRL_JAPAN;
197 1.1 christos AR_WRITE(sc, AR_PHY_CCK_TX_CTRL, reg);
198 1.1 christos }
199 1.1 christos }
200 1.1 christos else {
201 1.1 christos if (AR_SREV_9285_10_OR_LATER(sc) ||
202 1.1 christos sc->sc_eep_rev < AR_EEP_MINOR_VER_22 ||
203 1.1 christos !((struct ar5416_base_eep_header *)sc->sc_eep)->frac_n_5g) {
204 1.1 christos if ((freq % 20) == 0) {
205 1.1 christos ndiv = (freq * 3) / 60;
206 1.1 christos phy |= SM(AR9280_AMODE_REFSEL, 3);
207 1.1 christos }
208 1.1 christos else if ((freq % 10) == 0) {
209 1.1 christos ndiv = (freq * 6) / 60;
210 1.1 christos phy |= SM(AR9280_AMODE_REFSEL, 2);
211 1.1 christos }
212 1.1 christos }
213 1.1 christos if (ndiv != 0) {
214 1.1 christos phy |= (ndiv & 0x1ff) << 17;
215 1.1 christos phy |= (ndiv & ~0x1ff) * 2;
216 1.1 christos }
217 1.1 christos else {
218 1.1 christos phy |= (freq << 15) / 15;
219 1.1 christos phy |= AR9280_FRACMODE;
220 1.1 christos
221 1.1 christos reg = AR_READ(sc, AR_AN_SYNTH9);
222 1.1 christos reg = RW(reg, AR_AN_SYNTH9_REFDIVA, 1);
223 1.1 christos AR_WRITE(sc, AR_AN_SYNTH9, reg);
224 1.1 christos }
225 1.1 christos }
226 1.1 christos AR_WRITE_BARRIER(sc);
227 1.1 christos DPRINTFN(DBG_RF, sc, "AR9280_PHY_SYNTH_CONTROL=0x%08x\n", phy);
228 1.1 christos AR_WRITE(sc, AR9280_PHY_SYNTH_CONTROL, phy);
229 1.1 christos AR_WRITE_BARRIER(sc);
230 1.1 christos return 0;
231 1.1 christos }
232 1.1 christos
233 1.1 christos Static void
234 1.1 christos ar9280_init_from_rom(struct athn_softc *sc, struct ieee80211_channel *c,
235 1.1 christos struct ieee80211_channel *extc)
236 1.1 christos {
237 1.1 christos static const uint32_t chainoffset[] = { 0x0000, 0x2000, 0x1000 };
238 1.1 christos const struct ar5416_eeprom *eep = sc->sc_eep;
239 1.1 christos const struct ar5416_modal_eep_header *modal;
240 1.1 christos uint32_t reg, offset;
241 1.1 christos uint8_t txRxAtten;
242 1.1 christos int i;
243 1.1 christos
244 1.1 christos modal = &eep->modalHeader[IEEE80211_IS_CHAN_2GHZ(c)];
245 1.1 christos
246 1.1 christos AR_WRITE(sc, AR_PHY_SWITCH_COM, modal->antCtrlCommon);
247 1.1 christos
248 1.1 christos for (i = 0; i < AR9280_MAX_CHAINS; i++) {
249 1.1 christos if (sc->sc_rxchainmask == 0x5 || sc->sc_txchainmask == 0x5)
250 1.1 christos offset = chainoffset[i];
251 1.1 christos else
252 1.1 christos offset = i * 0x1000;
253 1.1 christos
254 1.1 christos AR_WRITE(sc, AR_PHY_SWITCH_CHAIN_0 + offset,
255 1.1 christos modal->antCtrlChain[i]);
256 1.1 christos
257 1.1 christos reg = AR_READ(sc, AR_PHY_TIMING_CTRL4_0 + offset);
258 1.1 christos reg = RW(reg, AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF,
259 1.1 christos modal->iqCalICh[i]);
260 1.1 christos reg = RW(reg, AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF,
261 1.1 christos modal->iqCalQCh[i]);
262 1.1 christos AR_WRITE(sc, AR_PHY_TIMING_CTRL4_0 + offset, reg);
263 1.1 christos
264 1.1 christos if (sc->sc_eep_rev >= AR_EEP_MINOR_VER_3) {
265 1.1 christos reg = AR_READ(sc, AR_PHY_GAIN_2GHZ + offset);
266 1.1 christos reg = RW(reg, AR_PHY_GAIN_2GHZ_XATTEN1_MARGIN,
267 1.1 christos modal->bswMargin[i]);
268 1.1 christos reg = RW(reg, AR_PHY_GAIN_2GHZ_XATTEN1_DB,
269 1.1 christos modal->bswAtten[i]);
270 1.1 christos reg = RW(reg, AR_PHY_GAIN_2GHZ_XATTEN2_MARGIN,
271 1.1 christos modal->xatten2Margin[i]);
272 1.1 christos reg = RW(reg, AR_PHY_GAIN_2GHZ_XATTEN2_DB,
273 1.1 christos modal->xatten2Db[i]);
274 1.1 christos AR_WRITE(sc, AR_PHY_GAIN_2GHZ + offset, reg);
275 1.1 christos }
276 1.1 christos if (sc->sc_eep_rev >= AR_EEP_MINOR_VER_3)
277 1.1 christos txRxAtten = modal->txRxAttenCh[i];
278 1.1 christos else /* Workaround for ROM versions < 14.3. */
279 1.1 christos txRxAtten = IEEE80211_IS_CHAN_2GHZ(c) ? 23 : 44;
280 1.1 christos reg = AR_READ(sc, AR_PHY_RXGAIN + offset);
281 1.1 christos reg = RW(reg, AR9280_PHY_RXGAIN_TXRX_ATTEN,
282 1.1 christos txRxAtten);
283 1.1 christos reg = RW(reg, AR9280_PHY_RXGAIN_TXRX_MARGIN,
284 1.1 christos modal->rxTxMarginCh[i]);
285 1.1 christos AR_WRITE(sc, AR_PHY_RXGAIN + offset, reg);
286 1.1 christos }
287 1.1 christos if (IEEE80211_IS_CHAN_2GHZ(c)) {
288 1.1 christos reg = AR_READ(sc, AR_AN_RF2G1_CH0);
289 1.1 christos reg = RW(reg, AR_AN_RF2G1_CH0_OB, modal->ob);
290 1.1 christos reg = RW(reg, AR_AN_RF2G1_CH0_DB, modal->db);
291 1.1 christos AR_WRITE(sc, AR_AN_RF2G1_CH0, reg);
292 1.1 christos AR_WRITE_BARRIER(sc);
293 1.1 christos DELAY(100);
294 1.1 christos
295 1.1 christos reg = AR_READ(sc, AR_AN_RF2G1_CH1);
296 1.1 christos reg = RW(reg, AR_AN_RF2G1_CH1_OB, modal->ob_ch1);
297 1.1 christos reg = RW(reg, AR_AN_RF2G1_CH1_DB, modal->db_ch1);
298 1.1 christos AR_WRITE(sc, AR_AN_RF2G1_CH1, reg);
299 1.1 christos AR_WRITE_BARRIER(sc);
300 1.1 christos DELAY(100);
301 1.1 christos }
302 1.1 christos else {
303 1.1 christos reg = AR_READ(sc, AR_AN_RF5G1_CH0);
304 1.1 christos reg = RW(reg, AR_AN_RF5G1_CH0_OB5, modal->ob);
305 1.1 christos reg = RW(reg, AR_AN_RF5G1_CH0_DB5, modal->db);
306 1.1 christos AR_WRITE(sc, AR_AN_RF5G1_CH0, reg);
307 1.1 christos AR_WRITE_BARRIER(sc);
308 1.1 christos DELAY(100);
309 1.1 christos
310 1.1 christos reg = AR_READ(sc, AR_AN_RF5G1_CH1);
311 1.1 christos reg = RW(reg, AR_AN_RF5G1_CH1_OB5, modal->ob_ch1);
312 1.1 christos reg = RW(reg, AR_AN_RF5G1_CH1_DB5, modal->db_ch1);
313 1.1 christos AR_WRITE(sc, AR_AN_RF5G1_CH1, reg);
314 1.1 christos AR_WRITE_BARRIER(sc);
315 1.1 christos DELAY(100);
316 1.1 christos }
317 1.1 christos reg = AR_READ(sc, AR_AN_TOP2);
318 1.1 christos if ((sc->sc_flags & ATHN_FLAG_USB) && IEEE80211_IS_CHAN_5GHZ(c)) {
319 1.1 christos /*
320 1.1 christos * Hardcode the output voltage of x-PA bias LDO to the
321 1.1 christos * lowest value for UB94 such that the card doesn't get
322 1.1 christos * too hot.
323 1.1 christos */
324 1.1 christos reg = RW(reg, AR_AN_TOP2_XPABIAS_LVL, 0);
325 1.1 christos }
326 1.1 christos else
327 1.1 christos reg = RW(reg, AR_AN_TOP2_XPABIAS_LVL, modal->xpaBiasLvl);
328 1.1 christos if (modal->flagBits & AR5416_EEP_FLAG_LOCALBIAS)
329 1.1 christos reg |= AR_AN_TOP2_LOCALBIAS;
330 1.1 christos else
331 1.1 christos reg &= ~AR_AN_TOP2_LOCALBIAS;
332 1.1 christos AR_WRITE(sc, AR_AN_TOP2, reg);
333 1.1 christos AR_WRITE_BARRIER(sc);
334 1.1 christos DELAY(100);
335 1.1 christos
336 1.1 christos reg = AR_READ(sc, AR_PHY_XPA_CFG);
337 1.1 christos if (modal->flagBits & AR5416_EEP_FLAG_FORCEXPAON)
338 1.1 christos reg |= AR_PHY_FORCE_XPA_CFG;
339 1.1 christos else
340 1.1 christos reg &= ~AR_PHY_FORCE_XPA_CFG;
341 1.1 christos AR_WRITE(sc, AR_PHY_XPA_CFG, reg);
342 1.1 christos
343 1.1 christos reg = AR_READ(sc, AR_PHY_SETTLING);
344 1.1 christos reg = RW(reg, AR_PHY_SETTLING_SWITCH, modal->switchSettling);
345 1.1 christos AR_WRITE(sc, AR_PHY_SETTLING, reg);
346 1.1 christos
347 1.1 christos reg = AR_READ(sc, AR_PHY_DESIRED_SZ);
348 1.1 christos reg = RW(reg, AR_PHY_DESIRED_SZ_ADC, modal->adcDesiredSize);
349 1.1 christos AR_WRITE(sc, AR_PHY_DESIRED_SZ, reg);
350 1.1 christos
351 1.1 christos reg = SM(AR_PHY_RF_CTL4_TX_END_XPAA_OFF, modal->txEndToXpaOff);
352 1.1 christos reg |= SM(AR_PHY_RF_CTL4_TX_END_XPAB_OFF, modal->txEndToXpaOff);
353 1.1 christos reg |= SM(AR_PHY_RF_CTL4_FRAME_XPAA_ON, modal->txFrameToXpaOn);
354 1.1 christos reg |= SM(AR_PHY_RF_CTL4_FRAME_XPAB_ON, modal->txFrameToXpaOn);
355 1.1 christos AR_WRITE(sc, AR_PHY_RF_CTL4, reg);
356 1.1 christos
357 1.1 christos reg = AR_READ(sc, AR_PHY_RF_CTL3);
358 1.1 christos reg = RW(reg, AR_PHY_TX_END_TO_A2_RX_ON, modal->txEndToRxOn);
359 1.1 christos AR_WRITE(sc, AR_PHY_RF_CTL3, reg);
360 1.1 christos
361 1.1 christos reg = AR_READ(sc, AR_PHY_CCA(0));
362 1.1 christos reg = RW(reg, AR9280_PHY_CCA_THRESH62, modal->thresh62);
363 1.1 christos AR_WRITE(sc, AR_PHY_CCA(0), reg);
364 1.1 christos
365 1.1 christos reg = AR_READ(sc, AR_PHY_EXT_CCA0);
366 1.1 christos reg = RW(reg, AR_PHY_EXT_CCA0_THRESH62, modal->thresh62);
367 1.1 christos AR_WRITE(sc, AR_PHY_EXT_CCA0, reg);
368 1.1 christos
369 1.1 christos if (sc->sc_eep_rev >= AR_EEP_MINOR_VER_2) {
370 1.1 christos reg = AR_READ(sc, AR_PHY_RF_CTL2);
371 1.1 christos reg = RW(reg, AR_PHY_TX_END_DATA_START,
372 1.1 christos modal->txFrameToDataStart);
373 1.1 christos reg = RW(reg, AR_PHY_TX_END_PA_ON, modal->txFrameToPaOn);
374 1.1 christos AR_WRITE(sc, AR_PHY_RF_CTL2, reg);
375 1.1 christos }
376 1.1 christos #ifndef IEEE80211_NO_HT
377 1.1 christos if (sc->sc_eep_rev >= AR_EEP_MINOR_VER_3 && extc != NULL) {
378 1.1 christos /* Overwrite switch settling with HT-40 value. */
379 1.1 christos reg = AR_READ(sc, AR_PHY_SETTLING);
380 1.1 christos reg = RW(reg, AR_PHY_SETTLING_SWITCH, modal->swSettleHt40);
381 1.1 christos AR_WRITE(sc, AR_PHY_SETTLING, reg);
382 1.1 christos }
383 1.1 christos #endif
384 1.1 christos if (sc->sc_eep_rev >= AR_EEP_MINOR_VER_19) {
385 1.1 christos reg = AR_READ(sc, AR_PHY_CCK_TX_CTRL);
386 1.1 christos reg = RW(reg, AR_PHY_CCK_TX_CTRL_TX_DAC_SCALE_CCK,
387 1.1 christos MS(modal->miscBits, AR5416_EEP_MISC_TX_DAC_SCALE_CCK));
388 1.1 christos AR_WRITE(sc, AR_PHY_CCK_TX_CTRL, reg);
389 1.1 christos }
390 1.1 christos if (AR_SREV_9280_20(sc) &&
391 1.1 christos sc->sc_eep_rev >= AR_EEP_MINOR_VER_20) {
392 1.1 christos reg = AR_READ(sc, AR_AN_TOP1);
393 1.1 christos if (eep->baseEepHeader.dacLpMode &&
394 1.1 christos (IEEE80211_IS_CHAN_2GHZ(c) ||
395 1.1 christos !eep->baseEepHeader.dacHiPwrMode_5G))
396 1.1 christos reg |= AR_AN_TOP1_DACLPMODE;
397 1.1 christos else
398 1.1 christos reg &= ~AR_AN_TOP1_DACLPMODE;
399 1.1 christos AR_WRITE(sc, AR_AN_TOP1, reg);
400 1.1 christos AR_WRITE_BARRIER(sc);
401 1.1 christos DELAY(100);
402 1.1 christos
403 1.1 christos reg = AR_READ(sc, AR_PHY_FRAME_CTL);
404 1.1 christos reg = RW(reg, AR_PHY_FRAME_CTL_TX_CLIP,
405 1.1 christos MS(modal->miscBits, AR5416_EEP_MISC_TX_CLIP));
406 1.1 christos AR_WRITE(sc, AR_PHY_FRAME_CTL, reg);
407 1.1 christos
408 1.1 christos reg = AR_READ(sc, AR_PHY_TX_PWRCTRL9);
409 1.1 christos reg = RW(reg, AR_PHY_TX_DESIRED_SCALE_CCK,
410 1.1 christos eep->baseEepHeader.desiredScaleCCK);
411 1.1 christos AR_WRITE(sc, AR_PHY_TX_PWRCTRL9, reg);
412 1.1 christos }
413 1.1 christos AR_WRITE_BARRIER(sc);
414 1.1 christos }
415 1.1 christos
416 1.1 christos PUBLIC void
417 1.1 christos ar9280_olpc_get_pdadcs(struct athn_softc *sc, struct ieee80211_channel *c,
418 1.1 christos int chain, uint8_t *boundaries, uint8_t *pdadcs, uint8_t *txgain)
419 1.1 christos {
420 1.1 christos const struct ar5416_eeprom *eep = sc->sc_eep;
421 1.1 christos const struct ar_cal_data_per_freq_olpc *pierdata;
422 1.1 christos const uint8_t *pierfreq;
423 1.1 christos uint8_t fbin, pcdac, pwr, idx;
424 1.1 christos int i, lo, hi, npiers;
425 1.1 christos
426 1.1 christos if (IEEE80211_IS_CHAN_2GHZ(c)) {
427 1.1 christos pierfreq = eep->calFreqPier2G;
428 1.1 christos pierdata = (const struct ar_cal_data_per_freq_olpc *)
429 1.1 christos eep->calPierData2G[chain];
430 1.1 christos npiers = AR5416_NUM_2G_CAL_PIERS;
431 1.1 christos }
432 1.1 christos else {
433 1.1 christos pierfreq = eep->calFreqPier5G;
434 1.1 christos pierdata = (const struct ar_cal_data_per_freq_olpc *)
435 1.1 christos eep->calPierData5G[chain];
436 1.1 christos npiers = AR5416_NUM_5G_CAL_PIERS;
437 1.1 christos }
438 1.1 christos /* Find channel in ROM pier table. */
439 1.1 christos fbin = athn_chan2fbin(c);
440 1.1 christos athn_get_pier_ival(fbin, pierfreq, npiers, &lo, &hi);
441 1.1 christos
442 1.1 christos /* Get average. */
443 1.1 christos pwr = (pierdata[lo].pwrPdg[0][0] + pierdata[hi].pwrPdg[0][0]) / 2;
444 1.1 christos pwr /= 2; /* Convert to dB. */
445 1.1 christos
446 1.1 christos /* Find power control digital-to-analog converter (PCDAC) value. */
447 1.1 christos pcdac = pierdata[hi].pcdac[0][0];
448 1.1 christos for (idx = 0; idx < AR9280_TX_GAIN_TABLE_SIZE - 1; idx++)
449 1.1 christos if (pcdac <= sc->sc_tx_gain_tbl[idx])
450 1.1 christos break;
451 1.1 christos *txgain = idx;
452 1.1 christos
453 1.1 christos DPRINTFN(DBG_RF, sc,
454 1.1 christos "fbin=%d lo=%d hi=%d pwr=%d pcdac=%d txgain=%d\n",
455 1.1 christos fbin, lo, hi, pwr, pcdac, idx);
456 1.1 christos
457 1.1 christos /* Fill phase domain analog-to-digital converter (PDADC) table. */
458 1.1 christos for (i = 0; i < AR_NUM_PDADC_VALUES; i++)
459 1.1 christos pdadcs[i] = (i < pwr) ? 0x00 : 0xff;
460 1.1 christos
461 1.1 christos for (i = 0; i < AR_PD_GAINS_IN_MASK; i++)
462 1.1 christos boundaries[i] = AR9280_PD_GAIN_BOUNDARY_DEFAULT;
463 1.1 christos }
464 1.1 christos
465 1.1 christos PUBLIC void
466 1.1 christos ar9280_spur_mitigate(struct athn_softc *sc, struct ieee80211_channel *c,
467 1.1 christos struct ieee80211_channel *extc)
468 1.1 christos {
469 1.1 christos const struct ar_spur_chan *spurchans;
470 1.1 christos int spur, bin, spur_delta_phase, spur_freq_sd, spur_subchannel_sd;
471 1.1 christos int spur_off, range, i;
472 1.1 christos
473 1.1 christos /* NB: Always clear. */
474 1.1 christos AR_CLRBITS(sc, AR_PHY_FORCE_CLKEN_CCK, AR_PHY_FORCE_CLKEN_CCK_MRC_MUX);
475 1.1 christos
476 1.1 christos range = (extc != NULL) ? 19 : 10;
477 1.1 christos
478 1.1 christos spurchans = sc->sc_ops.get_spur_chans(sc, IEEE80211_IS_CHAN_2GHZ(c));
479 1.1 christos for (i = 0; i < AR_EEPROM_MODAL_SPURS; i++) {
480 1.1 christos spur = spurchans[i].spurChan;
481 1.1 christos if (spur == AR_NO_SPUR)
482 1.1 christos return; /* XXX disable if it was enabled! */
483 1.1 christos spur /= 10;
484 1.1 christos if (IEEE80211_IS_CHAN_2GHZ(c))
485 1.1 christos spur += AR_BASE_FREQ_2GHZ;
486 1.1 christos else
487 1.1 christos spur += AR_BASE_FREQ_5GHZ;
488 1.1 christos spur -= c->ic_freq;
489 1.1 christos if (abs(spur) < range)
490 1.1 christos break;
491 1.1 christos }
492 1.1 christos if (i == AR_EEPROM_MODAL_SPURS)
493 1.1 christos return; /* XXX disable if it was enabled! */
494 1.1 christos DPRINTFN(DBG_RF, sc, "enabling spur mitigation\n");
495 1.1 christos
496 1.1 christos AR_SETBITS(sc, AR_PHY_TIMING_CTRL4_0,
497 1.1 christos AR_PHY_TIMING_CTRL4_ENABLE_SPUR_RSSI |
498 1.1 christos AR_PHY_TIMING_CTRL4_ENABLE_SPUR_FILTER |
499 1.1 christos AR_PHY_TIMING_CTRL4_ENABLE_CHAN_MASK |
500 1.1 christos AR_PHY_TIMING_CTRL4_ENABLE_PILOT_MASK);
501 1.1 christos
502 1.1 christos AR_WRITE(sc, AR_PHY_SPUR_REG,
503 1.1 christos AR_PHY_SPUR_REG_MASK_RATE_CNTL |
504 1.1 christos AR_PHY_SPUR_REG_ENABLE_MASK_PPM |
505 1.1 christos AR_PHY_SPUR_REG_MASK_RATE_SELECT |
506 1.1 christos AR_PHY_SPUR_REG_ENABLE_VIT_SPUR_RSSI |
507 1.1 christos SM(AR_PHY_SPUR_REG_SPUR_RSSI_THRESH, AR_SPUR_RSSI_THRESH));
508 1.1 christos
509 1.1 christos #ifndef IEEE80211_NO_HT
510 1.1 christos if (extc != NULL) {
511 1.1 christos spur_delta_phase = (spur * 262144) / 10;
512 1.1 christos if (spur < 0) {
513 1.1 christos spur_subchannel_sd = 1;
514 1.1 christos spur_off = spur + 10;
515 1.1 christos }
516 1.1 christos else {
517 1.1 christos spur_subchannel_sd = 0;
518 1.1 christos spur_off = spur - 10;
519 1.1 christos }
520 1.1 christos }
521 1.1 christos else
522 1.1 christos #endif
523 1.1 christos {
524 1.1 christos spur_delta_phase = (spur * 524288) / 10;
525 1.1 christos spur_subchannel_sd = 0;
526 1.1 christos spur_off = spur;
527 1.1 christos }
528 1.1 christos if (IEEE80211_IS_CHAN_2GHZ(c))
529 1.1 christos spur_freq_sd = (spur_off * 2048) / 44;
530 1.1 christos else
531 1.1 christos spur_freq_sd = (spur_off * 2048) / 40;
532 1.1 christos
533 1.1 christos AR_WRITE(sc, AR_PHY_TIMING11,
534 1.1 christos AR_PHY_TIMING11_USE_SPUR_IN_AGC |
535 1.1 christos SM(AR_PHY_TIMING11_SPUR_FREQ_SD, spur_freq_sd) |
536 1.1 christos SM(AR_PHY_TIMING11_SPUR_DELTA_PHASE, spur_delta_phase));
537 1.1 christos
538 1.1 christos AR_WRITE(sc, AR_PHY_SFCORR_EXT,
539 1.1 christos SM(AR_PHY_SFCORR_SPUR_SUBCHNL_SD, spur_subchannel_sd));
540 1.1 christos AR_WRITE_BARRIER(sc);
541 1.1 christos
542 1.1 christos bin = spur * 320;
543 1.1 christos ar5008_set_viterbi_mask(sc, bin);
544 1.1 christos }
545 1.1 christos
546 1.1 christos PUBLIC void
547 1.1 christos ar9280_reset_rx_gain(struct athn_softc *sc, struct ieee80211_channel *c)
548 1.1 christos {
549 1.1 christos const struct athn_gain *prog = sc->sc_rx_gain;
550 1.1 christos const uint32_t *pvals;
551 1.1 christos int i;
552 1.1 christos
553 1.1 christos if (IEEE80211_IS_CHAN_2GHZ(c))
554 1.1 christos pvals = prog->vals_2g;
555 1.1 christos else
556 1.1 christos pvals = prog->vals_5g;
557 1.1 christos for (i = 0; i < prog->nregs; i++)
558 1.1 christos AR_WRITE(sc, prog->regs[i], pvals[i]);
559 1.1 christos }
560 1.1 christos
561 1.1 christos PUBLIC void
562 1.1 christos ar9280_reset_tx_gain(struct athn_softc *sc, struct ieee80211_channel *c)
563 1.1 christos {
564 1.1 christos const struct athn_gain *prog = sc->sc_tx_gain;
565 1.1 christos const uint32_t *pvals;
566 1.1 christos int i;
567 1.1 christos
568 1.1 christos if (IEEE80211_IS_CHAN_2GHZ(c))
569 1.1 christos pvals = prog->vals_2g;
570 1.1 christos else
571 1.1 christos pvals = prog->vals_5g;
572 1.1 christos for (i = 0; i < prog->nregs; i++)
573 1.1 christos AR_WRITE(sc, prog->regs[i], pvals[i]);
574 1.1 christos }
575 1.1 christos
576 1.1 christos Static void
577 1.1 christos ar9280_olpc_init(struct athn_softc *sc)
578 1.1 christos {
579 1.1 christos uint32_t reg;
580 1.1 christos int i;
581 1.1 christos
582 1.1 christos /* Save original Tx gain values. */
583 1.1 christos for (i = 0; i < AR9280_TX_GAIN_TABLE_SIZE; i++) {
584 1.1 christos reg = AR_READ(sc, AR_PHY_TX_GAIN_TBL(i));
585 1.1 christos sc->sc_tx_gain_tbl[i] = MS(reg, AR_PHY_TX_GAIN);
586 1.1 christos }
587 1.1 christos /* Initial Tx gain temperature compensation. */
588 1.1 christos sc->sc_tcomp = 0;
589 1.1 christos }
590 1.1 christos
591 1.1 christos Static void
592 1.1 christos ar9280_olpc_temp_compensation(struct athn_softc *sc)
593 1.1 christos {
594 1.1 christos const struct ar5416_eeprom *eep = sc->sc_eep;
595 1.1 christos int8_t pdadc, txgain, tcomp;
596 1.1 christos uint32_t reg;
597 1.1 christos int i;
598 1.1 christos
599 1.1 christos reg = AR_READ(sc, AR_PHY_TX_PWRCTRL4);
600 1.1 christos pdadc = MS(reg, AR_PHY_TX_PWRCTRL_PD_AVG_OUT);
601 1.1 christos DPRINTFN(DBG_RF, sc, "PD Avg Out=%d\n", pdadc);
602 1.1 christos
603 1.1 christos if (sc->sc_pdadc == 0 || pdadc == 0)
604 1.1 christos return; /* No frames transmitted yet. */
605 1.1 christos
606 1.1 christos /* Compute Tx gain temperature compensation. */
607 1.1 christos if (sc->sc_eep_rev >= AR_EEP_MINOR_VER_20 &&
608 1.1 christos eep->baseEepHeader.dacHiPwrMode_5G)
609 1.1 christos tcomp = (pdadc - sc->sc_pdadc + 4) / 8;
610 1.1 christos else
611 1.1 christos tcomp = (pdadc - sc->sc_pdadc + 5) / 10;
612 1.1 christos DPRINTFN(DBG_RF, sc, "OLPC temp compensation=%d\n", tcomp);
613 1.1 christos
614 1.1 christos if (tcomp == sc->sc_tcomp)
615 1.1 christos return; /* Don't rewrite the same values. */
616 1.1 christos sc->sc_tcomp = tcomp;
617 1.1 christos
618 1.1 christos /* Adjust Tx gain values. */
619 1.1 christos for (i = 0; i < AR9280_TX_GAIN_TABLE_SIZE; i++) {
620 1.1 christos txgain = sc->sc_tx_gain_tbl[i] - tcomp;
621 1.1 christos if (txgain < 0)
622 1.1 christos txgain = 0;
623 1.1 christos reg = AR_READ(sc, AR_PHY_TX_GAIN_TBL(i));
624 1.1 christos reg = RW(reg, AR_PHY_TX_GAIN, txgain);
625 1.1 christos AR_WRITE(sc, AR_PHY_TX_GAIN_TBL(i), reg);
626 1.1 christos }
627 1.1 christos AR_WRITE_BARRIER(sc);
628 1.1 christos }
629