Home | History | Annotate | Line # | Download | only in pci
if_ral_pci.c revision 1.25
      1  1.25  jdolecek /*	$NetBSD: if_ral_pci.c,v 1.25 2018/12/09 11:14:02 jdolecek Exp $	*/
      2  1.22  christos /*	$OpenBSD: if_ral_pci.c,v 1.24 2015/11/24 17:11:39 mpi Exp $  */
      3   1.1  drochner 
      4   1.1  drochner /*-
      5  1.22  christos  * Copyright (c) 2005-2010 Damien Bergamini <damien.bergamini (at) free.fr>
      6   1.1  drochner  *
      7   1.1  drochner  * Permission to use, copy, modify, and distribute this software for any
      8   1.1  drochner  * purpose with or without fee is hereby granted, provided that the above
      9   1.1  drochner  * copyright notice and this permission notice appear in all copies.
     10   1.1  drochner  *
     11   1.1  drochner  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12   1.1  drochner  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13   1.1  drochner  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14   1.1  drochner  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15   1.1  drochner  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16   1.1  drochner  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17   1.1  drochner  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18   1.1  drochner  */
     19   1.1  drochner 
     20   1.1  drochner /*
     21  1.22  christos  * PCI front-end for the Ralink RT2560/RT2561/RT2860/RT3090 driver.
     22   1.1  drochner  */
     23   1.1  drochner #include <sys/cdefs.h>
     24  1.25  jdolecek __KERNEL_RCSID(0, "$NetBSD: if_ral_pci.c,v 1.25 2018/12/09 11:14:02 jdolecek Exp $");
     25   1.1  drochner 
     26   1.1  drochner 
     27   1.1  drochner #include <sys/param.h>
     28   1.1  drochner #include <sys/sockio.h>
     29   1.1  drochner #include <sys/mbuf.h>
     30   1.1  drochner #include <sys/kernel.h>
     31   1.1  drochner #include <sys/socket.h>
     32   1.1  drochner #include <sys/systm.h>
     33   1.1  drochner #include <sys/malloc.h>
     34   1.1  drochner #include <sys/device.h>
     35   1.1  drochner 
     36   1.7        ad #include <sys/bus.h>
     37   1.7        ad #include <sys/intr.h>
     38   1.1  drochner 
     39   1.1  drochner #include <net/if.h>
     40   1.1  drochner #include <net/if_dl.h>
     41   1.4    rpaulo #include <net/if_media.h>
     42   1.1  drochner #include <net/if_ether.h>
     43   1.1  drochner 
     44   1.1  drochner #include <netinet/in.h>
     45   1.1  drochner 
     46   1.1  drochner #include <net80211/ieee80211_var.h>
     47   1.9       scw #include <net80211/ieee80211_amrr.h>
     48   1.1  drochner #include <net80211/ieee80211_rssadapt.h>
     49   1.1  drochner #include <net80211/ieee80211_radiotap.h>
     50   1.1  drochner 
     51   1.4    rpaulo #include <dev/ic/rt2560var.h>
     52   1.4    rpaulo #include <dev/ic/rt2661var.h>
     53  1.22  christos #include <dev/ic/rt2860var.h>
     54   1.1  drochner 
     55   1.1  drochner #include <dev/pci/pcireg.h>
     56   1.1  drochner #include <dev/pci/pcivar.h>
     57   1.1  drochner #include <dev/pci/pcidevs.h>
     58   1.1  drochner 
     59   1.4    rpaulo static struct ral_opns {
     60   1.4    rpaulo 	int	(*attach)(void *, int);
     61   1.4    rpaulo 	int	(*detach)(void *);
     62   1.4    rpaulo 	int	(*intr)(void *);
     63   1.4    rpaulo 
     64   1.4    rpaulo }  ral_rt2560_opns = {
     65   1.4    rpaulo 	rt2560_attach,
     66   1.4    rpaulo 	rt2560_detach,
     67   1.4    rpaulo 	rt2560_intr
     68   1.4    rpaulo 
     69   1.4    rpaulo }, ral_rt2661_opns = {
     70   1.4    rpaulo 	rt2661_attach,
     71   1.4    rpaulo 	rt2661_detach,
     72   1.4    rpaulo 	rt2661_intr
     73  1.22  christos 
     74  1.22  christos }, ral_rt2860_opns = {
     75  1.22  christos 	rt2860_attach,
     76  1.22  christos 	rt2860_detach,
     77  1.22  christos 	rt2860_intr
     78   1.4    rpaulo };
     79   1.4    rpaulo 
     80   1.1  drochner struct ral_pci_softc {
     81   1.4    rpaulo 	union {
     82   1.4    rpaulo 		struct rt2560_softc	sc_rt2560;
     83   1.4    rpaulo 		struct rt2661_softc	sc_rt2661;
     84  1.22  christos 		struct rt2860_softc	sc_rt2860;
     85   1.4    rpaulo 	} u;
     86   1.4    rpaulo #define sc_sc	u.sc_rt2560
     87   1.1  drochner 
     88   1.1  drochner 	/* PCI specific goo */
     89   1.4    rpaulo 	struct ral_opns		*sc_opns;
     90   1.1  drochner 	pci_chipset_tag_t	sc_pc;
     91   1.1  drochner 	void			*sc_ih;
     92   1.1  drochner 	bus_size_t		sc_mapsize;
     93   1.1  drochner };
     94   1.1  drochner 
     95   1.1  drochner /* Base Address Register */
     96  1.18    dyoung #define RAL_PCI_BAR0 PCI_BAR(0)
     97   1.1  drochner 
     98  1.11    cegger int	ral_pci_match(device_t, cfdata_t, void *);
     99  1.11    cegger void	ral_pci_attach(device_t, device_t, void *);
    100  1.11    cegger int	ral_pci_detach(device_t, int);
    101   1.1  drochner 
    102  1.20  drochner CFATTACH_DECL_NEW(ral_pci, sizeof (struct ral_pci_softc),
    103  1.24      maya 	ral_pci_match, ral_pci_attach, ral_pci_detach, NULL);
    104  1.22  christos 
    105  1.22  christos static const struct ral_pci_matchid {
    106  1.22  christos 	pci_vendor_id_t         ral_vendor;
    107  1.22  christos 	pci_product_id_t        ral_product;
    108  1.22  christos } ral_pci_devices[] = {
    109  1.22  christos 	{ PCI_VENDOR_AWT,    PCI_PRODUCT_AWT_RT2890 },
    110  1.22  christos 	{ PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT2860_1 },
    111  1.22  christos 	{ PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT2860_2 },
    112  1.22  christos 	{ PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT2860_3 },
    113  1.22  christos 	{ PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT2860_4 },
    114  1.22  christos 	{ PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT2860_5 },
    115  1.22  christos 	{ PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT2860_6 },
    116  1.22  christos 	{ PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT2860_7 },
    117  1.23  christos 	{ PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT3591_1 },
    118  1.23  christos 	{ PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT3591_2 },
    119  1.23  christos 	{ PCI_VENDOR_MSI,    PCI_PRODUCT_AWT_RT2890 },
    120  1.23  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2560 },
    121  1.23  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2561 },
    122  1.23  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2561S },
    123  1.23  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2661 },
    124  1.23  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2760 },
    125  1.23  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2790 },
    126  1.23  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2860 },
    127  1.23  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2890 },
    128  1.22  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3060 },
    129  1.22  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3062 },
    130  1.22  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3090 },
    131  1.22  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3091 },
    132  1.22  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3092 },
    133  1.22  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3562 },
    134  1.22  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3592 },
    135  1.23  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3593 },
    136  1.23  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT5360 },
    137  1.23  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT5362 },
    138  1.23  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT5390_1 },
    139  1.23  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT5390_2 },
    140  1.23  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT5390_3 },
    141  1.23  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT5390_4 },
    142  1.23  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT5390_5 },
    143  1.22  christos };
    144   1.1  drochner 
    145   1.1  drochner int
    146  1.11    cegger ral_pci_match(device_t parent, cfdata_t cfdata,
    147   1.5  christos     void *aux)
    148   1.1  drochner {
    149   1.1  drochner 	struct pci_attach_args *pa = aux;
    150   1.1  drochner 
    151  1.22  christos 	for (size_t i = 0; i < __arraycount(ral_pci_devices); i++) {
    152  1.22  christos 		const struct ral_pci_matchid *ral = &ral_pci_devices[i];
    153  1.22  christos 		if (PCI_VENDOR(pa->pa_id) == ral->ral_vendor &&
    154  1.22  christos 		    PCI_PRODUCT(pa->pa_id) == ral->ral_product)
    155  1.22  christos 			return 1;
    156   1.4    rpaulo 	}
    157   1.1  drochner 
    158   1.1  drochner 	return 0;
    159   1.1  drochner }
    160   1.1  drochner 
    161   1.1  drochner void
    162  1.11    cegger ral_pci_attach(device_t parent, device_t self, void *aux)
    163   1.1  drochner {
    164  1.12    cegger 	struct ral_pci_softc *psc = device_private(self);
    165   1.4    rpaulo 	struct rt2560_softc *sc = &psc->sc_sc;
    166  1.17    dyoung 	const struct pci_attach_args *pa = aux;
    167   1.1  drochner 	const char *intrstr;
    168   1.1  drochner 	bus_addr_t base;
    169   1.1  drochner 	pci_intr_handle_t ih;
    170  1.22  christos 	pcireg_t memtype, reg;
    171  1.19  drochner 	int error;
    172  1.21  christos 	char intrbuf[PCI_INTRSTR_LEN];
    173   1.4    rpaulo 
    174  1.19  drochner 	pci_aprint_devinfo(pa, NULL);
    175   1.4    rpaulo 
    176  1.22  christos 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_RALINK) {
    177  1.22  christos 		switch (PCI_PRODUCT(pa->pa_id)) {
    178  1.22  christos 		case PCI_PRODUCT_RALINK_RT2560:
    179  1.22  christos 			psc->sc_opns = &ral_rt2560_opns;
    180  1.22  christos 			break;
    181  1.22  christos 		case PCI_PRODUCT_RALINK_RT2561:
    182  1.22  christos 		case PCI_PRODUCT_RALINK_RT2561S:
    183  1.22  christos 		case PCI_PRODUCT_RALINK_RT2661:
    184  1.22  christos 			psc->sc_opns = &ral_rt2661_opns;
    185  1.22  christos 			break;
    186  1.22  christos 		default:
    187  1.22  christos 			psc->sc_opns = &ral_rt2860_opns;
    188  1.22  christos 			break;
    189  1.22  christos 		}
    190  1.22  christos 	} else {
    191  1.22  christos 		/* all other vendors are RT2860 only */
    192  1.22  christos 		psc->sc_opns = &ral_rt2860_opns;
    193  1.22  christos 	}
    194   1.4    rpaulo 
    195  1.20  drochner 	sc->sc_dev = self;
    196   1.1  drochner 	sc->sc_dmat = pa->pa_dmat;
    197   1.1  drochner 	psc->sc_pc = pa->pa_pc;
    198   1.1  drochner 
    199   1.1  drochner 	/* enable the appropriate bits in the PCI CSR */
    200   1.1  drochner 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    201   1.1  drochner 	reg |= PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_MEM_ENABLE;
    202   1.1  drochner 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, reg);
    203   1.1  drochner 
    204   1.1  drochner 	/* map control/status registers */
    205  1.22  christos 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, RAL_PCI_BAR0);
    206  1.22  christos 	error = pci_mapreg_map(pa, RAL_PCI_BAR0, memtype, 0, &sc->sc_st,
    207  1.22  christos 	    &sc->sc_sh, &base, &psc->sc_mapsize);
    208   1.4    rpaulo 
    209   1.1  drochner 	if (error != 0) {
    210   1.4    rpaulo 		aprint_error(": could not map memory space\n");
    211   1.1  drochner 		return;
    212   1.1  drochner 	}
    213   1.1  drochner 
    214   1.1  drochner 	if (pci_intr_map(pa, &ih) != 0) {
    215   1.4    rpaulo 		aprint_error(": could not map interrupt\n");
    216   1.1  drochner 		return;
    217   1.1  drochner 	}
    218   1.1  drochner 
    219  1.21  christos 	intrstr = pci_intr_string(psc->sc_pc, ih, intrbuf, sizeof(intrbuf));
    220  1.25  jdolecek 	psc->sc_ih = pci_intr_establish_xname(psc->sc_pc, ih, IPL_NET,
    221  1.25  jdolecek 	    psc->sc_opns->intr, sc, device_xname(self));
    222   1.4    rpaulo 
    223   1.1  drochner 	if (psc->sc_ih == NULL) {
    224   1.4    rpaulo 		aprint_error(": could not establish interrupt");
    225   1.1  drochner 		if (intrstr != NULL)
    226  1.14     njoly 			aprint_error(" at %s", intrstr);
    227   1.4    rpaulo 		aprint_error("\n");
    228   1.1  drochner 		return;
    229   1.1  drochner 	}
    230  1.20  drochner 	aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
    231   1.1  drochner 
    232   1.4    rpaulo 	(*psc->sc_opns->attach)(sc, PCI_PRODUCT(pa->pa_id));
    233   1.1  drochner }
    234   1.1  drochner 
    235   1.1  drochner int
    236  1.11    cegger ral_pci_detach(device_t self, int flags)
    237   1.1  drochner {
    238  1.12    cegger 	struct ral_pci_softc *psc = device_private(self);
    239   1.4    rpaulo 	struct rt2560_softc *sc = &psc->sc_sc;
    240  1.22  christos 	int error;
    241  1.22  christos 
    242  1.22  christos 	if (psc->sc_ih != NULL) {
    243  1.22  christos 		pci_intr_disestablish(psc->sc_pc, psc->sc_ih);
    244  1.22  christos 
    245  1.22  christos 		error = (*psc->sc_opns->detach)(sc);
    246  1.22  christos 		if (error != 0)
    247  1.22  christos 			return error;
    248  1.22  christos 	}
    249  1.22  christos 
    250  1.22  christos 	if (psc->sc_mapsize > 0)
    251  1.22  christos 		bus_space_unmap(sc->sc_st, sc->sc_sh, psc->sc_mapsize);
    252   1.1  drochner 
    253  1.22  christos 	return 0;
    254  1.22  christos }
    255