1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2012 Hans Petter Selasky.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 #include "opt_acpi.h"
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/bus.h>
34 #include <sys/condvar.h>
35 #include <sys/kernel.h>
36 #include <sys/lock.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/mutex.h>
40 #include <sys/rman.h>
41
42 #include <contrib/dev/acpica/include/acpi.h>
43 #include <contrib/dev/acpica/include/accommon.h>
44
45 #include <dev/acpica/acpivar.h>
46
47 #include <dev/usb/usb.h>
48 #include <dev/usb/usbdi.h>
49
50 #include <dev/usb/usb_core.h>
51 #include <dev/usb/usb_busdma.h>
52 #include <dev/usb/usb_process.h>
53 #include <dev/usb/usb_util.h>
54
55 #include <dev/usb/usb_controller.h>
56 #include <dev/usb/usb_bus.h>
57
58 #include <dev/usb/controller/dwc_otg.h>
59
60 static device_probe_t dwc_otg_probe;
61 static device_attach_t dwc_otg_attach;
62 static device_attach_t dwc_otg_detach;
63
64 static char *dwc_otg_ids[] = {
65 "BCM2848",
66 NULL
67 };
68
69 static int
dwc_otg_probe(device_t dev)70 dwc_otg_probe(device_t dev)
71 {
72 int rv;
73
74 if (acpi_disabled("dwc_otg"))
75 return (ENXIO);
76
77 rv = ACPI_ID_PROBE(device_get_parent(dev), dev, dwc_otg_ids, NULL);
78 if (rv > 0)
79 return (rv);
80
81 device_set_desc(dev, "DWC OTG 2.0 integrated USB controller");
82
83 return (BUS_PROBE_DEFAULT);
84 }
85
86 static int
dwc_otg_attach(device_t dev)87 dwc_otg_attach(device_t dev)
88 {
89 struct dwc_otg_softc *sc = device_get_softc(dev);
90 int err;
91 int rid;
92
93 sc->sc_bus.parent = dev;
94
95 /* assume device mode (this is only used for the Raspberry Pi 4's
96 * USB-C port, which only works in device mode) */
97 sc->sc_mode = DWC_MODE_DEVICE;
98
99 rid = 0;
100 sc->sc_io_res =
101 bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
102
103 if (sc->sc_io_res == NULL)
104 goto error;
105
106 rid = 0;
107 sc->sc_irq_res =
108 bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
109 if (sc->sc_irq_res == NULL)
110 goto error;
111
112 err = dwc_otg_init(sc);
113 if (err == 0) {
114 err = device_probe_and_attach(sc->sc_bus.bdev);
115 }
116 if (err)
117 goto error;
118
119 return (0);
120
121 error:
122 dwc_otg_detach(dev);
123 return (ENXIO);
124 }
125
126 static int
dwc_otg_detach(device_t dev)127 dwc_otg_detach(device_t dev)
128 {
129 struct dwc_otg_softc *sc = device_get_softc(dev);
130 int error;
131
132 /* during module unload there are lots of children leftover */
133 error = bus_generic_detach(dev);
134 if (error != 0)
135 return (error);
136
137 if (sc->sc_irq_res && sc->sc_intr_hdl) {
138 /*
139 * only call dwc_otg_uninit() after dwc_otg_init()
140 */
141 dwc_otg_uninit(sc);
142
143 bus_teardown_intr(dev, sc->sc_irq_res,
144 sc->sc_intr_hdl);
145 sc->sc_intr_hdl = NULL;
146 }
147 /* free IRQ channel, if any */
148 if (sc->sc_irq_res) {
149 bus_release_resource(dev, SYS_RES_IRQ, 0,
150 sc->sc_irq_res);
151 sc->sc_irq_res = NULL;
152 }
153 /* free memory resource, if any */
154 if (sc->sc_io_res) {
155 bus_release_resource(dev, SYS_RES_MEMORY, 0,
156 sc->sc_io_res);
157 sc->sc_io_res = NULL;
158 }
159 usb_bus_mem_free_all(&sc->sc_bus, NULL);
160
161 return (0);
162 }
163
164 static device_method_t dwc_otg_methods[] = {
165 /* Device interface */
166 DEVMETHOD(device_probe, dwc_otg_probe),
167 DEVMETHOD(device_attach, dwc_otg_attach),
168 DEVMETHOD(device_detach, dwc_otg_detach),
169 DEVMETHOD(device_suspend, bus_generic_suspend),
170 DEVMETHOD(device_resume, bus_generic_resume),
171 DEVMETHOD(device_shutdown, bus_generic_shutdown),
172
173 DEVMETHOD_END
174 };
175
176 static driver_t dwc_otg_driver = {
177 .name = "dwcotg",
178 .methods = dwc_otg_methods,
179 .size = sizeof(struct dwc_otg_softc),
180 };
181
182 DRIVER_MODULE(dwcotg, acpi, dwc_otg_driver, 0, 0);
183 MODULE_DEPEND(dwcotg, usb, 1, 1, 1);
184