Home | History | Annotate | Line # | Download | only in pcmcia
if_awi_pcmcia.c revision 1.26
      1 /* $NetBSD: if_awi_pcmcia.c,v 1.26 2002/10/02 16:52:09 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Bill Sommerfeld
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * PCMCIA attachment for BayStack 650 802.11FH PCMCIA card,
     41  * based on the AMD 79c930 802.11 controller chip.
     42  *
     43  * This attachment can probably be trivally adapted for other FH and
     44  * DS cards based on the same chipset.
     45  */
     46 
     47 #include <sys/cdefs.h>
     48 __KERNEL_RCSID(0, "$NetBSD: if_awi_pcmcia.c,v 1.26 2002/10/02 16:52:09 thorpej Exp $");
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/mbuf.h>
     53 #include <sys/socket.h>
     54 #include <sys/ioctl.h>
     55 #include <sys/errno.h>
     56 #include <sys/syslog.h>
     57 #include <sys/select.h>
     58 #include <sys/device.h>
     59 
     60 #include <net/if.h>
     61 #include <net/if_dl.h>
     62 #include <net/if_ether.h>
     63 #include <net/if_media.h>
     64 #include <net/if_ieee80211.h>
     65 
     66 #include <machine/cpu.h>
     67 #include <machine/bus.h>
     68 #include <machine/intr.h>
     69 
     70 #include <dev/ic/am79c930reg.h>
     71 #include <dev/ic/am79c930var.h>
     72 #include <dev/ic/awireg.h>
     73 #include <dev/ic/awivar.h>
     74 
     75 #include <dev/pcmcia/pcmciareg.h>
     76 #include <dev/pcmcia/pcmciavar.h>
     77 #include <dev/pcmcia/pcmciadevs.h>
     78 
     79 static int awi_pcmcia_match(struct device *, struct cfdata *, void *);
     80 static void awi_pcmcia_attach(struct device *, struct device *, void *);
     81 static int awi_pcmcia_detach(struct device *, int);
     82 static int awi_pcmcia_enable(struct awi_softc *);
     83 static void awi_pcmcia_disable(struct awi_softc *);
     84 
     85 struct awi_pcmcia_softc {
     86 	struct awi_softc sc_awi;		/* real "awi" softc */
     87 
     88 	/* PCMCIA-specific goo */
     89 	struct pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
     90 	struct pcmcia_mem_handle sc_memh;	/* PCMCIA memory space info */
     91 	int sc_io_window;			/* our i/o window */
     92 	int sc_mem_window;			/* our memory window */
     93 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
     94 	void *sc_ih;				/* interrupt handler */
     95 };
     96 
     97 static int	awi_pcmcia_find __P((struct awi_pcmcia_softc *,
     98     struct pcmcia_attach_args *, struct pcmcia_config_entry *));
     99 
    100 CFATTACH_DECL(awi_pcmcia, sizeof(struct awi_pcmcia_softc),
    101     awi_pcmcia_match, awi_pcmcia_attach, awi_pcmcia_detach, awi_activate);
    102 
    103 static const struct awi_pcmcia_product {
    104 	u_int32_t	app_vendor;	/* vendor ID */
    105 	u_int32_t	app_product;	/* product ID */
    106 	const char	*app_cisinfo[4]; /* CIS information */
    107 	const char	*app_name;	/* product name */
    108 } awi_pcmcia_products[] = {
    109 	{ PCMCIA_VENDOR_BAY,		PCMCIA_PRODUCT_BAY_STACK_650,
    110 	  PCMCIA_CIS_BAY_STACK_650,	PCMCIA_STR_BAY_STACK_650 },
    111 
    112 	{ PCMCIA_VENDOR_BAY,		PCMCIA_PRODUCT_BAY_STACK_660,
    113 	  PCMCIA_CIS_BAY_STACK_660,	PCMCIA_STR_BAY_STACK_660 },
    114 
    115 	{ PCMCIA_VENDOR_BAY,		PCMCIA_PRODUCT_BAY_SURFER_PRO,
    116 	  PCMCIA_CIS_BAY_SURFER_PRO,	PCMCIA_STR_BAY_SURFER_PRO },
    117 
    118 	{ PCMCIA_VENDOR_AMD,		PCMCIA_PRODUCT_AMD_AM79C930,
    119 	  PCMCIA_CIS_AMD_AM79C930,	PCMCIA_STR_AMD_AM79C930 },
    120 
    121 	{ PCMCIA_VENDOR_ICOM,		PCMCIA_PRODUCT_ICOM_SL200,
    122 	  PCMCIA_CIS_ICOM_SL200,	PCMCIA_STR_ICOM_SL200 },
    123 
    124 	{ PCMCIA_VENDOR_NOKIA,		PCMCIA_PRODUCT_NOKIA_C020_WLAN,
    125 	  PCMCIA_CIS_NOKIA_C020_WLAN,	PCMCIA_STR_NOKIA_C020_WLAN },
    126 
    127 	{ PCMCIA_VENDOR_FARALLON,	PCMCIA_PRODUCT_FARALLON_SKYLINE,
    128 	  PCMCIA_CIS_FARALLON_SKYLINE,	PCMCIA_STR_FARALLON_SKYLINE },
    129 
    130 	{ 0,				0,
    131 	  { NULL, NULL, NULL, NULL },	NULL },
    132 };
    133 
    134 static const struct awi_pcmcia_product *
    135 	awi_pcmcia_lookup __P((struct pcmcia_attach_args *));
    136 
    137 static const struct awi_pcmcia_product *
    138 awi_pcmcia_lookup(pa)
    139 	struct pcmcia_attach_args *pa;
    140 {
    141 	const struct awi_pcmcia_product *app;
    142 
    143 	for (app = awi_pcmcia_products; app->app_name != NULL; app++) {
    144 		/* match by vendor/product id */
    145 		if (pa->manufacturer != PCMCIA_VENDOR_INVALID &&
    146 		    pa->manufacturer == app->app_vendor &&
    147 		    pa->product != PCMCIA_PRODUCT_INVALID &&
    148 		    pa->product == app->app_product)
    149 			return (app);
    150 
    151 		/* match by CIS information */
    152 		if (pa->card->cis1_info[0] != NULL &&
    153 		    app->app_cisinfo[0] != NULL &&
    154 		    strcmp(pa->card->cis1_info[0], app->app_cisinfo[0]) == 0 &&
    155 		    pa->card->cis1_info[1] != NULL &&
    156 		    app->app_cisinfo[1] != NULL &&
    157 		    strcmp(pa->card->cis1_info[1], app->app_cisinfo[1]) == 0)
    158 			return (app);
    159 	}
    160 
    161 	return (NULL);
    162 }
    163 
    164 static int
    165 awi_pcmcia_enable(sc)
    166 	struct awi_softc *sc;
    167 {
    168 	struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *)sc;
    169 	struct pcmcia_function *pf = psc->sc_pf;
    170 
    171 	/* establish the interrupt. */
    172 	psc->sc_ih = pcmcia_intr_establish(pf, IPL_NET, awi_intr, sc);
    173 	if (psc->sc_ih == NULL) {
    174 		printf("%s: couldn't establish interrupt\n",
    175 		    sc->sc_dev.dv_xname);
    176 		return (1);
    177 	}
    178 
    179 	if (pcmcia_function_enable(pf)) {
    180 		pcmcia_intr_disestablish(pf, psc->sc_ih);
    181 		return (1);
    182 	}
    183 	DELAY(1000);
    184 
    185 	return (0);
    186 }
    187 
    188 static void
    189 awi_pcmcia_disable(sc)
    190 	struct awi_softc *sc;
    191 {
    192 	struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *)sc;
    193 	struct pcmcia_function *pf = psc->sc_pf;
    194 
    195 	pcmcia_function_disable(pf);
    196 	pcmcia_intr_disestablish(pf, psc->sc_ih);
    197 }
    198 
    199 static int
    200 awi_pcmcia_match(parent, match, aux)
    201 	struct device *parent;
    202 	struct cfdata *match;
    203 	void *aux;
    204 {
    205 	struct pcmcia_attach_args *pa = aux;
    206 
    207 	if (awi_pcmcia_lookup(pa) != NULL)
    208 		return (1);
    209 
    210 	return (0);
    211 }
    212 
    213 static int
    214 awi_pcmcia_find(psc, pa, cfe)
    215 	struct awi_pcmcia_softc *psc;
    216 	struct pcmcia_attach_args *pa;
    217 	struct pcmcia_config_entry *cfe;
    218 {
    219 	struct awi_softc *sc = &psc->sc_awi;
    220 	int fail = 0;
    221 
    222 	/*
    223 	 * see if we can read the firmware version sanely
    224 	 * through the i/o ports.
    225 	 * if not, try a different CIS string..
    226 	 */
    227 	if (pcmcia_io_alloc(psc->sc_pf, cfe->iospace[0].start,
    228 	    cfe->iospace[0].length, AM79C930_IO_ALIGN,
    229 	    &psc->sc_pcioh) != 0)
    230 		goto fail;
    231 
    232 	if (pcmcia_io_map(psc->sc_pf, PCMCIA_WIDTH_AUTO, 0, psc->sc_pcioh.size,
    233 	    &psc->sc_pcioh, &psc->sc_io_window))
    234 		goto fail_io_free;
    235 
    236 	/* Enable the card. */
    237 	pcmcia_function_init(psc->sc_pf, cfe);
    238 	if (pcmcia_function_enable(psc->sc_pf))
    239 		goto fail_io_unmap;
    240 
    241 	sc->sc_chip.sc_bustype = AM79C930_BUS_PCMCIA;
    242 	sc->sc_chip.sc_iot = psc->sc_pcioh.iot;
    243 	sc->sc_chip.sc_ioh = psc->sc_pcioh.ioh;
    244 	am79c930_chip_init(&sc->sc_chip, 0);
    245 
    246 	DELAY(1000);
    247 
    248 	awi_read_bytes(sc, AWI_BANNER, sc->sc_banner, AWI_BANNER_LEN);
    249 
    250 	if (memcmp(sc->sc_banner, "PCnetMobile:", 12) == 0)
    251 		return (0);
    252 
    253 	fail++;
    254 	pcmcia_function_disable(psc->sc_pf);
    255 
    256  fail_io_unmap:
    257 	fail++;
    258 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    259 
    260  fail_io_free:
    261 	fail++;
    262 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    263  fail:
    264 	fail++;
    265 	psc->sc_io_window = -1;
    266 	return (fail);
    267 }
    268 
    269 static void
    270 awi_pcmcia_attach(parent, self, aux)
    271 	struct device  *parent, *self;
    272 	void           *aux;
    273 {
    274 	struct awi_pcmcia_softc *psc = (void *)self;
    275 	struct awi_softc *sc = &psc->sc_awi;
    276 	const struct awi_pcmcia_product *app;
    277 	struct pcmcia_attach_args *pa = aux;
    278 	struct pcmcia_config_entry *cfe;
    279 	bus_size_t memoff;
    280 
    281 	app = awi_pcmcia_lookup(pa);
    282 	if (app == NULL)
    283 		panic("awi_pcmcia_attach: impossible");
    284 
    285 	psc->sc_pf = pa->pf;
    286 
    287 	SIMPLEQ_FOREACH(cfe, &pa->pf->cfe_head, cfe_list) {
    288 		if (cfe->iftype != PCMCIA_IFTYPE_IO)
    289 			continue;
    290 		if (cfe->num_iospace < 1)
    291 			continue;
    292 		if (cfe->iospace[0].length < AM79C930_IO_SIZE)
    293 			continue;
    294 
    295 		if (awi_pcmcia_find(psc, pa, cfe) == 0)
    296 			break;
    297 	}
    298 	if (cfe == NULL) {
    299 		printf(": no suitable CIS info found\n");
    300 		goto no_config_entry;
    301 	}
    302 
    303 	sc->sc_enabled = 1;
    304 	printf(": %s\n", app->app_name);
    305 
    306 	psc->sc_mem_window = -1;
    307 	if (pcmcia_mem_alloc(psc->sc_pf, AM79C930_MEM_SIZE,
    308 	    &psc->sc_memh) != 0) {
    309 		printf("%s: unable to allocate memory space; using i/o only\n",
    310 		    sc->sc_dev.dv_xname);
    311 	} else if (pcmcia_mem_map(psc->sc_pf,
    312 	    PCMCIA_WIDTH_MEM8|PCMCIA_MEM_COMMON, AM79C930_MEM_BASE,
    313 	    AM79C930_MEM_SIZE, &psc->sc_memh, &memoff, &psc->sc_mem_window)) {
    314 		printf("%s: unable to map memory space; using i/o only\n",
    315 		    sc->sc_dev.dv_xname);
    316 		pcmcia_mem_free(psc->sc_pf, &psc->sc_memh);
    317 	} else {
    318 		sc->sc_chip.sc_memt = psc->sc_memh.memt;
    319 		sc->sc_chip.sc_memh = psc->sc_memh.memh;
    320 		am79c930_chip_init(&sc->sc_chip, 1);
    321 	}
    322 
    323 	sc->sc_enable = awi_pcmcia_enable;
    324 	sc->sc_disable = awi_pcmcia_disable;
    325 
    326 	/* establish the interrupt. */
    327 	psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, awi_intr, sc);
    328 	if (psc->sc_ih == NULL) {
    329 		printf("%s: couldn't establish interrupt\n",
    330 		    sc->sc_dev.dv_xname);
    331 		goto no_interrupt;
    332 	}
    333 	sc->sc_cansleep = 1;
    334 
    335 	if (awi_attach(sc) != 0) {
    336 		printf("%s: failed to attach controller\n",
    337 		    sc->sc_dev.dv_xname);
    338 		goto attach_failed;
    339 	}
    340 
    341 	sc->sc_enabled = 0;
    342 	/* disable device and disestablish the interrupt */
    343 	awi_pcmcia_disable(sc);
    344 	return;
    345 
    346  attach_failed:
    347 	pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    348 
    349  no_interrupt:
    350 	/* Unmap our memory window and space */
    351 	if (psc->sc_mem_window != -1) {
    352 		pcmcia_mem_unmap(psc->sc_pf, psc->sc_mem_window);
    353 		pcmcia_mem_free(psc->sc_pf, &psc->sc_memh);
    354 	}
    355 
    356 	/* Unmap our i/o window and space */
    357 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    358 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    359 
    360 	/* Disable the function */
    361 	pcmcia_function_disable(psc->sc_pf);
    362 
    363  no_config_entry:
    364 	psc->sc_io_window = -1;
    365 }
    366 
    367 
    368 static int
    369 awi_pcmcia_detach(self, flags)
    370 	struct device *self;
    371 	int flags;
    372 {
    373 	struct awi_pcmcia_softc *psc = (struct awi_pcmcia_softc *)self;
    374 	int error;
    375 
    376 	if (psc->sc_io_window == -1)
    377 		/* Nothing to detach. */
    378 		return (0);
    379 
    380 	error = awi_detach(&psc->sc_awi);
    381 	if (error != 0)
    382 		return (error);
    383 
    384 	/* Unmap our memory window and free memory space */
    385 	if (psc->sc_mem_window != -1) {
    386 		pcmcia_mem_unmap(psc->sc_pf, psc->sc_mem_window);
    387 		pcmcia_mem_free(psc->sc_pf, &psc->sc_memh);
    388 	}
    389 
    390 	/* Unmap our i/o window. */
    391 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    392 
    393 	/* Free our i/o space. */
    394 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    395 	return (0);
    396 }
    397