xref: /openbsd/sys/dev/acpi/xhci_acpi.c (revision 3a3d566b)
1 /*	$OpenBSD: xhci_acpi.c,v 1.13 2024/10/09 00:38:26 jsg Exp $	*/
2 /*
3  * Copyright (c) 2018 Mark Kettenis
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <sys/param.h>
19 #include <sys/systm.h>
20 #include <sys/device.h>
21 
22 #include <machine/bus.h>
23 #include <machine/intr.h>
24 
25 #include <dev/acpi/acpireg.h>
26 #include <dev/acpi/acpivar.h>
27 #include <dev/acpi/acpidev.h>
28 #include <dev/acpi/amltypes.h>
29 #include <dev/acpi/dsdt.h>
30 
31 #include <dev/usb/usb.h>
32 #include <dev/usb/usbdi.h>
33 #include <dev/usb/usbdivar.h>
34 #include <dev/usb/usb_mem.h>
35 
36 #include <dev/usb/xhcireg.h>
37 #include <dev/usb/xhcivar.h>
38 
39 struct xhci_acpi_softc {
40 	struct xhci_softc sc;
41 	struct acpi_softc *sc_acpi;
42 	struct aml_node *sc_node;
43 	void		*sc_ih;
44 };
45 
46 int	xhci_acpi_match(struct device *, void *, void *);
47 void	xhci_acpi_attach(struct device *, struct device *, void *);
48 
49 const struct cfattach xhci_acpi_ca = {
50 	sizeof(struct xhci_acpi_softc), xhci_acpi_match, xhci_acpi_attach,
51 	NULL, xhci_activate
52 };
53 
54 const char *xhci_hids[] = {
55 	"PNP0D10",
56 	"PNP0D15",
57 	"QCOM0304",		/* SDM845 URS */
58 	"QCOM0305",
59 	"QCOM0497",		/* SC8180 URS */
60 	"QCOM0498",
61 	"QCOM068B",		/* SC8280 URS */
62 	"QCOM068C",
63 	"QCOM0826",		/* SC7180 USB */
64 	"QCOM24B6",		/* SDM850 URS */
65 	"QCOM24B7",
66 	"QCOM0C8B",		/* X1E80100 URS */
67 	"QCOM0C8C",
68 	"QCOM0D07",
69 	NULL
70 };
71 
72 int
xhci_acpi_match(struct device * parent,void * match,void * aux)73 xhci_acpi_match(struct device *parent, void *match, void *aux)
74 {
75 	struct acpi_attach_args *aaa = aux;
76 	struct cfdata *cf = match;
77 
78 	if (aaa->aaa_naddr < 1)
79 		return 0;
80 	return acpi_matchhids(aaa, xhci_hids, cf->cf_driver->cd_name);
81 }
82 
83 void
xhci_acpi_attach(struct device * parent,struct device * self,void * aux)84 xhci_acpi_attach(struct device *parent, struct device *self, void *aux)
85 {
86 	struct xhci_acpi_softc *sc = (struct xhci_acpi_softc *)self;
87 	struct acpi_attach_args *aaa = aux;
88 	struct aml_node *node;
89 	int error;
90 
91 	sc->sc_acpi = (struct acpi_softc *)parent;
92 	sc->sc_node = aaa->aaa_node;
93 	printf(" %s", sc->sc_node->name);
94 
95 	/* XXX: Attaching on that specific controller resets the X13s */
96 	extern char *hw_ver;
97 	if (hw_ver && strcmp(hw_ver, "ThinkPad X13s Gen 1") == 0 &&
98 	    strncmp(sc->sc_node->name, "USB2", 4) == 0) {
99 		printf(": disabled\n");
100 		return;
101 	}
102 
103 	/*
104 	 * The Qualcomm dual role controller has the interrupt on a
105 	 * child node.  Find it and parse its resources to find the
106 	 * interrupt.
107 	 */
108 	if (strcmp(aaa->aaa_dev, "QCOM0304") == 0 ||
109 	    strcmp(aaa->aaa_dev, "QCOM0305") == 0 ||
110 	    strcmp(aaa->aaa_dev, "QCOM0497") == 0 ||
111 	    strcmp(aaa->aaa_dev, "QCOM0498") == 0 ||
112 	    strcmp(aaa->aaa_dev, "QCOM068B") == 0 ||
113 	    strcmp(aaa->aaa_dev, "QCOM068C") == 0 ||
114 	    strcmp(aaa->aaa_dev, "QCOM24B6") == 0 ||
115 	    strcmp(aaa->aaa_dev, "QCOM24B7") == 0 ||
116 	    strcmp(aaa->aaa_dev, "QCOM0C8B") == 0 ||
117 	    strcmp(aaa->aaa_dev, "QCOM0C8C") == 0 ||
118 	    strcmp(aaa->aaa_dev, "QCOM0D07") == 0) {
119 		SIMPLEQ_FOREACH(node, &sc->sc_node->son, sib) {
120 			if (strncmp(node->name, "USB", 3) == 0) {
121 				aaa->aaa_node = node;
122 				acpi_parse_crs(sc->sc_acpi, aaa);
123 				break;
124 			}
125 		}
126 	}
127 
128 	if (aaa->aaa_nirq < 1) {
129 		printf(": no interrupt\n");
130 		return;
131 	}
132 
133 	printf(" addr 0x%llx/0x%llx", aaa->aaa_addr[0], aaa->aaa_size[0]);
134 	printf(" irq %d", aaa->aaa_irq[0]);
135 
136 	sc->sc.iot = aaa->aaa_bst[0];
137 	sc->sc.sc_size = aaa->aaa_size[0];
138 	sc->sc.sc_bus.dmatag = aaa->aaa_dmat;
139 
140 	if (bus_space_map(sc->sc.iot, aaa->aaa_addr[0], aaa->aaa_size[0],
141 	    0, &sc->sc.ioh)) {
142 		printf(": can't map registers\n");
143 		return;
144 	}
145 
146 	sc->sc_ih = acpi_intr_establish(aaa->aaa_irq[0], aaa->aaa_irq_flags[0],
147 	    IPL_USB, xhci_intr, sc, sc->sc.sc_bus.bdev.dv_xname);
148 	if (sc->sc_ih == NULL) {
149 		printf(": can't establish interrupt\n");
150 		goto unmap;
151 	}
152 
153 	strlcpy(sc->sc.sc_vendor, "Generic", sizeof(sc->sc.sc_vendor));
154 	if ((error = xhci_init(&sc->sc)) != 0) {
155 		printf("%s: init failed, error=%d\n",
156 		    sc->sc.sc_bus.bdev.dv_xname, error);
157 		goto disestablish_ret;
158 	}
159 
160 	/* Attach usb device. */
161 	config_found(self, &sc->sc.sc_bus, usbctlprint);
162 
163 	/* Now that the stack is ready, config' the HC and enable interrupts. */
164 	xhci_config(&sc->sc);
165 
166 	return;
167 
168 disestablish_ret:
169 	acpi_intr_disestablish(sc->sc_ih);
170 unmap:
171 	bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
172 	return;
173 }
174