Home | History | Annotate | Line # | Download | only in ic
arn5008.c revision 1.8
      1  1.8      matt /*	$NetBSD: arn5008.c,v 1.8 2015/05/24 17:19:29 matt Exp $	*/
      2  1.1  christos /*	$OpenBSD: ar5008.c,v 1.21 2012/08/25 12:14:31 kettenis Exp $	*/
      3  1.1  christos 
      4  1.1  christos /*-
      5  1.1  christos  * Copyright (c) 2009 Damien Bergamini <damien.bergamini (at) free.fr>
      6  1.1  christos  * Copyright (c) 2008-2009 Atheros Communications Inc.
      7  1.1  christos  *
      8  1.1  christos  * Permission to use, copy, modify, and/or distribute this software for any
      9  1.1  christos  * purpose with or without fee is hereby granted, provided that the above
     10  1.1  christos  * copyright notice and this permission notice appear in all copies.
     11  1.1  christos  *
     12  1.1  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     13  1.1  christos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     14  1.1  christos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     15  1.1  christos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     16  1.1  christos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     17  1.1  christos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     18  1.1  christos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     19  1.1  christos  */
     20  1.1  christos 
     21  1.1  christos /*
     22  1.1  christos  * Driver for Atheros 802.11a/g/n chipsets.
     23  1.1  christos  * Routines common to AR5008, AR9001 and AR9002 families.
     24  1.1  christos  */
     25  1.1  christos 
     26  1.1  christos #include <sys/cdefs.h>
     27  1.8      matt __KERNEL_RCSID(0, "$NetBSD: arn5008.c,v 1.8 2015/05/24 17:19:29 matt Exp $");
     28  1.1  christos 
     29  1.1  christos #include <sys/param.h>
     30  1.1  christos #include <sys/sockio.h>
     31  1.1  christos #include <sys/mbuf.h>
     32  1.1  christos #include <sys/kernel.h>
     33  1.1  christos #include <sys/socket.h>
     34  1.1  christos #include <sys/systm.h>
     35  1.1  christos #include <sys/malloc.h>
     36  1.1  christos #include <sys/queue.h>
     37  1.1  christos #include <sys/conf.h>
     38  1.1  christos #include <sys/device.h>
     39  1.1  christos 
     40  1.1  christos #include <sys/bus.h>
     41  1.1  christos #include <sys/endian.h>
     42  1.1  christos #include <sys/intr.h>
     43  1.1  christos 
     44  1.1  christos #include <net/bpf.h>
     45  1.1  christos #include <net/if.h>
     46  1.1  christos #include <net/if_arp.h>
     47  1.1  christos #include <net/if_dl.h>
     48  1.2  christos #include <net/if_ether.h>
     49  1.1  christos #include <net/if_media.h>
     50  1.1  christos #include <net/if_types.h>
     51  1.1  christos 
     52  1.1  christos #include <netinet/in.h>
     53  1.1  christos #include <netinet/in_systm.h>
     54  1.1  christos #include <netinet/in_var.h>
     55  1.1  christos #include <netinet/ip.h>
     56  1.1  christos 
     57  1.1  christos #include <net80211/ieee80211_var.h>
     58  1.1  christos #include <net80211/ieee80211_amrr.h>
     59  1.1  christos #include <net80211/ieee80211_radiotap.h>
     60  1.1  christos 
     61  1.1  christos #include <dev/ic/athnreg.h>
     62  1.1  christos #include <dev/ic/athnvar.h>
     63  1.1  christos 
     64  1.1  christos #include <dev/ic/arn5008reg.h>
     65  1.1  christos #include <dev/ic/arn5008.h>
     66  1.1  christos #include <dev/ic/arn5416.h>
     67  1.1  christos #include <dev/ic/arn9280.h>
     68  1.1  christos 
     69  1.1  christos #define Static static
     70  1.1  christos 
     71  1.1  christos Static void	ar5008_calib_adc_dc_off(struct athn_softc *);
     72  1.1  christos Static void	ar5008_calib_adc_gain(struct athn_softc *);
     73  1.1  christos Static void	ar5008_calib_iq(struct athn_softc *);
     74  1.1  christos Static void	ar5008_disable_ofdm_weak_signal(struct athn_softc *);
     75  1.1  christos Static void	ar5008_disable_phy(struct athn_softc *);
     76  1.1  christos Static int	ar5008_dma_alloc(struct athn_softc *);
     77  1.1  christos Static void	ar5008_dma_free(struct athn_softc *);
     78  1.1  christos Static void	ar5008_do_calib(struct athn_softc *);
     79  1.1  christos Static void	ar5008_do_noisefloor_calib(struct athn_softc *);
     80  1.1  christos Static void	ar5008_enable_antenna_diversity(struct athn_softc *);
     81  1.1  christos Static void	ar5008_enable_ofdm_weak_signal(struct athn_softc *);
     82  1.1  christos Static uint8_t	ar5008_get_vpd(uint8_t, const uint8_t *, const uint8_t *, int);
     83  1.1  christos Static void	ar5008_gpio_config_input(struct athn_softc *, int);
     84  1.1  christos Static void	ar5008_gpio_config_output(struct athn_softc *, int, int);
     85  1.1  christos Static int	ar5008_gpio_read(struct athn_softc *, int);
     86  1.1  christos Static void	ar5008_gpio_write(struct athn_softc *, int, int);
     87  1.1  christos Static void	ar5008_hw_init(struct athn_softc *, struct ieee80211_channel *,
     88  1.1  christos 		    struct ieee80211_channel *);
     89  1.1  christos Static void	ar5008_init_baseband(struct athn_softc *);
     90  1.1  christos Static void	ar5008_init_chains(struct athn_softc *);
     91  1.1  christos Static int	ar5008_intr(struct athn_softc *);
     92  1.1  christos Static void	ar5008_next_calib(struct athn_softc *);
     93  1.1  christos Static int	ar5008_read_eep_word(struct athn_softc *, uint32_t,
     94  1.1  christos 		    uint16_t *);
     95  1.1  christos Static int	ar5008_read_rom(struct athn_softc *);
     96  1.1  christos Static void	ar5008_rf_bus_release(struct athn_softc *);
     97  1.1  christos Static int	ar5008_rf_bus_request(struct athn_softc *);
     98  1.1  christos Static void	ar5008_rfsilent_init(struct athn_softc *);
     99  1.1  christos Static int	ar5008_rx_alloc(struct athn_softc *);
    100  1.1  christos Static void	ar5008_rx_enable(struct athn_softc *);
    101  1.1  christos Static void	ar5008_rx_free(struct athn_softc *);
    102  1.1  christos Static void	ar5008_rx_intr(struct athn_softc *);
    103  1.1  christos Static void	ar5008_rx_radiotap(struct athn_softc *, struct mbuf *,
    104  1.1  christos 		    struct ar_rx_desc *);
    105  1.1  christos Static void	ar5008_set_cck_weak_signal(struct athn_softc *, int);
    106  1.1  christos Static void	ar5008_set_delta_slope(struct athn_softc *,
    107  1.1  christos 		    struct ieee80211_channel *, struct ieee80211_channel *);
    108  1.1  christos Static void	ar5008_set_firstep_level(struct athn_softc *, int);
    109  1.1  christos Static void	ar5008_set_noise_immunity_level(struct athn_softc *, int);
    110  1.1  christos Static void	ar5008_set_phy(struct athn_softc *, struct ieee80211_channel *,
    111  1.1  christos 		    struct ieee80211_channel *);
    112  1.1  christos Static void	ar5008_set_rf_mode(struct athn_softc *,
    113  1.1  christos 		    struct ieee80211_channel *);
    114  1.1  christos Static void	ar5008_set_rxchains(struct athn_softc *);
    115  1.1  christos Static void	ar5008_set_spur_immunity_level(struct athn_softc *, int);
    116  1.1  christos Static void	ar5008_swap_rom(struct athn_softc *);
    117  1.1  christos Static int	ar5008_swba_intr(struct athn_softc *);
    118  1.1  christos Static int	ar5008_tx(struct athn_softc *, struct mbuf *,
    119  1.1  christos 		    struct ieee80211_node *, int);
    120  1.1  christos Static int	ar5008_tx_alloc(struct athn_softc *);
    121  1.1  christos Static void	ar5008_tx_free(struct athn_softc *);
    122  1.1  christos Static void	ar5008_tx_intr(struct athn_softc *);
    123  1.1  christos Static int	ar5008_tx_process(struct athn_softc *, int);
    124  1.1  christos 
    125  1.1  christos #ifdef notused
    126  1.1  christos Static void	ar5008_bb_load_noisefloor(struct athn_softc *);
    127  1.1  christos Static void	ar5008_get_noisefloor(struct athn_softc *,
    128  1.1  christos 		    struct ieee80211_channel *);
    129  1.1  christos Static void	ar5008_noisefloor_calib(struct athn_softc *);
    130  1.1  christos Static void	ar5008_read_noisefloor(struct athn_softc *, int16_t *,
    131  1.1  christos 		    int16_t *);
    132  1.1  christos Static void	ar5008_write_noisefloor(struct athn_softc *, int16_t *,
    133  1.1  christos 		    int16_t *);
    134  1.1  christos #endif /* notused */
    135  1.1  christos 
    136  1.1  christos // bf->bf_m = MCLGETI(NULL, M_DONTWAIT, NULL, ATHN_RXBUFSZ);
    137  1.1  christos 
    138  1.1  christos /*
    139  1.1  christos  * XXX: see if_iwn.c:MCLGETIalt() for a better solution.
    140  1.1  christos  */
    141  1.1  christos static struct mbuf *
    142  1.1  christos MCLGETI(struct athn_softc *sc __unused, int how,
    143  1.1  christos     struct ifnet *ifp __unused, u_int size)
    144  1.1  christos {
    145  1.1  christos 	struct mbuf *m;
    146  1.1  christos 
    147  1.1  christos 	MGETHDR(m, how, MT_DATA);
    148  1.1  christos 	if (m == NULL)
    149  1.1  christos 		return NULL;
    150  1.1  christos 
    151  1.1  christos 	MEXTMALLOC(m, size, how);
    152  1.1  christos 	if ((m->m_flags & M_EXT) == 0) {
    153  1.1  christos 		m_freem(m);
    154  1.1  christos 		return NULL;
    155  1.1  christos 	}
    156  1.1  christos 	return m;
    157  1.1  christos }
    158  1.1  christos 
    159  1.1  christos PUBLIC int
    160  1.1  christos ar5008_attach(struct athn_softc *sc)
    161  1.1  christos {
    162  1.1  christos 	struct athn_ops *ops = &sc->sc_ops;
    163  1.1  christos 	struct ieee80211com *ic = &sc->sc_ic;
    164  1.1  christos 	struct ar_base_eep_header *base;
    165  1.1  christos 	uint8_t eep_ver, kc_entries_log;
    166  1.1  christos 	int error;
    167  1.1  christos 
    168  1.1  christos 	/* Set callbacks for AR5008, AR9001 and AR9002 families. */
    169  1.1  christos 	ops->gpio_read = ar5008_gpio_read;
    170  1.1  christos 	ops->gpio_write = ar5008_gpio_write;
    171  1.1  christos 	ops->gpio_config_input = ar5008_gpio_config_input;
    172  1.1  christos 	ops->gpio_config_output = ar5008_gpio_config_output;
    173  1.1  christos 	ops->rfsilent_init = ar5008_rfsilent_init;
    174  1.1  christos 
    175  1.1  christos 	ops->dma_alloc = ar5008_dma_alloc;
    176  1.1  christos 	ops->dma_free = ar5008_dma_free;
    177  1.1  christos 	ops->rx_enable = ar5008_rx_enable;
    178  1.1  christos 	ops->intr = ar5008_intr;
    179  1.1  christos 	ops->tx = ar5008_tx;
    180  1.1  christos 
    181  1.1  christos 	ops->set_rf_mode = ar5008_set_rf_mode;
    182  1.1  christos 	ops->rf_bus_request = ar5008_rf_bus_request;
    183  1.1  christos 	ops->rf_bus_release = ar5008_rf_bus_release;
    184  1.1  christos 	ops->set_phy = ar5008_set_phy;
    185  1.1  christos 	ops->set_delta_slope = ar5008_set_delta_slope;
    186  1.1  christos 	ops->enable_antenna_diversity = ar5008_enable_antenna_diversity;
    187  1.1  christos 	ops->init_baseband = ar5008_init_baseband;
    188  1.1  christos 	ops->disable_phy = ar5008_disable_phy;
    189  1.1  christos 	ops->set_rxchains = ar5008_set_rxchains;
    190  1.1  christos 	ops->noisefloor_calib = ar5008_do_noisefloor_calib;
    191  1.1  christos 	ops->do_calib = ar5008_do_calib;
    192  1.1  christos 	ops->next_calib = ar5008_next_calib;
    193  1.1  christos 	ops->hw_init = ar5008_hw_init;
    194  1.1  christos 
    195  1.1  christos 	ops->set_noise_immunity_level = ar5008_set_noise_immunity_level;
    196  1.1  christos 	ops->enable_ofdm_weak_signal = ar5008_enable_ofdm_weak_signal;
    197  1.1  christos 	ops->disable_ofdm_weak_signal = ar5008_disable_ofdm_weak_signal;
    198  1.1  christos 	ops->set_cck_weak_signal = ar5008_set_cck_weak_signal;
    199  1.1  christos 	ops->set_firstep_level = ar5008_set_firstep_level;
    200  1.1  christos 	ops->set_spur_immunity_level = ar5008_set_spur_immunity_level;
    201  1.1  christos 
    202  1.1  christos 	/* Set MAC registers offsets. */
    203  1.1  christos 	sc->sc_obs_off = AR_OBS;
    204  1.1  christos 	sc->sc_gpio_input_en_off = AR_GPIO_INPUT_EN_VAL;
    205  1.1  christos 
    206  1.1  christos 	if (!(sc->sc_flags & ATHN_FLAG_PCIE))
    207  1.1  christos 		athn_config_nonpcie(sc);
    208  1.1  christos 	else
    209  1.1  christos 		athn_config_pcie(sc);
    210  1.1  christos 
    211  1.1  christos 	/* Read entire ROM content in memory. */
    212  1.1  christos 	if ((error = ar5008_read_rom(sc)) != 0) {
    213  1.6  christos 		aprint_error_dev(sc->sc_dev, "could not read ROM\n");
    214  1.1  christos 		return error;
    215  1.1  christos 	}
    216  1.1  christos 
    217  1.1  christos 	/* Get RF revision. */
    218  1.1  christos 	sc->sc_rf_rev = ar5416_get_rf_rev(sc);
    219  1.1  christos 
    220  1.1  christos 	base = sc->sc_eep;
    221  1.1  christos 	eep_ver = (base->version >> 12) & 0xf;
    222  1.1  christos 	sc->sc_eep_rev = (base->version & 0xfff);
    223  1.1  christos 	if (eep_ver != AR_EEP_VER || sc->sc_eep_rev == 0) {
    224  1.6  christos 		aprint_error_dev(sc->sc_dev, "unsupported ROM version %d.%d\n",
    225  1.6  christos 		    eep_ver, sc->sc_eep_rev);
    226  1.1  christos 		return EINVAL;
    227  1.1  christos 	}
    228  1.1  christos 
    229  1.1  christos 	if (base->opCapFlags & AR_OPFLAGS_11A)
    230  1.1  christos 		sc->sc_flags |= ATHN_FLAG_11A;
    231  1.1  christos 	if (base->opCapFlags & AR_OPFLAGS_11G)
    232  1.1  christos 		sc->sc_flags |= ATHN_FLAG_11G;
    233  1.1  christos 	if (base->opCapFlags & AR_OPFLAGS_11N)
    234  1.1  christos 		sc->sc_flags |= ATHN_FLAG_11N;
    235  1.1  christos 
    236  1.1  christos 	IEEE80211_ADDR_COPY(ic->ic_myaddr, base->macAddr);
    237  1.1  christos 
    238  1.1  christos 	/* Check if we have a hardware radio switch. */
    239  1.1  christos 	if (base->rfSilent & AR_EEP_RFSILENT_ENABLED) {
    240  1.1  christos 		sc->sc_flags |= ATHN_FLAG_RFSILENT;
    241  1.1  christos 		/* Get GPIO pin used by hardware radio switch. */
    242  1.1  christos 		sc->sc_rfsilent_pin = MS(base->rfSilent,
    243  1.1  christos 		    AR_EEP_RFSILENT_GPIO_SEL);
    244  1.1  christos 		/* Get polarity of hardware radio switch. */
    245  1.1  christos 		if (base->rfSilent & AR_EEP_RFSILENT_POLARITY)
    246  1.1  christos 			sc->sc_flags |= ATHN_FLAG_RFSILENT_REVERSED;
    247  1.1  christos 	}
    248  1.1  christos 
    249  1.1  christos 	/* Get the number of HW key cache entries. */
    250  1.1  christos 	kc_entries_log = MS(base->deviceCap, AR_EEP_DEVCAP_KC_ENTRIES);
    251  1.1  christos 	sc->sc_kc_entries = kc_entries_log != 0 ?
    252  1.1  christos 	    1 << kc_entries_log : AR_KEYTABLE_SIZE;
    253  1.1  christos 
    254  1.1  christos 	sc->sc_txchainmask = base->txMask;
    255  1.1  christos 	if (sc->sc_mac_ver == AR_SREV_VERSION_5416_PCI &&
    256  1.1  christos 	    !(base->opCapFlags & AR_OPFLAGS_11A)) {
    257  1.1  christos 		/* For single-band AR5416 PCI, use GPIO pin 0. */
    258  1.1  christos 		sc->sc_rxchainmask = ar5008_gpio_read(sc, 0) ? 0x5 : 0x7;
    259  1.1  christos 	}
    260  1.1  christos 	else
    261  1.1  christos 		sc->sc_rxchainmask = base->rxMask;
    262  1.1  christos 
    263  1.1  christos 	ops->setup(sc);
    264  1.1  christos 	return 0;
    265  1.1  christos }
    266  1.1  christos 
    267  1.1  christos /*
    268  1.1  christos  * Read 16-bit word from ROM.
    269  1.1  christos  */
    270  1.1  christos Static int
    271  1.1  christos ar5008_read_eep_word(struct athn_softc *sc, uint32_t addr, uint16_t *val)
    272  1.1  christos {
    273  1.1  christos 	uint32_t reg;
    274  1.1  christos 	int ntries;
    275  1.1  christos 
    276  1.1  christos 	reg = AR_READ(sc, AR_EEPROM_OFFSET(addr));
    277  1.1  christos 	for (ntries = 0; ntries < 1000; ntries++) {
    278  1.1  christos 		reg = AR_READ(sc, AR_EEPROM_STATUS_DATA);
    279  1.1  christos 		if (!(reg & (AR_EEPROM_STATUS_DATA_BUSY |
    280  1.1  christos 		    AR_EEPROM_STATUS_DATA_PROT_ACCESS))) {
    281  1.1  christos 			*val = MS(reg, AR_EEPROM_STATUS_DATA_VAL);
    282  1.1  christos 			return 0;
    283  1.1  christos 		}
    284  1.1  christos 		DELAY(10);
    285  1.1  christos 	}
    286  1.1  christos 	*val = 0xffff;
    287  1.1  christos 	return ETIMEDOUT;
    288  1.1  christos }
    289  1.1  christos 
    290  1.1  christos Static int
    291  1.1  christos ar5008_read_rom(struct athn_softc *sc)
    292  1.1  christos {
    293  1.1  christos 	uint32_t addr, end;
    294  1.1  christos 	uint16_t magic, sum, *eep;
    295  1.1  christos 	int need_swap = 0;
    296  1.1  christos 	int error;
    297  1.1  christos 
    298  1.1  christos 	/* Determine ROM endianness. */
    299  1.1  christos 	error = ar5008_read_eep_word(sc, AR_EEPROM_MAGIC_OFFSET, &magic);
    300  1.1  christos 	if (error != 0)
    301  1.1  christos 		return error;
    302  1.1  christos 	if (magic != AR_EEPROM_MAGIC) {
    303  1.1  christos 		if (magic != bswap16(AR_EEPROM_MAGIC)) {
    304  1.1  christos 			DPRINTFN(DBG_INIT, sc,
    305  1.1  christos 			    "invalid ROM magic 0x%x != 0x%x\n",
    306  1.1  christos 			    magic, AR_EEPROM_MAGIC);
    307  1.1  christos 			return EIO;
    308  1.1  christos 		}
    309  1.1  christos 		DPRINTFN(DBG_INIT, sc, "non-native ROM endianness\n");
    310  1.1  christos 		need_swap = 1;
    311  1.1  christos 	}
    312  1.1  christos 
    313  1.1  christos 	/* Allocate space to store ROM in host memory. */
    314  1.1  christos 	sc->sc_eep = malloc(sc->sc_eep_size, M_DEVBUF, M_NOWAIT);
    315  1.1  christos 	if (sc->sc_eep == NULL)
    316  1.1  christos 		return ENOMEM;
    317  1.1  christos 
    318  1.1  christos 	/* Read entire ROM and compute checksum. */
    319  1.1  christos 	sum = 0;
    320  1.1  christos 	eep = sc->sc_eep;
    321  1.1  christos 	end = sc->sc_eep_base + sc->sc_eep_size / sizeof(uint16_t);
    322  1.1  christos 	for (addr = sc->sc_eep_base; addr < end; addr++, eep++) {
    323  1.1  christos 		if ((error = ar5008_read_eep_word(sc, addr, eep)) != 0) {
    324  1.1  christos 			DPRINTFN(DBG_INIT, sc,
    325  1.1  christos 			    "could not read ROM at 0x%x\n", addr);
    326  1.1  christos 			return error;
    327  1.1  christos 		}
    328  1.1  christos 		if (need_swap)
    329  1.1  christos 			*eep = bswap16(*eep);
    330  1.1  christos 		sum ^= *eep;
    331  1.1  christos 	}
    332  1.1  christos 	if (sum != 0xffff) {
    333  1.6  christos 		aprint_error_dev(sc->sc_dev, "bad ROM checksum 0x%04x\n", sum);
    334  1.1  christos 		return EIO;
    335  1.1  christos 	}
    336  1.1  christos 	if (need_swap)
    337  1.1  christos 		ar5008_swap_rom(sc);
    338  1.1  christos 
    339  1.1  christos 	return 0;
    340  1.1  christos }
    341  1.1  christos 
    342  1.1  christos Static void
    343  1.1  christos ar5008_swap_rom(struct athn_softc *sc)
    344  1.1  christos {
    345  1.1  christos 	struct ar_base_eep_header *base = sc->sc_eep;
    346  1.1  christos 
    347  1.1  christos 	/* Swap common fields first. */
    348  1.1  christos 	base->length = bswap16(base->length);
    349  1.1  christos 	base->version = bswap16(base->version);
    350  1.1  christos 	base->regDmn[0] = bswap16(base->regDmn[0]);
    351  1.1  christos 	base->regDmn[1] = bswap16(base->regDmn[1]);
    352  1.1  christos 	base->rfSilent = bswap16(base->rfSilent);
    353  1.1  christos 	base->blueToothOptions = bswap16(base->blueToothOptions);
    354  1.1  christos 	base->deviceCap = bswap16(base->deviceCap);
    355  1.1  christos 
    356  1.1  christos 	/* Swap device-dependent fields. */
    357  1.1  christos 	sc->sc_ops.swap_rom(sc);
    358  1.1  christos }
    359  1.1  christos 
    360  1.1  christos /*
    361  1.1  christos  * Access to General Purpose Input/Output ports.
    362  1.1  christos  */
    363  1.1  christos Static int
    364  1.1  christos ar5008_gpio_read(struct athn_softc *sc, int pin)
    365  1.1  christos {
    366  1.1  christos 
    367  1.1  christos 	KASSERT(pin < sc->sc_ngpiopins);
    368  1.1  christos 	if ((sc->sc_flags & ATHN_FLAG_USB) && !AR_SREV_9271(sc))
    369  1.1  christos 		return !((AR_READ(sc, AR7010_GPIO_IN) >> pin) & 1);
    370  1.1  christos 	return (AR_READ(sc, AR_GPIO_IN_OUT) >> (sc->sc_ngpiopins + pin)) & 1;
    371  1.1  christos }
    372  1.1  christos 
    373  1.1  christos Static void
    374  1.1  christos ar5008_gpio_write(struct athn_softc *sc, int pin, int set)
    375  1.1  christos {
    376  1.1  christos 	uint32_t reg;
    377  1.1  christos 
    378  1.1  christos 	KASSERT(pin < sc->sc_ngpiopins);
    379  1.1  christos 
    380  1.1  christos 	if (sc->sc_flags & ATHN_FLAG_USB)
    381  1.1  christos 		set = !set;	/* AR9271/AR7010 is reversed. */
    382  1.1  christos 
    383  1.1  christos 	if ((sc->sc_flags & ATHN_FLAG_USB) && !AR_SREV_9271(sc)) {
    384  1.1  christos 		/* Special case for AR7010. */
    385  1.1  christos 		reg = AR_READ(sc, AR7010_GPIO_OUT);
    386  1.1  christos 		if (set)
    387  1.1  christos 			reg |= 1 << pin;
    388  1.1  christos 		else
    389  1.1  christos 			reg &= ~(1 << pin);
    390  1.1  christos 		AR_WRITE(sc, AR7010_GPIO_OUT, reg);
    391  1.1  christos 	}
    392  1.1  christos 	else {
    393  1.1  christos 		reg = AR_READ(sc, AR_GPIO_IN_OUT);
    394  1.1  christos 		if (set)
    395  1.1  christos 			reg |= 1 << pin;
    396  1.1  christos 		else
    397  1.1  christos 			reg &= ~(1 << pin);
    398  1.1  christos 		AR_WRITE(sc, AR_GPIO_IN_OUT, reg);
    399  1.1  christos 	}
    400  1.1  christos 	AR_WRITE_BARRIER(sc);
    401  1.1  christos }
    402  1.1  christos 
    403  1.1  christos Static void
    404  1.1  christos ar5008_gpio_config_input(struct athn_softc *sc, int pin)
    405  1.1  christos {
    406  1.1  christos 	uint32_t reg;
    407  1.1  christos 
    408  1.1  christos 	if ((sc->sc_flags & ATHN_FLAG_USB) && !AR_SREV_9271(sc)) {
    409  1.1  christos 		/* Special case for AR7010. */
    410  1.1  christos 		AR_SETBITS(sc, AR7010_GPIO_OE, 1 << pin);
    411  1.1  christos 	}
    412  1.1  christos 	else {
    413  1.1  christos 		reg = AR_READ(sc, AR_GPIO_OE_OUT);
    414  1.1  christos 		reg &= ~(AR_GPIO_OE_OUT_DRV_M << (pin * 2));
    415  1.1  christos 		reg |= AR_GPIO_OE_OUT_DRV_NO << (pin * 2);
    416  1.1  christos 		AR_WRITE(sc, AR_GPIO_OE_OUT, reg);
    417  1.1  christos 	}
    418  1.1  christos 	AR_WRITE_BARRIER(sc);
    419  1.1  christos }
    420  1.1  christos 
    421  1.1  christos Static void
    422  1.1  christos ar5008_gpio_config_output(struct athn_softc *sc, int pin, int type)
    423  1.1  christos {
    424  1.1  christos 	uint32_t reg;
    425  1.1  christos 	int mux, off;
    426  1.1  christos 
    427  1.1  christos 	if ((sc->sc_flags & ATHN_FLAG_USB) && !AR_SREV_9271(sc)) {
    428  1.1  christos 		/* Special case for AR7010. */
    429  1.1  christos 		AR_CLRBITS(sc, AR7010_GPIO_OE, 1 << pin);
    430  1.1  christos 		AR_WRITE_BARRIER(sc);
    431  1.1  christos 		return;
    432  1.1  christos 	}
    433  1.1  christos 	mux = pin / 6;
    434  1.1  christos 	off = pin % 6;
    435  1.1  christos 
    436  1.1  christos 	reg = AR_READ(sc, AR_GPIO_OUTPUT_MUX(mux));
    437  1.1  christos 	if (!AR_SREV_9280_20_OR_LATER(sc) && mux == 0)
    438  1.1  christos 		reg = (reg & ~0x1f0) | (reg & 0x1f0) << 1;
    439  1.1  christos 	reg &= ~(0x1f << (off * 5));
    440  1.1  christos 	reg |= (type & 0x1f) << (off * 5);
    441  1.1  christos 	AR_WRITE(sc, AR_GPIO_OUTPUT_MUX(mux), reg);
    442  1.1  christos 
    443  1.1  christos 	reg = AR_READ(sc, AR_GPIO_OE_OUT);
    444  1.1  christos 	reg &= ~(AR_GPIO_OE_OUT_DRV_M << (pin * 2));
    445  1.1  christos 	reg |= AR_GPIO_OE_OUT_DRV_ALL << (pin * 2);
    446  1.1  christos 	AR_WRITE(sc, AR_GPIO_OE_OUT, reg);
    447  1.1  christos 	AR_WRITE_BARRIER(sc);
    448  1.1  christos }
    449  1.1  christos 
    450  1.1  christos Static void
    451  1.1  christos ar5008_rfsilent_init(struct athn_softc *sc)
    452  1.1  christos {
    453  1.1  christos 	uint32_t reg;
    454  1.1  christos 
    455  1.1  christos 	/* Configure hardware radio switch. */
    456  1.1  christos 	AR_SETBITS(sc, AR_GPIO_INPUT_EN_VAL, AR_GPIO_INPUT_EN_VAL_RFSILENT_BB);
    457  1.1  christos 	reg = AR_READ(sc, AR_GPIO_INPUT_MUX2);
    458  1.1  christos 	reg = RW(reg, AR_GPIO_INPUT_MUX2_RFSILENT, 0);
    459  1.1  christos 	AR_WRITE(sc, AR_GPIO_INPUT_MUX2, reg);
    460  1.1  christos 	ar5008_gpio_config_input(sc, sc->sc_rfsilent_pin);
    461  1.1  christos 	AR_SETBITS(sc, AR_PHY_TEST, AR_PHY_TEST_RFSILENT_BB);
    462  1.1  christos 	if (!(sc->sc_flags & ATHN_FLAG_RFSILENT_REVERSED)) {
    463  1.1  christos 		AR_SETBITS(sc, AR_GPIO_INTR_POL,
    464  1.1  christos 		    AR_GPIO_INTR_POL_PIN(sc->sc_rfsilent_pin));
    465  1.1  christos 	}
    466  1.1  christos 	AR_WRITE_BARRIER(sc);
    467  1.1  christos }
    468  1.1  christos 
    469  1.1  christos Static int
    470  1.1  christos ar5008_dma_alloc(struct athn_softc *sc)
    471  1.1  christos {
    472  1.1  christos 	int error;
    473  1.1  christos 
    474  1.1  christos 	error = ar5008_tx_alloc(sc);
    475  1.1  christos 	if (error != 0)
    476  1.1  christos 		return error;
    477  1.1  christos 
    478  1.1  christos 	error = ar5008_rx_alloc(sc);
    479  1.1  christos 	if (error != 0)
    480  1.1  christos 		return error;
    481  1.1  christos 
    482  1.1  christos 	return 0;
    483  1.1  christos }
    484  1.1  christos 
    485  1.1  christos Static void
    486  1.1  christos ar5008_dma_free(struct athn_softc *sc)
    487  1.1  christos {
    488  1.1  christos 
    489  1.1  christos 	ar5008_tx_free(sc);
    490  1.1  christos 	ar5008_rx_free(sc);
    491  1.1  christos }
    492  1.1  christos 
    493  1.1  christos Static int
    494  1.1  christos ar5008_tx_alloc(struct athn_softc *sc)
    495  1.1  christos {
    496  1.1  christos 	struct athn_tx_buf *bf;
    497  1.1  christos 	bus_size_t size;
    498  1.1  christos 	int error, nsegs, i;
    499  1.1  christos 
    500  1.1  christos 	/*
    501  1.1  christos 	 * Allocate a pool of Tx descriptors shared between all Tx queues.
    502  1.1  christos 	 */
    503  1.1  christos 	size = ATHN_NTXBUFS * AR5008_MAX_SCATTER * sizeof(struct ar_tx_desc);
    504  1.1  christos 
    505  1.1  christos 	error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
    506  1.1  christos 	    BUS_DMA_NOWAIT, &sc->sc_map);
    507  1.1  christos 	if (error != 0)
    508  1.1  christos 		goto fail;
    509  1.1  christos 
    510  1.1  christos 	error = bus_dmamem_alloc(sc->sc_dmat, size, 4, 0, &sc->sc_seg, 1,
    511  1.1  christos // XXX	    &nsegs, BUS_DMA_NOWAIT | BUS_DMA_ZERO);
    512  1.1  christos 	    &nsegs, BUS_DMA_NOWAIT);
    513  1.1  christos 	if (error != 0)
    514  1.1  christos 		goto fail;
    515  1.1  christos 
    516  1.1  christos 	error = bus_dmamem_map(sc->sc_dmat, &sc->sc_seg, 1, size,
    517  1.1  christos 	    (void **)&sc->sc_descs, BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
    518  1.1  christos 	if (error != 0)
    519  1.1  christos 		goto fail;
    520  1.1  christos 
    521  1.7      matt 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_map, sc->sc_descs,
    522  1.7      matt 	    size, NULL, BUS_DMA_NOWAIT);
    523  1.1  christos 	if (error != 0)
    524  1.1  christos 		goto fail;
    525  1.1  christos 
    526  1.1  christos 	SIMPLEQ_INIT(&sc->sc_txbufs);
    527  1.1  christos 	for (i = 0; i < ATHN_NTXBUFS; i++) {
    528  1.1  christos 		bf = &sc->sc_txpool[i];
    529  1.1  christos 
    530  1.1  christos 		error = bus_dmamap_create(sc->sc_dmat, ATHN_TXBUFSZ,
    531  1.1  christos 		    AR5008_MAX_SCATTER, ATHN_TXBUFSZ, 0, BUS_DMA_NOWAIT,
    532  1.1  christos 		    &bf->bf_map);
    533  1.1  christos 		if (error != 0) {
    534  1.6  christos 			aprint_error_dev(sc->sc_dev,
    535  1.6  christos 			    "could not create Tx buf DMA map\n");
    536  1.1  christos 			goto fail;
    537  1.1  christos 		}
    538  1.1  christos 
    539  1.1  christos 		bf->bf_descs =
    540  1.1  christos 		    &((struct ar_tx_desc *)sc->sc_descs)[i * AR5008_MAX_SCATTER];
    541  1.1  christos 		bf->bf_daddr = sc->sc_map->dm_segs[0].ds_addr +
    542  1.1  christos 		    i * AR5008_MAX_SCATTER * sizeof(struct ar_tx_desc);
    543  1.1  christos 
    544  1.1  christos 		SIMPLEQ_INSERT_TAIL(&sc->sc_txbufs, bf, bf_list);
    545  1.1  christos 	}
    546  1.1  christos 	return 0;
    547  1.1  christos  fail:
    548  1.1  christos 	ar5008_tx_free(sc);
    549  1.1  christos 	return error;
    550  1.1  christos }
    551  1.1  christos 
    552  1.1  christos Static void
    553  1.1  christos ar5008_tx_free(struct athn_softc *sc)
    554  1.1  christos {
    555  1.1  christos 	struct athn_tx_buf *bf;
    556  1.1  christos 	int i;
    557  1.1  christos 
    558  1.1  christos 	for (i = 0; i < ATHN_NTXBUFS; i++) {
    559  1.1  christos 		bf = &sc->sc_txpool[i];
    560  1.1  christos 
    561  1.1  christos 		if (bf->bf_map != NULL)
    562  1.1  christos 			bus_dmamap_destroy(sc->sc_dmat, bf->bf_map);
    563  1.1  christos 	}
    564  1.1  christos 	/* Free Tx descriptors. */
    565  1.1  christos 	if (sc->sc_map != NULL) {
    566  1.1  christos 		if (sc->sc_descs != NULL) {
    567  1.1  christos 			bus_dmamap_unload(sc->sc_dmat, sc->sc_map);
    568  1.1  christos 			bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_descs,
    569  1.1  christos 			    ATHN_NTXBUFS * AR5008_MAX_SCATTER *
    570  1.1  christos 			    sizeof(struct ar_tx_desc));
    571  1.1  christos 			bus_dmamem_free(sc->sc_dmat, &sc->sc_seg, 1);
    572  1.1  christos 		}
    573  1.1  christos 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_map);
    574  1.1  christos 	}
    575  1.1  christos }
    576  1.1  christos 
    577  1.1  christos Static int
    578  1.1  christos ar5008_rx_alloc(struct athn_softc *sc)
    579  1.1  christos {
    580  1.1  christos 	struct athn_rxq *rxq = &sc->sc_rxq[0];
    581  1.1  christos 	struct athn_rx_buf *bf;
    582  1.1  christos 	struct ar_rx_desc *ds;
    583  1.1  christos 	bus_size_t size;
    584  1.1  christos 	int error, nsegs, i;
    585  1.1  christos 
    586  1.1  christos 	rxq->bf = malloc(ATHN_NRXBUFS * sizeof(*bf), M_DEVBUF,
    587  1.1  christos 	    M_NOWAIT | M_ZERO);
    588  1.1  christos 	if (rxq->bf == NULL)
    589  1.1  christos 		return ENOMEM;
    590  1.1  christos 
    591  1.1  christos 	size = ATHN_NRXBUFS * sizeof(struct ar_rx_desc);
    592  1.1  christos 
    593  1.1  christos 	error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
    594  1.1  christos 	    BUS_DMA_NOWAIT, &rxq->map);
    595  1.1  christos 	if (error != 0)
    596  1.1  christos 		goto fail;
    597  1.1  christos 
    598  1.1  christos 	error = bus_dmamem_alloc(sc->sc_dmat, size, 0, 0, &rxq->seg, 1,
    599  1.1  christos //	    &nsegs, BUS_DMA_NOWAIT | BUS_DMA_ZERO);
    600  1.1  christos 	    &nsegs, BUS_DMA_NOWAIT);
    601  1.1  christos 	if (error != 0)
    602  1.1  christos 		goto fail;
    603  1.1  christos 
    604  1.1  christos 	error = bus_dmamem_map(sc->sc_dmat, &rxq->seg, 1, size,
    605  1.1  christos 	    (void **)&rxq->descs, BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
    606  1.1  christos 	if (error != 0)
    607  1.1  christos 		goto fail;
    608  1.1  christos 
    609  1.8      matt 	error = bus_dmamap_load(sc->sc_dmat, rxq->map, rxq->descs,
    610  1.7      matt 	    size, NULL, BUS_DMA_NOWAIT);
    611  1.1  christos 	if (error != 0)
    612  1.1  christos 		goto fail;
    613  1.1  christos 
    614  1.1  christos 	for (i = 0; i < ATHN_NRXBUFS; i++) {
    615  1.1  christos 		bf = &rxq->bf[i];
    616  1.1  christos 		ds = &((struct ar_rx_desc *)rxq->descs)[i];
    617  1.1  christos 
    618  1.1  christos 		error = bus_dmamap_create(sc->sc_dmat, ATHN_RXBUFSZ, 1,
    619  1.1  christos 		    ATHN_RXBUFSZ, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
    620  1.1  christos 		    &bf->bf_map);
    621  1.1  christos 		if (error != 0) {
    622  1.6  christos 			aprint_error_dev(sc->sc_dev,
    623  1.6  christos 			    " could not create Rx buf DMA map\n");
    624  1.1  christos 			goto fail;
    625  1.1  christos 		}
    626  1.1  christos 		/*
    627  1.1  christos 		 * Assumes MCLGETI returns cache-line-size aligned buffers.
    628  1.1  christos 		 * XXX: does ours?
    629  1.1  christos 		 */
    630  1.1  christos 		bf->bf_m = MCLGETI(NULL, M_DONTWAIT, NULL, ATHN_RXBUFSZ);
    631  1.1  christos 		if (bf->bf_m == NULL) {
    632  1.6  christos 			aprint_error_dev(sc->sc_dev,
    633  1.6  christos 			    "could not allocate Rx mbuf\n");
    634  1.1  christos 			error = ENOBUFS;
    635  1.1  christos 			goto fail;
    636  1.1  christos 		}
    637  1.1  christos 
    638  1.1  christos 		error = bus_dmamap_load(sc->sc_dmat, bf->bf_map,
    639  1.1  christos 		    mtod(bf->bf_m, void *), ATHN_RXBUFSZ, NULL,
    640  1.1  christos 		    BUS_DMA_NOWAIT | BUS_DMA_READ);
    641  1.1  christos 		if (error != 0) {
    642  1.6  christos 			aprint_error_dev(sc->sc_dev,
    643  1.6  christos 			    "could not DMA map Rx buffer\n");
    644  1.1  christos 			goto fail;
    645  1.1  christos 		}
    646  1.1  christos 
    647  1.1  christos 		bus_dmamap_sync(sc->sc_dmat, bf->bf_map, 0, ATHN_RXBUFSZ,
    648  1.1  christos 		    BUS_DMASYNC_PREREAD);
    649  1.1  christos 
    650  1.1  christos 		bf->bf_desc = ds;
    651  1.1  christos 		bf->bf_daddr = rxq->map->dm_segs[0].ds_addr +
    652  1.1  christos 		    i * sizeof(struct ar_rx_desc);
    653  1.1  christos 	}
    654  1.1  christos 	return 0;
    655  1.1  christos  fail:
    656  1.1  christos 	ar5008_rx_free(sc);
    657  1.1  christos 	return error;
    658  1.1  christos }
    659  1.1  christos 
    660  1.1  christos Static void
    661  1.1  christos ar5008_rx_free(struct athn_softc *sc)
    662  1.1  christos {
    663  1.1  christos 	struct athn_rxq *rxq = &sc->sc_rxq[0];
    664  1.1  christos 	struct athn_rx_buf *bf;
    665  1.1  christos 	int i;
    666  1.1  christos 
    667  1.1  christos 	if (rxq->bf == NULL)
    668  1.1  christos 		return;
    669  1.1  christos 	for (i = 0; i < ATHN_NRXBUFS; i++) {
    670  1.1  christos 		bf = &rxq->bf[i];
    671  1.1  christos 
    672  1.1  christos 		if (bf->bf_map != NULL)
    673  1.1  christos 			bus_dmamap_destroy(sc->sc_dmat, bf->bf_map);
    674  1.1  christos 		if (bf->bf_m != NULL)
    675  1.1  christos 			m_freem(bf->bf_m);
    676  1.1  christos 	}
    677  1.1  christos 	free(rxq->bf, M_DEVBUF);
    678  1.1  christos 
    679  1.1  christos 	/* Free Rx descriptors. */
    680  1.1  christos 	if (rxq->map != NULL) {
    681  1.1  christos 		if (rxq->descs != NULL) {
    682  1.1  christos 			bus_dmamap_unload(sc->sc_dmat, rxq->map);
    683  1.1  christos 			bus_dmamem_unmap(sc->sc_dmat, (void *)rxq->descs,
    684  1.1  christos 			    ATHN_NRXBUFS * sizeof(struct ar_rx_desc));
    685  1.1  christos 			bus_dmamem_free(sc->sc_dmat, &rxq->seg, 1);
    686  1.1  christos 		}
    687  1.1  christos 		bus_dmamap_destroy(sc->sc_dmat, rxq->map);
    688  1.1  christos 	}
    689  1.1  christos }
    690  1.1  christos 
    691  1.1  christos Static void
    692  1.1  christos ar5008_rx_enable(struct athn_softc *sc)
    693  1.1  christos {
    694  1.1  christos 	struct athn_rxq *rxq = &sc->sc_rxq[0];
    695  1.1  christos 	struct athn_rx_buf *bf;
    696  1.1  christos 	struct ar_rx_desc *ds;
    697  1.1  christos 	int i;
    698  1.1  christos 
    699  1.1  christos 	/* Setup and link Rx descriptors. */
    700  1.1  christos 	SIMPLEQ_INIT(&rxq->head);
    701  1.1  christos 	rxq->lastds = NULL;
    702  1.1  christos 	for (i = 0; i < ATHN_NRXBUFS; i++) {
    703  1.1  christos 		bf = &rxq->bf[i];
    704  1.1  christos 		ds = bf->bf_desc;
    705  1.1  christos 
    706  1.1  christos 		memset(ds, 0, sizeof(*ds));
    707  1.1  christos 		ds->ds_data = bf->bf_map->dm_segs[0].ds_addr;
    708  1.1  christos 		ds->ds_ctl1 = SM(AR_RXC1_BUF_LEN, ATHN_RXBUFSZ);
    709  1.1  christos 
    710  1.1  christos 		if (rxq->lastds != NULL) {
    711  1.1  christos 			((struct ar_rx_desc *)rxq->lastds)->ds_link =
    712  1.1  christos 			    bf->bf_daddr;
    713  1.1  christos 		}
    714  1.1  christos 		SIMPLEQ_INSERT_TAIL(&rxq->head, bf, bf_list);
    715  1.1  christos 		rxq->lastds = ds;
    716  1.1  christos 	}
    717  1.1  christos 	bus_dmamap_sync(sc->sc_dmat, rxq->map, 0, rxq->map->dm_mapsize,
    718  1.1  christos 	    BUS_DMASYNC_PREREAD);
    719  1.1  christos 
    720  1.1  christos 	/* Enable Rx. */
    721  1.1  christos 	AR_WRITE(sc, AR_RXDP, SIMPLEQ_FIRST(&rxq->head)->bf_daddr);
    722  1.1  christos 	AR_WRITE(sc, AR_CR, AR_CR_RXE);
    723  1.1  christos 	AR_WRITE_BARRIER(sc);
    724  1.1  christos }
    725  1.1  christos 
    726  1.1  christos Static void
    727  1.1  christos ar5008_rx_radiotap(struct athn_softc *sc, struct mbuf *m,
    728  1.1  christos     struct ar_rx_desc *ds)
    729  1.1  christos {
    730  1.1  christos 	struct athn_rx_radiotap_header *tap = &sc->sc_rxtap;
    731  1.1  christos 	struct ieee80211com *ic = &sc->sc_ic;
    732  1.1  christos 	uint64_t tsf;
    733  1.1  christos 	uint32_t tstamp;
    734  1.1  christos 	uint8_t rate;
    735  1.1  christos 
    736  1.1  christos 	/* Extend the 15-bit timestamp from Rx descriptor to 64-bit TSF. */
    737  1.1  christos 	tstamp = ds->ds_status2;
    738  1.1  christos 	tsf = AR_READ(sc, AR_TSF_U32);
    739  1.1  christos 	tsf = tsf << 32 | AR_READ(sc, AR_TSF_L32);
    740  1.1  christos 	if ((tsf & 0x7fff) < tstamp)
    741  1.1  christos 		tsf -= 0x8000;
    742  1.1  christos 	tsf = (tsf & ~0x7fff) | tstamp;
    743  1.1  christos 
    744  1.1  christos 	tap->wr_flags = IEEE80211_RADIOTAP_F_FCS;
    745  1.1  christos 	tap->wr_tsft = htole64(tsf);
    746  1.1  christos 	tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
    747  1.1  christos 	tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
    748  1.1  christos 	tap->wr_dbm_antsignal = MS(ds->ds_status4, AR_RXS4_RSSI_COMBINED);
    749  1.1  christos 	/* XXX noise. */
    750  1.1  christos 	tap->wr_antenna = MS(ds->ds_status3, AR_RXS3_ANTENNA);
    751  1.1  christos 	tap->wr_rate = 0;	/* In case it can't be found below. */
    752  1.1  christos 	if (AR_SREV_5416_20_OR_LATER(sc))
    753  1.1  christos 		rate = MS(ds->ds_status0, AR_RXS0_RATE);
    754  1.1  christos 	else
    755  1.1  christos 		rate = MS(ds->ds_status3, AR_RXS3_RATE);
    756  1.1  christos 	if (rate & 0x80) {		/* HT. */
    757  1.1  christos 		/* Bit 7 set means HT MCS instead of rate. */
    758  1.1  christos 		tap->wr_rate = rate;
    759  1.1  christos 		if (!(ds->ds_status3 & AR_RXS3_GI))
    760  1.1  christos 			tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
    761  1.1  christos 
    762  1.1  christos 	}
    763  1.1  christos 	else if (rate & 0x10) {	/* CCK. */
    764  1.1  christos 		if (rate & 0x04)
    765  1.1  christos 			tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
    766  1.1  christos 		switch (rate & ~0x14) {
    767  1.1  christos 		case 0xb: tap->wr_rate =   2; break;
    768  1.1  christos 		case 0xa: tap->wr_rate =   4; break;
    769  1.1  christos 		case 0x9: tap->wr_rate =  11; break;
    770  1.1  christos 		case 0x8: tap->wr_rate =  22; break;
    771  1.1  christos 		}
    772  1.1  christos 	}
    773  1.1  christos 	else {			/* OFDM. */
    774  1.1  christos 		switch (rate) {
    775  1.1  christos 		case 0xb: tap->wr_rate =  12; break;
    776  1.1  christos 		case 0xf: tap->wr_rate =  18; break;
    777  1.1  christos 		case 0xa: tap->wr_rate =  24; break;
    778  1.1  christos 		case 0xe: tap->wr_rate =  36; break;
    779  1.1  christos 		case 0x9: tap->wr_rate =  48; break;
    780  1.1  christos 		case 0xd: tap->wr_rate =  72; break;
    781  1.1  christos 		case 0x8: tap->wr_rate =  96; break;
    782  1.1  christos 		case 0xc: tap->wr_rate = 108; break;
    783  1.1  christos 		}
    784  1.1  christos 	}
    785  1.1  christos 	bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
    786  1.1  christos }
    787  1.1  christos 
    788  1.1  christos static __inline int
    789  1.1  christos ar5008_rx_process(struct athn_softc *sc)
    790  1.1  christos {
    791  1.1  christos 	struct ieee80211com *ic = &sc->sc_ic;
    792  1.1  christos 	struct ifnet *ifp = &sc->sc_if;
    793  1.1  christos 	struct athn_rxq *rxq = &sc->sc_rxq[0];
    794  1.1  christos 	struct athn_rx_buf *bf, *nbf;
    795  1.1  christos 	struct ar_rx_desc *ds;
    796  1.1  christos 	struct ieee80211_frame *wh;
    797  1.1  christos 	struct ieee80211_node *ni;
    798  1.1  christos 	struct mbuf *m, *m1;
    799  1.1  christos 	u_int32_t rstamp;
    800  1.1  christos 	int error, len, rssi;
    801  1.1  christos 
    802  1.1  christos 	bf = SIMPLEQ_FIRST(&rxq->head);
    803  1.1  christos 	if (__predict_false(bf == NULL)) {	/* Should not happen. */
    804  1.6  christos 		aprint_error_dev(sc->sc_dev, "Rx queue is empty!\n");
    805  1.1  christos 		return ENOENT;
    806  1.1  christos 	}
    807  1.1  christos 	ds = bf->bf_desc;
    808  1.1  christos 
    809  1.1  christos 	if (!(ds->ds_status8 & AR_RXS8_DONE)) {
    810  1.1  christos 		/*
    811  1.1  christos 		 * On some parts, the status words can get corrupted
    812  1.1  christos 		 * (including the "done" bit), so we check the next
    813  1.1  christos 		 * descriptor "done" bit.  If it is set, it is a good
    814  1.1  christos 		 * indication that the status words are corrupted, so
    815  1.1  christos 		 * we skip this descriptor and drop the frame.
    816  1.1  christos 		 */
    817  1.1  christos 		nbf = SIMPLEQ_NEXT(bf, bf_list);
    818  1.1  christos 		if (nbf != NULL &&
    819  1.1  christos 		    (((struct ar_rx_desc *)nbf->bf_desc)->ds_status8 &
    820  1.1  christos 		     AR_RXS8_DONE)) {
    821  1.1  christos 			DPRINTFN(DBG_RX, sc,
    822  1.1  christos 			    "corrupted descriptor status=0x%x\n",
    823  1.1  christos 			    ds->ds_status8);
    824  1.1  christos 			/* HW will not "move" RXDP in this case, so do it. */
    825  1.1  christos 			AR_WRITE(sc, AR_RXDP, nbf->bf_daddr);
    826  1.1  christos 			AR_WRITE_BARRIER(sc);
    827  1.1  christos 			ifp->if_ierrors++;
    828  1.1  christos 			goto skip;
    829  1.1  christos 		}
    830  1.1  christos 		return EBUSY;
    831  1.1  christos 	}
    832  1.1  christos 
    833  1.1  christos 	if (__predict_false(ds->ds_status1 & AR_RXS1_MORE)) {
    834  1.1  christos 		/* Drop frames that span multiple Rx descriptors. */
    835  1.1  christos 		DPRINTFN(DBG_RX, sc, "dropping split frame\n");
    836  1.1  christos 		ifp->if_ierrors++;
    837  1.1  christos 		goto skip;
    838  1.1  christos 	}
    839  1.1  christos 	if (!(ds->ds_status8 & AR_RXS8_FRAME_OK)) {
    840  1.1  christos 		if (ds->ds_status8 & AR_RXS8_CRC_ERR)
    841  1.1  christos 			DPRINTFN(DBG_RX, sc, "CRC error\n");
    842  1.1  christos 		else if (ds->ds_status8 & AR_RXS8_PHY_ERR)
    843  1.1  christos 			DPRINTFN(DBG_RX, sc, "PHY error=0x%x\n",
    844  1.1  christos 			    MS(ds->ds_status8, AR_RXS8_PHY_ERR_CODE));
    845  1.1  christos 		else if (ds->ds_status8 & AR_RXS8_DECRYPT_CRC_ERR)
    846  1.1  christos 			DPRINTFN(DBG_RX, sc, "Decryption CRC error\n");
    847  1.1  christos 		else if (ds->ds_status8 & AR_RXS8_MICHAEL_ERR) {
    848  1.1  christos 			DPRINTFN(DBG_RX, sc, "Michael MIC failure\n");
    849  1.1  christos 
    850  1.1  christos 			len = MS(ds->ds_status1, AR_RXS1_DATA_LEN);
    851  1.1  christos 			m = bf->bf_m;
    852  1.1  christos 			m->m_pkthdr.rcvif = ifp;
    853  1.1  christos 			m->m_pkthdr.len = m->m_len = len;
    854  1.1  christos 			wh = mtod(m, struct ieee80211_frame *);
    855  1.1  christos 
    856  1.1  christos 			/* Report Michael MIC failures to net80211. */
    857  1.1  christos 			ieee80211_notify_michael_failure(ic, wh, 0 /* XXX: keyix */);
    858  1.1  christos 		}
    859  1.1  christos 		ifp->if_ierrors++;
    860  1.1  christos 		goto skip;
    861  1.1  christos 	}
    862  1.1  christos 
    863  1.1  christos 	len = MS(ds->ds_status1, AR_RXS1_DATA_LEN);
    864  1.1  christos 	if (__predict_false(len < (int)IEEE80211_MIN_LEN || len > ATHN_RXBUFSZ)) {
    865  1.1  christos 		DPRINTFN(DBG_RX, sc, "corrupted descriptor length=%d\n", len);
    866  1.1  christos 		ifp->if_ierrors++;
    867  1.1  christos 		goto skip;
    868  1.1  christos 	}
    869  1.1  christos 
    870  1.1  christos 	/* Allocate a new Rx buffer. */
    871  1.1  christos 	m1 = MCLGETI(NULL, M_DONTWAIT, NULL, ATHN_RXBUFSZ);
    872  1.1  christos 	if (__predict_false(m1 == NULL)) {
    873  1.1  christos 		ic->ic_stats.is_rx_nobuf++;
    874  1.1  christos 		ifp->if_ierrors++;
    875  1.1  christos 		goto skip;
    876  1.1  christos 	}
    877  1.1  christos 
    878  1.1  christos 	/* Sync and unmap the old Rx buffer. */
    879  1.1  christos 	bus_dmamap_sync(sc->sc_dmat, bf->bf_map, 0, ATHN_RXBUFSZ,
    880  1.1  christos 	    BUS_DMASYNC_POSTREAD);
    881  1.1  christos 	bus_dmamap_unload(sc->sc_dmat, bf->bf_map);
    882  1.1  christos 
    883  1.1  christos 	/* Map the new Rx buffer. */
    884  1.1  christos 	error = bus_dmamap_load(sc->sc_dmat, bf->bf_map, mtod(m1, void *),
    885  1.1  christos 	    ATHN_RXBUFSZ, NULL, BUS_DMA_NOWAIT | BUS_DMA_READ);
    886  1.1  christos 	if (__predict_false(error != 0)) {
    887  1.1  christos 		m_freem(m1);
    888  1.1  christos 
    889  1.1  christos 		/* Remap the old Rx buffer or panic. */
    890  1.1  christos 		error = bus_dmamap_load(sc->sc_dmat, bf->bf_map,
    891  1.1  christos 		    mtod(bf->bf_m, void *), ATHN_RXBUFSZ, NULL,
    892  1.1  christos 		    BUS_DMA_NOWAIT | BUS_DMA_READ);
    893  1.1  christos 		KASSERT(error != 0);
    894  1.1  christos 		ifp->if_ierrors++;
    895  1.1  christos 		goto skip;
    896  1.1  christos 	}
    897  1.1  christos 
    898  1.1  christos 	bus_dmamap_sync(sc->sc_dmat, bf->bf_map, 0, ATHN_RXBUFSZ,
    899  1.1  christos 	    BUS_DMASYNC_PREREAD);
    900  1.1  christos 
    901  1.1  christos 	/* Write physical address of new Rx buffer. */
    902  1.1  christos 	ds->ds_data = bf->bf_map->dm_segs[0].ds_addr;
    903  1.1  christos 
    904  1.1  christos 	m = bf->bf_m;
    905  1.1  christos 	bf->bf_m = m1;
    906  1.1  christos 
    907  1.1  christos 	/* Finalize mbuf. */
    908  1.1  christos 	m->m_pkthdr.rcvif = ifp;
    909  1.1  christos 	m->m_pkthdr.len = m->m_len = len;
    910  1.1  christos 
    911  1.1  christos 	/* Grab a reference to the source node. */
    912  1.1  christos 	wh = mtod(m, struct ieee80211_frame *);
    913  1.1  christos 	ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
    914  1.1  christos 
    915  1.1  christos 	/* Remove any HW padding after the 802.11 header. */
    916  1.1  christos 	if (!(wh->i_fc[0] & IEEE80211_FC0_TYPE_CTL)) {
    917  1.1  christos 		u_int hdrlen = ieee80211_anyhdrsize(wh);
    918  1.1  christos 		if (hdrlen & 3) {
    919  1.1  christos 			ovbcopy(wh, (uint8_t *)wh + 2, hdrlen);
    920  1.1  christos 			m_adj(m, 2);
    921  1.1  christos 		}
    922  1.1  christos 	}
    923  1.1  christos 	if (__predict_false(sc->sc_drvbpf != NULL))
    924  1.1  christos 		ar5008_rx_radiotap(sc, m, ds);
    925  1.1  christos 
    926  1.1  christos 	/* Trim 802.11 FCS after radiotap. */
    927  1.1  christos 	m_adj(m, -IEEE80211_CRC_LEN);
    928  1.1  christos 
    929  1.1  christos 	/* Send the frame to the 802.11 layer. */
    930  1.1  christos 	rssi = MS(ds->ds_status4, AR_RXS4_RSSI_COMBINED);
    931  1.1  christos 	rstamp = ds->ds_status2;
    932  1.1  christos 	ieee80211_input(ic, m, ni, rssi, rstamp);
    933  1.1  christos 
    934  1.1  christos 	/* Node is no longer needed. */
    935  1.1  christos 	ieee80211_free_node(ni);
    936  1.1  christos 
    937  1.1  christos  skip:
    938  1.1  christos 	/* Unlink this descriptor from head. */
    939  1.1  christos 	SIMPLEQ_REMOVE_HEAD(&rxq->head, bf_list);
    940  1.1  christos 	memset(&ds->ds_status0, 0, 36);	/* XXX Really needed? */
    941  1.1  christos 	ds->ds_status8 &= ~AR_RXS8_DONE;
    942  1.1  christos 	ds->ds_link = 0;
    943  1.1  christos 
    944  1.1  christos 	/* Re-use this descriptor and link it to tail. */
    945  1.1  christos 	if (__predict_true(!SIMPLEQ_EMPTY(&rxq->head)))
    946  1.1  christos 		((struct ar_rx_desc *)rxq->lastds)->ds_link = bf->bf_daddr;
    947  1.1  christos 	else
    948  1.1  christos 		AR_WRITE(sc, AR_RXDP, bf->bf_daddr);
    949  1.1  christos 	SIMPLEQ_INSERT_TAIL(&rxq->head, bf, bf_list);
    950  1.1  christos 	rxq->lastds = ds;
    951  1.1  christos 
    952  1.1  christos 	/* Re-enable Rx. */
    953  1.1  christos 	AR_WRITE(sc, AR_CR, AR_CR_RXE);
    954  1.1  christos 	AR_WRITE_BARRIER(sc);
    955  1.1  christos 	return 0;
    956  1.1  christos }
    957  1.1  christos 
    958  1.1  christos Static void
    959  1.1  christos ar5008_rx_intr(struct athn_softc *sc)
    960  1.1  christos {
    961  1.1  christos 
    962  1.1  christos 	while (ar5008_rx_process(sc) == 0)
    963  1.1  christos 		continue;
    964  1.1  christos }
    965  1.1  christos 
    966  1.1  christos Static int
    967  1.1  christos ar5008_tx_process(struct athn_softc *sc, int qid)
    968  1.1  christos {
    969  1.1  christos 	struct ifnet *ifp = &sc->sc_if;
    970  1.1  christos 	struct athn_txq *txq = &sc->sc_txq[qid];
    971  1.1  christos 	struct athn_node *an;
    972  1.1  christos 	struct athn_tx_buf *bf;
    973  1.1  christos 	struct ar_tx_desc *ds;
    974  1.1  christos 	uint8_t failcnt;
    975  1.1  christos 
    976  1.1  christos 	bf = SIMPLEQ_FIRST(&txq->head);
    977  1.1  christos 	if (bf == NULL)
    978  1.1  christos 		return ENOENT;
    979  1.1  christos 	/* Get descriptor of last DMA segment. */
    980  1.1  christos 	ds = &((struct ar_tx_desc *)bf->bf_descs)[bf->bf_map->dm_nsegs - 1];
    981  1.1  christos 
    982  1.1  christos 	if (!(ds->ds_status9 & AR_TXS9_DONE))
    983  1.1  christos 		return EBUSY;
    984  1.1  christos 
    985  1.1  christos 	SIMPLEQ_REMOVE_HEAD(&txq->head, bf_list);
    986  1.1  christos 	ifp->if_opackets++;
    987  1.1  christos 
    988  1.1  christos 	sc->sc_tx_timer = 0;
    989  1.1  christos 
    990  1.1  christos 	if (ds->ds_status1 & AR_TXS1_EXCESSIVE_RETRIES)
    991  1.1  christos 		ifp->if_oerrors++;
    992  1.1  christos 
    993  1.1  christos 	if (ds->ds_status1 & AR_TXS1_UNDERRUN)
    994  1.1  christos 		athn_inc_tx_trigger_level(sc);
    995  1.1  christos 
    996  1.1  christos 	an = (struct athn_node *)bf->bf_ni;
    997  1.1  christos 	/*
    998  1.1  christos 	 * NB: the data fail count contains the number of un-acked tries
    999  1.1  christos 	 * for the final series used.  We must add the number of tries for
   1000  1.1  christos 	 * each series that was fully processed.
   1001  1.1  christos 	 */
   1002  1.1  christos 	failcnt  = MS(ds->ds_status1, AR_TXS1_DATA_FAIL_CNT);
   1003  1.1  christos 	/* NB: Assume two tries per series. */
   1004  1.1  christos 	failcnt += MS(ds->ds_status9, AR_TXS9_FINAL_IDX) * 2;
   1005  1.1  christos 
   1006  1.1  christos 	/* Update rate control statistics. */
   1007  1.1  christos 	an->amn.amn_txcnt++;
   1008  1.1  christos 	if (failcnt > 0)
   1009  1.1  christos 		an->amn.amn_retrycnt++;
   1010  1.1  christos 
   1011  1.1  christos 	DPRINTFN(DBG_TX, sc, "Tx done qid=%d status1=%d fail count=%d\n",
   1012  1.1  christos 	    qid, ds->ds_status1, failcnt);
   1013  1.1  christos 
   1014  1.1  christos 	bus_dmamap_sync(sc->sc_dmat, bf->bf_map, 0, bf->bf_map->dm_mapsize,
   1015  1.1  christos 	    BUS_DMASYNC_POSTWRITE);
   1016  1.1  christos 	bus_dmamap_unload(sc->sc_dmat, bf->bf_map);
   1017  1.1  christos 
   1018  1.1  christos 	m_freem(bf->bf_m);
   1019  1.1  christos 	bf->bf_m = NULL;
   1020  1.1  christos 	ieee80211_free_node(bf->bf_ni);
   1021  1.1  christos 	bf->bf_ni = NULL;
   1022  1.1  christos 
   1023  1.1  christos 	/* Link Tx buffer back to global free list. */
   1024  1.1  christos 	SIMPLEQ_INSERT_TAIL(&sc->sc_txbufs, bf, bf_list);
   1025  1.1  christos 	return 0;
   1026  1.1  christos }
   1027  1.1  christos 
   1028  1.1  christos Static void
   1029  1.1  christos ar5008_tx_intr(struct athn_softc *sc)
   1030  1.1  christos {
   1031  1.1  christos 	struct ifnet *ifp = &sc->sc_if;
   1032  1.1  christos 	uint16_t mask = 0;
   1033  1.1  christos 	uint32_t reg;
   1034  1.1  christos 	int qid;
   1035  1.1  christos 
   1036  1.1  christos 	reg = AR_READ(sc, AR_ISR_S0_S);
   1037  1.1  christos 	mask |= MS(reg, AR_ISR_S0_QCU_TXOK);
   1038  1.1  christos 	mask |= MS(reg, AR_ISR_S0_QCU_TXDESC);
   1039  1.1  christos 
   1040  1.1  christos 	reg = AR_READ(sc, AR_ISR_S1_S);
   1041  1.1  christos 	mask |= MS(reg, AR_ISR_S1_QCU_TXERR);
   1042  1.1  christos 	mask |= MS(reg, AR_ISR_S1_QCU_TXEOL);
   1043  1.1  christos 
   1044  1.1  christos 	DPRINTFN(DBG_TX, sc, "Tx interrupt mask=0x%x\n", mask);
   1045  1.1  christos 	for (qid = 0; mask != 0; mask >>= 1, qid++) {
   1046  1.1  christos 		if (mask & 1)
   1047  1.1  christos 			while (ar5008_tx_process(sc, qid) == 0);
   1048  1.1  christos 	}
   1049  1.1  christos 	if (!SIMPLEQ_EMPTY(&sc->sc_txbufs)) {
   1050  1.1  christos 		ifp->if_flags &= ~IFF_OACTIVE;
   1051  1.1  christos 		ifp->if_start(ifp);
   1052  1.1  christos 	}
   1053  1.1  christos }
   1054  1.1  christos 
   1055  1.1  christos #ifndef IEEE80211_STA_ONLY
   1056  1.1  christos /*
   1057  1.1  christos  * Process Software Beacon Alert interrupts.
   1058  1.1  christos  */
   1059  1.1  christos Static int
   1060  1.1  christos ar5008_swba_intr(struct athn_softc *sc)
   1061  1.1  christos {
   1062  1.1  christos 	struct ieee80211com *ic = &sc->sc_ic;
   1063  1.1  christos 	struct ifnet *ifp = &sc->sc_if;
   1064  1.1  christos 	struct ieee80211_node *ni = ic->ic_bss;
   1065  1.1  christos 	struct athn_tx_buf *bf = sc->sc_bcnbuf;
   1066  1.1  christos 	struct ieee80211_frame *wh;
   1067  1.1  christos 	struct ieee80211_beacon_offsets bo;
   1068  1.1  christos 	struct ar_tx_desc *ds;
   1069  1.1  christos 	struct mbuf *m;
   1070  1.1  christos 	uint8_t ridx, hwrate;
   1071  1.1  christos 	int error, totlen;
   1072  1.1  christos 
   1073  1.1  christos #if notyet
   1074  1.1  christos 	if (ic->ic_tim_mcast_pending &&
   1075  1.1  christos 	    IF_IS_EMPTY(&ni->ni_savedq) &&
   1076  1.1  christos 	    SIMPLEQ_EMPTY(&sc->sc_txq[ATHN_QID_CAB].head))
   1077  1.1  christos 		ic->ic_tim_mcast_pending = 0;
   1078  1.1  christos #endif
   1079  1.1  christos 	if (ic->ic_dtim_count == 0)
   1080  1.1  christos 		ic->ic_dtim_count = ic->ic_dtim_period - 1;
   1081  1.1  christos 	else
   1082  1.1  christos 		ic->ic_dtim_count--;
   1083  1.1  christos 
   1084  1.1  christos 	/* Make sure previous beacon has been sent. */
   1085  1.1  christos 	if (athn_tx_pending(sc, ATHN_QID_BEACON)) {
   1086  1.1  christos 		DPRINTFN(DBG_INTR, sc, "beacon stuck\n");
   1087  1.1  christos 		return EBUSY;
   1088  1.1  christos 	}
   1089  1.1  christos 	/* Get new beacon. */
   1090  1.1  christos 	m = ieee80211_beacon_alloc(ic, ic->ic_bss, &bo);
   1091  1.1  christos 	if (__predict_false(m == NULL))
   1092  1.1  christos 		return ENOBUFS;
   1093  1.1  christos 	/* Assign sequence number. */
   1094  1.1  christos 	/* XXX: use non-QoS tid? */
   1095  1.1  christos 	wh = mtod(m, struct ieee80211_frame *);
   1096  1.1  christos 	*(uint16_t *)&wh->i_seq[0] =
   1097  1.1  christos 	    htole16(ic->ic_bss->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT);
   1098  1.1  christos 	ic->ic_bss->ni_txseqs[0]++;
   1099  1.1  christos 
   1100  1.1  christos 	/* Unmap and free old beacon if any. */
   1101  1.1  christos 	if (__predict_true(bf->bf_m != NULL)) {
   1102  1.1  christos 		bus_dmamap_sync(sc->sc_dmat, bf->bf_map, 0,
   1103  1.1  christos 		    bf->bf_map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
   1104  1.1  christos 		bus_dmamap_unload(sc->sc_dmat, bf->bf_map);
   1105  1.1  christos 		m_freem(bf->bf_m);
   1106  1.1  christos 		bf->bf_m = NULL;
   1107  1.1  christos 	}
   1108  1.1  christos 	/* DMA map new beacon. */
   1109  1.1  christos 	error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_map, m,
   1110  1.1  christos 	    BUS_DMA_NOWAIT | BUS_DMA_WRITE);
   1111  1.1  christos 	if (__predict_false(error != 0)) {
   1112  1.1  christos 		m_freem(m);
   1113  1.1  christos 		return error;
   1114  1.1  christos 	}
   1115  1.1  christos 	bf->bf_m = m;
   1116  1.1  christos 
   1117  1.1  christos 	/* Setup Tx descriptor (simplified ar5008_tx()). */
   1118  1.1  christos 	ds = bf->bf_descs;
   1119  1.1  christos 	memset(ds, 0, sizeof(*ds));
   1120  1.1  christos 
   1121  1.1  christos 	totlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
   1122  1.1  christos 	ds->ds_ctl0 = SM(AR_TXC0_FRAME_LEN, totlen);
   1123  1.1  christos 	ds->ds_ctl0 |= SM(AR_TXC0_XMIT_POWER, AR_MAX_RATE_POWER);
   1124  1.1  christos 	ds->ds_ctl1 = SM(AR_TXC1_FRAME_TYPE, AR_FRAME_TYPE_BEACON);
   1125  1.1  christos 	ds->ds_ctl1 |= AR_TXC1_NO_ACK;
   1126  1.1  christos 	ds->ds_ctl6 = SM(AR_TXC6_ENCR_TYPE, AR_ENCR_TYPE_CLEAR);
   1127  1.1  christos 
   1128  1.1  christos 	/* Write number of tries. */
   1129  1.1  christos 	ds->ds_ctl2 = SM(AR_TXC2_XMIT_DATA_TRIES0, 1);
   1130  1.1  christos 
   1131  1.1  christos 	/* Write Tx rate. */
   1132  1.1  christos 	ridx = (ic->ic_curmode == IEEE80211_MODE_11A) ?
   1133  1.1  christos 	    ATHN_RIDX_OFDM6 : ATHN_RIDX_CCK1;
   1134  1.1  christos 	hwrate = athn_rates[ridx].hwrate;
   1135  1.1  christos 	ds->ds_ctl3 = SM(AR_TXC3_XMIT_RATE0, hwrate);
   1136  1.1  christos 
   1137  1.1  christos 	/* Write Tx chains. */
   1138  1.1  christos 	ds->ds_ctl7 = SM(AR_TXC7_CHAIN_SEL0, sc->sc_txchainmask);
   1139  1.1  christos 
   1140  1.1  christos 	ds->ds_data = bf->bf_map->dm_segs[0].ds_addr;
   1141  1.1  christos 	/* Segment length must be a multiple of 4. */
   1142  1.1  christos 	ds->ds_ctl1 |= SM(AR_TXC1_BUF_LEN,
   1143  1.1  christos 	    (bf->bf_map->dm_segs[0].ds_len + 3) & ~3);
   1144  1.1  christos 
   1145  1.1  christos 	bus_dmamap_sync(sc->sc_dmat, bf->bf_map, 0, bf->bf_map->dm_mapsize,
   1146  1.1  christos 	    BUS_DMASYNC_PREWRITE);
   1147  1.1  christos 
   1148  1.1  christos 	/* Stop Tx DMA before putting the new beacon on the queue. */
   1149  1.1  christos 	athn_stop_tx_dma(sc, ATHN_QID_BEACON);
   1150  1.1  christos 
   1151  1.1  christos 	AR_WRITE(sc, AR_QTXDP(ATHN_QID_BEACON), bf->bf_daddr);
   1152  1.1  christos 
   1153  1.1  christos 	for(;;) {
   1154  1.1  christos 		if (SIMPLEQ_EMPTY(&sc->sc_txbufs))
   1155  1.1  christos 			break;
   1156  1.1  christos 
   1157  1.1  christos 		IF_DEQUEUE(&ni->ni_savedq, m);
   1158  1.1  christos 		if (m == NULL)
   1159  1.1  christos 			break;
   1160  1.1  christos 		if (!IF_IS_EMPTY(&ni->ni_savedq)) {
   1161  1.1  christos 			/* more queued frames, set the more data bit */
   1162  1.1  christos 			wh = mtod(m, struct ieee80211_frame *);
   1163  1.1  christos 			wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
   1164  1.1  christos 		}
   1165  1.1  christos 
   1166  1.1  christos 		if (sc->sc_ops.tx(sc, m, ni, ATHN_TXFLAG_CAB) != 0) {
   1167  1.1  christos 			ieee80211_free_node(ni);
   1168  1.1  christos 			ifp->if_oerrors++;
   1169  1.1  christos 			break;
   1170  1.1  christos 		}
   1171  1.1  christos 	}
   1172  1.1  christos 
   1173  1.1  christos 	/* Kick Tx. */
   1174  1.1  christos 	AR_WRITE(sc, AR_Q_TXE, 1 << ATHN_QID_BEACON);
   1175  1.1  christos 	AR_WRITE_BARRIER(sc);
   1176  1.1  christos 	return 0;
   1177  1.1  christos }
   1178  1.1  christos #endif
   1179  1.1  christos 
   1180  1.1  christos Static int
   1181  1.1  christos ar5008_intr(struct athn_softc *sc)
   1182  1.1  christos {
   1183  1.4  christos 	uint32_t intr, intr5, sync;
   1184  1.1  christos 
   1185  1.1  christos 	/* Get pending interrupts. */
   1186  1.1  christos 	intr = AR_READ(sc, AR_INTR_ASYNC_CAUSE);
   1187  1.1  christos 	if (!(intr & AR_INTR_MAC_IRQ) || intr == AR_INTR_SPURIOUS) {
   1188  1.1  christos 		intr = AR_READ(sc, AR_INTR_SYNC_CAUSE);
   1189  1.1  christos 		if (intr == AR_INTR_SPURIOUS || (intr & sc->sc_isync) == 0)
   1190  1.1  christos 			return 0;	/* Not for us. */
   1191  1.1  christos 	}
   1192  1.1  christos 
   1193  1.1  christos 	if ((AR_READ(sc, AR_INTR_ASYNC_CAUSE) & AR_INTR_MAC_IRQ) &&
   1194  1.1  christos 	    (AR_READ(sc, AR_RTC_STATUS) & AR_RTC_STATUS_M) == AR_RTC_STATUS_ON)
   1195  1.1  christos 		intr = AR_READ(sc, AR_ISR);
   1196  1.1  christos 	else
   1197  1.1  christos 		intr = 0;
   1198  1.1  christos 	sync = AR_READ(sc, AR_INTR_SYNC_CAUSE) & sc->sc_isync;
   1199  1.1  christos 	if (intr == 0 && sync == 0)
   1200  1.1  christos 		return 0;	/* Not for us. */
   1201  1.1  christos 
   1202  1.1  christos 	if (intr != 0) {
   1203  1.1  christos 		if (intr & AR_ISR_BCNMISC) {
   1204  1.4  christos 			uint32_t intr2 = AR_READ(sc, AR_ISR_S2);
   1205  1.1  christos #if notyet
   1206  1.1  christos 			if (intr2 & AR_ISR_S2_TIM)
   1207  1.1  christos 				/* TBD */;
   1208  1.1  christos 			if (intr2 & AR_ISR_S2_TSFOOR)
   1209  1.1  christos 				/* TBD */;
   1210  1.4  christos #else
   1211  1.4  christos 			__USE(intr2);
   1212  1.1  christos #endif
   1213  1.1  christos 		}
   1214  1.1  christos 		intr = AR_READ(sc, AR_ISR_RAC);
   1215  1.1  christos 		if (intr == AR_INTR_SPURIOUS)
   1216  1.1  christos 			return 1;
   1217  1.1  christos 
   1218  1.1  christos #ifndef IEEE80211_STA_ONLY
   1219  1.1  christos 		if (intr & AR_ISR_SWBA)
   1220  1.1  christos 			ar5008_swba_intr(sc);
   1221  1.1  christos #endif
   1222  1.1  christos 		if (intr & (AR_ISR_RXMINTR | AR_ISR_RXINTM))
   1223  1.1  christos 			ar5008_rx_intr(sc);
   1224  1.1  christos 		if (intr & (AR_ISR_RXOK | AR_ISR_RXERR | AR_ISR_RXORN))
   1225  1.1  christos 			ar5008_rx_intr(sc);
   1226  1.1  christos 
   1227  1.1  christos 		if (intr & (AR_ISR_TXOK | AR_ISR_TXDESC |
   1228  1.1  christos 		    AR_ISR_TXERR | AR_ISR_TXEOL))
   1229  1.1  christos 			ar5008_tx_intr(sc);
   1230  1.1  christos 
   1231  1.1  christos 		intr5 = AR_READ(sc, AR_ISR_S5_S);
   1232  1.1  christos 		if (intr & AR_ISR_GENTMR) {
   1233  1.1  christos 			if (intr5 & AR_ISR_GENTMR) {
   1234  1.1  christos 				DPRINTFN(DBG_INTR, sc,
   1235  1.1  christos 				    "GENTMR trigger=%d thresh=%d\n",
   1236  1.1  christos 				    MS(intr5, AR_ISR_S5_GENTIMER_TRIG),
   1237  1.1  christos 				    MS(intr5, AR_ISR_S5_GENTIMER_THRESH));
   1238  1.1  christos 			}
   1239  1.1  christos 		}
   1240  1.1  christos #if notyet
   1241  1.1  christos 		if (intr5 & AR_ISR_S5_TIM_TIMER) {
   1242  1.1  christos 			/* TBD */;
   1243  1.1  christos 		}
   1244  1.1  christos #endif
   1245  1.1  christos 	}
   1246  1.1  christos 	if (sync != 0) {
   1247  1.1  christos #if notyet
   1248  1.1  christos 		if (sync &
   1249  1.1  christos 		    (AR_INTR_SYNC_HOST1_FATAL | AR_INTR_SYNC_HOST1_PERR)) {
   1250  1.1  christos 			/* TBD */;
   1251  1.1  christos 		}
   1252  1.1  christos #endif
   1253  1.1  christos 		if (sync & AR_INTR_SYNC_RADM_CPL_TIMEOUT) {
   1254  1.1  christos 			AR_WRITE(sc, AR_RC, AR_RC_HOSTIF);
   1255  1.1  christos 			AR_WRITE(sc, AR_RC, 0);
   1256  1.1  christos 		}
   1257  1.1  christos 
   1258  1.1  christos 		if ((sc->sc_flags & ATHN_FLAG_RFSILENT) &&
   1259  1.1  christos 		    (sync & AR_INTR_SYNC_GPIO_PIN(sc->sc_rfsilent_pin))) {
   1260  1.3    martin 			pmf_event_inject(sc->sc_dev, PMFE_RADIO_OFF);
   1261  1.1  christos 			return 1;
   1262  1.1  christos 		}
   1263  1.1  christos 
   1264  1.1  christos 		AR_WRITE(sc, AR_INTR_SYNC_CAUSE, sync);
   1265  1.1  christos 		(void)AR_READ(sc, AR_INTR_SYNC_CAUSE);
   1266  1.1  christos 	}
   1267  1.1  christos 	return 1;
   1268  1.1  christos }
   1269  1.1  christos 
   1270  1.1  christos Static int
   1271  1.1  christos ar5008_tx(struct athn_softc *sc, struct mbuf *m, struct ieee80211_node *ni,
   1272  1.1  christos     int txflags)
   1273  1.1  christos {
   1274  1.1  christos 	struct ieee80211com *ic = &sc->sc_ic;
   1275  1.1  christos 	struct ieee80211_key *k = NULL;
   1276  1.1  christos 	struct ieee80211_frame *wh;
   1277  1.1  christos 	struct athn_series series[4];
   1278  1.1  christos 	struct ar_tx_desc *ds, *lastds;
   1279  1.1  christos 	struct athn_txq *txq;
   1280  1.1  christos 	struct athn_tx_buf *bf;
   1281  1.1  christos 	struct athn_node *an = (void *)ni;
   1282  1.1  christos 	struct mbuf *m1;
   1283  1.1  christos 	uint16_t qos;
   1284  1.1  christos 	uint8_t txpower, type, encrtype, ridx[4];
   1285  1.1  christos 	int i, error, totlen, hasqos, qid;
   1286  1.1  christos 
   1287  1.1  christos 	/* Grab a Tx buffer from our global free list. */
   1288  1.1  christos 	bf = SIMPLEQ_FIRST(&sc->sc_txbufs);
   1289  1.1  christos 	KASSERT(bf != NULL);
   1290  1.1  christos 
   1291  1.1  christos 	/* Map 802.11 frame type to hardware frame type. */
   1292  1.1  christos 	wh = mtod(m, struct ieee80211_frame *);
   1293  1.1  christos 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
   1294  1.1  christos 	    IEEE80211_FC0_TYPE_MGT) {
   1295  1.1  christos 		/* NB: Beacons do not use ar5008_tx(). */
   1296  1.1  christos 		if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
   1297  1.1  christos 		    IEEE80211_FC0_SUBTYPE_PROBE_RESP)
   1298  1.1  christos 			type = AR_FRAME_TYPE_PROBE_RESP;
   1299  1.1  christos 		else if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
   1300  1.1  christos 		    IEEE80211_FC0_SUBTYPE_ATIM)
   1301  1.1  christos 			type = AR_FRAME_TYPE_ATIM;
   1302  1.1  christos 		else
   1303  1.1  christos 			type = AR_FRAME_TYPE_NORMAL;
   1304  1.1  christos 	}
   1305  1.1  christos 	else if ((wh->i_fc[0] &
   1306  1.1  christos 	    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
   1307  1.1  christos 	    (IEEE80211_FC0_TYPE_CTL  | IEEE80211_FC0_SUBTYPE_PS_POLL)) {
   1308  1.1  christos 		type = AR_FRAME_TYPE_PSPOLL;
   1309  1.1  christos 	}
   1310  1.1  christos 	else
   1311  1.1  christos 		type = AR_FRAME_TYPE_NORMAL;
   1312  1.1  christos 
   1313  1.1  christos 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
   1314  1.1  christos 		k = ieee80211_crypto_encap(ic, ni, m);
   1315  1.1  christos 		if (k == NULL)
   1316  1.1  christos 			return ENOBUFS;
   1317  1.1  christos 
   1318  1.1  christos 		/* packet header may have moved, reset our local pointer */
   1319  1.1  christos 		wh = mtod(m, struct ieee80211_frame *);
   1320  1.1  christos 	}
   1321  1.1  christos 
   1322  1.1  christos 	/* XXX 2-byte padding for QoS and 4-addr headers. */
   1323  1.1  christos 
   1324  1.1  christos 	/* Select the HW Tx queue to use for this frame. */
   1325  1.1  christos 	if ((hasqos = ieee80211_has_qos(wh))) {
   1326  1.1  christos #ifdef notyet_edca
   1327  1.1  christos 		uint8_t tid;
   1328  1.1  christos 
   1329  1.1  christos 		qos = ieee80211_get_qos(wh);
   1330  1.1  christos 		tid = qos & IEEE80211_QOS_TID;
   1331  1.1  christos 		qid = athn_ac2qid[ieee80211_up_to_ac(ic, tid)];
   1332  1.1  christos #else
   1333  1.1  christos 		qos = ieee80211_get_qos(wh);
   1334  1.1  christos 		qid = ATHN_QID_AC_BE;
   1335  1.1  christos #endif /* notyet_edca */
   1336  1.1  christos 	}
   1337  1.1  christos 	else if (type == AR_FRAME_TYPE_PSPOLL) {
   1338  1.1  christos 		qos = 0;
   1339  1.1  christos 		qid = ATHN_QID_PSPOLL;
   1340  1.1  christos 	}
   1341  1.1  christos 	else if (txflags & ATHN_TXFLAG_CAB) {
   1342  1.1  christos 		qos = 0;
   1343  1.1  christos 		qid = ATHN_QID_CAB;
   1344  1.1  christos 	}
   1345  1.1  christos 	else {
   1346  1.1  christos 		qos = 0;
   1347  1.1  christos 		qid = ATHN_QID_AC_BE;
   1348  1.1  christos 	}
   1349  1.1  christos 	txq = &sc->sc_txq[qid];
   1350  1.1  christos 
   1351  1.1  christos 	/* Select the transmit rates to use for this frame. */
   1352  1.1  christos 	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
   1353  1.1  christos 	    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) !=
   1354  1.1  christos 	    IEEE80211_FC0_TYPE_DATA) {
   1355  1.1  christos 		/* Use lowest rate for all tries. */
   1356  1.1  christos 		ridx[0] = ridx[1] = ridx[2] = ridx[3] =
   1357  1.1  christos 		    (ic->ic_curmode == IEEE80211_MODE_11A) ?
   1358  1.1  christos 			ATHN_RIDX_OFDM6 : ATHN_RIDX_CCK1;
   1359  1.1  christos 	}
   1360  1.1  christos 	else if (ic->ic_fixed_rate != -1) {
   1361  1.1  christos 		/* Use same fixed rate for all tries. */
   1362  1.1  christos 		ridx[0] = ridx[1] = ridx[2] = ridx[3] =
   1363  1.1  christos 		    sc->sc_fixed_ridx;
   1364  1.1  christos 	}
   1365  1.1  christos 	else {
   1366  1.1  christos 		int txrate = ni->ni_txrate;
   1367  1.1  christos 		/* Use fallback table of the node. */
   1368  1.1  christos 		for (i = 0; i < 4; i++) {
   1369  1.1  christos 			ridx[i] = an->ridx[txrate];
   1370  1.1  christos 			txrate = an->fallback[txrate];
   1371  1.1  christos 		}
   1372  1.1  christos 	}
   1373  1.1  christos 
   1374  1.1  christos 	if (__predict_false(sc->sc_drvbpf != NULL)) {
   1375  1.1  christos 		struct athn_tx_radiotap_header *tap = &sc->sc_txtap;
   1376  1.1  christos 
   1377  1.1  christos 		tap->wt_flags = 0;
   1378  1.1  christos 		/* Use initial transmit rate. */
   1379  1.1  christos 		tap->wt_rate = athn_rates[ridx[0]].rate;
   1380  1.1  christos 		tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
   1381  1.1  christos 		tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
   1382  1.1  christos // XXX		tap->wt_hwqueue = qid;
   1383  1.1  christos 		if (ridx[0] != ATHN_RIDX_CCK1 &&
   1384  1.1  christos 		    (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
   1385  1.1  christos 			tap->wt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
   1386  1.1  christos 
   1387  1.1  christos 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m);
   1388  1.1  christos 	}
   1389  1.1  christos 
   1390  1.1  christos 	/* DMA map mbuf. */
   1391  1.1  christos 	error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_map, m,
   1392  1.1  christos 	    BUS_DMA_NOWAIT | BUS_DMA_WRITE);
   1393  1.1  christos 	if (__predict_false(error != 0)) {
   1394  1.1  christos 		if (error != EFBIG) {
   1395  1.6  christos 			aprint_error_dev(sc->sc_dev,
   1396  1.6  christos 			    "can't map mbuf (error %d)\n", error);
   1397  1.1  christos 			m_freem(m);
   1398  1.1  christos 			return error;
   1399  1.1  christos 		}
   1400  1.1  christos 		/*
   1401  1.1  christos 		 * DMA mapping requires too many DMA segments; linearize
   1402  1.1  christos 		 * mbuf in kernel virtual address space and retry.
   1403  1.1  christos 		 */
   1404  1.1  christos 		MGETHDR(m1, M_DONTWAIT, MT_DATA);
   1405  1.1  christos 		if (m1 == NULL) {
   1406  1.1  christos 			m_freem(m);
   1407  1.1  christos 			return ENOBUFS;
   1408  1.1  christos 		}
   1409  1.1  christos 		if (m->m_pkthdr.len > (int)MHLEN) {
   1410  1.1  christos 			MCLGET(m1, M_DONTWAIT);
   1411  1.1  christos 			if (!(m1->m_flags & M_EXT)) {
   1412  1.1  christos 				m_freem(m);
   1413  1.1  christos 				m_freem(m1);
   1414  1.1  christos 				return ENOBUFS;
   1415  1.1  christos 			}
   1416  1.1  christos 		}
   1417  1.1  christos 		m_copydata(m, 0, m->m_pkthdr.len, mtod(m1, void *));
   1418  1.1  christos 		m1->m_pkthdr.len = m1->m_len = m->m_pkthdr.len;
   1419  1.1  christos 		m_freem(m);
   1420  1.1  christos 		m = m1;
   1421  1.1  christos 
   1422  1.1  christos 		error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_map, m,
   1423  1.1  christos 		    BUS_DMA_NOWAIT | BUS_DMA_WRITE);
   1424  1.1  christos 		if (error != 0) {
   1425  1.6  christos 			aprint_error_dev(sc->sc_dev,
   1426  1.6  christos 			    "can't map mbuf (error %d)\n", error);
   1427  1.1  christos 			m_freem(m);
   1428  1.1  christos 			return error;
   1429  1.1  christos 		}
   1430  1.1  christos 	}
   1431  1.1  christos 	bf->bf_m = m;
   1432  1.1  christos 	bf->bf_ni = ni;
   1433  1.1  christos 	bf->bf_txflags = txflags;
   1434  1.1  christos 
   1435  1.1  christos 	wh = mtod(m, struct ieee80211_frame *);
   1436  1.1  christos 
   1437  1.1  christos 	totlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
   1438  1.1  christos 
   1439  1.1  christos 	/* Clear all Tx descriptors that we will use. */
   1440  1.1  christos 	memset(bf->bf_descs, 0, bf->bf_map->dm_nsegs * sizeof(*ds));
   1441  1.1  christos 
   1442  1.1  christos 	/* Setup first Tx descriptor. */
   1443  1.1  christos 	ds = bf->bf_descs;
   1444  1.1  christos 
   1445  1.1  christos 	ds->ds_ctl0 = AR_TXC0_INTR_REQ | AR_TXC0_CLR_DEST_MASK;
   1446  1.1  christos 	txpower = AR_MAX_RATE_POWER;	/* Get from per-rate registers. */
   1447  1.1  christos 	ds->ds_ctl0 |= SM(AR_TXC0_XMIT_POWER, txpower);
   1448  1.1  christos 
   1449  1.1  christos 	ds->ds_ctl1 = SM(AR_TXC1_FRAME_TYPE, type);
   1450  1.1  christos 
   1451  1.1  christos 	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
   1452  1.1  christos 	    (hasqos && (qos & IEEE80211_QOS_ACKPOLICY_MASK) ==
   1453  1.1  christos 	     IEEE80211_QOS_ACKPOLICY_NOACK))
   1454  1.1  christos 		ds->ds_ctl1 |= AR_TXC1_NO_ACK;
   1455  1.1  christos #if notyet
   1456  1.1  christos 	if (0 && k != NULL) {
   1457  1.1  christos 		uintptr_t entry;
   1458  1.1  christos 
   1459  1.1  christos 		/*
   1460  1.1  christos 		 * Map 802.11 cipher to hardware encryption type and
   1461  1.1  christos 		 * compute MIC+ICV overhead.
   1462  1.1  christos 		 */
   1463  1.1  christos 		totlen += k->wk_keylen;
   1464  1.1  christos 		switch (k->wk_cipher->ic_cipher) {
   1465  1.1  christos 		case IEEE80211_CIPHER_WEP:
   1466  1.1  christos 			encrtype = AR_ENCR_TYPE_WEP;
   1467  1.1  christos 			break;
   1468  1.1  christos 		case IEEE80211_CIPHER_TKIP:
   1469  1.1  christos 			encrtype = AR_ENCR_TYPE_TKIP;
   1470  1.1  christos 			break;
   1471  1.1  christos 		case IEEE80211_CIPHER_AES_OCB:
   1472  1.1  christos 		case IEEE80211_CIPHER_AES_CCM:
   1473  1.1  christos 			encrtype = AR_ENCR_TYPE_AES;
   1474  1.1  christos 			break;
   1475  1.1  christos 		default:
   1476  1.1  christos 			panic("unsupported cipher");
   1477  1.1  christos 		}
   1478  1.1  christos 		/*
   1479  1.1  christos 		 * NB: The key cache entry index is stored in the key
   1480  1.1  christos 		 * private field when the key is installed.
   1481  1.1  christos 		 */
   1482  1.1  christos 		entry = (uintptr_t)k->k_priv;
   1483  1.1  christos 		ds->ds_ctl1 |= SM(AR_TXC1_DEST_IDX, entry);
   1484  1.1  christos 		ds->ds_ctl0 |= AR_TXC0_DEST_IDX_VALID;
   1485  1.1  christos 	}
   1486  1.1  christos 	else
   1487  1.1  christos #endif
   1488  1.1  christos 		encrtype = AR_ENCR_TYPE_CLEAR;
   1489  1.1  christos 	ds->ds_ctl6 = SM(AR_TXC6_ENCR_TYPE, encrtype);
   1490  1.1  christos 
   1491  1.1  christos 	/* Check if frame must be protected using RTS/CTS or CTS-to-self. */
   1492  1.1  christos 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
   1493  1.1  christos 		/* NB: Group frames are sent using CCK in 802.11b/g. */
   1494  1.1  christos 		if (totlen > ic->ic_rtsthreshold) {
   1495  1.1  christos 			ds->ds_ctl0 |= AR_TXC0_RTS_ENABLE;
   1496  1.1  christos 		}
   1497  1.1  christos 		else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
   1498  1.1  christos 		    athn_rates[ridx[0]].phy == IEEE80211_T_OFDM) {
   1499  1.1  christos 			if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
   1500  1.1  christos 				ds->ds_ctl0 |= AR_TXC0_RTS_ENABLE;
   1501  1.1  christos 			else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
   1502  1.1  christos 				ds->ds_ctl0 |= AR_TXC0_CTS_ENABLE;
   1503  1.1  christos 		}
   1504  1.1  christos 	}
   1505  1.1  christos 	if (ds->ds_ctl0 & (AR_TXC0_RTS_ENABLE | AR_TXC0_CTS_ENABLE)) {
   1506  1.1  christos 		/* Disable multi-rate retries when protection is used. */
   1507  1.1  christos 		ridx[1] = ridx[2] = ridx[3] = ridx[0];
   1508  1.1  christos 	}
   1509  1.1  christos 	/* Setup multi-rate retries. */
   1510  1.1  christos 	for (i = 0; i < 4; i++) {
   1511  1.1  christos 		series[i].hwrate = athn_rates[ridx[i]].hwrate;
   1512  1.1  christos 		if (athn_rates[ridx[i]].phy == IEEE80211_T_DS &&
   1513  1.1  christos 		    ridx[i] != ATHN_RIDX_CCK1 &&
   1514  1.1  christos 		    (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
   1515  1.1  christos 			series[i].hwrate |= 0x04;
   1516  1.1  christos 		series[i].dur = 0;
   1517  1.1  christos 	}
   1518  1.1  christos 	if (!(ds->ds_ctl1 & AR_TXC1_NO_ACK)) {
   1519  1.1  christos 		/* Compute duration for each series. */
   1520  1.1  christos 		for (i = 0; i < 4; i++) {
   1521  1.1  christos 			series[i].dur = athn_txtime(sc, IEEE80211_ACK_LEN,
   1522  1.1  christos 			    athn_rates[ridx[i]].rspridx, ic->ic_flags);
   1523  1.1  christos 		}
   1524  1.1  christos 	}
   1525  1.1  christos 
   1526  1.1  christos 	/* Write number of tries for each series. */
   1527  1.1  christos 	ds->ds_ctl2 =
   1528  1.1  christos 	    SM(AR_TXC2_XMIT_DATA_TRIES0, 2) |
   1529  1.1  christos 	    SM(AR_TXC2_XMIT_DATA_TRIES1, 2) |
   1530  1.1  christos 	    SM(AR_TXC2_XMIT_DATA_TRIES2, 2) |
   1531  1.1  christos 	    SM(AR_TXC2_XMIT_DATA_TRIES3, 4);
   1532  1.1  christos 
   1533  1.1  christos 	/* Tell HW to update duration field in 802.11 header. */
   1534  1.1  christos 	if (type != AR_FRAME_TYPE_PSPOLL)
   1535  1.1  christos 		ds->ds_ctl2 |= AR_TXC2_DUR_UPDATE_ENA;
   1536  1.1  christos 
   1537  1.1  christos 	/* Write Tx rate for each series. */
   1538  1.1  christos 	ds->ds_ctl3 =
   1539  1.1  christos 	    SM(AR_TXC3_XMIT_RATE0, series[0].hwrate) |
   1540  1.1  christos 	    SM(AR_TXC3_XMIT_RATE1, series[1].hwrate) |
   1541  1.1  christos 	    SM(AR_TXC3_XMIT_RATE2, series[2].hwrate) |
   1542  1.1  christos 	    SM(AR_TXC3_XMIT_RATE3, series[3].hwrate);
   1543  1.1  christos 
   1544  1.1  christos 	/* Write duration for each series. */
   1545  1.1  christos 	ds->ds_ctl4 =
   1546  1.1  christos 	    SM(AR_TXC4_PACKET_DUR0, series[0].dur) |
   1547  1.1  christos 	    SM(AR_TXC4_PACKET_DUR1, series[1].dur);
   1548  1.1  christos 	ds->ds_ctl5 =
   1549  1.1  christos 	    SM(AR_TXC5_PACKET_DUR2, series[2].dur) |
   1550  1.1  christos 	    SM(AR_TXC5_PACKET_DUR3, series[3].dur);
   1551  1.1  christos 
   1552  1.1  christos 	/* Use the same Tx chains for all tries. */
   1553  1.1  christos 	ds->ds_ctl7 =
   1554  1.1  christos 	    SM(AR_TXC7_CHAIN_SEL0, sc->sc_txchainmask) |
   1555  1.1  christos 	    SM(AR_TXC7_CHAIN_SEL1, sc->sc_txchainmask) |
   1556  1.1  christos 	    SM(AR_TXC7_CHAIN_SEL2, sc->sc_txchainmask) |
   1557  1.1  christos 	    SM(AR_TXC7_CHAIN_SEL3, sc->sc_txchainmask);
   1558  1.1  christos #ifdef notyet
   1559  1.1  christos #ifndef IEEE80211_NO_HT
   1560  1.1  christos 	/* Use the same short GI setting for all tries. */
   1561  1.1  christos 	if (ic->ic_flags & IEEE80211_F_SHGI)
   1562  1.1  christos 		ds->ds_ctl7 |= AR_TXC7_GI0123;
   1563  1.1  christos 	/* Use the same channel width for all tries. */
   1564  1.1  christos 	if (ic->ic_flags & IEEE80211_F_CBW40)
   1565  1.1  christos 		ds->ds_ctl7 |= AR_TXC7_2040_0123;
   1566  1.1  christos #endif
   1567  1.1  christos #endif
   1568  1.1  christos 
   1569  1.1  christos 	if (ds->ds_ctl0 & (AR_TXC0_RTS_ENABLE | AR_TXC0_CTS_ENABLE)) {
   1570  1.1  christos 		uint8_t protridx, hwrate;
   1571  1.1  christos 		uint16_t dur = 0;
   1572  1.1  christos 
   1573  1.1  christos 		/* Use the same protection mode for all tries. */
   1574  1.1  christos 		if (ds->ds_ctl0 & AR_TXC0_RTS_ENABLE) {
   1575  1.1  christos 			ds->ds_ctl4 |= AR_TXC4_RTSCTS_QUAL01;
   1576  1.1  christos 			ds->ds_ctl5 |= AR_TXC5_RTSCTS_QUAL23;
   1577  1.1  christos 		}
   1578  1.1  christos 		/* Select protection rate (suboptimal but ok). */
   1579  1.1  christos 		protridx = (ic->ic_curmode == IEEE80211_MODE_11A) ?
   1580  1.1  christos 		    ATHN_RIDX_OFDM6 : ATHN_RIDX_CCK2;
   1581  1.1  christos 		if (ds->ds_ctl0 & AR_TXC0_RTS_ENABLE) {
   1582  1.1  christos 			/* Account for CTS duration. */
   1583  1.1  christos 			dur += athn_txtime(sc, IEEE80211_ACK_LEN,
   1584  1.1  christos 			    athn_rates[protridx].rspridx, ic->ic_flags);
   1585  1.1  christos 		}
   1586  1.1  christos 		dur += athn_txtime(sc, totlen, ridx[0], ic->ic_flags);
   1587  1.1  christos 		if (!(ds->ds_ctl1 & AR_TXC1_NO_ACK)) {
   1588  1.1  christos 			/* Account for ACK duration. */
   1589  1.1  christos 			dur += athn_txtime(sc, IEEE80211_ACK_LEN,
   1590  1.1  christos 			    athn_rates[ridx[0]].rspridx, ic->ic_flags);
   1591  1.1  christos 		}
   1592  1.1  christos 		/* Write protection frame duration and rate. */
   1593  1.1  christos 		ds->ds_ctl2 |= SM(AR_TXC2_BURST_DUR, dur);
   1594  1.1  christos 		hwrate = athn_rates[protridx].hwrate;
   1595  1.1  christos 		if (protridx == ATHN_RIDX_CCK2 &&
   1596  1.1  christos 		    (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
   1597  1.1  christos 			hwrate |= 0x04;
   1598  1.1  christos 		ds->ds_ctl7 |= SM(AR_TXC7_RTSCTS_RATE, hwrate);
   1599  1.1  christos 	}
   1600  1.1  christos 
   1601  1.1  christos 	/* Finalize first Tx descriptor and fill others (if any). */
   1602  1.1  christos 	ds->ds_ctl0 |= SM(AR_TXC0_FRAME_LEN, totlen);
   1603  1.1  christos 
   1604  1.1  christos 	lastds = NULL;	/* XXX: gcc */
   1605  1.1  christos 	for (i = 0; i < bf->bf_map->dm_nsegs; i++, ds++) {
   1606  1.1  christos 		ds->ds_data = bf->bf_map->dm_segs[i].ds_addr;
   1607  1.1  christos 		ds->ds_ctl1 |= SM(AR_TXC1_BUF_LEN,
   1608  1.1  christos 		    bf->bf_map->dm_segs[i].ds_len);
   1609  1.1  christos 
   1610  1.1  christos 		if (i != bf->bf_map->dm_nsegs - 1)
   1611  1.1  christos 			ds->ds_ctl1 |= AR_TXC1_MORE;
   1612  1.1  christos 		ds->ds_link = 0;
   1613  1.1  christos 
   1614  1.1  christos 		/* Chain Tx descriptor. */
   1615  1.1  christos 		if (i != 0)
   1616  1.1  christos 			lastds->ds_link = bf->bf_daddr + i * sizeof(*ds);
   1617  1.1  christos 		lastds = ds;
   1618  1.1  christos 	}
   1619  1.1  christos 	bus_dmamap_sync(sc->sc_dmat, bf->bf_map, 0, bf->bf_map->dm_mapsize,
   1620  1.1  christos 	    BUS_DMASYNC_PREWRITE);
   1621  1.1  christos 
   1622  1.1  christos 	if (!SIMPLEQ_EMPTY(&txq->head))
   1623  1.1  christos 		((struct ar_tx_desc *)txq->lastds)->ds_link = bf->bf_daddr;
   1624  1.1  christos 	else
   1625  1.1  christos 		AR_WRITE(sc, AR_QTXDP(qid), bf->bf_daddr);
   1626  1.1  christos 	txq->lastds = lastds;
   1627  1.1  christos 	SIMPLEQ_REMOVE_HEAD(&sc->sc_txbufs, bf_list);
   1628  1.1  christos 	SIMPLEQ_INSERT_TAIL(&txq->head, bf, bf_list);
   1629  1.1  christos 
   1630  1.1  christos 	ds = bf->bf_descs;
   1631  1.1  christos 	DPRINTFN(DBG_TX, sc,
   1632  1.1  christos 	    "Tx qid=%d nsegs=%d ctl0=0x%x ctl1=0x%x ctl3=0x%x\n",
   1633  1.1  christos 	    qid, bf->bf_map->dm_nsegs, ds->ds_ctl0, ds->ds_ctl1, ds->ds_ctl3);
   1634  1.1  christos 
   1635  1.1  christos 	/* Kick Tx. */
   1636  1.1  christos 	AR_WRITE(sc, AR_Q_TXE, 1 << qid);
   1637  1.1  christos 	AR_WRITE_BARRIER(sc);
   1638  1.1  christos 	return 0;
   1639  1.1  christos }
   1640  1.1  christos 
   1641  1.1  christos Static void
   1642  1.1  christos ar5008_set_rf_mode(struct athn_softc *sc, struct ieee80211_channel *c)
   1643  1.1  christos {
   1644  1.1  christos 	uint32_t reg;
   1645  1.1  christos 
   1646  1.1  christos 	reg = IEEE80211_IS_CHAN_2GHZ(c) ?
   1647  1.1  christos 	    AR_PHY_MODE_DYNAMIC : AR_PHY_MODE_OFDM;
   1648  1.1  christos 	if (!AR_SREV_9280_10_OR_LATER(sc)) {
   1649  1.1  christos 		reg |= IEEE80211_IS_CHAN_2GHZ(c) ?
   1650  1.1  christos 		    AR_PHY_MODE_RF2GHZ : AR_PHY_MODE_RF5GHZ;
   1651  1.1  christos 	}
   1652  1.1  christos 	else if (IEEE80211_IS_CHAN_5GHZ(c) &&
   1653  1.1  christos 	    (sc->sc_flags & ATHN_FLAG_FAST_PLL_CLOCK)) {
   1654  1.1  christos 		reg |= AR_PHY_MODE_DYNAMIC | AR_PHY_MODE_DYN_CCK_DISABLE;
   1655  1.1  christos 	}
   1656  1.1  christos 	AR_WRITE(sc, AR_PHY_MODE, reg);
   1657  1.1  christos 	AR_WRITE_BARRIER(sc);
   1658  1.1  christos }
   1659  1.1  christos 
   1660  1.1  christos static __inline uint32_t
   1661  1.1  christos ar5008_synth_delay(struct athn_softc *sc)
   1662  1.1  christos {
   1663  1.5     skrll 	uint32_t synth_delay;
   1664  1.1  christos 
   1665  1.5     skrll 	synth_delay = MS(AR_READ(sc, AR_PHY_RX_DELAY), AR_PHY_RX_DELAY_DELAY);
   1666  1.1  christos 	if (sc->sc_ic.ic_curmode == IEEE80211_MODE_11B)
   1667  1.5     skrll 		synth_delay = (synth_delay * 4) / 22;
   1668  1.1  christos 	else
   1669  1.5     skrll 		synth_delay = synth_delay / 10;	/* in 100ns steps */
   1670  1.5     skrll 	return synth_delay;
   1671  1.1  christos }
   1672  1.1  christos 
   1673  1.1  christos Static int
   1674  1.1  christos ar5008_rf_bus_request(struct athn_softc *sc)
   1675  1.1  christos {
   1676  1.1  christos 	int ntries;
   1677  1.1  christos 
   1678  1.1  christos 	/* Request RF Bus grant. */
   1679  1.1  christos 	AR_WRITE(sc, AR_PHY_RFBUS_REQ, AR_PHY_RFBUS_REQ_EN);
   1680  1.1  christos 	for (ntries = 0; ntries < 10000; ntries++) {
   1681  1.1  christos 		if (AR_READ(sc, AR_PHY_RFBUS_GRANT) & AR_PHY_RFBUS_GRANT_EN)
   1682  1.1  christos 			return 0;
   1683  1.1  christos 		DELAY(10);
   1684  1.1  christos 	}
   1685  1.1  christos 	DPRINTFN(DBG_RF, sc, "could not kill baseband Rx");
   1686  1.1  christos 	return ETIMEDOUT;
   1687  1.1  christos }
   1688  1.1  christos 
   1689  1.1  christos Static void
   1690  1.1  christos ar5008_rf_bus_release(struct athn_softc *sc)
   1691  1.1  christos {
   1692  1.1  christos 
   1693  1.1  christos 	/* Wait for the synthesizer to settle. */
   1694  1.1  christos 	DELAY(AR_BASE_PHY_ACTIVE_DELAY + ar5008_synth_delay(sc));
   1695  1.1  christos 
   1696  1.1  christos 	/* Release the RF Bus grant. */
   1697  1.1  christos 	AR_WRITE(sc, AR_PHY_RFBUS_REQ, 0);
   1698  1.1  christos 	AR_WRITE_BARRIER(sc);
   1699  1.1  christos }
   1700  1.1  christos 
   1701  1.1  christos Static void
   1702  1.1  christos ar5008_set_phy(struct athn_softc *sc, struct ieee80211_channel *c,
   1703  1.1  christos     struct ieee80211_channel *extc)
   1704  1.1  christos {
   1705  1.1  christos 	uint32_t phy;
   1706  1.1  christos 
   1707  1.1  christos 	if (AR_SREV_9285_10_OR_LATER(sc))
   1708  1.1  christos 		phy = AR_READ(sc, AR_PHY_TURBO) & AR_PHY_FC_ENABLE_DAC_FIFO;
   1709  1.1  christos 	else
   1710  1.1  christos 		phy = 0;
   1711  1.1  christos 	phy |= AR_PHY_FC_HT_EN | AR_PHY_FC_SHORT_GI_40 |
   1712  1.1  christos 	    AR_PHY_FC_SINGLE_HT_LTF1 | AR_PHY_FC_WALSH;
   1713  1.1  christos #ifndef IEEE80211_NO_HT
   1714  1.1  christos 	if (extc != NULL) {
   1715  1.1  christos 		phy |= AR_PHY_FC_DYN2040_EN;
   1716  1.1  christos 		if (extc > c)	/* XXX */
   1717  1.1  christos 			phy |= AR_PHY_FC_DYN2040_PRI_CH;
   1718  1.1  christos 	}
   1719  1.1  christos #endif
   1720  1.1  christos 	AR_WRITE(sc, AR_PHY_TURBO, phy);
   1721  1.1  christos 
   1722  1.1  christos 	AR_WRITE(sc, AR_2040_MODE,
   1723  1.1  christos 	    (extc != NULL) ? AR_2040_JOINED_RX_CLEAR : 0);
   1724  1.1  christos 
   1725  1.1  christos 	/* Set global transmit timeout. */
   1726  1.1  christos 	AR_WRITE(sc, AR_GTXTO, SM(AR_GTXTO_TIMEOUT_LIMIT, 25));
   1727  1.1  christos 	/* Set carrier sense timeout. */
   1728  1.1  christos 	AR_WRITE(sc, AR_CST, SM(AR_CST_TIMEOUT_LIMIT, 15));
   1729  1.1  christos 	AR_WRITE_BARRIER(sc);
   1730  1.1  christos }
   1731  1.1  christos 
   1732  1.1  christos Static void
   1733  1.1  christos ar5008_set_delta_slope(struct athn_softc *sc, struct ieee80211_channel *c,
   1734  1.1  christos     struct ieee80211_channel *extc)
   1735  1.1  christos {
   1736  1.1  christos 	uint32_t coeff, exp, man, reg;
   1737  1.1  christos 
   1738  1.1  christos 	/* Set Delta Slope (exponent and mantissa). */
   1739  1.1  christos 	coeff = (100 << 24) / c->ic_freq;
   1740  1.1  christos 	athn_get_delta_slope(coeff, &exp, &man);
   1741  1.1  christos 	DPRINTFN(DBG_RX, sc, "delta slope coeff exp=%u man=%u\n", exp, man);
   1742  1.1  christos 
   1743  1.1  christos 	reg = AR_READ(sc, AR_PHY_TIMING3);
   1744  1.1  christos 	reg = RW(reg, AR_PHY_TIMING3_DSC_EXP, exp);
   1745  1.1  christos 	reg = RW(reg, AR_PHY_TIMING3_DSC_MAN, man);
   1746  1.1  christos 	AR_WRITE(sc, AR_PHY_TIMING3, reg);
   1747  1.1  christos 
   1748  1.1  christos 	/* For Short GI, coeff is 9/10 that of normal coeff. */
   1749  1.1  christos 	coeff = (9 * coeff) / 10;
   1750  1.1  christos 	athn_get_delta_slope(coeff, &exp, &man);
   1751  1.1  christos 	DPRINTFN(DBG_RX, sc, "delta slope coeff exp=%u man=%u\n", exp, man);
   1752  1.1  christos 
   1753  1.1  christos 	reg = AR_READ(sc, AR_PHY_HALFGI);
   1754  1.1  christos 	reg = RW(reg, AR_PHY_HALFGI_DSC_EXP, exp);
   1755  1.1  christos 	reg = RW(reg, AR_PHY_HALFGI_DSC_MAN, man);
   1756  1.1  christos 	AR_WRITE(sc, AR_PHY_HALFGI, reg);
   1757  1.1  christos 	AR_WRITE_BARRIER(sc);
   1758  1.1  christos }
   1759  1.1  christos 
   1760  1.1  christos Static void
   1761  1.1  christos ar5008_enable_antenna_diversity(struct athn_softc *sc)
   1762  1.1  christos {
   1763  1.1  christos 
   1764  1.1  christos 	AR_SETBITS(sc, AR_PHY_CCK_DETECT,
   1765  1.1  christos 	    AR_PHY_CCK_DETECT_BB_ENABLE_ANT_FAST_DIV);
   1766  1.1  christos 	AR_WRITE_BARRIER(sc);
   1767  1.1  christos }
   1768  1.1  christos 
   1769  1.1  christos Static void
   1770  1.1  christos ar5008_init_baseband(struct athn_softc *sc)
   1771  1.1  christos {
   1772  1.1  christos 	uint32_t synth_delay;
   1773  1.1  christos 
   1774  1.1  christos 	synth_delay = ar5008_synth_delay(sc);
   1775  1.1  christos 	/* Activate the PHY (includes baseband activate and synthesizer on). */
   1776  1.1  christos 	AR_WRITE(sc, AR_PHY_ACTIVE, AR_PHY_ACTIVE_EN);
   1777  1.1  christos 	AR_WRITE_BARRIER(sc);
   1778  1.1  christos 	DELAY(AR_BASE_PHY_ACTIVE_DELAY + synth_delay);
   1779  1.1  christos }
   1780  1.1  christos 
   1781  1.1  christos Static void
   1782  1.1  christos ar5008_disable_phy(struct athn_softc *sc)
   1783  1.1  christos {
   1784  1.1  christos 
   1785  1.1  christos 	AR_WRITE(sc, AR_PHY_ACTIVE, AR_PHY_ACTIVE_DIS);
   1786  1.1  christos 	AR_WRITE_BARRIER(sc);
   1787  1.1  christos }
   1788  1.1  christos 
   1789  1.1  christos Static void
   1790  1.1  christos ar5008_init_chains(struct athn_softc *sc)
   1791  1.1  christos {
   1792  1.1  christos 
   1793  1.1  christos 	if (sc->sc_rxchainmask == 0x5 || sc->sc_txchainmask == 0x5)
   1794  1.1  christos 		AR_SETBITS(sc, AR_PHY_ANALOG_SWAP, AR_PHY_SWAP_ALT_CHAIN);
   1795  1.1  christos 
   1796  1.1  christos 	/* Setup chain masks. */
   1797  1.1  christos 	if (sc->sc_mac_ver <= AR_SREV_VERSION_9160 &&
   1798  1.1  christos 	    (sc->sc_rxchainmask == 0x3 || sc->sc_rxchainmask == 0x5)) {
   1799  1.1  christos 		AR_WRITE(sc, AR_PHY_RX_CHAINMASK,  0x7);
   1800  1.1  christos 		AR_WRITE(sc, AR_PHY_CAL_CHAINMASK, 0x7);
   1801  1.1  christos 	}
   1802  1.1  christos 	else {
   1803  1.1  christos 		AR_WRITE(sc, AR_PHY_RX_CHAINMASK,  sc->sc_rxchainmask);
   1804  1.1  christos 		AR_WRITE(sc, AR_PHY_CAL_CHAINMASK, sc->sc_rxchainmask);
   1805  1.1  christos 	}
   1806  1.1  christos 	AR_WRITE(sc, AR_SELFGEN_MASK, sc->sc_txchainmask);
   1807  1.1  christos 	AR_WRITE_BARRIER(sc);
   1808  1.1  christos }
   1809  1.1  christos 
   1810  1.1  christos Static void
   1811  1.1  christos ar5008_set_rxchains(struct athn_softc *sc)
   1812  1.1  christos {
   1813  1.1  christos 
   1814  1.1  christos 	if (sc->sc_rxchainmask == 0x3 || sc->sc_rxchainmask == 0x5) {
   1815  1.1  christos 		AR_WRITE(sc, AR_PHY_RX_CHAINMASK,  sc->sc_rxchainmask);
   1816  1.1  christos 		AR_WRITE(sc, AR_PHY_CAL_CHAINMASK, sc->sc_rxchainmask);
   1817  1.1  christos 		AR_WRITE_BARRIER(sc);
   1818  1.1  christos 	}
   1819  1.1  christos }
   1820  1.1  christos 
   1821  1.1  christos #ifdef notused
   1822  1.1  christos Static void
   1823  1.1  christos ar5008_read_noisefloor(struct athn_softc *sc, int16_t *nf, int16_t *nf_ext)
   1824  1.1  christos {
   1825  1.1  christos /* Sign-extends 9-bit value (assumes upper bits are zeroes). */
   1826  1.1  christos #define SIGN_EXT(v)	(((v) ^ 0x100) - 0x100)
   1827  1.1  christos 	uint32_t reg;
   1828  1.1  christos 	int i;
   1829  1.1  christos 
   1830  1.1  christos 	for (i = 0; i < sc->sc_nrxchains; i++) {
   1831  1.1  christos 		reg = AR_READ(sc, AR_PHY_CCA(i));
   1832  1.1  christos 		if (AR_SREV_9280_10_OR_LATER(sc))
   1833  1.1  christos 			nf[i] = MS(reg, AR9280_PHY_MINCCA_PWR);
   1834  1.1  christos 		else
   1835  1.1  christos 			nf[i] = MS(reg, AR_PHY_MINCCA_PWR);
   1836  1.1  christos 		nf[i] = SIGN_EXT(nf[i]);
   1837  1.1  christos 
   1838  1.1  christos 		reg = AR_READ(sc, AR_PHY_EXT_CCA(i));
   1839  1.1  christos 		if (AR_SREV_9280_10_OR_LATER(sc))
   1840  1.1  christos 			nf_ext[i] = MS(reg, AR9280_PHY_EXT_MINCCA_PWR);
   1841  1.1  christos 		else
   1842  1.1  christos 			nf_ext[i] = MS(reg, AR_PHY_EXT_MINCCA_PWR);
   1843  1.1  christos 		nf_ext[i] = SIGN_EXT(nf_ext[i]);
   1844  1.1  christos 	}
   1845  1.1  christos #undef SIGN_EXT
   1846  1.1  christos }
   1847  1.1  christos #endif /* notused */
   1848  1.1  christos 
   1849  1.1  christos #ifdef notused
   1850  1.1  christos Static void
   1851  1.1  christos ar5008_write_noisefloor(struct athn_softc *sc, int16_t *nf, int16_t *nf_ext)
   1852  1.1  christos {
   1853  1.1  christos 	uint32_t reg;
   1854  1.1  christos 	int i;
   1855  1.1  christos 
   1856  1.1  christos 	for (i = 0; i < sc->sc_nrxchains; i++) {
   1857  1.1  christos 		reg = AR_READ(sc, AR_PHY_CCA(i));
   1858  1.1  christos 		reg = RW(reg, AR_PHY_MAXCCA_PWR, nf[i]);
   1859  1.1  christos 		AR_WRITE(sc, AR_PHY_CCA(i), reg);
   1860  1.1  christos 
   1861  1.1  christos 		reg = AR_READ(sc, AR_PHY_EXT_CCA(i));
   1862  1.1  christos 		reg = RW(reg, AR_PHY_EXT_MAXCCA_PWR, nf_ext[i]);
   1863  1.1  christos 		AR_WRITE(sc, AR_PHY_EXT_CCA(i), reg);
   1864  1.1  christos 	}
   1865  1.1  christos 	AR_WRITE_BARRIER(sc);
   1866  1.1  christos }
   1867  1.1  christos #endif /* notused */
   1868  1.1  christos 
   1869  1.1  christos #ifdef notused
   1870  1.1  christos Static void
   1871  1.1  christos ar5008_get_noisefloor(struct athn_softc *sc, struct ieee80211_channel *c)
   1872  1.1  christos {
   1873  1.1  christos 	int16_t nf[AR_MAX_CHAINS], nf_ext[AR_MAX_CHAINS];
   1874  1.1  christos 	int i;
   1875  1.1  christos 
   1876  1.1  christos 	if (AR_READ(sc, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF) {
   1877  1.1  christos 		/* Noisefloor calibration not finished. */
   1878  1.1  christos 		return;
   1879  1.1  christos 	}
   1880  1.1  christos 	/* Noisefloor calibration is finished. */
   1881  1.1  christos 	ar5008_read_noisefloor(sc, nf, nf_ext);
   1882  1.1  christos 
   1883  1.1  christos 	/* Update noisefloor history. */
   1884  1.1  christos 	for (i = 0; i < sc->sc_nrxchains; i++) {
   1885  1.1  christos 		sc->sc_nf_hist[sc->sc_nf_hist_cur].nf[i] = nf[i];
   1886  1.1  christos 		sc->sc_nf_hist[sc->sc_nf_hist_cur].nf_ext[i] = nf_ext[i];
   1887  1.1  christos 	}
   1888  1.1  christos 	if (++sc->sc_nf_hist_cur >= ATHN_NF_CAL_HIST_MAX)
   1889  1.1  christos 		sc->sc_nf_hist_cur = 0;
   1890  1.1  christos }
   1891  1.1  christos #endif /* notused */
   1892  1.1  christos 
   1893  1.1  christos #ifdef notused
   1894  1.1  christos Static void
   1895  1.1  christos ar5008_bb_load_noisefloor(struct athn_softc *sc)
   1896  1.1  christos {
   1897  1.1  christos 	int16_t nf[AR_MAX_CHAINS], nf_ext[AR_MAX_CHAINS];
   1898  1.1  christos 	int i, ntries;
   1899  1.1  christos 
   1900  1.1  christos 	/* Write filtered noisefloor values. */
   1901  1.1  christos 	for (i = 0; i < sc->sc_nrxchains; i++) {
   1902  1.1  christos 		nf[i] = sc->sc_nf_priv[i] * 2;
   1903  1.1  christos 		nf_ext[i] = sc->sc_nf_ext_priv[i] * 2;
   1904  1.1  christos 	}
   1905  1.1  christos 	ar5008_write_noisefloor(sc, nf, nf_ext);
   1906  1.1  christos 
   1907  1.1  christos 	/* Load filtered noisefloor values into baseband. */
   1908  1.1  christos 	AR_CLRBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_ENABLE_NF);
   1909  1.1  christos 	AR_CLRBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
   1910  1.1  christos 	AR_SETBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
   1911  1.1  christos 	/* Wait for load to complete. */
   1912  1.1  christos 	for (ntries = 0; ntries < 1000; ntries++) {
   1913  1.1  christos 		if (!(AR_READ(sc, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF))
   1914  1.1  christos 			break;
   1915  1.1  christos 		DELAY(50);
   1916  1.1  christos 	}
   1917  1.1  christos 	if (ntries == 1000) {
   1918  1.1  christos 		DPRINTFN(DBG_RF, sc, "failed to load noisefloor values\n");
   1919  1.1  christos 		return;
   1920  1.1  christos 	}
   1921  1.1  christos 
   1922  1.1  christos 	/* Restore noisefloor values to initial (max) values. */
   1923  1.1  christos 	for (i = 0; i < AR_MAX_CHAINS; i++)
   1924  1.1  christos 		nf[i] = nf_ext[i] = -50 * 2;
   1925  1.1  christos 	ar5008_write_noisefloor(sc, nf, nf_ext);
   1926  1.1  christos }
   1927  1.1  christos #endif /* notused */
   1928  1.1  christos 
   1929  1.1  christos #ifdef notused
   1930  1.1  christos Static void
   1931  1.1  christos ar5008_noisefloor_calib(struct athn_softc *sc)
   1932  1.1  christos {
   1933  1.1  christos 
   1934  1.1  christos 	AR_SETBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_ENABLE_NF);
   1935  1.1  christos 	AR_SETBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
   1936  1.1  christos 	AR_SETBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
   1937  1.1  christos 	AR_WRITE_BARRIER(sc);
   1938  1.1  christos }
   1939  1.1  christos #endif /* notused */
   1940  1.1  christos 
   1941  1.1  christos Static void
   1942  1.1  christos ar5008_do_noisefloor_calib(struct athn_softc *sc)
   1943  1.1  christos {
   1944  1.1  christos 
   1945  1.1  christos 	AR_SETBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
   1946  1.1  christos 	AR_WRITE_BARRIER(sc);
   1947  1.1  christos }
   1948  1.1  christos 
   1949  1.1  christos Static void
   1950  1.1  christos ar5008_do_calib(struct athn_softc *sc)
   1951  1.1  christos {
   1952  1.1  christos 	uint32_t mode, reg;
   1953  1.1  christos 	int log;
   1954  1.1  christos 
   1955  1.1  christos 	reg = AR_READ(sc, AR_PHY_TIMING_CTRL4_0);
   1956  1.1  christos 	log = AR_SREV_9280_10_OR_LATER(sc) ? 10 : 2;
   1957  1.1  christos 	reg = RW(reg, AR_PHY_TIMING_CTRL4_IQCAL_LOG_COUNT_MAX, log);
   1958  1.1  christos 	AR_WRITE(sc, AR_PHY_TIMING_CTRL4_0, reg);
   1959  1.1  christos 
   1960  1.1  christos 	if (sc->sc_cur_calib_mask & ATHN_CAL_ADC_GAIN)
   1961  1.1  christos 		mode = AR_PHY_CALMODE_ADC_GAIN;
   1962  1.1  christos 	else if (sc->sc_cur_calib_mask & ATHN_CAL_ADC_DC)
   1963  1.1  christos 		mode = AR_PHY_CALMODE_ADC_DC_PER;
   1964  1.1  christos 	else	/* ATHN_CAL_IQ */
   1965  1.1  christos 		mode = AR_PHY_CALMODE_IQ;
   1966  1.1  christos 	AR_WRITE(sc, AR_PHY_CALMODE, mode);
   1967  1.1  christos 
   1968  1.1  christos 	DPRINTFN(DBG_RF, sc, "starting calibration mode=0x%x\n", mode);
   1969  1.1  christos 	AR_SETBITS(sc, AR_PHY_TIMING_CTRL4_0, AR_PHY_TIMING_CTRL4_DO_CAL);
   1970  1.1  christos 	AR_WRITE_BARRIER(sc);
   1971  1.1  christos }
   1972  1.1  christos 
   1973  1.1  christos Static void
   1974  1.1  christos ar5008_next_calib(struct athn_softc *sc)
   1975  1.1  christos {
   1976  1.1  christos 
   1977  1.1  christos 	/* Check if we have any calibration in progress. */
   1978  1.1  christos 	if (sc->sc_cur_calib_mask != 0) {
   1979  1.1  christos 		if (!(AR_READ(sc, AR_PHY_TIMING_CTRL4_0) &
   1980  1.1  christos 		    AR_PHY_TIMING_CTRL4_DO_CAL)) {
   1981  1.1  christos 			/* Calibration completed for current sample. */
   1982  1.1  christos 			if (sc->sc_cur_calib_mask & ATHN_CAL_ADC_GAIN)
   1983  1.1  christos 				ar5008_calib_adc_gain(sc);
   1984  1.1  christos 			else if (sc->sc_cur_calib_mask & ATHN_CAL_ADC_DC)
   1985  1.1  christos 				ar5008_calib_adc_dc_off(sc);
   1986  1.1  christos 			else	/* ATHN_CAL_IQ */
   1987  1.1  christos 				ar5008_calib_iq(sc);
   1988  1.1  christos 		}
   1989  1.1  christos 	}
   1990  1.1  christos }
   1991  1.1  christos 
   1992  1.1  christos Static void
   1993  1.1  christos ar5008_calib_iq(struct athn_softc *sc)
   1994  1.1  christos {
   1995  1.1  christos 	struct athn_iq_cal *cal;
   1996  1.1  christos 	uint32_t reg, i_coff_denom, q_coff_denom;
   1997  1.1  christos 	int32_t i_coff, q_coff;
   1998  1.1  christos 	int i, iq_corr_neg;
   1999  1.1  christos 
   2000  1.1  christos 	for (i = 0; i < AR_MAX_CHAINS; i++) {
   2001  1.1  christos 		cal = &sc->sc_calib.iq[i];
   2002  1.1  christos 
   2003  1.1  christos 		/* Accumulate IQ calibration measures (clear on read). */
   2004  1.1  christos 		cal->pwr_meas_i += AR_READ(sc, AR_PHY_CAL_MEAS_0(i));
   2005  1.1  christos 		cal->pwr_meas_q += AR_READ(sc, AR_PHY_CAL_MEAS_1(i));
   2006  1.1  christos 		cal->iq_corr_meas +=
   2007  1.1  christos 		    (int32_t)AR_READ(sc, AR_PHY_CAL_MEAS_2(i));
   2008  1.1  christos 	}
   2009  1.1  christos 	if (!AR_SREV_9280_10_OR_LATER(sc) &&
   2010  1.1  christos 	    ++sc->sc_calib.nsamples < AR_CAL_SAMPLES) {
   2011  1.1  christos 		/* Not enough samples accumulated, continue. */
   2012  1.1  christos 		ar5008_do_calib(sc);
   2013  1.1  christos 		return;
   2014  1.1  christos 	}
   2015  1.1  christos 
   2016  1.1  christos 	for (i = 0; i < sc->sc_nrxchains; i++) {
   2017  1.1  christos 		cal = &sc->sc_calib.iq[i];
   2018  1.1  christos 
   2019  1.1  christos 		if (cal->pwr_meas_q == 0)
   2020  1.1  christos 			continue;
   2021  1.1  christos 
   2022  1.1  christos 		if ((iq_corr_neg = cal->iq_corr_meas < 0))
   2023  1.1  christos 			cal->iq_corr_meas = -cal->iq_corr_meas;
   2024  1.1  christos 
   2025  1.1  christos 		i_coff_denom =
   2026  1.1  christos 		    (cal->pwr_meas_i / 2 + cal->pwr_meas_q / 2) / 128;
   2027  1.1  christos 		q_coff_denom = cal->pwr_meas_q / 64;
   2028  1.1  christos 
   2029  1.1  christos 		if (i_coff_denom == 0 || q_coff_denom == 0)
   2030  1.1  christos 			continue;	/* Prevents division by zero. */
   2031  1.1  christos 
   2032  1.1  christos 		i_coff = cal->iq_corr_meas / i_coff_denom;
   2033  1.1  christos 		q_coff = (cal->pwr_meas_i / q_coff_denom) - 64;
   2034  1.1  christos 
   2035  1.1  christos 		/* Negate i_coff if iq_corr_meas is positive. */
   2036  1.1  christos 		if (!iq_corr_neg)
   2037  1.1  christos 			i_coff = 0x40 - (i_coff & 0x3f);
   2038  1.1  christos 		if (q_coff > 15)
   2039  1.1  christos 			q_coff = 15;
   2040  1.1  christos 		else if (q_coff <= -16)
   2041  1.1  christos 			q_coff = -16;	/* XXX Linux has a bug here? */
   2042  1.1  christos 
   2043  1.1  christos 		DPRINTFN(DBG_RF, sc, "IQ calibration for chain %d\n", i);
   2044  1.1  christos 		reg = AR_READ(sc, AR_PHY_TIMING_CTRL4(i));
   2045  1.1  christos 		reg = RW(reg, AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF, i_coff);
   2046  1.1  christos 		reg = RW(reg, AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF, q_coff);
   2047  1.1  christos 		AR_WRITE(sc, AR_PHY_TIMING_CTRL4(i), reg);
   2048  1.1  christos 	}
   2049  1.1  christos 
   2050  1.1  christos 	/* Apply new settings. */
   2051  1.1  christos 	AR_SETBITS(sc, AR_PHY_TIMING_CTRL4_0,
   2052  1.1  christos 	    AR_PHY_TIMING_CTRL4_IQCORR_ENABLE);
   2053  1.1  christos 	AR_WRITE_BARRIER(sc);
   2054  1.1  christos 
   2055  1.1  christos 	/* IQ calibration done. */
   2056  1.1  christos 	sc->sc_cur_calib_mask &= ~ATHN_CAL_IQ;
   2057  1.1  christos 	memset(&sc->sc_calib, 0, sizeof(sc->sc_calib));
   2058  1.1  christos }
   2059  1.1  christos 
   2060  1.1  christos Static void
   2061  1.1  christos ar5008_calib_adc_gain(struct athn_softc *sc)
   2062  1.1  christos {
   2063  1.1  christos 	struct athn_adc_cal *cal;
   2064  1.1  christos 	uint32_t reg, gain_mismatch_i, gain_mismatch_q;
   2065  1.1  christos 	int i;
   2066  1.1  christos 
   2067  1.1  christos 	for (i = 0; i < AR_MAX_CHAINS; i++) {
   2068  1.1  christos 		cal = &sc->sc_calib.adc_gain[i];
   2069  1.1  christos 
   2070  1.1  christos 		/* Accumulate ADC gain measures (clear on read). */
   2071  1.1  christos 		cal->pwr_meas_odd_i  += AR_READ(sc, AR_PHY_CAL_MEAS_0(i));
   2072  1.1  christos 		cal->pwr_meas_even_i += AR_READ(sc, AR_PHY_CAL_MEAS_1(i));
   2073  1.1  christos 		cal->pwr_meas_odd_q  += AR_READ(sc, AR_PHY_CAL_MEAS_2(i));
   2074  1.1  christos 		cal->pwr_meas_even_q += AR_READ(sc, AR_PHY_CAL_MEAS_3(i));
   2075  1.1  christos 	}
   2076  1.1  christos 	if (!AR_SREV_9280_10_OR_LATER(sc) &&
   2077  1.1  christos 	    ++sc->sc_calib.nsamples < AR_CAL_SAMPLES) {
   2078  1.1  christos 		/* Not enough samples accumulated, continue. */
   2079  1.1  christos 		ar5008_do_calib(sc);
   2080  1.1  christos 		return;
   2081  1.1  christos 	}
   2082  1.1  christos 
   2083  1.1  christos 	for (i = 0; i < sc->sc_nrxchains; i++) {
   2084  1.1  christos 		cal = &sc->sc_calib.adc_gain[i];
   2085  1.1  christos 
   2086  1.1  christos 		if (cal->pwr_meas_odd_i == 0 || cal->pwr_meas_even_q == 0)
   2087  1.1  christos 			continue;	/* Prevents division by zero. */
   2088  1.1  christos 
   2089  1.1  christos 		gain_mismatch_i =
   2090  1.1  christos 		    (cal->pwr_meas_even_i * 32) / cal->pwr_meas_odd_i;
   2091  1.1  christos 		gain_mismatch_q =
   2092  1.1  christos 		    (cal->pwr_meas_odd_q * 32) / cal->pwr_meas_even_q;
   2093  1.1  christos 
   2094  1.1  christos 		DPRINTFN(DBG_RF, sc, "ADC gain calibration for chain %d\n", i);
   2095  1.1  christos 		reg = AR_READ(sc, AR_PHY_NEW_ADC_DC_GAIN_CORR(i));
   2096  1.1  christos 		reg = RW(reg, AR_PHY_NEW_ADC_DC_GAIN_IGAIN, gain_mismatch_i);
   2097  1.1  christos 		reg = RW(reg, AR_PHY_NEW_ADC_DC_GAIN_QGAIN, gain_mismatch_q);
   2098  1.1  christos 		AR_WRITE(sc, AR_PHY_NEW_ADC_DC_GAIN_CORR(i), reg);
   2099  1.1  christos 	}
   2100  1.1  christos 
   2101  1.1  christos 	/* Apply new settings. */
   2102  1.1  christos 	AR_SETBITS(sc, AR_PHY_NEW_ADC_DC_GAIN_CORR(0),
   2103  1.1  christos 	    AR_PHY_NEW_ADC_GAIN_CORR_ENABLE);
   2104  1.1  christos 	AR_WRITE_BARRIER(sc);
   2105  1.1  christos 
   2106  1.1  christos 	/* ADC gain calibration done. */
   2107  1.1  christos 	sc->sc_cur_calib_mask &= ~ATHN_CAL_ADC_GAIN;
   2108  1.1  christos 	memset(&sc->sc_calib, 0, sizeof(sc->sc_calib));
   2109  1.1  christos }
   2110  1.1  christos 
   2111  1.1  christos Static void
   2112  1.1  christos ar5008_calib_adc_dc_off(struct athn_softc *sc)
   2113  1.1  christos {
   2114  1.1  christos 	struct athn_adc_cal *cal;
   2115  1.1  christos 	int32_t dc_offset_mismatch_i, dc_offset_mismatch_q;
   2116  1.1  christos 	uint32_t reg;
   2117  1.1  christos 	int count, i;
   2118  1.1  christos 
   2119  1.1  christos 	for (i = 0; i < AR_MAX_CHAINS; i++) {
   2120  1.1  christos 		cal = &sc->sc_calib.adc_dc_offset[i];
   2121  1.1  christos 
   2122  1.1  christos 		/* Accumulate ADC DC offset measures (clear on read). */
   2123  1.1  christos 		cal->pwr_meas_odd_i  += AR_READ(sc, AR_PHY_CAL_MEAS_0(i));
   2124  1.1  christos 		cal->pwr_meas_even_i += AR_READ(sc, AR_PHY_CAL_MEAS_1(i));
   2125  1.1  christos 		cal->pwr_meas_odd_q  += AR_READ(sc, AR_PHY_CAL_MEAS_2(i));
   2126  1.1  christos 		cal->pwr_meas_even_q += AR_READ(sc, AR_PHY_CAL_MEAS_3(i));
   2127  1.1  christos 	}
   2128  1.1  christos 	if (!AR_SREV_9280_10_OR_LATER(sc) &&
   2129  1.1  christos 	    ++sc->sc_calib.nsamples < AR_CAL_SAMPLES) {
   2130  1.1  christos 		/* Not enough samples accumulated, continue. */
   2131  1.1  christos 		ar5008_do_calib(sc);
   2132  1.1  christos 		return;
   2133  1.1  christos 	}
   2134  1.1  christos 
   2135  1.1  christos 	if (AR_SREV_9280_10_OR_LATER(sc))
   2136  1.1  christos 		count = (1 << (10 + 5));
   2137  1.1  christos 	else
   2138  1.1  christos 		count = (1 << ( 2 + 5)) * AR_CAL_SAMPLES;
   2139  1.1  christos 	for (i = 0; i < sc->sc_nrxchains; i++) {
   2140  1.1  christos 		cal = &sc->sc_calib.adc_dc_offset[i];
   2141  1.1  christos 
   2142  1.1  christos 		dc_offset_mismatch_i =
   2143  1.1  christos 		    (cal->pwr_meas_even_i - cal->pwr_meas_odd_i * 2) / count;
   2144  1.1  christos 		dc_offset_mismatch_q =
   2145  1.1  christos 		    (cal->pwr_meas_odd_q - cal->pwr_meas_even_q * 2) / count;
   2146  1.1  christos 
   2147  1.1  christos 		DPRINTFN(DBG_RF, sc, "ADC DC offset calibration for chain %d\n", i);
   2148  1.1  christos 		reg = AR_READ(sc, AR_PHY_NEW_ADC_DC_GAIN_CORR(i));
   2149  1.1  christos 		reg = RW(reg, AR_PHY_NEW_ADC_DC_GAIN_QDC,
   2150  1.1  christos 		    dc_offset_mismatch_q);
   2151  1.1  christos 		reg = RW(reg, AR_PHY_NEW_ADC_DC_GAIN_IDC,
   2152  1.1  christos 		    dc_offset_mismatch_i);
   2153  1.1  christos 		AR_WRITE(sc, AR_PHY_NEW_ADC_DC_GAIN_CORR(i), reg);
   2154  1.1  christos 	}
   2155  1.1  christos 
   2156  1.1  christos 	/* Apply new settings. */
   2157  1.1  christos 	AR_SETBITS(sc, AR_PHY_NEW_ADC_DC_GAIN_CORR(0),
   2158  1.1  christos 	    AR_PHY_NEW_ADC_DC_OFFSET_CORR_ENABLE);
   2159  1.1  christos 	AR_WRITE_BARRIER(sc);
   2160  1.1  christos 
   2161  1.1  christos 	/* ADC DC offset calibration done. */
   2162  1.1  christos 	sc->sc_cur_calib_mask &= ~ATHN_CAL_ADC_DC;
   2163  1.1  christos 	memset(&sc->sc_calib, 0, sizeof(sc->sc_calib));
   2164  1.1  christos }
   2165  1.1  christos 
   2166  1.1  christos PUBLIC void
   2167  1.1  christos ar5008_write_txpower(struct athn_softc *sc, int16_t power[ATHN_POWER_COUNT])
   2168  1.1  christos {
   2169  1.1  christos 
   2170  1.1  christos 	AR_WRITE(sc, AR_PHY_POWER_TX_RATE1,
   2171  1.1  christos 	    (power[ATHN_POWER_OFDM18  ] & 0x3f) << 24 |
   2172  1.1  christos 	    (power[ATHN_POWER_OFDM12  ] & 0x3f) << 16 |
   2173  1.1  christos 	    (power[ATHN_POWER_OFDM9   ] & 0x3f) <<  8 |
   2174  1.1  christos 	    (power[ATHN_POWER_OFDM6   ] & 0x3f));
   2175  1.1  christos 	AR_WRITE(sc, AR_PHY_POWER_TX_RATE2,
   2176  1.1  christos 	    (power[ATHN_POWER_OFDM54  ] & 0x3f) << 24 |
   2177  1.1  christos 	    (power[ATHN_POWER_OFDM48  ] & 0x3f) << 16 |
   2178  1.1  christos 	    (power[ATHN_POWER_OFDM36  ] & 0x3f) <<  8 |
   2179  1.1  christos 	    (power[ATHN_POWER_OFDM24  ] & 0x3f));
   2180  1.1  christos 	AR_WRITE(sc, AR_PHY_POWER_TX_RATE3,
   2181  1.1  christos 	    (power[ATHN_POWER_CCK2_SP ] & 0x3f) << 24 |
   2182  1.1  christos 	    (power[ATHN_POWER_CCK2_LP ] & 0x3f) << 16 |
   2183  1.1  christos 	    (power[ATHN_POWER_XR      ] & 0x3f) <<  8 |
   2184  1.1  christos 	    (power[ATHN_POWER_CCK1_LP ] & 0x3f));
   2185  1.1  christos 	AR_WRITE(sc, AR_PHY_POWER_TX_RATE4,
   2186  1.1  christos 	    (power[ATHN_POWER_CCK11_SP] & 0x3f) << 24 |
   2187  1.1  christos 	    (power[ATHN_POWER_CCK11_LP] & 0x3f) << 16 |
   2188  1.1  christos 	    (power[ATHN_POWER_CCK55_SP] & 0x3f) <<  8 |
   2189  1.1  christos 	    (power[ATHN_POWER_CCK55_LP] & 0x3f));
   2190  1.1  christos #ifndef IEEE80211_NO_HT
   2191  1.1  christos 	AR_WRITE(sc, AR_PHY_POWER_TX_RATE5,
   2192  1.1  christos 	    (power[ATHN_POWER_HT20(3) ] & 0x3f) << 24 |
   2193  1.1  christos 	    (power[ATHN_POWER_HT20(2) ] & 0x3f) << 16 |
   2194  1.1  christos 	    (power[ATHN_POWER_HT20(1) ] & 0x3f) <<  8 |
   2195  1.1  christos 	    (power[ATHN_POWER_HT20(0) ] & 0x3f));
   2196  1.1  christos 	AR_WRITE(sc, AR_PHY_POWER_TX_RATE6,
   2197  1.1  christos 	    (power[ATHN_POWER_HT20(7) ] & 0x3f) << 24 |
   2198  1.1  christos 	    (power[ATHN_POWER_HT20(6) ] & 0x3f) << 16 |
   2199  1.1  christos 	    (power[ATHN_POWER_HT20(5) ] & 0x3f) <<  8 |
   2200  1.1  christos 	    (power[ATHN_POWER_HT20(4) ] & 0x3f));
   2201  1.1  christos 	AR_WRITE(sc, AR_PHY_POWER_TX_RATE7,
   2202  1.1  christos 	    (power[ATHN_POWER_HT40(3) ] & 0x3f) << 24 |
   2203  1.1  christos 	    (power[ATHN_POWER_HT40(2) ] & 0x3f) << 16 |
   2204  1.1  christos 	    (power[ATHN_POWER_HT40(1) ] & 0x3f) <<  8 |
   2205  1.1  christos 	    (power[ATHN_POWER_HT40(0) ] & 0x3f));
   2206  1.1  christos 	AR_WRITE(sc, AR_PHY_POWER_TX_RATE8,
   2207  1.1  christos 	    (power[ATHN_POWER_HT40(7) ] & 0x3f) << 24 |
   2208  1.1  christos 	    (power[ATHN_POWER_HT40(6) ] & 0x3f) << 16 |
   2209  1.1  christos 	    (power[ATHN_POWER_HT40(5) ] & 0x3f) <<  8 |
   2210  1.1  christos 	    (power[ATHN_POWER_HT40(4) ] & 0x3f));
   2211  1.1  christos 	AR_WRITE(sc, AR_PHY_POWER_TX_RATE9,
   2212  1.1  christos 	    (power[ATHN_POWER_OFDM_EXT] & 0x3f) << 24 |
   2213  1.1  christos 	    (power[ATHN_POWER_CCK_EXT ] & 0x3f) << 16 |
   2214  1.1  christos 	    (power[ATHN_POWER_OFDM_DUP] & 0x3f) <<  8 |
   2215  1.1  christos 	    (power[ATHN_POWER_CCK_DUP ] & 0x3f));
   2216  1.1  christos #endif
   2217  1.1  christos 	AR_WRITE_BARRIER(sc);
   2218  1.1  christos }
   2219  1.1  christos 
   2220  1.1  christos PUBLIC void
   2221  1.1  christos ar5008_set_viterbi_mask(struct athn_softc *sc, int bin)
   2222  1.1  christos {
   2223  1.1  christos 	uint32_t mask[4], reg;
   2224  1.1  christos 	uint8_t m[62], p[62];	/* XXX use bit arrays? */
   2225  1.1  christos 	int i, bit, cur;
   2226  1.1  christos 
   2227  1.1  christos 	/* Compute pilot mask. */
   2228  1.1  christos 	cur = -6000;
   2229  1.1  christos 	for (i = 0; i < 4; i++) {
   2230  1.1  christos 		mask[i] = 0;
   2231  1.1  christos 		for (bit = 0; bit < 30; bit++) {
   2232  1.1  christos 			if (abs(cur - bin) < 100)
   2233  1.1  christos 				mask[i] |= 1 << bit;
   2234  1.1  christos 			cur += 100;
   2235  1.1  christos 		}
   2236  1.1  christos 		if (cur == 0)	/* Skip entry "0". */
   2237  1.1  christos 			cur = 100;
   2238  1.1  christos 	}
   2239  1.1  christos 	/* Write entries from -6000 to -3100. */
   2240  1.1  christos 	AR_WRITE(sc, AR_PHY_TIMING7, mask[0]);
   2241  1.1  christos 	AR_WRITE(sc, AR_PHY_TIMING9, mask[0]);
   2242  1.1  christos 	/* Write entries from -3000 to -100. */
   2243  1.1  christos 	AR_WRITE(sc, AR_PHY_TIMING8, mask[1]);
   2244  1.1  christos 	AR_WRITE(sc, AR_PHY_TIMING10, mask[1]);
   2245  1.1  christos 	/* Write entries from 100 to 3000. */
   2246  1.1  christos 	AR_WRITE(sc, AR_PHY_PILOT_MASK_01_30, mask[2]);
   2247  1.1  christos 	AR_WRITE(sc, AR_PHY_CHANNEL_MASK_01_30, mask[2]);
   2248  1.1  christos 	/* Write entries from 3100 to 6000. */
   2249  1.1  christos 	AR_WRITE(sc, AR_PHY_PILOT_MASK_31_60, mask[3]);
   2250  1.1  christos 	AR_WRITE(sc, AR_PHY_CHANNEL_MASK_31_60, mask[3]);
   2251  1.1  christos 
   2252  1.1  christos 	/* Compute viterbi mask. */
   2253  1.1  christos 	for (cur = 6100; cur >= 0; cur -= 100)
   2254  1.1  christos 		p[+cur / 100] = abs(cur - bin) < 75;
   2255  1.1  christos 	for (cur = -100; cur >= -6100; cur -= 100)
   2256  1.1  christos 		m[-cur / 100] = abs(cur - bin) < 75;
   2257  1.1  christos 
   2258  1.1  christos 	/* Write viterbi mask (XXX needs to be reworked). */
   2259  1.1  christos 	reg =
   2260  1.1  christos 	    m[46] << 30 | m[47] << 28 | m[48] << 26 | m[49] << 24 |
   2261  1.1  christos 	    m[50] << 22 | m[51] << 20 | m[52] << 18 | m[53] << 16 |
   2262  1.1  christos 	    m[54] << 14 | m[55] << 12 | m[56] << 10 | m[57] <<  8 |
   2263  1.1  christos 	    m[58] <<  6 | m[59] <<  4 | m[60] <<  2 | m[61] <<  0;
   2264  1.1  christos 	AR_WRITE(sc, AR_PHY_BIN_MASK_1, reg);
   2265  1.1  christos 	AR_WRITE(sc, AR_PHY_VIT_MASK2_M_46_61, reg);
   2266  1.1  christos 
   2267  1.1  christos 	/* XXX m[48] should be m[38] ? */
   2268  1.1  christos 	reg =             m[31] << 28 | m[32] << 26 | m[33] << 24 |
   2269  1.1  christos 	    m[34] << 22 | m[35] << 20 | m[36] << 18 | m[37] << 16 |
   2270  1.1  christos 	    m[48] << 14 | m[39] << 12 | m[40] << 10 | m[41] <<  8 |
   2271  1.1  christos 	    m[42] <<  6 | m[43] <<  4 | m[44] <<  2 | m[45] <<  0;
   2272  1.1  christos 	AR_WRITE(sc, AR_PHY_BIN_MASK_2, reg);
   2273  1.1  christos 	AR_WRITE(sc, AR_PHY_VIT_MASK2_M_31_45, reg);
   2274  1.1  christos 
   2275  1.1  christos 	/* XXX This one is weird too. */
   2276  1.1  christos 	reg =
   2277  1.1  christos 	    m[16] << 30 | m[16] << 28 | m[18] << 26 | m[18] << 24 |
   2278  1.1  christos 	    m[20] << 22 | m[20] << 20 | m[22] << 18 | m[22] << 16 |
   2279  1.1  christos 	    m[24] << 14 | m[24] << 12 | m[25] << 10 | m[26] <<  8 |
   2280  1.1  christos 	    m[27] <<  6 | m[28] <<  4 | m[29] <<  2 | m[30] <<  0;
   2281  1.1  christos 	AR_WRITE(sc, AR_PHY_BIN_MASK_3, reg);
   2282  1.1  christos 	AR_WRITE(sc, AR_PHY_VIT_MASK2_M_16_30, reg);
   2283  1.1  christos 
   2284  1.1  christos 	reg =
   2285  1.1  christos 	    m[ 0] << 30 | m[ 1] << 28 | m[ 2] << 26 | m[ 3] << 24 |
   2286  1.1  christos 	    m[ 4] << 22 | m[ 5] << 20 | m[ 6] << 18 | m[ 7] << 16 |
   2287  1.1  christos 	    m[ 8] << 14 | m[ 9] << 12 | m[10] << 10 | m[11] <<  8 |
   2288  1.1  christos 	    m[12] <<  6 | m[13] <<  4 | m[14] <<  2 | m[15] <<  0;
   2289  1.1  christos 	AR_WRITE(sc, AR_PHY_MASK_CTL, reg);
   2290  1.1  christos 	AR_WRITE(sc, AR_PHY_VIT_MASK2_M_00_15, reg);
   2291  1.1  christos 
   2292  1.1  christos 	reg =             p[15] << 28 | p[14] << 26 | p[13] << 24 |
   2293  1.1  christos 	    p[12] << 22 | p[11] << 20 | p[10] << 18 | p[ 9] << 16 |
   2294  1.1  christos 	    p[ 8] << 14 | p[ 7] << 12 | p[ 6] << 10 | p[ 5] <<  8 |
   2295  1.1  christos 	    p[ 4] <<  6 | p[ 3] <<  4 | p[ 2] <<  2 | p[ 1] <<  0;
   2296  1.1  christos 	AR_WRITE(sc, AR_PHY_BIN_MASK2_1, reg);
   2297  1.1  christos 	AR_WRITE(sc, AR_PHY_VIT_MASK2_P_15_01, reg);
   2298  1.1  christos 
   2299  1.1  christos 	reg =             p[30] << 28 | p[29] << 26 | p[28] << 24 |
   2300  1.1  christos 	    p[27] << 22 | p[26] << 20 | p[25] << 18 | p[24] << 16 |
   2301  1.1  christos 	    p[23] << 14 | p[22] << 12 | p[21] << 10 | p[20] <<  8 |
   2302  1.1  christos 	    p[19] <<  6 | p[18] <<  4 | p[17] <<  2 | p[16] <<  0;
   2303  1.1  christos 	AR_WRITE(sc, AR_PHY_BIN_MASK2_2, reg);
   2304  1.1  christos 	AR_WRITE(sc, AR_PHY_VIT_MASK2_P_30_16, reg);
   2305  1.1  christos 
   2306  1.1  christos 	reg =             p[45] << 28 | p[44] << 26 | p[43] << 24 |
   2307  1.1  christos 	    p[42] << 22 | p[41] << 20 | p[40] << 18 | p[39] << 16 |
   2308  1.1  christos 	    p[38] << 14 | p[37] << 12 | p[36] << 10 | p[35] <<  8 |
   2309  1.1  christos 	    p[34] <<  6 | p[33] <<  4 | p[32] <<  2 | p[31] <<  0;
   2310  1.1  christos 	AR_WRITE(sc, AR_PHY_BIN_MASK2_3, reg);
   2311  1.1  christos 	AR_WRITE(sc, AR_PHY_VIT_MASK2_P_45_31, reg);
   2312  1.1  christos 
   2313  1.1  christos 	reg =
   2314  1.1  christos 	    p[61] << 30 | p[60] << 28 | p[59] << 26 | p[58] << 24 |
   2315  1.1  christos 	    p[57] << 22 | p[56] << 20 | p[55] << 18 | p[54] << 16 |
   2316  1.1  christos 	    p[53] << 14 | p[52] << 12 | p[51] << 10 | p[50] <<  8 |
   2317  1.1  christos 	    p[49] <<  6 | p[48] <<  4 | p[47] <<  2 | p[46] <<  0;
   2318  1.1  christos 	AR_WRITE(sc, AR_PHY_BIN_MASK2_4, reg);
   2319  1.1  christos 	AR_WRITE(sc, AR_PHY_VIT_MASK2_P_61_46, reg);
   2320  1.1  christos 	AR_WRITE_BARRIER(sc);
   2321  1.1  christos }
   2322  1.1  christos 
   2323  1.1  christos Static void
   2324  1.1  christos ar5008_hw_init(struct athn_softc *sc, struct ieee80211_channel *c,
   2325  1.1  christos     struct ieee80211_channel *extc)
   2326  1.1  christos {
   2327  1.1  christos 	struct athn_ops *ops = &sc->sc_ops;
   2328  1.1  christos 	const struct athn_ini *ini = sc->sc_ini;
   2329  1.1  christos 	const uint32_t *pvals;
   2330  1.1  christos 	uint32_t reg;
   2331  1.1  christos 	int i;
   2332  1.1  christos 
   2333  1.1  christos 	AR_WRITE(sc, AR_PHY(0), 0x00000007);
   2334  1.1  christos 	AR_WRITE(sc, AR_PHY_ADC_SERIAL_CTL, AR_PHY_SEL_EXTERNAL_RADIO);
   2335  1.1  christos 
   2336  1.1  christos 	if (!AR_SINGLE_CHIP(sc))
   2337  1.1  christos 		ar5416_reset_addac(sc, c);
   2338  1.1  christos 
   2339  1.1  christos 	AR_WRITE(sc, AR_PHY_ADC_SERIAL_CTL, AR_PHY_SEL_INTERNAL_ADDAC);
   2340  1.1  christos 
   2341  1.1  christos 	/* First initialization step (depends on channel band/bandwidth). */
   2342  1.1  christos #ifndef IEEE80211_NO_HT
   2343  1.1  christos 	if (extc != NULL) {
   2344  1.1  christos 		if (IEEE80211_IS_CHAN_2GHZ(c))
   2345  1.1  christos 			pvals = ini->vals_2g40;
   2346  1.1  christos 		else
   2347  1.1  christos 			pvals = ini->vals_5g40;
   2348  1.1  christos 	}
   2349  1.1  christos 	else
   2350  1.1  christos #endif
   2351  1.1  christos 	{
   2352  1.1  christos 		if (IEEE80211_IS_CHAN_2GHZ(c))
   2353  1.1  christos 			pvals = ini->vals_2g20;
   2354  1.1  christos 		else
   2355  1.1  christos 			pvals = ini->vals_5g20;
   2356  1.1  christos 	}
   2357  1.1  christos 	DPRINTFN(DBG_INIT, sc, "writing modal init vals\n");
   2358  1.1  christos 	for (i = 0; i < ini->nregs; i++) {
   2359  1.1  christos 		uint32_t val = pvals[i];
   2360  1.1  christos 
   2361  1.1  christos 		/* Fix AR_AN_TOP2 initialization value if required. */
   2362  1.1  christos 		if (ini->regs[i] == AR_AN_TOP2 &&
   2363  1.1  christos 		    (sc->sc_flags & ATHN_FLAG_AN_TOP2_FIXUP))
   2364  1.1  christos 			val &= ~AR_AN_TOP2_PWDCLKIND;
   2365  1.1  christos 		AR_WRITE(sc, ini->regs[i], val);
   2366  1.1  christos 		if (AR_IS_ANALOG_REG(ini->regs[i])) {
   2367  1.1  christos 			AR_WRITE_BARRIER(sc);
   2368  1.1  christos 			DELAY(100);
   2369  1.1  christos 		}
   2370  1.1  christos 		if ((i & 0x1f) == 0)
   2371  1.1  christos 			DELAY(1);
   2372  1.1  christos 	}
   2373  1.1  christos 	AR_WRITE_BARRIER(sc);
   2374  1.1  christos 
   2375  1.1  christos 	if (sc->sc_rx_gain != NULL)
   2376  1.1  christos 		ar9280_reset_rx_gain(sc, c);
   2377  1.1  christos 	if (sc->sc_tx_gain != NULL)
   2378  1.1  christos 		ar9280_reset_tx_gain(sc, c);
   2379  1.1  christos 
   2380  1.1  christos 	if (AR_SREV_9271_10(sc)) {
   2381  1.1  christos 		AR_WRITE(sc, AR_PHY(68), 0x30002311);
   2382  1.1  christos 		AR_WRITE(sc, AR_PHY_RF_CTL3, 0x0a020001);
   2383  1.1  christos 	}
   2384  1.1  christos 	AR_WRITE_BARRIER(sc);
   2385  1.1  christos 
   2386  1.1  christos 	/* Second initialization step (common to all channels). */
   2387  1.1  christos 	DPRINTFN(DBG_INIT, sc, "writing common init vals\n");
   2388  1.1  christos 	for (i = 0; i < ini->ncmregs; i++) {
   2389  1.1  christos 		AR_WRITE(sc, ini->cmregs[i], ini->cmvals[i]);
   2390  1.1  christos 		if (AR_IS_ANALOG_REG(ini->cmregs[i])) {
   2391  1.1  christos 			AR_WRITE_BARRIER(sc);
   2392  1.1  christos 			DELAY(100);
   2393  1.1  christos 		}
   2394  1.1  christos 		if ((i & 0x1f) == 0)
   2395  1.1  christos 			DELAY(1);
   2396  1.1  christos 	}
   2397  1.1  christos 	AR_WRITE_BARRIER(sc);
   2398  1.1  christos 
   2399  1.1  christos 	if (!AR_SINGLE_CHIP(sc))
   2400  1.1  christos 		ar5416_reset_bb_gain(sc, c);
   2401  1.1  christos 
   2402  1.1  christos 	if (IEEE80211_IS_CHAN_5GHZ(c) &&
   2403  1.1  christos 	    (sc->sc_flags & ATHN_FLAG_FAST_PLL_CLOCK)) {
   2404  1.1  christos 		/* Update modal values for fast PLL clock. */
   2405  1.1  christos #ifndef IEEE80211_NO_HT
   2406  1.1  christos 		if (extc != NULL)
   2407  1.1  christos 			pvals = ini->fastvals_5g40;
   2408  1.1  christos 		else
   2409  1.1  christos #endif
   2410  1.1  christos 			pvals = ini->fastvals_5g20;
   2411  1.1  christos 		DPRINTFN(DBG_INIT, sc, "writing fast pll clock init vals\n");
   2412  1.1  christos 		for (i = 0; i < ini->nfastregs; i++) {
   2413  1.1  christos 			AR_WRITE(sc, ini->fastregs[i], pvals[i]);
   2414  1.1  christos 			if (AR_IS_ANALOG_REG(ini->fastregs[i])) {
   2415  1.1  christos 				AR_WRITE_BARRIER(sc);
   2416  1.1  christos 				DELAY(100);
   2417  1.1  christos 			}
   2418  1.1  christos 			if ((i & 0x1f) == 0)
   2419  1.1  christos 				DELAY(1);
   2420  1.1  christos 		}
   2421  1.1  christos 	}
   2422  1.1  christos 
   2423  1.1  christos 	/*
   2424  1.1  christos 	 * Set the RX_ABORT and RX_DIS bits to prevent frames with corrupted
   2425  1.1  christos 	 * descriptor status.
   2426  1.1  christos 	 */
   2427  1.1  christos 	AR_SETBITS(sc, AR_DIAG_SW, AR_DIAG_RX_DIS | AR_DIAG_RX_ABORT);
   2428  1.1  christos 
   2429  1.1  christos 	/* Hardware workarounds for occasional Rx data corruption. */
   2430  1.1  christos 	if (AR_SREV_9280_10_OR_LATER(sc)) {
   2431  1.1  christos 		reg = AR_READ(sc, AR_PCU_MISC_MODE2);
   2432  1.1  christos 		if (!AR_SREV_9271(sc))
   2433  1.1  christos 			reg &= ~AR_PCU_MISC_MODE2_HWWAR1;
   2434  1.1  christos 		if (AR_SREV_9287_10_OR_LATER(sc))
   2435  1.1  christos 			reg &= ~AR_PCU_MISC_MODE2_HWWAR2;
   2436  1.1  christos 		AR_WRITE(sc, AR_PCU_MISC_MODE2, reg);
   2437  1.1  christos 
   2438  1.1  christos 	}
   2439  1.1  christos 	else if (AR_SREV_5416_20_OR_LATER(sc)) {
   2440  1.1  christos 		/* Disable baseband clock gating. */
   2441  1.1  christos 		AR_WRITE(sc, AR_PHY(651), 0x11);
   2442  1.1  christos 
   2443  1.1  christos 		if (AR_SREV_9160(sc)) {
   2444  1.1  christos 			/* Disable RIFS search to fix baseband hang. */
   2445  1.1  christos 			AR_CLRBITS(sc, AR_PHY_HEAVY_CLIP_FACTOR_RIFS,
   2446  1.1  christos 			    AR_PHY_RIFS_INIT_DELAY_M);
   2447  1.1  christos 		}
   2448  1.1  christos 	}
   2449  1.1  christos 	AR_WRITE_BARRIER(sc);
   2450  1.1  christos 
   2451  1.1  christos 	ar5008_set_phy(sc, c, extc);
   2452  1.1  christos 	ar5008_init_chains(sc);
   2453  1.1  christos 
   2454  1.1  christos 	if (sc->sc_flags & ATHN_FLAG_OLPC) {
   2455  1.1  christos 		extern int ticks;
   2456  1.1  christos 		sc->sc_olpc_ticks = ticks;
   2457  1.1  christos 		ops->olpc_init(sc);
   2458  1.1  christos 	}
   2459  1.1  christos 
   2460  1.1  christos 	ops->set_txpower(sc, c, extc);
   2461  1.1  christos 
   2462  1.1  christos 	if (!AR_SINGLE_CHIP(sc))
   2463  1.1  christos 		ar5416_rf_reset(sc, c);
   2464  1.1  christos }
   2465  1.1  christos 
   2466  1.1  christos Static uint8_t
   2467  1.1  christos ar5008_get_vpd(uint8_t pwr, const uint8_t *pwrPdg, const uint8_t *vpdPdg,
   2468  1.1  christos     int nicepts)
   2469  1.1  christos {
   2470  1.1  christos 	uint8_t vpd;
   2471  1.1  christos 	int i, lo, hi;
   2472  1.1  christos 
   2473  1.1  christos 	for (i = 0; i < nicepts; i++)
   2474  1.1  christos 		if (pwrPdg[i] > pwr)
   2475  1.1  christos 			break;
   2476  1.1  christos 	hi = i;
   2477  1.1  christos 	lo = hi - 1;
   2478  1.1  christos 	if (lo == -1)
   2479  1.1  christos 		lo = hi;
   2480  1.1  christos 	else if (hi == nicepts)
   2481  1.1  christos 		hi = lo;
   2482  1.1  christos 
   2483  1.1  christos 	vpd = athn_interpolate(pwr, pwrPdg[lo], vpdPdg[lo],
   2484  1.1  christos 	    pwrPdg[hi], vpdPdg[hi]);
   2485  1.1  christos 	return vpd;
   2486  1.1  christos }
   2487  1.1  christos 
   2488  1.1  christos PUBLIC void
   2489  1.1  christos ar5008_get_pdadcs(struct athn_softc *sc, uint8_t fbin,
   2490  1.1  christos     struct athn_pier *lopier, struct athn_pier *hipier, int nxpdgains,
   2491  1.1  christos     int nicepts, uint8_t overlap, uint8_t *boundaries, uint8_t *pdadcs)
   2492  1.1  christos {
   2493  1.1  christos #define DB(x)	((x) / 2)	/* Convert half dB to dB. */
   2494  1.1  christos 	uint8_t minpwr[AR_PD_GAINS_IN_MASK], maxpwr[AR_PD_GAINS_IN_MASK];
   2495  1.1  christos 	uint8_t vpd[AR_MAX_PWR_RANGE_IN_HALF_DB], pwr;
   2496  1.1  christos 	uint8_t lovpd, hivpd, boundary;
   2497  1.1  christos 	int16_t ss, delta, vpdstep, val;
   2498  1.1  christos 	int i, j, npdadcs, nvpds, maxidx, tgtidx;
   2499  1.1  christos 
   2500  1.1  christos 	/* Compute min and max power in half dB for each pdGain. */
   2501  1.1  christos 	for (i = 0; i < nxpdgains; i++) {
   2502  1.1  christos 		minpwr[i] = MAX(lopier->pwr[i][0], hipier->pwr[i][0]);
   2503  1.1  christos 		maxpwr[i] = MIN(lopier->pwr[i][nicepts - 1],
   2504  1.1  christos 		    hipier->pwr[i][nicepts - 1]);
   2505  1.1  christos 	}
   2506  1.1  christos 
   2507  1.1  christos 	/* Fill phase domain analog-to-digital converter (PDADC) table. */
   2508  1.1  christos 	npdadcs = 0;
   2509  1.1  christos 	for (i = 0; i < nxpdgains; i++) {
   2510  1.1  christos 		if (i != nxpdgains - 1)
   2511  1.1  christos 			boundaries[i] = DB(maxpwr[i] + minpwr[i + 1]) / 2;
   2512  1.1  christos 		else
   2513  1.1  christos 			boundaries[i] = DB(maxpwr[i]);
   2514  1.1  christos 		if (boundaries[i] > AR_MAX_RATE_POWER)
   2515  1.1  christos 			boundaries[i] = AR_MAX_RATE_POWER;
   2516  1.1  christos 
   2517  1.1  christos 		if (i == 0 && !AR_SREV_5416_20_OR_LATER(sc)) {
   2518  1.1  christos 			/* Fix the gain delta (AR5416 1.0 only). */
   2519  1.1  christos 			delta = boundaries[0] - 23;
   2520  1.1  christos 			boundaries[0] = 23;
   2521  1.1  christos 		}
   2522  1.1  christos 		else
   2523  1.1  christos 			delta = 0;
   2524  1.1  christos 
   2525  1.1  christos 		/* Find starting index for this pdGain. */
   2526  1.1  christos 		if (i != 0) {
   2527  1.1  christos 			ss = boundaries[i - 1] - DB(minpwr[i]) -
   2528  1.1  christos 			    overlap + 1 + delta;
   2529  1.1  christos 		}
   2530  1.1  christos 		else if (AR_SREV_9280_10_OR_LATER(sc))
   2531  1.1  christos 			ss = -DB(minpwr[i]);
   2532  1.1  christos 		else
   2533  1.1  christos 			ss = 0;
   2534  1.1  christos 
   2535  1.1  christos 		/* Compute Vpd table for this pdGain. */
   2536  1.1  christos 		nvpds = DB(maxpwr[i] - minpwr[i]) + 1;
   2537  1.1  christos 		memset(vpd, 0, sizeof(vpd));
   2538  1.1  christos 		pwr = minpwr[i];
   2539  1.1  christos 		for (j = 0; j < nvpds; j++) {
   2540  1.1  christos 			/* Get lower and higher Vpd. */
   2541  1.1  christos 			lovpd = ar5008_get_vpd(pwr, lopier->pwr[i],
   2542  1.1  christos 			    lopier->vpd[i], nicepts);
   2543  1.1  christos 			hivpd = ar5008_get_vpd(pwr, hipier->pwr[i],
   2544  1.1  christos 			    hipier->vpd[i], nicepts);
   2545  1.1  christos 
   2546  1.1  christos 			/* Interpolate the final Vpd. */
   2547  1.1  christos 			vpd[j] = athn_interpolate(fbin,
   2548  1.1  christos 			    lopier->fbin, lovpd, hipier->fbin, hivpd);
   2549  1.1  christos 
   2550  1.1  christos 			pwr += 2;	/* In half dB. */
   2551  1.1  christos 		}
   2552  1.1  christos 
   2553  1.1  christos 		/* Extrapolate data for ss < 0. */
   2554  1.1  christos 		if (vpd[1] > vpd[0])
   2555  1.1  christos 			vpdstep = vpd[1] - vpd[0];
   2556  1.1  christos 		else
   2557  1.1  christos 			vpdstep = 1;
   2558  1.1  christos 		while (ss < 0 && npdadcs < AR_NUM_PDADC_VALUES - 1) {
   2559  1.1  christos 			val = vpd[0] + ss * vpdstep;
   2560  1.1  christos 			pdadcs[npdadcs++] = MAX(val, 0);
   2561  1.1  christos 			ss++;
   2562  1.1  christos 		}
   2563  1.1  christos 
   2564  1.1  christos 		tgtidx = boundaries[i] + overlap - DB(minpwr[i]);
   2565  1.1  christos 		maxidx = MIN(tgtidx, nvpds);
   2566  1.1  christos 		while (ss < maxidx && npdadcs < AR_NUM_PDADC_VALUES - 1)
   2567  1.1  christos 			pdadcs[npdadcs++] = vpd[ss++];
   2568  1.1  christos 
   2569  1.1  christos 		if (tgtidx < maxidx)
   2570  1.1  christos 			continue;
   2571  1.1  christos 
   2572  1.1  christos 		/* Extrapolate data for maxidx <= ss <= tgtidx. */
   2573  1.1  christos 		if (vpd[nvpds - 1] > vpd[nvpds - 2])
   2574  1.1  christos 			vpdstep = vpd[nvpds - 1] - vpd[nvpds - 2];
   2575  1.1  christos 		else
   2576  1.1  christos 			vpdstep = 1;
   2577  1.1  christos 		while (ss <= tgtidx && npdadcs < AR_NUM_PDADC_VALUES - 1) {
   2578  1.1  christos 			val = vpd[nvpds - 1] + (ss - maxidx + 1) * vpdstep;
   2579  1.1  christos 			pdadcs[npdadcs++] = MIN(val, 255);
   2580  1.1  christos 			ss++;
   2581  1.1  christos 		}
   2582  1.1  christos 	}
   2583  1.1  christos 
   2584  1.1  christos 	/* Fill remaining PDADC and boundaries entries. */
   2585  1.1  christos 	if (AR_SREV_9285(sc))
   2586  1.1  christos 		boundary = AR9285_PD_GAIN_BOUNDARY_DEFAULT;
   2587  1.1  christos 	else	/* Fill with latest. */
   2588  1.1  christos 		boundary = boundaries[nxpdgains - 1];
   2589  1.1  christos 
   2590  1.1  christos 	for (; nxpdgains < AR_PD_GAINS_IN_MASK; nxpdgains++)
   2591  1.1  christos 		boundaries[nxpdgains] = boundary;
   2592  1.1  christos 
   2593  1.1  christos 	for (; npdadcs < AR_NUM_PDADC_VALUES; npdadcs++)
   2594  1.1  christos 		pdadcs[npdadcs] = pdadcs[npdadcs - 1];
   2595  1.1  christos #undef DB
   2596  1.1  christos }
   2597  1.1  christos 
   2598  1.1  christos PUBLIC void
   2599  1.1  christos ar5008_get_lg_tpow(struct athn_softc *sc, struct ieee80211_channel *c,
   2600  1.1  christos     uint8_t ctl, const struct ar_cal_target_power_leg *tgt, int nchans,
   2601  1.1  christos     uint8_t tpow[4])
   2602  1.1  christos {
   2603  1.1  christos 	uint8_t fbin;
   2604  1.1  christos 	int i, lo, hi;
   2605  1.1  christos 
   2606  1.1  christos 	/* Find interval (lower and upper indices). */
   2607  1.1  christos 	fbin = athn_chan2fbin(c);
   2608  1.1  christos 	for (i = 0; i < nchans; i++) {
   2609  1.1  christos 		if (tgt[i].bChannel == AR_BCHAN_UNUSED ||
   2610  1.1  christos 		    tgt[i].bChannel > fbin)
   2611  1.1  christos 			break;
   2612  1.1  christos 	}
   2613  1.1  christos 	hi = i;
   2614  1.1  christos 	lo = hi - 1;
   2615  1.1  christos 	if (lo == -1)
   2616  1.1  christos 		lo = hi;
   2617  1.1  christos 	else if (hi == nchans || tgt[hi].bChannel == AR_BCHAN_UNUSED)
   2618  1.1  christos 		hi = lo;
   2619  1.1  christos 
   2620  1.1  christos 	/* Interpolate values. */
   2621  1.1  christos 	for (i = 0; i < 4; i++) {
   2622  1.1  christos 		tpow[i] = athn_interpolate(fbin,
   2623  1.1  christos 		    tgt[lo].bChannel, tgt[lo].tPow2x[i],
   2624  1.1  christos 		    tgt[hi].bChannel, tgt[hi].tPow2x[i]);
   2625  1.1  christos 	}
   2626  1.1  christos 	/* XXX Apply conformance testing limit. */
   2627  1.1  christos }
   2628  1.1  christos 
   2629  1.1  christos #ifndef IEEE80211_NO_HT
   2630  1.1  christos PUBLIC void
   2631  1.1  christos ar5008_get_ht_tpow(struct athn_softc *sc, struct ieee80211_channel *c,
   2632  1.1  christos     uint8_t ctl, const struct ar_cal_target_power_ht *tgt, int nchans,
   2633  1.1  christos     uint8_t tpow[8])
   2634  1.1  christos {
   2635  1.1  christos 	uint8_t fbin;
   2636  1.1  christos 	int i, lo, hi;
   2637  1.1  christos 
   2638  1.1  christos 	/* Find interval (lower and upper indices). */
   2639  1.1  christos 	fbin = athn_chan2fbin(c);
   2640  1.1  christos 	for (i = 0; i < nchans; i++) {
   2641  1.1  christos 		if (tgt[i].bChannel == AR_BCHAN_UNUSED ||
   2642  1.1  christos 		    tgt[i].bChannel > fbin)
   2643  1.1  christos 			break;
   2644  1.1  christos 	}
   2645  1.1  christos 	hi = i;
   2646  1.1  christos 	lo = hi - 1;
   2647  1.1  christos 	if (lo == -1)
   2648  1.1  christos 		lo = hi;
   2649  1.1  christos 	else if (hi == nchans || tgt[hi].bChannel == AR_BCHAN_UNUSED)
   2650  1.1  christos 		hi = lo;
   2651  1.1  christos 
   2652  1.1  christos 	/* Interpolate values. */
   2653  1.1  christos 	for (i = 0; i < 8; i++) {
   2654  1.1  christos 		tpow[i] = athn_interpolate(fbin,
   2655  1.1  christos 		    tgt[lo].bChannel, tgt[lo].tPow2x[i],
   2656  1.1  christos 		    tgt[hi].bChannel, tgt[hi].tPow2x[i]);
   2657  1.1  christos 	}
   2658  1.1  christos 	/* XXX Apply conformance testing limit. */
   2659  1.1  christos }
   2660  1.1  christos #endif
   2661  1.1  christos 
   2662  1.1  christos /*
   2663  1.1  christos  * Adaptive noise immunity.
   2664  1.1  christos  */
   2665  1.1  christos Static void
   2666  1.1  christos ar5008_set_noise_immunity_level(struct athn_softc *sc, int level)
   2667  1.1  christos {
   2668  1.1  christos 	int high = level == 4;
   2669  1.1  christos 	uint32_t reg;
   2670  1.1  christos 
   2671  1.1  christos 	reg = AR_READ(sc, AR_PHY_DESIRED_SZ);
   2672  1.1  christos 	reg = RW(reg, AR_PHY_DESIRED_SZ_TOT_DES, high ? -62 : -55);
   2673  1.1  christos 	AR_WRITE(sc, AR_PHY_DESIRED_SZ, reg);
   2674  1.1  christos 
   2675  1.1  christos 	reg = AR_READ(sc, AR_PHY_AGC_CTL1);
   2676  1.1  christos 	reg = RW(reg, AR_PHY_AGC_CTL1_COARSE_LOW, high ? -70 : -64);
   2677  1.1  christos 	reg = RW(reg, AR_PHY_AGC_CTL1_COARSE_HIGH, high ? -12 : -14);
   2678  1.1  christos 	AR_WRITE(sc, AR_PHY_AGC_CTL1, reg);
   2679  1.1  christos 
   2680  1.1  christos 	reg = AR_READ(sc, AR_PHY_FIND_SIG);
   2681  1.1  christos 	reg = RW(reg, AR_PHY_FIND_SIG_FIRPWR, high ? -80 : -78);
   2682  1.1  christos 	AR_WRITE(sc, AR_PHY_FIND_SIG, reg);
   2683  1.1  christos 
   2684  1.1  christos 	AR_WRITE_BARRIER(sc);
   2685  1.1  christos }
   2686  1.1  christos 
   2687  1.1  christos Static void
   2688  1.1  christos ar5008_enable_ofdm_weak_signal(struct athn_softc *sc)
   2689  1.1  christos {
   2690  1.1  christos 	uint32_t reg;
   2691  1.1  christos 
   2692  1.1  christos 	reg = AR_READ(sc, AR_PHY_SFCORR_LOW);
   2693  1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_LOW_M1_THRESH_LOW, 50);
   2694  1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_LOW_M2_THRESH_LOW, 40);
   2695  1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_LOW_M2COUNT_THR_LOW, 48);
   2696  1.1  christos 	AR_WRITE(sc, AR_PHY_SFCORR_LOW, reg);
   2697  1.1  christos 
   2698  1.1  christos 	reg = AR_READ(sc, AR_PHY_SFCORR);
   2699  1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_M1_THRESH, 77);
   2700  1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_M2_THRESH, 64);
   2701  1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_M2COUNT_THR, 16);
   2702  1.1  christos 	AR_WRITE(sc, AR_PHY_SFCORR, reg);
   2703  1.1  christos 
   2704  1.1  christos 	reg = AR_READ(sc, AR_PHY_SFCORR_EXT);
   2705  1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_EXT_M1_THRESH_LOW, 50);
   2706  1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_EXT_M2_THRESH_LOW, 40);
   2707  1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_EXT_M1_THRESH, 77);
   2708  1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_EXT_M2_THRESH, 64);
   2709  1.1  christos 	AR_WRITE(sc, AR_PHY_SFCORR_EXT, reg);
   2710  1.1  christos 
   2711  1.1  christos 	AR_SETBITS(sc, AR_PHY_SFCORR_LOW,
   2712  1.1  christos 	    AR_PHY_SFCORR_LOW_USE_SELF_CORR_LOW);
   2713  1.1  christos 	AR_WRITE_BARRIER(sc);
   2714  1.1  christos }
   2715  1.1  christos 
   2716  1.1  christos Static void
   2717  1.1  christos ar5008_disable_ofdm_weak_signal(struct athn_softc *sc)
   2718  1.1  christos {
   2719  1.1  christos 	uint32_t reg;
   2720  1.1  christos 
   2721  1.1  christos 	reg = AR_READ(sc, AR_PHY_SFCORR_LOW);
   2722  1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_LOW_M1_THRESH_LOW, 127);
   2723  1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_LOW_M2_THRESH_LOW, 127);
   2724  1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_LOW_M2COUNT_THR_LOW, 63);
   2725  1.1  christos 	AR_WRITE(sc, AR_PHY_SFCORR_LOW, reg);
   2726  1.1  christos 
   2727  1.1  christos 	reg = AR_READ(sc, AR_PHY_SFCORR);
   2728  1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_M1_THRESH, 127);
   2729  1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_M2_THRESH, 127);
   2730  1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_M2COUNT_THR, 31);
   2731  1.1  christos 	AR_WRITE(sc, AR_PHY_SFCORR, reg);
   2732  1.1  christos 
   2733  1.1  christos 	reg = AR_READ(sc, AR_PHY_SFCORR_EXT);
   2734  1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_EXT_M1_THRESH_LOW, 127);
   2735  1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_EXT_M2_THRESH_LOW, 127);
   2736  1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_EXT_M1_THRESH, 127);
   2737  1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_EXT_M2_THRESH, 127);
   2738  1.1  christos 	AR_WRITE(sc, AR_PHY_SFCORR_EXT, reg);
   2739  1.1  christos 
   2740  1.1  christos 	AR_CLRBITS(sc, AR_PHY_SFCORR_LOW,
   2741  1.1  christos 	    AR_PHY_SFCORR_LOW_USE_SELF_CORR_LOW);
   2742  1.1  christos 	AR_WRITE_BARRIER(sc);
   2743  1.1  christos }
   2744  1.1  christos 
   2745  1.1  christos Static void
   2746  1.1  christos ar5008_set_cck_weak_signal(struct athn_softc *sc, int high)
   2747  1.1  christos {
   2748  1.1  christos 	uint32_t reg;
   2749  1.1  christos 
   2750  1.1  christos 	reg = AR_READ(sc, AR_PHY_CCK_DETECT);
   2751  1.1  christos 	reg = RW(reg, AR_PHY_CCK_DETECT_WEAK_SIG_THR_CCK, high ? 6 : 8);
   2752  1.1  christos 	AR_WRITE(sc, AR_PHY_CCK_DETECT, reg);
   2753  1.1  christos 	AR_WRITE_BARRIER(sc);
   2754  1.1  christos }
   2755  1.1  christos 
   2756  1.1  christos Static void
   2757  1.1  christos ar5008_set_firstep_level(struct athn_softc *sc, int level)
   2758  1.1  christos {
   2759  1.1  christos 	uint32_t reg;
   2760  1.1  christos 
   2761  1.1  christos 	reg = AR_READ(sc, AR_PHY_FIND_SIG);
   2762  1.1  christos 	reg = RW(reg, AR_PHY_FIND_SIG_FIRSTEP, level * 4);
   2763  1.1  christos 	AR_WRITE(sc, AR_PHY_FIND_SIG, reg);
   2764  1.1  christos 	AR_WRITE_BARRIER(sc);
   2765  1.1  christos }
   2766  1.1  christos 
   2767  1.1  christos Static void
   2768  1.1  christos ar5008_set_spur_immunity_level(struct athn_softc *sc, int level)
   2769  1.1  christos {
   2770  1.1  christos 	uint32_t reg;
   2771  1.1  christos 
   2772  1.1  christos 	reg = AR_READ(sc, AR_PHY_TIMING5);
   2773  1.1  christos 	reg = RW(reg, AR_PHY_TIMING5_CYCPWR_THR1, (level + 1) * 2);
   2774  1.1  christos 	AR_WRITE(sc, AR_PHY_TIMING5, reg);
   2775  1.1  christos 	AR_WRITE_BARRIER(sc);
   2776  1.1  christos }
   2777