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 <sys/cdefs.h>
35 #include "opt_bus.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/bus.h>
40 #include <sys/rman.h>
41 #include <sys/condvar.h>
42 #include <sys/kernel.h>
43 #include <sys/module.h>
44
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_bus_subr.h>
47
48 #include <dev/usb/usb.h>
49 #include <dev/usb/usbdi.h>
50
51 #include <dev/usb/usb_core.h>
52 #include <dev/usb/usb_busdma.h>
53 #include <dev/usb/usb_process.h>
54 #include <dev/usb/usb_util.h>
55
56 #include <dev/usb/usb_controller.h>
57 #include <dev/usb/usb_bus.h>
58 #include <dev/usb/controller/ehci.h>
59 #include <dev/usb/controller/ehcireg.h>
60
61 struct ehci_msm_softc {
62 ehci_softc_t base;
63 struct resource *res[3];
64 };
65
66 static struct resource_spec ehci_msm_spec[] = {
67 { SYS_RES_MEMORY, 0, RF_ACTIVE },
68 { SYS_RES_MEMORY, 1, RF_ACTIVE },
69 { SYS_RES_IRQ, 0, RF_ACTIVE },
70 { -1, 0 }
71 };
72
73 #define EHCI_HC_DEVSTR "Qualcomm USB 2.0 controller"
74
75 static device_attach_t ehci_msm_attach;
76 static device_detach_t ehci_msm_detach;
77
78 static int
ehci_msm_probe(device_t dev)79 ehci_msm_probe(device_t dev)
80 {
81
82 if (!ofw_bus_is_compatible(dev, "qcom,ci-hdrc"))
83 return (ENXIO);
84
85 device_set_desc(dev, EHCI_HC_DEVSTR);
86
87 return (BUS_PROBE_DEFAULT);
88 }
89
90 static int
ehci_msm_attach(device_t dev)91 ehci_msm_attach(device_t dev)
92 {
93 struct ehci_msm_softc *esc;
94 bus_space_handle_t bsh;
95 ehci_softc_t *sc;
96 int err;
97
98 esc = device_get_softc(dev);
99 sc = &esc->base;
100 sc->sc_bus.parent = dev;
101 sc->sc_bus.devices = sc->sc_devices;
102 sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
103 sc->sc_bus.dma_bits = 32;
104
105 if (bus_alloc_resources(dev, ehci_msm_spec, esc->res)) {
106 device_printf(dev, "could not allocate resources\n");
107 return (ENXIO);
108 }
109
110 sc->sc_io_tag = rman_get_bustag(esc->res[0]);
111
112 /* Get all DMA memory */
113 if (usb_bus_mem_alloc_all(&sc->sc_bus,
114 USB_GET_DMA_TAG(dev), &ehci_iterate_hw_softc)) {
115 return (ENOMEM);
116 }
117
118 /* EHCI registers */
119 sc->sc_io_tag = rman_get_bustag(esc->res[0]);
120 bsh = rman_get_bushandle(esc->res[0]);
121 sc->sc_io_size = rman_get_size(esc->res[0]);
122
123 if (bus_space_subregion(sc->sc_io_tag, bsh, 0x100,
124 sc->sc_io_size, &sc->sc_io_hdl) != 0)
125 panic("%s: unable to subregion USB host registers",
126 device_get_name(dev));
127
128 sc->sc_bus.bdev = device_add_child(dev, "usbus", DEVICE_UNIT_ANY);
129 if (!sc->sc_bus.bdev) {
130 device_printf(dev, "Could not add USB device\n");
131 goto error;
132 }
133 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
134 device_set_desc(sc->sc_bus.bdev, EHCI_HC_DEVSTR);
135
136 sprintf(sc->sc_vendor, "Qualcomm");
137
138 err = bus_setup_intr(dev, esc->res[2], INTR_TYPE_BIO | INTR_MPSAFE,
139 NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
140 if (err) {
141 device_printf(dev, "Could not setup irq, %d\n", err);
142 sc->sc_intr_hdl = NULL;
143 goto error;
144 }
145
146 sc->sc_flags |= EHCI_SCFLG_DONTRESET | EHCI_SCFLG_NORESTERM;
147
148 err = ehci_init(sc);
149 if (!err) {
150 sc->sc_flags |= EHCI_SCFLG_DONEINIT;
151 err = device_probe_and_attach(sc->sc_bus.bdev);
152 }
153
154 if (err) {
155 device_printf(dev, "USB init failed err=%d\n", err);
156 goto error;
157 }
158 return (0);
159
160 error:
161 ehci_msm_detach(dev);
162 return (ENXIO);
163 }
164
165 static int
ehci_msm_detach(device_t dev)166 ehci_msm_detach(device_t dev)
167 {
168 ehci_softc_t *sc;
169 device_t bdev;
170 int err;
171
172 sc = device_get_softc(dev);
173
174 err = bus_generic_detach(dev);
175 if (err != 0)
176 return (err);
177
178 if (sc->sc_irq_res && sc->sc_intr_hdl) {
179 /* only call ehci_detach() after ehci_init() */
180 ehci_detach(sc);
181
182 err = bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intr_hdl);
183 if (err)
184 device_printf(dev, "Could not tear down irq, %d\n",
185 err);
186 sc->sc_intr_hdl = NULL;
187 }
188
189 if (sc->sc_irq_res) {
190 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
191 sc->sc_irq_res = NULL;
192 }
193 if (sc->sc_io_res) {
194 bus_release_resource(dev, SYS_RES_MEMORY, 0,
195 sc->sc_io_res);
196 sc->sc_io_res = NULL;
197 }
198
199 usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
200
201 return (0);
202 }
203
204 static device_method_t ehci_methods[] = {
205 /* Device interface */
206 DEVMETHOD(device_probe, ehci_msm_probe),
207 DEVMETHOD(device_attach, ehci_msm_attach),
208 DEVMETHOD(device_detach, ehci_msm_detach),
209 DEVMETHOD(device_suspend, bus_generic_suspend),
210 DEVMETHOD(device_resume, bus_generic_resume),
211 DEVMETHOD(device_shutdown, bus_generic_shutdown),
212 DEVMETHOD_END
213 };
214
215 static driver_t ehci_driver = {
216 .name = "ehci",
217 .methods = ehci_methods,
218 .size = sizeof(ehci_softc_t),
219 };
220
221 DRIVER_MODULE(ehci_msm, simplebus, ehci_driver, 0, 0);
222 MODULE_DEPEND(ehci_msm, usb, 1, 1, 1);
223