Home | History | Annotate | Line # | Download | only in cardbus
      1 /*	$NetBSD: if_ral_cardbus.c,v 1.26 2022/09/25 17:33:19 thorpej Exp $	*/
      2 /*	$OpenBSD: if_ral_cardbus.c,v 1.6 2006/01/09 20:03:31 damien Exp $  */
      3 
      4 /*-
      5  * Copyright (c) 2005, 2006
      6  *	Damien Bergamini <damien.bergamini (at) free.fr>
      7  *
      8  * Permission to use, copy, modify, and distribute this software for any
      9  * purpose with or without fee is hereby granted, provided that the above
     10  * copyright notice and this permission notice appear in all copies.
     11  *
     12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     19  */
     20 
     21 /*
     22  * CardBus front-end for the Ralink RT2560/RT2561/RT2561S/RT2661 driver.
     23  */
     24 #include <sys/cdefs.h>
     25 __KERNEL_RCSID(0, "$NetBSD: if_ral_cardbus.c,v 1.26 2022/09/25 17:33:19 thorpej Exp $");
     26 
     27 
     28 #include <sys/param.h>
     29 #include <sys/sockio.h>
     30 #include <sys/mbuf.h>
     31 #include <sys/kernel.h>
     32 #include <sys/socket.h>
     33 #include <sys/systm.h>
     34 #include <sys/callout.h>
     35 #include <sys/device.h>
     36 
     37 #include <sys/bus.h>
     38 #include <sys/intr.h>
     39 
     40 #include <net/if.h>
     41 #include <net/if_dl.h>
     42 #include <net/if_media.h>
     43 #include <net/if_ether.h>
     44 
     45 #include <netinet/in.h>
     46 
     47 #include <net80211/ieee80211_var.h>
     48 #include <net80211/ieee80211_amrr.h>
     49 #include <net80211/ieee80211_rssadapt.h>
     50 #include <net80211/ieee80211_radiotap.h>
     51 
     52 #include <dev/ic/rt2560var.h>
     53 #include <dev/ic/rt2661var.h>
     54 
     55 #include <dev/pci/pcireg.h>
     56 #include <dev/pci/pcivar.h>
     57 #include <dev/pci/pcidevs.h>
     58 
     59 #include <dev/cardbus/cardbusvar.h>
     60 
     61 static struct ral_opns {
     62 	int	(*attach)(void *, int);
     63 	int	(*detach)(void *);
     64 	int	(*intr)(void *);
     65 
     66 }  ral_rt2560_opns = {
     67 	rt2560_attach,
     68 	rt2560_detach,
     69 	rt2560_intr
     70 
     71 }, ral_rt2661_opns = {
     72 	rt2661_attach,
     73 	rt2661_detach,
     74 	rt2661_intr
     75 };
     76 
     77 struct ral_cardbus_softc {
     78 	union {
     79 		struct rt2560_softc	sc_rt2560;
     80 		struct rt2661_softc	sc_rt2661;
     81 	} u;
     82 #define sc_sc	u.sc_rt2560
     83 
     84 	/* cardbus specific goo */
     85 	struct ral_opns		*sc_opns;
     86 	cardbus_devfunc_t	sc_ct;
     87 	pcitag_t		sc_tag;
     88 	void			*sc_ih;
     89 	bus_size_t		sc_mapsize;
     90 	pcireg_t		sc_bar_val;
     91 };
     92 
     93 int	ral_cardbus_match(device_t, cfdata_t, void *);
     94 void	ral_cardbus_attach(device_t, device_t, void *);
     95 int	ral_cardbus_detach(device_t, int);
     96 
     97 CFATTACH_DECL_NEW(ral_cardbus, sizeof (struct ral_cardbus_softc),
     98     ral_cardbus_match, ral_cardbus_attach, ral_cardbus_detach, NULL);
     99 
    100 int	ral_cardbus_enable(struct rt2560_softc *);
    101 void	ral_cardbus_disable(struct rt2560_softc *);
    102 void	ral_cardbus_power(struct rt2560_softc *, int);
    103 void	ral_cardbus_setup(struct ral_cardbus_softc *);
    104 
    105 int
    106 ral_cardbus_match(device_t parent, cfdata_t cfdata, void *aux)
    107 {
    108 	struct cardbus_attach_args *ca = aux;
    109 
    110 	if (PCI_VENDOR(ca->ca_id) == PCI_VENDOR_RALINK) {
    111 		switch (PCI_PRODUCT(ca->ca_id)) {
    112 		case PCI_PRODUCT_RALINK_RT2560:
    113 		case PCI_PRODUCT_RALINK_RT2561:
    114 		case PCI_PRODUCT_RALINK_RT2561S:
    115 		case PCI_PRODUCT_RALINK_RT2661:
    116 			return 1;
    117 		default:
    118 			return 0;
    119 		}
    120 	}
    121 
    122 	return 0;
    123 }
    124 
    125 void
    126 ral_cardbus_attach(device_t parent, device_t self, void *aux)
    127 {
    128 	struct ral_cardbus_softc *csc = device_private(self);
    129 	struct rt2560_softc *sc = &csc->sc_sc;
    130 	struct cardbus_attach_args *ca = aux;
    131 	cardbus_devfunc_t ct = ca->ca_ct;
    132 	char devinfo[256];
    133 	bus_addr_t base;
    134 	int error, revision;
    135 
    136 	pci_devinfo(ca->ca_id, ca->ca_class, 0, devinfo, sizeof(devinfo));
    137 	revision = PCI_REVISION(ca->ca_class);
    138 	aprint_normal(": %s (rev. 0x%02x)\n", devinfo, revision);
    139 
    140 	csc->sc_opns =
    141 	    (PCI_PRODUCT(ca->ca_id) == PCI_PRODUCT_RALINK_RT2560) ?
    142 	    &ral_rt2560_opns : &ral_rt2661_opns;
    143 
    144 	sc->sc_dev = self;
    145 	sc->sc_dmat = ca->ca_dmat;
    146 	csc->sc_ct = ct;
    147 	csc->sc_tag = ca->ca_tag;
    148 
    149 	/* power management hooks */
    150 	sc->sc_enable = ral_cardbus_enable;
    151 	sc->sc_disable = ral_cardbus_disable;
    152 
    153 	/* map control/status registers */
    154 	error = Cardbus_mapreg_map(ct, PCI_BAR0,
    155 	    PCI_MAPREG_TYPE_MEM, 0, &sc->sc_st, &sc->sc_sh, &base,
    156 	    &csc->sc_mapsize);
    157 	if (error != 0) {
    158 		aprint_error(": could not map memory space\n");
    159 		return;
    160 	}
    161 
    162 	csc->sc_bar_val = base | PCI_MAPREG_TYPE_MEM;
    163 
    164 	/* set up the PCI configuration registers */
    165 	ral_cardbus_setup(csc);
    166 
    167 	(*csc->sc_opns->attach)(sc, PCI_PRODUCT(ca->ca_id));
    168 
    169 	Cardbus_function_disable(ct);
    170 }
    171 
    172 int
    173 ral_cardbus_detach(device_t self, int flags)
    174 {
    175 	struct ral_cardbus_softc *csc = device_private(self);
    176 	struct rt2560_softc *sc = &csc->sc_sc;
    177 	cardbus_devfunc_t ct = csc->sc_ct;
    178 	int error;
    179 
    180 	error = (*csc->sc_opns->detach)(sc);
    181 	if (error != 0)
    182 		return error;
    183 
    184 	/* unhook the interrupt handler */
    185 	if (csc->sc_ih != NULL) {
    186 		Cardbus_intr_disestablish(ct, csc->sc_ih);
    187 		csc->sc_ih = NULL;
    188 	}
    189 
    190 	/* release bus space and close window */
    191 	Cardbus_mapreg_unmap(ct, PCI_BAR0, sc->sc_st, sc->sc_sh,
    192 	    csc->sc_mapsize);
    193 
    194 	return 0;
    195 }
    196 
    197 int
    198 ral_cardbus_enable(struct rt2560_softc *sc)
    199 {
    200 	struct ral_cardbus_softc *csc = (struct ral_cardbus_softc *)sc;
    201 	cardbus_devfunc_t ct = csc->sc_ct;
    202 
    203 	/* power on the socket */
    204 	Cardbus_function_enable(ct);
    205 
    206 	/* setup the PCI configuration registers */
    207 	ral_cardbus_setup(csc);
    208 
    209 	/* map and establish the interrupt handler */
    210 	csc->sc_ih = Cardbus_intr_establish(ct, IPL_NET,
    211 	    csc->sc_opns->intr, sc);
    212 	if (csc->sc_ih == NULL) {
    213 		aprint_error_dev(sc->sc_dev,
    214 				 "could not establish interrupt\n");
    215 		Cardbus_function_disable(ct);
    216 		return 1;
    217 	}
    218 
    219 	return 0;
    220 }
    221 
    222 void
    223 ral_cardbus_disable(struct rt2560_softc *sc)
    224 {
    225 	struct ral_cardbus_softc *csc = (struct ral_cardbus_softc *)sc;
    226 	cardbus_devfunc_t ct = csc->sc_ct;
    227 
    228 	/* unhook the interrupt handler */
    229 	Cardbus_intr_disestablish(ct, csc->sc_ih);
    230 	csc->sc_ih = NULL;
    231 
    232 	/* power down the socket */
    233 	Cardbus_function_disable(ct);
    234 }
    235 
    236 void
    237 ral_cardbus_setup(struct ral_cardbus_softc *csc)
    238 {
    239 	cardbus_devfunc_t ct = csc->sc_ct;
    240 	pcireg_t reg;
    241 
    242 	/* program the BAR */
    243 	Cardbus_conf_write(ct, csc->sc_tag, PCI_BAR0, csc->sc_bar_val);
    244 
    245 	/* enable the appropriate bits in the PCI CSR */
    246 	reg = Cardbus_conf_read(ct, csc->sc_tag, PCI_COMMAND_STATUS_REG);
    247 	reg |= PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_MEM_ENABLE;
    248 	Cardbus_conf_write(ct, csc->sc_tag, PCI_COMMAND_STATUS_REG, reg);
    249 }
    250