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