11.2Sthorpej/* $NetBSD: sun9i_a80_usbphy.c,v 1.2 2021/01/27 03:10:20 thorpej Exp $ */ 21.1Sjmcneill 31.1Sjmcneill/*- 41.1Sjmcneill * Copyright (c) 2019 Jared McNeill <jmcneill@invisible.ca> 51.1Sjmcneill * All rights reserved. 61.1Sjmcneill * 71.1Sjmcneill * Redistribution and use in source and binary forms, with or without 81.1Sjmcneill * modification, are permitted provided that the following conditions 91.1Sjmcneill * are met: 101.1Sjmcneill * 1. Redistributions of source code must retain the above copyright 111.1Sjmcneill * notice, this list of conditions and the following disclaimer. 121.1Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright 131.1Sjmcneill * notice, this list of conditions and the following disclaimer in the 141.1Sjmcneill * documentation and/or other materials provided with the distribution. 151.1Sjmcneill * 161.1Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 171.1Sjmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 181.1Sjmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 191.1Sjmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 201.1Sjmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 211.1Sjmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 221.1Sjmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 231.1Sjmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 241.1Sjmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 251.1Sjmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 261.1Sjmcneill * POSSIBILITY OF SUCH DAMAGE. 271.1Sjmcneill */ 281.1Sjmcneill 291.1Sjmcneill#include <sys/cdefs.h> 301.1Sjmcneill 311.2Sthorpej__KERNEL_RCSID(0, "$NetBSD: sun9i_a80_usbphy.c,v 1.2 2021/01/27 03:10:20 thorpej Exp $"); 321.1Sjmcneill 331.1Sjmcneill#include <sys/param.h> 341.1Sjmcneill#include <sys/bus.h> 351.1Sjmcneill#include <sys/device.h> 361.1Sjmcneill#include <sys/intr.h> 371.1Sjmcneill#include <sys/systm.h> 381.1Sjmcneill#include <sys/time.h> 391.1Sjmcneill 401.1Sjmcneill#include <dev/fdt/fdtvar.h> 411.1Sjmcneill 421.1Sjmcneill/* PMU registers */ 431.1Sjmcneill#define PMU_CFG 0x00 441.1Sjmcneill#define EHCI_HS_FORCE __BIT(20) 451.1Sjmcneill#define HSIC_CONNECT_DET __BIT(17) 461.1Sjmcneill#define HSIC_CONNECT_INT __BIT(16) 471.1Sjmcneill#define AHB_INCR16 __BIT(11) 481.1Sjmcneill#define AHB_INCR8 __BIT(10) 491.1Sjmcneill#define AHB_INCR4 __BIT(9) 501.1Sjmcneill#define AHB_INCRX_ALIGN __BIT(8) 511.1Sjmcneill#define HSIC __BIT(1) 521.1Sjmcneill#define ULPI_BYPASS __BIT(0) 531.1Sjmcneill 541.1Sjmcneillstatic int sun9i_usbphy_match(device_t, cfdata_t, void *); 551.1Sjmcneillstatic void sun9i_usbphy_attach(device_t, device_t, void *); 561.1Sjmcneill 571.2Sthorpejstatic const struct device_compatible_entry compat_data[] = { 581.2Sthorpej { .compat = "allwinner,sun9i-a80-usb-phy" }, 591.2Sthorpej DEVICE_COMPAT_EOL 601.1Sjmcneill}; 611.1Sjmcneill 621.1Sjmcneillstruct sun9i_usbphy_softc { 631.1Sjmcneill device_t sc_dev; 641.1Sjmcneill bus_space_tag_t sc_bst; 651.1Sjmcneill bus_space_handle_t sc_bsh; 661.1Sjmcneill 671.1Sjmcneill struct clk *sc_clk_phy; 681.1Sjmcneill struct clk *sc_clk_hsic; 691.1Sjmcneill struct fdtbus_reset *sc_rst; 701.1Sjmcneill 711.1Sjmcneill struct fdtbus_regulator *sc_supply; 721.1Sjmcneill}; 731.1Sjmcneill 741.1Sjmcneill#define PHY_READ(sc, reg) \ 751.1Sjmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg)) 761.1Sjmcneill#define PHY_WRITE(sc, reg, val) \ 771.1Sjmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val)) 781.1Sjmcneill 791.1SjmcneillCFATTACH_DECL_NEW(sunxi_a80_usbphy, sizeof(struct sun9i_usbphy_softc), 801.1Sjmcneill sun9i_usbphy_match, sun9i_usbphy_attach, NULL, NULL); 811.1Sjmcneill 821.1Sjmcneillstatic void * 831.1Sjmcneillsun9i_usbphy_acquire(device_t dev, const void *data, size_t len) 841.1Sjmcneill{ 851.1Sjmcneill struct sun9i_usbphy_softc * const sc = device_private(dev); 861.1Sjmcneill 871.1Sjmcneill return sc; 881.1Sjmcneill} 891.1Sjmcneill 901.1Sjmcneillstatic void 911.1Sjmcneillsun9i_usbphy_release(device_t dev, void *priv) 921.1Sjmcneill{ 931.1Sjmcneill} 941.1Sjmcneill 951.1Sjmcneillstatic int 961.1Sjmcneillsun9i_usbphy_enable(device_t dev, void *priv, bool enable) 971.1Sjmcneill{ 981.1Sjmcneill struct sun9i_usbphy_softc * const sc = device_private(dev); 991.1Sjmcneill uint32_t passby_mask; 1001.1Sjmcneill uint32_t val; 1011.1Sjmcneill int error; 1021.1Sjmcneill 1031.1Sjmcneill passby_mask = ULPI_BYPASS|AHB_INCR16|AHB_INCR8|AHB_INCR4|AHB_INCRX_ALIGN; 1041.1Sjmcneill if (sc->sc_clk_hsic != NULL) 1051.1Sjmcneill passby_mask |= HSIC|EHCI_HS_FORCE|HSIC_CONNECT_DET|HSIC_CONNECT_INT; 1061.1Sjmcneill 1071.1Sjmcneill /* Enable/disable passby */ 1081.1Sjmcneill if (enable) { 1091.1Sjmcneill error = clk_enable(sc->sc_clk_phy); 1101.1Sjmcneill if (error != 0) 1111.1Sjmcneill return error; 1121.1Sjmcneill 1131.1Sjmcneill if (sc->sc_clk_hsic != NULL) { 1141.1Sjmcneill error = clk_enable(sc->sc_clk_hsic); 1151.1Sjmcneill if (error != 0) 1161.1Sjmcneill return error; 1171.1Sjmcneill } 1181.1Sjmcneill 1191.1Sjmcneill error = fdtbus_reset_deassert(sc->sc_rst); 1201.1Sjmcneill if (error != 0) 1211.1Sjmcneill return error; 1221.1Sjmcneill 1231.1Sjmcneill val = PHY_READ(sc, PMU_CFG); 1241.1Sjmcneill val |= passby_mask; 1251.1Sjmcneill PHY_WRITE(sc, PMU_CFG, val); 1261.1Sjmcneill } else { 1271.1Sjmcneill val = PHY_READ(sc, PMU_CFG); 1281.1Sjmcneill val &= ~passby_mask; 1291.1Sjmcneill PHY_WRITE(sc, PMU_CFG, val); 1301.1Sjmcneill 1311.1Sjmcneill error = fdtbus_reset_assert(sc->sc_rst); 1321.1Sjmcneill if (error != 0) 1331.1Sjmcneill return error; 1341.1Sjmcneill 1351.1Sjmcneill if (sc->sc_clk_hsic != NULL) { 1361.1Sjmcneill error = clk_disable(sc->sc_clk_hsic); 1371.1Sjmcneill if (error != 0) 1381.1Sjmcneill return error; 1391.1Sjmcneill } 1401.1Sjmcneill 1411.1Sjmcneill error = clk_disable(sc->sc_clk_phy); 1421.1Sjmcneill if (error != 0) 1431.1Sjmcneill return error; 1441.1Sjmcneill } 1451.1Sjmcneill 1461.1Sjmcneill return 0; 1471.1Sjmcneill} 1481.1Sjmcneill 1491.1Sjmcneillconst struct fdtbus_phy_controller_func sun9i_usbphy_funcs = { 1501.1Sjmcneill .acquire = sun9i_usbphy_acquire, 1511.1Sjmcneill .release = sun9i_usbphy_release, 1521.1Sjmcneill .enable = sun9i_usbphy_enable, 1531.1Sjmcneill}; 1541.1Sjmcneill 1551.1Sjmcneillstatic int 1561.1Sjmcneillsun9i_usbphy_match(device_t parent, cfdata_t cf, void *aux) 1571.1Sjmcneill{ 1581.1Sjmcneill struct fdt_attach_args * const faa = aux; 1591.1Sjmcneill 1601.2Sthorpej return of_compatible_match(faa->faa_phandle, compat_data); 1611.1Sjmcneill} 1621.1Sjmcneill 1631.1Sjmcneillstatic void 1641.1Sjmcneillsun9i_usbphy_attach(device_t parent, device_t self, void *aux) 1651.1Sjmcneill{ 1661.1Sjmcneill struct sun9i_usbphy_softc * const sc = device_private(self); 1671.1Sjmcneill struct fdt_attach_args * const faa = aux; 1681.1Sjmcneill const int phandle = faa->faa_phandle; 1691.1Sjmcneill const char *phy_type; 1701.1Sjmcneill bus_addr_t addr; 1711.1Sjmcneill bus_size_t size; 1721.1Sjmcneill 1731.1Sjmcneill sc->sc_dev = self; 1741.1Sjmcneill sc->sc_bst = faa->faa_bst; 1751.1Sjmcneill 1761.1Sjmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 1771.1Sjmcneill aprint_error(": couldn't get registers\n"); 1781.1Sjmcneill return; 1791.1Sjmcneill } 1801.1Sjmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) { 1811.1Sjmcneill aprint_error(": couldn't map registers\n"); 1821.1Sjmcneill return; 1831.1Sjmcneill } 1841.1Sjmcneill 1851.1Sjmcneill phy_type = fdtbus_get_string(phandle, "phy_type"); 1861.1Sjmcneill if (phy_type && strcmp(phy_type, "hsic") == 0) { 1871.1Sjmcneill sc->sc_clk_phy = fdtbus_clock_get(phandle, "hsic_480M"); 1881.1Sjmcneill sc->sc_clk_hsic = fdtbus_clock_get(phandle, "hsic_12M"); 1891.1Sjmcneill sc->sc_rst = fdtbus_reset_get(phandle, "hsic"); 1901.1Sjmcneill 1911.1Sjmcneill if (sc->sc_clk_phy == NULL || sc->sc_clk_hsic == NULL || sc->sc_rst == NULL) { 1921.1Sjmcneill aprint_error(": couldn't get hsic resources\n"); 1931.1Sjmcneill return; 1941.1Sjmcneill } 1951.1Sjmcneill } else { 1961.1Sjmcneill sc->sc_clk_phy = fdtbus_clock_get(phandle, "phy"); 1971.1Sjmcneill sc->sc_rst = fdtbus_reset_get(phandle, "phy"); 1981.1Sjmcneill if (sc->sc_clk_phy == NULL || sc->sc_rst == NULL) { 1991.1Sjmcneill aprint_error(": couldn't get phy resources\n"); 2001.1Sjmcneill return; 2011.1Sjmcneill } 2021.1Sjmcneill } 2031.1Sjmcneill 2041.1Sjmcneill aprint_naive("\n"); 2051.1Sjmcneill aprint_normal(": USB PHY\n"); 2061.1Sjmcneill 2071.1Sjmcneill sc->sc_supply = fdtbus_regulator_acquire(phandle, "phy-supply"); 2081.1Sjmcneill if (sc->sc_supply != NULL) { 2091.1Sjmcneill if (fdtbus_regulator_enable(sc->sc_supply) != 0) 2101.1Sjmcneill aprint_error_dev(self, "WARNING: couldn't enable power supply\n"); 2111.1Sjmcneill } 2121.1Sjmcneill 2131.1Sjmcneill fdtbus_register_phy_controller(self, phandle, &sun9i_usbphy_funcs); 2141.1Sjmcneill} 215