xref: /netbsd-src/sys/dev/ic/arn9380.c (revision 13d4bb4cc874de96add7fc4227d38a1d656b03d1)
1*13d4bb4cSthorpej /*	$NetBSD: arn9380.c,v 1.4 2022/09/25 18:43:32 thorpej Exp $	*/
264f89611Schristos /*	$OpenBSD: ar9380.c,v 1.17 2012/10/20 09:54:20 stsp Exp $	*/
364f89611Schristos 
464f89611Schristos /*-
564f89611Schristos  * Copyright (c) 2011 Damien Bergamini <damien.bergamini@free.fr>
664f89611Schristos  * Copyright (c) 2010 Atheros Communications Inc.
764f89611Schristos  *
864f89611Schristos  * Permission to use, copy, modify, and/or distribute this software for any
964f89611Schristos  * purpose with or without fee is hereby granted, provided that the above
1064f89611Schristos  * copyright notice and this permission notice appear in all copies.
1164f89611Schristos  *
1264f89611Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1364f89611Schristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1464f89611Schristos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1564f89611Schristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1664f89611Schristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1764f89611Schristos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1864f89611Schristos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1964f89611Schristos  */
2064f89611Schristos 
2164f89611Schristos /*
2264f89611Schristos  * Driver for Atheros 802.11a/g/n chipsets.
2364f89611Schristos  * Routines for AR9380 and AR9485 chipsets.
2464f89611Schristos  */
2564f89611Schristos 
2664f89611Schristos #include <sys/cdefs.h>
27*13d4bb4cSthorpej __KERNEL_RCSID(0, "$NetBSD: arn9380.c,v 1.4 2022/09/25 18:43:32 thorpej Exp $");
2864f89611Schristos 
2964f89611Schristos #include <sys/param.h>
3064f89611Schristos #include <sys/sockio.h>
3164f89611Schristos #include <sys/mbuf.h>
3264f89611Schristos #include <sys/kernel.h>
3364f89611Schristos #include <sys/socket.h>
3464f89611Schristos #include <sys/systm.h>
3564f89611Schristos #include <sys/queue.h>
3664f89611Schristos #include <sys/conf.h>
3764f89611Schristos #include <sys/device.h>
3864f89611Schristos 
3964f89611Schristos #include <sys/bus.h>
4064f89611Schristos #include <sys/endian.h>
4164f89611Schristos 
4264f89611Schristos #include <net/bpf.h>
4364f89611Schristos #include <net/if.h>
4464f89611Schristos #include <net/if_arp.h>
4564f89611Schristos #include <net/if_dl.h>
4602421171Schristos #include <net/if_ether.h>
4764f89611Schristos #include <net/if_media.h>
4864f89611Schristos #include <net/if_types.h>
4964f89611Schristos 
5064f89611Schristos #include <netinet/in.h>
5164f89611Schristos #include <netinet/in_systm.h>
5264f89611Schristos #include <netinet/in_var.h>
5364f89611Schristos 
5464f89611Schristos #include <net80211/ieee80211_var.h>
5564f89611Schristos #include <net80211/ieee80211_amrr.h>
5664f89611Schristos #include <net80211/ieee80211_radiotap.h>
5764f89611Schristos 
5864f89611Schristos #include <dev/ic/athnreg.h>
5964f89611Schristos #include <dev/ic/athnvar.h>
6064f89611Schristos 
6164f89611Schristos #include <dev/ic/arn9003reg.h>
6264f89611Schristos #include <dev/ic/arn9380reg.h>
6364f89611Schristos 
6464f89611Schristos #include <dev/ic/arn9003.h>
6564f89611Schristos #include <dev/ic/arn9380.h>
6664f89611Schristos 
6764f89611Schristos #define Static static
6864f89611Schristos 
6964f89611Schristos Static void	ar9380_get_correction(struct athn_softc *,
7064f89611Schristos 		    struct ieee80211_channel *, int, int *, int *);
7164f89611Schristos Static void	ar9380_get_paprd_masks(struct athn_softc *,
7264f89611Schristos 		    struct ieee80211_channel *, uint32_t *, uint32_t *);
7364f89611Schristos Static const uint8_t *
7464f89611Schristos 		ar9380_get_rom_template(struct athn_softc *, uint8_t);
7564f89611Schristos Static void	ar9380_init_from_rom(struct athn_softc *,
7664f89611Schristos 		    struct ieee80211_channel *, struct ieee80211_channel *);
7764f89611Schristos Static void	ar9380_set_correction(struct athn_softc *,
7864f89611Schristos 		    struct ieee80211_channel *);
7964f89611Schristos Static int	ar9380_set_synth(struct athn_softc *,
8064f89611Schristos 		    struct ieee80211_channel *, struct ieee80211_channel *);
8164f89611Schristos Static void	ar9380_set_txpower(struct athn_softc *,
8264f89611Schristos 		    struct ieee80211_channel *, struct ieee80211_channel *);
8364f89611Schristos Static void	ar9380_setup(struct athn_softc *);
8464f89611Schristos Static void	ar9380_spur_mitigate(struct athn_softc *,
8564f89611Schristos 		    struct ieee80211_channel *, struct ieee80211_channel *);
8664f89611Schristos Static void	ar9380_spur_mitigate_cck(struct athn_softc *,
8764f89611Schristos 		    struct ieee80211_channel *, struct ieee80211_channel *);
8864f89611Schristos Static void	ar9380_spur_mitigate_ofdm(struct athn_softc *,
8964f89611Schristos 		    struct ieee80211_channel *, struct ieee80211_channel *);
9064f89611Schristos Static void	ar9380_swap_rom(struct athn_softc *);
9164f89611Schristos 
9264f89611Schristos Static void	ar9485_init_swreg(struct athn_softc *);
9364f89611Schristos #define		ar9485_pmu_read		AR_READ
9464f89611Schristos Static int	ar9485_pmu_write(struct athn_softc *, uint32_t, uint32_t);
9564f89611Schristos 
9664f89611Schristos #ifdef notused
9764f89611Schristos Static void	ar9380_init_swreg(struct athn_softc *);
9864f89611Schristos #endif /* notused */
9964f89611Schristos 
10064f89611Schristos PUBLIC int
ar9380_attach(struct athn_softc * sc)10164f89611Schristos ar9380_attach(struct athn_softc *sc)
10264f89611Schristos {
10364f89611Schristos 
10464f89611Schristos 	sc->sc_ngpiopins = 17;
10564f89611Schristos 	sc->sc_ops.setup = ar9380_setup;
10664f89611Schristos 	sc->sc_ops.get_rom_template = ar9380_get_rom_template;
10764f89611Schristos 	sc->sc_ops.swap_rom = ar9380_swap_rom;
10864f89611Schristos 	sc->sc_ops.init_from_rom = ar9380_init_from_rom;
10964f89611Schristos 	sc->sc_ops.set_txpower = ar9380_set_txpower;
11064f89611Schristos 	sc->sc_ops.set_synth = ar9380_set_synth;
11164f89611Schristos 	sc->sc_ops.spur_mitigate = ar9380_spur_mitigate;
11264f89611Schristos 	sc->sc_ops.get_paprd_masks = ar9380_get_paprd_masks;
11364f89611Schristos 	sc->sc_cca_min_2g = AR9380_PHY_CCA_MIN_GOOD_VAL_2GHZ;
11464f89611Schristos 	sc->sc_cca_max_2g = AR9380_PHY_CCA_MAX_GOOD_VAL_2GHZ;
11564f89611Schristos 	sc->sc_cca_min_5g = AR9380_PHY_CCA_MIN_GOOD_VAL_5GHZ;
11664f89611Schristos 	sc->sc_cca_max_5g = AR9380_PHY_CCA_MAX_GOOD_VAL_5GHZ;
11764f89611Schristos 	if (AR_SREV_9485(sc)) {
11864f89611Schristos 		sc->sc_ini = &ar9485_1_1_ini;
11964f89611Schristos 		sc->sc_serdes = &ar9485_1_1_serdes;
12064f89611Schristos 	}
12164f89611Schristos 	else {
12264f89611Schristos 		sc->sc_ini = &ar9380_2_2_ini;
12364f89611Schristos 		sc->sc_serdes = &ar9380_2_2_serdes;
12464f89611Schristos 	}
12564f89611Schristos 
12664f89611Schristos 	return ar9003_attach(sc);
12764f89611Schristos }
12864f89611Schristos 
12964f89611Schristos Static void
ar9380_setup(struct athn_softc * sc)13064f89611Schristos ar9380_setup(struct athn_softc *sc)
13164f89611Schristos {
13264f89611Schristos 	struct ieee80211com *ic = &sc->sc_ic;
13364f89611Schristos 	struct ar9380_eeprom *eep = sc->sc_eep;
13464f89611Schristos 	struct ar9380_base_eep_hdr *base = &eep->baseEepHeader;
13564f89611Schristos 	uint8_t type;
13664f89611Schristos 
13764f89611Schristos 	if (base->opFlags & AR_OPFLAGS_11A)
13864f89611Schristos 		sc->sc_flags |= ATHN_FLAG_11A;
13964f89611Schristos 	if (base->opFlags & AR_OPFLAGS_11G)
14064f89611Schristos 		sc->sc_flags |= ATHN_FLAG_11G;
14164f89611Schristos 	if (base->opFlags & AR_OPFLAGS_11N)
14264f89611Schristos 		sc->sc_flags |= ATHN_FLAG_11N;
14364f89611Schristos 
14464f89611Schristos 	IEEE80211_ADDR_COPY(ic->ic_myaddr, eep->macAddr);
14564f89611Schristos 	sc->sc_led_pin = base->wlanLedGpio;
14664f89611Schristos 
14764f89611Schristos 	/* Check if we have a hardware radio switch. */
14864f89611Schristos 	if (base->rfSilent & AR_EEP_RFSILENT_ENABLED) {
14964f89611Schristos 		sc->sc_flags |= ATHN_FLAG_RFSILENT;
15064f89611Schristos 		/* Get GPIO pin used by hardware radio switch. */
15164f89611Schristos 		sc->sc_rfsilent_pin = MS(base->rfSilent,
15264f89611Schristos 		    AR_EEP_RFSILENT_GPIO_SEL);
15364f89611Schristos 		/* Get polarity of hardware radio switch. */
15464f89611Schristos 		if (base->rfSilent & AR_EEP_RFSILENT_POLARITY)
15564f89611Schristos 			sc->sc_flags |= ATHN_FLAG_RFSILENT_REVERSED;
15664f89611Schristos 	}
15764f89611Schristos 
15864f89611Schristos 	/* Set the number of HW key cache entries. */
15964f89611Schristos 	sc->sc_kc_entries = AR_KEYTABLE_SIZE;
16064f89611Schristos 
16164f89611Schristos 	sc->sc_txchainmask = MS(base->txrxMask, AR_EEP_TX_MASK);
16264f89611Schristos 	sc->sc_rxchainmask = MS(base->txrxMask, AR_EEP_RX_MASK);
16364f89611Schristos 
16464f89611Schristos 	/* Fast PLL clock is always supported. */
16564f89611Schristos 	sc->sc_flags |= ATHN_FLAG_FAST_PLL_CLOCK;
16664f89611Schristos 
16764f89611Schristos 	/* Enable PA predistortion if supported. */
16864f89611Schristos 	if (base->featureEnable & AR_EEP_PAPRD)
16964f89611Schristos 		sc->sc_flags |= ATHN_FLAG_PAPRD;
17064f89611Schristos 	/*
17164f89611Schristos 	 * Some 3-stream chips may exceed the PCIe power requirements,
17264f89611Schristos 	 * requiring to reduce the number of Tx chains in some cases.
17364f89611Schristos 	 */
17464f89611Schristos 	if ((base->miscConfiguration & AR_EEP_CHAIN_MASK_REDUCE) &&
17564f89611Schristos 	    sc->sc_txchainmask == 0x7)
17664f89611Schristos 		sc->sc_flags |= ATHN_FLAG_3TREDUCE_CHAIN;
17764f89611Schristos 
17864f89611Schristos 	/* Select initialization values based on ROM. */
17964f89611Schristos 	type = MS(eep->baseEepHeader.txrxgain, AR_EEP_RX_GAIN);
18064f89611Schristos 	if (!AR_SREV_9485(sc)) {
18164f89611Schristos 		if (type == AR_EEP_RX_GAIN_WO_XLNA)
18264f89611Schristos 			sc->sc_rx_gain = &ar9380_2_2_rx_gain_wo_xlna;
18364f89611Schristos 		else
18464f89611Schristos 			sc->sc_rx_gain = &ar9380_2_2_rx_gain;
18564f89611Schristos 	}
18664f89611Schristos 	else
18764f89611Schristos 		sc->sc_rx_gain = &ar9485_1_1_rx_gain;
18864f89611Schristos 
18964f89611Schristos 	/* Select initialization values based on ROM. */
19064f89611Schristos 	type = MS(eep->baseEepHeader.txrxgain, AR_EEP_TX_GAIN);
19164f89611Schristos 	if (!AR_SREV_9485(sc)) {
19264f89611Schristos 		if (type == AR_EEP_TX_GAIN_HIGH_OB_DB)
19364f89611Schristos 			sc->sc_tx_gain = &ar9380_2_2_tx_gain_high_ob_db;
19464f89611Schristos 		else if (type == AR_EEP_TX_GAIN_LOW_OB_DB)
19564f89611Schristos 			sc->sc_tx_gain = &ar9380_2_2_tx_gain_low_ob_db;
19664f89611Schristos 		else if (type == AR_EEP_TX_GAIN_HIGH_POWER)
19764f89611Schristos 			sc->sc_tx_gain = &ar9380_2_2_tx_gain_high_power;
19864f89611Schristos 		else
19964f89611Schristos 			sc->sc_tx_gain = &ar9380_2_2_tx_gain;
20064f89611Schristos 	}
20164f89611Schristos 	else
20264f89611Schristos 		sc->sc_tx_gain = &ar9485_1_1_tx_gain;
20364f89611Schristos }
20464f89611Schristos 
20564f89611Schristos Static const uint8_t *
ar9380_get_rom_template(struct athn_softc * sc,uint8_t ref)20664f89611Schristos ar9380_get_rom_template(struct athn_softc *sc, uint8_t ref)
20764f89611Schristos {
20864f89611Schristos 	size_t i;
20964f89611Schristos 
21064f89611Schristos 	/* Retrieve template ROM image for given reference. */
21164f89611Schristos 	for (i = 0; i < __arraycount(ar9380_rom_templates); i++)
21264f89611Schristos 		if (ar9380_rom_templates[i][1] == ref)
21364f89611Schristos 			return ar9380_rom_templates[i];
21464f89611Schristos 	return NULL;
21564f89611Schristos }
21664f89611Schristos 
21764f89611Schristos Static void
ar9380_swap_rom(struct athn_softc * sc)21864f89611Schristos ar9380_swap_rom(struct athn_softc *sc)
21964f89611Schristos {
22064f89611Schristos #if BYTE_ORDER == BIG_ENDIAN
22164f89611Schristos 	struct ar9380_eeprom *eep = sc->sc_eep;
22264f89611Schristos 	struct ar9380_base_eep_hdr *base = &eep->baseEepHeader;
22364f89611Schristos 	struct ar9380_modal_eep_header *modal;
22464f89611Schristos 	int i;
22564f89611Schristos 
2267050a06bSmatt 	base->regDmn[0] = bswap16(base->regDmn[0]);
2277050a06bSmatt 	base->regDmn[1] = bswap16(base->regDmn[1]);
2287050a06bSmatt 	base->swreg = bswap32(base->swreg);
22964f89611Schristos 
23064f89611Schristos 	modal = &eep->modalHeader2G;
2317050a06bSmatt 	modal->antCtrlCommon = bswap32(modal->antCtrlCommon);
2327050a06bSmatt 	modal->antCtrlCommon2 = bswap32(modal->antCtrlCommon2);
2337050a06bSmatt 	modal->papdRateMaskHt20 = bswap32(modal->papdRateMaskHt20);
2347050a06bSmatt 	modal->papdRateMaskHt40 = bswap32(modal->papdRateMaskHt40);
23564f89611Schristos 	for (i = 0; i < AR9380_MAX_CHAINS; i++)
2367050a06bSmatt 		modal->antCtrlChain[i] = bswap16(modal->antCtrlChain[i]);
23764f89611Schristos 
23864f89611Schristos 	modal = &eep->modalHeader5G;
2397050a06bSmatt 	modal->antCtrlCommon = bswap32(modal->antCtrlCommon);
2407050a06bSmatt 	modal->antCtrlCommon2 = bswap32(modal->antCtrlCommon2);
2417050a06bSmatt 	modal->papdRateMaskHt20 = bswap32(modal->papdRateMaskHt20);
2427050a06bSmatt 	modal->papdRateMaskHt40 = bswap32(modal->papdRateMaskHt40);
24364f89611Schristos 	for (i = 0; i < AR9380_MAX_CHAINS; i++)
2447050a06bSmatt 		modal->antCtrlChain[i] = bswap16(modal->antCtrlChain[i]);
24564f89611Schristos #endif
24664f89611Schristos }
24764f89611Schristos 
24864f89611Schristos Static void
ar9380_get_paprd_masks(struct athn_softc * sc,struct ieee80211_channel * c,uint32_t * ht20mask,uint32_t * ht40mask)24964f89611Schristos ar9380_get_paprd_masks(struct athn_softc *sc, struct ieee80211_channel *c,
25064f89611Schristos     uint32_t *ht20mask, uint32_t *ht40mask)
25164f89611Schristos {
25264f89611Schristos 	const struct ar9380_eeprom *eep = sc->sc_eep;
25364f89611Schristos 	const struct ar9380_modal_eep_header *modal;
25464f89611Schristos 
25564f89611Schristos 	if (IEEE80211_IS_CHAN_2GHZ(c))
25664f89611Schristos 		modal = &eep->modalHeader2G;
25764f89611Schristos 	else
25864f89611Schristos 		modal = &eep->modalHeader5G;
25964f89611Schristos 	*ht20mask = modal->papdRateMaskHt20;
26064f89611Schristos 	*ht40mask = modal->papdRateMaskHt40;
26164f89611Schristos }
26264f89611Schristos 
26364f89611Schristos Static int
ar9380_set_synth(struct athn_softc * sc,struct ieee80211_channel * c,struct ieee80211_channel * extc)26464f89611Schristos ar9380_set_synth(struct athn_softc *sc, struct ieee80211_channel *c,
26564f89611Schristos     struct ieee80211_channel *extc)
26664f89611Schristos {
26764f89611Schristos 	uint32_t freq = c->ic_freq;
26864f89611Schristos 	uint32_t chansel, phy;
26964f89611Schristos 
27064f89611Schristos 	if (IEEE80211_IS_CHAN_2GHZ(c)) {
27164f89611Schristos 		if (AR_SREV_9485(sc))
27264f89611Schristos 			chansel = ((freq << 16) - 215) / 15;
27364f89611Schristos 		else
27464f89611Schristos 			chansel = (freq << 16) / 15;
27564f89611Schristos 		AR_WRITE(sc, AR_PHY_SYNTH_CONTROL, AR9380_BMODE);
27664f89611Schristos 	}
27764f89611Schristos 	else {
27864f89611Schristos 		chansel = (freq << 15) / 15;
27964f89611Schristos 		chansel >>= 1;
28064f89611Schristos 		AR_WRITE(sc, AR_PHY_SYNTH_CONTROL, 0);
28164f89611Schristos 	}
28264f89611Schristos 
28364f89611Schristos 	/* Enable Long Shift Select for synthesizer. */
28464f89611Schristos 	AR_SETBITS(sc, AR_PHY_65NM_CH0_SYNTH4,
28564f89611Schristos 	    AR_PHY_SYNTH4_LONG_SHIFT_SELECT);
28664f89611Schristos 	AR_WRITE_BARRIER(sc);
28764f89611Schristos 
28864f89611Schristos 	/* Program synthesizer. */
28964f89611Schristos 	phy = (chansel << 2) | AR9380_FRACMODE;
29064f89611Schristos 	DPRINTFN(DBG_RF, sc, "AR_PHY_65NM_CH0_SYNTH7=0x%08x\n", phy);
29164f89611Schristos 	AR_WRITE(sc, AR_PHY_65NM_CH0_SYNTH7, phy);
29264f89611Schristos 	AR_WRITE_BARRIER(sc);
29364f89611Schristos 	/* Toggle Load Synth Channel bit. */
29464f89611Schristos 	AR_WRITE(sc, AR_PHY_65NM_CH0_SYNTH7, phy | AR9380_LOAD_SYNTH);
29564f89611Schristos 	AR_WRITE_BARRIER(sc);
29664f89611Schristos 	return 0;
29764f89611Schristos }
29864f89611Schristos 
29964f89611Schristos Static void
ar9380_init_from_rom(struct athn_softc * sc,struct ieee80211_channel * c,struct ieee80211_channel * extc)30064f89611Schristos ar9380_init_from_rom(struct athn_softc *sc, struct ieee80211_channel *c,
30164f89611Schristos     struct ieee80211_channel *extc)
30264f89611Schristos {
30364f89611Schristos 	const struct ar9380_eeprom *eep = sc->sc_eep;
30464f89611Schristos 	const struct ar9380_modal_eep_header *modal;
30564f89611Schristos 	uint8_t db, margin, ant_div_ctrl;
30664f89611Schristos 	uint32_t reg;
30764f89611Schristos 	int i, maxchains;
30864f89611Schristos 
30964f89611Schristos 	if (IEEE80211_IS_CHAN_2GHZ(c))
31064f89611Schristos 		modal = &eep->modalHeader2G;
31164f89611Schristos 	else
31264f89611Schristos 		modal = &eep->modalHeader5G;
31364f89611Schristos 
31464f89611Schristos 	/* Apply XPA bias level. */
31564f89611Schristos 	if (AR_SREV_9485(sc)) {
31664f89611Schristos 		reg = AR_READ(sc, AR9485_PHY_65NM_CH0_TOP2);
31764f89611Schristos 		reg = RW(reg, AR9485_PHY_65NM_CH0_TOP2_XPABIASLVL,
31864f89611Schristos 		    modal->xpaBiasLvl);
31964f89611Schristos 		AR_WRITE(sc, AR9485_PHY_65NM_CH0_TOP2, reg);
32064f89611Schristos 	}
32164f89611Schristos 	else {
32264f89611Schristos 		reg = AR_READ(sc, AR_PHY_65NM_CH0_TOP);
32364f89611Schristos 		reg = RW(reg, AR_PHY_65NM_CH0_TOP_XPABIASLVL,
32464f89611Schristos 		    modal->xpaBiasLvl & 0x3);
32564f89611Schristos 		AR_WRITE(sc, AR_PHY_65NM_CH0_TOP, reg);
32664f89611Schristos 		reg = AR_READ(sc, AR_PHY_65NM_CH0_THERM);
32764f89611Schristos 		reg = RW(reg, AR_PHY_65NM_CH0_THERM_XPABIASLVL_MSB,
32864f89611Schristos 		    modal->xpaBiasLvl >> 2);
32964f89611Schristos 		reg |= AR_PHY_65NM_CH0_THERM_XPASHORT2GND;
33064f89611Schristos 		AR_WRITE(sc, AR_PHY_65NM_CH0_THERM, reg);
33164f89611Schristos 	}
33264f89611Schristos 
33364f89611Schristos 	/* Apply antenna control. */
33464f89611Schristos 	reg = AR_READ(sc, AR_PHY_SWITCH_COM);
33564f89611Schristos 	reg = RW(reg, AR_SWITCH_TABLE_COM_ALL, modal->antCtrlCommon);
33664f89611Schristos 	AR_WRITE(sc, AR_PHY_SWITCH_COM, reg);
33764f89611Schristos 	reg = AR_READ(sc, AR_PHY_SWITCH_COM_2);
33864f89611Schristos 	reg = RW(reg, AR_SWITCH_TABLE_COM_2_ALL, modal->antCtrlCommon2);
33964f89611Schristos 	AR_WRITE(sc, AR_PHY_SWITCH_COM_2, reg);
34064f89611Schristos 
34164f89611Schristos 	maxchains = AR_SREV_9485(sc) ? 1 : AR9380_MAX_CHAINS;
34264f89611Schristos 	for (i = 0; i < maxchains; i++) {
34364f89611Schristos 		reg = AR_READ(sc, AR_PHY_SWITCH_CHAIN(i));
34464f89611Schristos 		reg = RW(reg, AR_SWITCH_TABLE_ALL, modal->antCtrlChain[i]);
34564f89611Schristos 		AR_WRITE(sc, AR_PHY_SWITCH_CHAIN(i), reg);
34664f89611Schristos 	}
34764f89611Schristos 
34864f89611Schristos 	if (AR_SREV_9485(sc)) {
34964f89611Schristos 		ant_div_ctrl = eep->base_ext1.ant_div_control;
35064f89611Schristos 		reg = AR_READ(sc, AR_PHY_MC_GAIN_CTRL);
35164f89611Schristos 		reg = RW(reg, AR_PHY_MC_GAIN_CTRL_ANT_DIV_CTRL_ALL,
35264f89611Schristos 		    MS(ant_div_ctrl, AR_EEP_ANT_DIV_CTRL_ALL));
35364f89611Schristos 		if (ant_div_ctrl & AR_EEP_ANT_DIV_CTRL_ANT_DIV)
35464f89611Schristos 			reg |= AR_PHY_MC_GAIN_CTRL_ENABLE_ANT_DIV;
35564f89611Schristos 		else
35664f89611Schristos 			reg &= ~AR_PHY_MC_GAIN_CTRL_ENABLE_ANT_DIV;
35764f89611Schristos 		AR_WRITE(sc, AR_PHY_MC_GAIN_CTRL, reg);
35864f89611Schristos 		reg = AR_READ(sc, AR_PHY_CCK_DETECT);
35964f89611Schristos 		if (ant_div_ctrl & AR_EEP_ANT_DIV_CTRL_FAST_DIV)
36064f89611Schristos 			reg |= AR_PHY_CCK_DETECT_BB_ENABLE_ANT_FAST_DIV;
36164f89611Schristos 		else
36264f89611Schristos 			reg &= ~AR_PHY_CCK_DETECT_BB_ENABLE_ANT_FAST_DIV;
36364f89611Schristos 		AR_WRITE(sc, AR_PHY_CCK_DETECT, reg);
36464f89611Schristos 	}
36564f89611Schristos 
36664f89611Schristos 	if (eep->baseEepHeader.miscConfiguration & AR_EEP_DRIVE_STRENGTH) {
36764f89611Schristos 		/* Apply drive strength. */
36864f89611Schristos 		reg = AR_READ(sc, AR_PHY_65NM_CH0_BIAS1);
36964f89611Schristos 		reg = RW(reg, AR_PHY_65NM_CH0_BIAS1_0, 5);
37064f89611Schristos 		reg = RW(reg, AR_PHY_65NM_CH0_BIAS1_1, 5);
37164f89611Schristos 		reg = RW(reg, AR_PHY_65NM_CH0_BIAS1_2, 5);
37264f89611Schristos 		reg = RW(reg, AR_PHY_65NM_CH0_BIAS1_3, 5);
37364f89611Schristos 		reg = RW(reg, AR_PHY_65NM_CH0_BIAS1_4, 5);
37464f89611Schristos 		reg = RW(reg, AR_PHY_65NM_CH0_BIAS1_5, 5);
37564f89611Schristos 		AR_WRITE(sc, AR_PHY_65NM_CH0_BIAS1, reg);
37664f89611Schristos 
37764f89611Schristos 		reg = AR_READ(sc, AR_PHY_65NM_CH0_BIAS2);
37864f89611Schristos 		reg = RW(reg, AR_PHY_65NM_CH0_BIAS2_0, 5);
37964f89611Schristos 		reg = RW(reg, AR_PHY_65NM_CH0_BIAS2_1, 5);
38064f89611Schristos 		reg = RW(reg, AR_PHY_65NM_CH0_BIAS2_2, 5);
38164f89611Schristos 		reg = RW(reg, AR_PHY_65NM_CH0_BIAS2_3, 5);
38264f89611Schristos 		reg = RW(reg, AR_PHY_65NM_CH0_BIAS2_4, 5);
38364f89611Schristos 		reg = RW(reg, AR_PHY_65NM_CH0_BIAS2_5, 5);
38464f89611Schristos 		reg = RW(reg, AR_PHY_65NM_CH0_BIAS2_6, 5);
38564f89611Schristos 		reg = RW(reg, AR_PHY_65NM_CH0_BIAS2_7, 5);
38664f89611Schristos 		reg = RW(reg, AR_PHY_65NM_CH0_BIAS2_8, 5);
38764f89611Schristos 		AR_WRITE(sc, AR_PHY_65NM_CH0_BIAS2, reg);
38864f89611Schristos 
38964f89611Schristos 		reg = AR_READ(sc, AR_PHY_65NM_CH0_BIAS4);
39064f89611Schristos 		reg = RW(reg, AR_PHY_65NM_CH0_BIAS4_0, 5);
39164f89611Schristos 		reg = RW(reg, AR_PHY_65NM_CH0_BIAS4_1, 5);
39264f89611Schristos 		reg = RW(reg, AR_PHY_65NM_CH0_BIAS4_2, 5);
39364f89611Schristos 		AR_WRITE(sc, AR_PHY_65NM_CH0_BIAS4, reg);
39464f89611Schristos 	}
39564f89611Schristos 
39664f89611Schristos 	/* Apply attenuation settings. */
39764f89611Schristos 	maxchains = AR_SREV_9485(sc) ? 1 : AR9380_MAX_CHAINS;
39864f89611Schristos 	for (i = 0; i < maxchains; i++) {
39964f89611Schristos 		if (IEEE80211_IS_CHAN_5GHZ(c) &&
40064f89611Schristos 		    eep->base_ext2.xatten1DBLow[i] != 0) {
40164f89611Schristos 			if (c->ic_freq <= 5500) {
40264f89611Schristos 				db = athn_interpolate(c->ic_freq,
40364f89611Schristos 				    5180, eep->base_ext2.xatten1DBLow[i],
40464f89611Schristos 				    5500, modal->xatten1DB[i]);
40564f89611Schristos 			}
40664f89611Schristos 			else {
40764f89611Schristos 				db = athn_interpolate(c->ic_freq,
40864f89611Schristos 				    5500, modal->xatten1DB[i],
40964f89611Schristos 				    5785, eep->base_ext2.xatten1DBHigh[i]);
41064f89611Schristos 			}
41164f89611Schristos 		}
41264f89611Schristos 		else
41364f89611Schristos 			db = modal->xatten1DB[i];
41464f89611Schristos 		if (IEEE80211_IS_CHAN_5GHZ(c) &&
41564f89611Schristos 		    eep->base_ext2.xatten1MarginLow[i] != 0) {
41664f89611Schristos 			if (c->ic_freq <= 5500) {
41764f89611Schristos 				margin = athn_interpolate(c->ic_freq,
41864f89611Schristos 				    5180, eep->base_ext2.xatten1MarginLow[i],
41964f89611Schristos 				    5500, modal->xatten1Margin[i]);
42064f89611Schristos 			}
42164f89611Schristos 			else {
42264f89611Schristos 				margin = athn_interpolate(c->ic_freq,
42364f89611Schristos 				    5500, modal->xatten1Margin[i],
42464f89611Schristos 				    5785, eep->base_ext2.xatten1MarginHigh[i]);
42564f89611Schristos 			}
42664f89611Schristos 		}
42764f89611Schristos 		else
42864f89611Schristos 			margin = modal->xatten1Margin[i];
42964f89611Schristos 		reg = AR_READ(sc, AR_PHY_EXT_ATTEN_CTL(i));
43064f89611Schristos 		reg = RW(reg, AR_PHY_EXT_ATTEN_CTL_XATTEN1_DB, db);
43164f89611Schristos 		reg = RW(reg, AR_PHY_EXT_ATTEN_CTL_XATTEN1_MARGIN, margin);
43264f89611Schristos 		AR_WRITE(sc, AR_PHY_EXT_ATTEN_CTL(i), reg);
43364f89611Schristos 	}
43464f89611Schristos 
43564f89611Schristos 	/* Initialize switching regulator. */
43664f89611Schristos 	if (AR_SREV_9485(sc))
43764f89611Schristos 		ar9485_init_swreg(sc);
43864f89611Schristos 	else
43964f89611Schristos 		ar9485_init_swreg(sc);
44064f89611Schristos 
44164f89611Schristos 	/* Apply tuning capabilities. */
44264f89611Schristos 	if (AR_SREV_9485(sc) &&
44364f89611Schristos 	    (eep->baseEepHeader.featureEnable & AR_EEP_TUNING_CAPS)) {
44464f89611Schristos 		reg = AR_READ(sc, AR9485_PHY_CH0_XTAL);
44564f89611Schristos 		reg = RW(reg, AR9485_PHY_CH0_XTAL_CAPINDAC,
44664f89611Schristos 		    eep->baseEepHeader.params_for_tuning_caps[0]);
44764f89611Schristos 		reg = RW(reg, AR9485_PHY_CH0_XTAL_CAPOUTDAC,
44864f89611Schristos 		    eep->baseEepHeader.params_for_tuning_caps[0]);
44964f89611Schristos 		AR_WRITE(sc, AR9485_PHY_CH0_XTAL, reg);
45064f89611Schristos 	}
45164f89611Schristos 	AR_WRITE_BARRIER(sc);
45264f89611Schristos }
45364f89611Schristos 
45464f89611Schristos #ifdef notused
45564f89611Schristos Static void
ar9380_init_swreg(struct athn_softc * sc)45664f89611Schristos ar9380_init_swreg(struct athn_softc *sc)
45764f89611Schristos {
45864f89611Schristos 	const struct ar9380_eeprom *eep = sc->sc_eep;
45964f89611Schristos 
46064f89611Schristos 	if (eep->baseEepHeader.featureEnable & AR_EEP_INTERNAL_REGULATOR) {
46164f89611Schristos 		/* Internal regulator is ON. */
46264f89611Schristos 		AR_CLRBITS(sc, AR_RTC_REG_CONTROL1,
46364f89611Schristos 		    AR_RTC_REG_CONTROL1_SWREG_PROGRAM);
46464f89611Schristos 		AR_WRITE(sc, AR_RTC_REG_CONTROL0, eep->baseEepHeader.swreg);
46564f89611Schristos 		AR_SETBITS(sc, AR_RTC_REG_CONTROL1,
46664f89611Schristos 		    AR_RTC_REG_CONTROL1_SWREG_PROGRAM);
46764f89611Schristos 	}
46864f89611Schristos 	else
46964f89611Schristos 		AR_SETBITS(sc, AR_RTC_SLEEP_CLK, AR_RTC_FORCE_SWREG_PRD);
47064f89611Schristos 	AR_WRITE_BARRIER(sc);
47164f89611Schristos }
47264f89611Schristos #endif /* notused */
47364f89611Schristos 
47464f89611Schristos Static int
ar9485_pmu_write(struct athn_softc * sc,uint32_t addr,uint32_t val)47564f89611Schristos ar9485_pmu_write(struct athn_softc *sc, uint32_t addr, uint32_t val)
47664f89611Schristos {
47764f89611Schristos 	int ntries;
47864f89611Schristos 
47964f89611Schristos 	AR_WRITE(sc, addr, val);
48064f89611Schristos 	/* Wait for write to complete. */
48164f89611Schristos 	for (ntries = 0; ntries < 100; ntries++) {
48264f89611Schristos 		if (AR_READ(sc, addr) == val)
48364f89611Schristos 			return 0;
48464f89611Schristos 		AR_WRITE(sc, addr, val);	/* Insist. */
48564f89611Schristos 		AR_WRITE_BARRIER(sc);
48664f89611Schristos 		DELAY(10);
48764f89611Schristos 	}
48864f89611Schristos 	return ETIMEDOUT;
48964f89611Schristos }
49064f89611Schristos 
49164f89611Schristos Static void
ar9485_init_swreg(struct athn_softc * sc)49264f89611Schristos ar9485_init_swreg(struct athn_softc *sc)
49364f89611Schristos {
49464f89611Schristos 	const struct ar9380_eeprom *eep = sc->sc_eep;
49564f89611Schristos 	uint32_t reg;
49664f89611Schristos 
49764f89611Schristos 	ar9485_pmu_write(sc, AR_PHY_PMU2,
49864f89611Schristos 	    ar9485_pmu_read(sc, AR_PHY_PMU2) & ~AR_PHY_PMU2_PGM);
49964f89611Schristos 
50064f89611Schristos 	if (eep->baseEepHeader.featureEnable & AR_EEP_INTERNAL_REGULATOR) {
50164f89611Schristos 		ar9485_pmu_write(sc, AR_PHY_PMU1, 0x131dc17a);
50264f89611Schristos 
50364f89611Schristos 		reg = ar9485_pmu_read(sc, AR_PHY_PMU2);
50464f89611Schristos 		reg = (reg & ~0xffc00000) | 0x10000000;
50564f89611Schristos 		ar9485_pmu_write(sc, AR_PHY_PMU2, reg);
50664f89611Schristos 	}
50764f89611Schristos 	else {
50864f89611Schristos 		ar9485_pmu_write(sc, AR_PHY_PMU1,
50964f89611Schristos 		    ar9485_pmu_read(sc, AR_PHY_PMU1) | AR_PHY_PMU1_PWD);
51064f89611Schristos 	}
51164f89611Schristos 
51264f89611Schristos 	ar9485_pmu_write(sc, AR_PHY_PMU2,
51364f89611Schristos 	    ar9485_pmu_read(sc, AR_PHY_PMU2) | AR_PHY_PMU2_PGM);
51464f89611Schristos }
51564f89611Schristos 
51664f89611Schristos /*
51764f89611Schristos  * NB: It is safe to call this function for 5GHz channels.
51864f89611Schristos  */
51964f89611Schristos Static void
ar9380_spur_mitigate_cck(struct athn_softc * sc,struct ieee80211_channel * c,struct ieee80211_channel * extc)52064f89611Schristos ar9380_spur_mitigate_cck(struct athn_softc *sc, struct ieee80211_channel *c,
52164f89611Schristos     struct ieee80211_channel *extc)
52264f89611Schristos {
52364f89611Schristos 	static const int16_t freqs[] = { 2420, 2440, 2464, 2480 };
52464f89611Schristos 	size_t i;
52564f89611Schristos 	int spur, freq;
52664f89611Schristos 	uint32_t reg;
52764f89611Schristos 
52864f89611Schristos 	for (i = 0; i < __arraycount(freqs); i++) {
52964f89611Schristos 		spur = freqs[i] - c->ic_freq;
53064f89611Schristos 		if (abs(spur) < 10)	/* +/- 10MHz range. */
53164f89611Schristos 			break;
53264f89611Schristos 	}
53364f89611Schristos 	if (i == __arraycount(freqs)) {
53464f89611Schristos 		/* Disable CCK spur mitigation. */
53564f89611Schristos 		reg = AR_READ(sc, AR_PHY_AGC_CONTROL);
53664f89611Schristos 		reg = RW(reg, AR_PHY_AGC_CONTROL_YCOK_MAX, 0x5);
53764f89611Schristos 		AR_WRITE(sc, AR_PHY_AGC_CONTROL, reg);
53864f89611Schristos 		reg = AR_READ(sc, AR_PHY_CCK_SPUR_MIT);
53964f89611Schristos 		reg = RW(reg, AR_PHY_CCK_SPUR_MIT_CCK_SPUR_FREQ, 0);
54064f89611Schristos 		reg &= ~AR_PHY_CCK_SPUR_MIT_USE_CCK_SPUR_MIT;
54164f89611Schristos 		AR_WRITE(sc, AR_PHY_CCK_SPUR_MIT, reg);
54264f89611Schristos 		AR_WRITE_BARRIER(sc);
54364f89611Schristos 		return;
54464f89611Schristos 	}
54564f89611Schristos 	freq = (spur * 524288) / 11;
54664f89611Schristos 
54764f89611Schristos 	reg = AR_READ(sc, AR_PHY_AGC_CONTROL);
54864f89611Schristos 	reg = RW(reg, AR_PHY_AGC_CONTROL_YCOK_MAX, 0x7);
54964f89611Schristos 	AR_WRITE(sc, AR_PHY_AGC_CONTROL, reg);
55064f89611Schristos 
55164f89611Schristos 	reg = AR_READ(sc, AR_PHY_CCK_SPUR_MIT);
55264f89611Schristos 	reg = RW(reg, AR_PHY_CCK_SPUR_MIT_CCK_SPUR_FREQ, freq);
55364f89611Schristos 	reg = RW(reg, AR_PHY_CCK_SPUR_MIT_SPUR_RSSI_THR, 0x7f);
55464f89611Schristos 	reg = RW(reg, AR_PHY_CCK_SPUR_MIT_SPUR_FILTER_TYPE, 0x2);
55564f89611Schristos 	reg |= AR_PHY_CCK_SPUR_MIT_USE_CCK_SPUR_MIT;
55664f89611Schristos 	AR_WRITE(sc, AR_PHY_CCK_SPUR_MIT, reg);
55764f89611Schristos 	AR_WRITE_BARRIER(sc);
55864f89611Schristos }
55964f89611Schristos 
56064f89611Schristos Static void
ar9380_spur_mitigate_ofdm(struct athn_softc * sc,struct ieee80211_channel * c,struct ieee80211_channel * extc)56164f89611Schristos ar9380_spur_mitigate_ofdm(struct athn_softc *sc, struct ieee80211_channel *c,
56264f89611Schristos     struct ieee80211_channel *extc)
56364f89611Schristos {
56464f89611Schristos 	const struct ar9380_eeprom *eep = sc->sc_eep;
56564f89611Schristos 	const uint8_t *spurchans;
56664f89611Schristos 	uint32_t reg;
56764f89611Schristos 	int idx, spur_delta_phase, spur_off, range, i;
56864f89611Schristos 	int freq, spur, spur_freq_sd, spur_subchannel_sd;
56964f89611Schristos 
57064f89611Schristos 	if (IEEE80211_IS_CHAN_2GHZ(c))
57164f89611Schristos 		spurchans = eep->modalHeader2G.spurChans;
57264f89611Schristos 	else
57364f89611Schristos 		spurchans = eep->modalHeader5G.spurChans;
57464f89611Schristos 	if (spurchans[0] == 0)
57564f89611Schristos 		return;
57664f89611Schristos 
57764f89611Schristos 	/* Disable OFDM spur mitigation. */
57864f89611Schristos 	AR_CLRBITS(sc, AR_PHY_TIMING4, AR_PHY_TIMING4_ENABLE_SPUR_FILTER);
57964f89611Schristos 
58064f89611Schristos 	reg = AR_READ(sc, AR_PHY_TIMING11);
58164f89611Schristos 	reg = RW(reg, AR_PHY_TIMING11_SPUR_FREQ_SD, 0);
58264f89611Schristos 	reg = RW(reg, AR_PHY_TIMING11_SPUR_DELTA_PHASE, 0);
58364f89611Schristos 	reg &= ~AR_PHY_TIMING11_USE_SPUR_FILTER_IN_AGC;
58464f89611Schristos 	reg &= ~AR_PHY_TIMING11_USE_SPUR_FILTER_IN_SELFCOR;
58564f89611Schristos 	AR_WRITE(sc, AR_PHY_TIMING11, reg);
58664f89611Schristos 
58764f89611Schristos 	AR_CLRBITS(sc, AR_PHY_SFCORR_EXT,
58864f89611Schristos 	    AR_PHY_SFCORR_EXT_SPUR_SUBCHANNEL_SD);
58964f89611Schristos 
59064f89611Schristos 	AR_CLRBITS(sc, AR_PHY_TIMING4, AR_PHY_TIMING4_ENABLE_SPUR_RSSI);
59164f89611Schristos 
59264f89611Schristos 	reg = AR_READ(sc, AR_PHY_SPUR_REG);
59364f89611Schristos 	reg = RW(reg, AR_PHY_SPUR_REG_MASK_RATE_CNTL, 0);
59464f89611Schristos 	reg &= ~AR_PHY_SPUR_REG_EN_VIT_SPUR_RSSI;
59564f89611Schristos 	reg &= ~AR_PHY_SPUR_REG_ENABLE_NF_RSSI_SPUR_MIT;
59664f89611Schristos 	reg &= ~AR_PHY_SPUR_REG_ENABLE_MASK_PPM;
59764f89611Schristos 	AR_WRITE(sc, AR_PHY_SPUR_REG, reg);
59864f89611Schristos 	AR_WRITE_BARRIER(sc);
59964f89611Schristos 
60064f89611Schristos 	freq = c->ic_freq;
60164f89611Schristos #ifndef IEEE80211_NO_HT
60264f89611Schristos 	if (extc != NULL) {
60364f89611Schristos 		range = 19;	/* +/- 19MHz range. */
60464f89611Schristos 		if (AR_READ(sc, AR_PHY_GEN_CTRL) & AR_PHY_GC_DYN2040_PRI_CH)
60564f89611Schristos 			freq += 10;
60664f89611Schristos 		else
60764f89611Schristos 			freq -= 10;
60864f89611Schristos 	}
60964f89611Schristos 	else
61064f89611Schristos #endif
61164f89611Schristos 		range = 10;	/* +/- 10MHz range. */
61264f89611Schristos 	for (i = 0; i < AR9380_EEPROM_MODAL_SPURS; i++) {
61364f89611Schristos 		spur = spurchans[i];
61464f89611Schristos 		if (spur == 0)
61564f89611Schristos 			return;
61664f89611Schristos 		/* Convert to frequency. */
61764f89611Schristos 		if (IEEE80211_IS_CHAN_2GHZ(c))
61864f89611Schristos 			spur = 2300 + spur;
61964f89611Schristos 		else
62064f89611Schristos 			spur = 4900 + (spur * 5);
62164f89611Schristos 		spur -= freq;
62264f89611Schristos 		if (abs(spur) < range)
62364f89611Schristos 			break;
62464f89611Schristos 	}
62564f89611Schristos 	if (i == AR9380_EEPROM_MODAL_SPURS)
62664f89611Schristos 		return;
62764f89611Schristos 
62864f89611Schristos 	/* Enable OFDM spur mitigation. */
62964f89611Schristos #ifndef IEEE80211_NO_HT
63064f89611Schristos 	if (extc != NULL) {
63164f89611Schristos 		spur_delta_phase = (spur * 131072) / 5;
63264f89611Schristos 		reg = AR_READ(sc, AR_PHY_GEN_CTRL);
63364f89611Schristos 		if (spur < 0) {
63464f89611Schristos 			spur_subchannel_sd =
63564f89611Schristos 			    (reg & AR_PHY_GC_DYN2040_PRI_CH) == 0;
63664f89611Schristos 			spur_off = spur + 10;
63764f89611Schristos 		}
63864f89611Schristos 		else {
63964f89611Schristos 			spur_subchannel_sd =
64064f89611Schristos 			    (reg & AR_PHY_GC_DYN2040_PRI_CH) != 0;
64164f89611Schristos 			spur_off = spur - 10;
64264f89611Schristos 		}
64364f89611Schristos 	}
64464f89611Schristos 	else
64564f89611Schristos #endif
64664f89611Schristos 	{
64764f89611Schristos 		spur_delta_phase = (spur * 262144) / 5;
64864f89611Schristos 		spur_subchannel_sd = 0;
64964f89611Schristos 		spur_off = spur;
65064f89611Schristos 	}
65164f89611Schristos 	spur_freq_sd = (spur_off * 512) / 11;
65264f89611Schristos 
65364f89611Schristos 	AR_SETBITS(sc, AR_PHY_TIMING4, AR_PHY_TIMING4_ENABLE_SPUR_FILTER);
65464f89611Schristos 
65564f89611Schristos 	reg = AR_READ(sc, AR_PHY_TIMING11);
65664f89611Schristos 	reg = RW(reg, AR_PHY_TIMING11_SPUR_FREQ_SD, spur_freq_sd);
65764f89611Schristos 	reg = RW(reg, AR_PHY_TIMING11_SPUR_DELTA_PHASE, spur_delta_phase);
65864f89611Schristos 	reg |= AR_PHY_TIMING11_USE_SPUR_FILTER_IN_AGC;
65964f89611Schristos 	reg |= AR_PHY_TIMING11_USE_SPUR_FILTER_IN_SELFCOR;
66064f89611Schristos 	AR_WRITE(sc, AR_PHY_TIMING11, reg);
66164f89611Schristos 
66264f89611Schristos 	reg = AR_READ(sc, AR_PHY_SFCORR_EXT);
66364f89611Schristos 	if (spur_subchannel_sd)
66464f89611Schristos 		reg |= AR_PHY_SFCORR_EXT_SPUR_SUBCHANNEL_SD;
66564f89611Schristos 	else
66664f89611Schristos 		reg &= ~AR_PHY_SFCORR_EXT_SPUR_SUBCHANNEL_SD;
66764f89611Schristos 	AR_WRITE(sc, AR_PHY_SFCORR_EXT, reg);
66864f89611Schristos 
66964f89611Schristos 	AR_SETBITS(sc, AR_PHY_TIMING4, AR_PHY_TIMING4_ENABLE_SPUR_RSSI);
67064f89611Schristos 
67164f89611Schristos 	reg = AR_READ(sc, AR_PHY_SPUR_REG);
67264f89611Schristos 	reg = RW(reg, AR_PHY_SPUR_REG_MASK_RATE_CNTL, 0xff);
67364f89611Schristos 	reg = RW(reg, AR_PHY_SPUR_REG_SPUR_RSSI_THRESH, 34);
67464f89611Schristos 	reg |= AR_PHY_SPUR_REG_EN_VIT_SPUR_RSSI;
67564f89611Schristos 	if (AR_READ(sc, AR_PHY_MODE) & AR_PHY_MODE_DYNAMIC)
67664f89611Schristos 		reg |= AR_PHY_SPUR_REG_ENABLE_NF_RSSI_SPUR_MIT;
67764f89611Schristos 	reg |= AR_PHY_SPUR_REG_ENABLE_MASK_PPM;
67864f89611Schristos 	AR_WRITE(sc, AR_PHY_SPUR_REG, reg);
67964f89611Schristos 
68064f89611Schristos 	idx = (spur * 16) / 5;
68164f89611Schristos 	if (idx < 0)
68264f89611Schristos 		idx--;
68364f89611Schristos 
68464f89611Schristos 	/* Write pilot mask. */
68564f89611Schristos 	AR_SETBITS(sc, AR_PHY_TIMING4,
68664f89611Schristos 	    AR_PHY_TIMING4_ENABLE_PILOT_MASK |
68764f89611Schristos 	    AR_PHY_TIMING4_ENABLE_CHAN_MASK);
68864f89611Schristos 
68964f89611Schristos 	reg = AR_READ(sc, AR_PHY_PILOT_SPUR_MASK);
69064f89611Schristos 	reg = RW(reg, AR_PHY_PILOT_SPUR_MASK_CF_PILOT_MASK_IDX_A, idx);
69164f89611Schristos 	reg = RW(reg, AR_PHY_PILOT_SPUR_MASK_CF_PILOT_MASK_A, 0x0c);
69264f89611Schristos 	AR_WRITE(sc, AR_PHY_PILOT_SPUR_MASK, reg);
69364f89611Schristos 
69464f89611Schristos 	reg = AR_READ(sc, AR_PHY_SPUR_MASK_A);
69564f89611Schristos 	reg = RW(reg, AR_PHY_SPUR_MASK_A_CF_PUNC_MASK_IDX_A, idx);
69664f89611Schristos 	reg = RW(reg, AR_PHY_SPUR_MASK_A_CF_PUNC_MASK_A, 0xa0);
69764f89611Schristos 	AR_WRITE(sc, AR_PHY_SPUR_MASK_A, reg);
69864f89611Schristos 
69964f89611Schristos 	reg = AR_READ(sc, AR_PHY_CHAN_SPUR_MASK);
70064f89611Schristos 	reg = RW(reg, AR_PHY_CHAN_SPUR_MASK_CF_CHAN_MASK_IDX_A, idx);
70164f89611Schristos 	reg = RW(reg, AR_PHY_CHAN_SPUR_MASK_CF_CHAN_MASK_A, 0x0c);
70264f89611Schristos 	AR_WRITE(sc, AR_PHY_CHAN_SPUR_MASK, reg);
70364f89611Schristos 	AR_WRITE_BARRIER(sc);
70464f89611Schristos }
70564f89611Schristos 
70664f89611Schristos Static void
ar9380_spur_mitigate(struct athn_softc * sc,struct ieee80211_channel * c,struct ieee80211_channel * extc)70764f89611Schristos ar9380_spur_mitigate(struct athn_softc *sc, struct ieee80211_channel *c,
70864f89611Schristos     struct ieee80211_channel *extc)
70964f89611Schristos {
71064f89611Schristos 
71164f89611Schristos 	/* NB: We call spur_mitigate_cck for 5GHz too, just to disable it. */
71264f89611Schristos 	ar9380_spur_mitigate_cck(sc, c, extc);
71364f89611Schristos 	ar9380_spur_mitigate_ofdm(sc, c, extc);
71464f89611Schristos }
71564f89611Schristos 
71664f89611Schristos Static void
ar9380_set_txpower(struct athn_softc * sc,struct ieee80211_channel * c,struct ieee80211_channel * extc)71764f89611Schristos ar9380_set_txpower(struct athn_softc *sc, struct ieee80211_channel *c,
71864f89611Schristos     struct ieee80211_channel *extc)
71964f89611Schristos {
72064f89611Schristos 	const struct ar9380_eeprom *eep = sc->sc_eep;
72164f89611Schristos 	uint8_t tpow_cck[4], tpow_ofdm[4];
72264f89611Schristos 	uint8_t tpow_ht20[14], tpow_ht40[14];
72364f89611Schristos 	int16_t power[ATHN_POWER_COUNT];
72464f89611Schristos 
72564f89611Schristos 	if (IEEE80211_IS_CHAN_2GHZ(c)) {
72664f89611Schristos 		/* Get CCK target powers. */
72764f89611Schristos 		ar9003_get_lg_tpow(sc, c, AR_CTL_11B,
72864f89611Schristos 		    eep->calTargetFbinCck, eep->calTargetPowerCck,
72964f89611Schristos 		    AR9380_NUM_2G_CCK_TARGET_POWERS, tpow_cck);
73064f89611Schristos 
73164f89611Schristos 		/* Get OFDM target powers. */
73264f89611Schristos 		ar9003_get_lg_tpow(sc, c, AR_CTL_11G,
73364f89611Schristos 		    eep->calTargetFbin2G, eep->calTargetPower2G,
73464f89611Schristos 		    AR9380_NUM_2G_20_TARGET_POWERS, tpow_ofdm);
73564f89611Schristos 
73664f89611Schristos 		/* Get HT-20 target powers. */
73764f89611Schristos 		ar9003_get_ht_tpow(sc, c, AR_CTL_2GHT20,
73864f89611Schristos 		    eep->calTargetFbin2GHT20, eep->calTargetPower2GHT20,
73964f89611Schristos 		    AR9380_NUM_2G_20_TARGET_POWERS, tpow_ht20);
74064f89611Schristos 
74164f89611Schristos 		if (extc != NULL) {
74264f89611Schristos 			/* Get HT-40 target powers. */
74364f89611Schristos 			ar9003_get_ht_tpow(sc, c, AR_CTL_2GHT40,
74464f89611Schristos 			    eep->calTargetFbin2GHT40,
74564f89611Schristos 			    eep->calTargetPower2GHT40,
74664f89611Schristos 			    AR9380_NUM_2G_40_TARGET_POWERS, tpow_ht40);
74764f89611Schristos 		}
74864f89611Schristos 	}
74964f89611Schristos 	else {
75064f89611Schristos 		/* Get OFDM target powers. */
75164f89611Schristos 		ar9003_get_lg_tpow(sc, c, AR_CTL_11A,
75264f89611Schristos 		    eep->calTargetFbin5G, eep->calTargetPower5G,
75364f89611Schristos 		    AR9380_NUM_5G_20_TARGET_POWERS, tpow_ofdm);
75464f89611Schristos 
75564f89611Schristos 		/* Get HT-20 target powers. */
75664f89611Schristos 		ar9003_get_ht_tpow(sc, c, AR_CTL_5GHT20,
75764f89611Schristos 		    eep->calTargetFbin5GHT20, eep->calTargetPower5GHT20,
75864f89611Schristos 		    AR9380_NUM_5G_20_TARGET_POWERS, tpow_ht20);
75964f89611Schristos 
76064f89611Schristos 		if (extc != NULL) {
76164f89611Schristos 			/* Get HT-40 target powers. */
76264f89611Schristos 			ar9003_get_ht_tpow(sc, c, AR_CTL_5GHT40,
76364f89611Schristos 			    eep->calTargetFbin5GHT40,
76464f89611Schristos 			    eep->calTargetPower5GHT40,
76564f89611Schristos 			    AR9380_NUM_5G_40_TARGET_POWERS, tpow_ht40);
76664f89611Schristos 		}
76764f89611Schristos 	}
76864f89611Schristos 
76964f89611Schristos 	memset(power, 0, sizeof(power));
77064f89611Schristos 	/* Shuffle target powers accross transmit rates. */
77164f89611Schristos 	power[ATHN_POWER_OFDM6 ] =
77264f89611Schristos 	power[ATHN_POWER_OFDM9 ] =
77364f89611Schristos 	power[ATHN_POWER_OFDM12] =
77464f89611Schristos 	power[ATHN_POWER_OFDM18] =
77564f89611Schristos 	power[ATHN_POWER_OFDM24] = tpow_ofdm[0];
77664f89611Schristos 	power[ATHN_POWER_OFDM36] = tpow_ofdm[1];
77764f89611Schristos 	power[ATHN_POWER_OFDM48] = tpow_ofdm[2];
77864f89611Schristos 	power[ATHN_POWER_OFDM54] = tpow_ofdm[3];
77964f89611Schristos 	if (IEEE80211_IS_CHAN_2GHZ(c)) {
78064f89611Schristos 		power[ATHN_POWER_CCK1_LP ] =
78164f89611Schristos 		power[ATHN_POWER_CCK2_LP ] =
78264f89611Schristos 		power[ATHN_POWER_CCK2_SP ] =
78364f89611Schristos 		power[ATHN_POWER_CCK55_LP] = tpow_cck[0];
78464f89611Schristos 		power[ATHN_POWER_CCK55_SP] = tpow_cck[1];
78564f89611Schristos 		power[ATHN_POWER_CCK11_LP] = tpow_cck[2];
78664f89611Schristos 		power[ATHN_POWER_CCK11_SP] = tpow_cck[3];
78764f89611Schristos 	}
78864f89611Schristos 	/* Next entry covers MCS0, MCS8 and MCS16. */
78964f89611Schristos 	power[ATHN_POWER_HT20( 0)] = tpow_ht20[ 0];
79064f89611Schristos 	/* Next entry covers MCS1-3, MCS9-11 and MCS17-19. */
79164f89611Schristos 	power[ATHN_POWER_HT20( 1)] = tpow_ht20[ 1];
79264f89611Schristos 	power[ATHN_POWER_HT20( 4)] = tpow_ht20[ 2];
79364f89611Schristos 	power[ATHN_POWER_HT20( 5)] = tpow_ht20[ 3];
79464f89611Schristos 	power[ATHN_POWER_HT20( 6)] = tpow_ht20[ 4];
79564f89611Schristos 	power[ATHN_POWER_HT20( 7)] = tpow_ht20[ 5];
79664f89611Schristos 	power[ATHN_POWER_HT20(12)] = tpow_ht20[ 6];
79764f89611Schristos 	power[ATHN_POWER_HT20(13)] = tpow_ht20[ 7];
79864f89611Schristos 	power[ATHN_POWER_HT20(14)] = tpow_ht20[ 8];
79964f89611Schristos 	power[ATHN_POWER_HT20(15)] = tpow_ht20[ 9];
80064f89611Schristos 	power[ATHN_POWER_HT20(20)] = tpow_ht20[10];
80164f89611Schristos 	power[ATHN_POWER_HT20(21)] = tpow_ht20[11];
80264f89611Schristos 	power[ATHN_POWER_HT20(22)] = tpow_ht20[12];
80364f89611Schristos 	power[ATHN_POWER_HT20(23)] = tpow_ht20[13];
80464f89611Schristos 	if (extc != NULL) {
80564f89611Schristos 		/* Next entry covers MCS0, MCS8 and MCS16. */
80664f89611Schristos 		power[ATHN_POWER_HT40( 0)] = tpow_ht40[ 0];
80764f89611Schristos 		/* Next entry covers MCS1-3, MCS9-11 and MCS17-19. */
80864f89611Schristos 		power[ATHN_POWER_HT40( 1)] = tpow_ht40[ 1];
80964f89611Schristos 		power[ATHN_POWER_HT40( 4)] = tpow_ht40[ 2];
81064f89611Schristos 		power[ATHN_POWER_HT40( 5)] = tpow_ht40[ 3];
81164f89611Schristos 		power[ATHN_POWER_HT40( 6)] = tpow_ht40[ 4];
81264f89611Schristos 		power[ATHN_POWER_HT40( 7)] = tpow_ht40[ 5];
81364f89611Schristos 		power[ATHN_POWER_HT40(12)] = tpow_ht40[ 6];
81464f89611Schristos 		power[ATHN_POWER_HT40(13)] = tpow_ht40[ 7];
81564f89611Schristos 		power[ATHN_POWER_HT40(14)] = tpow_ht40[ 8];
81664f89611Schristos 		power[ATHN_POWER_HT40(15)] = tpow_ht40[ 9];
81764f89611Schristos 		power[ATHN_POWER_HT40(20)] = tpow_ht40[10];
81864f89611Schristos 		power[ATHN_POWER_HT40(21)] = tpow_ht40[11];
81964f89611Schristos 		power[ATHN_POWER_HT40(22)] = tpow_ht40[12];
82064f89611Schristos 		power[ATHN_POWER_HT40(23)] = tpow_ht40[13];
82164f89611Schristos 	}
82264f89611Schristos 
82364f89611Schristos 	/* Write transmit power values to hardware. */
82464f89611Schristos 	ar9003_write_txpower(sc, power);
82564f89611Schristos 
82664f89611Schristos 	/* Apply transmit power correction. */
82764f89611Schristos 	ar9380_set_correction(sc, c);
82864f89611Schristos }
82964f89611Schristos 
83064f89611Schristos Static void
ar9380_get_correction(struct athn_softc * sc,struct ieee80211_channel * c,int chain,int * corr,int * temp)83164f89611Schristos ar9380_get_correction(struct athn_softc *sc, struct ieee80211_channel *c,
83264f89611Schristos     int chain, int *corr, int *temp)
83364f89611Schristos {
83464f89611Schristos 	const struct ar9380_eeprom *eep = sc->sc_eep;
83564f89611Schristos 	const struct ar9380_cal_data_per_freq_op_loop *pierdata;
83664f89611Schristos 	const uint8_t *pierfreq;
83764f89611Schristos 	uint8_t fbin;
83864f89611Schristos 	int lo, hi, npiers;
83964f89611Schristos 
84064f89611Schristos 	if (IEEE80211_IS_CHAN_2GHZ(c)) {
84164f89611Schristos 		pierfreq = eep->calFreqPier2G;
84264f89611Schristos 		pierdata = eep->calPierData2G[chain];
84364f89611Schristos 		npiers = AR9380_NUM_2G_CAL_PIERS;
84464f89611Schristos 	}
84564f89611Schristos 	else {
84664f89611Schristos 		pierfreq = eep->calFreqPier5G;
84764f89611Schristos 		pierdata = eep->calPierData5G[chain];
84864f89611Schristos 		npiers = AR9380_NUM_5G_CAL_PIERS;
84964f89611Schristos 	}
85064f89611Schristos 	/* Find channel in ROM pier table. */
85164f89611Schristos 	fbin = athn_chan2fbin(c);
85264f89611Schristos 	athn_get_pier_ival(fbin, pierfreq, npiers, &lo, &hi);
85364f89611Schristos 
85464f89611Schristos 	*corr = athn_interpolate(fbin,
85564f89611Schristos 	    pierfreq[lo], pierdata[lo].refPower,
85664f89611Schristos 	    pierfreq[hi], pierdata[hi].refPower);
85764f89611Schristos 	*temp = athn_interpolate(fbin,
85864f89611Schristos 	    pierfreq[lo], pierdata[lo].tempMeas,
85964f89611Schristos 	    pierfreq[hi], pierdata[hi].tempMeas);
86064f89611Schristos }
86164f89611Schristos 
86264f89611Schristos Static void
ar9380_set_correction(struct athn_softc * sc,struct ieee80211_channel * c)86364f89611Schristos ar9380_set_correction(struct athn_softc *sc, struct ieee80211_channel *c)
86464f89611Schristos {
86564f89611Schristos 	const struct ar9380_eeprom *eep = sc->sc_eep;
86664f89611Schristos 	const struct ar9380_modal_eep_header *modal;
86764f89611Schristos 	uint32_t reg;
86864f89611Schristos 	int8_t slope;
86964f89611Schristos 	int i, corr, temp, temp0;
87064f89611Schristos 
87164f89611Schristos 	if (IEEE80211_IS_CHAN_2GHZ(c))
87264f89611Schristos 		modal = &eep->modalHeader2G;
87364f89611Schristos 	else
87464f89611Schristos 		modal = &eep->modalHeader5G;
87564f89611Schristos 
87664f89611Schristos 	temp0 = 0;	/* XXX: gcc */
87764f89611Schristos 	for (i = 0; i < AR9380_MAX_CHAINS; i++) {
87864f89611Schristos 		ar9380_get_correction(sc, c, i, &corr, &temp);
87964f89611Schristos 		if (i == 0)
88064f89611Schristos 			temp0 = temp;
88164f89611Schristos 
88264f89611Schristos 		reg = AR_READ(sc, AR_PHY_TPC_11_B(i));
88364f89611Schristos 		reg = RW(reg, AR_PHY_TPC_11_OLPC_GAIN_DELTA, corr);
88464f89611Schristos 		AR_WRITE(sc, AR_PHY_TPC_11_B(i), reg);
88564f89611Schristos 
88664f89611Schristos 		/* Enable open loop power control. */
88764f89611Schristos 		reg = AR_READ(sc, AR_PHY_TPC_6_B(i));
88864f89611Schristos 		reg = RW(reg, AR_PHY_TPC_6_ERROR_EST_MODE, 3);
88964f89611Schristos 		AR_WRITE(sc, AR_PHY_TPC_6_B(i), reg);
89064f89611Schristos 	}
89164f89611Schristos 
89264f89611Schristos 	/* Enable temperature compensation. */
89364f89611Schristos 	if (IEEE80211_IS_CHAN_5GHZ(c) &&
89464f89611Schristos 	    eep->base_ext2.tempSlopeLow != 0) {
89564f89611Schristos 		if (c->ic_freq <= 5500) {
89664f89611Schristos 			slope = athn_interpolate(c->ic_freq,
89764f89611Schristos 			    5180, eep->base_ext2.tempSlopeLow,
89864f89611Schristos 			    5500, modal->tempSlope);
89964f89611Schristos 		}
90064f89611Schristos 		else {
90164f89611Schristos 			slope = athn_interpolate(c->ic_freq,
90264f89611Schristos 			    5500, modal->tempSlope,
90364f89611Schristos 			    5785, eep->base_ext2.tempSlopeHigh);
90464f89611Schristos 		}
90564f89611Schristos 	}
90664f89611Schristos 	else
90764f89611Schristos 		slope = modal->tempSlope;
90864f89611Schristos 
90964f89611Schristos 	reg = AR_READ(sc, AR_PHY_TPC_19);
91064f89611Schristos 	reg = RW(reg, AR_PHY_TPC_19_ALPHA_THERM, slope);
91164f89611Schristos 	AR_WRITE(sc, AR_PHY_TPC_19, reg);
91264f89611Schristos 
91364f89611Schristos 	reg = AR_READ(sc, AR_PHY_TPC_18);
91464f89611Schristos 	reg = RW(reg, AR_PHY_TPC_18_THERM_CAL, temp0);
91564f89611Schristos 	AR_WRITE(sc, AR_PHY_TPC_18, reg);
91664f89611Schristos 	AR_WRITE_BARRIER(sc);
91764f89611Schristos }
918