Home | History | Annotate | Line # | Download | only in pcmcia
if_mbe_pcmcia.c revision 1.34
      1  1.34   mycroft /*	$NetBSD: if_mbe_pcmcia.c,v 1.34 2004/08/10 20:47:17 mycroft Exp $	*/
      2   1.1     enami 
      3   1.1     enami /*-
      4  1.32   mycroft  * Copyright (c) 1998, 2000, 2004 The NetBSD Foundation, Inc.
      5   1.1     enami  * All rights reserved.
      6   1.1     enami  *
      7   1.1     enami  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1     enami  * by Enami Tsugutomo.
      9   1.1     enami  *
     10   1.1     enami  * Redistribution and use in source and binary forms, with or without
     11   1.1     enami  * modification, are permitted provided that the following conditions
     12   1.1     enami  * are met:
     13   1.1     enami  * 1. Redistributions of source code must retain the above copyright
     14   1.1     enami  *    notice, this list of conditions and the following disclaimer.
     15   1.1     enami  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1     enami  *    notice, this list of conditions and the following disclaimer in the
     17   1.1     enami  *    documentation and/or other materials provided with the distribution.
     18   1.1     enami  * 3. All advertising materials mentioning features or use of this software
     19   1.1     enami  *    must display the following acknowledgement:
     20   1.1     enami  *	This product includes software developed by the NetBSD
     21   1.1     enami  *	Foundation, Inc. and its contributors.
     22   1.1     enami  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.1     enami  *    contributors may be used to endorse or promote products derived
     24   1.1     enami  *    from this software without specific prior written permission.
     25   1.1     enami  *
     26   1.1     enami  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.1     enami  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.1     enami  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.1     enami  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.1     enami  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.1     enami  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.1     enami  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.1     enami  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.1     enami  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.1     enami  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.1     enami  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1     enami  */
     38  1.21     lukem 
     39  1.21     lukem #include <sys/cdefs.h>
     40  1.34   mycroft __KERNEL_RCSID(0, "$NetBSD: if_mbe_pcmcia.c,v 1.34 2004/08/10 20:47:17 mycroft Exp $");
     41   1.1     enami 
     42   1.1     enami #include <sys/param.h>
     43   1.1     enami #include <sys/systm.h>
     44   1.1     enami #include <sys/device.h>
     45   1.1     enami #include <sys/socket.h>
     46   1.1     enami 
     47   1.1     enami #include <net/if.h>
     48   1.1     enami #include <net/if_ether.h>
     49   1.1     enami #include <net/if_media.h>
     50   1.1     enami 
     51   1.1     enami #include <machine/intr.h>
     52   1.1     enami #include <machine/bus.h>
     53   1.1     enami 
     54   1.1     enami #include <dev/ic/mb86960reg.h>
     55   1.1     enami #include <dev/ic/mb86960var.h>
     56   1.1     enami 
     57   1.1     enami #include <dev/pcmcia/pcmciareg.h>
     58   1.1     enami #include <dev/pcmcia/pcmciavar.h>
     59   1.3  christos #include <dev/pcmcia/pcmciadevs.h>
     60   1.1     enami 
     61   1.1     enami int	mbe_pcmcia_match __P((struct device *, struct cfdata *, void *));
     62  1.34   mycroft int	mbe_pcmcia_validate_config __P((struct pcmcia_config_entry *));
     63   1.1     enami void	mbe_pcmcia_attach __P((struct device *, struct device *, void *));
     64   1.4   thorpej int	mbe_pcmcia_detach __P((struct device *, int));
     65   1.1     enami 
     66   1.1     enami struct mbe_pcmcia_softc {
     67   1.1     enami 	struct	mb86960_softc sc_mb86960;	/* real "mb" softc */
     68   1.1     enami 
     69  1.34   mycroft 	struct	pcmcia_function *sc_pf;		/* our PCMCIA function */
     70   1.1     enami 	void	*sc_ih;				/* interrupt cookie */
     71  1.34   mycroft 
     72  1.34   mycroft 	int	sc_state;
     73  1.34   mycroft #define	MBE_PCMCIA_ATTACHED	3
     74   1.1     enami };
     75   1.1     enami 
     76  1.28   thorpej CFATTACH_DECL(mbe_pcmcia, sizeof(struct mbe_pcmcia_softc),
     77  1.29   thorpej     mbe_pcmcia_match, mbe_pcmcia_attach, mbe_pcmcia_detach, mb86960_activate);
     78   1.1     enami 
     79   1.1     enami int	mbe_pcmcia_enable __P((struct mb86960_softc *));
     80   1.1     enami void	mbe_pcmcia_disable __P((struct mb86960_softc *));
     81   1.1     enami 
     82   1.1     enami struct mbe_pcmcia_get_enaddr_args {
     83   1.1     enami 	u_int8_t enaddr[ETHER_ADDR_LEN];
     84  1.18        is 	int maddr;
     85   1.1     enami };
     86  1.18        is int	mbe_pcmcia_get_enaddr_from_cis __P((struct pcmcia_tuple *, void *));
     87  1.18        is int	mbe_pcmcia_get_enaddr_from_mem __P((struct mbe_pcmcia_softc *,
     88  1.19     enami 	    struct mbe_pcmcia_get_enaddr_args *));
     89  1.25    ichiro int	mbe_pcmcia_get_enaddr_from_io __P((struct mbe_pcmcia_softc *,
     90  1.25    ichiro 	    struct mbe_pcmcia_get_enaddr_args *));
     91   1.1     enami 
     92  1.24    ichiro static const struct mbe_pcmcia_product {
     93  1.32   mycroft 	struct pcmcia_product mpp_product;
     94  1.24    ichiro 	int		mpp_enet_maddr;
     95  1.32   mycroft 	int		mpp_flags;
     96  1.25    ichiro #define MBH10302	0x0001			/* FUJITSU MBH10302 */
     97   1.7   thorpej } mbe_pcmcia_products[] = {
     98  1.32   mycroft 	{ { PCMCIA_VENDOR_TDK, PCMCIA_PRODUCT_TDK_LAK_CD021BX,
     99  1.32   mycroft 	    PCMCIA_CIS_TDK_LAK_CD021BX },
    100  1.34   mycroft 	  -1 },
    101  1.13       cgd 
    102  1.32   mycroft 	{ { PCMCIA_VENDOR_TDK, PCMCIA_PRODUCT_TDK_LAK_CF010,
    103  1.32   mycroft 	    PCMCIA_CIS_TDK_LAK_CF010 },
    104  1.34   mycroft 	  -1 },
    105  1.19     enami 
    106   1.7   thorpej #if 0 /* XXX 86960-based? */
    107  1.32   mycroft 	{ { PCMCIA_VENDOR_TDK, PCMCIA_PRODUCT_TDK_LAK_DFL9610,
    108  1.32   mycroft 	    PCMCIA_CIS_TDK_DFL9610 },
    109  1.34   mycroft 	  -1, MBH10302 /* XXX */ },
    110   1.7   thorpej #endif
    111   1.8      tron 
    112  1.32   mycroft 	{ { PCMCIA_VENDOR_CONTEC, PCMCIA_PRODUCT_CONTEC_CNETPC,
    113  1.32   mycroft 	    PCMCIA_CIS_CONTEC_CNETPC },
    114  1.34   mycroft 	  -1 },
    115  1.13       cgd 
    116  1.32   mycroft 	{ { PCMCIA_VENDOR_FUJITSU, PCMCIA_PRODUCT_FUJITSU_LA501,
    117  1.32   mycroft 	    PCMCIA_CIS_FUJITSU_LA501 },
    118  1.34   mycroft 	  -1 },
    119  1.13       cgd 
    120  1.32   mycroft 	{ { PCMCIA_VENDOR_FUJITSU, PCMCIA_PRODUCT_FUJITSU_FMV_J181,
    121  1.32   mycroft 	    PCMCIA_CIS_FUJITSU_FMV_J181 },
    122  1.34   mycroft 	  -1, MBH10302 },
    123  1.25    ichiro 
    124  1.32   mycroft 	{ { PCMCIA_VENDOR_FUJITSU, PCMCIA_PRODUCT_FUJITSU_FMV_J182,
    125  1.32   mycroft 	    PCMCIA_CIS_FUJITSU_FMV_J182 },
    126  1.34   mycroft 	  0xf2c },
    127  1.25    ichiro 
    128  1.32   mycroft 	{ { PCMCIA_VENDOR_FUJITSU, PCMCIA_PRODUCT_FUJITSU_FMV_J182A,
    129  1.32   mycroft 	    PCMCIA_CIS_FUJITSU_FMV_J182A },
    130  1.34   mycroft 	  0x1cc },
    131  1.23    ichiro 
    132  1.32   mycroft 	{ { PCMCIA_VENDOR_FUJITSU, PCMCIA_PRODUCT_FUJITSU_ITCFJ182A,
    133  1.32   mycroft 	    PCMCIA_CIS_FUJITSU_ITCFJ182A },
    134  1.34   mycroft 	  0x1cc },
    135  1.18        is 
    136  1.32   mycroft 	{ { PCMCIA_VENDOR_FUJITSU, PCMCIA_PRODUCT_FUJITSU_LA10S,
    137  1.32   mycroft 	    PCMCIA_CIS_FUJITSU_LA10S },
    138  1.34   mycroft 	  -1 },
    139  1.25    ichiro 
    140  1.32   mycroft 	{ { PCMCIA_VENDOR_RATOC, PCMCIA_PRODUCT_RATOC_REX_R280,
    141  1.32   mycroft 	    PCMCIA_CIS_RATOC_REX_R280 },
    142  1.34   mycroft 	  0x1fc },
    143   1.7   thorpej };
    144  1.32   mycroft static const size_t mbe_pcmcia_nproducts =
    145  1.32   mycroft     sizeof(mbe_pcmcia_products) / sizeof(mbe_pcmcia_products[0]);
    146  1.24    ichiro 
    147   1.1     enami int
    148   1.1     enami mbe_pcmcia_match(parent, match, aux)
    149   1.1     enami 	struct device *parent;
    150   1.1     enami 	struct cfdata *match;
    151   1.1     enami 	void *aux;
    152   1.1     enami {
    153   1.1     enami 	struct pcmcia_attach_args *pa = aux;
    154   1.1     enami 
    155  1.32   mycroft 	if (pcmcia_product_lookup(pa, mbe_pcmcia_products, mbe_pcmcia_nproducts,
    156  1.32   mycroft 	    sizeof(mbe_pcmcia_products[0]), NULL))
    157   1.7   thorpej 		return (1);
    158   1.1     enami 	return (0);
    159   1.1     enami }
    160   1.1     enami 
    161  1.34   mycroft int
    162  1.34   mycroft mbe_pcmcia_validate_config(cfe)
    163  1.34   mycroft 	struct pcmcia_config_entry *cfe;
    164  1.34   mycroft {
    165  1.34   mycroft 	if (cfe->iftype != PCMCIA_IFTYPE_IO ||
    166  1.34   mycroft 	    cfe->num_iospace < 1)
    167  1.34   mycroft 		return (EINVAL);
    168  1.34   mycroft 	return (0);
    169  1.34   mycroft }
    170  1.34   mycroft 
    171   1.1     enami void
    172   1.1     enami mbe_pcmcia_attach(parent, self, aux)
    173   1.1     enami 	struct device *parent, *self;
    174   1.1     enami 	void *aux;
    175   1.1     enami {
    176  1.34   mycroft 	struct mbe_pcmcia_softc *psc = (void *)self;
    177   1.1     enami 	struct mb86960_softc *sc = &psc->sc_mb86960;
    178   1.1     enami 	struct pcmcia_attach_args *pa = aux;
    179   1.1     enami 	struct pcmcia_config_entry *cfe;
    180   1.1     enami 	struct mbe_pcmcia_get_enaddr_args pgea;
    181   1.7   thorpej 	const struct mbe_pcmcia_product *mpp;
    182  1.34   mycroft 	int error;
    183   1.1     enami 
    184   1.1     enami 	psc->sc_pf = pa->pf;
    185   1.1     enami 
    186  1.34   mycroft 	error = pcmcia_function_configure(pa->pf, mbe_pcmcia_validate_config);
    187  1.34   mycroft 	if (error) {
    188  1.34   mycroft 		aprint_error("%s: configure failed, error=%d\n", self->dv_xname,
    189  1.34   mycroft 		    error);
    190  1.34   mycroft 		return;
    191   1.1     enami 	}
    192   1.1     enami 
    193  1.34   mycroft 	cfe = pa->pf->cfe;
    194  1.34   mycroft 	sc->sc_bst = cfe->iospace[0].handle.iot;
    195  1.34   mycroft 	sc->sc_bsh = cfe->iospace[0].handle.ioh;
    196  1.34   mycroft 
    197  1.34   mycroft 	error = mbe_pcmcia_enable(sc);
    198  1.34   mycroft 	if (error)
    199  1.34   mycroft 		goto fail;
    200   1.1     enami 
    201  1.34   mycroft 	mpp = pcmcia_product_lookup(pa, mbe_pcmcia_products,
    202  1.34   mycroft 	    mbe_pcmcia_nproducts, sizeof(mbe_pcmcia_products[0]), NULL);
    203  1.34   mycroft 	if (!mpp)
    204  1.34   mycroft 		panic("mbe_pcmcia_attach: impossible");
    205   1.1     enami 
    206   1.1     enami 	sc->sc_enable = mbe_pcmcia_enable;
    207   1.1     enami 	sc->sc_disable = mbe_pcmcia_disable;
    208   1.1     enami 
    209  1.25    ichiro 	/* Read station address from io/mem or CIS. */
    210  1.19     enami 	if (mpp->mpp_enet_maddr >= 0) {
    211  1.19     enami 		pgea.maddr = mpp->mpp_enet_maddr;
    212  1.18        is 		if (mbe_pcmcia_get_enaddr_from_mem(psc, &pgea) != 0) {
    213  1.34   mycroft 			printf("%s: couldn't get ethernet address "
    214  1.34   mycroft 			    "from memory\n", self->dv_xname);
    215  1.34   mycroft 			goto fail2;
    216  1.18        is 		}
    217  1.34   mycroft 	} else if (mpp->mpp_flags & MBH10302) {
    218  1.25    ichiro 		bus_space_write_1(sc->sc_bst, sc->sc_bsh, FE_MBH0 ,
    219  1.25    ichiro 				  FE_MBH0_MASK | FE_MBH0_INTR_ENABLE);
    220  1.25    ichiro 		if (mbe_pcmcia_get_enaddr_from_io(psc, &pgea) != 0) {
    221  1.34   mycroft 			printf("%s: couldn't get ethernet address from i/o\n",
    222  1.34   mycroft 			    self->dv_xname);
    223  1.34   mycroft 			goto fail2;
    224  1.25    ichiro 		}
    225  1.18        is 	} else {
    226  1.34   mycroft 		if (pa->pf->pf_funce_lan_nidlen != ETHER_ADDR_LEN) {
    227  1.34   mycroft 			printf("%s: couldn't get ethernet address from CIS\n",
    228  1.34   mycroft 			    self->dv_xname);
    229  1.34   mycroft 			goto fail2;
    230  1.18        is 		}
    231  1.34   mycroft 		memcpy(pgea.enaddr, pa->pf->pf_funce_lan_nid, ETHER_ADDR_LEN);
    232  1.10     enami 	}
    233   1.1     enami 
    234   1.1     enami 	/* Perform generic initialization. */
    235  1.34   mycroft 	if (mpp->mpp_flags & MBH10302)
    236  1.30   tsutsui 		sc->sc_flags |= FE_FLAGS_MB86960;
    237  1.30   tsutsui 
    238  1.30   tsutsui 	mb86960_attach(sc, pgea.enaddr);
    239   1.1     enami 
    240   1.1     enami 	mb86960_config(sc, NULL, 0, 0);
    241   1.1     enami 
    242  1.34   mycroft 	mbe_pcmcia_disable(sc);
    243  1.34   mycroft 	psc->sc_state = MBE_PCMCIA_ATTACHED;
    244  1.14     enami 	return;
    245  1.14     enami 
    246  1.34   mycroft fail2:
    247  1.34   mycroft 	mbe_pcmcia_disable(sc);
    248  1.34   mycroft fail:
    249  1.34   mycroft 	pcmcia_function_unconfigure(pa->pf);
    250   1.4   thorpej }
    251   1.4   thorpej 
    252   1.4   thorpej int
    253   1.4   thorpej mbe_pcmcia_detach(self, flags)
    254   1.4   thorpej 	struct device *self;
    255   1.4   thorpej 	int flags;
    256   1.4   thorpej {
    257  1.34   mycroft 	struct mbe_pcmcia_softc *psc = (void *)self;
    258  1.12     enami 	int error;
    259  1.12     enami 
    260  1.34   mycroft 	if (psc->sc_state != MBE_PCMCIA_ATTACHED)
    261  1.14     enami 		return (0);
    262  1.14     enami 
    263  1.14     enami 	error = mb86960_detach(&psc->sc_mb86960);
    264  1.34   mycroft 	if (error)
    265  1.12     enami 		return (error);
    266   1.5   thorpej 
    267  1.34   mycroft 	pcmcia_function_unconfigure(psc->sc_pf);
    268   1.5   thorpej 
    269   1.4   thorpej 	return (0);
    270   1.1     enami }
    271   1.1     enami 
    272   1.1     enami int
    273   1.1     enami mbe_pcmcia_enable(sc)
    274   1.1     enami 	struct mb86960_softc *sc;
    275   1.1     enami {
    276  1.34   mycroft 	struct mbe_pcmcia_softc *psc = (void *)sc;
    277  1.34   mycroft 	int error;
    278   1.1     enami 
    279   1.1     enami 	/* Establish the interrupt handler. */
    280   1.1     enami 	psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, mb86960_intr,
    281   1.1     enami 	    sc);
    282  1.34   mycroft 	if (!psc->sc_ih)
    283  1.34   mycroft 		return (EIO);
    284   1.1     enami 
    285  1.34   mycroft 	error = pcmcia_function_enable(psc->sc_pf);
    286  1.34   mycroft 	if (error) {
    287  1.17     enami 		pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    288  1.34   mycroft 		psc->sc_ih = 0;
    289  1.17     enami 	}
    290  1.16     enami 
    291  1.34   mycroft 	return (error);
    292   1.1     enami }
    293   1.1     enami 
    294   1.1     enami void
    295   1.1     enami mbe_pcmcia_disable(sc)
    296   1.1     enami 	struct mb86960_softc *sc;
    297   1.1     enami {
    298  1.34   mycroft 	struct mbe_pcmcia_softc *psc = (void *)sc;
    299   1.1     enami 
    300   1.1     enami 	pcmcia_function_disable(psc->sc_pf);
    301  1.17     enami 	pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    302  1.34   mycroft 	psc->sc_ih = 0;
    303  1.25    ichiro }
    304  1.25    ichiro 
    305  1.25    ichiro int
    306  1.25    ichiro mbe_pcmcia_get_enaddr_from_io(psc, ea)
    307  1.25    ichiro 	struct mbe_pcmcia_softc *psc;
    308  1.25    ichiro 	struct mbe_pcmcia_get_enaddr_args *ea;
    309  1.25    ichiro {
    310  1.34   mycroft 	struct mb86960_softc *sc = &psc->sc_mb86960;
    311  1.25    ichiro 	int i;
    312  1.25    ichiro 
    313  1.25    ichiro 	for (i = 0; i < ETHER_ADDR_LEN; i++)
    314  1.34   mycroft 		ea->enaddr[i] = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
    315  1.34   mycroft 		    FE_MBH_ENADDR + i);
    316  1.18        is 	return (0);
    317  1.18        is }
    318  1.18        is 
    319  1.18        is int
    320  1.18        is mbe_pcmcia_get_enaddr_from_mem(psc, ea)
    321  1.18        is 	struct mbe_pcmcia_softc *psc;
    322  1.18        is 	struct mbe_pcmcia_get_enaddr_args *ea;
    323  1.18        is {
    324  1.18        is 	struct mb86960_softc *sc = &psc->sc_mb86960;
    325  1.18        is 	struct pcmcia_mem_handle pcmh;
    326  1.22     soren 	bus_size_t offset;
    327  1.20     enami 	int i, mwindow, rv = 1;
    328  1.18        is 
    329  1.18        is 	if (ea->maddr < 0)
    330  1.20     enami 		goto bad_memaddr;
    331  1.18        is 
    332  1.18        is 	if (pcmcia_mem_alloc(psc->sc_pf, ETHER_ADDR_LEN * 2, &pcmh)) {
    333  1.18        is 		printf("%s: can't alloc mem for enet addr\n",
    334  1.19     enami 		    sc->sc_dev.dv_xname);
    335  1.20     enami 		goto memalloc_failed;
    336  1.18        is 	}
    337  1.18        is 
    338  1.18        is 	if (pcmcia_mem_map(psc->sc_pf, PCMCIA_MEM_ATTR, ea->maddr,
    339  1.19     enami 	    ETHER_ADDR_LEN * 2, &pcmh, &offset, &mwindow)) {
    340  1.18        is 		printf("%s: can't map mem for enet addr\n",
    341  1.19     enami 		    sc->sc_dev.dv_xname);
    342  1.20     enami 		goto memmap_failed;
    343  1.18        is 	}
    344  1.18        is 
    345  1.18        is 	for (i = 0; i < ETHER_ADDR_LEN; i++)
    346  1.18        is 		ea->enaddr[i] = bus_space_read_1(pcmh.memt, pcmh.memh,
    347  1.19     enami 		    offset + (i * 2));
    348  1.18        is 
    349  1.20     enami 	rv = 0;
    350  1.18        is 	pcmcia_mem_unmap(psc->sc_pf, mwindow);
    351  1.20     enami memmap_failed:
    352  1.18        is 	pcmcia_mem_free(psc->sc_pf, &pcmh);
    353  1.20     enami memalloc_failed:
    354  1.20     enami bad_memaddr:
    355  1.18        is 
    356  1.20     enami 	return (rv);
    357   1.1     enami }
    358