xref: /freebsd/sys/dev/usb/controller/dwc_otg_acpi.c (revision 61e21613)
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
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
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
127 dwc_otg_detach(device_t dev)
128 {
129 	struct dwc_otg_softc *sc = device_get_softc(dev);
130 
131 	/* during module unload there are lots of children leftover */
132 	device_delete_children(dev);
133 
134 	if (sc->sc_irq_res && sc->sc_intr_hdl) {
135 		/*
136 		 * only call dwc_otg_uninit() after dwc_otg_init()
137 		 */
138 		dwc_otg_uninit(sc);
139 
140 		bus_teardown_intr(dev, sc->sc_irq_res,
141 		    sc->sc_intr_hdl);
142 		sc->sc_intr_hdl = NULL;
143 	}
144 	/* free IRQ channel, if any */
145 	if (sc->sc_irq_res) {
146 		bus_release_resource(dev, SYS_RES_IRQ, 0,
147 		    sc->sc_irq_res);
148 		sc->sc_irq_res = NULL;
149 	}
150 	/* free memory resource, if any */
151 	if (sc->sc_io_res) {
152 		bus_release_resource(dev, SYS_RES_MEMORY, 0,
153 		    sc->sc_io_res);
154 		sc->sc_io_res = NULL;
155 	}
156 	usb_bus_mem_free_all(&sc->sc_bus, NULL);
157 
158 	return (0);
159 }
160 
161 static device_method_t dwc_otg_methods[] = {
162 	/* Device interface */
163 	DEVMETHOD(device_probe, dwc_otg_probe),
164 	DEVMETHOD(device_attach, dwc_otg_attach),
165 	DEVMETHOD(device_detach, dwc_otg_detach),
166 	DEVMETHOD(device_suspend, bus_generic_suspend),
167 	DEVMETHOD(device_resume, bus_generic_resume),
168 	DEVMETHOD(device_shutdown, bus_generic_shutdown),
169 
170 	DEVMETHOD_END
171 };
172 
173 static driver_t dwc_otg_driver = {
174 	.name = "dwcotg",
175 	.methods = dwc_otg_methods,
176 	.size = sizeof(struct dwc_otg_softc),
177 };
178 
179 DRIVER_MODULE(dwcotg, acpi, dwc_otg_driver, 0, 0);
180 MODULE_DEPEND(dwcotg, usb, 1, 1, 1);
181