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