Home | History | Annotate | Line # | Download | only in pcmcia
if_mbe_pcmcia.c revision 1.31
      1 /*	$NetBSD: if_mbe_pcmcia.c,v 1.31 2004/08/08 23:17:13 mycroft 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.31 2004/08/08 23:17:13 mycroft 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 static const struct mbe_pcmcia_product
     66 		*mbe_pcmcia_lookup __P((struct pcmcia_attach_args *pa));
     67 
     68 struct mbe_pcmcia_softc {
     69 	struct	mb86960_softc sc_mb86960;	/* real "mb" softc */
     70 
     71 	/* PCMCIA-specific goo. */
     72 	struct	pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
     73 	int	sc_io_window;			/* our i/o window */
     74 	void	*sc_ih;				/* interrupt cookie */
     75 	struct	pcmcia_function *sc_pf;		/* our PCMCIA function */
     76 };
     77 
     78 CFATTACH_DECL(mbe_pcmcia, sizeof(struct mbe_pcmcia_softc),
     79     mbe_pcmcia_match, mbe_pcmcia_attach, mbe_pcmcia_detach, mb86960_activate);
     80 
     81 int	mbe_pcmcia_enable __P((struct mb86960_softc *));
     82 void	mbe_pcmcia_disable __P((struct mb86960_softc *));
     83 
     84 struct mbe_pcmcia_get_enaddr_args {
     85 	u_int8_t enaddr[ETHER_ADDR_LEN];
     86 	int maddr;
     87 };
     88 int	mbe_pcmcia_get_enaddr_from_cis __P((struct pcmcia_tuple *, void *));
     89 int	mbe_pcmcia_get_enaddr_from_mem __P((struct mbe_pcmcia_softc *,
     90 	    struct mbe_pcmcia_get_enaddr_args *));
     91 int	mbe_pcmcia_get_enaddr_from_io __P((struct mbe_pcmcia_softc *,
     92 	    struct mbe_pcmcia_get_enaddr_args *));
     93 
     94 static const struct mbe_pcmcia_product {
     95 	const char	*mpp_name;		/* product name */
     96 	u_int32_t	mpp_vendor;		/* vendor ID */
     97 	u_int32_t	mpp_product;		/* product ID */
     98 	const char	*mpp_cisinfo[4];	/* CIS information */
     99 	u_int32_t	mpp_ioalign;		/* required alignment */
    100 	int		mpp_enet_maddr;
    101 	int		flags;
    102 #define MBH10302	0x0001			/* FUJITSU MBH10302 */
    103 } mbe_pcmcia_products[] = {
    104 	{ PCMCIA_STR_TDK_LAK_CD021BX,		PCMCIA_VENDOR_TDK,
    105 	  PCMCIA_PRODUCT_TDK_LAK_CD021BX,	PCMCIA_CIS_TDK_LAK_CD021BX,
    106 	  0, -1 },
    107 
    108 	{ PCMCIA_STR_TDK_LAK_CF010,		PCMCIA_VENDOR_TDK,
    109 	  PCMCIA_PRODUCT_TDK_LAK_CF010,		PCMCIA_CIS_TDK_LAK_CF010,
    110 	  0, -1 },
    111 
    112 #if 0 /* XXX 86960-based? */
    113 	{ PCMCIA_STR_TDK_LAK_DFL9610,		PCMCIA_VENDOR_TDK,
    114 	  PCMCIA_PRODUCT_TDK_LAK_DFL9610,	PCMCIA_CIS_TDK_DFL9610,
    115 	  0, -1, MBH10302 /* XXX */ },
    116 #endif
    117 
    118 	{ PCMCIA_STR_CONTEC_CNETPC,		PCMCIA_VENDOR_CONTEC,
    119 	  PCMCIA_PRODUCT_CONTEC_CNETPC,		PCMCIA_CIS_CONTEC_CNETPC,
    120 	  0, -1 },
    121 
    122 	{ PCMCIA_STR_FUJITSU_LA501,		PCMCIA_VENDOR_FUJITSU,
    123 	  PCMCIA_PRODUCT_FUJITSU_LA501,		PCMCIA_CIS_FUJITSU_LA501,
    124 	  0x20, -1 },
    125 
    126 	{ PCMCIA_STR_FUJITSU_FMV_J181,		PCMCIA_VENDOR_FUJITSU,
    127 	  PCMCIA_PRODUCT_FUJITSU_FMV_J181,	PCMCIA_CIS_FUJITSU_FMV_J181,
    128 	  0x20, -1, MBH10302 },
    129 
    130 	{ PCMCIA_STR_FUJITSU_FMV_J182,		PCMCIA_VENDOR_FUJITSU,
    131 	  PCMCIA_PRODUCT_FUJITSU_FMV_J182,	PCMCIA_CIS_FUJITSU_FMV_J182,
    132 	  0, 0xf2c },
    133 
    134 	{ PCMCIA_STR_FUJITSU_FMV_J182A,		PCMCIA_VENDOR_FUJITSU,
    135 	  PCMCIA_PRODUCT_FUJITSU_FMV_J182A,	PCMCIA_CIS_FUJITSU_FMV_J182A,
    136 	  0, 0x1cc },
    137 
    138 	{ PCMCIA_STR_FUJITSU_ITCFJ182A,		PCMCIA_VENDOR_FUJITSU,
    139 	  PCMCIA_PRODUCT_FUJITSU_ITCFJ182A,	PCMCIA_CIS_FUJITSU_ITCFJ182A,
    140 	  0, 0x1cc },
    141 
    142 	{ PCMCIA_STR_FUJITSU_LA10S,		PCMCIA_VENDOR_FUJITSU,
    143 	  PCMCIA_PRODUCT_FUJITSU_LA10S,		PCMCIA_CIS_FUJITSU_LA10S,
    144 	  0, -1 },
    145 
    146 	{ PCMCIA_STR_RATOC_REX_R280,		PCMCIA_VENDOR_RATOC,
    147 	  PCMCIA_PRODUCT_RATOC_REX_R280,	PCMCIA_CIS_RATOC_REX_R280,
    148 	  0, 0x1fc },
    149 
    150 	{ NULL,					0,
    151           0,					{ NULL, NULL, NULL, NULL },
    152           0, 0 }
    153 };
    154 
    155 static const struct mbe_pcmcia_product *
    156 mbe_pcmcia_lookup(pa)
    157 	struct pcmcia_attach_args *pa;
    158 {
    159 	const struct mbe_pcmcia_product *mpp;
    160 
    161 	for (mpp = mbe_pcmcia_products; mpp->mpp_name != NULL; mpp++) {
    162 		/* match by CIS information */
    163 		if (pa->card->cis1_info[0] != NULL &&
    164 		    mpp->mpp_cisinfo[0] != NULL &&
    165 		    strcmp(pa->card->cis1_info[0], mpp->mpp_cisinfo[0]) == 0 &&
    166 		    pa->card->cis1_info[1] != NULL &&
    167 		    mpp->mpp_cisinfo[1] != NULL &&
    168 		    strcmp(pa->card->cis1_info[1], mpp->mpp_cisinfo[1]) == 0 &&
    169 		   (mpp->mpp_cisinfo[2] == NULL ||
    170 		    strcmp(pa->card->cis1_info[2], mpp->mpp_cisinfo[2]) == 0))
    171 			return (mpp);
    172 
    173 		/* match by vendor/product id */
    174 		if (pa->manufacturer != PCMCIA_VENDOR_INVALID &&
    175 		    pa->manufacturer == mpp->mpp_vendor &&
    176 		    pa->product != PCMCIA_PRODUCT_INVALID &&
    177 		    pa->product == mpp->mpp_product)
    178 			return (mpp);
    179 	}
    180 
    181 	return (NULL);
    182 }
    183 
    184 int
    185 mbe_pcmcia_match(parent, match, aux)
    186 	struct device *parent;
    187 	struct cfdata *match;
    188 	void *aux;
    189 {
    190 	struct pcmcia_attach_args *pa = aux;
    191 
    192 	if (mbe_pcmcia_lookup(pa) != NULL)
    193 		return (1);
    194 	return (0);
    195 }
    196 
    197 void
    198 mbe_pcmcia_attach(parent, self, aux)
    199 	struct device *parent, *self;
    200 	void *aux;
    201 {
    202 	struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)self;
    203 	struct mb86960_softc *sc = &psc->sc_mb86960;
    204 	struct pcmcia_attach_args *pa = aux;
    205 	struct pcmcia_config_entry *cfe;
    206 	struct mbe_pcmcia_get_enaddr_args pgea;
    207 	const struct mbe_pcmcia_product *mpp;
    208 	int rv;
    209 
    210 	mpp = mbe_pcmcia_lookup(pa);
    211 	if (mpp == NULL) {
    212 		printf("\n");
    213 		panic("mbe_pcmcia_attach: impossible");
    214 	}
    215 
    216 	psc->sc_pf = pa->pf;
    217 	cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head);
    218 
    219 	/* Enable the card. */
    220 	pcmcia_function_init(pa->pf, cfe);
    221 	if (pcmcia_function_enable(pa->pf)) {
    222 		printf(": function enable failed\n");
    223 		goto enable_failed;
    224 	}
    225 
    226 	/* Allocate and map i/o space for the card. */
    227 	if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
    228 	    cfe->iospace[0].length,
    229 	    mpp->mpp_ioalign ? mpp->mpp_ioalign : cfe->iospace[0].length,
    230 	    &psc->sc_pcioh)) {
    231 		printf(": can't allocate i/o space\n");
    232 		goto ioalloc_failed;
    233 	}
    234 
    235 	sc->sc_bst = psc->sc_pcioh.iot;
    236 	sc->sc_bsh = psc->sc_pcioh.ioh;
    237 
    238 	sc->sc_enable = mbe_pcmcia_enable;
    239 	sc->sc_disable = mbe_pcmcia_disable;
    240 
    241 	/*
    242 	 * Don't bother checking flags; the back-end sets the chip
    243 	 * into 16-bit mode.
    244 	 */
    245 	if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_IO16, &psc->sc_pcioh,
    246 	    &psc->sc_io_window)) {
    247 		printf(": can't map i/o space\n");
    248 		goto iomap_failed;
    249 	}
    250 
    251 	printf(": %s\n", mpp->mpp_name);
    252 
    253 	/* Read station address from io/mem or CIS. */
    254 	if (mpp->mpp_enet_maddr >= 0) {
    255 		pgea.maddr = mpp->mpp_enet_maddr;
    256 		if (mbe_pcmcia_get_enaddr_from_mem(psc, &pgea) != 0) {
    257 			printf("%s: Couldn't get ethernet address "
    258 			    "from mem\n", sc->sc_dev.dv_xname);
    259 			goto no_enaddr;
    260 		}
    261 	} else if ((mpp->flags & MBH10302) != 0) {
    262 		bus_space_write_1(sc->sc_bst, sc->sc_bsh, FE_MBH0 ,
    263 				  FE_MBH0_MASK | FE_MBH0_INTR_ENABLE);
    264 		if (mbe_pcmcia_get_enaddr_from_io(psc, &pgea) != 0) {
    265 			printf("%s: Couldn't get ethernet address "
    266 			    "from io\n", sc->sc_dev.dv_xname);
    267 			goto no_enaddr;
    268 		}
    269 	} else {
    270 		rv = pcmcia_scan_cis(parent,
    271 		    mbe_pcmcia_get_enaddr_from_cis, &pgea);
    272 		if (rv == -1) {
    273 			printf("%s: Couldn't read CIS to get ethernet "
    274 			    "address\n", sc->sc_dev.dv_xname);
    275 			goto no_enaddr;
    276 		} else if (rv == 0) {
    277 			printf("%s: Couldn't get ethernet address "
    278 			    "from CIS\n", sc->sc_dev.dv_xname);
    279 			goto no_enaddr;
    280 		}
    281 #ifdef DIAGNOSTIC
    282 		if (rv != 1) {
    283 			printf("%s: pcmcia_scan_cis returns %d\n",
    284 			    sc->sc_dev.dv_xname, rv);
    285 			panic("mbe_pcmcia_attach");
    286 		}
    287 		printf("%s: Ethernet address from CIS: %s\n",
    288 		    sc->sc_dev.dv_xname, ether_sprintf(pgea.enaddr));
    289 #endif
    290 	}
    291 
    292 	/* Perform generic initialization. */
    293 	if ((mpp->flags & MBH10302) != 0)
    294 		sc->sc_flags |= FE_FLAGS_MB86960;
    295 
    296 	mb86960_attach(sc, pgea.enaddr);
    297 
    298 	mb86960_config(sc, NULL, 0, 0);
    299 
    300 	pcmcia_function_disable(pa->pf);
    301 	return;
    302 
    303  no_enaddr:
    304 	/* Unmap our i/o window. */
    305 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    306 
    307  iomap_failed:
    308 	/* Free our i/o space. */
    309 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    310 
    311  ioalloc_failed:
    312 	pcmcia_function_disable(pa->pf);
    313 
    314  enable_failed:
    315 	psc->sc_io_window = -1;
    316 }
    317 
    318 int
    319 mbe_pcmcia_detach(self, flags)
    320 	struct device *self;
    321 	int flags;
    322 {
    323 	struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)self;
    324 	int error;
    325 
    326 	if (psc->sc_io_window == -1)
    327 		/* Nothing to detach.  */
    328 		return (0);
    329 
    330 	error = mb86960_detach(&psc->sc_mb86960);
    331 	if (error != 0)
    332 		return (error);
    333 
    334 	/* Unmap our i/o window. */
    335 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    336 
    337 	/* Free our i/o space. */
    338 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    339 
    340 	return (0);
    341 }
    342 
    343 int
    344 mbe_pcmcia_enable(sc)
    345 	struct mb86960_softc *sc;
    346 {
    347 	struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)sc;
    348 
    349 	/* Establish the interrupt handler. */
    350 	psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, mb86960_intr,
    351 	    sc);
    352 	if (psc->sc_ih == NULL) {
    353 		printf("%s: couldn't establish interrupt handler\n",
    354 		    sc->sc_dev.dv_xname);
    355 		return (1);
    356 	}
    357 
    358 	if (pcmcia_function_enable(psc->sc_pf)) {
    359 		pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    360 		return (1);
    361 	}
    362 
    363 	return (0);
    364 }
    365 
    366 void
    367 mbe_pcmcia_disable(sc)
    368 	struct mb86960_softc *sc;
    369 {
    370 	struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)sc;
    371 
    372 	pcmcia_function_disable(psc->sc_pf);
    373 	pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    374 }
    375 
    376 int
    377 mbe_pcmcia_get_enaddr_from_cis(tuple, arg)
    378 	struct pcmcia_tuple *tuple;
    379 	void *arg;
    380 {
    381 	struct mbe_pcmcia_get_enaddr_args *p = arg;
    382 	int i;
    383 
    384 	if (tuple->code == PCMCIA_CISTPL_FUNCE) {
    385 		if (tuple->length < 2) /* sub code and ether addr length */
    386 			return (0);
    387 
    388 		if ((pcmcia_tuple_read_1(tuple, 0) !=
    389 			PCMCIA_TPLFE_TYPE_LAN_NID) ||
    390 		    (pcmcia_tuple_read_1(tuple, 1) != ETHER_ADDR_LEN))
    391 			return (0);
    392 
    393 		for (i = 0; i < ETHER_ADDR_LEN; i++)
    394 			p->enaddr[i] = pcmcia_tuple_read_1(tuple, i + 2);
    395 		return (1);
    396 	}
    397 	return (0);
    398 }
    399 
    400 int
    401 mbe_pcmcia_get_enaddr_from_io(psc, ea)
    402 	struct mbe_pcmcia_softc *psc;
    403 	struct mbe_pcmcia_get_enaddr_args *ea;
    404 {
    405 	int i;
    406 
    407 	for (i = 0; i < ETHER_ADDR_LEN; i++)
    408 		ea->enaddr[i] = bus_space_read_1(psc->sc_pcioh.iot,
    409 		    psc->sc_pcioh.ioh, FE_MBH_ENADDR + i);
    410 
    411 	if (ea->enaddr == NULL)
    412 		return (1);
    413 	return (0);
    414 }
    415 
    416 int
    417 mbe_pcmcia_get_enaddr_from_mem(psc, ea)
    418 	struct mbe_pcmcia_softc *psc;
    419 	struct mbe_pcmcia_get_enaddr_args *ea;
    420 {
    421 	struct mb86960_softc *sc = &psc->sc_mb86960;
    422 	struct pcmcia_mem_handle pcmh;
    423 	bus_size_t offset;
    424 	int i, mwindow, rv = 1;
    425 
    426 	if (ea->maddr < 0)
    427 		goto bad_memaddr;
    428 
    429 	if (pcmcia_mem_alloc(psc->sc_pf, ETHER_ADDR_LEN * 2, &pcmh)) {
    430 		printf("%s: can't alloc mem for enet addr\n",
    431 		    sc->sc_dev.dv_xname);
    432 		goto memalloc_failed;
    433 	}
    434 
    435 	if (pcmcia_mem_map(psc->sc_pf, PCMCIA_MEM_ATTR, ea->maddr,
    436 	    ETHER_ADDR_LEN * 2, &pcmh, &offset, &mwindow)) {
    437 		printf("%s: can't map mem for enet addr\n",
    438 		    sc->sc_dev.dv_xname);
    439 		goto memmap_failed;
    440 	}
    441 
    442 	for (i = 0; i < ETHER_ADDR_LEN; i++)
    443 		ea->enaddr[i] = bus_space_read_1(pcmh.memt, pcmh.memh,
    444 		    offset + (i * 2));
    445 
    446 	rv = 0;
    447 	pcmcia_mem_unmap(psc->sc_pf, mwindow);
    448 memmap_failed:
    449 	pcmcia_mem_free(psc->sc_pf, &pcmh);
    450 memalloc_failed:
    451 bad_memaddr:
    452 
    453 	return (rv);
    454 }
    455