Home | History | Annotate | Line # | Download | only in pci
if_rtw_pci.c revision 1.18
      1  1.18    dyoung /*	$NetBSD: if_rtw_pci.c,v 1.18 2010/03/04 22:57:37 dyoung Exp $	*/
      2   1.1    dyoung 
      3   1.1    dyoung /*-
      4   1.1    dyoung  * Copyright (c) 1998, 1999, 2000, 2002 The NetBSD Foundation, Inc.
      5   1.1    dyoung  * All rights reserved.
      6   1.1    dyoung  *
      7   1.1    dyoung  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1    dyoung  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9   1.1    dyoung  * NASA Ames Research Center; Charles M. Hannum; and David Young.
     10   1.1    dyoung  *
     11   1.1    dyoung  * Redistribution and use in source and binary forms, with or without
     12   1.1    dyoung  * modification, are permitted provided that the following conditions
     13   1.1    dyoung  * are met:
     14   1.1    dyoung  * 1. Redistributions of source code must retain the above copyright
     15   1.1    dyoung  *    notice, this list of conditions and the following disclaimer.
     16   1.1    dyoung  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1    dyoung  *    notice, this list of conditions and the following disclaimer in the
     18   1.1    dyoung  *    documentation and/or other materials provided with the distribution.
     19   1.1    dyoung  *
     20   1.1    dyoung  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21   1.1    dyoung  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22   1.1    dyoung  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23   1.1    dyoung  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24   1.1    dyoung  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25   1.1    dyoung  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26   1.1    dyoung  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   1.1    dyoung  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28   1.1    dyoung  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29   1.1    dyoung  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30   1.1    dyoung  * POSSIBILITY OF SUCH DAMAGE.
     31   1.1    dyoung  */
     32   1.1    dyoung 
     33   1.1    dyoung /*
     34   1.1    dyoung  * PCI bus front-end for the Realtek RTL8180 802.11 MAC/BBP chip.
     35   1.1    dyoung  *
     36   1.1    dyoung  * Derived from the ADMtek ADM8211 PCI bus front-end.
     37   1.1    dyoung  *
     38   1.1    dyoung  * Derived from the ``Tulip'' PCI bus front-end.
     39   1.1    dyoung  */
     40   1.1    dyoung 
     41   1.1    dyoung #include <sys/cdefs.h>
     42  1.18    dyoung __KERNEL_RCSID(0, "$NetBSD: if_rtw_pci.c,v 1.18 2010/03/04 22:57:37 dyoung Exp $");
     43   1.1    dyoung 
     44   1.1    dyoung #include <sys/param.h>
     45   1.2     perry #include <sys/systm.h>
     46   1.2     perry #include <sys/mbuf.h>
     47   1.1    dyoung #include <sys/malloc.h>
     48   1.1    dyoung #include <sys/kernel.h>
     49   1.1    dyoung #include <sys/socket.h>
     50   1.1    dyoung #include <sys/ioctl.h>
     51   1.1    dyoung #include <sys/errno.h>
     52   1.1    dyoung #include <sys/device.h>
     53   1.1    dyoung 
     54   1.1    dyoung #include <machine/endian.h>
     55   1.2     perry 
     56   1.1    dyoung #include <net/if.h>
     57   1.1    dyoung #include <net/if_dl.h>
     58   1.1    dyoung #include <net/if_media.h>
     59   1.1    dyoung #include <net/if_ether.h>
     60   1.1    dyoung 
     61   1.3    dyoung #include <net80211/ieee80211_netbsd.h>
     62   1.1    dyoung #include <net80211/ieee80211_radiotap.h>
     63   1.1    dyoung #include <net80211/ieee80211_var.h>
     64   1.1    dyoung 
     65   1.8        ad #include <sys/bus.h>
     66   1.8        ad #include <sys/intr.h>
     67   1.1    dyoung 
     68   1.1    dyoung #include <dev/ic/rtwreg.h>
     69   1.1    dyoung #include <dev/ic/rtwvar.h>
     70   1.1    dyoung 
     71   1.1    dyoung #include <dev/pci/pcivar.h>
     72   1.1    dyoung #include <dev/pci/pcireg.h>
     73   1.1    dyoung #include <dev/pci/pcidevs.h>
     74   1.1    dyoung 
     75   1.1    dyoung /*
     76  1.18    dyoung  * PCI configuration space registers used by the RTL8180.
     77   1.1    dyoung  */
     78   1.1    dyoung #define	RTW_PCI_IOBA		0x10	/* i/o mapped base */
     79   1.1    dyoung #define	RTW_PCI_MMBA		0x14	/* memory mapped base */
     80   1.1    dyoung 
     81   1.1    dyoung struct rtw_pci_softc {
     82  1.18    dyoung 	struct rtw_softc	psc_rtw;	/* real RTL8180 softc */
     83   1.1    dyoung 
     84   1.1    dyoung 	pci_intr_handle_t	psc_ih;		/* interrupt handle */
     85   1.1    dyoung 	void			*psc_intrcookie;
     86   1.1    dyoung 
     87   1.1    dyoung 	pci_chipset_tag_t	psc_pc;		/* our PCI chipset */
     88   1.1    dyoung 	pcitag_t		psc_pcitag;	/* our PCI tag */
     89   1.1    dyoung };
     90   1.1    dyoung 
     91  1.13    cegger static int	rtw_pci_match(device_t, cfdata_t, void *);
     92   1.9    dyoung static void	rtw_pci_attach(device_t, device_t, void *);
     93  1.10    dyoung static int	rtw_pci_detach(device_t, int);
     94   1.1    dyoung 
     95   1.9    dyoung CFATTACH_DECL_NEW(rtw_pci, sizeof(struct rtw_pci_softc),
     96  1.10    dyoung     rtw_pci_match, rtw_pci_attach, rtw_pci_detach, NULL);
     97   1.1    dyoung 
     98  1.18    dyoung static bool rtw_pci_resume(device_t, const pmf_qual_t *);
     99  1.18    dyoung static bool rtw_pci_suspend(device_t, const pmf_qual_t *);
    100  1.18    dyoung 
    101   1.1    dyoung static const struct rtw_pci_product {
    102   1.1    dyoung 	u_int32_t	app_vendor;	/* PCI vendor ID */
    103   1.1    dyoung 	u_int32_t	app_product;	/* PCI product ID */
    104   1.1    dyoung 	const char	*app_product_name;
    105   1.1    dyoung } rtw_pci_products[] = {
    106   1.1    dyoung 	{ PCI_VENDOR_REALTEK,		PCI_PRODUCT_REALTEK_RT8180,
    107   1.1    dyoung 	  "Realtek RTL8180 802.11 MAC/BBP" },
    108   1.4  christos 	{ PCI_VENDOR_BELKIN,		PCI_PRODUCT_BELKIN_F5D6001,
    109   1.4  christos 	  "Belkin F5D6001" },
    110   1.1    dyoung 
    111   1.1    dyoung 	{ 0,				0,				NULL },
    112   1.1    dyoung };
    113   1.1    dyoung 
    114   1.1    dyoung static const struct rtw_pci_product *
    115   1.1    dyoung rtw_pci_lookup(const struct pci_attach_args *pa)
    116   1.1    dyoung {
    117   1.1    dyoung 	const struct rtw_pci_product *app;
    118   1.1    dyoung 
    119   1.1    dyoung 	for (app = rtw_pci_products;
    120   1.1    dyoung 	     app->app_product_name != NULL;
    121   1.1    dyoung 	     app++) {
    122   1.1    dyoung 		if (PCI_VENDOR(pa->pa_id) == app->app_vendor &&
    123   1.1    dyoung 		    PCI_PRODUCT(pa->pa_id) == app->app_product)
    124   1.1    dyoung 			return (app);
    125   1.1    dyoung 	}
    126   1.1    dyoung 	return (NULL);
    127   1.1    dyoung }
    128   1.1    dyoung 
    129   1.1    dyoung static int
    130  1.13    cegger rtw_pci_match(device_t parent, cfdata_t match, void *aux)
    131   1.1    dyoung {
    132   1.1    dyoung 	struct pci_attach_args *pa = aux;
    133   1.1    dyoung 
    134   1.1    dyoung 	if (rtw_pci_lookup(pa) != NULL)
    135   1.1    dyoung 		return (1);
    136   1.1    dyoung 
    137   1.1    dyoung 	return (0);
    138   1.1    dyoung }
    139   1.1    dyoung 
    140   1.1    dyoung static void
    141   1.9    dyoung rtw_pci_attach(device_t parent, device_t self, void *aux)
    142   1.1    dyoung {
    143   1.9    dyoung 	struct rtw_pci_softc *psc = device_private(self);
    144   1.1    dyoung 	struct rtw_softc *sc = &psc->psc_rtw;
    145   1.1    dyoung 	struct rtw_regs *regs = &sc->sc_regs;
    146   1.1    dyoung 	struct pci_attach_args *pa = aux;
    147   1.1    dyoung 	pci_chipset_tag_t pc = pa->pa_pc;
    148   1.1    dyoung 	const char *intrstr = NULL;
    149   1.1    dyoung 	const struct rtw_pci_product *app;
    150   1.6  christos 	int error;
    151   1.1    dyoung 
    152   1.9    dyoung 	sc->sc_dev = self;
    153   1.1    dyoung 	psc->psc_pc = pa->pa_pc;
    154   1.1    dyoung 	psc->psc_pcitag = pa->pa_tag;
    155   1.1    dyoung 
    156   1.1    dyoung 	app = rtw_pci_lookup(pa);
    157   1.1    dyoung 	if (app == NULL) {
    158   1.1    dyoung 		printf("\n");
    159   1.1    dyoung 		panic("rtw_pci_attach: impossible");
    160   1.1    dyoung 	}
    161   1.1    dyoung 
    162   1.1    dyoung 	/*
    163   1.1    dyoung 	 * Get revision info, and set some chip-specific variables.
    164   1.1    dyoung 	 */
    165   1.1    dyoung 	sc->sc_rev = PCI_REVISION(pa->pa_class);
    166   1.5    rpaulo 	aprint_normal(": %s, revision %d.%d\n", app->app_product_name,
    167   1.1    dyoung 	    (sc->sc_rev >> 4) & 0xf, sc->sc_rev & 0xf);
    168   1.1    dyoung 
    169   1.6  christos 	/* power up chip */
    170  1.11    dyoung 	if ((error = pci_activate(pa->pa_pc, pa->pa_tag, self, NULL)) != 0 &&
    171  1.10    dyoung 	    error != EOPNOTSUPP) {
    172   1.9    dyoung 		aprint_error_dev(self, "cannot activate %d\n", error);
    173   1.6  christos 		return;
    174   1.1    dyoung 	}
    175   1.1    dyoung 
    176   1.1    dyoung 	/*
    177   1.1    dyoung 	 * Map the device.
    178   1.1    dyoung 	 */
    179  1.10    dyoung 	if (pci_mapreg_map(pa, RTW_PCI_MMBA,
    180   1.1    dyoung 	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
    181  1.10    dyoung 	    &regs->r_bt, &regs->r_bh, NULL, &regs->r_sz) == 0)
    182  1.10    dyoung 		;
    183  1.10    dyoung 	else if (pci_mapreg_map(pa, RTW_PCI_IOBA, PCI_MAPREG_TYPE_IO, 0,
    184  1.10    dyoung 	    &regs->r_bt, &regs->r_bh, NULL, &regs->r_sz) == 0)
    185  1.10    dyoung 		;
    186  1.10    dyoung 	else {
    187  1.10    dyoung 		aprint_error_dev(self, "unable to map device registers\n");
    188   1.1    dyoung 		return;
    189   1.1    dyoung 	}
    190   1.1    dyoung 
    191   1.1    dyoung 	sc->sc_dmat = pa->pa_dmat;
    192   1.1    dyoung 
    193   1.1    dyoung 	/*
    194   1.1    dyoung 	 * Make sure bus mastering is enabled.
    195   1.1    dyoung 	 */
    196   1.1    dyoung 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    197   1.1    dyoung 	    pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
    198   1.1    dyoung 	    PCI_COMMAND_MASTER_ENABLE);
    199   1.1    dyoung 
    200   1.1    dyoung 	/*
    201   1.1    dyoung 	 * Map and establish our interrupt.
    202   1.1    dyoung 	 */
    203   1.1    dyoung 	if (pci_intr_map(pa, &psc->psc_ih)) {
    204   1.9    dyoung 		aprint_error_dev(self, "unable to map interrupt\n");
    205   1.1    dyoung 		return;
    206   1.1    dyoung 	}
    207   1.2     perry 	intrstr = pci_intr_string(pc, psc->psc_ih);
    208   1.1    dyoung 	psc->psc_intrcookie = pci_intr_establish(pc, psc->psc_ih, IPL_NET,
    209   1.1    dyoung 	    rtw_intr, sc);
    210   1.1    dyoung 	if (psc->psc_intrcookie == NULL) {
    211   1.9    dyoung 		aprint_error_dev(self, "unable to establish interrupt");
    212   1.1    dyoung 		if (intrstr != NULL)
    213   1.5    rpaulo 			aprint_error(" at %s", intrstr);
    214   1.9    dyoung 		aprint_error("\n");
    215   1.1    dyoung 		return;
    216   1.1    dyoung 	}
    217   1.1    dyoung 
    218   1.9    dyoung 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
    219   1.1    dyoung 
    220   1.1    dyoung 	/*
    221   1.1    dyoung 	 * Finish off the attach.
    222   1.1    dyoung 	 */
    223   1.1    dyoung 	rtw_attach(sc);
    224  1.10    dyoung 
    225  1.14   tsutsui 	if (pmf_device_register(sc->sc_dev, rtw_pci_suspend, rtw_pci_resume)) {
    226  1.10    dyoung 		pmf_class_network_register(self, &sc->sc_if);
    227  1.10    dyoung 		/*
    228  1.10    dyoung 		 * Power down the socket.
    229  1.10    dyoung 		 */
    230  1.15    dyoung 		pmf_device_suspend(sc->sc_dev, &sc->sc_qual);
    231  1.14   tsutsui 	} else
    232  1.14   tsutsui 		aprint_error_dev(sc->sc_dev,
    233  1.14   tsutsui 		    "couldn't establish power handler\n");
    234  1.10    dyoung }
    235  1.10    dyoung 
    236  1.10    dyoung static int
    237  1.10    dyoung rtw_pci_detach(device_t self, int flags)
    238  1.10    dyoung {
    239  1.10    dyoung 	struct rtw_pci_softc *psc = device_private(self);
    240  1.10    dyoung 	struct rtw_softc *sc = &psc->psc_rtw;
    241  1.10    dyoung 	struct rtw_regs *regs = &sc->sc_regs;
    242  1.10    dyoung 	int rc;
    243  1.10    dyoung 
    244  1.10    dyoung 	if ((rc = rtw_detach(sc)) != 0)
    245  1.10    dyoung 		return rc;
    246  1.10    dyoung 	if (psc->psc_intrcookie != NULL)
    247  1.10    dyoung 		pci_intr_disestablish(psc->psc_pc, psc->psc_intrcookie);
    248  1.10    dyoung 	bus_space_unmap(regs->r_bt, regs->r_bh, regs->r_sz);
    249  1.10    dyoung 
    250  1.10    dyoung 	return 0;
    251   1.1    dyoung }
    252  1.18    dyoung 
    253  1.18    dyoung static bool
    254  1.18    dyoung rtw_pci_resume(device_t self, const pmf_qual_t *qual)
    255  1.18    dyoung {
    256  1.18    dyoung 	struct rtw_pci_softc *psc = device_private(self);
    257  1.18    dyoung 	struct rtw_softc *sc = &psc->psc_rtw;
    258  1.18    dyoung 
    259  1.18    dyoung 	/* Establish the interrupt. */
    260  1.18    dyoung 	psc->psc_intrcookie = pci_intr_establish(psc->psc_pc, psc->psc_ih,
    261  1.18    dyoung 	    IPL_NET, rtw_intr, sc);
    262  1.18    dyoung 	if (psc->psc_intrcookie == NULL) {
    263  1.18    dyoung 		aprint_error_dev(sc->sc_dev, "unable to establish interrupt\n");
    264  1.18    dyoung 		return false;
    265  1.18    dyoung 	}
    266  1.18    dyoung 
    267  1.18    dyoung 	return rtw_resume(self, qual);
    268  1.18    dyoung }
    269  1.18    dyoung 
    270  1.18    dyoung static bool
    271  1.18    dyoung rtw_pci_suspend(device_t self, const pmf_qual_t *qual)
    272  1.18    dyoung {
    273  1.18    dyoung 	struct rtw_pci_softc *psc = device_private(self);
    274  1.18    dyoung 
    275  1.18    dyoung 	if (!rtw_suspend(self, qual))
    276  1.18    dyoung 		return false;
    277  1.18    dyoung 
    278  1.18    dyoung 	/* Unhook the interrupt handler. */
    279  1.18    dyoung 	pci_intr_disestablish(psc->psc_pc, psc->psc_intrcookie);
    280  1.18    dyoung 	psc->psc_intrcookie = NULL;
    281  1.18    dyoung 	return true;
    282  1.18    dyoung }
    283