113540260SHans Petter Selasky /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni *
433cbbf26SHans Petter Selasky * Copyright (c) 2010-2022 Hans Petter Selasky
513540260SHans Petter Selasky *
613540260SHans Petter Selasky * Redistribution and use in source and binary forms, with or without
713540260SHans Petter Selasky * modification, are permitted provided that the following conditions
813540260SHans Petter Selasky * are met:
913540260SHans Petter Selasky * 1. Redistributions of source code must retain the above copyright
1013540260SHans Petter Selasky * notice, this list of conditions and the following disclaimer.
1113540260SHans Petter Selasky * 2. Redistributions in binary form must reproduce the above copyright
1213540260SHans Petter Selasky * notice, this list of conditions and the following disclaimer in the
1313540260SHans Petter Selasky * documentation and/or other materials provided with the distribution.
1413540260SHans Petter Selasky *
1513540260SHans Petter Selasky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1613540260SHans Petter Selasky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1713540260SHans Petter Selasky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1813540260SHans Petter Selasky * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1913540260SHans Petter Selasky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2013540260SHans Petter Selasky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2113540260SHans Petter Selasky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2213540260SHans Petter Selasky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2313540260SHans Petter Selasky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2413540260SHans Petter Selasky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2513540260SHans Petter Selasky * SUCH DAMAGE.
2613540260SHans Petter Selasky */
2713540260SHans Petter Selasky
2813540260SHans Petter Selasky #include <sys/stdint.h>
2913540260SHans Petter Selasky #include <sys/stddef.h>
3013540260SHans Petter Selasky #include <sys/param.h>
3113540260SHans Petter Selasky #include <sys/queue.h>
3213540260SHans Petter Selasky #include <sys/types.h>
3313540260SHans Petter Selasky #include <sys/systm.h>
3413540260SHans Petter Selasky #include <sys/kernel.h>
3513540260SHans Petter Selasky #include <sys/bus.h>
3613540260SHans Petter Selasky #include <sys/module.h>
3713540260SHans Petter Selasky #include <sys/lock.h>
3813540260SHans Petter Selasky #include <sys/mutex.h>
3913540260SHans Petter Selasky #include <sys/condvar.h>
4013540260SHans Petter Selasky #include <sys/sysctl.h>
4113540260SHans Petter Selasky #include <sys/sx.h>
4213540260SHans Petter Selasky #include <sys/unistd.h>
4313540260SHans Petter Selasky #include <sys/callout.h>
4413540260SHans Petter Selasky #include <sys/malloc.h>
4513540260SHans Petter Selasky #include <sys/priv.h>
4613540260SHans Petter Selasky
4713540260SHans Petter Selasky #include <dev/usb/usb.h>
4813540260SHans Petter Selasky #include <dev/usb/usbdi.h>
4913540260SHans Petter Selasky
5013540260SHans Petter Selasky #include <dev/usb/usb_core.h>
5113540260SHans Petter Selasky #include <dev/usb/usb_busdma.h>
5213540260SHans Petter Selasky #include <dev/usb/usb_process.h>
5313540260SHans Petter Selasky #include <dev/usb/usb_util.h>
5413540260SHans Petter Selasky
5513540260SHans Petter Selasky #include <dev/usb/usb_controller.h>
5613540260SHans Petter Selasky #include <dev/usb/usb_bus.h>
5713540260SHans Petter Selasky #include <dev/usb/usb_pci.h>
5813540260SHans Petter Selasky #include <dev/usb/controller/xhci.h>
5913540260SHans Petter Selasky #include <dev/usb/controller/xhcireg.h>
602e141748SHans Petter Selasky #include "usb_if.h"
6113540260SHans Petter Selasky
6242cf33ddSHans Petter Selasky #define PCI_XHCI_VENDORID_AMD 0x1022
6342cf33ddSHans Petter Selasky #define PCI_XHCI_VENDORID_INTEL 0x8086
642a31a06bSAlexander Motin #define PCI_XHCI_VENDORID_VMWARE 0x15ad
650d7064d5SDmitry Luhtionov #define PCI_XHCI_VENDORID_ZHAOXIN 0x1d17
6642cf33ddSHans Petter Selasky
6713540260SHans Petter Selasky static device_probe_t xhci_pci_probe;
6813540260SHans Petter Selasky static device_detach_t xhci_pci_detach;
692e141748SHans Petter Selasky static usb_take_controller_t xhci_pci_take_controller;
7013540260SHans Petter Selasky
7113540260SHans Petter Selasky static device_method_t xhci_device_methods[] = {
7213540260SHans Petter Selasky /* device interface */
7313540260SHans Petter Selasky DEVMETHOD(device_probe, xhci_pci_probe),
7413540260SHans Petter Selasky DEVMETHOD(device_attach, xhci_pci_attach),
7513540260SHans Petter Selasky DEVMETHOD(device_detach, xhci_pci_detach),
762e141748SHans Petter Selasky DEVMETHOD(device_suspend, bus_generic_suspend),
772e141748SHans Petter Selasky DEVMETHOD(device_resume, bus_generic_resume),
782e141748SHans Petter Selasky DEVMETHOD(device_shutdown, bus_generic_shutdown),
792e141748SHans Petter Selasky DEVMETHOD(usb_take_controller, xhci_pci_take_controller),
8013540260SHans Petter Selasky
814b7ec270SMarius Strobl DEVMETHOD_END
8213540260SHans Petter Selasky };
8313540260SHans Petter Selasky
8431e34625SAndrew Turner DEFINE_CLASS_0(xhci, xhci_pci_driver, xhci_device_methods,
8531e34625SAndrew Turner sizeof(struct xhci_softc));
8613540260SHans Petter Selasky
87bc9372d7SJohn Baldwin DRIVER_MODULE(xhci, pci, xhci_pci_driver, NULL, NULL);
8813540260SHans Petter Selasky MODULE_DEPEND(xhci, usb, 1, 1, 1);
8913540260SHans Petter Selasky
9013540260SHans Petter Selasky static const char *
xhci_pci_match(device_t self)9113540260SHans Petter Selasky xhci_pci_match(device_t self)
9213540260SHans Petter Selasky {
93f0d0cee0SAlexander Motin uint32_t device_id = pci_get_devid(self);
94f0d0cee0SAlexander Motin
95f0d0cee0SAlexander Motin switch (device_id) {
96d171d2f2SAlexander Motin case 0x145c1022:
97d171d2f2SAlexander Motin return ("AMD KERNCZ USB 3.0 controller");
984b4cce02SGleb Smirnoff case 0x148c1022:
994b4cce02SGleb Smirnoff return ("AMD Starship USB 3.0 controller");
1004b4cce02SGleb Smirnoff case 0x149c1022:
1014b4cce02SGleb Smirnoff return ("AMD Matisse USB 3.0 controller");
102*4d718f57STom Jones case 0x15b61022:
103*4d718f57STom Jones case 0x15b71022:
104*4d718f57STom Jones return ("AMD Raphael/Granite Ridge USB 3.1 controller");
105*4d718f57STom Jones case 0x15b81022:
106*4d718f57STom Jones return ("AMD Raphael/Granite Ridge USB 2.0 controller");
1074cc4b5e2SDmitry Luhtionov case 0x15e01022:
1084cc4b5e2SDmitry Luhtionov case 0x15e11022:
1094cc4b5e2SDmitry Luhtionov return ("AMD Raven USB 3.1 controller");
11066ad2538SConrad Meyer case 0x43ba1022:
11166ad2538SConrad Meyer return ("AMD X399 USB 3.0 controller");
112ca0a66baSMark Johnston case 0x43b91022: /* X370 */
113ca0a66baSMark Johnston case 0x43bb1022: /* B350 */
11483e67a9dSMarius Strobl return ("AMD 300 Series USB 3.1 controller");
11583e67a9dSMarius Strobl case 0x43d51022:
11683e67a9dSMarius Strobl return ("AMD 400 Series USB 3.1 controller");
117*4d718f57STom Jones case 0x43f71022:
118*4d718f57STom Jones return ("AMD 600 Series USB 3.2 controller");
119379797d4SJung-uk Kim case 0x78121022:
12015e01a35SAlexander Motin case 0x78141022:
12142cf33ddSHans Petter Selasky case 0x79141022:
12242cf33ddSHans Petter Selasky return ("AMD FCH USB 3.0 controller");
12315e01a35SAlexander Motin
1242a31a06bSAlexander Motin case 0x077815ad:
1252a31a06bSAlexander Motin case 0x077915ad:
1262a31a06bSAlexander Motin return ("VMware USB 3.0 controller");
1272a31a06bSAlexander Motin
128d82c0ebcSHans Petter Selasky case 0x145f1d94:
129d82c0ebcSHans Petter Selasky return ("Hygon USB 3.0 controller");
130d82c0ebcSHans Petter Selasky
131f0d0cee0SAlexander Motin case 0x01941033:
132f0d0cee0SAlexander Motin return ("NEC uPD720200 USB 3.0 controller");
1335df8285dSBruce M Simpson case 0x00151912:
1345df8285dSBruce M Simpson return ("NEC uPD720202 USB 3.0 controller");
135f0d0cee0SAlexander Motin
136dd4c1590SKevin Lo case 0x10001b73:
137dd4c1590SKevin Lo return ("Fresco Logic FL1000G USB 3.0 controller");
138082895ebSZhenlei Huang case 0x10091b73:
139082895ebSZhenlei Huang return ("Fresco Logic FL1009 USB 3.0 controller");
140f0db235bSMarius Strobl case 0x11001b73:
141f0db235bSMarius Strobl return ("Fresco Logic FL1100 USB 3.0 controller");
142dd4c1590SKevin Lo
143ec5cd818SAlexander Motin case 0x10421b21:
144ec5cd818SAlexander Motin return ("ASMedia ASM1042 USB 3.0 controller");
14543bc87c4SMarius Strobl case 0x11421b21:
14643bc87c4SMarius Strobl return ("ASMedia ASM1042A USB 3.0 controller");
147e85af89fSHans Petter Selasky case 0x13431b21:
148e85af89fSHans Petter Selasky return ("ASMedia ASM1143 USB 3.1 controller");
1494b4cce02SGleb Smirnoff case 0x32421b21:
1504b4cce02SGleb Smirnoff return ("ASMedia ASM3242 USB 3.2 controller");
151ec5cd818SAlexander Motin
152cfb0e4d7SAlexander Motin case 0x0b278086:
153cfb0e4d7SAlexander Motin return ("Intel Goshen Ridge Thunderbolt 4 USB controller");
1547eb88464SKevin Lo case 0x0f358086:
155dd4c1590SKevin Lo return ("Intel BayTrail USB 3.0 controller");
156cfb0e4d7SAlexander Motin case 0x11388086:
157cfb0e4d7SAlexander Motin return ("Intel Maple Ridge Thunderbolt 4 USB controller");
158cfb0e4d7SAlexander Motin case 0x15c18086:
159cfb0e4d7SAlexander Motin case 0x15d48086:
160cfb0e4d7SAlexander Motin case 0x15db8086:
161cfb0e4d7SAlexander Motin return ("Intel Alpine Ridge Thunderbolt 3 USB controller");
162cfb0e4d7SAlexander Motin case 0x15e98086:
163cfb0e4d7SAlexander Motin case 0x15ec8086:
164cfb0e4d7SAlexander Motin case 0x15f08086:
165cfb0e4d7SAlexander Motin return ("Intel Titan Ridge Thunderbolt 3 USB controller");
166eb76cfbcSAlexander Motin case 0x19d08086:
167eb76cfbcSAlexander Motin return ("Intel Denverton USB 3.0 controller");
16877bea00cSHans Petter Selasky case 0x9c318086:
169f0d0cee0SAlexander Motin case 0x1e318086:
170f0d0cee0SAlexander Motin return ("Intel Panther Point USB 3.0 controller");
171c5c43da8SMarius Strobl case 0x22b58086:
172c5c43da8SMarius Strobl return ("Intel Braswell USB 3.0 controller");
173c834f30aSHans Petter Selasky case 0x31a88086:
174c834f30aSHans Petter Selasky return ("Intel Gemini Lake USB 3.0 controller");
17583d7b2f3SAlexander Motin case 0x34ed8086:
17683d7b2f3SAlexander Motin return ("Intel Ice Lake-LP USB 3.1 controller");
17783d7b2f3SAlexander Motin case 0x43ed8086:
17883d7b2f3SAlexander Motin return ("Intel Tiger Lake-H USB 3.2 controller");
17983d7b2f3SAlexander Motin case 0x461e8086:
18083d7b2f3SAlexander Motin return ("Intel Alder Lake-P Thunderbolt 4 USB controller");
18183d7b2f3SAlexander Motin case 0x51ed8086:
18283d7b2f3SAlexander Motin return ("Intel Alder Lake USB 3.2 controller");
183c5c43da8SMarius Strobl case 0x5aa88086:
184c5c43da8SMarius Strobl return ("Intel Apollo Lake USB 3.0 controller");
185dc238358SAlexander Motin case 0x7ae08086:
186dc238358SAlexander Motin return ("Intel Alder Lake USB 3.2 controller");
18783d7b2f3SAlexander Motin case 0x8a138086:
18883d7b2f3SAlexander Motin return ("Intel Ice Lake Thunderbolt 3 USB controller");
189f4f6d5e0SAlexander Motin case 0x8c318086:
190f4f6d5e0SAlexander Motin return ("Intel Lynx Point USB 3.0 controller");
191e67f3becSAlexander Motin case 0x8cb18086:
192e67f3becSAlexander Motin return ("Intel Wildcat Point USB 3.0 controller");
193cb6b0ad1SAlexander Motin case 0x8d318086:
194cb6b0ad1SAlexander Motin return ("Intel Wellsburg USB 3.0 controller");
19583d7b2f3SAlexander Motin case 0x9a138086:
19683d7b2f3SAlexander Motin return ("Intel Tiger Lake-LP Thunderbolt 4 USB controller");
19783d7b2f3SAlexander Motin case 0x9a178086:
19883d7b2f3SAlexander Motin return ("Intel Tiger Lake-H Thunderbolt 4 USB controller");
1994be283b9SHans Petter Selasky case 0x9cb18086:
2004be283b9SHans Petter Selasky return ("Broadwell Integrated PCH-LP chipset USB 3.0 controller");
2012b064d46SAlexander Motin case 0x9d2f8086:
2022b064d46SAlexander Motin return ("Intel Sunrise Point-LP USB 3.0 controller");
20383d7b2f3SAlexander Motin case 0xa0ed8086:
20483d7b2f3SAlexander Motin return ("Intel Tiger Lake-LP USB 3.2 controller");
20571733a50SAlexander Motin case 0xa12f8086:
20671733a50SAlexander Motin return ("Intel Sunrise Point USB 3.0 controller");
207aaa9b2b3SAlexander Motin case 0xa1af8086:
208aaa9b2b3SAlexander Motin return ("Intel Lewisburg USB 3.0 controller");
209aaa9b2b3SAlexander Motin case 0xa2af8086:
210aaa9b2b3SAlexander Motin return ("Intel Union Point USB 3.0 controller");
2113194b270SHans Petter Selasky case 0xa36d8086:
212b55bfda7SHans Petter Selasky return ("Intel Cannon Lake USB 3.1 controller");
213f0d0cee0SAlexander Motin
214aafbd025SEd Maste case 0xa01b177d:
215aafbd025SEd Maste return ("Cavium ThunderX USB 3.0 controller");
216aafbd025SEd Maste
2174b4cce02SGleb Smirnoff case 0x1ada10de:
2184b4cce02SGleb Smirnoff return ("NVIDIA TU106 USB 3.1 controller");
2194b4cce02SGleb Smirnoff
220f50f5393SZhenlei Huang case 0x92021d17:
2210d7064d5SDmitry Luhtionov return ("Zhaoxin ZX-100 USB 3.0 controller");
222f50f5393SZhenlei Huang case 0x92031d17:
2230d7064d5SDmitry Luhtionov return ("Zhaoxin ZX-200 USB 3.0 controller");
224f50f5393SZhenlei Huang case 0x92041d17:
2250d7064d5SDmitry Luhtionov return ("Zhaoxin ZX-E USB 3.0 controller");
2260d7064d5SDmitry Luhtionov
227f0d0cee0SAlexander Motin default:
228f0d0cee0SAlexander Motin break;
229f0d0cee0SAlexander Motin }
230f0d0cee0SAlexander Motin
23113540260SHans Petter Selasky if ((pci_get_class(self) == PCIC_SERIALBUS)
23213540260SHans Petter Selasky && (pci_get_subclass(self) == PCIS_SERIALBUS_USB)
233934d7bccSRuslan Ermilov && (pci_get_progif(self) == PCIP_SERIALBUS_USB_XHCI)) {
23413540260SHans Petter Selasky return ("XHCI (generic) USB 3.0 controller");
23513540260SHans Petter Selasky }
23613540260SHans Petter Selasky return (NULL); /* dunno */
23713540260SHans Petter Selasky }
23813540260SHans Petter Selasky
23913540260SHans Petter Selasky static int
xhci_pci_probe(device_t self)24013540260SHans Petter Selasky xhci_pci_probe(device_t self)
24113540260SHans Petter Selasky {
24213540260SHans Petter Selasky const char *desc = xhci_pci_match(self);
24313540260SHans Petter Selasky
24413540260SHans Petter Selasky if (desc) {
24513540260SHans Petter Selasky device_set_desc(self, desc);
2469b0e3c5aSNeel Natu return (BUS_PROBE_DEFAULT);
24713540260SHans Petter Selasky } else {
24813540260SHans Petter Selasky return (ENXIO);
24913540260SHans Petter Selasky }
25013540260SHans Petter Selasky }
25113540260SHans Petter Selasky
25231b67ab2SKonstantin Belousov static int xhci_use_msi = 1;
25331b67ab2SKonstantin Belousov TUNABLE_INT("hw.usb.xhci.msi", &xhci_use_msi);
2542245b38fSAndrew Turner static int xhci_use_msix = 1;
2552245b38fSAndrew Turner TUNABLE_INT("hw.usb.xhci.msix", &xhci_use_msix);
25631b67ab2SKonstantin Belousov
25797d729cfSHans Petter Selasky static void
xhci_interrupt_poll(void * _sc)25897d729cfSHans Petter Selasky xhci_interrupt_poll(void *_sc)
25997d729cfSHans Petter Selasky {
26097d729cfSHans Petter Selasky struct xhci_softc *sc = _sc;
26197d729cfSHans Petter Selasky USB_BUS_UNLOCK(&sc->sc_bus);
26297d729cfSHans Petter Selasky xhci_interrupt(sc);
26397d729cfSHans Petter Selasky USB_BUS_LOCK(&sc->sc_bus);
26497d729cfSHans Petter Selasky usb_callout_reset(&sc->sc_callout, 1, (void *)&xhci_interrupt_poll, sc);
26597d729cfSHans Petter Selasky }
26697d729cfSHans Petter Selasky
26713540260SHans Petter Selasky static int
xhci_pci_port_route(device_t self,uint32_t set,uint32_t clear)2684c5d1323SHans Petter Selasky xhci_pci_port_route(device_t self, uint32_t set, uint32_t clear)
2694c5d1323SHans Petter Selasky {
2704c5d1323SHans Petter Selasky uint32_t temp;
2710a54509fSHans Petter Selasky uint32_t usb3_mask;
2720a54509fSHans Petter Selasky uint32_t usb2_mask;
2734c5d1323SHans Petter Selasky
2744c5d1323SHans Petter Selasky temp = pci_read_config(self, PCI_XHCI_INTEL_USB3_PSSEN, 4) |
2754c5d1323SHans Petter Selasky pci_read_config(self, PCI_XHCI_INTEL_XUSB2PR, 4);
2764c5d1323SHans Petter Selasky
2774c5d1323SHans Petter Selasky temp |= set;
2784c5d1323SHans Petter Selasky temp &= ~clear;
2794c5d1323SHans Petter Selasky
28088e0a639SHans Petter Selasky /* Don't set bits which the hardware doesn't support */
2810a54509fSHans Petter Selasky usb3_mask = pci_read_config(self, PCI_XHCI_INTEL_USB3PRM, 4);
2820a54509fSHans Petter Selasky usb2_mask = pci_read_config(self, PCI_XHCI_INTEL_USB2PRM, 4);
28388e0a639SHans Petter Selasky
2840a54509fSHans Petter Selasky pci_write_config(self, PCI_XHCI_INTEL_USB3_PSSEN, temp & usb3_mask, 4);
2850a54509fSHans Petter Selasky pci_write_config(self, PCI_XHCI_INTEL_XUSB2PR, temp & usb2_mask, 4);
2864c5d1323SHans Petter Selasky
2874c5d1323SHans Petter Selasky device_printf(self, "Port routing mask set to 0x%08x\n", temp);
2884c5d1323SHans Petter Selasky
2894c5d1323SHans Petter Selasky return (0);
2904c5d1323SHans Petter Selasky }
2914c5d1323SHans Petter Selasky
29231e34625SAndrew Turner int
xhci_pci_attach(device_t self)29313540260SHans Petter Selasky xhci_pci_attach(device_t self)
29413540260SHans Petter Selasky {
29513540260SHans Petter Selasky struct xhci_softc *sc = device_get_softc(self);
2962245b38fSAndrew Turner int count, err, msix_table, rid;
297dd4c1590SKevin Lo uint8_t usemsi = 1;
298dd4c1590SKevin Lo uint8_t usedma32 = 0;
29913540260SHans Petter Selasky
30013540260SHans Petter Selasky rid = PCI_XHCI_CBMEM;
30113540260SHans Petter Selasky sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
30213540260SHans Petter Selasky RF_ACTIVE);
30313540260SHans Petter Selasky if (!sc->sc_io_res) {
30413540260SHans Petter Selasky device_printf(self, "Could not map memory\n");
305b217d184SHans Petter Selasky return (ENOMEM);
30613540260SHans Petter Selasky }
30713540260SHans Petter Selasky sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
30813540260SHans Petter Selasky sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
30913540260SHans Petter Selasky sc->sc_io_size = rman_get_size(sc->sc_io_res);
31013540260SHans Petter Selasky
3110c31a8b0SHans Petter Selasky switch (pci_get_devid(self)) {
31219837718SHans Petter Selasky case 0x10091b73: /* Fresco Logic FL1009 USB3.0 xHCI Controller */
31333cbbf26SHans Petter Selasky case 0x8241104c: /* TUSB73x0 USB3.0 xHCI Controller */
31433cbbf26SHans Petter Selasky sc->sc_no_deconfigure = 1;
31533cbbf26SHans Petter Selasky break;
3160c31a8b0SHans Petter Selasky case 0x01941033: /* NEC uPD720200 USB 3.0 controller */
3170e8fa8c3SHans Petter Selasky case 0x00141912: /* NEC uPD720201 USB 3.0 controller */
31843bc87c4SMarius Strobl /* Don't use 64-bit DMA on these controllers. */
3190c31a8b0SHans Petter Selasky usedma32 = 1;
3200c31a8b0SHans Petter Selasky break;
321dd4c1590SKevin Lo case 0x10001b73: /* FL1000G */
322dd4c1590SKevin Lo /* Fresco Logic host doesn't support MSI. */
323dd4c1590SKevin Lo usemsi = 0;
324dd4c1590SKevin Lo break;
32543bc87c4SMarius Strobl case 0x0f358086: /* BayTrail */
32643bc87c4SMarius Strobl case 0x9c318086: /* Panther Point */
32743bc87c4SMarius Strobl case 0x1e318086: /* Panther Point */
32843bc87c4SMarius Strobl case 0x8c318086: /* Lynx Point */
32943bc87c4SMarius Strobl case 0x8cb18086: /* Wildcat Point */
3304be283b9SHans Petter Selasky case 0x9cb18086: /* Broadwell Mobile Integrated */
33143bc87c4SMarius Strobl /*
33243bc87c4SMarius Strobl * On Intel chipsets, reroute ports from EHCI to XHCI
33343bc87c4SMarius Strobl * controller and use a different IMOD value.
33443bc87c4SMarius Strobl */
33543bc87c4SMarius Strobl sc->sc_port_route = &xhci_pci_port_route;
33643bc87c4SMarius Strobl sc->sc_imod_default = XHCI_IMOD_DEFAULT_LP;
337dd7ea6c2SHans Petter Selasky sc->sc_ctlstep = 1;
3380c31a8b0SHans Petter Selasky break;
33933cbbf26SHans Petter Selasky default:
34033cbbf26SHans Petter Selasky break;
3410c31a8b0SHans Petter Selasky }
3420c31a8b0SHans Petter Selasky
3430c31a8b0SHans Petter Selasky if (xhci_init(sc, self, usedma32)) {
344b217d184SHans Petter Selasky device_printf(self, "Could not initialize softc\n");
345b217d184SHans Petter Selasky bus_release_resource(self, SYS_RES_MEMORY, PCI_XHCI_CBMEM,
346b217d184SHans Petter Selasky sc->sc_io_res);
347b217d184SHans Petter Selasky return (ENXIO);
348b217d184SHans Petter Selasky }
349b217d184SHans Petter Selasky
350b217d184SHans Petter Selasky pci_enable_busmaster(self);
351b217d184SHans Petter Selasky
35297d729cfSHans Petter Selasky usb_callout_init_mtx(&sc->sc_callout, &sc->sc_bus.bus_mtx, 0);
35397d729cfSHans Petter Selasky
354dcf83ff0SMarius Strobl rid = 0;
3552245b38fSAndrew Turner if (xhci_use_msix && (msix_table = pci_msix_table_bar(self)) >= 0) {
356116bc582SKonstantin Belousov if (msix_table == PCI_XHCI_CBMEM) {
357116bc582SKonstantin Belousov sc->sc_msix_res = sc->sc_io_res;
358116bc582SKonstantin Belousov } else {
359116bc582SKonstantin Belousov sc->sc_msix_res = bus_alloc_resource_any(self,
360116bc582SKonstantin Belousov SYS_RES_MEMORY, &msix_table, RF_ACTIVE);
3612245b38fSAndrew Turner if (sc->sc_msix_res == NULL) {
3622245b38fSAndrew Turner /* May not be enabled */
3632245b38fSAndrew Turner device_printf(self,
3642245b38fSAndrew Turner "Unable to map MSI-X table\n");
365116bc582SKonstantin Belousov }
366116bc582SKonstantin Belousov }
367116bc582SKonstantin Belousov if (sc->sc_msix_res != NULL) {
3682245b38fSAndrew Turner count = 1;
3692245b38fSAndrew Turner if (pci_alloc_msix(self, &count) == 0) {
3702245b38fSAndrew Turner if (bootverbose)
3712245b38fSAndrew Turner device_printf(self, "MSI-X enabled\n");
3722245b38fSAndrew Turner rid = 1;
3732245b38fSAndrew Turner } else {
374116bc582SKonstantin Belousov if (sc->sc_msix_res != sc->sc_io_res) {
375116bc582SKonstantin Belousov bus_release_resource(self,
376116bc582SKonstantin Belousov SYS_RES_MEMORY,
3772245b38fSAndrew Turner msix_table, sc->sc_msix_res);
378116bc582SKonstantin Belousov }
3792245b38fSAndrew Turner sc->sc_msix_res = NULL;
3802245b38fSAndrew Turner }
3812245b38fSAndrew Turner }
3822245b38fSAndrew Turner }
3832245b38fSAndrew Turner if (rid == 0 && xhci_use_msi && usemsi) {
38489d02670SKonstantin Belousov count = 1;
38589d02670SKonstantin Belousov if (pci_alloc_msi(self, &count) == 0) {
38689d02670SKonstantin Belousov if (bootverbose)
38789d02670SKonstantin Belousov device_printf(self, "MSI enabled\n");
388dcf83ff0SMarius Strobl rid = 1;
38989d02670SKonstantin Belousov }
39089d02670SKonstantin Belousov }
391dcf83ff0SMarius Strobl sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
392dcf83ff0SMarius Strobl RF_ACTIVE | (rid != 0 ? 0 : RF_SHAREABLE));
39313540260SHans Petter Selasky if (sc->sc_irq_res == NULL) {
394dcf83ff0SMarius Strobl pci_release_msi(self);
39513540260SHans Petter Selasky device_printf(self, "Could not allocate IRQ\n");
3964c5d1323SHans Petter Selasky /* goto error; FALLTHROUGH - use polling */
39713540260SHans Petter Selasky }
3985b56413dSWarner Losh sc->sc_bus.bdev = device_add_child(self, "usbus", DEVICE_UNIT_ANY);
39913540260SHans Petter Selasky if (sc->sc_bus.bdev == NULL) {
40013540260SHans Petter Selasky device_printf(self, "Could not add USB device\n");
40113540260SHans Petter Selasky goto error;
40213540260SHans Petter Selasky }
40313540260SHans Petter Selasky device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
40413540260SHans Petter Selasky
40542cf33ddSHans Petter Selasky switch (pci_get_vendor(self)) {
40642cf33ddSHans Petter Selasky case PCI_XHCI_VENDORID_AMD:
40742cf33ddSHans Petter Selasky strlcpy(sc->sc_vendor, "AMD", sizeof(sc->sc_vendor));
40842cf33ddSHans Petter Selasky break;
40942cf33ddSHans Petter Selasky case PCI_XHCI_VENDORID_INTEL:
41042cf33ddSHans Petter Selasky strlcpy(sc->sc_vendor, "Intel", sizeof(sc->sc_vendor));
41142cf33ddSHans Petter Selasky break;
4122a31a06bSAlexander Motin case PCI_XHCI_VENDORID_VMWARE:
4132a31a06bSAlexander Motin strlcpy(sc->sc_vendor, "VMware", sizeof(sc->sc_vendor));
4142a31a06bSAlexander Motin break;
4150d7064d5SDmitry Luhtionov case PCI_XHCI_VENDORID_ZHAOXIN:
4160d7064d5SDmitry Luhtionov strlcpy(sc->sc_vendor, "Zhaoxin", sizeof(sc->sc_vendor));
4170d7064d5SDmitry Luhtionov break;
41842cf33ddSHans Petter Selasky default:
41942cf33ddSHans Petter Selasky if (bootverbose)
42042cf33ddSHans Petter Selasky device_printf(self, "(New XHCI DeviceId=0x%08x)\n",
42142cf33ddSHans Petter Selasky pci_get_devid(self));
42242cf33ddSHans Petter Selasky snprintf(sc->sc_vendor, sizeof(sc->sc_vendor),
42342cf33ddSHans Petter Selasky "(0x%04x)", pci_get_vendor(self));
42442cf33ddSHans Petter Selasky break;
42542cf33ddSHans Petter Selasky }
42613540260SHans Petter Selasky
4273346ae0dSHans Petter Selasky if (sc->sc_irq_res != NULL && xhci_use_polling() == 0) {
42813540260SHans Petter Selasky err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
42913540260SHans Petter Selasky NULL, (driver_intr_t *)xhci_interrupt, sc, &sc->sc_intr_hdl);
43097d729cfSHans Petter Selasky if (err != 0) {
431dcf83ff0SMarius Strobl bus_release_resource(self, SYS_RES_IRQ,
432dcf83ff0SMarius Strobl rman_get_rid(sc->sc_irq_res), sc->sc_irq_res);
433dcf83ff0SMarius Strobl sc->sc_irq_res = NULL;
434dcf83ff0SMarius Strobl pci_release_msi(self);
43513540260SHans Petter Selasky device_printf(self, "Could not setup IRQ, err=%d\n", err);
43613540260SHans Petter Selasky sc->sc_intr_hdl = NULL;
43713540260SHans Petter Selasky }
43897d729cfSHans Petter Selasky }
439dcf83ff0SMarius Strobl if (sc->sc_irq_res == NULL || sc->sc_intr_hdl == NULL) {
440dcf83ff0SMarius Strobl if (xhci_use_polling() != 0) {
44197d729cfSHans Petter Selasky device_printf(self, "Interrupt polling at %dHz\n", hz);
44297d729cfSHans Petter Selasky USB_BUS_LOCK(&sc->sc_bus);
44397d729cfSHans Petter Selasky xhci_interrupt_poll(sc);
44497d729cfSHans Petter Selasky USB_BUS_UNLOCK(&sc->sc_bus);
445dcf83ff0SMarius Strobl } else
446dcf83ff0SMarius Strobl goto error;
44797d729cfSHans Petter Selasky }
44897d729cfSHans Petter Selasky
4492e141748SHans Petter Selasky xhci_pci_take_controller(self);
45013540260SHans Petter Selasky
45113540260SHans Petter Selasky err = xhci_halt_controller(sc);
45213540260SHans Petter Selasky
45313540260SHans Petter Selasky if (err == 0)
45413540260SHans Petter Selasky err = xhci_start_controller(sc);
45513540260SHans Petter Selasky
45613540260SHans Petter Selasky if (err == 0)
45713540260SHans Petter Selasky err = device_probe_and_attach(sc->sc_bus.bdev);
45813540260SHans Petter Selasky
45913540260SHans Petter Selasky if (err) {
46013540260SHans Petter Selasky device_printf(self, "XHCI halt/start/probe failed err=%d\n", err);
46113540260SHans Petter Selasky goto error;
46213540260SHans Petter Selasky }
46313540260SHans Petter Selasky return (0);
46413540260SHans Petter Selasky
46513540260SHans Petter Selasky error:
46613540260SHans Petter Selasky xhci_pci_detach(self);
46713540260SHans Petter Selasky return (ENXIO);
46813540260SHans Petter Selasky }
46913540260SHans Petter Selasky
47013540260SHans Petter Selasky static int
xhci_pci_detach(device_t self)47113540260SHans Petter Selasky xhci_pci_detach(device_t self)
47213540260SHans Petter Selasky {
47313540260SHans Petter Selasky struct xhci_softc *sc = device_get_softc(self);
4743ddaf820SJohn Baldwin int error;
47513540260SHans Petter Selasky
47613540260SHans Petter Selasky /* during module unload there are lots of children leftover */
4773ddaf820SJohn Baldwin error = bus_generic_detach(self);
4783ddaf820SJohn Baldwin if (error != 0)
4793ddaf820SJohn Baldwin return (error);
48013540260SHans Petter Selasky
48197d729cfSHans Petter Selasky usb_callout_drain(&sc->sc_callout);
48297d729cfSHans Petter Selasky xhci_halt_controller(sc);
483f515174bSHans Petter Selasky xhci_reset_controller(sc);
48497d729cfSHans Petter Selasky
48513540260SHans Petter Selasky pci_disable_busmaster(self);
48613540260SHans Petter Selasky
48713540260SHans Petter Selasky if (sc->sc_irq_res && sc->sc_intr_hdl) {
48813540260SHans Petter Selasky bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
48913540260SHans Petter Selasky sc->sc_intr_hdl = NULL;
49013540260SHans Petter Selasky }
49113540260SHans Petter Selasky if (sc->sc_irq_res) {
492dcf83ff0SMarius Strobl bus_release_resource(self, SYS_RES_IRQ,
493dcf83ff0SMarius Strobl rman_get_rid(sc->sc_irq_res), sc->sc_irq_res);
49413540260SHans Petter Selasky sc->sc_irq_res = NULL;
495dcf83ff0SMarius Strobl pci_release_msi(self);
49613540260SHans Petter Selasky }
497116bc582SKonstantin Belousov if (sc->sc_msix_res != NULL && sc->sc_msix_res != sc->sc_io_res) {
498116bc582SKonstantin Belousov bus_release_resource(self, SYS_RES_MEMORY,
499116bc582SKonstantin Belousov rman_get_rid(sc->sc_msix_res), sc->sc_msix_res);
500116bc582SKonstantin Belousov sc->sc_msix_res = NULL;
501116bc582SKonstantin Belousov }
50213540260SHans Petter Selasky if (sc->sc_io_res) {
50313540260SHans Petter Selasky bus_release_resource(self, SYS_RES_MEMORY, PCI_XHCI_CBMEM,
50413540260SHans Petter Selasky sc->sc_io_res);
50513540260SHans Petter Selasky sc->sc_io_res = NULL;
50613540260SHans Petter Selasky }
50713540260SHans Petter Selasky
50813540260SHans Petter Selasky xhci_uninit(sc);
50913540260SHans Petter Selasky
51013540260SHans Petter Selasky return (0);
51113540260SHans Petter Selasky }
51213540260SHans Petter Selasky
5132e141748SHans Petter Selasky static int
xhci_pci_take_controller(device_t self)5142e141748SHans Petter Selasky xhci_pci_take_controller(device_t self)
51513540260SHans Petter Selasky {
51613540260SHans Petter Selasky struct xhci_softc *sc = device_get_softc(self);
51713540260SHans Petter Selasky uint32_t cparams;
51813540260SHans Petter Selasky uint32_t eecp;
51913540260SHans Petter Selasky uint32_t eec;
52013540260SHans Petter Selasky uint16_t to;
52113540260SHans Petter Selasky uint8_t bios_sem;
52213540260SHans Petter Selasky
52313540260SHans Petter Selasky cparams = XREAD4(sc, capa, XHCI_HCSPARAMS0);
52413540260SHans Petter Selasky
52513540260SHans Petter Selasky eec = -1;
52613540260SHans Petter Selasky
52713540260SHans Petter Selasky /* Synchronise with the BIOS if it owns the controller. */
52813540260SHans Petter Selasky for (eecp = XHCI_HCS0_XECP(cparams) << 2; eecp != 0 && XHCI_XECP_NEXT(eec);
52913540260SHans Petter Selasky eecp += XHCI_XECP_NEXT(eec) << 2) {
53013540260SHans Petter Selasky eec = XREAD4(sc, capa, eecp);
53113540260SHans Petter Selasky
53213540260SHans Petter Selasky if (XHCI_XECP_ID(eec) != XHCI_ID_USB_LEGACY)
53313540260SHans Petter Selasky continue;
53413540260SHans Petter Selasky bios_sem = XREAD1(sc, capa, eecp +
53513540260SHans Petter Selasky XHCI_XECP_BIOS_SEM);
53613540260SHans Petter Selasky if (bios_sem == 0)
53713540260SHans Petter Selasky continue;
53813540260SHans Petter Selasky device_printf(sc->sc_bus.bdev, "waiting for BIOS "
53913540260SHans Petter Selasky "to give up control\n");
54013540260SHans Petter Selasky XWRITE1(sc, capa, eecp +
54113540260SHans Petter Selasky XHCI_XECP_OS_SEM, 1);
54213540260SHans Petter Selasky to = 500;
54313540260SHans Petter Selasky while (1) {
54413540260SHans Petter Selasky bios_sem = XREAD1(sc, capa, eecp +
54513540260SHans Petter Selasky XHCI_XECP_BIOS_SEM);
54613540260SHans Petter Selasky if (bios_sem == 0)
54713540260SHans Petter Selasky break;
54813540260SHans Petter Selasky
54913540260SHans Petter Selasky if (--to == 0) {
55013540260SHans Petter Selasky device_printf(sc->sc_bus.bdev,
55113540260SHans Petter Selasky "timed out waiting for BIOS\n");
55213540260SHans Petter Selasky break;
55313540260SHans Petter Selasky }
55413540260SHans Petter Selasky usb_pause_mtx(NULL, hz / 100); /* wait 10ms */
55513540260SHans Petter Selasky }
55613540260SHans Petter Selasky }
5572e141748SHans Petter Selasky return (0);
55813540260SHans Petter Selasky }
559