17a58744fSEmmanuel Vadot /*- 27a58744fSEmmanuel Vadot * Copyright (c) 2012 Ganbold Tsagaankhuu <ganbold@freebsd.org> 37a58744fSEmmanuel Vadot * Copyright (c) 2016 The FreeBSD Foundation 47a58744fSEmmanuel Vadot * All rights reserved. 57a58744fSEmmanuel Vadot * 67a58744fSEmmanuel Vadot * This software was developed by Andrew Turner under 77a58744fSEmmanuel Vadot * sponsorship from the FreeBSD Foundation. 87a58744fSEmmanuel Vadot * 97a58744fSEmmanuel Vadot * Redistribution and use in source and binary forms, with or without 107a58744fSEmmanuel Vadot * modification, are permitted provided that the following conditions 117a58744fSEmmanuel Vadot * are met: 127a58744fSEmmanuel Vadot * 1. Redistributions of source code must retain the above copyright 137a58744fSEmmanuel Vadot * notice, this list of conditions and the following disclaimer. 147a58744fSEmmanuel Vadot * 2. Redistributions in binary form must reproduce the above copyright 157a58744fSEmmanuel Vadot * notice, this list of conditions and the following disclaimer in the 167a58744fSEmmanuel Vadot * documentation and/or other materials provided with the distribution. 177a58744fSEmmanuel Vadot * 187a58744fSEmmanuel Vadot * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 197a58744fSEmmanuel Vadot * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 207a58744fSEmmanuel Vadot * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 217a58744fSEmmanuel Vadot * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 227a58744fSEmmanuel Vadot * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 237a58744fSEmmanuel Vadot * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 247a58744fSEmmanuel Vadot * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 257a58744fSEmmanuel Vadot * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 267a58744fSEmmanuel Vadot * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 277a58744fSEmmanuel Vadot * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 287a58744fSEmmanuel Vadot * SUCH DAMAGE. 297a58744fSEmmanuel Vadot */ 307a58744fSEmmanuel Vadot 317a58744fSEmmanuel Vadot #include <sys/cdefs.h> 327a58744fSEmmanuel Vadot __FBSDID("$FreeBSD$"); 337a58744fSEmmanuel Vadot 347a58744fSEmmanuel Vadot #include "opt_bus.h" 357a58744fSEmmanuel Vadot 367a58744fSEmmanuel Vadot #include <sys/param.h> 377a58744fSEmmanuel Vadot #include <sys/systm.h> 387a58744fSEmmanuel Vadot #include <sys/bus.h> 397a58744fSEmmanuel Vadot #include <sys/condvar.h> 407a58744fSEmmanuel Vadot #include <sys/kernel.h> 417a58744fSEmmanuel Vadot #include <sys/module.h> 427a58744fSEmmanuel Vadot 437a58744fSEmmanuel Vadot #include <dev/usb/usb.h> 447a58744fSEmmanuel Vadot #include <dev/usb/usbdi.h> 457a58744fSEmmanuel Vadot 467a58744fSEmmanuel Vadot #include <dev/usb/usb_core.h> 477a58744fSEmmanuel Vadot #include <dev/usb/usb_busdma.h> 487a58744fSEmmanuel Vadot #include <dev/usb/usb_process.h> 497a58744fSEmmanuel Vadot 507a58744fSEmmanuel Vadot #include <dev/usb/usb_controller.h> 517a58744fSEmmanuel Vadot #include <dev/usb/usb_bus.h> 527a58744fSEmmanuel Vadot #include <dev/usb/controller/ehci.h> 537a58744fSEmmanuel Vadot 547a58744fSEmmanuel Vadot #include <dev/fdt/fdt_common.h> 557a58744fSEmmanuel Vadot #include <dev/ofw/openfirm.h> 567a58744fSEmmanuel Vadot #include <dev/ofw/ofw_bus.h> 577a58744fSEmmanuel Vadot #include <dev/ofw/ofw_bus_subr.h> 587a58744fSEmmanuel Vadot 597a58744fSEmmanuel Vadot #ifdef EXT_RESOURCES 607a58744fSEmmanuel Vadot #include <dev/extres/clk/clk.h> 617a58744fSEmmanuel Vadot #include <dev/extres/hwreset/hwreset.h> 627a58744fSEmmanuel Vadot #include <dev/extres/phy/phy.h> 637a58744fSEmmanuel Vadot #include <dev/extres/phy/phy_usb.h> 647a58744fSEmmanuel Vadot #endif 657a58744fSEmmanuel Vadot 667a58744fSEmmanuel Vadot #include "generic_ehci.h" 677a58744fSEmmanuel Vadot 687a58744fSEmmanuel Vadot #ifdef EXT_RESOURCES 697a58744fSEmmanuel Vadot struct clk_list { 707a58744fSEmmanuel Vadot TAILQ_ENTRY(clk_list) next; 717a58744fSEmmanuel Vadot clk_t clk; 727a58744fSEmmanuel Vadot }; 737a58744fSEmmanuel Vadot 747a58744fSEmmanuel Vadot 757a58744fSEmmanuel Vadot struct hwrst_list { 767a58744fSEmmanuel Vadot TAILQ_ENTRY(hwrst_list) next; 777a58744fSEmmanuel Vadot hwreset_t rst; 787a58744fSEmmanuel Vadot }; 797a58744fSEmmanuel Vadot 807a58744fSEmmanuel Vadot struct phy_list { 817a58744fSEmmanuel Vadot TAILQ_ENTRY(phy_list) next; 827a58744fSEmmanuel Vadot phy_t phy; 837a58744fSEmmanuel Vadot }; 847a58744fSEmmanuel Vadot #endif 857a58744fSEmmanuel Vadot 867a58744fSEmmanuel Vadot struct generic_ehci_fdt_softc { 877a58744fSEmmanuel Vadot ehci_softc_t ehci_sc; 887a58744fSEmmanuel Vadot 897a58744fSEmmanuel Vadot #ifdef EXT_RESOURCES 907a58744fSEmmanuel Vadot TAILQ_HEAD(, clk_list) clk_list; 917a58744fSEmmanuel Vadot TAILQ_HEAD(, hwrst_list) rst_list; 927a58744fSEmmanuel Vadot TAILQ_HEAD(, phy_list) phy_list; 937a58744fSEmmanuel Vadot #endif 947a58744fSEmmanuel Vadot }; 957a58744fSEmmanuel Vadot 967a58744fSEmmanuel Vadot static device_probe_t generic_ehci_fdt_probe; 977a58744fSEmmanuel Vadot static device_attach_t generic_ehci_fdt_attach; 987a58744fSEmmanuel Vadot static device_detach_t generic_ehci_fdt_detach; 997a58744fSEmmanuel Vadot 1007a58744fSEmmanuel Vadot static int 1017a58744fSEmmanuel Vadot generic_ehci_fdt_probe(device_t self) 1027a58744fSEmmanuel Vadot { 1037a58744fSEmmanuel Vadot 1047a58744fSEmmanuel Vadot if (!ofw_bus_status_okay(self)) 1057a58744fSEmmanuel Vadot return (ENXIO); 1067a58744fSEmmanuel Vadot 1077a58744fSEmmanuel Vadot if (!ofw_bus_is_compatible(self, "generic-ehci")) 1087a58744fSEmmanuel Vadot return (ENXIO); 1097a58744fSEmmanuel Vadot 1107a58744fSEmmanuel Vadot device_set_desc(self, "Generic EHCI Controller"); 1117a58744fSEmmanuel Vadot return (BUS_PROBE_DEFAULT); 1127a58744fSEmmanuel Vadot } 1137a58744fSEmmanuel Vadot 1147a58744fSEmmanuel Vadot static int 1157a58744fSEmmanuel Vadot generic_ehci_fdt_attach(device_t dev) 1167a58744fSEmmanuel Vadot { 117*306e46ebSEmmanuel Vadot int err; 1187a58744fSEmmanuel Vadot #ifdef EXT_RESOURCES 1197a58744fSEmmanuel Vadot struct generic_ehci_fdt_softc *sc; 1207a58744fSEmmanuel Vadot struct clk_list *clkp; 1217a58744fSEmmanuel Vadot clk_t clk; 1227a58744fSEmmanuel Vadot struct hwrst_list *rstp; 1237a58744fSEmmanuel Vadot hwreset_t rst; 1247a58744fSEmmanuel Vadot struct phy_list *phyp; 1257a58744fSEmmanuel Vadot phy_t phy; 126*306e46ebSEmmanuel Vadot int off; 1277a58744fSEmmanuel Vadot 1287a58744fSEmmanuel Vadot sc = device_get_softc(dev); 1297a58744fSEmmanuel Vadot 1307a58744fSEmmanuel Vadot TAILQ_INIT(&sc->clk_list); 1317a58744fSEmmanuel Vadot /* Enable clock */ 1327a58744fSEmmanuel Vadot for (off = 0; clk_get_by_ofw_index(dev, 0, off, &clk) == 0; off++) { 1337a58744fSEmmanuel Vadot err = clk_enable(clk); 1347a58744fSEmmanuel Vadot if (err != 0) { 1357a58744fSEmmanuel Vadot device_printf(dev, "Could not enable clock %s\n", 1367a58744fSEmmanuel Vadot clk_get_name(clk)); 1377a58744fSEmmanuel Vadot goto error; 1387a58744fSEmmanuel Vadot } 1397a58744fSEmmanuel Vadot clkp = malloc(sizeof(*clkp), M_DEVBUF, M_WAITOK | M_ZERO); 1407a58744fSEmmanuel Vadot clkp->clk = clk; 1417a58744fSEmmanuel Vadot TAILQ_INSERT_TAIL(&sc->clk_list, clkp, next); 1427a58744fSEmmanuel Vadot } 1437a58744fSEmmanuel Vadot 1447a58744fSEmmanuel Vadot /* De-assert reset */ 1457a58744fSEmmanuel Vadot TAILQ_INIT(&sc->rst_list); 1467a58744fSEmmanuel Vadot for (off = 0; hwreset_get_by_ofw_idx(dev, 0, off, &rst) == 0; off++) { 1477a58744fSEmmanuel Vadot err = hwreset_deassert(rst); 1487a58744fSEmmanuel Vadot if (err != 0) { 1497a58744fSEmmanuel Vadot device_printf(dev, "Could not de-assert reset\n"); 1507a58744fSEmmanuel Vadot goto error; 1517a58744fSEmmanuel Vadot } 1527a58744fSEmmanuel Vadot rstp = malloc(sizeof(*rstp), M_DEVBUF, M_WAITOK | M_ZERO); 1537a58744fSEmmanuel Vadot rstp->rst = rst; 1547a58744fSEmmanuel Vadot TAILQ_INSERT_TAIL(&sc->rst_list, rstp, next); 1557a58744fSEmmanuel Vadot } 1567a58744fSEmmanuel Vadot 1577a58744fSEmmanuel Vadot /* Enable USB PHY */ 1587a58744fSEmmanuel Vadot TAILQ_INIT(&sc->phy_list); 1597a58744fSEmmanuel Vadot for (off = 0; phy_get_by_ofw_idx(dev, 0, off, &phy) == 0; off++) { 1607a58744fSEmmanuel Vadot err = phy_usb_set_mode(phy, PHY_USB_MODE_HOST); 1617a58744fSEmmanuel Vadot if (err != 0) { 1627a58744fSEmmanuel Vadot device_printf(dev, "Could not set phy to host mode\n"); 1637a58744fSEmmanuel Vadot goto error; 1647a58744fSEmmanuel Vadot } 1657a58744fSEmmanuel Vadot err = phy_enable(phy); 1667a58744fSEmmanuel Vadot if (err != 0) { 1677a58744fSEmmanuel Vadot device_printf(dev, "Could not enable phy\n"); 1687a58744fSEmmanuel Vadot goto error; 1697a58744fSEmmanuel Vadot } 1707a58744fSEmmanuel Vadot phyp = malloc(sizeof(*phyp), M_DEVBUF, M_WAITOK | M_ZERO); 1717a58744fSEmmanuel Vadot phyp->phy = phy; 1727a58744fSEmmanuel Vadot TAILQ_INSERT_TAIL(&sc->phy_list, phyp, next); 1737a58744fSEmmanuel Vadot } 1747a58744fSEmmanuel Vadot #endif 1757a58744fSEmmanuel Vadot 1767a58744fSEmmanuel Vadot err = generic_ehci_attach(dev); 1777a58744fSEmmanuel Vadot if (err != 0) 1787a58744fSEmmanuel Vadot goto error; 1797a58744fSEmmanuel Vadot 1807a58744fSEmmanuel Vadot return (0); 1817a58744fSEmmanuel Vadot 1827a58744fSEmmanuel Vadot error: 1837a58744fSEmmanuel Vadot generic_ehci_fdt_detach(dev); 1847a58744fSEmmanuel Vadot return (err); 1857a58744fSEmmanuel Vadot } 1867a58744fSEmmanuel Vadot 1877a58744fSEmmanuel Vadot static int 1887a58744fSEmmanuel Vadot generic_ehci_fdt_detach(device_t dev) 1897a58744fSEmmanuel Vadot { 1907a58744fSEmmanuel Vadot #ifdef EXT_RESOURCES 1917a58744fSEmmanuel Vadot struct generic_ehci_fdt_softc *sc; 1927a58744fSEmmanuel Vadot struct clk_list *clk, *clk_tmp; 1937a58744fSEmmanuel Vadot struct hwrst_list *rst, *rst_tmp; 1947a58744fSEmmanuel Vadot struct phy_list *phy, *phy_tmp; 1957a58744fSEmmanuel Vadot #endif 1967a58744fSEmmanuel Vadot int err; 1977a58744fSEmmanuel Vadot 1987a58744fSEmmanuel Vadot err = generic_ehci_detach(dev); 1997a58744fSEmmanuel Vadot if (err != 0) 2007a58744fSEmmanuel Vadot return (err); 2017a58744fSEmmanuel Vadot 2027a58744fSEmmanuel Vadot #ifdef EXT_RESOURCES 2037a58744fSEmmanuel Vadot sc = device_get_softc(dev); 2047a58744fSEmmanuel Vadot 2057a58744fSEmmanuel Vadot /* Disable clock */ 2067a58744fSEmmanuel Vadot TAILQ_FOREACH_SAFE(clk, &sc->clk_list, next, clk_tmp) { 2077a58744fSEmmanuel Vadot err = clk_disable(clk->clk); 2087a58744fSEmmanuel Vadot if (err != 0) 2097a58744fSEmmanuel Vadot device_printf(dev, "Could not disable clock %s\n", 2107a58744fSEmmanuel Vadot clk_get_name(clk->clk)); 2117a58744fSEmmanuel Vadot err = clk_release(clk->clk); 2127a58744fSEmmanuel Vadot if (err != 0) 2137a58744fSEmmanuel Vadot device_printf(dev, "Could not release clock %s\n", 2147a58744fSEmmanuel Vadot clk_get_name(clk->clk)); 2157a58744fSEmmanuel Vadot TAILQ_REMOVE(&sc->clk_list, clk, next); 2167a58744fSEmmanuel Vadot free(clk, M_DEVBUF); 2177a58744fSEmmanuel Vadot } 2187a58744fSEmmanuel Vadot 2197a58744fSEmmanuel Vadot /* Assert reset */ 2207a58744fSEmmanuel Vadot TAILQ_FOREACH_SAFE(rst, &sc->rst_list, next, rst_tmp) { 2217a58744fSEmmanuel Vadot hwreset_assert(rst->rst); 2227a58744fSEmmanuel Vadot hwreset_release(rst->rst); 2237a58744fSEmmanuel Vadot TAILQ_REMOVE(&sc->rst_list, rst, next); 2247a58744fSEmmanuel Vadot free(rst, M_DEVBUF); 2257a58744fSEmmanuel Vadot } 2267a58744fSEmmanuel Vadot 2277a58744fSEmmanuel Vadot /* Disable phys */ 2287a58744fSEmmanuel Vadot TAILQ_FOREACH_SAFE(phy, &sc->phy_list, next, phy_tmp) { 2297a58744fSEmmanuel Vadot err = phy_disable(phy->phy); 2307a58744fSEmmanuel Vadot if (err != 0) 2317a58744fSEmmanuel Vadot device_printf(dev, "Could not disable phy\n"); 2327a58744fSEmmanuel Vadot phy_release(phy->phy); 2337a58744fSEmmanuel Vadot TAILQ_REMOVE(&sc->phy_list, phy, next); 2347a58744fSEmmanuel Vadot free(phy, M_DEVBUF); 2357a58744fSEmmanuel Vadot } 2367a58744fSEmmanuel Vadot #endif 2377a58744fSEmmanuel Vadot 2387a58744fSEmmanuel Vadot return (0); 2397a58744fSEmmanuel Vadot } 2407a58744fSEmmanuel Vadot 2417a58744fSEmmanuel Vadot static device_method_t ehci_fdt_methods[] = { 2427a58744fSEmmanuel Vadot /* Device interface */ 2437a58744fSEmmanuel Vadot DEVMETHOD(device_probe, generic_ehci_fdt_probe), 2447a58744fSEmmanuel Vadot DEVMETHOD(device_attach, generic_ehci_fdt_attach), 2457a58744fSEmmanuel Vadot DEVMETHOD(device_detach, generic_ehci_fdt_detach), 2467a58744fSEmmanuel Vadot 2477a58744fSEmmanuel Vadot DEVMETHOD_END 2487a58744fSEmmanuel Vadot }; 2497a58744fSEmmanuel Vadot 2507a58744fSEmmanuel Vadot DEFINE_CLASS_1(ehci, ehci_fdt_driver, ehci_fdt_methods, 2517a58744fSEmmanuel Vadot sizeof(ehci_softc_t), generic_ehci_driver); 2527a58744fSEmmanuel Vadot 2537a58744fSEmmanuel Vadot static devclass_t ehci_fdt_devclass; 2547a58744fSEmmanuel Vadot 2557a58744fSEmmanuel Vadot DRIVER_MODULE(ehci, simplebus, ehci_fdt_driver, ehci_fdt_devclass, 0, 0); 2567a58744fSEmmanuel Vadot MODULE_DEPEND(ehci, usb, 1, 1, 1); 257