Home | History | Annotate | Line # | Download | only in pcmcia
if_mbe_pcmcia.c revision 1.6
      1 /*	$NetBSD: if_mbe_pcmcia.c,v 1.6 1998/11/18 18:34:53 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 int
     93 mbe_pcmcia_match(parent, match, aux)
     94 	struct device *parent;
     95 	struct cfdata *match;
     96 	void *aux;
     97 {
     98 	struct pcmcia_attach_args *pa = aux;
     99 
    100 	if (pa->manufacturer == PCMCIA_VENDOR_TDK) {
    101 		switch (pa->product) {
    102 		case PCMCIA_PRODUCT_TDK_LAK_CD021BX:
    103 			if (pa->pf->number == 0)
    104 				return (1);
    105 			break;
    106 #if 0				/* XXX Is this card mb86960 based one? */
    107 		case PCMCIA_PRODUCT_TDK_DFL9610:
    108 			if (pa->pf->number == 1)
    109 				return (1);
    110 			break;
    111 #endif
    112 		}
    113 	}
    114 
    115 	return (0);
    116 }
    117 
    118 void
    119 mbe_pcmcia_attach(parent, self, aux)
    120 	struct device *parent, *self;
    121 	void *aux;
    122 {
    123 	struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)self;
    124 	struct mb86960_softc *sc = &psc->sc_mb86960;
    125 	struct pcmcia_attach_args *pa = aux;
    126 	struct pcmcia_config_entry *cfe;
    127 	struct mbe_pcmcia_get_enaddr_args pgea;
    128 	const char *model;
    129 
    130 	psc->sc_pf = pa->pf;
    131 	cfe = pa->pf->cfe_head.sqh_first;
    132 
    133 	/* Enable the card. */
    134 	pcmcia_function_init(pa->pf, cfe);
    135 	if (pcmcia_function_enable(pa->pf)) {
    136 		printf(": function enable failed\n");
    137 		return;
    138 	}
    139 
    140 	/* Allocate and map i/o space for the card. */
    141 	if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
    142 	    cfe->iospace[0].length, cfe->iospace[0].length, &psc->sc_pcioh)) {
    143 		printf(": can't allocate i/o space\n");
    144 		return;
    145 	}
    146 
    147 	sc->sc_bst = psc->sc_pcioh.iot;
    148 	sc->sc_bsh = psc->sc_pcioh.ioh;
    149 
    150 	sc->sc_enable = mbe_pcmcia_enable;
    151 	sc->sc_disable = mbe_pcmcia_disable;
    152 
    153 	if (pcmcia_io_map(pa->pf, (cfe->flags & PCMCIA_CFE_IO16) ?
    154 	    PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8, 0, cfe->iospace[0].length,
    155 	    &psc->sc_pcioh, &psc->sc_io_window)) {
    156 		printf(": can't map i/o space\n");
    157 		return;
    158 	}
    159 
    160 	switch (pa->product) {
    161 	case PCMCIA_PRODUCT_TDK_LAK_CD021BX:
    162 		model = PCMCIA_STR_TDK_LAK_CD021BX;
    163 		break;
    164 	case PCMCIA_PRODUCT_TDK_DFL9610:
    165 		model = PCMCIA_STR_TDK_DFL9610;
    166 		break;
    167 
    168 	default:
    169 		printf("%s: Unknown MB86960 PCMCIA ethernet card (%d)\n",
    170 		    sc->sc_dev.dv_xname, pa->product);
    171 		panic("unknown card");
    172 	}
    173 	printf(": %s\n", model);
    174 
    175 	/* Read station address. */
    176 	pgea.got_enaddr = 0;
    177 	if (pcmcia_scan_cis(parent, mbe_pcmcia_get_enaddr, &pgea) == -1) {
    178 		printf("%s: Couldn't read CIS to get ethernet address\n",
    179 		    sc->sc_dev.dv_xname);
    180 		return;
    181 	} else if (!pgea.got_enaddr) {
    182 		printf("%s: Couldn't get ethernet address from CIS\n",
    183 		    sc->sc_dev.dv_xname);
    184 		return;
    185 	} else
    186 #ifdef DIAGNOSTIC
    187 		printf("%s: Ethernet address from CIS: %s\n",
    188 		    sc->sc_dev.dv_xname, ether_sprintf(pgea.enaddr))
    189 #endif
    190 		;
    191 
    192 	/* Perform generic initialization. */
    193 	mb86960_attach(sc, MB86960_TYPE_86965, pgea.enaddr);
    194 
    195 	mb86960_config(sc, NULL, 0, 0);
    196 
    197 	pcmcia_function_disable(pa->pf);
    198 }
    199 
    200 int
    201 mbe_pcmcia_detach(self, flags)
    202 	struct device *self;
    203 	int flags;
    204 {
    205 	struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)self;
    206 
    207 	/* Unmap our i/o window. */
    208 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    209 
    210 	/* Free our i/o space. */
    211 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    212 
    213 #ifdef notyet
    214 	/*
    215 	 * Our softc is about to go away, so drop our reference
    216 	 * to the ifnet.
    217 	 */
    218 	if_delref(psc->sc_mb86960.sc_ec.ec_if);
    219 	return (0);
    220 #else
    221 	return (EBUSY);
    222 #endif
    223 }
    224 
    225 int
    226 mbe_pcmcia_enable(sc)
    227 	struct mb86960_softc *sc;
    228 {
    229 	struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)sc;
    230 
    231 	/* Establish the interrupt handler. */
    232 	psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, mb86960_intr,
    233 	    sc);
    234 	if (psc->sc_ih == NULL) {
    235 		printf("%s: couldn't establish interrupt handler\n",
    236 		    sc->sc_dev.dv_xname);
    237 		return (1);
    238 	}
    239 
    240 	return (pcmcia_function_enable(psc->sc_pf));
    241 }
    242 
    243 void
    244 mbe_pcmcia_disable(sc)
    245 	struct mb86960_softc *sc;
    246 {
    247 	struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)sc;
    248 
    249 	pcmcia_function_disable(psc->sc_pf);
    250 
    251 	pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    252 }
    253 
    254 int
    255 mbe_pcmcia_get_enaddr(tuple, arg)
    256 	struct pcmcia_tuple *tuple;
    257 	void *arg;
    258 {
    259 	struct mbe_pcmcia_get_enaddr_args *p = arg;
    260 	int i;
    261 
    262 	if (tuple->code == PCMCIA_CISTPL_FUNCE) {
    263 		if (tuple->length < 2) /* sub code and ether addr length */
    264 			return (0);
    265 
    266 		if ((pcmcia_tuple_read_1(tuple, 0) !=
    267 			PCMCIA_TPLFE_TYPE_LAN_NID) ||
    268 		    (pcmcia_tuple_read_1(tuple, 1) != ETHER_ADDR_LEN))
    269 			return (0);
    270 
    271 		for (i = 0; i < ETHER_ADDR_LEN; i++)
    272 			p->enaddr[i] = pcmcia_tuple_read_1(tuple, i + 2);
    273 		p->got_enaddr = 1;
    274 		return (1);
    275 	}
    276 	return (0);
    277 }
    278