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