xhci-mtk.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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. }
  330. /* called during probe() after chip reset completes */
  331. static int xhci_mtk_setup(struct usb_hcd *hcd)
  332. {
  333. struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
  334. int ret;
  335. if (usb_hcd_is_primary_hcd(hcd)) {
  336. ret = xhci_mtk_ssusb_config(mtk);
  337. if (ret)
  338. return ret;
  339. }
  340. ret = xhci_gen_setup(hcd, xhci_mtk_quirks);
  341. if (ret)
  342. return ret;
  343. if (usb_hcd_is_primary_hcd(hcd)) {
  344. ret = xhci_mtk_sch_init(mtk);
  345. if (ret)
  346. return ret;
  347. }
  348. return ret;
  349. }
  350. static int xhci_mtk_probe(struct platform_device *pdev)
  351. {
  352. struct device *dev = &pdev->dev;
  353. struct device_node *node = dev->of_node;
  354. struct xhci_hcd_mtk *mtk;
  355. const struct hc_driver *driver;
  356. struct xhci_hcd *xhci;
  357. struct resource *res;
  358. struct usb_hcd *hcd;
  359. int ret = -ENODEV;
  360. int irq;
  361. if (usb_disabled())
  362. return -ENODEV;
  363. driver = &xhci_mtk_hc_driver;
  364. mtk = devm_kzalloc(dev, sizeof(*mtk), GFP_KERNEL);
  365. if (!mtk)
  366. return -ENOMEM;
  367. mtk->dev = dev;
  368. mtk->vbus = devm_regulator_get(dev, "vbus");
  369. if (IS_ERR(mtk->vbus)) {
  370. dev_err(dev, "fail to get vbus\n");
  371. return PTR_ERR(mtk->vbus);
  372. }
  373. mtk->vusb33 = devm_regulator_get(dev, "vusb33");
  374. if (IS_ERR(mtk->vusb33)) {
  375. dev_err(dev, "fail to get vusb33\n");
  376. return PTR_ERR(mtk->vusb33);
  377. }
  378. ret = xhci_mtk_clks_get(mtk);
  379. if (ret)
  380. return ret;
  381. mtk->lpm_support = of_property_read_bool(node, "usb3-lpm-capable");
  382. /* optional property, ignore the error if it does not exist */
  383. of_property_read_u32(node, "mediatek,u3p-dis-msk",
  384. &mtk->u3p_dis_msk);
  385. ret = usb_wakeup_of_property_parse(mtk, node);
  386. if (ret) {
  387. dev_err(dev, "failed to parse uwk property\n");
  388. return ret;
  389. }
  390. pm_runtime_enable(dev);
  391. pm_runtime_get_sync(dev);
  392. device_enable_async_suspend(dev);
  393. ret = xhci_mtk_ldos_enable(mtk);
  394. if (ret)
  395. goto disable_pm;
  396. ret = xhci_mtk_clks_enable(mtk);
  397. if (ret)
  398. goto disable_ldos;
  399. irq = platform_get_irq(pdev, 0);
  400. if (irq < 0) {
  401. ret = irq;
  402. goto disable_clk;
  403. }
  404. /* Initialize dma_mask and coherent_dma_mask to 32-bits */
  405. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
  406. if (ret)
  407. goto disable_clk;
  408. hcd = usb_create_hcd(driver, dev, dev_name(dev));
  409. if (!hcd) {
  410. ret = -ENOMEM;
  411. goto disable_clk;
  412. }
  413. /*
  414. * USB 2.0 roothub is stored in the platform_device.
  415. * Swap it with mtk HCD.
  416. */
  417. mtk->hcd = platform_get_drvdata(pdev);
  418. platform_set_drvdata(pdev, mtk);
  419. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mac");
  420. hcd->regs = devm_ioremap_resource(dev, res);
  421. if (IS_ERR(hcd->regs)) {
  422. ret = PTR_ERR(hcd->regs);
  423. goto put_usb2_hcd;
  424. }
  425. hcd->rsrc_start = res->start;
  426. hcd->rsrc_len = resource_size(res);
  427. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ippc");
  428. if (res) { /* ippc register is optional */
  429. mtk->ippc_regs = devm_ioremap_resource(dev, res);
  430. if (IS_ERR(mtk->ippc_regs)) {
  431. ret = PTR_ERR(mtk->ippc_regs);
  432. goto put_usb2_hcd;
  433. }
  434. mtk->has_ippc = true;
  435. } else {
  436. mtk->has_ippc = false;
  437. }
  438. device_init_wakeup(dev, true);
  439. xhci = hcd_to_xhci(hcd);
  440. xhci->main_hcd = hcd;
  441. /*
  442. * imod_interval is the interrupt moderation value in nanoseconds.
  443. * The increment interval is 8 times as much as that defined in
  444. * the xHCI spec on MTK's controller.
  445. */
  446. xhci->imod_interval = 5000;
  447. device_property_read_u32(dev, "imod-interval-ns", &xhci->imod_interval);
  448. xhci->shared_hcd = usb_create_shared_hcd(driver, dev,
  449. dev_name(dev), hcd);
  450. if (!xhci->shared_hcd) {
  451. ret = -ENOMEM;
  452. goto disable_device_wakeup;
  453. }
  454. ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
  455. if (ret)
  456. goto put_usb3_hcd;
  457. if (HCC_MAX_PSA(xhci->hcc_params) >= 4)
  458. xhci->shared_hcd->can_do_streams = 1;
  459. ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
  460. if (ret)
  461. goto dealloc_usb2_hcd;
  462. return 0;
  463. dealloc_usb2_hcd:
  464. usb_remove_hcd(hcd);
  465. put_usb3_hcd:
  466. xhci_mtk_sch_exit(mtk);
  467. usb_put_hcd(xhci->shared_hcd);
  468. disable_device_wakeup:
  469. device_init_wakeup(dev, false);
  470. put_usb2_hcd:
  471. usb_put_hcd(hcd);
  472. disable_clk:
  473. xhci_mtk_clks_disable(mtk);
  474. disable_ldos:
  475. xhci_mtk_ldos_disable(mtk);
  476. disable_pm:
  477. pm_runtime_put_sync(dev);
  478. pm_runtime_disable(dev);
  479. return ret;
  480. }
  481. static int xhci_mtk_remove(struct platform_device *dev)
  482. {
  483. struct xhci_hcd_mtk *mtk = platform_get_drvdata(dev);
  484. struct usb_hcd *hcd = mtk->hcd;
  485. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  486. struct usb_hcd *shared_hcd = xhci->shared_hcd;
  487. usb_remove_hcd(shared_hcd);
  488. xhci->shared_hcd = NULL;
  489. device_init_wakeup(&dev->dev, false);
  490. usb_remove_hcd(hcd);
  491. usb_put_hcd(shared_hcd);
  492. usb_put_hcd(hcd);
  493. xhci_mtk_sch_exit(mtk);
  494. xhci_mtk_clks_disable(mtk);
  495. xhci_mtk_ldos_disable(mtk);
  496. pm_runtime_put_sync(&dev->dev);
  497. pm_runtime_disable(&dev->dev);
  498. return 0;
  499. }
  500. /*
  501. * if ip sleep fails, and all clocks are disabled, access register will hang
  502. * AHB bus, so stop polling roothubs to avoid regs access on bus suspend.
  503. * and no need to check whether ip sleep failed or not; this will cause SPM
  504. * to wake up system immediately after system suspend complete if ip sleep
  505. * fails, it is what we wanted.
  506. */
  507. static int __maybe_unused xhci_mtk_suspend(struct device *dev)
  508. {
  509. struct xhci_hcd_mtk *mtk = dev_get_drvdata(dev);
  510. struct usb_hcd *hcd = mtk->hcd;
  511. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  512. xhci_dbg(xhci, "%s: stop port polling\n", __func__);
  513. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  514. del_timer_sync(&hcd->rh_timer);
  515. clear_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags);
  516. del_timer_sync(&xhci->shared_hcd->rh_timer);
  517. xhci_mtk_host_disable(mtk);
  518. xhci_mtk_clks_disable(mtk);
  519. usb_wakeup_set(mtk, true);
  520. return 0;
  521. }
  522. static int __maybe_unused xhci_mtk_resume(struct device *dev)
  523. {
  524. struct xhci_hcd_mtk *mtk = dev_get_drvdata(dev);
  525. struct usb_hcd *hcd = mtk->hcd;
  526. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  527. usb_wakeup_set(mtk, false);
  528. xhci_mtk_clks_enable(mtk);
  529. xhci_mtk_host_enable(mtk);
  530. xhci_dbg(xhci, "%s: restart port polling\n", __func__);
  531. set_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags);
  532. usb_hcd_poll_rh_status(xhci->shared_hcd);
  533. set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  534. usb_hcd_poll_rh_status(hcd);
  535. return 0;
  536. }
  537. static const struct dev_pm_ops xhci_mtk_pm_ops = {
  538. SET_SYSTEM_SLEEP_PM_OPS(xhci_mtk_suspend, xhci_mtk_resume)
  539. };
  540. #define DEV_PM_OPS IS_ENABLED(CONFIG_PM) ? &xhci_mtk_pm_ops : NULL
  541. #ifdef CONFIG_OF
  542. static const struct of_device_id mtk_xhci_of_match[] = {
  543. { .compatible = "mediatek,mt8173-xhci"},
  544. { .compatible = "mediatek,mtk-xhci"},
  545. { },
  546. };
  547. MODULE_DEVICE_TABLE(of, mtk_xhci_of_match);
  548. #endif
  549. static struct platform_driver mtk_xhci_driver = {
  550. .probe = xhci_mtk_probe,
  551. .remove = xhci_mtk_remove,
  552. .driver = {
  553. .name = "xhci-mtk",
  554. .pm = DEV_PM_OPS,
  555. .of_match_table = of_match_ptr(mtk_xhci_of_match),
  556. },
  557. };
  558. MODULE_ALIAS("platform:xhci-mtk");
  559. static int __init xhci_mtk_init(void)
  560. {
  561. xhci_init_driver(&xhci_mtk_hc_driver, &xhci_mtk_overrides);
  562. return platform_driver_register(&mtk_xhci_driver);
  563. }
  564. module_init(xhci_mtk_init);
  565. static void __exit xhci_mtk_exit(void)
  566. {
  567. platform_driver_unregister(&mtk_xhci_driver);
  568. }
  569. module_exit(xhci_mtk_exit);
  570. MODULE_AUTHOR("Chunfeng Yun <chunfeng.yun@mediatek.com>");
  571. MODULE_DESCRIPTION("MediaTek xHCI Host Controller Driver");
  572. MODULE_LICENSE("GPL v2");