Home | History | Annotate | Line # | Download | only in pcmcia
if_ep_pcmcia.c revision 1.35
      1 /*	$NetBSD: if_ep_pcmcia.c,v 1.35 2001/11/13 07:26:33 lukem Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     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  * Copyright (c) 1997 Marc Horowitz.  All rights reserved.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *	This product includes software developed by Marc Horowitz.
     54  * 4. The name of the author may not be used to endorse or promote products
     55  *    derived from this software without specific prior written permission.
     56  *
     57  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     58  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     59  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     60  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     61  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     62  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     63  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     64  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     65  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     66  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     67  */
     68 
     69 #include <sys/cdefs.h>
     70 __KERNEL_RCSID(0, "$NetBSD: if_ep_pcmcia.c,v 1.35 2001/11/13 07:26:33 lukem Exp $");
     71 
     72 #include <sys/param.h>
     73 #include <sys/systm.h>
     74 #include <sys/mbuf.h>
     75 #include <sys/socket.h>
     76 #include <sys/ioctl.h>
     77 #include <sys/errno.h>
     78 #include <sys/syslog.h>
     79 #include <sys/select.h>
     80 #include <sys/device.h>
     81 
     82 #include <net/if.h>
     83 #include <net/if_dl.h>
     84 #include <net/if_ether.h>
     85 #include <net/if_media.h>
     86 
     87 #include <machine/cpu.h>
     88 #include <machine/bus.h>
     89 #include <machine/intr.h>
     90 
     91 #include <dev/mii/miivar.h>
     92 
     93 #include <dev/ic/elink3var.h>
     94 #include <dev/ic/elink3reg.h>
     95 
     96 #include <dev/pcmcia/pcmciareg.h>
     97 #include <dev/pcmcia/pcmciavar.h>
     98 #include <dev/pcmcia/pcmciadevs.h>
     99 
    100 int	ep_pcmcia_match __P((struct device *, struct cfdata *, void *));
    101 void	ep_pcmcia_attach __P((struct device *, struct device *, void *));
    102 int	ep_pcmcia_detach __P((struct device *, int));
    103 
    104 int	ep_pcmcia_get_enaddr __P((struct pcmcia_tuple *, void *));
    105 int	ep_pcmcia_enable __P((struct ep_softc *));
    106 void	ep_pcmcia_disable __P((struct ep_softc *));
    107 
    108 int	ep_pcmcia_enable1 __P((struct ep_softc *));
    109 void	ep_pcmcia_disable1 __P((struct ep_softc *));
    110 
    111 struct ep_pcmcia_softc {
    112 	struct ep_softc sc_ep;			/* real "ep" softc */
    113 
    114 	/* PCMCIA-specific goo */
    115 	struct pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
    116 	int sc_io_window;			/* our i/o window */
    117 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
    118 };
    119 
    120 struct cfattach ep_pcmcia_ca = {
    121 	sizeof(struct ep_pcmcia_softc), ep_pcmcia_match, ep_pcmcia_attach,
    122 	    ep_pcmcia_detach, ep_activate
    123 };
    124 
    125 const struct ep_pcmcia_product {
    126 	struct pcmcia_product epp_product;
    127 	u_short		epp_chipset;	/* 3Com chipset used */
    128 	int		epp_flags;	/* initial softc flags */
    129 } ep_pcmcia_products[] = {
    130 	{ { PCMCIA_STR_3COM_3C562,		PCMCIA_VENDOR_3COM,
    131 	    PCMCIA_PRODUCT_3COM_3C562,		0 },
    132 	  ELINK_CHIPSET_3C509, 0 },
    133 
    134 	{ { PCMCIA_STR_3COM_3C589,		PCMCIA_VENDOR_3COM,
    135 	    PCMCIA_PRODUCT_3COM_3C589,		0 },
    136 	  ELINK_CHIPSET_3C509, 0 },
    137 
    138 	{ { PCMCIA_STR_3COM_3CXEM556,		PCMCIA_VENDOR_3COM,
    139 	    PCMCIA_PRODUCT_3COM_3CXEM556,	0 },
    140 	  ELINK_CHIPSET_3C509, 0 },
    141 
    142 	{ { PCMCIA_STR_3COM_3CXEM556INT,	PCMCIA_VENDOR_3COM,
    143 	    PCMCIA_PRODUCT_3COM_3CXEM556INT,	0 },
    144 	  ELINK_CHIPSET_3C509, 0 },
    145 
    146 	{ { PCMCIA_STR_3COM_3C574,		PCMCIA_VENDOR_3COM,
    147 	    PCMCIA_PRODUCT_3COM_3C574,		0 },
    148 	  ELINK_CHIPSET_ROADRUNNER, ELINK_FLAGS_MII },
    149 
    150 	{ { PCMCIA_STR_3COM_3CCFEM556BI,	PCMCIA_VENDOR_3COM,
    151 	    PCMCIA_PRODUCT_3COM_3CCFEM556BI,	0 },
    152 	  ELINK_CHIPSET_ROADRUNNER, ELINK_FLAGS_MII },
    153 
    154 	{ { PCMCIA_STR_3COM_3C1,		PCMCIA_VENDOR_3COM,
    155 	    PCMCIA_PRODUCT_3COM_3C1,		0 },
    156 	  ELINK_CHIPSET_3C509, 0 },
    157 
    158 	{ { NULL } }
    159 };
    160 
    161 int
    162 ep_pcmcia_match(parent, match, aux)
    163 	struct device *parent;
    164 	struct cfdata *match;
    165 	void *aux;
    166 {
    167 	struct pcmcia_attach_args *pa = aux;
    168 
    169 	if (pcmcia_product_lookup(pa,
    170 	    (const struct pcmcia_product *)ep_pcmcia_products,
    171 	    sizeof ep_pcmcia_products[0], NULL) != NULL)
    172 		return (1);
    173 
    174 	return (0);
    175 }
    176 
    177 int
    178 ep_pcmcia_enable(sc)
    179 	struct ep_softc *sc;
    180 {
    181 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
    182 	struct pcmcia_function *pf = psc->sc_pf;
    183 	int error;
    184 
    185 	/* establish the interrupt. */
    186 	sc->sc_ih = pcmcia_intr_establish(pf, IPL_NET, epintr, sc);
    187 	if (sc->sc_ih == NULL) {
    188 		printf("%s: couldn't establish interrupt\n",
    189 		    sc->sc_dev.dv_xname);
    190 		return (1);
    191 	}
    192 
    193 	error = ep_pcmcia_enable1(sc);
    194 	if (error != 0)
    195 		pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih);
    196 	return (error);
    197 }
    198 
    199 int
    200 ep_pcmcia_enable1(sc)
    201 	struct ep_softc *sc;
    202 {
    203 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
    204 	struct pcmcia_function *pf = psc->sc_pf;
    205 	int ret;
    206 
    207 	if ((ret = pcmcia_function_enable(pf)))
    208 		return (ret);
    209 
    210 	if ((psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3C562) ||
    211 	    (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3CXEM556) ||
    212 	    (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3CXEM556INT)) {
    213 		int reg;
    214 
    215 		/* turn off the serial-disable bit */
    216 
    217 		reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
    218 		if (reg & 0x08) {
    219 			reg &= ~0x08;
    220 			pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    221 		}
    222 
    223 	}
    224 
    225 	return (ret);
    226 }
    227 
    228 void
    229 ep_pcmcia_disable(sc)
    230 	struct ep_softc *sc;
    231 {
    232 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
    233 
    234 	/*
    235 	 * We must disestablish the interrupt before disabling the function,
    236 	 * because on a multifunction card the interrupt disestablishment
    237 	 * accesses CCR registers.
    238 	 */
    239 	pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih);
    240 	ep_pcmcia_disable1(sc);
    241 }
    242 
    243 void
    244 ep_pcmcia_disable1(sc)
    245 	struct ep_softc *sc;
    246 {
    247 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
    248 
    249 	pcmcia_function_disable(psc->sc_pf);
    250 }
    251 
    252 void
    253 ep_pcmcia_attach(parent, self, aux)
    254 	struct device  *parent, *self;
    255 	void           *aux;
    256 {
    257 	struct ep_pcmcia_softc *psc = (void *) self;
    258 	struct ep_softc *sc = &psc->sc_ep;
    259 	struct pcmcia_attach_args *pa = aux;
    260 	struct pcmcia_config_entry *cfe;
    261 	const struct ep_pcmcia_product *epp;
    262 	u_int8_t myla[ETHER_ADDR_LEN];
    263 	u_int8_t *enaddr = NULL;
    264 	int i;
    265 
    266 	psc->sc_pf = pa->pf;
    267 	cfe = pa->pf->cfe_head.sqh_first;
    268 
    269 	/* Enable the card. */
    270 	pcmcia_function_init(pa->pf, cfe);
    271 	if (ep_pcmcia_enable1(sc)) {
    272 		printf(": function enable failed\n");
    273 		goto enable_failed;
    274 	}
    275 	sc->enabled = 1;
    276 
    277 	if (cfe->num_memspace != 0) {
    278 		printf(": unexpected number of memory spaces %d should be 0\n",
    279 		    cfe->num_memspace);
    280 		goto ioalloc_failed;
    281 	}
    282 
    283 	if (cfe->num_iospace != 1) {
    284 		printf(": unexpected number of I/O spaces %d should be 1\n",
    285 		    cfe->num_iospace);
    286 		goto ioalloc_failed;
    287 	}
    288 
    289 	if (pa->product == PCMCIA_PRODUCT_3COM_3C562) {
    290 		bus_addr_t maxaddr = pa->pf->sc->iobase + pa->pf->sc->iosize;
    291 
    292 		for (i = pa->pf->sc->iobase; i < maxaddr; i += 0x10) {
    293 			/*
    294 			 * the 3c562 can only use 0x??00-0x??7f
    295 			 * according to the Linux driver
    296 			 */
    297 			if (i & 0x80)
    298 				continue;
    299 			if (pcmcia_io_alloc(pa->pf, i, cfe->iospace[0].length,
    300 			    0, &psc->sc_pcioh) == 0)
    301 				break;
    302 		}
    303 		if (i >= maxaddr) {
    304 			printf(": can't allocate i/o space\n");
    305 			goto ioalloc_failed;
    306 		}
    307 	} else {
    308 		if (pcmcia_io_alloc(pa->pf, 0, cfe->iospace[0].length,
    309 		    cfe->iospace[0].length, &psc->sc_pcioh)) {
    310 			printf(": can't allocate i/o space\n");
    311 			goto ioalloc_failed;
    312 		}
    313 	}
    314 
    315 	sc->sc_iot = psc->sc_pcioh.iot;
    316 	sc->sc_ioh = psc->sc_pcioh.ioh;
    317 
    318 	if (pcmcia_io_map(pa->pf, ((cfe->flags & PCMCIA_CFE_IO16) ?
    319 	    PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8), 0, cfe->iospace[0].length,
    320 	    &psc->sc_pcioh, &psc->sc_io_window)) {
    321 		printf(": can't map i/o space\n");
    322 		goto iomap_failed;
    323 	}
    324 
    325 	switch (pa->product) {
    326 	case PCMCIA_PRODUCT_3COM_3C562:
    327 		/*
    328 		 * 3c562a-c use this; 3c562d does it in the regular way.
    329 		 * we might want to check the revision and produce a warning
    330 		 * in the future.
    331 		 */
    332 		/* FALLTHROUGH */
    333 	case PCMCIA_PRODUCT_3COM_3C574:
    334 	case PCMCIA_PRODUCT_3COM_3CCFEM556BI:
    335 		/*
    336 		 * Apparently, some 3c574s do it this way, as well.
    337 		 */
    338 		if (pcmcia_scan_cis(parent, ep_pcmcia_get_enaddr, myla))
    339 			enaddr = myla;
    340 		break;
    341 	}
    342 
    343 	epp = (const struct ep_pcmcia_product *)pcmcia_product_lookup(pa,
    344             (const struct pcmcia_product *)ep_pcmcia_products,
    345             sizeof ep_pcmcia_products[0], NULL);
    346 	if (epp == NULL)
    347 		panic("ep_pcmcia_attach: impossible");
    348 
    349 	printf(": %s\n", epp->epp_product.pp_name);
    350 
    351 	sc->bustype = ELINK_BUS_PCMCIA;
    352 	sc->ep_flags = epp->epp_flags;
    353 
    354 	sc->enable = ep_pcmcia_enable;
    355 	sc->disable = ep_pcmcia_disable;
    356 
    357 	if (epconfig(sc, epp->epp_chipset, enaddr)) {
    358 		printf("%s: couldn't configure controller\n",
    359 		    sc->sc_dev.dv_xname);
    360 		goto config_failed;
    361 	}
    362 
    363 	sc->enabled = 0;
    364 	ep_pcmcia_disable1(sc);
    365 	return;
    366 
    367  config_failed:
    368 	/* Unmap our i/o window. */
    369 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    370 
    371  iomap_failed:
    372 	/* Free our i/o space. */
    373 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    374 
    375  ioalloc_failed:
    376 	sc->enabled = 0;
    377 	ep_pcmcia_disable1(sc);
    378 
    379  enable_failed:
    380 	psc->sc_io_window = -1;
    381 }
    382 
    383 int
    384 ep_pcmcia_detach(self, flags)
    385 	struct device *self;
    386 	int flags;
    387 {
    388 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *)self;
    389 	int rv;
    390 
    391 	if (psc->sc_io_window == -1)
    392 		/* Nothing to detach. */
    393 		return (0);
    394 
    395 	rv = ep_detach(self, flags);
    396 	if (rv != 0)
    397 		return (rv);
    398 
    399 	/* Unmap our i/o window. */
    400 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    401 
    402 	/* Free our i/o space. */
    403 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    404 
    405 	return (0);
    406 }
    407 
    408 int
    409 ep_pcmcia_get_enaddr(tuple, arg)
    410 	struct pcmcia_tuple *tuple;
    411 	void *arg;
    412 {
    413 	u_int8_t *myla = arg;
    414 	int i;
    415 
    416 	/* this is 3c562a-c magic */
    417 	if (tuple->code == 0x88) {
    418 		if (tuple->length < ETHER_ADDR_LEN)
    419 			return (0);
    420 
    421 		for (i = 0; i < ETHER_ADDR_LEN; i += 2) {
    422 			myla[i] = pcmcia_tuple_read_1(tuple, i + 1);
    423 			myla[i + 1] = pcmcia_tuple_read_1(tuple, i);
    424 		}
    425 
    426 		return (1);
    427 	}
    428 	return (0);
    429 }
    430