Home | History | Annotate | Line # | Download | only in cardbus
if_ral_cardbus.c revision 1.3.6.1
      1  1.3.6.1    simonb /*	$NetBSD: if_ral_cardbus.c,v 1.3.6.1 2006/04/22 11:38:51 simonb Exp $ */
      2      1.1  drochner /*	$OpenBSD: if_ral_cardbus.c,v 1.5 2005/05/16 01:36:25 brad Exp $  */
      3      1.1  drochner 
      4      1.1  drochner /*-
      5      1.1  drochner  * Copyright (c) 2005
      6      1.1  drochner  *	Damien Bergamini <damien.bergamini (at) free.fr>
      7      1.1  drochner  *
      8      1.1  drochner  * Permission to use, copy, modify, and distribute this software for any
      9      1.1  drochner  * purpose with or without fee is hereby granted, provided that the above
     10      1.1  drochner  * copyright notice and this permission notice appear in all copies.
     11      1.1  drochner  *
     12      1.1  drochner  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     13      1.1  drochner  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     14      1.1  drochner  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     15      1.1  drochner  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     16      1.1  drochner  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     17      1.1  drochner  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     18      1.1  drochner  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     19      1.1  drochner  */
     20      1.1  drochner 
     21      1.1  drochner /*
     22      1.1  drochner  * CardBus front-end for the Ralink RT2500 driver.
     23      1.1  drochner  */
     24      1.1  drochner 
     25      1.1  drochner #include <sys/cdefs.h>
     26  1.3.6.1    simonb __KERNEL_RCSID(0, "$NetBSD: if_ral_cardbus.c,v 1.3.6.1 2006/04/22 11:38:51 simonb Exp $");
     27      1.1  drochner 
     28      1.1  drochner #include "bpfilter.h"
     29      1.1  drochner 
     30      1.1  drochner #include <sys/param.h>
     31      1.1  drochner #include <sys/sockio.h>
     32      1.1  drochner #include <sys/mbuf.h>
     33      1.1  drochner #include <sys/kernel.h>
     34      1.1  drochner #include <sys/socket.h>
     35      1.1  drochner #include <sys/systm.h>
     36      1.1  drochner #include <sys/malloc.h>
     37      1.1  drochner #include <sys/device.h>
     38      1.1  drochner 
     39      1.1  drochner #include <machine/bus.h>
     40      1.1  drochner #include <machine/intr.h>
     41      1.1  drochner 
     42      1.1  drochner #include <net/if.h>
     43      1.1  drochner #include <net/if_dl.h>
     44      1.1  drochner #include <net/if_ether.h>
     45      1.1  drochner #include <net/if_media.h>
     46      1.1  drochner 
     47      1.1  drochner #include <netinet/in.h>
     48      1.1  drochner 
     49      1.1  drochner #include <net80211/ieee80211_var.h>
     50      1.1  drochner #include <net80211/ieee80211_rssadapt.h>
     51      1.1  drochner #include <net80211/ieee80211_radiotap.h>
     52      1.1  drochner 
     53      1.1  drochner #include <dev/ic/ralvar.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.1  drochner #include <dev/cardbus/cardbusvar.h>
     60      1.1  drochner 
     61      1.1  drochner struct ral_cardbus_softc {
     62      1.1  drochner 	struct ral_softc	sc_sc;
     63      1.1  drochner 
     64      1.1  drochner 	/* cardbus specific goo */
     65      1.1  drochner 	cardbus_devfunc_t	sc_ct;
     66      1.1  drochner 	cardbustag_t		sc_tag;
     67      1.1  drochner 	void			*sc_ih;
     68      1.1  drochner 	bus_size_t		sc_mapsize;
     69      1.1  drochner 	pcireg_t		sc_bar_val;
     70      1.1  drochner 	int			sc_intrline;
     71      1.1  drochner };
     72      1.1  drochner 
     73      1.1  drochner int	ral_cardbus_match(struct device *, struct cfdata *, void *);
     74      1.1  drochner void	ral_cardbus_attach(struct device *, struct device *, void *);
     75      1.1  drochner int	ral_cardbus_detach(struct device *, int);
     76      1.1  drochner 
     77      1.1  drochner CFATTACH_DECL(ral_cardbus, sizeof (struct ral_cardbus_softc),
     78      1.1  drochner     ral_cardbus_match, ral_cardbus_attach, ral_cardbus_detach, NULL);
     79      1.1  drochner 
     80      1.1  drochner int	ral_cardbus_enable(struct ral_softc *);
     81      1.1  drochner void	ral_cardbus_disable(struct ral_softc *);
     82      1.1  drochner void	ral_cardbus_power(struct ral_softc *, int);
     83      1.1  drochner void	ral_cardbus_setup(struct ral_cardbus_softc *);
     84      1.1  drochner 
     85      1.1  drochner int
     86      1.1  drochner ral_cardbus_match(struct device *parent, struct cfdata *match, void *aux)
     87      1.1  drochner {
     88      1.1  drochner 	struct cardbus_attach_args *ca = aux;
     89      1.1  drochner 
     90      1.1  drochner 	if (CARDBUS_VENDOR(ca->ca_id) == PCI_VENDOR_RALINK &&
     91      1.1  drochner 	    CARDBUS_PRODUCT(ca->ca_id) == PCI_PRODUCT_RALINK_RT2560)
     92      1.1  drochner 		return 1;
     93      1.1  drochner 
     94      1.1  drochner 	return 0;
     95      1.1  drochner }
     96      1.1  drochner 
     97      1.1  drochner void
     98      1.1  drochner ral_cardbus_attach(struct device *parent, struct device *self, void *aux)
     99      1.1  drochner {
    100  1.3.6.1    simonb 	struct ral_cardbus_softc *csc = device_private(self);
    101      1.1  drochner 	struct ral_softc *sc = &csc->sc_sc;
    102      1.1  drochner 	struct cardbus_attach_args *ca = aux;
    103      1.1  drochner 	cardbus_devfunc_t ct = ca->ca_ct;
    104      1.1  drochner 	bus_addr_t base;
    105      1.1  drochner 	int error;
    106      1.1  drochner 
    107      1.1  drochner 	sc->sc_dmat = ca->ca_dmat;
    108      1.1  drochner 	csc->sc_ct = ct;
    109      1.1  drochner 	csc->sc_tag = ca->ca_tag;
    110      1.1  drochner 	csc->sc_intrline = ca->ca_intrline;
    111      1.1  drochner 
    112      1.1  drochner 	/* power management hooks */
    113      1.1  drochner 	sc->sc_enable = ral_cardbus_enable;
    114      1.1  drochner 	sc->sc_disable = ral_cardbus_disable;
    115      1.1  drochner 	sc->sc_power = ral_cardbus_power;
    116      1.1  drochner 
    117      1.1  drochner 	/* map control/status registers */
    118      1.1  drochner 	error = Cardbus_mapreg_map(ct, CARDBUS_BASE0_REG,
    119      1.1  drochner 	    CARDBUS_MAPREG_TYPE_MEM, 0, &sc->sc_st, &sc->sc_sh, &base,
    120      1.1  drochner 	    &csc->sc_mapsize);
    121      1.1  drochner 	if (error != 0) {
    122      1.1  drochner 		printf(": could not map memory space\n");
    123      1.1  drochner 		return;
    124      1.1  drochner 	}
    125      1.1  drochner 
    126      1.1  drochner #if rbus
    127      1.1  drochner #else
    128      1.1  drochner 	(*cf->cardbus_mem_open)(cc, 0, base, base + csc->sc_mapsize);
    129      1.1  drochner #endif
    130      1.1  drochner 
    131      1.1  drochner 	csc->sc_bar_val = base | CARDBUS_MAPREG_TYPE_MEM;
    132      1.1  drochner 
    133      1.1  drochner 	/* set up the PCI configuration registers */
    134      1.1  drochner 	ral_cardbus_setup(csc);
    135      1.1  drochner 
    136      1.2  drochner 	printf(": irq %d\n", csc->sc_intrline);
    137      1.1  drochner 
    138      1.1  drochner 	ral_attach(sc);
    139      1.1  drochner 
    140      1.1  drochner 	Cardbus_function_disable(ct);
    141      1.1  drochner }
    142      1.1  drochner 
    143      1.1  drochner int
    144      1.1  drochner ral_cardbus_detach(struct device *self, int flags)
    145      1.1  drochner {
    146  1.3.6.1    simonb 	struct ral_cardbus_softc *csc = device_private(self);
    147      1.1  drochner 	struct ral_softc *sc = &csc->sc_sc;
    148      1.1  drochner 	cardbus_devfunc_t ct = csc->sc_ct;
    149      1.1  drochner 	cardbus_chipset_tag_t cc = ct->ct_cc;
    150      1.1  drochner 	cardbus_function_tag_t cf = ct->ct_cf;
    151      1.1  drochner 	int error;
    152      1.1  drochner 
    153      1.1  drochner 	error = ral_detach(sc);
    154      1.1  drochner 	if (error != 0)
    155      1.1  drochner 		return error;
    156      1.1  drochner 
    157      1.1  drochner 	/* unhook the interrupt handler */
    158      1.1  drochner 	if (csc->sc_ih != NULL) {
    159      1.1  drochner 		cardbus_intr_disestablish(cc, cf, csc->sc_ih);
    160      1.1  drochner 		csc->sc_ih = NULL;
    161      1.1  drochner 	}
    162      1.1  drochner 
    163      1.1  drochner 	/* release bus space and close window */
    164      1.1  drochner 	Cardbus_mapreg_unmap(ct, CARDBUS_BASE0_REG, sc->sc_st, sc->sc_sh,
    165      1.1  drochner 	    csc->sc_mapsize);
    166      1.1  drochner 
    167      1.1  drochner 	return 0;
    168      1.1  drochner }
    169      1.1  drochner 
    170      1.1  drochner int
    171      1.1  drochner ral_cardbus_enable(struct ral_softc *sc)
    172      1.1  drochner {
    173      1.1  drochner 	struct ral_cardbus_softc *csc = (struct ral_cardbus_softc *)sc;
    174      1.1  drochner 	cardbus_devfunc_t ct = csc->sc_ct;
    175      1.1  drochner 	cardbus_chipset_tag_t cc = ct->ct_cc;
    176      1.1  drochner 	cardbus_function_tag_t cf = ct->ct_cf;
    177      1.1  drochner 
    178      1.1  drochner 	/* power on the socket */
    179      1.1  drochner 	Cardbus_function_enable(ct);
    180      1.1  drochner 
    181      1.1  drochner 	/* setup the PCI configuration registers */
    182      1.1  drochner 	ral_cardbus_setup(csc);
    183      1.1  drochner 
    184      1.1  drochner 	/* map and establish the interrupt handler */
    185      1.1  drochner 	csc->sc_ih = cardbus_intr_establish(cc, cf, csc->sc_intrline, IPL_NET,
    186      1.1  drochner 	    ral_intr, sc);
    187      1.1  drochner 	if (csc->sc_ih == NULL) {
    188      1.1  drochner 		printf("%s: could not establish interrupt at %d\n",
    189      1.1  drochner 		    sc->sc_dev.dv_xname, csc->sc_intrline);
    190      1.1  drochner 		Cardbus_function_disable(ct);
    191      1.1  drochner 		return 1;
    192      1.1  drochner 	}
    193      1.1  drochner 
    194      1.1  drochner 	return 0;
    195      1.1  drochner }
    196      1.1  drochner 
    197      1.1  drochner void
    198      1.1  drochner ral_cardbus_disable(struct ral_softc *sc)
    199      1.1  drochner {
    200      1.1  drochner 	struct ral_cardbus_softc *csc = (struct ral_cardbus_softc *)sc;
    201      1.1  drochner 	cardbus_devfunc_t ct = csc->sc_ct;
    202      1.1  drochner 	cardbus_chipset_tag_t cc = ct->ct_cc;
    203      1.1  drochner 	cardbus_function_tag_t cf = ct->ct_cf;
    204      1.1  drochner 
    205      1.1  drochner 	/* unhook the interrupt handler */
    206      1.1  drochner 	cardbus_intr_disestablish(cc, cf, csc->sc_ih);
    207      1.1  drochner 	csc->sc_ih = NULL;
    208      1.1  drochner 
    209      1.1  drochner 	/* power down the socket */
    210      1.1  drochner 	Cardbus_function_disable(ct);
    211      1.1  drochner }
    212      1.1  drochner 
    213      1.1  drochner void
    214      1.1  drochner ral_cardbus_power(struct ral_softc *sc, int why)
    215      1.1  drochner {
    216      1.1  drochner 	struct ral_cardbus_softc *csc = (struct ral_cardbus_softc *)sc;
    217      1.1  drochner 
    218      1.1  drochner 	if (why == PWR_RESUME) {
    219      1.1  drochner 		/* kick the PCI configuration registers */
    220      1.1  drochner 		ral_cardbus_setup(csc);
    221      1.1  drochner 	}
    222      1.1  drochner }
    223      1.1  drochner 
    224      1.1  drochner void
    225      1.1  drochner ral_cardbus_setup(struct ral_cardbus_softc *csc)
    226      1.1  drochner {
    227      1.1  drochner 	cardbus_devfunc_t ct = csc->sc_ct;
    228      1.1  drochner 	cardbus_chipset_tag_t cc = ct->ct_cc;
    229      1.1  drochner 	cardbus_function_tag_t cf = ct->ct_cf;
    230      1.1  drochner 	pcireg_t reg;
    231      1.1  drochner 
    232      1.1  drochner 	/* program the BAR */
    233      1.1  drochner 	cardbus_conf_write(cc, cf, csc->sc_tag, CARDBUS_BASE0_REG,
    234      1.1  drochner 	    csc->sc_bar_val);
    235      1.1  drochner 
    236      1.1  drochner 	/* make sure the right access type is on the cardbus bridge */
    237      1.1  drochner 	(*cf->cardbus_ctrl)(cc, CARDBUS_MEM_ENABLE);
    238      1.1  drochner 	(*cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
    239      1.1  drochner 
    240      1.1  drochner 	/* enable the appropriate bits in the PCI CSR */
    241      1.1  drochner 	reg = cardbus_conf_read(cc, cf, csc->sc_tag,
    242      1.1  drochner 	    CARDBUS_COMMAND_STATUS_REG);
    243      1.1  drochner 	reg |= CARDBUS_COMMAND_MASTER_ENABLE | CARDBUS_COMMAND_MEM_ENABLE;
    244      1.1  drochner 	cardbus_conf_write(cc, cf, csc->sc_tag, CARDBUS_COMMAND_STATUS_REG,
    245      1.1  drochner 	    reg);
    246      1.1  drochner }
    247