xref: /netbsd-src/sys/dev/ic/arn9287.c (revision 13d4bb4cc874de96add7fc4227d38a1d656b03d1)
1*13d4bb4cSthorpej /*	$NetBSD: arn9287.c,v 1.4 2022/09/25 18:43:32 thorpej Exp $	*/
264f89611Schristos /*	$OpenBSD: ar9287.c,v 1.17 2012/06/10 21:23:36 kettenis Exp $	*/
364f89611Schristos 
464f89611Schristos /*-
564f89611Schristos  * Copyright (c) 2009 Damien Bergamini <damien.bergamini@free.fr>
664f89611Schristos  * Copyright (c) 2008-2009 Atheros Communications Inc.
764f89611Schristos  *
864f89611Schristos  * Permission to use, copy, modify, and/or distribute this software for any
964f89611Schristos  * purpose with or without fee is hereby granted, provided that the above
1064f89611Schristos  * copyright notice and this permission notice appear in all copies.
1164f89611Schristos  *
1264f89611Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1364f89611Schristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1464f89611Schristos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1564f89611Schristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1664f89611Schristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1764f89611Schristos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1864f89611Schristos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1964f89611Schristos  */
2064f89611Schristos 
2164f89611Schristos /*
2264f89611Schristos  * Driver for Atheros 802.11a/g/n chipsets.
2364f89611Schristos  * Routines for AR9227 and AR9287 chipsets.
2464f89611Schristos  */
2564f89611Schristos 
2664f89611Schristos #include <sys/cdefs.h>
27*13d4bb4cSthorpej __KERNEL_RCSID(0, "$NetBSD: arn9287.c,v 1.4 2022/09/25 18:43:32 thorpej Exp $");
2864f89611Schristos 
2964f89611Schristos #include <sys/param.h>
3064f89611Schristos #include <sys/sockio.h>
3164f89611Schristos #include <sys/mbuf.h>
3264f89611Schristos #include <sys/kernel.h>
3364f89611Schristos #include <sys/socket.h>
3464f89611Schristos #include <sys/systm.h>
3564f89611Schristos #include <sys/queue.h>
3664f89611Schristos #include <sys/callout.h>
3764f89611Schristos #include <sys/conf.h>
3864f89611Schristos #include <sys/device.h>
3964f89611Schristos 
4064f89611Schristos #include <sys/bus.h>
4164f89611Schristos #include <sys/endian.h>
4264f89611Schristos #include <sys/intr.h>
4364f89611Schristos 
4464f89611Schristos #include <net/bpf.h>
4564f89611Schristos #include <net/if.h>
4664f89611Schristos #include <net/if_arp.h>
4764f89611Schristos #include <net/if_dl.h>
4802421171Schristos #include <net/if_ether.h>
4964f89611Schristos #include <net/if_media.h>
5064f89611Schristos #include <net/if_types.h>
5164f89611Schristos 
5264f89611Schristos #include <netinet/in.h>
5364f89611Schristos #include <netinet/in_systm.h>
5464f89611Schristos #include <netinet/in_var.h>
5564f89611Schristos #include <netinet/ip.h>
5664f89611Schristos 
5764f89611Schristos #include <net80211/ieee80211_var.h>
5864f89611Schristos #include <net80211/ieee80211_amrr.h>
5964f89611Schristos #include <net80211/ieee80211_radiotap.h>
6064f89611Schristos 
6164f89611Schristos #include <dev/ic/athnreg.h>
6264f89611Schristos #include <dev/ic/athnvar.h>
6364f89611Schristos 
6464f89611Schristos #include <dev/ic/arn5008reg.h>
6564f89611Schristos #include <dev/ic/arn9280reg.h>
6664f89611Schristos #include <dev/ic/arn9287reg.h>
6764f89611Schristos 
6864f89611Schristos #include <dev/ic/arn5008.h>
6964f89611Schristos #include <dev/ic/arn9280.h>
7064f89611Schristos #include <dev/ic/arn9287.h>
7164f89611Schristos 
7264f89611Schristos #define Static static
7364f89611Schristos 
7464f89611Schristos Static void	ar9287_get_pdadcs(struct athn_softc *,
7564f89611Schristos 		    struct ieee80211_channel *, int, int, uint8_t, uint8_t *,
7664f89611Schristos 		    uint8_t *);
7764f89611Schristos Static const struct ar_spur_chan *
7864f89611Schristos 		ar9287_get_spur_chans(struct athn_softc *, int);
7964f89611Schristos Static void	ar9287_init_from_rom(struct athn_softc *,
8064f89611Schristos 		    struct ieee80211_channel *, struct ieee80211_channel *);
8164f89611Schristos Static void	ar9287_olpc_get_pdgain(struct athn_softc *,
8264f89611Schristos 		    struct ieee80211_channel *, int, int8_t *);
8364f89611Schristos Static void	ar9287_olpc_init(struct athn_softc *);
8464f89611Schristos Static void	ar9287_olpc_temp_compensation(struct athn_softc *);
8564f89611Schristos Static void	ar9287_set_power_calib(struct athn_softc *,
8664f89611Schristos 		    struct ieee80211_channel *);
8764f89611Schristos Static void	ar9287_set_txpower(struct athn_softc *,
8864f89611Schristos 		    struct ieee80211_channel *, struct ieee80211_channel *);
8964f89611Schristos Static void	ar9287_setup(struct athn_softc *);
9064f89611Schristos Static void	ar9287_swap_rom(struct athn_softc *);
9164f89611Schristos 
9264f89611Schristos PUBLIC int
ar9287_attach(struct athn_softc * sc)9364f89611Schristos ar9287_attach(struct athn_softc *sc)
9464f89611Schristos {
9564f89611Schristos 
9664f89611Schristos 	sc->sc_eep_base = AR9287_EEP_START_LOC;
9764f89611Schristos 	sc->sc_eep_size = sizeof(struct ar9287_eeprom);
9864f89611Schristos 	sc->sc_def_nf = AR9287_PHY_CCA_MAX_GOOD_VALUE;
9964f89611Schristos 	sc->sc_ngpiopins = (sc->sc_flags & ATHN_FLAG_USB) ? 16 : 11;
10064f89611Schristos 	sc->sc_led_pin = 8;
10164f89611Schristos 	sc->sc_workaround = AR9285_WA_DEFAULT;
10264f89611Schristos 	sc->sc_ops.setup = ar9287_setup;
10364f89611Schristos 	sc->sc_ops.swap_rom = ar9287_swap_rom;
10464f89611Schristos 	sc->sc_ops.init_from_rom = ar9287_init_from_rom;
10564f89611Schristos 	sc->sc_ops.set_txpower = ar9287_set_txpower;
10664f89611Schristos 	sc->sc_ops.set_synth = ar9280_set_synth;
10764f89611Schristos 	sc->sc_ops.spur_mitigate = ar9280_spur_mitigate;
10864f89611Schristos 	sc->sc_ops.get_spur_chans = ar9287_get_spur_chans;
10964f89611Schristos 	sc->sc_ops.olpc_init = ar9287_olpc_init;
11064f89611Schristos 	sc->sc_ops.olpc_temp_compensation = ar9287_olpc_temp_compensation;
11164f89611Schristos 	sc->sc_ini = &ar9287_1_1_ini;
11264f89611Schristos 	sc->sc_serdes = &ar9280_2_0_serdes;
11364f89611Schristos 
11464f89611Schristos 	return ar5008_attach(sc);
11564f89611Schristos }
11664f89611Schristos 
11764f89611Schristos Static void
ar9287_setup(struct athn_softc * sc)11864f89611Schristos ar9287_setup(struct athn_softc *sc)
11964f89611Schristos {
12064f89611Schristos 	const struct ar9287_eeprom *eep = sc->sc_eep;
12164f89611Schristos 
12264f89611Schristos 	/* Determine if open loop power control should be used. */
12364f89611Schristos 	if (eep->baseEepHeader.openLoopPwrCntl)
12464f89611Schristos 		sc->sc_flags |= ATHN_FLAG_OLPC;
12564f89611Schristos 
12664f89611Schristos 	sc->sc_rx_gain = &ar9287_1_1_rx_gain;
12764f89611Schristos 	sc->sc_tx_gain = &ar9287_1_1_tx_gain;
12864f89611Schristos }
12964f89611Schristos 
13064f89611Schristos Static void
ar9287_swap_rom(struct athn_softc * sc)13164f89611Schristos ar9287_swap_rom(struct athn_softc *sc)
13264f89611Schristos {
13364f89611Schristos 	struct ar9287_eeprom *eep = sc->sc_eep;
13464f89611Schristos 	int i;
13564f89611Schristos 
13664f89611Schristos 	eep->modalHeader.antCtrlCommon =
13764f89611Schristos 	    bswap32(eep->modalHeader.antCtrlCommon);
13864f89611Schristos 
13964f89611Schristos 	for (i = 0; i < AR9287_MAX_CHAINS; i++) {
14064f89611Schristos 		eep->modalHeader.antCtrlChain[i] =
14164f89611Schristos 		    bswap32(eep->modalHeader.antCtrlChain[i]);
14264f89611Schristos 	}
14364f89611Schristos 	for (i = 0; i < AR_EEPROM_MODAL_SPURS; i++) {
14464f89611Schristos 		eep->modalHeader.spurChans[i].spurChan =
14564f89611Schristos 		    bswap16(eep->modalHeader.spurChans[i].spurChan);
14664f89611Schristos 	}
14764f89611Schristos }
14864f89611Schristos 
14964f89611Schristos Static const struct ar_spur_chan *
ar9287_get_spur_chans(struct athn_softc * sc,int is2ghz)15064f89611Schristos ar9287_get_spur_chans(struct athn_softc *sc, int is2ghz)
15164f89611Schristos {
15264f89611Schristos 	const struct ar9287_eeprom *eep = sc->sc_eep;
15364f89611Schristos 
15464f89611Schristos 	KASSERT(is2ghz);
15564f89611Schristos 	return eep->modalHeader.spurChans;
15664f89611Schristos }
15764f89611Schristos 
15864f89611Schristos Static void
ar9287_init_from_rom(struct athn_softc * sc,struct ieee80211_channel * c,struct ieee80211_channel * extc)15964f89611Schristos ar9287_init_from_rom(struct athn_softc *sc, struct ieee80211_channel *c,
16064f89611Schristos     struct ieee80211_channel *extc)
16164f89611Schristos {
16264f89611Schristos 	const struct ar9287_eeprom *eep = sc->sc_eep;
16364f89611Schristos 	const struct ar9287_modal_eep_header *modal = &eep->modalHeader;
16464f89611Schristos 	uint32_t reg, offset;
16564f89611Schristos 	int i;
16664f89611Schristos 
16764f89611Schristos 	AR_WRITE(sc, AR_PHY_SWITCH_COM, modal->antCtrlCommon);
16864f89611Schristos 
16964f89611Schristos 	for (i = 0; i < AR9287_MAX_CHAINS; i++) {
17064f89611Schristos 		offset = i * 0x1000;
17164f89611Schristos 
17264f89611Schristos 		AR_WRITE(sc, AR_PHY_SWITCH_CHAIN_0 + offset,
17364f89611Schristos 		    modal->antCtrlChain[i]);
17464f89611Schristos 
17564f89611Schristos 		reg = AR_READ(sc, AR_PHY_TIMING_CTRL4_0 + offset);
17664f89611Schristos 		reg = RW(reg, AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF,
17764f89611Schristos 		    modal->iqCalICh[i]);
17864f89611Schristos 		reg = RW(reg, AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF,
17964f89611Schristos 		    modal->iqCalQCh[i]);
18064f89611Schristos 		AR_WRITE(sc, AR_PHY_TIMING_CTRL4_0 + offset, reg);
18164f89611Schristos 
18264f89611Schristos 		reg = AR_READ(sc, AR_PHY_GAIN_2GHZ + offset);
18364f89611Schristos 		reg = RW(reg, AR_PHY_GAIN_2GHZ_XATTEN1_MARGIN,
18464f89611Schristos 		    modal->bswMargin[i]);
18564f89611Schristos 		reg = RW(reg, AR_PHY_GAIN_2GHZ_XATTEN1_DB,
18664f89611Schristos 		    modal->bswAtten[i]);
18764f89611Schristos 		AR_WRITE(sc, AR_PHY_GAIN_2GHZ + offset, reg);
18864f89611Schristos 
18964f89611Schristos 		reg = AR_READ(sc, AR_PHY_RXGAIN + offset);
19064f89611Schristos 		reg = RW(reg, AR9280_PHY_RXGAIN_TXRX_MARGIN,
19164f89611Schristos 		    modal->rxTxMarginCh[i]);
19264f89611Schristos 		reg = RW(reg, AR9280_PHY_RXGAIN_TXRX_ATTEN,
19364f89611Schristos 		    modal->txRxAttenCh[i]);
19464f89611Schristos 		AR_WRITE(sc, AR_PHY_RXGAIN + offset, reg);
19564f89611Schristos 	}
19664f89611Schristos 
19764f89611Schristos 	reg = AR_READ(sc, AR_PHY_SETTLING);
19864f89611Schristos #ifndef IEEE80211_NO_HT
19964f89611Schristos 	if (extc != NULL)
20064f89611Schristos 		reg = RW(reg, AR_PHY_SETTLING_SWITCH, modal->swSettleHt40);
20164f89611Schristos 	else
20264f89611Schristos #endif
20364f89611Schristos 		reg = RW(reg, AR_PHY_SETTLING_SWITCH, modal->switchSettling);
20464f89611Schristos 	AR_WRITE(sc, AR_PHY_SETTLING, reg);
20564f89611Schristos 
20664f89611Schristos 	reg = AR_READ(sc, AR_PHY_DESIRED_SZ);
20764f89611Schristos 	reg = RW(reg, AR_PHY_DESIRED_SZ_ADC, modal->adcDesiredSize);
20864f89611Schristos 	AR_WRITE(sc, AR_PHY_DESIRED_SZ, reg);
20964f89611Schristos 
21064f89611Schristos 	reg  = SM(AR_PHY_RF_CTL4_TX_END_XPAA_OFF, modal->txEndToXpaOff);
21164f89611Schristos 	reg |= SM(AR_PHY_RF_CTL4_TX_END_XPAB_OFF, modal->txEndToXpaOff);
21264f89611Schristos 	reg |= SM(AR_PHY_RF_CTL4_FRAME_XPAA_ON, modal->txFrameToXpaOn);
21364f89611Schristos 	reg |= SM(AR_PHY_RF_CTL4_FRAME_XPAB_ON, modal->txFrameToXpaOn);
21464f89611Schristos 	AR_WRITE(sc, AR_PHY_RF_CTL4, reg);
21564f89611Schristos 
21664f89611Schristos 	reg = AR_READ(sc, AR_PHY_RF_CTL3);
21764f89611Schristos 	reg = RW(reg, AR_PHY_TX_END_TO_A2_RX_ON, modal->txEndToRxOn);
21864f89611Schristos 	AR_WRITE(sc, AR_PHY_RF_CTL3, reg);
21964f89611Schristos 
22064f89611Schristos 	reg = AR_READ(sc, AR_PHY_CCA(0));
22164f89611Schristos 	reg = RW(reg, AR9280_PHY_CCA_THRESH62, modal->thresh62);
22264f89611Schristos 	AR_WRITE(sc, AR_PHY_CCA(0), reg);
22364f89611Schristos 
22464f89611Schristos 	reg = AR_READ(sc, AR_PHY_EXT_CCA0);
22564f89611Schristos 	reg = RW(reg, AR_PHY_EXT_CCA0_THRESH62, modal->thresh62);
22664f89611Schristos 	AR_WRITE(sc, AR_PHY_EXT_CCA0, reg);
22764f89611Schristos 
22864f89611Schristos 	reg = AR_READ(sc, AR9287_AN_RF2G3_CH0);
22964f89611Schristos 	reg = RW(reg, AR9287_AN_RF2G3_DB1, modal->db1);
23064f89611Schristos 	reg = RW(reg, AR9287_AN_RF2G3_DB2, modal->db2);
23164f89611Schristos 	reg = RW(reg, AR9287_AN_RF2G3_OB_CCK, modal->ob_cck);
23264f89611Schristos 	reg = RW(reg, AR9287_AN_RF2G3_OB_PSK, modal->ob_psk);
23364f89611Schristos 	reg = RW(reg, AR9287_AN_RF2G3_OB_QAM, modal->ob_qam);
23464f89611Schristos 	reg = RW(reg, AR9287_AN_RF2G3_OB_PAL_OFF, modal->ob_pal_off);
23564f89611Schristos 	AR_WRITE(sc, AR9287_AN_RF2G3_CH0, reg);
23664f89611Schristos 	AR_WRITE_BARRIER(sc);
23764f89611Schristos 	DELAY(100);
23864f89611Schristos 
23964f89611Schristos 	reg = AR_READ(sc, AR9287_AN_RF2G3_CH1);
24064f89611Schristos 	reg = RW(reg, AR9287_AN_RF2G3_DB1, modal->db1);
24164f89611Schristos 	reg = RW(reg, AR9287_AN_RF2G3_DB2, modal->db2);
24264f89611Schristos 	reg = RW(reg, AR9287_AN_RF2G3_OB_CCK, modal->ob_cck);
24364f89611Schristos 	reg = RW(reg, AR9287_AN_RF2G3_OB_PSK, modal->ob_psk);
24464f89611Schristos 	reg = RW(reg, AR9287_AN_RF2G3_OB_QAM, modal->ob_qam);
24564f89611Schristos 	reg = RW(reg, AR9287_AN_RF2G3_OB_PAL_OFF, modal->ob_pal_off);
24664f89611Schristos 	AR_WRITE(sc, AR9287_AN_RF2G3_CH1, reg);
24764f89611Schristos 	AR_WRITE_BARRIER(sc);
24864f89611Schristos 	DELAY(100);
24964f89611Schristos 
25064f89611Schristos 	reg = AR_READ(sc, AR_PHY_RF_CTL2);
25164f89611Schristos 	reg = RW(reg, AR_PHY_TX_END_DATA_START, modal->txFrameToDataStart);
25264f89611Schristos 	reg = RW(reg, AR_PHY_TX_END_PA_ON, modal->txFrameToPaOn);
25364f89611Schristos 	AR_WRITE(sc, AR_PHY_RF_CTL2, reg);
25464f89611Schristos 
25564f89611Schristos 	reg = AR_READ(sc, AR9287_AN_TOP2);
25664f89611Schristos 	reg = RW(reg, AR9287_AN_TOP2_XPABIAS_LVL, modal->xpaBiasLvl);
25764f89611Schristos 	AR_WRITE(sc, AR9287_AN_TOP2, reg);
25864f89611Schristos 	AR_WRITE_BARRIER(sc);
25964f89611Schristos 	DELAY(100);
26064f89611Schristos }
26164f89611Schristos 
26264f89611Schristos Static void
ar9287_get_pdadcs(struct athn_softc * sc,struct ieee80211_channel * c,int chain,int nxpdgains,uint8_t overlap,uint8_t * boundaries,uint8_t * pdadcs)26364f89611Schristos ar9287_get_pdadcs(struct athn_softc *sc, struct ieee80211_channel *c,
26464f89611Schristos     int chain, int nxpdgains, uint8_t overlap, uint8_t *boundaries,
26564f89611Schristos     uint8_t *pdadcs)
26664f89611Schristos {
26764f89611Schristos 	const struct ar9287_eeprom *eep = sc->sc_eep;
26864f89611Schristos 	const struct ar9287_cal_data_per_freq *pierdata;
26964f89611Schristos 	const uint8_t *pierfreq;
27064f89611Schristos 	struct athn_pier lopier, hipier;
27164f89611Schristos 	int16_t delta;
27264f89611Schristos 	uint8_t fbin;
27364f89611Schristos 	int i, lo, hi, npiers;
27464f89611Schristos 
27564f89611Schristos 	pierfreq = eep->calFreqPier2G;
27664f89611Schristos 	pierdata = (const struct ar9287_cal_data_per_freq *)
27764f89611Schristos 	    eep->calPierData2G[chain];
27864f89611Schristos 	npiers = AR9287_NUM_2G_CAL_PIERS;
27964f89611Schristos 
28064f89611Schristos 	/* Find channel in ROM pier table. */
28164f89611Schristos 	fbin = athn_chan2fbin(c);
28264f89611Schristos 	athn_get_pier_ival(fbin, pierfreq, npiers, &lo, &hi);
28364f89611Schristos 
28464f89611Schristos 	lopier.fbin = pierfreq[lo];
28564f89611Schristos 	hipier.fbin = pierfreq[hi];
28664f89611Schristos 	for (i = 0; i < nxpdgains; i++) {
28764f89611Schristos 		lopier.pwr[i] = pierdata[lo].pwrPdg[i];
28864f89611Schristos 		lopier.vpd[i] = pierdata[lo].vpdPdg[i];
28964f89611Schristos 		hipier.pwr[i] = pierdata[lo].pwrPdg[i];
29064f89611Schristos 		hipier.vpd[i] = pierdata[lo].vpdPdg[i];
29164f89611Schristos 	}
29264f89611Schristos 	ar5008_get_pdadcs(sc, fbin, &lopier, &hipier, nxpdgains,
29364f89611Schristos 	    AR9287_PD_GAIN_ICEPTS, overlap, boundaries, pdadcs);
29464f89611Schristos 
29564f89611Schristos 	delta = (eep->baseEepHeader.pwrTableOffset -
29664f89611Schristos 	    AR_PWR_TABLE_OFFSET_DB) * 2;	/* In half dB. */
29764f89611Schristos 	if (delta != 0) {
29864f89611Schristos 		/* Shift the PDADC table to start at the new offset. */
29964f89611Schristos 		/* XXX Our padding value differs from Linux. */
30064f89611Schristos 		for (i = 0; i < AR_NUM_PDADC_VALUES; i++)
30164f89611Schristos 			pdadcs[i] = pdadcs[MIN(i + delta,
30264f89611Schristos 			    AR_NUM_PDADC_VALUES - 1)];
30364f89611Schristos 	}
30464f89611Schristos }
30564f89611Schristos 
30664f89611Schristos Static void
ar9287_olpc_get_pdgain(struct athn_softc * sc,struct ieee80211_channel * c,int chain,int8_t * pwr)30764f89611Schristos ar9287_olpc_get_pdgain(struct athn_softc *sc, struct ieee80211_channel *c,
30864f89611Schristos     int chain, int8_t *pwr)
30964f89611Schristos {
31064f89611Schristos 	const struct ar9287_eeprom *eep = sc->sc_eep;
31164f89611Schristos 	const struct ar_cal_data_per_freq_olpc *pierdata;
31264f89611Schristos 	const uint8_t *pierfreq;
31364f89611Schristos 	uint8_t fbin;
31464f89611Schristos 	int lo, hi, npiers;
31564f89611Schristos 
31664f89611Schristos 	pierfreq = eep->calFreqPier2G;
31764f89611Schristos 	pierdata = (const struct ar_cal_data_per_freq_olpc *)
31864f89611Schristos 	    eep->calPierData2G[chain];
31964f89611Schristos 	npiers = AR9287_NUM_2G_CAL_PIERS;
32064f89611Schristos 
32164f89611Schristos 	/* Find channel in ROM pier table. */
32264f89611Schristos 	fbin = athn_chan2fbin(c);
32364f89611Schristos 	athn_get_pier_ival(fbin, pierfreq, npiers, &lo, &hi);
32464f89611Schristos #if 0
32564f89611Schristos 	*pwr = athn_interpolate(fbin,
32664f89611Schristos 	    pierfreq[lo], pierdata[lo].pwrPdg[0][0],
32764f89611Schristos 	    pierfreq[hi], pierdata[hi].pwrPdg[0][0]);
32864f89611Schristos #else
32964f89611Schristos 	*pwr = (pierdata[lo].pwrPdg[0][0] + pierdata[hi].pwrPdg[0][0]) / 2;
33064f89611Schristos #endif
33164f89611Schristos }
33264f89611Schristos 
33364f89611Schristos Static void
ar9287_set_power_calib(struct athn_softc * sc,struct ieee80211_channel * c)33464f89611Schristos ar9287_set_power_calib(struct athn_softc *sc, struct ieee80211_channel *c)
33564f89611Schristos {
33664f89611Schristos 	const struct ar9287_eeprom *eep = sc->sc_eep;
33764f89611Schristos 	uint8_t boundaries[AR_PD_GAINS_IN_MASK];
33864f89611Schristos 	uint8_t pdadcs[AR_NUM_PDADC_VALUES];
33964f89611Schristos 	uint8_t xpdgains[AR9287_NUM_PD_GAINS];
34064f89611Schristos 	int8_t txpower;
34164f89611Schristos 	uint8_t overlap;
34264f89611Schristos 	uint32_t reg, offset;
34364f89611Schristos 	int i, j, nxpdgains;
34464f89611Schristos 
34564f89611Schristos 	if (sc->sc_eep_rev < AR_EEP_MINOR_VER_2) {
34664f89611Schristos 		overlap = MS(AR_READ(sc, AR_PHY_TPCRG5),
34764f89611Schristos 		    AR_PHY_TPCRG5_PD_GAIN_OVERLAP);
34864f89611Schristos 	}
34964f89611Schristos 	else
35064f89611Schristos 		overlap = eep->modalHeader.pdGainOverlap;
35164f89611Schristos 
35264f89611Schristos 	if (sc->sc_flags & ATHN_FLAG_OLPC) {
35364f89611Schristos 		/* XXX not here. */
35464f89611Schristos 		sc->sc_pdadc =
35564f89611Schristos 		    ((const struct ar_cal_data_per_freq_olpc *)
35664f89611Schristos 		     eep->calPierData2G[0])->vpdPdg[0][0];
35764f89611Schristos 	}
35864f89611Schristos 
35964f89611Schristos 	nxpdgains = 0;
36064f89611Schristos 	memset(xpdgains, 0, sizeof(xpdgains));
36164f89611Schristos 	for (i = AR9287_PD_GAINS_IN_MASK - 1; i >= 0; i--) {
36264f89611Schristos 		if (nxpdgains >= AR9287_NUM_PD_GAINS)
36364f89611Schristos 			break;		/* Can't happen. */
36464f89611Schristos 		if (eep->modalHeader.xpdGain & (1 << i))
36564f89611Schristos 			xpdgains[nxpdgains++] = i;
36664f89611Schristos 	}
36764f89611Schristos 	reg = AR_READ(sc, AR_PHY_TPCRG1);
36864f89611Schristos 	reg = RW(reg, AR_PHY_TPCRG1_NUM_PD_GAIN, nxpdgains - 1);
36964f89611Schristos 	reg = RW(reg, AR_PHY_TPCRG1_PD_GAIN_1, xpdgains[0]);
37064f89611Schristos 	reg = RW(reg, AR_PHY_TPCRG1_PD_GAIN_2, xpdgains[1]);
37164f89611Schristos 	AR_WRITE(sc, AR_PHY_TPCRG1, reg);
37264f89611Schristos 	AR_WRITE_BARRIER(sc);
37364f89611Schristos 
37464f89611Schristos 	for (i = 0; i < AR9287_MAX_CHAINS; i++)	{
37564f89611Schristos 		if (!(sc->sc_txchainmask & (1 << i)))
37664f89611Schristos 			continue;
37764f89611Schristos 
37864f89611Schristos 		offset = i * 0x1000;
37964f89611Schristos 
38064f89611Schristos 		if (sc->sc_flags & ATHN_FLAG_OLPC) {
38164f89611Schristos 			ar9287_olpc_get_pdgain(sc, c, i, &txpower);
38264f89611Schristos 
38364f89611Schristos 			reg = AR_READ(sc, AR_PHY_TX_PWRCTRL6_0);
38464f89611Schristos 			reg = RW(reg, AR_PHY_TX_PWRCTRL_ERR_EST_MODE, 3);
38564f89611Schristos 			AR_WRITE(sc, AR_PHY_TX_PWRCTRL6_0, reg);
38664f89611Schristos 
38764f89611Schristos 			reg = AR_READ(sc, AR_PHY_TX_PWRCTRL6_1);
38864f89611Schristos 			reg = RW(reg, AR_PHY_TX_PWRCTRL_ERR_EST_MODE, 3);
38964f89611Schristos 			AR_WRITE(sc, AR_PHY_TX_PWRCTRL6_1, reg);
39064f89611Schristos 
39164f89611Schristos 			/* NB: txpower is in half dB. */
39264f89611Schristos 			reg = AR_READ(sc, AR_PHY_CH0_TX_PWRCTRL11 + offset);
39364f89611Schristos 			reg = RW(reg, AR_PHY_TX_PWRCTRL_OLPC_PWR, txpower);
39464f89611Schristos 			AR_WRITE(sc, AR_PHY_CH0_TX_PWRCTRL11 + offset, reg);
39564f89611Schristos 
39664f89611Schristos 			AR_WRITE_BARRIER(sc);
39764f89611Schristos 			continue;	/* That's it for open loop mode. */
39864f89611Schristos 		}
39964f89611Schristos 
40064f89611Schristos 		/* Closed loop power control. */
40164f89611Schristos 		ar9287_get_pdadcs(sc, c, i, nxpdgains, overlap,
40264f89611Schristos 		    boundaries, pdadcs);
40364f89611Schristos 
40464f89611Schristos 		/* Write boundaries. */
40564f89611Schristos 		if (i == 0) {
40664f89611Schristos 			reg  = SM(AR_PHY_TPCRG5_PD_GAIN_OVERLAP,
40764f89611Schristos 			    overlap);
40864f89611Schristos 			reg |= SM(AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_1,
40964f89611Schristos 			    boundaries[0]);
41064f89611Schristos 			reg |= SM(AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_2,
41164f89611Schristos 			    boundaries[1]);
41264f89611Schristos 			reg |= SM(AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_3,
41364f89611Schristos 			    boundaries[2]);
41464f89611Schristos 			reg |= SM(AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_4,
41564f89611Schristos 			    boundaries[3]);
41664f89611Schristos 			AR_WRITE(sc, AR_PHY_TPCRG5 + offset, reg);
41764f89611Schristos 		}
41864f89611Schristos 		/* Write PDADC values. */
41964f89611Schristos 		for (j = 0; j < AR_NUM_PDADC_VALUES; j += 4) {
42064f89611Schristos 			AR_WRITE(sc, AR_PHY_PDADC_TBL_BASE + offset + j,
42164f89611Schristos 			    pdadcs[j + 0] <<  0 |
42264f89611Schristos 			    pdadcs[j + 1] <<  8 |
42364f89611Schristos 			    pdadcs[j + 2] << 16 |
42464f89611Schristos 			    pdadcs[j + 3] << 24);
42564f89611Schristos 		}
42664f89611Schristos 		AR_WRITE_BARRIER(sc);
42764f89611Schristos 	}
42864f89611Schristos }
42964f89611Schristos 
43064f89611Schristos Static void
ar9287_set_txpower(struct athn_softc * sc,struct ieee80211_channel * c,struct ieee80211_channel * extc)43164f89611Schristos ar9287_set_txpower(struct athn_softc *sc, struct ieee80211_channel *c,
43264f89611Schristos     struct ieee80211_channel *extc)
43364f89611Schristos {
43464f89611Schristos 	const struct ar9287_eeprom *eep = sc->sc_eep;
435b787afbfSchristos #ifdef notyet
43664f89611Schristos 	const struct ar9287_modal_eep_header *modal = &eep->modalHeader;
437b787afbfSchristos #endif
43864f89611Schristos 	uint8_t tpow_cck[4], tpow_ofdm[4];
43964f89611Schristos #ifndef IEEE80211_NO_HT
44064f89611Schristos 	uint8_t tpow_cck_ext[4], tpow_ofdm_ext[4];
44164f89611Schristos 	uint8_t tpow_ht20[8], tpow_ht40[8];
44264f89611Schristos 	uint8_t ht40inc;
44364f89611Schristos #endif
444b787afbfSchristos 	int16_t pwr = 0, power[ATHN_POWER_COUNT];
44564f89611Schristos 	int i;
44664f89611Schristos 
44764f89611Schristos 	ar9287_set_power_calib(sc, c);
44864f89611Schristos 
449b787afbfSchristos #ifdef notyet
45064f89611Schristos 	/* Compute transmit power reduction due to antenna gain. */
451b787afbfSchristos 	uint16_t max_ant_gain = MAX(modal->antennaGainCh[0], modal->antennaGainCh[1]);
45264f89611Schristos 	/* XXX */
453b787afbfSchristos #endif
45464f89611Schristos 
45564f89611Schristos 	/*
45664f89611Schristos 	 * Reduce scaled power by number of active chains to get per-chain
45764f89611Schristos 	 * transmit power level.
45864f89611Schristos 	 */
45964f89611Schristos 	if (sc->sc_ntxchains == 2)
46064f89611Schristos 		pwr -= AR_PWR_DECREASE_FOR_2_CHAIN;
46164f89611Schristos 	if (pwr < 0)
46264f89611Schristos 		pwr = 0;
46364f89611Schristos 
46464f89611Schristos 	/* Get CCK target powers. */
46564f89611Schristos 	ar5008_get_lg_tpow(sc, c, AR_CTL_11B, eep->calTargetPowerCck,
46664f89611Schristos 	    AR9287_NUM_2G_CCK_TARGET_POWERS, tpow_cck);
46764f89611Schristos 
46864f89611Schristos 	/* Get OFDM target powers. */
46964f89611Schristos 	ar5008_get_lg_tpow(sc, c, AR_CTL_11G, eep->calTargetPower2G,
47064f89611Schristos 	    AR9287_NUM_2G_20_TARGET_POWERS, tpow_ofdm);
47164f89611Schristos 
47264f89611Schristos #ifndef IEEE80211_NO_HT
47364f89611Schristos 	/* Get HT-20 target powers. */
47464f89611Schristos 	ar5008_get_ht_tpow(sc, c, AR_CTL_2GHT20, eep->calTargetPower2GHT20,
47564f89611Schristos 	    AR9287_NUM_2G_20_TARGET_POWERS, tpow_ht20);
47664f89611Schristos 
47764f89611Schristos 	if (extc != NULL) {
47864f89611Schristos 		/* Get HT-40 target powers. */
47964f89611Schristos 		ar5008_get_ht_tpow(sc, c, AR_CTL_2GHT40,
48064f89611Schristos 		    eep->calTargetPower2GHT40, AR9287_NUM_2G_40_TARGET_POWERS,
48164f89611Schristos 		    tpow_ht40);
48264f89611Schristos 
48364f89611Schristos 		/* Get secondary channel CCK target powers. */
48464f89611Schristos 		ar5008_get_lg_tpow(sc, extc, AR_CTL_11B,
48564f89611Schristos 		    eep->calTargetPowerCck, AR9287_NUM_2G_CCK_TARGET_POWERS,
48664f89611Schristos 		    tpow_cck_ext);
48764f89611Schristos 
48864f89611Schristos 		/* Get secondary channel OFDM target powers. */
48964f89611Schristos 		ar5008_get_lg_tpow(sc, extc, AR_CTL_11G,
49064f89611Schristos 		    eep->calTargetPower2G, AR9287_NUM_2G_20_TARGET_POWERS,
49164f89611Schristos 		    tpow_ofdm_ext);
49264f89611Schristos 	}
49364f89611Schristos #endif
49464f89611Schristos 
49564f89611Schristos 	memset(power, 0, sizeof(power));
49664f89611Schristos 	/* Shuffle target powers accross transmit rates. */
49764f89611Schristos 	power[ATHN_POWER_OFDM6   ] =
49864f89611Schristos 	power[ATHN_POWER_OFDM9   ] =
49964f89611Schristos 	power[ATHN_POWER_OFDM12  ] =
50064f89611Schristos 	power[ATHN_POWER_OFDM18  ] =
50164f89611Schristos 	power[ATHN_POWER_OFDM24  ] = tpow_ofdm[0];
50264f89611Schristos 	power[ATHN_POWER_OFDM36  ] = tpow_ofdm[1];
50364f89611Schristos 	power[ATHN_POWER_OFDM48  ] = tpow_ofdm[2];
50464f89611Schristos 	power[ATHN_POWER_OFDM54  ] = tpow_ofdm[3];
50564f89611Schristos 	power[ATHN_POWER_XR      ] = tpow_ofdm[0];
50664f89611Schristos 	power[ATHN_POWER_CCK1_LP ] = tpow_cck[0];
50764f89611Schristos 	power[ATHN_POWER_CCK2_LP ] =
50864f89611Schristos 	power[ATHN_POWER_CCK2_SP ] = tpow_cck[1];
50964f89611Schristos 	power[ATHN_POWER_CCK55_LP] =
51064f89611Schristos 	power[ATHN_POWER_CCK55_SP] = tpow_cck[2];
51164f89611Schristos 	power[ATHN_POWER_CCK11_LP] =
51264f89611Schristos 	power[ATHN_POWER_CCK11_SP] = tpow_cck[3];
51364f89611Schristos #ifndef IEEE80211_NO_HT
51464f89611Schristos 	for (i = 0; i < nitems(tpow_ht20); i++)
51564f89611Schristos 		power[ATHN_POWER_HT20(i)] = tpow_ht20[i];
51664f89611Schristos 	if (extc != NULL) {
51764f89611Schristos 		/* Correct PAR difference between HT40 and HT20/Legacy. */
51864f89611Schristos 		if (sc->sc_eep_rev >= AR_EEP_MINOR_VER_2)
51964f89611Schristos 			ht40inc = modal->ht40PowerIncForPdadc;
52064f89611Schristos 		else
52164f89611Schristos 			ht40inc = AR_HT40_POWER_INC_FOR_PDADC;
52264f89611Schristos 		for (i = 0; i < nitems(tpow_ht40); i++)
52364f89611Schristos 			power[ATHN_POWER_HT40(i)] = tpow_ht40[i] + ht40inc;
52464f89611Schristos 		power[ATHN_POWER_OFDM_DUP] = tpow_ht40[0];
52564f89611Schristos 		power[ATHN_POWER_CCK_DUP ] = tpow_ht40[0];
52664f89611Schristos 		power[ATHN_POWER_OFDM_EXT] = tpow_ofdm_ext[0];
52764f89611Schristos 		if (IEEE80211_IS_CHAN_2GHZ(c))
52864f89611Schristos 			power[ATHN_POWER_CCK_EXT] = tpow_cck_ext[0];
52964f89611Schristos 	}
53064f89611Schristos #endif
53164f89611Schristos 
53264f89611Schristos 	for (i = 0; i < ATHN_POWER_COUNT; i++) {
53364f89611Schristos 		power[i] -= AR_PWR_TABLE_OFFSET_DB * 2;	/* In half dB. */
53464f89611Schristos 		if (power[i] > AR_MAX_RATE_POWER)
53564f89611Schristos 			power[i] = AR_MAX_RATE_POWER;
53664f89611Schristos 	}
53764f89611Schristos 	/* Commit transmit power values to hardware. */
53864f89611Schristos 	ar5008_write_txpower(sc, power);
53964f89611Schristos }
54064f89611Schristos 
54164f89611Schristos Static void
ar9287_olpc_init(struct athn_softc * sc)54264f89611Schristos ar9287_olpc_init(struct athn_softc *sc)
54364f89611Schristos {
54464f89611Schristos 	uint32_t reg;
54564f89611Schristos 
54664f89611Schristos 	AR_SETBITS(sc, AR_PHY_TX_PWRCTRL9, AR_PHY_TX_PWRCTRL9_RES_DC_REMOVAL);
54764f89611Schristos 
54864f89611Schristos 	reg = AR_READ(sc, AR9287_AN_TXPC0);
54964f89611Schristos 	reg = RW(reg, AR9287_AN_TXPC0_TXPCMODE,
55064f89611Schristos 	    AR9287_AN_TXPC0_TXPCMODE_TEMPSENSE);
55164f89611Schristos 	AR_WRITE(sc, AR9287_AN_TXPC0, reg);
55264f89611Schristos 	AR_WRITE_BARRIER(sc);
55364f89611Schristos 	DELAY(100);
55464f89611Schristos }
55564f89611Schristos 
55664f89611Schristos Static void
ar9287_olpc_temp_compensation(struct athn_softc * sc)55764f89611Schristos ar9287_olpc_temp_compensation(struct athn_softc *sc)
55864f89611Schristos {
55964f89611Schristos 	const struct ar9287_eeprom *eep = sc->sc_eep;
56064f89611Schristos 	int8_t pdadc, slope, tcomp;
56164f89611Schristos 	uint32_t reg;
56264f89611Schristos 
56364f89611Schristos 	reg = AR_READ(sc, AR_PHY_TX_PWRCTRL4);
56464f89611Schristos 	pdadc = MS(reg, AR_PHY_TX_PWRCTRL_PD_AVG_OUT);
56564f89611Schristos 	DPRINTFN(DBG_RF, sc, "PD Avg Out=%d\n", pdadc);
56664f89611Schristos 
56764f89611Schristos 	if (sc->sc_pdadc == 0 || pdadc == 0)
56864f89611Schristos 		return;	/* No frames transmitted yet. */
56964f89611Schristos 
57064f89611Schristos 	/* Compute Tx gain temperature compensation. */
57164f89611Schristos 	if (sc->sc_eep_rev >= AR_EEP_MINOR_VER_2)
57264f89611Schristos 		slope = eep->baseEepHeader.tempSensSlope;
57364f89611Schristos 	else
57464f89611Schristos 		slope = 0;
57564f89611Schristos 	if (slope != 0)	/* Prevents division by zero. */
57664f89611Schristos 		tcomp = ((pdadc - sc->sc_pdadc) * 4) / slope;
57764f89611Schristos 	else
57864f89611Schristos 		tcomp = 0;
57964f89611Schristos 	DPRINTFN(DBG_RF, sc, "OLPC temp compensation=%d\n", tcomp);
58064f89611Schristos 
58164f89611Schristos 	/* Write compensation value for both Tx chains. */
58264f89611Schristos 	reg = AR_READ(sc, AR_PHY_CH0_TX_PWRCTRL11);
58364f89611Schristos 	reg = RW(reg, AR_PHY_TX_PWRCTRL_OLPC_TEMP_COMP, tcomp);
58464f89611Schristos 	AR_WRITE(sc, AR_PHY_CH0_TX_PWRCTRL11, reg);
58564f89611Schristos 
58664f89611Schristos 	reg = AR_READ(sc, AR_PHY_CH1_TX_PWRCTRL11);
58764f89611Schristos 	reg = RW(reg, AR_PHY_TX_PWRCTRL_OLPC_TEMP_COMP, tcomp);
58864f89611Schristos 	AR_WRITE(sc, AR_PHY_CH1_TX_PWRCTRL11, reg);
58964f89611Schristos 	AR_WRITE_BARRIER(sc);
59064f89611Schristos }
59164f89611Schristos 
59264f89611Schristos PUBLIC void
ar9287_1_3_enable_async_fifo(struct athn_softc * sc)59364f89611Schristos ar9287_1_3_enable_async_fifo(struct athn_softc *sc)
59464f89611Schristos {
59564f89611Schristos 
59664f89611Schristos 	/* Enable ASYNC FIFO. */
59764f89611Schristos 	AR_SETBITS(sc, AR_MAC_PCU_ASYNC_FIFO_REG3,
59864f89611Schristos 	    AR_MAC_PCU_ASYNC_FIFO_REG3_DATAPATH_SEL);
59964f89611Schristos 	AR_SETBITS(sc, AR_PHY_MODE, AR_PHY_MODE_ASYNCFIFO);
60064f89611Schristos 	AR_CLRBITS(sc, AR_MAC_PCU_ASYNC_FIFO_REG3,
60164f89611Schristos 	    AR_MAC_PCU_ASYNC_FIFO_REG3_SOFT_RESET);
60264f89611Schristos 	AR_SETBITS(sc, AR_MAC_PCU_ASYNC_FIFO_REG3,
60364f89611Schristos 	    AR_MAC_PCU_ASYNC_FIFO_REG3_SOFT_RESET);
60464f89611Schristos 	AR_WRITE_BARRIER(sc);
60564f89611Schristos }
60664f89611Schristos 
60764f89611Schristos PUBLIC void
ar9287_1_3_setup_async_fifo(struct athn_softc * sc)60864f89611Schristos ar9287_1_3_setup_async_fifo(struct athn_softc *sc)
60964f89611Schristos {
61064f89611Schristos 	uint32_t reg;
61164f89611Schristos 
61264f89611Schristos 	/*
61364f89611Schristos 	 * MAC runs at 117MHz (instead of 88/44MHz) when ASYNC FIFO is
61464f89611Schristos 	 * enabled, so the following counters have to be changed.
61564f89611Schristos 	 */
61664f89611Schristos 	AR_WRITE(sc, AR_D_GBL_IFS_SIFS, AR_D_GBL_IFS_SIFS_ASYNC_FIFO_DUR);
61764f89611Schristos 	AR_WRITE(sc, AR_D_GBL_IFS_SLOT, AR_D_GBL_IFS_SLOT_ASYNC_FIFO_DUR);
61864f89611Schristos 	AR_WRITE(sc, AR_D_GBL_IFS_EIFS, AR_D_GBL_IFS_EIFS_ASYNC_FIFO_DUR);
61964f89611Schristos 
62064f89611Schristos 	AR_WRITE(sc, AR_TIME_OUT, AR_TIME_OUT_ACK_CTS_ASYNC_FIFO_DUR);
62164f89611Schristos 	AR_WRITE(sc, AR_USEC, AR_USEC_ASYNC_FIFO_DUR);
62264f89611Schristos 
62364f89611Schristos 	AR_SETBITS(sc, AR_MAC_PCU_LOGIC_ANALYZER,
62464f89611Schristos 	    AR_MAC_PCU_LOGIC_ANALYZER_DISBUG20768);
62564f89611Schristos 
62664f89611Schristos 	reg = AR_READ(sc, AR_AHB_MODE);
62764f89611Schristos 	reg = RW(reg, AR_AHB_CUSTOM_BURST, AR_AHB_CUSTOM_BURST_ASYNC_FIFO_VAL);
62864f89611Schristos 	AR_WRITE(sc, AR_AHB_MODE, reg);
62964f89611Schristos 
63064f89611Schristos 	AR_SETBITS(sc, AR_PCU_MISC_MODE2, AR_PCU_MISC_MODE2_ENABLE_AGGWEP);
63164f89611Schristos 	AR_WRITE_BARRIER(sc);
63264f89611Schristos }
633