arn9285.c revision 1.2.6.1       1  1.2.6.1     rmind /*	$NetBSD: arn9285.c,v 1.2.6.1 2014/05/18 17:45:37 rmind Exp $	*/
      2      1.1  christos /*	$OpenBSD: ar9285.c,v 1.19 2012/06/10 21:23:36 kettenis Exp $	*/
      3      1.1  christos 
      4      1.1  christos /*-
      5      1.1  christos  * Copyright (c) 2009-2010 Damien Bergamini <damien.bergamini (at) free.fr>
      6      1.1  christos  * Copyright (c) 2008-2010 Atheros Communications Inc.
      7      1.1  christos  *
      8      1.1  christos  * Permission to use, copy, modify, and/or distribute this software for any
      9      1.1  christos  * purpose with or without fee is hereby granted, provided that the above
     10      1.1  christos  * copyright notice and this permission notice appear in all copies.
     11      1.1  christos  *
     12      1.1  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     13      1.1  christos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     14      1.1  christos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     15      1.1  christos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     16      1.1  christos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     17      1.1  christos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     18      1.1  christos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     19      1.1  christos  */
     20      1.1  christos 
     21      1.1  christos /*
     22      1.1  christos  * Driver for Atheros 802.11a/g/n chipsets.
     23      1.1  christos  * Routines for AR9285 and AR9271 chipsets.
     24      1.1  christos  */
     25      1.1  christos 
     26      1.1  christos #include <sys/cdefs.h>
     27  1.2.6.1     rmind __KERNEL_RCSID(0, "$NetBSD: arn9285.c,v 1.2.6.1 2014/05/18 17:45:37 rmind Exp $");
     28      1.1  christos 
     29      1.1  christos #ifndef _MODULE
     30      1.1  christos #include "athn_usb.h"
     31      1.1  christos #endif
     32      1.1  christos 
     33      1.1  christos #include <sys/param.h>
     34      1.1  christos #include <sys/sockio.h>
     35      1.1  christos #include <sys/mbuf.h>
     36      1.1  christos #include <sys/kernel.h>
     37      1.1  christos #include <sys/socket.h>
     38      1.1  christos #include <sys/systm.h>
     39      1.1  christos #include <sys/malloc.h>
     40      1.1  christos #include <sys/queue.h>
     41      1.1  christos #include <sys/callout.h>
     42      1.1  christos #include <sys/conf.h>
     43      1.1  christos #include <sys/device.h>
     44      1.1  christos 
     45      1.1  christos #include <sys/bus.h>
     46      1.1  christos #include <sys/endian.h>
     47      1.1  christos #include <sys/intr.h>
     48      1.1  christos 
     49      1.1  christos #include <net/bpf.h>
     50      1.1  christos #include <net/if.h>
     51      1.1  christos #include <net/if_arp.h>
     52      1.1  christos #include <net/if_dl.h>
     53      1.2  christos #include <net/if_ether.h>
     54      1.1  christos #include <net/if_media.h>
     55      1.1  christos #include <net/if_types.h>
     56      1.1  christos 
     57      1.1  christos #include <netinet/in.h>
     58      1.1  christos #include <netinet/in_systm.h>
     59      1.1  christos #include <netinet/in_var.h>
     60      1.1  christos #include <netinet/ip.h>
     61      1.1  christos 
     62      1.1  christos #include <net80211/ieee80211_var.h>
     63      1.1  christos #include <net80211/ieee80211_amrr.h>
     64      1.1  christos #include <net80211/ieee80211_radiotap.h>
     65      1.1  christos 
     66      1.1  christos #include <dev/ic/athnreg.h>
     67      1.1  christos #include <dev/ic/athnvar.h>
     68      1.1  christos #include <dev/ic/arn9285.h>
     69      1.1  christos 
     70      1.1  christos #include <dev/ic/arn5008reg.h>
     71      1.1  christos #include <dev/ic/arn9280reg.h>
     72      1.1  christos #include <dev/ic/arn9285reg.h>
     73      1.1  christos 
     74      1.1  christos #include <dev/ic/arn5008.h>
     75      1.1  christos #include <dev/ic/arn9280.h>
     76      1.1  christos #include <dev/ic/arn9285.h>
     77      1.1  christos 
     78      1.1  christos #define Static static
     79      1.1  christos 
     80      1.1  christos Static int	ar9285_cl_cal(struct athn_softc *, struct ieee80211_channel *,
     81      1.1  christos 		    struct ieee80211_channel *);
     82      1.1  christos Static void	ar9285_get_pdadcs(struct athn_softc *,
     83      1.1  christos 		    struct ieee80211_channel *, int, uint8_t, uint8_t *,
     84      1.1  christos 		    uint8_t *);
     85      1.1  christos Static const struct ar_spur_chan *
     86      1.1  christos 		ar9285_get_spur_chans(struct athn_softc *, int);
     87      1.1  christos Static void	ar9285_init_from_rom(struct athn_softc *,
     88      1.1  christos 		    struct ieee80211_channel *, struct ieee80211_channel *);
     89      1.1  christos Static void	ar9285_set_power_calib(struct athn_softc *,
     90      1.1  christos 		    struct ieee80211_channel *);
     91      1.1  christos Static void	ar9285_set_txpower(struct athn_softc *,
     92      1.1  christos 		    struct ieee80211_channel *, struct ieee80211_channel *);
     93      1.1  christos Static void	ar9285_setup(struct athn_softc *);
     94      1.1  christos Static void	ar9285_swap_rom(struct athn_softc *);
     95      1.1  christos 
     96      1.1  christos PUBLIC int
     97      1.1  christos ar9285_attach(struct athn_softc *sc)
     98      1.1  christos {
     99      1.1  christos 
    100      1.1  christos 	sc->sc_eep_base = AR9285_EEP_START_LOC;
    101      1.1  christos 	sc->sc_eep_size = sizeof(struct ar9285_eeprom);
    102      1.1  christos 	sc->sc_def_nf = AR9285_PHY_CCA_MAX_GOOD_VALUE;
    103      1.1  christos 	sc->sc_ngpiopins = (sc->sc_flags & ATHN_FLAG_USB) ? 16 : 12;
    104      1.1  christos 	sc->sc_led_pin = (sc->sc_flags & ATHN_FLAG_USB) ? 15 : 1;
    105      1.1  christos 	sc->sc_workaround = AR9285_WA_DEFAULT;
    106      1.1  christos 	sc->sc_ops.setup = ar9285_setup;
    107      1.1  christos 	sc->sc_ops.swap_rom = ar9285_swap_rom;
    108      1.1  christos 	sc->sc_ops.init_from_rom = ar9285_init_from_rom;
    109      1.1  christos 	sc->sc_ops.set_txpower = ar9285_set_txpower;
    110      1.1  christos 	sc->sc_ops.set_synth = ar9280_set_synth;
    111      1.1  christos 	sc->sc_ops.spur_mitigate = ar9280_spur_mitigate;
    112      1.1  christos 	sc->sc_ops.get_spur_chans = ar9285_get_spur_chans;
    113      1.1  christos #if NATHN_USB > 0
    114      1.1  christos 	if (AR_SREV_9271(sc))
    115      1.1  christos 		sc->sc_ini = &ar9271_ini;
    116      1.1  christos 	else
    117      1.1  christos #endif
    118      1.1  christos 		sc->sc_ini = &ar9285_1_2_ini;
    119      1.1  christos 	sc->sc_serdes = &ar9280_2_0_serdes;
    120      1.1  christos 
    121      1.1  christos 	return ar5008_attach(sc);
    122      1.1  christos }
    123      1.1  christos 
    124      1.1  christos Static void
    125      1.1  christos ar9285_setup(struct athn_softc *sc)
    126      1.1  christos {
    127      1.1  christos 	const struct ar9285_eeprom *eep = sc->sc_eep;
    128      1.1  christos 	uint8_t type;
    129      1.1  christos 
    130      1.1  christos 	/* Select initialization values based on ROM. */
    131      1.1  christos 	type = eep->baseEepHeader.txGainType;
    132      1.1  christos 	DPRINTFN(DBG_TX, sc, "Tx gain type=0x%x\n", type);
    133      1.1  christos #if NATHN_USB > 0
    134      1.1  christos 	if (AR_SREV_9271(sc)) {
    135      1.1  christos 		if (type == AR_EEP_TXGAIN_HIGH_POWER)
    136      1.1  christos 			sc->sc_tx_gain = &ar9271_tx_gain_high_power;
    137      1.1  christos 		else
    138      1.1  christos 			sc->sc_tx_gain = &ar9271_tx_gain;
    139      1.1  christos 	}
    140      1.1  christos 	else
    141      1.1  christos #endif	/* NATHN_USB */
    142      1.1  christos 	if ((AR_READ(sc, AR_AN_SYNTH9) & 0x7) == 0x1) {	/* XE rev. */
    143      1.1  christos 		if (type == AR_EEP_TXGAIN_HIGH_POWER)
    144      1.1  christos 			sc->sc_tx_gain = &ar9285_2_0_tx_gain_high_power;
    145      1.1  christos 		else
    146      1.1  christos 			sc->sc_tx_gain = &ar9285_2_0_tx_gain;
    147      1.1  christos 	}
    148      1.1  christos 	else {
    149      1.1  christos 		if (type == AR_EEP_TXGAIN_HIGH_POWER)
    150      1.1  christos 			sc->sc_tx_gain = &ar9285_1_2_tx_gain_high_power;
    151      1.1  christos 		else
    152      1.1  christos 			sc->sc_tx_gain = &ar9285_1_2_tx_gain;
    153      1.1  christos 	}
    154      1.1  christos }
    155      1.1  christos 
    156      1.1  christos Static void
    157      1.1  christos ar9285_swap_rom(struct athn_softc *sc)
    158      1.1  christos {
    159      1.1  christos 	struct ar9285_eeprom *eep = sc->sc_eep;
    160      1.1  christos 	int i;
    161      1.1  christos 
    162      1.1  christos 	eep->modalHeader.antCtrlCommon =
    163      1.1  christos 	    bswap32(eep->modalHeader.antCtrlCommon);
    164      1.1  christos 	eep->modalHeader.antCtrlChain =
    165      1.1  christos 	    bswap32(eep->modalHeader.antCtrlChain);
    166      1.1  christos 
    167      1.1  christos 	for (i = 0; i < AR_EEPROM_MODAL_SPURS; i++) {
    168      1.1  christos 		eep->modalHeader.spurChans[i].spurChan =
    169      1.1  christos 		    bswap16(eep->modalHeader.spurChans[i].spurChan);
    170      1.1  christos 	}
    171      1.1  christos }
    172      1.1  christos 
    173      1.1  christos Static const struct ar_spur_chan *
    174      1.1  christos ar9285_get_spur_chans(struct athn_softc *sc, int is2ghz)
    175      1.1  christos {
    176      1.1  christos 	const struct ar9285_eeprom *eep = sc->sc_eep;
    177      1.1  christos 
    178      1.1  christos 	KASSERT(is2ghz);
    179      1.1  christos 	return eep->modalHeader.spurChans;
    180      1.1  christos }
    181      1.1  christos 
    182      1.1  christos Static void
    183      1.1  christos ar9285_init_from_rom(struct athn_softc *sc, struct ieee80211_channel *c,
    184      1.1  christos     struct ieee80211_channel *extc)
    185      1.1  christos {
    186      1.1  christos 	const struct ar9285_eeprom *eep = sc->sc_eep;
    187      1.1  christos 	const struct ar9285_modal_eep_header *modal = &eep->modalHeader;
    188      1.1  christos 	uint32_t reg, offset = 0x1000;
    189      1.1  christos 	uint8_t ob[5], db1[5], db2[5];
    190      1.1  christos 	uint8_t txRxAtten;
    191      1.1  christos 
    192      1.1  christos 	AR_WRITE(sc, AR_PHY_SWITCH_COM, modal->antCtrlCommon);
    193      1.1  christos 	AR_WRITE(sc, AR_PHY_SWITCH_CHAIN_0, modal->antCtrlChain);
    194      1.1  christos 
    195      1.1  christos 	reg = AR_READ(sc, AR_PHY_TIMING_CTRL4_0);
    196      1.1  christos 	reg = RW(reg, AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF, modal->iqCalI);
    197      1.1  christos 	reg = RW(reg, AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF, modal->iqCalQ);
    198      1.1  christos 	AR_WRITE(sc, AR_PHY_TIMING_CTRL4_0, reg);
    199      1.1  christos 
    200      1.1  christos 	if (sc->sc_eep_rev >= AR_EEP_MINOR_VER_3) {
    201      1.1  christos 		reg = AR_READ(sc, AR_PHY_GAIN_2GHZ);
    202      1.1  christos 		reg = RW(reg, AR_PHY_GAIN_2GHZ_XATTEN1_MARGIN,
    203      1.1  christos 		    modal->bswMargin);
    204      1.1  christos 		reg = RW(reg, AR_PHY_GAIN_2GHZ_XATTEN1_DB,
    205      1.1  christos 		    modal->bswAtten);
    206      1.1  christos 		reg = RW(reg, AR_PHY_GAIN_2GHZ_XATTEN2_MARGIN,
    207      1.1  christos 		    modal->xatten2Margin);
    208      1.1  christos 		reg = RW(reg, AR_PHY_GAIN_2GHZ_XATTEN2_DB,
    209      1.1  christos 		    modal->xatten2Db);
    210      1.1  christos 		AR_WRITE(sc, AR_PHY_GAIN_2GHZ, reg);
    211      1.1  christos 
    212      1.1  christos 		/* Duplicate values of chain 0 for chain 1. */
    213      1.1  christos 		reg = AR_READ(sc, AR_PHY_GAIN_2GHZ + offset);
    214      1.1  christos 		reg = RW(reg, AR_PHY_GAIN_2GHZ_XATTEN1_MARGIN,
    215      1.1  christos 		    modal->bswMargin);
    216      1.1  christos 		reg = RW(reg, AR_PHY_GAIN_2GHZ_XATTEN1_DB,
    217      1.1  christos 		    modal->bswAtten);
    218      1.1  christos 		reg = RW(reg, AR_PHY_GAIN_2GHZ_XATTEN2_MARGIN,
    219      1.1  christos 		    modal->xatten2Margin);
    220      1.1  christos 		reg = RW(reg, AR_PHY_GAIN_2GHZ_XATTEN2_DB,
    221      1.1  christos 		    modal->xatten2Db);
    222      1.1  christos 		AR_WRITE(sc, AR_PHY_GAIN_2GHZ + offset, reg);
    223      1.1  christos 	}
    224      1.1  christos 	if (sc->sc_eep_rev >= AR_EEP_MINOR_VER_3)
    225      1.1  christos 		txRxAtten = modal->txRxAtten;
    226      1.1  christos 	else	/* Workaround for ROM versions < 14.3. */
    227      1.1  christos 		txRxAtten = 23;
    228      1.1  christos 	reg = AR_READ(sc, AR_PHY_RXGAIN);
    229      1.1  christos 	reg = RW(reg, AR9280_PHY_RXGAIN_TXRX_ATTEN, txRxAtten);
    230      1.1  christos 	reg = RW(reg, AR9280_PHY_RXGAIN_TXRX_MARGIN, modal->rxTxMargin);
    231      1.1  christos 	AR_WRITE(sc, AR_PHY_RXGAIN, reg);
    232      1.1  christos 
    233      1.1  christos 	/* Duplicate values of chain 0 for chain 1. */
    234      1.1  christos 	reg = AR_READ(sc, AR_PHY_RXGAIN + offset);
    235      1.1  christos 	reg = RW(reg, AR9280_PHY_RXGAIN_TXRX_ATTEN, txRxAtten);
    236      1.1  christos 	reg = RW(reg, AR9280_PHY_RXGAIN_TXRX_MARGIN, modal->rxTxMargin);
    237      1.1  christos 	AR_WRITE(sc, AR_PHY_RXGAIN + offset, reg);
    238      1.1  christos 
    239      1.1  christos 	if (modal->version >= 3) {
    240      1.1  christos 		/* Setup antenna diversity from ROM. */
    241      1.1  christos 		reg = AR_READ(sc, AR_PHY_MULTICHAIN_GAIN_CTL);
    242      1.1  christos 		reg = RW(reg, AR9285_PHY_ANT_DIV_CTL_ALL, 0);
    243      1.1  christos 		reg = RW(reg, AR9285_PHY_ANT_DIV_CTL,
    244      1.1  christos 		    (modal->ob_234  >> 12) & 0x1);
    245      1.1  christos 		reg = RW(reg, AR9285_PHY_ANT_DIV_ALT_LNACONF,
    246      1.1  christos 		    (modal->db1_234 >> 12) & 0x3);
    247      1.1  christos 		reg = RW(reg, AR9285_PHY_ANT_DIV_MAIN_LNACONF,
    248      1.1  christos 		    (modal->db1_234 >> 14) & 0x3);
    249      1.1  christos 		reg = RW(reg, AR9285_PHY_ANT_DIV_ALT_GAINTB,
    250      1.1  christos 		    (modal->ob_234  >> 13) & 0x1);
    251      1.1  christos 		reg = RW(reg, AR9285_PHY_ANT_DIV_MAIN_GAINTB,
    252      1.1  christos 		    (modal->ob_234  >> 14) & 0x1);
    253      1.1  christos 		AR_WRITE(sc, AR_PHY_MULTICHAIN_GAIN_CTL, reg);
    254      1.1  christos 		reg = AR_READ(sc, AR_PHY_MULTICHAIN_GAIN_CTL);	/* Flush. */
    255      1.1  christos 
    256      1.1  christos 		reg = AR_READ(sc, AR_PHY_CCK_DETECT);
    257      1.1  christos 		if (modal->ob_234 & (1 << 15))
    258      1.1  christos 			reg |= AR_PHY_CCK_DETECT_BB_ENABLE_ANT_FAST_DIV;
    259      1.1  christos 		else
    260      1.1  christos 			reg &= ~AR_PHY_CCK_DETECT_BB_ENABLE_ANT_FAST_DIV;
    261      1.1  christos 		AR_WRITE(sc, AR_PHY_CCK_DETECT, reg);
    262      1.1  christos 		reg = AR_READ(sc, AR_PHY_CCK_DETECT);		/* Flush. */
    263      1.1  christos 	}
    264      1.1  christos 	if (modal->version >= 2) {
    265      1.1  christos 		ob [0] = (modal->ob_01   >> 0) & 0xf;
    266      1.1  christos 		ob [1] = (modal->ob_01   >> 4) & 0xf;
    267      1.1  christos 		ob [2] = (modal->ob_234  >> 0) & 0xf;
    268      1.1  christos 		ob [3] = (modal->ob_234  >> 4) & 0xf;
    269      1.1  christos 		ob [4] = (modal->ob_234  >> 8) & 0xf;
    270      1.1  christos 
    271      1.1  christos 		db1[0] = (modal->db1_01  >> 0) & 0xf;
    272      1.1  christos 		db1[1] = (modal->db1_01  >> 4) & 0xf;
    273      1.1  christos 		db1[2] = (modal->db1_234 >> 0) & 0xf;
    274      1.1  christos 		db1[3] = (modal->db1_234 >> 4) & 0xf;
    275      1.1  christos 		db1[4] = (modal->db1_234 >> 8) & 0xf;
    276      1.1  christos 
    277      1.1  christos 		db2[0] = (modal->db2_01  >> 0) & 0xf;
    278      1.1  christos 		db2[1] = (modal->db2_01  >> 4) & 0xf;
    279      1.1  christos 		db2[2] = (modal->db2_234 >> 0) & 0xf;
    280      1.1  christos 		db2[3] = (modal->db2_234 >> 4) & 0xf;
    281      1.1  christos 		db2[4] = (modal->db2_234 >> 8) & 0xf;
    282      1.1  christos 
    283      1.1  christos 	}
    284      1.1  christos 	else if (modal->version == 1) {
    285      1.1  christos 		ob [0] = (modal->ob_01   >> 0) & 0xf;
    286      1.1  christos 		ob [1] = (modal->ob_01   >> 4) & 0xf;
    287      1.1  christos 		/* Field ob_234 does not exist, use ob_01. */
    288      1.1  christos 		ob [2] = ob [3] = ob [4] = ob [1];
    289      1.1  christos 
    290      1.1  christos 		db1[0] = (modal->db1_01  >> 0) & 0xf;
    291      1.1  christos 		db1[1] = (modal->db1_01  >> 4) & 0xf;
    292      1.1  christos 		/* Field db1_234 does not exist, use db1_01. */
    293      1.1  christos 		db1[2] = db1[3] = db1[4] = db1[1];
    294      1.1  christos 
    295      1.1  christos 		db2[0] = (modal->db2_01  >> 0) & 0xf;
    296      1.1  christos 		db2[1] = (modal->db2_01  >> 4) & 0xf;
    297      1.1  christos 		/* Field db2_234 does not exist, use db2_01. */
    298      1.1  christos 		db2[2] = db2[3] = db2[4] = db2[1];
    299      1.1  christos 
    300      1.1  christos 	}
    301      1.1  christos 	else {
    302      1.1  christos 		ob [0] = modal->ob_01;
    303      1.1  christos 		ob [1] = ob [2] = ob [3] = ob [4] = ob [0];
    304      1.1  christos 
    305      1.1  christos 		db1[0] = modal->db1_01;
    306      1.1  christos 		db1[1] = db1[2] = db1[3] = db1[4] = db1[0];
    307      1.1  christos 
    308      1.1  christos 		/* Field db2_01 does not exist, use db1_01. */
    309      1.1  christos 		db2[0] = modal->db1_01;
    310      1.1  christos 		db2[1] = db2[2] = db2[3] = db2[4] = db2[0];
    311      1.1  christos 	}
    312      1.1  christos #if NATHN_USB > 0
    313      1.1  christos 	if (AR_SREV_9271(sc)) {
    314      1.1  christos 		reg = AR_READ(sc, AR9285_AN_RF2G3);
    315      1.1  christos 		reg = RW(reg, AR9271_AN_RF2G3_OB_CCK, ob [0]);
    316      1.1  christos 		reg = RW(reg, AR9271_AN_RF2G3_OB_PSK, ob [1]);
    317      1.1  christos 		reg = RW(reg, AR9271_AN_RF2G3_OB_QAM, ob [2]);
    318      1.1  christos 		reg = RW(reg, AR9271_AN_RF2G3_DB1,    db1[0]);
    319      1.1  christos 		AR_WRITE(sc, AR9285_AN_RF2G3, reg);
    320      1.1  christos 		AR_WRITE_BARRIER(sc);
    321      1.1  christos 		DELAY(100);
    322      1.1  christos 		reg = AR_READ(sc, AR9285_AN_RF2G4);
    323      1.1  christos 		reg = RW(reg, AR9271_AN_RF2G4_DB2,    db2[0]);
    324      1.1  christos 		AR_WRITE(sc, AR9285_AN_RF2G4, reg);
    325      1.1  christos 		AR_WRITE_BARRIER(sc);
    326      1.1  christos 		DELAY(100);
    327      1.1  christos 	}
    328      1.1  christos 	else
    329      1.1  christos #endif	/* ATHN_USB */
    330      1.1  christos 	{
    331      1.1  christos 		reg = AR_READ(sc, AR9285_AN_RF2G3);
    332      1.1  christos 		reg = RW(reg, AR9285_AN_RF2G3_OB_0,  ob [0]);
    333      1.1  christos 		reg = RW(reg, AR9285_AN_RF2G3_OB_1,  ob [1]);
    334      1.1  christos 		reg = RW(reg, AR9285_AN_RF2G3_OB_2,  ob [2]);
    335      1.1  christos 		reg = RW(reg, AR9285_AN_RF2G3_OB_3,  ob [3]);
    336      1.1  christos 		reg = RW(reg, AR9285_AN_RF2G3_OB_4,  ob [4]);
    337      1.1  christos 		reg = RW(reg, AR9285_AN_RF2G3_DB1_0, db1[0]);
    338      1.1  christos 		reg = RW(reg, AR9285_AN_RF2G3_DB1_1, db1[1]);
    339      1.1  christos 		reg = RW(reg, AR9285_AN_RF2G3_DB1_2, db1[2]);
    340      1.1  christos 		AR_WRITE(sc, AR9285_AN_RF2G3, reg);
    341      1.1  christos 		AR_WRITE_BARRIER(sc);
    342      1.1  christos 		DELAY(100);
    343      1.1  christos 		reg = AR_READ(sc, AR9285_AN_RF2G4);
    344      1.1  christos 		reg = RW(reg, AR9285_AN_RF2G4_DB1_3, db1[3]);
    345      1.1  christos 		reg = RW(reg, AR9285_AN_RF2G4_DB1_4, db1[4]);
    346      1.1  christos 		reg = RW(reg, AR9285_AN_RF2G4_DB2_0, db2[0]);
    347      1.1  christos 		reg = RW(reg, AR9285_AN_RF2G4_DB2_1, db2[1]);
    348      1.1  christos 		reg = RW(reg, AR9285_AN_RF2G4_DB2_2, db2[2]);
    349      1.1  christos 		reg = RW(reg, AR9285_AN_RF2G4_DB2_3, db2[3]);
    350      1.1  christos 		reg = RW(reg, AR9285_AN_RF2G4_DB2_4, db2[4]);
    351      1.1  christos 		AR_WRITE(sc, AR9285_AN_RF2G4, reg);
    352      1.1  christos 		AR_WRITE_BARRIER(sc);
    353      1.1  christos 		DELAY(100);
    354      1.1  christos 	}
    355      1.1  christos 
    356      1.1  christos 	reg = AR_READ(sc, AR_PHY_SETTLING);
    357      1.1  christos 	reg = RW(reg, AR_PHY_SETTLING_SWITCH, modal->switchSettling);
    358      1.1  christos 	AR_WRITE(sc, AR_PHY_SETTLING, reg);
    359      1.1  christos 
    360      1.1  christos 	reg = AR_READ(sc, AR_PHY_DESIRED_SZ);
    361      1.1  christos 	reg = RW(reg, AR_PHY_DESIRED_SZ_ADC, modal->adcDesiredSize);
    362      1.1  christos 	AR_WRITE(sc, AR_PHY_DESIRED_SZ, reg);
    363      1.1  christos 
    364      1.1  christos 	reg =  SM(AR_PHY_RF_CTL4_TX_END_XPAA_OFF, modal->txEndToXpaOff);
    365      1.1  christos 	reg |= SM(AR_PHY_RF_CTL4_TX_END_XPAB_OFF, modal->txEndToXpaOff);
    366      1.1  christos 	reg |= SM(AR_PHY_RF_CTL4_FRAME_XPAA_ON, modal->txFrameToXpaOn);
    367      1.1  christos 	reg |= SM(AR_PHY_RF_CTL4_FRAME_XPAB_ON, modal->txFrameToXpaOn);
    368      1.1  christos 	AR_WRITE(sc, AR_PHY_RF_CTL4, reg);
    369      1.1  christos 
    370      1.1  christos 	reg = AR_READ(sc, AR_PHY_RF_CTL3);
    371      1.1  christos 	reg = RW(reg, AR_PHY_TX_END_TO_A2_RX_ON, modal->txEndToRxOn);
    372      1.1  christos 	AR_WRITE(sc, AR_PHY_RF_CTL3, reg);
    373      1.1  christos 
    374      1.1  christos 	reg = AR_READ(sc, AR_PHY_CCA(0));
    375      1.1  christos 	reg = RW(reg, AR9280_PHY_CCA_THRESH62, modal->thresh62);
    376      1.1  christos 	AR_WRITE(sc, AR_PHY_CCA(0), reg);
    377      1.1  christos 
    378      1.1  christos 	reg = AR_READ(sc, AR_PHY_EXT_CCA0);
    379      1.1  christos 	reg = RW(reg, AR_PHY_EXT_CCA0_THRESH62, modal->thresh62);
    380      1.1  christos 	AR_WRITE(sc, AR_PHY_EXT_CCA0, reg);
    381      1.1  christos 
    382      1.1  christos 	if (sc->sc_eep_rev >= AR_EEP_MINOR_VER_2) {
    383      1.1  christos 		reg = AR_READ(sc, AR_PHY_RF_CTL2);
    384      1.1  christos 		reg = RW(reg, AR_PHY_TX_END_PA_ON,
    385      1.1  christos 		    modal->txFrameToPaOn);
    386      1.1  christos 		reg = RW(reg, AR_PHY_TX_END_DATA_START,
    387      1.1  christos 		    modal->txFrameToDataStart);
    388      1.1  christos 		AR_WRITE(sc, AR_PHY_RF_CTL2, reg);
    389      1.1  christos 	}
    390      1.1  christos #ifndef IEEE80211_NO_HT
    391      1.1  christos 	if (sc->sc_eep_rev >= AR_EEP_MINOR_VER_3 && extc != NULL) {
    392      1.1  christos 		reg = AR_READ(sc, AR_PHY_SETTLING);
    393      1.1  christos 		reg = RW(reg, AR_PHY_SETTLING_SWITCH, modal->swSettleHt40);
    394      1.1  christos 		AR_WRITE(sc, AR_PHY_SETTLING, reg);
    395      1.1  christos 	}
    396      1.1  christos #endif
    397      1.1  christos 	AR_WRITE_BARRIER(sc);
    398      1.1  christos }
    399      1.1  christos 
    400      1.1  christos PUBLIC void
    401      1.1  christos ar9285_pa_calib(struct athn_softc *sc)
    402      1.1  christos {
    403      1.1  christos 	/* List of registers that need to be saved/restored. */
    404      1.1  christos 	static const uint16_t regs[] = {
    405      1.1  christos 		AR9285_AN_TOP3,
    406      1.1  christos 		AR9285_AN_RXTXBB1,
    407      1.1  christos 		AR9285_AN_RF2G1,
    408      1.1  christos 		AR9285_AN_RF2G2,
    409      1.1  christos 		AR9285_AN_TOP2,
    410      1.1  christos 		AR9285_AN_RF2G8,
    411      1.1  christos 		AR9285_AN_RF2G7
    412      1.1  christos 	};
    413      1.1  christos 	uint32_t svg[7], reg, ccomp_svg;
    414      1.1  christos 	size_t i;
    415      1.1  christos 
    416      1.1  christos 	/* No PA calibration needed for high power solutions. */
    417      1.1  christos 	if (AR_SREV_9285(sc) &&
    418      1.1  christos 	    ((struct ar9285_base_eep_header *)sc->sc_eep)->txGainType ==
    419      1.1  christos 	     AR_EEP_TXGAIN_HIGH_POWER)	/* XXX AR9287? */
    420      1.1  christos 		return;
    421      1.1  christos 
    422      1.1  christos 	/* Save registers. */
    423      1.1  christos 	for (i = 0; i < __arraycount(regs); i++)
    424      1.1  christos 		svg[i] = AR_READ(sc, regs[i]);
    425      1.1  christos 
    426      1.1  christos 	AR_CLRBITS(sc, AR9285_AN_RF2G6, 1);
    427      1.1  christos 	AR_SETBITS(sc, AR_PHY(2), 1 << 27);
    428      1.1  christos 
    429      1.1  christos 	AR_SETBITS(sc, AR9285_AN_TOP3, AR9285_AN_TOP3_PWDDAC);
    430      1.1  christos 	AR_SETBITS(sc, AR9285_AN_RXTXBB1, AR9285_AN_RXTXBB1_PDRXTXBB1);
    431      1.1  christos 	AR_SETBITS(sc, AR9285_AN_RXTXBB1, AR9285_AN_RXTXBB1_PDV2I);
    432      1.1  christos 	AR_SETBITS(sc, AR9285_AN_RXTXBB1, AR9285_AN_RXTXBB1_PDDACIF);
    433      1.1  christos 	AR_CLRBITS(sc, AR9285_AN_RF2G2, AR9285_AN_RF2G2_OFFCAL);
    434      1.1  christos 	AR_CLRBITS(sc, AR9285_AN_RF2G7, AR9285_AN_RF2G7_PWDDB);
    435      1.1  christos 	AR_CLRBITS(sc, AR9285_AN_RF2G1, AR9285_AN_RF2G1_ENPACAL);
    436      1.1  christos 	/* Power down PA drivers. */
    437      1.1  christos 	AR_CLRBITS(sc, AR9285_AN_RF2G1, AR9285_AN_RF2G1_PDPADRV1);
    438      1.1  christos 	AR_CLRBITS(sc, AR9285_AN_RF2G1, AR9285_AN_RF2G1_PDPADRV2);
    439      1.1  christos 	AR_CLRBITS(sc, AR9285_AN_RF2G1, AR9285_AN_RF2G1_PDPAOUT);
    440      1.1  christos 
    441      1.1  christos 	reg = AR_READ(sc, AR9285_AN_RF2G8);
    442      1.1  christos 	reg = RW(reg, AR9285_AN_RF2G8_PADRVGN2TAB0, 7);
    443      1.1  christos 	AR_WRITE(sc, AR9285_AN_RF2G8, reg);
    444      1.1  christos 
    445      1.1  christos 	reg = AR_READ(sc, AR9285_AN_RF2G7);
    446      1.1  christos 	reg = RW(reg, AR9285_AN_RF2G7_PADRVGN2TAB0, 0);
    447      1.1  christos 	AR_WRITE(sc, AR9285_AN_RF2G7, reg);
    448      1.1  christos 
    449      1.1  christos 	reg = AR_READ(sc, AR9285_AN_RF2G6);
    450      1.1  christos 	/* Save compensation capacitor value. */
    451      1.1  christos 	ccomp_svg = MS(reg, AR9285_AN_RF2G6_CCOMP);
    452      1.1  christos 	/* Program compensation capacitor for dynamic PA. */
    453      1.1  christos 	reg = RW(reg, AR9285_AN_RF2G6_CCOMP, 0xf);
    454      1.1  christos 	AR_WRITE(sc, AR9285_AN_RF2G6, reg);
    455      1.1  christos 
    456      1.1  christos 	AR_WRITE(sc, AR9285_AN_TOP2, AR9285_AN_TOP2_DEFAULT);
    457      1.1  christos 	AR_WRITE_BARRIER(sc);
    458      1.1  christos 	DELAY(30);
    459      1.1  christos 
    460      1.1  christos 	/* Clear offsets 6-1. */
    461      1.1  christos 	AR_CLRBITS(sc, AR9285_AN_RF2G6, AR9285_AN_RF2G6_OFFS_6_1);
    462      1.1  christos 	/* Clear offset 0. */
    463      1.1  christos 	AR_CLRBITS(sc, AR9285_AN_RF2G3, AR9285_AN_RF2G3_PDVCCOMP);
    464      1.1  christos 	/* Set offsets 6-1. */
    465      1.1  christos 	for (i = 6; i >= 1; i--) {
    466      1.1  christos 		AR_SETBITS(sc, AR9285_AN_RF2G6, AR9285_AN_RF2G6_OFFS(i));
    467      1.1  christos 		AR_WRITE_BARRIER(sc);
    468      1.1  christos 		DELAY(1);
    469      1.1  christos 		if (AR_READ(sc, AR9285_AN_RF2G9) & AR9285_AN_RXTXBB1_SPARE9) {
    470      1.1  christos 			AR_SETBITS(sc, AR9285_AN_RF2G6,
    471      1.1  christos 			    AR9285_AN_RF2G6_OFFS(i));
    472      1.1  christos 		}
    473      1.1  christos 		else {
    474      1.1  christos 			AR_CLRBITS(sc, AR9285_AN_RF2G6,
    475      1.1  christos 			    AR9285_AN_RF2G6_OFFS(i));
    476      1.1  christos 		}
    477      1.1  christos 	}
    478      1.1  christos 	/* Set offset 0. */
    479      1.1  christos 	AR_SETBITS(sc, AR9285_AN_RF2G3, AR9285_AN_RF2G3_PDVCCOMP);
    480      1.1  christos 	AR_WRITE_BARRIER(sc);
    481      1.1  christos 	DELAY(1);
    482      1.1  christos 	if (AR_READ(sc, AR9285_AN_RF2G9) & AR9285_AN_RXTXBB1_SPARE9)
    483      1.1  christos 		AR_SETBITS(sc, AR9285_AN_RF2G3, AR9285_AN_RF2G3_PDVCCOMP);
    484      1.1  christos 	else
    485      1.1  christos 		AR_CLRBITS(sc, AR9285_AN_RF2G3, AR9285_AN_RF2G3_PDVCCOMP);
    486      1.1  christos 
    487      1.1  christos 	AR_WRITE_BARRIER(sc);
    488      1.1  christos 
    489      1.1  christos 	AR_SETBITS(sc, AR9285_AN_RF2G6, 1);
    490      1.1  christos 	AR_CLRBITS(sc, AR_PHY(2), 1 << 27);
    491      1.1  christos 
    492      1.1  christos 	/* Restore registers. */
    493      1.1  christos 	for (i = 0; i < __arraycount(regs); i++)
    494      1.1  christos 		AR_WRITE(sc, regs[i], svg[i]);
    495      1.1  christos 
    496      1.1  christos 	/* Restore compensation capacitor value. */
    497      1.1  christos 	reg = AR_READ(sc, AR9285_AN_RF2G6);
    498      1.1  christos 	reg = RW(reg, AR9285_AN_RF2G6_CCOMP, ccomp_svg);
    499      1.1  christos 	AR_WRITE(sc, AR9285_AN_RF2G6, reg);
    500      1.1  christos 	AR_WRITE_BARRIER(sc);
    501      1.1  christos }
    502      1.1  christos 
    503      1.1  christos PUBLIC void
    504      1.1  christos ar9271_pa_calib(struct athn_softc *sc)
    505      1.1  christos {
    506      1.1  christos #if NATHN_USB > 0
    507      1.1  christos 	/* List of registers that need to be saved/restored. */
    508      1.1  christos 	static const uint16_t regs[] = {
    509      1.1  christos 		AR9285_AN_TOP3,
    510      1.1  christos 		AR9285_AN_RXTXBB1,
    511      1.1  christos 		AR9285_AN_RF2G1,
    512      1.1  christos 		AR9285_AN_RF2G2,
    513      1.1  christos 		AR9285_AN_TOP2,
    514      1.1  christos 		AR9285_AN_RF2G8,
    515      1.1  christos 		AR9285_AN_RF2G7
    516      1.1  christos 	};
    517      1.1  christos 	uint32_t svg[7], reg, rf2g3_svg;
    518      1.1  christos 	size_t i;
    519      1.1  christos 
    520      1.1  christos 	/* Save registers. */
    521      1.1  christos 	for (i = 0; i < __arraycount(regs); i++)
    522      1.1  christos 		svg[i] = AR_READ(sc, regs[i]);
    523      1.1  christos 
    524      1.1  christos 	AR_CLRBITS(sc, AR9285_AN_RF2G6, 1);
    525      1.1  christos 	AR_SETBITS(sc, AR_PHY(2), 1 << 27);
    526      1.1  christos 
    527      1.1  christos 	AR_SETBITS(sc, AR9285_AN_TOP3, AR9285_AN_TOP3_PWDDAC);
    528      1.1  christos 	AR_SETBITS(sc, AR9285_AN_RXTXBB1, AR9285_AN_RXTXBB1_PDRXTXBB1);
    529      1.1  christos 	AR_SETBITS(sc, AR9285_AN_RXTXBB1, AR9285_AN_RXTXBB1_PDV2I);
    530      1.1  christos 	AR_SETBITS(sc, AR9285_AN_RXTXBB1, AR9285_AN_RXTXBB1_PDDACIF);
    531      1.1  christos 	AR_CLRBITS(sc, AR9285_AN_RF2G2, AR9285_AN_RF2G2_OFFCAL);
    532      1.1  christos 	AR_CLRBITS(sc, AR9285_AN_RF2G7, AR9285_AN_RF2G7_PWDDB);
    533      1.1  christos 	AR_CLRBITS(sc, AR9285_AN_RF2G1, AR9285_AN_RF2G1_ENPACAL);
    534      1.1  christos 	/* Power down PA drivers. */
    535      1.1  christos 	AR_CLRBITS(sc, AR9285_AN_RF2G1, AR9285_AN_RF2G1_PDPADRV1);
    536      1.1  christos 	AR_CLRBITS(sc, AR9285_AN_RF2G1, AR9285_AN_RF2G1_PDPADRV2);
    537      1.1  christos 	AR_CLRBITS(sc, AR9285_AN_RF2G1, AR9285_AN_RF2G1_PDPAOUT);
    538      1.1  christos 
    539      1.1  christos 	reg = AR_READ(sc, AR9285_AN_RF2G8);
    540      1.1  christos 	reg = RW(reg, AR9285_AN_RF2G8_PADRVGN2TAB0, 7);
    541      1.1  christos 	AR_WRITE(sc, AR9285_AN_RF2G8, reg);
    542      1.1  christos 
    543      1.1  christos 	reg = AR_READ(sc, AR9285_AN_RF2G7);
    544      1.1  christos 	reg = RW(reg, AR9285_AN_RF2G7_PADRVGN2TAB0, 0);
    545      1.1  christos 	AR_WRITE(sc, AR9285_AN_RF2G7, reg);
    546      1.1  christos 
    547      1.1  christos 	/* Save compensation capacitor value. */
    548      1.1  christos 	reg = rf2g3_svg = AR_READ(sc, AR9285_AN_RF2G3);
    549      1.1  christos 	/* Program compensation capacitor for dynamic PA. */
    550      1.1  christos 	reg = RW(reg, AR9271_AN_RF2G3_CCOMP, 0xfff);
    551      1.1  christos 	AR_WRITE(sc, AR9285_AN_RF2G3, reg);
    552      1.1  christos 
    553      1.1  christos 	AR_WRITE(sc, AR9285_AN_TOP2, AR9285_AN_TOP2_DEFAULT);
    554      1.1  christos 	AR_WRITE_BARRIER(sc);
    555      1.1  christos 	DELAY(30);
    556      1.1  christos 
    557      1.1  christos 	/* Clear offsets 6-0. */
    558      1.1  christos 	AR_CLRBITS(sc, AR9285_AN_RF2G6, AR9271_AN_RF2G6_OFFS_6_0);
    559      1.1  christos 	/* Set offsets 6-1. */
    560      1.1  christos 	for (i = 6; i >= 1; i--) {
    561      1.1  christos 		reg = AR_READ(sc, AR9285_AN_RF2G6);
    562      1.1  christos 		reg |= AR9271_AN_RF2G6_OFFS(i);
    563      1.1  christos 		AR_WRITE(sc, AR9285_AN_RF2G6, reg);
    564      1.1  christos 		AR_WRITE_BARRIER(sc);
    565      1.1  christos 		DELAY(1);
    566      1.1  christos 		if (!(AR_READ(sc, AR9285_AN_RF2G9) & AR9285_AN_RXTXBB1_SPARE9))
    567      1.1  christos 			reg &= ~AR9271_AN_RF2G6_OFFS(i);
    568      1.1  christos 		AR_WRITE(sc, AR9285_AN_RF2G6, reg);
    569      1.1  christos 	}
    570      1.1  christos 	AR_WRITE_BARRIER(sc);
    571      1.1  christos 
    572      1.1  christos 	AR_SETBITS(sc, AR9285_AN_RF2G6, 1);
    573      1.1  christos 	AR_CLRBITS(sc, AR_PHY(2), 1 << 27);
    574      1.1  christos 
    575      1.1  christos 	/* Restore registers. */
    576      1.1  christos 	for (i = 0; i < __arraycount(regs); i++)
    577      1.1  christos 		AR_WRITE(sc, regs[i], svg[i]);
    578      1.1  christos 
    579      1.1  christos 	/* Restore compensation capacitor value. */
    580      1.1  christos 	AR_WRITE(sc, AR9285_AN_RF2G3, rf2g3_svg);
    581      1.1  christos 	AR_WRITE_BARRIER(sc);
    582      1.1  christos #endif	/* NATHN_USB */
    583      1.1  christos }
    584      1.1  christos 
    585      1.1  christos /*
    586      1.1  christos  * Carrier Leakage Calibration.
    587      1.1  christos  */
    588      1.1  christos int
    589      1.1  christos ar9285_cl_cal(struct athn_softc *sc, struct ieee80211_channel *c,
    590      1.1  christos     struct ieee80211_channel *extc)
    591      1.1  christos {
    592      1.1  christos 	int ntries;
    593      1.1  christos 
    594      1.1  christos 	AR_SETBITS(sc, AR_PHY_CL_CAL_CTL, AR_PHY_CL_CAL_ENABLE);
    595      1.1  christos #ifndef IEEE80211_NO_HT
    596      1.1  christos 	if (0 && extc == NULL) {	/* XXX IS_CHAN_HT20!! */
    597      1.1  christos 		AR_SETBITS(sc, AR_PHY_CL_CAL_CTL, AR_PHY_PARALLEL_CAL_ENABLE);
    598      1.1  christos 		AR_SETBITS(sc, AR_PHY_TURBO, AR_PHY_FC_DYN2040_EN);
    599      1.1  christos 		AR_CLRBITS(sc, AR_PHY_AGC_CONTROL,
    600      1.1  christos 		    AR_PHY_AGC_CONTROL_FLTR_CAL);
    601      1.1  christos 		AR_CLRBITS(sc, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_CAL_ENABLE);
    602      1.1  christos 		AR_SETBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL);
    603      1.1  christos 		for (ntries = 0; ntries < 10000; ntries++) {
    604      1.1  christos 			if (!(AR_READ(sc, AR_PHY_AGC_CONTROL) &
    605      1.1  christos 			    AR_PHY_AGC_CONTROL_CAL))
    606      1.1  christos 				break;
    607      1.1  christos 			DELAY(10);
    608      1.1  christos 		}
    609      1.1  christos 		if (ntries == 10000)
    610      1.1  christos 			return ETIMEDOUT;
    611      1.1  christos 		AR_CLRBITS(sc, AR_PHY_TURBO, AR_PHY_FC_DYN2040_EN);
    612      1.1  christos 		AR_CLRBITS(sc, AR_PHY_CL_CAL_CTL, AR_PHY_PARALLEL_CAL_ENABLE);
    613      1.1  christos 		AR_CLRBITS(sc, AR_PHY_CL_CAL_CTL, AR_PHY_CL_CAL_ENABLE);
    614      1.1  christos 	}
    615      1.1  christos #endif
    616      1.1  christos 	AR_CLRBITS(sc, AR_PHY_ADC_CTL, AR_PHY_ADC_CTL_OFF_PWDADC);
    617      1.1  christos 	AR_SETBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_FLTR_CAL);
    618      1.1  christos 	AR_SETBITS(sc, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_CAL_ENABLE);
    619      1.1  christos 	AR_SETBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL);
    620      1.1  christos 	for (ntries = 0; ntries < 10000; ntries++) {
    621      1.1  christos 		if (!(AR_READ(sc, AR_PHY_AGC_CONTROL) &
    622      1.1  christos 		    AR_PHY_AGC_CONTROL_CAL))
    623      1.1  christos 			break;
    624      1.1  christos 		DELAY(10);
    625      1.1  christos 	}
    626      1.1  christos 	if (ntries == 10000)
    627      1.1  christos 		return ETIMEDOUT;
    628      1.1  christos 	AR_SETBITS(sc, AR_PHY_ADC_CTL, AR_PHY_ADC_CTL_OFF_PWDADC);
    629      1.1  christos 	AR_CLRBITS(sc, AR_PHY_CL_CAL_CTL, AR_PHY_CL_CAL_ENABLE);
    630      1.1  christos 	AR_CLRBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_FLTR_CAL);
    631      1.1  christos 	AR_WRITE_BARRIER(sc);
    632      1.1  christos 	return 0;
    633      1.1  christos }
    634      1.1  christos 
    635      1.1  christos PUBLIC void
    636      1.1  christos ar9271_load_ani(struct athn_softc *sc)
    637      1.1  christos {
    638      1.1  christos 
    639      1.1  christos #if NATHN_USB > 0
    640      1.1  christos 	/* Write ANI registers. */
    641      1.1  christos 	AR_WRITE(sc, AR_PHY_DESIRED_SZ, 0x6d4000e2);
    642      1.1  christos 	AR_WRITE(sc, AR_PHY_AGC_CTL1,   0x3139605e);
    643      1.1  christos 	AR_WRITE(sc, AR_PHY_FIND_SIG,   0x7ec84d2e);
    644      1.1  christos 	AR_WRITE(sc, AR_PHY_SFCORR_LOW, 0x06903881);
    645      1.1  christos 	AR_WRITE(sc, AR_PHY_SFCORR,     0x5ac640d0);
    646      1.1  christos 	AR_WRITE(sc, AR_PHY_CCK_DETECT, 0x803e68c8);
    647      1.1  christos 	AR_WRITE(sc, AR_PHY_TIMING5,    0xd00a8007);
    648      1.1  christos 	AR_WRITE(sc, AR_PHY_SFCORR_EXT, 0x05eea6d4);
    649      1.1  christos 	AR_WRITE_BARRIER(sc);
    650      1.1  christos #endif	/* NATHN_USB */
    651      1.1  christos }
    652      1.1  christos 
    653      1.1  christos int
    654      1.1  christos ar9285_init_calib(struct athn_softc *sc, struct ieee80211_channel *c,
    655      1.1  christos     struct ieee80211_channel *extc)
    656      1.1  christos {
    657      1.1  christos 	uint32_t reg, mask, clcgain, rf2g5_svg;
    658      1.1  christos 	int i, maxgain, nclcs, thresh, error;
    659      1.1  christos 
    660      1.1  christos 	/* Do carrier leakage calibration. */
    661      1.1  christos 	if ((error = ar9285_cl_cal(sc, c, extc)) != 0)
    662      1.1  christos 		return error;
    663      1.1  christos 
    664      1.1  christos 	/* Workaround for high temperature is not applicable on AR9271. */
    665      1.1  christos 	if (AR_SREV_9271(sc))
    666      1.1  christos 		return 0;
    667      1.1  christos 
    668      1.1  christos 	mask = 0;
    669      1.1  christos 	nclcs = 0;
    670      1.1  christos 	reg = AR_READ(sc, AR_PHY_TX_PWRCTRL7);
    671      1.1  christos 	maxgain = MS(reg, AR_PHY_TX_PWRCTRL_TX_GAIN_TAB_MAX);
    672      1.1  christos 	for (i = 0; i <= maxgain; i++) {
    673      1.1  christos 		reg = AR_READ(sc, AR_PHY_TX_GAIN_TBL(i));
    674      1.1  christos 		clcgain = MS(reg, AR_PHY_TX_GAIN_CLC);
    675      1.1  christos 		/* NB: clcgain <= 0xf. */
    676      1.1  christos 		if (!(mask & (1 << clcgain))) {
    677      1.1  christos 			mask |= 1 << clcgain;
    678      1.1  christos 			nclcs++;
    679      1.1  christos 		}
    680      1.1  christos 	}
    681      1.1  christos 	thresh = 0;
    682      1.1  christos 	for (i = 0; i < nclcs; i++) {
    683      1.1  christos 		reg = AR_READ(sc, AR_PHY_CLC_TBL(i));
    684      1.1  christos 		if (MS(reg, AR_PHY_CLC_I0) == 0)
    685      1.1  christos 			thresh++;
    686      1.1  christos 		if (MS(reg, AR_PHY_CLC_Q0) == 0)
    687      1.1  christos 			thresh++;
    688      1.1  christos 	}
    689      1.1  christos 	if (thresh <= AR9285_CL_CAL_REDO_THRESH)
    690      1.1  christos 		return 0;	/* No need to redo. */
    691      1.1  christos 
    692      1.1  christos 	/* Threshold reached, redo carrier leakage calibration. */
    693      1.1  christos 	DPRINTFN(DBG_INIT, sc, "CLC threshold=%d\n", thresh);
    694      1.1  christos 	rf2g5_svg = reg = AR_READ(sc, AR9285_AN_RF2G5);
    695      1.1  christos 	if ((AR_READ(sc, AR_AN_SYNTH9) & 0x7) == 0x1)	/* XE rev. */
    696      1.1  christos 		reg = RW(reg, AR9285_AN_RF2G5_IC50TX, 0x5);
    697      1.1  christos 	else
    698      1.1  christos 		reg = RW(reg, AR9285_AN_RF2G5_IC50TX, 0x4);
    699      1.1  christos 	AR_WRITE(sc, AR9285_AN_RF2G5, reg);
    700      1.1  christos 	AR_WRITE_BARRIER(sc);
    701      1.1  christos 	error = ar9285_cl_cal(sc, c, extc);
    702      1.1  christos 	AR_WRITE(sc, AR9285_AN_RF2G5, rf2g5_svg);
    703      1.1  christos 	AR_WRITE_BARRIER(sc);
    704      1.1  christos 	return error;
    705      1.1  christos }
    706      1.1  christos 
    707      1.1  christos Static void
    708      1.1  christos ar9285_get_pdadcs(struct athn_softc *sc, struct ieee80211_channel *c,
    709      1.1  christos     int nxpdgains, uint8_t overlap, uint8_t *boundaries, uint8_t *pdadcs)
    710      1.1  christos {
    711      1.1  christos 	const struct ar9285_eeprom *eep = sc->sc_eep;
    712      1.1  christos 	const struct ar9285_cal_data_per_freq *pierdata;
    713      1.1  christos 	const uint8_t *pierfreq;
    714      1.1  christos 	struct athn_pier lopier, hipier;
    715      1.1  christos 	uint8_t fbin;
    716      1.1  christos 	int i, lo, hi, npiers;
    717      1.1  christos 
    718      1.1  christos 	pierfreq = eep->calFreqPier2G;
    719      1.1  christos 	pierdata = eep->calPierData2G;
    720      1.1  christos 	npiers = AR9285_NUM_2G_CAL_PIERS;
    721      1.1  christos 
    722      1.1  christos 	/* Find channel in ROM pier table. */
    723      1.1  christos 	fbin = athn_chan2fbin(c);
    724      1.1  christos 	athn_get_pier_ival(fbin, pierfreq, npiers, &lo, &hi);
    725      1.1  christos 
    726      1.1  christos 	lopier.fbin = pierfreq[lo];
    727      1.1  christos 	hipier.fbin = pierfreq[hi];
    728      1.1  christos 	for (i = 0; i < nxpdgains; i++) {
    729      1.1  christos 		lopier.pwr[i] = pierdata[lo].pwrPdg[i];
    730      1.1  christos 		lopier.vpd[i] = pierdata[lo].vpdPdg[i];
    731      1.1  christos 		hipier.pwr[i] = pierdata[lo].pwrPdg[i];
    732      1.1  christos 		hipier.vpd[i] = pierdata[lo].vpdPdg[i];
    733      1.1  christos 	}
    734      1.1  christos 	ar5008_get_pdadcs(sc, fbin, &lopier, &hipier, nxpdgains,
    735      1.1  christos 	    AR9285_PD_GAIN_ICEPTS, overlap, boundaries, pdadcs);
    736      1.1  christos }
    737      1.1  christos 
    738      1.1  christos Static void
    739      1.1  christos ar9285_set_power_calib(struct athn_softc *sc, struct ieee80211_channel *c)
    740      1.1  christos {
    741      1.1  christos 	const struct ar9285_eeprom *eep = sc->sc_eep;
    742      1.1  christos 	uint8_t boundaries[AR_PD_GAINS_IN_MASK];
    743      1.1  christos 	uint8_t pdadcs[AR_NUM_PDADC_VALUES];
    744      1.1  christos 	uint8_t xpdgains[AR9285_NUM_PD_GAINS];
    745      1.1  christos 	uint8_t overlap;
    746      1.1  christos 	uint32_t reg;
    747      1.1  christos 	int i, nxpdgains;
    748      1.1  christos 
    749      1.1  christos 	if (sc->sc_eep_rev < AR_EEP_MINOR_VER_2) {
    750      1.1  christos 		overlap = MS(AR_READ(sc, AR_PHY_TPCRG5),
    751      1.1  christos 		    AR_PHY_TPCRG5_PD_GAIN_OVERLAP);
    752      1.1  christos 	}
    753      1.1  christos 	else
    754      1.1  christos 		overlap = eep->modalHeader.pdGainOverlap;
    755      1.1  christos 
    756      1.1  christos 	nxpdgains = 0;
    757      1.1  christos 	memset(xpdgains, 0, sizeof(xpdgains));
    758      1.1  christos 	for (i = AR9285_PD_GAINS_IN_MASK - 1; i >= 0; i--) {
    759      1.1  christos 		if (nxpdgains >= AR9285_NUM_PD_GAINS)
    760      1.1  christos 			break;
    761      1.1  christos 		if (eep->modalHeader.xpdGain & (1 << i))
    762      1.1  christos 			xpdgains[nxpdgains++] = i;
    763      1.1  christos 	}
    764      1.1  christos 	reg = AR_READ(sc, AR_PHY_TPCRG1);
    765      1.1  christos 	reg = RW(reg, AR_PHY_TPCRG1_NUM_PD_GAIN, nxpdgains - 1);
    766      1.1  christos 	reg = RW(reg, AR_PHY_TPCRG1_PD_GAIN_1, xpdgains[0]);
    767      1.1  christos 	reg = RW(reg, AR_PHY_TPCRG1_PD_GAIN_2, xpdgains[1]);
    768      1.1  christos 	AR_WRITE(sc, AR_PHY_TPCRG1, reg);
    769      1.1  christos 
    770      1.1  christos 	/* NB: No open loop power control for AR9285. */
    771      1.1  christos 	ar9285_get_pdadcs(sc, c, nxpdgains, overlap, boundaries, pdadcs);
    772      1.1  christos 
    773      1.1  christos 	/* Write boundaries. */
    774      1.1  christos 	reg  = SM(AR_PHY_TPCRG5_PD_GAIN_OVERLAP, overlap);
    775      1.1  christos 	reg |= SM(AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_1, boundaries[0]);
    776      1.1  christos 	reg |= SM(AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_2, boundaries[1]);
    777      1.1  christos 	reg |= SM(AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_3, boundaries[2]);
    778      1.1  christos 	reg |= SM(AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_4, boundaries[3]);
    779      1.1  christos 	AR_WRITE(sc, AR_PHY_TPCRG5, reg);
    780      1.1  christos 
    781      1.1  christos 	/* Write PDADC values. */
    782      1.1  christos 	for (i = 0; i < AR_NUM_PDADC_VALUES; i += 4) {
    783      1.1  christos 		AR_WRITE(sc, AR_PHY_PDADC_TBL_BASE + i,
    784      1.1  christos 		    pdadcs[i + 0] <<  0 |
    785      1.1  christos 		    pdadcs[i + 1] <<  8 |
    786      1.1  christos 		    pdadcs[i + 2] << 16 |
    787      1.1  christos 		    pdadcs[i + 3] << 24);
    788      1.1  christos 	}
    789      1.1  christos 	AR_WRITE_BARRIER(sc);
    790      1.1  christos }
    791      1.1  christos 
    792      1.1  christos Static void
    793      1.1  christos ar9285_set_txpower(struct athn_softc *sc, struct ieee80211_channel *c,
    794      1.1  christos     struct ieee80211_channel *extc)
    795      1.1  christos {
    796      1.1  christos 	const struct ar9285_eeprom *eep = sc->sc_eep;
    797  1.2.6.1     rmind #ifdef notyet
    798      1.1  christos 	const struct ar9285_modal_eep_header *modal = &eep->modalHeader;
    799  1.2.6.1     rmind #endif
    800      1.1  christos 	uint8_t tpow_cck[4], tpow_ofdm[4];
    801      1.1  christos #ifndef IEEE80211_NO_HT
    802      1.1  christos 	uint8_t tpow_cck_ext[4], tpow_ofdm_ext[4];
    803      1.1  christos 	uint8_t tpow_ht20[8], tpow_ht40[8];
    804      1.1  christos 	uint8_t ht40inc;
    805      1.1  christos #endif
    806  1.2.6.1     rmind 	int16_t power[ATHN_POWER_COUNT];
    807      1.1  christos 	int i;
    808      1.1  christos 
    809      1.1  christos 	ar9285_set_power_calib(sc, c);
    810      1.1  christos 
    811  1.2.6.1     rmind #ifdef notyet
    812      1.1  christos 	/* Compute transmit power reduction due to antenna gain. */
    813  1.2.6.1     rmind 	uint16_t max_ant_gain = modal->antennaGain;
    814      1.1  christos 	/* XXX */
    815  1.2.6.1     rmind #endif
    816      1.1  christos 
    817      1.1  christos 	/* Get CCK target powers. */
    818      1.1  christos 	ar5008_get_lg_tpow(sc, c, AR_CTL_11B, eep->calTargetPowerCck,
    819      1.1  christos 	    AR9285_NUM_2G_CCK_TARGET_POWERS, tpow_cck);
    820      1.1  christos 
    821      1.1  christos 	/* Get OFDM target powers. */
    822      1.1  christos 	ar5008_get_lg_tpow(sc, c, AR_CTL_11G, eep->calTargetPower2G,
    823      1.1  christos 	    AR9285_NUM_2G_20_TARGET_POWERS, tpow_ofdm);
    824      1.1  christos 
    825      1.1  christos #ifndef IEEE80211_NO_HT
    826      1.1  christos 	/* Get HT-20 target powers. */
    827      1.1  christos 	ar5008_get_ht_tpow(sc, c, AR_CTL_2GHT20, eep->calTargetPower2GHT20,
    828      1.1  christos 	    AR9285_NUM_2G_20_TARGET_POWERS, tpow_ht20);
    829      1.1  christos 
    830      1.1  christos 	if (extc != NULL) {
    831      1.1  christos 		/* Get HT-40 target powers. */
    832      1.1  christos 		ar5008_get_ht_tpow(sc, c, AR_CTL_2GHT40,
    833      1.1  christos 		    eep->calTargetPower2GHT40, AR9285_NUM_2G_40_TARGET_POWERS,
    834      1.1  christos 		    tpow_ht40);
    835      1.1  christos 
    836      1.1  christos 		/* Get secondary channel CCK target powers. */
    837      1.1  christos 		ar5008_get_lg_tpow(sc, extc, AR_CTL_11B,
    838      1.1  christos 		    eep->calTargetPowerCck, AR9285_NUM_2G_CCK_TARGET_POWERS,
    839      1.1  christos 		    tpow_cck_ext);
    840      1.1  christos 
    841      1.1  christos 		/* Get secondary channel OFDM target powers. */
    842      1.1  christos 		ar5008_get_lg_tpow(sc, extc, AR_CTL_11G,
    843      1.1  christos 		    eep->calTargetPower2G, AR9285_NUM_2G_20_TARGET_POWERS,
    844      1.1  christos 		    tpow_ofdm_ext);
    845      1.1  christos 	}
    846      1.1  christos #endif
    847      1.1  christos 
    848      1.1  christos 	memset(power, 0, sizeof(power));
    849      1.1  christos 	/* Shuffle target powers accross transmit rates. */
    850      1.1  christos 	power[ATHN_POWER_OFDM6   ] =
    851      1.1  christos 	power[ATHN_POWER_OFDM9   ] =
    852      1.1  christos 	power[ATHN_POWER_OFDM12  ] =
    853      1.1  christos 	power[ATHN_POWER_OFDM18  ] =
    854      1.1  christos 	power[ATHN_POWER_OFDM24  ] = tpow_ofdm[0];
    855      1.1  christos 	power[ATHN_POWER_OFDM36  ] = tpow_ofdm[1];
    856      1.1  christos 	power[ATHN_POWER_OFDM48  ] = tpow_ofdm[2];
    857      1.1  christos 	power[ATHN_POWER_OFDM54  ] = tpow_ofdm[3];
    858      1.1  christos 	power[ATHN_POWER_XR      ] = tpow_ofdm[0];
    859      1.1  christos 	power[ATHN_POWER_CCK1_LP ] = tpow_cck[0];
    860      1.1  christos 	power[ATHN_POWER_CCK2_LP ] =
    861      1.1  christos 	power[ATHN_POWER_CCK2_SP ] = tpow_cck[1];
    862      1.1  christos 	power[ATHN_POWER_CCK55_LP] =
    863      1.1  christos 	power[ATHN_POWER_CCK55_SP] = tpow_cck[2];
    864      1.1  christos 	power[ATHN_POWER_CCK11_LP] =
    865      1.1  christos 	power[ATHN_POWER_CCK11_SP] = tpow_cck[3];
    866      1.1  christos #ifndef IEEE80211_NO_HT
    867      1.1  christos 	for (i = 0; i < __arraycount(tpow_ht20); i++)
    868      1.1  christos 		power[ATHN_POWER_HT20(i)] = tpow_ht20[i];
    869      1.1  christos 	if (extc != NULL) {
    870      1.1  christos 		/* Correct PAR difference between HT40 and HT20/Legacy. */
    871      1.1  christos 		if (sc->sc_eep_rev >= AR_EEP_MINOR_VER_2)
    872      1.1  christos 			ht40inc = modal->ht40PowerIncForPdadc;
    873      1.1  christos 		else
    874      1.1  christos 			ht40inc = AR_HT40_POWER_INC_FOR_PDADC;
    875      1.1  christos 		for (i = 0; i < __arraycount(tpow_ht40); i++)
    876      1.1  christos 			power[ATHN_POWER_HT40(i)] = tpow_ht40[i] + ht40inc;
    877      1.1  christos 		power[ATHN_POWER_OFDM_DUP] = tpow_ht40[0];
    878      1.1  christos 		power[ATHN_POWER_CCK_DUP ] = tpow_ht40[0];
    879      1.1  christos 		power[ATHN_POWER_OFDM_EXT] = tpow_ofdm_ext[0];
    880      1.1  christos 		power[ATHN_POWER_CCK_EXT ] = tpow_cck_ext[0];
    881      1.1  christos 	}
    882      1.1  christos #endif
    883      1.1  christos 
    884      1.1  christos 	for (i = 0; i < ATHN_POWER_COUNT; i++) {
    885      1.1  christos 		power[i] -= AR_PWR_TABLE_OFFSET_DB * 2;	/* In half dB. */
    886      1.1  christos 		if (power[i] > AR_MAX_RATE_POWER)
    887      1.1  christos 			power[i] = AR_MAX_RATE_POWER;
    888      1.1  christos 	}
    889      1.1  christos 
    890      1.1  christos 	/* Commit transmit power values to hardware. */
    891      1.1  christos 	ar5008_write_txpower(sc, power);
    892      1.1  christos }
    893