xref: /netbsd/sys/dev/pcmcia/slhci_pcmcia.c (revision 71112a2e)
1*71112a2eSskrll /* $NetBSD: slhci_pcmcia.c,v 1.10 2016/04/23 10:15:31 skrll Exp $ */
23f11af64Skiyohara /*
33f11af64Skiyohara  * Not (c) 2007 Matthew Orgass
43f11af64Skiyohara  * This file is public domain, meaning anyone can make any use of part or all
53f11af64Skiyohara  * of this file including copying into other works without credit.  Any use,
63f11af64Skiyohara  * modified or not, is solely the responsibility of the user.  If this file is
73f11af64Skiyohara  * part of a collection then use in the collection is governed by the terms of
83f11af64Skiyohara  * the collection.
93f11af64Skiyohara  */
103f11af64Skiyohara 
113f11af64Skiyohara /* Glue for RATOC USB HOST CF+ Card (SL811HS chip) */
123f11af64Skiyohara 
133f11af64Skiyohara #include <sys/cdefs.h>
14*71112a2eSskrll __KERNEL_RCSID(0, "$NetBSD: slhci_pcmcia.c,v 1.10 2016/04/23 10:15:31 skrll Exp $");
153f11af64Skiyohara 
163f11af64Skiyohara #include <sys/param.h>
173f11af64Skiyohara #include <sys/device.h>
183f11af64Skiyohara #include <sys/queue.h>
193f11af64Skiyohara #include <sys/gcq.h>
203f11af64Skiyohara #include <sys/systm.h>
213f11af64Skiyohara #include <sys/errno.h>
223f11af64Skiyohara 
23a2a38285Sad #include <sys/bus.h>
243f11af64Skiyohara #include <dev/pcmcia/pcmciareg.h>
253f11af64Skiyohara #include <dev/pcmcia/pcmciavar.h>
263f11af64Skiyohara #include <dev/pcmcia/pcmciadevs.h>
273f11af64Skiyohara 
283f11af64Skiyohara #include <dev/usb/usb.h>
293f11af64Skiyohara #include <dev/usb/usbdi.h>
303f11af64Skiyohara #include <dev/usb/usbdivar.h>
313f11af64Skiyohara 
323f11af64Skiyohara #include <dev/ic/sl811hsvar.h>
333f11af64Skiyohara 
343f11af64Skiyohara struct slhci_pcmcia_softc {
353f11af64Skiyohara 	struct slhci_softc sc_slhci;
363f11af64Skiyohara 
373f11af64Skiyohara 	struct pcmcia_function *sc_pf;
383f11af64Skiyohara 	void * sc_ih;
393f11af64Skiyohara 	int sc_flags;
403f11af64Skiyohara #define PFL_ENABLED 		(0x1)
413f11af64Skiyohara };
423f11af64Skiyohara 
433f11af64Skiyohara 
447cf29912Scegger int slhci_pcmcia_probe(device_t, cfdata_t, void *);
457cf29912Scegger void slhci_pcmcia_attach(device_t, device_t, void *);
467cf29912Scegger int slhci_pcmcia_detach(device_t, int);
473f11af64Skiyohara int slhci_pcmcia_validate_config(struct pcmcia_config_entry *);
483f11af64Skiyohara int slhci_pcmcia_enable(struct slhci_pcmcia_softc *, int);
493f11af64Skiyohara 
50fd0ded75Sdrochner CFATTACH_DECL_NEW(slhci_pcmcia, sizeof(struct slhci_pcmcia_softc),
513f11af64Skiyohara     slhci_pcmcia_probe, slhci_pcmcia_attach, slhci_pcmcia_detach,
523f11af64Skiyohara     slhci_activate);
533f11af64Skiyohara 
543f11af64Skiyohara /* Ratoc has two PCMCIA products with id 1.
553f11af64Skiyohara  * Ratoc has a firmware update that modifies the CIS.  The new CIS may
563f11af64Skiyohara  * need a new entry. */
573f11af64Skiyohara static const struct pcmcia_product slhci_pcmcia_products[] = {
583f11af64Skiyohara 	{ PCMCIA_VENDOR_RATOC, PCMCIA_PRODUCT_RATOC_REX_CFU1,
593f11af64Skiyohara 	  PCMCIA_CIS_RATOC_REX_CFU1 },
603f11af64Skiyohara };
613f11af64Skiyohara static const size_t slhci_pcmcia_nproducts =
623f11af64Skiyohara 	sizeof(slhci_pcmcia_products) / sizeof(slhci_pcmcia_products[0]);
633f11af64Skiyohara 
643f11af64Skiyohara int
slhci_pcmcia_probe(device_t parent,cfdata_t match,void * aux)657cf29912Scegger slhci_pcmcia_probe(device_t parent, cfdata_t match, void *aux)
663f11af64Skiyohara {
673f11af64Skiyohara 	struct pcmcia_attach_args *pa = aux;
683f11af64Skiyohara 
693f11af64Skiyohara 	if (pcmcia_product_lookup(pa, slhci_pcmcia_products,
703f11af64Skiyohara 	    slhci_pcmcia_nproducts, sizeof(slhci_pcmcia_products[0]), NULL))
713f11af64Skiyohara 		return 1;
723f11af64Skiyohara 
733f11af64Skiyohara 	return 0;
743f11af64Skiyohara }
753f11af64Skiyohara 
763f11af64Skiyohara int
slhci_pcmcia_validate_config(struct pcmcia_config_entry * cfe)773f11af64Skiyohara slhci_pcmcia_validate_config(struct pcmcia_config_entry *cfe)
783f11af64Skiyohara {
793f11af64Skiyohara 	if (cfe->num_iospace != 1 || cfe->num_memspace != 0)
803f11af64Skiyohara 		return EINVAL;
813f11af64Skiyohara 	return 0;
823f11af64Skiyohara }
833f11af64Skiyohara 
843f11af64Skiyohara void
slhci_pcmcia_attach(device_t parent,device_t self,void * aux)857cf29912Scegger slhci_pcmcia_attach(device_t parent, device_t self, void *aux)
863f11af64Skiyohara {
87fd0ded75Sdrochner 	struct slhci_pcmcia_softc *psc = device_private(self);
883f11af64Skiyohara 	struct pcmcia_attach_args *pa = aux;
893f11af64Skiyohara 	struct pcmcia_function *pf = pa->pf;
903f11af64Skiyohara 
91fd0ded75Sdrochner 	psc->sc_slhci.sc_dev = self;
92*71112a2eSskrll 	psc->sc_slhci.sc_bus.ub_hcpriv = &psc->sc_slhci;
93fd0ded75Sdrochner 
943f11af64Skiyohara 	psc->sc_pf = pf;
953f11af64Skiyohara 	psc->sc_flags = 0;
963f11af64Skiyohara 
973f11af64Skiyohara 	slhci_pcmcia_enable(psc, 1);
983f11af64Skiyohara 
993f11af64Skiyohara 	return;
1003f11af64Skiyohara }
1013f11af64Skiyohara 
1023f11af64Skiyohara int
slhci_pcmcia_detach(device_t self,int flags)1037cf29912Scegger slhci_pcmcia_detach(device_t self, int flags)
1043f11af64Skiyohara {
105fd0ded75Sdrochner 	struct slhci_pcmcia_softc *psc = device_private(self);
1063f11af64Skiyohara 
1073f11af64Skiyohara 	slhci_pcmcia_enable(psc, 0);
1083f11af64Skiyohara 
1093f11af64Skiyohara 	return slhci_detach(&psc->sc_slhci, flags);
1103f11af64Skiyohara }
1113f11af64Skiyohara 
1123f11af64Skiyohara int
slhci_pcmcia_enable(struct slhci_pcmcia_softc * psc,int enable)1133f11af64Skiyohara slhci_pcmcia_enable(struct slhci_pcmcia_softc *psc, int enable)
1143f11af64Skiyohara {
1153f11af64Skiyohara 	struct pcmcia_function *pf;
1163f11af64Skiyohara 	struct pcmcia_io_handle *pioh;
1173f11af64Skiyohara 	struct slhci_softc *sc;
1183f11af64Skiyohara 	int error;
1193f11af64Skiyohara 
1203f11af64Skiyohara 	pf = psc->sc_pf;
1213f11af64Skiyohara 	sc = &psc->sc_slhci;
1223f11af64Skiyohara 
1233f11af64Skiyohara 	if (enable) {
1243f11af64Skiyohara 		if (psc->sc_flags & PFL_ENABLED)
1253f11af64Skiyohara 			return 0;
1263f11af64Skiyohara 
1273f11af64Skiyohara 		error = pcmcia_function_configure(pf,
1283f11af64Skiyohara 		    slhci_pcmcia_validate_config);
1293f11af64Skiyohara 		if (error) {
1303f11af64Skiyohara 			printf("%s: configure failed, error=%d\n",
1313f11af64Skiyohara 			    SC_NAME(sc), error);
1323f11af64Skiyohara 			return 1;
1333f11af64Skiyohara 		}
1343f11af64Skiyohara 
1353f11af64Skiyohara 		pioh = &pf->cfe->iospace[0].handle;
1363f11af64Skiyohara 
1373f11af64Skiyohara 		/* The data port is repeated three times; using a stride of
1383f11af64Skiyohara 		 * 2 prevents read/write errors on a Clio C-1000 hpcmips
1393f11af64Skiyohara 		 * system.
1403f11af64Skiyohara 		 */
1413f11af64Skiyohara 		slhci_preinit(sc, NULL, pioh->iot, pioh->ioh, 100, 2);
1423f11af64Skiyohara 
14369b6c364Srmind 		psc->sc_ih = pcmcia_intr_establish(pf, IPL_USB,
1443f11af64Skiyohara 		    slhci_intr, sc);
1453f11af64Skiyohara 
1463f11af64Skiyohara 		if (psc->sc_ih == NULL) {
1473f11af64Skiyohara 			printf("%s: unable to establish interrupt\n",
1483f11af64Skiyohara 			    SC_NAME(sc));
1493f11af64Skiyohara 			goto fail1;
1503f11af64Skiyohara 		}
1513f11af64Skiyohara 
1523f11af64Skiyohara 		if (pcmcia_function_enable(pf)) {
1533f11af64Skiyohara 			printf("%s: function enable failed\n", SC_NAME(sc));
1543f11af64Skiyohara 			goto fail2;
1553f11af64Skiyohara 		}
1563f11af64Skiyohara 
1573f11af64Skiyohara 		if (slhci_attach(sc)) {
1583f11af64Skiyohara 			printf("%s: slhci_attach failed\n", SC_NAME(sc));
1593f11af64Skiyohara 			goto fail3;
1603f11af64Skiyohara 		}
1613f11af64Skiyohara 
1623f11af64Skiyohara 		psc->sc_flags |= PFL_ENABLED;
1633f11af64Skiyohara 		return 0;
1643f11af64Skiyohara 	} else {
1653f11af64Skiyohara 		if (!(psc->sc_flags & PFL_ENABLED))
1663f11af64Skiyohara 			return 1;
1673f11af64Skiyohara 		psc->sc_flags &= ~PFL_ENABLED;
1683f11af64Skiyohara fail3:
1693f11af64Skiyohara 		pcmcia_function_disable(pf);
1703f11af64Skiyohara fail2:
1713f11af64Skiyohara 		pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
1723f11af64Skiyohara fail1:
1733f11af64Skiyohara 		pcmcia_function_unconfigure(pf);
1743f11af64Skiyohara 
1753f11af64Skiyohara 		return 1;
1763f11af64Skiyohara 	}
1773f11af64Skiyohara }
1783f11af64Skiyohara 
1793f11af64Skiyohara 
180