xhci_pci.c revision 1.5 1 1.5 skrll /* $NetBSD: xhci_pci.c,v 1.5 2016/04/23 10:15:31 skrll Exp $ */
2 1.5 skrll /* OpenBSD: xhci_pci.c,v 1.4 2014/07/12 17:38:51 yuo Exp */
3 1.1 jakllsch
4 1.1 jakllsch /*
5 1.1 jakllsch * Copyright (c) 1998 The NetBSD Foundation, Inc.
6 1.1 jakllsch * All rights reserved.
7 1.1 jakllsch *
8 1.1 jakllsch * This code is derived from software contributed to The NetBSD Foundation
9 1.1 jakllsch * by Lennart Augustsson (lennart (at) augustsson.net) at
10 1.1 jakllsch * Carlstedt Research & Technology.
11 1.1 jakllsch *
12 1.1 jakllsch * Redistribution and use in source and binary forms, with or without
13 1.1 jakllsch * modification, are permitted provided that the following conditions
14 1.1 jakllsch * are met:
15 1.1 jakllsch * 1. Redistributions of source code must retain the above copyright
16 1.1 jakllsch * notice, this list of conditions and the following disclaimer.
17 1.1 jakllsch * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 jakllsch * notice, this list of conditions and the following disclaimer in the
19 1.1 jakllsch * documentation and/or other materials provided with the distribution.
20 1.1 jakllsch *
21 1.1 jakllsch * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 1.1 jakllsch * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 1.1 jakllsch * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 1.1 jakllsch * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 1.1 jakllsch * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 1.1 jakllsch * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 1.1 jakllsch * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.1 jakllsch * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 1.1 jakllsch * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 1.1 jakllsch * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 1.1 jakllsch * POSSIBILITY OF SUCH DAMAGE.
32 1.1 jakllsch */
33 1.1 jakllsch
34 1.1 jakllsch #include <sys/cdefs.h>
35 1.5 skrll __KERNEL_RCSID(0, "$NetBSD: xhci_pci.c,v 1.5 2016/04/23 10:15:31 skrll Exp $");
36 1.1 jakllsch
37 1.1 jakllsch #include <sys/param.h>
38 1.1 jakllsch #include <sys/systm.h>
39 1.1 jakllsch #include <sys/kernel.h>
40 1.1 jakllsch #include <sys/device.h>
41 1.1 jakllsch #include <sys/proc.h>
42 1.1 jakllsch #include <sys/queue.h>
43 1.1 jakllsch
44 1.1 jakllsch #include <sys/bus.h>
45 1.1 jakllsch
46 1.1 jakllsch #include <dev/pci/pcivar.h>
47 1.5 skrll #include <dev/pci/pcidevs.h>
48 1.1 jakllsch
49 1.1 jakllsch #include <dev/usb/usb.h>
50 1.1 jakllsch #include <dev/usb/usbdi.h>
51 1.1 jakllsch #include <dev/usb/usbdivar.h>
52 1.1 jakllsch #include <dev/usb/usb_mem.h>
53 1.1 jakllsch
54 1.1 jakllsch #include <dev/usb/xhcireg.h>
55 1.1 jakllsch #include <dev/usb/xhcivar.h>
56 1.1 jakllsch
57 1.5 skrll struct xhci_pci_quirk {
58 1.5 skrll pci_vendor_id_t vendor;
59 1.5 skrll pci_product_id_t product;
60 1.5 skrll int quirks;
61 1.5 skrll };
62 1.5 skrll
63 1.5 skrll static const struct xhci_pci_quirk xhci_pci_quirks[] = {
64 1.5 skrll { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_CORE4G_M_XHCI,
65 1.5 skrll XHCI_QUIRK_FORCE_INTR },
66 1.5 skrll };
67 1.5 skrll
68 1.1 jakllsch struct xhci_pci_softc {
69 1.1 jakllsch struct xhci_softc sc_xhci;
70 1.1 jakllsch pci_chipset_tag_t sc_pc;
71 1.1 jakllsch pcitag_t sc_tag;
72 1.5 skrll void *sc_ih;
73 1.5 skrll pci_intr_handle_t *sc_pihp;
74 1.1 jakllsch };
75 1.1 jakllsch
76 1.1 jakllsch static int
77 1.5 skrll xhci_pci_has_quirk(pci_vendor_id_t vendor, pci_product_id_t product)
78 1.5 skrll {
79 1.5 skrll int i;
80 1.5 skrll
81 1.5 skrll for (i = 0; i < __arraycount(xhci_pci_quirks); i++)
82 1.5 skrll if (vendor == xhci_pci_quirks[i].vendor &&
83 1.5 skrll product == xhci_pci_quirks[i].product)
84 1.5 skrll return xhci_pci_quirks[i].quirks;
85 1.5 skrll return 0;
86 1.5 skrll }
87 1.5 skrll
88 1.5 skrll static int
89 1.1 jakllsch xhci_pci_match(device_t parent, cfdata_t match, void *aux)
90 1.1 jakllsch {
91 1.1 jakllsch struct pci_attach_args *pa = (struct pci_attach_args *) aux;
92 1.1 jakllsch
93 1.1 jakllsch if (PCI_CLASS(pa->pa_class) == PCI_CLASS_SERIALBUS &&
94 1.1 jakllsch PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_SERIALBUS_USB &&
95 1.1 jakllsch PCI_INTERFACE(pa->pa_class) == PCI_INTERFACE_XHCI)
96 1.1 jakllsch return 1;
97 1.1 jakllsch
98 1.1 jakllsch return 0;
99 1.1 jakllsch }
100 1.1 jakllsch
101 1.5 skrll static int
102 1.5 skrll xhci_pci_port_route(struct xhci_pci_softc *psc)
103 1.5 skrll {
104 1.5 skrll struct xhci_softc * const sc = &psc->sc_xhci;
105 1.5 skrll
106 1.5 skrll pcireg_t val;
107 1.5 skrll
108 1.5 skrll /*
109 1.5 skrll * Check USB3 Port Routing Mask register that indicates the ports
110 1.5 skrll * can be changed from OS, and turn on by USB3 Port SS Enable register.
111 1.5 skrll */
112 1.5 skrll val = pci_conf_read(psc->sc_pc, psc->sc_tag, PCI_XHCI_INTEL_USB3PRM);
113 1.5 skrll aprint_debug_dev(sc->sc_dev,
114 1.5 skrll "USB3PRM / USB3.0 configurable ports: 0x%08x\n", val);
115 1.5 skrll
116 1.5 skrll pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_XHCI_INTEL_USB3_PSSEN, val);
117 1.5 skrll val = pci_conf_read(psc->sc_pc, psc->sc_tag,PCI_XHCI_INTEL_USB3_PSSEN);
118 1.5 skrll aprint_debug_dev(sc->sc_dev,
119 1.5 skrll "USB3_PSSEN / Enabled USB3.0 ports under xHCI: 0x%08x\n", val);
120 1.5 skrll
121 1.5 skrll /*
122 1.5 skrll * Check USB2 Port Routing Mask register that indicates the USB2.0
123 1.5 skrll * ports to be controlled by xHCI HC, and switch them to xHCI HC.
124 1.5 skrll */
125 1.5 skrll val = pci_conf_read(psc->sc_pc, psc->sc_tag, PCI_XHCI_INTEL_USB2PRM);
126 1.5 skrll aprint_debug_dev(sc->sc_dev,
127 1.5 skrll "XUSB2PRM / USB2.0 ports can switch from EHCI to xHCI:"
128 1.5 skrll "0x%08x\n", val);
129 1.5 skrll pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_XHCI_INTEL_XUSB2PR, val);
130 1.5 skrll val = pci_conf_read(psc->sc_pc, psc->sc_tag, PCI_XHCI_INTEL_XUSB2PR);
131 1.5 skrll aprint_debug_dev(sc->sc_dev,
132 1.5 skrll "XUSB2PR / USB2.0 ports under xHCI: 0x%08x\n", val);
133 1.5 skrll
134 1.5 skrll return 0;
135 1.5 skrll }
136 1.5 skrll
137 1.1 jakllsch static void
138 1.1 jakllsch xhci_pci_attach(device_t parent, device_t self, void *aux)
139 1.1 jakllsch {
140 1.1 jakllsch struct xhci_pci_softc * const psc = device_private(self);
141 1.1 jakllsch struct xhci_softc * const sc = &psc->sc_xhci;
142 1.1 jakllsch struct pci_attach_args *const pa = (struct pci_attach_args *)aux;
143 1.1 jakllsch const pci_chipset_tag_t pc = pa->pa_pc;
144 1.1 jakllsch const pcitag_t tag = pa->pa_tag;
145 1.1 jakllsch char const *intrstr;
146 1.1 jakllsch pcireg_t csr, memtype;
147 1.2 skrll int err;
148 1.1 jakllsch uint32_t hccparams;
149 1.3 christos char intrbuf[PCI_INTRSTR_LEN];
150 1.1 jakllsch
151 1.1 jakllsch sc->sc_dev = self;
152 1.5 skrll sc->sc_bus.ub_hcpriv = sc;
153 1.1 jakllsch
154 1.1 jakllsch pci_aprint_devinfo(pa, "USB Controller");
155 1.1 jakllsch
156 1.5 skrll /* Check for quirks */
157 1.5 skrll sc->sc_quirks = xhci_pci_has_quirk(PCI_VENDOR(pa->pa_id),
158 1.5 skrll PCI_PRODUCT(pa->pa_id));
159 1.5 skrll
160 1.1 jakllsch /* check if memory space access is enabled */
161 1.1 jakllsch csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
162 1.1 jakllsch #ifdef DEBUG
163 1.5 skrll printf("%s: csr: %08x\n", __func__, csr);
164 1.1 jakllsch #endif
165 1.1 jakllsch if ((csr & PCI_COMMAND_MEM_ENABLE) == 0) {
166 1.1 jakllsch aprint_error_dev(self, "memory access is disabled\n");
167 1.1 jakllsch return;
168 1.1 jakllsch }
169 1.1 jakllsch
170 1.1 jakllsch /* map MMIO registers */
171 1.1 jakllsch memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, PCI_CBMEM);
172 1.1 jakllsch switch (memtype) {
173 1.1 jakllsch case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
174 1.1 jakllsch case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
175 1.1 jakllsch if (pci_mapreg_map(pa, PCI_CBMEM, memtype, 0,
176 1.1 jakllsch &sc->sc_iot, &sc->sc_ioh, NULL, &sc->sc_ios)) {
177 1.1 jakllsch sc->sc_ios = 0;
178 1.1 jakllsch aprint_error_dev(self, "can't map mem space\n");
179 1.1 jakllsch return;
180 1.1 jakllsch }
181 1.1 jakllsch break;
182 1.1 jakllsch default:
183 1.1 jakllsch aprint_error_dev(self, "BAR not 64 or 32-bit MMIO\n");
184 1.1 jakllsch return;
185 1.1 jakllsch }
186 1.1 jakllsch
187 1.1 jakllsch psc->sc_pc = pc;
188 1.1 jakllsch psc->sc_tag = tag;
189 1.1 jakllsch
190 1.5 skrll hccparams = bus_space_read_4(sc->sc_iot, sc->sc_ioh, XHCI_HCCPARAMS);
191 1.1 jakllsch
192 1.5 skrll if (pci_dma64_available(pa) && (XHCI_HCC_AC64(hccparams) != 0))
193 1.5 skrll sc->sc_bus.ub_dmatag = pa->pa_dmat64;
194 1.1 jakllsch else
195 1.5 skrll sc->sc_bus.ub_dmatag = pa->pa_dmat;
196 1.1 jakllsch
197 1.1 jakllsch /* Enable the device. */
198 1.1 jakllsch pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG,
199 1.1 jakllsch csr | PCI_COMMAND_MASTER_ENABLE);
200 1.1 jakllsch
201 1.5 skrll /* Allocate and establish the interrupt. */
202 1.5 skrll if (pci_intr_alloc(pa, &psc->sc_pihp, NULL, 0)) {
203 1.5 skrll aprint_error_dev(self, "can't allocate handler\n");
204 1.1 jakllsch goto fail;
205 1.1 jakllsch }
206 1.5 skrll intrstr = pci_intr_string(pc, psc->sc_pihp[0], intrbuf,
207 1.5 skrll sizeof(intrbuf));
208 1.5 skrll psc->sc_ih = pci_intr_establish(pc, psc->sc_pihp[0], IPL_USB,
209 1.5 skrll xhci_intr, sc);
210 1.5 skrll if (psc->sc_ih == NULL) {
211 1.1 jakllsch aprint_error_dev(self, "couldn't establish interrupt");
212 1.1 jakllsch if (intrstr != NULL)
213 1.1 jakllsch aprint_error(" at %s", intrstr);
214 1.1 jakllsch aprint_error("\n");
215 1.1 jakllsch goto fail;
216 1.1 jakllsch }
217 1.1 jakllsch aprint_normal_dev(self, "interrupting at %s\n", intrstr);
218 1.1 jakllsch
219 1.1 jakllsch /* Figure out vendor for root hub descriptor. */
220 1.1 jakllsch sc->sc_id_vendor = PCI_VENDOR(pa->pa_id);
221 1.4 christos pci_findvendor(sc->sc_vendor, sizeof(sc->sc_vendor),
222 1.4 christos sc->sc_id_vendor);
223 1.5 skrll
224 1.5 skrll /* Intel chipset requires SuperSpeed enable and USB2 port routing */
225 1.5 skrll switch (PCI_VENDOR(pa->pa_id)) {
226 1.5 skrll case PCI_VENDOR_INTEL:
227 1.5 skrll sc->sc_quirks |= XHCI_QUIRK_INTEL;
228 1.5 skrll break;
229 1.5 skrll default:
230 1.5 skrll break;
231 1.5 skrll }
232 1.1 jakllsch
233 1.2 skrll err = xhci_init(sc);
234 1.2 skrll if (err) {
235 1.2 skrll aprint_error_dev(self, "init failed, error=%d\n", err);
236 1.1 jakllsch goto fail;
237 1.1 jakllsch }
238 1.1 jakllsch
239 1.5 skrll if ((sc->sc_quirks & XHCI_QUIRK_INTEL) != 0)
240 1.5 skrll xhci_pci_port_route(psc);
241 1.5 skrll
242 1.1 jakllsch if (!pmf_device_register1(self, xhci_suspend, xhci_resume,
243 1.1 jakllsch xhci_shutdown))
244 1.1 jakllsch aprint_error_dev(self, "couldn't establish power handler\n");
245 1.1 jakllsch
246 1.1 jakllsch /* Attach usb device. */
247 1.1 jakllsch sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint);
248 1.1 jakllsch return;
249 1.1 jakllsch
250 1.1 jakllsch fail:
251 1.5 skrll if (psc->sc_ih) {
252 1.5 skrll pci_intr_release(psc->sc_pc, psc->sc_pihp, 1);
253 1.5 skrll psc->sc_ih = NULL;
254 1.1 jakllsch }
255 1.1 jakllsch if (sc->sc_ios) {
256 1.1 jakllsch bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
257 1.1 jakllsch sc->sc_ios = 0;
258 1.1 jakllsch }
259 1.1 jakllsch return;
260 1.1 jakllsch }
261 1.1 jakllsch
262 1.1 jakllsch static int
263 1.1 jakllsch xhci_pci_detach(device_t self, int flags)
264 1.1 jakllsch {
265 1.1 jakllsch struct xhci_pci_softc * const psc = device_private(self);
266 1.1 jakllsch struct xhci_softc * const sc = &psc->sc_xhci;
267 1.1 jakllsch int rv;
268 1.1 jakllsch
269 1.1 jakllsch rv = xhci_detach(sc, flags);
270 1.1 jakllsch if (rv)
271 1.1 jakllsch return rv;
272 1.1 jakllsch
273 1.1 jakllsch pmf_device_deregister(self);
274 1.1 jakllsch
275 1.1 jakllsch xhci_shutdown(self, flags);
276 1.1 jakllsch
277 1.1 jakllsch if (sc->sc_ios) {
278 1.1 jakllsch #if 0
279 1.1 jakllsch /* Disable interrupts, so we don't get any spurious ones. */
280 1.1 jakllsch bus_space_write_4(sc->sc_iot, sc->sc_ioh,
281 1.1 jakllsch OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
282 1.1 jakllsch #endif
283 1.1 jakllsch }
284 1.1 jakllsch
285 1.5 skrll if (psc->sc_ih != NULL) {
286 1.5 skrll pci_intr_release(psc->sc_pc, psc->sc_pihp, 1);
287 1.5 skrll psc->sc_ih = NULL;
288 1.1 jakllsch }
289 1.1 jakllsch if (sc->sc_ios) {
290 1.1 jakllsch bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
291 1.1 jakllsch sc->sc_ios = 0;
292 1.1 jakllsch }
293 1.1 jakllsch
294 1.1 jakllsch return 0;
295 1.1 jakllsch }
296 1.1 jakllsch
297 1.1 jakllsch CFATTACH_DECL3_NEW(xhci_pci, sizeof(struct xhci_pci_softc),
298 1.1 jakllsch xhci_pci_match, xhci_pci_attach, xhci_pci_detach, xhci_activate, NULL,
299 1.1 jakllsch xhci_childdet, DVF_DETACH_SHUTDOWN);
300