Home | History | Annotate | Line # | Download | only in pcmcia
      1  1.22   msaitoh /* $NetBSD: if_cs_pcmcia.c,v 1.22 2016/07/07 06:55:42 msaitoh Exp $ */
      2   1.1      yamt 
      3   1.1      yamt /*-
      4   1.1      yamt  * Copyright (c)2001 YAMAMOTO Takashi,
      5   1.1      yamt  * All rights reserved.
      6   1.1      yamt  *
      7   1.1      yamt  * Redistribution and use in source and binary forms, with or without
      8   1.1      yamt  * modification, are permitted provided that the following conditions
      9   1.1      yamt  * are met:
     10   1.1      yamt  * 1. Redistributions of source code must retain the above copyright
     11   1.1      yamt  *    notice, this list of conditions and the following disclaimer.
     12   1.1      yamt  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1      yamt  *    notice, this list of conditions and the following disclaimer in the
     14   1.1      yamt  *    documentation and/or other materials provided with the distribution.
     15   1.1      yamt  *
     16   1.1      yamt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17   1.1      yamt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18   1.1      yamt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19   1.1      yamt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20   1.1      yamt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21   1.1      yamt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22   1.1      yamt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23   1.1      yamt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24   1.1      yamt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25   1.1      yamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26   1.1      yamt  * SUCH DAMAGE.
     27   1.1      yamt  */
     28   1.1      yamt 
     29   1.1      yamt #include <sys/cdefs.h>
     30  1.22   msaitoh __KERNEL_RCSID(0, "$NetBSD: if_cs_pcmcia.c,v 1.22 2016/07/07 06:55:42 msaitoh Exp $");
     31   1.1      yamt 
     32   1.1      yamt #include <sys/param.h>
     33   1.1      yamt #include <sys/systm.h>
     34   1.1      yamt #include <sys/device.h>
     35   1.1      yamt #include <sys/socket.h>
     36   1.1      yamt #include <sys/queue.h>
     37   1.1      yamt 
     38   1.1      yamt #include <net/if.h>
     39   1.1      yamt #include <net/if_ether.h>
     40   1.1      yamt #include <net/if_media.h>
     41   1.1      yamt 
     42  1.15        ad #include <sys/bus.h>
     43  1.15        ad #include <sys/intr.h>
     44   1.1      yamt 
     45   1.1      yamt #include <dev/pcmcia/pcmciareg.h>
     46   1.1      yamt #include <dev/pcmcia/pcmciavar.h>
     47   1.1      yamt #include <dev/pcmcia/pcmciadevs.h>
     48   1.1      yamt 
     49   1.2      yamt #include <dev/ic/cs89x0reg.h>
     50   1.2      yamt #include <dev/ic/cs89x0var.h>
     51   1.1      yamt 
     52   1.1      yamt struct cs_pcmcia_softc;
     53   1.1      yamt 
     54  1.18    cegger static int cs_pcmcia_match(device_t, cfdata_t, void *);
     55  1.10   mycroft static int cs_pcmcia_validate_config(struct pcmcia_config_entry *);
     56  1.18    cegger static void cs_pcmcia_attach(device_t, device_t, void *);
     57  1.18    cegger static int cs_pcmcia_detach(device_t, int);
     58   1.1      yamt static int cs_pcmcia_enable(struct cs_softc *);
     59   1.1      yamt static void cs_pcmcia_disable(struct cs_softc *);
     60   1.1      yamt 
     61   1.1      yamt struct cs_pcmcia_softc {
     62   1.1      yamt 	struct cs_softc sc_cs; /* real "cs" softc */
     63   1.1      yamt 
     64   1.1      yamt 	struct pcmcia_function *sc_pf;
     65  1.10   mycroft 
     66  1.10   mycroft 	int sc_state;
     67  1.10   mycroft #define	CS_PCMCIA_ATTACHED	3
     68   1.1      yamt };
     69   1.1      yamt 
     70  1.20       chs CFATTACH_DECL_NEW(cs_pcmcia, sizeof(struct cs_pcmcia_softc),
     71   1.5   thorpej     cs_pcmcia_match, cs_pcmcia_attach, cs_pcmcia_detach, cs_activate);
     72   1.1      yamt 
     73   1.1      yamt static int
     74  1.18    cegger cs_pcmcia_match(device_t parent, cfdata_t match,
     75  1.13  christos     void *aux)
     76   1.1      yamt {
     77   1.1      yamt 	struct pcmcia_attach_args *pa = aux;
     78   1.1      yamt 
     79  1.10   mycroft 	if (pa->manufacturer == PCMCIA_VENDOR_IBM &&
     80  1.10   mycroft 	    pa->product == PCMCIA_PRODUCT_IBM_ETHERJET)
     81  1.10   mycroft 		return (1);
     82  1.10   mycroft 	return (0);
     83  1.10   mycroft }
     84   1.1      yamt 
     85  1.10   mycroft static int
     86  1.10   mycroft cs_pcmcia_validate_config(struct pcmcia_config_entry *cfe)
     87  1.10   mycroft {
     88  1.10   mycroft 	if (cfe->iftype != PCMCIA_IFTYPE_IO ||
     89  1.10   mycroft 	    cfe->num_memspace != 0 ||
     90  1.10   mycroft 	    cfe->num_iospace != 1 ||
     91  1.10   mycroft 	    cfe->iospace[0].length < CS8900_IOSIZE)
     92  1.10   mycroft 		return (EINVAL);
     93  1.10   mycroft 	return (0);
     94   1.1      yamt }
     95   1.1      yamt 
     96   1.1      yamt static void
     97  1.18    cegger cs_pcmcia_attach(device_t parent, device_t self, void *aux)
     98   1.1      yamt {
     99  1.20       chs 	struct cs_pcmcia_softc *psc = device_private(self);
    100  1.20       chs 	struct cs_softc *sc = &psc->sc_cs;
    101   1.1      yamt 	struct pcmcia_attach_args *pa = aux;
    102   1.1      yamt 	struct pcmcia_config_entry *cfe;
    103   1.1      yamt 	struct pcmcia_function *pf;
    104  1.10   mycroft 	int error;
    105   1.1      yamt 
    106  1.20       chs 	sc->sc_dev = self;
    107   1.1      yamt 	pf = psc->sc_pf = pa->pf;
    108   1.1      yamt 
    109  1.10   mycroft 	error = pcmcia_function_configure(pa->pf, cs_pcmcia_validate_config);
    110  1.10   mycroft 	if (error) {
    111  1.22   msaitoh 		aprint_error_dev(self, "configure failed, error=%d\n", error);
    112  1.10   mycroft 		return;
    113   1.1      yamt 	}
    114   1.1      yamt 
    115  1.10   mycroft 	cfe = pf->cfe;
    116  1.10   mycroft 	sc->sc_iot = cfe->iospace[0].handle.iot;
    117  1.10   mycroft 	sc->sc_ioh = cfe->iospace[0].handle.ioh;
    118   1.1      yamt 	sc->sc_irq = -1;
    119   1.1      yamt #define CS_PCMCIA_HACK_FOR_CARDBUS
    120   1.1      yamt #ifdef CS_PCMCIA_HACK_FOR_CARDBUS
    121   1.1      yamt 	/*
    122   1.1      yamt 	 * XXX is there a generic way to know if it's a cardbus or not?
    123   1.1      yamt 	 */
    124   1.1      yamt 	sc->sc_cfgflags |= CFGFLG_CARDBUS_HACK;
    125   1.1      yamt #endif
    126  1.10   mycroft 
    127  1.10   mycroft 	error = cs_pcmcia_enable(sc);
    128  1.10   mycroft 	if (error)
    129  1.10   mycroft 		goto fail;
    130  1.10   mycroft 
    131   1.1      yamt 	sc->sc_enable = cs_pcmcia_enable;
    132   1.1      yamt 	sc->sc_disable = cs_pcmcia_disable;
    133   1.1      yamt 
    134   1.1      yamt 	/* chip attach */
    135  1.10   mycroft 	error = cs_attach(sc, 0, 0, 0, 0);
    136  1.10   mycroft 	if (error)
    137  1.10   mycroft 		goto fail2;
    138   1.1      yamt 
    139   1.1      yamt 	cs_pcmcia_disable(sc);
    140  1.10   mycroft 	psc->sc_state = CS_PCMCIA_ATTACHED;
    141   1.1      yamt 	return;
    142   1.1      yamt 
    143  1.10   mycroft fail2:
    144  1.10   mycroft 	cs_pcmcia_disable(sc);
    145   1.1      yamt fail:
    146  1.10   mycroft 	pcmcia_function_unconfigure(pf);
    147   1.1      yamt }
    148   1.1      yamt 
    149   1.1      yamt static int
    150  1.18    cegger cs_pcmcia_detach(device_t self, int flags)
    151   1.1      yamt {
    152  1.20       chs 	struct cs_pcmcia_softc *psc = device_private(self);
    153   1.1      yamt 	struct cs_softc *sc = &psc->sc_cs;
    154  1.10   mycroft 	int error;
    155   1.1      yamt 
    156  1.10   mycroft 	if (psc->sc_state != CS_PCMCIA_ATTACHED)
    157  1.10   mycroft 		return (0);
    158  1.10   mycroft 
    159  1.10   mycroft 	error = cs_detach(sc);
    160  1.10   mycroft 	if (error)
    161  1.10   mycroft 		return (error);
    162  1.11     perry 
    163  1.10   mycroft 	pcmcia_function_unconfigure(psc->sc_pf);
    164   1.1      yamt 
    165  1.10   mycroft 	return (0);
    166   1.1      yamt }
    167   1.1      yamt 
    168   1.1      yamt static int
    169   1.1      yamt cs_pcmcia_enable(struct cs_softc *sc)
    170   1.1      yamt {
    171   1.1      yamt 	struct cs_pcmcia_softc *psc = (void *)sc;
    172  1.10   mycroft 	int error;
    173   1.1      yamt 
    174  1.10   mycroft 	sc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, cs_intr, sc);
    175  1.10   mycroft 	if (!sc->sc_ih)
    176  1.10   mycroft 		return (EIO);
    177  1.10   mycroft 
    178  1.10   mycroft 	error = pcmcia_function_enable(psc->sc_pf);
    179  1.10   mycroft 	if (error) {
    180  1.10   mycroft 		pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih);
    181  1.10   mycroft 		sc->sc_ih = 0;
    182   1.1      yamt 	}
    183   1.1      yamt 
    184  1.10   mycroft 	return (error);
    185   1.1      yamt }
    186   1.1      yamt 
    187   1.1      yamt static void
    188   1.1      yamt cs_pcmcia_disable(struct cs_softc *sc)
    189   1.1      yamt {
    190   1.1      yamt 	struct cs_pcmcia_softc *psc = (void *)sc;
    191   1.1      yamt 
    192  1.10   mycroft 	pcmcia_function_disable(psc->sc_pf);
    193  1.10   mycroft 	pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih);
    194  1.10   mycroft 	sc->sc_ih = 0;
    195   1.1      yamt }
    196