xhci_acpi.c revision 1.14
1/* $NetBSD: xhci_acpi.c,v 1.14 2024/12/09 22:15:33 jmcneill 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.14 2024/12/09 22:15:33 jmcneill 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#define XHCI_ACPI_FLAG_URS	__BIT(0)
53
54static const struct device_compatible_entry compat_data[] = {
55	/* XHCI-compliant USB controller without standard debug */
56	{ .compat = "PNP0D10" },
57
58	/* XHCI-compliant USB controller with standard debug */
59	{ .compat = "PNP0D15" },
60
61	/* DesignWare Dual Role SuperSpeed USB controller */
62	{ .compat = "808622B7" },
63
64	/* XHCI-compliant URS USB controller */
65	{ .compat = "PNP0CA1", .value = XHCI_ACPI_FLAG_URS },
66
67	DEVICE_COMPAT_EOL
68};
69
70struct xhci_acpi_softc {
71	struct xhci_softc	sc_xhci;
72	ACPI_HANDLE		sc_handle;
73};
74
75static int	xhci_acpi_match(device_t, cfdata_t, void *);
76static void	xhci_acpi_attach(device_t, device_t, void *);
77
78static void	xhci_acpi_init(struct xhci_softc *);
79
80CFATTACH_DECL2_NEW(xhci_acpi, sizeof(struct xhci_acpi_softc),
81	xhci_acpi_match, xhci_acpi_attach, NULL,
82	xhci_activate, NULL, xhci_childdet);
83
84static int
85xhci_acpi_match(device_t parent, cfdata_t cf, void *aux)
86{
87	struct acpi_attach_args *aa = aux;
88
89	return acpi_compatible_match(aa, compat_data);
90}
91
92static void
93xhci_acpi_attach(device_t parent, device_t self, void *aux)
94{
95	struct xhci_acpi_softc * const asc = device_private(self);
96	struct xhci_softc * const sc = &asc->sc_xhci;
97	struct acpi_attach_args *aa = aux;
98	struct acpi_resources res, res_urs;
99	struct acpi_mem *mem;
100	struct acpi_irq *irq;
101	uint32_t hccparams;
102	uintptr_t flags;
103	ACPI_STATUS rv;
104	bool res_urs_valid = false;
105	int error;
106	void *ih;
107
108	flags = acpi_compatible_lookup(aa, compat_data)->value;
109
110	asc->sc_handle = aa->aa_node->ad_handle;
111
112	sc->sc_dev = self;
113	sc->sc_bus.ub_hcpriv = sc;
114	sc->sc_bus.ub_revision = USBREV_3_0;
115	sc->sc_quirks = 0;
116	sc->sc_vendor_init = xhci_acpi_init;
117
118	rv = acpi_resource_parse(sc->sc_dev, asc->sc_handle, "_CRS",
119	    &res, &acpi_resource_parse_ops_default);
120	if (ACPI_FAILURE(rv))
121		return;
122
123	mem = acpi_res_mem(&res, 0);
124	if (mem == NULL) {
125		aprint_error_dev(self, "couldn't find mem resource\n");
126		goto done;
127	}
128
129	if ((flags & XHCI_ACPI_FLAG_URS) == 0) {
130		irq = acpi_res_irq(&res, 0);
131		if (irq == NULL) {
132			aprint_error_dev(self, "couldn't find irq resource\n");
133			goto done;
134		}
135	} else {
136		struct acpi_devnode *ad;
137
138		/* For URS nodes, the interrupt resources is on a child with _ADR=0. */
139		irq = NULL;
140		SIMPLEQ_FOREACH(ad, &aa->aa_node->ad_child_head, ad_child_list) {
141			ACPI_INTEGER adr;
142			if (acpi_eval_integer(ad->ad_handle, "_ADR", &adr) == AE_OK &&
143			    adr == 0) {
144				rv = acpi_resource_parse(sc->sc_dev, ad->ad_handle,
145				    "_CRS", &res_urs, &acpi_resource_parse_ops_quiet);
146				if (ACPI_FAILURE(rv)) {
147					aprint_error_dev(self,
148					    "couldn't parse URS resources: %s\n",
149					    AcpiFormatException(rv));
150					goto done;
151				}
152				res_urs_valid = true;
153				irq = acpi_res_irq(&res_urs, 0);
154				break;
155			}
156		}
157		if (irq == NULL) {
158			aprint_error_dev(self, "couldn't find irq resource\n");
159			goto done;
160		}
161	}
162
163	sc->sc_ios = mem->ar_length;
164	sc->sc_iot = aa->aa_memt;
165	error = bus_space_map(sc->sc_iot, mem->ar_base, mem->ar_length, 0, &sc->sc_ioh);
166	if (error) {
167		aprint_error_dev(self, "couldn't map registers\n");
168		return;
169	}
170
171	hccparams = bus_space_read_4(sc->sc_iot, sc->sc_ioh, XHCI_HCCPARAMS);
172	if (XHCI_HCC_AC64(hccparams)) {
173		aprint_verbose_dev(self, "64-bit DMA");
174		if (BUS_DMA_TAG_VALID(aa->aa_dmat64)) {
175			aprint_verbose("\n");
176			sc->sc_bus.ub_dmatag = aa->aa_dmat64;
177		} else {
178			aprint_verbose(" - limited\n");
179			sc->sc_bus.ub_dmatag = aa->aa_dmat;
180		}
181	} else {
182		aprint_verbose_dev(self, "32-bit DMA\n");
183		sc->sc_bus.ub_dmatag = aa->aa_dmat;
184	}
185
186	ih = acpi_intr_establish_irq(self, irq,
187	    IPL_USB, true, xhci_intr, sc, device_xname(self));
188	if (ih == NULL) {
189		aprint_error_dev(self, "couldn't establish interrupt\n");
190		return;
191	}
192
193	error = xhci_init(sc);
194	if (error) {
195		aprint_error_dev(self, "init failed, error = %d\n", error);
196		return;
197	}
198
199	sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint, CFARGS_NONE);
200	sc->sc_child2 = config_found(self, &sc->sc_bus2, usbctlprint,
201	    CFARGS_NONE);
202
203done:
204	acpi_resource_cleanup(&res);
205	if (res_urs_valid) {
206		acpi_resource_cleanup(&res_urs);
207	}
208}
209
210static void
211xhci_acpi_init(struct xhci_softc *sc)
212{
213	struct xhci_acpi_softc * const asc = (struct xhci_acpi_softc *)sc;
214
215	acpi_usb_post_reset(asc->sc_handle);
216}
217