Home | History | Annotate | Line # | Download | only in ic
arn5008.c revision 1.6.8.2
      1  1.6.8.2     skrll /*	$NetBSD: arn5008.c,v 1.6.8.2 2016/03/19 11:30:09 skrll 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.6.8.2     skrll __KERNEL_RCSID(0, "$NetBSD: arn5008.c,v 1.6.8.2 2016/03/19 11:30:09 skrll 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.6.8.1     skrll 	error = bus_dmamap_load(sc->sc_dmat, sc->sc_map, sc->sc_descs,
    522  1.6.8.1     skrll 	    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.6.8.1     skrll 	error = bus_dmamap_load(sc->sc_dmat, rxq->map, rxq->descs,
    610  1.6.8.1     skrll 	    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.6.8.1     skrll 			AR_WRITE(sc, AR_INTR_SYNC_ENABLE, 0);
   1261  1.6.8.1     skrll 			(void)AR_READ(sc, AR_INTR_SYNC_ENABLE);
   1262      1.3    martin 			pmf_event_inject(sc->sc_dev, PMFE_RADIO_OFF);
   1263      1.1  christos 		}
   1264      1.1  christos 
   1265      1.1  christos 		AR_WRITE(sc, AR_INTR_SYNC_CAUSE, sync);
   1266      1.1  christos 		(void)AR_READ(sc, AR_INTR_SYNC_CAUSE);
   1267      1.1  christos 	}
   1268      1.1  christos 	return 1;
   1269      1.1  christos }
   1270      1.1  christos 
   1271      1.1  christos Static int
   1272      1.1  christos ar5008_tx(struct athn_softc *sc, struct mbuf *m, struct ieee80211_node *ni,
   1273      1.1  christos     int txflags)
   1274      1.1  christos {
   1275      1.1  christos 	struct ieee80211com *ic = &sc->sc_ic;
   1276      1.1  christos 	struct ieee80211_key *k = NULL;
   1277      1.1  christos 	struct ieee80211_frame *wh;
   1278      1.1  christos 	struct athn_series series[4];
   1279      1.1  christos 	struct ar_tx_desc *ds, *lastds;
   1280      1.1  christos 	struct athn_txq *txq;
   1281      1.1  christos 	struct athn_tx_buf *bf;
   1282      1.1  christos 	struct athn_node *an = (void *)ni;
   1283      1.1  christos 	struct mbuf *m1;
   1284      1.1  christos 	uint16_t qos;
   1285      1.1  christos 	uint8_t txpower, type, encrtype, ridx[4];
   1286      1.1  christos 	int i, error, totlen, hasqos, qid;
   1287      1.1  christos 
   1288      1.1  christos 	/* Grab a Tx buffer from our global free list. */
   1289      1.1  christos 	bf = SIMPLEQ_FIRST(&sc->sc_txbufs);
   1290      1.1  christos 	KASSERT(bf != NULL);
   1291      1.1  christos 
   1292      1.1  christos 	/* Map 802.11 frame type to hardware frame type. */
   1293      1.1  christos 	wh = mtod(m, struct ieee80211_frame *);
   1294      1.1  christos 	if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
   1295      1.1  christos 	    IEEE80211_FC0_TYPE_MGT) {
   1296      1.1  christos 		/* NB: Beacons do not use ar5008_tx(). */
   1297      1.1  christos 		if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
   1298      1.1  christos 		    IEEE80211_FC0_SUBTYPE_PROBE_RESP)
   1299      1.1  christos 			type = AR_FRAME_TYPE_PROBE_RESP;
   1300      1.1  christos 		else if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
   1301      1.1  christos 		    IEEE80211_FC0_SUBTYPE_ATIM)
   1302      1.1  christos 			type = AR_FRAME_TYPE_ATIM;
   1303      1.1  christos 		else
   1304      1.1  christos 			type = AR_FRAME_TYPE_NORMAL;
   1305      1.1  christos 	}
   1306      1.1  christos 	else if ((wh->i_fc[0] &
   1307      1.1  christos 	    (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
   1308      1.1  christos 	    (IEEE80211_FC0_TYPE_CTL  | IEEE80211_FC0_SUBTYPE_PS_POLL)) {
   1309      1.1  christos 		type = AR_FRAME_TYPE_PSPOLL;
   1310      1.1  christos 	}
   1311      1.1  christos 	else
   1312      1.1  christos 		type = AR_FRAME_TYPE_NORMAL;
   1313      1.1  christos 
   1314      1.1  christos 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
   1315      1.1  christos 		k = ieee80211_crypto_encap(ic, ni, m);
   1316      1.1  christos 		if (k == NULL)
   1317      1.1  christos 			return ENOBUFS;
   1318      1.1  christos 
   1319      1.1  christos 		/* packet header may have moved, reset our local pointer */
   1320      1.1  christos 		wh = mtod(m, struct ieee80211_frame *);
   1321      1.1  christos 	}
   1322      1.1  christos 
   1323      1.1  christos 	/* XXX 2-byte padding for QoS and 4-addr headers. */
   1324      1.1  christos 
   1325      1.1  christos 	/* Select the HW Tx queue to use for this frame. */
   1326      1.1  christos 	if ((hasqos = ieee80211_has_qos(wh))) {
   1327      1.1  christos #ifdef notyet_edca
   1328      1.1  christos 		uint8_t tid;
   1329      1.1  christos 
   1330      1.1  christos 		qos = ieee80211_get_qos(wh);
   1331      1.1  christos 		tid = qos & IEEE80211_QOS_TID;
   1332      1.1  christos 		qid = athn_ac2qid[ieee80211_up_to_ac(ic, tid)];
   1333      1.1  christos #else
   1334      1.1  christos 		qos = ieee80211_get_qos(wh);
   1335      1.1  christos 		qid = ATHN_QID_AC_BE;
   1336      1.1  christos #endif /* notyet_edca */
   1337      1.1  christos 	}
   1338      1.1  christos 	else if (type == AR_FRAME_TYPE_PSPOLL) {
   1339      1.1  christos 		qos = 0;
   1340      1.1  christos 		qid = ATHN_QID_PSPOLL;
   1341      1.1  christos 	}
   1342      1.1  christos 	else if (txflags & ATHN_TXFLAG_CAB) {
   1343      1.1  christos 		qos = 0;
   1344      1.1  christos 		qid = ATHN_QID_CAB;
   1345      1.1  christos 	}
   1346      1.1  christos 	else {
   1347      1.1  christos 		qos = 0;
   1348      1.1  christos 		qid = ATHN_QID_AC_BE;
   1349      1.1  christos 	}
   1350      1.1  christos 	txq = &sc->sc_txq[qid];
   1351      1.1  christos 
   1352      1.1  christos 	/* Select the transmit rates to use for this frame. */
   1353      1.1  christos 	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
   1354      1.1  christos 	    (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) !=
   1355      1.1  christos 	    IEEE80211_FC0_TYPE_DATA) {
   1356      1.1  christos 		/* Use lowest rate for all tries. */
   1357      1.1  christos 		ridx[0] = ridx[1] = ridx[2] = ridx[3] =
   1358      1.1  christos 		    (ic->ic_curmode == IEEE80211_MODE_11A) ?
   1359      1.1  christos 			ATHN_RIDX_OFDM6 : ATHN_RIDX_CCK1;
   1360      1.1  christos 	}
   1361      1.1  christos 	else if (ic->ic_fixed_rate != -1) {
   1362      1.1  christos 		/* Use same fixed rate for all tries. */
   1363      1.1  christos 		ridx[0] = ridx[1] = ridx[2] = ridx[3] =
   1364      1.1  christos 		    sc->sc_fixed_ridx;
   1365      1.1  christos 	}
   1366      1.1  christos 	else {
   1367      1.1  christos 		int txrate = ni->ni_txrate;
   1368      1.1  christos 		/* Use fallback table of the node. */
   1369      1.1  christos 		for (i = 0; i < 4; i++) {
   1370      1.1  christos 			ridx[i] = an->ridx[txrate];
   1371      1.1  christos 			txrate = an->fallback[txrate];
   1372      1.1  christos 		}
   1373      1.1  christos 	}
   1374      1.1  christos 
   1375      1.1  christos 	if (__predict_false(sc->sc_drvbpf != NULL)) {
   1376      1.1  christos 		struct athn_tx_radiotap_header *tap = &sc->sc_txtap;
   1377      1.1  christos 
   1378      1.1  christos 		tap->wt_flags = 0;
   1379      1.1  christos 		/* Use initial transmit rate. */
   1380      1.1  christos 		tap->wt_rate = athn_rates[ridx[0]].rate;
   1381      1.1  christos 		tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
   1382      1.1  christos 		tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
   1383      1.1  christos // XXX		tap->wt_hwqueue = qid;
   1384      1.1  christos 		if (ridx[0] != ATHN_RIDX_CCK1 &&
   1385      1.1  christos 		    (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
   1386      1.1  christos 			tap->wt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
   1387      1.1  christos 
   1388      1.1  christos 		bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m);
   1389      1.1  christos 	}
   1390      1.1  christos 
   1391      1.1  christos 	/* DMA map mbuf. */
   1392      1.1  christos 	error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_map, m,
   1393      1.1  christos 	    BUS_DMA_NOWAIT | BUS_DMA_WRITE);
   1394      1.1  christos 	if (__predict_false(error != 0)) {
   1395      1.1  christos 		if (error != EFBIG) {
   1396      1.6  christos 			aprint_error_dev(sc->sc_dev,
   1397      1.6  christos 			    "can't map mbuf (error %d)\n", error);
   1398      1.1  christos 			m_freem(m);
   1399      1.1  christos 			return error;
   1400      1.1  christos 		}
   1401      1.1  christos 		/*
   1402      1.1  christos 		 * DMA mapping requires too many DMA segments; linearize
   1403      1.1  christos 		 * mbuf in kernel virtual address space and retry.
   1404      1.1  christos 		 */
   1405      1.1  christos 		MGETHDR(m1, M_DONTWAIT, MT_DATA);
   1406      1.1  christos 		if (m1 == NULL) {
   1407      1.1  christos 			m_freem(m);
   1408      1.1  christos 			return ENOBUFS;
   1409      1.1  christos 		}
   1410      1.1  christos 		if (m->m_pkthdr.len > (int)MHLEN) {
   1411      1.1  christos 			MCLGET(m1, M_DONTWAIT);
   1412      1.1  christos 			if (!(m1->m_flags & M_EXT)) {
   1413      1.1  christos 				m_freem(m);
   1414      1.1  christos 				m_freem(m1);
   1415      1.1  christos 				return ENOBUFS;
   1416      1.1  christos 			}
   1417      1.1  christos 		}
   1418      1.1  christos 		m_copydata(m, 0, m->m_pkthdr.len, mtod(m1, void *));
   1419      1.1  christos 		m1->m_pkthdr.len = m1->m_len = m->m_pkthdr.len;
   1420      1.1  christos 		m_freem(m);
   1421      1.1  christos 		m = m1;
   1422      1.1  christos 
   1423      1.1  christos 		error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_map, m,
   1424      1.1  christos 		    BUS_DMA_NOWAIT | BUS_DMA_WRITE);
   1425      1.1  christos 		if (error != 0) {
   1426      1.6  christos 			aprint_error_dev(sc->sc_dev,
   1427      1.6  christos 			    "can't map mbuf (error %d)\n", error);
   1428      1.1  christos 			m_freem(m);
   1429      1.1  christos 			return error;
   1430      1.1  christos 		}
   1431      1.1  christos 	}
   1432      1.1  christos 	bf->bf_m = m;
   1433      1.1  christos 	bf->bf_ni = ni;
   1434      1.1  christos 	bf->bf_txflags = txflags;
   1435      1.1  christos 
   1436      1.1  christos 	wh = mtod(m, struct ieee80211_frame *);
   1437      1.1  christos 
   1438      1.1  christos 	totlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
   1439      1.1  christos 
   1440      1.1  christos 	/* Clear all Tx descriptors that we will use. */
   1441      1.1  christos 	memset(bf->bf_descs, 0, bf->bf_map->dm_nsegs * sizeof(*ds));
   1442      1.1  christos 
   1443      1.1  christos 	/* Setup first Tx descriptor. */
   1444      1.1  christos 	ds = bf->bf_descs;
   1445      1.1  christos 
   1446      1.1  christos 	ds->ds_ctl0 = AR_TXC0_INTR_REQ | AR_TXC0_CLR_DEST_MASK;
   1447      1.1  christos 	txpower = AR_MAX_RATE_POWER;	/* Get from per-rate registers. */
   1448      1.1  christos 	ds->ds_ctl0 |= SM(AR_TXC0_XMIT_POWER, txpower);
   1449      1.1  christos 
   1450      1.1  christos 	ds->ds_ctl1 = SM(AR_TXC1_FRAME_TYPE, type);
   1451      1.1  christos 
   1452      1.1  christos 	if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
   1453      1.1  christos 	    (hasqos && (qos & IEEE80211_QOS_ACKPOLICY_MASK) ==
   1454      1.1  christos 	     IEEE80211_QOS_ACKPOLICY_NOACK))
   1455      1.1  christos 		ds->ds_ctl1 |= AR_TXC1_NO_ACK;
   1456      1.1  christos #if notyet
   1457      1.1  christos 	if (0 && k != NULL) {
   1458      1.1  christos 		uintptr_t entry;
   1459      1.1  christos 
   1460      1.1  christos 		/*
   1461      1.1  christos 		 * Map 802.11 cipher to hardware encryption type and
   1462      1.1  christos 		 * compute MIC+ICV overhead.
   1463      1.1  christos 		 */
   1464      1.1  christos 		totlen += k->wk_keylen;
   1465      1.1  christos 		switch (k->wk_cipher->ic_cipher) {
   1466      1.1  christos 		case IEEE80211_CIPHER_WEP:
   1467      1.1  christos 			encrtype = AR_ENCR_TYPE_WEP;
   1468      1.1  christos 			break;
   1469      1.1  christos 		case IEEE80211_CIPHER_TKIP:
   1470      1.1  christos 			encrtype = AR_ENCR_TYPE_TKIP;
   1471      1.1  christos 			break;
   1472      1.1  christos 		case IEEE80211_CIPHER_AES_OCB:
   1473      1.1  christos 		case IEEE80211_CIPHER_AES_CCM:
   1474      1.1  christos 			encrtype = AR_ENCR_TYPE_AES;
   1475      1.1  christos 			break;
   1476      1.1  christos 		default:
   1477      1.1  christos 			panic("unsupported cipher");
   1478      1.1  christos 		}
   1479      1.1  christos 		/*
   1480      1.1  christos 		 * NB: The key cache entry index is stored in the key
   1481      1.1  christos 		 * private field when the key is installed.
   1482      1.1  christos 		 */
   1483      1.1  christos 		entry = (uintptr_t)k->k_priv;
   1484      1.1  christos 		ds->ds_ctl1 |= SM(AR_TXC1_DEST_IDX, entry);
   1485      1.1  christos 		ds->ds_ctl0 |= AR_TXC0_DEST_IDX_VALID;
   1486      1.1  christos 	}
   1487      1.1  christos 	else
   1488      1.1  christos #endif
   1489      1.1  christos 		encrtype = AR_ENCR_TYPE_CLEAR;
   1490      1.1  christos 	ds->ds_ctl6 = SM(AR_TXC6_ENCR_TYPE, encrtype);
   1491      1.1  christos 
   1492      1.1  christos 	/* Check if frame must be protected using RTS/CTS or CTS-to-self. */
   1493      1.1  christos 	if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
   1494      1.1  christos 		/* NB: Group frames are sent using CCK in 802.11b/g. */
   1495      1.1  christos 		if (totlen > ic->ic_rtsthreshold) {
   1496      1.1  christos 			ds->ds_ctl0 |= AR_TXC0_RTS_ENABLE;
   1497      1.1  christos 		}
   1498      1.1  christos 		else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
   1499      1.1  christos 		    athn_rates[ridx[0]].phy == IEEE80211_T_OFDM) {
   1500      1.1  christos 			if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
   1501      1.1  christos 				ds->ds_ctl0 |= AR_TXC0_RTS_ENABLE;
   1502      1.1  christos 			else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
   1503      1.1  christos 				ds->ds_ctl0 |= AR_TXC0_CTS_ENABLE;
   1504      1.1  christos 		}
   1505      1.1  christos 	}
   1506      1.1  christos 	if (ds->ds_ctl0 & (AR_TXC0_RTS_ENABLE | AR_TXC0_CTS_ENABLE)) {
   1507      1.1  christos 		/* Disable multi-rate retries when protection is used. */
   1508      1.1  christos 		ridx[1] = ridx[2] = ridx[3] = ridx[0];
   1509      1.1  christos 	}
   1510      1.1  christos 	/* Setup multi-rate retries. */
   1511      1.1  christos 	for (i = 0; i < 4; i++) {
   1512      1.1  christos 		series[i].hwrate = athn_rates[ridx[i]].hwrate;
   1513      1.1  christos 		if (athn_rates[ridx[i]].phy == IEEE80211_T_DS &&
   1514      1.1  christos 		    ridx[i] != ATHN_RIDX_CCK1 &&
   1515      1.1  christos 		    (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
   1516      1.1  christos 			series[i].hwrate |= 0x04;
   1517      1.1  christos 		series[i].dur = 0;
   1518      1.1  christos 	}
   1519      1.1  christos 	if (!(ds->ds_ctl1 & AR_TXC1_NO_ACK)) {
   1520      1.1  christos 		/* Compute duration for each series. */
   1521      1.1  christos 		for (i = 0; i < 4; i++) {
   1522      1.1  christos 			series[i].dur = athn_txtime(sc, IEEE80211_ACK_LEN,
   1523      1.1  christos 			    athn_rates[ridx[i]].rspridx, ic->ic_flags);
   1524      1.1  christos 		}
   1525      1.1  christos 	}
   1526      1.1  christos 
   1527      1.1  christos 	/* Write number of tries for each series. */
   1528      1.1  christos 	ds->ds_ctl2 =
   1529      1.1  christos 	    SM(AR_TXC2_XMIT_DATA_TRIES0, 2) |
   1530      1.1  christos 	    SM(AR_TXC2_XMIT_DATA_TRIES1, 2) |
   1531      1.1  christos 	    SM(AR_TXC2_XMIT_DATA_TRIES2, 2) |
   1532      1.1  christos 	    SM(AR_TXC2_XMIT_DATA_TRIES3, 4);
   1533      1.1  christos 
   1534      1.1  christos 	/* Tell HW to update duration field in 802.11 header. */
   1535      1.1  christos 	if (type != AR_FRAME_TYPE_PSPOLL)
   1536      1.1  christos 		ds->ds_ctl2 |= AR_TXC2_DUR_UPDATE_ENA;
   1537      1.1  christos 
   1538      1.1  christos 	/* Write Tx rate for each series. */
   1539      1.1  christos 	ds->ds_ctl3 =
   1540      1.1  christos 	    SM(AR_TXC3_XMIT_RATE0, series[0].hwrate) |
   1541      1.1  christos 	    SM(AR_TXC3_XMIT_RATE1, series[1].hwrate) |
   1542      1.1  christos 	    SM(AR_TXC3_XMIT_RATE2, series[2].hwrate) |
   1543      1.1  christos 	    SM(AR_TXC3_XMIT_RATE3, series[3].hwrate);
   1544      1.1  christos 
   1545      1.1  christos 	/* Write duration for each series. */
   1546      1.1  christos 	ds->ds_ctl4 =
   1547      1.1  christos 	    SM(AR_TXC4_PACKET_DUR0, series[0].dur) |
   1548      1.1  christos 	    SM(AR_TXC4_PACKET_DUR1, series[1].dur);
   1549      1.1  christos 	ds->ds_ctl5 =
   1550      1.1  christos 	    SM(AR_TXC5_PACKET_DUR2, series[2].dur) |
   1551      1.1  christos 	    SM(AR_TXC5_PACKET_DUR3, series[3].dur);
   1552      1.1  christos 
   1553      1.1  christos 	/* Use the same Tx chains for all tries. */
   1554      1.1  christos 	ds->ds_ctl7 =
   1555      1.1  christos 	    SM(AR_TXC7_CHAIN_SEL0, sc->sc_txchainmask) |
   1556      1.1  christos 	    SM(AR_TXC7_CHAIN_SEL1, sc->sc_txchainmask) |
   1557      1.1  christos 	    SM(AR_TXC7_CHAIN_SEL2, sc->sc_txchainmask) |
   1558      1.1  christos 	    SM(AR_TXC7_CHAIN_SEL3, sc->sc_txchainmask);
   1559      1.1  christos #ifdef notyet
   1560      1.1  christos #ifndef IEEE80211_NO_HT
   1561      1.1  christos 	/* Use the same short GI setting for all tries. */
   1562      1.1  christos 	if (ic->ic_flags & IEEE80211_F_SHGI)
   1563      1.1  christos 		ds->ds_ctl7 |= AR_TXC7_GI0123;
   1564      1.1  christos 	/* Use the same channel width for all tries. */
   1565      1.1  christos 	if (ic->ic_flags & IEEE80211_F_CBW40)
   1566      1.1  christos 		ds->ds_ctl7 |= AR_TXC7_2040_0123;
   1567      1.1  christos #endif
   1568      1.1  christos #endif
   1569      1.1  christos 
   1570      1.1  christos 	if (ds->ds_ctl0 & (AR_TXC0_RTS_ENABLE | AR_TXC0_CTS_ENABLE)) {
   1571      1.1  christos 		uint8_t protridx, hwrate;
   1572      1.1  christos 		uint16_t dur = 0;
   1573      1.1  christos 
   1574      1.1  christos 		/* Use the same protection mode for all tries. */
   1575      1.1  christos 		if (ds->ds_ctl0 & AR_TXC0_RTS_ENABLE) {
   1576      1.1  christos 			ds->ds_ctl4 |= AR_TXC4_RTSCTS_QUAL01;
   1577      1.1  christos 			ds->ds_ctl5 |= AR_TXC5_RTSCTS_QUAL23;
   1578      1.1  christos 		}
   1579      1.1  christos 		/* Select protection rate (suboptimal but ok). */
   1580      1.1  christos 		protridx = (ic->ic_curmode == IEEE80211_MODE_11A) ?
   1581      1.1  christos 		    ATHN_RIDX_OFDM6 : ATHN_RIDX_CCK2;
   1582      1.1  christos 		if (ds->ds_ctl0 & AR_TXC0_RTS_ENABLE) {
   1583      1.1  christos 			/* Account for CTS duration. */
   1584      1.1  christos 			dur += athn_txtime(sc, IEEE80211_ACK_LEN,
   1585      1.1  christos 			    athn_rates[protridx].rspridx, ic->ic_flags);
   1586      1.1  christos 		}
   1587      1.1  christos 		dur += athn_txtime(sc, totlen, ridx[0], ic->ic_flags);
   1588      1.1  christos 		if (!(ds->ds_ctl1 & AR_TXC1_NO_ACK)) {
   1589      1.1  christos 			/* Account for ACK duration. */
   1590      1.1  christos 			dur += athn_txtime(sc, IEEE80211_ACK_LEN,
   1591      1.1  christos 			    athn_rates[ridx[0]].rspridx, ic->ic_flags);
   1592      1.1  christos 		}
   1593      1.1  christos 		/* Write protection frame duration and rate. */
   1594      1.1  christos 		ds->ds_ctl2 |= SM(AR_TXC2_BURST_DUR, dur);
   1595      1.1  christos 		hwrate = athn_rates[protridx].hwrate;
   1596      1.1  christos 		if (protridx == ATHN_RIDX_CCK2 &&
   1597      1.1  christos 		    (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
   1598      1.1  christos 			hwrate |= 0x04;
   1599      1.1  christos 		ds->ds_ctl7 |= SM(AR_TXC7_RTSCTS_RATE, hwrate);
   1600      1.1  christos 	}
   1601      1.1  christos 
   1602      1.1  christos 	/* Finalize first Tx descriptor and fill others (if any). */
   1603      1.1  christos 	ds->ds_ctl0 |= SM(AR_TXC0_FRAME_LEN, totlen);
   1604      1.1  christos 
   1605      1.1  christos 	lastds = NULL;	/* XXX: gcc */
   1606      1.1  christos 	for (i = 0; i < bf->bf_map->dm_nsegs; i++, ds++) {
   1607      1.1  christos 		ds->ds_data = bf->bf_map->dm_segs[i].ds_addr;
   1608      1.1  christos 		ds->ds_ctl1 |= SM(AR_TXC1_BUF_LEN,
   1609      1.1  christos 		    bf->bf_map->dm_segs[i].ds_len);
   1610      1.1  christos 
   1611      1.1  christos 		if (i != bf->bf_map->dm_nsegs - 1)
   1612      1.1  christos 			ds->ds_ctl1 |= AR_TXC1_MORE;
   1613      1.1  christos 		ds->ds_link = 0;
   1614      1.1  christos 
   1615      1.1  christos 		/* Chain Tx descriptor. */
   1616      1.1  christos 		if (i != 0)
   1617      1.1  christos 			lastds->ds_link = bf->bf_daddr + i * sizeof(*ds);
   1618      1.1  christos 		lastds = ds;
   1619      1.1  christos 	}
   1620      1.1  christos 	bus_dmamap_sync(sc->sc_dmat, bf->bf_map, 0, bf->bf_map->dm_mapsize,
   1621      1.1  christos 	    BUS_DMASYNC_PREWRITE);
   1622      1.1  christos 
   1623      1.1  christos 	if (!SIMPLEQ_EMPTY(&txq->head))
   1624      1.1  christos 		((struct ar_tx_desc *)txq->lastds)->ds_link = bf->bf_daddr;
   1625      1.1  christos 	else
   1626      1.1  christos 		AR_WRITE(sc, AR_QTXDP(qid), bf->bf_daddr);
   1627      1.1  christos 	txq->lastds = lastds;
   1628      1.1  christos 	SIMPLEQ_REMOVE_HEAD(&sc->sc_txbufs, bf_list);
   1629      1.1  christos 	SIMPLEQ_INSERT_TAIL(&txq->head, bf, bf_list);
   1630      1.1  christos 
   1631      1.1  christos 	ds = bf->bf_descs;
   1632      1.1  christos 	DPRINTFN(DBG_TX, sc,
   1633      1.1  christos 	    "Tx qid=%d nsegs=%d ctl0=0x%x ctl1=0x%x ctl3=0x%x\n",
   1634      1.1  christos 	    qid, bf->bf_map->dm_nsegs, ds->ds_ctl0, ds->ds_ctl1, ds->ds_ctl3);
   1635      1.1  christos 
   1636      1.1  christos 	/* Kick Tx. */
   1637      1.1  christos 	AR_WRITE(sc, AR_Q_TXE, 1 << qid);
   1638      1.1  christos 	AR_WRITE_BARRIER(sc);
   1639      1.1  christos 	return 0;
   1640      1.1  christos }
   1641      1.1  christos 
   1642      1.1  christos Static void
   1643      1.1  christos ar5008_set_rf_mode(struct athn_softc *sc, struct ieee80211_channel *c)
   1644      1.1  christos {
   1645      1.1  christos 	uint32_t reg;
   1646      1.1  christos 
   1647      1.1  christos 	reg = IEEE80211_IS_CHAN_2GHZ(c) ?
   1648      1.1  christos 	    AR_PHY_MODE_DYNAMIC : AR_PHY_MODE_OFDM;
   1649      1.1  christos 	if (!AR_SREV_9280_10_OR_LATER(sc)) {
   1650      1.1  christos 		reg |= IEEE80211_IS_CHAN_2GHZ(c) ?
   1651      1.1  christos 		    AR_PHY_MODE_RF2GHZ : AR_PHY_MODE_RF5GHZ;
   1652      1.1  christos 	}
   1653      1.1  christos 	else if (IEEE80211_IS_CHAN_5GHZ(c) &&
   1654      1.1  christos 	    (sc->sc_flags & ATHN_FLAG_FAST_PLL_CLOCK)) {
   1655      1.1  christos 		reg |= AR_PHY_MODE_DYNAMIC | AR_PHY_MODE_DYN_CCK_DISABLE;
   1656      1.1  christos 	}
   1657      1.1  christos 	AR_WRITE(sc, AR_PHY_MODE, reg);
   1658      1.1  christos 	AR_WRITE_BARRIER(sc);
   1659      1.1  christos }
   1660      1.1  christos 
   1661      1.1  christos static __inline uint32_t
   1662      1.1  christos ar5008_synth_delay(struct athn_softc *sc)
   1663      1.1  christos {
   1664      1.5     skrll 	uint32_t synth_delay;
   1665      1.1  christos 
   1666      1.5     skrll 	synth_delay = MS(AR_READ(sc, AR_PHY_RX_DELAY), AR_PHY_RX_DELAY_DELAY);
   1667      1.1  christos 	if (sc->sc_ic.ic_curmode == IEEE80211_MODE_11B)
   1668      1.5     skrll 		synth_delay = (synth_delay * 4) / 22;
   1669      1.1  christos 	else
   1670      1.5     skrll 		synth_delay = synth_delay / 10;	/* in 100ns steps */
   1671      1.5     skrll 	return synth_delay;
   1672      1.1  christos }
   1673      1.1  christos 
   1674      1.1  christos Static int
   1675      1.1  christos ar5008_rf_bus_request(struct athn_softc *sc)
   1676      1.1  christos {
   1677      1.1  christos 	int ntries;
   1678      1.1  christos 
   1679      1.1  christos 	/* Request RF Bus grant. */
   1680      1.1  christos 	AR_WRITE(sc, AR_PHY_RFBUS_REQ, AR_PHY_RFBUS_REQ_EN);
   1681      1.1  christos 	for (ntries = 0; ntries < 10000; ntries++) {
   1682      1.1  christos 		if (AR_READ(sc, AR_PHY_RFBUS_GRANT) & AR_PHY_RFBUS_GRANT_EN)
   1683      1.1  christos 			return 0;
   1684      1.1  christos 		DELAY(10);
   1685      1.1  christos 	}
   1686      1.1  christos 	DPRINTFN(DBG_RF, sc, "could not kill baseband Rx");
   1687      1.1  christos 	return ETIMEDOUT;
   1688      1.1  christos }
   1689      1.1  christos 
   1690      1.1  christos Static void
   1691      1.1  christos ar5008_rf_bus_release(struct athn_softc *sc)
   1692      1.1  christos {
   1693      1.1  christos 
   1694      1.1  christos 	/* Wait for the synthesizer to settle. */
   1695      1.1  christos 	DELAY(AR_BASE_PHY_ACTIVE_DELAY + ar5008_synth_delay(sc));
   1696      1.1  christos 
   1697      1.1  christos 	/* Release the RF Bus grant. */
   1698      1.1  christos 	AR_WRITE(sc, AR_PHY_RFBUS_REQ, 0);
   1699      1.1  christos 	AR_WRITE_BARRIER(sc);
   1700      1.1  christos }
   1701      1.1  christos 
   1702      1.1  christos Static void
   1703      1.1  christos ar5008_set_phy(struct athn_softc *sc, struct ieee80211_channel *c,
   1704      1.1  christos     struct ieee80211_channel *extc)
   1705      1.1  christos {
   1706      1.1  christos 	uint32_t phy;
   1707      1.1  christos 
   1708      1.1  christos 	if (AR_SREV_9285_10_OR_LATER(sc))
   1709      1.1  christos 		phy = AR_READ(sc, AR_PHY_TURBO) & AR_PHY_FC_ENABLE_DAC_FIFO;
   1710      1.1  christos 	else
   1711      1.1  christos 		phy = 0;
   1712      1.1  christos 	phy |= AR_PHY_FC_HT_EN | AR_PHY_FC_SHORT_GI_40 |
   1713      1.1  christos 	    AR_PHY_FC_SINGLE_HT_LTF1 | AR_PHY_FC_WALSH;
   1714      1.1  christos #ifndef IEEE80211_NO_HT
   1715      1.1  christos 	if (extc != NULL) {
   1716      1.1  christos 		phy |= AR_PHY_FC_DYN2040_EN;
   1717      1.1  christos 		if (extc > c)	/* XXX */
   1718      1.1  christos 			phy |= AR_PHY_FC_DYN2040_PRI_CH;
   1719      1.1  christos 	}
   1720      1.1  christos #endif
   1721      1.1  christos 	AR_WRITE(sc, AR_PHY_TURBO, phy);
   1722      1.1  christos 
   1723      1.1  christos 	AR_WRITE(sc, AR_2040_MODE,
   1724      1.1  christos 	    (extc != NULL) ? AR_2040_JOINED_RX_CLEAR : 0);
   1725      1.1  christos 
   1726      1.1  christos 	/* Set global transmit timeout. */
   1727      1.1  christos 	AR_WRITE(sc, AR_GTXTO, SM(AR_GTXTO_TIMEOUT_LIMIT, 25));
   1728      1.1  christos 	/* Set carrier sense timeout. */
   1729      1.1  christos 	AR_WRITE(sc, AR_CST, SM(AR_CST_TIMEOUT_LIMIT, 15));
   1730      1.1  christos 	AR_WRITE_BARRIER(sc);
   1731      1.1  christos }
   1732      1.1  christos 
   1733      1.1  christos Static void
   1734      1.1  christos ar5008_set_delta_slope(struct athn_softc *sc, struct ieee80211_channel *c,
   1735      1.1  christos     struct ieee80211_channel *extc)
   1736      1.1  christos {
   1737      1.1  christos 	uint32_t coeff, exp, man, reg;
   1738      1.1  christos 
   1739      1.1  christos 	/* Set Delta Slope (exponent and mantissa). */
   1740      1.1  christos 	coeff = (100 << 24) / c->ic_freq;
   1741      1.1  christos 	athn_get_delta_slope(coeff, &exp, &man);
   1742      1.1  christos 	DPRINTFN(DBG_RX, sc, "delta slope coeff exp=%u man=%u\n", exp, man);
   1743      1.1  christos 
   1744      1.1  christos 	reg = AR_READ(sc, AR_PHY_TIMING3);
   1745      1.1  christos 	reg = RW(reg, AR_PHY_TIMING3_DSC_EXP, exp);
   1746      1.1  christos 	reg = RW(reg, AR_PHY_TIMING3_DSC_MAN, man);
   1747      1.1  christos 	AR_WRITE(sc, AR_PHY_TIMING3, reg);
   1748      1.1  christos 
   1749      1.1  christos 	/* For Short GI, coeff is 9/10 that of normal coeff. */
   1750      1.1  christos 	coeff = (9 * coeff) / 10;
   1751      1.1  christos 	athn_get_delta_slope(coeff, &exp, &man);
   1752      1.1  christos 	DPRINTFN(DBG_RX, sc, "delta slope coeff exp=%u man=%u\n", exp, man);
   1753      1.1  christos 
   1754      1.1  christos 	reg = AR_READ(sc, AR_PHY_HALFGI);
   1755      1.1  christos 	reg = RW(reg, AR_PHY_HALFGI_DSC_EXP, exp);
   1756      1.1  christos 	reg = RW(reg, AR_PHY_HALFGI_DSC_MAN, man);
   1757      1.1  christos 	AR_WRITE(sc, AR_PHY_HALFGI, reg);
   1758      1.1  christos 	AR_WRITE_BARRIER(sc);
   1759      1.1  christos }
   1760      1.1  christos 
   1761      1.1  christos Static void
   1762      1.1  christos ar5008_enable_antenna_diversity(struct athn_softc *sc)
   1763      1.1  christos {
   1764      1.1  christos 
   1765      1.1  christos 	AR_SETBITS(sc, AR_PHY_CCK_DETECT,
   1766      1.1  christos 	    AR_PHY_CCK_DETECT_BB_ENABLE_ANT_FAST_DIV);
   1767      1.1  christos 	AR_WRITE_BARRIER(sc);
   1768      1.1  christos }
   1769      1.1  christos 
   1770      1.1  christos Static void
   1771      1.1  christos ar5008_init_baseband(struct athn_softc *sc)
   1772      1.1  christos {
   1773      1.1  christos 	uint32_t synth_delay;
   1774      1.1  christos 
   1775      1.1  christos 	synth_delay = ar5008_synth_delay(sc);
   1776      1.1  christos 	/* Activate the PHY (includes baseband activate and synthesizer on). */
   1777      1.1  christos 	AR_WRITE(sc, AR_PHY_ACTIVE, AR_PHY_ACTIVE_EN);
   1778      1.1  christos 	AR_WRITE_BARRIER(sc);
   1779      1.1  christos 	DELAY(AR_BASE_PHY_ACTIVE_DELAY + synth_delay);
   1780      1.1  christos }
   1781      1.1  christos 
   1782      1.1  christos Static void
   1783      1.1  christos ar5008_disable_phy(struct athn_softc *sc)
   1784      1.1  christos {
   1785      1.1  christos 
   1786      1.1  christos 	AR_WRITE(sc, AR_PHY_ACTIVE, AR_PHY_ACTIVE_DIS);
   1787      1.1  christos 	AR_WRITE_BARRIER(sc);
   1788      1.1  christos }
   1789      1.1  christos 
   1790      1.1  christos Static void
   1791      1.1  christos ar5008_init_chains(struct athn_softc *sc)
   1792      1.1  christos {
   1793      1.1  christos 
   1794      1.1  christos 	if (sc->sc_rxchainmask == 0x5 || sc->sc_txchainmask == 0x5)
   1795      1.1  christos 		AR_SETBITS(sc, AR_PHY_ANALOG_SWAP, AR_PHY_SWAP_ALT_CHAIN);
   1796      1.1  christos 
   1797      1.1  christos 	/* Setup chain masks. */
   1798      1.1  christos 	if (sc->sc_mac_ver <= AR_SREV_VERSION_9160 &&
   1799      1.1  christos 	    (sc->sc_rxchainmask == 0x3 || sc->sc_rxchainmask == 0x5)) {
   1800      1.1  christos 		AR_WRITE(sc, AR_PHY_RX_CHAINMASK,  0x7);
   1801      1.1  christos 		AR_WRITE(sc, AR_PHY_CAL_CHAINMASK, 0x7);
   1802      1.1  christos 	}
   1803      1.1  christos 	else {
   1804      1.1  christos 		AR_WRITE(sc, AR_PHY_RX_CHAINMASK,  sc->sc_rxchainmask);
   1805      1.1  christos 		AR_WRITE(sc, AR_PHY_CAL_CHAINMASK, sc->sc_rxchainmask);
   1806      1.1  christos 	}
   1807      1.1  christos 	AR_WRITE(sc, AR_SELFGEN_MASK, sc->sc_txchainmask);
   1808      1.1  christos 	AR_WRITE_BARRIER(sc);
   1809      1.1  christos }
   1810      1.1  christos 
   1811      1.1  christos Static void
   1812      1.1  christos ar5008_set_rxchains(struct athn_softc *sc)
   1813      1.1  christos {
   1814      1.1  christos 
   1815      1.1  christos 	if (sc->sc_rxchainmask == 0x3 || sc->sc_rxchainmask == 0x5) {
   1816      1.1  christos 		AR_WRITE(sc, AR_PHY_RX_CHAINMASK,  sc->sc_rxchainmask);
   1817      1.1  christos 		AR_WRITE(sc, AR_PHY_CAL_CHAINMASK, sc->sc_rxchainmask);
   1818      1.1  christos 		AR_WRITE_BARRIER(sc);
   1819      1.1  christos 	}
   1820      1.1  christos }
   1821      1.1  christos 
   1822      1.1  christos #ifdef notused
   1823      1.1  christos Static void
   1824      1.1  christos ar5008_read_noisefloor(struct athn_softc *sc, int16_t *nf, int16_t *nf_ext)
   1825      1.1  christos {
   1826      1.1  christos /* Sign-extends 9-bit value (assumes upper bits are zeroes). */
   1827      1.1  christos #define SIGN_EXT(v)	(((v) ^ 0x100) - 0x100)
   1828      1.1  christos 	uint32_t reg;
   1829      1.1  christos 	int i;
   1830      1.1  christos 
   1831      1.1  christos 	for (i = 0; i < sc->sc_nrxchains; i++) {
   1832      1.1  christos 		reg = AR_READ(sc, AR_PHY_CCA(i));
   1833      1.1  christos 		if (AR_SREV_9280_10_OR_LATER(sc))
   1834      1.1  christos 			nf[i] = MS(reg, AR9280_PHY_MINCCA_PWR);
   1835      1.1  christos 		else
   1836      1.1  christos 			nf[i] = MS(reg, AR_PHY_MINCCA_PWR);
   1837      1.1  christos 		nf[i] = SIGN_EXT(nf[i]);
   1838      1.1  christos 
   1839      1.1  christos 		reg = AR_READ(sc, AR_PHY_EXT_CCA(i));
   1840      1.1  christos 		if (AR_SREV_9280_10_OR_LATER(sc))
   1841      1.1  christos 			nf_ext[i] = MS(reg, AR9280_PHY_EXT_MINCCA_PWR);
   1842      1.1  christos 		else
   1843      1.1  christos 			nf_ext[i] = MS(reg, AR_PHY_EXT_MINCCA_PWR);
   1844      1.1  christos 		nf_ext[i] = SIGN_EXT(nf_ext[i]);
   1845      1.1  christos 	}
   1846      1.1  christos #undef SIGN_EXT
   1847      1.1  christos }
   1848      1.1  christos #endif /* notused */
   1849      1.1  christos 
   1850      1.1  christos #ifdef notused
   1851      1.1  christos Static void
   1852      1.1  christos ar5008_write_noisefloor(struct athn_softc *sc, int16_t *nf, int16_t *nf_ext)
   1853      1.1  christos {
   1854      1.1  christos 	uint32_t reg;
   1855      1.1  christos 	int i;
   1856      1.1  christos 
   1857      1.1  christos 	for (i = 0; i < sc->sc_nrxchains; i++) {
   1858      1.1  christos 		reg = AR_READ(sc, AR_PHY_CCA(i));
   1859      1.1  christos 		reg = RW(reg, AR_PHY_MAXCCA_PWR, nf[i]);
   1860      1.1  christos 		AR_WRITE(sc, AR_PHY_CCA(i), reg);
   1861      1.1  christos 
   1862      1.1  christos 		reg = AR_READ(sc, AR_PHY_EXT_CCA(i));
   1863      1.1  christos 		reg = RW(reg, AR_PHY_EXT_MAXCCA_PWR, nf_ext[i]);
   1864      1.1  christos 		AR_WRITE(sc, AR_PHY_EXT_CCA(i), reg);
   1865      1.1  christos 	}
   1866      1.1  christos 	AR_WRITE_BARRIER(sc);
   1867      1.1  christos }
   1868      1.1  christos #endif /* notused */
   1869      1.1  christos 
   1870      1.1  christos #ifdef notused
   1871      1.1  christos Static void
   1872      1.1  christos ar5008_get_noisefloor(struct athn_softc *sc, struct ieee80211_channel *c)
   1873      1.1  christos {
   1874      1.1  christos 	int16_t nf[AR_MAX_CHAINS], nf_ext[AR_MAX_CHAINS];
   1875      1.1  christos 	int i;
   1876      1.1  christos 
   1877      1.1  christos 	if (AR_READ(sc, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF) {
   1878      1.1  christos 		/* Noisefloor calibration not finished. */
   1879      1.1  christos 		return;
   1880      1.1  christos 	}
   1881      1.1  christos 	/* Noisefloor calibration is finished. */
   1882      1.1  christos 	ar5008_read_noisefloor(sc, nf, nf_ext);
   1883      1.1  christos 
   1884      1.1  christos 	/* Update noisefloor history. */
   1885      1.1  christos 	for (i = 0; i < sc->sc_nrxchains; i++) {
   1886      1.1  christos 		sc->sc_nf_hist[sc->sc_nf_hist_cur].nf[i] = nf[i];
   1887      1.1  christos 		sc->sc_nf_hist[sc->sc_nf_hist_cur].nf_ext[i] = nf_ext[i];
   1888      1.1  christos 	}
   1889      1.1  christos 	if (++sc->sc_nf_hist_cur >= ATHN_NF_CAL_HIST_MAX)
   1890      1.1  christos 		sc->sc_nf_hist_cur = 0;
   1891      1.1  christos }
   1892      1.1  christos #endif /* notused */
   1893      1.1  christos 
   1894      1.1  christos #ifdef notused
   1895      1.1  christos Static void
   1896      1.1  christos ar5008_bb_load_noisefloor(struct athn_softc *sc)
   1897      1.1  christos {
   1898      1.1  christos 	int16_t nf[AR_MAX_CHAINS], nf_ext[AR_MAX_CHAINS];
   1899      1.1  christos 	int i, ntries;
   1900      1.1  christos 
   1901      1.1  christos 	/* Write filtered noisefloor values. */
   1902      1.1  christos 	for (i = 0; i < sc->sc_nrxchains; i++) {
   1903      1.1  christos 		nf[i] = sc->sc_nf_priv[i] * 2;
   1904      1.1  christos 		nf_ext[i] = sc->sc_nf_ext_priv[i] * 2;
   1905      1.1  christos 	}
   1906      1.1  christos 	ar5008_write_noisefloor(sc, nf, nf_ext);
   1907      1.1  christos 
   1908      1.1  christos 	/* Load filtered noisefloor values into baseband. */
   1909      1.1  christos 	AR_CLRBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_ENABLE_NF);
   1910      1.1  christos 	AR_CLRBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
   1911      1.1  christos 	AR_SETBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
   1912      1.1  christos 	/* Wait for load to complete. */
   1913      1.1  christos 	for (ntries = 0; ntries < 1000; ntries++) {
   1914      1.1  christos 		if (!(AR_READ(sc, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF))
   1915      1.1  christos 			break;
   1916      1.1  christos 		DELAY(50);
   1917      1.1  christos 	}
   1918      1.1  christos 	if (ntries == 1000) {
   1919      1.1  christos 		DPRINTFN(DBG_RF, sc, "failed to load noisefloor values\n");
   1920      1.1  christos 		return;
   1921      1.1  christos 	}
   1922      1.1  christos 
   1923      1.1  christos 	/* Restore noisefloor values to initial (max) values. */
   1924      1.1  christos 	for (i = 0; i < AR_MAX_CHAINS; i++)
   1925      1.1  christos 		nf[i] = nf_ext[i] = -50 * 2;
   1926      1.1  christos 	ar5008_write_noisefloor(sc, nf, nf_ext);
   1927      1.1  christos }
   1928      1.1  christos #endif /* notused */
   1929      1.1  christos 
   1930      1.1  christos #ifdef notused
   1931      1.1  christos Static void
   1932      1.1  christos ar5008_noisefloor_calib(struct athn_softc *sc)
   1933      1.1  christos {
   1934      1.1  christos 
   1935      1.1  christos 	AR_SETBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_ENABLE_NF);
   1936      1.1  christos 	AR_SETBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
   1937      1.1  christos 	AR_SETBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
   1938      1.1  christos 	AR_WRITE_BARRIER(sc);
   1939      1.1  christos }
   1940      1.1  christos #endif /* notused */
   1941      1.1  christos 
   1942      1.1  christos Static void
   1943      1.1  christos ar5008_do_noisefloor_calib(struct athn_softc *sc)
   1944      1.1  christos {
   1945      1.1  christos 
   1946      1.1  christos 	AR_SETBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
   1947      1.1  christos 	AR_WRITE_BARRIER(sc);
   1948      1.1  christos }
   1949      1.1  christos 
   1950      1.1  christos Static void
   1951      1.1  christos ar5008_do_calib(struct athn_softc *sc)
   1952      1.1  christos {
   1953      1.1  christos 	uint32_t mode, reg;
   1954      1.1  christos 	int log;
   1955      1.1  christos 
   1956      1.1  christos 	reg = AR_READ(sc, AR_PHY_TIMING_CTRL4_0);
   1957      1.1  christos 	log = AR_SREV_9280_10_OR_LATER(sc) ? 10 : 2;
   1958      1.1  christos 	reg = RW(reg, AR_PHY_TIMING_CTRL4_IQCAL_LOG_COUNT_MAX, log);
   1959      1.1  christos 	AR_WRITE(sc, AR_PHY_TIMING_CTRL4_0, reg);
   1960      1.1  christos 
   1961      1.1  christos 	if (sc->sc_cur_calib_mask & ATHN_CAL_ADC_GAIN)
   1962      1.1  christos 		mode = AR_PHY_CALMODE_ADC_GAIN;
   1963      1.1  christos 	else if (sc->sc_cur_calib_mask & ATHN_CAL_ADC_DC)
   1964      1.1  christos 		mode = AR_PHY_CALMODE_ADC_DC_PER;
   1965      1.1  christos 	else	/* ATHN_CAL_IQ */
   1966      1.1  christos 		mode = AR_PHY_CALMODE_IQ;
   1967      1.1  christos 	AR_WRITE(sc, AR_PHY_CALMODE, mode);
   1968      1.1  christos 
   1969      1.1  christos 	DPRINTFN(DBG_RF, sc, "starting calibration mode=0x%x\n", mode);
   1970      1.1  christos 	AR_SETBITS(sc, AR_PHY_TIMING_CTRL4_0, AR_PHY_TIMING_CTRL4_DO_CAL);
   1971      1.1  christos 	AR_WRITE_BARRIER(sc);
   1972      1.1  christos }
   1973      1.1  christos 
   1974      1.1  christos Static void
   1975      1.1  christos ar5008_next_calib(struct athn_softc *sc)
   1976      1.1  christos {
   1977      1.1  christos 
   1978      1.1  christos 	/* Check if we have any calibration in progress. */
   1979      1.1  christos 	if (sc->sc_cur_calib_mask != 0) {
   1980      1.1  christos 		if (!(AR_READ(sc, AR_PHY_TIMING_CTRL4_0) &
   1981      1.1  christos 		    AR_PHY_TIMING_CTRL4_DO_CAL)) {
   1982      1.1  christos 			/* Calibration completed for current sample. */
   1983      1.1  christos 			if (sc->sc_cur_calib_mask & ATHN_CAL_ADC_GAIN)
   1984      1.1  christos 				ar5008_calib_adc_gain(sc);
   1985      1.1  christos 			else if (sc->sc_cur_calib_mask & ATHN_CAL_ADC_DC)
   1986      1.1  christos 				ar5008_calib_adc_dc_off(sc);
   1987      1.1  christos 			else	/* ATHN_CAL_IQ */
   1988      1.1  christos 				ar5008_calib_iq(sc);
   1989      1.1  christos 		}
   1990      1.1  christos 	}
   1991      1.1  christos }
   1992      1.1  christos 
   1993      1.1  christos Static void
   1994      1.1  christos ar5008_calib_iq(struct athn_softc *sc)
   1995      1.1  christos {
   1996      1.1  christos 	struct athn_iq_cal *cal;
   1997      1.1  christos 	uint32_t reg, i_coff_denom, q_coff_denom;
   1998      1.1  christos 	int32_t i_coff, q_coff;
   1999      1.1  christos 	int i, iq_corr_neg;
   2000      1.1  christos 
   2001      1.1  christos 	for (i = 0; i < AR_MAX_CHAINS; i++) {
   2002      1.1  christos 		cal = &sc->sc_calib.iq[i];
   2003      1.1  christos 
   2004      1.1  christos 		/* Accumulate IQ calibration measures (clear on read). */
   2005      1.1  christos 		cal->pwr_meas_i += AR_READ(sc, AR_PHY_CAL_MEAS_0(i));
   2006      1.1  christos 		cal->pwr_meas_q += AR_READ(sc, AR_PHY_CAL_MEAS_1(i));
   2007      1.1  christos 		cal->iq_corr_meas +=
   2008      1.1  christos 		    (int32_t)AR_READ(sc, AR_PHY_CAL_MEAS_2(i));
   2009      1.1  christos 	}
   2010      1.1  christos 	if (!AR_SREV_9280_10_OR_LATER(sc) &&
   2011      1.1  christos 	    ++sc->sc_calib.nsamples < AR_CAL_SAMPLES) {
   2012      1.1  christos 		/* Not enough samples accumulated, continue. */
   2013      1.1  christos 		ar5008_do_calib(sc);
   2014      1.1  christos 		return;
   2015      1.1  christos 	}
   2016      1.1  christos 
   2017      1.1  christos 	for (i = 0; i < sc->sc_nrxchains; i++) {
   2018      1.1  christos 		cal = &sc->sc_calib.iq[i];
   2019      1.1  christos 
   2020      1.1  christos 		if (cal->pwr_meas_q == 0)
   2021      1.1  christos 			continue;
   2022      1.1  christos 
   2023  1.6.8.2     skrll 		if ((iq_corr_neg = cal->iq_corr_meas) < 0)
   2024      1.1  christos 			cal->iq_corr_meas = -cal->iq_corr_meas;
   2025      1.1  christos 
   2026      1.1  christos 		i_coff_denom =
   2027      1.1  christos 		    (cal->pwr_meas_i / 2 + cal->pwr_meas_q / 2) / 128;
   2028      1.1  christos 		q_coff_denom = cal->pwr_meas_q / 64;
   2029      1.1  christos 
   2030      1.1  christos 		if (i_coff_denom == 0 || q_coff_denom == 0)
   2031      1.1  christos 			continue;	/* Prevents division by zero. */
   2032      1.1  christos 
   2033      1.1  christos 		i_coff = cal->iq_corr_meas / i_coff_denom;
   2034      1.1  christos 		q_coff = (cal->pwr_meas_i / q_coff_denom) - 64;
   2035      1.1  christos 
   2036      1.1  christos 		/* Negate i_coff if iq_corr_meas is positive. */
   2037      1.1  christos 		if (!iq_corr_neg)
   2038      1.1  christos 			i_coff = 0x40 - (i_coff & 0x3f);
   2039      1.1  christos 		if (q_coff > 15)
   2040      1.1  christos 			q_coff = 15;
   2041      1.1  christos 		else if (q_coff <= -16)
   2042      1.1  christos 			q_coff = -16;	/* XXX Linux has a bug here? */
   2043      1.1  christos 
   2044      1.1  christos 		DPRINTFN(DBG_RF, sc, "IQ calibration for chain %d\n", i);
   2045      1.1  christos 		reg = AR_READ(sc, AR_PHY_TIMING_CTRL4(i));
   2046      1.1  christos 		reg = RW(reg, AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF, i_coff);
   2047      1.1  christos 		reg = RW(reg, AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF, q_coff);
   2048      1.1  christos 		AR_WRITE(sc, AR_PHY_TIMING_CTRL4(i), reg);
   2049      1.1  christos 	}
   2050      1.1  christos 
   2051      1.1  christos 	/* Apply new settings. */
   2052      1.1  christos 	AR_SETBITS(sc, AR_PHY_TIMING_CTRL4_0,
   2053      1.1  christos 	    AR_PHY_TIMING_CTRL4_IQCORR_ENABLE);
   2054      1.1  christos 	AR_WRITE_BARRIER(sc);
   2055      1.1  christos 
   2056      1.1  christos 	/* IQ calibration done. */
   2057      1.1  christos 	sc->sc_cur_calib_mask &= ~ATHN_CAL_IQ;
   2058      1.1  christos 	memset(&sc->sc_calib, 0, sizeof(sc->sc_calib));
   2059      1.1  christos }
   2060      1.1  christos 
   2061      1.1  christos Static void
   2062      1.1  christos ar5008_calib_adc_gain(struct athn_softc *sc)
   2063      1.1  christos {
   2064      1.1  christos 	struct athn_adc_cal *cal;
   2065      1.1  christos 	uint32_t reg, gain_mismatch_i, gain_mismatch_q;
   2066      1.1  christos 	int i;
   2067      1.1  christos 
   2068      1.1  christos 	for (i = 0; i < AR_MAX_CHAINS; i++) {
   2069      1.1  christos 		cal = &sc->sc_calib.adc_gain[i];
   2070      1.1  christos 
   2071      1.1  christos 		/* Accumulate ADC gain measures (clear on read). */
   2072      1.1  christos 		cal->pwr_meas_odd_i  += AR_READ(sc, AR_PHY_CAL_MEAS_0(i));
   2073      1.1  christos 		cal->pwr_meas_even_i += AR_READ(sc, AR_PHY_CAL_MEAS_1(i));
   2074      1.1  christos 		cal->pwr_meas_odd_q  += AR_READ(sc, AR_PHY_CAL_MEAS_2(i));
   2075      1.1  christos 		cal->pwr_meas_even_q += AR_READ(sc, AR_PHY_CAL_MEAS_3(i));
   2076      1.1  christos 	}
   2077      1.1  christos 	if (!AR_SREV_9280_10_OR_LATER(sc) &&
   2078      1.1  christos 	    ++sc->sc_calib.nsamples < AR_CAL_SAMPLES) {
   2079      1.1  christos 		/* Not enough samples accumulated, continue. */
   2080      1.1  christos 		ar5008_do_calib(sc);
   2081      1.1  christos 		return;
   2082      1.1  christos 	}
   2083      1.1  christos 
   2084      1.1  christos 	for (i = 0; i < sc->sc_nrxchains; i++) {
   2085      1.1  christos 		cal = &sc->sc_calib.adc_gain[i];
   2086      1.1  christos 
   2087      1.1  christos 		if (cal->pwr_meas_odd_i == 0 || cal->pwr_meas_even_q == 0)
   2088      1.1  christos 			continue;	/* Prevents division by zero. */
   2089      1.1  christos 
   2090      1.1  christos 		gain_mismatch_i =
   2091      1.1  christos 		    (cal->pwr_meas_even_i * 32) / cal->pwr_meas_odd_i;
   2092      1.1  christos 		gain_mismatch_q =
   2093      1.1  christos 		    (cal->pwr_meas_odd_q * 32) / cal->pwr_meas_even_q;
   2094      1.1  christos 
   2095      1.1  christos 		DPRINTFN(DBG_RF, sc, "ADC gain calibration for chain %d\n", i);
   2096      1.1  christos 		reg = AR_READ(sc, AR_PHY_NEW_ADC_DC_GAIN_CORR(i));
   2097      1.1  christos 		reg = RW(reg, AR_PHY_NEW_ADC_DC_GAIN_IGAIN, gain_mismatch_i);
   2098      1.1  christos 		reg = RW(reg, AR_PHY_NEW_ADC_DC_GAIN_QGAIN, gain_mismatch_q);
   2099      1.1  christos 		AR_WRITE(sc, AR_PHY_NEW_ADC_DC_GAIN_CORR(i), reg);
   2100      1.1  christos 	}
   2101      1.1  christos 
   2102      1.1  christos 	/* Apply new settings. */
   2103      1.1  christos 	AR_SETBITS(sc, AR_PHY_NEW_ADC_DC_GAIN_CORR(0),
   2104      1.1  christos 	    AR_PHY_NEW_ADC_GAIN_CORR_ENABLE);
   2105      1.1  christos 	AR_WRITE_BARRIER(sc);
   2106      1.1  christos 
   2107      1.1  christos 	/* ADC gain calibration done. */
   2108      1.1  christos 	sc->sc_cur_calib_mask &= ~ATHN_CAL_ADC_GAIN;
   2109      1.1  christos 	memset(&sc->sc_calib, 0, sizeof(sc->sc_calib));
   2110      1.1  christos }
   2111      1.1  christos 
   2112      1.1  christos Static void
   2113      1.1  christos ar5008_calib_adc_dc_off(struct athn_softc *sc)
   2114      1.1  christos {
   2115      1.1  christos 	struct athn_adc_cal *cal;
   2116      1.1  christos 	int32_t dc_offset_mismatch_i, dc_offset_mismatch_q;
   2117      1.1  christos 	uint32_t reg;
   2118      1.1  christos 	int count, i;
   2119      1.1  christos 
   2120      1.1  christos 	for (i = 0; i < AR_MAX_CHAINS; i++) {
   2121      1.1  christos 		cal = &sc->sc_calib.adc_dc_offset[i];
   2122      1.1  christos 
   2123      1.1  christos 		/* Accumulate ADC DC offset measures (clear on read). */
   2124      1.1  christos 		cal->pwr_meas_odd_i  += AR_READ(sc, AR_PHY_CAL_MEAS_0(i));
   2125      1.1  christos 		cal->pwr_meas_even_i += AR_READ(sc, AR_PHY_CAL_MEAS_1(i));
   2126      1.1  christos 		cal->pwr_meas_odd_q  += AR_READ(sc, AR_PHY_CAL_MEAS_2(i));
   2127      1.1  christos 		cal->pwr_meas_even_q += AR_READ(sc, AR_PHY_CAL_MEAS_3(i));
   2128      1.1  christos 	}
   2129      1.1  christos 	if (!AR_SREV_9280_10_OR_LATER(sc) &&
   2130      1.1  christos 	    ++sc->sc_calib.nsamples < AR_CAL_SAMPLES) {
   2131      1.1  christos 		/* Not enough samples accumulated, continue. */
   2132      1.1  christos 		ar5008_do_calib(sc);
   2133      1.1  christos 		return;
   2134      1.1  christos 	}
   2135      1.1  christos 
   2136      1.1  christos 	if (AR_SREV_9280_10_OR_LATER(sc))
   2137      1.1  christos 		count = (1 << (10 + 5));
   2138      1.1  christos 	else
   2139      1.1  christos 		count = (1 << ( 2 + 5)) * AR_CAL_SAMPLES;
   2140      1.1  christos 	for (i = 0; i < sc->sc_nrxchains; i++) {
   2141      1.1  christos 		cal = &sc->sc_calib.adc_dc_offset[i];
   2142      1.1  christos 
   2143      1.1  christos 		dc_offset_mismatch_i =
   2144      1.1  christos 		    (cal->pwr_meas_even_i - cal->pwr_meas_odd_i * 2) / count;
   2145      1.1  christos 		dc_offset_mismatch_q =
   2146      1.1  christos 		    (cal->pwr_meas_odd_q - cal->pwr_meas_even_q * 2) / count;
   2147      1.1  christos 
   2148      1.1  christos 		DPRINTFN(DBG_RF, sc, "ADC DC offset calibration for chain %d\n", i);
   2149      1.1  christos 		reg = AR_READ(sc, AR_PHY_NEW_ADC_DC_GAIN_CORR(i));
   2150      1.1  christos 		reg = RW(reg, AR_PHY_NEW_ADC_DC_GAIN_QDC,
   2151      1.1  christos 		    dc_offset_mismatch_q);
   2152      1.1  christos 		reg = RW(reg, AR_PHY_NEW_ADC_DC_GAIN_IDC,
   2153      1.1  christos 		    dc_offset_mismatch_i);
   2154      1.1  christos 		AR_WRITE(sc, AR_PHY_NEW_ADC_DC_GAIN_CORR(i), reg);
   2155      1.1  christos 	}
   2156      1.1  christos 
   2157      1.1  christos 	/* Apply new settings. */
   2158      1.1  christos 	AR_SETBITS(sc, AR_PHY_NEW_ADC_DC_GAIN_CORR(0),
   2159      1.1  christos 	    AR_PHY_NEW_ADC_DC_OFFSET_CORR_ENABLE);
   2160      1.1  christos 	AR_WRITE_BARRIER(sc);
   2161      1.1  christos 
   2162      1.1  christos 	/* ADC DC offset calibration done. */
   2163      1.1  christos 	sc->sc_cur_calib_mask &= ~ATHN_CAL_ADC_DC;
   2164      1.1  christos 	memset(&sc->sc_calib, 0, sizeof(sc->sc_calib));
   2165      1.1  christos }
   2166      1.1  christos 
   2167      1.1  christos PUBLIC void
   2168      1.1  christos ar5008_write_txpower(struct athn_softc *sc, int16_t power[ATHN_POWER_COUNT])
   2169      1.1  christos {
   2170      1.1  christos 
   2171      1.1  christos 	AR_WRITE(sc, AR_PHY_POWER_TX_RATE1,
   2172      1.1  christos 	    (power[ATHN_POWER_OFDM18  ] & 0x3f) << 24 |
   2173      1.1  christos 	    (power[ATHN_POWER_OFDM12  ] & 0x3f) << 16 |
   2174      1.1  christos 	    (power[ATHN_POWER_OFDM9   ] & 0x3f) <<  8 |
   2175      1.1  christos 	    (power[ATHN_POWER_OFDM6   ] & 0x3f));
   2176      1.1  christos 	AR_WRITE(sc, AR_PHY_POWER_TX_RATE2,
   2177      1.1  christos 	    (power[ATHN_POWER_OFDM54  ] & 0x3f) << 24 |
   2178      1.1  christos 	    (power[ATHN_POWER_OFDM48  ] & 0x3f) << 16 |
   2179      1.1  christos 	    (power[ATHN_POWER_OFDM36  ] & 0x3f) <<  8 |
   2180      1.1  christos 	    (power[ATHN_POWER_OFDM24  ] & 0x3f));
   2181      1.1  christos 	AR_WRITE(sc, AR_PHY_POWER_TX_RATE3,
   2182      1.1  christos 	    (power[ATHN_POWER_CCK2_SP ] & 0x3f) << 24 |
   2183      1.1  christos 	    (power[ATHN_POWER_CCK2_LP ] & 0x3f) << 16 |
   2184      1.1  christos 	    (power[ATHN_POWER_XR      ] & 0x3f) <<  8 |
   2185      1.1  christos 	    (power[ATHN_POWER_CCK1_LP ] & 0x3f));
   2186      1.1  christos 	AR_WRITE(sc, AR_PHY_POWER_TX_RATE4,
   2187      1.1  christos 	    (power[ATHN_POWER_CCK11_SP] & 0x3f) << 24 |
   2188      1.1  christos 	    (power[ATHN_POWER_CCK11_LP] & 0x3f) << 16 |
   2189      1.1  christos 	    (power[ATHN_POWER_CCK55_SP] & 0x3f) <<  8 |
   2190      1.1  christos 	    (power[ATHN_POWER_CCK55_LP] & 0x3f));
   2191      1.1  christos #ifndef IEEE80211_NO_HT
   2192      1.1  christos 	AR_WRITE(sc, AR_PHY_POWER_TX_RATE5,
   2193      1.1  christos 	    (power[ATHN_POWER_HT20(3) ] & 0x3f) << 24 |
   2194      1.1  christos 	    (power[ATHN_POWER_HT20(2) ] & 0x3f) << 16 |
   2195      1.1  christos 	    (power[ATHN_POWER_HT20(1) ] & 0x3f) <<  8 |
   2196      1.1  christos 	    (power[ATHN_POWER_HT20(0) ] & 0x3f));
   2197      1.1  christos 	AR_WRITE(sc, AR_PHY_POWER_TX_RATE6,
   2198      1.1  christos 	    (power[ATHN_POWER_HT20(7) ] & 0x3f) << 24 |
   2199      1.1  christos 	    (power[ATHN_POWER_HT20(6) ] & 0x3f) << 16 |
   2200      1.1  christos 	    (power[ATHN_POWER_HT20(5) ] & 0x3f) <<  8 |
   2201      1.1  christos 	    (power[ATHN_POWER_HT20(4) ] & 0x3f));
   2202      1.1  christos 	AR_WRITE(sc, AR_PHY_POWER_TX_RATE7,
   2203      1.1  christos 	    (power[ATHN_POWER_HT40(3) ] & 0x3f) << 24 |
   2204      1.1  christos 	    (power[ATHN_POWER_HT40(2) ] & 0x3f) << 16 |
   2205      1.1  christos 	    (power[ATHN_POWER_HT40(1) ] & 0x3f) <<  8 |
   2206      1.1  christos 	    (power[ATHN_POWER_HT40(0) ] & 0x3f));
   2207      1.1  christos 	AR_WRITE(sc, AR_PHY_POWER_TX_RATE8,
   2208      1.1  christos 	    (power[ATHN_POWER_HT40(7) ] & 0x3f) << 24 |
   2209      1.1  christos 	    (power[ATHN_POWER_HT40(6) ] & 0x3f) << 16 |
   2210      1.1  christos 	    (power[ATHN_POWER_HT40(5) ] & 0x3f) <<  8 |
   2211      1.1  christos 	    (power[ATHN_POWER_HT40(4) ] & 0x3f));
   2212      1.1  christos 	AR_WRITE(sc, AR_PHY_POWER_TX_RATE9,
   2213      1.1  christos 	    (power[ATHN_POWER_OFDM_EXT] & 0x3f) << 24 |
   2214      1.1  christos 	    (power[ATHN_POWER_CCK_EXT ] & 0x3f) << 16 |
   2215      1.1  christos 	    (power[ATHN_POWER_OFDM_DUP] & 0x3f) <<  8 |
   2216      1.1  christos 	    (power[ATHN_POWER_CCK_DUP ] & 0x3f));
   2217      1.1  christos #endif
   2218      1.1  christos 	AR_WRITE_BARRIER(sc);
   2219      1.1  christos }
   2220      1.1  christos 
   2221      1.1  christos PUBLIC void
   2222      1.1  christos ar5008_set_viterbi_mask(struct athn_softc *sc, int bin)
   2223      1.1  christos {
   2224      1.1  christos 	uint32_t mask[4], reg;
   2225      1.1  christos 	uint8_t m[62], p[62];	/* XXX use bit arrays? */
   2226      1.1  christos 	int i, bit, cur;
   2227      1.1  christos 
   2228      1.1  christos 	/* Compute pilot mask. */
   2229      1.1  christos 	cur = -6000;
   2230      1.1  christos 	for (i = 0; i < 4; i++) {
   2231      1.1  christos 		mask[i] = 0;
   2232      1.1  christos 		for (bit = 0; bit < 30; bit++) {
   2233      1.1  christos 			if (abs(cur - bin) < 100)
   2234      1.1  christos 				mask[i] |= 1 << bit;
   2235      1.1  christos 			cur += 100;
   2236      1.1  christos 		}
   2237      1.1  christos 		if (cur == 0)	/* Skip entry "0". */
   2238      1.1  christos 			cur = 100;
   2239      1.1  christos 	}
   2240      1.1  christos 	/* Write entries from -6000 to -3100. */
   2241      1.1  christos 	AR_WRITE(sc, AR_PHY_TIMING7, mask[0]);
   2242      1.1  christos 	AR_WRITE(sc, AR_PHY_TIMING9, mask[0]);
   2243      1.1  christos 	/* Write entries from -3000 to -100. */
   2244      1.1  christos 	AR_WRITE(sc, AR_PHY_TIMING8, mask[1]);
   2245      1.1  christos 	AR_WRITE(sc, AR_PHY_TIMING10, mask[1]);
   2246      1.1  christos 	/* Write entries from 100 to 3000. */
   2247      1.1  christos 	AR_WRITE(sc, AR_PHY_PILOT_MASK_01_30, mask[2]);
   2248      1.1  christos 	AR_WRITE(sc, AR_PHY_CHANNEL_MASK_01_30, mask[2]);
   2249      1.1  christos 	/* Write entries from 3100 to 6000. */
   2250      1.1  christos 	AR_WRITE(sc, AR_PHY_PILOT_MASK_31_60, mask[3]);
   2251      1.1  christos 	AR_WRITE(sc, AR_PHY_CHANNEL_MASK_31_60, mask[3]);
   2252      1.1  christos 
   2253      1.1  christos 	/* Compute viterbi mask. */
   2254      1.1  christos 	for (cur = 6100; cur >= 0; cur -= 100)
   2255      1.1  christos 		p[+cur / 100] = abs(cur - bin) < 75;
   2256      1.1  christos 	for (cur = -100; cur >= -6100; cur -= 100)
   2257      1.1  christos 		m[-cur / 100] = abs(cur - bin) < 75;
   2258      1.1  christos 
   2259      1.1  christos 	/* Write viterbi mask (XXX needs to be reworked). */
   2260      1.1  christos 	reg =
   2261      1.1  christos 	    m[46] << 30 | m[47] << 28 | m[48] << 26 | m[49] << 24 |
   2262      1.1  christos 	    m[50] << 22 | m[51] << 20 | m[52] << 18 | m[53] << 16 |
   2263      1.1  christos 	    m[54] << 14 | m[55] << 12 | m[56] << 10 | m[57] <<  8 |
   2264      1.1  christos 	    m[58] <<  6 | m[59] <<  4 | m[60] <<  2 | m[61] <<  0;
   2265      1.1  christos 	AR_WRITE(sc, AR_PHY_BIN_MASK_1, reg);
   2266      1.1  christos 	AR_WRITE(sc, AR_PHY_VIT_MASK2_M_46_61, reg);
   2267      1.1  christos 
   2268      1.1  christos 	/* XXX m[48] should be m[38] ? */
   2269      1.1  christos 	reg =             m[31] << 28 | m[32] << 26 | m[33] << 24 |
   2270      1.1  christos 	    m[34] << 22 | m[35] << 20 | m[36] << 18 | m[37] << 16 |
   2271      1.1  christos 	    m[48] << 14 | m[39] << 12 | m[40] << 10 | m[41] <<  8 |
   2272      1.1  christos 	    m[42] <<  6 | m[43] <<  4 | m[44] <<  2 | m[45] <<  0;
   2273      1.1  christos 	AR_WRITE(sc, AR_PHY_BIN_MASK_2, reg);
   2274      1.1  christos 	AR_WRITE(sc, AR_PHY_VIT_MASK2_M_31_45, reg);
   2275      1.1  christos 
   2276      1.1  christos 	/* XXX This one is weird too. */
   2277      1.1  christos 	reg =
   2278      1.1  christos 	    m[16] << 30 | m[16] << 28 | m[18] << 26 | m[18] << 24 |
   2279      1.1  christos 	    m[20] << 22 | m[20] << 20 | m[22] << 18 | m[22] << 16 |
   2280      1.1  christos 	    m[24] << 14 | m[24] << 12 | m[25] << 10 | m[26] <<  8 |
   2281      1.1  christos 	    m[27] <<  6 | m[28] <<  4 | m[29] <<  2 | m[30] <<  0;
   2282      1.1  christos 	AR_WRITE(sc, AR_PHY_BIN_MASK_3, reg);
   2283      1.1  christos 	AR_WRITE(sc, AR_PHY_VIT_MASK2_M_16_30, reg);
   2284      1.1  christos 
   2285      1.1  christos 	reg =
   2286      1.1  christos 	    m[ 0] << 30 | m[ 1] << 28 | m[ 2] << 26 | m[ 3] << 24 |
   2287      1.1  christos 	    m[ 4] << 22 | m[ 5] << 20 | m[ 6] << 18 | m[ 7] << 16 |
   2288      1.1  christos 	    m[ 8] << 14 | m[ 9] << 12 | m[10] << 10 | m[11] <<  8 |
   2289      1.1  christos 	    m[12] <<  6 | m[13] <<  4 | m[14] <<  2 | m[15] <<  0;
   2290      1.1  christos 	AR_WRITE(sc, AR_PHY_MASK_CTL, reg);
   2291      1.1  christos 	AR_WRITE(sc, AR_PHY_VIT_MASK2_M_00_15, reg);
   2292      1.1  christos 
   2293      1.1  christos 	reg =             p[15] << 28 | p[14] << 26 | p[13] << 24 |
   2294      1.1  christos 	    p[12] << 22 | p[11] << 20 | p[10] << 18 | p[ 9] << 16 |
   2295      1.1  christos 	    p[ 8] << 14 | p[ 7] << 12 | p[ 6] << 10 | p[ 5] <<  8 |
   2296      1.1  christos 	    p[ 4] <<  6 | p[ 3] <<  4 | p[ 2] <<  2 | p[ 1] <<  0;
   2297      1.1  christos 	AR_WRITE(sc, AR_PHY_BIN_MASK2_1, reg);
   2298      1.1  christos 	AR_WRITE(sc, AR_PHY_VIT_MASK2_P_15_01, reg);
   2299      1.1  christos 
   2300      1.1  christos 	reg =             p[30] << 28 | p[29] << 26 | p[28] << 24 |
   2301      1.1  christos 	    p[27] << 22 | p[26] << 20 | p[25] << 18 | p[24] << 16 |
   2302      1.1  christos 	    p[23] << 14 | p[22] << 12 | p[21] << 10 | p[20] <<  8 |
   2303      1.1  christos 	    p[19] <<  6 | p[18] <<  4 | p[17] <<  2 | p[16] <<  0;
   2304      1.1  christos 	AR_WRITE(sc, AR_PHY_BIN_MASK2_2, reg);
   2305      1.1  christos 	AR_WRITE(sc, AR_PHY_VIT_MASK2_P_30_16, reg);
   2306      1.1  christos 
   2307      1.1  christos 	reg =             p[45] << 28 | p[44] << 26 | p[43] << 24 |
   2308      1.1  christos 	    p[42] << 22 | p[41] << 20 | p[40] << 18 | p[39] << 16 |
   2309      1.1  christos 	    p[38] << 14 | p[37] << 12 | p[36] << 10 | p[35] <<  8 |
   2310      1.1  christos 	    p[34] <<  6 | p[33] <<  4 | p[32] <<  2 | p[31] <<  0;
   2311      1.1  christos 	AR_WRITE(sc, AR_PHY_BIN_MASK2_3, reg);
   2312      1.1  christos 	AR_WRITE(sc, AR_PHY_VIT_MASK2_P_45_31, reg);
   2313      1.1  christos 
   2314      1.1  christos 	reg =
   2315      1.1  christos 	    p[61] << 30 | p[60] << 28 | p[59] << 26 | p[58] << 24 |
   2316      1.1  christos 	    p[57] << 22 | p[56] << 20 | p[55] << 18 | p[54] << 16 |
   2317      1.1  christos 	    p[53] << 14 | p[52] << 12 | p[51] << 10 | p[50] <<  8 |
   2318      1.1  christos 	    p[49] <<  6 | p[48] <<  4 | p[47] <<  2 | p[46] <<  0;
   2319      1.1  christos 	AR_WRITE(sc, AR_PHY_BIN_MASK2_4, reg);
   2320      1.1  christos 	AR_WRITE(sc, AR_PHY_VIT_MASK2_P_61_46, reg);
   2321      1.1  christos 	AR_WRITE_BARRIER(sc);
   2322      1.1  christos }
   2323      1.1  christos 
   2324      1.1  christos Static void
   2325      1.1  christos ar5008_hw_init(struct athn_softc *sc, struct ieee80211_channel *c,
   2326      1.1  christos     struct ieee80211_channel *extc)
   2327      1.1  christos {
   2328      1.1  christos 	struct athn_ops *ops = &sc->sc_ops;
   2329      1.1  christos 	const struct athn_ini *ini = sc->sc_ini;
   2330      1.1  christos 	const uint32_t *pvals;
   2331      1.1  christos 	uint32_t reg;
   2332      1.1  christos 	int i;
   2333      1.1  christos 
   2334      1.1  christos 	AR_WRITE(sc, AR_PHY(0), 0x00000007);
   2335      1.1  christos 	AR_WRITE(sc, AR_PHY_ADC_SERIAL_CTL, AR_PHY_SEL_EXTERNAL_RADIO);
   2336      1.1  christos 
   2337      1.1  christos 	if (!AR_SINGLE_CHIP(sc))
   2338      1.1  christos 		ar5416_reset_addac(sc, c);
   2339      1.1  christos 
   2340      1.1  christos 	AR_WRITE(sc, AR_PHY_ADC_SERIAL_CTL, AR_PHY_SEL_INTERNAL_ADDAC);
   2341      1.1  christos 
   2342      1.1  christos 	/* First initialization step (depends on channel band/bandwidth). */
   2343      1.1  christos #ifndef IEEE80211_NO_HT
   2344      1.1  christos 	if (extc != NULL) {
   2345      1.1  christos 		if (IEEE80211_IS_CHAN_2GHZ(c))
   2346      1.1  christos 			pvals = ini->vals_2g40;
   2347      1.1  christos 		else
   2348      1.1  christos 			pvals = ini->vals_5g40;
   2349      1.1  christos 	}
   2350      1.1  christos 	else
   2351      1.1  christos #endif
   2352      1.1  christos 	{
   2353      1.1  christos 		if (IEEE80211_IS_CHAN_2GHZ(c))
   2354      1.1  christos 			pvals = ini->vals_2g20;
   2355      1.1  christos 		else
   2356      1.1  christos 			pvals = ini->vals_5g20;
   2357      1.1  christos 	}
   2358      1.1  christos 	DPRINTFN(DBG_INIT, sc, "writing modal init vals\n");
   2359      1.1  christos 	for (i = 0; i < ini->nregs; i++) {
   2360      1.1  christos 		uint32_t val = pvals[i];
   2361      1.1  christos 
   2362      1.1  christos 		/* Fix AR_AN_TOP2 initialization value if required. */
   2363      1.1  christos 		if (ini->regs[i] == AR_AN_TOP2 &&
   2364      1.1  christos 		    (sc->sc_flags & ATHN_FLAG_AN_TOP2_FIXUP))
   2365      1.1  christos 			val &= ~AR_AN_TOP2_PWDCLKIND;
   2366      1.1  christos 		AR_WRITE(sc, ini->regs[i], val);
   2367      1.1  christos 		if (AR_IS_ANALOG_REG(ini->regs[i])) {
   2368      1.1  christos 			AR_WRITE_BARRIER(sc);
   2369      1.1  christos 			DELAY(100);
   2370      1.1  christos 		}
   2371      1.1  christos 		if ((i & 0x1f) == 0)
   2372      1.1  christos 			DELAY(1);
   2373      1.1  christos 	}
   2374      1.1  christos 	AR_WRITE_BARRIER(sc);
   2375      1.1  christos 
   2376      1.1  christos 	if (sc->sc_rx_gain != NULL)
   2377      1.1  christos 		ar9280_reset_rx_gain(sc, c);
   2378      1.1  christos 	if (sc->sc_tx_gain != NULL)
   2379      1.1  christos 		ar9280_reset_tx_gain(sc, c);
   2380      1.1  christos 
   2381      1.1  christos 	if (AR_SREV_9271_10(sc)) {
   2382      1.1  christos 		AR_WRITE(sc, AR_PHY(68), 0x30002311);
   2383      1.1  christos 		AR_WRITE(sc, AR_PHY_RF_CTL3, 0x0a020001);
   2384      1.1  christos 	}
   2385      1.1  christos 	AR_WRITE_BARRIER(sc);
   2386      1.1  christos 
   2387      1.1  christos 	/* Second initialization step (common to all channels). */
   2388      1.1  christos 	DPRINTFN(DBG_INIT, sc, "writing common init vals\n");
   2389      1.1  christos 	for (i = 0; i < ini->ncmregs; i++) {
   2390      1.1  christos 		AR_WRITE(sc, ini->cmregs[i], ini->cmvals[i]);
   2391      1.1  christos 		if (AR_IS_ANALOG_REG(ini->cmregs[i])) {
   2392      1.1  christos 			AR_WRITE_BARRIER(sc);
   2393      1.1  christos 			DELAY(100);
   2394      1.1  christos 		}
   2395      1.1  christos 		if ((i & 0x1f) == 0)
   2396      1.1  christos 			DELAY(1);
   2397      1.1  christos 	}
   2398      1.1  christos 	AR_WRITE_BARRIER(sc);
   2399      1.1  christos 
   2400      1.1  christos 	if (!AR_SINGLE_CHIP(sc))
   2401      1.1  christos 		ar5416_reset_bb_gain(sc, c);
   2402      1.1  christos 
   2403      1.1  christos 	if (IEEE80211_IS_CHAN_5GHZ(c) &&
   2404      1.1  christos 	    (sc->sc_flags & ATHN_FLAG_FAST_PLL_CLOCK)) {
   2405      1.1  christos 		/* Update modal values for fast PLL clock. */
   2406      1.1  christos #ifndef IEEE80211_NO_HT
   2407      1.1  christos 		if (extc != NULL)
   2408      1.1  christos 			pvals = ini->fastvals_5g40;
   2409      1.1  christos 		else
   2410      1.1  christos #endif
   2411      1.1  christos 			pvals = ini->fastvals_5g20;
   2412      1.1  christos 		DPRINTFN(DBG_INIT, sc, "writing fast pll clock init vals\n");
   2413      1.1  christos 		for (i = 0; i < ini->nfastregs; i++) {
   2414      1.1  christos 			AR_WRITE(sc, ini->fastregs[i], pvals[i]);
   2415      1.1  christos 			if (AR_IS_ANALOG_REG(ini->fastregs[i])) {
   2416      1.1  christos 				AR_WRITE_BARRIER(sc);
   2417      1.1  christos 				DELAY(100);
   2418      1.1  christos 			}
   2419      1.1  christos 			if ((i & 0x1f) == 0)
   2420      1.1  christos 				DELAY(1);
   2421      1.1  christos 		}
   2422      1.1  christos 	}
   2423      1.1  christos 
   2424      1.1  christos 	/*
   2425      1.1  christos 	 * Set the RX_ABORT and RX_DIS bits to prevent frames with corrupted
   2426      1.1  christos 	 * descriptor status.
   2427      1.1  christos 	 */
   2428      1.1  christos 	AR_SETBITS(sc, AR_DIAG_SW, AR_DIAG_RX_DIS | AR_DIAG_RX_ABORT);
   2429      1.1  christos 
   2430      1.1  christos 	/* Hardware workarounds for occasional Rx data corruption. */
   2431      1.1  christos 	if (AR_SREV_9280_10_OR_LATER(sc)) {
   2432      1.1  christos 		reg = AR_READ(sc, AR_PCU_MISC_MODE2);
   2433      1.1  christos 		if (!AR_SREV_9271(sc))
   2434      1.1  christos 			reg &= ~AR_PCU_MISC_MODE2_HWWAR1;
   2435      1.1  christos 		if (AR_SREV_9287_10_OR_LATER(sc))
   2436      1.1  christos 			reg &= ~AR_PCU_MISC_MODE2_HWWAR2;
   2437      1.1  christos 		AR_WRITE(sc, AR_PCU_MISC_MODE2, reg);
   2438      1.1  christos 
   2439      1.1  christos 	}
   2440      1.1  christos 	else if (AR_SREV_5416_20_OR_LATER(sc)) {
   2441      1.1  christos 		/* Disable baseband clock gating. */
   2442      1.1  christos 		AR_WRITE(sc, AR_PHY(651), 0x11);
   2443      1.1  christos 
   2444      1.1  christos 		if (AR_SREV_9160(sc)) {
   2445      1.1  christos 			/* Disable RIFS search to fix baseband hang. */
   2446      1.1  christos 			AR_CLRBITS(sc, AR_PHY_HEAVY_CLIP_FACTOR_RIFS,
   2447      1.1  christos 			    AR_PHY_RIFS_INIT_DELAY_M);
   2448      1.1  christos 		}
   2449      1.1  christos 	}
   2450      1.1  christos 	AR_WRITE_BARRIER(sc);
   2451      1.1  christos 
   2452      1.1  christos 	ar5008_set_phy(sc, c, extc);
   2453      1.1  christos 	ar5008_init_chains(sc);
   2454      1.1  christos 
   2455      1.1  christos 	if (sc->sc_flags & ATHN_FLAG_OLPC) {
   2456      1.1  christos 		extern int ticks;
   2457      1.1  christos 		sc->sc_olpc_ticks = ticks;
   2458      1.1  christos 		ops->olpc_init(sc);
   2459      1.1  christos 	}
   2460      1.1  christos 
   2461      1.1  christos 	ops->set_txpower(sc, c, extc);
   2462      1.1  christos 
   2463      1.1  christos 	if (!AR_SINGLE_CHIP(sc))
   2464      1.1  christos 		ar5416_rf_reset(sc, c);
   2465      1.1  christos }
   2466      1.1  christos 
   2467      1.1  christos Static uint8_t
   2468      1.1  christos ar5008_get_vpd(uint8_t pwr, const uint8_t *pwrPdg, const uint8_t *vpdPdg,
   2469      1.1  christos     int nicepts)
   2470      1.1  christos {
   2471      1.1  christos 	uint8_t vpd;
   2472      1.1  christos 	int i, lo, hi;
   2473      1.1  christos 
   2474      1.1  christos 	for (i = 0; i < nicepts; i++)
   2475      1.1  christos 		if (pwrPdg[i] > pwr)
   2476      1.1  christos 			break;
   2477      1.1  christos 	hi = i;
   2478      1.1  christos 	lo = hi - 1;
   2479      1.1  christos 	if (lo == -1)
   2480      1.1  christos 		lo = hi;
   2481      1.1  christos 	else if (hi == nicepts)
   2482      1.1  christos 		hi = lo;
   2483      1.1  christos 
   2484      1.1  christos 	vpd = athn_interpolate(pwr, pwrPdg[lo], vpdPdg[lo],
   2485      1.1  christos 	    pwrPdg[hi], vpdPdg[hi]);
   2486      1.1  christos 	return vpd;
   2487      1.1  christos }
   2488      1.1  christos 
   2489      1.1  christos PUBLIC void
   2490      1.1  christos ar5008_get_pdadcs(struct athn_softc *sc, uint8_t fbin,
   2491      1.1  christos     struct athn_pier *lopier, struct athn_pier *hipier, int nxpdgains,
   2492      1.1  christos     int nicepts, uint8_t overlap, uint8_t *boundaries, uint8_t *pdadcs)
   2493      1.1  christos {
   2494      1.1  christos #define DB(x)	((x) / 2)	/* Convert half dB to dB. */
   2495      1.1  christos 	uint8_t minpwr[AR_PD_GAINS_IN_MASK], maxpwr[AR_PD_GAINS_IN_MASK];
   2496      1.1  christos 	uint8_t vpd[AR_MAX_PWR_RANGE_IN_HALF_DB], pwr;
   2497      1.1  christos 	uint8_t lovpd, hivpd, boundary;
   2498      1.1  christos 	int16_t ss, delta, vpdstep, val;
   2499      1.1  christos 	int i, j, npdadcs, nvpds, maxidx, tgtidx;
   2500      1.1  christos 
   2501      1.1  christos 	/* Compute min and max power in half dB for each pdGain. */
   2502      1.1  christos 	for (i = 0; i < nxpdgains; i++) {
   2503      1.1  christos 		minpwr[i] = MAX(lopier->pwr[i][0], hipier->pwr[i][0]);
   2504      1.1  christos 		maxpwr[i] = MIN(lopier->pwr[i][nicepts - 1],
   2505      1.1  christos 		    hipier->pwr[i][nicepts - 1]);
   2506      1.1  christos 	}
   2507      1.1  christos 
   2508      1.1  christos 	/* Fill phase domain analog-to-digital converter (PDADC) table. */
   2509      1.1  christos 	npdadcs = 0;
   2510      1.1  christos 	for (i = 0; i < nxpdgains; i++) {
   2511      1.1  christos 		if (i != nxpdgains - 1)
   2512      1.1  christos 			boundaries[i] = DB(maxpwr[i] + minpwr[i + 1]) / 2;
   2513      1.1  christos 		else
   2514      1.1  christos 			boundaries[i] = DB(maxpwr[i]);
   2515      1.1  christos 		if (boundaries[i] > AR_MAX_RATE_POWER)
   2516      1.1  christos 			boundaries[i] = AR_MAX_RATE_POWER;
   2517      1.1  christos 
   2518      1.1  christos 		if (i == 0 && !AR_SREV_5416_20_OR_LATER(sc)) {
   2519      1.1  christos 			/* Fix the gain delta (AR5416 1.0 only). */
   2520      1.1  christos 			delta = boundaries[0] - 23;
   2521      1.1  christos 			boundaries[0] = 23;
   2522      1.1  christos 		}
   2523      1.1  christos 		else
   2524      1.1  christos 			delta = 0;
   2525      1.1  christos 
   2526      1.1  christos 		/* Find starting index for this pdGain. */
   2527      1.1  christos 		if (i != 0) {
   2528      1.1  christos 			ss = boundaries[i - 1] - DB(minpwr[i]) -
   2529      1.1  christos 			    overlap + 1 + delta;
   2530      1.1  christos 		}
   2531      1.1  christos 		else if (AR_SREV_9280_10_OR_LATER(sc))
   2532      1.1  christos 			ss = -DB(minpwr[i]);
   2533      1.1  christos 		else
   2534      1.1  christos 			ss = 0;
   2535      1.1  christos 
   2536      1.1  christos 		/* Compute Vpd table for this pdGain. */
   2537      1.1  christos 		nvpds = DB(maxpwr[i] - minpwr[i]) + 1;
   2538      1.1  christos 		memset(vpd, 0, sizeof(vpd));
   2539      1.1  christos 		pwr = minpwr[i];
   2540      1.1  christos 		for (j = 0; j < nvpds; j++) {
   2541      1.1  christos 			/* Get lower and higher Vpd. */
   2542      1.1  christos 			lovpd = ar5008_get_vpd(pwr, lopier->pwr[i],
   2543      1.1  christos 			    lopier->vpd[i], nicepts);
   2544      1.1  christos 			hivpd = ar5008_get_vpd(pwr, hipier->pwr[i],
   2545      1.1  christos 			    hipier->vpd[i], nicepts);
   2546      1.1  christos 
   2547      1.1  christos 			/* Interpolate the final Vpd. */
   2548      1.1  christos 			vpd[j] = athn_interpolate(fbin,
   2549      1.1  christos 			    lopier->fbin, lovpd, hipier->fbin, hivpd);
   2550      1.1  christos 
   2551      1.1  christos 			pwr += 2;	/* In half dB. */
   2552      1.1  christos 		}
   2553      1.1  christos 
   2554      1.1  christos 		/* Extrapolate data for ss < 0. */
   2555      1.1  christos 		if (vpd[1] > vpd[0])
   2556      1.1  christos 			vpdstep = vpd[1] - vpd[0];
   2557      1.1  christos 		else
   2558      1.1  christos 			vpdstep = 1;
   2559      1.1  christos 		while (ss < 0 && npdadcs < AR_NUM_PDADC_VALUES - 1) {
   2560      1.1  christos 			val = vpd[0] + ss * vpdstep;
   2561      1.1  christos 			pdadcs[npdadcs++] = MAX(val, 0);
   2562      1.1  christos 			ss++;
   2563      1.1  christos 		}
   2564      1.1  christos 
   2565      1.1  christos 		tgtidx = boundaries[i] + overlap - DB(minpwr[i]);
   2566      1.1  christos 		maxidx = MIN(tgtidx, nvpds);
   2567      1.1  christos 		while (ss < maxidx && npdadcs < AR_NUM_PDADC_VALUES - 1)
   2568      1.1  christos 			pdadcs[npdadcs++] = vpd[ss++];
   2569      1.1  christos 
   2570      1.1  christos 		if (tgtidx < maxidx)
   2571      1.1  christos 			continue;
   2572      1.1  christos 
   2573      1.1  christos 		/* Extrapolate data for maxidx <= ss <= tgtidx. */
   2574      1.1  christos 		if (vpd[nvpds - 1] > vpd[nvpds - 2])
   2575      1.1  christos 			vpdstep = vpd[nvpds - 1] - vpd[nvpds - 2];
   2576      1.1  christos 		else
   2577      1.1  christos 			vpdstep = 1;
   2578      1.1  christos 		while (ss <= tgtidx && npdadcs < AR_NUM_PDADC_VALUES - 1) {
   2579      1.1  christos 			val = vpd[nvpds - 1] + (ss - maxidx + 1) * vpdstep;
   2580      1.1  christos 			pdadcs[npdadcs++] = MIN(val, 255);
   2581      1.1  christos 			ss++;
   2582      1.1  christos 		}
   2583      1.1  christos 	}
   2584      1.1  christos 
   2585      1.1  christos 	/* Fill remaining PDADC and boundaries entries. */
   2586      1.1  christos 	if (AR_SREV_9285(sc))
   2587      1.1  christos 		boundary = AR9285_PD_GAIN_BOUNDARY_DEFAULT;
   2588      1.1  christos 	else	/* Fill with latest. */
   2589      1.1  christos 		boundary = boundaries[nxpdgains - 1];
   2590      1.1  christos 
   2591      1.1  christos 	for (; nxpdgains < AR_PD_GAINS_IN_MASK; nxpdgains++)
   2592      1.1  christos 		boundaries[nxpdgains] = boundary;
   2593      1.1  christos 
   2594      1.1  christos 	for (; npdadcs < AR_NUM_PDADC_VALUES; npdadcs++)
   2595      1.1  christos 		pdadcs[npdadcs] = pdadcs[npdadcs - 1];
   2596      1.1  christos #undef DB
   2597      1.1  christos }
   2598      1.1  christos 
   2599      1.1  christos PUBLIC void
   2600      1.1  christos ar5008_get_lg_tpow(struct athn_softc *sc, struct ieee80211_channel *c,
   2601      1.1  christos     uint8_t ctl, const struct ar_cal_target_power_leg *tgt, int nchans,
   2602      1.1  christos     uint8_t tpow[4])
   2603      1.1  christos {
   2604      1.1  christos 	uint8_t fbin;
   2605      1.1  christos 	int i, lo, hi;
   2606      1.1  christos 
   2607      1.1  christos 	/* Find interval (lower and upper indices). */
   2608      1.1  christos 	fbin = athn_chan2fbin(c);
   2609      1.1  christos 	for (i = 0; i < nchans; i++) {
   2610      1.1  christos 		if (tgt[i].bChannel == AR_BCHAN_UNUSED ||
   2611      1.1  christos 		    tgt[i].bChannel > fbin)
   2612      1.1  christos 			break;
   2613      1.1  christos 	}
   2614      1.1  christos 	hi = i;
   2615      1.1  christos 	lo = hi - 1;
   2616      1.1  christos 	if (lo == -1)
   2617      1.1  christos 		lo = hi;
   2618      1.1  christos 	else if (hi == nchans || tgt[hi].bChannel == AR_BCHAN_UNUSED)
   2619      1.1  christos 		hi = lo;
   2620      1.1  christos 
   2621      1.1  christos 	/* Interpolate values. */
   2622      1.1  christos 	for (i = 0; i < 4; i++) {
   2623      1.1  christos 		tpow[i] = athn_interpolate(fbin,
   2624      1.1  christos 		    tgt[lo].bChannel, tgt[lo].tPow2x[i],
   2625      1.1  christos 		    tgt[hi].bChannel, tgt[hi].tPow2x[i]);
   2626      1.1  christos 	}
   2627      1.1  christos 	/* XXX Apply conformance testing limit. */
   2628      1.1  christos }
   2629      1.1  christos 
   2630      1.1  christos #ifndef IEEE80211_NO_HT
   2631      1.1  christos PUBLIC void
   2632      1.1  christos ar5008_get_ht_tpow(struct athn_softc *sc, struct ieee80211_channel *c,
   2633      1.1  christos     uint8_t ctl, const struct ar_cal_target_power_ht *tgt, int nchans,
   2634      1.1  christos     uint8_t tpow[8])
   2635      1.1  christos {
   2636      1.1  christos 	uint8_t fbin;
   2637      1.1  christos 	int i, lo, hi;
   2638      1.1  christos 
   2639      1.1  christos 	/* Find interval (lower and upper indices). */
   2640      1.1  christos 	fbin = athn_chan2fbin(c);
   2641      1.1  christos 	for (i = 0; i < nchans; i++) {
   2642      1.1  christos 		if (tgt[i].bChannel == AR_BCHAN_UNUSED ||
   2643      1.1  christos 		    tgt[i].bChannel > fbin)
   2644      1.1  christos 			break;
   2645      1.1  christos 	}
   2646      1.1  christos 	hi = i;
   2647      1.1  christos 	lo = hi - 1;
   2648      1.1  christos 	if (lo == -1)
   2649      1.1  christos 		lo = hi;
   2650      1.1  christos 	else if (hi == nchans || tgt[hi].bChannel == AR_BCHAN_UNUSED)
   2651      1.1  christos 		hi = lo;
   2652      1.1  christos 
   2653      1.1  christos 	/* Interpolate values. */
   2654      1.1  christos 	for (i = 0; i < 8; i++) {
   2655      1.1  christos 		tpow[i] = athn_interpolate(fbin,
   2656      1.1  christos 		    tgt[lo].bChannel, tgt[lo].tPow2x[i],
   2657      1.1  christos 		    tgt[hi].bChannel, tgt[hi].tPow2x[i]);
   2658      1.1  christos 	}
   2659      1.1  christos 	/* XXX Apply conformance testing limit. */
   2660      1.1  christos }
   2661      1.1  christos #endif
   2662      1.1  christos 
   2663      1.1  christos /*
   2664      1.1  christos  * Adaptive noise immunity.
   2665      1.1  christos  */
   2666      1.1  christos Static void
   2667      1.1  christos ar5008_set_noise_immunity_level(struct athn_softc *sc, int level)
   2668      1.1  christos {
   2669      1.1  christos 	int high = level == 4;
   2670      1.1  christos 	uint32_t reg;
   2671      1.1  christos 
   2672      1.1  christos 	reg = AR_READ(sc, AR_PHY_DESIRED_SZ);
   2673      1.1  christos 	reg = RW(reg, AR_PHY_DESIRED_SZ_TOT_DES, high ? -62 : -55);
   2674      1.1  christos 	AR_WRITE(sc, AR_PHY_DESIRED_SZ, reg);
   2675      1.1  christos 
   2676      1.1  christos 	reg = AR_READ(sc, AR_PHY_AGC_CTL1);
   2677      1.1  christos 	reg = RW(reg, AR_PHY_AGC_CTL1_COARSE_LOW, high ? -70 : -64);
   2678      1.1  christos 	reg = RW(reg, AR_PHY_AGC_CTL1_COARSE_HIGH, high ? -12 : -14);
   2679      1.1  christos 	AR_WRITE(sc, AR_PHY_AGC_CTL1, reg);
   2680      1.1  christos 
   2681      1.1  christos 	reg = AR_READ(sc, AR_PHY_FIND_SIG);
   2682      1.1  christos 	reg = RW(reg, AR_PHY_FIND_SIG_FIRPWR, high ? -80 : -78);
   2683      1.1  christos 	AR_WRITE(sc, AR_PHY_FIND_SIG, reg);
   2684      1.1  christos 
   2685      1.1  christos 	AR_WRITE_BARRIER(sc);
   2686      1.1  christos }
   2687      1.1  christos 
   2688      1.1  christos Static void
   2689      1.1  christos ar5008_enable_ofdm_weak_signal(struct athn_softc *sc)
   2690      1.1  christos {
   2691      1.1  christos 	uint32_t reg;
   2692      1.1  christos 
   2693      1.1  christos 	reg = AR_READ(sc, AR_PHY_SFCORR_LOW);
   2694      1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_LOW_M1_THRESH_LOW, 50);
   2695      1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_LOW_M2_THRESH_LOW, 40);
   2696      1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_LOW_M2COUNT_THR_LOW, 48);
   2697      1.1  christos 	AR_WRITE(sc, AR_PHY_SFCORR_LOW, reg);
   2698      1.1  christos 
   2699      1.1  christos 	reg = AR_READ(sc, AR_PHY_SFCORR);
   2700      1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_M1_THRESH, 77);
   2701      1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_M2_THRESH, 64);
   2702      1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_M2COUNT_THR, 16);
   2703      1.1  christos 	AR_WRITE(sc, AR_PHY_SFCORR, reg);
   2704      1.1  christos 
   2705      1.1  christos 	reg = AR_READ(sc, AR_PHY_SFCORR_EXT);
   2706      1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_EXT_M1_THRESH_LOW, 50);
   2707      1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_EXT_M2_THRESH_LOW, 40);
   2708      1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_EXT_M1_THRESH, 77);
   2709      1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_EXT_M2_THRESH, 64);
   2710      1.1  christos 	AR_WRITE(sc, AR_PHY_SFCORR_EXT, reg);
   2711      1.1  christos 
   2712      1.1  christos 	AR_SETBITS(sc, AR_PHY_SFCORR_LOW,
   2713      1.1  christos 	    AR_PHY_SFCORR_LOW_USE_SELF_CORR_LOW);
   2714      1.1  christos 	AR_WRITE_BARRIER(sc);
   2715      1.1  christos }
   2716      1.1  christos 
   2717      1.1  christos Static void
   2718      1.1  christos ar5008_disable_ofdm_weak_signal(struct athn_softc *sc)
   2719      1.1  christos {
   2720      1.1  christos 	uint32_t reg;
   2721      1.1  christos 
   2722      1.1  christos 	reg = AR_READ(sc, AR_PHY_SFCORR_LOW);
   2723      1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_LOW_M1_THRESH_LOW, 127);
   2724      1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_LOW_M2_THRESH_LOW, 127);
   2725      1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_LOW_M2COUNT_THR_LOW, 63);
   2726      1.1  christos 	AR_WRITE(sc, AR_PHY_SFCORR_LOW, reg);
   2727      1.1  christos 
   2728      1.1  christos 	reg = AR_READ(sc, AR_PHY_SFCORR);
   2729      1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_M1_THRESH, 127);
   2730      1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_M2_THRESH, 127);
   2731      1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_M2COUNT_THR, 31);
   2732      1.1  christos 	AR_WRITE(sc, AR_PHY_SFCORR, reg);
   2733      1.1  christos 
   2734      1.1  christos 	reg = AR_READ(sc, AR_PHY_SFCORR_EXT);
   2735      1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_EXT_M1_THRESH_LOW, 127);
   2736      1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_EXT_M2_THRESH_LOW, 127);
   2737      1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_EXT_M1_THRESH, 127);
   2738      1.1  christos 	reg = RW(reg, AR_PHY_SFCORR_EXT_M2_THRESH, 127);
   2739      1.1  christos 	AR_WRITE(sc, AR_PHY_SFCORR_EXT, reg);
   2740      1.1  christos 
   2741      1.1  christos 	AR_CLRBITS(sc, AR_PHY_SFCORR_LOW,
   2742      1.1  christos 	    AR_PHY_SFCORR_LOW_USE_SELF_CORR_LOW);
   2743      1.1  christos 	AR_WRITE_BARRIER(sc);
   2744      1.1  christos }
   2745      1.1  christos 
   2746      1.1  christos Static void
   2747      1.1  christos ar5008_set_cck_weak_signal(struct athn_softc *sc, int high)
   2748      1.1  christos {
   2749      1.1  christos 	uint32_t reg;
   2750      1.1  christos 
   2751      1.1  christos 	reg = AR_READ(sc, AR_PHY_CCK_DETECT);
   2752      1.1  christos 	reg = RW(reg, AR_PHY_CCK_DETECT_WEAK_SIG_THR_CCK, high ? 6 : 8);
   2753      1.1  christos 	AR_WRITE(sc, AR_PHY_CCK_DETECT, reg);
   2754      1.1  christos 	AR_WRITE_BARRIER(sc);
   2755      1.1  christos }
   2756      1.1  christos 
   2757      1.1  christos Static void
   2758      1.1  christos ar5008_set_firstep_level(struct athn_softc *sc, int level)
   2759      1.1  christos {
   2760      1.1  christos 	uint32_t reg;
   2761      1.1  christos 
   2762      1.1  christos 	reg = AR_READ(sc, AR_PHY_FIND_SIG);
   2763      1.1  christos 	reg = RW(reg, AR_PHY_FIND_SIG_FIRSTEP, level * 4);
   2764      1.1  christos 	AR_WRITE(sc, AR_PHY_FIND_SIG, reg);
   2765      1.1  christos 	AR_WRITE_BARRIER(sc);
   2766      1.1  christos }
   2767      1.1  christos 
   2768      1.1  christos Static void
   2769      1.1  christos ar5008_set_spur_immunity_level(struct athn_softc *sc, int level)
   2770      1.1  christos {
   2771      1.1  christos 	uint32_t reg;
   2772      1.1  christos 
   2773      1.1  christos 	reg = AR_READ(sc, AR_PHY_TIMING5);
   2774      1.1  christos 	reg = RW(reg, AR_PHY_TIMING5_CYCPWR_THR1, (level + 1) * 2);
   2775      1.1  christos 	AR_WRITE(sc, AR_PHY_TIMING5, reg);
   2776      1.1  christos 	AR_WRITE_BARRIER(sc);
   2777      1.1  christos }
   2778