blob: 91738af0ab14d449245762c647eacb549d4da836 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +02002/*
3 * MediaTek xHCI Host Controller Driver
4 *
5 * Copyright (c) 2015 MediaTek Inc.
6 * Author:
7 * Chunfeng Yun <chunfeng.yun@mediatek.com>
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +02008 */
9
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +020010#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 Yun0cbd4b32015-11-24 13:09:55 +020016#include <linux/platform_device.h>
17#include <linux/pm_runtime.h>
Chunfeng Yun04284eb2021-04-10 13:10:04 +080018#include <linux/pm_wakeirq.h>
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +020019#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 Yunce370bf2017-10-13 16:26:35 +080036#define STS1_U3_MAC_RST BIT(16)
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +020037#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 Yun926d60a2021-08-17 16:36:29 +080059/* 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 Yuna2ecc4d2018-01-03 16:53:20 +080080/* usb remote wakeup registers in syscon */
Chunfeng Yunc03b4cc2021-03-23 15:02:48 +080081
Chunfeng Yuna2ecc4d2018-01-03 16:53:20 +080082/* 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 Yun0cbd4b32015-11-24 13:09:55 +020087
Chunfeng Yunc03b4cc2021-03-23 15:02:48 +080088/* 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 Yun331c5052021-03-23 15:02:49 +080094/* mt8192 */
95#define WC0_SSUSB0_CDEN BIT(6)
96#define WC0_IS_SPM_EN BIT(1)
97
Chunfeng Yuna2ecc4d2018-01-03 16:53:20 +080098/* 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 Yun0cbd4b32015-11-24 13:09:55 +0200102
Chunfeng Yuna2ecc4d2018-01-03 16:53:20 +0800103enum ssusb_uwk_vers {
104 SSUSB_UWK_V1 = 1,
105 SSUSB_UWK_V2,
Chunfeng Yunc03b4cc2021-03-23 15:02:48 +0800106 SSUSB_UWK_V1_1 = 101, /* specific revision 1.01 */
Chunfeng Yun331c5052021-03-23 15:02:49 +0800107 SSUSB_UWK_V1_2, /* specific revision 1.2 */
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200108};
109
Chunfeng Yun926d60a2021-08-17 16:36:29 +0800110/*
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 */
115static 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 Yun0cbd4b32015-11-24 13:09:55 +0200150static 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 Wunderlich6e18cfc2020-08-08 14:49:06 +0200154 int u3_ports_disabled = 0;
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200155 int ret;
156 int i;
157
Chunfeng Yun065d48c2016-10-19 10:28:22 +0800158 if (!mtk->has_ippc)
159 return 0;
160
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200161 /* 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 Yun55ba6e92017-10-13 16:26:36 +0800166 /* power on and enable u3 ports except skipped ones */
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200167 for (i = 0; i < mtk->num_u3_ports; i++) {
Chunfeng Yun55ba6e92017-10-13 16:26:36 +0800168 if ((0x1 << i) & mtk->u3p_dis_msk) {
Frank Wunderlich6e18cfc2020-08-08 14:49:06 +0200169 u3_ports_disabled++;
Chunfeng Yun55ba6e92017-10-13 16:26:36 +0800170 continue;
171 }
172
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200173 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 Yun7465d7b2021-08-17 16:36:24 +0800179 /* power on and enable all u2 ports except skipped ones */
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200180 for (i = 0; i < mtk->num_u2_ports; i++) {
Chunfeng Yun7465d7b2021-08-17 16:36:24 +0800181 if (BIT(i) & mtk->u2p_dis_msk)
182 continue;
183
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200184 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 Wunderlich6e18cfc2020-08-08 14:49:06 +0200197 if (mtk->num_u3_ports > u3_ports_disabled)
Chunfeng Yunce370bf2017-10-13 16:26:35 +0800198 check_val |= STS1_U3_MAC_RST;
199
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200200 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
210static 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 Yun065d48c2016-10-19 10:28:22 +0800217 if (!mtk->has_ippc)
218 return 0;
219
Chunfeng Yun55ba6e92017-10-13 16:26:36 +0800220 /* power down u3 ports except skipped ones */
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200221 for (i = 0; i < mtk->num_u3_ports; i++) {
Chunfeng Yun55ba6e92017-10-13 16:26:36 +0800222 if ((0x1 << i) & mtk->u3p_dis_msk)
223 continue;
224
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200225 value = readl(&ippc->u3_ctrl_p[i]);
226 value |= CTRL_U3_PORT_PDN;
227 writel(value, &ippc->u3_ctrl_p[i]);
228 }
229
Chunfeng Yun7465d7b2021-08-17 16:36:24 +0800230 /* power down all u2 ports except skipped ones */
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200231 for (i = 0; i < mtk->num_u2_ports; i++) {
Chunfeng Yun7465d7b2021-08-17 16:36:24 +0800232 if (BIT(i) & mtk->u2p_dis_msk)
233 continue;
234
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200235 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 Yun0d8cfee2021-11-02 10:50:04 +0800248 if (ret)
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200249 dev_err(mtk->dev, "ip sleep failed!!!\n");
Chunfeng Yun0d8cfee2021-11-02 10:50:04 +0800250 else /* workaound for platforms using low level latch */
251 usleep_range(100, 200);
252
253 return ret;
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200254}
255
256static 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 Yun065d48c2016-10-19 10:28:22 +0800261 if (!mtk->has_ippc)
262 return 0;
263
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200264 /* 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 Yun0cbd4b32015-11-24 13:09:55 +0200290/* only clocks can be turn off for ip-sleep wakeup mode */
Chunfeng Yuna2ecc4d2018-01-03 16:53:20 +0800291static void usb_wakeup_ip_sleep_set(struct xhci_hcd_mtk *mtk, bool enable)
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200292{
Chunfeng Yuna2ecc4d2018-01-03 16:53:20 +0800293 u32 reg, msk, val;
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200294
Chunfeng Yuna2ecc4d2018-01-03 16:53:20 +0800295 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 Yunc03b4cc2021-03-23 15:02:48 +0800301 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 Yun0d8cfee2021-11-02 10:50:04 +0800304 val = enable ? (WC0_IS_EN | WC0_IS_C(0x1)) : 0;
Chunfeng Yunc03b4cc2021-03-23 15:02:48 +0800305 break;
Chunfeng Yun331c5052021-03-23 15:02:49 +0800306 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 Yuna2ecc4d2018-01-03 16:53:20 +0800311 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 Wudae06202018-01-13 01:46:49 +0800318 }
Chunfeng Yuna2ecc4d2018-01-03 16:53:20 +0800319 regmap_update_bits(mtk->uwk, reg, msk, val);
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200320}
321
322static int usb_wakeup_of_property_parse(struct xhci_hcd_mtk *mtk,
323 struct device_node *dn)
324{
Chunfeng Yuna2ecc4d2018-01-03 16:53:20 +0800325 struct of_phandle_args args;
326 int ret;
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200327
Chunfeng Yuna2ecc4d2018-01-03 16:53:20 +0800328 /* Wakeup function is optional */
329 mtk->uwk_en = of_property_read_bool(dn, "wakeup-source");
330 if (!mtk->uwk_en)
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200331 return 0;
332
Chunfeng Yuna2ecc4d2018-01-03 16:53:20 +0800333 ret = of_parse_phandle_with_fixed_args(dn,
334 "mediatek,syscon-wakeup", 2, 0, &args);
335 if (ret)
336 return ret;
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200337
Chunfeng Yuna2ecc4d2018-01-03 16:53:20 +0800338 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 Yuna2ecc4d2018-01-03 16:53:20 +0800346}
347
348static 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 Yun0cbd4b32015-11-24 13:09:55 +0200352}
353
Chunfeng Yun7fed6362021-04-10 13:10:05 +0800354static 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 Yun0cbd4b32015-11-24 13:09:55 +0200367static 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
386static void xhci_mtk_ldos_disable(struct xhci_hcd_mtk *mtk)
387{
388 regulator_disable(mtk->vbus);
389 regulator_disable(mtk->vusb33);
390}
391
392static 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 Yunbee1f892021-03-31 17:05:53 +0800411 if (mtk->u2_lpm_disable)
412 xhci->quirks |= XHCI_HW_LPM_DISABLE;
Chunfeng Yun1f743c82021-03-31 17:05:52 +0800413
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 Yun0cbd4b32015-11-24 13:09:55 +0200420}
421
422/* called during probe() after chip reset completes */
423static 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 Yun926d60a2021-08-17 16:36:29 +0800432
433 /* workaround only for mt8195 */
434 xhci_mtk_set_frame_interval(mtk);
Chunfeng Yun065d48c2016-10-19 10:28:22 +0800435 }
436
437 ret = xhci_gen_setup(hcd, xhci_mtk_quirks);
438 if (ret)
439 return ret;
440
Chunfeng Yun38269d22021-11-02 10:50:03 +0800441 if (usb_hcd_is_primary_hcd(hcd))
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200442 ret = xhci_mtk_sch_init(mtk);
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200443
Chunfeng Yun065d48c2016-10-19 10:28:22 +0800444 return ret;
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200445}
446
Chunfeng Yundc9d3b22021-03-08 10:52:03 +0800447static const struct xhci_driver_overrides xhci_mtk_overrides __initconst = {
448 .reset = xhci_mtk_setup,
Chunfeng Yun14295a1502021-03-08 10:52:04 +0800449 .add_endpoint = xhci_mtk_add_ep,
450 .drop_endpoint = xhci_mtk_drop_ep,
Chunfeng Yundc9d3b22021-03-08 10:52:03 +0800451 .check_bandwidth = xhci_mtk_check_bandwidth,
452 .reset_bandwidth = xhci_mtk_reset_bandwidth,
453};
454
455static struct hc_driver __read_mostly xhci_mtk_hc_driver;
456
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200457static 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 Yun0cbd4b32015-11-24 13:09:55 +0200466 int ret = -ENODEV;
Chunfeng Yun04284eb2021-04-10 13:10:04 +0800467 int wakeup_irq;
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200468 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 Yunb6bb72c2017-10-13 16:26:38 +0800491 ret = xhci_mtk_clks_get(mtk);
492 if (ret)
493 return ret;
Chunfeng Yun9c4afd42017-01-18 14:08:24 +0800494
Chunfeng Yun04284eb2021-04-10 13:10:04 +0800495 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 Yun0cbd4b32015-11-24 13:09:55 +0200510 mtk->lpm_support = of_property_read_bool(node, "usb3-lpm-capable");
Chunfeng Yunbee1f892021-03-31 17:05:53 +0800511 mtk->u2_lpm_disable = of_property_read_bool(node, "usb2-lpm-disable");
Chunfeng Yun55ba6e92017-10-13 16:26:36 +0800512 /* 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 Yun7465d7b2021-08-17 16:36:24 +0800515 of_property_read_u32(node, "mediatek,u2p-dis-msk",
516 &mtk->u2p_dis_msk);
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200517
518 ret = usb_wakeup_of_property_parse(mtk, node);
Chunfeng Yuna2ecc4d2018-01-03 16:53:20 +0800519 if (ret) {
520 dev_err(dev, "failed to parse uwk property\n");
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200521 return ret;
Chunfeng Yuna2ecc4d2018-01-03 16:53:20 +0800522 }
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200523
Chunfeng Yun04284eb2021-04-10 13:10:04 +0800524 pm_runtime_set_active(dev);
525 pm_runtime_use_autosuspend(dev);
526 pm_runtime_set_autosuspend_delay(dev, 4000);
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200527 pm_runtime_enable(dev);
528 pm_runtime_get_sync(dev);
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200529
530 ret = xhci_mtk_ldos_enable(mtk);
531 if (ret)
532 goto disable_pm;
533
Chunfeng Yun7fed6362021-04-10 13:10:05 +0800534 ret = clk_bulk_prepare_enable(BULK_CLKS_NUM, mtk->clks);
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200535 if (ret)
536 goto disable_ldos;
537
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200538 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 Yun065d48c2016-10-19 10:28:22 +0800551 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mac");
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200552 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 Yun065d48c2016-10-19 10:28:22 +0800560 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 Yun0cbd4b32015-11-24 13:09:55 +0200568 }
569
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200570 device_init_wakeup(dev, true);
571
572 xhci = hcd_to_xhci(hcd);
573 xhci->main_hcd = hcd;
Adam Wallisab725cb2017-12-08 17:59:13 +0200574
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 Yun0cbd4b32015-11-24 13:09:55 +0200583 xhci->shared_hcd = usb_create_shared_hcd(driver, dev,
584 dev_name(dev), hcd);
585 if (!xhci->shared_hcd) {
586 ret = -ENOMEM;
Martin Blumenstingl6ae9f502018-03-03 22:43:06 +0100587 goto disable_device_wakeup;
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200588 }
589
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200590 ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
591 if (ret)
592 goto put_usb3_hcd;
593
Chunfeng Yun1f743c82021-03-31 17:05:52 +0800594 if (HCC_MAX_PSA(xhci->hcc_params) >= 4 &&
595 !(xhci->quirks & XHCI_BROKEN_STREAMS))
Chunfeng Yun94a631d2017-03-09 15:39:34 +0200596 xhci->shared_hcd->can_do_streams = 1;
597
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200598 ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
599 if (ret)
600 goto dealloc_usb2_hcd;
601
Chunfeng Yun04284eb2021-04-10 13:10:04 +0800602 if (wakeup_irq > 0) {
Chunfeng Yun05372822021-10-25 15:01:54 +0800603 ret = dev_pm_set_dedicated_wake_irq_reverse(dev, wakeup_irq);
Chunfeng Yun04284eb2021-04-10 13:10:04 +0800604 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 Yun0cbd4b32015-11-24 13:09:55 +0200616 return 0;
617
Chunfeng Yun04284eb2021-04-10 13:10:04 +0800618dealloc_usb3_hcd:
619 usb_remove_hcd(xhci->shared_hcd);
620 xhci->shared_hcd = NULL;
621
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200622dealloc_usb2_hcd:
623 usb_remove_hcd(hcd);
624
625put_usb3_hcd:
626 xhci_mtk_sch_exit(mtk);
627 usb_put_hcd(xhci->shared_hcd);
628
Martin Blumenstingl6ae9f502018-03-03 22:43:06 +0100629disable_device_wakeup:
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200630 device_init_wakeup(dev, false);
631
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200632put_usb2_hcd:
633 usb_put_hcd(hcd);
634
635disable_clk:
Chunfeng Yun7fed6362021-04-10 13:10:05 +0800636 clk_bulk_disable_unprepare(BULK_CLKS_NUM, mtk->clks);
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200637
638disable_ldos:
639 xhci_mtk_ldos_disable(mtk);
640
641disable_pm:
Chunfeng Yun7f85c162021-08-17 16:36:23 +0800642 pm_runtime_put_noidle(dev);
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200643 pm_runtime_disable(dev);
644 return ret;
645}
646
Chunfeng Yun04284eb2021-04-10 13:10:04 +0800647static int xhci_mtk_remove(struct platform_device *pdev)
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200648{
Chunfeng Yun04284eb2021-04-10 13:10:04 +0800649 struct xhci_hcd_mtk *mtk = platform_get_drvdata(pdev);
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200650 struct usb_hcd *hcd = mtk->hcd;
651 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Mathias Nymanf0680902018-11-09 17:21:17 +0200652 struct usb_hcd *shared_hcd = xhci->shared_hcd;
Chunfeng Yun04284eb2021-04-10 13:10:04 +0800653 struct device *dev = &pdev->dev;
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200654
Chunfeng Yun04284eb2021-04-10 13:10:04 +0800655 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 Lina24d5072020-06-24 16:59:47 +0300659
Mathias Nymanf0680902018-11-09 17:21:17 +0200660 usb_remove_hcd(shared_hcd);
661 xhci->shared_hcd = NULL;
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200662 usb_remove_hcd(hcd);
Mathias Nymanf0680902018-11-09 17:21:17 +0200663 usb_put_hcd(shared_hcd);
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200664 usb_put_hcd(hcd);
665 xhci_mtk_sch_exit(mtk);
Chunfeng Yun7fed6362021-04-10 13:10:05 +0800666 clk_bulk_disable_unprepare(BULK_CLKS_NUM, mtk->clks);
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200667 xhci_mtk_ldos_disable(mtk);
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200668
Chunfeng Yun04284eb2021-04-10 13:10:04 +0800669 pm_runtime_disable(dev);
670 pm_runtime_put_noidle(dev);
671 pm_runtime_set_suspended(dev);
672
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200673 return 0;
674}
675
Arnd Bergmann8dac5302016-03-02 16:24:04 +0100676static int __maybe_unused xhci_mtk_suspend(struct device *dev)
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200677{
678 struct xhci_hcd_mtk *mtk = dev_get_drvdata(dev);
Chunfeng Yun882fa272016-01-26 17:50:10 +0200679 struct usb_hcd *hcd = mtk->hcd;
680 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Chunfeng Yun82dad9f2021-04-10 13:10:02 +0800681 int ret;
Chunfeng Yun882fa272016-01-26 17:50:10 +0200682
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 Yun0cbd4b32015-11-24 13:09:55 +0200688
Chunfeng Yun82dad9f2021-04-10 13:10:02 +0800689 ret = xhci_mtk_host_disable(mtk);
690 if (ret)
691 goto restart_poll_rh;
692
Chunfeng Yun7fed6362021-04-10 13:10:05 +0800693 clk_bulk_disable_unprepare(BULK_CLKS_NUM, mtk->clks);
Chunfeng Yuna2ecc4d2018-01-03 16:53:20 +0800694 usb_wakeup_set(mtk, true);
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200695 return 0;
Chunfeng Yun82dad9f2021-04-10 13:10:02 +0800696
697restart_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 Yun0cbd4b32015-11-24 13:09:55 +0200704}
705
Arnd Bergmann8dac5302016-03-02 16:24:04 +0100706static int __maybe_unused xhci_mtk_resume(struct device *dev)
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200707{
708 struct xhci_hcd_mtk *mtk = dev_get_drvdata(dev);
Chunfeng Yun882fa272016-01-26 17:50:10 +0200709 struct usb_hcd *hcd = mtk->hcd;
710 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Chunfeng Yun82dad9f2021-04-10 13:10:02 +0800711 int ret;
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200712
Chunfeng Yuna2ecc4d2018-01-03 16:53:20 +0800713 usb_wakeup_set(mtk, false);
Chunfeng Yun7fed6362021-04-10 13:10:05 +0800714 ret = clk_bulk_prepare_enable(BULK_CLKS_NUM, mtk->clks);
Chunfeng Yun82dad9f2021-04-10 13:10:02 +0800715 if (ret)
716 goto enable_wakeup;
717
718 ret = xhci_mtk_host_enable(mtk);
719 if (ret)
720 goto disable_clks;
Chunfeng Yun882fa272016-01-26 17:50:10 +0200721
722 xhci_dbg(xhci, "%s: restart port polling\n", __func__);
Chunfeng Yun882fa272016-01-26 17:50:10 +0200723 set_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags);
724 usb_hcd_poll_rh_status(xhci->shared_hcd);
Chunfeng Yun555df582018-10-01 18:36:08 +0300725 set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
726 usb_hcd_poll_rh_status(hcd);
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200727 return 0;
Chunfeng Yun82dad9f2021-04-10 13:10:02 +0800728
729disable_clks:
Chunfeng Yun7fed6362021-04-10 13:10:05 +0800730 clk_bulk_disable_unprepare(BULK_CLKS_NUM, mtk->clks);
Chunfeng Yun82dad9f2021-04-10 13:10:02 +0800731enable_wakeup:
732 usb_wakeup_set(mtk, true);
733 return ret;
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200734}
735
Chunfeng Yun04284eb2021-04-10 13:10:04 +0800736static 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 Yun5951b7c2021-04-16 14:48:26 +0800745 if (device_may_wakeup(dev))
746 ret = xhci_mtk_suspend(dev);
Chunfeng Yun04284eb2021-04-10 13:10:04 +0800747
748 /* -EBUSY: let PM automatically reschedule another autosuspend */
749 return ret ? -EBUSY : 0;
750}
751
752static 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 Yun0cbd4b32015-11-24 13:09:55 +0200767static const struct dev_pm_ops xhci_mtk_pm_ops = {
768 SET_SYSTEM_SLEEP_PM_OPS(xhci_mtk_suspend, xhci_mtk_resume)
Chunfeng Yun04284eb2021-04-10 13:10:04 +0800769 SET_RUNTIME_PM_OPS(xhci_mtk_runtime_suspend,
770 xhci_mtk_runtime_resume, NULL)
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200771};
Chunfeng Yun04284eb2021-04-10 13:10:04 +0800772
773#define DEV_PM_OPS (IS_ENABLED(CONFIG_PM) ? &xhci_mtk_pm_ops : NULL)
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200774
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200775static const struct of_device_id mtk_xhci_of_match[] = {
776 { .compatible = "mediatek,mt8173-xhci"},
Chunfeng Yun926d60a2021-08-17 16:36:29 +0800777 { .compatible = "mediatek,mt8195-xhci"},
Chunfeng Yun5675b4d2017-08-30 19:34:09 +0800778 { .compatible = "mediatek,mtk-xhci"},
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200779 { },
780};
781MODULE_DEVICE_TABLE(of, mtk_xhci_of_match);
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200782
783static 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 Yun6144ef32021-03-23 15:02:50 +0800789 .of_match_table = mtk_xhci_of_match,
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200790 },
791};
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200792
793static 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}
798module_init(xhci_mtk_init);
799
800static void __exit xhci_mtk_exit(void)
801{
802 platform_driver_unregister(&mtk_xhci_driver);
803}
804module_exit(xhci_mtk_exit);
805
806MODULE_AUTHOR("Chunfeng Yun <chunfeng.yun@mediatek.com>");
807MODULE_DESCRIPTION("MediaTek xHCI Host Controller Driver");
808MODULE_LICENSE("GPL v2");