ehci_acpi.c revision 1.2 1 /* $NetBSD: ehci_acpi.c,v 1.2 2018/10/26 23:33:38 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 (at) 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.2 2018/10/26 23:33:38 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_usb.h>
50
51 static const char * const compatible[] = {
52 "PNP0D20", /* EHCI-compliant USB controller without standard debug */
53 "PNP0D25", /* EHCI-compliant USB controller with standard debug */
54 NULL
55 };
56
57 struct ehci_acpi_softc {
58 struct ehci_softc sc_ehci;
59 ACPI_HANDLE sc_handle;
60 };
61
62 static int ehci_acpi_match(device_t, cfdata_t, void *);
63 static void ehci_acpi_attach(device_t, device_t, void *);
64
65 static void ehci_acpi_init(struct ehci_softc *);
66
67 CFATTACH_DECL2_NEW(ehci_acpi, sizeof(struct ehci_acpi_softc),
68 ehci_acpi_match, ehci_acpi_attach, NULL,
69 ehci_activate, NULL, ehci_childdet);
70
71 static int
72 ehci_acpi_match(device_t parent, cfdata_t cf, void *aux)
73 {
74 struct acpi_attach_args *aa = aux;
75
76 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
77 return 0;
78
79 return acpi_match_hid(aa->aa_node->ad_devinfo, compatible);
80 }
81
82 static void
83 ehci_acpi_attach(device_t parent, device_t self, void *aux)
84 {
85 struct ehci_acpi_softc * const asc = device_private(self);
86 struct ehci_softc * const sc = &asc->sc_ehci;
87 struct acpi_attach_args *aa = aux;
88 struct acpi_resources res;
89 struct acpi_mem *mem;
90 struct acpi_irq *irq;
91 ACPI_STATUS rv;
92 int error;
93 void *ih;
94
95 asc->sc_handle = aa->aa_node->ad_handle;
96
97 sc->sc_dev = self;
98 sc->sc_bus.ub_hcpriv = sc;
99 sc->sc_bus.ub_dmatag = aa->aa_dmat;
100 sc->sc_bus.ub_revision = USBREV_2_0;
101 sc->sc_flags = EHCIF_ETTF;
102 sc->sc_vendor_init = ehci_acpi_init;
103
104 rv = acpi_resource_parse(sc->sc_dev, asc->sc_handle, "_CRS",
105 &res, &acpi_resource_parse_ops_default);
106 if (ACPI_FAILURE(rv))
107 return;
108
109 mem = acpi_res_mem(&res, 0);
110 if (mem == NULL) {
111 aprint_error_dev(self, "couldn't find mem resource\n");
112 goto done;
113 }
114
115 irq = acpi_res_irq(&res, 0);
116 if (mem == NULL) {
117 aprint_error_dev(self, "couldn't find irq resource\n");
118 goto done;
119 }
120
121 sc->sc_size = mem->ar_length;
122 sc->iot = aa->aa_memt;
123 error = bus_space_map(sc->iot, mem->ar_base, mem->ar_length, 0, &sc->ioh);
124 if (error) {
125 aprint_error_dev(self, "couldn't map registers\n");
126 return;
127 }
128
129 /* Disable interrupts */
130 sc->sc_offs = EREAD1(sc, EHCI_CAPLENGTH);
131 EOWRITE4(sc, EHCI_USBINTR, 0);
132
133 const int type = (irq->ar_type == ACPI_EDGE_SENSITIVE) ? IST_EDGE : IST_LEVEL;
134 ih = intr_establish(irq->ar_irq, IPL_USB, type, ehci_intr, sc);
135 if (ih == NULL) {
136 aprint_error_dev(self, "couldn't establish interrupt\n");
137 return;
138 }
139
140 error = ehci_init(sc);
141 if (error) {
142 aprint_error_dev(self, "init failed, error = %d\n", error);
143 return;
144 }
145
146 sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint);
147
148 done:
149 acpi_resource_cleanup(&res);
150 }
151
152 static void
153 ehci_acpi_init(struct ehci_softc *sc)
154 {
155 struct ehci_acpi_softc * const asc = (struct ehci_acpi_softc *)sc;
156
157 acpi_usb_post_reset(asc->sc_handle);
158 }
159