2 * Copyright 2015 Alexander Kabaev <kan@FreeBSD.org>.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/kernel.h>
34 #include <sys/callout.h>
35 #include <sys/condvar.h>
36 #include <sys/module.h>
38 #include <dev/extres/clk/clk.h>
40 #include <dev/ofw/ofw_bus_subr.h>
42 #include <dev/usb/usb.h>
43 #include <dev/usb/usbdi.h>
45 #include <dev/usb/usb_busdma.h>
46 #include <dev/usb/usb_process.h>
48 #include <dev/usb/usb_controller.h>
49 #include <dev/usb/usb_bus.h>
51 #include <dev/usb/controller/dwc_otg.h>
52 #include <dev/usb/controller/dwc_otg_fdt.h>
54 #include <mips/ingenic/jz4780_clock.h>
55 #include <mips/ingenic/jz4780_regs.h>
57 static device_probe_t jz4780_dwc_otg_probe;
58 static device_attach_t jz4780_dwc_otg_attach;
59 static device_detach_t jz4780_dwc_otg_detach;
61 struct jz4780_dwc_otg_softc {
62 struct dwc_otg_fdt_softc base; /* storage for DWC OTG code */
68 jz4780_dwc_otg_clk_enable(device_t dev)
70 struct jz4780_dwc_otg_softc *sc;
73 sc = device_get_softc(dev);
75 /* Configure and enable phy clock */
76 err = clk_get_by_ofw_name(dev, 0, "otg_phy", &sc->phy_clk);
78 device_printf(dev, "unable to lookup %s clock\n", "otg_phy");
81 err = clk_set_freq(sc->phy_clk, 48000000, 0);
83 device_printf(dev, "unable to set %s clock to 48 kHZ\n",
87 err = clk_enable(sc->phy_clk);
89 device_printf(dev, "unable to enable %s clock\n", "otg_phy");
93 /* Configure and enable otg1 clock */
94 err = clk_get_by_ofw_name(dev, 0, "otg1", &sc->otg_clk);
96 device_printf(dev, "unable to lookup %s clock\n", "otg1");
99 err = clk_enable(sc->phy_clk);
101 device_printf(dev, "unable to enable %s clock\n", "otg1");
109 jz4780_dwc_otg_probe(device_t dev)
112 if (!ofw_bus_status_okay(dev))
115 if (!ofw_bus_is_compatible(dev, "ingenic,jz4780-otg"))
118 device_set_desc(dev, "DWC OTG 2.0 integrated USB controller (jz4780)");
120 return (BUS_PROBE_VENDOR);
124 jz4780_dwc_otg_attach(device_t dev)
126 struct jz4780_dwc_otg_softc *sc;
127 struct resource *res;
130 sc = device_get_softc(dev);
132 err = jz4780_dwc_otg_clk_enable(dev);
136 err = jz4780_otg_enable();
138 device_printf(dev, "CGU failed to enable OTG\n");
142 /* Voodoo: Switch off VBUS overcurrent detection in OTG PHY */
143 res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
147 reg = bus_read_4(res, JZ_DWC2_GUSBCFG);
149 bus_write_4(res, JZ_DWC2_GUSBCFG, reg);
150 bus_release_resource(dev, SYS_RES_MEMORY, rid, res);
153 sc->base.sc_otg.sc_phy_type = DWC_OTG_PHY_UTMI;
154 sc->base.sc_otg.sc_phy_bits = 16;
156 err = dwc_otg_attach(dev);
163 clk_release(sc->otg_clk);
165 clk_release(sc->phy_clk);
170 jz4780_dwc_otg_detach(device_t dev)
172 struct jz4780_dwc_otg_softc *sc;
175 err = dwc_otg_detach(dev);
179 sc = device_get_softc(dev);
181 clk_release(sc->otg_clk);
183 clk_release(sc->phy_clk);
187 static device_method_t jz4780_dwc_otg_methods[] = {
189 DEVMETHOD(device_probe, jz4780_dwc_otg_probe),
190 DEVMETHOD(device_attach, jz4780_dwc_otg_attach),
191 DEVMETHOD(device_detach, jz4780_dwc_otg_detach),
196 static devclass_t jz4780_dwc_otg_devclass;
198 DEFINE_CLASS_1(jzotg, jz4780_dwc_otg_driver, jz4780_dwc_otg_methods,
199 sizeof(struct jz4780_dwc_otg_softc), dwc_otg_driver);
200 DRIVER_MODULE(jzotg, simplebus, jz4780_dwc_otg_driver,
201 jz4780_dwc_otg_devclass, 0, 0);
202 MODULE_DEPEND(jzotg, usb, 1, 1, 1);