xref: /netbsd-src/sys/dev/ic/arn5008.c (revision 481d3881954fd794ca5f2d880b68c53a5db8620e)
1*481d3881Srin /*	$NetBSD: arn5008.c,v 1.20 2024/07/05 04:31:50 rin Exp $	*/
264f89611Schristos /*	$OpenBSD: ar5008.c,v 1.21 2012/08/25 12:14:31 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 common to AR5008, AR9001 and AR9002 families.
2464f89611Schristos  */
2564f89611Schristos 
2664f89611Schristos #include <sys/cdefs.h>
27*481d3881Srin __KERNEL_RCSID(0, "$NetBSD: arn5008.c,v 1.20 2024/07/05 04:31:50 rin 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/malloc.h>
3664f89611Schristos #include <sys/queue.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/arn5008.h>
6664f89611Schristos #include <dev/ic/arn5416.h>
6764f89611Schristos #include <dev/ic/arn9280.h>
6864f89611Schristos 
6964f89611Schristos #define Static static
7064f89611Schristos 
7164f89611Schristos Static void	ar5008_calib_adc_dc_off(struct athn_softc *);
7264f89611Schristos Static void	ar5008_calib_adc_gain(struct athn_softc *);
7364f89611Schristos Static void	ar5008_calib_iq(struct athn_softc *);
7464f89611Schristos Static void	ar5008_disable_ofdm_weak_signal(struct athn_softc *);
7564f89611Schristos Static void	ar5008_disable_phy(struct athn_softc *);
7664f89611Schristos Static int	ar5008_dma_alloc(struct athn_softc *);
7764f89611Schristos Static void	ar5008_dma_free(struct athn_softc *);
7864f89611Schristos Static void	ar5008_do_calib(struct athn_softc *);
7964f89611Schristos Static void	ar5008_do_noisefloor_calib(struct athn_softc *);
8064f89611Schristos Static void	ar5008_enable_antenna_diversity(struct athn_softc *);
8164f89611Schristos Static void	ar5008_enable_ofdm_weak_signal(struct athn_softc *);
8264f89611Schristos Static uint8_t	ar5008_get_vpd(uint8_t, const uint8_t *, const uint8_t *, int);
8364f89611Schristos Static void	ar5008_gpio_config_input(struct athn_softc *, int);
8464f89611Schristos Static void	ar5008_gpio_config_output(struct athn_softc *, int, int);
8564f89611Schristos Static int	ar5008_gpio_read(struct athn_softc *, int);
8664f89611Schristos Static void	ar5008_gpio_write(struct athn_softc *, int, int);
8764f89611Schristos Static void	ar5008_hw_init(struct athn_softc *, struct ieee80211_channel *,
8864f89611Schristos 		    struct ieee80211_channel *);
8964f89611Schristos Static void	ar5008_init_baseband(struct athn_softc *);
9064f89611Schristos Static void	ar5008_init_chains(struct athn_softc *);
915ffb0503Snonaka Static int	ar5008_intr_status(struct athn_softc *);
9264f89611Schristos Static int	ar5008_intr(struct athn_softc *);
9364f89611Schristos Static void	ar5008_next_calib(struct athn_softc *);
9464f89611Schristos Static int	ar5008_read_eep_word(struct athn_softc *, uint32_t,
9564f89611Schristos 		    uint16_t *);
9664f89611Schristos Static int	ar5008_read_rom(struct athn_softc *);
9764f89611Schristos Static void	ar5008_rf_bus_release(struct athn_softc *);
9864f89611Schristos Static int	ar5008_rf_bus_request(struct athn_softc *);
9964f89611Schristos Static void	ar5008_rfsilent_init(struct athn_softc *);
10064f89611Schristos Static int	ar5008_rx_alloc(struct athn_softc *);
10164f89611Schristos Static void	ar5008_rx_enable(struct athn_softc *);
10264f89611Schristos Static void	ar5008_rx_free(struct athn_softc *);
10364f89611Schristos Static void	ar5008_rx_intr(struct athn_softc *);
10464f89611Schristos Static void	ar5008_rx_radiotap(struct athn_softc *, struct mbuf *,
10564f89611Schristos 		    struct ar_rx_desc *);
10664f89611Schristos Static void	ar5008_set_cck_weak_signal(struct athn_softc *, int);
10764f89611Schristos Static void	ar5008_set_delta_slope(struct athn_softc *,
10864f89611Schristos 		    struct ieee80211_channel *, struct ieee80211_channel *);
10964f89611Schristos Static void	ar5008_set_firstep_level(struct athn_softc *, int);
11064f89611Schristos Static void	ar5008_set_noise_immunity_level(struct athn_softc *, int);
11164f89611Schristos Static void	ar5008_set_phy(struct athn_softc *, struct ieee80211_channel *,
11264f89611Schristos 		    struct ieee80211_channel *);
11364f89611Schristos Static void	ar5008_set_rf_mode(struct athn_softc *,
11464f89611Schristos 		    struct ieee80211_channel *);
11564f89611Schristos Static void	ar5008_set_rxchains(struct athn_softc *);
11664f89611Schristos Static void	ar5008_set_spur_immunity_level(struct athn_softc *, int);
11764f89611Schristos Static void	ar5008_swap_rom(struct athn_softc *);
11864f89611Schristos Static int	ar5008_swba_intr(struct athn_softc *);
11964f89611Schristos Static int	ar5008_tx(struct athn_softc *, struct mbuf *,
12064f89611Schristos 		    struct ieee80211_node *, int);
12164f89611Schristos Static int	ar5008_tx_alloc(struct athn_softc *);
12264f89611Schristos Static void	ar5008_tx_free(struct athn_softc *);
12364f89611Schristos Static void	ar5008_tx_intr(struct athn_softc *);
12464f89611Schristos Static int	ar5008_tx_process(struct athn_softc *, int);
12564f89611Schristos 
12664f89611Schristos #ifdef notused
12764f89611Schristos Static void	ar5008_bb_load_noisefloor(struct athn_softc *);
12864f89611Schristos Static void	ar5008_get_noisefloor(struct athn_softc *,
12964f89611Schristos 		    struct ieee80211_channel *);
13064f89611Schristos Static void	ar5008_noisefloor_calib(struct athn_softc *);
13164f89611Schristos Static void	ar5008_read_noisefloor(struct athn_softc *, int16_t *,
13264f89611Schristos 		    int16_t *);
13364f89611Schristos Static void	ar5008_write_noisefloor(struct athn_softc *, int16_t *,
13464f89611Schristos 		    int16_t *);
13564f89611Schristos #endif /* notused */
13664f89611Schristos 
13764f89611Schristos // bf->bf_m = MCLGETI(NULL, M_DONTWAIT, NULL, ATHN_RXBUFSZ);
13864f89611Schristos 
13964f89611Schristos /*
14064f89611Schristos  * XXX: see if_iwn.c:MCLGETIalt() for a better solution.
14164f89611Schristos  */
14264f89611Schristos static struct mbuf *
MCLGETI(struct athn_softc * sc __unused,int how,struct ifnet * ifp __unused,u_int size)14364f89611Schristos MCLGETI(struct athn_softc *sc __unused, int how,
14464f89611Schristos     struct ifnet *ifp __unused, u_int size)
14564f89611Schristos {
14664f89611Schristos 	struct mbuf *m;
14764f89611Schristos 
14864f89611Schristos 	MGETHDR(m, how, MT_DATA);
14964f89611Schristos 	if (m == NULL)
15064f89611Schristos 		return NULL;
15164f89611Schristos 
15264f89611Schristos 	MEXTMALLOC(m, size, how);
15364f89611Schristos 	if ((m->m_flags & M_EXT) == 0) {
15464f89611Schristos 		m_freem(m);
15564f89611Schristos 		return NULL;
15664f89611Schristos 	}
15764f89611Schristos 	return m;
15864f89611Schristos }
15964f89611Schristos 
16064f89611Schristos PUBLIC int
ar5008_attach(struct athn_softc * sc)16164f89611Schristos ar5008_attach(struct athn_softc *sc)
16264f89611Schristos {
16364f89611Schristos 	struct athn_ops *ops = &sc->sc_ops;
16464f89611Schristos 	struct ieee80211com *ic = &sc->sc_ic;
16564f89611Schristos 	struct ar_base_eep_header *base;
16664f89611Schristos 	uint8_t eep_ver, kc_entries_log;
16764f89611Schristos 	int error;
16864f89611Schristos 
16964f89611Schristos 	/* Set callbacks for AR5008, AR9001 and AR9002 families. */
17064f89611Schristos 	ops->gpio_read = ar5008_gpio_read;
17164f89611Schristos 	ops->gpio_write = ar5008_gpio_write;
17264f89611Schristos 	ops->gpio_config_input = ar5008_gpio_config_input;
17364f89611Schristos 	ops->gpio_config_output = ar5008_gpio_config_output;
17464f89611Schristos 	ops->rfsilent_init = ar5008_rfsilent_init;
17564f89611Schristos 
17664f89611Schristos 	ops->dma_alloc = ar5008_dma_alloc;
17764f89611Schristos 	ops->dma_free = ar5008_dma_free;
17864f89611Schristos 	ops->rx_enable = ar5008_rx_enable;
1795ffb0503Snonaka 	ops->intr_status = ar5008_intr_status;
18064f89611Schristos 	ops->intr = ar5008_intr;
18164f89611Schristos 	ops->tx = ar5008_tx;
18264f89611Schristos 
18364f89611Schristos 	ops->set_rf_mode = ar5008_set_rf_mode;
18464f89611Schristos 	ops->rf_bus_request = ar5008_rf_bus_request;
18564f89611Schristos 	ops->rf_bus_release = ar5008_rf_bus_release;
18664f89611Schristos 	ops->set_phy = ar5008_set_phy;
18764f89611Schristos 	ops->set_delta_slope = ar5008_set_delta_slope;
18864f89611Schristos 	ops->enable_antenna_diversity = ar5008_enable_antenna_diversity;
18964f89611Schristos 	ops->init_baseband = ar5008_init_baseband;
19064f89611Schristos 	ops->disable_phy = ar5008_disable_phy;
19164f89611Schristos 	ops->set_rxchains = ar5008_set_rxchains;
19264f89611Schristos 	ops->noisefloor_calib = ar5008_do_noisefloor_calib;
19364f89611Schristos 	ops->do_calib = ar5008_do_calib;
19464f89611Schristos 	ops->next_calib = ar5008_next_calib;
19564f89611Schristos 	ops->hw_init = ar5008_hw_init;
19664f89611Schristos 
19764f89611Schristos 	ops->set_noise_immunity_level = ar5008_set_noise_immunity_level;
19864f89611Schristos 	ops->enable_ofdm_weak_signal = ar5008_enable_ofdm_weak_signal;
19964f89611Schristos 	ops->disable_ofdm_weak_signal = ar5008_disable_ofdm_weak_signal;
20064f89611Schristos 	ops->set_cck_weak_signal = ar5008_set_cck_weak_signal;
20164f89611Schristos 	ops->set_firstep_level = ar5008_set_firstep_level;
20264f89611Schristos 	ops->set_spur_immunity_level = ar5008_set_spur_immunity_level;
20364f89611Schristos 
20464f89611Schristos 	/* Set MAC registers offsets. */
20564f89611Schristos 	sc->sc_obs_off = AR_OBS;
20664f89611Schristos 	sc->sc_gpio_input_en_off = AR_GPIO_INPUT_EN_VAL;
20764f89611Schristos 
20864f89611Schristos 	if (!(sc->sc_flags & ATHN_FLAG_PCIE))
20964f89611Schristos 		athn_config_nonpcie(sc);
21064f89611Schristos 	else
21164f89611Schristos 		athn_config_pcie(sc);
21264f89611Schristos 
21364f89611Schristos 	/* Read entire ROM content in memory. */
21464f89611Schristos 	if ((error = ar5008_read_rom(sc)) != 0) {
215dabffed6Schristos 		aprint_error_dev(sc->sc_dev, "could not read ROM\n");
21664f89611Schristos 		return error;
21764f89611Schristos 	}
21864f89611Schristos 
21964f89611Schristos 	/* Get RF revision. */
22064f89611Schristos 	sc->sc_rf_rev = ar5416_get_rf_rev(sc);
22164f89611Schristos 
22264f89611Schristos 	base = sc->sc_eep;
22364f89611Schristos 	eep_ver = (base->version >> 12) & 0xf;
22464f89611Schristos 	sc->sc_eep_rev = (base->version & 0xfff);
22564f89611Schristos 	if (eep_ver != AR_EEP_VER || sc->sc_eep_rev == 0) {
226dabffed6Schristos 		aprint_error_dev(sc->sc_dev, "unsupported ROM version %d.%d\n",
227dabffed6Schristos 		    eep_ver, sc->sc_eep_rev);
22864f89611Schristos 		return EINVAL;
22964f89611Schristos 	}
23064f89611Schristos 
23164f89611Schristos 	if (base->opCapFlags & AR_OPFLAGS_11A)
23264f89611Schristos 		sc->sc_flags |= ATHN_FLAG_11A;
23364f89611Schristos 	if (base->opCapFlags & AR_OPFLAGS_11G)
23464f89611Schristos 		sc->sc_flags |= ATHN_FLAG_11G;
23564f89611Schristos 	if (base->opCapFlags & AR_OPFLAGS_11N)
23664f89611Schristos 		sc->sc_flags |= ATHN_FLAG_11N;
23764f89611Schristos 
23864f89611Schristos 	IEEE80211_ADDR_COPY(ic->ic_myaddr, base->macAddr);
23964f89611Schristos 
24064f89611Schristos 	/* Check if we have a hardware radio switch. */
24164f89611Schristos 	if (base->rfSilent & AR_EEP_RFSILENT_ENABLED) {
24264f89611Schristos 		sc->sc_flags |= ATHN_FLAG_RFSILENT;
24364f89611Schristos 		/* Get GPIO pin used by hardware radio switch. */
24464f89611Schristos 		sc->sc_rfsilent_pin = MS(base->rfSilent,
24564f89611Schristos 		    AR_EEP_RFSILENT_GPIO_SEL);
24664f89611Schristos 		/* Get polarity of hardware radio switch. */
24764f89611Schristos 		if (base->rfSilent & AR_EEP_RFSILENT_POLARITY)
24864f89611Schristos 			sc->sc_flags |= ATHN_FLAG_RFSILENT_REVERSED;
24964f89611Schristos 	}
25064f89611Schristos 
25164f89611Schristos 	/* Get the number of HW key cache entries. */
25264f89611Schristos 	kc_entries_log = MS(base->deviceCap, AR_EEP_DEVCAP_KC_ENTRIES);
25364f89611Schristos 	sc->sc_kc_entries = kc_entries_log != 0 ?
25464f89611Schristos 	    1 << kc_entries_log : AR_KEYTABLE_SIZE;
25564f89611Schristos 
25664f89611Schristos 	sc->sc_txchainmask = base->txMask;
25764f89611Schristos 	if (sc->sc_mac_ver == AR_SREV_VERSION_5416_PCI &&
25864f89611Schristos 	    !(base->opCapFlags & AR_OPFLAGS_11A)) {
25964f89611Schristos 		/* For single-band AR5416 PCI, use GPIO pin 0. */
26064f89611Schristos 		sc->sc_rxchainmask = ar5008_gpio_read(sc, 0) ? 0x5 : 0x7;
26164f89611Schristos 	}
26264f89611Schristos 	else
26364f89611Schristos 		sc->sc_rxchainmask = base->rxMask;
26464f89611Schristos 
26564f89611Schristos 	ops->setup(sc);
26664f89611Schristos 	return 0;
26764f89611Schristos }
26864f89611Schristos 
26964f89611Schristos /*
27064f89611Schristos  * Read 16-bit word from ROM.
27164f89611Schristos  */
27264f89611Schristos Static int
ar5008_read_eep_word(struct athn_softc * sc,uint32_t addr,uint16_t * val)27364f89611Schristos ar5008_read_eep_word(struct athn_softc *sc, uint32_t addr, uint16_t *val)
27464f89611Schristos {
27564f89611Schristos 	uint32_t reg;
27664f89611Schristos 	int ntries;
27764f89611Schristos 
27864f89611Schristos 	reg = AR_READ(sc, AR_EEPROM_OFFSET(addr));
27964f89611Schristos 	for (ntries = 0; ntries < 1000; ntries++) {
28064f89611Schristos 		reg = AR_READ(sc, AR_EEPROM_STATUS_DATA);
28164f89611Schristos 		if (!(reg & (AR_EEPROM_STATUS_DATA_BUSY |
28264f89611Schristos 		    AR_EEPROM_STATUS_DATA_PROT_ACCESS))) {
28364f89611Schristos 			*val = MS(reg, AR_EEPROM_STATUS_DATA_VAL);
28464f89611Schristos 			return 0;
28564f89611Schristos 		}
28664f89611Schristos 		DELAY(10);
28764f89611Schristos 	}
28864f89611Schristos 	*val = 0xffff;
28964f89611Schristos 	return ETIMEDOUT;
29064f89611Schristos }
29164f89611Schristos 
29264f89611Schristos Static int
ar5008_read_rom(struct athn_softc * sc)29364f89611Schristos ar5008_read_rom(struct athn_softc *sc)
29464f89611Schristos {
29564f89611Schristos 	uint32_t addr, end;
29664f89611Schristos 	uint16_t magic, sum, *eep;
29764f89611Schristos 	int need_swap = 0;
29864f89611Schristos 	int error;
29964f89611Schristos 
30064f89611Schristos 	/* Determine ROM endianness. */
30164f89611Schristos 	error = ar5008_read_eep_word(sc, AR_EEPROM_MAGIC_OFFSET, &magic);
30264f89611Schristos 	if (error != 0)
30364f89611Schristos 		return error;
30464f89611Schristos 	if (magic != AR_EEPROM_MAGIC) {
30564f89611Schristos 		if (magic != bswap16(AR_EEPROM_MAGIC)) {
30664f89611Schristos 			DPRINTFN(DBG_INIT, sc,
30764f89611Schristos 			    "invalid ROM magic 0x%x != 0x%x\n",
30864f89611Schristos 			    magic, AR_EEPROM_MAGIC);
30964f89611Schristos 			return EIO;
31064f89611Schristos 		}
31164f89611Schristos 		DPRINTFN(DBG_INIT, sc, "non-native ROM endianness\n");
31264f89611Schristos 		need_swap = 1;
31364f89611Schristos 	}
31464f89611Schristos 
31564f89611Schristos 	/* Allocate space to store ROM in host memory. */
316d47bcd29Schs 	sc->sc_eep = malloc(sc->sc_eep_size, M_DEVBUF, M_WAITOK);
31764f89611Schristos 
31864f89611Schristos 	/* Read entire ROM and compute checksum. */
31964f89611Schristos 	sum = 0;
32064f89611Schristos 	eep = sc->sc_eep;
32164f89611Schristos 	end = sc->sc_eep_base + sc->sc_eep_size / sizeof(uint16_t);
32264f89611Schristos 	for (addr = sc->sc_eep_base; addr < end; addr++, eep++) {
32364f89611Schristos 		if ((error = ar5008_read_eep_word(sc, addr, eep)) != 0) {
32464f89611Schristos 			DPRINTFN(DBG_INIT, sc,
32564f89611Schristos 			    "could not read ROM at 0x%x\n", addr);
32664f89611Schristos 			return error;
32764f89611Schristos 		}
32864f89611Schristos 		if (need_swap)
32964f89611Schristos 			*eep = bswap16(*eep);
33064f89611Schristos 		sum ^= *eep;
33164f89611Schristos 	}
33264f89611Schristos 	if (sum != 0xffff) {
333dabffed6Schristos 		aprint_error_dev(sc->sc_dev, "bad ROM checksum 0x%04x\n", sum);
33464f89611Schristos 		return EIO;
33564f89611Schristos 	}
33664f89611Schristos 	if (need_swap)
33764f89611Schristos 		ar5008_swap_rom(sc);
33864f89611Schristos 
33964f89611Schristos 	return 0;
34064f89611Schristos }
34164f89611Schristos 
34264f89611Schristos Static void
ar5008_swap_rom(struct athn_softc * sc)34364f89611Schristos ar5008_swap_rom(struct athn_softc *sc)
34464f89611Schristos {
34564f89611Schristos 	struct ar_base_eep_header *base = sc->sc_eep;
34664f89611Schristos 
34764f89611Schristos 	/* Swap common fields first. */
34864f89611Schristos 	base->length = bswap16(base->length);
34964f89611Schristos 	base->version = bswap16(base->version);
35064f89611Schristos 	base->regDmn[0] = bswap16(base->regDmn[0]);
35164f89611Schristos 	base->regDmn[1] = bswap16(base->regDmn[1]);
35264f89611Schristos 	base->rfSilent = bswap16(base->rfSilent);
35364f89611Schristos 	base->blueToothOptions = bswap16(base->blueToothOptions);
35464f89611Schristos 	base->deviceCap = bswap16(base->deviceCap);
35564f89611Schristos 
35664f89611Schristos 	/* Swap device-dependent fields. */
35764f89611Schristos 	sc->sc_ops.swap_rom(sc);
35864f89611Schristos }
35964f89611Schristos 
36064f89611Schristos /*
36164f89611Schristos  * Access to General Purpose Input/Output ports.
36264f89611Schristos  */
36364f89611Schristos Static int
ar5008_gpio_read(struct athn_softc * sc,int pin)36464f89611Schristos ar5008_gpio_read(struct athn_softc *sc, int pin)
36564f89611Schristos {
36664f89611Schristos 
36764f89611Schristos 	KASSERT(pin < sc->sc_ngpiopins);
36864f89611Schristos 	if ((sc->sc_flags & ATHN_FLAG_USB) && !AR_SREV_9271(sc))
36964f89611Schristos 		return !((AR_READ(sc, AR7010_GPIO_IN) >> pin) & 1);
37064f89611Schristos 	return (AR_READ(sc, AR_GPIO_IN_OUT) >> (sc->sc_ngpiopins + pin)) & 1;
37164f89611Schristos }
37264f89611Schristos 
37364f89611Schristos Static void
ar5008_gpio_write(struct athn_softc * sc,int pin,int set)37464f89611Schristos ar5008_gpio_write(struct athn_softc *sc, int pin, int set)
37564f89611Schristos {
37664f89611Schristos 	uint32_t reg;
37764f89611Schristos 
37864f89611Schristos 	KASSERT(pin < sc->sc_ngpiopins);
37964f89611Schristos 
38064f89611Schristos 	if (sc->sc_flags & ATHN_FLAG_USB)
38164f89611Schristos 		set = !set;	/* AR9271/AR7010 is reversed. */
38264f89611Schristos 
38364f89611Schristos 	if ((sc->sc_flags & ATHN_FLAG_USB) && !AR_SREV_9271(sc)) {
38464f89611Schristos 		/* Special case for AR7010. */
38564f89611Schristos 		reg = AR_READ(sc, AR7010_GPIO_OUT);
38664f89611Schristos 		if (set)
38764f89611Schristos 			reg |= 1 << pin;
38864f89611Schristos 		else
38964f89611Schristos 			reg &= ~(1 << pin);
39064f89611Schristos 		AR_WRITE(sc, AR7010_GPIO_OUT, reg);
39164f89611Schristos 	}
39264f89611Schristos 	else {
39364f89611Schristos 		reg = AR_READ(sc, AR_GPIO_IN_OUT);
39464f89611Schristos 		if (set)
39564f89611Schristos 			reg |= 1 << pin;
39664f89611Schristos 		else
39764f89611Schristos 			reg &= ~(1 << pin);
39864f89611Schristos 		AR_WRITE(sc, AR_GPIO_IN_OUT, reg);
39964f89611Schristos 	}
40064f89611Schristos 	AR_WRITE_BARRIER(sc);
40164f89611Schristos }
40264f89611Schristos 
40364f89611Schristos Static void
ar5008_gpio_config_input(struct athn_softc * sc,int pin)40464f89611Schristos ar5008_gpio_config_input(struct athn_softc *sc, int pin)
40564f89611Schristos {
40664f89611Schristos 	uint32_t reg;
40764f89611Schristos 
40864f89611Schristos 	if ((sc->sc_flags & ATHN_FLAG_USB) && !AR_SREV_9271(sc)) {
40964f89611Schristos 		/* Special case for AR7010. */
41064f89611Schristos 		AR_SETBITS(sc, AR7010_GPIO_OE, 1 << pin);
41164f89611Schristos 	}
41264f89611Schristos 	else {
41364f89611Schristos 		reg = AR_READ(sc, AR_GPIO_OE_OUT);
41464f89611Schristos 		reg &= ~(AR_GPIO_OE_OUT_DRV_M << (pin * 2));
41564f89611Schristos 		reg |= AR_GPIO_OE_OUT_DRV_NO << (pin * 2);
41664f89611Schristos 		AR_WRITE(sc, AR_GPIO_OE_OUT, reg);
41764f89611Schristos 	}
41864f89611Schristos 	AR_WRITE_BARRIER(sc);
41964f89611Schristos }
42064f89611Schristos 
42164f89611Schristos Static void
ar5008_gpio_config_output(struct athn_softc * sc,int pin,int type)42264f89611Schristos ar5008_gpio_config_output(struct athn_softc *sc, int pin, int type)
42364f89611Schristos {
42464f89611Schristos 	uint32_t reg;
42564f89611Schristos 	int mux, off;
42664f89611Schristos 
42764f89611Schristos 	if ((sc->sc_flags & ATHN_FLAG_USB) && !AR_SREV_9271(sc)) {
42864f89611Schristos 		/* Special case for AR7010. */
42964f89611Schristos 		AR_CLRBITS(sc, AR7010_GPIO_OE, 1 << pin);
43064f89611Schristos 		AR_WRITE_BARRIER(sc);
43164f89611Schristos 		return;
43264f89611Schristos 	}
43364f89611Schristos 	mux = pin / 6;
43464f89611Schristos 	off = pin % 6;
43564f89611Schristos 
43664f89611Schristos 	reg = AR_READ(sc, AR_GPIO_OUTPUT_MUX(mux));
43764f89611Schristos 	if (!AR_SREV_9280_20_OR_LATER(sc) && mux == 0)
43864f89611Schristos 		reg = (reg & ~0x1f0) | (reg & 0x1f0) << 1;
43964f89611Schristos 	reg &= ~(0x1f << (off * 5));
44064f89611Schristos 	reg |= (type & 0x1f) << (off * 5);
44164f89611Schristos 	AR_WRITE(sc, AR_GPIO_OUTPUT_MUX(mux), reg);
44264f89611Schristos 
44364f89611Schristos 	reg = AR_READ(sc, AR_GPIO_OE_OUT);
44464f89611Schristos 	reg &= ~(AR_GPIO_OE_OUT_DRV_M << (pin * 2));
44564f89611Schristos 	reg |= AR_GPIO_OE_OUT_DRV_ALL << (pin * 2);
44664f89611Schristos 	AR_WRITE(sc, AR_GPIO_OE_OUT, reg);
44764f89611Schristos 	AR_WRITE_BARRIER(sc);
44864f89611Schristos }
44964f89611Schristos 
45064f89611Schristos Static void
ar5008_rfsilent_init(struct athn_softc * sc)45164f89611Schristos ar5008_rfsilent_init(struct athn_softc *sc)
45264f89611Schristos {
45364f89611Schristos 	uint32_t reg;
45464f89611Schristos 
45564f89611Schristos 	/* Configure hardware radio switch. */
45664f89611Schristos 	AR_SETBITS(sc, AR_GPIO_INPUT_EN_VAL, AR_GPIO_INPUT_EN_VAL_RFSILENT_BB);
45764f89611Schristos 	reg = AR_READ(sc, AR_GPIO_INPUT_MUX2);
45864f89611Schristos 	reg = RW(reg, AR_GPIO_INPUT_MUX2_RFSILENT, 0);
45964f89611Schristos 	AR_WRITE(sc, AR_GPIO_INPUT_MUX2, reg);
46064f89611Schristos 	ar5008_gpio_config_input(sc, sc->sc_rfsilent_pin);
46164f89611Schristos 	AR_SETBITS(sc, AR_PHY_TEST, AR_PHY_TEST_RFSILENT_BB);
46264f89611Schristos 	if (!(sc->sc_flags & ATHN_FLAG_RFSILENT_REVERSED)) {
46364f89611Schristos 		AR_SETBITS(sc, AR_GPIO_INTR_POL,
46464f89611Schristos 		    AR_GPIO_INTR_POL_PIN(sc->sc_rfsilent_pin));
46564f89611Schristos 	}
46664f89611Schristos 	AR_WRITE_BARRIER(sc);
46764f89611Schristos }
46864f89611Schristos 
46964f89611Schristos Static int
ar5008_dma_alloc(struct athn_softc * sc)47064f89611Schristos ar5008_dma_alloc(struct athn_softc *sc)
47164f89611Schristos {
47264f89611Schristos 	int error;
47364f89611Schristos 
47464f89611Schristos 	error = ar5008_tx_alloc(sc);
47564f89611Schristos 	if (error != 0)
47664f89611Schristos 		return error;
47764f89611Schristos 
47864f89611Schristos 	error = ar5008_rx_alloc(sc);
47964f89611Schristos 	if (error != 0)
48064f89611Schristos 		return error;
48164f89611Schristos 
48264f89611Schristos 	return 0;
48364f89611Schristos }
48464f89611Schristos 
48564f89611Schristos Static void
ar5008_dma_free(struct athn_softc * sc)48664f89611Schristos ar5008_dma_free(struct athn_softc *sc)
48764f89611Schristos {
48864f89611Schristos 
48964f89611Schristos 	ar5008_tx_free(sc);
49064f89611Schristos 	ar5008_rx_free(sc);
49164f89611Schristos }
49264f89611Schristos 
49364f89611Schristos Static int
ar5008_tx_alloc(struct athn_softc * sc)49464f89611Schristos ar5008_tx_alloc(struct athn_softc *sc)
49564f89611Schristos {
49664f89611Schristos 	struct athn_tx_buf *bf;
49764f89611Schristos 	bus_size_t size;
49864f89611Schristos 	int error, nsegs, i;
49964f89611Schristos 
50064f89611Schristos 	/*
50164f89611Schristos 	 * Allocate a pool of Tx descriptors shared between all Tx queues.
50264f89611Schristos 	 */
50364f89611Schristos 	size = ATHN_NTXBUFS * AR5008_MAX_SCATTER * sizeof(struct ar_tx_desc);
50464f89611Schristos 
50564f89611Schristos 	error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
50664f89611Schristos 	    BUS_DMA_NOWAIT, &sc->sc_map);
50764f89611Schristos 	if (error != 0)
50864f89611Schristos 		goto fail;
50964f89611Schristos 
51064f89611Schristos 	error = bus_dmamem_alloc(sc->sc_dmat, size, 4, 0, &sc->sc_seg, 1,
51164f89611Schristos // XXX	    &nsegs, BUS_DMA_NOWAIT | BUS_DMA_ZERO);
51264f89611Schristos 	    &nsegs, BUS_DMA_NOWAIT);
51364f89611Schristos 	if (error != 0)
51464f89611Schristos 		goto fail;
51564f89611Schristos 
51664f89611Schristos 	error = bus_dmamem_map(sc->sc_dmat, &sc->sc_seg, 1, size,
51764f89611Schristos 	    (void **)&sc->sc_descs, BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
51864f89611Schristos 	if (error != 0)
51964f89611Schristos 		goto fail;
52064f89611Schristos 
521482fe1eaSmatt 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_map, sc->sc_descs,
522482fe1eaSmatt 	    size, NULL, BUS_DMA_NOWAIT);
52364f89611Schristos 	if (error != 0)
52464f89611Schristos 		goto fail;
52564f89611Schristos 
52664f89611Schristos 	SIMPLEQ_INIT(&sc->sc_txbufs);
52764f89611Schristos 	for (i = 0; i < ATHN_NTXBUFS; i++) {
52864f89611Schristos 		bf = &sc->sc_txpool[i];
52964f89611Schristos 
53064f89611Schristos 		error = bus_dmamap_create(sc->sc_dmat, ATHN_TXBUFSZ,
53164f89611Schristos 		    AR5008_MAX_SCATTER, ATHN_TXBUFSZ, 0, BUS_DMA_NOWAIT,
53264f89611Schristos 		    &bf->bf_map);
53364f89611Schristos 		if (error != 0) {
534dabffed6Schristos 			aprint_error_dev(sc->sc_dev,
535dabffed6Schristos 			    "could not create Tx buf DMA map\n");
53664f89611Schristos 			goto fail;
53764f89611Schristos 		}
53864f89611Schristos 
53964f89611Schristos 		bf->bf_descs =
54064f89611Schristos 		    &((struct ar_tx_desc *)sc->sc_descs)[i * AR5008_MAX_SCATTER];
54164f89611Schristos 		bf->bf_daddr = sc->sc_map->dm_segs[0].ds_addr +
54264f89611Schristos 		    i * AR5008_MAX_SCATTER * sizeof(struct ar_tx_desc);
54364f89611Schristos 
54464f89611Schristos 		SIMPLEQ_INSERT_TAIL(&sc->sc_txbufs, bf, bf_list);
54564f89611Schristos 	}
54664f89611Schristos 	return 0;
54764f89611Schristos  fail:
54864f89611Schristos 	ar5008_tx_free(sc);
54964f89611Schristos 	return error;
55064f89611Schristos }
55164f89611Schristos 
55264f89611Schristos Static void
ar5008_tx_free(struct athn_softc * sc)55364f89611Schristos ar5008_tx_free(struct athn_softc *sc)
55464f89611Schristos {
55564f89611Schristos 	struct athn_tx_buf *bf;
55664f89611Schristos 	int i;
55764f89611Schristos 
55864f89611Schristos 	for (i = 0; i < ATHN_NTXBUFS; i++) {
55964f89611Schristos 		bf = &sc->sc_txpool[i];
56064f89611Schristos 
56164f89611Schristos 		if (bf->bf_map != NULL)
56264f89611Schristos 			bus_dmamap_destroy(sc->sc_dmat, bf->bf_map);
56364f89611Schristos 	}
56464f89611Schristos 	/* Free Tx descriptors. */
56564f89611Schristos 	if (sc->sc_map != NULL) {
56664f89611Schristos 		if (sc->sc_descs != NULL) {
56764f89611Schristos 			bus_dmamap_unload(sc->sc_dmat, sc->sc_map);
56864f89611Schristos 			bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_descs,
56964f89611Schristos 			    ATHN_NTXBUFS * AR5008_MAX_SCATTER *
57064f89611Schristos 			    sizeof(struct ar_tx_desc));
57164f89611Schristos 			bus_dmamem_free(sc->sc_dmat, &sc->sc_seg, 1);
57264f89611Schristos 		}
57364f89611Schristos 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_map);
57464f89611Schristos 	}
57564f89611Schristos }
57664f89611Schristos 
57764f89611Schristos Static int
ar5008_rx_alloc(struct athn_softc * sc)57864f89611Schristos ar5008_rx_alloc(struct athn_softc *sc)
57964f89611Schristos {
58064f89611Schristos 	struct athn_rxq *rxq = &sc->sc_rxq[0];
58164f89611Schristos 	struct athn_rx_buf *bf;
58264f89611Schristos 	struct ar_rx_desc *ds;
58364f89611Schristos 	bus_size_t size;
58464f89611Schristos 	int error, nsegs, i;
58564f89611Schristos 
58664f89611Schristos 	rxq->bf = malloc(ATHN_NRXBUFS * sizeof(*bf), M_DEVBUF,
587d47bcd29Schs 	    M_WAITOK | M_ZERO);
58864f89611Schristos 
58964f89611Schristos 	size = ATHN_NRXBUFS * sizeof(struct ar_rx_desc);
59064f89611Schristos 
59164f89611Schristos 	error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
59264f89611Schristos 	    BUS_DMA_NOWAIT, &rxq->map);
59364f89611Schristos 	if (error != 0)
59464f89611Schristos 		goto fail;
59564f89611Schristos 
59664f89611Schristos 	error = bus_dmamem_alloc(sc->sc_dmat, size, 0, 0, &rxq->seg, 1,
59764f89611Schristos //	    &nsegs, BUS_DMA_NOWAIT | BUS_DMA_ZERO);
59864f89611Schristos 	    &nsegs, BUS_DMA_NOWAIT);
59964f89611Schristos 	if (error != 0)
60064f89611Schristos 		goto fail;
60164f89611Schristos 
60264f89611Schristos 	error = bus_dmamem_map(sc->sc_dmat, &rxq->seg, 1, size,
60364f89611Schristos 	    (void **)&rxq->descs, BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
60464f89611Schristos 	if (error != 0)
60564f89611Schristos 		goto fail;
60664f89611Schristos 
607d823699bSmatt 	error = bus_dmamap_load(sc->sc_dmat, rxq->map, rxq->descs,
608482fe1eaSmatt 	    size, NULL, BUS_DMA_NOWAIT);
60964f89611Schristos 	if (error != 0)
61064f89611Schristos 		goto fail;
61164f89611Schristos 
61264f89611Schristos 	for (i = 0; i < ATHN_NRXBUFS; i++) {
61364f89611Schristos 		bf = &rxq->bf[i];
61464f89611Schristos 		ds = &((struct ar_rx_desc *)rxq->descs)[i];
61564f89611Schristos 
61664f89611Schristos 		error = bus_dmamap_create(sc->sc_dmat, ATHN_RXBUFSZ, 1,
61764f89611Schristos 		    ATHN_RXBUFSZ, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
61864f89611Schristos 		    &bf->bf_map);
61964f89611Schristos 		if (error != 0) {
620dabffed6Schristos 			aprint_error_dev(sc->sc_dev,
621dabffed6Schristos 			    " could not create Rx buf DMA map\n");
62264f89611Schristos 			goto fail;
62364f89611Schristos 		}
62464f89611Schristos 		/*
62564f89611Schristos 		 * Assumes MCLGETI returns cache-line-size aligned buffers.
62664f89611Schristos 		 * XXX: does ours?
62764f89611Schristos 		 */
62864f89611Schristos 		bf->bf_m = MCLGETI(NULL, M_DONTWAIT, NULL, ATHN_RXBUFSZ);
62964f89611Schristos 		if (bf->bf_m == NULL) {
630dabffed6Schristos 			aprint_error_dev(sc->sc_dev,
631dabffed6Schristos 			    "could not allocate Rx mbuf\n");
63264f89611Schristos 			error = ENOBUFS;
63364f89611Schristos 			goto fail;
63464f89611Schristos 		}
63564f89611Schristos 
63664f89611Schristos 		error = bus_dmamap_load(sc->sc_dmat, bf->bf_map,
63764f89611Schristos 		    mtod(bf->bf_m, void *), ATHN_RXBUFSZ, NULL,
63864f89611Schristos 		    BUS_DMA_NOWAIT | BUS_DMA_READ);
63964f89611Schristos 		if (error != 0) {
640dabffed6Schristos 			aprint_error_dev(sc->sc_dev,
641dabffed6Schristos 			    "could not DMA map Rx buffer\n");
64264f89611Schristos 			goto fail;
64364f89611Schristos 		}
64464f89611Schristos 
64564f89611Schristos 		bus_dmamap_sync(sc->sc_dmat, bf->bf_map, 0, ATHN_RXBUFSZ,
64664f89611Schristos 		    BUS_DMASYNC_PREREAD);
64764f89611Schristos 
64864f89611Schristos 		bf->bf_desc = ds;
64964f89611Schristos 		bf->bf_daddr = rxq->map->dm_segs[0].ds_addr +
65064f89611Schristos 		    i * sizeof(struct ar_rx_desc);
65164f89611Schristos 	}
65264f89611Schristos 	return 0;
65364f89611Schristos  fail:
65464f89611Schristos 	ar5008_rx_free(sc);
65564f89611Schristos 	return error;
65664f89611Schristos }
65764f89611Schristos 
65864f89611Schristos Static void
ar5008_rx_free(struct athn_softc * sc)65964f89611Schristos ar5008_rx_free(struct athn_softc *sc)
66064f89611Schristos {
66164f89611Schristos 	struct athn_rxq *rxq = &sc->sc_rxq[0];
66264f89611Schristos 	struct athn_rx_buf *bf;
66364f89611Schristos 	int i;
66464f89611Schristos 
66564f89611Schristos 	if (rxq->bf == NULL)
66664f89611Schristos 		return;
66764f89611Schristos 	for (i = 0; i < ATHN_NRXBUFS; i++) {
66864f89611Schristos 		bf = &rxq->bf[i];
66964f89611Schristos 
67064f89611Schristos 		if (bf->bf_map != NULL)
67164f89611Schristos 			bus_dmamap_destroy(sc->sc_dmat, bf->bf_map);
67264f89611Schristos 		m_freem(bf->bf_m);
67364f89611Schristos 	}
67464f89611Schristos 	free(rxq->bf, M_DEVBUF);
67564f89611Schristos 
67664f89611Schristos 	/* Free Rx descriptors. */
67764f89611Schristos 	if (rxq->map != NULL) {
67864f89611Schristos 		if (rxq->descs != NULL) {
67964f89611Schristos 			bus_dmamap_unload(sc->sc_dmat, rxq->map);
68064f89611Schristos 			bus_dmamem_unmap(sc->sc_dmat, (void *)rxq->descs,
68164f89611Schristos 			    ATHN_NRXBUFS * sizeof(struct ar_rx_desc));
68264f89611Schristos 			bus_dmamem_free(sc->sc_dmat, &rxq->seg, 1);
68364f89611Schristos 		}
68464f89611Schristos 		bus_dmamap_destroy(sc->sc_dmat, rxq->map);
68564f89611Schristos 	}
68664f89611Schristos }
68764f89611Schristos 
68864f89611Schristos Static void
ar5008_rx_enable(struct athn_softc * sc)68964f89611Schristos ar5008_rx_enable(struct athn_softc *sc)
69064f89611Schristos {
69164f89611Schristos 	struct athn_rxq *rxq = &sc->sc_rxq[0];
69264f89611Schristos 	struct athn_rx_buf *bf;
69364f89611Schristos 	struct ar_rx_desc *ds;
69464f89611Schristos 	int i;
69564f89611Schristos 
69664f89611Schristos 	/* Setup and link Rx descriptors. */
69764f89611Schristos 	SIMPLEQ_INIT(&rxq->head);
69864f89611Schristos 	rxq->lastds = NULL;
69964f89611Schristos 	for (i = 0; i < ATHN_NRXBUFS; i++) {
70064f89611Schristos 		bf = &rxq->bf[i];
70164f89611Schristos 		ds = bf->bf_desc;
70264f89611Schristos 
70364f89611Schristos 		memset(ds, 0, sizeof(*ds));
70464f89611Schristos 		ds->ds_data = bf->bf_map->dm_segs[0].ds_addr;
70564f89611Schristos 		ds->ds_ctl1 = SM(AR_RXC1_BUF_LEN, ATHN_RXBUFSZ);
70664f89611Schristos 
70764f89611Schristos 		if (rxq->lastds != NULL) {
70864f89611Schristos 			((struct ar_rx_desc *)rxq->lastds)->ds_link =
70964f89611Schristos 			    bf->bf_daddr;
71064f89611Schristos 		}
71164f89611Schristos 		SIMPLEQ_INSERT_TAIL(&rxq->head, bf, bf_list);
71264f89611Schristos 		rxq->lastds = ds;
71364f89611Schristos 	}
71464f89611Schristos 	bus_dmamap_sync(sc->sc_dmat, rxq->map, 0, rxq->map->dm_mapsize,
71564f89611Schristos 	    BUS_DMASYNC_PREREAD);
71664f89611Schristos 
71764f89611Schristos 	/* Enable Rx. */
71864f89611Schristos 	AR_WRITE(sc, AR_RXDP, SIMPLEQ_FIRST(&rxq->head)->bf_daddr);
71964f89611Schristos 	AR_WRITE(sc, AR_CR, AR_CR_RXE);
72064f89611Schristos 	AR_WRITE_BARRIER(sc);
72164f89611Schristos }
72264f89611Schristos 
72364f89611Schristos Static void
ar5008_rx_radiotap(struct athn_softc * sc,struct mbuf * m,struct ar_rx_desc * ds)72464f89611Schristos ar5008_rx_radiotap(struct athn_softc *sc, struct mbuf *m,
72564f89611Schristos     struct ar_rx_desc *ds)
72664f89611Schristos {
72764f89611Schristos 	struct athn_rx_radiotap_header *tap = &sc->sc_rxtap;
72864f89611Schristos 	struct ieee80211com *ic = &sc->sc_ic;
72964f89611Schristos 	uint64_t tsf;
73064f89611Schristos 	uint32_t tstamp;
73164f89611Schristos 	uint8_t rate;
73264f89611Schristos 
73364f89611Schristos 	/* Extend the 15-bit timestamp from Rx descriptor to 64-bit TSF. */
73464f89611Schristos 	tstamp = ds->ds_status2;
73564f89611Schristos 	tsf = AR_READ(sc, AR_TSF_U32);
73664f89611Schristos 	tsf = tsf << 32 | AR_READ(sc, AR_TSF_L32);
73764f89611Schristos 	if ((tsf & 0x7fff) < tstamp)
73864f89611Schristos 		tsf -= 0x8000;
73964f89611Schristos 	tsf = (tsf & ~0x7fff) | tstamp;
74064f89611Schristos 
74164f89611Schristos 	tap->wr_flags = IEEE80211_RADIOTAP_F_FCS;
74264f89611Schristos 	tap->wr_tsft = htole64(tsf);
74364f89611Schristos 	tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
74464f89611Schristos 	tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
74564f89611Schristos 	tap->wr_dbm_antsignal = MS(ds->ds_status4, AR_RXS4_RSSI_COMBINED);
74664f89611Schristos 	/* XXX noise. */
74764f89611Schristos 	tap->wr_antenna = MS(ds->ds_status3, AR_RXS3_ANTENNA);
74864f89611Schristos 	tap->wr_rate = 0;	/* In case it can't be found below. */
74964f89611Schristos 	if (AR_SREV_5416_20_OR_LATER(sc))
75064f89611Schristos 		rate = MS(ds->ds_status0, AR_RXS0_RATE);
75164f89611Schristos 	else
75264f89611Schristos 		rate = MS(ds->ds_status3, AR_RXS3_RATE);
75364f89611Schristos 	if (rate & 0x80) {		/* HT. */
75464f89611Schristos 		/* Bit 7 set means HT MCS instead of rate. */
75564f89611Schristos 		tap->wr_rate = rate;
75664f89611Schristos 		if (!(ds->ds_status3 & AR_RXS3_GI))
75764f89611Schristos 			tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
75864f89611Schristos 
75964f89611Schristos 	}
76064f89611Schristos 	else if (rate & 0x10) {	/* CCK. */
76164f89611Schristos 		if (rate & 0x04)
76264f89611Schristos 			tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
76364f89611Schristos 		switch (rate & ~0x14) {
76464f89611Schristos 		case 0xb: tap->wr_rate =   2; break;
76564f89611Schristos 		case 0xa: tap->wr_rate =   4; break;
76664f89611Schristos 		case 0x9: tap->wr_rate =  11; break;
76764f89611Schristos 		case 0x8: tap->wr_rate =  22; break;
76864f89611Schristos 		}
76964f89611Schristos 	}
77064f89611Schristos 	else {			/* OFDM. */
77164f89611Schristos 		switch (rate) {
77264f89611Schristos 		case 0xb: tap->wr_rate =  12; break;
77364f89611Schristos 		case 0xf: tap->wr_rate =  18; break;
77464f89611Schristos 		case 0xa: tap->wr_rate =  24; break;
77564f89611Schristos 		case 0xe: tap->wr_rate =  36; break;
77664f89611Schristos 		case 0x9: tap->wr_rate =  48; break;
77764f89611Schristos 		case 0xd: tap->wr_rate =  72; break;
77864f89611Schristos 		case 0x8: tap->wr_rate =  96; break;
77964f89611Schristos 		case 0xc: tap->wr_rate = 108; break;
78064f89611Schristos 		}
78164f89611Schristos 	}
7823cd62456Smsaitoh 	bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m, BPF_D_IN);
78364f89611Schristos }
78464f89611Schristos 
78564f89611Schristos static __inline int
ar5008_rx_process(struct athn_softc * sc)78664f89611Schristos ar5008_rx_process(struct athn_softc *sc)
78764f89611Schristos {
78864f89611Schristos 	struct ieee80211com *ic = &sc->sc_ic;
78964f89611Schristos 	struct ifnet *ifp = &sc->sc_if;
79064f89611Schristos 	struct athn_rxq *rxq = &sc->sc_rxq[0];
79164f89611Schristos 	struct athn_rx_buf *bf, *nbf;
79264f89611Schristos 	struct ar_rx_desc *ds;
79364f89611Schristos 	struct ieee80211_frame *wh;
79464f89611Schristos 	struct ieee80211_node *ni;
79564f89611Schristos 	struct mbuf *m, *m1;
79664f89611Schristos 	u_int32_t rstamp;
7975ffb0503Snonaka 	int error, len, rssi, s;
79864f89611Schristos 
79964f89611Schristos 	bf = SIMPLEQ_FIRST(&rxq->head);
80064f89611Schristos 	if (__predict_false(bf == NULL)) {	/* Should not happen. */
801dabffed6Schristos 		aprint_error_dev(sc->sc_dev, "Rx queue is empty!\n");
80264f89611Schristos 		return ENOENT;
80364f89611Schristos 	}
80464f89611Schristos 	ds = bf->bf_desc;
80564f89611Schristos 
80664f89611Schristos 	if (!(ds->ds_status8 & AR_RXS8_DONE)) {
80764f89611Schristos 		/*
80864f89611Schristos 		 * On some parts, the status words can get corrupted
80964f89611Schristos 		 * (including the "done" bit), so we check the next
81064f89611Schristos 		 * descriptor "done" bit.  If it is set, it is a good
81164f89611Schristos 		 * indication that the status words are corrupted, so
81264f89611Schristos 		 * we skip this descriptor and drop the frame.
81364f89611Schristos 		 */
81464f89611Schristos 		nbf = SIMPLEQ_NEXT(bf, bf_list);
81564f89611Schristos 		if (nbf != NULL &&
81664f89611Schristos 		    (((struct ar_rx_desc *)nbf->bf_desc)->ds_status8 &
81764f89611Schristos 		     AR_RXS8_DONE)) {
81864f89611Schristos 			DPRINTFN(DBG_RX, sc,
81964f89611Schristos 			    "corrupted descriptor status=0x%x\n",
82064f89611Schristos 			    ds->ds_status8);
82164f89611Schristos 			/* HW will not "move" RXDP in this case, so do it. */
82264f89611Schristos 			AR_WRITE(sc, AR_RXDP, nbf->bf_daddr);
82364f89611Schristos 			AR_WRITE_BARRIER(sc);
8242d501ecfSthorpej 			if_statinc(ifp, if_ierrors);
82564f89611Schristos 			goto skip;
82664f89611Schristos 		}
82764f89611Schristos 		return EBUSY;
82864f89611Schristos 	}
82964f89611Schristos 
83064f89611Schristos 	if (__predict_false(ds->ds_status1 & AR_RXS1_MORE)) {
83164f89611Schristos 		/* Drop frames that span multiple Rx descriptors. */
83264f89611Schristos 		DPRINTFN(DBG_RX, sc, "dropping split frame\n");
8332d501ecfSthorpej 		if_statinc(ifp, if_ierrors);
83464f89611Schristos 		goto skip;
83564f89611Schristos 	}
83664f89611Schristos 	if (!(ds->ds_status8 & AR_RXS8_FRAME_OK)) {
83764f89611Schristos 		if (ds->ds_status8 & AR_RXS8_CRC_ERR)
83864f89611Schristos 			DPRINTFN(DBG_RX, sc, "CRC error\n");
83964f89611Schristos 		else if (ds->ds_status8 & AR_RXS8_PHY_ERR)
84064f89611Schristos 			DPRINTFN(DBG_RX, sc, "PHY error=0x%x\n",
84164f89611Schristos 			    MS(ds->ds_status8, AR_RXS8_PHY_ERR_CODE));
84264f89611Schristos 		else if (ds->ds_status8 & AR_RXS8_DECRYPT_CRC_ERR)
84364f89611Schristos 			DPRINTFN(DBG_RX, sc, "Decryption CRC error\n");
84464f89611Schristos 		else if (ds->ds_status8 & AR_RXS8_MICHAEL_ERR) {
84564f89611Schristos 			DPRINTFN(DBG_RX, sc, "Michael MIC failure\n");
84664f89611Schristos 
84764f89611Schristos 			len = MS(ds->ds_status1, AR_RXS1_DATA_LEN);
84864f89611Schristos 			m = bf->bf_m;
849d938d837Sozaki-r 			m_set_rcvif(m, ifp);
85064f89611Schristos 			m->m_pkthdr.len = m->m_len = len;
85164f89611Schristos 			wh = mtod(m, struct ieee80211_frame *);
85264f89611Schristos 
85364f89611Schristos 			/* Report Michael MIC failures to net80211. */
85464f89611Schristos 			ieee80211_notify_michael_failure(ic, wh, 0 /* XXX: keyix */);
85564f89611Schristos 		}
8562d501ecfSthorpej 		if_statinc(ifp, if_ierrors);
85764f89611Schristos 		goto skip;
85864f89611Schristos 	}
85964f89611Schristos 
86064f89611Schristos 	len = MS(ds->ds_status1, AR_RXS1_DATA_LEN);
86164f89611Schristos 	if (__predict_false(len < (int)IEEE80211_MIN_LEN || len > ATHN_RXBUFSZ)) {
86264f89611Schristos 		DPRINTFN(DBG_RX, sc, "corrupted descriptor length=%d\n", len);
8632d501ecfSthorpej 		if_statinc(ifp, if_ierrors);
86464f89611Schristos 		goto skip;
86564f89611Schristos 	}
86664f89611Schristos 
86764f89611Schristos 	/* Allocate a new Rx buffer. */
86864f89611Schristos 	m1 = MCLGETI(NULL, M_DONTWAIT, NULL, ATHN_RXBUFSZ);
86964f89611Schristos 	if (__predict_false(m1 == NULL)) {
87064f89611Schristos 		ic->ic_stats.is_rx_nobuf++;
8712d501ecfSthorpej 		if_statinc(ifp, if_ierrors);
87264f89611Schristos 		goto skip;
87364f89611Schristos 	}
87464f89611Schristos 
87564f89611Schristos 	/* Sync and unmap the old Rx buffer. */
87664f89611Schristos 	bus_dmamap_sync(sc->sc_dmat, bf->bf_map, 0, ATHN_RXBUFSZ,
87764f89611Schristos 	    BUS_DMASYNC_POSTREAD);
87864f89611Schristos 	bus_dmamap_unload(sc->sc_dmat, bf->bf_map);
87964f89611Schristos 
88064f89611Schristos 	/* Map the new Rx buffer. */
88164f89611Schristos 	error = bus_dmamap_load(sc->sc_dmat, bf->bf_map, mtod(m1, void *),
88264f89611Schristos 	    ATHN_RXBUFSZ, NULL, BUS_DMA_NOWAIT | BUS_DMA_READ);
88364f89611Schristos 	if (__predict_false(error != 0)) {
88464f89611Schristos 		m_freem(m1);
88564f89611Schristos 
88664f89611Schristos 		/* Remap the old Rx buffer or panic. */
88764f89611Schristos 		error = bus_dmamap_load(sc->sc_dmat, bf->bf_map,
88864f89611Schristos 		    mtod(bf->bf_m, void *), ATHN_RXBUFSZ, NULL,
88964f89611Schristos 		    BUS_DMA_NOWAIT | BUS_DMA_READ);
89064f89611Schristos 		KASSERT(error != 0);
8912d501ecfSthorpej 		if_statinc(ifp, if_ierrors);
89264f89611Schristos 		goto skip;
89364f89611Schristos 	}
89464f89611Schristos 
89564f89611Schristos 	bus_dmamap_sync(sc->sc_dmat, bf->bf_map, 0, ATHN_RXBUFSZ,
89664f89611Schristos 	    BUS_DMASYNC_PREREAD);
89764f89611Schristos 
89864f89611Schristos 	/* Write physical address of new Rx buffer. */
89964f89611Schristos 	ds->ds_data = bf->bf_map->dm_segs[0].ds_addr;
90064f89611Schristos 
90164f89611Schristos 	m = bf->bf_m;
90264f89611Schristos 	bf->bf_m = m1;
90364f89611Schristos 
90464f89611Schristos 	/* Finalize mbuf. */
905d938d837Sozaki-r 	m_set_rcvif(m, ifp);
90664f89611Schristos 	m->m_pkthdr.len = m->m_len = len;
90764f89611Schristos 
9085ffb0503Snonaka 	s = splnet();
9095ffb0503Snonaka 
91064f89611Schristos 	/* Grab a reference to the source node. */
91164f89611Schristos 	wh = mtod(m, struct ieee80211_frame *);
91264f89611Schristos 	ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
91364f89611Schristos 
91464f89611Schristos 	/* Remove any HW padding after the 802.11 header. */
91564f89611Schristos 	if (!(wh->i_fc[0] & IEEE80211_FC0_TYPE_CTL)) {
91664f89611Schristos 		u_int hdrlen = ieee80211_anyhdrsize(wh);
91764f89611Schristos 		if (hdrlen & 3) {
918ef294642Smaxv 			memmove((uint8_t *)wh + 2, wh, hdrlen);
91964f89611Schristos 			m_adj(m, 2);
92064f89611Schristos 		}
92164f89611Schristos 	}
92264f89611Schristos 	if (__predict_false(sc->sc_drvbpf != NULL))
92364f89611Schristos 		ar5008_rx_radiotap(sc, m, ds);
92464f89611Schristos 
92564f89611Schristos 	/* Trim 802.11 FCS after radiotap. */
92664f89611Schristos 	m_adj(m, -IEEE80211_CRC_LEN);
92764f89611Schristos 
92864f89611Schristos 	/* Send the frame to the 802.11 layer. */
92964f89611Schristos 	rssi = MS(ds->ds_status4, AR_RXS4_RSSI_COMBINED);
93064f89611Schristos 	rstamp = ds->ds_status2;
93164f89611Schristos 	ieee80211_input(ic, m, ni, rssi, rstamp);
93264f89611Schristos 
93364f89611Schristos 	/* Node is no longer needed. */
93464f89611Schristos 	ieee80211_free_node(ni);
93564f89611Schristos 
9365ffb0503Snonaka 	splx(s);
9375ffb0503Snonaka 
93864f89611Schristos  skip:
93964f89611Schristos 	/* Unlink this descriptor from head. */
94064f89611Schristos 	SIMPLEQ_REMOVE_HEAD(&rxq->head, bf_list);
94164f89611Schristos 	memset(&ds->ds_status0, 0, 36);	/* XXX Really needed? */
94264f89611Schristos 	ds->ds_status8 &= ~AR_RXS8_DONE;
94364f89611Schristos 	ds->ds_link = 0;
94464f89611Schristos 
94564f89611Schristos 	/* Re-use this descriptor and link it to tail. */
94664f89611Schristos 	if (__predict_true(!SIMPLEQ_EMPTY(&rxq->head)))
94764f89611Schristos 		((struct ar_rx_desc *)rxq->lastds)->ds_link = bf->bf_daddr;
94864f89611Schristos 	else
94964f89611Schristos 		AR_WRITE(sc, AR_RXDP, bf->bf_daddr);
95064f89611Schristos 	SIMPLEQ_INSERT_TAIL(&rxq->head, bf, bf_list);
95164f89611Schristos 	rxq->lastds = ds;
95264f89611Schristos 
95364f89611Schristos 	/* Re-enable Rx. */
95464f89611Schristos 	AR_WRITE(sc, AR_CR, AR_CR_RXE);
95564f89611Schristos 	AR_WRITE_BARRIER(sc);
95664f89611Schristos 	return 0;
95764f89611Schristos }
95864f89611Schristos 
95964f89611Schristos Static void
ar5008_rx_intr(struct athn_softc * sc)96064f89611Schristos ar5008_rx_intr(struct athn_softc *sc)
96164f89611Schristos {
96264f89611Schristos 
96364f89611Schristos 	while (ar5008_rx_process(sc) == 0)
96464f89611Schristos 		continue;
96564f89611Schristos }
96664f89611Schristos 
96764f89611Schristos Static int
ar5008_tx_process(struct athn_softc * sc,int qid)96864f89611Schristos ar5008_tx_process(struct athn_softc *sc, int qid)
96964f89611Schristos {
97064f89611Schristos 	struct ifnet *ifp = &sc->sc_if;
97164f89611Schristos 	struct athn_txq *txq = &sc->sc_txq[qid];
97264f89611Schristos 	struct athn_node *an;
97364f89611Schristos 	struct athn_tx_buf *bf;
97464f89611Schristos 	struct ar_tx_desc *ds;
97564f89611Schristos 	uint8_t failcnt;
97664f89611Schristos 
97764f89611Schristos 	bf = SIMPLEQ_FIRST(&txq->head);
97864f89611Schristos 	if (bf == NULL)
97964f89611Schristos 		return ENOENT;
98064f89611Schristos 	/* Get descriptor of last DMA segment. */
98164f89611Schristos 	ds = &((struct ar_tx_desc *)bf->bf_descs)[bf->bf_map->dm_nsegs - 1];
98264f89611Schristos 
98364f89611Schristos 	if (!(ds->ds_status9 & AR_TXS9_DONE))
98464f89611Schristos 		return EBUSY;
98564f89611Schristos 
98664f89611Schristos 	SIMPLEQ_REMOVE_HEAD(&txq->head, bf_list);
9872d501ecfSthorpej 	if_statinc(ifp, if_opackets);
98864f89611Schristos 
98964f89611Schristos 	sc->sc_tx_timer = 0;
99064f89611Schristos 
99164f89611Schristos 	if (ds->ds_status1 & AR_TXS1_EXCESSIVE_RETRIES)
9922d501ecfSthorpej 		if_statinc(ifp, if_oerrors);
99364f89611Schristos 
99464f89611Schristos 	if (ds->ds_status1 & AR_TXS1_UNDERRUN)
99564f89611Schristos 		athn_inc_tx_trigger_level(sc);
99664f89611Schristos 
99764f89611Schristos 	an = (struct athn_node *)bf->bf_ni;
99864f89611Schristos 	/*
99964f89611Schristos 	 * NB: the data fail count contains the number of un-acked tries
100064f89611Schristos 	 * for the final series used.  We must add the number of tries for
100164f89611Schristos 	 * each series that was fully processed.
100264f89611Schristos 	 */
100364f89611Schristos 	failcnt  = MS(ds->ds_status1, AR_TXS1_DATA_FAIL_CNT);
100464f89611Schristos 	/* NB: Assume two tries per series. */
100564f89611Schristos 	failcnt += MS(ds->ds_status9, AR_TXS9_FINAL_IDX) * 2;
100664f89611Schristos 
100764f89611Schristos 	/* Update rate control statistics. */
100864f89611Schristos 	an->amn.amn_txcnt++;
100964f89611Schristos 	if (failcnt > 0)
101064f89611Schristos 		an->amn.amn_retrycnt++;
101164f89611Schristos 
101264f89611Schristos 	DPRINTFN(DBG_TX, sc, "Tx done qid=%d status1=%d fail count=%d\n",
101364f89611Schristos 	    qid, ds->ds_status1, failcnt);
101464f89611Schristos 
101564f89611Schristos 	bus_dmamap_sync(sc->sc_dmat, bf->bf_map, 0, bf->bf_map->dm_mapsize,
101664f89611Schristos 	    BUS_DMASYNC_POSTWRITE);
101764f89611Schristos 	bus_dmamap_unload(sc->sc_dmat, bf->bf_map);
101864f89611Schristos 
101964f89611Schristos 	m_freem(bf->bf_m);
102064f89611Schristos 	bf->bf_m = NULL;
102164f89611Schristos 	ieee80211_free_node(bf->bf_ni);
102264f89611Schristos 	bf->bf_ni = NULL;
102364f89611Schristos 
102464f89611Schristos 	/* Link Tx buffer back to global free list. */
102564f89611Schristos 	SIMPLEQ_INSERT_TAIL(&sc->sc_txbufs, bf, bf_list);
102664f89611Schristos 	return 0;
102764f89611Schristos }
102864f89611Schristos 
102964f89611Schristos Static void
ar5008_tx_intr(struct athn_softc * sc)103064f89611Schristos ar5008_tx_intr(struct athn_softc *sc)
103164f89611Schristos {
103264f89611Schristos 	struct ifnet *ifp = &sc->sc_if;
103364f89611Schristos 	uint16_t mask = 0;
103464f89611Schristos 	uint32_t reg;
10355ffb0503Snonaka 	int qid, s;
10365ffb0503Snonaka 
10375ffb0503Snonaka 	s = splnet();
103864f89611Schristos 
103964f89611Schristos 	reg = AR_READ(sc, AR_ISR_S0_S);
104064f89611Schristos 	mask |= MS(reg, AR_ISR_S0_QCU_TXOK);
104164f89611Schristos 	mask |= MS(reg, AR_ISR_S0_QCU_TXDESC);
104264f89611Schristos 
104364f89611Schristos 	reg = AR_READ(sc, AR_ISR_S1_S);
104464f89611Schristos 	mask |= MS(reg, AR_ISR_S1_QCU_TXERR);
104564f89611Schristos 	mask |= MS(reg, AR_ISR_S1_QCU_TXEOL);
104664f89611Schristos 
104764f89611Schristos 	DPRINTFN(DBG_TX, sc, "Tx interrupt mask=0x%x\n", mask);
104864f89611Schristos 	for (qid = 0; mask != 0; mask >>= 1, qid++) {
104964f89611Schristos 		if (mask & 1)
105064f89611Schristos 			while (ar5008_tx_process(sc, qid) == 0);
105164f89611Schristos 	}
105264f89611Schristos 	if (!SIMPLEQ_EMPTY(&sc->sc_txbufs)) {
105364f89611Schristos 		ifp->if_flags &= ~IFF_OACTIVE;
1054a1d5203dSozaki-r 		ifp->if_start(ifp); /* in softint */
105564f89611Schristos 	}
10565ffb0503Snonaka 
10575ffb0503Snonaka 	splx(s);
105864f89611Schristos }
105964f89611Schristos 
106064f89611Schristos #ifndef IEEE80211_STA_ONLY
106164f89611Schristos /*
106264f89611Schristos  * Process Software Beacon Alert interrupts.
106364f89611Schristos  */
106464f89611Schristos Static int
ar5008_swba_intr(struct athn_softc * sc)106564f89611Schristos ar5008_swba_intr(struct athn_softc *sc)
106664f89611Schristos {
106764f89611Schristos 	struct ieee80211com *ic = &sc->sc_ic;
106864f89611Schristos 	struct ifnet *ifp = &sc->sc_if;
106964f89611Schristos 	struct ieee80211_node *ni = ic->ic_bss;
107064f89611Schristos 	struct athn_tx_buf *bf = sc->sc_bcnbuf;
107164f89611Schristos 	struct ieee80211_frame *wh;
107264f89611Schristos 	struct ieee80211_beacon_offsets bo;
107364f89611Schristos 	struct ar_tx_desc *ds;
107464f89611Schristos 	struct mbuf *m;
107564f89611Schristos 	uint8_t ridx, hwrate;
107664f89611Schristos 	int error, totlen;
107764f89611Schristos 
107864f89611Schristos #if notyet
107964f89611Schristos 	if (ic->ic_tim_mcast_pending &&
108064f89611Schristos 	    IF_IS_EMPTY(&ni->ni_savedq) &&
108164f89611Schristos 	    SIMPLEQ_EMPTY(&sc->sc_txq[ATHN_QID_CAB].head))
108264f89611Schristos 		ic->ic_tim_mcast_pending = 0;
108364f89611Schristos #endif
108464f89611Schristos 	if (ic->ic_dtim_count == 0)
108564f89611Schristos 		ic->ic_dtim_count = ic->ic_dtim_period - 1;
108664f89611Schristos 	else
108764f89611Schristos 		ic->ic_dtim_count--;
108864f89611Schristos 
108964f89611Schristos 	/* Make sure previous beacon has been sent. */
109064f89611Schristos 	if (athn_tx_pending(sc, ATHN_QID_BEACON)) {
109164f89611Schristos 		DPRINTFN(DBG_INTR, sc, "beacon stuck\n");
109264f89611Schristos 		return EBUSY;
109364f89611Schristos 	}
109464f89611Schristos 	/* Get new beacon. */
109564f89611Schristos 	m = ieee80211_beacon_alloc(ic, ic->ic_bss, &bo);
109664f89611Schristos 	if (__predict_false(m == NULL))
109764f89611Schristos 		return ENOBUFS;
109864f89611Schristos 	/* Assign sequence number. */
109964f89611Schristos 	/* XXX: use non-QoS tid? */
110064f89611Schristos 	wh = mtod(m, struct ieee80211_frame *);
110164f89611Schristos 	*(uint16_t *)&wh->i_seq[0] =
110264f89611Schristos 	    htole16(ic->ic_bss->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT);
110364f89611Schristos 	ic->ic_bss->ni_txseqs[0]++;
110464f89611Schristos 
110564f89611Schristos 	/* Unmap and free old beacon if any. */
110664f89611Schristos 	if (__predict_true(bf->bf_m != NULL)) {
110764f89611Schristos 		bus_dmamap_sync(sc->sc_dmat, bf->bf_map, 0,
110864f89611Schristos 		    bf->bf_map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
110964f89611Schristos 		bus_dmamap_unload(sc->sc_dmat, bf->bf_map);
111064f89611Schristos 		m_freem(bf->bf_m);
111164f89611Schristos 		bf->bf_m = NULL;
111264f89611Schristos 	}
111364f89611Schristos 	/* DMA map new beacon. */
111464f89611Schristos 	error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_map, m,
111564f89611Schristos 	    BUS_DMA_NOWAIT | BUS_DMA_WRITE);
111664f89611Schristos 	if (__predict_false(error != 0)) {
111764f89611Schristos 		m_freem(m);
111864f89611Schristos 		return error;
111964f89611Schristos 	}
112064f89611Schristos 	bf->bf_m = m;
112164f89611Schristos 
112264f89611Schristos 	/* Setup Tx descriptor (simplified ar5008_tx()). */
112364f89611Schristos 	ds = bf->bf_descs;
112464f89611Schristos 	memset(ds, 0, sizeof(*ds));
112564f89611Schristos 
112664f89611Schristos 	totlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
112764f89611Schristos 	ds->ds_ctl0 = SM(AR_TXC0_FRAME_LEN, totlen);
112864f89611Schristos 	ds->ds_ctl0 |= SM(AR_TXC0_XMIT_POWER, AR_MAX_RATE_POWER);
112964f89611Schristos 	ds->ds_ctl1 = SM(AR_TXC1_FRAME_TYPE, AR_FRAME_TYPE_BEACON);
113064f89611Schristos 	ds->ds_ctl1 |= AR_TXC1_NO_ACK;
113164f89611Schristos 	ds->ds_ctl6 = SM(AR_TXC6_ENCR_TYPE, AR_ENCR_TYPE_CLEAR);
113264f89611Schristos 
113364f89611Schristos 	/* Write number of tries. */
113464f89611Schristos 	ds->ds_ctl2 = SM(AR_TXC2_XMIT_DATA_TRIES0, 1);
113564f89611Schristos 
113664f89611Schristos 	/* Write Tx rate. */
113764f89611Schristos 	ridx = (ic->ic_curmode == IEEE80211_MODE_11A) ?
113864f89611Schristos 	    ATHN_RIDX_OFDM6 : ATHN_RIDX_CCK1;
113964f89611Schristos 	hwrate = athn_rates[ridx].hwrate;
114064f89611Schristos 	ds->ds_ctl3 = SM(AR_TXC3_XMIT_RATE0, hwrate);
114164f89611Schristos 
114264f89611Schristos 	/* Write Tx chains. */
114364f89611Schristos 	ds->ds_ctl7 = SM(AR_TXC7_CHAIN_SEL0, sc->sc_txchainmask);
114464f89611Schristos 
114564f89611Schristos 	ds->ds_data = bf->bf_map->dm_segs[0].ds_addr;
114664f89611Schristos 	/* Segment length must be a multiple of 4. */
114764f89611Schristos 	ds->ds_ctl1 |= SM(AR_TXC1_BUF_LEN,
114864f89611Schristos 	    (bf->bf_map->dm_segs[0].ds_len + 3) & ~3);
114964f89611Schristos 
115064f89611Schristos 	bus_dmamap_sync(sc->sc_dmat, bf->bf_map, 0, bf->bf_map->dm_mapsize,
115164f89611Schristos 	    BUS_DMASYNC_PREWRITE);
115264f89611Schristos 
115364f89611Schristos 	/* Stop Tx DMA before putting the new beacon on the queue. */
115464f89611Schristos 	athn_stop_tx_dma(sc, ATHN_QID_BEACON);
115564f89611Schristos 
115664f89611Schristos 	AR_WRITE(sc, AR_QTXDP(ATHN_QID_BEACON), bf->bf_daddr);
115764f89611Schristos 
115864f89611Schristos 	for(;;) {
115964f89611Schristos 		if (SIMPLEQ_EMPTY(&sc->sc_txbufs))
116064f89611Schristos 			break;
116164f89611Schristos 
116264f89611Schristos 		IF_DEQUEUE(&ni->ni_savedq, m);
116364f89611Schristos 		if (m == NULL)
116464f89611Schristos 			break;
116564f89611Schristos 		if (!IF_IS_EMPTY(&ni->ni_savedq)) {
116664f89611Schristos 			/* more queued frames, set the more data bit */
116764f89611Schristos 			wh = mtod(m, struct ieee80211_frame *);
116864f89611Schristos 			wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
116964f89611Schristos 		}
117064f89611Schristos 
117164f89611Schristos 		if (sc->sc_ops.tx(sc, m, ni, ATHN_TXFLAG_CAB) != 0) {
117264f89611Schristos 			ieee80211_free_node(ni);
11732d501ecfSthorpej 			if_statinc(ifp, if_oerrors);
117464f89611Schristos 			break;
117564f89611Schristos 		}
117664f89611Schristos 	}
117764f89611Schristos 
117864f89611Schristos 	/* Kick Tx. */
117964f89611Schristos 	AR_WRITE(sc, AR_Q_TXE, 1 << ATHN_QID_BEACON);
118064f89611Schristos 	AR_WRITE_BARRIER(sc);
118164f89611Schristos 	return 0;
118264f89611Schristos }
118364f89611Schristos #endif
118464f89611Schristos 
11855ffb0503Snonaka static int
ar5008_get_intr_status(struct athn_softc * sc,uint32_t * intrp,uint32_t * syncp)11865ffb0503Snonaka ar5008_get_intr_status(struct athn_softc *sc, uint32_t *intrp, uint32_t *syncp)
118764f89611Schristos {
11885ffb0503Snonaka 	uint32_t intr, sync;
118964f89611Schristos 
119064f89611Schristos 	/* Get pending interrupts. */
119164f89611Schristos 	intr = AR_READ(sc, AR_INTR_ASYNC_CAUSE);
119264f89611Schristos 	if (!(intr & AR_INTR_MAC_IRQ) || intr == AR_INTR_SPURIOUS) {
119364f89611Schristos 		intr = AR_READ(sc, AR_INTR_SYNC_CAUSE);
119464f89611Schristos 		if (intr == AR_INTR_SPURIOUS || (intr & sc->sc_isync) == 0)
119564f89611Schristos 			return 0;	/* Not for us. */
119664f89611Schristos 	}
119764f89611Schristos 
119864f89611Schristos 	if ((AR_READ(sc, AR_INTR_ASYNC_CAUSE) & AR_INTR_MAC_IRQ) &&
119964f89611Schristos 	    (AR_READ(sc, AR_RTC_STATUS) & AR_RTC_STATUS_M) == AR_RTC_STATUS_ON)
120064f89611Schristos 		intr = AR_READ(sc, AR_ISR);
120164f89611Schristos 	else
120264f89611Schristos 		intr = 0;
120364f89611Schristos 	sync = AR_READ(sc, AR_INTR_SYNC_CAUSE) & sc->sc_isync;
120464f89611Schristos 	if (intr == 0 && sync == 0)
120564f89611Schristos 		return 0;	/* Not for us. */
120664f89611Schristos 
12075ffb0503Snonaka 	*intrp = intr;
12085ffb0503Snonaka 	*syncp = sync;
12095ffb0503Snonaka 	return 1;
12105ffb0503Snonaka }
12115ffb0503Snonaka 
12125ffb0503Snonaka 
12135ffb0503Snonaka Static int
ar5008_intr_status(struct athn_softc * sc)12145ffb0503Snonaka ar5008_intr_status(struct athn_softc *sc)
12155ffb0503Snonaka {
12165ffb0503Snonaka 	uint32_t intr, sync;
12175ffb0503Snonaka 
12185ffb0503Snonaka 	return ar5008_get_intr_status(sc, &intr, &sync);
12195ffb0503Snonaka }
12205ffb0503Snonaka 
12215ffb0503Snonaka Static int
ar5008_intr(struct athn_softc * sc)12225ffb0503Snonaka ar5008_intr(struct athn_softc *sc)
12235ffb0503Snonaka {
12245ffb0503Snonaka 	uint32_t intr, intr5, sync;
12255ffb0503Snonaka #ifndef IEEE80211_STA_ONLY
12265ffb0503Snonaka 	int s;
12275ffb0503Snonaka #endif
12285ffb0503Snonaka 
12295ffb0503Snonaka 	if (!ar5008_get_intr_status(sc, &intr, &sync))
12305ffb0503Snonaka 		return 0;
12315ffb0503Snonaka 
123264f89611Schristos 	if (intr != 0) {
123364f89611Schristos 		if (intr & AR_ISR_BCNMISC) {
1234b787afbfSchristos 			uint32_t intr2 = AR_READ(sc, AR_ISR_S2);
123564f89611Schristos #if notyet
123664f89611Schristos 			if (intr2 & AR_ISR_S2_TIM)
123764f89611Schristos 				/* TBD */;
123864f89611Schristos 			if (intr2 & AR_ISR_S2_TSFOOR)
123964f89611Schristos 				/* TBD */;
1240b787afbfSchristos #else
1241b787afbfSchristos 			__USE(intr2);
124264f89611Schristos #endif
124364f89611Schristos 		}
124464f89611Schristos 		intr = AR_READ(sc, AR_ISR_RAC);
124564f89611Schristos 		if (intr == AR_INTR_SPURIOUS)
124664f89611Schristos 			return 1;
124764f89611Schristos 
124864f89611Schristos #ifndef IEEE80211_STA_ONLY
12495ffb0503Snonaka 		if (intr & AR_ISR_SWBA) {
12505ffb0503Snonaka 			s = splnet();
125164f89611Schristos 			ar5008_swba_intr(sc);
12525ffb0503Snonaka 			splx(s);
12535ffb0503Snonaka 		}
125464f89611Schristos #endif
125564f89611Schristos 		if (intr & (AR_ISR_RXMINTR | AR_ISR_RXINTM))
125664f89611Schristos 			ar5008_rx_intr(sc);
125764f89611Schristos 		if (intr & (AR_ISR_RXOK | AR_ISR_RXERR | AR_ISR_RXORN))
125864f89611Schristos 			ar5008_rx_intr(sc);
125964f89611Schristos 
126064f89611Schristos 		if (intr & (AR_ISR_TXOK | AR_ISR_TXDESC |
126164f89611Schristos 		    AR_ISR_TXERR | AR_ISR_TXEOL))
126264f89611Schristos 			ar5008_tx_intr(sc);
126364f89611Schristos 
126464f89611Schristos 		intr5 = AR_READ(sc, AR_ISR_S5_S);
126564f89611Schristos 		if (intr & AR_ISR_GENTMR) {
126664f89611Schristos 			if (intr5 & AR_ISR_GENTMR) {
126764f89611Schristos 				DPRINTFN(DBG_INTR, sc,
126864f89611Schristos 				    "GENTMR trigger=%d thresh=%d\n",
126964f89611Schristos 				    MS(intr5, AR_ISR_S5_GENTIMER_TRIG),
127064f89611Schristos 				    MS(intr5, AR_ISR_S5_GENTIMER_THRESH));
127164f89611Schristos 			}
127264f89611Schristos 		}
127364f89611Schristos #if notyet
127464f89611Schristos 		if (intr5 & AR_ISR_S5_TIM_TIMER) {
127564f89611Schristos 			/* TBD */;
127664f89611Schristos 		}
127764f89611Schristos #endif
127864f89611Schristos 	}
127964f89611Schristos 	if (sync != 0) {
128064f89611Schristos #if notyet
128164f89611Schristos 		if (sync &
128264f89611Schristos 		    (AR_INTR_SYNC_HOST1_FATAL | AR_INTR_SYNC_HOST1_PERR)) {
128364f89611Schristos 			/* TBD */;
128464f89611Schristos 		}
128564f89611Schristos #endif
128664f89611Schristos 		if (sync & AR_INTR_SYNC_RADM_CPL_TIMEOUT) {
128764f89611Schristos 			AR_WRITE(sc, AR_RC, AR_RC_HOSTIF);
128864f89611Schristos 			AR_WRITE(sc, AR_RC, 0);
128964f89611Schristos 		}
129064f89611Schristos 
129164f89611Schristos 		if ((sc->sc_flags & ATHN_FLAG_RFSILENT) &&
129264f89611Schristos 		    (sync & AR_INTR_SYNC_GPIO_PIN(sc->sc_rfsilent_pin))) {
12930c8d7957Sjmcneill 			AR_WRITE(sc, AR_INTR_SYNC_ENABLE, 0);
12940c8d7957Sjmcneill 			(void)AR_READ(sc, AR_INTR_SYNC_ENABLE);
12950c2c845fSmartin 			pmf_event_inject(sc->sc_dev, PMFE_RADIO_OFF);
129664f89611Schristos 		}
129764f89611Schristos 
129864f89611Schristos 		AR_WRITE(sc, AR_INTR_SYNC_CAUSE, sync);
129964f89611Schristos 		(void)AR_READ(sc, AR_INTR_SYNC_CAUSE);
130064f89611Schristos 	}
130164f89611Schristos 	return 1;
130264f89611Schristos }
130364f89611Schristos 
130464f89611Schristos Static int
ar5008_tx(struct athn_softc * sc,struct mbuf * m,struct ieee80211_node * ni,int txflags)130564f89611Schristos ar5008_tx(struct athn_softc *sc, struct mbuf *m, struct ieee80211_node *ni,
130664f89611Schristos     int txflags)
130764f89611Schristos {
130864f89611Schristos 	struct ieee80211com *ic = &sc->sc_ic;
130964f89611Schristos 	struct ieee80211_key *k = NULL;
131064f89611Schristos 	struct ieee80211_frame *wh;
131164f89611Schristos 	struct athn_series series[4];
131264f89611Schristos 	struct ar_tx_desc *ds, *lastds;
131364f89611Schristos 	struct athn_txq *txq;
131464f89611Schristos 	struct athn_tx_buf *bf;
131564f89611Schristos 	struct athn_node *an = (void *)ni;
131664f89611Schristos 	struct mbuf *m1;
131764f89611Schristos 	uint16_t qos;
131864f89611Schristos 	uint8_t txpower, type, encrtype, ridx[4];
131964f89611Schristos 	int i, error, totlen, hasqos, qid;
132064f89611Schristos 
132164f89611Schristos 	/* Grab a Tx buffer from our global free list. */
132264f89611Schristos 	bf = SIMPLEQ_FIRST(&sc->sc_txbufs);
132364f89611Schristos 	KASSERT(bf != NULL);
132464f89611Schristos 
132564f89611Schristos 	/* Map 802.11 frame type to hardware frame type. */
132664f89611Schristos 	wh = mtod(m, struct ieee80211_frame *);
132764f89611Schristos 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
132864f89611Schristos 	    IEEE80211_FC0_TYPE_MGT) {
132964f89611Schristos 		/* NB: Beacons do not use ar5008_tx(). */
133064f89611Schristos 		if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
133164f89611Schristos 		    IEEE80211_FC0_SUBTYPE_PROBE_RESP)
133264f89611Schristos 			type = AR_FRAME_TYPE_PROBE_RESP;
133364f89611Schristos 		else if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
133464f89611Schristos 		    IEEE80211_FC0_SUBTYPE_ATIM)
133564f89611Schristos 			type = AR_FRAME_TYPE_ATIM;
133664f89611Schristos 		else
133764f89611Schristos 			type = AR_FRAME_TYPE_NORMAL;
133864f89611Schristos 	}
133964f89611Schristos 	else if ((wh->i_fc[0] &
134064f89611Schristos 	    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
134164f89611Schristos 	    (IEEE80211_FC0_TYPE_CTL  | IEEE80211_FC0_SUBTYPE_PS_POLL)) {
134264f89611Schristos 		type = AR_FRAME_TYPE_PSPOLL;
134364f89611Schristos 	}
134464f89611Schristos 	else
134564f89611Schristos 		type = AR_FRAME_TYPE_NORMAL;
134664f89611Schristos 
134764f89611Schristos 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
134864f89611Schristos 		k = ieee80211_crypto_encap(ic, ni, m);
134964f89611Schristos 		if (k == NULL)
135064f89611Schristos 			return ENOBUFS;
135164f89611Schristos 
135264f89611Schristos 		/* packet header may have moved, reset our local pointer */
135364f89611Schristos 		wh = mtod(m, struct ieee80211_frame *);
135464f89611Schristos 	}
135564f89611Schristos 
135664f89611Schristos 	/* XXX 2-byte padding for QoS and 4-addr headers. */
135764f89611Schristos 
135864f89611Schristos 	/* Select the HW Tx queue to use for this frame. */
135964f89611Schristos 	if ((hasqos = ieee80211_has_qos(wh))) {
136064f89611Schristos #ifdef notyet_edca
136164f89611Schristos 		uint8_t tid;
136264f89611Schristos 
136364f89611Schristos 		qos = ieee80211_get_qos(wh);
136464f89611Schristos 		tid = qos & IEEE80211_QOS_TID;
136564f89611Schristos 		qid = athn_ac2qid[ieee80211_up_to_ac(ic, tid)];
136664f89611Schristos #else
136764f89611Schristos 		qos = ieee80211_get_qos(wh);
136864f89611Schristos 		qid = ATHN_QID_AC_BE;
136964f89611Schristos #endif /* notyet_edca */
137064f89611Schristos 	}
137164f89611Schristos 	else if (type == AR_FRAME_TYPE_PSPOLL) {
137264f89611Schristos 		qos = 0;
137364f89611Schristos 		qid = ATHN_QID_PSPOLL;
137464f89611Schristos 	}
137564f89611Schristos 	else if (txflags & ATHN_TXFLAG_CAB) {
137664f89611Schristos 		qos = 0;
137764f89611Schristos 		qid = ATHN_QID_CAB;
137864f89611Schristos 	}
137964f89611Schristos 	else {
138064f89611Schristos 		qos = 0;
138164f89611Schristos 		qid = ATHN_QID_AC_BE;
138264f89611Schristos 	}
138364f89611Schristos 	txq = &sc->sc_txq[qid];
138464f89611Schristos 
138564f89611Schristos 	/* Select the transmit rates to use for this frame. */
138664f89611Schristos 	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
138764f89611Schristos 	    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) !=
138864f89611Schristos 	    IEEE80211_FC0_TYPE_DATA) {
138964f89611Schristos 		/* Use lowest rate for all tries. */
139064f89611Schristos 		ridx[0] = ridx[1] = ridx[2] = ridx[3] =
139164f89611Schristos 		    (ic->ic_curmode == IEEE80211_MODE_11A) ?
139264f89611Schristos 			ATHN_RIDX_OFDM6 : ATHN_RIDX_CCK1;
139364f89611Schristos 	}
139464f89611Schristos 	else if (ic->ic_fixed_rate != -1) {
139564f89611Schristos 		/* Use same fixed rate for all tries. */
139664f89611Schristos 		ridx[0] = ridx[1] = ridx[2] = ridx[3] =
139764f89611Schristos 		    sc->sc_fixed_ridx;
139864f89611Schristos 	}
139964f89611Schristos 	else {
140064f89611Schristos 		int txrate = ni->ni_txrate;
140164f89611Schristos 		/* Use fallback table of the node. */
140264f89611Schristos 		for (i = 0; i < 4; i++) {
140364f89611Schristos 			ridx[i] = an->ridx[txrate];
140464f89611Schristos 			txrate = an->fallback[txrate];
140564f89611Schristos 		}
140664f89611Schristos 	}
140764f89611Schristos 
140864f89611Schristos 	if (__predict_false(sc->sc_drvbpf != NULL)) {
140964f89611Schristos 		struct athn_tx_radiotap_header *tap = &sc->sc_txtap;
141064f89611Schristos 
141164f89611Schristos 		tap->wt_flags = 0;
141264f89611Schristos 		/* Use initial transmit rate. */
141364f89611Schristos 		tap->wt_rate = athn_rates[ridx[0]].rate;
141464f89611Schristos 		tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
141564f89611Schristos 		tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
141664f89611Schristos // XXX		tap->wt_hwqueue = qid;
141764f89611Schristos 		if (ridx[0] != ATHN_RIDX_CCK1 &&
141864f89611Schristos 		    (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
141964f89611Schristos 			tap->wt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
142064f89611Schristos 
14213cd62456Smsaitoh 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m, BPF_D_OUT);
142264f89611Schristos 	}
142364f89611Schristos 
142464f89611Schristos 	/* DMA map mbuf. */
142564f89611Schristos 	error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_map, m,
142664f89611Schristos 	    BUS_DMA_NOWAIT | BUS_DMA_WRITE);
142764f89611Schristos 	if (__predict_false(error != 0)) {
142864f89611Schristos 		if (error != EFBIG) {
1429dabffed6Schristos 			aprint_error_dev(sc->sc_dev,
1430dabffed6Schristos 			    "can't map mbuf (error %d)\n", error);
143164f89611Schristos 			m_freem(m);
143264f89611Schristos 			return error;
143364f89611Schristos 		}
143464f89611Schristos 		/*
143564f89611Schristos 		 * DMA mapping requires too many DMA segments; linearize
143664f89611Schristos 		 * mbuf in kernel virtual address space and retry.
143764f89611Schristos 		 */
143864f89611Schristos 		MGETHDR(m1, M_DONTWAIT, MT_DATA);
143964f89611Schristos 		if (m1 == NULL) {
144064f89611Schristos 			m_freem(m);
144164f89611Schristos 			return ENOBUFS;
144264f89611Schristos 		}
144364f89611Schristos 		if (m->m_pkthdr.len > (int)MHLEN) {
144464f89611Schristos 			MCLGET(m1, M_DONTWAIT);
144564f89611Schristos 			if (!(m1->m_flags & M_EXT)) {
144664f89611Schristos 				m_freem(m);
144764f89611Schristos 				m_freem(m1);
144864f89611Schristos 				return ENOBUFS;
144964f89611Schristos 			}
145064f89611Schristos 		}
145164f89611Schristos 		m_copydata(m, 0, m->m_pkthdr.len, mtod(m1, void *));
145264f89611Schristos 		m1->m_pkthdr.len = m1->m_len = m->m_pkthdr.len;
145364f89611Schristos 		m_freem(m);
145464f89611Schristos 		m = m1;
145564f89611Schristos 
145664f89611Schristos 		error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_map, m,
145764f89611Schristos 		    BUS_DMA_NOWAIT | BUS_DMA_WRITE);
145864f89611Schristos 		if (error != 0) {
1459dabffed6Schristos 			aprint_error_dev(sc->sc_dev,
1460dabffed6Schristos 			    "can't map mbuf (error %d)\n", error);
146164f89611Schristos 			m_freem(m);
146264f89611Schristos 			return error;
146364f89611Schristos 		}
146464f89611Schristos 	}
146564f89611Schristos 	bf->bf_m = m;
146664f89611Schristos 	bf->bf_ni = ni;
146764f89611Schristos 	bf->bf_txflags = txflags;
146864f89611Schristos 
146964f89611Schristos 	wh = mtod(m, struct ieee80211_frame *);
147064f89611Schristos 
147164f89611Schristos 	totlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
147264f89611Schristos 
147364f89611Schristos 	/* Clear all Tx descriptors that we will use. */
147464f89611Schristos 	memset(bf->bf_descs, 0, bf->bf_map->dm_nsegs * sizeof(*ds));
147564f89611Schristos 
147664f89611Schristos 	/* Setup first Tx descriptor. */
147764f89611Schristos 	ds = bf->bf_descs;
147864f89611Schristos 
147964f89611Schristos 	ds->ds_ctl0 = AR_TXC0_INTR_REQ | AR_TXC0_CLR_DEST_MASK;
148064f89611Schristos 	txpower = AR_MAX_RATE_POWER;	/* Get from per-rate registers. */
148164f89611Schristos 	ds->ds_ctl0 |= SM(AR_TXC0_XMIT_POWER, txpower);
148264f89611Schristos 
148364f89611Schristos 	ds->ds_ctl1 = SM(AR_TXC1_FRAME_TYPE, type);
148464f89611Schristos 
148564f89611Schristos 	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
148664f89611Schristos 	    (hasqos && (qos & IEEE80211_QOS_ACKPOLICY_MASK) ==
148764f89611Schristos 	     IEEE80211_QOS_ACKPOLICY_NOACK))
148864f89611Schristos 		ds->ds_ctl1 |= AR_TXC1_NO_ACK;
148964f89611Schristos #if notyet
149064f89611Schristos 	if (0 && k != NULL) {
149164f89611Schristos 		uintptr_t entry;
149264f89611Schristos 
149364f89611Schristos 		/*
149464f89611Schristos 		 * Map 802.11 cipher to hardware encryption type and
149564f89611Schristos 		 * compute MIC+ICV overhead.
149664f89611Schristos 		 */
149764f89611Schristos 		totlen += k->wk_keylen;
149864f89611Schristos 		switch (k->wk_cipher->ic_cipher) {
149964f89611Schristos 		case IEEE80211_CIPHER_WEP:
150064f89611Schristos 			encrtype = AR_ENCR_TYPE_WEP;
150164f89611Schristos 			break;
150264f89611Schristos 		case IEEE80211_CIPHER_TKIP:
150364f89611Schristos 			encrtype = AR_ENCR_TYPE_TKIP;
150464f89611Schristos 			break;
150564f89611Schristos 		case IEEE80211_CIPHER_AES_OCB:
150664f89611Schristos 		case IEEE80211_CIPHER_AES_CCM:
150764f89611Schristos 			encrtype = AR_ENCR_TYPE_AES;
150864f89611Schristos 			break;
150964f89611Schristos 		default:
151064f89611Schristos 			panic("unsupported cipher");
151164f89611Schristos 		}
151264f89611Schristos 		/*
151364f89611Schristos 		 * NB: The key cache entry index is stored in the key
151464f89611Schristos 		 * private field when the key is installed.
151564f89611Schristos 		 */
151664f89611Schristos 		entry = (uintptr_t)k->k_priv;
151764f89611Schristos 		ds->ds_ctl1 |= SM(AR_TXC1_DEST_IDX, entry);
151864f89611Schristos 		ds->ds_ctl0 |= AR_TXC0_DEST_IDX_VALID;
151964f89611Schristos 	}
152064f89611Schristos 	else
152164f89611Schristos #endif
152264f89611Schristos 		encrtype = AR_ENCR_TYPE_CLEAR;
152364f89611Schristos 	ds->ds_ctl6 = SM(AR_TXC6_ENCR_TYPE, encrtype);
152464f89611Schristos 
152564f89611Schristos 	/* Check if frame must be protected using RTS/CTS or CTS-to-self. */
152664f89611Schristos 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
152764f89611Schristos 		/* NB: Group frames are sent using CCK in 802.11b/g. */
152864f89611Schristos 		if (totlen > ic->ic_rtsthreshold) {
152964f89611Schristos 			ds->ds_ctl0 |= AR_TXC0_RTS_ENABLE;
153064f89611Schristos 		}
153164f89611Schristos 		else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
153264f89611Schristos 		    athn_rates[ridx[0]].phy == IEEE80211_T_OFDM) {
153364f89611Schristos 			if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
153464f89611Schristos 				ds->ds_ctl0 |= AR_TXC0_RTS_ENABLE;
153564f89611Schristos 			else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
153664f89611Schristos 				ds->ds_ctl0 |= AR_TXC0_CTS_ENABLE;
153764f89611Schristos 		}
153864f89611Schristos 	}
153964f89611Schristos 	if (ds->ds_ctl0 & (AR_TXC0_RTS_ENABLE | AR_TXC0_CTS_ENABLE)) {
154064f89611Schristos 		/* Disable multi-rate retries when protection is used. */
154164f89611Schristos 		ridx[1] = ridx[2] = ridx[3] = ridx[0];
154264f89611Schristos 	}
154364f89611Schristos 	/* Setup multi-rate retries. */
154464f89611Schristos 	for (i = 0; i < 4; i++) {
154564f89611Schristos 		series[i].hwrate = athn_rates[ridx[i]].hwrate;
154664f89611Schristos 		if (athn_rates[ridx[i]].phy == IEEE80211_T_DS &&
154764f89611Schristos 		    ridx[i] != ATHN_RIDX_CCK1 &&
154864f89611Schristos 		    (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
154964f89611Schristos 			series[i].hwrate |= 0x04;
155064f89611Schristos 		series[i].dur = 0;
155164f89611Schristos 	}
155264f89611Schristos 	if (!(ds->ds_ctl1 & AR_TXC1_NO_ACK)) {
155364f89611Schristos 		/* Compute duration for each series. */
155464f89611Schristos 		for (i = 0; i < 4; i++) {
155564f89611Schristos 			series[i].dur = athn_txtime(sc, IEEE80211_ACK_LEN,
155664f89611Schristos 			    athn_rates[ridx[i]].rspridx, ic->ic_flags);
155764f89611Schristos 		}
155864f89611Schristos 	}
155964f89611Schristos 
156064f89611Schristos 	/* Write number of tries for each series. */
156164f89611Schristos 	ds->ds_ctl2 =
156264f89611Schristos 	    SM(AR_TXC2_XMIT_DATA_TRIES0, 2) |
156364f89611Schristos 	    SM(AR_TXC2_XMIT_DATA_TRIES1, 2) |
156464f89611Schristos 	    SM(AR_TXC2_XMIT_DATA_TRIES2, 2) |
156564f89611Schristos 	    SM(AR_TXC2_XMIT_DATA_TRIES3, 4);
156664f89611Schristos 
156764f89611Schristos 	/* Tell HW to update duration field in 802.11 header. */
156864f89611Schristos 	if (type != AR_FRAME_TYPE_PSPOLL)
156964f89611Schristos 		ds->ds_ctl2 |= AR_TXC2_DUR_UPDATE_ENA;
157064f89611Schristos 
157164f89611Schristos 	/* Write Tx rate for each series. */
157264f89611Schristos 	ds->ds_ctl3 =
157364f89611Schristos 	    SM(AR_TXC3_XMIT_RATE0, series[0].hwrate) |
157464f89611Schristos 	    SM(AR_TXC3_XMIT_RATE1, series[1].hwrate) |
157564f89611Schristos 	    SM(AR_TXC3_XMIT_RATE2, series[2].hwrate) |
157664f89611Schristos 	    SM(AR_TXC3_XMIT_RATE3, series[3].hwrate);
157764f89611Schristos 
157864f89611Schristos 	/* Write duration for each series. */
157964f89611Schristos 	ds->ds_ctl4 =
158064f89611Schristos 	    SM(AR_TXC4_PACKET_DUR0, series[0].dur) |
158164f89611Schristos 	    SM(AR_TXC4_PACKET_DUR1, series[1].dur);
158264f89611Schristos 	ds->ds_ctl5 =
158364f89611Schristos 	    SM(AR_TXC5_PACKET_DUR2, series[2].dur) |
158464f89611Schristos 	    SM(AR_TXC5_PACKET_DUR3, series[3].dur);
158564f89611Schristos 
158664f89611Schristos 	/* Use the same Tx chains for all tries. */
158764f89611Schristos 	ds->ds_ctl7 =
158864f89611Schristos 	    SM(AR_TXC7_CHAIN_SEL0, sc->sc_txchainmask) |
158964f89611Schristos 	    SM(AR_TXC7_CHAIN_SEL1, sc->sc_txchainmask) |
159064f89611Schristos 	    SM(AR_TXC7_CHAIN_SEL2, sc->sc_txchainmask) |
159164f89611Schristos 	    SM(AR_TXC7_CHAIN_SEL3, sc->sc_txchainmask);
159264f89611Schristos #ifdef notyet
159364f89611Schristos #ifndef IEEE80211_NO_HT
159464f89611Schristos 	/* Use the same short GI setting for all tries. */
159564f89611Schristos 	if (ic->ic_flags & IEEE80211_F_SHGI)
159664f89611Schristos 		ds->ds_ctl7 |= AR_TXC7_GI0123;
159764f89611Schristos 	/* Use the same channel width for all tries. */
159864f89611Schristos 	if (ic->ic_flags & IEEE80211_F_CBW40)
159964f89611Schristos 		ds->ds_ctl7 |= AR_TXC7_2040_0123;
160064f89611Schristos #endif
160164f89611Schristos #endif
160264f89611Schristos 
160364f89611Schristos 	if (ds->ds_ctl0 & (AR_TXC0_RTS_ENABLE | AR_TXC0_CTS_ENABLE)) {
160464f89611Schristos 		uint8_t protridx, hwrate;
160564f89611Schristos 		uint16_t dur = 0;
160664f89611Schristos 
160764f89611Schristos 		/* Use the same protection mode for all tries. */
160864f89611Schristos 		if (ds->ds_ctl0 & AR_TXC0_RTS_ENABLE) {
160964f89611Schristos 			ds->ds_ctl4 |= AR_TXC4_RTSCTS_QUAL01;
161064f89611Schristos 			ds->ds_ctl5 |= AR_TXC5_RTSCTS_QUAL23;
161164f89611Schristos 		}
161264f89611Schristos 		/* Select protection rate (suboptimal but ok). */
161364f89611Schristos 		protridx = (ic->ic_curmode == IEEE80211_MODE_11A) ?
161464f89611Schristos 		    ATHN_RIDX_OFDM6 : ATHN_RIDX_CCK2;
161564f89611Schristos 		if (ds->ds_ctl0 & AR_TXC0_RTS_ENABLE) {
161664f89611Schristos 			/* Account for CTS duration. */
161764f89611Schristos 			dur += athn_txtime(sc, IEEE80211_ACK_LEN,
161864f89611Schristos 			    athn_rates[protridx].rspridx, ic->ic_flags);
161964f89611Schristos 		}
162064f89611Schristos 		dur += athn_txtime(sc, totlen, ridx[0], ic->ic_flags);
162164f89611Schristos 		if (!(ds->ds_ctl1 & AR_TXC1_NO_ACK)) {
162264f89611Schristos 			/* Account for ACK duration. */
162364f89611Schristos 			dur += athn_txtime(sc, IEEE80211_ACK_LEN,
162464f89611Schristos 			    athn_rates[ridx[0]].rspridx, ic->ic_flags);
162564f89611Schristos 		}
162664f89611Schristos 		/* Write protection frame duration and rate. */
162764f89611Schristos 		ds->ds_ctl2 |= SM(AR_TXC2_BURST_DUR, dur);
162864f89611Schristos 		hwrate = athn_rates[protridx].hwrate;
162964f89611Schristos 		if (protridx == ATHN_RIDX_CCK2 &&
163064f89611Schristos 		    (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
163164f89611Schristos 			hwrate |= 0x04;
163264f89611Schristos 		ds->ds_ctl7 |= SM(AR_TXC7_RTSCTS_RATE, hwrate);
163364f89611Schristos 	}
163464f89611Schristos 
163564f89611Schristos 	/* Finalize first Tx descriptor and fill others (if any). */
163664f89611Schristos 	ds->ds_ctl0 |= SM(AR_TXC0_FRAME_LEN, totlen);
163764f89611Schristos 
163864f89611Schristos 	lastds = NULL;	/* XXX: gcc */
163964f89611Schristos 	for (i = 0; i < bf->bf_map->dm_nsegs; i++, ds++) {
164064f89611Schristos 		ds->ds_data = bf->bf_map->dm_segs[i].ds_addr;
164164f89611Schristos 		ds->ds_ctl1 |= SM(AR_TXC1_BUF_LEN,
164264f89611Schristos 		    bf->bf_map->dm_segs[i].ds_len);
164364f89611Schristos 
164464f89611Schristos 		if (i != bf->bf_map->dm_nsegs - 1)
164564f89611Schristos 			ds->ds_ctl1 |= AR_TXC1_MORE;
164664f89611Schristos 		ds->ds_link = 0;
164764f89611Schristos 
164864f89611Schristos 		/* Chain Tx descriptor. */
164964f89611Schristos 		if (i != 0)
165064f89611Schristos 			lastds->ds_link = bf->bf_daddr + i * sizeof(*ds);
165164f89611Schristos 		lastds = ds;
165264f89611Schristos 	}
165364f89611Schristos 	bus_dmamap_sync(sc->sc_dmat, bf->bf_map, 0, bf->bf_map->dm_mapsize,
165464f89611Schristos 	    BUS_DMASYNC_PREWRITE);
165564f89611Schristos 
165664f89611Schristos 	if (!SIMPLEQ_EMPTY(&txq->head))
165764f89611Schristos 		((struct ar_tx_desc *)txq->lastds)->ds_link = bf->bf_daddr;
165864f89611Schristos 	else
165964f89611Schristos 		AR_WRITE(sc, AR_QTXDP(qid), bf->bf_daddr);
166064f89611Schristos 	txq->lastds = lastds;
166164f89611Schristos 	SIMPLEQ_REMOVE_HEAD(&sc->sc_txbufs, bf_list);
166264f89611Schristos 	SIMPLEQ_INSERT_TAIL(&txq->head, bf, bf_list);
166364f89611Schristos 
166464f89611Schristos 	ds = bf->bf_descs;
166564f89611Schristos 	DPRINTFN(DBG_TX, sc,
166664f89611Schristos 	    "Tx qid=%d nsegs=%d ctl0=0x%x ctl1=0x%x ctl3=0x%x\n",
166764f89611Schristos 	    qid, bf->bf_map->dm_nsegs, ds->ds_ctl0, ds->ds_ctl1, ds->ds_ctl3);
166864f89611Schristos 
166964f89611Schristos 	/* Kick Tx. */
167064f89611Schristos 	AR_WRITE(sc, AR_Q_TXE, 1 << qid);
167164f89611Schristos 	AR_WRITE_BARRIER(sc);
167264f89611Schristos 	return 0;
167364f89611Schristos }
167464f89611Schristos 
167564f89611Schristos Static void
ar5008_set_rf_mode(struct athn_softc * sc,struct ieee80211_channel * c)167664f89611Schristos ar5008_set_rf_mode(struct athn_softc *sc, struct ieee80211_channel *c)
167764f89611Schristos {
167864f89611Schristos 	uint32_t reg;
167964f89611Schristos 
168064f89611Schristos 	reg = IEEE80211_IS_CHAN_2GHZ(c) ?
168164f89611Schristos 	    AR_PHY_MODE_DYNAMIC : AR_PHY_MODE_OFDM;
168264f89611Schristos 	if (!AR_SREV_9280_10_OR_LATER(sc)) {
168364f89611Schristos 		reg |= IEEE80211_IS_CHAN_2GHZ(c) ?
168464f89611Schristos 		    AR_PHY_MODE_RF2GHZ : AR_PHY_MODE_RF5GHZ;
168564f89611Schristos 	}
168664f89611Schristos 	else if (IEEE80211_IS_CHAN_5GHZ(c) &&
168764f89611Schristos 	    (sc->sc_flags & ATHN_FLAG_FAST_PLL_CLOCK)) {
168864f89611Schristos 		reg |= AR_PHY_MODE_DYNAMIC | AR_PHY_MODE_DYN_CCK_DISABLE;
168964f89611Schristos 	}
169064f89611Schristos 	AR_WRITE(sc, AR_PHY_MODE, reg);
169164f89611Schristos 	AR_WRITE_BARRIER(sc);
169264f89611Schristos }
169364f89611Schristos 
169464f89611Schristos static __inline uint32_t
ar5008_synth_delay(struct athn_softc * sc)169564f89611Schristos ar5008_synth_delay(struct athn_softc *sc)
169664f89611Schristos {
16975abc6aa5Sskrll 	uint32_t synth_delay;
169864f89611Schristos 
16995abc6aa5Sskrll 	synth_delay = MS(AR_READ(sc, AR_PHY_RX_DELAY), AR_PHY_RX_DELAY_DELAY);
170064f89611Schristos 	if (sc->sc_ic.ic_curmode == IEEE80211_MODE_11B)
17015abc6aa5Sskrll 		synth_delay = (synth_delay * 4) / 22;
170264f89611Schristos 	else
17035abc6aa5Sskrll 		synth_delay = synth_delay / 10;	/* in 100ns steps */
17045abc6aa5Sskrll 	return synth_delay;
170564f89611Schristos }
170664f89611Schristos 
170764f89611Schristos Static int
ar5008_rf_bus_request(struct athn_softc * sc)170864f89611Schristos ar5008_rf_bus_request(struct athn_softc *sc)
170964f89611Schristos {
171064f89611Schristos 	int ntries;
171164f89611Schristos 
171264f89611Schristos 	/* Request RF Bus grant. */
171364f89611Schristos 	AR_WRITE(sc, AR_PHY_RFBUS_REQ, AR_PHY_RFBUS_REQ_EN);
171464f89611Schristos 	for (ntries = 0; ntries < 10000; ntries++) {
171564f89611Schristos 		if (AR_READ(sc, AR_PHY_RFBUS_GRANT) & AR_PHY_RFBUS_GRANT_EN)
171664f89611Schristos 			return 0;
171764f89611Schristos 		DELAY(10);
171864f89611Schristos 	}
171964f89611Schristos 	DPRINTFN(DBG_RF, sc, "could not kill baseband Rx");
172064f89611Schristos 	return ETIMEDOUT;
172164f89611Schristos }
172264f89611Schristos 
172364f89611Schristos Static void
ar5008_rf_bus_release(struct athn_softc * sc)172464f89611Schristos ar5008_rf_bus_release(struct athn_softc *sc)
172564f89611Schristos {
172664f89611Schristos 
172764f89611Schristos 	/* Wait for the synthesizer to settle. */
172864f89611Schristos 	DELAY(AR_BASE_PHY_ACTIVE_DELAY + ar5008_synth_delay(sc));
172964f89611Schristos 
173064f89611Schristos 	/* Release the RF Bus grant. */
173164f89611Schristos 	AR_WRITE(sc, AR_PHY_RFBUS_REQ, 0);
173264f89611Schristos 	AR_WRITE_BARRIER(sc);
173364f89611Schristos }
173464f89611Schristos 
173564f89611Schristos Static void
ar5008_set_phy(struct athn_softc * sc,struct ieee80211_channel * c,struct ieee80211_channel * extc)173664f89611Schristos ar5008_set_phy(struct athn_softc *sc, struct ieee80211_channel *c,
173764f89611Schristos     struct ieee80211_channel *extc)
173864f89611Schristos {
173964f89611Schristos 	uint32_t phy;
174064f89611Schristos 
174164f89611Schristos 	if (AR_SREV_9285_10_OR_LATER(sc))
174264f89611Schristos 		phy = AR_READ(sc, AR_PHY_TURBO) & AR_PHY_FC_ENABLE_DAC_FIFO;
174364f89611Schristos 	else
174464f89611Schristos 		phy = 0;
174564f89611Schristos 	phy |= AR_PHY_FC_HT_EN | AR_PHY_FC_SHORT_GI_40 |
174664f89611Schristos 	    AR_PHY_FC_SINGLE_HT_LTF1 | AR_PHY_FC_WALSH;
174764f89611Schristos #ifndef IEEE80211_NO_HT
174864f89611Schristos 	if (extc != NULL) {
174964f89611Schristos 		phy |= AR_PHY_FC_DYN2040_EN;
175064f89611Schristos 		if (extc > c)	/* XXX */
175164f89611Schristos 			phy |= AR_PHY_FC_DYN2040_PRI_CH;
175264f89611Schristos 	}
175364f89611Schristos #endif
175464f89611Schristos 	AR_WRITE(sc, AR_PHY_TURBO, phy);
175564f89611Schristos 
175664f89611Schristos 	AR_WRITE(sc, AR_2040_MODE,
175764f89611Schristos 	    (extc != NULL) ? AR_2040_JOINED_RX_CLEAR : 0);
175864f89611Schristos 
175964f89611Schristos 	/* Set global transmit timeout. */
176064f89611Schristos 	AR_WRITE(sc, AR_GTXTO, SM(AR_GTXTO_TIMEOUT_LIMIT, 25));
176164f89611Schristos 	/* Set carrier sense timeout. */
176264f89611Schristos 	AR_WRITE(sc, AR_CST, SM(AR_CST_TIMEOUT_LIMIT, 15));
176364f89611Schristos 	AR_WRITE_BARRIER(sc);
176464f89611Schristos }
176564f89611Schristos 
176664f89611Schristos Static void
ar5008_set_delta_slope(struct athn_softc * sc,struct ieee80211_channel * c,struct ieee80211_channel * extc)176764f89611Schristos ar5008_set_delta_slope(struct athn_softc *sc, struct ieee80211_channel *c,
176864f89611Schristos     struct ieee80211_channel *extc)
176964f89611Schristos {
177064f89611Schristos 	uint32_t coeff, exp, man, reg;
177164f89611Schristos 
177264f89611Schristos 	/* Set Delta Slope (exponent and mantissa). */
177364f89611Schristos 	coeff = (100 << 24) / c->ic_freq;
177464f89611Schristos 	athn_get_delta_slope(coeff, &exp, &man);
177564f89611Schristos 	DPRINTFN(DBG_RX, sc, "delta slope coeff exp=%u man=%u\n", exp, man);
177664f89611Schristos 
177764f89611Schristos 	reg = AR_READ(sc, AR_PHY_TIMING3);
177864f89611Schristos 	reg = RW(reg, AR_PHY_TIMING3_DSC_EXP, exp);
177964f89611Schristos 	reg = RW(reg, AR_PHY_TIMING3_DSC_MAN, man);
178064f89611Schristos 	AR_WRITE(sc, AR_PHY_TIMING3, reg);
178164f89611Schristos 
178264f89611Schristos 	/* For Short GI, coeff is 9/10 that of normal coeff. */
178364f89611Schristos 	coeff = (9 * coeff) / 10;
178464f89611Schristos 	athn_get_delta_slope(coeff, &exp, &man);
178564f89611Schristos 	DPRINTFN(DBG_RX, sc, "delta slope coeff exp=%u man=%u\n", exp, man);
178664f89611Schristos 
178764f89611Schristos 	reg = AR_READ(sc, AR_PHY_HALFGI);
178864f89611Schristos 	reg = RW(reg, AR_PHY_HALFGI_DSC_EXP, exp);
178964f89611Schristos 	reg = RW(reg, AR_PHY_HALFGI_DSC_MAN, man);
179064f89611Schristos 	AR_WRITE(sc, AR_PHY_HALFGI, reg);
179164f89611Schristos 	AR_WRITE_BARRIER(sc);
179264f89611Schristos }
179364f89611Schristos 
179464f89611Schristos Static void
ar5008_enable_antenna_diversity(struct athn_softc * sc)179564f89611Schristos ar5008_enable_antenna_diversity(struct athn_softc *sc)
179664f89611Schristos {
179764f89611Schristos 
179864f89611Schristos 	AR_SETBITS(sc, AR_PHY_CCK_DETECT,
179964f89611Schristos 	    AR_PHY_CCK_DETECT_BB_ENABLE_ANT_FAST_DIV);
180064f89611Schristos 	AR_WRITE_BARRIER(sc);
180164f89611Schristos }
180264f89611Schristos 
180364f89611Schristos Static void
ar5008_init_baseband(struct athn_softc * sc)180464f89611Schristos ar5008_init_baseband(struct athn_softc *sc)
180564f89611Schristos {
180664f89611Schristos 	uint32_t synth_delay;
180764f89611Schristos 
180864f89611Schristos 	synth_delay = ar5008_synth_delay(sc);
180964f89611Schristos 	/* Activate the PHY (includes baseband activate and synthesizer on). */
181064f89611Schristos 	AR_WRITE(sc, AR_PHY_ACTIVE, AR_PHY_ACTIVE_EN);
181164f89611Schristos 	AR_WRITE_BARRIER(sc);
181264f89611Schristos 	DELAY(AR_BASE_PHY_ACTIVE_DELAY + synth_delay);
181364f89611Schristos }
181464f89611Schristos 
181564f89611Schristos Static void
ar5008_disable_phy(struct athn_softc * sc)181664f89611Schristos ar5008_disable_phy(struct athn_softc *sc)
181764f89611Schristos {
181864f89611Schristos 
181964f89611Schristos 	AR_WRITE(sc, AR_PHY_ACTIVE, AR_PHY_ACTIVE_DIS);
182064f89611Schristos 	AR_WRITE_BARRIER(sc);
182164f89611Schristos }
182264f89611Schristos 
182364f89611Schristos Static void
ar5008_init_chains(struct athn_softc * sc)182464f89611Schristos ar5008_init_chains(struct athn_softc *sc)
182564f89611Schristos {
182664f89611Schristos 
182764f89611Schristos 	if (sc->sc_rxchainmask == 0x5 || sc->sc_txchainmask == 0x5)
182864f89611Schristos 		AR_SETBITS(sc, AR_PHY_ANALOG_SWAP, AR_PHY_SWAP_ALT_CHAIN);
182964f89611Schristos 
183064f89611Schristos 	/* Setup chain masks. */
183164f89611Schristos 	if (sc->sc_mac_ver <= AR_SREV_VERSION_9160 &&
183264f89611Schristos 	    (sc->sc_rxchainmask == 0x3 || sc->sc_rxchainmask == 0x5)) {
183364f89611Schristos 		AR_WRITE(sc, AR_PHY_RX_CHAINMASK,  0x7);
183464f89611Schristos 		AR_WRITE(sc, AR_PHY_CAL_CHAINMASK, 0x7);
183564f89611Schristos 	}
183664f89611Schristos 	else {
183764f89611Schristos 		AR_WRITE(sc, AR_PHY_RX_CHAINMASK,  sc->sc_rxchainmask);
183864f89611Schristos 		AR_WRITE(sc, AR_PHY_CAL_CHAINMASK, sc->sc_rxchainmask);
183964f89611Schristos 	}
184064f89611Schristos 	AR_WRITE(sc, AR_SELFGEN_MASK, sc->sc_txchainmask);
184164f89611Schristos 	AR_WRITE_BARRIER(sc);
184264f89611Schristos }
184364f89611Schristos 
184464f89611Schristos Static void
ar5008_set_rxchains(struct athn_softc * sc)184564f89611Schristos ar5008_set_rxchains(struct athn_softc *sc)
184664f89611Schristos {
184764f89611Schristos 
184864f89611Schristos 	if (sc->sc_rxchainmask == 0x3 || sc->sc_rxchainmask == 0x5) {
184964f89611Schristos 		AR_WRITE(sc, AR_PHY_RX_CHAINMASK,  sc->sc_rxchainmask);
185064f89611Schristos 		AR_WRITE(sc, AR_PHY_CAL_CHAINMASK, sc->sc_rxchainmask);
185164f89611Schristos 		AR_WRITE_BARRIER(sc);
185264f89611Schristos 	}
185364f89611Schristos }
185464f89611Schristos 
185564f89611Schristos #ifdef notused
185664f89611Schristos Static void
ar5008_read_noisefloor(struct athn_softc * sc,int16_t * nf,int16_t * nf_ext)185764f89611Schristos ar5008_read_noisefloor(struct athn_softc *sc, int16_t *nf, int16_t *nf_ext)
185864f89611Schristos {
185964f89611Schristos /* Sign-extends 9-bit value (assumes upper bits are zeroes). */
186064f89611Schristos #define SIGN_EXT(v)	(((v) ^ 0x100) - 0x100)
186164f89611Schristos 	uint32_t reg;
186264f89611Schristos 	int i;
186364f89611Schristos 
186464f89611Schristos 	for (i = 0; i < sc->sc_nrxchains; i++) {
186564f89611Schristos 		reg = AR_READ(sc, AR_PHY_CCA(i));
186664f89611Schristos 		if (AR_SREV_9280_10_OR_LATER(sc))
186764f89611Schristos 			nf[i] = MS(reg, AR9280_PHY_MINCCA_PWR);
186864f89611Schristos 		else
186964f89611Schristos 			nf[i] = MS(reg, AR_PHY_MINCCA_PWR);
187064f89611Schristos 		nf[i] = SIGN_EXT(nf[i]);
187164f89611Schristos 
187264f89611Schristos 		reg = AR_READ(sc, AR_PHY_EXT_CCA(i));
187364f89611Schristos 		if (AR_SREV_9280_10_OR_LATER(sc))
187464f89611Schristos 			nf_ext[i] = MS(reg, AR9280_PHY_EXT_MINCCA_PWR);
187564f89611Schristos 		else
187664f89611Schristos 			nf_ext[i] = MS(reg, AR_PHY_EXT_MINCCA_PWR);
187764f89611Schristos 		nf_ext[i] = SIGN_EXT(nf_ext[i]);
187864f89611Schristos 	}
187964f89611Schristos #undef SIGN_EXT
188064f89611Schristos }
188164f89611Schristos #endif /* notused */
188264f89611Schristos 
188364f89611Schristos #ifdef notused
188464f89611Schristos Static void
ar5008_write_noisefloor(struct athn_softc * sc,int16_t * nf,int16_t * nf_ext)188564f89611Schristos ar5008_write_noisefloor(struct athn_softc *sc, int16_t *nf, int16_t *nf_ext)
188664f89611Schristos {
188764f89611Schristos 	uint32_t reg;
188864f89611Schristos 	int i;
188964f89611Schristos 
189064f89611Schristos 	for (i = 0; i < sc->sc_nrxchains; i++) {
189164f89611Schristos 		reg = AR_READ(sc, AR_PHY_CCA(i));
189264f89611Schristos 		reg = RW(reg, AR_PHY_MAXCCA_PWR, nf[i]);
189364f89611Schristos 		AR_WRITE(sc, AR_PHY_CCA(i), reg);
189464f89611Schristos 
189564f89611Schristos 		reg = AR_READ(sc, AR_PHY_EXT_CCA(i));
189664f89611Schristos 		reg = RW(reg, AR_PHY_EXT_MAXCCA_PWR, nf_ext[i]);
189764f89611Schristos 		AR_WRITE(sc, AR_PHY_EXT_CCA(i), reg);
189864f89611Schristos 	}
189964f89611Schristos 	AR_WRITE_BARRIER(sc);
190064f89611Schristos }
190164f89611Schristos #endif /* notused */
190264f89611Schristos 
190364f89611Schristos #ifdef notused
190464f89611Schristos Static void
ar5008_get_noisefloor(struct athn_softc * sc,struct ieee80211_channel * c)190564f89611Schristos ar5008_get_noisefloor(struct athn_softc *sc, struct ieee80211_channel *c)
190664f89611Schristos {
190764f89611Schristos 	int16_t nf[AR_MAX_CHAINS], nf_ext[AR_MAX_CHAINS];
190864f89611Schristos 	int i;
190964f89611Schristos 
191064f89611Schristos 	if (AR_READ(sc, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF) {
191164f89611Schristos 		/* Noisefloor calibration not finished. */
191264f89611Schristos 		return;
191364f89611Schristos 	}
191464f89611Schristos 	/* Noisefloor calibration is finished. */
191564f89611Schristos 	ar5008_read_noisefloor(sc, nf, nf_ext);
191664f89611Schristos 
191764f89611Schristos 	/* Update noisefloor history. */
191864f89611Schristos 	for (i = 0; i < sc->sc_nrxchains; i++) {
191964f89611Schristos 		sc->sc_nf_hist[sc->sc_nf_hist_cur].nf[i] = nf[i];
192064f89611Schristos 		sc->sc_nf_hist[sc->sc_nf_hist_cur].nf_ext[i] = nf_ext[i];
192164f89611Schristos 	}
192264f89611Schristos 	if (++sc->sc_nf_hist_cur >= ATHN_NF_CAL_HIST_MAX)
192364f89611Schristos 		sc->sc_nf_hist_cur = 0;
192464f89611Schristos }
192564f89611Schristos #endif /* notused */
192664f89611Schristos 
192764f89611Schristos #ifdef notused
192864f89611Schristos Static void
ar5008_bb_load_noisefloor(struct athn_softc * sc)192964f89611Schristos ar5008_bb_load_noisefloor(struct athn_softc *sc)
193064f89611Schristos {
193164f89611Schristos 	int16_t nf[AR_MAX_CHAINS], nf_ext[AR_MAX_CHAINS];
193264f89611Schristos 	int i, ntries;
193364f89611Schristos 
193464f89611Schristos 	/* Write filtered noisefloor values. */
193564f89611Schristos 	for (i = 0; i < sc->sc_nrxchains; i++) {
193664f89611Schristos 		nf[i] = sc->sc_nf_priv[i] * 2;
193764f89611Schristos 		nf_ext[i] = sc->sc_nf_ext_priv[i] * 2;
193864f89611Schristos 	}
193964f89611Schristos 	ar5008_write_noisefloor(sc, nf, nf_ext);
194064f89611Schristos 
194164f89611Schristos 	/* Load filtered noisefloor values into baseband. */
194264f89611Schristos 	AR_CLRBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_ENABLE_NF);
194364f89611Schristos 	AR_CLRBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
194464f89611Schristos 	AR_SETBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
194564f89611Schristos 	/* Wait for load to complete. */
194664f89611Schristos 	for (ntries = 0; ntries < 1000; ntries++) {
194764f89611Schristos 		if (!(AR_READ(sc, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF))
194864f89611Schristos 			break;
194964f89611Schristos 		DELAY(50);
195064f89611Schristos 	}
195164f89611Schristos 	if (ntries == 1000) {
195264f89611Schristos 		DPRINTFN(DBG_RF, sc, "failed to load noisefloor values\n");
195364f89611Schristos 		return;
195464f89611Schristos 	}
195564f89611Schristos 
195664f89611Schristos 	/* Restore noisefloor values to initial (max) values. */
195764f89611Schristos 	for (i = 0; i < AR_MAX_CHAINS; i++)
195864f89611Schristos 		nf[i] = nf_ext[i] = -50 * 2;
195964f89611Schristos 	ar5008_write_noisefloor(sc, nf, nf_ext);
196064f89611Schristos }
196164f89611Schristos #endif /* notused */
196264f89611Schristos 
196364f89611Schristos #ifdef notused
196464f89611Schristos Static void
ar5008_noisefloor_calib(struct athn_softc * sc)196564f89611Schristos ar5008_noisefloor_calib(struct athn_softc *sc)
196664f89611Schristos {
196764f89611Schristos 
196864f89611Schristos 	AR_SETBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_ENABLE_NF);
196964f89611Schristos 	AR_SETBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
197064f89611Schristos 	AR_SETBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
197164f89611Schristos 	AR_WRITE_BARRIER(sc);
197264f89611Schristos }
197364f89611Schristos #endif /* notused */
197464f89611Schristos 
197564f89611Schristos Static void
ar5008_do_noisefloor_calib(struct athn_softc * sc)197664f89611Schristos ar5008_do_noisefloor_calib(struct athn_softc *sc)
197764f89611Schristos {
197864f89611Schristos 
197964f89611Schristos 	AR_SETBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
198064f89611Schristos 	AR_WRITE_BARRIER(sc);
198164f89611Schristos }
198264f89611Schristos 
198364f89611Schristos Static void
ar5008_do_calib(struct athn_softc * sc)198464f89611Schristos ar5008_do_calib(struct athn_softc *sc)
198564f89611Schristos {
198664f89611Schristos 	uint32_t mode, reg;
198764f89611Schristos 	int log;
198864f89611Schristos 
198964f89611Schristos 	reg = AR_READ(sc, AR_PHY_TIMING_CTRL4_0);
199064f89611Schristos 	log = AR_SREV_9280_10_OR_LATER(sc) ? 10 : 2;
199164f89611Schristos 	reg = RW(reg, AR_PHY_TIMING_CTRL4_IQCAL_LOG_COUNT_MAX, log);
199264f89611Schristos 	AR_WRITE(sc, AR_PHY_TIMING_CTRL4_0, reg);
199364f89611Schristos 
199464f89611Schristos 	if (sc->sc_cur_calib_mask & ATHN_CAL_ADC_GAIN)
199564f89611Schristos 		mode = AR_PHY_CALMODE_ADC_GAIN;
199664f89611Schristos 	else if (sc->sc_cur_calib_mask & ATHN_CAL_ADC_DC)
199764f89611Schristos 		mode = AR_PHY_CALMODE_ADC_DC_PER;
199864f89611Schristos 	else	/* ATHN_CAL_IQ */
199964f89611Schristos 		mode = AR_PHY_CALMODE_IQ;
200064f89611Schristos 	AR_WRITE(sc, AR_PHY_CALMODE, mode);
200164f89611Schristos 
200264f89611Schristos 	DPRINTFN(DBG_RF, sc, "starting calibration mode=0x%x\n", mode);
200364f89611Schristos 	AR_SETBITS(sc, AR_PHY_TIMING_CTRL4_0, AR_PHY_TIMING_CTRL4_DO_CAL);
200464f89611Schristos 	AR_WRITE_BARRIER(sc);
200564f89611Schristos }
200664f89611Schristos 
200764f89611Schristos Static void
ar5008_next_calib(struct athn_softc * sc)200864f89611Schristos ar5008_next_calib(struct athn_softc *sc)
200964f89611Schristos {
201064f89611Schristos 
201164f89611Schristos 	/* Check if we have any calibration in progress. */
201264f89611Schristos 	if (sc->sc_cur_calib_mask != 0) {
201364f89611Schristos 		if (!(AR_READ(sc, AR_PHY_TIMING_CTRL4_0) &
201464f89611Schristos 		    AR_PHY_TIMING_CTRL4_DO_CAL)) {
201564f89611Schristos 			/* Calibration completed for current sample. */
201664f89611Schristos 			if (sc->sc_cur_calib_mask & ATHN_CAL_ADC_GAIN)
201764f89611Schristos 				ar5008_calib_adc_gain(sc);
201864f89611Schristos 			else if (sc->sc_cur_calib_mask & ATHN_CAL_ADC_DC)
201964f89611Schristos 				ar5008_calib_adc_dc_off(sc);
202064f89611Schristos 			else	/* ATHN_CAL_IQ */
202164f89611Schristos 				ar5008_calib_iq(sc);
202264f89611Schristos 		}
202364f89611Schristos 	}
202464f89611Schristos }
202564f89611Schristos 
202664f89611Schristos Static void
ar5008_calib_iq(struct athn_softc * sc)202764f89611Schristos ar5008_calib_iq(struct athn_softc *sc)
202864f89611Schristos {
202964f89611Schristos 	struct athn_iq_cal *cal;
203064f89611Schristos 	uint32_t reg, i_coff_denom, q_coff_denom;
203164f89611Schristos 	int32_t i_coff, q_coff;
203264f89611Schristos 	int i, iq_corr_neg;
203364f89611Schristos 
203464f89611Schristos 	for (i = 0; i < AR_MAX_CHAINS; i++) {
203564f89611Schristos 		cal = &sc->sc_calib.iq[i];
203664f89611Schristos 
203764f89611Schristos 		/* Accumulate IQ calibration measures (clear on read). */
203864f89611Schristos 		cal->pwr_meas_i += AR_READ(sc, AR_PHY_CAL_MEAS_0(i));
203964f89611Schristos 		cal->pwr_meas_q += AR_READ(sc, AR_PHY_CAL_MEAS_1(i));
204064f89611Schristos 		cal->iq_corr_meas +=
204164f89611Schristos 		    (int32_t)AR_READ(sc, AR_PHY_CAL_MEAS_2(i));
204264f89611Schristos 	}
204364f89611Schristos 	if (!AR_SREV_9280_10_OR_LATER(sc) &&
204464f89611Schristos 	    ++sc->sc_calib.nsamples < AR_CAL_SAMPLES) {
204564f89611Schristos 		/* Not enough samples accumulated, continue. */
204664f89611Schristos 		ar5008_do_calib(sc);
204764f89611Schristos 		return;
204864f89611Schristos 	}
204964f89611Schristos 
205064f89611Schristos 	for (i = 0; i < sc->sc_nrxchains; i++) {
205164f89611Schristos 		cal = &sc->sc_calib.iq[i];
205264f89611Schristos 
205364f89611Schristos 		if (cal->pwr_meas_q == 0)
205464f89611Schristos 			continue;
205564f89611Schristos 
20564b1394afSchristos 		if ((iq_corr_neg = cal->iq_corr_meas) < 0)
205764f89611Schristos 			cal->iq_corr_meas = -cal->iq_corr_meas;
205864f89611Schristos 
205964f89611Schristos 		i_coff_denom =
206064f89611Schristos 		    (cal->pwr_meas_i / 2 + cal->pwr_meas_q / 2) / 128;
206164f89611Schristos 		q_coff_denom = cal->pwr_meas_q / 64;
206264f89611Schristos 
206364f89611Schristos 		if (i_coff_denom == 0 || q_coff_denom == 0)
206464f89611Schristos 			continue;	/* Prevents division by zero. */
206564f89611Schristos 
206664f89611Schristos 		i_coff = cal->iq_corr_meas / i_coff_denom;
206764f89611Schristos 		q_coff = (cal->pwr_meas_i / q_coff_denom) - 64;
206864f89611Schristos 
206964f89611Schristos 		/* Negate i_coff if iq_corr_meas is positive. */
207064f89611Schristos 		if (!iq_corr_neg)
207164f89611Schristos 			i_coff = 0x40 - (i_coff & 0x3f);
207264f89611Schristos 		if (q_coff > 15)
207364f89611Schristos 			q_coff = 15;
207464f89611Schristos 		else if (q_coff <= -16)
207564f89611Schristos 			q_coff = -16;	/* XXX Linux has a bug here? */
207664f89611Schristos 
207764f89611Schristos 		DPRINTFN(DBG_RF, sc, "IQ calibration for chain %d\n", i);
207864f89611Schristos 		reg = AR_READ(sc, AR_PHY_TIMING_CTRL4(i));
207964f89611Schristos 		reg = RW(reg, AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF, i_coff);
208064f89611Schristos 		reg = RW(reg, AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF, q_coff);
208164f89611Schristos 		AR_WRITE(sc, AR_PHY_TIMING_CTRL4(i), reg);
208264f89611Schristos 	}
208364f89611Schristos 
208464f89611Schristos 	/* Apply new settings. */
208564f89611Schristos 	AR_SETBITS(sc, AR_PHY_TIMING_CTRL4_0,
208664f89611Schristos 	    AR_PHY_TIMING_CTRL4_IQCORR_ENABLE);
208764f89611Schristos 	AR_WRITE_BARRIER(sc);
208864f89611Schristos 
208964f89611Schristos 	/* IQ calibration done. */
209064f89611Schristos 	sc->sc_cur_calib_mask &= ~ATHN_CAL_IQ;
209164f89611Schristos 	memset(&sc->sc_calib, 0, sizeof(sc->sc_calib));
209264f89611Schristos }
209364f89611Schristos 
209464f89611Schristos Static void
ar5008_calib_adc_gain(struct athn_softc * sc)209564f89611Schristos ar5008_calib_adc_gain(struct athn_softc *sc)
209664f89611Schristos {
209764f89611Schristos 	struct athn_adc_cal *cal;
209864f89611Schristos 	uint32_t reg, gain_mismatch_i, gain_mismatch_q;
209964f89611Schristos 	int i;
210064f89611Schristos 
210164f89611Schristos 	for (i = 0; i < AR_MAX_CHAINS; i++) {
210264f89611Schristos 		cal = &sc->sc_calib.adc_gain[i];
210364f89611Schristos 
210464f89611Schristos 		/* Accumulate ADC gain measures (clear on read). */
210564f89611Schristos 		cal->pwr_meas_odd_i  += AR_READ(sc, AR_PHY_CAL_MEAS_0(i));
210664f89611Schristos 		cal->pwr_meas_even_i += AR_READ(sc, AR_PHY_CAL_MEAS_1(i));
210764f89611Schristos 		cal->pwr_meas_odd_q  += AR_READ(sc, AR_PHY_CAL_MEAS_2(i));
210864f89611Schristos 		cal->pwr_meas_even_q += AR_READ(sc, AR_PHY_CAL_MEAS_3(i));
210964f89611Schristos 	}
211064f89611Schristos 	if (!AR_SREV_9280_10_OR_LATER(sc) &&
211164f89611Schristos 	    ++sc->sc_calib.nsamples < AR_CAL_SAMPLES) {
211264f89611Schristos 		/* Not enough samples accumulated, continue. */
211364f89611Schristos 		ar5008_do_calib(sc);
211464f89611Schristos 		return;
211564f89611Schristos 	}
211664f89611Schristos 
211764f89611Schristos 	for (i = 0; i < sc->sc_nrxchains; i++) {
211864f89611Schristos 		cal = &sc->sc_calib.adc_gain[i];
211964f89611Schristos 
212064f89611Schristos 		if (cal->pwr_meas_odd_i == 0 || cal->pwr_meas_even_q == 0)
212164f89611Schristos 			continue;	/* Prevents division by zero. */
212264f89611Schristos 
212364f89611Schristos 		gain_mismatch_i =
212464f89611Schristos 		    (cal->pwr_meas_even_i * 32) / cal->pwr_meas_odd_i;
212564f89611Schristos 		gain_mismatch_q =
212664f89611Schristos 		    (cal->pwr_meas_odd_q * 32) / cal->pwr_meas_even_q;
212764f89611Schristos 
212864f89611Schristos 		DPRINTFN(DBG_RF, sc, "ADC gain calibration for chain %d\n", i);
212964f89611Schristos 		reg = AR_READ(sc, AR_PHY_NEW_ADC_DC_GAIN_CORR(i));
213064f89611Schristos 		reg = RW(reg, AR_PHY_NEW_ADC_DC_GAIN_IGAIN, gain_mismatch_i);
213164f89611Schristos 		reg = RW(reg, AR_PHY_NEW_ADC_DC_GAIN_QGAIN, gain_mismatch_q);
213264f89611Schristos 		AR_WRITE(sc, AR_PHY_NEW_ADC_DC_GAIN_CORR(i), reg);
213364f89611Schristos 	}
213464f89611Schristos 
213564f89611Schristos 	/* Apply new settings. */
213664f89611Schristos 	AR_SETBITS(sc, AR_PHY_NEW_ADC_DC_GAIN_CORR(0),
213764f89611Schristos 	    AR_PHY_NEW_ADC_GAIN_CORR_ENABLE);
213864f89611Schristos 	AR_WRITE_BARRIER(sc);
213964f89611Schristos 
214064f89611Schristos 	/* ADC gain calibration done. */
214164f89611Schristos 	sc->sc_cur_calib_mask &= ~ATHN_CAL_ADC_GAIN;
214264f89611Schristos 	memset(&sc->sc_calib, 0, sizeof(sc->sc_calib));
214364f89611Schristos }
214464f89611Schristos 
214564f89611Schristos Static void
ar5008_calib_adc_dc_off(struct athn_softc * sc)214664f89611Schristos ar5008_calib_adc_dc_off(struct athn_softc *sc)
214764f89611Schristos {
214864f89611Schristos 	struct athn_adc_cal *cal;
214964f89611Schristos 	int32_t dc_offset_mismatch_i, dc_offset_mismatch_q;
215064f89611Schristos 	uint32_t reg;
215164f89611Schristos 	int count, i;
215264f89611Schristos 
215364f89611Schristos 	for (i = 0; i < AR_MAX_CHAINS; i++) {
215464f89611Schristos 		cal = &sc->sc_calib.adc_dc_offset[i];
215564f89611Schristos 
215664f89611Schristos 		/* Accumulate ADC DC offset measures (clear on read). */
215764f89611Schristos 		cal->pwr_meas_odd_i  += AR_READ(sc, AR_PHY_CAL_MEAS_0(i));
215864f89611Schristos 		cal->pwr_meas_even_i += AR_READ(sc, AR_PHY_CAL_MEAS_1(i));
215964f89611Schristos 		cal->pwr_meas_odd_q  += AR_READ(sc, AR_PHY_CAL_MEAS_2(i));
216064f89611Schristos 		cal->pwr_meas_even_q += AR_READ(sc, AR_PHY_CAL_MEAS_3(i));
216164f89611Schristos 	}
216264f89611Schristos 	if (!AR_SREV_9280_10_OR_LATER(sc) &&
216364f89611Schristos 	    ++sc->sc_calib.nsamples < AR_CAL_SAMPLES) {
216464f89611Schristos 		/* Not enough samples accumulated, continue. */
216564f89611Schristos 		ar5008_do_calib(sc);
216664f89611Schristos 		return;
216764f89611Schristos 	}
216864f89611Schristos 
216964f89611Schristos 	if (AR_SREV_9280_10_OR_LATER(sc))
217064f89611Schristos 		count = (1 << (10 + 5));
217164f89611Schristos 	else
217264f89611Schristos 		count = (1 << ( 2 + 5)) * AR_CAL_SAMPLES;
217364f89611Schristos 	for (i = 0; i < sc->sc_nrxchains; i++) {
217464f89611Schristos 		cal = &sc->sc_calib.adc_dc_offset[i];
217564f89611Schristos 
217664f89611Schristos 		dc_offset_mismatch_i =
217764f89611Schristos 		    (cal->pwr_meas_even_i - cal->pwr_meas_odd_i * 2) / count;
217864f89611Schristos 		dc_offset_mismatch_q =
217964f89611Schristos 		    (cal->pwr_meas_odd_q - cal->pwr_meas_even_q * 2) / count;
218064f89611Schristos 
218164f89611Schristos 		DPRINTFN(DBG_RF, sc, "ADC DC offset calibration for chain %d\n", i);
218264f89611Schristos 		reg = AR_READ(sc, AR_PHY_NEW_ADC_DC_GAIN_CORR(i));
218364f89611Schristos 		reg = RW(reg, AR_PHY_NEW_ADC_DC_GAIN_QDC,
218464f89611Schristos 		    dc_offset_mismatch_q);
218564f89611Schristos 		reg = RW(reg, AR_PHY_NEW_ADC_DC_GAIN_IDC,
218664f89611Schristos 		    dc_offset_mismatch_i);
218764f89611Schristos 		AR_WRITE(sc, AR_PHY_NEW_ADC_DC_GAIN_CORR(i), reg);
218864f89611Schristos 	}
218964f89611Schristos 
219064f89611Schristos 	/* Apply new settings. */
219164f89611Schristos 	AR_SETBITS(sc, AR_PHY_NEW_ADC_DC_GAIN_CORR(0),
219264f89611Schristos 	    AR_PHY_NEW_ADC_DC_OFFSET_CORR_ENABLE);
219364f89611Schristos 	AR_WRITE_BARRIER(sc);
219464f89611Schristos 
219564f89611Schristos 	/* ADC DC offset calibration done. */
219664f89611Schristos 	sc->sc_cur_calib_mask &= ~ATHN_CAL_ADC_DC;
219764f89611Schristos 	memset(&sc->sc_calib, 0, sizeof(sc->sc_calib));
219864f89611Schristos }
219964f89611Schristos 
220064f89611Schristos PUBLIC void
ar5008_write_txpower(struct athn_softc * sc,int16_t power[ATHN_POWER_COUNT])220164f89611Schristos ar5008_write_txpower(struct athn_softc *sc, int16_t power[ATHN_POWER_COUNT])
220264f89611Schristos {
220364f89611Schristos 
220464f89611Schristos 	AR_WRITE(sc, AR_PHY_POWER_TX_RATE1,
220564f89611Schristos 	    (power[ATHN_POWER_OFDM18  ] & 0x3f) << 24 |
220664f89611Schristos 	    (power[ATHN_POWER_OFDM12  ] & 0x3f) << 16 |
220764f89611Schristos 	    (power[ATHN_POWER_OFDM9   ] & 0x3f) <<  8 |
220864f89611Schristos 	    (power[ATHN_POWER_OFDM6   ] & 0x3f));
220964f89611Schristos 	AR_WRITE(sc, AR_PHY_POWER_TX_RATE2,
221064f89611Schristos 	    (power[ATHN_POWER_OFDM54  ] & 0x3f) << 24 |
221164f89611Schristos 	    (power[ATHN_POWER_OFDM48  ] & 0x3f) << 16 |
221264f89611Schristos 	    (power[ATHN_POWER_OFDM36  ] & 0x3f) <<  8 |
221364f89611Schristos 	    (power[ATHN_POWER_OFDM24  ] & 0x3f));
221464f89611Schristos 	AR_WRITE(sc, AR_PHY_POWER_TX_RATE3,
221564f89611Schristos 	    (power[ATHN_POWER_CCK2_SP ] & 0x3f) << 24 |
221664f89611Schristos 	    (power[ATHN_POWER_CCK2_LP ] & 0x3f) << 16 |
221764f89611Schristos 	    (power[ATHN_POWER_XR      ] & 0x3f) <<  8 |
221864f89611Schristos 	    (power[ATHN_POWER_CCK1_LP ] & 0x3f));
221964f89611Schristos 	AR_WRITE(sc, AR_PHY_POWER_TX_RATE4,
222064f89611Schristos 	    (power[ATHN_POWER_CCK11_SP] & 0x3f) << 24 |
222164f89611Schristos 	    (power[ATHN_POWER_CCK11_LP] & 0x3f) << 16 |
222264f89611Schristos 	    (power[ATHN_POWER_CCK55_SP] & 0x3f) <<  8 |
222364f89611Schristos 	    (power[ATHN_POWER_CCK55_LP] & 0x3f));
222464f89611Schristos #ifndef IEEE80211_NO_HT
222564f89611Schristos 	AR_WRITE(sc, AR_PHY_POWER_TX_RATE5,
222664f89611Schristos 	    (power[ATHN_POWER_HT20(3) ] & 0x3f) << 24 |
222764f89611Schristos 	    (power[ATHN_POWER_HT20(2) ] & 0x3f) << 16 |
222864f89611Schristos 	    (power[ATHN_POWER_HT20(1) ] & 0x3f) <<  8 |
222964f89611Schristos 	    (power[ATHN_POWER_HT20(0) ] & 0x3f));
223064f89611Schristos 	AR_WRITE(sc, AR_PHY_POWER_TX_RATE6,
223164f89611Schristos 	    (power[ATHN_POWER_HT20(7) ] & 0x3f) << 24 |
223264f89611Schristos 	    (power[ATHN_POWER_HT20(6) ] & 0x3f) << 16 |
223364f89611Schristos 	    (power[ATHN_POWER_HT20(5) ] & 0x3f) <<  8 |
223464f89611Schristos 	    (power[ATHN_POWER_HT20(4) ] & 0x3f));
223564f89611Schristos 	AR_WRITE(sc, AR_PHY_POWER_TX_RATE7,
223664f89611Schristos 	    (power[ATHN_POWER_HT40(3) ] & 0x3f) << 24 |
223764f89611Schristos 	    (power[ATHN_POWER_HT40(2) ] & 0x3f) << 16 |
223864f89611Schristos 	    (power[ATHN_POWER_HT40(1) ] & 0x3f) <<  8 |
223964f89611Schristos 	    (power[ATHN_POWER_HT40(0) ] & 0x3f));
224064f89611Schristos 	AR_WRITE(sc, AR_PHY_POWER_TX_RATE8,
224164f89611Schristos 	    (power[ATHN_POWER_HT40(7) ] & 0x3f) << 24 |
224264f89611Schristos 	    (power[ATHN_POWER_HT40(6) ] & 0x3f) << 16 |
224364f89611Schristos 	    (power[ATHN_POWER_HT40(5) ] & 0x3f) <<  8 |
224464f89611Schristos 	    (power[ATHN_POWER_HT40(4) ] & 0x3f));
224564f89611Schristos 	AR_WRITE(sc, AR_PHY_POWER_TX_RATE9,
224664f89611Schristos 	    (power[ATHN_POWER_OFDM_EXT] & 0x3f) << 24 |
224764f89611Schristos 	    (power[ATHN_POWER_CCK_EXT ] & 0x3f) << 16 |
224864f89611Schristos 	    (power[ATHN_POWER_OFDM_DUP] & 0x3f) <<  8 |
224964f89611Schristos 	    (power[ATHN_POWER_CCK_DUP ] & 0x3f));
225064f89611Schristos #endif
225164f89611Schristos 	AR_WRITE_BARRIER(sc);
225264f89611Schristos }
225364f89611Schristos 
225464f89611Schristos PUBLIC void
ar5008_set_viterbi_mask(struct athn_softc * sc,int bin)225564f89611Schristos ar5008_set_viterbi_mask(struct athn_softc *sc, int bin)
225664f89611Schristos {
225764f89611Schristos 	uint32_t mask[4], reg;
225864f89611Schristos 	uint8_t m[62], p[62];	/* XXX use bit arrays? */
225964f89611Schristos 	int i, bit, cur;
226064f89611Schristos 
226164f89611Schristos 	/* Compute pilot mask. */
226264f89611Schristos 	cur = -6000;
226364f89611Schristos 	for (i = 0; i < 4; i++) {
226464f89611Schristos 		mask[i] = 0;
226564f89611Schristos 		for (bit = 0; bit < 30; bit++) {
226664f89611Schristos 			if (abs(cur - bin) < 100)
226764f89611Schristos 				mask[i] |= 1 << bit;
226864f89611Schristos 			cur += 100;
226964f89611Schristos 		}
227064f89611Schristos 		if (cur == 0)	/* Skip entry "0". */
227164f89611Schristos 			cur = 100;
227264f89611Schristos 	}
227364f89611Schristos 	/* Write entries from -6000 to -3100. */
227464f89611Schristos 	AR_WRITE(sc, AR_PHY_TIMING7, mask[0]);
227564f89611Schristos 	AR_WRITE(sc, AR_PHY_TIMING9, mask[0]);
227664f89611Schristos 	/* Write entries from -3000 to -100. */
227764f89611Schristos 	AR_WRITE(sc, AR_PHY_TIMING8, mask[1]);
227864f89611Schristos 	AR_WRITE(sc, AR_PHY_TIMING10, mask[1]);
227964f89611Schristos 	/* Write entries from 100 to 3000. */
228064f89611Schristos 	AR_WRITE(sc, AR_PHY_PILOT_MASK_01_30, mask[2]);
228164f89611Schristos 	AR_WRITE(sc, AR_PHY_CHANNEL_MASK_01_30, mask[2]);
228264f89611Schristos 	/* Write entries from 3100 to 6000. */
228364f89611Schristos 	AR_WRITE(sc, AR_PHY_PILOT_MASK_31_60, mask[3]);
228464f89611Schristos 	AR_WRITE(sc, AR_PHY_CHANNEL_MASK_31_60, mask[3]);
228564f89611Schristos 
228664f89611Schristos 	/* Compute viterbi mask. */
228764f89611Schristos 	for (cur = 6100; cur >= 0; cur -= 100)
228864f89611Schristos 		p[+cur / 100] = abs(cur - bin) < 75;
2289f09faf1bSmrg 	for (cur = 0; cur >= -6100; cur -= 100)
229064f89611Schristos 		m[-cur / 100] = abs(cur - bin) < 75;
229164f89611Schristos 
229264f89611Schristos 	/* Write viterbi mask (XXX needs to be reworked). */
229364f89611Schristos 	reg =
229464f89611Schristos 	    m[46] << 30 | m[47] << 28 | m[48] << 26 | m[49] << 24 |
229564f89611Schristos 	    m[50] << 22 | m[51] << 20 | m[52] << 18 | m[53] << 16 |
229664f89611Schristos 	    m[54] << 14 | m[55] << 12 | m[56] << 10 | m[57] <<  8 |
229764f89611Schristos 	    m[58] <<  6 | m[59] <<  4 | m[60] <<  2 | m[61] <<  0;
229864f89611Schristos 	AR_WRITE(sc, AR_PHY_BIN_MASK_1, reg);
229964f89611Schristos 	AR_WRITE(sc, AR_PHY_VIT_MASK2_M_46_61, reg);
230064f89611Schristos 
230164f89611Schristos 	/* XXX m[48] should be m[38] ? */
230264f89611Schristos 	reg =             m[31] << 28 | m[32] << 26 | m[33] << 24 |
230364f89611Schristos 	    m[34] << 22 | m[35] << 20 | m[36] << 18 | m[37] << 16 |
230464f89611Schristos 	    m[48] << 14 | m[39] << 12 | m[40] << 10 | m[41] <<  8 |
230564f89611Schristos 	    m[42] <<  6 | m[43] <<  4 | m[44] <<  2 | m[45] <<  0;
230664f89611Schristos 	AR_WRITE(sc, AR_PHY_BIN_MASK_2, reg);
230764f89611Schristos 	AR_WRITE(sc, AR_PHY_VIT_MASK2_M_31_45, reg);
230864f89611Schristos 
230964f89611Schristos 	/* XXX This one is weird too. */
231064f89611Schristos 	reg =
231164f89611Schristos 	    m[16] << 30 | m[16] << 28 | m[18] << 26 | m[18] << 24 |
231264f89611Schristos 	    m[20] << 22 | m[20] << 20 | m[22] << 18 | m[22] << 16 |
231364f89611Schristos 	    m[24] << 14 | m[24] << 12 | m[25] << 10 | m[26] <<  8 |
231464f89611Schristos 	    m[27] <<  6 | m[28] <<  4 | m[29] <<  2 | m[30] <<  0;
231564f89611Schristos 	AR_WRITE(sc, AR_PHY_BIN_MASK_3, reg);
231664f89611Schristos 	AR_WRITE(sc, AR_PHY_VIT_MASK2_M_16_30, reg);
231764f89611Schristos 
231864f89611Schristos 	reg =
231964f89611Schristos 	    m[ 0] << 30 | m[ 1] << 28 | m[ 2] << 26 | m[ 3] << 24 |
232064f89611Schristos 	    m[ 4] << 22 | m[ 5] << 20 | m[ 6] << 18 | m[ 7] << 16 |
232164f89611Schristos 	    m[ 8] << 14 | m[ 9] << 12 | m[10] << 10 | m[11] <<  8 |
232264f89611Schristos 	    m[12] <<  6 | m[13] <<  4 | m[14] <<  2 | m[15] <<  0;
232364f89611Schristos 	AR_WRITE(sc, AR_PHY_MASK_CTL, reg);
232464f89611Schristos 	AR_WRITE(sc, AR_PHY_VIT_MASK2_M_00_15, reg);
232564f89611Schristos 
232664f89611Schristos 	reg =             p[15] << 28 | p[14] << 26 | p[13] << 24 |
232764f89611Schristos 	    p[12] << 22 | p[11] << 20 | p[10] << 18 | p[ 9] << 16 |
232864f89611Schristos 	    p[ 8] << 14 | p[ 7] << 12 | p[ 6] << 10 | p[ 5] <<  8 |
232964f89611Schristos 	    p[ 4] <<  6 | p[ 3] <<  4 | p[ 2] <<  2 | p[ 1] <<  0;
233064f89611Schristos 	AR_WRITE(sc, AR_PHY_BIN_MASK2_1, reg);
233164f89611Schristos 	AR_WRITE(sc, AR_PHY_VIT_MASK2_P_15_01, reg);
233264f89611Schristos 
233364f89611Schristos 	reg =             p[30] << 28 | p[29] << 26 | p[28] << 24 |
233464f89611Schristos 	    p[27] << 22 | p[26] << 20 | p[25] << 18 | p[24] << 16 |
233564f89611Schristos 	    p[23] << 14 | p[22] << 12 | p[21] << 10 | p[20] <<  8 |
233664f89611Schristos 	    p[19] <<  6 | p[18] <<  4 | p[17] <<  2 | p[16] <<  0;
233764f89611Schristos 	AR_WRITE(sc, AR_PHY_BIN_MASK2_2, reg);
233864f89611Schristos 	AR_WRITE(sc, AR_PHY_VIT_MASK2_P_30_16, reg);
233964f89611Schristos 
234064f89611Schristos 	reg =             p[45] << 28 | p[44] << 26 | p[43] << 24 |
234164f89611Schristos 	    p[42] << 22 | p[41] << 20 | p[40] << 18 | p[39] << 16 |
234264f89611Schristos 	    p[38] << 14 | p[37] << 12 | p[36] << 10 | p[35] <<  8 |
234364f89611Schristos 	    p[34] <<  6 | p[33] <<  4 | p[32] <<  2 | p[31] <<  0;
234464f89611Schristos 	AR_WRITE(sc, AR_PHY_BIN_MASK2_3, reg);
234564f89611Schristos 	AR_WRITE(sc, AR_PHY_VIT_MASK2_P_45_31, reg);
234664f89611Schristos 
234764f89611Schristos 	reg =
234864f89611Schristos 	    p[61] << 30 | p[60] << 28 | p[59] << 26 | p[58] << 24 |
234964f89611Schristos 	    p[57] << 22 | p[56] << 20 | p[55] << 18 | p[54] << 16 |
235064f89611Schristos 	    p[53] << 14 | p[52] << 12 | p[51] << 10 | p[50] <<  8 |
235164f89611Schristos 	    p[49] <<  6 | p[48] <<  4 | p[47] <<  2 | p[46] <<  0;
235264f89611Schristos 	AR_WRITE(sc, AR_PHY_BIN_MASK2_4, reg);
235364f89611Schristos 	AR_WRITE(sc, AR_PHY_VIT_MASK2_P_61_46, reg);
235464f89611Schristos 	AR_WRITE_BARRIER(sc);
235564f89611Schristos }
235664f89611Schristos 
235764f89611Schristos Static void
ar5008_hw_init(struct athn_softc * sc,struct ieee80211_channel * c,struct ieee80211_channel * extc)235864f89611Schristos ar5008_hw_init(struct athn_softc *sc, struct ieee80211_channel *c,
235964f89611Schristos     struct ieee80211_channel *extc)
236064f89611Schristos {
236164f89611Schristos 	struct athn_ops *ops = &sc->sc_ops;
236264f89611Schristos 	const struct athn_ini *ini = sc->sc_ini;
236364f89611Schristos 	const uint32_t *pvals;
236464f89611Schristos 	uint32_t reg;
236564f89611Schristos 	int i;
236664f89611Schristos 
236764f89611Schristos 	AR_WRITE(sc, AR_PHY(0), 0x00000007);
236864f89611Schristos 	AR_WRITE(sc, AR_PHY_ADC_SERIAL_CTL, AR_PHY_SEL_EXTERNAL_RADIO);
236964f89611Schristos 
237064f89611Schristos 	if (!AR_SINGLE_CHIP(sc))
237164f89611Schristos 		ar5416_reset_addac(sc, c);
237264f89611Schristos 
237364f89611Schristos 	AR_WRITE(sc, AR_PHY_ADC_SERIAL_CTL, AR_PHY_SEL_INTERNAL_ADDAC);
237464f89611Schristos 
237564f89611Schristos 	/* First initialization step (depends on channel band/bandwidth). */
237664f89611Schristos #ifndef IEEE80211_NO_HT
237764f89611Schristos 	if (extc != NULL) {
237864f89611Schristos 		if (IEEE80211_IS_CHAN_2GHZ(c))
237964f89611Schristos 			pvals = ini->vals_2g40;
238064f89611Schristos 		else
238164f89611Schristos 			pvals = ini->vals_5g40;
238264f89611Schristos 	}
238364f89611Schristos 	else
238464f89611Schristos #endif
238564f89611Schristos 	{
238664f89611Schristos 		if (IEEE80211_IS_CHAN_2GHZ(c))
238764f89611Schristos 			pvals = ini->vals_2g20;
238864f89611Schristos 		else
238964f89611Schristos 			pvals = ini->vals_5g20;
239064f89611Schristos 	}
239164f89611Schristos 	DPRINTFN(DBG_INIT, sc, "writing modal init vals\n");
239264f89611Schristos 	for (i = 0; i < ini->nregs; i++) {
239364f89611Schristos 		uint32_t val = pvals[i];
239464f89611Schristos 
239564f89611Schristos 		/* Fix AR_AN_TOP2 initialization value if required. */
239664f89611Schristos 		if (ini->regs[i] == AR_AN_TOP2 &&
239764f89611Schristos 		    (sc->sc_flags & ATHN_FLAG_AN_TOP2_FIXUP))
239864f89611Schristos 			val &= ~AR_AN_TOP2_PWDCLKIND;
239964f89611Schristos 		AR_WRITE(sc, ini->regs[i], val);
240064f89611Schristos 		if (AR_IS_ANALOG_REG(ini->regs[i])) {
240164f89611Schristos 			AR_WRITE_BARRIER(sc);
240264f89611Schristos 			DELAY(100);
240364f89611Schristos 		}
240464f89611Schristos 		if ((i & 0x1f) == 0)
240564f89611Schristos 			DELAY(1);
240664f89611Schristos 	}
240764f89611Schristos 	AR_WRITE_BARRIER(sc);
240864f89611Schristos 
240964f89611Schristos 	if (sc->sc_rx_gain != NULL)
241064f89611Schristos 		ar9280_reset_rx_gain(sc, c);
241164f89611Schristos 	if (sc->sc_tx_gain != NULL)
241264f89611Schristos 		ar9280_reset_tx_gain(sc, c);
241364f89611Schristos 
241464f89611Schristos 	if (AR_SREV_9271_10(sc)) {
241564f89611Schristos 		AR_WRITE(sc, AR_PHY(68), 0x30002311);
241664f89611Schristos 		AR_WRITE(sc, AR_PHY_RF_CTL3, 0x0a020001);
241764f89611Schristos 	}
241864f89611Schristos 	AR_WRITE_BARRIER(sc);
241964f89611Schristos 
242064f89611Schristos 	/* Second initialization step (common to all channels). */
242164f89611Schristos 	DPRINTFN(DBG_INIT, sc, "writing common init vals\n");
242264f89611Schristos 	for (i = 0; i < ini->ncmregs; i++) {
242364f89611Schristos 		AR_WRITE(sc, ini->cmregs[i], ini->cmvals[i]);
242464f89611Schristos 		if (AR_IS_ANALOG_REG(ini->cmregs[i])) {
242564f89611Schristos 			AR_WRITE_BARRIER(sc);
242664f89611Schristos 			DELAY(100);
242764f89611Schristos 		}
242864f89611Schristos 		if ((i & 0x1f) == 0)
242964f89611Schristos 			DELAY(1);
243064f89611Schristos 	}
243164f89611Schristos 	AR_WRITE_BARRIER(sc);
243264f89611Schristos 
243364f89611Schristos 	if (!AR_SINGLE_CHIP(sc))
243464f89611Schristos 		ar5416_reset_bb_gain(sc, c);
243564f89611Schristos 
243664f89611Schristos 	if (IEEE80211_IS_CHAN_5GHZ(c) &&
243764f89611Schristos 	    (sc->sc_flags & ATHN_FLAG_FAST_PLL_CLOCK)) {
243864f89611Schristos 		/* Update modal values for fast PLL clock. */
243964f89611Schristos #ifndef IEEE80211_NO_HT
244064f89611Schristos 		if (extc != NULL)
244164f89611Schristos 			pvals = ini->fastvals_5g40;
244264f89611Schristos 		else
244364f89611Schristos #endif
244464f89611Schristos 			pvals = ini->fastvals_5g20;
244564f89611Schristos 		DPRINTFN(DBG_INIT, sc, "writing fast pll clock init vals\n");
244664f89611Schristos 		for (i = 0; i < ini->nfastregs; i++) {
244764f89611Schristos 			AR_WRITE(sc, ini->fastregs[i], pvals[i]);
244864f89611Schristos 			if (AR_IS_ANALOG_REG(ini->fastregs[i])) {
244964f89611Schristos 				AR_WRITE_BARRIER(sc);
245064f89611Schristos 				DELAY(100);
245164f89611Schristos 			}
245264f89611Schristos 			if ((i & 0x1f) == 0)
245364f89611Schristos 				DELAY(1);
245464f89611Schristos 		}
245564f89611Schristos 	}
245664f89611Schristos 
245764f89611Schristos 	/*
245864f89611Schristos 	 * Set the RX_ABORT and RX_DIS bits to prevent frames with corrupted
245964f89611Schristos 	 * descriptor status.
246064f89611Schristos 	 */
246164f89611Schristos 	AR_SETBITS(sc, AR_DIAG_SW, AR_DIAG_RX_DIS | AR_DIAG_RX_ABORT);
246264f89611Schristos 
246364f89611Schristos 	/* Hardware workarounds for occasional Rx data corruption. */
246464f89611Schristos 	if (AR_SREV_9280_10_OR_LATER(sc)) {
246564f89611Schristos 		reg = AR_READ(sc, AR_PCU_MISC_MODE2);
246664f89611Schristos 		if (!AR_SREV_9271(sc))
246764f89611Schristos 			reg &= ~AR_PCU_MISC_MODE2_HWWAR1;
246864f89611Schristos 		if (AR_SREV_9287_10_OR_LATER(sc))
246964f89611Schristos 			reg &= ~AR_PCU_MISC_MODE2_HWWAR2;
247064f89611Schristos 		AR_WRITE(sc, AR_PCU_MISC_MODE2, reg);
247164f89611Schristos 
247264f89611Schristos 	}
247364f89611Schristos 	else if (AR_SREV_5416_20_OR_LATER(sc)) {
247464f89611Schristos 		/* Disable baseband clock gating. */
247564f89611Schristos 		AR_WRITE(sc, AR_PHY(651), 0x11);
247664f89611Schristos 
247764f89611Schristos 		if (AR_SREV_9160(sc)) {
247864f89611Schristos 			/* Disable RIFS search to fix baseband hang. */
247964f89611Schristos 			AR_CLRBITS(sc, AR_PHY_HEAVY_CLIP_FACTOR_RIFS,
248064f89611Schristos 			    AR_PHY_RIFS_INIT_DELAY_M);
248164f89611Schristos 		}
248264f89611Schristos 	}
248364f89611Schristos 	AR_WRITE_BARRIER(sc);
248464f89611Schristos 
248564f89611Schristos 	ar5008_set_phy(sc, c, extc);
248664f89611Schristos 	ar5008_init_chains(sc);
248764f89611Schristos 
248864f89611Schristos 	if (sc->sc_flags & ATHN_FLAG_OLPC) {
248964f89611Schristos 		sc->sc_olpc_ticks = ticks;
249064f89611Schristos 		ops->olpc_init(sc);
249164f89611Schristos 	}
249264f89611Schristos 
249364f89611Schristos 	ops->set_txpower(sc, c, extc);
249464f89611Schristos 
249564f89611Schristos 	if (!AR_SINGLE_CHIP(sc))
249664f89611Schristos 		ar5416_rf_reset(sc, c);
249764f89611Schristos }
249864f89611Schristos 
249964f89611Schristos Static uint8_t
ar5008_get_vpd(uint8_t pwr,const uint8_t * pwrPdg,const uint8_t * vpdPdg,int nicepts)250064f89611Schristos ar5008_get_vpd(uint8_t pwr, const uint8_t *pwrPdg, const uint8_t *vpdPdg,
250164f89611Schristos     int nicepts)
250264f89611Schristos {
250364f89611Schristos 	uint8_t vpd;
250464f89611Schristos 	int i, lo, hi;
250564f89611Schristos 
250664f89611Schristos 	for (i = 0; i < nicepts; i++)
250764f89611Schristos 		if (pwrPdg[i] > pwr)
250864f89611Schristos 			break;
250964f89611Schristos 	hi = i;
251064f89611Schristos 	lo = hi - 1;
251164f89611Schristos 	if (lo == -1)
251264f89611Schristos 		lo = hi;
251364f89611Schristos 	else if (hi == nicepts)
251464f89611Schristos 		hi = lo;
251564f89611Schristos 
251664f89611Schristos 	vpd = athn_interpolate(pwr, pwrPdg[lo], vpdPdg[lo],
251764f89611Schristos 	    pwrPdg[hi], vpdPdg[hi]);
251864f89611Schristos 	return vpd;
251964f89611Schristos }
252064f89611Schristos 
252164f89611Schristos PUBLIC void
ar5008_get_pdadcs(struct athn_softc * sc,uint8_t fbin,struct athn_pier * lopier,struct athn_pier * hipier,int nxpdgains,int nicepts,uint8_t overlap,uint8_t * boundaries,uint8_t * pdadcs)252264f89611Schristos ar5008_get_pdadcs(struct athn_softc *sc, uint8_t fbin,
252364f89611Schristos     struct athn_pier *lopier, struct athn_pier *hipier, int nxpdgains,
252464f89611Schristos     int nicepts, uint8_t overlap, uint8_t *boundaries, uint8_t *pdadcs)
252564f89611Schristos {
252664f89611Schristos #define DB(x)	((x) / 2)	/* Convert half dB to dB. */
252764f89611Schristos 	uint8_t minpwr[AR_PD_GAINS_IN_MASK], maxpwr[AR_PD_GAINS_IN_MASK];
252864f89611Schristos 	uint8_t vpd[AR_MAX_PWR_RANGE_IN_HALF_DB], pwr;
252964f89611Schristos 	uint8_t lovpd, hivpd, boundary;
253064f89611Schristos 	int16_t ss, delta, vpdstep, val;
253164f89611Schristos 	int i, j, npdadcs, nvpds, maxidx, tgtidx;
253264f89611Schristos 
253364f89611Schristos 	/* Compute min and max power in half dB for each pdGain. */
253464f89611Schristos 	for (i = 0; i < nxpdgains; i++) {
253564f89611Schristos 		minpwr[i] = MAX(lopier->pwr[i][0], hipier->pwr[i][0]);
253664f89611Schristos 		maxpwr[i] = MIN(lopier->pwr[i][nicepts - 1],
253764f89611Schristos 		    hipier->pwr[i][nicepts - 1]);
253864f89611Schristos 	}
253964f89611Schristos 
254064f89611Schristos 	/* Fill phase domain analog-to-digital converter (PDADC) table. */
254164f89611Schristos 	npdadcs = 0;
254264f89611Schristos 	for (i = 0; i < nxpdgains; i++) {
254364f89611Schristos 		if (i != nxpdgains - 1)
254464f89611Schristos 			boundaries[i] = DB(maxpwr[i] + minpwr[i + 1]) / 2;
254564f89611Schristos 		else
254664f89611Schristos 			boundaries[i] = DB(maxpwr[i]);
254764f89611Schristos 		if (boundaries[i] > AR_MAX_RATE_POWER)
254864f89611Schristos 			boundaries[i] = AR_MAX_RATE_POWER;
254964f89611Schristos 
255064f89611Schristos 		if (i == 0 && !AR_SREV_5416_20_OR_LATER(sc)) {
255164f89611Schristos 			/* Fix the gain delta (AR5416 1.0 only). */
255264f89611Schristos 			delta = boundaries[0] - 23;
255364f89611Schristos 			boundaries[0] = 23;
255464f89611Schristos 		}
255564f89611Schristos 		else
255664f89611Schristos 			delta = 0;
255764f89611Schristos 
255864f89611Schristos 		/* Find starting index for this pdGain. */
255964f89611Schristos 		if (i != 0) {
256064f89611Schristos 			ss = boundaries[i - 1] - DB(minpwr[i]) -
256164f89611Schristos 			    overlap + 1 + delta;
256264f89611Schristos 		}
256364f89611Schristos 		else if (AR_SREV_9280_10_OR_LATER(sc))
256464f89611Schristos 			ss = -DB(minpwr[i]);
256564f89611Schristos 		else
256664f89611Schristos 			ss = 0;
256764f89611Schristos 
256864f89611Schristos 		/* Compute Vpd table for this pdGain. */
256964f89611Schristos 		nvpds = DB(maxpwr[i] - minpwr[i]) + 1;
257064f89611Schristos 		memset(vpd, 0, sizeof(vpd));
257164f89611Schristos 		pwr = minpwr[i];
257264f89611Schristos 		for (j = 0; j < nvpds; j++) {
257364f89611Schristos 			/* Get lower and higher Vpd. */
257464f89611Schristos 			lovpd = ar5008_get_vpd(pwr, lopier->pwr[i],
257564f89611Schristos 			    lopier->vpd[i], nicepts);
257664f89611Schristos 			hivpd = ar5008_get_vpd(pwr, hipier->pwr[i],
257764f89611Schristos 			    hipier->vpd[i], nicepts);
257864f89611Schristos 
257964f89611Schristos 			/* Interpolate the final Vpd. */
258064f89611Schristos 			vpd[j] = athn_interpolate(fbin,
258164f89611Schristos 			    lopier->fbin, lovpd, hipier->fbin, hivpd);
258264f89611Schristos 
258364f89611Schristos 			pwr += 2;	/* In half dB. */
258464f89611Schristos 		}
258564f89611Schristos 
258664f89611Schristos 		/* Extrapolate data for ss < 0. */
258764f89611Schristos 		if (vpd[1] > vpd[0])
258864f89611Schristos 			vpdstep = vpd[1] - vpd[0];
258964f89611Schristos 		else
259064f89611Schristos 			vpdstep = 1;
259164f89611Schristos 		while (ss < 0 && npdadcs < AR_NUM_PDADC_VALUES - 1) {
259264f89611Schristos 			val = vpd[0] + ss * vpdstep;
259364f89611Schristos 			pdadcs[npdadcs++] = MAX(val, 0);
259464f89611Schristos 			ss++;
259564f89611Schristos 		}
259664f89611Schristos 
259764f89611Schristos 		tgtidx = boundaries[i] + overlap - DB(minpwr[i]);
259864f89611Schristos 		maxidx = MIN(tgtidx, nvpds);
259964f89611Schristos 		while (ss < maxidx && npdadcs < AR_NUM_PDADC_VALUES - 1)
260064f89611Schristos 			pdadcs[npdadcs++] = vpd[ss++];
260164f89611Schristos 
260264f89611Schristos 		if (tgtidx < maxidx)
260364f89611Schristos 			continue;
260464f89611Schristos 
260564f89611Schristos 		/* Extrapolate data for maxidx <= ss <= tgtidx. */
260664f89611Schristos 		if (vpd[nvpds - 1] > vpd[nvpds - 2])
260764f89611Schristos 			vpdstep = vpd[nvpds - 1] - vpd[nvpds - 2];
260864f89611Schristos 		else
260964f89611Schristos 			vpdstep = 1;
261064f89611Schristos 		while (ss <= tgtidx && npdadcs < AR_NUM_PDADC_VALUES - 1) {
261164f89611Schristos 			val = vpd[nvpds - 1] + (ss - maxidx + 1) * vpdstep;
261264f89611Schristos 			pdadcs[npdadcs++] = MIN(val, 255);
261364f89611Schristos 			ss++;
261464f89611Schristos 		}
261564f89611Schristos 	}
261664f89611Schristos 
261764f89611Schristos 	/* Fill remaining PDADC and boundaries entries. */
261864f89611Schristos 	if (AR_SREV_9285(sc))
261964f89611Schristos 		boundary = AR9285_PD_GAIN_BOUNDARY_DEFAULT;
262064f89611Schristos 	else	/* Fill with latest. */
262164f89611Schristos 		boundary = boundaries[nxpdgains - 1];
262264f89611Schristos 
262364f89611Schristos 	for (; nxpdgains < AR_PD_GAINS_IN_MASK; nxpdgains++)
262464f89611Schristos 		boundaries[nxpdgains] = boundary;
262564f89611Schristos 
262664f89611Schristos 	for (; npdadcs < AR_NUM_PDADC_VALUES; npdadcs++)
262764f89611Schristos 		pdadcs[npdadcs] = pdadcs[npdadcs - 1];
262864f89611Schristos #undef DB
262964f89611Schristos }
263064f89611Schristos 
263164f89611Schristos PUBLIC void
ar5008_get_lg_tpow(struct athn_softc * sc,struct ieee80211_channel * c,uint8_t ctl,const struct ar_cal_target_power_leg * tgt,int nchans,uint8_t tpow[4])263264f89611Schristos ar5008_get_lg_tpow(struct athn_softc *sc, struct ieee80211_channel *c,
263364f89611Schristos     uint8_t ctl, const struct ar_cal_target_power_leg *tgt, int nchans,
263464f89611Schristos     uint8_t tpow[4])
263564f89611Schristos {
263664f89611Schristos 	uint8_t fbin;
263764f89611Schristos 	int i, lo, hi;
263864f89611Schristos 
263964f89611Schristos 	/* Find interval (lower and upper indices). */
264064f89611Schristos 	fbin = athn_chan2fbin(c);
264164f89611Schristos 	for (i = 0; i < nchans; i++) {
264264f89611Schristos 		if (tgt[i].bChannel == AR_BCHAN_UNUSED ||
264364f89611Schristos 		    tgt[i].bChannel > fbin)
264464f89611Schristos 			break;
264564f89611Schristos 	}
264664f89611Schristos 	hi = i;
264764f89611Schristos 	lo = hi - 1;
264864f89611Schristos 	if (lo == -1)
264964f89611Schristos 		lo = hi;
265064f89611Schristos 	else if (hi == nchans || tgt[hi].bChannel == AR_BCHAN_UNUSED)
265164f89611Schristos 		hi = lo;
265264f89611Schristos 
265364f89611Schristos 	/* Interpolate values. */
265464f89611Schristos 	for (i = 0; i < 4; i++) {
265564f89611Schristos 		tpow[i] = athn_interpolate(fbin,
265664f89611Schristos 		    tgt[lo].bChannel, tgt[lo].tPow2x[i],
265764f89611Schristos 		    tgt[hi].bChannel, tgt[hi].tPow2x[i]);
265864f89611Schristos 	}
265964f89611Schristos 	/* XXX Apply conformance testing limit. */
266064f89611Schristos }
266164f89611Schristos 
266264f89611Schristos #ifndef IEEE80211_NO_HT
266364f89611Schristos PUBLIC void
ar5008_get_ht_tpow(struct athn_softc * sc,struct ieee80211_channel * c,uint8_t ctl,const struct ar_cal_target_power_ht * tgt,int nchans,uint8_t tpow[8])266464f89611Schristos ar5008_get_ht_tpow(struct athn_softc *sc, struct ieee80211_channel *c,
266564f89611Schristos     uint8_t ctl, const struct ar_cal_target_power_ht *tgt, int nchans,
266664f89611Schristos     uint8_t tpow[8])
266764f89611Schristos {
266864f89611Schristos 	uint8_t fbin;
266964f89611Schristos 	int i, lo, hi;
267064f89611Schristos 
267164f89611Schristos 	/* Find interval (lower and upper indices). */
267264f89611Schristos 	fbin = athn_chan2fbin(c);
267364f89611Schristos 	for (i = 0; i < nchans; i++) {
267464f89611Schristos 		if (tgt[i].bChannel == AR_BCHAN_UNUSED ||
267564f89611Schristos 		    tgt[i].bChannel > fbin)
267664f89611Schristos 			break;
267764f89611Schristos 	}
267864f89611Schristos 	hi = i;
267964f89611Schristos 	lo = hi - 1;
268064f89611Schristos 	if (lo == -1)
268164f89611Schristos 		lo = hi;
268264f89611Schristos 	else if (hi == nchans || tgt[hi].bChannel == AR_BCHAN_UNUSED)
268364f89611Schristos 		hi = lo;
268464f89611Schristos 
268564f89611Schristos 	/* Interpolate values. */
268664f89611Schristos 	for (i = 0; i < 8; i++) {
268764f89611Schristos 		tpow[i] = athn_interpolate(fbin,
268864f89611Schristos 		    tgt[lo].bChannel, tgt[lo].tPow2x[i],
268964f89611Schristos 		    tgt[hi].bChannel, tgt[hi].tPow2x[i]);
269064f89611Schristos 	}
269164f89611Schristos 	/* XXX Apply conformance testing limit. */
269264f89611Schristos }
269364f89611Schristos #endif
269464f89611Schristos 
269564f89611Schristos /*
269664f89611Schristos  * Adaptive noise immunity.
269764f89611Schristos  */
269864f89611Schristos Static void
ar5008_set_noise_immunity_level(struct athn_softc * sc,int level)269964f89611Schristos ar5008_set_noise_immunity_level(struct athn_softc *sc, int level)
270064f89611Schristos {
270164f89611Schristos 	int high = level == 4;
270264f89611Schristos 	uint32_t reg;
270364f89611Schristos 
270464f89611Schristos 	reg = AR_READ(sc, AR_PHY_DESIRED_SZ);
270564f89611Schristos 	reg = RW(reg, AR_PHY_DESIRED_SZ_TOT_DES, high ? -62 : -55);
270664f89611Schristos 	AR_WRITE(sc, AR_PHY_DESIRED_SZ, reg);
270764f89611Schristos 
270864f89611Schristos 	reg = AR_READ(sc, AR_PHY_AGC_CTL1);
270964f89611Schristos 	reg = RW(reg, AR_PHY_AGC_CTL1_COARSE_LOW, high ? -70 : -64);
271064f89611Schristos 	reg = RW(reg, AR_PHY_AGC_CTL1_COARSE_HIGH, high ? -12 : -14);
271164f89611Schristos 	AR_WRITE(sc, AR_PHY_AGC_CTL1, reg);
271264f89611Schristos 
271364f89611Schristos 	reg = AR_READ(sc, AR_PHY_FIND_SIG);
271464f89611Schristos 	reg = RW(reg, AR_PHY_FIND_SIG_FIRPWR, high ? -80 : -78);
271564f89611Schristos 	AR_WRITE(sc, AR_PHY_FIND_SIG, reg);
271664f89611Schristos 
271764f89611Schristos 	AR_WRITE_BARRIER(sc);
271864f89611Schristos }
271964f89611Schristos 
272064f89611Schristos Static void
ar5008_enable_ofdm_weak_signal(struct athn_softc * sc)272164f89611Schristos ar5008_enable_ofdm_weak_signal(struct athn_softc *sc)
272264f89611Schristos {
272364f89611Schristos 	uint32_t reg;
272464f89611Schristos 
272564f89611Schristos 	reg = AR_READ(sc, AR_PHY_SFCORR_LOW);
272664f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_LOW_M1_THRESH_LOW, 50);
272764f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_LOW_M2_THRESH_LOW, 40);
272864f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_LOW_M2COUNT_THR_LOW, 48);
272964f89611Schristos 	AR_WRITE(sc, AR_PHY_SFCORR_LOW, reg);
273064f89611Schristos 
273164f89611Schristos 	reg = AR_READ(sc, AR_PHY_SFCORR);
273264f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_M1_THRESH, 77);
273364f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_M2_THRESH, 64);
273464f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_M2COUNT_THR, 16);
273564f89611Schristos 	AR_WRITE(sc, AR_PHY_SFCORR, reg);
273664f89611Schristos 
273764f89611Schristos 	reg = AR_READ(sc, AR_PHY_SFCORR_EXT);
273864f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_EXT_M1_THRESH_LOW, 50);
273964f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_EXT_M2_THRESH_LOW, 40);
274064f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_EXT_M1_THRESH, 77);
274164f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_EXT_M2_THRESH, 64);
274264f89611Schristos 	AR_WRITE(sc, AR_PHY_SFCORR_EXT, reg);
274364f89611Schristos 
274464f89611Schristos 	AR_SETBITS(sc, AR_PHY_SFCORR_LOW,
274564f89611Schristos 	    AR_PHY_SFCORR_LOW_USE_SELF_CORR_LOW);
274664f89611Schristos 	AR_WRITE_BARRIER(sc);
274764f89611Schristos }
274864f89611Schristos 
274964f89611Schristos Static void
ar5008_disable_ofdm_weak_signal(struct athn_softc * sc)275064f89611Schristos ar5008_disable_ofdm_weak_signal(struct athn_softc *sc)
275164f89611Schristos {
275264f89611Schristos 	uint32_t reg;
275364f89611Schristos 
275464f89611Schristos 	reg = AR_READ(sc, AR_PHY_SFCORR_LOW);
275564f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_LOW_M1_THRESH_LOW, 127);
275664f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_LOW_M2_THRESH_LOW, 127);
275764f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_LOW_M2COUNT_THR_LOW, 63);
275864f89611Schristos 	AR_WRITE(sc, AR_PHY_SFCORR_LOW, reg);
275964f89611Schristos 
276064f89611Schristos 	reg = AR_READ(sc, AR_PHY_SFCORR);
276164f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_M1_THRESH, 127);
276264f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_M2_THRESH, 127);
276364f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_M2COUNT_THR, 31);
276464f89611Schristos 	AR_WRITE(sc, AR_PHY_SFCORR, reg);
276564f89611Schristos 
276664f89611Schristos 	reg = AR_READ(sc, AR_PHY_SFCORR_EXT);
276764f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_EXT_M1_THRESH_LOW, 127);
276864f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_EXT_M2_THRESH_LOW, 127);
276964f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_EXT_M1_THRESH, 127);
277064f89611Schristos 	reg = RW(reg, AR_PHY_SFCORR_EXT_M2_THRESH, 127);
277164f89611Schristos 	AR_WRITE(sc, AR_PHY_SFCORR_EXT, reg);
277264f89611Schristos 
277364f89611Schristos 	AR_CLRBITS(sc, AR_PHY_SFCORR_LOW,
277464f89611Schristos 	    AR_PHY_SFCORR_LOW_USE_SELF_CORR_LOW);
277564f89611Schristos 	AR_WRITE_BARRIER(sc);
277664f89611Schristos }
277764f89611Schristos 
277864f89611Schristos Static void
ar5008_set_cck_weak_signal(struct athn_softc * sc,int high)277964f89611Schristos ar5008_set_cck_weak_signal(struct athn_softc *sc, int high)
278064f89611Schristos {
278164f89611Schristos 	uint32_t reg;
278264f89611Schristos 
278364f89611Schristos 	reg = AR_READ(sc, AR_PHY_CCK_DETECT);
278464f89611Schristos 	reg = RW(reg, AR_PHY_CCK_DETECT_WEAK_SIG_THR_CCK, high ? 6 : 8);
278564f89611Schristos 	AR_WRITE(sc, AR_PHY_CCK_DETECT, reg);
278664f89611Schristos 	AR_WRITE_BARRIER(sc);
278764f89611Schristos }
278864f89611Schristos 
278964f89611Schristos Static void
ar5008_set_firstep_level(struct athn_softc * sc,int level)279064f89611Schristos ar5008_set_firstep_level(struct athn_softc *sc, int level)
279164f89611Schristos {
279264f89611Schristos 	uint32_t reg;
279364f89611Schristos 
279464f89611Schristos 	reg = AR_READ(sc, AR_PHY_FIND_SIG);
279564f89611Schristos 	reg = RW(reg, AR_PHY_FIND_SIG_FIRSTEP, level * 4);
279664f89611Schristos 	AR_WRITE(sc, AR_PHY_FIND_SIG, reg);
279764f89611Schristos 	AR_WRITE_BARRIER(sc);
279864f89611Schristos }
279964f89611Schristos 
280064f89611Schristos Static void
ar5008_set_spur_immunity_level(struct athn_softc * sc,int level)280164f89611Schristos ar5008_set_spur_immunity_level(struct athn_softc *sc, int level)
280264f89611Schristos {
280364f89611Schristos 	uint32_t reg;
280464f89611Schristos 
280564f89611Schristos 	reg = AR_READ(sc, AR_PHY_TIMING5);
280664f89611Schristos 	reg = RW(reg, AR_PHY_TIMING5_CYCPWR_THR1, (level + 1) * 2);
280764f89611Schristos 	AR_WRITE(sc, AR_PHY_TIMING5, reg);
280864f89611Schristos 	AR_WRITE_BARRIER(sc);
280964f89611Schristos }
2810