Home | History | Annotate | Line # | Download | only in pcmcia
if_mbe_pcmcia.c revision 1.2
      1 /*	$NetBSD: if_mbe_pcmcia.c,v 1.2 1998/06/09 07:32:55 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 
     57 
     58 #define	PCMCIA_MANUFACTURER_TDK		0x0105
     59 #define	PCMCIA_PRODUCT_TDK_LAK_CD021BX	0x0200
     60 #define	PCMCIA_PRODUCT_TDK_DFL9610	0x0d0a
     61 
     62 int	mbe_pcmcia_match __P((struct device *, struct cfdata *, void *));
     63 void	mbe_pcmcia_attach __P((struct device *, struct device *, void *));
     64 
     65 struct mbe_pcmcia_softc {
     66 	struct	mb86960_softc sc_mb86960;	/* real "mb" softc */
     67 
     68 	/* PCMCIA-specific goo. */
     69 	struct	pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
     70 	int	sc_io_window;			/* our i/o window */
     71 	void	*sc_ih;				/* interrupt cookie */
     72 	struct	pcmcia_function *sc_pf;		/* our PCMCIA function */
     73 };
     74 
     75 struct cfattach mbe_pcmcia_ca = {
     76 	sizeof(struct mbe_pcmcia_softc), mbe_pcmcia_match, mbe_pcmcia_attach
     77 };
     78 
     79 #if NetBSD <= 199712
     80 struct cfdriver mbe_cd = {			/* XXX shouldn't be here */
     81 	NULL, "mbe", DV_IFNET
     82 };
     83 #endif
     84 
     85 int	mbe_pcmcia_enable __P((struct mb86960_softc *));
     86 void	mbe_pcmcia_disable __P((struct mb86960_softc *));
     87 
     88 struct mbe_pcmcia_get_enaddr_args {
     89 	int got_enaddr;
     90 	u_int8_t enaddr[ETHER_ADDR_LEN];
     91 };
     92 int	mbe_pcmcia_get_enaddr __P((struct pcmcia_tuple *, void *));
     93 
     94 int
     95 mbe_pcmcia_match(parent, match, aux)
     96 	struct device *parent;
     97 	struct cfdata *match;
     98 	void *aux;
     99 {
    100 	struct pcmcia_attach_args *pa = aux;
    101 
    102 	if (pa->manufacturer == PCMCIA_MANUFACTURER_TDK) {
    103 		switch (pa->product) {
    104 		case PCMCIA_PRODUCT_TDK_LAK_CD021BX:
    105 			if (pa->pf->number == 0)
    106 				return (1);
    107 			break;
    108 #if 0				/* XXX Is this card mb86960 based one? */
    109 		case PCMCIA_PRODUCT_TDK_DFL9610:
    110 			if (pa->pf->number == 1)
    111 				return (1);
    112 			break;
    113 #endif
    114 		}
    115 	}
    116 
    117 	return (0);
    118 }
    119 
    120 void
    121 mbe_pcmcia_attach(parent, self, aux)
    122 	struct device *parent, *self;
    123 	void *aux;
    124 {
    125 	struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)self;
    126 	struct mb86960_softc *sc = &psc->sc_mb86960;
    127 	struct pcmcia_attach_args *pa = aux;
    128 	struct pcmcia_config_entry *cfe;
    129 	struct mbe_pcmcia_get_enaddr_args pgea;
    130 	const char *model;
    131 
    132 	psc->sc_pf = pa->pf;
    133 	cfe = pa->pf->cfe_head.sqh_first;
    134 
    135 	/* Enable the card. */
    136 	pcmcia_function_init(pa->pf, cfe);
    137 	if (pcmcia_function_enable(pa->pf)) {
    138 		printf(": function enable failed\n");
    139 		return;
    140 	}
    141 
    142 	/* Allocate and map i/o space for the card. */
    143 	if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
    144 	    cfe->iospace[0].length, cfe->iospace[0].length, &psc->sc_pcioh)) {
    145 		printf(": can't allocate i/o space\n");
    146 		return;
    147 	}
    148 
    149 	sc->sc_bst = psc->sc_pcioh.iot;
    150 	sc->sc_bsh = psc->sc_pcioh.ioh;
    151 
    152 	sc->sc_enable = mbe_pcmcia_enable;
    153 	sc->sc_disable = mbe_pcmcia_disable;
    154 
    155 	if (pcmcia_io_map(pa->pf, (cfe->flags & PCMCIA_CFE_IO16) ?
    156 	    PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8, 0, cfe->iospace[0].length,
    157 	    &psc->sc_pcioh, &psc->sc_io_window)) {
    158 		printf(": can't map i/o space\n");
    159 		return;
    160 	}
    161 
    162 	switch (pa->product) {
    163 	case PCMCIA_PRODUCT_TDK_LAK_CD021BX:
    164 		model = "TDK LAK-CD021BX Ethernet";
    165 		break;
    166 	case PCMCIA_PRODUCT_TDK_DFL9610:
    167 		model = "TDK DFL9610 Ethernet & Digital Cellular";
    168 		break;
    169 
    170 	default:
    171 		printf("%s: Unknown MB86960 PCMCIA ethernet card (%d)\n",
    172 		    sc->sc_dev.dv_xname, pa->product);
    173 		panic("unknown card");
    174 	}
    175 	printf(": %s\n", model);
    176 
    177 	/* Read station address. */
    178 	pgea.got_enaddr = 0;
    179 	if (pcmcia_scan_cis(parent, mbe_pcmcia_get_enaddr, &pgea) == -1) {
    180 		printf("%s: Couldn't read CIS to get ethernet address\n",
    181 		    sc->sc_dev.dv_xname);
    182 		return;
    183 	} else if (!pgea.got_enaddr) {
    184 		printf("%s: Couldn't get ethernet address from CIS\n",
    185 		    sc->sc_dev.dv_xname);
    186 		return;
    187 	} else
    188 #ifdef DIAGNOSTIC
    189 		printf("%s: Ethernet address from CIS: %s\n",
    190 		    sc->sc_dev.dv_xname, ether_sprintf(pgea.enaddr))
    191 #endif
    192 		;
    193 
    194 	/* Perform generic initialization. */
    195 	mb86960_attach(sc, MB86960_TYPE_86965, pgea.enaddr);
    196 
    197 	mb86960_config(sc, NULL, 0, 0);
    198 
    199 	pcmcia_function_disable(pa->pf);
    200 }
    201 
    202 int
    203 mbe_pcmcia_enable(sc)
    204 	struct mb86960_softc *sc;
    205 {
    206 	struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)sc;
    207 
    208 	/* Establish the interrupt handler. */
    209 	psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, mb86960_intr,
    210 	    sc);
    211 	if (psc->sc_ih == NULL) {
    212 		printf("%s: couldn't establish interrupt handler\n",
    213 		    sc->sc_dev.dv_xname);
    214 		return (1);
    215 	}
    216 
    217 	return (pcmcia_function_enable(psc->sc_pf));
    218 }
    219 
    220 void
    221 mbe_pcmcia_disable(sc)
    222 	struct mb86960_softc *sc;
    223 {
    224 	struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)sc;
    225 
    226 	pcmcia_function_disable(psc->sc_pf);
    227 
    228 	pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    229 }
    230 
    231 int
    232 mbe_pcmcia_get_enaddr(tuple, arg)
    233 	struct pcmcia_tuple *tuple;
    234 	void *arg;
    235 {
    236 	struct mbe_pcmcia_get_enaddr_args *p = arg;
    237 	int i;
    238 
    239 	if (tuple->code == PCMCIA_CISTPL_FUNCE) {
    240 		if (tuple->length < 2) /* sub code and ether addr length */
    241 			return (0);
    242 
    243 		if ((pcmcia_tuple_read_1(tuple, 0) !=
    244 			PCMCIA_TPLFE_TYPE_LAN_NID) ||
    245 		    (pcmcia_tuple_read_1(tuple, 1) != ETHER_ADDR_LEN))
    246 			return (0);
    247 
    248 		for (i = 0; i < ETHER_ADDR_LEN; i++)
    249 			p->enaddr[i] = pcmcia_tuple_read_1(tuple, i + 2);
    250 		p->got_enaddr = 1;
    251 		return (1);
    252 	}
    253 	return (0);
    254 }
    255