2 * Copyright (c) 2012 Ganbold Tsagaankhuu <ganbold@freebsd.org>
3 * Copyright (c) 2016 The FreeBSD Foundation
6 * This software was developed by Andrew Turner under
7 * sponsorship from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
39 #include <sys/condvar.h>
40 #include <sys/kernel.h>
41 #include <sys/module.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>
50 #include <dev/usb/usb_controller.h>
51 #include <dev/usb/usb_bus.h>
52 #include <dev/usb/controller/ehci.h>
54 #include <dev/fdt/fdt_common.h>
55 #include <dev/ofw/openfirm.h>
56 #include <dev/ofw/ofw_bus.h>
57 #include <dev/ofw/ofw_bus_subr.h>
60 #include <dev/extres/clk/clk.h>
61 #include <dev/extres/hwreset/hwreset.h>
62 #include <dev/extres/phy/phy.h>
63 #include <dev/extres/phy/phy_usb.h>
66 #include "generic_ehci.h"
70 TAILQ_ENTRY(clk_list) next;
76 TAILQ_ENTRY(hwrst_list) next;
81 TAILQ_ENTRY(phy_list) next;
86 struct generic_ehci_fdt_softc {
90 TAILQ_HEAD(, clk_list) clk_list;
91 TAILQ_HEAD(, hwrst_list) rst_list;
92 TAILQ_HEAD(, phy_list) phy_list;
96 static device_probe_t generic_ehci_fdt_probe;
97 static device_attach_t generic_ehci_fdt_attach;
98 static device_detach_t generic_ehci_fdt_detach;
101 generic_ehci_fdt_probe(device_t self)
104 if (!ofw_bus_status_okay(self))
107 if (!ofw_bus_is_compatible(self, "generic-ehci"))
110 device_set_desc(self, "Generic EHCI Controller");
111 return (BUS_PROBE_DEFAULT);
115 generic_ehci_fdt_attach(device_t dev)
119 struct generic_ehci_fdt_softc *sc;
120 struct clk_list *clkp;
122 struct hwrst_list *rstp;
124 struct phy_list *phyp;
128 sc = device_get_softc(dev);
130 TAILQ_INIT(&sc->clk_list);
132 for (off = 0; clk_get_by_ofw_index(dev, 0, off, &clk) == 0; off++) {
133 err = clk_enable(clk);
135 device_printf(dev, "Could not enable clock %s\n",
139 clkp = malloc(sizeof(*clkp), M_DEVBUF, M_WAITOK | M_ZERO);
141 TAILQ_INSERT_TAIL(&sc->clk_list, clkp, next);
144 /* De-assert reset */
145 TAILQ_INIT(&sc->rst_list);
146 for (off = 0; hwreset_get_by_ofw_idx(dev, 0, off, &rst) == 0; off++) {
147 err = hwreset_deassert(rst);
149 device_printf(dev, "Could not de-assert reset\n");
152 rstp = malloc(sizeof(*rstp), M_DEVBUF, M_WAITOK | M_ZERO);
154 TAILQ_INSERT_TAIL(&sc->rst_list, rstp, next);
158 TAILQ_INIT(&sc->phy_list);
159 for (off = 0; phy_get_by_ofw_idx(dev, 0, off, &phy) == 0; off++) {
160 err = phy_usb_set_mode(phy, PHY_USB_MODE_HOST);
162 device_printf(dev, "Could not set phy to host mode\n");
165 err = phy_enable(phy);
167 device_printf(dev, "Could not enable phy\n");
170 phyp = malloc(sizeof(*phyp), M_DEVBUF, M_WAITOK | M_ZERO);
172 TAILQ_INSERT_TAIL(&sc->phy_list, phyp, next);
176 err = generic_ehci_attach(dev);
183 generic_ehci_fdt_detach(dev);
188 generic_ehci_fdt_detach(device_t dev)
191 struct generic_ehci_fdt_softc *sc;
192 struct clk_list *clk, *clk_tmp;
193 struct hwrst_list *rst, *rst_tmp;
194 struct phy_list *phy, *phy_tmp;
198 err = generic_ehci_detach(dev);
203 sc = device_get_softc(dev);
206 TAILQ_FOREACH_SAFE(clk, &sc->clk_list, next, clk_tmp) {
207 err = clk_disable(clk->clk);
209 device_printf(dev, "Could not disable clock %s\n",
210 clk_get_name(clk->clk));
211 err = clk_release(clk->clk);
213 device_printf(dev, "Could not release clock %s\n",
214 clk_get_name(clk->clk));
215 TAILQ_REMOVE(&sc->clk_list, clk, next);
220 TAILQ_FOREACH_SAFE(rst, &sc->rst_list, next, rst_tmp) {
221 hwreset_assert(rst->rst);
222 hwreset_release(rst->rst);
223 TAILQ_REMOVE(&sc->rst_list, rst, next);
228 TAILQ_FOREACH_SAFE(phy, &sc->phy_list, next, phy_tmp) {
229 err = phy_disable(phy->phy);
231 device_printf(dev, "Could not disable phy\n");
232 phy_release(phy->phy);
233 TAILQ_REMOVE(&sc->phy_list, phy, next);
241 static device_method_t ehci_fdt_methods[] = {
242 /* Device interface */
243 DEVMETHOD(device_probe, generic_ehci_fdt_probe),
244 DEVMETHOD(device_attach, generic_ehci_fdt_attach),
245 DEVMETHOD(device_detach, generic_ehci_fdt_detach),
250 DEFINE_CLASS_1(ehci, ehci_fdt_driver, ehci_fdt_methods,
251 sizeof(ehci_softc_t), generic_ehci_driver);
253 static devclass_t ehci_fdt_devclass;
255 DRIVER_MODULE(ehci, simplebus, ehci_fdt_driver, ehci_fdt_devclass, 0, 0);
256 MODULE_DEPEND(ehci, usb, 1, 1, 1);