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