xhci-mtk.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  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 <chunfeng.yun@mediatek.com>
  8. */
  9. #include <linux/clk.h>
  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>
  16. #include <linux/platform_device.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/regmap.h>
  19. #include <linux/regulator/consumer.h>
  20. #include "xhci.h"
  21. #include "xhci-mtk.h"
  22. /* ip_pw_ctrl0 register */
  23. #define CTRL0_IP_SW_RST BIT(0)
  24. /* ip_pw_ctrl1 register */
  25. #define CTRL1_IP_HOST_PDN BIT(0)
  26. /* ip_pw_ctrl2 register */
  27. #define CTRL2_IP_DEV_PDN BIT(0)
  28. /* ip_pw_sts1 register */
  29. #define STS1_IP_SLEEP_STS BIT(30)
  30. #define STS1_U3_MAC_RST BIT(16)
  31. #define STS1_XHCI_RST BIT(11)
  32. #define STS1_SYS125_RST BIT(10)
  33. #define STS1_REF_RST BIT(8)
  34. #define STS1_SYSPLL_STABLE BIT(0)
  35. /* ip_xhci_cap register */
  36. #define CAP_U3_PORT_NUM(p) ((p) & 0xff)
  37. #define CAP_U2_PORT_NUM(p) (((p) >> 8) & 0xff)
  38. /* u3_ctrl_p register */
  39. #define CTRL_U3_PORT_HOST_SEL BIT(2)
  40. #define CTRL_U3_PORT_PDN BIT(1)
  41. #define CTRL_U3_PORT_DIS BIT(0)
  42. /* u2_ctrl_p register */
  43. #define CTRL_U2_PORT_HOST_SEL BIT(2)
  44. #define CTRL_U2_PORT_PDN BIT(1)
  45. #define CTRL_U2_PORT_DIS BIT(0)
  46. /* u2_phy_pll register */
  47. #define CTRL_U2_FORCE_PLL_STB BIT(28)
  48. /* usb remote wakeup registers in syscon */
  49. /* mt8173 etc */
  50. #define PERI_WK_CTRL1 0x4
  51. #define WC1_IS_C(x) (((x) & 0xf) << 26) /* cycle debounce */
  52. #define WC1_IS_EN BIT(25)
  53. #define WC1_IS_P BIT(6) /* polarity for ip sleep */
  54. /* mt2712 etc */
  55. #define PERI_SSUSB_SPM_CTRL 0x0
  56. #define SSC_IP_SLEEP_EN BIT(4)
  57. #define SSC_SPM_INT_EN BIT(1)
  58. enum ssusb_uwk_vers {
  59. SSUSB_UWK_V1 = 1,
  60. SSUSB_UWK_V2,
  61. };
  62. static int xhci_mtk_host_enable(struct xhci_hcd_mtk *mtk)
  63. {
  64. struct mu3c_ippc_regs __iomem *ippc = mtk->ippc_regs;
  65. u32 value, check_val;
  66. int u3_ports_disabed = 0;
  67. int ret;
  68. int i;
  69. if (!mtk->has_ippc)
  70. return 0;
  71. /* power on host ip */
  72. value = readl(&ippc->ip_pw_ctr1);
  73. value &= ~CTRL1_IP_HOST_PDN;
  74. writel(value, &ippc->ip_pw_ctr1);
  75. /* power on and enable u3 ports except skipped ones */
  76. for (i = 0; i < mtk->num_u3_ports; i++) {
  77. if ((0x1 << i) & mtk->u3p_dis_msk) {
  78. u3_ports_disabed++;
  79. continue;
  80. }
  81. value = readl(&ippc->u3_ctrl_p[i]);
  82. value &= ~(CTRL_U3_PORT_PDN | CTRL_U3_PORT_DIS);
  83. value |= CTRL_U3_PORT_HOST_SEL;
  84. writel(value, &ippc->u3_ctrl_p[i]);
  85. }
  86. /* power on and enable all u2 ports */
  87. for (i = 0; i < mtk->num_u2_ports; i++) {
  88. value = readl(&ippc->u2_ctrl_p[i]);
  89. value &= ~(CTRL_U2_PORT_PDN | CTRL_U2_PORT_DIS);
  90. value |= CTRL_U2_PORT_HOST_SEL;
  91. writel(value, &ippc->u2_ctrl_p[i]);
  92. }
  93. /*
  94. * wait for clocks to be stable, and clock domains reset to
  95. * be inactive after power on and enable ports
  96. */
  97. check_val = STS1_SYSPLL_STABLE | STS1_REF_RST |
  98. STS1_SYS125_RST | STS1_XHCI_RST;
  99. if (mtk->num_u3_ports > u3_ports_disabed)
  100. check_val |= STS1_U3_MAC_RST;
  101. ret = readl_poll_timeout(&ippc->ip_pw_sts1, value,
  102. (check_val == (value & check_val)), 100, 20000);
  103. if (ret) {
  104. dev_err(mtk->dev, "clocks are not stable (0x%x)\n", value);
  105. return ret;
  106. }
  107. return 0;
  108. }
  109. static int xhci_mtk_host_disable(struct xhci_hcd_mtk *mtk)
  110. {
  111. struct mu3c_ippc_regs __iomem *ippc = mtk->ippc_regs;
  112. u32 value;
  113. int ret;
  114. int i;
  115. if (!mtk->has_ippc)
  116. return 0;
  117. /* power down u3 ports except skipped ones */
  118. for (i = 0; i < mtk->num_u3_ports; i++) {
  119. if ((0x1 << i) & mtk->u3p_dis_msk)
  120. continue;
  121. value = readl(&ippc->u3_ctrl_p[i]);
  122. value |= CTRL_U3_PORT_PDN;
  123. writel(value, &ippc->u3_ctrl_p[i]);
  124. }
  125. /* power down all u2 ports */
  126. for (i = 0; i < mtk->num_u2_ports; i++) {
  127. value = readl(&ippc->u2_ctrl_p[i]);
  128. value |= CTRL_U2_PORT_PDN;
  129. writel(value, &ippc->u2_ctrl_p[i]);
  130. }
  131. /* power down host ip */
  132. value = readl(&ippc->ip_pw_ctr1);
  133. value |= CTRL1_IP_HOST_PDN;
  134. writel(value, &ippc->ip_pw_ctr1);
  135. /* wait for host ip to sleep */
  136. ret = readl_poll_timeout(&ippc->ip_pw_sts1, value,
  137. (value & STS1_IP_SLEEP_STS), 100, 100000);
  138. if (ret) {
  139. dev_err(mtk->dev, "ip sleep failed!!!\n");
  140. return ret;
  141. }
  142. return 0;
  143. }
  144. static int xhci_mtk_ssusb_config(struct xhci_hcd_mtk *mtk)
  145. {
  146. struct mu3c_ippc_regs __iomem *ippc = mtk->ippc_regs;
  147. u32 value;
  148. if (!mtk->has_ippc)
  149. return 0;
  150. /* reset whole ip */
  151. value = readl(&ippc->ip_pw_ctr0);
  152. value |= CTRL0_IP_SW_RST;
  153. writel(value, &ippc->ip_pw_ctr0);
  154. udelay(1);
  155. value = readl(&ippc->ip_pw_ctr0);
  156. value &= ~CTRL0_IP_SW_RST;
  157. writel(value, &ippc->ip_pw_ctr0);
  158. /*
  159. * device ip is default power-on in fact
  160. * power down device ip, otherwise ip-sleep will fail
  161. */
  162. value = readl(&ippc->ip_pw_ctr2);
  163. value |= CTRL2_IP_DEV_PDN;
  164. writel(value, &ippc->ip_pw_ctr2);
  165. value = readl(&ippc->ip_xhci_cap);
  166. mtk->num_u3_ports = CAP_U3_PORT_NUM(value);
  167. mtk->num_u2_ports = CAP_U2_PORT_NUM(value);
  168. dev_dbg(mtk->dev, "%s u2p:%d, u3p:%d\n", __func__,
  169. mtk->num_u2_ports, mtk->num_u3_ports);
  170. return xhci_mtk_host_enable(mtk);
  171. }
  172. /* ignore the error if the clock does not exist */
  173. static struct clk *optional_clk_get(struct device *dev, const char *id)
  174. {
  175. struct clk *opt_clk;
  176. opt_clk = devm_clk_get(dev, id);
  177. /* ignore error number except EPROBE_DEFER */
  178. if (IS_ERR(opt_clk) && (PTR_ERR(opt_clk) != -EPROBE_DEFER))
  179. opt_clk = NULL;
  180. return opt_clk;
  181. }
  182. static int xhci_mtk_clks_get(struct xhci_hcd_mtk *mtk)
  183. {
  184. struct device *dev = mtk->dev;
  185. mtk->sys_clk = devm_clk_get(dev, "sys_ck");
  186. if (IS_ERR(mtk->sys_clk)) {
  187. dev_err(dev, "fail to get sys_ck\n");
  188. return PTR_ERR(mtk->sys_clk);
  189. }
  190. mtk->ref_clk = optional_clk_get(dev, "ref_ck");
  191. if (IS_ERR(mtk->ref_clk))
  192. return PTR_ERR(mtk->ref_clk);
  193. mtk->mcu_clk = optional_clk_get(dev, "mcu_ck");
  194. if (IS_ERR(mtk->mcu_clk))
  195. return PTR_ERR(mtk->mcu_clk);
  196. mtk->dma_clk = optional_clk_get(dev, "dma_ck");
  197. return PTR_ERR_OR_ZERO(mtk->dma_clk);
  198. }
  199. static int xhci_mtk_clks_enable(struct xhci_hcd_mtk *mtk)
  200. {
  201. int ret;
  202. ret = clk_prepare_enable(mtk->ref_clk);
  203. if (ret) {
  204. dev_err(mtk->dev, "failed to enable ref_clk\n");
  205. goto ref_clk_err;
  206. }
  207. ret = clk_prepare_enable(mtk->sys_clk);
  208. if (ret) {
  209. dev_err(mtk->dev, "failed to enable sys_clk\n");
  210. goto sys_clk_err;
  211. }
  212. ret = clk_prepare_enable(mtk->mcu_clk);
  213. if (ret) {
  214. dev_err(mtk->dev, "failed to enable mcu_clk\n");
  215. goto mcu_clk_err;
  216. }
  217. ret = clk_prepare_enable(mtk->dma_clk);
  218. if (ret) {
  219. dev_err(mtk->dev, "failed to enable dma_clk\n");
  220. goto dma_clk_err;
  221. }
  222. return 0;
  223. dma_clk_err:
  224. clk_disable_unprepare(mtk->mcu_clk);
  225. mcu_clk_err:
  226. clk_disable_unprepare(mtk->sys_clk);
  227. sys_clk_err:
  228. clk_disable_unprepare(mtk->ref_clk);
  229. ref_clk_err:
  230. return ret;
  231. }
  232. static void xhci_mtk_clks_disable(struct xhci_hcd_mtk *mtk)
  233. {
  234. clk_disable_unprepare(mtk->dma_clk);
  235. clk_disable_unprepare(mtk->mcu_clk);
  236. clk_disable_unprepare(mtk->sys_clk);
  237. clk_disable_unprepare(mtk->ref_clk);
  238. }
  239. /* only clocks can be turn off for ip-sleep wakeup mode */
  240. static void usb_wakeup_ip_sleep_set(struct xhci_hcd_mtk *mtk, bool enable)
  241. {
  242. u32 reg, msk, val;
  243. switch (mtk->uwk_vers) {
  244. case SSUSB_UWK_V1:
  245. reg = mtk->uwk_reg_base + PERI_WK_CTRL1;
  246. msk = WC1_IS_EN | WC1_IS_C(0xf) | WC1_IS_P;
  247. val = enable ? (WC1_IS_EN | WC1_IS_C(0x8)) : 0;
  248. break;
  249. case SSUSB_UWK_V2:
  250. reg = mtk->uwk_reg_base + PERI_SSUSB_SPM_CTRL;
  251. msk = SSC_IP_SLEEP_EN | SSC_SPM_INT_EN;
  252. val = enable ? msk : 0;
  253. break;
  254. default:
  255. return;
  256. }
  257. regmap_update_bits(mtk->uwk, reg, msk, val);
  258. }
  259. static int usb_wakeup_of_property_parse(struct xhci_hcd_mtk *mtk,
  260. struct device_node *dn)
  261. {
  262. struct of_phandle_args args;
  263. int ret;
  264. /* Wakeup function is optional */
  265. mtk->uwk_en = of_property_read_bool(dn, "wakeup-source");
  266. if (!mtk->uwk_en)
  267. return 0;
  268. ret = of_parse_phandle_with_fixed_args(dn,
  269. "mediatek,syscon-wakeup", 2, 0, &args);
  270. if (ret)
  271. return ret;
  272. mtk->uwk_reg_base = args.args[0];
  273. mtk->uwk_vers = args.args[1];
  274. mtk->uwk = syscon_node_to_regmap(args.np);
  275. of_node_put(args.np);
  276. dev_info(mtk->dev, "uwk - reg:0x%x, version:%d\n",
  277. mtk->uwk_reg_base, mtk->uwk_vers);
  278. return PTR_ERR_OR_ZERO(mtk->uwk);
  279. }
  280. static void usb_wakeup_set(struct xhci_hcd_mtk *mtk, bool enable)
  281. {
  282. if (mtk->uwk_en)
  283. usb_wakeup_ip_sleep_set(mtk, enable);
  284. }
  285. static int xhci_mtk_setup(struct usb_hcd *hcd);
  286. static const struct xhci_driver_overrides xhci_mtk_overrides __initconst = {
  287. .reset = xhci_mtk_setup,
  288. };
  289. static struct hc_driver __read_mostly xhci_mtk_hc_driver;
  290. static int xhci_mtk_ldos_enable(struct xhci_hcd_mtk *mtk)
  291. {
  292. int ret;
  293. ret = regulator_enable(mtk->vbus);
  294. if (ret) {
  295. dev_err(mtk->dev, "failed to enable vbus\n");
  296. return ret;
  297. }
  298. ret = regulator_enable(mtk->vusb33);
  299. if (ret) {
  300. dev_err(mtk->dev, "failed to enable vusb33\n");
  301. regulator_disable(mtk->vbus);
  302. return ret;
  303. }
  304. return 0;
  305. }
  306. static void xhci_mtk_ldos_disable(struct xhci_hcd_mtk *mtk)
  307. {
  308. regulator_disable(mtk->vbus);
  309. regulator_disable(mtk->vusb33);
  310. }
  311. static void xhci_mtk_quirks(struct device *dev, struct xhci_hcd *xhci)
  312. {
  313. struct usb_hcd *hcd = xhci_to_hcd(xhci);
  314. struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
  315. /*
  316. * As of now platform drivers don't provide MSI support so we ensure
  317. * here that the generic code does not try to make a pci_dev from our
  318. * dev struct in order to setup MSI
  319. */
  320. xhci->quirks |= XHCI_PLAT;
  321. xhci->quirks |= XHCI_MTK_HOST;
  322. /*
  323. * MTK host controller gives a spurious successful event after a
  324. * short transfer. Ignore it.
  325. */
  326. xhci->quirks |= XHCI_SPURIOUS_SUCCESS;
  327. if (mtk->lpm_support)
  328. xhci->quirks |= XHCI_LPM_SUPPORT;
  329. if (mtk->u2_lpm_disable)
  330. xhci->quirks |= XHCI_HW_LPM_DISABLE;
  331. /*
  332. * MTK xHCI 0.96: PSA is 1 by default even if doesn't support stream,
  333. * and it's 3 when support it.
  334. */
  335. if (xhci->hci_version < 0x100 && HCC_MAX_PSA(xhci->hcc_params) == 4)
  336. xhci->quirks |= XHCI_BROKEN_STREAMS;
  337. }
  338. /* called during probe() after chip reset completes */
  339. static int xhci_mtk_setup(struct usb_hcd *hcd)
  340. {
  341. struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
  342. int ret;
  343. if (usb_hcd_is_primary_hcd(hcd)) {
  344. ret = xhci_mtk_ssusb_config(mtk);
  345. if (ret)
  346. return ret;
  347. }
  348. ret = xhci_gen_setup(hcd, xhci_mtk_quirks);
  349. if (ret)
  350. return ret;
  351. if (usb_hcd_is_primary_hcd(hcd)) {
  352. ret = xhci_mtk_sch_init(mtk);
  353. if (ret)
  354. return ret;
  355. }
  356. return ret;
  357. }
  358. static int xhci_mtk_probe(struct platform_device *pdev)
  359. {
  360. struct device *dev = &pdev->dev;
  361. struct device_node *node = dev->of_node;
  362. struct xhci_hcd_mtk *mtk;
  363. const struct hc_driver *driver;
  364. struct xhci_hcd *xhci;
  365. struct resource *res;
  366. struct usb_hcd *hcd;
  367. int ret = -ENODEV;
  368. int irq;
  369. if (usb_disabled())
  370. return -ENODEV;
  371. driver = &xhci_mtk_hc_driver;
  372. mtk = devm_kzalloc(dev, sizeof(*mtk), GFP_KERNEL);
  373. if (!mtk)
  374. return -ENOMEM;
  375. mtk->dev = dev;
  376. mtk->vbus = devm_regulator_get(dev, "vbus");
  377. if (IS_ERR(mtk->vbus)) {
  378. dev_err(dev, "fail to get vbus\n");
  379. return PTR_ERR(mtk->vbus);
  380. }
  381. mtk->vusb33 = devm_regulator_get(dev, "vusb33");
  382. if (IS_ERR(mtk->vusb33)) {
  383. dev_err(dev, "fail to get vusb33\n");
  384. return PTR_ERR(mtk->vusb33);
  385. }
  386. ret = xhci_mtk_clks_get(mtk);
  387. if (ret)
  388. return ret;
  389. mtk->lpm_support = of_property_read_bool(node, "usb3-lpm-capable");
  390. mtk->u2_lpm_disable = of_property_read_bool(node, "usb2-lpm-disable");
  391. /* optional property, ignore the error if it does not exist */
  392. of_property_read_u32(node, "mediatek,u3p-dis-msk",
  393. &mtk->u3p_dis_msk);
  394. ret = usb_wakeup_of_property_parse(mtk, node);
  395. if (ret) {
  396. dev_err(dev, "failed to parse uwk property\n");
  397. return ret;
  398. }
  399. pm_runtime_enable(dev);
  400. pm_runtime_get_sync(dev);
  401. device_enable_async_suspend(dev);
  402. ret = xhci_mtk_ldos_enable(mtk);
  403. if (ret)
  404. goto disable_pm;
  405. ret = xhci_mtk_clks_enable(mtk);
  406. if (ret)
  407. goto disable_ldos;
  408. irq = platform_get_irq(pdev, 0);
  409. if (irq < 0) {
  410. ret = irq;
  411. goto disable_clk;
  412. }
  413. /* Initialize dma_mask and coherent_dma_mask to 32-bits */
  414. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
  415. if (ret)
  416. goto disable_clk;
  417. hcd = usb_create_hcd(driver, dev, dev_name(dev));
  418. if (!hcd) {
  419. ret = -ENOMEM;
  420. goto disable_clk;
  421. }
  422. /*
  423. * USB 2.0 roothub is stored in the platform_device.
  424. * Swap it with mtk HCD.
  425. */
  426. mtk->hcd = platform_get_drvdata(pdev);
  427. platform_set_drvdata(pdev, mtk);
  428. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mac");
  429. hcd->regs = devm_ioremap_resource(dev, res);
  430. if (IS_ERR(hcd->regs)) {
  431. ret = PTR_ERR(hcd->regs);
  432. goto put_usb2_hcd;
  433. }
  434. hcd->rsrc_start = res->start;
  435. hcd->rsrc_len = resource_size(res);
  436. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ippc");
  437. if (res) { /* ippc register is optional */
  438. mtk->ippc_regs = devm_ioremap_resource(dev, res);
  439. if (IS_ERR(mtk->ippc_regs)) {
  440. ret = PTR_ERR(mtk->ippc_regs);
  441. goto put_usb2_hcd;
  442. }
  443. mtk->has_ippc = true;
  444. } else {
  445. mtk->has_ippc = false;
  446. }
  447. device_init_wakeup(dev, true);
  448. xhci = hcd_to_xhci(hcd);
  449. xhci->main_hcd = hcd;
  450. /*
  451. * imod_interval is the interrupt moderation value in nanoseconds.
  452. * The increment interval is 8 times as much as that defined in
  453. * the xHCI spec on MTK's controller.
  454. */
  455. xhci->imod_interval = 5000;
  456. device_property_read_u32(dev, "imod-interval-ns", &xhci->imod_interval);
  457. xhci->shared_hcd = usb_create_shared_hcd(driver, dev,
  458. dev_name(dev), hcd);
  459. if (!xhci->shared_hcd) {
  460. ret = -ENOMEM;
  461. goto disable_device_wakeup;
  462. }
  463. ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
  464. if (ret)
  465. goto put_usb3_hcd;
  466. if (HCC_MAX_PSA(xhci->hcc_params) >= 4 &&
  467. !(xhci->quirks & XHCI_BROKEN_STREAMS))
  468. xhci->shared_hcd->can_do_streams = 1;
  469. ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
  470. if (ret)
  471. goto dealloc_usb2_hcd;
  472. return 0;
  473. dealloc_usb2_hcd:
  474. usb_remove_hcd(hcd);
  475. put_usb3_hcd:
  476. xhci_mtk_sch_exit(mtk);
  477. usb_put_hcd(xhci->shared_hcd);
  478. disable_device_wakeup:
  479. device_init_wakeup(dev, false);
  480. put_usb2_hcd:
  481. usb_put_hcd(hcd);
  482. disable_clk:
  483. xhci_mtk_clks_disable(mtk);
  484. disable_ldos:
  485. xhci_mtk_ldos_disable(mtk);
  486. disable_pm:
  487. pm_runtime_put_sync(dev);
  488. pm_runtime_disable(dev);
  489. return ret;
  490. }
  491. static int xhci_mtk_remove(struct platform_device *dev)
  492. {
  493. struct xhci_hcd_mtk *mtk = platform_get_drvdata(dev);
  494. struct usb_hcd *hcd = mtk->hcd;
  495. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  496. struct usb_hcd *shared_hcd = xhci->shared_hcd;
  497. pm_runtime_put_noidle(&dev->dev);
  498. pm_runtime_disable(&dev->dev);
  499. usb_remove_hcd(shared_hcd);
  500. xhci->shared_hcd = NULL;
  501. device_init_wakeup(&dev->dev, false);
  502. usb_remove_hcd(hcd);
  503. usb_put_hcd(shared_hcd);
  504. usb_put_hcd(hcd);
  505. xhci_mtk_sch_exit(mtk);
  506. xhci_mtk_clks_disable(mtk);
  507. xhci_mtk_ldos_disable(mtk);
  508. return 0;
  509. }
  510. /*
  511. * if ip sleep fails, and all clocks are disabled, access register will hang
  512. * AHB bus, so stop polling roothubs to avoid regs access on bus suspend.
  513. * and no need to check whether ip sleep failed or not; this will cause SPM
  514. * to wake up system immediately after system suspend complete if ip sleep
  515. * fails, it is what we wanted.
  516. */
  517. static int __maybe_unused xhci_mtk_suspend(struct device *dev)
  518. {
  519. struct xhci_hcd_mtk *mtk = dev_get_drvdata(dev);
  520. struct usb_hcd *hcd = mtk->hcd;
  521. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  522. xhci_dbg(xhci, "%s: stop port polling\n", __func__);
  523. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  524. del_timer_sync(&hcd->rh_timer);
  525. clear_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags);
  526. del_timer_sync(&xhci->shared_hcd->rh_timer);
  527. xhci_mtk_host_disable(mtk);
  528. xhci_mtk_clks_disable(mtk);
  529. usb_wakeup_set(mtk, true);
  530. return 0;
  531. }
  532. static int __maybe_unused xhci_mtk_resume(struct device *dev)
  533. {
  534. struct xhci_hcd_mtk *mtk = dev_get_drvdata(dev);
  535. struct usb_hcd *hcd = mtk->hcd;
  536. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  537. usb_wakeup_set(mtk, false);
  538. xhci_mtk_clks_enable(mtk);
  539. xhci_mtk_host_enable(mtk);
  540. xhci_dbg(xhci, "%s: restart port polling\n", __func__);
  541. set_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags);
  542. usb_hcd_poll_rh_status(xhci->shared_hcd);
  543. set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  544. usb_hcd_poll_rh_status(hcd);
  545. return 0;
  546. }
  547. static const struct dev_pm_ops xhci_mtk_pm_ops = {
  548. SET_SYSTEM_SLEEP_PM_OPS(xhci_mtk_suspend, xhci_mtk_resume)
  549. };
  550. #define DEV_PM_OPS IS_ENABLED(CONFIG_PM) ? &xhci_mtk_pm_ops : NULL
  551. #ifdef CONFIG_OF
  552. static const struct of_device_id mtk_xhci_of_match[] = {
  553. { .compatible = "mediatek,mt8173-xhci"},
  554. { .compatible = "mediatek,mtk-xhci"},
  555. { },
  556. };
  557. MODULE_DEVICE_TABLE(of, mtk_xhci_of_match);
  558. #endif
  559. static struct platform_driver mtk_xhci_driver = {
  560. .probe = xhci_mtk_probe,
  561. .remove = xhci_mtk_remove,
  562. .driver = {
  563. .name = "xhci-mtk",
  564. .pm = DEV_PM_OPS,
  565. .of_match_table = of_match_ptr(mtk_xhci_of_match),
  566. },
  567. };
  568. MODULE_ALIAS("platform:xhci-mtk");
  569. static int __init xhci_mtk_init(void)
  570. {
  571. xhci_init_driver(&xhci_mtk_hc_driver, &xhci_mtk_overrides);
  572. return platform_driver_register(&mtk_xhci_driver);
  573. }
  574. module_init(xhci_mtk_init);
  575. static void __exit xhci_mtk_exit(void)
  576. {
  577. platform_driver_unregister(&mtk_xhci_driver);
  578. }
  579. module_exit(xhci_mtk_exit);
  580. MODULE_AUTHOR("Chunfeng Yun <chunfeng.yun@mediatek.com>");
  581. MODULE_DESCRIPTION("MediaTek xHCI Host Controller Driver");
  582. MODULE_LICENSE("GPL v2");