Home | History | Annotate | Line # | Download | only in pci
if_wi_pci.c revision 1.1.2.6
      1 /*      $NetBSD: if_wi_pci.c,v 1.1.2.6 2002/09/23 15:42:20 thorpej Exp $  */
      2 
      3 /*-
      4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Hideaki Imaizumi <hiddy (at) sfc.wide.ad.jp>
      9  * and Ichiro FUKUHARA (ichiro (at) ichiro.org).
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *        This product includes software developed by the NetBSD
     22  *        Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * PCI bus front-end for the Intersil PCI WaveLan.
     42  * Works with Prism2.5 Mini-PCI wavelan.
     43  */
     44 
     45 #include <sys/cdefs.h>
     46 <<<<<<< if_wi_pci.c
     47 __KERNEL_RCSID(0, "$NetBSD: if_wi_pci.c,v 1.1.2.6 2002/09/23 15:42:20 thorpej Exp $");
     48 =======
     49 __KERNEL_RCSID(0, "$NetBSD: if_wi_pci.c,v 1.1.2.6 2002/09/23 15:42:20 thorpej Exp $");
     50 >>>>>>> 1.8
     51 
     52 #include <sys/param.h>
     53 #include <sys/systm.h>
     54 #include <sys/mbuf.h>
     55 #include <sys/syslog.h>
     56 #include <sys/socket.h>
     57 #include <sys/device.h>
     58 #include <sys/callout.h>
     59 
     60 #include <net/if.h>
     61 #include <net/if_ether.h>
     62 #include <net/if_media.h>
     63 #include <net/if_ieee80211.h>
     64 
     65 #include <machine/bus.h>
     66 #include <machine/intr.h>
     67 
     68 #include <dev/pci/pcireg.h>
     69 #include <dev/pci/pcivar.h>
     70 #include <dev/pci/pcidevs.h>
     71 
     72 #include <dev/ic/wi_ieee.h>
     73 #include <dev/ic/wireg.h>
     74 #include <dev/ic/wivar.h>
     75 
     76 #define WI_PCI_PLX_LOMEM	0x10	/* PLX chip membase */
     77 #define WI_PCI_PLX_LOIO		0x14	/* PLX chip iobase */
     78 #define WI_PCI_LOMEM		0x18	/* ISA membase */
     79 #define WI_PCI_LOIO		0x1C	/* ISA iobase */
     80 
     81 #define WI_PLX_COR_OFFSET       0x3E0
     82 #define WI_PLX_COR_VALUE        0x41
     83 
     84 struct wi_pci_softc {
     85 	struct wi_softc psc_wi;		/* real "wi" softc */
     86 
     87 	/* PCI-specific goo */
     88 	pci_intr_handle_t psc_ih;
     89 	struct pci_attach_args *psc_pa;
     90 
     91 	void *sc_powerhook;		/* power hook descriptor */
     92 };
     93 
     94 static int	wi_pci_match __P((struct device *, struct cfdata *, void *));
     95 static void	wi_pci_attach __P((struct device *, struct device *, void *));
     96 static int	wi_pci_enable __P((struct wi_softc *));
     97 static void	wi_pci_disable __P((struct wi_softc *));
     98 static void	wi_pci_reset __P((struct wi_softc *));
     99 static void	wi_pci_powerhook __P((int, void *));
    100 
    101 static const struct wi_pci_product
    102 	*wi_pci_lookup __P((struct pci_attach_args *));
    103 
    104 struct cfattach wi_pci_ca = {
    105 	sizeof(struct wi_pci_softc), wi_pci_match, wi_pci_attach
    106 };
    107 
    108 const struct wi_pci_product {
    109 	pci_vendor_id_t		wpp_vendor;	/* vendor ID */
    110 	pci_product_id_t	wpp_product;	/* product ID */
    111 	const char		*wpp_name;	/* product name */
    112 	int			wpp_plx;	/* uses PLX chip */
    113 } wi_pci_products[] = {
    114 	{ PCI_VENDOR_GLOBALSUN,		PCI_PRODUCT_GLOBALSUN_GL24110P,
    115 	  NULL, 1 },
    116 	{ PCI_VENDOR_GLOBALSUN,		PCI_PRODUCT_GLOBALSUN_GL24110P02,
    117 	  NULL, 1 },
    118 	{ PCI_VENDOR_EUMITCOM,		PCI_PRODUCT_EUMITCOM_WL11000P,
    119 	  NULL, 1 },
    120 	{ PCI_VENDOR_3COM,		PCI_PRODUCT_3COM_3CRWE777A,
    121 	  NULL, 1 },
    122 	{ PCI_VENDOR_NETGEAR,		PCI_PRODUCT_NETGEAR_MA301,
    123 	  NULL, 1 },
    124 	{ PCI_VENDOR_INTERSIL,		PCI_PRODUCT_INTERSIL_MINI_PCI_WLAN,
    125 	  "Intersil Prism2.5", 0 },
    126 	{ 0,				0,
    127 	  NULL, 0},
    128 };
    129 
    130 static int
    131 wi_pci_enable(sc)
    132 	struct wi_softc *sc;
    133 {
    134 	struct wi_pci_softc *psc = (struct wi_pci_softc *)sc;
    135 
    136 	/* establish the interrupt. */
    137 	sc->sc_ih = pci_intr_establish(psc->psc_pa->pa_pc,
    138 					psc->psc_ih, IPL_NET, wi_intr, sc);
    139 	if (sc->sc_ih == NULL) {
    140 		printf("%s: couldn't establish interrupt\n",
    141 		    sc->sc_dev.dv_xname);
    142 		return (EIO);
    143 	}
    144 
    145 	/* reset HFA3842 MAC core */
    146 	wi_pci_reset(sc);
    147 
    148 	return (0);
    149 }
    150 
    151 static void
    152 wi_pci_disable(sc)
    153 	struct wi_softc *sc;
    154 {
    155 	struct wi_pci_softc *psc = (struct wi_pci_softc *)sc;
    156 
    157 	pci_intr_disestablish(psc->psc_pa->pa_pc, sc->sc_ih);
    158 }
    159 
    160 static void
    161 wi_pci_reset(sc)
    162 	struct wi_softc		*sc;
    163 {
    164 	int i, secs, usecs;
    165 
    166 	CSR_WRITE_2(sc, WI_PCI_COR_OFFSET, WI_COR_SOFT_RESET);
    167 	DELAY(250*1000); /* 1/4 second */
    168 
    169 	CSR_WRITE_2(sc, WI_PCI_COR_OFFSET, WI_COR_CLEAR);
    170 	DELAY(500*1000); /* 1/2 second */
    171 
    172 	/* wait 2 seconds for firmware to complete initialization. */
    173 
    174 	for (i = 200000; i--; DELAY(10))
    175 		if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY))
    176 			break;
    177 
    178 	if (i < 0) {
    179 		printf("%s: PCI reset timed out\n", sc->sc_dev.dv_xname);
    180 	} else if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG) {
    181 		usecs = (200000 - i) * 10;
    182 		secs = usecs / 1000000;
    183 		usecs %= 1000000;
    184 
    185 		printf("%s: PCI reset in %d.%06d seconds\n",
    186                        sc->sc_dev.dv_xname, secs, usecs);
    187 	}
    188 
    189 	return;
    190 }
    191 
    192 static const struct wi_pci_product *
    193 wi_pci_lookup(pa)
    194 	struct pci_attach_args *pa;
    195 {
    196 	const struct wi_pci_product *wpp;
    197 
    198 	for (wpp = wi_pci_products; wpp->wpp_vendor != 0; wpp++) {
    199 		if (PCI_VENDOR(pa->pa_id) == wpp->wpp_vendor &&
    200 		    PCI_PRODUCT(pa->pa_id) == wpp->wpp_product)
    201 			return (wpp);
    202 	}
    203 	return (NULL);
    204 }
    205 
    206 static int
    207 wi_pci_match(parent, match, aux)
    208 	struct device *parent;
    209 	struct cfdata *match;
    210 	void *aux;
    211 {
    212 	struct pci_attach_args *pa = aux;
    213 
    214 	if (wi_pci_lookup(pa) != NULL)
    215 		return (1);
    216 	return (0);
    217 }
    218 
    219 static void
    220 wi_pci_attach(parent, self, aux)
    221 	struct device *parent, *self;
    222 	void *aux;
    223 {
    224 	struct wi_pci_softc *psc = (struct wi_pci_softc *)self;
    225 	struct wi_softc *sc = &psc->psc_wi;
    226 	struct pci_attach_args *pa = aux;
    227 	pci_chipset_tag_t pc = pa->pa_pc;
    228 	const char *intrstr;
    229 	const struct wi_pci_product *wpp;
    230 	pci_intr_handle_t ih;
    231 	bus_space_tag_t memt, iot;
    232 	bus_space_handle_t memh, ioh;
    233 
    234 	psc->psc_pa = pa;
    235 
    236 	wpp = wi_pci_lookup(pa);
    237 #ifdef DIAGNOSTIC
    238 	if (wpp == NULL) {
    239 		printf("\n");
    240 		panic("wi_pci_attach: impossible");
    241 	}
    242 #endif
    243 
    244 	if (wpp->wpp_plx) {
    245 		/* Map memory and I/O registers. */
    246 		if (pci_mapreg_map(pa, WI_PCI_LOMEM, PCI_MAPREG_TYPE_MEM, 0,
    247 		    &memt, &memh, NULL, NULL) != 0) {
    248 			printf(": can't map mem space\n");
    249 			return;
    250 		}
    251 		if (pci_mapreg_map(pa, WI_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0,
    252 		    &iot, &ioh, NULL, NULL) != 0) {
    253 			printf(": can't map I/O space\n");
    254 			return;
    255 		}
    256 	} else {
    257 		if (pci_mapreg_map(pa, WI_PCI_CBMA,
    258 		    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
    259 		    0, &iot, &ioh, NULL, NULL) != 0) {
    260 			printf(": can't map mem space\n");
    261 			return;
    262 		}
    263 
    264 		memt = iot;
    265 		memh = ioh;
    266 		sc->sc_pci = 1;
    267 	}
    268 
    269 	if (wpp->wpp_name != NULL) {
    270 		printf(": %s Wireless Lan\n", wpp->wpp_name);
    271 	} else {
    272 		char devinfo[256];
    273 
    274 		pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
    275 		printf(": %s (rev. 0x%02x)\n", devinfo,
    276 		       PCI_REVISION(pa->pa_class));
    277 	}
    278 
    279 	sc->sc_enabled = 1;
    280 	sc->sc_enable = wi_pci_enable;
    281 	sc->sc_disable = wi_pci_disable;
    282 
    283 	/* Enable the card. */
    284 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    285 		pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
    286 		PCI_COMMAND_MASTER_ENABLE);
    287 
    288 	sc->sc_iot = iot;
    289 	sc->sc_ioh = ioh;
    290 	/* Make sure interrupts are disabled. */
    291 	CSR_WRITE_2(sc, WI_INT_EN, 0);
    292 	CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
    293 
    294 	/* Map and establish the interrupt. */
    295 	if (pci_intr_map(pa, &ih)) {
    296 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
    297 		return;
    298 	}
    299 	intrstr = pci_intr_string(pc, ih);
    300 
    301 	psc->psc_ih = ih;
    302 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, wi_intr, sc);
    303 	if (sc->sc_ih == NULL) {
    304 		printf("%s: couldn't establish interrupt",
    305 		    sc->sc_dev.dv_xname);
    306 		if (intrstr != NULL)
    307 			printf(" at %s", intrstr);
    308 		printf("\n");
    309 		return;
    310 	}
    311 
    312 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    313 
    314 	if (wpp->wpp_plx) {
    315 		/*
    316 		 * Setup the PLX chip for level interrupts and config index 1
    317 		 * XXX - should really reset the PLX chip too.
    318 		 */
    319 		bus_space_write_1(memt, memh,
    320 		    WI_PLX_COR_OFFSET, WI_PLX_COR_VALUE);
    321 	} else {
    322 		/* reset HFA3842 MAC core */
    323 		wi_pci_reset(sc);
    324 	}
    325 
    326 	printf("%s:", sc->sc_dev.dv_xname);
    327 	sc->sc_ifp = &sc->sc_ethercom.ec_if;
    328 	if (wi_attach(sc) != 0) {
    329 		printf("%s: failed to attach controller\n",
    330 			sc->sc_dev.dv_xname);
    331 		pci_intr_disestablish(pa->pa_pc, sc->sc_ih);
    332 		return;
    333 	}
    334 
    335 	/* Add a suspend hook to restore PCI config state */
    336 	psc->sc_powerhook = powerhook_establish(wi_pci_powerhook, psc);
    337 	if (psc->sc_powerhook == NULL)
    338 		printf ("%s: WARNING: unable to establish pci power hook\n",
    339 		        sc->sc_dev.dv_xname);
    340 }
    341 
    342 static void
    343 wi_pci_powerhook(why, arg)
    344 	int why;
    345 	void *arg;
    346 {
    347 	struct wi_pci_softc *psc = arg;
    348 	struct wi_softc *sc = &psc->psc_wi;
    349 
    350 	wi_power(sc, why);
    351 }
    352