xref: /netbsd-src/sys/dev/ic/arn5416.c (revision 13d4bb4cc874de96add7fc4227d38a1d656b03d1)
1*13d4bb4cSthorpej /*	$NetBSD: arn5416.c,v 1.3 2022/09/25 18:43:32 thorpej Exp $	*/
264f89611Schristos /*	$OpenBSD: ar5416.c,v 1.12 2012/06/10 21:23:36 kettenis Exp $	*/
364f89611Schristos 
464f89611Schristos /*-
564f89611Schristos  * Copyright (c) 2009 Damien Bergamini <damien.bergamini@free.fr>
664f89611Schristos  * Copyright (c) 2008-2009 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 AR5416, AR5418 and AR9160 chipsets.
2464f89611Schristos  */
2564f89611Schristos 
2664f89611Schristos #include <sys/cdefs.h>
27*13d4bb4cSthorpej __KERNEL_RCSID(0, "$NetBSD: arn5416.c,v 1.3 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/callout.h>
3764f89611Schristos #include <sys/conf.h>
3864f89611Schristos #include <sys/device.h>
3964f89611Schristos 
4064f89611Schristos #include <sys/bus.h>
4164f89611Schristos #include <sys/endian.h>
4264f89611Schristos #include <sys/intr.h>
4364f89611Schristos 
4464f89611Schristos #include <net/bpf.h>
4564f89611Schristos #include <net/if.h>
4664f89611Schristos #include <net/if_arp.h>
4764f89611Schristos #include <net/if_dl.h>
4802421171Schristos #include <net/if_ether.h>
4964f89611Schristos #include <net/if_media.h>
5064f89611Schristos #include <net/if_types.h>
5164f89611Schristos 
5264f89611Schristos #include <netinet/in.h>
5364f89611Schristos #include <netinet/in_systm.h>
5464f89611Schristos #include <netinet/in_var.h>
5564f89611Schristos #include <netinet/ip.h>
5664f89611Schristos 
5764f89611Schristos #include <net80211/ieee80211_var.h>
5864f89611Schristos #include <net80211/ieee80211_amrr.h>
5964f89611Schristos #include <net80211/ieee80211_radiotap.h>
6064f89611Schristos 
6164f89611Schristos #include <dev/ic/athnreg.h>
6264f89611Schristos #include <dev/ic/athnvar.h>
6364f89611Schristos 
6464f89611Schristos #include <dev/ic/arn5008reg.h>
6564f89611Schristos #include <dev/ic/arn5008.h>
6664f89611Schristos #include <dev/ic/arn5416reg.h>
6764f89611Schristos #include <dev/ic/arn5416.h>
6864f89611Schristos #include <dev/ic/arn9280.h>
6964f89611Schristos 
7064f89611Schristos #define Static static
7164f89611Schristos 
7264f89611Schristos Static void	ar5416_force_bias(struct athn_softc *,
7364f89611Schristos 		    struct ieee80211_channel *);
7464f89611Schristos Static void	ar5416_get_pdadcs(struct athn_softc *,
7564f89611Schristos 		    struct ieee80211_channel *, int, int, uint8_t, uint8_t *,
7664f89611Schristos 		    uint8_t *);
7764f89611Schristos Static void	ar5416_init_from_rom(struct athn_softc *,
7864f89611Schristos 		    struct ieee80211_channel *, struct ieee80211_channel *);
7964f89611Schristos Static uint8_t	ar5416_reverse_bits(uint8_t, int);
8064f89611Schristos Static void	ar5416_rw_bank6tpc(struct athn_softc *,
8164f89611Schristos 		    struct ieee80211_channel *, uint32_t *);
8264f89611Schristos Static void	ar5416_rw_rfbits(uint32_t *, int, int, uint32_t, int);
8364f89611Schristos Static void	ar5416_set_power_calib(struct athn_softc *,
8464f89611Schristos 		    struct ieee80211_channel *);
8564f89611Schristos Static int	ar5416_set_synth(struct athn_softc *,
8664f89611Schristos 		    struct ieee80211_channel *, struct ieee80211_channel *);
8764f89611Schristos Static void	ar5416_setup(struct athn_softc *);
8864f89611Schristos Static void	ar5416_spur_mitigate(struct athn_softc *,
8964f89611Schristos 		    struct ieee80211_channel *, struct ieee80211_channel *);
9064f89611Schristos Static void	ar9160_rw_addac(struct athn_softc *,
9164f89611Schristos 		    struct ieee80211_channel *, uint32_t *);
9264f89611Schristos 
9364f89611Schristos PUBLIC int
ar5416_attach(struct athn_softc * sc)9464f89611Schristos ar5416_attach(struct athn_softc *sc)
9564f89611Schristos {
9664f89611Schristos 	sc->sc_eep_base = AR5416_EEP_START_LOC;
9764f89611Schristos 	sc->sc_eep_size = sizeof(struct ar5416_eeprom);
9864f89611Schristos 	sc->sc_def_nf = AR5416_PHY_CCA_MAX_GOOD_VALUE;
9964f89611Schristos 	sc->sc_ngpiopins = 14;
10064f89611Schristos 	sc->sc_led_pin = 1;
10164f89611Schristos 	sc->sc_workaround = AR5416_WA_DEFAULT;
10264f89611Schristos 	sc->sc_ops.setup = ar5416_setup;
10364f89611Schristos 	sc->sc_ops.swap_rom = ar5416_swap_rom;
10464f89611Schristos 	sc->sc_ops.init_from_rom = ar5416_init_from_rom;
10564f89611Schristos 	sc->sc_ops.set_txpower = ar5416_set_txpower;
10664f89611Schristos 	sc->sc_ops.set_synth = ar5416_set_synth;
10764f89611Schristos 	sc->sc_ops.spur_mitigate = ar5416_spur_mitigate;
10864f89611Schristos 	sc->sc_ops.get_spur_chans = ar5416_get_spur_chans;
10964f89611Schristos 	if (AR_SREV_9160_10_OR_LATER(sc))
11064f89611Schristos 		sc->sc_ini = &ar9160_ini;
11164f89611Schristos 	else
11264f89611Schristos 		sc->sc_ini = &ar5416_ini;
11364f89611Schristos 	sc->sc_serdes = &ar5416_serdes;
11464f89611Schristos 
11564f89611Schristos 	return ar5008_attach(sc);
11664f89611Schristos }
11764f89611Schristos 
11864f89611Schristos Static void
ar5416_setup(struct athn_softc * sc)11964f89611Schristos ar5416_setup(struct athn_softc *sc)
12064f89611Schristos {
12164f89611Schristos 	/* Select ADDAC programming. */
12264f89611Schristos 	if (AR_SREV_9160_11(sc))
12364f89611Schristos 		sc->sc_addac = &ar9160_1_1_addac;
12464f89611Schristos 	else if (AR_SREV_9160_10_OR_LATER(sc))
12564f89611Schristos 		sc->sc_addac = &ar9160_1_0_addac;
12664f89611Schristos 	else if (AR_SREV_5416_22_OR_LATER(sc))
12764f89611Schristos 		sc->sc_addac = &ar5416_2_2_addac;
12864f89611Schristos 	else
12964f89611Schristos 		sc->sc_addac = &ar5416_2_1_addac;
13064f89611Schristos }
13164f89611Schristos 
13264f89611Schristos PUBLIC void
ar5416_swap_rom(struct athn_softc * sc)13364f89611Schristos ar5416_swap_rom(struct athn_softc *sc)
13464f89611Schristos {
13564f89611Schristos 	struct ar5416_eeprom *eep = sc->sc_eep;
13664f89611Schristos 	struct ar5416_modal_eep_header *modal;
13764f89611Schristos 	int i, j;
13864f89611Schristos 
13964f89611Schristos 	for (i = 0; i < 2; i++) {	/* Dual-band. */
14064f89611Schristos 		modal = &eep->modalHeader[i];
14164f89611Schristos 
14264f89611Schristos 		modal->antCtrlCommon = bswap32(modal->antCtrlCommon);
14364f89611Schristos 		for (j = 0; j < AR5416_MAX_CHAINS; j++) {
14464f89611Schristos 			modal->antCtrlChain[j] =
14564f89611Schristos 			    bswap32(modal->antCtrlChain[j]);
14664f89611Schristos 		}
14764f89611Schristos 		for (j = 0; j < AR_EEPROM_MODAL_SPURS; j++) {
14864f89611Schristos 			modal->spurChans[j].spurChan =
14964f89611Schristos 			    bswap16(modal->spurChans[j].spurChan);
15064f89611Schristos 		}
15164f89611Schristos 	}
15264f89611Schristos }
15364f89611Schristos 
15464f89611Schristos PUBLIC const struct ar_spur_chan *
ar5416_get_spur_chans(struct athn_softc * sc,int is2ghz)15564f89611Schristos ar5416_get_spur_chans(struct athn_softc *sc, int is2ghz)
15664f89611Schristos {
15764f89611Schristos 	const struct ar5416_eeprom *eep = sc->sc_eep;
15864f89611Schristos 
15964f89611Schristos 	return eep->modalHeader[is2ghz].spurChans;
16064f89611Schristos }
16164f89611Schristos 
16264f89611Schristos Static int
ar5416_set_synth(struct athn_softc * sc,struct ieee80211_channel * c,struct ieee80211_channel * extc)16364f89611Schristos ar5416_set_synth(struct athn_softc *sc, struct ieee80211_channel *c,
16464f89611Schristos     struct ieee80211_channel *extc)
16564f89611Schristos {
16664f89611Schristos 	uint32_t phy, reg;
16764f89611Schristos 	uint32_t freq = c->ic_freq;
16864f89611Schristos 	uint8_t chansel;
16964f89611Schristos 
17064f89611Schristos 	phy = 0;
17164f89611Schristos 	if (IEEE80211_IS_CHAN_2GHZ(c)) {
17264f89611Schristos 		if (((freq - 2192) % 5) == 0) {
17364f89611Schristos 			chansel = ((freq - 672) * 2 - 3040) / 10;
17464f89611Schristos 		}
17564f89611Schristos 		else if (((freq - 2224) % 5) == 0) {
17664f89611Schristos 			chansel = ((freq - 704) * 2 - 3040) / 10;
17764f89611Schristos 			phy |= AR5416_BMODE_SYNTH;
17864f89611Schristos 		}
17964f89611Schristos 		else
18064f89611Schristos 			return EINVAL;
18164f89611Schristos 		chansel <<= 2;
18264f89611Schristos 
18364f89611Schristos 		reg = AR_READ(sc, AR_PHY_CCK_TX_CTRL);
18464f89611Schristos 		if (freq == 2484)	/* Channel 14. */
18564f89611Schristos 			reg |= AR_PHY_CCK_TX_CTRL_JAPAN;
18664f89611Schristos 		else
18764f89611Schristos 			reg &= ~AR_PHY_CCK_TX_CTRL_JAPAN;
18864f89611Schristos 		AR_WRITE(sc, AR_PHY_CCK_TX_CTRL, reg);
18964f89611Schristos 
19064f89611Schristos 		/* Fix for orientation sensitivity issue. */
19164f89611Schristos 		if (AR_SREV_5416(sc))
19264f89611Schristos 			ar5416_force_bias(sc, c);
19364f89611Schristos 	}
19464f89611Schristos 	else {
19564f89611Schristos 		if (freq >= 5120 && (freq % 20) == 0) {
19664f89611Schristos 			chansel = (freq - 4800) / 20;
19764f89611Schristos 			chansel <<= 2;
19864f89611Schristos 			phy |= SM(AR5416_AMODE_REFSEL, 2);
19964f89611Schristos 		}
20064f89611Schristos 		else if ((freq % 10) == 0) {
20164f89611Schristos 			chansel = (freq - 4800) / 10;
20264f89611Schristos 			chansel <<= 1;
20364f89611Schristos 			if (AR_SREV_9160_10_OR_LATER(sc))
20464f89611Schristos 				phy |= SM(AR5416_AMODE_REFSEL, 1);
20564f89611Schristos 			else
20664f89611Schristos 				phy |= SM(AR5416_AMODE_REFSEL, 2);
20764f89611Schristos 		}
20864f89611Schristos 		else if ((freq % 5) == 0) {
20964f89611Schristos 			chansel = (freq - 4800) / 5;
21064f89611Schristos 			phy |= SM(AR5416_AMODE_REFSEL, 2);
21164f89611Schristos 		}
21264f89611Schristos 		else
21364f89611Schristos 			return EINVAL;
21464f89611Schristos 	}
21564f89611Schristos 	chansel = ar5416_reverse_bits(chansel, 8);
21664f89611Schristos 	phy |= chansel << 8 | 1 << 5 | 1;
21764f89611Schristos 	DPRINTFN(DBG_RF, sc, "AR_PHY(0x37)=0x%08x\n", phy);
21864f89611Schristos 	AR_WRITE(sc, AR_PHY(0x37), phy);
21964f89611Schristos 	return 0;
22064f89611Schristos }
22164f89611Schristos 
22264f89611Schristos Static void
ar5416_init_from_rom(struct athn_softc * sc,struct ieee80211_channel * c,struct ieee80211_channel * extc)22364f89611Schristos ar5416_init_from_rom(struct athn_softc *sc, struct ieee80211_channel *c,
22464f89611Schristos     struct ieee80211_channel *extc)
22564f89611Schristos {
22664f89611Schristos 	static const uint32_t chainoffset[] = { 0x0000, 0x2000, 0x1000 };
22764f89611Schristos 	const struct ar5416_eeprom *eep = sc->sc_eep;
22864f89611Schristos 	const struct ar5416_modal_eep_header *modal;
22964f89611Schristos 	uint32_t reg, offset;
23064f89611Schristos 	uint8_t txRxAtten;
23164f89611Schristos 	int i;
23264f89611Schristos 
23364f89611Schristos 	modal = &eep->modalHeader[IEEE80211_IS_CHAN_2GHZ(c)];
23464f89611Schristos 
23564f89611Schristos 	AR_WRITE(sc, AR_PHY_SWITCH_COM, modal->antCtrlCommon);
23664f89611Schristos 
23764f89611Schristos 	for (i = 0; i < AR5416_MAX_CHAINS; i++) {
23864f89611Schristos 		if (AR_SREV_5416_20_OR_LATER(sc) &&
23964f89611Schristos 		    (sc->sc_rxchainmask == 0x5 || sc->sc_txchainmask == 0x5))
24064f89611Schristos 			offset = chainoffset[i];
24164f89611Schristos 		else
24264f89611Schristos 			offset = i * 0x1000;
24364f89611Schristos 
24464f89611Schristos 		AR_WRITE(sc, AR_PHY_SWITCH_CHAIN_0 + offset,
24564f89611Schristos 		    modal->antCtrlChain[i]);
24664f89611Schristos 
24764f89611Schristos 		reg = AR_READ(sc, AR_PHY_TIMING_CTRL4_0 + offset);
24864f89611Schristos 		reg = RW(reg, AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF,
24964f89611Schristos 		    modal->iqCalICh[i]);
25064f89611Schristos 		reg = RW(reg, AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF,
25164f89611Schristos 		    modal->iqCalQCh[i]);
25264f89611Schristos 		AR_WRITE(sc, AR_PHY_TIMING_CTRL4_0 + offset, reg);
25364f89611Schristos 
25464f89611Schristos 		if (i > 0 && !AR_SREV_5416_20_OR_LATER(sc))
25564f89611Schristos 			continue;
25664f89611Schristos 
25764f89611Schristos 		if (sc->sc_eep_rev >= AR_EEP_MINOR_VER_3) {
25864f89611Schristos 			reg = AR_READ(sc, AR_PHY_GAIN_2GHZ + offset);
25964f89611Schristos 			reg = RW(reg, AR_PHY_GAIN_2GHZ_BSW_MARGIN,
26064f89611Schristos 			    modal->bswMargin[i]);
26164f89611Schristos 			reg = RW(reg, AR_PHY_GAIN_2GHZ_BSW_ATTEN,
26264f89611Schristos 			    modal->bswAtten[i]);
26364f89611Schristos 			AR_WRITE(sc, AR_PHY_GAIN_2GHZ + offset, reg);
26464f89611Schristos 		}
26564f89611Schristos 		if (sc->sc_eep_rev >= AR_EEP_MINOR_VER_3)
26664f89611Schristos 			txRxAtten = modal->txRxAttenCh[i];
26764f89611Schristos 		else	/* Workaround for ROM versions < 14.3. */
26864f89611Schristos 			txRxAtten = IEEE80211_IS_CHAN_2GHZ(c) ? 23 : 44;
26964f89611Schristos 		reg = AR_READ(sc, AR_PHY_RXGAIN + offset);
27064f89611Schristos 		reg = RW(reg, AR_PHY_RXGAIN_TXRX_ATTEN, txRxAtten);
27164f89611Schristos 		AR_WRITE(sc, AR_PHY_RXGAIN + offset, reg);
27264f89611Schristos 
27364f89611Schristos 		reg = AR_READ(sc, AR_PHY_GAIN_2GHZ + offset);
27464f89611Schristos 		reg = RW(reg, AR_PHY_GAIN_2GHZ_RXTX_MARGIN,
27564f89611Schristos 		    modal->rxTxMarginCh[i]);
27664f89611Schristos 		AR_WRITE(sc, AR_PHY_GAIN_2GHZ + offset, reg);
27764f89611Schristos 	}
27864f89611Schristos 	reg = AR_READ(sc, AR_PHY_SETTLING);
27964f89611Schristos 	reg = RW(reg, AR_PHY_SETTLING_SWITCH, modal->switchSettling);
28064f89611Schristos 	AR_WRITE(sc, AR_PHY_SETTLING, reg);
28164f89611Schristos 
28264f89611Schristos 	reg = AR_READ(sc, AR_PHY_DESIRED_SZ);
28364f89611Schristos 	reg = RW(reg, AR_PHY_DESIRED_SZ_ADC, modal->adcDesiredSize);
28464f89611Schristos 	reg = RW(reg, AR_PHY_DESIRED_SZ_PGA, modal->pgaDesiredSize);
28564f89611Schristos 	AR_WRITE(sc, AR_PHY_DESIRED_SZ, reg);
28664f89611Schristos 
28764f89611Schristos 	reg =  SM(AR_PHY_RF_CTL4_TX_END_XPAA_OFF, modal->txEndToXpaOff);
28864f89611Schristos 	reg |= SM(AR_PHY_RF_CTL4_TX_END_XPAB_OFF, modal->txEndToXpaOff);
28964f89611Schristos 	reg |= SM(AR_PHY_RF_CTL4_FRAME_XPAA_ON, modal->txFrameToXpaOn);
29064f89611Schristos 	reg |= SM(AR_PHY_RF_CTL4_FRAME_XPAB_ON, modal->txFrameToXpaOn);
29164f89611Schristos 	AR_WRITE(sc, AR_PHY_RF_CTL4, reg);
29264f89611Schristos 
29364f89611Schristos 	reg = AR_READ(sc, AR_PHY_RF_CTL3);
29464f89611Schristos 	reg = RW(reg, AR_PHY_TX_END_TO_A2_RX_ON, modal->txEndToRxOn);
29564f89611Schristos 	AR_WRITE(sc, AR_PHY_RF_CTL3, reg);
29664f89611Schristos 
29764f89611Schristos 	reg = AR_READ(sc, AR_PHY_CCA(0));
29864f89611Schristos 	reg = RW(reg, AR_PHY_CCA_THRESH62, modal->thresh62);
29964f89611Schristos 	AR_WRITE(sc, AR_PHY_CCA(0), reg);
30064f89611Schristos 
30164f89611Schristos 	reg = AR_READ(sc, AR_PHY_EXT_CCA(0));
30264f89611Schristos 	reg = RW(reg, AR_PHY_EXT_CCA_THRESH62, modal->thresh62);
30364f89611Schristos 	AR_WRITE(sc, AR_PHY_EXT_CCA(0), reg);
30464f89611Schristos 
30564f89611Schristos 	if (sc->sc_eep_rev >= AR_EEP_MINOR_VER_2) {
30664f89611Schristos 		reg = AR_READ(sc, AR_PHY_RF_CTL2);
30764f89611Schristos 		reg = RW(reg, AR_PHY_TX_END_DATA_START,
30864f89611Schristos 		    modal->txFrameToDataStart);
30964f89611Schristos 		reg = RW(reg, AR_PHY_TX_END_PA_ON, modal->txFrameToPaOn);
31064f89611Schristos 		AR_WRITE(sc, AR_PHY_RF_CTL2, reg);
31164f89611Schristos 	}
31264f89611Schristos #ifndef IEEE80211_NO_HT
31364f89611Schristos 	if (sc->sc_eep_rev >= AR_EEP_MINOR_VER_3 && extc != NULL) {
31464f89611Schristos 		/* Overwrite switch settling with HT-40 value. */
31564f89611Schristos 		reg = AR_READ(sc, AR_PHY_SETTLING);
31664f89611Schristos 		reg = RW(reg, AR_PHY_SETTLING_SWITCH, modal->swSettleHt40);
31764f89611Schristos 		AR_WRITE(sc, AR_PHY_SETTLING, reg);
31864f89611Schristos 	}
31964f89611Schristos #endif
32064f89611Schristos }
32164f89611Schristos 
32264f89611Schristos PUBLIC int
ar5416_init_calib(struct athn_softc * sc,struct ieee80211_channel * c,struct ieee80211_channel * extc)32364f89611Schristos ar5416_init_calib(struct athn_softc *sc, struct ieee80211_channel *c,
32464f89611Schristos     struct ieee80211_channel *extc)
32564f89611Schristos {
32664f89611Schristos 	int ntries;
32764f89611Schristos 
32864f89611Schristos 	if (AR_SREV_9280_10_OR_LATER(sc)) {
32964f89611Schristos 		/* XXX Linux tests AR9287?! */
33064f89611Schristos 		AR_CLRBITS(sc, AR_PHY_ADC_CTL, AR_PHY_ADC_CTL_OFF_PWDADC);
33164f89611Schristos 		AR_SETBITS(sc, AR_PHY_AGC_CONTROL,
33264f89611Schristos 		    AR_PHY_AGC_CONTROL_FLTR_CAL);
33364f89611Schristos 	}
33464f89611Schristos 	/* Calibrate the AGC. */
33564f89611Schristos 	AR_SETBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL);
33664f89611Schristos 	/* Poll for offset calibration completion. */
33764f89611Schristos 	for (ntries = 0; ntries < 10000; ntries++) {
33864f89611Schristos 		if (!(AR_READ(sc, AR_PHY_AGC_CONTROL) &
33964f89611Schristos 		    AR_PHY_AGC_CONTROL_CAL))
34064f89611Schristos 			break;
34164f89611Schristos 		DELAY(10);
34264f89611Schristos 	}
34364f89611Schristos 	if (ntries == 10000)
34464f89611Schristos 		return ETIMEDOUT;
34564f89611Schristos 	if (AR_SREV_9280_10_OR_LATER(sc)) {
34664f89611Schristos 		AR_SETBITS(sc, AR_PHY_ADC_CTL, AR_PHY_ADC_CTL_OFF_PWDADC);
34764f89611Schristos 		AR_CLRBITS(sc, AR_PHY_AGC_CONTROL,
34864f89611Schristos 		    AR_PHY_AGC_CONTROL_FLTR_CAL);
34964f89611Schristos 	}
35064f89611Schristos 	return 0;
35164f89611Schristos }
35264f89611Schristos 
35364f89611Schristos Static void
ar5416_get_pdadcs(struct athn_softc * sc,struct ieee80211_channel * c,int chain,int nxpdgains,uint8_t overlap,uint8_t * boundaries,uint8_t * pdadcs)35464f89611Schristos ar5416_get_pdadcs(struct athn_softc *sc, struct ieee80211_channel *c,
35564f89611Schristos     int chain, int nxpdgains, uint8_t overlap, uint8_t *boundaries,
35664f89611Schristos     uint8_t *pdadcs)
35764f89611Schristos {
35864f89611Schristos 	const struct ar5416_eeprom *eep = sc->sc_eep;
35964f89611Schristos 	const struct ar5416_cal_data_per_freq *pierdata;
36064f89611Schristos 	const uint8_t *pierfreq;
36164f89611Schristos 	struct athn_pier lopier, hipier;
36264f89611Schristos 	int16_t delta;
36364f89611Schristos 	uint8_t fbin, pwroff;
36464f89611Schristos 	int i, lo, hi, npiers;
36564f89611Schristos 
36664f89611Schristos 	if (IEEE80211_IS_CHAN_2GHZ(c)) {
36764f89611Schristos 		pierfreq = eep->calFreqPier2G;
36864f89611Schristos 		pierdata = eep->calPierData2G[chain];
36964f89611Schristos 		npiers = AR5416_NUM_2G_CAL_PIERS;
37064f89611Schristos 	}
37164f89611Schristos 	else {
37264f89611Schristos 		pierfreq = eep->calFreqPier5G;
37364f89611Schristos 		pierdata = eep->calPierData5G[chain];
37464f89611Schristos 		npiers = AR5416_NUM_5G_CAL_PIERS;
37564f89611Schristos 	}
37664f89611Schristos 	/* Find channel in ROM pier table. */
37764f89611Schristos 	fbin = athn_chan2fbin(c);
37864f89611Schristos 	athn_get_pier_ival(fbin, pierfreq, npiers, &lo, &hi);
37964f89611Schristos 
38064f89611Schristos 	lopier.fbin = pierfreq[lo];
38164f89611Schristos 	hipier.fbin = pierfreq[hi];
38264f89611Schristos 	for (i = 0; i < nxpdgains; i++) {
38364f89611Schristos 		lopier.pwr[i] = pierdata[lo].pwrPdg[i];
38464f89611Schristos 		lopier.vpd[i] = pierdata[lo].vpdPdg[i];
38564f89611Schristos 		hipier.pwr[i] = pierdata[lo].pwrPdg[i];
38664f89611Schristos 		hipier.vpd[i] = pierdata[lo].vpdPdg[i];
38764f89611Schristos 	}
38864f89611Schristos 	ar5008_get_pdadcs(sc, fbin, &lopier, &hipier, nxpdgains,
38964f89611Schristos 	    AR5416_PD_GAIN_ICEPTS, overlap, boundaries, pdadcs);
39064f89611Schristos 
39164f89611Schristos 	if (!AR_SREV_9280_20_OR_LATER(sc))
39264f89611Schristos 		return;
39364f89611Schristos 
39464f89611Schristos 	if (sc->sc_eep_rev >= AR_EEP_MINOR_VER_21)
39564f89611Schristos 		pwroff = eep->baseEepHeader.pwrTableOffset;
39664f89611Schristos 	else
39764f89611Schristos 		pwroff = AR_PWR_TABLE_OFFSET_DB;
39864f89611Schristos 	delta = (pwroff - AR_PWR_TABLE_OFFSET_DB) * 2;	/* In half dB. */
39964f89611Schristos 
40064f89611Schristos 	/* Change the original gain boundaries setting. */
40164f89611Schristos 	for (i = 0; i < nxpdgains; i++) {
40264f89611Schristos 		/* XXX Possible overflows? */
40364f89611Schristos 		boundaries[i] -= delta;
40464f89611Schristos 		if (boundaries[i] > AR_MAX_RATE_POWER - overlap)
40564f89611Schristos 			boundaries[i] = AR_MAX_RATE_POWER - overlap;
40664f89611Schristos 	}
40764f89611Schristos 	if (delta != 0) {
40864f89611Schristos 		/* Shift the PDADC table to start at the new offset. */
40964f89611Schristos 		for (i = 0; i < AR_NUM_PDADC_VALUES; i++)
41064f89611Schristos 			pdadcs[i] = pdadcs[MIN(i + delta,
41164f89611Schristos 			    AR_NUM_PDADC_VALUES - 1)];
41264f89611Schristos 	}
41364f89611Schristos }
41464f89611Schristos 
41564f89611Schristos Static void
ar5416_set_power_calib(struct athn_softc * sc,struct ieee80211_channel * c)41664f89611Schristos ar5416_set_power_calib(struct athn_softc *sc, struct ieee80211_channel *c)
41764f89611Schristos {
41864f89611Schristos 	static const uint32_t chainoffset[] = { 0x0000, 0x2000, 0x1000 };
41964f89611Schristos 	const struct ar5416_eeprom *eep = sc->sc_eep;
42064f89611Schristos 	const struct ar5416_modal_eep_header *modal;
42164f89611Schristos 	uint8_t boundaries[AR_PD_GAINS_IN_MASK];
42264f89611Schristos 	uint8_t pdadcs[AR_NUM_PDADC_VALUES];
42364f89611Schristos 	uint8_t xpdgains[AR5416_NUM_PD_GAINS];
42464f89611Schristos 	uint8_t overlap, txgain;
42564f89611Schristos 	uint32_t reg, offset;
42664f89611Schristos 	int i, j, nxpdgains;
42764f89611Schristos 
42864f89611Schristos 	modal = &eep->modalHeader[IEEE80211_IS_CHAN_2GHZ(c)];
42964f89611Schristos 
43064f89611Schristos 	if (sc->sc_eep_rev < AR_EEP_MINOR_VER_2) {
43164f89611Schristos 		overlap = MS(AR_READ(sc, AR_PHY_TPCRG5),
43264f89611Schristos 		    AR_PHY_TPCRG5_PD_GAIN_OVERLAP);
43364f89611Schristos 	}
43464f89611Schristos 	else
43564f89611Schristos 		overlap = modal->pdGainOverlap;
43664f89611Schristos 
43764f89611Schristos 	if ((sc->sc_flags & ATHN_FLAG_OLPC) && IEEE80211_IS_CHAN_2GHZ(c)) {
43864f89611Schristos 		/* XXX not here. */
43964f89611Schristos 		sc->sc_pdadc =
44064f89611Schristos 		    ((const struct ar_cal_data_per_freq_olpc *)
44164f89611Schristos 		     eep->calPierData2G[0])->vpdPdg[0][0];
44264f89611Schristos 	}
44364f89611Schristos 
44464f89611Schristos 	nxpdgains = 0;
44564f89611Schristos 	memset(xpdgains, 0, sizeof(xpdgains));
44664f89611Schristos 	for (i = AR5416_PD_GAINS_IN_MASK - 1; i >= 0; i--) {
44764f89611Schristos 		if (nxpdgains >= AR5416_NUM_PD_GAINS)
44864f89611Schristos 			break;	/* Can't happen. */
44964f89611Schristos 		if (modal->xpdGain & (1 << i))
45064f89611Schristos 			xpdgains[nxpdgains++] = i;
45164f89611Schristos 	}
45264f89611Schristos 	reg = AR_READ(sc, AR_PHY_TPCRG1);
45364f89611Schristos 	reg = RW(reg, AR_PHY_TPCRG1_NUM_PD_GAIN, nxpdgains - 1);
45464f89611Schristos 	reg = RW(reg, AR_PHY_TPCRG1_PD_GAIN_1, xpdgains[0]);
45564f89611Schristos 	reg = RW(reg, AR_PHY_TPCRG1_PD_GAIN_2, xpdgains[1]);
45664f89611Schristos 	reg = RW(reg, AR_PHY_TPCRG1_PD_GAIN_3, xpdgains[2]);
45764f89611Schristos 	AR_WRITE(sc, AR_PHY_TPCRG1, reg);
45864f89611Schristos 
45964f89611Schristos 	for (i = 0; i < AR5416_MAX_CHAINS; i++) {
46064f89611Schristos 		if (!(sc->sc_txchainmask & (1 << i)))
46164f89611Schristos 			continue;
46264f89611Schristos 
46364f89611Schristos 		if (AR_SREV_5416_20_OR_LATER(sc) &&
46464f89611Schristos 		    (sc->sc_rxchainmask == 0x5 || sc->sc_txchainmask == 0x5))
46564f89611Schristos 			offset = chainoffset[i];
46664f89611Schristos 		else
46764f89611Schristos 			offset = i * 0x1000;
46864f89611Schristos 
46964f89611Schristos 		if (sc->sc_flags & ATHN_FLAG_OLPC) {
47064f89611Schristos 			ar9280_olpc_get_pdadcs(sc, c, i, boundaries,
47164f89611Schristos 			    pdadcs, &txgain);
47264f89611Schristos 
47364f89611Schristos 			reg = AR_READ(sc, AR_PHY_TX_PWRCTRL6_0);
47464f89611Schristos 			reg = RW(reg, AR_PHY_TX_PWRCTRL_ERR_EST_MODE, 3);
47564f89611Schristos 			AR_WRITE(sc, AR_PHY_TX_PWRCTRL6_0, reg);
47664f89611Schristos 
47764f89611Schristos 			reg = AR_READ(sc, AR_PHY_TX_PWRCTRL6_1);
47864f89611Schristos 			reg = RW(reg, AR_PHY_TX_PWRCTRL_ERR_EST_MODE, 3);
47964f89611Schristos 			AR_WRITE(sc, AR_PHY_TX_PWRCTRL6_1, reg);
48064f89611Schristos 
48164f89611Schristos 			reg = AR_READ(sc, AR_PHY_TX_PWRCTRL7);
48264f89611Schristos 			reg = RW(reg, AR_PHY_TX_PWRCTRL_INIT_TX_GAIN, txgain);
48364f89611Schristos 			AR_WRITE(sc, AR_PHY_TX_PWRCTRL7, reg);
48464f89611Schristos 
48564f89611Schristos 			overlap = 6;
48664f89611Schristos 		}
48764f89611Schristos 		else {
48864f89611Schristos 			ar5416_get_pdadcs(sc, c, i, nxpdgains, overlap,
48964f89611Schristos 			    boundaries, pdadcs);
49064f89611Schristos 		}
49164f89611Schristos 		/* Write boundaries. */
49264f89611Schristos 		if (i == 0 || AR_SREV_5416_20_OR_LATER(sc)) {
49364f89611Schristos 			reg  = SM(AR_PHY_TPCRG5_PD_GAIN_OVERLAP,
49464f89611Schristos 			    overlap);
49564f89611Schristos 			reg |= SM(AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_1,
49664f89611Schristos 			    boundaries[0]);
49764f89611Schristos 			reg |= SM(AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_2,
49864f89611Schristos 			    boundaries[1]);
49964f89611Schristos 			reg |= SM(AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_3,
50064f89611Schristos 			    boundaries[2]);
50164f89611Schristos 			reg |= SM(AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_4,
50264f89611Schristos 			    boundaries[3]);
50364f89611Schristos 			AR_WRITE(sc, AR_PHY_TPCRG5 + offset, reg);
50464f89611Schristos 		}
50564f89611Schristos 		/* Write PDADC values. */
50664f89611Schristos 		for (j = 0; j < AR_NUM_PDADC_VALUES; j += 4) {
50764f89611Schristos 			AR_WRITE(sc, AR_PHY_PDADC_TBL_BASE + offset + j,
50864f89611Schristos 			    pdadcs[j + 0] <<  0 |
50964f89611Schristos 			    pdadcs[j + 1] <<  8 |
51064f89611Schristos 			    pdadcs[j + 2] << 16 |
51164f89611Schristos 			    pdadcs[j + 3] << 24);
51264f89611Schristos 		}
51364f89611Schristos 	}
51464f89611Schristos }
51564f89611Schristos 
51664f89611Schristos PUBLIC void
ar5416_set_txpower(struct athn_softc * sc,struct ieee80211_channel * c,struct ieee80211_channel * extc)51764f89611Schristos ar5416_set_txpower(struct athn_softc *sc, struct ieee80211_channel *c,
51864f89611Schristos     struct ieee80211_channel *extc)
51964f89611Schristos {
52064f89611Schristos 	const struct ar5416_eeprom *eep = sc->sc_eep;
52164f89611Schristos 	const struct ar5416_modal_eep_header *modal;
52264f89611Schristos 	uint8_t tpow_cck[4], tpow_ofdm[4];
52364f89611Schristos #ifndef IEEE80211_NO_HT
52464f89611Schristos 	uint8_t tpow_cck_ext[4], tpow_ofdm_ext[4];
52564f89611Schristos 	uint8_t tpow_ht20[8], tpow_ht40[8];
52664f89611Schristos 	uint8_t ht40inc;
52764f89611Schristos #endif
52864f89611Schristos 	int16_t pwr = 0, pwroff, max_ant_gain, power[ATHN_POWER_COUNT];
52964f89611Schristos 	uint8_t cckinc;
53064f89611Schristos 	int i;
53164f89611Schristos 
53264f89611Schristos 	ar5416_set_power_calib(sc, c);
53364f89611Schristos 
53464f89611Schristos 	modal = &eep->modalHeader[IEEE80211_IS_CHAN_2GHZ(c)];
53564f89611Schristos 
53664f89611Schristos 	/* Compute transmit power reduction due to antenna gain. */
53764f89611Schristos 	max_ant_gain = MAX(modal->antennaGainCh[0], modal->antennaGainCh[1]);
53864f89611Schristos 	max_ant_gain = MAX(modal->antennaGainCh[2], max_ant_gain);
53964f89611Schristos 	/* XXX */
54064f89611Schristos 
54164f89611Schristos 	/*
54264f89611Schristos 	 * Reduce scaled power by number of active chains to get per-chain
54364f89611Schristos 	 * transmit power level.
54464f89611Schristos 	 */
54564f89611Schristos 	if (sc->sc_ntxchains == 2)
54664f89611Schristos 		pwr -= AR_PWR_DECREASE_FOR_2_CHAIN;
54764f89611Schristos 	else if (sc->sc_ntxchains == 3)
54864f89611Schristos 		pwr -= AR_PWR_DECREASE_FOR_3_CHAIN;
54964f89611Schristos 	if (pwr < 0)
55064f89611Schristos 		pwr = 0;
55164f89611Schristos 
55264f89611Schristos 	if (IEEE80211_IS_CHAN_2GHZ(c)) {
55364f89611Schristos 		/* Get CCK target powers. */
55464f89611Schristos 		ar5008_get_lg_tpow(sc, c, AR_CTL_11B, eep->calTargetPowerCck,
55564f89611Schristos 		    AR5416_NUM_2G_CCK_TARGET_POWERS, tpow_cck);
55664f89611Schristos 
55764f89611Schristos 		/* Get OFDM target powers. */
55864f89611Schristos 		ar5008_get_lg_tpow(sc, c, AR_CTL_11G, eep->calTargetPower2G,
55964f89611Schristos 		    AR5416_NUM_2G_20_TARGET_POWERS, tpow_ofdm);
56064f89611Schristos 
56164f89611Schristos #ifndef IEEE80211_NO_HT
56264f89611Schristos 		/* Get HT-20 target powers. */
56364f89611Schristos 		ar5008_get_ht_tpow(sc, c, AR_CTL_2GHT20,
56464f89611Schristos 		    eep->calTargetPower2GHT20, AR5416_NUM_2G_20_TARGET_POWERS,
56564f89611Schristos 		    tpow_ht20);
56664f89611Schristos 
56764f89611Schristos 		if (extc != NULL) {
56864f89611Schristos 			/* Get HT-40 target powers. */
56964f89611Schristos 			ar5008_get_ht_tpow(sc, c, AR_CTL_2GHT40,
57064f89611Schristos 			    eep->calTargetPower2GHT40,
57164f89611Schristos 			    AR5416_NUM_2G_40_TARGET_POWERS, tpow_ht40);
57264f89611Schristos 
57364f89611Schristos 			/* Get secondary channel CCK target powers. */
57464f89611Schristos 			ar5008_get_lg_tpow(sc, extc, AR_CTL_11B,
57564f89611Schristos 			    eep->calTargetPowerCck,
57664f89611Schristos 			    AR5416_NUM_2G_CCK_TARGET_POWERS, tpow_cck_ext);
57764f89611Schristos 
57864f89611Schristos 			/* Get secondary channel OFDM target powers. */
57964f89611Schristos 			ar5008_get_lg_tpow(sc, extc, AR_CTL_11G,
58064f89611Schristos 			    eep->calTargetPower2G,
58164f89611Schristos 			    AR5416_NUM_2G_20_TARGET_POWERS, tpow_ofdm_ext);
58264f89611Schristos 		}
58364f89611Schristos #endif
58464f89611Schristos 	}
58564f89611Schristos 	else {
58664f89611Schristos 		/* Get OFDM target powers. */
58764f89611Schristos 		ar5008_get_lg_tpow(sc, c, AR_CTL_11A, eep->calTargetPower5G,
58864f89611Schristos 		    AR5416_NUM_5G_20_TARGET_POWERS, tpow_ofdm);
58964f89611Schristos 
59064f89611Schristos #ifndef IEEE80211_NO_HT
59164f89611Schristos 		/* Get HT-20 target powers. */
59264f89611Schristos 		ar5008_get_ht_tpow(sc, c, AR_CTL_5GHT20,
59364f89611Schristos 		    eep->calTargetPower5GHT20, AR5416_NUM_5G_20_TARGET_POWERS,
59464f89611Schristos 		    tpow_ht20);
59564f89611Schristos 
59664f89611Schristos 		if (extc != NULL) {
59764f89611Schristos 			/* Get HT-40 target powers. */
59864f89611Schristos 			ar5008_get_ht_tpow(sc, c, AR_CTL_5GHT40,
59964f89611Schristos 			    eep->calTargetPower5GHT40,
60064f89611Schristos 			    AR5416_NUM_5G_40_TARGET_POWERS, tpow_ht40);
60164f89611Schristos 
60264f89611Schristos 			/* Get secondary channel OFDM target powers. */
60364f89611Schristos 			ar5008_get_lg_tpow(sc, extc, AR_CTL_11A,
60464f89611Schristos 			    eep->calTargetPower5G,
60564f89611Schristos 			    AR5416_NUM_5G_20_TARGET_POWERS, tpow_ofdm_ext);
60664f89611Schristos 		}
60764f89611Schristos #endif
60864f89611Schristos 	}
60964f89611Schristos 
61064f89611Schristos 	/* Compute CCK/OFDM delta. */
61164f89611Schristos 	cckinc = (sc->sc_flags & ATHN_FLAG_OLPC) ? -2 : 0;
61264f89611Schristos 
61364f89611Schristos 	memset(power, 0, sizeof(power));
61464f89611Schristos 	/* Shuffle target powers accross transmit rates. */
61564f89611Schristos 	power[ATHN_POWER_OFDM6 ] =
61664f89611Schristos 	power[ATHN_POWER_OFDM9 ] =
61764f89611Schristos 	power[ATHN_POWER_OFDM12] =
61864f89611Schristos 	power[ATHN_POWER_OFDM18] =
61964f89611Schristos 	power[ATHN_POWER_OFDM24] = tpow_ofdm[0];
62064f89611Schristos 	power[ATHN_POWER_OFDM36] = tpow_ofdm[1];
62164f89611Schristos 	power[ATHN_POWER_OFDM48] = tpow_ofdm[2];
62264f89611Schristos 	power[ATHN_POWER_OFDM54] = tpow_ofdm[3];
62364f89611Schristos 	power[ATHN_POWER_XR    ] = tpow_ofdm[0];
62464f89611Schristos 	if (IEEE80211_IS_CHAN_2GHZ(c)) {
62564f89611Schristos 		power[ATHN_POWER_CCK1_LP ] = tpow_cck[0] + cckinc;
62664f89611Schristos 		power[ATHN_POWER_CCK2_LP ] =
62764f89611Schristos 		power[ATHN_POWER_CCK2_SP ] = tpow_cck[1] + cckinc;
62864f89611Schristos 		power[ATHN_POWER_CCK55_LP] =
62964f89611Schristos 		power[ATHN_POWER_CCK55_SP] = tpow_cck[2] + cckinc;
63064f89611Schristos 		power[ATHN_POWER_CCK11_LP] =
63164f89611Schristos 		power[ATHN_POWER_CCK11_SP] = tpow_cck[3] + cckinc;
63264f89611Schristos 	}
63364f89611Schristos #ifndef IEEE80211_NO_HT
63464f89611Schristos 	for (i = 0; i < nitems(tpow_ht20); i++)
63564f89611Schristos 		power[ATHN_POWER_HT20(i)] = tpow_ht20[i];
63664f89611Schristos 	if (extc != NULL) {
63764f89611Schristos 		/* Correct PAR difference between HT40 and HT20/Legacy. */
63864f89611Schristos 		if (sc->sc_eep_rev >= AR_EEP_MINOR_VER_2)
63964f89611Schristos 			ht40inc = modal->ht40PowerIncForPdadc;
64064f89611Schristos 		else
64164f89611Schristos 			ht40inc = AR_HT40_POWER_INC_FOR_PDADC;
64264f89611Schristos 		for (i = 0; i < nitems(tpow_ht40); i++)
64364f89611Schristos 			power[ATHN_POWER_HT40(i)] = tpow_ht40[i] + ht40inc;
64464f89611Schristos 		power[ATHN_POWER_OFDM_DUP] = tpow_ht40[0];
64564f89611Schristos 		power[ATHN_POWER_CCK_DUP ] = tpow_ht40[0] + cckinc;
64664f89611Schristos 		power[ATHN_POWER_OFDM_EXT] = tpow_ofdm_ext[0];
64764f89611Schristos 		if (IEEE80211_IS_CHAN_2GHZ(c))
64864f89611Schristos 			power[ATHN_POWER_CCK_EXT] = tpow_cck_ext[0] + cckinc;
64964f89611Schristos 	}
65064f89611Schristos #endif
65164f89611Schristos 
65264f89611Schristos 	if (AR_SREV_9280_10_OR_LATER(sc)) {
65364f89611Schristos 		if (sc->sc_eep_rev >= AR_EEP_MINOR_VER_21)
65464f89611Schristos 			pwroff = eep->baseEepHeader.pwrTableOffset;
65564f89611Schristos 		else
65664f89611Schristos 			pwroff = AR_PWR_TABLE_OFFSET_DB;
65764f89611Schristos 		for (i = 0; i < ATHN_POWER_COUNT; i++)
65864f89611Schristos 			power[i] -= pwroff * 2;	/* In half dB. */
65964f89611Schristos 	}
66064f89611Schristos 	for (i = 0; i < ATHN_POWER_COUNT; i++) {
66164f89611Schristos 		if (power[i] > AR_MAX_RATE_POWER)
66264f89611Schristos 			power[i] = AR_MAX_RATE_POWER;
66364f89611Schristos 	}
66464f89611Schristos 
66564f89611Schristos 	/* Write transmit power values to hardware. */
66664f89611Schristos 	ar5008_write_txpower(sc, power);
66764f89611Schristos 
66864f89611Schristos 	/*
66964f89611Schristos 	 * Write transmit power substraction for dynamic chain changing
67064f89611Schristos 	 * and per-packet transmit power.
67164f89611Schristos 	 */
67264f89611Schristos 	AR_WRITE(sc, AR_PHY_POWER_TX_SUB,
67364f89611Schristos 	    (modal->pwrDecreaseFor3Chain & 0x3f) << 6 |
67464f89611Schristos 	    (modal->pwrDecreaseFor2Chain & 0x3f));
67564f89611Schristos }
67664f89611Schristos 
67764f89611Schristos Static void
ar5416_spur_mitigate(struct athn_softc * sc,struct ieee80211_channel * c,struct ieee80211_channel * extc)67864f89611Schristos ar5416_spur_mitigate(struct athn_softc *sc, struct ieee80211_channel *c,
67964f89611Schristos     struct ieee80211_channel *extc)
68064f89611Schristos {
68164f89611Schristos 	const struct ar_spur_chan *spurchans;
68264f89611Schristos 	int i, spur, bin, spur_delta_phase, spur_freq_sd;
68364f89611Schristos 
68464f89611Schristos 	spurchans = sc->sc_ops.get_spur_chans(sc, IEEE80211_IS_CHAN_2GHZ(c));
68564f89611Schristos 	for (i = 0; i < AR_EEPROM_MODAL_SPURS; i++) {
68664f89611Schristos 		spur = spurchans[i].spurChan;
68764f89611Schristos 		if (spur == AR_NO_SPUR)
68864f89611Schristos 			return; /* XXX disable if it was enabled! */
68964f89611Schristos 		spur -= c->ic_freq * 10;
69064f89611Schristos 		/* Verify range +/-9.5MHz */
69164f89611Schristos 		if (abs(spur) < 95)
69264f89611Schristos 			break;
69364f89611Schristos 	}
69464f89611Schristos 	if (i == AR_EEPROM_MODAL_SPURS)
69564f89611Schristos 		return; /* XXX disable if it was enabled! */
69664f89611Schristos 	DPRINTFN(DBG_RF, sc, "enabling spur mitigation\n");
69764f89611Schristos 
69864f89611Schristos 	AR_SETBITS(sc, AR_PHY_TIMING_CTRL4_0,
69964f89611Schristos 	    AR_PHY_TIMING_CTRL4_ENABLE_SPUR_RSSI |
70064f89611Schristos 	    AR_PHY_TIMING_CTRL4_ENABLE_SPUR_FILTER |
70164f89611Schristos 	    AR_PHY_TIMING_CTRL4_ENABLE_CHAN_MASK |
70264f89611Schristos 	    AR_PHY_TIMING_CTRL4_ENABLE_PILOT_MASK);
70364f89611Schristos 
70464f89611Schristos 	AR_WRITE(sc, AR_PHY_SPUR_REG,
70564f89611Schristos 	    AR_PHY_SPUR_REG_MASK_RATE_CNTL |
70664f89611Schristos 	    AR_PHY_SPUR_REG_ENABLE_MASK_PPM |
70764f89611Schristos 	    AR_PHY_SPUR_REG_MASK_RATE_SELECT |
70864f89611Schristos 	    AR_PHY_SPUR_REG_ENABLE_VIT_SPUR_RSSI |
70964f89611Schristos 	    SM(AR_PHY_SPUR_REG_SPUR_RSSI_THRESH, AR_SPUR_RSSI_THRESH));
71064f89611Schristos 
71164f89611Schristos 	spur_delta_phase = (spur * 524288) / 100;
71264f89611Schristos 	if (IEEE80211_IS_CHAN_2GHZ(c))
71364f89611Schristos 		spur_freq_sd = (spur * 2048) / 440;
71464f89611Schristos 	else
71564f89611Schristos 		spur_freq_sd = (spur * 2048) / 400;
71664f89611Schristos 
71764f89611Schristos 	AR_WRITE(sc, AR_PHY_TIMING11,
71864f89611Schristos 	    AR_PHY_TIMING11_USE_SPUR_IN_AGC |
71964f89611Schristos 	    SM(AR_PHY_TIMING11_SPUR_FREQ_SD, spur_freq_sd) |
72064f89611Schristos 	    SM(AR_PHY_TIMING11_SPUR_DELTA_PHASE, spur_delta_phase));
72164f89611Schristos 
72264f89611Schristos 	bin = spur * 32;
72364f89611Schristos 	ar5008_set_viterbi_mask(sc, bin);
72464f89611Schristos }
72564f89611Schristos 
72664f89611Schristos Static uint8_t
ar5416_reverse_bits(uint8_t v,int nbits)72764f89611Schristos ar5416_reverse_bits(uint8_t v, int nbits)
72864f89611Schristos {
72964f89611Schristos 	KASSERT(nbits <= 8);
73064f89611Schristos 	v = ((v >> 1) & 0x55) | ((v & 0x55) << 1);
73164f89611Schristos 	v = ((v >> 2) & 0x33) | ((v & 0x33) << 2);
73264f89611Schristos 	v = ((v >> 4) & 0x0f) | ((v & 0x0f) << 4);
73364f89611Schristos 	return v >> (8 - nbits);
73464f89611Schristos }
73564f89611Schristos 
73664f89611Schristos PUBLIC uint8_t
ar5416_get_rf_rev(struct athn_softc * sc)73764f89611Schristos ar5416_get_rf_rev(struct athn_softc *sc)
73864f89611Schristos {
73964f89611Schristos 	uint8_t rev, reg;
74064f89611Schristos 	int i;
74164f89611Schristos 
74264f89611Schristos 	/* Allow access to analog chips. */
74364f89611Schristos 	AR_WRITE(sc, AR_PHY(0), 0x00000007);
74464f89611Schristos 
74564f89611Schristos 	AR_WRITE(sc, AR_PHY(0x36), 0x00007058);
74664f89611Schristos 	for (i = 0; i < 8; i++)
74764f89611Schristos 		AR_WRITE(sc, AR_PHY(0x20), 0x00010000);
74864f89611Schristos 	reg = (AR_READ(sc, AR_PHY(256)) >> 24) & 0xff;
74964f89611Schristos 	reg = (reg & 0xf0) >> 4 | (reg & 0x0f) << 4;
75064f89611Schristos 
75164f89611Schristos 	rev = ar5416_reverse_bits(reg, 8);
75264f89611Schristos 	if ((rev & AR_RADIO_SREV_MAJOR) == 0)
75364f89611Schristos 		rev = AR_RAD5133_SREV_MAJOR;
75464f89611Schristos 	return rev;
75564f89611Schristos }
75664f89611Schristos 
75764f89611Schristos /*
75864f89611Schristos  * Replace bits "off" to "off+nbits-1" in column "col" with the specified
75964f89611Schristos  * value.
76064f89611Schristos  */
76164f89611Schristos Static void
ar5416_rw_rfbits(uint32_t * buf,int col,int off,uint32_t val,int nbits)76264f89611Schristos ar5416_rw_rfbits(uint32_t *buf, int col, int off, uint32_t val, int nbits)
76364f89611Schristos {
76464f89611Schristos 	int idx, bit;
76564f89611Schristos 
76664f89611Schristos 	KASSERT(off >= 1 && col < 4 && nbits <= 32);
76764f89611Schristos 
76864f89611Schristos 	off--;	/* Starts at 1. */
76964f89611Schristos 	while (nbits-- > 0) {
77064f89611Schristos 		idx = off / 8;
77164f89611Schristos 		bit = off % 8;
77264f89611Schristos 		buf[idx] &= ~(1 << (bit + col * 8));
77364f89611Schristos 		buf[idx] |= ((val >> nbits) & 1) << (bit + col * 8);
77464f89611Schristos 		off++;
77564f89611Schristos 	}
77664f89611Schristos }
77764f89611Schristos 
77864f89611Schristos /*
77964f89611Schristos  * Overwrite db and ob based on ROM settings.
78064f89611Schristos  */
78164f89611Schristos Static void
ar5416_rw_bank6tpc(struct athn_softc * sc,struct ieee80211_channel * c,uint32_t * rwbank6tpc)78264f89611Schristos ar5416_rw_bank6tpc(struct athn_softc *sc, struct ieee80211_channel *c,
78364f89611Schristos     uint32_t *rwbank6tpc)
78464f89611Schristos {
78564f89611Schristos 	const struct ar5416_eeprom *eep = sc->sc_eep;
78664f89611Schristos 	const struct ar5416_modal_eep_header *modal;
78764f89611Schristos 
78864f89611Schristos 	if (IEEE80211_IS_CHAN_5GHZ(c)) {
78964f89611Schristos 		modal = &eep->modalHeader[0];
79064f89611Schristos 		/* 5GHz db in column 0, bits [200-202]. */
79164f89611Schristos 		ar5416_rw_rfbits(rwbank6tpc, 0, 200, modal->db, 3);
79264f89611Schristos 		/* 5GHz ob in column 0, bits [203-205]. */
79364f89611Schristos 		ar5416_rw_rfbits(rwbank6tpc, 0, 203, modal->ob, 3);
79464f89611Schristos 	}
79564f89611Schristos 	else {
79664f89611Schristos 		modal = &eep->modalHeader[1];
79764f89611Schristos 		/* 2GHz db in column 0, bits [194-196]. */
79864f89611Schristos 		ar5416_rw_rfbits(rwbank6tpc, 0, 194, modal->db, 3);
79964f89611Schristos 		/* 2GHz ob in column 0, bits [197-199]. */
80064f89611Schristos 		ar5416_rw_rfbits(rwbank6tpc, 0, 197, modal->ob, 3);
80164f89611Schristos 	}
80264f89611Schristos }
80364f89611Schristos 
80464f89611Schristos /*
80564f89611Schristos  * Program analog RF.
80664f89611Schristos  */
80764f89611Schristos PUBLIC void
ar5416_rf_reset(struct athn_softc * sc,struct ieee80211_channel * c)80864f89611Schristos ar5416_rf_reset(struct athn_softc *sc, struct ieee80211_channel *c)
80964f89611Schristos {
81064f89611Schristos 	const uint32_t *bank6tpc;
81164f89611Schristos 	int i;
81264f89611Schristos 
81364f89611Schristos 	/* Bank 0. */
81464f89611Schristos 	AR_WRITE(sc, 0x98b0, 0x1e5795e5);
81564f89611Schristos 	AR_WRITE(sc, 0x98e0, 0x02008020);
81664f89611Schristos 
81764f89611Schristos 	/* Bank 1. */
81864f89611Schristos 	AR_WRITE(sc, 0x98b0, 0x02108421);
81964f89611Schristos 	AR_WRITE(sc, 0x98ec, 0x00000008);
82064f89611Schristos 
82164f89611Schristos 	/* Bank 2. */
82264f89611Schristos 	AR_WRITE(sc, 0x98b0, 0x0e73ff17);
82364f89611Schristos 	AR_WRITE(sc, 0x98e0, 0x00000420);
82464f89611Schristos 
82564f89611Schristos 	/* Bank 3. */
82664f89611Schristos 	if (IEEE80211_IS_CHAN_5GHZ(c))
82764f89611Schristos 		AR_WRITE(sc, 0x98f0, 0x01400018);
82864f89611Schristos 	else
82964f89611Schristos 		AR_WRITE(sc, 0x98f0, 0x01c00018);
83064f89611Schristos 
83164f89611Schristos 	/* Select the Bank 6 TPC values to use. */
83264f89611Schristos 	if (AR_SREV_9160_10_OR_LATER(sc))
83364f89611Schristos 		bank6tpc = ar9160_bank6tpc_vals;
83464f89611Schristos 	else
83564f89611Schristos 		bank6tpc = ar5416_bank6tpc_vals;
83664f89611Schristos 	if (sc->sc_eep_rev >= AR_EEP_MINOR_VER_2) {
83764f89611Schristos 		uint32_t *rwbank6tpc = sc->sc_rwbuf;
83864f89611Schristos 
83964f89611Schristos 		/* Copy values from .rodata to writable buffer. */
84064f89611Schristos 		memcpy(rwbank6tpc, bank6tpc, 32 * sizeof(uint32_t));
84164f89611Schristos 		ar5416_rw_bank6tpc(sc, c, rwbank6tpc);
84264f89611Schristos 		bank6tpc = rwbank6tpc;
84364f89611Schristos 	}
84464f89611Schristos 	/* Bank 6 TPC. */
84564f89611Schristos 	for (i = 0; i < 32; i++)
84664f89611Schristos 		AR_WRITE(sc, 0x989c, bank6tpc[i]);
84764f89611Schristos 	if (IEEE80211_IS_CHAN_5GHZ(c))
84864f89611Schristos 		AR_WRITE(sc, 0x98d0, 0x0000000f);
84964f89611Schristos 	else
85064f89611Schristos 		AR_WRITE(sc, 0x98d0, 0x0010000f);
85164f89611Schristos 
85264f89611Schristos 	/* Bank 7. */
85364f89611Schristos 	AR_WRITE(sc, 0x989c, 0x00000500);
85464f89611Schristos 	AR_WRITE(sc, 0x989c, 0x00000800);
85564f89611Schristos 	AR_WRITE(sc, 0x98cc, 0x0000000e);
85664f89611Schristos }
85764f89611Schristos 
85864f89611Schristos PUBLIC void
ar5416_reset_bb_gain(struct athn_softc * sc,struct ieee80211_channel * c)85964f89611Schristos ar5416_reset_bb_gain(struct athn_softc *sc, struct ieee80211_channel *c)
86064f89611Schristos {
86164f89611Schristos 	const uint32_t *pvals;
86264f89611Schristos 	int i;
86364f89611Schristos 
86464f89611Schristos 	if (IEEE80211_IS_CHAN_2GHZ(c))
86564f89611Schristos 		pvals = ar5416_bb_rfgain_vals_2g;
86664f89611Schristos 	else
86764f89611Schristos 		pvals = ar5416_bb_rfgain_vals_5g;
86864f89611Schristos 	for (i = 0; i < 64; i++)
86964f89611Schristos 		AR_WRITE(sc, AR_PHY_BB_RFGAIN(i), pvals[i]);
87064f89611Schristos }
87164f89611Schristos 
87264f89611Schristos /*
87364f89611Schristos  * Fix orientation sensitivity issue on AR5416/2GHz by increasing
87464f89611Schristos  * rf_pwd_icsyndiv.
87564f89611Schristos  */
87664f89611Schristos Static void
ar5416_force_bias(struct athn_softc * sc,struct ieee80211_channel * c)87764f89611Schristos ar5416_force_bias(struct athn_softc *sc, struct ieee80211_channel *c)
87864f89611Schristos {
87964f89611Schristos 	uint32_t *rwbank6 = sc->sc_rwbuf;
88064f89611Schristos 	uint8_t bias;
88164f89611Schristos 	int i;
88264f89611Schristos 
88364f89611Schristos 	KASSERT(IEEE80211_IS_CHAN_2GHZ(c));
88464f89611Schristos 
88564f89611Schristos 	/* Copy values from .rodata to writable buffer. */
88664f89611Schristos 	memcpy(rwbank6, ar5416_bank6_vals, sizeof(ar5416_bank6_vals));
88764f89611Schristos 
88864f89611Schristos 	if (c->ic_freq < 2412)
88964f89611Schristos 		bias = 0;
89064f89611Schristos 	else if (c->ic_freq < 2422)
89164f89611Schristos 		bias = 1;
89264f89611Schristos 	else
89364f89611Schristos 		bias = 2;
89464f89611Schristos 	ar5416_reverse_bits(bias, 3);
89564f89611Schristos 
89664f89611Schristos 	/* Overwrite "rf_pwd_icsyndiv" (column 3, bits [181-183].) */
89764f89611Schristos 	ar5416_rw_rfbits(rwbank6, 3, 181, bias, 3);
89864f89611Schristos 
89964f89611Schristos 	/* Write Bank 6. */
90064f89611Schristos 	for (i = 0; i < 32; i++)
90164f89611Schristos 		AR_WRITE(sc, 0x989c, rwbank6[i]);
90264f89611Schristos 	AR_WRITE(sc, 0x98d0, 0x0010000f);
90364f89611Schristos }
90464f89611Schristos 
90564f89611Schristos /*
90664f89611Schristos  * Overwrite XPA bias level based on ROM setting.
90764f89611Schristos  */
90864f89611Schristos Static void
ar9160_rw_addac(struct athn_softc * sc,struct ieee80211_channel * c,uint32_t * addac)90964f89611Schristos ar9160_rw_addac(struct athn_softc *sc, struct ieee80211_channel *c,
91064f89611Schristos     uint32_t *addac)
91164f89611Schristos {
91264f89611Schristos 	struct ar5416_eeprom *eep = sc->sc_eep;
91364f89611Schristos 	struct ar5416_modal_eep_header *modal;
91464f89611Schristos 	uint8_t fbin, bias;
91564f89611Schristos 	int i;
91664f89611Schristos 
91764f89611Schristos 	/* XXX xpaBiasLvlFreq values have not been endian-swapped? */
91864f89611Schristos 
91964f89611Schristos 	/* Get the XPA bias level to use for the specified channel. */
92064f89611Schristos 	modal = &eep->modalHeader[IEEE80211_IS_CHAN_2GHZ(c)];
92164f89611Schristos 	if (modal->xpaBiasLvl == 0xff) {
92264f89611Schristos 		bias = modal->xpaBiasLvlFreq[0] >> 14;
92364f89611Schristos 		fbin = athn_chan2fbin(c);
92464f89611Schristos 		for (i = 1; i < 3; i++) {
92564f89611Schristos 			if (modal->xpaBiasLvlFreq[i] == 0)
92664f89611Schristos 				break;
92764f89611Schristos 			if ((modal->xpaBiasLvlFreq[i] & 0xff) < fbin)
92864f89611Schristos 				break;
92964f89611Schristos 			bias = modal->xpaBiasLvlFreq[i] >> 14;
93064f89611Schristos 		}
93164f89611Schristos 	}
93264f89611Schristos 	else
93364f89611Schristos 		bias = modal->xpaBiasLvl & 0x3;
93464f89611Schristos 
93564f89611Schristos 	bias = ar5416_reverse_bits(bias, 2);	/* Put in host bit-order. */
93664f89611Schristos 	DPRINTFN(DBG_RF, sc, "bias level=%d\n", bias);
93764f89611Schristos 	if (IEEE80211_IS_CHAN_2GHZ(c))
93864f89611Schristos 		ar5416_rw_rfbits(addac, 0, 60, bias, 2);
93964f89611Schristos 	else
94064f89611Schristos 		ar5416_rw_rfbits(addac, 0, 55, bias, 2);
94164f89611Schristos }
94264f89611Schristos 
94364f89611Schristos PUBLIC void
ar5416_reset_addac(struct athn_softc * sc,struct ieee80211_channel * c)94464f89611Schristos ar5416_reset_addac(struct athn_softc *sc, struct ieee80211_channel *c)
94564f89611Schristos {
94664f89611Schristos 	const struct athn_addac *addac = sc->sc_addac;
94764f89611Schristos 	const uint32_t *pvals;
94864f89611Schristos 	int i;
94964f89611Schristos 
95064f89611Schristos 	if (AR_SREV_9160(sc) && sc->sc_eep_rev >= AR_EEP_MINOR_VER_7) {
95164f89611Schristos 		uint32_t *rwaddac = sc->sc_rwbuf;
95264f89611Schristos 
95364f89611Schristos 		/* Copy values from .rodata to writable buffer. */
95464f89611Schristos 		memcpy(rwaddac, addac->vals, addac->nvals * sizeof(uint32_t));
95564f89611Schristos 		ar9160_rw_addac(sc, c, rwaddac);
95664f89611Schristos 		pvals = rwaddac;
95764f89611Schristos 	}
95864f89611Schristos 	else
95964f89611Schristos 		pvals = addac->vals;
96064f89611Schristos 	for (i = 0; i < addac->nvals; i++)
96164f89611Schristos 		AR_WRITE(sc, 0x989c, pvals[i]);
96264f89611Schristos 	AR_WRITE(sc, 0x98cc, 0);	/* Finalize. */
96364f89611Schristos }
964