Home | History | Annotate | Line # | Download | only in pcmcia
if_mbe_pcmcia.c revision 1.14
      1 /*	$NetBSD: if_mbe_pcmcia.c,v 1.14 2000/02/04 03:38:06 enami 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 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 	u_int8_t enaddr[ETHER_ADDR_LEN];
     88 };
     89 int	mbe_pcmcia_get_enaddr __P((struct pcmcia_tuple *, void *));
     90 
     91 const struct mbe_pcmcia_product {
     92 	struct pcmcia_product mpp_product;
     93 	u_int32_t	mpp_ioalign;	/* required alignment */
     94 } mbe_pcmcia_products[] = {
     95 	{ { PCMCIA_STR_TDK_LAK_CD021BX,		PCMCIA_VENDOR_TDK,
     96 	    PCMCIA_PRODUCT_TDK_LAK_CD021BX,	0 },
     97 	  0 },
     98 
     99 	{ { PCMCIA_STR_TDK_LAK_CF010,		PCMCIA_VENDOR_TDK,
    100 	    PCMCIA_PRODUCT_TDK_LAK_CF010,	0 },
    101 	  0 },
    102 #if 0 /* XXX 86960-based? */
    103 	{ { PCMCIA_STR_TDK_LAK_DFL9610,		PCMCIA_VENDOR_TDK,
    104 	    PCMCIA_PRODUCT_TDK_LAK_DFL9610,	1 },
    105 	  0 },
    106 #endif
    107 
    108 	{ { PCMCIA_STR_CONTEC_CNETPC,		PCMCIA_VENDOR_CONTEC,
    109 	    PCMCIA_PRODUCT_CONTEC_CNETPC,	0 },
    110 	  0 },
    111 
    112 	{ { PCMCIA_STR_FUJITSU_LA501,		PCMCIA_VENDOR_FUJITSU,
    113 	    PCMCIA_PRODUCT_FUJITSU_LA501,	0 },
    114 	  0x20 },
    115 
    116 	{ { PCMCIA_STR_FUJITSU_LA10S,		PCMCIA_VENDOR_FUJITSU,
    117 	    PCMCIA_PRODUCT_FUJITSU_LA10S,	0 },
    118 	  0 },
    119 
    120 	{ { NULL } }
    121 };
    122 
    123 int
    124 mbe_pcmcia_match(parent, match, aux)
    125 	struct device *parent;
    126 	struct cfdata *match;
    127 	void *aux;
    128 {
    129 	struct pcmcia_attach_args *pa = aux;
    130 
    131 	if (pcmcia_product_lookup(pa,
    132 	    (const struct pcmcia_product *)mbe_pcmcia_products,
    133 	    sizeof mbe_pcmcia_products[0], NULL) != NULL)
    134 		return (1);
    135 
    136 	return (0);
    137 }
    138 
    139 void
    140 mbe_pcmcia_attach(parent, self, aux)
    141 	struct device *parent, *self;
    142 	void *aux;
    143 {
    144 	struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)self;
    145 	struct mb86960_softc *sc = &psc->sc_mb86960;
    146 	struct pcmcia_attach_args *pa = aux;
    147 	struct pcmcia_config_entry *cfe;
    148 	struct mbe_pcmcia_get_enaddr_args pgea;
    149 	const struct mbe_pcmcia_product *mpp;
    150 	int rv;
    151 
    152 	mpp = (const struct mbe_pcmcia_product *)pcmcia_product_lookup(pa,
    153 	    (const struct pcmcia_product *)mbe_pcmcia_products,
    154 	    sizeof mbe_pcmcia_products[0], NULL);
    155 	if (mpp == NULL) {
    156 		printf("\n");
    157 		panic("mbe_pcmcia_attach: impossible");
    158 	}
    159 
    160 	psc->sc_pf = pa->pf;
    161 	cfe = pa->pf->cfe_head.sqh_first;
    162 	psc->sc_io_window = -1;
    163 
    164 	/* Enable the card. */
    165 	pcmcia_function_init(pa->pf, cfe);
    166 	if (pcmcia_function_enable(pa->pf)) {
    167 		printf(": function enable failed\n");
    168 		return;
    169 	}
    170 
    171 	/* Allocate and map i/o space for the card. */
    172 	if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
    173 	    cfe->iospace[0].length,
    174 	    mpp->mpp_ioalign ? mpp->mpp_ioalign : cfe->iospace[0].length,
    175 	    &psc->sc_pcioh)) {
    176 		printf(": can't allocate i/o space\n");
    177 		goto ioalloc_failed;
    178 	}
    179 
    180 	sc->sc_bst = psc->sc_pcioh.iot;
    181 	sc->sc_bsh = psc->sc_pcioh.ioh;
    182 
    183 	sc->sc_enable = mbe_pcmcia_enable;
    184 	sc->sc_disable = mbe_pcmcia_disable;
    185 
    186 	/*
    187 	 * Don't bother checking flags; the back-end sets the chip
    188 	 * into 16-bit mode.
    189 	 */
    190 	if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_IO16, 0, cfe->iospace[0].length,
    191 	    &psc->sc_pcioh, &psc->sc_io_window)) {
    192 		printf(": can't map i/o space\n");
    193 		goto iomap_failed;
    194 	}
    195 
    196 	printf(": %s\n", mpp->mpp_product.pp_name);
    197 
    198 	/* Read station address from CIS. */
    199 	rv = pcmcia_scan_cis(parent, mbe_pcmcia_get_enaddr, &pgea);
    200 	if (rv == -1) {
    201 		printf("%s: Couldn't read CIS to get ethernet address\n",
    202 		    sc->sc_dev.dv_xname);
    203 		goto no_enaddr;
    204 	} else if (rv == 0) {
    205 		printf("%s: Couldn't get ethernet address from CIS\n",
    206 		    sc->sc_dev.dv_xname);
    207 		goto no_enaddr;
    208 	}
    209 
    210 #ifdef DIAGNOSTIC
    211 	if (rv != 1) {
    212 		printf("%s: pcmcia_scan_cis returns %d\n", sc->sc_dev.dv_xname,
    213 		    rv);
    214 		panic("mbe_pcmcia_attach");
    215 	}
    216 	printf("%s: Ethernet address from CIS: %s\n",
    217 	    sc->sc_dev.dv_xname, ether_sprintf(pgea.enaddr));
    218 #endif
    219 
    220 	/* Perform generic initialization. */
    221 	mb86960_attach(sc, MB86960_TYPE_86965, pgea.enaddr);
    222 
    223 	mb86960_config(sc, NULL, 0, 0);
    224 
    225 	pcmcia_function_disable(pa->pf);
    226 	return;
    227 
    228  no_enaddr:
    229 	/* Unmap our i/o window. */
    230 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    231 
    232  iomap_failed:
    233 	/* Free our i/o space. */
    234 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    235 
    236  ioalloc_failed:
    237 	pcmcia_function_disable(pa->pf);
    238 }
    239 
    240 int
    241 mbe_pcmcia_detach(self, flags)
    242 	struct device *self;
    243 	int flags;
    244 {
    245 	struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)self;
    246 	int error;
    247 
    248 	if (psc->sc_io_window == -1)
    249 		/* Nothing to detach since we've failed to attach.  */
    250 		return (0);
    251 
    252 	error = mb86960_detach(&psc->sc_mb86960);
    253 	if (error != 0)
    254 		return (error);
    255 
    256 	/* Unmap our i/o window. */
    257 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    258 
    259 	/* Free our i/o space. */
    260 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    261 
    262 	return (0);
    263 }
    264 
    265 int
    266 mbe_pcmcia_enable(sc)
    267 	struct mb86960_softc *sc;
    268 {
    269 	struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)sc;
    270 
    271 #ifdef DIAGNOSTIC
    272 	if (psc->sc_ih != NULL) {
    273 		printf("%s: interrupt is already established\n",
    274 		    sc->sc_dev.dv_xname);
    275 		panic("mbe_pcmcia_enable");
    276 	}
    277 #endif
    278 	/* Establish the interrupt handler. */
    279 	psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, mb86960_intr,
    280 	    sc);
    281 	if (psc->sc_ih == NULL) {
    282 		printf("%s: couldn't establish interrupt handler\n",
    283 		    sc->sc_dev.dv_xname);
    284 		return (1);
    285 	}
    286 
    287 	return (pcmcia_function_enable(psc->sc_pf));
    288 }
    289 
    290 void
    291 mbe_pcmcia_disable(sc)
    292 	struct mb86960_softc *sc;
    293 {
    294 	struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)sc;
    295 
    296 	pcmcia_function_disable(psc->sc_pf);
    297 
    298 	if (psc->sc_ih != NULL)
    299 		pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    300 	psc->sc_ih = NULL;
    301 }
    302 
    303 int
    304 mbe_pcmcia_get_enaddr(tuple, arg)
    305 	struct pcmcia_tuple *tuple;
    306 	void *arg;
    307 {
    308 	struct mbe_pcmcia_get_enaddr_args *p = arg;
    309 	int i;
    310 
    311 	if (tuple->code == PCMCIA_CISTPL_FUNCE) {
    312 		if (tuple->length < 2) /* sub code and ether addr length */
    313 			return (0);
    314 
    315 		if ((pcmcia_tuple_read_1(tuple, 0) !=
    316 			PCMCIA_TPLFE_TYPE_LAN_NID) ||
    317 		    (pcmcia_tuple_read_1(tuple, 1) != ETHER_ADDR_LEN))
    318 			return (0);
    319 
    320 		for (i = 0; i < ETHER_ADDR_LEN; i++)
    321 			p->enaddr[i] = pcmcia_tuple_read_1(tuple, i + 2);
    322 		return (1);
    323 	}
    324 	return (0);
    325 }
    326