Home | History | Annotate | Line # | Download | only in pcmcia
      1  1.10     skrll /* $NetBSD: slhci_pcmcia.c,v 1.10 2016/04/23 10:15:31 skrll Exp $ */
      2   1.2  kiyohara /*
      3   1.2  kiyohara  * Not (c) 2007 Matthew Orgass
      4   1.2  kiyohara  * This file is public domain, meaning anyone can make any use of part or all
      5   1.2  kiyohara  * of this file including copying into other works without credit.  Any use,
      6   1.2  kiyohara  * modified or not, is solely the responsibility of the user.  If this file is
      7   1.2  kiyohara  * part of a collection then use in the collection is governed by the terms of
      8   1.2  kiyohara  * the collection.
      9   1.2  kiyohara  */
     10   1.2  kiyohara 
     11   1.2  kiyohara /* Glue for RATOC USB HOST CF+ Card (SL811HS chip) */
     12   1.2  kiyohara 
     13   1.2  kiyohara #include <sys/cdefs.h>
     14  1.10     skrll __KERNEL_RCSID(0, "$NetBSD: slhci_pcmcia.c,v 1.10 2016/04/23 10:15:31 skrll Exp $");
     15   1.2  kiyohara 
     16   1.2  kiyohara #include <sys/param.h>
     17   1.2  kiyohara #include <sys/device.h>
     18   1.2  kiyohara #include <sys/queue.h>
     19   1.2  kiyohara #include <sys/gcq.h>
     20   1.2  kiyohara #include <sys/systm.h>
     21   1.2  kiyohara #include <sys/errno.h>
     22   1.2  kiyohara 
     23   1.3        ad #include <sys/bus.h>
     24   1.2  kiyohara #include <dev/pcmcia/pcmciareg.h>
     25   1.2  kiyohara #include <dev/pcmcia/pcmciavar.h>
     26   1.2  kiyohara #include <dev/pcmcia/pcmciadevs.h>
     27   1.2  kiyohara 
     28   1.2  kiyohara #include <dev/usb/usb.h>
     29   1.2  kiyohara #include <dev/usb/usbdi.h>
     30   1.2  kiyohara #include <dev/usb/usbdivar.h>
     31   1.2  kiyohara 
     32   1.2  kiyohara #include <dev/ic/sl811hsvar.h>
     33   1.2  kiyohara 
     34   1.2  kiyohara struct slhci_pcmcia_softc {
     35   1.2  kiyohara 	struct slhci_softc sc_slhci;
     36   1.2  kiyohara 
     37   1.2  kiyohara 	struct pcmcia_function *sc_pf;
     38   1.2  kiyohara 	void * sc_ih;
     39   1.2  kiyohara 	int sc_flags;
     40   1.2  kiyohara #define PFL_ENABLED 		(0x1)
     41   1.2  kiyohara };
     42   1.2  kiyohara 
     43   1.2  kiyohara 
     44   1.6    cegger int slhci_pcmcia_probe(device_t, cfdata_t, void *);
     45   1.6    cegger void slhci_pcmcia_attach(device_t, device_t, void *);
     46   1.6    cegger int slhci_pcmcia_detach(device_t, int);
     47   1.2  kiyohara int slhci_pcmcia_validate_config(struct pcmcia_config_entry *);
     48   1.2  kiyohara int slhci_pcmcia_enable(struct slhci_pcmcia_softc *, int);
     49   1.2  kiyohara 
     50   1.4  drochner CFATTACH_DECL_NEW(slhci_pcmcia, sizeof(struct slhci_pcmcia_softc),
     51   1.2  kiyohara     slhci_pcmcia_probe, slhci_pcmcia_attach, slhci_pcmcia_detach,
     52   1.2  kiyohara     slhci_activate);
     53   1.2  kiyohara 
     54   1.2  kiyohara /* Ratoc has two PCMCIA products with id 1.
     55   1.2  kiyohara  * Ratoc has a firmware update that modifies the CIS.  The new CIS may
     56   1.2  kiyohara  * need a new entry. */
     57   1.2  kiyohara static const struct pcmcia_product slhci_pcmcia_products[] = {
     58   1.2  kiyohara 	{ PCMCIA_VENDOR_RATOC, PCMCIA_PRODUCT_RATOC_REX_CFU1,
     59   1.2  kiyohara 	  PCMCIA_CIS_RATOC_REX_CFU1 },
     60   1.2  kiyohara };
     61   1.2  kiyohara static const size_t slhci_pcmcia_nproducts =
     62   1.2  kiyohara 	sizeof(slhci_pcmcia_products) / sizeof(slhci_pcmcia_products[0]);
     63   1.2  kiyohara 
     64   1.2  kiyohara int
     65   1.6    cegger slhci_pcmcia_probe(device_t parent, cfdata_t match, void *aux)
     66   1.2  kiyohara {
     67   1.2  kiyohara 	struct pcmcia_attach_args *pa = aux;
     68   1.2  kiyohara 
     69   1.2  kiyohara 	if (pcmcia_product_lookup(pa, slhci_pcmcia_products,
     70   1.2  kiyohara 	    slhci_pcmcia_nproducts, sizeof(slhci_pcmcia_products[0]), NULL))
     71   1.2  kiyohara 		return 1;
     72   1.2  kiyohara 
     73   1.2  kiyohara 	return 0;
     74   1.2  kiyohara }
     75   1.2  kiyohara 
     76   1.2  kiyohara int
     77   1.2  kiyohara slhci_pcmcia_validate_config(struct pcmcia_config_entry *cfe)
     78   1.2  kiyohara {
     79   1.2  kiyohara 	if (cfe->num_iospace != 1 || cfe->num_memspace != 0)
     80   1.2  kiyohara 		return EINVAL;
     81   1.2  kiyohara 	return 0;
     82   1.2  kiyohara }
     83   1.2  kiyohara 
     84   1.2  kiyohara void
     85   1.6    cegger slhci_pcmcia_attach(device_t parent, device_t self, void *aux)
     86   1.2  kiyohara {
     87   1.4  drochner 	struct slhci_pcmcia_softc *psc = device_private(self);
     88   1.2  kiyohara 	struct pcmcia_attach_args *pa = aux;
     89   1.2  kiyohara 	struct pcmcia_function *pf = pa->pf;
     90   1.2  kiyohara 
     91   1.4  drochner 	psc->sc_slhci.sc_dev = self;
     92  1.10     skrll 	psc->sc_slhci.sc_bus.ub_hcpriv = &psc->sc_slhci;
     93   1.4  drochner 
     94   1.2  kiyohara 	psc->sc_pf = pf;
     95   1.2  kiyohara 	psc->sc_flags = 0;
     96   1.2  kiyohara 
     97   1.2  kiyohara 	slhci_pcmcia_enable(psc, 1);
     98   1.2  kiyohara 
     99   1.2  kiyohara 	return;
    100   1.2  kiyohara }
    101   1.2  kiyohara 
    102   1.2  kiyohara int
    103   1.6    cegger slhci_pcmcia_detach(device_t self, int flags)
    104   1.2  kiyohara {
    105   1.4  drochner 	struct slhci_pcmcia_softc *psc = device_private(self);
    106   1.2  kiyohara 
    107   1.2  kiyohara 	slhci_pcmcia_enable(psc, 0);
    108   1.2  kiyohara 
    109   1.2  kiyohara 	return slhci_detach(&psc->sc_slhci, flags);
    110   1.2  kiyohara }
    111   1.2  kiyohara 
    112   1.2  kiyohara int
    113   1.2  kiyohara slhci_pcmcia_enable(struct slhci_pcmcia_softc *psc, int enable)
    114   1.2  kiyohara {
    115   1.2  kiyohara 	struct pcmcia_function *pf;
    116   1.2  kiyohara 	struct pcmcia_io_handle *pioh;
    117   1.2  kiyohara 	struct slhci_softc *sc;
    118   1.2  kiyohara 	int error;
    119   1.2  kiyohara 
    120   1.2  kiyohara 	pf = psc->sc_pf;
    121   1.2  kiyohara 	sc = &psc->sc_slhci;
    122   1.2  kiyohara 
    123   1.2  kiyohara 	if (enable) {
    124   1.2  kiyohara 		if (psc->sc_flags & PFL_ENABLED)
    125   1.2  kiyohara 			return 0;
    126   1.2  kiyohara 
    127   1.2  kiyohara 		error = pcmcia_function_configure(pf,
    128   1.2  kiyohara 		    slhci_pcmcia_validate_config);
    129   1.2  kiyohara 		if (error) {
    130   1.2  kiyohara 			printf("%s: configure failed, error=%d\n",
    131   1.2  kiyohara 			    SC_NAME(sc), error);
    132   1.2  kiyohara 			return 1;
    133   1.2  kiyohara 		}
    134   1.2  kiyohara 
    135   1.2  kiyohara 		pioh = &pf->cfe->iospace[0].handle;
    136   1.2  kiyohara 
    137   1.2  kiyohara 		/* The data port is repeated three times; using a stride of
    138   1.2  kiyohara 		 * 2 prevents read/write errors on a Clio C-1000 hpcmips
    139   1.2  kiyohara 		 * system.
    140   1.2  kiyohara 		 */
    141   1.2  kiyohara 		slhci_preinit(sc, NULL, pioh->iot, pioh->ioh, 100, 2);
    142   1.2  kiyohara 
    143   1.9     rmind 		psc->sc_ih = pcmcia_intr_establish(pf, IPL_USB,
    144   1.2  kiyohara 		    slhci_intr, sc);
    145   1.2  kiyohara 
    146   1.2  kiyohara 		if (psc->sc_ih == NULL) {
    147   1.2  kiyohara 			printf("%s: unable to establish interrupt\n",
    148   1.2  kiyohara 			    SC_NAME(sc));
    149   1.2  kiyohara 			goto fail1;
    150   1.2  kiyohara 		}
    151   1.2  kiyohara 
    152   1.2  kiyohara 		if (pcmcia_function_enable(pf)) {
    153   1.2  kiyohara 			printf("%s: function enable failed\n", SC_NAME(sc));
    154   1.2  kiyohara 			goto fail2;
    155   1.2  kiyohara 		}
    156   1.2  kiyohara 
    157   1.2  kiyohara 		if (slhci_attach(sc)) {
    158   1.2  kiyohara 			printf("%s: slhci_attach failed\n", SC_NAME(sc));
    159   1.2  kiyohara 			goto fail3;
    160   1.2  kiyohara 		}
    161   1.2  kiyohara 
    162   1.2  kiyohara 		psc->sc_flags |= PFL_ENABLED;
    163   1.2  kiyohara 		return 0;
    164   1.2  kiyohara 	} else {
    165   1.2  kiyohara 		if (!(psc->sc_flags & PFL_ENABLED))
    166   1.2  kiyohara 			return 1;
    167   1.2  kiyohara 		psc->sc_flags &= ~PFL_ENABLED;
    168   1.2  kiyohara fail3:
    169   1.2  kiyohara 		pcmcia_function_disable(pf);
    170   1.2  kiyohara fail2:
    171   1.2  kiyohara 		pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    172   1.2  kiyohara fail1:
    173   1.2  kiyohara 		pcmcia_function_unconfigure(pf);
    174   1.2  kiyohara 
    175   1.2  kiyohara 		return 1;
    176   1.2  kiyohara 	}
    177   1.2  kiyohara }
    178   1.2  kiyohara 
    179   1.2  kiyohara 
    180