2 * Copyright (c) 2015, Alexander Kabaev <kan@FreeBSD.org>
3 * Copyright (c) 2009, Oleksandr Tymoshenko <gonzo@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice unmodified, this list of conditions, and the following
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
36 #include <sys/condvar.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
41 #include <dev/extres/clk/clk.h>
43 #include <dev/usb/usb.h>
44 #include <dev/usb/usbdi.h>
46 #include <dev/usb/usb_core.h>
47 #include <dev/usb/usb_busdma.h>
48 #include <dev/usb/usb_process.h>
49 #include <dev/usb/usb_util.h>
51 #include <dev/usb/usb_controller.h>
52 #include <dev/usb/usb_bus.h>
53 #include <dev/usb/controller/ohci.h>
54 #include <dev/usb/controller/ohcireg.h>
56 #include <dev/ofw/ofw_bus.h>
57 #include <dev/ofw/ofw_bus_subr.h>
59 #include <dev/gpio/gpiobusvar.h>
61 #include <mips/ingenic/jz4780_clock.h>
62 #include <mips/ingenic/jz4780_regs.h>
64 static int jz4780_ohci_attach(device_t dev);
65 static int jz4780_ohci_detach(device_t dev);
66 static int jz4780_ohci_probe(device_t dev);
68 struct jz4780_ohci_softc
70 struct ohci_softc sc_ohci;
71 struct gpiobus_pin *gpio_vbus;
76 jz4780_ohci_probe(device_t dev)
78 if (!ofw_bus_status_okay(dev))
81 if (!ofw_bus_is_compatible(dev, "ingenic,jz4780-ohci"))
84 device_set_desc(dev, "Ingenic JZ4780 OHCI");
86 return (BUS_PROBE_DEFAULT);
90 jz4780_ohci_vbus_gpio_enable(device_t dev, struct jz4780_ohci_softc *sc)
92 struct gpiobus_pin *gpio_vbus;
95 error = ofw_gpiobus_parse_gpios(dev, "ingenic,vbus-gpio", &gpio_vbus);
97 * The pin can be mapped already by other device. Assume it also has need
98 * activated and proceed happily.
103 sc->gpio_vbus = gpio_vbus;
105 device_printf(dev, "too many vbus gpios\n");
109 if (sc->gpio_vbus != NULL) {
110 error = GPIO_PIN_SET(sc->gpio_vbus->dev, sc->gpio_vbus->pin, 1);
112 device_printf(dev, "Cannot configure GPIO pin %d on %s\n",
113 sc->gpio_vbus->pin, device_get_nameunit(sc->gpio_vbus->dev));
117 error = GPIO_PIN_SETFLAGS(sc->gpio_vbus->dev, sc->gpio_vbus->pin,
120 device_printf(dev, "Cannot configure GPIO pin %d on %s\n",
121 sc->gpio_vbus->pin, device_get_nameunit(sc->gpio_vbus->dev));
129 jz4780_ohci_clk_enable(device_t dev)
131 struct jz4780_ohci_softc *sc;
134 sc = device_get_softc(dev);
136 err = clk_get_by_ofw_index(dev, 0, 0, &sc->clk);
138 device_printf(dev, "unable to lookup device clock\n");
141 err = clk_enable(sc->clk);
143 device_printf(dev, "unable to enable device clock\n");
146 err = clk_set_freq(sc->clk, 48000000, 0);
148 device_printf(dev, "unable to set device clock to 48 kHZ\n");
155 jz4780_ohci_attach(device_t dev)
157 struct jz4780_ohci_softc *sc = device_get_softc(dev);
161 /* initialize some bus fields */
162 sc->sc_ohci.sc_bus.parent = dev;
163 sc->sc_ohci.sc_bus.devices = sc->sc_ohci.sc_devices;
164 sc->sc_ohci.sc_bus.devices_max = OHCI_MAX_DEVICES;
165 sc->sc_ohci.sc_bus.dma_bits = 32;
167 /* get all DMA memory */
168 if (usb_bus_mem_alloc_all(&sc->sc_ohci.sc_bus,
169 USB_GET_DMA_TAG(dev), &ohci_iterate_hw_softc)) {
173 sc->sc_ohci.sc_dev = dev;
176 err = jz4780_ohci_vbus_gpio_enable(dev, sc);
180 err = jz4780_ohci_clk_enable(dev);
185 sc->sc_ohci.sc_io_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
187 if (sc->sc_ohci.sc_io_res == NULL) {
191 sc->sc_ohci.sc_io_tag = rman_get_bustag(sc->sc_ohci.sc_io_res);
192 sc->sc_ohci.sc_io_hdl = rman_get_bushandle(sc->sc_ohci.sc_io_res);
193 sc->sc_ohci.sc_io_size = rman_get_size(sc->sc_ohci.sc_io_res);
196 sc->sc_ohci.sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
198 if (sc->sc_ohci.sc_irq_res == NULL) {
203 if (jz4780_ohci_enable() != 0) {
204 device_printf(dev, "CGU failed to enable OHCI\n");
209 sc->sc_ohci.sc_bus.bdev = device_add_child(dev, "usbus", -1);
210 if (sc->sc_ohci.sc_bus.bdev == NULL) {
214 device_set_ivars(sc->sc_ohci.sc_bus.bdev, &sc->sc_ohci.sc_bus);
216 err = bus_setup_intr(dev, sc->sc_ohci.sc_irq_res,
217 INTR_TYPE_BIO | INTR_MPSAFE, NULL,
218 (driver_intr_t *)ohci_interrupt, sc, &sc->sc_ohci.sc_intr_hdl);
224 strlcpy(sc->sc_ohci.sc_vendor, "Ingenic", sizeof(sc->sc_ohci.sc_vendor));
225 bus_space_write_4(sc->sc_ohci.sc_io_tag, sc->sc_ohci.sc_io_hdl, OHCI_CONTROL, 0);
227 err = ohci_init(&sc->sc_ohci);
229 err = device_probe_and_attach(sc->sc_ohci.sc_bus.bdev);
237 jz4780_ohci_detach(dev);
242 jz4780_ohci_detach(device_t dev)
244 struct jz4780_ohci_softc *sc = device_get_softc(dev);
247 if (sc->sc_ohci.sc_bus.bdev) {
248 bdev = sc->sc_ohci.sc_bus.bdev;
250 device_delete_child(dev, bdev);
252 /* during module unload there are lots of children leftover */
253 device_delete_children(dev);
256 * Put the controller into reset, then disable clocks and do
257 * the MI tear down. We have to disable the clocks/hardware
258 * after we do the rest of the teardown. We also disable the
259 * clocks in the opposite order we acquire them, but that
260 * doesn't seem to be absolutely necessary. We free up the
261 * clocks after we disable them, so the system could, in
262 * theory, reuse them.
264 if (sc->sc_ohci.sc_io_res != NULL) {
265 bus_space_write_4(sc->sc_ohci.sc_io_tag, sc->sc_ohci.sc_io_hdl,
269 if (sc->sc_ohci.sc_intr_hdl) {
270 bus_teardown_intr(dev, sc->sc_ohci.sc_irq_res, sc->sc_ohci.sc_intr_hdl);
271 sc->sc_ohci.sc_intr_hdl = NULL;
274 if (sc->sc_ohci.sc_irq_res && sc->sc_ohci.sc_intr_hdl) {
276 * only call ohci_detach() after ohci_init()
278 ohci_detach(&sc->sc_ohci);
280 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_ohci.sc_irq_res);
281 sc->sc_ohci.sc_irq_res = NULL;
283 if (sc->sc_ohci.sc_io_res) {
284 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_ohci.sc_io_res);
285 sc->sc_ohci.sc_io_res = NULL;
286 sc->sc_ohci.sc_io_tag = 0;
287 sc->sc_ohci.sc_io_hdl = 0;
291 clk_release(sc->clk);
293 usb_bus_mem_free_all(&sc->sc_ohci.sc_bus, &ohci_iterate_hw_softc);
294 free(sc->gpio_vbus, M_DEVBUF);
298 static device_method_t ohci_methods[] = {
299 /* Device interface */
300 DEVMETHOD(device_probe, jz4780_ohci_probe),
301 DEVMETHOD(device_attach, jz4780_ohci_attach),
302 DEVMETHOD(device_detach, jz4780_ohci_detach),
303 DEVMETHOD(device_suspend, bus_generic_suspend),
304 DEVMETHOD(device_resume, bus_generic_resume),
305 DEVMETHOD(device_shutdown, bus_generic_shutdown),
310 static driver_t ohci_driver = {
312 .methods = ohci_methods,
313 .size = sizeof(struct jz4780_ohci_softc),
316 static devclass_t ohci_devclass;
318 DRIVER_MODULE(ohci, simplebus, ohci_driver, ohci_devclass, 0, 0);