arn5416.c revision 1.2 1 1.2 christos /* $NetBSD: arn5416.c,v 1.2 2013/04/03 14:20:02 christos Exp $ */
2 1.1 christos /* $OpenBSD: ar5416.c,v 1.12 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 AR5416, AR5418 and AR9160 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: arn5416.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>
68 1.1 christos #include <dev/ic/arn5416.h>
69 1.1 christos #include <dev/ic/arn9280.h>
70 1.1 christos
71 1.1 christos #define Static static
72 1.1 christos
73 1.1 christos Static void ar5416_force_bias(struct athn_softc *,
74 1.1 christos struct ieee80211_channel *);
75 1.1 christos Static void ar5416_get_pdadcs(struct athn_softc *,
76 1.1 christos struct ieee80211_channel *, int, int, uint8_t, uint8_t *,
77 1.1 christos uint8_t *);
78 1.1 christos Static void ar5416_init_from_rom(struct athn_softc *,
79 1.1 christos struct ieee80211_channel *, struct ieee80211_channel *);
80 1.1 christos Static uint8_t ar5416_reverse_bits(uint8_t, int);
81 1.1 christos Static void ar5416_rw_bank6tpc(struct athn_softc *,
82 1.1 christos struct ieee80211_channel *, uint32_t *);
83 1.1 christos Static void ar5416_rw_rfbits(uint32_t *, int, int, uint32_t, int);
84 1.1 christos Static void ar5416_set_power_calib(struct athn_softc *,
85 1.1 christos struct ieee80211_channel *);
86 1.1 christos Static int ar5416_set_synth(struct athn_softc *,
87 1.1 christos struct ieee80211_channel *, struct ieee80211_channel *);
88 1.1 christos Static void ar5416_setup(struct athn_softc *);
89 1.1 christos Static void ar5416_spur_mitigate(struct athn_softc *,
90 1.1 christos struct ieee80211_channel *, struct ieee80211_channel *);
91 1.1 christos Static void ar9160_rw_addac(struct athn_softc *,
92 1.1 christos struct ieee80211_channel *, uint32_t *);
93 1.1 christos
94 1.1 christos PUBLIC int
95 1.1 christos ar5416_attach(struct athn_softc *sc)
96 1.1 christos {
97 1.1 christos sc->sc_eep_base = AR5416_EEP_START_LOC;
98 1.1 christos sc->sc_eep_size = sizeof(struct ar5416_eeprom);
99 1.1 christos sc->sc_def_nf = AR5416_PHY_CCA_MAX_GOOD_VALUE;
100 1.1 christos sc->sc_ngpiopins = 14;
101 1.1 christos sc->sc_led_pin = 1;
102 1.1 christos sc->sc_workaround = AR5416_WA_DEFAULT;
103 1.1 christos sc->sc_ops.setup = ar5416_setup;
104 1.1 christos sc->sc_ops.swap_rom = ar5416_swap_rom;
105 1.1 christos sc->sc_ops.init_from_rom = ar5416_init_from_rom;
106 1.1 christos sc->sc_ops.set_txpower = ar5416_set_txpower;
107 1.1 christos sc->sc_ops.set_synth = ar5416_set_synth;
108 1.1 christos sc->sc_ops.spur_mitigate = ar5416_spur_mitigate;
109 1.1 christos sc->sc_ops.get_spur_chans = ar5416_get_spur_chans;
110 1.1 christos if (AR_SREV_9160_10_OR_LATER(sc))
111 1.1 christos sc->sc_ini = &ar9160_ini;
112 1.1 christos else
113 1.1 christos sc->sc_ini = &ar5416_ini;
114 1.1 christos sc->sc_serdes = &ar5416_serdes;
115 1.1 christos
116 1.1 christos return ar5008_attach(sc);
117 1.1 christos }
118 1.1 christos
119 1.1 christos Static void
120 1.1 christos ar5416_setup(struct athn_softc *sc)
121 1.1 christos {
122 1.1 christos /* Select ADDAC programming. */
123 1.1 christos if (AR_SREV_9160_11(sc))
124 1.1 christos sc->sc_addac = &ar9160_1_1_addac;
125 1.1 christos else if (AR_SREV_9160_10_OR_LATER(sc))
126 1.1 christos sc->sc_addac = &ar9160_1_0_addac;
127 1.1 christos else if (AR_SREV_5416_22_OR_LATER(sc))
128 1.1 christos sc->sc_addac = &ar5416_2_2_addac;
129 1.1 christos else
130 1.1 christos sc->sc_addac = &ar5416_2_1_addac;
131 1.1 christos }
132 1.1 christos
133 1.1 christos PUBLIC void
134 1.1 christos ar5416_swap_rom(struct athn_softc *sc)
135 1.1 christos {
136 1.1 christos struct ar5416_eeprom *eep = sc->sc_eep;
137 1.1 christos struct ar5416_modal_eep_header *modal;
138 1.1 christos int i, j;
139 1.1 christos
140 1.1 christos for (i = 0; i < 2; i++) { /* Dual-band. */
141 1.1 christos modal = &eep->modalHeader[i];
142 1.1 christos
143 1.1 christos modal->antCtrlCommon = bswap32(modal->antCtrlCommon);
144 1.1 christos for (j = 0; j < AR5416_MAX_CHAINS; j++) {
145 1.1 christos modal->antCtrlChain[j] =
146 1.1 christos bswap32(modal->antCtrlChain[j]);
147 1.1 christos }
148 1.1 christos for (j = 0; j < AR_EEPROM_MODAL_SPURS; j++) {
149 1.1 christos modal->spurChans[j].spurChan =
150 1.1 christos bswap16(modal->spurChans[j].spurChan);
151 1.1 christos }
152 1.1 christos }
153 1.1 christos }
154 1.1 christos
155 1.1 christos PUBLIC const struct ar_spur_chan *
156 1.1 christos ar5416_get_spur_chans(struct athn_softc *sc, int is2ghz)
157 1.1 christos {
158 1.1 christos const struct ar5416_eeprom *eep = sc->sc_eep;
159 1.1 christos
160 1.1 christos return eep->modalHeader[is2ghz].spurChans;
161 1.1 christos }
162 1.1 christos
163 1.1 christos Static int
164 1.1 christos ar5416_set_synth(struct athn_softc *sc, struct ieee80211_channel *c,
165 1.1 christos struct ieee80211_channel *extc)
166 1.1 christos {
167 1.1 christos uint32_t phy, reg;
168 1.1 christos uint32_t freq = c->ic_freq;
169 1.1 christos uint8_t chansel;
170 1.1 christos
171 1.1 christos phy = 0;
172 1.1 christos if (IEEE80211_IS_CHAN_2GHZ(c)) {
173 1.1 christos if (((freq - 2192) % 5) == 0) {
174 1.1 christos chansel = ((freq - 672) * 2 - 3040) / 10;
175 1.1 christos }
176 1.1 christos else if (((freq - 2224) % 5) == 0) {
177 1.1 christos chansel = ((freq - 704) * 2 - 3040) / 10;
178 1.1 christos phy |= AR5416_BMODE_SYNTH;
179 1.1 christos }
180 1.1 christos else
181 1.1 christos return EINVAL;
182 1.1 christos chansel <<= 2;
183 1.1 christos
184 1.1 christos reg = AR_READ(sc, AR_PHY_CCK_TX_CTRL);
185 1.1 christos if (freq == 2484) /* Channel 14. */
186 1.1 christos reg |= AR_PHY_CCK_TX_CTRL_JAPAN;
187 1.1 christos else
188 1.1 christos reg &= ~AR_PHY_CCK_TX_CTRL_JAPAN;
189 1.1 christos AR_WRITE(sc, AR_PHY_CCK_TX_CTRL, reg);
190 1.1 christos
191 1.1 christos /* Fix for orientation sensitivity issue. */
192 1.1 christos if (AR_SREV_5416(sc))
193 1.1 christos ar5416_force_bias(sc, c);
194 1.1 christos }
195 1.1 christos else {
196 1.1 christos if (freq >= 5120 && (freq % 20) == 0) {
197 1.1 christos chansel = (freq - 4800) / 20;
198 1.1 christos chansel <<= 2;
199 1.1 christos phy |= SM(AR5416_AMODE_REFSEL, 2);
200 1.1 christos }
201 1.1 christos else if ((freq % 10) == 0) {
202 1.1 christos chansel = (freq - 4800) / 10;
203 1.1 christos chansel <<= 1;
204 1.1 christos if (AR_SREV_9160_10_OR_LATER(sc))
205 1.1 christos phy |= SM(AR5416_AMODE_REFSEL, 1);
206 1.1 christos else
207 1.1 christos phy |= SM(AR5416_AMODE_REFSEL, 2);
208 1.1 christos }
209 1.1 christos else if ((freq % 5) == 0) {
210 1.1 christos chansel = (freq - 4800) / 5;
211 1.1 christos phy |= SM(AR5416_AMODE_REFSEL, 2);
212 1.1 christos }
213 1.1 christos else
214 1.1 christos return EINVAL;
215 1.1 christos }
216 1.1 christos chansel = ar5416_reverse_bits(chansel, 8);
217 1.1 christos phy |= chansel << 8 | 1 << 5 | 1;
218 1.1 christos DPRINTFN(DBG_RF, sc, "AR_PHY(0x37)=0x%08x\n", phy);
219 1.1 christos AR_WRITE(sc, AR_PHY(0x37), phy);
220 1.1 christos return 0;
221 1.1 christos }
222 1.1 christos
223 1.1 christos Static void
224 1.1 christos ar5416_init_from_rom(struct athn_softc *sc, struct ieee80211_channel *c,
225 1.1 christos struct ieee80211_channel *extc)
226 1.1 christos {
227 1.1 christos static const uint32_t chainoffset[] = { 0x0000, 0x2000, 0x1000 };
228 1.1 christos const struct ar5416_eeprom *eep = sc->sc_eep;
229 1.1 christos const struct ar5416_modal_eep_header *modal;
230 1.1 christos uint32_t reg, offset;
231 1.1 christos uint8_t txRxAtten;
232 1.1 christos int i;
233 1.1 christos
234 1.1 christos modal = &eep->modalHeader[IEEE80211_IS_CHAN_2GHZ(c)];
235 1.1 christos
236 1.1 christos AR_WRITE(sc, AR_PHY_SWITCH_COM, modal->antCtrlCommon);
237 1.1 christos
238 1.1 christos for (i = 0; i < AR5416_MAX_CHAINS; i++) {
239 1.1 christos if (AR_SREV_5416_20_OR_LATER(sc) &&
240 1.1 christos (sc->sc_rxchainmask == 0x5 || sc->sc_txchainmask == 0x5))
241 1.1 christos offset = chainoffset[i];
242 1.1 christos else
243 1.1 christos offset = i * 0x1000;
244 1.1 christos
245 1.1 christos AR_WRITE(sc, AR_PHY_SWITCH_CHAIN_0 + offset,
246 1.1 christos modal->antCtrlChain[i]);
247 1.1 christos
248 1.1 christos reg = AR_READ(sc, AR_PHY_TIMING_CTRL4_0 + offset);
249 1.1 christos reg = RW(reg, AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF,
250 1.1 christos modal->iqCalICh[i]);
251 1.1 christos reg = RW(reg, AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF,
252 1.1 christos modal->iqCalQCh[i]);
253 1.1 christos AR_WRITE(sc, AR_PHY_TIMING_CTRL4_0 + offset, reg);
254 1.1 christos
255 1.1 christos if (i > 0 && !AR_SREV_5416_20_OR_LATER(sc))
256 1.1 christos continue;
257 1.1 christos
258 1.1 christos if (sc->sc_eep_rev >= AR_EEP_MINOR_VER_3) {
259 1.1 christos reg = AR_READ(sc, AR_PHY_GAIN_2GHZ + offset);
260 1.1 christos reg = RW(reg, AR_PHY_GAIN_2GHZ_BSW_MARGIN,
261 1.1 christos modal->bswMargin[i]);
262 1.1 christos reg = RW(reg, AR_PHY_GAIN_2GHZ_BSW_ATTEN,
263 1.1 christos modal->bswAtten[i]);
264 1.1 christos AR_WRITE(sc, AR_PHY_GAIN_2GHZ + offset, reg);
265 1.1 christos }
266 1.1 christos if (sc->sc_eep_rev >= AR_EEP_MINOR_VER_3)
267 1.1 christos txRxAtten = modal->txRxAttenCh[i];
268 1.1 christos else /* Workaround for ROM versions < 14.3. */
269 1.1 christos txRxAtten = IEEE80211_IS_CHAN_2GHZ(c) ? 23 : 44;
270 1.1 christos reg = AR_READ(sc, AR_PHY_RXGAIN + offset);
271 1.1 christos reg = RW(reg, AR_PHY_RXGAIN_TXRX_ATTEN, txRxAtten);
272 1.1 christos AR_WRITE(sc, AR_PHY_RXGAIN + offset, reg);
273 1.1 christos
274 1.1 christos reg = AR_READ(sc, AR_PHY_GAIN_2GHZ + offset);
275 1.1 christos reg = RW(reg, AR_PHY_GAIN_2GHZ_RXTX_MARGIN,
276 1.1 christos modal->rxTxMarginCh[i]);
277 1.1 christos AR_WRITE(sc, AR_PHY_GAIN_2GHZ + offset, reg);
278 1.1 christos }
279 1.1 christos reg = AR_READ(sc, AR_PHY_SETTLING);
280 1.1 christos reg = RW(reg, AR_PHY_SETTLING_SWITCH, modal->switchSettling);
281 1.1 christos AR_WRITE(sc, AR_PHY_SETTLING, reg);
282 1.1 christos
283 1.1 christos reg = AR_READ(sc, AR_PHY_DESIRED_SZ);
284 1.1 christos reg = RW(reg, AR_PHY_DESIRED_SZ_ADC, modal->adcDesiredSize);
285 1.1 christos reg = RW(reg, AR_PHY_DESIRED_SZ_PGA, modal->pgaDesiredSize);
286 1.1 christos AR_WRITE(sc, AR_PHY_DESIRED_SZ, reg);
287 1.1 christos
288 1.1 christos reg = SM(AR_PHY_RF_CTL4_TX_END_XPAA_OFF, modal->txEndToXpaOff);
289 1.1 christos reg |= SM(AR_PHY_RF_CTL4_TX_END_XPAB_OFF, modal->txEndToXpaOff);
290 1.1 christos reg |= SM(AR_PHY_RF_CTL4_FRAME_XPAA_ON, modal->txFrameToXpaOn);
291 1.1 christos reg |= SM(AR_PHY_RF_CTL4_FRAME_XPAB_ON, modal->txFrameToXpaOn);
292 1.1 christos AR_WRITE(sc, AR_PHY_RF_CTL4, reg);
293 1.1 christos
294 1.1 christos reg = AR_READ(sc, AR_PHY_RF_CTL3);
295 1.1 christos reg = RW(reg, AR_PHY_TX_END_TO_A2_RX_ON, modal->txEndToRxOn);
296 1.1 christos AR_WRITE(sc, AR_PHY_RF_CTL3, reg);
297 1.1 christos
298 1.1 christos reg = AR_READ(sc, AR_PHY_CCA(0));
299 1.1 christos reg = RW(reg, AR_PHY_CCA_THRESH62, modal->thresh62);
300 1.1 christos AR_WRITE(sc, AR_PHY_CCA(0), reg);
301 1.1 christos
302 1.1 christos reg = AR_READ(sc, AR_PHY_EXT_CCA(0));
303 1.1 christos reg = RW(reg, AR_PHY_EXT_CCA_THRESH62, modal->thresh62);
304 1.1 christos AR_WRITE(sc, AR_PHY_EXT_CCA(0), reg);
305 1.1 christos
306 1.1 christos if (sc->sc_eep_rev >= AR_EEP_MINOR_VER_2) {
307 1.1 christos reg = AR_READ(sc, AR_PHY_RF_CTL2);
308 1.1 christos reg = RW(reg, AR_PHY_TX_END_DATA_START,
309 1.1 christos modal->txFrameToDataStart);
310 1.1 christos reg = RW(reg, AR_PHY_TX_END_PA_ON, modal->txFrameToPaOn);
311 1.1 christos AR_WRITE(sc, AR_PHY_RF_CTL2, reg);
312 1.1 christos }
313 1.1 christos #ifndef IEEE80211_NO_HT
314 1.1 christos if (sc->sc_eep_rev >= AR_EEP_MINOR_VER_3 && extc != NULL) {
315 1.1 christos /* Overwrite switch settling with HT-40 value. */
316 1.1 christos reg = AR_READ(sc, AR_PHY_SETTLING);
317 1.1 christos reg = RW(reg, AR_PHY_SETTLING_SWITCH, modal->swSettleHt40);
318 1.1 christos AR_WRITE(sc, AR_PHY_SETTLING, reg);
319 1.1 christos }
320 1.1 christos #endif
321 1.1 christos }
322 1.1 christos
323 1.1 christos PUBLIC int
324 1.1 christos ar5416_init_calib(struct athn_softc *sc, struct ieee80211_channel *c,
325 1.1 christos struct ieee80211_channel *extc)
326 1.1 christos {
327 1.1 christos int ntries;
328 1.1 christos
329 1.1 christos if (AR_SREV_9280_10_OR_LATER(sc)) {
330 1.1 christos /* XXX Linux tests AR9287?! */
331 1.1 christos AR_CLRBITS(sc, AR_PHY_ADC_CTL, AR_PHY_ADC_CTL_OFF_PWDADC);
332 1.1 christos AR_SETBITS(sc, AR_PHY_AGC_CONTROL,
333 1.1 christos AR_PHY_AGC_CONTROL_FLTR_CAL);
334 1.1 christos }
335 1.1 christos /* Calibrate the AGC. */
336 1.1 christos AR_SETBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL);
337 1.1 christos /* Poll for offset calibration completion. */
338 1.1 christos for (ntries = 0; ntries < 10000; ntries++) {
339 1.1 christos if (!(AR_READ(sc, AR_PHY_AGC_CONTROL) &
340 1.1 christos AR_PHY_AGC_CONTROL_CAL))
341 1.1 christos break;
342 1.1 christos DELAY(10);
343 1.1 christos }
344 1.1 christos if (ntries == 10000)
345 1.1 christos return ETIMEDOUT;
346 1.1 christos if (AR_SREV_9280_10_OR_LATER(sc)) {
347 1.1 christos AR_SETBITS(sc, AR_PHY_ADC_CTL, AR_PHY_ADC_CTL_OFF_PWDADC);
348 1.1 christos AR_CLRBITS(sc, AR_PHY_AGC_CONTROL,
349 1.1 christos AR_PHY_AGC_CONTROL_FLTR_CAL);
350 1.1 christos }
351 1.1 christos return 0;
352 1.1 christos }
353 1.1 christos
354 1.1 christos Static void
355 1.1 christos ar5416_get_pdadcs(struct athn_softc *sc, struct ieee80211_channel *c,
356 1.1 christos int chain, int nxpdgains, uint8_t overlap, uint8_t *boundaries,
357 1.1 christos uint8_t *pdadcs)
358 1.1 christos {
359 1.1 christos const struct ar5416_eeprom *eep = sc->sc_eep;
360 1.1 christos const struct ar5416_cal_data_per_freq *pierdata;
361 1.1 christos const uint8_t *pierfreq;
362 1.1 christos struct athn_pier lopier, hipier;
363 1.1 christos int16_t delta;
364 1.1 christos uint8_t fbin, pwroff;
365 1.1 christos int i, lo, hi, npiers;
366 1.1 christos
367 1.1 christos if (IEEE80211_IS_CHAN_2GHZ(c)) {
368 1.1 christos pierfreq = eep->calFreqPier2G;
369 1.1 christos pierdata = eep->calPierData2G[chain];
370 1.1 christos npiers = AR5416_NUM_2G_CAL_PIERS;
371 1.1 christos }
372 1.1 christos else {
373 1.1 christos pierfreq = eep->calFreqPier5G;
374 1.1 christos pierdata = eep->calPierData5G[chain];
375 1.1 christos npiers = AR5416_NUM_5G_CAL_PIERS;
376 1.1 christos }
377 1.1 christos /* Find channel in ROM pier table. */
378 1.1 christos fbin = athn_chan2fbin(c);
379 1.1 christos athn_get_pier_ival(fbin, pierfreq, npiers, &lo, &hi);
380 1.1 christos
381 1.1 christos lopier.fbin = pierfreq[lo];
382 1.1 christos hipier.fbin = pierfreq[hi];
383 1.1 christos for (i = 0; i < nxpdgains; i++) {
384 1.1 christos lopier.pwr[i] = pierdata[lo].pwrPdg[i];
385 1.1 christos lopier.vpd[i] = pierdata[lo].vpdPdg[i];
386 1.1 christos hipier.pwr[i] = pierdata[lo].pwrPdg[i];
387 1.1 christos hipier.vpd[i] = pierdata[lo].vpdPdg[i];
388 1.1 christos }
389 1.1 christos ar5008_get_pdadcs(sc, fbin, &lopier, &hipier, nxpdgains,
390 1.1 christos AR5416_PD_GAIN_ICEPTS, overlap, boundaries, pdadcs);
391 1.1 christos
392 1.1 christos if (!AR_SREV_9280_20_OR_LATER(sc))
393 1.1 christos return;
394 1.1 christos
395 1.1 christos if (sc->sc_eep_rev >= AR_EEP_MINOR_VER_21)
396 1.1 christos pwroff = eep->baseEepHeader.pwrTableOffset;
397 1.1 christos else
398 1.1 christos pwroff = AR_PWR_TABLE_OFFSET_DB;
399 1.1 christos delta = (pwroff - AR_PWR_TABLE_OFFSET_DB) * 2; /* In half dB. */
400 1.1 christos
401 1.1 christos /* Change the original gain boundaries setting. */
402 1.1 christos for (i = 0; i < nxpdgains; i++) {
403 1.1 christos /* XXX Possible overflows? */
404 1.1 christos boundaries[i] -= delta;
405 1.1 christos if (boundaries[i] > AR_MAX_RATE_POWER - overlap)
406 1.1 christos boundaries[i] = AR_MAX_RATE_POWER - overlap;
407 1.1 christos }
408 1.1 christos if (delta != 0) {
409 1.1 christos /* Shift the PDADC table to start at the new offset. */
410 1.1 christos for (i = 0; i < AR_NUM_PDADC_VALUES; i++)
411 1.1 christos pdadcs[i] = pdadcs[MIN(i + delta,
412 1.1 christos AR_NUM_PDADC_VALUES - 1)];
413 1.1 christos }
414 1.1 christos }
415 1.1 christos
416 1.1 christos Static void
417 1.1 christos ar5416_set_power_calib(struct athn_softc *sc, struct ieee80211_channel *c)
418 1.1 christos {
419 1.1 christos static const uint32_t chainoffset[] = { 0x0000, 0x2000, 0x1000 };
420 1.1 christos const struct ar5416_eeprom *eep = sc->sc_eep;
421 1.1 christos const struct ar5416_modal_eep_header *modal;
422 1.1 christos uint8_t boundaries[AR_PD_GAINS_IN_MASK];
423 1.1 christos uint8_t pdadcs[AR_NUM_PDADC_VALUES];
424 1.1 christos uint8_t xpdgains[AR5416_NUM_PD_GAINS];
425 1.1 christos uint8_t overlap, txgain;
426 1.1 christos uint32_t reg, offset;
427 1.1 christos int i, j, nxpdgains;
428 1.1 christos
429 1.1 christos modal = &eep->modalHeader[IEEE80211_IS_CHAN_2GHZ(c)];
430 1.1 christos
431 1.1 christos if (sc->sc_eep_rev < AR_EEP_MINOR_VER_2) {
432 1.1 christos overlap = MS(AR_READ(sc, AR_PHY_TPCRG5),
433 1.1 christos AR_PHY_TPCRG5_PD_GAIN_OVERLAP);
434 1.1 christos }
435 1.1 christos else
436 1.1 christos overlap = modal->pdGainOverlap;
437 1.1 christos
438 1.1 christos if ((sc->sc_flags & ATHN_FLAG_OLPC) && IEEE80211_IS_CHAN_2GHZ(c)) {
439 1.1 christos /* XXX not here. */
440 1.1 christos sc->sc_pdadc =
441 1.1 christos ((const struct ar_cal_data_per_freq_olpc *)
442 1.1 christos eep->calPierData2G[0])->vpdPdg[0][0];
443 1.1 christos }
444 1.1 christos
445 1.1 christos nxpdgains = 0;
446 1.1 christos memset(xpdgains, 0, sizeof(xpdgains));
447 1.1 christos for (i = AR5416_PD_GAINS_IN_MASK - 1; i >= 0; i--) {
448 1.1 christos if (nxpdgains >= AR5416_NUM_PD_GAINS)
449 1.1 christos break; /* Can't happen. */
450 1.1 christos if (modal->xpdGain & (1 << i))
451 1.1 christos xpdgains[nxpdgains++] = i;
452 1.1 christos }
453 1.1 christos reg = AR_READ(sc, AR_PHY_TPCRG1);
454 1.1 christos reg = RW(reg, AR_PHY_TPCRG1_NUM_PD_GAIN, nxpdgains - 1);
455 1.1 christos reg = RW(reg, AR_PHY_TPCRG1_PD_GAIN_1, xpdgains[0]);
456 1.1 christos reg = RW(reg, AR_PHY_TPCRG1_PD_GAIN_2, xpdgains[1]);
457 1.1 christos reg = RW(reg, AR_PHY_TPCRG1_PD_GAIN_3, xpdgains[2]);
458 1.1 christos AR_WRITE(sc, AR_PHY_TPCRG1, reg);
459 1.1 christos
460 1.1 christos for (i = 0; i < AR5416_MAX_CHAINS; i++) {
461 1.1 christos if (!(sc->sc_txchainmask & (1 << i)))
462 1.1 christos continue;
463 1.1 christos
464 1.1 christos if (AR_SREV_5416_20_OR_LATER(sc) &&
465 1.1 christos (sc->sc_rxchainmask == 0x5 || sc->sc_txchainmask == 0x5))
466 1.1 christos offset = chainoffset[i];
467 1.1 christos else
468 1.1 christos offset = i * 0x1000;
469 1.1 christos
470 1.1 christos if (sc->sc_flags & ATHN_FLAG_OLPC) {
471 1.1 christos ar9280_olpc_get_pdadcs(sc, c, i, boundaries,
472 1.1 christos pdadcs, &txgain);
473 1.1 christos
474 1.1 christos reg = AR_READ(sc, AR_PHY_TX_PWRCTRL6_0);
475 1.1 christos reg = RW(reg, AR_PHY_TX_PWRCTRL_ERR_EST_MODE, 3);
476 1.1 christos AR_WRITE(sc, AR_PHY_TX_PWRCTRL6_0, reg);
477 1.1 christos
478 1.1 christos reg = AR_READ(sc, AR_PHY_TX_PWRCTRL6_1);
479 1.1 christos reg = RW(reg, AR_PHY_TX_PWRCTRL_ERR_EST_MODE, 3);
480 1.1 christos AR_WRITE(sc, AR_PHY_TX_PWRCTRL6_1, reg);
481 1.1 christos
482 1.1 christos reg = AR_READ(sc, AR_PHY_TX_PWRCTRL7);
483 1.1 christos reg = RW(reg, AR_PHY_TX_PWRCTRL_INIT_TX_GAIN, txgain);
484 1.1 christos AR_WRITE(sc, AR_PHY_TX_PWRCTRL7, reg);
485 1.1 christos
486 1.1 christos overlap = 6;
487 1.1 christos }
488 1.1 christos else {
489 1.1 christos ar5416_get_pdadcs(sc, c, i, nxpdgains, overlap,
490 1.1 christos boundaries, pdadcs);
491 1.1 christos }
492 1.1 christos /* Write boundaries. */
493 1.1 christos if (i == 0 || AR_SREV_5416_20_OR_LATER(sc)) {
494 1.1 christos reg = SM(AR_PHY_TPCRG5_PD_GAIN_OVERLAP,
495 1.1 christos overlap);
496 1.1 christos reg |= SM(AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_1,
497 1.1 christos boundaries[0]);
498 1.1 christos reg |= SM(AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_2,
499 1.1 christos boundaries[1]);
500 1.1 christos reg |= SM(AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_3,
501 1.1 christos boundaries[2]);
502 1.1 christos reg |= SM(AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_4,
503 1.1 christos boundaries[3]);
504 1.1 christos AR_WRITE(sc, AR_PHY_TPCRG5 + offset, reg);
505 1.1 christos }
506 1.1 christos /* Write PDADC values. */
507 1.1 christos for (j = 0; j < AR_NUM_PDADC_VALUES; j += 4) {
508 1.1 christos AR_WRITE(sc, AR_PHY_PDADC_TBL_BASE + offset + j,
509 1.1 christos pdadcs[j + 0] << 0 |
510 1.1 christos pdadcs[j + 1] << 8 |
511 1.1 christos pdadcs[j + 2] << 16 |
512 1.1 christos pdadcs[j + 3] << 24);
513 1.1 christos }
514 1.1 christos }
515 1.1 christos }
516 1.1 christos
517 1.1 christos PUBLIC void
518 1.1 christos ar5416_set_txpower(struct athn_softc *sc, struct ieee80211_channel *c,
519 1.1 christos struct ieee80211_channel *extc)
520 1.1 christos {
521 1.1 christos const struct ar5416_eeprom *eep = sc->sc_eep;
522 1.1 christos const struct ar5416_modal_eep_header *modal;
523 1.1 christos uint8_t tpow_cck[4], tpow_ofdm[4];
524 1.1 christos #ifndef IEEE80211_NO_HT
525 1.1 christos uint8_t tpow_cck_ext[4], tpow_ofdm_ext[4];
526 1.1 christos uint8_t tpow_ht20[8], tpow_ht40[8];
527 1.1 christos uint8_t ht40inc;
528 1.1 christos #endif
529 1.1 christos int16_t pwr = 0, pwroff, max_ant_gain, power[ATHN_POWER_COUNT];
530 1.1 christos uint8_t cckinc;
531 1.1 christos int i;
532 1.1 christos
533 1.1 christos ar5416_set_power_calib(sc, c);
534 1.1 christos
535 1.1 christos modal = &eep->modalHeader[IEEE80211_IS_CHAN_2GHZ(c)];
536 1.1 christos
537 1.1 christos /* Compute transmit power reduction due to antenna gain. */
538 1.1 christos max_ant_gain = MAX(modal->antennaGainCh[0], modal->antennaGainCh[1]);
539 1.1 christos max_ant_gain = MAX(modal->antennaGainCh[2], max_ant_gain);
540 1.1 christos /* XXX */
541 1.1 christos
542 1.1 christos /*
543 1.1 christos * Reduce scaled power by number of active chains to get per-chain
544 1.1 christos * transmit power level.
545 1.1 christos */
546 1.1 christos if (sc->sc_ntxchains == 2)
547 1.1 christos pwr -= AR_PWR_DECREASE_FOR_2_CHAIN;
548 1.1 christos else if (sc->sc_ntxchains == 3)
549 1.1 christos pwr -= AR_PWR_DECREASE_FOR_3_CHAIN;
550 1.1 christos if (pwr < 0)
551 1.1 christos pwr = 0;
552 1.1 christos
553 1.1 christos if (IEEE80211_IS_CHAN_2GHZ(c)) {
554 1.1 christos /* Get CCK target powers. */
555 1.1 christos ar5008_get_lg_tpow(sc, c, AR_CTL_11B, eep->calTargetPowerCck,
556 1.1 christos AR5416_NUM_2G_CCK_TARGET_POWERS, tpow_cck);
557 1.1 christos
558 1.1 christos /* Get OFDM target powers. */
559 1.1 christos ar5008_get_lg_tpow(sc, c, AR_CTL_11G, eep->calTargetPower2G,
560 1.1 christos AR5416_NUM_2G_20_TARGET_POWERS, tpow_ofdm);
561 1.1 christos
562 1.1 christos #ifndef IEEE80211_NO_HT
563 1.1 christos /* Get HT-20 target powers. */
564 1.1 christos ar5008_get_ht_tpow(sc, c, AR_CTL_2GHT20,
565 1.1 christos eep->calTargetPower2GHT20, AR5416_NUM_2G_20_TARGET_POWERS,
566 1.1 christos tpow_ht20);
567 1.1 christos
568 1.1 christos if (extc != NULL) {
569 1.1 christos /* Get HT-40 target powers. */
570 1.1 christos ar5008_get_ht_tpow(sc, c, AR_CTL_2GHT40,
571 1.1 christos eep->calTargetPower2GHT40,
572 1.1 christos AR5416_NUM_2G_40_TARGET_POWERS, tpow_ht40);
573 1.1 christos
574 1.1 christos /* Get secondary channel CCK target powers. */
575 1.1 christos ar5008_get_lg_tpow(sc, extc, AR_CTL_11B,
576 1.1 christos eep->calTargetPowerCck,
577 1.1 christos AR5416_NUM_2G_CCK_TARGET_POWERS, tpow_cck_ext);
578 1.1 christos
579 1.1 christos /* Get secondary channel OFDM target powers. */
580 1.1 christos ar5008_get_lg_tpow(sc, extc, AR_CTL_11G,
581 1.1 christos eep->calTargetPower2G,
582 1.1 christos AR5416_NUM_2G_20_TARGET_POWERS, tpow_ofdm_ext);
583 1.1 christos }
584 1.1 christos #endif
585 1.1 christos }
586 1.1 christos else {
587 1.1 christos /* Get OFDM target powers. */
588 1.1 christos ar5008_get_lg_tpow(sc, c, AR_CTL_11A, eep->calTargetPower5G,
589 1.1 christos AR5416_NUM_5G_20_TARGET_POWERS, tpow_ofdm);
590 1.1 christos
591 1.1 christos #ifndef IEEE80211_NO_HT
592 1.1 christos /* Get HT-20 target powers. */
593 1.1 christos ar5008_get_ht_tpow(sc, c, AR_CTL_5GHT20,
594 1.1 christos eep->calTargetPower5GHT20, AR5416_NUM_5G_20_TARGET_POWERS,
595 1.1 christos tpow_ht20);
596 1.1 christos
597 1.1 christos if (extc != NULL) {
598 1.1 christos /* Get HT-40 target powers. */
599 1.1 christos ar5008_get_ht_tpow(sc, c, AR_CTL_5GHT40,
600 1.1 christos eep->calTargetPower5GHT40,
601 1.1 christos AR5416_NUM_5G_40_TARGET_POWERS, tpow_ht40);
602 1.1 christos
603 1.1 christos /* Get secondary channel OFDM target powers. */
604 1.1 christos ar5008_get_lg_tpow(sc, extc, AR_CTL_11A,
605 1.1 christos eep->calTargetPower5G,
606 1.1 christos AR5416_NUM_5G_20_TARGET_POWERS, tpow_ofdm_ext);
607 1.1 christos }
608 1.1 christos #endif
609 1.1 christos }
610 1.1 christos
611 1.1 christos /* Compute CCK/OFDM delta. */
612 1.1 christos cckinc = (sc->sc_flags & ATHN_FLAG_OLPC) ? -2 : 0;
613 1.1 christos
614 1.1 christos memset(power, 0, sizeof(power));
615 1.1 christos /* Shuffle target powers accross transmit rates. */
616 1.1 christos power[ATHN_POWER_OFDM6 ] =
617 1.1 christos power[ATHN_POWER_OFDM9 ] =
618 1.1 christos power[ATHN_POWER_OFDM12] =
619 1.1 christos power[ATHN_POWER_OFDM18] =
620 1.1 christos power[ATHN_POWER_OFDM24] = tpow_ofdm[0];
621 1.1 christos power[ATHN_POWER_OFDM36] = tpow_ofdm[1];
622 1.1 christos power[ATHN_POWER_OFDM48] = tpow_ofdm[2];
623 1.1 christos power[ATHN_POWER_OFDM54] = tpow_ofdm[3];
624 1.1 christos power[ATHN_POWER_XR ] = tpow_ofdm[0];
625 1.1 christos if (IEEE80211_IS_CHAN_2GHZ(c)) {
626 1.1 christos power[ATHN_POWER_CCK1_LP ] = tpow_cck[0] + cckinc;
627 1.1 christos power[ATHN_POWER_CCK2_LP ] =
628 1.1 christos power[ATHN_POWER_CCK2_SP ] = tpow_cck[1] + cckinc;
629 1.1 christos power[ATHN_POWER_CCK55_LP] =
630 1.1 christos power[ATHN_POWER_CCK55_SP] = tpow_cck[2] + cckinc;
631 1.1 christos power[ATHN_POWER_CCK11_LP] =
632 1.1 christos power[ATHN_POWER_CCK11_SP] = tpow_cck[3] + cckinc;
633 1.1 christos }
634 1.1 christos #ifndef IEEE80211_NO_HT
635 1.1 christos for (i = 0; i < nitems(tpow_ht20); i++)
636 1.1 christos power[ATHN_POWER_HT20(i)] = tpow_ht20[i];
637 1.1 christos if (extc != NULL) {
638 1.1 christos /* Correct PAR difference between HT40 and HT20/Legacy. */
639 1.1 christos if (sc->sc_eep_rev >= AR_EEP_MINOR_VER_2)
640 1.1 christos ht40inc = modal->ht40PowerIncForPdadc;
641 1.1 christos else
642 1.1 christos ht40inc = AR_HT40_POWER_INC_FOR_PDADC;
643 1.1 christos for (i = 0; i < nitems(tpow_ht40); i++)
644 1.1 christos power[ATHN_POWER_HT40(i)] = tpow_ht40[i] + ht40inc;
645 1.1 christos power[ATHN_POWER_OFDM_DUP] = tpow_ht40[0];
646 1.1 christos power[ATHN_POWER_CCK_DUP ] = tpow_ht40[0] + cckinc;
647 1.1 christos power[ATHN_POWER_OFDM_EXT] = tpow_ofdm_ext[0];
648 1.1 christos if (IEEE80211_IS_CHAN_2GHZ(c))
649 1.1 christos power[ATHN_POWER_CCK_EXT] = tpow_cck_ext[0] + cckinc;
650 1.1 christos }
651 1.1 christos #endif
652 1.1 christos
653 1.1 christos if (AR_SREV_9280_10_OR_LATER(sc)) {
654 1.1 christos if (sc->sc_eep_rev >= AR_EEP_MINOR_VER_21)
655 1.1 christos pwroff = eep->baseEepHeader.pwrTableOffset;
656 1.1 christos else
657 1.1 christos pwroff = AR_PWR_TABLE_OFFSET_DB;
658 1.1 christos for (i = 0; i < ATHN_POWER_COUNT; i++)
659 1.1 christos power[i] -= pwroff * 2; /* In half dB. */
660 1.1 christos }
661 1.1 christos for (i = 0; i < ATHN_POWER_COUNT; i++) {
662 1.1 christos if (power[i] > AR_MAX_RATE_POWER)
663 1.1 christos power[i] = AR_MAX_RATE_POWER;
664 1.1 christos }
665 1.1 christos
666 1.1 christos /* Write transmit power values to hardware. */
667 1.1 christos ar5008_write_txpower(sc, power);
668 1.1 christos
669 1.1 christos /*
670 1.1 christos * Write transmit power substraction for dynamic chain changing
671 1.1 christos * and per-packet transmit power.
672 1.1 christos */
673 1.1 christos AR_WRITE(sc, AR_PHY_POWER_TX_SUB,
674 1.1 christos (modal->pwrDecreaseFor3Chain & 0x3f) << 6 |
675 1.1 christos (modal->pwrDecreaseFor2Chain & 0x3f));
676 1.1 christos }
677 1.1 christos
678 1.1 christos Static void
679 1.1 christos ar5416_spur_mitigate(struct athn_softc *sc, struct ieee80211_channel *c,
680 1.1 christos struct ieee80211_channel *extc)
681 1.1 christos {
682 1.1 christos const struct ar_spur_chan *spurchans;
683 1.1 christos int i, spur, bin, spur_delta_phase, spur_freq_sd;
684 1.1 christos
685 1.1 christos spurchans = sc->sc_ops.get_spur_chans(sc, IEEE80211_IS_CHAN_2GHZ(c));
686 1.1 christos for (i = 0; i < AR_EEPROM_MODAL_SPURS; i++) {
687 1.1 christos spur = spurchans[i].spurChan;
688 1.1 christos if (spur == AR_NO_SPUR)
689 1.1 christos return; /* XXX disable if it was enabled! */
690 1.1 christos spur -= c->ic_freq * 10;
691 1.1 christos /* Verify range +/-9.5MHz */
692 1.1 christos if (abs(spur) < 95)
693 1.1 christos break;
694 1.1 christos }
695 1.1 christos if (i == AR_EEPROM_MODAL_SPURS)
696 1.1 christos return; /* XXX disable if it was enabled! */
697 1.1 christos DPRINTFN(DBG_RF, sc, "enabling spur mitigation\n");
698 1.1 christos
699 1.1 christos AR_SETBITS(sc, AR_PHY_TIMING_CTRL4_0,
700 1.1 christos AR_PHY_TIMING_CTRL4_ENABLE_SPUR_RSSI |
701 1.1 christos AR_PHY_TIMING_CTRL4_ENABLE_SPUR_FILTER |
702 1.1 christos AR_PHY_TIMING_CTRL4_ENABLE_CHAN_MASK |
703 1.1 christos AR_PHY_TIMING_CTRL4_ENABLE_PILOT_MASK);
704 1.1 christos
705 1.1 christos AR_WRITE(sc, AR_PHY_SPUR_REG,
706 1.1 christos AR_PHY_SPUR_REG_MASK_RATE_CNTL |
707 1.1 christos AR_PHY_SPUR_REG_ENABLE_MASK_PPM |
708 1.1 christos AR_PHY_SPUR_REG_MASK_RATE_SELECT |
709 1.1 christos AR_PHY_SPUR_REG_ENABLE_VIT_SPUR_RSSI |
710 1.1 christos SM(AR_PHY_SPUR_REG_SPUR_RSSI_THRESH, AR_SPUR_RSSI_THRESH));
711 1.1 christos
712 1.1 christos spur_delta_phase = (spur * 524288) / 100;
713 1.1 christos if (IEEE80211_IS_CHAN_2GHZ(c))
714 1.1 christos spur_freq_sd = (spur * 2048) / 440;
715 1.1 christos else
716 1.1 christos spur_freq_sd = (spur * 2048) / 400;
717 1.1 christos
718 1.1 christos AR_WRITE(sc, AR_PHY_TIMING11,
719 1.1 christos AR_PHY_TIMING11_USE_SPUR_IN_AGC |
720 1.1 christos SM(AR_PHY_TIMING11_SPUR_FREQ_SD, spur_freq_sd) |
721 1.1 christos SM(AR_PHY_TIMING11_SPUR_DELTA_PHASE, spur_delta_phase));
722 1.1 christos
723 1.1 christos bin = spur * 32;
724 1.1 christos ar5008_set_viterbi_mask(sc, bin);
725 1.1 christos }
726 1.1 christos
727 1.1 christos Static uint8_t
728 1.1 christos ar5416_reverse_bits(uint8_t v, int nbits)
729 1.1 christos {
730 1.1 christos KASSERT(nbits <= 8);
731 1.1 christos v = ((v >> 1) & 0x55) | ((v & 0x55) << 1);
732 1.1 christos v = ((v >> 2) & 0x33) | ((v & 0x33) << 2);
733 1.1 christos v = ((v >> 4) & 0x0f) | ((v & 0x0f) << 4);
734 1.1 christos return v >> (8 - nbits);
735 1.1 christos }
736 1.1 christos
737 1.1 christos PUBLIC uint8_t
738 1.1 christos ar5416_get_rf_rev(struct athn_softc *sc)
739 1.1 christos {
740 1.1 christos uint8_t rev, reg;
741 1.1 christos int i;
742 1.1 christos
743 1.1 christos /* Allow access to analog chips. */
744 1.1 christos AR_WRITE(sc, AR_PHY(0), 0x00000007);
745 1.1 christos
746 1.1 christos AR_WRITE(sc, AR_PHY(0x36), 0x00007058);
747 1.1 christos for (i = 0; i < 8; i++)
748 1.1 christos AR_WRITE(sc, AR_PHY(0x20), 0x00010000);
749 1.1 christos reg = (AR_READ(sc, AR_PHY(256)) >> 24) & 0xff;
750 1.1 christos reg = (reg & 0xf0) >> 4 | (reg & 0x0f) << 4;
751 1.1 christos
752 1.1 christos rev = ar5416_reverse_bits(reg, 8);
753 1.1 christos if ((rev & AR_RADIO_SREV_MAJOR) == 0)
754 1.1 christos rev = AR_RAD5133_SREV_MAJOR;
755 1.1 christos return rev;
756 1.1 christos }
757 1.1 christos
758 1.1 christos /*
759 1.1 christos * Replace bits "off" to "off+nbits-1" in column "col" with the specified
760 1.1 christos * value.
761 1.1 christos */
762 1.1 christos Static void
763 1.1 christos ar5416_rw_rfbits(uint32_t *buf, int col, int off, uint32_t val, int nbits)
764 1.1 christos {
765 1.1 christos int idx, bit;
766 1.1 christos
767 1.1 christos KASSERT(off >= 1 && col < 4 && nbits <= 32);
768 1.1 christos
769 1.1 christos off--; /* Starts at 1. */
770 1.1 christos while (nbits-- > 0) {
771 1.1 christos idx = off / 8;
772 1.1 christos bit = off % 8;
773 1.1 christos buf[idx] &= ~(1 << (bit + col * 8));
774 1.1 christos buf[idx] |= ((val >> nbits) & 1) << (bit + col * 8);
775 1.1 christos off++;
776 1.1 christos }
777 1.1 christos }
778 1.1 christos
779 1.1 christos /*
780 1.1 christos * Overwrite db and ob based on ROM settings.
781 1.1 christos */
782 1.1 christos Static void
783 1.1 christos ar5416_rw_bank6tpc(struct athn_softc *sc, struct ieee80211_channel *c,
784 1.1 christos uint32_t *rwbank6tpc)
785 1.1 christos {
786 1.1 christos const struct ar5416_eeprom *eep = sc->sc_eep;
787 1.1 christos const struct ar5416_modal_eep_header *modal;
788 1.1 christos
789 1.1 christos if (IEEE80211_IS_CHAN_5GHZ(c)) {
790 1.1 christos modal = &eep->modalHeader[0];
791 1.1 christos /* 5GHz db in column 0, bits [200-202]. */
792 1.1 christos ar5416_rw_rfbits(rwbank6tpc, 0, 200, modal->db, 3);
793 1.1 christos /* 5GHz ob in column 0, bits [203-205]. */
794 1.1 christos ar5416_rw_rfbits(rwbank6tpc, 0, 203, modal->ob, 3);
795 1.1 christos }
796 1.1 christos else {
797 1.1 christos modal = &eep->modalHeader[1];
798 1.1 christos /* 2GHz db in column 0, bits [194-196]. */
799 1.1 christos ar5416_rw_rfbits(rwbank6tpc, 0, 194, modal->db, 3);
800 1.1 christos /* 2GHz ob in column 0, bits [197-199]. */
801 1.1 christos ar5416_rw_rfbits(rwbank6tpc, 0, 197, modal->ob, 3);
802 1.1 christos }
803 1.1 christos }
804 1.1 christos
805 1.1 christos /*
806 1.1 christos * Program analog RF.
807 1.1 christos */
808 1.1 christos PUBLIC void
809 1.1 christos ar5416_rf_reset(struct athn_softc *sc, struct ieee80211_channel *c)
810 1.1 christos {
811 1.1 christos const uint32_t *bank6tpc;
812 1.1 christos int i;
813 1.1 christos
814 1.1 christos /* Bank 0. */
815 1.1 christos AR_WRITE(sc, 0x98b0, 0x1e5795e5);
816 1.1 christos AR_WRITE(sc, 0x98e0, 0x02008020);
817 1.1 christos
818 1.1 christos /* Bank 1. */
819 1.1 christos AR_WRITE(sc, 0x98b0, 0x02108421);
820 1.1 christos AR_WRITE(sc, 0x98ec, 0x00000008);
821 1.1 christos
822 1.1 christos /* Bank 2. */
823 1.1 christos AR_WRITE(sc, 0x98b0, 0x0e73ff17);
824 1.1 christos AR_WRITE(sc, 0x98e0, 0x00000420);
825 1.1 christos
826 1.1 christos /* Bank 3. */
827 1.1 christos if (IEEE80211_IS_CHAN_5GHZ(c))
828 1.1 christos AR_WRITE(sc, 0x98f0, 0x01400018);
829 1.1 christos else
830 1.1 christos AR_WRITE(sc, 0x98f0, 0x01c00018);
831 1.1 christos
832 1.1 christos /* Select the Bank 6 TPC values to use. */
833 1.1 christos if (AR_SREV_9160_10_OR_LATER(sc))
834 1.1 christos bank6tpc = ar9160_bank6tpc_vals;
835 1.1 christos else
836 1.1 christos bank6tpc = ar5416_bank6tpc_vals;
837 1.1 christos if (sc->sc_eep_rev >= AR_EEP_MINOR_VER_2) {
838 1.1 christos uint32_t *rwbank6tpc = sc->sc_rwbuf;
839 1.1 christos
840 1.1 christos /* Copy values from .rodata to writable buffer. */
841 1.1 christos memcpy(rwbank6tpc, bank6tpc, 32 * sizeof(uint32_t));
842 1.1 christos ar5416_rw_bank6tpc(sc, c, rwbank6tpc);
843 1.1 christos bank6tpc = rwbank6tpc;
844 1.1 christos }
845 1.1 christos /* Bank 6 TPC. */
846 1.1 christos for (i = 0; i < 32; i++)
847 1.1 christos AR_WRITE(sc, 0x989c, bank6tpc[i]);
848 1.1 christos if (IEEE80211_IS_CHAN_5GHZ(c))
849 1.1 christos AR_WRITE(sc, 0x98d0, 0x0000000f);
850 1.1 christos else
851 1.1 christos AR_WRITE(sc, 0x98d0, 0x0010000f);
852 1.1 christos
853 1.1 christos /* Bank 7. */
854 1.1 christos AR_WRITE(sc, 0x989c, 0x00000500);
855 1.1 christos AR_WRITE(sc, 0x989c, 0x00000800);
856 1.1 christos AR_WRITE(sc, 0x98cc, 0x0000000e);
857 1.1 christos }
858 1.1 christos
859 1.1 christos PUBLIC void
860 1.1 christos ar5416_reset_bb_gain(struct athn_softc *sc, struct ieee80211_channel *c)
861 1.1 christos {
862 1.1 christos const uint32_t *pvals;
863 1.1 christos int i;
864 1.1 christos
865 1.1 christos if (IEEE80211_IS_CHAN_2GHZ(c))
866 1.1 christos pvals = ar5416_bb_rfgain_vals_2g;
867 1.1 christos else
868 1.1 christos pvals = ar5416_bb_rfgain_vals_5g;
869 1.1 christos for (i = 0; i < 64; i++)
870 1.1 christos AR_WRITE(sc, AR_PHY_BB_RFGAIN(i), pvals[i]);
871 1.1 christos }
872 1.1 christos
873 1.1 christos /*
874 1.1 christos * Fix orientation sensitivity issue on AR5416/2GHz by increasing
875 1.1 christos * rf_pwd_icsyndiv.
876 1.1 christos */
877 1.1 christos Static void
878 1.1 christos ar5416_force_bias(struct athn_softc *sc, struct ieee80211_channel *c)
879 1.1 christos {
880 1.1 christos uint32_t *rwbank6 = sc->sc_rwbuf;
881 1.1 christos uint8_t bias;
882 1.1 christos int i;
883 1.1 christos
884 1.1 christos KASSERT(IEEE80211_IS_CHAN_2GHZ(c));
885 1.1 christos
886 1.1 christos /* Copy values from .rodata to writable buffer. */
887 1.1 christos memcpy(rwbank6, ar5416_bank6_vals, sizeof(ar5416_bank6_vals));
888 1.1 christos
889 1.1 christos if (c->ic_freq < 2412)
890 1.1 christos bias = 0;
891 1.1 christos else if (c->ic_freq < 2422)
892 1.1 christos bias = 1;
893 1.1 christos else
894 1.1 christos bias = 2;
895 1.1 christos ar5416_reverse_bits(bias, 3);
896 1.1 christos
897 1.1 christos /* Overwrite "rf_pwd_icsyndiv" (column 3, bits [181-183].) */
898 1.1 christos ar5416_rw_rfbits(rwbank6, 3, 181, bias, 3);
899 1.1 christos
900 1.1 christos /* Write Bank 6. */
901 1.1 christos for (i = 0; i < 32; i++)
902 1.1 christos AR_WRITE(sc, 0x989c, rwbank6[i]);
903 1.1 christos AR_WRITE(sc, 0x98d0, 0x0010000f);
904 1.1 christos }
905 1.1 christos
906 1.1 christos /*
907 1.1 christos * Overwrite XPA bias level based on ROM setting.
908 1.1 christos */
909 1.1 christos Static void
910 1.1 christos ar9160_rw_addac(struct athn_softc *sc, struct ieee80211_channel *c,
911 1.1 christos uint32_t *addac)
912 1.1 christos {
913 1.1 christos struct ar5416_eeprom *eep = sc->sc_eep;
914 1.1 christos struct ar5416_modal_eep_header *modal;
915 1.1 christos uint8_t fbin, bias;
916 1.1 christos int i;
917 1.1 christos
918 1.1 christos /* XXX xpaBiasLvlFreq values have not been endian-swapped? */
919 1.1 christos
920 1.1 christos /* Get the XPA bias level to use for the specified channel. */
921 1.1 christos modal = &eep->modalHeader[IEEE80211_IS_CHAN_2GHZ(c)];
922 1.1 christos if (modal->xpaBiasLvl == 0xff) {
923 1.1 christos bias = modal->xpaBiasLvlFreq[0] >> 14;
924 1.1 christos fbin = athn_chan2fbin(c);
925 1.1 christos for (i = 1; i < 3; i++) {
926 1.1 christos if (modal->xpaBiasLvlFreq[i] == 0)
927 1.1 christos break;
928 1.1 christos if ((modal->xpaBiasLvlFreq[i] & 0xff) < fbin)
929 1.1 christos break;
930 1.1 christos bias = modal->xpaBiasLvlFreq[i] >> 14;
931 1.1 christos }
932 1.1 christos }
933 1.1 christos else
934 1.1 christos bias = modal->xpaBiasLvl & 0x3;
935 1.1 christos
936 1.1 christos bias = ar5416_reverse_bits(bias, 2); /* Put in host bit-order. */
937 1.1 christos DPRINTFN(DBG_RF, sc, "bias level=%d\n", bias);
938 1.1 christos if (IEEE80211_IS_CHAN_2GHZ(c))
939 1.1 christos ar5416_rw_rfbits(addac, 0, 60, bias, 2);
940 1.1 christos else
941 1.1 christos ar5416_rw_rfbits(addac, 0, 55, bias, 2);
942 1.1 christos }
943 1.1 christos
944 1.1 christos PUBLIC void
945 1.1 christos ar5416_reset_addac(struct athn_softc *sc, struct ieee80211_channel *c)
946 1.1 christos {
947 1.1 christos const struct athn_addac *addac = sc->sc_addac;
948 1.1 christos const uint32_t *pvals;
949 1.1 christos int i;
950 1.1 christos
951 1.1 christos if (AR_SREV_9160(sc) && sc->sc_eep_rev >= AR_EEP_MINOR_VER_7) {
952 1.1 christos uint32_t *rwaddac = sc->sc_rwbuf;
953 1.1 christos
954 1.1 christos /* Copy values from .rodata to writable buffer. */
955 1.1 christos memcpy(rwaddac, addac->vals, addac->nvals * sizeof(uint32_t));
956 1.1 christos ar9160_rw_addac(sc, c, rwaddac);
957 1.1 christos pvals = rwaddac;
958 1.1 christos }
959 1.1 christos else
960 1.1 christos pvals = addac->vals;
961 1.1 christos for (i = 0; i < addac->nvals; i++)
962 1.1 christos AR_WRITE(sc, 0x989c, pvals[i]);
963 1.1 christos AR_WRITE(sc, 0x98cc, 0); /* Finalize. */
964 1.1 christos }
965