arn9380.c revision 1.3 1 1.3 matt /* $NetBSD: arn9380.c,v 1.3 2014/01/22 17:29:29 matt Exp $ */
2 1.1 christos /* $OpenBSD: ar9380.c,v 1.17 2012/10/20 09:54:20 stsp Exp $ */
3 1.1 christos
4 1.1 christos /*-
5 1.1 christos * Copyright (c) 2011 Damien Bergamini <damien.bergamini (at) free.fr>
6 1.1 christos * Copyright (c) 2010 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 AR9380 and AR9485 chipsets.
24 1.1 christos */
25 1.1 christos
26 1.1 christos #include <sys/cdefs.h>
27 1.3 matt __KERNEL_RCSID(0, "$NetBSD: arn9380.c,v 1.3 2014/01/22 17:29:29 matt 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/conf.h>
38 1.1 christos #include <sys/device.h>
39 1.1 christos
40 1.1 christos #include <sys/bus.h>
41 1.1 christos #include <sys/endian.h>
42 1.1 christos
43 1.1 christos #include <net/bpf.h>
44 1.1 christos #include <net/if.h>
45 1.1 christos #include <net/if_arp.h>
46 1.1 christos #include <net/if_dl.h>
47 1.2 christos #include <net/if_ether.h>
48 1.1 christos #include <net/if_media.h>
49 1.1 christos #include <net/if_types.h>
50 1.1 christos
51 1.1 christos #include <netinet/in.h>
52 1.1 christos #include <netinet/in_systm.h>
53 1.1 christos #include <netinet/in_var.h>
54 1.1 christos
55 1.1 christos #include <net80211/ieee80211_var.h>
56 1.1 christos #include <net80211/ieee80211_amrr.h>
57 1.1 christos #include <net80211/ieee80211_radiotap.h>
58 1.1 christos
59 1.1 christos #include <dev/ic/athnreg.h>
60 1.1 christos #include <dev/ic/athnvar.h>
61 1.1 christos
62 1.1 christos #include <dev/ic/arn9003reg.h>
63 1.1 christos #include <dev/ic/arn9380reg.h>
64 1.1 christos
65 1.1 christos #include <dev/ic/arn9003.h>
66 1.1 christos #include <dev/ic/arn9380.h>
67 1.1 christos
68 1.1 christos #define Static static
69 1.1 christos
70 1.1 christos Static void ar9380_get_correction(struct athn_softc *,
71 1.1 christos struct ieee80211_channel *, int, int *, int *);
72 1.1 christos Static void ar9380_get_paprd_masks(struct athn_softc *,
73 1.1 christos struct ieee80211_channel *, uint32_t *, uint32_t *);
74 1.1 christos Static const uint8_t *
75 1.1 christos ar9380_get_rom_template(struct athn_softc *, uint8_t);
76 1.1 christos Static void ar9380_init_from_rom(struct athn_softc *,
77 1.1 christos struct ieee80211_channel *, struct ieee80211_channel *);
78 1.1 christos Static void ar9380_set_correction(struct athn_softc *,
79 1.1 christos struct ieee80211_channel *);
80 1.1 christos Static int ar9380_set_synth(struct athn_softc *,
81 1.1 christos struct ieee80211_channel *, struct ieee80211_channel *);
82 1.1 christos Static void ar9380_set_txpower(struct athn_softc *,
83 1.1 christos struct ieee80211_channel *, struct ieee80211_channel *);
84 1.1 christos Static void ar9380_setup(struct athn_softc *);
85 1.1 christos Static void ar9380_spur_mitigate(struct athn_softc *,
86 1.1 christos struct ieee80211_channel *, struct ieee80211_channel *);
87 1.1 christos Static void ar9380_spur_mitigate_cck(struct athn_softc *,
88 1.1 christos struct ieee80211_channel *, struct ieee80211_channel *);
89 1.1 christos Static void ar9380_spur_mitigate_ofdm(struct athn_softc *,
90 1.1 christos struct ieee80211_channel *, struct ieee80211_channel *);
91 1.1 christos Static void ar9380_swap_rom(struct athn_softc *);
92 1.1 christos
93 1.1 christos Static void ar9485_init_swreg(struct athn_softc *);
94 1.1 christos #define ar9485_pmu_read AR_READ
95 1.1 christos Static int ar9485_pmu_write(struct athn_softc *, uint32_t, uint32_t);
96 1.1 christos
97 1.1 christos #ifdef notused
98 1.1 christos Static void ar9380_init_swreg(struct athn_softc *);
99 1.1 christos #endif /* notused */
100 1.1 christos
101 1.1 christos PUBLIC int
102 1.1 christos ar9380_attach(struct athn_softc *sc)
103 1.1 christos {
104 1.1 christos
105 1.1 christos sc->sc_ngpiopins = 17;
106 1.1 christos sc->sc_ops.setup = ar9380_setup;
107 1.1 christos sc->sc_ops.get_rom_template = ar9380_get_rom_template;
108 1.1 christos sc->sc_ops.swap_rom = ar9380_swap_rom;
109 1.1 christos sc->sc_ops.init_from_rom = ar9380_init_from_rom;
110 1.1 christos sc->sc_ops.set_txpower = ar9380_set_txpower;
111 1.1 christos sc->sc_ops.set_synth = ar9380_set_synth;
112 1.1 christos sc->sc_ops.spur_mitigate = ar9380_spur_mitigate;
113 1.1 christos sc->sc_ops.get_paprd_masks = ar9380_get_paprd_masks;
114 1.1 christos sc->sc_cca_min_2g = AR9380_PHY_CCA_MIN_GOOD_VAL_2GHZ;
115 1.1 christos sc->sc_cca_max_2g = AR9380_PHY_CCA_MAX_GOOD_VAL_2GHZ;
116 1.1 christos sc->sc_cca_min_5g = AR9380_PHY_CCA_MIN_GOOD_VAL_5GHZ;
117 1.1 christos sc->sc_cca_max_5g = AR9380_PHY_CCA_MAX_GOOD_VAL_5GHZ;
118 1.1 christos if (AR_SREV_9485(sc)) {
119 1.1 christos sc->sc_ini = &ar9485_1_1_ini;
120 1.1 christos sc->sc_serdes = &ar9485_1_1_serdes;
121 1.1 christos }
122 1.1 christos else {
123 1.1 christos sc->sc_ini = &ar9380_2_2_ini;
124 1.1 christos sc->sc_serdes = &ar9380_2_2_serdes;
125 1.1 christos }
126 1.1 christos
127 1.1 christos return ar9003_attach(sc);
128 1.1 christos }
129 1.1 christos
130 1.1 christos Static void
131 1.1 christos ar9380_setup(struct athn_softc *sc)
132 1.1 christos {
133 1.1 christos struct ieee80211com *ic = &sc->sc_ic;
134 1.1 christos struct ar9380_eeprom *eep = sc->sc_eep;
135 1.1 christos struct ar9380_base_eep_hdr *base = &eep->baseEepHeader;
136 1.1 christos uint8_t type;
137 1.1 christos
138 1.1 christos if (base->opFlags & AR_OPFLAGS_11A)
139 1.1 christos sc->sc_flags |= ATHN_FLAG_11A;
140 1.1 christos if (base->opFlags & AR_OPFLAGS_11G)
141 1.1 christos sc->sc_flags |= ATHN_FLAG_11G;
142 1.1 christos if (base->opFlags & AR_OPFLAGS_11N)
143 1.1 christos sc->sc_flags |= ATHN_FLAG_11N;
144 1.1 christos
145 1.1 christos IEEE80211_ADDR_COPY(ic->ic_myaddr, eep->macAddr);
146 1.1 christos sc->sc_led_pin = base->wlanLedGpio;
147 1.1 christos
148 1.1 christos /* Check if we have a hardware radio switch. */
149 1.1 christos if (base->rfSilent & AR_EEP_RFSILENT_ENABLED) {
150 1.1 christos sc->sc_flags |= ATHN_FLAG_RFSILENT;
151 1.1 christos /* Get GPIO pin used by hardware radio switch. */
152 1.1 christos sc->sc_rfsilent_pin = MS(base->rfSilent,
153 1.1 christos AR_EEP_RFSILENT_GPIO_SEL);
154 1.1 christos /* Get polarity of hardware radio switch. */
155 1.1 christos if (base->rfSilent & AR_EEP_RFSILENT_POLARITY)
156 1.1 christos sc->sc_flags |= ATHN_FLAG_RFSILENT_REVERSED;
157 1.1 christos }
158 1.1 christos
159 1.1 christos /* Set the number of HW key cache entries. */
160 1.1 christos sc->sc_kc_entries = AR_KEYTABLE_SIZE;
161 1.1 christos
162 1.1 christos sc->sc_txchainmask = MS(base->txrxMask, AR_EEP_TX_MASK);
163 1.1 christos sc->sc_rxchainmask = MS(base->txrxMask, AR_EEP_RX_MASK);
164 1.1 christos
165 1.1 christos /* Fast PLL clock is always supported. */
166 1.1 christos sc->sc_flags |= ATHN_FLAG_FAST_PLL_CLOCK;
167 1.1 christos
168 1.1 christos /* Enable PA predistortion if supported. */
169 1.1 christos if (base->featureEnable & AR_EEP_PAPRD)
170 1.1 christos sc->sc_flags |= ATHN_FLAG_PAPRD;
171 1.1 christos /*
172 1.1 christos * Some 3-stream chips may exceed the PCIe power requirements,
173 1.1 christos * requiring to reduce the number of Tx chains in some cases.
174 1.1 christos */
175 1.1 christos if ((base->miscConfiguration & AR_EEP_CHAIN_MASK_REDUCE) &&
176 1.1 christos sc->sc_txchainmask == 0x7)
177 1.1 christos sc->sc_flags |= ATHN_FLAG_3TREDUCE_CHAIN;
178 1.1 christos
179 1.1 christos /* Select initialization values based on ROM. */
180 1.1 christos type = MS(eep->baseEepHeader.txrxgain, AR_EEP_RX_GAIN);
181 1.1 christos if (!AR_SREV_9485(sc)) {
182 1.1 christos if (type == AR_EEP_RX_GAIN_WO_XLNA)
183 1.1 christos sc->sc_rx_gain = &ar9380_2_2_rx_gain_wo_xlna;
184 1.1 christos else
185 1.1 christos sc->sc_rx_gain = &ar9380_2_2_rx_gain;
186 1.1 christos }
187 1.1 christos else
188 1.1 christos sc->sc_rx_gain = &ar9485_1_1_rx_gain;
189 1.1 christos
190 1.1 christos /* Select initialization values based on ROM. */
191 1.1 christos type = MS(eep->baseEepHeader.txrxgain, AR_EEP_TX_GAIN);
192 1.1 christos if (!AR_SREV_9485(sc)) {
193 1.1 christos if (type == AR_EEP_TX_GAIN_HIGH_OB_DB)
194 1.1 christos sc->sc_tx_gain = &ar9380_2_2_tx_gain_high_ob_db;
195 1.1 christos else if (type == AR_EEP_TX_GAIN_LOW_OB_DB)
196 1.1 christos sc->sc_tx_gain = &ar9380_2_2_tx_gain_low_ob_db;
197 1.1 christos else if (type == AR_EEP_TX_GAIN_HIGH_POWER)
198 1.1 christos sc->sc_tx_gain = &ar9380_2_2_tx_gain_high_power;
199 1.1 christos else
200 1.1 christos sc->sc_tx_gain = &ar9380_2_2_tx_gain;
201 1.1 christos }
202 1.1 christos else
203 1.1 christos sc->sc_tx_gain = &ar9485_1_1_tx_gain;
204 1.1 christos }
205 1.1 christos
206 1.1 christos Static const uint8_t *
207 1.1 christos ar9380_get_rom_template(struct athn_softc *sc, uint8_t ref)
208 1.1 christos {
209 1.1 christos size_t i;
210 1.1 christos
211 1.1 christos /* Retrieve template ROM image for given reference. */
212 1.1 christos for (i = 0; i < __arraycount(ar9380_rom_templates); i++)
213 1.1 christos if (ar9380_rom_templates[i][1] == ref)
214 1.1 christos return ar9380_rom_templates[i];
215 1.1 christos return NULL;
216 1.1 christos }
217 1.1 christos
218 1.1 christos Static void
219 1.1 christos ar9380_swap_rom(struct athn_softc *sc)
220 1.1 christos {
221 1.1 christos #if BYTE_ORDER == BIG_ENDIAN
222 1.1 christos struct ar9380_eeprom *eep = sc->sc_eep;
223 1.1 christos struct ar9380_base_eep_hdr *base = &eep->baseEepHeader;
224 1.1 christos struct ar9380_modal_eep_header *modal;
225 1.1 christos int i;
226 1.1 christos
227 1.3 matt base->regDmn[0] = bswap16(base->regDmn[0]);
228 1.3 matt base->regDmn[1] = bswap16(base->regDmn[1]);
229 1.3 matt base->swreg = bswap32(base->swreg);
230 1.1 christos
231 1.1 christos modal = &eep->modalHeader2G;
232 1.3 matt modal->antCtrlCommon = bswap32(modal->antCtrlCommon);
233 1.3 matt modal->antCtrlCommon2 = bswap32(modal->antCtrlCommon2);
234 1.3 matt modal->papdRateMaskHt20 = bswap32(modal->papdRateMaskHt20);
235 1.3 matt modal->papdRateMaskHt40 = bswap32(modal->papdRateMaskHt40);
236 1.1 christos for (i = 0; i < AR9380_MAX_CHAINS; i++)
237 1.3 matt modal->antCtrlChain[i] = bswap16(modal->antCtrlChain[i]);
238 1.1 christos
239 1.1 christos modal = &eep->modalHeader5G;
240 1.3 matt modal->antCtrlCommon = bswap32(modal->antCtrlCommon);
241 1.3 matt modal->antCtrlCommon2 = bswap32(modal->antCtrlCommon2);
242 1.3 matt modal->papdRateMaskHt20 = bswap32(modal->papdRateMaskHt20);
243 1.3 matt modal->papdRateMaskHt40 = bswap32(modal->papdRateMaskHt40);
244 1.1 christos for (i = 0; i < AR9380_MAX_CHAINS; i++)
245 1.3 matt modal->antCtrlChain[i] = bswap16(modal->antCtrlChain[i]);
246 1.1 christos #endif
247 1.1 christos }
248 1.1 christos
249 1.1 christos Static void
250 1.1 christos ar9380_get_paprd_masks(struct athn_softc *sc, struct ieee80211_channel *c,
251 1.1 christos uint32_t *ht20mask, uint32_t *ht40mask)
252 1.1 christos {
253 1.1 christos const struct ar9380_eeprom *eep = sc->sc_eep;
254 1.1 christos const struct ar9380_modal_eep_header *modal;
255 1.1 christos
256 1.1 christos if (IEEE80211_IS_CHAN_2GHZ(c))
257 1.1 christos modal = &eep->modalHeader2G;
258 1.1 christos else
259 1.1 christos modal = &eep->modalHeader5G;
260 1.1 christos *ht20mask = modal->papdRateMaskHt20;
261 1.1 christos *ht40mask = modal->papdRateMaskHt40;
262 1.1 christos }
263 1.1 christos
264 1.1 christos Static int
265 1.1 christos ar9380_set_synth(struct athn_softc *sc, struct ieee80211_channel *c,
266 1.1 christos struct ieee80211_channel *extc)
267 1.1 christos {
268 1.1 christos uint32_t freq = c->ic_freq;
269 1.1 christos uint32_t chansel, phy;
270 1.1 christos
271 1.1 christos if (IEEE80211_IS_CHAN_2GHZ(c)) {
272 1.1 christos if (AR_SREV_9485(sc))
273 1.1 christos chansel = ((freq << 16) - 215) / 15;
274 1.1 christos else
275 1.1 christos chansel = (freq << 16) / 15;
276 1.1 christos AR_WRITE(sc, AR_PHY_SYNTH_CONTROL, AR9380_BMODE);
277 1.1 christos }
278 1.1 christos else {
279 1.1 christos chansel = (freq << 15) / 15;
280 1.1 christos chansel >>= 1;
281 1.1 christos AR_WRITE(sc, AR_PHY_SYNTH_CONTROL, 0);
282 1.1 christos }
283 1.1 christos
284 1.1 christos /* Enable Long Shift Select for synthesizer. */
285 1.1 christos AR_SETBITS(sc, AR_PHY_65NM_CH0_SYNTH4,
286 1.1 christos AR_PHY_SYNTH4_LONG_SHIFT_SELECT);
287 1.1 christos AR_WRITE_BARRIER(sc);
288 1.1 christos
289 1.1 christos /* Program synthesizer. */
290 1.1 christos phy = (chansel << 2) | AR9380_FRACMODE;
291 1.1 christos DPRINTFN(DBG_RF, sc, "AR_PHY_65NM_CH0_SYNTH7=0x%08x\n", phy);
292 1.1 christos AR_WRITE(sc, AR_PHY_65NM_CH0_SYNTH7, phy);
293 1.1 christos AR_WRITE_BARRIER(sc);
294 1.1 christos /* Toggle Load Synth Channel bit. */
295 1.1 christos AR_WRITE(sc, AR_PHY_65NM_CH0_SYNTH7, phy | AR9380_LOAD_SYNTH);
296 1.1 christos AR_WRITE_BARRIER(sc);
297 1.1 christos return 0;
298 1.1 christos }
299 1.1 christos
300 1.1 christos Static void
301 1.1 christos ar9380_init_from_rom(struct athn_softc *sc, struct ieee80211_channel *c,
302 1.1 christos struct ieee80211_channel *extc)
303 1.1 christos {
304 1.1 christos const struct ar9380_eeprom *eep = sc->sc_eep;
305 1.1 christos const struct ar9380_modal_eep_header *modal;
306 1.1 christos uint8_t db, margin, ant_div_ctrl;
307 1.1 christos uint32_t reg;
308 1.1 christos int i, maxchains;
309 1.1 christos
310 1.1 christos if (IEEE80211_IS_CHAN_2GHZ(c))
311 1.1 christos modal = &eep->modalHeader2G;
312 1.1 christos else
313 1.1 christos modal = &eep->modalHeader5G;
314 1.1 christos
315 1.1 christos /* Apply XPA bias level. */
316 1.1 christos if (AR_SREV_9485(sc)) {
317 1.1 christos reg = AR_READ(sc, AR9485_PHY_65NM_CH0_TOP2);
318 1.1 christos reg = RW(reg, AR9485_PHY_65NM_CH0_TOP2_XPABIASLVL,
319 1.1 christos modal->xpaBiasLvl);
320 1.1 christos AR_WRITE(sc, AR9485_PHY_65NM_CH0_TOP2, reg);
321 1.1 christos }
322 1.1 christos else {
323 1.1 christos reg = AR_READ(sc, AR_PHY_65NM_CH0_TOP);
324 1.1 christos reg = RW(reg, AR_PHY_65NM_CH0_TOP_XPABIASLVL,
325 1.1 christos modal->xpaBiasLvl & 0x3);
326 1.1 christos AR_WRITE(sc, AR_PHY_65NM_CH0_TOP, reg);
327 1.1 christos reg = AR_READ(sc, AR_PHY_65NM_CH0_THERM);
328 1.1 christos reg = RW(reg, AR_PHY_65NM_CH0_THERM_XPABIASLVL_MSB,
329 1.1 christos modal->xpaBiasLvl >> 2);
330 1.1 christos reg |= AR_PHY_65NM_CH0_THERM_XPASHORT2GND;
331 1.1 christos AR_WRITE(sc, AR_PHY_65NM_CH0_THERM, reg);
332 1.1 christos }
333 1.1 christos
334 1.1 christos /* Apply antenna control. */
335 1.1 christos reg = AR_READ(sc, AR_PHY_SWITCH_COM);
336 1.1 christos reg = RW(reg, AR_SWITCH_TABLE_COM_ALL, modal->antCtrlCommon);
337 1.1 christos AR_WRITE(sc, AR_PHY_SWITCH_COM, reg);
338 1.1 christos reg = AR_READ(sc, AR_PHY_SWITCH_COM_2);
339 1.1 christos reg = RW(reg, AR_SWITCH_TABLE_COM_2_ALL, modal->antCtrlCommon2);
340 1.1 christos AR_WRITE(sc, AR_PHY_SWITCH_COM_2, reg);
341 1.1 christos
342 1.1 christos maxchains = AR_SREV_9485(sc) ? 1 : AR9380_MAX_CHAINS;
343 1.1 christos for (i = 0; i < maxchains; i++) {
344 1.1 christos reg = AR_READ(sc, AR_PHY_SWITCH_CHAIN(i));
345 1.1 christos reg = RW(reg, AR_SWITCH_TABLE_ALL, modal->antCtrlChain[i]);
346 1.1 christos AR_WRITE(sc, AR_PHY_SWITCH_CHAIN(i), reg);
347 1.1 christos }
348 1.1 christos
349 1.1 christos if (AR_SREV_9485(sc)) {
350 1.1 christos ant_div_ctrl = eep->base_ext1.ant_div_control;
351 1.1 christos reg = AR_READ(sc, AR_PHY_MC_GAIN_CTRL);
352 1.1 christos reg = RW(reg, AR_PHY_MC_GAIN_CTRL_ANT_DIV_CTRL_ALL,
353 1.1 christos MS(ant_div_ctrl, AR_EEP_ANT_DIV_CTRL_ALL));
354 1.1 christos if (ant_div_ctrl & AR_EEP_ANT_DIV_CTRL_ANT_DIV)
355 1.1 christos reg |= AR_PHY_MC_GAIN_CTRL_ENABLE_ANT_DIV;
356 1.1 christos else
357 1.1 christos reg &= ~AR_PHY_MC_GAIN_CTRL_ENABLE_ANT_DIV;
358 1.1 christos AR_WRITE(sc, AR_PHY_MC_GAIN_CTRL, reg);
359 1.1 christos reg = AR_READ(sc, AR_PHY_CCK_DETECT);
360 1.1 christos if (ant_div_ctrl & AR_EEP_ANT_DIV_CTRL_FAST_DIV)
361 1.1 christos reg |= AR_PHY_CCK_DETECT_BB_ENABLE_ANT_FAST_DIV;
362 1.1 christos else
363 1.1 christos reg &= ~AR_PHY_CCK_DETECT_BB_ENABLE_ANT_FAST_DIV;
364 1.1 christos AR_WRITE(sc, AR_PHY_CCK_DETECT, reg);
365 1.1 christos }
366 1.1 christos
367 1.1 christos if (eep->baseEepHeader.miscConfiguration & AR_EEP_DRIVE_STRENGTH) {
368 1.1 christos /* Apply drive strength. */
369 1.1 christos reg = AR_READ(sc, AR_PHY_65NM_CH0_BIAS1);
370 1.1 christos reg = RW(reg, AR_PHY_65NM_CH0_BIAS1_0, 5);
371 1.1 christos reg = RW(reg, AR_PHY_65NM_CH0_BIAS1_1, 5);
372 1.1 christos reg = RW(reg, AR_PHY_65NM_CH0_BIAS1_2, 5);
373 1.1 christos reg = RW(reg, AR_PHY_65NM_CH0_BIAS1_3, 5);
374 1.1 christos reg = RW(reg, AR_PHY_65NM_CH0_BIAS1_4, 5);
375 1.1 christos reg = RW(reg, AR_PHY_65NM_CH0_BIAS1_5, 5);
376 1.1 christos AR_WRITE(sc, AR_PHY_65NM_CH0_BIAS1, reg);
377 1.1 christos
378 1.1 christos reg = AR_READ(sc, AR_PHY_65NM_CH0_BIAS2);
379 1.1 christos reg = RW(reg, AR_PHY_65NM_CH0_BIAS2_0, 5);
380 1.1 christos reg = RW(reg, AR_PHY_65NM_CH0_BIAS2_1, 5);
381 1.1 christos reg = RW(reg, AR_PHY_65NM_CH0_BIAS2_2, 5);
382 1.1 christos reg = RW(reg, AR_PHY_65NM_CH0_BIAS2_3, 5);
383 1.1 christos reg = RW(reg, AR_PHY_65NM_CH0_BIAS2_4, 5);
384 1.1 christos reg = RW(reg, AR_PHY_65NM_CH0_BIAS2_5, 5);
385 1.1 christos reg = RW(reg, AR_PHY_65NM_CH0_BIAS2_6, 5);
386 1.1 christos reg = RW(reg, AR_PHY_65NM_CH0_BIAS2_7, 5);
387 1.1 christos reg = RW(reg, AR_PHY_65NM_CH0_BIAS2_8, 5);
388 1.1 christos AR_WRITE(sc, AR_PHY_65NM_CH0_BIAS2, reg);
389 1.1 christos
390 1.1 christos reg = AR_READ(sc, AR_PHY_65NM_CH0_BIAS4);
391 1.1 christos reg = RW(reg, AR_PHY_65NM_CH0_BIAS4_0, 5);
392 1.1 christos reg = RW(reg, AR_PHY_65NM_CH0_BIAS4_1, 5);
393 1.1 christos reg = RW(reg, AR_PHY_65NM_CH0_BIAS4_2, 5);
394 1.1 christos AR_WRITE(sc, AR_PHY_65NM_CH0_BIAS4, reg);
395 1.1 christos }
396 1.1 christos
397 1.1 christos /* Apply attenuation settings. */
398 1.1 christos maxchains = AR_SREV_9485(sc) ? 1 : AR9380_MAX_CHAINS;
399 1.1 christos for (i = 0; i < maxchains; i++) {
400 1.1 christos if (IEEE80211_IS_CHAN_5GHZ(c) &&
401 1.1 christos eep->base_ext2.xatten1DBLow[i] != 0) {
402 1.1 christos if (c->ic_freq <= 5500) {
403 1.1 christos db = athn_interpolate(c->ic_freq,
404 1.1 christos 5180, eep->base_ext2.xatten1DBLow[i],
405 1.1 christos 5500, modal->xatten1DB[i]);
406 1.1 christos }
407 1.1 christos else {
408 1.1 christos db = athn_interpolate(c->ic_freq,
409 1.1 christos 5500, modal->xatten1DB[i],
410 1.1 christos 5785, eep->base_ext2.xatten1DBHigh[i]);
411 1.1 christos }
412 1.1 christos }
413 1.1 christos else
414 1.1 christos db = modal->xatten1DB[i];
415 1.1 christos if (IEEE80211_IS_CHAN_5GHZ(c) &&
416 1.1 christos eep->base_ext2.xatten1MarginLow[i] != 0) {
417 1.1 christos if (c->ic_freq <= 5500) {
418 1.1 christos margin = athn_interpolate(c->ic_freq,
419 1.1 christos 5180, eep->base_ext2.xatten1MarginLow[i],
420 1.1 christos 5500, modal->xatten1Margin[i]);
421 1.1 christos }
422 1.1 christos else {
423 1.1 christos margin = athn_interpolate(c->ic_freq,
424 1.1 christos 5500, modal->xatten1Margin[i],
425 1.1 christos 5785, eep->base_ext2.xatten1MarginHigh[i]);
426 1.1 christos }
427 1.1 christos }
428 1.1 christos else
429 1.1 christos margin = modal->xatten1Margin[i];
430 1.1 christos reg = AR_READ(sc, AR_PHY_EXT_ATTEN_CTL(i));
431 1.1 christos reg = RW(reg, AR_PHY_EXT_ATTEN_CTL_XATTEN1_DB, db);
432 1.1 christos reg = RW(reg, AR_PHY_EXT_ATTEN_CTL_XATTEN1_MARGIN, margin);
433 1.1 christos AR_WRITE(sc, AR_PHY_EXT_ATTEN_CTL(i), reg);
434 1.1 christos }
435 1.1 christos
436 1.1 christos /* Initialize switching regulator. */
437 1.1 christos if (AR_SREV_9485(sc))
438 1.1 christos ar9485_init_swreg(sc);
439 1.1 christos else
440 1.1 christos ar9485_init_swreg(sc);
441 1.1 christos
442 1.1 christos /* Apply tuning capabilities. */
443 1.1 christos if (AR_SREV_9485(sc) &&
444 1.1 christos (eep->baseEepHeader.featureEnable & AR_EEP_TUNING_CAPS)) {
445 1.1 christos reg = AR_READ(sc, AR9485_PHY_CH0_XTAL);
446 1.1 christos reg = RW(reg, AR9485_PHY_CH0_XTAL_CAPINDAC,
447 1.1 christos eep->baseEepHeader.params_for_tuning_caps[0]);
448 1.1 christos reg = RW(reg, AR9485_PHY_CH0_XTAL_CAPOUTDAC,
449 1.1 christos eep->baseEepHeader.params_for_tuning_caps[0]);
450 1.1 christos AR_WRITE(sc, AR9485_PHY_CH0_XTAL, reg);
451 1.1 christos }
452 1.1 christos AR_WRITE_BARRIER(sc);
453 1.1 christos }
454 1.1 christos
455 1.1 christos #ifdef notused
456 1.1 christos Static void
457 1.1 christos ar9380_init_swreg(struct athn_softc *sc)
458 1.1 christos {
459 1.1 christos const struct ar9380_eeprom *eep = sc->sc_eep;
460 1.1 christos
461 1.1 christos if (eep->baseEepHeader.featureEnable & AR_EEP_INTERNAL_REGULATOR) {
462 1.1 christos /* Internal regulator is ON. */
463 1.1 christos AR_CLRBITS(sc, AR_RTC_REG_CONTROL1,
464 1.1 christos AR_RTC_REG_CONTROL1_SWREG_PROGRAM);
465 1.1 christos AR_WRITE(sc, AR_RTC_REG_CONTROL0, eep->baseEepHeader.swreg);
466 1.1 christos AR_SETBITS(sc, AR_RTC_REG_CONTROL1,
467 1.1 christos AR_RTC_REG_CONTROL1_SWREG_PROGRAM);
468 1.1 christos }
469 1.1 christos else
470 1.1 christos AR_SETBITS(sc, AR_RTC_SLEEP_CLK, AR_RTC_FORCE_SWREG_PRD);
471 1.1 christos AR_WRITE_BARRIER(sc);
472 1.1 christos }
473 1.1 christos #endif /* notused */
474 1.1 christos
475 1.1 christos Static int
476 1.1 christos ar9485_pmu_write(struct athn_softc *sc, uint32_t addr, uint32_t val)
477 1.1 christos {
478 1.1 christos int ntries;
479 1.1 christos
480 1.1 christos AR_WRITE(sc, addr, val);
481 1.1 christos /* Wait for write to complete. */
482 1.1 christos for (ntries = 0; ntries < 100; ntries++) {
483 1.1 christos if (AR_READ(sc, addr) == val)
484 1.1 christos return 0;
485 1.1 christos AR_WRITE(sc, addr, val); /* Insist. */
486 1.1 christos AR_WRITE_BARRIER(sc);
487 1.1 christos DELAY(10);
488 1.1 christos }
489 1.1 christos return ETIMEDOUT;
490 1.1 christos }
491 1.1 christos
492 1.1 christos Static void
493 1.1 christos ar9485_init_swreg(struct athn_softc *sc)
494 1.1 christos {
495 1.1 christos const struct ar9380_eeprom *eep = sc->sc_eep;
496 1.1 christos uint32_t reg;
497 1.1 christos
498 1.1 christos ar9485_pmu_write(sc, AR_PHY_PMU2,
499 1.1 christos ar9485_pmu_read(sc, AR_PHY_PMU2) & ~AR_PHY_PMU2_PGM);
500 1.1 christos
501 1.1 christos if (eep->baseEepHeader.featureEnable & AR_EEP_INTERNAL_REGULATOR) {
502 1.1 christos ar9485_pmu_write(sc, AR_PHY_PMU1, 0x131dc17a);
503 1.1 christos
504 1.1 christos reg = ar9485_pmu_read(sc, AR_PHY_PMU2);
505 1.1 christos reg = (reg & ~0xffc00000) | 0x10000000;
506 1.1 christos ar9485_pmu_write(sc, AR_PHY_PMU2, reg);
507 1.1 christos }
508 1.1 christos else {
509 1.1 christos ar9485_pmu_write(sc, AR_PHY_PMU1,
510 1.1 christos ar9485_pmu_read(sc, AR_PHY_PMU1) | AR_PHY_PMU1_PWD);
511 1.1 christos }
512 1.1 christos
513 1.1 christos ar9485_pmu_write(sc, AR_PHY_PMU2,
514 1.1 christos ar9485_pmu_read(sc, AR_PHY_PMU2) | AR_PHY_PMU2_PGM);
515 1.1 christos }
516 1.1 christos
517 1.1 christos /*
518 1.1 christos * NB: It is safe to call this function for 5GHz channels.
519 1.1 christos */
520 1.1 christos Static void
521 1.1 christos ar9380_spur_mitigate_cck(struct athn_softc *sc, struct ieee80211_channel *c,
522 1.1 christos struct ieee80211_channel *extc)
523 1.1 christos {
524 1.1 christos static const int16_t freqs[] = { 2420, 2440, 2464, 2480 };
525 1.1 christos size_t i;
526 1.1 christos int spur, freq;
527 1.1 christos uint32_t reg;
528 1.1 christos
529 1.1 christos for (i = 0; i < __arraycount(freqs); i++) {
530 1.1 christos spur = freqs[i] - c->ic_freq;
531 1.1 christos if (abs(spur) < 10) /* +/- 10MHz range. */
532 1.1 christos break;
533 1.1 christos }
534 1.1 christos if (i == __arraycount(freqs)) {
535 1.1 christos /* Disable CCK spur mitigation. */
536 1.1 christos reg = AR_READ(sc, AR_PHY_AGC_CONTROL);
537 1.1 christos reg = RW(reg, AR_PHY_AGC_CONTROL_YCOK_MAX, 0x5);
538 1.1 christos AR_WRITE(sc, AR_PHY_AGC_CONTROL, reg);
539 1.1 christos reg = AR_READ(sc, AR_PHY_CCK_SPUR_MIT);
540 1.1 christos reg = RW(reg, AR_PHY_CCK_SPUR_MIT_CCK_SPUR_FREQ, 0);
541 1.1 christos reg &= ~AR_PHY_CCK_SPUR_MIT_USE_CCK_SPUR_MIT;
542 1.1 christos AR_WRITE(sc, AR_PHY_CCK_SPUR_MIT, reg);
543 1.1 christos AR_WRITE_BARRIER(sc);
544 1.1 christos return;
545 1.1 christos }
546 1.1 christos freq = (spur * 524288) / 11;
547 1.1 christos
548 1.1 christos reg = AR_READ(sc, AR_PHY_AGC_CONTROL);
549 1.1 christos reg = RW(reg, AR_PHY_AGC_CONTROL_YCOK_MAX, 0x7);
550 1.1 christos AR_WRITE(sc, AR_PHY_AGC_CONTROL, reg);
551 1.1 christos
552 1.1 christos reg = AR_READ(sc, AR_PHY_CCK_SPUR_MIT);
553 1.1 christos reg = RW(reg, AR_PHY_CCK_SPUR_MIT_CCK_SPUR_FREQ, freq);
554 1.1 christos reg = RW(reg, AR_PHY_CCK_SPUR_MIT_SPUR_RSSI_THR, 0x7f);
555 1.1 christos reg = RW(reg, AR_PHY_CCK_SPUR_MIT_SPUR_FILTER_TYPE, 0x2);
556 1.1 christos reg |= AR_PHY_CCK_SPUR_MIT_USE_CCK_SPUR_MIT;
557 1.1 christos AR_WRITE(sc, AR_PHY_CCK_SPUR_MIT, reg);
558 1.1 christos AR_WRITE_BARRIER(sc);
559 1.1 christos }
560 1.1 christos
561 1.1 christos Static void
562 1.1 christos ar9380_spur_mitigate_ofdm(struct athn_softc *sc, struct ieee80211_channel *c,
563 1.1 christos struct ieee80211_channel *extc)
564 1.1 christos {
565 1.1 christos const struct ar9380_eeprom *eep = sc->sc_eep;
566 1.1 christos const uint8_t *spurchans;
567 1.1 christos uint32_t reg;
568 1.1 christos int idx, spur_delta_phase, spur_off, range, i;
569 1.1 christos int freq, spur, spur_freq_sd, spur_subchannel_sd;
570 1.1 christos
571 1.1 christos if (IEEE80211_IS_CHAN_2GHZ(c))
572 1.1 christos spurchans = eep->modalHeader2G.spurChans;
573 1.1 christos else
574 1.1 christos spurchans = eep->modalHeader5G.spurChans;
575 1.1 christos if (spurchans[0] == 0)
576 1.1 christos return;
577 1.1 christos
578 1.1 christos /* Disable OFDM spur mitigation. */
579 1.1 christos AR_CLRBITS(sc, AR_PHY_TIMING4, AR_PHY_TIMING4_ENABLE_SPUR_FILTER);
580 1.1 christos
581 1.1 christos reg = AR_READ(sc, AR_PHY_TIMING11);
582 1.1 christos reg = RW(reg, AR_PHY_TIMING11_SPUR_FREQ_SD, 0);
583 1.1 christos reg = RW(reg, AR_PHY_TIMING11_SPUR_DELTA_PHASE, 0);
584 1.1 christos reg &= ~AR_PHY_TIMING11_USE_SPUR_FILTER_IN_AGC;
585 1.1 christos reg &= ~AR_PHY_TIMING11_USE_SPUR_FILTER_IN_SELFCOR;
586 1.1 christos AR_WRITE(sc, AR_PHY_TIMING11, reg);
587 1.1 christos
588 1.1 christos AR_CLRBITS(sc, AR_PHY_SFCORR_EXT,
589 1.1 christos AR_PHY_SFCORR_EXT_SPUR_SUBCHANNEL_SD);
590 1.1 christos
591 1.1 christos AR_CLRBITS(sc, AR_PHY_TIMING4, AR_PHY_TIMING4_ENABLE_SPUR_RSSI);
592 1.1 christos
593 1.1 christos reg = AR_READ(sc, AR_PHY_SPUR_REG);
594 1.1 christos reg = RW(reg, AR_PHY_SPUR_REG_MASK_RATE_CNTL, 0);
595 1.1 christos reg &= ~AR_PHY_SPUR_REG_EN_VIT_SPUR_RSSI;
596 1.1 christos reg &= ~AR_PHY_SPUR_REG_ENABLE_NF_RSSI_SPUR_MIT;
597 1.1 christos reg &= ~AR_PHY_SPUR_REG_ENABLE_MASK_PPM;
598 1.1 christos AR_WRITE(sc, AR_PHY_SPUR_REG, reg);
599 1.1 christos AR_WRITE_BARRIER(sc);
600 1.1 christos
601 1.1 christos freq = c->ic_freq;
602 1.1 christos #ifndef IEEE80211_NO_HT
603 1.1 christos if (extc != NULL) {
604 1.1 christos range = 19; /* +/- 19MHz range. */
605 1.1 christos if (AR_READ(sc, AR_PHY_GEN_CTRL) & AR_PHY_GC_DYN2040_PRI_CH)
606 1.1 christos freq += 10;
607 1.1 christos else
608 1.1 christos freq -= 10;
609 1.1 christos }
610 1.1 christos else
611 1.1 christos #endif
612 1.1 christos range = 10; /* +/- 10MHz range. */
613 1.1 christos for (i = 0; i < AR9380_EEPROM_MODAL_SPURS; i++) {
614 1.1 christos spur = spurchans[i];
615 1.1 christos if (spur == 0)
616 1.1 christos return;
617 1.1 christos /* Convert to frequency. */
618 1.1 christos if (IEEE80211_IS_CHAN_2GHZ(c))
619 1.1 christos spur = 2300 + spur;
620 1.1 christos else
621 1.1 christos spur = 4900 + (spur * 5);
622 1.1 christos spur -= freq;
623 1.1 christos if (abs(spur) < range)
624 1.1 christos break;
625 1.1 christos }
626 1.1 christos if (i == AR9380_EEPROM_MODAL_SPURS)
627 1.1 christos return;
628 1.1 christos
629 1.1 christos /* Enable OFDM spur mitigation. */
630 1.1 christos #ifndef IEEE80211_NO_HT
631 1.1 christos if (extc != NULL) {
632 1.1 christos spur_delta_phase = (spur * 131072) / 5;
633 1.1 christos reg = AR_READ(sc, AR_PHY_GEN_CTRL);
634 1.1 christos if (spur < 0) {
635 1.1 christos spur_subchannel_sd =
636 1.1 christos (reg & AR_PHY_GC_DYN2040_PRI_CH) == 0;
637 1.1 christos spur_off = spur + 10;
638 1.1 christos }
639 1.1 christos else {
640 1.1 christos spur_subchannel_sd =
641 1.1 christos (reg & AR_PHY_GC_DYN2040_PRI_CH) != 0;
642 1.1 christos spur_off = spur - 10;
643 1.1 christos }
644 1.1 christos }
645 1.1 christos else
646 1.1 christos #endif
647 1.1 christos {
648 1.1 christos spur_delta_phase = (spur * 262144) / 5;
649 1.1 christos spur_subchannel_sd = 0;
650 1.1 christos spur_off = spur;
651 1.1 christos }
652 1.1 christos spur_freq_sd = (spur_off * 512) / 11;
653 1.1 christos
654 1.1 christos AR_SETBITS(sc, AR_PHY_TIMING4, AR_PHY_TIMING4_ENABLE_SPUR_FILTER);
655 1.1 christos
656 1.1 christos reg = AR_READ(sc, AR_PHY_TIMING11);
657 1.1 christos reg = RW(reg, AR_PHY_TIMING11_SPUR_FREQ_SD, spur_freq_sd);
658 1.1 christos reg = RW(reg, AR_PHY_TIMING11_SPUR_DELTA_PHASE, spur_delta_phase);
659 1.1 christos reg |= AR_PHY_TIMING11_USE_SPUR_FILTER_IN_AGC;
660 1.1 christos reg |= AR_PHY_TIMING11_USE_SPUR_FILTER_IN_SELFCOR;
661 1.1 christos AR_WRITE(sc, AR_PHY_TIMING11, reg);
662 1.1 christos
663 1.1 christos reg = AR_READ(sc, AR_PHY_SFCORR_EXT);
664 1.1 christos if (spur_subchannel_sd)
665 1.1 christos reg |= AR_PHY_SFCORR_EXT_SPUR_SUBCHANNEL_SD;
666 1.1 christos else
667 1.1 christos reg &= ~AR_PHY_SFCORR_EXT_SPUR_SUBCHANNEL_SD;
668 1.1 christos AR_WRITE(sc, AR_PHY_SFCORR_EXT, reg);
669 1.1 christos
670 1.1 christos AR_SETBITS(sc, AR_PHY_TIMING4, AR_PHY_TIMING4_ENABLE_SPUR_RSSI);
671 1.1 christos
672 1.1 christos reg = AR_READ(sc, AR_PHY_SPUR_REG);
673 1.1 christos reg = RW(reg, AR_PHY_SPUR_REG_MASK_RATE_CNTL, 0xff);
674 1.1 christos reg = RW(reg, AR_PHY_SPUR_REG_SPUR_RSSI_THRESH, 34);
675 1.1 christos reg |= AR_PHY_SPUR_REG_EN_VIT_SPUR_RSSI;
676 1.1 christos if (AR_READ(sc, AR_PHY_MODE) & AR_PHY_MODE_DYNAMIC)
677 1.1 christos reg |= AR_PHY_SPUR_REG_ENABLE_NF_RSSI_SPUR_MIT;
678 1.1 christos reg |= AR_PHY_SPUR_REG_ENABLE_MASK_PPM;
679 1.1 christos AR_WRITE(sc, AR_PHY_SPUR_REG, reg);
680 1.1 christos
681 1.1 christos idx = (spur * 16) / 5;
682 1.1 christos if (idx < 0)
683 1.1 christos idx--;
684 1.1 christos
685 1.1 christos /* Write pilot mask. */
686 1.1 christos AR_SETBITS(sc, AR_PHY_TIMING4,
687 1.1 christos AR_PHY_TIMING4_ENABLE_PILOT_MASK |
688 1.1 christos AR_PHY_TIMING4_ENABLE_CHAN_MASK);
689 1.1 christos
690 1.1 christos reg = AR_READ(sc, AR_PHY_PILOT_SPUR_MASK);
691 1.1 christos reg = RW(reg, AR_PHY_PILOT_SPUR_MASK_CF_PILOT_MASK_IDX_A, idx);
692 1.1 christos reg = RW(reg, AR_PHY_PILOT_SPUR_MASK_CF_PILOT_MASK_A, 0x0c);
693 1.1 christos AR_WRITE(sc, AR_PHY_PILOT_SPUR_MASK, reg);
694 1.1 christos
695 1.1 christos reg = AR_READ(sc, AR_PHY_SPUR_MASK_A);
696 1.1 christos reg = RW(reg, AR_PHY_SPUR_MASK_A_CF_PUNC_MASK_IDX_A, idx);
697 1.1 christos reg = RW(reg, AR_PHY_SPUR_MASK_A_CF_PUNC_MASK_A, 0xa0);
698 1.1 christos AR_WRITE(sc, AR_PHY_SPUR_MASK_A, reg);
699 1.1 christos
700 1.1 christos reg = AR_READ(sc, AR_PHY_CHAN_SPUR_MASK);
701 1.1 christos reg = RW(reg, AR_PHY_CHAN_SPUR_MASK_CF_CHAN_MASK_IDX_A, idx);
702 1.1 christos reg = RW(reg, AR_PHY_CHAN_SPUR_MASK_CF_CHAN_MASK_A, 0x0c);
703 1.1 christos AR_WRITE(sc, AR_PHY_CHAN_SPUR_MASK, reg);
704 1.1 christos AR_WRITE_BARRIER(sc);
705 1.1 christos }
706 1.1 christos
707 1.1 christos Static void
708 1.1 christos ar9380_spur_mitigate(struct athn_softc *sc, struct ieee80211_channel *c,
709 1.1 christos struct ieee80211_channel *extc)
710 1.1 christos {
711 1.1 christos
712 1.1 christos /* NB: We call spur_mitigate_cck for 5GHz too, just to disable it. */
713 1.1 christos ar9380_spur_mitigate_cck(sc, c, extc);
714 1.1 christos ar9380_spur_mitigate_ofdm(sc, c, extc);
715 1.1 christos }
716 1.1 christos
717 1.1 christos Static void
718 1.1 christos ar9380_set_txpower(struct athn_softc *sc, struct ieee80211_channel *c,
719 1.1 christos struct ieee80211_channel *extc)
720 1.1 christos {
721 1.1 christos const struct ar9380_eeprom *eep = sc->sc_eep;
722 1.1 christos uint8_t tpow_cck[4], tpow_ofdm[4];
723 1.1 christos uint8_t tpow_ht20[14], tpow_ht40[14];
724 1.1 christos int16_t power[ATHN_POWER_COUNT];
725 1.1 christos
726 1.1 christos if (IEEE80211_IS_CHAN_2GHZ(c)) {
727 1.1 christos /* Get CCK target powers. */
728 1.1 christos ar9003_get_lg_tpow(sc, c, AR_CTL_11B,
729 1.1 christos eep->calTargetFbinCck, eep->calTargetPowerCck,
730 1.1 christos AR9380_NUM_2G_CCK_TARGET_POWERS, tpow_cck);
731 1.1 christos
732 1.1 christos /* Get OFDM target powers. */
733 1.1 christos ar9003_get_lg_tpow(sc, c, AR_CTL_11G,
734 1.1 christos eep->calTargetFbin2G, eep->calTargetPower2G,
735 1.1 christos AR9380_NUM_2G_20_TARGET_POWERS, tpow_ofdm);
736 1.1 christos
737 1.1 christos /* Get HT-20 target powers. */
738 1.1 christos ar9003_get_ht_tpow(sc, c, AR_CTL_2GHT20,
739 1.1 christos eep->calTargetFbin2GHT20, eep->calTargetPower2GHT20,
740 1.1 christos AR9380_NUM_2G_20_TARGET_POWERS, tpow_ht20);
741 1.1 christos
742 1.1 christos if (extc != NULL) {
743 1.1 christos /* Get HT-40 target powers. */
744 1.1 christos ar9003_get_ht_tpow(sc, c, AR_CTL_2GHT40,
745 1.1 christos eep->calTargetFbin2GHT40,
746 1.1 christos eep->calTargetPower2GHT40,
747 1.1 christos AR9380_NUM_2G_40_TARGET_POWERS, tpow_ht40);
748 1.1 christos }
749 1.1 christos }
750 1.1 christos else {
751 1.1 christos /* Get OFDM target powers. */
752 1.1 christos ar9003_get_lg_tpow(sc, c, AR_CTL_11A,
753 1.1 christos eep->calTargetFbin5G, eep->calTargetPower5G,
754 1.1 christos AR9380_NUM_5G_20_TARGET_POWERS, tpow_ofdm);
755 1.1 christos
756 1.1 christos /* Get HT-20 target powers. */
757 1.1 christos ar9003_get_ht_tpow(sc, c, AR_CTL_5GHT20,
758 1.1 christos eep->calTargetFbin5GHT20, eep->calTargetPower5GHT20,
759 1.1 christos AR9380_NUM_5G_20_TARGET_POWERS, tpow_ht20);
760 1.1 christos
761 1.1 christos if (extc != NULL) {
762 1.1 christos /* Get HT-40 target powers. */
763 1.1 christos ar9003_get_ht_tpow(sc, c, AR_CTL_5GHT40,
764 1.1 christos eep->calTargetFbin5GHT40,
765 1.1 christos eep->calTargetPower5GHT40,
766 1.1 christos AR9380_NUM_5G_40_TARGET_POWERS, tpow_ht40);
767 1.1 christos }
768 1.1 christos }
769 1.1 christos
770 1.1 christos memset(power, 0, sizeof(power));
771 1.1 christos /* Shuffle target powers accross transmit rates. */
772 1.1 christos power[ATHN_POWER_OFDM6 ] =
773 1.1 christos power[ATHN_POWER_OFDM9 ] =
774 1.1 christos power[ATHN_POWER_OFDM12] =
775 1.1 christos power[ATHN_POWER_OFDM18] =
776 1.1 christos power[ATHN_POWER_OFDM24] = tpow_ofdm[0];
777 1.1 christos power[ATHN_POWER_OFDM36] = tpow_ofdm[1];
778 1.1 christos power[ATHN_POWER_OFDM48] = tpow_ofdm[2];
779 1.1 christos power[ATHN_POWER_OFDM54] = tpow_ofdm[3];
780 1.1 christos if (IEEE80211_IS_CHAN_2GHZ(c)) {
781 1.1 christos power[ATHN_POWER_CCK1_LP ] =
782 1.1 christos power[ATHN_POWER_CCK2_LP ] =
783 1.1 christos power[ATHN_POWER_CCK2_SP ] =
784 1.1 christos power[ATHN_POWER_CCK55_LP] = tpow_cck[0];
785 1.1 christos power[ATHN_POWER_CCK55_SP] = tpow_cck[1];
786 1.1 christos power[ATHN_POWER_CCK11_LP] = tpow_cck[2];
787 1.1 christos power[ATHN_POWER_CCK11_SP] = tpow_cck[3];
788 1.1 christos }
789 1.1 christos /* Next entry covers MCS0, MCS8 and MCS16. */
790 1.1 christos power[ATHN_POWER_HT20( 0)] = tpow_ht20[ 0];
791 1.1 christos /* Next entry covers MCS1-3, MCS9-11 and MCS17-19. */
792 1.1 christos power[ATHN_POWER_HT20( 1)] = tpow_ht20[ 1];
793 1.1 christos power[ATHN_POWER_HT20( 4)] = tpow_ht20[ 2];
794 1.1 christos power[ATHN_POWER_HT20( 5)] = tpow_ht20[ 3];
795 1.1 christos power[ATHN_POWER_HT20( 6)] = tpow_ht20[ 4];
796 1.1 christos power[ATHN_POWER_HT20( 7)] = tpow_ht20[ 5];
797 1.1 christos power[ATHN_POWER_HT20(12)] = tpow_ht20[ 6];
798 1.1 christos power[ATHN_POWER_HT20(13)] = tpow_ht20[ 7];
799 1.1 christos power[ATHN_POWER_HT20(14)] = tpow_ht20[ 8];
800 1.1 christos power[ATHN_POWER_HT20(15)] = tpow_ht20[ 9];
801 1.1 christos power[ATHN_POWER_HT20(20)] = tpow_ht20[10];
802 1.1 christos power[ATHN_POWER_HT20(21)] = tpow_ht20[11];
803 1.1 christos power[ATHN_POWER_HT20(22)] = tpow_ht20[12];
804 1.1 christos power[ATHN_POWER_HT20(23)] = tpow_ht20[13];
805 1.1 christos if (extc != NULL) {
806 1.1 christos /* Next entry covers MCS0, MCS8 and MCS16. */
807 1.1 christos power[ATHN_POWER_HT40( 0)] = tpow_ht40[ 0];
808 1.1 christos /* Next entry covers MCS1-3, MCS9-11 and MCS17-19. */
809 1.1 christos power[ATHN_POWER_HT40( 1)] = tpow_ht40[ 1];
810 1.1 christos power[ATHN_POWER_HT40( 4)] = tpow_ht40[ 2];
811 1.1 christos power[ATHN_POWER_HT40( 5)] = tpow_ht40[ 3];
812 1.1 christos power[ATHN_POWER_HT40( 6)] = tpow_ht40[ 4];
813 1.1 christos power[ATHN_POWER_HT40( 7)] = tpow_ht40[ 5];
814 1.1 christos power[ATHN_POWER_HT40(12)] = tpow_ht40[ 6];
815 1.1 christos power[ATHN_POWER_HT40(13)] = tpow_ht40[ 7];
816 1.1 christos power[ATHN_POWER_HT40(14)] = tpow_ht40[ 8];
817 1.1 christos power[ATHN_POWER_HT40(15)] = tpow_ht40[ 9];
818 1.1 christos power[ATHN_POWER_HT40(20)] = tpow_ht40[10];
819 1.1 christos power[ATHN_POWER_HT40(21)] = tpow_ht40[11];
820 1.1 christos power[ATHN_POWER_HT40(22)] = tpow_ht40[12];
821 1.1 christos power[ATHN_POWER_HT40(23)] = tpow_ht40[13];
822 1.1 christos }
823 1.1 christos
824 1.1 christos /* Write transmit power values to hardware. */
825 1.1 christos ar9003_write_txpower(sc, power);
826 1.1 christos
827 1.1 christos /* Apply transmit power correction. */
828 1.1 christos ar9380_set_correction(sc, c);
829 1.1 christos }
830 1.1 christos
831 1.1 christos Static void
832 1.1 christos ar9380_get_correction(struct athn_softc *sc, struct ieee80211_channel *c,
833 1.1 christos int chain, int *corr, int *temp)
834 1.1 christos {
835 1.1 christos const struct ar9380_eeprom *eep = sc->sc_eep;
836 1.1 christos const struct ar9380_cal_data_per_freq_op_loop *pierdata;
837 1.1 christos const uint8_t *pierfreq;
838 1.1 christos uint8_t fbin;
839 1.1 christos int lo, hi, npiers;
840 1.1 christos
841 1.1 christos if (IEEE80211_IS_CHAN_2GHZ(c)) {
842 1.1 christos pierfreq = eep->calFreqPier2G;
843 1.1 christos pierdata = eep->calPierData2G[chain];
844 1.1 christos npiers = AR9380_NUM_2G_CAL_PIERS;
845 1.1 christos }
846 1.1 christos else {
847 1.1 christos pierfreq = eep->calFreqPier5G;
848 1.1 christos pierdata = eep->calPierData5G[chain];
849 1.1 christos npiers = AR9380_NUM_5G_CAL_PIERS;
850 1.1 christos }
851 1.1 christos /* Find channel in ROM pier table. */
852 1.1 christos fbin = athn_chan2fbin(c);
853 1.1 christos athn_get_pier_ival(fbin, pierfreq, npiers, &lo, &hi);
854 1.1 christos
855 1.1 christos *corr = athn_interpolate(fbin,
856 1.1 christos pierfreq[lo], pierdata[lo].refPower,
857 1.1 christos pierfreq[hi], pierdata[hi].refPower);
858 1.1 christos *temp = athn_interpolate(fbin,
859 1.1 christos pierfreq[lo], pierdata[lo].tempMeas,
860 1.1 christos pierfreq[hi], pierdata[hi].tempMeas);
861 1.1 christos }
862 1.1 christos
863 1.1 christos Static void
864 1.1 christos ar9380_set_correction(struct athn_softc *sc, struct ieee80211_channel *c)
865 1.1 christos {
866 1.1 christos const struct ar9380_eeprom *eep = sc->sc_eep;
867 1.1 christos const struct ar9380_modal_eep_header *modal;
868 1.1 christos uint32_t reg;
869 1.1 christos int8_t slope;
870 1.1 christos int i, corr, temp, temp0;
871 1.1 christos
872 1.1 christos if (IEEE80211_IS_CHAN_2GHZ(c))
873 1.1 christos modal = &eep->modalHeader2G;
874 1.1 christos else
875 1.1 christos modal = &eep->modalHeader5G;
876 1.1 christos
877 1.1 christos temp0 = 0; /* XXX: gcc */
878 1.1 christos for (i = 0; i < AR9380_MAX_CHAINS; i++) {
879 1.1 christos ar9380_get_correction(sc, c, i, &corr, &temp);
880 1.1 christos if (i == 0)
881 1.1 christos temp0 = temp;
882 1.1 christos
883 1.1 christos reg = AR_READ(sc, AR_PHY_TPC_11_B(i));
884 1.1 christos reg = RW(reg, AR_PHY_TPC_11_OLPC_GAIN_DELTA, corr);
885 1.1 christos AR_WRITE(sc, AR_PHY_TPC_11_B(i), reg);
886 1.1 christos
887 1.1 christos /* Enable open loop power control. */
888 1.1 christos reg = AR_READ(sc, AR_PHY_TPC_6_B(i));
889 1.1 christos reg = RW(reg, AR_PHY_TPC_6_ERROR_EST_MODE, 3);
890 1.1 christos AR_WRITE(sc, AR_PHY_TPC_6_B(i), reg);
891 1.1 christos }
892 1.1 christos
893 1.1 christos /* Enable temperature compensation. */
894 1.1 christos if (IEEE80211_IS_CHAN_5GHZ(c) &&
895 1.1 christos eep->base_ext2.tempSlopeLow != 0) {
896 1.1 christos if (c->ic_freq <= 5500) {
897 1.1 christos slope = athn_interpolate(c->ic_freq,
898 1.1 christos 5180, eep->base_ext2.tempSlopeLow,
899 1.1 christos 5500, modal->tempSlope);
900 1.1 christos }
901 1.1 christos else {
902 1.1 christos slope = athn_interpolate(c->ic_freq,
903 1.1 christos 5500, modal->tempSlope,
904 1.1 christos 5785, eep->base_ext2.tempSlopeHigh);
905 1.1 christos }
906 1.1 christos }
907 1.1 christos else
908 1.1 christos slope = modal->tempSlope;
909 1.1 christos
910 1.1 christos reg = AR_READ(sc, AR_PHY_TPC_19);
911 1.1 christos reg = RW(reg, AR_PHY_TPC_19_ALPHA_THERM, slope);
912 1.1 christos AR_WRITE(sc, AR_PHY_TPC_19, reg);
913 1.1 christos
914 1.1 christos reg = AR_READ(sc, AR_PHY_TPC_18);
915 1.1 christos reg = RW(reg, AR_PHY_TPC_18_THERM_CAL, temp0);
916 1.1 christos AR_WRITE(sc, AR_PHY_TPC_18, reg);
917 1.1 christos AR_WRITE_BARRIER(sc);
918 1.1 christos }
919