2 * Copyright (c) 2015 Oleksandr Tymoshenko <gonzo@freebsd.org>.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * JZ4780 attachment driver for the USB Enhanced Host Controller.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
39 #include <sys/condvar.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
44 #include <dev/extres/clk/clk.h>
46 #include <dev/usb/usb.h>
47 #include <dev/usb/usbdi.h>
49 #include <dev/usb/usb_core.h>
50 #include <dev/usb/usb_busdma.h>
51 #include <dev/usb/usb_process.h>
52 #include <dev/usb/usb_util.h>
54 #include <dev/usb/usb_controller.h>
55 #include <dev/usb/usb_bus.h>
56 #include <dev/usb/controller/ehci.h>
57 #include <dev/usb/controller/ehcireg.h>
59 #include <dev/ofw/ofw_bus.h>
60 #include <dev/ofw/ofw_bus_subr.h>
62 #include <dev/gpio/gpiobusvar.h>
64 #include <mips/ingenic/jz4780_clock.h>
65 #include <mips/ingenic/jz4780_regs.h>
67 #define EHCI_HC_DEVSTR "Ingenic JZ4780 EHCI"
69 struct jz4780_ehci_softc {
70 ehci_softc_t base; /* storage for EHCI code */
72 struct gpiobus_pin *gpio_vbus;
75 static device_probe_t jz4780_ehci_probe;
76 static device_attach_t jz4780_ehci_attach;
77 static device_detach_t jz4780_ehci_detach;
80 jz4780_ehci_vbus_gpio_enable(device_t dev)
82 struct gpiobus_pin *gpio_vbus;
83 struct jz4780_ehci_softc *sc;
86 sc = device_get_softc(dev);
88 err = ofw_gpiobus_parse_gpios(dev, "ingenic,vbus-gpio", &gpio_vbus);
90 * The pin can ne already mapped by other device. Optimistically
96 sc->gpio_vbus = gpio_vbus;
98 device_printf(dev, "too many vbus gpios\n");
102 if (sc->gpio_vbus != NULL) {
103 err = GPIO_PIN_SETFLAGS(sc->gpio_vbus->dev, sc->gpio_vbus->pin,
106 device_printf(dev, "Cannot configure GPIO pin %d on %s\n",
107 sc->gpio_vbus->pin, device_get_nameunit(sc->gpio_vbus->dev));
111 err = GPIO_PIN_SET(sc->gpio_vbus->dev, sc->gpio_vbus->pin, 1);
113 device_printf(dev, "Cannot configure GPIO pin %d on %s\n",
114 sc->gpio_vbus->pin, device_get_nameunit(sc->gpio_vbus->dev));
122 jz4780_ehci_clk_enable(device_t dev)
124 struct jz4780_ehci_softc *sc;
127 sc = device_get_softc(dev);
129 err = clk_get_by_ofw_index(dev, 0, 0, &sc->clk);
131 device_printf(dev, "unable to lookup device clock\n");
134 err = clk_enable(sc->clk);
136 device_printf(dev, "unable to enable device clock\n");
139 err = clk_set_freq(sc->clk, 48000000, 0);
141 device_printf(dev, "unable to set device clock to 48 kHZ\n");
148 jz4780_ehci_intr(void *arg)
155 jz4780_ehci_probe(device_t dev)
158 if (!ofw_bus_status_okay(dev))
161 if (!ofw_bus_is_compatible(dev, "ingenic,jz4780-ehci"))
164 device_set_desc(dev, EHCI_HC_DEVSTR);
166 return (BUS_PROBE_DEFAULT);
170 jz4780_ehci_attach(device_t dev)
172 struct jz4780_ehci_softc *isc;
178 isc = device_get_softc(dev);
181 /* initialise some bus fields */
182 sc->sc_bus.parent = dev;
183 sc->sc_bus.devices = sc->sc_devices;
184 sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
185 sc->sc_bus.dma_bits = 32;
187 /* get all DMA memory */
188 if (usb_bus_mem_alloc_all(&sc->sc_bus,
189 USB_GET_DMA_TAG(dev), &ehci_iterate_hw_softc)) {
193 sc->sc_bus.usbrev = USB_REV_2_0;
195 err = jz4780_ehci_vbus_gpio_enable(dev);
200 sc->sc_io_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
201 if (!sc->sc_io_res) {
202 device_printf(dev, "Could not map memory\n");
207 * Craft special resource for bus space ops that handle
208 * byte-alignment of non-word addresses.
210 sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
211 sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
212 sc->sc_io_size = rman_get_size(sc->sc_io_res);
214 err = jz4780_ehci_clk_enable(dev);
218 if (jz4780_ehci_enable() != 0) {
219 device_printf(dev, "CGU failed to enable EHCI\n");
224 EWRITE4(sc, EHCI_USBINTR, 0);
227 sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
228 RF_ACTIVE | RF_SHAREABLE);
229 if (sc->sc_irq_res == NULL) {
230 device_printf(dev, "Could not allocate irq\n");
233 sc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
234 if (!sc->sc_bus.bdev) {
235 device_printf(dev, "Could not add USB device\n");
238 device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
239 device_set_desc(sc->sc_bus.bdev, EHCI_HC_DEVSTR);
241 sprintf(sc->sc_vendor, "Ingenic");
243 err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
244 NULL, jz4780_ehci_intr, sc, &sc->sc_intr_hdl);
246 device_printf(dev, "Could not setup irq, %d\n", err);
247 sc->sc_intr_hdl = NULL;
253 /* Voodoo: set utmi data bus width on controller to 16 bit */
254 reg = EREAD4(sc, JZ_EHCI_REG_UTMI_BUS);
255 reg |= UTMI_BUS_WIDTH;
256 EWRITE4(sc, JZ_EHCI_REG_UTMI_BUS, reg);
258 err = device_probe_and_attach(sc->sc_bus.bdev);
261 device_printf(dev, "USB init failed err=%d\n", err);
267 jz4780_ehci_detach(dev);
272 jz4780_ehci_detach(device_t dev)
274 struct jz4780_ehci_softc *isc;
279 isc = device_get_softc(dev);
282 if (sc->sc_bus.bdev) {
283 bdev = sc->sc_bus.bdev;
285 device_delete_child(dev, bdev);
287 /* during module unload there are lots of children leftover */
288 device_delete_children(dev);
290 if (sc->sc_irq_res && sc->sc_intr_hdl) {
292 * only call ehci_detach() after ehci_init()
296 err = bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intr_hdl);
299 /* XXX or should we panic? */
300 device_printf(dev, "Could not tear down irq, %d\n",
302 sc->sc_intr_hdl = NULL;
305 if (sc->sc_irq_res) {
306 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
307 sc->sc_irq_res = NULL;
310 bus_release_resource(dev, SYS_RES_MEMORY, 0,
312 sc->sc_io_res = NULL;
316 clk_release(isc->clk);
318 usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
319 free(isc->gpio_vbus, M_DEVBUF);
323 static device_method_t ehci_methods[] = {
324 /* Device interface */
325 DEVMETHOD(device_probe, jz4780_ehci_probe),
326 DEVMETHOD(device_attach, jz4780_ehci_attach),
327 DEVMETHOD(device_detach, jz4780_ehci_detach),
328 DEVMETHOD(device_suspend, bus_generic_suspend),
329 DEVMETHOD(device_resume, bus_generic_resume),
330 DEVMETHOD(device_shutdown, bus_generic_shutdown),
335 static driver_t ehci_driver = {
337 .methods = ehci_methods,
338 .size = sizeof(struct jz4780_ehci_softc),
341 static devclass_t ehci_devclass;
343 DRIVER_MODULE(ehci, simplebus, ehci_driver, ehci_devclass, 0, 0);
344 MODULE_DEPEND(ehci, usb, 1, 1, 1);
345 MODULE_DEPEND(ehci, gpio, 1, 1, 1);