xhci_pci.c revision 1.31 1 1.31 msaitoh /* $NetBSD: xhci_pci.c,v 1.31 2022/10/11 09:18:22 msaitoh 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.31 msaitoh __KERNEL_RCSID(0, "$NetBSD: xhci_pci.c,v 1.31 2022/10/11 09:18:22 msaitoh Exp $");
36 1.9 skrll
37 1.9 skrll #ifdef _KERNEL_OPT
38 1.9 skrll #include "opt_xhci_pci.h"
39 1.9 skrll #endif
40 1.1 jakllsch
41 1.1 jakllsch #include <sys/param.h>
42 1.1 jakllsch #include <sys/systm.h>
43 1.1 jakllsch #include <sys/kernel.h>
44 1.1 jakllsch #include <sys/device.h>
45 1.1 jakllsch #include <sys/proc.h>
46 1.1 jakllsch #include <sys/queue.h>
47 1.1 jakllsch
48 1.1 jakllsch #include <sys/bus.h>
49 1.1 jakllsch
50 1.1 jakllsch #include <dev/pci/pcivar.h>
51 1.5 skrll #include <dev/pci/pcidevs.h>
52 1.1 jakllsch
53 1.1 jakllsch #include <dev/usb/usb.h>
54 1.1 jakllsch #include <dev/usb/usbdi.h>
55 1.1 jakllsch #include <dev/usb/usbdivar.h>
56 1.1 jakllsch #include <dev/usb/usb_mem.h>
57 1.1 jakllsch
58 1.1 jakllsch #include <dev/usb/xhcireg.h>
59 1.1 jakllsch #include <dev/usb/xhcivar.h>
60 1.1 jakllsch
61 1.1 jakllsch struct xhci_pci_softc {
62 1.1 jakllsch struct xhci_softc sc_xhci;
63 1.1 jakllsch pci_chipset_tag_t sc_pc;
64 1.1 jakllsch pcitag_t sc_tag;
65 1.5 skrll void *sc_ih;
66 1.5 skrll pci_intr_handle_t *sc_pihp;
67 1.1 jakllsch };
68 1.1 jakllsch
69 1.1 jakllsch static int
70 1.1 jakllsch xhci_pci_match(device_t parent, cfdata_t match, void *aux)
71 1.1 jakllsch {
72 1.1 jakllsch struct pci_attach_args *pa = (struct pci_attach_args *) aux;
73 1.1 jakllsch
74 1.1 jakllsch if (PCI_CLASS(pa->pa_class) == PCI_CLASS_SERIALBUS &&
75 1.1 jakllsch PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_SERIALBUS_USB &&
76 1.1 jakllsch PCI_INTERFACE(pa->pa_class) == PCI_INTERFACE_XHCI)
77 1.1 jakllsch return 1;
78 1.1 jakllsch
79 1.1 jakllsch return 0;
80 1.1 jakllsch }
81 1.1 jakllsch
82 1.5 skrll static int
83 1.5 skrll xhci_pci_port_route(struct xhci_pci_softc *psc)
84 1.5 skrll {
85 1.5 skrll struct xhci_softc * const sc = &psc->sc_xhci;
86 1.5 skrll
87 1.5 skrll pcireg_t val;
88 1.5 skrll
89 1.5 skrll /*
90 1.5 skrll * Check USB3 Port Routing Mask register that indicates the ports
91 1.5 skrll * can be changed from OS, and turn on by USB3 Port SS Enable register.
92 1.5 skrll */
93 1.5 skrll val = pci_conf_read(psc->sc_pc, psc->sc_tag, PCI_XHCI_INTEL_USB3PRM);
94 1.5 skrll aprint_debug_dev(sc->sc_dev,
95 1.5 skrll "USB3PRM / USB3.0 configurable ports: 0x%08x\n", val);
96 1.5 skrll
97 1.5 skrll pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_XHCI_INTEL_USB3_PSSEN, val);
98 1.5 skrll val = pci_conf_read(psc->sc_pc, psc->sc_tag,PCI_XHCI_INTEL_USB3_PSSEN);
99 1.5 skrll aprint_debug_dev(sc->sc_dev,
100 1.5 skrll "USB3_PSSEN / Enabled USB3.0 ports under xHCI: 0x%08x\n", val);
101 1.5 skrll
102 1.5 skrll /*
103 1.5 skrll * Check USB2 Port Routing Mask register that indicates the USB2.0
104 1.5 skrll * ports to be controlled by xHCI HC, and switch them to xHCI HC.
105 1.5 skrll */
106 1.5 skrll val = pci_conf_read(psc->sc_pc, psc->sc_tag, PCI_XHCI_INTEL_USB2PRM);
107 1.5 skrll aprint_debug_dev(sc->sc_dev,
108 1.5 skrll "XUSB2PRM / USB2.0 ports can switch from EHCI to xHCI:"
109 1.5 skrll "0x%08x\n", val);
110 1.5 skrll pci_conf_write(psc->sc_pc, psc->sc_tag, PCI_XHCI_INTEL_XUSB2PR, val);
111 1.5 skrll val = pci_conf_read(psc->sc_pc, psc->sc_tag, PCI_XHCI_INTEL_XUSB2PR);
112 1.5 skrll aprint_debug_dev(sc->sc_dev,
113 1.5 skrll "XUSB2PR / USB2.0 ports under xHCI: 0x%08x\n", val);
114 1.5 skrll
115 1.5 skrll return 0;
116 1.5 skrll }
117 1.5 skrll
118 1.1 jakllsch static void
119 1.1 jakllsch xhci_pci_attach(device_t parent, device_t self, void *aux)
120 1.1 jakllsch {
121 1.1 jakllsch struct xhci_pci_softc * const psc = device_private(self);
122 1.1 jakllsch struct xhci_softc * const sc = &psc->sc_xhci;
123 1.1 jakllsch struct pci_attach_args *const pa = (struct pci_attach_args *)aux;
124 1.1 jakllsch const pci_chipset_tag_t pc = pa->pa_pc;
125 1.1 jakllsch const pcitag_t tag = pa->pa_tag;
126 1.1 jakllsch char const *intrstr;
127 1.13 msaitoh pcireg_t csr, memtype, usbrev;
128 1.1 jakllsch uint32_t hccparams;
129 1.27 dbj int counts[PCI_INTR_TYPE_SIZE];
130 1.3 christos char intrbuf[PCI_INTRSTR_LEN];
131 1.19 msaitoh bus_addr_t memaddr;
132 1.19 msaitoh int flags, msixoff;
133 1.19 msaitoh int err;
134 1.1 jakllsch
135 1.1 jakllsch sc->sc_dev = self;
136 1.1 jakllsch
137 1.1 jakllsch pci_aprint_devinfo(pa, "USB Controller");
138 1.1 jakllsch
139 1.5 skrll /* Check for quirks */
140 1.6 skrll sc->sc_quirks = 0;
141 1.5 skrll
142 1.1 jakllsch csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
143 1.1 jakllsch if ((csr & PCI_COMMAND_MEM_ENABLE) == 0) {
144 1.21 msaitoh /*
145 1.21 msaitoh * Enable address decoding for memory range in case BIOS or
146 1.21 msaitoh * UEFI didn't set it.
147 1.21 msaitoh */
148 1.21 msaitoh csr = pci_conf_read(pa->pa_pc, pa->pa_tag,
149 1.21 msaitoh PCI_COMMAND_STATUS_REG);
150 1.21 msaitoh csr |= PCI_COMMAND_MEM_ENABLE;
151 1.21 msaitoh pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
152 1.21 msaitoh csr);
153 1.1 jakllsch }
154 1.1 jakllsch
155 1.1 jakllsch /* map MMIO registers */
156 1.1 jakllsch memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, PCI_CBMEM);
157 1.19 msaitoh if (PCI_MAPREG_TYPE(memtype) != PCI_MAPREG_TYPE_MEM) {
158 1.14 mrg sc->sc_ios = 0;
159 1.1 jakllsch aprint_error_dev(self, "BAR not 64 or 32-bit MMIO\n");
160 1.1 jakllsch return;
161 1.1 jakllsch }
162 1.1 jakllsch
163 1.19 msaitoh sc->sc_iot = pa->pa_memt;
164 1.19 msaitoh if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, PCI_CBMEM, memtype,
165 1.19 msaitoh &memaddr, &sc->sc_ios, &flags) != 0) {
166 1.19 msaitoh sc->sc_ios = 0;
167 1.19 msaitoh aprint_error_dev(self, "can't get map info\n");
168 1.19 msaitoh return;
169 1.19 msaitoh }
170 1.19 msaitoh
171 1.19 msaitoh if (pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_MSIX, &msixoff,
172 1.19 msaitoh NULL)) {
173 1.19 msaitoh pcireg_t msixtbl;
174 1.19 msaitoh uint32_t table_offset;
175 1.19 msaitoh int bir;
176 1.19 msaitoh
177 1.19 msaitoh msixtbl = pci_conf_read(pa->pa_pc, pa->pa_tag,
178 1.19 msaitoh msixoff + PCI_MSIX_TBLOFFSET);
179 1.19 msaitoh table_offset = msixtbl & PCI_MSIX_TBLOFFSET_MASK;
180 1.24 msaitoh bir = msixtbl & PCI_MSIX_TBLBIR_MASK;
181 1.19 msaitoh /* Shrink map area for MSI-X table */
182 1.19 msaitoh if (bir == PCI_MAPREG_NUM(PCI_CBMEM))
183 1.19 msaitoh sc->sc_ios = table_offset;
184 1.19 msaitoh }
185 1.19 msaitoh if (bus_space_map(sc->sc_iot, memaddr, sc->sc_ios, flags,
186 1.19 msaitoh &sc->sc_ioh)) {
187 1.19 msaitoh sc->sc_ios = 0;
188 1.19 msaitoh aprint_error_dev(self, "can't map mem space\n");
189 1.19 msaitoh return;
190 1.19 msaitoh }
191 1.19 msaitoh
192 1.1 jakllsch psc->sc_pc = pc;
193 1.1 jakllsch psc->sc_tag = tag;
194 1.1 jakllsch
195 1.5 skrll hccparams = bus_space_read_4(sc->sc_iot, sc->sc_ioh, XHCI_HCCPARAMS);
196 1.1 jakllsch
197 1.25 skrll if (XHCI_HCC_AC64(hccparams) != 0) {
198 1.25 skrll aprint_verbose_dev(self, "64-bit DMA");
199 1.25 skrll if (pci_dma64_available(pa)) {
200 1.25 skrll sc->sc_bus.ub_dmatag = pa->pa_dmat64;
201 1.25 skrll aprint_verbose("\n");
202 1.25 skrll } else {
203 1.25 skrll aprint_verbose(" - limited\n");
204 1.25 skrll sc->sc_bus.ub_dmatag = pa->pa_dmat;
205 1.25 skrll }
206 1.25 skrll } else {
207 1.25 skrll aprint_verbose_dev(self, "32-bit DMA\n");
208 1.5 skrll sc->sc_bus.ub_dmatag = pa->pa_dmat;
209 1.25 skrll }
210 1.1 jakllsch
211 1.1 jakllsch /* Enable the device. */
212 1.1 jakllsch pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG,
213 1.1 jakllsch csr | PCI_COMMAND_MASTER_ENABLE);
214 1.1 jakllsch
215 1.27 dbj for (int i = 0; i < PCI_INTR_TYPE_SIZE; i++) {
216 1.27 dbj counts[i] = 1;
217 1.27 dbj }
218 1.27 dbj #ifdef XHCI_DISABLE_MSI
219 1.27 dbj counts[PCI_INTR_TYPE_MSI] = 0;
220 1.27 dbj #endif
221 1.27 dbj #ifdef XHCI_DISABLE_MSIX
222 1.27 dbj counts[PCI_INTR_TYPE_MSIX] = 0;
223 1.27 dbj #endif
224 1.27 dbj
225 1.5 skrll /* Allocate and establish the interrupt. */
226 1.28 ryoon if (pci_intr_alloc(pa, &psc->sc_pihp, counts, PCI_INTR_TYPE_MSIX)) {
227 1.5 skrll aprint_error_dev(self, "can't allocate handler\n");
228 1.1 jakllsch goto fail;
229 1.1 jakllsch }
230 1.5 skrll intrstr = pci_intr_string(pc, psc->sc_pihp[0], intrbuf,
231 1.5 skrll sizeof(intrbuf));
232 1.7 jdolecek psc->sc_ih = pci_intr_establish_xname(pc, psc->sc_pihp[0], IPL_USB,
233 1.7 jdolecek xhci_intr, sc, device_xname(sc->sc_dev));
234 1.5 skrll if (psc->sc_ih == NULL) {
235 1.10 msaitoh pci_intr_release(pc, psc->sc_pihp, 1);
236 1.26 jakllsch psc->sc_pihp = NULL;
237 1.18 jdolecek aprint_error_dev(self, "couldn't establish interrupt");
238 1.18 jdolecek if (intrstr != NULL)
239 1.18 jdolecek aprint_error(" at %s", intrstr);
240 1.18 jdolecek aprint_error("\n");
241 1.18 jdolecek goto fail;
242 1.1 jakllsch }
243 1.1 jakllsch aprint_normal_dev(self, "interrupting at %s\n", intrstr);
244 1.1 jakllsch
245 1.13 msaitoh usbrev = pci_conf_read(pc, tag, PCI_USBREV) & PCI_USBREV_MASK;
246 1.13 msaitoh switch (usbrev) {
247 1.13 msaitoh case PCI_USBREV_3_0:
248 1.13 msaitoh sc->sc_bus.ub_revision = USBREV_3_0;
249 1.13 msaitoh break;
250 1.13 msaitoh case PCI_USBREV_3_1:
251 1.13 msaitoh sc->sc_bus.ub_revision = USBREV_3_1;
252 1.13 msaitoh break;
253 1.13 msaitoh default:
254 1.13 msaitoh if (usbrev < PCI_USBREV_3_0) {
255 1.20 skrll aprint_error_dev(self, "Unknown revision (%02x). Set to 3.0.\n",
256 1.13 msaitoh usbrev);
257 1.20 skrll sc->sc_bus.ub_revision = USBREV_3_0;
258 1.13 msaitoh } else {
259 1.13 msaitoh /* Default to the latest revision */
260 1.13 msaitoh aprint_normal_dev(self,
261 1.13 msaitoh "Unknown revision (%02x). Set to 3.1.\n", usbrev);
262 1.13 msaitoh sc->sc_bus.ub_revision = USBREV_3_1;
263 1.13 msaitoh }
264 1.13 msaitoh break;
265 1.13 msaitoh }
266 1.13 msaitoh
267 1.5 skrll /* Intel chipset requires SuperSpeed enable and USB2 port routing */
268 1.5 skrll switch (PCI_VENDOR(pa->pa_id)) {
269 1.5 skrll case PCI_VENDOR_INTEL:
270 1.5 skrll sc->sc_quirks |= XHCI_QUIRK_INTEL;
271 1.5 skrll break;
272 1.5 skrll default:
273 1.5 skrll break;
274 1.5 skrll }
275 1.1 jakllsch
276 1.2 skrll err = xhci_init(sc);
277 1.2 skrll if (err) {
278 1.2 skrll aprint_error_dev(self, "init failed, error=%d\n", err);
279 1.1 jakllsch goto fail;
280 1.1 jakllsch }
281 1.1 jakllsch
282 1.5 skrll if ((sc->sc_quirks & XHCI_QUIRK_INTEL) != 0)
283 1.5 skrll xhci_pci_port_route(psc);
284 1.5 skrll
285 1.1 jakllsch if (!pmf_device_register1(self, xhci_suspend, xhci_resume,
286 1.1 jakllsch xhci_shutdown))
287 1.1 jakllsch aprint_error_dev(self, "couldn't establish power handler\n");
288 1.1 jakllsch
289 1.8 skrll /* Attach usb buses. */
290 1.31 msaitoh if (sc->sc_usb3nports != 0)
291 1.31 msaitoh sc->sc_child =
292 1.31 msaitoh config_found(self, &sc->sc_bus, usbctlprint, CFARGS_NONE);
293 1.31 msaitoh
294 1.31 msaitoh if (sc->sc_usb2nports != 0)
295 1.31 msaitoh sc->sc_child2 =
296 1.31 msaitoh config_found(self, &sc->sc_bus2, usbctlprint, CFARGS_NONE);
297 1.8 skrll
298 1.1 jakllsch return;
299 1.1 jakllsch
300 1.1 jakllsch fail:
301 1.10 msaitoh if (psc->sc_ih != NULL) {
302 1.10 msaitoh pci_intr_disestablish(psc->sc_pc, psc->sc_ih);
303 1.10 msaitoh psc->sc_ih = NULL;
304 1.10 msaitoh }
305 1.10 msaitoh if (psc->sc_pihp != NULL) {
306 1.5 skrll pci_intr_release(psc->sc_pc, psc->sc_pihp, 1);
307 1.10 msaitoh psc->sc_pihp = NULL;
308 1.1 jakllsch }
309 1.1 jakllsch if (sc->sc_ios) {
310 1.1 jakllsch bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
311 1.1 jakllsch sc->sc_ios = 0;
312 1.1 jakllsch }
313 1.1 jakllsch return;
314 1.1 jakllsch }
315 1.1 jakllsch
316 1.1 jakllsch static int
317 1.1 jakllsch xhci_pci_detach(device_t self, int flags)
318 1.1 jakllsch {
319 1.1 jakllsch struct xhci_pci_softc * const psc = device_private(self);
320 1.1 jakllsch struct xhci_softc * const sc = &psc->sc_xhci;
321 1.1 jakllsch int rv;
322 1.1 jakllsch
323 1.11 msaitoh if (sc->sc_ios != 0) {
324 1.11 msaitoh rv = xhci_detach(sc, flags);
325 1.11 msaitoh if (rv)
326 1.11 msaitoh return rv;
327 1.1 jakllsch
328 1.11 msaitoh pmf_device_deregister(self);
329 1.1 jakllsch
330 1.11 msaitoh xhci_shutdown(self, flags);
331 1.1 jakllsch
332 1.1 jakllsch #if 0
333 1.1 jakllsch /* Disable interrupts, so we don't get any spurious ones. */
334 1.1 jakllsch bus_space_write_4(sc->sc_iot, sc->sc_ioh,
335 1.1 jakllsch OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
336 1.1 jakllsch #endif
337 1.1 jakllsch }
338 1.1 jakllsch
339 1.5 skrll if (psc->sc_ih != NULL) {
340 1.10 msaitoh pci_intr_disestablish(psc->sc_pc, psc->sc_ih);
341 1.10 msaitoh psc->sc_ih = NULL;
342 1.10 msaitoh }
343 1.10 msaitoh if (psc->sc_pihp != NULL) {
344 1.5 skrll pci_intr_release(psc->sc_pc, psc->sc_pihp, 1);
345 1.10 msaitoh psc->sc_pihp = NULL;
346 1.1 jakllsch }
347 1.1 jakllsch if (sc->sc_ios) {
348 1.1 jakllsch bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
349 1.1 jakllsch sc->sc_ios = 0;
350 1.1 jakllsch }
351 1.1 jakllsch
352 1.1 jakllsch return 0;
353 1.1 jakllsch }
354 1.1 jakllsch
355 1.1 jakllsch CFATTACH_DECL3_NEW(xhci_pci, sizeof(struct xhci_pci_softc),
356 1.1 jakllsch xhci_pci_match, xhci_pci_attach, xhci_pci_detach, xhci_activate, NULL,
357 1.1 jakllsch xhci_childdet, DVF_DETACH_SHUTDOWN);
358