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