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