11.15Sjmcneill/* $NetBSD: xhci_acpi.c,v 1.15 2025/01/30 00:42:47 jmcneill Exp $ */
21.1Sjmcneill
31.1Sjmcneill/*-
41.1Sjmcneill * Copyright (c) 2018 The NetBSD Foundation, Inc.
51.1Sjmcneill * All rights reserved.
61.1Sjmcneill *
71.1Sjmcneill * This code is derived from software contributed to The NetBSD Foundation
81.1Sjmcneill * by Jared McNeill <jmcneill@invisible.ca>.
91.1Sjmcneill *
101.1Sjmcneill * Redistribution and use in source and binary forms, with or without
111.1Sjmcneill * modification, are permitted provided that the following conditions
121.1Sjmcneill * are met:
131.1Sjmcneill * 1. Redistributions of source code must retain the above copyright
141.1Sjmcneill *    notice, this list of conditions and the following disclaimer.
151.1Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
161.1Sjmcneill *    notice, this list of conditions and the following disclaimer in the
171.1Sjmcneill *    documentation and/or other materials provided with the distribution.
181.1Sjmcneill *
191.1Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201.1Sjmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211.1Sjmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221.1Sjmcneill * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231.1Sjmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241.1Sjmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251.1Sjmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261.1Sjmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271.1Sjmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281.1Sjmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291.1Sjmcneill * POSSIBILITY OF SUCH DAMAGE.
301.1Sjmcneill */
311.1Sjmcneill
321.1Sjmcneill#include <sys/cdefs.h>
331.15Sjmcneill__KERNEL_RCSID(0, "$NetBSD: xhci_acpi.c,v 1.15 2025/01/30 00:42:47 jmcneill Exp $");
341.1Sjmcneill
351.1Sjmcneill#include <sys/param.h>
361.1Sjmcneill#include <sys/bus.h>
371.1Sjmcneill#include <sys/cpu.h>
381.1Sjmcneill#include <sys/device.h>
391.1Sjmcneill
401.1Sjmcneill#include <dev/usb/usb.h>
411.1Sjmcneill#include <dev/usb/usbdi.h>
421.1Sjmcneill#include <dev/usb/usbdivar.h>
431.1Sjmcneill#include <dev/usb/usb_mem.h>
441.1Sjmcneill#include <dev/usb/xhcireg.h>
451.1Sjmcneill#include <dev/usb/xhcivar.h>
461.1Sjmcneill
471.1Sjmcneill#include <dev/acpi/acpireg.h>
481.1Sjmcneill#include <dev/acpi/acpivar.h>
491.2Sjmcneill#include <dev/acpi/acpi_intr.h>
501.1Sjmcneill#include <dev/acpi/acpi_usb.h>
511.1Sjmcneill
521.14Sjmcneill#define XHCI_ACPI_FLAG_URS	__BIT(0)
531.14Sjmcneill
541.11Sthorpejstatic const struct device_compatible_entry compat_data[] = {
551.11Sthorpej	/* XHCI-compliant USB controller without standard debug */
561.11Sthorpej	{ .compat = "PNP0D10" },
571.11Sthorpej
581.11Sthorpej	/* XHCI-compliant USB controller with standard debug */
591.11Sthorpej	{ .compat = "PNP0D15" },
601.11Sthorpej
611.11Sthorpej	/* DesignWare Dual Role SuperSpeed USB controller */
621.14Sjmcneill	{ .compat = "808622B7" },
631.14Sjmcneill
641.14Sjmcneill	/* XHCI-compliant URS USB controller */
651.14Sjmcneill	{ .compat = "PNP0CA1", .value = XHCI_ACPI_FLAG_URS },
661.11Sthorpej
671.11Sthorpej	DEVICE_COMPAT_EOL
681.1Sjmcneill};
691.1Sjmcneill
701.1Sjmcneillstruct xhci_acpi_softc {
711.1Sjmcneill	struct xhci_softc	sc_xhci;
721.1Sjmcneill	ACPI_HANDLE		sc_handle;
731.1Sjmcneill};
741.1Sjmcneill
751.1Sjmcneillstatic int	xhci_acpi_match(device_t, cfdata_t, void *);
761.1Sjmcneillstatic void	xhci_acpi_attach(device_t, device_t, void *);
771.1Sjmcneill
781.1Sjmcneillstatic void	xhci_acpi_init(struct xhci_softc *);
791.1Sjmcneill
801.1SjmcneillCFATTACH_DECL2_NEW(xhci_acpi, sizeof(struct xhci_acpi_softc),
811.1Sjmcneill	xhci_acpi_match, xhci_acpi_attach, NULL,
821.1Sjmcneill	xhci_activate, NULL, xhci_childdet);
831.1Sjmcneill
841.1Sjmcneillstatic int
851.1Sjmcneillxhci_acpi_match(device_t parent, cfdata_t cf, void *aux)
861.1Sjmcneill{
871.1Sjmcneill	struct acpi_attach_args *aa = aux;
881.1Sjmcneill
891.11Sthorpej	return acpi_compatible_match(aa, compat_data);
901.1Sjmcneill}
911.1Sjmcneill
921.1Sjmcneillstatic void
931.1Sjmcneillxhci_acpi_attach(device_t parent, device_t self, void *aux)
941.1Sjmcneill{
951.1Sjmcneill	struct xhci_acpi_softc * const asc = device_private(self);
961.1Sjmcneill	struct xhci_softc * const sc = &asc->sc_xhci;
971.1Sjmcneill	struct acpi_attach_args *aa = aux;
981.14Sjmcneill	struct acpi_resources res, res_urs;
991.1Sjmcneill	struct acpi_mem *mem;
1001.1Sjmcneill	struct acpi_irq *irq;
1011.4Sjmcneill	uint32_t hccparams;
1021.14Sjmcneill	uintptr_t flags;
1031.1Sjmcneill	ACPI_STATUS rv;
1041.14Sjmcneill	bool res_urs_valid = false;
1051.1Sjmcneill	int error;
1061.1Sjmcneill	void *ih;
1071.1Sjmcneill
1081.14Sjmcneill	flags = acpi_compatible_lookup(aa, compat_data)->value;
1091.14Sjmcneill
1101.1Sjmcneill	asc->sc_handle = aa->aa_node->ad_handle;
1111.1Sjmcneill
1121.1Sjmcneill	sc->sc_dev = self;
1131.1Sjmcneill	sc->sc_bus.ub_hcpriv = sc;
1141.1Sjmcneill	sc->sc_bus.ub_revision = USBREV_3_0;
1151.15Sjmcneill	sc->sc_quirks = XHCI_32BIT_ACCESS;
1161.1Sjmcneill	sc->sc_vendor_init = xhci_acpi_init;
1171.1Sjmcneill
1181.1Sjmcneill	rv = acpi_resource_parse(sc->sc_dev, asc->sc_handle, "_CRS",
1191.1Sjmcneill	    &res, &acpi_resource_parse_ops_default);
1201.1Sjmcneill	if (ACPI_FAILURE(rv))
1211.1Sjmcneill		return;
1221.1Sjmcneill
1231.1Sjmcneill	mem = acpi_res_mem(&res, 0);
1241.1Sjmcneill	if (mem == NULL) {
1251.1Sjmcneill		aprint_error_dev(self, "couldn't find mem resource\n");
1261.1Sjmcneill		goto done;
1271.1Sjmcneill	}
1281.1Sjmcneill
1291.14Sjmcneill	if ((flags & XHCI_ACPI_FLAG_URS) == 0) {
1301.14Sjmcneill		irq = acpi_res_irq(&res, 0);
1311.14Sjmcneill		if (irq == NULL) {
1321.14Sjmcneill			aprint_error_dev(self, "couldn't find irq resource\n");
1331.14Sjmcneill			goto done;
1341.14Sjmcneill		}
1351.14Sjmcneill	} else {
1361.14Sjmcneill		struct acpi_devnode *ad;
1371.14Sjmcneill
1381.14Sjmcneill		/* For URS nodes, the interrupt resources is on a child with _ADR=0. */
1391.14Sjmcneill		irq = NULL;
1401.14Sjmcneill		SIMPLEQ_FOREACH(ad, &aa->aa_node->ad_child_head, ad_child_list) {
1411.14Sjmcneill			ACPI_INTEGER adr;
1421.14Sjmcneill			if (acpi_eval_integer(ad->ad_handle, "_ADR", &adr) == AE_OK &&
1431.14Sjmcneill			    adr == 0) {
1441.14Sjmcneill				rv = acpi_resource_parse(sc->sc_dev, ad->ad_handle,
1451.14Sjmcneill				    "_CRS", &res_urs, &acpi_resource_parse_ops_quiet);
1461.14Sjmcneill				if (ACPI_FAILURE(rv)) {
1471.14Sjmcneill					aprint_error_dev(self,
1481.14Sjmcneill					    "couldn't parse URS resources: %s\n",
1491.14Sjmcneill					    AcpiFormatException(rv));
1501.14Sjmcneill					goto done;
1511.14Sjmcneill				}
1521.14Sjmcneill				res_urs_valid = true;
1531.14Sjmcneill				irq = acpi_res_irq(&res_urs, 0);
1541.14Sjmcneill				break;
1551.14Sjmcneill			}
1561.14Sjmcneill		}
1571.14Sjmcneill		if (irq == NULL) {
1581.14Sjmcneill			aprint_error_dev(self, "couldn't find irq resource\n");
1591.14Sjmcneill			goto done;
1601.14Sjmcneill		}
1611.1Sjmcneill	}
1621.1Sjmcneill
1631.1Sjmcneill	sc->sc_ios = mem->ar_length;
1641.1Sjmcneill	sc->sc_iot = aa->aa_memt;
1651.1Sjmcneill	error = bus_space_map(sc->sc_iot, mem->ar_base, mem->ar_length, 0, &sc->sc_ioh);
1661.1Sjmcneill	if (error) {
1671.1Sjmcneill		aprint_error_dev(self, "couldn't map registers\n");
1681.1Sjmcneill		return;
1691.1Sjmcneill	}
1701.1Sjmcneill
1711.4Sjmcneill	hccparams = bus_space_read_4(sc->sc_iot, sc->sc_ioh, XHCI_HCCPARAMS);
1721.9Sskrll	if (XHCI_HCC_AC64(hccparams)) {
1731.9Sskrll		aprint_verbose_dev(self, "64-bit DMA");
1741.9Sskrll		if (BUS_DMA_TAG_VALID(aa->aa_dmat64)) {
1751.9Sskrll			aprint_verbose("\n");
1761.9Sskrll			sc->sc_bus.ub_dmatag = aa->aa_dmat64;
1771.9Sskrll		} else {
1781.9Sskrll			aprint_verbose(" - limited\n");
1791.9Sskrll			sc->sc_bus.ub_dmatag = aa->aa_dmat;
1801.9Sskrll		}
1811.4Sjmcneill	} else {
1821.9Sskrll		aprint_verbose_dev(self, "32-bit DMA\n");
1831.6Sjmcneill		sc->sc_bus.ub_dmatag = aa->aa_dmat;
1841.4Sjmcneill	}
1851.4Sjmcneill
1861.14Sjmcneill	ih = acpi_intr_establish_irq(self, irq,
1871.2Sjmcneill	    IPL_USB, true, xhci_intr, sc, device_xname(self));
1881.1Sjmcneill	if (ih == NULL) {
1891.1Sjmcneill		aprint_error_dev(self, "couldn't establish interrupt\n");
1901.1Sjmcneill		return;
1911.1Sjmcneill	}
1921.1Sjmcneill
1931.1Sjmcneill	error = xhci_init(sc);
1941.1Sjmcneill	if (error) {
1951.1Sjmcneill		aprint_error_dev(self, "init failed, error = %d\n", error);
1961.1Sjmcneill		return;
1971.1Sjmcneill	}
1981.1Sjmcneill
1991.13Sthorpej	sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint, CFARGS_NONE);
2001.12Sthorpej	sc->sc_child2 = config_found(self, &sc->sc_bus2, usbctlprint,
2011.13Sthorpej	    CFARGS_NONE);
2021.1Sjmcneill
2031.1Sjmcneilldone:
2041.1Sjmcneill	acpi_resource_cleanup(&res);
2051.14Sjmcneill	if (res_urs_valid) {
2061.14Sjmcneill		acpi_resource_cleanup(&res_urs);
2071.14Sjmcneill	}
2081.1Sjmcneill}
2091.1Sjmcneill
2101.1Sjmcneillstatic void
2111.1Sjmcneillxhci_acpi_init(struct xhci_softc *sc)
2121.1Sjmcneill{
2131.1Sjmcneill	struct xhci_acpi_softc * const asc = (struct xhci_acpi_softc *)sc;
2141.1Sjmcneill
2151.1Sjmcneill	acpi_usb_post_reset(asc->sc_handle);
2161.1Sjmcneill}
217