xhci-mtk.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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. static int xhci_mtk_clks_get(struct xhci_hcd_mtk *mtk)
  173. {
  174. struct device *dev = mtk->dev;
  175. mtk->sys_clk = devm_clk_get(dev, "sys_ck");
  176. if (IS_ERR(mtk->sys_clk)) {
  177. dev_err(dev, "fail to get sys_ck\n");
  178. return PTR_ERR(mtk->sys_clk);
  179. }
  180. mtk->xhci_clk = devm_clk_get_optional(dev, "xhci_ck");
  181. if (IS_ERR(mtk->xhci_clk))
  182. return PTR_ERR(mtk->xhci_clk);
  183. mtk->ref_clk = devm_clk_get_optional(dev, "ref_ck");
  184. if (IS_ERR(mtk->ref_clk))
  185. return PTR_ERR(mtk->ref_clk);
  186. mtk->mcu_clk = devm_clk_get_optional(dev, "mcu_ck");
  187. if (IS_ERR(mtk->mcu_clk))
  188. return PTR_ERR(mtk->mcu_clk);
  189. mtk->dma_clk = devm_clk_get_optional(dev, "dma_ck");
  190. return PTR_ERR_OR_ZERO(mtk->dma_clk);
  191. }
  192. static int xhci_mtk_clks_enable(struct xhci_hcd_mtk *mtk)
  193. {
  194. int ret;
  195. ret = clk_prepare_enable(mtk->ref_clk);
  196. if (ret) {
  197. dev_err(mtk->dev, "failed to enable ref_clk\n");
  198. goto ref_clk_err;
  199. }
  200. ret = clk_prepare_enable(mtk->sys_clk);
  201. if (ret) {
  202. dev_err(mtk->dev, "failed to enable sys_clk\n");
  203. goto sys_clk_err;
  204. }
  205. ret = clk_prepare_enable(mtk->xhci_clk);
  206. if (ret) {
  207. dev_err(mtk->dev, "failed to enable xhci_clk\n");
  208. goto xhci_clk_err;
  209. }
  210. ret = clk_prepare_enable(mtk->mcu_clk);
  211. if (ret) {
  212. dev_err(mtk->dev, "failed to enable mcu_clk\n");
  213. goto mcu_clk_err;
  214. }
  215. ret = clk_prepare_enable(mtk->dma_clk);
  216. if (ret) {
  217. dev_err(mtk->dev, "failed to enable dma_clk\n");
  218. goto dma_clk_err;
  219. }
  220. return 0;
  221. dma_clk_err:
  222. clk_disable_unprepare(mtk->mcu_clk);
  223. mcu_clk_err:
  224. clk_disable_unprepare(mtk->xhci_clk);
  225. xhci_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->xhci_clk);
  237. clk_disable_unprepare(mtk->sys_clk);
  238. clk_disable_unprepare(mtk->ref_clk);
  239. }
  240. /* only clocks can be turn off for ip-sleep wakeup mode */
  241. static void usb_wakeup_ip_sleep_set(struct xhci_hcd_mtk *mtk, bool enable)
  242. {
  243. u32 reg, msk, val;
  244. switch (mtk->uwk_vers) {
  245. case SSUSB_UWK_V1:
  246. reg = mtk->uwk_reg_base + PERI_WK_CTRL1;
  247. msk = WC1_IS_EN | WC1_IS_C(0xf) | WC1_IS_P;
  248. val = enable ? (WC1_IS_EN | WC1_IS_C(0x8)) : 0;
  249. break;
  250. case SSUSB_UWK_V2:
  251. reg = mtk->uwk_reg_base + PERI_SSUSB_SPM_CTRL;
  252. msk = SSC_IP_SLEEP_EN | SSC_SPM_INT_EN;
  253. val = enable ? msk : 0;
  254. break;
  255. default:
  256. return;
  257. }
  258. regmap_update_bits(mtk->uwk, reg, msk, val);
  259. }
  260. static int usb_wakeup_of_property_parse(struct xhci_hcd_mtk *mtk,
  261. struct device_node *dn)
  262. {
  263. struct of_phandle_args args;
  264. int ret;
  265. /* Wakeup function is optional */
  266. mtk->uwk_en = of_property_read_bool(dn, "wakeup-source");
  267. if (!mtk->uwk_en)
  268. return 0;
  269. ret = of_parse_phandle_with_fixed_args(dn,
  270. "mediatek,syscon-wakeup", 2, 0, &args);
  271. if (ret)
  272. return ret;
  273. mtk->uwk_reg_base = args.args[0];
  274. mtk->uwk_vers = args.args[1];
  275. mtk->uwk = syscon_node_to_regmap(args.np);
  276. of_node_put(args.np);
  277. dev_info(mtk->dev, "uwk - reg:0x%x, version:%d\n",
  278. mtk->uwk_reg_base, mtk->uwk_vers);
  279. return PTR_ERR_OR_ZERO(mtk->uwk);
  280. }
  281. static void usb_wakeup_set(struct xhci_hcd_mtk *mtk, bool enable)
  282. {
  283. if (mtk->uwk_en)
  284. usb_wakeup_ip_sleep_set(mtk, enable);
  285. }
  286. static int xhci_mtk_setup(struct usb_hcd *hcd);
  287. static const struct xhci_driver_overrides xhci_mtk_overrides __initconst = {
  288. .reset = xhci_mtk_setup,
  289. .check_bandwidth = xhci_mtk_check_bandwidth,
  290. .reset_bandwidth = xhci_mtk_reset_bandwidth,
  291. };
  292. static struct hc_driver __read_mostly xhci_mtk_hc_driver;
  293. static int xhci_mtk_ldos_enable(struct xhci_hcd_mtk *mtk)
  294. {
  295. int ret;
  296. ret = regulator_enable(mtk->vbus);
  297. if (ret) {
  298. dev_err(mtk->dev, "failed to enable vbus\n");
  299. return ret;
  300. }
  301. ret = regulator_enable(mtk->vusb33);
  302. if (ret) {
  303. dev_err(mtk->dev, "failed to enable vusb33\n");
  304. regulator_disable(mtk->vbus);
  305. return ret;
  306. }
  307. return 0;
  308. }
  309. static void xhci_mtk_ldos_disable(struct xhci_hcd_mtk *mtk)
  310. {
  311. regulator_disable(mtk->vbus);
  312. regulator_disable(mtk->vusb33);
  313. }
  314. static void xhci_mtk_quirks(struct device *dev, struct xhci_hcd *xhci)
  315. {
  316. struct usb_hcd *hcd = xhci_to_hcd(xhci);
  317. struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
  318. /*
  319. * As of now platform drivers don't provide MSI support so we ensure
  320. * here that the generic code does not try to make a pci_dev from our
  321. * dev struct in order to setup MSI
  322. */
  323. xhci->quirks |= XHCI_PLAT;
  324. xhci->quirks |= XHCI_MTK_HOST;
  325. /*
  326. * MTK host controller gives a spurious successful event after a
  327. * short transfer. Ignore it.
  328. */
  329. xhci->quirks |= XHCI_SPURIOUS_SUCCESS;
  330. if (mtk->lpm_support)
  331. xhci->quirks |= XHCI_LPM_SUPPORT;
  332. if (mtk->u2_lpm_disable)
  333. xhci->quirks |= XHCI_HW_LPM_DISABLE;
  334. /*
  335. * MTK xHCI 0.96: PSA is 1 by default even if doesn't support stream,
  336. * and it's 3 when support it.
  337. */
  338. if (xhci->hci_version < 0x100 && HCC_MAX_PSA(xhci->hcc_params) == 4)
  339. xhci->quirks |= XHCI_BROKEN_STREAMS;
  340. }
  341. /* called during probe() after chip reset completes */
  342. static int xhci_mtk_setup(struct usb_hcd *hcd)
  343. {
  344. struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
  345. int ret;
  346. if (usb_hcd_is_primary_hcd(hcd)) {
  347. ret = xhci_mtk_ssusb_config(mtk);
  348. if (ret)
  349. return ret;
  350. }
  351. ret = xhci_gen_setup(hcd, xhci_mtk_quirks);
  352. if (ret)
  353. return ret;
  354. if (usb_hcd_is_primary_hcd(hcd)) {
  355. ret = xhci_mtk_sch_init(mtk);
  356. if (ret)
  357. return ret;
  358. }
  359. return ret;
  360. }
  361. static int xhci_mtk_probe(struct platform_device *pdev)
  362. {
  363. struct device *dev = &pdev->dev;
  364. struct device_node *node = dev->of_node;
  365. struct xhci_hcd_mtk *mtk;
  366. const struct hc_driver *driver;
  367. struct xhci_hcd *xhci;
  368. struct resource *res;
  369. struct usb_hcd *hcd;
  370. int ret = -ENODEV;
  371. int irq;
  372. if (usb_disabled())
  373. return -ENODEV;
  374. driver = &xhci_mtk_hc_driver;
  375. mtk = devm_kzalloc(dev, sizeof(*mtk), GFP_KERNEL);
  376. if (!mtk)
  377. return -ENOMEM;
  378. mtk->dev = dev;
  379. mtk->vbus = devm_regulator_get(dev, "vbus");
  380. if (IS_ERR(mtk->vbus)) {
  381. dev_err(dev, "fail to get vbus\n");
  382. return PTR_ERR(mtk->vbus);
  383. }
  384. mtk->vusb33 = devm_regulator_get(dev, "vusb33");
  385. if (IS_ERR(mtk->vusb33)) {
  386. dev_err(dev, "fail to get vusb33\n");
  387. return PTR_ERR(mtk->vusb33);
  388. }
  389. ret = xhci_mtk_clks_get(mtk);
  390. if (ret)
  391. return ret;
  392. mtk->lpm_support = of_property_read_bool(node, "usb3-lpm-capable");
  393. mtk->u2_lpm_disable = of_property_read_bool(node, "usb2-lpm-disable");
  394. /* optional property, ignore the error if it does not exist */
  395. of_property_read_u32(node, "mediatek,u3p-dis-msk",
  396. &mtk->u3p_dis_msk);
  397. ret = usb_wakeup_of_property_parse(mtk, node);
  398. if (ret) {
  399. dev_err(dev, "failed to parse uwk property\n");
  400. return ret;
  401. }
  402. pm_runtime_enable(dev);
  403. pm_runtime_get_sync(dev);
  404. device_enable_async_suspend(dev);
  405. ret = xhci_mtk_ldos_enable(mtk);
  406. if (ret)
  407. goto disable_pm;
  408. ret = xhci_mtk_clks_enable(mtk);
  409. if (ret)
  410. goto disable_ldos;
  411. irq = platform_get_irq(pdev, 0);
  412. if (irq < 0) {
  413. ret = irq;
  414. goto disable_clk;
  415. }
  416. /* Initialize dma_mask and coherent_dma_mask to 32-bits */
  417. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
  418. if (ret)
  419. goto disable_clk;
  420. hcd = usb_create_hcd(driver, dev, dev_name(dev));
  421. if (!hcd) {
  422. ret = -ENOMEM;
  423. goto disable_clk;
  424. }
  425. /*
  426. * USB 2.0 roothub is stored in the platform_device.
  427. * Swap it with mtk HCD.
  428. */
  429. mtk->hcd = platform_get_drvdata(pdev);
  430. platform_set_drvdata(pdev, mtk);
  431. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mac");
  432. hcd->regs = devm_ioremap_resource(dev, res);
  433. if (IS_ERR(hcd->regs)) {
  434. ret = PTR_ERR(hcd->regs);
  435. goto put_usb2_hcd;
  436. }
  437. hcd->rsrc_start = res->start;
  438. hcd->rsrc_len = resource_size(res);
  439. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ippc");
  440. if (res) { /* ippc register is optional */
  441. mtk->ippc_regs = devm_ioremap_resource(dev, res);
  442. if (IS_ERR(mtk->ippc_regs)) {
  443. ret = PTR_ERR(mtk->ippc_regs);
  444. goto put_usb2_hcd;
  445. }
  446. mtk->has_ippc = true;
  447. } else {
  448. mtk->has_ippc = false;
  449. }
  450. device_init_wakeup(dev, true);
  451. xhci = hcd_to_xhci(hcd);
  452. xhci->main_hcd = hcd;
  453. /*
  454. * imod_interval is the interrupt moderation value in nanoseconds.
  455. * The increment interval is 8 times as much as that defined in
  456. * the xHCI spec on MTK's controller.
  457. */
  458. xhci->imod_interval = 5000;
  459. device_property_read_u32(dev, "imod-interval-ns", &xhci->imod_interval);
  460. xhci->shared_hcd = usb_create_shared_hcd(driver, dev,
  461. dev_name(dev), hcd);
  462. if (!xhci->shared_hcd) {
  463. ret = -ENOMEM;
  464. goto disable_device_wakeup;
  465. }
  466. ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
  467. if (ret)
  468. goto put_usb3_hcd;
  469. if (HCC_MAX_PSA(xhci->hcc_params) >= 4 &&
  470. !(xhci->quirks & XHCI_BROKEN_STREAMS))
  471. xhci->shared_hcd->can_do_streams = 1;
  472. ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
  473. if (ret)
  474. goto dealloc_usb2_hcd;
  475. return 0;
  476. dealloc_usb2_hcd:
  477. usb_remove_hcd(hcd);
  478. put_usb3_hcd:
  479. xhci_mtk_sch_exit(mtk);
  480. usb_put_hcd(xhci->shared_hcd);
  481. disable_device_wakeup:
  482. device_init_wakeup(dev, false);
  483. put_usb2_hcd:
  484. usb_put_hcd(hcd);
  485. disable_clk:
  486. xhci_mtk_clks_disable(mtk);
  487. disable_ldos:
  488. xhci_mtk_ldos_disable(mtk);
  489. disable_pm:
  490. pm_runtime_put_sync(dev);
  491. pm_runtime_disable(dev);
  492. return ret;
  493. }
  494. static int xhci_mtk_remove(struct platform_device *dev)
  495. {
  496. struct xhci_hcd_mtk *mtk = platform_get_drvdata(dev);
  497. struct usb_hcd *hcd = mtk->hcd;
  498. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  499. struct usb_hcd *shared_hcd = xhci->shared_hcd;
  500. pm_runtime_put_noidle(&dev->dev);
  501. pm_runtime_disable(&dev->dev);
  502. usb_remove_hcd(shared_hcd);
  503. xhci->shared_hcd = NULL;
  504. device_init_wakeup(&dev->dev, false);
  505. usb_remove_hcd(hcd);
  506. usb_put_hcd(shared_hcd);
  507. usb_put_hcd(hcd);
  508. xhci_mtk_sch_exit(mtk);
  509. xhci_mtk_clks_disable(mtk);
  510. xhci_mtk_ldos_disable(mtk);
  511. return 0;
  512. }
  513. /*
  514. * if ip sleep fails, and all clocks are disabled, access register will hang
  515. * AHB bus, so stop polling roothubs to avoid regs access on bus suspend.
  516. * and no need to check whether ip sleep failed or not; this will cause SPM
  517. * to wake up system immediately after system suspend complete if ip sleep
  518. * fails, it is what we wanted.
  519. */
  520. static int __maybe_unused xhci_mtk_suspend(struct device *dev)
  521. {
  522. struct xhci_hcd_mtk *mtk = dev_get_drvdata(dev);
  523. struct usb_hcd *hcd = mtk->hcd;
  524. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  525. xhci_dbg(xhci, "%s: stop port polling\n", __func__);
  526. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  527. del_timer_sync(&hcd->rh_timer);
  528. clear_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags);
  529. del_timer_sync(&xhci->shared_hcd->rh_timer);
  530. xhci_mtk_host_disable(mtk);
  531. xhci_mtk_clks_disable(mtk);
  532. usb_wakeup_set(mtk, true);
  533. return 0;
  534. }
  535. static int __maybe_unused xhci_mtk_resume(struct device *dev)
  536. {
  537. struct xhci_hcd_mtk *mtk = dev_get_drvdata(dev);
  538. struct usb_hcd *hcd = mtk->hcd;
  539. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  540. usb_wakeup_set(mtk, false);
  541. xhci_mtk_clks_enable(mtk);
  542. xhci_mtk_host_enable(mtk);
  543. xhci_dbg(xhci, "%s: restart port polling\n", __func__);
  544. set_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags);
  545. usb_hcd_poll_rh_status(xhci->shared_hcd);
  546. set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  547. usb_hcd_poll_rh_status(hcd);
  548. return 0;
  549. }
  550. static const struct dev_pm_ops xhci_mtk_pm_ops = {
  551. SET_SYSTEM_SLEEP_PM_OPS(xhci_mtk_suspend, xhci_mtk_resume)
  552. };
  553. #define DEV_PM_OPS IS_ENABLED(CONFIG_PM) ? &xhci_mtk_pm_ops : NULL
  554. #ifdef CONFIG_OF
  555. static const struct of_device_id mtk_xhci_of_match[] = {
  556. { .compatible = "mediatek,mt8173-xhci"},
  557. { .compatible = "mediatek,mtk-xhci"},
  558. { },
  559. };
  560. MODULE_DEVICE_TABLE(of, mtk_xhci_of_match);
  561. #endif
  562. static struct platform_driver mtk_xhci_driver = {
  563. .probe = xhci_mtk_probe,
  564. .remove = xhci_mtk_remove,
  565. .driver = {
  566. .name = "xhci-mtk",
  567. .pm = DEV_PM_OPS,
  568. .of_match_table = of_match_ptr(mtk_xhci_of_match),
  569. },
  570. };
  571. MODULE_ALIAS("platform:xhci-mtk");
  572. static int __init xhci_mtk_init(void)
  573. {
  574. xhci_init_driver(&xhci_mtk_hc_driver, &xhci_mtk_overrides);
  575. return platform_driver_register(&mtk_xhci_driver);
  576. }
  577. module_init(xhci_mtk_init);
  578. static void __exit xhci_mtk_exit(void)
  579. {
  580. platform_driver_unregister(&mtk_xhci_driver);
  581. }
  582. module_exit(xhci_mtk_exit);
  583. MODULE_AUTHOR("Chunfeng Yun <chunfeng.yun@mediatek.com>");
  584. MODULE_DESCRIPTION("MediaTek xHCI Host Controller Driver");
  585. MODULE_LICENSE("GPL v2");