Home | History | Annotate | Line # | Download | only in pcmcia
if_mbe_pcmcia.c revision 1.5
      1 /*	$NetBSD: if_mbe_pcmcia.c,v 1.5 1998/11/18 18:15:56 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 int	mbe_pcmcia_activate __P((struct device *, enum devact));
     62 
     63 struct mbe_pcmcia_softc {
     64 	struct	mb86960_softc sc_mb86960;	/* real "mb" softc */
     65 
     66 	/* PCMCIA-specific goo. */
     67 	struct	pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
     68 	int	sc_io_window;			/* our i/o window */
     69 	void	*sc_ih;				/* interrupt cookie */
     70 	struct	pcmcia_function *sc_pf;		/* our PCMCIA function */
     71 };
     72 
     73 struct cfattach mbe_pcmcia_ca = {
     74 	sizeof(struct mbe_pcmcia_softc), mbe_pcmcia_match, mbe_pcmcia_attach,
     75 	    mbe_pcmcia_detach, mbe_pcmcia_activate
     76 };
     77 
     78 #if NetBSD <= 199712
     79 struct cfdriver mbe_cd = {			/* XXX shouldn't be here */
     80 	NULL, "mbe", DV_IFNET
     81 };
     82 #endif
     83 
     84 int	mbe_pcmcia_enable __P((struct mb86960_softc *));
     85 void	mbe_pcmcia_disable __P((struct mb86960_softc *));
     86 
     87 struct mbe_pcmcia_get_enaddr_args {
     88 	int got_enaddr;
     89 	u_int8_t enaddr[ETHER_ADDR_LEN];
     90 };
     91 int	mbe_pcmcia_get_enaddr __P((struct pcmcia_tuple *, void *));
     92 
     93 int
     94 mbe_pcmcia_match(parent, match, aux)
     95 	struct device *parent;
     96 	struct cfdata *match;
     97 	void *aux;
     98 {
     99 	struct pcmcia_attach_args *pa = aux;
    100 
    101 	if (pa->manufacturer == PCMCIA_VENDOR_TDK) {
    102 		switch (pa->product) {
    103 		case PCMCIA_PRODUCT_TDK_LAK_CD021BX:
    104 			if (pa->pf->number == 0)
    105 				return (1);
    106 			break;
    107 #if 0				/* XXX Is this card mb86960 based one? */
    108 		case PCMCIA_PRODUCT_TDK_DFL9610:
    109 			if (pa->pf->number == 1)
    110 				return (1);
    111 			break;
    112 #endif
    113 		}
    114 	}
    115 
    116 	return (0);
    117 }
    118 
    119 void
    120 mbe_pcmcia_attach(parent, self, aux)
    121 	struct device *parent, *self;
    122 	void *aux;
    123 {
    124 	struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)self;
    125 	struct mb86960_softc *sc = &psc->sc_mb86960;
    126 	struct pcmcia_attach_args *pa = aux;
    127 	struct pcmcia_config_entry *cfe;
    128 	struct mbe_pcmcia_get_enaddr_args pgea;
    129 	const char *model;
    130 
    131 	psc->sc_pf = pa->pf;
    132 	cfe = pa->pf->cfe_head.sqh_first;
    133 
    134 	/* Enable the card. */
    135 	pcmcia_function_init(pa->pf, cfe);
    136 	if (pcmcia_function_enable(pa->pf)) {
    137 		printf(": function enable failed\n");
    138 		return;
    139 	}
    140 
    141 	/* Allocate and map i/o space for the card. */
    142 	if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
    143 	    cfe->iospace[0].length, cfe->iospace[0].length, &psc->sc_pcioh)) {
    144 		printf(": can't allocate i/o space\n");
    145 		return;
    146 	}
    147 
    148 	sc->sc_bst = psc->sc_pcioh.iot;
    149 	sc->sc_bsh = psc->sc_pcioh.ioh;
    150 
    151 	sc->sc_enable = mbe_pcmcia_enable;
    152 	sc->sc_disable = mbe_pcmcia_disable;
    153 
    154 	if (pcmcia_io_map(pa->pf, (cfe->flags & PCMCIA_CFE_IO16) ?
    155 	    PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8, 0, cfe->iospace[0].length,
    156 	    &psc->sc_pcioh, &psc->sc_io_window)) {
    157 		printf(": can't map i/o space\n");
    158 		return;
    159 	}
    160 
    161 	switch (pa->product) {
    162 	case PCMCIA_PRODUCT_TDK_LAK_CD021BX:
    163 		model = PCMCIA_STR_TDK_LAK_CD021BX;
    164 		break;
    165 	case PCMCIA_PRODUCT_TDK_DFL9610:
    166 		model = PCMCIA_STR_TDK_DFL9610;
    167 		break;
    168 
    169 	default:
    170 		printf("%s: Unknown MB86960 PCMCIA ethernet card (%d)\n",
    171 		    sc->sc_dev.dv_xname, pa->product);
    172 		panic("unknown card");
    173 	}
    174 	printf(": %s\n", model);
    175 
    176 	/* Read station address. */
    177 	pgea.got_enaddr = 0;
    178 	if (pcmcia_scan_cis(parent, mbe_pcmcia_get_enaddr, &pgea) == -1) {
    179 		printf("%s: Couldn't read CIS to get ethernet address\n",
    180 		    sc->sc_dev.dv_xname);
    181 		return;
    182 	} else if (!pgea.got_enaddr) {
    183 		printf("%s: Couldn't get ethernet address from CIS\n",
    184 		    sc->sc_dev.dv_xname);
    185 		return;
    186 	} else
    187 #ifdef DIAGNOSTIC
    188 		printf("%s: Ethernet address from CIS: %s\n",
    189 		    sc->sc_dev.dv_xname, ether_sprintf(pgea.enaddr))
    190 #endif
    191 		;
    192 
    193 	/* Perform generic initialization. */
    194 	mb86960_attach(sc, MB86960_TYPE_86965, pgea.enaddr);
    195 
    196 	mb86960_config(sc, NULL, 0, 0);
    197 
    198 	pcmcia_function_disable(pa->pf);
    199 }
    200 
    201 int
    202 mbe_pcmcia_detach(self, flags)
    203 	struct device *self;
    204 	int flags;
    205 {
    206 	struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)self;
    207 
    208 	/* Unmap our i/o window. */
    209 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    210 
    211 	/* Free our i/o space. */
    212 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    213 
    214 #ifdef notyet
    215 	/*
    216 	 * Our softc is about to go away, so drop our reference
    217 	 * to the ifnet.
    218 	 */
    219 	if_delref(psc->sc_mb86960.sc_ec.ec_if);
    220 	return (0);
    221 #else
    222 	return (EBUSY);
    223 #endif
    224 }
    225 
    226 int
    227 mbe_pcmcia_activate(self, act)
    228 	struct device *self;
    229 	enum devact act;
    230 {
    231 	struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)self;
    232 	struct mb86960_softc *sc = &psc->sc_mb86960;
    233 	int rv = 0;
    234 
    235 	switch (act) {
    236 	case DVACT_ACTIVATE:
    237 		rv = EOPNOTSUPP;
    238 		break;
    239 
    240 	case DVACT_DEACTIVATE:
    241 #ifdef notyet
    242 		/* First, kill off the interface. */
    243 		if_detach(sc->sc_ec.ec_if);
    244 #endif
    245 
    246 		/* Now disable the interface.  This releases our interrupt. */
    247 		mb86960_disable(sc);
    248 		break;
    249 	}
    250 	return (rv);
    251 }
    252 
    253 int
    254 mbe_pcmcia_enable(sc)
    255 	struct mb86960_softc *sc;
    256 {
    257 	struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)sc;
    258 
    259 	/* Establish the interrupt handler. */
    260 	psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, mb86960_intr,
    261 	    sc);
    262 	if (psc->sc_ih == NULL) {
    263 		printf("%s: couldn't establish interrupt handler\n",
    264 		    sc->sc_dev.dv_xname);
    265 		return (1);
    266 	}
    267 
    268 	return (pcmcia_function_enable(psc->sc_pf));
    269 }
    270 
    271 void
    272 mbe_pcmcia_disable(sc)
    273 	struct mb86960_softc *sc;
    274 {
    275 	struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)sc;
    276 
    277 	pcmcia_function_disable(psc->sc_pf);
    278 
    279 	pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    280 }
    281 
    282 int
    283 mbe_pcmcia_get_enaddr(tuple, arg)
    284 	struct pcmcia_tuple *tuple;
    285 	void *arg;
    286 {
    287 	struct mbe_pcmcia_get_enaddr_args *p = arg;
    288 	int i;
    289 
    290 	if (tuple->code == PCMCIA_CISTPL_FUNCE) {
    291 		if (tuple->length < 2) /* sub code and ether addr length */
    292 			return (0);
    293 
    294 		if ((pcmcia_tuple_read_1(tuple, 0) !=
    295 			PCMCIA_TPLFE_TYPE_LAN_NID) ||
    296 		    (pcmcia_tuple_read_1(tuple, 1) != ETHER_ADDR_LEN))
    297 			return (0);
    298 
    299 		for (i = 0; i < ETHER_ADDR_LEN; i++)
    300 			p->enaddr[i] = pcmcia_tuple_read_1(tuple, i + 2);
    301 		p->got_enaddr = 1;
    302 		return (1);
    303 	}
    304 	return (0);
    305 }
    306