Home | History | Annotate | Line # | Download | only in pcmcia
if_mbe_pcmcia.c revision 1.33
      1 /*	$NetBSD: if_mbe_pcmcia.c,v 1.33 2004/08/10 18:43:49 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.33 2004/08/10 18:43:49 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 	mpp = pcmcia_product_lookup(pa, mbe_pcmcia_products,
    175 	    mbe_pcmcia_nproducts, sizeof(mbe_pcmcia_products[0]), NULL);
    176 	if (!mpp)
    177 		panic("mbe_pcmcia_attach: impossible");
    178 
    179 	psc->sc_pf = pa->pf;
    180 	cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head);
    181 
    182 	/* Enable the card. */
    183 	pcmcia_function_init(pa->pf, cfe);
    184 	if (pcmcia_function_enable(pa->pf)) {
    185 		printf("%s: function enable failed\n", sc->sc_dev.dv_xname);
    186 		goto enable_failed;
    187 	}
    188 
    189 	/* Allocate and map i/o space for the card. */
    190 	if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
    191 	    cfe->iospace[0].length,
    192 	    mpp->mpp_ioalign ? mpp->mpp_ioalign : cfe->iospace[0].length,
    193 	    &psc->sc_pcioh)) {
    194 		printf("%s: can't allocate i/o space\n", sc->sc_dev.dv_xname);
    195 		goto ioalloc_failed;
    196 	}
    197 
    198 	sc->sc_bst = psc->sc_pcioh.iot;
    199 	sc->sc_bsh = psc->sc_pcioh.ioh;
    200 
    201 	sc->sc_enable = mbe_pcmcia_enable;
    202 	sc->sc_disable = mbe_pcmcia_disable;
    203 
    204 	/*
    205 	 * Don't bother checking flags; the back-end sets the chip
    206 	 * into 16-bit mode.
    207 	 */
    208 	if (pcmcia_io_map(pa->pf, PCMCIA_WIDTH_IO16, &psc->sc_pcioh,
    209 	    &psc->sc_io_window)) {
    210 		printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
    211 		goto iomap_failed;
    212 	}
    213 
    214 	/* Read station address from io/mem or CIS. */
    215 	if (mpp->mpp_enet_maddr >= 0) {
    216 		pgea.maddr = mpp->mpp_enet_maddr;
    217 		if (mbe_pcmcia_get_enaddr_from_mem(psc, &pgea) != 0) {
    218 			printf("%s: Couldn't get ethernet address "
    219 			    "from mem\n", sc->sc_dev.dv_xname);
    220 			goto no_enaddr;
    221 		}
    222 	} else if ((mpp->mpp_flags & MBH10302) != 0) {
    223 		bus_space_write_1(sc->sc_bst, sc->sc_bsh, FE_MBH0 ,
    224 				  FE_MBH0_MASK | FE_MBH0_INTR_ENABLE);
    225 		if (mbe_pcmcia_get_enaddr_from_io(psc, &pgea) != 0) {
    226 			printf("%s: Couldn't get ethernet address "
    227 			    "from io\n", sc->sc_dev.dv_xname);
    228 			goto no_enaddr;
    229 		}
    230 	} else {
    231 		rv = pcmcia_scan_cis(parent,
    232 		    mbe_pcmcia_get_enaddr_from_cis, &pgea);
    233 		if (rv == -1) {
    234 			printf("%s: Couldn't read CIS to get ethernet "
    235 			    "address\n", sc->sc_dev.dv_xname);
    236 			goto no_enaddr;
    237 		} else if (rv == 0) {
    238 			printf("%s: Couldn't get ethernet address "
    239 			    "from CIS\n", sc->sc_dev.dv_xname);
    240 			goto no_enaddr;
    241 		}
    242 #ifdef DIAGNOSTIC
    243 		if (rv != 1) {
    244 			printf("%s: pcmcia_scan_cis returns %d\n",
    245 			    sc->sc_dev.dv_xname, rv);
    246 			panic("mbe_pcmcia_attach");
    247 		}
    248 		printf("%s: Ethernet address from CIS: %s\n",
    249 		    sc->sc_dev.dv_xname, ether_sprintf(pgea.enaddr));
    250 #endif
    251 	}
    252 
    253 	/* Perform generic initialization. */
    254 	if ((mpp->mpp_flags & MBH10302) != 0)
    255 		sc->sc_flags |= FE_FLAGS_MB86960;
    256 
    257 	mb86960_attach(sc, pgea.enaddr);
    258 
    259 	mb86960_config(sc, NULL, 0, 0);
    260 
    261 	pcmcia_function_disable(pa->pf);
    262 	return;
    263 
    264  no_enaddr:
    265 	/* Unmap our i/o window. */
    266 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    267 
    268  iomap_failed:
    269 	/* Free our i/o space. */
    270 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    271 
    272  ioalloc_failed:
    273 	pcmcia_function_disable(pa->pf);
    274 
    275  enable_failed:
    276 	psc->sc_io_window = -1;
    277 }
    278 
    279 int
    280 mbe_pcmcia_detach(self, flags)
    281 	struct device *self;
    282 	int flags;
    283 {
    284 	struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)self;
    285 	int error;
    286 
    287 	if (psc->sc_io_window == -1)
    288 		/* Nothing to detach.  */
    289 		return (0);
    290 
    291 	error = mb86960_detach(&psc->sc_mb86960);
    292 	if (error != 0)
    293 		return (error);
    294 
    295 	/* Unmap our i/o window. */
    296 	pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    297 
    298 	/* Free our i/o space. */
    299 	pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    300 
    301 	return (0);
    302 }
    303 
    304 int
    305 mbe_pcmcia_enable(sc)
    306 	struct mb86960_softc *sc;
    307 {
    308 	struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)sc;
    309 
    310 	/* Establish the interrupt handler. */
    311 	psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, mb86960_intr,
    312 	    sc);
    313 	if (psc->sc_ih == NULL) {
    314 		printf("%s: couldn't establish interrupt handler\n",
    315 		    sc->sc_dev.dv_xname);
    316 		return (1);
    317 	}
    318 
    319 	if (pcmcia_function_enable(psc->sc_pf)) {
    320 		pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    321 		return (1);
    322 	}
    323 
    324 	return (0);
    325 }
    326 
    327 void
    328 mbe_pcmcia_disable(sc)
    329 	struct mb86960_softc *sc;
    330 {
    331 	struct mbe_pcmcia_softc *psc = (struct mbe_pcmcia_softc *)sc;
    332 
    333 	pcmcia_function_disable(psc->sc_pf);
    334 	pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    335 }
    336 
    337 int
    338 mbe_pcmcia_get_enaddr_from_cis(tuple, arg)
    339 	struct pcmcia_tuple *tuple;
    340 	void *arg;
    341 {
    342 	struct mbe_pcmcia_get_enaddr_args *p = arg;
    343 	int i;
    344 
    345 	if (tuple->code == PCMCIA_CISTPL_FUNCE) {
    346 		if (tuple->length < 2) /* sub code and ether addr length */
    347 			return (0);
    348 
    349 		if ((pcmcia_tuple_read_1(tuple, 0) !=
    350 			PCMCIA_TPLFE_TYPE_LAN_NID) ||
    351 		    (pcmcia_tuple_read_1(tuple, 1) != ETHER_ADDR_LEN))
    352 			return (0);
    353 
    354 		for (i = 0; i < ETHER_ADDR_LEN; i++)
    355 			p->enaddr[i] = pcmcia_tuple_read_1(tuple, i + 2);
    356 		return (1);
    357 	}
    358 	return (0);
    359 }
    360 
    361 int
    362 mbe_pcmcia_get_enaddr_from_io(psc, ea)
    363 	struct mbe_pcmcia_softc *psc;
    364 	struct mbe_pcmcia_get_enaddr_args *ea;
    365 {
    366 	int i;
    367 
    368 	for (i = 0; i < ETHER_ADDR_LEN; i++)
    369 		ea->enaddr[i] = bus_space_read_1(psc->sc_pcioh.iot,
    370 		    psc->sc_pcioh.ioh, FE_MBH_ENADDR + i);
    371 
    372 	if (ea->enaddr == NULL)
    373 		return (1);
    374 	return (0);
    375 }
    376 
    377 int
    378 mbe_pcmcia_get_enaddr_from_mem(psc, ea)
    379 	struct mbe_pcmcia_softc *psc;
    380 	struct mbe_pcmcia_get_enaddr_args *ea;
    381 {
    382 	struct mb86960_softc *sc = &psc->sc_mb86960;
    383 	struct pcmcia_mem_handle pcmh;
    384 	bus_size_t offset;
    385 	int i, mwindow, rv = 1;
    386 
    387 	if (ea->maddr < 0)
    388 		goto bad_memaddr;
    389 
    390 	if (pcmcia_mem_alloc(psc->sc_pf, ETHER_ADDR_LEN * 2, &pcmh)) {
    391 		printf("%s: can't alloc mem for enet addr\n",
    392 		    sc->sc_dev.dv_xname);
    393 		goto memalloc_failed;
    394 	}
    395 
    396 	if (pcmcia_mem_map(psc->sc_pf, PCMCIA_MEM_ATTR, ea->maddr,
    397 	    ETHER_ADDR_LEN * 2, &pcmh, &offset, &mwindow)) {
    398 		printf("%s: can't map mem for enet addr\n",
    399 		    sc->sc_dev.dv_xname);
    400 		goto memmap_failed;
    401 	}
    402 
    403 	for (i = 0; i < ETHER_ADDR_LEN; i++)
    404 		ea->enaddr[i] = bus_space_read_1(pcmh.memt, pcmh.memh,
    405 		    offset + (i * 2));
    406 
    407 	rv = 0;
    408 	pcmcia_mem_unmap(psc->sc_pf, mwindow);
    409 memmap_failed:
    410 	pcmcia_mem_free(psc->sc_pf, &pcmh);
    411 memalloc_failed:
    412 bad_memaddr:
    413 
    414 	return (rv);
    415 }
    416