1/* $NetBSD: ehci_acpi.c,v 1.9 2021/12/24 00:27:22 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: ehci_acpi.c,v 1.9 2021/12/24 00:27:22 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/ehcireg.h>
45#include <dev/usb/ehcivar.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
52static const struct device_compatible_entry compat_data[] = {
53	/* EHCI-compliant USB controller without standard debug */
54	{ .compat = "PNP0D20" },
55
56	/* EHCI-compliant USB controller with standard debug */
57	{ .compat = "PNP0D25" },
58
59	DEVICE_COMPAT_EOL
60};
61
62struct ehci_acpi_softc {
63	struct ehci_softc	sc_ehci;
64	ACPI_HANDLE		sc_handle;
65};
66
67static int	ehci_acpi_match(device_t, cfdata_t, void *);
68static void	ehci_acpi_attach(device_t, device_t, void *);
69
70static void	ehci_acpi_init(struct ehci_softc *);
71
72static int	ehci_acpi_num_companions(struct acpi_attach_args *);
73
74CFATTACH_DECL2_NEW(ehci_acpi, sizeof(struct ehci_acpi_softc),
75	ehci_acpi_match, ehci_acpi_attach, NULL,
76	ehci_activate, NULL, ehci_childdet);
77
78static int
79ehci_acpi_match(device_t parent, cfdata_t cf, void *aux)
80{
81	struct acpi_attach_args *aa = aux;
82
83	return acpi_compatible_match(aa, compat_data);
84}
85
86static void
87ehci_acpi_attach(device_t parent, device_t self, void *aux)
88{
89	struct ehci_acpi_softc * const asc = device_private(self);
90	struct ehci_softc * const sc = &asc->sc_ehci;
91	struct acpi_attach_args *aa = aux;
92	struct acpi_resources res;
93	struct acpi_mem *mem;
94	struct acpi_irq *irq;
95	ACPI_STATUS rv;
96	int error;
97	void *ih;
98
99	acpi_claim_childdevs(self, aa->aa_node);
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_2_0;
106	sc->sc_vendor_init = ehci_acpi_init;
107
108	rv = acpi_resource_parse(sc->sc_dev, asc->sc_handle, "_CRS",
109	    &res, &acpi_resource_parse_ops_default);
110	if (ACPI_FAILURE(rv))
111		return;
112
113	sc->sc_ncomp = ehci_acpi_num_companions(aa);
114	if (sc->sc_ncomp == 0) {
115		sc->sc_flags = EHCIF_ETTF;
116	}
117	mem = acpi_res_mem(&res, 0);
118	if (mem == NULL) {
119		aprint_error_dev(self, "couldn't find mem resource\n");
120		goto done;
121	}
122
123	irq = acpi_res_irq(&res, 0);
124	if (irq == NULL) {
125		aprint_error_dev(self, "couldn't find irq resource\n");
126		goto done;
127	}
128
129	sc->sc_size = mem->ar_length;
130	sc->iot = aa->aa_memt;
131	error = bus_space_map(sc->iot, mem->ar_base, mem->ar_length, 0, &sc->ioh);
132	if (error) {
133		aprint_error_dev(self, "couldn't map registers\n");
134		goto done;
135	}
136
137	/* Disable interrupts */
138	sc->sc_offs = EREAD1(sc, EHCI_CAPLENGTH);
139	EOWRITE4(sc, EHCI_USBINTR, 0);
140
141	const uint32_t hccparams = EREAD4(sc, EHCI_HCCPARAMS);
142	if (EHCI_HCC_64BIT(hccparams)) {
143		aprint_verbose_dev(self, "64-bit DMA");
144		if (BUS_DMA_TAG_VALID(aa->aa_dmat64)) {
145			aprint_verbose("\n");
146			sc->sc_bus.ub_dmatag = aa->aa_dmat64;
147		} else {
148			aprint_verbose(" - limited\n");
149			sc->sc_bus.ub_dmatag = aa->aa_dmat;
150		}
151	} else {
152		aprint_verbose_dev(self, "32-bit DMA\n");
153		sc->sc_bus.ub_dmatag = aa->aa_dmat;
154	}
155
156	ih = acpi_intr_establish(self,
157	    (uint64_t)(uintptr_t)aa->aa_node->ad_handle,
158	    IPL_USB, true, ehci_intr, sc, device_xname(self));
159	if (ih == NULL) {
160		aprint_error_dev(self, "couldn't establish interrupt\n");
161		goto done;
162	}
163
164	error = ehci_init(sc);
165	if (error) {
166		aprint_error_dev(self, "init failed, error = %d\n", error);
167		acpi_intr_disestablish(ih);
168		goto done;
169	}
170
171	sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint, CFARGS_NONE);
172
173done:
174	acpi_resource_cleanup(&res);
175}
176
177static void
178ehci_acpi_init(struct ehci_softc *sc)
179{
180	struct ehci_acpi_softc * const asc = (struct ehci_acpi_softc *)sc;
181
182	acpi_usb_post_reset(asc->sc_handle);
183}
184
185static int
186ehci_acpi_port_has_companion(struct acpi_devnode *portad, ACPI_INTEGER portno)
187{
188	struct acpi_devnode *ad;
189	ACPI_BUFFER portbuf, buf;
190	ACPI_OBJECT *portobj, *obj;
191	ACPI_OBJECT *portpld, *pld;
192	ACPI_STATUS rv;
193	int ncomp = 0;
194
195	rv = acpi_eval_struct(portad->ad_handle, "_PLD", &portbuf);
196	if (ACPI_FAILURE(rv)) {
197		return 0;
198	}
199	portobj = portbuf.Pointer;
200	if (portobj->Type != ACPI_TYPE_PACKAGE ||
201	    portobj->Package.Count == 0 ||
202	    portobj->Package.Elements[0].Type != ACPI_TYPE_BUFFER) {
203		return 0;
204	}
205	portpld = &portobj->Package.Elements[0];
206
207	/*
208	 * Look through all ACPI device nodes and try to find another
209	 * one that matches our _PLD. If we have a match, it means we
210	 * have a companion controller somewhere.
211	 */
212	SIMPLEQ_FOREACH(ad, &acpi_softc->sc_head, ad_list) {
213		if (ad == portad) {
214			continue;
215		}
216		rv = acpi_eval_struct(ad->ad_handle, "_PLD", &buf);
217		if (ACPI_FAILURE(rv)) {
218			continue;
219		}
220		obj = buf.Pointer;
221		if (obj->Type == ACPI_TYPE_PACKAGE &&
222		    obj->Package.Count != 0 &&
223		    obj->Package.Elements[0].Type == ACPI_TYPE_BUFFER) {
224			pld = &obj->Package.Elements[0];
225			if (memcmp(pld->Buffer.Pointer, portpld->Buffer.Pointer,
226			    pld->Buffer.Length) == 0) {
227				aprint_verbose_dev(portad->ad_device,
228				    "companion port: %s\n", acpi_name(ad->ad_handle));
229				ncomp = 1;
230			}
231		}
232		ACPI_FREE(buf.Pointer);
233		if (ncomp != 0) {
234			break;
235		}
236	}
237
238	ACPI_FREE(portbuf.Pointer);
239
240	return ncomp;
241}
242
243static int
244ehci_acpi_num_companion_ports(struct acpi_devnode *hubad)
245{
246	struct acpi_devnode *ad;
247	ACPI_STATUS rv;
248	ACPI_INTEGER val;
249	int ncomp = 0;
250
251	/* Look for child ports with _ADR != 0 */
252	SIMPLEQ_FOREACH(ad, &hubad->ad_child_head, ad_child_list) {
253		rv = acpi_eval_integer(ad->ad_handle, "_ADR", &val);
254		if (ACPI_SUCCESS(rv) && val != 0) {
255			ncomp += ehci_acpi_port_has_companion(ad, val);
256		}
257	}
258
259	return ncomp;
260}
261
262static int
263ehci_acpi_num_companions(struct acpi_attach_args *aa)
264{
265	struct acpi_devnode *ad;
266	ACPI_STATUS rv;
267	ACPI_INTEGER val;
268	int ncomp = 0;
269
270	/* Look for a child node with _ADR 0 that represents our root hub. */
271	SIMPLEQ_FOREACH(ad, &aa->aa_node->ad_child_head, ad_child_list) {
272		rv = acpi_eval_integer(ad->ad_handle, "_ADR", &val);
273		if (ACPI_SUCCESS(rv) && val == 0) {
274			/*
275			 * Count the number of ports on this hub.
276			 */
277			ncomp = ehci_acpi_num_companion_ports(ad);
278			break;
279		}
280	}
281
282	return ncomp;
283}
284