Home | History | Annotate | Line # | Download | only in pcmcia
if_mbe_pcmcia.c revision 1.23
      1 /*	$NetBSD: if_mbe_pcmcia.c,v 1.23 2001/12/16 05:44:33 ichiro 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/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: if_mbe_pcmcia.c,v 1.23 2001/12/16 05:44:33 ichiro Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/device.h>
     45 #include <sys/socket.h>
     46 
     47 #include <net/if.h>
     48 #include <net/if_ether.h>
     49 #include <net/if_media.h>
     50 
     51 #include <machine/intr.h>
     52 #include <machine/bus.h>
     53 
     54 #include <dev/ic/mb86960reg.h>
     55 #include <dev/ic/mb86960var.h>
     56 
     57 #include <dev/pcmcia/pcmciareg.h>
     58 #include <dev/pcmcia/pcmciavar.h>
     59 #include <dev/pcmcia/pcmciadevs.h>
     60 
     61 int	mbe_pcmcia_match __P((struct device *, struct cfdata *, void *));
     62 void	mbe_pcmcia_attach __P((struct device *, struct device *, void *));
     63 int	mbe_pcmcia_detach __P((struct device *, int));
     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 	    mbe_pcmcia_detach, mb86960_activate
     78 };
     79 
     80 int	mbe_pcmcia_enable __P((struct mb86960_softc *));
     81 void	mbe_pcmcia_disable __P((struct mb86960_softc *));
     82 
     83 struct mbe_pcmcia_get_enaddr_args {
     84 	u_int8_t enaddr[ETHER_ADDR_LEN];
     85 	int maddr;
     86 };
     87 int	mbe_pcmcia_get_enaddr_from_cis __P((struct pcmcia_tuple *, void *));
     88 int	mbe_pcmcia_get_enaddr_from_mem __P((struct mbe_pcmcia_softc *,
     89 	    struct mbe_pcmcia_get_enaddr_args *));
     90 
     91 const struct mbe_pcmcia_product {
     92 	struct pcmcia_product mpp_product;
     93 	u_int32_t mpp_ioalign;			/* required alignment */
     94 	int mpp_enet_maddr;
     95 } mbe_pcmcia_products[] = {
     96 	{ { PCMCIA_STR_TDK_LAK_CD021BX,		PCMCIA_VENDOR_TDK,
     97 	    PCMCIA_PRODUCT_TDK_LAK_CD021BX,	0 },
     98 	  0, -1 },
     99 
    100 	{ { PCMCIA_STR_TDK_LAK_CF010,		PCMCIA_VENDOR_TDK,
    101 	    PCMCIA_PRODUCT_TDK_LAK_CF010,	0 },
    102 	  0, -1 },
    103 
    104 #if 0 /* XXX 86960-based? */
    105 	{ { PCMCIA_STR_TDK_LAK_DFL9610,		PCMCIA_VENDOR_TDK,
    106 	    PCMCIA_PRODUCT_TDK_LAK_DFL9610,	1 },
    107 	  0, -1 },
    108 #endif
    109 
    110 	{ { PCMCIA_STR_CONTEC_CNETPC,		PCMCIA_VENDOR_CONTEC,
    111 	    PCMCIA_PRODUCT_CONTEC_CNETPC,	0 },
    112 	  0, -1 },
    113 
    114 	{ { PCMCIA_STR_FUJITSU_LA501,		PCMCIA_VENDOR_FUJITSU,
    115 	    PCMCIA_PRODUCT_FUJITSU_LA501,	0 },
    116 	  0x20, -1 },
    117 
    118 	{ { PCMCIA_STR_FUJITSU_LA10S,		PCMCIA_VENDOR_FUJITSU,
    119 	    PCMCIA_PRODUCT_FUJITSU_LA10S,	0 },
    120 	  0, -1 },
    121 
    122 	{ { PCMCIA_STR_FUJITSU_ITCFJ182A,	PCMCIA_VENDOR_FUJITSU,
    123 	    PCMCIA_PRODUCT_FUJITSU_ITCFJ182A,	0 },
    124 	  0, 0x1cc },
    125 
    126 	{ { PCMCIA_STR_RATOC_REX_R280,		PCMCIA_VENDOR_RATOC,
    127 	    PCMCIA_PRODUCT_RATOC_REX_R280,	0 },
    128 	  0, 0x1fc },
    129 
    130 	{ { NULL } }
    131 };
    132 
    133 int
    134 mbe_pcmcia_match(parent, match, aux)
    135 	struct device *parent;
    136 	struct cfdata *match;
    137 	void *aux;
    138 {
    139 	struct pcmcia_attach_args *pa = aux;
    140 
    141 	if (pcmcia_product_lookup(pa,
    142 	    (const struct pcmcia_product *)mbe_pcmcia_products,
    143 	    sizeof mbe_pcmcia_products[0], NULL) != 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 	int rv;
    161 
    162 	mpp = (const struct mbe_pcmcia_product *)pcmcia_product_lookup(pa,
    163 	    (const struct pcmcia_product *)mbe_pcmcia_products,
    164 	    sizeof mbe_pcmcia_products[0], NULL);
    165 	if (mpp == NULL) {
    166 		printf("\n");
    167 		panic("mbe_pcmcia_attach: impossible");
    168 	}
    169 
    170 	psc->sc_pf = pa->pf;
    171 	cfe = pa->pf->cfe_head.sqh_first;
    172 
    173 	/* Enable the card. */
    174 	pcmcia_function_init(pa->pf, cfe);
    175 	if (pcmcia_function_enable(pa->pf)) {
    176 		printf(": function enable failed\n");
    177 		goto enable_failed;
    178 	}
    179 
    180 	/* Allocate and map i/o space for the card. */
    181 	if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
    182 	    cfe->iospace[0].length,
    183 	    mpp->mpp_ioalign ? mpp->mpp_ioalign : cfe->iospace[0].length,
    184 	    &psc->sc_pcioh)) {
    185 		printf(": can't allocate i/o space\n");
    186 		goto ioalloc_failed;
    187 	}
    188 
    189 	sc->sc_bst = psc->sc_pcioh.iot;
    190 	sc->sc_bsh = psc->sc_pcioh.ioh;
    191 
    192 	sc->sc_enable = mbe_pcmcia_enable;
    193 	sc->sc_disable = mbe_pcmcia_disable;
    194 
    195 	/*
    196 	 * Don't bother checking flags; the back-end sets the chip
    197 	 * into 16-bit mode.
    198 	 */
    199 	if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_IO16, 0, cfe->iospace[0].length,
    200 	    &psc->sc_pcioh, &psc->sc_io_window)) {
    201 		printf(": can't map i/o space\n");
    202 		goto iomap_failed;
    203 	}
    204 
    205 	printf(": %s\n", mpp->mpp_product.pp_name);
    206 
    207 	/* Read station address from mem or CIS. */
    208 	if (mpp->mpp_enet_maddr >= 0) {
    209 		pgea.maddr = mpp->mpp_enet_maddr;
    210 		if (mbe_pcmcia_get_enaddr_from_mem(psc, &pgea) != 0) {
    211 			printf("%s: Couldn't get ethernet address "
    212 			    "from mem\n", sc->sc_dev.dv_xname);
    213 			goto no_enaddr;
    214 		}
    215 	} else {
    216 		rv = pcmcia_scan_cis(parent,
    217 		    mbe_pcmcia_get_enaddr_from_cis, &pgea);
    218 		if (rv == -1) {
    219 			printf("%s: Couldn't read CIS to get ethernet "
    220 			    "address\n", sc->sc_dev.dv_xname);
    221 			goto no_enaddr;
    222 		} else if (rv == 0) {
    223 			printf("%s: Couldn't get ethernet address "
    224 			    "from CIS\n", sc->sc_dev.dv_xname);
    225 			goto no_enaddr;
    226 		}
    227 #ifdef DIAGNOSTIC
    228 		if (rv != 1) {
    229 			printf("%s: pcmcia_scan_cis returns %d\n",
    230 			    sc->sc_dev.dv_xname, rv);
    231 			panic("mbe_pcmcia_attach");
    232 		}
    233 		printf("%s: Ethernet address from CIS: %s\n",
    234 		    sc->sc_dev.dv_xname, ether_sprintf(pgea.enaddr));
    235 #endif
    236 	}
    237 
    238 	/* Perform generic initialization. */
    239 	mb86960_attach(sc, MB86960_TYPE_86965, pgea.enaddr);
    240 
    241 	mb86960_config(sc, NULL, 0, 0);
    242 
    243 	pcmcia_function_disable(pa->pf);
    244 	return;
    245 
    246  no_enaddr:
    247 	/* Unmap our i/o window. */
    248 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    249 
    250  iomap_failed:
    251 	/* Free our i/o space. */
    252 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    253 
    254  ioalloc_failed:
    255 	pcmcia_function_disable(pa->pf);
    256 
    257  enable_failed:
    258 	psc->sc_io_window = -1;
    259 }
    260 
    261 int
    262 mbe_pcmcia_detach(self, flags)
    263 	struct device *self;
    264 	int flags;
    265 {
    266 	struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)self;
    267 	int error;
    268 
    269 	if (psc->sc_io_window == -1)
    270 		/* Nothing to detach.  */
    271 		return (0);
    272 
    273 	error = mb86960_detach(&psc->sc_mb86960);
    274 	if (error != 0)
    275 		return (error);
    276 
    277 	/* Unmap our i/o window. */
    278 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    279 
    280 	/* Free our i/o space. */
    281 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    282 
    283 	return (0);
    284 }
    285 
    286 int
    287 mbe_pcmcia_enable(sc)
    288 	struct mb86960_softc *sc;
    289 {
    290 	struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)sc;
    291 
    292 	/* Establish the interrupt handler. */
    293 	psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, mb86960_intr,
    294 	    sc);
    295 	if (psc->sc_ih == NULL) {
    296 		printf("%s: couldn't establish interrupt handler\n",
    297 		    sc->sc_dev.dv_xname);
    298 		return (1);
    299 	}
    300 
    301 	if (pcmcia_function_enable(psc->sc_pf)) {
    302 		pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    303 		return (1);
    304 	}
    305 
    306 	return (0);
    307 }
    308 
    309 void
    310 mbe_pcmcia_disable(sc)
    311 	struct mb86960_softc *sc;
    312 {
    313 	struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)sc;
    314 
    315 	pcmcia_function_disable(psc->sc_pf);
    316 	pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    317 }
    318 
    319 int
    320 mbe_pcmcia_get_enaddr_from_cis(tuple, arg)
    321 	struct pcmcia_tuple *tuple;
    322 	void *arg;
    323 {
    324 	struct mbe_pcmcia_get_enaddr_args *p = arg;
    325 	int i;
    326 
    327 	if (tuple->code == PCMCIA_CISTPL_FUNCE) {
    328 		if (tuple->length < 2) /* sub code and ether addr length */
    329 			return (0);
    330 
    331 		if ((pcmcia_tuple_read_1(tuple, 0) !=
    332 			PCMCIA_TPLFE_TYPE_LAN_NID) ||
    333 		    (pcmcia_tuple_read_1(tuple, 1) != ETHER_ADDR_LEN))
    334 			return (0);
    335 
    336 		for (i = 0; i < ETHER_ADDR_LEN; i++)
    337 			p->enaddr[i] = pcmcia_tuple_read_1(tuple, i + 2);
    338 		return (1);
    339 	}
    340 	return (0);
    341 }
    342 
    343 int
    344 mbe_pcmcia_get_enaddr_from_mem(psc, ea)
    345 	struct mbe_pcmcia_softc *psc;
    346 	struct mbe_pcmcia_get_enaddr_args *ea;
    347 {
    348 	struct mb86960_softc *sc = &psc->sc_mb86960;
    349 	struct pcmcia_mem_handle pcmh;
    350 	bus_size_t offset;
    351 	int i, mwindow, rv = 1;
    352 
    353 	if (ea->maddr < 0)
    354 		goto bad_memaddr;
    355 
    356 	if (pcmcia_mem_alloc(psc->sc_pf, ETHER_ADDR_LEN * 2, &pcmh)) {
    357 		printf("%s: can't alloc mem for enet addr\n",
    358 		    sc->sc_dev.dv_xname);
    359 		goto memalloc_failed;
    360 	}
    361 
    362 	if (pcmcia_mem_map(psc->sc_pf, PCMCIA_MEM_ATTR, ea->maddr,
    363 	    ETHER_ADDR_LEN * 2, &pcmh, &offset, &mwindow)) {
    364 		printf("%s: can't map mem for enet addr\n",
    365 		    sc->sc_dev.dv_xname);
    366 		goto memmap_failed;
    367 	}
    368 
    369 	for (i = 0; i < ETHER_ADDR_LEN; i++)
    370 		ea->enaddr[i] = bus_space_read_1(pcmh.memt, pcmh.memh,
    371 		    offset + (i * 2));
    372 
    373 	rv = 0;
    374 	pcmcia_mem_unmap(psc->sc_pf, mwindow);
    375 memmap_failed:
    376 	pcmcia_mem_free(psc->sc_pf, &pcmh);
    377 memalloc_failed:
    378 bad_memaddr:
    379 
    380 	return (rv);
    381 }
    382