Home | History | Annotate | Line # | Download | only in pcmcia
if_ep_pcmcia.c revision 1.46
      1 /*	$NetBSD: if_ep_pcmcia.c,v 1.46 2004/08/09 18:30:51 mycroft 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.46 2004/08/09 18:30:51 mycroft 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 void	ep_pcmcia_disable1 __P((struct ep_softc *));
    109 
    110 struct ep_pcmcia_softc {
    111 	struct ep_softc sc_ep;			/* real "ep" softc */
    112 
    113 	/* PCMCIA-specific goo */
    114 	struct pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
    115 	int sc_io_window;			/* our i/o window */
    116 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
    117 };
    118 
    119 CFATTACH_DECL(ep_pcmcia, sizeof(struct ep_pcmcia_softc),
    120     ep_pcmcia_match, ep_pcmcia_attach, ep_pcmcia_detach, ep_activate);
    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_VENDOR_3COM,
    128 	    PCMCIA_PRODUCT_3COM_3C562,		0 },
    129 	  ELINK_CHIPSET_3C509, 0 },
    130 
    131 	{ { "",		PCMCIA_VENDOR_3COM,
    132 	    PCMCIA_PRODUCT_3COM_3C589,		0 },
    133 	  ELINK_CHIPSET_3C509, 0 },
    134 
    135 	{ { "",		PCMCIA_VENDOR_3COM,
    136 	    PCMCIA_PRODUCT_3COM_3CXEM556,	0 },
    137 	  ELINK_CHIPSET_3C509, 0 },
    138 
    139 	{ { "",	PCMCIA_VENDOR_3COM,
    140 	    PCMCIA_PRODUCT_3COM_3CXEM556INT,	0 },
    141 	  ELINK_CHIPSET_3C509, 0 },
    142 
    143 	{ { "",		PCMCIA_VENDOR_3COM,
    144 	    PCMCIA_PRODUCT_3COM_3C574,		0 },
    145 	  ELINK_CHIPSET_ROADRUNNER, ELINK_FLAGS_MII },
    146 
    147 	{ { "",		PCMCIA_VENDOR_3COM,
    148 	    PCMCIA_PRODUCT_3COM_3CCFEM556BI,	0 },
    149 	  ELINK_CHIPSET_ROADRUNNER, ELINK_FLAGS_MII },
    150 
    151 	{ { "",		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 	if ((error = pcmcia_function_enable(pf))) {
    191 		pcmcia_intr_disestablish(pf, sc->sc_ih);
    192 		return (error);
    193 	}
    194 
    195 	if ((psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3C562) ||
    196 	    (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3CXEM556) ||
    197 	    (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3CXEM556INT)) {
    198 		int reg;
    199 
    200 		/* turn off the serial-disable bit */
    201 
    202 		reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
    203 		if (reg & 0x08) {
    204 			reg &= ~0x08;
    205 			pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
    206 		}
    207 
    208 	}
    209 
    210 	return (0);
    211 }
    212 
    213 void
    214 ep_pcmcia_disable(sc)
    215 	struct ep_softc *sc;
    216 {
    217 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc;
    218 
    219 	pcmcia_function_disable(psc->sc_pf);
    220 	pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih);
    221 }
    222 
    223 void
    224 ep_pcmcia_attach(parent, self, aux)
    225 	struct device  *parent, *self;
    226 	void           *aux;
    227 {
    228 	struct ep_pcmcia_softc *psc = (void *) self;
    229 	struct ep_softc *sc = &psc->sc_ep;
    230 	struct pcmcia_attach_args *pa = aux;
    231 	struct pcmcia_config_entry *cfe;
    232 	const struct ep_pcmcia_product *epp;
    233 	u_int8_t myla[ETHER_ADDR_LEN];
    234 	u_int8_t *enaddr = NULL;
    235 	int i;
    236 
    237 	aprint_normal("\n");
    238 	psc->sc_pf = pa->pf;
    239 
    240 	SIMPLEQ_FOREACH(cfe, &pa->pf->cfe_head, cfe_list) {
    241 		if (cfe->num_memspace != 0)
    242 			continue;
    243 		if (cfe->num_iospace != 1)
    244 			continue;
    245 
    246 		if (pa->product == PCMCIA_PRODUCT_3COM_3C562) {
    247 			/*
    248 			 * the 3c562 can only use 0x??00-0x??7f
    249 			 * according to the Linux driver
    250 			 */
    251 
    252 			/*
    253 			 * 3c562 i/o may decodes address line not only A0-3
    254 			 * but also A7.  Anyway, we must sweep at most
    255 			 * [0x0000, 0x0100).  The address higher is given by a
    256 			 * pcmcia bridge.  But pcmcia bus-space allocation
    257 			 * function implies cards will decode 10-bit address
    258 			 * line.  So we must search [0x0000, 0x0400).
    259 			 *
    260 			 * XXX: We must not check the bunch of I/O space range
    261 			 * [0x400*n, 0x300 + 0x400*n) because they are
    262 			 * reserved for legacy ISA devices and their alias
    263 			 * images on PC/AT architecture.
    264 			 */
    265 			for (i = 0x0300; i < 0x0380; i += 0x10) {
    266 				if (pcmcia_io_alloc(pa->pf, i,
    267 				    cfe->iospace[0].length,
    268 				    cfe->iospace[0].length,
    269 				    &psc->sc_pcioh) == 0)
    270 					break;
    271 			}
    272 			if (i != 0x0380)
    273 				break;
    274 		} else {
    275 			if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
    276 			    cfe->iospace[0].length, cfe->iospace[0].length,
    277 			    &psc->sc_pcioh) == 0)
    278 				break;
    279 		}
    280 	}
    281 	if (!cfe) {
    282 		aprint_error("%s: failed to allocate I/O space\n",
    283 		    self->dv_xname);
    284 		goto ioalloc_failed;
    285 	}
    286 
    287 	sc->sc_iot = psc->sc_pcioh.iot;
    288 	sc->sc_ioh = psc->sc_pcioh.ioh;
    289 
    290 	/* Enable the card. */
    291 	pcmcia_function_init(pa->pf, cfe);
    292 
    293 	if (pcmcia_io_map(pa->pf, ((cfe->flags & PCMCIA_CFE_IO16) ?
    294 	    PCMCIA_WIDTH_AUTO : PCMCIA_WIDTH_IO8), &psc->sc_pcioh,
    295 	    &psc->sc_io_window)) {
    296 		aprint_error("%s: can't map i/o space\n", self->dv_xname);
    297 		goto iomap_failed;
    298 	}
    299 
    300 	if (ep_pcmcia_enable(sc)) {
    301 		aprint_error("%s: function enable failed\n", self->dv_xname);
    302 		goto enable_failed;
    303 	}
    304 	sc->enabled = 1;
    305 
    306 	switch (pa->product) {
    307 	case PCMCIA_PRODUCT_3COM_3C562:
    308 		/*
    309 		 * 3c562a-c use this; 3c562d does it in the regular way.
    310 		 * we might want to check the revision and produce a warning
    311 		 * in the future.
    312 		 */
    313 		/* FALLTHROUGH */
    314 	case PCMCIA_PRODUCT_3COM_3C574:
    315 	case PCMCIA_PRODUCT_3COM_3CCFEM556BI:
    316 		/*
    317 		 * Apparently, some 3c574s do it this way, as well.
    318 		 */
    319 		if (pcmcia_scan_cis(parent, ep_pcmcia_get_enaddr, myla))
    320 			enaddr = myla;
    321 		break;
    322 	}
    323 
    324 	epp = (const struct ep_pcmcia_product *)pcmcia_product_lookup(pa,
    325             (const struct pcmcia_product *)ep_pcmcia_products,
    326             sizeof ep_pcmcia_products[0], NULL);
    327 	if (epp == NULL)
    328 		panic("ep_pcmcia_attach: impossible");
    329 
    330 	sc->bustype = ELINK_BUS_PCMCIA;
    331 	sc->ep_flags = epp->epp_flags;
    332 
    333 	sc->enable = ep_pcmcia_enable;
    334 	sc->disable = ep_pcmcia_disable;
    335 
    336 	if (epconfig(sc, epp->epp_chipset, enaddr)) {
    337 		aprint_error("%s: couldn't configure controller\n",
    338 		    self->dv_xname);
    339 		goto config_failed;
    340 	}
    341 
    342 	sc->enabled = 0;
    343 	ep_pcmcia_disable(sc);
    344 	return;
    345 
    346 config_failed:
    347 	sc->enabled = 0;
    348 	ep_pcmcia_disable(sc);
    349 enable_failed:
    350 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    351 iomap_failed:
    352 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    353 ioalloc_failed:
    354 	psc->sc_io_window = -1;
    355 }
    356 
    357 int
    358 ep_pcmcia_detach(self, flags)
    359 	struct device *self;
    360 	int flags;
    361 {
    362 	struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *)self;
    363 	int rv;
    364 
    365 	if (psc->sc_io_window == -1)
    366 		/* Nothing to detach. */
    367 		return (0);
    368 
    369 	rv = ep_detach(self, flags);
    370 	if (rv != 0)
    371 		return (rv);
    372 
    373 	/* Unmap our i/o window. */
    374 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    375 
    376 	/* Free our i/o space. */
    377 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    378 
    379 	return (0);
    380 }
    381 
    382 int
    383 ep_pcmcia_get_enaddr(tuple, arg)
    384 	struct pcmcia_tuple *tuple;
    385 	void *arg;
    386 {
    387 	u_int8_t *myla = arg;
    388 	int i;
    389 
    390 	/* this is 3c562a-c magic */
    391 	if (tuple->code == 0x88) {
    392 		if (tuple->length < ETHER_ADDR_LEN)
    393 			return (0);
    394 
    395 		for (i = 0; i < ETHER_ADDR_LEN; i += 2) {
    396 			myla[i] = pcmcia_tuple_read_1(tuple, i + 1);
    397 			myla[i + 1] = pcmcia_tuple_read_1(tuple, i);
    398 		}
    399 
    400 		return (1);
    401 	}
    402 	return (0);
    403 }
    404