Greg Kroah-Hartman | 5fd54ac | 2017-11-03 11:28:30 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 2 | /* |
| 3 | * MediaTek xHCI Host Controller Driver |
| 4 | * |
| 5 | * Copyright (c) 2015 MediaTek Inc. |
| 6 | * Author: |
| 7 | * Chunfeng Yun <chunfeng.yun@mediatek.com> |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 8 | */ |
| 9 | |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 10 | #include <linux/dma-mapping.h> |
| 11 | #include <linux/iopoll.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/mfd/syscon.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/of.h> |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/pm_runtime.h> |
Chunfeng Yun | 04284eb | 2021-04-10 13:10:04 +0800 | [diff] [blame] | 18 | #include <linux/pm_wakeirq.h> |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 19 | #include <linux/regmap.h> |
| 20 | #include <linux/regulator/consumer.h> |
| 21 | |
| 22 | #include "xhci.h" |
| 23 | #include "xhci-mtk.h" |
| 24 | |
| 25 | /* ip_pw_ctrl0 register */ |
| 26 | #define CTRL0_IP_SW_RST BIT(0) |
| 27 | |
| 28 | /* ip_pw_ctrl1 register */ |
| 29 | #define CTRL1_IP_HOST_PDN BIT(0) |
| 30 | |
| 31 | /* ip_pw_ctrl2 register */ |
| 32 | #define CTRL2_IP_DEV_PDN BIT(0) |
| 33 | |
| 34 | /* ip_pw_sts1 register */ |
| 35 | #define STS1_IP_SLEEP_STS BIT(30) |
Chunfeng Yun | ce370bf | 2017-10-13 16:26:35 +0800 | [diff] [blame] | 36 | #define STS1_U3_MAC_RST BIT(16) |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 37 | #define STS1_XHCI_RST BIT(11) |
| 38 | #define STS1_SYS125_RST BIT(10) |
| 39 | #define STS1_REF_RST BIT(8) |
| 40 | #define STS1_SYSPLL_STABLE BIT(0) |
| 41 | |
| 42 | /* ip_xhci_cap register */ |
| 43 | #define CAP_U3_PORT_NUM(p) ((p) & 0xff) |
| 44 | #define CAP_U2_PORT_NUM(p) (((p) >> 8) & 0xff) |
| 45 | |
| 46 | /* u3_ctrl_p register */ |
| 47 | #define CTRL_U3_PORT_HOST_SEL BIT(2) |
| 48 | #define CTRL_U3_PORT_PDN BIT(1) |
| 49 | #define CTRL_U3_PORT_DIS BIT(0) |
| 50 | |
| 51 | /* u2_ctrl_p register */ |
| 52 | #define CTRL_U2_PORT_HOST_SEL BIT(2) |
| 53 | #define CTRL_U2_PORT_PDN BIT(1) |
| 54 | #define CTRL_U2_PORT_DIS BIT(0) |
| 55 | |
| 56 | /* u2_phy_pll register */ |
| 57 | #define CTRL_U2_FORCE_PLL_STB BIT(28) |
| 58 | |
Chunfeng Yun | 926d60a | 2021-08-17 16:36:29 +0800 | [diff] [blame] | 59 | /* xHCI CSR */ |
| 60 | #define LS_EOF_CFG 0x930 |
| 61 | #define LSEOF_OFFSET 0x89 |
| 62 | |
| 63 | #define FS_EOF_CFG 0x934 |
| 64 | #define FSEOF_OFFSET 0x2e |
| 65 | |
| 66 | #define SS_GEN1_EOF_CFG 0x93c |
| 67 | #define SSG1EOF_OFFSET 0x78 |
| 68 | |
| 69 | #define HFCNTR_CFG 0x944 |
| 70 | #define ITP_DELTA_CLK (0xa << 1) |
| 71 | #define ITP_DELTA_CLK_MASK GENMASK(5, 1) |
| 72 | #define FRMCNT_LEV1_RANG (0x12b << 8) |
| 73 | #define FRMCNT_LEV1_RANG_MASK GENMASK(19, 8) |
| 74 | |
| 75 | #define SS_GEN2_EOF_CFG 0x990 |
| 76 | #define SSG2EOF_OFFSET 0x3c |
| 77 | |
| 78 | #define XSEOF_OFFSET_MASK GENMASK(11, 0) |
| 79 | |
Chunfeng Yun | a2ecc4d | 2018-01-03 16:53:20 +0800 | [diff] [blame] | 80 | /* usb remote wakeup registers in syscon */ |
Chunfeng Yun | c03b4cc | 2021-03-23 15:02:48 +0800 | [diff] [blame] | 81 | |
Chunfeng Yun | a2ecc4d | 2018-01-03 16:53:20 +0800 | [diff] [blame] | 82 | /* mt8173 etc */ |
| 83 | #define PERI_WK_CTRL1 0x4 |
| 84 | #define WC1_IS_C(x) (((x) & 0xf) << 26) /* cycle debounce */ |
| 85 | #define WC1_IS_EN BIT(25) |
| 86 | #define WC1_IS_P BIT(6) /* polarity for ip sleep */ |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 87 | |
Chunfeng Yun | c03b4cc | 2021-03-23 15:02:48 +0800 | [diff] [blame] | 88 | /* mt8183 */ |
| 89 | #define PERI_WK_CTRL0 0x0 |
| 90 | #define WC0_IS_C(x) ((u32)(((x) & 0xf) << 28)) /* cycle debounce */ |
| 91 | #define WC0_IS_P BIT(12) /* polarity */ |
| 92 | #define WC0_IS_EN BIT(6) |
| 93 | |
Chunfeng Yun | 331c505 | 2021-03-23 15:02:49 +0800 | [diff] [blame] | 94 | /* mt8192 */ |
| 95 | #define WC0_SSUSB0_CDEN BIT(6) |
| 96 | #define WC0_IS_SPM_EN BIT(1) |
| 97 | |
Chunfeng Yun | a2ecc4d | 2018-01-03 16:53:20 +0800 | [diff] [blame] | 98 | /* mt2712 etc */ |
| 99 | #define PERI_SSUSB_SPM_CTRL 0x0 |
| 100 | #define SSC_IP_SLEEP_EN BIT(4) |
| 101 | #define SSC_SPM_INT_EN BIT(1) |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 102 | |
Chunfeng Yun | a2ecc4d | 2018-01-03 16:53:20 +0800 | [diff] [blame] | 103 | enum ssusb_uwk_vers { |
| 104 | SSUSB_UWK_V1 = 1, |
| 105 | SSUSB_UWK_V2, |
Chunfeng Yun | c03b4cc | 2021-03-23 15:02:48 +0800 | [diff] [blame] | 106 | SSUSB_UWK_V1_1 = 101, /* specific revision 1.01 */ |
Chunfeng Yun | 331c505 | 2021-03-23 15:02:49 +0800 | [diff] [blame] | 107 | SSUSB_UWK_V1_2, /* specific revision 1.2 */ |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 108 | }; |
| 109 | |
Chunfeng Yun | 926d60a | 2021-08-17 16:36:29 +0800 | [diff] [blame] | 110 | /* |
| 111 | * MT8195 has 4 controllers, the controller1~3's default SOF/ITP interval |
| 112 | * is calculated from the frame counter clock 24M, but in fact, the clock |
| 113 | * is 48M, add workaround for it. |
| 114 | */ |
| 115 | static void xhci_mtk_set_frame_interval(struct xhci_hcd_mtk *mtk) |
| 116 | { |
| 117 | struct device *dev = mtk->dev; |
| 118 | struct usb_hcd *hcd = mtk->hcd; |
| 119 | u32 value; |
| 120 | |
| 121 | if (!of_device_is_compatible(dev->of_node, "mediatek,mt8195-xhci")) |
| 122 | return; |
| 123 | |
| 124 | value = readl(hcd->regs + HFCNTR_CFG); |
| 125 | value &= ~(ITP_DELTA_CLK_MASK | FRMCNT_LEV1_RANG_MASK); |
| 126 | value |= (ITP_DELTA_CLK | FRMCNT_LEV1_RANG); |
| 127 | writel(value, hcd->regs + HFCNTR_CFG); |
| 128 | |
| 129 | value = readl(hcd->regs + LS_EOF_CFG); |
| 130 | value &= ~XSEOF_OFFSET_MASK; |
| 131 | value |= LSEOF_OFFSET; |
| 132 | writel(value, hcd->regs + LS_EOF_CFG); |
| 133 | |
| 134 | value = readl(hcd->regs + FS_EOF_CFG); |
| 135 | value &= ~XSEOF_OFFSET_MASK; |
| 136 | value |= FSEOF_OFFSET; |
| 137 | writel(value, hcd->regs + FS_EOF_CFG); |
| 138 | |
| 139 | value = readl(hcd->regs + SS_GEN1_EOF_CFG); |
| 140 | value &= ~XSEOF_OFFSET_MASK; |
| 141 | value |= SSG1EOF_OFFSET; |
| 142 | writel(value, hcd->regs + SS_GEN1_EOF_CFG); |
| 143 | |
| 144 | value = readl(hcd->regs + SS_GEN2_EOF_CFG); |
| 145 | value &= ~XSEOF_OFFSET_MASK; |
| 146 | value |= SSG2EOF_OFFSET; |
| 147 | writel(value, hcd->regs + SS_GEN2_EOF_CFG); |
| 148 | } |
| 149 | |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 150 | static int xhci_mtk_host_enable(struct xhci_hcd_mtk *mtk) |
| 151 | { |
| 152 | struct mu3c_ippc_regs __iomem *ippc = mtk->ippc_regs; |
| 153 | u32 value, check_val; |
Frank Wunderlich | 6e18cfc | 2020-08-08 14:49:06 +0200 | [diff] [blame] | 154 | int u3_ports_disabled = 0; |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 155 | int ret; |
| 156 | int i; |
| 157 | |
Chunfeng Yun | 065d48c | 2016-10-19 10:28:22 +0800 | [diff] [blame] | 158 | if (!mtk->has_ippc) |
| 159 | return 0; |
| 160 | |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 161 | /* power on host ip */ |
| 162 | value = readl(&ippc->ip_pw_ctr1); |
| 163 | value &= ~CTRL1_IP_HOST_PDN; |
| 164 | writel(value, &ippc->ip_pw_ctr1); |
| 165 | |
Chunfeng Yun | 55ba6e9 | 2017-10-13 16:26:36 +0800 | [diff] [blame] | 166 | /* power on and enable u3 ports except skipped ones */ |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 167 | for (i = 0; i < mtk->num_u3_ports; i++) { |
Chunfeng Yun | 55ba6e9 | 2017-10-13 16:26:36 +0800 | [diff] [blame] | 168 | if ((0x1 << i) & mtk->u3p_dis_msk) { |
Frank Wunderlich | 6e18cfc | 2020-08-08 14:49:06 +0200 | [diff] [blame] | 169 | u3_ports_disabled++; |
Chunfeng Yun | 55ba6e9 | 2017-10-13 16:26:36 +0800 | [diff] [blame] | 170 | continue; |
| 171 | } |
| 172 | |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 173 | value = readl(&ippc->u3_ctrl_p[i]); |
| 174 | value &= ~(CTRL_U3_PORT_PDN | CTRL_U3_PORT_DIS); |
| 175 | value |= CTRL_U3_PORT_HOST_SEL; |
| 176 | writel(value, &ippc->u3_ctrl_p[i]); |
| 177 | } |
| 178 | |
Chunfeng Yun | 7465d7b | 2021-08-17 16:36:24 +0800 | [diff] [blame] | 179 | /* power on and enable all u2 ports except skipped ones */ |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 180 | for (i = 0; i < mtk->num_u2_ports; i++) { |
Chunfeng Yun | 7465d7b | 2021-08-17 16:36:24 +0800 | [diff] [blame] | 181 | if (BIT(i) & mtk->u2p_dis_msk) |
| 182 | continue; |
| 183 | |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 184 | value = readl(&ippc->u2_ctrl_p[i]); |
| 185 | value &= ~(CTRL_U2_PORT_PDN | CTRL_U2_PORT_DIS); |
| 186 | value |= CTRL_U2_PORT_HOST_SEL; |
| 187 | writel(value, &ippc->u2_ctrl_p[i]); |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * wait for clocks to be stable, and clock domains reset to |
| 192 | * be inactive after power on and enable ports |
| 193 | */ |
| 194 | check_val = STS1_SYSPLL_STABLE | STS1_REF_RST | |
| 195 | STS1_SYS125_RST | STS1_XHCI_RST; |
| 196 | |
Frank Wunderlich | 6e18cfc | 2020-08-08 14:49:06 +0200 | [diff] [blame] | 197 | if (mtk->num_u3_ports > u3_ports_disabled) |
Chunfeng Yun | ce370bf | 2017-10-13 16:26:35 +0800 | [diff] [blame] | 198 | check_val |= STS1_U3_MAC_RST; |
| 199 | |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 200 | ret = readl_poll_timeout(&ippc->ip_pw_sts1, value, |
| 201 | (check_val == (value & check_val)), 100, 20000); |
| 202 | if (ret) { |
| 203 | dev_err(mtk->dev, "clocks are not stable (0x%x)\n", value); |
| 204 | return ret; |
| 205 | } |
| 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | static int xhci_mtk_host_disable(struct xhci_hcd_mtk *mtk) |
| 211 | { |
| 212 | struct mu3c_ippc_regs __iomem *ippc = mtk->ippc_regs; |
| 213 | u32 value; |
| 214 | int ret; |
| 215 | int i; |
| 216 | |
Chunfeng Yun | 065d48c | 2016-10-19 10:28:22 +0800 | [diff] [blame] | 217 | if (!mtk->has_ippc) |
| 218 | return 0; |
| 219 | |
Chunfeng Yun | 55ba6e9 | 2017-10-13 16:26:36 +0800 | [diff] [blame] | 220 | /* power down u3 ports except skipped ones */ |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 221 | for (i = 0; i < mtk->num_u3_ports; i++) { |
Chunfeng Yun | 55ba6e9 | 2017-10-13 16:26:36 +0800 | [diff] [blame] | 222 | if ((0x1 << i) & mtk->u3p_dis_msk) |
| 223 | continue; |
| 224 | |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 225 | value = readl(&ippc->u3_ctrl_p[i]); |
| 226 | value |= CTRL_U3_PORT_PDN; |
| 227 | writel(value, &ippc->u3_ctrl_p[i]); |
| 228 | } |
| 229 | |
Chunfeng Yun | 7465d7b | 2021-08-17 16:36:24 +0800 | [diff] [blame] | 230 | /* power down all u2 ports except skipped ones */ |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 231 | for (i = 0; i < mtk->num_u2_ports; i++) { |
Chunfeng Yun | 7465d7b | 2021-08-17 16:36:24 +0800 | [diff] [blame] | 232 | if (BIT(i) & mtk->u2p_dis_msk) |
| 233 | continue; |
| 234 | |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 235 | value = readl(&ippc->u2_ctrl_p[i]); |
| 236 | value |= CTRL_U2_PORT_PDN; |
| 237 | writel(value, &ippc->u2_ctrl_p[i]); |
| 238 | } |
| 239 | |
| 240 | /* power down host ip */ |
| 241 | value = readl(&ippc->ip_pw_ctr1); |
| 242 | value |= CTRL1_IP_HOST_PDN; |
| 243 | writel(value, &ippc->ip_pw_ctr1); |
| 244 | |
| 245 | /* wait for host ip to sleep */ |
| 246 | ret = readl_poll_timeout(&ippc->ip_pw_sts1, value, |
| 247 | (value & STS1_IP_SLEEP_STS), 100, 100000); |
Chunfeng Yun | 0d8cfee | 2021-11-02 10:50:04 +0800 | [diff] [blame] | 248 | if (ret) |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 249 | dev_err(mtk->dev, "ip sleep failed!!!\n"); |
Chunfeng Yun | 0d8cfee | 2021-11-02 10:50:04 +0800 | [diff] [blame] | 250 | else /* workaound for platforms using low level latch */ |
| 251 | usleep_range(100, 200); |
| 252 | |
| 253 | return ret; |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | static int xhci_mtk_ssusb_config(struct xhci_hcd_mtk *mtk) |
| 257 | { |
| 258 | struct mu3c_ippc_regs __iomem *ippc = mtk->ippc_regs; |
| 259 | u32 value; |
| 260 | |
Chunfeng Yun | 065d48c | 2016-10-19 10:28:22 +0800 | [diff] [blame] | 261 | if (!mtk->has_ippc) |
| 262 | return 0; |
| 263 | |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 264 | /* reset whole ip */ |
| 265 | value = readl(&ippc->ip_pw_ctr0); |
| 266 | value |= CTRL0_IP_SW_RST; |
| 267 | writel(value, &ippc->ip_pw_ctr0); |
| 268 | udelay(1); |
| 269 | value = readl(&ippc->ip_pw_ctr0); |
| 270 | value &= ~CTRL0_IP_SW_RST; |
| 271 | writel(value, &ippc->ip_pw_ctr0); |
| 272 | |
| 273 | /* |
| 274 | * device ip is default power-on in fact |
| 275 | * power down device ip, otherwise ip-sleep will fail |
| 276 | */ |
| 277 | value = readl(&ippc->ip_pw_ctr2); |
| 278 | value |= CTRL2_IP_DEV_PDN; |
| 279 | writel(value, &ippc->ip_pw_ctr2); |
| 280 | |
| 281 | value = readl(&ippc->ip_xhci_cap); |
| 282 | mtk->num_u3_ports = CAP_U3_PORT_NUM(value); |
| 283 | mtk->num_u2_ports = CAP_U2_PORT_NUM(value); |
| 284 | dev_dbg(mtk->dev, "%s u2p:%d, u3p:%d\n", __func__, |
| 285 | mtk->num_u2_ports, mtk->num_u3_ports); |
| 286 | |
| 287 | return xhci_mtk_host_enable(mtk); |
| 288 | } |
| 289 | |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 290 | /* only clocks can be turn off for ip-sleep wakeup mode */ |
Chunfeng Yun | a2ecc4d | 2018-01-03 16:53:20 +0800 | [diff] [blame] | 291 | static void usb_wakeup_ip_sleep_set(struct xhci_hcd_mtk *mtk, bool enable) |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 292 | { |
Chunfeng Yun | a2ecc4d | 2018-01-03 16:53:20 +0800 | [diff] [blame] | 293 | u32 reg, msk, val; |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 294 | |
Chunfeng Yun | a2ecc4d | 2018-01-03 16:53:20 +0800 | [diff] [blame] | 295 | switch (mtk->uwk_vers) { |
| 296 | case SSUSB_UWK_V1: |
| 297 | reg = mtk->uwk_reg_base + PERI_WK_CTRL1; |
| 298 | msk = WC1_IS_EN | WC1_IS_C(0xf) | WC1_IS_P; |
| 299 | val = enable ? (WC1_IS_EN | WC1_IS_C(0x8)) : 0; |
| 300 | break; |
Chunfeng Yun | c03b4cc | 2021-03-23 15:02:48 +0800 | [diff] [blame] | 301 | case SSUSB_UWK_V1_1: |
| 302 | reg = mtk->uwk_reg_base + PERI_WK_CTRL0; |
| 303 | msk = WC0_IS_EN | WC0_IS_C(0xf) | WC0_IS_P; |
Chunfeng Yun | 0d8cfee | 2021-11-02 10:50:04 +0800 | [diff] [blame] | 304 | val = enable ? (WC0_IS_EN | WC0_IS_C(0x1)) : 0; |
Chunfeng Yun | c03b4cc | 2021-03-23 15:02:48 +0800 | [diff] [blame] | 305 | break; |
Chunfeng Yun | 331c505 | 2021-03-23 15:02:49 +0800 | [diff] [blame] | 306 | case SSUSB_UWK_V1_2: |
| 307 | reg = mtk->uwk_reg_base + PERI_WK_CTRL0; |
| 308 | msk = WC0_SSUSB0_CDEN | WC0_IS_SPM_EN; |
| 309 | val = enable ? msk : 0; |
| 310 | break; |
Chunfeng Yun | a2ecc4d | 2018-01-03 16:53:20 +0800 | [diff] [blame] | 311 | case SSUSB_UWK_V2: |
| 312 | reg = mtk->uwk_reg_base + PERI_SSUSB_SPM_CTRL; |
| 313 | msk = SSC_IP_SLEEP_EN | SSC_SPM_INT_EN; |
| 314 | val = enable ? msk : 0; |
| 315 | break; |
| 316 | default: |
| 317 | return; |
Fengguang Wu | dae0620 | 2018-01-13 01:46:49 +0800 | [diff] [blame] | 318 | } |
Chunfeng Yun | a2ecc4d | 2018-01-03 16:53:20 +0800 | [diff] [blame] | 319 | regmap_update_bits(mtk->uwk, reg, msk, val); |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | static int usb_wakeup_of_property_parse(struct xhci_hcd_mtk *mtk, |
| 323 | struct device_node *dn) |
| 324 | { |
Chunfeng Yun | a2ecc4d | 2018-01-03 16:53:20 +0800 | [diff] [blame] | 325 | struct of_phandle_args args; |
| 326 | int ret; |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 327 | |
Chunfeng Yun | a2ecc4d | 2018-01-03 16:53:20 +0800 | [diff] [blame] | 328 | /* Wakeup function is optional */ |
| 329 | mtk->uwk_en = of_property_read_bool(dn, "wakeup-source"); |
| 330 | if (!mtk->uwk_en) |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 331 | return 0; |
| 332 | |
Chunfeng Yun | a2ecc4d | 2018-01-03 16:53:20 +0800 | [diff] [blame] | 333 | ret = of_parse_phandle_with_fixed_args(dn, |
| 334 | "mediatek,syscon-wakeup", 2, 0, &args); |
| 335 | if (ret) |
| 336 | return ret; |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 337 | |
Chunfeng Yun | a2ecc4d | 2018-01-03 16:53:20 +0800 | [diff] [blame] | 338 | mtk->uwk_reg_base = args.args[0]; |
| 339 | mtk->uwk_vers = args.args[1]; |
| 340 | mtk->uwk = syscon_node_to_regmap(args.np); |
| 341 | of_node_put(args.np); |
| 342 | dev_info(mtk->dev, "uwk - reg:0x%x, version:%d\n", |
| 343 | mtk->uwk_reg_base, mtk->uwk_vers); |
| 344 | |
| 345 | return PTR_ERR_OR_ZERO(mtk->uwk); |
Chunfeng Yun | a2ecc4d | 2018-01-03 16:53:20 +0800 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | static void usb_wakeup_set(struct xhci_hcd_mtk *mtk, bool enable) |
| 349 | { |
| 350 | if (mtk->uwk_en) |
| 351 | usb_wakeup_ip_sleep_set(mtk, enable); |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 352 | } |
| 353 | |
Chunfeng Yun | 7fed636 | 2021-04-10 13:10:05 +0800 | [diff] [blame] | 354 | static int xhci_mtk_clks_get(struct xhci_hcd_mtk *mtk) |
| 355 | { |
| 356 | struct clk_bulk_data *clks = mtk->clks; |
| 357 | |
| 358 | clks[0].id = "sys_ck"; |
| 359 | clks[1].id = "xhci_ck"; |
| 360 | clks[2].id = "ref_ck"; |
| 361 | clks[3].id = "mcu_ck"; |
| 362 | clks[4].id = "dma_ck"; |
| 363 | |
| 364 | return devm_clk_bulk_get_optional(mtk->dev, BULK_CLKS_NUM, clks); |
| 365 | } |
| 366 | |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 367 | static int xhci_mtk_ldos_enable(struct xhci_hcd_mtk *mtk) |
| 368 | { |
| 369 | int ret; |
| 370 | |
| 371 | ret = regulator_enable(mtk->vbus); |
| 372 | if (ret) { |
| 373 | dev_err(mtk->dev, "failed to enable vbus\n"); |
| 374 | return ret; |
| 375 | } |
| 376 | |
| 377 | ret = regulator_enable(mtk->vusb33); |
| 378 | if (ret) { |
| 379 | dev_err(mtk->dev, "failed to enable vusb33\n"); |
| 380 | regulator_disable(mtk->vbus); |
| 381 | return ret; |
| 382 | } |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | static void xhci_mtk_ldos_disable(struct xhci_hcd_mtk *mtk) |
| 387 | { |
| 388 | regulator_disable(mtk->vbus); |
| 389 | regulator_disable(mtk->vusb33); |
| 390 | } |
| 391 | |
| 392 | static void xhci_mtk_quirks(struct device *dev, struct xhci_hcd *xhci) |
| 393 | { |
| 394 | struct usb_hcd *hcd = xhci_to_hcd(xhci); |
| 395 | struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd); |
| 396 | |
| 397 | /* |
| 398 | * As of now platform drivers don't provide MSI support so we ensure |
| 399 | * here that the generic code does not try to make a pci_dev from our |
| 400 | * dev struct in order to setup MSI |
| 401 | */ |
| 402 | xhci->quirks |= XHCI_PLAT; |
| 403 | xhci->quirks |= XHCI_MTK_HOST; |
| 404 | /* |
| 405 | * MTK host controller gives a spurious successful event after a |
| 406 | * short transfer. Ignore it. |
| 407 | */ |
| 408 | xhci->quirks |= XHCI_SPURIOUS_SUCCESS; |
| 409 | if (mtk->lpm_support) |
| 410 | xhci->quirks |= XHCI_LPM_SUPPORT; |
Chunfeng Yun | bee1f89 | 2021-03-31 17:05:53 +0800 | [diff] [blame] | 411 | if (mtk->u2_lpm_disable) |
| 412 | xhci->quirks |= XHCI_HW_LPM_DISABLE; |
Chunfeng Yun | 1f743c8 | 2021-03-31 17:05:52 +0800 | [diff] [blame] | 413 | |
| 414 | /* |
| 415 | * MTK xHCI 0.96: PSA is 1 by default even if doesn't support stream, |
| 416 | * and it's 3 when support it. |
| 417 | */ |
| 418 | if (xhci->hci_version < 0x100 && HCC_MAX_PSA(xhci->hcc_params) == 4) |
| 419 | xhci->quirks |= XHCI_BROKEN_STREAMS; |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | /* called during probe() after chip reset completes */ |
| 423 | static int xhci_mtk_setup(struct usb_hcd *hcd) |
| 424 | { |
| 425 | struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd); |
| 426 | int ret; |
| 427 | |
| 428 | if (usb_hcd_is_primary_hcd(hcd)) { |
| 429 | ret = xhci_mtk_ssusb_config(mtk); |
| 430 | if (ret) |
| 431 | return ret; |
Chunfeng Yun | 926d60a | 2021-08-17 16:36:29 +0800 | [diff] [blame] | 432 | |
| 433 | /* workaround only for mt8195 */ |
| 434 | xhci_mtk_set_frame_interval(mtk); |
Chunfeng Yun | 065d48c | 2016-10-19 10:28:22 +0800 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | ret = xhci_gen_setup(hcd, xhci_mtk_quirks); |
| 438 | if (ret) |
| 439 | return ret; |
| 440 | |
Chunfeng Yun | 38269d2 | 2021-11-02 10:50:03 +0800 | [diff] [blame] | 441 | if (usb_hcd_is_primary_hcd(hcd)) |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 442 | ret = xhci_mtk_sch_init(mtk); |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 443 | |
Chunfeng Yun | 065d48c | 2016-10-19 10:28:22 +0800 | [diff] [blame] | 444 | return ret; |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 445 | } |
| 446 | |
Chunfeng Yun | dc9d3b2 | 2021-03-08 10:52:03 +0800 | [diff] [blame] | 447 | static const struct xhci_driver_overrides xhci_mtk_overrides __initconst = { |
| 448 | .reset = xhci_mtk_setup, |
Chunfeng Yun | 14295a150 | 2021-03-08 10:52:04 +0800 | [diff] [blame] | 449 | .add_endpoint = xhci_mtk_add_ep, |
| 450 | .drop_endpoint = xhci_mtk_drop_ep, |
Chunfeng Yun | dc9d3b2 | 2021-03-08 10:52:03 +0800 | [diff] [blame] | 451 | .check_bandwidth = xhci_mtk_check_bandwidth, |
| 452 | .reset_bandwidth = xhci_mtk_reset_bandwidth, |
| 453 | }; |
| 454 | |
| 455 | static struct hc_driver __read_mostly xhci_mtk_hc_driver; |
| 456 | |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 457 | static int xhci_mtk_probe(struct platform_device *pdev) |
| 458 | { |
| 459 | struct device *dev = &pdev->dev; |
| 460 | struct device_node *node = dev->of_node; |
| 461 | struct xhci_hcd_mtk *mtk; |
| 462 | const struct hc_driver *driver; |
| 463 | struct xhci_hcd *xhci; |
| 464 | struct resource *res; |
| 465 | struct usb_hcd *hcd; |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 466 | int ret = -ENODEV; |
Chunfeng Yun | 04284eb | 2021-04-10 13:10:04 +0800 | [diff] [blame] | 467 | int wakeup_irq; |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 468 | int irq; |
| 469 | |
| 470 | if (usb_disabled()) |
| 471 | return -ENODEV; |
| 472 | |
| 473 | driver = &xhci_mtk_hc_driver; |
| 474 | mtk = devm_kzalloc(dev, sizeof(*mtk), GFP_KERNEL); |
| 475 | if (!mtk) |
| 476 | return -ENOMEM; |
| 477 | |
| 478 | mtk->dev = dev; |
| 479 | mtk->vbus = devm_regulator_get(dev, "vbus"); |
| 480 | if (IS_ERR(mtk->vbus)) { |
| 481 | dev_err(dev, "fail to get vbus\n"); |
| 482 | return PTR_ERR(mtk->vbus); |
| 483 | } |
| 484 | |
| 485 | mtk->vusb33 = devm_regulator_get(dev, "vusb33"); |
| 486 | if (IS_ERR(mtk->vusb33)) { |
| 487 | dev_err(dev, "fail to get vusb33\n"); |
| 488 | return PTR_ERR(mtk->vusb33); |
| 489 | } |
| 490 | |
Chunfeng Yun | b6bb72c | 2017-10-13 16:26:38 +0800 | [diff] [blame] | 491 | ret = xhci_mtk_clks_get(mtk); |
| 492 | if (ret) |
| 493 | return ret; |
Chunfeng Yun | 9c4afd4 | 2017-01-18 14:08:24 +0800 | [diff] [blame] | 494 | |
Chunfeng Yun | 04284eb | 2021-04-10 13:10:04 +0800 | [diff] [blame] | 495 | irq = platform_get_irq_byname_optional(pdev, "host"); |
| 496 | if (irq < 0) { |
| 497 | if (irq == -EPROBE_DEFER) |
| 498 | return irq; |
| 499 | |
| 500 | /* for backward compatibility */ |
| 501 | irq = platform_get_irq(pdev, 0); |
| 502 | if (irq < 0) |
| 503 | return irq; |
| 504 | } |
| 505 | |
| 506 | wakeup_irq = platform_get_irq_byname_optional(pdev, "wakeup"); |
| 507 | if (wakeup_irq == -EPROBE_DEFER) |
| 508 | return wakeup_irq; |
| 509 | |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 510 | mtk->lpm_support = of_property_read_bool(node, "usb3-lpm-capable"); |
Chunfeng Yun | bee1f89 | 2021-03-31 17:05:53 +0800 | [diff] [blame] | 511 | mtk->u2_lpm_disable = of_property_read_bool(node, "usb2-lpm-disable"); |
Chunfeng Yun | 55ba6e9 | 2017-10-13 16:26:36 +0800 | [diff] [blame] | 512 | /* optional property, ignore the error if it does not exist */ |
| 513 | of_property_read_u32(node, "mediatek,u3p-dis-msk", |
| 514 | &mtk->u3p_dis_msk); |
Chunfeng Yun | 7465d7b | 2021-08-17 16:36:24 +0800 | [diff] [blame] | 515 | of_property_read_u32(node, "mediatek,u2p-dis-msk", |
| 516 | &mtk->u2p_dis_msk); |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 517 | |
| 518 | ret = usb_wakeup_of_property_parse(mtk, node); |
Chunfeng Yun | a2ecc4d | 2018-01-03 16:53:20 +0800 | [diff] [blame] | 519 | if (ret) { |
| 520 | dev_err(dev, "failed to parse uwk property\n"); |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 521 | return ret; |
Chunfeng Yun | a2ecc4d | 2018-01-03 16:53:20 +0800 | [diff] [blame] | 522 | } |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 523 | |
Chunfeng Yun | 04284eb | 2021-04-10 13:10:04 +0800 | [diff] [blame] | 524 | pm_runtime_set_active(dev); |
| 525 | pm_runtime_use_autosuspend(dev); |
| 526 | pm_runtime_set_autosuspend_delay(dev, 4000); |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 527 | pm_runtime_enable(dev); |
| 528 | pm_runtime_get_sync(dev); |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 529 | |
| 530 | ret = xhci_mtk_ldos_enable(mtk); |
| 531 | if (ret) |
| 532 | goto disable_pm; |
| 533 | |
Chunfeng Yun | 7fed636 | 2021-04-10 13:10:05 +0800 | [diff] [blame] | 534 | ret = clk_bulk_prepare_enable(BULK_CLKS_NUM, mtk->clks); |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 535 | if (ret) |
| 536 | goto disable_ldos; |
| 537 | |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 538 | hcd = usb_create_hcd(driver, dev, dev_name(dev)); |
| 539 | if (!hcd) { |
| 540 | ret = -ENOMEM; |
| 541 | goto disable_clk; |
| 542 | } |
| 543 | |
| 544 | /* |
| 545 | * USB 2.0 roothub is stored in the platform_device. |
| 546 | * Swap it with mtk HCD. |
| 547 | */ |
| 548 | mtk->hcd = platform_get_drvdata(pdev); |
| 549 | platform_set_drvdata(pdev, mtk); |
| 550 | |
Chunfeng Yun | 065d48c | 2016-10-19 10:28:22 +0800 | [diff] [blame] | 551 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mac"); |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 552 | hcd->regs = devm_ioremap_resource(dev, res); |
| 553 | if (IS_ERR(hcd->regs)) { |
| 554 | ret = PTR_ERR(hcd->regs); |
| 555 | goto put_usb2_hcd; |
| 556 | } |
| 557 | hcd->rsrc_start = res->start; |
| 558 | hcd->rsrc_len = resource_size(res); |
| 559 | |
Chunfeng Yun | 065d48c | 2016-10-19 10:28:22 +0800 | [diff] [blame] | 560 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ippc"); |
| 561 | if (res) { /* ippc register is optional */ |
| 562 | mtk->ippc_regs = devm_ioremap_resource(dev, res); |
| 563 | if (IS_ERR(mtk->ippc_regs)) { |
| 564 | ret = PTR_ERR(mtk->ippc_regs); |
| 565 | goto put_usb2_hcd; |
| 566 | } |
| 567 | mtk->has_ippc = true; |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 568 | } |
| 569 | |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 570 | device_init_wakeup(dev, true); |
| 571 | |
| 572 | xhci = hcd_to_xhci(hcd); |
| 573 | xhci->main_hcd = hcd; |
Adam Wallis | ab725cb | 2017-12-08 17:59:13 +0200 | [diff] [blame] | 574 | |
| 575 | /* |
| 576 | * imod_interval is the interrupt moderation value in nanoseconds. |
| 577 | * The increment interval is 8 times as much as that defined in |
| 578 | * the xHCI spec on MTK's controller. |
| 579 | */ |
| 580 | xhci->imod_interval = 5000; |
| 581 | device_property_read_u32(dev, "imod-interval-ns", &xhci->imod_interval); |
| 582 | |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 583 | xhci->shared_hcd = usb_create_shared_hcd(driver, dev, |
| 584 | dev_name(dev), hcd); |
| 585 | if (!xhci->shared_hcd) { |
| 586 | ret = -ENOMEM; |
Martin Blumenstingl | 6ae9f50 | 2018-03-03 22:43:06 +0100 | [diff] [blame] | 587 | goto disable_device_wakeup; |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 588 | } |
| 589 | |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 590 | ret = usb_add_hcd(hcd, irq, IRQF_SHARED); |
| 591 | if (ret) |
| 592 | goto put_usb3_hcd; |
| 593 | |
Chunfeng Yun | 1f743c8 | 2021-03-31 17:05:52 +0800 | [diff] [blame] | 594 | if (HCC_MAX_PSA(xhci->hcc_params) >= 4 && |
| 595 | !(xhci->quirks & XHCI_BROKEN_STREAMS)) |
Chunfeng Yun | 94a631d | 2017-03-09 15:39:34 +0200 | [diff] [blame] | 596 | xhci->shared_hcd->can_do_streams = 1; |
| 597 | |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 598 | ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED); |
| 599 | if (ret) |
| 600 | goto dealloc_usb2_hcd; |
| 601 | |
Chunfeng Yun | 04284eb | 2021-04-10 13:10:04 +0800 | [diff] [blame] | 602 | if (wakeup_irq > 0) { |
Chunfeng Yun | 0537282 | 2021-10-25 15:01:54 +0800 | [diff] [blame] | 603 | ret = dev_pm_set_dedicated_wake_irq_reverse(dev, wakeup_irq); |
Chunfeng Yun | 04284eb | 2021-04-10 13:10:04 +0800 | [diff] [blame] | 604 | if (ret) { |
| 605 | dev_err(dev, "set wakeup irq %d failed\n", wakeup_irq); |
| 606 | goto dealloc_usb3_hcd; |
| 607 | } |
| 608 | dev_info(dev, "wakeup irq %d\n", wakeup_irq); |
| 609 | } |
| 610 | |
| 611 | device_enable_async_suspend(dev); |
| 612 | pm_runtime_mark_last_busy(dev); |
| 613 | pm_runtime_put_autosuspend(dev); |
| 614 | pm_runtime_forbid(dev); |
| 615 | |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 616 | return 0; |
| 617 | |
Chunfeng Yun | 04284eb | 2021-04-10 13:10:04 +0800 | [diff] [blame] | 618 | dealloc_usb3_hcd: |
| 619 | usb_remove_hcd(xhci->shared_hcd); |
| 620 | xhci->shared_hcd = NULL; |
| 621 | |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 622 | dealloc_usb2_hcd: |
| 623 | usb_remove_hcd(hcd); |
| 624 | |
| 625 | put_usb3_hcd: |
| 626 | xhci_mtk_sch_exit(mtk); |
| 627 | usb_put_hcd(xhci->shared_hcd); |
| 628 | |
Martin Blumenstingl | 6ae9f50 | 2018-03-03 22:43:06 +0100 | [diff] [blame] | 629 | disable_device_wakeup: |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 630 | device_init_wakeup(dev, false); |
| 631 | |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 632 | put_usb2_hcd: |
| 633 | usb_put_hcd(hcd); |
| 634 | |
| 635 | disable_clk: |
Chunfeng Yun | 7fed636 | 2021-04-10 13:10:05 +0800 | [diff] [blame] | 636 | clk_bulk_disable_unprepare(BULK_CLKS_NUM, mtk->clks); |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 637 | |
| 638 | disable_ldos: |
| 639 | xhci_mtk_ldos_disable(mtk); |
| 640 | |
| 641 | disable_pm: |
Chunfeng Yun | 7f85c16 | 2021-08-17 16:36:23 +0800 | [diff] [blame] | 642 | pm_runtime_put_noidle(dev); |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 643 | pm_runtime_disable(dev); |
| 644 | return ret; |
| 645 | } |
| 646 | |
Chunfeng Yun | 04284eb | 2021-04-10 13:10:04 +0800 | [diff] [blame] | 647 | static int xhci_mtk_remove(struct platform_device *pdev) |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 648 | { |
Chunfeng Yun | 04284eb | 2021-04-10 13:10:04 +0800 | [diff] [blame] | 649 | struct xhci_hcd_mtk *mtk = platform_get_drvdata(pdev); |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 650 | struct usb_hcd *hcd = mtk->hcd; |
| 651 | struct xhci_hcd *xhci = hcd_to_xhci(hcd); |
Mathias Nyman | f068090 | 2018-11-09 17:21:17 +0200 | [diff] [blame] | 652 | struct usb_hcd *shared_hcd = xhci->shared_hcd; |
Chunfeng Yun | 04284eb | 2021-04-10 13:10:04 +0800 | [diff] [blame] | 653 | struct device *dev = &pdev->dev; |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 654 | |
Chunfeng Yun | 04284eb | 2021-04-10 13:10:04 +0800 | [diff] [blame] | 655 | pm_runtime_get_sync(dev); |
| 656 | xhci->xhc_state |= XHCI_STATE_REMOVING; |
| 657 | dev_pm_clear_wake_irq(dev); |
| 658 | device_init_wakeup(dev, false); |
Macpaul Lin | a24d507 | 2020-06-24 16:59:47 +0300 | [diff] [blame] | 659 | |
Mathias Nyman | f068090 | 2018-11-09 17:21:17 +0200 | [diff] [blame] | 660 | usb_remove_hcd(shared_hcd); |
| 661 | xhci->shared_hcd = NULL; |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 662 | usb_remove_hcd(hcd); |
Mathias Nyman | f068090 | 2018-11-09 17:21:17 +0200 | [diff] [blame] | 663 | usb_put_hcd(shared_hcd); |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 664 | usb_put_hcd(hcd); |
| 665 | xhci_mtk_sch_exit(mtk); |
Chunfeng Yun | 7fed636 | 2021-04-10 13:10:05 +0800 | [diff] [blame] | 666 | clk_bulk_disable_unprepare(BULK_CLKS_NUM, mtk->clks); |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 667 | xhci_mtk_ldos_disable(mtk); |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 668 | |
Chunfeng Yun | 04284eb | 2021-04-10 13:10:04 +0800 | [diff] [blame] | 669 | pm_runtime_disable(dev); |
| 670 | pm_runtime_put_noidle(dev); |
| 671 | pm_runtime_set_suspended(dev); |
| 672 | |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 673 | return 0; |
| 674 | } |
| 675 | |
Arnd Bergmann | 8dac530 | 2016-03-02 16:24:04 +0100 | [diff] [blame] | 676 | static int __maybe_unused xhci_mtk_suspend(struct device *dev) |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 677 | { |
| 678 | struct xhci_hcd_mtk *mtk = dev_get_drvdata(dev); |
Chunfeng Yun | 882fa27 | 2016-01-26 17:50:10 +0200 | [diff] [blame] | 679 | struct usb_hcd *hcd = mtk->hcd; |
| 680 | struct xhci_hcd *xhci = hcd_to_xhci(hcd); |
Chunfeng Yun | 82dad9f | 2021-04-10 13:10:02 +0800 | [diff] [blame] | 681 | int ret; |
Chunfeng Yun | 882fa27 | 2016-01-26 17:50:10 +0200 | [diff] [blame] | 682 | |
| 683 | xhci_dbg(xhci, "%s: stop port polling\n", __func__); |
| 684 | clear_bit(HCD_FLAG_POLL_RH, &hcd->flags); |
| 685 | del_timer_sync(&hcd->rh_timer); |
| 686 | clear_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags); |
| 687 | del_timer_sync(&xhci->shared_hcd->rh_timer); |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 688 | |
Chunfeng Yun | 82dad9f | 2021-04-10 13:10:02 +0800 | [diff] [blame] | 689 | ret = xhci_mtk_host_disable(mtk); |
| 690 | if (ret) |
| 691 | goto restart_poll_rh; |
| 692 | |
Chunfeng Yun | 7fed636 | 2021-04-10 13:10:05 +0800 | [diff] [blame] | 693 | clk_bulk_disable_unprepare(BULK_CLKS_NUM, mtk->clks); |
Chunfeng Yun | a2ecc4d | 2018-01-03 16:53:20 +0800 | [diff] [blame] | 694 | usb_wakeup_set(mtk, true); |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 695 | return 0; |
Chunfeng Yun | 82dad9f | 2021-04-10 13:10:02 +0800 | [diff] [blame] | 696 | |
| 697 | restart_poll_rh: |
| 698 | xhci_dbg(xhci, "%s: restart port polling\n", __func__); |
| 699 | set_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags); |
| 700 | usb_hcd_poll_rh_status(xhci->shared_hcd); |
| 701 | set_bit(HCD_FLAG_POLL_RH, &hcd->flags); |
| 702 | usb_hcd_poll_rh_status(hcd); |
| 703 | return ret; |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 704 | } |
| 705 | |
Arnd Bergmann | 8dac530 | 2016-03-02 16:24:04 +0100 | [diff] [blame] | 706 | static int __maybe_unused xhci_mtk_resume(struct device *dev) |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 707 | { |
| 708 | struct xhci_hcd_mtk *mtk = dev_get_drvdata(dev); |
Chunfeng Yun | 882fa27 | 2016-01-26 17:50:10 +0200 | [diff] [blame] | 709 | struct usb_hcd *hcd = mtk->hcd; |
| 710 | struct xhci_hcd *xhci = hcd_to_xhci(hcd); |
Chunfeng Yun | 82dad9f | 2021-04-10 13:10:02 +0800 | [diff] [blame] | 711 | int ret; |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 712 | |
Chunfeng Yun | a2ecc4d | 2018-01-03 16:53:20 +0800 | [diff] [blame] | 713 | usb_wakeup_set(mtk, false); |
Chunfeng Yun | 7fed636 | 2021-04-10 13:10:05 +0800 | [diff] [blame] | 714 | ret = clk_bulk_prepare_enable(BULK_CLKS_NUM, mtk->clks); |
Chunfeng Yun | 82dad9f | 2021-04-10 13:10:02 +0800 | [diff] [blame] | 715 | if (ret) |
| 716 | goto enable_wakeup; |
| 717 | |
| 718 | ret = xhci_mtk_host_enable(mtk); |
| 719 | if (ret) |
| 720 | goto disable_clks; |
Chunfeng Yun | 882fa27 | 2016-01-26 17:50:10 +0200 | [diff] [blame] | 721 | |
| 722 | xhci_dbg(xhci, "%s: restart port polling\n", __func__); |
Chunfeng Yun | 882fa27 | 2016-01-26 17:50:10 +0200 | [diff] [blame] | 723 | set_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags); |
| 724 | usb_hcd_poll_rh_status(xhci->shared_hcd); |
Chunfeng Yun | 555df58 | 2018-10-01 18:36:08 +0300 | [diff] [blame] | 725 | set_bit(HCD_FLAG_POLL_RH, &hcd->flags); |
| 726 | usb_hcd_poll_rh_status(hcd); |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 727 | return 0; |
Chunfeng Yun | 82dad9f | 2021-04-10 13:10:02 +0800 | [diff] [blame] | 728 | |
| 729 | disable_clks: |
Chunfeng Yun | 7fed636 | 2021-04-10 13:10:05 +0800 | [diff] [blame] | 730 | clk_bulk_disable_unprepare(BULK_CLKS_NUM, mtk->clks); |
Chunfeng Yun | 82dad9f | 2021-04-10 13:10:02 +0800 | [diff] [blame] | 731 | enable_wakeup: |
| 732 | usb_wakeup_set(mtk, true); |
| 733 | return ret; |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 734 | } |
| 735 | |
Chunfeng Yun | 04284eb | 2021-04-10 13:10:04 +0800 | [diff] [blame] | 736 | static int __maybe_unused xhci_mtk_runtime_suspend(struct device *dev) |
| 737 | { |
| 738 | struct xhci_hcd_mtk *mtk = dev_get_drvdata(dev); |
| 739 | struct xhci_hcd *xhci = hcd_to_xhci(mtk->hcd); |
| 740 | int ret = 0; |
| 741 | |
| 742 | if (xhci->xhc_state) |
| 743 | return -ESHUTDOWN; |
| 744 | |
Chunfeng Yun | 5951b7c | 2021-04-16 14:48:26 +0800 | [diff] [blame] | 745 | if (device_may_wakeup(dev)) |
| 746 | ret = xhci_mtk_suspend(dev); |
Chunfeng Yun | 04284eb | 2021-04-10 13:10:04 +0800 | [diff] [blame] | 747 | |
| 748 | /* -EBUSY: let PM automatically reschedule another autosuspend */ |
| 749 | return ret ? -EBUSY : 0; |
| 750 | } |
| 751 | |
| 752 | static int __maybe_unused xhci_mtk_runtime_resume(struct device *dev) |
| 753 | { |
| 754 | struct xhci_hcd_mtk *mtk = dev_get_drvdata(dev); |
| 755 | struct xhci_hcd *xhci = hcd_to_xhci(mtk->hcd); |
| 756 | int ret = 0; |
| 757 | |
| 758 | if (xhci->xhc_state) |
| 759 | return -ESHUTDOWN; |
| 760 | |
| 761 | if (device_may_wakeup(dev)) |
| 762 | ret = xhci_mtk_resume(dev); |
| 763 | |
| 764 | return ret; |
| 765 | } |
| 766 | |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 767 | static const struct dev_pm_ops xhci_mtk_pm_ops = { |
| 768 | SET_SYSTEM_SLEEP_PM_OPS(xhci_mtk_suspend, xhci_mtk_resume) |
Chunfeng Yun | 04284eb | 2021-04-10 13:10:04 +0800 | [diff] [blame] | 769 | SET_RUNTIME_PM_OPS(xhci_mtk_runtime_suspend, |
| 770 | xhci_mtk_runtime_resume, NULL) |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 771 | }; |
Chunfeng Yun | 04284eb | 2021-04-10 13:10:04 +0800 | [diff] [blame] | 772 | |
| 773 | #define DEV_PM_OPS (IS_ENABLED(CONFIG_PM) ? &xhci_mtk_pm_ops : NULL) |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 774 | |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 775 | static const struct of_device_id mtk_xhci_of_match[] = { |
| 776 | { .compatible = "mediatek,mt8173-xhci"}, |
Chunfeng Yun | 926d60a | 2021-08-17 16:36:29 +0800 | [diff] [blame] | 777 | { .compatible = "mediatek,mt8195-xhci"}, |
Chunfeng Yun | 5675b4d | 2017-08-30 19:34:09 +0800 | [diff] [blame] | 778 | { .compatible = "mediatek,mtk-xhci"}, |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 779 | { }, |
| 780 | }; |
| 781 | MODULE_DEVICE_TABLE(of, mtk_xhci_of_match); |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 782 | |
| 783 | static struct platform_driver mtk_xhci_driver = { |
| 784 | .probe = xhci_mtk_probe, |
| 785 | .remove = xhci_mtk_remove, |
| 786 | .driver = { |
| 787 | .name = "xhci-mtk", |
| 788 | .pm = DEV_PM_OPS, |
Chunfeng Yun | 6144ef3 | 2021-03-23 15:02:50 +0800 | [diff] [blame] | 789 | .of_match_table = mtk_xhci_of_match, |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 790 | }, |
| 791 | }; |
Chunfeng Yun | 0cbd4b3 | 2015-11-24 13:09:55 +0200 | [diff] [blame] | 792 | |
| 793 | static int __init xhci_mtk_init(void) |
| 794 | { |
| 795 | xhci_init_driver(&xhci_mtk_hc_driver, &xhci_mtk_overrides); |
| 796 | return platform_driver_register(&mtk_xhci_driver); |
| 797 | } |
| 798 | module_init(xhci_mtk_init); |
| 799 | |
| 800 | static void __exit xhci_mtk_exit(void) |
| 801 | { |
| 802 | platform_driver_unregister(&mtk_xhci_driver); |
| 803 | } |
| 804 | module_exit(xhci_mtk_exit); |
| 805 | |
| 806 | MODULE_AUTHOR("Chunfeng Yun <chunfeng.yun@mediatek.com>"); |
| 807 | MODULE_DESCRIPTION("MediaTek xHCI Host Controller Driver"); |
| 808 | MODULE_LICENSE("GPL v2"); |