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