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