Home | History | Annotate | Line # | Download | only in pci
if_wi_pci.c revision 1.22
      1 /*      $NetBSD: if_wi_pci.c,v 1.22 2003/03/29 20:49:04 jdc 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 __KERNEL_RCSID(0, "$NetBSD: if_wi_pci.c,v 1.22 2003/03/29 20:49:04 jdc Exp $");
     47 
     48 #include <sys/param.h>
     49 #include <sys/systm.h>
     50 #include <sys/mbuf.h>
     51 #include <sys/syslog.h>
     52 #include <sys/socket.h>
     53 #include <sys/device.h>
     54 #include <sys/callout.h>
     55 
     56 #include <net/if.h>
     57 #include <net/if_ether.h>
     58 #include <net/if_media.h>
     59 #include <net/if_ieee80211.h>
     60 
     61 #include <machine/bus.h>
     62 #include <machine/intr.h>
     63 
     64 #include <dev/pci/pcireg.h>
     65 #include <dev/pci/pcivar.h>
     66 #include <dev/pci/pcidevs.h>
     67 
     68 #include <dev/ic/wi_ieee.h>
     69 #include <dev/ic/wireg.h>
     70 #include <dev/ic/wivar.h>
     71 
     72 #define WI_PCI_CBMA		0x10	/* Configuration Base Memory Address */
     73 #define WI_PCI_PLX_LOMEM	0x10	/* PLX chip membase */
     74 #define WI_PCI_PLX_LOIO		0x14	/* PLX chip iobase */
     75 #define WI_PCI_LOMEM		0x18	/* ISA membase */
     76 #define WI_PCI_LOIO		0x1C	/* ISA iobase */
     77 
     78 #define WI_PLX_COR_OFFSET       0x3E0
     79 #define WI_PLX_COR_VALUE        0x41
     80 
     81 struct wi_pci_softc {
     82 	struct wi_softc psc_wi;		/* real "wi" softc */
     83 
     84 	/* PCI-specific goo */
     85 	pci_intr_handle_t psc_ih;
     86 	struct pci_attach_args *psc_pa;
     87 
     88 	void *sc_powerhook;		/* power hook descriptor */
     89 };
     90 
     91 static int	wi_pci_match __P((struct device *, struct cfdata *, void *));
     92 static void	wi_pci_attach __P((struct device *, struct device *, void *));
     93 static int	wi_pci_enable __P((struct wi_softc *));
     94 static void	wi_pci_disable __P((struct wi_softc *));
     95 static void	wi_pci_reset __P((struct wi_softc *));
     96 static void	wi_pci_powerhook __P((int, void *));
     97 
     98 static const struct wi_pci_product
     99 	*wi_pci_lookup __P((struct pci_attach_args *));
    100 
    101 CFATTACH_DECL(wi_pci, sizeof(struct wi_pci_softc),
    102     wi_pci_match, wi_pci_attach, NULL, NULL);
    103 
    104 const struct wi_pci_product {
    105 	pci_vendor_id_t		wpp_vendor;	/* vendor ID */
    106 	pci_product_id_t	wpp_product;	/* product ID */
    107 	const char		*wpp_name;	/* product name */
    108 	int			wpp_plx;	/* uses PLX chip */
    109 } wi_pci_products[] = {
    110 	{ PCI_VENDOR_GLOBALSUN,		PCI_PRODUCT_GLOBALSUN_GL24110P,
    111 	  NULL, 1 },
    112 	{ PCI_VENDOR_GLOBALSUN,		PCI_PRODUCT_GLOBALSUN_GL24110P02,
    113 	  NULL, 1 },
    114 	{ PCI_VENDOR_EUMITCOM,		PCI_PRODUCT_EUMITCOM_WL11000P,
    115 	  NULL, 1 },
    116 	{ PCI_VENDOR_3COM,		PCI_PRODUCT_3COM_3CRWE777A,
    117 	  NULL, 1 },
    118 	{ PCI_VENDOR_NETGEAR,		PCI_PRODUCT_NETGEAR_MA301,
    119 	  NULL, 1 },
    120 	{ PCI_VENDOR_INTERSIL,		PCI_PRODUCT_INTERSIL_MINI_PCI_WLAN,
    121 	  "Intersil Prism2.5", 0 },
    122 	{ PCI_VENDOR_NDC,		PCI_PRODUCT_NDC_NCP130,
    123 	  NULL, 1 },
    124 	{ PCI_VENDOR_USR2,		PCI_PRODUCT_USR2_2415,
    125 	  NULL, 1 },
    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 	bus_space_write_2(sc->sc_iot, sc->sc_ioh,
    167 	    WI_PCI_COR, WI_COR_SOFT_RESET);
    168 	DELAY(250*1000); /* 1/4 second */
    169 
    170 	bus_space_write_2(sc->sc_iot, sc->sc_ioh,
    171 	    WI_PCI_COR, WI_COR_CLEAR);
    172 	DELAY(500*1000); /* 1/2 second */
    173 
    174 	/* wait 2 seconds for firmware to complete initialization. */
    175 
    176 	for (i = 200000; i--; DELAY(10))
    177 		if (!(CSR_READ_2(sc, WI_COMMAND) & WI_CMD_BUSY))
    178 			break;
    179 
    180 	if (i < 0) {
    181 		printf("%s: PCI reset timed out\n", sc->sc_dev.dv_xname);
    182 	} else if (sc->sc_if.if_flags & IFF_DEBUG) {
    183 		usecs = (200000 - i) * 10;
    184 		secs = usecs / 1000000;
    185 		usecs %= 1000000;
    186 
    187 		printf("%s: PCI reset in %d.%06d seconds\n",
    188                        sc->sc_dev.dv_xname, secs, usecs);
    189 	}
    190 
    191 	return;
    192 }
    193 
    194 static const struct wi_pci_product *
    195 wi_pci_lookup(pa)
    196 	struct pci_attach_args *pa;
    197 {
    198 	const struct wi_pci_product *wpp;
    199 
    200 	for (wpp = wi_pci_products; wpp->wpp_vendor != 0; wpp++) {
    201 		if (PCI_VENDOR(pa->pa_id) == wpp->wpp_vendor &&
    202 		    PCI_PRODUCT(pa->pa_id) == wpp->wpp_product)
    203 			return (wpp);
    204 	}
    205 	return (NULL);
    206 }
    207 
    208 static int
    209 wi_pci_match(parent, match, aux)
    210 	struct device *parent;
    211 	struct cfdata *match;
    212 	void *aux;
    213 {
    214 	struct pci_attach_args *pa = aux;
    215 
    216 	if (wi_pci_lookup(pa) != NULL)
    217 		return (1);
    218 	return (0);
    219 }
    220 
    221 static void
    222 wi_pci_attach(parent, self, aux)
    223 	struct device *parent, *self;
    224 	void *aux;
    225 {
    226 	struct wi_pci_softc *psc = (struct wi_pci_softc *)self;
    227 	struct wi_softc *sc = &psc->psc_wi;
    228 	struct pci_attach_args *pa = aux;
    229 	pci_chipset_tag_t pc = pa->pa_pc;
    230 	const char *intrstr;
    231 	const struct wi_pci_product *wpp;
    232 	pci_intr_handle_t ih;
    233 	bus_space_tag_t memt, iot, plxt;
    234 	bus_space_handle_t memh, ioh, plxh;
    235 
    236 	psc->psc_pa = pa;
    237 
    238 	wpp = wi_pci_lookup(pa);
    239 #ifdef DIAGNOSTIC
    240 	if (wpp == NULL) {
    241 		printf("\n");
    242 		panic("wi_pci_attach: impossible");
    243 	}
    244 #endif
    245 
    246 	if (wpp->wpp_plx) {
    247 		/* Map memory and I/O registers. */
    248 		if (pci_mapreg_map(pa, WI_PCI_LOMEM, PCI_MAPREG_TYPE_MEM, 0,
    249 		    &memt, &memh, NULL, NULL) != 0) {
    250 			printf(": can't map mem space\n");
    251 			return;
    252 		}
    253 		if (pci_mapreg_map(pa, WI_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0,
    254 		    &iot, &ioh, NULL, NULL) != 0) {
    255 			printf(": can't map I/O space\n");
    256 			return;
    257 		}
    258 		/* Map PLX. */
    259 		if (pci_mapreg_map(pa, WI_PCI_PLX_LOIO, PCI_MAPREG_TYPE_IO, 0,
    260 		    &plxt, &plxh, NULL, NULL) != 0) {
    261 			printf(": can't map PLX\n");
    262 			return;
    263 		}
    264 	} else {
    265 		if (pci_mapreg_map(pa, WI_PCI_CBMA,
    266 		    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
    267 		    0, &iot, &ioh, NULL, NULL) != 0) {
    268 			printf(": can't map mem space\n");
    269 			return;
    270 		}
    271 
    272 		memt = iot;
    273 		memh = ioh;
    274 		sc->sc_pci = 1;
    275 	}
    276 
    277 	if (wpp->wpp_name != NULL) {
    278 		printf(": %s Wireless Lan\n", wpp->wpp_name);
    279 	} else {
    280 		char devinfo[256];
    281 
    282 		pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
    283 		printf(": %s (rev. 0x%02x)\n", devinfo,
    284 		       PCI_REVISION(pa->pa_class));
    285 	}
    286 
    287 	sc->sc_enabled = 1;
    288 	sc->sc_enable = wi_pci_enable;
    289 	sc->sc_disable = wi_pci_disable;
    290 
    291 	sc->sc_iot = iot;
    292 	sc->sc_ioh = ioh;
    293 	/* Make sure interrupts are disabled. */
    294 	CSR_WRITE_2(sc, WI_INT_EN, 0);
    295 	CSR_WRITE_2(sc, WI_EVENT_ACK, 0xFFFF);
    296 
    297 	if (wpp->wpp_plx) {
    298 		uint32_t command;
    299 #define	WI_LOCAL_INTCSR		0x4c
    300 #define	WI_LOCAL_INTEN		0x40	/* poke this into INTCSR */
    301 
    302 		command = bus_space_read_4(plxt, plxh, WI_LOCAL_INTCSR);
    303 		command |= WI_LOCAL_INTEN;
    304 		bus_space_write_4(plxt, plxh, WI_LOCAL_INTCSR, command);
    305 	}
    306 
    307 	/* Map and establish the interrupt. */
    308 	if (pci_intr_map(pa, &ih)) {
    309 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
    310 		return;
    311 	}
    312 	intrstr = pci_intr_string(pc, ih);
    313 
    314 	psc->psc_ih = ih;
    315 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, wi_intr, sc);
    316 	if (sc->sc_ih == NULL) {
    317 		printf("%s: couldn't establish interrupt",
    318 		    sc->sc_dev.dv_xname);
    319 		if (intrstr != NULL)
    320 			printf(" at %s", intrstr);
    321 		printf("\n");
    322 		return;
    323 	}
    324 
    325 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    326 
    327 	if (wpp->wpp_plx) {
    328 		/*
    329 		 * Setup the PLX chip for level interrupts and config index 1
    330 		 * XXX - should really reset the PLX chip too.
    331 		 */
    332 		bus_space_write_1(memt, memh,
    333 		    WI_PLX_COR_OFFSET, WI_PLX_COR_VALUE);
    334 	} else {
    335 		/* reset HFA3842 MAC core */
    336 		wi_pci_reset(sc);
    337 	}
    338 
    339 	printf("%s:", sc->sc_dev.dv_xname);
    340 	if (wi_attach(sc) != 0) {
    341 		printf("%s: failed to attach controller\n",
    342 			sc->sc_dev.dv_xname);
    343 		pci_intr_disestablish(pa->pa_pc, sc->sc_ih);
    344 		return;
    345 	}
    346 
    347 	sc->sc_reset = wi_pci_reset;
    348 
    349 	/* Add a suspend hook to restore PCI config state */
    350 	psc->sc_powerhook = powerhook_establish(wi_pci_powerhook, psc);
    351 	if (psc->sc_powerhook == NULL)
    352 		printf ("%s: WARNING: unable to establish pci power hook\n",
    353 		        sc->sc_dev.dv_xname);
    354 }
    355 
    356 static void
    357 wi_pci_powerhook(why, arg)
    358 	int why;
    359 	void *arg;
    360 {
    361 	struct wi_pci_softc *psc = arg;
    362 	struct wi_softc *sc = &psc->psc_wi;
    363 
    364 	wi_power(sc, why);
    365 }
    366