Home | History | Annotate | Line # | Download | only in pcmcia
if_ep_pcmcia.c revision 1.34
      1 /*	$NetBSD: if_ep_pcmcia.c,v 1.34 2001/07/01 16:35:37 thorpej 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/param.h>
     70 #include <sys/systm.h>
     71 #include <sys/mbuf.h>
     72 #include <sys/socket.h>
     73 #include <sys/ioctl.h>
     74 #include <sys/errno.h>
     75 #include <sys/syslog.h>
     76 #include <sys/select.h>
     77 #include <sys/device.h>
     78 
     79 #include <net/if.h>
     80 #include <net/if_dl.h>
     81 #include <net/if_ether.h>
     82 #include <net/if_media.h>
     83 
     84 #include <machine/cpu.h>
     85 #include <machine/bus.h>
     86 #include <machine/intr.h>
     87 
     88 #include <dev/mii/miivar.h>
     89 
     90 #include <dev/ic/elink3var.h>
     91 #include <dev/ic/elink3reg.h>
     92 
     93 #include <dev/pcmcia/pcmciareg.h>
     94 #include <dev/pcmcia/pcmciavar.h>
     95 #include <dev/pcmcia/pcmciadevs.h>
     96 
     97 int	ep_pcmcia_match __P((struct device *, struct cfdata *, void *));
     98 void	ep_pcmcia_attach __P((struct device *, struct device *, void *));
     99 int	ep_pcmcia_detach __P((struct device *, int));
    100 
    101 int	ep_pcmcia_get_enaddr __P((struct pcmcia_tuple *, void *));
    102 int	ep_pcmcia_enable __P((struct ep_softc *));
    103 void	ep_pcmcia_disable __P((struct ep_softc *));
    104 
    105 int	ep_pcmcia_enable1 __P((struct ep_softc *));
    106 void	ep_pcmcia_disable1 __P((struct ep_softc *));
    107 
    108 struct ep_pcmcia_softc {
    109 	struct ep_softc sc_ep;			/* real "ep" softc */
    110 
    111 	/* PCMCIA-specific goo */
    112 	struct pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
    113 	int sc_io_window;			/* our i/o window */
    114 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
    115 };
    116 
    117 struct cfattach ep_pcmcia_ca = {
    118 	sizeof(struct ep_pcmcia_softc), ep_pcmcia_match, ep_pcmcia_attach,
    119 	    ep_pcmcia_detach, ep_activate
    120 };
    121 
    122 const struct ep_pcmcia_product {
    123 	struct pcmcia_product epp_product;
    124 	u_short		epp_chipset;	/* 3Com chipset used */
    125 	int		epp_flags;	/* initial softc flags */
    126 } ep_pcmcia_products[] = {
    127 	{ { PCMCIA_STR_3COM_3C562,		PCMCIA_VENDOR_3COM,
    128 	    PCMCIA_PRODUCT_3COM_3C562,		0 },
    129 	  ELINK_CHIPSET_3C509, 0 },
    130 
    131 	{ { PCMCIA_STR_3COM_3C589,		PCMCIA_VENDOR_3COM,
    132 	    PCMCIA_PRODUCT_3COM_3C589,		0 },
    133 	  ELINK_CHIPSET_3C509, 0 },
    134 
    135 	{ { PCMCIA_STR_3COM_3CXEM556,		PCMCIA_VENDOR_3COM,
    136 	    PCMCIA_PRODUCT_3COM_3CXEM556,	0 },
    137 	  ELINK_CHIPSET_3C509, 0 },
    138 
    139 	{ { PCMCIA_STR_3COM_3CXEM556INT,	PCMCIA_VENDOR_3COM,
    140 	    PCMCIA_PRODUCT_3COM_3CXEM556INT,	0 },
    141 	  ELINK_CHIPSET_3C509, 0 },
    142 
    143 	{ { PCMCIA_STR_3COM_3C574,		PCMCIA_VENDOR_3COM,
    144 	    PCMCIA_PRODUCT_3COM_3C574,		0 },
    145 	  ELINK_CHIPSET_ROADRUNNER, ELINK_FLAGS_MII },
    146 
    147 	{ { PCMCIA_STR_3COM_3CCFEM556BI,	PCMCIA_VENDOR_3COM,
    148 	    PCMCIA_PRODUCT_3COM_3CCFEM556BI,	0 },
    149 	  ELINK_CHIPSET_ROADRUNNER, ELINK_FLAGS_MII },
    150 
    151 	{ { PCMCIA_STR_3COM_3C1,		PCMCIA_VENDOR_3COM,
    152 	    PCMCIA_PRODUCT_3COM_3C1,		0 },
    153 	  ELINK_CHIPSET_3C509, 0 },
    154 
    155 	{ { NULL } }
    156 };
    157 
    158 int
    159 ep_pcmcia_match(parent, match, aux)
    160 	struct device *parent;
    161 	struct cfdata *match;
    162 	void *aux;
    163 {
    164 	struct pcmcia_attach_args *pa = aux;
    165 
    166 	if (pcmcia_product_lookup(pa,
    167 	    (const struct pcmcia_product *)ep_pcmcia_products,
    168 	    sizeof ep_pcmcia_products[0], NULL) != NULL)
    169 		return (1);
    170 
    171 	return (0);
    172 }
    173 
    174 int
    175 ep_pcmcia_enable(sc)
    176 	struct ep_softc *sc;
    177 {
    178 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
    179 	struct pcmcia_function *pf = psc->sc_pf;
    180 	int error;
    181 
    182 	/* establish the interrupt. */
    183 	sc->sc_ih = pcmcia_intr_establish(pf, IPL_NET, epintr, sc);
    184 	if (sc->sc_ih == NULL) {
    185 		printf("%s: couldn't establish interrupt\n",
    186 		    sc->sc_dev.dv_xname);
    187 		return (1);
    188 	}
    189 
    190 	error = ep_pcmcia_enable1(sc);
    191 	if (error != 0)
    192 		pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih);
    193 	return (error);
    194 }
    195 
    196 int
    197 ep_pcmcia_enable1(sc)
    198 	struct ep_softc *sc;
    199 {
    200 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
    201 	struct pcmcia_function *pf = psc->sc_pf;
    202 	int ret;
    203 
    204 	if ((ret = pcmcia_function_enable(pf)))
    205 		return (ret);
    206 
    207 	if ((psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3C562) ||
    208 	    (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3CXEM556) ||
    209 	    (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3CXEM556INT)) {
    210 		int reg;
    211 
    212 		/* turn off the serial-disable bit */
    213 
    214 		reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
    215 		if (reg & 0x08) {
    216 			reg &= ~0x08;
    217 			pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    218 		}
    219 
    220 	}
    221 
    222 	return (ret);
    223 }
    224 
    225 void
    226 ep_pcmcia_disable(sc)
    227 	struct ep_softc *sc;
    228 {
    229 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
    230 
    231 	/*
    232 	 * We must disestablish the interrupt before disabling the function,
    233 	 * because on a multifunction card the interrupt disestablishment
    234 	 * accesses CCR registers.
    235 	 */
    236 	pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih);
    237 	ep_pcmcia_disable1(sc);
    238 }
    239 
    240 void
    241 ep_pcmcia_disable1(sc)
    242 	struct ep_softc *sc;
    243 {
    244 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
    245 
    246 	pcmcia_function_disable(psc->sc_pf);
    247 }
    248 
    249 void
    250 ep_pcmcia_attach(parent, self, aux)
    251 	struct device  *parent, *self;
    252 	void           *aux;
    253 {
    254 	struct ep_pcmcia_softc *psc = (void *) self;
    255 	struct ep_softc *sc = &psc->sc_ep;
    256 	struct pcmcia_attach_args *pa = aux;
    257 	struct pcmcia_config_entry *cfe;
    258 	const struct ep_pcmcia_product *epp;
    259 	u_int8_t myla[ETHER_ADDR_LEN];
    260 	u_int8_t *enaddr = NULL;
    261 	int i;
    262 
    263 	psc->sc_pf = pa->pf;
    264 	cfe = pa->pf->cfe_head.sqh_first;
    265 
    266 	/* Enable the card. */
    267 	pcmcia_function_init(pa->pf, cfe);
    268 	if (ep_pcmcia_enable1(sc)) {
    269 		printf(": function enable failed\n");
    270 		goto enable_failed;
    271 	}
    272 	sc->enabled = 1;
    273 
    274 	if (cfe->num_memspace != 0) {
    275 		printf(": unexpected number of memory spaces %d should be 0\n",
    276 		    cfe->num_memspace);
    277 		goto ioalloc_failed;
    278 	}
    279 
    280 	if (cfe->num_iospace != 1) {
    281 		printf(": unexpected number of I/O spaces %d should be 1\n",
    282 		    cfe->num_iospace);
    283 		goto ioalloc_failed;
    284 	}
    285 
    286 	if (pa->product == PCMCIA_PRODUCT_3COM_3C562) {
    287 		bus_addr_t maxaddr = pa->pf->sc->iobase + pa->pf->sc->iosize;
    288 
    289 		for (i = pa->pf->sc->iobase; i < maxaddr; i += 0x10) {
    290 			/*
    291 			 * the 3c562 can only use 0x??00-0x??7f
    292 			 * according to the Linux driver
    293 			 */
    294 			if (i & 0x80)
    295 				continue;
    296 			if (pcmcia_io_alloc(pa->pf, i, cfe->iospace[0].length,
    297 			    0, &psc->sc_pcioh) == 0)
    298 				break;
    299 		}
    300 		if (i >= maxaddr) {
    301 			printf(": can't allocate i/o space\n");
    302 			goto ioalloc_failed;
    303 		}
    304 	} else {
    305 		if (pcmcia_io_alloc(pa->pf, 0, cfe->iospace[0].length,
    306 		    cfe->iospace[0].length, &psc->sc_pcioh)) {
    307 			printf(": can't allocate i/o space\n");
    308 			goto ioalloc_failed;
    309 		}
    310 	}
    311 
    312 	sc->sc_iot = psc->sc_pcioh.iot;
    313 	sc->sc_ioh = psc->sc_pcioh.ioh;
    314 
    315 	if (pcmcia_io_map(pa->pf, ((cfe->flags & PCMCIA_CFE_IO16) ?
    316 	    PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8), 0, cfe->iospace[0].length,
    317 	    &psc->sc_pcioh, &psc->sc_io_window)) {
    318 		printf(": can't map i/o space\n");
    319 		goto iomap_failed;
    320 	}
    321 
    322 	switch (pa->product) {
    323 	case PCMCIA_PRODUCT_3COM_3C562:
    324 		/*
    325 		 * 3c562a-c use this; 3c562d does it in the regular way.
    326 		 * we might want to check the revision and produce a warning
    327 		 * in the future.
    328 		 */
    329 		/* FALLTHROUGH */
    330 	case PCMCIA_PRODUCT_3COM_3C574:
    331 	case PCMCIA_PRODUCT_3COM_3CCFEM556BI:
    332 		/*
    333 		 * Apparently, some 3c574s do it this way, as well.
    334 		 */
    335 		if (pcmcia_scan_cis(parent, ep_pcmcia_get_enaddr, myla))
    336 			enaddr = myla;
    337 		break;
    338 	}
    339 
    340 	epp = (const struct ep_pcmcia_product *)pcmcia_product_lookup(pa,
    341             (const struct pcmcia_product *)ep_pcmcia_products,
    342             sizeof ep_pcmcia_products[0], NULL);
    343 	if (epp == NULL)
    344 		panic("ep_pcmcia_attach: impossible");
    345 
    346 	printf(": %s\n", epp->epp_product.pp_name);
    347 
    348 	sc->bustype = ELINK_BUS_PCMCIA;
    349 	sc->ep_flags = epp->epp_flags;
    350 
    351 	sc->enable = ep_pcmcia_enable;
    352 	sc->disable = ep_pcmcia_disable;
    353 
    354 	if (epconfig(sc, epp->epp_chipset, enaddr)) {
    355 		printf("%s: couldn't configure controller\n",
    356 		    sc->sc_dev.dv_xname);
    357 		goto config_failed;
    358 	}
    359 
    360 	sc->enabled = 0;
    361 	ep_pcmcia_disable1(sc);
    362 	return;
    363 
    364  config_failed:
    365 	/* Unmap our i/o window. */
    366 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    367 
    368  iomap_failed:
    369 	/* Free our i/o space. */
    370 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    371 
    372  ioalloc_failed:
    373 	sc->enabled = 0;
    374 	ep_pcmcia_disable1(sc);
    375 
    376  enable_failed:
    377 	psc->sc_io_window = -1;
    378 }
    379 
    380 int
    381 ep_pcmcia_detach(self, flags)
    382 	struct device *self;
    383 	int flags;
    384 {
    385 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *)self;
    386 	int rv;
    387 
    388 	if (psc->sc_io_window == -1)
    389 		/* Nothing to detach. */
    390 		return (0);
    391 
    392 	rv = ep_detach(self, flags);
    393 	if (rv != 0)
    394 		return (rv);
    395 
    396 	/* Unmap our i/o window. */
    397 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    398 
    399 	/* Free our i/o space. */
    400 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    401 
    402 	return (0);
    403 }
    404 
    405 int
    406 ep_pcmcia_get_enaddr(tuple, arg)
    407 	struct pcmcia_tuple *tuple;
    408 	void *arg;
    409 {
    410 	u_int8_t *myla = arg;
    411 	int i;
    412 
    413 	/* this is 3c562a-c magic */
    414 	if (tuple->code == 0x88) {
    415 		if (tuple->length < ETHER_ADDR_LEN)
    416 			return (0);
    417 
    418 		for (i = 0; i < ETHER_ADDR_LEN; i += 2) {
    419 			myla[i] = pcmcia_tuple_read_1(tuple, i + 1);
    420 			myla[i + 1] = pcmcia_tuple_read_1(tuple, i);
    421 		}
    422 
    423 		return (1);
    424 	}
    425 	return (0);
    426 }
    427