Home | History | Annotate | Line # | Download | only in pcmcia
if_mbe_pcmcia.c revision 1.7
      1 /*	$NetBSD: if_mbe_pcmcia.c,v 1.7 1999/04/27 04:56:18 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 Enami Tsugutomo.
      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 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/device.h>
     42 #include <sys/socket.h>
     43 
     44 #include <net/if.h>
     45 #include <net/if_ether.h>
     46 #include <net/if_media.h>
     47 
     48 #include <machine/intr.h>
     49 #include <machine/bus.h>
     50 
     51 #include <dev/ic/mb86960reg.h>
     52 #include <dev/ic/mb86960var.h>
     53 
     54 #include <dev/pcmcia/pcmciareg.h>
     55 #include <dev/pcmcia/pcmciavar.h>
     56 #include <dev/pcmcia/pcmciadevs.h>
     57 
     58 int	mbe_pcmcia_match __P((struct device *, struct cfdata *, void *));
     59 void	mbe_pcmcia_attach __P((struct device *, struct device *, void *));
     60 int	mbe_pcmcia_detach __P((struct device *, int));
     61 
     62 struct mbe_pcmcia_softc {
     63 	struct	mb86960_softc sc_mb86960;	/* real "mb" softc */
     64 
     65 	/* PCMCIA-specific goo. */
     66 	struct	pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
     67 	int	sc_io_window;			/* our i/o window */
     68 	void	*sc_ih;				/* interrupt cookie */
     69 	struct	pcmcia_function *sc_pf;		/* our PCMCIA function */
     70 };
     71 
     72 struct cfattach mbe_pcmcia_ca = {
     73 	sizeof(struct mbe_pcmcia_softc), mbe_pcmcia_match, mbe_pcmcia_attach,
     74 	    mbe_pcmcia_detach, mb86960_activate
     75 };
     76 
     77 #if NetBSD <= 199712
     78 struct cfdriver mbe_cd = {			/* XXX shouldn't be here */
     79 	NULL, "mbe", DV_IFNET
     80 };
     81 #endif
     82 
     83 int	mbe_pcmcia_enable __P((struct mb86960_softc *));
     84 void	mbe_pcmcia_disable __P((struct mb86960_softc *));
     85 
     86 struct mbe_pcmcia_get_enaddr_args {
     87 	int got_enaddr;
     88 	u_int8_t enaddr[ETHER_ADDR_LEN];
     89 };
     90 int	mbe_pcmcia_get_enaddr __P((struct pcmcia_tuple *, void *));
     91 
     92 struct mbe_pcmcia_product {
     93 	u_int32_t	mpp_vendor;	/* vendor ID */
     94 	u_int32_t	mpp_product;	/* product ID */
     95 	int		mpp_expfunc;	/* exptected function */
     96 	u_int32_t	mpp_ioalign;	/* required alignment */
     97 	const char	*mpp_name;	/* product name */
     98 } mbe_pcmcia_products[] = {
     99 	{ PCMCIA_VENDOR_TDK,		PCMCIA_PRODUCT_TDK_LAK_CD021BX,
    100 	  0,				0,
    101 	  PCMCIA_STR_TDK_LAK_CD021BX },
    102 #if 0 /* XXX 86960-based? */
    103 	{ PCMCIA_VENDOR_TDK,		PCMCIA_PRODUCT_TDK_LAK_DFL9610,
    104 	  1,				0,
    105 	  PCMCIA_STR_TDK_LAK_DFL9610 }
    106 #endif
    107 
    108 	{ PCMCIA_VENDOR_FUJITSU,	PCMCIA_PRODUCT_FUJITSU_LA501,
    109 	  0,				0x20,
    110 	  PCMCIA_STR_FUJITSU_LA501 },
    111 
    112 	{ 0,				0,
    113 	  0,				0,
    114 	  NULL },
    115 };
    116 
    117 const struct mbe_pcmcia_product *mbe_pcmcia_lookup
    118     __P((const struct pcmcia_attach_args *pa));
    119 
    120 const struct mbe_pcmcia_product *
    121 mbe_pcmcia_lookup(pa)
    122 	const struct pcmcia_attach_args *pa;
    123 {
    124 	const struct mbe_pcmcia_product *mpp;
    125 
    126 	for (mpp = mbe_pcmcia_products; mpp->mpp_name != NULL; mpp++) {
    127 		if (pa->manufacturer == mpp->mpp_vendor &&
    128 		    pa->product == mpp->mpp_product &&
    129 		    pa->pf->number == mpp->mpp_expfunc)
    130 			return (mpp);
    131 	}
    132 	return (NULL);
    133 }
    134 
    135 int
    136 mbe_pcmcia_match(parent, match, aux)
    137 	struct device *parent;
    138 	struct cfdata *match;
    139 	void *aux;
    140 {
    141 	struct pcmcia_attach_args *pa = aux;
    142 
    143 	if (mbe_pcmcia_lookup(pa) != NULL)
    144 		return (1);
    145 
    146 	return (0);
    147 }
    148 
    149 void
    150 mbe_pcmcia_attach(parent, self, aux)
    151 	struct device *parent, *self;
    152 	void *aux;
    153 {
    154 	struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)self;
    155 	struct mb86960_softc *sc = &psc->sc_mb86960;
    156 	struct pcmcia_attach_args *pa = aux;
    157 	struct pcmcia_config_entry *cfe;
    158 	struct mbe_pcmcia_get_enaddr_args pgea;
    159 	const struct mbe_pcmcia_product *mpp;
    160 
    161 	mpp = mbe_pcmcia_lookup(pa);
    162 	if (mpp == NULL) {
    163 		printf("\n");
    164 		panic("mbe_pcmcia_attach: impossible");
    165 	}
    166 
    167 	psc->sc_pf = pa->pf;
    168 	cfe = pa->pf->cfe_head.sqh_first;
    169 
    170 	/* Enable the card. */
    171 	pcmcia_function_init(pa->pf, cfe);
    172 	if (pcmcia_function_enable(pa->pf)) {
    173 		printf(": function enable failed\n");
    174 		return;
    175 	}
    176 
    177 	/* Allocate and map i/o space for the card. */
    178 	if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
    179 	    cfe->iospace[0].length,
    180 	    mpp->mpp_ioalign ? mpp->mpp_ioalign : cfe->iospace[0].length,
    181 	    &psc->sc_pcioh)) {
    182 		printf(": can't allocate i/o space\n");
    183 		return;
    184 	}
    185 
    186 	sc->sc_bst = psc->sc_pcioh.iot;
    187 	sc->sc_bsh = psc->sc_pcioh.ioh;
    188 
    189 	sc->sc_enable = mbe_pcmcia_enable;
    190 	sc->sc_disable = mbe_pcmcia_disable;
    191 
    192 	/*
    193 	 * Don't bother checking flags; the back-end sets the chip
    194 	 * into 16-bit mode.
    195 	 */
    196 	if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_IO16, 0, cfe->iospace[0].length,
    197 	    &psc->sc_pcioh, &psc->sc_io_window)) {
    198 		printf(": can't map i/o space\n");
    199 		return;
    200 	}
    201 
    202 	printf(": %s\n", mpp->mpp_name);
    203 
    204 	/* Read station address. */
    205 	pgea.got_enaddr = 0;
    206 	if (pcmcia_scan_cis(parent, mbe_pcmcia_get_enaddr, &pgea) == -1) {
    207 		printf("%s: Couldn't read CIS to get ethernet address\n",
    208 		    sc->sc_dev.dv_xname);
    209 		return;
    210 	} else if (!pgea.got_enaddr) {
    211 		printf("%s: Couldn't get ethernet address from CIS\n",
    212 		    sc->sc_dev.dv_xname);
    213 		return;
    214 	} else
    215 #ifdef DIAGNOSTIC
    216 		printf("%s: Ethernet address from CIS: %s\n",
    217 		    sc->sc_dev.dv_xname, ether_sprintf(pgea.enaddr))
    218 #endif
    219 		;
    220 
    221 	/* Perform generic initialization. */
    222 	mb86960_attach(sc, MB86960_TYPE_86965, pgea.enaddr);
    223 
    224 	mb86960_config(sc, NULL, 0, 0);
    225 
    226 	pcmcia_function_disable(pa->pf);
    227 }
    228 
    229 int
    230 mbe_pcmcia_detach(self, flags)
    231 	struct device *self;
    232 	int flags;
    233 {
    234 	struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)self;
    235 
    236 	/* Unmap our i/o window. */
    237 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    238 
    239 	/* Free our i/o space. */
    240 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    241 
    242 #ifdef notyet
    243 	/*
    244 	 * Our softc is about to go away, so drop our reference
    245 	 * to the ifnet.
    246 	 */
    247 	if_delref(psc->sc_mb86960.sc_ec.ec_if);
    248 	return (0);
    249 #else
    250 	return (EBUSY);
    251 #endif
    252 }
    253 
    254 int
    255 mbe_pcmcia_enable(sc)
    256 	struct mb86960_softc *sc;
    257 {
    258 	struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)sc;
    259 
    260 	/* Establish the interrupt handler. */
    261 	psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, mb86960_intr,
    262 	    sc);
    263 	if (psc->sc_ih == NULL) {
    264 		printf("%s: couldn't establish interrupt handler\n",
    265 		    sc->sc_dev.dv_xname);
    266 		return (1);
    267 	}
    268 
    269 	return (pcmcia_function_enable(psc->sc_pf));
    270 }
    271 
    272 void
    273 mbe_pcmcia_disable(sc)
    274 	struct mb86960_softc *sc;
    275 {
    276 	struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)sc;
    277 
    278 	pcmcia_function_disable(psc->sc_pf);
    279 
    280 	pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    281 }
    282 
    283 int
    284 mbe_pcmcia_get_enaddr(tuple, arg)
    285 	struct pcmcia_tuple *tuple;
    286 	void *arg;
    287 {
    288 	struct mbe_pcmcia_get_enaddr_args *p = arg;
    289 	int i;
    290 
    291 	if (tuple->code == PCMCIA_CISTPL_FUNCE) {
    292 		if (tuple->length < 2) /* sub code and ether addr length */
    293 			return (0);
    294 
    295 		if ((pcmcia_tuple_read_1(tuple, 0) !=
    296 			PCMCIA_TPLFE_TYPE_LAN_NID) ||
    297 		    (pcmcia_tuple_read_1(tuple, 1) != ETHER_ADDR_LEN))
    298 			return (0);
    299 
    300 		for (i = 0; i < ETHER_ADDR_LEN; i++)
    301 			p->enaddr[i] = pcmcia_tuple_read_1(tuple, i + 2);
    302 		p->got_enaddr = 1;
    303 		return (1);
    304 	}
    305 	return (0);
    306 }
    307