xref: /f-stack/freebsd/mips/mediatek/mtk_dotg.c (revision 22ce4aff)
1 #include <sys/cdefs.h>
2 __FBSDID("$FreeBSD$");
3 
4 /*-
5  * Copyright (c) 2015-2016 Stanislav Galabov. All rights reserved.
6  * Copyright (c) 2010,2011 Aleksandr Rybalko. All rights reserved.
7  * Copyright (c) 2007-2008 Hans Petter Selasky. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/stdint.h>
32 #include <sys/stddef.h>
33 #include <sys/param.h>
34 #include <sys/queue.h>
35 #include <sys/types.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/bus.h>
39 #include <sys/module.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/condvar.h>
43 #include <sys/sysctl.h>
44 #include <sys/sx.h>
45 #include <sys/unistd.h>
46 #include <sys/callout.h>
47 #include <sys/malloc.h>
48 #include <sys/priv.h>
49 #include <sys/rman.h>
50 
51 #include <dev/usb/usb.h>
52 #include <dev/usb/usbdi.h>
53 
54 #include <dev/usb/usb_core.h>
55 #include <dev/usb/usb_busdma.h>
56 #include <dev/usb/usb_process.h>
57 #include <dev/usb/usb_util.h>
58 
59 #include <dev/usb/usb_controller.h>
60 #include <dev/usb/usb_bus.h>
61 
62 #include <dev/usb/controller/dwc_otg.h>
63 
64 #include <dev/ofw/openfirm.h>
65 #include <dev/ofw/ofw_bus.h>
66 #include <dev/ofw/ofw_bus_subr.h>
67 
68 #define	MEM_RID	0
69 
70 static device_probe_t dotg_fdt_probe;
71 static device_attach_t dotg_fdt_attach;
72 static device_detach_t dotg_fdt_detach;
73 
74 static int
dotg_fdt_probe(device_t dev)75 dotg_fdt_probe(device_t dev)
76 {
77 
78 	if (!ofw_bus_status_okay(dev))
79 		return (ENXIO);
80 
81 	if (!ofw_bus_is_compatible(dev, "ralink,rt3050-otg"))
82 		return (ENXIO);
83 
84 	device_set_desc(dev, "MTK DWC-OTG USB Controller");
85 	return (0);
86 }
87 
88 static int
dotg_fdt_attach(device_t dev)89 dotg_fdt_attach(device_t dev)
90 {
91 	struct dwc_otg_softc *sc = device_get_softc(dev);
92 	int err, rid;
93 
94 	/* setup controller interface softc */
95 
96 	/* initialise some bus fields */
97 	sc->sc_mode = DWC_MODE_HOST;
98 	sc->sc_bus.parent = dev;
99 
100 	rid = 0;
101 	sc->sc_io_res =
102 	    bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
103 	if (!(sc->sc_io_res)) {
104 		printf("Can`t alloc MEM\n");
105 		goto error;
106 	}
107 
108 	rid = 0;
109 	sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
110 	    &rid, RF_ACTIVE);
111 	if (!(sc->sc_irq_res)) {
112 		printf("Can`t alloc IRQ\n");
113 		goto error;
114 	}
115 
116 	sc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
117 	if (!(sc->sc_bus.bdev)) {
118 		printf("Can`t add usbus\n");
119 		goto error;
120 	}
121 	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
122 
123 	err = dwc_otg_init(sc);
124 	if (err) printf("dotg_init fail\n");
125 	if (!err) {
126 		err = device_probe_and_attach(sc->sc_bus.bdev);
127 		if (err) printf("device_probe_and_attach fail %d\n", err);
128 	}
129 	if (err) {
130 		goto error;
131 	}
132 	return (0);
133 
134 error:
135 	dotg_fdt_detach(dev);
136 	return (ENXIO);
137 }
138 
139 static int
dotg_fdt_detach(device_t dev)140 dotg_fdt_detach(device_t dev)
141 {
142 	struct dwc_otg_softc *sc = device_get_softc(dev);
143 	int err;
144 
145 	/* during module unload there are lots of children leftover */
146 	device_delete_children(dev);
147 
148 	if (sc->sc_irq_res && sc->sc_intr_hdl) {
149 		/*
150 		 * only call dotg_fdt_uninit() after dotg_fdt_init()
151 		 */
152 		dwc_otg_uninit(sc);
153 
154 		err = bus_teardown_intr(dev, sc->sc_irq_res,
155 		    sc->sc_intr_hdl);
156 		sc->sc_intr_hdl = NULL;
157 	}
158 	if (sc->sc_irq_res) {
159 		bus_release_resource(dev, SYS_RES_IRQ, 0,
160 		    sc->sc_irq_res);
161 		sc->sc_irq_res = NULL;
162 	}
163 	if (sc->sc_io_res) {
164 		bus_release_resource(dev, SYS_RES_MEMORY, 0,
165 		    sc->sc_io_res);
166 		sc->sc_io_res = NULL;
167 	}
168 	usb_bus_mem_free_all(&sc->sc_bus, NULL);
169 
170 	return (0);
171 }
172 
173 static device_method_t dotg_fdt_methods[] = {
174 	/* Device interface */
175 	DEVMETHOD(device_probe, dotg_fdt_probe),
176 	DEVMETHOD(device_attach, dotg_fdt_attach),
177 	DEVMETHOD(device_detach, dotg_fdt_detach),
178 	DEVMETHOD(device_suspend, bus_generic_suspend),
179 	DEVMETHOD(device_resume, bus_generic_resume),
180 	DEVMETHOD(device_shutdown, bus_generic_shutdown),
181 
182 	DEVMETHOD_END
183 };
184 
185 static driver_t dotg_fdt_driver = {
186 	.name = "dwcotg",
187 	.methods = dotg_fdt_methods,
188 	.size = sizeof(struct dwc_otg_softc),
189 };
190 
191 static devclass_t dotg_fdt_devclass;
192 
193 DRIVER_MODULE(dotg, simplebus, dotg_fdt_driver, dotg_fdt_devclass, 0, 0);
194