Home | History | Annotate | Line # | Download | only in ic
      1  1.4   thorpej /*	$NetBSD: arn9287.c,v 1.4 2022/09/25 18:43:32 thorpej Exp $	*/
      2  1.1  christos /*	$OpenBSD: ar9287.c,v 1.17 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 AR9227 and AR9287 chipsets.
     24  1.1  christos  */
     25  1.1  christos 
     26  1.1  christos #include <sys/cdefs.h>
     27  1.4   thorpej __KERNEL_RCSID(0, "$NetBSD: arn9287.c,v 1.4 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/arn9280reg.h>
     66  1.1  christos #include <dev/ic/arn9287reg.h>
     67  1.1  christos 
     68  1.1  christos #include <dev/ic/arn5008.h>
     69  1.1  christos #include <dev/ic/arn9280.h>
     70  1.1  christos #include <dev/ic/arn9287.h>
     71  1.1  christos 
     72  1.1  christos #define Static static
     73  1.1  christos 
     74  1.1  christos Static void	ar9287_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 const struct ar_spur_chan *
     78  1.1  christos 		ar9287_get_spur_chans(struct athn_softc *, int);
     79  1.1  christos Static void	ar9287_init_from_rom(struct athn_softc *,
     80  1.1  christos 		    struct ieee80211_channel *, struct ieee80211_channel *);
     81  1.1  christos Static void	ar9287_olpc_get_pdgain(struct athn_softc *,
     82  1.1  christos 		    struct ieee80211_channel *, int, int8_t *);
     83  1.1  christos Static void	ar9287_olpc_init(struct athn_softc *);
     84  1.1  christos Static void	ar9287_olpc_temp_compensation(struct athn_softc *);
     85  1.1  christos Static void	ar9287_set_power_calib(struct athn_softc *,
     86  1.1  christos 		    struct ieee80211_channel *);
     87  1.1  christos Static void	ar9287_set_txpower(struct athn_softc *,
     88  1.1  christos 		    struct ieee80211_channel *, struct ieee80211_channel *);
     89  1.1  christos Static void	ar9287_setup(struct athn_softc *);
     90  1.1  christos Static void	ar9287_swap_rom(struct athn_softc *);
     91  1.1  christos 
     92  1.1  christos PUBLIC int
     93  1.1  christos ar9287_attach(struct athn_softc *sc)
     94  1.1  christos {
     95  1.1  christos 
     96  1.1  christos 	sc->sc_eep_base = AR9287_EEP_START_LOC;
     97  1.1  christos 	sc->sc_eep_size = sizeof(struct ar9287_eeprom);
     98  1.1  christos 	sc->sc_def_nf = AR9287_PHY_CCA_MAX_GOOD_VALUE;
     99  1.1  christos 	sc->sc_ngpiopins = (sc->sc_flags & ATHN_FLAG_USB) ? 16 : 11;
    100  1.1  christos 	sc->sc_led_pin = 8;
    101  1.1  christos 	sc->sc_workaround = AR9285_WA_DEFAULT;
    102  1.1  christos 	sc->sc_ops.setup = ar9287_setup;
    103  1.1  christos 	sc->sc_ops.swap_rom = ar9287_swap_rom;
    104  1.1  christos 	sc->sc_ops.init_from_rom = ar9287_init_from_rom;
    105  1.1  christos 	sc->sc_ops.set_txpower = ar9287_set_txpower;
    106  1.1  christos 	sc->sc_ops.set_synth = ar9280_set_synth;
    107  1.1  christos 	sc->sc_ops.spur_mitigate = ar9280_spur_mitigate;
    108  1.1  christos 	sc->sc_ops.get_spur_chans = ar9287_get_spur_chans;
    109  1.1  christos 	sc->sc_ops.olpc_init = ar9287_olpc_init;
    110  1.1  christos 	sc->sc_ops.olpc_temp_compensation = ar9287_olpc_temp_compensation;
    111  1.1  christos 	sc->sc_ini = &ar9287_1_1_ini;
    112  1.1  christos 	sc->sc_serdes = &ar9280_2_0_serdes;
    113  1.1  christos 
    114  1.1  christos 	return ar5008_attach(sc);
    115  1.1  christos }
    116  1.1  christos 
    117  1.1  christos Static void
    118  1.1  christos ar9287_setup(struct athn_softc *sc)
    119  1.1  christos {
    120  1.1  christos 	const struct ar9287_eeprom *eep = sc->sc_eep;
    121  1.1  christos 
    122  1.1  christos 	/* Determine if open loop power control should be used. */
    123  1.1  christos 	if (eep->baseEepHeader.openLoopPwrCntl)
    124  1.1  christos 		sc->sc_flags |= ATHN_FLAG_OLPC;
    125  1.1  christos 
    126  1.1  christos 	sc->sc_rx_gain = &ar9287_1_1_rx_gain;
    127  1.1  christos 	sc->sc_tx_gain = &ar9287_1_1_tx_gain;
    128  1.1  christos }
    129  1.1  christos 
    130  1.1  christos Static void
    131  1.1  christos ar9287_swap_rom(struct athn_softc *sc)
    132  1.1  christos {
    133  1.1  christos 	struct ar9287_eeprom *eep = sc->sc_eep;
    134  1.1  christos 	int i;
    135  1.1  christos 
    136  1.1  christos 	eep->modalHeader.antCtrlCommon =
    137  1.1  christos 	    bswap32(eep->modalHeader.antCtrlCommon);
    138  1.1  christos 
    139  1.1  christos 	for (i = 0; i < AR9287_MAX_CHAINS; i++) {
    140  1.1  christos 		eep->modalHeader.antCtrlChain[i] =
    141  1.1  christos 		    bswap32(eep->modalHeader.antCtrlChain[i]);
    142  1.1  christos 	}
    143  1.1  christos 	for (i = 0; i < AR_EEPROM_MODAL_SPURS; i++) {
    144  1.1  christos 		eep->modalHeader.spurChans[i].spurChan =
    145  1.1  christos 		    bswap16(eep->modalHeader.spurChans[i].spurChan);
    146  1.1  christos 	}
    147  1.1  christos }
    148  1.1  christos 
    149  1.1  christos Static const struct ar_spur_chan *
    150  1.1  christos ar9287_get_spur_chans(struct athn_softc *sc, int is2ghz)
    151  1.1  christos {
    152  1.1  christos 	const struct ar9287_eeprom *eep = sc->sc_eep;
    153  1.1  christos 
    154  1.1  christos 	KASSERT(is2ghz);
    155  1.1  christos 	return eep->modalHeader.spurChans;
    156  1.1  christos }
    157  1.1  christos 
    158  1.1  christos Static void
    159  1.1  christos ar9287_init_from_rom(struct athn_softc *sc, struct ieee80211_channel *c,
    160  1.1  christos     struct ieee80211_channel *extc)
    161  1.1  christos {
    162  1.1  christos 	const struct ar9287_eeprom *eep = sc->sc_eep;
    163  1.1  christos 	const struct ar9287_modal_eep_header *modal = &eep->modalHeader;
    164  1.1  christos 	uint32_t reg, offset;
    165  1.1  christos 	int i;
    166  1.1  christos 
    167  1.1  christos 	AR_WRITE(sc, AR_PHY_SWITCH_COM, modal->antCtrlCommon);
    168  1.1  christos 
    169  1.1  christos 	for (i = 0; i < AR9287_MAX_CHAINS; i++) {
    170  1.1  christos 		offset = i * 0x1000;
    171  1.1  christos 
    172  1.1  christos 		AR_WRITE(sc, AR_PHY_SWITCH_CHAIN_0 + offset,
    173  1.1  christos 		    modal->antCtrlChain[i]);
    174  1.1  christos 
    175  1.1  christos 		reg = AR_READ(sc, AR_PHY_TIMING_CTRL4_0 + offset);
    176  1.1  christos 		reg = RW(reg, AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF,
    177  1.1  christos 		    modal->iqCalICh[i]);
    178  1.1  christos 		reg = RW(reg, AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF,
    179  1.1  christos 		    modal->iqCalQCh[i]);
    180  1.1  christos 		AR_WRITE(sc, AR_PHY_TIMING_CTRL4_0 + offset, reg);
    181  1.1  christos 
    182  1.1  christos 		reg = AR_READ(sc, AR_PHY_GAIN_2GHZ + offset);
    183  1.1  christos 		reg = RW(reg, AR_PHY_GAIN_2GHZ_XATTEN1_MARGIN,
    184  1.1  christos 		    modal->bswMargin[i]);
    185  1.1  christos 		reg = RW(reg, AR_PHY_GAIN_2GHZ_XATTEN1_DB,
    186  1.1  christos 		    modal->bswAtten[i]);
    187  1.1  christos 		AR_WRITE(sc, AR_PHY_GAIN_2GHZ + offset, reg);
    188  1.1  christos 
    189  1.1  christos 		reg = AR_READ(sc, AR_PHY_RXGAIN + offset);
    190  1.1  christos 		reg = RW(reg, AR9280_PHY_RXGAIN_TXRX_MARGIN,
    191  1.1  christos 		    modal->rxTxMarginCh[i]);
    192  1.1  christos 		reg = RW(reg, AR9280_PHY_RXGAIN_TXRX_ATTEN,
    193  1.1  christos 		    modal->txRxAttenCh[i]);
    194  1.1  christos 		AR_WRITE(sc, AR_PHY_RXGAIN + offset, reg);
    195  1.1  christos 	}
    196  1.1  christos 
    197  1.1  christos 	reg = AR_READ(sc, AR_PHY_SETTLING);
    198  1.1  christos #ifndef IEEE80211_NO_HT
    199  1.1  christos 	if (extc != NULL)
    200  1.1  christos 		reg = RW(reg, AR_PHY_SETTLING_SWITCH, modal->swSettleHt40);
    201  1.1  christos 	else
    202  1.1  christos #endif
    203  1.1  christos 		reg = RW(reg, AR_PHY_SETTLING_SWITCH, modal->switchSettling);
    204  1.1  christos 	AR_WRITE(sc, AR_PHY_SETTLING, reg);
    205  1.1  christos 
    206  1.1  christos 	reg = AR_READ(sc, AR_PHY_DESIRED_SZ);
    207  1.1  christos 	reg = RW(reg, AR_PHY_DESIRED_SZ_ADC, modal->adcDesiredSize);
    208  1.1  christos 	AR_WRITE(sc, AR_PHY_DESIRED_SZ, reg);
    209  1.1  christos 
    210  1.1  christos 	reg  = SM(AR_PHY_RF_CTL4_TX_END_XPAA_OFF, modal->txEndToXpaOff);
    211  1.1  christos 	reg |= SM(AR_PHY_RF_CTL4_TX_END_XPAB_OFF, modal->txEndToXpaOff);
    212  1.1  christos 	reg |= SM(AR_PHY_RF_CTL4_FRAME_XPAA_ON, modal->txFrameToXpaOn);
    213  1.1  christos 	reg |= SM(AR_PHY_RF_CTL4_FRAME_XPAB_ON, modal->txFrameToXpaOn);
    214  1.1  christos 	AR_WRITE(sc, AR_PHY_RF_CTL4, reg);
    215  1.1  christos 
    216  1.1  christos 	reg = AR_READ(sc, AR_PHY_RF_CTL3);
    217  1.1  christos 	reg = RW(reg, AR_PHY_TX_END_TO_A2_RX_ON, modal->txEndToRxOn);
    218  1.1  christos 	AR_WRITE(sc, AR_PHY_RF_CTL3, reg);
    219  1.1  christos 
    220  1.1  christos 	reg = AR_READ(sc, AR_PHY_CCA(0));
    221  1.1  christos 	reg = RW(reg, AR9280_PHY_CCA_THRESH62, modal->thresh62);
    222  1.1  christos 	AR_WRITE(sc, AR_PHY_CCA(0), reg);
    223  1.1  christos 
    224  1.1  christos 	reg = AR_READ(sc, AR_PHY_EXT_CCA0);
    225  1.1  christos 	reg = RW(reg, AR_PHY_EXT_CCA0_THRESH62, modal->thresh62);
    226  1.1  christos 	AR_WRITE(sc, AR_PHY_EXT_CCA0, reg);
    227  1.1  christos 
    228  1.1  christos 	reg = AR_READ(sc, AR9287_AN_RF2G3_CH0);
    229  1.1  christos 	reg = RW(reg, AR9287_AN_RF2G3_DB1, modal->db1);
    230  1.1  christos 	reg = RW(reg, AR9287_AN_RF2G3_DB2, modal->db2);
    231  1.1  christos 	reg = RW(reg, AR9287_AN_RF2G3_OB_CCK, modal->ob_cck);
    232  1.1  christos 	reg = RW(reg, AR9287_AN_RF2G3_OB_PSK, modal->ob_psk);
    233  1.1  christos 	reg = RW(reg, AR9287_AN_RF2G3_OB_QAM, modal->ob_qam);
    234  1.1  christos 	reg = RW(reg, AR9287_AN_RF2G3_OB_PAL_OFF, modal->ob_pal_off);
    235  1.1  christos 	AR_WRITE(sc, AR9287_AN_RF2G3_CH0, reg);
    236  1.1  christos 	AR_WRITE_BARRIER(sc);
    237  1.1  christos 	DELAY(100);
    238  1.1  christos 
    239  1.1  christos 	reg = AR_READ(sc, AR9287_AN_RF2G3_CH1);
    240  1.1  christos 	reg = RW(reg, AR9287_AN_RF2G3_DB1, modal->db1);
    241  1.1  christos 	reg = RW(reg, AR9287_AN_RF2G3_DB2, modal->db2);
    242  1.1  christos 	reg = RW(reg, AR9287_AN_RF2G3_OB_CCK, modal->ob_cck);
    243  1.1  christos 	reg = RW(reg, AR9287_AN_RF2G3_OB_PSK, modal->ob_psk);
    244  1.1  christos 	reg = RW(reg, AR9287_AN_RF2G3_OB_QAM, modal->ob_qam);
    245  1.1  christos 	reg = RW(reg, AR9287_AN_RF2G3_OB_PAL_OFF, modal->ob_pal_off);
    246  1.1  christos 	AR_WRITE(sc, AR9287_AN_RF2G3_CH1, reg);
    247  1.1  christos 	AR_WRITE_BARRIER(sc);
    248  1.1  christos 	DELAY(100);
    249  1.1  christos 
    250  1.1  christos 	reg = AR_READ(sc, AR_PHY_RF_CTL2);
    251  1.1  christos 	reg = RW(reg, AR_PHY_TX_END_DATA_START, modal->txFrameToDataStart);
    252  1.1  christos 	reg = RW(reg, AR_PHY_TX_END_PA_ON, modal->txFrameToPaOn);
    253  1.1  christos 	AR_WRITE(sc, AR_PHY_RF_CTL2, reg);
    254  1.1  christos 
    255  1.1  christos 	reg = AR_READ(sc, AR9287_AN_TOP2);
    256  1.1  christos 	reg = RW(reg, AR9287_AN_TOP2_XPABIAS_LVL, modal->xpaBiasLvl);
    257  1.1  christos 	AR_WRITE(sc, AR9287_AN_TOP2, reg);
    258  1.1  christos 	AR_WRITE_BARRIER(sc);
    259  1.1  christos 	DELAY(100);
    260  1.1  christos }
    261  1.1  christos 
    262  1.1  christos Static void
    263  1.1  christos ar9287_get_pdadcs(struct athn_softc *sc, struct ieee80211_channel *c,
    264  1.1  christos     int chain, int nxpdgains, uint8_t overlap, uint8_t *boundaries,
    265  1.1  christos     uint8_t *pdadcs)
    266  1.1  christos {
    267  1.1  christos 	const struct ar9287_eeprom *eep = sc->sc_eep;
    268  1.1  christos 	const struct ar9287_cal_data_per_freq *pierdata;
    269  1.1  christos 	const uint8_t *pierfreq;
    270  1.1  christos 	struct athn_pier lopier, hipier;
    271  1.1  christos 	int16_t delta;
    272  1.1  christos 	uint8_t fbin;
    273  1.1  christos 	int i, lo, hi, npiers;
    274  1.1  christos 
    275  1.1  christos 	pierfreq = eep->calFreqPier2G;
    276  1.1  christos 	pierdata = (const struct ar9287_cal_data_per_freq *)
    277  1.1  christos 	    eep->calPierData2G[chain];
    278  1.1  christos 	npiers = AR9287_NUM_2G_CAL_PIERS;
    279  1.1  christos 
    280  1.1  christos 	/* Find channel in ROM pier table. */
    281  1.1  christos 	fbin = athn_chan2fbin(c);
    282  1.1  christos 	athn_get_pier_ival(fbin, pierfreq, npiers, &lo, &hi);
    283  1.1  christos 
    284  1.1  christos 	lopier.fbin = pierfreq[lo];
    285  1.1  christos 	hipier.fbin = pierfreq[hi];
    286  1.1  christos 	for (i = 0; i < nxpdgains; i++) {
    287  1.1  christos 		lopier.pwr[i] = pierdata[lo].pwrPdg[i];
    288  1.1  christos 		lopier.vpd[i] = pierdata[lo].vpdPdg[i];
    289  1.1  christos 		hipier.pwr[i] = pierdata[lo].pwrPdg[i];
    290  1.1  christos 		hipier.vpd[i] = pierdata[lo].vpdPdg[i];
    291  1.1  christos 	}
    292  1.1  christos 	ar5008_get_pdadcs(sc, fbin, &lopier, &hipier, nxpdgains,
    293  1.1  christos 	    AR9287_PD_GAIN_ICEPTS, overlap, boundaries, pdadcs);
    294  1.1  christos 
    295  1.1  christos 	delta = (eep->baseEepHeader.pwrTableOffset -
    296  1.1  christos 	    AR_PWR_TABLE_OFFSET_DB) * 2;	/* In half dB. */
    297  1.1  christos 	if (delta != 0) {
    298  1.1  christos 		/* Shift the PDADC table to start at the new offset. */
    299  1.1  christos 		/* XXX Our padding value differs from Linux. */
    300  1.1  christos 		for (i = 0; i < AR_NUM_PDADC_VALUES; i++)
    301  1.1  christos 			pdadcs[i] = pdadcs[MIN(i + delta,
    302  1.1  christos 			    AR_NUM_PDADC_VALUES - 1)];
    303  1.1  christos 	}
    304  1.1  christos }
    305  1.1  christos 
    306  1.1  christos Static void
    307  1.1  christos ar9287_olpc_get_pdgain(struct athn_softc *sc, struct ieee80211_channel *c,
    308  1.1  christos     int chain, int8_t *pwr)
    309  1.1  christos {
    310  1.1  christos 	const struct ar9287_eeprom *eep = sc->sc_eep;
    311  1.1  christos 	const struct ar_cal_data_per_freq_olpc *pierdata;
    312  1.1  christos 	const uint8_t *pierfreq;
    313  1.1  christos 	uint8_t fbin;
    314  1.1  christos 	int lo, hi, npiers;
    315  1.1  christos 
    316  1.1  christos 	pierfreq = eep->calFreqPier2G;
    317  1.1  christos 	pierdata = (const struct ar_cal_data_per_freq_olpc *)
    318  1.1  christos 	    eep->calPierData2G[chain];
    319  1.1  christos 	npiers = AR9287_NUM_2G_CAL_PIERS;
    320  1.1  christos 
    321  1.1  christos 	/* Find channel in ROM pier table. */
    322  1.1  christos 	fbin = athn_chan2fbin(c);
    323  1.1  christos 	athn_get_pier_ival(fbin, pierfreq, npiers, &lo, &hi);
    324  1.1  christos #if 0
    325  1.1  christos 	*pwr = athn_interpolate(fbin,
    326  1.1  christos 	    pierfreq[lo], pierdata[lo].pwrPdg[0][0],
    327  1.1  christos 	    pierfreq[hi], pierdata[hi].pwrPdg[0][0]);
    328  1.1  christos #else
    329  1.1  christos 	*pwr = (pierdata[lo].pwrPdg[0][0] + pierdata[hi].pwrPdg[0][0]) / 2;
    330  1.1  christos #endif
    331  1.1  christos }
    332  1.1  christos 
    333  1.1  christos Static void
    334  1.1  christos ar9287_set_power_calib(struct athn_softc *sc, struct ieee80211_channel *c)
    335  1.1  christos {
    336  1.1  christos 	const struct ar9287_eeprom *eep = sc->sc_eep;
    337  1.1  christos 	uint8_t boundaries[AR_PD_GAINS_IN_MASK];
    338  1.1  christos 	uint8_t pdadcs[AR_NUM_PDADC_VALUES];
    339  1.1  christos 	uint8_t xpdgains[AR9287_NUM_PD_GAINS];
    340  1.1  christos 	int8_t txpower;
    341  1.1  christos 	uint8_t overlap;
    342  1.1  christos 	uint32_t reg, offset;
    343  1.1  christos 	int i, j, nxpdgains;
    344  1.1  christos 
    345  1.1  christos 	if (sc->sc_eep_rev < AR_EEP_MINOR_VER_2) {
    346  1.1  christos 		overlap = MS(AR_READ(sc, AR_PHY_TPCRG5),
    347  1.1  christos 		    AR_PHY_TPCRG5_PD_GAIN_OVERLAP);
    348  1.1  christos 	}
    349  1.1  christos 	else
    350  1.1  christos 		overlap = eep->modalHeader.pdGainOverlap;
    351  1.1  christos 
    352  1.1  christos 	if (sc->sc_flags & ATHN_FLAG_OLPC) {
    353  1.1  christos 		/* XXX not here. */
    354  1.1  christos 		sc->sc_pdadc =
    355  1.1  christos 		    ((const struct ar_cal_data_per_freq_olpc *)
    356  1.1  christos 		     eep->calPierData2G[0])->vpdPdg[0][0];
    357  1.1  christos 	}
    358  1.1  christos 
    359  1.1  christos 	nxpdgains = 0;
    360  1.1  christos 	memset(xpdgains, 0, sizeof(xpdgains));
    361  1.1  christos 	for (i = AR9287_PD_GAINS_IN_MASK - 1; i >= 0; i--) {
    362  1.1  christos 		if (nxpdgains >= AR9287_NUM_PD_GAINS)
    363  1.1  christos 			break;		/* Can't happen. */
    364  1.1  christos 		if (eep->modalHeader.xpdGain & (1 << i))
    365  1.1  christos 			xpdgains[nxpdgains++] = i;
    366  1.1  christos 	}
    367  1.1  christos 	reg = AR_READ(sc, AR_PHY_TPCRG1);
    368  1.1  christos 	reg = RW(reg, AR_PHY_TPCRG1_NUM_PD_GAIN, nxpdgains - 1);
    369  1.1  christos 	reg = RW(reg, AR_PHY_TPCRG1_PD_GAIN_1, xpdgains[0]);
    370  1.1  christos 	reg = RW(reg, AR_PHY_TPCRG1_PD_GAIN_2, xpdgains[1]);
    371  1.1  christos 	AR_WRITE(sc, AR_PHY_TPCRG1, reg);
    372  1.1  christos 	AR_WRITE_BARRIER(sc);
    373  1.1  christos 
    374  1.1  christos 	for (i = 0; i < AR9287_MAX_CHAINS; i++)	{
    375  1.1  christos 		if (!(sc->sc_txchainmask & (1 << i)))
    376  1.1  christos 			continue;
    377  1.1  christos 
    378  1.1  christos 		offset = i * 0x1000;
    379  1.1  christos 
    380  1.1  christos 		if (sc->sc_flags & ATHN_FLAG_OLPC) {
    381  1.1  christos 			ar9287_olpc_get_pdgain(sc, c, i, &txpower);
    382  1.1  christos 
    383  1.1  christos 			reg = AR_READ(sc, AR_PHY_TX_PWRCTRL6_0);
    384  1.1  christos 			reg = RW(reg, AR_PHY_TX_PWRCTRL_ERR_EST_MODE, 3);
    385  1.1  christos 			AR_WRITE(sc, AR_PHY_TX_PWRCTRL6_0, reg);
    386  1.1  christos 
    387  1.1  christos 			reg = AR_READ(sc, AR_PHY_TX_PWRCTRL6_1);
    388  1.1  christos 			reg = RW(reg, AR_PHY_TX_PWRCTRL_ERR_EST_MODE, 3);
    389  1.1  christos 			AR_WRITE(sc, AR_PHY_TX_PWRCTRL6_1, reg);
    390  1.1  christos 
    391  1.1  christos 			/* NB: txpower is in half dB. */
    392  1.1  christos 			reg = AR_READ(sc, AR_PHY_CH0_TX_PWRCTRL11 + offset);
    393  1.1  christos 			reg = RW(reg, AR_PHY_TX_PWRCTRL_OLPC_PWR, txpower);
    394  1.1  christos 			AR_WRITE(sc, AR_PHY_CH0_TX_PWRCTRL11 + offset, reg);
    395  1.1  christos 
    396  1.1  christos 			AR_WRITE_BARRIER(sc);
    397  1.1  christos 			continue;	/* That's it for open loop mode. */
    398  1.1  christos 		}
    399  1.1  christos 
    400  1.1  christos 		/* Closed loop power control. */
    401  1.1  christos 		ar9287_get_pdadcs(sc, c, i, nxpdgains, overlap,
    402  1.1  christos 		    boundaries, pdadcs);
    403  1.1  christos 
    404  1.1  christos 		/* Write boundaries. */
    405  1.1  christos 		if (i == 0) {
    406  1.1  christos 			reg  = SM(AR_PHY_TPCRG5_PD_GAIN_OVERLAP,
    407  1.1  christos 			    overlap);
    408  1.1  christos 			reg |= SM(AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_1,
    409  1.1  christos 			    boundaries[0]);
    410  1.1  christos 			reg |= SM(AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_2,
    411  1.1  christos 			    boundaries[1]);
    412  1.1  christos 			reg |= SM(AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_3,
    413  1.1  christos 			    boundaries[2]);
    414  1.1  christos 			reg |= SM(AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_4,
    415  1.1  christos 			    boundaries[3]);
    416  1.1  christos 			AR_WRITE(sc, AR_PHY_TPCRG5 + offset, reg);
    417  1.1  christos 		}
    418  1.1  christos 		/* Write PDADC values. */
    419  1.1  christos 		for (j = 0; j < AR_NUM_PDADC_VALUES; j += 4) {
    420  1.1  christos 			AR_WRITE(sc, AR_PHY_PDADC_TBL_BASE + offset + j,
    421  1.1  christos 			    pdadcs[j + 0] <<  0 |
    422  1.1  christos 			    pdadcs[j + 1] <<  8 |
    423  1.1  christos 			    pdadcs[j + 2] << 16 |
    424  1.1  christos 			    pdadcs[j + 3] << 24);
    425  1.1  christos 		}
    426  1.1  christos 		AR_WRITE_BARRIER(sc);
    427  1.1  christos 	}
    428  1.1  christos }
    429  1.1  christos 
    430  1.1  christos Static void
    431  1.1  christos ar9287_set_txpower(struct athn_softc *sc, struct ieee80211_channel *c,
    432  1.1  christos     struct ieee80211_channel *extc)
    433  1.1  christos {
    434  1.1  christos 	const struct ar9287_eeprom *eep = sc->sc_eep;
    435  1.3  christos #ifdef notyet
    436  1.1  christos 	const struct ar9287_modal_eep_header *modal = &eep->modalHeader;
    437  1.3  christos #endif
    438  1.1  christos 	uint8_t tpow_cck[4], tpow_ofdm[4];
    439  1.1  christos #ifndef IEEE80211_NO_HT
    440  1.1  christos 	uint8_t tpow_cck_ext[4], tpow_ofdm_ext[4];
    441  1.1  christos 	uint8_t tpow_ht20[8], tpow_ht40[8];
    442  1.1  christos 	uint8_t ht40inc;
    443  1.1  christos #endif
    444  1.3  christos 	int16_t pwr = 0, power[ATHN_POWER_COUNT];
    445  1.1  christos 	int i;
    446  1.1  christos 
    447  1.1  christos 	ar9287_set_power_calib(sc, c);
    448  1.1  christos 
    449  1.3  christos #ifdef notyet
    450  1.1  christos 	/* Compute transmit power reduction due to antenna gain. */
    451  1.3  christos 	uint16_t max_ant_gain = MAX(modal->antennaGainCh[0], modal->antennaGainCh[1]);
    452  1.1  christos 	/* XXX */
    453  1.3  christos #endif
    454  1.1  christos 
    455  1.1  christos 	/*
    456  1.1  christos 	 * Reduce scaled power by number of active chains to get per-chain
    457  1.1  christos 	 * transmit power level.
    458  1.1  christos 	 */
    459  1.1  christos 	if (sc->sc_ntxchains == 2)
    460  1.1  christos 		pwr -= AR_PWR_DECREASE_FOR_2_CHAIN;
    461  1.1  christos 	if (pwr < 0)
    462  1.1  christos 		pwr = 0;
    463  1.1  christos 
    464  1.1  christos 	/* Get CCK target powers. */
    465  1.1  christos 	ar5008_get_lg_tpow(sc, c, AR_CTL_11B, eep->calTargetPowerCck,
    466  1.1  christos 	    AR9287_NUM_2G_CCK_TARGET_POWERS, tpow_cck);
    467  1.1  christos 
    468  1.1  christos 	/* Get OFDM target powers. */
    469  1.1  christos 	ar5008_get_lg_tpow(sc, c, AR_CTL_11G, eep->calTargetPower2G,
    470  1.1  christos 	    AR9287_NUM_2G_20_TARGET_POWERS, tpow_ofdm);
    471  1.1  christos 
    472  1.1  christos #ifndef IEEE80211_NO_HT
    473  1.1  christos 	/* Get HT-20 target powers. */
    474  1.1  christos 	ar5008_get_ht_tpow(sc, c, AR_CTL_2GHT20, eep->calTargetPower2GHT20,
    475  1.1  christos 	    AR9287_NUM_2G_20_TARGET_POWERS, tpow_ht20);
    476  1.1  christos 
    477  1.1  christos 	if (extc != NULL) {
    478  1.1  christos 		/* Get HT-40 target powers. */
    479  1.1  christos 		ar5008_get_ht_tpow(sc, c, AR_CTL_2GHT40,
    480  1.1  christos 		    eep->calTargetPower2GHT40, AR9287_NUM_2G_40_TARGET_POWERS,
    481  1.1  christos 		    tpow_ht40);
    482  1.1  christos 
    483  1.1  christos 		/* Get secondary channel CCK target powers. */
    484  1.1  christos 		ar5008_get_lg_tpow(sc, extc, AR_CTL_11B,
    485  1.1  christos 		    eep->calTargetPowerCck, AR9287_NUM_2G_CCK_TARGET_POWERS,
    486  1.1  christos 		    tpow_cck_ext);
    487  1.1  christos 
    488  1.1  christos 		/* Get secondary channel OFDM target powers. */
    489  1.1  christos 		ar5008_get_lg_tpow(sc, extc, AR_CTL_11G,
    490  1.1  christos 		    eep->calTargetPower2G, AR9287_NUM_2G_20_TARGET_POWERS,
    491  1.1  christos 		    tpow_ofdm_ext);
    492  1.1  christos 	}
    493  1.1  christos #endif
    494  1.1  christos 
    495  1.1  christos 	memset(power, 0, sizeof(power));
    496  1.1  christos 	/* Shuffle target powers accross transmit rates. */
    497  1.1  christos 	power[ATHN_POWER_OFDM6   ] =
    498  1.1  christos 	power[ATHN_POWER_OFDM9   ] =
    499  1.1  christos 	power[ATHN_POWER_OFDM12  ] =
    500  1.1  christos 	power[ATHN_POWER_OFDM18  ] =
    501  1.1  christos 	power[ATHN_POWER_OFDM24  ] = tpow_ofdm[0];
    502  1.1  christos 	power[ATHN_POWER_OFDM36  ] = tpow_ofdm[1];
    503  1.1  christos 	power[ATHN_POWER_OFDM48  ] = tpow_ofdm[2];
    504  1.1  christos 	power[ATHN_POWER_OFDM54  ] = tpow_ofdm[3];
    505  1.1  christos 	power[ATHN_POWER_XR      ] = tpow_ofdm[0];
    506  1.1  christos 	power[ATHN_POWER_CCK1_LP ] = tpow_cck[0];
    507  1.1  christos 	power[ATHN_POWER_CCK2_LP ] =
    508  1.1  christos 	power[ATHN_POWER_CCK2_SP ] = tpow_cck[1];
    509  1.1  christos 	power[ATHN_POWER_CCK55_LP] =
    510  1.1  christos 	power[ATHN_POWER_CCK55_SP] = tpow_cck[2];
    511  1.1  christos 	power[ATHN_POWER_CCK11_LP] =
    512  1.1  christos 	power[ATHN_POWER_CCK11_SP] = tpow_cck[3];
    513  1.1  christos #ifndef IEEE80211_NO_HT
    514  1.1  christos 	for (i = 0; i < nitems(tpow_ht20); i++)
    515  1.1  christos 		power[ATHN_POWER_HT20(i)] = tpow_ht20[i];
    516  1.1  christos 	if (extc != NULL) {
    517  1.1  christos 		/* Correct PAR difference between HT40 and HT20/Legacy. */
    518  1.1  christos 		if (sc->sc_eep_rev >= AR_EEP_MINOR_VER_2)
    519  1.1  christos 			ht40inc = modal->ht40PowerIncForPdadc;
    520  1.1  christos 		else
    521  1.1  christos 			ht40inc = AR_HT40_POWER_INC_FOR_PDADC;
    522  1.1  christos 		for (i = 0; i < nitems(tpow_ht40); i++)
    523  1.1  christos 			power[ATHN_POWER_HT40(i)] = tpow_ht40[i] + ht40inc;
    524  1.1  christos 		power[ATHN_POWER_OFDM_DUP] = tpow_ht40[0];
    525  1.1  christos 		power[ATHN_POWER_CCK_DUP ] = tpow_ht40[0];
    526  1.1  christos 		power[ATHN_POWER_OFDM_EXT] = tpow_ofdm_ext[0];
    527  1.1  christos 		if (IEEE80211_IS_CHAN_2GHZ(c))
    528  1.1  christos 			power[ATHN_POWER_CCK_EXT] = tpow_cck_ext[0];
    529  1.1  christos 	}
    530  1.1  christos #endif
    531  1.1  christos 
    532  1.1  christos 	for (i = 0; i < ATHN_POWER_COUNT; i++) {
    533  1.1  christos 		power[i] -= AR_PWR_TABLE_OFFSET_DB * 2;	/* In half dB. */
    534  1.1  christos 		if (power[i] > AR_MAX_RATE_POWER)
    535  1.1  christos 			power[i] = AR_MAX_RATE_POWER;
    536  1.1  christos 	}
    537  1.1  christos 	/* Commit transmit power values to hardware. */
    538  1.1  christos 	ar5008_write_txpower(sc, power);
    539  1.1  christos }
    540  1.1  christos 
    541  1.1  christos Static void
    542  1.1  christos ar9287_olpc_init(struct athn_softc *sc)
    543  1.1  christos {
    544  1.1  christos 	uint32_t reg;
    545  1.1  christos 
    546  1.1  christos 	AR_SETBITS(sc, AR_PHY_TX_PWRCTRL9, AR_PHY_TX_PWRCTRL9_RES_DC_REMOVAL);
    547  1.1  christos 
    548  1.1  christos 	reg = AR_READ(sc, AR9287_AN_TXPC0);
    549  1.1  christos 	reg = RW(reg, AR9287_AN_TXPC0_TXPCMODE,
    550  1.1  christos 	    AR9287_AN_TXPC0_TXPCMODE_TEMPSENSE);
    551  1.1  christos 	AR_WRITE(sc, AR9287_AN_TXPC0, reg);
    552  1.1  christos 	AR_WRITE_BARRIER(sc);
    553  1.1  christos 	DELAY(100);
    554  1.1  christos }
    555  1.1  christos 
    556  1.1  christos Static void
    557  1.1  christos ar9287_olpc_temp_compensation(struct athn_softc *sc)
    558  1.1  christos {
    559  1.1  christos 	const struct ar9287_eeprom *eep = sc->sc_eep;
    560  1.1  christos 	int8_t pdadc, slope, tcomp;
    561  1.1  christos 	uint32_t reg;
    562  1.1  christos 
    563  1.1  christos 	reg = AR_READ(sc, AR_PHY_TX_PWRCTRL4);
    564  1.1  christos 	pdadc = MS(reg, AR_PHY_TX_PWRCTRL_PD_AVG_OUT);
    565  1.1  christos 	DPRINTFN(DBG_RF, sc, "PD Avg Out=%d\n", pdadc);
    566  1.1  christos 
    567  1.1  christos 	if (sc->sc_pdadc == 0 || pdadc == 0)
    568  1.1  christos 		return;	/* No frames transmitted yet. */
    569  1.1  christos 
    570  1.1  christos 	/* Compute Tx gain temperature compensation. */
    571  1.1  christos 	if (sc->sc_eep_rev >= AR_EEP_MINOR_VER_2)
    572  1.1  christos 		slope = eep->baseEepHeader.tempSensSlope;
    573  1.1  christos 	else
    574  1.1  christos 		slope = 0;
    575  1.1  christos 	if (slope != 0)	/* Prevents division by zero. */
    576  1.1  christos 		tcomp = ((pdadc - sc->sc_pdadc) * 4) / slope;
    577  1.1  christos 	else
    578  1.1  christos 		tcomp = 0;
    579  1.1  christos 	DPRINTFN(DBG_RF, sc, "OLPC temp compensation=%d\n", tcomp);
    580  1.1  christos 
    581  1.1  christos 	/* Write compensation value for both Tx chains. */
    582  1.1  christos 	reg = AR_READ(sc, AR_PHY_CH0_TX_PWRCTRL11);
    583  1.1  christos 	reg = RW(reg, AR_PHY_TX_PWRCTRL_OLPC_TEMP_COMP, tcomp);
    584  1.1  christos 	AR_WRITE(sc, AR_PHY_CH0_TX_PWRCTRL11, reg);
    585  1.1  christos 
    586  1.1  christos 	reg = AR_READ(sc, AR_PHY_CH1_TX_PWRCTRL11);
    587  1.1  christos 	reg = RW(reg, AR_PHY_TX_PWRCTRL_OLPC_TEMP_COMP, tcomp);
    588  1.1  christos 	AR_WRITE(sc, AR_PHY_CH1_TX_PWRCTRL11, reg);
    589  1.1  christos 	AR_WRITE_BARRIER(sc);
    590  1.1  christos }
    591  1.1  christos 
    592  1.1  christos PUBLIC void
    593  1.1  christos ar9287_1_3_enable_async_fifo(struct athn_softc *sc)
    594  1.1  christos {
    595  1.1  christos 
    596  1.1  christos 	/* Enable ASYNC FIFO. */
    597  1.1  christos 	AR_SETBITS(sc, AR_MAC_PCU_ASYNC_FIFO_REG3,
    598  1.1  christos 	    AR_MAC_PCU_ASYNC_FIFO_REG3_DATAPATH_SEL);
    599  1.1  christos 	AR_SETBITS(sc, AR_PHY_MODE, AR_PHY_MODE_ASYNCFIFO);
    600  1.1  christos 	AR_CLRBITS(sc, AR_MAC_PCU_ASYNC_FIFO_REG3,
    601  1.1  christos 	    AR_MAC_PCU_ASYNC_FIFO_REG3_SOFT_RESET);
    602  1.1  christos 	AR_SETBITS(sc, AR_MAC_PCU_ASYNC_FIFO_REG3,
    603  1.1  christos 	    AR_MAC_PCU_ASYNC_FIFO_REG3_SOFT_RESET);
    604  1.1  christos 	AR_WRITE_BARRIER(sc);
    605  1.1  christos }
    606  1.1  christos 
    607  1.1  christos PUBLIC void
    608  1.1  christos ar9287_1_3_setup_async_fifo(struct athn_softc *sc)
    609  1.1  christos {
    610  1.1  christos 	uint32_t reg;
    611  1.1  christos 
    612  1.1  christos 	/*
    613  1.1  christos 	 * MAC runs at 117MHz (instead of 88/44MHz) when ASYNC FIFO is
    614  1.1  christos 	 * enabled, so the following counters have to be changed.
    615  1.1  christos 	 */
    616  1.1  christos 	AR_WRITE(sc, AR_D_GBL_IFS_SIFS, AR_D_GBL_IFS_SIFS_ASYNC_FIFO_DUR);
    617  1.1  christos 	AR_WRITE(sc, AR_D_GBL_IFS_SLOT, AR_D_GBL_IFS_SLOT_ASYNC_FIFO_DUR);
    618  1.1  christos 	AR_WRITE(sc, AR_D_GBL_IFS_EIFS, AR_D_GBL_IFS_EIFS_ASYNC_FIFO_DUR);
    619  1.1  christos 
    620  1.1  christos 	AR_WRITE(sc, AR_TIME_OUT, AR_TIME_OUT_ACK_CTS_ASYNC_FIFO_DUR);
    621  1.1  christos 	AR_WRITE(sc, AR_USEC, AR_USEC_ASYNC_FIFO_DUR);
    622  1.1  christos 
    623  1.1  christos 	AR_SETBITS(sc, AR_MAC_PCU_LOGIC_ANALYZER,
    624  1.1  christos 	    AR_MAC_PCU_LOGIC_ANALYZER_DISBUG20768);
    625  1.1  christos 
    626  1.1  christos 	reg = AR_READ(sc, AR_AHB_MODE);
    627  1.1  christos 	reg = RW(reg, AR_AHB_CUSTOM_BURST, AR_AHB_CUSTOM_BURST_ASYNC_FIFO_VAL);
    628  1.1  christos 	AR_WRITE(sc, AR_AHB_MODE, reg);
    629  1.1  christos 
    630  1.1  christos 	AR_SETBITS(sc, AR_PCU_MISC_MODE2, AR_PCU_MISC_MODE2_ENABLE_AGGWEP);
    631  1.1  christos 	AR_WRITE_BARRIER(sc);
    632  1.1  christos }
    633