xref: /freebsd/sys/dev/usb/controller/dwc_otg_fdt.c (revision 39ee7a7a6bdd1557b1c3532abf60d139798ac88b)
1 /*-
2  * Copyright (c) 2012 Hans Petter Selasky. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/bus.h>
32 #include <sys/condvar.h>
33 #include <sys/kernel.h>
34 #include <sys/lock.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/mutex.h>
38 #include <sys/rman.h>
39 
40 #include <dev/ofw/openfirm.h>
41 #include <dev/ofw/ofw_bus.h>
42 #include <dev/ofw/ofw_bus_subr.h>
43 
44 #include <dev/usb/usb.h>
45 #include <dev/usb/usbdi.h>
46 
47 #include <dev/usb/usb_core.h>
48 #include <dev/usb/usb_busdma.h>
49 #include <dev/usb/usb_process.h>
50 #include <dev/usb/usb_util.h>
51 
52 #include <dev/usb/usb_controller.h>
53 #include <dev/usb/usb_bus.h>
54 
55 #include <dev/usb/controller/dwc_otg.h>
56 #include <dev/usb/controller/dwc_otg_fdt.h>
57 
58 static device_probe_t dwc_otg_probe;
59 static device_detach_t dwc_otg_detach;
60 
61 static int
62 dwc_otg_probe(device_t dev)
63 {
64 
65 	if (!ofw_bus_status_okay(dev))
66 		return (ENXIO);
67 
68 	if (!ofw_bus_is_compatible(dev, "synopsys,designware-hs-otg2"))
69 		return (ENXIO);
70 
71 	device_set_desc(dev, "DWC OTG 2.0 integrated USB controller");
72 
73 	return (BUS_PROBE_DEFAULT);
74 }
75 
76 int
77 dwc_otg_attach(device_t dev)
78 {
79 	struct dwc_otg_fdt_softc *sc = device_get_softc(dev);
80 	char usb_mode[24];
81 	int err;
82 	int rid;
83 
84 	/* initialise some bus fields */
85 	sc->sc_otg.sc_bus.parent = dev;
86 	sc->sc_otg.sc_bus.devices = sc->sc_otg.sc_devices;
87 	sc->sc_otg.sc_bus.devices_max = DWC_OTG_MAX_DEVICES;
88 	sc->sc_otg.sc_bus.dma_bits = 32;
89 
90 	/* get USB mode, if any */
91 	if (OF_getprop(ofw_bus_get_node(dev), "dr_mode",
92 	    &usb_mode, sizeof(usb_mode)) > 0) {
93 
94 		/* ensure proper zero termination */
95 		usb_mode[sizeof(usb_mode) - 1] = 0;
96 
97 		if (strcasecmp(usb_mode, "host") == 0)
98 			sc->sc_otg.sc_mode = DWC_MODE_HOST;
99 		else if (strcasecmp(usb_mode, "peripheral") == 0)
100 			sc->sc_otg.sc_mode = DWC_MODE_DEVICE;
101 		else if (strcasecmp(usb_mode, "otg") != 0) {
102 			device_printf(dev, "Invalid FDT dr_mode: %s\n",
103 			    usb_mode);
104 		}
105 	}
106 
107 	/* get all DMA memory */
108 	if (usb_bus_mem_alloc_all(&sc->sc_otg.sc_bus,
109 	    USB_GET_DMA_TAG(dev), NULL)) {
110 		return (ENOMEM);
111 	}
112 	rid = 0;
113 	sc->sc_otg.sc_io_res =
114 	    bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
115 
116 	if (!(sc->sc_otg.sc_io_res)) {
117 		err = ENOMEM;
118 		goto error;
119 	}
120 	sc->sc_otg.sc_io_tag = rman_get_bustag(sc->sc_otg.sc_io_res);
121 	sc->sc_otg.sc_io_hdl = rman_get_bushandle(sc->sc_otg.sc_io_res);
122 	sc->sc_otg.sc_io_size = rman_get_size(sc->sc_otg.sc_io_res);
123 
124 	rid = 0;
125 	sc->sc_otg.sc_irq_res =
126 	    bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
127 	if (sc->sc_otg.sc_irq_res == NULL)
128 		goto error;
129 
130 	sc->sc_otg.sc_bus.bdev = device_add_child(dev, "usbus", -1);
131 	if (sc->sc_otg.sc_bus.bdev == NULL)
132 		goto error;
133 
134 	device_set_ivars(sc->sc_otg.sc_bus.bdev, &sc->sc_otg.sc_bus);
135 
136 	err = bus_setup_intr(dev, sc->sc_otg.sc_irq_res, INTR_TYPE_TTY | INTR_MPSAFE,
137 	    &dwc_otg_filter_interrupt, &dwc_otg_interrupt, sc, &sc->sc_otg.sc_intr_hdl);
138 	if (err) {
139 		sc->sc_otg.sc_intr_hdl = NULL;
140 		goto error;
141 	}
142 	err = dwc_otg_init(&sc->sc_otg);
143 	if (err == 0) {
144 		err = device_probe_and_attach(sc->sc_otg.sc_bus.bdev);
145 	}
146 	if (err)
147 		goto error;
148 
149 
150 	return (0);
151 
152 error:
153 	dwc_otg_detach(dev);
154 	return (ENXIO);
155 }
156 
157 static int
158 dwc_otg_detach(device_t dev)
159 {
160 	struct dwc_otg_fdt_softc *sc = device_get_softc(dev);
161 	device_t bdev;
162 	int err;
163 
164 	if (sc->sc_otg.sc_bus.bdev) {
165 		bdev = sc->sc_otg.sc_bus.bdev;
166 		device_detach(bdev);
167 		device_delete_child(dev, bdev);
168 	}
169 	/* during module unload there are lots of children leftover */
170 	device_delete_children(dev);
171 
172 	if (sc->sc_otg.sc_irq_res && sc->sc_otg.sc_intr_hdl) {
173 		/*
174 		 * only call dwc_otg_uninit() after dwc_otg_init()
175 		 */
176 		dwc_otg_uninit(&sc->sc_otg);
177 
178 		err = bus_teardown_intr(dev, sc->sc_otg.sc_irq_res,
179 		    sc->sc_otg.sc_intr_hdl);
180 		sc->sc_otg.sc_intr_hdl = NULL;
181 	}
182 	/* free IRQ channel, if any */
183 	if (sc->sc_otg.sc_irq_res) {
184 		bus_release_resource(dev, SYS_RES_IRQ, 0,
185 		    sc->sc_otg.sc_irq_res);
186 		sc->sc_otg.sc_irq_res = NULL;
187 	}
188 	/* free memory resource, if any */
189 	if (sc->sc_otg.sc_io_res) {
190 		bus_release_resource(dev, SYS_RES_MEMORY, 0,
191 		    sc->sc_otg.sc_io_res);
192 		sc->sc_otg.sc_io_res = NULL;
193 	}
194 	usb_bus_mem_free_all(&sc->sc_otg.sc_bus, NULL);
195 
196 	return (0);
197 }
198 
199 static device_method_t dwc_otg_methods[] = {
200 	/* Device interface */
201 	DEVMETHOD(device_probe, dwc_otg_probe),
202 	DEVMETHOD(device_attach, dwc_otg_attach),
203 	DEVMETHOD(device_detach, dwc_otg_detach),
204 	DEVMETHOD(device_suspend, bus_generic_suspend),
205 	DEVMETHOD(device_resume, bus_generic_resume),
206 	DEVMETHOD(device_shutdown, bus_generic_shutdown),
207 
208 	DEVMETHOD_END
209 };
210 
211 driver_t dwc_otg_driver = {
212 	.name = "dwcotg",
213 	.methods = dwc_otg_methods,
214 	.size = sizeof(struct dwc_otg_fdt_softc),
215 };
216 
217 static devclass_t dwc_otg_devclass;
218 
219 DRIVER_MODULE(dwcotg, simplebus, dwc_otg_driver, dwc_otg_devclass, 0, 0);
220 MODULE_DEPEND(dwcotg, usb, 1, 1, 1);
221