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