1 #include <sys/cdefs.h>
2 /*-
3 * Copyright (c) 2015 Stanislav Galabov. All rights reserved.
4 * Copyright (c) 2010,2011 Aleksandr Rybalko. All rights reserved.
5 * Copyright (c) 2007-2008 Hans Petter Selasky. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/stdint.h>
30 #include <sys/stddef.h>
31 #include <sys/param.h>
32 #include <sys/queue.h>
33 #include <sys/types.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/bus.h>
37 #include <sys/module.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #include <sys/condvar.h>
41 #include <sys/sysctl.h>
42 #include <sys/sx.h>
43 #include <sys/unistd.h>
44 #include <sys/callout.h>
45 #include <sys/malloc.h>
46 #include <sys/priv.h>
47 #include <sys/rman.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/ehci.h>
61
62 #include <dev/ofw/openfirm.h>
63 #include <dev/ofw/ofw_bus.h>
64 #include <dev/ofw/ofw_bus_subr.h>
65
66 #define EHCI_HC_DEVSTR "MTK USB 2.0 Controller"
67
68 static device_probe_t ehci_fdt_probe;
69 static device_attach_t ehci_fdt_attach;
70 static device_detach_t ehci_fdt_detach;
71
72 static int
ehci_fdt_probe(device_t self)73 ehci_fdt_probe(device_t self)
74 {
75
76 if (!ofw_bus_status_okay(self))
77 return (ENXIO);
78
79 if (!ofw_bus_is_compatible(self, "generic-ehci"))
80 return (ENXIO);
81
82 device_set_desc(self, EHCI_HC_DEVSTR);
83
84 return (BUS_PROBE_DEFAULT);
85 }
86
87 static int
ehci_fdt_attach(device_t self)88 ehci_fdt_attach(device_t self)
89 {
90 ehci_softc_t *sc = device_get_softc(self);
91 int err;
92 int rid;
93
94 /* initialise some bus fields */
95 sc->sc_bus.parent = self;
96 sc->sc_bus.devices = sc->sc_devices;
97 sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
98 sc->sc_bus.dma_bits = 32;
99
100 /* get all DMA memory */
101 if (usb_bus_mem_alloc_all(&sc->sc_bus,
102 USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) {
103 printf("No mem\n");
104 return (ENOMEM);
105 }
106
107 rid = 0;
108 sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
109 RF_ACTIVE);
110 if (!sc->sc_io_res) {
111 device_printf(self, "Could not map memory\n");
112 goto error;
113 }
114 sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
115 sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
116 sc->sc_io_size = rman_get_size(sc->sc_io_res);
117
118 rid = 0;
119 sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
120 RF_SHAREABLE | RF_ACTIVE);
121 if (sc->sc_irq_res == NULL) {
122 device_printf(self, "Could not allocate irq\n");
123 goto error;
124 }
125
126 sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
127 if (!(sc->sc_bus.bdev)) {
128 device_printf(self, "Could not add USB device\n");
129 goto error;
130 }
131 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
132 device_set_desc(sc->sc_bus.bdev, EHCI_HC_DEVSTR);
133
134 sprintf(sc->sc_vendor, "MediaTek");
135
136 err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
137 NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
138 if (err) {
139 device_printf(self, "Could not setup irq, %d\n", err);
140 sc->sc_intr_hdl = NULL;
141 goto error;
142 }
143
144 err = ehci_init(sc);
145 if (!err) {
146 err = device_probe_and_attach(sc->sc_bus.bdev);
147 }
148 if (err) {
149 device_printf(self, "USB init failed err=%d\n", err);
150 goto error;
151 }
152 return (0);
153
154 error:
155 ehci_fdt_detach(self);
156 return (ENXIO);
157 }
158
159 static int
ehci_fdt_detach(device_t self)160 ehci_fdt_detach(device_t self)
161 {
162 ehci_softc_t *sc = device_get_softc(self);
163 int err;
164
165 /* during module unload there are lots of children leftover */
166 device_delete_children(self);
167
168 if (sc->sc_irq_res && sc->sc_intr_hdl) {
169 /*
170 * only call ehci_detach() after ehci_init()
171 */
172 ehci_detach(sc);
173
174 err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
175 if (err)
176 device_printf(self, "Could not tear down irq, %d\n",
177 err);
178 sc->sc_intr_hdl = NULL;
179 }
180 if (sc->sc_irq_res) {
181 bus_release_resource(self, SYS_RES_IRQ, 0,
182 sc->sc_irq_res);
183 sc->sc_irq_res = NULL;
184 }
185 if (sc->sc_io_res) {
186 bus_release_resource(self, SYS_RES_MEMORY, 0,
187 sc->sc_io_res);
188 sc->sc_io_res = NULL;
189 }
190 usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
191
192 return (0);
193 }
194
195 static device_method_t ehci_fdt_methods[] = {
196 /* Device interface */
197 DEVMETHOD(device_probe, ehci_fdt_probe),
198 DEVMETHOD(device_attach, ehci_fdt_attach),
199 DEVMETHOD(device_detach, ehci_fdt_detach),
200 DEVMETHOD(device_suspend, bus_generic_suspend),
201 DEVMETHOD(device_resume, bus_generic_resume),
202 DEVMETHOD(device_shutdown, bus_generic_shutdown),
203
204 DEVMETHOD_END
205 };
206
207 static driver_t ehci_fdt_driver = {
208 .name = "ehci",
209 .methods = ehci_fdt_methods,
210 .size = sizeof(ehci_softc_t),
211 };
212
213 static devclass_t ehci_fdt_devclass;
214
215 DRIVER_MODULE(ehci, simplebus, ehci_fdt_driver, ehci_fdt_devclass, 0, 0);
216