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