Home | History | Annotate | Line # | Download | only in pcmcia
slhci_pcmcia.c revision 1.2
      1  1.2  kiyohara /* $NetBSD: slhci_pcmcia.c,v 1.2 2007/08/16 02:30:22 kiyohara 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.2  kiyohara __KERNEL_RCSID(0, "$NetBSD: slhci_pcmcia.c,v 1.2 2007/08/16 02:30:22 kiyohara 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.2  kiyohara #include <machine/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/usb_port.h>
     30  1.2  kiyohara #include <dev/usb/usbdi.h>
     31  1.2  kiyohara #include <dev/usb/usbdivar.h>
     32  1.2  kiyohara 
     33  1.2  kiyohara #include <dev/ic/sl811hsvar.h>
     34  1.2  kiyohara 
     35  1.2  kiyohara struct slhci_pcmcia_softc {
     36  1.2  kiyohara 	struct slhci_softc sc_slhci;
     37  1.2  kiyohara 
     38  1.2  kiyohara 	struct pcmcia_function *sc_pf;
     39  1.2  kiyohara 	void * sc_ih;
     40  1.2  kiyohara 	int sc_flags;
     41  1.2  kiyohara #define PFL_ENABLED 		(0x1)
     42  1.2  kiyohara };
     43  1.2  kiyohara 
     44  1.2  kiyohara 
     45  1.2  kiyohara int slhci_pcmcia_probe(struct device *, struct cfdata *, void *);
     46  1.2  kiyohara void slhci_pcmcia_attach(struct device *, struct device *, void *);
     47  1.2  kiyohara int slhci_pcmcia_detach(struct device *, int);
     48  1.2  kiyohara int slhci_pcmcia_validate_config(struct pcmcia_config_entry *);
     49  1.2  kiyohara int slhci_pcmcia_enable(struct slhci_pcmcia_softc *, int);
     50  1.2  kiyohara 
     51  1.2  kiyohara CFATTACH_DECL(slhci_pcmcia, sizeof(struct slhci_pcmcia_softc),
     52  1.2  kiyohara     slhci_pcmcia_probe, slhci_pcmcia_attach, slhci_pcmcia_detach,
     53  1.2  kiyohara     slhci_activate);
     54  1.2  kiyohara 
     55  1.2  kiyohara /* Ratoc has two PCMCIA products with id 1.
     56  1.2  kiyohara  * Ratoc has a firmware update that modifies the CIS.  The new CIS may
     57  1.2  kiyohara  * need a new entry. */
     58  1.2  kiyohara static const struct pcmcia_product slhci_pcmcia_products[] = {
     59  1.2  kiyohara 	{ PCMCIA_VENDOR_RATOC, PCMCIA_PRODUCT_RATOC_REX_CFU1,
     60  1.2  kiyohara 	  PCMCIA_CIS_RATOC_REX_CFU1 },
     61  1.2  kiyohara };
     62  1.2  kiyohara static const size_t slhci_pcmcia_nproducts =
     63  1.2  kiyohara 	sizeof(slhci_pcmcia_products) / sizeof(slhci_pcmcia_products[0]);
     64  1.2  kiyohara 
     65  1.2  kiyohara int
     66  1.2  kiyohara slhci_pcmcia_probe(struct device *parent, struct cfdata *match, void *aux)
     67  1.2  kiyohara {
     68  1.2  kiyohara 	struct pcmcia_attach_args *pa = aux;
     69  1.2  kiyohara 
     70  1.2  kiyohara 	if (pcmcia_product_lookup(pa, slhci_pcmcia_products,
     71  1.2  kiyohara 	    slhci_pcmcia_nproducts, sizeof(slhci_pcmcia_products[0]), NULL))
     72  1.2  kiyohara 		return 1;
     73  1.2  kiyohara 
     74  1.2  kiyohara 	return 0;
     75  1.2  kiyohara }
     76  1.2  kiyohara 
     77  1.2  kiyohara int
     78  1.2  kiyohara slhci_pcmcia_validate_config(struct pcmcia_config_entry *cfe)
     79  1.2  kiyohara {
     80  1.2  kiyohara 	if (cfe->num_iospace != 1 || cfe->num_memspace != 0)
     81  1.2  kiyohara 		return EINVAL;
     82  1.2  kiyohara 	return 0;
     83  1.2  kiyohara }
     84  1.2  kiyohara 
     85  1.2  kiyohara void
     86  1.2  kiyohara slhci_pcmcia_attach(struct device *parent, struct device *self, void *aux)
     87  1.2  kiyohara {
     88  1.2  kiyohara 	struct slhci_pcmcia_softc *psc = (void *)self;
     89  1.2  kiyohara 	struct pcmcia_attach_args *pa = aux;
     90  1.2  kiyohara 	struct pcmcia_function *pf = pa->pf;
     91  1.2  kiyohara 
     92  1.2  kiyohara 	psc->sc_pf = pf;
     93  1.2  kiyohara 	psc->sc_flags = 0;
     94  1.2  kiyohara 
     95  1.2  kiyohara 	slhci_pcmcia_enable(psc, 1);
     96  1.2  kiyohara 
     97  1.2  kiyohara 	return;
     98  1.2  kiyohara }
     99  1.2  kiyohara 
    100  1.2  kiyohara int
    101  1.2  kiyohara slhci_pcmcia_detach(struct device *self, int flags)
    102  1.2  kiyohara {
    103  1.2  kiyohara 	struct slhci_pcmcia_softc *psc = (void *)self;
    104  1.2  kiyohara 
    105  1.2  kiyohara 	slhci_pcmcia_enable(psc, 0);
    106  1.2  kiyohara 
    107  1.2  kiyohara 	return slhci_detach(&psc->sc_slhci, flags);
    108  1.2  kiyohara }
    109  1.2  kiyohara 
    110  1.2  kiyohara int
    111  1.2  kiyohara slhci_pcmcia_enable(struct slhci_pcmcia_softc *psc, int enable)
    112  1.2  kiyohara {
    113  1.2  kiyohara 	struct pcmcia_function *pf;
    114  1.2  kiyohara 	struct pcmcia_io_handle *pioh;
    115  1.2  kiyohara 	struct slhci_softc *sc;
    116  1.2  kiyohara 	int error;
    117  1.2  kiyohara 
    118  1.2  kiyohara 	pf = psc->sc_pf;
    119  1.2  kiyohara 	sc = &psc->sc_slhci;
    120  1.2  kiyohara 
    121  1.2  kiyohara 	if (enable) {
    122  1.2  kiyohara 		if (psc->sc_flags & PFL_ENABLED)
    123  1.2  kiyohara 			return 0;
    124  1.2  kiyohara 
    125  1.2  kiyohara 		error = pcmcia_function_configure(pf,
    126  1.2  kiyohara 		    slhci_pcmcia_validate_config);
    127  1.2  kiyohara 		if (error) {
    128  1.2  kiyohara 			printf("%s: configure failed, error=%d\n",
    129  1.2  kiyohara 			    SC_NAME(sc), error);
    130  1.2  kiyohara 			return 1;
    131  1.2  kiyohara 		}
    132  1.2  kiyohara 
    133  1.2  kiyohara 		pioh = &pf->cfe->iospace[0].handle;
    134  1.2  kiyohara 
    135  1.2  kiyohara 		/* The data port is repeated three times; using a stride of
    136  1.2  kiyohara 		 * 2 prevents read/write errors on a Clio C-1000 hpcmips
    137  1.2  kiyohara 		 * system.
    138  1.2  kiyohara 		 */
    139  1.2  kiyohara 		slhci_preinit(sc, NULL, pioh->iot, pioh->ioh, 100, 2);
    140  1.2  kiyohara 
    141  1.2  kiyohara 		psc->sc_ih = pcmcia_intr_establish(pf, IPL_HARDUSB,
    142  1.2  kiyohara 		    slhci_intr, sc);
    143  1.2  kiyohara 
    144  1.2  kiyohara 		if (psc->sc_ih == NULL) {
    145  1.2  kiyohara 			printf("%s: unable to establish interrupt\n",
    146  1.2  kiyohara 			    SC_NAME(sc));
    147  1.2  kiyohara 			goto fail1;
    148  1.2  kiyohara 		}
    149  1.2  kiyohara 
    150  1.2  kiyohara 		if (pcmcia_function_enable(pf)) {
    151  1.2  kiyohara 			printf("%s: function enable failed\n", SC_NAME(sc));
    152  1.2  kiyohara 			goto fail2;
    153  1.2  kiyohara 		}
    154  1.2  kiyohara 
    155  1.2  kiyohara 		if (slhci_attach(sc)) {
    156  1.2  kiyohara 			printf("%s: slhci_attach failed\n", SC_NAME(sc));
    157  1.2  kiyohara 			goto fail3;
    158  1.2  kiyohara 		}
    159  1.2  kiyohara 
    160  1.2  kiyohara 		psc->sc_flags |= PFL_ENABLED;
    161  1.2  kiyohara 		return 0;
    162  1.2  kiyohara 	} else {
    163  1.2  kiyohara 		if (!(psc->sc_flags & PFL_ENABLED))
    164  1.2  kiyohara 			return 1;
    165  1.2  kiyohara 		psc->sc_flags &= ~PFL_ENABLED;
    166  1.2  kiyohara fail3:
    167  1.2  kiyohara 		pcmcia_function_disable(pf);
    168  1.2  kiyohara fail2:
    169  1.2  kiyohara 		pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    170  1.2  kiyohara fail1:
    171  1.2  kiyohara 		pcmcia_function_unconfigure(pf);
    172  1.2  kiyohara 
    173  1.2  kiyohara 		return 1;
    174  1.2  kiyohara 	}
    175  1.2  kiyohara }
    176  1.2  kiyohara 
    177  1.2  kiyohara 
    178