sunxi_usb3phy.c revision 1.5
11.5Sthorpej/* $NetBSD: sunxi_usb3phy.c,v 1.5 2021/01/27 03:10:20 thorpej Exp $ */ 21.1Sjmcneill 31.1Sjmcneill/*- 41.1Sjmcneill * Copyright (c) 2018 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.5Sthorpej__KERNEL_RCSID(0, "$NetBSD: sunxi_usb3phy.c,v 1.5 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#define SUNXI_APP 0x00 431.1Sjmcneill#define APP_FORCE_VBUS __BITS(13,12) 441.1Sjmcneill 451.1Sjmcneill#define SUNXI_PIPE_CLOCK_CONTROL 0x14 461.1Sjmcneill#define PCC_PIPE_CLK_OPEN __BIT(6) 471.1Sjmcneill 481.1Sjmcneill#define SUNXI_PHY_TUNE_LOW 0x18 491.1Sjmcneill#define PTL_MAGIC 0x0047fc87 501.1Sjmcneill 511.1Sjmcneill#define SUNXI_PHY_TUNE_HIGH 0x1c 521.1Sjmcneill#define PTH_TX_DEEMPH_3P5DB __BITS(24,19) 531.1Sjmcneill#define PTH_TX_DEEMPH_6DB __BITS(18,13) 541.1Sjmcneill#define PTH_TX_SWING_FULL __BITS(12,6) 551.1Sjmcneill#define PTH_LOS_BIAS __BITS(5,3) 561.1Sjmcneill#define PTH_TX_BOOST_LVL __BITS(2,0) 571.1Sjmcneill 581.1Sjmcneill#define SUNXI_PHY_EXTERNAL_CONTROL 0x20 591.1Sjmcneill#define PEC_REF_SSP_EN __BIT(26) 601.1Sjmcneill#define PEC_SSC_EN __BIT(24) 611.1Sjmcneill#define PEC_EXTERN_VBUS __BITS(2,1) 621.1Sjmcneill 631.1Sjmcneillstatic int sunxi_usb3phy_match(device_t, cfdata_t, void *); 641.1Sjmcneillstatic void sunxi_usb3phy_attach(device_t, device_t, void *); 651.1Sjmcneill 661.1Sjmcneillenum sunxi_usb3phy_type { 671.1Sjmcneill USB3PHY_H6 = 1, 681.1Sjmcneill}; 691.1Sjmcneill 701.2Sthorpejstatic const struct device_compatible_entry compat_data[] = { 711.2Sthorpej { .compat = "allwinner,sun50i-h6-usb3-phy", .value = USB3PHY_H6 }, 721.4Sthorpej DEVICE_COMPAT_EOL 731.1Sjmcneill}; 741.1Sjmcneill 751.1Sjmcneillstruct sunxi_usb3phy { 761.1Sjmcneill bus_space_tag_t phy_bst; 771.1Sjmcneill bus_space_handle_t phy_bsh; 781.1Sjmcneill struct fdtbus_regulator *phy_reg; 791.1Sjmcneill}; 801.1Sjmcneill 811.1Sjmcneillstruct sunxi_usb3phy_softc { 821.1Sjmcneill device_t sc_dev; 831.1Sjmcneill enum sunxi_usb3phy_type sc_type; 841.1Sjmcneill 851.1Sjmcneill struct sunxi_usb3phy sc_phy; 861.1Sjmcneill 871.1Sjmcneill struct fdtbus_gpio_pin *sc_gpio_id_det; 881.1Sjmcneill struct fdtbus_gpio_pin *sc_gpio_vbus_det; 891.1Sjmcneill}; 901.1Sjmcneill 911.1Sjmcneill#define PHY_READ(phy, reg) \ 921.1Sjmcneill bus_space_read_4((phy)->phy_bst, (phy)->phy_bsh, (reg)) 931.1Sjmcneill#define PHY_WRITE(phy, reg, val) \ 941.1Sjmcneill bus_space_write_4((phy)->phy_bst, (phy)->phy_bsh, (reg), (val)) 951.1Sjmcneill 961.1SjmcneillCFATTACH_DECL_NEW(sunxi_usb3phy, sizeof(struct sunxi_usb3phy_softc), 971.1Sjmcneill sunxi_usb3phy_match, sunxi_usb3phy_attach, NULL, NULL); 981.1Sjmcneill 991.1Sjmcneillstatic void * 1001.1Sjmcneillsunxi_usb3phy_acquire(device_t dev, const void *data, size_t len) 1011.1Sjmcneill{ 1021.1Sjmcneill struct sunxi_usb3phy_softc * const sc = device_private(dev); 1031.1Sjmcneill 1041.1Sjmcneill return &sc->sc_phy; 1051.1Sjmcneill} 1061.1Sjmcneill 1071.1Sjmcneillstatic void 1081.1Sjmcneillsunxi_usb3phy_release(device_t dev, void *priv) 1091.1Sjmcneill{ 1101.1Sjmcneill} 1111.1Sjmcneill 1121.1Sjmcneillstatic int 1131.1Sjmcneillsunxi_usb3phy_enable(device_t dev, void *priv, bool enable) 1141.1Sjmcneill{ 1151.1Sjmcneill struct sunxi_usb3phy * const phy = priv; 1161.1Sjmcneill uint32_t val; 1171.1Sjmcneill 1181.1Sjmcneill if (enable) { 1191.1Sjmcneill val = PHY_READ(phy, SUNXI_PHY_EXTERNAL_CONTROL); 1201.1Sjmcneill val |= PEC_EXTERN_VBUS; 1211.1Sjmcneill val |= PEC_SSC_EN; 1221.1Sjmcneill val |= PEC_REF_SSP_EN; 1231.1Sjmcneill PHY_WRITE(phy, SUNXI_PHY_EXTERNAL_CONTROL, val); 1241.1Sjmcneill 1251.1Sjmcneill val = PHY_READ(phy, SUNXI_PIPE_CLOCK_CONTROL); 1261.1Sjmcneill val |= PCC_PIPE_CLK_OPEN; 1271.1Sjmcneill PHY_WRITE(phy, SUNXI_PIPE_CLOCK_CONTROL, val); 1281.1Sjmcneill 1291.1Sjmcneill val = PHY_READ(phy, SUNXI_APP); 1301.1Sjmcneill val |= APP_FORCE_VBUS; 1311.1Sjmcneill PHY_WRITE(phy, SUNXI_APP, val); 1321.1Sjmcneill 1331.1Sjmcneill PHY_WRITE(phy, SUNXI_PHY_TUNE_LOW, PTL_MAGIC); 1341.1Sjmcneill 1351.1Sjmcneill val = PHY_READ(phy, SUNXI_PHY_TUNE_HIGH); 1361.1Sjmcneill val |= PTH_TX_BOOST_LVL; 1371.1Sjmcneill val |= PTH_LOS_BIAS; 1381.1Sjmcneill val &= ~PTH_TX_SWING_FULL; 1391.1Sjmcneill val |= __SHIFTIN(0x55, PTH_TX_SWING_FULL); 1401.1Sjmcneill val &= ~PTH_TX_DEEMPH_6DB; 1411.1Sjmcneill val |= __SHIFTIN(0x20, PTH_TX_DEEMPH_6DB); 1421.1Sjmcneill val &= ~PTH_TX_DEEMPH_3P5DB; 1431.1Sjmcneill val |= __SHIFTIN(0x15, PTH_TX_DEEMPH_3P5DB); 1441.1Sjmcneill PHY_WRITE(phy, SUNXI_PHY_TUNE_HIGH, val); 1451.1Sjmcneill 1461.1Sjmcneill return phy->phy_reg ? fdtbus_regulator_enable(phy->phy_reg) : 0; 1471.1Sjmcneill } else { 1481.1Sjmcneill return phy->phy_reg ? fdtbus_regulator_disable(phy->phy_reg) : 0; 1491.1Sjmcneill } 1501.1Sjmcneill} 1511.1Sjmcneill 1521.1Sjmcneillconst struct fdtbus_phy_controller_func sunxi_usb3phy_funcs = { 1531.1Sjmcneill .acquire = sunxi_usb3phy_acquire, 1541.1Sjmcneill .release = sunxi_usb3phy_release, 1551.1Sjmcneill .enable = sunxi_usb3phy_enable, 1561.1Sjmcneill}; 1571.1Sjmcneill 1581.1Sjmcneillstatic int 1591.1Sjmcneillsunxi_usb3phy_match(device_t parent, cfdata_t cf, void *aux) 1601.1Sjmcneill{ 1611.1Sjmcneill struct fdt_attach_args * const faa = aux; 1621.1Sjmcneill 1631.5Sthorpej return of_compatible_match(faa->faa_phandle, compat_data); 1641.1Sjmcneill} 1651.1Sjmcneill 1661.1Sjmcneillstatic void 1671.1Sjmcneillsunxi_usb3phy_attach(device_t parent, device_t self, void *aux) 1681.1Sjmcneill{ 1691.1Sjmcneill struct sunxi_usb3phy_softc * const sc = device_private(self); 1701.1Sjmcneill struct sunxi_usb3phy *phy = &sc->sc_phy; 1711.1Sjmcneill struct fdt_attach_args * const faa = aux; 1721.1Sjmcneill const int phandle = faa->faa_phandle; 1731.1Sjmcneill struct fdtbus_reset *rst; 1741.1Sjmcneill struct clk *clk; 1751.1Sjmcneill bus_addr_t addr; 1761.1Sjmcneill bus_size_t size; 1771.1Sjmcneill u_int n; 1781.1Sjmcneill 1791.1Sjmcneill sc->sc_dev = self; 1801.5Sthorpej sc->sc_type = of_compatible_lookup(phandle, compat_data)->value; 1811.1Sjmcneill 1821.1Sjmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 1831.1Sjmcneill aprint_error(": couldn't get phy registers\n"); 1841.1Sjmcneill return; 1851.1Sjmcneill } 1861.1Sjmcneill 1871.1Sjmcneill phy->phy_bst = faa->faa_bst; 1881.1Sjmcneill if (bus_space_map(phy->phy_bst, addr, size, 0, &phy->phy_bsh) != 0) { 1891.1Sjmcneill aprint_error(": couldn't map phy registers\n"); 1901.1Sjmcneill return; 1911.1Sjmcneill } 1921.1Sjmcneill 1931.1Sjmcneill /* Get optional regulator */ 1941.1Sjmcneill phy->phy_reg = fdtbus_regulator_acquire(phandle, "phy-supply"); 1951.1Sjmcneill 1961.1Sjmcneill /* Enable clocks */ 1971.1Sjmcneill for (n = 0; (clk = fdtbus_clock_get_index(phandle, n)) != NULL; n++) 1981.1Sjmcneill if (clk_enable(clk) != 0) { 1991.1Sjmcneill aprint_error(": couldn't enable clock #%d\n", n); 2001.1Sjmcneill return; 2011.1Sjmcneill } 2021.1Sjmcneill /* De-assert resets */ 2031.1Sjmcneill for (n = 0; (rst = fdtbus_reset_get_index(phandle, n)) != NULL; n++) 2041.1Sjmcneill if (fdtbus_reset_deassert(rst) != 0) { 2051.1Sjmcneill aprint_error(": couldn't de-assert reset #%d\n", n); 2061.1Sjmcneill return; 2071.1Sjmcneill } 2081.1Sjmcneill 2091.1Sjmcneill aprint_naive("\n"); 2101.1Sjmcneill aprint_normal(": USB3 PHY\n"); 2111.1Sjmcneill 2121.1Sjmcneill fdtbus_register_phy_controller(self, phandle, &sunxi_usb3phy_funcs); 2131.1Sjmcneill} 214