1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2018 Ruslan Bukin <br@bsdpad.com>
5 * All rights reserved.
6 *
7 * This software was developed by BAE Systems, the University of Cambridge
8 * Computer Laboratory, and Memorial University under DARPA/AFRL contract
9 * FA8650-15-C-7558 ("CADETS"), as part of the DARPA Transparent Computing
10 * (TC) research program.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #include "opt_bus.h"
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/bus.h>
39 #include <sys/rman.h>
40 #include <sys/condvar.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
43
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.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 #include <dev/usb/controller/ehci.h>
58 #include <dev/usb/controller/ehcireg.h>
59
60 struct ehci_msm_softc {
61 ehci_softc_t base;
62 struct resource *res[3];
63 };
64
65 static struct resource_spec ehci_msm_spec[] = {
66 { SYS_RES_MEMORY, 0, RF_ACTIVE },
67 { SYS_RES_MEMORY, 1, RF_ACTIVE },
68 { SYS_RES_IRQ, 0, RF_ACTIVE },
69 { -1, 0 }
70 };
71
72 #define EHCI_HC_DEVSTR "Qualcomm USB 2.0 controller"
73
74 static device_attach_t ehci_msm_attach;
75 static device_detach_t ehci_msm_detach;
76
77 static int
ehci_msm_probe(device_t dev)78 ehci_msm_probe(device_t dev)
79 {
80
81 if (!ofw_bus_is_compatible(dev, "qcom,ci-hdrc"))
82 return (ENXIO);
83
84 device_set_desc(dev, EHCI_HC_DEVSTR);
85
86 return (BUS_PROBE_DEFAULT);
87 }
88
89 static int
ehci_msm_attach(device_t dev)90 ehci_msm_attach(device_t dev)
91 {
92 struct ehci_msm_softc *esc;
93 bus_space_handle_t bsh;
94 ehci_softc_t *sc;
95 int err;
96
97 esc = device_get_softc(dev);
98 sc = &esc->base;
99 sc->sc_bus.parent = dev;
100 sc->sc_bus.devices = sc->sc_devices;
101 sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
102 sc->sc_bus.dma_bits = 32;
103
104 if (bus_alloc_resources(dev, ehci_msm_spec, esc->res)) {
105 device_printf(dev, "could not allocate resources\n");
106 return (ENXIO);
107 }
108
109 sc->sc_io_tag = rman_get_bustag(esc->res[0]);
110
111 /* Get all DMA memory */
112 if (usb_bus_mem_alloc_all(&sc->sc_bus,
113 USB_GET_DMA_TAG(dev), &ehci_iterate_hw_softc)) {
114 return (ENOMEM);
115 }
116
117 /* EHCI registers */
118 sc->sc_io_tag = rman_get_bustag(esc->res[0]);
119 bsh = rman_get_bushandle(esc->res[0]);
120 sc->sc_io_size = rman_get_size(esc->res[0]);
121
122 if (bus_space_subregion(sc->sc_io_tag, bsh, 0x100,
123 sc->sc_io_size, &sc->sc_io_hdl) != 0)
124 panic("%s: unable to subregion USB host registers",
125 device_get_name(dev));
126
127 sc->sc_bus.bdev = device_add_child(dev, "usbus", DEVICE_UNIT_ANY);
128 if (!sc->sc_bus.bdev) {
129 device_printf(dev, "Could not add USB device\n");
130 goto error;
131 }
132 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
133 device_set_desc(sc->sc_bus.bdev, EHCI_HC_DEVSTR);
134
135 sprintf(sc->sc_vendor, "Qualcomm");
136
137 err = bus_setup_intr(dev, esc->res[2], INTR_TYPE_BIO | INTR_MPSAFE,
138 NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
139 if (err) {
140 device_printf(dev, "Could not setup irq, %d\n", err);
141 sc->sc_intr_hdl = NULL;
142 goto error;
143 }
144
145 sc->sc_flags |= EHCI_SCFLG_DONTRESET | EHCI_SCFLG_NORESTERM;
146
147 err = ehci_init(sc);
148 if (!err) {
149 sc->sc_flags |= EHCI_SCFLG_DONEINIT;
150 err = device_probe_and_attach(sc->sc_bus.bdev);
151 }
152
153 if (err) {
154 device_printf(dev, "USB init failed err=%d\n", err);
155 goto error;
156 }
157 return (0);
158
159 error:
160 ehci_msm_detach(dev);
161 return (ENXIO);
162 }
163
164 static int
ehci_msm_detach(device_t dev)165 ehci_msm_detach(device_t dev)
166 {
167 ehci_softc_t *sc;
168 device_t bdev;
169 int err;
170
171 sc = device_get_softc(dev);
172
173 err = bus_generic_detach(dev);
174 if (err != 0)
175 return (err);
176
177 if (sc->sc_irq_res && sc->sc_intr_hdl) {
178 /* only call ehci_detach() after ehci_init() */
179 ehci_detach(sc);
180
181 err = bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intr_hdl);
182 if (err)
183 device_printf(dev, "Could not tear down irq, %d\n",
184 err);
185 sc->sc_intr_hdl = NULL;
186 }
187
188 if (sc->sc_irq_res) {
189 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
190 sc->sc_irq_res = NULL;
191 }
192 if (sc->sc_io_res) {
193 bus_release_resource(dev, SYS_RES_MEMORY, 0,
194 sc->sc_io_res);
195 sc->sc_io_res = NULL;
196 }
197
198 usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
199
200 return (0);
201 }
202
203 static device_method_t ehci_methods[] = {
204 /* Device interface */
205 DEVMETHOD(device_probe, ehci_msm_probe),
206 DEVMETHOD(device_attach, ehci_msm_attach),
207 DEVMETHOD(device_detach, ehci_msm_detach),
208 DEVMETHOD(device_suspend, bus_generic_suspend),
209 DEVMETHOD(device_resume, bus_generic_resume),
210 DEVMETHOD(device_shutdown, bus_generic_shutdown),
211 DEVMETHOD_END
212 };
213
214 static driver_t ehci_driver = {
215 .name = "ehci",
216 .methods = ehci_methods,
217 .size = sizeof(ehci_softc_t),
218 };
219
220 DRIVER_MODULE(ehci_msm, simplebus, ehci_driver, 0, 0);
221 MODULE_DEPEND(ehci_msm, usb, 1, 1, 1);
222