Home | History | Annotate | Line # | Download | only in pci
if_wi_pci.c revision 1.2.2.2
      1  1.2.2.2  thorpej /*      $NetBSD: if_wi_pci.c,v 1.2.2.2 2002/01/10 19:56:47 thorpej Exp $  */
      2  1.2.2.2  thorpej 
      3  1.2.2.2  thorpej /*-
      4  1.2.2.2  thorpej  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5  1.2.2.2  thorpej  * All rights reserved.
      6  1.2.2.2  thorpej  *
      7  1.2.2.2  thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.2.2.2  thorpej  * by Hideaki Imaizumi <hiddy (at) sfc.wide.ad.jp>
      9  1.2.2.2  thorpej  * and Ichiro FUKUHARA (ichiro (at) ichiro.org).
     10  1.2.2.2  thorpej  *
     11  1.2.2.2  thorpej  * Redistribution and use in source and binary forms, with or without
     12  1.2.2.2  thorpej  * modification, are permitted provided that the following conditions
     13  1.2.2.2  thorpej  * are met:
     14  1.2.2.2  thorpej  * 1. Redistributions of source code must retain the above copyright
     15  1.2.2.2  thorpej  *    notice, this list of conditions and the following disclaimer.
     16  1.2.2.2  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.2.2.2  thorpej  *    notice, this list of conditions and the following disclaimer in the
     18  1.2.2.2  thorpej  *    documentation and/or other materials provided with the distribution.
     19  1.2.2.2  thorpej  * 3. All advertising materials mentioning features or use of this software
     20  1.2.2.2  thorpej  *    must display the following acknowledgement:
     21  1.2.2.2  thorpej  *        This product includes software developed by the NetBSD
     22  1.2.2.2  thorpej  *        Foundation, Inc. and its contributors.
     23  1.2.2.2  thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.2.2.2  thorpej  *    contributors may be used to endorse or promote products derived
     25  1.2.2.2  thorpej  *    from this software without specific prior written permission.
     26  1.2.2.2  thorpej  *
     27  1.2.2.2  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.2.2.2  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.2.2.2  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.2.2.2  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.2.2.2  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.2.2.2  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.2.2.2  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.2.2.2  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.2.2.2  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.2.2.2  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.2.2.2  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     38  1.2.2.2  thorpej  */
     39  1.2.2.2  thorpej 
     40  1.2.2.2  thorpej /*
     41  1.2.2.2  thorpej  * PCI bus front-end for the Intersil PCI WaveLan.
     42  1.2.2.2  thorpej  * Works with Prism2.5 Mini-PCI wavelan.
     43  1.2.2.2  thorpej  */
     44  1.2.2.2  thorpej 
     45  1.2.2.2  thorpej #include <sys/cdefs.h>
     46  1.2.2.2  thorpej __KERNEL_RCSID(0, "$NetBSD: if_wi_pci.c,v 1.2.2.2 2002/01/10 19:56:47 thorpej Exp $");
     47  1.2.2.2  thorpej 
     48  1.2.2.2  thorpej #include <sys/param.h>
     49  1.2.2.2  thorpej #include <sys/systm.h>
     50  1.2.2.2  thorpej #include <sys/mbuf.h>
     51  1.2.2.2  thorpej #include <sys/syslog.h>
     52  1.2.2.2  thorpej #include <sys/socket.h>
     53  1.2.2.2  thorpej #include <sys/device.h>
     54  1.2.2.2  thorpej #include <sys/callout.h>
     55  1.2.2.2  thorpej 
     56  1.2.2.2  thorpej #include <net/if.h>
     57  1.2.2.2  thorpej #include <net/if_ether.h>
     58  1.2.2.2  thorpej #include <net/if_media.h>
     59  1.2.2.2  thorpej #include <net/if_ieee80211.h>
     60  1.2.2.2  thorpej 
     61  1.2.2.2  thorpej #include <machine/bus.h>
     62  1.2.2.2  thorpej #include <machine/intr.h>
     63  1.2.2.2  thorpej 
     64  1.2.2.2  thorpej #include <dev/pci/pcireg.h>
     65  1.2.2.2  thorpej #include <dev/pci/pcivar.h>
     66  1.2.2.2  thorpej #include <dev/pci/pcidevs.h>
     67  1.2.2.2  thorpej 
     68  1.2.2.2  thorpej #include <dev/ic/wi_ieee.h>
     69  1.2.2.2  thorpej #include <dev/ic/wireg.h>
     70  1.2.2.2  thorpej #include <dev/ic/wivar.h>
     71  1.2.2.2  thorpej 
     72  1.2.2.2  thorpej struct wi_pci_softc {
     73  1.2.2.2  thorpej 	struct wi_softc psc_wi;		/* real "wi" softc */
     74  1.2.2.2  thorpej 
     75  1.2.2.2  thorpej 	/* PCI-specific goo */
     76  1.2.2.2  thorpej 	pci_intr_handle_t psc_ih;
     77  1.2.2.2  thorpej 	struct pci_attach_args *psc_pa;
     78  1.2.2.2  thorpej 
     79  1.2.2.2  thorpej 	void *sc_powerhook;		/* power hook descriptor */
     80  1.2.2.2  thorpej };
     81  1.2.2.2  thorpej 
     82  1.2.2.2  thorpej static int	wi_pci_match __P((struct device *, struct cfdata *, void *));
     83  1.2.2.2  thorpej static void	wi_pci_attach __P((struct device *, struct device *, void *));
     84  1.2.2.2  thorpej static int	wi_pci_enable __P((struct wi_softc *));
     85  1.2.2.2  thorpej static void	wi_pci_disable __P((struct wi_softc *));
     86  1.2.2.2  thorpej static void	wi_pci_powerhook __P((int, void *));
     87  1.2.2.2  thorpej 
     88  1.2.2.2  thorpej static const struct wi_pci_product
     89  1.2.2.2  thorpej 	*wi_pci_lookup __P((struct pci_attach_args *));
     90  1.2.2.2  thorpej 
     91  1.2.2.2  thorpej struct cfattach wi_pci_ca = {
     92  1.2.2.2  thorpej 	sizeof(struct wi_pci_softc), wi_pci_match, wi_pci_attach
     93  1.2.2.2  thorpej };
     94  1.2.2.2  thorpej 
     95  1.2.2.2  thorpej const struct wi_pci_product {
     96  1.2.2.2  thorpej 	pci_vendor_id_t		wpp_vendor;	/* vendor ID */
     97  1.2.2.2  thorpej 	pci_product_id_t	wpp_product;	/* product ID */
     98  1.2.2.2  thorpej 	const char		*wpp_name;	/* product name */
     99  1.2.2.2  thorpej } wi_pci_products[] = {
    100  1.2.2.2  thorpej 	{ PCI_VENDOR_INTERSIL,		PCI_PRODUCT_INTERSIL_MINI_PCI_WLAN,
    101  1.2.2.2  thorpej 	  "Intersil Prism2.5" },
    102  1.2.2.2  thorpej 	{ 0,				0,
    103  1.2.2.2  thorpej 	  NULL},
    104  1.2.2.2  thorpej };
    105  1.2.2.2  thorpej 
    106  1.2.2.2  thorpej static int
    107  1.2.2.2  thorpej wi_pci_enable(sc)
    108  1.2.2.2  thorpej 	struct wi_softc *sc;
    109  1.2.2.2  thorpej {
    110  1.2.2.2  thorpej 	struct wi_pci_softc *psc = (struct wi_pci_softc *)sc;
    111  1.2.2.2  thorpej 
    112  1.2.2.2  thorpej 	/* establish the interrupt. */
    113  1.2.2.2  thorpej 	sc->sc_ih = pci_intr_establish(psc->psc_pa->pa_pc,
    114  1.2.2.2  thorpej 					psc->psc_ih, IPL_NET, wi_intr, sc);
    115  1.2.2.2  thorpej 	if (sc->sc_ih == NULL) {
    116  1.2.2.2  thorpej 		printf("%s: couldn't establish interrupt\n",
    117  1.2.2.2  thorpej 		    sc->sc_dev.dv_xname);
    118  1.2.2.2  thorpej 		return (EIO);
    119  1.2.2.2  thorpej 	}
    120  1.2.2.2  thorpej 	sc->sc_pci = 1;
    121  1.2.2.2  thorpej 
    122  1.2.2.2  thorpej 	/* reset HFA3842 MAC core */
    123  1.2.2.2  thorpej 	wi_pci_reset(sc);
    124  1.2.2.2  thorpej 
    125  1.2.2.2  thorpej 	return (0);
    126  1.2.2.2  thorpej }
    127  1.2.2.2  thorpej 
    128  1.2.2.2  thorpej static void
    129  1.2.2.2  thorpej wi_pci_disable(sc)
    130  1.2.2.2  thorpej 	struct wi_softc *sc;
    131  1.2.2.2  thorpej {
    132  1.2.2.2  thorpej 	struct wi_pci_softc *psc = (struct wi_pci_softc *)sc;
    133  1.2.2.2  thorpej 
    134  1.2.2.2  thorpej 	pci_intr_disestablish(psc->psc_pa->pa_pc, sc->sc_ih);
    135  1.2.2.2  thorpej }
    136  1.2.2.2  thorpej 
    137  1.2.2.2  thorpej static const struct wi_pci_product *
    138  1.2.2.2  thorpej wi_pci_lookup(pa)
    139  1.2.2.2  thorpej 	struct pci_attach_args *pa;
    140  1.2.2.2  thorpej {
    141  1.2.2.2  thorpej 	const struct wi_pci_product *wpp;
    142  1.2.2.2  thorpej 
    143  1.2.2.2  thorpej 	for (wpp = wi_pci_products; wpp->wpp_name != NULL; wpp++) {
    144  1.2.2.2  thorpej 		if (PCI_VENDOR(pa->pa_id) == wpp->wpp_vendor &&
    145  1.2.2.2  thorpej 		    PCI_PRODUCT(pa->pa_id) == wpp->wpp_product)
    146  1.2.2.2  thorpej 			return (wpp);
    147  1.2.2.2  thorpej 	}
    148  1.2.2.2  thorpej 	return (NULL);
    149  1.2.2.2  thorpej }
    150  1.2.2.2  thorpej 
    151  1.2.2.2  thorpej static int
    152  1.2.2.2  thorpej wi_pci_match(parent, match, aux)
    153  1.2.2.2  thorpej 	struct device *parent;
    154  1.2.2.2  thorpej 	struct cfdata *match;
    155  1.2.2.2  thorpej 	void *aux;
    156  1.2.2.2  thorpej {
    157  1.2.2.2  thorpej 	struct pci_attach_args *pa = aux;
    158  1.2.2.2  thorpej 
    159  1.2.2.2  thorpej 	if (wi_pci_lookup(pa) != NULL)
    160  1.2.2.2  thorpej 		return (1);
    161  1.2.2.2  thorpej 	return (0);
    162  1.2.2.2  thorpej }
    163  1.2.2.2  thorpej 
    164  1.2.2.2  thorpej static void
    165  1.2.2.2  thorpej wi_pci_attach(parent, self, aux)
    166  1.2.2.2  thorpej 	struct device *parent, *self;
    167  1.2.2.2  thorpej 	void *aux;
    168  1.2.2.2  thorpej {
    169  1.2.2.2  thorpej 	struct wi_pci_softc *psc = (struct wi_pci_softc *)self;
    170  1.2.2.2  thorpej 	struct wi_softc *sc = &psc->psc_wi;
    171  1.2.2.2  thorpej 	struct pci_attach_args *pa = aux;
    172  1.2.2.2  thorpej 	pci_chipset_tag_t pc = pa->pa_pc;
    173  1.2.2.2  thorpej 	const char *intrstr;
    174  1.2.2.2  thorpej 	const struct wi_pci_product *wpp;
    175  1.2.2.2  thorpej 	pci_intr_handle_t ih;
    176  1.2.2.2  thorpej 	bus_space_tag_t memt;
    177  1.2.2.2  thorpej 	bus_space_handle_t memh;
    178  1.2.2.2  thorpej 	int memh_valid;
    179  1.2.2.2  thorpej 	bus_addr_t busbase;
    180  1.2.2.2  thorpej 	bus_size_t bussize;
    181  1.2.2.2  thorpej 
    182  1.2.2.2  thorpej 	psc->psc_pa = pa;
    183  1.2.2.2  thorpej 
    184  1.2.2.2  thorpej 	memh_valid = !pci_mapreg_map(pa, WI_PCI_CBMA,
    185  1.2.2.2  thorpej 		PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0,
    186  1.2.2.2  thorpej 	    	    		   &memt, &memh, &busbase, &bussize);
    187  1.2.2.2  thorpej 
    188  1.2.2.2  thorpej 	if(memh_valid){
    189  1.2.2.2  thorpej 		sc->sc_iot = memt;
    190  1.2.2.2  thorpej 		sc->sc_ioh = memh;
    191  1.2.2.2  thorpej 	}
    192  1.2.2.2  thorpej 	else {
    193  1.2.2.2  thorpej 		printf(": unable to map device registers\n");
    194  1.2.2.2  thorpej 		return;
    195  1.2.2.2  thorpej 	}
    196  1.2.2.2  thorpej 
    197  1.2.2.2  thorpej 	wpp = wi_pci_lookup(pa);
    198  1.2.2.2  thorpej 	if (wpp == NULL) {
    199  1.2.2.2  thorpej 		printf("\n");
    200  1.2.2.2  thorpej 		panic("wi_pci_attach: impossible");
    201  1.2.2.2  thorpej 	}
    202  1.2.2.2  thorpej 
    203  1.2.2.2  thorpej 	printf(": %s Wireless Lan\n", wpp->wpp_name);
    204  1.2.2.2  thorpej 
    205  1.2.2.2  thorpej 	sc->sc_pci = 1;
    206  1.2.2.2  thorpej 	sc->sc_enabled = 1;
    207  1.2.2.2  thorpej 	sc->sc_enable = wi_pci_enable;
    208  1.2.2.2  thorpej 	sc->sc_disable = wi_pci_disable;
    209  1.2.2.2  thorpej 
    210  1.2.2.2  thorpej 	/* Enable the card. */
    211  1.2.2.2  thorpej 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    212  1.2.2.2  thorpej 		pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
    213  1.2.2.2  thorpej 		PCI_COMMAND_MASTER_ENABLE);
    214  1.2.2.2  thorpej 
    215  1.2.2.2  thorpej 
    216  1.2.2.2  thorpej 	/* Map and establish the interrupt. */
    217  1.2.2.2  thorpej 	if (pci_intr_map(pa, &ih)) {
    218  1.2.2.2  thorpej 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
    219  1.2.2.2  thorpej 		return;
    220  1.2.2.2  thorpej 	}
    221  1.2.2.2  thorpej 	intrstr = pci_intr_string(pc, ih);
    222  1.2.2.2  thorpej 
    223  1.2.2.2  thorpej 	psc->psc_ih = ih;
    224  1.2.2.2  thorpej 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, wi_intr, sc);
    225  1.2.2.2  thorpej 	if (sc->sc_ih == NULL) {
    226  1.2.2.2  thorpej 		printf("%s: couldn't establish interrupt",
    227  1.2.2.2  thorpej 		    sc->sc_dev.dv_xname);
    228  1.2.2.2  thorpej 		if (intrstr != NULL)
    229  1.2.2.2  thorpej 			printf(" at %s", intrstr);
    230  1.2.2.2  thorpej 		printf("\n");
    231  1.2.2.2  thorpej 		return;
    232  1.2.2.2  thorpej 	}
    233  1.2.2.2  thorpej 
    234  1.2.2.2  thorpej 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    235  1.2.2.2  thorpej 
    236  1.2.2.2  thorpej 	/* reset HFA3842 MAC core */
    237  1.2.2.2  thorpej 	wi_pci_reset(sc);
    238  1.2.2.2  thorpej 
    239  1.2.2.2  thorpej 	sc->sc_ifp = &sc->sc_ethercom.ec_if;
    240  1.2.2.2  thorpej 	if (wi_attach(sc) != 0) {
    241  1.2.2.2  thorpej 		printf("%s: failed to attach controller\n",
    242  1.2.2.2  thorpej 		    sc->sc_dev.dv_xname);
    243  1.2.2.2  thorpej 		pci_intr_disestablish(pa->pa_pc, sc->sc_ih);
    244  1.2.2.2  thorpej 		return;
    245  1.2.2.2  thorpej 	}
    246  1.2.2.2  thorpej 
    247  1.2.2.2  thorpej 	/* Add a suspend hook to restore PCI config state */
    248  1.2.2.2  thorpej 	psc->sc_powerhook = powerhook_establish(wi_pci_powerhook, psc);
    249  1.2.2.2  thorpej 	if (psc->sc_powerhook == NULL)
    250  1.2.2.2  thorpej 		printf ("%s: WARNING: unable to establish pci power hook\n",
    251  1.2.2.2  thorpej 		        sc->sc_dev.dv_xname);
    252  1.2.2.2  thorpej }
    253  1.2.2.2  thorpej 
    254  1.2.2.2  thorpej static void
    255  1.2.2.2  thorpej wi_pci_powerhook(why, arg)
    256  1.2.2.2  thorpej 	int why;
    257  1.2.2.2  thorpej 	void *arg;
    258  1.2.2.2  thorpej {
    259  1.2.2.2  thorpej 	struct wi_pci_softc *psc = arg;
    260  1.2.2.2  thorpej 	struct wi_softc *sc = &psc->psc_wi;
    261  1.2.2.2  thorpej 
    262  1.2.2.2  thorpej 	wi_power(sc, why);
    263  1.2.2.2  thorpej }
    264