Home | History | Annotate | Line # | Download | only in cardbus
if_ath_cardbus.c revision 1.41
      1  1.41    dyoung /*	$NetBSD: if_ath_cardbus.c,v 1.41 2010/03/02 20:31:14 dyoung Exp $ */
      2   1.1    ichiro /*
      3   1.1    ichiro  * Copyright (c) 2003
      4   1.1    ichiro  *	Ichiro FUKUHARA <ichiro (at) ichiro.org>.
      5   1.1    ichiro  * All rights reserved.
      6   1.1    ichiro  *
      7   1.1    ichiro  * Redistribution and use in source and binary forms, with or without
      8   1.1    ichiro  * modification, are permitted provided that the following conditions
      9   1.1    ichiro  * are met:
     10   1.1    ichiro  * 1. Redistributions of source code must retain the above copyright
     11   1.1    ichiro  *    notice, this list of conditions and the following disclaimer.
     12   1.1    ichiro  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1    ichiro  *    notice, this list of conditions and the following disclaimer in the
     14   1.1    ichiro  *    documentation and/or other materials provided with the distribution.
     15   1.1    ichiro  *
     16   1.1    ichiro  * THIS SOFTWARE IS PROVIDED BY ICHIRO FUKUHARA ``AS IS'' AND ANY EXPRESS OR
     17   1.1    ichiro  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18   1.1    ichiro  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19   1.1    ichiro  * IN NO EVENT SHALL ICHIRO FUKUHARA OR THE VOICES IN HIS HEAD BE LIABLE FOR
     20   1.1    ichiro  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21   1.1    ichiro  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22   1.1    ichiro  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23   1.1    ichiro  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24   1.1    ichiro  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25   1.1    ichiro  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26   1.1    ichiro  * SUCH DAMAGE.
     27   1.1    ichiro  */
     28   1.1    ichiro /*
     29   1.1    ichiro  * CardBus bus front-end for the AR5001 Wireless LAN 802.11a/b/g CardBus.
     30   1.1    ichiro  */
     31   1.1    ichiro 
     32   1.1    ichiro #include <sys/cdefs.h>
     33  1.41    dyoung __KERNEL_RCSID(0, "$NetBSD: if_ath_cardbus.c,v 1.41 2010/03/02 20:31:14 dyoung Exp $");
     34   1.1    ichiro 
     35   1.1    ichiro #include "opt_inet.h"
     36   1.1    ichiro 
     37   1.1    ichiro #include <sys/param.h>
     38   1.6     perry #include <sys/systm.h>
     39   1.6     perry #include <sys/mbuf.h>
     40   1.1    ichiro #include <sys/malloc.h>
     41   1.1    ichiro #include <sys/kernel.h>
     42   1.1    ichiro #include <sys/socket.h>
     43   1.1    ichiro #include <sys/ioctl.h>
     44   1.1    ichiro #include <sys/errno.h>
     45   1.1    ichiro #include <sys/device.h>
     46   1.1    ichiro 
     47   1.1    ichiro #include <machine/endian.h>
     48   1.6     perry 
     49   1.1    ichiro #include <net/if.h>
     50   1.1    ichiro #include <net/if_dl.h>
     51   1.1    ichiro #include <net/if_media.h>
     52   1.1    ichiro #include <net/if_ether.h>
     53   1.1    ichiro 
     54   1.7    dyoung #include <net80211/ieee80211_netbsd.h>
     55   1.1    ichiro #include <net80211/ieee80211_var.h>
     56   1.1    ichiro 
     57   1.1    ichiro #ifdef INET
     58   1.6     perry #include <netinet/in.h>
     59   1.1    ichiro #include <netinet/if_inarp.h>
     60   1.1    ichiro #endif
     61   1.1    ichiro 
     62   1.1    ichiro 
     63  1.19        ad #include <sys/bus.h>
     64  1.19        ad #include <sys/intr.h>
     65   1.1    ichiro 
     66   1.1    ichiro #include <dev/mii/miivar.h>
     67   1.1    ichiro #include <dev/mii/mii_bitbang.h>
     68   1.1    ichiro 
     69   1.7    dyoung #include <dev/ic/ath_netbsd.h>
     70   1.1    ichiro #include <dev/ic/athvar.h>
     71  1.32       alc 
     72  1.32       alc #include <external/isc/atheros_hal/dist/ah.h>
     73   1.1    ichiro 
     74   1.1    ichiro #include <dev/pci/pcivar.h>
     75   1.1    ichiro #include <dev/pci/pcireg.h>
     76   1.1    ichiro #include <dev/pci/pcidevs.h>
     77   1.1    ichiro 
     78   1.1    ichiro #include <dev/cardbus/cardbusvar.h>
     79   1.4   mycroft #include <dev/pci/pcidevs.h>
     80   1.1    ichiro 
     81   1.1    ichiro /*
     82   1.1    ichiro  * PCI configuration space registers
     83   1.1    ichiro  */
     84   1.1    ichiro #define	ATH_PCI_MMBA		0x10	/* memory mapped base */
     85   1.1    ichiro 
     86   1.1    ichiro struct ath_cardbus_softc {
     87   1.1    ichiro 	struct ath_softc	sc_ath;
     88   1.1    ichiro 
     89   1.1    ichiro 	/* CardBus-specific goo. */
     90   1.1    ichiro 	void	*sc_ih;			/* interrupt handle */
     91   1.1    ichiro 	cardbus_devfunc_t sc_ct;	/* our CardBus devfuncs */
     92  1.39    dyoung 	pcitag_t sc_tag;		/* our CardBus tag */
     93   1.1    ichiro 	bus_size_t sc_mapsize;		/* the size of mapped bus space region */
     94   1.1    ichiro 
     95   1.1    ichiro 	pcireg_t sc_bar_val;		/* value of the BAR */
     96   1.1    ichiro 
     97  1.29  drochner 	cardbus_intr_line_t sc_intrline; /* interrupt line */
     98  1.13   gdamore 	bus_space_tag_t sc_iot;
     99  1.13   gdamore 	bus_space_handle_t sc_ioh;
    100   1.1    ichiro };
    101   1.1    ichiro 
    102  1.30     joerg int	ath_cardbus_match(device_t, cfdata_t, void *);
    103  1.26    dyoung void	ath_cardbus_attach(device_t, device_t, void *);
    104  1.26    dyoung int	ath_cardbus_detach(device_t, int);
    105   1.1    ichiro 
    106  1.30     joerg CFATTACH_DECL_NEW(ath_cardbus, sizeof(struct ath_cardbus_softc),
    107  1.26    dyoung     ath_cardbus_match, ath_cardbus_attach, ath_cardbus_detach, NULL);
    108   1.1    ichiro 
    109   1.1    ichiro void	ath_cardbus_setup(struct ath_cardbus_softc *);
    110   1.1    ichiro 
    111  1.26    dyoung static bool
    112  1.39    dyoung ath_cardbus_suspend(device_t self, const pmf_qual_t *qual)
    113  1.26    dyoung {
    114  1.26    dyoung 	struct ath_cardbus_softc *csc = device_private(self);
    115  1.26    dyoung 
    116  1.26    dyoung 	ath_suspend(&csc->sc_ath);
    117  1.26    dyoung 	if (csc->sc_ih != NULL) {
    118  1.41    dyoung 		Cardbus_intr_disestablish(csc->sc_ct, csc->sc_ih);
    119  1.26    dyoung 		csc->sc_ih = NULL;
    120  1.26    dyoung 	}
    121  1.26    dyoung 	return true;
    122  1.26    dyoung }
    123  1.21  jmcneill 
    124  1.21  jmcneill static bool
    125  1.39    dyoung ath_cardbus_resume(device_t self, const pmf_qual_t *qual)
    126  1.21  jmcneill {
    127  1.26    dyoung 	struct ath_cardbus_softc *csc = device_private(self);
    128  1.21  jmcneill 
    129  1.41    dyoung 	csc->sc_ih = Cardbus_intr_establish(csc->sc_ct,
    130  1.41    dyoung 	    csc->sc_intrline, IPL_NET, ath_intr,
    131  1.26    dyoung 	    &csc->sc_ath);
    132  1.26    dyoung 
    133  1.26    dyoung 	if (csc->sc_ih == NULL) {
    134  1.26    dyoung 		aprint_error_dev(self,
    135  1.29  drochner 		    "unable to establish interrupt\n");
    136  1.26    dyoung 		return false;
    137  1.26    dyoung 	}
    138  1.26    dyoung 
    139  1.26    dyoung 	return ath_resume(&csc->sc_ath);
    140  1.21  jmcneill }
    141   1.1    ichiro 
    142   1.1    ichiro int
    143  1.33    cegger ath_cardbus_match(device_t parent, cfdata_t match, void *aux)
    144   1.1    ichiro {
    145   1.1    ichiro 	struct cardbus_attach_args *ca = aux;
    146  1.26    dyoung 	const char *devname;
    147   1.1    ichiro 
    148  1.26    dyoung 	devname = ath_hal_probe(PCI_VENDOR(ca->ca_id), PCI_PRODUCT(ca->ca_id));
    149   1.1    ichiro 
    150   1.1    ichiro 	if (devname)
    151  1.26    dyoung 		return 1;
    152   1.1    ichiro 
    153  1.26    dyoung 	return 0;
    154   1.1    ichiro }
    155   1.1    ichiro 
    156   1.1    ichiro void
    157  1.26    dyoung ath_cardbus_attach(device_t parent, device_t self, void *aux)
    158   1.1    ichiro {
    159  1.10   thorpej 	struct ath_cardbus_softc *csc = device_private(self);
    160   1.1    ichiro 	struct ath_softc *sc = &csc->sc_ath;
    161   1.1    ichiro 	struct cardbus_attach_args *ca = aux;
    162   1.1    ichiro 	cardbus_devfunc_t ct = ca->ca_ct;
    163   1.1    ichiro 	bus_addr_t adr;
    164   1.1    ichiro 
    165  1.30     joerg 	sc->sc_dev = self;
    166   1.1    ichiro 	sc->sc_dmat = ca->ca_dmat;
    167   1.1    ichiro 	csc->sc_ct = ct;
    168   1.1    ichiro 	csc->sc_tag = ca->ca_tag;
    169   1.1    ichiro 
    170  1.26    dyoung 	aprint_normal("\n");
    171  1.15     seanb 
    172   1.1    ichiro 	/*
    173   1.1    ichiro 	 * Map the device.
    174   1.1    ichiro 	 */
    175  1.26    dyoung 	if (Cardbus_mapreg_map(ct, ATH_PCI_MMBA, PCI_MAPREG_TYPE_MEM, 0,
    176  1.13   gdamore 	    &csc->sc_iot, &csc->sc_ioh, &adr, &csc->sc_mapsize) == 0) {
    177   1.1    ichiro #if rbus
    178   1.1    ichiro #else
    179   1.1    ichiro 		(*ct->ct_cf->cardbus_mem_open)(cc, 0, adr, adr+csc->sc_mapsize);
    180   1.1    ichiro #endif
    181  1.26    dyoung 		csc->sc_bar_val = adr | PCI_MAPREG_TYPE_MEM;
    182  1.26    dyoung 	} else {
    183  1.26    dyoung 		aprint_error_dev(self, "unable to map device registers\n");
    184   1.1    ichiro 		return;
    185   1.1    ichiro 	}
    186   1.1    ichiro 
    187  1.13   gdamore 	sc->sc_st = HALTAG(csc->sc_iot);
    188  1.13   gdamore 	sc->sc_sh = HALHANDLE(csc->sc_ioh);
    189  1.13   gdamore 
    190   1.1    ichiro 	/*
    191   1.1    ichiro 	 * Set up the PCI configuration registers.
    192   1.1    ichiro 	 */
    193   1.1    ichiro 	ath_cardbus_setup(csc);
    194   1.1    ichiro 
    195   1.1    ichiro 	/* Remember which interrupt line. */
    196   1.1    ichiro 	csc->sc_intrline = ca->ca_intrline;
    197   1.1    ichiro 
    198  1.24    dyoung 	ATH_LOCK_INIT(sc);
    199  1.24    dyoung 
    200   1.1    ichiro 	/*
    201   1.1    ichiro 	 * Finish off the attach.
    202   1.1    ichiro 	 */
    203  1.26    dyoung 	if (ath_attach(PCI_PRODUCT(ca->ca_id), sc) != 0)
    204  1.26    dyoung 		return;
    205  1.26    dyoung 
    206  1.34   tsutsui 	if (pmf_device_register(self,
    207  1.34   tsutsui 	    ath_cardbus_suspend, ath_cardbus_resume)) {
    208  1.26    dyoung 		pmf_class_network_register(self, &sc->sc_if);
    209  1.35    dyoung 		pmf_device_suspend(self, &sc->sc_qual);
    210  1.34   tsutsui 	} else
    211  1.34   tsutsui 		aprint_error_dev(self, "couldn't establish power handler\n");
    212   1.1    ichiro }
    213   1.1    ichiro 
    214   1.1    ichiro int
    215  1.26    dyoung ath_cardbus_detach(device_t self, int flags)
    216   1.1    ichiro {
    217  1.10   thorpej 	struct ath_cardbus_softc *csc = device_private(self);
    218   1.1    ichiro 	struct ath_softc *sc = &csc->sc_ath;
    219   1.1    ichiro 	struct cardbus_devfunc *ct = csc->sc_ct;
    220   1.1    ichiro 	int rv;
    221   1.1    ichiro 
    222   1.1    ichiro #if defined(DIAGNOSTIC)
    223   1.1    ichiro 	if (ct == NULL)
    224  1.31    cegger 		panic("%s: data structure lacks", device_xname(sc->sc_dev));
    225   1.1    ichiro #endif
    226   1.1    ichiro 
    227   1.1    ichiro 	rv = ath_detach(sc);
    228   1.1    ichiro 	if (rv)
    229   1.1    ichiro 		return (rv);
    230   1.1    ichiro 
    231  1.21  jmcneill 	pmf_device_deregister(self);
    232  1.21  jmcneill 
    233   1.1    ichiro 	/*
    234   1.1    ichiro 	 * Unhook the interrupt handler.
    235   1.1    ichiro 	 */
    236  1.25    dyoung 	if (csc->sc_ih != NULL) {
    237  1.41    dyoung 		Cardbus_intr_disestablish(ct, csc->sc_ih);
    238   1.1    ichiro 		csc->sc_ih = NULL;
    239  1.25    dyoung 	}
    240   1.1    ichiro 
    241   1.1    ichiro 	/*
    242   1.1    ichiro 	 * Release bus space and close window.
    243   1.1    ichiro 	 */
    244  1.14  nakayama 	Cardbus_mapreg_unmap(ct, ATH_PCI_MMBA, csc->sc_iot, csc->sc_ioh,
    245  1.14  nakayama 	    csc->sc_mapsize);
    246   1.1    ichiro 
    247  1.24    dyoung 	ATH_LOCK_DESTROY(sc);
    248  1.24    dyoung 
    249   1.1    ichiro 	return (0);
    250   1.1    ichiro }
    251   1.1    ichiro 
    252   1.1    ichiro void
    253   1.1    ichiro ath_cardbus_setup(struct ath_cardbus_softc *csc)
    254   1.1    ichiro {
    255   1.1    ichiro 	cardbus_devfunc_t ct = csc->sc_ct;
    256  1.26    dyoung 	int rc;
    257   1.1    ichiro 	pcireg_t reg;
    258   1.1    ichiro 
    259  1.26    dyoung 	if ((rc = cardbus_set_powerstate(ct, csc->sc_tag, PCI_PWR_D0)) != 0)
    260  1.26    dyoung 		aprint_debug("%s: cardbus_set_powerstate %d\n", __func__, rc);
    261   1.1    ichiro 
    262   1.3   mycroft 	/* Program the BAR. */
    263  1.40    dyoung 	Cardbus_conf_write(ct, csc->sc_tag, ATH_PCI_MMBA, csc->sc_bar_val);
    264   1.1    ichiro 
    265   1.1    ichiro 	/* Enable the appropriate bits in the PCI CSR. */
    266  1.40    dyoung 	reg = Cardbus_conf_read(ct, csc->sc_tag,
    267  1.26    dyoung 	    PCI_COMMAND_STATUS_REG);
    268  1.26    dyoung 	reg |= PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_MEM_ENABLE;
    269  1.40    dyoung 	Cardbus_conf_write(ct, csc->sc_tag, PCI_COMMAND_STATUS_REG, reg);
    270   1.1    ichiro }
    271