1c9999337SNeil Armstrong // SPDX-License-Identifier: GPL-2.0 2c9999337SNeil Armstrong /* 3c9999337SNeil Armstrong * USB Glue for Amlogic G12A SoCs 4c9999337SNeil Armstrong * 5c9999337SNeil Armstrong * Copyright (c) 2019 BayLibre, SAS 6c9999337SNeil Armstrong * Author: Neil Armstrong <narmstrong@baylibre.com> 7c9999337SNeil Armstrong */ 8c9999337SNeil Armstrong 9c9999337SNeil Armstrong /* 10c9999337SNeil Armstrong * The USB is organized with a glue around the DWC3 Controller IP as : 11c9999337SNeil Armstrong * - Control registers for each USB2 Ports 12c9999337SNeil Armstrong * - Control registers for the USB PHY layer 13c9999337SNeil Armstrong * - SuperSpeed PHY can be enabled only if port is used 14f90db107SNeil Armstrong * - Dynamic OTG switching with ID change interrupt 15c9999337SNeil Armstrong */ 16c9999337SNeil Armstrong 17c9999337SNeil Armstrong #include <linux/module.h> 18c9999337SNeil Armstrong #include <linux/kernel.h> 19c9999337SNeil Armstrong #include <linux/platform_device.h> 20c9999337SNeil Armstrong #include <linux/clk.h> 21c9999337SNeil Armstrong #include <linux/of.h> 22c9999337SNeil Armstrong #include <linux/of_platform.h> 23c9999337SNeil Armstrong #include <linux/pm_runtime.h> 24c9999337SNeil Armstrong #include <linux/regmap.h> 25c9999337SNeil Armstrong #include <linux/bitfield.h> 26c9999337SNeil Armstrong #include <linux/bitops.h> 27c9999337SNeil Armstrong #include <linux/reset.h> 28c9999337SNeil Armstrong #include <linux/phy/phy.h> 29c9999337SNeil Armstrong #include <linux/usb/otg.h> 30c9999337SNeil Armstrong #include <linux/usb/role.h> 31c9999337SNeil Armstrong #include <linux/regulator/consumer.h> 32c9999337SNeil Armstrong 33013af227SNeil Armstrong /* USB2 Ports Control Registers, offsets are per-port */ 34c9999337SNeil Armstrong 35c9999337SNeil Armstrong #define U2P_REG_SIZE 0x20 36c9999337SNeil Armstrong 37c9999337SNeil Armstrong #define U2P_R0 0x0 38c9999337SNeil Armstrong #define U2P_R0_HOST_DEVICE BIT(0) 39c9999337SNeil Armstrong #define U2P_R0_POWER_OK BIT(1) 40c9999337SNeil Armstrong #define U2P_R0_HAST_MODE BIT(2) 41c9999337SNeil Armstrong #define U2P_R0_POWER_ON_RESET BIT(3) 42c9999337SNeil Armstrong #define U2P_R0_ID_PULLUP BIT(4) 43c9999337SNeil Armstrong #define U2P_R0_DRV_VBUS BIT(5) 44c9999337SNeil Armstrong 45c9999337SNeil Armstrong #define U2P_R1 0x4 46c9999337SNeil Armstrong #define U2P_R1_PHY_READY BIT(0) 47c9999337SNeil Armstrong #define U2P_R1_ID_DIG BIT(1) 48c9999337SNeil Armstrong #define U2P_R1_OTG_SESSION_VALID BIT(2) 49c9999337SNeil Armstrong #define U2P_R1_VBUS_VALID BIT(3) 50c9999337SNeil Armstrong 51c9999337SNeil Armstrong /* USB Glue Control Registers */ 52c9999337SNeil Armstrong 53013af227SNeil Armstrong #define G12A_GLUE_OFFSET 0x80 54013af227SNeil Armstrong 55013af227SNeil Armstrong #define USB_R0 0x00 56c9999337SNeil Armstrong #define USB_R0_P30_LANE0_TX2RX_LOOPBACK BIT(17) 57c9999337SNeil Armstrong #define USB_R0_P30_LANE0_EXT_PCLK_REQ BIT(18) 58c9999337SNeil Armstrong #define USB_R0_P30_PCS_RX_LOS_MASK_VAL_MASK GENMASK(28, 19) 59c9999337SNeil Armstrong #define USB_R0_U2D_SS_SCALEDOWN_MODE_MASK GENMASK(30, 29) 60c9999337SNeil Armstrong #define USB_R0_U2D_ACT BIT(31) 61c9999337SNeil Armstrong 62013af227SNeil Armstrong #define USB_R1 0x04 63c9999337SNeil Armstrong #define USB_R1_U3H_BIGENDIAN_GS BIT(0) 64c9999337SNeil Armstrong #define USB_R1_U3H_PME_ENABLE BIT(1) 65c9999337SNeil Armstrong #define USB_R1_U3H_HUB_PORT_OVERCURRENT_MASK GENMASK(4, 2) 66c9999337SNeil Armstrong #define USB_R1_U3H_HUB_PORT_PERM_ATTACH_MASK GENMASK(9, 7) 67c9999337SNeil Armstrong #define USB_R1_U3H_HOST_U2_PORT_DISABLE_MASK GENMASK(13, 12) 68c9999337SNeil Armstrong #define USB_R1_U3H_HOST_U3_PORT_DISABLE BIT(16) 69c9999337SNeil Armstrong #define USB_R1_U3H_HOST_PORT_POWER_CONTROL_PRESENT BIT(17) 70c9999337SNeil Armstrong #define USB_R1_U3H_HOST_MSI_ENABLE BIT(18) 71c9999337SNeil Armstrong #define USB_R1_U3H_FLADJ_30MHZ_REG_MASK GENMASK(24, 19) 72c9999337SNeil Armstrong #define USB_R1_P30_PCS_TX_SWING_FULL_MASK GENMASK(31, 25) 73c9999337SNeil Armstrong 74013af227SNeil Armstrong #define USB_R2 0x08 75c9999337SNeil Armstrong #define USB_R2_P30_PCS_TX_DEEMPH_3P5DB_MASK GENMASK(25, 20) 76c9999337SNeil Armstrong #define USB_R2_P30_PCS_TX_DEEMPH_6DB_MASK GENMASK(31, 26) 77c9999337SNeil Armstrong 78013af227SNeil Armstrong #define USB_R3 0x0c 79c9999337SNeil Armstrong #define USB_R3_P30_SSC_ENABLE BIT(0) 80c9999337SNeil Armstrong #define USB_R3_P30_SSC_RANGE_MASK GENMASK(3, 1) 81c9999337SNeil Armstrong #define USB_R3_P30_SSC_REF_CLK_SEL_MASK GENMASK(12, 4) 82c9999337SNeil Armstrong #define USB_R3_P30_REF_SSP_EN BIT(13) 83c9999337SNeil Armstrong 84013af227SNeil Armstrong #define USB_R4 0x10 85c9999337SNeil Armstrong #define USB_R4_P21_PORT_RESET_0 BIT(0) 86c9999337SNeil Armstrong #define USB_R4_P21_SLEEP_M0 BIT(1) 87c9999337SNeil Armstrong #define USB_R4_MEM_PD_MASK GENMASK(3, 2) 88c9999337SNeil Armstrong #define USB_R4_P21_ONLY BIT(4) 89c9999337SNeil Armstrong 90013af227SNeil Armstrong #define USB_R5 0x14 91c9999337SNeil Armstrong #define USB_R5_ID_DIG_SYNC BIT(0) 92c9999337SNeil Armstrong #define USB_R5_ID_DIG_REG BIT(1) 93c9999337SNeil Armstrong #define USB_R5_ID_DIG_CFG_MASK GENMASK(3, 2) 94c9999337SNeil Armstrong #define USB_R5_ID_DIG_EN_0 BIT(4) 95c9999337SNeil Armstrong #define USB_R5_ID_DIG_EN_1 BIT(5) 96c9999337SNeil Armstrong #define USB_R5_ID_DIG_CURR BIT(6) 97c9999337SNeil Armstrong #define USB_R5_ID_DIG_IRQ BIT(7) 98c9999337SNeil Armstrong #define USB_R5_ID_DIG_TH_MASK GENMASK(15, 8) 99c9999337SNeil Armstrong #define USB_R5_ID_DIG_CNT_MASK GENMASK(23, 16) 100c9999337SNeil Armstrong 1015174564cSNeil Armstrong #define PHY_COUNT 3 1025174564cSNeil Armstrong #define USB2_OTG_PHY 1 103c9999337SNeil Armstrong 104a9fc15e0SNeil Armstrong static struct clk_bulk_data meson_gxl_clocks[] = { 105a9fc15e0SNeil Armstrong { .id = "usb_ctrl" }, 106a9fc15e0SNeil Armstrong { .id = "ddr" }, 107a9fc15e0SNeil Armstrong }; 108a9fc15e0SNeil Armstrong 1091e355f21SHanjie Lin static struct clk_bulk_data meson_g12a_clocks[] = { 1101e355f21SHanjie Lin { .id = NULL }, 1111e355f21SHanjie Lin }; 1121e355f21SHanjie Lin 1131e355f21SHanjie Lin static struct clk_bulk_data meson_a1_clocks[] = { 1141e355f21SHanjie Lin { .id = "usb_ctrl" }, 1151e355f21SHanjie Lin { .id = "usb_bus" }, 1161e355f21SHanjie Lin { .id = "xtal_usb_ctrl" }, 1171e355f21SHanjie Lin }; 1181e355f21SHanjie Lin 119e5ee93d4SFelipe Balbi static const char * const meson_gxm_phy_names[] = { 120a9fc15e0SNeil Armstrong "usb2-phy0", "usb2-phy1", "usb2-phy2", 121a9fc15e0SNeil Armstrong }; 122a9fc15e0SNeil Armstrong 123e5ee93d4SFelipe Balbi static const char * const meson_g12a_phy_names[] = { 1245174564cSNeil Armstrong "usb2-phy0", "usb2-phy1", "usb3-phy0", 1255174564cSNeil Armstrong }; 1265174564cSNeil Armstrong 1275174564cSNeil Armstrong /* 1285174564cSNeil Armstrong * Amlogic A1 has a single physical PHY, in slot 1, but still has the 1295174564cSNeil Armstrong * two U2 PHY controls register blocks like G12A. 13065f3d449SNeil Armstrong * AXG has the similar scheme, thus needs the same tweak. 1315174564cSNeil Armstrong * Handling the first PHY on slot 1 would need a large amount of code 1325174564cSNeil Armstrong * changes, and the current management is generic enough to handle it 1335174564cSNeil Armstrong * correctly when only the "usb2-phy1" phy is specified on-par with the 1345174564cSNeil Armstrong * DT bindings. 1355174564cSNeil Armstrong */ 136e5ee93d4SFelipe Balbi static const char * const meson_a1_phy_names[] = { 1375174564cSNeil Armstrong "usb2-phy0", "usb2-phy1" 1385174564cSNeil Armstrong }; 1395174564cSNeil Armstrong 140013af227SNeil Armstrong struct dwc3_meson_g12a; 141013af227SNeil Armstrong 1421e355f21SHanjie Lin struct dwc3_meson_g12a_drvdata { 143df7e3745SNeil Armstrong bool otg_phy_host_port_disable; 1441e355f21SHanjie Lin struct clk_bulk_data *clks; 1451e355f21SHanjie Lin int num_clks; 146e5ee93d4SFelipe Balbi const char * const *phy_names; 1475174564cSNeil Armstrong int num_phys; 148013af227SNeil Armstrong int (*setup_regmaps)(struct dwc3_meson_g12a *priv, void __iomem *base); 14931306821SNeil Armstrong int (*usb2_init_phy)(struct dwc3_meson_g12a *priv, int i, 15031306821SNeil Armstrong enum phy_mode mode); 15131306821SNeil Armstrong int (*set_phy_mode)(struct dwc3_meson_g12a *priv, int i, 15231306821SNeil Armstrong enum phy_mode mode); 1535b0ba0caSNeil Armstrong int (*usb_init)(struct dwc3_meson_g12a *priv); 1545b0ba0caSNeil Armstrong int (*usb_post_init)(struct dwc3_meson_g12a *priv); 1551e355f21SHanjie Lin }; 1561e355f21SHanjie Lin 157a9fc15e0SNeil Armstrong static int dwc3_meson_gxl_setup_regmaps(struct dwc3_meson_g12a *priv, 158a9fc15e0SNeil Armstrong void __iomem *base); 159013af227SNeil Armstrong static int dwc3_meson_g12a_setup_regmaps(struct dwc3_meson_g12a *priv, 160013af227SNeil Armstrong void __iomem *base); 161013af227SNeil Armstrong 16231306821SNeil Armstrong static int dwc3_meson_g12a_usb2_init_phy(struct dwc3_meson_g12a *priv, int i, 16331306821SNeil Armstrong enum phy_mode mode); 164a9fc15e0SNeil Armstrong static int dwc3_meson_gxl_usb2_init_phy(struct dwc3_meson_g12a *priv, int i, 165a9fc15e0SNeil Armstrong enum phy_mode mode); 16631306821SNeil Armstrong 16731306821SNeil Armstrong static int dwc3_meson_g12a_set_phy_mode(struct dwc3_meson_g12a *priv, 16831306821SNeil Armstrong int i, enum phy_mode mode); 169a9fc15e0SNeil Armstrong static int dwc3_meson_gxl_set_phy_mode(struct dwc3_meson_g12a *priv, 170a9fc15e0SNeil Armstrong int i, enum phy_mode mode); 17131306821SNeil Armstrong 1725b0ba0caSNeil Armstrong static int dwc3_meson_g12a_usb_init(struct dwc3_meson_g12a *priv); 173a9fc15e0SNeil Armstrong static int dwc3_meson_gxl_usb_init(struct dwc3_meson_g12a *priv); 174a9fc15e0SNeil Armstrong 175a9fc15e0SNeil Armstrong static int dwc3_meson_gxl_usb_post_init(struct dwc3_meson_g12a *priv); 1765b0ba0caSNeil Armstrong 177df7e3745SNeil Armstrong /* 178df7e3745SNeil Armstrong * For GXL and GXM SoCs: 179df7e3745SNeil Armstrong * USB Phy muxing between the DWC2 Device controller and the DWC3 Host 180df7e3745SNeil Armstrong * controller is buggy when switching from Device to Host when USB port 181df7e3745SNeil Armstrong * is unpopulated, it causes the DWC3 to hard crash. 182df7e3745SNeil Armstrong * When populated (including OTG switching with ID pin), the switch works 183df7e3745SNeil Armstrong * like a charm like on the G12A platforms. 184df7e3745SNeil Armstrong * In order to still switch from Host to Device on an USB Type-A port, 185df7e3745SNeil Armstrong * an U2_PORT_DISABLE bit has been added to disconnect the DWC3 Host 186df7e3745SNeil Armstrong * controller from the port, but when used the DWC3 controller must be 187df7e3745SNeil Armstrong * reset to recover usage of the port. 188df7e3745SNeil Armstrong */ 189df7e3745SNeil Armstrong 190850ebb27SHeiner Kallweit static const struct dwc3_meson_g12a_drvdata gxl_drvdata = { 191a9fc15e0SNeil Armstrong .otg_phy_host_port_disable = true, 192a9fc15e0SNeil Armstrong .clks = meson_gxl_clocks, 193a9fc15e0SNeil Armstrong .num_clks = ARRAY_SIZE(meson_g12a_clocks), 194a9fc15e0SNeil Armstrong .phy_names = meson_a1_phy_names, 195a9fc15e0SNeil Armstrong .num_phys = ARRAY_SIZE(meson_a1_phy_names), 196a9fc15e0SNeil Armstrong .setup_regmaps = dwc3_meson_gxl_setup_regmaps, 197a9fc15e0SNeil Armstrong .usb2_init_phy = dwc3_meson_gxl_usb2_init_phy, 198a9fc15e0SNeil Armstrong .set_phy_mode = dwc3_meson_gxl_set_phy_mode, 199a9fc15e0SNeil Armstrong .usb_init = dwc3_meson_gxl_usb_init, 200a9fc15e0SNeil Armstrong .usb_post_init = dwc3_meson_gxl_usb_post_init, 201a9fc15e0SNeil Armstrong }; 202a9fc15e0SNeil Armstrong 203850ebb27SHeiner Kallweit static const struct dwc3_meson_g12a_drvdata gxm_drvdata = { 204a9fc15e0SNeil Armstrong .otg_phy_host_port_disable = true, 205a9fc15e0SNeil Armstrong .clks = meson_gxl_clocks, 206a9fc15e0SNeil Armstrong .num_clks = ARRAY_SIZE(meson_g12a_clocks), 207a9fc15e0SNeil Armstrong .phy_names = meson_gxm_phy_names, 208a9fc15e0SNeil Armstrong .num_phys = ARRAY_SIZE(meson_gxm_phy_names), 209a9fc15e0SNeil Armstrong .setup_regmaps = dwc3_meson_gxl_setup_regmaps, 210a9fc15e0SNeil Armstrong .usb2_init_phy = dwc3_meson_gxl_usb2_init_phy, 211a9fc15e0SNeil Armstrong .set_phy_mode = dwc3_meson_gxl_set_phy_mode, 212a9fc15e0SNeil Armstrong .usb_init = dwc3_meson_gxl_usb_init, 213a9fc15e0SNeil Armstrong .usb_post_init = dwc3_meson_gxl_usb_post_init, 214a9fc15e0SNeil Armstrong }; 215a9fc15e0SNeil Armstrong 216850ebb27SHeiner Kallweit static const struct dwc3_meson_g12a_drvdata axg_drvdata = { 21765f3d449SNeil Armstrong .clks = meson_gxl_clocks, 21865f3d449SNeil Armstrong .num_clks = ARRAY_SIZE(meson_gxl_clocks), 21965f3d449SNeil Armstrong .phy_names = meson_a1_phy_names, 22065f3d449SNeil Armstrong .num_phys = ARRAY_SIZE(meson_a1_phy_names), 22165f3d449SNeil Armstrong .setup_regmaps = dwc3_meson_gxl_setup_regmaps, 22265f3d449SNeil Armstrong .usb2_init_phy = dwc3_meson_gxl_usb2_init_phy, 22365f3d449SNeil Armstrong .set_phy_mode = dwc3_meson_gxl_set_phy_mode, 22465f3d449SNeil Armstrong .usb_init = dwc3_meson_g12a_usb_init, 22565f3d449SNeil Armstrong .usb_post_init = dwc3_meson_gxl_usb_post_init, 22665f3d449SNeil Armstrong }; 22765f3d449SNeil Armstrong 228850ebb27SHeiner Kallweit static const struct dwc3_meson_g12a_drvdata g12a_drvdata = { 2291e355f21SHanjie Lin .clks = meson_g12a_clocks, 2301e355f21SHanjie Lin .num_clks = ARRAY_SIZE(meson_g12a_clocks), 2315174564cSNeil Armstrong .phy_names = meson_g12a_phy_names, 2325174564cSNeil Armstrong .num_phys = ARRAY_SIZE(meson_g12a_phy_names), 233013af227SNeil Armstrong .setup_regmaps = dwc3_meson_g12a_setup_regmaps, 23431306821SNeil Armstrong .usb2_init_phy = dwc3_meson_g12a_usb2_init_phy, 23531306821SNeil Armstrong .set_phy_mode = dwc3_meson_g12a_set_phy_mode, 2365b0ba0caSNeil Armstrong .usb_init = dwc3_meson_g12a_usb_init, 2371e355f21SHanjie Lin }; 2381e355f21SHanjie Lin 239850ebb27SHeiner Kallweit static const struct dwc3_meson_g12a_drvdata a1_drvdata = { 2401e355f21SHanjie Lin .clks = meson_a1_clocks, 2411e355f21SHanjie Lin .num_clks = ARRAY_SIZE(meson_a1_clocks), 2425174564cSNeil Armstrong .phy_names = meson_a1_phy_names, 2435174564cSNeil Armstrong .num_phys = ARRAY_SIZE(meson_a1_phy_names), 244013af227SNeil Armstrong .setup_regmaps = dwc3_meson_g12a_setup_regmaps, 24531306821SNeil Armstrong .usb2_init_phy = dwc3_meson_g12a_usb2_init_phy, 24631306821SNeil Armstrong .set_phy_mode = dwc3_meson_g12a_set_phy_mode, 2475b0ba0caSNeil Armstrong .usb_init = dwc3_meson_g12a_usb_init, 2481e355f21SHanjie Lin }; 2491e355f21SHanjie Lin 250c9999337SNeil Armstrong struct dwc3_meson_g12a { 251c9999337SNeil Armstrong struct device *dev; 252013af227SNeil Armstrong struct regmap *u2p_regmap[PHY_COUNT]; 253013af227SNeil Armstrong struct regmap *usb_glue_regmap; 254c9999337SNeil Armstrong struct reset_control *reset; 255c9999337SNeil Armstrong struct phy *phys[PHY_COUNT]; 256c9999337SNeil Armstrong enum usb_dr_mode otg_mode; 257c9999337SNeil Armstrong enum phy_mode otg_phy_mode; 258c9999337SNeil Armstrong unsigned int usb2_ports; 259c9999337SNeil Armstrong unsigned int usb3_ports; 260c9999337SNeil Armstrong struct regulator *vbus; 261c9999337SNeil Armstrong struct usb_role_switch_desc switch_desc; 262c9999337SNeil Armstrong struct usb_role_switch *role_switch; 2631e355f21SHanjie Lin const struct dwc3_meson_g12a_drvdata *drvdata; 264c9999337SNeil Armstrong }; 265c9999337SNeil Armstrong 266a9fc15e0SNeil Armstrong static int dwc3_meson_gxl_set_phy_mode(struct dwc3_meson_g12a *priv, 267a9fc15e0SNeil Armstrong int i, enum phy_mode mode) 268a9fc15e0SNeil Armstrong { 269a9fc15e0SNeil Armstrong return phy_set_mode(priv->phys[i], mode); 270a9fc15e0SNeil Armstrong } 271a9fc15e0SNeil Armstrong 272a9fc15e0SNeil Armstrong static int dwc3_meson_gxl_usb2_init_phy(struct dwc3_meson_g12a *priv, int i, 273a9fc15e0SNeil Armstrong enum phy_mode mode) 274a9fc15e0SNeil Armstrong { 275a9fc15e0SNeil Armstrong /* On GXL PHY must be started in device mode for DWC2 init */ 276a9fc15e0SNeil Armstrong return priv->drvdata->set_phy_mode(priv, i, 277a9fc15e0SNeil Armstrong (i == USB2_OTG_PHY) ? PHY_MODE_USB_DEVICE 278a9fc15e0SNeil Armstrong : PHY_MODE_USB_HOST); 279a9fc15e0SNeil Armstrong } 280a9fc15e0SNeil Armstrong 28131306821SNeil Armstrong static int dwc3_meson_g12a_set_phy_mode(struct dwc3_meson_g12a *priv, 282c9999337SNeil Armstrong int i, enum phy_mode mode) 283c9999337SNeil Armstrong { 284c9999337SNeil Armstrong if (mode == PHY_MODE_USB_HOST) 285013af227SNeil Armstrong regmap_update_bits(priv->u2p_regmap[i], U2P_R0, 286c9999337SNeil Armstrong U2P_R0_HOST_DEVICE, 287c9999337SNeil Armstrong U2P_R0_HOST_DEVICE); 288c9999337SNeil Armstrong else 289013af227SNeil Armstrong regmap_update_bits(priv->u2p_regmap[i], U2P_R0, 290c9999337SNeil Armstrong U2P_R0_HOST_DEVICE, 0); 29131306821SNeil Armstrong 29231306821SNeil Armstrong return 0; 29331306821SNeil Armstrong } 29431306821SNeil Armstrong 29531306821SNeil Armstrong static int dwc3_meson_g12a_usb2_init_phy(struct dwc3_meson_g12a *priv, int i, 29631306821SNeil Armstrong enum phy_mode mode) 29731306821SNeil Armstrong { 29831306821SNeil Armstrong int ret; 29931306821SNeil Armstrong 30031306821SNeil Armstrong regmap_update_bits(priv->u2p_regmap[i], U2P_R0, 30131306821SNeil Armstrong U2P_R0_POWER_ON_RESET, 30231306821SNeil Armstrong U2P_R0_POWER_ON_RESET); 30331306821SNeil Armstrong 3046bae03b0SDmitry Rokosov if (i == USB2_OTG_PHY) { 30531306821SNeil Armstrong regmap_update_bits(priv->u2p_regmap[i], U2P_R0, 30631306821SNeil Armstrong U2P_R0_ID_PULLUP | U2P_R0_DRV_VBUS, 30731306821SNeil Armstrong U2P_R0_ID_PULLUP | U2P_R0_DRV_VBUS); 30831306821SNeil Armstrong 30931306821SNeil Armstrong ret = priv->drvdata->set_phy_mode(priv, i, mode); 31031306821SNeil Armstrong } else 31131306821SNeil Armstrong ret = priv->drvdata->set_phy_mode(priv, i, 31231306821SNeil Armstrong PHY_MODE_USB_HOST); 31331306821SNeil Armstrong 31431306821SNeil Armstrong if (ret) 31531306821SNeil Armstrong return ret; 31631306821SNeil Armstrong 31731306821SNeil Armstrong regmap_update_bits(priv->u2p_regmap[i], U2P_R0, 31831306821SNeil Armstrong U2P_R0_POWER_ON_RESET, 0); 31931306821SNeil Armstrong 32031306821SNeil Armstrong return 0; 321c9999337SNeil Armstrong } 322c9999337SNeil Armstrong 3235b0ba0caSNeil Armstrong static int dwc3_meson_g12a_usb2_init(struct dwc3_meson_g12a *priv, 3245b0ba0caSNeil Armstrong enum phy_mode mode) 325c9999337SNeil Armstrong { 32631306821SNeil Armstrong int i, ret; 327c9999337SNeil Armstrong 3285174564cSNeil Armstrong for (i = 0; i < priv->drvdata->num_phys; ++i) { 329c9999337SNeil Armstrong if (!priv->phys[i]) 330c9999337SNeil Armstrong continue; 331c9999337SNeil Armstrong 3325174564cSNeil Armstrong if (!strstr(priv->drvdata->phy_names[i], "usb2")) 3335174564cSNeil Armstrong continue; 3345174564cSNeil Armstrong 3355b0ba0caSNeil Armstrong ret = priv->drvdata->usb2_init_phy(priv, i, mode); 33631306821SNeil Armstrong if (ret) 33731306821SNeil Armstrong return ret; 338c9999337SNeil Armstrong } 339c9999337SNeil Armstrong 340c9999337SNeil Armstrong return 0; 341c9999337SNeil Armstrong } 342c9999337SNeil Armstrong 343c9999337SNeil Armstrong static void dwc3_meson_g12a_usb3_init(struct dwc3_meson_g12a *priv) 344c9999337SNeil Armstrong { 345013af227SNeil Armstrong regmap_update_bits(priv->usb_glue_regmap, USB_R3, 346c9999337SNeil Armstrong USB_R3_P30_SSC_RANGE_MASK | 347c9999337SNeil Armstrong USB_R3_P30_REF_SSP_EN, 348c9999337SNeil Armstrong USB_R3_P30_SSC_ENABLE | 349c9999337SNeil Armstrong FIELD_PREP(USB_R3_P30_SSC_RANGE_MASK, 2) | 350c9999337SNeil Armstrong USB_R3_P30_REF_SSP_EN); 351c9999337SNeil Armstrong udelay(2); 352c9999337SNeil Armstrong 353013af227SNeil Armstrong regmap_update_bits(priv->usb_glue_regmap, USB_R2, 354c9999337SNeil Armstrong USB_R2_P30_PCS_TX_DEEMPH_3P5DB_MASK, 355c9999337SNeil Armstrong FIELD_PREP(USB_R2_P30_PCS_TX_DEEMPH_3P5DB_MASK, 0x15)); 356c9999337SNeil Armstrong 357013af227SNeil Armstrong regmap_update_bits(priv->usb_glue_regmap, USB_R2, 358c9999337SNeil Armstrong USB_R2_P30_PCS_TX_DEEMPH_6DB_MASK, 359c9999337SNeil Armstrong FIELD_PREP(USB_R2_P30_PCS_TX_DEEMPH_6DB_MASK, 0x20)); 360c9999337SNeil Armstrong 361c9999337SNeil Armstrong udelay(2); 362c9999337SNeil Armstrong 363013af227SNeil Armstrong regmap_update_bits(priv->usb_glue_regmap, USB_R1, 364c9999337SNeil Armstrong USB_R1_U3H_HOST_PORT_POWER_CONTROL_PRESENT, 365c9999337SNeil Armstrong USB_R1_U3H_HOST_PORT_POWER_CONTROL_PRESENT); 366c9999337SNeil Armstrong 367013af227SNeil Armstrong regmap_update_bits(priv->usb_glue_regmap, USB_R1, 368c9999337SNeil Armstrong USB_R1_P30_PCS_TX_SWING_FULL_MASK, 369c9999337SNeil Armstrong FIELD_PREP(USB_R1_P30_PCS_TX_SWING_FULL_MASK, 127)); 370c9999337SNeil Armstrong } 371c9999337SNeil Armstrong 3725b0ba0caSNeil Armstrong static void dwc3_meson_g12a_usb_otg_apply_mode(struct dwc3_meson_g12a *priv, 3735b0ba0caSNeil Armstrong enum phy_mode mode) 374c9999337SNeil Armstrong { 3755b0ba0caSNeil Armstrong if (mode == PHY_MODE_USB_DEVICE) { 376df7e3745SNeil Armstrong if (priv->otg_mode != USB_DR_MODE_OTG && 377df7e3745SNeil Armstrong priv->drvdata->otg_phy_host_port_disable) 378df7e3745SNeil Armstrong /* Isolate the OTG PHY port from the Host Controller */ 379df7e3745SNeil Armstrong regmap_update_bits(priv->usb_glue_regmap, USB_R1, 380df7e3745SNeil Armstrong USB_R1_U3H_HOST_U2_PORT_DISABLE_MASK, 381df7e3745SNeil Armstrong FIELD_PREP(USB_R1_U3H_HOST_U2_PORT_DISABLE_MASK, 382df7e3745SNeil Armstrong BIT(USB2_OTG_PHY))); 383df7e3745SNeil Armstrong 384013af227SNeil Armstrong regmap_update_bits(priv->usb_glue_regmap, USB_R0, 385c9999337SNeil Armstrong USB_R0_U2D_ACT, USB_R0_U2D_ACT); 386013af227SNeil Armstrong regmap_update_bits(priv->usb_glue_regmap, USB_R0, 387c9999337SNeil Armstrong USB_R0_U2D_SS_SCALEDOWN_MODE_MASK, 0); 388013af227SNeil Armstrong regmap_update_bits(priv->usb_glue_regmap, USB_R4, 389c9999337SNeil Armstrong USB_R4_P21_SLEEP_M0, USB_R4_P21_SLEEP_M0); 390c9999337SNeil Armstrong } else { 391df7e3745SNeil Armstrong if (priv->otg_mode != USB_DR_MODE_OTG && 392df7e3745SNeil Armstrong priv->drvdata->otg_phy_host_port_disable) { 393df7e3745SNeil Armstrong regmap_update_bits(priv->usb_glue_regmap, USB_R1, 394df7e3745SNeil Armstrong USB_R1_U3H_HOST_U2_PORT_DISABLE_MASK, 0); 395df7e3745SNeil Armstrong msleep(500); 396df7e3745SNeil Armstrong } 397013af227SNeil Armstrong regmap_update_bits(priv->usb_glue_regmap, USB_R0, 398c9999337SNeil Armstrong USB_R0_U2D_ACT, 0); 399013af227SNeil Armstrong regmap_update_bits(priv->usb_glue_regmap, USB_R4, 400c9999337SNeil Armstrong USB_R4_P21_SLEEP_M0, 0); 401c9999337SNeil Armstrong } 402c9999337SNeil Armstrong } 403c9999337SNeil Armstrong 4045b0ba0caSNeil Armstrong static int dwc3_meson_g12a_usb_init_glue(struct dwc3_meson_g12a *priv, 4055b0ba0caSNeil Armstrong enum phy_mode mode) 406c9999337SNeil Armstrong { 407c9999337SNeil Armstrong int ret; 408c9999337SNeil Armstrong 4095b0ba0caSNeil Armstrong ret = dwc3_meson_g12a_usb2_init(priv, mode); 410c9999337SNeil Armstrong if (ret) 411c9999337SNeil Armstrong return ret; 412c9999337SNeil Armstrong 413013af227SNeil Armstrong regmap_update_bits(priv->usb_glue_regmap, USB_R1, 414c9999337SNeil Armstrong USB_R1_U3H_FLADJ_30MHZ_REG_MASK, 415c9999337SNeil Armstrong FIELD_PREP(USB_R1_U3H_FLADJ_30MHZ_REG_MASK, 0x20)); 416c9999337SNeil Armstrong 417013af227SNeil Armstrong regmap_update_bits(priv->usb_glue_regmap, USB_R5, 418c9999337SNeil Armstrong USB_R5_ID_DIG_EN_0, 419c9999337SNeil Armstrong USB_R5_ID_DIG_EN_0); 420013af227SNeil Armstrong regmap_update_bits(priv->usb_glue_regmap, USB_R5, 421c9999337SNeil Armstrong USB_R5_ID_DIG_EN_1, 422c9999337SNeil Armstrong USB_R5_ID_DIG_EN_1); 423013af227SNeil Armstrong regmap_update_bits(priv->usb_glue_regmap, USB_R5, 424c9999337SNeil Armstrong USB_R5_ID_DIG_TH_MASK, 425c9999337SNeil Armstrong FIELD_PREP(USB_R5_ID_DIG_TH_MASK, 0xff)); 426c9999337SNeil Armstrong 427c9999337SNeil Armstrong /* If we have an actual SuperSpeed port, initialize it */ 428c9999337SNeil Armstrong if (priv->usb3_ports) 429c9999337SNeil Armstrong dwc3_meson_g12a_usb3_init(priv); 430c9999337SNeil Armstrong 4315b0ba0caSNeil Armstrong dwc3_meson_g12a_usb_otg_apply_mode(priv, mode); 432c9999337SNeil Armstrong 433c9999337SNeil Armstrong return 0; 434c9999337SNeil Armstrong } 435c9999337SNeil Armstrong 436013af227SNeil Armstrong static const struct regmap_config phy_meson_g12a_usb_glue_regmap_conf = { 437013af227SNeil Armstrong .name = "usb-glue", 438c9999337SNeil Armstrong .reg_bits = 8, 439c9999337SNeil Armstrong .val_bits = 32, 440c9999337SNeil Armstrong .reg_stride = 4, 441c9999337SNeil Armstrong .max_register = USB_R5, 442c9999337SNeil Armstrong }; 443c9999337SNeil Armstrong 444c9999337SNeil Armstrong static int dwc3_meson_g12a_get_phys(struct dwc3_meson_g12a *priv) 445c9999337SNeil Armstrong { 4465174564cSNeil Armstrong const char *phy_name; 447c9999337SNeil Armstrong int i; 448c9999337SNeil Armstrong 4495174564cSNeil Armstrong for (i = 0 ; i < priv->drvdata->num_phys ; ++i) { 4505174564cSNeil Armstrong phy_name = priv->drvdata->phy_names[i]; 4515174564cSNeil Armstrong priv->phys[i] = devm_phy_optional_get(priv->dev, phy_name); 452c9999337SNeil Armstrong if (!priv->phys[i]) 453c9999337SNeil Armstrong continue; 454c9999337SNeil Armstrong 455c9999337SNeil Armstrong if (IS_ERR(priv->phys[i])) 456c9999337SNeil Armstrong return PTR_ERR(priv->phys[i]); 457c9999337SNeil Armstrong 4585174564cSNeil Armstrong if (strstr(phy_name, "usb3")) 459c9999337SNeil Armstrong priv->usb3_ports++; 460c9999337SNeil Armstrong else 461c9999337SNeil Armstrong priv->usb2_ports++; 462c9999337SNeil Armstrong } 463c9999337SNeil Armstrong 464c9999337SNeil Armstrong dev_info(priv->dev, "USB2 ports: %d\n", priv->usb2_ports); 465c9999337SNeil Armstrong dev_info(priv->dev, "USB3 ports: %d\n", priv->usb3_ports); 466c9999337SNeil Armstrong 467c9999337SNeil Armstrong return 0; 468c9999337SNeil Armstrong } 469c9999337SNeil Armstrong 470c9999337SNeil Armstrong static enum phy_mode dwc3_meson_g12a_get_id(struct dwc3_meson_g12a *priv) 471c9999337SNeil Armstrong { 472c9999337SNeil Armstrong u32 reg; 473c9999337SNeil Armstrong 474013af227SNeil Armstrong regmap_read(priv->usb_glue_regmap, USB_R5, ®); 475c9999337SNeil Armstrong 476c9999337SNeil Armstrong if (reg & (USB_R5_ID_DIG_SYNC | USB_R5_ID_DIG_REG)) 477c9999337SNeil Armstrong return PHY_MODE_USB_DEVICE; 478c9999337SNeil Armstrong 479c9999337SNeil Armstrong return PHY_MODE_USB_HOST; 480c9999337SNeil Armstrong } 481c9999337SNeil Armstrong 482c9999337SNeil Armstrong static int dwc3_meson_g12a_otg_mode_set(struct dwc3_meson_g12a *priv, 483c9999337SNeil Armstrong enum phy_mode mode) 484c9999337SNeil Armstrong { 485c9999337SNeil Armstrong int ret; 486c9999337SNeil Armstrong 4876bae03b0SDmitry Rokosov if (!priv->phys[USB2_OTG_PHY]) 488c9999337SNeil Armstrong return -EINVAL; 489c9999337SNeil Armstrong 490c9999337SNeil Armstrong if (mode == PHY_MODE_USB_HOST) 491c9999337SNeil Armstrong dev_info(priv->dev, "switching to Host Mode\n"); 492c9999337SNeil Armstrong else 493c9999337SNeil Armstrong dev_info(priv->dev, "switching to Device Mode\n"); 494c9999337SNeil Armstrong 495c9999337SNeil Armstrong if (priv->vbus) { 496c9999337SNeil Armstrong if (mode == PHY_MODE_USB_DEVICE) 497c9999337SNeil Armstrong ret = regulator_disable(priv->vbus); 498c9999337SNeil Armstrong else 499c9999337SNeil Armstrong ret = regulator_enable(priv->vbus); 500c9999337SNeil Armstrong if (ret) 501c9999337SNeil Armstrong return ret; 502c9999337SNeil Armstrong } 503c9999337SNeil Armstrong 504c9999337SNeil Armstrong priv->otg_phy_mode = mode; 505c9999337SNeil Armstrong 50631306821SNeil Armstrong ret = priv->drvdata->set_phy_mode(priv, USB2_OTG_PHY, mode); 50731306821SNeil Armstrong if (ret) 50831306821SNeil Armstrong return ret; 509c9999337SNeil Armstrong 5105b0ba0caSNeil Armstrong dwc3_meson_g12a_usb_otg_apply_mode(priv, mode); 511c9999337SNeil Armstrong 512c9999337SNeil Armstrong return 0; 513c9999337SNeil Armstrong } 514c9999337SNeil Armstrong 515bce3052fSHeikki Krogerus static int dwc3_meson_g12a_role_set(struct usb_role_switch *sw, 516bce3052fSHeikki Krogerus enum usb_role role) 517c9999337SNeil Armstrong { 518bce3052fSHeikki Krogerus struct dwc3_meson_g12a *priv = usb_role_switch_get_drvdata(sw); 519c9999337SNeil Armstrong enum phy_mode mode; 520c9999337SNeil Armstrong 521c9999337SNeil Armstrong if (role == USB_ROLE_NONE) 522c9999337SNeil Armstrong return 0; 523c9999337SNeil Armstrong 524c9999337SNeil Armstrong mode = (role == USB_ROLE_HOST) ? PHY_MODE_USB_HOST 525c9999337SNeil Armstrong : PHY_MODE_USB_DEVICE; 526c9999337SNeil Armstrong 527c9999337SNeil Armstrong if (mode == priv->otg_phy_mode) 528c9999337SNeil Armstrong return 0; 529c9999337SNeil Armstrong 530df7e3745SNeil Armstrong if (priv->drvdata->otg_phy_host_port_disable) 531e5ee93d4SFelipe Balbi dev_warn_once(priv->dev, "Broken manual OTG switch\n"); 532df7e3745SNeil Armstrong 533c9999337SNeil Armstrong return dwc3_meson_g12a_otg_mode_set(priv, mode); 534c9999337SNeil Armstrong } 535c9999337SNeil Armstrong 536bce3052fSHeikki Krogerus static enum usb_role dwc3_meson_g12a_role_get(struct usb_role_switch *sw) 537c9999337SNeil Armstrong { 538bce3052fSHeikki Krogerus struct dwc3_meson_g12a *priv = usb_role_switch_get_drvdata(sw); 539c9999337SNeil Armstrong 540c9999337SNeil Armstrong return priv->otg_phy_mode == PHY_MODE_USB_HOST ? 541c9999337SNeil Armstrong USB_ROLE_HOST : USB_ROLE_DEVICE; 542c9999337SNeil Armstrong } 543c9999337SNeil Armstrong 544f90db107SNeil Armstrong static irqreturn_t dwc3_meson_g12a_irq_thread(int irq, void *data) 545f90db107SNeil Armstrong { 546f90db107SNeil Armstrong struct dwc3_meson_g12a *priv = data; 547f90db107SNeil Armstrong enum phy_mode otg_id; 548f90db107SNeil Armstrong 549f90db107SNeil Armstrong otg_id = dwc3_meson_g12a_get_id(priv); 550f90db107SNeil Armstrong if (otg_id != priv->otg_phy_mode) { 551f90db107SNeil Armstrong if (dwc3_meson_g12a_otg_mode_set(priv, otg_id)) 552f90db107SNeil Armstrong dev_warn(priv->dev, "Failed to switch OTG mode\n"); 553f90db107SNeil Armstrong } 554f90db107SNeil Armstrong 555013af227SNeil Armstrong regmap_update_bits(priv->usb_glue_regmap, USB_R5, 556013af227SNeil Armstrong USB_R5_ID_DIG_IRQ, 0); 557f90db107SNeil Armstrong 558f90db107SNeil Armstrong return IRQ_HANDLED; 559f90db107SNeil Armstrong } 560f90db107SNeil Armstrong 561c9999337SNeil Armstrong static struct device *dwc3_meson_g12_find_child(struct device *dev, 562c9999337SNeil Armstrong const char *compatible) 563c9999337SNeil Armstrong { 564c9999337SNeil Armstrong struct platform_device *pdev; 565c9999337SNeil Armstrong struct device_node *np; 566c9999337SNeil Armstrong 567c9999337SNeil Armstrong np = of_get_compatible_child(dev->of_node, compatible); 568c9999337SNeil Armstrong if (!np) 569c9999337SNeil Armstrong return NULL; 570c9999337SNeil Armstrong 571c9999337SNeil Armstrong pdev = of_find_device_by_node(np); 572c9999337SNeil Armstrong of_node_put(np); 573c9999337SNeil Armstrong if (!pdev) 574c9999337SNeil Armstrong return NULL; 575c9999337SNeil Armstrong 576c9999337SNeil Armstrong return &pdev->dev; 577c9999337SNeil Armstrong } 578c9999337SNeil Armstrong 5791e355f21SHanjie Lin static int dwc3_meson_g12a_otg_init(struct platform_device *pdev, 5801e355f21SHanjie Lin struct dwc3_meson_g12a *priv) 581c9999337SNeil Armstrong { 582c9999337SNeil Armstrong enum phy_mode otg_id; 5831e355f21SHanjie Lin int ret, irq; 5841e355f21SHanjie Lin struct device *dev = &pdev->dev; 585c9999337SNeil Armstrong 586f90db107SNeil Armstrong if (priv->otg_mode == USB_DR_MODE_OTG) { 587f90db107SNeil Armstrong /* Ack irq before registering */ 588013af227SNeil Armstrong regmap_update_bits(priv->usb_glue_regmap, USB_R5, 589f90db107SNeil Armstrong USB_R5_ID_DIG_IRQ, 0); 590f90db107SNeil Armstrong 591f90db107SNeil Armstrong irq = platform_get_irq(pdev, 0); 592baa2986bSSergey Shtylyov if (irq < 0) 593baa2986bSSergey Shtylyov return irq; 594f90db107SNeil Armstrong ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, 595f90db107SNeil Armstrong dwc3_meson_g12a_irq_thread, 596f90db107SNeil Armstrong IRQF_ONESHOT, pdev->name, priv); 597f90db107SNeil Armstrong if (ret) 598f90db107SNeil Armstrong return ret; 599f90db107SNeil Armstrong } 600f90db107SNeil Armstrong 601c9999337SNeil Armstrong /* Setup OTG mode corresponding to the ID pin */ 602c9999337SNeil Armstrong if (priv->otg_mode == USB_DR_MODE_OTG) { 603c9999337SNeil Armstrong otg_id = dwc3_meson_g12a_get_id(priv); 604c9999337SNeil Armstrong if (otg_id != priv->otg_phy_mode) { 605c9999337SNeil Armstrong if (dwc3_meson_g12a_otg_mode_set(priv, otg_id)) 606c9999337SNeil Armstrong dev_warn(dev, "Failed to switch OTG mode\n"); 607c9999337SNeil Armstrong } 608c9999337SNeil Armstrong } 609c9999337SNeil Armstrong 610c9999337SNeil Armstrong /* Setup role switcher */ 611c9999337SNeil Armstrong priv->switch_desc.usb2_port = dwc3_meson_g12_find_child(dev, 612c9999337SNeil Armstrong "snps,dwc3"); 613c9999337SNeil Armstrong priv->switch_desc.udc = dwc3_meson_g12_find_child(dev, "snps,dwc2"); 614c9999337SNeil Armstrong priv->switch_desc.allow_userspace_control = true; 615c9999337SNeil Armstrong priv->switch_desc.set = dwc3_meson_g12a_role_set; 616c9999337SNeil Armstrong priv->switch_desc.get = dwc3_meson_g12a_role_get; 617bce3052fSHeikki Krogerus priv->switch_desc.driver_data = priv; 618c9999337SNeil Armstrong 619c9999337SNeil Armstrong priv->role_switch = usb_role_switch_register(dev, &priv->switch_desc); 620c9999337SNeil Armstrong if (IS_ERR(priv->role_switch)) 621c9999337SNeil Armstrong dev_warn(dev, "Unable to register Role Switch\n"); 622c9999337SNeil Armstrong 623238d7602SNathan Chancellor return 0; 6241e355f21SHanjie Lin } 6251e355f21SHanjie Lin 626a9fc15e0SNeil Armstrong static int dwc3_meson_gxl_setup_regmaps(struct dwc3_meson_g12a *priv, 627a9fc15e0SNeil Armstrong void __iomem *base) 628a9fc15e0SNeil Armstrong { 629a9fc15e0SNeil Armstrong /* GXL controls the PHY mode in the PHY registers unlike G12A */ 630a9fc15e0SNeil Armstrong priv->usb_glue_regmap = devm_regmap_init_mmio(priv->dev, base, 631a9fc15e0SNeil Armstrong &phy_meson_g12a_usb_glue_regmap_conf); 632a793cf81SFelipe Balbi return PTR_ERR_OR_ZERO(priv->usb_glue_regmap); 633a9fc15e0SNeil Armstrong } 634a9fc15e0SNeil Armstrong 635013af227SNeil Armstrong static int dwc3_meson_g12a_setup_regmaps(struct dwc3_meson_g12a *priv, 636013af227SNeil Armstrong void __iomem *base) 637013af227SNeil Armstrong { 638013af227SNeil Armstrong int i; 639013af227SNeil Armstrong 640013af227SNeil Armstrong priv->usb_glue_regmap = devm_regmap_init_mmio(priv->dev, 641013af227SNeil Armstrong base + G12A_GLUE_OFFSET, 642013af227SNeil Armstrong &phy_meson_g12a_usb_glue_regmap_conf); 643013af227SNeil Armstrong if (IS_ERR(priv->usb_glue_regmap)) 644013af227SNeil Armstrong return PTR_ERR(priv->usb_glue_regmap); 645013af227SNeil Armstrong 646013af227SNeil Armstrong /* Create a regmap for each USB2 PHY control register set */ 6474d2aa178SNeil Armstrong for (i = 0; i < priv->drvdata->num_phys; i++) { 648013af227SNeil Armstrong struct regmap_config u2p_regmap_config = { 649013af227SNeil Armstrong .reg_bits = 8, 650013af227SNeil Armstrong .val_bits = 32, 651013af227SNeil Armstrong .reg_stride = 4, 652013af227SNeil Armstrong .max_register = U2P_R1, 653013af227SNeil Armstrong }; 654013af227SNeil Armstrong 6554d2aa178SNeil Armstrong if (!strstr(priv->drvdata->phy_names[i], "usb2")) 6564d2aa178SNeil Armstrong continue; 6574d2aa178SNeil Armstrong 658013af227SNeil Armstrong u2p_regmap_config.name = devm_kasprintf(priv->dev, GFP_KERNEL, 659013af227SNeil Armstrong "u2p-%d", i); 660013af227SNeil Armstrong if (!u2p_regmap_config.name) 661013af227SNeil Armstrong return -ENOMEM; 662013af227SNeil Armstrong 663013af227SNeil Armstrong priv->u2p_regmap[i] = devm_regmap_init_mmio(priv->dev, 664013af227SNeil Armstrong base + (i * U2P_REG_SIZE), 665013af227SNeil Armstrong &u2p_regmap_config); 666013af227SNeil Armstrong if (IS_ERR(priv->u2p_regmap[i])) 667013af227SNeil Armstrong return PTR_ERR(priv->u2p_regmap[i]); 668013af227SNeil Armstrong } 669013af227SNeil Armstrong 670013af227SNeil Armstrong return 0; 671013af227SNeil Armstrong } 672013af227SNeil Armstrong 6735b0ba0caSNeil Armstrong static int dwc3_meson_g12a_usb_init(struct dwc3_meson_g12a *priv) 6745b0ba0caSNeil Armstrong { 6755b0ba0caSNeil Armstrong return dwc3_meson_g12a_usb_init_glue(priv, priv->otg_phy_mode); 6765b0ba0caSNeil Armstrong } 6775b0ba0caSNeil Armstrong 678a9fc15e0SNeil Armstrong static int dwc3_meson_gxl_usb_init(struct dwc3_meson_g12a *priv) 679a9fc15e0SNeil Armstrong { 680a9fc15e0SNeil Armstrong return dwc3_meson_g12a_usb_init_glue(priv, PHY_MODE_USB_DEVICE); 681a9fc15e0SNeil Armstrong } 682a9fc15e0SNeil Armstrong 683a9fc15e0SNeil Armstrong static int dwc3_meson_gxl_usb_post_init(struct dwc3_meson_g12a *priv) 684a9fc15e0SNeil Armstrong { 685a9fc15e0SNeil Armstrong int ret; 686a9fc15e0SNeil Armstrong 687a9fc15e0SNeil Armstrong ret = priv->drvdata->set_phy_mode(priv, USB2_OTG_PHY, 688a9fc15e0SNeil Armstrong priv->otg_phy_mode); 689a9fc15e0SNeil Armstrong if (ret) 690a9fc15e0SNeil Armstrong return ret; 691a9fc15e0SNeil Armstrong 692a9fc15e0SNeil Armstrong dwc3_meson_g12a_usb_otg_apply_mode(priv, priv->otg_phy_mode); 693a9fc15e0SNeil Armstrong 694a9fc15e0SNeil Armstrong return 0; 695a9fc15e0SNeil Armstrong } 696a9fc15e0SNeil Armstrong 6971e355f21SHanjie Lin static int dwc3_meson_g12a_probe(struct platform_device *pdev) 6981e355f21SHanjie Lin { 6991e355f21SHanjie Lin struct dwc3_meson_g12a *priv; 7001e355f21SHanjie Lin struct device *dev = &pdev->dev; 7011e355f21SHanjie Lin struct device_node *np = dev->of_node; 7021e355f21SHanjie Lin void __iomem *base; 7031e355f21SHanjie Lin int ret, i; 7041e355f21SHanjie Lin 7051e355f21SHanjie Lin priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 7061e355f21SHanjie Lin if (!priv) 7071e355f21SHanjie Lin return -ENOMEM; 7081e355f21SHanjie Lin 7091e355f21SHanjie Lin base = devm_platform_ioremap_resource(pdev, 0); 7101e355f21SHanjie Lin if (IS_ERR(base)) 7111e355f21SHanjie Lin return PTR_ERR(base); 7121e355f21SHanjie Lin 713013af227SNeil Armstrong priv->drvdata = of_device_get_match_data(&pdev->dev); 714013af227SNeil Armstrong priv->dev = dev; 7151e355f21SHanjie Lin 7161e355f21SHanjie Lin priv->vbus = devm_regulator_get_optional(dev, "vbus"); 7171e355f21SHanjie Lin if (IS_ERR(priv->vbus)) { 7181e355f21SHanjie Lin if (PTR_ERR(priv->vbus) == -EPROBE_DEFER) 7191e355f21SHanjie Lin return PTR_ERR(priv->vbus); 7201e355f21SHanjie Lin priv->vbus = NULL; 7211e355f21SHanjie Lin } 7221e355f21SHanjie Lin 7231e355f21SHanjie Lin ret = devm_clk_bulk_get(dev, 7241e355f21SHanjie Lin priv->drvdata->num_clks, 7251e355f21SHanjie Lin priv->drvdata->clks); 7261e355f21SHanjie Lin if (ret) 7271e355f21SHanjie Lin return ret; 7281e355f21SHanjie Lin 7291e355f21SHanjie Lin ret = clk_bulk_prepare_enable(priv->drvdata->num_clks, 7301e355f21SHanjie Lin priv->drvdata->clks); 7311e355f21SHanjie Lin if (ret) 7321e355f21SHanjie Lin return ret; 7331e355f21SHanjie Lin 7341e355f21SHanjie Lin platform_set_drvdata(pdev, priv); 7351e355f21SHanjie Lin 7366d9fa35aSNeil Armstrong priv->reset = devm_reset_control_get_shared(dev, NULL); 7371e355f21SHanjie Lin if (IS_ERR(priv->reset)) { 7381e355f21SHanjie Lin ret = PTR_ERR(priv->reset); 7391e355f21SHanjie Lin dev_err(dev, "failed to get device reset, err=%d\n", ret); 740be8c1001SMartin Blumenstingl goto err_disable_clks; 7411e355f21SHanjie Lin } 7421e355f21SHanjie Lin 743a6498d51SAmjad Ouled-Ameur ret = reset_control_reset(priv->reset); 7441e355f21SHanjie Lin if (ret) 745a6498d51SAmjad Ouled-Ameur goto err_disable_clks; 7461e355f21SHanjie Lin 7471e355f21SHanjie Lin ret = dwc3_meson_g12a_get_phys(priv); 7481e355f21SHanjie Lin if (ret) 7494ce3b457SAmjad Ouled-Ameur goto err_rearm; 7501e355f21SHanjie Lin 751347052e3SMartin Blumenstingl ret = priv->drvdata->setup_regmaps(priv, base); 752347052e3SMartin Blumenstingl if (ret) 7534ce3b457SAmjad Ouled-Ameur goto err_rearm; 754347052e3SMartin Blumenstingl 7551e355f21SHanjie Lin if (priv->vbus) { 7561e355f21SHanjie Lin ret = regulator_enable(priv->vbus); 7571e355f21SHanjie Lin if (ret) 7584ce3b457SAmjad Ouled-Ameur goto err_rearm; 7591e355f21SHanjie Lin } 7601e355f21SHanjie Lin 7611e355f21SHanjie Lin /* Get dr_mode */ 7621e355f21SHanjie Lin priv->otg_mode = usb_get_dr_mode(dev); 7631e355f21SHanjie Lin 7645b0ba0caSNeil Armstrong if (priv->otg_mode == USB_DR_MODE_PERIPHERAL) 7655b0ba0caSNeil Armstrong priv->otg_phy_mode = PHY_MODE_USB_DEVICE; 7665b0ba0caSNeil Armstrong else 7675b0ba0caSNeil Armstrong priv->otg_phy_mode = PHY_MODE_USB_HOST; 7685b0ba0caSNeil Armstrong 7695b0ba0caSNeil Armstrong ret = priv->drvdata->usb_init(priv); 7708f5bc1ecSNeil Armstrong if (ret) 7711d0d3d81SChristophe JAILLET goto err_disable_regulator; 7721e355f21SHanjie Lin 7731e355f21SHanjie Lin /* Init PHYs */ 7741e355f21SHanjie Lin for (i = 0 ; i < PHY_COUNT ; ++i) { 7751e355f21SHanjie Lin ret = phy_init(priv->phys[i]); 7761e355f21SHanjie Lin if (ret) 7771d0d3d81SChristophe JAILLET goto err_disable_regulator; 7781e355f21SHanjie Lin } 7791e355f21SHanjie Lin 7801e355f21SHanjie Lin /* Set PHY Power */ 7811e355f21SHanjie Lin for (i = 0 ; i < PHY_COUNT ; ++i) { 7821e355f21SHanjie Lin ret = phy_power_on(priv->phys[i]); 7831e355f21SHanjie Lin if (ret) 7841e355f21SHanjie Lin goto err_phys_exit; 7851e355f21SHanjie Lin } 7861e355f21SHanjie Lin 7875b0ba0caSNeil Armstrong if (priv->drvdata->usb_post_init) { 7885b0ba0caSNeil Armstrong ret = priv->drvdata->usb_post_init(priv); 7895b0ba0caSNeil Armstrong if (ret) 7905b0ba0caSNeil Armstrong goto err_phys_power; 7915b0ba0caSNeil Armstrong } 7925b0ba0caSNeil Armstrong 7931e355f21SHanjie Lin ret = of_platform_populate(np, NULL, NULL, dev); 7941e355f21SHanjie Lin if (ret) 7951e355f21SHanjie Lin goto err_phys_power; 7961e355f21SHanjie Lin 7971e355f21SHanjie Lin ret = dwc3_meson_g12a_otg_init(pdev, priv); 7981e355f21SHanjie Lin if (ret) 799*01052b91SChristophe JAILLET goto err_plat_depopulate; 8001e355f21SHanjie Lin 801c9999337SNeil Armstrong pm_runtime_set_active(dev); 802c9999337SNeil Armstrong pm_runtime_enable(dev); 803c9999337SNeil Armstrong pm_runtime_get_sync(dev); 804c9999337SNeil Armstrong 805c9999337SNeil Armstrong return 0; 806c9999337SNeil Armstrong 807*01052b91SChristophe JAILLET err_plat_depopulate: 808*01052b91SChristophe JAILLET of_platform_depopulate(dev); 809*01052b91SChristophe JAILLET 810c9999337SNeil Armstrong err_phys_power: 811c9999337SNeil Armstrong for (i = 0 ; i < PHY_COUNT ; ++i) 812c9999337SNeil Armstrong phy_power_off(priv->phys[i]); 813c9999337SNeil Armstrong 814c9999337SNeil Armstrong err_phys_exit: 815c9999337SNeil Armstrong for (i = 0 ; i < PHY_COUNT ; ++i) 816c9999337SNeil Armstrong phy_exit(priv->phys[i]); 817c9999337SNeil Armstrong 8181d0d3d81SChristophe JAILLET err_disable_regulator: 8191d0d3d81SChristophe JAILLET if (priv->vbus) 8201d0d3d81SChristophe JAILLET regulator_disable(priv->vbus); 8211d0d3d81SChristophe JAILLET 8224ce3b457SAmjad Ouled-Ameur err_rearm: 8234ce3b457SAmjad Ouled-Ameur reset_control_rearm(priv->reset); 8244ce3b457SAmjad Ouled-Ameur 8251e355f21SHanjie Lin err_disable_clks: 8261e355f21SHanjie Lin clk_bulk_disable_unprepare(priv->drvdata->num_clks, 8271e355f21SHanjie Lin priv->drvdata->clks); 8281e355f21SHanjie Lin 829c9999337SNeil Armstrong return ret; 830c9999337SNeil Armstrong } 831c9999337SNeil Armstrong 8323ffea6e0SUwe Kleine-König static void dwc3_meson_g12a_remove(struct platform_device *pdev) 833c9999337SNeil Armstrong { 834c9999337SNeil Armstrong struct dwc3_meson_g12a *priv = platform_get_drvdata(pdev); 835c9999337SNeil Armstrong struct device *dev = &pdev->dev; 836c9999337SNeil Armstrong int i; 837c9999337SNeil Armstrong 838c9999337SNeil Armstrong usb_role_switch_unregister(priv->role_switch); 839c9999337SNeil Armstrong 840c9999337SNeil Armstrong of_platform_depopulate(dev); 841c9999337SNeil Armstrong 842c9999337SNeil Armstrong for (i = 0 ; i < PHY_COUNT ; ++i) { 843c9999337SNeil Armstrong phy_power_off(priv->phys[i]); 844c9999337SNeil Armstrong phy_exit(priv->phys[i]); 845c9999337SNeil Armstrong } 846c9999337SNeil Armstrong 847c9999337SNeil Armstrong pm_runtime_disable(dev); 848c9999337SNeil Armstrong pm_runtime_put_noidle(dev); 849c9999337SNeil Armstrong pm_runtime_set_suspended(dev); 850c9999337SNeil Armstrong 8514ce3b457SAmjad Ouled-Ameur reset_control_rearm(priv->reset); 8524ce3b457SAmjad Ouled-Ameur 8531e355f21SHanjie Lin clk_bulk_disable_unprepare(priv->drvdata->num_clks, 8541e355f21SHanjie Lin priv->drvdata->clks); 855c9999337SNeil Armstrong } 856c9999337SNeil Armstrong 857c9999337SNeil Armstrong static int __maybe_unused dwc3_meson_g12a_runtime_suspend(struct device *dev) 858c9999337SNeil Armstrong { 859c9999337SNeil Armstrong struct dwc3_meson_g12a *priv = dev_get_drvdata(dev); 860c9999337SNeil Armstrong 8611e355f21SHanjie Lin clk_bulk_disable_unprepare(priv->drvdata->num_clks, 8621e355f21SHanjie Lin priv->drvdata->clks); 863c9999337SNeil Armstrong 864c9999337SNeil Armstrong return 0; 865c9999337SNeil Armstrong } 866c9999337SNeil Armstrong 867c9999337SNeil Armstrong static int __maybe_unused dwc3_meson_g12a_runtime_resume(struct device *dev) 868c9999337SNeil Armstrong { 869c9999337SNeil Armstrong struct dwc3_meson_g12a *priv = dev_get_drvdata(dev); 870c9999337SNeil Armstrong 8711e355f21SHanjie Lin return clk_bulk_prepare_enable(priv->drvdata->num_clks, 8721e355f21SHanjie Lin priv->drvdata->clks); 873c9999337SNeil Armstrong } 874c9999337SNeil Armstrong 875c9999337SNeil Armstrong static int __maybe_unused dwc3_meson_g12a_suspend(struct device *dev) 876c9999337SNeil Armstrong { 877c9999337SNeil Armstrong struct dwc3_meson_g12a *priv = dev_get_drvdata(dev); 8781cf084d1SNeil Armstrong int i, ret; 8791cf084d1SNeil Armstrong 8801cf084d1SNeil Armstrong if (priv->vbus && priv->otg_phy_mode == PHY_MODE_USB_HOST) { 8811cf084d1SNeil Armstrong ret = regulator_disable(priv->vbus); 8821cf084d1SNeil Armstrong if (ret) 8831cf084d1SNeil Armstrong return ret; 8841cf084d1SNeil Armstrong } 885c9999337SNeil Armstrong 886c9999337SNeil Armstrong for (i = 0 ; i < PHY_COUNT ; ++i) { 887c9999337SNeil Armstrong phy_power_off(priv->phys[i]); 888c9999337SNeil Armstrong phy_exit(priv->phys[i]); 889c9999337SNeil Armstrong } 890c9999337SNeil Armstrong 8914ce3b457SAmjad Ouled-Ameur reset_control_rearm(priv->reset); 892c9999337SNeil Armstrong 893c9999337SNeil Armstrong return 0; 894c9999337SNeil Armstrong } 895c9999337SNeil Armstrong 896c9999337SNeil Armstrong static int __maybe_unused dwc3_meson_g12a_resume(struct device *dev) 897c9999337SNeil Armstrong { 898c9999337SNeil Armstrong struct dwc3_meson_g12a *priv = dev_get_drvdata(dev); 899c9999337SNeil Armstrong int i, ret; 900c9999337SNeil Armstrong 9014ce3b457SAmjad Ouled-Ameur ret = reset_control_reset(priv->reset); 9024ce3b457SAmjad Ouled-Ameur if (ret) 9034ce3b457SAmjad Ouled-Ameur return ret; 904c9999337SNeil Armstrong 9055b0ba0caSNeil Armstrong ret = priv->drvdata->usb_init(priv); 9065b0ba0caSNeil Armstrong if (ret) 9075b0ba0caSNeil Armstrong return ret; 908c9999337SNeil Armstrong 909c9999337SNeil Armstrong /* Init PHYs */ 910c9999337SNeil Armstrong for (i = 0 ; i < PHY_COUNT ; ++i) { 911c9999337SNeil Armstrong ret = phy_init(priv->phys[i]); 912c9999337SNeil Armstrong if (ret) 913c9999337SNeil Armstrong return ret; 914c9999337SNeil Armstrong } 915c9999337SNeil Armstrong 916c9999337SNeil Armstrong /* Set PHY Power */ 917c9999337SNeil Armstrong for (i = 0 ; i < PHY_COUNT ; ++i) { 918c9999337SNeil Armstrong ret = phy_power_on(priv->phys[i]); 919c9999337SNeil Armstrong if (ret) 920c9999337SNeil Armstrong return ret; 921c9999337SNeil Armstrong } 922c9999337SNeil Armstrong 9231cf084d1SNeil Armstrong if (priv->vbus && priv->otg_phy_mode == PHY_MODE_USB_HOST) { 9241cf084d1SNeil Armstrong ret = regulator_enable(priv->vbus); 9251cf084d1SNeil Armstrong if (ret) 9261cf084d1SNeil Armstrong return ret; 9271cf084d1SNeil Armstrong } 9281cf084d1SNeil Armstrong 929c9999337SNeil Armstrong return 0; 930c9999337SNeil Armstrong } 931c9999337SNeil Armstrong 932c9999337SNeil Armstrong static const struct dev_pm_ops dwc3_meson_g12a_dev_pm_ops = { 933c9999337SNeil Armstrong SET_SYSTEM_SLEEP_PM_OPS(dwc3_meson_g12a_suspend, dwc3_meson_g12a_resume) 934c9999337SNeil Armstrong SET_RUNTIME_PM_OPS(dwc3_meson_g12a_runtime_suspend, 935c9999337SNeil Armstrong dwc3_meson_g12a_runtime_resume, NULL) 936c9999337SNeil Armstrong }; 937c9999337SNeil Armstrong 938c9999337SNeil Armstrong static const struct of_device_id dwc3_meson_g12a_match[] = { 9391e355f21SHanjie Lin { 940a9fc15e0SNeil Armstrong .compatible = "amlogic,meson-gxl-usb-ctrl", 941a9fc15e0SNeil Armstrong .data = &gxl_drvdata, 942a9fc15e0SNeil Armstrong }, 943a9fc15e0SNeil Armstrong { 944a9fc15e0SNeil Armstrong .compatible = "amlogic,meson-gxm-usb-ctrl", 945a9fc15e0SNeil Armstrong .data = &gxm_drvdata, 946a9fc15e0SNeil Armstrong }, 947a9fc15e0SNeil Armstrong { 94865f3d449SNeil Armstrong .compatible = "amlogic,meson-axg-usb-ctrl", 94965f3d449SNeil Armstrong .data = &axg_drvdata, 95065f3d449SNeil Armstrong }, 95165f3d449SNeil Armstrong { 9521e355f21SHanjie Lin .compatible = "amlogic,meson-g12a-usb-ctrl", 9531e355f21SHanjie Lin .data = &g12a_drvdata, 9541e355f21SHanjie Lin }, 9551e355f21SHanjie Lin { 9561e355f21SHanjie Lin .compatible = "amlogic,meson-a1-usb-ctrl", 9571e355f21SHanjie Lin .data = &a1_drvdata, 9581e355f21SHanjie Lin }, 959c9999337SNeil Armstrong { /* Sentinel */ } 960c9999337SNeil Armstrong }; 961c9999337SNeil Armstrong MODULE_DEVICE_TABLE(of, dwc3_meson_g12a_match); 962c9999337SNeil Armstrong 963c9999337SNeil Armstrong static struct platform_driver dwc3_meson_g12a_driver = { 964c9999337SNeil Armstrong .probe = dwc3_meson_g12a_probe, 9653ffea6e0SUwe Kleine-König .remove_new = dwc3_meson_g12a_remove, 966c9999337SNeil Armstrong .driver = { 967c9999337SNeil Armstrong .name = "dwc3-meson-g12a", 968c9999337SNeil Armstrong .of_match_table = dwc3_meson_g12a_match, 969c9999337SNeil Armstrong .pm = &dwc3_meson_g12a_dev_pm_ops, 970c9999337SNeil Armstrong }, 971c9999337SNeil Armstrong }; 972c9999337SNeil Armstrong 973c9999337SNeil Armstrong module_platform_driver(dwc3_meson_g12a_driver); 974c9999337SNeil Armstrong MODULE_LICENSE("GPL v2"); 975c9999337SNeil Armstrong MODULE_DESCRIPTION("Amlogic Meson G12A USB Glue Layer"); 976c9999337SNeil Armstrong MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>"); 977