xref: /freebsd/sys/dev/usb/controller/dwc_otg_acpi.c (revision 38a52bd3)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
30 
31 #include "opt_acpi.h"
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/bus.h>
36 #include <sys/condvar.h>
37 #include <sys/kernel.h>
38 #include <sys/lock.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <sys/rman.h>
43 
44 #include <contrib/dev/acpica/include/acpi.h>
45 #include <contrib/dev/acpica/include/accommon.h>
46 
47 #include <dev/acpica/acpivar.h>
48 
49 #include <dev/usb/usb.h>
50 #include <dev/usb/usbdi.h>
51 
52 #include <dev/usb/usb_core.h>
53 #include <dev/usb/usb_busdma.h>
54 #include <dev/usb/usb_process.h>
55 #include <dev/usb/usb_util.h>
56 
57 #include <dev/usb/usb_controller.h>
58 #include <dev/usb/usb_bus.h>
59 
60 #include <dev/usb/controller/dwc_otg.h>
61 
62 static device_probe_t dwc_otg_probe;
63 static device_attach_t dwc_otg_attach;
64 static device_attach_t dwc_otg_detach;
65 
66 static char *dwc_otg_ids[] = {
67 	"BCM2848",
68 	NULL
69 };
70 
71 static int
72 dwc_otg_probe(device_t dev)
73 {
74 	int rv;
75 
76 	if (acpi_disabled("dwc_otg"))
77 		return (ENXIO);
78 
79 	rv = ACPI_ID_PROBE(device_get_parent(dev), dev, dwc_otg_ids, NULL);
80 	if (rv > 0)
81 		return (rv);
82 
83 	device_set_desc(dev, "DWC OTG 2.0 integrated USB controller");
84 
85 	return (BUS_PROBE_DEFAULT);
86 }
87 
88 static int
89 dwc_otg_attach(device_t dev)
90 {
91 	struct dwc_otg_softc *sc = device_get_softc(dev);
92 	int err;
93 	int rid;
94 
95 	sc->sc_bus.parent = dev;
96 
97 	/* assume device mode (this is only used for the Raspberry Pi 4's
98 	 * USB-C port, which only works in device mode) */
99 	sc->sc_mode = DWC_MODE_DEVICE;
100 
101 	rid = 0;
102 	sc->sc_io_res =
103 	    bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
104 
105 	if (sc->sc_io_res == NULL)
106 		goto error;
107 
108 	rid = 0;
109 	sc->sc_irq_res =
110 	    bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
111 	if (sc->sc_irq_res == NULL)
112 		goto error;
113 
114 	err = dwc_otg_init(sc);
115 	if (err == 0) {
116 		err = device_probe_and_attach(sc->sc_bus.bdev);
117 	}
118 	if (err)
119 		goto error;
120 
121 	return (0);
122 
123 error:
124 	dwc_otg_detach(dev);
125 	return (ENXIO);
126 }
127 
128 static int
129 dwc_otg_detach(device_t dev)
130 {
131 	struct dwc_otg_softc *sc = device_get_softc(dev);
132 
133 	/* during module unload there are lots of children leftover */
134 	device_delete_children(dev);
135 
136 	if (sc->sc_irq_res && sc->sc_intr_hdl) {
137 		/*
138 		 * only call dwc_otg_uninit() after dwc_otg_init()
139 		 */
140 		dwc_otg_uninit(sc);
141 
142 		bus_teardown_intr(dev, sc->sc_irq_res,
143 		    sc->sc_intr_hdl);
144 		sc->sc_intr_hdl = NULL;
145 	}
146 	/* free IRQ channel, if any */
147 	if (sc->sc_irq_res) {
148 		bus_release_resource(dev, SYS_RES_IRQ, 0,
149 		    sc->sc_irq_res);
150 		sc->sc_irq_res = NULL;
151 	}
152 	/* free memory resource, if any */
153 	if (sc->sc_io_res) {
154 		bus_release_resource(dev, SYS_RES_MEMORY, 0,
155 		    sc->sc_io_res);
156 		sc->sc_io_res = NULL;
157 	}
158 	usb_bus_mem_free_all(&sc->sc_bus, NULL);
159 
160 	return (0);
161 }
162 
163 static device_method_t dwc_otg_methods[] = {
164 	/* Device interface */
165 	DEVMETHOD(device_probe, dwc_otg_probe),
166 	DEVMETHOD(device_attach, dwc_otg_attach),
167 	DEVMETHOD(device_detach, dwc_otg_detach),
168 	DEVMETHOD(device_suspend, bus_generic_suspend),
169 	DEVMETHOD(device_resume, bus_generic_resume),
170 	DEVMETHOD(device_shutdown, bus_generic_shutdown),
171 
172 	DEVMETHOD_END
173 };
174 
175 static driver_t dwc_otg_driver = {
176 	.name = "dwcotg",
177 	.methods = dwc_otg_methods,
178 	.size = sizeof(struct dwc_otg_softc),
179 };
180 
181 DRIVER_MODULE(dwcotg, acpi, dwc_otg_driver, 0, 0);
182 MODULE_DEPEND(dwcotg, usb, 1, 1, 1);
183