xhci_pci.c revision 1.1 1 /* $NetBSD: xhci_pci.c,v 1.1 2013/09/14 00:40:31 jakllsch Exp $ */
2
3 /*
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart (at) augustsson.net) at
9 * Carlstedt Research & Technology.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: xhci_pci.c,v 1.1 2013/09/14 00:40:31 jakllsch Exp $");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/device.h>
40 #include <sys/proc.h>
41 #include <sys/queue.h>
42
43 #include <sys/bus.h>
44
45 #include <dev/pci/pcivar.h>
46
47 #include <dev/usb/usb.h>
48 #include <dev/usb/usbdi.h>
49 #include <dev/usb/usbdivar.h>
50 #include <dev/usb/usb_mem.h>
51
52 #include <dev/usb/xhcireg.h>
53 #include <dev/usb/xhcivar.h>
54
55 struct xhci_pci_softc {
56 struct xhci_softc sc_xhci;
57 pci_chipset_tag_t sc_pc;
58 pcitag_t sc_tag;
59 };
60
61 static int
62 xhci_pci_match(device_t parent, cfdata_t match, void *aux)
63 {
64 struct pci_attach_args *pa = (struct pci_attach_args *) aux;
65
66 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_SERIALBUS &&
67 PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_SERIALBUS_USB &&
68 PCI_INTERFACE(pa->pa_class) == PCI_INTERFACE_XHCI)
69 return 1;
70
71 return 0;
72 }
73
74 static void
75 xhci_pci_attach(device_t parent, device_t self, void *aux)
76 {
77 struct xhci_pci_softc * const psc = device_private(self);
78 struct xhci_softc * const sc = &psc->sc_xhci;
79 struct pci_attach_args *const pa = (struct pci_attach_args *)aux;
80 const pci_chipset_tag_t pc = pa->pa_pc;
81 const pcitag_t tag = pa->pa_tag;
82 char const *intrstr;
83 pci_intr_handle_t ih;
84 pcireg_t csr, memtype;
85 usbd_status r;
86 //const char *vendor;
87 uint32_t hccparams;
88
89 sc->sc_dev = self;
90 sc->sc_bus.hci_private = sc;
91
92 pci_aprint_devinfo(pa, "USB Controller");
93
94 /* check if memory space access is enabled */
95 csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
96 #ifdef DEBUG
97 printf("csr: %08x\n", csr);
98 #endif
99 if ((csr & PCI_COMMAND_MEM_ENABLE) == 0) {
100 aprint_error_dev(self, "memory access is disabled\n");
101 return;
102 }
103
104 /* map MMIO registers */
105 memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, PCI_CBMEM);
106 switch (memtype) {
107 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
108 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
109 if (pci_mapreg_map(pa, PCI_CBMEM, memtype, 0,
110 &sc->sc_iot, &sc->sc_ioh, NULL, &sc->sc_ios)) {
111 sc->sc_ios = 0;
112 aprint_error_dev(self, "can't map mem space\n");
113 return;
114 }
115 break;
116 default:
117 aprint_error_dev(self, "BAR not 64 or 32-bit MMIO\n");
118 return;
119 break;
120 }
121
122 psc->sc_pc = pc;
123 psc->sc_tag = tag;
124
125 hccparams = bus_space_read_4(sc->sc_iot, sc->sc_ioh, 0x10);
126
127 if (pci_dma64_available(pa) && ((hccparams&1)==1))
128 sc->sc_bus.dmatag = pa->pa_dmat64;
129 else
130 sc->sc_bus.dmatag = pa->pa_dmat;
131
132 /* Enable the device. */
133 pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG,
134 csr | PCI_COMMAND_MASTER_ENABLE);
135
136 /* Map and establish the interrupt. */
137 if (pci_intr_map(pa, &ih)) {
138 aprint_error_dev(self, "couldn't map interrupt\n");
139 goto fail;
140 }
141
142 /*
143 * Allocate IRQ
144 */
145 intrstr = pci_intr_string(pc, ih);
146 sc->sc_ih = pci_intr_establish(pc, ih, IPL_USB, xhci_intr, sc);
147 if (sc->sc_ih == NULL) {
148 aprint_error_dev(self, "couldn't establish interrupt");
149 if (intrstr != NULL)
150 aprint_error(" at %s", intrstr);
151 aprint_error("\n");
152 goto fail;
153 }
154 aprint_normal_dev(self, "interrupting at %s\n", intrstr);
155
156 #if 0
157 /* Figure out vendor for root hub descriptor. */
158 vendor = pci_findvendor(pa->pa_id);
159 sc->sc_id_vendor = PCI_VENDOR(pa->pa_id);
160 if (vendor)
161 strlcpy(sc->sc_vendor, vendor, sizeof(sc->sc_vendor));
162 else
163 snprintf(sc->sc_vendor, sizeof(sc->sc_vendor),
164 "vendor 0x%04x", PCI_VENDOR(pa->pa_id));
165 #endif
166
167 r = xhci_init(sc);
168 if (r != USBD_NORMAL_COMPLETION) {
169 aprint_error_dev(self, "init failed, error=%d\n", r);
170 goto fail;
171 }
172
173 if (!pmf_device_register1(self, xhci_suspend, xhci_resume,
174 xhci_shutdown))
175 aprint_error_dev(self, "couldn't establish power handler\n");
176
177 /* Attach usb device. */
178 sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint);
179 return;
180
181 fail:
182 if (sc->sc_ih) {
183 pci_intr_disestablish(psc->sc_pc, sc->sc_ih);
184 sc->sc_ih = NULL;
185 }
186 if (sc->sc_ios) {
187 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
188 sc->sc_ios = 0;
189 }
190 return;
191 }
192
193 static int
194 xhci_pci_detach(device_t self, int flags)
195 {
196 struct xhci_pci_softc * const psc = device_private(self);
197 struct xhci_softc * const sc = &psc->sc_xhci;
198 int rv;
199
200 rv = xhci_detach(sc, flags);
201 if (rv)
202 return rv;
203
204 pmf_device_deregister(self);
205
206 xhci_shutdown(self, flags);
207
208 if (sc->sc_ios) {
209 #if 0
210 /* Disable interrupts, so we don't get any spurious ones. */
211 bus_space_write_4(sc->sc_iot, sc->sc_ioh,
212 OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
213 #endif
214 }
215
216 if (sc->sc_ih != NULL) {
217 pci_intr_disestablish(psc->sc_pc, sc->sc_ih);
218 sc->sc_ih = NULL;
219 }
220 if (sc->sc_ios) {
221 bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
222 sc->sc_ios = 0;
223 }
224
225 return 0;
226 }
227
228 CFATTACH_DECL3_NEW(xhci_pci, sizeof(struct xhci_pci_softc),
229 xhci_pci_match, xhci_pci_attach, xhci_pci_detach, xhci_activate, NULL,
230 xhci_childdet, DVF_DETACH_SHUTDOWN);
231