18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Meson GXL and GXM USB2 PHY driver 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2017 Martin Blumenstingl <martin.blumenstingl@googlemail.com> 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/clk.h> 98c2ecf20Sopenharmony_ci#include <linux/delay.h> 108c2ecf20Sopenharmony_ci#include <linux/io.h> 118c2ecf20Sopenharmony_ci#include <linux/module.h> 128c2ecf20Sopenharmony_ci#include <linux/of_device.h> 138c2ecf20Sopenharmony_ci#include <linux/regmap.h> 148c2ecf20Sopenharmony_ci#include <linux/reset.h> 158c2ecf20Sopenharmony_ci#include <linux/phy/phy.h> 168c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci/* bits [31:27] are read-only */ 198c2ecf20Sopenharmony_ci#define U2P_R0 0x0 208c2ecf20Sopenharmony_ci #define U2P_R0_BYPASS_SEL BIT(0) 218c2ecf20Sopenharmony_ci #define U2P_R0_BYPASS_DM_EN BIT(1) 228c2ecf20Sopenharmony_ci #define U2P_R0_BYPASS_DP_EN BIT(2) 238c2ecf20Sopenharmony_ci #define U2P_R0_TXBITSTUFF_ENH BIT(3) 248c2ecf20Sopenharmony_ci #define U2P_R0_TXBITSTUFF_EN BIT(4) 258c2ecf20Sopenharmony_ci #define U2P_R0_DM_PULLDOWN BIT(5) 268c2ecf20Sopenharmony_ci #define U2P_R0_DP_PULLDOWN BIT(6) 278c2ecf20Sopenharmony_ci #define U2P_R0_DP_VBUS_VLD_EXT_SEL BIT(7) 288c2ecf20Sopenharmony_ci #define U2P_R0_DP_VBUS_VLD_EXT BIT(8) 298c2ecf20Sopenharmony_ci #define U2P_R0_ADP_PRB_EN BIT(9) 308c2ecf20Sopenharmony_ci #define U2P_R0_ADP_DISCHARGE BIT(10) 318c2ecf20Sopenharmony_ci #define U2P_R0_ADP_CHARGE BIT(11) 328c2ecf20Sopenharmony_ci #define U2P_R0_DRV_VBUS BIT(12) 338c2ecf20Sopenharmony_ci #define U2P_R0_ID_PULLUP BIT(13) 348c2ecf20Sopenharmony_ci #define U2P_R0_LOOPBACK_EN_B BIT(14) 358c2ecf20Sopenharmony_ci #define U2P_R0_OTG_DISABLE BIT(15) 368c2ecf20Sopenharmony_ci #define U2P_R0_COMMON_ONN BIT(16) 378c2ecf20Sopenharmony_ci #define U2P_R0_FSEL_MASK GENMASK(19, 17) 388c2ecf20Sopenharmony_ci #define U2P_R0_REF_CLK_SEL_MASK GENMASK(21, 20) 398c2ecf20Sopenharmony_ci #define U2P_R0_POWER_ON_RESET BIT(22) 408c2ecf20Sopenharmony_ci #define U2P_R0_V_ATE_TEST_EN_B_MASK GENMASK(24, 23) 418c2ecf20Sopenharmony_ci #define U2P_R0_ID_SET_ID_DQ BIT(25) 428c2ecf20Sopenharmony_ci #define U2P_R0_ATE_RESET BIT(26) 438c2ecf20Sopenharmony_ci #define U2P_R0_FSV_MINUS BIT(27) 448c2ecf20Sopenharmony_ci #define U2P_R0_FSV_PLUS BIT(28) 458c2ecf20Sopenharmony_ci #define U2P_R0_BYPASS_DM_DATA BIT(29) 468c2ecf20Sopenharmony_ci #define U2P_R0_BYPASS_DP_DATA BIT(30) 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci#define U2P_R1 0x4 498c2ecf20Sopenharmony_ci #define U2P_R1_BURN_IN_TEST BIT(0) 508c2ecf20Sopenharmony_ci #define U2P_R1_ACA_ENABLE BIT(1) 518c2ecf20Sopenharmony_ci #define U2P_R1_DCD_ENABLE BIT(2) 528c2ecf20Sopenharmony_ci #define U2P_R1_VDAT_SRC_EN_B BIT(3) 538c2ecf20Sopenharmony_ci #define U2P_R1_VDAT_DET_EN_B BIT(4) 548c2ecf20Sopenharmony_ci #define U2P_R1_CHARGES_SEL BIT(5) 558c2ecf20Sopenharmony_ci #define U2P_R1_TX_PREEMP_PULSE_TUNE BIT(6) 568c2ecf20Sopenharmony_ci #define U2P_R1_TX_PREEMP_AMP_TUNE_MASK GENMASK(8, 7) 578c2ecf20Sopenharmony_ci #define U2P_R1_TX_RES_TUNE_MASK GENMASK(10, 9) 588c2ecf20Sopenharmony_ci #define U2P_R1_TX_RISE_TUNE_MASK GENMASK(12, 11) 598c2ecf20Sopenharmony_ci #define U2P_R1_TX_VREF_TUNE_MASK GENMASK(16, 13) 608c2ecf20Sopenharmony_ci #define U2P_R1_TX_FSLS_TUNE_MASK GENMASK(20, 17) 618c2ecf20Sopenharmony_ci #define U2P_R1_TX_HSXV_TUNE_MASK GENMASK(22, 21) 628c2ecf20Sopenharmony_ci #define U2P_R1_OTG_TUNE_MASK GENMASK(25, 23) 638c2ecf20Sopenharmony_ci #define U2P_R1_SQRX_TUNE_MASK GENMASK(28, 26) 648c2ecf20Sopenharmony_ci #define U2P_R1_COMP_DIS_TUNE_MASK GENMASK(31, 29) 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci/* bits [31:14] are read-only */ 678c2ecf20Sopenharmony_ci#define U2P_R2 0x8 688c2ecf20Sopenharmony_ci #define U2P_R2_TESTDATA_IN_MASK GENMASK(7, 0) 698c2ecf20Sopenharmony_ci #define U2P_R2_TESTADDR_MASK GENMASK(11, 8) 708c2ecf20Sopenharmony_ci #define U2P_R2_TESTDATA_OUT_SEL BIT(12) 718c2ecf20Sopenharmony_ci #define U2P_R2_TESTCLK BIT(13) 728c2ecf20Sopenharmony_ci #define U2P_R2_TESTDATA_OUT_MASK GENMASK(17, 14) 738c2ecf20Sopenharmony_ci #define U2P_R2_ACA_PIN_RANGE_C BIT(18) 748c2ecf20Sopenharmony_ci #define U2P_R2_ACA_PIN_RANGE_B BIT(19) 758c2ecf20Sopenharmony_ci #define U2P_R2_ACA_PIN_RANGE_A BIT(20) 768c2ecf20Sopenharmony_ci #define U2P_R2_ACA_PIN_GND BIT(21) 778c2ecf20Sopenharmony_ci #define U2P_R2_ACA_PIN_FLOAT BIT(22) 788c2ecf20Sopenharmony_ci #define U2P_R2_CHARGE_DETECT BIT(23) 798c2ecf20Sopenharmony_ci #define U2P_R2_DEVICE_SESSION_VALID BIT(24) 808c2ecf20Sopenharmony_ci #define U2P_R2_ADP_PROBE BIT(25) 818c2ecf20Sopenharmony_ci #define U2P_R2_ADP_SENSE BIT(26) 828c2ecf20Sopenharmony_ci #define U2P_R2_SESSION_END BIT(27) 838c2ecf20Sopenharmony_ci #define U2P_R2_VBUS_VALID BIT(28) 848c2ecf20Sopenharmony_ci #define U2P_R2_B_VALID BIT(29) 858c2ecf20Sopenharmony_ci #define U2P_R2_A_VALID BIT(30) 868c2ecf20Sopenharmony_ci #define U2P_R2_ID_DIG BIT(31) 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci#define U2P_R3 0xc 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci#define RESET_COMPLETE_TIME 500 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_cistruct phy_meson_gxl_usb2_priv { 938c2ecf20Sopenharmony_ci struct regmap *regmap; 948c2ecf20Sopenharmony_ci enum phy_mode mode; 958c2ecf20Sopenharmony_ci int is_enabled; 968c2ecf20Sopenharmony_ci struct clk *clk; 978c2ecf20Sopenharmony_ci struct reset_control *reset; 988c2ecf20Sopenharmony_ci}; 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_cistatic const struct regmap_config phy_meson_gxl_usb2_regmap_conf = { 1018c2ecf20Sopenharmony_ci .reg_bits = 8, 1028c2ecf20Sopenharmony_ci .val_bits = 32, 1038c2ecf20Sopenharmony_ci .reg_stride = 4, 1048c2ecf20Sopenharmony_ci .max_register = U2P_R3, 1058c2ecf20Sopenharmony_ci}; 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_cistatic int phy_meson_gxl_usb2_init(struct phy *phy) 1088c2ecf20Sopenharmony_ci{ 1098c2ecf20Sopenharmony_ci struct phy_meson_gxl_usb2_priv *priv = phy_get_drvdata(phy); 1108c2ecf20Sopenharmony_ci int ret; 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci ret = reset_control_reset(priv->reset); 1138c2ecf20Sopenharmony_ci if (ret) 1148c2ecf20Sopenharmony_ci return ret; 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci ret = clk_prepare_enable(priv->clk); 1178c2ecf20Sopenharmony_ci if (ret) 1188c2ecf20Sopenharmony_ci return ret; 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci return 0; 1218c2ecf20Sopenharmony_ci} 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_cistatic int phy_meson_gxl_usb2_exit(struct phy *phy) 1248c2ecf20Sopenharmony_ci{ 1258c2ecf20Sopenharmony_ci struct phy_meson_gxl_usb2_priv *priv = phy_get_drvdata(phy); 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci clk_disable_unprepare(priv->clk); 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci return 0; 1308c2ecf20Sopenharmony_ci} 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_cistatic int phy_meson_gxl_usb2_reset(struct phy *phy) 1338c2ecf20Sopenharmony_ci{ 1348c2ecf20Sopenharmony_ci struct phy_meson_gxl_usb2_priv *priv = phy_get_drvdata(phy); 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci if (priv->is_enabled) { 1378c2ecf20Sopenharmony_ci /* reset the PHY and wait until settings are stabilized */ 1388c2ecf20Sopenharmony_ci regmap_update_bits(priv->regmap, U2P_R0, U2P_R0_POWER_ON_RESET, 1398c2ecf20Sopenharmony_ci U2P_R0_POWER_ON_RESET); 1408c2ecf20Sopenharmony_ci udelay(RESET_COMPLETE_TIME); 1418c2ecf20Sopenharmony_ci regmap_update_bits(priv->regmap, U2P_R0, U2P_R0_POWER_ON_RESET, 1428c2ecf20Sopenharmony_ci 0); 1438c2ecf20Sopenharmony_ci udelay(RESET_COMPLETE_TIME); 1448c2ecf20Sopenharmony_ci } 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci return 0; 1478c2ecf20Sopenharmony_ci} 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_cistatic int phy_meson_gxl_usb2_set_mode(struct phy *phy, 1508c2ecf20Sopenharmony_ci enum phy_mode mode, int submode) 1518c2ecf20Sopenharmony_ci{ 1528c2ecf20Sopenharmony_ci struct phy_meson_gxl_usb2_priv *priv = phy_get_drvdata(phy); 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci switch (mode) { 1558c2ecf20Sopenharmony_ci case PHY_MODE_USB_HOST: 1568c2ecf20Sopenharmony_ci case PHY_MODE_USB_OTG: 1578c2ecf20Sopenharmony_ci regmap_update_bits(priv->regmap, U2P_R0, U2P_R0_DM_PULLDOWN, 1588c2ecf20Sopenharmony_ci U2P_R0_DM_PULLDOWN); 1598c2ecf20Sopenharmony_ci regmap_update_bits(priv->regmap, U2P_R0, U2P_R0_DP_PULLDOWN, 1608c2ecf20Sopenharmony_ci U2P_R0_DP_PULLDOWN); 1618c2ecf20Sopenharmony_ci regmap_update_bits(priv->regmap, U2P_R0, U2P_R0_ID_PULLUP, 0); 1628c2ecf20Sopenharmony_ci break; 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci case PHY_MODE_USB_DEVICE: 1658c2ecf20Sopenharmony_ci regmap_update_bits(priv->regmap, U2P_R0, U2P_R0_DM_PULLDOWN, 1668c2ecf20Sopenharmony_ci 0); 1678c2ecf20Sopenharmony_ci regmap_update_bits(priv->regmap, U2P_R0, U2P_R0_DP_PULLDOWN, 1688c2ecf20Sopenharmony_ci 0); 1698c2ecf20Sopenharmony_ci regmap_update_bits(priv->regmap, U2P_R0, U2P_R0_ID_PULLUP, 1708c2ecf20Sopenharmony_ci U2P_R0_ID_PULLUP); 1718c2ecf20Sopenharmony_ci break; 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci default: 1748c2ecf20Sopenharmony_ci return -EINVAL; 1758c2ecf20Sopenharmony_ci } 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci phy_meson_gxl_usb2_reset(phy); 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci priv->mode = mode; 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci return 0; 1828c2ecf20Sopenharmony_ci} 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_cistatic int phy_meson_gxl_usb2_power_off(struct phy *phy) 1858c2ecf20Sopenharmony_ci{ 1868c2ecf20Sopenharmony_ci struct phy_meson_gxl_usb2_priv *priv = phy_get_drvdata(phy); 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci priv->is_enabled = 0; 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ci /* power off the PHY by putting it into reset mode */ 1918c2ecf20Sopenharmony_ci regmap_update_bits(priv->regmap, U2P_R0, U2P_R0_POWER_ON_RESET, 1928c2ecf20Sopenharmony_ci U2P_R0_POWER_ON_RESET); 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ci return 0; 1958c2ecf20Sopenharmony_ci} 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_cistatic int phy_meson_gxl_usb2_power_on(struct phy *phy) 1988c2ecf20Sopenharmony_ci{ 1998c2ecf20Sopenharmony_ci struct phy_meson_gxl_usb2_priv *priv = phy_get_drvdata(phy); 2008c2ecf20Sopenharmony_ci int ret; 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci priv->is_enabled = 1; 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci /* power on the PHY by taking it out of reset mode */ 2058c2ecf20Sopenharmony_ci regmap_update_bits(priv->regmap, U2P_R0, U2P_R0_POWER_ON_RESET, 0); 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ci ret = phy_meson_gxl_usb2_set_mode(phy, priv->mode, 0); 2088c2ecf20Sopenharmony_ci if (ret) { 2098c2ecf20Sopenharmony_ci phy_meson_gxl_usb2_power_off(phy); 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci dev_err(&phy->dev, "Failed to initialize PHY with mode %d\n", 2128c2ecf20Sopenharmony_ci priv->mode); 2138c2ecf20Sopenharmony_ci return ret; 2148c2ecf20Sopenharmony_ci } 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci return 0; 2178c2ecf20Sopenharmony_ci} 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_cistatic const struct phy_ops phy_meson_gxl_usb2_ops = { 2208c2ecf20Sopenharmony_ci .init = phy_meson_gxl_usb2_init, 2218c2ecf20Sopenharmony_ci .exit = phy_meson_gxl_usb2_exit, 2228c2ecf20Sopenharmony_ci .power_on = phy_meson_gxl_usb2_power_on, 2238c2ecf20Sopenharmony_ci .power_off = phy_meson_gxl_usb2_power_off, 2248c2ecf20Sopenharmony_ci .set_mode = phy_meson_gxl_usb2_set_mode, 2258c2ecf20Sopenharmony_ci .reset = phy_meson_gxl_usb2_reset, 2268c2ecf20Sopenharmony_ci .owner = THIS_MODULE, 2278c2ecf20Sopenharmony_ci}; 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_cistatic int phy_meson_gxl_usb2_probe(struct platform_device *pdev) 2308c2ecf20Sopenharmony_ci{ 2318c2ecf20Sopenharmony_ci struct device *dev = &pdev->dev; 2328c2ecf20Sopenharmony_ci struct phy_provider *phy_provider; 2338c2ecf20Sopenharmony_ci struct resource *res; 2348c2ecf20Sopenharmony_ci struct phy_meson_gxl_usb2_priv *priv; 2358c2ecf20Sopenharmony_ci struct phy *phy; 2368c2ecf20Sopenharmony_ci void __iomem *base; 2378c2ecf20Sopenharmony_ci int ret; 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 2408c2ecf20Sopenharmony_ci if (!priv) 2418c2ecf20Sopenharmony_ci return -ENOMEM; 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, priv); 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_ci res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 2468c2ecf20Sopenharmony_ci base = devm_ioremap_resource(dev, res); 2478c2ecf20Sopenharmony_ci if (IS_ERR(base)) 2488c2ecf20Sopenharmony_ci return PTR_ERR(base); 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ci /* start in host mode */ 2518c2ecf20Sopenharmony_ci priv->mode = PHY_MODE_USB_HOST; 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_ci priv->regmap = devm_regmap_init_mmio(dev, base, 2548c2ecf20Sopenharmony_ci &phy_meson_gxl_usb2_regmap_conf); 2558c2ecf20Sopenharmony_ci if (IS_ERR(priv->regmap)) 2568c2ecf20Sopenharmony_ci return PTR_ERR(priv->regmap); 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_ci priv->clk = devm_clk_get_optional(dev, "phy"); 2598c2ecf20Sopenharmony_ci if (IS_ERR(priv->clk)) 2608c2ecf20Sopenharmony_ci return PTR_ERR(priv->clk); 2618c2ecf20Sopenharmony_ci 2628c2ecf20Sopenharmony_ci priv->reset = devm_reset_control_get_optional_shared(dev, "phy"); 2638c2ecf20Sopenharmony_ci if (IS_ERR(priv->reset)) 2648c2ecf20Sopenharmony_ci return PTR_ERR(priv->reset); 2658c2ecf20Sopenharmony_ci 2668c2ecf20Sopenharmony_ci phy = devm_phy_create(dev, NULL, &phy_meson_gxl_usb2_ops); 2678c2ecf20Sopenharmony_ci if (IS_ERR(phy)) { 2688c2ecf20Sopenharmony_ci ret = PTR_ERR(phy); 2698c2ecf20Sopenharmony_ci if (ret != -EPROBE_DEFER) 2708c2ecf20Sopenharmony_ci dev_err(dev, "failed to create PHY\n"); 2718c2ecf20Sopenharmony_ci 2728c2ecf20Sopenharmony_ci return ret; 2738c2ecf20Sopenharmony_ci } 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ci phy_set_drvdata(phy, priv); 2768c2ecf20Sopenharmony_ci 2778c2ecf20Sopenharmony_ci phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate); 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_ci return PTR_ERR_OR_ZERO(phy_provider); 2808c2ecf20Sopenharmony_ci} 2818c2ecf20Sopenharmony_ci 2828c2ecf20Sopenharmony_cistatic const struct of_device_id phy_meson_gxl_usb2_of_match[] = { 2838c2ecf20Sopenharmony_ci { .compatible = "amlogic,meson-gxl-usb2-phy", }, 2848c2ecf20Sopenharmony_ci { }, 2858c2ecf20Sopenharmony_ci}; 2868c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, phy_meson_gxl_usb2_of_match); 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_cistatic struct platform_driver phy_meson_gxl_usb2_driver = { 2898c2ecf20Sopenharmony_ci .probe = phy_meson_gxl_usb2_probe, 2908c2ecf20Sopenharmony_ci .driver = { 2918c2ecf20Sopenharmony_ci .name = "phy-meson-gxl-usb2", 2928c2ecf20Sopenharmony_ci .of_match_table = phy_meson_gxl_usb2_of_match, 2938c2ecf20Sopenharmony_ci }, 2948c2ecf20Sopenharmony_ci}; 2958c2ecf20Sopenharmony_cimodule_platform_driver(phy_meson_gxl_usb2_driver); 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_ciMODULE_AUTHOR("Martin Blumenstingl <martin.blumenstingl@googlemail.com>"); 2988c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Meson GXL and GXM USB2 PHY driver"); 2998c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 300