xhci-mtk.c 21 KB

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