xref: /netbsd-src/sys/dev/acpi/xhci_acpi.c (revision ccd9df534e375a4366c5b55f23782053c7a98d82)
1 /* $NetBSD: xhci_acpi.c,v 1.13 2021/08/07 16:19:09 thorpej Exp $ */
2 
3 /*-
4  * Copyright (c) 2018 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jared McNeill <jmcneill@invisible.ca>.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: xhci_acpi.c,v 1.13 2021/08/07 16:19:09 thorpej Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/bus.h>
37 #include <sys/cpu.h>
38 #include <sys/device.h>
39 
40 #include <dev/usb/usb.h>
41 #include <dev/usb/usbdi.h>
42 #include <dev/usb/usbdivar.h>
43 #include <dev/usb/usb_mem.h>
44 #include <dev/usb/xhcireg.h>
45 #include <dev/usb/xhcivar.h>
46 
47 #include <dev/acpi/acpireg.h>
48 #include <dev/acpi/acpivar.h>
49 #include <dev/acpi/acpi_intr.h>
50 #include <dev/acpi/acpi_usb.h>
51 
52 static const struct device_compatible_entry compat_data[] = {
53 	/* XHCI-compliant USB controller without standard debug */
54 	{ .compat = "PNP0D10" },
55 
56 	/* XHCI-compliant USB controller with standard debug */
57 	{ .compat = "PNP0D15" },
58 
59 	/* DesignWare Dual Role SuperSpeed USB controller */
60 	{ .compat = "808622B7", },
61 
62 	DEVICE_COMPAT_EOL
63 };
64 
65 struct xhci_acpi_softc {
66 	struct xhci_softc	sc_xhci;
67 	ACPI_HANDLE		sc_handle;
68 };
69 
70 static int	xhci_acpi_match(device_t, cfdata_t, void *);
71 static void	xhci_acpi_attach(device_t, device_t, void *);
72 
73 static void	xhci_acpi_init(struct xhci_softc *);
74 
75 CFATTACH_DECL2_NEW(xhci_acpi, sizeof(struct xhci_acpi_softc),
76 	xhci_acpi_match, xhci_acpi_attach, NULL,
77 	xhci_activate, NULL, xhci_childdet);
78 
79 static int
80 xhci_acpi_match(device_t parent, cfdata_t cf, void *aux)
81 {
82 	struct acpi_attach_args *aa = aux;
83 
84 	return acpi_compatible_match(aa, compat_data);
85 }
86 
87 static void
88 xhci_acpi_attach(device_t parent, device_t self, void *aux)
89 {
90 	struct xhci_acpi_softc * const asc = device_private(self);
91 	struct xhci_softc * const sc = &asc->sc_xhci;
92 	struct acpi_attach_args *aa = aux;
93 	struct acpi_resources res;
94 	struct acpi_mem *mem;
95 	struct acpi_irq *irq;
96 	uint32_t hccparams;
97 	ACPI_STATUS rv;
98 	int error;
99 	void *ih;
100 
101 	asc->sc_handle = aa->aa_node->ad_handle;
102 
103 	sc->sc_dev = self;
104 	sc->sc_bus.ub_hcpriv = sc;
105 	sc->sc_bus.ub_revision = USBREV_3_0;
106 	sc->sc_quirks = 0;
107 	sc->sc_vendor_init = xhci_acpi_init;
108 
109 	rv = acpi_resource_parse(sc->sc_dev, asc->sc_handle, "_CRS",
110 	    &res, &acpi_resource_parse_ops_default);
111 	if (ACPI_FAILURE(rv))
112 		return;
113 
114 	mem = acpi_res_mem(&res, 0);
115 	if (mem == NULL) {
116 		aprint_error_dev(self, "couldn't find mem resource\n");
117 		goto done;
118 	}
119 
120 	irq = acpi_res_irq(&res, 0);
121 	if (irq == NULL) {
122 		aprint_error_dev(self, "couldn't find irq resource\n");
123 		goto done;
124 	}
125 
126 	sc->sc_ios = mem->ar_length;
127 	sc->sc_iot = aa->aa_memt;
128 	error = bus_space_map(sc->sc_iot, mem->ar_base, mem->ar_length, 0, &sc->sc_ioh);
129 	if (error) {
130 		aprint_error_dev(self, "couldn't map registers\n");
131 		return;
132 	}
133 
134 	hccparams = bus_space_read_4(sc->sc_iot, sc->sc_ioh, XHCI_HCCPARAMS);
135 	if (XHCI_HCC_AC64(hccparams)) {
136 		aprint_verbose_dev(self, "64-bit DMA");
137 		if (BUS_DMA_TAG_VALID(aa->aa_dmat64)) {
138 			aprint_verbose("\n");
139 			sc->sc_bus.ub_dmatag = aa->aa_dmat64;
140 		} else {
141 			aprint_verbose(" - limited\n");
142 			sc->sc_bus.ub_dmatag = aa->aa_dmat;
143 		}
144 	} else {
145 		aprint_verbose_dev(self, "32-bit DMA\n");
146 		sc->sc_bus.ub_dmatag = aa->aa_dmat;
147 	}
148 
149 	ih = acpi_intr_establish(self,
150 	    (uint64_t)(uintptr_t)aa->aa_node->ad_handle,
151 	    IPL_USB, true, xhci_intr, sc, device_xname(self));
152 	if (ih == NULL) {
153 		aprint_error_dev(self, "couldn't establish interrupt\n");
154 		return;
155 	}
156 
157 	error = xhci_init(sc);
158 	if (error) {
159 		aprint_error_dev(self, "init failed, error = %d\n", error);
160 		return;
161 	}
162 
163 	sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint, CFARGS_NONE);
164 	sc->sc_child2 = config_found(self, &sc->sc_bus2, usbctlprint,
165 	    CFARGS_NONE);
166 
167 done:
168 	acpi_resource_cleanup(&res);
169 }
170 
171 static void
172 xhci_acpi_init(struct xhci_softc *sc)
173 {
174 	struct xhci_acpi_softc * const asc = (struct xhci_acpi_softc *)sc;
175 
176 	acpi_usb_post_reset(asc->sc_handle);
177 }
178