xref: /netbsd-src/sys/dev/acpi/ohci_acpi.c (revision 86e4d8c787d0278e655e0d3945b669a9dd2c3501)
1 /* $NetBSD: ohci_acpi.c,v 1.2 2021/12/26 14:35:03 jmcneill Exp $ */
2 
3 /*-
4  * Copyright (c) 2021 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: ohci_acpi.c,v 1.2 2021/12/26 14:35:03 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/ohcireg.h>
45 #include <dev/usb/ohcivar.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 #include <dev/pci/pcireg.h>
53 
54 static int	ohci_acpi_match(device_t, cfdata_t, void *);
55 static void	ohci_acpi_attach(device_t, device_t, void *);
56 
57 CFATTACH_DECL2_NEW(ohci_acpi, sizeof(struct ohci_softc),
58 	ohci_acpi_match, ohci_acpi_attach, NULL,
59 	ohci_activate, NULL, ohci_childdet);
60 
61 static const struct device_compatible_entry compat_data[] = {
62 	{ .compat = "generic-ohci" },		/* DT link */
63 	DEVICE_COMPAT_EOL
64 };
65 
66 static int
ohci_acpi_match(device_t parent,cfdata_t cf,void * aux)67 ohci_acpi_match(device_t parent, cfdata_t cf, void *aux)
68 {
69 	struct acpi_attach_args *aa = aux;
70 	int match;
71 
72 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE) {
73 		return 0;
74 	}
75 
76 	match = acpi_compatible_match(aa, compat_data);
77 	if (!match) {
78 		match = acpi_match_class(aa->aa_node->ad_handle,
79 		    PCI_CLASS_SERIALBUS,
80 		    PCI_SUBCLASS_SERIALBUS_USB,
81 		    PCI_INTERFACE_OHCI);
82 	}
83 
84 	return match;
85 }
86 
87 static void
ohci_acpi_attach(device_t parent,device_t self,void * aux)88 ohci_acpi_attach(device_t parent, device_t self, void *aux)
89 {
90 	struct ohci_softc * const sc = device_private(self);
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 	sc->sc_dev = self;
100 	sc->sc_bus.ub_hcpriv = sc;
101 	sc->sc_bus.ub_dmatag = aa->aa_dmat;
102 	sc->sc_bus.ub_revision = USBREV_1_0;
103 
104 	rv = acpi_resource_parse(sc->sc_dev, aa->aa_node->ad_handle, "_CRS",
105 	    &res, &acpi_resource_parse_ops_default);
106 	if (ACPI_FAILURE(rv)) {
107 		return;
108 	}
109 
110 	mem = acpi_res_mem(&res, 0);
111 	if (mem == NULL) {
112 		aprint_error_dev(self, "couldn't find mem resource\n");
113 		goto done;
114 	}
115 
116 	irq = acpi_res_irq(&res, 0);
117 	if (irq == NULL) {
118 		aprint_error_dev(self, "couldn't find irq resource\n");
119 		goto done;
120 	}
121 
122 	sc->sc_size = mem->ar_length;
123 	sc->iot = aa->aa_memt;
124 	error = bus_space_map(sc->iot, mem->ar_base, mem->ar_length, 0, &sc->ioh);
125 	if (error) {
126 		aprint_error_dev(self, "couldn't map registers\n");
127 		goto done;
128 	}
129 
130 	/* Disable interrupts */
131 	bus_space_write_4(sc->iot, sc->ioh, OHCI_INTERRUPT_DISABLE,
132 	    OHCI_ALL_INTRS);
133 
134 	ih = acpi_intr_establish(self,
135 	    (uint64_t)(uintptr_t)aa->aa_node->ad_handle,
136 	    IPL_USB, true, ohci_intr, sc, device_xname(self));
137 	if (ih == NULL) {
138 		aprint_error_dev(self, "couldn't establish interrupt\n");
139 		goto done;
140 	}
141 
142 	error = ohci_init(sc);
143 	if (error) {
144 		aprint_error_dev(self, "init failed, error = %d\n", error);
145 		acpi_intr_disestablish(ih);
146 		goto done;
147 	}
148 
149 	sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint, CFARGS_NONE);
150 
151 done:
152 	acpi_resource_cleanup(&res);
153 }
154