Home | History | Annotate | Line # | Download | only in pci
if_ral_pci.c revision 1.23
      1  1.23  christos /*	$NetBSD: if_ral_pci.c,v 1.23 2016/07/06 14:28:51 christos 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.23  christos __KERNEL_RCSID(0, "$NetBSD: if_ral_pci.c,v 1.23 2016/07/06 14:28:51 christos 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.22  christos #define RAL_POWER_MANAGEMENT 0	/* Disabled for now */
     60  1.22  christos 
     61   1.4    rpaulo static struct ral_opns {
     62   1.4    rpaulo 	int	(*attach)(void *, int);
     63   1.4    rpaulo 	int	(*detach)(void *);
     64  1.22  christos #if RAL_POWER_MANAGEMENT
     65  1.22  christos 	void	(*suspend)(void *);
     66  1.22  christos 	void	(*wakeup)(void *);
     67  1.22  christos #endif
     68   1.4    rpaulo 	int	(*intr)(void *);
     69   1.4    rpaulo 
     70   1.4    rpaulo }  ral_rt2560_opns = {
     71   1.4    rpaulo 	rt2560_attach,
     72   1.4    rpaulo 	rt2560_detach,
     73  1.22  christos #if RAL_POWER_MANAGEMENT
     74  1.22  christos 	rt2560_suspend,
     75  1.22  christos 	rt2560_wakeup,
     76  1.22  christos #endif
     77   1.4    rpaulo 	rt2560_intr
     78   1.4    rpaulo 
     79   1.4    rpaulo }, ral_rt2661_opns = {
     80   1.4    rpaulo 	rt2661_attach,
     81   1.4    rpaulo 	rt2661_detach,
     82  1.22  christos #if RAL_POWER_MANAGEMENT
     83  1.22  christos 	rt2661_suspend,
     84  1.22  christos 	rt2661_wakeup,
     85  1.22  christos #endif
     86   1.4    rpaulo 	rt2661_intr
     87  1.22  christos 
     88  1.22  christos }, ral_rt2860_opns = {
     89  1.22  christos 	rt2860_attach,
     90  1.22  christos 	rt2860_detach,
     91  1.22  christos #if RAL_POWER_MANAGEMENT
     92  1.22  christos 	rt2860_suspend,
     93  1.22  christos 	rt2860_wakeup,
     94  1.22  christos #endif
     95  1.22  christos 	rt2860_intr
     96   1.4    rpaulo };
     97   1.4    rpaulo 
     98   1.1  drochner struct ral_pci_softc {
     99   1.4    rpaulo 	union {
    100   1.4    rpaulo 		struct rt2560_softc	sc_rt2560;
    101   1.4    rpaulo 		struct rt2661_softc	sc_rt2661;
    102  1.22  christos 		struct rt2860_softc	sc_rt2860;
    103   1.4    rpaulo 	} u;
    104   1.4    rpaulo #define sc_sc	u.sc_rt2560
    105   1.1  drochner 
    106   1.1  drochner 	/* PCI specific goo */
    107   1.4    rpaulo 	struct ral_opns		*sc_opns;
    108   1.1  drochner 	pci_chipset_tag_t	sc_pc;
    109   1.1  drochner 	void			*sc_ih;
    110   1.1  drochner 	bus_size_t		sc_mapsize;
    111   1.1  drochner };
    112   1.1  drochner 
    113   1.1  drochner /* Base Address Register */
    114  1.18    dyoung #define RAL_PCI_BAR0 PCI_BAR(0)
    115   1.1  drochner 
    116  1.11    cegger int	ral_pci_match(device_t, cfdata_t, void *);
    117  1.11    cegger void	ral_pci_attach(device_t, device_t, void *);
    118  1.11    cegger int	ral_pci_detach(device_t, int);
    119  1.22  christos #if RAL_POWER_MANAGEMENT
    120  1.22  christos int	ral_pci_activate(struct device *, devact_t);
    121  1.22  christos void	ral_pci_wakeup(struct ral_pci_softc *);
    122  1.22  christos #else
    123  1.22  christos #define ral_pci_activate NULL
    124  1.22  christos #endif
    125   1.1  drochner 
    126  1.20  drochner CFATTACH_DECL_NEW(ral_pci, sizeof (struct ral_pci_softc),
    127  1.22  christos 	ral_pci_match, ral_pci_attach, ral_pci_detach, ral_pci_activate);
    128  1.22  christos 
    129  1.22  christos static const struct ral_pci_matchid {
    130  1.22  christos 	pci_vendor_id_t         ral_vendor;
    131  1.22  christos 	pci_product_id_t        ral_product;
    132  1.22  christos } ral_pci_devices[] = {
    133  1.22  christos 	{ PCI_VENDOR_AWT,    PCI_PRODUCT_AWT_RT2890 },
    134  1.22  christos 	{ PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT2860_1 },
    135  1.22  christos 	{ PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT2860_2 },
    136  1.22  christos 	{ PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT2860_3 },
    137  1.22  christos 	{ PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT2860_4 },
    138  1.22  christos 	{ PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT2860_5 },
    139  1.22  christos 	{ PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT2860_6 },
    140  1.22  christos 	{ PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT2860_7 },
    141  1.23  christos 	{ PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT3591_1 },
    142  1.23  christos 	{ PCI_VENDOR_EDIMAX, PCI_PRODUCT_EDIMAX_RT3591_2 },
    143  1.23  christos 	{ PCI_VENDOR_MSI,    PCI_PRODUCT_AWT_RT2890 },
    144  1.23  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2560 },
    145  1.23  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2561 },
    146  1.23  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2561S },
    147  1.23  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2661 },
    148  1.23  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2760 },
    149  1.23  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2790 },
    150  1.23  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2860 },
    151  1.23  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT2890 },
    152  1.22  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3060 },
    153  1.22  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3062 },
    154  1.22  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3090 },
    155  1.22  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3091 },
    156  1.22  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3092 },
    157  1.22  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3562 },
    158  1.22  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3592 },
    159  1.23  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT3593 },
    160  1.23  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT5360 },
    161  1.23  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT5362 },
    162  1.23  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT5390_1 },
    163  1.23  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT5390_2 },
    164  1.23  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT5390_3 },
    165  1.23  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT5390_4 },
    166  1.23  christos 	{ PCI_VENDOR_RALINK, PCI_PRODUCT_RALINK_RT5390_5 },
    167  1.22  christos };
    168   1.1  drochner 
    169   1.1  drochner int
    170  1.11    cegger ral_pci_match(device_t parent, cfdata_t cfdata,
    171   1.5  christos     void *aux)
    172   1.1  drochner {
    173   1.1  drochner 	struct pci_attach_args *pa = aux;
    174   1.1  drochner 
    175  1.22  christos 	for (size_t i = 0; i < __arraycount(ral_pci_devices); i++) {
    176  1.22  christos 		const struct ral_pci_matchid *ral = &ral_pci_devices[i];
    177  1.22  christos 		if (PCI_VENDOR(pa->pa_id) == ral->ral_vendor &&
    178  1.22  christos 		    PCI_PRODUCT(pa->pa_id) == ral->ral_product)
    179  1.22  christos 			return 1;
    180   1.4    rpaulo 	}
    181   1.1  drochner 
    182   1.1  drochner 	return 0;
    183   1.1  drochner }
    184   1.1  drochner 
    185   1.1  drochner void
    186  1.11    cegger ral_pci_attach(device_t parent, device_t self, void *aux)
    187   1.1  drochner {
    188  1.12    cegger 	struct ral_pci_softc *psc = device_private(self);
    189   1.4    rpaulo 	struct rt2560_softc *sc = &psc->sc_sc;
    190  1.17    dyoung 	const struct pci_attach_args *pa = aux;
    191   1.1  drochner 	const char *intrstr;
    192   1.1  drochner 	bus_addr_t base;
    193   1.1  drochner 	pci_intr_handle_t ih;
    194  1.22  christos 	pcireg_t memtype, reg;
    195  1.19  drochner 	int error;
    196  1.21  christos 	char intrbuf[PCI_INTRSTR_LEN];
    197   1.4    rpaulo 
    198  1.19  drochner 	pci_aprint_devinfo(pa, NULL);
    199   1.4    rpaulo 
    200  1.22  christos 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_RALINK) {
    201  1.22  christos 		switch (PCI_PRODUCT(pa->pa_id)) {
    202  1.22  christos 		case PCI_PRODUCT_RALINK_RT2560:
    203  1.22  christos 			psc->sc_opns = &ral_rt2560_opns;
    204  1.22  christos 			break;
    205  1.22  christos 		case PCI_PRODUCT_RALINK_RT2561:
    206  1.22  christos 		case PCI_PRODUCT_RALINK_RT2561S:
    207  1.22  christos 		case PCI_PRODUCT_RALINK_RT2661:
    208  1.22  christos 			psc->sc_opns = &ral_rt2661_opns;
    209  1.22  christos 			break;
    210  1.22  christos 		default:
    211  1.22  christos 			psc->sc_opns = &ral_rt2860_opns;
    212  1.22  christos 			break;
    213  1.22  christos 		}
    214  1.22  christos 	} else {
    215  1.22  christos 		/* all other vendors are RT2860 only */
    216  1.22  christos 		psc->sc_opns = &ral_rt2860_opns;
    217  1.22  christos 	}
    218   1.4    rpaulo 
    219  1.20  drochner 	sc->sc_dev = self;
    220   1.1  drochner 	sc->sc_dmat = pa->pa_dmat;
    221   1.1  drochner 	psc->sc_pc = pa->pa_pc;
    222   1.1  drochner 
    223   1.1  drochner 	/* enable the appropriate bits in the PCI CSR */
    224   1.1  drochner 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    225   1.1  drochner 	reg |= PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_MEM_ENABLE;
    226   1.1  drochner 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, reg);
    227   1.1  drochner 
    228   1.1  drochner 	/* map control/status registers */
    229  1.22  christos 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, RAL_PCI_BAR0);
    230  1.22  christos 	error = pci_mapreg_map(pa, RAL_PCI_BAR0, memtype, 0, &sc->sc_st,
    231  1.22  christos 	    &sc->sc_sh, &base, &psc->sc_mapsize);
    232   1.4    rpaulo 
    233   1.1  drochner 	if (error != 0) {
    234   1.4    rpaulo 		aprint_error(": could not map memory space\n");
    235   1.1  drochner 		return;
    236   1.1  drochner 	}
    237   1.1  drochner 
    238   1.1  drochner 	if (pci_intr_map(pa, &ih) != 0) {
    239   1.4    rpaulo 		aprint_error(": could not map interrupt\n");
    240   1.1  drochner 		return;
    241   1.1  drochner 	}
    242   1.1  drochner 
    243  1.21  christos 	intrstr = pci_intr_string(psc->sc_pc, ih, intrbuf, sizeof(intrbuf));
    244   1.4    rpaulo 	psc->sc_ih = pci_intr_establish(psc->sc_pc, ih, IPL_NET,
    245   1.4    rpaulo 	    psc->sc_opns->intr, sc);
    246   1.4    rpaulo 
    247   1.1  drochner 	if (psc->sc_ih == NULL) {
    248   1.4    rpaulo 		aprint_error(": could not establish interrupt");
    249   1.1  drochner 		if (intrstr != NULL)
    250  1.14     njoly 			aprint_error(" at %s", intrstr);
    251   1.4    rpaulo 		aprint_error("\n");
    252   1.1  drochner 		return;
    253   1.1  drochner 	}
    254  1.20  drochner 	aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
    255   1.1  drochner 
    256   1.4    rpaulo 	(*psc->sc_opns->attach)(sc, PCI_PRODUCT(pa->pa_id));
    257   1.1  drochner }
    258   1.1  drochner 
    259   1.1  drochner int
    260  1.11    cegger ral_pci_detach(device_t self, int flags)
    261   1.1  drochner {
    262  1.12    cegger 	struct ral_pci_softc *psc = device_private(self);
    263   1.4    rpaulo 	struct rt2560_softc *sc = &psc->sc_sc;
    264  1.22  christos 	int error;
    265  1.22  christos 
    266  1.22  christos 	if (psc->sc_ih != NULL) {
    267  1.22  christos 		pci_intr_disestablish(psc->sc_pc, psc->sc_ih);
    268  1.22  christos 
    269  1.22  christos 		error = (*psc->sc_opns->detach)(sc);
    270  1.22  christos 		if (error != 0)
    271  1.22  christos 			return error;
    272  1.22  christos 	}
    273  1.22  christos 
    274  1.22  christos 	if (psc->sc_mapsize > 0)
    275  1.22  christos 		bus_space_unmap(sc->sc_st, sc->sc_sh, psc->sc_mapsize);
    276   1.1  drochner 
    277  1.22  christos 	return 0;
    278  1.22  christos }
    279  1.22  christos 
    280  1.22  christos #if RAL_POWER_MANAGEMENT
    281  1.22  christos int
    282  1.22  christos ral_pci_activate(struct device *self, devact_t act)
    283  1.22  christos {
    284  1.22  christos 	struct ral_pci_softc *psc = (struct ral_pci_softc *)self;
    285  1.22  christos 	struct rt2560_softc *sc = &psc->sc_sc;
    286   1.1  drochner 
    287  1.22  christos 	switch (act) {
    288  1.22  christos 	case DVACT_SUSPEND:
    289  1.22  christos 		(*psc->sc_opns->suspend)(sc);
    290  1.22  christos 		break;
    291  1.22  christos 	case DVACT_WAKEUP:
    292  1.22  christos 		ral_pci_wakeup(psc);
    293  1.22  christos 		break;
    294  1.22  christos 	}
    295   1.1  drochner 	return 0;
    296   1.1  drochner }
    297   1.4    rpaulo 
    298  1.22  christos void
    299  1.22  christos ral_pci_wakeup(struct ral_pci_softc *psc)
    300  1.22  christos {
    301  1.22  christos 	struct rt2560_softc *sc = &psc->sc_sc;
    302  1.22  christos 	int s;
    303  1.22  christos 
    304  1.22  christos 	s = splnet();
    305  1.22  christos 	(*psc->sc_opns->wakeup)(sc);
    306  1.22  christos 	splx(s);
    307  1.22  christos }
    308  1.22  christos #endif
    309